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