3 * Copyright (c) 2006,2011 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
29 // specs say that maximum block size is 1Mb
30 #define WV_BLOCK_LIMIT 1047576
32 #define WV_EXTRA_SIZE 12
34 #define WV_START_BLOCK 0x0800
35 #define WV_END_BLOCK 0x1000
36 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
52 static const int wv_rates[16] = {
53 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
54 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
58 uint32_t blksize, flags;
61 uint32_t samples, soff;
64 uint8_t extra[WV_EXTRA_SIZE];
68 static int wv_probe(AVProbeData *p)
70 /* check file header */
71 if (p->buf_size <= 32)
73 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
74 p->buf[2] == 'p' && p->buf[3] == 'k')
75 return AVPROBE_SCORE_MAX;
80 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb, int append)
82 WVContext *wc = ctx->priv_data;
88 wc->pos = avio_tell(pb);
91 if (tag != MKTAG('w', 'v', 'p', 'k'))
94 if(size < 24 || size > WV_BLOCK_LIMIT){
95 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
100 if(ver < 0x402 || ver > 0x410){
101 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
104 avio_r8(pb); // track no
105 avio_r8(pb); // track sub index
106 wc->samples = avio_rl32(pb); // total samples in file
107 wc->soff = avio_rl32(pb); // offset in samples of current block
108 avio_read(pb, wc->extra, WV_EXTRA_SIZE);
112 wc->flags = AV_RL32(wc->extra + 4);
113 // blocks with zero samples don't contain actual audio information and should be ignored
114 if (!AV_RN32(wc->extra))
117 bpp = ((wc->flags & 3) + 1) << 3;
118 chan = 1 + !(wc->flags & WV_MONO);
119 chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
120 rate = wv_rates[(wc->flags >> 23) & 0xF];
121 wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
122 if(wc->multichannel){
126 if((rate == -1 || !chan) && !wc->block_parsed){
127 int64_t block_end = avio_tell(pb) + wc->blksize - 24;
129 av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
132 while(avio_tell(pb) < block_end){
135 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
142 av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
148 chmask = avio_r8(pb);
151 chmask = avio_rl16(pb);
154 chmask = avio_rl24(pb);
157 chmask = avio_rl32(pb);
161 chan |= (avio_r8(pb) & 0xF) << 8;
162 chmask = avio_rl24(pb);
165 av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
170 rate = avio_rl24(pb);
179 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
182 avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
184 if(!wc->bpp) wc->bpp = bpp;
185 if(!wc->chan) wc->chan = chan;
186 if(!wc->chmask) wc->chmask = chmask;
187 if(!wc->rate) wc->rate = rate;
189 if(wc->flags && bpp != wc->bpp){
190 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
193 if(wc->flags && !wc->multichannel && chan != wc->chan){
194 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
197 if(wc->flags && rate != -1 && rate != wc->rate){
198 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
201 wc->blksize = size - 24;
205 static int wv_read_header(AVFormatContext *s,
206 AVFormatParameters *ap)
208 AVIOContext *pb = s->pb;
209 WVContext *wc = s->priv_data;
212 wc->block_parsed = 0;
214 if(wv_read_block_header(s, pb, 0) < 0)
216 if(!AV_RN32(wc->extra))
217 avio_skip(pb, wc->blksize - 24);
222 /* now we are ready: build format streams */
223 st = avformat_new_stream(s, NULL);
226 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
227 st->codec->codec_id = CODEC_ID_WAVPACK;
228 st->codec->channels = wc->chan;
229 st->codec->channel_layout = wc->chmask;
230 st->codec->sample_rate = wc->rate;
231 st->codec->bits_per_coded_sample = wc->bpp;
232 av_set_pts_info(st, 64, 1, wc->rate);
234 st->duration = wc->samples;
236 if(s->pb->seekable) {
237 int64_t cur = avio_tell(s->pb);
239 if(!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
241 avio_seek(s->pb, cur, SEEK_SET);
247 static int wv_read_packet(AVFormatContext *s,
250 WVContext *wc = s->priv_data;
255 if (s->pb->eof_reached)
257 if(wc->block_parsed){
258 if(wv_read_block_header(s, s->pb, 0) < 0)
263 off = wc->multichannel ? 4 : 0;
264 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
265 return AVERROR(ENOMEM);
267 AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
268 memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
269 ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
270 if(ret != wc->blksize){
274 while(!(wc->flags & WV_END_BLOCK)){
275 if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
279 if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
283 size = AV_RL32(pkt->data + pkt->size - 4);
284 if(size < 24 || size > WV_BLOCK_LIMIT){
286 av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
290 ver = avio_rl16(s->pb);
291 if(ver < 0x402 || ver > 0x410){
293 av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
296 avio_r8(s->pb); // track no
297 avio_r8(s->pb); // track sub index
298 wc->samples = avio_rl32(s->pb); // total samples in file
299 wc->soff = avio_rl32(s->pb); // offset in samples of current block
300 if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
304 memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
306 if(wv_read_block_header(s, s->pb, 1) < 0){
310 ret = av_append_packet(s->pb, pkt, wc->blksize);
316 pkt->stream_index = 0;
317 wc->block_parsed = 1;
319 av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
323 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
325 AVStream *st = s->streams[stream_index];
326 WVContext *wc = s->priv_data;
327 AVPacket pkt1, *pkt = &pkt1;
329 int index = av_index_search_timestamp(st, timestamp, flags);
332 /* if found, seek there */
334 timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
335 wc->block_parsed = 1;
336 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
339 /* if timestamp is out of bounds, return error */
340 if(timestamp < 0 || timestamp >= s->duration)
343 pos = avio_tell(s->pb);
345 ret = av_read_frame(s, pkt);
347 avio_seek(s->pb, pos, SEEK_SET);
352 }while(pts < timestamp);
356 AVInputFormat ff_wv_demuxer = {
358 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
359 .priv_data_size = sizeof(WVContext),
360 .read_probe = wv_probe,
361 .read_header = wv_read_header,
362 .read_packet = wv_read_packet,
363 .read_seek = wv_read_seek,