2 * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
3 * Copyright (c) 2003 The ffmpeg Project
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.
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.
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
22 * Westwood Studios VQA & AUD file demuxers
23 * by Mike Melanson (melanson@pcisys.net)
24 * for more information on the Westwood file formats, visit:
25 * http://www.pcisys.net/~melanson/codecs/
26 * http://www.geocities.com/SiliconValley/8682/aud3.txt
28 * Implementation note: There is no definite file signature for AUD files.
29 * The demuxer uses a probabilistic strategy for content detection. This
30 * entails performing sanity checks on certain header values in order to
31 * qualify a file. Refer to wsaud_probe() for the precise parameters.
36 #define AUD_HEADER_SIZE 12
37 #define AUD_CHUNK_PREAMBLE_SIZE 8
38 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
40 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
41 #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
42 #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
43 #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
44 #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
45 #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
46 #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
48 /* don't know what these tags are for, but acknowledge their existence */
49 #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
50 #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
51 #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
52 #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
53 #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
54 #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
56 #define VQA_HEADER_SIZE 0x2A
57 #define VQA_FRAMERATE 15
58 #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
59 #define VQA_PREAMBLE_SIZE 8
61 typedef struct WsAudDemuxContext {
66 int audio_stream_index;
67 int64_t audio_frame_counter;
70 typedef struct WsVqaDemuxContext {
75 int audio_stream_index;
76 int video_stream_index;
78 int64_t audio_frame_counter;
82 static int wsaud_probe(AVProbeData *p)
86 /* Probabilistic content detection strategy: There is no file signature
87 * so perform sanity checks on various header parameters:
88 * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
89 * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
90 * There is a total of 24 bits. The number space contains 2^24 =
91 * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
92 * of numbers. There is a 80002/16777216 = 0.48% chance of a false
96 if (p->buf_size < AUD_HEADER_SIZE)
99 /* check sample rate */
100 field = LE_16(&p->buf[0]);
101 if ((field < 8000) || (field > 48000))
104 /* note: only check for WS IMA (type 99) right now since there is no
105 * support for type 1 */
106 if (p->buf[11] != 99)
109 /* return 1/2 certainty since this file check is a little sketchy */
110 return AVPROBE_SCORE_MAX / 2;
113 static int wsaud_read_header(AVFormatContext *s,
114 AVFormatParameters *ap)
116 WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
117 ByteIOContext *pb = &s->pb;
119 unsigned char header[AUD_HEADER_SIZE];
121 if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
123 wsaud->audio_samplerate = LE_16(&header[0]);
124 if (header[11] == 99)
125 wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
127 return AVERROR_INVALIDDATA;
129 /* flag 0 indicates stereo */
130 wsaud->audio_channels = (header[10] & 0x1) + 1;
131 /* flag 1 indicates 16 bit audio */
132 wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
134 /* initialize the audio decoder stream */
135 st = av_new_stream(s, 0);
137 return AVERROR_NOMEM;
138 av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
139 st->codec.codec_type = CODEC_TYPE_AUDIO;
140 st->codec.codec_id = wsaud->audio_type;
141 st->codec.codec_tag = 0; /* no tag */
142 st->codec.channels = wsaud->audio_channels;
143 st->codec.sample_rate = wsaud->audio_samplerate;
144 st->codec.bits_per_sample = wsaud->audio_bits;
145 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
146 st->codec.bits_per_sample / 4;
147 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
149 wsaud->audio_stream_index = st->index;
150 wsaud->audio_frame_counter = 0;
155 static int wsaud_read_packet(AVFormatContext *s,
158 WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
159 ByteIOContext *pb = &s->pb;
160 unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
161 unsigned int chunk_size;
164 if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
165 AUD_CHUNK_PREAMBLE_SIZE)
168 /* validate the chunk */
169 if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
170 return AVERROR_INVALIDDATA;
172 chunk_size = LE_16(&preamble[0]);
173 if (av_new_packet(pkt, chunk_size))
175 pkt->stream_index = wsaud->audio_stream_index;
176 pkt->pts = wsaud->audio_frame_counter;
177 pkt->pts /= wsaud->audio_samplerate;
178 if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
183 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
184 wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
189 static int wsaud_read_close(AVFormatContext *s)
191 // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
197 static int wsvqa_probe(AVProbeData *p)
199 /* need 12 bytes to qualify */
200 if (p->buf_size < 12)
203 /* check for the VQA signatures */
204 if ((BE_32(&p->buf[0]) != FORM_TAG) ||
205 (BE_32(&p->buf[8]) != WVQA_TAG))
208 return AVPROBE_SCORE_MAX;
211 static int wsvqa_read_header(AVFormatContext *s,
212 AVFormatParameters *ap)
214 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
215 ByteIOContext *pb = &s->pb;
217 unsigned char *header;
218 unsigned char scratch[VQA_PREAMBLE_SIZE];
219 unsigned int chunk_tag;
220 unsigned int chunk_size;
222 /* initialize the video decoder stream */
223 st = av_new_stream(s, 0);
225 return AVERROR_NOMEM;
226 av_set_pts_info(st, 33, 1, 90000);
227 wsvqa->video_stream_index = st->index;
228 st->codec.codec_type = CODEC_TYPE_VIDEO;
229 st->codec.codec_id = CODEC_ID_WS_VQA;
230 st->codec.codec_tag = 0; /* no fourcc */
232 /* skip to the start of the VQA header */
233 url_fseek(pb, 20, SEEK_SET);
235 /* the VQA header needs to go to the decoder */
236 st->codec.extradata_size = VQA_HEADER_SIZE;
237 st->codec.extradata = av_malloc(VQA_HEADER_SIZE);
238 header = (unsigned char *)st->codec.extradata;
239 if (get_buffer(pb, st->codec.extradata, VQA_HEADER_SIZE) !=
241 av_free(st->codec.extradata);
244 st->codec.width = LE_16(&header[6]);
245 st->codec.height = LE_16(&header[8]);
247 /* initialize the audio decoder stream is sample rate is non-zero */
248 if (LE_16(&header[24])) {
249 st = av_new_stream(s, 0);
251 return AVERROR_NOMEM;
252 av_set_pts_info(st, 33, 1, 90000);
253 st->codec.codec_type = CODEC_TYPE_AUDIO;
254 st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
255 st->codec.codec_tag = 0; /* no tag */
256 st->codec.sample_rate = LE_16(&header[24]);
257 st->codec.channels = header[26];
258 st->codec.bits_per_sample = 16;
259 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
260 st->codec.bits_per_sample / 4;
261 st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
263 wsvqa->audio_stream_index = st->index;
264 wsvqa->audio_samplerate = st->codec.sample_rate;
265 wsvqa->audio_channels = st->codec.channels;
266 wsvqa->audio_frame_counter = 0;
269 /* there are 0 or more chunks before the FINF chunk; iterate until
270 * FINF has been skipped and the file will be ready to be demuxed */
272 if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
273 av_free(st->codec.extradata);
276 chunk_tag = BE_32(&scratch[0]);
277 chunk_size = BE_32(&scratch[4]);
279 /* catch any unknown header tags, for curiousity */
291 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
292 scratch[0], scratch[1],
293 scratch[2], scratch[3]);
297 url_fseek(pb, chunk_size, SEEK_CUR);
298 } while (chunk_tag != FINF_TAG);
300 wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
305 static int wsvqa_read_packet(AVFormatContext *s,
308 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
309 ByteIOContext *pb = &s->pb;
311 unsigned char preamble[VQA_PREAMBLE_SIZE];
312 unsigned int chunk_type;
313 unsigned int chunk_size;
316 if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
319 chunk_type = BE_32(&preamble[0]);
320 chunk_size = BE_32(&preamble[4]);
321 skip_byte = chunk_size & 0x01;
323 if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
325 if (av_new_packet(pkt, chunk_size))
327 ret = get_buffer(pb, pkt->data, chunk_size);
328 if (ret != chunk_size) {
333 if (chunk_type == SND2_TAG) {
334 pkt->stream_index = wsvqa->audio_stream_index;
337 pkt->pts *= wsvqa->audio_frame_counter;
338 pkt->pts /= wsvqa->audio_samplerate;
340 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
341 wsvqa->audio_frame_counter += (chunk_size * 2) /
342 wsvqa->audio_channels;
344 pkt->stream_index = wsvqa->video_stream_index;
345 pkt->pts = wsvqa->video_pts;
346 wsvqa->video_pts += VQA_VIDEO_PTS_INC;
350 return AVERROR_INVALIDDATA;
352 /* stay on 16-bit alignment */
354 url_fseek(pb, 1, SEEK_CUR);
359 static int wsvqa_read_close(AVFormatContext *s)
361 // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
366 static AVInputFormat wsaud_iformat = {
368 "Westwood Studios audio format",
369 sizeof(WsAudDemuxContext),
376 static AVInputFormat wsvqa_iformat = {
378 "Westwood Studios VQA format",
379 sizeof(WsVqaDemuxContext),
386 int westwood_init(void)
388 av_register_input_format(&wsaud_iformat);
389 av_register_input_format(&wsvqa_iformat);