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/channel_layout.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
46 static const int wv_rates[16] = {
47 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
48 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
51 typedef struct WVContext {
52 uint8_t block_header[WV_HEADER_SIZE];
63 static int wv_probe(const AVProbeData *p)
65 /* check file header */
66 if (p->buf_size <= 32)
68 if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
69 AV_RL32(&p->buf[4]) >= 24 &&
70 AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
71 AV_RL16(&p->buf[8]) >= 0x402 &&
72 AV_RL16(&p->buf[8]) <= 0x410)
73 return AVPROBE_SCORE_MAX;
78 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
80 WVContext *wc = ctx->priv_data;
83 uint32_t chmask, flags;
86 wc->pos = avio_tell(pb);
88 /* don't return bogus packets with the ape tag data */
89 if (wc->apetag_start && wc->pos >= wc->apetag_start)
92 ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
93 if (ret != WV_HEADER_SIZE)
94 return (ret < 0) ? ret : AVERROR_EOF;
96 ret = ff_wv_parse_header(&wc->header, wc->block_header);
98 av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
102 if (wc->header.version < 0x402 || wc->header.version > 0x410) {
103 avpriv_report_missing_feature(ctx, "WV version 0x%03X",
105 return AVERROR_PATCHWELCOME;
108 /* Blocks with zero samples don't contain actual audio information
109 * and should be ignored */
110 if (!wc->header.samples)
113 flags = wc->header.flags;
114 rate_x = (flags & WV_DSD) ? 4 : 1;
115 bpp = (flags & WV_DSD) ? 0 : ((flags & 3) + 1) << 3;
116 chan = 1 + !(flags & WV_MONO);
117 chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
118 rate = wv_rates[(flags >> 23) & 0xF];
119 wc->multichannel = !(wc->header.initial && wc->header.final);
120 if (wc->multichannel) {
124 if ((rate == -1 || !chan || flags & WV_DSD) && !wc->block_parsed) {
125 int64_t block_end = avio_tell(pb) + wc->header.blocksize;
126 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
127 av_log(ctx, AV_LOG_ERROR,
128 "Cannot determine additional parameters\n");
129 return AVERROR_INVALIDDATA;
131 while (avio_tell(pb) < block_end && !avio_feof(pb)) {
134 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
141 av_log(ctx, AV_LOG_ERROR,
142 "Insufficient channel information\n");
143 return AVERROR_INVALIDDATA;
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;
163 chmask = avio_rl24(pb);
167 chan |= (avio_r8(pb) & 0xF) << 8;
169 chmask = avio_rl32(pb);
172 av_log(ctx, AV_LOG_ERROR,
173 "Invalid channel info size %d\n", size);
174 return AVERROR_INVALIDDATA;
179 av_log(ctx, AV_LOG_ERROR,
180 "Invalid DSD block\n");
181 return AVERROR_INVALIDDATA;
183 rate_x = 1U << (avio_r8(pb) & 0x1f);
185 avio_skip(pb, size-1);
188 rate = avio_rl24(pb);
196 if (rate == -1 || rate * (uint64_t)rate_x >= INT_MAX) {
197 av_log(ctx, AV_LOG_ERROR,
198 "Cannot determine custom sampling rate\n");
199 return AVERROR_INVALIDDATA;
201 avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
210 wc->rate = rate * rate_x;
212 if (flags && bpp != wc->bpp) {
213 av_log(ctx, AV_LOG_ERROR,
214 "Bits per sample differ, this block: %i, header block: %i\n",
216 return AVERROR_INVALIDDATA;
218 if (flags && !wc->multichannel && chan != wc->chan) {
219 av_log(ctx, AV_LOG_ERROR,
220 "Channels differ, this block: %i, header block: %i\n",
222 return AVERROR_INVALIDDATA;
224 if (flags && rate != -1 && !(flags & WV_DSD) && rate * rate_x != wc->rate) {
225 av_log(ctx, AV_LOG_ERROR,
226 "Sampling rate differ, this block: %i, header block: %i\n",
227 rate * rate_x, wc->rate);
228 return AVERROR_INVALIDDATA;
233 static int wv_read_header(AVFormatContext *s)
235 AVIOContext *pb = s->pb;
236 WVContext *wc = s->priv_data;
240 wc->block_parsed = 0;
242 if ((ret = wv_read_block_header(s, pb)) < 0)
244 if (!wc->header.samples)
245 avio_skip(pb, wc->header.blocksize);
250 /* now we are ready: build format streams */
251 st = avformat_new_stream(s, NULL);
253 return AVERROR(ENOMEM);
254 if ((ret = ff_alloc_extradata(st->codecpar, 2)) < 0)
256 AV_WL16(st->codecpar->extradata, wc->header.version);
257 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
258 st->codecpar->codec_id = AV_CODEC_ID_WAVPACK;
259 st->codecpar->channels = wc->chan;
260 st->codecpar->channel_layout = wc->chmask;
261 st->codecpar->sample_rate = wc->rate;
262 st->codecpar->bits_per_coded_sample = wc->bpp;
263 avpriv_set_pts_info(st, 64, 1, wc->rate);
265 if (wc->header.total_samples != 0xFFFFFFFFu)
266 st->duration = wc->header.total_samples;
268 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
269 int64_t cur = avio_tell(s->pb);
270 wc->apetag_start = ff_ape_parse_tag(s);
271 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
273 avio_seek(s->pb, cur, SEEK_SET);
279 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
281 WVContext *wc = s->priv_data;
285 uint32_t block_samples;
287 if (avio_feof(s->pb))
289 if (wc->block_parsed) {
290 if ((ret = wv_read_block_header(s, s->pb)) < 0)
295 if ((ret = av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE)) < 0)
297 memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
298 ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
299 if (ret != wc->header.blocksize) {
302 while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
303 if ((ret = wv_read_block_header(s, s->pb)) < 0) {
308 if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
311 memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
313 ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
314 if (ret != wc->header.blocksize) {
315 return (ret < 0) ? ret : AVERROR_EOF;
318 pkt->stream_index = 0;
320 wc->block_parsed = 1;
321 pkt->pts = wc->header.block_idx;
322 block_samples = wc->header.samples;
323 if (block_samples > INT32_MAX)
324 av_log(s, AV_LOG_WARNING,
325 "Too many samples in block: %"PRIu32"\n", block_samples);
327 pkt->duration = block_samples;
332 AVInputFormat ff_wv_demuxer = {
334 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
335 .priv_data_size = sizeof(WVContext),
336 .read_probe = wv_probe,
337 .read_header = wv_read_header,
338 .read_packet = wv_read_packet,
339 .flags = AVFMT_GENERIC_INDEX,