Libav
mpegts.c
Go to the documentation of this file.
1 /*
2  * MPEG2 transport stream (aka DVB) demuxer
3  * Copyright (c) 2002-2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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/intreadwrite.h"
25 #include "libavutil/log.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 #include "libavcodec/bytestream.h"
30 #include "libavcodec/get_bits.h"
31 #include "libavcodec/mathops.h"
32 #include "avformat.h"
33 #include "mpegts.h"
34 #include "internal.h"
35 #include "avio_internal.h"
36 #include "seek.h"
37 #include "mpeg.h"
38 #include "isom.h"
39 
40 /* maximum size in which we look for synchronisation if
41  synchronisation is lost */
42 #define MAX_RESYNC_SIZE 65536
43 
44 #define MAX_PES_PAYLOAD 200*1024
45 
46 #define MAX_MP4_DESCR_COUNT 16
47 
51 };
52 
53 typedef struct MpegTSFilter MpegTSFilter;
54 
55 typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
56 
57 typedef struct MpegTSPESFilter {
59  void *opaque;
61 
62 typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
63 
64 typedef void SetServiceCallback(void *opaque, int ret);
65 
66 typedef struct MpegTSSectionFilter {
70  unsigned int check_crc:1;
71  unsigned int end_of_section_reached:1;
73  void *opaque;
75 
76 struct MpegTSFilter {
77  int pid;
78  int es_id;
79  int last_cc; /* last cc code (-1 if first packet) */
81  union {
84  } u;
85 };
86 
87 #define MAX_PIDS_PER_PROGRAM 64
88 struct Program {
89  unsigned int id; //program id/service id
90  unsigned int nb_pids;
91  unsigned int pids[MAX_PIDS_PER_PROGRAM];
92 };
93 
94 struct MpegTSContext {
95  const AVClass *class;
96  /* user data */
100 
101  int pos47;
103  int64_t pos;
104 
107 
110 
111  int64_t cur_pcr;
112  int pcr_incr;
114  /* data needed to handle file based ts */
120  int64_t last_pos;
121 
122  /******************************************/
123  /* private mpegts data */
124  /* scan context */
126  unsigned int nb_prg;
127  struct Program *prg;
128 
129 
132 };
133 
134 static const AVOption options[] = {
135  {"compute_pcr", "Compute exact PCR for each transport stream packet.", offsetof(MpegTSContext, mpeg2ts_compute_pcr), AV_OPT_TYPE_INT,
136  {.i64 = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
137  { NULL },
138 };
139 
140 static const AVClass mpegtsraw_class = {
141  .class_name = "mpegtsraw demuxer",
142  .item_name = av_default_item_name,
143  .option = options,
144  .version = LIBAVUTIL_VERSION_INT,
145 };
146 
147 /* TS stream handling */
148 
155 };
156 
157 /* enough for PES header + length */
158 #define PES_START_SIZE 6
159 #define PES_HEADER_SIZE 9
160 #define MAX_PES_HEADER_SIZE (9 + 255)
161 
162 typedef struct PESContext {
163  int pid;
164  int pcr_pid;
171  /* used to get the format */
173  int flags;
177  int64_t pts, dts;
178  int64_t ts_packet_pos;
182 } PESContext;
183 
185 
186 static void clear_program(MpegTSContext *ts, unsigned int programid)
187 {
188  int i;
189 
190  for(i=0; i<ts->nb_prg; i++)
191  if(ts->prg[i].id == programid)
192  ts->prg[i].nb_pids = 0;
193 }
194 
196 {
197  av_freep(&ts->prg);
198  ts->nb_prg=0;
199 }
200 
201 static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
202 {
203  struct Program *p;
204  if (av_reallocp_array(&ts->prg, ts->nb_prg + 1, sizeof(*ts->prg)) < 0) {
205  ts->nb_prg = 0;
206  return;
207  }
208  p = &ts->prg[ts->nb_prg];
209  p->id = programid;
210  p->nb_pids = 0;
211  ts->nb_prg++;
212 }
213 
214 static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
215 {
216  int i;
217  struct Program *p = NULL;
218  for(i=0; i<ts->nb_prg; i++) {
219  if(ts->prg[i].id == programid) {
220  p = &ts->prg[i];
221  break;
222  }
223  }
224  if(!p)
225  return;
226 
227  if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
228  return;
229  p->pids[p->nb_pids++] = pid;
230 }
231 
240 static int discard_pid(MpegTSContext *ts, unsigned int pid)
241 {
242  int i, j, k;
243  int used = 0, discarded = 0;
244  struct Program *p;
245 
246  /* If none of the programs have .discard=AVDISCARD_ALL then there's
247  * no way we have to discard this packet
248  */
249  for (k = 0; k < ts->stream->nb_programs; k++) {
250  if (ts->stream->programs[k]->discard == AVDISCARD_ALL)
251  break;
252  }
253  if (k == ts->stream->nb_programs)
254  return 0;
255 
256  for(i=0; i<ts->nb_prg; i++) {
257  p = &ts->prg[i];
258  for(j=0; j<p->nb_pids; j++) {
259  if(p->pids[j] != pid)
260  continue;
261  //is program with id p->id set to be discarded?
262  for(k=0; k<ts->stream->nb_programs; k++) {
263  if(ts->stream->programs[k]->id == p->id) {
264  if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
265  discarded++;
266  else
267  used++;
268  }
269  }
270  }
271  }
272 
273  return !used && discarded;
274 }
275 
281  const uint8_t *buf, int buf_size, int is_start)
282 {
283  MpegTSSectionFilter *tss = &tss1->u.section_filter;
284  int len;
285 
286  if (is_start) {
287  memcpy(tss->section_buf, buf, buf_size);
288  tss->section_index = buf_size;
289  tss->section_h_size = -1;
290  tss->end_of_section_reached = 0;
291  } else {
292  if (tss->end_of_section_reached)
293  return;
294  len = 4096 - tss->section_index;
295  if (buf_size < len)
296  len = buf_size;
297  memcpy(tss->section_buf + tss->section_index, buf, len);
298  tss->section_index += len;
299  }
300 
301  /* compute section length if possible */
302  if (tss->section_h_size == -1 && tss->section_index >= 3) {
303  len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
304  if (len > 4096)
305  return;
306  tss->section_h_size = len;
307  }
308 
309  if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
310  tss->end_of_section_reached = 1;
311  if (!tss->check_crc ||
313  tss->section_buf, tss->section_h_size) == 0)
314  tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
315  }
316 }
317 
319  SectionCallback *section_cb, void *opaque,
320  int check_crc)
321 
322 {
324  MpegTSSectionFilter *sec;
325 
326  av_dlog(ts->stream, "Filter: pid=0x%x\n", pid);
327 
328  if (pid >= NB_PID_MAX || ts->pids[pid])
329  return NULL;
330  filter = av_mallocz(sizeof(MpegTSFilter));
331  if (!filter)
332  return NULL;
333  ts->pids[pid] = filter;
334  filter->type = MPEGTS_SECTION;
335  filter->pid = pid;
336  filter->es_id = -1;
337  filter->last_cc = -1;
338  sec = &filter->u.section_filter;
339  sec->section_cb = section_cb;
340  sec->opaque = opaque;
342  sec->check_crc = check_crc;
343  if (!sec->section_buf) {
344  av_free(filter);
345  return NULL;
346  }
347  return filter;
348 }
349 
350 static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
351  PESCallback *pes_cb,
352  void *opaque)
353 {
355  MpegTSPESFilter *pes;
356 
357  if (pid >= NB_PID_MAX || ts->pids[pid])
358  return NULL;
359  filter = av_mallocz(sizeof(MpegTSFilter));
360  if (!filter)
361  return NULL;
362  ts->pids[pid] = filter;
363  filter->type = MPEGTS_PES;
364  filter->pid = pid;
365  filter->es_id = -1;
366  filter->last_cc = -1;
367  pes = &filter->u.pes_filter;
368  pes->pes_cb = pes_cb;
369  pes->opaque = opaque;
370  return filter;
371 }
372 
374 {
375  int pid;
376 
377  pid = filter->pid;
378  if (filter->type == MPEGTS_SECTION)
380  else if (filter->type == MPEGTS_PES) {
381  PESContext *pes = filter->u.pes_filter.opaque;
382  av_buffer_unref(&pes->buffer);
383  /* referenced private data will be freed later in
384  * avformat_close_input */
385  if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
386  av_freep(&filter->u.pes_filter.opaque);
387  }
388  }
389 
390  av_free(filter);
391  ts->pids[pid] = NULL;
392 }
393 
394 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
395  int stat[TS_MAX_PACKET_SIZE];
396  int i;
397  int x=0;
398  int best_score=0;
399 
400  memset(stat, 0, packet_size*sizeof(int));
401 
402  for(x=i=0; i<size-3; i++){
403  if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
404  stat[x]++;
405  if(stat[x] > best_score){
406  best_score= stat[x];
407  if(index) *index= x;
408  }
409  }
410 
411  x++;
412  if(x == packet_size) x= 0;
413  }
414 
415  return best_score;
416 }
417 
418 /* autodetect fec presence. Must have at least 1024 bytes */
419 static int get_packet_size(const uint8_t *buf, int size)
420 {
421  int score, fec_score, dvhs_score;
422 
423  if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
424  return -1;
425 
426  score = analyze(buf, size, TS_PACKET_SIZE, NULL);
427  dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
428  fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
429  av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
430  score, dvhs_score, fec_score);
431 
432  if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
433  else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
434  else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
435  else return -1;
436 }
437 
438 typedef struct SectionHeader {
440  uint16_t id;
444 } SectionHeader;
445 
446 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
447 {
448  const uint8_t *p;
449  int c;
450 
451  p = *pp;
452  if (p >= p_end)
453  return -1;
454  c = *p++;
455  *pp = p;
456  return c;
457 }
458 
459 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
460 {
461  const uint8_t *p;
462  int c;
463 
464  p = *pp;
465  if ((p + 1) >= p_end)
466  return -1;
467  c = AV_RB16(p);
468  p += 2;
469  *pp = p;
470  return c;
471 }
472 
473 /* read and allocate a DVB string preceded by its length */
474 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
475 {
476  int len;
477  const uint8_t *p;
478  char *str;
479 
480  p = *pp;
481  len = get8(&p, p_end);
482  if (len < 0)
483  return NULL;
484  if ((p + len) > p_end)
485  return NULL;
486  str = av_malloc(len + 1);
487  if (!str)
488  return NULL;
489  memcpy(str, p, len);
490  str[len] = '\0';
491  p += len;
492  *pp = p;
493  return str;
494 }
495 
497  const uint8_t **pp, const uint8_t *p_end)
498 {
499  int val;
500 
501  val = get8(pp, p_end);
502  if (val < 0)
503  return -1;
504  h->tid = val;
505  *pp += 2;
506  val = get16(pp, p_end);
507  if (val < 0)
508  return -1;
509  h->id = val;
510  val = get8(pp, p_end);
511  if (val < 0)
512  return -1;
513  h->version = (val >> 1) & 0x1f;
514  val = get8(pp, p_end);
515  if (val < 0)
516  return -1;
517  h->sec_num = val;
518  val = get8(pp, p_end);
519  if (val < 0)
520  return -1;
521  h->last_sec_num = val;
522  return 0;
523 }
524 
525 typedef struct {
526  uint32_t stream_type;
529 } StreamType;
530 
531 static const StreamType ISO_types[] = {
538  { 0x11, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AAC_LATM }, /* LATM syntax */
544  { 0 },
545 };
546 
547 static const StreamType HDMV_types[] = {
553  { 0x85, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD */
554  { 0x86, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS }, /* DTS HD MASTER*/
556  { 0 },
557 };
558 
559 /* ATSC ? */
560 static const StreamType MISC_types[] = {
563  { 0 },
564 };
565 
566 static const StreamType REGD_types[] = {
567  { MKTAG('d','r','a','c'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC },
568  { MKTAG('A','C','-','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 },
569  { MKTAG('B','S','S','D'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_S302M },
570  { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
571  { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
572  { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS },
573  { MKTAG('H','E','V','C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC },
574  { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VC1 },
575  { 0 },
576 };
577 
578 /* descriptor present */
579 static const StreamType DESC_types[] = {
580  { 0x6a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_AC3 }, /* AC-3 descriptor */
581  { 0x7a, AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
584  { 0x59, AVMEDIA_TYPE_SUBTITLE, AV_CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
585  { 0 },
586 };
587 
589  uint32_t stream_type, const StreamType *types)
590 {
591  for (; types->stream_type; types++) {
592  if (stream_type == types->stream_type) {
593  st->codec->codec_type = types->codec_type;
594  st->codec->codec_id = types->codec_id;
595  return;
596  }
597  }
598 }
599 
601  uint32_t stream_type, uint32_t prog_reg_desc)
602 {
603  avpriv_set_pts_info(st, 33, 1, 90000);
604  st->priv_data = pes;
608  pes->st = st;
609  pes->stream_type = stream_type;
610 
611  av_log(pes->stream, AV_LOG_DEBUG,
612  "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
613  st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
614 
615  st->codec->codec_tag = pes->stream_type;
616 
617  mpegts_find_stream_type(st, pes->stream_type, ISO_types);
618  if (prog_reg_desc == AV_RL32("HDMV") &&
619  st->codec->codec_id == AV_CODEC_ID_NONE) {
620  mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
621  if (pes->stream_type == 0x83) {
622  // HDMV TrueHD streams also contain an AC3 coded version of the
623  // audio track - add a second stream for this
624  AVStream *sub_st;
625  // priv_data cannot be shared between streams
626  PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
627  if (!sub_pes)
628  return AVERROR(ENOMEM);
629  memcpy(sub_pes, pes, sizeof(*sub_pes));
630 
631  sub_st = avformat_new_stream(pes->stream, NULL);
632  if (!sub_st) {
633  av_free(sub_pes);
634  return AVERROR(ENOMEM);
635  }
636 
637  sub_st->id = pes->pid;
638  avpriv_set_pts_info(sub_st, 33, 1, 90000);
639  sub_st->priv_data = sub_pes;
641  sub_st->codec->codec_id = AV_CODEC_ID_AC3;
643  sub_pes->sub_st = pes->sub_st = sub_st;
644  }
645  }
646  if (st->codec->codec_id == AV_CODEC_ID_NONE)
647  mpegts_find_stream_type(st, pes->stream_type, MISC_types);
648 
649  return 0;
650 }
651 
652 static void new_pes_packet(PESContext *pes, AVPacket *pkt)
653 {
654  av_init_packet(pkt);
655 
656  pkt->buf = pes->buffer;
657  pkt->data = pes->buffer->data;
658  pkt->size = pes->data_index;
659 
660  if(pes->total_size != MAX_PES_PAYLOAD &&
661  pes->pes_header_size + pes->data_index != pes->total_size + PES_START_SIZE) {
662  av_log(pes->stream, AV_LOG_WARNING, "PES packet size mismatch\n");
663  pes->flags |= AV_PKT_FLAG_CORRUPT;
664  }
665  memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
666 
667  // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
668  if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
669  pkt->stream_index = pes->sub_st->index;
670  else
671  pkt->stream_index = pes->st->index;
672  pkt->pts = pes->pts;
673  pkt->dts = pes->dts;
674  /* store position of first TS packet of this PES packet */
675  pkt->pos = pes->ts_packet_pos;
676  pkt->flags = pes->flags;
677 
678  /* reset pts values */
679  pes->pts = AV_NOPTS_VALUE;
680  pes->dts = AV_NOPTS_VALUE;
681  pes->buffer = NULL;
682  pes->data_index = 0;
683  pes->flags = 0;
684 }
685 
686 static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
687 {
688  GetBitContext gb;
689  int au_start_flag = 0, au_end_flag = 0, ocr_flag = 0, idle_flag = 0;
690  int padding_flag = 0, padding_bits = 0, inst_bitrate_flag = 0;
691  int dts_flag = -1, cts_flag = -1;
692  int64_t dts = AV_NOPTS_VALUE, cts = AV_NOPTS_VALUE;
693  init_get_bits(&gb, buf, buf_size*8);
694 
695  if (sl->use_au_start)
696  au_start_flag = get_bits1(&gb);
697  if (sl->use_au_end)
698  au_end_flag = get_bits1(&gb);
699  if (!sl->use_au_start && !sl->use_au_end)
700  au_start_flag = au_end_flag = 1;
701  if (sl->ocr_len > 0)
702  ocr_flag = get_bits1(&gb);
703  if (sl->use_idle)
704  idle_flag = get_bits1(&gb);
705  if (sl->use_padding)
706  padding_flag = get_bits1(&gb);
707  if (padding_flag)
708  padding_bits = get_bits(&gb, 3);
709 
710  if (!idle_flag && (!padding_flag || padding_bits != 0)) {
711  if (sl->packet_seq_num_len)
713  if (sl->degr_prior_len)
714  if (get_bits1(&gb))
715  skip_bits(&gb, sl->degr_prior_len);
716  if (ocr_flag)
717  skip_bits_long(&gb, sl->ocr_len);
718  if (au_start_flag) {
719  if (sl->use_rand_acc_pt)
720  get_bits1(&gb);
721  if (sl->au_seq_num_len > 0)
722  skip_bits_long(&gb, sl->au_seq_num_len);
723  if (sl->use_timestamps) {
724  dts_flag = get_bits1(&gb);
725  cts_flag = get_bits1(&gb);
726  }
727  }
728  if (sl->inst_bitrate_len)
729  inst_bitrate_flag = get_bits1(&gb);
730  if (dts_flag == 1)
731  dts = get_bits64(&gb, sl->timestamp_len);
732  if (cts_flag == 1)
733  cts = get_bits64(&gb, sl->timestamp_len);
734  if (sl->au_len > 0)
735  skip_bits_long(&gb, sl->au_len);
736  if (inst_bitrate_flag)
738  }
739 
740  if (dts != AV_NOPTS_VALUE)
741  pes->dts = dts;
742  if (cts != AV_NOPTS_VALUE)
743  pes->pts = cts;
744 
745  if (sl->timestamp_len && sl->timestamp_res)
747 
748  return (get_bits_count(&gb) + 7) >> 3;
749 }
750 
751 /* return non zero if a packet could be constructed */
753  const uint8_t *buf, int buf_size, int is_start,
754  int64_t pos)
755 {
756  PESContext *pes = filter->u.pes_filter.opaque;
757  MpegTSContext *ts = pes->ts;
758  const uint8_t *p;
759  int len, code;
760 
761  if(!ts->pkt)
762  return 0;
763 
764  if (is_start) {
765  if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
766  new_pes_packet(pes, ts->pkt);
767  ts->stop_parse = 1;
768  }
769  pes->state = MPEGTS_HEADER;
770  pes->data_index = 0;
771  pes->ts_packet_pos = pos;
772  }
773  p = buf;
774  while (buf_size > 0) {
775  switch(pes->state) {
776  case MPEGTS_HEADER:
777  len = PES_START_SIZE - pes->data_index;
778  if (len > buf_size)
779  len = buf_size;
780  memcpy(pes->header + pes->data_index, p, len);
781  pes->data_index += len;
782  p += len;
783  buf_size -= len;
784  if (pes->data_index == PES_START_SIZE) {
785  /* we got all the PES or section header. We can now
786  decide */
787  if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
788  pes->header[2] == 0x01) {
789  /* it must be an mpeg2 PES stream */
790  code = pes->header[3] | 0x100;
791  av_dlog(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
792 
793  if ((pes->st && pes->st->discard == AVDISCARD_ALL &&
794  (!pes->sub_st || pes->sub_st->discard == AVDISCARD_ALL)) ||
795  code == 0x1be) /* padding_stream */
796  goto skip;
797 
798  /* stream not present in PMT */
799  if (!pes->st) {
800  pes->st = avformat_new_stream(ts->stream, NULL);
801  if (!pes->st)
802  return AVERROR(ENOMEM);
803  pes->st->id = pes->pid;
804  mpegts_set_stream_info(pes->st, pes, 0, 0);
805  }
806 
807  pes->total_size = AV_RB16(pes->header + 4);
808  /* NOTE: a zero total size means the PES size is
809  unbounded */
810  if (!pes->total_size)
812 
813  /* allocate pes buffer */
814  pes->buffer = av_buffer_alloc(pes->total_size +
816  if (!pes->buffer)
817  return AVERROR(ENOMEM);
818 
819  if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
820  code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
821  code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
822  code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
823  pes->state = MPEGTS_PESHEADER;
824  if (pes->st->codec->codec_id == AV_CODEC_ID_NONE) {
825  av_dlog(pes->stream, "pid=%x stream_type=%x probing\n",
826  pes->pid, pes->stream_type);
828  }
829  } else {
830  pes->state = MPEGTS_PAYLOAD;
831  pes->data_index = 0;
832  }
833  } else {
834  /* otherwise, it should be a table */
835  /* skip packet */
836  skip:
837  pes->state = MPEGTS_SKIP;
838  continue;
839  }
840  }
841  break;
842  /**********************************************/
843  /* PES packing parsing */
844  case MPEGTS_PESHEADER:
845  len = PES_HEADER_SIZE - pes->data_index;
846  if (len < 0)
847  return -1;
848  if (len > buf_size)
849  len = buf_size;
850  memcpy(pes->header + pes->data_index, p, len);
851  pes->data_index += len;
852  p += len;
853  buf_size -= len;
854  if (pes->data_index == PES_HEADER_SIZE) {
855  pes->pes_header_size = pes->header[8] + 9;
857  }
858  break;
860  len = pes->pes_header_size - pes->data_index;
861  if (len < 0)
862  return -1;
863  if (len > buf_size)
864  len = buf_size;
865  memcpy(pes->header + pes->data_index, p, len);
866  pes->data_index += len;
867  p += len;
868  buf_size -= len;
869  if (pes->data_index == pes->pes_header_size) {
870  const uint8_t *r;
871  unsigned int flags, pes_ext, skip;
872 
873  flags = pes->header[7];
874  r = pes->header + 9;
875  pes->pts = AV_NOPTS_VALUE;
876  pes->dts = AV_NOPTS_VALUE;
877  if ((flags & 0xc0) == 0x80) {
878  pes->dts = pes->pts = ff_parse_pes_pts(r);
879  r += 5;
880  } else if ((flags & 0xc0) == 0xc0) {
881  pes->pts = ff_parse_pes_pts(r);
882  r += 5;
883  pes->dts = ff_parse_pes_pts(r);
884  r += 5;
885  }
886  pes->extended_stream_id = -1;
887  if (flags & 0x01) { /* PES extension */
888  pes_ext = *r++;
889  /* Skip PES private data, program packet sequence counter and P-STD buffer */
890  skip = (pes_ext >> 4) & 0xb;
891  skip += skip & 0x9;
892  r += skip;
893  if ((pes_ext & 0x41) == 0x01 &&
894  (r + 2) <= (pes->header + pes->pes_header_size)) {
895  /* PES extension 2 */
896  if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
897  pes->extended_stream_id = r[1];
898  }
899  }
900 
901  /* we got the full header. We parse it and get the payload */
902  pes->state = MPEGTS_PAYLOAD;
903  pes->data_index = 0;
904  if (pes->stream_type == 0x12 && buf_size > 0) {
905  int sl_header_bytes = read_sl_header(pes, &pes->sl, p, buf_size);
906  pes->pes_header_size += sl_header_bytes;
907  p += sl_header_bytes;
908  buf_size -= sl_header_bytes;
909  }
910  }
911  break;
912  case MPEGTS_PAYLOAD:
913  if (buf_size > 0 && pes->buffer) {
914  if (pes->data_index > 0 && pes->data_index+buf_size > pes->total_size) {
915  new_pes_packet(pes, ts->pkt);
918  if (!pes->buffer)
919  return AVERROR(ENOMEM);
920  ts->stop_parse = 1;
921  } else if (pes->data_index == 0 && buf_size > pes->total_size) {
922  // pes packet size is < ts size packet and pes data is padded with 0xff
923  // not sure if this is legal in ts but see issue #2392
924  buf_size = pes->total_size;
925  }
926  memcpy(pes->buffer->data + pes->data_index, p, buf_size);
927  pes->data_index += buf_size;
928  }
929  buf_size = 0;
930  /* emit complete packets with known packet size
931  * decreases demuxer delay for infrequent packets like subtitles from
932  * a couple of seconds to milliseconds for properly muxed files.
933  * total_size is the number of bytes following pes_packet_length
934  * in the pes header, i.e. not counting the first PES_START_SIZE bytes */
935  if (!ts->stop_parse && pes->total_size < MAX_PES_PAYLOAD &&
936  pes->pes_header_size + pes->data_index == pes->total_size + PES_START_SIZE) {
937  ts->stop_parse = 1;
938  new_pes_packet(pes, ts->pkt);
939  }
940  break;
941  case MPEGTS_SKIP:
942  buf_size = 0;
943  break;
944  }
945  }
946 
947  return 0;
948 }
949 
950 static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
951 {
952  MpegTSFilter *tss;
953  PESContext *pes;
954 
955  /* if no pid found, then add a pid context */
956  pes = av_mallocz(sizeof(PESContext));
957  if (!pes)
958  return 0;
959  pes->ts = ts;
960  pes->stream = ts->stream;
961  pes->pid = pid;
962  pes->pcr_pid = pcr_pid;
963  pes->state = MPEGTS_SKIP;
964  pes->pts = AV_NOPTS_VALUE;
965  pes->dts = AV_NOPTS_VALUE;
966  tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
967  if (!tss) {
968  av_free(pes);
969  return 0;
970  }
971  return pes;
972 }
973 
974 #define MAX_LEVEL 4
975 typedef struct {
982  int level;
984 
986  MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf,
987  unsigned size, Mp4Descr *descr, int max_descr_count)
988 {
989  int ret;
990  if (size > (1<<30))
991  return AVERROR_INVALIDDATA;
992 
993  if ((ret = ffio_init_context(&d->pb, (unsigned char*)buf, size, 0,
994  NULL, NULL, NULL, NULL)) < 0)
995  return ret;
996 
997  d->s = s;
998  d->level = 0;
999  d->descr_count = 0;
1000  d->descr = descr;
1001  d->active_descr = NULL;
1002  d->max_descr_count = max_descr_count;
1003 
1004  return 0;
1005 }
1006 
1007 static void update_offsets(AVIOContext *pb, int64_t *off, int *len) {
1008  int64_t new_off = avio_tell(pb);
1009  (*len) -= new_off - *off;
1010  *off = new_off;
1011 }
1012 
1013 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1014  int target_tag);
1015 
1016 static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
1017 {
1018  while (len > 0) {
1019  if (parse_mp4_descr(d, off, len, 0) < 0)
1020  return -1;
1021  update_offsets(&d->pb, &off, &len);
1022  }
1023  return 0;
1024 }
1025 
1026 static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1027 {
1028  avio_rb16(&d->pb); // ID
1029  avio_r8(&d->pb);
1030  avio_r8(&d->pb);
1031  avio_r8(&d->pb);
1032  avio_r8(&d->pb);
1033  avio_r8(&d->pb);
1034  update_offsets(&d->pb, &off, &len);
1035  return parse_mp4_descr_arr(d, off, len);
1036 }
1037 
1038 static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
1039 {
1040  int id_flags;
1041  if (len < 2)
1042  return 0;
1043  id_flags = avio_rb16(&d->pb);
1044  if (!(id_flags & 0x0020)) { //URL_Flag
1045  update_offsets(&d->pb, &off, &len);
1046  return parse_mp4_descr_arr(d, off, len); //ES_Descriptor[]
1047  } else {
1048  return 0;
1049  }
1050 }
1051 
1052 static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1053 {
1054  int es_id = 0;
1055  if (d->descr_count >= d->max_descr_count)
1056  return -1;
1057  ff_mp4_parse_es_descr(&d->pb, &es_id);
1058  d->active_descr = d->descr + (d->descr_count++);
1059 
1060  d->active_descr->es_id = es_id;
1061  update_offsets(&d->pb, &off, &len);
1062  parse_mp4_descr(d, off, len, MP4DecConfigDescrTag);
1063  update_offsets(&d->pb, &off, &len);
1064  if (len > 0)
1065  parse_mp4_descr(d, off, len, MP4SLDescrTag);
1066  d->active_descr = NULL;
1067  return 0;
1068 }
1069 
1071 {
1072  Mp4Descr *descr = d->active_descr;
1073  if (!descr)
1074  return -1;
1076  if (!descr->dec_config_descr)
1077  return AVERROR(ENOMEM);
1078  descr->dec_config_descr_len = len;
1079  avio_read(&d->pb, descr->dec_config_descr, len);
1080  return 0;
1081 }
1082 
1083 static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
1084 {
1085  Mp4Descr *descr = d->active_descr;
1086  int predefined;
1087  if (!descr)
1088  return -1;
1089 
1090  predefined = avio_r8(&d->pb);
1091  if (!predefined) {
1092  int lengths;
1093  int flags = avio_r8(&d->pb);
1094  descr->sl.use_au_start = !!(flags & 0x80);
1095  descr->sl.use_au_end = !!(flags & 0x40);
1096  descr->sl.use_rand_acc_pt = !!(flags & 0x20);
1097  descr->sl.use_padding = !!(flags & 0x08);
1098  descr->sl.use_timestamps = !!(flags & 0x04);
1099  descr->sl.use_idle = !!(flags & 0x02);
1100  descr->sl.timestamp_res = avio_rb32(&d->pb);
1101  avio_rb32(&d->pb);
1102  descr->sl.timestamp_len = avio_r8(&d->pb);
1103  descr->sl.ocr_len = avio_r8(&d->pb);
1104  descr->sl.au_len = avio_r8(&d->pb);
1105  descr->sl.inst_bitrate_len = avio_r8(&d->pb);
1106  lengths = avio_rb16(&d->pb);
1107  descr->sl.degr_prior_len = lengths >> 12;
1108  descr->sl.au_seq_num_len = (lengths >> 7) & 0x1f;
1109  descr->sl.packet_seq_num_len = (lengths >> 2) & 0x1f;
1110  } else {
1111  avpriv_report_missing_feature(d->s, "Predefined SLConfigDescriptor");
1112  }
1113  return 0;
1114 }
1115 
1116 static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len,
1117  int target_tag) {
1118  int tag;
1119  int len1 = ff_mp4_read_descr(d->s, &d->pb, &tag);
1120  update_offsets(&d->pb, &off, &len);
1121  if (len < 0 || len1 > len || len1 <= 0) {
1122  av_log(d->s, AV_LOG_ERROR, "Tag %x length violation new length %d bytes remaining %d\n", tag, len1, len);
1123  return -1;
1124  }
1125 
1126  if (d->level++ >= MAX_LEVEL) {
1127  av_log(d->s, AV_LOG_ERROR, "Maximum MP4 descriptor level exceeded\n");
1128  goto done;
1129  }
1130 
1131  if (target_tag && tag != target_tag) {
1132  av_log(d->s, AV_LOG_ERROR, "Found tag %x expected %x\n", tag, target_tag);
1133  goto done;
1134  }
1135 
1136  switch (tag) {
1137  case MP4IODescrTag:
1138  parse_MP4IODescrTag(d, off, len1);
1139  break;
1140  case MP4ODescrTag:
1141  parse_MP4ODescrTag(d, off, len1);
1142  break;
1143  case MP4ESDescrTag:
1144  parse_MP4ESDescrTag(d, off, len1);
1145  break;
1146  case MP4DecConfigDescrTag:
1147  parse_MP4DecConfigDescrTag(d, off, len1);
1148  break;
1149  case MP4SLDescrTag:
1150  parse_MP4SLDescrTag(d, off, len1);
1151  break;
1152  }
1153 
1154 done:
1155  d->level--;
1156  avio_seek(&d->pb, off + len1, SEEK_SET);
1157  return 0;
1158 }
1159 
1160 static int mp4_read_iods(AVFormatContext *s, const uint8_t *buf, unsigned size,
1161  Mp4Descr *descr, int *descr_count, int max_descr_count)
1162 {
1164  if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
1165  return -1;
1166 
1167  parse_mp4_descr(&d, avio_tell(&d.pb), size, MP4IODescrTag);
1168 
1169  *descr_count = d.descr_count;
1170  return 0;
1171 }
1172 
1173 static int mp4_read_od(AVFormatContext *s, const uint8_t *buf, unsigned size,
1174  Mp4Descr *descr, int *descr_count, int max_descr_count)
1175 {
1177  if (init_MP4DescrParseContext(&d, s, buf, size, descr, max_descr_count) < 0)
1178  return -1;
1179 
1180  parse_mp4_descr_arr(&d, avio_tell(&d.pb), size);
1181 
1182  *descr_count = d.descr_count;
1183  return 0;
1184 }
1185 
1186 static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1187 {
1188  MpegTSContext *ts = filter->u.section_filter.opaque;
1189  SectionHeader h;
1190  const uint8_t *p, *p_end;
1191  AVIOContext pb;
1192  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
1193  int mp4_descr_count = 0;
1194  int i, pid;
1195  AVFormatContext *s = ts->stream;
1196 
1197  p_end = section + section_len - 4;
1198  p = section;
1199  if (parse_section_header(&h, &p, p_end) < 0)
1200  return;
1201  if (h.tid != M4OD_TID)
1202  return;
1203 
1204  mp4_read_od(s, p, (unsigned)(p_end - p), mp4_descr, &mp4_descr_count, MAX_MP4_DESCR_COUNT);
1205 
1206  for (pid = 0; pid < NB_PID_MAX; pid++) {
1207  if (!ts->pids[pid])
1208  continue;
1209  for (i = 0; i < mp4_descr_count; i++) {
1210  PESContext *pes;
1211  AVStream *st;
1212  if (ts->pids[pid]->es_id != mp4_descr[i].es_id)
1213  continue;
1214  if (!(ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES)) {
1215  av_log(s, AV_LOG_ERROR, "pid %x is not PES\n", pid);
1216  continue;
1217  }
1218  pes = ts->pids[pid]->u.pes_filter.opaque;
1219  st = pes->st;
1220  if (!st) {
1221  continue;
1222  }
1223 
1224  pes->sl = mp4_descr[i].sl;
1225 
1226  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1227  mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1228  ff_mp4_read_dec_config_descr(s, st, &pb);
1229  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1230  st->codec->extradata_size > 0)
1231  st->need_parsing = 0;
1232  if (st->codec->codec_id == AV_CODEC_ID_H264 &&
1233  st->codec->extradata_size > 0)
1234  st->need_parsing = 0;
1235 
1236  if (st->codec->codec_id <= AV_CODEC_ID_NONE) {
1237  } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_AUDIO) {
1239  } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_SUBTITLE) {
1241  } else if (st->codec->codec_id < AV_CODEC_ID_FIRST_UNKNOWN) {
1243  }
1244  }
1245  }
1246  for (i = 0; i < mp4_descr_count; i++)
1247  av_free(mp4_descr[i].dec_config_descr);
1248 }
1249 
1251  const uint8_t **pp, const uint8_t *desc_list_end,
1252  Mp4Descr *mp4_descr, int mp4_descr_count, int pid,
1253  MpegTSContext *ts)
1254 {
1255  const uint8_t *desc_end;
1256  int desc_len, desc_tag, desc_es_id;
1257  char language[252];
1258  int i;
1259 
1260  desc_tag = get8(pp, desc_list_end);
1261  if (desc_tag < 0)
1262  return -1;
1263  desc_len = get8(pp, desc_list_end);
1264  if (desc_len < 0)
1265  return -1;
1266  desc_end = *pp + desc_len;
1267  if (desc_end > desc_list_end)
1268  return -1;
1269 
1270  av_dlog(fc, "tag: 0x%02x len=%d\n", desc_tag, desc_len);
1271 
1272  if (st->codec->codec_id == AV_CODEC_ID_NONE &&
1273  stream_type == STREAM_TYPE_PRIVATE_DATA)
1274  mpegts_find_stream_type(st, desc_tag, DESC_types);
1275 
1276  switch(desc_tag) {
1277  case 0x1E: /* SL descriptor */
1278  desc_es_id = get16(pp, desc_end);
1279  if (ts && ts->pids[pid])
1280  ts->pids[pid]->es_id = desc_es_id;
1281  for (i = 0; i < mp4_descr_count; i++)
1282  if (mp4_descr[i].dec_config_descr_len &&
1283  mp4_descr[i].es_id == desc_es_id) {
1284  AVIOContext pb;
1285  ffio_init_context(&pb, mp4_descr[i].dec_config_descr,
1286  mp4_descr[i].dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1287  ff_mp4_read_dec_config_descr(fc, st, &pb);
1288  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1289  st->codec->extradata_size > 0)
1290  st->need_parsing = 0;
1292  mpegts_open_section_filter(ts, pid, m4sl_cb, ts, 1);
1293  }
1294  break;
1295  case 0x1F: /* FMC descriptor */
1296  get16(pp, desc_end);
1297  if (mp4_descr_count > 0 && st->codec->codec_id == AV_CODEC_ID_AAC_LATM &&
1298  mp4_descr->dec_config_descr_len && mp4_descr->es_id == pid) {
1299  AVIOContext pb;
1300  ffio_init_context(&pb, mp4_descr->dec_config_descr,
1301  mp4_descr->dec_config_descr_len, 0, NULL, NULL, NULL, NULL);
1302  ff_mp4_read_dec_config_descr(fc, st, &pb);
1303  if (st->codec->codec_id == AV_CODEC_ID_AAC &&
1304  st->codec->extradata_size > 0)
1305  st->need_parsing = 0;
1306  }
1307  break;
1308  case 0x56: /* DVB teletext descriptor */
1309  language[0] = get8(pp, desc_end);
1310  language[1] = get8(pp, desc_end);
1311  language[2] = get8(pp, desc_end);
1312  language[3] = 0;
1313  av_dict_set(&st->metadata, "language", language, 0);
1314  break;
1315  case 0x59: /* subtitling descriptor */
1316  language[0] = get8(pp, desc_end);
1317  language[1] = get8(pp, desc_end);
1318  language[2] = get8(pp, desc_end);
1319  language[3] = 0;
1320  /* hearing impaired subtitles detection */
1321  switch(get8(pp, desc_end)) {
1322  case 0x20: /* DVB subtitles (for the hard of hearing) with no monitor aspect ratio criticality */
1323  case 0x21: /* DVB subtitles (for the hard of hearing) for display on 4:3 aspect ratio monitor */
1324  case 0x22: /* DVB subtitles (for the hard of hearing) for display on 16:9 aspect ratio monitor */
1325  case 0x23: /* DVB subtitles (for the hard of hearing) for display on 2.21:1 aspect ratio monitor */
1326  case 0x24: /* DVB subtitles (for the hard of hearing) for display on a high definition monitor */
1327  case 0x25: /* DVB subtitles (for the hard of hearing) with plano-stereoscopic disparity for display on a high definition monitor */
1329  break;
1330  }
1331  if (st->codec->extradata) {
1332  if (st->codec->extradata_size == 4 && memcmp(st->codec->extradata, *pp, 4))
1333  avpriv_request_sample(fc, "DVB sub with multiple IDs");
1334  } else {
1336  if (st->codec->extradata) {
1337  st->codec->extradata_size = 4;
1338  memcpy(st->codec->extradata, *pp, 4);
1339  }
1340  }
1341  *pp += 4;
1342  av_dict_set(&st->metadata, "language", language, 0);
1343  break;
1344  case 0x0a: /* ISO 639 language descriptor */
1345  for (i = 0; i + 4 <= desc_len; i += 4) {
1346  language[i + 0] = get8(pp, desc_end);
1347  language[i + 1] = get8(pp, desc_end);
1348  language[i + 2] = get8(pp, desc_end);
1349  language[i + 3] = ',';
1350  switch (get8(pp, desc_end)) {
1351  case 0x01: st->disposition |= AV_DISPOSITION_CLEAN_EFFECTS; break;
1352  case 0x02: st->disposition |= AV_DISPOSITION_HEARING_IMPAIRED; break;
1353  case 0x03: st->disposition |= AV_DISPOSITION_VISUAL_IMPAIRED; break;
1354  }
1355  }
1356  if (i) {
1357  language[i - 1] = 0;
1358  av_dict_set(&st->metadata, "language", language, 0);
1359  }
1360  break;
1361  case 0x05: /* registration descriptor */
1362  st->codec->codec_tag = bytestream_get_le32(pp);
1363  av_dlog(fc, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
1364  if (st->codec->codec_id == AV_CODEC_ID_NONE)
1365  mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
1366  break;
1367  default:
1368  break;
1369  }
1370  *pp = desc_end;
1371  return 0;
1372 }
1373 
1374 static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1375 {
1376  MpegTSContext *ts = filter->u.section_filter.opaque;
1377  SectionHeader h1, *h = &h1;
1378  PESContext *pes;
1379  AVStream *st;
1380  const uint8_t *p, *p_end, *desc_list_end;
1381  int program_info_length, pcr_pid, pid, stream_type;
1382  int desc_list_len;
1383  uint32_t prog_reg_desc = 0; /* registration descriptor */
1384 
1385  Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = {{ 0 }};
1386  int mp4_descr_count = 0;
1387  int i;
1388 
1389  av_dlog(ts->stream, "PMT: len %i\n", section_len);
1390  hex_dump_debug(ts->stream, section, section_len);
1391 
1392  p_end = section + section_len - 4;
1393  p = section;
1394  if (parse_section_header(h, &p, p_end) < 0)
1395  return;
1396 
1397  av_dlog(ts->stream, "sid=0x%x sec_num=%d/%d\n",
1398  h->id, h->sec_num, h->last_sec_num);
1399 
1400  if (h->tid != PMT_TID)
1401  return;
1402 
1403  clear_program(ts, h->id);
1404  pcr_pid = get16(&p, p_end);
1405  if (pcr_pid < 0)
1406  return;
1407  pcr_pid &= 0x1fff;
1408  add_pid_to_pmt(ts, h->id, pcr_pid);
1409 
1410  av_dlog(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
1411 
1412  program_info_length = get16(&p, p_end);
1413  if (program_info_length < 0)
1414  return;
1415  program_info_length &= 0xfff;
1416  while(program_info_length >= 2) {
1417  uint8_t tag, len;
1418  tag = get8(&p, p_end);
1419  len = get8(&p, p_end);
1420 
1421  av_dlog(ts->stream, "program tag: 0x%02x len=%d\n", tag, len);
1422 
1423  if(len > program_info_length - 2)
1424  //something else is broken, exit the program_descriptors_loop
1425  break;
1426  program_info_length -= len + 2;
1427  if (tag == 0x1d) { // IOD descriptor
1428  get8(&p, p_end); // scope
1429  get8(&p, p_end); // label
1430  len -= 2;
1431  mp4_read_iods(ts->stream, p, len, mp4_descr + mp4_descr_count,
1432  &mp4_descr_count, MAX_MP4_DESCR_COUNT);
1433  } else if (tag == 0x05 && len >= 4) { // registration descriptor
1434  prog_reg_desc = bytestream_get_le32(&p);
1435  len -= 4;
1436  }
1437  p += len;
1438  }
1439  p += program_info_length;
1440  if (p >= p_end)
1441  goto out;
1442 
1443  // stop parsing after pmt, we found header
1444  if (!ts->stream->nb_streams)
1445  ts->stop_parse = 1;
1446 
1447  for(;;) {
1448  st = 0;
1449  pes = NULL;
1450  stream_type = get8(&p, p_end);
1451  if (stream_type < 0)
1452  break;
1453  pid = get16(&p, p_end);
1454  if (pid < 0)
1455  break;
1456  pid &= 0x1fff;
1457 
1458  /* now create stream */
1459  if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
1460  pes = ts->pids[pid]->u.pes_filter.opaque;
1461  if (!pes->st) {
1462  pes->st = avformat_new_stream(pes->stream, NULL);
1463  pes->st->id = pes->pid;
1464  }
1465  st = pes->st;
1466  } else if (stream_type != 0x13) {
1467  if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
1468  pes = add_pes_stream(ts, pid, pcr_pid);
1469  if (pes) {
1470  st = avformat_new_stream(pes->stream, NULL);
1471  st->id = pes->pid;
1472  }
1473  } else {
1474  int idx = ff_find_stream_index(ts->stream, pid);
1475  if (idx >= 0) {
1476  st = ts->stream->streams[idx];
1477  } else {
1478  st = avformat_new_stream(ts->stream, NULL);
1479  st->id = pid;
1481  }
1482  }
1483 
1484  if (!st)
1485  goto out;
1486 
1487  if (pes && !pes->stream_type)
1488  mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
1489 
1490  add_pid_to_pmt(ts, h->id, pid);
1491 
1493 
1494  desc_list_len = get16(&p, p_end);
1495  if (desc_list_len < 0)
1496  break;
1497  desc_list_len &= 0xfff;
1498  desc_list_end = p + desc_list_len;
1499  if (desc_list_end > p_end)
1500  break;
1501  for(;;) {
1502  if (ff_parse_mpeg2_descriptor(ts->stream, st, stream_type, &p, desc_list_end,
1503  mp4_descr, mp4_descr_count, pid, ts) < 0)
1504  break;
1505 
1506  if (pes && prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
1508  pes->sub_st->codec->codec_tag = st->codec->codec_tag;
1509  }
1510  }
1511  p = desc_list_end;
1512  }
1513 
1514  out:
1515  for (i = 0; i < mp4_descr_count; i++)
1516  av_free(mp4_descr[i].dec_config_descr);
1517 }
1518 
1519 static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1520 {
1521  MpegTSContext *ts = filter->u.section_filter.opaque;
1522  SectionHeader h1, *h = &h1;
1523  const uint8_t *p, *p_end;
1524  int sid, pmt_pid;
1525 
1526  av_dlog(ts->stream, "PAT:\n");
1527  hex_dump_debug(ts->stream, section, section_len);
1528 
1529  p_end = section + section_len - 4;
1530  p = section;
1531  if (parse_section_header(h, &p, p_end) < 0)
1532  return;
1533  if (h->tid != PAT_TID)
1534  return;
1535 
1536  clear_programs(ts);
1537  for(;;) {
1538  sid = get16(&p, p_end);
1539  if (sid < 0)
1540  break;
1541  pmt_pid = get16(&p, p_end);
1542  if (pmt_pid < 0)
1543  break;
1544  pmt_pid &= 0x1fff;
1545 
1546  av_dlog(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1547 
1548  if (sid == 0x0000) {
1549  /* NIT info */
1550  } else {
1551  av_new_program(ts->stream, sid);
1552  if (ts->pids[pmt_pid])
1553  mpegts_close_filter(ts, ts->pids[pmt_pid]);
1554  mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
1555  add_pat_entry(ts, sid);
1556  add_pid_to_pmt(ts, sid, 0); //add pat pid to program
1557  add_pid_to_pmt(ts, sid, pmt_pid);
1558  }
1559  }
1560 }
1561 
1562 static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
1563 {
1564  MpegTSContext *ts = filter->u.section_filter.opaque;
1565  SectionHeader h1, *h = &h1;
1566  const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1567  int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1568  char *name, *provider_name;
1569 
1570  av_dlog(ts->stream, "SDT:\n");
1571  hex_dump_debug(ts->stream, section, section_len);
1572 
1573  p_end = section + section_len - 4;
1574  p = section;
1575  if (parse_section_header(h, &p, p_end) < 0)
1576  return;
1577  if (h->tid != SDT_TID)
1578  return;
1579  onid = get16(&p, p_end);
1580  if (onid < 0)
1581  return;
1582  val = get8(&p, p_end);
1583  if (val < 0)
1584  return;
1585  for(;;) {
1586  sid = get16(&p, p_end);
1587  if (sid < 0)
1588  break;
1589  val = get8(&p, p_end);
1590  if (val < 0)
1591  break;
1592  desc_list_len = get16(&p, p_end);
1593  if (desc_list_len < 0)
1594  break;
1595  desc_list_len &= 0xfff;
1596  desc_list_end = p + desc_list_len;
1597  if (desc_list_end > p_end)
1598  break;
1599  for(;;) {
1600  desc_tag = get8(&p, desc_list_end);
1601  if (desc_tag < 0)
1602  break;
1603  desc_len = get8(&p, desc_list_end);
1604  desc_end = p + desc_len;
1605  if (desc_end > desc_list_end)
1606  break;
1607 
1608  av_dlog(ts->stream, "tag: 0x%02x len=%d\n",
1609  desc_tag, desc_len);
1610 
1611  switch(desc_tag) {
1612  case 0x48:
1613  service_type = get8(&p, p_end);
1614  if (service_type < 0)
1615  break;
1616  provider_name = getstr8(&p, p_end);
1617  if (!provider_name)
1618  break;
1619  name = getstr8(&p, p_end);
1620  if (name) {
1621  AVProgram *program = av_new_program(ts->stream, sid);
1622  if(program) {
1623  av_dict_set(&program->metadata, "service_name", name, 0);
1624  av_dict_set(&program->metadata, "service_provider", provider_name, 0);
1625  }
1626  }
1627  av_free(name);
1628  av_free(provider_name);
1629  break;
1630  default:
1631  break;
1632  }
1633  p = desc_end;
1634  }
1635  p = desc_list_end;
1636  }
1637 }
1638 
1639 /* handle one TS packet */
1640 static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
1641 {
1642  AVFormatContext *s = ts->stream;
1643  MpegTSFilter *tss;
1644  int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity,
1645  has_adaptation, has_payload;
1646  const uint8_t *p, *p_end;
1647  int64_t pos;
1648 
1649  pid = AV_RB16(packet + 1) & 0x1fff;
1650  if(pid && discard_pid(ts, pid))
1651  return 0;
1652  is_start = packet[1] & 0x40;
1653  tss = ts->pids[pid];
1654  if (ts->auto_guess && tss == NULL && is_start) {
1655  add_pes_stream(ts, pid, -1);
1656  tss = ts->pids[pid];
1657  }
1658  if (!tss)
1659  return 0;
1660 
1661  afc = (packet[3] >> 4) & 3;
1662  if (afc == 0) /* reserved value */
1663  return 0;
1664  has_adaptation = afc & 2;
1665  has_payload = afc & 1;
1666  is_discontinuity = has_adaptation
1667  && packet[4] != 0 /* with length > 0 */
1668  && (packet[5] & 0x80); /* and discontinuity indicated */
1669 
1670  /* continuity check (currently not used) */
1671  cc = (packet[3] & 0xf);
1672  expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc;
1673  cc_ok = pid == 0x1FFF // null packet PID
1674  || is_discontinuity
1675  || tss->last_cc < 0
1676  || expected_cc == cc;
1677 
1678  tss->last_cc = cc;
1679  if (!cc_ok) {
1681  "Continuity check failed for pid %d expected %d got %d\n",
1682  pid, expected_cc, cc);
1683  if(tss->type == MPEGTS_PES) {
1684  PESContext *pc = tss->u.pes_filter.opaque;
1685  pc->flags |= AV_PKT_FLAG_CORRUPT;
1686  }
1687  }
1688 
1689  if (!has_payload)
1690  return 0;
1691  p = packet + 4;
1692  if (has_adaptation) {
1693  /* skip adaptation field */
1694  p += p[0] + 1;
1695  }
1696  /* if past the end of packet, ignore */
1697  p_end = packet + TS_PACKET_SIZE;
1698  if (p >= p_end)
1699  return 0;
1700 
1701  pos = avio_tell(ts->stream->pb);
1702  MOD_UNLIKELY(ts->pos47, pos, ts->raw_packet_size, ts->pos);
1703 
1704  if (tss->type == MPEGTS_SECTION) {
1705  if (is_start) {
1706  /* pointer field present */
1707  len = *p++;
1708  if (p + len > p_end)
1709  return 0;
1710  if (len && cc_ok) {
1711  /* write remaining section bytes */
1712  write_section_data(s, tss,
1713  p, len, 0);
1714  /* check whether filter has been closed */
1715  if (!ts->pids[pid])
1716  return 0;
1717  }
1718  p += len;
1719  if (p < p_end) {
1720  write_section_data(s, tss,
1721  p, p_end - p, 1);
1722  }
1723  } else {
1724  if (cc_ok) {
1725  write_section_data(s, tss,
1726  p, p_end - p, 0);
1727  }
1728  }
1729  } else {
1730  int ret;
1731  // Note: The position here points actually behind the current packet.
1732  if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1733  pos - ts->raw_packet_size)) < 0)
1734  return ret;
1735  }
1736 
1737  return 0;
1738 }
1739 
1740 /* XXX: try to find a better synchro over several packets (use
1741  get_packet_size() ?) */
1743 {
1744  AVIOContext *pb = s->pb;
1745  int c, i;
1746 
1747  for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1748  c = avio_r8(pb);
1749  if (pb->eof_reached)
1750  return -1;
1751  if (c == 0x47) {
1752  avio_seek(pb, -1, SEEK_CUR);
1753  return 0;
1754  }
1755  }
1756  av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
1757  /* no sync found */
1758  return -1;
1759 }
1760 
1761 /* return -1 if error or EOF. Return 0 if OK. */
1762 static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
1763 {
1764  AVIOContext *pb = s->pb;
1765  int len;
1766 
1767  for(;;) {
1768  len = ffio_read_indirect(pb, buf, TS_PACKET_SIZE, data);
1769  if (len != TS_PACKET_SIZE)
1770  return len < 0 ? len : AVERROR_EOF;
1771  /* check packet sync byte */
1772  if ((*data)[0] != 0x47) {
1773  /* find a new packet start */
1774  avio_seek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1775  if (mpegts_resync(s) < 0)
1776  return AVERROR(EAGAIN);
1777  else
1778  continue;
1779  } else {
1780  break;
1781  }
1782  }
1783  return 0;
1784 }
1785 
1786 static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
1787 {
1788  AVIOContext *pb = s->pb;
1789  int skip = raw_packet_size - TS_PACKET_SIZE;
1790  if (skip > 0)
1791  avio_skip(pb, skip);
1792 }
1793 
1794 static int handle_packets(MpegTSContext *ts, int nb_packets)
1795 {
1796  AVFormatContext *s = ts->stream;
1798  const uint8_t *data;
1799  int packet_num, ret = 0;
1800 
1801  if (avio_tell(s->pb) != ts->last_pos) {
1802  int i;
1803  av_dlog(ts->stream, "Skipping after seek\n");
1804  /* seek detected, flush pes buffer */
1805  for (i = 0; i < NB_PID_MAX; i++) {
1806  if (ts->pids[i]) {
1807  if (ts->pids[i]->type == MPEGTS_PES) {
1808  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1809  av_buffer_unref(&pes->buffer);
1810  pes->data_index = 0;
1811  pes->state = MPEGTS_SKIP; /* skip until pes header */
1812  }
1813  ts->pids[i]->last_cc = -1;
1814  }
1815  }
1816  }
1817 
1818  ts->stop_parse = 0;
1819  packet_num = 0;
1820  memset(packet + TS_PACKET_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
1821  for(;;) {
1822  if (ts->stop_parse>0)
1823  break;
1824  packet_num++;
1825  if (nb_packets != 0 && packet_num >= nb_packets)
1826  break;
1827  ret = read_packet(s, packet, ts->raw_packet_size, &data);
1828  if (ret != 0)
1829  break;
1830  ret = handle_packet(ts, data);
1832  if (ret != 0)
1833  break;
1834  }
1835  ts->last_pos = avio_tell(s->pb);
1836  return ret;
1837 }
1838 
1840 {
1841  const int size= p->buf_size;
1842  int score, fec_score, dvhs_score;
1843  int check_count= size / TS_FEC_PACKET_SIZE;
1844 #define CHECK_COUNT 10
1845 
1846  if (check_count < CHECK_COUNT)
1847  return -1;
1848 
1849  score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1850  dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1851  fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1852  av_dlog(NULL, "score: %d, dvhs_score: %d, fec_score: %d \n",
1853  score, dvhs_score, fec_score);
1854 
1855 // we need a clear definition for the returned score otherwise things will become messy sooner or later
1856  if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1857  else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
1858  else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1859  else return -1;
1860 }
1861 
1862 /* return the 90kHz PCR and the extension for the 27MHz PCR. return
1863  (-1) if not available */
1864 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1865  const uint8_t *packet)
1866 {
1867  int afc, len, flags;
1868  const uint8_t *p;
1869  unsigned int v;
1870 
1871  afc = (packet[3] >> 4) & 3;
1872  if (afc <= 1)
1873  return -1;
1874  p = packet + 4;
1875  len = p[0];
1876  p++;
1877  if (len == 0)
1878  return -1;
1879  flags = *p++;
1880  len--;
1881  if (!(flags & 0x10))
1882  return -1;
1883  if (len < 6)
1884  return -1;
1885  v = AV_RB32(p);
1886  *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1887  *ppcr_low = ((p[4] & 1) << 8) | p[5];
1888  return 0;
1889 }
1890 
1892 {
1893  MpegTSContext *ts = s->priv_data;
1894  AVIOContext *pb = s->pb;
1895  uint8_t buf[5*1024];
1896  int len;
1897  int64_t pos;
1898 
1899  /* read the first 1024 bytes to get packet size */
1900  pos = avio_tell(pb);
1901  len = avio_read(pb, buf, sizeof(buf));
1902  if (len != sizeof(buf))
1903  goto fail;
1904  ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1905  if (ts->raw_packet_size <= 0)
1906  goto fail;
1907  ts->stream = s;
1908  ts->auto_guess = 0;
1909 
1910  if (s->iformat == &ff_mpegts_demuxer) {
1911  /* normal demux */
1912 
1913  /* first do a scan to get all the services */
1914  if (avio_seek(pb, pos, SEEK_SET) < 0 && pb->seekable)
1915  av_log(s, AV_LOG_ERROR, "Unable to seek back to the start\n");
1916 
1918 
1920 
1922  /* if could not find service, enable auto_guess */
1923 
1924  ts->auto_guess = 1;
1925 
1926  av_dlog(ts->stream, "tuning done\n");
1927 
1929  } else {
1930  AVStream *st;
1931  int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1932  int64_t pcrs[2], pcr_h;
1933  int packet_count[2];
1934  uint8_t packet[TS_PACKET_SIZE];
1935  const uint8_t *data;
1936 
1937  /* only read packets */
1938 
1939  st = avformat_new_stream(s, NULL);
1940  if (!st)
1941  goto fail;
1942  avpriv_set_pts_info(st, 60, 1, 27000000);
1945 
1946  /* we iterate until we find two PCRs to estimate the bitrate */
1947  pcr_pid = -1;
1948  nb_pcrs = 0;
1949  nb_packets = 0;
1950  for(;;) {
1951  ret = read_packet(s, packet, ts->raw_packet_size, &data);
1952  if (ret < 0)
1953  return -1;
1954  pid = AV_RB16(data + 1) & 0x1fff;
1955  if ((pcr_pid == -1 || pcr_pid == pid) &&
1956  parse_pcr(&pcr_h, &pcr_l, data) == 0) {
1958  pcr_pid = pid;
1959  packet_count[nb_pcrs] = nb_packets;
1960  pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1961  nb_pcrs++;
1962  if (nb_pcrs >= 2)
1963  break;
1964  } else {
1966  }
1967  nb_packets++;
1968  }
1969 
1970  /* NOTE1: the bitrate is computed without the FEC */
1971  /* NOTE2: it is only the bitrate of the start of the stream */
1972  ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1973  ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1974  s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1975  st->codec->bit_rate = s->bit_rate;
1976  st->start_time = ts->cur_pcr;
1977  av_dlog(ts->stream, "start=%0.3f pcr=%0.3f incr=%d\n",
1978  st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1979  }
1980 
1981  avio_seek(pb, pos, SEEK_SET);
1982  return 0;
1983  fail:
1984  return -1;
1985 }
1986 
1987 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1988 
1990  AVPacket *pkt)
1991 {
1992  MpegTSContext *ts = s->priv_data;
1993  int ret, i;
1994  int64_t pcr_h, next_pcr_h, pos;
1995  int pcr_l, next_pcr_l;
1996  uint8_t pcr_buf[12];
1997  const uint8_t *data;
1998 
1999  if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
2000  return AVERROR(ENOMEM);
2001  pkt->pos= avio_tell(s->pb);
2002  ret = read_packet(s, pkt->data, ts->raw_packet_size, &data);
2003  if (ret < 0) {
2004  av_free_packet(pkt);
2005  return ret;
2006  }
2007  if (data != pkt->data)
2008  memcpy(pkt->data, data, ts->raw_packet_size);
2010  if (ts->mpeg2ts_compute_pcr) {
2011  /* compute exact PCR for each packet */
2012  if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
2013  /* we read the next PCR (XXX: optimize it by using a bigger buffer */
2014  pos = avio_tell(s->pb);
2015  for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
2016  avio_seek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
2017  avio_read(s->pb, pcr_buf, 12);
2018  if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
2019  /* XXX: not precise enough */
2020  ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
2021  (i + 1);
2022  break;
2023  }
2024  }
2025  avio_seek(s->pb, pos, SEEK_SET);
2026  /* no next PCR found: we use previous increment */
2027  ts->cur_pcr = pcr_h * 300 + pcr_l;
2028  }
2029  pkt->pts = ts->cur_pcr;
2030  pkt->duration = ts->pcr_incr;
2031  ts->cur_pcr += ts->pcr_incr;
2032  }
2033  pkt->stream_index = 0;
2034  return 0;
2035 }
2036 
2038  AVPacket *pkt)
2039 {
2040  MpegTSContext *ts = s->priv_data;
2041  int ret, i;
2042 
2043  pkt->size = -1;
2044  ts->pkt = pkt;
2045  ret = handle_packets(ts, 0);
2046  if (ret < 0) {
2047  /* flush pes data left */
2048  for (i = 0; i < NB_PID_MAX; i++) {
2049  if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
2050  PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
2051  if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
2052  new_pes_packet(pes, pkt);
2053  pes->state = MPEGTS_SKIP;
2054  ret = 0;
2055  break;
2056  }
2057  }
2058  }
2059  }
2060 
2061  if (!ret && pkt->size < 0)
2062  ret = AVERROR(EINTR);
2063  return ret;
2064 }
2065 
2066 static void mpegts_free(MpegTSContext *ts)
2067 {
2068  int i;
2069 
2070  clear_programs(ts);
2071 
2072  for(i=0;i<NB_PID_MAX;i++)
2073  if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
2074 }
2075 
2077 {
2078  MpegTSContext *ts = s->priv_data;
2079  mpegts_free(ts);
2080  return 0;
2081 }
2082 
2083 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
2084  int64_t *ppos, int64_t pos_limit)
2085 {
2086  MpegTSContext *ts = s->priv_data;
2087  int64_t pos, timestamp;
2088  uint8_t buf[TS_PACKET_SIZE];
2089  int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
2090  const int find_next= 1;
2091  pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
2092  if (find_next) {
2093  for(;;) {
2094  avio_seek(s->pb, pos, SEEK_SET);
2095  if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2096  return AV_NOPTS_VALUE;
2097  if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2098  parse_pcr(&timestamp, &pcr_l, buf) == 0) {
2099  break;
2100  }
2101  pos += ts->raw_packet_size;
2102  }
2103  } else {
2104  for(;;) {
2105  pos -= ts->raw_packet_size;
2106  if (pos < 0)
2107  return AV_NOPTS_VALUE;
2108  avio_seek(s->pb, pos, SEEK_SET);
2109  if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2110  return AV_NOPTS_VALUE;
2111  if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
2112  parse_pcr(&timestamp, &pcr_l, buf) == 0) {
2113  break;
2114  }
2115  }
2116  }
2117  *ppos = pos;
2118 
2119  return timestamp;
2120 }
2121 
2122 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
2123  MpegTSContext *ts = s->priv_data;
2124  uint8_t buf[TS_PACKET_SIZE];
2125  int64_t pos;
2126 
2127  if (ff_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
2128  return -1;
2129 
2130  pos= avio_tell(s->pb);
2131 
2132  for(;;) {
2133  avio_seek(s->pb, pos, SEEK_SET);
2134  if (avio_read(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
2135  return -1;
2136 // pid = AV_RB16(buf + 1) & 0x1fff;
2137  if(buf[1] & 0x40) break;
2138  pos += ts->raw_packet_size;
2139  }
2140  avio_seek(s->pb, pos, SEEK_SET);
2141 
2142  return 0;
2143 }
2144 
2145 /**************************************************************/
2146 /* parsing functions - called from other demuxers such as RTP */
2147 
2149 {
2150  MpegTSContext *ts;
2151 
2152  ts = av_mallocz(sizeof(MpegTSContext));
2153  if (!ts)
2154  return NULL;
2155  /* no stream case, currently used by RTP */
2157  ts->stream = s;
2158  ts->auto_guess = 1;
2159  return ts;
2160 }
2161 
2162 /* return the consumed length if a packet was output, or -1 if no
2163  packet is output */
2165  const uint8_t *buf, int len)
2166 {
2167  int len1;
2168 
2169  len1 = len;
2170  ts->pkt = pkt;
2171  ts->stop_parse = 0;
2172  for(;;) {
2173  if (ts->stop_parse>0)
2174  break;
2175  if (len < TS_PACKET_SIZE)
2176  return -1;
2177  if (buf[0] != 0x47) {
2178  buf++;
2179  len--;
2180  } else {
2181  handle_packet(ts, buf);
2182  buf += TS_PACKET_SIZE;
2183  len -= TS_PACKET_SIZE;
2184  }
2185  }
2186  return len1 - len;
2187 }
2188 
2190 {
2191  mpegts_free(ts);
2192  av_free(ts);
2193 }
2194 
2195 AVInputFormat ff_mpegts_demuxer = {
2196  .name = "mpegts",
2197  .long_name = NULL_IF_CONFIG_SMALL("MPEG-TS (MPEG-2 Transport Stream)"),
2198  .priv_data_size = sizeof(MpegTSContext),
2203  .read_seek = read_seek,
2204  .read_timestamp = mpegts_get_pcr,
2206 };
2207 
2209  .name = "mpegtsraw",
2210  .long_name = NULL_IF_CONFIG_SMALL("raw MPEG-TS (MPEG-2 Transport Stream)"),
2211  .priv_data_size = sizeof(MpegTSContext),
2215  .read_seek = read_seek,
2216  .read_timestamp = mpegts_get_pcr,
2218  .priv_class = &mpegtsraw_class,
2219 };
static void mpegts_free(MpegTSContext *ts)
Definition: mpegts.c:2066
codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it ...
Definition: avcodec.h:452
uint8_t last_sec_num
Definition: mpegts.c:443
#define SDT_PID
Definition: mpegts.h:37
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
Bytestream IO Context.
Definition: avio.h:68
int es_id
Definition: mpegts.c:78
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
#define PES_START_SIZE
Definition: mpegts.c:158
#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:105
int size
#define MP4ESDescrTag
Definition: isom.h:164
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
int total_size
Definition: mpegts.c:174
static int mpegts_resync(AVFormatContext *s)
Definition: mpegts.c:1742
void ff_mp4_parse_es_descr(AVIOContext *pb, int *es_id)
Definition: isom.c:389
static void finished_reading_packet(AVFormatContext *s, int raw_packet_size)
Definition: mpegts.c:1786
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:275
AVOption.
Definition: opt.h:233
A dummy id pointing at the start of audio codecs.
Definition: avcodec.h:289
int64_t cur_pcr
used to estimate the exact PCR
Definition: mpegts.c:111
#define MAX_PACKET_READAHEAD
Definition: mpegts.c:1987
enum AVMediaType codec_type
Definition: mpegts.c:527
int64_t dts
Definition: mpegts.c:177
static int parse_MP4ODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1038
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:240
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
Definition: mpegts.c:1640
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1002
static void clear_program(MpegTSContext *ts, unsigned int programid)
Definition: mpegts.c:186
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:3208
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:199
static int mpegts_probe(AVProbeData *p)
Definition: mpegts.c:1839
#define MP4ODescrTag
Definition: isom.h:162
int index
stream index in AVFormatContext
Definition: avformat.h:684
int size
Definition: avcodec.h:974
MpegTSPESFilter pes_filter
Definition: mpegts.c:82
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
uint8_t version
Definition: mpegts.c:441
enum AVMediaType codec_type
Definition: rtp.c:36
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:665
AVInputFormat ff_mpegts_demuxer
Definition: mpegts.c:2195
static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1562
A dummy ID pointing at the start of various fake codecs.
Definition: avcodec.h:449
void * priv_data
Definition: avformat.h:703
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:667
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
#define M4OD_TID
Definition: mpegts.h:42
static int parse_section_header(SectionHeader *h, const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:496
discard all
Definition: avcodec.h:546
enum MpegTSFilterType type
Definition: mpegts.c:80
#define MAX_RESYNC_SIZE
Definition: mpegts.c:42
int pid
Definition: mpegts.c:163
struct Program * prg
Definition: mpegts.c:127
static int mpegts_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegts.c:2037
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:574
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:916
int pcr_pid
if -1 then all packets containing PCR are considered
Definition: mpegts.c:164
int id
Definition: avformat.h:842
SLConfigDescr sl
Definition: mpegts.h:90
int packet_seq_num_len
Definition: mpegts.h:83
int es_id
Definition: mpegts.h:87
int inst_bitrate_len
Definition: mpegts.h:80
unsigned int nb_prg
structure to keep track of Program->pids mapping
Definition: mpegts.c:126
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
#define AVFMT_SHOW_IDS
Show format stream IDs numbers.
Definition: avformat.h:402
int last_cc
Definition: mpegts.c:79
Format I/O context.
Definition: avformat.h:871
static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size, const uint8_t **data)
Definition: mpegts.c:1762
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:38
unsigned int pids[MAX_PIDS_PER_PROGRAM]
Definition: mpegts.c:91
Definition: mpegts.c:88
static void write_section_data(AVFormatContext *s, 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:280
Public dictionary API.
static int read_sl_header(PESContext *pes, SLConfigDescr *sl, const uint8_t *buf, int buf_size)
Definition: mpegts.c:686
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
MpegTSFilter * pids[NB_PID_MAX]
filters for various streams specified by PMT + for the PAT and PMT
Definition: mpegts.c:131
static int mpegts_read_header(AVFormatContext *s)
Definition: mpegts.c:1891
AVFormatContext * s
Definition: mpegts.c:976
uint8_t
Opaque data information usually continuous.
Definition: avutil.h:189
enum MpegTSState state
Definition: mpegts.c:170
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:850
AVOptions.
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:261
int au_seq_num_len
Definition: mpegts.h:82
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:589
int stop_parse
stop parsing loop
Definition: mpegts.c:116
static int parse_MP4ESDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1052
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:240
#define AV_RB32
Definition: intreadwrite.h:130
static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
Definition: mpegts.c:201
static int mpegts_push_data(MpegTSFilter *filter, const uint8_t *buf, int buf_size, int is_start, int64_t pos)
Definition: mpegts.c:752
int id
Format-specific stream ID.
Definition: avformat.h:690
enum AVStreamParseType need_parsing
Definition: avformat.h:816
int use_au_start
Definition: mpegts.h:70
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1162
const char * name
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:935
#define TS_PACKET_SIZE
Definition: mpegts.h:29
Mp4Descr * descr
Definition: mpegts.c:978
const char data[16]
Definition: mxf.c:66
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:493
static void update_offsets(AVIOContext *pb, int64_t *off, int *len)
Definition: mpegts.c:1007
uint8_t * data
Definition: avcodec.h:973
AVProgram * av_new_program(AVFormatContext *s, int id)
Definition: utils.c:2711
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
MpegTSState
Definition: mpegts.c:149
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:822
#define AVERROR_EOF
End of file.
Definition: error.h:51
bitstream reader API header.
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int pid
Definition: mpegts.c:77
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:844
static PESContext * add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
Definition: mpegts.c:950
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:995
#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:446
#define r
Definition: input.c:51
static MpegTSFilter * mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid, PESCallback *pes_cb, void *opaque)
Definition: mpegts.c:350
AVInputFormat ff_mpegtsraw_demuxer
Definition: mpegts.c:2208
static int init_MP4DescrParseContext(MP4DescrParseContext *d, AVFormatContext *s, const uint8_t *buf, unsigned size, Mp4Descr *descr, int max_descr_count)
Definition: mpegts.c:985
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:80
static uint64_t get_bits64(GetBitContext *s, int n)
Definition: get_bits.h:322
enum AVCodecID codec_id
Definition: mpegts.c:528
AVBufferRef * buffer
Definition: mpegts.c:180
int ff_mp4_read_dec_config_descr(AVFormatContext *fc, AVStream *st, AVIOContext *pb)
Definition: isom.c:414
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:167
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static const StreamType HDMV_types[]
Definition: mpegts.c:547
union MpegTSFilter::@112 u
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:409
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
static int mpegts_read_close(AVFormatContext *s)
Definition: mpegts.c:2076
unsigned int check_crc
Definition: mpegts.c:70
static int mpegts_raw_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: mpegts.c:1989
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:142
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
unsigned int nb_programs
Definition: avformat.h:1003
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:369
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
int pes_header_size
Definition: mpegts.c:175
void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2662
#define TS_FEC_PACKET_SIZE
Definition: mpegts.h:27
enum AVCodecID codec_id
Definition: mov_chan.c:432
New fields can be added to the end with minor version bumps.
Definition: avformat.h:841
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:77
static int parse_MP4DecConfigDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1070
int flags
copied to the AVPacket flags
Definition: mpegts.c:173
#define MAX_SECTION_SIZE
Definition: mpegts.h:33
int pcr_incr
used to estimate the exact PCR
Definition: mpegts.c:112
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
int off
Definition: dsputil_bfin.c:29
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:437
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:702
MpegTSContext * ff_mpegts_parse_open(AVFormatContext *s)
Definition: mpegts.c:2148
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:391
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:390
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
Definition: mpegaudioenc.c:307
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:923
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:3285
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:117
int bit_rate
the average bitrate
Definition: avcodec.h:1112
int64_t ts_packet_pos
position of first TS packet of this PES packet
Definition: mpegts.c:178
SectionCallback * section_cb
Definition: mpegts.c:72
static const StreamType REGD_types[]
Definition: mpegts.c:566
int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos)
Definition: mpegts.c:55
static const StreamType MISC_types[]
Definition: mpegts.c:560
uint16_t id
Definition: mpegts.c:440
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:1173
#define TS_DVHS_PACKET_SIZE
Definition: mpegts.h:28
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
AVStream * st
Definition: mpegts.c:168
#define MP4IODescrTag
Definition: isom.h:163
AVFormatContext * stream
Definition: mpegts.c:97
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
StreamType
Definition: avserver.c:187
int ocr_len
Definition: mpegts.h:78
static int handle_packets(MpegTSContext *ts, int nb_packets)
Definition: mpegts.c:1794
static int mpegts_set_stream_info(AVStream *st, PESContext *pes, uint32_t stream_type, uint32_t prog_reg_desc)
Definition: mpegts.c:600
#define AV_RL32
Definition: intreadwrite.h:146
AVDictionary * metadata
Definition: avformat.h:749
static int parse_mp4_descr(MP4DescrParseContext *d, int64_t off, int len, int target_tag)
Definition: mpegts.c:1116
#define CHECK_COUNT
static int parse_MP4SLDescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1083
int mpeg2ts_compute_pcr
compute exact PCR for each transport stream packet
Definition: mpegts.c:109
static const AVClass mpegtsraw_class
Definition: mpegts.c:140
MpegTSSectionFilter section_filter
Definition: mpegts.c:83
unsigned int probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:991
int ff_mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt, const uint8_t *buf, int len)
Definition: mpegts.c:2164
uint8_t sec_num
Definition: mpegts.c:442
int extended_stream_id
Definition: mpegts.c:176
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int stream_type
Definition: mpegts.c:165
int auto_guess
if true, all pids are analyzed to find streams
Definition: mpegts.c:106
int raw_packet_size
raw packet size, including FEC if present
Definition: mpegts.c:99
static const StreamType ISO_types[]
Definition: mpegts.c:531
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:536
if(ac->has_optimized_func)
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:666
Stream structure.
Definition: avformat.h:683
FAKE codec to indicate a MPEG-4 Systems stream (only used by libavformat)
Definition: avcodec.h:456
NULL
Definition: eval.c:55
#define TS_MAX_PACKET_SIZE
Definition: mpegts.h:30
uint8_t * dec_config_descr
Definition: mpegts.h:89
enum AVMediaType codec_type
Definition: avcodec.h:1062
unsigned int id
Definition: mpegts.c:89
#define MP4DecConfigDescrTag
Definition: isom.h:165
enum AVCodecID codec_id
Definition: avcodec.h:1065
static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Definition: mpegts.c:2122
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:65
#define hex_dump_debug(class, buf, size)
Definition: internal.h:32
AVIOContext * pb
I/O context.
Definition: avformat.h:913
av_default_item_name
Definition: dnxhdenc.c:45
int use_au_end
Definition: mpegts.h:71
static int parse_mp4_descr_arr(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1016
uint8_t * data
The data buffer.
Definition: buffer.h:89
static void new_pes_packet(PESContext *pes, AVPacket *pkt)
Definition: mpegts.c:652
static int get_packet_size(const uint8_t *buf, int size)
Definition: mpegts.c:419
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1080
#define STREAM_TYPE_PRIVATE_DATA
Definition: mpeg.h:53
static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit)
Definition: mpegts.c:2083
#define MAX_PES_HEADER_SIZE
Definition: mpegts.c:160
static int get8(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:446
int extradata_size
Definition: avcodec.h:1163
#define MAX_LEVEL
Definition: mpegts.c:974
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:62
#define MAX_PIDS_PER_PROGRAM
Definition: mpegts.c:87
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:271
static int parse_pcr(int64_t *ppcr_high, int *ppcr_low, const uint8_t *packet)
Definition: mpegts.c:1864
int use_timestamps
Definition: mpegts.h:74
Describe the class of an AVClass context structure.
Definition: log.h:33
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
int index
Definition: gxfenc.c:72
static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1374
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:264
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
AVMediaType
Definition: avutil.h:185
refcounted data buffer API
static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
Definition: mpegts.c:373
static const StreamType DESC_types[]
Definition: mpegts.c:579
This structure contains the data a format has to probe a file.
Definition: avformat.h:388
int use_idle
Definition: mpegts.h:75
SLConfigDescr sl
Definition: mpegts.c:181
int dec_config_descr_len
Definition: mpegts.h:88
uint8_t * section_buf
Definition: mpegts.c:69
uint8_t tid
Definition: mpegts.c:439
AVDictionary * metadata
Definition: avformat.h:847
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
unsigned int end_of_section_reached
Definition: mpegts.c:71
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:1250
static void clear_programs(MpegTSContext *ts)
Definition: mpegts.c:195
#define MOD_UNLIKELY(modulus, dividend, divisor, prev_dividend)
Definition: mathops.h:199
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:395
A reference to a data buffer.
Definition: buffer.h:81
static const AVOption options[]
Definition: mpegts.c:134
full parsing and repack
Definition: avformat.h:637
AVFormatContext * stream
Definition: mpegts.c:167
Main libavformat public API header.
static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1519
int ff_mp4_read_descr(AVFormatContext *fc, AVIOContext *pb, int *tag)
Definition: isom.c:380
void ff_mpegts_parse_close(MpegTSContext *ts)
Definition: mpegts.c:2189
int use_rand_acc_pt
Definition: mpegts.h:72
int data_index
Definition: mpegts.c:172
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:70
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:727
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:738
int64_t pos
position corresponding to pos47, or 0 if pos47 invalid
Definition: mpegts.c:103
uint8_t header[MAX_PES_HEADER_SIZE]
Definition: mpegts.c:179
#define MAX_PES_PAYLOAD
Definition: mpegts.c:44
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1024
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:883
PESCallback * pes_cb
Definition: mpegts.c:58
static MpegTSFilter * mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid, SectionCallback *section_cb, void *opaque, int check_crc)
Definition: mpegts.c:318
FAKE codec to indicate a raw MPEG-2 TS stream (only used by libavformat)
Definition: avcodec.h:454
unsigned int nb_pids
Definition: mpegts.c:90
void * opaque
Definition: mpegts.c:59
static int get16(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:459
int eof_reached
true if eof reached
Definition: avio.h:96
Mp4Descr * active_descr
Definition: mpegts.c:979
AVPacket * pkt
packet containing Audio/Video data
Definition: mpegts.c:118
int len
#define PAT_PID
Definition: mpegts.h:36
void * priv_data
Format private data.
Definition: avformat.h:899
int64_t last_pos
to detect seek
Definition: mpegts.c:120
int timestamp_res
Definition: mpegts.h:76
static int analyze(const uint8_t *buf, int size, int packet_size, int *index)
Definition: mpegts.c:394
#define PES_HEADER_SIZE
Definition: mpegts.c:159
static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
Definition: mpegts.c:214
int64_t pts
Definition: mpegts.c:177
int use_padding
Definition: mpegts.h:73
void SetServiceCallback(void *opaque, int ret)
Definition: mpegts.c:64
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:969
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:516
int degr_prior_len
Definition: mpegts.h:81
static int parse_MP4IODescrTag(MP4DescrParseContext *d, int64_t off, int len)
Definition: mpegts.c:1026
static void m4sl_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
Definition: mpegts.c:1186
AVIOContext pb
Definition: mpegts.c:977
int stream_index
Definition: avcodec.h:975
MpegTSFilterType
Definition: mpegts.c:48
#define MAX_MP4_DESCR_COUNT
Definition: mpegts.c:46
#define MKTAG(a, b, c, d)
Definition: common.h:238
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:740
static void mpegts_find_stream_type(AVStream *st, uint32_t stream_type, const StreamType *types)
Definition: mpegts.c:588
int au_len
Definition: mpegts.h:79
This structure stores compressed data.
Definition: avcodec.h:950
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
uint32_t stream_type
Definition: mpegts.c:526
static char * getstr8(const uint8_t **pp, const uint8_t *p_end)
Definition: mpegts.c:474
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
A dummy ID pointing at the start of subtitle codecs.
Definition: avcodec.h:437
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and AVInputFormat.read_timestamp().
Definition: utils.c:1359
AVProgram ** programs
Definition: avformat.h:1004
#define MP4SLDescrTag
Definition: isom.h:167
#define NB_PID_MAX
Definition: mpegts.h:32
#define SDT_TID
Definition: mpegts.h:43
void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len)
Definition: mpegts.c:62
AVStream * sub_st
stream for the embedded AC3 stream in HDMV TrueHD
Definition: mpegts.c:169
MpegTSContext * ts
Definition: mpegts.c:166
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:1160