3 * MPEG file format decoder for the Wiretap library.
4 * Written by Shaun Jackman <sjackman@gmail.com>
5 * Copyright 2007 Shaun Jackman
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
34 #include "wsutil/mpeg-audio.h"
37 #include <wsutil/buffer.h>
38 #include "file_wrappers.h"
45 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
53 mpeg_resync(FILE_T fh, int *err)
55 gint64 offset = file_tell(fh);
57 int byte = file_getc(fh);
60 if (byte == 0xff && count > 0) {
62 if (byte != EOF && (byte & 0xe0) == 0xe0)
68 if (file_seek(fh, offset, SEEK_SET, err) == -1)
73 #define SCRHZ 27000000
76 mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
77 gboolean is_random, int *err, gchar **err_info)
79 mpeg_t *mpeg = (mpeg_t *)wth->priv;
81 unsigned int packet_size;
82 nstime_t ts = mpeg->now;
85 * All packets have at least 4 bytes in them. Read the first
86 * 4 bytes and determine whether it's a PES packet or not
89 * XXX - can an MPEG file contain a mixture of PES and non-PES
90 * packets? If not, can we determine whether the packets will
91 * be PES packets or not based on the magic number (i.e., if the
92 * file begins with 0x00 0x00 0x01, it contains PES packets,
93 * otherwise it doesn't)?
95 if (!wtap_read_bytes_or_eof(fh, &n, sizeof n, err, err_info))
97 if (file_seek(fh, -(gint64)(sizeof n), SEEK_CUR, err) == -1)
101 gint64 offset = file_tell(fh);
104 if (!file_skip(fh, 3, err))
107 if (!wtap_read_bytes(fh, &stream, sizeof stream, err, err_info))
110 if (stream == 0xba) {
116 if (!wtap_read_bytes(fh, &pack1, sizeof pack1, err, err_info))
118 if (!wtap_read_bytes(fh, &pack0, sizeof pack0, err, err_info))
120 pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
122 switch (pack >> 62) {
124 if (!file_skip(fh, 1, err))
126 if (!wtap_read_bytes(fh, &stuffing,
127 sizeof stuffing, err, err_info))
130 packet_size = 14 + stuffing;
133 guint64 bytes = pack >> 16;
135 (bytes >> 43 & 0x0007) << 30 |
136 (bytes >> 27 & 0x7fff) << 15 |
137 (bytes >> 11 & 0x7fff) << 0;
138 guint ext = (guint)((bytes >> 1) & 0x1ff);
139 guint64 cr = 300 * ts_val + ext;
140 guint rem = (guint)(cr % SCRHZ);
142 = mpeg->t0 + (time_t)(cr / SCRHZ);
144 = (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
153 if (!wtap_read_bytes(fh, &length, sizeof length, err, err_info))
155 length = g_ntohs(length);
156 packet_size = 6 + length;
159 if (file_seek(fh, offset, SEEK_SET, err) == -1)
164 MPA_UNMARSHAL(&mpa, n);
165 if (MPA_VALID(&mpa)) {
166 packet_size = MPA_BYTES(&mpa);
168 mpeg->now.nsecs += MPA_DURATION_NS(&mpa);
169 if (mpeg->now.nsecs >= 1000000000) {
171 mpeg->now.nsecs -= 1000000000;
175 packet_size = mpeg_resync(fh, err);
176 if (packet_size == 0)
181 if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
184 phdr->rec_type = REC_TYPE_PACKET;
186 /* XXX - relative, not absolute, time stamps */
188 phdr->presence_flags = WTAP_HAS_TS;
191 phdr->caplen = packet_size;
192 phdr->len = packet_size;
198 mpeg_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
200 *data_offset = file_tell(wth->fh);
202 return mpeg_read_packet(wth, wth->fh, &wth->phdr, wth->frame_buffer,
203 FALSE, err, err_info);
207 mpeg_seek_read(wtap *wth, gint64 seek_off,
208 struct wtap_pkthdr *phdr, Buffer *buf,
209 int *err, gchar **err_info)
211 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
214 if (!mpeg_read_packet(wth, wth->random_fh, phdr, buf, TRUE, err,
217 *err = WTAP_ERR_SHORT_READ;
227 { 3, "TAG" }, /* ID3v1 */
228 { 3, "ID3" }, /* ID3v2 */
229 { 3, "\0\0\1" }, /* MPEG PES */
230 { 2, "\xff\xfb" }, /* MP3, taken from http://en.wikipedia.org/wiki/MP3#File_structure */
235 mpeg_open(wtap *wth, int *err, gchar **err_info)
238 struct _mpeg_magic* m;
241 if (!wtap_read_bytes(wth->fh, magic_buf, sizeof magic_buf,
243 if (*err != WTAP_ERR_SHORT_READ)
244 return WTAP_OPEN_ERROR;
245 return WTAP_OPEN_NOT_MINE;
248 for (m=magic; m->match; m++) {
249 if (memcmp(magic_buf, m->match, m->len) == 0)
253 return WTAP_OPEN_NOT_MINE;
256 /* This appears to be a file with MPEG data. */
257 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
258 return WTAP_OPEN_ERROR;
260 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG;
261 wth->file_encap = WTAP_ENCAP_MPEG;
262 wth->file_tsprec = WTAP_TSPREC_NSEC;
263 wth->subtype_read = mpeg_read;
264 wth->subtype_seek_read = mpeg_seek_read;
265 wth->snapshot_length = 0;
267 mpeg = (mpeg_t *)g_malloc(sizeof(mpeg_t));
268 wth->priv = (void *)mpeg;
271 mpeg->t0 = mpeg->now.secs;
273 return WTAP_OPEN_MINE;
277 * Editor modelines - http://www.wireshark.org/tools/modelines.html
282 * indent-tabs-mode: t
285 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
286 * :indentSize=8:tabSize=8:noTabs=false: