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