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