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 != 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 != 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 != 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 != 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 != 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 == 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 == 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 != 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 == SAMPLE_FMT_FLT)
00627 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
00628 else if(type == 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->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 s->stereo = (avctx->channels == 2);
00664 if(avctx->bits_per_coded_sample <= 16)
00665 avctx->sample_fmt = SAMPLE_FMT_S16;
00666 else
00667 avctx->sample_fmt = SAMPLE_FMT_S32;
00668 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00669
00670 wv_reset_saved_context(s);
00671
00672 return 0;
00673 }
00674
00675 static int wavpack_decode_frame(AVCodecContext *avctx,
00676 void *data, int *data_size,
00677 AVPacket *avpkt)
00678 {
00679 const uint8_t *buf = avpkt->data;
00680 int buf_size = avpkt->size;
00681 WavpackContext *s = avctx->priv_data;
00682 void *samples = data;
00683 int samplecount;
00684 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
00685 int got_hybrid = 0;
00686 const uint8_t* buf_end = buf + buf_size;
00687 int i, j, id, size, ssize, weights, t;
00688 int bpp;
00689
00690 if (buf_size == 0){
00691 *data_size = 0;
00692 return 0;
00693 }
00694
00695 if(!s->samples_left){
00696 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00697 memset(s->ch, 0, sizeof(s->ch));
00698 s->extra_bits = 0;
00699 s->and = s->or = s->shift = 0;
00700 s->got_extra_bits = 0;
00701 }
00702
00703 s->samples = AV_RL32(buf); buf += 4;
00704 if(!s->samples){
00705 *data_size = 0;
00706 return buf_size;
00707 }
00708 s->frame_flags = AV_RL32(buf); buf += 4;
00709 if(s->frame_flags&0x80){
00710 bpp = sizeof(float);
00711 avctx->sample_fmt = SAMPLE_FMT_FLT;
00712 } else if((s->frame_flags&0x03) <= 1){
00713 bpp = 2;
00714 avctx->sample_fmt = SAMPLE_FMT_S16;
00715 } else {
00716 bpp = 4;
00717 avctx->sample_fmt = SAMPLE_FMT_S32;
00718 }
00719 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00720 s->joint = s->frame_flags & WV_JOINT_STEREO;
00721 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00722 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00723 s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
00724 s->CRC = AV_RL32(buf); buf += 4;
00725
00726 s->max_samples = *data_size / (bpp * avctx->channels);
00727 s->max_samples = FFMIN(s->max_samples, s->samples);
00728 if(s->samples_left > 0){
00729 s->max_samples = FFMIN(s->max_samples, s->samples_left);
00730 buf = buf_end;
00731 }
00732
00733
00734 while(buf < buf_end){
00735 id = *buf++;
00736 size = *buf++;
00737 if(id & WP_IDF_LONG) {
00738 size |= (*buf++) << 8;
00739 size |= (*buf++) << 16;
00740 }
00741 size <<= 1;
00742 ssize = size;
00743 if(id & WP_IDF_ODD) size--;
00744 if(size < 0){
00745 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00746 break;
00747 }
00748 if(buf + ssize > buf_end){
00749 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00750 break;
00751 }
00752 if(id & WP_IDF_IGNORE){
00753 buf += ssize;
00754 continue;
00755 }
00756 switch(id & WP_IDF_MASK){
00757 case WP_ID_DECTERMS:
00758 s->terms = size;
00759 if(s->terms > MAX_TERMS){
00760 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00761 buf += ssize;
00762 continue;
00763 }
00764 for(i = 0; i < s->terms; i++) {
00765 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00766 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00767 buf++;
00768 }
00769 got_terms = 1;
00770 break;
00771 case WP_ID_DECWEIGHTS:
00772 if(!got_terms){
00773 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00774 continue;
00775 }
00776 weights = size >> s->stereo_in;
00777 if(weights > MAX_TERMS || weights > s->terms){
00778 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00779 buf += ssize;
00780 continue;
00781 }
00782 for(i = 0; i < weights; i++) {
00783 t = (int8_t)(*buf++);
00784 s->decorr[s->terms - i - 1].weightA = t << 3;
00785 if(s->decorr[s->terms - i - 1].weightA > 0)
00786 s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00787 if(s->stereo_in){
00788 t = (int8_t)(*buf++);
00789 s->decorr[s->terms - i - 1].weightB = t << 3;
00790 if(s->decorr[s->terms - i - 1].weightB > 0)
00791 s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00792 }
00793 }
00794 got_weights = 1;
00795 break;
00796 case WP_ID_DECSAMPLES:
00797 if(!got_terms){
00798 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00799 continue;
00800 }
00801 t = 0;
00802 for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
00803 if(s->decorr[i].value > 8){
00804 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00805 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00806 if(s->stereo_in){
00807 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00808 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00809 t += 4;
00810 }
00811 t += 4;
00812 }else if(s->decorr[i].value < 0){
00813 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00814 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00815 t += 4;
00816 }else{
00817 for(j = 0; j < s->decorr[i].value; j++){
00818 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00819 if(s->stereo_in){
00820 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00821 }
00822 }
00823 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00824 }
00825 }
00826 got_samples = 1;
00827 break;
00828 case WP_ID_ENTROPY:
00829 if(size != 6 * (s->stereo_in + 1)){
00830 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
00831 buf += ssize;
00832 continue;
00833 }
00834 for(j = 0; j <= s->stereo_in; j++){
00835 for(i = 0; i < 3; i++){
00836 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00837 buf += 2;
00838 }
00839 }
00840 got_entropy = 1;
00841 break;
00842 case WP_ID_HYBRID:
00843 if(s->hybrid_bitrate){
00844 for(i = 0; i <= s->stereo_in; i++){
00845 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00846 buf += 2;
00847 size -= 2;
00848 }
00849 }
00850 for(i = 0; i < (s->stereo_in + 1); i++){
00851 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00852 buf += 2;
00853 size -= 2;
00854 }
00855 if(size > 0){
00856 for(i = 0; i < (s->stereo_in + 1); i++){
00857 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00858 buf += 2;
00859 }
00860 }else{
00861 for(i = 0; i < (s->stereo_in + 1); i++)
00862 s->ch[i].bitrate_delta = 0;
00863 }
00864 got_hybrid = 1;
00865 break;
00866 case WP_ID_INT32INFO:
00867 if(size != 4){
00868 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00869 buf += ssize;
00870 continue;
00871 }
00872 if(buf[0])
00873 s->extra_bits = buf[0];
00874 else if(buf[1])
00875 s->shift = buf[1];
00876 else if(buf[2]){
00877 s->and = s->or = 1;
00878 s->shift = buf[2];
00879 }else if(buf[3]){
00880 s->and = 1;
00881 s->shift = buf[3];
00882 }
00883 buf += 4;
00884 break;
00885 case WP_ID_FLOATINFO:
00886 if(size != 4){
00887 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00888 buf += ssize;
00889 continue;
00890 }
00891 s->float_flag = buf[0];
00892 s->float_shift = buf[1];
00893 s->float_max_exp = buf[2];
00894 buf += 4;
00895 got_float = 1;
00896 break;
00897 case WP_ID_DATA:
00898 s->sc.offset = buf - avpkt->data;
00899 s->sc.size = size * 8;
00900 init_get_bits(&s->gb, buf, size * 8);
00901 s->data_size = size * 8;
00902 buf += size;
00903 got_bs = 1;
00904 break;
00905 case WP_ID_EXTRABITS:
00906 if(size <= 4){
00907 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
00908 buf += size;
00909 continue;
00910 }
00911 s->extra_sc.offset = buf - avpkt->data;
00912 s->extra_sc.size = size * 8;
00913 init_get_bits(&s->gb_extra_bits, buf, size * 8);
00914 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
00915 buf += size;
00916 s->got_extra_bits = 1;
00917 break;
00918 default:
00919 buf += size;
00920 }
00921 if(id & WP_IDF_ODD) buf++;
00922 }
00923 if(!s->samples_left){
00924 if(!got_terms){
00925 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
00926 return -1;
00927 }
00928 if(!got_weights){
00929 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
00930 return -1;
00931 }
00932 if(!got_samples){
00933 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
00934 return -1;
00935 }
00936 if(!got_entropy){
00937 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
00938 return -1;
00939 }
00940 if(s->hybrid && !got_hybrid){
00941 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
00942 return -1;
00943 }
00944 if(!got_bs){
00945 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
00946 return -1;
00947 }
00948 if(!got_float && avctx->sample_fmt == SAMPLE_FMT_FLT){
00949 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
00950 return -1;
00951 }
00952 if(s->got_extra_bits && avctx->sample_fmt != SAMPLE_FMT_FLT){
00953 const int size = get_bits_left(&s->gb_extra_bits);
00954 const int wanted = s->samples * s->extra_bits << s->stereo_in;
00955 if(size < wanted){
00956 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
00957 s->got_extra_bits = 0;
00958 }
00959 }
00960 s->samples_left = s->samples;
00961 }else{
00962 init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
00963 skip_bits_long(&s->gb, s->sc.bits_used);
00964 if(s->got_extra_bits){
00965 init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
00966 s->extra_sc.size);
00967 skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
00968 }
00969 }
00970
00971 if(s->stereo_in){
00972 if(avctx->sample_fmt == SAMPLE_FMT_S16)
00973 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S16);
00974 else if(avctx->sample_fmt == SAMPLE_FMT_S32)
00975 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S32);
00976 else
00977 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT);
00978
00979 }else{
00980 if(avctx->sample_fmt == SAMPLE_FMT_S16)
00981 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16);
00982 else if(avctx->sample_fmt == SAMPLE_FMT_S32)
00983 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S32);
00984 else
00985 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT);
00986
00987 if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){
00988 int16_t *dst = (int16_t*)samples + samplecount * 2;
00989 int16_t *src = (int16_t*)samples + samplecount;
00990 int cnt = samplecount;
00991 while(cnt--){
00992 *--dst = *--src;
00993 *--dst = *src;
00994 }
00995 samplecount *= 2;
00996 }else if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S32){
00997 int32_t *dst = (int32_t*)samples + samplecount * 2;
00998 int32_t *src = (int32_t*)samples + samplecount;
00999 int cnt = samplecount;
01000 while(cnt--){
01001 *--dst = *--src;
01002 *--dst = *src;
01003 }
01004 samplecount *= 2;
01005 }else if(s->stereo){
01006 float *dst = (float*)samples + samplecount * 2;
01007 float *src = (float*)samples + samplecount;
01008 int cnt = samplecount;
01009 while(cnt--){
01010 *--dst = *--src;
01011 *--dst = *src;
01012 }
01013 samplecount *= 2;
01014 }
01015 }
01016 *data_size = samplecount * bpp;
01017
01018 return s->samples_left > 0 ? 0 : buf_size;
01019 }
01020
01021 AVCodec wavpack_decoder = {
01022 "wavpack",
01023 AVMEDIA_TYPE_AUDIO,
01024 CODEC_ID_WAVPACK,
01025 sizeof(WavpackContext),
01026 wavpack_decode_init,
01027 NULL,
01028 NULL,
01029 wavpack_decode_frame,
01030 .capabilities = CODEC_CAP_SUBFRAMES,
01031 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
01032 };