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