3 * Copyright (c) 2006,2011 Konstantin Shishkov
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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 (p->buf[0] == 'w' && p->buf[1] == 'v' &&
68 p->buf[2] == 'p' && p->buf[3] == 'k')
69 return AVPROBE_SCORE_MAX;
74 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb)
76 WVContext *wc = ctx->priv_data;
79 uint32_t chmask, flags;
81 wc->pos = avio_tell(pb);
83 /* don't return bogus packets with the ape tag data */
84 if (wc->apetag_start && wc->pos >= wc->apetag_start)
87 ret = avio_read(pb, wc->block_header, WV_HEADER_SIZE);
88 if (ret != WV_HEADER_SIZE)
89 return (ret < 0) ? ret : AVERROR_EOF;
91 ret = ff_wv_parse_header(&wc->header, wc->block_header);
93 av_log(ctx, AV_LOG_ERROR, "Invalid block header.\n");
97 if (wc->header.version < 0x402 || wc->header.version > 0x410) {
98 avpriv_report_missing_feature(ctx, "WV version 0x%03X",
100 return AVERROR_PATCHWELCOME;
103 /* Blocks with zero samples don't contain actual audio information
104 * and should be ignored */
105 if (!wc->header.samples)
108 flags = wc->header.flags;
109 bpp = ((flags & 3) + 1) << 3;
110 chan = 1 + !(flags & WV_MONO);
111 chmask = flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
112 rate = wv_rates[(flags >> 23) & 0xF];
113 wc->multichannel = !(wc->header.initial && wc->header.final);
114 if (wc->multichannel) {
118 if ((rate == -1 || !chan) && !wc->block_parsed) {
119 int64_t block_end = avio_tell(pb) + wc->header.blocksize;
120 if (!(pb->seekable & AVIO_SEEKABLE_NORMAL)) {
121 av_log(ctx, AV_LOG_ERROR,
122 "Cannot determine additional parameters\n");
123 return AVERROR_INVALIDDATA;
125 while (avio_tell(pb) < block_end) {
128 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
135 av_log(ctx, AV_LOG_ERROR,
136 "Insufficient channel information\n");
137 return AVERROR_INVALIDDATA;
142 chmask = avio_r8(pb);
145 chmask = avio_rl16(pb);
148 chmask = avio_rl24(pb);
151 chmask = avio_rl32(pb);
155 chan |= (avio_r8(pb) & 0xF) << 8;
156 chmask = avio_rl24(pb);
159 av_log(ctx, AV_LOG_ERROR,
160 "Invalid channel info size %d\n", size);
161 return AVERROR_INVALIDDATA;
165 rate = avio_rl24(pb);
174 av_log(ctx, AV_LOG_ERROR,
175 "Cannot determine custom sampling rate\n");
176 return AVERROR_INVALIDDATA;
178 avio_seek(pb, block_end - wc->header.blocksize, SEEK_SET);
189 if (flags && bpp != wc->bpp) {
190 av_log(ctx, AV_LOG_ERROR,
191 "Bits per sample differ, this block: %i, header block: %i\n",
193 return AVERROR_INVALIDDATA;
195 if (flags && !wc->multichannel && chan != wc->chan) {
196 av_log(ctx, AV_LOG_ERROR,
197 "Channels differ, this block: %i, header block: %i\n",
199 return AVERROR_INVALIDDATA;
201 if (flags && rate != -1 && rate != wc->rate) {
202 av_log(ctx, AV_LOG_ERROR,
203 "Sampling rate differ, this block: %i, header block: %i\n",
205 return AVERROR_INVALIDDATA;
210 static int wv_read_header(AVFormatContext *s)
212 AVIOContext *pb = s->pb;
213 WVContext *wc = s->priv_data;
217 wc->block_parsed = 0;
219 if ((ret = wv_read_block_header(s, pb)) < 0)
221 if (!wc->header.samples)
222 avio_skip(pb, wc->header.blocksize);
227 /* now we are ready: build format streams */
228 st = avformat_new_stream(s, NULL);
230 return AVERROR(ENOMEM);
231 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
232 st->codecpar->codec_id = AV_CODEC_ID_WAVPACK;
233 st->codecpar->channels = wc->chan;
234 st->codecpar->channel_layout = wc->chmask;
235 st->codecpar->sample_rate = wc->rate;
236 st->codecpar->bits_per_coded_sample = wc->bpp;
237 avpriv_set_pts_info(st, 64, 1, wc->rate);
239 st->duration = wc->header.total_samples;
241 if (s->pb->seekable & AVIO_SEEKABLE_NORMAL) {
242 int64_t cur = avio_tell(s->pb);
243 wc->apetag_start = ff_ape_parse_tag(s);
244 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
246 avio_seek(s->pb, cur, SEEK_SET);
252 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
254 WVContext *wc = s->priv_data;
258 uint32_t block_samples;
260 if (s->pb->eof_reached)
262 if (wc->block_parsed) {
263 if ((ret = wv_read_block_header(s, s->pb)) < 0)
268 if (av_new_packet(pkt, wc->header.blocksize + WV_HEADER_SIZE) < 0)
269 return AVERROR(ENOMEM);
270 memcpy(pkt->data, wc->block_header, WV_HEADER_SIZE);
271 ret = avio_read(s->pb, pkt->data + WV_HEADER_SIZE, wc->header.blocksize);
272 if (ret != wc->header.blocksize) {
273 av_packet_unref(pkt);
276 while (!(wc->header.flags & WV_FLAG_FINAL_BLOCK)) {
277 if ((ret = wv_read_block_header(s, s->pb)) < 0) {
278 av_packet_unref(pkt);
283 if ((ret = av_grow_packet(pkt, WV_HEADER_SIZE + wc->header.blocksize)) < 0) {
284 av_packet_unref(pkt);
287 memcpy(pkt->data + off, wc->block_header, WV_HEADER_SIZE);
289 ret = avio_read(s->pb, pkt->data + off + WV_HEADER_SIZE, wc->header.blocksize);
290 if (ret != wc->header.blocksize) {
291 av_packet_unref(pkt);
292 return (ret < 0) ? ret : AVERROR_EOF;
295 pkt->stream_index = 0;
296 wc->block_parsed = 1;
297 pkt->pts = wc->header.block_idx;
298 block_samples = wc->header.samples;
299 if (block_samples > INT32_MAX)
300 av_log(s, AV_LOG_WARNING,
301 "Too many samples in block: %"PRIu32"\n", block_samples);
303 pkt->duration = block_samples;
305 av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
309 static int wv_read_seek(AVFormatContext *s, int stream_index,
310 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 timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
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)
328 return AVERROR(EINVAL);
330 pos = avio_tell(s->pb);
332 ret = av_read_frame(s, pkt);
334 avio_seek(s->pb, pos, SEEK_SET);
338 av_packet_unref(pkt);
339 } while(pts < timestamp);
343 AVInputFormat ff_wv_demuxer = {
345 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
346 .priv_data_size = sizeof(WVContext),
347 .read_probe = wv_probe,
348 .read_header = wv_read_header,
349 .read_packet = wv_read_packet,
350 .read_seek = wv_read_seek,