00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.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 if (wc->samples != 0xFFFFFFFFu)
00259 st->duration = wc->samples;
00260
00261 if (s->pb->seekable) {
00262 int64_t cur = avio_tell(s->pb);
00263 wc->apetag_start = ff_ape_parse_tag(s);
00264 if (!av_dict_get(s->metadata, "", NULL, AV_DICT_IGNORE_SUFFIX))
00265 ff_id3v1_read(s);
00266 avio_seek(s->pb, cur, SEEK_SET);
00267 }
00268
00269 return 0;
00270 }
00271
00272 static int wv_read_packet(AVFormatContext *s, AVPacket *pkt)
00273 {
00274 WVContext *wc = s->priv_data;
00275 int ret;
00276 int size, ver, off;
00277 int64_t pos;
00278 uint32_t block_samples;
00279
00280 if (url_feof(s->pb))
00281 return AVERROR_EOF;
00282 if (wc->block_parsed) {
00283 if ((ret = wv_read_block_header(s, s->pb, 0)) < 0)
00284 return ret;
00285 }
00286
00287 pos = wc->pos;
00288 off = wc->multichannel ? 4 : 0;
00289 if (av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE + off) < 0)
00290 return AVERROR(ENOMEM);
00291 if (wc->multichannel)
00292 AV_WL32(pkt->data, wc->blksize + WV_EXTRA_SIZE + 12);
00293 memcpy(pkt->data + off, wc->extra, WV_EXTRA_SIZE);
00294 ret = avio_read(s->pb, pkt->data + WV_EXTRA_SIZE + off, wc->blksize);
00295 if (ret != wc->blksize) {
00296 av_free_packet(pkt);
00297 return AVERROR(EIO);
00298 }
00299 while (!(wc->flags & WV_END_BLOCK)) {
00300 if (avio_rl32(s->pb) != MKTAG('w', 'v', 'p', 'k')) {
00301 av_free_packet(pkt);
00302 return AVERROR_INVALIDDATA;
00303 }
00304 if ((ret = av_append_packet(s->pb, pkt, 4)) < 0) {
00305 av_free_packet(pkt);
00306 return ret;
00307 }
00308 size = AV_RL32(pkt->data + pkt->size - 4);
00309 if (size < 24 || size > WV_BLOCK_LIMIT) {
00310 av_free_packet(pkt);
00311 av_log(s, AV_LOG_ERROR, "Incorrect block size %d\n", size);
00312 return AVERROR_INVALIDDATA;
00313 }
00314 wc->blksize = size;
00315 ver = avio_rl16(s->pb);
00316 if (ver < 0x402 || ver > 0x410) {
00317 av_free_packet(pkt);
00318 av_log(s, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
00319 return AVERROR_PATCHWELCOME;
00320 }
00321 avio_r8(s->pb);
00322 avio_r8(s->pb);
00323 wc->samples = avio_rl32(s->pb);
00324 wc->soff = avio_rl32(s->pb);
00325 if ((ret = av_append_packet(s->pb, pkt, WV_EXTRA_SIZE)) < 0) {
00326 av_free_packet(pkt);
00327 return ret;
00328 }
00329 memcpy(wc->extra, pkt->data + pkt->size - WV_EXTRA_SIZE, WV_EXTRA_SIZE);
00330
00331 if ((ret = wv_read_block_header(s, s->pb, 1)) < 0) {
00332 av_free_packet(pkt);
00333 return ret;
00334 }
00335 ret = av_append_packet(s->pb, pkt, wc->blksize);
00336 if (ret < 0) {
00337 av_free_packet(pkt);
00338 return ret;
00339 }
00340 }
00341 pkt->stream_index = 0;
00342 wc->block_parsed = 1;
00343 pkt->pts = wc->soff;
00344 block_samples = AV_RL32(wc->extra);
00345 if (block_samples > INT32_MAX)
00346 av_log(s, AV_LOG_WARNING,
00347 "Too many samples in block: %"PRIu32"\n", block_samples);
00348 else
00349 pkt->duration = block_samples;
00350
00351 av_add_index_entry(s->streams[0], pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
00352 return 0;
00353 }
00354
00355 static int wv_read_seek(AVFormatContext *s, int stream_index,
00356 int64_t timestamp, int flags)
00357 {
00358 AVStream *st = s->streams[stream_index];
00359 WVContext *wc = s->priv_data;
00360 AVPacket pkt1, *pkt = &pkt1;
00361 int ret;
00362 int index = av_index_search_timestamp(st, timestamp, flags);
00363 int64_t pos, pts;
00364
00365
00366 if (index >= 0 &&
00367 timestamp <= st->index_entries[st->nb_index_entries - 1].timestamp) {
00368 wc->block_parsed = 1;
00369 avio_seek(s->pb, st->index_entries[index].pos, SEEK_SET);
00370 return 0;
00371 }
00372
00373 if (timestamp < 0 || timestamp >= s->duration)
00374 return AVERROR(EINVAL);
00375
00376 pos = avio_tell(s->pb);
00377 do {
00378 ret = av_read_frame(s, pkt);
00379 if (ret < 0) {
00380 avio_seek(s->pb, pos, SEEK_SET);
00381 return ret;
00382 }
00383 pts = pkt->pts;
00384 av_free_packet(pkt);
00385 } while(pts < timestamp);
00386 return 0;
00387 }
00388
00389 AVInputFormat ff_wv_demuxer = {
00390 .name = "wv",
00391 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
00392 .priv_data_size = sizeof(WVContext),
00393 .read_probe = wv_probe,
00394 .read_header = wv_read_header,
00395 .read_packet = wv_read_packet,
00396 .read_seek = wv_read_seek,
00397 };