2 * MPEG2 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"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/get_bits.h"
34 #include "avio_internal.h"
39 /* maximum size in which we look for synchronisation if
40 * synchronisation is lost */
41 #define MAX_RESYNC_SIZE 65536
43 #define MAX_PES_PAYLOAD 200 * 1024
45 #define MAX_MP4_DESCR_COUNT 16
47 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
49 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
50 (modulus) = (dividend) % (divisor); \
51 (prev_dividend) = (dividend); \
54 enum MpegTSFilterType {
59 typedef struct MpegTSFilter MpegTSFilter;
61 typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
62 int is_start, int64_t pos);
64 typedef struct MpegTSPESFilter {
69 typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len);
71 typedef void SetServiceCallback (void *opaque, int ret);
73 typedef struct MpegTSSectionFilter {
77 unsigned int check_crc : 1;
78 unsigned int end_of_section_reached : 1;
79 SectionCallback *section_cb;
81 } MpegTSSectionFilter;
86 int last_cc; /* last cc code (-1 if first packet) */
87 enum MpegTSFilterType type;
89 MpegTSPESFilter pes_filter;
90 MpegTSSectionFilter section_filter;
94 #define MAX_PIDS_PER_PROGRAM 64
96 unsigned int id; // program id/service id
98 unsigned int pids[MAX_PIDS_PER_PROGRAM];
101 struct MpegTSContext {
102 const AVClass *class;
104 AVFormatContext *stream;
105 /** raw packet size, including FEC if present */
109 /** position corresponding to pos47, or 0 if pos47 invalid */
112 /** if true, all pids are analyzed to find streams */
115 /** compute exact PCR for each transport stream packet */
116 int mpeg2ts_compute_pcr;
118 int64_t cur_pcr; /**< used to estimate the exact PCR */
119 int pcr_incr; /**< used to estimate the exact PCR */
121 /* data needed to handle file based ts */
122 /** stop parsing loop */
124 /** packet containing Audio/Video data */
126 /** to detect seek */
129 /******************************************/
130 /* private mpegts data */
132 /** structure to keep track of Program->pids mapping */
136 /** filters for various streams specified by PMT + for the PAT and PMT */
137 MpegTSFilter *pids[NB_PID_MAX];
140 static const AVOption options[] = {
141 { "compute_pcr", "Compute exact PCR for each transport stream packet.",
142 offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
143 { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
144 { "ts_packetsize", "Output option carrying the raw packet size.",
145 offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
147 AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY },
151 static const AVClass mpegtsraw_class = {
152 .class_name = "mpegtsraw demuxer",
153 .item_name = av_default_item_name,
155 .version = LIBAVUTIL_VERSION_INT,
158 /* TS stream handling */
163 MPEGTS_PESHEADER_FILL,
168 /* enough for PES header + length */
169 #define PES_START_SIZE 6
170 #define PES_HEADER_SIZE 9
171 #define MAX_PES_HEADER_SIZE (9 + 255)
173 typedef struct PESContext {
175 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
178 AVFormatContext *stream;
180 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
181 enum MpegTSState state;
182 /* used to get the format */
184 int flags; /**< copied to the AVPacket flags */
187 int extended_stream_id;
189 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
190 uint8_t header[MAX_PES_HEADER_SIZE];
195 extern AVInputFormat ff_mpegts_demuxer;
197 static void clear_program(MpegTSContext *ts, unsigned int programid)
201 for (i = 0; i < ts->nb_prg; i++)
202 if (ts->prg[i].id == programid)
203 ts->prg[i].nb_pids = 0;
206 static void clear_programs(MpegTSContext *ts)
212 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
215 if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
219 p = &ts->prg[ts->nb_prg];
225 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid,
229 struct Program *p = NULL;
230 for (i = 0; i < ts->nb_prg; i++) {
231 if (ts->prg[i].id == programid) {
239 if (p->nb_pids >= MAX_PIDS_PER_PROGRAM)
241 p->pids[p->nb_pids++] = pid;
245 * @brief discard_pid() decides if the pid is to be discarded according
246 * to caller's programs selection
247 * @param ts : - TS context
249 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
252 static int discard_pid(MpegTSContext *ts, unsigned int pid)
255 int used = 0, discarded = 0;
258 /* If none of the programs have .discard=AVDISCARD_ALL then there's
259 * no way we have to discard this packet */
260 for (k = 0; k < ts->stream->nb_programs; k++)
261 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
263 if (k == ts->stream->nb_programs)
266 for (i = 0; i < ts->nb_prg; i++) {
268 for (j = 0; j < p->nb_pids; j++) {
269 if (p->pids[j] != pid)
271 // is program with id p->id set to be discarded?
272 for (k = 0; k < ts->stream->nb_programs; k++) {
273 if (ts->stream->programs[k]->id == p->id) {
274 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
283 return !used && discarded;
287 * Assemble PES packets out of TS packets, and then call the "section_cb"
288 * function when they are complete.
290 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
291 const uint8_t *buf, int buf_size, int is_start)
293 MpegTSSectionFilter *tss = &tss1->u.section_filter;
297 memcpy(tss->section_buf, buf, buf_size);
298 tss->section_index = buf_size;
299 tss->section_h_size = -1;
300 tss->end_of_section_reached = 0;
302 if (tss->end_of_section_reached)
304 len = 4096 - tss->section_index;
307 memcpy(tss->section_buf + tss->section_index, buf, len);
308 tss->section_index += len;
311 /* compute section length if possible */
312 if (tss->section_h_size == -1 && tss->section_index >= 3) {
313 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
316 tss->section_h_size = len;
319 if (tss->section_h_size != -1 &&
320 tss->section_index >= tss->section_h_size) {
321 tss->end_of_section_reached = 1;
322 if (!tss->check_crc ||
323 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
324 tss->section_buf, tss->section_h_size) == 0)
325 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
329 static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts,
331 SectionCallback *section_cb,
335 MpegTSFilter *filter;
336 MpegTSSectionFilter *sec;
338 av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
340 if (pid >= NB_PID_MAX || ts->pids[pid])
342 filter = av_mallocz(sizeof(MpegTSFilter));
345 ts->pids[pid] = filter;
347 filter->type = MPEGTS_SECTION;
350 filter->last_cc = -1;
352 sec = &filter->u.section_filter;
353 sec->section_cb = section_cb;
354 sec->opaque = opaque;
355 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
356 sec->check_crc = check_crc;
357 if (!sec->section_buf) {
364 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
368 MpegTSFilter *filter;
369 MpegTSPESFilter *pes;
371 if (pid >= NB_PID_MAX || ts->pids[pid])
373 filter = av_mallocz(sizeof(MpegTSFilter));
377 ts->pids[pid] = filter;
378 filter->type = MPEGTS_PES;
381 filter->last_cc = -1;
383 pes = &filter->u.pes_filter;
384 pes->pes_cb = pes_cb;
385 pes->opaque = opaque;
389 static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
394 if (filter->type == MPEGTS_SECTION)
395 av_freep(&filter->u.section_filter.section_buf);
396 else if (filter->type == MPEGTS_PES) {
397 PESContext *pes = filter->u.pes_filter.opaque;
398 av_buffer_unref(&pes->buffer);
399 /* referenced private data will be freed later in
400 * avformat_close_input */
401 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
402 av_freep(&filter->u.pes_filter.opaque);
407 ts->pids[pid] = NULL;
410 static int analyze(const uint8_t *buf, int size, int packet_size, int *index)
412 int stat[TS_MAX_PACKET_SIZE];
417 memset(stat, 0, packet_size * sizeof(int));
419 for (x = i = 0; i < size - 3; i++) {
420 if (buf[i] == 0x47 && !(buf[i + 1] & 0x80) && (buf[i + 3] & 0x30)) {
422 if (stat[x] > best_score) {
423 best_score = stat[x];
430 if (x == packet_size)
437 /* autodetect fec presence. Must have at least 1024 bytes */
438 static int get_packet_size(const uint8_t *buf, int size)
440 int score, fec_score, dvhs_score;
442 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
443 return AVERROR_INVALIDDATA;
445 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
446 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
447 fec_score = analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
448 av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
449 score, dvhs_score, fec_score);
451 if (score > fec_score && score > dvhs_score)
452 return TS_PACKET_SIZE;
453 else if (dvhs_score > score && dvhs_score > fec_score)
454 return TS_DVHS_PACKET_SIZE;
455 else if (score < fec_score && dvhs_score < fec_score)
456 return TS_FEC_PACKET_SIZE;
458 return AVERROR_INVALIDDATA;
461 typedef struct SectionHeader {
466 uint8_t last_sec_num;
469 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
476 return AVERROR_INVALIDDATA;
482 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
488 if ((p + 1) >= p_end)
489 return AVERROR_INVALIDDATA;
496 /* read and allocate a DVB string preceded by its length */
497 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
504 len = get8(&p, p_end);
507 if ((p + len) > p_end)
509 str = av_malloc(len + 1);
519 static int parse_section_header(SectionHeader *h,
520 const uint8_t **pp, const uint8_t *p_end)
524 val = get8(pp, p_end);
529 val = get16(pp, p_end);
533 val = get8(pp, p_end);
536 h->version = (val >> 1) & 0x1f;
537 val = get8(pp, p_end);
541 val = get8(pp, p_end);
544 h->last_sec_num = val;
549 uint32_t stream_type;
550 enum AVMediaType codec_type;
551 enum AVCodecID codec_id;
554 static const StreamType ISO_types[] = {
555 { 0x01, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
556 { 0x02, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG2VIDEO },
557 { 0x03, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
558 { 0x04, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_MP3 },
559 { 0x0f, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC },
560 { 0x10, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_MPEG4 },
561 { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
562 { 0x1b, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264 },
563 { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
564 { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS },
565 { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
566 { 0xea, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
570 static const StreamType HDMV_types[] = {
571 { 0x80, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_PCM_BLURAY },
572 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
573 { 0x82, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
574 { 0x83, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_TRUEHD },
575 { 0x84, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
576 { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
577 { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
578 { 0x90, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_HDMV_PGS_SUBTITLE },
583 static const StreamType MISC_types[] = {
584 { 0x81, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
585 { 0x8a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
589 static const StreamType REGD_types[] = {
590 { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
591 { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
592 { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
593 { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
594 { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
595 { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
596 { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
597 { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
601 /* descriptor present */
602 static const StreamType DESC_types[] = {
603 { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
604 { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
605 { 0x7b, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
606 { 0x56, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_TELETEXT },
607 { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
611 static void mpegts_find_stream_type(AVStream *st,
612 uint32_t stream_type,
613 const StreamType *types)
615 for (; types->stream_type; types++)
616 if (stream_type == types->stream_type) {
617 st->codec->codec_type = types->codec_type;
618 st->codec->codec_id = types->codec_id;
623 static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
624 uint32_t stream_type, uint32_t prog_reg_desc)
626 avpriv_set_pts_info(st, 33, 1, 90000);
628 st->codec->codec_type = AVMEDIA_TYPE_DATA;
629 st->codec->codec_id = AV_CODEC_ID_NONE;
630 st->need_parsing = AVSTREAM_PARSE_FULL;
632 pes->stream_type = stream_type;
634 av_log(pes->stream, AV_LOG_DEBUG,
635 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
636 st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
638 st->codec->codec_tag = pes->stream_type;
640 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
641 if (prog_reg_desc == AV_RL32("HDMV") &&
642 st->codec->codec_id == AV_CODEC_ID_NONE) {
643 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
644 if (pes->stream_type == 0x83) {
645 // HDMV TrueHD streams also contain an AC3 coded version of the
646 // audio track - add a second stream for this
648 // priv_data cannot be shared between streams
649 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
651 return AVERROR(ENOMEM);
652 memcpy(sub_pes, pes, sizeof(*sub_pes));
654 sub_st = avformat_new_stream(pes->stream, NULL);
657 return AVERROR(ENOMEM);
660 sub_st->id = pes->pid;
661 avpriv_set_pts_info(sub_st, 33, 1, 90000);
662 sub_st->priv_data = sub_pes;
663 sub_st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
664 sub_st->codec->codec_id = AV_CODEC_ID_AC3;
665 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
666 sub_pes->sub_st = pes->sub_st = sub_st;
669 if (st->codec->codec_id == AV_CODEC_ID_NONE)
670 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
675 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
679 pkt->buf = pes->buffer;
680 pkt->data = pes->buffer->data;
681 pkt->size = pes->data_index;
683 if (pes->total_size != MAX_PES_PAYLOAD &&
684 pes->pes_header_size + pes->data_index != pes->total_size +
686 av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
687 pes->flags |= AV_PKT_FLAG_CORRUPT;
689 memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
691 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
692 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
693 pkt->stream_index = pes->sub_st->index;
695 pkt->stream_index = pes->st->index;
698 /* store position of first TS packet of this PES packet */
699 pkt->pos = pes->ts_packet_pos;
700 pkt->flags = pes->flags;
702 /* reset pts values */
703 pes->pts = AV_NOPTS_VALUE;
704 pes->dts = AV_NOPTS_VALUE;
710 static int read_sl_header(PESContext *pes, SLConfigDescr *sl,
711 const uint8_t *buf, int buf_size)
714 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
715 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
716 int dts_flag = -1, cts_flag = -1;
717 int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
718 init_get_bits(&gb, buf, buf_size * 8);
720 if (sl->use_au_start)
721 au_start_flag = get_bits1(&gb);
723 au_end_flag = get_bits1(&gb);
724 if (!sl->use_au_start && !sl->use_au_end)
725 au_start_flag = au_end_flag = 1;
727 ocr_flag = get_bits1(&gb);
729 idle_flag = get_bits1(&gb);
731 padding_flag = get_bits1(&gb);
733 padding_bits = get_bits(&gb, 3);
735 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
736 if (sl->packet_seq_num_len)
737 skip_bits_long(&gb, sl->packet_seq_num_len);
738 if (sl->degr_prior_len)
740 skip_bits(&gb, sl->degr_prior_len);
742 skip_bits_long(&gb, sl->ocr_len);
744 if (sl->use_rand_acc_pt)
746 if (sl->au_seq_num_len > 0)
747 skip_bits_long(&gb, sl->au_seq_num_len);
748 if (sl->use_timestamps) {
749 dts_flag = get_bits1(&gb);
750 cts_flag = get_bits1(&gb);
753 if (sl->inst_bitrate_len)
754 inst_bitrate_flag = get_bits1(&gb);
756 dts = get_bits64(&gb, sl->timestamp_len);
758 cts = get_bits64(&gb, sl->timestamp_len);
760 skip_bits_long(&gb, sl->au_len);
761 if (inst_bitrate_flag)
762 skip_bits_long(&gb, sl->inst_bitrate_len);
765 if (dts != AV_NOPTS_VALUE)
767 if (cts != AV_NOPTS_VALUE)
770 if (sl->timestamp_len && sl->timestamp_res)
771 avpriv_set_pts_info(pes->st, sl->timestamp_len, 1, sl->timestamp_res);
773 return (get_bits_count(&gb) + 7) >> 3;
776 /* return non zero if a packet could be constructed */
777 static int mpegts_push_data(MpegTSFilter *filter,
778 const uint8_t *buf, int buf_size, int is_start,
781 PESContext *pes = filter->u.pes_filter.opaque;
782 MpegTSContext *ts = pes->ts;
790 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
791 new_pes_packet(pes, ts->pkt);
794 pes->state = MPEGTS_HEADER;
796 pes->ts_packet_pos = pos;
799 while (buf_size > 0) {
800 switch (pes->state) {
802 len = PES_START_SIZE - pes->data_index;
805 memcpy(pes->header + pes->data_index, p, len);
806 pes->data_index += len;
809 if (pes->data_index == PES_START_SIZE) {
810 /* we got all the PES or section header. We can now
812 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
813 pes->header[2] == 0x01) {
814 /* it must be an mpeg2 PES stream */
815 code = pes->header[3] | 0x100;
816 av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid,
819 if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
821 pes->sub_st->discard == AVDISCARD_ALL)) ||
822 code == 0x1be) /* padding_stream */
825 /* stream not present in PMT */
827 pes->st = avformat_new_stream(ts->stream, NULL);
829 return AVERROR(ENOMEM);
830 pes->st->id = pes->pid;
831 mpegts_set_stream_info(pes->st, pes, 0, 0);
834 pes->total_size = AV_RB16(pes->header + 4);
835 /* NOTE: a zero total size means the PES size is
837 if (!pes->total_size)
838 pes->total_size = MAX_PES_PAYLOAD;
840 /* allocate pes buffer */
841 pes->buffer = av_buffer_alloc(pes->total_size +
842 FF_INPUT_BUFFER_PADDING_SIZE);
844 return AVERROR(ENOMEM);
846 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
847 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
848 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
849 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
850 pes->state = MPEGTS_PESHEADER;
851 if (pes->st->codec->codec_id == AV_CODEC_ID_NONE) {
853 "pid=%x stream_type=%x probing\n",
856 pes->st->codec->codec_id = AV_CODEC_ID_PROBE;
859 pes->state = MPEGTS_PAYLOAD;
863 /* otherwise, it should be a table */
866 pes->state = MPEGTS_SKIP;
871 /**********************************************/
872 /* PES packing parsing */
873 case MPEGTS_PESHEADER:
874 len = PES_HEADER_SIZE - pes->data_index;
876 return AVERROR_INVALIDDATA;
879 memcpy(pes->header + pes->data_index, p, len);
880 pes->data_index += len;
883 if (pes->data_index == PES_HEADER_SIZE) {
884 pes->pes_header_size = pes->header[8] + 9;
885 pes->state = MPEGTS_PESHEADER_FILL;
888 case MPEGTS_PESHEADER_FILL:
889 len = pes->pes_header_size - pes->data_index;
891 return AVERROR_INVALIDDATA;
894 memcpy(pes->header + pes->data_index, p, len);
895 pes->data_index += len;
898 if (pes->data_index == pes->pes_header_size) {
900 unsigned int flags, pes_ext, skip;
902 flags = pes->header[7];
904 pes->pts = AV_NOPTS_VALUE;
905 pes->dts = AV_NOPTS_VALUE;
906 if ((flags & 0xc0) == 0x80) {
907 pes->dts = pes->pts = ff_parse_pes_pts(r);
909 } else if ((flags & 0xc0) == 0xc0) {
910 pes->pts = ff_parse_pes_pts(r);
912 pes->dts = ff_parse_pes_pts(r);
915 pes->extended_stream_id = -1;
916 if (flags & 0x01) { /* PES extension */
918 /* Skip PES private data, program packet sequence counter and P-STD buffer */
919 skip = (pes_ext >> 4) & 0xb;
922 if ((pes_ext & 0x41) == 0x01 &&
923 (r + 2) <= (pes->header + pes->pes_header_size)) {
924 /* PES extension 2 */
925 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
926 pes->extended_stream_id = r[1];
930 /* we got the full header. We parse it and get the payload */
931 pes->state = MPEGTS_PAYLOAD;
933 if (pes->stream_type == 0x12 && buf_size > 0) {
934 int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
936 pes->pes_header_size += sl_header_bytes;
937 p += sl_header_bytes;
938 buf_size -= sl_header_bytes;
943 if (buf_size > 0 && pes->buffer) {
944 if (pes->data_index > 0 &&
945 pes->data_index + buf_size > pes->total_size) {
946 new_pes_packet(pes, ts->pkt);
947 pes->total_size = MAX_PES_PAYLOAD;
948 pes->buffer = av_buffer_alloc(pes->total_size +
949 FF_INPUT_BUFFER_PADDING_SIZE);
951 return AVERROR(ENOMEM);
953 } else if (pes->data_index == 0 &&
954 buf_size > pes->total_size) {
955 // pes packet size is < ts size packet and pes data is padded with 0xff
956 // not sure if this is legal in ts but see issue #2392
957 buf_size = pes->total_size;
959 memcpy(pes->buffer->data + pes->data_index, p, buf_size);
960 pes->data_index += buf_size;
963 /* emit complete packets with known packet size
964 * decreases demuxer delay for infrequent packets like subtitles from
965 * a couple of seconds to milliseconds for properly muxed files.
966 * total_size is the number of bytes following pes_packet_length
967 * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
968 if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
969 pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
971 new_pes_packet(pes, ts->pkt);
983 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
988 /* if no pid found, then add a pid context */
989 pes = av_mallocz(sizeof(PESContext));
993 pes->stream = ts->stream;
995 pes->pcr_pid = pcr_pid;
996 pes->state = MPEGTS_SKIP;
997 pes->pts = AV_NOPTS_VALUE;
998 pes->dts = AV_NOPTS_VALUE;
999 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1012 Mp4Descr *active_descr;
1014 int max_descr_count;
1016 } MP4DescrParseContext;
1018 static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s,
1019 const uint8_t *buf, unsigned size,
1020 Mp4Descr *descr, int max_descr_count)
1023 if (size > (1 << 30))
1024 return AVERROR_INVALIDDATA;
1026 if ((ret = ffio_init_context(&d->pb, (unsigned char *)buf, size, 0,
1027 NULL, NULL, NULL, NULL)) < 0)
1034 d->active_descr = NULL;
1035 d->max_descr_count = max_descr_count;
1040 static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1042 int64_t new_off = avio_tell(pb);
1043 (*len) -= new_off - *off;
1047 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1050 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1053 int ret = parse_mp4_descr(d, off, len, 0);
1056 update_offsets(&d->pb, &off, &len);
1061 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1063 avio_rb16(&d->pb); // ID
1069 update_offsets(&d->pb, &off, &len);
1070 return parse_mp4_descr_arr(d, off, len);
1073 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1078 id_flags = avio_rb16(&d->pb);
1079 if (!(id_flags & 0x0020)) { // URL_Flag
1080 update_offsets(&d->pb, &off, &len);
1081 return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1087 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1090 if (d->descr_count >= d->max_descr_count)
1091 return AVERROR_INVALIDDATA;
1092 ff_mp4_parse_es_descr(&d->pb, &es_id);
1093 d->active_descr = d->descr + (d->descr_count++);
1095 d->active_descr->es_id = es_id;
1096 update_offsets(&d->pb, &off, &len);
1097 parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
1098 update_offsets(&d->pb, &off, &len);
1100 parse_mp4_descr(d, off, len, MP4SLDescrTag);
1101 d->active_descr = NULL;
1105 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off,
1108 Mp4Descr *descr = d->active_descr;
1110 return AVERROR_INVALIDDATA;
1111 d->active_descr->dec_config_descr = av_malloc(len);
1112 if (!descr->dec_config_descr)
1113 return AVERROR(ENOMEM);
1114 descr->dec_config_descr_len = len;
1115 avio_read(&d->pb, descr->dec_config_descr, len);
1119 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1121 Mp4Descr *descr = d->active_descr;
1124 return AVERROR_INVALIDDATA;
1126 predefined = avio_r8(&d->pb);
1129 int flags = avio_r8(&d->pb);
1130 descr->sl.use_au_start = !!(flags & 0x80);
1131 descr->sl.use_au_end = !!(flags & 0x40);
1132 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1133 descr->sl.use_padding = !!(flags & 0x08);
1134 descr->sl.use_timestamps = !!(flags & 0x04);
1135 descr->sl.use_idle = !!(flags & 0x02);
1136 descr->sl.timestamp_res = avio_rb32(&d->pb);
1138 descr->sl.timestamp_len = avio_r8(&d->pb);
1139 descr->sl.ocr_len = avio_r8(&d->pb);
1140 descr->sl.au_len = avio_r8(&d->pb);
1141 descr->sl.inst_bitrate_len = avio_r8(&d->pb);
1142 lengths = avio_rb16(&d->pb);
1143 descr->sl.degr_prior_len = lengths >> 12;
1144 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1145 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1147 avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1152 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1156 int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
1157 update_offsets(&d->pb, &off, &len);
1158 if (len < 0 || len1 > len || len1 <= 0) {
1159 av_log(d->s, AV_LOG_ERROR,
1160 "Tag %x length violation new length %d bytes remaining %d\n",
1162 return AVERROR_INVALIDDATA;
1165 if (d->level++ >= MAX_LEVEL) {
1166 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1170 if (target_tag && tag != target_tag) {
1171 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1178 parse_MP4IODescrTag(d, off, len1);
1181 parse_MP4ODescrTag(d, off, len1);
1184 parse_MP4ESDescrTag(d, off, len1);
1186 case MP4DecConfigDescrTag:
1187 parse_MP4DecConfigDescrTag(d, off, len1);
1190 parse_MP4SLDescrTag(d, off, len1);
1197 avio_seek(&d->pb, off + len1, SEEK_SET);
1201 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1202 Mp4Descr *descr, int *descr_count, int max_descr_count)
1204 MP4DescrParseContext d;
1207 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1211 ret = parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
1213 *descr_count = d.descr_count;
1217 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1218 Mp4Descr *descr, int *descr_count, int max_descr_count)
1220 MP4DescrParseContext d;
1223 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1227 ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
1229 *descr_count = d.descr_count;
1233 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
1236 MpegTSContext *ts = filter->u.section_filter.opaque;
1238 const uint8_t *p, *p_end;
1240 int mp4_descr_count = 0;
1241 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1243 AVFormatContext *s = ts->stream;
1245 p_end = section + section_len - 4;
1247 if (parse_section_header(&h, &p, p_end) < 0)
1249 if (h.tid != M4OD_TID)
1252 mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1253 MAX_MP4_DESCR_COUNT);
1255 for (pid = 0; pid < NB_PID_MAX; pid++) {
1258 for (i = 0; i < mp4_descr_count; i++) {
1261 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1263 if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
1264 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1267 pes = ts->pids[pid]->u.pes_filter.opaque;
1272 pes->sl = mp4_descr[i].sl;
1274 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1275 mp4_descr[i].dec_config_descr_len, 0,
1276 NULL, NULL, NULL, NULL);
1277 ff_mp4_read_dec_config_descr(s, st, &pb);
1278 if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1279 st->codec->extradata_size > 0)
1280 st->need_parsing = 0;
1281 if (st->codec->codec_id == AV_CODEC_ID_H264 &&
1282 st->codec->extradata_size > 0)
1283 st->need_parsing = 0;
1285 if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
1287 } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO)
1288 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
1289 else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE)
1290 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
1291 else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN)
1292 st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
1295 for (i = 0; i < mp4_descr_count; i++)
1296 av_free(mp4_descr[i].dec_config_descr);
1299 int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type,
1300 const uint8_t **pp, const uint8_t *desc_list_end,
1301 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1304 const uint8_t *desc_end;
1305 int desc_len, desc_tag, desc_es_id;
1309 desc_tag = get8(pp, desc_list_end);
1311 return AVERROR_INVALIDDATA;
1312 desc_len = get8(pp, desc_list_end);
1314 return AVERROR_INVALIDDATA;
1315 desc_end = *pp + desc_len;
1316 if (desc_end > desc_list_end)
1317 return AVERROR_INVALIDDATA;
1319 av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1321 if (st->codec->codec_id == AV_CODEC_ID_NONE &&
1322 stream_type == STREAM_TYPE_PRIVATE_DATA)
1323 mpegts_find_stream_type(st, desc_tag, DESC_types);
1326 case 0x1E: /* SL descriptor */
1327 desc_es_id = get16(pp, desc_end);
1328 if (ts && ts->pids[pid])
1329 ts->pids[pid]->es_id = desc_es_id;
1330 for (i = 0; i < mp4_descr_count; i++)
1331 if (mp4_descr[i].dec_config_descr_len &&
1332 mp4_descr[i].es_id == desc_es_id) {
1334 ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1335 mp4_descr[i].dec_config_descr_len, 0,
1336 NULL, NULL, NULL, NULL);
1337 ff_mp4_read_dec_config_descr(fc, st, &pb);
1338 if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1339 st->codec->extradata_size > 0)
1340 st->need_parsing = 0;
1341 if (st->codec->codec_id == AV_CODEC_ID_MPEG4SYSTEMS)
1342 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1345 case 0x1F: /* FMC descriptor */
1346 get16(pp, desc_end);
1347 if (mp4_descr_count > 0 &&
1348 st->codec->codec_id == AV_CODEC_ID_AAC_LATM &&
1349 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1351 ffio_init_context(&pb, mp4_descr->dec_config_descr,
1352 mp4_descr->dec_config_descr_len, 0,
1353 NULL, NULL, NULL, NULL);
1354 ff_mp4_read_dec_config_descr(fc, st, &pb);
1355 if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1356 st->codec->extradata_size > 0)
1357 st->need_parsing = 0;
1360 case 0x56: /* DVB teletext descriptor */
1361 language[0] = get8(pp, desc_end);
1362 language[1] = get8(pp, desc_end);
1363 language[2] = get8(pp, desc_end);
1365 av_dict_set(&st->metadata, "language", language, 0);
1367 case 0x59: /* subtitling descriptor */
1368 language[0] = get8(pp, desc_end);
1369 language[1] = get8(pp, desc_end);
1370 language[2] = get8(pp, desc_end);
1372 /* hearing impaired subtitles detection */
1373 switch (get8(pp, desc_end)) {
1374 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1375 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1376 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1377 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1378 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1379 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1380 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
1383 if (st->codec->extradata) {
1384 if (st->codec->extradata_size == 4 &&
1385 memcmp(st->codec->extradata, *pp, 4))
1386 avpriv_request_sample(fc, "DVB sub with multiple IDs");
1388 st->codec->extradata = av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE);
1389 if (st->codec->extradata) {
1390 st->codec->extradata_size = 4;
1391 memcpy(st->codec->extradata, *pp, 4);
1395 av_dict_set(&st->metadata, "language", language, 0);
1397 case 0x0a: /* ISO 639 language descriptor */
1398 for (i = 0; i + 4 <= desc_len; i += 4) {
1399 language[i + 0] = get8(pp, desc_end);
1400 language[i + 1] = get8(pp, desc_end);
1401 language[i + 2] = get8(pp, desc_end);
1402 language[i + 3] = ',';
1403 switch (get8(pp, desc_end)) {
1405 st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS;
1408 st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED;
1411 st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED;
1416 language[i - 1] = 0;
1417 av_dict_set(&st->metadata, "language", language, 0);
1420 case 0x05: /* registration descriptor */
1421 st->codec->codec_tag = bytestream_get_le32(pp);
1422 av_dlog(fc, "reg_desc=%.4s\n", (char *)&st->codec->codec_tag);
1423 if (st->codec->codec_id == AV_CODEC_ID_NONE)
1424 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
1433 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1435 MpegTSContext *ts = filter->u.section_filter.opaque;
1436 SectionHeader h1, *h = &h1;
1439 const uint8_t *p, *p_end, *desc_list_end;
1440 int program_info_length, pcr_pid, pid, stream_type;
1442 uint32_t prog_reg_desc = 0; /* registration descriptor */
1444 int mp4_descr_count = 0;
1445 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1448 av_dlog(ts->stream, "PMT: len %i\n", section_len);
1449 hex_dump_debug(ts->stream, section, section_len);
1451 p_end = section + section_len - 4;
1453 if (parse_section_header(h, &p, p_end) < 0)
1456 av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
1457 h->id, h->sec_num, h->last_sec_num);
1459 if (h->tid != PMT_TID)
1462 clear_program(ts, h->id);
1463 pcr_pid = get16(&p, p_end);
1467 add_pid_to_pmt(ts, h->id, pcr_pid);
1469 av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
1471 program_info_length = get16(&p, p_end);
1472 if (program_info_length < 0)
1474 program_info_length &= 0xfff;
1475 while (program_info_length >= 2) {
1477 tag = get8(&p, p_end);
1478 len = get8(&p, p_end);
1480 av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
1482 if (len > program_info_length - 2)
1483 // something else is broken, exit the program_descriptors_loop
1485 program_info_length -= len + 2;
1486 if (tag == 0x1d) { // IOD descriptor
1487 get8(&p, p_end); // scope
1488 get8(&p, p_end); // label
1490 mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
1491 &mp4_descr_count, MAX_MP4_DESCR_COUNT);
1492 } else if (tag == 0x05 && len >= 4) { // registration descriptor
1493 prog_reg_desc = bytestream_get_le32(&p);
1498 p += program_info_length;
1502 // stop parsing after pmt, we found header
1503 if (!ts->stream->nb_streams)
1510 stream_type = get8(&p, p_end);
1511 if (stream_type < 0)
1513 pid = get16(&p, p_end);
1518 /* now create stream */
1519 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
1520 pes = ts->pids[pid]->u.pes_filter.opaque;
1522 pes->st = avformat_new_stream(pes->stream, NULL);
1523 pes->st->id = pes->pid;
1526 } else if (stream_type != 0x13) {
1528 mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
1529 pes = add_pes_stream(ts, pid, pcr_pid);
1531 st = avformat_new_stream(pes->stream, NULL);
1535 int idx = ff_find_stream_index(ts->stream, pid);
1537 st = ts->stream->streams[idx];
1539 st = avformat_new_stream(ts->stream, NULL);
1541 st->codec->codec_type = AVMEDIA_TYPE_DATA;
1548 if (pes && !pes->stream_type)
1549 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
1551 add_pid_to_pmt(ts, h->id, pid);
1553 ff_program_add_stream_index(ts->stream, h->id, st->index);
1555 desc_list_len = get16(&p, p_end);
1556 if (desc_list_len < 0)
1558 desc_list_len &= 0xfff;
1559 desc_list_end = p + desc_list_len;
1560 if (desc_list_end > p_end)
1563 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
1564 desc_list_end, mp4_descr,
1565 mp4_descr_count, pid, ts) < 0)
1568 if (pes && prog_reg_desc == AV_RL32("HDMV") &&
1569 stream_type == 0x83 && pes->sub_st) {
1570 ff_program_add_stream_index(ts->stream, h->id,
1571 pes->sub_st->index);
1572 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
1579 for (i = 0; i < mp4_descr_count; i++)
1580 av_free(mp4_descr[i].dec_config_descr);
1583 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1585 MpegTSContext *ts = filter->u.section_filter.opaque;
1586 SectionHeader h1, *h = &h1;
1587 const uint8_t *p, *p_end;
1590 av_dlog(ts->stream, "PAT:\n");
1591 hex_dump_debug(ts->stream, section, section_len);
1593 p_end = section + section_len - 4;
1595 if (parse_section_header(h, &p, p_end) < 0)
1597 if (h->tid != PAT_TID)
1602 sid = get16(&p, p_end);
1605 pmt_pid = get16(&p, p_end);
1610 av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1612 if (sid == 0x0000) {
1615 av_new_program(ts->stream, sid);
1616 if (ts->pids[pmt_pid])
1617 mpegts_close_filter(ts, ts->pids[pmt_pid]);
1618 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
1619 add_pat_entry(ts, sid);
1620 add_pid_to_pmt(ts, sid, 0); // add pat pid to program
1621 add_pid_to_pmt(ts, sid, pmt_pid);
1626 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1628 MpegTSContext *ts = filter->u.section_filter.opaque;
1629 SectionHeader h1, *h = &h1;
1630 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1631 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1632 char *name, *provider_name;
1634 av_dlog(ts->stream, "SDT:\n");
1635 hex_dump_debug(ts->stream, section, section_len);
1637 p_end = section + section_len - 4;
1639 if (parse_section_header(h, &p, p_end) < 0)
1641 if (h->tid != SDT_TID)
1643 onid = get16(&p, p_end);
1646 val = get8(&p, p_end);
1650 sid = get16(&p, p_end);
1653 val = get8(&p, p_end);
1656 desc_list_len = get16(&p, p_end);
1657 if (desc_list_len < 0)
1659 desc_list_len &= 0xfff;
1660 desc_list_end = p + desc_list_len;
1661 if (desc_list_end > p_end)
1664 desc_tag = get8(&p, desc_list_end);
1667 desc_len = get8(&p, desc_list_end);
1668 desc_end = p + desc_len;
1669 if (desc_end > desc_list_end)
1672 av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
1673 desc_tag, desc_len);
1677 service_type = get8(&p, p_end);
1678 if (service_type < 0)
1680 provider_name = getstr8(&p, p_end);
1683 name = getstr8(&p, p_end);
1685 AVProgram *program = av_new_program(ts->stream, sid);
1687 av_dict_set(&program->metadata, "service_name", name, 0);
1688 av_dict_set(&program->metadata, "service_provider",
1693 av_free(provider_name);
1704 /* handle one TS packet */
1705 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1707 AVFormatContext *s = ts->stream;
1709 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1710 has_adaptation, has_payload;
1711 const uint8_t *p, *p_end;
1714 pid = AV_RB16(packet + 1) & 0x1fff;
1715 if (pid && discard_pid(ts, pid))
1717 is_start = packet[1] & 0x40;
1718 tss = ts->pids[pid];
1719 if (ts->auto_guess && tss == NULL && is_start) {
1720 add_pes_stream(ts, pid, -1);
1721 tss = ts->pids[pid];
1726 afc = (packet[3] >> 4) & 3;
1727 if (afc == 0) /* reserved value */
1729 has_adaptation = afc & 2;
1730 has_payload = afc & 1;
1731 is_discontinuity = has_adaptation &&
1732 packet[4] != 0 && /* with length > 0 */
1733 (packet[5] & 0x80); /* and discontinuity indicated */
1735 /* continuity check (currently not used) */
1736 cc = (packet[3] & 0xf);
1737 expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
1738 cc_ok = pid == 0x1FFF || // null packet PID
1745 av_log(ts->stream, AV_LOG_WARNING,
1746 "Continuity check failed for pid %d expected %d got %d\n",
1747 pid, expected_cc, cc);
1748 if (tss->type == MPEGTS_PES) {
1749 PESContext *pc = tss->u.pes_filter.opaque;
1750 pc->flags |= AV_PKT_FLAG_CORRUPT;
1757 if (has_adaptation) {
1758 /* skip adaptation field */
1761 /* if past the end of packet, ignore */
1762 p_end = packet + TS_PACKET_SIZE;
1766 pos = avio_tell(ts->stream->pb);
1767 MOD_UNLIKELY(ts->pos47, pos, ts->raw_packet_size, ts->pos);
1769 if (tss->type == MPEGTS_SECTION) {
1771 /* pointer field present */
1773 if (p + len > p_end)
1776 /* write remaining section bytes */
1777 write_section_data(s, tss,
1779 /* check whether filter has been closed */
1785 write_section_data(s, tss,
1790 write_section_data(s, tss,
1796 // Note: The position here points actually behind the current packet.
1797 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1798 pos - ts->raw_packet_size)) < 0)
1805 /* XXX: try to find a better synchro over several packets (use
1806 * get_packet_size() ?) */
1807 static int mpegts_resync(AVFormatContext *s)
1809 AVIOContext *pb = s->pb;
1812 for (i = 0; i < MAX_RESYNC_SIZE; i++) {
1814 if (pb->eof_reached)
1817 avio_seek(pb, -1, SEEK_CUR);
1821 av_log(s, AV_LOG_ERROR,
1822 "max resync size reached, could not find sync byte\n");
1824 return AVERROR_INVALIDDATA;
1827 /* return AVERROR_something if error or EOF. Return 0 if OK. */
1828 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
1829 const uint8_t **data)
1831 AVIOContext *pb = s->pb;
1835 len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
1836 if (len != TS_PACKET_SIZE)
1837 return len < 0 ? len : AVERROR_EOF;
1838 /* check packet sync byte */
1839 if ((*data)[0] != 0x47) {
1840 /* find a new packet start */
1841 avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1842 if (mpegts_resync(s) < 0)
1843 return AVERROR(EAGAIN);
1853 static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
1855 AVIOContext *pb = s->pb;
1856 int skip = raw_packet_size - TS_PACKET_SIZE;
1858 avio_skip(pb, skip);
1861 static int handle_packets(MpegTSContext *ts, int nb_packets)
1863 AVFormatContext *s = ts->stream;
1864 uint8_t packet[TS_PACKET_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
1865 const uint8_t *data;
1866 int packet_num, ret = 0;
1868 if (avio_tell(s->pb) != ts->last_pos) {
1870 av_dlog(ts->stream, "Skipping after seek\n");
1871 /* seek detected, flush pes buffer */
1872 for (i = 0; i < NB_PID_MAX; i++) {
1874 if (ts->pids[i]->type == MPEGTS_PES) {
1875 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1876 av_buffer_unref(&pes->buffer);
1877 pes->data_index = 0;
1878 pes->state = MPEGTS_SKIP; /* skip until pes header */
1880 ts->pids[i]->last_cc = -1;
1887 memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1889 if (ts->stop_parse > 0)
1892 if (nb_packets != 0 && packet_num >= nb_packets)
1894 ret = read_packet(s, packet, ts->raw_packet_size, &data);
1897 ret = handle_packet(ts, data);
1898 finished_reading_packet(s, ts->raw_packet_size);
1902 ts->last_pos = avio_tell(s->pb);
1906 static int mpegts_probe(AVProbeData *p)
1908 const int size = p->buf_size;
1909 int score, fec_score, dvhs_score;
1910 int check_count = size / TS_FEC_PACKET_SIZE;
1911 #define CHECK_COUNT 10
1913 if (check_count < CHECK_COUNT)
1914 return AVERROR_INVALIDDATA;
1916 score = analyze(p->buf, TS_PACKET_SIZE * check_count,
1917 TS_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
1918 dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE * check_count,
1919 TS_DVHS_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
1920 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE * check_count,
1921 TS_FEC_PACKET_SIZE, NULL) * CHECK_COUNT / check_count;
1922 av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
1923 score, dvhs_score, fec_score);
1925 /* we need a clear definition for the returned score otherwise
1926 * things will become messy sooner or later */
1927 if (score > fec_score && score > dvhs_score && score > 6)
1928 return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1929 else if (dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6)
1930 return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
1931 else if (fec_score > 6)
1932 return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1934 return AVERROR_INVALIDDATA;
1937 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1938 * (-1) if not available */
1939 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
1941 int afc, len, flags;
1945 afc = (packet[3] >> 4) & 3;
1947 return AVERROR_INVALIDDATA;
1952 return AVERROR_INVALIDDATA;
1955 if (!(flags & 0x10))
1956 return AVERROR_INVALIDDATA;
1958 return AVERROR_INVALIDDATA;
1960 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
1961 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1965 static int mpegts_read_header(AVFormatContext *s)
1967 MpegTSContext *ts = s->priv_data;
1968 AVIOContext *pb = s->pb;
1969 uint8_t buf[5 * 1024];
1973 /* read the first 1024 bytes to get packet size */
1974 pos = avio_tell(pb);
1975 len = avio_read(pb, buf, sizeof(buf));
1978 if (len != sizeof(buf))
1980 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1981 if (ts->raw_packet_size <= 0)
1982 return AVERROR_INVALIDDATA;
1986 if (s->iformat == &ff_mpegts_demuxer) {
1989 /* first do a scan to get all the services */
1990 if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)
1991 av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
1993 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
1995 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
1997 handle_packets(ts, s->probesize / ts->raw_packet_size);
1998 /* if could not find service, enable auto_guess */
2002 av_dlog(ts->stream, "tuning done\n");
2004 s->ctx_flags |= AVFMTCTX_NOHEADER;
2007 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
2008 int64_t pcrs[2], pcr_h;
2009 int packet_count[2];
2010 uint8_t packet[TS_PACKET_SIZE];
2011 const uint8_t *data;
2013 /* only read packets */
2015 st = avformat_new_stream(s, NULL);
2017 return AVERROR(ENOMEM);
2018 avpriv_set_pts_info(st, 60, 1, 27000000);
2019 st->codec->codec_type = AVMEDIA_TYPE_DATA;
2020 st->codec->codec_id = AV_CODEC_ID_MPEG2TS;
2022 /* we iterate until we find two PCRs to estimate the bitrate */
2027 ret = read_packet(s, packet, ts->raw_packet_size, &data);
2030 pid = AV_RB16(data + 1) & 0x1fff;
2031 if ((pcr_pid == -1 || pcr_pid == pid) &&
2032 parse_pcr(&pcr_h, &pcr_l, data) == 0) {
2033 finished_reading_packet(s, ts->raw_packet_size);
2035 packet_count[nb_pcrs] = nb_packets;
2036 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
2041 finished_reading_packet(s, ts->raw_packet_size);
2046 /* NOTE1: the bitrate is computed without the FEC */
2047 /* NOTE2: it is only the bitrate of the start of the stream */
2048 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
2049 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
2050 s->bit_rate = TS_PACKET_SIZE * 8 * 27e6 / ts->pcr_incr;
2051 st->codec->bit_rate = s->bit_rate;
2052 st->start_time = ts->cur_pcr;
2053 av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
2054 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
2057 avio_seek(pb, pos, SEEK_SET);
2061 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2063 static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
2065 MpegTSContext *ts = s->priv_data;
2067 int64_t pcr_h, next_pcr_h, pos;
2068 int pcr_l, next_pcr_l;
2069 uint8_t pcr_buf[12];
2070 const uint8_t *data;
2072 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
2073 return AVERROR(ENOMEM);
2074 ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
2075 pkt->pos = avio_tell(s->pb);
2077 av_free_packet(pkt);
2080 if (data != pkt->data)
2081 memcpy(pkt->data, data, ts->raw_packet_size);
2082 finished_reading_packet(s, ts->raw_packet_size);
2083 if (ts->mpeg2ts_compute_pcr) {
2084 /* compute exact PCR for each packet */
2085 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
2086 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2087 pos = avio_tell(s->pb);
2088 for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
2089 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
2090 avio_read(s->pb, pcr_buf, 12);
2091 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2092 /* XXX: not precise enough */
2094 ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2099 avio_seek(s->pb, pos, SEEK_SET);
2100 /* no next PCR found: we use previous increment */
2101 ts->cur_pcr = pcr_h * 300 + pcr_l;
2103 pkt->pts = ts->cur_pcr;
2104 pkt->duration = ts->pcr_incr;
2105 ts->cur_pcr += ts->pcr_incr;
2107 pkt->stream_index = 0;
2111 static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
2113 MpegTSContext *ts = s->priv_data;
2118 ret = handle_packets(ts, 0);
2120 /* flush pes data left */
2121 for (i = 0; i < NB_PID_MAX; i++)
2122 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
2123 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
2124 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
2125 new_pes_packet(pes, pkt);
2126 pes->state = MPEGTS_SKIP;
2133 if (!ret && pkt->size < 0)
2134 ret = AVERROR(EINTR);
2138 static void mpegts_free(MpegTSContext *ts)
2144 for (i = 0; i < NB_PID_MAX; i++)
2146 mpegts_close_filter(ts, ts->pids[i]);
2149 static int mpegts_read_close(AVFormatContext *s)
2151 MpegTSContext *ts = s->priv_data;
2156 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
2157 int64_t *ppos, int64_t pos_limit)
2159 MpegTSContext *ts = s->priv_data;
2160 int64_t pos, timestamp;
2161 uint8_t buf[TS_PACKET_SIZE];
2162 int pcr_l, pcr_pid =
2163 ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
2164 const int find_next = 1;
2166 ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) *
2167 ts->raw_packet_size + ts->pos47;
2170 avio_seek(s->pb, pos, SEEK_SET);
2171 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2172 return AV_NOPTS_VALUE;
2173 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2174 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2177 pos += ts->raw_packet_size;
2181 pos -= ts->raw_packet_size;
2183 return AV_NOPTS_VALUE;
2184 avio_seek(s->pb, pos, SEEK_SET);
2185 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2186 return AV_NOPTS_VALUE;
2187 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2188 parse_pcr(×tamp, &pcr_l, buf) == 0) {
2198 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
2200 MpegTSContext *ts = s->priv_data;
2201 uint8_t buf[TS_PACKET_SIZE];
2205 ret = ff_seek_frame_binary(s, stream_index, target_ts, flags);
2209 pos = avio_tell(s->pb);
2212 avio_seek(s->pb, pos, SEEK_SET);
2213 ret = avio_read(s->pb, buf, TS_PACKET_SIZE);
2216 if (ret != TS_PACKET_SIZE)
2218 // pid = AV_RB16(buf + 1) & 0x1fff;
2221 pos += ts->raw_packet_size;
2223 avio_seek(s->pb, pos, SEEK_SET);
2228 /**************************************************************/
2229 /* parsing functions - called from other demuxers such as RTP */
2231 MpegTSContext *ff_mpegts_parse_open(AVFormatContext *s)
2235 ts = av_mallocz(sizeof(MpegTSContext));
2238 /* no stream case, currently used by RTP */
2239 ts->raw_packet_size = TS_PACKET_SIZE;
2245 /* return the consumed length if a packet was output, or -1 if no
2246 * packet is output */
2247 int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
2248 const uint8_t *buf, int len)
2256 if (ts->stop_parse > 0)
2258 if (len < TS_PACKET_SIZE)
2259 return AVERROR_INVALIDDATA;
2260 if (buf[0] != 0x47) {
2264 handle_packet(ts, buf);
2265 buf += TS_PACKET_SIZE;
2266 len -= TS_PACKET_SIZE;
2272 void ff_mpegts_parse_close(MpegTSContext *ts)
2278 AVInputFormat ff_mpegts_demuxer = {
2280 .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2281 .priv_data_size = sizeof(MpegTSContext),
2282 .read_probe = mpegts_probe,
2283 .read_header = mpegts_read_header,
2284 .read_packet = mpegts_read_packet,
2285 .read_close = mpegts_read_close,
2286 .read_seek = read_seek,
2287 .read_timestamp = mpegts_get_pcr,
2288 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
2291 AVInputFormat ff_mpegtsraw_demuxer = {
2292 .name = "mpegtsraw",
2293 .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
2294 .priv_data_size = sizeof(MpegTSContext),
2295 .read_header = mpegts_read_header,
2296 .read_packet = mpegts_raw_read_packet,
2297 .read_close = mpegts_read_close,
2298 .read_seek = read_seek,
2299 .read_timestamp = mpegts_get_pcr,
2300 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
2301 .priv_class = &mpegtsraw_class,