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