vqa: remove unused context fields, audio_samplerate and audio_bits
[libav.git] / libavformat / westwood_vqa.c
CommitLineData
2fdf638b 1/*
e924a4ba 2 * Westwood Studios VQA Format Demuxer
2fdf638b
MM
3 * Copyright (c) 2003 The ffmpeg Project
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
2fdf638b
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
2fdf638b 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
2fdf638b
MM
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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2fdf638b
MM
20 */
21
22/**
ba87f080 23 * @file
e924a4ba 24 * Westwood Studios VQA file demuxer
2fdf638b
MM
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
2fdf638b
MM
29 */
30
6a5d31ac 31#include "libavutil/intreadwrite.h"
2fdf638b 32#include "avformat.h"
c3f9ebf7 33#include "internal.h"
2fdf638b 34
3a278992
MM
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')
61710f7b 40#define SND1_TAG MKBETAG('S', 'N', 'D', '1')
3a278992
MM
41#define SND2_TAG MKBETAG('S', 'N', 'D', '2')
42#define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
2fdf638b 43
20f269dc 44/* don't know what these tags are for, but acknowledge their existence */
3a278992
MM
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')
61710f7b 51#define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
20f269dc 52
2fdf638b 53#define VQA_HEADER_SIZE 0x2A
2fdf638b
MM
54#define VQA_PREAMBLE_SIZE 8
55
2fdf638b 56typedef struct WsVqaDemuxContext {
2fdf638b 57 int audio_channels;
2fdf638b
MM
58 int audio_stream_index;
59 int video_stream_index;
2fdf638b
MM
60} WsVqaDemuxContext;
61
2fdf638b
MM
62static int wsvqa_probe(AVProbeData *p)
63{
64 /* need 12 bytes to qualify */
65 if (p->buf_size < 12)
66 return 0;
67
68 /* check for the VQA signatures */
fead30d4
AB
69 if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
70 (AV_RB32(&p->buf[8]) != WVQA_TAG))
2fdf638b
MM
71 return 0;
72
73 return AVPROBE_SCORE_MAX;
74}
75
76static int wsvqa_read_header(AVFormatContext *s,
77 AVFormatParameters *ap)
78{
e4141433 79 WsVqaDemuxContext *wsvqa = s->priv_data;
ae628ec1 80 AVIOContext *pb = s->pb;
2fdf638b
MM
81 AVStream *st;
82 unsigned char *header;
83 unsigned char scratch[VQA_PREAMBLE_SIZE];
20f269dc
MM
84 unsigned int chunk_tag;
85 unsigned int chunk_size;
7e813d59 86 int fps, version, flags, sample_rate, channels;
2fdf638b 87
2fdf638b 88 /* initialize the video decoder stream */
3b3bbdd3 89 st = avformat_new_stream(s, NULL);
2fdf638b 90 if (!st)
769e10f0 91 return AVERROR(ENOMEM);
e1ac69fa 92 st->start_time = 0;
2fdf638b 93 wsvqa->video_stream_index = st->index;
72415b2a 94 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c
MN
95 st->codec->codec_id = CODEC_ID_WS_VQA;
96 st->codec->codec_tag = 0; /* no fourcc */
2fdf638b
MM
97
98 /* skip to the start of the VQA header */
6b4aa5da 99 avio_seek(pb, 20, SEEK_SET);
2fdf638b
MM
100
101 /* the VQA header needs to go to the decoder */
01f4895c 102 st->codec->extradata_size = VQA_HEADER_SIZE;
d76319b1 103 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
01f4895c 104 header = (unsigned char *)st->codec->extradata;
b7effd4e 105 if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
2fdf638b 106 VQA_HEADER_SIZE) {
01f4895c 107 av_free(st->codec->extradata);
6f3e0b21 108 return AVERROR(EIO);
2fdf638b 109 }
fead30d4
AB
110 st->codec->width = AV_RL16(&header[6]);
111 st->codec->height = AV_RL16(&header[8]);
5b4c5628
JR
112 fps = header[12];
113 if (fps < 1 || fps > 30) {
114 av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
115 return AVERROR_INVALIDDATA;
116 }
117 avpriv_set_pts_info(st, 64, 1, fps);
2fdf638b 118
61710f7b 119 /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
7e813d59
JR
120 version = AV_RL16(&header[ 0]);
121 flags = AV_RL16(&header[ 2]);
122 sample_rate = AV_RL16(&header[24]);
123 channels = header[26];
124 if (sample_rate || (version == 1 && flags == 1)) {
3b3bbdd3 125 st = avformat_new_stream(s, NULL);
20f269dc 126 if (!st)
769e10f0 127 return AVERROR(ENOMEM);
e1ac69fa 128 st->start_time = 0;
72415b2a 129 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
20f269dc 130
7e813d59
JR
131 if (!sample_rate)
132 sample_rate = 22050;
133 st->codec->sample_rate = sample_rate;
53ed79a2
JR
134 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
135
7e813d59
JR
136 if (!channels)
137 channels = 1;
138 st->codec->channels = channels;
139
140 switch (version) {
141 case 1:
142 st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
143 break;
144 case 2:
145 case 3:
146 st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
147 st->codec->bits_per_coded_sample = 4;
148 st->codec->bit_rate = channels * sample_rate * 4;
149 break;
150 default:
151 /* NOTE: version 0 is supposedly raw pcm_u8 or pcm_s16le, but we do
152 not have any samples to validate this */
153 av_log_ask_for_sample(s, "VQA version %d audio\n", version);
154 return AVERROR_PATCHWELCOME;
155 }
156
20f269dc 157 wsvqa->audio_stream_index = st->index;
01f4895c 158 wsvqa->audio_channels = st->codec->channels;
20f269dc 159 }
2fdf638b 160
20f269dc
MM
161 /* there are 0 or more chunks before the FINF chunk; iterate until
162 * FINF has been skipped and the file will be ready to be demuxed */
163 do {
b7effd4e 164 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
01f4895c 165 av_free(st->codec->extradata);
6f3e0b21 166 return AVERROR(EIO);
20f269dc 167 }
fead30d4
AB
168 chunk_tag = AV_RB32(&scratch[0]);
169 chunk_size = AV_RB32(&scratch[4]);
20f269dc
MM
170
171 /* catch any unknown header tags, for curiousity */
172 switch (chunk_tag) {
173 case CINF_TAG:
174 case CINH_TAG:
175 case CIND_TAG:
176 case PINF_TAG:
177 case PINH_TAG:
178 case PIND_TAG:
179 case FINF_TAG:
61710f7b 180 case CMDS_TAG:
20f269dc
MM
181 break;
182
183 default:
bc874dae 184 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
20f269dc
MM
185 scratch[0], scratch[1],
186 scratch[2], scratch[3]);
187 break;
188 }
189
45a8a02a 190 avio_skip(pb, chunk_size);
20f269dc 191 } while (chunk_tag != FINF_TAG);
2fdf638b 192
2fdf638b
MM
193 return 0;
194}
195
196static int wsvqa_read_packet(AVFormatContext *s,
197 AVPacket *pkt)
198{
e4141433 199 WsVqaDemuxContext *wsvqa = s->priv_data;
ae628ec1 200 AVIOContext *pb = s->pb;
61710f7b 201 int ret = -1;
2fdf638b
MM
202 unsigned char preamble[VQA_PREAMBLE_SIZE];
203 unsigned int chunk_type;
204 unsigned int chunk_size;
205 int skip_byte;
206
b7effd4e 207 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
fead30d4
AB
208 chunk_type = AV_RB32(&preamble[0]);
209 chunk_size = AV_RB32(&preamble[4]);
61710f7b
KS
210 skip_byte = chunk_size & 0x01;
211
566ee0ea
SG
212 if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
213 av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
214 return AVERROR_INVALIDDATA;
215 }
216
61710f7b
KS
217 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
218
219 if (av_new_packet(pkt, chunk_size))
6f3e0b21 220 return AVERROR(EIO);
b7effd4e 221 ret = avio_read(pb, pkt->data, chunk_size);
61710f7b
KS
222 if (ret != chunk_size) {
223 av_free_packet(pkt);
6f3e0b21 224 return AVERROR(EIO);
61710f7b
KS
225 }
226
227 if (chunk_type == SND2_TAG) {
228 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b 229 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
9ae84697 230 pkt->duration = (chunk_size * 2) / wsvqa->audio_channels;
61710f7b
KS
231 } else if(chunk_type == SND1_TAG) {
232 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b 233 /* unpacked size is stored in header */
9ae84697 234 pkt->duration = AV_RL16(pkt->data) / wsvqa->audio_channels;
61710f7b
KS
235 } else {
236 pkt->stream_index = wsvqa->video_stream_index;
9ae84697 237 pkt->duration = 1;
61710f7b
KS
238 }
239 /* stay on 16-bit alignment */
240 if (skip_byte)
45a8a02a 241 avio_skip(pb, 1);
61710f7b
KS
242
243 return ret;
2fdf638b 244 } else {
61710f7b
KS
245 switch(chunk_type){
246 case CMDS_TAG:
247 case SND0_TAG:
248 break;
249 default:
250 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
251 }
45a8a02a 252 avio_skip(pb, chunk_size + skip_byte);
2fdf638b 253 }
61710f7b 254 }
2fdf638b
MM
255
256 return ret;
257}
258
c6610a21 259AVInputFormat ff_wsvqa_demuxer = {
dfc2c4d9
AK
260 .name = "wsvqa",
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,
2fdf638b 266};