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 "get_bits.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 #define WV_FLT_SHIFT_ONES 0x01
00041 #define WV_FLT_SHIFT_SAME 0x02
00042 #define WV_FLT_SHIFT_SENT 0x04
00043 #define WV_FLT_ZERO_SENT 0x08
00044 #define WV_FLT_ZERO_SIGN 0x10
00045
00046 enum WP_ID_Flags{
00047 WP_IDF_MASK = 0x1F,
00048 WP_IDF_IGNORE = 0x20,
00049 WP_IDF_ODD = 0x40,
00050 WP_IDF_LONG = 0x80
00051 };
00052
00053 enum WP_ID{
00054 WP_ID_DUMMY = 0,
00055 WP_ID_ENCINFO,
00056 WP_ID_DECTERMS,
00057 WP_ID_DECWEIGHTS,
00058 WP_ID_DECSAMPLES,
00059 WP_ID_ENTROPY,
00060 WP_ID_HYBRID,
00061 WP_ID_SHAPING,
00062 WP_ID_FLOATINFO,
00063 WP_ID_INT32INFO,
00064 WP_ID_DATA,
00065 WP_ID_CORR,
00066 WP_ID_EXTRABITS,
00067 WP_ID_CHANINFO
00068 };
00069
00070 typedef struct SavedContext {
00071 int offset;
00072 int size;
00073 int bits_used;
00074 uint32_t crc;
00075 } SavedContext;
00076
00077 #define MAX_TERMS 16
00078
00079 typedef struct Decorr {
00080 int delta;
00081 int value;
00082 int weightA;
00083 int weightB;
00084 int samplesA[8];
00085 int samplesB[8];
00086 } Decorr;
00087
00088 typedef struct WvChannel {
00089 int median[3];
00090 int slow_level, error_limit;
00091 int bitrate_acc, bitrate_delta;
00092 } WvChannel;
00093
00094 typedef struct WavpackContext {
00095 AVCodecContext *avctx;
00096 int frame_flags;
00097 int stereo, stereo_in;
00098 int joint;
00099 uint32_t CRC;
00100 GetBitContext gb;
00101 int got_extra_bits;
00102 uint32_t crc_extra_bits;
00103 GetBitContext gb_extra_bits;
00104 int data_size;
00105 int samples;
00106 int terms;
00107 Decorr decorr[MAX_TERMS];
00108 int zero, one, zeroes;
00109 int extra_bits;
00110 int and, or, shift;
00111 int post_shift;
00112 int hybrid, hybrid_bitrate;
00113 int float_flag;
00114 int float_shift;
00115 int float_max_exp;
00116 WvChannel ch[2];
00117 int samples_left;
00118 int max_samples;
00119 int pos;
00120 SavedContext sc, extra_sc;
00121 } WavpackContext;
00122
00123
00124 static const uint8_t wp_exp2_table [256] = {
00125 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
00126 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
00127 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
00128 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
00129 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
00130 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
00131 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
00132 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
00133 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
00134 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
00135 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
00136 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
00137 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
00138 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
00139 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
00140 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
00141 };
00142
00143 static const uint8_t wp_log2_table [] = {
00144 0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
00145 0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
00146 0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
00147 0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
00148 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
00149 0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
00150 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
00151 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
00152 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
00153 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
00154 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
00155 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
00156 0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
00157 0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
00158 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
00159 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff
00160 };
00161
00162 static av_always_inline int wp_exp2(int16_t val)
00163 {
00164 int res, neg = 0;
00165
00166 if(val < 0){
00167 val = -val;
00168 neg = 1;
00169 }
00170
00171 res = wp_exp2_table[val & 0xFF] | 0x100;
00172 val >>= 8;
00173 res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
00174 return neg ? -res : res;
00175 }
00176
00177 static av_always_inline int wp_log2(int32_t val)
00178 {
00179 int bits;
00180
00181 if(!val)
00182 return 0;
00183 if(val == 1)
00184 return 256;
00185 val += val >> 9;
00186 bits = av_log2(val) + 1;
00187 if(bits < 9)
00188 return (bits << 8) + wp_log2_table[(val << (9 - bits)) & 0xFF];
00189 else
00190 return (bits << 8) + wp_log2_table[(val >> (bits - 9)) & 0xFF];
00191 }
00192
00193 #define LEVEL_DECAY(a) ((a + 0x80) >> 8)
00194
00195
00196 #define GET_MED(n) ((c->median[n] >> 4) + 1)
00197 #define DEC_MED(n) c->median[n] -= ((c->median[n] + (128>>n) - 2) / (128>>n)) * 2
00198 #define INC_MED(n) c->median[n] += ((c->median[n] + (128>>n)) / (128>>n)) * 5
00199
00200
00201 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
00202 if(samples && in){ \
00203 if((samples ^ in) < 0){ \
00204 weight -= delta; \
00205 if(weight < -1024) weight = -1024; \
00206 }else{ \
00207 weight += delta; \
00208 if(weight > 1024) weight = 1024; \
00209 } \
00210 }
00211
00212
00213 static av_always_inline int get_tail(GetBitContext *gb, int k)
00214 {
00215 int p, e, res;
00216
00217 if(k<1)return 0;
00218 p = av_log2(k);
00219 e = (1 << (p + 1)) - k - 1;
00220 res = p ? get_bits(gb, p) : 0;
00221 if(res >= e){
00222 res = (res<<1) - e + get_bits1(gb);
00223 }
00224 return res;
00225 }
00226
00227 static void update_error_limit(WavpackContext *ctx)
00228 {
00229 int i, br[2], sl[2];
00230
00231 for(i = 0; i <= ctx->stereo_in; i++){
00232 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
00233 br[i] = ctx->ch[i].bitrate_acc >> 16;
00234 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
00235 }
00236 if(ctx->stereo_in && ctx->hybrid_bitrate){
00237 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
00238 if(balance > br[0]){
00239 br[1] = br[0] << 1;
00240 br[0] = 0;
00241 }else if(-balance > br[0]){
00242 br[0] <<= 1;
00243 br[1] = 0;
00244 }else{
00245 br[1] = br[0] + balance;
00246 br[0] = br[0] - balance;
00247 }
00248 }
00249 for(i = 0; i <= ctx->stereo_in; i++){
00250 if(ctx->hybrid_bitrate){
00251 if(sl[i] - br[i] > -0x100)
00252 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
00253 else
00254 ctx->ch[i].error_limit = 0;
00255 }else{
00256 ctx->ch[i].error_limit = wp_exp2(br[i]);
00257 }
00258 }
00259 }
00260
00261 static int wv_get_value(WavpackContext *ctx, GetBitContext *gb, int channel, int *last)
00262 {
00263 int t, t2;
00264 int sign, base, add, ret;
00265 WvChannel *c = &ctx->ch[channel];
00266
00267 *last = 0;
00268
00269 if((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) && !ctx->zero && !ctx->one){
00270 if(ctx->zeroes){
00271 ctx->zeroes--;
00272 if(ctx->zeroes){
00273 c->slow_level -= LEVEL_DECAY(c->slow_level);
00274 return 0;
00275 }
00276 }else{
00277 t = get_unary_0_33(gb);
00278 if(t >= 2) t = get_bits(gb, t - 1) | (1 << (t-1));
00279 ctx->zeroes = t;
00280 if(ctx->zeroes){
00281 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
00282 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
00283 c->slow_level -= LEVEL_DECAY(c->slow_level);
00284 return 0;
00285 }
00286 }
00287 }
00288
00289 if(get_bits_count(gb) >= ctx->data_size){
00290 *last = 1;
00291 return 0;
00292 }
00293
00294 if(ctx->zero){
00295 t = 0;
00296 ctx->zero = 0;
00297 }else{
00298 t = get_unary_0_33(gb);
00299 if(get_bits_count(gb) >= ctx->data_size){
00300 *last = 1;
00301 return 0;
00302 }
00303 if(t == 16) {
00304 t2 = get_unary_0_33(gb);
00305 if(t2 < 2) t += t2;
00306 else t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
00307 }
00308
00309 if(ctx->one){
00310 ctx->one = t&1;
00311 t = (t>>1) + 1;
00312 }else{
00313 ctx->one = t&1;
00314 t >>= 1;
00315 }
00316 ctx->zero = !ctx->one;
00317 }
00318
00319 if(ctx->hybrid && !channel)
00320 update_error_limit(ctx);
00321
00322 if(!t){
00323 base = 0;
00324 add = GET_MED(0) - 1;
00325 DEC_MED(0);
00326 }else if(t == 1){
00327 base = GET_MED(0);
00328 add = GET_MED(1) - 1;
00329 INC_MED(0);
00330 DEC_MED(1);
00331 }else if(t == 2){
00332 base = GET_MED(0) + GET_MED(1);
00333 add = GET_MED(2) - 1;
00334 INC_MED(0);
00335 INC_MED(1);
00336 DEC_MED(2);
00337 }else{
00338 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
00339 add = GET_MED(2) - 1;
00340 INC_MED(0);
00341 INC_MED(1);
00342 INC_MED(2);
00343 }
00344 if(!c->error_limit){
00345 ret = base + get_tail(gb, add);
00346 }else{
00347 int mid = (base*2 + add + 1) >> 1;
00348 while(add > c->error_limit){
00349 if(get_bits1(gb)){
00350 add -= (mid - base);
00351 base = mid;
00352 }else
00353 add = mid - base - 1;
00354 mid = (base*2 + add + 1) >> 1;
00355 }
00356 ret = mid;
00357 }
00358 sign = get_bits1(gb);
00359 if(ctx->hybrid_bitrate)
00360 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
00361 return sign ? ~ret : ret;
00362 }
00363
00364 static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S)
00365 {
00366 int bit;
00367
00368 if(s->extra_bits){
00369 S <<= s->extra_bits;
00370
00371 if(s->got_extra_bits){
00372 S |= get_bits(&s->gb_extra_bits, s->extra_bits);
00373 *crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
00374 }
00375 }
00376 bit = (S & s->and) | s->or;
00377 return (((S + bit) << s->shift) - bit) << s->post_shift;
00378 }
00379
00380 static float wv_get_value_float(WavpackContext *s, uint32_t *crc, int S)
00381 {
00382 union {
00383 float f;
00384 uint32_t u;
00385 } value;
00386
00387 int sign;
00388 int exp = s->float_max_exp;
00389
00390 if(s->got_extra_bits){
00391 const int max_bits = 1 + 23 + 8 + 1;
00392 const int left_bits = get_bits_left(&s->gb_extra_bits);
00393
00394 if(left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
00395 return 0.0;
00396 }
00397
00398 if(S){
00399 S <<= s->float_shift;
00400 sign = S < 0;
00401 if(sign)
00402 S = -S;
00403 if(S >= 0x1000000){
00404 if(s->got_extra_bits && get_bits1(&s->gb_extra_bits)){
00405 S = get_bits(&s->gb_extra_bits, 23);
00406 }else{
00407 S = 0;
00408 }
00409 exp = 255;
00410 }else if(exp){
00411 int shift = 23 - av_log2(S);
00412 exp = s->float_max_exp;
00413 if(exp <= shift){
00414 shift = --exp;
00415 }
00416 exp -= shift;
00417
00418 if(shift){
00419 S <<= shift;
00420 if((s->float_flag & WV_FLT_SHIFT_ONES) ||
00421 (s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) && get_bits1(&s->gb_extra_bits)) ){
00422 S |= (1 << shift) - 1;
00423 } else if(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SENT)){
00424 S |= get_bits(&s->gb_extra_bits, shift);
00425 }
00426 }
00427 }else{
00428 exp = s->float_max_exp;
00429 }
00430 S &= 0x7fffff;
00431 }else{
00432 sign = 0;
00433 exp = 0;
00434 if(s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)){
00435 if(get_bits1(&s->gb_extra_bits)){
00436 S = get_bits(&s->gb_extra_bits, 23);
00437 if(s->float_max_exp >= 25)
00438 exp = get_bits(&s->gb_extra_bits, 8);
00439 sign = get_bits1(&s->gb_extra_bits);
00440 }else{
00441 if(s->float_flag & WV_FLT_ZERO_SIGN)
00442 sign = get_bits1(&s->gb_extra_bits);
00443 }
00444 }
00445 }
00446
00447 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
00448
00449 value.u = (sign << 31) | (exp << 23) | S;
00450 return value.f;
00451 }
00452
00453 static void wv_reset_saved_context(WavpackContext *s)
00454 {
00455 s->pos = 0;
00456 s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
00457 }
00458
00459 static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
00460 {
00461 int i, j, count = 0;
00462 int last, t;
00463 int A, B, L, L2, R, R2;
00464 int pos = s->pos;
00465 uint32_t crc = s->sc.crc;
00466 uint32_t crc_extra_bits = s->extra_sc.crc;
00467 int16_t *dst16 = dst;
00468 int32_t *dst32 = dst;
00469 float *dstfl = dst;
00470
00471 if(s->samples_left == s->samples)
00472 s->one = s->zero = s->zeroes = 0;
00473 do{
00474 L = wv_get_value(s, gb, 0, &last);
00475 if(last) break;
00476 R = wv_get_value(s, gb, 1, &last);
00477 if(last) break;
00478 for(i = 0; i < s->terms; i++){
00479 t = s->decorr[i].value;
00480 if(t > 0){
00481 if(t > 8){
00482 if(t & 1){
00483 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00484 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
00485 }else{
00486 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00487 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
00488 }
00489 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00490 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
00491 j = 0;
00492 }else{
00493 A = s->decorr[i].samplesA[pos];
00494 B = s->decorr[i].samplesB[pos];
00495 j = (pos + t) & 7;
00496 }
00497 if(type != AV_SAMPLE_FMT_S16){
00498 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00499 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
00500 }else{
00501 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
00502 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
00503 }
00504 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00505 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
00506 s->decorr[i].samplesA[j] = L = L2;
00507 s->decorr[i].samplesB[j] = R = R2;
00508 }else if(t == -1){
00509 if(type != AV_SAMPLE_FMT_S16)
00510 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
00511 else
00512 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
00513 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
00514 L = L2;
00515 if(type != AV_SAMPLE_FMT_S16)
00516 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
00517 else
00518 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
00519 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
00520 R = R2;
00521 s->decorr[i].samplesA[0] = R;
00522 }else{
00523 if(type != AV_SAMPLE_FMT_S16)
00524 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
00525 else
00526 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
00527 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
00528 R = R2;
00529
00530 if(t == -3){
00531 R2 = s->decorr[i].samplesA[0];
00532 s->decorr[i].samplesA[0] = R;
00533 }
00534
00535 if(type != AV_SAMPLE_FMT_S16)
00536 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
00537 else
00538 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
00539 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
00540 L = L2;
00541 s->decorr[i].samplesB[0] = L;
00542 }
00543 }
00544 pos = (pos + 1) & 7;
00545 if(s->joint)
00546 L += (R -= (L >> 1));
00547 crc = (crc * 3 + L) * 3 + R;
00548
00549 if(type == AV_SAMPLE_FMT_FLT){
00550 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
00551 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
00552 } else if(type == AV_SAMPLE_FMT_S32){
00553 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
00554 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
00555 } else {
00556 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
00557 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
00558 }
00559 count++;
00560 }while(!last && count < s->max_samples);
00561
00562 s->samples_left -= count;
00563 if(!s->samples_left){
00564 if(crc != s->CRC){
00565 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00566 return -1;
00567 }
00568 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
00569 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00570 return -1;
00571 }
00572 wv_reset_saved_context(s);
00573 }else{
00574 s->pos = pos;
00575 s->sc.crc = crc;
00576 s->sc.bits_used = get_bits_count(&s->gb);
00577 if(s->got_extra_bits){
00578 s->extra_sc.crc = crc_extra_bits;
00579 s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
00580 }
00581 }
00582 return count * 2;
00583 }
00584
00585 static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
00586 {
00587 int i, j, count = 0;
00588 int last, t;
00589 int A, S, T;
00590 int pos = s->pos;
00591 uint32_t crc = s->sc.crc;
00592 uint32_t crc_extra_bits = s->extra_sc.crc;
00593 int16_t *dst16 = dst;
00594 int32_t *dst32 = dst;
00595 float *dstfl = dst;
00596
00597 if(s->samples_left == s->samples)
00598 s->one = s->zero = s->zeroes = 0;
00599 do{
00600 T = wv_get_value(s, gb, 0, &last);
00601 S = 0;
00602 if(last) break;
00603 for(i = 0; i < s->terms; i++){
00604 t = s->decorr[i].value;
00605 if(t > 8){
00606 if(t & 1)
00607 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00608 else
00609 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00610 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00611 j = 0;
00612 }else{
00613 A = s->decorr[i].samplesA[pos];
00614 j = (pos + t) & 7;
00615 }
00616 if(type != AV_SAMPLE_FMT_S16)
00617 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00618 else
00619 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
00620 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00621 s->decorr[i].samplesA[j] = T = S;
00622 }
00623 pos = (pos + 1) & 7;
00624 crc = crc * 3 + S;
00625
00626 if(type == AV_SAMPLE_FMT_FLT)
00627 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
00628 else if(type == AV_SAMPLE_FMT_S32)
00629 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
00630 else
00631 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
00632 count++;
00633 }while(!last && count < s->max_samples);
00634
00635 s->samples_left -= count;
00636 if(!s->samples_left){
00637 if(crc != s->CRC){
00638 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00639 return -1;
00640 }
00641 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
00642 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00643 return -1;
00644 }
00645 wv_reset_saved_context(s);
00646 }else{
00647 s->pos = pos;
00648 s->sc.crc = crc;
00649 s->sc.bits_used = get_bits_count(&s->gb);
00650 if(s->got_extra_bits){
00651 s->extra_sc.crc = crc_extra_bits;
00652 s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
00653 }
00654 }
00655 return count;
00656 }
00657
00658 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
00659 {
00660 WavpackContext *s = avctx->priv_data;
00661
00662 s->avctx = avctx;
00663 if (avctx->channels > 2) {
00664 av_log(avctx, AV_LOG_ERROR, "Multichannel WavPack is not supported yet.\n");
00665 return -1;
00666 }
00667 s->stereo = (avctx->channels == 2);
00668 if(avctx->bits_per_coded_sample <= 16)
00669 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00670 else
00671 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00672 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00673
00674 wv_reset_saved_context(s);
00675
00676 return 0;
00677 }
00678
00679 static int wavpack_decode_frame(AVCodecContext *avctx,
00680 void *data, int *data_size,
00681 AVPacket *avpkt)
00682 {
00683 const uint8_t *buf = avpkt->data;
00684 int buf_size = avpkt->size;
00685 WavpackContext *s = avctx->priv_data;
00686 void *samples = data;
00687 int samplecount;
00688 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
00689 int got_hybrid = 0;
00690 const uint8_t* buf_end = buf + buf_size;
00691 int i, j, id, size, ssize, weights, t;
00692 int bpp;
00693
00694 if (buf_size == 0){
00695 *data_size = 0;
00696 return 0;
00697 }
00698
00699 if(!s->samples_left){
00700 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00701 memset(s->ch, 0, sizeof(s->ch));
00702 s->extra_bits = 0;
00703 s->and = s->or = s->shift = 0;
00704 s->got_extra_bits = 0;
00705 }
00706
00707 s->samples = AV_RL32(buf); buf += 4;
00708 if(!s->samples){
00709 *data_size = 0;
00710 return buf_size;
00711 }
00712 s->frame_flags = AV_RL32(buf); buf += 4;
00713 if(s->frame_flags&0x80){
00714 bpp = sizeof(float);
00715 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00716 } else if((s->frame_flags&0x03) <= 1){
00717 bpp = 2;
00718 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00719 } else {
00720 bpp = 4;
00721 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00722 }
00723 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00724 s->joint = s->frame_flags & WV_JOINT_STEREO;
00725 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00726 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00727 s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
00728 s->CRC = AV_RL32(buf); buf += 4;
00729
00730 s->max_samples = *data_size / (bpp * avctx->channels);
00731 s->max_samples = FFMIN(s->max_samples, s->samples);
00732 if(s->samples_left > 0){
00733 s->max_samples = FFMIN(s->max_samples, s->samples_left);
00734 buf = buf_end;
00735 }
00736
00737
00738 while(buf < buf_end){
00739 id = *buf++;
00740 size = *buf++;
00741 if(id & WP_IDF_LONG) {
00742 size |= (*buf++) << 8;
00743 size |= (*buf++) << 16;
00744 }
00745 size <<= 1;
00746 ssize = size;
00747 if(id & WP_IDF_ODD) size--;
00748 if(size < 0){
00749 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00750 break;
00751 }
00752 if(buf + ssize > buf_end){
00753 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00754 break;
00755 }
00756 if(id & WP_IDF_IGNORE){
00757 buf += ssize;
00758 continue;
00759 }
00760 switch(id & WP_IDF_MASK){
00761 case WP_ID_DECTERMS:
00762 s->terms = size;
00763 if(s->terms > MAX_TERMS){
00764 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00765 buf += ssize;
00766 continue;
00767 }
00768 for(i = 0; i < s->terms; i++) {
00769 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00770 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00771 buf++;
00772 }
00773 got_terms = 1;
00774 break;
00775 case WP_ID_DECWEIGHTS:
00776 if(!got_terms){
00777 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00778 continue;
00779 }
00780 weights = size >> s->stereo_in;
00781 if(weights > MAX_TERMS || weights > s->terms){
00782 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00783 buf += ssize;
00784 continue;
00785 }
00786 for(i = 0; i < weights; i++) {
00787 t = (int8_t)(*buf++);
00788 s->decorr[s->terms - i - 1].weightA = t << 3;
00789 if(s->decorr[s->terms - i - 1].weightA > 0)
00790 s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00791 if(s->stereo_in){
00792 t = (int8_t)(*buf++);
00793 s->decorr[s->terms - i - 1].weightB = t << 3;
00794 if(s->decorr[s->terms - i - 1].weightB > 0)
00795 s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00796 }
00797 }
00798 got_weights = 1;
00799 break;
00800 case WP_ID_DECSAMPLES:
00801 if(!got_terms){
00802 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00803 continue;
00804 }
00805 t = 0;
00806 for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
00807 if(s->decorr[i].value > 8){
00808 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00809 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00810 if(s->stereo_in){
00811 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00812 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00813 t += 4;
00814 }
00815 t += 4;
00816 }else if(s->decorr[i].value < 0){
00817 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00818 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00819 t += 4;
00820 }else{
00821 for(j = 0; j < s->decorr[i].value; j++){
00822 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00823 if(s->stereo_in){
00824 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00825 }
00826 }
00827 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00828 }
00829 }
00830 got_samples = 1;
00831 break;
00832 case WP_ID_ENTROPY:
00833 if(size != 6 * (s->stereo_in + 1)){
00834 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
00835 buf += ssize;
00836 continue;
00837 }
00838 for(j = 0; j <= s->stereo_in; j++){
00839 for(i = 0; i < 3; i++){
00840 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00841 buf += 2;
00842 }
00843 }
00844 got_entropy = 1;
00845 break;
00846 case WP_ID_HYBRID:
00847 if(s->hybrid_bitrate){
00848 for(i = 0; i <= s->stereo_in; i++){
00849 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00850 buf += 2;
00851 size -= 2;
00852 }
00853 }
00854 for(i = 0; i < (s->stereo_in + 1); i++){
00855 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00856 buf += 2;
00857 size -= 2;
00858 }
00859 if(size > 0){
00860 for(i = 0; i < (s->stereo_in + 1); i++){
00861 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00862 buf += 2;
00863 }
00864 }else{
00865 for(i = 0; i < (s->stereo_in + 1); i++)
00866 s->ch[i].bitrate_delta = 0;
00867 }
00868 got_hybrid = 1;
00869 break;
00870 case WP_ID_INT32INFO:
00871 if(size != 4){
00872 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00873 buf += ssize;
00874 continue;
00875 }
00876 if(buf[0])
00877 s->extra_bits = buf[0];
00878 else if(buf[1])
00879 s->shift = buf[1];
00880 else if(buf[2]){
00881 s->and = s->or = 1;
00882 s->shift = buf[2];
00883 }else if(buf[3]){
00884 s->and = 1;
00885 s->shift = buf[3];
00886 }
00887 buf += 4;
00888 break;
00889 case WP_ID_FLOATINFO:
00890 if(size != 4){
00891 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00892 buf += ssize;
00893 continue;
00894 }
00895 s->float_flag = buf[0];
00896 s->float_shift = buf[1];
00897 s->float_max_exp = buf[2];
00898 buf += 4;
00899 got_float = 1;
00900 break;
00901 case WP_ID_DATA:
00902 s->sc.offset = buf - avpkt->data;
00903 s->sc.size = size * 8;
00904 init_get_bits(&s->gb, buf, size * 8);
00905 s->data_size = size * 8;
00906 buf += size;
00907 got_bs = 1;
00908 break;
00909 case WP_ID_EXTRABITS:
00910 if(size <= 4){
00911 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
00912 buf += size;
00913 continue;
00914 }
00915 s->extra_sc.offset = buf - avpkt->data;
00916 s->extra_sc.size = size * 8;
00917 init_get_bits(&s->gb_extra_bits, buf, size * 8);
00918 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
00919 buf += size;
00920 s->got_extra_bits = 1;
00921 break;
00922 default:
00923 buf += size;
00924 }
00925 if(id & WP_IDF_ODD) buf++;
00926 }
00927 if(!s->samples_left){
00928 if(!got_terms){
00929 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
00930 return -1;
00931 }
00932 if(!got_weights){
00933 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
00934 return -1;
00935 }
00936 if(!got_samples){
00937 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
00938 return -1;
00939 }
00940 if(!got_entropy){
00941 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
00942 return -1;
00943 }
00944 if(s->hybrid && !got_hybrid){
00945 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
00946 return -1;
00947 }
00948 if(!got_bs){
00949 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
00950 return -1;
00951 }
00952 if(!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT){
00953 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
00954 return -1;
00955 }
00956 if(s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT){
00957 const int size = get_bits_left(&s->gb_extra_bits);
00958 const int wanted = s->samples * s->extra_bits << s->stereo_in;
00959 if(size < wanted){
00960 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
00961 s->got_extra_bits = 0;
00962 }
00963 }
00964 s->samples_left = s->samples;
00965 }else{
00966 init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
00967 skip_bits_long(&s->gb, s->sc.bits_used);
00968 if(s->got_extra_bits){
00969 init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
00970 s->extra_sc.size);
00971 skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
00972 }
00973 }
00974
00975 if(s->stereo_in){
00976 if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
00977 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
00978 else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00979 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
00980 else
00981 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
00982
00983 }else{
00984 if(avctx->sample_fmt == AV_SAMPLE_FMT_S16)
00985 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
00986 else if(avctx->sample_fmt == AV_SAMPLE_FMT_S32)
00987 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
00988 else
00989 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
00990
00991 if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16){
00992 int16_t *dst = (int16_t*)samples + samplecount * 2;
00993 int16_t *src = (int16_t*)samples + samplecount;
00994 int cnt = samplecount;
00995 while(cnt--){
00996 *--dst = *--src;
00997 *--dst = *src;
00998 }
00999 samplecount *= 2;
01000 }else if(s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32){
01001 int32_t *dst = (int32_t*)samples + samplecount * 2;
01002 int32_t *src = (int32_t*)samples + samplecount;
01003 int cnt = samplecount;
01004 while(cnt--){
01005 *--dst = *--src;
01006 *--dst = *src;
01007 }
01008 samplecount *= 2;
01009 }else if(s->stereo){
01010 float *dst = (float*)samples + samplecount * 2;
01011 float *src = (float*)samples + samplecount;
01012 int cnt = samplecount;
01013 while(cnt--){
01014 *--dst = *--src;
01015 *--dst = *src;
01016 }
01017 samplecount *= 2;
01018 }
01019 }
01020 *data_size = samplecount * bpp;
01021
01022 return s->samples_left > 0 ? 0 : buf_size;
01023 }
01024
01025 AVCodec wavpack_decoder = {
01026 "wavpack",
01027 AVMEDIA_TYPE_AUDIO,
01028 CODEC_ID_WAVPACK,
01029 sizeof(WavpackContext),
01030 wavpack_decode_init,
01031 NULL,
01032 NULL,
01033 wavpack_decode_frame,
01034 .capabilities = CODEC_CAP_SUBFRAMES,
01035 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
01036 };