get the video dispatch straight
[libav.git] / libavformat / 4xm.c
1 /*
2 * 4X Technologies .4xm File Demuxer (no muxer)
3 * Copyright (c) 2003 The ffmpeg Project
4 *
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.
9 *
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.
14 *
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
18 */
19
20 /**
21 * @file 4xm.c
22 * 4X Technologies file demuxer
23 * by Mike Melanson (melanson@pcisys.net)
24 * for more information on the .4xm file format, visit:
25 * http://www.pcisys.net/~melanson/codecs/
26 */
27
28 #include "avformat.h"
29
30 #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
31 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
32 (((uint8_t*)(x))[2] << 16) | \
33 (((uint8_t*)(x))[1] << 8) | \
34 ((uint8_t*)(x))[0])
35
36 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
37 ( (long)(unsigned char)(ch0) | \
38 ( (long)(unsigned char)(ch1) << 8 ) | \
39 ( (long)(unsigned char)(ch2) << 16 ) | \
40 ( (long)(unsigned char)(ch3) << 24 ) )
41
42 #define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
43 #define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
44 #define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
45 #define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
46 #define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
47 #define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
48 #define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
49 #define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
50 #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
51 #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
52 #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
53 #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
54 #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
55 #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
56 #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
57 #define _TAG FOURCC_TAG('', '', '', '')
58
59 #define vtrk_SIZE 0x44
60 #define strk_SIZE 0x28
61
62 #define GET_LIST_HEADER() \
63 fourcc_tag = get_le32(pb); \
64 size = get_le32(pb); \
65 if (fourcc_tag != LIST_TAG) \
66 return AVERROR_INVALIDDATA; \
67 fourcc_tag = get_le32(pb);
68
69 typedef struct AudioTrack {
70 int sample_rate;
71 int bits;
72 int channels;
73 int stream_index;
74 } AudioTrack;
75
76 typedef struct FourxmDemuxContext {
77 int width;
78 int height;
79 int video_stream_index;
80 int track_count;
81 AudioTrack *tracks;
82 int selected_track;
83
84 int64_t pts;
85 int last_chunk_was_audio;
86 int last_audio_frame_count;
87 } FourxmDemuxContext;
88
89 static int fourxm_probe(AVProbeData *p)
90 {
91 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
92 (LE_32(&p->buf[8]) != _4XMV_TAG))
93 return 0;
94
95 return AVPROBE_SCORE_MAX;
96 }
97
98 static int fourxm_read_header(AVFormatContext *s,
99 AVFormatParameters *ap)
100 {
101 ByteIOContext *pb = &s->pb;
102 unsigned int fourcc_tag;
103 unsigned int size;
104 int header_size;
105 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
106 unsigned char *header;
107 int i;
108 int current_track = -1;
109 AVStream *st;
110
111 fourxm->track_count = 0;
112 fourxm->tracks = NULL;
113 fourxm->selected_track = 0;
114
115 /* skip the first 3 32-bit numbers */
116 url_fseek(pb, 12, SEEK_CUR);
117
118 /* check for LIST-HEAD */
119 GET_LIST_HEADER();
120 header_size = size - 4;
121 if (fourcc_tag != HEAD_TAG)
122 return AVERROR_INVALIDDATA;
123
124 /* allocate space for the header and load the whole thing */
125 header = av_malloc(header_size);
126 if (!header)
127 return AVERROR_NOMEM;
128 if (get_buffer(pb, header, header_size) != header_size)
129 return AVERROR_IO;
130
131 /* take the lazy approach and search for any and all vtrk and strk chunks */
132 for (i = 0; i < header_size - 8; i++) {
133 fourcc_tag = LE_32(&header[i]);
134 size = LE_32(&header[i + 4]);
135
136 if (fourcc_tag == vtrk_TAG) {
137 /* check that there is enough data */
138 if (size != vtrk_SIZE) {
139 av_free(header);
140 return AVERROR_INVALIDDATA;
141 }
142 fourxm->width = LE_32(&header[i + 36]);
143 fourxm->height = LE_32(&header[i + 40]);
144 i += 8 + size;
145
146 /* allocate a new AVStream */
147 st = av_new_stream(s, 0);
148 if (!st)
149 return AVERROR_NOMEM;
150
151 fourxm->video_stream_index = st->index;
152
153 st->codec.codec_type = CODEC_TYPE_VIDEO;
154 st->codec.codec_id = CODEC_ID_4XM;
155 st->codec.codec_tag = 0; /* no fourcc */
156 st->codec.width = fourxm->width;
157 st->codec.height = fourxm->height;
158
159 } else if (fourcc_tag == strk_TAG) {
160 /* check that there is enough data */
161 if (size != strk_SIZE) {
162 av_free(header);
163 return AVERROR_INVALIDDATA;
164 }
165 current_track = LE_32(&header[i + 8]);
166 if (current_track + 1 > fourxm->track_count) {
167 fourxm->track_count = current_track + 1;
168 fourxm->tracks = av_realloc(fourxm->tracks,
169 fourxm->track_count * sizeof(AudioTrack));
170 if (!fourxm->tracks) {
171 av_free(header);
172 return AVERROR_NOMEM;
173 }
174 }
175 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
176 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
177 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
178 i += 8 + size;
179
180 /* allocate a new AVStream */
181 st = av_new_stream(s, current_track);
182 if (!st)
183 return AVERROR_NOMEM;
184
185 fourxm->tracks[current_track].stream_index = st->index;
186
187 st->codec.codec_type = CODEC_TYPE_AUDIO;
188 st->codec.codec_tag = 1;
189 st->codec.channels = fourxm->tracks[current_track].channels;
190 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
191 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
192 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
193 st->codec.bits_per_sample;
194 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
195 if (st->codec.bits_per_sample == 8)
196 st->codec.codec_id = CODEC_ID_PCM_U8;
197 else
198 st->codec.codec_id = CODEC_ID_PCM_S16LE;
199 }
200 }
201
202 av_free(header);
203
204 /* skip over the LIST-MOVI chunk (which is where the stream should be */
205 GET_LIST_HEADER();
206 if (fourcc_tag != MOVI_TAG)
207 return AVERROR_INVALIDDATA;
208
209 /* initialize context members */
210 fourxm->pts = 0;
211 fourxm->last_chunk_was_audio = 0;
212 fourxm->last_audio_frame_count = 0;
213
214 /* set the pts reference (1 pts = 1/90000) */
215 s->pts_num = 1;
216 s->pts_den = 90000;
217
218 return 0;
219 }
220
221 static int fourxm_read_packet(AVFormatContext *s,
222 AVPacket *pkt)
223 {
224 FourxmDemuxContext *fourxm = s->priv_data;
225 ByteIOContext *pb = &s->pb;
226 unsigned int fourcc_tag;
227 unsigned int size;
228 int ret = 0;
229 int track_number;
230 int packet_read = 0;
231 unsigned char header[8];
232 int64_t pts_inc;
233
234 while (!packet_read) {
235
236 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
237 return ret;
238 fourcc_tag = LE_32(&header[0]);
239 size = LE_32(&header[4]);
240
241 if (url_feof(pb))
242 return -EIO;
243
244 switch (fourcc_tag) {
245
246 case LIST_TAG:
247 /* skip the LIST-* tag and move on to the next fourcc */
248 get_le32(pb);
249 break;
250
251 case ifrm_TAG:
252 case pfrm_TAG:
253 case cfrm_TAG:{
254
255 int id, whole;
256 static int stats[1000];
257
258 /* bump the pts if this last data sent out was audio */
259 if (fourxm->last_chunk_was_audio) {
260 fourxm->last_chunk_was_audio = 0;
261 pts_inc = fourxm->last_audio_frame_count;
262 pts_inc *= 90000;
263 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
264 fourxm->pts += pts_inc;
265 }
266
267 /* allocate 8 more bytes than 'size' to account for fourcc
268 * and size */
269 if (av_new_packet(pkt, size + 8))
270 return -EIO;
271 pkt->stream_index = fourxm->video_stream_index;
272 pkt->pts = fourxm->pts;
273 memcpy(pkt->data, header, 8);
274 ret = get_buffer(&s->pb, &pkt->data[8], size);
275
276 if (fourcc_tag == cfrm_TAG) {
277 id = LE_32(&pkt->data[12]);
278 whole = LE_32(&pkt->data[16]);
279 stats[id] += size - 12;
280 printf(" cfrm chunk id:%d size:%d whole:%d until now:%d\n", id, size, whole, stats[id]);
281 }
282
283 if (ret < 0)
284 av_free_packet(pkt);
285 else
286 packet_read = 1;
287 break;
288 }
289
290 case snd__TAG:
291 printf (" snd_ chunk, ");
292 track_number = get_le32(pb);
293 size = get_le32(pb);
294 if (track_number == fourxm->selected_track) {
295 printf ("correct track, dispatching...\n");
296 if (av_new_packet(pkt, size))
297 return -EIO;
298 pkt->stream_index =
299 fourxm->tracks[fourxm->selected_track].stream_index;
300 pkt->pts = fourxm->pts;
301 ret = get_buffer(&s->pb, pkt->data, size);
302 if (ret < 0)
303 av_free_packet(pkt);
304 else
305 packet_read = 1;
306
307 /* maintain pts info for the benefit of the video track */
308 fourxm->last_chunk_was_audio = 1;
309 fourxm->last_audio_frame_count = size /
310 ((fourxm->tracks[fourxm->selected_track].bits / 8) *
311 fourxm->tracks[fourxm->selected_track].channels);
312
313 } else {
314 printf ("wrong track, skipping...\n");
315 url_fseek(pb, size, SEEK_CUR);
316 }
317 break;
318
319 default:
320 url_fseek(pb, size, SEEK_CUR);
321 break;
322 }
323 }
324
325 return ret;
326 }
327
328 static int fourxm_read_close(AVFormatContext *s)
329 {
330 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
331
332 av_free(fourxm->tracks);
333
334 return 0;
335 }
336
337 static AVInputFormat fourxm_iformat = {
338 "4xm",
339 "4X Technologies format",
340 sizeof(FourxmDemuxContext),
341 fourxm_probe,
342 fourxm_read_header,
343 fourxm_read_packet,
344 fourxm_read_close,
345 };
346
347 int fourxm_init(void)
348 {
349 av_register_input_format(&fourxm_iformat);
350 return 0;
351 }