set frame rate information, for good measure
[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 std__TAG FOURCC_TAG('s', 't', 'd', '_')
51 #define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
52 #define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
53 #define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
54 #define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
55 #define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
56 #define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
57 #define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
58 #define _TAG FOURCC_TAG('', '', '', '')
59
60 #define vtrk_SIZE 0x44
61 #define strk_SIZE 0x28
62
63 #define GET_LIST_HEADER() \
64 fourcc_tag = get_le32(pb); \
65 size = get_le32(pb); \
66 if (fourcc_tag != LIST_TAG) \
67 return AVERROR_INVALIDDATA; \
68 fourcc_tag = get_le32(pb);
69
70 typedef struct AudioTrack {
71 int sample_rate;
72 int bits;
73 int channels;
74 int stream_index;
75 int adpcm;
76 } AudioTrack;
77
78 typedef struct FourxmDemuxContext {
79 int width;
80 int height;
81 int video_stream_index;
82 int track_count;
83 AudioTrack *tracks;
84 int selected_track;
85
86 int64_t audio_pts;
87 int64_t video_pts;
88 int video_pts_inc;
89 float fps;
90 } FourxmDemuxContext;
91
92 static float get_le_float(unsigned char *buffer)
93 {
94 float f;
95 unsigned char *float_buffer = (unsigned char *)&f;
96
97 #ifdef WORDS_BIGENDIAN
98 float_buffer[0] = buffer[3];
99 float_buffer[1] = buffer[2];
100 float_buffer[2] = buffer[1];
101 float_buffer[3] = buffer[0];
102 #else
103 float_buffer[0] = buffer[0];
104 float_buffer[1] = buffer[1];
105 float_buffer[2] = buffer[2];
106 float_buffer[3] = buffer[3];
107 #endif
108
109 return f;
110 }
111
112 static int fourxm_probe(AVProbeData *p)
113 {
114 if (p->buf_size < 12)
115 return 0;
116
117 if ((LE_32(&p->buf[0]) != RIFF_TAG) ||
118 (LE_32(&p->buf[8]) != _4XMV_TAG))
119 return 0;
120
121 return AVPROBE_SCORE_MAX;
122 }
123
124 static int fourxm_read_header(AVFormatContext *s,
125 AVFormatParameters *ap)
126 {
127 ByteIOContext *pb = &s->pb;
128 unsigned int fourcc_tag;
129 unsigned int size;
130 int header_size;
131 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
132 unsigned char *header;
133 int i;
134 int current_track = -1;
135 AVStream *st;
136
137 fourxm->track_count = 0;
138 fourxm->tracks = NULL;
139 fourxm->selected_track = 0;
140 fourxm->fps = 1.0;
141
142 /* skip the first 3 32-bit numbers */
143 url_fseek(pb, 12, SEEK_CUR);
144
145 /* check for LIST-HEAD */
146 GET_LIST_HEADER();
147 header_size = size - 4;
148 if (fourcc_tag != HEAD_TAG)
149 return AVERROR_INVALIDDATA;
150
151 /* allocate space for the header and load the whole thing */
152 header = av_malloc(header_size);
153 if (!header)
154 return AVERROR_NOMEM;
155 if (get_buffer(pb, header, header_size) != header_size)
156 return AVERROR_IO;
157
158 /* take the lazy approach and search for any and all vtrk and strk chunks */
159 for (i = 0; i < header_size - 8; i++) {
160 fourcc_tag = LE_32(&header[i]);
161 size = LE_32(&header[i + 4]);
162
163 if (fourcc_tag == std__TAG) {
164 fourxm->fps = get_le_float(&header[i + 12]);
165 fourxm->video_pts_inc = (int)(90000.0 / fourxm->fps);
166 } else if (fourcc_tag == vtrk_TAG) {
167 /* check that there is enough data */
168 if (size != vtrk_SIZE) {
169 av_free(header);
170 return AVERROR_INVALIDDATA;
171 }
172 fourxm->width = LE_32(&header[i + 36]);
173 fourxm->height = LE_32(&header[i + 40]);
174 i += 8 + size;
175
176 /* allocate a new AVStream */
177 st = av_new_stream(s, 0);
178 if (!st)
179 return AVERROR_NOMEM;
180
181 fourxm->video_stream_index = st->index;
182
183 st->codec.frame_rate = fourxm->fps;
184 st->codec.frame_rate_base = 1.0;
185 st->codec.codec_type = CODEC_TYPE_VIDEO;
186 st->codec.codec_id = CODEC_ID_4XM;
187 st->codec.codec_tag = 0; /* no fourcc */
188 st->codec.width = fourxm->width;
189 st->codec.height = fourxm->height;
190
191 } else if (fourcc_tag == strk_TAG) {
192 /* check that there is enough data */
193 if (size != strk_SIZE) {
194 av_free(header);
195 return AVERROR_INVALIDDATA;
196 }
197 current_track = LE_32(&header[i + 8]);
198 if (current_track + 1 > fourxm->track_count) {
199 fourxm->track_count = current_track + 1;
200 fourxm->tracks = av_realloc(fourxm->tracks,
201 fourxm->track_count * sizeof(AudioTrack));
202 if (!fourxm->tracks) {
203 av_free(header);
204 return AVERROR_NOMEM;
205 }
206 }
207 fourxm->tracks[current_track].adpcm = LE_32(&header[i + 12]);
208 fourxm->tracks[current_track].channels = LE_32(&header[i + 36]);
209 fourxm->tracks[current_track].sample_rate = LE_32(&header[i + 40]);
210 fourxm->tracks[current_track].bits = LE_32(&header[i + 44]);
211 i += 8 + size;
212
213 /* allocate a new AVStream */
214 st = av_new_stream(s, current_track);
215 if (!st)
216 return AVERROR_NOMEM;
217
218 fourxm->tracks[current_track].stream_index = st->index;
219
220 st->codec.codec_type = CODEC_TYPE_AUDIO;
221 st->codec.codec_tag = 1;
222 st->codec.channels = fourxm->tracks[current_track].channels;
223 st->codec.sample_rate = fourxm->tracks[current_track].sample_rate;
224 st->codec.bits_per_sample = fourxm->tracks[current_track].bits;
225 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
226 st->codec.bits_per_sample;
227 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
228 if (fourxm->tracks[current_track].adpcm)
229 st->codec.codec_id = CODEC_ID_ADPCM_4XM;
230 else if (st->codec.bits_per_sample == 8)
231 st->codec.codec_id = CODEC_ID_PCM_U8;
232 else
233 st->codec.codec_id = CODEC_ID_PCM_S16LE;
234 }
235 }
236
237 av_free(header);
238
239 /* skip over the LIST-MOVI chunk (which is where the stream should be */
240 GET_LIST_HEADER();
241 if (fourcc_tag != MOVI_TAG)
242 return AVERROR_INVALIDDATA;
243
244 /* initialize context members */
245 fourxm->video_pts = -fourxm->video_pts_inc; /* first frame will push to 0 */
246 fourxm->audio_pts = 0;
247
248 /* set the pts reference (1 pts = 1/90000) */
249 s->pts_num = 1;
250 s->pts_den = 90000;
251
252 return 0;
253 }
254
255 static int fourxm_read_packet(AVFormatContext *s,
256 AVPacket *pkt)
257 {
258 FourxmDemuxContext *fourxm = s->priv_data;
259 ByteIOContext *pb = &s->pb;
260 unsigned int fourcc_tag;
261 unsigned int size, out_size;
262 int ret = 0;
263 int track_number;
264 int packet_read = 0;
265 unsigned char header[8];
266 int64_t pts_inc;
267 int audio_frame_count;
268
269 while (!packet_read) {
270
271 if ((ret = get_buffer(&s->pb, header, 8)) < 0)
272 return ret;
273 fourcc_tag = LE_32(&header[0]);
274 size = LE_32(&header[4]);
275 if (url_feof(pb))
276 return -EIO;
277 switch (fourcc_tag) {
278
279 case LIST_TAG:
280 /* this is a good time to bump the video pts */
281 fourxm->video_pts += fourxm->video_pts_inc;
282
283 /* skip the LIST-* tag and move on to the next fourcc */
284 get_le32(pb);
285 break;
286
287 case ifrm_TAG:
288 case pfrm_TAG:
289 case cfrm_TAG:{
290
291 /* allocate 8 more bytes than 'size' to account for fourcc
292 * and size */
293 if (av_new_packet(pkt, size + 8))
294 return -EIO;
295 pkt->stream_index = fourxm->video_stream_index;
296 pkt->pts = fourxm->video_pts;
297 memcpy(pkt->data, header, 8);
298 ret = get_buffer(&s->pb, &pkt->data[8], size);
299
300 if (ret < 0)
301 av_free_packet(pkt);
302 else
303 packet_read = 1;
304 break;
305 }
306
307 case snd__TAG:
308 track_number = get_le32(pb);
309 out_size= get_le32(pb);
310 size-=8;
311
312 if (track_number == fourxm->selected_track) {
313 if (av_new_packet(pkt, size))
314 return -EIO;
315 pkt->stream_index =
316 fourxm->tracks[fourxm->selected_track].stream_index;
317 pkt->pts = fourxm->audio_pts;
318 ret = get_buffer(&s->pb, pkt->data, size);
319 if (ret < 0)
320 av_free_packet(pkt);
321 else
322 packet_read = 1;
323
324 /* pts accounting */
325 audio_frame_count = size;
326 if (fourxm->tracks[fourxm->selected_track].adpcm)
327 audio_frame_count -=
328 2 * (fourxm->tracks[fourxm->selected_track].channels);
329 audio_frame_count /=
330 fourxm->tracks[fourxm->selected_track].channels;
331 if (fourxm->tracks[fourxm->selected_track].adpcm)
332 audio_frame_count *= 2;
333 else
334 audio_frame_count /=
335 (fourxm->tracks[fourxm->selected_track].bits / 8);
336 pts_inc = audio_frame_count;
337 pts_inc *= 90000;
338 pts_inc /= fourxm->tracks[fourxm->selected_track].sample_rate;
339 fourxm->audio_pts += pts_inc;
340
341 } else {
342 url_fseek(pb, size, SEEK_CUR);
343 }
344 break;
345
346 default:
347 url_fseek(pb, size, SEEK_CUR);
348 break;
349 }
350 }
351 return ret;
352 }
353
354 static int fourxm_read_close(AVFormatContext *s)
355 {
356 FourxmDemuxContext *fourxm = (FourxmDemuxContext *)s->priv_data;
357
358 av_free(fourxm->tracks);
359
360 return 0;
361 }
362
363 static AVInputFormat fourxm_iformat = {
364 "4xm",
365 "4X Technologies format",
366 sizeof(FourxmDemuxContext),
367 fourxm_probe,
368 fourxm_read_header,
369 fourxm_read_packet,
370 fourxm_read_close,
371 };
372
373 int fourxm_init(void)
374 {
375 av_register_input_format(&fourxm_iformat);
376 return 0;
377 }