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>
36 #include <wsutil/buffer.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,
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))
74 phdr->rec_type = REC_TYPE_PACKET;
76 /* XXX - relative, not absolute, time stamps */
77 phdr->presence_flags = WTAP_HAS_TS;
80 * Every packet in an MPEG2-TS stream is has a fixed size of
81 * MP2T_SIZE plus the number of trailer bytes.
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.
87 * It would be really cool to be able to configure the bitrate...
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);
93 phdr->caplen = MP2T_SIZE;
94 phdr->len = MP2T_SIZE;
100 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
102 mp2t_filetype_t *mp2t;
104 mp2t = (mp2t_filetype_t*) wth->priv;
106 *data_offset = file_tell(wth->fh);
108 if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->phdr,
109 wth->frame_buffer, err, err_info)) {
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)) {
124 mp2t_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
125 Buffer *buf, int *err, gchar **err_info)
127 mp2t_filetype_t *mp2t;
129 if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
133 mp2t = (mp2t_filetype_t*) wth->priv;
135 if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, phdr, buf,
138 *err = WTAP_ERR_SHORT_READ;
145 mp2t_open(wtap *wth, int *err, gchar **err_info)
147 guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
148 guint8 trailer_len = 0;
149 guint sync_steps = 0;
152 mp2t_filetype_t *mp2t;
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)
163 for (i = 0; i < MP2T_SIZE; i++) {
164 if (MP2T_SYNC_BYTE == buffer[i]) {
170 return 0; /* wrong file type - not an mpeg2 ts file */
173 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
176 /* read some packets and make sure they all start with a sync byte */
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 */
184 if (buffer[0] == MP2T_SYNC_BYTE) {
188 /* no sync byte found, check if trailing data is appended
189 and we have to increase the packet size */
191 /* if we've already detected a trailer field, we must remain in sync
192 another mismatch means we have no mpeg2 ts file */
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) {
200 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
207 /* no sync byte found in the vicinity, this is no mpeg2 ts file */
208 if (i==TRAILER_LEN_MAX)
211 } while (sync_steps < SYNC_STEPS);
213 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
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;
224 mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
230 mp2t->start_offset = first;
231 mp2t->trailer_len = trailer_len;