Sigh. The "data" element of a GArray is, alas, a "gchar *", not a "void
[obnox/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  * $Id$
8  *
9  * Wiretap Library
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36
37 #include "mpeg.h"
38 #include "wsutil/mpeg-audio.h"
39
40 #include "wtap-int.h"
41 #include "buffer.h"
42 #include "file_wrappers.h"
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47
48 #define PES_PREFIX 1
49 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
50
51 typedef struct {
52         struct wtap_nstime now;
53         time_t t0;
54 } mpeg_t;
55
56 static int 
57 mpeg_resync(wtap *wth, int *err, gchar **err_info _U_)
58 {
59         gint64 offset = file_tell(wth->fh);
60         int count = 0;
61         int byte = file_getc(wth->fh);
62
63         while (byte != EOF) {
64                 if (byte == 0xff && count > 0) {
65                         byte = file_getc(wth->fh);
66                         if (byte != EOF && (byte & 0xe0) == 0xe0)
67                                 break;
68                 } else
69                         byte = file_getc(wth->fh);
70                 count++;
71         }
72         if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
73                 return 0;
74         return count;
75 }
76
77 static int 
78 mpeg_read_header(wtap *wth, int *err, gchar **err_info _U_,
79                 guint32 *n)
80 {
81         int bytes_read;
82
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;
89                 return -1;
90         }
91         *n = g_ntohl(*n);
92         if (file_seek(wth->fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
93                 return -1;
94         return bytes_read;
95 }
96
97 static gboolean
98 mpeg_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
99 {
100         int     bytes_read;
101
102         errno = WTAP_ERR_CANT_READ;
103         bytes_read = file_read(pd, length, fh);
104
105         if (bytes_read != length) {
106                 *err = file_error(fh);
107                 if (*err == 0)
108                         *err = WTAP_ERR_SHORT_READ;
109                 return FALSE;
110         }
111         return TRUE;
112 }
113
114 #define SCRHZ 27000000
115
116 static gboolean 
117 mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
118                 gint64 *data_offset)
119 {
120         mpeg_t *mpeg = (mpeg_t *)wth->priv;
121         guint32 n;
122         int bytes_read = mpeg_read_header(wth, err, err_info, &n);
123         unsigned int packet_size;
124         struct wtap_nstime ts = mpeg->now;
125
126         if (bytes_read == -1)
127                 return FALSE;
128         if (PES_VALID(n)) {
129                 gint64 offset = file_tell(wth->fh);
130                 guint8 stream;
131
132                 if (offset == -1)
133                         return -1;
134                 if (file_seek(wth->fh, 3, SEEK_CUR, err) == -1)
135                         return FALSE;
136
137                 bytes_read = file_read(&stream, sizeof stream, wth->fh);
138                 if (bytes_read != sizeof stream) {
139                         *err = file_error(wth->fh);
140                         return FALSE;
141                 }
142
143                 if (stream == 0xba) {
144                         guint32 pack1;
145                         guint32 pack0;
146                         guint64 pack;
147                         guint8 stuffing;
148
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;
154                                 return FALSE;
155                         }
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;
161                                 return FALSE;
162                         }
163                         pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
164
165                         switch (pack >> 62) {
166                                 case 1:
167                                         if (file_seek(wth->fh, 1, SEEK_CUR, err) == -1)
168                                                 return FALSE;
169                                         bytes_read = file_read(&stuffing,
170                                                         sizeof stuffing, wth->fh);
171                                         if (bytes_read != sizeof stuffing) {
172                                                 *err = file_error(wth->fh);
173                                                 return FALSE;
174                                         }
175                                         stuffing &= 0x07;
176                                         packet_size = 14 + stuffing;
177
178                                         {
179                                                 guint64 bytes = pack >> 16;
180                                                 guint64 ts_val =
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);
187                                                 mpeg->now.secs
188                                                         = mpeg->t0 + (time_t)(cr / SCRHZ);
189                                                 mpeg->now.nsecs
190                                                         = (int)(G_GINT64_CONSTANT(1000000000) * rem / SCRHZ);
191                                         }
192                                         ts = mpeg->now;
193                                         break;
194                                 default:
195                                         packet_size = 12;
196                         }
197                 } else {
198                         guint16 length;
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;
204                                 return FALSE;
205                         }
206                         length = g_ntohs(length);
207                         packet_size = 6 + length;
208                 }
209
210                 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
211                         return FALSE;
212         } else {
213                 struct mpa mpa;
214
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) {
220                                 mpeg->now.secs++;
221                                 mpeg->now.nsecs -= 1000000000;
222                         }
223                 } else {
224                         packet_size = mpeg_resync(wth, err, err_info);
225                         if (packet_size == 0)
226                                 return FALSE;
227                 }
228         }
229         *data_offset = wth->data_offset;
230
231         buffer_assure_space(wth->frame_buffer, packet_size);
232         if (!mpeg_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
233                                 packet_size, err))
234                 return FALSE;
235         wth->data_offset += packet_size;
236         wth->phdr.ts = ts;
237         wth->phdr.caplen = packet_size;
238         wth->phdr.len = packet_size;
239         return TRUE;
240 }
241
242 static gboolean
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_)
246 {
247         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
248                 return FALSE;
249         return mpeg_read_rec_data(wth->random_fh, pd, length, err);
250 }
251
252 struct _mpeg_magic {
253         size_t len;
254         const gchar* match;
255 } magic[] = {
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 */
260         { 0, NULL }
261 };
262
263 int 
264 mpeg_open(wtap *wth, int *err, gchar **err_info _U_)
265 {
266         int bytes_read;
267         char magic_buf[16];
268         struct _mpeg_magic* m;
269         mpeg_t *mpeg;
270         
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);
275                 if (*err != 0)
276                         return -1;
277                 return 0;
278         }
279
280         for (m=magic; m->match; m++) {
281                 if (memcmp(magic_buf, m->match, m->len) == 0)
282                         goto good_magic;
283         }
284         
285         return 0;
286
287 good_magic:
288         /* This appears to be a file with MPEG data. */
289         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
290                 return -1;
291
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;
298
299         mpeg = (mpeg_t *)g_malloc(sizeof(mpeg_t));
300         wth->priv = (void *)mpeg;
301         mpeg->now.secs = time(NULL);
302         mpeg->now.nsecs = 0;
303         mpeg->t0 = mpeg->now.secs;
304
305         return 1;
306 }