fix a new warning
[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 "mpeg-audio.h"
39
40 #include "wtap-int.h"
41 #include "buffer.h"
42 #include "file_wrappers.h"
43 #include <errno.h>
44 #include <math.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <time.h>
48
49 #define PES_PREFIX 1
50 #define PES_VALID(n) (((n) >> 8 & 0xffffff) == PES_PREFIX)
51
52 static size_t 
53 mpeg_resync(wtap *wth, int *err, gchar **err_info _U_)
54 {
55         gint64 offset = file_tell(wth->fh);
56         size_t count = 0;
57         int sync = file_getc(wth->fh);
58
59         while (sync != EOF) {
60                 if (sync == 0xff && count > 0) {
61                         sync = file_getc(wth->fh);
62                         if (sync != EOF && (sync & 0xe0) == 0xe0)
63                                 break;
64                 } else
65                         sync = file_getc(wth->fh);
66                 count++;
67         }
68         file_seek(wth->fh, offset, SEEK_SET, err);
69         return count;
70 }
71
72 static int 
73 mpeg_read_header(wtap *wth, int *err, gchar **err_info _U_,
74                 guint32 *n)
75 {
76         int bytes_read;
77
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;
84                 return -1;
85         }
86         *n = g_ntohl(*n);
87         if (file_seek(wth->fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
88                 return -1;
89         return bytes_read;
90 }
91
92 static gboolean
93 mpeg_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
94 {
95         int     bytes_read;
96
97         errno = WTAP_ERR_CANT_READ;
98         bytes_read = file_read(pd, 1, length, fh);
99
100         if (bytes_read != length) {
101                 *err = file_error(fh);
102                 if (*err == 0)
103                         *err = WTAP_ERR_SHORT_READ;
104                 return FALSE;
105         }
106         return TRUE;
107 }
108
109 static struct wtap_nstime now;
110 static double t0;
111
112 static gboolean 
113 mpeg_read(wtap *wth, int *err, gchar **err_info _U_,
114                 gint64 *data_offset)
115 {
116         guint32 n;
117         int bytes_read = mpeg_read_header(wth, err, err_info, &n);
118         unsigned packet_size;
119         struct wtap_nstime ts = now;
120
121         if (bytes_read == -1)
122                 return FALSE;
123         if (PES_VALID(n)) {
124                 gint64 offset = file_tell(wth->fh);
125                 guint8 stream;
126                 int bytes_read;
127
128                 if (offset == -1)
129                         return -1;
130                 if (file_seek(wth->fh, 3, SEEK_CUR, err) == -1)
131                         return FALSE;
132
133                 bytes_read = file_read(&stream, 1, sizeof stream, wth->fh);
134                 if (bytes_read != sizeof stream) {
135                         *err = file_error(wth->fh);
136                         return FALSE;
137                 }
138
139                 if (stream == 0xba) {
140                         guint32 pack1;
141                         guint32 pack0;
142                         guint64 pack;
143                         guint8 stuffing;
144                         guint32 scr;
145                         guint16 scr_ext;
146                         double t;
147                         double secs;
148
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;
154                                 return FALSE;
155                         }
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;
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                                                         1, 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                                         scr = (guint32)
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;
184
185                                         now.nsecs = (int)(modf(t, &secs) * 1e9);
186                                         now.secs = (time_t)secs;
187                                         ts = now;
188                                         break;
189                                 default:
190                                         packet_size = 12;
191                         }
192                 } else {
193                         guint16 length;
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;
199                                 return FALSE;
200                         }
201                         length = g_ntohs(length);
202                         packet_size = 6 + length;
203                 }
204
205                 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
206                         return FALSE;
207         } else {
208                 struct mpa mpa;
209
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) {
215                                 now.secs++;
216                                 now.nsecs -= 1000000000;
217                         }
218                 } else {
219                         packet_size = mpeg_resync(wth, err, err_info);
220                         if (packet_size == 0)
221                                 return FALSE;
222                 }
223         }
224         *data_offset = wth->data_offset;
225
226         buffer_assure_space(wth->frame_buffer, packet_size);
227         if (!mpeg_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
228                                 packet_size, err))
229                 return FALSE;
230         wth->data_offset += packet_size;
231         wth->phdr.ts = ts;
232         wth->phdr.caplen = packet_size;
233         wth->phdr.len = packet_size;
234         return TRUE;
235 }
236
237 static gboolean
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_)
241 {
242         if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
243                 return FALSE;
244         return mpeg_read_rec_data(wth->random_fh, pd, length, err);
245 }
246
247 static void
248 mpeg_close(wtap *wth _U_)
249 {
250
251 }
252
253 int 
254 mpeg_open(wtap *wth, int *err, gchar **err_info)
255 {
256         guint32 n;
257         struct mpa mpa;
258
259         now.secs = time(NULL);
260         now.nsecs = 0;
261         t0 = (double) now.secs;
262
263         if (mpeg_read_header(wth, err, err_info, &n) == -1)
264                 return -1;
265         MPA_UNMARSHAL(&mpa, n);
266         if (!MPA_SYNC_VALID(&mpa)) {
267                 gint64 offset;
268                 size_t count;
269
270                 offset = file_tell(wth->fh);
271                 if (offset == -1)
272                         return -1;
273                 count = mpeg_resync(wth, err, err_info);
274                 if (count == 0)
275                         return 0;
276                 if (file_seek(wth->fh, count, SEEK_CUR, err) == -1)
277                         return -1;
278                 if (mpeg_read_header(wth, err, err_info, &n) == -1)
279                         return 0;
280                 MPA_UNMARSHAL(&mpa, n);
281                 if (!MPA_SYNC_VALID(&mpa))
282                         return 0;
283                 if (file_seek(wth->fh, offset, SEEK_SET, err) == -1)
284                         return -1;
285         }
286
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;
294         return 1;
295 }