3 * Copyright (c) 2006 Konstantin Shishkov.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "allformats.h"
24 // specs say that maximum block size is 1Mb
25 #define WV_BLOCK_LIMIT 1047576
27 #define WV_EXTRA_SIZE 12
43 static const int wv_rates[16] = {
44 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
45 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
49 uint32_t blksize, flags;
52 uint8_t extra[WV_EXTRA_SIZE];
55 static int wv_probe(AVProbeData *p)
57 /* check file header */
58 if (p->buf_size <= 32)
60 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
61 p->buf[2] == 'p' && p->buf[3] == 'k')
62 return AVPROBE_SCORE_MAX;
67 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
69 WVContext *wc = ctx->priv_data;
75 if (tag != MKTAG('w', 'v', 'p', 'k'))
78 if(size < 24 || size > WV_BLOCK_LIMIT){
79 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
84 if(ver < 0x402 || ver > 0x40F){
85 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
88 get_byte(pb); // track no
89 get_byte(pb); // track sub index
90 get_le32(pb); // total samples in file
91 get_le32(pb); // offset in samples of current block
92 get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
93 wc->flags = LE_32(wc->extra + 4);
95 if(wc->flags & WV_FLOAT){
96 av_log(ctx, AV_LOG_ERROR, "Floating point data is not supported\n");
99 if(wc->flags & WV_HYBRID){
100 av_log(ctx, AV_LOG_ERROR, "Hybrid coding mode is not supported\n");
103 if(wc->flags & WV_INT32){
104 av_log(ctx, AV_LOG_ERROR, "Integer point data is not supported\n");
108 bpp = ((wc->flags & 3) + 1) << 3;
109 chan = 1 + !(wc->flags & WV_MONO);
110 rate = wv_rates[(wc->flags >> 23) & 0xF];
112 av_log(ctx, AV_LOG_ERROR, "Unknown sampling rate\n");
115 if(!wc->bpp) wc->bpp = bpp;
116 if(!wc->chan) wc->chan = chan;
117 if(!wc->rate) wc->rate = rate;
120 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
123 if(chan != wc->chan){
124 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
127 if(rate != wc->rate){
128 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
131 wc->blksize = size - 24;
135 static int wv_read_header(AVFormatContext *s,
136 AVFormatParameters *ap)
138 ByteIOContext *pb = &s->pb;
139 WVContext *wc = s->priv_data;
142 if(wv_read_block_header(s, pb) < 0)
145 wc->block_parsed = 0;
146 /* now we are ready: build format streams */
147 st = av_new_stream(s, 0);
150 st->codec->codec_type = CODEC_TYPE_AUDIO;
151 st->codec->codec_id = CODEC_ID_WAVPACK;
152 st->codec->channels = wc->chan;
153 st->codec->sample_rate = wc->rate;
154 st->codec->bits_per_sample = wc->bpp;
155 av_set_pts_info(st, 64, 1, wc->rate);
159 static int wv_read_packet(AVFormatContext *s,
162 WVContext *wc = s->priv_data;
165 if (url_feof(&s->pb))
167 if(wc->block_parsed){
168 if(wv_read_block_header(s, &s->pb) < 0)
172 samples = LE_32(wc->extra);
173 /* should not happen but who knows */
174 if(samples * 2 * wc->chan > AVCODEC_MAX_AUDIO_FRAME_SIZE){
175 av_log(s, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
178 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
179 return AVERROR_NOMEM;
180 memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
181 ret = get_buffer(&s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
182 if(ret != wc->blksize){
186 pkt->stream_index = 0;
187 wc->block_parsed = 1;
188 pkt->size = ret + WV_EXTRA_SIZE;
193 static int wv_read_close(AVFormatContext *s)
198 AVInputFormat wv_demuxer = {