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
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.
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.
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.
25 #ifdef HAVE_SYS_TYPES_H
26 #include <sys/types.h>
37 #include "file_wrappers.h"
43 #define MP2T_SYNC_BYTE 0x47
45 #define MP2T_QAM256_BITRATE 38810700 /* bits per second */
46 #define MP2T_QAM64_BITRATE 26970350 /* bits per second */
48 /* we try to detect trailing data up to 40 bytes after each packet */
49 #define TRAILER_LEN_MAX 40
51 /* number of consecutive packets we must read to decide that a file
52 is actually an mpeg2 ts */
58 /* length of trailing data (e.g. FEC) that's appended after each packet */
63 mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
64 struct wtap_pkthdr *phdr, Buffer *buf, int *err,
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;
82 /* XXX - relative, not absolute, time stamps */
83 phdr->presence_flags = WTAP_HAS_TS;
86 * Every packet in an MPEG2-TS stream is has a fixed size of
87 * MP2T_SIZE plus the number of trailer bytes.
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.
93 * It would be really cool to be able to configure the bitrate...
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);
99 phdr->caplen = MP2T_SIZE;
100 phdr->len = MP2T_SIZE;
106 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
108 mp2t_filetype_t *mp2t;
110 mp2t = (mp2t_filetype_t*) wth->priv;
112 *data_offset = file_tell(wth->fh);
114 if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->phdr,
115 wth->frame_buffer, err, err_info)) {
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)) {
130 mp2t_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
131 Buffer *buf, int *err, gchar **err_info)
133 mp2t_filetype_t *mp2t;
135 if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
139 mp2t = (mp2t_filetype_t*) wth->priv;
141 if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, phdr, buf,
144 *err = WTAP_ERR_SHORT_READ;
151 mp2t_open(wtap *wth, int *err, gchar **err_info)
154 guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
155 guint8 trailer_len = 0;
156 guint sync_steps = 0;
159 mp2t_filetype_t *mp2t;
162 errno = WTAP_ERR_CANT_READ;
163 bytes_read = file_read(buffer, MP2T_SIZE, wth->fh);
165 if (MP2T_SIZE != bytes_read) {
166 *err = file_error(wth->fh, err_info);
167 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
173 for (i = 0; i < MP2T_SIZE; i++) {
174 if (MP2T_SYNC_BYTE == buffer[i]) {
180 return 0; /* wrong file type - not an mpeg2 ts file */
183 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
186 /* read some packets and make sure they all start with a sync byte */
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 */
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 */
197 if (buffer[0] == MP2T_SYNC_BYTE) {
201 /* no sync byte found, check if trailing data is appended
202 and we have to increase the packet size */
204 /* if we've already detected a trailer field, we must remain in sync
205 another mismatch means we have no mpeg2 ts file */
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) {
213 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
220 /* no sync byte found in the vicinity, this is no mpeg2 ts file */
221 if (i==TRAILER_LEN_MAX)
224 } while (sync_steps < SYNC_STEPS);
226 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
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;
237 mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
243 mp2t->start_offset = first;
244 mp2t->trailer_len = trailer_len;