ERF: Make ERF wiretap forwards compatible.
[metze/wireshark/wip.git] / wiretap / mpeg.c
1 /* mpeg.c
2  *
3  * MPEG file format decoder for the Wiretap library.
4  * Written by Shaun Jackman <sjackman@gmail.com>
5  * Copyright 2007 Shaun Jackman
6  *
7  * Wiretap Library
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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
27 #endif
28
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
32
33 #include "mpeg.h"
34 #include "wsutil/mpeg-audio.h"
35
36 #include "wtap-int.h"
37 #include <wsutil/buffer.h>
38 #include "file_wrappers.h"
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <time.h>
43
44 #define PES_PREFIX 1
45 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
46
47 typedef struct {
48         nstime_t now;
49         time_t t0;
50 } mpeg_t;
51
52 static int
53 mpeg_resync(FILE_T fh, int *err)
54 {
55         gint64 offset = file_tell(fh);
56         int count = 0;
57         int byte = file_getc(fh);
58
59         while (byte != EOF) {
60                 if (byte == 0xff && count > 0) {
61                         byte = file_getc(fh);
62                         if (byte != EOF && (byte & 0xe0) == 0xe0)
63                                 break;
64                 } else
65                         byte = file_getc(fh);
66                 count++;
67         }
68         if (file_seek(fh, offset, SEEK_SET, err) == -1)
69                 return 0;
70         return count;
71 }
72
73 #define SCRHZ 27000000
74
75 static gboolean
76 mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
77     gboolean is_random, int *err, gchar **err_info)
78 {
79         mpeg_t *mpeg = (mpeg_t *)wth->priv;
80         guint32 n;
81         unsigned int packet_size;
82         nstime_t ts = mpeg->now;
83
84         /*
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
87          * based on that.
88          *
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)?
94          */
95         if (!wtap_read_bytes_or_eof(fh, &n, sizeof n, err, err_info))
96                 return FALSE;
97         if (file_seek(fh, -(gint64)(sizeof n), SEEK_CUR, err) == -1)
98                 return FALSE;
99         n = g_ntohl(n);
100         if (PES_VALID(n)) {
101                 gint64 offset = file_tell(fh);
102                 guint8 stream;
103
104                 if (!file_skip(fh, 3, err))
105                         return FALSE;
106
107                 if (!wtap_read_bytes(fh, &stream, sizeof stream, err, err_info))
108                         return FALSE;
109
110                 if (stream == 0xba) {
111                         guint32 pack1;
112                         guint32 pack0;
113                         guint64 pack;
114                         guint8 stuffing;
115
116                         if (!wtap_read_bytes(fh, &pack1, sizeof pack1, err, err_info))
117                                 return FALSE;
118                         if (!wtap_read_bytes(fh, &pack0, sizeof pack0, err, err_info))
119                                 return FALSE;
120                         pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
121
122                         switch (pack >> 62) {
123                                 case 1:
124                                         if (!file_skip(fh, 1, err))
125                                                 return FALSE;
126                                         if (!wtap_read_bytes(fh, &stuffing,
127                                             sizeof stuffing, err, err_info))
128                                                 return FALSE;
129                                         stuffing &= 0x07;
130                                         packet_size = 14 + stuffing;
131
132                                         if (!is_random) {
133                                                 guint64 bytes = pack >> 16;
134                                                 guint64 ts_val =
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);
141                                                 mpeg->now.secs
142                                                         = mpeg->t0 + (time_t)(cr / SCRHZ);
143                                                 mpeg->now.nsecs
144                                                         = (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
145                                         }
146                                         ts = mpeg->now;
147                                         break;
148                                 default:
149                                         packet_size = 12;
150                         }
151                 } else {
152                         guint16 length;
153                         if (!wtap_read_bytes(fh, &length, sizeof length, err, err_info))
154                                 return FALSE;
155                         length = g_ntohs(length);
156                         packet_size = 6 + length;
157                 }
158
159                 if (file_seek(fh, offset, SEEK_SET, err) == -1)
160                         return FALSE;
161         } else {
162                 struct mpa mpa;
163
164                 MPA_UNMARSHAL(&mpa, n);
165                 if (MPA_VALID(&mpa)) {
166                         packet_size = MPA_BYTES(&mpa);
167                         if (!is_random) {
168                                 mpeg->now.nsecs += MPA_DURATION_NS(&mpa);
169                                 if (mpeg->now.nsecs >= 1000000000) {
170                                         mpeg->now.secs++;
171                                         mpeg->now.nsecs -= 1000000000;
172                                 }
173                         }
174                 } else {
175                         packet_size = mpeg_resync(fh, err);
176                         if (packet_size == 0)
177                                 return FALSE;
178                 }
179         }
180
181         if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
182                 return FALSE;
183
184         phdr->rec_type = REC_TYPE_PACKET;
185
186         /* XXX - relative, not absolute, time stamps */
187         if (!is_random) {
188                 phdr->presence_flags = WTAP_HAS_TS;
189                 phdr->ts = ts;
190         }
191         phdr->caplen = packet_size;
192         phdr->len = packet_size;
193
194         return TRUE;
195 }
196
197 static gboolean
198 mpeg_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
199 {
200         *data_offset = file_tell(wth->fh);
201
202         return mpeg_read_packet(wth, wth->fh, &wth->phdr, wth->frame_buffer,
203             FALSE, err, err_info);
204 }
205
206 static gboolean
207 mpeg_seek_read(wtap *wth, gint64 seek_off,
208                 struct wtap_pkthdr *phdr, Buffer *buf,
209                 int *err, gchar **err_info)
210 {
211         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
212                 return FALSE;
213
214         if (!mpeg_read_packet(wth, wth->random_fh, phdr, buf, TRUE, err,
215             err_info)) {
216                 if (*err == 0)
217                         *err = WTAP_ERR_SHORT_READ;
218                 return FALSE;
219         }
220         return TRUE;
221 }
222
223 struct _mpeg_magic {
224         size_t len;
225         const gchar* match;
226 } magic[] = {
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 */
231         { 0, NULL }
232 };
233
234 wtap_open_return_val
235 mpeg_open(wtap *wth, int *err, gchar **err_info)
236 {
237         char magic_buf[16];
238         struct _mpeg_magic* m;
239         mpeg_t *mpeg;
240
241         if (!wtap_read_bytes(wth->fh, magic_buf, sizeof magic_buf,
242             err, err_info)) {
243                 if (*err != WTAP_ERR_SHORT_READ)
244                         return WTAP_OPEN_ERROR;
245                 return WTAP_OPEN_NOT_MINE;
246         }
247
248         for (m=magic; m->match; m++) {
249                 if (memcmp(magic_buf, m->match, m->len) == 0)
250                         goto good_magic;
251         }
252
253         return WTAP_OPEN_NOT_MINE;
254
255 good_magic:
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;
259
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;
266
267         mpeg = (mpeg_t *)g_malloc(sizeof(mpeg_t));
268         wth->priv = (void *)mpeg;
269         mpeg->now.secs = 0;
270         mpeg->now.nsecs = 0;
271         mpeg->t0 = mpeg->now.secs;
272
273         return WTAP_OPEN_MINE;
274 }
275
276 /*
277  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
278  *
279  * Local variables:
280  * c-basic-offset: 8
281  * tab-width: 8
282  * indent-tabs-mode: t
283  * End:
284  *
285  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
286  * :indentSize=8:tabSize=8:noTabs=false:
287  */