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 "mpeg-audio.h"
42 #include "file_wrappers.h"
50 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
53 mpeg_resync(wtap *wth, int *err, gchar **err_info _U_)
55 gint64 offset = file_tell(wth->fh);
57 int sync = file_getc(wth->fh);
60 if (sync == 0xff && count > 0) {
61 sync = file_getc(wth->fh);
62 if (sync != EOF && (sync & 0xe0) == 0xe0)
65 sync = file_getc(wth->fh);
68 file_seek(wth->fh, offset, SEEK_SET, err);
73 mpeg_read_header(wtap *wth, int *err, gchar **err_info _U_,
78 errno = WTAP_ERR_CANT_READ;
79 bytes_read = file_read(n, 1, sizeof *n, wth->fh);
80 if (bytes_read != sizeof *n) {
81 *err = file_error(wth->fh);
82 if (*err == 0 && bytes_read != 0)
83 *err = WTAP_ERR_SHORT_READ;
87 if (file_seek(wth->fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
93 mpeg_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
97 errno = WTAP_ERR_CANT_READ;
98 bytes_read = file_read(pd, 1, length, fh);
100 if (bytes_read != length) {
101 *err = file_error(fh);
103 *err = WTAP_ERR_SHORT_READ;
109 static struct wtap_nstime now;
113 mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
117 int bytes_read = mpeg_read_header(wth, err, err_info, &n);
118 unsigned packet_size;
119 struct wtap_nstime ts = now;
121 if (bytes_read == -1)
124 gint64 offset = file_tell(wth->fh);
130 if (file_seek(wth->fh, 3, SEEK_CUR, err) == -1)
133 bytes_read = file_read(&stream, 1, sizeof stream, wth->fh);
134 if (bytes_read != sizeof stream) {
135 *err = file_error(wth->fh);
139 if (stream == 0xba) {
149 bytes_read = file_read(&pack1, 1, 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, 1, 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 1, sizeof stuffing, wth->fh);
171 if (bytes_read != sizeof stuffing) {
172 *err = file_error(wth->fh);
176 packet_size = 14 + stuffing;
179 ((pack >> 59 & 0x0007) << 30 |
180 (pack >> 43 & 0x7fff) << 15 |
181 (pack >> 27 & 0x7fff) << 0);
182 scr_ext = (guint16)(pack >> 17 & 0x1ff);
183 t = t0 + scr / 90e3 + scr_ext / 27e6;
185 now.nsecs = (int)(modf(t, &secs) * 1e9);
186 now.secs = (time_t)secs;
194 bytes_read = file_read(&length, 1, sizeof length, wth->fh);
195 if (bytes_read != sizeof length) {
196 *err = file_error(wth->fh);
197 if (*err == 0 && bytes_read != 0)
198 *err = WTAP_ERR_SHORT_READ;
201 length = g_ntohs(length);
202 packet_size = 6 + length;
205 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
210 MPA_UNMARSHAL(&mpa, n);
211 if (MPA_VALID(&mpa)) {
212 packet_size = MPA_BYTES(&mpa);
213 now.nsecs += MPA_DURATION_NS(&mpa);
214 if (now.nsecs >= 1000000000) {
216 now.nsecs -= 1000000000;
219 packet_size = mpeg_resync(wth, err, err_info);
220 if (packet_size == 0)
224 *data_offset = wth->data_offset;
226 buffer_assure_space(wth->frame_buffer, packet_size);
227 if (!mpeg_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
230 wth->data_offset += packet_size;
232 wth->phdr.caplen = packet_size;
233 wth->phdr.len = packet_size;
238 mpeg_seek_read(wtap *wth, gint64 seek_off,
239 union wtap_pseudo_header *pseudo_header _U_, guchar *pd, int length,
240 int *err, gchar **err_info _U_)
242 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
244 return mpeg_read_rec_data(wth->random_fh, pd, length, err);
248 mpeg_close(wtap *wth _U_)
254 mpeg_open(wtap *wth, int *err, gchar **err_info)
259 now.secs = time(NULL);
261 t0 = (double) now.secs;
263 if (mpeg_read_header(wth, err, err_info, &n) == -1)
265 MPA_UNMARSHAL(&mpa, n);
266 if (!MPA_SYNC_VALID(&mpa)) {
270 offset = file_tell(wth->fh);
273 count = mpeg_resync(wth, err, err_info);
276 if (file_seek(wth->fh, count, SEEK_CUR, err) == -1)
278 if (mpeg_read_header(wth, err, err_info, &n) == -1)
280 MPA_UNMARSHAL(&mpa, n);
281 if (!MPA_SYNC_VALID(&mpa))
283 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
287 wth->file_type = WTAP_FILE_MPEG;
288 wth->file_encap = WTAP_ENCAP_MPEG;
289 wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
290 wth->subtype_read = mpeg_read;
291 wth->subtype_seek_read = mpeg_seek_read;
292 wth->subtype_close = mpeg_close;
293 wth->snapshot_length = 0;