annotate mpegts.c @ 0:05318cf2e886 libavformat

renamed libav to libavformat
author bellard
date Mon, 25 Nov 2002 19:07:40 +0000
parents
children a58a8a53eb46
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
1 /*
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
2 * MPEG2 transport stream (aka DVB) demux
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
3 * Copyright (c) 2002 Fabrice Bellard.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
4 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
9 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
14 *
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
18 */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
19 #include "avformat.h"
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
20
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
21 #define TS_FEC_PACKET_SIZE 204
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
22 #define TS_PACKET_SIZE 188
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
23 #define NB_PID_MAX 8192
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
24
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
25 enum MpegTSState {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
26 MPEGTS_HEADER = 0,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
27 MPEGTS_PESHEADER_FILL,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
28 MPEGTS_PESHEADER_FLAGS,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
29 MPEGTS_PESHEADER_SIZE,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
30 MPEGTS_PESHEADER_READ,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
31 MPEGTS_PAYLOAD,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
32 MPEGTS_SKIP,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
33 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
34
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
35 /* enough for PES header + length */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
36 #define MAX_HEADER_SIZE 6
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
37
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
38 typedef struct MpegTSStream {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
39 int pid;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
40 enum MpegTSState state;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
41 int last_cc; /* last cc code (-1 if first packet) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
42 /* used to get the format */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
43 int header_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
44 int payload_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
45 int pes_header_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
46 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
47 unsigned char header[MAX_HEADER_SIZE];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
48 } MpegTSStream;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
49
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
50 typedef struct MpegTSContext {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
51 int raw_packet_size; /* raw packet size, including FEC if present */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
52 MpegTSStream *pids[NB_PID_MAX];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
53 } MpegTSContext;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
54
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
55 /* autodetect fec presence. Must have at least 1024 bytes */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
56 static int get_packet_size(const unsigned char *buf, int size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
57 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
58 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
59
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
60 if (size < (TS_FEC_PACKET_SIZE * 5 + 1))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
61 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
62 for(i=0;i<5;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
63 if (buf[i * TS_PACKET_SIZE] != 0x47)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
64 goto try_fec;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
65 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
66 return TS_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
67 try_fec:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
68 for(i=0;i<5;i++) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
69 if (buf[i * TS_FEC_PACKET_SIZE] != 0x47)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
70 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
71 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
72 return TS_FEC_PACKET_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
73 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
74
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
75 static int mpegts_probe(AVProbeData *p)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
76 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
77 int size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
78 size = get_packet_size(p->buf, p->buf_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
79 if (size < 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
80 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
81 return AVPROBE_SCORE_MAX;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
82 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
83
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
84 static int mpegts_read_header(AVFormatContext *s,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
85 AVFormatParameters *ap)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
86 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
87 MpegTSContext *ts = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
88 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
89 unsigned char buf[1024];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
90 int len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
91 INT64 pos;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
92
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
93 /* read the first 1024 bytes to get packet size */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
94 pos = url_ftell(pb);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
95 len = get_buffer(pb, buf, sizeof(buf));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
96 if (len != sizeof(buf))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
97 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
98 ts->raw_packet_size = get_packet_size(buf, sizeof(buf));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
99 if (ts->raw_packet_size <= 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
100 goto fail;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
101 /* go again to the start */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
102 url_fseek(pb, pos, SEEK_SET);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
103 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
104 fail:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
105 return -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
106 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
107
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
108 /* return non zero if a packet could be constructed */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
109 static int mpegts_push_data(AVFormatContext *s, MpegTSStream *tss,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
110 AVPacket *pkt,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
111 const unsigned char *buf, int buf_size, int is_start)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
112 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
113 AVStream *st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
114 const unsigned char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
115 int len, code, codec_type, codec_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
116
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
117 if (is_start) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
118 tss->state = MPEGTS_HEADER;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
119 tss->header_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
120 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
121 p = buf;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
122 while (buf_size > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
123 len = buf_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
124 switch(tss->state) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
125 case MPEGTS_HEADER:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
126 if (len > MAX_HEADER_SIZE - tss->header_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
127 len = MAX_HEADER_SIZE - tss->header_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
128 memcpy(tss->header, p, len);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
129 tss->header_size += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
130 p += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
131 buf_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
132 if (tss->header_size == MAX_HEADER_SIZE) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
133 /* we got all the PES or section header. We can now
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
134 decide */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
135 #if 0
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
136 av_hex_dump(tss->header, tss->header_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
137 #endif
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
138 if (tss->header[0] == 0x00 && tss->header[1] == 0x00 &&
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
139 tss->header[2] == 0x01) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
140 /* it must be an mpeg2 PES stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
141 /* XXX: add AC3 support */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
142 code = tss->header[3] | 0x100;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
143 if (!((code >= 0x1c0 && code <= 0x1df) ||
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
144 (code >= 0x1e0 && code <= 0x1ef)))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
145 goto skip;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
146 if (!tss->st) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
147 /* allocate stream */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
148 if (code >= 0x1c0 && code <= 0x1df) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
149 codec_type = CODEC_TYPE_AUDIO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
150 codec_id = CODEC_ID_MP2;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
151 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
152 codec_type = CODEC_TYPE_VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
153 codec_id = CODEC_ID_MPEG1VIDEO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
154 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
155 st = av_new_stream(s, tss->pid);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
156 if (st) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
157 st->priv_data = tss;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
158 st->codec.codec_type = codec_type;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
159 st->codec.codec_id = codec_id;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
160 tss->st = st;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
161 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
162 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
163 tss->state = MPEGTS_PESHEADER_FILL;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
164 tss->payload_size = (tss->header[4] << 8) | tss->header[5];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
165 if (tss->payload_size == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
166 tss->payload_size = 65536;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
167 } else {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
168 /* otherwise, it should be a table */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
169 /* skip packet */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
170 skip:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
171 tss->state = MPEGTS_SKIP;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
172 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
173 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
174 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
175 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
176 /**********************************************/
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
177 /* PES packing parsing */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
178 case MPEGTS_PESHEADER_FILL:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
179 /* skip filling */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
180 code = *p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
181 buf_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
182 tss->payload_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
183 if (code != 0xff) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
184 if ((code & 0xc0) != 0x80)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
185 goto skip;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
186 tss->state = MPEGTS_PESHEADER_FLAGS;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
187 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
188 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
189 case MPEGTS_PESHEADER_FLAGS:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
190 code = *p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
191 buf_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
192 tss->payload_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
193 tss->state = MPEGTS_PESHEADER_SIZE;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
194 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
195 case MPEGTS_PESHEADER_SIZE:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
196 tss->pes_header_size = *p++;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
197 buf_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
198 tss->payload_size--;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
199 tss->state = MPEGTS_PESHEADER_READ;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
200 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
201 case MPEGTS_PESHEADER_READ:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
202 /* currently we do nothing except skipping */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
203 if (len > tss->pes_header_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
204 len = tss->pes_header_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
205 p += len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
206 buf_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
207 tss->pes_header_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
208 tss->payload_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
209 if (tss->pes_header_size == 0)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
210 tss->state = MPEGTS_PAYLOAD;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
211 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
212 case MPEGTS_PAYLOAD:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
213 if (len > tss->payload_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
214 len = tss->payload_size;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
215 if (len > 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
216 if (tss->st && av_new_packet(pkt, buf_size) == 0) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
217 memcpy(pkt->data, p, buf_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
218 pkt->stream_index = tss->st->index;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
219 return 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
220 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
221 tss->payload_size -= len;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
222 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
223 buf_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
224 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
225 case MPEGTS_SKIP:
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
226 buf_size = 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
227 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
228 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
229 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
230 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
231 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
232
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
233 static int mpegts_read_packet(AVFormatContext *s,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
234 AVPacket *pkt)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
235 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
236 MpegTSContext *ts = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
237 MpegTSStream *tss;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
238 ByteIOContext *pb = &s->pb;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
239 unsigned char packet[TS_FEC_PACKET_SIZE];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
240 int len, pid, cc, cc_ok, afc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
241 const unsigned char *p;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
242
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
243 for(;;) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
244 len = get_buffer(pb, packet, ts->raw_packet_size);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
245 if (len != ts->raw_packet_size)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
246 return AVERROR_IO;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
247 /* check paquet sync byte */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
248 /* XXX: accept to resync ? */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
249 if (packet[0] != 0x47)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
250 return AVERROR_INVALIDDATA;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
251
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
252 pid = ((packet[1] & 0x1f) << 8) | packet[2];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
253 tss = ts->pids[pid];
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
254 if (tss == NULL) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
255 /* if no pid found, then add a pid context */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
256 tss = av_mallocz(sizeof(MpegTSStream));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
257 if (!tss)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
258 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
259 ts->pids[pid] = tss;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
260 tss->pid = pid;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
261 tss->last_cc = -1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
262 // printf("new pid=0x%x\n", pid);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
263 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
264
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
265 /* continuity check (currently not used) */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
266 cc = (packet[3] & 0xf);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
267 cc_ok = (tss->last_cc < 0) || ((((tss->last_cc + 1) & 0x0f) == cc));
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
268 tss->last_cc = cc;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
269
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
270 /* skip adaptation field */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
271 afc = (packet[3] >> 4) & 3;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
272 p = packet + 4;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
273 if (afc == 0) /* reserved value */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
274 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
275 if (afc == 2) /* adaptation field only */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
276 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
277 if (afc == 3) {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
278 /* skip adapation field */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
279 p += p[0] + 1;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
280 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
281 /* if past the end of packet, ignore */
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
282 if (p >= packet + TS_PACKET_SIZE)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
283 continue;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
284
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
285 if (mpegts_push_data(s, tss, pkt, p, TS_PACKET_SIZE - (p - packet),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
286 packet[1] & 0x40))
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
287 break;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
288 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
289 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
290 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
291
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
292 static int mpegts_read_close(AVFormatContext *s)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
293 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
294 MpegTSContext *ts = s->priv_data;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
295 int i;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
296 for(i=0;i<NB_PID_MAX;i++)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
297 av_free(ts->pids[i]);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
298 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
299 }
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
300
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
301 AVInputFormat mpegts_demux = {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
302 "mpegts",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
303 "MPEG2 transport stream format",
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
304 sizeof(MpegTSContext),
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
305 mpegts_probe,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
306 mpegts_read_header,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
307 mpegts_read_packet,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
308 mpegts_read_close,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
309 .flags = AVFMT_NOHEADER | AVFMT_SHOW_IDS,
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
310 };
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
311
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
312 int mpegts_init(void)
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
313 {
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
314 av_register_input_format(&mpegts_demux);
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
315 return 0;
05318cf2e886 renamed libav to libavformat
bellard
parents:
diff changeset
316 }