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 LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
37 #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
38 (((uint8_t*)(x))[2] << 16) | \
39 (((uint8_t*)(x))[1] << 8) | \
41 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
42 (((uint8_t*)(x))[1] << 16) | \
43 (((uint8_t*)(x))[2] << 8) | \
46 #define AUD_HEADER_SIZE 12
47 #define AUD_CHUNK_PREAMBLE_SIZE 8
48 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
50 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
51 ( (long)(unsigned char)(ch3) | \
52 ( (long)(unsigned char)(ch2) << 8 ) | \
53 ( (long)(unsigned char)(ch1) << 16 ) | \
54 ( (long)(unsigned char)(ch0) << 24 ) )
56 #define FORM_TAG FOURCC_TAG('F', 'O', 'R', 'M')
57 #define WVQA_TAG FOURCC_TAG('W', 'V', 'Q', 'A')
58 #define VQHD_TAG FOURCC_TAG('V', 'Q', 'H', 'D')
59 #define FINF_TAG FOURCC_TAG('F', 'I', 'N', 'F')
60 #define SND0_TAG FOURCC_TAG('S', 'N', 'D', '0')
61 #define SND2_TAG FOURCC_TAG('S', 'N', 'D', '2')
62 #define VQFR_TAG FOURCC_TAG('V', 'Q', 'F', 'R')
64 #define VQA_HEADER_SIZE 0x2A
65 #define VQA_FRAMERATE 15
66 #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
67 #define VQA_PREAMBLE_SIZE 8
69 typedef struct WsAudDemuxContext
{
74 int audio_stream_index
;
75 int64_t audio_frame_counter
;
78 typedef struct WsVqaDemuxContext
{
83 int audio_stream_index
;
84 int video_stream_index
;
86 int64_t audio_frame_counter
;
90 static int wsaud_probe(AVProbeData
*p
)
94 /* Probabilistic content detection strategy: There is no file signature
95 * so perform sanity checks on various header parameters:
96 * 8000 <= sample rate (16 bits) <= 48000 ==> 40001 acceptable numbers
97 * compression type (8 bits) = 1 or 99 ==> 2 acceptable numbers
98 * There is a total of 24 bits. The number space contains 2^24 =
99 * 16777216 numbers. There are 40001 * 2 = 80002 acceptable combinations
100 * of numbers. There is a 80002/16777216 = 0.48% chance of a false
104 if (p
->buf_size
< AUD_HEADER_SIZE
)
107 /* check sample rate */
108 field
= LE_16(&p
->buf
[0]);
109 if ((field
< 8000) || (field
> 48000))
112 /* note: only check for WS IMA (type 99) right now since there is no
113 * support for type 1 */
114 if (p
->buf
[11] != 99)
117 /* return 1/2 certainty since this file check is a little sketchy */
118 return AVPROBE_SCORE_MAX
/ 2;
121 static int wsaud_read_header(AVFormatContext
*s
,
122 AVFormatParameters
*ap
)
124 WsAudDemuxContext
*wsaud
= (WsAudDemuxContext
*)s
->priv_data
;
125 ByteIOContext
*pb
= &s
->pb
;
127 unsigned char header
[AUD_HEADER_SIZE
];
129 if (get_buffer(pb
, header
, AUD_HEADER_SIZE
) != AUD_HEADER_SIZE
)
131 wsaud
->audio_samplerate
= LE_16(&header
[0]);
132 if (header
[11] == 99)
133 wsaud
->audio_type
= CODEC_ID_ADPCM_IMA_WS
;
135 return AVERROR_INVALIDDATA
;
137 /* flag 0 indicates stereo */
138 wsaud
->audio_channels
= (header
[10] & 0x1) + 1;
139 /* flag 1 indicates 16 bit audio */
140 wsaud
->audio_bits
= (((header
[10] & 0x2) >> 1) + 1) * 8;
142 /* set the pts reference the same as the sample rate */
144 s
->pts_den
= wsaud
->audio_samplerate
;
146 /* initialize the audio decoder stream */
147 st
= av_new_stream(s
, 0);
149 return AVERROR_NOMEM
;
150 st
->codec
.codec_type
= CODEC_TYPE_AUDIO
;
151 st
->codec
.codec_id
= wsaud
->audio_type
;
152 st
->codec
.codec_tag
= 0; /* no tag */
153 st
->codec
.channels
= wsaud
->audio_channels
;
154 st
->codec
.sample_rate
= wsaud
->audio_samplerate
;
155 st
->codec
.bits_per_sample
= wsaud
->audio_bits
;
156 st
->codec
.bit_rate
= st
->codec
.channels
* st
->codec
.sample_rate
*
157 st
->codec
.bits_per_sample
/ 4;
158 st
->codec
.block_align
= st
->codec
.channels
* st
->codec
.bits_per_sample
;
160 wsaud
->audio_stream_index
= st
->index
;
161 wsaud
->audio_frame_counter
= 0;
166 static int wsaud_read_packet(AVFormatContext
*s
,
169 WsAudDemuxContext
*wsaud
= (WsAudDemuxContext
*)s
->priv_data
;
170 ByteIOContext
*pb
= &s
->pb
;
171 unsigned char preamble
[AUD_CHUNK_PREAMBLE_SIZE
];
172 unsigned int chunk_size
;
175 if (get_buffer(pb
, preamble
, AUD_CHUNK_PREAMBLE_SIZE
) !=
176 AUD_CHUNK_PREAMBLE_SIZE
)
179 /* validate the chunk */
180 if (LE_32(&preamble
[4]) != AUD_CHUNK_SIGNATURE
)
181 return AVERROR_INVALIDDATA
;
183 chunk_size
= LE_16(&preamble
[0]);
184 if (av_new_packet(pkt
, chunk_size
))
186 pkt
->stream_index
= wsaud
->audio_stream_index
;
187 pkt
->pts
= wsaud
->audio_frame_counter
;
188 pkt
->pts
/= wsaud
->audio_samplerate
;
189 if ((ret
= get_buffer(pb
, pkt
->data
, chunk_size
)) != chunk_size
) {
194 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
195 wsaud
->audio_frame_counter
+= (chunk_size
* 2) / wsaud
->audio_channels
;
200 static int wsaud_read_close(AVFormatContext
*s
)
202 // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
208 static int wsvqa_probe(AVProbeData
*p
)
210 /* need 12 bytes to qualify */
211 if (p
->buf_size
< 12)
214 /* check for the VQA signatures */
215 if ((BE_32(&p
->buf
[0]) != FORM_TAG
) ||
216 (BE_32(&p
->buf
[8]) != WVQA_TAG
))
219 return AVPROBE_SCORE_MAX
;
222 static int wsvqa_read_header(AVFormatContext
*s
,
223 AVFormatParameters
*ap
)
225 WsVqaDemuxContext
*wsvqa
= (WsVqaDemuxContext
*)s
->priv_data
;
226 ByteIOContext
*pb
= &s
->pb
;
228 unsigned char *header
;
229 unsigned char scratch
[VQA_PREAMBLE_SIZE
];
231 /* set the pts reference (1 pts = 1/90000) */
235 /* initialize the video decoder stream */
236 st
= av_new_stream(s
, 0);
238 return AVERROR_NOMEM
;
239 wsvqa
->video_stream_index
= st
->index
;
240 st
->codec
.codec_type
= CODEC_TYPE_VIDEO
;
241 st
->codec
.codec_id
= CODEC_ID_WS_VQA
;
242 st
->codec
.codec_tag
= 0; /* no fourcc */
244 /* skip to the start of the VQA header */
245 url_fseek(pb
, 20, SEEK_SET
);
247 /* the VQA header needs to go to the decoder */
248 st
->codec
.extradata_size
= VQA_HEADER_SIZE
;
249 st
->codec
.extradata
= av_malloc(VQA_HEADER_SIZE
);
250 header
= (unsigned char *)st
->codec
.extradata
;
251 if (get_buffer(pb
, st
->codec
.extradata
, VQA_HEADER_SIZE
) !=
253 av_free(st
->codec
.extradata
);
256 st
->codec
.width
= LE_16(&header
[6]);
257 st
->codec
.height
= LE_16(&header
[8]);
259 /* initialize the audio decoder stream */
260 st
= av_new_stream(s
, 0);
262 return AVERROR_NOMEM
;
263 st
->codec
.codec_type
= CODEC_TYPE_AUDIO
;
264 st
->codec
.codec_id
= CODEC_ID_ADPCM_IMA_WS
;
265 st
->codec
.codec_tag
= 0; /* no tag */
266 st
->codec
.sample_rate
= LE_16(&header
[24]);
267 st
->codec
.channels
= header
[26];
268 st
->codec
.bits_per_sample
= 16;
269 st
->codec
.bit_rate
= st
->codec
.channels
* st
->codec
.sample_rate
*
270 st
->codec
.bits_per_sample
/ 4;
271 st
->codec
.block_align
= st
->codec
.channels
* st
->codec
.bits_per_sample
;
273 wsvqa
->audio_stream_index
= st
->index
;
274 wsvqa
->audio_samplerate
= st
->codec
.sample_rate
;
275 wsvqa
->audio_channels
= st
->codec
.channels
;
276 wsvqa
->audio_frame_counter
= 0;
278 /* skip the useless FINF chunk index */
279 if (get_buffer(pb
, scratch
, VQA_PREAMBLE_SIZE
) != VQA_PREAMBLE_SIZE
) {
280 av_free(st
->codec
.extradata
);
283 url_fseek(pb
, BE_32(&scratch
[4]), SEEK_CUR
);
284 wsvqa
->video_pts
= wsvqa
->audio_frame_counter
= 0;
289 static int wsvqa_read_packet(AVFormatContext
*s
,
292 WsVqaDemuxContext
*wsvqa
= (WsVqaDemuxContext
*)s
->priv_data
;
293 ByteIOContext
*pb
= &s
->pb
;
295 unsigned char preamble
[VQA_PREAMBLE_SIZE
];
296 unsigned int chunk_type
;
297 unsigned int chunk_size
;
300 if (get_buffer(pb
, preamble
, VQA_PREAMBLE_SIZE
) != VQA_PREAMBLE_SIZE
)
303 chunk_type
= BE_32(&preamble
[0]);
304 chunk_size
= BE_32(&preamble
[4]);
305 skip_byte
= chunk_size
& 0x01;
307 if ((chunk_type
== SND2_TAG
) || (chunk_type
== VQFR_TAG
)) {
309 if (av_new_packet(pkt
, chunk_size
))
311 ret
= get_buffer(pb
, pkt
->data
, chunk_size
);
312 if (ret
!= chunk_size
) {
317 if (chunk_type
== SND2_TAG
) {
318 pkt
->stream_index
= wsvqa
->audio_stream_index
;
321 pkt
->pts
*= wsvqa
->audio_frame_counter
;
322 pkt
->pts
/= wsvqa
->audio_samplerate
;
324 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
325 wsvqa
->audio_frame_counter
+= (chunk_size
* 2) /
326 wsvqa
->audio_channels
;
328 pkt
->stream_index
= wsvqa
->video_stream_index
;
329 pkt
->pts
= wsvqa
->video_pts
;
330 wsvqa
->video_pts
+= VQA_VIDEO_PTS_INC
;
334 return AVERROR_INVALIDDATA
;
336 /* stay on 16-bit alignment */
338 url_fseek(pb
, 1, SEEK_CUR
);
343 static int wsvqa_read_close(AVFormatContext
*s
)
345 // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
350 static AVInputFormat wsaud_iformat
= {
352 "Westwood Studios audio format",
353 sizeof(WsAudDemuxContext
),
360 static AVInputFormat wsvqa_iformat
= {
362 "Westwood Studios VQA format",
363 sizeof(WsVqaDemuxContext
),
370 int westwood_init(void)
372 av_register_input_format(&wsaud_iformat
);
373 av_register_input_format(&wsvqa_iformat
);