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