FFmpeg
westwood_vqa.c
Go to the documentation of this file.
1 /*
2  * Westwood Studios VQA Format Demuxer
3  * Copyright (c) 2003 Mike Melanson <melanson@pcisys.net>
4  * Copyright (c) 2021 Pekka Väänänen <pekka.vaananen@iki.fi>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * Westwood Studios VQA file demuxer
26  * by Mike Melanson (melanson@pcisys.net)
27  * for more information on the Westwood file formats, visit:
28  * http://www.pcisys.net/~melanson/codecs/
29  * http://www.geocities.com/SiliconValley/8682/aud3.txt
30  */
31 
32 #include "libavutil/intreadwrite.h"
33 #include "avformat.h"
34 #include "avio_internal.h"
35 #include "internal.h"
36 
37 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
38 #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
39 #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
40 #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
41 #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
42 #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
43 #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
44 #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
45 #define VQFL_TAG MKBETAG('V', 'Q', 'F', 'L')
46 
47 /* don't know what these tags are for, but acknowledge their existence */
48 #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
49 #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
50 #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
51 #define LINF_TAG MKBETAG('L', 'I', 'N', 'F')
52 #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
53 #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
54 #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
55 #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
56 #define SN2J_TAG MKBETAG('S', 'N', '2', 'J')
57 #define VIEW_TAG MKBETAG('V', 'I', 'E', 'W')
58 #define ZBUF_TAG MKBETAG('Z', 'B', 'U', 'F')
59 
60 #define VQA_HEADER_SIZE 0x2A
61 #define VQA_PREAMBLE_SIZE 8
62 
63 typedef struct WsVqaDemuxContext {
64  int version;
65  int bps;
66  int channels;
70  int64_t vqfl_chunk_pos;
73 
74 static int wsvqa_probe(const AVProbeData *p)
75 {
76  /* need 12 bytes to qualify */
77  if (p->buf_size < 12)
78  return 0;
79 
80  /* check for the VQA signatures */
81  if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
82  (AV_RB32(&p->buf[8]) != WVQA_TAG))
83  return 0;
84 
85  return AVPROBE_SCORE_MAX;
86 }
87 
89 {
90  WsVqaDemuxContext *wsvqa = s->priv_data;
91  AVIOContext *pb = s->pb;
92  AVStream *st;
93  uint8_t *header;
94  uint8_t scratch[VQA_PREAMBLE_SIZE];
95  uint32_t chunk_tag;
96  uint32_t chunk_size;
97  int fps, ret;
98 
99  /* initialize the video decoder stream */
100  st = avformat_new_stream(s, NULL);
101  if (!st)
102  return AVERROR(ENOMEM);
103  st->start_time = 0;
104  wsvqa->video_stream_index = st->index;
107  st->codecpar->codec_tag = 0; /* no fourcc */
108 
109  /* skip to the start of the VQA header */
110  avio_seek(pb, 20, SEEK_SET);
111 
112  /* the VQA header needs to go to the decoder */
113  if ((ret = ff_get_extradata(s, st->codecpar, pb, VQA_HEADER_SIZE)) < 0)
114  return ret;
115  header = st->codecpar->extradata;
116  st->codecpar->width = AV_RL16(&header[6]);
117  st->codecpar->height = AV_RL16(&header[8]);
118  fps = header[12];
119  st->nb_frames =
120  st->duration = AV_RL16(&header[4]);
121  if (fps < 1 || fps > 30) {
122  av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
123  return AVERROR_INVALIDDATA;
124  }
125  avpriv_set_pts_info(st, 64, 1, fps);
126 
127  wsvqa->version = AV_RL16(&header[ 0]);
128  wsvqa->sample_rate = AV_RL16(&header[24]);
129  wsvqa->channels = header[26];
130  wsvqa->bps = header[27];
131  wsvqa->audio_stream_index = -1;
132  wsvqa->vqfl_chunk_pos = 0;
133  wsvqa->vqfl_chunk_size = 0;
134 
135  s->ctx_flags |= AVFMTCTX_NOHEADER;
136 
137  /* there are 0 or more chunks before the FINF chunk; iterate until
138  * FINF has been skipped and the file will be ready to be demuxed */
139  do {
140  if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
141  return AVERROR(EIO);
142  chunk_tag = AV_RB32(&scratch[0]);
143  chunk_size = AV_RB32(&scratch[4]);
144 
145  /* catch any unknown header tags, for curiosity */
146  switch (chunk_tag) {
147  case CINF_TAG:
148  case CINH_TAG:
149  case CIND_TAG:
150  case LINF_TAG:
151  case PINF_TAG:
152  case PINH_TAG:
153  case PIND_TAG:
154  case FINF_TAG:
155  case CMDS_TAG:
156  case VIEW_TAG:
157  case ZBUF_TAG:
158  break;
159 
160  default:
161  av_log(s, AV_LOG_ERROR, " note: unknown chunk seen (%s)\n",
162  av_fourcc2str(chunk_tag));
163  break;
164  }
165 
166  avio_skip(pb, chunk_size);
167  } while (chunk_tag != FINF_TAG);
168 
169  return 0;
170 }
171 
173  AVPacket *pkt)
174 {
175  WsVqaDemuxContext *wsvqa = s->priv_data;
176  AVIOContext *pb = s->pb;
177  int ret = -1;
178  uint8_t preamble[VQA_PREAMBLE_SIZE];
179  uint32_t chunk_type;
180  uint32_t chunk_size;
181  int skip_byte;
182 
183  while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
184  chunk_type = AV_RB32(&preamble[0]);
185  chunk_size = AV_RB32(&preamble[4]);
186 
187  skip_byte = chunk_size & 0x01;
188 
189  if (chunk_type == VQFL_TAG) {
190  /* Each VQFL chunk carries only a codebook update inside which must be applied
191  * before the next VQFR is rendered. That's why we stash the VQFL offset here
192  * so it can be combined with the next VQFR packet. This way each packet
193  * includes a whole frame as expected. */
194  wsvqa->vqfl_chunk_pos = avio_tell(pb);
195  wsvqa->vqfl_chunk_size = (int)(chunk_size);
196  if (wsvqa->vqfl_chunk_size < 0 || wsvqa->vqfl_chunk_size > 3 * (1 << 20))
197  return AVERROR_INVALIDDATA;
198  /* We need a big seekback buffer because there can be SNxx, VIEW and ZBUF
199  * chunks (<512 KiB total) in the stream before we read VQFR (<256 KiB) and
200  * seek back here. */
201  ffio_ensure_seekback(pb, wsvqa->vqfl_chunk_size + (512 + 256) * 1024);
202  avio_skip(pb, chunk_size + skip_byte);
203  continue;
204  } else if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
205  (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
206 
207  ret= av_get_packet(pb, pkt, chunk_size);
208  if (ret<0)
209  return AVERROR(EIO);
210 
211  switch (chunk_type) {
212  case SND0_TAG:
213  case SND1_TAG:
214  case SND2_TAG:
215  if (wsvqa->audio_stream_index == -1) {
217  if (!st)
218  return AVERROR(ENOMEM);
219 
220  wsvqa->audio_stream_index = st->index;
221  if (!wsvqa->sample_rate)
222  wsvqa->sample_rate = 22050;
223  if (!wsvqa->channels)
224  wsvqa->channels = 1;
225  if (!wsvqa->bps)
226  wsvqa->bps = 8;
227  st->codecpar->sample_rate = wsvqa->sample_rate;
228  st->codecpar->bits_per_coded_sample = wsvqa->bps;
229  st->codecpar->channels = wsvqa->channels;
231 
232  avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
233 
234  switch (chunk_type) {
235  case SND0_TAG:
236  if (wsvqa->bps == 16)
238  else
240  break;
241  case SND1_TAG:
243  break;
244  case SND2_TAG:
246  if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
247  return ret;
248  AV_WL16(st->codecpar->extradata, wsvqa->version);
249  break;
250  }
251  }
252 
254  switch (chunk_type) {
255  case SND1_TAG:
256  /* unpacked size is stored in header */
257  if(pkt->data)
258  pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
259  break;
260  case SND2_TAG:
261  /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
262  pkt->duration = (chunk_size * 2) / wsvqa->channels;
263  break;
264  }
265  break;
266  case VQFR_TAG:
267  /* if a new codebook is available inside an earlier a VQFL chunk then
268  * append it to 'pkt' */
269  if (wsvqa->vqfl_chunk_size > 0) {
270  int64_t current_pos = pkt->pos;
271 
272  if (avio_seek(pb, wsvqa->vqfl_chunk_pos, SEEK_SET) < 0)
273  return AVERROR(EIO);
274 
275  /* the decoder expects chunks to be 16-bit aligned */
276  if (wsvqa->vqfl_chunk_size % 2 == 1)
277  wsvqa->vqfl_chunk_size++;
278 
279  if (av_append_packet(pb, pkt, wsvqa->vqfl_chunk_size) < 0)
280  return AVERROR(EIO);
281 
282  if (avio_seek(pb, current_pos, SEEK_SET) < 0)
283  return AVERROR(EIO);
284 
285  wsvqa->vqfl_chunk_pos = 0;
286  wsvqa->vqfl_chunk_size = 0;
287  }
288 
290  pkt->duration = 1;
291  break;
292  }
293 
294  /* stay on 16-bit alignment */
295  if (skip_byte)
296  avio_skip(pb, 1);
297 
298  return ret;
299  } else {
300  switch(chunk_type){
301  case CMDS_TAG:
302  case SN2J_TAG:
303  case VIEW_TAG:
304  case ZBUF_TAG:
305  break;
306  default:
307  av_log(s, AV_LOG_INFO, "Skipping unknown chunk %s\n",
308  av_fourcc2str(av_bswap32(chunk_type)));
309  }
310  avio_skip(pb, chunk_size + skip_byte);
311  }
312  }
313 
314  return ret;
315 }
316 
318  .name = "wsvqa",
319  .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
320  .priv_data_size = sizeof(WsVqaDemuxContext),
324 };
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:314
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:768
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:469
PIND_TAG
#define PIND_TAG
Definition: westwood_vqa.c:54
wsvqa_read_packet
static int wsvqa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: westwood_vqa.c:172
AVPacket::data
uint8_t * data
Definition: packet.h:373
ZBUF_TAG
#define ZBUF_TAG
Definition: westwood_vqa.c:58
WsVqaDemuxContext::version
int version
Definition: westwood_vqa.c:64
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:391
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:450
WsVqaDemuxContext::vqfl_chunk_pos
int64_t vqfl_chunk_pos
Definition: westwood_vqa.c:70
WsVqaDemuxContext::channels
int channels
Definition: westwood_vqa.c:66
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:459
WsVqaDemuxContext::vqfl_chunk_size
int vqfl_chunk_size
Definition: westwood_vqa.c:71
WsVqaDemuxContext::bps
int bps
Definition: westwood_vqa.c:65
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
SN2J_TAG
#define SN2J_TAG
Definition: westwood_vqa.c:56
CMDS_TAG
#define CMDS_TAG
Definition: westwood_vqa.c:55
WsVqaDemuxContext::video_stream_index
int video_stream_index
Definition: westwood_vqa.c:69
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:504
VQFR_TAG
#define VQFR_TAG
Definition: westwood_vqa.c:44
CINH_TAG
#define CINH_TAG
Definition: westwood_vqa.c:49
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:985
FORM_TAG
#define FORM_TAG
Definition: westwood_vqa.c:37
VQFL_TAG
#define VQFL_TAG
Definition: westwood_vqa.c:45
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:180
AVInputFormat
Definition: avformat.h:650
LINF_TAG
#define LINF_TAG
Definition: westwood_vqa.c:51
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:655
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:449
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:94
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:53
AVFormatContext
Format I/O context.
Definition: avformat.h:1200
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1095
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:41
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:1151
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:447
ff_wsvqa_demuxer
const AVInputFormat ff_wsvqa_demuxer
Definition: westwood_vqa.c:317
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:987
WVQA_TAG
#define WVQA_TAG
Definition: westwood_vqa.c:38
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:61
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:43
header
static const uint8_t header[24]
Definition: sdr2.c:67
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
ffio_ensure_seekback
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition: aviobuf.c:1055
FINF_TAG
#define FINF_TAG
Definition: westwood_vqa.c:40
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
CINF_TAG
#define CINF_TAG
Definition: westwood_vqa.c:48
VQA_HEADER_SIZE
#define VQA_HEADER_SIZE
Definition: westwood_vqa.c:60
avio_internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
PINF_TAG
#define PINF_TAG
Definition: westwood_vqa.c:52
wsvqa_read_header
static int wsvqa_read_header(AVFormatContext *s)
Definition: westwood_vqa.c:88
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:197
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:935
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
av_append_packet
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:213
avformat.h
AV_CODEC_ID_WESTWOOD_SND1
@ AV_CODEC_ID_WESTWOOD_SND1
Definition: codec_id.h:440
VIEW_TAG
#define VIEW_TAG
Definition: westwood_vqa.c:57
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:943
wsvqa_probe
static int wsvqa_probe(const AVProbeData *p)
Definition: westwood_vqa.c:74
WsVqaDemuxContext
Definition: westwood_vqa.c:63
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:641
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, 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:1196
AVPacket::stream_index
int stream_index
Definition: packet.h:375
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:347
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
SND1_TAG
#define SND1_TAG
Definition: westwood_vqa.c:42
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:50
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:319
WsVqaDemuxContext::sample_rate
int sample_rate
Definition: westwood_vqa.c:67
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:350
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:393
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:61
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:975
int
int
Definition: ffmpeg_filter.c:153
WsVqaDemuxContext::audio_stream_index
int audio_stream_index
Definition: westwood_vqa.c:68
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:451