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 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include <sys/types.h>
22 #include <wsutil/buffer.h>
23 #include "file_wrappers.h"
29 #define MP2T_SYNC_BYTE 0x47
31 #define MP2T_QAM64_BITRATE 26970350 /* bits per second */
32 #define MP2T_PCR_CLOCK 27000000 /* cycles per second - 27MHz */
34 /* we try to detect trailing data up to 40 bytes after each packet */
35 #define TRAILER_LEN_MAX 40
37 /* number of consecutive packets we must read to decide that a file
38 is actually an mpeg2 ts */
45 /* length of trailing data (e.g. FEC) that's appended after each packet */
50 mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
51 wtap_rec *rec, Buffer *buf, int *err,
57 * MP2T_SIZE will always be less than WTAP_MAX_PACKET_SIZE_STANDARD, so
58 * we don't have to worry about the packet being too big.
60 ws_buffer_assure_space(buf, MP2T_SIZE);
61 if (!wtap_read_bytes_or_eof(fh, ws_buffer_start_ptr(buf), MP2T_SIZE, err, err_info))
64 rec->rec_type = REC_TYPE_PACKET;
66 /* XXX - relative, not absolute, time stamps */
67 rec->presence_flags = WTAP_HAS_TS;
70 * Every packet in an MPEG2-TS stream is has a fixed size of
71 * MP2T_SIZE plus the number of trailer bytes.
73 * We assume that the bits in the transport stream are supplied at
74 * a constant rate; is that guaranteed by all media that use
75 * MPEG2-TS? If so, the time offset, from the beginning of the
76 * stream, of a given packet is the packet offset, in bits, divided
79 * It would be really cool to be able to configure the bitrate, in
80 * case our attempt to guess it from the PCRs of one of the programs
81 * doesn't get the right answer.
83 tmp = ((guint64)(offset - mp2t->start_offset) * 8); /* offset, in bits */
84 rec->ts.secs = (time_t)(tmp / mp2t->bitrate);
85 rec->ts.nsecs = (int)((tmp % mp2t->bitrate) * 1000000000 / mp2t->bitrate);
87 rec->rec_header.packet_header.caplen = MP2T_SIZE;
88 rec->rec_header.packet_header.len = MP2T_SIZE;
94 mp2t_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
96 mp2t_filetype_t *mp2t;
98 mp2t = (mp2t_filetype_t*) wth->priv;
100 *data_offset = file_tell(wth->fh);
102 if (!mp2t_read_packet(mp2t, wth->fh, *data_offset, &wth->rec,
103 wth->rec_data, err, err_info)) {
107 /* if there's a trailer, skip it and go to the start of the next packet */
108 if (mp2t->trailer_len!=0) {
109 if (!wtap_read_bytes(wth->fh, NULL, mp2t->trailer_len, err, err_info)) {
118 mp2t_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
119 Buffer *buf, int *err, gchar **err_info)
121 mp2t_filetype_t *mp2t;
123 if (-1 == file_seek(wth->random_fh, seek_off, SEEK_SET, err)) {
127 mp2t = (mp2t_filetype_t*) wth->priv;
129 if (!mp2t_read_packet(mp2t, wth->random_fh, seek_off, rec, buf,
132 *err = WTAP_ERR_SHORT_READ;
139 mp2t_read_pcr(guint8 *buffer)
144 base = pntoh40(buffer);
147 ext = pntoh16(&buffer[4]);
150 return (base * 300 + ext);
154 mp2t_find_next_pcr(wtap *wth, guint8 trailer_len,
155 int *err, gchar **err_info, guint32 *idx, guint64 *pcr, guint16 *pid)
157 guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
163 while (FALSE == found && timeout++ < SYNC_STEPS * SYNC_STEPS) {
165 if (!wtap_read_bytes_or_eof(
166 wth->fh, buffer, MP2T_SIZE+trailer_len, err, err_info)) {
167 /* Read error, short read, or EOF */
171 if (MP2T_SYNC_BYTE != buffer[0]) {
175 /* Read out the AFC value. */
176 afc = 3 & (buffer[3] >> 4);
181 /* Check the length. */
186 /* Check that there is the PCR flag. */
187 if (0x10 != (0x10 & buffer[5])) {
191 /* We have a PCR value! */
192 *pcr = mp2t_read_pcr(&buffer[6]);
193 *pid = 0x01ff & pntoh16(&buffer[1]);
200 static wtap_open_return_val
201 mp2t_bits_per_second(wtap *wth, guint32 first, guint8 trailer_len,
202 guint64 *bitrate, int *err, gchar **err_info)
208 guint64 pcr_delta, bits_passed;
210 /* Find the first PCR + PID.
211 * Then find another PCR in that PID.
212 * Take the difference and that's our bitrate.
213 * All the different PCRs in different PIDs 'should' be the same.
215 * XXX - is this assuming that the time stamps in the PCRs correspond
216 * to the time scale of the underlying transport stream?
220 if (!mp2t_find_next_pcr(wth, trailer_len, err, err_info, &idx, &pcr1, &pid1)) {
221 /* Read error, short read, or EOF */
222 if (*err == WTAP_ERR_SHORT_READ)
223 return WTAP_OPEN_NOT_MINE; /* not a full frame */
225 return WTAP_OPEN_ERROR;
227 /* We don't have any PCRs, so we can't guess the bit rate.
228 * Default to something reasonable.
230 *bitrate = MP2T_QAM64_BITRATE;
231 return WTAP_OPEN_MINE;
238 if (!mp2t_find_next_pcr(wth, trailer_len, err, err_info, &idx, &pcr2, &pid2)) {
239 /* Read error, short read, or EOF */
240 if (*err == WTAP_ERR_SHORT_READ)
241 return WTAP_OPEN_NOT_MINE; /* not a full frame */
243 return WTAP_OPEN_ERROR;
245 /* We don't have two PCRs for the same PID, so we can't guess
247 * Default to something reasonable.
249 *bitrate = MP2T_QAM64_BITRATE;
250 return WTAP_OPEN_MINE;
259 /* The PCRs for that PID didn't go forward; treat that as an
260 * indication that this isn't an MPEG-2 TS.
262 return WTAP_OPEN_NOT_MINE;
264 pcr_delta = pcr2 - pcr1;
265 /* cast one of the factors to guint64
266 otherwise, the multiplication would use guint32 and could
267 overflow before the result is assigned to the guint64 bits_passed */
268 bits_passed = (guint64)MP2T_SIZE * (pn2 - pn1) * 8;
270 *bitrate = ((MP2T_PCR_CLOCK * bits_passed) / pcr_delta);
272 /* pcr_delta < MP2T_PCR_CLOCK * bits_passed (pn2 != pn1,
273 * as that's the test for the loop above, so bits_passed
276 * That will produce a fractional bitrate, which turns
277 * into zero, causing a zero divide later.
279 * XXX - should we report this as "not ours"? A bitrate
280 * of less than 1 bit per second is not very useful for any
281 * form of audio/video, so presumably that's unlikely to
284 return WTAP_OPEN_ERROR;
286 return WTAP_OPEN_MINE;
290 mp2t_open(wtap *wth, int *err, gchar **err_info)
292 guint8 buffer[MP2T_SIZE+TRAILER_LEN_MAX];
293 guint8 trailer_len = 0;
294 guint sync_steps = 0;
297 mp2t_filetype_t *mp2t;
298 wtap_open_return_val status;
302 if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE, err, err_info)) {
303 if (*err != WTAP_ERR_SHORT_READ)
304 return WTAP_OPEN_ERROR;
305 return WTAP_OPEN_NOT_MINE;
308 for (i = 0; i < MP2T_SIZE; i++) {
309 if (MP2T_SYNC_BYTE == buffer[i]) {
315 * No sync bytes found, so not an MPEG-2 Transport Stream file.
317 return WTAP_OPEN_NOT_MINE; /* wrong file type - not an mpeg2 ts file */
320 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
321 return WTAP_OPEN_ERROR;
324 /* read some packets and make sure they all start with a sync byte */
326 if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE+trailer_len, err, err_info)) {
327 if (*err != WTAP_ERR_SHORT_READ)
328 return WTAP_OPEN_ERROR; /* read error */
329 if(sync_steps<2) return WTAP_OPEN_NOT_MINE; /* wrong file type - not an mpeg2 ts file */
330 break; /* end of file, that's ok if we're still in sync */
332 if (buffer[0] == MP2T_SYNC_BYTE) {
336 /* no sync byte found, check if trailing data is appended
337 and we have to increase the packet size */
339 /* if we've already detected a trailer field, we must remain in sync
340 another mismatch means we have no mpeg2 ts file */
342 return WTAP_OPEN_NOT_MINE;
344 /* check if a trailer is appended to the packet */
345 for (i=0; i<TRAILER_LEN_MAX; i++) {
346 if (buffer[i] == MP2T_SYNC_BYTE) {
348 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
349 return WTAP_OPEN_ERROR;
355 /* no sync byte found in the vicinity, this is no mpeg2 ts file */
356 if (i==TRAILER_LEN_MAX)
357 return WTAP_OPEN_NOT_MINE;
359 } while (sync_steps < SYNC_STEPS);
361 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
362 return WTAP_OPEN_ERROR;
365 /* Ensure there is a valid bitrate */
366 status = mp2t_bits_per_second(wth, first, trailer_len,
367 &bitrate, err, err_info);
368 if (status != WTAP_OPEN_MINE) {
372 if (-1 == file_seek(wth->fh, first, SEEK_SET, err)) {
373 return WTAP_OPEN_ERROR;
376 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MPEG_2_TS;
377 wth->file_encap = WTAP_ENCAP_MPEG_2_TS;
378 wth->file_tsprec = WTAP_TSPREC_NSEC;
379 wth->subtype_read = mp2t_read;
380 wth->subtype_seek_read = mp2t_seek_read;
381 wth->snapshot_length = 0;
383 mp2t = (mp2t_filetype_t*) g_malloc(sizeof(mp2t_filetype_t));
386 mp2t->start_offset = first;
387 mp2t->trailer_len = trailer_len;
388 mp2t->bitrate = bitrate;
390 return WTAP_OPEN_MINE;
394 * Editor modelines - http://www.wireshark.org/tools/modelines.html
399 * indent-tabs-mode: nil
402 * vi: set shiftwidth=4 tabstop=8 expandtab:
403 * :indentSize=4:tabSize=8:noTabs=true: