FFmpeg
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 
24 #include "libavutil/buffer.h"
25 #include "libavutil/common.h"
26 #include "libavutil/crc.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/log.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/dovi_meta.h"
35 #include "libavcodec/avcodec.h"
36 #include "libavcodec/bytestream.h"
37 #include "libavcodec/get_bits.h"
38 #include "libavcodec/opus.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 #if CONFIG_ICONV
47 #include <iconv.h>
48 #endif
49 
50 /* maximum size in which we look for synchronization if
51  * synchronization is lost */
52 #define MAX_RESYNC_SIZE 65536
53 
54 #define MAX_MP4_DESCR_COUNT 16
55 
56 #define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend) \
57  do { \
58  if ((prev_dividend) == 0 || (dividend) - (prev_dividend) != (divisor)) \
59  (modulus) = (dividend) % (divisor); \
60  (prev_dividend) = (dividend); \
61  } while (0)
62 
63 #define PROBE_PACKET_MAX_BUF 8192
64 #define PROBE_PACKET_MARGIN 5
65 
70 };
71 
72 typedef struct MpegTSFilter MpegTSFilter;
73 
74 typedef int PESCallback (MpegTSFilter *f, const uint8_t *buf, int len,
75  int is_start, int64_t pos);
76 
77 typedef struct MpegTSPESFilter {
79  void *opaque;
81 
82 typedef void SectionCallback (MpegTSFilter *f, const uint8_t *buf, int len);
83 
84 typedef void SetServiceCallback (void *opaque, int ret);
85 
86 typedef struct MpegTSSectionFilter {
89  int last_ver;
90  unsigned crc;
91  unsigned last_crc;
92  uint8_t *section_buf;
93  unsigned int check_crc : 1;
94  unsigned int end_of_section_reached : 1;
96  void *opaque;
98 
99 struct MpegTSFilter {
100  int pid;
101  int es_id;
102  int last_cc; /* last cc code (-1 if first packet) */
103  int64_t last_pcr;
104  int discard;
106  union {
109  } u;
110 };
111 
112 struct Stream {
113  int idx;
115 };
116 
117 #define MAX_STREAMS_PER_PROGRAM 128
118 #define MAX_PIDS_PER_PROGRAM (MAX_STREAMS_PER_PROGRAM + 2)
119 struct Program {
120  unsigned int id; // program id/service id
121  unsigned int nb_pids;
122  unsigned int pids[MAX_PIDS_PER_PROGRAM];
123  unsigned int nb_streams;
125 
126  /** have we found pmt for this program */
128 };
129 
131  const AVClass *class;
132  /* user data */
134  /** raw packet size, including FEC if present */
136 
137  int64_t pos47_full;
138 
139  /** if true, all pids are analyzed to find streams */
141 
142  /** compute exact PCR for each transport stream packet */
144 
145  /** fix dvb teletext pts */
147 
148  int64_t cur_pcr; /**< used to estimate the exact PCR */
149  int64_t pcr_incr; /**< used to estimate the exact PCR */
150 
151  /* data needed to handle file based ts */
152  /** stop parsing loop */
154  /** packet containing Audio/Video data */
156  /** to detect seek */
157  int64_t last_pos;
158 
162 
164 
168 
169  /******************************************/
170  /* private mpegts data */
171  /* scan context */
172  /** structure to keep track of Program->pids mapping */
173  unsigned int nb_prg;
174  struct Program *prg;
175 
177  /** filters for various streams specified by PMT + for the PAT and PMT */
180 
183 };
184 
185 #define MPEGTS_OPTIONS \
186  { "resync_size", "set size limit for looking up a new synchronization", offsetof(MpegTSContext, resync_size), AV_OPT_TYPE_INT, { .i64 = MAX_RESYNC_SIZE}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM }
187 
188 static const AVOption options[] = {
190  {"fix_teletext_pts", "try to fix pts values of dvb teletext streams", offsetof(MpegTSContext, fix_teletext_pts), AV_OPT_TYPE_BOOL,
191  {.i64 = 1}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
192  {"ts_packetsize", "output option carrying the raw packet size", offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
194  {"scan_all_pmts", "scan and combine all PMTs", offsetof(MpegTSContext, scan_all_pmts), AV_OPT_TYPE_BOOL,
195  {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM },
196  {"skip_unknown_pmt", "skip PMTs for programs not advertised in the PAT", offsetof(MpegTSContext, skip_unknown_pmt), AV_OPT_TYPE_BOOL,
197  {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
198  {"merge_pmt_versions", "re-use streams when PMT's version/pids change", offsetof(MpegTSContext, merge_pmt_versions), AV_OPT_TYPE_BOOL,
199  {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
200  {"skip_changes", "skip changing / adding streams / programs", offsetof(MpegTSContext, skip_changes), AV_OPT_TYPE_BOOL,
201  {.i64 = 0}, 0, 1, 0 },
202  {"skip_clear", "skip clearing programs", offsetof(MpegTSContext, skip_clear), AV_OPT_TYPE_BOOL,
203  {.i64 = 0}, 0, 1, 0 },
204  {"max_packet_size", "maximum size of emitted packet", offsetof(MpegTSContext, max_packet_size), AV_OPT_TYPE_INT,
205  {.i64 = 204800}, 1, INT_MAX/2, AV_OPT_FLAG_DECODING_PARAM },
206  { NULL },
207 };
208 
209 static const AVClass mpegts_class = {
210  .class_name = "mpegts demuxer",
211  .item_name = av_default_item_name,
212  .option = options,
213  .version = LIBAVUTIL_VERSION_INT,
214 };
215 
216 static const AVOption raw_options[] = {
218  { "compute_pcr", "compute exact PCR for each transport stream packet",
219  offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_BOOL,
220  { .i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
221  { "ts_packetsize", "output option carrying the raw packet size",
222  offsetof(MpegTSContext, raw_packet_size), AV_OPT_TYPE_INT,
223  { .i64 = 0 }, 0, 0,
225  { NULL },
226 };
227 
228 static const AVClass mpegtsraw_class = {
229  .class_name = "mpegtsraw demuxer",
230  .item_name = av_default_item_name,
231  .option = raw_options,
232  .version = LIBAVUTIL_VERSION_INT,
233 };
234 
235 /* TS stream handling */
236 
243 };
244 
245 /* enough for PES header + length */
246 #define PES_START_SIZE 6
247 #define PES_HEADER_SIZE 9
248 #define MAX_PES_HEADER_SIZE (9 + 255)
249 
250 typedef struct PESContext {
251  int pid;
252  int pcr_pid; /**< if -1 then all packets containing PCR are considered */
257  AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
259  /* used to get the format */
261  int flags; /**< copied to the AVPacket flags */
265  uint8_t stream_id;
266  int64_t pts, dts;
267  int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
272 } PESContext;
273 
274 extern const AVInputFormat ff_mpegts_demuxer;
275 
276 static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
277 {
278  int i;
279  for (i = 0; i < ts->nb_prg; i++) {
280  if (ts->prg[i].id == programid) {
281  return &ts->prg[i];
282  }
283  }
284  return NULL;
285 }
286 
287 static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
288 {
289  AVProgram *prg = NULL;
290  int i;
291 
292  for (i = 0; i < ts->stream->nb_programs; i++)
293  if (ts->stream->programs[i]->id == programid) {
294  prg = ts->stream->programs[i];
295  break;
296  }
297  if (!prg)
298  return;
299  prg->nb_stream_indexes = 0;
300 }
301 
302 static void clear_program(struct Program *p)
303 {
304  if (!p)
305  return;
306  p->nb_pids = 0;
307  p->nb_streams = 0;
308  p->pmt_found = 0;
309 }
310 
312 {
313  av_freep(&ts->prg);
314  ts->nb_prg = 0;
315 }
316 
317 static struct Program * add_program(MpegTSContext *ts, unsigned int programid)
318 {
319  struct Program *p = get_program(ts, programid);
320  if (p)
321  return p;
322  if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
323  ts->nb_prg = 0;
324  return NULL;
325  }
326  p = &ts->prg[ts->nb_prg];
327  p->id = programid;
328  clear_program(p);
329  ts->nb_prg++;
330  return p;
331 }
332 
333 static void add_pid_to_program(struct Program *p, unsigned int pid)
334 {
335  int i;
336  if (!p)
337  return;
338 
339  if (p->nb_pids >= MAX_PIDS_PER_PROGRAM)
340  return;
341 
342  for (i = 0; i < p->nb_pids; i++)
343  if (p->pids[i] == pid)
344  return;
345 
346  p->pids[p->nb_pids++] = pid;
347 }
348 
349 static void update_av_program_info(AVFormatContext *s, unsigned int programid,
350  unsigned int pid, int version)
351 {
352  int i;
353  for (i = 0; i < s->nb_programs; i++) {
354  AVProgram *program = s->programs[i];
355  if (program->id == programid) {
356  int old_pcr_pid = program->pcr_pid,
357  old_version = program->pmt_version;
358  program->pcr_pid = pid;
359  program->pmt_version = version;
360 
361  if (old_version != -1 && old_version != version) {
363  "detected PMT change (program=%d, version=%d/%d, pcr_pid=0x%x/0x%x)\n",
364  programid, old_version, version, old_pcr_pid, pid);
365  }
366  break;
367  }
368  }
369 }
370 
371 /**
372  * @brief discard_pid() decides if the pid is to be discarded according
373  * to caller's programs selection
374  * @param ts : - TS context
375  * @param pid : - pid
376  * @return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
377  * 0 otherwise
378  */
379 static int discard_pid(MpegTSContext *ts, unsigned int pid)
380 {
381  int i, j, k;
382  int used = 0, discarded = 0;
383  struct Program *p;
384 
385  if (pid == PAT_PID)
386  return 0;
387 
388  /* If none of the programs have .discard=AVDISCARD_ALL then there's
389  * no way we have to discard this packet */
390  for (k = 0; k < ts->stream->nb_programs; k++)
391  if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
392  break;
393  if (k == ts->stream->nb_programs)
394  return 0;
395 
396  for (i = 0; i < ts->nb_prg; i++) {
397  p = &ts->prg[i];
398  for (j = 0; j < p->nb_pids; j++) {
399  if (p->pids[j] != pid)
400  continue;
401  // is program with id p->id set to be discarded?
402  for (k = 0; k < ts->stream->nb_programs; k++) {
403  if (ts->stream->programs[k]->id == p->id) {
404  if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
405  discarded++;
406  else
407  used++;
408  }
409  }
410  }
411  }
412 
413  return !used && discarded;
414 }
415 
416 /**
417  * Assemble PES packets out of TS packets, and then call the "section_cb"
418  * function when they are complete.
419  */
421  const uint8_t *buf, int buf_size, int is_start)
422 {
423  MpegTSSectionFilter *tss = &tss1->u.section_filter;
424  uint8_t *cur_section_buf = NULL;
425  int len, offset;
426 
427  if (is_start) {
428  memcpy(tss->section_buf, buf, buf_size);
429  tss->section_index = buf_size;
430  tss->section_h_size = -1;
431  tss->end_of_section_reached = 0;
432  } else {
433  if (tss->end_of_section_reached)
434  return;
436  if (buf_size < len)
437  len = buf_size;
438  memcpy(tss->section_buf + tss->section_index, buf, len);
439  tss->section_index += len;
440  }
441 
442  offset = 0;
443  cur_section_buf = tss->section_buf;
444  while (cur_section_buf - tss->section_buf < MAX_SECTION_SIZE && cur_section_buf[0] != 0xff) {
445  /* compute section length if possible */
446  if (tss->section_h_size == -1 && tss->section_index - offset >= 3) {
447  len = (AV_RB16(cur_section_buf + 1) & 0xfff) + 3;
448  if (len > MAX_SECTION_SIZE)
449  return;
450  tss->section_h_size = len;
451  }
452 
453  if (tss->section_h_size != -1 &&
454  tss->section_index >= offset + tss->section_h_size) {
455  int crc_valid = 1;
456  tss->end_of_section_reached = 1;
457 
458  if (tss->check_crc) {
459  crc_valid = !av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1, cur_section_buf, tss->section_h_size);
460  if (tss->section_h_size >= 4)
461  tss->crc = AV_RB32(cur_section_buf + tss->section_h_size - 4);
462 
463  if (crc_valid) {
464  ts->crc_validity[ tss1->pid ] = 100;
465  }else if (ts->crc_validity[ tss1->pid ] > -10) {
466  ts->crc_validity[ tss1->pid ]--;
467  }else
468  crc_valid = 2;
469  }
470  if (crc_valid) {
471  tss->section_cb(tss1, cur_section_buf, tss->section_h_size);
472  if (crc_valid != 1)
473  tss->last_ver = -1;
474  }
475 
476  cur_section_buf += tss->section_h_size;
477  offset += tss->section_h_size;
478  tss->section_h_size = -1;
479  } else {
480  tss->section_h_size = -1;
481  tss->end_of_section_reached = 0;
482  break;
483  }
484  }
485 }
486 
487 static MpegTSFilter *mpegts_open_filter(MpegTSContext *ts, unsigned int pid,
488  enum MpegTSFilterType type)
489 {
491 
492  av_log(ts->stream, AV_LOG_TRACE, "Filter: pid=0x%x type=%d\n", pid, type);
493 
494  if (pid >= NB_PID_MAX || ts->pids[pid])
495  return NULL;
496  filter = av_mallocz(sizeof(MpegTSFilter));
497  if (!filter)
498  return NULL;
499  ts->pids[pid] = filter;
500 
501  filter->type = type;
502  filter->pid = pid;
503  filter->es_id = -1;
504  filter->last_cc = -1;
505  filter->last_pcr= -1;
506 
507  return filter;
508 }
509 
511  unsigned int pid,
512  SectionCallback *section_cb,
513  void *opaque,
514  int check_crc)
515 {
517  MpegTSSectionFilter *sec;
518  uint8_t *section_buf = av_mallocz(MAX_SECTION_SIZE);
519 
520  if (!section_buf)
521  return NULL;
522 
523  if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_SECTION))) {
524  av_free(section_buf);
525  return NULL;
526  }
527  sec = &filter->u.section_filter;
528  sec->section_cb = section_cb;
529  sec->opaque = opaque;
530  sec->section_buf = section_buf;
531  sec->check_crc = check_crc;
532  sec->last_ver = -1;
533 
534  return filter;
535 }
536 
537 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
538  PESCallback *pes_cb,
539  void *opaque)
540 {
542  MpegTSPESFilter *pes;
543 
544  if (!(filter = mpegts_open_filter(ts, pid, MPEGTS_PES)))
545  return NULL;
546 
547  pes = &filter->u.pes_filter;
548  pes->pes_cb = pes_cb;
549  pes->opaque = opaque;
550  return filter;
551 }
552 
553 static MpegTSFilter *mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid)
554 {
555  return mpegts_open_filter(ts, pid, MPEGTS_PCR);
556 }
557 
559 {
560  int pid;
561 
562  pid = filter->pid;
563  if (filter->type == MPEGTS_SECTION)
564  av_freep(&filter->u.section_filter.section_buf);
565  else if (filter->type == MPEGTS_PES) {
566  PESContext *pes = filter->u.pes_filter.opaque;
567  av_buffer_unref(&pes->buffer);
568  /* referenced private data will be freed later in
569  * avformat_close_input (pes->st->priv_data == pes) */
570  if (!pes->st || pes->merged_st) {
571  av_freep(&filter->u.pes_filter.opaque);
572  }
573  }
574 
575  av_free(filter);
576  ts->pids[pid] = NULL;
577 }
578 
579 static int analyze(const uint8_t *buf, int size, int packet_size,
580  int probe)
581 {
582  int stat[TS_MAX_PACKET_SIZE];
583  int stat_all = 0;
584  int i;
585  int best_score = 0;
586 
587  memset(stat, 0, packet_size * sizeof(*stat));
588 
589  for (i = 0; i < size - 3; i++) {
590  if (buf[i] == 0x47) {
591  int pid = AV_RB16(buf+1) & 0x1FFF;
592  int asc = buf[i + 3] & 0x30;
593  if (!probe || pid == 0x1FFF || asc) {
594  int x = i % packet_size;
595  stat[x]++;
596  stat_all++;
597  if (stat[x] > best_score) {
598  best_score = stat[x];
599  }
600  }
601  }
602  }
603 
604  return best_score - FFMAX(stat_all - 10*best_score, 0)/10;
605 }
606 
607 /* autodetect fec presence */
609 {
610  int score, fec_score, dvhs_score;
611  int margin;
612  int ret;
613 
614  /*init buffer to store stream for probing */
615  uint8_t buf[PROBE_PACKET_MAX_BUF] = {0};
616  int buf_size = 0;
617  int max_iterations = 16;
618 
619  while (buf_size < PROBE_PACKET_MAX_BUF && max_iterations--) {
620  ret = avio_read_partial(s->pb, buf + buf_size, PROBE_PACKET_MAX_BUF - buf_size);
621  if (ret < 0)
622  return AVERROR_INVALIDDATA;
623  buf_size += ret;
624 
625  score = analyze(buf, buf_size, TS_PACKET_SIZE, 0);
626  dvhs_score = analyze(buf, buf_size, TS_DVHS_PACKET_SIZE, 0);
627  fec_score = analyze(buf, buf_size, TS_FEC_PACKET_SIZE, 0);
628  av_log(s, AV_LOG_TRACE, "Probe: %d, score: %d, dvhs_score: %d, fec_score: %d \n",
629  buf_size, score, dvhs_score, fec_score);
630 
631  margin = mid_pred(score, fec_score, dvhs_score);
632 
633  if (buf_size < PROBE_PACKET_MAX_BUF)
634  margin += PROBE_PACKET_MARGIN; /*if buffer not filled */
635 
636  if (score > margin)
637  return TS_PACKET_SIZE;
638  else if (dvhs_score > margin)
639  return TS_DVHS_PACKET_SIZE;
640  else if (fec_score > margin)
641  return TS_FEC_PACKET_SIZE;
642  }
643  return AVERROR_INVALIDDATA;
644 }
645 
646 typedef struct SectionHeader {
647  uint8_t tid;
648  uint16_t id;
649  uint8_t version;
650  uint8_t current_next;
651  uint8_t sec_num;
652  uint8_t last_sec_num;
653 } SectionHeader;
654 
656 {
657  if (h->version == tssf->last_ver && tssf->last_crc == tssf->crc)
658  return 1;
659 
660  tssf->last_ver = h->version;
661  tssf->last_crc = tssf->crc;
662 
663  return 0;
664 }
665 
666 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
667 {
668  const uint8_t *p;
669  int c;
670 
671  p = *pp;
672  if (p >= p_end)
673  return AVERROR_INVALIDDATA;
674  c = *p++;
675  *pp = p;
676  return c;
677 }
678 
679 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
680 {
681  const uint8_t *p;
682  int c;
683 
684  p = *pp;
685  if (1 >= p_end - p)
686  return AVERROR_INVALIDDATA;
687  c = AV_RB16(p);
688  p += 2;
689  *pp = p;
690  return c;
691 }
692 
693 /* read and allocate a DVB string preceded by its length */
694 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
695 {
696  int len;
697  const uint8_t *p;
698  char *str;
699 
700  p = *pp;
701  len = get8(&p, p_end);
702  if (len < 0)
703  return NULL;
704  if (len > p_end - p)
705  return NULL;
706 #if CONFIG_ICONV
707  if (len) {
708  const char *encodings[] = {
709  "ISO6937", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7",
710  "ISO-8859-8", "ISO-8859-9", "ISO-8859-10", "ISO-8859-11",
711  "", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "", "", "", "",
712  "", "UCS-2BE", "KSC_5601", "GB2312", "UCS-2BE", "UTF-8", "", "",
713  "", "", "", "", "", "", "", ""
714  };
715  iconv_t cd;
716  char *in, *out;
717  size_t inlen = len, outlen = inlen * 6 + 1;
718  if (len >= 3 && p[0] == 0x10 && !p[1] && p[2] && p[2] <= 0xf && p[2] != 0xc) {
719  char iso8859[12];
720  snprintf(iso8859, sizeof(iso8859), "ISO-8859-%d", p[2]);
721  inlen -= 3;
722  in = (char *)p + 3;
723  cd = iconv_open("UTF-8", iso8859);
724  } else if (p[0] < 0x20) {
725  inlen -= 1;
726  in = (char *)p + 1;
727  cd = iconv_open("UTF-8", encodings[*p]);
728  } else {
729  in = (char *)p;
730  cd = iconv_open("UTF-8", encodings[0]);
731  }
732  if (cd == (iconv_t)-1)
733  goto no_iconv;
734  str = out = av_malloc(outlen);
735  if (!str) {
736  iconv_close(cd);
737  return NULL;
738  }
739  if (iconv(cd, &in, &inlen, &out, &outlen) == -1) {
740  iconv_close(cd);
741  av_freep(&str);
742  goto no_iconv;
743  }
744  iconv_close(cd);
745  *out = 0;
746  *pp = p + len;
747  return str;
748  }
749 no_iconv:
750 #endif
751  str = av_malloc(len + 1);
752  if (!str)
753  return NULL;
754  memcpy(str, p, len);
755  str[len] = '\0';
756  p += len;
757  *pp = p;
758  return str;
759 }
760 
762  const uint8_t **pp, const uint8_t *p_end)
763 {
764  int val;
765 
766  val = get8(pp, p_end);
767  if (val < 0)
768  return val;
769  h->tid = val;
770  *pp += 2;
771  val = get16(pp, p_end);
772  if (val < 0)
773  return val;
774  h->id = val;
775  val = get8(pp, p_end);
776  if (val < 0)
777  return val;
778  h->version = (val >> 1) & 0x1f;
779  h->current_next = val & 0x01;
780  val = get8(pp, p_end);
781  if (val < 0)
782  return val;
783  h->sec_num = val;
784  val = get8(pp, p_end);
785  if (val < 0)
786  return val;
787  h->last_sec_num = val;
788  return 0;
789 }
790 
791 typedef struct StreamType {
792  uint32_t stream_type;
795 } StreamType;
796 
797 static const StreamType ISO_types[] = {
804  /* Makito encoder sets stream type 0x11 for AAC,
805  * so auto-detect LOAS/LATM instead of hardcoding it. */
806 #if !CONFIG_LOAS_DEMUXER
807  { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
808 #endif
819  { 0 },
820 };
821 
822 static const StreamType HDMV_types[] = {
828  { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
829  { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
830  { 0xa1, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC3 Secondary Audio */
831  { 0xa2, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS Express Secondary Audio */
834  { 0 },
835 };
836 
837 /* SCTE types */
838 static const StreamType SCTE_types[] = {
840  { 0 },
841 };
842 
843 /* ATSC ? */
844 static const StreamType MISC_types[] = {
847  { 0 },
848 };
849 
850 /* HLS Sample Encryption Types */
856  { 0 },
857 };
858 
859 static const StreamType REGD_types[] = {
860  { MKTAG('d', 'r', 'a', 'c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
861  { MKTAG('A', 'C', '-', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
862  { MKTAG('B', 'S', 'S', 'D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
863  { MKTAG('D', 'T', 'S', '1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
864  { MKTAG('D', 'T', 'S', '2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
865  { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
866  { MKTAG('E', 'A', 'C', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 },
867  { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
868  { MKTAG('K', 'L', 'V', 'A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
869  { MKTAG('I', 'D', '3', ' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
870  { MKTAG('V', 'C', '-', '1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
871  { MKTAG('O', 'p', 'u', 's'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_OPUS },
872  { 0 },
873 };
874 
875 static const StreamType METADATA_types[] = {
876  { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA, AV_CODEC_ID_SMPTE_KLV },
877  { MKTAG('I','D','3',' '), AVMEDIA_TYPE_DATA, AV_CODEC_ID_TIMED_ID3 },
878  { 0 },
879 };
880 
881 /* descriptor present */
882 static const StreamType DESC_types[] = {
883  { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
884  { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
887  { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
888  { 0 },
889 };
890 
892  uint32_t stream_type,
893  const StreamType *types)
894 {
895  FFStream *const sti = ffstream(st);
896  for (; types->stream_type; types++)
897  if (stream_type == types->stream_type) {
898  if (st->codecpar->codec_type != types->codec_type ||
899  st->codecpar->codec_id != types->codec_id) {
900  st->codecpar->codec_type = types->codec_type;
901  st->codecpar->codec_id = types->codec_id;
902  sti->need_context_update = 1;
903  }
904  sti->request_probe = 0;
905  return;
906  }
907 }
908 
910  uint32_t stream_type, uint32_t prog_reg_desc)
911 {
912  FFStream *const sti = ffstream(st);
913  int old_codec_type = st->codecpar->codec_type;
914  int old_codec_id = st->codecpar->codec_id;
915  int old_codec_tag = st->codecpar->codec_tag;
916 
917  if (avcodec_is_open(sti->avctx)) {
918  av_log(pes->stream, AV_LOG_DEBUG, "cannot set stream info, internal codec is open\n");
919  return 0;
920  }
921 
922  avpriv_set_pts_info(st, 33, 1, 90000);
923  st->priv_data = pes;
927  pes->st = st;
928  pes->stream_type = stream_type;
929 
930  av_log(pes->stream, AV_LOG_DEBUG,
931  "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
932  st->index, pes->stream_type, pes->pid, (char *)&prog_reg_desc);
933 
934  st->codecpar->codec_tag = pes->stream_type;
935 
937  if (pes->stream_type == 4 || pes->stream_type == 0x0f)
938  sti->request_probe = 50;
939  if ((prog_reg_desc == AV_RL32("HDMV") ||
940  prog_reg_desc == AV_RL32("HDPR")) &&
943  if (pes->stream_type == 0x83) {
944  // HDMV TrueHD streams also contain an AC3 coded version of the
945  // audio track - add a second stream for this
946  AVStream *sub_st;
947  // priv_data cannot be shared between streams
948  PESContext *sub_pes = av_memdup(pes, sizeof(*sub_pes));
949  if (!sub_pes)
950  return AVERROR(ENOMEM);
951 
952  sub_st = avformat_new_stream(pes->stream, NULL);
953  if (!sub_st) {
954  av_free(sub_pes);
955  return AVERROR(ENOMEM);
956  }
957 
958  sub_st->id = pes->pid;
959  avpriv_set_pts_info(sub_st, 33, 1, 90000);
960  sub_st->priv_data = sub_pes;
962  sub_st->codecpar->codec_id = AV_CODEC_ID_AC3;
964  sub_pes->sub_st = pes->sub_st = sub_st;
965  }
966  }
967  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
969  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
971  if (st->codecpar->codec_id == AV_CODEC_ID_NONE) {
972  st->codecpar->codec_id = old_codec_id;
973  st->codecpar->codec_type = old_codec_type;
974  }
975  if ((st->codecpar->codec_id == AV_CODEC_ID_NONE ||
976  (sti->request_probe > 0 && sti->request_probe < AVPROBE_SCORE_STREAM_RETRY / 5)) &&
977  sti->probe_packets > 0 &&
978  stream_type == STREAM_TYPE_PRIVATE_DATA) {
982  }
983 
984  /* queue a context update if properties changed */
985  if (old_codec_type != st->codecpar->codec_type ||
986  old_codec_id != st->codecpar->codec_id ||
987  old_codec_tag != st->codecpar->codec_tag)
988  sti->need_context_update = 1;
989 
990  return 0;
991 }
992 
994 {
995  pes->pts = AV_NOPTS_VALUE;
996  pes->dts = AV_NOPTS_VALUE;
997  pes->data_index = 0;
998  pes->flags = 0;
999  av_buffer_unref(&pes->buffer);
1000 }
1001 
1002 static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
1003 {
1005  pkt->data = (uint8_t *)buffer;
1006  pkt->size = len;
1007 }
1008 
1010 {
1011  uint8_t *sd;
1012 
1014 
1015  pkt->buf = pes->buffer;
1016  pkt->data = pes->buffer->data;
1017  pkt->size = pes->data_index;
1018 
1019  if (pes->PES_packet_length &&
1020  pes->pes_header_size + pes->data_index != pes->PES_packet_length +
1021  PES_START_SIZE) {
1022  av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
1023  pes->flags |= AV_PKT_FLAG_CORRUPT;
1024  }
1025  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
1026 
1027  // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
1028  if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
1029  pkt->stream_index = pes->sub_st->index;
1030  else
1031  pkt->stream_index = pes->st->index;
1032  pkt->pts = pes->pts;
1033  pkt->dts = pes->dts;
1034  /* store position of first TS packet of this PES packet */
1035  pkt->pos = pes->ts_packet_pos;
1036  pkt->flags = pes->flags;
1037 
1038  pes->buffer = NULL;
1040 
1042  if (!sd)
1043  return AVERROR(ENOMEM);
1044  *sd = pes->stream_id;
1045 
1046  return 0;
1047 }
1048 
1049 static uint64_t get_ts64(GetBitContext *gb, int bits)
1050 {
1051  if (get_bits_left(gb) < bits)
1052  return AV_NOPTS_VALUE;
1053  return get_bits64(gb, bits);
1054 }
1055 
1057  const uint8_t *buf, int buf_size)
1058 {
1059  GetBitContext gb;
1060  int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
1061  int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
1062  int dts_flag = -1, cts_flag = -1;
1063  int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
1064  uint8_t buf_padded[128 + AV_INPUT_BUFFER_PADDING_SIZE];
1065  int buf_padded_size = FFMIN(buf_size, sizeof(buf_padded) - AV_INPUT_BUFFER_PADDING_SIZE);
1066 
1067  memcpy(buf_padded, buf, buf_padded_size);
1068 
1069  init_get_bits(&gb, buf_padded, buf_padded_size * 8);
1070 
1071  if (sl->use_au_start)
1072  au_start_flag = get_bits1(&gb);
1073  if (sl->use_au_end)
1074  au_end_flag = get_bits1(&gb);
1075  if (!sl->use_au_start && !sl->use_au_end)
1076  au_start_flag = au_end_flag = 1;
1077  if (sl->ocr_len > 0)
1078  ocr_flag = get_bits1(&gb);
1079  if (sl->use_idle)
1080  idle_flag = get_bits1(&gb);
1081  if (sl->use_padding)
1082  padding_flag = get_bits1(&gb);
1083  if (padding_flag)
1084  padding_bits = get_bits(&gb, 3);
1085 
1086  if (!idle_flag && (!padding_flag || padding_bits != 0)) {
1087  if (sl->packet_seq_num_len)
1089  if (sl->degr_prior_len)
1090  if (get_bits1(&gb))
1091  skip_bits(&gb, sl->degr_prior_len);
1092  if (ocr_flag)
1093  skip_bits_long(&gb, sl->ocr_len);
1094  if (au_start_flag) {
1095  if (sl->use_rand_acc_pt)
1096  get_bits1(&gb);
1097  if (sl->au_seq_num_len > 0)
1098  skip_bits_long(&gb, sl->au_seq_num_len);
1099  if (sl->use_timestamps) {
1100  dts_flag = get_bits1(&gb);
1101  cts_flag = get_bits1(&gb);
1102  }
1103  }
1104  if (sl->inst_bitrate_len)
1105  inst_bitrate_flag = get_bits1(&gb);
1106  if (dts_flag == 1)
1107  dts = get_ts64(&gb, sl->timestamp_len);
1108  if (cts_flag == 1)
1109  cts = get_ts64(&gb, sl->timestamp_len);
1110  if (sl->au_len > 0)
1111  skip_bits_long(&gb, sl->au_len);
1112  if (inst_bitrate_flag)
1113  skip_bits_long(&gb, sl->inst_bitrate_len);
1114  }
1115 
1116  if (dts != AV_NOPTS_VALUE)
1117  pes->dts = dts;
1118  if (cts != AV_NOPTS_VALUE)
1119  pes->pts = cts;
1120 
1121  if (sl->timestamp_len && sl->timestamp_res)
1123 
1124  return (get_bits_count(&gb) + 7) >> 3;
1125 }
1126 
1128 {
1130  if (!ts->pools[index]) {
1131  int pool_size = FFMIN(ts->max_packet_size + AV_INPUT_BUFFER_PADDING_SIZE, 2 << index);
1132  ts->pools[index] = av_buffer_pool_init(pool_size, NULL);
1133  if (!ts->pools[index])
1134  return NULL;
1135  }
1136  return av_buffer_pool_get(ts->pools[index]);
1137 }
1138 
1139 /* return non zero if a packet could be constructed */
1141  const uint8_t *buf, int buf_size, int is_start,
1142  int64_t pos)
1143 {
1144  PESContext *pes = filter->u.pes_filter.opaque;
1145  MpegTSContext *ts = pes->ts;
1146  const uint8_t *p;
1147  int ret, len;
1148 
1149  if (!ts->pkt)
1150  return 0;
1151 
1152  if (is_start) {
1153  if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1154  ret = new_pes_packet(pes, ts->pkt);
1155  if (ret < 0)
1156  return ret;
1157  ts->stop_parse = 1;
1158  } else {
1160  }
1161  pes->state = MPEGTS_HEADER;
1162  pes->ts_packet_pos = pos;
1163  }
1164  p = buf;
1165  while (buf_size > 0) {
1166  switch (pes->state) {
1167  case MPEGTS_HEADER:
1168  len = PES_START_SIZE - pes->data_index;
1169  if (len > buf_size)
1170  len = buf_size;
1171  memcpy(pes->header + pes->data_index, p, len);
1172  pes->data_index += len;
1173  p += len;
1174  buf_size -= len;
1175  if (pes->data_index == PES_START_SIZE) {
1176  /* we got all the PES or section header. We can now
1177  * decide */
1178  if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
1179  pes->header[2] == 0x01) {
1180  /* it must be an MPEG-2 PES stream */
1181  pes->stream_id = pes->header[3];
1182  av_log(pes->stream, AV_LOG_TRACE, "pid=%x stream_id=%#x\n", pes->pid, pes->stream_id);
1183 
1184  if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
1185  (!pes->sub_st ||
1186  pes->sub_st->discard == AVDISCARD_ALL)) ||
1188  goto skip;
1189 
1190  /* stream not present in PMT */
1191  if (!pes->st) {
1192  if (ts->skip_changes)
1193  goto skip;
1194  if (ts->merge_pmt_versions)
1195  goto skip; /* wait for PMT to merge new stream */
1196 
1197  pes->st = avformat_new_stream(ts->stream, NULL);
1198  if (!pes->st)
1199  return AVERROR(ENOMEM);
1200  pes->st->id = pes->pid;
1201  mpegts_set_stream_info(pes->st, pes, 0, 0);
1202  }
1203 
1204  pes->PES_packet_length = AV_RB16(pes->header + 4);
1205  /* NOTE: zero length means the PES size is unbounded */
1206 
1209  pes->stream_id != STREAM_ID_ECM_STREAM &&
1210  pes->stream_id != STREAM_ID_EMM_STREAM &&
1214  FFStream *const pes_sti = ffstream(pes->st);
1215  pes->state = MPEGTS_PESHEADER;
1216  if (pes->st->codecpar->codec_id == AV_CODEC_ID_NONE && !pes_sti->request_probe) {
1217  av_log(pes->stream, AV_LOG_TRACE,
1218  "pid=%x stream_type=%x probing\n",
1219  pes->pid,
1220  pes->stream_type);
1221  pes_sti->request_probe = 1;
1222  }
1223  } else {
1224  pes->pes_header_size = 6;
1225  pes->state = MPEGTS_PAYLOAD;
1226  pes->data_index = 0;
1227  }
1228  } else {
1229  /* otherwise, it should be a table */
1230  /* skip packet */
1231 skip:
1232  pes->state = MPEGTS_SKIP;
1233  continue;
1234  }
1235  }
1236  break;
1237  /**********************************************/
1238  /* PES packing parsing */
1239  case MPEGTS_PESHEADER:
1240  len = PES_HEADER_SIZE - pes->data_index;
1241  if (len < 0)
1242  return AVERROR_INVALIDDATA;
1243  if (len > buf_size)
1244  len = buf_size;
1245  memcpy(pes->header + pes->data_index, p, len);
1246  pes->data_index += len;
1247  p += len;
1248  buf_size -= len;
1249  if (pes->data_index == PES_HEADER_SIZE) {
1250  pes->pes_header_size = pes->header[8] + 9;
1252  }
1253  break;
1254  case MPEGTS_PESHEADER_FILL:
1255  len = pes->pes_header_size - pes->data_index;
1256  if (len < 0)
1257  return AVERROR_INVALIDDATA;
1258  if (len > buf_size)
1259  len = buf_size;
1260  memcpy(pes->header + pes->data_index, p, len);
1261  pes->data_index += len;
1262  p += len;
1263  buf_size -= len;
1264  if (pes->data_index == pes->pes_header_size) {
1265  const uint8_t *r;
1266  unsigned int flags, pes_ext, skip;
1267 
1268  flags = pes->header[7];
1269  r = pes->header + 9;
1270  pes->pts = AV_NOPTS_VALUE;
1271  pes->dts = AV_NOPTS_VALUE;
1272  if ((flags & 0xc0) == 0x80) {
1273  pes->dts = pes->pts = ff_parse_pes_pts(r);
1274  r += 5;
1275  } else if ((flags & 0xc0) == 0xc0) {
1276  pes->pts = ff_parse_pes_pts(r);
1277  r += 5;
1278  pes->dts = ff_parse_pes_pts(r);
1279  r += 5;
1280  }
1281  pes->extended_stream_id = -1;
1282  if (flags & 0x01) { /* PES extension */
1283  pes_ext = *r++;
1284  /* Skip PES private data, program packet sequence counter and P-STD buffer */
1285  skip = (pes_ext >> 4) & 0xb;
1286  skip += skip & 0x9;
1287  r += skip;
1288  if ((pes_ext & 0x41) == 0x01 &&
1289  (r + 2) <= (pes->header + pes->pes_header_size)) {
1290  /* PES extension 2 */
1291  if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
1292  pes->extended_stream_id = r[1];
1293  }
1294  }
1295 
1296  /* we got the full header. We parse it and get the payload */
1297  pes->state = MPEGTS_PAYLOAD;
1298  pes->data_index = 0;
1299  if (pes->stream_type == 0x12 && buf_size > 0) {
1300  int sl_header_bytes = read_sl_header(pes, &pes->sl, p,
1301  buf_size);
1302  pes->pes_header_size += sl_header_bytes;
1303  p += sl_header_bytes;
1304  buf_size -= sl_header_bytes;
1305  }
1306  if (pes->stream_type == 0x15 && buf_size >= 5) {
1307  /* skip metadata access unit header */
1308  pes->pes_header_size += 5;
1309  p += 5;
1310  buf_size -= 5;
1311  }
1312  if ( pes->ts->fix_teletext_pts
1315  ) {
1316  AVProgram *p = NULL;
1317  int pcr_found = 0;
1318  while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
1319  if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
1320  MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
1321  if (f) {
1322  AVStream *st = NULL;
1323  if (f->type == MPEGTS_PES) {
1324  PESContext *pcrpes = f->u.pes_filter.opaque;
1325  if (pcrpes)
1326  st = pcrpes->st;
1327  } else if (f->type == MPEGTS_PCR) {
1328  int i;
1329  for (i = 0; i < p->nb_stream_indexes; i++) {
1330  AVStream *pst = pes->stream->streams[p->stream_index[i]];
1331  if (pst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1332  st = pst;
1333  }
1334  }
1335  if (f->last_pcr != -1 && !f->discard) {
1336  // teletext packets do not always have correct timestamps,
1337  // the standard says they should be handled after 40.6 ms at most,
1338  // and the pcr error to this packet should be no more than 100 ms.
1339  // TODO: we should interpolate the PCR, not just use the last one
1340  int64_t pcr = f->last_pcr / 300;
1341  pcr_found = 1;
1342  if (st) {
1343  const FFStream *const sti = ffstream(st);
1344  FFStream *const pes_sti = ffstream(pes->st);
1345 
1346  pes_sti->pts_wrap_reference = sti->pts_wrap_reference;
1347  pes_sti->pts_wrap_behavior = sti->pts_wrap_behavior;
1348  }
1349  if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
1350  pes->pts = pes->dts = pcr;
1351  } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1352  pes->dts > pcr + 3654 + 9000) {
1353  pes->pts = pes->dts = pcr + 3654 + 9000;
1354  } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1355  pes->dts > pcr + 10*90000) { //10sec
1356  pes->pts = pes->dts = pcr + 3654 + 9000;
1357  }
1358  break;
1359  }
1360  }
1361  }
1362  }
1363 
1364  if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1365  !pcr_found) {
1367  "Forcing DTS/PTS to be unset for a "
1368  "non-trustworthy PES packet for PID %d as "
1369  "PCR hasn't been received yet.\n",
1370  pes->pid);
1371  pes->dts = pes->pts = AV_NOPTS_VALUE;
1372  }
1373  }
1374  }
1375  break;
1376  case MPEGTS_PAYLOAD:
1377  do {
1378  int max_packet_size = ts->max_packet_size;
1380  max_packet_size = pes->PES_packet_length + PES_START_SIZE - pes->pes_header_size;
1381 
1382  if (pes->data_index > 0 &&
1383  pes->data_index + buf_size > max_packet_size) {
1384  ret = new_pes_packet(pes, ts->pkt);
1385  if (ret < 0)
1386  return ret;
1387  pes->PES_packet_length = 0;
1388  max_packet_size = ts->max_packet_size;
1389  ts->stop_parse = 1;
1390  } else if (pes->data_index == 0 &&
1391  buf_size > max_packet_size) {
1392  // pes packet size is < ts size packet and pes data is padded with 0xff
1393  // not sure if this is legal in ts but see issue #2392
1394  buf_size = max_packet_size;
1395  }
1396 
1397  if (!pes->buffer) {
1398  pes->buffer = buffer_pool_get(ts, max_packet_size);
1399  if (!pes->buffer)
1400  return AVERROR(ENOMEM);
1401  }
1402 
1403  memcpy(pes->buffer->data + pes->data_index, p, buf_size);
1404  pes->data_index += buf_size;
1405  /* emit complete packets with known packet size
1406  * decreases demuxer delay for infrequent packets like subtitles from
1407  * a couple of seconds to milliseconds for properly muxed files. */
1408  if (!ts->stop_parse && pes->PES_packet_length &&
1410  ts->stop_parse = 1;
1411  ret = new_pes_packet(pes, ts->pkt);
1412  pes->state = MPEGTS_SKIP;
1413  if (ret < 0)
1414  return ret;
1415  }
1416  } while (0);
1417  buf_size = 0;
1418  break;
1419  case MPEGTS_SKIP:
1420  buf_size = 0;
1421  break;
1422  }
1423  }
1424 
1425  return 0;
1426 }
1427 
1428 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
1429 {
1430  MpegTSFilter *tss;
1431  PESContext *pes;
1432 
1433  /* if no pid found, then add a pid context */
1434  pes = av_mallocz(sizeof(PESContext));
1435  if (!pes)
1436  return 0;
1437  pes->ts = ts;
1438  pes->stream = ts->stream;
1439  pes->pid = pid;
1440  pes->pcr_pid = pcr_pid;
1441  pes->state = MPEGTS_SKIP;
1442  pes->pts = AV_NOPTS_VALUE;
1443  pes->dts = AV_NOPTS_VALUE;
1444  tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1445  if (!tss) {
1446  av_free(pes);
1447  return 0;
1448  }
1449  return pes;
1450 }
1451 
1452 #define MAX_LEVEL 4
1453 typedef struct MP4DescrParseContext {
1460  int level;
1463 
1465  const uint8_t *buf, unsigned size,
1466  Mp4Descr *descr, int max_descr_count)
1467 {
1468  if (size > (1 << 30))
1469  return AVERROR_INVALIDDATA;
1470 
1471  ffio_init_context(&d->pb, (unsigned char *)buf, size,
1472  0, NULL, NULL, NULL, NULL);
1473 
1474  d->s = s;
1475  d->level = 0;
1476  d->descr_count = 0;
1477  d->descr = descr;
1478  d->active_descr = NULL;
1479  d->max_descr_count = max_descr_count;
1480 
1481  return 0;
1482 }
1483 
1484 static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1485 {
1486  int64_t new_off = avio_tell(pb);
1487  (*len) -= new_off - *off;
1488  *off = new_off;
1489 }
1490 
1491 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1492  int target_tag);
1493 
1494 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1495 {
1496  while (len > 0) {
1497  int ret = parse_mp4_descr(d, off, len, 0);
1498  if (ret < 0)
1499  return ret;
1500  update_offsets(&d->pb.pub, &off, &len);
1501  }
1502  return 0;
1503 }
1504 
1505 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1506 {
1507  AVIOContext *const pb = &d->pb.pub;
1508  avio_rb16(pb); // ID
1509  avio_r8(pb);
1510  avio_r8(pb);
1511  avio_r8(pb);
1512  avio_r8(pb);
1513  avio_r8(pb);
1514  update_offsets(pb, &off, &len);
1515  return parse_mp4_descr_arr(d, off, len);
1516 }
1517 
1518 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1519 {
1520  int id_flags;
1521  if (len < 2)
1522  return 0;
1523  id_flags = avio_rb16(&d->pb.pub);
1524  if (!(id_flags & 0x0020)) { // URL_Flag
1525  update_offsets(&d->pb.pub, &off, &len);
1526  return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1527  } else {
1528  return 0;
1529  }
1530 }
1531 
1532 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1533 {
1534  AVIOContext *const pb = &d->pb.pub;
1535  int es_id = 0;
1536  int ret = 0;
1537 
1538  if (d->descr_count >= d->max_descr_count)
1539  return AVERROR_INVALIDDATA;
1540  ff_mp4_parse_es_descr(pb, &es_id);
1541  d->active_descr = d->descr + (d->descr_count++);
1542 
1543  d->active_descr->es_id = es_id;
1544  update_offsets(pb, &off, &len);
1545  if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
1546  return ret;
1547  update_offsets(pb, &off, &len);
1548  if (len > 0)
1550  d->active_descr = NULL;
1551  return ret;
1552 }
1553 
1555  int len)
1556 {
1557  Mp4Descr *descr = d->active_descr;
1558  if (!descr)
1559  return AVERROR_INVALIDDATA;
1560  d->active_descr->dec_config_descr = av_malloc(len);
1561  if (!descr->dec_config_descr)
1562  return AVERROR(ENOMEM);
1563  descr->dec_config_descr_len = len;
1564  avio_read(&d->pb.pub, descr->dec_config_descr, len);
1565  return 0;
1566 }
1567 
1568 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1569 {
1570  Mp4Descr *descr = d->active_descr;
1571  AVIOContext *const pb = &d->pb.pub;
1572  int predefined;
1573  if (!descr)
1574  return AVERROR_INVALIDDATA;
1575 
1576 #define R8_CHECK_CLIP_MAX(dst, maxv) do { \
1577  descr->sl.dst = avio_r8(pb); \
1578  if (descr->sl.dst > maxv) { \
1579  descr->sl.dst = maxv; \
1580  return AVERROR_INVALIDDATA; \
1581  } \
1582 } while (0)
1583 
1584  predefined = avio_r8(pb);
1585  if (!predefined) {
1586  int lengths;
1587  int flags = avio_r8(pb);
1588  descr->sl.use_au_start = !!(flags & 0x80);
1589  descr->sl.use_au_end = !!(flags & 0x40);
1590  descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1591  descr->sl.use_padding = !!(flags & 0x08);
1592  descr->sl.use_timestamps = !!(flags & 0x04);
1593  descr->sl.use_idle = !!(flags & 0x02);
1594  descr->sl.timestamp_res = avio_rb32(pb);
1595  avio_rb32(pb);
1596  R8_CHECK_CLIP_MAX(timestamp_len, 63);
1597  R8_CHECK_CLIP_MAX(ocr_len, 63);
1598  R8_CHECK_CLIP_MAX(au_len, 31);
1599  descr->sl.inst_bitrate_len = avio_r8(pb);
1600  lengths = avio_rb16(pb);
1601  descr->sl.degr_prior_len = lengths >> 12;
1602  descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1603  descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1604  } else if (!d->predefined_SLConfigDescriptor_seen){
1605  avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1606  d->predefined_SLConfigDescriptor_seen = 1;
1607  }
1608  return 0;
1609 }
1610 
1611 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1612  int target_tag)
1613 {
1614  int tag;
1615  AVIOContext *const pb = &d->pb.pub;
1616  int len1 = ff_mp4_read_descr(d->s, pb, &tag);
1617  int ret = 0;
1618 
1619  update_offsets(pb, &off, &len);
1620  if (len < 0 || len1 > len || len1 <= 0) {
1621  av_log(d->s, AV_LOG_ERROR,
1622  "Tag %x length violation new length %d bytes remaining %d\n",
1623  tag, len1, len);
1624  return AVERROR_INVALIDDATA;
1625  }
1626 
1627  if (d->level++ >= MAX_LEVEL) {
1628  av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1630  goto done;
1631  }
1632 
1633  if (target_tag && tag != target_tag) {
1634  av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1635  target_tag);
1637  goto done;
1638  }
1639 
1640  switch (tag) {
1641  case MP4IODescrTag:
1642  ret = parse_MP4IODescrTag(d, off, len1);
1643  break;
1644  case MP4ODescrTag:
1645  ret = parse_MP4ODescrTag(d, off, len1);
1646  break;
1647  case MP4ESDescrTag:
1648  ret = parse_MP4ESDescrTag(d, off, len1);
1649  break;
1650  case MP4DecConfigDescrTag:
1651  ret = parse_MP4DecConfigDescrTag(d, off, len1);
1652  break;
1653  case MP4SLDescrTag:
1654  ret = parse_MP4SLDescrTag(d, off, len1);
1655  break;
1656  }
1657 
1658 
1659 done:
1660  d->level--;
1661  avio_seek(pb, off + len1, SEEK_SET);
1662  return ret;
1663 }
1664 
1665 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1666  Mp4Descr *descr, int *descr_count, int max_descr_count)
1667 {
1669  int ret;
1670 
1671  ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1672  if (ret < 0)
1673  return ret;
1674 
1675  ret = parse_mp4_descr(&d, avio_tell(&d.pb.pub), size, MP4IODescrTag);
1676 
1677  *descr_count = d.descr_count;
1678  return ret;
1679 }
1680 
1681 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1682  Mp4Descr *descr, int *descr_count, int max_descr_count)
1683 {
1685  int ret;
1686 
1687  ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1688  if (ret < 0)
1689  return ret;
1690 
1691  ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb.pub), size);
1692 
1693  *descr_count = d.descr_count;
1694  return ret;
1695 }
1696 
1697 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section,
1698  int section_len)
1699 {
1700  MpegTSContext *ts = filter->u.section_filter.opaque;
1701  MpegTSSectionFilter *tssf = &filter->u.section_filter;
1702  SectionHeader h;
1703  const uint8_t *p, *p_end;
1704  int mp4_descr_count = 0;
1705  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1706  int i, pid;
1707  AVFormatContext *s = ts->stream;
1708 
1709  p_end = section + section_len - 4;
1710  p = section;
1711  if (parse_section_header(&h, &p, p_end) < 0)
1712  return;
1713  if (h.tid != M4OD_TID)
1714  return;
1715  if (skip_identical(&h, tssf))
1716  return;
1717 
1718  mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1720 
1721  for (pid = 0; pid < NB_PID_MAX; pid++) {
1722  if (!ts->pids[pid])
1723  continue;
1724  for (i = 0; i < mp4_descr_count; i++) {
1725  PESContext *pes;
1726  AVStream *st;
1727  FFStream *sti;
1728  FFIOContext pb;
1729  if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1730  continue;
1731  if (ts->pids[pid]->type != MPEGTS_PES) {
1732  av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1733  continue;
1734  }
1735  pes = ts->pids[pid]->u.pes_filter.opaque;
1736  st = pes->st;
1737  if (!st)
1738  continue;
1739  sti = ffstream(st);
1740 
1741  pes->sl = mp4_descr[i].sl;
1742 
1743  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1744  mp4_descr[i].dec_config_descr_len, 0,
1745  NULL, NULL, NULL, NULL);
1747  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1748  st->codecpar->extradata_size > 0)
1749  sti->need_parsing = 0;
1750  if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
1751  st->codecpar->extradata_size > 0)
1752  sti->need_parsing = 0;
1753 
1755  sti->need_context_update = 1;
1756  }
1757  }
1758  for (i = 0; i < mp4_descr_count; i++)
1759  av_free(mp4_descr[i].dec_config_descr);
1760 }
1761 
1762 static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section,
1763  int section_len)
1764 {
1765  AVProgram *prg = NULL;
1766  MpegTSContext *ts = filter->u.section_filter.opaque;
1767 
1768  int idx = ff_find_stream_index(ts->stream, filter->pid);
1769  if (idx < 0)
1770  return;
1771 
1772  /**
1773  * In case we receive an SCTE-35 packet before mpegts context is fully
1774  * initialized.
1775  */
1776  if (!ts->pkt)
1777  return;
1778 
1779  new_data_packet(section, section_len, ts->pkt);
1780  ts->pkt->stream_index = idx;
1781  prg = av_find_program_from_stream(ts->stream, NULL, idx);
1782  if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) {
1783  MpegTSFilter *f = ts->pids[prg->pcr_pid];
1784  if (f && f->last_pcr != -1)
1785  ts->pkt->pts = ts->pkt->dts = f->last_pcr/300;
1786  }
1787  ts->stop_parse = 1;
1788 
1789 }
1790 
1791 static const uint8_t opus_coupled_stream_cnt[9] = {
1792  1, 0, 1, 1, 2, 2, 2, 3, 3
1793 };
1794 
1795 static const uint8_t opus_stream_cnt[9] = {
1796  1, 1, 1, 2, 2, 3, 4, 4, 5,
1797 };
1798 
1799 static const uint8_t opus_channel_map[8][8] = {
1800  { 0 },
1801  { 0,1 },
1802  { 0,2,1 },
1803  { 0,1,2,3 },
1804  { 0,4,1,2,3 },
1805  { 0,4,1,2,3,5 },
1806  { 0,4,1,2,3,5,6 },
1807  { 0,6,1,2,3,4,5,7 },
1808 };
1809 
1811  const uint8_t **pp, const uint8_t *desc_list_end,
1812  Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1813  MpegTSContext *ts)
1814 {
1815  FFStream *const sti = ffstream(st);
1816  const uint8_t *desc_end;
1817  int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1818  char language[252];
1819  int i;
1820 
1821  desc_tag = get8(pp, desc_list_end);
1822  if (desc_tag < 0)
1823  return AVERROR_INVALIDDATA;
1824  desc_len = get8(pp, desc_list_end);
1825  if (desc_len < 0)
1826  return AVERROR_INVALIDDATA;
1827  desc_end = *pp + desc_len;
1828  if (desc_end > desc_list_end)
1829  return AVERROR_INVALIDDATA;
1830 
1831  av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1832 
1833  if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) &&
1834  stream_type == STREAM_TYPE_PRIVATE_DATA)
1835  mpegts_find_stream_type(st, desc_tag, DESC_types);
1836 
1837  switch (desc_tag) {
1839  if (get8(pp, desc_end) & 0x1) {
1841  }
1842  break;
1843  case SL_DESCRIPTOR:
1844  desc_es_id = get16(pp, desc_end);
1845  if (desc_es_id < 0)
1846  break;
1847  if (ts && ts->pids[pid])
1848  ts->pids[pid]->es_id = desc_es_id;
1849  for (i = 0; i < mp4_descr_count; i++)
1850  if (mp4_descr[i].dec_config_descr_len &&
1851  mp4_descr[i].es_id == desc_es_id) {
1852  FFIOContext pb;
1853  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1854  mp4_descr[i].dec_config_descr_len, 0,
1855  NULL, NULL, NULL, NULL);
1857  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1858  st->codecpar->extradata_size > 0) {
1859  sti->need_parsing = 0;
1860  sti->need_context_update = 1;
1861  }
1863  mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1864  }
1865  break;
1866  case FMC_DESCRIPTOR:
1867  if (get16(pp, desc_end) < 0)
1868  break;
1869  if (mp4_descr_count > 0 &&
1871  (sti->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
1872  sti->request_probe > 0) &&
1873  mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1874  FFIOContext pb;
1875  ffio_init_context(&pb, mp4_descr->dec_config_descr,
1876  mp4_descr->dec_config_descr_len, 0,
1877  NULL, NULL, NULL, NULL);
1879  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1880  st->codecpar->extradata_size > 0) {
1881  sti->request_probe = sti->need_parsing = 0;
1883  sti->need_context_update = 1;
1884  }
1885  }
1886  break;
1887  case 0x56: /* DVB teletext descriptor */
1888  {
1889  uint8_t *extradata = NULL;
1890  int language_count = desc_len / 5, ret;
1891 
1892  if (desc_len > 0 && desc_len % 5 != 0)
1893  return AVERROR_INVALIDDATA;
1894 
1895  if (language_count > 0) {
1896  /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1897  av_assert0(language_count <= sizeof(language) / 4);
1898 
1899  if (st->codecpar->extradata == NULL) {
1900  ret = ff_alloc_extradata(st->codecpar, language_count * 2);
1901  if (ret < 0)
1902  return ret;
1903  }
1904 
1905  if (st->codecpar->extradata_size < language_count * 2)
1906  return AVERROR_INVALIDDATA;
1907 
1908  extradata = st->codecpar->extradata;
1909 
1910  for (i = 0; i < language_count; i++) {
1911  language[i * 4 + 0] = get8(pp, desc_end);
1912  language[i * 4 + 1] = get8(pp, desc_end);
1913  language[i * 4 + 2] = get8(pp, desc_end);
1914  language[i * 4 + 3] = ',';
1915 
1916  memcpy(extradata, *pp, 2);
1917  extradata += 2;
1918 
1919  *pp += 2;
1920  }
1921 
1922  language[i * 4 - 1] = 0;
1923  av_dict_set(&st->metadata, "language", language, 0);
1924  sti->need_context_update = 1;
1925  }
1926  }
1927  break;
1928  case 0x59: /* subtitling descriptor */
1929  {
1930  /* 8 bytes per DVB subtitle substream data:
1931  * ISO_639_language_code (3 bytes),
1932  * subtitling_type (1 byte),
1933  * composition_page_id (2 bytes),
1934  * ancillary_page_id (2 bytes) */
1935  int language_count = desc_len / 8, ret;
1936 
1937  if (desc_len > 0 && desc_len % 8 != 0)
1938  return AVERROR_INVALIDDATA;
1939 
1940  if (language_count > 1) {
1941  avpriv_request_sample(fc, "DVB subtitles with multiple languages");
1942  }
1943 
1944  if (language_count > 0) {
1945  uint8_t *extradata;
1946 
1947  /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1948  av_assert0(language_count <= sizeof(language) / 4);
1949 
1950  if (st->codecpar->extradata == NULL) {
1951  ret = ff_alloc_extradata(st->codecpar, language_count * 5);
1952  if (ret < 0)
1953  return ret;
1954  }
1955 
1956  if (st->codecpar->extradata_size < language_count * 5)
1957  return AVERROR_INVALIDDATA;
1958 
1959  extradata = st->codecpar->extradata;
1960 
1961  for (i = 0; i < language_count; i++) {
1962  language[i * 4 + 0] = get8(pp, desc_end);
1963  language[i * 4 + 1] = get8(pp, desc_end);
1964  language[i * 4 + 2] = get8(pp, desc_end);
1965  language[i * 4 + 3] = ',';
1966 
1967  /* hearing impaired subtitles detection using subtitling_type */
1968  switch (*pp[0]) {
1969  case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1970  case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1971  case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1972  case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1973  case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1974  case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1976  break;
1977  }
1978 
1979  extradata[4] = get8(pp, desc_end); /* subtitling_type */
1980  memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */
1981  extradata += 5;
1982 
1983  *pp += 4;
1984  }
1985 
1986  language[i * 4 - 1] = 0;
1987  av_dict_set(&st->metadata, "language", language, 0);
1988  sti->need_context_update = 1;
1989  }
1990  }
1991  break;
1993  for (i = 0; i + 4 <= desc_len; i += 4) {
1994  language[i + 0] = get8(pp, desc_end);
1995  language[i + 1] = get8(pp, desc_end);
1996  language[i + 2] = get8(pp, desc_end);
1997  language[i + 3] = ',';
1998  switch (get8(pp, desc_end)) {
1999  case 0x01:
2001  break;
2002  case 0x02:
2004  break;
2005  case 0x03:
2008  break;
2009  }
2010  }
2011  if (i && language[0]) {
2012  language[i - 1] = 0;
2013  /* don't overwrite language, as it may already have been set by
2014  * another, more specific descriptor (e.g. supplementary audio) */
2016  }
2017  break;
2019  st->codecpar->codec_tag = bytestream_get_le32(pp);
2020  av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
2021  if (st->codecpar->codec_id == AV_CODEC_ID_NONE || sti->request_probe > 0) {
2023  if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D'))
2024  sti->request_probe = 50;
2025  }
2026  break;
2027  case 0x52: /* stream identifier descriptor */
2028  sti->stream_identifier = 1 + get8(pp, desc_end);
2029  break;
2030  case METADATA_DESCRIPTOR:
2031  if (get16(pp, desc_end) == 0xFFFF)
2032  *pp += 4;
2033  if (get8(pp, desc_end) == 0xFF) {
2034  st->codecpar->codec_tag = bytestream_get_le32(pp);
2035  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2037  }
2038  break;
2039  case 0x7f: /* DVB extension descriptor */
2040  ext_desc_tag = get8(pp, desc_end);
2041  if (ext_desc_tag < 0)
2042  return AVERROR_INVALIDDATA;
2043  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
2044  ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
2045  if (!st->codecpar->extradata) {
2048  if (!st->codecpar->extradata)
2049  return AVERROR(ENOMEM);
2050 
2053 
2054  channel_config_code = get8(pp, desc_end);
2055  if (channel_config_code < 0)
2056  return AVERROR_INVALIDDATA;
2057  if (channel_config_code <= 0x8) {
2058  st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
2059  AV_WL32(&st->codecpar->extradata[12], 48000);
2060  st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : /* Dual Mono */ 255;
2061  st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
2062  st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
2063  memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
2064  st->codecpar->extradata_size = st->codecpar->extradata[18] ? 21 + channels : 19;
2065  } else {
2066  avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
2067  }
2069  sti->need_context_update = 1;
2070  }
2071  }
2072  if (ext_desc_tag == 0x06) { /* supplementary audio descriptor */
2073  int flags;
2074 
2075  if (desc_len < 1)
2076  return AVERROR_INVALIDDATA;
2077  flags = get8(pp, desc_end);
2078 
2079  if ((flags & 0x80) == 0) /* mix_type */
2081 
2082  switch ((flags >> 2) & 0x1F) { /* editorial_classification */
2083  case 0x01:
2086  break;
2087  case 0x02:
2089  break;
2090  case 0x03:
2092  break;
2093  }
2094 
2095  if (flags & 0x01) { /* language_code_present */
2096  if (desc_len < 4)
2097  return AVERROR_INVALIDDATA;
2098  language[0] = get8(pp, desc_end);
2099  language[1] = get8(pp, desc_end);
2100  language[2] = get8(pp, desc_end);
2101  language[3] = 0;
2102 
2103  /* This language always has to override a possible
2104  * ISO 639 language descriptor language */
2105  if (language[0])
2106  av_dict_set(&st->metadata, "language", language, 0);
2107  }
2108  }
2109  break;
2110  case 0x6a: /* ac-3_descriptor */
2111  {
2112  int component_type_flag = get8(pp, desc_end) & (1 << 7);
2113  if (component_type_flag) {
2114  int component_type = get8(pp, desc_end);
2115  int service_type_mask = 0x38; // 0b00111000
2116  int service_type = ((component_type & service_type_mask) >> 3);
2117  if (service_type == 0x02 /* 0b010 */) {
2119  av_log(ts ? ts->stream : fc, AV_LOG_DEBUG, "New track disposition for id %u: %u\n", st->id, st->disposition);
2120  }
2121  }
2122  }
2123  break;
2124  case 0x7a: /* enhanced_ac-3_descriptor */
2125  {
2126  int component_type_flag = get8(pp, desc_end) & (1 << 7);
2127  if (component_type_flag) {
2128  int component_type = get8(pp, desc_end);
2129  int service_type_mask = 0x38; // 0b00111000
2130  int service_type = ((component_type & service_type_mask) >> 3);
2131  if (service_type == 0x02 /* 0b010 */) {
2133  av_log(ts ? ts->stream : fc, AV_LOG_DEBUG, "New track disposition for id %u: %u\n", st->id, st->disposition);
2134  }
2135  }
2136  }
2137  break;
2138  case 0xfd: /* ARIB data coding type descriptor */
2139  // STD-B24, fascicle 3, chapter 4 defines private_stream_1
2140  // for captions
2141  if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
2142  // This structure is defined in STD-B10, part 1, listing 5.4 and
2143  // part 2, 6.2.20).
2144  // Listing of data_component_ids is in STD-B10, part 2, Annex J.
2145  // Component tag limits are documented in TR-B14, fascicle 2,
2146  // Vol. 3, Section 2, 4.2.8.1
2147  int actual_component_tag = sti->stream_identifier - 1;
2148  int picked_profile = FF_PROFILE_UNKNOWN;
2149  int data_component_id = get16(pp, desc_end);
2150  if (data_component_id < 0)
2151  return AVERROR_INVALIDDATA;
2152 
2153  switch (data_component_id) {
2154  case 0x0008:
2155  // [0x30..0x37] are component tags utilized for
2156  // non-mobile captioning service ("profile A").
2157  if (actual_component_tag >= 0x30 &&
2158  actual_component_tag <= 0x37) {
2159  picked_profile = FF_PROFILE_ARIB_PROFILE_A;
2160  }
2161  break;
2162  case 0x0012:
2163  // component tag 0x87 signifies a mobile/partial reception
2164  // (1seg) captioning service ("profile C").
2165  if (actual_component_tag == 0x87) {
2166  picked_profile = FF_PROFILE_ARIB_PROFILE_C;
2167  }
2168  break;
2169  default:
2170  break;
2171  }
2172 
2173  if (picked_profile == FF_PROFILE_UNKNOWN)
2174  break;
2175 
2178  st->codecpar->profile = picked_profile;
2179  sti->request_probe = 0;
2180  }
2181  break;
2182  case 0xb0: /* DOVI video stream descriptor */
2183  {
2184  uint32_t buf;
2186  size_t dovi_size;
2187  int ret;
2188  int dependency_pid;
2189 
2190  if (desc_end - *pp < 4) // (8 + 8 + 7 + 6 + 1 + 1 + 1) / 8
2191  return AVERROR_INVALIDDATA;
2192 
2193  dovi = av_dovi_alloc(&dovi_size);
2194  if (!dovi)
2195  return AVERROR(ENOMEM);
2196 
2197  dovi->dv_version_major = get8(pp, desc_end);
2198  dovi->dv_version_minor = get8(pp, desc_end);
2199  buf = get16(pp, desc_end);
2200  dovi->dv_profile = (buf >> 9) & 0x7f; // 7 bits
2201  dovi->dv_level = (buf >> 3) & 0x3f; // 6 bits
2202  dovi->rpu_present_flag = (buf >> 2) & 0x01; // 1 bit
2203  dovi->el_present_flag = (buf >> 1) & 0x01; // 1 bit
2204  dovi->bl_present_flag = buf & 0x01; // 1 bit
2205  if (!dovi->bl_present_flag && desc_end - *pp >= 2) {
2206  buf = get16(pp, desc_end);
2207  dependency_pid = buf >> 3; // 13 bits
2208  }
2209  if (desc_end - *pp >= 1) { // 8 bits
2210  buf = get8(pp, desc_end);
2211  dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
2212  } else {
2213  // 0 stands for None
2214  // Dolby Vision V1.2.93 profiles and levels
2216  }
2217 
2219  (uint8_t *)dovi, dovi_size);
2220  if (ret < 0) {
2221  av_free(dovi);
2222  return ret;
2223  }
2224 
2225  av_log(fc, AV_LOG_TRACE, "DOVI, version: %d.%d, profile: %d, level: %d, "
2226  "rpu flag: %d, el flag: %d, bl flag: %d, dependency_pid: %d, compatibility id: %d\n",
2227  dovi->dv_version_major, dovi->dv_version_minor,
2228  dovi->dv_profile, dovi->dv_level,
2229  dovi->rpu_present_flag,
2230  dovi->el_present_flag,
2231  dovi->bl_present_flag,
2232  dependency_pid,
2233  dovi->dv_bl_signal_compatibility_id);
2234  }
2235  break;
2236  default:
2237  break;
2238  }
2239  *pp = desc_end;
2240  return 0;
2241 }
2242 
2243 static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid,
2244  int stream_identifier, int pmt_stream_idx, struct Program *p)
2245 {
2246  AVFormatContext *s = ts->stream;
2247  AVStream *found = NULL;
2248 
2249  if (stream_identifier) { /* match based on "stream identifier descriptor" if present */
2250  for (int i = 0; i < p->nb_streams; i++) {
2251  if (p->streams[i].stream_identifier == stream_identifier)
2252  if (!found || pmt_stream_idx == i) /* fallback to idx based guess if multiple streams have the same identifier */
2253  found = s->streams[p->streams[i].idx];
2254  }
2255  } else if (pmt_stream_idx < p->nb_streams) { /* match based on position within the PMT */
2256  found = s->streams[p->streams[pmt_stream_idx].idx];
2257  }
2258 
2259  if (found) {
2261  "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
2263  found->index, found->id, pid);
2264  }
2265 
2266  return found;
2267 }
2268 
2269 static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
2270 {
2271  const uint8_t **pp = &p;
2272  const uint8_t *desc_list_end;
2273  const uint8_t *desc_end;
2274  int desc_list_len;
2275  int desc_len, desc_tag;
2276 
2277  desc_list_len = get16(pp, p_end);
2278  if (desc_list_len < 0)
2279  return -1;
2280  desc_list_len &= 0xfff;
2281  desc_list_end = p + desc_list_len;
2282  if (desc_list_end > p_end)
2283  return -1;
2284 
2285  while (1) {
2286  desc_tag = get8(pp, desc_list_end);
2287  if (desc_tag < 0)
2288  return -1;
2289  desc_len = get8(pp, desc_list_end);
2290  if (desc_len < 0)
2291  return -1;
2292  desc_end = *pp + desc_len;
2293  if (desc_end > desc_list_end)
2294  return -1;
2295 
2296  if (desc_tag == 0x52) {
2297  return get8(pp, desc_end);
2298  }
2299  *pp = desc_end;
2300  }
2301 
2302  return -1;
2303 }
2304 
2305 static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
2306 {
2307  return !(stream_type == 0x13 ||
2308  (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) );
2309 }
2310 
2311 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2312 {
2313  MpegTSContext *ts = filter->u.section_filter.opaque;
2314  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2315  struct Program old_program;
2316  SectionHeader h1, *h = &h1;
2317  PESContext *pes;
2318  AVStream *st;
2319  const uint8_t *p, *p_end, *desc_list_end;
2320  int program_info_length, pcr_pid, pid, stream_type;
2321  int desc_list_len;
2322  uint32_t prog_reg_desc = 0; /* registration descriptor */
2323  int stream_identifier = -1;
2324  struct Program *prg;
2325 
2326  int mp4_descr_count = 0;
2327  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
2328  int i;
2329 
2330  av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len);
2331  hex_dump_debug(ts->stream, section, section_len);
2332 
2333  p_end = section + section_len - 4;
2334  p = section;
2335  if (parse_section_header(h, &p, p_end) < 0)
2336  return;
2337  if (h->tid != PMT_TID)
2338  return;
2339  if (!h->current_next)
2340  return;
2341  if (skip_identical(h, tssf))
2342  return;
2343 
2344  av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d version=%d tid=%d\n",
2345  h->id, h->sec_num, h->last_sec_num, h->version, h->tid);
2346 
2347  if (!ts->scan_all_pmts && ts->skip_changes)
2348  return;
2349 
2350  prg = get_program(ts, h->id);
2351  if (prg)
2352  old_program = *prg;
2353  else
2354  clear_program(&old_program);
2355 
2356  if (ts->skip_unknown_pmt && !prg)
2357  return;
2358  if (prg && prg->nb_pids && prg->pids[0] != ts->current_pid)
2359  return;
2360  if (!ts->skip_clear)
2361  clear_avprogram(ts, h->id);
2362  clear_program(prg);
2363  add_pid_to_program(prg, ts->current_pid);
2364 
2365  pcr_pid = get16(&p, p_end);
2366  if (pcr_pid < 0)
2367  return;
2368  pcr_pid &= 0x1fff;
2369  add_pid_to_program(prg, pcr_pid);
2370  update_av_program_info(ts->stream, h->id, pcr_pid, h->version);
2371 
2372  av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid);
2373 
2374  program_info_length = get16(&p, p_end);
2375  if (program_info_length < 0)
2376  return;
2377  program_info_length &= 0xfff;
2378  while (program_info_length >= 2) {
2379  uint8_t tag, len;
2380  tag = get8(&p, p_end);
2381  len = get8(&p, p_end);
2382 
2383  av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len);
2384 
2385  program_info_length -= 2;
2386  if (len > program_info_length)
2387  // something else is broken, exit the program_descriptors_loop
2388  break;
2389  program_info_length -= len;
2390  if (tag == IOD_DESCRIPTOR) {
2391  get8(&p, p_end); // scope
2392  get8(&p, p_end); // label
2393  len -= 2;
2394  mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
2395  &mp4_descr_count, MAX_MP4_DESCR_COUNT);
2396  } else if (tag == REGISTRATION_DESCRIPTOR && len >= 4) {
2397  prog_reg_desc = bytestream_get_le32(&p);
2398  len -= 4;
2399  }
2400  p += len;
2401  }
2402  p += program_info_length;
2403  if (p >= p_end)
2404  goto out;
2405 
2406  // stop parsing after pmt, we found header
2407  if (!ts->pkt)
2408  ts->stop_parse = 2;
2409 
2410  if (prg)
2411  prg->pmt_found = 1;
2412 
2413  for (i = 0; i < MAX_STREAMS_PER_PROGRAM; i++) {
2414  st = 0;
2415  pes = NULL;
2416  stream_type = get8(&p, p_end);
2417  if (stream_type < 0)
2418  break;
2419  pid = get16(&p, p_end);
2420  if (pid < 0)
2421  goto out;
2422  pid &= 0x1fff;
2423  if (pid == ts->current_pid)
2424  goto out;
2425 
2426  stream_identifier = parse_stream_identifier_desc(p, p_end) + 1;
2427 
2428  /* now create stream */
2429  if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
2430  pes = ts->pids[pid]->u.pes_filter.opaque;
2431  if (ts->merge_pmt_versions && !pes->st) {
2432  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2433  if (st) {
2434  pes->st = st;
2435  pes->stream_type = stream_type;
2436  pes->merged_st = 1;
2437  }
2438  }
2439  if (!pes->st) {
2440  pes->st = avformat_new_stream(pes->stream, NULL);
2441  if (!pes->st)
2442  goto out;
2443  pes->st->id = pes->pid;
2444  }
2445  st = pes->st;
2446  } else if (is_pes_stream(stream_type, prog_reg_desc)) {
2447  if (ts->pids[pid])
2448  mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
2449  pes = add_pes_stream(ts, pid, pcr_pid);
2450  if (ts->merge_pmt_versions && pes && !pes->st) {
2451  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2452  if (st) {
2453  pes->st = st;
2454  pes->stream_type = stream_type;
2455  pes->merged_st = 1;
2456  }
2457  }
2458  if (pes && !pes->st) {
2459  st = avformat_new_stream(pes->stream, NULL);
2460  if (!st)
2461  goto out;
2462  st->id = pes->pid;
2463  }
2464  } else {
2465  int idx = ff_find_stream_index(ts->stream, pid);
2466  if (idx >= 0) {
2467  st = ts->stream->streams[idx];
2468  }
2469  if (ts->merge_pmt_versions && !st) {
2470  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2471  }
2472  if (!st) {
2473  st = avformat_new_stream(ts->stream, NULL);
2474  if (!st)
2475  goto out;
2476  st->id = pid;
2478  if (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) {
2479  mpegts_find_stream_type(st, stream_type, SCTE_types);
2480  mpegts_open_section_filter(ts, pid, scte_data_cb, ts, 1);
2481  }
2482  }
2483  }
2484 
2485  if (!st)
2486  goto out;
2487 
2488  if (pes && !pes->stream_type)
2489  mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
2490 
2491  add_pid_to_program(prg, pid);
2492  if (prg) {
2493  prg->streams[i].idx = st->index;
2494  prg->streams[i].stream_identifier = stream_identifier;
2495  prg->nb_streams++;
2496  }
2497 
2498  av_program_add_stream_index(ts->stream, h->id, st->index);
2499 
2500  desc_list_len = get16(&p, p_end);
2501  if (desc_list_len < 0)
2502  goto out;
2503  desc_list_len &= 0xfff;
2504  desc_list_end = p + desc_list_len;
2505  if (desc_list_end > p_end)
2506  goto out;
2507  for (;;) {
2508  if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
2509  desc_list_end, mp4_descr,
2510  mp4_descr_count, pid, ts) < 0)
2511  break;
2512 
2513  if (pes && prog_reg_desc == AV_RL32("HDMV") &&
2514  stream_type == 0x83 && pes->sub_st) {
2516  pes->sub_st->index);
2517  pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag;
2518  }
2519  }
2520  p = desc_list_end;
2521  }
2522 
2523  if (!ts->pids[pcr_pid])
2524  mpegts_open_pcr_filter(ts, pcr_pid);
2525 
2526 out:
2527  for (i = 0; i < mp4_descr_count; i++)
2528  av_free(mp4_descr[i].dec_config_descr);
2529 }
2530 
2531 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2532 {
2533  MpegTSContext *ts = filter->u.section_filter.opaque;
2534  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2535  SectionHeader h1, *h = &h1;
2536  const uint8_t *p, *p_end;
2537  int sid, pmt_pid;
2538  int nb_prg = 0;
2539  AVProgram *program;
2540 
2541  av_log(ts->stream, AV_LOG_TRACE, "PAT:\n");
2542  hex_dump_debug(ts->stream, section, section_len);
2543 
2544  p_end = section + section_len - 4;
2545  p = section;
2546  if (parse_section_header(h, &p, p_end) < 0)
2547  return;
2548  if (h->tid != PAT_TID)
2549  return;
2550  if (!h->current_next)
2551  return;
2552  if (ts->skip_changes)
2553  return;
2554 
2555  if (skip_identical(h, tssf))
2556  return;
2557  ts->stream->ts_id = h->id;
2558 
2559  for (;;) {
2560  sid = get16(&p, p_end);
2561  if (sid < 0)
2562  break;
2563  pmt_pid = get16(&p, p_end);
2564  if (pmt_pid < 0)
2565  break;
2566  pmt_pid &= 0x1fff;
2567 
2568  if (pmt_pid == ts->current_pid)
2569  break;
2570 
2571  av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
2572 
2573  if (sid == 0x0000) {
2574  /* NIT info */
2575  } else {
2576  MpegTSFilter *fil = ts->pids[pmt_pid];
2577  struct Program *prg;
2578  program = av_new_program(ts->stream, sid);
2579  if (program) {
2580  program->program_num = sid;
2581  program->pmt_pid = pmt_pid;
2582  }
2583  if (fil)
2584  if ( fil->type != MPEGTS_SECTION
2585  || fil->pid != pmt_pid
2586  || fil->u.section_filter.section_cb != pmt_cb)
2587  mpegts_close_filter(ts, ts->pids[pmt_pid]);
2588 
2589  if (!ts->pids[pmt_pid])
2590  mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
2591  prg = add_program(ts, sid);
2592  if (prg) {
2593  unsigned prg_idx = prg - ts->prg;
2594  if (prg->nb_pids && prg->pids[0] != pmt_pid)
2595  clear_program(prg);
2596  add_pid_to_program(prg, pmt_pid);
2597  if (prg_idx > nb_prg)
2598  FFSWAP(struct Program, ts->prg[nb_prg], ts->prg[prg_idx]);
2599  if (prg_idx >= nb_prg)
2600  nb_prg++;
2601  }
2602  }
2603  }
2604  ts->nb_prg = nb_prg;
2605 
2606  if (sid < 0) {
2607  int i,j;
2608  for (j=0; j<ts->stream->nb_programs; j++) {
2609  for (i = 0; i < ts->nb_prg; i++)
2610  if (ts->prg[i].id == ts->stream->programs[j]->id)
2611  break;
2612  if (i==ts->nb_prg && !ts->skip_clear)
2613  clear_avprogram(ts, ts->stream->programs[j]->id);
2614  }
2615  }
2616 }
2617 
2618 static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2619 {
2620  MpegTSContext *ts = filter->u.section_filter.opaque;
2621  const uint8_t *p, *p_end;
2622  SectionHeader h1, *h = &h1;
2623 
2624  /*
2625  * Sometimes we receive EPG packets but SDT table do not have
2626  * eit_pres_following or eit_sched turned on, so we open EPG
2627  * stream directly here.
2628  */
2629  if (!ts->epg_stream) {
2631  if (!ts->epg_stream)
2632  return;
2633  ts->epg_stream->id = EIT_PID;
2636  }
2637 
2638  if (ts->epg_stream->discard == AVDISCARD_ALL)
2639  return;
2640 
2641  p_end = section + section_len - 4;
2642  p = section;
2643 
2644  if (parse_section_header(h, &p, p_end) < 0)
2645  return;
2646  if (h->tid < EIT_TID || h->tid > OEITS_END_TID)
2647  return;
2648 
2649  av_log(ts->stream, AV_LOG_TRACE, "EIT: tid received = %.02x\n", h->tid);
2650 
2651  /**
2652  * Service_id 0xFFFF is reserved, it indicates that the current EIT table
2653  * is scrambled.
2654  */
2655  if (h->id == 0xFFFF) {
2656  av_log(ts->stream, AV_LOG_TRACE, "Scrambled EIT table received.\n");
2657  return;
2658  }
2659 
2660  /**
2661  * In case we receive an EPG packet before mpegts context is fully
2662  * initialized.
2663  */
2664  if (!ts->pkt)
2665  return;
2666 
2667  new_data_packet(section, section_len, ts->pkt);
2668  ts->pkt->stream_index = ts->epg_stream->index;
2669  ts->stop_parse = 1;
2670 }
2671 
2672 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2673 {
2674  MpegTSContext *ts = filter->u.section_filter.opaque;
2675  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2676  SectionHeader h1, *h = &h1;
2677  const uint8_t *p, *p_end, *desc_list_end, *desc_end;
2678  int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
2679  char *name, *provider_name;
2680 
2681  av_log(ts->stream, AV_LOG_TRACE, "SDT:\n");
2682  hex_dump_debug(ts->stream, section, section_len);
2683 
2684  p_end = section + section_len - 4;
2685  p = section;
2686  if (parse_section_header(h, &p, p_end) < 0)
2687  return;
2688  if (h->tid != SDT_TID)
2689  return;
2690  if (!h->current_next)
2691  return;
2692  if (ts->skip_changes)
2693  return;
2694  if (skip_identical(h, tssf))
2695  return;
2696 
2697  onid = get16(&p, p_end);
2698  if (onid < 0)
2699  return;
2700  val = get8(&p, p_end);
2701  if (val < 0)
2702  return;
2703  for (;;) {
2704  sid = get16(&p, p_end);
2705  if (sid < 0)
2706  break;
2707  val = get8(&p, p_end);
2708  if (val < 0)
2709  break;
2710  desc_list_len = get16(&p, p_end);
2711  if (desc_list_len < 0)
2712  break;
2713  desc_list_len &= 0xfff;
2714  desc_list_end = p + desc_list_len;
2715  if (desc_list_end > p_end)
2716  break;
2717  for (;;) {
2718  desc_tag = get8(&p, desc_list_end);
2719  if (desc_tag < 0)
2720  break;
2721  desc_len = get8(&p, desc_list_end);
2722  desc_end = p + desc_len;
2723  if (desc_len < 0 || desc_end > desc_list_end)
2724  break;
2725 
2726  av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n",
2727  desc_tag, desc_len);
2728 
2729  switch (desc_tag) {
2730  case 0x48:
2731  service_type = get8(&p, desc_end);
2732  if (service_type < 0)
2733  break;
2734  provider_name = getstr8(&p, desc_end);
2735  if (!provider_name)
2736  break;
2737  name = getstr8(&p, desc_end);
2738  if (name) {
2739  AVProgram *program = av_new_program(ts->stream, sid);
2740  if (program) {
2741  av_dict_set(&program->metadata, "service_name", name, 0);
2742  av_dict_set(&program->metadata, "service_provider",
2743  provider_name, 0);
2744  }
2745  }
2746  av_free(name);
2747  av_free(provider_name);
2748  break;
2749  default:
2750  break;
2751  }
2752  p = desc_end;
2753  }
2754  p = desc_list_end;
2755  }
2756 }
2757 
2758 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
2759  const uint8_t *packet);
2760 
2761 /* handle one TS packet */
2762 static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
2763 {
2764  MpegTSFilter *tss;
2765  int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2766  has_adaptation, has_payload;
2767  const uint8_t *p, *p_end;
2768 
2769  pid = AV_RB16(packet + 1) & 0x1fff;
2770  is_start = packet[1] & 0x40;
2771  tss = ts->pids[pid];
2772  if (ts->auto_guess && !tss && is_start) {
2773  add_pes_stream(ts, pid, -1);
2774  tss = ts->pids[pid];
2775  }
2776  if (!tss)
2777  return 0;
2778  if (is_start)
2779  tss->discard = discard_pid(ts, pid);
2780  if (tss->discard)
2781  return 0;
2782  ts->current_pid = pid;
2783 
2784  afc = (packet[3] >> 4) & 3;
2785  if (afc == 0) /* reserved value */
2786  return 0;
2787  has_adaptation = afc & 2;
2788  has_payload = afc & 1;
2789  is_discontinuity = has_adaptation &&
2790  packet[4] != 0 && /* with length > 0 */
2791  (packet[5] & 0x80); /* and discontinuity indicated */
2792 
2793  /* continuity check (currently not used) */
2794  cc = (packet[3] & 0xf);
2795  expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
2796  cc_ok = pid == 0x1FFF || // null packet PID
2797  is_discontinuity ||
2798  tss->last_cc < 0 ||
2799  expected_cc == cc;
2800 
2801  tss->last_cc = cc;
2802  if (!cc_ok) {
2803  av_log(ts->stream, AV_LOG_DEBUG,
2804  "Continuity check failed for pid %d expected %d got %d\n",
2805  pid, expected_cc, cc);
2806  if (tss->type == MPEGTS_PES) {
2807  PESContext *pc = tss->u.pes_filter.opaque;
2808  pc->flags |= AV_PKT_FLAG_CORRUPT;
2809  }
2810  }
2811 
2812  if (packet[1] & 0x80) {
2813  av_log(ts->stream, AV_LOG_DEBUG, "Packet had TEI flag set; marking as corrupt\n");
2814  if (tss->type == MPEGTS_PES) {
2815  PESContext *pc = tss->u.pes_filter.opaque;
2816  pc->flags |= AV_PKT_FLAG_CORRUPT;
2817  }
2818  }
2819 
2820  p = packet + 4;
2821  if (has_adaptation) {
2822  int64_t pcr_h;
2823  int pcr_l;
2824  if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2825  tss->last_pcr = pcr_h * 300 + pcr_l;
2826  /* skip adaptation field */
2827  p += p[0] + 1;
2828  }
2829  /* if past the end of packet, ignore */
2830  p_end = packet + TS_PACKET_SIZE;
2831  if (p >= p_end || !has_payload)
2832  return 0;
2833 
2834  if (pos >= 0) {
2836  ts->pos47_full = pos - TS_PACKET_SIZE;
2837  }
2838 
2839  if (tss->type == MPEGTS_SECTION) {
2840  if (is_start) {
2841  /* pointer field present */
2842  len = *p++;
2843  if (len > p_end - p)
2844  return 0;
2845  if (len && cc_ok) {
2846  /* write remaining section bytes */
2847  write_section_data(ts, tss,
2848  p, len, 0);
2849  /* check whether filter has been closed */
2850  if (!ts->pids[pid])
2851  return 0;
2852  }
2853  p += len;
2854  if (p < p_end) {
2855  write_section_data(ts, tss,
2856  p, p_end - p, 1);
2857  }
2858  } else {
2859  if (cc_ok) {
2860  write_section_data(ts, tss,
2861  p, p_end - p, 0);
2862  }
2863  }
2864 
2865  // stop find_stream_info from waiting for more streams
2866  // when all programs have received a PMT
2867  if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
2868  int i;
2869  for (i = 0; i < ts->nb_prg; i++) {
2870  if (!ts->prg[i].pmt_found)
2871  break;
2872  }
2873  if (i == ts->nb_prg && ts->nb_prg > 0) {
2874  av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
2876  }
2877  }
2878 
2879  } else {
2880  int ret;
2881  // Note: The position here points actually behind the current packet.
2882  if (tss->type == MPEGTS_PES) {
2883  if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
2884  pos - ts->raw_packet_size)) < 0)
2885  return ret;
2886  }
2887  }
2888 
2889  return 0;
2890 }
2891 
2892 static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet)
2893 {
2894  MpegTSContext *ts = s->priv_data;
2895  AVIOContext *pb = s->pb;
2896  int c, i;
2897  uint64_t pos = avio_tell(pb);
2898  int64_t back = FFMIN(seekback, pos);
2899 
2900  //Special case for files like 01c56b0dc1.ts
2901  if (current_packet[0] == 0x80 && current_packet[12] == 0x47 && pos >= TS_PACKET_SIZE) {
2902  avio_seek(pb, 12 - TS_PACKET_SIZE, SEEK_CUR);
2903  return 0;
2904  }
2905 
2906  avio_seek(pb, -back, SEEK_CUR);
2907 
2908  for (i = 0; i < ts->resync_size; i++) {
2909  c = avio_r8(pb);
2910  if (avio_feof(pb))
2911  return AVERROR_EOF;
2912  if (c == 0x47) {
2913  int new_packet_size, ret;
2914  avio_seek(pb, -1, SEEK_CUR);
2915  pos = avio_tell(pb);
2917  if (ret < 0)
2918  return ret;
2919  new_packet_size = get_packet_size(s);
2920  if (new_packet_size > 0 && new_packet_size != ts->raw_packet_size) {
2921  av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", new_packet_size);
2922  ts->raw_packet_size = new_packet_size;
2923  }
2924  avio_seek(pb, pos, SEEK_SET);
2925  return 0;
2926  }
2927  }
2929  "max resync size reached, could not find sync byte\n");
2930  /* no sync found */
2931  return AVERROR_INVALIDDATA;
2932 }
2933 
2934 /* return AVERROR_something if error or EOF. Return 0 if OK. */
2935 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
2936  const uint8_t **data)
2937 {
2938  AVIOContext *pb = s->pb;
2939  int len;
2940 
2941  for (;;) {
2943  if (len != TS_PACKET_SIZE)
2944  return len < 0 ? len : AVERROR_EOF;
2945  /* check packet sync byte */
2946  if ((*data)[0] != 0x47) {
2947  /* find a new packet start */
2948 
2949  if (mpegts_resync(s, raw_packet_size, *data) < 0)
2950  return AVERROR(EAGAIN);
2951  else
2952  continue;
2953  } else {
2954  break;
2955  }
2956  }
2957  return 0;
2958 }
2959 
2960 static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
2961 {
2962  AVIOContext *pb = s->pb;
2963  int skip = raw_packet_size - TS_PACKET_SIZE;
2964  if (skip > 0)
2965  avio_skip(pb, skip);
2966 }
2967 
2968 static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
2969 {
2970  AVFormatContext *s = ts->stream;
2971  uint8_t packet[TS_PACKET_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
2972  const uint8_t *data;
2973  int64_t packet_num;
2974  int ret = 0;
2975 
2976  if (avio_tell(s->pb) != ts->last_pos) {
2977  int i;
2978  av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n");
2979  /* seek detected, flush pes buffer */
2980  for (i = 0; i < NB_PID_MAX; i++) {
2981  if (ts->pids[i]) {
2982  if (ts->pids[i]->type == MPEGTS_PES) {
2983  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
2984  av_buffer_unref(&pes->buffer);
2985  pes->data_index = 0;
2986  pes->state = MPEGTS_SKIP; /* skip until pes header */
2987  } else if (ts->pids[i]->type == MPEGTS_SECTION) {
2988  ts->pids[i]->u.section_filter.last_ver = -1;
2989  }
2990  ts->pids[i]->last_cc = -1;
2991  ts->pids[i]->last_pcr = -1;
2992  }
2993  }
2994  }
2995 
2996  ts->stop_parse = 0;
2997  packet_num = 0;
2998  memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2999  for (;;) {
3000  packet_num++;
3001  if (nb_packets != 0 && packet_num >= nb_packets ||
3002  ts->stop_parse > 1) {
3003  ret = AVERROR(EAGAIN);
3004  break;
3005  }
3006  if (ts->stop_parse > 0)
3007  break;
3008 
3009  ret = read_packet(s, packet, ts->raw_packet_size, &data);
3010  if (ret != 0)
3011  break;
3012  ret = handle_packet(ts, data, avio_tell(s->pb));
3014  if (ret != 0)
3015  break;
3016  }
3017  ts->last_pos = avio_tell(s->pb);
3018  return ret;
3019 }
3020 
3021 static int mpegts_probe(const AVProbeData *p)
3022 {
3023  const int size = p->buf_size;
3024  int maxscore = 0;
3025  int sumscore = 0;
3026  int i;
3027  int check_count = size / TS_FEC_PACKET_SIZE;
3028 #define CHECK_COUNT 10
3029 #define CHECK_BLOCK 100
3030 
3031  if (!check_count)
3032  return 0;
3033 
3034  for (i = 0; i<check_count; i+=CHECK_BLOCK) {
3035  int left = FFMIN(check_count - i, CHECK_BLOCK);
3036  int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , 1);
3039  score = FFMAX3(score, dvhs_score, fec_score);
3040  sumscore += score;
3041  maxscore = FFMAX(maxscore, score);
3042  }
3043 
3044  sumscore = sumscore * CHECK_COUNT / check_count;
3045  maxscore = maxscore * CHECK_COUNT / CHECK_BLOCK;
3046 
3047  ff_dlog(0, "TS score: %d %d\n", sumscore, maxscore);
3048 
3049  if (check_count > CHECK_COUNT && sumscore > 6) {
3050  return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
3051  } else if (check_count >= CHECK_COUNT && sumscore > 6) {
3052  return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3053  } else if (check_count >= CHECK_COUNT && maxscore > 6) {
3054  return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3055  } else if (sumscore > 6) {
3056  return 2;
3057  } else {
3058  return 0;
3059  }
3060 }
3061 
3062 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
3063  * (-1) if not available */
3064 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
3065 {
3066  int afc, len, flags;
3067  const uint8_t *p;
3068  unsigned int v;
3069 
3070  afc = (packet[3] >> 4) & 3;
3071  if (afc <= 1)
3072  return AVERROR_INVALIDDATA;
3073  p = packet + 4;
3074  len = p[0];
3075  p++;
3076  if (len == 0)
3077  return AVERROR_INVALIDDATA;
3078  flags = *p++;
3079  len--;
3080  if (!(flags & 0x10))
3081  return AVERROR_INVALIDDATA;
3082  if (len < 6)
3083  return AVERROR_INVALIDDATA;
3084  v = AV_RB32(p);
3085  *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
3086  *ppcr_low = ((p[4] & 1) << 8) | p[5];
3087  return 0;
3088 }
3089 
3090 static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos) {
3091 
3092  /* NOTE: We attempt to seek on non-seekable files as well, as the
3093  * probe buffer usually is big enough. Only warn if the seek failed
3094  * on files where the seek should work. */
3095  if (avio_seek(pb, pos, SEEK_SET) < 0)
3096  av_log(s, (pb->seekable & AVIO_SEEKABLE_NORMAL) ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
3097 }
3098 
3100 {
3101  MpegTSContext *ts = s->priv_data;
3102  AVIOContext *pb = s->pb;
3103  int64_t pos, probesize = s->probesize;
3104  int64_t seekback = FFMAX(s->probesize, (int64_t)ts->resync_size + PROBE_PACKET_MAX_BUF);
3105 
3107 
3108  if (ffio_ensure_seekback(pb, seekback) < 0)
3109  av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n");
3110 
3111  pos = avio_tell(pb);
3113  if (ts->raw_packet_size <= 0) {
3114  av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
3116  }
3117  ts->stream = s;
3118  ts->auto_guess = 0;
3119 
3120  if (s->iformat == &ff_mpegts_demuxer) {
3121  /* normal demux */
3122 
3123  /* first do a scan to get all the services */
3124  seek_back(s, pb, pos);
3125 
3129 
3130  handle_packets(ts, probesize / ts->raw_packet_size);
3131  /* if could not find service, enable auto_guess */
3132 
3133  ts->auto_guess = 1;
3134 
3135  av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
3136 
3137  s->ctx_flags |= AVFMTCTX_NOHEADER;
3138  } else {
3139  AVStream *st;
3140  int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
3141  int64_t pcrs[2], pcr_h;
3142  uint8_t packet[TS_PACKET_SIZE];
3143  const uint8_t *data;
3144 
3145  /* only read packets */
3146 
3147  st = avformat_new_stream(s, NULL);
3148  if (!st)
3149  return AVERROR(ENOMEM);
3150  avpriv_set_pts_info(st, 60, 1, 27000000);
3153 
3154  /* we iterate until we find two PCRs to estimate the bitrate */
3155  pcr_pid = -1;
3156  nb_pcrs = 0;
3157  nb_packets = 0;
3158  for (;;) {
3159  ret = read_packet(s, packet, ts->raw_packet_size, &data);
3160  if (ret < 0)
3161  return ret;
3162  pid = AV_RB16(data + 1) & 0x1fff;
3163  if ((pcr_pid == -1 || pcr_pid == pid) &&
3164  parse_pcr(&pcr_h, &pcr_l, data) == 0) {
3166  pcr_pid = pid;
3167  pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
3168  nb_pcrs++;
3169  if (nb_pcrs >= 2) {
3170  if (pcrs[1] - pcrs[0] > 0) {
3171  /* the difference needs to be positive to make sense for bitrate computation */
3172  break;
3173  } else {
3174  av_log(ts->stream, AV_LOG_WARNING, "invalid pcr pair %"PRId64" >= %"PRId64"\n", pcrs[0], pcrs[1]);
3175  pcrs[0] = pcrs[1];
3176  nb_pcrs--;
3177  }
3178  }
3179  } else {
3181  }
3182  nb_packets++;
3183  }
3184 
3185  /* NOTE1: the bitrate is computed without the FEC */
3186  /* NOTE2: it is only the bitrate of the start of the stream */
3187  ts->pcr_incr = pcrs[1] - pcrs[0];
3188  ts->cur_pcr = pcrs[0] - ts->pcr_incr * (nb_packets - 1);
3189  s->bit_rate = TS_PACKET_SIZE * 8 * 27000000LL / ts->pcr_incr;
3190  st->codecpar->bit_rate = s->bit_rate;
3191  st->start_time = ts->cur_pcr;
3192  av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%"PRId64"\n",
3193  st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
3194  }
3195 
3196  seek_back(s, pb, pos);
3197  return 0;
3198 }
3199 
3200 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
3201 
3203 {
3204  MpegTSContext *ts = s->priv_data;
3205  int ret, i;
3206  int64_t pcr_h, next_pcr_h, pos;
3207  int pcr_l, next_pcr_l;
3208  uint8_t pcr_buf[12];
3209  const uint8_t *data;
3210 
3211  if ((ret = av_new_packet(pkt, TS_PACKET_SIZE)) < 0)
3212  return ret;
3214  pkt->pos = avio_tell(s->pb);
3215  if (ret < 0) {
3216  return ret;
3217  }
3218  if (data != pkt->data)
3219  memcpy(pkt->data, data, TS_PACKET_SIZE);
3221  if (ts->mpeg2ts_compute_pcr) {
3222  /* compute exact PCR for each packet */
3223  if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
3224  /* we read the next PCR (XXX: optimize it by using a bigger buffer */
3225  pos = avio_tell(s->pb);
3226  for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
3227  avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
3228  avio_read(s->pb, pcr_buf, 12);
3229  if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
3230  /* XXX: not precise enough */
3231  ts->pcr_incr =
3232  ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
3233  (i + 1);
3234  break;
3235  }
3236  }
3237  avio_seek(s->pb, pos, SEEK_SET);
3238  /* no next PCR found: we use previous increment */
3239  ts->cur_pcr = pcr_h * 300 + pcr_l;
3240  }
3241  pkt->pts = ts->cur_pcr;
3242  pkt->duration = ts->pcr_incr;
3243  ts->cur_pcr += ts->pcr_incr;
3244  }
3245  pkt->stream_index = 0;
3246  return 0;
3247 }
3248 
3250 {
3251  MpegTSContext *ts = s->priv_data;
3252  int ret, i;
3253 
3254  pkt->size = -1;
3255  ts->pkt = pkt;
3256  ret = handle_packets(ts, 0);
3257  if (ret < 0) {
3258  av_packet_unref(ts->pkt);
3259  /* flush pes data left */
3260  for (i = 0; i < NB_PID_MAX; i++)
3261  if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
3262  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
3263  if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
3264  ret = new_pes_packet(pes, pkt);
3265  if (ret < 0)
3266  return ret;
3267  pes->state = MPEGTS_SKIP;
3268  ret = 0;
3269  break;
3270  }
3271  }
3272  }
3273 
3274  if (!ret && pkt->size < 0)
3276  return ret;
3277 }
3278 
3279 static void mpegts_free(MpegTSContext *ts)
3280 {
3281  int i;
3282 
3283  clear_programs(ts);
3284 
3285  for (i = 0; i < FF_ARRAY_ELEMS(ts->pools); i++)
3286  av_buffer_pool_uninit(&ts->pools[i]);
3287 
3288  for (i = 0; i < NB_PID_MAX; i++)
3289  if (ts->pids[i])
3290  mpegts_close_filter(ts, ts->pids[i]);
3291 }
3292 
3294 {
3295  MpegTSContext *ts = s->priv_data;
3296  mpegts_free(ts);
3297  return 0;
3298 }
3299 
3300 static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
3301  int64_t *ppos, int64_t pos_limit)
3302 {
3303  MpegTSContext *ts = s->priv_data;
3304  int64_t pos, timestamp;
3305  uint8_t buf[TS_PACKET_SIZE];
3306  int pcr_l, pcr_pid =
3307  ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
3308  int pos47 = ts->pos47_full % ts->raw_packet_size;
3309  pos =
3310  ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) *
3311  ts->raw_packet_size + pos47;
3312  while(pos < pos_limit) {
3313  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3314  return AV_NOPTS_VALUE;
3315  if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
3316  return AV_NOPTS_VALUE;
3317  if (buf[0] != 0x47) {
3318  if (mpegts_resync(s, TS_PACKET_SIZE, buf) < 0)
3319  return AV_NOPTS_VALUE;
3320  pos = avio_tell(s->pb);
3321  continue;
3322  }
3323  if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
3324  parse_pcr(&timestamp, &pcr_l, buf) == 0) {
3325  *ppos = pos;
3326  return timestamp;
3327  }
3328  pos += ts->raw_packet_size;
3329  }
3330 
3331  return AV_NOPTS_VALUE;
3332 }
3333 
3334 static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
3335  int64_t *ppos, int64_t pos_limit)
3336 {
3337  MpegTSContext *ts = s->priv_data;
3338  AVPacket *pkt;
3339  int64_t pos;
3340  int pos47 = ts->pos47_full % ts->raw_packet_size;
3341  pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
3343  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3344  return AV_NOPTS_VALUE;
3345  pkt = av_packet_alloc();
3346  if (!pkt)
3347  return AV_NOPTS_VALUE;
3348  while(pos < pos_limit) {
3349  int ret = av_read_frame(s, pkt);
3350  if (ret < 0) {
3351  av_packet_free(&pkt);
3352  return AV_NOPTS_VALUE;
3353  }
3354  if (pkt->dts != AV_NOPTS_VALUE && pkt->pos >= 0) {
3356  av_add_index_entry(s->streams[pkt->stream_index], pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
3357  if (pkt->stream_index == stream_index && pkt->pos >= *ppos) {
3358  int64_t dts = pkt->dts;
3359  *ppos = pkt->pos;
3360  av_packet_free(&pkt);
3361  return dts;
3362  }
3363  }
3364  pos = pkt->pos;
3366  }
3367 
3368  av_packet_free(&pkt);
3369  return AV_NOPTS_VALUE;
3370 }
3371 
3372 /**************************************************************/
3373 /* parsing functions - called from other demuxers such as RTP */
3374 
3376 {
3377  MpegTSContext *ts;
3378 
3379  ts = av_mallocz(sizeof(MpegTSContext));
3380  if (!ts)
3381  return NULL;
3382  /* no stream case, currently used by RTP */
3384  ts->max_packet_size = 2048000;
3385  ts->stream = s;
3386  ts->auto_guess = 1;
3387 
3391 
3392  return ts;
3393 }
3394 
3395 /* return the consumed length if a packet was output, or -1 if no
3396  * packet is output */
3398  const uint8_t *buf, int len)
3399 {
3400  int len1;
3401 
3402  len1 = len;
3403  ts->pkt = pkt;
3404  for (;;) {
3405  ts->stop_parse = 0;
3406  if (len < TS_PACKET_SIZE)
3407  return AVERROR_INVALIDDATA;
3408  if (buf[0] != 0x47) {
3409  buf++;
3410  len--;
3411  } else {
3412  handle_packet(ts, buf, len1 - len + TS_PACKET_SIZE);
3413  buf += TS_PACKET_SIZE;
3414  len -= TS_PACKET_SIZE;
3415  if (ts->stop_parse == 1)
3416  break;
3417  }
3418  }
3419  return len1 - len;
3420 }
3421 
3423 {
3424  mpegts_free(ts);
3425  av_free(ts);
3426 }
3427 
3429  .name = "mpegts",
3430  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
3431  .priv_data_size = sizeof(MpegTSContext),
3438  .priv_class = &mpegts_class,
3439 };
3440 
3442  .name = "mpegtsraw",
3443  .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
3444  .priv_data_size = sizeof(MpegTSContext),
3450  .priv_class = &mpegtsraw_class,
3451 };
parse_MP4DecConfigDescrTag
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1554
mpegts_set_stream_info
static int mpegts_set_stream_info(AVStream *st, PESContext *pes, uint32_t stream_type, uint32_t prog_reg_desc)
Definition: mpegts.c:909
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
parse_mp4_descr_arr
static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1494
new_pes_packet
static int new_pes_packet(PESContext *pes, AVPacket *pkt)
Definition: mpegts.c:1009
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:280
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:291
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
StreamType::stream_type
uint32_t stream_type
Definition: mpegts.c:792
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:76
MP4DescrParseContext::descr_count
int descr_count
Definition: mpegts.c:1458
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
MP4DecConfigDescrTag
#define MP4DecConfigDescrTag
Definition: isom.h:339
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:438
program
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C program
Definition: undefined.txt:6
MPEGTS_PESHEADER_FILL
@ MPEGTS_PESHEADER_FILL
Definition: mpegts.c:240
MpegTSFilter::discard
int discard
Definition: mpegts.c:104
Program::nb_streams
unsigned int nb_streams
Definition: mpegts.c:123
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:839
r
const char * r
Definition: vf_curves.c:126
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: options.c:237
MAX_LEVEL
#define MAX_LEVEL
Definition: mpegts.c:1452
AV_CODEC_ID_PCM_BLURAY
@ AV_CODEC_ID_PCM_BLURAY
Definition: codec_id.h:350
FFFormatContext::prefer_codec_framerate
int prefer_codec_framerate
Definition: internal.h:178
PAT_PID
#define PAT_PID
Definition: mpegts.h:37
AV_OPT_FLAG_READONLY
#define AV_OPT_FLAG_READONLY
The option may not be set through the AVOptions API, only read.
Definition: opt.h:294
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
AVFMT_SHOW_IDS
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition: avformat.h:479
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
mpegts.h
AVProgram::nb_stream_indexes
unsigned int nb_stream_indexes
Definition: avformat.h:1145
AV_PKT_DATA_MPEGTS_STREAM_ID
@ 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:216
out
FILE * out
Definition: movenc.c:54
ff_parse_pes_pts
static int64_t ff_parse_pes_pts(const uint8_t *buf)
Parse MPEG-PES five-byte timestamp.
Definition: mpeg.h:68
TS_DVHS_PACKET_SIZE
#define TS_DVHS_PACKET_SIZE
Definition: mpegts.h:28
pmt_cb
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2311
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:88
MpegTSFilter::pid
int pid
Definition: mpegts.c:100
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:191
ffio_read_indirect
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:703
STREAM_TYPE_PRIVATE_DATA
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:54
PESContext::flags
int flags
copied to the AVPacket flags
Definition: mpegts.c:261
AVStream::priv_data
void * priv_data
Definition: avformat.h:964
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
avpriv_mpegts_parse_packet
int avpriv_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, const uint8_t *buf, int len)
Definition: mpegts.c:3397
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
opus_default_extradata
static const uint8_t opus_default_extradata[30]
Definition: opus.h:35
avcodec_get_type
enum AVMediaType avcodec_get_type(enum AVCodecID codec_id)
Get the type of the given codec.
Definition: codec_desc.c:3642
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:1010
av_find_program_from_stream
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition: avformat.c:362
STREAM_ID_EMM_STREAM
#define STREAM_ID_EMM_STREAM
Definition: mpegts.h:150
mpegts_close_filter
static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
Definition: mpegts.c:558
STREAM_ID_PADDING_STREAM
#define STREAM_ID_PADDING_STREAM
Definition: mpegts.h:145
AV_CODEC_ID_DIRAC
@ AV_CODEC_ID_DIRAC
Definition: codec_id.h:168
STREAM_ID_PROGRAM_STREAM_MAP
#define STREAM_ID_PROGRAM_STREAM_MAP
Definition: mpegts.h:143
SLConfigDescr::au_seq_num_len
int au_seq_num_len
Definition: mpegts.h:187
Program::pmt_found
int pmt_found
have we found pmt for this program
Definition: mpegts.c:127
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
PESContext::dts
int64_t dts
Definition: mpegts.c:266
METADATA_types
static const StreamType METADATA_types[]
Definition: mpegts.c:875
av_unused
#define av_unused
Definition: attributes.h:131
MP4DescrParseContext::max_descr_count
int max_descr_count
Definition: mpegts.c:1459
Stream::stream_identifier
int stream_identifier
Definition: mpegts.c:114
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
MpegTSContext::skip_changes
int skip_changes
Definition: mpegts.c:159
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1284
ff_mp4_read_dec_config_descr
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb)
Definition: isom.c:328
mpegts_find_stream_type
static void mpegts_find_stream_type(AVStream *st, uint32_t stream_type, const StreamType *types)
Definition: mpegts.c:891
MpegTSContext::auto_guess
int auto_guess
if true, all pids are analyzed to find streams
Definition: mpegts.c:140
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_CODEC_ID_DVB_TELETEXT
@ AV_CODEC_ID_DVB_TELETEXT
Definition: codec_id.h:546
clear_avprogram
static void clear_avprogram(MpegTSContext *ts, unsigned int programid)
Definition: mpegts.c:287
CHECK_BLOCK
#define CHECK_BLOCK
AVOption
AVOption.
Definition: opt.h:251
MpegTSSectionFilter
Definition: mpegts.c:86
MPEGTS_SECTION
@ MPEGTS_SECTION
Definition: mpegts.c:68
getstr8
static char * getstr8(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:694
MpegTSSectionFilter::section_h_size
int section_h_size
Definition: mpegts.c:88
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:246
data
const char data[16]
Definition: mxf.c:146
opus.h
MpegTSFilter::section_filter
MpegTSSectionFilter section_filter
Definition: mpegts.c:108
HLS_SAMPLE_ENC_types
static const StreamType HLS_SAMPLE_ENC_types[]
Definition: mpegts.c:851
MpegTSState
MpegTSState
Definition: mpegts.c:237
MP4SLDescrTag
#define MP4SLDescrTag
Definition: isom.h:341
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
MP4DescrParseContext::s
AVFormatContext * s
Definition: mpegts.c:1454
buffer_pool_get
static AVBufferRef * buffer_pool_get(MpegTSContext *ts, int size)
Definition: mpegts.c:1127
PES_HEADER_SIZE
#define PES_HEADER_SIZE
Definition: mpegts.c:247
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:551
AVFormatContext::programs
AVProgram ** programs
Definition: avformat.h:1385
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:66
mathematics.h
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:456
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
SetServiceCallback
void SetServiceCallback(void *opaque, int ret)
Definition: mpegts.c:84
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1439
avcodec_is_open
int avcodec_is_open(AVCodecContext *s)
Definition: avcodec.c:704
Stream::idx
int idx
Definition: mpegts.c:113
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:545
ff_read_frame_flush
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: seek.c:717
add_pid_to_program
static void add_pid_to_program(struct Program *p, unsigned int pid)
Definition: mpegts.c:333
PESContext::pts
int64_t pts
Definition: mpegts.c:266
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:479
FFIOContext
Definition: avio_internal.h:29
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:649
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:73
MpegTSContext::nb_prg
unsigned int nb_prg
structure to keep track of Program->pids mapping
Definition: mpegts.c:173
MpegTSPESFilter::pes_cb
PESCallback * pes_cb
Definition: mpegts.c:78
Program::streams
struct Stream streams[MAX_STREAMS_PER_PROGRAM]
Definition: mpegts.c:124
AV_CODEC_ID_BIN_DATA
@ AV_CODEC_ID_BIN_DATA
Definition: codec_id.h:579
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
PESContext::pcr_pid
int pcr_pid
if -1 then all packets containing PCR are considered
Definition: mpegts.c:252
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:815
SectionHeader::id
uint16_t id
Definition: mpegts.c:648
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:312
SLConfigDescr::use_idle
int use_idle
Definition: mpegts.h:180
crc.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:465
PROBE_PACKET_MAX_BUF
#define PROBE_PACKET_MAX_BUF
Definition: mpegts.c:63
mpegtsraw_class
static const AVClass mpegtsraw_class
Definition: mpegts.c:228
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
PESContext::state
enum MpegTSState state
Definition: mpegts.c:258
MpegTSFilter::pes_filter
MpegTSPESFilter pes_filter
Definition: mpegts.c:107
METADATA_DESCRIPTOR
#define METADATA_DESCRIPTOR
Definition: mpegts.h:164
SLConfigDescr::inst_bitrate_len
int inst_bitrate_len
Definition: mpegts.h:185
avpriv_set_pts_info
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:763
REGISTRATION_DESCRIPTOR
#define REGISTRATION_DESCRIPTOR
Definition: mpegts.h:159
mpegts_read_close
static int mpegts_read_close(AVFormatContext *s)
Definition: mpegts.c:3293
mpegts_raw_read_packet
static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegts.c:3202
find_matching_stream
static AVStream * find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid, int stream_identifier, int pmt_stream_idx, struct Program *p)
Definition: mpegts.c:2243
update_av_program_info
static void update_av_program_info(AVFormatContext *s, unsigned int programid, unsigned int pid, int version)
Definition: mpegts.c:349
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:413
MP4ODescrTag
#define MP4ODescrTag
Definition: isom.h:336
PESCallback
int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos)
Definition: mpegts.c:74
VIDEO_STREAM_DESCRIPTOR
#define VIDEO_STREAM_DESCRIPTOR
Definition: mpegts.h:158
STREAM_ID_DSMCC_STREAM
#define STREAM_ID_DSMCC_STREAM
Definition: mpegts.h:151
av_add_index_entry
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:120
pat_cb
static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2531
GetBitContext
Definition: get_bits.h:61
Program::nb_pids
unsigned int nb_pids
Definition: mpegts.c:121
SLConfigDescr::use_padding
int use_padding
Definition: mpegts.h:178
mp4_read_iods
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:1665
AVProgram::discard
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:1143
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
AV_DISPOSITION_STILL_IMAGE
#define AV_DISPOSITION_STILL_IMAGE
The video stream contains still images.
Definition: avformat.h:918
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:510
FFStream::avctx
struct AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:223
SectionHeader::last_sec_num
uint8_t last_sec_num
Definition: mpegts.c:652
val
static double val(void *priv, double ch)
Definition: aeval.c:77
EIT_TID
#define EIT_TID
Definition: mpegts.h:95
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
MP4IODescrTag
#define MP4IODescrTag
Definition: isom.h:337
PESContext::sl
SLConfigDescr sl
Definition: mpegts.c:270
SLConfigDescr::use_rand_acc_pt
int use_rand_acc_pt
Definition: mpegts.h:177
SDT_PID
#define SDT_PID
Definition: mpegts.h:43
av_new_program
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition: avformat.c:304
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:436
PESContext::stream
AVFormatContext * stream
Definition: mpegts.c:255
SLConfigDescr::timestamp_len
int timestamp_len
Definition: mpegts.h:182
MAX_SECTION_SIZE
#define MAX_SECTION_SIZE
Definition: mpegts.h:34
av_dovi_alloc
AVDOVIDecoderConfigurationRecord * av_dovi_alloc(size_t *size)
Allocate a AVDOVIDecoderConfigurationRecord structure and initialize its fields to default values.
Definition: dovi_meta.c:24
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:540
PESContext::sub_st
AVStream * sub_st
stream for the embedded AC3 stream in HDMV TrueHD
Definition: mpegts.c:257
ff_mp4_parse_es_descr
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition: isom.c:303
SectionHeader::current_next
uint8_t current_next
Definition: mpegts.c:650
MpegTSContext::merge_pmt_versions
int merge_pmt_versions
Definition: mpegts.c:166
AV_DISPOSITION_CLEAN_EFFECTS
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition: avformat.h:871
PESContext::header
uint8_t header[MAX_PES_HEADER_SIZE]
Definition: mpegts.c:268
avassert.h
MPEGTS_OPTIONS
#define MPEGTS_OPTIONS
Definition: mpegts.c:185
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:792
MpegTSContext::pos47_full
int64_t pos47_full
Definition: mpegts.c:137
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
avpriv_mpegts_parse_close
void avpriv_mpegts_parse_close(MpegTSContext *ts)
Definition: mpegts.c:3422
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
AVInputFormat
Definition: avformat.h:656
StreamType::codec_id
enum AVCodecID codec_id
Definition: mpegts.c:794
PESContext
Definition: mpegts.c:250
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:430
Mp4Descr::sl
SLConfigDescr sl
Definition: mpegts.h:195
opus_coupled_stream_cnt
static const uint8_t opus_coupled_stream_cnt[9]
Definition: mpegts.c:1791
AVFormatContext::ctx_flags
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1265
AVProgram::id
int id
Definition: avformat.h:1141
ff_parse_mpeg2_descriptor
int ff_parse_mpeg2_descriptor(AVFormatContext *fc, AVStream *st, int stream_type, 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:1810
MpegTSContext::pools
AVBufferPool * pools[32]
Definition: mpegts.c:182
parse_pcr
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
Definition: mpegts.c:3064
AV_CODEC_ID_S302M
@ AV_CODEC_ID_S302M
Definition: codec_id.h:352
av_buffer_pool_get
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:384
MpegTSContext::stream
AVFormatContext * stream
Definition: mpegts.c:133
AV_CODEC_ID_MPEG4SYSTEMS
@ AV_CODEC_ID_MPEG4SYSTEMS
FAKE codec to indicate a MPEG-4 Systems stream (only used by libavformat)
Definition: codec_id.h:586
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:97
mpegts_get_pcr
static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: mpegts.c:3300
mpegts_class
static const AVClass mpegts_class
Definition: mpegts.c:209
AVFormatContext::nb_programs
unsigned int nb_programs
Definition: avformat.h:1384
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:661
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:455
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
FF_PROFILE_ARIB_PROFILE_C
#define FF_PROFILE_ARIB_PROFILE_C
Definition: avcodec.h:1666
bits
uint8_t bits
Definition: vp3data.h:128
SLConfigDescr::degr_prior_len
int degr_prior_len
Definition: mpegts.h:186
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1551
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
mpegts_open_filter
static MpegTSFilter * mpegts_open_filter(MpegTSContext *ts, unsigned int pid, enum MpegTSFilterType type)
Definition: mpegts.c:487
AVDOVIDecoderConfigurationRecord::dv_profile
uint8_t dv_profile
Definition: dovi_meta.h:55
PES_START_SIZE
#define PES_START_SIZE
Definition: mpegts.c:246
MpegTSContext::resync_size
int resync_size
Definition: mpegts.c:165
channels
channels
Definition: aptx.h:31
get_bits.h
SectionHeader::sec_num
uint8_t sec_num
Definition: mpegts.c:651
STREAM_ID_TYPE_E_STREAM
#define STREAM_ID_TYPE_E_STREAM
Definition: mpegts.h:152
nb_streams
static int nb_streams
Definition: ffprobe.c:308
PESContext::stream_type
int stream_type
Definition: mpegts.c:253
parse_MP4IODescrTag
static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1505
PMT_TID
#define PMT_TID
Definition: mpegts.h:81
skip_identical
static int skip_identical(const SectionHeader *h, MpegTSSectionFilter *tssf)
Definition: mpegts.c:655
opus_stream_cnt
static const uint8_t opus_stream_cnt[9]
Definition: mpegts.c:1795
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
AVDOVIDecoderConfigurationRecord::dv_version_major
uint8_t dv_version_major
Definition: dovi_meta.h:53
MPEGTS_SKIP
@ MPEGTS_SKIP
Definition: mpegts.c:242
MpegTSPESFilter::opaque
void * opaque
Definition: mpegts.c:79
get8
static int get8(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:666
discard_pid
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:379
AV_CODEC_ID_ARIB_CAPTION
@ AV_CODEC_ID_ARIB_CAPTION
Definition: codec_id.h:564
if
if(ret)
Definition: filter_design.txt:179
MpegTSContext::cur_pcr
int64_t cur_pcr
used to estimate the exact PCR
Definition: mpegts.c:148
clear_programs
static void clear_programs(MpegTSContext *ts)
Definition: mpegts.c:311
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:386
MP4ESDescrTag
#define MP4ESDescrTag
Definition: isom.h:338
AV_CODEC_ID_AVS3
@ AV_CODEC_ID_AVS3
Definition: codec_id.h:248
MP4DescrParseContext::active_descr
Mp4Descr * active_descr
Definition: mpegts.c:1457
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:76
AVFormatContext
Format I/O context.
Definition: avformat.h:1216
FMC_DESCRIPTOR
#define FMC_DESCRIPTOR
Definition: mpegts.h:163
internal.h
MpegTSContext::pcr_incr
int64_t pcr_incr
used to estimate the exact PCR
Definition: mpegts.c:149
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:357
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1108
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVDOVIDecoderConfigurationRecord::dv_level
uint8_t dv_level
Definition: dovi_meta.h:56
av_program_add_stream_index
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition: avformat.c:335
AVDOVIDecoderConfigurationRecord::dv_bl_signal_compatibility_id
uint8_t dv_bl_signal_compatibility_id
Definition: dovi_meta.h:60
MPEGTS_HEADER
@ MPEGTS_HEADER
Definition: mpegts.c:238
MpegTSSectionFilter::crc
unsigned crc
Definition: mpegts.c:90
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
MpegTSContext::stop_parse
int stop_parse
stop parsing loop
Definition: mpegts.c:153
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1167
isom.h
AV_CODEC_ID_TIMED_ID3
@ AV_CODEC_ID_TIMED_ID3
Definition: codec_id.h:578
MpegTSFilter::u
union MpegTSFilter::@294 u
MpegTSContext::current_pid
int current_pid
Definition: mpegts.c:179
MpegTSSectionFilter::section_index
int section_index
Definition: mpegts.c:87
MpegTSContext::last_pos
int64_t last_pos
to detect seek
Definition: mpegts.c:157
Mp4Descr::es_id
int es_id
Definition: mpegts.h:192
MpegTSFilter::es_id
int es_id
Definition: mpegts.c:101
MPEGTS_PESHEADER
@ MPEGTS_PESHEADER
Definition: mpegts.c:239
DESC_types
static const StreamType DESC_types[]
Definition: mpegts.c:882
PESContext::extended_stream_id
int extended_stream_id
Definition: mpegts.c:264
av_stream_add_side_data
int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as stream side data.
Definition: avformat.c:155
Mp4Descr::dec_config_descr_len
int dec_config_descr_len
Definition: mpegts.h:193
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
MpegTSSectionFilter::section_buf
uint8_t * section_buf
Definition: mpegts.c:92
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
eit_cb
static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2618
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:322
MpegTSFilter::type
enum MpegTSFilterType type
Definition: mpegts.c:105
mpegts_open_pcr_filter
static MpegTSFilter * mpegts_open_pcr_filter(MpegTSContext *ts, unsigned int pid)
Definition: mpegts.c:553
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:453
SectionHeader::version
uint8_t version
Definition: mpegts.c:649
seek_back
static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos)
Definition: mpegts.c:3090
SLConfigDescr::ocr_len
int ocr_len
Definition: mpegts.h:183
AVProgram::stream_index
unsigned int * stream_index
Definition: avformat.h:1144
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:1019
AV_CODEC_ID_MPEG2TS
@ AV_CODEC_ID_MPEG2TS
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: codec_id.h:584
add_pes_stream
static PESContext * add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
Definition: mpegts.c:1428
MpegTSFilterType
MpegTSFilterType
Definition: mpegts.c:66
AV_DICT_DONT_OVERWRITE
#define AV_DICT_DONT_OVERWRITE
Don't overwrite existing entries.
Definition: dict.h:81
StreamType
Definition: mpegts.c:791
AV_CODEC_ID_SMPTE_KLV
@ AV_CODEC_ID_SMPTE_KLV
Definition: codec_id.h:576
ff_mp4_read_descr
int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag)
Definition: isom.c:294
mpegts_open_section_filter
static MpegTSFilter * mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid, SectionCallback *section_cb, void *opaque, int check_crc)
Definition: mpegts.c:510
SLConfigDescr::packet_seq_num_len
int packet_seq_num_len
Definition: mpegts.h:188
mpegts_open_pes_filter
static MpegTSFilter * mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, PESCallback *pes_cb, void *opaque)
Definition: mpegts.c:537
PESContext::ts
MpegTSContext * ts
Definition: mpegts.c:254
OEITS_END_TID
#define OEITS_END_TID
Definition: mpegts.h:100
init_MP4DescrParseContext
static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int max_descr_count)
Definition: mpegts.c:1464
MAX_PES_HEADER_SIZE
#define MAX_PES_HEADER_SIZE
Definition: mpegts.c:248
MAX_PACKET_READAHEAD
#define MAX_PACKET_READAHEAD
Definition: mpegts.c:3200
MAX_PIDS_PER_PROGRAM
#define MAX_PIDS_PER_PROGRAM
Definition: mpegts.c:118
MpegTSSectionFilter::end_of_section_reached
unsigned int end_of_section_reached
Definition: mpegts.c:94
index
int index
Definition: gxfenc.c:89
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
parse_MP4ODescrTag
static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1518
mpegts_push_data
static int mpegts_push_data(MpegTSFilter *filter, const uint8_t *buf, int buf_size, int is_start, int64_t pos)
Definition: mpegts.c:1140
SLConfigDescr::use_au_start
int use_au_start
Definition: mpegts.h:175
new_data_packet
static void new_data_packet(const uint8_t *buffer, int len, AVPacket *pkt)
Definition: mpegts.c:1002
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:475
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:80
SDT_TID
#define SDT_TID
Definition: mpegts.h:87
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:437
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
SCTE_types
static const StreamType SCTE_types[]
Definition: mpegts.c:838
MPEGTS_PES
@ MPEGTS_PES
Definition: mpegts.c:67
f
f
Definition: af_crystalizer.c:122
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
AVMediaType
AVMediaType
Definition: avutil.h:199
MP4DescrParseContext::descr
Mp4Descr * descr
Definition: mpegts.c:1456
AVPacket::size
int size
Definition: packet.h:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
MpegTSContext::max_packet_size
int max_packet_size
Definition: mpegts.c:167
FFStream
Definition: internal.h:196
SectionHeader::tid
uint8_t tid
Definition: mpegts.c:647
reset_pes_packet_state
static void reset_pes_packet_state(PESContext *pes)
Definition: mpegts.c:993
FFIOContext::pub
AVIOContext pub
Definition: avio_internal.h:30
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:439
Program
Definition: mpegts.c:119
MpegTSContext
Definition: mpegts.c:130
MpegTSFilter
Definition: mpegts.c:99
size
int size
Definition: twinvq_data.h:10344
MPEGTS_PAYLOAD
@ MPEGTS_PAYLOAD
Definition: mpegts.c:241
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
MpegTSContext::raw_packet_size
int raw_packet_size
raw packet size, including FEC if present
Definition: mpegts.c:135
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
finished_reading_packet
static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
Definition: mpegts.c:2960
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
section
Definition: ffprobe.c:157
STREAM_ID_PRIVATE_STREAM_2
#define STREAM_ID_PRIVATE_STREAM_2
Definition: mpegts.h:146
SLConfigDescr::use_au_end
int use_au_end
Definition: mpegts.h:176
MpegTSSectionFilter::check_crc
unsigned int check_crc
Definition: mpegts.c:93
MpegTSContext::skip_clear
int skip_clear
Definition: mpegts.c:160
AVCodecParameters::profile
int profile
Codec-specific bitstream restrictions that the stream conforms to.
Definition: codec_par.h:122
mpegts_get_dts
static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: mpegts.c:3334
AV_CODEC_ID_OPUS
@ AV_CODEC_ID_OPUS
Definition: codec_id.h:495
mpegts_read_packet
static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegts.c:3249
ff_find_stream_index
int ff_find_stream_index(const AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: demux_utils.c:371
parse_MP4ESDescrTag
static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1532
buffer.h
AV_DISPOSITION_HEARING_IMPAIRED
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition: avformat.h:863
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:634
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:233
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1057
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
SectionCallback
void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len)
Definition: mpegts.c:82
mpeg.h
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
FFStream::pts_wrap_behavior
int pts_wrap_behavior
Options for behavior, when a wrap is detected.
Definition: internal.h:334
PESContext::pid
int pid
Definition: mpegts.c:251
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:62
version
version
Definition: libkvazaar.c:313
FFStream::probe_packets
int probe_packets
Number of packets to buffer for codec probing.
Definition: internal.h:383
handle_packet
static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
Definition: mpegts.c:2762
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
update_offsets
static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
Definition: mpegts.c:1484
get_bits64
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
Definition: get_bits.h:572
EIT_PID
#define EIT_PID
Definition: mpegts.h:45
is_pes_stream
static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
Definition: mpegts.c:2305
FF_PROFILE_ARIB_PROFILE_A
#define FF_PROFILE_ARIB_PROFILE_A
Definition: avcodec.h:1665
SectionHeader
Definition: mpegts.c:646
ISO_639_LANGUAGE_DESCRIPTOR
#define ISO_639_LANGUAGE_DESCRIPTOR
Definition: mpegts.h:160
MP4DescrParseContext::pb
FFIOContext pb
Definition: mpegts.c:1455
log.h
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
avio_internal.h
IOD_DESCRIPTOR
#define IOD_DESCRIPTOR
Definition: mpegts.h:161
parse_stream_identifier_desc
static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
Definition: mpegts.c:2269
MAX_MP4_DESCR_COUNT
#define MAX_MP4_DESCR_COUNT
Definition: mpegts.c:54
PESContext::data_index
int data_index
Definition: mpegts.c:260
internal.h
get_program
static struct Program * get_program(MpegTSContext *ts, unsigned int programid)
Definition: mpegts.c:276
PAT_TID
#define PAT_TID
Definition: mpegts.h:79
ff_mpegts_demuxer
const AVInputFormat ff_mpegts_demuxer
Definition: mpegts.c:3428
MpegTSContext::pids
MpegTSFilter * pids[NB_PID_MAX]
filters for various streams specified by PMT + for the PAT and PMT
Definition: mpegts.c:178
PESContext::pes_header_size
int pes_header_size
Definition: mpegts.c:263
ffio_init_context
void ffio_init_context(FFIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:81
AV_CODEC_ID_CAVS
@ AV_CODEC_ID_CAVS
Definition: codec_id.h:139
get16
static int get16(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:679
parse_section_header
static int parse_section_header(SectionHeader *h, const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:761
MpegTSContext::epg_stream
AVStream * epg_stream
Definition: mpegts.c:181
common.h
AV_CODEC_ID_EPG
@ AV_CODEC_ID_EPG
Definition: codec_id.h:571
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:664
mpegts_resync
static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet)
Definition: mpegts.c:2892
MpegTSContext::crc_validity
int8_t crc_validity[NB_PID_MAX]
Definition: mpegts.c:176
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
PESContext::st
AVStream * st
Definition: mpegts.c:256
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:264
AVProgram
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1140
handle_packets
static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
Definition: mpegts.c:2968
AV_CODEC_ID_VC1
@ AV_CODEC_ID_VC1
Definition: codec_id.h:122
demux.h
MpegTSContext::prg
struct Program * prg
Definition: mpegts.c:174
AV_DISPOSITION_DEPENDENT
#define AV_DISPOSITION_DEPENDENT
The audio stream is intended to be mixed with another stream before presentation.
Definition: avformat.h:914
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
AV_CRC_32_IEEE
@ AV_CRC_32_IEEE
Definition: crc.h:52
MpegTSPESFilter
Definition: mpegts.c:77
SLConfigDescr::au_len
int au_len
Definition: mpegts.h:184
avcodec.h
MpegTSFilter::last_pcr
int64_t last_pcr
Definition: mpegts.c:103
MpegTSSectionFilter::last_crc
unsigned last_crc
Definition: mpegts.c:91
language
Undefined Behavior In the C language
Definition: undefined.txt:3
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:1008
mid_pred
#define mid_pred
Definition: mathops.h:98
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition: avformat.h:867
MP4DescrParseContext
Definition: mpegts.c:1453
tag
uint32_t tag
Definition: movenc.c:1647
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:962
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:948
MpegTSSectionFilter::last_ver
int last_ver
Definition: mpegts.c:89
AVFormatContext::ts_id
int ts_id
Transport stream id.
Definition: avformat.h:1558
AV_PKT_DATA_DOVI_CONF
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
Definition: packet.h:284
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:262
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
Mp4Descr::dec_config_descr
uint8_t * dec_config_descr
Definition: mpegts.h:194
SLConfigDescr::use_timestamps
int use_timestamps
Definition: mpegts.h:179
avio_rb16
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:777
mp4_read_od
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:1681
scte_data_cb
static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1762
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
dovi_meta.h
m4sl_cb
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1697
dict.h
AV_DISPOSITION_DESCRIPTIONS
#define AV_DISPOSITION_DESCRIPTIONS
The subtitle stream contains a textual description of the video content.
Definition: avformat.h:903
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
TS_MAX_PACKET_SIZE
#define TS_MAX_PACKET_SIZE
Definition: mpegts.h:30
M4OD_TID
#define M4OD_TID
Definition: mpegts.h:84
av_get_media_type_string
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
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:956
R8_CHECK_CLIP_MAX
#define R8_CHECK_CLIP_MAX(dst, maxv)
probe
static int probe(const AVProbeData *p)
Definition: act.c:38
mpegts_probe
static int mpegts_probe(const AVProbeData *p)
Definition: mpegts.c:3021
PESContext::PES_packet_length
int PES_packet_length
Definition: mpegts.c:262
AV_OPT_FLAG_EXPORT
#define AV_OPT_FLAG_EXPORT
The option is intended for exporting values to the caller.
Definition: opt.h:289
clear_program
static void clear_program(struct Program *p)
Definition: mpegts.c:302
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:230
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
av_crc
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:392
NB_PID_MAX
#define NB_PID_MAX
Definition: mpegts.h:32
AVDOVIDecoderConfigurationRecord::bl_present_flag
uint8_t bl_present_flag
Definition: dovi_meta.h:59
SL_DESCRIPTOR
#define SL_DESCRIPTOR
Definition: mpegts.h:162
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:643
PESContext::stream_id
uint8_t stream_id
Definition: mpegts.c:265
parse_MP4SLDescrTag
static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1568
avpriv_mpegts_parse_open
MpegTSContext * avpriv_mpegts_parse_open(AVFormatContext *s)
Definition: mpegts.c:3375
PESContext::buffer
AVBufferRef * buffer
Definition: mpegts.c:269
CHECK_COUNT
#define CHECK_COUNT
AVDOVIDecoderConfigurationRecord::rpu_present_flag
uint8_t rpu_present_flag
Definition: dovi_meta.h:57
mpegts_free
static void mpegts_free(MpegTSContext *ts)
Definition: mpegts.c:3279
HDMV_types
static const StreamType HDMV_types[]
Definition: mpegts.c:822
AVDOVIDecoderConfigurationRecord::el_present_flag
uint8_t el_present_flag
Definition: dovi_meta.h:58
AVPacket::stream_index
int stream_index
Definition: packet.h:376
AVPROBE_SCORE_STREAM_RETRY
#define AVPROBE_SCORE_STREAM_RETRY
Definition: avformat.h:461
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:349
SLConfigDescr::timestamp_res
int timestamp_res
Definition: mpegts.h:181
ISO_types
static const StreamType ISO_types[]
Definition: mpegts.c:797
AVDOVIDecoderConfigurationRecord::dv_version_minor
uint8_t dv_version_minor
Definition: dovi_meta.h:54
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
hex_dump_debug
#define hex_dump_debug(class, buf, size)
Definition: internal.h:40
read_sl_header
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
Definition: mpegts.c:1056
AVFMT_TS_DISCONT
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:483
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
MpegTSContext::pkt
AVPacket * pkt
packet containing Audio/Video data
Definition: mpegts.c:155
mpegts_read_header
static int mpegts_read_header(AVFormatContext *s)
Definition: mpegts.c:3099
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
MPEGTS_PCR
@ MPEGTS_PCR
Definition: mpegts.c:69
FFStream::request_probe
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: internal.h:265
Program::id
unsigned int id
Definition: mpegts.c:120
STREAM_ID_ECM_STREAM
#define STREAM_ID_ECM_STREAM
Definition: mpegts.h:149
MpegTSSectionFilter::opaque
void * opaque
Definition: mpegts.c:96
PESContext::merged_st
int merged_st
Definition: mpegts.c:271
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
TS_FEC_PACKET_SIZE
#define TS_FEC_PACKET_SIZE
Definition: mpegts.h:27
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
MAX_STREAMS_PER_PROGRAM
#define MAX_STREAMS_PER_PROGRAM
Definition: mpegts.c:117
AVPacket
This structure stores compressed data.
Definition: packet.h:351
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
STREAM_ID_PROGRAM_STREAM_DIRECTORY
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY
Definition: mpegts.h:155
MpegTSFilter::last_cc
int last_cc
Definition: mpegts.c:102
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:86
MpegTSContext::scan_all_pmts
int scan_all_pmts
Definition: mpegts.c:163
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
MP4DescrParseContext::predefined_SLConfigDescriptor_seen
int predefined_SLConfigDescriptor_seen
Definition: mpegts.c:1461
FFMAX3
#define FFMAX3(a, b, c)
Definition: macros.h:48
Stream
Definition: mpegts.c:112
d
d
Definition: ffmpeg_filter.c:156
MP4DescrParseContext::level
int level
Definition: mpegts.c:1460
bytestream.h
convert_header.str
string str
Definition: convert_header.py:20
FFStream::stream_identifier
int stream_identifier
Stream Identifier This is the MPEG-TS stream identifier +1 0 means unknown.
Definition: internal.h:399
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:798
MpegTSContext::skip_unknown_pmt
int skip_unknown_pmt
Definition: mpegts.c:161
raw_options
static const AVOption raw_options[]
Definition: mpegts.c:216
FFStream::need_context_update
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition: internal.h:240
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
opus_channel_map
static const uint8_t opus_channel_map[8][8]
Definition: mpegts.c:1799
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:91
Program::pids
unsigned int pids[MAX_PIDS_PER_PROGRAM]
Definition: mpegts.c:122
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:484
ff_mpegtsraw_demuxer
const AVInputFormat ff_mpegtsraw_demuxer
Definition: mpegts.c:3441
parse_mp4_descr
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, int target_tag)
Definition: mpegts.c:1611
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_HDMV_TEXT_SUBTITLE
@ AV_CODEC_ID_HDMV_TEXT_SUBTITLE
Definition: codec_id.h:562
TS_PACKET_SIZE
#define TS_PACKET_SIZE
Definition: mpegts.h:29
MpegTSSectionFilter::section_cb
SectionCallback * section_cb
Definition: mpegts.c:95
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
PROBE_PACKET_MARGIN
#define PROBE_PACKET_MARGIN
Definition: mpegts.c:64
h
h
Definition: vp9dsp_template.c:2038
read_timestamp
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:276
get_ts64
static uint64_t get_ts64(GetBitContext *gb, int bits)
Definition: mpegts.c:1049
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:988
get_packet_size
static int get_packet_size(AVFormatContext *s)
Definition: mpegts.c:608
analyze
static int analyze(const uint8_t *buf, int size, int packet_size, int probe)
Definition: mpegts.c:579
write_section_data
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:420
ff_reduce_index
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:48
read_packet
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
Definition: mpegts.c:2935
MpegTSContext::mpeg2ts_compute_pcr
int mpeg2ts_compute_pcr
compute exact PCR for each transport stream packet
Definition: mpegts.c:143
REGD_types
static const StreamType REGD_types[]
Definition: mpegts.c:859
MISC_types
static const StreamType MISC_types[]
Definition: mpegts.c:844
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
add_program
static struct Program * add_program(MpegTSContext *ts, unsigned int programid)
Definition: mpegts.c:317
options
static const AVOption options[]
Definition: mpegts.c:188
snprintf
#define snprintf
Definition: snprintf.h:34
PESContext::ts_packet_pos
int64_t ts_packet_pos
position of first TS packet of this PES packet
Definition: mpegts.c:267
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
AVProgram::pcr_pid
int pcr_pid
Definition: avformat.h:1150
FFStream::pts_wrap_reference
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition: internal.h:322
Mp4Descr
Definition: mpegts.h:191
SLConfigDescr
Definition: mpegts.h:174
avio_read_partial
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:715
sdt_cb
static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2672
MpegTSContext::fix_teletext_pts
int fix_teletext_pts
fix dvb teletext pts
Definition: mpegts.c:146
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
ff_alloc_extradata
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:238
AVDOVIDecoderConfigurationRecord
Definition: dovi_meta.h:52
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:377
AV_CODEC_ID_SCTE_35
@ AV_CODEC_ID_SCTE_35
Contain timestamp estimated through PCR of program stream.
Definition: codec_id.h:570
StreamType::codec_type
enum AVMediaType codec_type
Definition: mpegts.c:793