westwood_vqa.c
Go to the documentation of this file.
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 
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_PREAMBLE_SIZE 8
55 
56 typedef struct WsVqaDemuxContext {
57  int version;
58  int bps;
59  int channels;
64 
65 static 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 */
72  if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
73  (AV_RB32(&p->buf[8]) != WVQA_TAG))
74  return 0;
75 
76  return AVPROBE_SCORE_MAX;
77 }
78 
80 {
81  WsVqaDemuxContext *wsvqa = s->priv_data;
82  AVIOContext *pb = s->pb;
83  AVStream *st;
84  unsigned char *header;
85  unsigned char scratch[VQA_PREAMBLE_SIZE];
86  unsigned int chunk_tag;
87  unsigned int chunk_size;
88  int fps;
89 
90  /* initialize the video decoder stream */
91  st = avformat_new_stream(s, NULL);
92  if (!st)
93  return AVERROR(ENOMEM);
94  st->start_time = 0;
95  wsvqa->video_stream_index = st->index;
98  st->codec->codec_tag = 0; /* no fourcc */
99 
100  /* skip to the start of the VQA header */
101  avio_seek(pb, 20, SEEK_SET);
102 
103  /* the VQA header needs to go to the decoder */
106  header = (unsigned char *)st->codec->extradata;
107  if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
108  VQA_HEADER_SIZE) {
109  return AVERROR(EIO);
110  }
111  st->codec->width = AV_RL16(&header[6]);
112  st->codec->height = AV_RL16(&header[8]);
113  fps = header[12];
114  st->nb_frames =
115  st->duration = AV_RL16(&header[4]);
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);
121 
122  wsvqa->version = AV_RL16(&header[ 0]);
123  wsvqa->sample_rate = AV_RL16(&header[24]);
124  wsvqa->channels = header[26];
125  wsvqa->bps = header[27];
126  wsvqa->audio_stream_index = -1;
127 
129 
130  /* there are 0 or more chunks before the FINF chunk; iterate until
131  * FINF has been skipped and the file will be ready to be demuxed */
132  do {
133  if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
134  return AVERROR(EIO);
135  }
136  chunk_tag = AV_RB32(&scratch[0]);
137  chunk_size = AV_RB32(&scratch[4]);
138 
139  /* catch any unknown header tags, for curiousity */
140  switch (chunk_tag) {
141  case CINF_TAG:
142  case CINH_TAG:
143  case CIND_TAG:
144  case PINF_TAG:
145  case PINH_TAG:
146  case PIND_TAG:
147  case FINF_TAG:
148  case CMDS_TAG:
149  break;
150 
151  default:
152  av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
153  scratch[0], scratch[1],
154  scratch[2], scratch[3]);
155  break;
156  }
157 
158  avio_skip(pb, chunk_size);
159  } while (chunk_tag != FINF_TAG);
160 
161  return 0;
162 }
163 
165  AVPacket *pkt)
166 {
167  WsVqaDemuxContext *wsvqa = s->priv_data;
168  AVIOContext *pb = s->pb;
169  int ret = -1;
170  unsigned char preamble[VQA_PREAMBLE_SIZE];
171  unsigned int chunk_type;
172  unsigned int chunk_size;
173  int skip_byte;
174 
175  while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
176  chunk_type = AV_RB32(&preamble[0]);
177  chunk_size = AV_RB32(&preamble[4]);
178  skip_byte = chunk_size & 0x01;
179 
180  if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
181  (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
182 
183  if (av_new_packet(pkt, chunk_size))
184  return AVERROR(EIO);
185  ret = avio_read(pb, pkt->data, chunk_size);
186  if (ret != chunk_size) {
187  av_free_packet(pkt);
188  return AVERROR(EIO);
189  }
190 
191  switch (chunk_type) {
192  case SND0_TAG:
193  case SND1_TAG:
194  case SND2_TAG:
195  if (wsvqa->audio_stream_index == -1) {
197  if (!st)
198  return AVERROR(ENOMEM);
199 
200  wsvqa->audio_stream_index = st->index;
201  if (!wsvqa->sample_rate)
202  wsvqa->sample_rate = 22050;
203  if (!wsvqa->channels)
204  wsvqa->channels = 1;
205  if (!wsvqa->bps)
206  wsvqa->bps = 8;
207  st->codec->sample_rate = wsvqa->sample_rate;
208  st->codec->bits_per_coded_sample = wsvqa->bps;
209  st->codec->channels = wsvqa->channels;
211 
212  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
213 
214  switch (chunk_type) {
215  case SND0_TAG:
216  if (wsvqa->bps == 16)
218  else
220  break;
221  case SND1_TAG:
223  break;
224  case SND2_TAG:
226  st->codec->extradata_size = 2;
228  if (!st->codec->extradata)
229  return AVERROR(ENOMEM);
230  AV_WL16(st->codec->extradata, wsvqa->version);
231  break;
232  }
233  }
234 
235  pkt->stream_index = wsvqa->audio_stream_index;
236  switch (chunk_type) {
237  case SND1_TAG:
238  /* unpacked size is stored in header */
239  pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
240  break;
241  case SND2_TAG:
242  /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
243  pkt->duration = (chunk_size * 2) / wsvqa->channels;
244  break;
245  }
246  break;
247  case VQFR_TAG:
248  pkt->stream_index = wsvqa->video_stream_index;
249  pkt->duration = 1;
250  break;
251  }
252 
253  /* stay on 16-bit alignment */
254  if (skip_byte)
255  avio_skip(pb, 1);
256 
257  return ret;
258  } else {
259  switch(chunk_type){
260  case CMDS_TAG:
261  break;
262  default:
263  av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
264  }
265  avio_skip(pb, chunk_size + skip_byte);
266  }
267  }
268 
269  return ret;
270 }
271 
273  .name = "wsvqa",
274  .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
275  .priv_data_size = sizeof(WsVqaDemuxContext),
279 };