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