vqa: set time base to frame rate as coded in the header.
[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
MM
56typedef struct WsVqaDemuxContext {
57 int audio_samplerate;
58 int audio_channels;
59 int audio_bits;
60
61 int audio_stream_index;
62 int video_stream_index;
2fdf638b
MM
63} WsVqaDemuxContext;
64
2fdf638b
MM
65static int wsvqa_probe(AVProbeData *p)
66{
67 /* need 12 bytes to qualify */
68 if (p->buf_size < 12)
69 return 0;
70
71 /* check for the VQA signatures */
fead30d4
AB
72 if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
73 (AV_RB32(&p->buf[8]) != WVQA_TAG))
2fdf638b
MM
74 return 0;
75
76 return AVPROBE_SCORE_MAX;
77}
78
79static int wsvqa_read_header(AVFormatContext *s,
80 AVFormatParameters *ap)
81{
e4141433 82 WsVqaDemuxContext *wsvqa = s->priv_data;
ae628ec1 83 AVIOContext *pb = s->pb;
2fdf638b
MM
84 AVStream *st;
85 unsigned char *header;
86 unsigned char scratch[VQA_PREAMBLE_SIZE];
20f269dc
MM
87 unsigned int chunk_tag;
88 unsigned int chunk_size;
5b4c5628 89 int fps;
2fdf638b 90
2fdf638b 91 /* initialize the video decoder stream */
3b3bbdd3 92 st = avformat_new_stream(s, NULL);
2fdf638b 93 if (!st)
769e10f0 94 return AVERROR(ENOMEM);
e1ac69fa 95 st->start_time = 0;
2fdf638b 96 wsvqa->video_stream_index = st->index;
72415b2a 97 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
01f4895c
MN
98 st->codec->codec_id = CODEC_ID_WS_VQA;
99 st->codec->codec_tag = 0; /* no fourcc */
2fdf638b
MM
100
101 /* skip to the start of the VQA header */
6b4aa5da 102 avio_seek(pb, 20, SEEK_SET);
2fdf638b
MM
103
104 /* the VQA header needs to go to the decoder */
01f4895c 105 st->codec->extradata_size = VQA_HEADER_SIZE;
d76319b1 106 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
01f4895c 107 header = (unsigned char *)st->codec->extradata;
b7effd4e 108 if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
2fdf638b 109 VQA_HEADER_SIZE) {
01f4895c 110 av_free(st->codec->extradata);
6f3e0b21 111 return AVERROR(EIO);
2fdf638b 112 }
fead30d4
AB
113 st->codec->width = AV_RL16(&header[6]);
114 st->codec->height = AV_RL16(&header[8]);
5b4c5628
JR
115 fps = header[12];
116 if (fps < 1 || fps > 30) {
117 av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
118 return AVERROR_INVALIDDATA;
119 }
120 avpriv_set_pts_info(st, 64, 1, fps);
2fdf638b 121
61710f7b 122 /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
fead30d4 123 if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
3b3bbdd3 124 st = avformat_new_stream(s, NULL);
20f269dc 125 if (!st)
769e10f0 126 return AVERROR(ENOMEM);
e1ac69fa 127 st->start_time = 0;
72415b2a 128 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
fead30d4 129 if (AV_RL16(&header[0]) == 1)
61710f7b
KS
130 st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
131 else
132 st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
01f4895c 133 st->codec->codec_tag = 0; /* no tag */
fead30d4 134 st->codec->sample_rate = AV_RL16(&header[24]);
61710f7b
KS
135 if (!st->codec->sample_rate)
136 st->codec->sample_rate = 22050;
01f4895c 137 st->codec->channels = header[26];
61710f7b
KS
138 if (!st->codec->channels)
139 st->codec->channels = 1;
dd1c8f3e 140 st->codec->bits_per_coded_sample = 16;
01f4895c 141 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
dd1c8f3e
LA
142 st->codec->bits_per_coded_sample / 4;
143 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
20f269dc 144
53ed79a2
JR
145 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
146
20f269dc 147 wsvqa->audio_stream_index = st->index;
01f4895c
MN
148 wsvqa->audio_samplerate = st->codec->sample_rate;
149 wsvqa->audio_channels = st->codec->channels;
20f269dc 150 }
2fdf638b 151
20f269dc
MM
152 /* there are 0 or more chunks before the FINF chunk; iterate until
153 * FINF has been skipped and the file will be ready to be demuxed */
154 do {
b7effd4e 155 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
01f4895c 156 av_free(st->codec->extradata);
6f3e0b21 157 return AVERROR(EIO);
20f269dc 158 }
fead30d4
AB
159 chunk_tag = AV_RB32(&scratch[0]);
160 chunk_size = AV_RB32(&scratch[4]);
20f269dc
MM
161
162 /* catch any unknown header tags, for curiousity */
163 switch (chunk_tag) {
164 case CINF_TAG:
165 case CINH_TAG:
166 case CIND_TAG:
167 case PINF_TAG:
168 case PINH_TAG:
169 case PIND_TAG:
170 case FINF_TAG:
61710f7b 171 case CMDS_TAG:
20f269dc
MM
172 break;
173
174 default:
bc874dae 175 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
20f269dc
MM
176 scratch[0], scratch[1],
177 scratch[2], scratch[3]);
178 break;
179 }
180
45a8a02a 181 avio_skip(pb, chunk_size);
20f269dc 182 } while (chunk_tag != FINF_TAG);
2fdf638b 183
2fdf638b
MM
184 return 0;
185}
186
187static int wsvqa_read_packet(AVFormatContext *s,
188 AVPacket *pkt)
189{
e4141433 190 WsVqaDemuxContext *wsvqa = s->priv_data;
ae628ec1 191 AVIOContext *pb = s->pb;
61710f7b 192 int ret = -1;
2fdf638b
MM
193 unsigned char preamble[VQA_PREAMBLE_SIZE];
194 unsigned int chunk_type;
195 unsigned int chunk_size;
196 int skip_byte;
197
b7effd4e 198 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
fead30d4
AB
199 chunk_type = AV_RB32(&preamble[0]);
200 chunk_size = AV_RB32(&preamble[4]);
61710f7b
KS
201 skip_byte = chunk_size & 0x01;
202
566ee0ea
SG
203 if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
204 av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
205 return AVERROR_INVALIDDATA;
206 }
207
61710f7b
KS
208 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
209
210 if (av_new_packet(pkt, chunk_size))
6f3e0b21 211 return AVERROR(EIO);
b7effd4e 212 ret = avio_read(pb, pkt->data, chunk_size);
61710f7b
KS
213 if (ret != chunk_size) {
214 av_free_packet(pkt);
6f3e0b21 215 return AVERROR(EIO);
61710f7b
KS
216 }
217
218 if (chunk_type == SND2_TAG) {
219 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b 220 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
9ae84697 221 pkt->duration = (chunk_size * 2) / wsvqa->audio_channels;
61710f7b
KS
222 } else if(chunk_type == SND1_TAG) {
223 pkt->stream_index = wsvqa->audio_stream_index;
61710f7b 224 /* unpacked size is stored in header */
9ae84697 225 pkt->duration = AV_RL16(pkt->data) / wsvqa->audio_channels;
61710f7b
KS
226 } else {
227 pkt->stream_index = wsvqa->video_stream_index;
9ae84697 228 pkt->duration = 1;
61710f7b
KS
229 }
230 /* stay on 16-bit alignment */
231 if (skip_byte)
45a8a02a 232 avio_skip(pb, 1);
61710f7b
KS
233
234 return ret;
2fdf638b 235 } else {
61710f7b
KS
236 switch(chunk_type){
237 case CMDS_TAG:
238 case SND0_TAG:
239 break;
240 default:
241 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
242 }
45a8a02a 243 avio_skip(pb, chunk_size + skip_byte);
2fdf638b 244 }
61710f7b 245 }
2fdf638b
MM
246
247 return ret;
248}
249
c6610a21 250AVInputFormat ff_wsvqa_demuxer = {
dfc2c4d9
AK
251 .name = "wsvqa",
252 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
253 .priv_data_size = sizeof(WsVqaDemuxContext),
254 .read_probe = wsvqa_probe,
255 .read_header = wsvqa_read_header,
256 .read_packet = wsvqa_read_packet,
2fdf638b 257};