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