FFmpeg
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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
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_PREAMBLE_SIZE 8
55 
56 typedef struct WsVqaDemuxContext {
57  int version;
58  int bps;
59  int channels;
64 
65 static int wsvqa_probe(const 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  uint8_t *header;
85  uint8_t scratch[VQA_PREAMBLE_SIZE];
86  uint32_t chunk_tag;
87  uint32_t chunk_size;
88  int fps, ret;
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->codecpar->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 */
104  if ((ret = ff_get_extradata(s, st->codecpar, pb, VQA_HEADER_SIZE)) < 0)
105  return ret;
106  header = st->codecpar->extradata;
107  st->codecpar->width = AV_RL16(&header[6]);
108  st->codecpar->height = AV_RL16(&header[8]);
109  fps = header[12];
110  st->nb_frames =
111  st->duration = AV_RL16(&header[4]);
112  if (fps < 1 || fps > 30) {
113  av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
114  return AVERROR_INVALIDDATA;
115  }
116  avpriv_set_pts_info(st, 64, 1, fps);
117 
118  wsvqa->version = AV_RL16(&header[ 0]);
119  wsvqa->sample_rate = AV_RL16(&header[24]);
120  wsvqa->channels = header[26];
121  wsvqa->bps = header[27];
122  wsvqa->audio_stream_index = -1;
123 
124  s->ctx_flags |= AVFMTCTX_NOHEADER;
125 
126  /* there are 0 or more chunks before the FINF chunk; iterate until
127  * FINF has been skipped and the file will be ready to be demuxed */
128  do {
129  if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
130  return AVERROR(EIO);
131  chunk_tag = AV_RB32(&scratch[0]);
132  chunk_size = AV_RB32(&scratch[4]);
133 
134  /* catch any unknown header tags, for curiosity */
135  switch (chunk_tag) {
136  case CINF_TAG:
137  case CINH_TAG:
138  case CIND_TAG:
139  case PINF_TAG:
140  case PINH_TAG:
141  case PIND_TAG:
142  case FINF_TAG:
143  case CMDS_TAG:
144  break;
145 
146  default:
147  av_log(s, AV_LOG_ERROR, " note: unknown chunk seen (%s)\n",
148  av_fourcc2str(chunk_tag));
149  break;
150  }
151 
152  avio_skip(pb, chunk_size);
153  } while (chunk_tag != FINF_TAG);
154 
155  return 0;
156 }
157 
159  AVPacket *pkt)
160 {
161  WsVqaDemuxContext *wsvqa = s->priv_data;
162  AVIOContext *pb = s->pb;
163  int ret = -1;
164  uint8_t preamble[VQA_PREAMBLE_SIZE];
165  uint32_t chunk_type;
166  uint32_t chunk_size;
167  int skip_byte;
168 
169  while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
170  chunk_type = AV_RB32(&preamble[0]);
171  chunk_size = AV_RB32(&preamble[4]);
172 
173  skip_byte = chunk_size & 0x01;
174 
175  if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
176  (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
177 
178  ret= av_get_packet(pb, pkt, chunk_size);
179  if (ret<0)
180  return AVERROR(EIO);
181 
182  switch (chunk_type) {
183  case SND0_TAG:
184  case SND1_TAG:
185  case SND2_TAG:
186  if (wsvqa->audio_stream_index == -1) {
188  if (!st)
189  return AVERROR(ENOMEM);
190 
191  wsvqa->audio_stream_index = st->index;
192  if (!wsvqa->sample_rate)
193  wsvqa->sample_rate = 22050;
194  if (!wsvqa->channels)
195  wsvqa->channels = 1;
196  if (!wsvqa->bps)
197  wsvqa->bps = 8;
198  st->codecpar->sample_rate = wsvqa->sample_rate;
199  st->codecpar->bits_per_coded_sample = wsvqa->bps;
200  st->codecpar->channels = wsvqa->channels;
202 
203  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
204 
205  switch (chunk_type) {
206  case SND0_TAG:
207  if (wsvqa->bps == 16)
209  else
211  break;
212  case SND1_TAG:
214  break;
215  case SND2_TAG:
217  if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
218  return ret;
219  AV_WL16(st->codecpar->extradata, wsvqa->version);
220  break;
221  }
222  }
223 
225  switch (chunk_type) {
226  case SND1_TAG:
227  /* unpacked size is stored in header */
228  if(pkt->data)
229  pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
230  break;
231  case SND2_TAG:
232  /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
233  pkt->duration = (chunk_size * 2) / wsvqa->channels;
234  break;
235  }
236  break;
237  case VQFR_TAG:
239  pkt->duration = 1;
240  break;
241  }
242 
243  /* stay on 16-bit alignment */
244  if (skip_byte)
245  avio_skip(pb, 1);
246 
247  return ret;
248  } else {
249  switch(chunk_type){
250  case CMDS_TAG:
251  break;
252  default:
253  av_log(s, AV_LOG_INFO, "Skipping unknown chunk %s\n",
254  av_fourcc2str(av_bswap32(chunk_type)));
255  }
256  avio_skip(pb, chunk_size + skip_byte);
257  }
258  }
259 
260  return ret;
261 }
262 
264  .name = "wsvqa",
265  .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
266  .priv_data_size = sizeof(WsVqaDemuxContext),
270 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3332
PIND_TAG
#define PIND_TAG
Definition: westwood_vqa.c:50
wsvqa_read_packet
static int wsvqa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: westwood_vqa.c:158
AVPacket::data
uint8_t * data
Definition: packet.h:369
WsVqaDemuxContext::version
int version
Definition: westwood_vqa.c:57
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
WsVqaDemuxContext::channels
int channels
Definition: westwood_vqa.c:59
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
WsVqaDemuxContext::bps
int bps
Definition: westwood_vqa.c:58
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
CMDS_TAG
#define CMDS_TAG
Definition: westwood_vqa.c:51
WsVqaDemuxContext::video_stream_index
int video_stream_index
Definition: westwood_vqa.c:62
VQFR_TAG
#define VQFR_TAG
Definition: westwood_vqa.c:42
CINH_TAG
#define CINH_TAG
Definition: westwood_vqa.c:46
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
FORM_TAG
#define FORM_TAG
Definition: westwood_vqa.c:35
av_bswap32
#define av_bswap32
Definition: bswap.h:33
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVInputFormat
Definition: avformat.h:640
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AV_CODEC_ID_WS_VQA
@ AV_CODEC_ID_WS_VQA
Definition: codec_id.h:93
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
PINH_TAG
#define PINH_TAG
Definition: westwood_vqa.c:49
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
NULL
#define NULL
Definition: coverity.c:32
SND0_TAG
#define SND0_TAG
Definition: westwood_vqa.c:39
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1177
AV_CODEC_ID_ADPCM_IMA_WS
@ AV_CODEC_ID_ADPCM_IMA_WS
Definition: codec_id.h:357
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:924
WVQA_TAG
#define WVQA_TAG
Definition: westwood_vqa.c:36
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
VQA_PREAMBLE_SIZE
#define VQA_PREAMBLE_SIZE
Definition: westwood_vqa.c:54
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
SND2_TAG
#define SND2_TAG
Definition: westwood_vqa.c:41
ff_wsvqa_demuxer
AVInputFormat ff_wsvqa_demuxer
Definition: westwood_vqa.c:263
header
static const uint8_t header[24]
Definition: sdr2.c:67
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
FINF_TAG
#define FINF_TAG
Definition: westwood_vqa.c:38
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
CINF_TAG
#define CINF_TAG
Definition: westwood_vqa.c:45
VQA_HEADER_SIZE
#define VQA_HEADER_SIZE
Definition: westwood_vqa.c:53
AVCodecParameters::height
int height
Definition: codec_par.h:127
PINF_TAG
#define PINF_TAG
Definition: westwood_vqa.c:48
wsvqa_read_header
static int wsvqa_read_header(AVFormatContext *s)
Definition: westwood_vqa.c:79
uint8_t
uint8_t
Definition: audio_convert.c:194
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:873
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
avformat.h
AV_CODEC_ID_WESTWOOD_SND1
@ AV_CODEC_ID_WESTWOOD_SND1
Definition: codec_id.h:441
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:874
wsvqa_probe
static int wsvqa_probe(const AVProbeData *p)
Definition: westwood_vqa.c:65
WsVqaDemuxContext
Definition: westwood_vqa.c:56
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
AVPacket::stream_index
int stream_index
Definition: packet.h:371
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SND1_TAG
#define SND1_TAG
Definition: westwood_vqa.c:40
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
CIND_TAG
#define CIND_TAG
Definition: westwood_vqa.c:47
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
WsVqaDemuxContext::sample_rate
int sample_rate
Definition: westwood_vqa.c:60
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:346
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
WsVqaDemuxContext::audio_stream_index
int audio_stream_index
Definition: westwood_vqa.c:61
av_fourcc2str
#define av_fourcc2str(fourcc)
Definition: avutil.h:348
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3314