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