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