]> git.sesse.net Git - ffmpeg/blob - libavformat/westwood.c
nut (de)muxer update
[ffmpeg] / libavformat / westwood.c
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 AUD_HEADER_SIZE 12
37 #define AUD_CHUNK_PREAMBLE_SIZE 8
38 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
39
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')
47
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')
55
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
60
61 typedef struct WsAudDemuxContext {
62     int audio_samplerate;
63     int audio_channels;
64     int audio_bits;
65     int audio_type;
66     int audio_stream_index;
67     int64_t audio_frame_counter;
68 } WsAudDemuxContext;
69
70 typedef struct WsVqaDemuxContext {
71     int audio_samplerate;
72     int audio_channels;
73     int audio_bits;
74
75     int audio_stream_index;
76     int video_stream_index;
77
78     int64_t audio_frame_counter;
79     int64_t video_pts;
80 } WsVqaDemuxContext;
81
82 static int wsaud_probe(AVProbeData *p)
83 {
84     int field;
85
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
93      * positive.
94      */
95
96     if (p->buf_size < AUD_HEADER_SIZE)
97         return 0;
98
99     /* check sample rate */
100     field = LE_16(&p->buf[0]);
101     if ((field < 8000) || (field > 48000))
102         return 0;
103
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)
107         return 0;
108
109     /* return 1/2 certainty since this file check is a little sketchy */
110     return AVPROBE_SCORE_MAX / 2;
111 }
112
113 static int wsaud_read_header(AVFormatContext *s,
114                              AVFormatParameters *ap)
115 {
116     WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
117     ByteIOContext *pb = &s->pb;
118     AVStream *st;
119     unsigned char header[AUD_HEADER_SIZE];
120
121     if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
122         return -EIO;
123     wsaud->audio_samplerate = LE_16(&header[0]);
124     if (header[11] == 99)
125         wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
126     else
127         return AVERROR_INVALIDDATA;
128
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;
133
134     /* set the pts reference the same as the sample rate */
135     s->pts_num = 1;
136     s->pts_den = wsaud->audio_samplerate;
137
138     /* initialize the audio decoder stream */
139     st = av_new_stream(s, 0);
140     if (!st)
141         return AVERROR_NOMEM;
142     st->codec.codec_type = CODEC_TYPE_AUDIO;
143     st->codec.codec_id = wsaud->audio_type;
144     st->codec.codec_tag = 0;  /* no tag */
145     st->codec.channels = wsaud->audio_channels;
146     st->codec.sample_rate = wsaud->audio_samplerate;
147     st->codec.bits_per_sample = wsaud->audio_bits;
148     st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
149         st->codec.bits_per_sample / 4;
150     st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
151
152     wsaud->audio_stream_index = st->index;
153     wsaud->audio_frame_counter = 0;
154
155     return 0;
156 }
157
158 static int wsaud_read_packet(AVFormatContext *s,
159                              AVPacket *pkt)
160 {
161     WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
162     ByteIOContext *pb = &s->pb;
163     unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
164     unsigned int chunk_size;
165     int ret = 0;
166
167     if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
168         AUD_CHUNK_PREAMBLE_SIZE)
169         return -EIO;
170
171     /* validate the chunk */
172     if (LE_32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
173         return AVERROR_INVALIDDATA;
174
175     chunk_size = LE_16(&preamble[0]);
176     if (av_new_packet(pkt, chunk_size))
177         return -EIO;
178     pkt->stream_index = wsaud->audio_stream_index;
179     pkt->pts = wsaud->audio_frame_counter;
180     pkt->pts /= wsaud->audio_samplerate;
181     if ((ret = get_buffer(pb, pkt->data, chunk_size)) != chunk_size) {
182         av_free_packet(pkt);
183         ret = -EIO;
184     }
185
186     /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
187     wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
188
189     return ret;
190 }
191
192 static int wsaud_read_close(AVFormatContext *s)
193 {
194 //    WsAudDemuxContext *wsaud = (WsAudDemuxContext *)s->priv_data;
195
196     return 0;
197 }
198
199
200 static int wsvqa_probe(AVProbeData *p)
201 {
202     /* need 12 bytes to qualify */
203     if (p->buf_size < 12)
204         return 0;
205
206     /* check for the VQA signatures */
207     if ((BE_32(&p->buf[0]) != FORM_TAG) ||
208         (BE_32(&p->buf[8]) != WVQA_TAG))
209         return 0;
210
211     return AVPROBE_SCORE_MAX;
212 }
213
214 static int wsvqa_read_header(AVFormatContext *s,
215                              AVFormatParameters *ap)
216 {
217     WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
218     ByteIOContext *pb = &s->pb;
219     AVStream *st;
220     unsigned char *header;
221     unsigned char scratch[VQA_PREAMBLE_SIZE];
222     unsigned int chunk_tag;
223     unsigned int chunk_size;
224
225     /* set the pts reference (1 pts = 1/90000) */
226     s->pts_num = 1;
227     s->pts_den = 90000;
228
229     /* initialize the video decoder stream */
230     st = av_new_stream(s, 0);
231     if (!st)
232         return AVERROR_NOMEM;
233     wsvqa->video_stream_index = st->index;
234     st->codec.codec_type = CODEC_TYPE_VIDEO;
235     st->codec.codec_id = CODEC_ID_WS_VQA;
236     st->codec.codec_tag = 0;  /* no fourcc */
237
238     /* skip to the start of the VQA header */
239     url_fseek(pb, 20, SEEK_SET);
240
241     /* the VQA header needs to go to the decoder */
242     st->codec.extradata_size = VQA_HEADER_SIZE;
243     st->codec.extradata = av_malloc(VQA_HEADER_SIZE);
244     header = (unsigned char *)st->codec.extradata;
245     if (get_buffer(pb, st->codec.extradata, VQA_HEADER_SIZE) !=
246         VQA_HEADER_SIZE) {
247         av_free(st->codec.extradata);
248         return -EIO;
249     }
250     st->codec.width = LE_16(&header[6]);
251     st->codec.height = LE_16(&header[8]);
252
253     /* initialize the audio decoder stream is sample rate is non-zero */
254     if (LE_16(&header[24])) {
255         st = av_new_stream(s, 0);
256         if (!st)
257             return AVERROR_NOMEM;
258         st->codec.codec_type = CODEC_TYPE_AUDIO;
259         st->codec.codec_id = CODEC_ID_ADPCM_IMA_WS;
260         st->codec.codec_tag = 0;  /* no tag */
261         st->codec.sample_rate = LE_16(&header[24]);
262         st->codec.channels = header[26];
263         st->codec.bits_per_sample = 16;
264         st->codec.bit_rate = st->codec.channels * st->codec.sample_rate *
265             st->codec.bits_per_sample / 4;
266         st->codec.block_align = st->codec.channels * st->codec.bits_per_sample;
267
268         wsvqa->audio_stream_index = st->index;
269         wsvqa->audio_samplerate = st->codec.sample_rate;
270         wsvqa->audio_channels = st->codec.channels;
271         wsvqa->audio_frame_counter = 0;
272     }
273
274     /* there are 0 or more chunks before the FINF chunk; iterate until
275      * FINF has been skipped and the file will be ready to be demuxed */
276     do {
277         if (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
278             av_free(st->codec.extradata);
279             return -EIO;
280         }
281         chunk_tag = BE_32(&scratch[0]);
282         chunk_size = BE_32(&scratch[4]);
283
284         /* catch any unknown header tags, for curiousity */
285         switch (chunk_tag) {
286         case CINF_TAG:
287         case CINH_TAG:
288         case CIND_TAG:
289         case PINF_TAG:
290         case PINH_TAG:
291         case PIND_TAG:
292         case FINF_TAG:
293             break;
294
295         default:
296             av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
297                 scratch[0], scratch[1],
298                 scratch[2], scratch[3]);
299             break;
300         }
301
302         url_fseek(pb, chunk_size, SEEK_CUR);
303     } while (chunk_tag != FINF_TAG);
304
305     wsvqa->video_pts = wsvqa->audio_frame_counter = 0;
306
307     return 0;
308 }
309
310 static int wsvqa_read_packet(AVFormatContext *s,
311                              AVPacket *pkt)
312 {
313     WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
314     ByteIOContext *pb = &s->pb;
315     int ret = 0;
316     unsigned char preamble[VQA_PREAMBLE_SIZE];
317     unsigned int chunk_type;
318     unsigned int chunk_size;
319     int skip_byte;
320
321     if (get_buffer(pb, preamble, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
322         return -EIO;
323
324     chunk_type = BE_32(&preamble[0]);
325     chunk_size = BE_32(&preamble[4]);
326     skip_byte = chunk_size & 0x01;
327
328     if ((chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
329
330         if (av_new_packet(pkt, chunk_size))
331             return -EIO;
332         ret = get_buffer(pb, pkt->data, chunk_size);
333         if (ret != chunk_size) {
334             av_free_packet(pkt);
335             ret = -EIO;
336         }
337
338         if (chunk_type == SND2_TAG) {
339             pkt->stream_index = wsvqa->audio_stream_index;
340
341             pkt->pts = 90000;
342             pkt->pts *= wsvqa->audio_frame_counter;
343             pkt->pts /= wsvqa->audio_samplerate;
344
345             /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
346             wsvqa->audio_frame_counter += (chunk_size * 2) / 
347                 wsvqa->audio_channels;
348         } else {
349             pkt->stream_index = wsvqa->video_stream_index;
350             pkt->pts = wsvqa->video_pts;
351             wsvqa->video_pts += VQA_VIDEO_PTS_INC;
352         }
353
354     } else
355         return AVERROR_INVALIDDATA;
356
357     /* stay on 16-bit alignment */
358     if (skip_byte)
359         url_fseek(pb, 1, SEEK_CUR);
360
361     return ret;
362 }
363
364 static int wsvqa_read_close(AVFormatContext *s)
365 {
366 //    WsVqaDemuxContext *wsvqa = (WsVqaDemuxContext *)s->priv_data;
367
368     return 0;
369 }
370
371 static AVInputFormat wsaud_iformat = {
372     "wsaud",
373     "Westwood Studios audio format",
374     sizeof(WsAudDemuxContext),
375     wsaud_probe,
376     wsaud_read_header,
377     wsaud_read_packet,
378     wsaud_read_close,
379 };
380
381 static AVInputFormat wsvqa_iformat = {
382     "wsvqa",
383     "Westwood Studios VQA format",
384     sizeof(WsVqaDemuxContext),
385     wsvqa_probe,
386     wsvqa_read_header,
387     wsvqa_read_packet,
388     wsvqa_read_close,
389 };
390
391 int westwood_init(void)
392 {
393     av_register_input_format(&wsaud_iformat);
394     av_register_input_format(&wsvqa_iformat);
395     return 0;
396 }