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"
45 static const int wv_rates[16] = {
46 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
47 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
50 typedef struct WVContext {
51 uint8_t block_header[WV_HEADER_SIZE];
62 static int wv_probe(AVProbeData *p)
64 /* check file header */
65 if (p->buf_size <= 32)
67 if (AV_RL32(&p->buf[0]) == MKTAG('w', 'v', 'p', 'k') &&
68 AV_RL32(&p->buf[4]) >= 24 &&
69 AV_RL32(&p->buf[4]) <= WV_BLOCK_LIMIT &&
70 AV_RL16(&p->buf[8]) >= 0x402 &&
71 AV_RL16(&p->buf[8]) <= 0x410)
72 return AVPROBE_SCORE_MAX;
77 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
79 WVContext *wc = ctx->priv_data;
82 uint32_t chmask, flags;
84 wc->pos = avio_tell(pb);
86 /* don't return bogus packets with the ape tag data */
87 if (wc->apetag_start && wc->pos >= wc->apetag_start)
90 ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
91 if (ret != WV_HEADER_SIZE)
92 return (ret < 0) ? ret : AVERROR_EOF;
94 ret = ff_wv_parse_header(&wc->header, wc->block_header);
96 av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
100 if (wc->header.version < 0x402 || wc->header.version > 0x410) {
101 avpriv_report_missing_feature(ctx, "WV version 0x%03X",
103 return AVERROR_PATCHWELCOME;
106 /* Blocks with zero samples don't contain actual audio information
107 * and should be ignored */
108 if (!wc->header.samples)
111 flags = wc->header.flags;
112 bpp = ((flags & 3) + 1) << 3;
113 chan = 1 + !(flags & WV_MONO);
114 chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
115 rate = wv_rates[(flags >> 23) & 0xF];
116 wc->multichannel = !(wc->header.initial && wc->header.final);
117 if (wc->multichannel) {
121 if ((rate == -1 || !chan) && !wc->block_parsed) {
122 int64_t block_end = avio_tell(pb) + wc->header.blocksize;
123 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
124 av_log(ctx, AV_LOG_ERROR,
125 "Cannot determine additional parameters\n");
126 return AVERROR_INVALIDDATA;
128 while (avio_tell(pb) < block_end && !avio_feof(pb)) {
131 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
138 av_log(ctx, AV_LOG_ERROR,
139 "Insufficient channel information\n");
140 return AVERROR_INVALIDDATA;
145 chmask = avio_r8(pb);
148 chmask = avio_rl16(pb);
151 chmask = avio_rl24(pb);
154 chmask = avio_rl32(pb);
158 chan |= (avio_r8(pb) & 0xF) << 8;
160 chmask = avio_rl24(pb);
164 chan |= (avio_r8(pb) & 0xF) << 8;
166 chmask = avio_rl32(pb);
169 av_log(ctx, AV_LOG_ERROR,
170 "Invalid channel info size %d\n", size);
171 return AVERROR_INVALIDDATA;
175 rate = avio_rl24(pb);
184 av_log(ctx, AV_LOG_ERROR,
185 "Cannot determine custom sampling rate\n");
186 return AVERROR_INVALIDDATA;
188 avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
199 if (flags && bpp != wc->bpp) {
200 av_log(ctx, AV_LOG_ERROR,
201 "Bits per sample differ, this block: %i, header block: %i\n",
203 return AVERROR_INVALIDDATA;
205 if (flags && !wc->multichannel && chan != wc->chan) {
206 av_log(ctx, AV_LOG_ERROR,
207 "Channels differ, this block: %i, header block: %i\n",
209 return AVERROR_INVALIDDATA;
211 if (flags && rate != -1 && rate != wc->rate) {
212 av_log(ctx, AV_LOG_ERROR,
213 "Sampling rate differ, this block: %i, header block: %i\n",
215 return AVERROR_INVALIDDATA;
220 static int wv_read_header(AVFormatContext *s)
222 AVIOContext *pb = s->pb;
223 WVContext *wc = s->priv_data;
227 wc->block_parsed = 0;
229 if ((ret = wv_read_block_header(s, pb)) < 0)
231 if (!wc->header.samples)
232 avio_skip(pb, wc->header.blocksize);
237 /* now we are ready: build format streams */
238 st = avformat_new_stream(s, NULL);
240 return AVERROR(ENOMEM);
241 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
242 st->codecpar->codec_id = AV_CODEC_ID_WAVPACK;
243 st->codecpar->channels = wc->chan;
244 st->codecpar->channel_layout = wc->chmask;
245 st->codecpar->sample_rate = wc->rate;
246 st->codecpar->bits_per_coded_sample = wc->bpp;
247 avpriv_set_pts_info(st, 64, 1, wc->rate);
249 if (wc->header.total_samples != 0xFFFFFFFFu)
250 st->duration = wc->header.total_samples;
252 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
253 int64_t cur = avio_tell(s->pb);
254 wc->apetag_start = ff_ape_parse_tag(s);
255 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
257 avio_seek(s->pb, cur, SEEK_SET);
263 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
265 WVContext *wc = s->priv_data;
269 uint32_t block_samples;
271 if (avio_feof(s->pb))
273 if (wc->block_parsed) {
274 if ((ret = wv_read_block_header(s, s->pb)) < 0)
279 if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
280 return AVERROR(ENOMEM);
281 memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
282 ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
283 if (ret != wc->header.blocksize) {
284 av_packet_unref(pkt);
287 while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
288 if ((ret = wv_read_block_header(s, s->pb)) < 0) {
289 av_packet_unref(pkt);
294 if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
295 av_packet_unref(pkt);
298 memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
300 ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
301 if (ret != wc->header.blocksize) {
302 av_packet_unref(pkt);
303 return (ret < 0) ? ret : AVERROR_EOF;
306 pkt->stream_index = 0;
308 wc->block_parsed = 1;
309 pkt->pts = wc->header.block_idx;
310 block_samples = wc->header.samples;
311 if (block_samples > INT32_MAX)
312 av_log(s, AV_LOG_WARNING,
313 "Too many samples in block: %"PRIu32"\n", block_samples);
315 pkt->duration = block_samples;
320 AVInputFormat ff_wv_demuxer = {
322 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
323 .priv_data_size = sizeof(WVContext),
324 .read_probe = wv_probe,
325 .read_header = wv_read_header,
326 .read_packet = wv_read_packet,
327 .flags = AVFMT_GENERIC_INDEX,