wv.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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "libavutil/audioconvert.h"
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 
30 // specs say that maximum block size is 1Mb
31 #define WV_BLOCK_LIMIT 1047576
32 
33 #define WV_EXTRA_SIZE 12
34 
35 #define WV_START_BLOCK 0x0800
36 #define WV_END_BLOCK 0x1000
37 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
38 
39 enum WV_FLAGS{
40  WV_MONO = 0x0004,
41  WV_HYBRID = 0x0008,
42  WV_JOINT = 0x0010,
43  WV_CROSSD = 0x0020,
44  WV_HSHAPE = 0x0040,
45  WV_FLOAT = 0x0080,
46  WV_INT32 = 0x0100,
47  WV_HBR = 0x0200,
48  WV_HBAL = 0x0400,
49  WV_MCINIT = 0x0800,
50  WV_MCEND = 0x1000,
51 };
52 
53 static const int wv_rates[16] = {
54  6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
55  32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
56 };
57 
58 typedef struct{
59  uint32_t blksize, flags;
60  int rate, chan, bpp;
61  uint32_t chmask;
62  uint32_t samples, soff;
65  uint8_t extra[WV_EXTRA_SIZE];
66  int64_t pos;
67 }WVContext;
68 
69 static int wv_probe(AVProbeData *p)
70 {
71  /* check file header */
72  if (p->buf_size <= 32)
73  return 0;
74  if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
75  p->buf[2] == 'p' && p->buf[3] == 'k')
76  return AVPROBE_SCORE_MAX;
77  else
78  return 0;
79 }
80 
82 {
83  WVContext *wc = ctx->priv_data;
84  uint32_t tag, ver;
85  int size;
86  int rate, bpp, chan;
87  uint32_t chmask;
88 
89  wc->pos = avio_tell(pb);
90  if(!append){
91  tag = avio_rl32(pb);
92  if (tag != MKTAG('w', 'v', 'p', 'k'))
93  return -1;
94  size = avio_rl32(pb);
95  if(size < 24 || size > WV_BLOCK_LIMIT){
96  av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
97  return -1;
98  }
99  wc->blksize = size;
100  ver = avio_rl16(pb);
101  if(ver < 0x402 || ver > 0x410){
102  av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
103  return -1;
104  }
105  avio_r8(pb); // track no
106  avio_r8(pb); // track sub index
107  wc->samples = avio_rl32(pb); // total samples in file
108  wc->soff = avio_rl32(pb); // offset in samples of current block
109  avio_read(pb, wc->extra, WV_EXTRA_SIZE);
110  }else{
111  size = wc->blksize;
112  }
113  wc->flags = AV_RL32(wc->extra + 4);
114  // blocks with zero samples don't contain actual audio information and should be ignored
115  if (!AV_RN32(wc->extra))
116  return 0;
117  //parse flags
118  bpp = ((wc->flags & 3) + 1) << 3;
119  chan = 1 + !(wc->flags & WV_MONO);
121  rate = wv_rates[(wc->flags >> 23) & 0xF];
122  wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
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->blksize - 24;
129  if(!pb->seekable){
130  av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
131  return -1;
132  }
133  while(avio_tell(pb) < block_end){
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){
143  av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
144  return -1;
145  }
146  chan = avio_r8(pb);
147  switch(size - 2){
148  case 0:
149  chmask = avio_r8(pb);
150  break;
151  case 1:
152  chmask = avio_rl16(pb);
153  break;
154  case 2:
155  chmask = avio_rl24(pb);
156  break;
157  case 3:
158  chmask = avio_rl32(pb);
159  break;
160  case 5:
161  avio_skip(pb, 1);
162  chan |= (avio_r8(pb) & 0xF) << 8;
163  chmask = avio_rl24(pb);
164  break;
165  default:
166  av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
167  return -1;
168  }
169  break;
170  case 0x27:
171  rate = avio_rl24(pb);
172  break;
173  default:
174  avio_skip(pb, size);
175  }
176  if(id&0x40)
177  avio_skip(pb, 1);
178  }
179  if(rate == -1){
180  av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
181  return -1;
182  }
183  avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
184  }
185  if(!wc->bpp) wc->bpp = bpp;
186  if(!wc->chan) wc->chan = chan;
187  if(!wc->chmask) wc->chmask = chmask;
188  if(!wc->rate) wc->rate = rate;
189 
190  if(wc->flags && bpp != wc->bpp){
191  av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
192  return -1;
193  }
194  if(wc->flags && !wc->multichannel && chan != wc->chan){
195  av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
196  return -1;
197  }
198  if(wc->flags && rate != -1 && rate != wc->rate){
199  av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
200  return -1;
201  }
202  wc->blksize = size - 24;
203  return 0;
204 }
205 
207  AVFormatParameters *ap)
208 {
209  AVIOContext *pb = s->pb;
210  WVContext *wc = s->priv_data;
211  AVStream *st;
212 
213  wc->block_parsed = 0;
214  for(;;){
215  if(wv_read_block_header(s, pb, 0) < 0)
216  return -1;
217  if(!AV_RN32(wc->extra))
218  avio_skip(pb, wc->blksize - 24);
219  else
220  break;
221  }
222 
223  /* now we are ready: build format streams */
224  st = avformat_new_stream(s, NULL);
225  if (!st)
226  return -1;
229  st->codec->channels = wc->chan;
230  st->codec->channel_layout = wc->chmask;
231  st->codec->sample_rate = wc->rate;
232  st->codec->bits_per_coded_sample = wc->bpp;
233  avpriv_set_pts_info(st, 64, 1, wc->rate);
234  st->start_time = 0;
235  st->duration = wc->samples;
236 
237  if(s->pb->seekable) {
238  int64_t cur = avio_tell(s->pb);
239  ff_ape_parse_tag(s);
241  ff_id3v1_read(s);
242  avio_seek(s->pb, cur, SEEK_SET);
243  }
244 
245  return 0;
246 }
247 
249  AVPacket *pkt)
250 {
251  WVContext *wc = s->priv_data;
252  int ret;
253  int size, ver, off;
254  int64_t pos;
255 
256  if (s->pb->eof_reached)
257  return AVERROR(EIO);
258  if(wc->block_parsed){
259  if(wv_read_block_header(s, s->pb, 0) < 0)
260  return -1;
261  }
262 
263  pos = wc->pos;
264  off = wc->multichannel ? 4 : 0;
265  if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
266  return AVERROR(ENOMEM);
267  if(wc->multichannel)
268  AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
269  memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
270  ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
271  if(ret != wc->blksize){
272  av_free_packet(pkt);
273  return AVERROR(EIO);
274  }
275  while(!(wc->flags & WV_END_BLOCK)){
276  if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
277  av_free_packet(pkt);
278  return -1;
279  }
280  if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
281  av_free_packet(pkt);
282  return ret;
283  }
284  size = AV_RL32(pkt->data + pkt->size - 4);
285  if(size < 24 || size > WV_BLOCK_LIMIT){
286  av_free_packet(pkt);
287  av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
288  return -1;
289  }
290  wc->blksize = size;
291  ver = avio_rl16(s->pb);
292  if(ver < 0x402 || ver > 0x410){
293  av_free_packet(pkt);
294  av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
295  return -1;
296  }
297  avio_r8(s->pb); // track no
298  avio_r8(s->pb); // track sub index
299  wc->samples = avio_rl32(s->pb); // total samples in file
300  wc->soff = avio_rl32(s->pb); // offset in samples of current block
301  if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
302  av_free_packet(pkt);
303  return ret;
304  }
305  memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
306 
307  if(wv_read_block_header(s, s->pb, 1) < 0){
308  av_free_packet(pkt);
309  return -1;
310  }
311  ret = av_append_packet(s->pb, pkt, wc->blksize);
312  if(ret < 0){
313  av_free_packet(pkt);
314  return ret;
315  }
316  }
317  pkt->stream_index = 0;
318  wc->block_parsed = 1;
319  pkt->pts = wc->soff;
320  av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
321  return 0;
322 }
323 
324 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
325 {
326  AVStream *st = s->streams[stream_index];
327  WVContext *wc = s->priv_data;
328  AVPacket pkt1, *pkt = &pkt1;
329  int ret;
330  int index = av_index_search_timestamp(st, timestamp, flags);
331  int64_t pos, pts;
332 
333  /* if found, seek there */
334  if (index >= 0 &&
335  timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
336  wc->block_parsed = 1;
337  avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
338  return 0;
339  }
340  /* if timestamp is out of bounds, return error */
341  if(timestamp < 0 || timestamp >= s->duration)
342  return -1;
343 
344  pos = avio_tell(s->pb);
345  do{
346  ret = av_read_frame(s, pkt);
347  if (ret < 0){
348  avio_seek(s->pb, pos, SEEK_SET);
349  return -1;
350  }
351  pts = pkt->pts;
352  av_free_packet(pkt);
353  }while(pts < timestamp);
354  return 0;
355 }
356 
358  .name = "wv",
359  .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
360  .priv_data_size = sizeof(WVContext),
361  .read_probe = wv_probe,
365 };
int64_t pos
Definition: wv.c:66
int block_parsed
Definition: wv.c:64
uint32_t soff
Definition: wv.c:62
Bytestream IO Context.
Definition: avio.h:68
int size
static short * samples
Definition: ffmpeg.c:233
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: utils.c:1474
static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: wv.c:324
Definition: wv.c:48
int rate
Definition: wv.c:60
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the pts for a given stream.
Definition: utils.c:3828
int64_t pos
Definition: avformat.h:588
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:148
void ff_id3v1_read(AVFormatContext *s)
Read an ID3v1 tag.
Definition: id3v1.c:227
int size
Definition: avcodec.h:909
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:211
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:818
Definition: wv.c:46
Definition: wv.c:43
#define AV_CH_LAYOUT_STEREO
Definition: audioconvert.h:77
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:27
static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb, int append)
Definition: wv.c:81
Definition: wv.c:49
int multichannel
Definition: wv.c:63
Format I/O context.
Definition: avformat.h:863
Public dictionary API.
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
AVStream ** streams
Definition: avformat.h:908
static uint8_t * append(uint8_t *buf, const uint8_t *src, int size)
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
Definition: wv.c:45
static int flags
Definition: log.c:34
uint32_t tag
Definition: movenc.c:670
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:287
#define WV_BLOCK_LIMIT
Definition: wv.c:31
audio conversion routines
#define AVINDEX_KEYFRAME
Definition: avformat.h:590
AVDictionary * metadata
Definition: avformat.h:1085
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1516
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#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:191
AV_RL32
Definition: bytestream.h:89
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
#define WV_END_BLOCK
Definition: wv.c:36
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
Definition: wv.c:47
int off
Definition: dsputil_bfin.c:28
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2870
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:342
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
uint8_t extra[WV_EXTRA_SIZE]
Definition: wv.c:65
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:122
#define WV_EXTRA_SIZE
Definition: wv.c:33
static int wv_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: wv.c:206
void ff_ape_parse_tag(AVFormatContext *s)
Read and parse an APE tag.
Definition: apetag.c:67
uint32_t blksize
Definition: wv.c:59
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
static int read_probe(AVProbeData *p)
Definition: img2.c:185
int bpp
Definition: wv.c:60
AVInputFormat ff_wv_demuxer
Definition: wv.c:357
#define WV_SINGLE_BLOCK
Definition: wv.c:37
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
Definition: wv.c:58
uint32_t flags
Definition: wv.c:59
enum AVMediaType codec_type
Definition: avcodec.h:1574
int sample_rate
samples per second
Definition: avcodec.h:1456
AVIOContext * pb
Definition: avformat.h:896
WV_FLAGS
Definition: wv.c:39
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
int nb_index_entries
Definition: avformat.h:820
int index
Definition: gxfenc.c:73
int chan
Definition: wv.c:60
uint32_t chmask
Definition: wv.c:61
enum CodecID id
Definition: mxfenc.c:85
static int wv_probe(AVProbeData *p)
Definition: wv.c:69
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
#define AV_RN32(p)
Definition: intreadwrite.h:326
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1264
static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: wv.c:248
Definition: wv.c:50
static const int wv_rates[16]
Definition: wv.c:53
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
Definition: wv.c:40
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:345
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
Definition: wv.c:44
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
Definition: wv.c:41
Definition: wv.c:42
uint32_t samples
Definition: wv.c:62
int eof_reached
true if eof reached
Definition: avio.h:98
int channels
number of audio channels
Definition: avcodec.h:1457
void * priv_data
Format private data.
Definition: avformat.h:883
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:943
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:62
unsigned int avio_rl24(AVIOContext *s)
Definition: aviobuf.c:730
int stream_index
Definition: avcodec.h:910
#define AV_CH_LAYOUT_MONO
Definition: audioconvert.h:76
#define MKTAG(a, b, c, d)
Definition: common.h:231
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:901
enum CodecID codec_id
Definition: avcodec.h:1575