2 * Westwood Studios VQA Format Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Westwood Studios VQA file demuxer
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
31 #include "libavutil/intreadwrite.h"
35 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
36 #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
37 #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
38 #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
39 #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
40 #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
41 #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
42 #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
44 /* don't know what these tags are for, but acknowledge their existence */
45 #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
46 #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
47 #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
48 #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
49 #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
50 #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
51 #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
53 #define VQA_HEADER_SIZE 0x2A
54 #define VQA_PREAMBLE_SIZE 8
56 typedef struct WsVqaDemuxContext {
58 int audio_stream_index;
59 int video_stream_index;
62 static int wsvqa_probe(AVProbeData *p)
64 /* need 12 bytes to qualify */
68 /* check for the VQA signatures */
69 if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
70 (AV_RB32(&p->buf[8]) != WVQA_TAG))
73 return AVPROBE_SCORE_MAX;
76 static int wsvqa_read_header(AVFormatContext *s)
78 WsVqaDemuxContext *wsvqa = s->priv_data;
79 AVIOContext *pb = s->pb;
81 unsigned char *header;
82 unsigned char scratch[VQA_PREAMBLE_SIZE];
83 unsigned int chunk_tag;
84 unsigned int chunk_size;
85 int fps, version, flags, sample_rate, channels;
87 /* initialize the video decoder stream */
88 st = avformat_new_stream(s, NULL);
90 return AVERROR(ENOMEM);
92 wsvqa->video_stream_index = st->index;
93 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
94 st->codec->codec_id = CODEC_ID_WS_VQA;
95 st->codec->codec_tag = 0; /* no fourcc */
97 /* skip to the start of the VQA header */
98 avio_seek(pb, 20, SEEK_SET);
100 /* the VQA header needs to go to the decoder */
101 st->codec->extradata_size = VQA_HEADER_SIZE;
102 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
103 header = (unsigned char *)st->codec->extradata;
104 if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
106 av_free(st->codec->extradata);
109 st->codec->width = AV_RL16(&header[6]);
110 st->codec->height = AV_RL16(&header[8]);
112 if (fps < 1 || fps > 30) {
113 av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
114 return AVERROR_INVALIDDATA;
116 avpriv_set_pts_info(st, 64, 1, fps);
118 /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
119 version = AV_RL16(&header[ 0]);
120 flags = AV_RL16(&header[ 2]);
121 sample_rate = AV_RL16(&header[24]);
122 channels = header[26];
123 if (sample_rate || (version == 1 && flags == 1)) {
124 st = avformat_new_stream(s, NULL);
126 return AVERROR(ENOMEM);
128 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
130 st->codec->extradata_size = VQA_HEADER_SIZE;
131 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
132 if (!st->codec->extradata)
133 return AVERROR(ENOMEM);
134 memcpy(st->codec->extradata, header, VQA_HEADER_SIZE);
138 st->codec->sample_rate = sample_rate;
139 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
143 st->codec->channels = channels;
147 st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
151 st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
152 st->codec->bits_per_coded_sample = 4;
153 st->codec->bit_rate = channels * sample_rate * 4;
156 /* NOTE: version 0 is supposedly raw pcm_u8 or pcm_s16le, but we do
157 not have any samples to validate this */
158 av_log_ask_for_sample(s, "VQA version %d audio\n", version);
159 return AVERROR_PATCHWELCOME;
162 wsvqa->audio_stream_index = st->index;
163 wsvqa->audio_channels = st->codec->channels;
166 /* there are 0 or more chunks before the FINF chunk; iterate until
167 * FINF has been skipped and the file will be ready to be demuxed */
169 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
171 chunk_tag = AV_RB32(&scratch[0]);
172 chunk_size = AV_RB32(&scratch[4]);
174 /* catch any unknown header tags, for curiousity */
187 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
188 scratch[0], scratch[1],
189 scratch[2], scratch[3]);
193 avio_skip(pb, chunk_size);
194 } while (chunk_tag != FINF_TAG);
199 static int wsvqa_read_packet(AVFormatContext *s,
202 WsVqaDemuxContext *wsvqa = s->priv_data;
203 AVIOContext *pb = s->pb;
205 unsigned char preamble[VQA_PREAMBLE_SIZE];
206 unsigned int chunk_type;
207 unsigned int chunk_size;
210 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
211 chunk_type = AV_RB32(&preamble[0]);
212 chunk_size = AV_RB32(&preamble[4]);
214 skip_byte = chunk_size & 0x01;
216 if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
217 av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
218 return AVERROR_INVALIDDATA;
221 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
223 ret= av_get_packet(pb, pkt, chunk_size);
227 if (chunk_type == SND2_TAG) {
228 pkt->stream_index = wsvqa->audio_stream_index;
229 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
230 pkt->duration = (chunk_size * 2) / wsvqa->audio_channels;
231 } else if(chunk_type == SND1_TAG) {
232 pkt->stream_index = wsvqa->audio_stream_index;
233 /* unpacked size is stored in header */
234 pkt->duration = AV_RL16(pkt->data) / wsvqa->audio_channels;
236 pkt->stream_index = wsvqa->video_stream_index;
239 /* stay on 16-bit alignment */
250 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
252 avio_skip(pb, chunk_size + skip_byte);
259 AVInputFormat ff_wsvqa_demuxer = {
261 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
262 .priv_data_size = sizeof(WsVqaDemuxContext),
263 .read_probe = wsvqa_probe,
264 .read_header = wsvqa_read_header,
265 .read_packet = wsvqa_read_packet,