2 * MPEG-2 transport stream (aka DVB) demuxer
3 * Copyright (c) 2002-2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/buffer.h"
23 #include "libavutil/crc.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
30 #include "libavcodec/bitstream.h"
31 #include "libavcodec/bytestream.h"
32 #include "libavcodec/opus.h"
37 #include "avio_internal.h"
41 /* maximum size in which we look for synchronization if
42 * synchronization is lost */
43 #define MAX_RESYNC_SIZE 65536
45 #define MAX_PES_PAYLOAD 200 * 1024
47 #define MAX_MP4_DESCR_COUNT 16
49 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
51 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
52 (modulus) = (dividend) % (divisor); \
53 (prev_dividend) = (dividend); \
56 enum MpegTSFilterType {
61 typedef struct MpegTSFilter MpegTSFilter;
63 typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
64 int is_start, int64_t pos);
66 typedef struct MpegTSPESFilter {
71 typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len);
73 typedef void SetServiceCallback (void *opaque, int ret);
75 typedef struct MpegTSSectionFilter {
80 unsigned int check_crc : 1;
81 unsigned int end_of_section_reached : 1;
82 SectionCallback *section_cb;
84 } MpegTSSectionFilter;
89 int last_cc; /* last cc code (-1 if first packet) */
90 enum MpegTSFilterType type;
92 MpegTSPESFilter pes_filter;
93 MpegTSSectionFilter section_filter;
97 #define MAX_PIDS_PER_PROGRAM 64
99 unsigned int id; // program id/service id
100 unsigned int nb_pids;
101 unsigned int pids[MAX_PIDS_PER_PROGRAM];
104 struct MpegTSContext {
105 const AVClass *class;
107 AVFormatContext *stream;
108 /** raw packet size, including FEC if present */
112 /** position corresponding to pos47, or 0 if pos47 invalid */
115 /** if true, all pids are analyzed to find streams */
118 /** compute exact PCR for each transport stream packet */
119 int mpeg2ts_compute_pcr;
121 int64_t cur_pcr; /**< used to estimate the exact PCR */
122 int pcr_incr; /**< used to estimate the exact PCR */
124 /* data needed to handle file based ts */
125 /** stop parsing loop */
127 /** packet containing Audio/Video data */
129 /** to detect seek */
134 /******************************************/
135 /* private mpegts data */
137 /** structure to keep track of Program->pids mapping */
141 /** filters for various streams specified by PMT + for the PAT and PMT */
142 MpegTSFilter *pids[NB_PID_MAX];
145 #define MPEGTS_OPTIONS \
146 { "resync_size", "Size limit for looking up a new synchronization.", offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }
148 static const AVOption options[] = {
153 static const AVClass mpegts_class = {
154 .class_name = "mpegts demuxer",
155 .item_name = av_default_item_name,
157 .version = LIBAVUTIL_VERSION_INT,
160 static const AVOption raw_options[] = {
162 { "compute_pcr", "Compute exact PCR for each transport stream packet.",
163 offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
164 { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
165 { "ts_packetsize", "Output option carrying the raw packet size.",
166 offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
168 AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
172 static const AVClass mpegtsraw_class = {
173 .class_name = "mpegtsraw demuxer",
174 .item_name = av_default_item_name,
175 .option = raw_options,
176 .version = LIBAVUTIL_VERSION_INT,
179 /* TS stream handling */
184 MPEGTS_PESHEADER_FILL,
189 /* enough for PES header + length */
190 #define PES_START_SIZE 6
191 #define PES_HEADER_SIZE 9
192 #define MAX_PES_HEADER_SIZE (9 + 255)
194 typedef struct PESContext {
196 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
199 AVFormatContext *stream;
201 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
202 enum MpegTSState state;
203 /* used to get the format */
205 int flags; /**< copied to the AVPacket flags */
208 int extended_stream_id;
210 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
211 uint8_t header[MAX_PES_HEADER_SIZE];
216 extern AVInputFormat ff_mpegts_demuxer;
218 static void clear_program(MpegTSContext *ts, unsigned int programid)
222 for (i = 0; i < ts->nb_prg; i++)
223 if (ts->prg[i].id == programid)
224 ts->prg[i].nb_pids = 0;
227 static void clear_programs(MpegTSContext *ts)
233 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
236 if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
240 p = &ts->prg[ts->nb_prg];
246 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid,
250 struct Program *p = NULL;
251 for (i = 0; i < ts->nb_prg; i++) {
252 if (ts->prg[i].id == programid) {
260 if (p->nb_pids >= MAX_PIDS_PER_PROGRAM)
262 p->pids[p->nb_pids++] = pid;
266 * @brief discard_pid() decides if the pid is to be discarded according
267 * to caller's programs selection
268 * @param ts : - TS context
270 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
273 static int discard_pid(MpegTSContext *ts, unsigned int pid)
276 int used = 0, discarded = 0;
279 /* If none of the programs have .discard=AVDISCARD_ALL then there's
280 * no way we have to discard this packet */
281 for (k = 0; k < ts->stream->nb_programs; k++)
282 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
284 if (k == ts->stream->nb_programs)
287 for (i = 0; i < ts->nb_prg; i++) {
289 for (j = 0; j < p->nb_pids; j++) {
290 if (p->pids[j] != pid)
292 // is program with id p->id set to be discarded?
293 for (k = 0; k < ts->stream->nb_programs; k++) {
294 if (ts->stream->programs[k]->id == p->id) {
295 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
304 return !used && discarded;
308 * Assemble PES packets out of TS packets, and then call the "section_cb"
309 * function when they are complete.
311 static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1,
312 const uint8_t *buf, int buf_size, int is_start)
314 MpegTSSectionFilter *tss = &tss1->u.section_filter;
318 memcpy(tss->section_buf, buf, buf_size);
319 tss->section_index = buf_size;
320 tss->section_h_size = -1;
321 tss->end_of_section_reached = 0;
323 if (tss->end_of_section_reached)
325 len = 4096 - tss->section_index;
328 memcpy(tss->section_buf + tss->section_index, buf, len);
329 tss->section_index += len;
332 /* compute section length if possible */
333 if (tss->section_h_size == -1 && tss->section_index >= 3) {
334 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
337 tss->section_h_size = len;
340 if (tss->section_h_size != -1 &&
341 tss->section_index >= tss->section_h_size) {
342 tss->end_of_section_reached = 1;
343 if (!tss->check_crc ||
344 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
345 tss->section_buf, tss->section_h_size) == 0)
346 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
350 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts,
352 SectionCallback *section_cb,
356 MpegTSFilter *filter;
357 MpegTSSectionFilter *sec;
359 av_log(ts->stream, AV_LOG_TRACE, "Filter: pid=0x%x\n", pid);
361 if (pid >= NB_PID_MAX || ts->pids[pid])
363 filter = av_mallocz(sizeof(MpegTSFilter));
366 ts->pids[pid] = filter;
368 filter->type = MPEGTS_SECTION;
371 filter->last_cc = -1;
373 sec = &filter->u.section_filter;
374 sec->section_cb = section_cb;
375 sec->opaque = opaque;
376 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
377 sec->check_crc = check_crc;
380 if (!sec->section_buf) {
387 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
391 MpegTSFilter *filter;
392 MpegTSPESFilter *pes;
394 if (pid >= NB_PID_MAX || ts->pids[pid])
396 filter = av_mallocz(sizeof(MpegTSFilter));
400 ts->pids[pid] = filter;
401 filter->type = MPEGTS_PES;
404 filter->last_cc = -1;
406 pes = &filter->u.pes_filter;
407 pes->pes_cb = pes_cb;
408 pes->opaque = opaque;
412 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
417 if (filter->type == MPEGTS_SECTION)
418 av_freep(&filter->u.section_filter.section_buf);
419 else if (filter->type == MPEGTS_PES) {
420 PESContext *pes = filter->u.pes_filter.opaque;
421 av_buffer_unref(&pes->buffer);
422 /* referenced private data will be freed later in
423 * avformat_close_input */
424 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
425 av_freep(&filter->u.pes_filter.opaque);
430 ts->pids[pid] = NULL;
433 static int analyze(const uint8_t *buf, int size, int packet_size, int *index,
436 int stat[TS_MAX_PACKET_SIZE];
441 memset(stat, 0, packet_size * sizeof(int));
443 for (x = i = 0; i < size - 3; i++) {
444 if (buf[i] == 0x47 &&
445 (!probe || (!(buf[i + 1] & 0x80) && (buf[i + 3] & 0x30)))) {
447 if (stat[x] > best_score) {
448 best_score = stat[x];
455 if (x == packet_size)
462 /* autodetect fec presence. Must have at least 1024 bytes */
463 static int get_packet_size(const uint8_t *buf, int size)
465 int score, fec_score, dvhs_score;
467 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
468 return AVERROR_INVALIDDATA;
470 score = analyze(buf, size, TS_PACKET_SIZE, NULL, 0);
471 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL, 0);
472 fec_score = analyze(buf, size, TS_FEC_PACKET_SIZE, NULL, 0);
473 av_log(NULL, AV_LOG_TRACE, "score: %d, dvhs_score: %d, fec_score: %d \n",
474 score, dvhs_score, fec_score);
476 if (score > fec_score && score > dvhs_score)
477 return TS_PACKET_SIZE;
478 else if (dvhs_score > score && dvhs_score > fec_score)
479 return TS_DVHS_PACKET_SIZE;
480 else if (score < fec_score && dvhs_score < fec_score)
481 return TS_FEC_PACKET_SIZE;
483 return AVERROR_INVALIDDATA;
486 typedef struct SectionHeader {
491 uint8_t last_sec_num;
494 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
501 return AVERROR_INVALIDDATA;
507 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
513 if ((p + 1) >= p_end)
514 return AVERROR_INVALIDDATA;
521 /* read and allocate a DVB string preceded by its length */
522 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
529 len = get8(&p, p_end);
532 if ((p + len) > p_end)
534 str = av_malloc(len + 1);
544 static int parse_section_header(SectionHeader *h,
545 const uint8_t **pp, const uint8_t *p_end)
549 val = get8(pp, p_end);
554 val = get16(pp, p_end);
558 val = get8(pp, p_end);
561 h->version = (val >> 1) & 0x1f;
562 val = get8(pp, p_end);
566 val = get8(pp, p_end);
569 h->last_sec_num = val;
573 typedef struct StreamType {
574 uint32_t stream_type;
575 enum AVMediaType codec_type;
576 enum AVCodecID codec_id;
579 static const StreamType ISO_types[] = {
580 { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
581 { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
582 { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
583 { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
584 { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
585 { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
586 { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
587 { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
588 { 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000 },
589 { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
590 { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
591 { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
592 { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
596 static const StreamType HDMV_types[] = {
597 { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
598 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
599 { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
600 { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
601 { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
602 { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
603 { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
604 { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
609 static const StreamType MISC_types[] = {
610 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
611 { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
615 static const StreamType REGD_types[] = {
616 { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
617 { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
618 { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
619 { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
620 { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
621 { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
622 { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
623 { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
624 { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS },
628 /* descriptor present */
629 static const StreamType DESC_types[] = {
630 { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
631 { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
632 { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
633 { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
634 { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
638 static void mpegts_find_stream_type(AVStream *st,
639 uint32_t stream_type,
640 const StreamType *types)
642 for (; types->stream_type; types++)
643 if (stream_type == types->stream_type) {
644 st->codecpar->codec_type = types->codec_type;
645 st->codecpar->codec_id = types->codec_id;
650 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
651 uint32_t stream_type, uint32_t prog_reg_desc)
653 avpriv_set_pts_info(st, 33, 1, 90000);
655 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
656 st->codecpar->codec_id = AV_CODEC_ID_NONE;
657 st->need_parsing = AVSTREAM_PARSE_FULL;
659 pes->stream_type = stream_type;
661 av_log(pes->stream, AV_LOG_DEBUG,
662 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
663 st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
665 st->codecpar->codec_tag = pes->stream_type;
667 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
668 if (prog_reg_desc == AV_RL32("HDMV") &&
669 st->codecpar->codec_id == AV_CODEC_ID_NONE) {
670 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
671 if (pes->stream_type == 0x83) {
672 // HDMV TrueHD streams also contain an AC3 coded version of the
673 // audio track - add a second stream for this
675 // priv_data cannot be shared between streams
676 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
678 return AVERROR(ENOMEM);
679 memcpy(sub_pes, pes, sizeof(*sub_pes));
681 sub_st = avformat_new_stream(pes->stream, NULL);
684 return AVERROR(ENOMEM);
687 sub_st->id = pes->pid;
688 avpriv_set_pts_info(sub_st, 33, 1, 90000);
689 sub_st->priv_data = sub_pes;
690 sub_st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
691 sub_st->codecpar->codec_id = AV_CODEC_ID_AC3;
692 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
693 sub_pes->sub_st = pes->sub_st = sub_st;
696 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
697 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
702 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
706 pkt->buf = pes->buffer;
707 pkt->data = pes->buffer->data;
708 pkt->size = pes->data_index;
710 if (pes->total_size != MAX_PES_PAYLOAD &&
711 pes->pes_header_size + pes->data_index != pes->total_size +
713 av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
714 pes->flags |= AV_PKT_FLAG_CORRUPT;
716 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
718 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
719 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
720 pkt->stream_index = pes->sub_st->index;
722 pkt->stream_index = pes->st->index;
725 /* store position of first TS packet of this PES packet */
726 pkt->pos = pes->ts_packet_pos;
727 pkt->flags = pes->flags;
729 /* reset pts values */
730 pes->pts = AV_NOPTS_VALUE;
731 pes->dts = AV_NOPTS_VALUE;
737 static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
738 const uint8_t *buf, int buf_size)
741 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
742 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
743 int dts_flag = -1, cts_flag = -1;
744 int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
745 bitstream_init8(&bc, buf, buf_size);
747 if (sl->use_au_start)
748 au_start_flag = bitstream_read_bit(&bc);
750 au_end_flag = bitstream_read_bit(&bc);
751 if (!sl->use_au_start && !sl->use_au_end)
752 au_start_flag = au_end_flag = 1;
754 ocr_flag = bitstream_read_bit(&bc);
756 idle_flag = bitstream_read_bit(&bc);
758 padding_flag = bitstream_read_bit(&bc);
760 padding_bits = bitstream_read(&bc, 3);
762 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
763 if (sl->packet_seq_num_len)
764 bitstream_skip(&bc, sl->packet_seq_num_len);
765 if (sl->degr_prior_len)
766 if (bitstream_read_bit(&bc))
767 bitstream_skip(&bc, sl->degr_prior_len);
769 bitstream_skip(&bc, sl->ocr_len);
771 if (sl->use_rand_acc_pt)
772 bitstream_read_bit(&bc);
773 if (sl->au_seq_num_len > 0)
774 bitstream_skip(&bc, sl->au_seq_num_len);
775 if (sl->use_timestamps) {
776 dts_flag = bitstream_read_bit(&bc);
777 cts_flag = bitstream_read_bit(&bc);
780 if (sl->inst_bitrate_len)
781 inst_bitrate_flag = bitstream_read_bit(&bc);
783 dts = bitstream_read_63(&bc, sl->timestamp_len);
785 cts = bitstream_read_63(&bc, sl->timestamp_len);
787 bitstream_skip(&bc, sl->au_len);
788 if (inst_bitrate_flag)
789 bitstream_skip(&bc, sl->inst_bitrate_len);
792 if (dts != AV_NOPTS_VALUE)
794 if (cts != AV_NOPTS_VALUE)
797 if (sl->timestamp_len && sl->timestamp_res)
798 avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
800 return (bitstream_tell(&bc) + 7) >> 3;
803 /* return non zero if a packet could be constructed */
804 static int mpegts_push_data(MpegTSFilter *filter,
805 const uint8_t *buf, int buf_size, int is_start,
808 PESContext *pes = filter->u.pes_filter.opaque;
809 MpegTSContext *ts = pes->ts;
817 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
818 new_pes_packet(pes, ts->pkt);
821 pes->state = MPEGTS_HEADER;
823 pes->ts_packet_pos = pos;
826 while (buf_size > 0) {
827 switch (pes->state) {
829 len = PES_START_SIZE - pes->data_index;
832 memcpy(pes->header + pes->data_index, p, len);
833 pes->data_index += len;
836 if (pes->data_index == PES_START_SIZE) {
837 /* we got all the PES or section header. We can now
839 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
840 pes->header[2] == 0x01) {
841 /* it must be an MPEG-2 PES stream */
842 code = pes->header[3] | 0x100;
843 av_log(pes->stream, AV_LOG_TRACE, "pid=%x pes_code=%#x\n", pes->pid,
846 if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
848 pes->sub_st->discard == AVDISCARD_ALL)) ||
849 code == 0x1be) /* padding_stream */
852 /* stream not present in PMT */
854 pes->st = avformat_new_stream(ts->stream, NULL);
856 return AVERROR(ENOMEM);
857 pes->st->id = pes->pid;
858 mpegts_set_stream_info(pes->st, pes, 0, 0);
861 pes->total_size = AV_RB16(pes->header + 4);
862 /* NOTE: a zero total size means the PES size is
864 if (!pes->total_size)
865 pes->total_size = MAX_PES_PAYLOAD;
867 /* allocate pes buffer */
868 pes->buffer = av_buffer_alloc(pes->total_size +
869 AV_INPUT_BUFFER_PADDING_SIZE);
871 return AVERROR(ENOMEM);
873 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
874 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
875 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
876 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
877 pes->state = MPEGTS_PESHEADER;
878 if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE) {
879 av_log(pes->stream, AV_LOG_TRACE,
880 "pid=%x stream_type=%x probing\n",
883 pes->st->codecpar->codec_id = AV_CODEC_ID_PROBE;
886 pes->state = MPEGTS_PAYLOAD;
890 /* otherwise, it should be a table */
893 pes->state = MPEGTS_SKIP;
898 /**********************************************/
899 /* PES packing parsing */
900 case MPEGTS_PESHEADER:
901 len = PES_HEADER_SIZE - pes->data_index;
903 return AVERROR_INVALIDDATA;
906 memcpy(pes->header + pes->data_index, p, len);
907 pes->data_index += len;
910 if (pes->data_index == PES_HEADER_SIZE) {
911 pes->pes_header_size = pes->header[8] + 9;
912 pes->state = MPEGTS_PESHEADER_FILL;
915 case MPEGTS_PESHEADER_FILL:
916 len = pes->pes_header_size - pes->data_index;
918 return AVERROR_INVALIDDATA;
921 memcpy(pes->header + pes->data_index, p, len);
922 pes->data_index += len;
925 if (pes->data_index == pes->pes_header_size) {
927 unsigned int flags, pes_ext, skip;
929 flags = pes->header[7];
931 pes->pts = AV_NOPTS_VALUE;
932 pes->dts = AV_NOPTS_VALUE;
933 if ((flags & 0xc0) == 0x80) {
934 pes->dts = pes->pts = ff_parse_pes_pts(r);
936 } else if ((flags & 0xc0) == 0xc0) {
937 pes->pts = ff_parse_pes_pts(r);
939 pes->dts = ff_parse_pes_pts(r);
942 pes->extended_stream_id = -1;
943 if (flags & 0x01) { /* PES extension */
945 /* Skip PES private data, program packet sequence counter and P-STD buffer */
946 skip = (pes_ext >> 4) & 0xb;
949 if ((pes_ext & 0x41) == 0x01 &&
950 (r + 2) <= (pes->header + pes->pes_header_size)) {
951 /* PES extension 2 */
952 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
953 pes->extended_stream_id = r[1];
957 /* we got the full header. We parse it and get the payload */
958 pes->state = MPEGTS_PAYLOAD;
960 if (pes->stream_type == 0x12 && buf_size > 0) {
961 int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
963 pes->pes_header_size += sl_header_bytes;
964 p += sl_header_bytes;
965 buf_size -= sl_header_bytes;
970 if (buf_size > 0 && pes->buffer) {
971 if (pes->data_index > 0 &&
972 pes->data_index + buf_size > pes->total_size) {
973 new_pes_packet(pes, ts->pkt);
974 pes->total_size = MAX_PES_PAYLOAD;
975 pes->buffer = av_buffer_alloc(pes->total_size +
976 AV_INPUT_BUFFER_PADDING_SIZE);
978 return AVERROR(ENOMEM);
980 } else if (pes->data_index == 0 &&
981 buf_size > pes->total_size) {
982 // pes packet size is < ts size packet and pes data is padded with 0xff
983 // not sure if this is legal in ts but see issue #2392
984 buf_size = pes->total_size;
986 memcpy(pes->buffer->data + pes->data_index, p, buf_size);
987 pes->data_index += buf_size;
990 /* emit complete packets with known packet size
991 * decreases demuxer delay for infrequent packets like subtitles from
992 * a couple of seconds to milliseconds for properly muxed files.
993 * total_size is the number of bytes following pes_packet_length
994 * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
995 if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
996 pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
998 new_pes_packet(pes, ts->pkt);
1010 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
1015 /* if no pid found, then add a pid context */
1016 pes = av_mallocz(sizeof(PESContext));
1020 pes->stream = ts->stream;
1022 pes->pcr_pid = pcr_pid;
1023 pes->state = MPEGTS_SKIP;
1024 pes->pts = AV_NOPTS_VALUE;
1025 pes->dts = AV_NOPTS_VALUE;
1026 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1035 typedef struct MP4DescrParseContext {
1039 Mp4Descr *active_descr;
1041 int max_descr_count;
1043 } MP4DescrParseContext;
1045 static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s,
1046 const uint8_t *buf, unsigned size,
1047 Mp4Descr *descr, int max_descr_count)
1050 if (size > (1 << 30))
1051 return AVERROR_INVALIDDATA;
1053 if ((ret = ffio_init_context(&d->pb, (unsigned char *)buf, size, 0,
1054 NULL, NULL, NULL, NULL)) < 0)
1061 d->active_descr = NULL;
1062 d->max_descr_count = max_descr_count;
1067 static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1069 int64_t new_off = avio_tell(pb);
1070 (*len) -= new_off - *off;
1074 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1077 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1080 int ret = parse_mp4_descr(d, off, len, 0);
1083 update_offsets(&d->pb, &off, &len);
1088 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1090 avio_rb16(&d->pb); // ID
1096 update_offsets(&d->pb, &off, &len);
1097 return parse_mp4_descr_arr(d, off, len);
1100 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1105 id_flags = avio_rb16(&d->pb);
1106 if (!(id_flags & 0x0020)) { // URL_Flag
1107 update_offsets(&d->pb, &off, &len);
1108 return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1114 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1119 if (d->descr_count >= d->max_descr_count)
1120 return AVERROR_INVALIDDATA;
1121 ff_mp4_parse_es_descr(&d->pb, &es_id);
1122 d->active_descr = d->descr + (d->descr_count++);
1124 d->active_descr->es_id = es_id;
1125 update_offsets(&d->pb, &off, &len);
1126 if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
1128 update_offsets(&d->pb, &off, &len);
1130 ret = parse_mp4_descr(d, off, len, MP4SLDescrTag);
1131 d->active_descr = NULL;
1135 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off,
1138 Mp4Descr *descr = d->active_descr;
1140 return AVERROR_INVALIDDATA;
1141 d->active_descr->dec_config_descr = av_malloc(len);
1142 if (!descr->dec_config_descr)
1143 return AVERROR(ENOMEM);
1144 descr->dec_config_descr_len = len;
1145 avio_read(&d->pb, descr->dec_config_descr, len);
1149 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1151 Mp4Descr *descr = d->active_descr;
1154 return AVERROR_INVALIDDATA;
1156 predefined = avio_r8(&d->pb);
1159 int flags = avio_r8(&d->pb);
1160 descr->sl.use_au_start = !!(flags & 0x80);
1161 descr->sl.use_au_end = !!(flags & 0x40);
1162 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1163 descr->sl.use_padding = !!(flags & 0x08);
1164 descr->sl.use_timestamps = !!(flags & 0x04);
1165 descr->sl.use_idle = !!(flags & 0x02);
1166 descr->sl.timestamp_res = avio_rb32(&d->pb);
1168 descr->sl.timestamp_len = avio_r8(&d->pb);
1169 descr->sl.ocr_len = avio_r8(&d->pb);
1170 descr->sl.au_len = avio_r8(&d->pb);
1171 descr->sl.inst_bitrate_len = avio_r8(&d->pb);
1172 lengths = avio_rb16(&d->pb);
1173 descr->sl.degr_prior_len = lengths >> 12;
1174 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1175 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1176 if (descr->sl.timestamp_len >= 64 ||
1177 descr->sl.ocr_len >= 64 ||
1178 descr->sl.au_len >= 32) {
1179 return AVERROR_INVALIDDATA;
1182 avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1187 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1191 int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
1194 update_offsets(&d->pb, &off, &len);
1195 if (len < 0 || len1 > len || len1 <= 0) {
1196 av_log(d->s, AV_LOG_ERROR,
1197 "Tag %x length violation new length %d bytes remaining %d\n",
1199 return AVERROR_INVALIDDATA;
1202 if (d->level++ >= MAX_LEVEL) {
1203 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1204 ret = AVERROR_INVALIDDATA;
1208 if (target_tag && tag != target_tag) {
1209 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1211 ret = AVERROR_INVALIDDATA;
1217 ret = parse_MP4IODescrTag(d, off, len1);
1220 ret = parse_MP4ODescrTag(d, off, len1);
1223 ret = parse_MP4ESDescrTag(d, off, len1);
1225 case MP4DecConfigDescrTag:
1226 ret = parse_MP4DecConfigDescrTag(d, off, len1);
1229 ret = parse_MP4SLDescrTag(d, off, len1);
1236 avio_seek(&d->pb, off + len1, SEEK_SET);
1240 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1241 Mp4Descr *descr, int *descr_count, int max_descr_count)
1243 MP4DescrParseContext d;
1246 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1250 ret = parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
1252 *descr_count = d.descr_count;
1256 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1257 Mp4Descr *descr, int *descr_count, int max_descr_count)
1259 MP4DescrParseContext d;
1262 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1266 ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
1268 *descr_count = d.descr_count;
1272 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
1275 MpegTSContext *ts = filter->u.section_filter.opaque;
1276 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1278 const uint8_t *p, *p_end;
1280 int mp4_descr_count = 0;
1281 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1283 AVFormatContext *s = ts->stream;
1285 p_end = section + section_len - 4;
1287 if (parse_section_header(&h, &p, p_end) < 0)
1289 if (h.tid != M4OD_TID)
1291 if (h.version == tssf->last_ver)
1293 tssf->last_ver = h.version;
1295 mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1296 MAX_MP4_DESCR_COUNT);
1298 for (pid = 0; pid < NB_PID_MAX; pid++) {
1301 for (i = 0; i < mp4_descr_count; i++) {
1304 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1306 if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
1307 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1310 pes = ts->pids[pid]->u.pes_filter.opaque;
1315 pes->sl = mp4_descr[i].sl;
1317 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1318 mp4_descr[i].dec_config_descr_len, 0,
1319 NULL, NULL, NULL, NULL);
1320 ff_mp4_read_dec_config_descr(s, st, &pb);
1321 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1322 st->codecpar->extradata_size > 0)
1323 st->need_parsing = 0;
1324 if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
1325 st->codecpar->extradata_size > 0)
1326 st->need_parsing = 0;
1328 st->codecpar->codec_type = avcodec_get_type(st->codecpar->codec_id);
1331 for (i = 0; i < mp4_descr_count; i++)
1332 av_free(mp4_descr[i].dec_config_descr);
1335 static const uint8_t opus_coupled_stream_cnt[9] = {
1336 1, 0, 1, 1, 2, 2, 2, 3, 3
1339 static const uint8_t opus_stream_cnt[9] = {
1340 1, 1, 1, 2, 2, 3, 4, 4, 5,
1343 static const uint8_t opus_channel_map[8][8] = {
1351 { 0,6,1,2,3,4,5,7 },
1354 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
1355 const uint8_t **pp, const uint8_t *desc_list_end,
1356 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1359 const uint8_t *desc_end;
1360 int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1364 desc_tag = get8(pp, desc_list_end);
1366 return AVERROR_INVALIDDATA;
1367 desc_len = get8(pp, desc_list_end);
1369 return AVERROR_INVALIDDATA;
1370 desc_end = *pp + desc_len;
1371 if (desc_end > desc_list_end)
1372 return AVERROR_INVALIDDATA;
1374 av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1376 if (st->codecpar->codec_id == AV_CODEC_ID_NONE &&
1377 stream_type == STREAM_TYPE_PRIVATE_DATA)
1378 mpegts_find_stream_type(st, desc_tag, DESC_types);
1381 case 0x1E: /* SL descriptor */
1382 desc_es_id = get16(pp, desc_end);
1385 if (ts && ts->pids[pid])
1386 ts->pids[pid]->es_id = desc_es_id;
1387 for (i = 0; i < mp4_descr_count; i++)
1388 if (mp4_descr[i].dec_config_descr_len &&
1389 mp4_descr[i].es_id == desc_es_id) {
1391 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1392 mp4_descr[i].dec_config_descr_len, 0,
1393 NULL, NULL, NULL, NULL);
1394 ff_mp4_read_dec_config_descr(fc, st, &pb);
1395 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1396 st->codecpar->extradata_size > 0)
1397 st->need_parsing = 0;
1398 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
1399 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1402 case 0x1F: /* FMC descriptor */
1403 if (get16(pp, desc_end) < 0)
1405 if (mp4_descr_count > 0 &&
1406 st->codecpar->codec_id == AV_CODEC_ID_AAC_LATM &&
1407 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1409 ffio_init_context(&pb, mp4_descr->dec_config_descr,
1410 mp4_descr->dec_config_descr_len, 0,
1411 NULL, NULL, NULL, NULL);
1412 ff_mp4_read_dec_config_descr(fc, st, &pb);
1413 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1414 st->codecpar->extradata_size > 0)
1415 st->need_parsing = 0;
1418 case 0x56: /* DVB teletext descriptor */
1419 language[0] = get8(pp, desc_end);
1420 language[1] = get8(pp, desc_end);
1421 language[2] = get8(pp, desc_end);
1423 av_dict_set(&st->metadata, "language", language, 0);
1425 case 0x59: /* subtitling descriptor */
1426 language[0] = get8(pp, desc_end);
1427 language[1] = get8(pp, desc_end);
1428 language[2] = get8(pp, desc_end);
1430 /* hearing impaired subtitles detection */
1431 switch (get8(pp, desc_end)) {
1432 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1433 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1434 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1435 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1436 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1437 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1438 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
1441 if (st->codecpar->extradata) {
1442 if (st->codecpar->extradata_size == 4 &&
1443 memcmp(st->codecpar->extradata, *pp, 4))
1444 avpriv_request_sample(fc, "DVB sub with multiple IDs");
1446 st->codecpar->extradata = av_malloc(4 + AV_INPUT_BUFFER_PADDING_SIZE);
1447 if (st->codecpar->extradata) {
1448 st->codecpar->extradata_size = 4;
1449 memcpy(st->codecpar->extradata, *pp, 4);
1453 av_dict_set(&st->metadata, "language", language, 0);
1455 case 0x0a: /* ISO 639 language descriptor */
1456 for (i = 0; i + 4 <= desc_len; i += 4) {
1457 language[i + 0] = get8(pp, desc_end);
1458 language[i + 1] = get8(pp, desc_end);
1459 language[i + 2] = get8(pp, desc_end);
1460 language[i + 3] = ',';
1461 switch (get8(pp, desc_end)) {
1463 st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS;
1466 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
1469 st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
1473 if (i && language[0]) {
1474 language[i - 1] = 0;
1475 av_dict_set(&st->metadata, "language", language, 0);
1478 case 0x05: /* registration descriptor */
1479 st->codecpar->codec_tag = bytestream_get_le32(pp);
1480 av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
1481 if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
1482 mpegts_find_stream_type(st, st->codecpar->codec_tag, REGD_types);
1484 case 0x7f: /* DVB extension descriptor */
1485 ext_desc_tag = get8(pp, desc_end);
1486 if (ext_desc_tag < 0)
1487 return AVERROR_INVALIDDATA;
1488 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
1489 ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
1490 if (!st->codecpar->extradata) {
1491 st->codecpar->extradata = av_mallocz(sizeof(opus_default_extradata) +
1492 AV_INPUT_BUFFER_PADDING_SIZE);
1493 if (!st->codecpar->extradata)
1494 return AVERROR(ENOMEM);
1496 st->codecpar->extradata_size = sizeof(opus_default_extradata);
1497 memcpy(st->codecpar->extradata, opus_default_extradata, sizeof(opus_default_extradata));
1499 channel_config_code = get8(pp, desc_end);
1500 if (channel_config_code < 0)
1501 return AVERROR_INVALIDDATA;
1502 if (channel_config_code <= 0x8) {
1503 st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
1504 st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : 255;
1505 st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
1506 st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
1507 memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
1509 avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
1511 st->need_parsing = AVSTREAM_PARSE_FULL;
1522 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1524 MpegTSContext *ts = filter->u.section_filter.opaque;
1525 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1526 SectionHeader h1, *h = &h1;
1529 const uint8_t *p, *p_end, *desc_list_end;
1530 int program_info_length, pcr_pid, pid, stream_type;
1532 uint32_t prog_reg_desc = 0; /* registration descriptor */
1534 int mp4_descr_count = 0;
1535 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1538 av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len);
1539 hex_dump_debug(ts->stream, section, section_len);
1541 p_end = section + section_len - 4;
1543 if (parse_section_header(h, &p, p_end) < 0)
1545 if (h->version == tssf->last_ver)
1547 tssf->last_ver = h->version;
1549 av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d\n",
1550 h->id, h->sec_num, h->last_sec_num);
1552 if (h->tid != PMT_TID)
1555 clear_program(ts, h->id);
1556 pcr_pid = get16(&p, p_end);
1560 add_pid_to_pmt(ts, h->id, pcr_pid);
1562 av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid);
1564 program_info_length = get16(&p, p_end);
1565 if (program_info_length < 0)
1567 program_info_length &= 0xfff;
1568 while (program_info_length >= 2) {
1570 tag = get8(&p, p_end);
1571 len = get8(&p, p_end);
1573 av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len);
1575 if (len > program_info_length - 2)
1576 // something else is broken, exit the program_descriptors_loop
1578 program_info_length -= len + 2;
1579 if (tag == 0x1d) { // IOD descriptor
1580 get8(&p, p_end); // scope
1581 get8(&p, p_end); // label
1583 mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
1584 &mp4_descr_count, MAX_MP4_DESCR_COUNT);
1585 } else if (tag == 0x05 && len >= 4) { // registration descriptor
1586 prog_reg_desc = bytestream_get_le32(&p);
1591 p += program_info_length;
1595 // stop parsing after pmt, we found header
1596 if (!ts->stream->nb_streams)
1603 stream_type = get8(&p, p_end);
1604 if (stream_type < 0)
1606 pid = get16(&p, p_end);
1611 /* now create stream */
1612 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
1613 pes = ts->pids[pid]->u.pes_filter.opaque;
1615 pes->st = avformat_new_stream(pes->stream, NULL);
1616 pes->st->id = pes->pid;
1619 } else if (stream_type != 0x13) {
1621 mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
1622 pes = add_pes_stream(ts, pid, pcr_pid);
1624 st = avformat_new_stream(pes->stream, NULL);
1628 int idx = ff_find_stream_index(ts->stream, pid);
1630 st = ts->stream->streams[idx];
1632 st = avformat_new_stream(ts->stream, NULL);
1634 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
1641 if (pes && !pes->stream_type)
1642 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
1644 add_pid_to_pmt(ts, h->id, pid);
1646 ff_program_add_stream_index(ts->stream, h->id, st->index);
1648 desc_list_len = get16(&p, p_end);
1649 if (desc_list_len < 0)
1651 desc_list_len &= 0xfff;
1652 desc_list_end = p + desc_list_len;
1653 if (desc_list_end > p_end)
1656 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
1657 desc_list_end, mp4_descr,
1658 mp4_descr_count, pid, ts) < 0)
1661 if (pes && prog_reg_desc == AV_RL32("HDMV") &&
1662 stream_type == 0x83 && pes->sub_st) {
1663 ff_program_add_stream_index(ts->stream, h->id,
1664 pes->sub_st->index);
1665 pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag;
1672 for (i = 0; i < mp4_descr_count; i++)
1673 av_free(mp4_descr[i].dec_config_descr);
1676 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1678 MpegTSContext *ts = filter->u.section_filter.opaque;
1679 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1680 SectionHeader h1, *h = &h1;
1681 const uint8_t *p, *p_end;
1684 av_log(ts->stream, AV_LOG_TRACE, "PAT:\n");
1685 hex_dump_debug(ts->stream, section, section_len);
1687 p_end = section + section_len - 4;
1689 if (parse_section_header(h, &p, p_end) < 0)
1691 if (h->tid != PAT_TID)
1693 if (h->version == tssf->last_ver)
1695 tssf->last_ver = h->version;
1699 sid = get16(&p, p_end);
1702 pmt_pid = get16(&p, p_end);
1707 av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1709 if (sid == 0x0000) {
1712 av_new_program(ts->stream, sid);
1713 if (ts->pids[pmt_pid])
1714 mpegts_close_filter(ts, ts->pids[pmt_pid]);
1715 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
1716 add_pat_entry(ts, sid);
1717 add_pid_to_pmt(ts, sid, 0); // add pat pid to program
1718 add_pid_to_pmt(ts, sid, pmt_pid);
1723 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1725 MpegTSContext *ts = filter->u.section_filter.opaque;
1726 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1727 SectionHeader h1, *h = &h1;
1728 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1729 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1730 char *name, *provider_name;
1732 av_log(ts->stream, AV_LOG_TRACE, "SDT:\n");
1733 hex_dump_debug(ts->stream, section, section_len);
1735 p_end = section + section_len - 4;
1737 if (parse_section_header(h, &p, p_end) < 0)
1739 if (h->tid != SDT_TID)
1741 if (h->version == tssf->last_ver)
1743 tssf->last_ver = h->version;
1745 onid = get16(&p, p_end);
1748 val = get8(&p, p_end);
1752 sid = get16(&p, p_end);
1755 val = get8(&p, p_end);
1758 desc_list_len = get16(&p, p_end);
1759 if (desc_list_len < 0)
1761 desc_list_len &= 0xfff;
1762 desc_list_end = p + desc_list_len;
1763 if (desc_list_end > p_end)
1766 desc_tag = get8(&p, desc_list_end);
1769 desc_len = get8(&p, desc_list_end);
1770 desc_end = p + desc_len;
1771 if (desc_end > desc_list_end)
1774 av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n",
1775 desc_tag, desc_len);
1779 service_type = get8(&p, p_end);
1780 if (service_type < 0)
1782 provider_name = getstr8(&p, p_end);
1785 name = getstr8(&p, p_end);
1787 AVProgram *program = av_new_program(ts->stream, sid);
1789 av_dict_set(&program->metadata, "service_name", name, 0);
1790 av_dict_set(&program->metadata, "service_provider",
1795 av_free(provider_name);
1806 /* handle one TS packet */
1807 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1810 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1811 has_adaptation, has_payload;
1812 const uint8_t *p, *p_end;
1815 pid = AV_RB16(packet + 1) & 0x1fff;
1816 if (pid && discard_pid(ts, pid))
1818 is_start = packet[1] & 0x40;
1819 tss = ts->pids[pid];
1820 if (ts->auto_guess && !tss && is_start) {
1821 add_pes_stream(ts, pid, -1);
1822 tss = ts->pids[pid];
1827 afc = (packet[3] >> 4) & 3;
1828 if (afc == 0) /* reserved value */
1830 has_adaptation = afc & 2;
1831 has_payload = afc & 1;
1832 is_discontinuity = has_adaptation &&
1833 packet[4] != 0 && /* with length > 0 */
1834 (packet[5] & 0x80); /* and discontinuity indicated */
1836 /* continuity check (currently not used) */
1837 cc = (packet[3] & 0xf);
1838 expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
1839 cc_ok = pid == 0x1FFF || // null packet PID
1846 av_log(ts->stream, AV_LOG_WARNING,
1847 "Continuity check failed for pid %d expected %d got %d\n",
1848 pid, expected_cc, cc);
1849 if (tss->type == MPEGTS_PES) {
1850 PESContext *pc = tss->u.pes_filter.opaque;
1851 pc->flags |= AV_PKT_FLAG_CORRUPT;
1858 if (has_adaptation) {
1859 /* skip adaptation field */
1862 /* if past the end of packet, ignore */
1863 p_end = packet + TS_PACKET_SIZE;
1867 pos = avio_tell(ts->stream->pb);
1868 MOD_UNLIKELY(ts->pos47, pos, ts->raw_packet_size, ts->pos);
1870 if (tss->type == MPEGTS_SECTION) {
1872 /* pointer field present */
1874 if (p + len > p_end)
1877 /* write remaining section bytes */
1878 write_section_data(ts, tss,
1880 /* check whether filter has been closed */
1886 write_section_data(ts, tss,
1891 write_section_data(ts, tss,
1897 // Note: The position here points actually behind the current packet.
1898 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1899 pos - ts->raw_packet_size)) < 0)
1906 /* XXX: try to find a better synchro over several packets (use
1907 * get_packet_size() ?) */
1908 static int mpegts_resync(AVFormatContext *s)
1910 MpegTSContext *ts = s->priv_data;
1911 AVIOContext *pb = s->pb;
1914 for (i = 0; i < ts->resync_size; i++) {
1916 if (pb->eof_reached)
1919 avio_seek(pb, -1, SEEK_CUR);
1923 av_log(s, AV_LOG_ERROR,
1924 "max resync size reached, could not find sync byte\n");
1926 return AVERROR_INVALIDDATA;
1929 /* return AVERROR_something if error or EOF. Return 0 if OK. */
1930 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
1931 const uint8_t **data)
1933 AVIOContext *pb = s->pb;
1937 len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
1938 if (len != TS_PACKET_SIZE)
1939 return len < 0 ? len : AVERROR_EOF;
1940 /* check packet sync byte */
1941 if ((*data)[0] != 0x47) {
1942 /* find a new packet start */
1943 avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1944 if (mpegts_resync(s) < 0)
1945 return AVERROR(EAGAIN);
1955 static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
1957 AVIOContext *pb = s->pb;
1958 int skip = raw_packet_size - TS_PACKET_SIZE;
1960 avio_skip(pb, skip);
1963 static int handle_packets(MpegTSContext *ts, int nb_packets)
1965 AVFormatContext *s = ts->stream;
1966 uint8_t packet[TS_PACKET_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
1967 const uint8_t *data;
1968 int packet_num, ret = 0;
1970 if (avio_tell(s->pb) != ts->last_pos) {
1972 av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n");
1973 /* seek detected, flush pes buffer */
1974 for (i = 0; i < NB_PID_MAX; i++) {
1976 if (ts->pids[i]->type == MPEGTS_PES) {
1977 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1978 av_buffer_unref(&pes->buffer);
1979 pes->data_index = 0;
1980 pes->state = MPEGTS_SKIP; /* skip until pes header */
1982 ts->pids[i]->last_cc = -1;
1989 memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1991 if (ts->stop_parse > 0)
1994 if (nb_packets != 0 && packet_num >= nb_packets)
1996 ret = read_packet(s, packet, ts->raw_packet_size, &data);
1999 ret = handle_packet(ts, data);
2000 finished_reading_packet(s, ts->raw_packet_size);
2004 ts->last_pos = avio_tell(s->pb);
2008 static int mpegts_probe(AVProbeData *p)
2010 const int size = p->buf_size;
2011 int score, fec_score, dvhs_score;
2012 int check_count = size / TS_FEC_PACKET_SIZE;
2013 #define CHECK_COUNT 10
2015 if (check_count < CHECK_COUNT)
2016 return AVERROR_INVALIDDATA;
2018 score = analyze(p->buf, TS_PACKET_SIZE * check_count,
2019 TS_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
2020 dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * check_count,
2021 TS_DVHS_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
2022 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE * check_count,
2023 TS_FEC_PACKET_SIZE, NULL, 1) * CHECK_COUNT / check_count;
2024 av_log(NULL, AV_LOG_TRACE, "score: %d, dvhs_score: %d, fec_score: %d \n",
2025 score, dvhs_score, fec_score);
2027 /* we need a clear definition for the returned score otherwise
2028 * things will become messy sooner or later */
2029 if (score > fec_score && score > dvhs_score && score > 6)
2030 return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
2031 else if (dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6)
2032 return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
2033 else if (fec_score > 6)
2034 return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
2036 return AVERROR_INVALIDDATA;
2039 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
2040 * (-1) if not available */
2041 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
2043 int afc, len, flags;
2047 afc = (packet[3] >> 4) & 3;
2049 return AVERROR_INVALIDDATA;
2054 return AVERROR_INVALIDDATA;
2057 if (!(flags & 0x10))
2058 return AVERROR_INVALIDDATA;
2060 return AVERROR_INVALIDDATA;
2062 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
2063 *ppcr_low = ((p[4] & 1) << 8) | p[5];
2067 static int mpegts_read_header(AVFormatContext *s)
2069 MpegTSContext *ts = s->priv_data;
2070 AVIOContext *pb = s->pb;
2071 uint8_t buf[5 * 1024];
2075 /* read the first 1024 bytes to get packet size */
2076 pos = avio_tell(pb);
2077 len = avio_read(pb, buf, sizeof(buf));
2080 if (len != sizeof(buf))
2082 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
2083 if (ts->raw_packet_size <= 0)
2084 return AVERROR_INVALIDDATA;
2088 if (s->iformat == &ff_mpegts_demuxer) {
2091 /* first do a scan to get all the services */
2092 if (avio_seek(pb, pos, SEEK_SET) < 0 &&
2093 (pb->seekable & AVIO_SEEKABLE_NORMAL))
2094 av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
2096 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
2098 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
2100 handle_packets(ts, s->probesize / ts->raw_packet_size);
2101 /* if could not find service, enable auto_guess */
2105 av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
2107 s->ctx_flags |= AVFMTCTX_NOHEADER;
2110 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
2111 int64_t pcrs[2], pcr_h;
2112 int packet_count[2];
2113 uint8_t packet[TS_PACKET_SIZE];
2114 const uint8_t *data;
2116 /* only read packets */
2118 st = avformat_new_stream(s, NULL);
2120 return AVERROR(ENOMEM);
2121 avpriv_set_pts_info(st, 60, 1, 27000000);
2122 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
2123 st->codecpar->codec_id = AV_CODEC_ID_MPEG2TS;
2125 /* we iterate until we find two PCRs to estimate the bitrate */
2130 ret = read_packet(s, packet, ts->raw_packet_size, &data);
2133 pid = AV_RB16(data + 1) & 0x1fff;
2134 if ((pcr_pid == -1 || pcr_pid == pid) &&
2135 parse_pcr(&pcr_h, &pcr_l, data) == 0) {
2136 finished_reading_packet(s, ts->raw_packet_size);
2138 packet_count[nb_pcrs] = nb_packets;
2139 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2144 finished_reading_packet(s, ts->raw_packet_size);
2149 /* NOTE1: the bitrate is computed without the FEC */
2150 /* NOTE2: it is only the bitrate of the start of the stream */
2151 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2152 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
2153 s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;
2154 st->codecpar->bit_rate = s->bit_rate;
2155 st->start_time = ts->cur_pcr;
2156 av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%d\n",
2157 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
2160 avio_seek(pb, pos, SEEK_SET);
2164 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2166 static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
2168 MpegTSContext *ts = s->priv_data;
2170 int64_t pcr_h, next_pcr_h, pos;
2171 int pcr_l, next_pcr_l;
2172 uint8_t pcr_buf[12];
2173 const uint8_t *data;
2175 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
2176 return AVERROR(ENOMEM);
2177 ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
2178 pkt->pos = avio_tell(s->pb);
2180 av_packet_unref(pkt);
2183 if (data != pkt->data)
2184 memcpy(pkt->data, data, ts->raw_packet_size);
2185 finished_reading_packet(s, ts->raw_packet_size);
2186 if (ts->mpeg2ts_compute_pcr) {
2187 /* compute exact PCR for each packet */
2188 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
2189 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2190 pos = avio_tell(s->pb);
2191 for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
2192 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
2193 avio_read(s->pb, pcr_buf, 12);
2194 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2195 /* XXX: not precise enough */
2197 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2202 avio_seek(s->pb, pos, SEEK_SET);
2203 /* no next PCR found: we use previous increment */
2204 ts->cur_pcr = pcr_h * 300 + pcr_l;
2206 pkt->pts = ts->cur_pcr;
2207 pkt->duration = ts->pcr_incr;
2208 ts->cur_pcr += ts->pcr_incr;
2210 pkt->stream_index = 0;
2214 static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
2216 MpegTSContext *ts = s->priv_data;
2221 ret = handle_packets(ts, 0);
2223 /* flush pes data left */
2224 for (i = 0; i < NB_PID_MAX; i++)
2225 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
2226 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
2227 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
2228 new_pes_packet(pes, pkt);
2229 pes->state = MPEGTS_SKIP;
2236 if (!ret && pkt->size < 0)
2237 ret = AVERROR(EINTR);
2241 static void mpegts_free(MpegTSContext *ts)
2247 for (i = 0; i < NB_PID_MAX; i++)
2249 mpegts_close_filter(ts, ts->pids[i]);
2252 static int mpegts_read_close(AVFormatContext *s)
2254 MpegTSContext *ts = s->priv_data;
2259 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
2260 int64_t *ppos, int64_t pos_limit)
2262 MpegTSContext *ts = s->priv_data;
2263 int64_t pos, timestamp;
2264 uint8_t buf[TS_PACKET_SIZE];
2265 int pcr_l, pcr_pid =
2266 ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
2267 const int find_next = 1;
2269 ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) *
2270 ts->raw_packet_size + ts->pos47;
2273 avio_seek(s->pb, pos, SEEK_SET);
2274 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2275 return AV_NOPTS_VALUE;
2276 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2277 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2280 pos += ts->raw_packet_size;
2284 pos -= ts->raw_packet_size;
2286 return AV_NOPTS_VALUE;
2287 avio_seek(s->pb, pos, SEEK_SET);
2288 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2289 return AV_NOPTS_VALUE;
2290 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2291 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2301 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
2303 MpegTSContext *ts = s->priv_data;
2304 uint8_t buf[TS_PACKET_SIZE];
2308 ret = ff_seek_frame_binary(s, stream_index, target_ts, flags);
2312 pos = avio_tell(s->pb);
2315 avio_seek(s->pb, pos, SEEK_SET);
2316 ret = avio_read(s->pb, buf, TS_PACKET_SIZE);
2319 if (ret != TS_PACKET_SIZE)
2321 // pid = AV_RB16(buf + 1) & 0x1fff;
2324 pos += ts->raw_packet_size;
2326 avio_seek(s->pb, pos, SEEK_SET);
2331 /**************************************************************/
2332 /* parsing functions - called from other demuxers such as RTP */
2334 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
2338 ts = av_mallocz(sizeof(MpegTSContext));
2341 /* no stream case, currently used by RTP */
2342 ts->raw_packet_size = TS_PACKET_SIZE;
2348 /* return the consumed length if a packet was output, or -1 if no
2349 * packet is output */
2350 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
2351 const uint8_t *buf, int len)
2359 if (ts->stop_parse > 0)
2361 if (len < TS_PACKET_SIZE)
2362 return AVERROR_INVALIDDATA;
2363 if (buf[0] != 0x47) {
2367 handle_packet(ts, buf);
2368 buf += TS_PACKET_SIZE;
2369 len -= TS_PACKET_SIZE;
2375 void ff_mpegts_parse_close(MpegTSContext *ts)
2381 AVInputFormat ff_mpegts_demuxer = {
2383 .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2384 .priv_data_size = sizeof(MpegTSContext),
2385 .read_probe = mpegts_probe,
2386 .read_header = mpegts_read_header,
2387 .read_packet = mpegts_read_packet,
2388 .read_close = mpegts_read_close,
2389 .read_seek = read_seek,
2390 .read_timestamp = mpegts_get_pcr,
2391 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
2392 .priv_class = &mpegts_class,
2395 AVInputFormat ff_mpegtsraw_demuxer = {
2396 .name = "mpegtsraw",
2397 .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
2398 .priv_data_size = sizeof(MpegTSContext),
2399 .read_header = mpegts_read_header,
2400 .read_packet = mpegts_raw_read_packet,
2401 .read_close = mpegts_read_close,
2402 .read_seek = read_seek,
2403 .read_timestamp = mpegts_get_pcr,
2404 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
2405 .priv_class = &mpegtsraw_class,