FFmpeg
wvdec.c
Go to the documentation of this file.
1 /*
2  * WavPack demuxer
3  * Copyright (c) 2006,2011 Konstantin Shishkov
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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "avformat.h"
26 #include "avio_internal.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "apetag.h"
30 #include "id3v1.h"
31 #include "wv.h"
32 
33 enum WV_FLAGS {
34  WV_MONO = 0x0004,
35  WV_HYBRID = 0x0008,
36  WV_JOINT = 0x0010,
37  WV_CROSSD = 0x0020,
38  WV_HSHAPE = 0x0040,
39  WV_FLOAT = 0x0080,
40  WV_INT32 = 0x0100,
41  WV_HBR = 0x0200,
42  WV_HBAL = 0x0400,
43  WV_MCINIT = 0x0800,
44  WV_MCEND = 0x1000,
45  WV_DSD = 0x80000000,
46 };
47 
48 static const int wv_rates[16] = {
49  6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
50  32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
51 };
52 
53 typedef struct WVContext {
56  int rate, chan, bpp;
57  uint32_t chmask;
61 
63 } WVContext;
64 
65 static int wv_probe(const AVProbeData *p)
66 {
67  /* check file header */
68  if (p->buf_size <= 32)
69  return 0;
70  if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
71  AV_RL32(&p->buf[4]) >= 24 &&
72  AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
73  AV_RL16(&p->buf[8]) >= 0x402 &&
74  AV_RL16(&p->buf[8]) <= 0x410)
75  return AVPROBE_SCORE_MAX;
76  else
77  return 0;
78 }
79 
81 {
82  WVContext *wc = ctx->priv_data;
83  int ret;
84  int rate, bpp, chan;
85  uint32_t chmask, flags;
86  unsigned rate_x;
87 
88  wc->pos = avio_tell(pb);
89 
90  /* don't return bogus packets with the ape tag data */
91  if (wc->apetag_start && wc->pos >= wc->apetag_start)
92  return AVERROR_EOF;
93 
95  if (ret != WV_HEADER_SIZE)
96  return (ret < 0) ? ret : AVERROR_EOF;
97 
99  if (ret < 0) {
100  av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
101  return ret;
102  }
103 
104  if (wc->header.version < 0x402 || wc->header.version > 0x410) {
105  avpriv_report_missing_feature(ctx, "WV version 0x%03X",
106  wc->header.version);
107  return AVERROR_PATCHWELCOME;
108  }
109 
110  /* Blocks with zero samples don't contain actual audio information
111  * and should be ignored */
112  if (!wc->header.samples)
113  return 0;
114  // parse flags
115  flags = wc->header.flags;
116  rate_x = (flags & WV_DSD) ? 4 : 1;
117  bpp = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
118  chan = 1 + !(flags & WV_MONO);
120  rate = wv_rates[(flags >> 23) & 0xF];
121  wc->multichannel = !(wc->header.initial && wc->header.final);
122  if (wc->multichannel) {
123  chan = wc->chan;
124  chmask = wc->chmask;
125  }
126  if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
127  int64_t block_end = avio_tell(pb) + wc->header.blocksize;
128  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
130  "Cannot determine additional parameters\n");
131  return AVERROR_INVALIDDATA;
132  }
133  while (avio_tell(pb) < block_end && !avio_feof(pb)) {
134  int id, size;
135  id = avio_r8(pb);
136  size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
137  size <<= 1;
138  if (id & 0x40)
139  size--;
140  switch (id & 0x3F) {
141  case 0xD:
142  if (size <= 1) {
144  "Insufficient channel information\n");
145  return AVERROR_INVALIDDATA;
146  }
147  chan = avio_r8(pb);
148  switch (size - 2) {
149  case 0:
150  chmask = avio_r8(pb);
151  break;
152  case 1:
153  chmask = avio_rl16(pb);
154  break;
155  case 2:
156  chmask = avio_rl24(pb);
157  break;
158  case 3:
159  chmask = avio_rl32(pb);
160  break;
161  case 4:
162  avio_skip(pb, 1);
163  chan |= (avio_r8(pb) & 0xF) << 8;
164  chan += 1;
165  chmask = avio_rl24(pb);
166  break;
167  case 5:
168  avio_skip(pb, 1);
169  chan |= (avio_r8(pb) & 0xF) << 8;
170  chan += 1;
171  chmask = avio_rl32(pb);
172  break;
173  default:
175  "Invalid channel info size %d\n", size);
176  return AVERROR_INVALIDDATA;
177  }
178  break;
179  case 0xE:
180  if (size <= 1) {
182  "Invalid DSD block\n");
183  return AVERROR_INVALIDDATA;
184  }
185  rate_x = 1U << (avio_r8(pb) & 0x1f);
186  if (size)
187  avio_skip(pb, size-1);
188  break;
189  case 0x27:
190  rate = avio_rl24(pb);
191  break;
192  default:
193  avio_skip(pb, size);
194  }
195  if (id & 0x40)
196  avio_skip(pb, 1);
197  }
198  if (rate == -1 || rate * (uint64_t)rate_x >= INT_MAX) {
200  "Cannot determine custom sampling rate\n");
201  return AVERROR_INVALIDDATA;
202  }
203  avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
204  }
205  if (!wc->bpp)
206  wc->bpp = bpp;
207  if (!wc->chan)
208  wc->chan = chan;
209  if (!wc->chmask)
210  wc->chmask = chmask;
211  if (!wc->rate)
212  wc->rate = rate * rate_x;
213 
214  if (flags && bpp != wc->bpp) {
216  "Bits per sample differ, this block: %i, header block: %i\n",
217  bpp, wc->bpp);
218  return AVERROR_INVALIDDATA;
219  }
220  if (flags && !wc->multichannel && chan != wc->chan) {
222  "Channels differ, this block: %i, header block: %i\n",
223  chan, wc->chan);
224  return AVERROR_INVALIDDATA;
225  }
226  if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
228  "Sampling rate differ, this block: %i, header block: %i\n",
229  rate * rate_x, wc->rate);
230  return AVERROR_INVALIDDATA;
231  }
232  return 0;
233 }
234 
236 {
237  AVIOContext *pb = s->pb;
238  WVContext *wc = s->priv_data;
239  AVStream *st;
240  int ret;
241 
242  wc->block_parsed = 0;
243  for (;;) {
244  if ((ret = wv_read_block_header(s, pb)) < 0)
245  return ret;
246  if (!wc->header.samples)
247  avio_skip(pb, wc->header.blocksize);
248  else
249  break;
250  }
251 
252  /* now we are ready: build format streams */
253  st = avformat_new_stream(s, NULL);
254  if (!st)
255  return AVERROR(ENOMEM);
256  if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
257  return ret;
262  st->codecpar->sample_rate = wc->rate;
264  avpriv_set_pts_info(st, 64, 1, wc->rate);
265  st->start_time = 0;
266  if (wc->header.total_samples != 0xFFFFFFFFu)
267  st->duration = wc->header.total_samples;
268 
269  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
270  int64_t cur = avio_tell(s->pb);
272  if (av_dict_count(s->metadata) == 0)
273  ff_id3v1_read(s);
274  avio_seek(s->pb, cur, SEEK_SET);
275  }
276 
277  return 0;
278 }
279 
281 {
282  WVContext *wc = s->priv_data;
283  int ret;
284  int off;
285  int64_t pos;
286  uint32_t block_samples;
287 
288  if (avio_feof(s->pb))
289  return AVERROR_EOF;
290  if (wc->block_parsed) {
291  if ((ret = wv_read_block_header(s, s->pb)) < 0)
292  return ret;
293  }
294 
295  pos = wc->pos;
296  if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
297  return ret;
298  memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
300  if (ret < 0) {
301  return ret;
302  }
303  while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
304  if ((ret = wv_read_block_header(s, s->pb)) < 0) {
305  return ret;
306  }
307 
308  off = pkt->size;
309  if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
310  return ret;
311  }
312  memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
313 
314  ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
315  if (ret != wc->header.blocksize) {
316  return (ret < 0) ? ret : AVERROR_EOF;
317  }
318  }
319  pkt->stream_index = 0;
320  pkt->pos = pos;
321  wc->block_parsed = 1;
322  pkt->pts = wc->header.block_idx;
323  block_samples = wc->header.samples;
324  if (block_samples > INT32_MAX)
326  "Too many samples in block: %"PRIu32"\n", block_samples);
327  else
328  pkt->duration = block_samples;
329 
330  return 0;
331 }
332 
334  .p.name = "wv",
335  .p.long_name = NULL_IF_CONFIG_SMALL("WavPack"),
336  .p.flags = AVFMT_GENERIC_INDEX,
337  .priv_data_size = sizeof(WVContext),
338  .read_probe = wv_probe,
341 };
flags
const SwsFlags flags[]
Definition: swscale.c:61
WvHeader::total_samples
uint32_t total_samples
Definition: wv.h:37
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
WV_FLOAT
@ WV_FLOAT
Definition: wvdec.c:39
WV_MCEND
@ WV_MCEND
Definition: wvdec.c:44
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
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
WV_BLOCK_LIMIT
#define WV_BLOCK_LIMIT
Definition: wv.h:32
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
av_dict_count
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:37
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:217
wv_read_header
static int wv_read_header(AVFormatContext *s)
Definition: wvdec.c:235
int64_t
long long int64_t
Definition: coverity.c:34
av_grow_packet
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: packet.c:121
wv_read_packet
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wvdec.c:280
apetag.h
WvHeader::samples
uint32_t samples
Definition: wv.h:39
AVPacket::data
uint8_t * data
Definition: packet.h:588
ff_id3v1_read
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:278
WV_MCINIT
@ WV_MCINIT
Definition: wvdec.c:43
WV_MONO
@ WV_MONO
Definition: wvdec.c:34
WV_HYBRID
@ WV_HYBRID
Definition: wvdec.c:35
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:606
WvHeader
Definition: wv.h:34
id3v1.h
wv_probe
static int wv_probe(const AVProbeData *p)
Definition: wvdec.c:65
ff_ape_parse_tag
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:110
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
WVContext::multichannel
int multichannel
Definition: wvdec.c:58
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: avformat.c:777
WV_DSD
@ WV_DSD
Definition: wvdec.c:45
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
WvHeader::block_idx
uint32_t block_idx
Definition: wv.h:38
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:479
WvHeader::blocksize
uint32_t blocksize
Definition: wv.h:35
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:803
WV_HSHAPE
@ WV_HSHAPE
Definition: wvdec.c:38
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:717
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:218
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
WVContext::pos
int64_t pos
Definition: wvdec.c:60
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:549
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:201
av_channel_layout_from_mask
int av_channel_layout_from_mask(AVChannelLayout *channel_layout, uint64_t mask)
Initialize a native channel layout from a bitmask indicating which channels are present.
Definition: channel_layout.c:252
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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
WV_FLAG_FINAL_BLOCK
#define WV_FLAG_FINAL_BLOCK
Definition: wv.h:29
WVContext::apetag_start
int64_t apetag_start
Definition: wvdec.c:62
AVFormatContext
Format I/O context.
Definition: avformat.h:1264
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:767
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
WV_FLAGS
WV_FLAGS
Definition: wvdec.c:33
WV_HBR
@ WV_HBR
Definition: wvdec.c:41
WV_HEADER_SIZE
#define WV_HEADER_SIZE
Definition: wavpack.h:33
WV_JOINT
@ WV_JOINT
Definition: wvdec.c:36
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
ff_wv_parse_header
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:30
WvHeader::initial
int initial
Definition: wv.h:43
WVContext::header
WvHeader header
Definition: wvdec.c:55
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:733
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
WvHeader::flags
uint32_t flags
Definition: wv.h:40
AVPacket::size
int size
Definition: packet.h:589
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:94
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
size
int size
Definition: twinvq_data.h:10344
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:51
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:408
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:606
WV_CROSSD
@ WV_CROSSD
Definition: wvdec.c:37
read_header
static int read_header(FFV1Context *f, RangeCoder *c)
Definition: ffv1dec.c:498
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:581
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:725
avio_internal.h
demux.h
WVContext::chan
int chan
Definition: wvdec.c:56
WVContext::chmask
uint32_t chmask
Definition: wvdec.c:57
WVContext
Definition: wvdec.c:53
WvHeader::version
uint16_t version
Definition: wv.h:36
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:744
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:236
WVContext::block_header
uint8_t block_header[WV_HEADER_SIZE]
Definition: wvdec.c:54
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
dict.h
id
enum AVCodecID id
Definition: dts2pts.c:549
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
channel_layout.h
WVContext::rate
int rate
Definition: wvdec.c:56
wv.h
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:615
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVPacket::stream_index
int stream_index
Definition: packet.h:590
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:321
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:110
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:565
WV_HBAL
@ WV_HBAL
Definition: wvdec.c:42
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:608
WVContext::bpp
int bpp
Definition: wvdec.c:56
FFInputFormat
Definition: demux.h:47
WvHeader::final
int final
Definition: wv.h:43
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_CODEC_ID_WAVPACK
@ AV_CODEC_ID_WAVPACK
Definition: codec_id.h:484
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ff_wv_demuxer
const FFInputFormat ff_wv_demuxer
Definition: wvdec.c:333
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:665
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:793
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1292
wv_read_block_header
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
Definition: wvdec.c:80
WV_INT32
@ WV_INT32
Definition: wvdec.c:40
WVContext::block_parsed
int block_parsed
Definition: wvdec.c:59
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:237
wv_rates
static const int wv_rates[16]
Definition: wvdec.c:48
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:349