comparison westwood.c @ 266:8bb470d85249 libavformat

New demuxers: Sega FILM/CPK, Westwood VQA & AUD; new decoders: MS RLE & Video-1, Apple RPZA, Cinepak, Westwood IMA ADPCM
author tmmm
date Wed, 01 Oct 2003 04:39:38 +0000
parents
children 377bd276adaa
comparison
equal deleted inserted replaced
265:786e8286ea4a 266:8bb470d85249
1 /*
2 * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
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 westwood.c
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
27 *
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.
32 */
33
34 #include "avformat.h"
35
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) | \
40 ((uint8_t*)(x))[0])
41 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
42 (((uint8_t*)(x))[1] << 16) | \
43 (((uint8_t*)(x))[2] << 8) | \
44 ((uint8_t*)(x))[3])
45
46 #define AUD_HEADER_SIZE 12
47 #define AUD_CHUNK_PREAMBLE_SIZE 8
48 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
49
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 ) )
55
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')
63
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
68
69 typedef struct WsAudDemuxContext {
70 int audio_samplerate;
71 int audio_channels;
72 int audio_bits;
73 int audio_type;
74 int audio_stream_index;
75 int64_t audio_frame_counter;
76 } WsAudDemuxContext;
77
78 typedef struct WsVqaDemuxContext {
79 int audio_samplerate;
80 int audio_channels;
81 int audio_bits;
82
83 int audio_stream_index;
84 int video_stream_index;
85
86 int64_t audio_frame_counter;
87 int64_t video_pts;
88 } WsVqaDemuxContext;
89
90 static int wsaud_probe(AVProbeData *p)
91 {
92 int field;
93
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
101 * positive.
102 */
103
104 if (p->buf_size < AUD_HEADER_SIZE)
105 return 0;
106
107 /* check sample rate */
108 field = LE_16(&p->buf[0]);
109 if ((field < 8000) || (field > 48000))
110 return 0;
111
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)
115 return 0;
116
117 /* return 1/2 certainty since this file check is a little sketchy */
118 return AVPROBE_SCORE_MAX / 2;
119 }
120
121 static int wsaud_read_header(AVFormatContext *s,
122 AVFormatParameters *ap)
123 {
124 WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
125 ByteIOContext *pb = &s->pb;
126 AVStream *st;
127 unsigned char header[AUD_HEADER_SIZE];
128
129 if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
130 return -EIO;
131 wsaud->audio_samplerate = LE_16(&header[0]);
132 if (header[11] == 99)
133 wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
134 else
135 return AVERROR_INVALIDDATA;
136
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;
141
142 /* set the pts reference the same as the sample rate */
143 s->pts_num = 1;
144 s->pts_den = wsaud->audio_samplerate;
145
146 /* initialize the audio decoder stream */
147 st = av_new_stream(s, 0);
148 if (!st)
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;
159
160 wsaud->audio_stream_index = st->index;
161 wsaud->audio_frame_counter = 0;
162
163 return 0;
164 }
165
166 static int wsaud_read_packet(AVFormatContext *s,
167 AVPacket *pkt)
168 {
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;
173 int ret = 0;
174
175 if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
176 AUD_CHUNK_PREAMBLE_SIZE)
177 return -EIO;
178
179 /* validate the chunk */
180 if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
181 return AVERROR_INVALIDDATA;
182
183 chunk_size = LE_16(&preamble[0]);
184 if (av_new_packet(pkt, chunk_size))
185 return -EIO;
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) {
190 av_free_packet(pkt);
191 ret = -EIO;
192 }
193
194 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
195 wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
196
197 return ret;
198 }
199
200 static int wsaud_read_close(AVFormatContext *s)
201 {
202 // WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
203
204 return 0;
205 }
206
207
208 static int wsvqa_probe(AVProbeData *p)
209 {
210 /* need 12 bytes to qualify */
211 if (p->buf_size < 12)
212 return 0;
213
214 /* check for the VQA signatures */
215 if ((BE_32(&p->buf[0]) != FORM_TAG) ||
216 (BE_32(&p->buf[8]) != WVQA_TAG))
217 return 0;
218
219 return AVPROBE_SCORE_MAX;
220 }
221
222 static int wsvqa_read_header(AVFormatContext *s,
223 AVFormatParameters *ap)
224 {
225 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
226 ByteIOContext *pb = &s->pb;
227 AVStream *st;
228 unsigned char *header;
229 unsigned char scratch[VQA_PREAMBLE_SIZE];
230
231 /* set the pts reference (1 pts = 1/90000) */
232 s->pts_num = 1;
233 s->pts_den = 90000;
234
235 /* initialize the video decoder stream */
236 st = av_new_stream(s, 0);
237 if (!st)
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 */
243
244 /* skip to the start of the VQA header */
245 url_fseek(pb, 20, SEEK_SET);
246
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) !=
252 VQA_HEADER_SIZE) {
253 av_free(st->codec.extradata);
254 return -EIO;
255 }
256 st->codec.width = LE_16(&header[6]);
257 st->codec.height = LE_16(&header[8]);
258
259 /* initialize the audio decoder stream */
260 st = av_new_stream(s, 0);
261 if (!st)
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;
272
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;
277
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);
281 return -EIO;
282 }
283 url_fseek(pb, BE_32(&scratch[4]), SEEK_CUR);
284 wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
285
286 return 0;
287 }
288
289 static int wsvqa_read_packet(AVFormatContext *s,
290 AVPacket *pkt)
291 {
292 WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
293 ByteIOContext *pb = &s->pb;
294 int ret = 0;
295 unsigned char preamble[VQA_PREAMBLE_SIZE];
296 unsigned int chunk_type;
297 unsigned int chunk_size;
298 int skip_byte;
299
300 if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
301 return -EIO;
302
303 chunk_type = BE_32(&preamble[0]);
304 chunk_size = BE_32(&preamble[4]);
305 skip_byte = chunk_size & 0x01;
306
307 if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
308
309 if (av_new_packet(pkt, chunk_size))
310 return -EIO;
311 ret = get_buffer(pb, pkt->data, chunk_size);
312 if (ret != chunk_size) {
313 av_free_packet(pkt);
314 ret = -EIO;
315 }
316
317 if (chunk_type == SND2_TAG) {
318 pkt->stream_index = wsvqa->audio_stream_index;
319
320 pkt->pts = 90000;
321 pkt->pts *= wsvqa->audio_frame_counter;
322 pkt->pts /= wsvqa->audio_samplerate;
323
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;
327 } else {
328 pkt->stream_index = wsvqa->video_stream_index;
329 pkt->pts = wsvqa->video_pts;
330 wsvqa->video_pts += VQA_VIDEO_PTS_INC;
331 }
332
333 } else
334 return AVERROR_INVALIDDATA;
335
336 /* stay on 16-bit alignment */
337 if (skip_byte)
338 url_fseek(pb, 1, SEEK_CUR);
339
340 return ret;
341 }
342
343 static int wsvqa_read_close(AVFormatContext *s)
344 {
345 // WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
346
347 return 0;
348 }
349
350 static AVInputFormat wsaud_iformat = {
351 "wsaud",
352 "Westwood Studios audio format",
353 sizeof(WsAudDemuxContext),
354 wsaud_probe,
355 wsaud_read_header,
356 wsaud_read_packet,
357 wsaud_read_close,
358 };
359
360 static AVInputFormat wsvqa_iformat = {
361 "wsvqa",
362 "Westwood Studios VQA format",
363 sizeof(WsVqaDemuxContext),
364 wsvqa_probe,
365 wsvqa_read_header,
366 wsvqa_read_packet,
367 wsvqa_read_close,
368 };
369
370 int westwood_init(void)
371 {
372 av_register_input_format(&wsaud_iformat);
373 av_register_input_format(&wsvqa_iformat);
374 return 0;
375 }