FFmpeg
Loading...
Searching...
No Matches
mpegts.c
Go to the documentation of this file.
1/*
2 * MPEG-2 transport stream (aka DVB) demuxer
3 * Copyright (c) 2002-2003 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg 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.
11 *
12 * FFmpeg 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "config_components.h"
23
25#include "libavutil/buffer.h"
26#include "libavutil/crc.h"
27#include "libavutil/internal.h"
29#include "libavutil/log.h"
30#include "libavutil/dict.h"
31#include "libavutil/mem.h"
32#include "libavutil/opt.h"
33#include "libavutil/avassert.h"
34#include "libavutil/dovi_meta.h"
36#include "libavcodec/defs.h"
37#include "libavcodec/get_bits.h"
39#include "avformat.h"
40#include "mpegts.h"
41#include "internal.h"
42#include "avio_internal.h"
43#include "demux.h"
44#include "mpeg.h"
45#include "isom.h"
46#include "id3v2.h"
47#if CONFIG_ICONV
48#include <iconv.h>
49#endif
50
51/* maximum size in which we look for synchronization if
52 * synchronization is lost */
53#define MAX_RESYNC_SIZE 65536
54
55/* payload of a TS packet which carries no adaptation field */
56#define TS_PAYLOAD_SIZE (TS_PACKET_SIZE - 4)
57
58#define MAX_MP4_DESCR_COUNT 16
59
60#define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
61 do { \
62 if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
63 (modulus) = (dividend) % (divisor); \
64 (prev_dividend) = (dividend); \
65 } while (0)
66
67#define PROBE_PACKET_MAX_BUF 8192
68#define PROBE_PACKET_MARGIN 5
69
75
76typedef struct MpegTSFilter MpegTSFilter;
77
78typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
79 int is_start, int64_t pos);
80
85
86typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len);
87
88typedef void SetServiceCallback (void *opaque, int ret);
89
102
104 int pid;
105 int es_id;
106 int last_cc; /* last cc code (-1 if first packet) */
110 union {
113 } u;
114};
115
116struct Stream {
117 int idx;
119};
120
121#define MAX_STREAMS_PER_PROGRAM 128
122#define MAX_PIDS_PER_PROGRAM (MAX_STREAMS_PER_PROGRAM + 2)
123
126 int id;
127 int dep_pid; /* PID of the linked dependency stream */
128 unsigned int nb_streams;
130};
131
132struct Program {
133 unsigned int id; // program id/service id
134 unsigned int nb_pids;
136 unsigned int nb_streams;
138 unsigned int nb_stream_groups;
140
141 /** have we found pmt for this program */
143};
144
146 const AVClass *class;
147 /* user data */
149 /** raw packet size, including FEC if present */
151
153
154 /** if true, all pids are analyzed to find streams */
156
157 /** compute exact PCR for each transport stream packet */
159
160 /** fix dvb teletext pts */
162
163 int64_t cur_pcr; /**< used to estimate the exact PCR */
164 int64_t pcr_incr; /**< used to estimate the exact PCR */
165
166 /* data needed to handle file based ts */
167 /** stop parsing loop */
169 /** packet containing Audio/Video data */
171 /** to detect seek */
173
177
179
183
184 int id;
185
186 /******************************************/
187 /* private mpegts data */
188 /* scan context */
189 /** structure to keep track of Program->pids mapping */
190 unsigned int nb_prg;
191 unsigned int prg_size; ///< allocated size of prg in bytes
192 struct Program *prg;
193
195 /** filters for various streams specified by PMT + for the PAT and PMT */
198
201};
202
203#define MPEGTS_OPTIONS \
204 { "resync_size", "set size limit for looking up a new synchronization", \
205 offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, \
206 { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }, \
207 { "ts_id", "transport stream id", \
208 offsetof(MpegTSContext, id), AV_OPT_TYPE_INT, \
209 { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY }, \
210 { "ts_packetsize", "output option carrying the raw packet size", \
211 offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT, \
212 { .i64 = 0 }, 0, INT_MAX, AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY }
213
214static const AVOption options[] = {
216 {"fix_teletext_pts", "try to fix pts values of dvb teletext streams", offsetof(MpegTSContext, fix_teletext_pts), AV_OPT_TYPE_BOOL,
217 {.i64 = 1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
218 {"scan_all_pmts", "scan and combine all PMTs", offsetof(MpegTSContext, scan_all_pmts), AV_OPT_TYPE_BOOL,
219 {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM },
220 {"skip_unknown_pmt", "skip PMTs for programs not advertised in the PAT", offsetof(MpegTSContext, skip_unknown_pmt), AV_OPT_TYPE_BOOL,
221 {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
222 {"merge_pmt_versions", "reuse streams when PMT's version/pids change", offsetof(MpegTSContext, merge_pmt_versions), AV_OPT_TYPE_BOOL,
223 {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
224 {"skip_changes", "skip changing / adding streams / programs", offsetof(MpegTSContext, skip_changes), AV_OPT_TYPE_BOOL,
225 {.i64 = 0}, 0, 1, 0 },
226 {"skip_clear", "skip clearing programs", offsetof(MpegTSContext, skip_clear), AV_OPT_TYPE_BOOL,
227 {.i64 = 0}, 0, 1, 0 },
228 {"max_packet_size", "maximum size of emitted packet", offsetof(MpegTSContext, max_packet_size), AV_OPT_TYPE_INT,
229 {.i64 = 204800}, TS_PAYLOAD_SIZE, INT_MAX/2, AV_OPT_FLAG_DECODING_PARAM },
230 { NULL },
231};
232
233static const AVClass mpegts_class = {
234 .class_name = "mpegts demuxer",
235 .item_name = av_default_item_name,
236 .option = options,
237 .version = LIBAVUTIL_VERSION_INT,
238};
239
240static const AVOption raw_options[] = {
242 { "compute_pcr", "compute exact PCR for each transport stream packet",
243 offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_BOOL,
244 { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
245 { NULL },
246};
247
248static const AVClass mpegtsraw_class = {
249 .class_name = "mpegtsraw demuxer",
250 .item_name = av_default_item_name,
251 .option = raw_options,
252 .version = LIBAVUTIL_VERSION_INT,
253};
254
255/* TS stream handling */
256
264
265/* enough for PES header + length */
266#define PES_START_SIZE 6
267#define PES_HEADER_SIZE 9
268#define MAX_PES_HEADER_SIZE (9 + 255)
269
270typedef struct PESContext {
271 int pid;
272 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
277 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
279 /* used to get the format */
281 int flags; /**< copied to the AVPacket flags */
285 uint8_t stream_id;
287 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
292} PESContext;
293
295
296static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
297{
298 int i;
299 if (!ts)
300 return NULL;
301 for (i = 0; i < ts->nb_prg; i++) {
302 if (ts->prg[i].id == programid) {
303 return &ts->prg[i];
304 }
305 }
306 return NULL;
307}
308
309static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
310{
311 AVProgram *prg = NULL;
312 int i;
313
314 for (i = 0; i < ts->stream->nb_programs; i++)
315 if (ts->stream->programs[i]->id == programid) {
316 prg = ts->stream->programs[i];
317 break;
318 }
319 if (!prg)
320 return;
321 prg->nb_stream_indexes = 0;
322}
323
324static void clear_program(struct Program *p)
325{
326 if (!p)
327 return;
328 p->nb_pids = 0;
329 p->nb_streams = 0;
330 p->nb_stream_groups = 0;
331 memset(p->stream_groups, 0, sizeof(p->stream_groups));
332 p->pmt_found = 0;
333}
334
336{
337 av_freep(&ts->prg);
338 ts->nb_prg = 0;
339 ts->prg_size = 0;
340}
341
342static struct Program * add_program(MpegTSContext *ts, unsigned int programid)
343{
344 struct Program *p = get_program(ts, programid);
345 struct Program *tmp = NULL;
346 size_t new_prg_size;
347 if (p)
348 return p;
349
350 if (!av_size_mult(ts->nb_prg + 1, sizeof(*ts->prg), &new_prg_size))
351 tmp = av_fast_realloc(ts->prg, &ts->prg_size,new_prg_size);
352 if (!tmp) {
353 av_freep(&ts->prg);
354 ts->nb_prg = 0;
355 ts->prg_size = 0;
356 return NULL;
357 }
358 ts->prg = tmp;
359 p = &ts->prg[ts->nb_prg];
360 p->id = programid;
361 clear_program(p);
362 ts->nb_prg++;
363 return p;
364}
365
366static void add_pid_to_program(struct Program *p, unsigned int pid)
367{
368 int i;
369 if (!p)
370 return;
371
372 if (p->nb_pids >= MAX_PIDS_PER_PROGRAM)
373 return;
374
375 for (i = 0; i < p->nb_pids; i++)
376 if (p->pids[i] == pid)
377 return;
378
379 p->pids[p->nb_pids++] = pid;
380}
381
382static void update_av_program_info(AVFormatContext *s, unsigned int programid,
383 unsigned int pid, int version)
384{
385 int i;
386 for (i = 0; i < s->nb_programs; i++) {
387 AVProgram *program = s->programs[i];
388 if (program->id == programid) {
389 int old_pcr_pid = program->pcr_pid,
390 old_version = program->pmt_version;
391 program->pcr_pid = pid;
392 program->pmt_version = version;
393
394 if (old_version != -1 && old_version != version) {
396 "detected PMT change (program=%d, version=%d/%d, pcr_pid=0x%x/0x%x)\n",
397 programid, old_version, version, old_pcr_pid, pid);
398 }
399 break;
400 }
401 }
402}
403
404/**
405 * @brief discard_pid() decides if the pid is to be discarded according
406 * to caller's programs selection
407 * @param ts : - TS context
408 * @param pid : - pid
409 * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
410 * 0 otherwise
411 */
412static int discard_pid(MpegTSContext *ts, unsigned int pid)
413{
414 int i, j, k;
415 int used = 0, discarded = 0;
416 struct Program *p;
417
418 if (pid == PAT_PID)
419 return 0;
420
421 /* If none of the programs have .discard=AVDISCARD_ALL then there's
422 * no way we have to discard this packet */
423 for (k = 0; k < ts->stream->nb_programs; k++)
424 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
425 break;
426 if (k == ts->stream->nb_programs)
427 return 0;
428
429 for (i = 0; i < ts->nb_prg; i++) {
430 p = &ts->prg[i];
431 for (j = 0; j < p->nb_pids; j++) {
432 if (p->pids[j] != pid)
433 continue;
434 // is program with id p->id set to be discarded?
435 for (k = 0; k < ts->stream->nb_programs; k++) {
436 if (ts->stream->programs[k]->id == p->id) {
437 if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
438 discarded++;
439 else
440 used++;
441 }
442 }
443 }
444 }
445
446 return !used && discarded;
447}
448
449/**
450 * Assemble PES packets out of TS packets, and then call the "section_cb"
451 * function when they are complete.
452 */
454 const uint8_t *buf, int buf_size, int is_start)
455{
457 uint8_t *cur_section_buf = NULL;
458 int len, offset;
459
460 if (is_start) {
461 memcpy(tss->section_buf, buf, buf_size);
462 tss->section_index = buf_size;
463 tss->section_h_size = -1;
464 tss->end_of_section_reached = 0;
465 } else {
466 if (tss->end_of_section_reached)
467 return;
469 if (buf_size < len)
470 len = buf_size;
471 memcpy(tss->section_buf + tss->section_index, buf, len);
472 tss->section_index += len;
473 }
474
475 offset = 0;
476 cur_section_buf = tss->section_buf;
477 while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != STUFFING_BYTE) {
478 /* compute section length if possible */
479 if (tss->section_h_size == -1 && tss->section_index - offset >= 3) {
480 len = (AV_RB16(cur_section_buf + 1) & 0xfff) + 3;
481 if (len > MAX_SECTION_SIZE)
482 return;
483 tss->section_h_size = len;
484 }
485
486 if (tss->section_h_size != -1 &&
487 tss->section_index >= offset + tss->section_h_size) {
488 int crc_valid = 1;
489 tss->end_of_section_reached = 1;
490
491 if (tss->check_crc) {
492 crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, cur_section_buf, tss->section_h_size);
493 if (tss->section_h_size >= 4)
494 tss->crc = AV_RB32(cur_section_buf + tss->section_h_size - 4);
495
496 if (crc_valid) {
497 ts->crc_validity[ tss1->pid ] = 100;
498 }else if (ts->crc_validity[ tss1->pid ] > -10) {
499 ts->crc_validity[ tss1->pid ]--;
500 }else
501 crc_valid = 2;
502 }
503 if (crc_valid) {
504 tss->section_cb(tss1, cur_section_buf, tss->section_h_size);
505 if (crc_valid != 1)
506 tss->last_ver = -1;
507 }
508
509 cur_section_buf += tss->section_h_size;
510 offset += tss->section_h_size;
511 tss->section_h_size = -1;
512 } else {
513 tss->section_h_size = -1;
514 tss->end_of_section_reached = 0;
515 break;
516 }
517 }
518}
519
520static MpegTSFilter *mpegts_open_filter(MpegTSContext *ts, unsigned int pid,
522{
524
525 av_log(ts->stream, AV_LOG_TRACE, "Filter: pid=0x%x type=%d\n", pid, type);
526
527 if (pid >= NB_PID_MAX || ts->pids[pid])
528 return NULL;
529 filter = av_mallocz(sizeof(MpegTSFilter));
530 if (!filter)
531 return NULL;
532 ts->pids[pid] = filter;
533
534 filter->type = type;
535 filter->pid = pid;
536 filter->es_id = -1;
537 filter->last_cc = -1;
538 filter->last_pcr= -1;
539
540 return filter;
541}
542
544 unsigned int pid,
545 SectionCallback *section_cb,
546 void *opaque,
547 int check_crc)
548{
551 uint8_t *section_buf = av_mallocz(MAX_SECTION_SIZE);
552
553 if (!section_buf)
554 return NULL;
555
556 if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_SECTION))) {
557 av_free(section_buf);
558 return NULL;
559 }
560 sec = &filter->u.section_filter;
561 sec->section_cb = section_cb;
562 sec->opaque = opaque;
563 sec->section_buf = section_buf;
564 sec->check_crc = check_crc;
565 sec->last_ver = -1;
566
567 return filter;
568}
569
571 PESCallback *pes_cb,
572 void *opaque)
573{
575 MpegTSPESFilter *pes;
576
577 if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_PES)))
578 return NULL;
579
580 pes = &filter->u.pes_filter;
581 pes->pes_cb = pes_cb;
582 pes->opaque = opaque;
583 return filter;
584}
585
587{
588 return mpegts_open_filter(ts, pid, MPEGTS_PCR);
589}
590
592{
593 int pid;
594
595 pid = filter->pid;
596 if (filter->type == MPEGTS_SECTION)
597 av_freep(&filter->u.section_filter.section_buf);
598 else if (filter->type == MPEGTS_PES) {
599 PESContext *pes = filter->u.pes_filter.opaque;
600 av_buffer_unref(&pes->buffer);
601 /* referenced private data will be freed later in
602 * avformat_close_input (pes->st->priv_data == pes) */
603 if (!pes->st || pes->merged_st || !pes->st->priv_data) {
604 av_freep(&filter->u.pes_filter.opaque);
605 }
606 }
607
609 ts->pids[pid] = NULL;
610}
611
612static int analyze(const uint8_t *buf, int size, int packet_size,
613 int probe)
614{
615 int stat[TS_MAX_PACKET_SIZE];
616 int stat_all = 0;
617 int i;
618 int best_score = 0;
619
620 memset(stat, 0, packet_size * sizeof(*stat));
621
622 for (i = 0; i < size - 3; i++) {
623 if (buf[i] == SYNC_BYTE) {
624 int pid = AV_RB16(buf+1) & 0x1FFF;
625 int asc = buf[i + 3] & 0x30;
626 if (!probe || pid == 0x1FFF || asc) {
627 int x = i % packet_size;
628 stat[x]++;
629 stat_all++;
630 if (stat[x] > best_score) {
631 best_score = stat[x];
632 }
633 }
634 }
635 }
636
637 return best_score - FFMAX(stat_all - 10*best_score, 0)/10;
638}
639
640/* autodetect fec presence */
642{
643 int score, fec_score, dvhs_score;
644 int margin;
645 int ret;
646
647 /*init buffer to store stream for probing */
648 uint8_t buf[PROBE_PACKET_MAX_BUF] = {0};
649 int buf_size = 0;
650 int max_iterations = 16;
651
652 while (buf_size < PROBE_PACKET_MAX_BUF && max_iterations--) {
653 ret = avio_read_partial(s->pb, buf + buf_size, PROBE_PACKET_MAX_BUF - buf_size);
654 if (ret < 0)
655 return AVERROR_INVALIDDATA;
656 buf_size += ret;
657
658 score = analyze(buf, buf_size, TS_PACKET_SIZE, 0);
659 dvhs_score = analyze(buf, buf_size, TS_DVHS_PACKET_SIZE, 0);
660 fec_score = analyze(buf, buf_size, TS_FEC_PACKET_SIZE, 0);
661 av_log(s, AV_LOG_TRACE, "Probe: %d, score: %d, dvhs_score: %d, fec_score: %d \n",
662 buf_size, score, dvhs_score, fec_score);
663
664 margin = mid_pred(score, fec_score, dvhs_score);
665
666 if (buf_size < PROBE_PACKET_MAX_BUF)
667 margin += PROBE_PACKET_MARGIN; /*if buffer not filled */
668
669 if (score > margin)
670 return TS_PACKET_SIZE;
671 else if (dvhs_score > margin)
672 return TS_DVHS_PACKET_SIZE;
673 else if (fec_score > margin)
674 return TS_FEC_PACKET_SIZE;
675 }
676 return AVERROR_INVALIDDATA;
677}
678
679typedef struct SectionHeader {
680 uint8_t tid;
681 uint16_t id;
682 uint8_t version;
684 uint8_t sec_num;
687
689{
690 if (h->version == tssf->last_ver && tssf->last_crc == tssf->crc)
691 return 1;
692
693 tssf->last_ver = h->version;
694 tssf->last_crc = tssf->crc;
695
696 return 0;
697}
698
699static inline int get8(const uint8_t **pp, const uint8_t *p_end)
700{
701 const uint8_t *p;
702 int c;
703
704 p = *pp;
705 if (p >= p_end)
706 return AVERROR_INVALIDDATA;
707 c = *p++;
708 *pp = p;
709 return c;
710}
711
712static inline int get16(const uint8_t **pp, const uint8_t *p_end)
713{
714 const uint8_t *p;
715 int c;
716
717 p = *pp;
718 if (1 >= p_end - p)
719 return AVERROR_INVALIDDATA;
720 c = AV_RB16(p);
721 p += 2;
722 *pp = p;
723 return c;
724}
725
726/* read and allocate a DVB string preceded by its length */
727static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
728{
729 int len;
730 const uint8_t *p;
731 char *str;
732
733 p = *pp;
734 len = get8(&p, p_end);
735 if (len < 0)
736 return NULL;
737 if (len > p_end - p)
738 return NULL;
739#if CONFIG_ICONV
740 if (len) {
741 const char *encodings[] = {
742 "ISO6937", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7",
743 "ISO-8859-8", "ISO-8859-9", "ISO-8859-10", "ISO-8859-11",
744 "", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "", "", "", "",
745 "", "UCS-2BE", "KSC_5601", "GB2312", "UCS-2BE", "UTF-8", "", "",
746 "", "", "", "", "", "", "", ""
747 };
748 iconv_t cd;
749 char *in, *out;
750 size_t inlen = len, outlen = inlen * 6 + 1;
751 if (len >= 3 && p[0] == 0x10 && !p[1] && p[2] && p[2] <= 0xf && p[2] != 0xc) {
752 char iso8859[12];
753 snprintf(iso8859, sizeof(iso8859), "ISO-8859-%d", p[2]);
754 inlen -= 3;
755 in = (char *)p + 3;
756 cd = iconv_open("UTF-8", iso8859);
757 } else if (p[0] < 0x20) {
758 inlen -= 1;
759 in = (char *)p + 1;
760 cd = iconv_open("UTF-8", encodings[*p]);
761 } else {
762 in = (char *)p;
763 cd = iconv_open("UTF-8", encodings[0]);
764 }
765 if (cd == (iconv_t)-1)
766 goto no_iconv;
767 str = out = av_malloc(outlen);
768 if (!str) {
769 iconv_close(cd);
770 return NULL;
771 }
772 if (iconv(cd, &in, &inlen, &out, &outlen) == -1) {
773 iconv_close(cd);
774 av_freep(&str);
775 goto no_iconv;
776 }
777 iconv_close(cd);
778 *out = 0;
779 *pp = p + len;
780 return str;
781 }
782no_iconv:
783#endif
784 str = av_malloc(len + 1);
785 if (!str)
786 return NULL;
787 memcpy(str, p, len);
788 str[len] = '\0';
789 p += len;
790 *pp = p;
791 return str;
792}
793
795 const uint8_t **pp, const uint8_t *p_end)
796{
797 int val;
798
799 val = get8(pp, p_end);
800 if (val < 0)
801 return val;
802 h->tid = val;
803 *pp += 2;
804 val = get16(pp, p_end);
805 if (val < 0)
806 return val;
807 h->id = val;
808 val = get8(pp, p_end);
809 if (val < 0)
810 return val;
811 h->version = (val >> 1) & 0x1f;
812 h->current_next = val & 0x01;
813 val = get8(pp, p_end);
814 if (val < 0)
815 return val;
816 h->sec_num = val;
817 val = get8(pp, p_end);
818 if (val < 0)
819 return val;
820 h->last_sec_num = val;
821 return 0;
822}
823
829
830static const StreamType ISO_types[] = {
837 /* Makito encoder sets stream type 0x11 for AAC,
838 * so auto-detect LOAS/LATM instead of hardcoding it. */
839#if !CONFIG_LOAS_DEMUXER
841#endif
855 { 0 },
856};
857
872
873/* SCTE types */
878
879/* ATSC ? */
886
887/* HLS Sample Encryption Types */
895
896static const StreamType REGD_types[] = {
897 { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
898 { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
899 { MKTAG('A', 'C', '-', '4'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC4 },
900 { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
901 { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
902 { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
903 { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
904 { MKTAG('E', 'A', 'C', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
905 { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
906 { MKTAG('V', 'V', 'C', ' '), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VVC },
907 { MKTAG('K', 'L', 'V', 'A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
908 { MKTAG('V', 'A', 'N', 'C'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_2038 },
909 { MKTAG('I', 'D', '3', ' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
910 { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
911 { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS },
912 { 0 },
913};
914
915static const StreamType METADATA_types[] = {
916 { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
917 { MKTAG('I','D','3',' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
918 { 0 },
919};
920
921/* descriptor present */
930
932 uint32_t stream_type,
933 const StreamType *types)
934{
935 FFStream *const sti = ffstream(st);
936 for (; types->stream_type; types++)
937 if (stream_type == types->stream_type) {
938 if (st->codecpar->codec_type != types->codec_type ||
939 st->codecpar->codec_id != types->codec_id) {
940 st->codecpar->codec_type = types->codec_type;
941 st->codecpar->codec_id = types->codec_id;
942 sti->need_context_update = 1;
943 }
944 sti->request_probe = 0;
945 return;
946 }
947}
948
950 uint32_t stream_type, uint32_t prog_reg_desc)
951{
952 FFStream *const sti = ffstream(st);
953 int old_codec_type = st->codecpar->codec_type;
954 int old_codec_id = st->codecpar->codec_id;
955 int old_codec_tag = st->codecpar->codec_tag;
956
957 avpriv_set_pts_info(st, 33, 1, 90000);
958 st->priv_data = pes;
962 pes->st = st;
963 pes->stream_type = stream_type;
964
966 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
967 st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
968
969 st->codecpar->codec_tag = pes->stream_type;
970
973 sti->request_probe = 50;
976 if ((prog_reg_desc == AV_RL32("HDMV") ||
977 prog_reg_desc == AV_RL32("HDPR")) &&
981 // HDMV TrueHD streams also contain an AC3 coded version of the
982 // audio track - add a second stream for this
983 AVStream *sub_st;
984 // priv_data cannot be shared between streams
985 PESContext *sub_pes = av_memdup(pes, sizeof(*sub_pes));
986 if (!sub_pes)
987 return AVERROR(ENOMEM);
988
989 sub_st = avformat_new_stream(pes->stream, NULL);
990 if (!sub_st) {
991 av_free(sub_pes);
992 return AVERROR(ENOMEM);
993 }
994
995 sub_st->id = pes->pid;
996 avpriv_set_pts_info(sub_st, 33, 1, 90000);
997 sub_st->priv_data = sub_pes;
1001 sub_pes->sub_st = pes->sub_st = sub_st;
1002 }
1003 }
1008 if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
1009 st->codecpar->codec_id = old_codec_id;
1010 st->codecpar->codec_type = old_codec_type;
1011 }
1012 if ((st->codecpar->codec_id == AV_CODEC_ID_NONE ||
1013 (sti->request_probe > 0 && sti->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) &&
1014 sti->probe_packets > 0 &&
1015 stream_type == STREAM_TYPE_PRIVATE_DATA) {
1019 }
1020
1021 /* queue a context update if properties changed */
1022 if (old_codec_type != st->codecpar->codec_type ||
1023 old_codec_id != st->codecpar->codec_id ||
1024 old_codec_tag != st->codecpar->codec_tag)
1025 sti->need_context_update = 1;
1026
1027 return 0;
1028}
1029
1031{
1032 pes->pts = AV_NOPTS_VALUE;
1033 pes->dts = AV_NOPTS_VALUE;
1034 pes->data_index = 0;
1035 pes->flags = 0;
1036 av_buffer_unref(&pes->buffer);
1037}
1038
1039static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
1040{
1042 pkt->data = (uint8_t *)buffer;
1043 pkt->size = len;
1044}
1045
1047{
1048 FFIOContext id3_buf;
1049 ID3v2ExtraMeta *extra_meta = NULL;
1051 int ret = 0;
1052
1053 ffio_init_read_context(&id3_buf, pkt->data, pkt->size);
1054 ff_id3v2_read_dict(NULL, &id3_buf.pub, &metadata, ID3v2_DEFAULT_MAGIC, &extra_meta);
1055 ret = ff_id3v2_parse_priv_dict(&metadata, extra_meta);
1056 ff_id3v2_free_extra_meta(&extra_meta);
1057
1058 if (ret < 0 || !av_dict_count(metadata))
1059 goto end;
1060
1061 ret = av_dict_copy(&s->metadata, metadata, 0);
1062 if (ret == 0)
1064
1065end:
1067 return ret;
1068}
1069
1071{
1072 uint8_t *sd;
1073
1075
1076 pkt->buf = pes->buffer;
1077 pkt->data = pes->buffer->data;
1078 pkt->size = pes->data_index;
1079
1080 if (pes->PES_packet_length &&
1081 pes->pes_header_size + pes->data_index != pes->PES_packet_length +
1083 av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
1084 pes->flags |= AV_PKT_FLAG_CORRUPT;
1085 }
1086
1087 // JPEG-XS PES payload
1088 if (pes->stream_id == 0xbd && pes->stream_type == 0x32 &&
1089 pkt->size >= 8 && memcmp(pkt->data + 4, "jxes", 4) == 0)
1090 {
1091 uint32_t header_size = AV_RB32(pkt->data);
1092 if (header_size > pkt->size) {
1094 "Invalid JPEG-XS header size %"PRIu32" > packet size %d\n",
1095 header_size, pkt->size);
1096 pes->flags |= AV_PKT_FLAG_CORRUPT;
1097 } else {
1098 pkt->data += header_size;
1099 pkt->size -= header_size;
1100 }
1101 }
1102
1103 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1104
1105 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
1106 if (pes->sub_st && pes->stream_type == STREAM_TYPE_BLURAY_AUDIO_TRUEHD && pes->extended_stream_id == 0x76)
1107 pkt->stream_index = pes->sub_st->index;
1108 else
1109 pkt->stream_index = pes->st->index;
1110 pkt->pts = pes->pts;
1111 pkt->dts = pes->dts;
1112 /* store position of first TS packet of this PES packet */
1113 pkt->pos = pes->ts_packet_pos;
1114 pkt->flags = pes->flags;
1115
1116 pes->buffer = NULL;
1118
1120 if (!sd)
1121 return AVERROR(ENOMEM);
1122 *sd = pes->stream_id;
1123
1124 if (pes->st->codecpar->codec_id == AV_CODEC_ID_TIMED_ID3) {
1125 int ret = timed_id3_update_metadata(pes->st, pkt);
1126 if (ret < 0)
1127 return ret;
1128 }
1129
1130 return 0;
1131}
1132
1133static uint64_t get_ts64(GetBitContext *gb, int bits)
1134{
1135 if (get_bits_left(gb) < bits)
1136 return AV_NOPTS_VALUE;
1137 return get_bits64(gb, bits);
1138}
1139
1141 const uint8_t *buf, int buf_size)
1142{
1143 GetBitContext gb;
1144 int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
1145 int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
1146 int dts_flag = -1, cts_flag = -1;
1148 uint8_t buf_padded[128 + AV_INPUT_BUFFER_PADDING_SIZE];
1149 int buf_padded_size = FFMIN(buf_size, sizeof(buf_padded) - AV_INPUT_BUFFER_PADDING_SIZE);
1150
1151 memcpy(buf_padded, buf, buf_padded_size);
1152
1153 init_get_bits(&gb, buf_padded, buf_padded_size * 8);
1154
1155 if (sl->use_au_start)
1156 au_start_flag = get_bits1(&gb);
1157 if (sl->use_au_end)
1158 au_end_flag = get_bits1(&gb);
1159 if (!sl->use_au_start && !sl->use_au_end)
1160 au_start_flag = au_end_flag = 1;
1161 if (sl->ocr_len > 0)
1162 ocr_flag = get_bits1(&gb);
1163 if (sl->use_idle)
1164 idle_flag = get_bits1(&gb);
1165 if (sl->use_padding)
1166 padding_flag = get_bits1(&gb);
1167 if (padding_flag)
1168 padding_bits = get_bits(&gb, 3);
1169
1170 if (!idle_flag && (!padding_flag || padding_bits != 0)) {
1171 if (sl->packet_seq_num_len)
1173 if (sl->degr_prior_len)
1174 if (get_bits1(&gb))
1175 skip_bits(&gb, sl->degr_prior_len);
1176 if (ocr_flag)
1177 skip_bits_long(&gb, sl->ocr_len);
1178 if (au_start_flag) {
1179 if (sl->use_rand_acc_pt)
1180 get_bits1(&gb);
1181 if (sl->au_seq_num_len > 0)
1183 if (sl->use_timestamps) {
1184 dts_flag = get_bits1(&gb);
1185 cts_flag = get_bits1(&gb);
1186 }
1187 }
1188 if (sl->inst_bitrate_len)
1189 inst_bitrate_flag = get_bits1(&gb);
1190 if (dts_flag == 1)
1191 dts = get_ts64(&gb, sl->timestamp_len);
1192 if (cts_flag == 1)
1193 cts = get_ts64(&gb, sl->timestamp_len);
1194 if (sl->au_len > 0)
1195 skip_bits_long(&gb, sl->au_len);
1196 if (inst_bitrate_flag)
1198 }
1199
1200 if (dts != AV_NOPTS_VALUE)
1201 pes->dts = dts;
1202 if (cts != AV_NOPTS_VALUE)
1203 pes->pts = cts;
1204
1205 if (sl->timestamp_len && sl->timestamp_res)
1207
1208 return (get_bits_count(&gb) + 7) >> 3;
1209}
1210
1212{
1214 if (!ts->pools[index]) {
1215 int pool_size = FFMIN(ts->max_packet_size + AV_INPUT_BUFFER_PADDING_SIZE, 2 << index);
1216 ts->pools[index] = av_buffer_pool_init(pool_size, NULL);
1217 if (!ts->pools[index])
1218 return NULL;
1219 }
1220 return av_buffer_pool_get(ts->pools[index]);
1221}
1222
1223/* return non zero if a packet could be constructed */
1225 const uint8_t *buf, int buf_size, int is_start,
1226 int64_t pos)
1227{
1228 PESContext *pes = filter->u.pes_filter.opaque;
1229 MpegTSContext *ts = pes->ts;
1230 const uint8_t *p;
1231 int ret, len;
1232
1233 if (!ts->pkt)
1234 return 0;
1235
1236 if (is_start) {
1237 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1238 ret = new_pes_packet(pes, ts->pkt);
1239 if (ret < 0)
1240 return ret;
1241 ts->stop_parse = 1;
1242 } else {
1244 }
1245 pes->state = MPEGTS_HEADER;
1246 pes->ts_packet_pos = pos;
1247 }
1248 p = buf;
1249 while (buf_size > 0) {
1250 switch (pes->state) {
1251 case MPEGTS_HEADER:
1252 len = PES_START_SIZE - pes->data_index;
1253 if (len > buf_size)
1254 len = buf_size;
1255 memcpy(pes->header + pes->data_index, p, len);
1256 pes->data_index += len;
1257 p += len;
1258 buf_size -= len;
1259 if (pes->data_index == PES_START_SIZE) {
1260 /* we got all the PES or section header. We can now
1261 * decide */
1262 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
1263 pes->header[2] == 0x01) {
1264 /* it must be an MPEG-2 PES stream */
1265 pes->stream_id = pes->header[3];
1266 av_log(pes->stream, AV_LOG_TRACE, "pid=%x stream_id=%#x\n", pes->pid, pes->stream_id);
1267
1268 if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
1269 (!pes->sub_st ||
1270 pes->sub_st->discard == AVDISCARD_ALL)) ||
1272 goto skip;
1273
1274 /* stream not present in PMT */
1275 if (!pes->st) {
1276 if (ts->skip_changes)
1277 goto skip;
1278 if (ts->merge_pmt_versions)
1279 goto skip; /* wait for PMT to merge new stream */
1280
1281 pes->st = avformat_new_stream(ts->stream, NULL);
1282 if (!pes->st)
1283 return AVERROR(ENOMEM);
1284 pes->st->id = pes->pid;
1285 mpegts_set_stream_info(pes->st, pes, 0, 0);
1286 }
1287
1288 pes->PES_packet_length = AV_RB16(pes->header + 4);
1289 /* NOTE: zero length means the PES size is unbounded */
1290
1298 FFStream *const pes_sti = ffstream(pes->st);
1299 pes->state = MPEGTS_PESHEADER;
1300 if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !pes_sti->request_probe) {
1302 "pid=%x stream_type=%x probing\n",
1303 pes->pid,
1304 pes->stream_type);
1305 pes_sti->request_probe = 1;
1306 }
1307 } else {
1308 pes->pes_header_size = 6;
1309 pes->state = MPEGTS_PAYLOAD;
1310 pes->data_index = 0;
1311 }
1312 } else {
1313 /* otherwise, it should be a table */
1314 /* skip packet */
1315skip:
1316 pes->state = MPEGTS_SKIP;
1317 continue;
1318 }
1319 }
1320 break;
1321 /**********************************************/
1322 /* PES packing parsing */
1323 case MPEGTS_PESHEADER:
1325 if (len < 0)
1326 return AVERROR_INVALIDDATA;
1327 if (len > buf_size)
1328 len = buf_size;
1329 memcpy(pes->header + pes->data_index, p, len);
1330 pes->data_index += len;
1331 p += len;
1332 buf_size -= len;
1333 if (pes->data_index == PES_HEADER_SIZE) {
1334 pes->pes_header_size = pes->header[8] + 9;
1336 }
1337 break;
1339 len = pes->pes_header_size - pes->data_index;
1340 if (len < 0)
1341 return AVERROR_INVALIDDATA;
1342 if (len > buf_size)
1343 len = buf_size;
1344 memcpy(pes->header + pes->data_index, p, len);
1345 pes->data_index += len;
1346 p += len;
1347 buf_size -= len;
1348 if (pes->data_index == pes->pes_header_size) {
1349 const uint8_t *r;
1350 unsigned int flags, pes_ext, skip;
1351
1352 flags = pes->header[7];
1353 r = pes->header + 9;
1354 pes->pts = AV_NOPTS_VALUE;
1355 pes->dts = AV_NOPTS_VALUE;
1356 if ((flags & 0xc0) == 0x80) {
1357 pes->dts = pes->pts = ff_parse_pes_pts(r);
1358 r += 5;
1359 } else if ((flags & 0xc0) == 0xc0) {
1360 pes->pts = ff_parse_pes_pts(r);
1361 r += 5;
1362 pes->dts = ff_parse_pes_pts(r);
1363 r += 5;
1364 }
1365 pes->extended_stream_id = -1;
1366 if (flags & 0x01) { /* PES extension */
1367 pes_ext = *r++;
1368 /* Skip PES private data, program packet sequence counter and P-STD buffer */
1369 skip = (pes_ext >> 4) & 0xb;
1370 skip += skip & 0x9;
1371 r += skip;
1372 if ((pes_ext & 0x41) == 0x01 &&
1373 (r + 2) <= (pes->header + pes->pes_header_size)) {
1374 /* PES extension 2 */
1375 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1376 pes->extended_stream_id = r[1];
1377 }
1378 }
1379
1380 /* we got the full header. We parse it and get the payload */
1381 pes->state = MPEGTS_PAYLOAD;
1382 pes->data_index = 0;
1383 if (pes->stream_type == STREAM_TYPE_ISO_IEC_14496_PES && buf_size > 0) {
1384 int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
1385 buf_size);
1386 pes->pes_header_size += sl_header_bytes;
1387 p += sl_header_bytes;
1388 buf_size -= sl_header_bytes;
1389 }
1390 if (pes->stream_type == STREAM_TYPE_METADATA &&
1393 buf_size >= 5) {
1394 /* skip metadata access unit header - see MISB ST 1402 */
1395 pes->pes_header_size += 5;
1396 p += 5;
1397 buf_size -= 5;
1398 }
1399 if ( pes->ts->fix_teletext_pts
1402 ) {
1403 AVProgram *p = NULL;
1404 int pcr_found = 0;
1405 while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
1406 if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
1407 MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
1408 if (f) {
1409 AVStream *st = NULL;
1410 if (f->type == MPEGTS_PES) {
1411 PESContext *pcrpes = f->u.pes_filter.opaque;
1412 if (pcrpes)
1413 st = pcrpes->st;
1414 } else if (f->type == MPEGTS_PCR) {
1415 int i;
1416 for (i = 0; i < p->nb_stream_indexes; i++) {
1417 AVStream *pst = pes->stream->streams[p->stream_index[i]];
1419 st = pst;
1420 }
1421 }
1422 if (f->last_pcr != -1 && !f->discard) {
1423 // teletext packets do not always have correct timestamps,
1424 // the standard says they should be handled after 40.6 ms at most,
1425 // and the pcr error to this packet should be no more than 100 ms.
1426 // TODO: we should interpolate the PCR, not just use the last one
1427 int64_t pcr = f->last_pcr / SYSTEM_CLOCK_FREQUENCY_DIVISOR;
1428 pcr_found = 1;
1429 if (st) {
1430 const FFStream *const sti = ffstream(st);
1431 FFStream *const pes_sti = ffstream(pes->st);
1432
1433 pes_sti->pts_wrap_reference = sti->pts_wrap_reference;
1434 pes_sti->pts_wrap_behavior = sti->pts_wrap_behavior;
1435 }
1436 if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
1437 pes->pts = pes->dts = pcr;
1438 } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1439 pes->dts > pcr + 3654 + 9000) {
1440 pes->pts = pes->dts = pcr + 3654 + 9000;
1441 } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1442 pes->dts > pcr + 10*90000) { //10sec
1443 pes->pts = pes->dts = pcr + 3654 + 9000;
1444 }
1445 break;
1446 }
1447 }
1448 }
1449 }
1450
1452 !pcr_found) {
1454 "Forcing DTS/PTS to be unset for a "
1455 "non-trustworthy PES packet for PID %d as "
1456 "PCR hasn't been received yet.\n",
1457 pes->pid);
1458 pes->dts = pes->pts = AV_NOPTS_VALUE;
1459 }
1460 }
1461 }
1462 break;
1463 case MPEGTS_PAYLOAD:
1464 do {
1465 int max_packet_size = ts->max_packet_size;
1467 const int pes_payload_size = pes->PES_packet_length + PES_START_SIZE - pes->pes_header_size;
1468
1469 if (pes_payload_size > ts->max_packet_size)
1470 pes->PES_packet_length = 0;
1471 else
1472 max_packet_size = pes_payload_size;
1473 }
1474
1475 if (pes->data_index > 0 &&
1476 pes->data_index + buf_size > max_packet_size) {
1477 ret = new_pes_packet(pes, ts->pkt);
1478 if (ret < 0)
1479 return ret;
1480 pes->PES_packet_length = 0;
1481 max_packet_size = ts->max_packet_size;
1482 ts->stop_parse = 1;
1483 } else if (pes->data_index == 0 &&
1484 buf_size > max_packet_size) {
1485 // pes packet size is < ts size packet and pes data is padded with STUFFING_BYTE
1486 // not sure if this is legal in ts but see issue #2392
1487 buf_size = max_packet_size;
1488 }
1489
1490 if (!pes->buffer) {
1491 pes->buffer = buffer_pool_get(ts, max_packet_size);
1492 if (!pes->buffer)
1493 return AVERROR(ENOMEM);
1494 }
1495
1496 memcpy(pes->buffer->data + pes->data_index, p, buf_size);
1497 pes->data_index += buf_size;
1498 /* emit complete packets with known packet size
1499 * decreases demuxer delay for infrequent packets like subtitles from
1500 * a couple of seconds to milliseconds for properly muxed files. */
1501 if (!ts->stop_parse && pes->PES_packet_length &&
1503 ts->stop_parse = 1;
1504 ret = new_pes_packet(pes, ts->pkt);
1505 pes->state = MPEGTS_SKIP;
1506 if (ret < 0)
1507 return ret;
1508 }
1509 } while (0);
1510 buf_size = 0;
1511 break;
1512 case MPEGTS_SKIP:
1513 buf_size = 0;
1514 break;
1515 }
1516 }
1517
1518 return 0;
1519}
1520
1521static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
1522{
1523 MpegTSFilter *tss;
1524 PESContext *pes;
1525
1526 /* if no pid found, then add a pid context */
1527 pes = av_mallocz(sizeof(PESContext));
1528 if (!pes)
1529 return 0;
1530 pes->ts = ts;
1531 pes->stream = ts->stream;
1532 pes->pid = pid;
1533 pes->pcr_pid = pcr_pid;
1534 pes->state = MPEGTS_SKIP;
1535 pes->pts = AV_NOPTS_VALUE;
1536 pes->dts = AV_NOPTS_VALUE;
1537 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1538 if (!tss) {
1539 av_free(pes);
1540 return 0;
1541 }
1542 return pes;
1543}
1544
1545#define MAX_LEVEL 4
1556
1558 const uint8_t *buf, unsigned size,
1559 Mp4Descr *descr, int max_descr_count)
1560{
1561 if (size > (1 << 30))
1562 return AVERROR_INVALIDDATA;
1563
1564 ffio_init_read_context(&d->pb, buf, size);
1565
1566 d->s = s;
1567 d->level = 0;
1568 d->descr_count = 0;
1569 d->descr = descr;
1570 d->active_descr = NULL;
1571 d->max_descr_count = max_descr_count;
1572
1573 return 0;
1574}
1575
1576static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1577{
1578 int64_t new_off = avio_tell(pb);
1579 (*len) -= new_off - *off;
1580 *off = new_off;
1581}
1582
1583static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1584 int target_tag);
1585
1587{
1588 while (len > 0) {
1589 int ret = parse_mp4_descr(d, off, len, 0);
1590 if (ret < 0)
1591 return ret;
1592 update_offsets(&d->pb.pub, &off, &len);
1593 }
1594 return 0;
1595}
1596
1598{
1599 AVIOContext *const pb = &d->pb.pub;
1600 avio_rb16(pb); // ID
1601 avio_r8(pb);
1602 avio_r8(pb);
1603 avio_r8(pb);
1604 avio_r8(pb);
1605 avio_r8(pb);
1606 update_offsets(pb, &off, &len);
1607 return parse_mp4_descr_arr(d, off, len);
1608}
1609
1611{
1612 int id_flags;
1613 if (len < 2)
1614 return 0;
1615 id_flags = avio_rb16(&d->pb.pub);
1616 if (!(id_flags & 0x0020)) { // URL_Flag
1617 update_offsets(&d->pb.pub, &off, &len);
1618 return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1619 } else {
1620 return 0;
1621 }
1622}
1623
1625{
1626 AVIOContext *const pb = &d->pb.pub;
1627 int es_id = 0;
1628 int ret = 0;
1629
1630 if (d->descr_count >= d->max_descr_count)
1631 return AVERROR_INVALIDDATA;
1632 ff_mp4_parse_es_descr(pb, &es_id);
1633 d->active_descr = d->descr + (d->descr_count++);
1634
1635 d->active_descr->es_id = es_id;
1636 update_offsets(pb, &off, &len);
1637 if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
1638 return ret;
1639 update_offsets(pb, &off, &len);
1640 if (len > 0)
1641 ret = parse_mp4_descr(d, off, len, MP4SLDescrTag);
1642 d->active_descr = NULL;
1643 return ret;
1644}
1645
1647 int len)
1648{
1649 Mp4Descr *descr = d->active_descr;
1650 if (!descr)
1651 return AVERROR_INVALIDDATA;
1653 if (!descr->dec_config_descr)
1654 return AVERROR(ENOMEM);
1655 descr->dec_config_descr_len = len;
1656 avio_read(&d->pb.pub, descr->dec_config_descr, len);
1657 return 0;
1658}
1659
1661{
1662 Mp4Descr *descr = d->active_descr;
1663 AVIOContext *const pb = &d->pb.pub;
1664 int predefined;
1665 if (!descr)
1666 return AVERROR_INVALIDDATA;
1667
1668#define R8_CHECK_CLIP_MAX(dst, maxv) do { \
1669 descr->sl.dst = avio_r8(pb); \
1670 if (descr->sl.dst > maxv) { \
1671 descr->sl.dst = maxv; \
1672 return AVERROR_INVALIDDATA; \
1673 } \
1674} while (0)
1675
1676 predefined = avio_r8(pb);
1677 if (!predefined) {
1678 int lengths;
1679 int flags = avio_r8(pb);
1680 descr->sl.use_au_start = !!(flags & 0x80);
1681 descr->sl.use_au_end = !!(flags & 0x40);
1682 descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1683 descr->sl.use_padding = !!(flags & 0x08);
1684 descr->sl.use_timestamps = !!(flags & 0x04);
1685 descr->sl.use_idle = !!(flags & 0x02);
1686 descr->sl.timestamp_res = avio_rb32(pb);
1687 avio_rb32(pb);
1688 R8_CHECK_CLIP_MAX(timestamp_len, 63);
1689 R8_CHECK_CLIP_MAX(ocr_len, 63);
1690 R8_CHECK_CLIP_MAX(au_len, 31);
1691 descr->sl.inst_bitrate_len = avio_r8(pb);
1692 lengths = avio_rb16(pb);
1693 descr->sl.degr_prior_len = lengths >> 12;
1694 descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1695 descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1696 } else if (!d->predefined_SLConfigDescriptor_seen){
1697 avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1699 }
1700 return 0;
1701}
1702
1704 int target_tag)
1705{
1706 int tag;
1707 AVIOContext *const pb = &d->pb.pub;
1708 int len1 = ff_mp4_read_descr(d->s, pb, &tag);
1709 int ret = 0;
1710
1711 update_offsets(pb, &off, &len);
1712 if (len < 0 || len1 > len || len1 <= 0) {
1713 av_log(d->s, AV_LOG_ERROR,
1714 "Tag %x length violation new length %d bytes remaining %d\n",
1715 tag, len1, len);
1716 return AVERROR_INVALIDDATA;
1717 }
1718
1719 if (d->level++ >= MAX_LEVEL) {
1720 av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1721 ret = AVERROR_INVALIDDATA;
1722 goto done;
1723 }
1724
1725 if (target_tag && tag != target_tag) {
1726 av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1727 target_tag);
1728 ret = AVERROR_INVALIDDATA;
1729 goto done;
1730 }
1731
1732 switch (tag) {
1733 case MP4IODescrTag:
1734 ret = parse_MP4IODescrTag(d, off, len1);
1735 break;
1736 case MP4ODescrTag:
1737 ret = parse_MP4ODescrTag(d, off, len1);
1738 break;
1739 case MP4ESDescrTag:
1740 ret = parse_MP4ESDescrTag(d, off, len1);
1741 break;
1743 ret = parse_MP4DecConfigDescrTag(d, off, len1);
1744 break;
1745 case MP4SLDescrTag:
1746 ret = parse_MP4SLDescrTag(d, off, len1);
1747 break;
1748 }
1749
1750
1751done:
1752 d->level--;
1753 avio_seek(pb, off + len1, SEEK_SET);
1754 return ret;
1755}
1756
1757static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1758 Mp4Descr *descr, int *descr_count, int max_descr_count)
1759{
1761 int ret;
1762
1764
1765 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1766 if (ret < 0)
1767 return ret;
1768
1770
1771 *descr_count += d.descr_count;
1772 return ret;
1773}
1774
1775static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1776 Mp4Descr *descr, int *descr_count, int max_descr_count)
1777{
1779 int ret;
1780
1781 ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1782 if (ret < 0)
1783 return ret;
1784
1785 ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb.pub), size);
1786
1787 *descr_count = d.descr_count;
1788 return ret;
1789}
1790
1791static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
1792 int section_len)
1793{
1794 MpegTSContext *ts = filter->u.section_filter.opaque;
1795 MpegTSSectionFilter *tssf = &filter->u.section_filter;
1797 const uint8_t *p, *p_end;
1798 int mp4_descr_count = 0;
1799 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1800 int i, pid;
1801 AVFormatContext *s = ts->stream;
1802
1803 p_end = section + section_len - 4;
1804 p = section;
1805 if (parse_section_header(&h, &p, p_end) < 0)
1806 return;
1807 if (h.tid != M4OD_TID)
1808 return;
1809 if (skip_identical(&h, tssf))
1810 return;
1811
1812 mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1814
1815 for (pid = 0; pid < NB_PID_MAX; pid++) {
1816 if (!ts->pids[pid])
1817 continue;
1818 for (i = 0; i < mp4_descr_count; i++) {
1819 PESContext *pes;
1820 AVStream *st;
1821 FFStream *sti;
1822 FFIOContext pb;
1823 if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1824 continue;
1825 if (ts->pids[pid]->type != MPEGTS_PES) {
1826 av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1827 continue;
1828 }
1829 pes = ts->pids[pid]->u.pes_filter.opaque;
1830 st = pes->st;
1831 if (!st)
1832 continue;
1833 sti = ffstream(st);
1834
1835 pes->sl = mp4_descr[i].sl;
1836
1837 ffio_init_read_context(&pb, mp4_descr[i].dec_config_descr,
1838 mp4_descr[i].dec_config_descr_len);
1840 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1841 st->codecpar->extradata_size > 0)
1842 sti->need_parsing = 0;
1843 if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
1844 st->codecpar->extradata_size > 0)
1845 sti->need_parsing = 0;
1846
1848 sti->need_context_update = 1;
1849 }
1850 }
1851 for (i = 0; i < mp4_descr_count; i++)
1852 av_free(mp4_descr[i].dec_config_descr);
1853}
1854
1855static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section,
1856 int section_len)
1857{
1858 AVProgram *prg = NULL;
1859 MpegTSContext *ts = filter->u.section_filter.opaque;
1860
1861 int idx = ff_find_stream_index(ts->stream, filter->pid);
1862 if (idx < 0)
1863 return;
1864
1865 /**
1866 * In case we receive an SCTE-35 packet before mpegts context is fully
1867 * initialized.
1868 */
1869 if (!ts->pkt)
1870 return;
1871
1872 new_data_packet(section, section_len, ts->pkt);
1873 ts->pkt->stream_index = idx;
1874 prg = av_find_program_from_stream(ts->stream, NULL, idx);
1875 if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) {
1876 MpegTSFilter *f = ts->pids[prg->pcr_pid];
1877 if (f && f->last_pcr != -1)
1878 ts->pkt->pts = ts->pkt->dts = f->last_pcr/SYSTEM_CLOCK_FREQUENCY_DIVISOR;
1879 }
1880 ts->stop_parse = 1;
1881
1882}
1883
1884static const uint8_t opus_coupled_stream_cnt[9] = {
1885 1, 0, 1, 1, 2, 2, 2, 3, 3
1886};
1887
1888static const uint8_t opus_stream_cnt[9] = {
1889 1, 1, 1, 2, 2, 3, 4, 4, 5,
1890};
1891
1892static const uint8_t opus_channel_map[8][8] = {
1893 { 0 },
1894 { 0,1 },
1895 { 0,2,1 },
1896 { 0,1,2,3 },
1897 { 0,4,1,2,3 },
1898 { 0,4,1,2,3,5 },
1899 { 0,4,1,2,3,5,6 },
1900 { 0,6,1,2,3,4,5,7 },
1901};
1902
1904 const uint8_t **pp, const uint8_t *desc_end,
1905 MpegTSContext *ts)
1906{
1907 int ext_tag = get8(pp, desc_end);
1908
1909 switch (ext_tag) {
1910 case JXS_VIDEO_DESCRIPTOR: /* JPEG-XS video descriptor*/
1911 {
1912 int horizontal_size, vertical_size, schar;
1913 int colour_primaries, transfer_characteristics, matrix_coefficients, video_full_range_flag;
1914 int descriptor_version, interlace_mode, n_fields;
1915 unsigned frat;
1916
1917 if (desc_end - *pp < 29)
1918 return AVERROR_INVALIDDATA;
1919
1920 descriptor_version = get8(pp, desc_end);
1921 if (descriptor_version) {
1922 av_log(fc, AV_LOG_WARNING, "Unsupported JPEG-XS descriptor version (%d != 0)", descriptor_version);
1923 return AVERROR_INVALIDDATA;
1924 }
1925
1926 horizontal_size = get16(pp, desc_end);
1927 vertical_size = get16(pp, desc_end);
1928 *pp += 4; /* brat */
1929 frat = bytestream_get_be32(pp);
1930 schar = get16(pp, desc_end);
1931 *pp += 2; /* Ppih */
1932 *pp += 2; /* Plev */
1933 *pp += 4; /* max_buffer_size */
1934 *pp += 1; /* buffer_model_type */
1935 colour_primaries = get8(pp, desc_end);
1936 transfer_characteristics = get8(pp, desc_end);
1937 matrix_coefficients = get8(pp, desc_end);
1938 video_full_range_flag = (get8(pp, desc_end) & 0x80) == 0x80 ? 1 : 0;
1939
1940 interlace_mode = (frat >> 30) & 0x3;
1941 if (interlace_mode == 3) {
1942 av_log(fc, AV_LOG_WARNING, "Unknown JPEG XS interlace mode 3");
1943 return AVERROR_INVALIDDATA;
1944 }
1945
1946 st->codecpar->field_order = interlace_mode == 0 ? AV_FIELD_PROGRESSIVE
1947 : (interlace_mode == 1 ? AV_FIELD_TT : AV_FIELD_BB);
1948 n_fields = st->codecpar->field_order == AV_FIELD_PROGRESSIVE ? 1 : 2;
1949
1950 st->codecpar->width = horizontal_size;
1951 st->codecpar->height = vertical_size * n_fields;
1952
1953 if (frat != 0) {
1954 int framerate_num = (frat & 0x0000FFFFU);
1955 int framerate_den = ((frat >> 24) & 0x0000003FU);
1956
1957 if (framerate_den == 2) {
1958 framerate_num *= 1000;
1959 framerate_den = 1001;
1960 } else if (framerate_den != 1) {
1961 av_log(fc, AV_LOG_WARNING, "Unknown JPEG XS framerate denominator code %u", framerate_den);
1962 return AVERROR_INVALIDDATA;
1963 }
1964
1965 st->codecpar->framerate.num = framerate_num;
1966 st->codecpar->framerate.den = framerate_den;
1967 }
1968
1969 switch (schar & 0xf) {
1970 case 0: st->codecpar->format = AV_PIX_FMT_YUV422P10LE; break;
1971 case 1: st->codecpar->format = AV_PIX_FMT_YUV444P10LE; break;
1972 default:
1973 av_log(fc, AV_LOG_WARNING, "Unknown JPEG XS sampling format");
1974 break;
1975 }
1976
1977 st->codecpar->color_range = video_full_range_flag ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
1978 st->codecpar->color_primaries = colour_primaries;
1980 st->codecpar->color_space = matrix_coefficients;
1981 }
1982 break;
1984 {
1985 struct Program *p = get_program(ts, prg_id);
1986 struct StreamGroup *stg;
1987 int lcevc_stream_tag = get8(pp, desc_end);
1988 int i;
1989
1990 if (!p)
1991 return 0;
1992
1994 return AVERROR_INVALIDDATA;
1995
1996 for (i = 0; i < p->nb_stream_groups; i++) {
1997 stg = &p->stream_groups[i];
1999 continue;
2000 if (stg->id == lcevc_stream_tag)
2001 break;
2002 }
2003 if (i == p->nb_stream_groups) {
2004 if (p->nb_stream_groups == MAX_STREAMS_PER_PROGRAM)
2005 return AVERROR(EINVAL);
2006 p->nb_stream_groups++;
2007 }
2008
2009 stg = &p->stream_groups[i];
2010 stg->id = lcevc_stream_tag;
2012 for (i = 0; i < stg->nb_streams; i++) {
2014 break;
2015 }
2016 if (i == stg->nb_streams) {
2018 return AVERROR(EINVAL);
2019 stg->streams[stg->nb_streams++] = st;
2020 } else
2021 stg->streams[i] = st;
2022
2024 }
2025 break;
2027 {
2028 struct Program *p = get_program(ts, prg_id);
2029 int num_lcevc_stream_tags = get8(pp, desc_end);
2030
2031 if (!p)
2032 return 0;
2033
2035 return AVERROR_INVALIDDATA;
2036
2037 for (int i = 0; i < num_lcevc_stream_tags; i++) {
2038 struct StreamGroup *stg = NULL;
2039 int lcevc_stream_tag = get8(pp, desc_end);;
2040 int j;
2041
2042 for (j = 0; j < p->nb_stream_groups; j++) {
2043 stg = &p->stream_groups[j];
2045 continue;
2046 if (stg->id == lcevc_stream_tag)
2047 break;
2048 }
2049 if (j == p->nb_stream_groups) {
2050 if (p->nb_stream_groups == MAX_STREAMS_PER_PROGRAM)
2051 return AVERROR(EINVAL);
2052 p->nb_stream_groups++;
2053 }
2054
2055 stg = &p->stream_groups[j];
2056 stg->id = lcevc_stream_tag;
2058 for (j = 0; j < stg->nb_streams; j++) {
2059 if (stg->streams[j]->index == st->index)
2060 break;
2061 }
2062 if (j == stg->nb_streams) {
2064 return AVERROR(EINVAL);
2065 stg->streams[stg->nb_streams++] = st;
2066 }
2067 }
2068 }
2069 break;
2070 default:
2071 break;
2072 }
2073
2074 return 0;
2075}
2076
2077int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, int prg_id,
2078 const uint8_t **pp, const uint8_t *desc_list_end,
2079 Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
2080 MpegTSContext *ts)
2081{
2082 FFStream *const sti = ffstream(st);
2083 const uint8_t *desc_end;
2084 int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
2085 char language[252];
2086 int i;
2087
2088 desc_tag = get8(pp, desc_list_end);
2089 if (desc_tag < 0)
2090 return AVERROR_INVALIDDATA;
2091 desc_len = get8(pp, desc_list_end);
2092 if (desc_len < 0)
2093 return AVERROR_INVALIDDATA;
2094 desc_end = *pp + desc_len;
2095 if (desc_end > desc_list_end)
2096 return AVERROR_INVALIDDATA;
2097
2098 av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
2099
2100 if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) &&
2101 stream_type == STREAM_TYPE_PRIVATE_DATA)
2102 mpegts_find_stream_type(st, desc_tag, DESC_types);
2103
2104 switch (desc_tag) {
2106 if (get8(pp, desc_end) & 0x1) {
2108 }
2109 break;
2110 case SL_DESCRIPTOR:
2111 desc_es_id = get16(pp, desc_end);
2112 if (desc_es_id < 0)
2113 break;
2114 if (ts && ts->pids[pid])
2115 ts->pids[pid]->es_id = desc_es_id;
2116 for (i = 0; i < mp4_descr_count; i++)
2117 if (mp4_descr[i].dec_config_descr_len &&
2118 mp4_descr[i].es_id == desc_es_id) {
2119 FFIOContext pb;
2120 ffio_init_read_context(&pb, mp4_descr[i].dec_config_descr,
2121 mp4_descr[i].dec_config_descr_len);
2123 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
2124 st->codecpar->extradata_size > 0) {
2125 sti->need_parsing = 0;
2126 sti->need_context_update = 1;
2127 }
2129 mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
2130 }
2131 break;
2132 case FMC_DESCRIPTOR:
2133 if (get16(pp, desc_end) < 0)
2134 break;
2135 if (mp4_descr_count > 0 &&
2137 (sti->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
2138 sti->request_probe > 0) &&
2139 mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
2140 FFIOContext pb;
2142 mp4_descr->dec_config_descr_len);
2144 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
2145 st->codecpar->extradata_size > 0) {
2146 sti->request_probe = sti->need_parsing = 0;
2148 sti->need_context_update = 1;
2149 }
2150 }
2151 break;
2153 {
2154 uint8_t *extradata = NULL;
2155 int language_count = desc_len / 5, ret;
2156
2157 if (desc_len > 0 && desc_len % 5 != 0)
2158 return AVERROR_INVALIDDATA;
2159
2160 if (language_count > 0) {
2161 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
2162 av_assert0(language_count <= sizeof(language) / 4);
2163
2164 if (st->codecpar->extradata == NULL) {
2165 ret = ff_alloc_extradata(st->codecpar, language_count * 2);
2166 if (ret < 0)
2167 return ret;
2168 }
2169
2170 if (st->codecpar->extradata_size < language_count * 2)
2171 return AVERROR_INVALIDDATA;
2172
2173 extradata = st->codecpar->extradata;
2174
2175 for (i = 0; i < language_count; i++) {
2176 language[i * 4 + 0] = get8(pp, desc_end);
2177 language[i * 4 + 1] = get8(pp, desc_end);
2178 language[i * 4 + 2] = get8(pp, desc_end);
2179 language[i * 4 + 3] = ',';
2180
2181 memcpy(extradata, *pp, 2);
2182 extradata += 2;
2183
2184 *pp += 2;
2185 }
2186
2187 language[i * 4 - 1] = 0;
2188 av_dict_set(&st->metadata, "language", language, 0);
2189 sti->need_context_update = 1;
2190 }
2191 }
2192 break;
2194 {
2195 /* 8 bytes per DVB subtitle substream data:
2196 * ISO_639_language_code (3 bytes),
2197 * subtitling_type (1 byte),
2198 * composition_page_id (2 bytes),
2199 * ancillary_page_id (2 bytes) */
2200 int language_count = desc_len / 8, ret;
2201
2202 if (desc_len > 0 && desc_len % 8 != 0)
2203 return AVERROR_INVALIDDATA;
2204
2205 if (language_count > 1) {
2206 avpriv_request_sample(fc, "DVB subtitles with multiple languages");
2207 }
2208
2209 if (language_count > 0) {
2210 uint8_t *extradata;
2211
2212 /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
2213 av_assert0(language_count <= sizeof(language) / 4);
2214
2215 if (st->codecpar->extradata == NULL) {
2216 ret = ff_alloc_extradata(st->codecpar, language_count * 5);
2217 if (ret < 0)
2218 return ret;
2219 }
2220
2221 if (st->codecpar->extradata_size < language_count * 5)
2222 return AVERROR_INVALIDDATA;
2223
2224 extradata = st->codecpar->extradata;
2225
2226 for (i = 0; i < language_count; i++) {
2227 language[i * 4 + 0] = get8(pp, desc_end);
2228 language[i * 4 + 1] = get8(pp, desc_end);
2229 language[i * 4 + 2] = get8(pp, desc_end);
2230 language[i * 4 + 3] = ',';
2231
2232 /* hearing impaired subtitles detection using subtitling_type */
2233 switch (*pp[0]) {
2234 case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
2235 case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
2236 case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
2237 case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
2238 case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
2239 case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
2241 break;
2242 }
2243
2244 extradata[4] = get8(pp, desc_end); /* subtitling_type */
2245 memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */
2246 extradata += 5;
2247
2248 *pp += 4;
2249 }
2250
2251 language[i * 4 - 1] = 0;
2252 av_dict_set(&st->metadata, "language", language, 0);
2253 sti->need_context_update = 1;
2254 }
2255 }
2256 break;
2258 for (i = 0; i + 4 <= desc_len; i += 4) {
2259 language[i + 0] = get8(pp, desc_end);
2260 language[i + 1] = get8(pp, desc_end);
2261 language[i + 2] = get8(pp, desc_end);
2262 language[i + 3] = ',';
2263 switch (get8(pp, desc_end)) {
2264 case 0x01:
2266 break;
2267 case 0x02:
2269 break;
2270 case 0x03:
2273 break;
2274 }
2275 }
2276 if (i && language[0]) {
2277 language[i - 1] = 0;
2278 /* don't overwrite language, as it may already have been set by
2279 * another, more specific descriptor (e.g. supplementary audio) */
2280 av_dict_set(&st->metadata, "language", language, AV_DICT_DONT_OVERWRITE);
2281 }
2282 break;
2284 st->codecpar->codec_tag = bytestream_get_le32(pp);
2285 av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
2286 if (st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) {
2288 if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D'))
2289 sti->request_probe = 50;
2290 }
2291 break;
2293 sti->stream_identifier = 1 + get8(pp, desc_end);
2294 break;
2296 if (get16(pp, desc_end) == 0xFFFF)
2297 *pp += 4;
2298 if (get8(pp, desc_end) == 0xFF) {
2299 st->codecpar->codec_tag = bytestream_get_le32(pp);
2302 }
2303 break;
2304 case DVB_EXTENSION_DESCRIPTOR: /* DVB extension descriptor */
2305 ext_desc_tag = get8(pp, desc_end);
2306 if (ext_desc_tag < 0)
2307 return AVERROR_INVALIDDATA;
2308 if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
2309 ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
2310 if (!st->codecpar->extradata) {
2313 if (!st->codecpar->extradata)
2314 return AVERROR(ENOMEM);
2315
2318
2319 channel_config_code = get8(pp, desc_end);
2320 if (channel_config_code < 0)
2321 return AVERROR_INVALIDDATA;
2322 if (channel_config_code <= 0x8) {
2323 st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
2324 AV_WL32(&st->codecpar->extradata[12], 48000);
2325 st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : /* Dual Mono */ 255;
2326 st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
2327 st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
2328 memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
2329 st->codecpar->extradata_size = st->codecpar->extradata[18] ? 21 + channels : 19;
2330 } else {
2331 avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
2332 }
2334 sti->need_context_update = 1;
2335 }
2336 break;
2337 }
2338 if (ext_desc_tag == SUPPLEMENTARY_AUDIO_DESCRIPTOR) {
2339 int flags;
2340
2341 if (desc_len < 1)
2342 return AVERROR_INVALIDDATA;
2343 flags = get8(pp, desc_end);
2344
2345 if ((flags & 0x80) == 0) /* mix_type */
2347
2348 switch ((flags >> 2) & 0x1F) { /* editorial_classification */
2349 case 0x01:
2352 break;
2353 case 0x02:
2355 break;
2356 case 0x03:
2358 break;
2359 }
2360
2361 if (flags & 0x01) { /* language_code_present */
2362 if (desc_len < 4)
2363 return AVERROR_INVALIDDATA;
2364 language[0] = get8(pp, desc_end);
2365 language[1] = get8(pp, desc_end);
2366 language[2] = get8(pp, desc_end);
2367 language[3] = 0;
2368
2369 /* This language always has to override a possible
2370 * ISO 639 language descriptor language */
2371 if (language[0])
2372 av_dict_set(&st->metadata, "language", language, 0);
2373 }
2374 break;
2375 }
2376 if (ext_desc_tag == AC4_DESCRIPTOR) {
2379 }
2380 break;
2381 case AC3_DESCRIPTOR:
2383 {
2384 int component_type_flag = get8(pp, desc_end) & (1 << 7);
2385 if (component_type_flag) {
2386 int component_type = get8(pp, desc_end);
2387 int service_type_mask = 0x38; // 0b00111000
2388 int service_type = ((component_type & service_type_mask) >> 3);
2389 if (service_type == 0x02 /* 0b010 */) {
2391 av_log(ts ? ts->stream : fc, AV_LOG_DEBUG, "New track disposition for id %u: %u\n", st->id, st->disposition);
2392 }
2393 }
2394 }
2395 break;
2397 // STD-B24, fascicle 3, chapter 4 defines private_stream_1
2398 // for captions
2399 if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
2400 // This structure is defined in STD-B10, part 1, listing 5.4 and
2401 // part 2, 6.2.20).
2402 // Listing of data_component_ids is in STD-B10, part 2, Annex J.
2403 // Component tag limits are documented in TR-B14, fascicle 2,
2404 // Vol. 3, Section 2, 4.2.8.1
2405 int actual_component_tag = sti->stream_identifier - 1;
2406 int picked_profile = AV_PROFILE_UNKNOWN;
2407 int data_component_id = get16(pp, desc_end);
2408 if (data_component_id < 0)
2409 return AVERROR_INVALIDDATA;
2410
2411 switch (data_component_id) {
2412 case 0x0008:
2413 // [0x30..0x37] are component tags utilized for
2414 // non-mobile captioning service ("profile A").
2415 if (actual_component_tag >= 0x30 &&
2416 actual_component_tag <= 0x37) {
2417 picked_profile = AV_PROFILE_ARIB_PROFILE_A;
2418 }
2419 break;
2420 case 0x0012:
2421 // component tag 0x87 signifies a mobile/partial reception
2422 // (1seg) captioning service ("profile C").
2423 if (actual_component_tag == 0x87) {
2424 picked_profile = AV_PROFILE_ARIB_PROFILE_C;
2425 }
2426 break;
2427 default:
2428 break;
2429 }
2430
2431 if (picked_profile == AV_PROFILE_UNKNOWN)
2432 break;
2433
2436 if (st->codecpar->profile != picked_profile) {
2437 st->codecpar->profile = picked_profile;
2438 sti->need_context_update = 1;
2439 }
2440 sti->request_probe = 0;
2441 sti->need_parsing = 0;
2442 }
2443 break;
2445 {
2446 uint32_t buf;
2448 size_t dovi_size;
2449 int dependency_pid = -1; // Unset
2450
2451 if (desc_end - *pp < 4) // (8 + 8 + 7 + 6 + 1 + 1 + 1) / 8
2452 return AVERROR_INVALIDDATA;
2453
2454 dovi = av_dovi_alloc(&dovi_size);
2455 if (!dovi)
2456 return AVERROR(ENOMEM);
2457
2458 dovi->dv_version_major = get8(pp, desc_end);
2459 dovi->dv_version_minor = get8(pp, desc_end);
2460 buf = get16(pp, desc_end);
2461 dovi->dv_profile = (buf >> 9) & 0x7f; // 7 bits
2462 dovi->dv_level = (buf >> 3) & 0x3f; // 6 bits
2463 dovi->rpu_present_flag = (buf >> 2) & 0x01; // 1 bit
2464 dovi->el_present_flag = (buf >> 1) & 0x01; // 1 bit
2465 dovi->bl_present_flag = buf & 0x01; // 1 bit
2466 if (!dovi->bl_present_flag && desc_end - *pp >= 2) {
2467 buf = get16(pp, desc_end);
2468 dependency_pid = buf >> 3; // 13 bits
2469 }
2470 if (desc_end - *pp >= 1) { // 8 bits
2471 buf = get8(pp, desc_end);
2472 dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
2473 dovi->dv_md_compression = (buf >> 2) & 0x03; // 2 bits
2474 } else {
2475 // 0 stands for None
2476 // Dolby Vision V1.2.93 profiles and levels
2479 }
2480
2484 (uint8_t *)dovi, dovi_size, 0)) {
2485 av_free(dovi);
2486 return AVERROR(ENOMEM);
2487 }
2488
2489 av_log(fc, AV_LOG_TRACE, "DOVI, version: %d.%d, profile: %d, level: %d, "
2490 "rpu flag: %d, el flag: %d, bl flag: %d, dependency_pid: %d, "
2491 "compatibility id: %d, compression: %d\n",
2493 dovi->dv_profile, dovi->dv_level,
2494 dovi->rpu_present_flag,
2495 dovi->el_present_flag,
2496 dovi->bl_present_flag,
2497 dependency_pid,
2499 dovi->dv_md_compression);
2500
2501 /* A Profile 7 dual-track EL stream points at its base layer via
2502 * the descriptor's dependency_pid. Record a pending group entry,
2503 * it will be resolved once all streams are available. */
2504 if (dovi->dv_profile == 7 && dovi->el_present_flag &&
2505 !dovi->bl_present_flag && dependency_pid >= 0) {
2506 struct Program *p = get_program(ts, prg_id);
2507 struct StreamGroup *stg;
2508 int gi;
2509
2510 if (!p)
2511 break;
2512
2513 for (gi = 0; gi < p->nb_stream_groups; gi++) {
2514 stg = &p->stream_groups[gi];
2516 stg->nb_streams && stg->streams[0] == st)
2517 break;
2518 }
2519 if (gi == p->nb_stream_groups) {
2520 if (p->nb_stream_groups == MAX_STREAMS_PER_PROGRAM)
2521 return AVERROR(EINVAL);
2522 p->nb_stream_groups++;
2523 stg = &p->stream_groups[gi];
2525 stg->id = st->id;
2526 stg->streams[stg->nb_streams++] = st;
2527 }
2528 stg->dep_pid = dependency_pid;
2529 }
2530 }
2531 break;
2532 case EXTENSION_DESCRIPTOR: /* descriptor extension */
2533 {
2534 int ret = parse_mpeg2_extension_descriptor(fc, st, prg_id, pp, desc_end, ts);
2535
2536 if (ret < 0)
2537 return ret;
2538 }
2539 break;
2540 default:
2541 break;
2542 }
2543 *pp = desc_end;
2544 return 0;
2545}
2546
2547static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid,
2548 int stream_identifier, int pmt_stream_idx, struct Program *p)
2549{
2550 AVFormatContext *s = ts->stream;
2551 AVStream *found = NULL;
2552
2553 if (stream_identifier) { /* match based on "stream identifier descriptor" if present */
2554 for (int i = 0; i < p->nb_streams; i++) {
2555 if (p->streams[i].stream_identifier == stream_identifier)
2556 if (!found || pmt_stream_idx == i) /* fallback to idx based guess if multiple streams have the same identifier */
2557 found = s->streams[p->streams[i].idx];
2558 }
2559 } else if (pmt_stream_idx < p->nb_streams) { /* match based on position within the PMT */
2560 found = s->streams[p->streams[pmt_stream_idx].idx];
2561 }
2562
2563 if (found) {
2565 "reusing existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
2567 found->index, found->id, pid);
2568 }
2569
2570 return found;
2571}
2572
2573static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
2574{
2575 const uint8_t **pp = &p;
2576 const uint8_t *desc_list_end;
2577 const uint8_t *desc_end;
2578 int desc_list_len;
2579 int desc_len, desc_tag;
2580
2581 desc_list_len = get16(pp, p_end);
2582 if (desc_list_len < 0)
2583 return -1;
2584 desc_list_len &= 0xfff;
2585 desc_list_end = p + desc_list_len;
2586 if (desc_list_end > p_end)
2587 return -1;
2588
2589 while (1) {
2590 desc_tag = get8(pp, desc_list_end);
2591 if (desc_tag < 0)
2592 return -1;
2593 desc_len = get8(pp, desc_list_end);
2594 if (desc_len < 0)
2595 return -1;
2596 desc_end = *pp + desc_len;
2597 if (desc_end > desc_list_end)
2598 return -1;
2599
2600 if (desc_tag == STREAM_IDENTIFIER_DESCRIPTOR) {
2601 return get8(pp, desc_end);
2602 }
2603 *pp = desc_end;
2604 }
2605
2606 return -1;
2607}
2608
2609static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
2610{
2611 switch (stream_type) {
2614 return 0;
2616 /* This User Private stream_type value is used by multiple organizations
2617 for different things. ANSI/SCTE 35 splice_info_section() is a
2618 private_section() not a PES_packet(). */
2619 return !(prog_reg_desc == AV_RL32("CUEI"));
2620 default:
2621 return 1;
2622 }
2623}
2624
2625/* UHD Blu-ray titles don't follow the Dolby Vision MPEG-TS spec [1] when
2626 * signaling a dual-PID Profile 7 stream. The enhancement-layer PID lacks the
2627 * DOVI_video_stream_descriptor, is advertised with stream_type 0x24 (HEVC)
2628 * instead of the spec-mandated 0x06, and uses the HDMV registration descriptor
2629 * instead of "DOVI". EL always has M2TS_VIDEO_EL_PID (0x1015) PID.
2630 * [1] <https://professionalsupport.dolby.com/s/article/How-to-signal-Dolby-Vision-in-MPEG-2-TS>
2631 */
2632static void detect_bdmv_dovi_group(MpegTSContext *ts, struct Program *prg,
2633 uint32_t prog_reg_desc)
2634{
2635 AVStream *bl_st = NULL, *el_st = NULL;
2636 struct StreamGroup *grp;
2637
2638 if (prog_reg_desc != AV_RL32("HDMV"))
2639 return;
2640
2642 return;
2643
2644 for (int i = 0; i < prg->nb_stream_groups; i++) {
2646 return;
2647 }
2648
2649 for (int j = 0; j < prg->nb_streams; j++) {
2650 int idx = prg->streams[j].idx;
2651 AVStream *st;
2652 if (idx < 0 || idx >= ts->stream->nb_streams)
2653 continue;
2654 st = ts->stream->streams[idx];
2656 continue;
2657 if (st->id == M2TS_VIDEO_PID)
2658 bl_st = st;
2659 else if (st->id == M2TS_VIDEO_EL_PID)
2660 el_st = st;
2661 }
2662
2663 if (!bl_st || !el_st)
2664 return;
2665
2666 grp = &prg->stream_groups[prg->nb_stream_groups++];
2668 grp->id = el_st->id;
2669 grp->streams[0] = bl_st;
2670 grp->streams[1] = el_st;
2671 grp->nb_streams = 2;
2672}
2673
2674static void create_stream_groups(MpegTSContext *ts, struct Program *prg)
2675{
2676 for (int i = 0; i < prg->nb_stream_groups; i++) {
2677 struct StreamGroup *grp = &prg->stream_groups[i];
2678 AVStreamGroup *stg;
2679 int j;
2680
2681 /* The DOVI descriptor on a Profile 7 EL points at its base layer
2682 * via dependency_pid. Resolve the BL now that all streams of the
2683 * program have been added. */
2684 if (grp->type == AV_STREAM_GROUP_PARAMS_DOLBY_VISION && grp->nb_streams == 1) {
2685 for (j = 0; j < prg->nb_streams; j++) {
2686 int idx = prg->streams[j].idx;
2687 AVStream *cand;
2688 if (idx < 0 || idx >= ts->stream->nb_streams)
2689 continue;
2690 cand = ts->stream->streams[idx];
2691 if (cand == grp->streams[0] || cand->id != grp->dep_pid)
2692 continue;
2693 grp->streams[1] = grp->streams[0];
2694 grp->streams[0] = cand;
2695 grp->nb_streams = 2;
2696 break;
2697 }
2698 }
2699
2700 if (grp->nb_streams < 2)
2701 continue;
2702 for (j = 0; j < ts->stream->nb_stream_groups; j++) {
2703 stg = ts->stream->stream_groups[j];
2704 if (stg->id == grp->id)
2705 break;
2706 }
2707 if (j == ts->stream->nb_stream_groups)
2708 stg = avformat_stream_group_create(ts->stream, grp->type, NULL);
2709 else
2710 continue;
2711 if (!stg)
2712 continue;
2713 stg->id = grp->id;
2714 for (int j = 0; j < grp->nb_streams; j++) {
2715 int ret = avformat_stream_group_add_stream(stg, grp->streams[j]);
2716 if (ret < 0) {
2718 continue;
2719 }
2720 switch (grp->type) {
2722 if (grp->streams[j]->codecpar->codec_id == AV_CODEC_ID_LCEVC)
2723 stg->params.layered_video->el_index = stg->nb_streams - 1;
2724 break;
2726 if (j == 0) {
2727 stg->params.layered_video->width = grp->streams[j]->codecpar->width;
2729 }
2730 if (j == grp->nb_streams - 1)
2731 stg->params.layered_video->el_index = stg->nb_streams - 1;
2732 break;
2733 default:
2734 av_unreachable("Invalid group type!");
2735 }
2736 }
2737 }
2738}
2739
2740static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2741{
2742 MpegTSContext *ts = filter->u.section_filter.opaque;
2743 MpegTSSectionFilter *tssf = &filter->u.section_filter;
2744 struct Program old_program;
2745 SectionHeader h1, *h = &h1;
2746 PESContext *pes;
2747 AVStream *st;
2748 const uint8_t *p, *p_end, *desc_list_end;
2749 int program_info_length, pcr_pid, pid, stream_type;
2750 int desc_list_len;
2751 uint32_t prog_reg_desc = 0; /* registration descriptor */
2752 int stream_identifier = -1;
2753 struct Program *prg;
2754
2755 int mp4_descr_count = 0;
2756 Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
2757 int i;
2758
2759 av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len);
2760 hex_dump_debug(ts->stream, section, section_len);
2761
2762 p_end = section + section_len - 4;
2763 p = section;
2764 if (parse_section_header(h, &p, p_end) < 0)
2765 return;
2766 if (h->tid != PMT_TID)
2767 return;
2768 if (!h->current_next)
2769 return;
2770 if (skip_identical(h, tssf))
2771 return;
2772
2773 av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d version=%d tid=%d\n",
2774 h->id, h->sec_num, h->last_sec_num, h->version, h->tid);
2775
2776 if (!ts->scan_all_pmts && ts->skip_changes)
2777 return;
2778
2779 prg = get_program(ts, h->id);
2780 if (prg)
2781 old_program = *prg;
2782 else
2783 clear_program(&old_program);
2784
2785 if (ts->skip_unknown_pmt && !prg)
2786 return;
2787 if (prg && prg->nb_pids && prg->pids[0] != ts->current_pid)
2788 return;
2789 if (!ts->skip_clear)
2790 clear_avprogram(ts, h->id);
2791 clear_program(prg);
2793
2794 pcr_pid = get16(&p, p_end);
2795 if (pcr_pid < 0)
2796 return;
2797 pcr_pid &= 0x1fff;
2798 add_pid_to_program(prg, pcr_pid);
2799 update_av_program_info(ts->stream, h->id, pcr_pid, h->version);
2800
2801 av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid);
2802
2803 program_info_length = get16(&p, p_end);
2804
2805 if (program_info_length < 0 || (program_info_length & 0xFFF) > p_end - p)
2806 return;
2807 program_info_length &= 0xfff;
2808 while (program_info_length >= 2) {
2809 uint8_t tag, len;
2810 tag = get8(&p, p_end);
2811 len = get8(&p, p_end);
2812
2813 av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len);
2814
2815 program_info_length -= 2;
2816 if (len > program_info_length)
2817 // something else is broken, exit the program_descriptors_loop
2818 break;
2819 program_info_length -= len;
2820 if (tag == IOD_DESCRIPTOR && len >= 2) {
2821 get8(&p, p_end); // scope
2822 get8(&p, p_end); // label
2823 len -= 2;
2824 mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
2825 &mp4_descr_count, MAX_MP4_DESCR_COUNT - mp4_descr_count);
2826 } else if (tag == REGISTRATION_DESCRIPTOR && len >= 4) {
2827 prog_reg_desc = bytestream_get_le32(&p);
2828 len -= 4;
2829 }
2830 p += len;
2831 }
2832 p += program_info_length;
2833 if (p >= p_end)
2834 goto out;
2835
2836 // stop parsing after pmt, we found header
2837 if (!ts->pkt)
2838 ts->stop_parse = 2;
2839
2840 if (prg)
2841 prg->pmt_found = 1;
2842
2843 for (i = 0; i < MAX_STREAMS_PER_PROGRAM; i++) {
2844 st = 0;
2845 pes = NULL;
2846 stream_type = get8(&p, p_end);
2847 if (stream_type < 0)
2848 break;
2849 pid = get16(&p, p_end);
2850 if (pid < 0)
2851 goto out;
2852 pid &= 0x1fff;
2853 if (pid == ts->current_pid)
2854 goto out;
2855
2856 stream_identifier = parse_stream_identifier_desc(p, p_end) + 1;
2857
2858 /* now create stream */
2859 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
2860 pes = ts->pids[pid]->u.pes_filter.opaque;
2861 if (ts->merge_pmt_versions && !pes->st) {
2862 st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2863 if (st) {
2864 pes->st = st;
2865 pes->stream_type = stream_type;
2866 pes->merged_st = 1;
2867 }
2868 }
2869 if (!pes->st) {
2870 pes->st = avformat_new_stream(pes->stream, NULL);
2871 if (!pes->st)
2872 goto out;
2873 pes->st->id = pes->pid;
2874 }
2875 st = pes->st;
2876 } else if (is_pes_stream(stream_type, prog_reg_desc)) {
2877 if (ts->pids[pid])
2878 mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
2879 pes = add_pes_stream(ts, pid, pcr_pid);
2880 if (ts->merge_pmt_versions && pes && !pes->st) {
2881 st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2882 if (st) {
2883 pes->st = st;
2884 pes->stream_type = stream_type;
2885 pes->merged_st = 1;
2886 }
2887 }
2888 if (pes && !pes->st) {
2889 st = avformat_new_stream(pes->stream, NULL);
2890 if (!st)
2891 goto out;
2892 st->id = pes->pid;
2893 }
2894 } else {
2895 int idx = ff_find_stream_index(ts->stream, pid);
2896 if (idx >= 0) {
2897 st = ts->stream->streams[idx];
2898 }
2899 if (ts->merge_pmt_versions && !st) {
2900 st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2901 }
2902 if (!st) {
2903 st = avformat_new_stream(ts->stream, NULL);
2904 if (!st)
2905 goto out;
2906 st->id = pid;
2908 if (stream_type == STREAM_TYPE_SCTE_DATA_SCTE_35 && prog_reg_desc == AV_RL32("CUEI")) {
2909 mpegts_find_stream_type(st, stream_type, SCTE_types);
2911 }
2912 }
2913 }
2914
2915 if (!st)
2916 goto out;
2917
2918 if (pes && pes->stream_type != stream_type)
2919 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
2920
2921 add_pid_to_program(prg, pid);
2922 if (prg) {
2923 prg->streams[i].idx = st->index;
2924 prg->streams[i].stream_identifier = stream_identifier;
2925 prg->nb_streams++;
2926 }
2927
2929
2930 desc_list_len = get16(&p, p_end);
2931 if (desc_list_len < 0)
2932 goto out;
2933 desc_list_len &= 0xfff;
2934 desc_list_end = p + desc_list_len;
2935 if (desc_list_end > p_end)
2936 goto out;
2937 for (;;) {
2938 if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, h->id, &p,
2939 desc_list_end, mp4_descr,
2940 mp4_descr_count, pid, ts) < 0)
2941 break;
2942
2943 if (pes && prog_reg_desc == AV_RL32("HDMV") &&
2944 stream_type == STREAM_TYPE_BLURAY_AUDIO_TRUEHD && pes->sub_st) {
2946 pes->sub_st->index);
2948 }
2949 }
2950 p = desc_list_end;
2951 }
2952
2953 if (!ts->pids[pcr_pid])
2954 mpegts_open_pcr_filter(ts, pcr_pid);
2955
2956out:
2957 if (prg) {
2958 detect_bdmv_dovi_group(ts, prg, prog_reg_desc);
2959 create_stream_groups(ts, prg);
2960 }
2961
2962 for (i = 0; i < mp4_descr_count; i++)
2963 av_free(mp4_descr[i].dec_config_descr);
2964}
2965
2966static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2967{
2968 MpegTSContext *ts = filter->u.section_filter.opaque;
2969 MpegTSSectionFilter *tssf = &filter->u.section_filter;
2970 SectionHeader h1, *h = &h1;
2971 const uint8_t *p, *p_end;
2972 int sid, pmt_pid;
2973 int nb_prg = 0;
2974 AVProgram *program;
2975
2976 av_log(ts->stream, AV_LOG_TRACE, "PAT:\n");
2977 hex_dump_debug(ts->stream, section, section_len);
2978
2979 p_end = section + section_len - 4;
2980 p = section;
2981 if (parse_section_header(h, &p, p_end) < 0)
2982 return;
2983 if (h->tid != PAT_TID)
2984 return;
2985 if (!h->current_next)
2986 return;
2987 if (ts->skip_changes)
2988 return;
2989
2990 if (skip_identical(h, tssf))
2991 return;
2992 ts->id = h->id;
2993
2994 for (;;) {
2995 sid = get16(&p, p_end);
2996 if (sid < 0)
2997 break;
2998 pmt_pid = get16(&p, p_end);
2999 if (pmt_pid < 0)
3000 break;
3001 pmt_pid &= 0x1fff;
3002
3003 if (pmt_pid <= 0x000F || pmt_pid == 0x1FFF) {
3005 "Ignoring invalid PAT entry: sid=0x%x pid=0x%x\n", sid, pmt_pid);
3006 continue;
3007 }
3008
3009 av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
3010
3011 if (sid == 0x0000) {
3012 /* NIT info */
3013 } else {
3014 MpegTSFilter *fil = ts->pids[pmt_pid];
3015 struct Program *prg;
3016 program = av_new_program(ts->stream, sid);
3017 if (program) {
3018 program->program_num = sid;
3019 program->pmt_pid = pmt_pid;
3020 }
3021 if (fil)
3022 if ( fil->type != MPEGTS_SECTION
3023 || fil->pid != pmt_pid
3024 || fil->u.section_filter.section_cb != pmt_cb)
3025 mpegts_close_filter(ts, ts->pids[pmt_pid]);
3026
3027 if (!ts->pids[pmt_pid])
3028 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
3029 prg = add_program(ts, sid);
3030 if (prg) {
3031 unsigned prg_idx = prg - ts->prg;
3032 if (prg->nb_pids && prg->pids[0] != pmt_pid)
3033 clear_program(prg);
3034 add_pid_to_program(prg, pmt_pid);
3035 if (prg_idx > nb_prg)
3036 FFSWAP(struct Program, ts->prg[nb_prg], ts->prg[prg_idx]);
3037 if (prg_idx >= nb_prg)
3038 nb_prg++;
3039 } else
3040 nb_prg = 0;
3041 }
3042 }
3043 ts->nb_prg = nb_prg;
3044
3045 if (sid < 0) {
3046 int i,j;
3047 for (j=0; j<ts->stream->nb_programs; j++) {
3048 for (i = 0; i < ts->nb_prg; i++)
3049 if (ts->prg[i].id == ts->stream->programs[j]->id)
3050 break;
3051 if (i==ts->nb_prg && !ts->skip_clear)
3052 clear_avprogram(ts, ts->stream->programs[j]->id);
3053 }
3054 }
3055}
3056
3057static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
3058{
3059 MpegTSContext *ts = filter->u.section_filter.opaque;
3060 const uint8_t *p, *p_end;
3061 SectionHeader h1, *h = &h1;
3062
3063 /*
3064 * Sometimes we receive EPG packets but SDT table do not have
3065 * eit_pres_following or eit_sched turned on, so we open EPG
3066 * stream directly here.
3067 */
3068 if (!ts->epg_stream) {
3070 if (!ts->epg_stream)
3071 return;
3072 ts->epg_stream->id = EIT_PID;
3075 }
3076
3077 if (ts->epg_stream->discard == AVDISCARD_ALL)
3078 return;
3079
3080 p_end = section + section_len - 4;
3081 p = section;
3082
3083 if (parse_section_header(h, &p, p_end) < 0)
3084 return;
3085 if (h->tid < EIT_TID || h->tid > OEITS_END_TID)
3086 return;
3087
3088 av_log(ts->stream, AV_LOG_TRACE, "EIT: tid received = %.02x\n", h->tid);
3089
3090 /**
3091 * Service_id 0xFFFF is reserved, it indicates that the current EIT table
3092 * is scrambled.
3093 */
3094 if (h->id == 0xFFFF) {
3095 av_log(ts->stream, AV_LOG_TRACE, "Scrambled EIT table received.\n");
3096 return;
3097 }
3098
3099 /**
3100 * In case we receive an EPG packet before mpegts context is fully
3101 * initialized.
3102 */
3103 if (!ts->pkt)
3104 return;
3105
3106 new_data_packet(section, section_len, ts->pkt);
3107 ts->pkt->stream_index = ts->epg_stream->index;
3108 ts->stop_parse = 1;
3109}
3110
3111static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
3112{
3113 MpegTSContext *ts = filter->u.section_filter.opaque;
3114 MpegTSSectionFilter *tssf = &filter->u.section_filter;
3115 SectionHeader h1, *h = &h1;
3116 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
3117 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
3118 char *name, *provider_name;
3119
3120 av_log(ts->stream, AV_LOG_TRACE, "SDT:\n");
3121 hex_dump_debug(ts->stream, section, section_len);
3122
3123 p_end = section + section_len - 4;
3124 p = section;
3125 if (parse_section_header(h, &p, p_end) < 0)
3126 return;
3127 if (h->tid != SDT_TID)
3128 return;
3129 if (!h->current_next)
3130 return;
3131 if (ts->skip_changes)
3132 return;
3133 if (skip_identical(h, tssf))
3134 return;
3135
3136 onid = get16(&p, p_end);
3137 if (onid < 0)
3138 return;
3139 val = get8(&p, p_end);
3140 if (val < 0)
3141 return;
3142 for (;;) {
3143 sid = get16(&p, p_end);
3144 if (sid < 0)
3145 break;
3146 val = get8(&p, p_end);
3147 if (val < 0)
3148 break;
3149 desc_list_len = get16(&p, p_end);
3150 if (desc_list_len < 0)
3151 break;
3152 desc_list_len &= 0xfff;
3153 desc_list_end = p + desc_list_len;
3154 if (desc_list_end > p_end)
3155 break;
3156 for (;;) {
3157 desc_tag = get8(&p, desc_list_end);
3158 if (desc_tag < 0)
3159 break;
3160 desc_len = get8(&p, desc_list_end);
3161 desc_end = p + desc_len;
3162 if (desc_len < 0 || desc_end > desc_list_end)
3163 break;
3164
3165 av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n",
3166 desc_tag, desc_len);
3167
3168 switch (desc_tag) {
3169 case SERVICE_DESCRIPTOR:
3170 service_type = get8(&p, desc_end);
3171 if (service_type < 0)
3172 break;
3173 provider_name = getstr8(&p, desc_end);
3174 if (!provider_name)
3175 break;
3176 name = getstr8(&p, desc_end);
3177 if (name) {
3178 AVProgram *program = av_new_program(ts->stream, sid);
3179 if (program) {
3180 av_dict_set(&program->metadata, "service_name", name, 0);
3181 av_dict_set(&program->metadata, "service_provider",
3182 provider_name, 0);
3183 }
3184 }
3185 av_free(name);
3186 av_free(provider_name);
3187 break;
3188 default:
3189 break;
3190 }
3191 p = desc_end;
3192 }
3193 p = desc_list_end;
3194 }
3195}
3196
3197static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
3198 const uint8_t *packet);
3199
3200/* handle one TS packet */
3201static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
3202{
3203 MpegTSFilter *tss;
3204 int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
3205 has_adaptation, has_payload;
3206 const uint8_t *p, *p_end;
3207
3208 pid = AV_RB16(packet + 1) & 0x1fff;
3209 is_start = packet[1] & 0x40;
3210 tss = ts->pids[pid];
3211 if (ts->auto_guess && !tss && is_start) {
3212 add_pes_stream(ts, pid, -1);
3213 tss = ts->pids[pid];
3214 }
3215 if (!tss)
3216 return 0;
3217 if (is_start)
3218 tss->discard = discard_pid(ts, pid);
3219 if (tss->discard)
3220 return 0;
3221 ts->current_pid = pid;
3222
3223 afc = (packet[3] >> 4) & 3;
3224 if (afc == 0) /* reserved value */
3225 return 0;
3226 has_adaptation = afc & 2;
3227 has_payload = afc & 1;
3228 is_discontinuity = has_adaptation &&
3229 packet[4] != 0 && /* with length > 0 */
3230 (packet[5] & 0x80); /* and discontinuity indicated */
3231
3232 /* continuity check (currently not used) */
3233 cc = (packet[3] & 0xf);
3234 expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
3235 cc_ok = pid == NULL_PID ||
3236 is_discontinuity ||
3237 tss->last_cc < 0 ||
3238 expected_cc == cc;
3239
3240 tss->last_cc = cc;
3241 if (!cc_ok) {
3243 "Continuity check failed for pid %d expected %d got %d\n",
3244 pid, expected_cc, cc);
3245 if (tss->type == MPEGTS_PES) {
3246 PESContext *pc = tss->u.pes_filter.opaque;
3248 }
3249 }
3250
3251 if (packet[1] & 0x80) {
3252 av_log(ts->stream, AV_LOG_DEBUG, "Packet had TEI flag set; marking as corrupt\n");
3253 if (tss->type == MPEGTS_PES) {
3254 PESContext *pc = tss->u.pes_filter.opaque;
3256 }
3257 }
3258
3259 p = packet + 4;
3260 if (has_adaptation) {
3261 int64_t pcr_h;
3262 int pcr_l;
3263 if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
3264 tss->last_pcr = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
3265 /* skip adaptation field */
3266 p += p[0] + 1;
3267 }
3268 /* if past the end of packet, ignore */
3269 p_end = packet + TS_PACKET_SIZE;
3270 if (p >= p_end || !has_payload)
3271 return 0;
3272
3273 if (pos >= 0) {
3276 }
3277
3278 if (tss->type == MPEGTS_SECTION) {
3279 if (is_start) {
3280 /* pointer field present */
3281 len = *p++;
3282 if (len > p_end - p)
3283 return 0;
3284 if (len && cc_ok) {
3285 /* write remaining section bytes */
3286 write_section_data(ts, tss,
3287 p, len, 0);
3288 /* check whether filter has been closed */
3289 if (!ts->pids[pid])
3290 return 0;
3291 }
3292 p += len;
3293 if (p < p_end) {
3294 write_section_data(ts, tss,
3295 p, p_end - p, 1);
3296 }
3297 } else {
3298 if (cc_ok) {
3299 write_section_data(ts, tss,
3300 p, p_end - p, 0);
3301 }
3302 }
3303
3304 // stop find_stream_info from waiting for more streams
3305 // when all programs have received a PMT
3306 if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
3307 int i;
3308 for (i = 0; i < ts->nb_prg; i++) {
3309 if (!ts->prg[i].pmt_found)
3310 break;
3311 }
3312 if (i == ts->nb_prg && ts->nb_prg > 0) {
3313 av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
3315 }
3316 }
3317
3318 } else {
3319 int ret;
3320 // Note: The position here points actually behind the current packet.
3321 if (tss->type == MPEGTS_PES) {
3322 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
3323 pos - ts->raw_packet_size)) < 0)
3324 return ret;
3325 }
3326 }
3327
3328 return 0;
3329}
3330
3331static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet)
3332{
3333 MpegTSContext *ts = s->priv_data;
3334 AVIOContext *pb = s->pb;
3335 int c, i;
3336 uint64_t pos = avio_tell(pb);
3337 int64_t back = FFMIN(seekback, pos);
3338
3339 //Special case for files like 01c56b0dc1.ts
3340 if (current_packet[0] == 0x80 && current_packet[12] == SYNC_BYTE && pos >= TS_PACKET_SIZE) {
3341 avio_seek(pb, 12 - TS_PACKET_SIZE, SEEK_CUR);
3342 return 0;
3343 }
3344
3345 avio_seek(pb, -back, SEEK_CUR);
3346
3347 for (i = 0; i < ts->resync_size; i++) {
3348 c = avio_r8(pb);
3349 if (avio_feof(pb))
3350 return AVERROR_EOF;
3351 if (c == SYNC_BYTE) {
3352 int new_packet_size, ret;
3353 avio_seek(pb, -1, SEEK_CUR);
3354 pos = avio_tell(pb);
3356 if (ret < 0)
3357 return ret;
3358 new_packet_size = get_packet_size(s);
3359 if (new_packet_size > 0 && new_packet_size != ts->raw_packet_size) {
3360 av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", new_packet_size);
3361 ts->raw_packet_size = new_packet_size;
3362 }
3363 avio_seek(pb, pos, SEEK_SET);
3364 return 0;
3365 }
3366 }
3368 "max resync size reached, could not find sync byte\n");
3369 /* no sync found */
3370 return AVERROR_INVALIDDATA;
3371}
3372
3373/* return AVERROR_something if error or EOF. Return 0 if OK. */
3374static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
3375 const uint8_t **data)
3376{
3377 AVIOContext *pb = s->pb;
3378 int len;
3379
3380 // 192 bytes source packet that start with a 4 bytes TP_extra_header
3381 // followed by 188 bytes of TS packet. The sync byte is at offset 4, so skip
3382 // the first 4 bytes otherwise we'll end up syncing to the wrong packet.
3383 if (raw_packet_size == TS_DVHS_PACKET_SIZE)
3384 avio_skip(pb, 4);
3385
3386 for (;;) {
3388 if (len != TS_PACKET_SIZE)
3389 return len < 0 ? len : AVERROR_EOF;
3390 /* check packet sync byte */
3391 if ((*data)[0] != SYNC_BYTE) {
3392 /* find a new packet start */
3393
3394 if (mpegts_resync(s, raw_packet_size, *data) < 0)
3395 return AVERROR(EAGAIN);
3396 else
3397 continue;
3398 } else {
3399 break;
3400 }
3401 }
3402 return 0;
3403}
3404
3405static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
3406{
3407 AVIOContext *pb = s->pb;
3408 int skip;
3409 if (raw_packet_size == TS_DVHS_PACKET_SIZE)
3410 skip = raw_packet_size - TS_DVHS_PACKET_SIZE;
3411 else
3412 skip = raw_packet_size - TS_PACKET_SIZE;
3413 if (skip > 0)
3414 avio_skip(pb, skip);
3415}
3416
3417static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
3418{
3419 AVFormatContext *s = ts->stream;
3421 const uint8_t *data;
3422 int64_t packet_num;
3423 int ret = 0;
3424
3425 if (avio_tell(s->pb) != ts->last_pos) {
3426 int i;
3427 av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n");
3428 /* seek detected, flush pes buffer */
3429 for (i = 0; i < NB_PID_MAX; i++) {
3430 if (ts->pids[i]) {
3431 if (ts->pids[i]->type == MPEGTS_PES) {
3432 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
3433 av_buffer_unref(&pes->buffer);
3434 pes->data_index = 0;
3435 pes->state = MPEGTS_SKIP; /* skip until pes header */
3436 } else if (ts->pids[i]->type == MPEGTS_SECTION) {
3437 ts->pids[i]->u.section_filter.last_ver = -1;
3438 }
3439 ts->pids[i]->last_cc = -1;
3440 ts->pids[i]->last_pcr = -1;
3441 }
3442 }
3443 }
3444
3445 ts->stop_parse = 0;
3446 packet_num = 0;
3447 memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
3448 for (;;) {
3449 packet_num++;
3450 if (nb_packets != 0 && packet_num >= nb_packets ||
3451 ts->stop_parse > 1) {
3452 ret = AVERROR(EAGAIN);
3453 break;
3454 }
3455 if (ts->stop_parse > 0)
3456 break;
3457
3458 ret = read_packet(s, packet, ts->raw_packet_size, &data);
3459 if (ret != 0)
3460 break;
3461 ret = handle_packet(ts, data, avio_tell(s->pb));
3463 if (ret != 0)
3464 break;
3465 }
3466 ts->last_pos = avio_tell(s->pb);
3467 return ret;
3468}
3469
3470static int mpegts_probe(const AVProbeData *p)
3471{
3472 const int size = p->buf_size;
3473 int maxscore = 0;
3474 int sumscore = 0;
3475 int i;
3476 int check_count = size / TS_FEC_PACKET_SIZE;
3477#define CHECK_COUNT 10
3478#define CHECK_BLOCK 100
3479
3480 if (!check_count)
3481 return 0;
3482
3483 for (i = 0; i<check_count; i+=CHECK_BLOCK) {
3484 int left = FFMIN(check_count - i, CHECK_BLOCK);
3485 int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , 1);
3487 int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , 1);
3488 score = FFMAX3(score, dvhs_score, fec_score);
3489 sumscore += score;
3490 maxscore = FFMAX(maxscore, score);
3491 }
3492
3493 sumscore = sumscore * CHECK_COUNT / check_count;
3494 maxscore = maxscore * CHECK_COUNT / CHECK_BLOCK;
3495
3496 ff_dlog(0, "TS score: %d %d\n", sumscore, maxscore);
3497
3498 if (check_count > CHECK_COUNT && sumscore > 6) {
3499 return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
3500 } else if (check_count >= CHECK_COUNT && sumscore >= CHECK_COUNT) {
3501 return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
3502 } else if (check_count >= CHECK_COUNT && sumscore > 6) {
3503 return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3504 } else if (check_count >= CHECK_COUNT && maxscore > 6) {
3505 return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3506 } else if (sumscore > 6) {
3507 return 2;
3508 } else {
3509 return 0;
3510 }
3511}
3512
3513/* return the 90kHz PCR and the extension for the 27MHz PCR. return
3514 * (-1) if not available */
3515static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
3516{
3517 int afc, len, flags;
3518 const uint8_t *p;
3519 unsigned int v;
3520
3521 afc = (packet[3] >> 4) & 3;
3522 if (afc <= 1)
3523 return AVERROR_INVALIDDATA;
3524 p = packet + 4;
3525 len = p[0];
3526 p++;
3527 if (len == 0)
3528 return AVERROR_INVALIDDATA;
3529 flags = *p++;
3530 len--;
3531 if (!(flags & 0x10))
3532 return AVERROR_INVALIDDATA;
3533 if (len < 6)
3534 return AVERROR_INVALIDDATA;
3535 v = AV_RB32(p);
3536 *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
3537 *ppcr_low = ((p[4] & 1) << 8) | p[5];
3538 return 0;
3539}
3540
3542
3543 /* NOTE: We attempt to seek on non-seekable files as well, as the
3544 * probe buffer usually is big enough. Only warn if the seek failed
3545 * on files where the seek should work. */
3546 if (avio_seek(pb, pos, SEEK_SET) < 0)
3547 av_log(s, (pb->seekable & AVIO_SEEKABLE_NORMAL) ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
3548}
3549
3551{
3552 MpegTSContext *ts = s->priv_data;
3553 AVIOContext *pb = s->pb;
3554 int64_t pos, probesize = s->probesize;
3555 int64_t seekback = FFMAX(s->probesize, (int64_t)ts->resync_size + PROBE_PACKET_MAX_BUF);
3556
3557 if (ffio_ensure_seekback(pb, seekback) < 0)
3558 av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n");
3559
3560 pos = avio_tell(pb);
3562 if (ts->raw_packet_size <= 0) {
3563 av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
3565 }
3566 ts->stream = s;
3567 ts->auto_guess = 0;
3568
3569 if (s->iformat == &ff_mpegts_demuxer.p) {
3570 /* normal demux */
3571
3572 /* first do a scan to get all the services */
3573 seek_back(s, pb, pos);
3574
3578
3579 handle_packets(ts, probesize / ts->raw_packet_size);
3580 /* if could not find service, enable auto_guess */
3581
3582 ts->auto_guess = 1;
3583
3584 av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
3585
3586 s->ctx_flags |= AVFMTCTX_NOHEADER;
3587 } else {
3588 AVStream *st;
3589 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
3590 int64_t pcrs[2], pcr_h;
3591 uint8_t packet[TS_PACKET_SIZE];
3592 const uint8_t *data;
3593
3594 /* only read packets */
3595
3596 st = avformat_new_stream(s, NULL);
3597 if (!st)
3598 return AVERROR(ENOMEM);
3599 avpriv_set_pts_info(st, 60, 1, 27000000);
3602
3603 /* we iterate until we find two PCRs to estimate the bitrate */
3604 pcr_pid = -1;
3605 nb_pcrs = 0;
3606 nb_packets = 0;
3607 for (;;) {
3608 ret = read_packet(s, packet, ts->raw_packet_size, &data);
3609 if (ret < 0)
3610 return ret;
3611 pid = AV_RB16(data + 1) & 0x1fff;
3612 if ((pcr_pid == -1 || pcr_pid == pid) &&
3613 parse_pcr(&pcr_h, &pcr_l, data) == 0) {
3615 pcr_pid = pid;
3616 pcrs[nb_pcrs] = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
3617 nb_pcrs++;
3618 if (nb_pcrs >= 2) {
3619 if (pcrs[1] - pcrs[0] > 0) {
3620 /* the difference needs to be positive to make sense for bitrate computation */
3621 break;
3622 } else {
3623 av_log(ts->stream, AV_LOG_WARNING, "invalid pcr pair %"PRId64" >= %"PRId64"\n", pcrs[0], pcrs[1]);
3624 pcrs[0] = pcrs[1];
3625 nb_pcrs--;
3626 }
3627 }
3628 } else {
3630 }
3631 nb_packets++;
3632 }
3633
3634 /* NOTE1: the bitrate is computed without the FEC */
3635 /* NOTE2: it is only the bitrate of the start of the stream */
3636 ts->pcr_incr = pcrs[1] - pcrs[0];
3637 ts->cur_pcr = pcrs[0] - ts->pcr_incr * (nb_packets - 1);
3638 s->bit_rate = TS_PACKET_SIZE * 8 * 27000000LL / ts->pcr_incr;
3639 st->codecpar->bit_rate = s->bit_rate;
3640 st->start_time = ts->cur_pcr;
3641 av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%"PRId64"\n",
3642 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
3643 }
3644
3645 seek_back(s, pb, pos);
3646 return 0;
3647}
3648
3649#define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
3650
3652{
3653 MpegTSContext *ts = s->priv_data;
3654 int ret, i;
3655 int64_t pcr_h, next_pcr_h, pos;
3656 int pcr_l, next_pcr_l;
3657 uint8_t pcr_buf[12];
3658 const uint8_t *data;
3659
3660 if ((ret = av_new_packet(pkt, TS_PACKET_SIZE)) < 0)
3661 return ret;
3662 ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
3663 pkt->pos = avio_tell(s->pb);
3664 if (ret < 0) {
3665 return ret;
3666 }
3667 if (data != pkt->data)
3668 memcpy(pkt->data, data, TS_PACKET_SIZE);
3670 if (ts->mpeg2ts_compute_pcr) {
3671 /* compute exact PCR for each packet */
3672 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
3673 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
3674 pos = avio_tell(s->pb);
3675 for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
3676 avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
3677 avio_read(s->pb, pcr_buf, 12);
3678 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
3679 /* XXX: not precise enough */
3680 ts->pcr_incr =
3681 ((next_pcr_h - pcr_h) * SYSTEM_CLOCK_FREQUENCY_DIVISOR + (next_pcr_l - pcr_l)) /
3682 (i + 1);
3683 break;
3684 }
3685 }
3686 avio_seek(s->pb, pos, SEEK_SET);
3687 /* no next PCR found: we use previous increment */
3688 ts->cur_pcr = pcr_h * SYSTEM_CLOCK_FREQUENCY_DIVISOR + pcr_l;
3689 }
3690 pkt->pts = ts->cur_pcr;
3691 pkt->duration = ts->pcr_incr;
3692 ts->cur_pcr += ts->pcr_incr;
3693 }
3694 pkt->stream_index = 0;
3695 return 0;
3696}
3697
3699{
3700 MpegTSContext *ts = s->priv_data;
3701 int ret, i;
3702
3703 pkt->size = -1;
3704 ts->pkt = pkt;
3705 ret = handle_packets(ts, 0);
3706 if (ret < 0) {
3707 av_packet_unref(ts->pkt);
3708 /* flush pes data left */
3709 for (i = 0; i < NB_PID_MAX; i++)
3710 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
3711 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
3712 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
3713 ret = new_pes_packet(pes, pkt);
3714 if (ret < 0)
3715 return ret;
3716 pes->state = MPEGTS_SKIP;
3717 ret = 0;
3718 break;
3719 }
3720 }
3721 }
3722
3723 if (!ret && pkt->size < 0)
3724 ret = AVERROR_INVALIDDATA;
3725 return ret;
3726}
3727
3729{
3730 int i;
3731
3732 clear_programs(ts);
3733
3734 for (i = 0; i < FF_ARRAY_ELEMS(ts->pools); i++)
3736
3737 for (i = 0; i < NB_PID_MAX; i++)
3738 if (ts->pids[i])
3739 mpegts_close_filter(ts, ts->pids[i]);
3740}
3741
3743{
3744 MpegTSContext *ts = s->priv_data;
3745 mpegts_free(ts);
3746 return 0;
3747}
3748
3750 int64_t *ppos, int64_t pos_limit)
3751{
3752 MpegTSContext *ts = s->priv_data;
3753 int64_t pos, timestamp;
3754 uint8_t buf[TS_PACKET_SIZE];
3755 int pcr_l, pcr_pid =
3756 ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
3757 int pos47 = ts->pos47_full % ts->raw_packet_size;
3758 pos =
3759 ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) *
3760 ts->raw_packet_size + pos47;
3761 while(pos < pos_limit) {
3762 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3763 return AV_NOPTS_VALUE;
3764 if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
3765 return AV_NOPTS_VALUE;
3766 if (buf[0] != SYNC_BYTE) {
3767 if (mpegts_resync(s, TS_PACKET_SIZE, buf) < 0)
3768 return AV_NOPTS_VALUE;
3769 pos = avio_tell(s->pb);
3770 continue;
3771 }
3772 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
3773 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
3774 *ppos = pos;
3775 return timestamp;
3776 }
3777 pos += ts->raw_packet_size;
3778 }
3779
3780 return AV_NOPTS_VALUE;
3781}
3782
3783static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
3784 int64_t *ppos, int64_t pos_limit)
3785{
3786 MpegTSContext *ts = s->priv_data;
3787 AVPacket *pkt;
3788 int64_t pos;
3789 int pos47 = ts->pos47_full % ts->raw_packet_size;
3790 pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
3792 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3793 return AV_NOPTS_VALUE;
3794 pkt = av_packet_alloc();
3795 if (!pkt)
3796 return AV_NOPTS_VALUE;
3797 while(pos < pos_limit) {
3798 int ret = av_read_frame(s, pkt);
3799 if (ret < 0) {
3801 return AV_NOPTS_VALUE;
3802 }
3803 if (pkt->dts != AV_NOPTS_VALUE && pkt->pos >= 0) {
3804 ff_reduce_index(s, pkt->stream_index);
3805 av_add_index_entry(s->streams[pkt->stream_index], pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
3806 if (pkt->stream_index == stream_index && pkt->pos >= *ppos) {
3807 int64_t dts = pkt->dts;
3808 *ppos = pkt->pos;
3810 return dts;
3811 }
3812 }
3813 pos = pkt->pos;
3815 }
3816
3818 return AV_NOPTS_VALUE;
3819}
3820
3821/**************************************************************/
3822/* parsing functions - called from other demuxers such as RTP */
3823
3825{
3826 MpegTSContext *ts;
3827
3828 ts = av_mallocz(sizeof(MpegTSContext));
3829 if (!ts)
3830 return NULL;
3831 /* no stream case, currently used by RTP */
3833 ts->max_packet_size = 2048000;
3834 ts->stream = s;
3835 ts->auto_guess = 1;
3836
3840
3841 return ts;
3842}
3843
3844/* return the consumed length if a packet was output, or -1 if no
3845 * packet is output */
3847 const uint8_t *buf, int len)
3848{
3849 int len1;
3850
3851 len1 = len;
3852 ts->pkt = pkt;
3853 for (;;) {
3854 ts->stop_parse = 0;
3855 if (len < TS_PACKET_SIZE)
3856 return AVERROR_INVALIDDATA;
3857 if (buf[0] != SYNC_BYTE) {
3858 buf++;
3859 len--;
3860 } else {
3861 handle_packet(ts, buf, len1 - len + TS_PACKET_SIZE);
3862 buf += TS_PACKET_SIZE;
3864 if (ts->stop_parse == 1)
3865 break;
3866 }
3867 }
3868 return len1 - len;
3869}
3870
3872{
3873 mpegts_free(ts);
3874 av_free(ts);
3875}
3876
3878 .p.name = "mpegts",
3879 .p.long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
3880 .p.flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
3881 .p.priv_class = &mpegts_class,
3882 .priv_data_size = sizeof(MpegTSContext),
3888 .flags_internal = FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE,
3889};
3890
3892 .p.name = "mpegtsraw",
3893 .p.long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
3894 .p.flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
3895 .p.priv_class = &mpegtsraw_class,
3896 .priv_data_size = sizeof(MpegTSContext),
3901 .flags_internal = FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE,
3902};
static int probe(const AVProbeData *p)
Definition act.c:39
static double val(void *priv, double ch)
Definition aeval.c:77
const FFInputFormat ff_mpegts_demuxer
Definition mpegts.c:3877
const FFInputFormat ff_mpegtsraw_demuxer
Definition mpegts.c:3891
channels
Definition aptx.h:31
#define EXTERN
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_unreachable(msg)
Asserts that are used as compiler optimization hints depending upon ASSERT_LEVEL and NBDEBUG.
Definition avassert.h:109
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
void ff_remove_stream_group(AVFormatContext *s, AVStreamGroup *stg)
Remove a stream group from its AVFormatContext and free it.
Definition avformat.c:130
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition avformat.c:343
Main libavformat public API header.
#define AVPROBE_SCORE_STREAM_RETRY
Definition avformat.h:479
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AV_DISPOSITION_STILL_IMAGE
The video stream contains still images.
Definition avformat.h:731
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition avformat.h:676
AVStreamGroupParamsType
Definition avformat.h:1146
@ AV_STREAM_GROUP_PARAMS_DOLBY_VISION
Definition avformat.h:1153
@ AV_STREAM_GROUP_PARAMS_LCEVC
Definition avformat.h:1151
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition avformat.h:500
#define AV_DISPOSITION_DEPENDENT
The stream is intended to be mixed with another stream before presentation.
Definition avformat.h:727
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition avformat.h:496
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition avformat.h:684
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition avformat.h:680
#define AVSTREAM_EVENT_FLAG_METADATA_UPDATED
Definition avformat.h:884
#define AV_DISPOSITION_DESCRIPTIONS
The subtitle stream contains a textual description of the video content.
Definition avformat.h:716
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:611
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:687
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_indirect(AVIOContext *s, unsigned char *buf, int size, const unsigned char **data)
Read size bytes from AVIOContext, returning a pointer.
Definition aviobuf.c:675
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition aviobuf.c:1026
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition aviobuf.c:99
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
refcounted data buffer API
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Public header for CRC hash function implementation.
static const uint16_t fc[]
Definition dcaenc.h:43
Misc types and constants that do not belong anywhere else.
#define AV_PROFILE_ARIB_PROFILE_A
Definition defs.h:191
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_ARIB_PROFILE_C
Definition defs.h:192
@ AV_FIELD_TT
Top coded_first, top displayed first.
Definition defs.h:214
@ AV_FIELD_BB
Bottom coded first, bottom displayed first.
Definition defs.h:215
@ AV_FIELD_PROGRESSIVE
Definition defs.h:213
#define FF_INFMT_FLAG_PREFER_CODEC_FRAMERATE
Definition demux.h:40
int ff_find_stream_index(const AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition seek.c:716
void ff_reduce_index(AVFormatContext *s, int stream_index)
Ensure the index uses less memory than the maximum specified in AVFormatContext.max_index_size by dis...
Definition seek.c:50
static AVPacket * pkt
Public dictionary API.
AVDOVIDecoderConfigurationRecord * av_dovi_alloc(size_t *size)
Allocate a AVDOVIDecoderConfigurationRecord structure and initialize its fields to default values.
Definition dovi_meta.c:26
DOVI configuration.
@ AV_DOVI_COMPRESSION_NONE
Definition dovi_meta.h:68
static const uint8_t bits[8]
Definition fastaudio.c:100
static unsigned int nb_streams
Definition ffprobe.c:352
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition get_bits.h:280
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition get_bits.h:456
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition get_bits.h:517
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_DIRAC
Definition codec_id.h:166
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition codec_id.h:616
@ AV_CODEC_ID_AVS3
Definition codec_id.h:245
@ AV_CODEC_ID_TIMED_ID3
Definition codec_id.h:606
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition codec_id.h:572
@ AV_CODEC_ID_EAC3
Definition codec_id.h:493
@ AV_CODEC_ID_DVB_SUBTITLE
Definition codec_id.h:567
@ AV_CODEC_ID_S302M
Definition codec_id.h:356
@ AV_CODEC_ID_SMPTE_2038
Definition codec_id.h:608
@ AV_CODEC_ID_LCEVC
Definition codec_id.h:609
@ AV_CODEC_ID_PCM_BLURAY
Definition codec_id.h:354
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_SMPTE_KLV
Definition codec_id.h:604
@ AV_CODEC_ID_EPG
Definition codec_id.h:599
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_JPEG2000
Definition codec_id.h:138
@ AV_CODEC_ID_HDMV_TEXT_SUBTITLE
Definition codec_id.h:589
@ AV_CODEC_ID_VVC
Definition codec_id.h:247
@ AV_CODEC_ID_JPEGXS
Definition codec_id.h:325
@ AV_CODEC_ID_SCTE_35
Contain timestamp estimated through PCR of program stream.
Definition codec_id.h:598
@ AV_CODEC_ID_VC1
Definition codec_id.h:120
@ AV_CODEC_ID_ARIB_CAPTION
Definition codec_id.h:591
@ AV_CODEC_ID_CAVS
Definition codec_id.h:137
@ AV_CODEC_ID_DTS
Definition codec_id.h:457
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_BIN_DATA
Definition codec_id.h:607
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_TRUEHD
Definition codec_id.h:497
@ AV_CODEC_ID_AC3
Definition codec_id.h:456
@ AV_CODEC_ID_DVB_TELETEXT
Definition codec_id.h:573
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_AVS2
Definition codec_id.h:243
@ AV_CODEC_ID_AC4
Definition codec_id.h:556
@ AV_CODEC_ID_MPEG4SYSTEMS
FAKE codec to indicate a MPEG-4 Systems stream (only used by libavformat)
Definition codec_id.h:618
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
@ AV_CODEC_ID_AAC_LATM
Definition codec_id.h:502
@ AV_CODEC_ID_OPUS
Definition codec_id.h:513
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:232
AVPacketSideData * av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size, int flags)
Wrap existing data as packet side data.
Definition packet.c:613
@ AV_PKT_DATA_MPEGTS_STREAM_ID
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
Definition packet.h:212
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition packet.h:280
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition packet.h:651
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition avformat.c:282
AVStreamGroup * avformat_stream_group_create(AVFormatContext *s, enum AVStreamGroupParamsType type, AVDictionary **options)
Add a new empty stream group to a media file.
Definition options.c:470
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int avformat_stream_group_add_stream(AVStreamGroup *stg, AVStream *st)
Add an already allocated stream to a stream group.
Definition options.c:558
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition avformat.c:454
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition demux.c:1588
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition buffer.c:328
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition buffer.c:283
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition buffer.c:390
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition crc.c:389
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition crc.c:421
@ AV_CRC_32_IEEE
Definition crc.h:52
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition dict.c:247
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition dict.h:81
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition dict.c:37
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
int av_size_mult(size_t a, size_t b, size_t *r)
Multiply two size_t values checking for overflow.
Definition mem.c:565
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
Definition utils.c:28
AVMediaType
Definition avutil.h:198
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
cl_device_type type
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition id3v2.c:1186
int ff_id3v2_parse_priv_dict(AVDictionary **metadata, ID3v2ExtraMeta *extra_meta)
Parse PRIV tags into a dictionary.
Definition id3v2.c:1258
void ff_id3v2_read_dict(AVFormatContext *s, AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition id3v2.c:1174
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition id3v2.h:35
#define r
Definition input.c:42
#define av_log2
Definition intmath.h:84
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RB32(p)
#define AV_RB16(p)
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition isom.c:305
int ff_mp4_read_dec_config_descr(void *logctx, AVStream *st, AVIOContext *pb)
Definition isom.c:330
int ff_mp4_read_descr(void *logctx, AVIOContext *pb, int *tag)
Definition isom.c:296
#define MP4ESDescrTag
Definition isom.h:403
#define MP4ODescrTag
Definition isom.h:401
#define MP4SLDescrTag
Definition isom.h:406
#define MP4DecConfigDescrTag
Definition isom.h:404
#define MP4IODescrTag
Definition isom.h:402
unsigned offset
Definition libaomenc.c:763
#define u(width, name, range_min, range_max)
Definition cbs_apv.c:68
static const struct TransferCharacteristics transfer_characteristics[]
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define hex_dump_debug(class, buf, size)
Definition internal.h:39
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition utils.c:237
#define av_unused
Definition attributes.h:164
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
version
Definition libkvazaar.c:313
#define FFMAX3(a, b, c)
Definition macros.h:48
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
#define mid_pred
Definition mathops.h:115
Memory handling functions.
uint32_t tag
Definition movenc.c:2073
#define STREAM_TYPE_VIDEO_MPEG4
Definition mpeg.h:56
#define STREAM_TYPE_VIDEO_CAVS
Definition mpeg.h:60
#define STREAM_TYPE_AUDIO_MPEG2
Definition mpeg.h:52
#define STREAM_TYPE_VIDEO_HEVC
Definition mpeg.h:58
#define STREAM_TYPE_PRIVATE_SECTION
Definition mpeg.h:53
#define STREAM_TYPE_VIDEO_VVC
Definition mpeg.h:59
#define STREAM_TYPE_PRIVATE_DATA
Definition mpeg.h:54
#define STREAM_TYPE_VIDEO_MPEG1
Definition mpeg.h:49
#define STREAM_TYPE_AUDIO_AAC
Definition mpeg.h:55
#define STREAM_TYPE_VIDEO_H264
Definition mpeg.h:57
#define STREAM_TYPE_AUDIO_MPEG1
Definition mpeg.h:51
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition mpeg.h:69
#define STREAM_TYPE_VIDEO_MPEG2
Definition mpeg.h:50
#define MAX_PIDS_PER_PROGRAM
Definition mpegts.c:122
#define R8_CHECK_CLIP_MAX(dst, maxv)
static const StreamType ISO_types[]
Definition mpegts.c:830
static void mpegts_free(MpegTSContext *ts)
Definition mpegts.c:3728
static void clear_program(struct Program *p)
Definition mpegts.c:324
static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1586
#define MAX_STREAMS_PER_PROGRAM
Definition mpegts.c:121
static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos)
Definition mpegts.c:3541
static void add_pid_to_program(struct Program *p, unsigned int pid)
Definition mpegts.c:366
static void write_section_data(MpegTSContext *ts, MpegTSFilter *tss1, const uint8_t *buf, int buf_size, int is_start)
Assemble PES packets out of TS packets, and then call the "section_cb" function when they are complet...
Definition mpegts.c:453
static const uint8_t opus_channel_map[8][8]
Definition mpegts.c:1892
void avpriv_mpegts_parse_close(MpegTSContext *ts)
Definition mpegts.c:3871
static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1660
static const StreamType MISC_types[]
Definition mpegts.c:880
static void detect_bdmv_dovi_group(MpegTSContext *ts, struct Program *prg, uint32_t prog_reg_desc)
Definition mpegts.c:2632
static int skip_identical(const SectionHeader *h, MpegTSSectionFilter *tssf)
Definition mpegts.c:688
int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, int prg_id, const uint8_t **pp, const uint8_t *desc_list_end, Mp4Descr *mp4_descr, int mp4_descr_count, int pid, MpegTSContext *ts)
Parse an MPEG-2 descriptor.
Definition mpegts.c:2077
static int timed_id3_update_metadata(AVStream *s, AVPacket *pkt)
Definition mpegts.c:1046
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1646
static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int max_descr_count)
Definition mpegts.c:1557
static void reset_pes_packet_state(PESContext *pes)
Definition mpegts.c:1030
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
Definition mpegts.c:3515
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:2740
static const uint8_t opus_coupled_stream_cnt[9]
Definition mpegts.c:1884
static int get_packet_size(AVFormatContext *s)
Definition mpegts.c:641
static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition mpegts.c:3749
static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mpegts.c:3698
static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1624
static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
Definition mpegts.c:591
static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:2966
static MpegTSFilter * mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid)
Definition mpegts.c:586
static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
Definition mpegts.c:296
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, int target_tag)
Definition mpegts.c:1703
static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
Definition mpegts.c:2573
static const StreamType REGD_types[]
Definition mpegts.c:896
static int mpegts_set_stream_info(AVStream *st, PESContext *pes, uint32_t stream_type, uint32_t prog_reg_desc)
Definition mpegts.c:949
static const StreamType HLS_SAMPLE_ENC_types[]
Definition mpegts.c:888
static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
Definition mpegts.c:309
static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
Definition mpegts.c:3405
static void clear_programs(MpegTSContext *ts)
Definition mpegts.c:335
static int analyze(const uint8_t *buf, int size, int packet_size, int probe)
Definition mpegts.c:612
static const StreamType METADATA_types[]
Definition mpegts.c:915
static int new_pes_packet(PESContext *pes, AVPacket *pkt)
Definition mpegts.c:1070
static const AVClass mpegts_class
Definition mpegts.c:233
static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int *descr_count, int max_descr_count)
Definition mpegts.c:1757
void SetServiceCallback(void *opaque, int ret)
Definition mpegts.c:88
static int mpegts_read_header(AVFormatContext *s)
Definition mpegts.c:3550
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:1791
static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition mpegts.c:3783
static AVBufferRef * buffer_pool_get(MpegTSContext *ts, int size)
Definition mpegts.c:1211
static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
Definition mpegts.c:1039
static uint64_t get_ts64(GetBitContext *gb, int bits)
Definition mpegts.c:1133
static void create_stream_groups(MpegTSContext *ts, struct Program *prg)
Definition mpegts.c:2674
#define TS_PAYLOAD_SIZE
Definition mpegts.c:56
#define PROBE_PACKET_MARGIN
Definition mpegts.c:68
static int parse_section_header(SectionHeader *h, const uint8_t **pp, const uint8_t *p_end)
Definition mpegts.c:794
void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len)
Definition mpegts.c:86
static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
Definition mpegts.c:2609
static int mpegts_push_data(MpegTSFilter *filter, const uint8_t *buf, int buf_size, int is_start, int64_t pos)
Definition mpegts.c:1224
MpegTSContext * avpriv_mpegts_parse_open(AVFormatContext *s)
Definition mpegts.c:3824
static MpegTSFilter * mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid, SectionCallback *section_cb, void *opaque, int check_crc)
Definition mpegts.c:543
static int discard_pid(MpegTSContext *ts, unsigned int pid)
discard_pid() decides if the pid is to be discarded according to caller's programs selection
Definition mpegts.c:412
static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
Definition mpegts.c:3417
static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:3057
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
Definition mpegts.c:3374
static MpegTSFilter * mpegts_open_filter(MpegTSContext *ts, unsigned int pid, enum MpegTSFilterType type)
Definition mpegts.c:520
static void update_av_program_info(AVFormatContext *s, unsigned int programid, unsigned int pid, int version)
Definition mpegts.c:382
static int get8(const uint8_t **pp, const uint8_t *p_end)
Definition mpegts.c:699
static char * getstr8(const uint8_t **pp, const uint8_t *p_end)
Definition mpegts.c:727
int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, const uint8_t *buf, int len)
Definition mpegts.c:3846
static int mpegts_read_close(AVFormatContext *s)
Definition mpegts.c:3742
static void mpegts_find_stream_type(AVStream *st, uint32_t stream_type, const StreamType *types)
Definition mpegts.c:931
#define PROBE_PACKET_MAX_BUF
Definition mpegts.c:67
static const uint8_t opus_stream_cnt[9]
Definition mpegts.c:1888
static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
Definition mpegts.c:1576
int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos)
Definition mpegts.c:78
static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition mpegts.c:3651
static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:3111
#define MAX_MP4_DESCR_COUNT
Definition mpegts.c:58
static int get16(const uint8_t **pp, const uint8_t *p_end)
Definition mpegts.c:712
MpegTSFilterType
Definition mpegts.c:70
@ MPEGTS_SECTION
Definition mpegts.c:72
@ MPEGTS_PES
Definition mpegts.c:71
@ MPEGTS_PCR
Definition mpegts.c:73
static const AVOption raw_options[]
Definition mpegts.c:240
static const AVClass mpegtsraw_class
Definition mpegts.c:248
static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int *descr_count, int max_descr_count)
Definition mpegts.c:1775
static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
Definition mpegts.c:3201
static const StreamType SCTE_types[]
Definition mpegts.c:874
static const StreamType DESC_types[]
Definition mpegts.c:922
MpegTSState
Definition mpegts.c:257
@ MPEGTS_HEADER
Definition mpegts.c:258
@ MPEGTS_SKIP
Definition mpegts.c:262
@ MPEGTS_PESHEADER
Definition mpegts.c:259
@ MPEGTS_PESHEADER_FILL
Definition mpegts.c:260
@ MPEGTS_PAYLOAD
Definition mpegts.c:261
#define MAX_PES_HEADER_SIZE
Definition mpegts.c:268
static int mpegts_probe(const AVProbeData *p)
Definition mpegts.c:3470
#define PES_HEADER_SIZE
Definition mpegts.c:267
static const StreamType HDMV_types[]
Definition mpegts.c:858
static struct Program * add_program(MpegTSContext *ts, unsigned int programid)
Definition mpegts.c:342
static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet)
Definition mpegts.c:3331
static int parse_mpeg2_extension_descriptor(AVFormatContext *fc, AVStream *st, int prg_id, const uint8_t **pp, const uint8_t *desc_end, MpegTSContext *ts)
Definition mpegts.c:1903
static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1610
static AVStream * find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid, int stream_identifier, int pmt_stream_idx, struct Program *p)
Definition mpegts.c:2547
static MpegTSFilter * mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, PESCallback *pes_cb, void *opaque)
Definition mpegts.c:570
#define PES_START_SIZE
Definition mpegts.c:266
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
Definition mpegts.c:1140
static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition mpegts.c:1597
#define MPEGTS_OPTIONS
Definition mpegts.c:203
#define MAX_PACKET_READAHEAD
Definition mpegts.c:3649
static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition mpegts.c:1855
static PESContext * add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
Definition mpegts.c:1521
#define STREAM_ID_PRIVATE_STREAM_2
Definition mpegts.h:188
#define STREAM_TYPE_HLS_SE_AUDIO_AC3
Definition mpegts.h:180
#define TS_MAX_PACKET_SIZE
Definition mpegts.h:30
#define STREAM_TYPE_VIDEO_AVS3
Definition mpegts.h:153
#define STREAM_TYPE_VIDEO_MVC
Definition mpegts.h:145
#define STREAM_TYPE_VIDEO_DIRAC
Definition mpegts.h:155
#define STREAM_ID_PADDING_STREAM
Definition mpegts.h:187
#define DVB_EXTENSION_DESCRIPTOR
Definition mpegts.h:227
#define FMC_DESCRIPTOR
Definition mpegts.h:205
#define STREAM_TYPE_BLURAY_SUBTITLE_TEXT
Definition mpegts.h:168
#define STREAM_TYPE_AUDIO_MPEG4
ISO/IEC 14496-3 Audio, without using any additional transport syntax, such as DST,...
Definition mpegts.h:144
#define NB_PID_MAX
Definition mpegts.h:32
#define STREAM_TYPE_VIDEO_VC1
Definition mpegts.h:154
#define PAT_PID
Definition mpegts.h:41
#define STREAM_TYPE_ATSC_AUDIO_AC3
Definition mpegts.h:172
#define M2TS_VIDEO_EL_PID
Definition mpegts.h:76
#define STREAM_TYPE_BLURAY_AUDIO_EAC3
Definition mpegts.h:162
#define TS_FEC_PACKET_SIZE
Definition mpegts.h:27
#define IOD_DESCRIPTOR
Definition mpegts.h:203
#define STREAM_ID_METADATA_STREAM
Definition mpegts.h:195
#define M2TS_VIDEO_PID
Definition mpegts.h:75
#define STREAM_TYPE_BLURAY_SUBTITLE_PGS
Definition mpegts.h:167
#define STREAM_TYPE_HLS_SE_AUDIO_AAC
Definition mpegts.h:179
#define PAT_TID
Definition mpegts.h:84
#define STREAM_TYPE_VIDEO_JPEGXS
Definition mpegts.h:148
#define SUBTITLING_DESCRIPTOR
Definition mpegts.h:223
#define STREAM_ID_EMM_STREAM
Definition mpegts.h:192
#define STREAM_TYPE_BLURAY_AUDIO_DTS_EXPRESS_SECONDARY
Definition mpegts.h:166
#define STREAM_TYPE_BLURAY_AUDIO_TRUEHD
Definition mpegts.h:161
#define LCEVC_LINKAGE_DESCRIPTOR
Definition mpegts.h:214
#define PMT_TID
Definition mpegts.h:86
#define STREAM_TYPE_ISO_IEC_14496_SECTION
ISO/IEC 14496-1 (MPEG-4 Systems) SL-packetized stream or FlexMux stream carried in ISO_IEC_14496_sect...
Definition mpegts.h:139
#define STREAM_TYPE_BLURAY_AUDIO_DTS
Definition mpegts.h:160
#define EIT_PID
Definition mpegts.h:49
#define SDT_TID
Definition mpegts.h:92
#define STREAM_TYPE_AUDIO_AAC_LATM
Definition mpegts.h:132
#define STREAM_TYPE_BLURAY_AUDIO_EAC3_SECONDARY
Definition mpegts.h:165
#define MAX_SECTION_SIZE
Definition mpegts.h:34
#define SDT_PID
Definition mpegts.h:47
#define SUPPLEMENTARY_AUDIO_DESCRIPTOR
Definition mpegts.h:231
#define ISO_639_LANGUAGE_DESCRIPTOR
Definition mpegts.h:202
#define JXS_VIDEO_DESCRIPTOR
Definition mpegts.h:212
#define STREAM_IDENTIFIER_DESCRIPTOR
Definition mpegts.h:221
#define TELETEXT_DESCRIPTOR
Definition mpegts.h:222
#define STREAM_TYPE_VIDEO_LCEVC
Definition mpegts.h:150
#define LCEVC_VIDEO_DESCRIPTOR
Definition mpegts.h:213
#define TS_DVHS_PACKET_SIZE
Definition mpegts.h:28
#define STREAM_TYPE_VIDEO_JPEG2000
Definition mpegts.h:146
#define STREAM_TYPE_VIDEO_AVS2
Definition mpegts.h:152
#define STREAM_TYPE_ATSC_AUDIO_EAC3
Definition mpegts.h:173
#define OEITS_END_TID
Definition mpegts.h:105
#define REGISTRATION_DESCRIPTOR
Definition mpegts.h:201
#define SL_DESCRIPTOR
Definition mpegts.h:204
#define SERVICE_DESCRIPTOR
Definition mpegts.h:220
#define EIT_TID
Definition mpegts.h:100
#define STREAM_TYPE_BLURAY_AUDIO_PCM_BLURAY
Definition mpegts.h:158
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY
Definition mpegts.h:197
#define STREAM_TYPE_BLURAY_AUDIO_AC3
Definition mpegts.h:159
#define STREAM_ID_DSMCC_STREAM
Definition mpegts.h:193
#define ENHANCED_AC3_DESCRIPTOR
Definition mpegts.h:225
#define DATA_COMPONENT_DESCRIPTOR
Definition mpegts.h:238
#define STREAM_ID_PROGRAM_STREAM_MAP
Definition mpegts.h:185
#define STREAM_ID_ECM_STREAM
Definition mpegts.h:191
#define NULL_PID
Definition mpegts.h:70
#define METADATA_DESCRIPTOR
Definition mpegts.h:206
#define AC3_DESCRIPTOR
Definition mpegts.h:224
#define STREAM_TYPE_HLS_SE_AUDIO_EAC3
Definition mpegts.h:181
#define STREAM_TYPE_HLS_SE_VIDEO_H264
Definition mpegts.h:178
#define STREAM_ID_TYPE_E_STREAM
Definition mpegts.h:194
#define DTS_DESCRIPTOR
Definition mpegts.h:226
#define VIDEO_STREAM_DESCRIPTOR
Definition mpegts.h:200
#define STREAM_TYPE_BLURAY_AUDIO_DTS_HD
Definition mpegts.h:163
#define SYSTEM_CLOCK_FREQUENCY_DIVISOR
Definition mpegts.h:38
#define SYNC_BYTE
Definition mpegts.h:36
#define STREAM_TYPE_BLURAY_AUDIO_DTS_HD_MASTER
Definition mpegts.h:164
#define AC4_DESCRIPTOR
Definition mpegts.h:232
#define STREAM_TYPE_SCTE_DATA_SCTE_35
Definition mpegts.h:170
#define EXTENSION_DESCRIPTOR
Definition mpegts.h:208
#define STREAM_TYPE_METADATA
Definition mpegts.h:140
#define STREAM_TYPE_ISO_IEC_14496_PES
ISO/IEC 14496-1 (MPEG-4 Systems) SL-packetized stream or FlexMux stream carried in PES packets.
Definition mpegts.h:136
#define TS_PACKET_SIZE
Definition mpegts.h:29
#define STUFFING_BYTE
Definition mpegts.h:37
#define DOVI_VIDEO_STREAM_DESCRIPTOR
see "Dolby Vision Streams Within the MPEG-2 Transport Stream Format" https://professional....
Definition mpegts.h:236
#define M4OD_TID
Definition mpegts.h:89
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
static const uint8_t opus_default_extradata[30]
Definition opus.h:35
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
@ AV_PIX_FMT_YUV422P10LE
planar YUV 4:2:2, 20bpp, (1 Cr & Cb sample per 2x1 Y samples), little-endian
Definition pixfmt.h:158
@ AV_PIX_FMT_YUV444P10LE
planar YUV 4:4:4, 30bpp, (1 Cr & Cb sample per 1x1 Y samples), little-endian
Definition pixfmt.h:162
const char * name
Definition qsvenc.c:142
#define MAX_LEVEL
Definition rl.h:36
#define CHECK_BLOCK()
Definition rpza.c:54
static int64_t read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition seek.c:281
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition snprintf.h:34
unsigned int pos
Definition spdifenc.c:431
The buffer pool.
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVColorSpace color_space
Definition codec_par.h:192
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
enum AVFieldOrder field_order
The order of the fields in interlaced video.
Definition codec_par.h:182
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int nb_coded_side_data
Amount of entries in coded_side_data.
Definition codec_par.h:88
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
AVRational framerate
Number of frames per second, for streams with constant frame durations.
Definition codec_par.h:175
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition codec_par.h:135
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
enum AVColorPrimaries color_primaries
Definition codec_par.h:190
enum AVColorTransferCharacteristic color_trc
Definition codec_par.h:191
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
Definition codec_par.h:83
enum AVColorRange color_range
Additional colorspace characteristics.
Definition codec_par.h:189
Format I/O context.
Definition avformat.h:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
AVStreamGroup ** stream_groups
A list of all stream groups in the file.
Definition avformat.h:1420
AVProgram ** programs
Definition avformat.h:1546
int ctx_flags
Flags signalling stream properties.
Definition avformat.h:1382
unsigned int nb_programs
Definition avformat.h:1545
unsigned int nb_stream_groups
Number of elements in AVFormatContext.stream_groups.
Definition avformat.h:1408
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
Bytestream IO Context.
Definition avio.h:160
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int stream_index
Definition packet.h:605
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition packet.h:596
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
This structure contains the data a format has to probe a file.
Definition avformat.h:471
New fields can be added to the end with minor version bumps.
Definition avformat.h:1257
int pmt_pid
Definition avformat.h:1266
unsigned int nb_stream_indexes
Definition avformat.h:1262
int program_num
Definition avformat.h:1265
int pcr_pid
Definition avformat.h:1267
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition avformat.h:1260
int pmt_version
Definition avformat.h:1268
AVDictionary * metadata
Definition avformat.h:1263
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
int height
Height of the final image for presentation.
Definition avformat.h:1118
unsigned int el_index
Index of the enhancement layer stream in AVStreamGroup.
Definition avformat.h:1102
int width
Width of the final stream for presentation.
Definition avformat.h:1114
union AVStreamGroup::@166361102046003066253145020066347265153020354020 params
Group type-specific parameters.
unsigned int nb_streams
Number of elements in AVStreamGroup.streams.
Definition avformat.h:1221
int64_t id
Group type-specific group ID.
Definition avformat.h:1178
struct AVStreamGroupLayeredVideo * layered_video
Definition avformat.h:1195
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:837
AVDictionary * metadata
Definition avformat.h:846
void * priv_data
Definition avformat.h:791
int id
Format-specific stream ID.
Definition avformat.h:778
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition avformat.h:835
AVIOContext pub
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition internal.h:262
int probe_packets
Number of packets to buffer for codec probing.
Definition internal.h:318
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition internal.h:205
int pts_wrap_behavior
Options for behavior, when a wrap is detected.
Definition internal.h:274
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition internal.h:180
int stream_identifier
Stream Identifier This is the MPEG-TS stream identifier +1 0 means unknown.
Definition internal.h:349
enum AVStreamParseType need_parsing
Definition internal.h:321
Mp4Descr * descr
Definition mpegts.c:1549
int predefined_SLConfigDescriptor_seen
Definition mpegts.c:1554
FFIOContext pb
Definition mpegts.c:1548
Mp4Descr * active_descr
Definition mpegts.c:1550
AVFormatContext * s
Definition mpegts.c:1547
int es_id
Definition mpegts.h:265
uint8_t * dec_config_descr
Definition mpegts.h:267
SLConfigDescr sl
Definition mpegts.h:268
int dec_config_descr_len
Definition mpegts.h:266
AVStream * epg_stream
Definition mpegts.c:199
int fix_teletext_pts
fix dvb teletext pts
Definition mpegts.c:161
int skip_unknown_pmt
Definition mpegts.c:176
int raw_packet_size
raw packet size, including FEC if present
Definition mpegts.c:150
int64_t pos47_full
Definition mpegts.c:152
AVBufferPool * pools[32]
Definition mpegts.c:200
int stop_parse
stop parsing loop
Definition mpegts.c:168
MpegTSFilter * pids[NB_PID_MAX]
filters for various streams specified by PMT + for the PAT and PMT
Definition mpegts.c:196
int current_pid
Definition mpegts.c:197
int max_packet_size
Definition mpegts.c:182
int skip_clear
Definition mpegts.c:175
int8_t crc_validity[NB_PID_MAX]
Definition mpegts.c:194
int64_t pcr_incr
used to estimate the exact PCR
Definition mpegts.c:164
int auto_guess
if true, all pids are analyzed to find streams
Definition mpegts.c:155
int64_t last_pos
to detect seek
Definition mpegts.c:172
AVPacket * pkt
packet containing Audio/Video data
Definition mpegts.c:170
int mpeg2ts_compute_pcr
compute exact PCR for each transport stream packet
Definition mpegts.c:158
int64_t cur_pcr
used to estimate the exact PCR
Definition mpegts.c:163
int scan_all_pmts
Definition mpegts.c:178
int skip_changes
Definition mpegts.c:174
int merge_pmt_versions
Definition mpegts.c:181
unsigned int nb_prg
structure to keep track of Program->pids mapping
Definition mpegts.c:190
int resync_size
Definition mpegts.c:180
AVFormatContext * stream
Definition mpegts.c:148
struct Program * prg
Definition mpegts.c:192
unsigned int prg_size
allocated size of prg in bytes
Definition mpegts.c:191
int64_t last_pcr
Definition mpegts.c:107
enum MpegTSFilterType type
Definition mpegts.c:109
MpegTSSectionFilter section_filter
Definition mpegts.c:112
int last_cc
Definition mpegts.c:106
MpegTSPESFilter pes_filter
Definition mpegts.c:111
union MpegTSFilter::@312121205260244256256257337225030272055136325323 u
int discard
Definition mpegts.c:108
void * opaque
Definition mpegts.c:83
PESCallback * pes_cb
Definition mpegts.c:82
SectionCallback * section_cb
Definition mpegts.c:99
unsigned int end_of_section_reached
Definition mpegts.c:98
unsigned int check_crc
Definition mpegts.c:97
uint8_t * section_buf
Definition mpegts.c:96
unsigned last_crc
Definition mpegts.c:95
int PES_packet_length
Definition mpegts.c:282
MpegTSContext * ts
Definition mpegts.c:274
AVStream * st
Definition mpegts.c:276
int merged_st
Definition mpegts.c:291
AVFormatContext * stream
Definition mpegts.c:275
SLConfigDescr sl
Definition mpegts.c:290
int pid
Definition mpegts.c:271
int data_index
Definition mpegts.c:280
int64_t dts
Definition mpegts.c:286
int64_t ts_packet_pos
position of first TS packet of this PES packet
Definition mpegts.c:287
int pcr_pid
if -1 then all packets containing PCR are considered
Definition mpegts.c:272
uint8_t stream_id
Definition mpegts.c:285
enum MpegTSState state
Definition mpegts.c:278
AVBufferRef * buffer
Definition mpegts.c:289
uint8_t header[MAX_PES_HEADER_SIZE]
Definition mpegts.c:288
int pes_header_size
Definition mpegts.c:283
AVStream * sub_st
stream for the embedded AC3 stream in HDMV TrueHD
Definition mpegts.c:277
int stream_type
Definition mpegts.c:273
int extended_stream_id
Definition mpegts.c:284
int flags
copied to the AVPacket flags
Definition mpegts.c:281
int64_t pts
Definition mpegts.c:286
unsigned int nb_stream_groups
Definition mpegts.c:138
int pmt_found
have we found pmt for this program
Definition mpegts.c:142
unsigned int nb_pids
Definition mpegts.c:134
struct StreamGroup stream_groups[MAX_STREAMS_PER_PROGRAM]
Definition mpegts.c:139
unsigned int nb_streams
Definition mpegts.c:136
unsigned int pids[MAX_PIDS_PER_PROGRAM]
Definition mpegts.c:135
struct Stream streams[MAX_STREAMS_PER_PROGRAM]
Definition mpegts.c:137
unsigned int id
Definition mpegts.c:133
int use_au_start
Definition mpegts.h:248
int timestamp_res
Definition mpegts.h:254
int timestamp_len
Definition mpegts.h:255
int degr_prior_len
Definition mpegts.h:259
int au_seq_num_len
Definition mpegts.h:260
int packet_seq_num_len
Definition mpegts.h:261
int use_rand_acc_pt
Definition mpegts.h:250
int use_au_end
Definition mpegts.h:249
int inst_bitrate_len
Definition mpegts.h:258
int use_timestamps
Definition mpegts.h:252
int use_padding
Definition mpegts.h:251
uint8_t version
Definition mpegts.c:682
uint8_t sec_num
Definition mpegts.c:684
uint8_t tid
Definition mpegts.c:680
uint8_t last_sec_num
Definition mpegts.c:685
uint16_t id
Definition mpegts.c:681
uint8_t current_next
Definition mpegts.c:683
unsigned int nb_streams
Definition mpegts.c:128
AVStream * streams[MAX_STREAMS_PER_PROGRAM]
Definition mpegts.c:129
int dep_pid
Definition mpegts.c:127
enum AVStreamGroupParamsType type
Definition mpegts.c:125
uint32_t stream_type
Definition mpegts.c:825
enum AVMediaType codec_type
Definition mpegts.c:826
enum AVCodecID codec_id
Definition mpegts.c:827
int idx
Definition mpegts.c:117
int stream_identifier
Definition mpegts.c:118
#define av_free(p)
#define av_mallocz(s)
#define ff_dlog(a,...)
#define avpriv_request_sample(...)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
static void check_crc(const AVCRC *table_new, const char *name, size_t size, size_t offset)
Definition crc.c:58
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static FILE * out
Definition movenc.c:55
static char buffer[20]
Definition seek.c:32
int size
int len
#define CHECK_COUNT()
Definition vqavideo.c:217
static double c[64]