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"
31 #include "libavcodec/opus.h"
35 #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
{
78 unsigned int check_crc
: 1;
79 unsigned int end_of_section_reached
: 1;
80 SectionCallback
*section_cb
;
82 } MpegTSSectionFilter
;
87 int last_cc
; /* last cc code (-1 if first packet) */
88 enum MpegTSFilterType type
;
90 MpegTSPESFilter pes_filter
;
91 MpegTSSectionFilter section_filter
;
95 #define MAX_PIDS_PER_PROGRAM 64
97 unsigned int id
; // program id/service id
99 unsigned int pids
[MAX_PIDS_PER_PROGRAM
];
102 struct MpegTSContext
{
103 const AVClass
*class;
105 AVFormatContext
*stream
;
106 /** raw packet size, including FEC if present */
110 /** position corresponding to pos47, or 0 if pos47 invalid */
113 /** if true, all pids are analyzed to find streams */
116 /** compute exact PCR for each transport stream packet */
117 int mpeg2ts_compute_pcr
;
119 int64_t cur_pcr
; /**< used to estimate the exact PCR */
120 int pcr_incr
; /**< used to estimate the exact PCR */
122 /* data needed to handle file based ts */
123 /** stop parsing loop */
125 /** packet containing Audio/Video data */
127 /** to detect seek */
132 /******************************************/
133 /* private mpegts data */
135 /** structure to keep track of Program->pids mapping */
139 /** filters for various streams specified by PMT + for the PAT and PMT */
140 MpegTSFilter
*pids
[NB_PID_MAX
];
143 #define MPEGTS_OPTIONS \
144 { "resync_size", "Size limit for looking up a new syncronization.", offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }
146 static const AVOption options
[] = {
151 static const AVClass mpegts_class
= {
152 .class_name
= "mpegts demuxer",
153 .item_name
= av_default_item_name
,
155 .version
= LIBAVUTIL_VERSION_INT
,
158 static const AVOption raw_options
[] = {
160 { "compute_pcr", "Compute exact PCR for each transport stream packet.",
161 offsetof(MpegTSContext
, mpeg2ts_compute_pcr
), AV_OPT_TYPE_INT
,
162 { .i64
= 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM
},
163 { "ts_packetsize", "Output option carrying the raw packet size.",
164 offsetof(MpegTSContext
, raw_packet_size
), AV_OPT_TYPE_INT
,
166 AV_OPT_FLAG_DECODING_PARAM
| AV_OPT_FLAG_EXPORT
| AV_OPT_FLAG_READONLY
},
170 static const AVClass mpegtsraw_class
= {
171 .class_name
= "mpegtsraw demuxer",
172 .item_name
= av_default_item_name
,
173 .option
= raw_options
,
174 .version
= LIBAVUTIL_VERSION_INT
,
177 /* TS stream handling */
182 MPEGTS_PESHEADER_FILL
,
187 /* enough for PES header + length */
188 #define PES_START_SIZE 6
189 #define PES_HEADER_SIZE 9
190 #define MAX_PES_HEADER_SIZE (9 + 255)
192 typedef struct PESContext
{
194 int pcr_pid
; /**< if -1 then all packets containing PCR are considered */
197 AVFormatContext
*stream
;
199 AVStream
*sub_st
; /**< stream for the embedded AC3 stream in HDMV TrueHD */
200 enum MpegTSState state
;
201 /* used to get the format */
203 int flags
; /**< copied to the AVPacket flags */
206 int extended_stream_id
;
208 int64_t ts_packet_pos
; /**< position of first TS packet of this PES packet */
209 uint8_t header
[MAX_PES_HEADER_SIZE
];
214 extern AVInputFormat ff_mpegts_demuxer
;
216 static void clear_program(MpegTSContext
*ts
, unsigned int programid
)
220 for (i
= 0; i
< ts
->nb_prg
; i
++)
221 if (ts
->prg
[i
].id
== programid
)
222 ts
->prg
[i
].nb_pids
= 0;
225 static void clear_programs(MpegTSContext
*ts
)
231 static void add_pat_entry(MpegTSContext
*ts
, unsigned int programid
)
234 if (av_reallocp_array(&ts
->prg
, ts
->nb_prg
+ 1, sizeof(*ts
->prg
)) < 0) {
238 p
= &ts
->prg
[ts
->nb_prg
];
244 static void add_pid_to_pmt(MpegTSContext
*ts
, unsigned int programid
,
248 struct Program
*p
= NULL
;
249 for (i
= 0; i
< ts
->nb_prg
; i
++) {
250 if (ts
->prg
[i
].id
== programid
) {
258 if (p
->nb_pids
>= MAX_PIDS_PER_PROGRAM
)
260 p
->pids
[p
->nb_pids
++] = pid
;
264 * @brief discard_pid() decides if the pid is to be discarded according
265 * to caller's programs selection
266 * @param ts : - TS context
268 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
271 static int discard_pid(MpegTSContext
*ts
, unsigned int pid
)
274 int used
= 0, discarded
= 0;
277 /* If none of the programs have .discard=AVDISCARD_ALL then there's
278 * no way we have to discard this packet */
279 for (k
= 0; k
< ts
->stream
->nb_programs
; k
++)
280 if (ts
->stream
->programs
[k
]->discard
== AVDISCARD_ALL
)
282 if (k
== ts
->stream
->nb_programs
)
285 for (i
= 0; i
< ts
->nb_prg
; i
++) {
287 for (j
= 0; j
< p
->nb_pids
; j
++) {
288 if (p
->pids
[j
] != pid
)
290 // is program with id p->id set to be discarded?
291 for (k
= 0; k
< ts
->stream
->nb_programs
; k
++) {
292 if (ts
->stream
->programs
[k
]->id
== p
->id
) {
293 if (ts
->stream
->programs
[k
]->discard
== AVDISCARD_ALL
)
302 return !used
&& discarded
;
306 * Assemble PES packets out of TS packets, and then call the "section_cb"
307 * function when they are complete.
309 static void write_section_data(MpegTSContext
*ts
, MpegTSFilter
*tss1
,
310 const uint8_t *buf
, int buf_size
, int is_start
)
312 MpegTSSectionFilter
*tss
= &tss1
->u
.section_filter
;
316 memcpy(tss
->section_buf
, buf
, buf_size
);
317 tss
->section_index
= buf_size
;
318 tss
->section_h_size
= -1;
319 tss
->end_of_section_reached
= 0;
321 if (tss
->end_of_section_reached
)
323 len
= 4096 - tss
->section_index
;
326 memcpy(tss
->section_buf
+ tss
->section_index
, buf
, len
);
327 tss
->section_index
+= len
;
330 /* compute section length if possible */
331 if (tss
->section_h_size
== -1 && tss
->section_index
>= 3) {
332 len
= (AV_RB16(tss
->section_buf
+ 1) & 0xfff) + 3;
335 tss
->section_h_size
= len
;
338 if (tss
->section_h_size
!= -1 &&
339 tss
->section_index
>= tss
->section_h_size
) {
340 tss
->end_of_section_reached
= 1;
341 if (!tss
->check_crc
||
342 av_crc(av_crc_get_table(AV_CRC_32_IEEE
), -1,
343 tss
->section_buf
, tss
->section_h_size
) == 0)
344 tss
->section_cb(tss1
, tss
->section_buf
, tss
->section_h_size
);
348 static MpegTSFilter
*mpegts_open_section_filter(MpegTSContext
*ts
,
350 SectionCallback
*section_cb
,
354 MpegTSFilter
*filter
;
355 MpegTSSectionFilter
*sec
;
357 av_log(ts
->stream
, AV_LOG_TRACE
, "Filter: pid=0x%x\n", pid
);
359 if (pid
>= NB_PID_MAX
|| ts
->pids
[pid
])
361 filter
= av_mallocz(sizeof(MpegTSFilter
));
364 ts
->pids
[pid
] = filter
;
366 filter
->type
= MPEGTS_SECTION
;
369 filter
->last_cc
= -1;
371 sec
= &filter
->u
.section_filter
;
372 sec
->section_cb
= section_cb
;
373 sec
->opaque
= opaque
;
374 sec
->section_buf
= av_malloc(MAX_SECTION_SIZE
);
375 sec
->check_crc
= check_crc
;
378 if (!sec
->section_buf
) {
385 static MpegTSFilter
*mpegts_open_pes_filter(MpegTSContext
*ts
, unsigned int pid
,
389 MpegTSFilter
*filter
;
390 MpegTSPESFilter
*pes
;
392 if (pid
>= NB_PID_MAX
|| ts
->pids
[pid
])
394 filter
= av_mallocz(sizeof(MpegTSFilter
));
398 ts
->pids
[pid
] = filter
;
399 filter
->type
= MPEGTS_PES
;
402 filter
->last_cc
= -1;
404 pes
= &filter
->u
.pes_filter
;
405 pes
->pes_cb
= pes_cb
;
406 pes
->opaque
= opaque
;
410 static void mpegts_close_filter(MpegTSContext
*ts
, MpegTSFilter
*filter
)
415 if (filter
->type
== MPEGTS_SECTION
)
416 av_freep(&filter
->u
.section_filter
.section_buf
);
417 else if (filter
->type
== MPEGTS_PES
) {
418 PESContext
*pes
= filter
->u
.pes_filter
.opaque
;
419 av_buffer_unref(&pes
->buffer
);
420 /* referenced private data will be freed later in
421 * avformat_close_input */
422 if (!((PESContext
*)filter
->u
.pes_filter
.opaque
)->st
) {
423 av_freep(&filter
->u
.pes_filter
.opaque
);
428 ts
->pids
[pid
] = NULL
;
431 static int analyze(const uint8_t *buf
, int size
, int packet_size
, int *index
,
434 int stat
[TS_MAX_PACKET_SIZE
];
439 memset(stat
, 0, packet_size
* sizeof(int));
441 for (x
= i
= 0; i
< size
- 3; i
++) {
442 if (buf
[i
] == 0x47 &&
443 (!probe
|| (!(buf
[i
+ 1] & 0x80) && (buf
[i
+ 3] & 0x30)))) {
445 if (stat
[x
] > best_score
) {
446 best_score
= stat
[x
];
453 if (x
== packet_size
)
460 /* autodetect fec presence. Must have at least 1024 bytes */
461 static int get_packet_size(const uint8_t *buf
, int size
)
463 int score
, fec_score
, dvhs_score
;
465 if (size
< (TS_FEC_PACKET_SIZE
* 5 + 1))
466 return AVERROR_INVALIDDATA
;
468 score
= analyze(buf
, size
, TS_PACKET_SIZE
, NULL
, 0);
469 dvhs_score
= analyze(buf
, size
, TS_DVHS_PACKET_SIZE
, NULL
, 0);
470 fec_score
= analyze(buf
, size
, TS_FEC_PACKET_SIZE
, NULL
, 0);
471 av_log(NULL
, AV_LOG_TRACE
, "score: %d, dvhs_score: %d, fec_score: %d \n",
472 score
, dvhs_score
, fec_score
);
474 if (score
> fec_score
&& score
> dvhs_score
)
475 return TS_PACKET_SIZE
;
476 else if (dvhs_score
> score
&& dvhs_score
> fec_score
)
477 return TS_DVHS_PACKET_SIZE
;
478 else if (score
< fec_score
&& dvhs_score
< fec_score
)
479 return TS_FEC_PACKET_SIZE
;
481 return AVERROR_INVALIDDATA
;
484 typedef struct SectionHeader
{
489 uint8_t last_sec_num
;
492 static inline int get8(const uint8_t **pp
, const uint8_t *p_end
)
499 return AVERROR_INVALIDDATA
;
505 static inline int get16(const uint8_t **pp
, const uint8_t *p_end
)
511 if ((p
+ 1) >= p_end
)
512 return AVERROR_INVALIDDATA
;
519 /* read and allocate a DVB string preceded by its length */
520 static char *getstr8(const uint8_t **pp
, const uint8_t *p_end
)
527 len
= get8(&p
, p_end
);
530 if ((p
+ len
) > p_end
)
532 str
= av_malloc(len
+ 1);
542 static int parse_section_header(SectionHeader
*h
,
543 const uint8_t **pp
, const uint8_t *p_end
)
547 val
= get8(pp
, p_end
);
552 val
= get16(pp
, p_end
);
556 val
= get8(pp
, p_end
);
559 h
->version
= (val
>> 1) & 0x1f;
560 val
= get8(pp
, p_end
);
564 val
= get8(pp
, p_end
);
567 h
->last_sec_num
= val
;
571 typedef struct StreamType
{
572 uint32_t stream_type
;
573 enum AVMediaType codec_type
;
574 enum AVCodecID codec_id
;
577 static const StreamType ISO_types
[] = {
578 { 0x01, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_MPEG2VIDEO
},
579 { 0x02, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_MPEG2VIDEO
},
580 { 0x03, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_MP3
},
581 { 0x04, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_MP3
},
582 { 0x0f, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AAC
},
583 { 0x10, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_MPEG4
},
584 { 0x11, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AAC_LATM
}, /* LATM syntax */
585 { 0x1b, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_H264
},
586 { 0x21, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_JPEG2000
},
587 { 0x24, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_HEVC
},
588 { 0x42, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_CAVS
},
589 { 0xd1, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_DIRAC
},
590 { 0xea, AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_VC1
},
594 static const StreamType HDMV_types
[] = {
595 { 0x80, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_PCM_BLURAY
},
596 { 0x81, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AC3
},
597 { 0x82, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
598 { 0x83, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_TRUEHD
},
599 { 0x84, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_EAC3
},
600 { 0x85, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
}, /* DTS HD */
601 { 0x86, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
}, /* DTS HD MASTER*/
602 { 0x90, AVMEDIA_TYPE_SUBTITLE
, AV_CODEC_ID_HDMV_PGS_SUBTITLE
},
607 static const StreamType MISC_types
[] = {
608 { 0x81, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AC3
},
609 { 0x8a, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
613 static const StreamType REGD_types
[] = {
614 { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_DIRAC
},
615 { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AC3
},
616 { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_S302M
},
617 { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
618 { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
619 { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
620 { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_HEVC
},
621 { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO
, AV_CODEC_ID_VC1
},
622 { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_OPUS
},
626 /* descriptor present */
627 static const StreamType DESC_types
[] = {
628 { 0x6a, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_AC3
}, /* AC-3 descriptor */
629 { 0x7a, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_EAC3
}, /* E-AC-3 descriptor */
630 { 0x7b, AVMEDIA_TYPE_AUDIO
, AV_CODEC_ID_DTS
},
631 { 0x56, AVMEDIA_TYPE_SUBTITLE
, AV_CODEC_ID_DVB_TELETEXT
},
632 { 0x59, AVMEDIA_TYPE_SUBTITLE
, AV_CODEC_ID_DVB_SUBTITLE
}, /* subtitling descriptor */
636 static void mpegts_find_stream_type(AVStream
*st
,
637 uint32_t stream_type
,
638 const StreamType
*types
)
640 for (; types
->stream_type
; types
++)
641 if (stream_type
== types
->stream_type
) {
642 st
->codec
->codec_type
= types
->codec_type
;
643 st
->codec
->codec_id
= types
->codec_id
;
648 static int mpegts_set_stream_info(AVStream
*st
, PESContext
*pes
,
649 uint32_t stream_type
, uint32_t prog_reg_desc
)
651 avpriv_set_pts_info(st
, 33, 1, 90000);
653 st
->codec
->codec_type
= AVMEDIA_TYPE_DATA
;
654 st
->codec
->codec_id
= AV_CODEC_ID_NONE
;
655 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
657 pes
->stream_type
= stream_type
;
659 av_log(pes
->stream
, AV_LOG_DEBUG
,
660 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
661 st
->index
, pes
->stream_type
, pes
->pid
, (char *)&prog_reg_desc
);
663 st
->codec
->codec_tag
= pes
->stream_type
;
665 mpegts_find_stream_type(st
, pes
->stream_type
, ISO_types
);
666 if (prog_reg_desc
== AV_RL32("HDMV") &&
667 st
->codec
->codec_id
== AV_CODEC_ID_NONE
) {
668 mpegts_find_stream_type(st
, pes
->stream_type
, HDMV_types
);
669 if (pes
->stream_type
== 0x83) {
670 // HDMV TrueHD streams also contain an AC3 coded version of the
671 // audio track - add a second stream for this
673 // priv_data cannot be shared between streams
674 PESContext
*sub_pes
= av_malloc(sizeof(*sub_pes
));
676 return AVERROR(ENOMEM
);
677 memcpy(sub_pes
, pes
, sizeof(*sub_pes
));
679 sub_st
= avformat_new_stream(pes
->stream
, NULL
);
682 return AVERROR(ENOMEM
);
685 sub_st
->id
= pes
->pid
;
686 avpriv_set_pts_info(sub_st
, 33, 1, 90000);
687 sub_st
->priv_data
= sub_pes
;
688 sub_st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
689 sub_st
->codec
->codec_id
= AV_CODEC_ID_AC3
;
690 sub_st
->need_parsing
= AVSTREAM_PARSE_FULL
;
691 sub_pes
->sub_st
= pes
->sub_st
= sub_st
;
694 if (st
->codec
->codec_id
== AV_CODEC_ID_NONE
)
695 mpegts_find_stream_type(st
, pes
->stream_type
, MISC_types
);
700 static void new_pes_packet(PESContext
*pes
, AVPacket
*pkt
)
704 pkt
->buf
= pes
->buffer
;
705 pkt
->data
= pes
->buffer
->data
;
706 pkt
->size
= pes
->data_index
;
708 if (pes
->total_size
!= MAX_PES_PAYLOAD
&&
709 pes
->pes_header_size
+ pes
->data_index
!= pes
->total_size
+
711 av_log(pes
->stream
, AV_LOG_WARNING
, "PES packet size mismatch\n");
712 pes
->flags
|= AV_PKT_FLAG_CORRUPT
;
714 memset(pkt
->data
+ pkt
->size
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
716 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
717 if (pes
->sub_st
&& pes
->stream_type
== 0x83 && pes
->extended_stream_id
== 0x76)
718 pkt
->stream_index
= pes
->sub_st
->index
;
720 pkt
->stream_index
= pes
->st
->index
;
723 /* store position of first TS packet of this PES packet */
724 pkt
->pos
= pes
->ts_packet_pos
;
725 pkt
->flags
= pes
->flags
;
727 /* reset pts values */
728 pes
->pts
= AV_NOPTS_VALUE
;
729 pes
->dts
= AV_NOPTS_VALUE
;
735 static int read_sl_header(PESContext
*pes
, SLConfigDescr
*sl
,
736 const uint8_t *buf
, int buf_size
)
739 int au_start_flag
= 0, au_end_flag
= 0, ocr_flag
= 0, idle_flag
= 0;
740 int padding_flag
= 0, padding_bits
= 0, inst_bitrate_flag
= 0;
741 int dts_flag
= -1, cts_flag
= -1;
742 int64_t dts
= AV_NOPTS_VALUE
, cts
= AV_NOPTS_VALUE
;
743 init_get_bits(&gb
, buf
, buf_size
* 8);
745 if (sl
->use_au_start
)
746 au_start_flag
= get_bits1(&gb
);
748 au_end_flag
= get_bits1(&gb
);
749 if (!sl
->use_au_start
&& !sl
->use_au_end
)
750 au_start_flag
= au_end_flag
= 1;
752 ocr_flag
= get_bits1(&gb
);
754 idle_flag
= get_bits1(&gb
);
756 padding_flag
= get_bits1(&gb
);
758 padding_bits
= get_bits(&gb
, 3);
760 if (!idle_flag
&& (!padding_flag
|| padding_bits
!= 0)) {
761 if (sl
->packet_seq_num_len
)
762 skip_bits_long(&gb
, sl
->packet_seq_num_len
);
763 if (sl
->degr_prior_len
)
765 skip_bits(&gb
, sl
->degr_prior_len
);
767 skip_bits_long(&gb
, sl
->ocr_len
);
769 if (sl
->use_rand_acc_pt
)
771 if (sl
->au_seq_num_len
> 0)
772 skip_bits_long(&gb
, sl
->au_seq_num_len
);
773 if (sl
->use_timestamps
) {
774 dts_flag
= get_bits1(&gb
);
775 cts_flag
= get_bits1(&gb
);
778 if (sl
->inst_bitrate_len
)
779 inst_bitrate_flag
= get_bits1(&gb
);
781 dts
= get_bits64(&gb
, sl
->timestamp_len
);
783 cts
= get_bits64(&gb
, sl
->timestamp_len
);
785 skip_bits_long(&gb
, sl
->au_len
);
786 if (inst_bitrate_flag
)
787 skip_bits_long(&gb
, sl
->inst_bitrate_len
);
790 if (dts
!= AV_NOPTS_VALUE
)
792 if (cts
!= AV_NOPTS_VALUE
)
795 if (sl
->timestamp_len
&& sl
->timestamp_res
)
796 avpriv_set_pts_info(pes
->st
, sl
->timestamp_len
, 1, sl
->timestamp_res
);
798 return (get_bits_count(&gb
) + 7) >> 3;
801 /* return non zero if a packet could be constructed */
802 static int mpegts_push_data(MpegTSFilter
*filter
,
803 const uint8_t *buf
, int buf_size
, int is_start
,
806 PESContext
*pes
= filter
->u
.pes_filter
.opaque
;
807 MpegTSContext
*ts
= pes
->ts
;
815 if (pes
->state
== MPEGTS_PAYLOAD
&& pes
->data_index
> 0) {
816 new_pes_packet(pes
, ts
->pkt
);
819 pes
->state
= MPEGTS_HEADER
;
821 pes
->ts_packet_pos
= pos
;
824 while (buf_size
> 0) {
825 switch (pes
->state
) {
827 len
= PES_START_SIZE
- pes
->data_index
;
830 memcpy(pes
->header
+ pes
->data_index
, p
, len
);
831 pes
->data_index
+= len
;
834 if (pes
->data_index
== PES_START_SIZE
) {
835 /* we got all the PES or section header. We can now
837 if (pes
->header
[0] == 0x00 && pes
->header
[1] == 0x00 &&
838 pes
->header
[2] == 0x01) {
839 /* it must be an mpeg2 PES stream */
840 code
= pes
->header
[3] | 0x100;
841 av_log(pes
->stream
, AV_LOG_TRACE
, "pid=%x pes_code=%#x\n", pes
->pid
,
844 if ((pes
->st
&& pes
->st
->discard
== AVDISCARD_ALL
&&
846 pes
->sub_st
->discard
== AVDISCARD_ALL
)) ||
847 code
== 0x1be) /* padding_stream */
850 /* stream not present in PMT */
852 pes
->st
= avformat_new_stream(ts
->stream
, NULL
);
854 return AVERROR(ENOMEM
);
855 pes
->st
->id
= pes
->pid
;
856 mpegts_set_stream_info(pes
->st
, pes
, 0, 0);
859 pes
->total_size
= AV_RB16(pes
->header
+ 4);
860 /* NOTE: a zero total size means the PES size is
862 if (!pes
->total_size
)
863 pes
->total_size
= MAX_PES_PAYLOAD
;
865 /* allocate pes buffer */
866 pes
->buffer
= av_buffer_alloc(pes
->total_size
+
867 FF_INPUT_BUFFER_PADDING_SIZE
);
869 return AVERROR(ENOMEM
);
871 if (code
!= 0x1bc && code
!= 0x1bf && /* program_stream_map, private_stream_2 */
872 code
!= 0x1f0 && code
!= 0x1f1 && /* ECM, EMM */
873 code
!= 0x1ff && code
!= 0x1f2 && /* program_stream_directory, DSMCC_stream */
874 code
!= 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
875 pes
->state
= MPEGTS_PESHEADER
;
876 if (pes
->st
->codec
->codec_id
== AV_CODEC_ID_NONE
) {
877 av_log(pes
->stream
, AV_LOG_TRACE
,
878 "pid=%x stream_type=%x probing\n",
881 pes
->st
->codec
->codec_id
= AV_CODEC_ID_PROBE
;
884 pes
->state
= MPEGTS_PAYLOAD
;
888 /* otherwise, it should be a table */
891 pes
->state
= MPEGTS_SKIP
;
896 /**********************************************/
897 /* PES packing parsing */
898 case MPEGTS_PESHEADER
:
899 len
= PES_HEADER_SIZE
- pes
->data_index
;
901 return AVERROR_INVALIDDATA
;
904 memcpy(pes
->header
+ pes
->data_index
, p
, len
);
905 pes
->data_index
+= len
;
908 if (pes
->data_index
== PES_HEADER_SIZE
) {
909 pes
->pes_header_size
= pes
->header
[8] + 9;
910 pes
->state
= MPEGTS_PESHEADER_FILL
;
913 case MPEGTS_PESHEADER_FILL
:
914 len
= pes
->pes_header_size
- pes
->data_index
;
916 return AVERROR_INVALIDDATA
;
919 memcpy(pes
->header
+ pes
->data_index
, p
, len
);
920 pes
->data_index
+= len
;
923 if (pes
->data_index
== pes
->pes_header_size
) {
925 unsigned int flags
, pes_ext
, skip
;
927 flags
= pes
->header
[7];
929 pes
->pts
= AV_NOPTS_VALUE
;
930 pes
->dts
= AV_NOPTS_VALUE
;
931 if ((flags
& 0xc0) == 0x80) {
932 pes
->dts
= pes
->pts
= ff_parse_pes_pts(r
);
934 } else if ((flags
& 0xc0) == 0xc0) {
935 pes
->pts
= ff_parse_pes_pts(r
);
937 pes
->dts
= ff_parse_pes_pts(r
);
940 pes
->extended_stream_id
= -1;
941 if (flags
& 0x01) { /* PES extension */
943 /* Skip PES private data, program packet sequence counter and P-STD buffer */
944 skip
= (pes_ext
>> 4) & 0xb;
947 if ((pes_ext
& 0x41) == 0x01 &&
948 (r
+ 2) <= (pes
->header
+ pes
->pes_header_size
)) {
949 /* PES extension 2 */
950 if ((r
[0] & 0x7f) > 0 && (r
[1] & 0x80) == 0)
951 pes
->extended_stream_id
= r
[1];
955 /* we got the full header. We parse it and get the payload */
956 pes
->state
= MPEGTS_PAYLOAD
;
958 if (pes
->stream_type
== 0x12 && buf_size
> 0) {
959 int sl_header_bytes
= read_sl_header(pes
, &pes
->sl
, p
,
961 pes
->pes_header_size
+= sl_header_bytes
;
962 p
+= sl_header_bytes
;
963 buf_size
-= sl_header_bytes
;
968 if (buf_size
> 0 && pes
->buffer
) {
969 if (pes
->data_index
> 0 &&
970 pes
->data_index
+ buf_size
> pes
->total_size
) {
971 new_pes_packet(pes
, ts
->pkt
);
972 pes
->total_size
= MAX_PES_PAYLOAD
;
973 pes
->buffer
= av_buffer_alloc(pes
->total_size
+
974 FF_INPUT_BUFFER_PADDING_SIZE
);
976 return AVERROR(ENOMEM
);
978 } else if (pes
->data_index
== 0 &&
979 buf_size
> pes
->total_size
) {
980 // pes packet size is < ts size packet and pes data is padded with 0xff
981 // not sure if this is legal in ts but see issue #2392
982 buf_size
= pes
->total_size
;
984 memcpy(pes
->buffer
->data
+ pes
->data_index
, p
, buf_size
);
985 pes
->data_index
+= buf_size
;
988 /* emit complete packets with known packet size
989 * decreases demuxer delay for infrequent packets like subtitles from
990 * a couple of seconds to milliseconds for properly muxed files.
991 * total_size is the number of bytes following pes_packet_length
992 * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
993 if (!ts
->stop_parse
&& pes
->total_size
< MAX_PES_PAYLOAD
&&
994 pes
->pes_header_size
+ pes
->data_index
== pes
->total_size
+ PES_START_SIZE
) {
996 new_pes_packet(pes
, ts
->pkt
);
1008 static PESContext
*add_pes_stream(MpegTSContext
*ts
, int pid
, int pcr_pid
)
1013 /* if no pid found, then add a pid context */
1014 pes
= av_mallocz(sizeof(PESContext
));
1018 pes
->stream
= ts
->stream
;
1020 pes
->pcr_pid
= pcr_pid
;
1021 pes
->state
= MPEGTS_SKIP
;
1022 pes
->pts
= AV_NOPTS_VALUE
;
1023 pes
->dts
= AV_NOPTS_VALUE
;
1024 tss
= mpegts_open_pes_filter(ts
, pid
, mpegts_push_data
, pes
);
1033 typedef struct MP4DescrParseContext
{
1037 Mp4Descr
*active_descr
;
1039 int max_descr_count
;
1041 } MP4DescrParseContext
;
1043 static int init_MP4DescrParseContext(MP4DescrParseContext
*d
, AVFormatContext
*s
,
1044 const uint8_t *buf
, unsigned size
,
1045 Mp4Descr
*descr
, int max_descr_count
)
1048 if (size
> (1 << 30))
1049 return AVERROR_INVALIDDATA
;
1051 if ((ret
= ffio_init_context(&d
->pb
, (unsigned char *)buf
, size
, 0,
1052 NULL
, NULL
, NULL
, NULL
)) < 0)
1059 d
->active_descr
= NULL
;
1060 d
->max_descr_count
= max_descr_count
;
1065 static void update_offsets(AVIOContext
*pb
, int64_t *off
, int *len
)
1067 int64_t new_off
= avio_tell(pb
);
1068 (*len
) -= new_off
- *off
;
1072 static int parse_mp4_descr(MP4DescrParseContext
*d
, int64_t off
, int len
,
1075 static int parse_mp4_descr_arr(MP4DescrParseContext
*d
, int64_t off
, int len
)
1078 int ret
= parse_mp4_descr(d
, off
, len
, 0);
1081 update_offsets(&d
->pb
, &off
, &len
);
1086 static int parse_MP4IODescrTag(MP4DescrParseContext
*d
, int64_t off
, int len
)
1088 avio_rb16(&d
->pb
); // ID
1094 update_offsets(&d
->pb
, &off
, &len
);
1095 return parse_mp4_descr_arr(d
, off
, len
);
1098 static int parse_MP4ODescrTag(MP4DescrParseContext
*d
, int64_t off
, int len
)
1103 id_flags
= avio_rb16(&d
->pb
);
1104 if (!(id_flags
& 0x0020)) { // URL_Flag
1105 update_offsets(&d
->pb
, &off
, &len
);
1106 return parse_mp4_descr_arr(d
, off
, len
); // ES_Descriptor[]
1112 static int parse_MP4ESDescrTag(MP4DescrParseContext
*d
, int64_t off
, int len
)
1115 if (d
->descr_count
>= d
->max_descr_count
)
1116 return AVERROR_INVALIDDATA
;
1117 ff_mp4_parse_es_descr(&d
->pb
, &es_id
);
1118 d
->active_descr
= d
->descr
+ (d
->descr_count
++);
1120 d
->active_descr
->es_id
= es_id
;
1121 update_offsets(&d
->pb
, &off
, &len
);
1122 parse_mp4_descr(d
, off
, len
, MP4DecConfigDescrTag
);
1123 update_offsets(&d
->pb
, &off
, &len
);
1125 parse_mp4_descr(d
, off
, len
, MP4SLDescrTag
);
1126 d
->active_descr
= NULL
;
1130 static int parse_MP4DecConfigDescrTag(MP4DescrParseContext
*d
, int64_t off
,
1133 Mp4Descr
*descr
= d
->active_descr
;
1135 return AVERROR_INVALIDDATA
;
1136 d
->active_descr
->dec_config_descr
= av_malloc(len
);
1137 if (!descr
->dec_config_descr
)
1138 return AVERROR(ENOMEM
);
1139 descr
->dec_config_descr_len
= len
;
1140 avio_read(&d
->pb
, descr
->dec_config_descr
, len
);
1144 static int parse_MP4SLDescrTag(MP4DescrParseContext
*d
, int64_t off
, int len
)
1146 Mp4Descr
*descr
= d
->active_descr
;
1149 return AVERROR_INVALIDDATA
;
1151 predefined
= avio_r8(&d
->pb
);
1154 int flags
= avio_r8(&d
->pb
);
1155 descr
->sl
.use_au_start
= !!(flags
& 0x80);
1156 descr
->sl
.use_au_end
= !!(flags
& 0x40);
1157 descr
->sl
.use_rand_acc_pt
= !!(flags
& 0x20);
1158 descr
->sl
.use_padding
= !!(flags
& 0x08);
1159 descr
->sl
.use_timestamps
= !!(flags
& 0x04);
1160 descr
->sl
.use_idle
= !!(flags
& 0x02);
1161 descr
->sl
.timestamp_res
= avio_rb32(&d
->pb
);
1163 descr
->sl
.timestamp_len
= avio_r8(&d
->pb
);
1164 descr
->sl
.ocr_len
= avio_r8(&d
->pb
);
1165 descr
->sl
.au_len
= avio_r8(&d
->pb
);
1166 descr
->sl
.inst_bitrate_len
= avio_r8(&d
->pb
);
1167 lengths
= avio_rb16(&d
->pb
);
1168 descr
->sl
.degr_prior_len
= lengths
>> 12;
1169 descr
->sl
.au_seq_num_len
= (lengths
>> 7) & 0x1f;
1170 descr
->sl
.packet_seq_num_len
= (lengths
>> 2) & 0x1f;
1172 avpriv_report_missing_feature(d
->s
, "Predefined SLConfigDescriptor");
1177 static int parse_mp4_descr(MP4DescrParseContext
*d
, int64_t off
, int len
,
1181 int len1
= ff_mp4_read_descr(d
->s
, &d
->pb
, &tag
);
1182 update_offsets(&d
->pb
, &off
, &len
);
1183 if (len
< 0 || len1
> len
|| len1
<= 0) {
1184 av_log(d
->s
, AV_LOG_ERROR
,
1185 "Tag %x length violation new length %d bytes remaining %d\n",
1187 return AVERROR_INVALIDDATA
;
1190 if (d
->level
++ >= MAX_LEVEL
) {
1191 av_log(d
->s
, AV_LOG_ERROR
, "Maximum MP4 descriptor level exceeded\n");
1195 if (target_tag
&& tag
!= target_tag
) {
1196 av_log(d
->s
, AV_LOG_ERROR
, "Found tag %x expected %x\n", tag
,
1203 parse_MP4IODescrTag(d
, off
, len1
);
1206 parse_MP4ODescrTag(d
, off
, len1
);
1209 parse_MP4ESDescrTag(d
, off
, len1
);
1211 case MP4DecConfigDescrTag
:
1212 parse_MP4DecConfigDescrTag(d
, off
, len1
);
1215 parse_MP4SLDescrTag(d
, off
, len1
);
1222 avio_seek(&d
->pb
, off
+ len1
, SEEK_SET
);
1226 static int mp4_read_iods(AVFormatContext
*s
, const uint8_t *buf
, unsigned size
,
1227 Mp4Descr
*descr
, int *descr_count
, int max_descr_count
)
1229 MP4DescrParseContext d
;
1232 ret
= init_MP4DescrParseContext(&d
, s
, buf
, size
, descr
, max_descr_count
);
1236 ret
= parse_mp4_descr(&d
, avio_tell(&d
.pb
), size
, MP4IODescrTag
);
1238 *descr_count
= d
.descr_count
;
1242 static int mp4_read_od(AVFormatContext
*s
, const uint8_t *buf
, unsigned size
,
1243 Mp4Descr
*descr
, int *descr_count
, int max_descr_count
)
1245 MP4DescrParseContext d
;
1248 ret
= init_MP4DescrParseContext(&d
, s
, buf
, size
, descr
, max_descr_count
);
1252 ret
= parse_mp4_descr_arr(&d
, avio_tell(&d
.pb
), size
);
1254 *descr_count
= d
.descr_count
;
1258 static void m4sl_cb(MpegTSFilter
*filter
, const uint8_t *section
,
1261 MpegTSContext
*ts
= filter
->u
.section_filter
.opaque
;
1262 MpegTSSectionFilter
*tssf
= &filter
->u
.section_filter
;
1264 const uint8_t *p
, *p_end
;
1266 int mp4_descr_count
= 0;
1267 Mp4Descr mp4_descr
[MAX_MP4_DESCR_COUNT
] = { { 0 } };
1269 AVFormatContext
*s
= ts
->stream
;
1271 p_end
= section
+ section_len
- 4;
1273 if (parse_section_header(&h
, &p
, p_end
) < 0)
1275 if (h
.tid
!= M4OD_TID
)
1277 if (h
.version
== tssf
->last_ver
)
1279 tssf
->last_ver
= h
.version
;
1281 mp4_read_od(s
, p
, (unsigned) (p_end
- p
), mp4_descr
, &mp4_descr_count
,
1282 MAX_MP4_DESCR_COUNT
);
1284 for (pid
= 0; pid
< NB_PID_MAX
; pid
++) {
1287 for (i
= 0; i
< mp4_descr_count
; i
++) {
1290 if (ts
->pids
[pid
]->es_id
!= mp4_descr
[i
].es_id
)
1292 if (!(ts
->pids
[pid
] && ts
->pids
[pid
]->type
== MPEGTS_PES
)) {
1293 av_log(s
, AV_LOG_ERROR
, "pid %x is not PES\n", pid
);
1296 pes
= ts
->pids
[pid
]->u
.pes_filter
.opaque
;
1301 pes
->sl
= mp4_descr
[i
].sl
;
1303 ffio_init_context(&pb
, mp4_descr
[i
].dec_config_descr
,
1304 mp4_descr
[i
].dec_config_descr_len
, 0,
1305 NULL
, NULL
, NULL
, NULL
);
1306 ff_mp4_read_dec_config_descr(s
, st
, &pb
);
1307 if (st
->codec
->codec_id
== AV_CODEC_ID_AAC
&&
1308 st
->codec
->extradata_size
> 0)
1309 st
->need_parsing
= 0;
1310 if (st
->codec
->codec_id
== AV_CODEC_ID_H264
&&
1311 st
->codec
->extradata_size
> 0)
1312 st
->need_parsing
= 0;
1314 if (st
->codec
->codec_id
<= AV_CODEC_ID_NONE
) {
1316 } else if (st
->codec
->codec_id
< AV_CODEC_ID_FIRST_AUDIO
)
1317 st
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
1318 else if (st
->codec
->codec_id
< AV_CODEC_ID_FIRST_SUBTITLE
)
1319 st
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
1320 else if (st
->codec
->codec_id
< AV_CODEC_ID_FIRST_UNKNOWN
)
1321 st
->codec
->codec_type
= AVMEDIA_TYPE_SUBTITLE
;
1324 for (i
= 0; i
< mp4_descr_count
; i
++)
1325 av_free(mp4_descr
[i
].dec_config_descr
);
1328 static const uint8_t opus_coupled_stream_cnt
[9] = {
1329 1, 0, 1, 1, 2, 2, 2, 3, 3
1332 static const uint8_t opus_stream_cnt
[9] = {
1333 1, 1, 1, 2, 2, 3, 4, 4, 5,
1336 static const uint8_t opus_channel_map
[8][8] = {
1344 { 0,6,1,2,3,4,5,7 },
1347 int ff_parse_mpeg2_descriptor(AVFormatContext
*fc
, AVStream
*st
, int stream_type
,
1348 const uint8_t **pp
, const uint8_t *desc_list_end
,
1349 Mp4Descr
*mp4_descr
, int mp4_descr_count
, int pid
,
1352 const uint8_t *desc_end
;
1353 int desc_len
, desc_tag
, desc_es_id
, ext_desc_tag
, channels
, channel_config_code
;
1357 desc_tag
= get8(pp
, desc_list_end
);
1359 return AVERROR_INVALIDDATA
;
1360 desc_len
= get8(pp
, desc_list_end
);
1362 return AVERROR_INVALIDDATA
;
1363 desc_end
= *pp
+ desc_len
;
1364 if (desc_end
> desc_list_end
)
1365 return AVERROR_INVALIDDATA
;
1367 av_log(fc
, AV_LOG_TRACE
, "tag: 0x%02x len=%d\n", desc_tag
, desc_len
);
1369 if (st
->codec
->codec_id
== AV_CODEC_ID_NONE
&&
1370 stream_type
== STREAM_TYPE_PRIVATE_DATA
)
1371 mpegts_find_stream_type(st
, desc_tag
, DESC_types
);
1374 case 0x1E: /* SL descriptor */
1375 desc_es_id
= get16(pp
, desc_end
);
1378 if (ts
&& ts
->pids
[pid
])
1379 ts
->pids
[pid
]->es_id
= desc_es_id
;
1380 for (i
= 0; i
< mp4_descr_count
; i
++)
1381 if (mp4_descr
[i
].dec_config_descr_len
&&
1382 mp4_descr
[i
].es_id
== desc_es_id
) {
1384 ffio_init_context(&pb
, mp4_descr
[i
].dec_config_descr
,
1385 mp4_descr
[i
].dec_config_descr_len
, 0,
1386 NULL
, NULL
, NULL
, NULL
);
1387 ff_mp4_read_dec_config_descr(fc
, st
, &pb
);
1388 if (st
->codec
->codec_id
== AV_CODEC_ID_AAC
&&
1389 st
->codec
->extradata_size
> 0)
1390 st
->need_parsing
= 0;
1391 if (st
->codec
->codec_id
== AV_CODEC_ID_MPEG4SYSTEMS
)
1392 mpegts_open_section_filter(ts
, pid
, m4sl_cb
, ts
, 1);
1395 case 0x1F: /* FMC descriptor */
1396 if (get16(pp
, desc_end
) < 0)
1398 if (mp4_descr_count
> 0 &&
1399 st
->codec
->codec_id
== AV_CODEC_ID_AAC_LATM
&&
1400 mp4_descr
->dec_config_descr_len
&& mp4_descr
->es_id
== pid
) {
1402 ffio_init_context(&pb
, mp4_descr
->dec_config_descr
,
1403 mp4_descr
->dec_config_descr_len
, 0,
1404 NULL
, NULL
, NULL
, NULL
);
1405 ff_mp4_read_dec_config_descr(fc
, st
, &pb
);
1406 if (st
->codec
->codec_id
== AV_CODEC_ID_AAC
&&
1407 st
->codec
->extradata_size
> 0)
1408 st
->need_parsing
= 0;
1411 case 0x56: /* DVB teletext descriptor */
1412 language
[0] = get8(pp
, desc_end
);
1413 language
[1] = get8(pp
, desc_end
);
1414 language
[2] = get8(pp
, desc_end
);
1416 av_dict_set(&st
->metadata
, "language", language
, 0);
1418 case 0x59: /* subtitling descriptor */
1419 language
[0] = get8(pp
, desc_end
);
1420 language
[1] = get8(pp
, desc_end
);
1421 language
[2] = get8(pp
, desc_end
);
1423 /* hearing impaired subtitles detection */
1424 switch (get8(pp
, desc_end
)) {
1425 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1426 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1427 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1428 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1429 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1430 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1431 st
->disposition
|= AV_DISPOSITION_HEARING_IMPAIRED
;
1434 if (st
->codec
->extradata
) {
1435 if (st
->codec
->extradata_size
== 4 &&
1436 memcmp(st
->codec
->extradata
, *pp
, 4))
1437 avpriv_request_sample(fc
, "DVB sub with multiple IDs");
1439 st
->codec
->extradata
= av_malloc(4 + FF_INPUT_BUFFER_PADDING_SIZE
);
1440 if (st
->codec
->extradata
) {
1441 st
->codec
->extradata_size
= 4;
1442 memcpy(st
->codec
->extradata
, *pp
, 4);
1446 av_dict_set(&st
->metadata
, "language", language
, 0);
1448 case 0x0a: /* ISO 639 language descriptor */
1449 for (i
= 0; i
+ 4 <= desc_len
; i
+= 4) {
1450 language
[i
+ 0] = get8(pp
, desc_end
);
1451 language
[i
+ 1] = get8(pp
, desc_end
);
1452 language
[i
+ 2] = get8(pp
, desc_end
);
1453 language
[i
+ 3] = ',';
1454 switch (get8(pp
, desc_end
)) {
1456 st
->disposition
|= AV_DISPOSITION_CLEAN_EFFECTS
;
1459 st
->disposition
|= AV_DISPOSITION_HEARING_IMPAIRED
;
1462 st
->disposition
|= AV_DISPOSITION_VISUAL_IMPAIRED
;
1466 if (i
&& language
[0]) {
1467 language
[i
- 1] = 0;
1468 av_dict_set(&st
->metadata
, "language", language
, 0);
1471 case 0x05: /* registration descriptor */
1472 st
->codec
->codec_tag
= bytestream_get_le32(pp
);
1473 av_log(fc
, AV_LOG_TRACE
, "reg_desc=%.4s\n", (char *)&st
->codec
->codec_tag
);
1474 if (st
->codec
->codec_id
== AV_CODEC_ID_NONE
)
1475 mpegts_find_stream_type(st
, st
->codec
->codec_tag
, REGD_types
);
1477 case 0x7f: /* DVB extension descriptor */
1478 ext_desc_tag
= get8(pp
, desc_end
);
1479 if (ext_desc_tag
< 0)
1480 return AVERROR_INVALIDDATA
;
1481 if (st
->codec
->codec_id
== AV_CODEC_ID_OPUS
&&
1482 ext_desc_tag
== 0x80) { /* User defined (provisional Opus) */
1483 if (!st
->codec
->extradata
) {
1484 st
->codec
->extradata
= av_mallocz(sizeof(opus_default_extradata
) +
1485 FF_INPUT_BUFFER_PADDING_SIZE
);
1486 if (!st
->codec
->extradata
)
1487 return AVERROR(ENOMEM
);
1489 st
->codec
->extradata_size
= sizeof(opus_default_extradata
);
1490 memcpy(st
->codec
->extradata
, opus_default_extradata
, sizeof(opus_default_extradata
));
1492 channel_config_code
= get8(pp
, desc_end
);
1493 if (channel_config_code
< 0)
1494 return AVERROR_INVALIDDATA
;
1495 if (channel_config_code
<= 0x8) {
1496 st
->codec
->extradata
[9] = channels
= channel_config_code ? channel_config_code
: 2;
1497 st
->codec
->extradata
[18] = channel_config_code ?
(channels
> 2) : 255;
1498 st
->codec
->extradata
[19] = opus_stream_cnt
[channel_config_code
];
1499 st
->codec
->extradata
[20] = opus_coupled_stream_cnt
[channel_config_code
];
1500 memcpy(&st
->codec
->extradata
[21], opus_channel_map
[channels
- 1], channels
);
1502 avpriv_request_sample(fc
, "Opus in MPEG-TS - channel_config_code > 0x8");
1504 st
->need_parsing
= AVSTREAM_PARSE_FULL
;
1515 static void pmt_cb(MpegTSFilter
*filter
, const uint8_t *section
, int section_len
)
1517 MpegTSContext
*ts
= filter
->u
.section_filter
.opaque
;
1518 MpegTSSectionFilter
*tssf
= &filter
->u
.section_filter
;
1519 SectionHeader h1
, *h
= &h1
;
1522 const uint8_t *p
, *p_end
, *desc_list_end
;
1523 int program_info_length
, pcr_pid
, pid
, stream_type
;
1525 uint32_t prog_reg_desc
= 0; /* registration descriptor */
1527 int mp4_descr_count
= 0;
1528 Mp4Descr mp4_descr
[MAX_MP4_DESCR_COUNT
] = { { 0 } };
1531 av_log(ts
->stream
, AV_LOG_TRACE
, "PMT: len %i\n", section_len
);
1532 hex_dump_debug(ts
->stream
, section
, section_len
);
1534 p_end
= section
+ section_len
- 4;
1536 if (parse_section_header(h
, &p
, p_end
) < 0)
1538 if (h
->version
== tssf
->last_ver
)
1540 tssf
->last_ver
= h
->version
;
1542 av_log(ts
->stream
, AV_LOG_TRACE
, "sid=0x%x sec_num=%d/%d\n",
1543 h
->id
, h
->sec_num
, h
->last_sec_num
);
1545 if (h
->tid
!= PMT_TID
)
1548 clear_program(ts
, h
->id
);
1549 pcr_pid
= get16(&p
, p_end
);
1553 add_pid_to_pmt(ts
, h
->id
, pcr_pid
);
1555 av_log(ts
->stream
, AV_LOG_TRACE
, "pcr_pid=0x%x\n", pcr_pid
);
1557 program_info_length
= get16(&p
, p_end
);
1558 if (program_info_length
< 0)
1560 program_info_length
&= 0xfff;
1561 while (program_info_length
>= 2) {
1563 tag
= get8(&p
, p_end
);
1564 len
= get8(&p
, p_end
);
1566 av_log(ts
->stream
, AV_LOG_TRACE
, "program tag: 0x%02x len=%d\n", tag
, len
);
1568 if (len
> program_info_length
- 2)
1569 // something else is broken, exit the program_descriptors_loop
1571 program_info_length
-= len
+ 2;
1572 if (tag
== 0x1d) { // IOD descriptor
1573 get8(&p
, p_end
); // scope
1574 get8(&p
, p_end
); // label
1576 mp4_read_iods(ts
->stream
, p
, len
, mp4_descr
+ mp4_descr_count
,
1577 &mp4_descr_count
, MAX_MP4_DESCR_COUNT
);
1578 } else if (tag
== 0x05 && len
>= 4) { // registration descriptor
1579 prog_reg_desc
= bytestream_get_le32(&p
);
1584 p
+= program_info_length
;
1588 // stop parsing after pmt, we found header
1589 if (!ts
->stream
->nb_streams
)
1596 stream_type
= get8(&p
, p_end
);
1597 if (stream_type
< 0)
1599 pid
= get16(&p
, p_end
);
1604 /* now create stream */
1605 if (ts
->pids
[pid
] && ts
->pids
[pid
]->type
== MPEGTS_PES
) {
1606 pes
= ts
->pids
[pid
]->u
.pes_filter
.opaque
;
1608 pes
->st
= avformat_new_stream(pes
->stream
, NULL
);
1609 pes
->st
->id
= pes
->pid
;
1612 } else if (stream_type
!= 0x13) {
1614 mpegts_close_filter(ts
, ts
->pids
[pid
]); // wrongly added sdt filter probably
1615 pes
= add_pes_stream(ts
, pid
, pcr_pid
);
1617 st
= avformat_new_stream(pes
->stream
, NULL
);
1621 int idx
= ff_find_stream_index(ts
->stream
, pid
);
1623 st
= ts
->stream
->streams
[idx
];
1625 st
= avformat_new_stream(ts
->stream
, NULL
);
1627 st
->codec
->codec_type
= AVMEDIA_TYPE_DATA
;
1634 if (pes
&& !pes
->stream_type
)
1635 mpegts_set_stream_info(st
, pes
, stream_type
, prog_reg_desc
);
1637 add_pid_to_pmt(ts
, h
->id
, pid
);
1639 ff_program_add_stream_index(ts
->stream
, h
->id
, st
->index
);
1641 desc_list_len
= get16(&p
, p_end
);
1642 if (desc_list_len
< 0)
1644 desc_list_len
&= 0xfff;
1645 desc_list_end
= p
+ desc_list_len
;
1646 if (desc_list_end
> p_end
)
1649 if (ff_parse_mpeg2_descriptor(ts
->stream
, st
, stream_type
, &p
,
1650 desc_list_end
, mp4_descr
,
1651 mp4_descr_count
, pid
, ts
) < 0)
1654 if (pes
&& prog_reg_desc
== AV_RL32("HDMV") &&
1655 stream_type
== 0x83 && pes
->sub_st
) {
1656 ff_program_add_stream_index(ts
->stream
, h
->id
,
1657 pes
->sub_st
->index
);
1658 pes
->sub_st
->codec
->codec_tag
= st
->codec
->codec_tag
;
1665 for (i
= 0; i
< mp4_descr_count
; i
++)
1666 av_free(mp4_descr
[i
].dec_config_descr
);
1669 static void pat_cb(MpegTSFilter
*filter
, const uint8_t *section
, int section_len
)
1671 MpegTSContext
*ts
= filter
->u
.section_filter
.opaque
;
1672 MpegTSSectionFilter
*tssf
= &filter
->u
.section_filter
;
1673 SectionHeader h1
, *h
= &h1
;
1674 const uint8_t *p
, *p_end
;
1677 av_log(ts
->stream
, AV_LOG_TRACE
, "PAT:\n");
1678 hex_dump_debug(ts
->stream
, section
, section_len
);
1680 p_end
= section
+ section_len
- 4;
1682 if (parse_section_header(h
, &p
, p_end
) < 0)
1684 if (h
->tid
!= PAT_TID
)
1686 if (h
->version
== tssf
->last_ver
)
1688 tssf
->last_ver
= h
->version
;
1692 sid
= get16(&p
, p_end
);
1695 pmt_pid
= get16(&p
, p_end
);
1700 av_log(ts
->stream
, AV_LOG_TRACE
, "sid=0x%x pid=0x%x\n", sid
, pmt_pid
);
1702 if (sid
== 0x0000) {
1705 av_new_program(ts
->stream
, sid
);
1706 if (ts
->pids
[pmt_pid
])
1707 mpegts_close_filter(ts
, ts
->pids
[pmt_pid
]);
1708 mpegts_open_section_filter(ts
, pmt_pid
, pmt_cb
, ts
, 1);
1709 add_pat_entry(ts
, sid
);
1710 add_pid_to_pmt(ts
, sid
, 0); // add pat pid to program
1711 add_pid_to_pmt(ts
, sid
, pmt_pid
);
1716 static void sdt_cb(MpegTSFilter
*filter
, const uint8_t *section
, int section_len
)
1718 MpegTSContext
*ts
= filter
->u
.section_filter
.opaque
;
1719 MpegTSSectionFilter
*tssf
= &filter
->u
.section_filter
;
1720 SectionHeader h1
, *h
= &h1
;
1721 const uint8_t *p
, *p_end
, *desc_list_end
, *desc_end
;
1722 int onid
, val
, sid
, desc_list_len
, desc_tag
, desc_len
, service_type
;
1723 char *name
, *provider_name
;
1725 av_log(ts
->stream
, AV_LOG_TRACE
, "SDT:\n");
1726 hex_dump_debug(ts
->stream
, section
, section_len
);
1728 p_end
= section
+ section_len
- 4;
1730 if (parse_section_header(h
, &p
, p_end
) < 0)
1732 if (h
->tid
!= SDT_TID
)
1734 if (h
->version
== tssf
->last_ver
)
1736 tssf
->last_ver
= h
->version
;
1738 onid
= get16(&p
, p_end
);
1741 val
= get8(&p
, p_end
);
1745 sid
= get16(&p
, p_end
);
1748 val
= get8(&p
, p_end
);
1751 desc_list_len
= get16(&p
, p_end
);
1752 if (desc_list_len
< 0)
1754 desc_list_len
&= 0xfff;
1755 desc_list_end
= p
+ desc_list_len
;
1756 if (desc_list_end
> p_end
)
1759 desc_tag
= get8(&p
, desc_list_end
);
1762 desc_len
= get8(&p
, desc_list_end
);
1763 desc_end
= p
+ desc_len
;
1764 if (desc_end
> desc_list_end
)
1767 av_log(ts
->stream
, AV_LOG_TRACE
, "tag: 0x%02x len=%d\n",
1768 desc_tag
, desc_len
);
1772 service_type
= get8(&p
, p_end
);
1773 if (service_type
< 0)
1775 provider_name
= getstr8(&p
, p_end
);
1778 name
= getstr8(&p
, p_end
);
1780 AVProgram
*program
= av_new_program(ts
->stream
, sid
);
1782 av_dict_set(&program
->metadata
, "service_name", name
, 0);
1783 av_dict_set(&program
->metadata
, "service_provider",
1788 av_free(provider_name
);
1799 /* handle one TS packet */
1800 static int handle_packet(MpegTSContext
*ts
, const uint8_t *packet
)
1803 int len
, pid
, cc
, expected_cc
, cc_ok
, afc
, is_start
, is_discontinuity
,
1804 has_adaptation
, has_payload
;
1805 const uint8_t *p
, *p_end
;
1808 pid
= AV_RB16(packet
+ 1) & 0x1fff;
1809 if (pid
&& discard_pid(ts
, pid
))
1811 is_start
= packet
[1] & 0x40;
1812 tss
= ts
->pids
[pid
];
1813 if (ts
->auto_guess
&& !tss
&& is_start
) {
1814 add_pes_stream(ts
, pid
, -1);
1815 tss
= ts
->pids
[pid
];
1820 afc
= (packet
[3] >> 4) & 3;
1821 if (afc
== 0) /* reserved value */
1823 has_adaptation
= afc
& 2;
1824 has_payload
= afc
& 1;
1825 is_discontinuity
= has_adaptation
&&
1826 packet
[4] != 0 && /* with length > 0 */
1827 (packet
[5] & 0x80); /* and discontinuity indicated */
1829 /* continuity check (currently not used) */
1830 cc
= (packet
[3] & 0xf);
1831 expected_cc
= has_payload ?
(tss
->last_cc
+ 1) & 0x0f : tss
->last_cc
;
1832 cc_ok
= pid
== 0x1FFF || // null packet PID
1839 av_log(ts
->stream
, AV_LOG_WARNING
,
1840 "Continuity check failed for pid %d expected %d got %d\n",
1841 pid
, expected_cc
, cc
);
1842 if (tss
->type
== MPEGTS_PES
) {
1843 PESContext
*pc
= tss
->u
.pes_filter
.opaque
;
1844 pc
->flags
|= AV_PKT_FLAG_CORRUPT
;
1851 if (has_adaptation
) {
1852 /* skip adaptation field */
1855 /* if past the end of packet, ignore */
1856 p_end
= packet
+ TS_PACKET_SIZE
;
1860 pos
= avio_tell(ts
->stream
->pb
);
1861 MOD_UNLIKELY(ts
->pos47
, pos
, ts
->raw_packet_size
, ts
->pos
);
1863 if (tss
->type
== MPEGTS_SECTION
) {
1865 /* pointer field present */
1867 if (p
+ len
> p_end
)
1870 /* write remaining section bytes */
1871 write_section_data(ts
, tss
,
1873 /* check whether filter has been closed */
1879 write_section_data(ts
, tss
,
1884 write_section_data(ts
, tss
,
1890 // Note: The position here points actually behind the current packet.
1891 if ((ret
= tss
->u
.pes_filter
.pes_cb(tss
, p
, p_end
- p
, is_start
,
1892 pos
- ts
->raw_packet_size
)) < 0)
1899 /* XXX: try to find a better synchro over several packets (use
1900 * get_packet_size() ?) */
1901 static int mpegts_resync(AVFormatContext
*s
)
1903 MpegTSContext
*ts
= s
->priv_data
;
1904 AVIOContext
*pb
= s
->pb
;
1907 for (i
= 0; i
< ts
->resync_size
; i
++) {
1909 if (pb
->eof_reached
)
1912 avio_seek(pb
, -1, SEEK_CUR
);
1916 av_log(s
, AV_LOG_ERROR
,
1917 "max resync size reached, could not find sync byte\n");
1919 return AVERROR_INVALIDDATA
;
1922 /* return AVERROR_something if error or EOF. Return 0 if OK. */
1923 static int read_packet(AVFormatContext
*s
, uint8_t *buf
, int raw_packet_size
,
1924 const uint8_t **data
)
1926 AVIOContext
*pb
= s
->pb
;
1930 len
= ffio_read_indirect(pb
, buf
, TS_PACKET_SIZE
, data
);
1931 if (len
!= TS_PACKET_SIZE
)
1932 return len
< 0 ? len
: AVERROR_EOF
;
1933 /* check packet sync byte */
1934 if ((*data
)[0] != 0x47) {
1935 /* find a new packet start */
1936 avio_seek(pb
, -TS_PACKET_SIZE
, SEEK_CUR
);
1937 if (mpegts_resync(s
) < 0)
1938 return AVERROR(EAGAIN
);
1948 static void finished_reading_packet(AVFormatContext
*s
, int raw_packet_size
)
1950 AVIOContext
*pb
= s
->pb
;
1951 int skip
= raw_packet_size
- TS_PACKET_SIZE
;
1953 avio_skip(pb
, skip
);
1956 static int handle_packets(MpegTSContext
*ts
, int nb_packets
)
1958 AVFormatContext
*s
= ts
->stream
;
1959 uint8_t packet
[TS_PACKET_SIZE
+ FF_INPUT_BUFFER_PADDING_SIZE
];
1960 const uint8_t *data
;
1961 int packet_num
, ret
= 0;
1963 if (avio_tell(s
->pb
) != ts
->last_pos
) {
1965 av_log(ts
->stream
, AV_LOG_TRACE
, "Skipping after seek\n");
1966 /* seek detected, flush pes buffer */
1967 for (i
= 0; i
< NB_PID_MAX
; i
++) {
1969 if (ts
->pids
[i
]->type
== MPEGTS_PES
) {
1970 PESContext
*pes
= ts
->pids
[i
]->u
.pes_filter
.opaque
;
1971 av_buffer_unref(&pes
->buffer
);
1972 pes
->data_index
= 0;
1973 pes
->state
= MPEGTS_SKIP
; /* skip until pes header */
1975 ts
->pids
[i
]->last_cc
= -1;
1982 memset(packet
+ TS_PACKET_SIZE
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
1984 if (ts
->stop_parse
> 0)
1987 if (nb_packets
!= 0 && packet_num
>= nb_packets
)
1989 ret
= read_packet(s
, packet
, ts
->raw_packet_size
, &data
);
1992 ret
= handle_packet(ts
, data
);
1993 finished_reading_packet(s
, ts
->raw_packet_size
);
1997 ts
->last_pos
= avio_tell(s
->pb
);
2001 static int mpegts_probe(AVProbeData
*p
)
2003 const int size
= p
->buf_size
;
2004 int score
, fec_score
, dvhs_score
;
2005 int check_count
= size
/ TS_FEC_PACKET_SIZE
;
2006 #define CHECK_COUNT 10
2008 if (check_count
< CHECK_COUNT
)
2009 return AVERROR_INVALIDDATA
;
2011 score
= analyze(p
->buf
, TS_PACKET_SIZE
* check_count
,
2012 TS_PACKET_SIZE
, NULL
, 1) * CHECK_COUNT
/ check_count
;
2013 dvhs_score
= analyze(p
->buf
, TS_DVHS_PACKET_SIZE
* check_count
,
2014 TS_DVHS_PACKET_SIZE
, NULL
, 1) * CHECK_COUNT
/ check_count
;
2015 fec_score
= analyze(p
->buf
, TS_FEC_PACKET_SIZE
* check_count
,
2016 TS_FEC_PACKET_SIZE
, NULL
, 1) * CHECK_COUNT
/ check_count
;
2017 av_log(NULL
, AV_LOG_TRACE
, "score: %d, dvhs_score: %d, fec_score: %d \n",
2018 score
, dvhs_score
, fec_score
);
2020 /* we need a clear definition for the returned score otherwise
2021 * things will become messy sooner or later */
2022 if (score
> fec_score
&& score
> dvhs_score
&& score
> 6)
2023 return AVPROBE_SCORE_MAX
+ score
- CHECK_COUNT
;
2024 else if (dvhs_score
> score
&& dvhs_score
> fec_score
&& dvhs_score
> 6)
2025 return AVPROBE_SCORE_MAX
+ dvhs_score
- CHECK_COUNT
;
2026 else if (fec_score
> 6)
2027 return AVPROBE_SCORE_MAX
+ fec_score
- CHECK_COUNT
;
2029 return AVERROR_INVALIDDATA
;
2032 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
2033 * (-1) if not available */
2034 static int parse_pcr(int64_t *ppcr_high
, int *ppcr_low
, const uint8_t *packet
)
2036 int afc
, len
, flags
;
2040 afc
= (packet
[3] >> 4) & 3;
2042 return AVERROR_INVALIDDATA
;
2047 return AVERROR_INVALIDDATA
;
2050 if (!(flags
& 0x10))
2051 return AVERROR_INVALIDDATA
;
2053 return AVERROR_INVALIDDATA
;
2055 *ppcr_high
= ((int64_t) v
<< 1) | (p
[4] >> 7);
2056 *ppcr_low
= ((p
[4] & 1) << 8) | p
[5];
2060 static int mpegts_read_header(AVFormatContext
*s
)
2062 MpegTSContext
*ts
= s
->priv_data
;
2063 AVIOContext
*pb
= s
->pb
;
2064 uint8_t buf
[5 * 1024];
2068 /* read the first 1024 bytes to get packet size */
2069 pos
= avio_tell(pb
);
2070 len
= avio_read(pb
, buf
, sizeof(buf
));
2073 if (len
!= sizeof(buf
))
2075 ts
->raw_packet_size
= get_packet_size(buf
, sizeof(buf
));
2076 if (ts
->raw_packet_size
<= 0)
2077 return AVERROR_INVALIDDATA
;
2081 if (s
->iformat
== &ff_mpegts_demuxer
) {
2084 /* first do a scan to get all the services */
2085 if (avio_seek(pb
, pos
, SEEK_SET
) < 0 && pb
->seekable
)
2086 av_log(s
, AV_LOG_ERROR
, "Unable to seek back to the start\n");
2088 mpegts_open_section_filter(ts
, SDT_PID
, sdt_cb
, ts
, 1);
2090 mpegts_open_section_filter(ts
, PAT_PID
, pat_cb
, ts
, 1);
2092 handle_packets(ts
, s
->probesize
/ ts
->raw_packet_size
);
2093 /* if could not find service, enable auto_guess */
2097 av_log(ts
->stream
, AV_LOG_TRACE
, "tuning done\n");
2099 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
2102 int pcr_pid
, pid
, nb_packets
, nb_pcrs
, ret
, pcr_l
;
2103 int64_t pcrs
[2], pcr_h
;
2104 int packet_count
[2];
2105 uint8_t packet
[TS_PACKET_SIZE
];
2106 const uint8_t *data
;
2108 /* only read packets */
2110 st
= avformat_new_stream(s
, NULL
);
2112 return AVERROR(ENOMEM
);
2113 avpriv_set_pts_info(st
, 60, 1, 27000000);
2114 st
->codec
->codec_type
= AVMEDIA_TYPE_DATA
;
2115 st
->codec
->codec_id
= AV_CODEC_ID_MPEG2TS
;
2117 /* we iterate until we find two PCRs to estimate the bitrate */
2122 ret
= read_packet(s
, packet
, ts
->raw_packet_size
, &data
);
2125 pid
= AV_RB16(data
+ 1) & 0x1fff;
2126 if ((pcr_pid
== -1 || pcr_pid
== pid
) &&
2127 parse_pcr(&pcr_h
, &pcr_l
, data
) == 0) {
2128 finished_reading_packet(s
, ts
->raw_packet_size
);
2130 packet_count
[nb_pcrs
] = nb_packets
;
2131 pcrs
[nb_pcrs
] = pcr_h
* 300 + pcr_l
;
2136 finished_reading_packet(s
, ts
->raw_packet_size
);
2141 /* NOTE1: the bitrate is computed without the FEC */
2142 /* NOTE2: it is only the bitrate of the start of the stream */
2143 ts
->pcr_incr
= (pcrs
[1] - pcrs
[0]) / (packet_count
[1] - packet_count
[0]);
2144 ts
->cur_pcr
= pcrs
[0] - ts
->pcr_incr
* packet_count
[0];
2145 s
->bit_rate
= TS_PACKET_SIZE
* 8 * 27e6
/ ts
->pcr_incr
;
2146 st
->codec
->bit_rate
= s
->bit_rate
;
2147 st
->start_time
= ts
->cur_pcr
;
2148 av_log(ts
->stream
, AV_LOG_TRACE
, "start=%0.3f pcr=%0.3f incr=%d\n",
2149 st
->start_time
/ 1000000.0, pcrs
[0] / 27e6
, ts
->pcr_incr
);
2152 avio_seek(pb
, pos
, SEEK_SET
);
2156 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
2158 static int mpegts_raw_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
2160 MpegTSContext
*ts
= s
->priv_data
;
2162 int64_t pcr_h
, next_pcr_h
, pos
;
2163 int pcr_l
, next_pcr_l
;
2164 uint8_t pcr_buf
[12];
2165 const uint8_t *data
;
2167 if (av_new_packet(pkt
, TS_PACKET_SIZE
) < 0)
2168 return AVERROR(ENOMEM
);
2169 ret
= read_packet(s
, pkt
->data
, ts
->raw_packet_size
, &data
);
2170 pkt
->pos
= avio_tell(s
->pb
);
2172 av_free_packet(pkt
);
2175 if (data
!= pkt
->data
)
2176 memcpy(pkt
->data
, data
, ts
->raw_packet_size
);
2177 finished_reading_packet(s
, ts
->raw_packet_size
);
2178 if (ts
->mpeg2ts_compute_pcr
) {
2179 /* compute exact PCR for each packet */
2180 if (parse_pcr(&pcr_h
, &pcr_l
, pkt
->data
) == 0) {
2181 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2182 pos
= avio_tell(s
->pb
);
2183 for (i
= 0; i
< MAX_PACKET_READAHEAD
; i
++) {
2184 avio_seek(s
->pb
, pos
+ i
* ts
->raw_packet_size
, SEEK_SET
);
2185 avio_read(s
->pb
, pcr_buf
, 12);
2186 if (parse_pcr(&next_pcr_h
, &next_pcr_l
, pcr_buf
) == 0) {
2187 /* XXX: not precise enough */
2189 ((next_pcr_h
- pcr_h
) * 300 + (next_pcr_l
- pcr_l
)) /
2194 avio_seek(s
->pb
, pos
, SEEK_SET
);
2195 /* no next PCR found: we use previous increment */
2196 ts
->cur_pcr
= pcr_h
* 300 + pcr_l
;
2198 pkt
->pts
= ts
->cur_pcr
;
2199 pkt
->duration
= ts
->pcr_incr
;
2200 ts
->cur_pcr
+= ts
->pcr_incr
;
2202 pkt
->stream_index
= 0;
2206 static int mpegts_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
2208 MpegTSContext
*ts
= s
->priv_data
;
2213 ret
= handle_packets(ts
, 0);
2215 /* flush pes data left */
2216 for (i
= 0; i
< NB_PID_MAX
; i
++)
2217 if (ts
->pids
[i
] && ts
->pids
[i
]->type
== MPEGTS_PES
) {
2218 PESContext
*pes
= ts
->pids
[i
]->u
.pes_filter
.opaque
;
2219 if (pes
->state
== MPEGTS_PAYLOAD
&& pes
->data_index
> 0) {
2220 new_pes_packet(pes
, pkt
);
2221 pes
->state
= MPEGTS_SKIP
;
2228 if (!ret
&& pkt
->size
< 0)
2229 ret
= AVERROR(EINTR
);
2233 static void mpegts_free(MpegTSContext
*ts
)
2239 for (i
= 0; i
< NB_PID_MAX
; i
++)
2241 mpegts_close_filter(ts
, ts
->pids
[i
]);
2244 static int mpegts_read_close(AVFormatContext
*s
)
2246 MpegTSContext
*ts
= s
->priv_data
;
2251 static int64_t mpegts_get_pcr(AVFormatContext
*s
, int stream_index
,
2252 int64_t *ppos
, int64_t pos_limit
)
2254 MpegTSContext
*ts
= s
->priv_data
;
2255 int64_t pos
, timestamp
;
2256 uint8_t buf
[TS_PACKET_SIZE
];
2257 int pcr_l
, pcr_pid
=
2258 ((PESContext
*)s
->streams
[stream_index
]->priv_data
)->pcr_pid
;
2259 const int find_next
= 1;
2261 ((*ppos
+ ts
->raw_packet_size
- 1 - ts
->pos47
) / ts
->raw_packet_size
) *
2262 ts
->raw_packet_size
+ ts
->pos47
;
2265 avio_seek(s
->pb
, pos
, SEEK_SET
);
2266 if (avio_read(s
->pb
, buf
, TS_PACKET_SIZE
) != TS_PACKET_SIZE
)
2267 return AV_NOPTS_VALUE
;
2268 if ((pcr_pid
< 0 || (AV_RB16(buf
+ 1) & 0x1fff) == pcr_pid
) &&
2269 parse_pcr(×tamp
, &pcr_l
, buf
) == 0) {
2272 pos
+= ts
->raw_packet_size
;
2276 pos
-= ts
->raw_packet_size
;
2278 return AV_NOPTS_VALUE
;
2279 avio_seek(s
->pb
, pos
, SEEK_SET
);
2280 if (avio_read(s
->pb
, buf
, TS_PACKET_SIZE
) != TS_PACKET_SIZE
)
2281 return AV_NOPTS_VALUE
;
2282 if ((pcr_pid
< 0 || (AV_RB16(buf
+ 1) & 0x1fff) == pcr_pid
) &&
2283 parse_pcr(×tamp
, &pcr_l
, buf
) == 0) {
2293 static int read_seek(AVFormatContext
*s
, int stream_index
, int64_t target_ts
, int flags
)
2295 MpegTSContext
*ts
= s
->priv_data
;
2296 uint8_t buf
[TS_PACKET_SIZE
];
2300 ret
= ff_seek_frame_binary(s
, stream_index
, target_ts
, flags
);
2304 pos
= avio_tell(s
->pb
);
2307 avio_seek(s
->pb
, pos
, SEEK_SET
);
2308 ret
= avio_read(s
->pb
, buf
, TS_PACKET_SIZE
);
2311 if (ret
!= TS_PACKET_SIZE
)
2313 // pid = AV_RB16(buf + 1) & 0x1fff;
2316 pos
+= ts
->raw_packet_size
;
2318 avio_seek(s
->pb
, pos
, SEEK_SET
);
2323 /**************************************************************/
2324 /* parsing functions - called from other demuxers such as RTP */
2326 MpegTSContext
*ff_mpegts_parse_open(AVFormatContext
*s
)
2330 ts
= av_mallocz(sizeof(MpegTSContext
));
2333 /* no stream case, currently used by RTP */
2334 ts
->raw_packet_size
= TS_PACKET_SIZE
;
2340 /* return the consumed length if a packet was output, or -1 if no
2341 * packet is output */
2342 int ff_mpegts_parse_packet(MpegTSContext
*ts
, AVPacket
*pkt
,
2343 const uint8_t *buf
, int len
)
2351 if (ts
->stop_parse
> 0)
2353 if (len
< TS_PACKET_SIZE
)
2354 return AVERROR_INVALIDDATA
;
2355 if (buf
[0] != 0x47) {
2359 handle_packet(ts
, buf
);
2360 buf
+= TS_PACKET_SIZE
;
2361 len
-= TS_PACKET_SIZE
;
2367 void ff_mpegts_parse_close(MpegTSContext
*ts
)
2373 AVInputFormat ff_mpegts_demuxer
= {
2375 .long_name
= NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2376 .priv_data_size
= sizeof(MpegTSContext
),
2377 .read_probe
= mpegts_probe
,
2378 .read_header
= mpegts_read_header
,
2379 .read_packet
= mpegts_read_packet
,
2380 .read_close
= mpegts_read_close
,
2381 .read_seek
= read_seek
,
2382 .read_timestamp
= mpegts_get_pcr
,
2383 .flags
= AVFMT_SHOW_IDS
| AVFMT_TS_DISCONT
,
2384 .priv_class
= &mpegts_class
,
2387 AVInputFormat ff_mpegtsraw_demuxer
= {
2388 .name
= "mpegtsraw",
2389 .long_name
= NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
2390 .priv_data_size
= sizeof(MpegTSContext
),
2391 .read_header
= mpegts_read_header
,
2392 .read_packet
= mpegts_raw_read_packet
,
2393 .read_close
= mpegts_read_close
,
2394 .read_seek
= read_seek
,
2395 .read_timestamp
= mpegts_get_pcr
,
2396 .flags
= AVFMT_SHOW_IDS
| AVFMT_TS_DISCONT
,
2397 .priv_class
= &mpegtsraw_class
,