3 * MPEG file format decoder for the Wiretap library.
4 * Written by Shaun Jackman <sjackman@gmail.com>
5 * Copyright 2007 Shaun Jackman
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
38 #include "wsutil/mpeg-audio.h"
42 #include "file_wrappers.h"
49 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
52 struct wtap_nstime now;
57 mpeg_resync(wtap *wth, int *err, gchar **err_info _U_)
59 gint64 offset = file_tell(wth->fh);
61 int byte = file_getc(wth->fh);
64 if (byte == 0xff && count > 0) {
65 byte = file_getc(wth->fh);
66 if (byte != EOF && (byte & 0xe0) == 0xe0)
69 byte = file_getc(wth->fh);
72 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
78 mpeg_read_header(wtap *wth, int *err, gchar **err_info _U_,
83 errno = WTAP_ERR_CANT_READ;
84 bytes_read = file_read(n, sizeof *n, wth->fh);
85 if (bytes_read != sizeof *n) {
86 *err = file_error(wth->fh);
87 if (*err == 0 && bytes_read != 0)
88 *err = WTAP_ERR_SHORT_READ;
92 if (file_seek(wth->fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
98 mpeg_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
102 errno = WTAP_ERR_CANT_READ;
103 bytes_read = file_read(pd, length, fh);
105 if (bytes_read != length) {
106 *err = file_error(fh);
108 *err = WTAP_ERR_SHORT_READ;
114 #define SCRHZ 27000000
117 mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
120 mpeg_t *mpeg = (mpeg_t *)wth->priv;
122 int bytes_read = mpeg_read_header(wth, err, err_info, &n);
123 unsigned int packet_size;
124 struct wtap_nstime ts = mpeg->now;
126 if (bytes_read == -1)
129 gint64 offset = file_tell(wth->fh);
134 if (file_seek(wth->fh, 3, SEEK_CUR, err) == -1)
137 bytes_read = file_read(&stream, sizeof stream, wth->fh);
138 if (bytes_read != sizeof stream) {
139 *err = file_error(wth->fh);
143 if (stream == 0xba) {
149 bytes_read = file_read(&pack1, sizeof pack1, wth->fh);
150 if (bytes_read != sizeof pack1) {
151 *err = file_error(wth->fh);
152 if (*err == 0 && bytes_read != 0)
153 *err = WTAP_ERR_SHORT_READ;
156 bytes_read = file_read(&pack0, sizeof pack0, wth->fh);
157 if (bytes_read != sizeof pack0) {
158 *err = file_error(wth->fh);
159 if (*err == 0 && bytes_read != 0)
160 *err = WTAP_ERR_SHORT_READ;
163 pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
165 switch (pack >> 62) {
167 if (file_seek(wth->fh, 1, SEEK_CUR, err) == -1)
169 bytes_read = file_read(&stuffing,
170 sizeof stuffing, wth->fh);
171 if (bytes_read != sizeof stuffing) {
172 *err = file_error(wth->fh);
176 packet_size = 14 + stuffing;
179 guint64 bytes = pack >> 16;
181 (bytes >> 43 & 0x0007) << 30 |
182 (bytes >> 27 & 0x7fff) << 15 |
183 (bytes >> 11 & 0x7fff) << 0;
184 unsigned ext = (unsigned)((bytes >> 1) & 0x1ff);
185 guint64 cr = 300 * ts_val + ext;
186 unsigned rem = (unsigned)(cr % SCRHZ);
188 = mpeg->t0 + (time_t)(cr / SCRHZ);
190 = (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
199 bytes_read = file_read(&length, sizeof length, wth->fh);
200 if (bytes_read != sizeof length) {
201 *err = file_error(wth->fh);
202 if (*err == 0 && bytes_read != 0)
203 *err = WTAP_ERR_SHORT_READ;
206 length = g_ntohs(length);
207 packet_size = 6 + length;
210 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
215 MPA_UNMARSHAL(&mpa, n);
216 if (MPA_VALID(&mpa)) {
217 packet_size = MPA_BYTES(&mpa);
218 mpeg->now.nsecs += MPA_DURATION_NS(&mpa);
219 if (mpeg->now.nsecs >= 1000000000) {
221 mpeg->now.nsecs -= 1000000000;
224 packet_size = mpeg_resync(wth, err, err_info);
225 if (packet_size == 0)
229 *data_offset = wth->data_offset;
231 buffer_assure_space(wth->frame_buffer, packet_size);
232 if (!mpeg_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
235 wth->data_offset += packet_size;
237 wth->phdr.caplen = packet_size;
238 wth->phdr.len = packet_size;
243 mpeg_seek_read(wtap *wth, gint64 seek_off,
244 union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int length,
245 int *err, gchar **err_info _U_)
247 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
249 return mpeg_read_rec_data(wth->random_fh, pd, length, err);
256 { 3, "TAG" }, /* ID3v1 */
257 { 3, "ID3" }, /* ID3v2 */
258 { 3, "\0\0\1" }, /* MPEG PES */
259 { 2, "\xff\xfb" }, /* MP3, taken from http://en.wikipedia.org/wiki/MP3#File_structure */
264 mpeg_open(wtap *wth, int *err, gchar **err_info _U_)
268 struct _mpeg_magic* m;
271 errno = WTAP_ERR_CANT_READ;
272 bytes_read = file_read(magic_buf, sizeof magic_buf, wth->fh);
273 if (bytes_read != (int) sizeof magic_buf) {
274 *err = file_error(wth->fh);
280 for (m=magic; m->match; m++) {
281 if (memcmp(magic_buf, m->match, m->len) == 0)
288 /* This appears to be a file with MPEG data. */
289 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
292 wth->file_type = WTAP_FILE_MPEG;
293 wth->file_encap = WTAP_ENCAP_MPEG;
294 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
295 wth->subtype_read = mpeg_read;
296 wth->subtype_seek_read = mpeg_seek_read;
297 wth->snapshot_length = 0;
299 mpeg = (mpeg_t *)g_malloc(sizeof(mpeg_t));
300 wth->priv = (void *)mpeg;
301 mpeg->now.secs = time(NULL);
303 mpeg->t0 = mpeg->now.secs;