3 * Copyright (c) 2006,2011 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/intreadwrite.h"
28 // specs say that maximum block size is 1Mb
29 #define WV_BLOCK_LIMIT 1047576
31 #define WV_EXTRA_SIZE 12
33 #define WV_START_BLOCK 0x0800
34 #define WV_END_BLOCK 0x1000
35 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
51 static const int wv_rates[16] = {
52 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
53 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
57 uint32_t blksize, flags;
60 uint32_t samples, soff;
63 uint8_t extra[WV_EXTRA_SIZE];
67 static int wv_probe(AVProbeData *p)
69 /* check file header */
70 if (p->buf_size <= 32)
72 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
73 p->buf[2] == 'p' && p->buf[3] == 'k')
74 return AVPROBE_SCORE_MAX;
79 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb, int append)
81 WVContext *wc = ctx->priv_data;
87 wc->pos = avio_tell(pb);
90 if (tag != MKTAG('w', 'v', 'p', 'k'))
93 if(size < 24 || size > WV_BLOCK_LIMIT){
94 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
99 if(ver < 0x402 || ver > 0x410){
100 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
103 avio_r8(pb); // track no
104 avio_r8(pb); // track sub index
105 wc->samples = avio_rl32(pb); // total samples in file
106 wc->soff = avio_rl32(pb); // offset in samples of current block
107 avio_read(pb, wc->extra, WV_EXTRA_SIZE);
111 wc->flags = AV_RL32(wc->extra + 4);
113 bpp = ((wc->flags & 3) + 1) << 3;
114 chan = 1 + !(wc->flags & WV_MONO);
115 chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
116 rate = wv_rates[(wc->flags >> 23) & 0xF];
117 wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
118 if(wc->multichannel){
122 if((rate == -1 || !chan) && !wc->block_parsed){
123 int64_t block_end = avio_tell(pb) + wc->blksize - 24;
124 if(url_is_streamed(pb)){
125 av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
128 while(avio_tell(pb) < block_end){
131 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
138 av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
144 chmask = avio_r8(pb);
147 chmask = avio_rl16(pb);
150 chmask = avio_rl24(pb);
153 chmask = avio_rl32(pb);
156 avio_seek(pb, 1, SEEK_CUR);
157 chan |= (avio_r8(pb) & 0xF) << 8;
158 chmask = avio_rl24(pb);
161 av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
166 rate = avio_rl24(pb);
169 avio_seek(pb, size, SEEK_CUR);
172 avio_seek(pb, 1, SEEK_CUR);
175 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
178 avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
180 if(!wc->bpp) wc->bpp = bpp;
181 if(!wc->chan) wc->chan = chan;
182 if(!wc->chmask) wc->chmask = chmask;
183 if(!wc->rate) wc->rate = rate;
185 if(wc->flags && bpp != wc->bpp){
186 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
189 if(wc->flags && !wc->multichannel && chan != wc->chan){
190 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
193 if(wc->flags && rate != -1 && rate != wc->rate){
194 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
197 wc->blksize = size - 24;
201 static int wv_read_header(AVFormatContext *s,
202 AVFormatParameters *ap)
204 AVIOContext *pb = s->pb;
205 WVContext *wc = s->priv_data;
208 wc->block_parsed = 0;
209 if(wv_read_block_header(s, pb, 0) < 0)
212 /* now we are ready: build format streams */
213 st = av_new_stream(s, 0);
216 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
217 st->codec->codec_id = CODEC_ID_WAVPACK;
218 st->codec->channels = wc->chan;
219 st->codec->channel_layout = wc->chmask;
220 st->codec->sample_rate = wc->rate;
221 st->codec->bits_per_coded_sample = wc->bpp;
222 av_set_pts_info(st, 64, 1, wc->rate);
224 st->duration = wc->samples;
226 if(!url_is_streamed(s->pb)) {
227 int64_t cur = avio_tell(s->pb);
229 if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
231 avio_seek(s->pb, cur, SEEK_SET);
237 static int wv_read_packet(AVFormatContext *s,
240 WVContext *wc = s->priv_data;
246 if(wc->block_parsed){
247 if(wv_read_block_header(s, s->pb, 0) < 0)
251 off = wc->multichannel ? 4 : 0;
252 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
253 return AVERROR(ENOMEM);
255 AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
256 memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
257 ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
258 if(ret != wc->blksize){
262 while(!(wc->flags & WV_END_BLOCK)){
263 if(avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
267 if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
271 size = AV_RL32(pkt->data + pkt->size - 4);
272 if(size < 24 || size > WV_BLOCK_LIMIT){
274 av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
278 ver = avio_rl16(s->pb);
279 if(ver < 0x402 || ver > 0x410){
281 av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
284 avio_r8(s->pb); // track no
285 avio_r8(s->pb); // track sub index
286 wc->samples = avio_rl32(s->pb); // total samples in file
287 wc->soff = avio_rl32(s->pb); // offset in samples of current block
288 if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
292 memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
294 if(wv_read_block_header(s, s->pb, 1) < 0){
298 ret = av_append_packet(s->pb, pkt, wc->blksize);
304 pkt->stream_index = 0;
305 wc->block_parsed = 1;
307 av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
311 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
313 AVStream *st = s->streams[stream_index];
314 WVContext *wc = s->priv_data;
315 AVPacket pkt1, *pkt = &pkt1;
317 int index = av_index_search_timestamp(st, timestamp, flags);
320 /* if found, seek there */
322 wc->block_parsed = 1;
323 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
326 /* if timestamp is out of bounds, return error */
327 if(timestamp < 0 || timestamp >= s->duration)
330 pos = avio_tell(s->pb);
332 ret = av_read_frame(s, pkt);
334 avio_seek(s->pb, pos, SEEK_SET);
339 }while(pts < timestamp);
343 AVInputFormat ff_wv_demuxer = {
345 NULL_IF_CONFIG_SMALL("WavPack"),