2 * MPEG2 transport stream (aka DVB) demux
3 * Copyright (c) 2002-2003 Fabrice Bellard.
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.
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.
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
26 /* 1.0 second at 24Mbit/s */
27 #define MAX_SCAN_PACKETS 32000
29 /* maximum size in which we look for synchronisation if
30 synchronisation is lost */
31 #define MAX_RESYNC_SIZE 4096
33 static int add_pes_stream(MpegTSContext *ts, int pid, int stream_type);
35 enum MpegTSFilterType {
40 typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
42 typedef struct MpegTSPESFilter {
47 typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
49 typedef void SetServiceCallback(void *opaque, int ret);
51 typedef struct MpegTSSectionFilter {
56 int end_of_section_reached:1;
57 SectionCallback *section_cb;
59 } MpegTSSectionFilter;
61 typedef struct MpegTSFilter {
63 int last_cc; /* last cc code (-1 if first packet) */
64 enum MpegTSFilterType type;
66 MpegTSPESFilter pes_filter;
67 MpegTSSectionFilter section_filter;
71 typedef struct MpegTSService {
78 struct MpegTSContext {
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 */
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 */
88 /* used to estimate the exact PCR */
93 /* data needed to handle file based ts */
94 int stop_parse; /* stop parsing loop */
95 AVPacket *pkt; /* packet containing av data */
97 /******************************************/
98 /* private mpegts data */
100 MpegTSFilter *sdt_filter;
102 MpegTSService **services;
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;
111 MpegTSFilter *pids[NB_PID_MAX];
114 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
115 const uint8_t *buf, int buf_size, int is_start)
117 MpegTSSectionFilter *tss = &tss1->u.section_filter;
121 memcpy(tss->section_buf, buf, buf_size);
122 tss->section_index = buf_size;
123 tss->section_h_size = -1;
124 tss->end_of_section_reached = 0;
126 if (tss->end_of_section_reached)
128 len = 4096 - tss->section_index;
131 memcpy(tss->section_buf + tss->section_index, buf, len);
132 tss->section_index += len;
135 /* compute section length if possible */
136 if (tss->section_h_size == -1 && tss->section_index >= 3) {
137 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
140 tss->section_h_size = len;
143 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
144 tss->end_of_section_reached = 1;
145 if (!tss->check_crc ||
146 mpegts_crc32(tss->section_buf, tss->section_h_size) == 0)
147 tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
151 MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
152 SectionCallback *section_cb, void *opaque,
156 MpegTSFilter *filter;
157 MpegTSSectionFilter *sec;
160 printf("Filter: pid=0x%x\n", pid);
162 if (pid >= NB_PID_MAX || ts->pids[pid])
164 filter = av_mallocz(sizeof(MpegTSFilter));
167 ts->pids[pid] = filter;
168 filter->type = MPEGTS_SECTION;
170 filter->last_cc = -1;
171 sec = &filter->u.section_filter;
172 sec->section_cb = section_cb;
173 sec->opaque = opaque;
174 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
175 sec->check_crc = check_crc;
176 if (!sec->section_buf) {
183 MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
187 MpegTSFilter *filter;
188 MpegTSPESFilter *pes;
190 if (pid >= NB_PID_MAX || ts->pids[pid])
192 filter = av_mallocz(sizeof(MpegTSFilter));
195 ts->pids[pid] = filter;
196 filter->type = MPEGTS_PES;
198 filter->last_cc = -1;
199 pes = &filter->u.pes_filter;
200 pes->pes_cb = pes_cb;
201 pes->opaque = opaque;
205 void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
210 if (filter->type == MPEGTS_SECTION)
211 av_freep(&filter->u.section_filter.section_buf);
212 else if (filter->type == MPEGTS_PES)
213 av_freep(&filter->u.pes_filter.opaque);
216 ts->pids[pid] = NULL;
219 static int analyze(const uint8_t *buf, int size, int packet_size, int *index){
220 int stat[packet_size];
225 memset(stat, 0, packet_size*sizeof(int));
227 for(x=i=0; i<size; i++){
230 if(stat[x] > best_score){
237 if(x == packet_size) x= 0;
243 /* autodetect fec presence. Must have at least 1024 bytes */
244 static int get_packet_size(const uint8_t *buf, int size)
246 int score, fec_score;
248 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
251 score = analyze(buf, size, TS_PACKET_SIZE, NULL);
252 fec_score= analyze(buf, size, TS_FEC_PACKET_SIZE, NULL);
253 // av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
255 if (score > fec_score) return TS_PACKET_SIZE;
256 else if(score < fec_score) return TS_FEC_PACKET_SIZE;
260 typedef struct SectionHeader {
265 uint8_t last_sec_num;
268 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
281 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
287 if ((p + 1) >= p_end)
289 c = (p[0] << 8) | p[1];
295 /* read and allocate a DVB string preceeded by its length */
296 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
303 len = get8(&p, p_end);
306 if ((p + len) > p_end)
308 str = av_malloc(len + 1);
318 static int parse_section_header(SectionHeader *h,
319 const uint8_t **pp, const uint8_t *p_end)
323 val = get8(pp, p_end);
328 val = get16(pp, p_end);
332 val = get8(pp, p_end);
335 h->version = (val >> 1) & 0x1f;
336 val = get8(pp, p_end);
340 val = get8(pp, p_end);
343 h->last_sec_num = val;
347 static MpegTSService *new_service(MpegTSContext *ts, int sid,
348 char *provider_name, char *name)
350 MpegTSService *service;
353 printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
354 sid, provider_name, name);
357 service = av_mallocz(sizeof(MpegTSService));
361 service->provider_name = provider_name;
362 service->name = name;
363 dynarray_add(&ts->services, &ts->nb_services, service);
367 static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
369 MpegTSContext *ts = opaque;
370 SectionHeader h1, *h = &h1;
371 const uint8_t *p, *p_end;
372 int program_info_length, pcr_pid, pid, stream_type, desc_length;
376 av_hex_dump(stdout, (uint8_t *)section, section_len);
378 p_end = section + section_len - 4;
380 if (parse_section_header(h, &p, p_end) < 0)
383 printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
385 if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
388 pcr_pid = get16(&p, p_end) & 0x1fff;
391 ts->pcr_pid = pcr_pid;
393 printf("pcr_pid=0x%x\n", pcr_pid);
395 program_info_length = get16(&p, p_end) & 0xfff;
396 if (program_info_length < 0)
398 p += program_info_length;
402 stream_type = get8(&p, p_end);
405 pid = get16(&p, p_end) & 0x1fff;
408 desc_length = get16(&p, p_end) & 0xfff;
416 printf("stream_type=%d pid=0x%x\n", stream_type, pid);
419 /* now create ffmpeg stream */
420 switch(stream_type) {
421 case STREAM_TYPE_AUDIO_MPEG1:
422 case STREAM_TYPE_AUDIO_MPEG2:
423 case STREAM_TYPE_VIDEO_MPEG1:
424 case STREAM_TYPE_VIDEO_MPEG2:
425 case STREAM_TYPE_VIDEO_MPEG4:
426 case STREAM_TYPE_VIDEO_H264:
427 case STREAM_TYPE_AUDIO_AAC:
428 case STREAM_TYPE_AUDIO_AC3:
429 case STREAM_TYPE_AUDIO_DTS:
430 add_pes_stream(ts, pid, stream_type);
433 /* we ignore the other streams */
437 /* all parameters are there */
438 ts->set_service_cb(ts->set_service_opaque, 0);
439 mpegts_close_filter(ts, ts->pmt_filter);
440 ts->pmt_filter = NULL;
443 static void pat_cb(void *opaque, const uint8_t *section, int section_len)
445 MpegTSContext *ts = opaque;
446 SectionHeader h1, *h = &h1;
447 const uint8_t *p, *p_end;
452 av_hex_dump(stdout, (uint8_t *)section, section_len);
454 p_end = section + section_len - 4;
456 if (parse_section_header(h, &p, p_end) < 0)
458 if (h->tid != PAT_TID)
462 sid = get16(&p, p_end);
465 pmt_pid = get16(&p, p_end) & 0x1fff;
469 printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
474 if (ts->req_sid == sid) {
475 ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
482 ts->set_service_cb(ts->set_service_opaque, -1);
485 mpegts_close_filter(ts, ts->pat_filter);
486 ts->pat_filter = NULL;
489 /* add all services found in the PAT */
490 static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
492 MpegTSContext *ts = opaque;
493 SectionHeader h1, *h = &h1;
494 const uint8_t *p, *p_end;
496 char *provider_name, *name;
501 av_hex_dump(stdout, (uint8_t *)section, section_len);
503 p_end = section + section_len - 4;
505 if (parse_section_header(h, &p, p_end) < 0)
507 if (h->tid != PAT_TID)
511 sid = get16(&p, p_end);
514 pmt_pid = get16(&p, p_end) & 0x1fff;
518 printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
523 /* add the service with a dummy name */
524 snprintf(buf, sizeof(buf), "Service %x\n", sid);
525 name = av_strdup(buf);
526 provider_name = av_strdup("");
527 if (name && provider_name) {
528 new_service(ts, sid, provider_name, name);
531 av_freep(&provider_name);
538 mpegts_close_filter(ts, ts->pat_filter);
539 ts->pat_filter = NULL;
542 void mpegts_set_service(MpegTSContext *ts, int sid,
543 SetServiceCallback *set_service_cb, void *opaque)
545 ts->set_service_cb = set_service_cb;
546 ts->set_service_opaque = opaque;
548 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
552 static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
554 MpegTSContext *ts = opaque;
555 SectionHeader h1, *h = &h1;
556 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
557 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
558 char *name, *provider_name;
562 av_hex_dump(stdout, (uint8_t *)section, section_len);
565 p_end = section + section_len - 4;
567 if (parse_section_header(h, &p, p_end) < 0)
569 if (h->tid != SDT_TID)
571 onid = get16(&p, p_end);
574 val = get8(&p, p_end);
578 sid = get16(&p, p_end);
581 val = get8(&p, p_end);
584 desc_list_len = get16(&p, p_end) & 0xfff;
585 if (desc_list_len < 0)
587 desc_list_end = p + desc_list_len;
588 if (desc_list_end > p_end)
591 desc_tag = get8(&p, desc_list_end);
594 desc_len = get8(&p, desc_list_end);
595 desc_end = p + desc_len;
596 if (desc_end > desc_list_end)
599 printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
603 service_type = get8(&p, p_end);
604 if (service_type < 0)
606 provider_name = getstr8(&p, p_end);
609 name = getstr8(&p, p_end);
612 new_service(ts, sid, provider_name, name);
624 mpegts_close_filter(ts, ts->sdt_filter);
625 ts->sdt_filter = NULL;
628 /* scan services in a transport stream by looking at the SDT */
629 void mpegts_scan_sdt(MpegTSContext *ts)
631 ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
635 /* scan services in a transport stream by looking at the PAT (better
637 void mpegts_scan_pat(MpegTSContext *ts)
639 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
643 /* TS stream handling */
647 MPEGTS_PESHEADER_FILL,
652 /* enough for PES header + length */
653 #define PES_START_SIZE 9
654 #define MAX_PES_HEADER_SIZE (9 + 255)
656 typedef struct PESContext {
660 AVFormatContext *stream;
662 enum MpegTSState state;
663 /* used to get the format */
668 uint8_t header[MAX_PES_HEADER_SIZE];
671 static int64_t get_pts(const uint8_t *p)
676 pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
677 val = (p[1] << 8) | p[2];
678 pts |= (int64_t)(val >> 1) << 15;
679 val = (p[3] << 8) | p[4];
680 pts |= (int64_t)(val >> 1);
684 /* return non zero if a packet could be constructed */
685 static void mpegts_push_data(void *opaque,
686 const uint8_t *buf, int buf_size, int is_start)
688 PESContext *pes = opaque;
689 MpegTSContext *ts = pes->ts;
692 int len, code, codec_type, codec_id;
695 pes->state = MPEGTS_HEADER;
699 while (buf_size > 0) {
702 len = PES_START_SIZE - pes->data_index;
705 memcpy(pes->header + pes->data_index, p, len);
706 pes->data_index += len;
709 if (pes->data_index == PES_START_SIZE) {
710 /* we got all the PES or section header. We can now
713 av_hex_dump(pes->header, pes->data_index);
715 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
716 pes->header[2] == 0x01) {
717 /* it must be an mpeg2 PES stream */
718 code = pes->header[3] | 0x100;
719 if (!((code >= 0x1c0 && code <= 0x1df) ||
720 (code >= 0x1e0 && code <= 0x1ef) ||
724 /* allocate stream */
725 switch(pes->stream_type){
726 case STREAM_TYPE_AUDIO_MPEG1:
727 case STREAM_TYPE_AUDIO_MPEG2:
728 codec_type = CODEC_TYPE_AUDIO;
729 codec_id = CODEC_ID_MP3;
731 case STREAM_TYPE_VIDEO_MPEG1:
732 case STREAM_TYPE_VIDEO_MPEG2:
733 codec_type = CODEC_TYPE_VIDEO;
734 codec_id = CODEC_ID_MPEG2VIDEO;
736 case STREAM_TYPE_VIDEO_MPEG4:
737 codec_type = CODEC_TYPE_VIDEO;
738 codec_id = CODEC_ID_MPEG4;
740 case STREAM_TYPE_VIDEO_H264:
741 codec_type = CODEC_TYPE_VIDEO;
742 codec_id = CODEC_ID_H264;
744 case STREAM_TYPE_AUDIO_AAC:
745 codec_type = CODEC_TYPE_AUDIO;
746 codec_id = CODEC_ID_AAC;
748 case STREAM_TYPE_AUDIO_AC3:
749 codec_type = CODEC_TYPE_AUDIO;
750 codec_id = CODEC_ID_AC3;
752 case STREAM_TYPE_AUDIO_DTS:
753 codec_type = CODEC_TYPE_AUDIO;
754 codec_id = CODEC_ID_DTS;
757 if (code >= 0x1c0 && code <= 0x1df) {
758 codec_type = CODEC_TYPE_AUDIO;
759 codec_id = CODEC_ID_MP2;
760 } else if (code == 0x1bd) {
761 codec_type = CODEC_TYPE_AUDIO;
762 codec_id = CODEC_ID_AC3;
764 codec_type = CODEC_TYPE_VIDEO;
765 codec_id = CODEC_ID_MPEG1VIDEO;
769 st = av_new_stream(pes->stream, pes->pid);
771 av_set_pts_info(st, 33, 1, 90000);
773 st->codec.codec_type = codec_type;
774 st->codec.codec_id = codec_id;
775 st->need_parsing = 1;
779 pes->state = MPEGTS_PESHEADER_FILL;
780 pes->total_size = (pes->header[4] << 8) | pes->header[5];
781 /* NOTE: a zero total size means the PES size is
784 pes->total_size += 6;
785 pes->pes_header_size = pes->header[8] + 9;
787 /* otherwise, it should be a table */
790 pes->state = MPEGTS_SKIP;
795 /**********************************************/
796 /* PES packing parsing */
797 case MPEGTS_PESHEADER_FILL:
798 len = pes->pes_header_size - pes->data_index;
801 memcpy(pes->header + pes->data_index, p, len);
802 pes->data_index += len;
805 if (pes->data_index == pes->pes_header_size) {
809 flags = pes->header[7];
811 pes->pts = AV_NOPTS_VALUE;
812 pes->dts = AV_NOPTS_VALUE;
813 if ((flags & 0xc0) == 0x80) {
814 pes->pts = get_pts(r);
816 } else if ((flags & 0xc0) == 0xc0) {
817 pes->pts = get_pts(r);
819 pes->dts = get_pts(r);
822 /* we got the full header. We parse it and get the payload */
823 pes->state = MPEGTS_PAYLOAD;
827 if (pes->total_size) {
828 len = pes->total_size - pes->data_index;
835 AVPacket *pkt = ts->pkt;
836 if (pes->st && av_new_packet(pkt, len) == 0) {
837 memcpy(pkt->data, p, len);
838 pkt->stream_index = pes->st->index;
841 /* reset pts values */
842 pes->pts = AV_NOPTS_VALUE;
843 pes->dts = AV_NOPTS_VALUE;
857 static int add_pes_stream(MpegTSContext *ts, int pid, int stream_type)
862 /* if no pid found, then add a pid context */
863 pes = av_mallocz(sizeof(PESContext));
867 pes->stream = ts->stream;
869 pes->stream_type = stream_type;
870 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
878 /* handle one TS packet */
879 static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
881 AVFormatContext *s = ts->stream;
883 int len, pid, cc, cc_ok, afc, is_start;
884 const uint8_t *p, *p_end;
886 pid = ((packet[1] & 0x1f) << 8) | packet[2];
887 is_start = packet[1] & 0x40;
889 if (ts->auto_guess && tss == NULL && is_start) {
890 add_pes_stream(ts, pid, 0);
896 /* continuity check (currently not used) */
897 cc = (packet[3] & 0xf);
898 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
901 /* skip adaptation field */
902 afc = (packet[3] >> 4) & 3;
904 if (afc == 0) /* reserved value */
906 if (afc == 2) /* adaptation field only */
909 /* skip adapation field */
912 /* if past the end of packet, ignore */
913 p_end = packet + TS_PACKET_SIZE;
917 if (tss->type == MPEGTS_SECTION) {
919 /* pointer field present */
924 /* write remaining section bytes */
925 write_section_data(s, tss,
927 /* check whether filter has been closed */
933 write_section_data(s, tss,
938 write_section_data(s, tss,
943 tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
944 p, p_end - p, is_start);
948 /* XXX: try to find a better synchro over several packets (use
949 get_packet_size() ?) */
950 static int mpegts_resync(ByteIOContext *pb)
954 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
959 url_fseek(pb, -1, SEEK_CUR);
967 /* return -1 if error or EOF. Return 0 if OK. */
968 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
973 len = get_buffer(pb, buf, TS_PACKET_SIZE);
974 if (len != TS_PACKET_SIZE)
976 /* check paquet sync byte */
977 if (buf[0] != 0x47) {
978 /* find a new packet start */
979 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
980 if (mpegts_resync(pb) < 0)
981 return AVERROR_INVALIDDATA;
985 skip = raw_packet_size - TS_PACKET_SIZE;
994 static int handle_packets(MpegTSContext *ts, int nb_packets)
996 AVFormatContext *s = ts->stream;
997 ByteIOContext *pb = &s->pb;
998 uint8_t packet[TS_PACKET_SIZE];
1007 if (nb_packets != 0 && packet_num >= nb_packets)
1009 ret = read_packet(pb, packet, ts->raw_packet_size);
1012 handle_packet(ts, packet);
1017 static int mpegts_probe(AVProbeData *p)
1020 const int size= p->buf_size;
1021 int score, fec_score;
1022 #define CHECK_COUNT 10
1024 if (size < (TS_FEC_PACKET_SIZE * CHECK_COUNT))
1027 score = analyze(p->buf, TS_PACKET_SIZE *CHECK_COUNT, TS_PACKET_SIZE, NULL);
1028 fec_score= analyze(p->buf, TS_FEC_PACKET_SIZE*CHECK_COUNT, TS_FEC_PACKET_SIZE, NULL);
1029 // av_log(NULL, AV_LOG_DEBUG, "score: %d, fec_score: %d \n", score, fec_score);
1031 // we need a clear definition for the returned score otherwise things will become messy sooner or later
1032 if (score > fec_score && score > 6) return AVPROBE_SCORE_MAX + score - CHECK_COUNT;
1033 else if( fec_score > 6) return AVPROBE_SCORE_MAX + fec_score - CHECK_COUNT;
1036 /* only use the extension for safer guess */
1037 if (match_ext(p->filename, "ts"))
1038 return AVPROBE_SCORE_MAX;
1044 void set_service_cb(void *opaque, int ret)
1046 MpegTSContext *ts = opaque;
1047 ts->set_service_ret = ret;
1051 /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
1052 (-1) if not available */
1053 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
1054 const uint8_t *packet)
1056 int afc, len, flags;
1060 afc = (packet[3] >> 4) & 3;
1070 if (!(flags & 0x10))
1074 v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1075 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
1076 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1080 static int mpegts_read_header(AVFormatContext *s,
1081 AVFormatParameters *ap)
1083 MpegTSContext *ts = s->priv_data;
1084 ByteIOContext *pb = &s->pb;
1088 MpegTSService *service;
1091 ts->mpeg2ts_raw = ap->mpeg2ts_raw;
1092 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1095 /* read the first 1024 bytes to get packet size */
1096 pos = url_ftell(pb);
1097 len = get_buffer(pb, buf, sizeof(buf));
1098 if (len != sizeof(buf))
1100 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1101 if (ts->raw_packet_size <= 0)
1106 if (!ts->mpeg2ts_raw) {
1109 if (!ts->auto_guess) {
1110 ts->set_service_ret = -1;
1112 /* first do a scaning to get all the services */
1113 url_fseek(pb, pos, SEEK_SET);
1114 mpegts_scan_sdt(ts);
1116 handle_packets(ts, MAX_SCAN_PACKETS);
1118 if (ts->nb_services <= 0) {
1119 /* no SDT found, we try to look at the PAT */
1121 /* First remove the SDT filters from each PID */
1123 for (i=0; i < NB_PID_MAX; i++) {
1125 mpegts_close_filter(ts, ts->pids[i]);
1127 url_fseek(pb, pos, SEEK_SET);
1128 mpegts_scan_pat(ts);
1130 handle_packets(ts, MAX_SCAN_PACKETS);
1133 if (ts->nb_services <= 0) {
1134 /* raw transport stream */
1136 s->ctx_flags |= AVFMTCTX_NOHEADER;
1140 /* tune to first service found */
1141 for(i=0; i<ts->nb_services && ts->set_service_ret; i++){
1142 service = ts->services[i];
1145 printf("tuning to '%s'\n", service->name);
1148 /* now find the info for the first service if we found any,
1149 otherwise try to filter all PATs */
1151 url_fseek(pb, pos, SEEK_SET);
1152 mpegts_set_service(ts, sid, set_service_cb, ts);
1154 handle_packets(ts, MAX_SCAN_PACKETS);
1156 /* if could not find service, exit */
1158 if (ts->set_service_ret != 0)
1162 printf("tuning done\n");
1165 s->ctx_flags |= AVFMTCTX_NOHEADER;
1168 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1169 int64_t pcrs[2], pcr_h;
1170 int packet_count[2];
1171 uint8_t packet[TS_PACKET_SIZE];
1173 /* only read packets */
1176 st = av_new_stream(s, 0);
1179 av_set_pts_info(st, 60, 1, 27000000);
1180 st->codec.codec_type = CODEC_TYPE_DATA;
1181 st->codec.codec_id = CODEC_ID_MPEG2TS;
1183 /* we iterate until we find two PCRs to estimate the bitrate */
1188 ret = read_packet(&s->pb, packet, ts->raw_packet_size);
1191 pid = ((packet[1] & 0x1f) << 8) | packet[2];
1192 if ((pcr_pid == -1 || pcr_pid == pid) &&
1193 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1195 packet_count[nb_pcrs] = nb_packets;
1196 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1203 ts->pcr_pid = pcr_pid;
1205 /* NOTE1: the bitrate is computed without the FEC */
1206 /* NOTE2: it is only the bitrate of the start of the stream */
1207 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1208 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1209 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1210 st->codec.bit_rate = s->bit_rate;
1211 st->start_time = ts->cur_pcr;
1213 printf("start=%0.3f pcr=%0.3f incr=%d\n",
1214 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1218 url_fseek(pb, pos, SEEK_SET);
1224 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1226 static int mpegts_raw_read_packet(AVFormatContext *s,
1229 MpegTSContext *ts = s->priv_data;
1231 int64_t pcr_h, next_pcr_h, pos;
1232 int pcr_l, next_pcr_l;
1233 uint8_t pcr_buf[12];
1235 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1237 ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
1239 av_free_packet(pkt);
1242 if (ts->mpeg2ts_compute_pcr) {
1243 /* compute exact PCR for each packet */
1244 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1245 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1246 pos = url_ftell(&s->pb);
1247 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1248 url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1249 get_buffer(&s->pb, pcr_buf, 12);
1250 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1251 /* XXX: not precise enough */
1252 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1257 url_fseek(&s->pb, pos, SEEK_SET);
1258 /* no next PCR found: we use previous increment */
1259 ts->cur_pcr = pcr_h * 300 + pcr_l;
1261 pkt->pts = ts->cur_pcr;
1262 pkt->duration = ts->pcr_incr;
1263 ts->cur_pcr += ts->pcr_incr;
1265 pkt->stream_index = 0;
1269 static int mpegts_read_packet(AVFormatContext *s,
1272 MpegTSContext *ts = s->priv_data;
1274 if (!ts->mpeg2ts_raw) {
1276 return handle_packets(ts, 0);
1278 return mpegts_raw_read_packet(s, pkt);
1282 static int mpegts_read_close(AVFormatContext *s)
1284 MpegTSContext *ts = s->priv_data;
1286 for(i=0;i<NB_PID_MAX;i++)
1287 if (ts->pids[i]) mpegts_close_filter(ts, ts->pids[i]);
1291 static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
1292 int64_t *ppos, int64_t pos_limit)
1294 MpegTSContext *ts = s->priv_data;
1295 int64_t pos, timestamp;
1296 uint8_t buf[TS_PACKET_SIZE];
1298 const int find_next= 1;
1299 pos = ((*ppos + ts->raw_packet_size - 1) / ts->raw_packet_size) * ts->raw_packet_size;
1302 url_fseek(&s->pb, pos, SEEK_SET);
1303 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1304 return AV_NOPTS_VALUE;
1305 pid = ((buf[1] & 0x1f) << 8) | buf[2];
1306 if (pid == ts->pcr_pid &&
1307 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1310 pos += ts->raw_packet_size;
1314 pos -= ts->raw_packet_size;
1316 return AV_NOPTS_VALUE;
1317 url_fseek(&s->pb, pos, SEEK_SET);
1318 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1319 return AV_NOPTS_VALUE;
1320 pid = ((buf[1] & 0x1f) << 8) | buf[2];
1321 if (pid == ts->pcr_pid &&
1322 parse_pcr(×tamp, &pcr_l, buf) == 0) {
1332 static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1333 MpegTSContext *ts = s->priv_data;
1334 uint8_t buf[TS_PACKET_SIZE];
1337 if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0)
1340 pos= url_ftell(&s->pb);
1343 url_fseek(&s->pb, pos, SEEK_SET);
1344 if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
1346 // pid = ((buf[1] & 0x1f) << 8) | buf[2];
1347 if(buf[1] & 0x40) break;
1348 pos += ts->raw_packet_size;
1350 url_fseek(&s->pb, pos, SEEK_SET);
1355 /**************************************************************/
1356 /* parsing functions - called from other demuxers such as RTP */
1358 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1362 ts = av_mallocz(sizeof(MpegTSContext));
1365 /* no stream case, currently used by RTP */
1366 ts->raw_packet_size = TS_PACKET_SIZE;
1372 /* return the consumed length if a packet was output, or -1 if no
1374 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1375 const uint8_t *buf, int len)
1385 if (len < TS_PACKET_SIZE)
1387 if (buf[0] != 0x47) {
1391 handle_packet(ts, buf);
1392 buf += TS_PACKET_SIZE;
1393 len -= TS_PACKET_SIZE;
1399 void mpegts_parse_close(MpegTSContext *ts)
1403 for(i=0;i<NB_PID_MAX;i++)
1404 av_free(ts->pids[i]);
1408 AVInputFormat mpegts_demux = {
1410 "MPEG2 transport stream format",
1411 sizeof(MpegTSContext),
1418 .flags = AVFMT_SHOW_IDS,
1421 int mpegts_init(void)
1423 av_register_input_format(&mpegts_demux);
1424 #ifdef CONFIG_ENCODERS
1425 av_register_output_format(&mpegts_mux);