2 * MPEG2 transport stream (aka DVB) demux
3 * Copyright (c) 2002 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
21 #define TS_FEC_PACKET_SIZE 204
22 #define TS_PACKET_SIZE 188
23 #define NB_PID_MAX 8192
27 MPEGTS_PESHEADER_FILL,
28 MPEGTS_PESHEADER_FLAGS,
29 MPEGTS_PESHEADER_SIZE,
30 MPEGTS_PESHEADER_READ,
35 /* enough for PES header + length */
36 #define MAX_HEADER_SIZE 6
38 typedef struct MpegTSStream {
40 enum MpegTSState state;
41 int last_cc; /* last cc code (-1 if first packet) */
42 /* used to get the format */
47 unsigned char header[MAX_HEADER_SIZE];
50 typedef struct MpegTSContext {
51 int raw_packet_size; /* raw packet size, including FEC if present */
52 MpegTSStream *pids[NB_PID_MAX];
55 /* autodetect fec presence. Must have at least 1024 bytes */
56 static int get_packet_size(const unsigned char *buf, int size)
60 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
63 if (buf[i * TS_PACKET_SIZE] != 0x47)
66 return TS_PACKET_SIZE;
69 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
72 return TS_FEC_PACKET_SIZE;
75 static int mpegts_probe(AVProbeData *p)
78 size = get_packet_size(p->buf, p->buf_size);
81 return AVPROBE_SCORE_MAX;
84 static int mpegts_read_header(AVFormatContext *s,
85 AVFormatParameters *ap)
87 MpegTSContext *ts = s->priv_data;
88 ByteIOContext *pb = &s->pb;
89 unsigned char buf[1024];
93 /* read the first 1024 bytes to get packet size */
95 len = get_buffer(pb, buf, sizeof(buf));
96 if (len != sizeof(buf))
98 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
99 if (ts->raw_packet_size <= 0)
101 /* go again to the start */
102 url_fseek(pb, pos, SEEK_SET);
108 /* return non zero if a packet could be constructed */
109 static int mpegts_push_data(AVFormatContext *s, MpegTSStream *tss,
111 const unsigned char *buf, int buf_size, int is_start)
114 const unsigned char *p;
115 int len, code, codec_type, codec_id;
118 tss->state = MPEGTS_HEADER;
119 tss->header_size = 0;
122 while (buf_size > 0) {
126 if (len > MAX_HEADER_SIZE - tss->header_size)
127 len = MAX_HEADER_SIZE - tss->header_size;
128 memcpy(tss->header, p, len);
129 tss->header_size += len;
132 if (tss->header_size == MAX_HEADER_SIZE) {
133 /* we got all the PES or section header. We can now
136 av_hex_dump(tss->header, tss->header_size);
138 if (tss->header[0] == 0x00 && tss->header[1] == 0x00 &&
139 tss->header[2] == 0x01) {
140 /* it must be an mpeg2 PES stream */
141 /* XXX: add AC3 support */
142 code = tss->header[3] | 0x100;
143 if (!((code >= 0x1c0 && code <= 0x1df) ||
144 (code >= 0x1e0 && code <= 0x1ef)))
147 /* allocate stream */
148 if (code >= 0x1c0 && code <= 0x1df) {
149 codec_type = CODEC_TYPE_AUDIO;
150 codec_id = CODEC_ID_MP2;
152 codec_type = CODEC_TYPE_VIDEO;
153 codec_id = CODEC_ID_MPEG1VIDEO;
155 st = av_new_stream(s, tss->pid);
158 st->codec.codec_type = codec_type;
159 st->codec.codec_id = codec_id;
163 tss->state = MPEGTS_PESHEADER_FILL;
164 tss->payload_size = (tss->header[4] << 8) | tss->header[5];
165 if (tss->payload_size == 0)
166 tss->payload_size = 65536;
168 /* otherwise, it should be a table */
171 tss->state = MPEGTS_SKIP;
176 /**********************************************/
177 /* PES packing parsing */
178 case MPEGTS_PESHEADER_FILL:
184 if ((code & 0xc0) != 0x80)
186 tss->state = MPEGTS_PESHEADER_FLAGS;
189 case MPEGTS_PESHEADER_FLAGS:
193 tss->state = MPEGTS_PESHEADER_SIZE;
195 case MPEGTS_PESHEADER_SIZE:
196 tss->pes_header_size = *p++;
199 tss->state = MPEGTS_PESHEADER_READ;
201 case MPEGTS_PESHEADER_READ:
202 /* currently we do nothing except skipping */
203 if (len > tss->pes_header_size)
204 len = tss->pes_header_size;
207 tss->pes_header_size -= len;
208 tss->payload_size -= len;
209 if (tss->pes_header_size == 0)
210 tss->state = MPEGTS_PAYLOAD;
213 if (len > tss->payload_size)
214 len = tss->payload_size;
216 if (tss->st && av_new_packet(pkt, buf_size) == 0) {
217 memcpy(pkt->data, p, buf_size);
218 pkt->stream_index = tss->st->index;
221 tss->payload_size -= len;
233 static int mpegts_read_packet(AVFormatContext *s,
236 MpegTSContext *ts = s->priv_data;
238 ByteIOContext *pb = &s->pb;
239 unsigned char packet[TS_FEC_PACKET_SIZE];
240 int len, pid, cc, cc_ok, afc;
241 const unsigned char *p;
244 len = get_buffer(pb, packet, ts->raw_packet_size);
245 if (len != ts->raw_packet_size)
247 /* check paquet sync byte */
248 /* XXX: accept to resync ? */
249 if (packet[0] != 0x47)
250 return AVERROR_INVALIDDATA;
252 pid = ((packet[1] & 0x1f) << 8) | packet[2];
255 /* if no pid found, then add a pid context */
256 tss = av_mallocz(sizeof(MpegTSStream));
262 // printf("new pid=0x%x\n", pid);
265 /* continuity check (currently not used) */
266 cc = (packet[3] & 0xf);
267 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
270 /* skip adaptation field */
271 afc = (packet[3] >> 4) & 3;
273 if (afc == 0) /* reserved value */
275 if (afc == 2) /* adaptation field only */
278 /* skip adapation field */
281 /* if past the end of packet, ignore */
282 if (p >= packet + TS_PACKET_SIZE)
285 if (mpegts_push_data(s, tss, pkt, p, TS_PACKET_SIZE - (p - packet),
292 static int mpegts_read_close(AVFormatContext *s)
294 MpegTSContext *ts = s->priv_data;
296 for(i=0;i<NB_PID_MAX;i++)
297 av_free(ts->pids[i]);
301 AVInputFormat mpegts_demux = {
303 "MPEG2 transport stream format",
304 sizeof(MpegTSContext),
309 flags: AVFMT_NOHEADER | AVFMT_SHOW_IDS,
312 int mpegts_init(void)
314 av_register_input_format(&mpegts_demux);