5337d1d35b6a32f93b781231f78b6a96fe97999a
[metze/wireshark/wip.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 <wsutil/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
69     ws_buffer_assure_space(buf, MP2T_SIZE);
70     errno = WTAP_ERR_CANT_READ;
71     if (!wtap_read_bytes_or_eof(fh, ws_buffer_start_ptr(buf), MP2T_SIZE, err, err_info))
72         return FALSE;
73
74     phdr->rec_type = REC_TYPE_PACKET;
75
76     /* XXX - relative, not absolute, time stamps */
77     phdr->presence_flags = WTAP_HAS_TS;
78
79     /*
80      * Every packet in an MPEG2-TS stream is has a fixed size of
81      * MP2T_SIZE plus the number of trailer bytes.
82      *
83      * The bitrate is constant, so the time offset, from the beginning
84      * of the stream, of a given packet is the packet offset, in bits,
85      * divided by the bitrate.
86      *
87      * It would be really cool to be able to configure the bitrate...
88      */
89     tmp = ((guint64)(offset - mp2t->start_offset) * 8); /* offset, in bits */
90     phdr->ts.secs = (time_t)(tmp / MP2T_QAM256_BITRATE);
91     phdr->ts.nsecs = (int)((tmp % MP2T_QAM256_BITRATE) * 1000000000 / MP2T_QAM256_BITRATE);
92
93     phdr->caplen = MP2T_SIZE;
94     phdr->len = MP2T_SIZE;
95
96     return TRUE;
97 }
98
99 static gboolean
100 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
101 {
102     mp2t_filetype_t *mp2t;
103
104     mp2t = (mp2t_filetype_t*) wth->priv;
105
106     *data_offset = file_tell(wth->fh);
107
108     if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->phdr,
109                           wth->frame_buffer, err, err_info)) {
110         return FALSE;
111     }
112
113     /* if there's a trailer, skip it and go to the start of the next packet */
114     if (mp2t->trailer_len!=0) {
115         if (-1 == file_seek(wth->fh, mp2t->trailer_len, SEEK_CUR, err)) {
116             return FALSE;
117         }
118     }
119
120     return TRUE;
121 }
122
123 static gboolean
124 mp2t_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
125         Buffer *buf, int *err, gchar **err_info)
126 {
127     mp2t_filetype_t *mp2t;
128
129     if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
130         return FALSE;
131     }
132
133     mp2t = (mp2t_filetype_t*) wth->priv;
134
135     if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, phdr, buf,
136                           err, err_info)) {
137         if (*err == 0)
138             *err = WTAP_ERR_SHORT_READ;
139         return FALSE;
140     }
141     return TRUE;
142 }
143
144 int
145 mp2t_open(wtap *wth, int *err, gchar **err_info)
146 {
147     guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
148     guint8 trailer_len = 0;
149     guint sync_steps = 0;
150     int i;
151     int first;
152     mp2t_filetype_t *mp2t;
153
154
155     errno = WTAP_ERR_CANT_READ;
156     if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE, err, err_info)) {
157         if (*err != WTAP_ERR_SHORT_READ)
158             return -1;
159         return 0;
160     }
161
162     first = -1;
163     for (i = 0; i < MP2T_SIZE; i++) {
164         if (MP2T_SYNC_BYTE == buffer[i]) {
165             first = i;
166             break;
167         }
168     }
169     if (-1 == first) {
170         return 0; /* wrong file type - not an mpeg2 ts file */
171     }
172
173     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
174         return -1;
175     }
176     /* read some packets and make sure they all start with a sync byte */
177     do {
178        if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE+trailer_len, err, err_info)) {
179           if (*err != WTAP_ERR_SHORT_READ)
180             return -1;  /* read error */
181           if(sync_steps<2) return 0; /* wrong file type - not an mpeg2 ts file */
182           break;  /* end of file, that's ok if we're still in sync */
183        }
184        if (buffer[0] == MP2T_SYNC_BYTE) {
185                sync_steps++;
186        }
187        else {
188            /* no sync byte found, check if trailing data is appended
189               and we have to increase the packet size */
190
191            /* if we've already detected a trailer field, we must remain in sync
192               another mismatch means we have no mpeg2 ts file */
193            if (trailer_len>0)
194                return 0;
195
196            /* check if a trailer is appended to the packet */
197            for (i=0; i<TRAILER_LEN_MAX; i++) {
198                if (buffer[i] == MP2T_SYNC_BYTE) {
199                    trailer_len = i;
200                    if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
201                        return -1;
202                    }
203                    sync_steps = 0;
204                    break;
205                }
206            }
207            /* no sync byte found in the vicinity, this is no mpeg2 ts file */
208            if (i==TRAILER_LEN_MAX)
209                return 0;
210        }
211     } while (sync_steps < SYNC_STEPS);
212
213     if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
214         return -1;
215     }
216
217     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG_2_TS;
218     wth->file_encap = WTAP_ENCAP_MPEG_2_TS;
219     wth->file_tsprec = WTAP_TSPREC_NSEC;
220     wth->subtype_read = mp2t_read;
221     wth->subtype_seek_read = mp2t_seek_read;
222     wth->snapshot_length = 0;
223
224     mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
225     if (NULL == mp2t) {
226         return -1;
227     }
228
229     wth->priv = mp2t;
230     mp2t->start_offset = first;
231     mp2t->trailer_len = trailer_len;
232
233     return 1;
234 }