00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avformat.h"
00024 #include "apetag.h"
00025 #include "id3v1.h"
00026
00027
00028 #define WV_BLOCK_LIMIT 1047576
00029
00030 #define WV_EXTRA_SIZE 12
00031
00032 enum WV_FLAGS{
00033 WV_MONO = 0x0004,
00034 WV_HYBRID = 0x0008,
00035 WV_JOINT = 0x0010,
00036 WV_CROSSD = 0x0020,
00037 WV_HSHAPE = 0x0040,
00038 WV_FLOAT = 0x0080,
00039 WV_INT32 = 0x0100,
00040 WV_HBR = 0x0200,
00041 WV_HBAL = 0x0400,
00042 WV_MCINIT = 0x0800,
00043 WV_MCEND = 0x1000,
00044 };
00045
00046 static const int wv_rates[16] = {
00047 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
00048 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
00049 };
00050
00051 typedef struct{
00052 uint32_t blksize, flags;
00053 int rate, chan, bpp;
00054 uint32_t samples, soff;
00055 int block_parsed;
00056 uint8_t extra[WV_EXTRA_SIZE];
00057 int64_t pos;
00058 }WVContext;
00059
00060 static int wv_probe(AVProbeData *p)
00061 {
00062
00063 if (p->buf_size <= 32)
00064 return 0;
00065 if (p->buf[0] == 'w' && p->buf[1] == 'v' &&
00066 p->buf[2] == 'p' && p->buf[3] == 'k')
00067 return AVPROBE_SCORE_MAX;
00068 else
00069 return 0;
00070 }
00071
00072 static int wv_read_block_header(AVFormatContext *ctx, ByteIOContext *pb)
00073 {
00074 WVContext *wc = ctx->priv_data;
00075 uint32_t tag, ver;
00076 int size;
00077 int rate, bpp, chan;
00078
00079 wc->pos = url_ftell(pb);
00080 tag = get_le32(pb);
00081 if (tag != MKTAG('w', 'v', 'p', 'k'))
00082 return -1;
00083 size = get_le32(pb);
00084 if(size < 24 || size > WV_BLOCK_LIMIT){
00085 av_log(ctx, AV_LOG_ERROR, "Incorrect block size %i\n", size);
00086 return -1;
00087 }
00088 wc->blksize = size;
00089 ver = get_le16(pb);
00090 if(ver < 0x402 || ver > 0x410){
00091 av_log(ctx, AV_LOG_ERROR, "Unsupported version %03X\n", ver);
00092 return -1;
00093 }
00094 get_byte(pb);
00095 get_byte(pb);
00096 wc->samples = get_le32(pb);
00097 wc->soff = get_le32(pb);
00098 get_buffer(pb, wc->extra, WV_EXTRA_SIZE);
00099 wc->flags = AV_RL32(wc->extra + 4);
00100
00101 bpp = ((wc->flags & 3) + 1) << 3;
00102 chan = 1 + !(wc->flags & WV_MONO);
00103 rate = wv_rates[(wc->flags >> 23) & 0xF];
00104 if((wc->flags & 0x1800) != 0x1800){
00105 av_log(ctx, AV_LOG_ERROR, "Multichannel WavPack is not supported yet.\n");
00106 return -1;
00107 }
00108 if(rate == -1 && !wc->block_parsed){
00109 int64_t block_end = url_ftell(pb) + wc->blksize - 24;
00110 if(url_is_streamed(pb)){
00111 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
00112 return -1;
00113 }
00114 while(url_ftell(pb) < block_end){
00115 int id, size;
00116 id = get_byte(pb);
00117 size = (id & 0x80) ? get_le24(pb) : get_byte(pb);
00118 size <<= 1;
00119 if(id&0x40)
00120 size--;
00121 if((id&0x3F) == 0x27){
00122 rate = get_le24(pb);
00123 break;
00124 }else{
00125 url_fskip(pb, size);
00126 }
00127 }
00128 if(rate == -1){
00129 av_log(ctx, AV_LOG_ERROR, "Cannot determine custom sampling rate\n");
00130 return -1;
00131 }
00132 url_fseek(pb, block_end - wc->blksize + 24, SEEK_SET);
00133 }
00134 if(!wc->bpp) wc->bpp = bpp;
00135 if(!wc->chan) wc->chan = chan;
00136 if(!wc->rate) wc->rate = rate;
00137
00138 if(wc->flags && bpp != wc->bpp){
00139 av_log(ctx, AV_LOG_ERROR, "Bits per sample differ, this block: %i, header block: %i\n", bpp, wc->bpp);
00140 return -1;
00141 }
00142 if(wc->flags && chan != wc->chan){
00143 av_log(ctx, AV_LOG_ERROR, "Channels differ, this block: %i, header block: %i\n", chan, wc->chan);
00144 return -1;
00145 }
00146 if(wc->flags && rate != -1 && rate != wc->rate){
00147 av_log(ctx, AV_LOG_ERROR, "Sampling rate differ, this block: %i, header block: %i\n", rate, wc->rate);
00148 return -1;
00149 }
00150 wc->blksize = size - 24;
00151 return 0;
00152 }
00153
00154 static int wv_read_header(AVFormatContext *s,
00155 AVFormatParameters *ap)
00156 {
00157 ByteIOContext *pb = s->pb;
00158 WVContext *wc = s->priv_data;
00159 AVStream *st;
00160
00161 wc->block_parsed = 0;
00162 if(wv_read_block_header(s, pb) < 0)
00163 return -1;
00164
00165
00166 st = av_new_stream(s, 0);
00167 if (!st)
00168 return -1;
00169 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00170 st->codec->codec_id = CODEC_ID_WAVPACK;
00171 st->codec->channels = wc->chan;
00172 st->codec->sample_rate = wc->rate;
00173 st->codec->bits_per_coded_sample = wc->bpp;
00174 av_set_pts_info(st, 64, 1, wc->rate);
00175 st->start_time = 0;
00176 st->duration = wc->samples;
00177
00178 if(!url_is_streamed(s->pb)) {
00179 int64_t cur = url_ftell(s->pb);
00180 ff_ape_parse_tag(s);
00181 if(!av_metadata_get(s->metadata, "", NULL, AV_METADATA_IGNORE_SUFFIX))
00182 ff_id3v1_read(s);
00183 url_fseek(s->pb, cur, SEEK_SET);
00184 }
00185
00186 return 0;
00187 }
00188
00189 static int wv_read_packet(AVFormatContext *s,
00190 AVPacket *pkt)
00191 {
00192 WVContext *wc = s->priv_data;
00193 int ret;
00194
00195 if (url_feof(s->pb))
00196 return AVERROR(EIO);
00197 if(wc->block_parsed){
00198 if(wv_read_block_header(s, s->pb) < 0)
00199 return -1;
00200 }
00201
00202 if(av_new_packet(pkt, wc->blksize + WV_EXTRA_SIZE) < 0)
00203 return AVERROR(ENOMEM);
00204 memcpy(pkt->data, wc->extra, WV_EXTRA_SIZE);
00205 ret = get_buffer(s->pb, pkt->data + WV_EXTRA_SIZE, wc->blksize);
00206 if(ret != wc->blksize){
00207 av_free_packet(pkt);
00208 return AVERROR(EIO);
00209 }
00210 pkt->stream_index = 0;
00211 wc->block_parsed = 1;
00212 pkt->size = ret + WV_EXTRA_SIZE;
00213 pkt->pts = wc->soff;
00214 av_add_index_entry(s->streams[0], wc->pos, pkt->pts, 0, 0, AVINDEX_KEYFRAME);
00215 return 0;
00216 }
00217
00218 static int wv_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00219 {
00220 AVStream *st = s->streams[stream_index];
00221 WVContext *wc = s->priv_data;
00222 AVPacket pkt1, *pkt = &pkt1;
00223 int ret;
00224 int index = av_index_search_timestamp(st, timestamp, flags);
00225 int64_t pos, pts;
00226
00227
00228 if (index >= 0){
00229 wc->block_parsed = 1;
00230 url_fseek(s->pb, st->index_entries[index].pos, SEEK_SET);
00231 return 0;
00232 }
00233
00234 if(timestamp < 0 || timestamp >= s->duration)
00235 return -1;
00236
00237 pos = url_ftell(s->pb);
00238 do{
00239 ret = av_read_frame(s, pkt);
00240 if (ret < 0){
00241 url_fseek(s->pb, pos, SEEK_SET);
00242 return -1;
00243 }
00244 pts = pkt->pts;
00245 av_free_packet(pkt);
00246 }while(pts < timestamp);
00247 return 0;
00248 }
00249
00250 AVInputFormat wv_demuxer = {
00251 "wv",
00252 NULL_IF_CONFIG_SMALL("WavPack"),
00253 sizeof(WVContext),
00254 wv_probe,
00255 wv_read_header,
00256 wv_read_packet,
00257 NULL,
00258 wv_read_seek,
00259 };