remove useless cast that makes code unreadable
[libav.git] / libavformat / westwood.c
CommitLineData
2fdf638b
MM
1/*
2 * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
3 * Copyright (c) 2003 The ffmpeg Project
4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
2fdf638b
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
2fdf638b 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
2fdf638b
MM
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2fdf638b
MM
20 */
21
22/**
23 * @file westwood.c
24 * Westwood Studios VQA & AUD file demuxers
25 * by Mike Melanson (melanson@pcisys.net)
26 * for more information on the Westwood file formats, visit:
27 * http://www.pcisys.net/~melanson/codecs/
28 * http://www.geocities.com/SiliconValley/8682/aud3.txt
29 *
30 * Implementation note: There is no definite file signature for AUD files.
31 * The demuxer uses a probabilistic strategy for content detection. This
32 * entails performing sanity checks on certain header values in order to
33 * qualify a file. Refer to wsaud_probe() for the precise parameters.
34 */
35
36#include "avformat.h"
37
2fdf638b
MM
38#define AUD_HEADER_SIZE 12
39#define AUD_CHUNK_PREAMBLE_SIZE 8
40#define AUD_CHUNK_SIGNATURE 0x0000DEAF
41
3a278992
MM
42#define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
43#define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
44#define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
45#define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
46#define SND0_TAG MKBETAG('S', 'N', 'D', '0')
61710f7b 47#define SND1_TAG MKBETAG('S', 'N', 'D', '1')
3a278992
MM
48#define SND2_TAG MKBETAG('S', 'N', 'D', '2')
49#define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
2fdf638b 50
20f269dc 51/* don't know what these tags are for, but acknowledge their existence */
3a278992
MM
52#define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
53#define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
54#define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
55#define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
56#define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
57#define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
61710f7b 58#define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
20f269dc 59
2fdf638b
MM
60#define VQA_HEADER_SIZE 0x2A
61#define VQA_FRAMERATE 15
62#define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
63#define VQA_PREAMBLE_SIZE 8
64
65typedef struct WsAudDemuxContext {
66 int audio_samplerate;
67 int audio_channels;
68 int audio_bits;
69 int audio_type;
70 int audio_stream_index;
71 int64_t audio_frame_counter;
72} WsAudDemuxContext;
73
74typedef struct WsVqaDemuxContext {
75 int audio_samplerate;
76 int audio_channels;
77 int audio_bits;
78
79 int audio_stream_index;
80 int video_stream_index;
81
82 int64_t audio_frame_counter;
83 int64_t video_pts;
84} WsVqaDemuxContext;
85
86static int wsaud_probe(AVProbeData *p)
87{
88 int field;
89
90 /* Probabilistic content detection strategy: There is no file signature
91 * so perform sanity checks on various header parameters:
92 * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
93 * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
94 * There is a total of 24 bits. The number space contains 2^24 =
95 * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
96 * of numbers. There is a 80002/16777216 = 0.48% chance of a false
97 * positive.
98 */
99
100 if (p->buf_size < AUD_HEADER_SIZE)
101 return 0;
102
103 /* check sample rate */
104 field = LE_16(&p->buf[0]);
105 if ((field < 8000) || (field > 48000))
106 return 0;
107
115329f1 108 /* note: only check for WS IMA (type 99) right now since there is no
2fdf638b
MM
109 * support for type 1 */
110 if (p->buf[11] != 99)
111 return 0;
112
113 /* return 1/2 certainty since this file check is a little sketchy */
114 return AVPROBE_SCORE_MAX / 2;
115}
116
117static int wsaud_read_header(AVFormatContext *s,
118 AVFormatParameters *ap)
119{
120 WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
121 ByteIOContext *pb = &s->pb;
122 AVStream *st;
123 unsigned char header[AUD_HEADER_SIZE];
124
125 if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
0bd586c5 126 return AVERROR_IO;
2fdf638b
MM
127 wsaud->audio_samplerate = LE_16(&header[0]);
128 if (header[11] == 99)
129 wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
130 else
131 return AVERROR_INVALIDDATA;
132
133 /* flag 0 indicates stereo */
134 wsaud->audio_channels = (header[10] & 0x1) + 1;
135 /* flag 1 indicates 16 bit audio */
136 wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
137
2fdf638b
MM
138 /* initialize the audio decoder stream */
139 st = av_new_stream(s, 0);
140 if (!st)
141 return AVERROR_NOMEM;
9ee91c2f 142 av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
01f4895c
MN
143 st->codec->codec_type = CODEC_TYPE_AUDIO;
144 st->codec->codec_id = wsaud->audio_type;
145 st->codec->codec_tag = 0; /* no tag */
146 st->codec->channels = wsaud->audio_channels;
147 st->codec->sample_rate = wsaud->audio_samplerate;
148 st->codec->bits_per_sample = wsaud->audio_bits;
149 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
150 st->codec->bits_per_sample / 4;
151 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
2fdf638b
MM
152
153 wsaud->audio_stream_index = st->index;
154 wsaud->audio_frame_counter = 0;
155
156 return 0;
157}
158
159static int wsaud_read_packet(AVFormatContext *s,
160 AVPacket *pkt)
161{
162 WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
163 ByteIOContext *pb = &s->pb;
164 unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
165 unsigned int chunk_size;
166 int ret = 0;
167
168 if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
169 AUD_CHUNK_PREAMBLE_SIZE)
0bd586c5 170 return AVERROR_IO;
2fdf638b
MM
171
172 /* validate the chunk */
173 if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
174 return AVERROR_INVALIDDATA;
175
176 chunk_size = LE_16(&preamble[0]);
2692067a
MN
177 ret= av_get_packet(pb, pkt, chunk_size);
178 if (ret != chunk_size)
0bd586c5 179 return AVERROR_IO;
2fdf638b
MM
180 pkt->stream_index = wsaud->audio_stream_index;
181 pkt->pts = wsaud->audio_frame_counter;
182 pkt->pts /= wsaud->audio_samplerate;
2fdf638b
MM
183
184 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
185 wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
186
187 return ret;
188}
189
190static int wsaud_read_close(AVFormatContext *s)
191{
192// WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
193
194 return 0;
195}
196
197
198static int wsvqa_probe(AVProbeData *p)
199{
200 /* need 12 bytes to qualify */
201 if (p->buf_size < 12)
202 return 0;
203
204 /* check for the VQA signatures */
205 if ((BE_32(&p->buf[0]) != FORM_TAG) ||
206 (BE_32(&p->buf[8]) != WVQA_TAG))
207 return 0;
208
209 return AVPROBE_SCORE_MAX;
210}
211
212static int wsvqa_read_header(AVFormatContext *s,
213 AVFormatParameters *ap)
214{
215 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
216 ByteIOContext *pb = &s->pb;
217 AVStream *st;
218 unsigned char *header;
219 unsigned char scratch[VQA_PREAMBLE_SIZE];
20f269dc
MM
220 unsigned int chunk_tag;
221 unsigned int chunk_size;
2fdf638b 222
2fdf638b
MM
223 /* initialize the video decoder stream */
224 st = av_new_stream(s, 0);
225 if (!st)
226 return AVERROR_NOMEM;
5f3c4ba4 227 av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
2fdf638b 228 wsvqa->video_stream_index = st->index;
01f4895c
MN
229 st->codec->codec_type = CODEC_TYPE_VIDEO;
230 st->codec->codec_id = CODEC_ID_WS_VQA;
231 st->codec->codec_tag = 0; /* no fourcc */
2fdf638b
MM
232
233 /* skip to the start of the VQA header */
234 url_fseek(pb, 20, SEEK_SET);
235
236 /* the VQA header needs to go to the decoder */
01f4895c 237 st->codec->extradata_size = VQA_HEADER_SIZE;
d76319b1 238 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
01f4895c
MN
239 header = (unsigned char *)st->codec->extradata;
240 if (get_buffer(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
2fdf638b 241 VQA_HEADER_SIZE) {
01f4895c 242 av_free(st->codec->extradata);
0bd586c5 243 return AVERROR_IO;
2fdf638b 244 }
01f4895c
MN
245 st->codec->width = LE_16(&header[6]);
246 st->codec->height = LE_16(&header[8]);
2fdf638b 247
61710f7b 248 /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
dfbb76d3 249 if (LE_16(&header[24]) || (LE_16(&header[0]) == 1 && LE_16(&header[2]) == 1)) {
20f269dc
MM
250 st = av_new_stream(s, 0);
251 if (!st)
252 return AVERROR_NOMEM;
5f3c4ba4 253 av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
01f4895c 254 st->codec->codec_type = CODEC_TYPE_AUDIO;
61710f7b
KS
255 if (LE_16(&header[0]) == 1)
256 st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
257 else
258 st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
01f4895c
MN
259 st->codec->codec_tag = 0; /* no tag */
260 st->codec->sample_rate = LE_16(&header[24]);
61710f7b
KS
261 if (!st->codec->sample_rate)
262 st->codec->sample_rate = 22050;
01f4895c 263 st->codec->channels = header[26];
61710f7b
KS
264 if (!st->codec->channels)
265 st->codec->channels = 1;
01f4895c
MN
266 st->codec->bits_per_sample = 16;
267 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
268 st->codec->bits_per_sample / 4;
269 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
20f269dc
MM
270
271 wsvqa->audio_stream_index = st->index;
01f4895c
MN
272 wsvqa->audio_samplerate = st->codec->sample_rate;
273 wsvqa->audio_channels = st->codec->channels;
20f269dc
MM
274 wsvqa->audio_frame_counter = 0;
275 }
2fdf638b 276
20f269dc
MM
277 /* there are 0 or more chunks before the FINF chunk; iterate until
278 * FINF has been skipped and the file will be ready to be demuxed */
279 do {
280 if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
01f4895c 281 av_free(st->codec->extradata);
0bd586c5 282 return AVERROR_IO;
20f269dc
MM
283 }
284 chunk_tag = BE_32(&scratch[0]);
285 chunk_size = BE_32(&scratch[4]);
286
287 /* catch any unknown header tags, for curiousity */
288 switch (chunk_tag) {
289 case CINF_TAG:
290 case CINH_TAG:
291 case CIND_TAG:
292 case PINF_TAG:
293 case PINH_TAG:
294 case PIND_TAG:
295 case FINF_TAG:
61710f7b 296 case CMDS_TAG:
20f269dc
MM
297 break;
298
299 default:
bc874dae 300 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
20f269dc
MM
301 scratch[0], scratch[1],
302 scratch[2], scratch[3]);
303 break;
304 }
305
306 url_fseek(pb, chunk_size, SEEK_CUR);
307 } while (chunk_tag != FINF_TAG);
2fdf638b 308
2fdf638b
MM
309 wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
310
311 return 0;
312}
313
314static int wsvqa_read_packet(AVFormatContext *s,
315 AVPacket *pkt)
316{
317 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
318 ByteIOContext *pb = &s->pb;
61710f7b 319 int ret = -1;
2fdf638b
MM
320 unsigned char preamble[VQA_PREAMBLE_SIZE];
321 unsigned int chunk_type;
322 unsigned int chunk_size;
323 int skip_byte;
324
61710f7b
KS
325 while (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
326 chunk_type = BE_32(&preamble[0]);
327 chunk_size = BE_32(&preamble[4]);
328 skip_byte = chunk_size & 0x01;
329
330 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
331
332 if (av_new_packet(pkt, chunk_size))
333 return AVERROR_IO;
334 ret = get_buffer(pb, pkt->data, chunk_size);
335 if (ret != chunk_size) {
336 av_free_packet(pkt);
337 return AVERROR_IO;
338 }
339
340 if (chunk_type == SND2_TAG) {
341 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b
KS
342 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
343 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
344 } else if(chunk_type == SND1_TAG) {
345 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b
KS
346 /* unpacked size is stored in header */
347 wsvqa->audio_frame_counter += LE_16(pkt->data) / wsvqa->audio_channels;
348 } else {
349 pkt->stream_index = wsvqa->video_stream_index;
61710f7b
KS
350 wsvqa->video_pts += VQA_VIDEO_PTS_INC;
351 }
352 /* stay on 16-bit alignment */
353 if (skip_byte)
354 url_fseek(pb, 1, SEEK_CUR);
355
356 return ret;
2fdf638b 357 } else {
61710f7b
KS
358 switch(chunk_type){
359 case CMDS_TAG:
360 case SND0_TAG:
361 break;
362 default:
363 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
364 }
365 url_fseek(pb, chunk_size + skip_byte, SEEK_CUR);
2fdf638b 366 }
61710f7b 367 }
2fdf638b
MM
368
369 return ret;
370}
371
372static int wsvqa_read_close(AVFormatContext *s)
373{
374// WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
375
376 return 0;
377}
378
ff70e601
MR
379#ifdef CONFIG_WSAUD_DEMUXER
380AVInputFormat wsaud_demuxer = {
2fdf638b
MM
381 "wsaud",
382 "Westwood Studios audio format",
383 sizeof(WsAudDemuxContext),
384 wsaud_probe,
385 wsaud_read_header,
386 wsaud_read_packet,
387 wsaud_read_close,
388};
ff70e601
MR
389#endif
390#ifdef CONFIG_WSVQA_DEMUXER
391AVInputFormat wsvqa_demuxer = {
2fdf638b
MM
392 "wsvqa",
393 "Westwood Studios VQA format",
394 sizeof(WsVqaDemuxContext),
395 wsvqa_probe,
396 wsvqa_read_header,
397 wsvqa_read_packet,
398 wsvqa_read_close,
399};
ff70e601 400#endif