FFmpeg  4.4.8
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->merged_st) {
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 == 0x15 && buf_size >= 5) {
1303  /* skip metadata access unit header */
1304  pes->pes_header_size += 5;
1305  p += 5;
1306  buf_size -= 5;
1307  }
1308  if ( pes->ts->fix_teletext_pts
1311  ) {
1312  AVProgram *p = NULL;
1313  int pcr_found = 0;
1314  while ((p = av_find_program_from_stream(pes->stream, p, pes->st->index))) {
1315  if (p->pcr_pid != -1 && p->discard != AVDISCARD_ALL) {
1316  MpegTSFilter *f = pes->ts->pids[p->pcr_pid];
1317  if (f) {
1318  AVStream *st = NULL;
1319  if (f->type == MPEGTS_PES) {
1320  PESContext *pcrpes = f->u.pes_filter.opaque;
1321  if (pcrpes)
1322  st = pcrpes->st;
1323  } else if (f->type == MPEGTS_PCR) {
1324  int i;
1325  for (i = 0; i < p->nb_stream_indexes; i++) {
1326  AVStream *pst = pes->stream->streams[p->stream_index[i]];
1327  if (pst->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
1328  st = pst;
1329  }
1330  }
1331  if (f->last_pcr != -1 && !f->discard) {
1332  // teletext packets do not always have correct timestamps,
1333  // the standard says they should be handled after 40.6 ms at most,
1334  // and the pcr error to this packet should be no more than 100 ms.
1335  // TODO: we should interpolate the PCR, not just use the last one
1336  int64_t pcr = f->last_pcr / 300;
1337  pcr_found = 1;
1338  if (st) {
1341  }
1342  if (pes->dts == AV_NOPTS_VALUE || pes->dts < pcr) {
1343  pes->pts = pes->dts = pcr;
1344  } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1345  pes->dts > pcr + 3654 + 9000) {
1346  pes->pts = pes->dts = pcr + 3654 + 9000;
1347  } else if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE &&
1348  pes->dts > pcr + 10*90000) { //10sec
1349  pes->pts = pes->dts = pcr + 3654 + 9000;
1350  }
1351  break;
1352  }
1353  }
1354  }
1355  }
1356 
1357  if (pes->st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT &&
1358  !pcr_found) {
1360  "Forcing DTS/PTS to be unset for a "
1361  "non-trustworthy PES packet for PID %d as "
1362  "PCR hasn't been received yet.\n",
1363  pes->pid);
1364  pes->dts = pes->pts = AV_NOPTS_VALUE;
1365  }
1366  }
1367  }
1368  break;
1369  case MPEGTS_PAYLOAD:
1370  if (pes->buffer) {
1371  if (pes->data_index > 0 &&
1372  pes->data_index + buf_size > pes->total_size) {
1373  ret = new_pes_packet(pes, ts->pkt);
1374  if (ret < 0)
1375  return ret;
1376  pes->total_size = MAX_PES_PAYLOAD;
1377  pes->buffer = buffer_pool_get(ts, pes->total_size);
1378  if (!pes->buffer)
1379  return AVERROR(ENOMEM);
1380  ts->stop_parse = 1;
1381  } else if (pes->data_index == 0 &&
1382  buf_size > pes->total_size) {
1383  // pes packet size is < ts size packet and pes data is padded with 0xff
1384  // not sure if this is legal in ts but see issue #2392
1385  buf_size = pes->total_size;
1386  }
1387  memcpy(pes->buffer->data + pes->data_index, p, buf_size);
1388  pes->data_index += buf_size;
1389  /* emit complete packets with known packet size
1390  * decreases demuxer delay for infrequent packets like subtitles from
1391  * a couple of seconds to milliseconds for properly muxed files.
1392  * total_size is the number of bytes following pes_packet_length
1393  * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
1394  if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
1395  pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
1396  ts->stop_parse = 1;
1397  ret = new_pes_packet(pes, ts->pkt);
1398  if (ret < 0)
1399  return ret;
1400  }
1401  }
1402  buf_size = 0;
1403  break;
1404  case MPEGTS_SKIP:
1405  buf_size = 0;
1406  break;
1407  }
1408  }
1409 
1410  return 0;
1411 }
1412 
1413 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
1414 {
1415  MpegTSFilter *tss;
1416  PESContext *pes;
1417 
1418  /* if no pid found, then add a pid context */
1419  pes = av_mallocz(sizeof(PESContext));
1420  if (!pes)
1421  return 0;
1422  pes->ts = ts;
1423  pes->stream = ts->stream;
1424  pes->pid = pid;
1425  pes->pcr_pid = pcr_pid;
1426  pes->state = MPEGTS_SKIP;
1427  pes->pts = AV_NOPTS_VALUE;
1428  pes->dts = AV_NOPTS_VALUE;
1429  tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
1430  if (!tss) {
1431  av_free(pes);
1432  return 0;
1433  }
1434  return pes;
1435 }
1436 
1437 #define MAX_LEVEL 4
1438 typedef struct MP4DescrParseContext {
1445  int level;
1448 
1450  const uint8_t *buf, unsigned size,
1451  Mp4Descr *descr, int max_descr_count)
1452 {
1453  int ret;
1454  if (size > (1 << 30))
1455  return AVERROR_INVALIDDATA;
1456 
1457  if ((ret = ffio_init_context(&d->pb, (unsigned char *)buf, size, 0,
1458  NULL, NULL, NULL, NULL)) < 0)
1459  return ret;
1460 
1461  d->s = s;
1462  d->level = 0;
1463  d->descr_count = 0;
1464  d->descr = descr;
1465  d->active_descr = NULL;
1466  d->max_descr_count = max_descr_count;
1467 
1468  return 0;
1469 }
1470 
1471 static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
1472 {
1473  int64_t new_off = avio_tell(pb);
1474  (*len) -= new_off - *off;
1475  *off = new_off;
1476 }
1477 
1478 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1479  int target_tag);
1480 
1482 {
1483  while (len > 0) {
1484  int ret = parse_mp4_descr(d, off, len, 0);
1485  if (ret < 0)
1486  return ret;
1487  update_offsets(&d->pb, &off, &len);
1488  }
1489  return 0;
1490 }
1491 
1493 {
1494  avio_rb16(&d->pb); // ID
1495  avio_r8(&d->pb);
1496  avio_r8(&d->pb);
1497  avio_r8(&d->pb);
1498  avio_r8(&d->pb);
1499  avio_r8(&d->pb);
1500  update_offsets(&d->pb, &off, &len);
1501  return parse_mp4_descr_arr(d, off, len);
1502 }
1503 
1505 {
1506  int id_flags;
1507  if (len < 2)
1508  return 0;
1509  id_flags = avio_rb16(&d->pb);
1510  if (!(id_flags & 0x0020)) { // URL_Flag
1511  update_offsets(&d->pb, &off, &len);
1512  return parse_mp4_descr_arr(d, off, len); // ES_Descriptor[]
1513  } else {
1514  return 0;
1515  }
1516 }
1517 
1519 {
1520  int es_id = 0;
1521  int ret = 0;
1522 
1523  if (d->descr_count >= d->max_descr_count)
1524  return AVERROR_INVALIDDATA;
1525  ff_mp4_parse_es_descr(&d->pb, &es_id);
1526  d->active_descr = d->descr + (d->descr_count++);
1527 
1528  d->active_descr->es_id = es_id;
1529  update_offsets(&d->pb, &off, &len);
1530  if ((ret = parse_mp4_descr(d, off, len, MP4DecConfigDescrTag)) < 0)
1531  return ret;
1532  update_offsets(&d->pb, &off, &len);
1533  if (len > 0)
1534  ret = parse_mp4_descr(d, off, len, MP4SLDescrTag);
1535  d->active_descr = NULL;
1536  return ret;
1537 }
1538 
1540  int len)
1541 {
1542  Mp4Descr *descr = d->active_descr;
1543  if (!descr)
1544  return AVERROR_INVALIDDATA;
1546  if (!descr->dec_config_descr)
1547  return AVERROR(ENOMEM);
1548  descr->dec_config_descr_len = len;
1549  avio_read(&d->pb, descr->dec_config_descr, len);
1550  return 0;
1551 }
1552 
1554 {
1555  Mp4Descr *descr = d->active_descr;
1556  int predefined;
1557  if (!descr)
1558  return AVERROR_INVALIDDATA;
1559 
1560 #define R8_CHECK_CLIP_MAX(dst, maxv) do { \
1561  descr->sl.dst = avio_r8(&d->pb); \
1562  if (descr->sl.dst > maxv) { \
1563  descr->sl.dst = maxv; \
1564  return AVERROR_INVALIDDATA; \
1565  } \
1566 } while (0)
1567 
1568  predefined = avio_r8(&d->pb);
1569  if (!predefined) {
1570  int lengths;
1571  int flags = avio_r8(&d->pb);
1572  descr->sl.use_au_start = !!(flags & 0x80);
1573  descr->sl.use_au_end = !!(flags & 0x40);
1574  descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1575  descr->sl.use_padding = !!(flags & 0x08);
1576  descr->sl.use_timestamps = !!(flags & 0x04);
1577  descr->sl.use_idle = !!(flags & 0x02);
1578  descr->sl.timestamp_res = avio_rb32(&d->pb);
1579  avio_rb32(&d->pb);
1580  R8_CHECK_CLIP_MAX(timestamp_len, 63);
1581  R8_CHECK_CLIP_MAX(ocr_len, 63);
1582  R8_CHECK_CLIP_MAX(au_len, 31);
1583  descr->sl.inst_bitrate_len = avio_r8(&d->pb);
1584  lengths = avio_rb16(&d->pb);
1585  descr->sl.degr_prior_len = lengths >> 12;
1586  descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1587  descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1588  } else if (!d->predefined_SLConfigDescriptor_seen){
1589  avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1591  }
1592  return 0;
1593 }
1594 
1596  int target_tag)
1597 {
1598  int tag;
1599  int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
1600  int ret = 0;
1601 
1602  update_offsets(&d->pb, &off, &len);
1603  if (len < 0 || len1 > len || len1 <= 0) {
1604  av_log(d->s, AV_LOG_ERROR,
1605  "Tag %x length violation new length %d bytes remaining %d\n",
1606  tag, len1, len);
1607  return AVERROR_INVALIDDATA;
1608  }
1609 
1610  if (d->level++ >= MAX_LEVEL) {
1611  av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1612  ret = AVERROR_INVALIDDATA;
1613  goto done;
1614  }
1615 
1616  if (target_tag && tag != target_tag) {
1617  av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag,
1618  target_tag);
1619  ret = AVERROR_INVALIDDATA;
1620  goto done;
1621  }
1622 
1623  switch (tag) {
1624  case MP4IODescrTag:
1625  ret = parse_MP4IODescrTag(d, off, len1);
1626  break;
1627  case MP4ODescrTag:
1628  ret = parse_MP4ODescrTag(d, off, len1);
1629  break;
1630  case MP4ESDescrTag:
1631  ret = parse_MP4ESDescrTag(d, off, len1);
1632  break;
1633  case MP4DecConfigDescrTag:
1634  ret = parse_MP4DecConfigDescrTag(d, off, len1);
1635  break;
1636  case MP4SLDescrTag:
1637  ret = parse_MP4SLDescrTag(d, off, len1);
1638  break;
1639  }
1640 
1641 
1642 done:
1643  d->level--;
1644  avio_seek(&d->pb, off + len1, SEEK_SET);
1645  return ret;
1646 }
1647 
1648 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1649  Mp4Descr *descr, int *descr_count, int max_descr_count)
1650 {
1652  int ret;
1653 
1655 
1656  ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1657  if (ret < 0)
1658  return ret;
1659 
1660  ret = parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
1661 
1662  *descr_count += d.descr_count;
1663  return ret;
1664 }
1665 
1666 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1667  Mp4Descr *descr, int *descr_count, int max_descr_count)
1668 {
1670  int ret;
1671 
1672  ret = init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count);
1673  if (ret < 0)
1674  return ret;
1675 
1676  ret = parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
1677 
1678  *descr_count = d.descr_count;
1679  return ret;
1680 }
1681 
1683  int section_len)
1684 {
1685  MpegTSContext *ts = filter->u.section_filter.opaque;
1686  MpegTSSectionFilter *tssf = &filter->u.section_filter;
1687  SectionHeader h;
1688  const uint8_t *p, *p_end;
1689  AVIOContext pb;
1690  int mp4_descr_count = 0;
1691  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
1692  int i, pid;
1693  AVFormatContext *s = ts->stream;
1694 
1695  p_end = section + section_len - 4;
1696  p = section;
1697  if (parse_section_header(&h, &p, p_end) < 0)
1698  return;
1699  if (h.tid != M4OD_TID)
1700  return;
1701  if (skip_identical(&h, tssf))
1702  return;
1703 
1704  mp4_read_od(s, p, (unsigned) (p_end - p), mp4_descr, &mp4_descr_count,
1706 
1707  for (pid = 0; pid < NB_PID_MAX; pid++) {
1708  if (!ts->pids[pid])
1709  continue;
1710  for (i = 0; i < mp4_descr_count; i++) {
1711  PESContext *pes;
1712  AVStream *st;
1713  if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1714  continue;
1715  if (ts->pids[pid]->type != MPEGTS_PES) {
1716  av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1717  continue;
1718  }
1719  pes = ts->pids[pid]->u.pes_filter.opaque;
1720  st = pes->st;
1721  if (!st)
1722  continue;
1723 
1724  pes->sl = mp4_descr[i].sl;
1725 
1726  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1727  mp4_descr[i].dec_config_descr_len, 0,
1728  NULL, NULL, NULL, NULL);
1729  ff_mp4_read_dec_config_descr(s, st, &pb);
1730  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1731  st->codecpar->extradata_size > 0)
1732  st->need_parsing = 0;
1733  if (st->codecpar->codec_id == AV_CODEC_ID_H264 &&
1734  st->codecpar->extradata_size > 0)
1735  st->need_parsing = 0;
1736 
1738  st->internal->need_context_update = 1;
1739  }
1740  }
1741  for (i = 0; i < mp4_descr_count; i++)
1742  av_free(mp4_descr[i].dec_config_descr);
1743 }
1744 
1746  int section_len)
1747 {
1748  AVProgram *prg = NULL;
1749  MpegTSContext *ts = filter->u.section_filter.opaque;
1750 
1751  int idx = ff_find_stream_index(ts->stream, filter->pid);
1752  if (idx < 0)
1753  return;
1754 
1755  /**
1756  * In case we receive an SCTE-35 packet before mpegts context is fully
1757  * initialized.
1758  */
1759  if (!ts->pkt)
1760  return;
1761 
1762  new_data_packet(section, section_len, ts->pkt);
1763  ts->pkt->stream_index = idx;
1764  prg = av_find_program_from_stream(ts->stream, NULL, idx);
1765  if (prg && prg->pcr_pid != -1 && prg->discard != AVDISCARD_ALL) {
1766  MpegTSFilter *f = ts->pids[prg->pcr_pid];
1767  if (f && f->last_pcr != -1)
1768  ts->pkt->pts = ts->pkt->dts = f->last_pcr/300;
1769  }
1770  ts->stop_parse = 1;
1771 
1772 }
1773 
1775  1, 0, 1, 1, 2, 2, 2, 3, 3
1776 };
1777 
1778 static const uint8_t opus_stream_cnt[9] = {
1779  1, 1, 1, 2, 2, 3, 4, 4, 5,
1780 };
1781 
1782 static const uint8_t opus_channel_map[8][8] = {
1783  { 0 },
1784  { 0,1 },
1785  { 0,2,1 },
1786  { 0,1,2,3 },
1787  { 0,4,1,2,3 },
1788  { 0,4,1,2,3,5 },
1789  { 0,4,1,2,3,5,6 },
1790  { 0,6,1,2,3,4,5,7 },
1791 };
1792 
1794  const uint8_t **pp, const uint8_t *desc_list_end,
1795  Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1796  MpegTSContext *ts)
1797 {
1798  const uint8_t *desc_end;
1799  int desc_len, desc_tag, desc_es_id, ext_desc_tag, channels, channel_config_code;
1800  char language[252];
1801  int i;
1802 
1803  desc_tag = get8(pp, desc_list_end);
1804  if (desc_tag < 0)
1805  return AVERROR_INVALIDDATA;
1806  desc_len = get8(pp, desc_list_end);
1807  if (desc_len < 0)
1808  return AVERROR_INVALIDDATA;
1809  desc_end = *pp + desc_len;
1810  if (desc_end > desc_list_end)
1811  return AVERROR_INVALIDDATA;
1812 
1813  av_log(fc, AV_LOG_TRACE, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1814 
1815  if ((st->codecpar->codec_id == AV_CODEC_ID_NONE || st->internal->request_probe > 0) &&
1816  stream_type == STREAM_TYPE_PRIVATE_DATA)
1817  mpegts_find_stream_type(st, desc_tag, DESC_types);
1818 
1819  switch (desc_tag) {
1821  if (get8(pp, desc_end) & 0x1) {
1823  }
1824  break;
1825  case SL_DESCRIPTOR:
1826  desc_es_id = get16(pp, desc_end);
1827  if (desc_es_id < 0)
1828  break;
1829  if (ts && ts->pids[pid])
1830  ts->pids[pid]->es_id = desc_es_id;
1831  for (i = 0; i < mp4_descr_count; i++)
1832  if (mp4_descr[i].dec_config_descr_len &&
1833  mp4_descr[i].es_id == desc_es_id) {
1834  AVIOContext pb;
1835  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1836  mp4_descr[i].dec_config_descr_len, 0,
1837  NULL, NULL, NULL, NULL);
1838  ff_mp4_read_dec_config_descr(fc, st, &pb);
1839  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1840  st->codecpar->extradata_size > 0) {
1841  st->need_parsing = 0;
1842  st->internal->need_context_update = 1;
1843  }
1845  mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1846  }
1847  break;
1848  case FMC_DESCRIPTOR:
1849  if (get16(pp, desc_end) < 0)
1850  break;
1851  if (mp4_descr_count > 0 &&
1853  (st->internal->request_probe == 0 && st->codecpar->codec_id == AV_CODEC_ID_NONE) ||
1854  st->internal->request_probe > 0) &&
1855  mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1856  AVIOContext pb;
1857  ffio_init_context(&pb, mp4_descr->dec_config_descr,
1858  mp4_descr->dec_config_descr_len, 0,
1859  NULL, NULL, NULL, NULL);
1860  ff_mp4_read_dec_config_descr(fc, st, &pb);
1861  if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
1862  st->codecpar->extradata_size > 0) {
1863  st->internal->request_probe = st->need_parsing = 0;
1865  st->internal->need_context_update = 1;
1866  }
1867  }
1868  break;
1869  case 0x56: /* DVB teletext descriptor */
1870  {
1871  uint8_t *extradata = NULL;
1872  int language_count = desc_len / 5, ret;
1873 
1874  if (desc_len > 0 && desc_len % 5 != 0)
1875  return AVERROR_INVALIDDATA;
1876 
1877  if (language_count > 0) {
1878  /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1879  av_assert0(language_count <= sizeof(language) / 4);
1880 
1881  if (st->codecpar->extradata == NULL) {
1882  ret = ff_alloc_extradata(st->codecpar, language_count * 2);
1883  if (ret < 0)
1884  return ret;
1885  }
1886 
1887  if (st->codecpar->extradata_size < language_count * 2)
1888  return AVERROR_INVALIDDATA;
1889 
1890  extradata = st->codecpar->extradata;
1891 
1892  for (i = 0; i < language_count; i++) {
1893  language[i * 4 + 0] = get8(pp, desc_end);
1894  language[i * 4 + 1] = get8(pp, desc_end);
1895  language[i * 4 + 2] = get8(pp, desc_end);
1896  language[i * 4 + 3] = ',';
1897 
1898  memcpy(extradata, *pp, 2);
1899  extradata += 2;
1900 
1901  *pp += 2;
1902  }
1903 
1904  language[i * 4 - 1] = 0;
1905  av_dict_set(&st->metadata, "language", language, 0);
1906  st->internal->need_context_update = 1;
1907  }
1908  }
1909  break;
1910  case 0x59: /* subtitling descriptor */
1911  {
1912  /* 8 bytes per DVB subtitle substream data:
1913  * ISO_639_language_code (3 bytes),
1914  * subtitling_type (1 byte),
1915  * composition_page_id (2 bytes),
1916  * ancillary_page_id (2 bytes) */
1917  int language_count = desc_len / 8, ret;
1918 
1919  if (desc_len > 0 && desc_len % 8 != 0)
1920  return AVERROR_INVALIDDATA;
1921 
1922  if (language_count > 1) {
1923  avpriv_request_sample(fc, "DVB subtitles with multiple languages");
1924  }
1925 
1926  if (language_count > 0) {
1927  uint8_t *extradata;
1928 
1929  /* 4 bytes per language code (3 bytes) with comma or NUL byte should fit language buffer */
1930  av_assert0(language_count <= sizeof(language) / 4);
1931 
1932  if (st->codecpar->extradata == NULL) {
1933  ret = ff_alloc_extradata(st->codecpar, language_count * 5);
1934  if (ret < 0)
1935  return ret;
1936  }
1937 
1938  if (st->codecpar->extradata_size < language_count * 5)
1939  return AVERROR_INVALIDDATA;
1940 
1941  extradata = st->codecpar->extradata;
1942 
1943  for (i = 0; i < language_count; i++) {
1944  language[i * 4 + 0] = get8(pp, desc_end);
1945  language[i * 4 + 1] = get8(pp, desc_end);
1946  language[i * 4 + 2] = get8(pp, desc_end);
1947  language[i * 4 + 3] = ',';
1948 
1949  /* hearing impaired subtitles detection using subtitling_type */
1950  switch (*pp[0]) {
1951  case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1952  case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1953  case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1954  case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1955  case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1956  case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1958  break;
1959  }
1960 
1961  extradata[4] = get8(pp, desc_end); /* subtitling_type */
1962  memcpy(extradata, *pp, 4); /* composition_page_id and ancillary_page_id */
1963  extradata += 5;
1964 
1965  *pp += 4;
1966  }
1967 
1968  language[i * 4 - 1] = 0;
1969  av_dict_set(&st->metadata, "language", language, 0);
1970  st->internal->need_context_update = 1;
1971  }
1972  }
1973  break;
1975  for (i = 0; i + 4 <= desc_len; i += 4) {
1976  language[i + 0] = get8(pp, desc_end);
1977  language[i + 1] = get8(pp, desc_end);
1978  language[i + 2] = get8(pp, desc_end);
1979  language[i + 3] = ',';
1980  switch (get8(pp, desc_end)) {
1981  case 0x01:
1983  break;
1984  case 0x02:
1986  break;
1987  case 0x03:
1990  break;
1991  }
1992  }
1993  if (i && language[0]) {
1994  language[i - 1] = 0;
1995  /* don't overwrite language, as it may already have been set by
1996  * another, more specific descriptor (e.g. supplementary audio) */
1997  av_dict_set(&st->metadata, "language", language, AV_DICT_DONT_OVERWRITE);
1998  }
1999  break;
2001  st->codecpar->codec_tag = bytestream_get_le32(pp);
2002  av_log(fc, AV_LOG_TRACE, "reg_desc=%.4s\n", (char *)&st->codecpar->codec_tag);
2003  if (st->codecpar->codec_id == AV_CODEC_ID_NONE || st->internal->request_probe > 0) {
2005  if (st->codecpar->codec_tag == MKTAG('B', 'S', 'S', 'D'))
2006  st->internal->request_probe = 50;
2007  }
2008  break;
2009  case 0x52: /* stream identifier descriptor */
2010  st->stream_identifier = 1 + get8(pp, desc_end);
2011  break;
2012  case METADATA_DESCRIPTOR:
2013  if (get16(pp, desc_end) == 0xFFFF)
2014  *pp += 4;
2015  if (get8(pp, desc_end) == 0xFF) {
2016  st->codecpar->codec_tag = bytestream_get_le32(pp);
2017  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
2019  }
2020  break;
2021  case 0x7f: /* DVB extension descriptor */
2022  ext_desc_tag = get8(pp, desc_end);
2023  if (ext_desc_tag < 0)
2024  return AVERROR_INVALIDDATA;
2025  if (st->codecpar->codec_id == AV_CODEC_ID_OPUS &&
2026  ext_desc_tag == 0x80) { /* User defined (provisional Opus) */
2027  if (!st->codecpar->extradata) {
2030  if (!st->codecpar->extradata)
2031  return AVERROR(ENOMEM);
2032 
2035 
2036  channel_config_code = get8(pp, desc_end);
2037  if (channel_config_code < 0)
2038  return AVERROR_INVALIDDATA;
2039  if (channel_config_code <= 0x8) {
2040  st->codecpar->extradata[9] = channels = channel_config_code ? channel_config_code : 2;
2041  AV_WL32(&st->codecpar->extradata[12], 48000);
2042  st->codecpar->extradata[18] = channel_config_code ? (channels > 2) : /* Dual Mono */ 255;
2043  st->codecpar->extradata[19] = opus_stream_cnt[channel_config_code];
2044  st->codecpar->extradata[20] = opus_coupled_stream_cnt[channel_config_code];
2045  memcpy(&st->codecpar->extradata[21], opus_channel_map[channels - 1], channels);
2046  } else {
2047  avpriv_request_sample(fc, "Opus in MPEG-TS - channel_config_code > 0x8");
2048  }
2050  st->internal->need_context_update = 1;
2051  }
2052  }
2053  if (ext_desc_tag == 0x06) { /* supplementary audio descriptor */
2054  int flags;
2055 
2056  if (desc_len < 1)
2057  return AVERROR_INVALIDDATA;
2058  flags = get8(pp, desc_end);
2059 
2060  if ((flags & 0x80) == 0) /* mix_type */
2062 
2063  switch ((flags >> 2) & 0x1F) { /* editorial_classification */
2064  case 0x01:
2067  break;
2068  case 0x02:
2070  break;
2071  case 0x03:
2073  break;
2074  }
2075 
2076  if (flags & 0x01) { /* language_code_present */
2077  if (desc_len < 4)
2078  return AVERROR_INVALIDDATA;
2079  language[0] = get8(pp, desc_end);
2080  language[1] = get8(pp, desc_end);
2081  language[2] = get8(pp, desc_end);
2082  language[3] = 0;
2083 
2084  /* This language always has to override a possible
2085  * ISO 639 language descriptor language */
2086  if (language[0])
2087  av_dict_set(&st->metadata, "language", language, 0);
2088  }
2089  }
2090  break;
2091  case 0x6a: /* ac-3_descriptor */
2092  {
2093  int component_type_flag = get8(pp, desc_end) & (1 << 7);
2094  if (component_type_flag) {
2095  int component_type = get8(pp, desc_end);
2096  int service_type_mask = 0x38; // 0b00111000
2097  int service_type = ((component_type & service_type_mask) >> 3);
2098  if (service_type == 0x02 /* 0b010 */) {
2100  av_log(ts ? ts->stream : fc, AV_LOG_DEBUG, "New track disposition for id %u: %u\n", st->id, st->disposition);
2101  }
2102  }
2103  }
2104  break;
2105  case 0x7a: /* enhanced_ac-3_descriptor */
2106  {
2107  int component_type_flag = get8(pp, desc_end) & (1 << 7);
2108  if (component_type_flag) {
2109  int component_type = get8(pp, desc_end);
2110  int service_type_mask = 0x38; // 0b00111000
2111  int service_type = ((component_type & service_type_mask) >> 3);
2112  if (service_type == 0x02 /* 0b010 */) {
2114  av_log(ts ? ts->stream : fc, AV_LOG_DEBUG, "New track disposition for id %u: %u\n", st->id, st->disposition);
2115  }
2116  }
2117  }
2118  break;
2119  case 0xfd: /* ARIB data coding type descriptor */
2120  // STD-B24, fascicle 3, chapter 4 defines private_stream_1
2121  // for captions
2122  if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
2123  // This structure is defined in STD-B10, part 1, listing 5.4 and
2124  // part 2, 6.2.20).
2125  // Listing of data_component_ids is in STD-B10, part 2, Annex J.
2126  // Component tag limits are documented in TR-B14, fascicle 2,
2127  // Vol. 3, Section 2, 4.2.8.1
2128  int actual_component_tag = st->stream_identifier - 1;
2129  int picked_profile = FF_PROFILE_UNKNOWN;
2130  int data_component_id = get16(pp, desc_end);
2131  if (data_component_id < 0)
2132  return AVERROR_INVALIDDATA;
2133 
2134  switch (data_component_id) {
2135  case 0x0008:
2136  // [0x30..0x37] are component tags utilized for
2137  // non-mobile captioning service ("profile A").
2138  if (actual_component_tag >= 0x30 &&
2139  actual_component_tag <= 0x37) {
2140  picked_profile = FF_PROFILE_ARIB_PROFILE_A;
2141  }
2142  break;
2143  case 0x0012:
2144  // component tag 0x87 signifies a mobile/partial reception
2145  // (1seg) captioning service ("profile C").
2146  if (actual_component_tag == 0x87) {
2147  picked_profile = FF_PROFILE_ARIB_PROFILE_C;
2148  }
2149  break;
2150  default:
2151  break;
2152  }
2153 
2154  if (picked_profile == FF_PROFILE_UNKNOWN)
2155  break;
2156 
2159  st->codecpar->profile = picked_profile;
2160  st->internal->request_probe = 0;
2161  }
2162  break;
2163  case 0xb0: /* DOVI video stream descriptor */
2164  {
2165  uint32_t buf;
2167  size_t dovi_size;
2168  int ret;
2169  if (desc_end - *pp < 4) // (8 + 8 + 7 + 6 + 1 + 1 + 1) / 8
2170  return AVERROR_INVALIDDATA;
2171 
2172  dovi = av_dovi_alloc(&dovi_size);
2173  if (!dovi)
2174  return AVERROR(ENOMEM);
2175 
2176  dovi->dv_version_major = get8(pp, desc_end);
2177  dovi->dv_version_minor = get8(pp, desc_end);
2178  buf = get16(pp, desc_end);
2179  dovi->dv_profile = (buf >> 9) & 0x7f; // 7 bits
2180  dovi->dv_level = (buf >> 3) & 0x3f; // 6 bits
2181  dovi->rpu_present_flag = (buf >> 2) & 0x01; // 1 bit
2182  dovi->el_present_flag = (buf >> 1) & 0x01; // 1 bit
2183  dovi->bl_present_flag = buf & 0x01; // 1 bit
2184  if (desc_end - *pp >= 20) { // 4 + 4 * 4
2185  buf = get8(pp, desc_end);
2186  dovi->dv_bl_signal_compatibility_id = (buf >> 4) & 0x0f; // 4 bits
2187  } else {
2188  // 0 stands for None
2189  // Dolby Vision V1.2.93 profiles and levels
2191  }
2192 
2194  (uint8_t *)dovi, dovi_size);
2195  if (ret < 0) {
2196  av_free(dovi);
2197  return ret;
2198  }
2199 
2200  av_log(fc, AV_LOG_TRACE, "DOVI, version: %d.%d, profile: %d, level: %d, "
2201  "rpu flag: %d, el flag: %d, bl flag: %d, compatibility id: %d\n",
2202  dovi->dv_version_major, dovi->dv_version_minor,
2203  dovi->dv_profile, dovi->dv_level,
2204  dovi->rpu_present_flag,
2205  dovi->el_present_flag,
2206  dovi->bl_present_flag,
2208  }
2209  break;
2210  default:
2211  break;
2212  }
2213  *pp = desc_end;
2214  return 0;
2215 }
2216 
2217 static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int programid,
2218  int stream_identifier, int pmt_stream_idx, struct Program *p)
2219 {
2220  AVFormatContext *s = ts->stream;
2221  int i;
2222  AVStream *found = NULL;
2223 
2224  if (stream_identifier) { /* match based on "stream identifier descriptor" if present */
2225  for (i = 0; i < p->nb_streams; i++) {
2226  if (p->streams[i].stream_identifier == stream_identifier)
2227  if (!found || pmt_stream_idx == i) /* fallback to idx based guess if multiple streams have the same identifier */
2228  found = s->streams[p->streams[i].idx];
2229  }
2230  } else if (pmt_stream_idx < p->nb_streams) { /* match based on position within the PMT */
2231  found = s->streams[p->streams[pmt_stream_idx].idx];
2232  }
2233 
2234  if (found) {
2236  "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
2238  i, found->id, pid);
2239  }
2240 
2241  return found;
2242 }
2243 
2244 static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
2245 {
2246  const uint8_t **pp = &p;
2247  const uint8_t *desc_list_end;
2248  const uint8_t *desc_end;
2249  int desc_list_len;
2250  int desc_len, desc_tag;
2251 
2252  desc_list_len = get16(pp, p_end);
2253  if (desc_list_len < 0)
2254  return -1;
2255  desc_list_len &= 0xfff;
2256  desc_list_end = p + desc_list_len;
2257  if (desc_list_end > p_end)
2258  return -1;
2259 
2260  while (1) {
2261  desc_tag = get8(pp, desc_list_end);
2262  if (desc_tag < 0)
2263  return -1;
2264  desc_len = get8(pp, desc_list_end);
2265  if (desc_len < 0)
2266  return -1;
2267  desc_end = *pp + desc_len;
2268  if (desc_end > desc_list_end)
2269  return -1;
2270 
2271  if (desc_tag == 0x52) {
2272  return get8(pp, desc_end);
2273  }
2274  *pp = desc_end;
2275  }
2276 
2277  return -1;
2278 }
2279 
2280 static int is_pes_stream(int stream_type, uint32_t prog_reg_desc)
2281 {
2282  return !(stream_type == 0x13 ||
2283  (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) );
2284 }
2285 
2286 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2287 {
2288  MpegTSContext *ts = filter->u.section_filter.opaque;
2289  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2290  struct Program old_program;
2291  SectionHeader h1, *h = &h1;
2292  PESContext *pes;
2293  AVStream *st;
2294  const uint8_t *p, *p_end, *desc_list_end;
2295  int program_info_length, pcr_pid, pid, stream_type;
2296  int desc_list_len;
2297  uint32_t prog_reg_desc = 0; /* registration descriptor */
2298  int stream_identifier = -1;
2299  struct Program *prg;
2300 
2301  int mp4_descr_count = 0;
2302  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
2303  int i;
2304 
2305  av_log(ts->stream, AV_LOG_TRACE, "PMT: len %i\n", section_len);
2306  hex_dump_debug(ts->stream, section, section_len);
2307 
2308  p_end = section + section_len - 4;
2309  p = section;
2310  if (parse_section_header(h, &p, p_end) < 0)
2311  return;
2312  if (h->tid != PMT_TID)
2313  return;
2314  if (skip_identical(h, tssf))
2315  return;
2316 
2317  av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x sec_num=%d/%d version=%d tid=%d\n",
2318  h->id, h->sec_num, h->last_sec_num, h->version, h->tid);
2319 
2320  if (!ts->scan_all_pmts && ts->skip_changes)
2321  return;
2322 
2323  prg = get_program(ts, h->id);
2324  if (prg)
2325  old_program = *prg;
2326  else
2327  clear_program(&old_program);
2328 
2329  if (ts->skip_unknown_pmt && !prg)
2330  return;
2331  if (prg && prg->nb_pids && prg->pids[0] != ts->current_pid)
2332  return;
2333  if (!ts->skip_clear)
2334  clear_avprogram(ts, h->id);
2335  clear_program(prg);
2336  add_pid_to_program(prg, ts->current_pid);
2337 
2338  pcr_pid = get16(&p, p_end);
2339  if (pcr_pid < 0)
2340  return;
2341  pcr_pid &= 0x1fff;
2342  add_pid_to_program(prg, pcr_pid);
2343  update_av_program_info(ts->stream, h->id, pcr_pid, h->version);
2344 
2345  av_log(ts->stream, AV_LOG_TRACE, "pcr_pid=0x%x\n", pcr_pid);
2346 
2347  program_info_length = get16(&p, p_end);
2348 
2349  if (program_info_length < 0 || (program_info_length & 0xFFF) > p_end - p)
2350  return;
2351  program_info_length &= 0xfff;
2352  while (program_info_length >= 2) {
2353  uint8_t tag, len;
2354  tag = get8(&p, p_end);
2355  len = get8(&p, p_end);
2356 
2357  av_log(ts->stream, AV_LOG_TRACE, "program tag: 0x%02x len=%d\n", tag, len);
2358 
2359  if (len > program_info_length - 2)
2360  // something else is broken, exit the program_descriptors_loop
2361  break;
2362  program_info_length -= len + 2;
2363  if (tag == IOD_DESCRIPTOR && len >= 2) {
2364  get8(&p, p_end); // scope
2365  get8(&p, p_end); // label
2366  len -= 2;
2367  mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
2368  &mp4_descr_count, MAX_MP4_DESCR_COUNT - mp4_descr_count);
2369  } else if (tag == REGISTRATION_DESCRIPTOR && len >= 4) {
2370  prog_reg_desc = bytestream_get_le32(&p);
2371  len -= 4;
2372  }
2373  p += len;
2374  }
2375  p += program_info_length;
2376  if (p >= p_end)
2377  goto out;
2378 
2379  // stop parsing after pmt, we found header
2380  if (!ts->pkt)
2381  ts->stop_parse = 2;
2382 
2383  if (prg)
2384  prg->pmt_found = 1;
2385 
2386  for (i = 0; i < MAX_STREAMS_PER_PROGRAM; i++) {
2387  st = 0;
2388  pes = NULL;
2389  stream_type = get8(&p, p_end);
2390  if (stream_type < 0)
2391  break;
2392  pid = get16(&p, p_end);
2393  if (pid < 0)
2394  goto out;
2395  pid &= 0x1fff;
2396  if (pid == ts->current_pid)
2397  goto out;
2398 
2399  stream_identifier = parse_stream_identifier_desc(p, p_end) + 1;
2400 
2401  /* now create stream */
2402  if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
2403  pes = ts->pids[pid]->u.pes_filter.opaque;
2404  if (ts->merge_pmt_versions && !pes->st) {
2405  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2406  if (st) {
2407  pes->st = st;
2408  pes->stream_type = stream_type;
2409  pes->merged_st = 1;
2410  }
2411  }
2412  if (!pes->st) {
2413  pes->st = avformat_new_stream(pes->stream, NULL);
2414  if (!pes->st)
2415  goto out;
2416  pes->st->id = pes->pid;
2417  }
2418  st = pes->st;
2419  } else if (is_pes_stream(stream_type, prog_reg_desc)) {
2420  if (ts->pids[pid])
2421  mpegts_close_filter(ts, ts->pids[pid]); // wrongly added sdt filter probably
2422  pes = add_pes_stream(ts, pid, pcr_pid);
2423  if (ts->merge_pmt_versions && pes && !pes->st) {
2424  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2425  if (st) {
2426  pes->st = st;
2427  pes->stream_type = stream_type;
2428  pes->merged_st = 1;
2429  }
2430  }
2431  if (pes && !pes->st) {
2432  st = avformat_new_stream(pes->stream, NULL);
2433  if (!st)
2434  goto out;
2435  st->id = pes->pid;
2436  }
2437  } else {
2438  int idx = ff_find_stream_index(ts->stream, pid);
2439  if (idx >= 0) {
2440  st = ts->stream->streams[idx];
2441  }
2442  if (ts->merge_pmt_versions && !st) {
2443  st = find_matching_stream(ts, pid, h->id, stream_identifier, i, &old_program);
2444  }
2445  if (!st) {
2446  st = avformat_new_stream(ts->stream, NULL);
2447  if (!st)
2448  goto out;
2449  st->id = pid;
2451  if (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) {
2452  mpegts_find_stream_type(st, stream_type, SCTE_types);
2453  mpegts_open_section_filter(ts, pid, scte_data_cb, ts, 1);
2454  }
2455  }
2456  }
2457 
2458  if (!st)
2459  goto out;
2460 
2461  if (pes && !pes->stream_type)
2462  mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
2463 
2464  add_pid_to_program(prg, pid);
2465  if (prg) {
2466  prg->streams[i].idx = st->index;
2467  prg->streams[i].stream_identifier = stream_identifier;
2468  prg->nb_streams++;
2469  }
2470 
2471  av_program_add_stream_index(ts->stream, h->id, st->index);
2472 
2473  desc_list_len = get16(&p, p_end);
2474  if (desc_list_len < 0)
2475  goto out;
2476  desc_list_len &= 0xfff;
2477  desc_list_end = p + desc_list_len;
2478  if (desc_list_end > p_end)
2479  goto out;
2480  for (;;) {
2481  if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p,
2482  desc_list_end, mp4_descr,
2483  mp4_descr_count, pid, ts) < 0)
2484  break;
2485 
2486  if (pes && prog_reg_desc == AV_RL32("HDMV") &&
2487  stream_type == 0x83 && pes->sub_st) {
2489  pes->sub_st->index);
2490  pes->sub_st->codecpar->codec_tag = st->codecpar->codec_tag;
2491  }
2492  }
2493  p = desc_list_end;
2494  }
2495 
2496  if (!ts->pids[pcr_pid])
2497  mpegts_open_pcr_filter(ts, pcr_pid);
2498 
2499 out:
2500  for (i = 0; i < mp4_descr_count; i++)
2501  av_free(mp4_descr[i].dec_config_descr);
2502 }
2503 
2504 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2505 {
2506  MpegTSContext *ts = filter->u.section_filter.opaque;
2507  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2508  SectionHeader h1, *h = &h1;
2509  const uint8_t *p, *p_end;
2510  int sid, pmt_pid;
2511  int nb_prg = 0;
2512  AVProgram *program;
2513 
2514  av_log(ts->stream, AV_LOG_TRACE, "PAT:\n");
2515  hex_dump_debug(ts->stream, section, section_len);
2516 
2517  p_end = section + section_len - 4;
2518  p = section;
2519  if (parse_section_header(h, &p, p_end) < 0)
2520  return;
2521  if (h->tid != PAT_TID)
2522  return;
2523  if (ts->skip_changes)
2524  return;
2525 
2526  if (skip_identical(h, tssf))
2527  return;
2528  ts->stream->ts_id = h->id;
2529 
2530  for (;;) {
2531  sid = get16(&p, p_end);
2532  if (sid < 0)
2533  break;
2534  pmt_pid = get16(&p, p_end);
2535  if (pmt_pid < 0)
2536  break;
2537  pmt_pid &= 0x1fff;
2538 
2539  if (pmt_pid == ts->current_pid)
2540  break;
2541 
2542  av_log(ts->stream, AV_LOG_TRACE, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
2543 
2544  if (sid == 0x0000) {
2545  /* NIT info */
2546  } else {
2547  MpegTSFilter *fil = ts->pids[pmt_pid];
2548  struct Program *prg;
2549  program = av_new_program(ts->stream, sid);
2550  if (program) {
2551  program->program_num = sid;
2552  program->pmt_pid = pmt_pid;
2553  }
2554  if (fil)
2555  if ( fil->type != MPEGTS_SECTION
2556  || fil->pid != pmt_pid
2557  || fil->u.section_filter.section_cb != pmt_cb)
2558  mpegts_close_filter(ts, ts->pids[pmt_pid]);
2559 
2560  if (!ts->pids[pmt_pid])
2561  mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
2562  prg = add_program(ts, sid);
2563  if (prg) {
2564  unsigned prg_idx = prg - ts->prg;
2565  if (prg->nb_pids && prg->pids[0] != pmt_pid)
2566  clear_program(prg);
2567  add_pid_to_program(prg, pmt_pid);
2568  if (prg_idx > nb_prg)
2569  FFSWAP(struct Program, ts->prg[nb_prg], ts->prg[prg_idx]);
2570  if (prg_idx >= nb_prg)
2571  nb_prg++;
2572  } else
2573  nb_prg = 0;
2574  }
2575  }
2576  ts->nb_prg = nb_prg;
2577 
2578  if (sid < 0) {
2579  int i,j;
2580  for (j=0; j<ts->stream->nb_programs; j++) {
2581  for (i = 0; i < ts->nb_prg; i++)
2582  if (ts->prg[i].id == ts->stream->programs[j]->id)
2583  break;
2584  if (i==ts->nb_prg && !ts->skip_clear)
2585  clear_avprogram(ts, ts->stream->programs[j]->id);
2586  }
2587  }
2588 }
2589 
2590 static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2591 {
2592  MpegTSContext *ts = filter->u.section_filter.opaque;
2593  const uint8_t *p, *p_end;
2594  SectionHeader h1, *h = &h1;
2595 
2596  /*
2597  * Sometimes we receive EPG packets but SDT table do not have
2598  * eit_pres_following or eit_sched turned on, so we open EPG
2599  * stream directly here.
2600  */
2601  if (!ts->epg_stream) {
2603  if (!ts->epg_stream)
2604  return;
2605  ts->epg_stream->id = EIT_PID;
2608  }
2609 
2610  if (ts->epg_stream->discard == AVDISCARD_ALL)
2611  return;
2612 
2613  p_end = section + section_len - 4;
2614  p = section;
2615 
2616  if (parse_section_header(h, &p, p_end) < 0)
2617  return;
2618  if (h->tid < EIT_TID || h->tid > OEITS_END_TID)
2619  return;
2620 
2621  av_log(ts->stream, AV_LOG_TRACE, "EIT: tid received = %.02x\n", h->tid);
2622 
2623  /**
2624  * Service_id 0xFFFF is reserved, it indicates that the current EIT table
2625  * is scrambled.
2626  */
2627  if (h->id == 0xFFFF) {
2628  av_log(ts->stream, AV_LOG_TRACE, "Scrambled EIT table received.\n");
2629  return;
2630  }
2631 
2632  /**
2633  * In case we receive an EPG packet before mpegts context is fully
2634  * initialized.
2635  */
2636  if (!ts->pkt)
2637  return;
2638 
2639  new_data_packet(section, section_len, ts->pkt);
2640  ts->pkt->stream_index = ts->epg_stream->index;
2641  ts->stop_parse = 1;
2642 }
2643 
2644 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
2645 {
2646  MpegTSContext *ts = filter->u.section_filter.opaque;
2647  MpegTSSectionFilter *tssf = &filter->u.section_filter;
2648  SectionHeader h1, *h = &h1;
2649  const uint8_t *p, *p_end, *desc_list_end, *desc_end;
2650  int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
2651  char *name, *provider_name;
2652 
2653  av_log(ts->stream, AV_LOG_TRACE, "SDT:\n");
2654  hex_dump_debug(ts->stream, section, section_len);
2655 
2656  p_end = section + section_len - 4;
2657  p = section;
2658  if (parse_section_header(h, &p, p_end) < 0)
2659  return;
2660  if (h->tid != SDT_TID)
2661  return;
2662  if (ts->skip_changes)
2663  return;
2664  if (skip_identical(h, tssf))
2665  return;
2666 
2667  onid = get16(&p, p_end);
2668  if (onid < 0)
2669  return;
2670  val = get8(&p, p_end);
2671  if (val < 0)
2672  return;
2673  for (;;) {
2674  sid = get16(&p, p_end);
2675  if (sid < 0)
2676  break;
2677  val = get8(&p, p_end);
2678  if (val < 0)
2679  break;
2680  desc_list_len = get16(&p, p_end);
2681  if (desc_list_len < 0)
2682  break;
2683  desc_list_len &= 0xfff;
2684  desc_list_end = p + desc_list_len;
2685  if (desc_list_end > p_end)
2686  break;
2687  for (;;) {
2688  desc_tag = get8(&p, desc_list_end);
2689  if (desc_tag < 0)
2690  break;
2691  desc_len = get8(&p, desc_list_end);
2692  desc_end = p + desc_len;
2693  if (desc_len < 0 || desc_end > desc_list_end)
2694  break;
2695 
2696  av_log(ts->stream, AV_LOG_TRACE, "tag: 0x%02x len=%d\n",
2697  desc_tag, desc_len);
2698 
2699  switch (desc_tag) {
2700  case 0x48:
2701  service_type = get8(&p, p_end);
2702  if (service_type < 0)
2703  break;
2704  provider_name = getstr8(&p, p_end);
2705  if (!provider_name)
2706  break;
2707  name = getstr8(&p, p_end);
2708  if (name) {
2709  AVProgram *program = av_new_program(ts->stream, sid);
2710  if (program) {
2711  av_dict_set(&program->metadata, "service_name", name, 0);
2712  av_dict_set(&program->metadata, "service_provider",
2713  provider_name, 0);
2714  }
2715  }
2716  av_free(name);
2717  av_free(provider_name);
2718  break;
2719  default:
2720  break;
2721  }
2722  p = desc_end;
2723  }
2724  p = desc_list_end;
2725  }
2726 }
2727 
2728 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
2729  const uint8_t *packet);
2730 
2731 /* handle one TS packet */
2732 static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
2733 {
2734  MpegTSFilter *tss;
2735  int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
2736  has_adaptation, has_payload;
2737  const uint8_t *p, *p_end;
2738 
2739  pid = AV_RB16(packet + 1) & 0x1fff;
2740  is_start = packet[1] & 0x40;
2741  tss = ts->pids[pid];
2742  if (ts->auto_guess && !tss && is_start) {
2743  add_pes_stream(ts, pid, -1);
2744  tss = ts->pids[pid];
2745  }
2746  if (!tss)
2747  return 0;
2748  if (is_start)
2749  tss->discard = discard_pid(ts, pid);
2750  if (tss->discard)
2751  return 0;
2752  ts->current_pid = pid;
2753 
2754  afc = (packet[3] >> 4) & 3;
2755  if (afc == 0) /* reserved value */
2756  return 0;
2757  has_adaptation = afc & 2;
2758  has_payload = afc & 1;
2759  is_discontinuity = has_adaptation &&
2760  packet[4] != 0 && /* with length > 0 */
2761  (packet[5] & 0x80); /* and discontinuity indicated */
2762 
2763  /* continuity check (currently not used) */
2764  cc = (packet[3] & 0xf);
2765  expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
2766  cc_ok = pid == 0x1FFF || // null packet PID
2767  is_discontinuity ||
2768  tss->last_cc < 0 ||
2769  expected_cc == cc;
2770 
2771  tss->last_cc = cc;
2772  if (!cc_ok) {
2773  av_log(ts->stream, AV_LOG_DEBUG,
2774  "Continuity check failed for pid %d expected %d got %d\n",
2775  pid, expected_cc, cc);
2776  if (tss->type == MPEGTS_PES) {
2777  PESContext *pc = tss->u.pes_filter.opaque;
2778  pc->flags |= AV_PKT_FLAG_CORRUPT;
2779  }
2780  }
2781 
2782  if (packet[1] & 0x80) {
2783  av_log(ts->stream, AV_LOG_DEBUG, "Packet had TEI flag set; marking as corrupt\n");
2784  if (tss->type == MPEGTS_PES) {
2785  PESContext *pc = tss->u.pes_filter.opaque;
2786  pc->flags |= AV_PKT_FLAG_CORRUPT;
2787  }
2788  }
2789 
2790  p = packet + 4;
2791  if (has_adaptation) {
2792  int64_t pcr_h;
2793  int pcr_l;
2794  if (parse_pcr(&pcr_h, &pcr_l, packet) == 0)
2795  tss->last_pcr = pcr_h * 300 + pcr_l;
2796  /* skip adaptation field */
2797  p += p[0] + 1;
2798  }
2799  /* if past the end of packet, ignore */
2800  p_end = packet + TS_PACKET_SIZE;
2801  if (p >= p_end || !has_payload)
2802  return 0;
2803 
2804  if (pos >= 0) {
2806  ts->pos47_full = pos - TS_PACKET_SIZE;
2807  }
2808 
2809  if (tss->type == MPEGTS_SECTION) {
2810  if (is_start) {
2811  /* pointer field present */
2812  len = *p++;
2813  if (len > p_end - p)
2814  return 0;
2815  if (len && cc_ok) {
2816  /* write remaining section bytes */
2817  write_section_data(ts, tss,
2818  p, len, 0);
2819  /* check whether filter has been closed */
2820  if (!ts->pids[pid])
2821  return 0;
2822  }
2823  p += len;
2824  if (p < p_end) {
2825  write_section_data(ts, tss,
2826  p, p_end - p, 1);
2827  }
2828  } else {
2829  if (cc_ok) {
2830  write_section_data(ts, tss,
2831  p, p_end - p, 0);
2832  }
2833  }
2834 
2835  // stop find_stream_info from waiting for more streams
2836  // when all programs have received a PMT
2837  if (ts->stream->ctx_flags & AVFMTCTX_NOHEADER && ts->scan_all_pmts <= 0) {
2838  int i;
2839  for (i = 0; i < ts->nb_prg; i++) {
2840  if (!ts->prg[i].pmt_found)
2841  break;
2842  }
2843  if (i == ts->nb_prg && ts->nb_prg > 0) {
2844  int types = 0;
2845  for (i = 0; i < ts->stream->nb_streams; i++) {
2846  AVStream *st = ts->stream->streams[i];
2847  if (st->codecpar->codec_type >= 0)
2848  types |= 1<<st->codecpar->codec_type;
2849  }
2850  if ((types & (1<<AVMEDIA_TYPE_AUDIO) && types & (1<<AVMEDIA_TYPE_VIDEO)) || pos > 100000) {
2851  av_log(ts->stream, AV_LOG_DEBUG, "All programs have pmt, headers found\n");
2853  }
2854  }
2855  }
2856 
2857  } else {
2858  int ret;
2859  // Note: The position here points actually behind the current packet.
2860  if (tss->type == MPEGTS_PES) {
2861  if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
2862  pos - ts->raw_packet_size)) < 0)
2863  return ret;
2864  }
2865  }
2866 
2867  return 0;
2868 }
2869 
2870 static int mpegts_resync(AVFormatContext *s, int seekback, const uint8_t *current_packet)
2871 {
2872  MpegTSContext *ts = s->priv_data;
2873  AVIOContext *pb = s->pb;
2874  int c, i;
2875  uint64_t pos = avio_tell(pb);
2876  int64_t back = FFMIN(seekback, pos);
2877 
2878  //Special case for files like 01c56b0dc1.ts
2879  if (current_packet[0] == 0x80 && current_packet[12] == 0x47 && pos >= TS_PACKET_SIZE) {
2880  avio_seek(pb, 12 - TS_PACKET_SIZE, SEEK_CUR);
2881  return 0;
2882  }
2883 
2884  avio_seek(pb, -back, SEEK_CUR);
2885 
2886  for (i = 0; i < ts->resync_size; i++) {
2887  c = avio_r8(pb);
2888  if (avio_feof(pb))
2889  return AVERROR_EOF;
2890  if (c == 0x47) {
2891  int new_packet_size, ret;
2892  avio_seek(pb, -1, SEEK_CUR);
2893  pos = avio_tell(pb);
2895  if (ret < 0)
2896  return ret;
2897  new_packet_size = get_packet_size(s);
2898  if (new_packet_size > 0 && new_packet_size != ts->raw_packet_size) {
2899  av_log(ts->stream, AV_LOG_WARNING, "changing packet size to %d\n", new_packet_size);
2900  ts->raw_packet_size = new_packet_size;
2901  }
2902  avio_seek(pb, pos, SEEK_SET);
2903  return 0;
2904  }
2905  }
2907  "max resync size reached, could not find sync byte\n");
2908  /* no sync found */
2909  return AVERROR_INVALIDDATA;
2910 }
2911 
2912 /* return AVERROR_something if error or EOF. Return 0 if OK. */
2913 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size,
2914  const uint8_t **data)
2915 {
2916  AVIOContext *pb = s->pb;
2917  int len;
2918 
2919  for (;;) {
2921  if (len != TS_PACKET_SIZE)
2922  return len < 0 ? len : AVERROR_EOF;
2923  /* check packet sync byte */
2924  if ((*data)[0] != 0x47) {
2925  /* find a new packet start */
2926 
2927  if (mpegts_resync(s, raw_packet_size, *data) < 0)
2928  return AVERROR(EAGAIN);
2929  else
2930  continue;
2931  } else {
2932  break;
2933  }
2934  }
2935  return 0;
2936 }
2937 
2938 static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
2939 {
2940  AVIOContext *pb = s->pb;
2941  int skip = raw_packet_size - TS_PACKET_SIZE;
2942  if (skip > 0)
2943  avio_skip(pb, skip);
2944 }
2945 
2946 static int handle_packets(MpegTSContext *ts, int64_t nb_packets)
2947 {
2948  AVFormatContext *s = ts->stream;
2950  const uint8_t *data;
2951  int64_t packet_num;
2952  int ret = 0;
2953 
2954  if (avio_tell(s->pb) != ts->last_pos) {
2955  int i;
2956  av_log(ts->stream, AV_LOG_TRACE, "Skipping after seek\n");
2957  /* seek detected, flush pes buffer */
2958  for (i = 0; i < NB_PID_MAX; i++) {
2959  if (ts->pids[i]) {
2960  if (ts->pids[i]->type == MPEGTS_PES) {
2961  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
2962  av_buffer_unref(&pes->buffer);
2963  pes->data_index = 0;
2964  pes->state = MPEGTS_SKIP; /* skip until pes header */
2965  } else if (ts->pids[i]->type == MPEGTS_SECTION) {
2966  ts->pids[i]->u.section_filter.last_ver = -1;
2967  }
2968  ts->pids[i]->last_cc = -1;
2969  ts->pids[i]->last_pcr = -1;
2970  }
2971  }
2972  }
2973 
2974  ts->stop_parse = 0;
2975  packet_num = 0;
2976  memset(packet + TS_PACKET_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
2977  for (;;) {
2978  packet_num++;
2979  if (nb_packets != 0 && packet_num >= nb_packets ||
2980  ts->stop_parse > 1) {
2981  ret = AVERROR(EAGAIN);
2982  break;
2983  }
2984  if (ts->stop_parse > 0)
2985  break;
2986 
2987  ret = read_packet(s, packet, ts->raw_packet_size, &data);
2988  if (ret != 0)
2989  break;
2990  ret = handle_packet(ts, data, avio_tell(s->pb));
2992  if (ret != 0)
2993  break;
2994  }
2995  ts->last_pos = avio_tell(s->pb);
2996  return ret;
2997 }
2998 
2999 static int mpegts_probe(const AVProbeData *p)
3000 {
3001  const int size = p->buf_size;
3002  int maxscore = 0;
3003  int sumscore = 0;
3004  int i;
3005  int check_count = size / TS_FEC_PACKET_SIZE;
3006 #define CHECK_COUNT 10
3007 #define CHECK_BLOCK 100
3008 
3009  if (!check_count)
3010  return 0;
3011 
3012  for (i = 0; i<check_count; i+=CHECK_BLOCK) {
3013  int left = FFMIN(check_count - i, CHECK_BLOCK);
3014  int score = analyze(p->buf + TS_PACKET_SIZE *i, TS_PACKET_SIZE *left, TS_PACKET_SIZE , 1);
3015  int dvhs_score = analyze(p->buf + TS_DVHS_PACKET_SIZE*i, TS_DVHS_PACKET_SIZE*left, TS_DVHS_PACKET_SIZE, 1);
3016  int fec_score = analyze(p->buf + TS_FEC_PACKET_SIZE *i, TS_FEC_PACKET_SIZE *left, TS_FEC_PACKET_SIZE , 1);
3017  score = FFMAX3(score, dvhs_score, fec_score);
3018  sumscore += score;
3019  maxscore = FFMAX(maxscore, score);
3020  }
3021 
3022  sumscore = sumscore * CHECK_COUNT / check_count;
3023  maxscore = maxscore * CHECK_COUNT / CHECK_BLOCK;
3024 
3025  ff_dlog(0, "TS score: %d %d\n", sumscore, maxscore);
3026 
3027  if (check_count > CHECK_COUNT && sumscore > 6) {
3028  return AVPROBE_SCORE_MAX + sumscore - CHECK_COUNT;
3029  } else if (check_count >= CHECK_COUNT && sumscore > 6) {
3030  return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3031  } else if (check_count >= CHECK_COUNT && maxscore > 6) {
3032  return AVPROBE_SCORE_MAX/2 + sumscore - CHECK_COUNT;
3033  } else if (sumscore > 6) {
3034  return 2;
3035  } else {
3036  return 0;
3037  }
3038 }
3039 
3040 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
3041  * (-1) if not available */
3042 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
3043 {
3044  int afc, len, flags;
3045  const uint8_t *p;
3046  unsigned int v;
3047 
3048  afc = (packet[3] >> 4) & 3;
3049  if (afc <= 1)
3050  return AVERROR_INVALIDDATA;
3051  p = packet + 4;
3052  len = p[0];
3053  p++;
3054  if (len == 0)
3055  return AVERROR_INVALIDDATA;
3056  flags = *p++;
3057  len--;
3058  if (!(flags & 0x10))
3059  return AVERROR_INVALIDDATA;
3060  if (len < 6)
3061  return AVERROR_INVALIDDATA;
3062  v = AV_RB32(p);
3063  *ppcr_high = ((int64_t) v << 1) | (p[4] >> 7);
3064  *ppcr_low = ((p[4] & 1) << 8) | p[5];
3065  return 0;
3066 }
3067 
3069 
3070  /* NOTE: We attempt to seek on non-seekable files as well, as the
3071  * probe buffer usually is big enough. Only warn if the seek failed
3072  * on files where the seek should work. */
3073  if (avio_seek(pb, pos, SEEK_SET) < 0)
3074  av_log(s, (pb->seekable & AVIO_SEEKABLE_NORMAL) ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
3075 }
3076 
3078 {
3079  MpegTSContext *ts = s->priv_data;
3080  AVIOContext *pb = s->pb;
3081  int64_t pos, probesize = s->probesize;
3082  int64_t seekback = FFMAX(s->probesize, (int64_t)ts->resync_size + PROBE_PACKET_MAX_BUF);
3083 
3084  s->internal->prefer_codec_framerate = 1;
3085 
3086  if (ffio_ensure_seekback(pb, seekback) < 0)
3087  av_log(s, AV_LOG_WARNING, "Failed to allocate buffers for seekback\n");
3088 
3089  pos = avio_tell(pb);
3091  if (ts->raw_packet_size <= 0) {
3092  av_log(s, AV_LOG_WARNING, "Could not detect TS packet size, defaulting to non-FEC/DVHS\n");
3094  }
3095  ts->stream = s;
3096  ts->auto_guess = 0;
3097 
3098  if (s->iformat == &ff_mpegts_demuxer) {
3099  /* normal demux */
3100 
3101  /* first do a scan to get all the services */
3102  seek_back(s, pb, pos);
3103 
3107 
3108  handle_packets(ts, probesize / ts->raw_packet_size);
3109  /* if could not find service, enable auto_guess */
3110 
3111  ts->auto_guess = 1;
3112 
3113  av_log(ts->stream, AV_LOG_TRACE, "tuning done\n");
3114 
3115  s->ctx_flags |= AVFMTCTX_NOHEADER;
3116  } else {
3117  AVStream *st;
3118  int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
3119  int64_t pcrs[2], pcr_h;
3120  uint8_t packet[TS_PACKET_SIZE];
3121  const uint8_t *data;
3122 
3123  /* only read packets */
3124 
3125  st = avformat_new_stream(s, NULL);
3126  if (!st)
3127  return AVERROR(ENOMEM);
3128  avpriv_set_pts_info(st, 60, 1, 27000000);
3131 
3132  /* we iterate until we find two PCRs to estimate the bitrate */
3133  pcr_pid = -1;
3134  nb_pcrs = 0;
3135  nb_packets = 0;
3136  for (;;) {
3137  ret = read_packet(s, packet, ts->raw_packet_size, &data);
3138  if (ret < 0)
3139  return ret;
3140  pid = AV_RB16(data + 1) & 0x1fff;
3141  if ((pcr_pid == -1 || pcr_pid == pid) &&
3142  parse_pcr(&pcr_h, &pcr_l, data) == 0) {
3144  pcr_pid = pid;
3145  pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
3146  nb_pcrs++;
3147  if (nb_pcrs >= 2) {
3148  if (pcrs[1] - pcrs[0] > 0) {
3149  /* the difference needs to be positive to make sense for bitrate computation */
3150  break;
3151  } else {
3152  av_log(ts->stream, AV_LOG_WARNING, "invalid pcr pair %"PRId64" >= %"PRId64"\n", pcrs[0], pcrs[1]);
3153  pcrs[0] = pcrs[1];
3154  nb_pcrs--;
3155  }
3156  }
3157  } else {
3159  }
3160  nb_packets++;
3161  }
3162 
3163  /* NOTE1: the bitrate is computed without the FEC */
3164  /* NOTE2: it is only the bitrate of the start of the stream */
3165  ts->pcr_incr = pcrs[1] - pcrs[0];
3166  ts->cur_pcr = pcrs[0] - ts->pcr_incr * (nb_packets - 1);
3167  s->bit_rate = TS_PACKET_SIZE * 8 * 27000000LL / ts->pcr_incr;
3168  st->codecpar->bit_rate = s->bit_rate;
3169  st->start_time = ts->cur_pcr;
3170  av_log(ts->stream, AV_LOG_TRACE, "start=%0.3f pcr=%0.3f incr=%"PRId64"\n",
3171  st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
3172  }
3173 
3174  seek_back(s, pb, pos);
3175  return 0;
3176 }
3177 
3178 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
3179 
3181 {
3182  MpegTSContext *ts = s->priv_data;
3183  int ret, i;
3184  int64_t pcr_h, next_pcr_h, pos;
3185  int pcr_l, next_pcr_l;
3186  uint8_t pcr_buf[12];
3187  const uint8_t *data;
3188 
3189  if ((ret = av_new_packet(pkt, TS_PACKET_SIZE)) < 0)
3190  return ret;
3191  ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
3192  pkt->pos = avio_tell(s->pb);
3193  if (ret < 0) {
3194  return ret;
3195  }
3196  if (data != pkt->data)
3197  memcpy(pkt->data, data, TS_PACKET_SIZE);
3199  if (ts->mpeg2ts_compute_pcr) {
3200  /* compute exact PCR for each packet */
3201  if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
3202  /* we read the next PCR (XXX: optimize it by using a bigger buffer */
3203  pos = avio_tell(s->pb);
3204  for (i = 0; i < MAX_PACKET_READAHEAD; i++) {
3205  avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
3206  avio_read(s->pb, pcr_buf, 12);
3207  if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
3208  /* XXX: not precise enough */
3209  ts->pcr_incr =
3210  ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
3211  (i + 1);
3212  break;
3213  }
3214  }
3215  avio_seek(s->pb, pos, SEEK_SET);
3216  /* no next PCR found: we use previous increment */
3217  ts->cur_pcr = pcr_h * 300 + pcr_l;
3218  }
3219  pkt->pts = ts->cur_pcr;
3220  pkt->duration = ts->pcr_incr;
3221  ts->cur_pcr += ts->pcr_incr;
3222  }
3223  pkt->stream_index = 0;
3224  return 0;
3225 }
3226 
3228 {
3229  MpegTSContext *ts = s->priv_data;
3230  int ret, i;
3231 
3232  pkt->size = -1;
3233  ts->pkt = pkt;
3234  ret = handle_packets(ts, 0);
3235  if (ret < 0) {
3236  av_packet_unref(ts->pkt);
3237  /* flush pes data left */
3238  for (i = 0; i < NB_PID_MAX; i++)
3239  if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
3240  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
3241  if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
3242  ret = new_pes_packet(pes, pkt);
3243  if (ret < 0)
3244  return ret;
3245  pes->state = MPEGTS_SKIP;
3246  ret = 0;
3247  break;
3248  }
3249  }
3250  }
3251 
3252  if (!ret && pkt->size < 0)
3253  ret = AVERROR_INVALIDDATA;
3254  return ret;
3255 }
3256 
3257 static void mpegts_free(MpegTSContext *ts)
3258 {
3259  int i;
3260 
3261  clear_programs(ts);
3262 
3263  for (i = 0; i < FF_ARRAY_ELEMS(ts->pools); i++)
3264  av_buffer_pool_uninit(&ts->pools[i]);
3265 
3266  for (i = 0; i < NB_PID_MAX; i++)
3267  if (ts->pids[i])
3268  mpegts_close_filter(ts, ts->pids[i]);
3269 }
3270 
3272 {
3273  MpegTSContext *ts = s->priv_data;
3274  mpegts_free(ts);
3275  return 0;
3276 }
3277 
3279  int64_t *ppos, int64_t pos_limit)
3280 {
3281  MpegTSContext *ts = s->priv_data;
3282  int64_t pos, timestamp;
3283  uint8_t buf[TS_PACKET_SIZE];
3284  int pcr_l, pcr_pid =
3285  ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
3286  int pos47 = ts->pos47_full % ts->raw_packet_size;
3287  pos =
3288  ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) *
3289  ts->raw_packet_size + pos47;
3290  while(pos < pos_limit) {
3291  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3292  return AV_NOPTS_VALUE;
3293  if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
3294  return AV_NOPTS_VALUE;
3295  if (buf[0] != 0x47) {
3296  if (mpegts_resync(s, TS_PACKET_SIZE, buf) < 0)
3297  return AV_NOPTS_VALUE;
3298  pos = avio_tell(s->pb);
3299  continue;
3300  }
3301  if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
3302  parse_pcr(&timestamp, &pcr_l, buf) == 0) {
3303  *ppos = pos;
3304  return timestamp;
3305  }
3306  pos += ts->raw_packet_size;
3307  }
3308 
3309  return AV_NOPTS_VALUE;
3310 }
3311 
3312 static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index,
3313  int64_t *ppos, int64_t pos_limit)
3314 {
3315  MpegTSContext *ts = s->priv_data;
3316  AVPacket *pkt;
3317  int64_t pos;
3318  int pos47 = ts->pos47_full % ts->raw_packet_size;
3319  pos = ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) * ts->raw_packet_size + pos47;
3321  if (avio_seek(s->pb, pos, SEEK_SET) < 0)
3322  return AV_NOPTS_VALUE;
3323  pkt = av_packet_alloc();
3324  if (!pkt)
3325  return AV_NOPTS_VALUE;
3326  while(pos < pos_limit) {
3327  int ret = av_read_frame(s, pkt);
3328  if (ret < 0) {
3329  av_packet_free(&pkt);
3330  return AV_NOPTS_VALUE;
3331  }
3332  if (pkt->dts != AV_NOPTS_VALUE && pkt->pos >= 0) {
3334  av_add_index_entry(s->streams[pkt->stream_index], pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
3335  if (pkt->stream_index == stream_index && pkt->pos >= *ppos) {
3336  int64_t dts = pkt->dts;
3337  *ppos = pkt->pos;
3338  av_packet_free(&pkt);
3339  return dts;
3340  }
3341  }
3342  pos = pkt->pos;
3344  }
3345 
3346  av_packet_free(&pkt);
3347  return AV_NOPTS_VALUE;
3348 }
3349 
3350 /**************************************************************/
3351 /* parsing functions - called from other demuxers such as RTP */
3352 
3354 {
3355  MpegTSContext *ts;
3356 
3357  ts = av_mallocz(sizeof(MpegTSContext));
3358  if (!ts)
3359  return NULL;
3360  /* no stream case, currently used by RTP */
3362  ts->stream = s;
3363  ts->auto_guess = 1;
3364 
3368 
3369  return ts;
3370 }
3371 
3372 /* return the consumed length if a packet was output, or -1 if no
3373  * packet is output */
3375  const uint8_t *buf, int len)
3376 {
3377  int len1;
3378 
3379  len1 = len;
3380  ts->pkt = pkt;
3381  for (;;) {
3382  ts->stop_parse = 0;
3383  if (len < TS_PACKET_SIZE)
3384  return AVERROR_INVALIDDATA;
3385  if (buf[0] != 0x47) {
3386  buf++;
3387  len--;
3388  } else {
3389  handle_packet(ts, buf, len1 - len + TS_PACKET_SIZE);
3390  buf += TS_PACKET_SIZE;
3391  len -= TS_PACKET_SIZE;
3392  if (ts->stop_parse == 1)
3393  break;
3394  }
3395  }
3396  return len1 - len;
3397 }
3398 
3400 {
3401  mpegts_free(ts);
3402  av_free(ts);
3403 }
3404 
3406  .name = "mpegts",
3407  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
3408  .priv_data_size = sizeof(MpegTSContext),
3413  .read_timestamp = mpegts_get_dts,
3415  .priv_class = &mpegts_class,
3416 };
3417 
3419  .name = "mpegtsraw",
3420  .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
3421  .priv_data_size = sizeof(MpegTSContext),
3425  .read_timestamp = mpegts_get_dts,
3427  .priv_class = &mpegtsraw_class,
3428 };
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:253
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:766
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:364
int avio_read_partial(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:704
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:337
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:781
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:624
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:692
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:998
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:550
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:848
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:5522
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:4607
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
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:392
@ 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:309
#define MP4ODescrTag
Definition: isom.h:307
#define MP4SLDescrTag
Definition: isom.h:312
#define MP4DecConfigDescrTag
Definition: isom.h:310
#define MP4IODescrTag
Definition: isom.h:308
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:5029
#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:4945
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:3257
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:1481
#define MAX_STREAMS_PER_PROGRAM
Definition: mpegts.c:115
static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos)
Definition: mpegts.c:3068
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:1782
void avpriv_mpegts_parse_close(MpegTSContext *ts)
Definition: mpegts.c:3399
static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1553
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:1413
static const AVOption options[]
Definition: mpegts.c:186
#define CHECK_COUNT
AVInputFormat ff_mpegts_demuxer
Definition: mpegts.c:3405
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:1793
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1539
static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int max_descr_count)
Definition: mpegts.c:1449
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:3042
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2286
static const uint8_t opus_coupled_stream_cnt[9]
Definition: mpegts.c:1774
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:3278
static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegts.c:3227
static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1518
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:2504
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, int target_tag)
Definition: mpegts.c:1595
#define MAX_LEVEL
Definition: mpegts.c:1437
static int parse_stream_identifier_desc(const uint8_t *p, const uint8_t *p_end)
Definition: mpegts.c:2244
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:2938
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:1648
void SetServiceCallback(void *opaque, int ret)
Definition: mpegts.c:82
static int mpegts_read_header(AVFormatContext *s)
Definition: mpegts.c:3077
#define MAX_PES_PAYLOAD
Definition: mpegts.c:50
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1682
static int64_t mpegts_get_dts(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: mpegts.c:3312
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:2280
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:2946
static void eit_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2590
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
Definition: mpegts.c:2913
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:3374
static int mpegts_read_close(AVFormatContext *s)
Definition: mpegts.c:3271
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:1778
static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
Definition: mpegts.c:1471
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:3180
static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:2644
#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:3418
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:1666
static int handle_packet(MpegTSContext *ts, const uint8_t *packet, int64_t pos)
Definition: mpegts.c:2732
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:2999
#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:2870
static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1504
MpegTSContext * avpriv_mpegts_parse_open(AVFormatContext *s)
Definition: mpegts.c:3353
#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:2217
static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1492
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:3178
static void scte_data_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1745
#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 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 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:1440
Mp4Descr * descr
Definition: mpegts.c:1441
int predefined_SLConfigDescriptor_seen
Definition: mpegts.c:1446
Mp4Descr * active_descr
Definition: mpegts.c:1442
AVFormatContext * s
Definition: mpegts.c:1439
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
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
union MpegTSFilter::@270 u
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]