move code to avoid forward declaration
[libav.git] / libavformat / mpegts.c
CommitLineData
fe9cf0d4 1/*
7fbde343 2 * MPEG2 transport stream (aka DVB) demuxer
406792e7 3 * Copyright (c) 2002-2003 Fabrice Bellard
fe9cf0d4 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
fe9cf0d4 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
fe9cf0d4 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
fe9cf0d4 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
fe9cf0d4 20 */
245976da 21
e9859317
BC
22//#define DEBUG
23//#define DEBUG_SEEK
af8aae3f 24//#define USE_SYNCPOINT_SEARCH
e9859317 25
245976da 26#include "libavutil/crc.h"
6a5d31ac 27#include "libavutil/intreadwrite.h"
4f1db48e 28#include "libavcodec/bytestream.h"
fe9cf0d4 29#include "avformat.h"
5dbafeb7 30#include "mpegts.h"
ecfb51d1 31#include "internal.h"
a8dd8dc6 32#include "seek.h"
fe9cf0d4 33
5dbafeb7 34/* 1.0 second at 24Mbit/s */
fc48fe84
FB
35#define MAX_SCAN_PACKETS 32000
36
37/* maximum size in which we look for synchronisation if
38 synchronisation is lost */
1303d62d 39#define MAX_RESYNC_SIZE 65536
5dbafeb7 40
0c137557
BC
41#define MAX_PES_PAYLOAD 200*1024
42
c6ec28b1
MN
43typedef struct PESContext PESContext;
44
5dbafeb7
FB
45enum MpegTSFilterType {
46 MPEGTS_PES,
47 MPEGTS_SECTION,
fe9cf0d4
FB
48};
49
b474d1f3
MR
50typedef struct MpegTSFilter MpegTSFilter;
51
bdfa9824 52typedef int PESCallback(MpegTSFilter *f, const uint8_t *buf, int len, int is_start, int64_t pos);
fe9cf0d4 53
5dbafeb7
FB
54typedef struct MpegTSPESFilter {
55 PESCallback *pes_cb;
56 void *opaque;
57} MpegTSPESFilter;
58
b474d1f3 59typedef void SectionCallback(MpegTSFilter *f, const uint8_t *buf, int len);
5dbafeb7
FB
60
61typedef void SetServiceCallback(void *opaque, int ret);
62
63typedef struct MpegTSSectionFilter {
64 int section_index;
65 int section_h_size;
66 uint8_t *section_buf;
72e043dd
EH
67 unsigned int check_crc:1;
68 unsigned int end_of_section_reached:1;
5dbafeb7
FB
69 SectionCallback *section_cb;
70 void *opaque;
71} MpegTSSectionFilter;
72
b474d1f3 73struct MpegTSFilter {
fe9cf0d4 74 int pid;
fe9cf0d4 75 int last_cc; /* last cc code (-1 if first packet) */
5dbafeb7
FB
76 enum MpegTSFilterType type;
77 union {
78 MpegTSPESFilter pes_filter;
79 MpegTSSectionFilter section_filter;
80 } u;
b474d1f3 81};
5dbafeb7 82
172d1171 83#define MAX_PIDS_PER_PROGRAM 64
8c39a758 84struct Program {
172d1171
NS
85 unsigned int id; //program id/service id
86 unsigned int nb_pids;
87 unsigned int pids[MAX_PIDS_PER_PROGRAM];
8c39a758 88};
172d1171 89
b45a7a18 90struct MpegTSContext {
5dbafeb7
FB
91 /* user data */
92 AVFormatContext *stream;
e995cfca
DK
93 /** raw packet size, including FEC if present */
94 int raw_packet_size;
5fd63f3c
MN
95
96 int pos47;
97
e995cfca
DK
98 /** if true, all pids are analyzed to find streams */
99 int auto_guess;
5dbafeb7 100
e995cfca
DK
101 /** compute exact PCR for each transport stream packet */
102 int mpeg2ts_compute_pcr;
b45a7a18 103
e995cfca
DK
104 int64_t cur_pcr; /**< used to estimate the exact PCR */
105 int pcr_incr; /**< used to estimate the exact PCR */
115329f1 106
5dbafeb7 107 /* data needed to handle file based ts */
e995cfca
DK
108 /** stop parsing loop */
109 int stop_parse;
110 /** packet containing Audio/Video data */
111 AVPacket *pkt;
0c137557
BC
112 /** to detect seek */
113 int64_t last_pos;
5dbafeb7
FB
114
115 /******************************************/
116 /* private mpegts data */
117 /* scan context */
172d1171
NS
118 /** structure to keep track of Program->pids mapping */
119 unsigned int nb_prg;
8c39a758 120 struct Program *prg;
115329f1 121
5dbafeb7 122
e995cfca 123 /** filters for various streams specified by PMT + for the PAT and PMT */
5dbafeb7 124 MpegTSFilter *pids[NB_PID_MAX];
b45a7a18 125};
fe9cf0d4 126
ecc31d1d
MN
127/* TS stream handling */
128
129enum MpegTSState {
130 MPEGTS_HEADER = 0,
b2984add 131 MPEGTS_PESHEADER,
ecc31d1d
MN
132 MPEGTS_PESHEADER_FILL,
133 MPEGTS_PAYLOAD,
134 MPEGTS_SKIP,
135};
136
137/* enough for PES header + length */
b2984add
BC
138#define PES_START_SIZE 6
139#define PES_HEADER_SIZE 9
ecc31d1d
MN
140#define MAX_PES_HEADER_SIZE (9 + 255)
141
142struct PESContext {
143 int pid;
dbf6b678 144 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
ecc31d1d
MN
145 int stream_type;
146 MpegTSContext *ts;
147 AVFormatContext *stream;
148 AVStream *st;
63380b5e 149 AVStream *sub_st; /**< stream for the embedded AC3 stream in HDMV TrueHD */
ecc31d1d
MN
150 enum MpegTSState state;
151 /* used to get the format */
152 int data_index;
153 int total_size;
154 int pes_header_size;
63380b5e 155 int extended_stream_id;
ecc31d1d 156 int64_t pts, dts;
4c6b49bf 157 int64_t ts_packet_pos; /**< position of first TS packet of this PES packet */
ecc31d1d 158 uint8_t header[MAX_PES_HEADER_SIZE];
0c137557 159 uint8_t *buffer;
ecc31d1d
MN
160};
161
6369638b
AJ
162extern AVInputFormat mpegts_demuxer;
163
172d1171
NS
164static void clear_program(MpegTSContext *ts, unsigned int programid)
165{
166 int i;
167
168 for(i=0; i<ts->nb_prg; i++)
169 if(ts->prg[i].id == programid)
170 ts->prg[i].nb_pids = 0;
171}
172
173static void clear_programs(MpegTSContext *ts)
174{
175 av_freep(&ts->prg);
176 ts->nb_prg=0;
177}
178
179static void add_pat_entry(MpegTSContext *ts, unsigned int programid)
180{
8c39a758
MR
181 struct Program *p;
182 void *tmp = av_realloc(ts->prg, (ts->nb_prg+1)*sizeof(struct Program));
172d1171
NS
183 if(!tmp)
184 return;
185 ts->prg = tmp;
186 p = &ts->prg[ts->nb_prg];
187 p->id = programid;
188 p->nb_pids = 0;
189 ts->nb_prg++;
190}
191
192static void add_pid_to_pmt(MpegTSContext *ts, unsigned int programid, unsigned int pid)
193{
194 int i;
8c39a758 195 struct Program *p = NULL;
172d1171
NS
196 for(i=0; i<ts->nb_prg; i++) {
197 if(ts->prg[i].id == programid) {
198 p = &ts->prg[i];
199 break;
200 }
201 }
202 if(!p)
203 return;
204
205 if(p->nb_pids >= MAX_PIDS_PER_PROGRAM)
206 return;
207 p->pids[p->nb_pids++] = pid;
208}
209
210/**
211 * \brief discard_pid() decides if the pid is to be discarded according
212 * to caller's programs selection
213 * \param ts : - TS context
214 * \param pid : - pid
215 * \return 1 if the pid is only comprised in programs that have .discard=AVDISCARD_ALL
216 * 0 otherwise
217 */
218static int discard_pid(MpegTSContext *ts, unsigned int pid)
219{
220 int i, j, k;
221 int used = 0, discarded = 0;
8c39a758 222 struct Program *p;
172d1171
NS
223 for(i=0; i<ts->nb_prg; i++) {
224 p = &ts->prg[i];
225 for(j=0; j<p->nb_pids; j++) {
226 if(p->pids[j] != pid)
227 continue;
228 //is program with id p->id set to be discarded?
229 for(k=0; k<ts->stream->nb_programs; k++) {
230 if(ts->stream->programs[k]->id == p->id) {
231 if(ts->stream->programs[k]->discard == AVDISCARD_ALL)
232 discarded++;
233 else
234 used++;
235 }
236 }
237 }
238 }
239
ccd425e7 240 return !used && discarded;
172d1171
NS
241}
242
e995cfca
DK
243/**
244 * Assembles PES packets out of TS packets, and then calls the "section_cb"
245 * function when they are complete.
246 */
5dbafeb7
FB
247static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
248 const uint8_t *buf, int buf_size, int is_start)
249{
250 MpegTSSectionFilter *tss = &tss1->u.section_filter;
251 int len;
115329f1 252
5dbafeb7
FB
253 if (is_start) {
254 memcpy(tss->section_buf, buf, buf_size);
255 tss->section_index = buf_size;
256 tss->section_h_size = -1;
257 tss->end_of_section_reached = 0;
258 } else {
259 if (tss->end_of_section_reached)
260 return;
261 len = 4096 - tss->section_index;
262 if (buf_size < len)
263 len = buf_size;
264 memcpy(tss->section_buf + tss->section_index, buf, len);
265 tss->section_index += len;
266 }
267
268 /* compute section length if possible */
269 if (tss->section_h_size == -1 && tss->section_index >= 3) {
80fb8234 270 len = (AV_RB16(tss->section_buf + 1) & 0xfff) + 3;
5dbafeb7
FB
271 if (len > 4096)
272 return;
273 tss->section_h_size = len;
274 }
275
276 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
5dbafeb7 277 tss->end_of_section_reached = 1;
27bb1ed3 278 if (!tss->check_crc ||
3abe5fbd
AJ
279 av_crc(av_crc_get_table(AV_CRC_32_IEEE), -1,
280 tss->section_buf, tss->section_h_size) == 0)
4d9a577e 281 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
5dbafeb7
FB
282 }
283}
284
7b49ce2e 285static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
5dbafeb7
FB
286 SectionCallback *section_cb, void *opaque,
287 int check_crc)
288
289{
290 MpegTSFilter *filter;
291 MpegTSSectionFilter *sec;
115329f1 292
ebcf352a
BC
293 dprintf(ts->stream, "Filter: pid=0x%x\n", pid);
294
5dbafeb7
FB
295 if (pid >= NB_PID_MAX || ts->pids[pid])
296 return NULL;
297 filter = av_mallocz(sizeof(MpegTSFilter));
115329f1 298 if (!filter)
5dbafeb7
FB
299 return NULL;
300 ts->pids[pid] = filter;
301 filter->type = MPEGTS_SECTION;
302 filter->pid = pid;
303 filter->last_cc = -1;
304 sec = &filter->u.section_filter;
305 sec->section_cb = section_cb;
306 sec->opaque = opaque;
307 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
308 sec->check_crc = check_crc;
309 if (!sec->section_buf) {
310 av_free(filter);
311 return NULL;
312 }
313 return filter;
314}
315
7b49ce2e 316static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
5dbafeb7
FB
317 PESCallback *pes_cb,
318 void *opaque)
319{
320 MpegTSFilter *filter;
321 MpegTSPESFilter *pes;
322
323 if (pid >= NB_PID_MAX || ts->pids[pid])
324 return NULL;
325 filter = av_mallocz(sizeof(MpegTSFilter));
115329f1 326 if (!filter)
5dbafeb7
FB
327 return NULL;
328 ts->pids[pid] = filter;
329 filter->type = MPEGTS_PES;
330 filter->pid = pid;
331 filter->last_cc = -1;
332 pes = &filter->u.pes_filter;
333 pes->pes_cb = pes_cb;
334 pes->opaque = opaque;
335 return filter;
336}
337
7b49ce2e 338static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
5dbafeb7
FB
339{
340 int pid;
341
342 pid = filter->pid;
343 if (filter->type == MPEGTS_SECTION)
344 av_freep(&filter->u.section_filter.section_buf);
dcd913d9 345 else if (filter->type == MPEGTS_PES) {
0c137557
BC
346 PESContext *pes = filter->u.pes_filter.opaque;
347 av_freep(&pes->buffer);
dcd913d9
JM
348 /* referenced private data will be freed later in
349 * av_close_input_stream */
350 if (!((PESContext *)filter->u.pes_filter.opaque)->st) {
351 av_freep(&filter->u.pes_filter.opaque);
352 }
353 }
ec7d0d2e 354
5dbafeb7
FB
355 av_free(filter);
356 ts->pids[pid] = NULL;
357}
358
a0b8f70c 359static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
cf5ec607 360 int stat[TS_MAX_PACKET_SIZE];
a0b8f70c
MN
361 int i;
362 int x=0;
363 int best_score=0;
364
365 memset(stat, 0, packet_size*sizeof(int));
366
c6002ea1
MR
367 for(x=i=0; i<size-3; i++){
368 if(buf[i] == 0x47 && !(buf[i+1] & 0x80) && (buf[i+3] & 0x30)){
a0b8f70c
MN
369 stat[x]++;
370 if(stat[x] > best_score){
371 best_score= stat[x];
372 if(index) *index= x;
373 }
374 }
375
376 x++;
377 if(x == packet_size) x= 0;
378 }
379
380 return best_score;
381}
382
fe9cf0d4 383/* autodetect fec presence. Must have at least 1024 bytes */
5dbafeb7 384static int get_packet_size(const uint8_t *buf, int size)
fe9cf0d4 385{
5d4d67e0 386 int score, fec_score, dvhs_score;
fe9cf0d4
FB
387
388 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
389 return -1;
115329f1 390
a0b8f70c 391 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
5d4d67e0 392 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
a0b8f70c 393 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
5d4d67e0 394// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
115329f1 395
5d4d67e0
NS
396 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
397 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
398 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
a0b8f70c 399 else return -1;
fe9cf0d4
FB
400}
401
5dbafeb7
FB
402typedef struct SectionHeader {
403 uint8_t tid;
404 uint16_t id;
405 uint8_t version;
406 uint8_t sec_num;
407 uint8_t last_sec_num;
408} SectionHeader;
409
410static inline int get8(const uint8_t **pp, const uint8_t *p_end)
fe9cf0d4 411{
5dbafeb7
FB
412 const uint8_t *p;
413 int c;
414
415 p = *pp;
416 if (p >= p_end)
417 return -1;
418 c = *p++;
419 *pp = p;
420 return c;
fe9cf0d4
FB
421}
422
5dbafeb7
FB
423static inline int get16(const uint8_t **pp, const uint8_t *p_end)
424{
425 const uint8_t *p;
426 int c;
427
428 p = *pp;
429 if ((p + 1) >= p_end)
430 return -1;
80fb8234 431 c = AV_RB16(p);
5dbafeb7
FB
432 p += 2;
433 *pp = p;
434 return c;
435}
436
437/* read and allocate a DVB string preceeded by its length */
438static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
fe9cf0d4 439{
fe9cf0d4 440 int len;
5dbafeb7
FB
441 const uint8_t *p;
442 char *str;
fe9cf0d4 443
5dbafeb7
FB
444 p = *pp;
445 len = get8(&p, p_end);
446 if (len < 0)
447 return NULL;
448 if ((p + len) > p_end)
449 return NULL;
450 str = av_malloc(len + 1);
451 if (!str)
452 return NULL;
453 memcpy(str, p, len);
454 str[len] = '\0';
455 p += len;
456 *pp = p;
457 return str;
458}
459
115329f1 460static int parse_section_header(SectionHeader *h,
5dbafeb7
FB
461 const uint8_t **pp, const uint8_t *p_end)
462{
463 int val;
464
465 val = get8(pp, p_end);
466 if (val < 0)
467 return -1;
468 h->tid = val;
469 *pp += 2;
470 val = get16(pp, p_end);
471 if (val < 0)
472 return -1;
473 h->id = val;
474 val = get8(pp, p_end);
475 if (val < 0)
476 return -1;
477 h->version = (val >> 1) & 0x1f;
478 val = get8(pp, p_end);
479 if (val < 0)
480 return -1;
481 h->sec_num = val;
482 val = get8(pp, p_end);
483 if (val < 0)
484 return -1;
485 h->last_sec_num = val;
fe9cf0d4 486 return 0;
5dbafeb7
FB
487}
488
08f94e98
BC
489typedef struct {
490 uint32_t stream_type;
491 enum CodecType codec_type;
492 enum CodecID codec_id;
493} StreamType;
494
495static const StreamType ISO_types[] = {
496 { 0x01, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
497 { 0x02, CODEC_TYPE_VIDEO, CODEC_ID_MPEG2VIDEO },
498 { 0x03, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },
499 { 0x04, CODEC_TYPE_AUDIO, CODEC_ID_MP3 },
500 { 0x0f, CODEC_TYPE_AUDIO, CODEC_ID_AAC },
501 { 0x10, CODEC_TYPE_VIDEO, CODEC_ID_MPEG4 },
28f008a9 502 { 0x11, CODEC_TYPE_AUDIO, CODEC_ID_AAC }, /* LATM syntax */
08f94e98
BC
503 { 0x1b, CODEC_TYPE_VIDEO, CODEC_ID_H264 },
504 { 0xd1, CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
505 { 0xea, CODEC_TYPE_VIDEO, CODEC_ID_VC1 },
506 { 0 },
507};
508
509static const StreamType HDMV_types[] = {
5dd3707b 510 { 0x80, CODEC_TYPE_AUDIO, CODEC_ID_PCM_BLURAY },
08f94e98
BC
511 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
512 { 0x82, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
63380b5e 513 { 0x83, CODEC_TYPE_AUDIO, CODEC_ID_TRUEHD },
0096d766 514 { 0x84, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 },
c58b82a2 515 { 0x90, CODEC_TYPE_SUBTITLE, CODEC_ID_HDMV_PGS_SUBTITLE },
08f94e98
BC
516 { 0 },
517};
518
519/* ATSC ? */
520static const StreamType MISC_types[] = {
521 { 0x81, CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
522 { 0x8a, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
08f94e98
BC
523 { 0 },
524};
525
526static const StreamType REGD_types[] = {
527 { MKTAG('d','r','a','c'), CODEC_TYPE_VIDEO, CODEC_ID_DIRAC },
528 { MKTAG('A','C','-','3'), CODEC_TYPE_AUDIO, CODEC_ID_AC3 },
529 { 0 },
530};
531
6a2a50f8
BC
532/* descriptor present */
533static const StreamType DESC_types[] = {
534 { 0x6a, CODEC_TYPE_AUDIO, CODEC_ID_AC3 }, /* AC-3 descriptor */
84c7d45e 535 { 0x7a, CODEC_TYPE_AUDIO, CODEC_ID_EAC3 }, /* E-AC-3 descriptor */
6a2a50f8 536 { 0x7b, CODEC_TYPE_AUDIO, CODEC_ID_DTS },
e58992e4 537 { 0x56, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_TELETEXT },
6a2a50f8 538 { 0x59, CODEC_TYPE_SUBTITLE, CODEC_ID_DVB_SUBTITLE }, /* subtitling descriptor */
896f2b32 539 { 0 },
6a2a50f8
BC
540};
541
08f94e98
BC
542static void mpegts_find_stream_type(AVStream *st,
543 uint32_t stream_type, const StreamType *types)
544{
545 for (; types->stream_type; types++) {
546 if (stream_type == types->stream_type) {
547 st->codec->codec_type = types->codec_type;
548 st->codec->codec_id = types->codec_id;
549 return;
550 }
551 }
552}
553
b3f9f7a3
BC
554static int mpegts_set_stream_info(AVStream *st, PESContext *pes,
555 uint32_t stream_type, uint32_t prog_reg_desc)
08f94e98 556{
08f94e98
BC
557 av_set_pts_info(st, 33, 1, 90000);
558 st->priv_data = pes;
559 st->codec->codec_type = CODEC_TYPE_DATA;
d23612ef 560 st->codec->codec_id = CODEC_ID_NONE;
08f94e98
BC
561 st->need_parsing = AVSTREAM_PARSE_FULL;
562 pes->st = st;
b3f9f7a3 563 pes->stream_type = stream_type;
08f94e98 564
b3f9f7a3
BC
565 av_log(pes->stream, AV_LOG_DEBUG,
566 "stream=%d stream_type=%x pid=%x prog_reg_desc=%.4s\n",
567 st->index, pes->stream_type, pes->pid, (char*)&prog_reg_desc);
08f94e98 568
7103a77b
BC
569 st->codec->codec_tag = pes->stream_type;
570
f2c357d9
BC
571 mpegts_find_stream_type(st, pes->stream_type, ISO_types);
572 if (prog_reg_desc == AV_RL32("HDMV") &&
63380b5e 573 st->codec->codec_id == CODEC_ID_NONE) {
f2c357d9 574 mpegts_find_stream_type(st, pes->stream_type, HDMV_types);
63380b5e
IC
575 if (pes->stream_type == 0x83) {
576 // HDMV TrueHD streams also contain an AC3 coded version of the
577 // audio track - add a second stream for this
578 AVStream *sub_st;
579 // priv_data cannot be shared between streams
580 PESContext *sub_pes = av_malloc(sizeof(*sub_pes));
581 if (!sub_pes)
b3f9f7a3 582 return AVERROR(ENOMEM);
63380b5e
IC
583 memcpy(sub_pes, pes, sizeof(*sub_pes));
584
585 sub_st = av_new_stream(pes->stream, pes->pid);
586 if (!sub_st) {
587 av_free(sub_pes);
b3f9f7a3 588 return AVERROR(ENOMEM);
63380b5e
IC
589 }
590
591 av_set_pts_info(sub_st, 33, 1, 90000);
592 sub_st->priv_data = sub_pes;
593 sub_st->codec->codec_type = CODEC_TYPE_AUDIO;
594 sub_st->codec->codec_id = CODEC_ID_AC3;
595 sub_st->need_parsing = AVSTREAM_PARSE_FULL;
596 sub_pes->sub_st = pes->sub_st = sub_st;
597 }
598 }
d23612ef 599 if (st->codec->codec_id == CODEC_ID_NONE)
f2c357d9 600 mpegts_find_stream_type(st, pes->stream_type, MISC_types);
08f94e98 601
b3f9f7a3 602 return 0;
08f94e98 603}
5dbafeb7 604
609d8646
BC
605static int64_t get_pts(const uint8_t *p)
606{
607 int64_t pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
608 pts |= (AV_RB16(p + 1) >> 1) << 15;
609 pts |= AV_RB16(p + 3) >> 1;
610 return pts;
611}
612
613static void new_pes_packet(PESContext *pes, AVPacket *pkt)
614{
615 av_init_packet(pkt);
616
617 pkt->destruct = av_destruct_packet;
618 pkt->data = pes->buffer;
619 pkt->size = pes->data_index;
620 memset(pkt->data+pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
621
622 // Separate out the AC3 substream from an HDMV combined TrueHD/AC3 PID
623 if (pes->sub_st && pes->stream_type == 0x83 && pes->extended_stream_id == 0x76)
624 pkt->stream_index = pes->sub_st->index;
625 else
626 pkt->stream_index = pes->st->index;
627 pkt->pts = pes->pts;
628 pkt->dts = pes->dts;
629 /* store position of first TS packet of this PES packet */
630 pkt->pos = pes->ts_packet_pos;
631
632 /* reset pts values */
633 pes->pts = AV_NOPTS_VALUE;
634 pes->dts = AV_NOPTS_VALUE;
635 pes->buffer = NULL;
636 pes->data_index = 0;
637}
638
639/* return non zero if a packet could be constructed */
640static int mpegts_push_data(MpegTSFilter *filter,
641 const uint8_t *buf, int buf_size, int is_start,
642 int64_t pos)
643{
644 PESContext *pes = filter->u.pes_filter.opaque;
645 MpegTSContext *ts = pes->ts;
646 const uint8_t *p;
647 int len, code;
648
649 if(!ts->pkt)
650 return 0;
651
652 if (is_start) {
653 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
654 new_pes_packet(pes, ts->pkt);
655 ts->stop_parse = 1;
656 }
657 pes->state = MPEGTS_HEADER;
658 pes->data_index = 0;
659 pes->ts_packet_pos = pos;
660 }
661 p = buf;
662 while (buf_size > 0) {
663 switch(pes->state) {
664 case MPEGTS_HEADER:
665 len = PES_START_SIZE - pes->data_index;
666 if (len > buf_size)
667 len = buf_size;
668 memcpy(pes->header + pes->data_index, p, len);
669 pes->data_index += len;
670 p += len;
671 buf_size -= len;
672 if (pes->data_index == PES_START_SIZE) {
673 /* we got all the PES or section header. We can now
674 decide */
675#if 0
676 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
677#endif
678 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
679 pes->header[2] == 0x01) {
680 /* it must be an mpeg2 PES stream */
681 code = pes->header[3] | 0x100;
682 dprintf(pes->stream, "pid=%x pes_code=%#x\n", pes->pid, code);
683
684 if ((!pes->st && pes->stream->nb_streams == MAX_STREAMS) ||
685 (pes->st && pes->st->discard == AVDISCARD_ALL) ||
686 code == 0x1be) /* padding_stream */
687 goto skip;
688
689 /* stream not present in PMT */
690 if (!pes->st) {
691 pes->st = av_new_stream(ts->stream, pes->pid);
692 if (!pes->st)
693 return AVERROR(ENOMEM);
694 mpegts_set_stream_info(pes->st, pes, 0, 0);
695 }
696
697 pes->total_size = AV_RB16(pes->header + 4);
698 /* NOTE: a zero total size means the PES size is
699 unbounded */
700 if (!pes->total_size)
701 pes->total_size = MAX_PES_PAYLOAD;
702
703 /* allocate pes buffer */
704 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
705 if (!pes->buffer)
706 return AVERROR(ENOMEM);
707
708 if (code != 0x1bc && code != 0x1bf && /* program_stream_map, private_stream_2 */
709 code != 0x1f0 && code != 0x1f1 && /* ECM, EMM */
710 code != 0x1ff && code != 0x1f2 && /* program_stream_directory, DSMCC_stream */
711 code != 0x1f8) { /* ITU-T Rec. H.222.1 type E stream */
712 pes->state = MPEGTS_PESHEADER;
713 if (pes->st->codec->codec_id == CODEC_ID_NONE) {
714 dprintf(pes->stream, "pid=%x stream_type=%x probing\n",
715 pes->pid, pes->stream_type);
716 pes->st->codec->codec_id = CODEC_ID_PROBE;
717 }
718 } else {
719 pes->state = MPEGTS_PAYLOAD;
720 pes->data_index = 0;
721 }
722 } else {
723 /* otherwise, it should be a table */
724 /* skip packet */
725 skip:
726 pes->state = MPEGTS_SKIP;
727 continue;
728 }
729 }
730 break;
731 /**********************************************/
732 /* PES packing parsing */
733 case MPEGTS_PESHEADER:
734 len = PES_HEADER_SIZE - pes->data_index;
735 if (len < 0)
736 return -1;
737 if (len > buf_size)
738 len = buf_size;
739 memcpy(pes->header + pes->data_index, p, len);
740 pes->data_index += len;
741 p += len;
742 buf_size -= len;
743 if (pes->data_index == PES_HEADER_SIZE) {
744 pes->pes_header_size = pes->header[8] + 9;
745 pes->state = MPEGTS_PESHEADER_FILL;
746 }
747 break;
748 case MPEGTS_PESHEADER_FILL:
749 len = pes->pes_header_size - pes->data_index;
750 if (len < 0)
751 return -1;
752 if (len > buf_size)
753 len = buf_size;
754 memcpy(pes->header + pes->data_index, p, len);
755 pes->data_index += len;
756 p += len;
757 buf_size -= len;
758 if (pes->data_index == pes->pes_header_size) {
759 const uint8_t *r;
760 unsigned int flags, pes_ext, skip;
761
762 flags = pes->header[7];
763 r = pes->header + 9;
764 pes->pts = AV_NOPTS_VALUE;
765 pes->dts = AV_NOPTS_VALUE;
766 if ((flags & 0xc0) == 0x80) {
767 pes->dts = pes->pts = get_pts(r);
768 r += 5;
769 } else if ((flags & 0xc0) == 0xc0) {
770 pes->pts = get_pts(r);
771 r += 5;
772 pes->dts = get_pts(r);
773 r += 5;
774 }
775 pes->extended_stream_id = -1;
776 if (flags & 0x01) { /* PES extension */
777 pes_ext = *r++;
778 /* Skip PES private data, program packet sequence counter and P-STD buffer */
779 skip = (pes_ext >> 4) & 0xb;
780 skip += skip & 0x9;
781 r += skip;
782 if ((pes_ext & 0x41) == 0x01 &&
783 (r + 2) <= (pes->header + pes->pes_header_size)) {
784 /* PES extension 2 */
785 if ((r[0] & 0x7f) > 0 && (r[1] & 0x80) == 0)
786 pes->extended_stream_id = r[1];
787 }
788 }
789
790 /* we got the full header. We parse it and get the payload */
791 pes->state = MPEGTS_PAYLOAD;
792 pes->data_index = 0;
793 }
794 break;
795 case MPEGTS_PAYLOAD:
796 if (buf_size > 0 && pes->buffer) {
797 if (pes->data_index+buf_size > pes->total_size) {
798 new_pes_packet(pes, ts->pkt);
799 pes->total_size = MAX_PES_PAYLOAD;
800 pes->buffer = av_malloc(pes->total_size+FF_INPUT_BUFFER_PADDING_SIZE);
801 if (!pes->buffer)
802 return AVERROR(ENOMEM);
803 ts->stop_parse = 1;
804 }
805 memcpy(pes->buffer+pes->data_index, p, buf_size);
806 pes->data_index += buf_size;
807 }
808 buf_size = 0;
809 break;
810 case MPEGTS_SKIP:
811 buf_size = 0;
812 break;
813 }
814 }
815
816 return 0;
817}
818
819static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid)
820{
821 MpegTSFilter *tss;
822 PESContext *pes;
823
824 /* if no pid found, then add a pid context */
825 pes = av_mallocz(sizeof(PESContext));
826 if (!pes)
827 return 0;
828 pes->ts = ts;
829 pes->stream = ts->stream;
830 pes->pid = pid;
831 pes->pcr_pid = pcr_pid;
832 pes->state = MPEGTS_SKIP;
833 pes->pts = AV_NOPTS_VALUE;
834 pes->dts = AV_NOPTS_VALUE;
835 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
836 if (!tss) {
837 av_free(pes);
838 return 0;
839 }
840 return pes;
841}
842
4d9a577e 843static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 844{
4d9a577e 845 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7 846 SectionHeader h1, *h = &h1;
c6ec28b1
MN
847 PESContext *pes;
848 AVStream *st;
849 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
850 int program_info_length, pcr_pid, pid, stream_type;
851 int desc_list_len, desc_len, desc_tag;
ed68efad
BC
852 int comp_page, anc_page;
853 char language[4];
8430f3ec 854 uint32_t prog_reg_desc = 0; /* registration descriptor */
115329f1 855
ebcf352a
BC
856#ifdef DEBUG
857 dprintf(ts->stream, "PMT: len %i\n", section_len);
750f0e1f 858 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7 859#endif
ebcf352a 860
5dbafeb7
FB
861 p_end = section + section_len - 4;
862 p = section;
863 if (parse_section_header(h, &p, p_end) < 0)
864 return;
ebcf352a
BC
865
866 dprintf(ts->stream, "sid=0x%x sec_num=%d/%d\n",
2ddb79be 867 h->id, h->sec_num, h->last_sec_num);
ebcf352a 868
90d13e30 869 if (h->tid != PMT_TID)
5dbafeb7
FB
870 return;
871
172d1171 872 clear_program(ts, h->id);
5dbafeb7
FB
873 pcr_pid = get16(&p, p_end) & 0x1fff;
874 if (pcr_pid < 0)
875 return;
172d1171 876 add_pid_to_pmt(ts, h->id, pcr_pid);
ebcf352a
BC
877
878 dprintf(ts->stream, "pcr_pid=0x%x\n", pcr_pid);
879
5dbafeb7
FB
880 program_info_length = get16(&p, p_end) & 0xfff;
881 if (program_info_length < 0)
882 return;
a45b40c4
NS
883 while(program_info_length >= 2) {
884 uint8_t tag, len;
885 tag = get8(&p, p_end);
886 len = get8(&p, p_end);
887 if(len > program_info_length - 2)
888 //something else is broken, exit the program_descriptors_loop
889 break;
890 program_info_length -= len + 2;
2fef7b53 891 if(tag == 0x05 && len >= 4) { // registration descriptor
8430f3ec 892 prog_reg_desc = bytestream_get_le32(&p);
a45b40c4 893 len -= 4;
a45b40c4
NS
894 }
895 p += len;
896 }
5dbafeb7
FB
897 p += program_info_length;
898 if (p >= p_end)
899 return;
d46c84ea
BC
900
901 // stop parsing after pmt, we found header
902 if (!ts->stream->nb_streams)
903 ts->stop_parse = 1;
904
5dbafeb7 905 for(;;) {
c6ec28b1 906 st = 0;
5dbafeb7
FB
907 stream_type = get8(&p, p_end);
908 if (stream_type < 0)
909 break;
910 pid = get16(&p, p_end) & 0x1fff;
911 if (pid < 0)
912 break;
f42d1d82
BC
913
914 /* now create ffmpeg stream */
915 if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
916 pes = ts->pids[pid]->u.pes_filter.opaque;
917 st = pes->st;
918 } else {
919 if (ts->pids[pid]) mpegts_close_filter(ts, ts->pids[pid]); //wrongly added sdt filter probably
b3f9f7a3 920 pes = add_pes_stream(ts, pid, pcr_pid);
f42d1d82 921 if (pes)
b3f9f7a3 922 st = av_new_stream(pes->stream, pes->pid);
f42d1d82
BC
923 }
924
925 if (!st)
926 return;
927
b3f9f7a3
BC
928 if (!pes->stream_type)
929 mpegts_set_stream_info(st, pes, stream_type, prog_reg_desc);
930
f42d1d82
BC
931 add_pid_to_pmt(ts, h->id, pid);
932
933 av_program_add_stream_index(ts->stream, h->id, st->index);
934
c6ec28b1
MN
935 desc_list_len = get16(&p, p_end) & 0xfff;
936 if (desc_list_len < 0)
937 break;
938 desc_list_end = p + desc_list_len;
a4d2af9b 939 if (desc_list_end > p_end)
5dbafeb7 940 break;
c6ec28b1
MN
941 for(;;) {
942 desc_tag = get8(&p, desc_list_end);
943 if (desc_tag < 0)
944 break;
945 desc_len = get8(&p, desc_list_end);
a4d2af9b
BA
946 if (desc_len < 0)
947 break;
c6ec28b1
MN
948 desc_end = p + desc_len;
949 if (desc_end > desc_list_end)
950 break;
ebcf352a
BC
951
952 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
2ddb79be 953 desc_tag, desc_len);
ebcf352a 954
d23612ef 955 if (st->codec->codec_id == CODEC_ID_NONE &&
6a2a50f8
BC
956 stream_type == STREAM_TYPE_PRIVATE_DATA)
957 mpegts_find_stream_type(st, desc_tag, DESC_types);
958
c6ec28b1 959 switch(desc_tag) {
e58992e4
FL
960 case 0x56: /* DVB teletext descriptor */
961 language[0] = get8(&p, desc_end);
962 language[1] = get8(&p, desc_end);
963 language[2] = get8(&p, desc_end);
964 language[3] = 0;
965 av_metadata_set(&st->metadata, "language", language);
966 break;
2fef7b53 967 case 0x59: /* subtitling descriptor */
c6ec28b1
MN
968 language[0] = get8(&p, desc_end);
969 language[1] = get8(&p, desc_end);
970 language[2] = get8(&p, desc_end);
971 language[3] = 0;
972 get8(&p, desc_end);
973 comp_page = get16(&p, desc_end);
974 anc_page = get16(&p, desc_end);
f42d1d82
BC
975 st->codec->sub_id = (anc_page << 16) | comp_page;
976 av_metadata_set(&st->metadata, "language", language);
c6ec28b1
MN
977 break;
978 case 0x0a: /* ISO 639 language descriptor */
979 language[0] = get8(&p, desc_end);
980 language[1] = get8(&p, desc_end);
981 language[2] = get8(&p, desc_end);
982 language[3] = 0;
f42d1d82 983 av_metadata_set(&st->metadata, "language", language);
c6ec28b1 984 break;
2fef7b53 985 case 0x05: /* registration descriptor */
5b3c4c8a 986 st->codec->codec_tag = bytestream_get_le32(&p);
1f076543 987 dprintf(ts->stream, "reg_desc=%.4s\n", (char*)&st->codec->codec_tag);
d23612ef 988 if (st->codec->codec_id == CODEC_ID_NONE &&
f42d1d82 989 stream_type == STREAM_TYPE_PRIVATE_DATA)
5b3c4c8a 990 mpegts_find_stream_type(st, st->codec->codec_tag, REGD_types);
4f1db48e 991 break;
c6ec28b1
MN
992 default:
993 break;
994 }
995 p = desc_end;
63380b5e
IC
996
997 if (prog_reg_desc == AV_RL32("HDMV") && stream_type == 0x83 && pes->sub_st) {
998 av_program_add_stream_index(ts->stream, h->id, pes->sub_st->index);
999 pes->sub_st->codec->codec_tag = st->codec->codec_tag;
1000 }
c6ec28b1
MN
1001 }
1002 p = desc_list_end;
5dbafeb7
FB
1003 }
1004 /* all parameters are there */
4d9a577e 1005 mpegts_close_filter(ts, filter);
5dbafeb7
FB
1006}
1007
4d9a577e 1008static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 1009{
4d9a577e 1010 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7
FB
1011 SectionHeader h1, *h = &h1;
1012 const uint8_t *p, *p_end;
1013 int sid, pmt_pid;
1014
ebcf352a
BC
1015#ifdef DEBUG
1016 dprintf(ts->stream, "PAT:\n");
750f0e1f 1017 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7
FB
1018#endif
1019 p_end = section + section_len - 4;
1020 p = section;
1021 if (parse_section_header(h, &p, p_end) < 0)
1022 return;
1023 if (h->tid != PAT_TID)
1024 return;
1025
172d1171 1026 clear_programs(ts);
5dbafeb7
FB
1027 for(;;) {
1028 sid = get16(&p, p_end);
1029 if (sid < 0)
1030 break;
1031 pmt_pid = get16(&p, p_end) & 0x1fff;
1032 if (pmt_pid < 0)
1033 break;
ebcf352a
BC
1034
1035 dprintf(ts->stream, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
1036
5dbafeb7
FB
1037 if (sid == 0x0000) {
1038 /* NIT info */
1039 } else {
1d257254 1040 av_new_program(ts->stream, sid);
28f03d04 1041 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
172d1171
NS
1042 add_pat_entry(ts, sid);
1043 add_pid_to_pmt(ts, sid, 0); //add pat pid to program
1044 add_pid_to_pmt(ts, sid, pmt_pid);
5dbafeb7
FB
1045 }
1046 }
5dbafeb7
FB
1047}
1048
4d9a577e 1049static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 1050{
4d9a577e 1051 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7
FB
1052 SectionHeader h1, *h = &h1;
1053 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
1054 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
1055 char *name, *provider_name;
1056
ebcf352a
BC
1057#ifdef DEBUG
1058 dprintf(ts->stream, "SDT:\n");
750f0e1f 1059 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7
FB
1060#endif
1061
1062 p_end = section + section_len - 4;
1063 p = section;
1064 if (parse_section_header(h, &p, p_end) < 0)
1065 return;
1066 if (h->tid != SDT_TID)
1067 return;
1068 onid = get16(&p, p_end);
1069 if (onid < 0)
1070 return;
1071 val = get8(&p, p_end);
1072 if (val < 0)
1073 return;
1074 for(;;) {
1075 sid = get16(&p, p_end);
1076 if (sid < 0)
1077 break;
1078 val = get8(&p, p_end);
1079 if (val < 0)
1080 break;
1081 desc_list_len = get16(&p, p_end) & 0xfff;
1082 if (desc_list_len < 0)
1083 break;
1084 desc_list_end = p + desc_list_len;
1085 if (desc_list_end > p_end)
1086 break;
1087 for(;;) {
1088 desc_tag = get8(&p, desc_list_end);
1089 if (desc_tag < 0)
1090 break;
1091 desc_len = get8(&p, desc_list_end);
1092 desc_end = p + desc_len;
1093 if (desc_end > desc_list_end)
1094 break;
ebcf352a
BC
1095
1096 dprintf(ts->stream, "tag: 0x%02x len=%d\n",
2ddb79be 1097 desc_tag, desc_len);
ebcf352a 1098
5dbafeb7
FB
1099 switch(desc_tag) {
1100 case 0x48:
1101 service_type = get8(&p, p_end);
1102 if (service_type < 0)
1103 break;
1104 provider_name = getstr8(&p, p_end);
1105 if (!provider_name)
1106 break;
1107 name = getstr8(&p, p_end);
1d257254
NS
1108 if (name) {
1109 AVProgram *program = av_new_program(ts->stream, sid);
08681191
AJ
1110 if(program) {
1111 av_metadata_set(&program->metadata, "name", name);
1112 av_metadata_set(&program->metadata, "provider_name", provider_name);
1113 }
1d257254 1114 }
81d5ae6d
MN
1115 av_free(name);
1116 av_free(provider_name);
5dbafeb7
FB
1117 break;
1118 default:
1119 break;
1120 }
1121 p = desc_end;
1122 }
1123 p = desc_list_end;
1124 }
5dbafeb7
FB
1125}
1126
5dbafeb7 1127/* handle one TS packet */
bdfa9824 1128static int handle_packet(MpegTSContext *ts, const uint8_t *packet)
fe9cf0d4 1129{
b45a7a18 1130 AVFormatContext *s = ts->stream;
5dbafeb7
FB
1131 MpegTSFilter *tss;
1132 int len, pid, cc, cc_ok, afc, is_start;
1133 const uint8_t *p, *p_end;
4c6b49bf 1134 int64_t pos;
5dbafeb7 1135
80fb8234 1136 pid = AV_RB16(packet + 1) & 0x1fff;
172d1171 1137 if(pid && discard_pid(ts, pid))
bdfa9824 1138 return 0;
5dbafeb7
FB
1139 is_start = packet[1] & 0x40;
1140 tss = ts->pids[pid];
1141 if (ts->auto_guess && tss == NULL && is_start) {
b3f9f7a3 1142 add_pes_stream(ts, pid, -1);
5dbafeb7
FB
1143 tss = ts->pids[pid];
1144 }
1145 if (!tss)
bdfa9824 1146 return 0;
5dbafeb7
FB
1147
1148 /* continuity check (currently not used) */
1149 cc = (packet[3] & 0xf);
1150 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
1151 tss->last_cc = cc;
115329f1 1152
5dbafeb7
FB
1153 /* skip adaptation field */
1154 afc = (packet[3] >> 4) & 3;
1155 p = packet + 4;
1156 if (afc == 0) /* reserved value */
bdfa9824 1157 return 0;
5dbafeb7 1158 if (afc == 2) /* adaptation field only */
bdfa9824 1159 return 0;
5dbafeb7
FB
1160 if (afc == 3) {
1161 /* skip adapation field */
1162 p += p[0] + 1;
1163 }
1164 /* if past the end of packet, ignore */
1165 p_end = packet + TS_PACKET_SIZE;
1166 if (p >= p_end)
bdfa9824 1167 return 0;
115329f1 1168
4c6b49bf
IS
1169 pos = url_ftell(ts->stream->pb);
1170 ts->pos47= pos % ts->raw_packet_size;
5fd63f3c 1171
5dbafeb7
FB
1172 if (tss->type == MPEGTS_SECTION) {
1173 if (is_start) {
1174 /* pointer field present */
1175 len = *p++;
1176 if (p + len > p_end)
bdfa9824 1177 return 0;
5dbafeb7 1178 if (len && cc_ok) {
27bb1ed3 1179 /* write remaining section bytes */
115329f1 1180 write_section_data(s, tss,
5dbafeb7 1181 p, len, 0);
27bb1ed3
WG
1182 /* check whether filter has been closed */
1183 if (!ts->pids[pid])
bdfa9824 1184 return 0;
5dbafeb7
FB
1185 }
1186 p += len;
1187 if (p < p_end) {
115329f1 1188 write_section_data(s, tss,
5dbafeb7
FB
1189 p, p_end - p, 1);
1190 }
1191 } else {
1192 if (cc_ok) {
115329f1 1193 write_section_data(s, tss,
5dbafeb7 1194 p, p_end - p, 0);
fc48fe84 1195 }
5dbafeb7
FB
1196 }
1197 } else {
bdfa9824 1198 int ret;
4c6b49bf 1199 // Note: The position here points actually behind the current packet.
bdfa9824
BC
1200 if ((ret = tss->u.pes_filter.pes_cb(tss, p, p_end - p, is_start,
1201 pos - ts->raw_packet_size)) < 0)
1202 return ret;
5dbafeb7 1203 }
bdfa9824
BC
1204
1205 return 0;
5dbafeb7
FB
1206}
1207
fc48fe84
FB
1208/* XXX: try to find a better synchro over several packets (use
1209 get_packet_size() ?) */
1303d62d 1210static int mpegts_resync(AVFormatContext *s)
fc48fe84 1211{
1303d62d 1212 ByteIOContext *pb = s->pb;
fc48fe84
FB
1213 int c, i;
1214
1215 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1216 c = url_fgetc(pb);
1217 if (c < 0)
1218 return -1;
1219 if (c == 0x47) {
1220 url_fseek(pb, -1, SEEK_CUR);
1221 return 0;
1222 }
1223 }
1303d62d 1224 av_log(s, AV_LOG_ERROR, "max resync size reached, could not find sync byte\n");
fc48fe84
FB
1225 /* no sync found */
1226 return -1;
1227}
1228
b45a7a18 1229/* return -1 if error or EOF. Return 0 if OK. */
1303d62d 1230static int read_packet(AVFormatContext *s, uint8_t *buf, int raw_packet_size)
5dbafeb7 1231{
1303d62d 1232 ByteIOContext *pb = s->pb;
b45a7a18
FB
1233 int skip, len;
1234
1235 for(;;) {
1236 len = get_buffer(pb, buf, TS_PACKET_SIZE);
1237 if (len != TS_PACKET_SIZE)
6f3e0b21 1238 return AVERROR(EIO);
b45a7a18
FB
1239 /* check paquet sync byte */
1240 if (buf[0] != 0x47) {
1241 /* find a new packet start */
1242 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1303d62d
BC
1243 if (mpegts_resync(s) < 0)
1244 return AVERROR(EAGAIN);
b45a7a18
FB
1245 else
1246 continue;
1247 } else {
1248 skip = raw_packet_size - TS_PACKET_SIZE;
1249 if (skip > 0)
1250 url_fskip(pb, skip);
1251 break;
1252 }
1253 }
1254 return 0;
1255}
1256
1257static int handle_packets(MpegTSContext *ts, int nb_packets)
1258{
1259 AVFormatContext *s = ts->stream;
b45a7a18
FB
1260 uint8_t packet[TS_PACKET_SIZE];
1261 int packet_num, ret;
5dbafeb7
FB
1262
1263 ts->stop_parse = 0;
1264 packet_num = 0;
fe9cf0d4 1265 for(;;) {
90d13e30 1266 if (ts->stop_parse>0)
5dbafeb7
FB
1267 break;
1268 packet_num++;
1269 if (nb_packets != 0 && packet_num >= nb_packets)
1270 break;
1303d62d 1271 ret = read_packet(s, packet, ts->raw_packet_size);
b45a7a18
FB
1272 if (ret != 0)
1273 return ret;
bdfa9824
BC
1274 ret = handle_packet(ts, packet);
1275 if (ret != 0)
1276 return ret;
5dbafeb7
FB
1277 }
1278 return 0;
1279}
fe9cf0d4 1280
5dbafeb7
FB
1281static int mpegts_probe(AVProbeData *p)
1282{
fc48fe84 1283#if 1
a0b8f70c 1284 const int size= p->buf_size;
5d4d67e0 1285 int score, fec_score, dvhs_score;
1558d0b9 1286 int check_count= size / TS_FEC_PACKET_SIZE;
a0b8f70c 1287#define CHECK_COUNT 10
115329f1 1288
1558d0b9 1289 if (check_count < CHECK_COUNT)
a0b8f70c 1290 return -1;
115329f1 1291
1558d0b9
MN
1292 score = analyze(p->buf, TS_PACKET_SIZE *check_count, TS_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
1293 dvhs_score= analyze(p->buf, TS_DVHS_PACKET_SIZE*check_count, TS_DVHS_PACKET_SIZE, NULL)*CHECK_COUNT/check_count;
1294 fec_score = analyze(p->buf, TS_FEC_PACKET_SIZE *check_count, TS_FEC_PACKET_SIZE , NULL)*CHECK_COUNT/check_count;
5d4d67e0 1295// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
115329f1 1296
a0b8f70c 1297// we need a clear definition for the returned score otherwise things will become messy sooner or later
5d4d67e0
NS
1298 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1299 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
a0b8f70c
MN
1300 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1301 else return -1;
fc48fe84
FB
1302#else
1303 /* only use the extension for safer guess */
1304 if (match_ext(p->filename, "ts"))
1305 return AVPROBE_SCORE_MAX;
1306 else
1307 return 0;
1308#endif
5dbafeb7
FB
1309}
1310
910f02a0 1311/* return the 90kHz PCR and the extension for the 27MHz PCR. return
b45a7a18 1312 (-1) if not available */
115329f1 1313static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
b45a7a18
FB
1314 const uint8_t *packet)
1315{
1316 int afc, len, flags;
1317 const uint8_t *p;
1318 unsigned int v;
1319
1320 afc = (packet[3] >> 4) & 3;
1321 if (afc <= 1)
1322 return -1;
1323 p = packet + 4;
1324 len = p[0];
1325 p++;
1326 if (len == 0)
1327 return -1;
1328 flags = *p++;
1329 len--;
1330 if (!(flags & 0x10))
1331 return -1;
1332 if (len < 6)
1333 return -1;
80fb8234 1334 v = AV_RB32(p);
b45a7a18
FB
1335 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1336 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1337 return 0;
1338}
1339
5dbafeb7
FB
1340static int mpegts_read_header(AVFormatContext *s,
1341 AVFormatParameters *ap)
1342{
1343 MpegTSContext *ts = s->priv_data;
899681cd 1344 ByteIOContext *pb = s->pb;
1558d0b9 1345 uint8_t buf[5*1024];
4dc8a963 1346 int len;
5dbafeb7 1347 int64_t pos;
e05655fb 1348
b45a7a18 1349 if (ap) {
b45a7a18 1350 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
38c48be2
MN
1351 if(ap->mpeg2ts_raw){
1352 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
1353 return -1;
1354 }
b45a7a18
FB
1355 }
1356
5dbafeb7
FB
1357 /* read the first 1024 bytes to get packet size */
1358 pos = url_ftell(pb);
1359 len = get_buffer(pb, buf, sizeof(buf));
1360 if (len != sizeof(buf))
1361 goto fail;
1362 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1363 if (ts->raw_packet_size <= 0)
1364 goto fail;
b45a7a18 1365 ts->stream = s;
5dbafeb7 1366 ts->auto_guess = 0;
5dbafeb7 1367
38c48be2 1368 if (s->iformat == &mpegts_demuxer) {
b45a7a18 1369 /* normal demux */
fc48fe84 1370
a45ec414
BF
1371 /* first do a scaning to get all the services */
1372 url_fseek(pb, pos, SEEK_SET);
c950c25a
BC
1373
1374 mpegts_open_section_filter(ts, SDT_PID, sdt_cb, ts, 1);
115329f1 1375
7b9677f6 1376 mpegts_open_section_filter(ts, PAT_PID, pat_cb, ts, 1);
115329f1 1377
45a49840 1378 handle_packets(ts, s->probesize / ts->raw_packet_size);
a45ec414 1379 /* if could not find service, enable auto_guess */
115329f1 1380
a45ec414 1381 ts->auto_guess = 1;
115329f1 1382
ebcf352a
BC
1383 dprintf(ts->stream, "tuning done\n");
1384
b45a7a18
FB
1385 s->ctx_flags |= AVFMTCTX_NOHEADER;
1386 } else {
1387 AVStream *st;
1388 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1389 int64_t pcrs[2], pcr_h;
1390 int packet_count[2];
1391 uint8_t packet[TS_PACKET_SIZE];
115329f1 1392
b45a7a18 1393 /* only read packets */
115329f1 1394
b45a7a18
FB
1395 st = av_new_stream(s, 0);
1396 if (!st)
1397 goto fail;
9ee91c2f 1398 av_set_pts_info(st, 60, 1, 27000000);
01f4895c
MN
1399 st->codec->codec_type = CODEC_TYPE_DATA;
1400 st->codec->codec_id = CODEC_ID_MPEG2TS;
115329f1 1401
b45a7a18
FB
1402 /* we iterate until we find two PCRs to estimate the bitrate */
1403 pcr_pid = -1;
1404 nb_pcrs = 0;
1405 nb_packets = 0;
1406 for(;;) {
1303d62d 1407 ret = read_packet(s, packet, ts->raw_packet_size);
b45a7a18
FB
1408 if (ret < 0)
1409 return -1;
80fb8234 1410 pid = AV_RB16(packet + 1) & 0x1fff;
b45a7a18
FB
1411 if ((pcr_pid == -1 || pcr_pid == pid) &&
1412 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1413 pcr_pid = pid;
1414 packet_count[nb_pcrs] = nb_packets;
1415 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1416 nb_pcrs++;
1417 if (nb_pcrs >= 2)
1418 break;
1419 }
1420 nb_packets++;
1421 }
5dbafeb7 1422
b45a7a18
FB
1423 /* NOTE1: the bitrate is computed without the FEC */
1424 /* NOTE2: it is only the bitrate of the start of the stream */
1425 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1426 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1427 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
01f4895c 1428 st->codec->bit_rate = s->bit_rate;
c0df9d75 1429 st->start_time = ts->cur_pcr;
b45a7a18 1430#if 0
f0ee3ea6 1431 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
b45a7a18 1432 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
5dbafeb7 1433#endif
fe9cf0d4 1434 }
5dbafeb7
FB
1435
1436 url_fseek(pb, pos, SEEK_SET);
fe9cf0d4 1437 return 0;
5dbafeb7
FB
1438 fail:
1439 return -1;
1440}
1441
b45a7a18
FB
1442#define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1443
1444static int mpegts_raw_read_packet(AVFormatContext *s,
1445 AVPacket *pkt)
1446{
1447 MpegTSContext *ts = s->priv_data;
1448 int ret, i;
1449 int64_t pcr_h, next_pcr_h, pos;
1450 int pcr_l, next_pcr_l;
1451 uint8_t pcr_buf[12];
1452
1453 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
8fa36ae0 1454 return AVERROR(ENOMEM);
899681cd 1455 pkt->pos= url_ftell(s->pb);
1303d62d 1456 ret = read_packet(s, pkt->data, ts->raw_packet_size);
b45a7a18
FB
1457 if (ret < 0) {
1458 av_free_packet(pkt);
1459 return ret;
1460 }
1461 if (ts->mpeg2ts_compute_pcr) {
1462 /* compute exact PCR for each packet */
1463 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1464 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
899681cd 1465 pos = url_ftell(s->pb);
b45a7a18 1466 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
899681cd
BA
1467 url_fseek(s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1468 get_buffer(s->pb, pcr_buf, 12);
b45a7a18
FB
1469 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1470 /* XXX: not precise enough */
115329f1 1471 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
b45a7a18
FB
1472 (i + 1);
1473 break;
1474 }
1475 }
899681cd 1476 url_fseek(s->pb, pos, SEEK_SET);
b45a7a18
FB
1477 /* no next PCR found: we use previous increment */
1478 ts->cur_pcr = pcr_h * 300 + pcr_l;
1479 }
1480 pkt->pts = ts->cur_pcr;
1481 pkt->duration = ts->pcr_incr;
1482 ts->cur_pcr += ts->pcr_incr;
1483 }
1484 pkt->stream_index = 0;
1485 return 0;
1486}
1487
5dbafeb7
FB
1488static int mpegts_read_packet(AVFormatContext *s,
1489 AVPacket *pkt)
1490{
1491 MpegTSContext *ts = s->priv_data;
0c137557
BC
1492 int ret, i;
1493
1494 if (url_ftell(s->pb) != ts->last_pos) {
1495 /* seek detected, flush pes buffer */
1496 for (i = 0; i < NB_PID_MAX; i++) {
1497 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1498 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1499 av_freep(&pes->buffer);
1500 pes->data_index = 0;
1501 pes->state = MPEGTS_SKIP; /* skip until pes header */
1502 }
1503 }
1504 }
b45a7a18 1505
38c48be2 1506 ts->pkt = pkt;
0c137557
BC
1507 ret = handle_packets(ts, 0);
1508 if (ret < 0) {
1509 /* flush pes data left */
1510 for (i = 0; i < NB_PID_MAX; i++) {
1511 if (ts->pids[i] && ts->pids[i]->type == MPEGTS_PES) {
1512 PESContext *pes = ts->pids[i]->u.pes_filter.opaque;
1513 if (pes->state == MPEGTS_PAYLOAD && pes->data_index > 0) {
1514 new_pes_packet(pes, pkt);
8956f680 1515 pes->state = MPEGTS_SKIP;
0c137557
BC
1516 ret = 0;
1517 break;
1518 }
1519 }
1520 }
1521 }
1522
1523 ts->last_pos = url_ftell(s->pb);
1524
1525 return ret;
fe9cf0d4
FB
1526}
1527
1528static int mpegts_read_close(AVFormatContext *s)
1529{
1530 MpegTSContext *ts = s->priv_data;
1531 int i;
81d5ae6d
MN
1532
1533 clear_programs(ts);
1534
fe9cf0d4 1535 for(i=0;i<NB_PID_MAX;i++)
ec7d0d2e 1536 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
00a6b92b 1537
fe9cf0d4
FB
1538 return 0;
1539}
1540
115329f1 1541static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
cdd5034f 1542 int64_t *ppos, int64_t pos_limit)
27f388aa
FB
1543{
1544 MpegTSContext *ts = s->priv_data;
1545 int64_t pos, timestamp;
1546 uint8_t buf[TS_PACKET_SIZE];
dbf6b678 1547 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
cdd5034f 1548 const int find_next= 1;
5fd63f3c 1549 pos = ((*ppos + ts->raw_packet_size - 1 - ts->pos47) / ts->raw_packet_size) * ts->raw_packet_size + ts->pos47;
27f388aa
FB
1550 if (find_next) {
1551 for(;;) {
899681cd
BA
1552 url_fseek(s->pb, pos, SEEK_SET);
1553 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
27f388aa 1554 return AV_NOPTS_VALUE;
80fb8234 1555 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
27f388aa
FB
1556 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1557 break;
1558 }
1559 pos += ts->raw_packet_size;
1560 }
1561 } else {
1562 for(;;) {
1563 pos -= ts->raw_packet_size;
1564 if (pos < 0)
1565 return AV_NOPTS_VALUE;
899681cd
BA
1566 url_fseek(s->pb, pos, SEEK_SET);
1567 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
27f388aa 1568 return AV_NOPTS_VALUE;
80fb8234 1569 if ((pcr_pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pcr_pid) &&
27f388aa
FB
1570 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1571 break;
1572 }
1573 }
1574 }
1575 *ppos = pos;
27f388aa 1576
cdd5034f 1577 return timestamp;
27f388aa
FB
1578}
1579
af8aae3f
IS
1580#ifdef USE_SYNCPOINT_SEARCH
1581
608be2ac
IS
1582static int read_seek2(AVFormatContext *s,
1583 int stream_index,
1584 int64_t min_ts,
1585 int64_t target_ts,
1586 int64_t max_ts,
1587 int flags)
a8dd8dc6 1588{
e05655fb
MN
1589 int64_t pos;
1590
a8dd8dc6
IS
1591 int64_t ts_ret, ts_adj;
1592 int stream_index_gen_search;
1593 AVStream *st;
1594 AVParserState *backup;
e05655fb 1595
a8dd8dc6 1596 backup = ff_store_parser_state(s);
e05655fb 1597
a8dd8dc6 1598 // detect direction of seeking for search purposes
608be2ac
IS
1599 flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ?
1600 AVSEEK_FLAG_BACKWARD : 0;
a8dd8dc6
IS
1601
1602 if (flags & AVSEEK_FLAG_BYTE) {
608be2ac 1603 // use position directly, we will search starting from it
a8dd8dc6
IS
1604 pos = target_ts;
1605 } else {
608be2ac
IS
1606 // search for some position with good timestamp match
1607 if (stream_index < 0) {
a8dd8dc6
IS
1608 stream_index_gen_search = av_find_default_stream_index(s);
1609 if (stream_index_gen_search < 0) {
1610 ff_restore_parser_state(s, backup);
1611 return -1;
1612 }
1613
1614 st = s->streams[stream_index_gen_search];
608be2ac
IS
1615 // timestamp for default must be expressed in AV_TIME_BASE units
1616 ts_adj = av_rescale(target_ts,
1617 st->time_base.den,
1618 AV_TIME_BASE * (int64_t)st->time_base.num);
a8dd8dc6
IS
1619 } else {
1620 ts_adj = target_ts;
1621 stream_index_gen_search = stream_index;
1622 }
1623 pos = av_gen_search(s, stream_index_gen_search, ts_adj,
1624 0, INT64_MAX, -1,
1625 AV_NOPTS_VALUE,
1626 AV_NOPTS_VALUE,
1627 flags, &ts_ret, mpegts_get_pcr);
1628 if (pos < 0) {
1629 ff_restore_parser_state(s, backup);
e05655fb 1630 return -1;
a8dd8dc6 1631 }
115329f1 1632 }
e05655fb 1633
608be2ac
IS
1634 // search for actual matching keyframe/starting position for all streams
1635 if (ff_gen_syncpoint_search(s, stream_index, pos,
1636 min_ts, target_ts, max_ts,
1637 flags) < 0) {
a8dd8dc6
IS
1638 ff_restore_parser_state(s, backup);
1639 return -1;
1640 }
1641
1642 ff_free_parser_state(s, backup);
e05655fb
MN
1643 return 0;
1644}
1645
a8dd8dc6
IS
1646static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1647{
1648 int ret;
1649 if (flags & AVSEEK_FLAG_BACKWARD) {
608be2ac
IS
1650 flags &= ~AVSEEK_FLAG_BACKWARD;
1651 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags);
1652 if (ret < 0)
1653 // for compatibility reasons, seek to the best-fitting timestamp
1654 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
a8dd8dc6
IS
1655 } else {
1656 ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags);
608be2ac
IS
1657 if (ret < 0)
1658 // for compatibility reasons, seek to the best-fitting timestamp
a8dd8dc6 1659 ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags);
a8dd8dc6
IS
1660 }
1661 return ret;
1662}
1663
af8aae3f
IS
1664#else
1665
1666static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1667 MpegTSContext *ts = s->priv_data;
1668 uint8_t buf[TS_PACKET_SIZE];
1669 int64_t pos;
1670
1671 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1672 return -1;
1673
1674 pos= url_ftell(s->pb);
1675
1676 for(;;) {
1677 url_fseek(s->pb, pos, SEEK_SET);
1678 if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1679 return -1;
1680// pid = AV_RB16(buf + 1) & 0x1fff;
1681 if(buf[1] & 0x40) break;
1682 pos += ts->raw_packet_size;
1683 }
1684 url_fseek(s->pb, pos, SEEK_SET);
1685
1686 return 0;
1687}
1688
1689#endif
1690
b45a7a18
FB
1691/**************************************************************/
1692/* parsing functions - called from other demuxers such as RTP */
1693
1694MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1695{
1696 MpegTSContext *ts;
115329f1 1697
b45a7a18
FB
1698 ts = av_mallocz(sizeof(MpegTSContext));
1699 if (!ts)
1700 return NULL;
1701 /* no stream case, currently used by RTP */
1702 ts->raw_packet_size = TS_PACKET_SIZE;
1703 ts->stream = s;
1704 ts->auto_guess = 1;
1705 return ts;
1706}
1707
1708/* return the consumed length if a packet was output, or -1 if no
1709 packet is output */
1710int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1711 const uint8_t *buf, int len)
1712{
1713 int len1;
1714
1715 len1 = len;
1716 ts->pkt = pkt;
1717 ts->stop_parse = 0;
1718 for(;;) {
90d13e30 1719 if (ts->stop_parse>0)
b45a7a18
FB
1720 break;
1721 if (len < TS_PACKET_SIZE)
1722 return -1;
1723 if (buf[0] != 0x47) {
550f0a9b 1724 buf++;
b45a7a18
FB
1725 len--;
1726 } else {
1727 handle_packet(ts, buf);
1728 buf += TS_PACKET_SIZE;
1729 len -= TS_PACKET_SIZE;
1730 }
1731 }
1732 return len1 - len;
1733}
1734
1735void mpegts_parse_close(MpegTSContext *ts)
1736{
1737 int i;
1738
1739 for(i=0;i<NB_PID_MAX;i++)
1740 av_free(ts->pids[i]);
1741 av_free(ts);
1742}
1743
d2a067d1 1744AVInputFormat mpegts_demuxer = {
fe9cf0d4 1745 "mpegts",
bde15e74 1746 NULL_IF_CONFIG_SMALL("MPEG-2 transport stream format"),
fe9cf0d4
FB
1747 sizeof(MpegTSContext),
1748 mpegts_probe,
1749 mpegts_read_header,
1750 mpegts_read_packet,
1751 mpegts_read_close,
e05655fb 1752 read_seek,
cdd5034f 1753 mpegts_get_pcr,
ff9c8d7c 1754 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
af8aae3f 1755#ifdef USE_SYNCPOINT_SEARCH
a8dd8dc6 1756 .read_seek2 = read_seek2,
af8aae3f 1757#endif
fe9cf0d4 1758};
38c48be2
MN
1759
1760AVInputFormat mpegtsraw_demuxer = {
1761 "mpegtsraw",
bde15e74 1762 NULL_IF_CONFIG_SMALL("MPEG-2 raw transport stream format"),
38c48be2 1763 sizeof(MpegTSContext),
056f6b8f 1764 NULL,
38c48be2
MN
1765 mpegts_read_header,
1766 mpegts_raw_read_packet,
1767 mpegts_read_close,
1768 read_seek,
1769 mpegts_get_pcr,
ff9c8d7c 1770 .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
af8aae3f 1771#ifdef USE_SYNCPOINT_SEARCH
a8dd8dc6 1772 .read_seek2 = read_seek2,
af8aae3f 1773#endif
38c48be2 1774};