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(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;
108  VQA_HEADER_SIZE) {
109  av_free(st->codec->extradata);
110  return AVERROR(EIO);
111  }
112  st->codec->width = AV_RL16(&header[6]);
113  st->codec->height = AV_RL16(&header[8]);
114  fps = header[12];
115  st->nb_frames =
116  st->duration = AV_RL16(&header[4]);
117  if (fps < 1 || fps > 30) {
118  av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
119  return AVERROR_INVALIDDATA;
120  }
121  avpriv_set_pts_info(st, 64, 1, fps);
122 
123  wsvqa->version = AV_RL16(&header[ 0]);
124  wsvqa->sample_rate = AV_RL16(&header[24]);
125  wsvqa->channels = header[26];
126  wsvqa->bps = header[27];
127  wsvqa->audio_stream_index = -1;
128 
130 
131  /* there are 0 or more chunks before the FINF chunk; iterate until
132  * FINF has been skipped and the file will be ready to be demuxed */
133  do {
134  if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
135  return AVERROR(EIO);
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 
179  skip_byte = chunk_size & 0x01;
180 
181  if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
182  (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
183 
184  ret= av_get_packet(pb, pkt, chunk_size);
185  if (ret<0)
186  return AVERROR(EIO);
187 
188  switch (chunk_type) {
189  case SND0_TAG:
190  case SND1_TAG:
191  case SND2_TAG:
192  if (wsvqa->audio_stream_index == -1) {
194  if (!st)
195  return AVERROR(ENOMEM);
196 
197  wsvqa->audio_stream_index = st->index;
198  if (!wsvqa->sample_rate)
199  wsvqa->sample_rate = 22050;
200  if (!wsvqa->channels)
201  wsvqa->channels = 1;
202  if (!wsvqa->bps)
203  wsvqa->bps = 8;
204  st->codec->sample_rate = wsvqa->sample_rate;
205  st->codec->bits_per_coded_sample = wsvqa->bps;
206  st->codec->channels = wsvqa->channels;
208 
209  avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
210 
211  switch (chunk_type) {
212  case SND0_TAG:
213  if (wsvqa->bps == 16)
215  else
217  break;
218  case SND1_TAG:
220  break;
221  case SND2_TAG:
223  st->codec->extradata_size = 2;
225  if (!st->codec->extradata)
226  return AVERROR(ENOMEM);
227  AV_WL16(st->codec->extradata, wsvqa->version);
228  break;
229  }
230  }
231 
232  pkt->stream_index = wsvqa->audio_stream_index;
233  switch (chunk_type) {
234  case SND1_TAG:
235  /* unpacked size is stored in header */
236  if(pkt->data)
237  pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
238  break;
239  case SND2_TAG:
240  /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
241  pkt->duration = (chunk_size * 2) / wsvqa->channels;
242  break;
243  }
244  break;
245  case VQFR_TAG:
246  pkt->stream_index = wsvqa->video_stream_index;
247  pkt->duration = 1;
248  break;
249  }
250 
251  /* stay on 16-bit alignment */
252  if (skip_byte)
253  avio_skip(pb, 1);
254 
255  return ret;
256  } else {
257  switch(chunk_type){
258  case CMDS_TAG:
259  break;
260  default:
261  av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
262  }
263  avio_skip(pb, chunk_size + skip_byte);
264  }
265  }
266 
267  return ret;
268 }
269 
271  .name = "wsvqa",
272  .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
273  .priv_data_size = sizeof(WsVqaDemuxContext),
277 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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.
int index
stream index in AVFormatContext
Definition: avformat.h:644
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
#define PIND_TAG
Definition: westwood_vqa.c:50
#define AV_RL16
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
static int wsvqa_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: westwood_vqa.c:164
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:980
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
Format I/O context.
Definition: avformat.h:944
#define FORM_TAG
Definition: westwood_vqa.c:35
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:914
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
uint8_t * data
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.
#define CMDS_TAG
Definition: westwood_vqa.c:51
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
#define CINH_TAG
Definition: westwood_vqa.c:46
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
#define VQFR_TAG
Definition: westwood_vqa.c:42
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define AV_WL16(p, darg)
Definition: intreadwrite.h:250
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
#define PINH_TAG
Definition: westwood_vqa.c:49
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
#define SND0_TAG
Definition: westwood_vqa.c:39
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
int width
picture width / height.
#define SND2_TAG
Definition: westwood_vqa.c:41
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
#define WVQA_TAG
Definition: westwood_vqa.c:36
#define FINF_TAG
Definition: westwood_vqa.c:38
NULL
Definition: eval.c:55
#define VQA_PREAMBLE_SIZE
Definition: westwood_vqa.c:54
#define CINF_TAG
Definition: westwood_vqa.c:45
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
static int wsvqa_probe(AVProbeData *p)
Definition: westwood_vqa.c:65
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
AVInputFormat ff_wsvqa_demuxer
Definition: westwood_vqa.c:270
#define VQA_HEADER_SIZE
Definition: westwood_vqa.c:53
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define PINF_TAG
Definition: westwood_vqa.c:48
static int wsvqa_read_header(AVFormatContext *s)
Definition: westwood_vqa.c:79
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
#define CIND_TAG
Definition: westwood_vqa.c:47
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:698
struct WsVqaDemuxContext WsVqaDemuxContext
int channels
number of audio channels
void * priv_data
Format private data.
Definition: avformat.h:964
#define AV_LOG_INFO
Definition: log.h:156
#define SND1_TAG
Definition: westwood_vqa.c:40
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.