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
25 /* 1.0 second at 24Mbit/s */
26 #define MAX_SCAN_PACKETS 32000
28 /* maximum size in which we look for synchronisation if
29 synchronisation is lost */
30 #define MAX_RESYNC_SIZE 4096
32 static int add_pes_stream(MpegTSContext *ts, int pid);
34 enum MpegTSFilterType {
39 typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
41 typedef struct MpegTSPESFilter {
46 typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
48 typedef void SetServiceCallback(void *opaque, int ret);
50 typedef struct MpegTSSectionFilter {
55 int end_of_section_reached:1;
56 SectionCallback *section_cb;
58 } MpegTSSectionFilter;
60 typedef struct MpegTSFilter {
62 int last_cc; /* last cc code (-1 if first packet) */
63 enum MpegTSFilterType type;
65 MpegTSPESFilter pes_filter;
66 MpegTSSectionFilter section_filter;
70 typedef struct MpegTSService {
77 struct MpegTSContext {
79 AVFormatContext *stream;
80 int raw_packet_size; /* raw packet size, including FEC if present */
81 int auto_guess; /* if true, all pids are analized to find streams */
84 int mpeg2ts_raw; /* force raw MPEG2 transport stream output, if possible */
85 int mpeg2ts_compute_pcr; /* compute exact PCR for each transport stream packet */
87 /* used to estimate the exact PCR */
91 /* data needed to handle file based ts */
92 int stop_parse; /* stop parsing loop */
93 AVPacket *pkt; /* packet containing av data */
95 /******************************************/
96 /* private mpegts data */
98 MpegTSFilter *sdt_filter;
100 MpegTSService **services;
102 /* set service context (XXX: allocated it ?) */
103 SetServiceCallback *set_service_cb;
104 void *set_service_opaque;
105 MpegTSFilter *pat_filter;
106 MpegTSFilter *pmt_filter;
109 MpegTSFilter *pids[NB_PID_MAX];
112 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
113 const uint8_t *buf, int buf_size, int is_start)
115 MpegTSSectionFilter *tss = &tss1->u.section_filter;
120 memcpy(tss->section_buf, buf, buf_size);
121 tss->section_index = buf_size;
122 tss->section_h_size = -1;
123 tss->end_of_section_reached = 0;
125 if (tss->end_of_section_reached)
127 len = 4096 - tss->section_index;
130 memcpy(tss->section_buf + tss->section_index, buf, len);
131 tss->section_index += len;
134 /* compute section length if possible */
135 if (tss->section_h_size == -1 && tss->section_index >= 3) {
136 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
139 tss->section_h_size = len;
142 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
143 if (tss->check_crc) {
144 crc = mpegts_crc32(tss->section_buf, tss->section_h_size);
148 tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
150 tss->end_of_section_reached = 1;
154 MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
155 SectionCallback *section_cb, void *opaque,
159 MpegTSFilter *filter;
160 MpegTSSectionFilter *sec;
163 printf("Filter: pid=0x%x\n", pid);
165 if (pid >= NB_PID_MAX || ts->pids[pid])
167 filter = av_mallocz(sizeof(MpegTSFilter));
170 ts->pids[pid] = filter;
171 filter->type = MPEGTS_SECTION;
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) {
186 MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
190 MpegTSFilter *filter;
191 MpegTSPESFilter *pes;
193 if (pid >= NB_PID_MAX || ts->pids[pid])
195 filter = av_mallocz(sizeof(MpegTSFilter));
198 ts->pids[pid] = filter;
199 filter->type = MPEGTS_PES;
201 filter->last_cc = -1;
202 pes = &filter->u.pes_filter;
203 pes->pes_cb = pes_cb;
204 pes->opaque = opaque;
208 void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
213 if (filter->type == MPEGTS_SECTION)
214 av_freep(&filter->u.section_filter.section_buf);
216 ts->pids[pid] = NULL;
219 /* autodetect fec presence. Must have at least 1024 bytes */
220 static int get_packet_size(const uint8_t *buf, int size)
224 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
227 if (buf[i * TS_PACKET_SIZE] != 0x47)
230 return TS_PACKET_SIZE;
233 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
236 return TS_FEC_PACKET_SIZE;
239 typedef struct SectionHeader {
244 uint8_t last_sec_num;
247 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
260 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
266 if ((p + 1) >= p_end)
268 c = (p[0] << 8) | p[1];
274 /* read and allocate a DVB string preceeded by its length */
275 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
282 len = get8(&p, p_end);
285 if ((p + len) > p_end)
287 str = av_malloc(len + 1);
297 static int parse_section_header(SectionHeader *h,
298 const uint8_t **pp, const uint8_t *p_end)
302 val = get8(pp, p_end);
307 val = get16(pp, p_end);
311 val = get8(pp, p_end);
314 h->version = (val >> 1) & 0x1f;
315 val = get8(pp, p_end);
319 val = get8(pp, p_end);
322 h->last_sec_num = val;
326 static MpegTSService *new_service(MpegTSContext *ts, int sid,
327 char *provider_name, char *name)
329 MpegTSService *service;
332 printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
333 sid, provider_name, name);
336 service = av_mallocz(sizeof(MpegTSService));
340 service->provider_name = provider_name;
341 service->name = name;
342 dynarray_add(&ts->services, &ts->nb_services, service);
346 static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
348 MpegTSContext *ts = opaque;
349 SectionHeader h1, *h = &h1;
350 const uint8_t *p, *p_end;
351 int program_info_length, pcr_pid, pid, stream_type, desc_length;
355 av_hex_dump((uint8_t *)section, section_len);
357 p_end = section + section_len - 4;
359 if (parse_section_header(h, &p, p_end) < 0)
362 printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
364 if (h->tid != PMT_TID || (ts->req_sid >= 0 && h->id != ts->req_sid) )
367 pcr_pid = get16(&p, p_end) & 0x1fff;
371 printf("pcr_pid=0x%x\n", pcr_pid);
373 program_info_length = get16(&p, p_end) & 0xfff;
374 if (program_info_length < 0)
376 p += program_info_length;
380 stream_type = get8(&p, p_end);
383 pid = get16(&p, p_end) & 0x1fff;
386 desc_length = get16(&p, p_end) & 0xfff;
394 printf("stream_type=%d pid=0x%x\n", stream_type, pid);
397 /* now create ffmpeg stream */
398 switch(stream_type) {
399 case STREAM_TYPE_AUDIO_MPEG1:
400 case STREAM_TYPE_AUDIO_MPEG2:
401 case STREAM_TYPE_VIDEO_MPEG1:
402 case STREAM_TYPE_VIDEO_MPEG2:
403 case STREAM_TYPE_AUDIO_AC3:
404 add_pes_stream(ts, pid);
407 /* we ignore the other streams */
411 /* all parameters are there */
412 ts->set_service_cb(ts->set_service_opaque, 0);
413 mpegts_close_filter(ts, ts->pmt_filter);
414 ts->pmt_filter = NULL;
417 static void pat_cb(void *opaque, const uint8_t *section, int section_len)
419 MpegTSContext *ts = opaque;
420 SectionHeader h1, *h = &h1;
421 const uint8_t *p, *p_end;
426 av_hex_dump((uint8_t *)section, section_len);
428 p_end = section + section_len - 4;
430 if (parse_section_header(h, &p, p_end) < 0)
432 if (h->tid != PAT_TID)
436 sid = get16(&p, p_end);
439 pmt_pid = get16(&p, p_end) & 0x1fff;
443 printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
448 if (ts->req_sid == sid) {
449 ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
456 ts->set_service_cb(ts->set_service_opaque, -1);
459 mpegts_close_filter(ts, ts->pat_filter);
460 ts->pat_filter = NULL;
463 /* add all services found in the PAT */
464 static void pat_scan_cb(void *opaque, const uint8_t *section, int section_len)
466 MpegTSContext *ts = opaque;
467 SectionHeader h1, *h = &h1;
468 const uint8_t *p, *p_end;
470 char *provider_name, *name;
475 av_hex_dump((uint8_t *)section, section_len);
477 p_end = section + section_len - 4;
479 if (parse_section_header(h, &p, p_end) < 0)
481 if (h->tid != PAT_TID)
485 sid = get16(&p, p_end);
488 pmt_pid = get16(&p, p_end) & 0x1fff;
492 printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
497 /* add the service with a dummy name */
498 snprintf(buf, sizeof(buf), "Service %x\n", sid);
499 name = av_strdup(buf);
500 provider_name = av_strdup("");
501 if (name && provider_name) {
502 new_service(ts, sid, provider_name, name);
505 av_freep(&provider_name);
512 mpegts_close_filter(ts, ts->pat_filter);
513 ts->pat_filter = NULL;
516 void mpegts_set_service(MpegTSContext *ts, int sid,
517 SetServiceCallback *set_service_cb, void *opaque)
519 ts->set_service_cb = set_service_cb;
520 ts->set_service_opaque = opaque;
522 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
526 static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
528 MpegTSContext *ts = opaque;
529 SectionHeader h1, *h = &h1;
530 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
531 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
532 char *name, *provider_name;
536 av_hex_dump((uint8_t *)section, section_len);
539 p_end = section + section_len - 4;
541 if (parse_section_header(h, &p, p_end) < 0)
543 if (h->tid != SDT_TID)
545 onid = get16(&p, p_end);
548 val = get8(&p, p_end);
552 sid = get16(&p, p_end);
555 val = get8(&p, p_end);
558 desc_list_len = get16(&p, p_end) & 0xfff;
559 if (desc_list_len < 0)
561 desc_list_end = p + desc_list_len;
562 if (desc_list_end > p_end)
565 desc_tag = get8(&p, desc_list_end);
568 desc_len = get8(&p, desc_list_end);
569 desc_end = p + desc_len;
570 if (desc_end > desc_list_end)
573 printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
577 service_type = get8(&p, p_end);
578 if (service_type < 0)
580 provider_name = getstr8(&p, p_end);
583 name = getstr8(&p, p_end);
586 new_service(ts, sid, provider_name, name);
598 mpegts_close_filter(ts, ts->sdt_filter);
599 ts->sdt_filter = NULL;
602 /* scan services in a transport stream by looking at the SDT */
603 void mpegts_scan_sdt(MpegTSContext *ts)
605 ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
609 /* scan services in a transport stream by looking at the PAT (better
611 void mpegts_scan_pat(MpegTSContext *ts)
613 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
617 /* TS stream handling */
621 MPEGTS_PESHEADER_FILL,
626 /* enough for PES header + length */
627 #define PES_START_SIZE 9
628 #define MAX_PES_HEADER_SIZE (9 + 255)
630 typedef struct PESContext {
633 AVFormatContext *stream;
635 enum MpegTSState state;
636 /* used to get the format */
641 uint8_t header[MAX_PES_HEADER_SIZE];
644 static int64_t get_pts(const uint8_t *p)
649 pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
650 val = (p[1] << 8) | p[2];
651 pts |= (int64_t)(val >> 1) << 15;
652 val = (p[3] << 8) | p[4];
653 pts |= (int64_t)(val >> 1);
657 /* return non zero if a packet could be constructed */
658 static void mpegts_push_data(void *opaque,
659 const uint8_t *buf, int buf_size, int is_start)
661 PESContext *pes = opaque;
662 MpegTSContext *ts = pes->ts;
665 int len, code, codec_type, codec_id;
668 pes->state = MPEGTS_HEADER;
672 while (buf_size > 0) {
675 len = PES_START_SIZE - pes->data_index;
678 memcpy(pes->header + pes->data_index, p, len);
679 pes->data_index += len;
682 if (pes->data_index == PES_START_SIZE) {
683 /* we got all the PES or section header. We can now
686 av_hex_dump(pes->header, pes->data_index);
688 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
689 pes->header[2] == 0x01) {
690 /* it must be an mpeg2 PES stream */
691 code = pes->header[3] | 0x100;
692 if (!((code >= 0x1c0 && code <= 0x1df) ||
693 (code >= 0x1e0 && code <= 0x1ef) ||
697 /* allocate stream */
698 if (code >= 0x1c0 && code <= 0x1df) {
699 codec_type = CODEC_TYPE_AUDIO;
700 codec_id = CODEC_ID_MP2;
701 } else if (code == 0x1bd) {
702 codec_type = CODEC_TYPE_AUDIO;
703 codec_id = CODEC_ID_AC3;
705 codec_type = CODEC_TYPE_VIDEO;
706 codec_id = CODEC_ID_MPEG1VIDEO;
708 st = av_new_stream(pes->stream, pes->pid);
711 st->codec.codec_type = codec_type;
712 st->codec.codec_id = codec_id;
716 pes->state = MPEGTS_PESHEADER_FILL;
717 pes->total_size = (pes->header[4] << 8) | pes->header[5];
718 /* NOTE: a zero total size means the PES size is
721 pes->total_size += 6;
722 pes->pes_header_size = pes->header[8] + 9;
724 /* otherwise, it should be a table */
727 pes->state = MPEGTS_SKIP;
732 /**********************************************/
733 /* PES packing parsing */
734 case MPEGTS_PESHEADER_FILL:
735 len = pes->pes_header_size - pes->data_index;
738 memcpy(pes->header + pes->data_index, p, len);
739 pes->data_index += len;
742 if (pes->data_index == pes->pes_header_size) {
746 flags = pes->header[7];
748 pes->pts = AV_NOPTS_VALUE;
749 pes->dts = AV_NOPTS_VALUE;
750 if ((flags & 0xc0) == 0x80) {
751 pes->pts = get_pts(r);
753 } else if ((flags & 0xc0) == 0xc0) {
754 pes->pts = get_pts(r);
756 pes->dts = get_pts(r);
759 /* we got the full header. We parse it and get the payload */
760 pes->state = MPEGTS_PAYLOAD;
764 if (pes->total_size) {
765 len = pes->total_size - pes->data_index;
772 AVPacket *pkt = ts->pkt;
773 if (pes->st && av_new_packet(pkt, len) == 0) {
774 memcpy(pkt->data, p, len);
775 pkt->stream_index = pes->st->index;
777 /* reset pts values */
778 pes->pts = AV_NOPTS_VALUE;
779 pes->dts = AV_NOPTS_VALUE;
793 static int add_pes_stream(MpegTSContext *ts, int pid)
798 /* if no pid found, then add a pid context */
799 pes = av_mallocz(sizeof(PESContext));
803 pes->stream = ts->stream;
805 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
813 /* handle one TS packet */
814 static void handle_packet(MpegTSContext *ts, const uint8_t *packet)
816 AVFormatContext *s = ts->stream;
818 int len, pid, cc, cc_ok, afc, is_start;
819 const uint8_t *p, *p_end;
821 pid = ((packet[1] & 0x1f) << 8) | packet[2];
822 is_start = packet[1] & 0x40;
824 if (ts->auto_guess && tss == NULL && is_start) {
825 add_pes_stream(ts, pid);
831 /* continuity check (currently not used) */
832 cc = (packet[3] & 0xf);
833 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
836 /* skip adaptation field */
837 afc = (packet[3] >> 4) & 3;
839 if (afc == 0) /* reserved value */
841 if (afc == 2) /* adaptation field only */
844 /* skip adapation field */
847 /* if past the end of packet, ignore */
848 p_end = packet + TS_PACKET_SIZE;
852 if (tss->type == MPEGTS_SECTION) {
854 /* pointer field present */
859 /* write remaning section bytes */
860 write_section_data(s, tss,
865 write_section_data(s, tss,
870 write_section_data(s, tss,
875 tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
876 p, p_end - p, is_start);
880 /* XXX: try to find a better synchro over several packets (use
881 get_packet_size() ?) */
882 static int mpegts_resync(ByteIOContext *pb)
886 for(i = 0;i < MAX_RESYNC_SIZE; i++) {
891 url_fseek(pb, -1, SEEK_CUR);
899 /* return -1 if error or EOF. Return 0 if OK. */
900 static int read_packet(ByteIOContext *pb, uint8_t *buf, int raw_packet_size)
905 len = get_buffer(pb, buf, TS_PACKET_SIZE);
906 if (len != TS_PACKET_SIZE)
908 /* check paquet sync byte */
909 if (buf[0] != 0x47) {
910 /* find a new packet start */
911 url_fseek(pb, -TS_PACKET_SIZE, SEEK_CUR);
912 if (mpegts_resync(pb) < 0)
913 return AVERROR_INVALIDDATA;
917 skip = raw_packet_size - TS_PACKET_SIZE;
926 static int handle_packets(MpegTSContext *ts, int nb_packets)
928 AVFormatContext *s = ts->stream;
929 ByteIOContext *pb = &s->pb;
930 uint8_t packet[TS_PACKET_SIZE];
939 if (nb_packets != 0 && packet_num >= nb_packets)
941 ret = read_packet(pb, packet, ts->raw_packet_size);
944 handle_packet(ts, packet);
949 static int mpegts_probe(AVProbeData *p)
953 size = get_packet_size(p->buf, p->buf_size);
956 return AVPROBE_SCORE_MAX - 1;
958 /* only use the extension for safer guess */
959 if (match_ext(p->filename, "ts"))
960 return AVPROBE_SCORE_MAX;
966 void set_service_cb(void *opaque, int ret)
968 MpegTSContext *ts = opaque;
969 ts->set_service_ret = ret;
973 /* return the 90 kHz PCR and the extension for the 27 MHz PCR. return
974 (-1) if not available */
975 static int parse_pcr(int64_t *ppcr_high, int *ppcr_low,
976 const uint8_t *packet)
982 afc = (packet[3] >> 4) & 3;
996 v = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
997 *ppcr_high = ((int64_t)v << 1) | (p[4] >> 7);
998 *ppcr_low = ((p[4] & 1) << 8) | p[5];
1002 static int mpegts_read_header(AVFormatContext *s,
1003 AVFormatParameters *ap)
1005 MpegTSContext *ts = s->priv_data;
1006 ByteIOContext *pb = &s->pb;
1010 MpegTSService *service;
1013 ts->mpeg2ts_raw = ap->mpeg2ts_raw;
1014 ts->mpeg2ts_compute_pcr = ap->mpeg2ts_compute_pcr;
1017 /* read the first 1024 bytes to get packet size */
1018 pos = url_ftell(pb);
1019 len = get_buffer(pb, buf, sizeof(buf));
1020 if (len != sizeof(buf))
1022 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
1023 if (ts->raw_packet_size <= 0)
1028 if (!ts->mpeg2ts_raw) {
1031 if (!ts->auto_guess) {
1032 ts->set_service_ret = -1;
1034 /* first do a scaning to get all the services */
1035 url_fseek(pb, pos, SEEK_SET);
1036 mpegts_scan_sdt(ts);
1038 handle_packets(ts, MAX_SCAN_PACKETS);
1040 if (ts->nb_services <= 0) {
1041 /* no SDT found, we try to look at the PAT */
1043 /* First remove the SDT filters from each PID */
1045 for (i=0; i < NB_PID_MAX; i++) {
1047 mpegts_close_filter(ts, ts->pids[i]);
1049 url_fseek(pb, pos, SEEK_SET);
1050 mpegts_scan_pat(ts);
1052 handle_packets(ts, MAX_SCAN_PACKETS);
1055 if (ts->nb_services <= 0)
1058 /* tune to first service found */
1059 service = ts->services[0];
1062 printf("tuning to '%s'\n", service->name);
1065 /* now find the info for the first service if we found any,
1066 otherwise try to filter all PATs */
1068 url_fseek(pb, pos, SEEK_SET);
1069 mpegts_set_service(ts, sid, set_service_cb, ts);
1071 handle_packets(ts, MAX_SCAN_PACKETS);
1073 /* if could not find service, exit */
1074 if (ts->set_service_ret != 0)
1078 printf("tuning done\n");
1081 s->ctx_flags |= AVFMTCTX_NOHEADER;
1084 int pcr_pid, pid, nb_packets, nb_pcrs, ret, pcr_l;
1085 int64_t pcrs[2], pcr_h;
1086 int packet_count[2];
1087 uint8_t packet[TS_PACKET_SIZE];
1089 /* only read packets */
1092 s->pts_den = 27000000;
1094 st = av_new_stream(s, 0);
1097 st->codec.codec_type = CODEC_TYPE_DATA;
1098 st->codec.codec_id = CODEC_ID_MPEG2TS;
1100 /* we iterate until we find two PCRs to estimate the bitrate */
1105 ret = read_packet(&s->pb, packet, ts->raw_packet_size);
1108 pid = ((packet[1] & 0x1f) << 8) | packet[2];
1109 if ((pcr_pid == -1 || pcr_pid == pid) &&
1110 parse_pcr(&pcr_h, &pcr_l, packet) == 0) {
1112 packet_count[nb_pcrs] = nb_packets;
1113 pcrs[nb_pcrs] = pcr_h * 300 + pcr_l;
1121 /* NOTE1: the bitrate is computed without the FEC */
1122 /* NOTE2: it is only the bitrate of the start of the stream */
1123 ts->pcr_incr = (pcrs[1] - pcrs[0]) / (packet_count[1] - packet_count[0]);
1124 ts->cur_pcr = pcrs[0] - ts->pcr_incr * packet_count[0];
1125 s->bit_rate = (TS_PACKET_SIZE * 8) * 27e6 / ts->pcr_incr;
1126 st->codec.bit_rate = s->bit_rate;
1127 st->start_time = ts->cur_pcr * 1000000.0 / 27.0e6;
1129 printf("start=%0.3f pcr=%0.3f incr=%d\n",
1130 st->start_time / 1000000.0, pcrs[0] / 27e6, ts->pcr_incr);
1134 url_fseek(pb, pos, SEEK_SET);
1140 #define MAX_PACKET_READAHEAD ((128 * 1024) / 188)
1142 static int mpegts_raw_read_packet(AVFormatContext *s,
1145 MpegTSContext *ts = s->priv_data;
1147 int64_t pcr_h, next_pcr_h, pos;
1148 int pcr_l, next_pcr_l;
1149 uint8_t pcr_buf[12];
1151 if (av_new_packet(pkt, TS_PACKET_SIZE) < 0)
1153 ret = read_packet(&s->pb, pkt->data, ts->raw_packet_size);
1155 av_free_packet(pkt);
1158 if (ts->mpeg2ts_compute_pcr) {
1159 /* compute exact PCR for each packet */
1160 if (parse_pcr(&pcr_h, &pcr_l, pkt->data) == 0) {
1161 /* we read the next PCR (XXX: optimize it by using a bigger buffer */
1162 pos = url_ftell(&s->pb);
1163 for(i = 0; i < MAX_PACKET_READAHEAD; i++) {
1164 url_fseek(&s->pb, pos + i * ts->raw_packet_size, SEEK_SET);
1165 get_buffer(&s->pb, pcr_buf, 12);
1166 if (parse_pcr(&next_pcr_h, &next_pcr_l, pcr_buf) == 0) {
1167 /* XXX: not precise enough */
1168 ts->pcr_incr = ((next_pcr_h - pcr_h) * 300 + (next_pcr_l - pcr_l)) /
1173 url_fseek(&s->pb, pos, SEEK_SET);
1174 /* no next PCR found: we use previous increment */
1175 ts->cur_pcr = pcr_h * 300 + pcr_l;
1177 pkt->pts = ts->cur_pcr;
1178 pkt->duration = ts->pcr_incr;
1179 ts->cur_pcr += ts->pcr_incr;
1181 pkt->stream_index = 0;
1185 static int mpegts_read_packet(AVFormatContext *s,
1188 MpegTSContext *ts = s->priv_data;
1190 if (!ts->mpeg2ts_raw) {
1192 return handle_packets(ts, 0);
1194 return mpegts_raw_read_packet(s, pkt);
1198 static int mpegts_read_close(AVFormatContext *s)
1200 MpegTSContext *ts = s->priv_data;
1202 for(i=0;i<NB_PID_MAX;i++)
1203 av_free(ts->pids[i]);
1207 /**************************************************************/
1208 /* parsing functions - called from other demuxers such as RTP */
1210 MpegTSContext *mpegts_parse_open(AVFormatContext *s)
1214 ts = av_mallocz(sizeof(MpegTSContext));
1217 /* no stream case, currently used by RTP */
1218 ts->raw_packet_size = TS_PACKET_SIZE;
1224 /* return the consumed length if a packet was output, or -1 if no
1226 int mpegts_parse_packet(MpegTSContext *ts, AVPacket *pkt,
1227 const uint8_t *buf, int len)
1237 if (len < TS_PACKET_SIZE)
1239 if (buf[0] != 0x47) {
1243 handle_packet(ts, buf);
1244 buf += TS_PACKET_SIZE;
1245 len -= TS_PACKET_SIZE;
1251 void mpegts_parse_close(MpegTSContext *ts)
1255 for(i=0;i<NB_PID_MAX;i++)
1256 av_free(ts->pids[i]);
1260 AVInputFormat mpegts_demux = {
1262 "MPEG2 transport stream format",
1263 sizeof(MpegTSContext),
1268 .flags = AVFMT_SHOW_IDS,
1271 int mpegts_init(void)
1273 av_register_input_format(&mpegts_demux);
1274 #ifdef CONFIG_ENCODERS
1275 av_register_output_format(&mpegts_mux);