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