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