vqa: set stream start_time to 0.
[libav.git] / libavformat / westwood_vqa.c
1 /*
2 * Westwood Studios VQA Format Demuxer
3 * Copyright (c) 2003 The ffmpeg Project
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
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
29 */
30
31 #include "libavutil/intreadwrite.h"
32 #include "avformat.h"
33 #include "internal.h"
34
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')
43
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')
52
53 #define VQA_HEADER_SIZE 0x2A
54 #define VQA_FRAMERATE 15
55 #define VQA_PREAMBLE_SIZE 8
56
57 typedef struct WsVqaDemuxContext {
58 int audio_samplerate;
59 int audio_channels;
60 int audio_bits;
61
62 int audio_stream_index;
63 int video_stream_index;
64
65 int64_t audio_frame_counter;
66 } WsVqaDemuxContext;
67
68 static int wsvqa_probe(AVProbeData *p)
69 {
70 /* need 12 bytes to qualify */
71 if (p->buf_size < 12)
72 return 0;
73
74 /* check for the VQA signatures */
75 if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
76 (AV_RB32(&p->buf[8]) != WVQA_TAG))
77 return 0;
78
79 return AVPROBE_SCORE_MAX;
80 }
81
82 static int wsvqa_read_header(AVFormatContext *s,
83 AVFormatParameters *ap)
84 {
85 WsVqaDemuxContext *wsvqa = s->priv_data;
86 AVIOContext *pb = s->pb;
87 AVStream *st;
88 unsigned char *header;
89 unsigned char scratch[VQA_PREAMBLE_SIZE];
90 unsigned int chunk_tag;
91 unsigned int chunk_size;
92
93 /* initialize the video decoder stream */
94 st = avformat_new_stream(s, NULL);
95 if (!st)
96 return AVERROR(ENOMEM);
97 st->start_time = 0;
98 avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
99 wsvqa->video_stream_index = st->index;
100 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
101 st->codec->codec_id = CODEC_ID_WS_VQA;
102 st->codec->codec_tag = 0; /* no fourcc */
103
104 /* skip to the start of the VQA header */
105 avio_seek(pb, 20, SEEK_SET);
106
107 /* the VQA header needs to go to the decoder */
108 st->codec->extradata_size = VQA_HEADER_SIZE;
109 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
110 header = (unsigned char *)st->codec->extradata;
111 if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
112 VQA_HEADER_SIZE) {
113 av_free(st->codec->extradata);
114 return AVERROR(EIO);
115 }
116 st->codec->width = AV_RL16(&header[6]);
117 st->codec->height = AV_RL16(&header[8]);
118
119 /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
120 if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
121 st = avformat_new_stream(s, NULL);
122 if (!st)
123 return AVERROR(ENOMEM);
124 st->start_time = 0;
125 avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
126 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
127 if (AV_RL16(&header[0]) == 1)
128 st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
129 else
130 st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
131 st->codec->codec_tag = 0; /* no tag */
132 st->codec->sample_rate = AV_RL16(&header[24]);
133 if (!st->codec->sample_rate)
134 st->codec->sample_rate = 22050;
135 st->codec->channels = header[26];
136 if (!st->codec->channels)
137 st->codec->channels = 1;
138 st->codec->bits_per_coded_sample = 16;
139 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
140 st->codec->bits_per_coded_sample / 4;
141 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
142
143 wsvqa->audio_stream_index = st->index;
144 wsvqa->audio_samplerate = st->codec->sample_rate;
145 wsvqa->audio_channels = st->codec->channels;
146 wsvqa->audio_frame_counter = 0;
147 }
148
149 /* there are 0 or more chunks before the FINF chunk; iterate until
150 * FINF has been skipped and the file will be ready to be demuxed */
151 do {
152 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
153 av_free(st->codec->extradata);
154 return AVERROR(EIO);
155 }
156 chunk_tag = AV_RB32(&scratch[0]);
157 chunk_size = AV_RB32(&scratch[4]);
158
159 /* catch any unknown header tags, for curiousity */
160 switch (chunk_tag) {
161 case CINF_TAG:
162 case CINH_TAG:
163 case CIND_TAG:
164 case PINF_TAG:
165 case PINH_TAG:
166 case PIND_TAG:
167 case FINF_TAG:
168 case CMDS_TAG:
169 break;
170
171 default:
172 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
173 scratch[0], scratch[1],
174 scratch[2], scratch[3]);
175 break;
176 }
177
178 avio_skip(pb, chunk_size);
179 } while (chunk_tag != FINF_TAG);
180
181 return 0;
182 }
183
184 static int wsvqa_read_packet(AVFormatContext *s,
185 AVPacket *pkt)
186 {
187 WsVqaDemuxContext *wsvqa = s->priv_data;
188 AVIOContext *pb = s->pb;
189 int ret = -1;
190 unsigned char preamble[VQA_PREAMBLE_SIZE];
191 unsigned int chunk_type;
192 unsigned int chunk_size;
193 int skip_byte;
194
195 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
196 chunk_type = AV_RB32(&preamble[0]);
197 chunk_size = AV_RB32(&preamble[4]);
198 skip_byte = chunk_size & 0x01;
199
200 if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
201 av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
202 return AVERROR_INVALIDDATA;
203 }
204
205 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
206
207 if (av_new_packet(pkt, chunk_size))
208 return AVERROR(EIO);
209 ret = avio_read(pb, pkt->data, chunk_size);
210 if (ret != chunk_size) {
211 av_free_packet(pkt);
212 return AVERROR(EIO);
213 }
214
215 if (chunk_type == SND2_TAG) {
216 pkt->stream_index = wsvqa->audio_stream_index;
217 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
218 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
219 } else if(chunk_type == SND1_TAG) {
220 pkt->stream_index = wsvqa->audio_stream_index;
221 /* unpacked size is stored in header */
222 wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
223 } else {
224 pkt->stream_index = wsvqa->video_stream_index;
225 }
226 /* stay on 16-bit alignment */
227 if (skip_byte)
228 avio_skip(pb, 1);
229
230 return ret;
231 } else {
232 switch(chunk_type){
233 case CMDS_TAG:
234 case SND0_TAG:
235 break;
236 default:
237 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
238 }
239 avio_skip(pb, chunk_size + skip_byte);
240 }
241 }
242
243 return ret;
244 }
245
246 AVInputFormat ff_wsvqa_demuxer = {
247 .name = "wsvqa",
248 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
249 .priv_data_size = sizeof(WsVqaDemuxContext),
250 .read_probe = wsvqa_probe,
251 .read_header = wsvqa_read_header,
252 .read_packet = wsvqa_read_packet,
253 };