00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define ALT_BITSTREAM_READER_LE
00022 #include "avcodec.h"
00023 #include "bitstream.h"
00024 #include "unary.h"
00025
00031 #define WV_MONO 0x00000004
00032 #define WV_JOINT_STEREO 0x00000010
00033 #define WV_FALSE_STEREO 0x40000000
00034
00035 #define WV_HYBRID_MODE 0x00000008
00036 #define WV_HYBRID_SHAPE 0x00000008
00037 #define WV_HYBRID_BITRATE 0x00000200
00038 #define WV_HYBRID_BALANCE 0x00000400
00039
00040 enum WP_ID_Flags{
00041 WP_IDF_MASK = 0x1F,
00042 WP_IDF_IGNORE = 0x20,
00043 WP_IDF_ODD = 0x40,
00044 WP_IDF_LONG = 0x80
00045 };
00046
00047 enum WP_ID{
00048 WP_ID_DUMMY = 0,
00049 WP_ID_ENCINFO,
00050 WP_ID_DECTERMS,
00051 WP_ID_DECWEIGHTS,
00052 WP_ID_DECSAMPLES,
00053 WP_ID_ENTROPY,
00054 WP_ID_HYBRID,
00055 WP_ID_SHAPING,
00056 WP_ID_FLOATINFO,
00057 WP_ID_INT32INFO,
00058 WP_ID_DATA,
00059 WP_ID_CORR,
00060 WP_ID_FLT,
00061 WP_ID_CHANINFO
00062 };
00063
00064 #define MAX_TERMS 16
00065
00066 typedef struct Decorr {
00067 int delta;
00068 int value;
00069 int weightA;
00070 int weightB;
00071 int samplesA[8];
00072 int samplesB[8];
00073 } Decorr;
00074
00075 typedef struct WvChannel {
00076 int median[3];
00077 int slow_level, error_limit;
00078 int bitrate_acc, bitrate_delta;
00079 } WvChannel;
00080
00081 typedef struct WavpackContext {
00082 AVCodecContext *avctx;
00083 int frame_flags;
00084 int stereo, stereo_in;
00085 int joint;
00086 uint32_t CRC;
00087 GetBitContext gb;
00088 int data_size;
00089 int samples;
00090 int terms;
00091 Decorr decorr[MAX_TERMS];
00092 int zero, one, zeroes;
00093 int and, or, shift;
00094 int hybrid, hybrid_bitrate;
00095 WvChannel ch[2];
00096 } WavpackContext;
00097
00098
00099 static const uint8_t wp_exp2_table [256] = {
00100 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
00101 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
00102 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
00103 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
00104 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
00105 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
00106 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
00107 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00108 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00109 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
00110 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
00111 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
00112 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
00113 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
00114 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
00115 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
00116 };
00117
00118 static const uint8_t wp_log2_table [] = {
00119 0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
00120 0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
00121 0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
00122 0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
00123 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
00124 0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
00125 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
00126 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
00127 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
00128 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
00129 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
00130 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
00131 0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
00132 0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
00133 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
00134 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff
00135 };
00136
00137 static av_always_inline int wp_exp2(int16_t val)
00138 {
00139 int res, neg = 0;
00140
00141 if(val < 0){
00142 val = -val;
00143 neg = 1;
00144 }
00145
00146 res = wp_exp2_table[val & 0xFF] | 0x100;
00147 val >>= 8;
00148 res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
00149 return neg ? -res : res;
00150 }
00151
00152 static av_always_inline int wp_log2(int32_t val)
00153 {
00154 int bits;
00155
00156 if(!val)
00157 return 0;
00158 if(val == 1)
00159 return 256;
00160 val += val >> 9;
00161 bits = av_log2(val) + 1;
00162 if(bits < 9)
00163 return (bits << 8) + wp_log2_table[(val << (9 - bits)) & 0xFF];
00164 else
00165 return (bits << 8) + wp_log2_table[(val >> (bits - 9)) & 0xFF];
00166 }
00167
00168 #define LEVEL_DECAY(a) ((a + 0x80) >> 8)
00169
00170
00171 #define GET_MED(n) ((c->median[n] >> 4) + 1)
00172 #define DEC_MED(n) c->median[n] -= ((c->median[n] + (128>>n) - 2) / (128>>n)) * 2
00173 #define INC_MED(n) c->median[n] += ((c->median[n] + (128>>n)) / (128>>n)) * 5
00174
00175
00176 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
00177 if(samples && in){ \
00178 if((samples ^ in) < 0){ \
00179 weight -= delta; \
00180 if(weight < -1024) weight = -1024; \
00181 }else{ \
00182 weight += delta; \
00183 if(weight > 1024) weight = 1024; \
00184 } \
00185 }
00186
00187
00188 static av_always_inline int get_tail(GetBitContext *gb, int k)
00189 {
00190 int p, e, res;
00191
00192 if(k<1)return 0;
00193 p = av_log2(k);
00194 e = (1 << (p + 1)) - k - 1;
00195 res = p ? get_bits(gb, p) : 0;
00196 if(res >= e){
00197 res = (res<<1) - e + get_bits1(gb);
00198 }
00199 return res;
00200 }
00201
00202 static void update_error_limit(WavpackContext *ctx)
00203 {
00204 int i, br[2], sl[2];
00205
00206 for(i = 0; i <= ctx->stereo_in; i++){
00207 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
00208 br[i] = ctx->ch[i].bitrate_acc >> 16;
00209 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
00210 }
00211 if(ctx->stereo_in && ctx->hybrid_bitrate){
00212 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
00213 if(balance > br[0]){
00214 br[1] = br[0] << 1;
00215 br[0] = 0;
00216 }else if(-balance > br[0]){
00217 br[0] <<= 1;
00218 br[1] = 0;
00219 }else{
00220 br[1] = br[0] + balance;
00221 br[0] = br[0] - balance;
00222 }
00223 }
00224 for(i = 0; i <= ctx->stereo_in; i++){
00225 if(ctx->hybrid_bitrate){
00226 if(sl[i] - br[i] > -0x100)
00227 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
00228 else
00229 ctx->ch[i].error_limit = 0;
00230 }else{
00231 ctx->ch[i].error_limit = wp_exp2(br[i]);
00232 }
00233 }
00234 }
00235
00236 static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int *last)
00237 {
00238 int t, t2;
00239 int sign, base, add, ret;
00240 WvChannel *c = &ctx->ch[channel];
00241
00242 *last = 0;
00243
00244 if((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) && !ctx->zero && !ctx->one){
00245 if(ctx->zeroes){
00246 ctx->zeroes--;
00247 if(ctx->zeroes){
00248 c->slow_level -= LEVEL_DECAY(c->slow_level);
00249 return 0;
00250 }
00251 }else{
00252 t = get_unary_0_33(gb);
00253 if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1));
00254 ctx->zeroes = t;
00255 if(ctx->zeroes){
00256 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
00257 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
00258 c->slow_level -= LEVEL_DECAY(c->slow_level);
00259 return 0;
00260 }
00261 }
00262 }
00263
00264 if(get_bits_count(gb) >= ctx->data_size){
00265 *last = 1;
00266 return 0;
00267 }
00268
00269 if(ctx->zero){
00270 t = 0;
00271 ctx->zero = 0;
00272 }else{
00273 t = get_unary_0_33(gb);
00274 if(get_bits_count(gb) >= ctx->data_size){
00275 *last = 1;
00276 return 0;
00277 }
00278 if(t == 16) {
00279 t2 = get_unary_0_33(gb);
00280 if(t2 < 2) t += t2;
00281 else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
00282 }
00283
00284 if(ctx->one){
00285 ctx->one = t&1;
00286 t = (t>>1) + 1;
00287 }else{
00288 ctx->one = t&1;
00289 t >>= 1;
00290 }
00291 ctx->zero = !ctx->one;
00292 }
00293
00294 if(ctx->hybrid && !channel)
00295 update_error_limit(ctx);
00296
00297 if(!t){
00298 base = 0;
00299 add = GET_MED(0) - 1;
00300 DEC_MED(0);
00301 }else if(t == 1){
00302 base = GET_MED(0);
00303 add = GET_MED(1) - 1;
00304 INC_MED(0);
00305 DEC_MED(1);
00306 }else if(t == 2){
00307 base = GET_MED(0) + GET_MED(1);
00308 add = GET_MED(2) - 1;
00309 INC_MED(0);
00310 INC_MED(1);
00311 DEC_MED(2);
00312 }else{
00313 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
00314 add = GET_MED(2) - 1;
00315 INC_MED(0);
00316 INC_MED(1);
00317 INC_MED(2);
00318 }
00319 if(!c->error_limit){
00320 ret = base + get_tail(gb, add);
00321 }else{
00322 int mid = (base*2 + add + 1) >> 1;
00323 while(add > c->error_limit){
00324 if(get_bits1(gb)){
00325 add -= (mid - base);
00326 base = mid;
00327 }else
00328 add = mid - base - 1;
00329 mid = (base*2 + add + 1) >> 1;
00330 }
00331 ret = mid;
00332 }
00333 sign = get_bits1(gb);
00334 if(ctx->hybrid_bitrate)
00335 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
00336 return sign ? ~ret : ret;
00337 }
00338
00339 static int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, int16_t *dst)
00340 {
00341 int i, j, count = 0;
00342 int last, t;
00343 int A, B, L, L2, R, R2, bit;
00344 int pos = 0;
00345 uint32_t crc = 0xFFFFFFFF;
00346
00347 s->one = s->zero = s->zeroes = 0;
00348 do{
00349 L = wv_get_value(s, gb, 0, &last);
00350 if(last) break;
00351 R = wv_get_value(s, gb, 1, &last);
00352 if(last) break;
00353 for(i = 0; i < s->terms; i++){
00354 t = s->decorr[i].value;
00355 j = 0;
00356 if(t > 0){
00357 if(t > 8){
00358 if(t & 1){
00359 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00360 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
00361 }else{
00362 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00363 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
00364 }
00365 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00366 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
00367 j = 0;
00368 }else{
00369 A = s->decorr[i].samplesA[pos];
00370 B = s->decorr[i].samplesB[pos];
00371 j = (pos + t) & 7;
00372 }
00373 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
00374 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
00375 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00376 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
00377 s->decorr[i].samplesA[j] = L = L2;
00378 s->decorr[i].samplesB[j] = R = R2;
00379 }else if(t == -1){
00380 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
00381 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
00382 L = L2;
00383 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
00384 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
00385 R = R2;
00386 s->decorr[i].samplesA[0] = R;
00387 }else{
00388 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
00389 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
00390 R = R2;
00391
00392 if(t == -3){
00393 R2 = s->decorr[i].samplesA[0];
00394 s->decorr[i].samplesA[0] = R;
00395 }
00396
00397 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
00398 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
00399 L = L2;
00400 s->decorr[i].samplesB[0] = L;
00401 }
00402 }
00403 pos = (pos + 1) & 7;
00404 if(s->joint)
00405 L += (R -= (L >> 1));
00406 crc = (crc * 3 + L) * 3 + R;
00407 bit = (L & s->and) | s->or;
00408 *dst++ = ((L + bit) << s->shift) - bit;
00409 bit = (R & s->and) | s->or;
00410 *dst++ = ((R + bit) << s->shift) - bit;
00411 count++;
00412 }while(!last && count < s->samples);
00413
00414 if(crc != s->CRC){
00415 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00416 return -1;
00417 }
00418 return count * 2;
00419 }
00420
00421 static int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, int16_t *dst)
00422 {
00423 int i, j, count = 0;
00424 int last, t;
00425 int A, S, T, bit;
00426 int pos = 0;
00427 uint32_t crc = 0xFFFFFFFF;
00428
00429 s->one = s->zero = s->zeroes = 0;
00430 do{
00431 T = wv_get_value(s, gb, 0, &last);
00432 S = 0;
00433 if(last) break;
00434 for(i = 0; i < s->terms; i++){
00435 t = s->decorr[i].value;
00436 if(t > 8){
00437 if(t & 1)
00438 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00439 else
00440 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00441 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00442 j = 0;
00443 }else{
00444 A = s->decorr[i].samplesA[pos];
00445 j = (pos + t) & 7;
00446 }
00447 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
00448 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00449 s->decorr[i].samplesA[j] = T = S;
00450 }
00451 pos = (pos + 1) & 7;
00452 crc = crc * 3 + S;
00453 bit = (S & s->and) | s->or;
00454 *dst++ = ((S + bit) << s->shift) - bit;
00455 count++;
00456 }while(!last && count < s->samples);
00457
00458 if(crc != s->CRC){
00459 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00460 return -1;
00461 }
00462 return count;
00463 }
00464
00465 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
00466 {
00467 WavpackContext *s = avctx->priv_data;
00468
00469 s->avctx = avctx;
00470 s->stereo = (avctx->channels == 2);
00471 avctx->sample_fmt = SAMPLE_FMT_S16;
00472 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00473
00474 return 0;
00475 }
00476
00477 static int wavpack_decode_frame(AVCodecContext *avctx,
00478 void *data, int *data_size,
00479 const uint8_t *buf, int buf_size)
00480 {
00481 WavpackContext *s = avctx->priv_data;
00482 int16_t *samples = data;
00483 int samplecount;
00484 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0;
00485 int got_hybrid = 0;
00486 const uint8_t* buf_end = buf + buf_size;
00487 int i, j, id, size, ssize, weights, t;
00488
00489 if (buf_size == 0){
00490 *data_size = 0;
00491 return 0;
00492 }
00493
00494 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00495 memset(s->ch, 0, sizeof(s->ch));
00496 s->and = s->or = s->shift = 0;
00497
00498 s->samples = AV_RL32(buf); buf += 4;
00499 if(!s->samples){
00500 *data_size = 0;
00501 return buf_size;
00502 }
00503
00504 if(s->samples * 2 * avctx->channels > *data_size){
00505 av_log(avctx, AV_LOG_ERROR, "Packet size is too big to be handled in lavc!\n");
00506 return -1;
00507 }
00508 s->frame_flags = AV_RL32(buf); buf += 4;
00509 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00510 s->joint = s->frame_flags & WV_JOINT_STEREO;
00511 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00512 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00513 s->CRC = AV_RL32(buf); buf += 4;
00514
00515 while(buf < buf_end){
00516 id = *buf++;
00517 size = *buf++;
00518 if(id & WP_IDF_LONG) {
00519 size |= (*buf++) << 8;
00520 size |= (*buf++) << 16;
00521 }
00522 size <<= 1;
00523 ssize = size;
00524 if(id & WP_IDF_ODD) size--;
00525 if(size < 0){
00526 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00527 break;
00528 }
00529 if(buf + ssize > buf_end){
00530 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00531 break;
00532 }
00533 if(id & WP_IDF_IGNORE){
00534 buf += ssize;
00535 continue;
00536 }
00537 switch(id & WP_IDF_MASK){
00538 case WP_ID_DECTERMS:
00539 if(size > MAX_TERMS){
00540 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00541 s->terms = 0;
00542 buf += ssize;
00543 continue;
00544 }
00545 s->terms = size;
00546 for(i = 0; i < s->terms; i++) {
00547 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00548 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00549 buf++;
00550 }
00551 got_terms = 1;
00552 break;
00553 case WP_ID_DECWEIGHTS:
00554 if(!got_terms){
00555 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00556 continue;
00557 }
00558 weights = size >> s->stereo_in;
00559 if(weights > MAX_TERMS || weights > s->terms){
00560 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00561 buf += ssize;
00562 continue;
00563 }
00564 for(i = 0; i < weights; i++) {
00565 t = (int8_t)(*buf++);
00566 s->decorr[s->terms - i - 1].weightA = t << 3;
00567 if(s->decorr[s->terms - i - 1].weightA > 0)
00568 s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00569 if(s->stereo_in){
00570 t = (int8_t)(*buf++);
00571 s->decorr[s->terms - i - 1].weightB = t << 3;
00572 if(s->decorr[s->terms - i - 1].weightB > 0)
00573 s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00574 }
00575 }
00576 got_weights = 1;
00577 break;
00578 case WP_ID_DECSAMPLES:
00579 if(!got_terms){
00580 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00581 continue;
00582 }
00583 t = 0;
00584 for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
00585 if(s->decorr[i].value > 8){
00586 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00587 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00588 if(s->stereo_in){
00589 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00590 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00591 t += 4;
00592 }
00593 t += 4;
00594 }else if(s->decorr[i].value < 0){
00595 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00596 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00597 t += 4;
00598 }else{
00599 for(j = 0; j < s->decorr[i].value; j++){
00600 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00601 if(s->stereo_in){
00602 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00603 }
00604 }
00605 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00606 }
00607 }
00608 got_samples = 1;
00609 break;
00610 case WP_ID_ENTROPY:
00611 if(size != 6 * (s->stereo_in + 1)){
00612 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
00613 buf += ssize;
00614 continue;
00615 }
00616 for(j = 0; j <= s->stereo_in; j++){
00617 for(i = 0; i < 3; i++){
00618 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00619 buf += 2;
00620 }
00621 }
00622 got_entropy = 1;
00623 break;
00624 case WP_ID_HYBRID:
00625 if(s->hybrid_bitrate){
00626 for(i = 0; i <= s->stereo_in; i++){
00627 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00628 buf += 2;
00629 size -= 2;
00630 }
00631 }
00632 for(i = 0; i < (s->stereo_in + 1); i++){
00633 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00634 buf += 2;
00635 size -= 2;
00636 }
00637 if(size > 0){
00638 for(i = 0; i < (s->stereo_in + 1); i++){
00639 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00640 buf += 2;
00641 }
00642 }else{
00643 for(i = 0; i < (s->stereo_in + 1); i++)
00644 s->ch[i].bitrate_delta = 0;
00645 }
00646 got_hybrid = 1;
00647 break;
00648 case WP_ID_INT32INFO:
00649 if(size != 4 || *buf){
00650 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00651 buf += ssize;
00652 continue;
00653 }
00654 if(buf[1])
00655 s->shift = buf[1];
00656 else if(buf[2]){
00657 s->and = s->or = 1;
00658 s->shift = buf[2];
00659 }else if(buf[3]){
00660 s->and = 1;
00661 s->shift = buf[3];
00662 }
00663 buf += 4;
00664 break;
00665 case WP_ID_DATA:
00666 init_get_bits(&s->gb, buf, size * 8);
00667 s->data_size = size * 8;
00668 buf += size;
00669 got_bs = 1;
00670 break;
00671 default:
00672 buf += size;
00673 }
00674 if(id & WP_IDF_ODD) buf++;
00675 }
00676 if(!got_terms){
00677 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
00678 return -1;
00679 }
00680 if(!got_weights){
00681 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
00682 return -1;
00683 }
00684 if(!got_samples){
00685 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
00686 return -1;
00687 }
00688 if(!got_entropy){
00689 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
00690 return -1;
00691 }
00692 if(s->hybrid && !got_hybrid){
00693 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
00694 return -1;
00695 }
00696 if(!got_bs){
00697 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
00698 return -1;
00699 }
00700
00701 if(s->stereo_in)
00702 samplecount = wv_unpack_stereo(s, &s->gb, samples);
00703 else{
00704 samplecount = wv_unpack_mono(s, &s->gb, samples);
00705 if(s->stereo){
00706 int16_t *dst = samples + samplecount * 2;
00707 int16_t *src = samples + samplecount;
00708 int cnt = samplecount;
00709 while(cnt--){
00710 *--dst = *--src;
00711 *--dst = *src;
00712 }
00713 samplecount *= 2;
00714 }
00715 }
00716 *data_size = samplecount * 2;
00717
00718 return buf_size;
00719 }
00720
00721 AVCodec wavpack_decoder = {
00722 "wavpack",
00723 CODEC_TYPE_AUDIO,
00724 CODEC_ID_WAVPACK,
00725 sizeof(WavpackContext),
00726 wavpack_decode_init,
00727 NULL,
00728 NULL,
00729 wavpack_decode_frame,
00730 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
00731 };