844fb40a1362108a9404e7d192961b8412b934a0
[jlayton/wireshark.git] / wiretap / mp2t.c
1 /* mp2t.c
2  *
3  * ISO/IEC 13818-1 MPEG2-TS file format decoder for the Wiretap library.
4  * Written by Weston Schmidt <weston_schmidt@alumni.purdue.edu>
5  * Copyright 2012 Weston Schmidt
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 "mp2t.h"
34
35 #include "wtap-int.h"
36 #include "buffer.h"
37 #include "file_wrappers.h"
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42
43 #define MP2T_SYNC_BYTE      0x47
44 #define MP2T_SIZE           188
45 #define MP2T_QAM256_BITRATE 38810700    /* bits per second */
46 #define MP2T_QAM64_BITRATE  26970350    /* bits per second */
47
48 /* we try to detect trailing data up to 40 bytes after each packet */
49 #define TRAILER_LEN_MAX 40
50
51 /* number of consecutive packets we must read to decide that a file
52    is actually an mpeg2 ts */
53 #define SYNC_STEPS   10
54
55
56 typedef struct {
57     int start_offset;
58     /* length of trailing data (e.g. FEC) that's appended after each packet */
59     guint8  trailer_len;
60 } mp2t_filetype_t;
61
62 static gboolean
63 mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
64                  struct wtap_pkthdr *phdr, Buffer *buf, int *err,
65                  gchar **err_info)
66 {
67     guint64 tmp;
68     int bytes_read;
69
70     buffer_assure_space(buf, MP2T_SIZE);
71     errno = WTAP_ERR_CANT_READ;
72     bytes_read = file_read(buffer_start_ptr(buf), MP2T_SIZE, fh);
73     if (MP2T_SIZE != bytes_read) {
74         *err = file_error(fh, err_info);
75         /* bytes_read==0 is end of file, not a short read */
76         if (bytes_read>0 && *err == 0) {
77             *err = WTAP_ERR_SHORT_READ;
78         }
79         return FALSE;
80     }
81
82     /* XXX - relative, not absolute, time stamps */
83     phdr->presence_flags = WTAP_HAS_TS;
84
85     /*
86      * Every packet in an MPEG2-TS stream is has a fixed size of
87      * MP2T_SIZE plus the number of trailer bytes.
88      *
89      * The bitrate is constant, so the time offset, from the beginning
90      * of the stream, of a given packet is the packet offset, in bits,
91      * divided by the bitrate.
92      *
93      * It would be really cool to be able to configure the bitrate...
94      */
95     tmp = ((guint64)(offset - mp2t->start_offset) * 8); /* offset, in bits */
96     phdr->ts.secs = (time_t)(tmp / MP2T_QAM256_BITRATE);
97     phdr->ts.nsecs = (int)((tmp % MP2T_QAM256_BITRATE) * 1000000000 / MP2T_QAM256_BITRATE);
98
99     phdr->caplen = MP2T_SIZE;
100     phdr->len = MP2T_SIZE;
101
102     return TRUE;
103 }
104
105 static gboolean
106 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
107 {
108     mp2t_filetype_t *mp2t;
109
110     mp2t = (mp2t_filetype_t*) wth->priv;
111
112     *data_offset = file_tell(wth->fh);
113
114     if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->phdr,
115                           wth->frame_buffer, err, err_info)) {
116         return FALSE;
117     }
118
119     /* if there's a trailer, skip it and go to the start of the next packet */
120     if (mp2t->trailer_len!=0) {
121         if (-1 == file_seek(wth->fh, mp2t->trailer_len, SEEK_CUR, err)) {
122             return FALSE;
123         }
124     }
125
126     return TRUE;
127 }
128
129 static gboolean
130 mp2t_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
131         Buffer *buf, int *err, gchar **err_info)
132 {
133     mp2t_filetype_t *mp2t;
134
135     if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
136         return FALSE;
137     }
138
139     mp2t = (mp2t_filetype_t*) wth->priv;
140
141     if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, phdr, buf,
142                           err, err_info)) {
143         if (*err == 0)
144             *err = WTAP_ERR_SHORT_READ;
145         return FALSE;
146     }
147     return TRUE;
148 }
149
150 int
151 mp2t_open(wtap *wth, int *err, gchar **err_info)
152 {
153     int bytes_read;
154     guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
155     guint8 trailer_len = 0;
156     guint sync_steps = 0;
157     int i;
158     int first;
159     mp2t_filetype_t *mp2t;
160
161
162     errno = WTAP_ERR_CANT_READ;
163     bytes_read = file_read(buffer, MP2T_SIZE, wth->fh);
164
165     if (MP2T_SIZE != bytes_read) {
166         *err = file_error(wth->fh, err_info);
167         if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
168             return -1;
169         return 0;
170     }
171
172     first = -1;
173     for (i = 0; i < MP2T_SIZE; i++) {
174         if (MP2T_SYNC_BYTE == buffer[i]) {
175             first = i;
176             break;
177         }
178     }
179     if (-1 == first) {
180         return 0; /* wrong file type - not an mpeg2 ts file */
181     }
182
183     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
184         return -1;
185     }
186     /* read some packets and make sure they all start with a sync byte */
187     do {
188        bytes_read = file_read(buffer, MP2T_SIZE+trailer_len, wth->fh);
189        if (bytes_read < 0) {
190           *err = file_error(wth->fh, err_info);
191           return -1;  /* read error */
192        }
193        if (bytes_read < MP2T_SIZE+trailer_len) {
194           if(sync_steps<2) return 0; /* wrong file type - not an mpeg2 ts file */
195           break;  /* end of file, that's ok if we're still in sync */
196        }
197        if (buffer[0] == MP2T_SYNC_BYTE) {
198                sync_steps++;
199        }
200        else {
201            /* no sync byte found, check if trailing data is appended
202               and we have to increase the packet size */
203
204            /* if we've already detected a trailer field, we must remain in sync
205               another mismatch means we have no mpeg2 ts file */
206            if (trailer_len>0)
207                return 0;
208
209            /* check if a trailer is appended to the packet */
210            for (i=0; i<TRAILER_LEN_MAX; i++) {
211                if (buffer[i] == MP2T_SYNC_BYTE) {
212                    trailer_len = i;
213                    if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
214                        return -1;
215                    }
216                    sync_steps = 0;
217                    break;
218                }
219            }
220            /* no sync byte found in the vicinity, this is no mpeg2 ts file */
221            if (i==TRAILER_LEN_MAX)
222                return 0;
223        }
224     } while (sync_steps < SYNC_STEPS);
225
226     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
227         return -1;
228     }
229
230     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG_2_TS;
231     wth->file_encap = WTAP_ENCAP_MPEG_2_TS;
232     wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
233     wth->subtype_read = mp2t_read;
234     wth->subtype_seek_read = mp2t_seek_read;
235     wth->snapshot_length = 0;
236
237     mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
238     if (NULL == mp2t) {
239         return -1;
240     }
241
242     wth->priv = mp2t;
243     mp2t->start_offset = first;
244     mp2t->trailer_len = trailer_len;
245
246     return 1;
247 }