00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/audioconvert.h"
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/dict.h"
00025 #include "avformat.h"
00026 #include "internal.h"
00027 #include "apetag.h"
00028 #include "id3v1.h"
00029
00030
00031 #define WV_BLOCK_LIMIT 1047576
00032
00033 #define WV_EXTRA_SIZE 12
00034
00035 #define WV_START_BLOCK 0x0800
00036 #define WV_END_BLOCK 0x1000
00037 #define WV_SINGLE_BLOCK (WV_START_BLOCK | WV_END_BLOCK)
00038
00039 enum WV_FLAGS {
00040 WV_MONO = 0x0004,
00041 WV_HYBRID = 0x0008,
00042 WV_JOINT = 0x0010,
00043 WV_CROSSD = 0x0020,
00044 WV_HSHAPE = 0x0040,
00045 WV_FLOAT = 0x0080,
00046 WV_INT32 = 0x0100,
00047 WV_HBR = 0x0200,
00048 WV_HBAL = 0x0400,
00049 WV_MCINIT = 0x0800,
00050 WV_MCEND = 0x1000,
00051 };
00052
00053 static const int wv_rates[16] = {
00054 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
00055 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
00056 };
00057
00058 typedef struct {
00059 uint32_t blksize, flags;
00060 int rate, chan, bpp;
00061 uint32_t chmask;
00062 uint32_t samples, soff;
00063 int multichannel;
00064 int block_parsed;
00065 uint8_t extra[WV_EXTRA_SIZE];
00066 int64_t pos;
00067
00068 int64_t apetag_start;
00069 } WVContext;
00070
00071 static int wv_probe(AVProbeData *p)
00072 {
00073
00074 if (p->buf_size <= 32)
00075 return 0;
00076 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
00077 p->buf[2] == 'p' && p->buf[3] == 'k')
00078 return AVPROBE_SCORE_MAX;
00079 else
00080 return 0;
00081 }
00082
00083 static int wv_read_block_header(AVFormatContext *ctx, AVIOContext *pb,
00084 int append)
00085 {
00086 WVContext *wc = ctx->priv_data;
00087 uint32_t tag, ver;
00088 int size;
00089 int rate, bpp, chan;
00090 uint32_t chmask;
00091
00092 wc->pos = avio_tell(pb);
00093
00094
00095 if (wc->apetag_start && wc->pos >= wc->apetag_start)
00096 return AVERROR_EOF;
00097
00098 if (!append) {
00099 tag = avio_rl32(pb);
00100 if (tag != MKTAG('w', 'v', 'p', 'k'))
00101 return AVERROR_INVALIDDATA;
00102 size = avio_rl32(pb);
00103 if (size < 24 || size > WV_BLOCK_LIMIT) {
00104 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
00105 return AVERROR_INVALIDDATA;
00106 }
00107 wc->blksize = size;
00108 ver = avio_rl16(pb);
00109 if (ver < 0x402 || ver > 0x410) {
00110 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
00111 return AVERROR_PATCHWELCOME;
00112 }
00113 avio_r8(pb);
00114 avio_r8(pb);
00115 wc->samples = avio_rl32(pb);
00116 wc->soff = avio_rl32(pb);
00117 avio_read(pb, wc->extra, WV_EXTRA_SIZE);
00118 } else {
00119 size = wc->blksize;
00120 }
00121 wc->flags = AV_RL32(wc->extra + 4);
00122
00123
00124 if (!AV_RN32(wc->extra))
00125 return 0;
00126
00127 bpp = ((wc->flags & 3) + 1) << 3;
00128 chan = 1 + !(wc->flags & WV_MONO);
00129 chmask = wc->flags & WV_MONO ? AV_CH_LAYOUT_MONO : AV_CH_LAYOUT_STEREO;
00130 rate = wv_rates[(wc->flags >> 23) & 0xF];
00131 wc->multichannel = !!((wc->flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK);
00132 if (wc->multichannel) {
00133 chan = wc->chan;
00134 chmask = wc->chmask;
00135 }
00136 if ((rate == -1 || !chan) && !wc->block_parsed) {
00137 int64_t block_end = avio_tell(pb) + wc->blksize - 24;
00138 if (!pb->seekable) {
00139 av_log(ctx, AV_LOG_ERROR,
00140 "Cannot determine additional parameters\n");
00141 return AVERROR_INVALIDDATA;
00142 }
00143 while (avio_tell(pb) < block_end) {
00144 int id, size;
00145 id = avio_r8(pb);
00146 size = (id & 0x80) ? avio_rl24(pb) : avio_r8(pb);
00147 size <<= 1;
00148 if (id & 0x40)
00149 size--;
00150 switch (id & 0x3F) {
00151 case 0xD:
00152 if (size <= 1) {
00153 av_log(ctx, AV_LOG_ERROR,
00154 "Insufficient channel information\n");
00155 return AVERROR_INVALIDDATA;
00156 }
00157 chan = avio_r8(pb);
00158 switch (size - 2) {
00159 case 0:
00160 chmask = avio_r8(pb);
00161 break;
00162 case 1:
00163 chmask = avio_rl16(pb);
00164 break;
00165 case 2:
00166 chmask = avio_rl24(pb);
00167 break;
00168 case 3:
00169 chmask = avio_rl32(pb);
00170 break;
00171 case 5:
00172 avio_skip(pb, 1);
00173 chan |= (avio_r8(pb) & 0xF) << 8;
00174 chmask = avio_rl24(pb);
00175 break;
00176 default:
00177 av_log(ctx, AV_LOG_ERROR,
00178 "Invalid channel info size %d\n", size);
00179 return AVERROR_INVALIDDATA;
00180 }
00181 break;
00182 case 0x27:
00183 rate = avio_rl24(pb);
00184 break;
00185 default:
00186 avio_skip(pb, size);
00187 }
00188 if (id & 0x40)
00189 avio_skip(pb, 1);
00190 }
00191 if (rate == -1) {
00192 av_log(ctx, AV_LOG_ERROR,
00193 "Cannot determine custom sampling rate\n");
00194 return AVERROR_INVALIDDATA;
00195 }
00196 avio_seek(pb, block_end - wc->blksize + 24, SEEK_SET);
00197 }
00198 if (!wc->bpp)
00199 wc->bpp = bpp;
00200 if (!wc->chan)
00201 wc->chan = chan;
00202 if (!wc->chmask)
00203 wc->chmask = chmask;
00204 if (!wc->rate)
00205 wc->rate = rate;
00206
00207 if (wc->flags && bpp != wc->bpp) {
00208 av_log(ctx, AV_LOG_ERROR,
00209 "Bits per sample differ, this block: %i, header block: %i\n",
00210 bpp, wc->bpp);
00211 return AVERROR_INVALIDDATA;
00212 }
00213 if (wc->flags && !wc->multichannel && chan != wc->chan) {
00214 av_log(ctx, AV_LOG_ERROR,
00215 "Channels differ, this block: %i, header block: %i\n",
00216 chan, wc->chan);
00217 return AVERROR_INVALIDDATA;
00218 }
00219 if (wc->flags && rate != -1 && rate != wc->rate) {
00220 av_log(ctx, AV_LOG_ERROR,
00221 "Sampling rate differ, this block: %i, header block: %i\n",
00222 rate, wc->rate);
00223 return AVERROR_INVALIDDATA;
00224 }
00225 wc->blksize = size - 24;
00226 return 0;
00227 }
00228
00229 static int wv_read_header(AVFormatContext *s)
00230 {
00231 AVIOContext *pb = s->pb;
00232 WVContext *wc = s->priv_data;
00233 AVStream *st;
00234 int ret;
00235
00236 wc->block_parsed = 0;
00237 for (;;) {
00238 if ((ret = wv_read_block_header(s, pb, 0)) < 0)
00239 return ret;
00240 if (!AV_RN32(wc->extra))
00241 avio_skip(pb, wc->blksize - 24);
00242 else
00243 break;
00244 }
00245
00246
00247 st = avformat_new_stream(s, NULL);
00248 if (!st)
00249 return AVERROR(ENOMEM);
00250 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00251 st->codec->codec_id = AV_CODEC_ID_WAVPACK;
00252 st->codec->channels = wc->chan;
00253 st->codec->channel_layout = wc->chmask;
00254 st->codec->sample_rate = wc->rate;
00255 st->codec->bits_per_coded_sample = wc->bpp;
00256 avpriv_set_pts_info(st, 64, 1, wc->rate);
00257 st->start_time = 0;
00258 st->duration = wc->samples;
00259
00260 if (s->pb->seekable) {
00261 int64_t cur = avio_tell(s->pb);
00262 wc->apetag_start = ff_ape_parse_tag(s);
00263 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00264 ff_id3v1_read(s);
00265 avio_seek(s->pb, cur, SEEK_SET);
00266 }
00267
00268 return 0;
00269 }
00270
00271 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
00272 {
00273 WVContext *wc = s->priv_data;
00274 int ret;
00275 int size, ver, off;
00276 int64_t pos;
00277 uint32_t block_samples;
00278
00279 if (url_feof(s->pb))
00280 return AVERROR_EOF;
00281 if (wc->block_parsed) {
00282 if ((ret = wv_read_block_header(s, s->pb, 0)) < 0)
00283 return ret;
00284 }
00285
00286 pos = wc->pos;
00287 off = wc->multichannel ? 4 : 0;
00288 if (av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
00289 return AVERROR(ENOMEM);
00290 if (wc->multichannel)
00291 AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
00292 memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
00293 ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
00294 if (ret != wc->blksize) {
00295 av_free_packet(pkt);
00296 return AVERROR(EIO);
00297 }
00298 while (!(wc->flags & WV_END_BLOCK)) {
00299 if (avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')) {
00300 av_free_packet(pkt);
00301 return AVERROR_INVALIDDATA;
00302 }
00303 if ((ret = av_append_packet(s->pb, pkt, 4)) < 0) {
00304 av_free_packet(pkt);
00305 return ret;
00306 }
00307 size = AV_RL32(pkt->data + pkt->size - 4);
00308 if (size < 24 || size > WV_BLOCK_LIMIT) {
00309 av_free_packet(pkt);
00310 av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
00311 return AVERROR_INVALIDDATA;
00312 }
00313 wc->blksize = size;
00314 ver = avio_rl16(s->pb);
00315 if (ver < 0x402 || ver > 0x410) {
00316 av_free_packet(pkt);
00317 av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
00318 return AVERROR_PATCHWELCOME;
00319 }
00320 avio_r8(s->pb);
00321 avio_r8(s->pb);
00322 wc->samples = avio_rl32(s->pb);
00323 wc->soff = avio_rl32(s->pb);
00324 if ((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0) {
00325 av_free_packet(pkt);
00326 return ret;
00327 }
00328 memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
00329
00330 if ((ret = wv_read_block_header(s, s->pb, 1)) < 0) {
00331 av_free_packet(pkt);
00332 return ret;
00333 }
00334 ret = av_append_packet(s->pb, pkt, wc->blksize);
00335 if (ret < 0) {
00336 av_free_packet(pkt);
00337 return ret;
00338 }
00339 }
00340 pkt->stream_index = 0;
00341 wc->block_parsed = 1;
00342 pkt->pts = wc->soff;
00343 block_samples = AV_RN32(wc->extra);
00344 if (block_samples > INT32_MAX)
00345 av_log(s, AV_LOG_WARNING,
00346 "Too many samples in block: %"PRIu32"\n", block_samples);
00347 else
00348 pkt->duration = block_samples;
00349
00350 av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
00351 return 0;
00352 }
00353
00354 static int wv_read_seek(AVFormatContext *s, int stream_index,
00355 int64_t timestamp, int flags)
00356 {
00357 AVStream *st = s->streams[stream_index];
00358 WVContext *wc = s->priv_data;
00359 AVPacket pkt1, *pkt = &pkt1;
00360 int ret;
00361 int index = av_index_search_timestamp(st, timestamp, flags);
00362 int64_t pos, pts;
00363
00364
00365 if (index >= 0 &&
00366 timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
00367 wc->block_parsed = 1;
00368 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
00369 return 0;
00370 }
00371
00372 if (timestamp < 0 || timestamp >= s->duration)
00373 return AVERROR(EINVAL);
00374
00375 pos = avio_tell(s->pb);
00376 do {
00377 ret = av_read_frame(s, pkt);
00378 if (ret < 0) {
00379 avio_seek(s->pb, pos, SEEK_SET);
00380 return ret;
00381 }
00382 pts = pkt->pts;
00383 av_free_packet(pkt);
00384 } while(pts < timestamp);
00385 return 0;
00386 }
00387
00388 AVInputFormat ff_wv_demuxer = {
00389 .name = "wv",
00390 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
00391 .priv_data_size = sizeof(WVContext),
00392 .read_probe = wv_probe,
00393 .read_header = wv_read_header,
00394 .read_packet = wv_read_packet,
00395 .read_seek = wv_read_seek,
00396 };