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 "internal.h"
27 #include "apetag.h"
28 #include "id3v1.h"
29 #include "wv.h"
30 
31 enum WV_FLAGS {
32  WV_MONO = 0x0004,
33  WV_HYBRID = 0x0008,
34  WV_JOINT = 0x0010,
35  WV_CROSSD = 0x0020,
36  WV_HSHAPE = 0x0040,
37  WV_FLOAT = 0x0080,
38  WV_INT32 = 0x0100,
39  WV_HBR = 0x0200,
40  WV_HBAL = 0x0400,
41  WV_MCINIT = 0x0800,
42  WV_MCEND = 0x1000,
43  WV_DSD = 0x80000000,
44 };
45 
46 static const int wv_rates[16] = {
47  6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
48  32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
49 };
50 
51 typedef struct WVContext {
54  int rate, chan, bpp;
55  uint32_t chmask;
58  int64_t pos;
59 
60  int64_t apetag_start;
61 } WVContext;
62 
63 static int wv_probe(const AVProbeData *p)
64 {
65  /* check file header */
66  if (p->buf_size <= 32)
67  return 0;
68  if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
69  AV_RL32(&p->buf[4]) >= 24 &&
70  AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
71  AV_RL16(&p->buf[8]) >= 0x402 &&
72  AV_RL16(&p->buf[8]) <= 0x410)
73  return AVPROBE_SCORE_MAX;
74  else
75  return 0;
76 }
77 
79 {
80  WVContext *wc = ctx->priv_data;
81  int ret;
82  int rate, bpp, chan;
83  uint32_t chmask, flags;
84 
85  wc->pos = avio_tell(pb);
86 
87  /* don't return bogus packets with the ape tag data */
88  if (wc->apetag_start && wc->pos >= wc->apetag_start)
89  return AVERROR_EOF;
90 
92  if (ret != WV_HEADER_SIZE)
93  return (ret < 0) ? ret : AVERROR_EOF;
94 
96  if (ret < 0) {
97  av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
98  return ret;
99  }
100 
101  if (wc->header.flags & WV_DSD) {
103  return AVERROR_PATCHWELCOME;
104  }
105 
106  if (wc->header.version < 0x402 || wc->header.version > 0x410) {
107  avpriv_report_missing_feature(ctx, "WV version 0x%03X",
108  wc->header.version);
109  return AVERROR_PATCHWELCOME;
110  }
111 
112  /* Blocks with zero samples don't contain actual audio information
113  * and should be ignored */
114  if (!wc->header.samples)
115  return 0;
116  // parse flags
117  flags = wc->header.flags;
118  bpp = ((flags & 3) + 1) << 3;
119  chan = 1 + !(flags & WV_MONO);
121  rate = wv_rates[(flags >> 23) & 0xF];
122  wc->multichannel = !(wc->header.initial && wc->header.final);
123  if (wc->multichannel) {
124  chan = wc->chan;
125  chmask = wc->chmask;
126  }
127  if ((rate == -1 || !chan) && !wc->block_parsed) {
128  int64_t block_end = avio_tell(pb) + wc->header.blocksize;
129  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
131  "Cannot determine additional parameters\n");
132  return AVERROR_INVALIDDATA;
133  }
134  while (avio_tell(pb) < block_end && !avio_feof(pb)) {
135  int id, size;
136  id = avio_r8(pb);
137  size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
138  size <<= 1;
139  if (id & 0x40)
140  size--;
141  switch (id & 0x3F) {
142  case 0xD:
143  if (size <= 1) {
145  "Insufficient channel information\n");
146  return AVERROR_INVALIDDATA;
147  }
148  chan = avio_r8(pb);
149  switch (size - 2) {
150  case 0:
151  chmask = avio_r8(pb);
152  break;
153  case 1:
154  chmask = avio_rl16(pb);
155  break;
156  case 2:
157  chmask = avio_rl24(pb);
158  break;
159  case 3:
160  chmask = avio_rl32(pb);
161  break;
162  case 4:
163  avio_skip(pb, 1);
164  chan |= (avio_r8(pb) & 0xF) << 8;
165  chan += 1;
166  chmask = avio_rl24(pb);
167  break;
168  case 5:
169  avio_skip(pb, 1);
170  chan |= (avio_r8(pb) & 0xF) << 8;
171  chan += 1;
172  chmask = avio_rl32(pb);
173  break;
174  default:
176  "Invalid channel info size %d\n", size);
177  return AVERROR_INVALIDDATA;
178  }
179  break;
180  case 0x27:
181  rate = avio_rl24(pb);
182  break;
183  default:
184  avio_skip(pb, size);
185  }
186  if (id & 0x40)
187  avio_skip(pb, 1);
188  }
189  if (rate == -1) {
191  "Cannot determine custom sampling rate\n");
192  return AVERROR_INVALIDDATA;
193  }
194  avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
195  }
196  if (!wc->bpp)
197  wc->bpp = bpp;
198  if (!wc->chan)
199  wc->chan = chan;
200  if (!wc->chmask)
201  wc->chmask = chmask;
202  if (!wc->rate)
203  wc->rate = rate;
204 
205  if (flags && bpp != wc->bpp) {
207  "Bits per sample differ, this block: %i, header block: %i\n",
208  bpp, wc->bpp);
209  return AVERROR_INVALIDDATA;
210  }
211  if (flags && !wc->multichannel && chan != wc->chan) {
213  "Channels differ, this block: %i, header block: %i\n",
214  chan, wc->chan);
215  return AVERROR_INVALIDDATA;
216  }
217  if (flags && rate != -1 && rate != wc->rate) {
219  "Sampling rate differ, this block: %i, header block: %i\n",
220  rate, wc->rate);
221  return AVERROR_INVALIDDATA;
222  }
223  return 0;
224 }
225 
227 {
228  AVIOContext *pb = s->pb;
229  WVContext *wc = s->priv_data;
230  AVStream *st;
231  int ret;
232 
233  wc->block_parsed = 0;
234  for (;;) {
235  if ((ret = wv_read_block_header(s, pb)) < 0)
236  return ret;
237  if (!wc->header.samples)
238  avio_skip(pb, wc->header.blocksize);
239  else
240  break;
241  }
242 
243  /* now we are ready: build format streams */
244  st = avformat_new_stream(s, NULL);
245  if (!st)
246  return AVERROR(ENOMEM);
249  st->codecpar->channels = wc->chan;
250  st->codecpar->channel_layout = wc->chmask;
251  st->codecpar->sample_rate = wc->rate;
253  avpriv_set_pts_info(st, 64, 1, wc->rate);
254  st->start_time = 0;
255  if (wc->header.total_samples != 0xFFFFFFFFu)
256  st->duration = wc->header.total_samples;
257 
258  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
259  int64_t cur = avio_tell(s->pb);
261  if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
262  ff_id3v1_read(s);
263  avio_seek(s->pb, cur, SEEK_SET);
264  }
265 
266  return 0;
267 }
268 
270 {
271  WVContext *wc = s->priv_data;
272  int ret;
273  int off;
274  int64_t pos;
275  uint32_t block_samples;
276 
277  if (avio_feof(s->pb))
278  return AVERROR_EOF;
279  if (wc->block_parsed) {
280  if ((ret = wv_read_block_header(s, s->pb)) < 0)
281  return ret;
282  }
283 
284  pos = wc->pos;
286  return AVERROR(ENOMEM);
287  memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
289  if (ret != wc->header.blocksize) {
291  return AVERROR(EIO);
292  }
293  while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
294  if ((ret = wv_read_block_header(s, s->pb)) < 0) {
296  return ret;
297  }
298 
299  off = pkt->size;
300  if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
302  return ret;
303  }
304  memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
305 
306  ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
307  if (ret != wc->header.blocksize) {
309  return (ret < 0) ? ret : AVERROR_EOF;
310  }
311  }
312  pkt->stream_index = 0;
313  pkt->pos = pos;
314  wc->block_parsed = 1;
315  pkt->pts = wc->header.block_idx;
316  block_samples = wc->header.samples;
317  if (block_samples > INT32_MAX)
319  "Too many samples in block: %"PRIu32"\n", block_samples);
320  else
321  pkt->duration = block_samples;
322 
323  return 0;
324 }
325 
327  .name = "wv",
328  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
329  .priv_data_size = sizeof(WVContext),
330  .read_probe = wv_probe,
334 };
WvHeader::total_samples
uint32_t total_samples
Definition: wv.h:37
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
WV_FLOAT
@ WV_FLOAT
Definition: wvdec.c:37
WV_MCEND
@ WV_MCEND
Definition: wvdec.c:42
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:4480
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3953
WV_BLOCK_LIMIT
#define WV_BLOCK_LIMIT
Definition: wv.h:32
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:366
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:85
wv_read_header
static int wv_read_header(AVFormatContext *s)
Definition: wvdec.c:226
av_grow_packet
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
wv_read_packet
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wvdec.c:269
apetag.h
WvHeader::samples
uint32_t samples
Definition: wv.h:39
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
ff_id3v1_read
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:235
WV_MCINIT
@ WV_MCINIT
Definition: wvdec.c:41
WV_MONO
@ WV_MONO
Definition: wvdec.c:32
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
WV_HYBRID
@ WV_HYBRID
Definition: wvdec.c:33
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1495
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:449
WvHeader
Definition: wv.h:34
id3v1.h
wv_probe
static int wv_probe(const AVProbeData *p)
Definition: wvdec.c:63
ff_ape_parse_tag
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:118
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:458
WVContext::multichannel
int multichannel
Definition: wvdec.c:56
AVCodecParameters::channels
int channels
Audio only.
Definition: avcodec.h:4063
WV_DSD
@ WV_DSD
Definition: wvdec.c:43
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
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:468
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:919
WV_HSHAPE
@ WV_HSHAPE
Definition: wvdec.c:36
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVInputFormat
Definition: avformat.h:640
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
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: avpacket.c:86
WVContext::pos
int64_t pos
Definition: wvdec.c:58
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:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ctx
AVFormatContext * ctx
Definition: movenc.c:48
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:90
WV_FLAG_FINAL_BLOCK
#define WV_FLAG_FINAL_BLOCK
Definition: wv.h:29
WVContext::apetag_start
int64_t apetag_start
Definition: wvdec.c:60
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1017
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
WV_FLAGS
WV_FLAGS
Definition: wvdec.c:31
WV_HBR
@ WV_HBR
Definition: wvdec.c:39
WV_HEADER_SIZE
#define WV_HEADER_SIZE
Definition: wavpack.h:30
WV_JOINT
@ WV_JOINT
Definition: wvdec.c:34
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: avcodec.h:4067
ff_wv_parse_header
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
WvHeader::initial
int initial
Definition: wv.h:43
WVContext::header
WvHeader header
Definition: wvdec.c:53
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
WvHeader::flags
uint32_t flags
Definition: wv.h:40
AVPacket::size
int size
Definition: avcodec.h:1478
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:188
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:329
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
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:4910
size
int size
Definition: twinvq_data.h:11134
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.
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
WV_CROSSD
@ WV_CROSSD
Definition: wvdec.c:35
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
avio_rl24
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:761
uint8_t
uint8_t
Definition: audio_convert.c:194
WVContext::chan
int chan
Definition: wvdec.c:54
WVContext::chmask
uint32_t chmask
Definition: wvdec.c:55
WVContext
Definition: wvdec.c:51
WvHeader::version
uint16_t version
Definition: wv.h:36
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:870
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:246
WVContext::block_header
uint8_t block_header[WV_HEADER_SIZE]
Definition: wvdec.c:52
avformat.h
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:88
channel_layout.h
WVContext::rate
int rate
Definition: wvdec.c:54
wv.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
ff_wv_demuxer
AVInputFormat ff_wv_demuxer
Definition: wvdec.c:326
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVCodecParameters::bits_per_coded_sample
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:3999
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3957
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
WV_HBAL
@ WV_HBAL
Definition: wvdec.c:40
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1497
WVContext::bpp
int bpp
Definition: wvdec.c:54
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4059
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
WvHeader::final
int final
Definition: wv.h:43
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_CODEC_ID_WAVPACK
@ AV_CODEC_ID_WAVPACK
Definition: avcodec.h:589
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:909
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1370
wv_read_block_header
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
Definition: wvdec.c:78
WV_INT32
@ WV_INT32
Definition: wvdec.c:38
WVContext::block_parsed
int block_parsed
Definition: wvdec.c:57
wv_rates
static const int wv_rates[16]
Definition: wvdec.c:46
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:358