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 16000
28 static int add_pes_stream(AVFormatContext *s, int pid);
30 enum MpegTSFilterType {
35 /* XXX: suppress 'pkt' parameter */
36 typedef void PESCallback(void *opaque, const uint8_t *buf, int len, int is_start);
38 typedef struct MpegTSPESFilter {
43 typedef void SectionCallback(void *opaque, const uint8_t *buf, int len);
45 typedef void SetServiceCallback(void *opaque, int ret);
47 typedef struct MpegTSSectionFilter {
52 int end_of_section_reached:1;
53 SectionCallback *section_cb;
55 } MpegTSSectionFilter;
57 typedef struct MpegTSFilter {
59 int last_cc; /* last cc code (-1 if first packet) */
60 enum MpegTSFilterType type;
62 MpegTSPESFilter pes_filter;
63 MpegTSSectionFilter section_filter;
67 typedef struct MpegTSService {
74 typedef struct MpegTSContext {
76 AVFormatContext *stream;
77 int raw_packet_size; /* raw packet size, including FEC if present */
78 int auto_guess; /* if true, all pids are analized to find streams */
81 /* data needed to handle file based ts */
82 int stop_parse; /* stop parsing loop */
83 AVPacket *pkt; /* packet containing av data */
85 /******************************************/
86 /* private mpegts data */
88 MpegTSFilter *sdt_filter;
90 MpegTSService **services;
92 /* set service context (XXX: allocated it ?) */
93 SetServiceCallback *set_service_cb;
94 void *set_service_opaque;
95 MpegTSFilter *pat_filter;
96 MpegTSFilter *pmt_filter;
99 MpegTSFilter *pids[NB_PID_MAX];
102 static void write_section_data(AVFormatContext *s, MpegTSFilter *tss1,
103 const uint8_t *buf, int buf_size, int is_start)
105 MpegTSSectionFilter *tss = &tss1->u.section_filter;
110 memcpy(tss->section_buf, buf, buf_size);
111 tss->section_index = buf_size;
112 tss->section_h_size = -1;
113 tss->end_of_section_reached = 0;
115 if (tss->end_of_section_reached)
117 len = 4096 - tss->section_index;
120 memcpy(tss->section_buf + tss->section_index, buf, len);
121 tss->section_index += len;
124 /* compute section length if possible */
125 if (tss->section_h_size == -1 && tss->section_index >= 3) {
126 len = (((tss->section_buf[1] & 0xf) << 8) | tss->section_buf[2]) + 3;
129 tss->section_h_size = len;
132 if (tss->section_h_size != -1 && tss->section_index >= tss->section_h_size) {
133 if (tss->check_crc) {
134 crc = mpegts_crc32(tss->section_buf, tss->section_h_size);
138 tss->section_cb(tss->opaque, tss->section_buf, tss->section_h_size);
140 tss->end_of_section_reached = 1;
144 MpegTSFilter *mpegts_open_section_filter(MpegTSContext *ts, unsigned int pid,
145 SectionCallback *section_cb, void *opaque,
149 MpegTSFilter *filter;
150 MpegTSSectionFilter *sec;
152 if (pid >= NB_PID_MAX || ts->pids[pid])
154 filter = av_mallocz(sizeof(MpegTSFilter));
157 ts->pids[pid] = filter;
158 filter->type = MPEGTS_SECTION;
160 filter->last_cc = -1;
161 sec = &filter->u.section_filter;
162 sec->section_cb = section_cb;
163 sec->opaque = opaque;
164 sec->section_buf = av_malloc(MAX_SECTION_SIZE);
165 sec->check_crc = check_crc;
166 if (!sec->section_buf) {
173 MpegTSFilter *mpegts_open_pes_filter(MpegTSContext *ts, unsigned int pid,
177 MpegTSFilter *filter;
178 MpegTSPESFilter *pes;
180 if (pid >= NB_PID_MAX || ts->pids[pid])
182 filter = av_mallocz(sizeof(MpegTSFilter));
185 ts->pids[pid] = filter;
186 filter->type = MPEGTS_PES;
188 filter->last_cc = -1;
189 pes = &filter->u.pes_filter;
190 pes->pes_cb = pes_cb;
191 pes->opaque = opaque;
195 void mpegts_close_filter(MpegTSContext *ts, MpegTSFilter *filter)
200 if (filter->type == MPEGTS_SECTION)
201 av_freep(&filter->u.section_filter.section_buf);
203 ts->pids[pid] = NULL;
206 /* autodetect fec presence. Must have at least 1024 bytes */
207 static int get_packet_size(const uint8_t *buf, int size)
211 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
214 if (buf[i * TS_PACKET_SIZE] != 0x47)
217 return TS_PACKET_SIZE;
220 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
223 return TS_FEC_PACKET_SIZE;
226 typedef struct SectionHeader {
231 uint8_t last_sec_num;
234 static inline int get8(const uint8_t **pp, const uint8_t *p_end)
247 static inline int get16(const uint8_t **pp, const uint8_t *p_end)
253 if ((p + 1) >= p_end)
255 c = (p[0] << 8) | p[1];
261 /* read and allocate a DVB string preceeded by its length */
262 static char *getstr8(const uint8_t **pp, const uint8_t *p_end)
269 len = get8(&p, p_end);
272 if ((p + len) > p_end)
274 str = av_malloc(len + 1);
284 static int parse_section_header(SectionHeader *h,
285 const uint8_t **pp, const uint8_t *p_end)
289 val = get8(pp, p_end);
294 val = get16(pp, p_end);
298 val = get8(pp, p_end);
301 h->version = (val >> 1) & 0x1f;
302 val = get8(pp, p_end);
306 val = get8(pp, p_end);
309 h->last_sec_num = val;
313 static MpegTSService *new_service(MpegTSContext *ts, int sid,
314 char *provider_name, char *name)
316 MpegTSService *service;
319 printf("new_service: sid=0x%04x provider='%s' name='%s'\n",
320 sid, provider_name, name);
323 service = av_mallocz(sizeof(MpegTSService));
327 service->provider_name = provider_name;
328 service->name = name;
329 dynarray_add(&ts->services, &ts->nb_services, service);
333 static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
335 MpegTSContext *ts = opaque;
336 SectionHeader h1, *h = &h1;
337 const uint8_t *p, *p_end;
338 int program_info_length, pcr_pid, pid, stream_type, desc_length;
342 av_hex_dump((uint8_t *)section, section_len);
344 p_end = section + section_len - 4;
346 if (parse_section_header(h, &p, p_end) < 0)
349 printf("sid=0x%x sec_num=%d/%d\n", h->id, h->sec_num, h->last_sec_num);
351 if (h->tid != PMT_TID || h->id != ts->req_sid)
354 pcr_pid = get16(&p, p_end) & 0x1fff;
358 printf("pcr_pid=0x%x\n", pcr_pid);
360 program_info_length = get16(&p, p_end) & 0xfff;
361 if (program_info_length < 0)
363 p += program_info_length;
367 stream_type = get8(&p, p_end);
370 pid = get16(&p, p_end) & 0x1fff;
373 desc_length = get16(&p, p_end) & 0xfff;
381 printf("stream_type=%d pid=0x%x\n", stream_type, pid);
384 /* now create ffmpeg stream */
385 switch(stream_type) {
386 case STREAM_TYPE_AUDIO:
387 case STREAM_TYPE_VIDEO:
388 add_pes_stream(ts->stream, pid);
391 /* we ignore the other streams */
395 /* all parameters are there */
396 ts->set_service_cb(ts->set_service_opaque, 0);
397 mpegts_close_filter(ts, ts->pmt_filter);
398 ts->pmt_filter = NULL;
401 static void pat_cb(void *opaque, const uint8_t *section, int section_len)
403 MpegTSContext *ts = opaque;
404 SectionHeader h1, *h = &h1;
405 const uint8_t *p, *p_end;
410 av_hex_dump((uint8_t *)section, section_len);
412 p_end = section + section_len - 4;
414 if (parse_section_header(h, &p, p_end) < 0)
416 if (h->tid != PAT_TID)
420 sid = get16(&p, p_end);
423 pmt_pid = get16(&p, p_end) & 0x1fff;
427 printf("sid=0x%x pid=0x%x\n", sid, pmt_pid);
432 if (ts->req_sid == sid) {
433 ts->pmt_filter = mpegts_open_section_filter(ts, pmt_pid,
440 ts->set_service_cb(ts->set_service_opaque, -1);
443 mpegts_close_filter(ts, ts->pat_filter);
444 ts->pat_filter = NULL;
447 void mpegts_set_service(MpegTSContext *ts, int sid,
448 SetServiceCallback *set_service_cb, void *opaque)
450 ts->set_service_cb = set_service_cb;
451 ts->set_service_opaque = opaque;
453 ts->pat_filter = mpegts_open_section_filter(ts, PAT_PID,
457 static void sdt_cb(void *opaque, const uint8_t *section, int section_len)
459 MpegTSContext *ts = opaque;
460 SectionHeader h1, *h = &h1;
461 const uint8_t *p, *p_end, *desc_list_end, *desc_end;
462 int onid, val, sid, desc_list_len, desc_tag, desc_len, service_type;
463 char *name, *provider_name;
467 av_hex_dump((uint8_t *)section, section_len);
470 p_end = section + section_len - 4;
472 if (parse_section_header(h, &p, p_end) < 0)
474 if (h->tid != SDT_TID)
476 onid = get16(&p, p_end);
479 val = get8(&p, p_end);
483 sid = get16(&p, p_end);
486 val = get8(&p, p_end);
489 desc_list_len = get16(&p, p_end) & 0xfff;
490 if (desc_list_len < 0)
492 desc_list_end = p + desc_list_len;
493 if (desc_list_end > p_end)
496 desc_tag = get8(&p, desc_list_end);
499 desc_len = get8(&p, desc_list_end);
500 desc_end = p + desc_len;
501 if (desc_end > desc_list_end)
504 printf("tag: 0x%02x len=%d\n", desc_tag, desc_len);
508 service_type = get8(&p, p_end);
509 if (service_type < 0)
511 provider_name = getstr8(&p, p_end);
514 name = getstr8(&p, p_end);
517 new_service(ts, sid, provider_name, name);
529 mpegts_close_filter(ts, ts->sdt_filter);
530 ts->sdt_filter = NULL;
533 /* scan services an a transport stream by looking at the sdt */
534 void mpegts_scan_sdt(MpegTSContext *ts)
536 ts->sdt_filter = mpegts_open_section_filter(ts, SDT_PID,
541 /* TS stream handling */
545 MPEGTS_PESHEADER_FILL,
550 /* enough for PES header + length */
551 #define PES_START_SIZE 9
552 #define MAX_PES_HEADER_SIZE (9 + 255)
554 typedef struct PESContext {
556 AVFormatContext *stream;
558 enum MpegTSState state;
559 /* used to get the format */
564 uint8_t header[MAX_PES_HEADER_SIZE];
567 static int64_t get_pts(const uint8_t *p)
572 pts = (int64_t)((p[0] >> 1) & 0x07) << 30;
573 val = (p[1] << 8) | p[2];
574 pts |= (int64_t)(val >> 1) << 15;
575 val = (p[3] << 8) | p[4];
576 pts |= (int64_t)(val >> 1);
580 /* return non zero if a packet could be constructed */
581 static void mpegts_push_data(void *opaque,
582 const uint8_t *buf, int buf_size, int is_start)
584 PESContext *pes = opaque;
585 MpegTSContext *ts = pes->stream->priv_data;
588 int len, code, codec_type, codec_id;
591 pes->state = MPEGTS_HEADER;
595 while (buf_size > 0) {
598 len = PES_START_SIZE - pes->data_index;
601 memcpy(pes->header + pes->data_index, p, len);
602 pes->data_index += len;
605 if (pes->data_index == PES_START_SIZE) {
606 /* we got all the PES or section header. We can now
609 av_hex_dump(pes->header, pes->data_index);
611 if (pes->header[0] == 0x00 && pes->header[1] == 0x00 &&
612 pes->header[2] == 0x01) {
613 /* it must be an mpeg2 PES stream */
614 /* XXX: add AC3 support */
615 code = pes->header[3] | 0x100;
616 if (!((code >= 0x1c0 && code <= 0x1df) ||
617 (code >= 0x1e0 && code <= 0x1ef)))
620 /* allocate stream */
621 if (code >= 0x1c0 && code <= 0x1df) {
622 codec_type = CODEC_TYPE_AUDIO;
623 codec_id = CODEC_ID_MP2;
625 codec_type = CODEC_TYPE_VIDEO;
626 codec_id = CODEC_ID_MPEG1VIDEO;
628 st = av_new_stream(pes->stream, pes->pid);
631 st->codec.codec_type = codec_type;
632 st->codec.codec_id = codec_id;
636 pes->state = MPEGTS_PESHEADER_FILL;
637 pes->total_size = (pes->header[4] << 8) | pes->header[5];
638 /* NOTE: a zero total size means the PES size is
641 pes->total_size += 6;
642 pes->pes_header_size = pes->header[8] + 9;
644 /* otherwise, it should be a table */
647 pes->state = MPEGTS_SKIP;
652 /**********************************************/
653 /* PES packing parsing */
654 case MPEGTS_PESHEADER_FILL:
655 len = pes->pes_header_size - pes->data_index;
658 memcpy(pes->header + pes->data_index, p, len);
659 pes->data_index += len;
662 if (pes->data_index == pes->pes_header_size) {
666 flags = pes->header[7];
668 pes->pts = AV_NOPTS_VALUE;
669 pes->dts = AV_NOPTS_VALUE;
670 if ((flags & 0xc0) == 0x80) {
671 pes->pts = get_pts(r);
673 } else if ((flags & 0xc0) == 0xc0) {
674 pes->pts = get_pts(r);
676 pes->dts = get_pts(r);
679 /* we got the full header. We parse it and get the payload */
680 pes->state = MPEGTS_PAYLOAD;
684 if (pes->total_size) {
685 len = pes->total_size - pes->data_index;
692 AVPacket *pkt = ts->pkt;
693 if (pes->st && av_new_packet(pkt, len) == 0) {
694 memcpy(pkt->data, p, len);
695 pkt->stream_index = pes->st->index;
697 /* reset pts values */
698 pes->pts = AV_NOPTS_VALUE;
699 pes->dts = AV_NOPTS_VALUE;
713 static int add_pes_stream(AVFormatContext *s, int pid)
715 MpegTSContext *ts = s->priv_data;
719 /* if no pid found, then add a pid context */
720 pes = av_mallocz(sizeof(PESContext));
725 tss = mpegts_open_pes_filter(ts, pid, mpegts_push_data, pes);
733 /* handle one TS packet */
734 static void handle_packet(AVFormatContext *s, uint8_t *packet)
736 MpegTSContext *ts = s->priv_data;
738 int len, pid, cc, cc_ok, afc, is_start;
739 const uint8_t *p, *p_end;
741 pid = ((packet[1] & 0x1f) << 8) | packet[2];
742 is_start = packet[1] & 0x40;
744 if (ts->auto_guess && tss == NULL && is_start) {
745 add_pes_stream(s, pid);
751 /* continuity check (currently not used) */
752 cc = (packet[3] & 0xf);
753 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
756 /* skip adaptation field */
757 afc = (packet[3] >> 4) & 3;
759 if (afc == 0) /* reserved value */
761 if (afc == 2) /* adaptation field only */
764 /* skip adapation field */
767 /* if past the end of packet, ignore */
768 p_end = packet + TS_PACKET_SIZE;
772 if (tss->type == MPEGTS_SECTION) {
774 /* pointer field present */
779 /* write remaning section bytes */
780 write_section_data(s, tss,
785 write_section_data(s, tss,
790 write_section_data(s, tss,
795 tss->u.pes_filter.pes_cb(tss->u.pes_filter.opaque,
796 p, p_end - p, is_start);
800 static int handle_packets(AVFormatContext *s, int nb_packets)
802 MpegTSContext *ts = s->priv_data;
803 ByteIOContext *pb = &s->pb;
804 uint8_t packet[TS_FEC_PACKET_SIZE];
813 if (nb_packets != 0 && packet_num >= nb_packets)
815 len = get_buffer(pb, packet, ts->raw_packet_size);
816 if (len != ts->raw_packet_size)
818 /* check paquet sync byte */
819 /* XXX: accept to resync ? */
820 if (packet[0] != 0x47)
821 return AVERROR_INVALIDDATA;
822 handle_packet(s, packet);
827 static int mpegts_probe(AVProbeData *p)
830 size = get_packet_size(p->buf, p->buf_size);
833 return AVPROBE_SCORE_MAX - 1;
836 void set_service_cb(void *opaque, int ret)
838 MpegTSContext *ts = opaque;
839 ts->set_service_ret = ret;
843 static int mpegts_read_header(AVFormatContext *s,
844 AVFormatParameters *ap)
846 MpegTSContext *ts = s->priv_data;
847 ByteIOContext *pb = &s->pb;
851 MpegTSService *service;
853 /* read the first 1024 bytes to get packet size */
855 len = get_buffer(pb, buf, sizeof(buf));
856 if (len != sizeof(buf))
858 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
859 if (ts->raw_packet_size <= 0)
863 if (!ts->auto_guess) {
865 /* first do a scaning to get all the services */
866 url_fseek(pb, pos, SEEK_SET);
869 handle_packets(s, MAX_SCAN_PACKETS);
871 /* if no service found, no need to do anything */
872 if (ts->nb_services <= 0)
875 /* now find the info for the first service */
877 url_fseek(pb, pos, SEEK_SET);
878 service = ts->services[0];
880 printf("tuning to '%s'\n", service->name);
882 /* tune to first service found */
884 mpegts_set_service(ts, service->sid, set_service_cb, ts);
886 handle_packets(s, MAX_SCAN_PACKETS);
888 /* if could not find service, exit */
889 if (ts->set_service_ret != 0)
893 printf("tuning done\n");
897 url_fseek(pb, pos, SEEK_SET);
903 static int mpegts_read_packet(AVFormatContext *s,
906 MpegTSContext *ts = s->priv_data;
908 return handle_packets(s, 0);
911 static int mpegts_read_close(AVFormatContext *s)
913 MpegTSContext *ts = s->priv_data;
915 for(i=0;i<NB_PID_MAX;i++)
916 av_free(ts->pids[i]);
920 AVInputFormat mpegts_demux = {
922 "MPEG2 transport stream format",
923 sizeof(MpegTSContext),
928 .flags = AVFMT_NOHEADER | AVFMT_SHOW_IDS,
931 int mpegts_init(void)
933 av_register_input_format(&mpegts_demux);
934 av_register_output_format(&mpegts_mux);