FFmpeg  4.4.9
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 #define WV_MONO 0x00000004U
32 #define WV_HYBRID 0x00000008U
33 #define WV_JOINT 0x00000010U
34 #define WV_CROSSD 0x00000020U
35 #define WV_HSHAPE 0x00000040U
36 #define WV_FLOAT 0x00000080U
37 #define WV_INT32 0x00000100U
38 #define WV_HBR 0x00000200U
39 #define WV_HBAL 0x00000400U
40 #define WV_MCINIT 0x00000800U
41 #define WV_MCEND 0x00001000U
42 #define WV_DSD 0x80000000U
43 
44 static const int wv_rates[16] = {
45  6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
46  32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
47 };
48 
49 typedef struct WVContext {
52  int rate, chan, bpp;
53  uint32_t chmask;
57 
59 } WVContext;
60 
61 static int wv_probe(const AVProbeData *p)
62 {
63  /* check file header */
64  if (p->buf_size <= 32)
65  return 0;
66  if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
67  AV_RL32(&p->buf[4]) >= 24 &&
68  AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
69  AV_RL16(&p->buf[8]) >= 0x402 &&
70  AV_RL16(&p->buf[8]) <= 0x410)
71  return AVPROBE_SCORE_MAX;
72  else
73  return 0;
74 }
75 
77 {
78  WVContext *wc = ctx->priv_data;
79  int ret;
80  int rate, bpp, chan;
81  uint32_t chmask, flags;
82  unsigned rate_x;
83 
84  wc->pos = avio_tell(pb);
85 
86  /* don't return bogus packets with the ape tag data */
87  if (wc->apetag_start && wc->pos >= wc->apetag_start)
88  return AVERROR_EOF;
89 
90  ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
91  if (ret != WV_HEADER_SIZE)
92  return (ret < 0) ? ret : AVERROR_EOF;
93 
94  ret = ff_wv_parse_header(&wc->header, wc->block_header);
95  if (ret < 0) {
96  av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
97  return ret;
98  }
99 
100  if (wc->header.version < 0x402 || wc->header.version > 0x410) {
101  avpriv_report_missing_feature(ctx, "WV version 0x%03X",
102  wc->header.version);
103  return AVERROR_PATCHWELCOME;
104  }
105 
106  /* Blocks with zero samples don't contain actual audio information
107  * and should be ignored */
108  if (!wc->header.samples)
109  return 0;
110  // parse flags
111  flags = wc->header.flags;
112  rate_x = (flags & WV_DSD) ? 4 : 1;
113  bpp = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
114  chan = 1 + !(flags & WV_MONO);
116  rate = wv_rates[(flags >> 23) & 0xF];
117  wc->multichannel = !(wc->header.initial && wc->header.final);
118  if (wc->multichannel) {
119  chan = wc->chan;
120  chmask = wc->chmask;
121  }
122  if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
123  int64_t block_end = avio_tell(pb) + wc->header.blocksize;
124  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
126  "Cannot determine additional parameters\n");
127  return AVERROR_INVALIDDATA;
128  }
129  while (avio_tell(pb) < block_end && !avio_feof(pb)) {
130  int id, size;
131  id = avio_r8(pb);
132  size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
133  size <<= 1;
134  if (id & 0x40)
135  size--;
136  switch (id & 0x3F) {
137  case 0xD:
138  if (size <= 1) {
140  "Insufficient channel information\n");
141  return AVERROR_INVALIDDATA;
142  }
143  chan = avio_r8(pb);
144  switch (size - 2) {
145  case 0:
146  chmask = avio_r8(pb);
147  break;
148  case 1:
149  chmask = avio_rl16(pb);
150  break;
151  case 2:
152  chmask = avio_rl24(pb);
153  break;
154  case 3:
155  chmask = avio_rl32(pb);
156  break;
157  case 4:
158  avio_skip(pb, 1);
159  chan |= (avio_r8(pb) & 0xF) << 8;
160  chan += 1;
161  chmask = avio_rl24(pb);
162  break;
163  case 5:
164  avio_skip(pb, 1);
165  chan |= (avio_r8(pb) & 0xF) << 8;
166  chan += 1;
167  chmask = avio_rl32(pb);
168  break;
169  default:
171  "Invalid channel info size %d\n", size);
172  return AVERROR_INVALIDDATA;
173  }
174  break;
175  case 0xE:
176  if (size <= 1) {
178  "Invalid DSD block\n");
179  return AVERROR_INVALIDDATA;
180  }
181  rate_x = 1U << (avio_r8(pb) & 0x1f);
182  if (size)
183  avio_skip(pb, size-1);
184  break;
185  case 0x27:
186  rate = avio_rl24(pb);
187  break;
188  default:
189  avio_skip(pb, size);
190  }
191  if (id & 0x40)
192  avio_skip(pb, 1);
193  }
194  if (rate == -1 || rate * (uint64_t)rate_x >= INT_MAX) {
196  "Cannot determine custom sampling rate\n");
197  return AVERROR_INVALIDDATA;
198  }
199  avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
200  }
201  if (!wc->bpp)
202  wc->bpp = bpp;
203  if (!wc->chan)
204  wc->chan = chan;
205  if (!wc->chmask)
206  wc->chmask = chmask;
207  if (!wc->rate)
208  wc->rate = rate * rate_x;
209 
210  if (flags && bpp != wc->bpp) {
212  "Bits per sample differ, this block: %i, header block: %i\n",
213  bpp, wc->bpp);
214  return AVERROR_INVALIDDATA;
215  }
216  if (flags && !wc->multichannel && chan != wc->chan) {
218  "Channels differ, this block: %i, header block: %i\n",
219  chan, wc->chan);
220  return AVERROR_INVALIDDATA;
221  }
222  if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
224  "Sampling rate differ, this block: %i, header block: %i\n",
225  rate * rate_x, wc->rate);
226  return AVERROR_INVALIDDATA;
227  }
228  return 0;
229 }
230 
232 {
233  AVIOContext *pb = s->pb;
234  WVContext *wc = s->priv_data;
235  AVStream *st;
236  int ret;
237 
238  wc->block_parsed = 0;
239  for (;;) {
240  if ((ret = wv_read_block_header(s, pb)) < 0)
241  return ret;
242  if (!wc->header.samples)
243  avio_skip(pb, wc->header.blocksize);
244  else
245  break;
246  }
247 
248  /* now we are ready: build format streams */
249  st = avformat_new_stream(s, NULL);
250  if (!st)
251  return AVERROR(ENOMEM);
252  if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
253  return ret;
257  st->codecpar->channels = wc->chan;
258  st->codecpar->channel_layout = wc->chmask;
259  st->codecpar->sample_rate = wc->rate;
261  avpriv_set_pts_info(st, 64, 1, wc->rate);
262  st->start_time = 0;
263  if (wc->header.total_samples != 0xFFFFFFFFu)
264  st->duration = wc->header.total_samples;
265 
266  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
267  int64_t cur = avio_tell(s->pb);
269  if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
270  ff_id3v1_read(s);
271  avio_seek(s->pb, cur, SEEK_SET);
272  }
273 
274  return 0;
275 }
276 
278 {
279  WVContext *wc = s->priv_data;
280  int ret;
281  int off;
282  int64_t pos;
283  uint32_t block_samples;
284 
285  if (avio_feof(s->pb))
286  return AVERROR_EOF;
287  if (wc->block_parsed) {
288  if ((ret = wv_read_block_header(s, s->pb)) < 0)
289  return ret;
290  }
291 
292  pos = wc->pos;
293  if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
294  return ret;
295  memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
296  ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
297  if (ret != wc->header.blocksize) {
298  return AVERROR(EIO);
299  }
300  while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
301  if ((ret = wv_read_block_header(s, s->pb)) < 0) {
302  return ret;
303  }
304 
305  off = pkt->size;
306  if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
307  return ret;
308  }
309  memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
310 
311  ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
312  if (ret != wc->header.blocksize) {
313  return (ret < 0) ? ret : AVERROR_EOF;
314  }
315  }
316  pkt->stream_index = 0;
317  pkt->pos = pos;
318  wc->block_parsed = 1;
319  pkt->pts = wc->header.block_idx;
320  block_samples = wc->header.samples;
321  if (block_samples > INT32_MAX)
323  "Too many samples in block: %"PRIu32"\n", block_samples);
324  else
325  pkt->duration = block_samples;
326 
327  return 0;
328 }
329 
331  .name = "wv",
332  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
333  .priv_data_size = sizeof(WVContext),
334  .read_probe = wv_probe,
338 };
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:118
uint8_t
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:260
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:371
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:750
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:344
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:645
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:766
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:758
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:636
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AV_RL16
Definition: intreadwrite.h:42
#define AV_RL32
Definition: intreadwrite.h:146
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
audio channel layout utility functions
#define MKTAG(a, b, c, d)
Definition: common.h:478
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
Public dictionary API.
enum AVCodecID id
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:555
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
@ AV_CODEC_ID_WAVPACK
Definition: codec_id.h:449
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:122
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4508
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
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
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:279
#define AV_WL16(p, v)
Definition: intreadwrite.h:412
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:4944
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
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
unsigned int pos
Definition: spdifenc.c:412
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: codec_par.h:102
int channels
Audio only.
Definition: codec_par.h:166
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int sample_rate
Audio only.
Definition: codec_par.h:170
Format I/O context.
Definition: avformat.h:1232
void * priv_data
Format private data.
Definition: avformat.h:1260
Bytestream IO Context.
Definition: avio.h:161
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
This structure stores compressed data.
Definition: packet.h:346
int stream_index
Definition: packet.h:371
int size
Definition: packet.h:370
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
uint8_t * data
Definition: packet.h:369
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
Stream structure.
Definition: avformat.h:873
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:912
int64_t pos
Definition: wvdec.c:56
uint32_t chmask
Definition: wvdec.c:53
int multichannel
Definition: wvdec.c:54
int block_parsed
Definition: wvdec.c:55
int64_t apetag_start
Definition: wvdec.c:58
int chan
Definition: wvdec.c:52
WvHeader header
Definition: wvdec.c:51
int rate
Definition: wvdec.c:52
uint8_t block_header[WV_HEADER_SIZE]
Definition: wvdec.c:50
int bpp
Definition: wvdec.c:52
Definition: wv.h:34
uint32_t total_samples
Definition: wv.h:37
uint32_t block_idx
Definition: wv.h:38
int final
Definition: wv.h:43
int initial
Definition: wv.h:43
uint32_t samples
Definition: wv.h:39
uint32_t flags
Definition: wv.h:40
uint16_t version
Definition: wv.h:36
uint32_t blocksize
Definition: wv.h:35
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59
AVFormatContext * ctx
Definition: movenc.c:48
int size
#define WV_HEADER_SIZE
Definition: wavpack.h:30
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
#define WV_FLAG_FINAL_BLOCK
Definition: wv.h:29
#define WV_BLOCK_LIMIT
Definition: wv.h:32
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wvdec.c:277
#define WV_MONO
Definition: wvdec.c:31
#define WV_DSD
Definition: wvdec.c:42
static const int wv_rates[16]
Definition: wvdec.c:44
static int wv_read_header(AVFormatContext *s)
Definition: wvdec.c:231
AVInputFormat ff_wv_demuxer
Definition: wvdec.c:330
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
Definition: wvdec.c:76
static int wv_probe(const AVProbeData *p)
Definition: wvdec.c:61