proper per stream pcr_pid support by (Wolfram Gloger wmglo dent.med.uni-muenchen...
[libav.git] / libavformat / mpegts.c
CommitLineData
fe9cf0d4 1/*
7fbde343 2 * MPEG2 transport stream (aka DVB) demuxer
5dbafeb7 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
FB
20 */
21#include "avformat.h"
43a80cce 22#include "crc.h"
5dbafeb7 23#include "mpegts.h"
38c48be2 24#include "allformats.h"
fe9cf0d4 25
5dbafeb7 26//#define DEBUG_SI
27f388aa 27//#define DEBUG_SEEK
5dbafeb7
FB
28
29/* 1.0 second at 24Mbit/s */
fc48fe84
FB
30#define MAX_SCAN_PACKETS 32000
31
32/* maximum size in which we look for synchronisation if
33 synchronisation is lost */
34#define MAX_RESYNC_SIZE 4096
5dbafeb7 35
c6ec28b1
MN
36typedef struct PESContext PESContext;
37
dbf6b678 38static PESContext* add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type);
c6ec28b1 39static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code);
5dbafeb7
FB
40
41enum MpegTSFilterType {
42 MPEGTS_PES,
43 MPEGTS_SECTION,
fe9cf0d4
FB
44};
45
5dbafeb7 46typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
fe9cf0d4 47
5dbafeb7
FB
48typedef struct MpegTSPESFilter {
49 PESCallback *pes_cb;
50 void *opaque;
51} MpegTSPESFilter;
52
53typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
54
55typedef void SetServiceCallback(void *opaque, int ret);
56
57typedef struct MpegTSSectionFilter {
58 int section_index;
59 int section_h_size;
60 uint8_t *section_buf;
61 int check_crc:1;
62 int end_of_section_reached:1;
63 SectionCallback *section_cb;
64 void *opaque;
65} MpegTSSectionFilter;
66
67typedef struct MpegTSFilter {
fe9cf0d4 68 int pid;
fe9cf0d4 69 int last_cc; /* last cc code (-1 if first packet) */
5dbafeb7
FB
70 enum MpegTSFilterType type;
71 union {
72 MpegTSPESFilter pes_filter;
73 MpegTSSectionFilter section_filter;
74 } u;
75} MpegTSFilter;
76
77typedef struct MpegTSService {
78 int running:1;
e995cfca
DK
79 int sid; /**< MPEG Program Number of stream */
80 char *provider_name; /**< DVB Network name, "" if not DVB stream */
81 char *name; /**< DVB Service name, "MPEG Program [sid]" if not DVB stream*/
5dbafeb7 82} MpegTSService;
fe9cf0d4 83
b45a7a18 84struct MpegTSContext {
5dbafeb7
FB
85 /* user data */
86 AVFormatContext *stream;
e995cfca
DK
87 /** raw packet size, including FEC if present */
88 int raw_packet_size;
89 /** if true, all pids are analyzed to find streams */
90 int auto_guess;
5dbafeb7 91
e995cfca
DK
92 /** compute exact PCR for each transport stream packet */
93 int mpeg2ts_compute_pcr;
b45a7a18 94
e995cfca
DK
95 int64_t cur_pcr; /**< used to estimate the exact PCR */
96 int pcr_incr; /**< used to estimate the exact PCR */
97 int pcr_pid; /**< used to estimate the exact PCR */
115329f1 98
5dbafeb7 99 /* data needed to handle file based ts */
e995cfca
DK
100 /** stop parsing loop */
101 int stop_parse;
102 /** packet containing Audio/Video data */
103 AVPacket *pkt;
5dbafeb7
FB
104
105 /******************************************/
106 /* private mpegts data */
107 /* scan context */
e995cfca 108 /** number of PMTs in the last PAT seen */
5dbafeb7 109 int nb_services;
e995cfca 110 /** list of PMTs in the last PAT seen */
5dbafeb7 111 MpegTSService **services;
115329f1 112
5dbafeb7 113
e995cfca 114 /** filters for various streams specified by PMT + for the PAT and PMT */
5dbafeb7 115 MpegTSFilter *pids[NB_PID_MAX];
b45a7a18 116};
fe9cf0d4 117
ecc31d1d
MN
118/* TS stream handling */
119
120enum MpegTSState {
121 MPEGTS_HEADER = 0,
122 MPEGTS_PESHEADER_FILL,
123 MPEGTS_PAYLOAD,
124 MPEGTS_SKIP,
125};
126
127/* enough for PES header + length */
128#define PES_START_SIZE 9
129#define MAX_PES_HEADER_SIZE (9 + 255)
130
131struct PESContext {
132 int pid;
dbf6b678 133 int pcr_pid; /**< if -1 then all packets containing PCR are considered */
ecc31d1d
MN
134 int stream_type;
135 MpegTSContext *ts;
136 AVFormatContext *stream;
137 AVStream *st;
138 enum MpegTSState state;
139 /* used to get the format */
140 int data_index;
141 int total_size;
142 int pes_header_size;
143 int64_t pts, dts;
144 uint8_t header[MAX_PES_HEADER_SIZE];
145};
146
e995cfca
DK
147/**
148 * Assembles PES packets out of TS packets, and then calls the "section_cb"
149 * function when they are complete.
150 */
5dbafeb7
FB
151static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
152 const uint8_t *buf, int buf_size, int is_start)
153{
154 MpegTSSectionFilter *tss = &tss1->u.section_filter;
155 int len;
115329f1 156
5dbafeb7
FB
157 if (is_start) {
158 memcpy(tss->section_buf, buf, buf_size);
159 tss->section_index = buf_size;
160 tss->section_h_size = -1;
161 tss->end_of_section_reached = 0;
162 } else {
163 if (tss->end_of_section_reached)
164 return;
165 len = 4096 - tss->section_index;
166 if (buf_size < len)
167 len = buf_size;
168 memcpy(tss->section_buf + tss->section_index, buf, len);
169 tss->section_index += len;
170 }
171
172 /* compute section length if possible */
173 if (tss->section_h_size == -1 && tss->section_index >= 3) {
174 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
175 if (len > 4096)
176 return;
177 tss->section_h_size = len;
178 }
179
180 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
5dbafeb7 181 tss->end_of_section_reached = 1;
27bb1ed3 182 if (!tss->check_crc ||
43a80cce 183 av_crc(av_crc04C11DB7, -1, tss->section_buf, tss->section_h_size) == 0)
4d9a577e 184 tss->section_cb(tss1, tss->section_buf, tss->section_h_size);
5dbafeb7
FB
185 }
186}
187
7b49ce2e 188static MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
5dbafeb7
FB
189 SectionCallback *section_cb, void *opaque,
190 int check_crc)
191
192{
193 MpegTSFilter *filter;
194 MpegTSSectionFilter *sec;
115329f1 195
b45a7a18 196#ifdef DEBUG_SI
f0ee3ea6 197 av_log(ts->stream, AV_LOG_DEBUG, "Filter: pid=0x%x\n", pid);
b45a7a18 198#endif
5dbafeb7
FB
199 if (pid >= NB_PID_MAX || ts->pids[pid])
200 return NULL;
201 filter = av_mallocz(sizeof(MpegTSFilter));
115329f1 202 if (!filter)
5dbafeb7
FB
203 return NULL;
204 ts->pids[pid] = filter;
205 filter->type = MPEGTS_SECTION;
206 filter->pid = pid;
207 filter->last_cc = -1;
208 sec = &filter->u.section_filter;
209 sec->section_cb = section_cb;
210 sec->opaque = opaque;
211 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
212 sec->check_crc = check_crc;
213 if (!sec->section_buf) {
214 av_free(filter);
215 return NULL;
216 }
217 return filter;
218}
219
7b49ce2e 220static MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
5dbafeb7
FB
221 PESCallback *pes_cb,
222 void *opaque)
223{
224 MpegTSFilter *filter;
225 MpegTSPESFilter *pes;
226
227 if (pid >= NB_PID_MAX || ts->pids[pid])
228 return NULL;
229 filter = av_mallocz(sizeof(MpegTSFilter));
115329f1 230 if (!filter)
5dbafeb7
FB
231 return NULL;
232 ts->pids[pid] = filter;
233 filter->type = MPEGTS_PES;
234 filter->pid = pid;
235 filter->last_cc = -1;
236 pes = &filter->u.pes_filter;
237 pes->pes_cb = pes_cb;
238 pes->opaque = opaque;
239 return filter;
240}
241
7b49ce2e 242static void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
5dbafeb7
FB
243{
244 int pid;
245
246 pid = filter->pid;
247 if (filter->type == MPEGTS_SECTION)
248 av_freep(&filter->u.section_filter.section_buf);
ec7d0d2e
GB
249 else if (filter->type == MPEGTS_PES)
250 av_freep(&filter->u.pes_filter.opaque);
251
5dbafeb7
FB
252 av_free(filter);
253 ts->pids[pid] = NULL;
254}
255
a0b8f70c
MN
256static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
257 int stat[packet_size];
258 int i;
259 int x=0;
260 int best_score=0;
261
262 memset(stat, 0, packet_size*sizeof(int));
263
264 for(x=i=0; i<size; i++){
265 if(buf[i] == 0x47){
266 stat[x]++;
267 if(stat[x] > best_score){
268 best_score= stat[x];
269 if(index) *index= x;
270 }
271 }
272
273 x++;
274 if(x == packet_size) x= 0;
275 }
276
277 return best_score;
278}
279
fe9cf0d4 280/* autodetect fec presence. Must have at least 1024 bytes */
5dbafeb7 281static int get_packet_size(const uint8_t *buf, int size)
fe9cf0d4 282{
5d4d67e0 283 int score, fec_score, dvhs_score;
fe9cf0d4
FB
284
285 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
286 return -1;
115329f1 287
a0b8f70c 288 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
5d4d67e0 289 dvhs_score = analyze(buf, size, TS_DVHS_PACKET_SIZE, NULL);
a0b8f70c 290 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
5d4d67e0 291// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
115329f1 292
5d4d67e0
NS
293 if (score > fec_score && score > dvhs_score) return TS_PACKET_SIZE;
294 else if(dvhs_score > score && dvhs_score > fec_score) return TS_DVHS_PACKET_SIZE;
295 else if(score < fec_score && dvhs_score < fec_score) return TS_FEC_PACKET_SIZE;
a0b8f70c 296 else return -1;
fe9cf0d4
FB
297}
298
5dbafeb7
FB
299typedef struct SectionHeader {
300 uint8_t tid;
301 uint16_t id;
302 uint8_t version;
303 uint8_t sec_num;
304 uint8_t last_sec_num;
305} SectionHeader;
306
307static inline int get8(const uint8_t **pp, const uint8_t *p_end)
fe9cf0d4 308{
5dbafeb7
FB
309 const uint8_t *p;
310 int c;
311
312 p = *pp;
313 if (p >= p_end)
314 return -1;
315 c = *p++;
316 *pp = p;
317 return c;
fe9cf0d4
FB
318}
319
5dbafeb7
FB
320static inline int get16(const uint8_t **pp, const uint8_t *p_end)
321{
322 const uint8_t *p;
323 int c;
324
325 p = *pp;
326 if ((p + 1) >= p_end)
327 return -1;
328 c = (p[0] << 8) | p[1];
329 p += 2;
330 *pp = p;
331 return c;
332}
333
334/* read and allocate a DVB string preceeded by its length */
335static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
fe9cf0d4 336{
fe9cf0d4 337 int len;
5dbafeb7
FB
338 const uint8_t *p;
339 char *str;
fe9cf0d4 340
5dbafeb7
FB
341 p = *pp;
342 len = get8(&p, p_end);
343 if (len < 0)
344 return NULL;
345 if ((p + len) > p_end)
346 return NULL;
347 str = av_malloc(len + 1);
348 if (!str)
349 return NULL;
350 memcpy(str, p, len);
351 str[len] = '\0';
352 p += len;
353 *pp = p;
354 return str;
355}
356
115329f1 357static int parse_section_header(SectionHeader *h,
5dbafeb7
FB
358 const uint8_t **pp, const uint8_t *p_end)
359{
360 int val;
361
362 val = get8(pp, p_end);
363 if (val < 0)
364 return -1;
365 h->tid = val;
366 *pp += 2;
367 val = get16(pp, p_end);
368 if (val < 0)
369 return -1;
370 h->id = val;
371 val = get8(pp, p_end);
372 if (val < 0)
373 return -1;
374 h->version = (val >> 1) & 0x1f;
375 val = get8(pp, p_end);
376 if (val < 0)
377 return -1;
378 h->sec_num = val;
379 val = get8(pp, p_end);
380 if (val < 0)
381 return -1;
382 h->last_sec_num = val;
fe9cf0d4 383 return 0;
5dbafeb7
FB
384}
385
115329f1 386static MpegTSService *new_service(MpegTSContext *ts, int sid,
5dbafeb7
FB
387 char *provider_name, char *name)
388{
657ced8c
MN
389 MpegTSService *service=NULL;
390 int i;
5dbafeb7
FB
391
392#ifdef DEBUG_SI
f0ee3ea6 393 av_log(ts->stream, AV_LOG_DEBUG, "new_service: "
2ddb79be 394 "sid=0x%04x provider='%s' name='%s'\n",
5dbafeb7
FB
395 sid, provider_name, name);
396#endif
397
657ced8c
MN
398 for(i=0; i<ts->nb_services; i++)
399 if(ts->services[i]->sid == sid)
400 service= ts->services[i];
401
402 if(!service){
403 service = av_mallocz(sizeof(MpegTSService));
404 if (!service)
405 return NULL;
406 dynarray_add(&ts->services, &ts->nb_services, service);
407 }
5dbafeb7 408 service->sid = sid;
657ced8c
MN
409 assert((!provider_name) == (!name));
410 if(name){
411 av_free(service->provider_name);
412 av_free(service-> name);
413 service->provider_name = provider_name;
414 service-> name = name;
415 }
5dbafeb7
FB
416 return service;
417}
418
4d9a577e 419static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 420{
4d9a577e 421 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7 422 SectionHeader h1, *h = &h1;
c6ec28b1
MN
423 PESContext *pes;
424 AVStream *st;
425 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
426 int program_info_length, pcr_pid, pid, stream_type;
427 int desc_list_len, desc_len, desc_tag;
79396ac6 428 int comp_page = 0, anc_page = 0; /* initialize to kill warnings */
828bb5f1 429 char language[4] = {0}; /* initialize to kill warnings */
115329f1 430
5dbafeb7 431#ifdef DEBUG_SI
f0ee3ea6 432 av_log(ts->stream, AV_LOG_DEBUG, "PMT: len %i\n", section_len);
750f0e1f 433 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7
FB
434#endif
435 p_end = section + section_len - 4;
436 p = section;
437 if (parse_section_header(h, &p, p_end) < 0)
438 return;
439#ifdef DEBUG_SI
f0ee3ea6 440 av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x sec_num=%d/%d\n",
2ddb79be 441 h->id, h->sec_num, h->last_sec_num);
5dbafeb7 442#endif
90d13e30 443 if (h->tid != PMT_TID)
5dbafeb7
FB
444 return;
445
446 pcr_pid = get16(&p, p_end) & 0x1fff;
447 if (pcr_pid < 0)
448 return;
27f388aa 449 ts->pcr_pid = pcr_pid;
5dbafeb7 450#ifdef DEBUG_SI
f0ee3ea6 451 av_log(ts->stream, AV_LOG_DEBUG, "pcr_pid=0x%x\n", pcr_pid);
5dbafeb7
FB
452#endif
453 program_info_length = get16(&p, p_end) & 0xfff;
454 if (program_info_length < 0)
455 return;
456 p += program_info_length;
457 if (p >= p_end)
458 return;
459 for(;;) {
c6ec28b1
MN
460 language[0] = 0;
461 st = 0;
5dbafeb7
FB
462 stream_type = get8(&p, p_end);
463 if (stream_type < 0)
464 break;
465 pid = get16(&p, p_end) & 0x1fff;
466 if (pid < 0)
467 break;
c6ec28b1
MN
468 desc_list_len = get16(&p, p_end) & 0xfff;
469 if (desc_list_len < 0)
470 break;
471 desc_list_end = p + desc_list_len;
472 if (desc_list_end > p_end)
5dbafeb7 473 break;
c6ec28b1
MN
474 for(;;) {
475 desc_tag = get8(&p, desc_list_end);
476 if (desc_tag < 0)
477 break;
2eb1a65d
NS
478 if (stream_type == STREAM_TYPE_PRIVATE_DATA) {
479 if((desc_tag == 0x6A) || (desc_tag == 0x7A)) {
9d9b432a
MR
480 /*assume DVB AC-3 Audio*/
481 stream_type = STREAM_TYPE_AUDIO_AC3;
2eb1a65d
NS
482 } else if(desc_tag == 0x7B) {
483 /* DVB DTS audio */
484 stream_type = STREAM_TYPE_AUDIO_DTS;
485 }
9d9b432a 486 }
c6ec28b1
MN
487 desc_len = get8(&p, desc_list_end);
488 desc_end = p + desc_len;
489 if (desc_end > desc_list_end)
490 break;
491#ifdef DEBUG_SI
f0ee3ea6 492 av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
2ddb79be 493 desc_tag, desc_len);
c6ec28b1
MN
494#endif
495 switch(desc_tag) {
496 case DVB_SUBT_DESCID:
497 if (stream_type == STREAM_TYPE_PRIVATE_DATA)
498 stream_type = STREAM_TYPE_SUBTITLE_DVB;
499
500 language[0] = get8(&p, desc_end);
501 language[1] = get8(&p, desc_end);
502 language[2] = get8(&p, desc_end);
503 language[3] = 0;
504 get8(&p, desc_end);
505 comp_page = get16(&p, desc_end);
506 anc_page = get16(&p, desc_end);
507
508 break;
509 case 0x0a: /* ISO 639 language descriptor */
510 language[0] = get8(&p, desc_end);
511 language[1] = get8(&p, desc_end);
512 language[2] = get8(&p, desc_end);
513 language[3] = 0;
514 break;
515 default:
516 break;
517 }
518 p = desc_end;
519 }
520 p = desc_list_end;
5dbafeb7
FB
521
522#ifdef DEBUG_SI
f0ee3ea6 523 av_log(ts->stream, AV_LOG_DEBUG, "stream_type=%d pid=0x%x\n",
2ddb79be 524 stream_type, pid);
5dbafeb7
FB
525#endif
526
527 /* now create ffmpeg stream */
528 switch(stream_type) {
ce34182d
MN
529 case STREAM_TYPE_AUDIO_MPEG1:
530 case STREAM_TYPE_AUDIO_MPEG2:
531 case STREAM_TYPE_VIDEO_MPEG1:
532 case STREAM_TYPE_VIDEO_MPEG2:
278de475
MR
533 case STREAM_TYPE_VIDEO_MPEG4:
534 case STREAM_TYPE_VIDEO_H264:
cf10ee18 535 case STREAM_TYPE_VIDEO_VC1:
278de475 536 case STREAM_TYPE_AUDIO_AAC:
ec23a472 537 case STREAM_TYPE_AUDIO_AC3:
23c99253 538 case STREAM_TYPE_AUDIO_DTS:
c6ec28b1 539 case STREAM_TYPE_SUBTITLE_DVB:
90d13e30
MN
540 if(ts->pids[pid]){
541 assert(ts->pids[pid].type == MPEGTS_PES);
542 pes= ts->pids[pid]->u.pes_filter.opaque;
543 st= pes->st;
544 }else{
dbf6b678 545 pes = add_pes_stream(ts, pid, pcr_pid, stream_type);
28f03d04
MN
546 if (pes)
547 st = new_pes_av_stream(pes, 0);
90d13e30 548 }
04ec18b8 549 break;
5dbafeb7
FB
550 default:
551 /* we ignore the other streams */
552 break;
553 }
c6ec28b1
MN
554
555 if (st) {
556 if (language[0] != 0) {
557 st->language[0] = language[0];
558 st->language[1] = language[1];
559 st->language[2] = language[2];
560 st->language[3] = language[3];
561 }
562
563 if (stream_type == STREAM_TYPE_SUBTITLE_DVB) {
01f4895c 564 st->codec->sub_id = (anc_page << 16) | comp_page;
c6ec28b1
MN
565 }
566 }
5dbafeb7
FB
567 }
568 /* all parameters are there */
90d13e30 569 ts->stop_parse++;
4d9a577e 570 mpegts_close_filter(ts, filter);
5dbafeb7
FB
571}
572
4d9a577e 573static void pat_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 574{
4d9a577e 575 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7
FB
576 SectionHeader h1, *h = &h1;
577 const uint8_t *p, *p_end;
578 int sid, pmt_pid;
579
580#ifdef DEBUG_SI
f0ee3ea6 581 av_log(ts->stream, AV_LOG_DEBUG, "PAT:\n");
750f0e1f 582 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7
FB
583#endif
584 p_end = section + section_len - 4;
585 p = section;
586 if (parse_section_header(h, &p, p_end) < 0)
587 return;
588 if (h->tid != PAT_TID)
589 return;
590
591 for(;;) {
592 sid = get16(&p, p_end);
593 if (sid < 0)
594 break;
595 pmt_pid = get16(&p, p_end) & 0x1fff;
596 if (pmt_pid < 0)
597 break;
598#ifdef DEBUG_SI
f0ee3ea6 599 av_log(ts->stream, AV_LOG_DEBUG, "sid=0x%x pid=0x%x\n", sid, pmt_pid);
5dbafeb7
FB
600#endif
601 if (sid == 0x0000) {
602 /* NIT info */
603 } else {
ad39abcd 604 new_service(ts, sid, NULL, NULL);
90d13e30 605 ts->stop_parse--;
28f03d04 606 mpegts_open_section_filter(ts, pmt_pid, pmt_cb, ts, 1);
5dbafeb7
FB
607 }
608 }
609 /* not found */
90d13e30 610 ts->stop_parse++;
5dbafeb7 611
4d9a577e 612 mpegts_close_filter(ts, filter);
5dbafeb7
FB
613}
614
90d13e30 615static void mpegts_set_service(MpegTSContext *ts)
5dbafeb7 616{
4d9a577e 617 mpegts_open_section_filter(ts, PAT_PID,
5dbafeb7
FB
618 pat_cb, ts, 1);
619}
620
4d9a577e 621static void sdt_cb(MpegTSFilter *filter, const uint8_t *section, int section_len)
5dbafeb7 622{
4d9a577e 623 MpegTSContext *ts = filter->u.section_filter.opaque;
5dbafeb7
FB
624 SectionHeader h1, *h = &h1;
625 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
626 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
627 char *name, *provider_name;
628
629#ifdef DEBUG_SI
f0ee3ea6 630 av_log(ts->stream, AV_LOG_DEBUG, "SDT:\n");
750f0e1f 631 av_hex_dump_log(ts->stream, AV_LOG_DEBUG, (uint8_t *)section, section_len);
5dbafeb7
FB
632#endif
633
634 p_end = section + section_len - 4;
635 p = section;
636 if (parse_section_header(h, &p, p_end) < 0)
637 return;
638 if (h->tid != SDT_TID)
639 return;
640 onid = get16(&p, p_end);
641 if (onid < 0)
642 return;
643 val = get8(&p, p_end);
644 if (val < 0)
645 return;
646 for(;;) {
647 sid = get16(&p, p_end);
648 if (sid < 0)
649 break;
650 val = get8(&p, p_end);
651 if (val < 0)
652 break;
653 desc_list_len = get16(&p, p_end) & 0xfff;
654 if (desc_list_len < 0)
655 break;
656 desc_list_end = p + desc_list_len;
657 if (desc_list_end > p_end)
658 break;
659 for(;;) {
660 desc_tag = get8(&p, desc_list_end);
661 if (desc_tag < 0)
662 break;
663 desc_len = get8(&p, desc_list_end);
664 desc_end = p + desc_len;
665 if (desc_end > desc_list_end)
666 break;
667#ifdef DEBUG_SI
f0ee3ea6 668 av_log(ts->stream, AV_LOG_DEBUG, "tag: 0x%02x len=%d\n",
2ddb79be 669 desc_tag, desc_len);
5dbafeb7
FB
670#endif
671 switch(desc_tag) {
672 case 0x48:
673 service_type = get8(&p, p_end);
674 if (service_type < 0)
675 break;
676 provider_name = getstr8(&p, p_end);
677 if (!provider_name)
678 break;
679 name = getstr8(&p, p_end);
680 if (!name)
681 break;
682 new_service(ts, sid, provider_name, name);
683 break;
684 default:
685 break;
686 }
687 p = desc_end;
688 }
689 p = desc_list_end;
690 }
5dbafeb7
FB
691}
692
fc48fe84 693/* scan services in a transport stream by looking at the SDT */
7b49ce2e 694static void mpegts_scan_sdt(MpegTSContext *ts)
5dbafeb7 695{
37dfd633 696 mpegts_open_section_filter(ts, SDT_PID,
5dbafeb7
FB
697 sdt_cb, ts, 1);
698}
699
5dbafeb7
FB
700static int64_t get_pts(const uint8_t *p)
701{
702 int64_t pts;
703 int val;
704
705 pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
706 val = (p[1] << 8) | p[2];
707 pts |= (int64_t)(val >> 1) << 15;
708 val = (p[3] << 8) | p[4];
709 pts |= (int64_t)(val >> 1);
710 return pts;
fe9cf0d4
FB
711}
712
713/* return non zero if a packet could be constructed */
4d9a577e 714static void mpegts_push_data(MpegTSFilter *filter,
5dbafeb7 715 const uint8_t *buf, int buf_size, int is_start)
fe9cf0d4 716{
4d9a577e 717 PESContext *pes = filter->u.pes_filter.opaque;
b45a7a18 718 MpegTSContext *ts = pes->ts;
5dbafeb7 719 const uint8_t *p;
c6ec28b1 720 int len, code;
115329f1 721
b2292194
MN
722 if(!ts->pkt)
723 return;
724
fe9cf0d4 725 if (is_start) {
5dbafeb7
FB
726 pes->state = MPEGTS_HEADER;
727 pes->data_index = 0;
fe9cf0d4
FB
728 }
729 p = buf;
730 while (buf_size > 0) {
5dbafeb7 731 switch(pes->state) {
fe9cf0d4 732 case MPEGTS_HEADER:
5dbafeb7
FB
733 len = PES_START_SIZE - pes->data_index;
734 if (len > buf_size)
735 len = buf_size;
736 memcpy(pes->header + pes->data_index, p, len);
737 pes->data_index += len;
fe9cf0d4
FB
738 p += len;
739 buf_size -= len;
5dbafeb7 740 if (pes->data_index == PES_START_SIZE) {
fe9cf0d4
FB
741 /* we got all the PES or section header. We can now
742 decide */
743#if 0
750f0e1f 744 av_hex_dump_log(pes->stream, AV_LOG_DEBUG, pes->header, pes->data_index);
fe9cf0d4 745#endif
5dbafeb7
FB
746 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
747 pes->header[2] == 0x01) {
fe9cf0d4 748 /* it must be an mpeg2 PES stream */
5dbafeb7 749 code = pes->header[3] | 0x100;
fe9cf0d4 750 if (!((code >= 0x1c0 && code <= 0x1df) ||
ec23a472 751 (code >= 0x1e0 && code <= 0x1ef) ||
cf10ee18 752 (code == 0x1bd) || (code == 0x1fd)))
fe9cf0d4 753 goto skip;
5dbafeb7 754 if (!pes->st) {
fe9cf0d4 755 /* allocate stream */
c6ec28b1 756 new_pes_av_stream(pes, code);
fe9cf0d4 757 }
5dbafeb7
FB
758 pes->state = MPEGTS_PESHEADER_FILL;
759 pes->total_size = (pes->header[4] << 8) | pes->header[5];
760 /* NOTE: a zero total size means the PES size is
761 unbounded */
762 if (pes->total_size)
763 pes->total_size += 6;
764 pes->pes_header_size = pes->header[8] + 9;
fe9cf0d4
FB
765 } else {
766 /* otherwise, it should be a table */
767 /* skip packet */
768 skip:
5dbafeb7 769 pes->state = MPEGTS_SKIP;
fe9cf0d4
FB
770 continue;
771 }
772 }
773 break;
774 /**********************************************/
775 /* PES packing parsing */
776 case MPEGTS_PESHEADER_FILL:
5dbafeb7
FB
777 len = pes->pes_header_size - pes->data_index;
778 if (len > buf_size)
779 len = buf_size;
780 memcpy(pes->header + pes->data_index, p, len);
781 pes->data_index += len;
fe9cf0d4
FB
782 p += len;
783 buf_size -= len;
5dbafeb7
FB
784 if (pes->data_index == pes->pes_header_size) {
785 const uint8_t *r;
786 unsigned int flags;
787
788 flags = pes->header[7];
789 r = pes->header + 9;
790 pes->pts = AV_NOPTS_VALUE;
791 pes->dts = AV_NOPTS_VALUE;
792 if ((flags & 0xc0) == 0x80) {
793 pes->pts = get_pts(r);
794 r += 5;
795 } else if ((flags & 0xc0) == 0xc0) {
796 pes->pts = get_pts(r);
797 r += 5;
798 pes->dts = get_pts(r);
799 r += 5;
800 }
801 /* we got the full header. We parse it and get the payload */
802 pes->state = MPEGTS_PAYLOAD;
803 }
fe9cf0d4
FB
804 break;
805 case MPEGTS_PAYLOAD:
5dbafeb7
FB
806 if (pes->total_size) {
807 len = pes->total_size - pes->data_index;
808 if (len > buf_size)
809 len = buf_size;
810 } else {
811 len = buf_size;
812 }
fe9cf0d4 813 if (len > 0) {
5dbafeb7
FB
814 AVPacket *pkt = ts->pkt;
815 if (pes->st && av_new_packet(pkt, len) == 0) {
816 memcpy(pkt->data, p, len);
817 pkt->stream_index = pes->st->index;
818 pkt->pts = pes->pts;
2092bd75 819 pkt->dts = pes->dts;
5dbafeb7
FB
820 /* reset pts values */
821 pes->pts = AV_NOPTS_VALUE;
822 pes->dts = AV_NOPTS_VALUE;
823 ts->stop_parse = 1;
824 return;
fe9cf0d4 825 }
fe9cf0d4
FB
826 }
827 buf_size = 0;
828 break;
829 case MPEGTS_SKIP:
830 buf_size = 0;
831 break;
832 }
833 }
5dbafeb7
FB
834}
835
c6ec28b1
MN
836static AVStream* new_pes_av_stream(PESContext *pes, uint32_t code)
837{
838 AVStream *st;
839 int codec_type, codec_id;
840
841 switch(pes->stream_type){
842 case STREAM_TYPE_AUDIO_MPEG1:
843 case STREAM_TYPE_AUDIO_MPEG2:
844 codec_type = CODEC_TYPE_AUDIO;
845 codec_id = CODEC_ID_MP3;
846 break;
847 case STREAM_TYPE_VIDEO_MPEG1:
848 case STREAM_TYPE_VIDEO_MPEG2:
849 codec_type = CODEC_TYPE_VIDEO;
850 codec_id = CODEC_ID_MPEG2VIDEO;
851 break;
852 case STREAM_TYPE_VIDEO_MPEG4:
853 codec_type = CODEC_TYPE_VIDEO;
854 codec_id = CODEC_ID_MPEG4;
855 break;
856 case STREAM_TYPE_VIDEO_H264:
857 codec_type = CODEC_TYPE_VIDEO;
858 codec_id = CODEC_ID_H264;
859 break;
cf10ee18
NS
860 case STREAM_TYPE_VIDEO_VC1:
861 codec_type = CODEC_TYPE_VIDEO;
862 codec_id = CODEC_ID_VC1;
863 break;
c6ec28b1
MN
864 case STREAM_TYPE_AUDIO_AAC:
865 codec_type = CODEC_TYPE_AUDIO;
866 codec_id = CODEC_ID_AAC;
867 break;
868 case STREAM_TYPE_AUDIO_AC3:
869 codec_type = CODEC_TYPE_AUDIO;
870 codec_id = CODEC_ID_AC3;
871 break;
872 case STREAM_TYPE_AUDIO_DTS:
873 codec_type = CODEC_TYPE_AUDIO;
874 codec_id = CODEC_ID_DTS;
875 break;
876 case STREAM_TYPE_SUBTITLE_DVB:
877 codec_type = CODEC_TYPE_SUBTITLE;
878 codec_id = CODEC_ID_DVB_SUBTITLE;
879 break;
880 default:
881 if (code >= 0x1c0 && code <= 0x1df) {
882 codec_type = CODEC_TYPE_AUDIO;
883 codec_id = CODEC_ID_MP2;
884 } else if (code == 0x1bd) {
885 codec_type = CODEC_TYPE_AUDIO;
886 codec_id = CODEC_ID_AC3;
887 } else {
888 codec_type = CODEC_TYPE_VIDEO;
889 codec_id = CODEC_ID_MPEG1VIDEO;
890 }
891 break;
892 }
893 st = av_new_stream(pes->stream, pes->pid);
894 if (st) {
895 av_set_pts_info(st, 33, 1, 90000);
896 st->priv_data = pes;
01f4895c
MN
897 st->codec->codec_type = codec_type;
898 st->codec->codec_id = codec_id;
57004ff1 899 st->need_parsing = AVSTREAM_PARSE_FULL;
c6ec28b1
MN
900 pes->st = st;
901 }
902 return st;
903}
904
905
dbf6b678 906static PESContext *add_pes_stream(MpegTSContext *ts, int pid, int pcr_pid, int stream_type)
5dbafeb7 907{
5dbafeb7
FB
908 MpegTSFilter *tss;
909 PESContext *pes;
910
911 /* if no pid found, then add a pid context */
912 pes = av_mallocz(sizeof(PESContext));
913 if (!pes)
c6ec28b1 914 return 0;
b45a7a18
FB
915 pes->ts = ts;
916 pes->stream = ts->stream;
5dbafeb7 917 pes->pid = pid;
dbf6b678 918 pes->pcr_pid = pcr_pid;
278de475 919 pes->stream_type = stream_type;
5dbafeb7
FB
920 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
921 if (!tss) {
922 av_free(pes);
c6ec28b1 923 return 0;
5dbafeb7 924 }
c6ec28b1 925 return pes;
fe9cf0d4
FB
926}
927
5dbafeb7 928/* handle one TS packet */
b45a7a18 929static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
fe9cf0d4 930{
b45a7a18 931 AVFormatContext *s = ts->stream;
5dbafeb7
FB
932 MpegTSFilter *tss;
933 int len, pid, cc, cc_ok, afc, is_start;
934 const uint8_t *p, *p_end;
935
936 pid = ((packet[1] & 0x1f) << 8) | packet[2];
937 is_start = packet[1] & 0x40;
938 tss = ts->pids[pid];
939 if (ts->auto_guess && tss == NULL && is_start) {
dbf6b678 940 add_pes_stream(ts, pid, -1, 0);
5dbafeb7
FB
941 tss = ts->pids[pid];
942 }
943 if (!tss)
944 return;
945
946 /* continuity check (currently not used) */
947 cc = (packet[3] & 0xf);
948 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
949 tss->last_cc = cc;
115329f1 950
5dbafeb7
FB
951 /* skip adaptation field */
952 afc = (packet[3] >> 4) & 3;
953 p = packet + 4;
954 if (afc == 0) /* reserved value */
955 return;
956 if (afc == 2) /* adaptation field only */
957 return;
958 if (afc == 3) {
959 /* skip adapation field */
960 p += p[0] + 1;
961 }
962 /* if past the end of packet, ignore */
963 p_end = packet + TS_PACKET_SIZE;
964 if (p >= p_end)
965 return;
115329f1 966
5dbafeb7
FB
967 if (tss->type == MPEGTS_SECTION) {
968 if (is_start) {
969 /* pointer field present */
970 len = *p++;
971 if (p + len > p_end)
972 return;
973 if (len && cc_ok) {
27bb1ed3 974 /* write remaining section bytes */
115329f1 975 write_section_data(s, tss,
5dbafeb7 976 p, len, 0);
27bb1ed3
WG
977 /* check whether filter has been closed */
978 if (!ts->pids[pid])
979 return;
5dbafeb7
FB
980 }
981 p += len;
982 if (p < p_end) {
115329f1 983 write_section_data(s, tss,
5dbafeb7
FB
984 p, p_end - p, 1);
985 }
986 } else {
987 if (cc_ok) {
115329f1 988 write_section_data(s, tss,
5dbafeb7 989 p, p_end - p, 0);
fc48fe84 990 }
5dbafeb7
FB
991 }
992 } else {
4d9a577e 993 tss->u.pes_filter.pes_cb(tss,
5dbafeb7
FB
994 p, p_end - p, is_start);
995 }
996}
997
fc48fe84
FB
998/* XXX: try to find a better synchro over several packets (use
999 get_packet_size() ?) */
b45a7a18 1000static int mpegts_resync(ByteIOContext *pb)
fc48fe84 1001{
fc48fe84
FB
1002 int c, i;
1003
1004 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
1005 c = url_fgetc(pb);
1006 if (c < 0)
1007 return -1;
1008 if (c == 0x47) {
1009 url_fseek(pb, -1, SEEK_CUR);
1010 return 0;
1011 }
1012 }
1013 /* no sync found */
1014 return -1;
1015}
1016
b45a7a18
FB
1017/* return -1 if error or EOF. Return 0 if OK. */
1018static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
5dbafeb7 1019{
b45a7a18
FB
1020 int skip, len;
1021
1022 for(;;) {
1023 len = get_buffer(pb, buf, TS_PACKET_SIZE);
1024 if (len != TS_PACKET_SIZE)
1025 return AVERROR_IO;
1026 /* check paquet sync byte */
1027 if (buf[0] != 0x47) {
1028 /* find a new packet start */
1029 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
1030 if (mpegts_resync(pb) < 0)
1031 return AVERROR_INVALIDDATA;
1032 else
1033 continue;
1034 } else {
1035 skip = raw_packet_size - TS_PACKET_SIZE;
1036 if (skip > 0)
1037 url_fskip(pb, skip);
1038 break;
1039 }
1040 }
1041 return 0;
1042}
1043
1044static int handle_packets(MpegTSContext *ts, int nb_packets)
1045{
1046 AVFormatContext *s = ts->stream;
5dbafeb7 1047 ByteIOContext *pb = &s->pb;
b45a7a18
FB
1048 uint8_t packet[TS_PACKET_SIZE];
1049 int packet_num, ret;
5dbafeb7
FB
1050
1051 ts->stop_parse = 0;
1052 packet_num = 0;
fe9cf0d4 1053 for(;;) {
90d13e30 1054 if (ts->stop_parse>0)
5dbafeb7
FB
1055 break;
1056 packet_num++;
1057 if (nb_packets != 0 && packet_num >= nb_packets)
1058 break;
b45a7a18
FB
1059 ret = read_packet(pb, packet, ts->raw_packet_size);
1060 if (ret != 0)
1061 return ret;
1062 handle_packet(ts, packet);
5dbafeb7
FB
1063 }
1064 return 0;
1065}
fe9cf0d4 1066
5dbafeb7
FB
1067static int mpegts_probe(AVProbeData *p)
1068{
fc48fe84 1069#if 1
a0b8f70c 1070 const int size= p->buf_size;
5d4d67e0 1071 int score, fec_score, dvhs_score;
a0b8f70c 1072#define CHECK_COUNT 10
115329f1 1073
a0b8f70c
MN
1074 if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
1075 return -1;
115329f1 1076
a0b8f70c 1077 score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
5d4d67e0 1078 dvhs_score = analyze(p->buf, TS_DVHS_PACKET_SIZE *CHECK_COUNT, TS_DVHS_PACKET_SIZE, NULL);
a0b8f70c 1079 fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
5d4d67e0 1080// av_log(NULL, AV_LOG_DEBUG, "score: %d, dvhs_score: %d, fec_score: %d \n", score, dvhs_score, fec_score);
115329f1 1081
a0b8f70c 1082// we need a clear definition for the returned score otherwise things will become messy sooner or later
5d4d67e0
NS
1083 if (score > fec_score && score > dvhs_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1084 else if(dvhs_score > score && dvhs_score > fec_score && dvhs_score > 6) return AVPROBE_SCORE_MAX + dvhs_score - CHECK_COUNT;
a0b8f70c
MN
1085 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1086 else return -1;
fc48fe84
FB
1087#else
1088 /* only use the extension for safer guess */
1089 if (match_ext(p->filename, "ts"))
1090 return AVPROBE_SCORE_MAX;
1091 else
1092 return 0;
1093#endif
5dbafeb7
FB
1094}
1095
b45a7a18
FB
1096/* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
1097 (-1) if not available */
115329f1 1098static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
b45a7a18
FB
1099 const uint8_t *packet)
1100{
1101 int afc, len, flags;
1102 const uint8_t *p;
1103 unsigned int v;
1104
1105 afc = (packet[3] >> 4) & 3;
1106 if (afc <= 1)
1107 return -1;
1108 p = packet + 4;
1109 len = p[0];
1110 p++;
1111 if (len == 0)
1112 return -1;
1113 flags = *p++;
1114 len--;
1115 if (!(flags & 0x10))
1116 return -1;
1117 if (len < 6)
1118 return -1;
1119 v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1120 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1121 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1122 return 0;
1123}
1124
5dbafeb7
FB
1125static int mpegts_read_header(AVFormatContext *s,
1126 AVFormatParameters *ap)
1127{
1128 MpegTSContext *ts = s->priv_data;
1129 ByteIOContext *pb = &s->pb;
1130 uint8_t buf[1024];
4dc8a963 1131 int len;
5dbafeb7 1132 int64_t pos;
e05655fb 1133
b45a7a18 1134 if (ap) {
b45a7a18 1135 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
38c48be2
MN
1136 if(ap->mpeg2ts_raw){
1137 av_log(s, AV_LOG_ERROR, "use mpegtsraw_demuxer!\n");
1138 return -1;
1139 }
b45a7a18
FB
1140 }
1141
5dbafeb7
FB
1142 /* read the first 1024 bytes to get packet size */
1143 pos = url_ftell(pb);
1144 len = get_buffer(pb, buf, sizeof(buf));
1145 if (len != sizeof(buf))
1146 goto fail;
1147 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1148 if (ts->raw_packet_size <= 0)
1149 goto fail;
b45a7a18 1150 ts->stream = s;
5dbafeb7 1151 ts->auto_guess = 0;
5dbafeb7 1152
38c48be2 1153 if (s->iformat == &mpegts_demuxer) {
b45a7a18 1154 /* normal demux */
fc48fe84 1155
a45ec414
BF
1156 /* first do a scaning to get all the services */
1157 url_fseek(pb, pos, SEEK_SET);
1158 mpegts_scan_sdt(ts);
115329f1 1159
28f03d04 1160 mpegts_set_service(ts);
115329f1 1161
28f03d04 1162 handle_packets(ts, s->probesize);
a45ec414 1163 /* if could not find service, enable auto_guess */
115329f1 1164
a45ec414 1165 ts->auto_guess = 1;
115329f1 1166
b45a7a18 1167#ifdef DEBUG_SI
a45ec414 1168 av_log(ts->stream, AV_LOG_DEBUG, "tuning done\n");
b45a7a18 1169#endif
b45a7a18
FB
1170 s->ctx_flags |= AVFMTCTX_NOHEADER;
1171 } else {
1172 AVStream *st;
1173 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1174 int64_t pcrs[2], pcr_h;
1175 int packet_count[2];
1176 uint8_t packet[TS_PACKET_SIZE];
115329f1 1177
b45a7a18 1178 /* only read packets */
115329f1 1179
b45a7a18
FB
1180 st = av_new_stream(s, 0);
1181 if (!st)
1182 goto fail;
9ee91c2f 1183 av_set_pts_info(st, 60, 1, 27000000);
01f4895c
MN
1184 st->codec->codec_type = CODEC_TYPE_DATA;
1185 st->codec->codec_id = CODEC_ID_MPEG2TS;
115329f1 1186
b45a7a18
FB
1187 /* we iterate until we find two PCRs to estimate the bitrate */
1188 pcr_pid = -1;
1189 nb_pcrs = 0;
1190 nb_packets = 0;
1191 for(;;) {
1192 ret = read_packet(&s->pb, packet, ts->raw_packet_size);
1193 if (ret < 0)
1194 return -1;
1195 pid = ((packet[1] & 0x1f) << 8) | packet[2];
1196 if ((pcr_pid == -1 || pcr_pid == pid) &&
1197 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1198 pcr_pid = pid;
1199 packet_count[nb_pcrs] = nb_packets;
1200 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1201 nb_pcrs++;
1202 if (nb_pcrs >= 2)
1203 break;
1204 }
1205 nb_packets++;
1206 }
27f388aa 1207 ts->pcr_pid = pcr_pid;
5dbafeb7 1208
b45a7a18
FB
1209 /* NOTE1: the bitrate is computed without the FEC */
1210 /* NOTE2: it is only the bitrate of the start of the stream */
1211 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1212 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1213 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
01f4895c 1214 st->codec->bit_rate = s->bit_rate;
c0df9d75 1215 st->start_time = ts->cur_pcr;
b45a7a18 1216#if 0
f0ee3ea6 1217 av_log(ts->stream, AV_LOG_DEBUG, "start=%0.3f pcr=%0.3f incr=%d\n",
b45a7a18 1218 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
5dbafeb7 1219#endif
fe9cf0d4 1220 }
5dbafeb7
FB
1221
1222 url_fseek(pb, pos, SEEK_SET);
fe9cf0d4 1223 return 0;
5dbafeb7
FB
1224 fail:
1225 return -1;
1226}
1227
b45a7a18
FB
1228#define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1229
1230static int mpegts_raw_read_packet(AVFormatContext *s,
1231 AVPacket *pkt)
1232{
1233 MpegTSContext *ts = s->priv_data;
1234 int ret, i;
1235 int64_t pcr_h, next_pcr_h, pos;
1236 int pcr_l, next_pcr_l;
1237 uint8_t pcr_buf[12];
1238
1239 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
8fa36ae0 1240 return AVERROR(ENOMEM);
2692067a 1241 pkt->pos= url_ftell(&s->pb);
b45a7a18
FB
1242 ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
1243 if (ret < 0) {
1244 av_free_packet(pkt);
1245 return ret;
1246 }
1247 if (ts->mpeg2ts_compute_pcr) {
1248 /* compute exact PCR for each packet */
1249 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1250 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1251 pos = url_ftell(&s->pb);
1252 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1253 url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1254 get_buffer(&s->pb, pcr_buf, 12);
1255 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1256 /* XXX: not precise enough */
115329f1 1257 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
b45a7a18
FB
1258 (i + 1);
1259 break;
1260 }
1261 }
1262 url_fseek(&s->pb, pos, SEEK_SET);
1263 /* no next PCR found: we use previous increment */
1264 ts->cur_pcr = pcr_h * 300 + pcr_l;
1265 }
1266 pkt->pts = ts->cur_pcr;
1267 pkt->duration = ts->pcr_incr;
1268 ts->cur_pcr += ts->pcr_incr;
1269 }
1270 pkt->stream_index = 0;
1271 return 0;
1272}
1273
5dbafeb7
FB
1274static int mpegts_read_packet(AVFormatContext *s,
1275 AVPacket *pkt)
1276{
1277 MpegTSContext *ts = s->priv_data;
b45a7a18 1278
38c48be2
MN
1279 ts->pkt = pkt;
1280 return handle_packets(ts, 0);
fe9cf0d4
FB
1281}
1282
1283static int mpegts_read_close(AVFormatContext *s)
1284{
1285 MpegTSContext *ts = s->priv_data;
1286 int i;
1287 for(i=0;i<NB_PID_MAX;i++)
ec7d0d2e 1288 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
00a6b92b
MR
1289
1290 for(i = 0; i < ts->nb_services; i++){
1291 av_free(ts->services[i]->provider_name);
1292 av_free(ts->services[i]->name);
1293 av_free(ts->services[i]);
1294 }
1295 av_freep(&ts->services);
1296
fe9cf0d4
FB
1297 return 0;
1298}
1299
115329f1 1300static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
cdd5034f 1301 int64_t *ppos, int64_t pos_limit)
27f388aa
FB
1302{
1303 MpegTSContext *ts = s->priv_data;
1304 int64_t pos, timestamp;
1305 uint8_t buf[TS_PACKET_SIZE];
dbf6b678 1306 int pcr_l, pcr_pid = ((PESContext*)s->streams[stream_index]->priv_data)->pcr_pid;
cdd5034f
MN
1307 const int find_next= 1;
1308 pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
27f388aa
FB
1309 if (find_next) {
1310 for(;;) {
1311 url_fseek(&s->pb, pos, SEEK_SET);
1312 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1313 return AV_NOPTS_VALUE;
dbf6b678 1314 if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
27f388aa
FB
1315 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1316 break;
1317 }
1318 pos += ts->raw_packet_size;
1319 }
1320 } else {
1321 for(;;) {
1322 pos -= ts->raw_packet_size;
1323 if (pos < 0)
1324 return AV_NOPTS_VALUE;
1325 url_fseek(&s->pb, pos, SEEK_SET);
1326 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1327 return AV_NOPTS_VALUE;
dbf6b678 1328 if ((pcr_pid < 0 || (((buf[1] & 0x1f) << 8) | buf[2]) == pcr_pid) &&
27f388aa
FB
1329 parse_pcr(&timestamp, &pcr_l, buf) == 0) {
1330 break;
1331 }
1332 }
1333 }
1334 *ppos = pos;
27f388aa 1335
cdd5034f 1336 return timestamp;
27f388aa
FB
1337}
1338
3ba1438d 1339static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
e05655fb
MN
1340 MpegTSContext *ts = s->priv_data;
1341 uint8_t buf[TS_PACKET_SIZE];
1342 int64_t pos;
1343
3ba1438d 1344 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
e05655fb
MN
1345 return -1;
1346
1347 pos= url_ftell(&s->pb);
1348
1349 for(;;) {
1350 url_fseek(&s->pb, pos, SEEK_SET);
1351 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1352 return -1;
1353// pid = ((buf[1] & 0x1f) << 8) | buf[2];
1354 if(buf[1] & 0x40) break;
1355 pos += ts->raw_packet_size;
115329f1 1356 }
e05655fb
MN
1357 url_fseek(&s->pb, pos, SEEK_SET);
1358
1359 return 0;
1360}
1361
b45a7a18
FB
1362/**************************************************************/
1363/* parsing functions - called from other demuxers such as RTP */
1364
1365MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1366{
1367 MpegTSContext *ts;
115329f1 1368
b45a7a18
FB
1369 ts = av_mallocz(sizeof(MpegTSContext));
1370 if (!ts)
1371 return NULL;
1372 /* no stream case, currently used by RTP */
1373 ts->raw_packet_size = TS_PACKET_SIZE;
1374 ts->stream = s;
1375 ts->auto_guess = 1;
1376 return ts;
1377}
1378
1379/* return the consumed length if a packet was output, or -1 if no
1380 packet is output */
1381int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1382 const uint8_t *buf, int len)
1383{
1384 int len1;
1385
1386 len1 = len;
1387 ts->pkt = pkt;
1388 ts->stop_parse = 0;
1389 for(;;) {
90d13e30 1390 if (ts->stop_parse>0)
b45a7a18
FB
1391 break;
1392 if (len < TS_PACKET_SIZE)
1393 return -1;
1394 if (buf[0] != 0x47) {
550f0a9b 1395 buf++;
b45a7a18
FB
1396 len--;
1397 } else {
1398 handle_packet(ts, buf);
1399 buf += TS_PACKET_SIZE;
1400 len -= TS_PACKET_SIZE;
1401 }
1402 }
1403 return len1 - len;
1404}
1405
1406void mpegts_parse_close(MpegTSContext *ts)
1407{
1408 int i;
1409
1410 for(i=0;i<NB_PID_MAX;i++)
1411 av_free(ts->pids[i]);
1412 av_free(ts);
1413}
1414
d2a067d1 1415AVInputFormat mpegts_demuxer = {
fe9cf0d4
FB
1416 "mpegts",
1417 "MPEG2 transport stream format",
1418 sizeof(MpegTSContext),
1419 mpegts_probe,
1420 mpegts_read_header,
1421 mpegts_read_packet,
1422 mpegts_read_close,
e05655fb 1423 read_seek,
cdd5034f 1424 mpegts_get_pcr,
b45a7a18 1425 .flags = AVFMT_SHOW_IDS,
fe9cf0d4 1426};
38c48be2
MN
1427
1428AVInputFormat mpegtsraw_demuxer = {
1429 "mpegtsraw",
1430 "MPEG2 raw transport stream format",
1431 sizeof(MpegTSContext),
1432 mpegts_probe,
1433 mpegts_read_header,
1434 mpegts_raw_read_packet,
1435 mpegts_read_close,
1436 read_seek,
1437 mpegts_get_pcr,
1438 .flags = AVFMT_SHOW_IDS,
1439};