FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 };
44 
45 static const int wv_rates[16] = {
46  6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
47  32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
48 };
49 
50 typedef struct WVContext {
53  int rate, chan, bpp;
54  uint32_t chmask;
57  int64_t pos;
58 
59  int64_t apetag_start;
60 } WVContext;
61 
62 static int wv_probe(AVProbeData *p)
63 {
64  /* check file header */
65  if (p->buf_size <= 32)
66  return 0;
67  if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
68  AV_RL32(&p->buf[4]) >= 24 &&
69  AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
70  AV_RL16(&p->buf[8]) >= 0x402 &&
71  AV_RL16(&p->buf[8]) <= 0x410)
72  return AVPROBE_SCORE_MAX;
73  else
74  return 0;
75 }
76 
78 {
79  WVContext *wc = ctx->priv_data;
80  int ret;
81  int rate, bpp, chan;
82  uint32_t chmask, flags;
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  bpp = ((flags & 3) + 1) << 3;
113  chan = 1 + !(flags & WV_MONO);
114  chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
115  rate = wv_rates[(flags >> 23) & 0xF];
116  wc->multichannel = !(wc->header.initial && wc->header.final);
117  if (wc->multichannel) {
118  chan = wc->chan;
119  chmask = wc->chmask;
120  }
121  if ((rate == -1 || !chan) && !wc->block_parsed) {
122  int64_t block_end = avio_tell(pb) + wc->header.blocksize;
123  if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
124  av_log(ctx, AV_LOG_ERROR,
125  "Cannot determine additional parameters\n");
126  return AVERROR_INVALIDDATA;
127  }
128  while (avio_tell(pb) < block_end && !avio_feof(pb)) {
129  int id, size;
130  id = avio_r8(pb);
131  size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
132  size <<= 1;
133  if (id & 0x40)
134  size--;
135  switch (id & 0x3F) {
136  case 0xD:
137  if (size <= 1) {
138  av_log(ctx, AV_LOG_ERROR,
139  "Insufficient channel information\n");
140  return AVERROR_INVALIDDATA;
141  }
142  chan = avio_r8(pb);
143  switch (size - 2) {
144  case 0:
145  chmask = avio_r8(pb);
146  break;
147  case 1:
148  chmask = avio_rl16(pb);
149  break;
150  case 2:
151  chmask = avio_rl24(pb);
152  break;
153  case 3:
154  chmask = avio_rl32(pb);
155  break;
156  case 5:
157  avio_skip(pb, 1);
158  chan |= (avio_r8(pb) & 0xF) << 8;
159  chmask = avio_rl24(pb);
160  break;
161  default:
162  av_log(ctx, AV_LOG_ERROR,
163  "Invalid channel info size %d\n", size);
164  return AVERROR_INVALIDDATA;
165  }
166  break;
167  case 0x27:
168  rate = avio_rl24(pb);
169  break;
170  default:
171  avio_skip(pb, size);
172  }
173  if (id & 0x40)
174  avio_skip(pb, 1);
175  }
176  if (rate == -1) {
177  av_log(ctx, AV_LOG_ERROR,
178  "Cannot determine custom sampling rate\n");
179  return AVERROR_INVALIDDATA;
180  }
181  avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
182  }
183  if (!wc->bpp)
184  wc->bpp = bpp;
185  if (!wc->chan)
186  wc->chan = chan;
187  if (!wc->chmask)
188  wc->chmask = chmask;
189  if (!wc->rate)
190  wc->rate = rate;
191 
192  if (flags && bpp != wc->bpp) {
193  av_log(ctx, AV_LOG_ERROR,
194  "Bits per sample differ, this block: %i, header block: %i\n",
195  bpp, wc->bpp);
196  return AVERROR_INVALIDDATA;
197  }
198  if (flags && !wc->multichannel && chan != wc->chan) {
199  av_log(ctx, AV_LOG_ERROR,
200  "Channels differ, this block: %i, header block: %i\n",
201  chan, wc->chan);
202  return AVERROR_INVALIDDATA;
203  }
204  if (flags && rate != -1 && rate != wc->rate) {
205  av_log(ctx, AV_LOG_ERROR,
206  "Sampling rate differ, this block: %i, header block: %i\n",
207  rate, wc->rate);
208  return AVERROR_INVALIDDATA;
209  }
210  return 0;
211 }
212 
214 {
215  AVIOContext *pb = s->pb;
216  WVContext *wc = s->priv_data;
217  AVStream *st;
218  int ret;
219 
220  wc->block_parsed = 0;
221  for (;;) {
222  if ((ret = wv_read_block_header(s, pb)) < 0)
223  return ret;
224  if (!wc->header.samples)
225  avio_skip(pb, wc->header.blocksize);
226  else
227  break;
228  }
229 
230  /* now we are ready: build format streams */
231  st = avformat_new_stream(s, NULL);
232  if (!st)
233  return AVERROR(ENOMEM);
236  st->codecpar->channels = wc->chan;
237  st->codecpar->channel_layout = wc->chmask;
238  st->codecpar->sample_rate = wc->rate;
240  avpriv_set_pts_info(st, 64, 1, wc->rate);
241  st->start_time = 0;
242  if (wc->header.total_samples != 0xFFFFFFFFu)
243  st->duration = wc->header.total_samples;
244 
245  if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
246  int64_t cur = avio_tell(s->pb);
249  ff_id3v1_read(s);
250  avio_seek(s->pb, cur, SEEK_SET);
251  }
252 
253  return 0;
254 }
255 
257 {
258  WVContext *wc = s->priv_data;
259  int ret;
260  int off;
261  int64_t pos;
262  uint32_t block_samples;
263 
264  if (avio_feof(s->pb))
265  return AVERROR_EOF;
266  if (wc->block_parsed) {
267  if ((ret = wv_read_block_header(s, s->pb)) < 0)
268  return ret;
269  }
270 
271  pos = wc->pos;
272  if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
273  return AVERROR(ENOMEM);
274  memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
275  ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
276  if (ret != wc->header.blocksize) {
277  av_packet_unref(pkt);
278  return AVERROR(EIO);
279  }
280  while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
281  if ((ret = wv_read_block_header(s, s->pb)) < 0) {
282  av_packet_unref(pkt);
283  return ret;
284  }
285 
286  off = pkt->size;
287  if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
288  av_packet_unref(pkt);
289  return ret;
290  }
291  memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
292 
293  ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
294  if (ret != wc->header.blocksize) {
295  av_packet_unref(pkt);
296  return (ret < 0) ? ret : AVERROR_EOF;
297  }
298  }
299  pkt->stream_index = 0;
300  pkt->pos = pos;
301  wc->block_parsed = 1;
302  pkt->pts = wc->header.block_idx;
303  block_samples = wc->header.samples;
304  if (block_samples > INT32_MAX)
306  "Too many samples in block: %"PRIu32"\n", block_samples);
307  else
308  pkt->duration = block_samples;
309 
310  return 0;
311 }
312 
314  .name = "wv",
315  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
316  .priv_data_size = sizeof(WVContext),
317  .read_probe = wv_probe,
321 };
int64_t pos
Definition: wvdec.c:57
#define NULL
Definition: coverity.c:32
int block_parsed
Definition: wvdec.c:56
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t block_header[WV_HEADER_SIZE]
Definition: wvdec.c:51
uint32_t samples
Definition: wv.h:39
int initial
Definition: wv.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1699
int rate
Definition: wvdec.c:53
Definition: wvdec.c:32
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:4737
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:235
int size
Definition: avcodec.h:1680
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
#define WV_BLOCK_LIMIT
Definition: wv.h:32
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wvdec.c:256
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
int multichannel
Definition: wvdec.c:55
Format I/O context.
Definition: avformat.h:1349
Public dictionary API.
static int wv_probe(AVProbeData *p)
Definition: wvdec.c:62
uint8_t
uint32_t flags
Definition: wv.h:40
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
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
uint8_t * data
Definition: avcodec.h:1679
static int flags
Definition: log.c:57
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define WV_HEADER_SIZE
Definition: wavpack.h:30
ptrdiff_t size
Definition: opengl_enc.c:101
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:556
Definition: wv.h:34
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4254
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
WV_FLAGS
Definition: wvdec.c:31
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
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1567
AVInputFormat ff_wv_demuxer
Definition: wvdec.c:313
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
int final
Definition: wv.h:43
int64_t ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:118
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
uint16_t version
Definition: wv.h:36
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:261
audio channel layout utility functions
Definition: wvdec.c:39
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
static const int wv_rates[16]
Definition: wvdec.c:45
int ff_wv_parse_header(WvHeader *wv, const uint8_t *data)
Parse a WavPack block header.
Definition: wv.c:29
AVFormatContext * ctx
Definition: movenc.c:48
int bpp
Definition: wvdec.c:53
int64_t apetag_start
Definition: wvdec.c:59
WvHeader header
Definition: wvdec.c:52
Definition: wvdec.c:34
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:528
Stream structure.
Definition: avformat.h:889
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:618
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:487
uint32_t total_samples
Definition: wv.h:37
int chan
Definition: wvdec.c:53
uint32_t chmask
Definition: wvdec.c:54
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint32_t block_idx
Definition: wv.h:38
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:743
Main libavformat public API header.
Definition: wvdec.c:40
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:936
static int wv_read_header(AVFormatContext *s)
Definition: wvdec.c:213
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
Definition: wvdec.c:38
Definition: wvdec.c:37
void * priv_data
Format private data.
Definition: avformat.h:1377
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition: avcodec.h:4194
int channels
Audio only.
Definition: avcodec.h:4258
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
AVCodecParameters * codecpar
Definition: avformat.h:1252
Definition: wvdec.c:42
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:751
int stream_index
Definition: avcodec.h:1681
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
Definition: wvdec.c:77
#define AV_CH_LAYOUT_MONO
#define MKTAG(a, b, c, d)
Definition: common.h:342
enum AVCodecID id
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:87
This structure stores compressed data.
Definition: avcodec.h:1656
#define WV_FLAG_FINAL_BLOCK
Definition: wv.h:29
uint32_t blocksize
Definition: wv.h:35
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1672