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/intreadwrite.h"
27 // specs say that maximum block size is 1Mb
28 #define WV_BLOCK_LIMIT 1047576
30 #define WV_EXTRA_SIZE 12
32 #define WV_START_BLOCK 0x0800
33 #define WV_END_BLOCK 0x1000
34 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
50 static const int wv_rates[16] = {
51 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
52 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
56 uint32_t blksize, flags;
59 uint32_t samples, soff;
62 uint8_t extra[WV_EXTRA_SIZE];
66 static int wv_probe(AVProbeData *p)
68 /* check file header */
69 if (p->buf_size <= 32)
71 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
72 p->buf[2] == 'p' && p->buf[3] == 'k')
73 return AVPROBE_SCORE_MAX;
78 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb, int append)
80 WVContext *wc = ctx->priv_data;
86 wc->pos = url_ftell(pb);
89 if (tag != MKTAG('w', 'v', 'p', 'k'))
92 if(size < 24 || size > WV_BLOCK_LIMIT){
93 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
98 if(ver < 0x402 || ver > 0x410){
99 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
102 get_byte(pb); // track no
103 get_byte(pb); // track sub index
104 wc->samples = get_le32(pb); // total samples in file
105 wc->soff = get_le32(pb); // offset in samples of current block
106 get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
110 wc->flags = AV_RL32(wc->extra + 4);
112 bpp = ((wc->flags & 3) + 1) << 3;
113 chan = 1 + !(wc->flags & WV_MONO);
114 chmask = wc->flags & WV_MONO ? CH_LAYOUT_MONO : CH_LAYOUT_STEREO;
115 rate = wv_rates[(wc->flags >> 23) & 0xF];
116 wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
117 if(wc->multichannel){
121 if((rate == -1 || !chan) && !wc->block_parsed){
122 int64_t block_end = url_ftell(pb) + wc->blksize - 24;
123 if(url_is_streamed(pb)){
124 av_log(ctx, AV_LOG_ERROR, "Cannot determine additional parameters\n");
127 while(url_ftell(pb) < block_end){
130 size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
137 av_log(ctx, AV_LOG_ERROR, "Insufficient channel information\n");
143 chmask = get_byte(pb);
146 chmask = get_le16(pb);
149 chmask = get_le24(pb);
152 chmask = get_le32(pb);
156 chan |= (get_byte(pb) & 0xF) << 8;
157 chmask = get_le24(pb);
160 av_log(ctx, AV_LOG_ERROR, "Invalid channel info size %d\n", size);
174 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
177 url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
179 if(!wc->bpp) wc->bpp = bpp;
180 if(!wc->chan) wc->chan = chan;
181 if(!wc->chmask) wc->chmask = chmask;
182 if(!wc->rate) wc->rate = rate;
184 if(wc->flags && bpp != wc->bpp){
185 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
188 if(wc->flags && !wc->multichannel && chan != wc->chan){
189 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
192 if(wc->flags && rate != -1 && rate != wc->rate){
193 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
196 wc->blksize = size - 24;
200 static int wv_read_header(AVFormatContext *s,
201 AVFormatParameters *ap)
203 ByteIOContext *pb = s->pb;
204 WVContext *wc = s->priv_data;
207 wc->block_parsed = 0;
208 if(wv_read_block_header(s, pb, 0) < 0)
211 /* now we are ready: build format streams */
212 st = av_new_stream(s, 0);
215 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
216 st->codec->codec_id = CODEC_ID_WAVPACK;
217 st->codec->channels = wc->chan;
218 st->codec->channel_layout = wc->chmask;
219 st->codec->sample_rate = wc->rate;
220 st->codec->bits_per_coded_sample = wc->bpp;
221 av_set_pts_info(st, 64, 1, wc->rate);
223 st->duration = wc->samples;
225 if(!url_is_streamed(s->pb)) {
226 int64_t cur = url_ftell(s->pb);
228 if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
230 url_fseek(s->pb, cur, SEEK_SET);
236 static int wv_read_packet(AVFormatContext *s,
239 WVContext *wc = s->priv_data;
245 if(wc->block_parsed){
246 if(wv_read_block_header(s, s->pb, 0) < 0)
250 off = wc->multichannel ? 4 : 0;
251 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
252 return AVERROR(ENOMEM);
254 AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
255 memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
256 ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
257 if(ret != wc->blksize){
261 while(!(wc->flags & WV_END_BLOCK)){
262 if(get_le32(s->pb) != MKTAG('w', 'v', 'p', 'k')){
266 if((ret = av_append_packet(s->pb, pkt, 4)) < 0){
270 size = AV_RL32(pkt->data + pkt->size - 4);
271 if(size < 24 || size > WV_BLOCK_LIMIT){
273 av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
277 ver = get_le16(s->pb);
278 if(ver < 0x402 || ver > 0x410){
280 av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
283 get_byte(s->pb); // track no
284 get_byte(s->pb); // track sub index
285 wc->samples = get_le32(s->pb); // total samples in file
286 wc->soff = get_le32(s->pb); // offset in samples of current block
287 if((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0){
291 memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
293 if(wv_read_block_header(s, s->pb, 1) < 0){
297 ret = av_append_packet(s->pb, pkt, wc->blksize);
303 pkt->stream_index = 0;
304 wc->block_parsed = 1;
306 av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
310 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
312 AVStream *st = s->streams[stream_index];
313 WVContext *wc = s->priv_data;
314 AVPacket pkt1, *pkt = &pkt1;
316 int index = av_index_search_timestamp(st, timestamp, flags);
319 /* if found, seek there */
321 wc->block_parsed = 1;
322 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
325 /* if timestamp is out of bounds, return error */
326 if(timestamp < 0 || timestamp >= s->duration)
329 pos = url_ftell(s->pb);
331 ret = av_read_frame(s, pkt);
333 url_fseek(s->pb, pos, SEEK_SET);
338 }while(pts < timestamp);
342 AVInputFormat wv_demuxer = {
344 NULL_IF_CONFIG_SMALL("WavPack"),