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){
00279 if(get_bits_left(gb) < t-1)
00280 goto error;
00281 t = get_bits(gb, t - 1) | (1 << (t-1));
00282 }else{
00283 if(get_bits_left(gb) < 0)
00284 goto error;
00285 }
00286 ctx->zeroes = t;
00287 if(ctx->zeroes){
00288 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
00289 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
00290 c->slow_level -= LEVEL_DECAY(c->slow_level);
00291 return 0;
00292 }
00293 }
00294 }
00295
00296 if(ctx->zero){
00297 t = 0;
00298 ctx->zero = 0;
00299 }else{
00300 t = get_unary_0_33(gb);
00301 if(get_bits_left(gb) < 0)
00302 goto error;
00303 if(t == 16) {
00304 t2 = get_unary_0_33(gb);
00305 if(t2 < 2){
00306 if(get_bits_left(gb) < 0)
00307 goto error;
00308 t += t2;
00309 }else{
00310 if(get_bits_left(gb) < t2 - 1)
00311 goto error;
00312 t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
00313 }
00314 }
00315
00316 if(ctx->one){
00317 ctx->one = t&1;
00318 t = (t>>1) + 1;
00319 }else{
00320 ctx->one = t&1;
00321 t >>= 1;
00322 }
00323 ctx->zero = !ctx->one;
00324 }
00325
00326 if(ctx->hybrid && !channel)
00327 update_error_limit(ctx);
00328
00329 if(!t){
00330 base = 0;
00331 add = GET_MED(0) - 1;
00332 DEC_MED(0);
00333 }else if(t == 1){
00334 base = GET_MED(0);
00335 add = GET_MED(1) - 1;
00336 INC_MED(0);
00337 DEC_MED(1);
00338 }else if(t == 2){
00339 base = GET_MED(0) + GET_MED(1);
00340 add = GET_MED(2) - 1;
00341 INC_MED(0);
00342 INC_MED(1);
00343 DEC_MED(2);
00344 }else{
00345 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
00346 add = GET_MED(2) - 1;
00347 INC_MED(0);
00348 INC_MED(1);
00349 INC_MED(2);
00350 }
00351 if(!c->error_limit){
00352 ret = base + get_tail(gb, add);
00353 if (get_bits_left(gb) <= 0)
00354 goto error;
00355 }else{
00356 int mid = (base*2 + add + 1) >> 1;
00357 while(add > c->error_limit){
00358 if(get_bits_left(gb) <= 0)
00359 goto error;
00360 if(get_bits1(gb)){
00361 add -= (mid - base);
00362 base = mid;
00363 }else
00364 add = mid - base - 1;
00365 mid = (base*2 + add + 1) >> 1;
00366 }
00367 ret = mid;
00368 }
00369 sign = get_bits1(gb);
00370 if(ctx->hybrid_bitrate)
00371 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
00372 return sign ? ~ret : ret;
00373
00374 error:
00375 *last = 1;
00376 return 0;
00377 }
00378
00379 static inline int wv_get_value_integer(WavpackContext *s, uint32_t *crc, int S)
00380 {
00381 int bit;
00382
00383 if(s->extra_bits){
00384 S <<= s->extra_bits;
00385
00386 if(s->got_extra_bits && get_bits_left(&s->gb_extra_bits) >= s->extra_bits){
00387 S |= get_bits(&s->gb_extra_bits, s->extra_bits);
00388 *crc = *crc * 9 + (S&0xffff) * 3 + ((unsigned)S>>16);
00389 }
00390 }
00391 bit = (S & s->and) | s->or;
00392 return (((S + bit) << s->shift) - bit) << s->post_shift;
00393 }
00394
00395 static float wv_get_value_float(WavpackContext *s, uint32_t *crc, int S)
00396 {
00397 union {
00398 float f;
00399 uint32_t u;
00400 } value;
00401
00402 int sign;
00403 int exp = s->float_max_exp;
00404
00405 if(s->got_extra_bits){
00406 const int max_bits = 1 + 23 + 8 + 1;
00407 const int left_bits = get_bits_left(&s->gb_extra_bits);
00408
00409 if(left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
00410 return 0.0;
00411 }
00412
00413 if(S){
00414 S <<= s->float_shift;
00415 sign = S < 0;
00416 if(sign)
00417 S = -S;
00418 if(S >= 0x1000000){
00419 if(s->got_extra_bits && get_bits1(&s->gb_extra_bits)){
00420 S = get_bits(&s->gb_extra_bits, 23);
00421 }else{
00422 S = 0;
00423 }
00424 exp = 255;
00425 }else if(exp){
00426 int shift = 23 - av_log2(S);
00427 exp = s->float_max_exp;
00428 if(exp <= shift){
00429 shift = --exp;
00430 }
00431 exp -= shift;
00432
00433 if(shift){
00434 S <<= shift;
00435 if((s->float_flag & WV_FLT_SHIFT_ONES) ||
00436 (s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SAME) && get_bits1(&s->gb_extra_bits)) ){
00437 S |= (1 << shift) - 1;
00438 } else if(s->got_extra_bits && (s->float_flag & WV_FLT_SHIFT_SENT)){
00439 S |= get_bits(&s->gb_extra_bits, shift);
00440 }
00441 }
00442 }else{
00443 exp = s->float_max_exp;
00444 }
00445 S &= 0x7fffff;
00446 }else{
00447 sign = 0;
00448 exp = 0;
00449 if(s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)){
00450 if(get_bits1(&s->gb_extra_bits)){
00451 S = get_bits(&s->gb_extra_bits, 23);
00452 if(s->float_max_exp >= 25)
00453 exp = get_bits(&s->gb_extra_bits, 8);
00454 sign = get_bits1(&s->gb_extra_bits);
00455 }else{
00456 if(s->float_flag & WV_FLT_ZERO_SIGN)
00457 sign = get_bits1(&s->gb_extra_bits);
00458 }
00459 }
00460 }
00461
00462 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
00463
00464 value.u = (sign << 31) | (exp << 23) | S;
00465 return value.f;
00466 }
00467
00468 static void wv_reset_saved_context(WavpackContext *s)
00469 {
00470 s->pos = 0;
00471 s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
00472 }
00473
00474 static inline int wv_unpack_stereo(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
00475 {
00476 int i, j, count = 0;
00477 int last, t;
00478 int A, B, L, L2, R, R2;
00479 int pos = s->pos;
00480 uint32_t crc = s->sc.crc;
00481 uint32_t crc_extra_bits = s->extra_sc.crc;
00482 int16_t *dst16 = dst;
00483 int32_t *dst32 = dst;
00484 float *dstfl = dst;
00485
00486 if(s->samples_left == s->samples)
00487 s->one = s->zero = s->zeroes = 0;
00488 do{
00489 L = wv_get_value(s, gb, 0, &last);
00490 if(last) break;
00491 R = wv_get_value(s, gb, 1, &last);
00492 if(last) break;
00493 for(i = 0; i < s->terms; i++){
00494 t = s->decorr[i].value;
00495 if(t > 0){
00496 if(t > 8){
00497 if(t & 1){
00498 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00499 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
00500 }else{
00501 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00502 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
00503 }
00504 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00505 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
00506 j = 0;
00507 }else{
00508 A = s->decorr[i].samplesA[pos];
00509 B = s->decorr[i].samplesB[pos];
00510 j = (pos + t) & 7;
00511 }
00512 if(type != SAMPLE_FMT_S16){
00513 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00514 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
00515 }else{
00516 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
00517 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
00518 }
00519 if(A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00520 if(B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
00521 s->decorr[i].samplesA[j] = L = L2;
00522 s->decorr[i].samplesB[j] = R = R2;
00523 }else if(t == -1){
00524 if(type != SAMPLE_FMT_S16)
00525 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
00526 else
00527 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
00528 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
00529 L = L2;
00530 if(type != SAMPLE_FMT_S16)
00531 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
00532 else
00533 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
00534 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
00535 R = R2;
00536 s->decorr[i].samplesA[0] = R;
00537 }else{
00538 if(type != SAMPLE_FMT_S16)
00539 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
00540 else
00541 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
00542 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
00543 R = R2;
00544
00545 if(t == -3){
00546 R2 = s->decorr[i].samplesA[0];
00547 s->decorr[i].samplesA[0] = R;
00548 }
00549
00550 if(type != SAMPLE_FMT_S16)
00551 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
00552 else
00553 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
00554 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
00555 L = L2;
00556 s->decorr[i].samplesB[0] = L;
00557 }
00558 }
00559 pos = (pos + 1) & 7;
00560 if(s->joint)
00561 L += (R -= (L >> 1));
00562 crc = (crc * 3 + L) * 3 + R;
00563
00564 if(type == SAMPLE_FMT_FLT){
00565 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
00566 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
00567 } else if(type == SAMPLE_FMT_S32){
00568 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
00569 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
00570 } else {
00571 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
00572 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
00573 }
00574 count++;
00575 }while(!last && count < s->max_samples);
00576
00577 if (last)
00578 s->samples_left = 0;
00579 else
00580 s->samples_left -= count;
00581 if(!s->samples_left){
00582 if(crc != s->CRC){
00583 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00584 return -1;
00585 }
00586 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
00587 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00588 return -1;
00589 }
00590 wv_reset_saved_context(s);
00591 }else{
00592 s->pos = pos;
00593 s->sc.crc = crc;
00594 s->sc.bits_used = get_bits_count(&s->gb);
00595 if(s->got_extra_bits){
00596 s->extra_sc.crc = crc_extra_bits;
00597 s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
00598 }
00599 }
00600 return count * 2;
00601 }
00602
00603 static inline int wv_unpack_mono(WavpackContext *s, GetBitContext *gb, void *dst, const int type)
00604 {
00605 int i, j, count = 0;
00606 int last, t;
00607 int A, S, T;
00608 int pos = s->pos;
00609 uint32_t crc = s->sc.crc;
00610 uint32_t crc_extra_bits = s->extra_sc.crc;
00611 int16_t *dst16 = dst;
00612 int32_t *dst32 = dst;
00613 float *dstfl = dst;
00614
00615 if(s->samples_left == s->samples)
00616 s->one = s->zero = s->zeroes = 0;
00617 do{
00618 T = wv_get_value(s, gb, 0, &last);
00619 S = 0;
00620 if(last) break;
00621 for(i = 0; i < s->terms; i++){
00622 t = s->decorr[i].value;
00623 if(t > 8){
00624 if(t & 1)
00625 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00626 else
00627 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00628 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00629 j = 0;
00630 }else{
00631 A = s->decorr[i].samplesA[pos];
00632 j = (pos + t) & 7;
00633 }
00634 if(type != SAMPLE_FMT_S16)
00635 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00636 else
00637 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
00638 if(A && T) s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00639 s->decorr[i].samplesA[j] = T = S;
00640 }
00641 pos = (pos + 1) & 7;
00642 crc = crc * 3 + S;
00643
00644 if(type == SAMPLE_FMT_FLT)
00645 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
00646 else if(type == SAMPLE_FMT_S32)
00647 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
00648 else
00649 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
00650 count++;
00651 }while(!last && count < s->samples);
00652
00653 if (last)
00654 s->samples_left = 0;
00655 else
00656 s->samples_left -= count;
00657 if(!s->samples_left){
00658 if(crc != s->CRC){
00659 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00660 return -1;
00661 }
00662 if(s->got_extra_bits && crc_extra_bits != s->crc_extra_bits){
00663 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00664 return -1;
00665 }
00666 wv_reset_saved_context(s);
00667 }else{
00668 s->pos = pos;
00669 s->sc.crc = crc;
00670 s->sc.bits_used = get_bits_count(&s->gb);
00671 if(s->got_extra_bits){
00672 s->extra_sc.crc = crc_extra_bits;
00673 s->extra_sc.bits_used = get_bits_count(&s->gb_extra_bits);
00674 }
00675 }
00676 return count;
00677 }
00678
00679 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
00680 {
00681 WavpackContext *s = avctx->priv_data;
00682
00683 s->avctx = avctx;
00684 s->stereo = (avctx->channels == 2);
00685 if(avctx->bits_per_coded_sample <= 16)
00686 avctx->sample_fmt = SAMPLE_FMT_S16;
00687 else
00688 avctx->sample_fmt = SAMPLE_FMT_S32;
00689 avctx->channel_layout = (avctx->channels==2) ? CH_LAYOUT_STEREO : CH_LAYOUT_MONO;
00690
00691 wv_reset_saved_context(s);
00692
00693 return 0;
00694 }
00695
00696 static int wavpack_decode_frame(AVCodecContext *avctx,
00697 void *data, int *data_size,
00698 AVPacket *avpkt)
00699 {
00700 const uint8_t *buf = avpkt->data;
00701 int buf_size = avpkt->size;
00702 WavpackContext *s = avctx->priv_data;
00703 void *samples = data;
00704 int samplecount;
00705 int got_terms = 0, got_weights = 0, got_samples = 0, got_entropy = 0, got_bs = 0, got_float = 0;
00706 int got_hybrid = 0;
00707 const uint8_t* buf_end = buf + buf_size;
00708 int i, j, id, size, ssize, weights, t;
00709 int bpp;
00710
00711 if (buf_size == 0){
00712 *data_size = 0;
00713 return 0;
00714 }
00715
00716 if(!s->samples_left){
00717 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00718 memset(s->ch, 0, sizeof(s->ch));
00719 s->extra_bits = 0;
00720 s->and = s->or = s->shift = 0;
00721 s->got_extra_bits = 0;
00722 }
00723
00724 s->samples = AV_RL32(buf); buf += 4;
00725 if(!s->samples){
00726 *data_size = 0;
00727 return buf_size;
00728 }
00729 s->frame_flags = AV_RL32(buf); buf += 4;
00730 if(s->frame_flags&0x80){
00731 bpp = sizeof(float);
00732 avctx->sample_fmt = SAMPLE_FMT_FLT;
00733 } else if((s->frame_flags&0x03) <= 1){
00734 bpp = 2;
00735 avctx->sample_fmt = SAMPLE_FMT_S16;
00736 } else {
00737 bpp = 4;
00738 avctx->sample_fmt = SAMPLE_FMT_S32;
00739 }
00740 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00741 s->joint = s->frame_flags & WV_JOINT_STEREO;
00742 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00743 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00744 s->post_shift = 8 * (bpp-1-(s->frame_flags&0x03)) + ((s->frame_flags >> 13) & 0x1f);
00745 s->CRC = AV_RL32(buf); buf += 4;
00746
00747 s->max_samples = *data_size / (bpp * avctx->channels);
00748 s->max_samples = FFMIN(s->max_samples, s->samples);
00749 if(s->samples_left > 0){
00750 s->max_samples = FFMIN(s->max_samples, s->samples_left);
00751 buf = buf_end;
00752 }
00753
00754
00755 while(buf < buf_end){
00756 id = *buf++;
00757 size = *buf++;
00758 if(id & WP_IDF_LONG) {
00759 size |= (*buf++) << 8;
00760 size |= (*buf++) << 16;
00761 }
00762 size <<= 1;
00763 ssize = size;
00764 if(id & WP_IDF_ODD) size--;
00765 if(size < 0){
00766 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00767 break;
00768 }
00769 if(buf + ssize > buf_end){
00770 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00771 break;
00772 }
00773 if(id & WP_IDF_IGNORE){
00774 buf += ssize;
00775 continue;
00776 }
00777 switch(id & WP_IDF_MASK){
00778 case WP_ID_DECTERMS:
00779 if(size > MAX_TERMS){
00780 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00781 s->terms = 0;
00782 buf += ssize;
00783 continue;
00784 }
00785 s->terms = size;
00786 for(i = 0; i < s->terms; i++) {
00787 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00788 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00789 buf++;
00790 }
00791 got_terms = 1;
00792 break;
00793 case WP_ID_DECWEIGHTS:
00794 if(!got_terms){
00795 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00796 continue;
00797 }
00798 weights = size >> s->stereo_in;
00799 if(weights > MAX_TERMS || weights > s->terms){
00800 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00801 buf += ssize;
00802 continue;
00803 }
00804 for(i = 0; i < weights; i++) {
00805 t = (int8_t)(*buf++);
00806 s->decorr[s->terms - i - 1].weightA = t << 3;
00807 if(s->decorr[s->terms - i - 1].weightA > 0)
00808 s->decorr[s->terms - i - 1].weightA += (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00809 if(s->stereo_in){
00810 t = (int8_t)(*buf++);
00811 s->decorr[s->terms - i - 1].weightB = t << 3;
00812 if(s->decorr[s->terms - i - 1].weightB > 0)
00813 s->decorr[s->terms - i - 1].weightB += (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00814 }
00815 }
00816 got_weights = 1;
00817 break;
00818 case WP_ID_DECSAMPLES:
00819 if(!got_terms){
00820 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00821 continue;
00822 }
00823 t = 0;
00824 for(i = s->terms - 1; (i >= 0) && (t < size); i--) {
00825 if(s->decorr[i].value > 8){
00826 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00827 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00828 if(s->stereo_in){
00829 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00830 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00831 t += 4;
00832 }
00833 t += 4;
00834 }else if(s->decorr[i].value < 0){
00835 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00836 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00837 t += 4;
00838 }else{
00839 for(j = 0; j < s->decorr[i].value; j++){
00840 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00841 if(s->stereo_in){
00842 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00843 }
00844 }
00845 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00846 }
00847 }
00848 got_samples = 1;
00849 break;
00850 case WP_ID_ENTROPY:
00851 if(size != 6 * (s->stereo_in + 1)){
00852 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, got %i", 6 * (s->stereo_in + 1), size);
00853 buf += ssize;
00854 continue;
00855 }
00856 for(j = 0; j <= s->stereo_in; j++){
00857 for(i = 0; i < 3; i++){
00858 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00859 buf += 2;
00860 }
00861 }
00862 got_entropy = 1;
00863 break;
00864 case WP_ID_HYBRID:
00865 if(s->hybrid_bitrate){
00866 for(i = 0; i <= s->stereo_in; i++){
00867 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00868 buf += 2;
00869 size -= 2;
00870 }
00871 }
00872 for(i = 0; i < (s->stereo_in + 1); i++){
00873 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00874 buf += 2;
00875 size -= 2;
00876 }
00877 if(size > 0){
00878 for(i = 0; i < (s->stereo_in + 1); i++){
00879 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00880 buf += 2;
00881 }
00882 }else{
00883 for(i = 0; i < (s->stereo_in + 1); i++)
00884 s->ch[i].bitrate_delta = 0;
00885 }
00886 got_hybrid = 1;
00887 break;
00888 case WP_ID_INT32INFO:
00889 if(size != 4){
00890 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00891 buf += ssize;
00892 continue;
00893 }
00894 if(buf[0])
00895 s->extra_bits = buf[0];
00896 else if(buf[1])
00897 s->shift = buf[1];
00898 else if(buf[2]){
00899 s->and = s->or = 1;
00900 s->shift = buf[2];
00901 }else if(buf[3]){
00902 s->and = 1;
00903 s->shift = buf[3];
00904 }
00905 buf += 4;
00906 break;
00907 case WP_ID_FLOATINFO:
00908 if(size != 4){
00909 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00910 buf += ssize;
00911 continue;
00912 }
00913 s->float_flag = buf[0];
00914 s->float_shift = buf[1];
00915 s->float_max_exp = buf[2];
00916 buf += 4;
00917 got_float = 1;
00918 break;
00919 case WP_ID_DATA:
00920 s->sc.offset = buf - avpkt->data;
00921 s->sc.size = size * 8;
00922 init_get_bits(&s->gb, buf, size * 8);
00923 s->data_size = size * 8;
00924 buf += size;
00925 got_bs = 1;
00926 break;
00927 case WP_ID_EXTRABITS:
00928 if(size <= 4){
00929 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n", size);
00930 buf += size;
00931 continue;
00932 }
00933 s->extra_sc.offset = buf - avpkt->data;
00934 s->extra_sc.size = size * 8;
00935 init_get_bits(&s->gb_extra_bits, buf, size * 8);
00936 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
00937 buf += size;
00938 s->got_extra_bits = 1;
00939 break;
00940 default:
00941 buf += size;
00942 }
00943 if(id & WP_IDF_ODD) buf++;
00944 }
00945 if(!s->samples_left){
00946 if(!got_terms){
00947 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
00948 return -1;
00949 }
00950 if(!got_weights){
00951 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
00952 return -1;
00953 }
00954 if(!got_samples){
00955 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
00956 return -1;
00957 }
00958 if(!got_entropy){
00959 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
00960 return -1;
00961 }
00962 if(s->hybrid && !got_hybrid){
00963 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
00964 return -1;
00965 }
00966 if(!got_bs){
00967 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
00968 return -1;
00969 }
00970 if(!got_float && avctx->sample_fmt == SAMPLE_FMT_FLT){
00971 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
00972 return -1;
00973 }
00974 if(s->got_extra_bits && avctx->sample_fmt != SAMPLE_FMT_FLT){
00975 const int size = get_bits_left(&s->gb_extra_bits);
00976 const int wanted = s->samples * s->extra_bits << s->stereo_in;
00977 if(size < wanted){
00978 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
00979 s->got_extra_bits = 0;
00980 }
00981 }
00982 s->samples_left = s->samples;
00983 }else{
00984 init_get_bits(&s->gb, avpkt->data + s->sc.offset, s->sc.size);
00985 skip_bits_long(&s->gb, s->sc.bits_used);
00986 if(s->got_extra_bits){
00987 init_get_bits(&s->gb_extra_bits, avpkt->data + s->extra_sc.offset,
00988 s->extra_sc.size);
00989 skip_bits_long(&s->gb_extra_bits, s->extra_sc.bits_used);
00990 }
00991 }
00992
00993 if(s->stereo_in){
00994 if(avctx->sample_fmt == SAMPLE_FMT_S16)
00995 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S16);
00996 else if(avctx->sample_fmt == SAMPLE_FMT_S32)
00997 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_S32);
00998 else
00999 samplecount = wv_unpack_stereo(s, &s->gb, samples, SAMPLE_FMT_FLT);
01000
01001 if (samplecount < 0)
01002 return -1;
01003
01004 }else{
01005 if(avctx->sample_fmt == SAMPLE_FMT_S16)
01006 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S16);
01007 else if(avctx->sample_fmt == SAMPLE_FMT_S32)
01008 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_S32);
01009 else
01010 samplecount = wv_unpack_mono(s, &s->gb, samples, SAMPLE_FMT_FLT);
01011
01012 if (samplecount < 0)
01013 return -1;
01014
01015 if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S16){
01016 int16_t *dst = (int16_t*)samples + samplecount * 2;
01017 int16_t *src = (int16_t*)samples + samplecount;
01018 int cnt = samplecount;
01019 while(cnt-- > 0){
01020 *--dst = *--src;
01021 *--dst = *src;
01022 }
01023 samplecount *= 2;
01024 }else if(s->stereo && avctx->sample_fmt == SAMPLE_FMT_S32){
01025 int32_t *dst = (int32_t*)samples + samplecount * 2;
01026 int32_t *src = (int32_t*)samples + samplecount;
01027 int cnt = samplecount;
01028 while(cnt-- > 0){
01029 *--dst = *--src;
01030 *--dst = *src;
01031 }
01032 samplecount *= 2;
01033 }else if(s->stereo){
01034 float *dst = (float*)samples + samplecount * 2;
01035 float *src = (float*)samples + samplecount;
01036 int cnt = samplecount;
01037 while(cnt-- > 0){
01038 *--dst = *--src;
01039 *--dst = *src;
01040 }
01041 samplecount *= 2;
01042 }
01043 }
01044 *data_size = samplecount * bpp;
01045
01046 return s->samples_left > 0 ? 0 : buf_size;
01047 }
01048
01049 AVCodec wavpack_decoder = {
01050 "wavpack",
01051 AVMEDIA_TYPE_AUDIO,
01052 CODEC_ID_WAVPACK,
01053 sizeof(WavpackContext),
01054 wavpack_decode_init,
01055 NULL,
01056 NULL,
01057 wavpack_decode_frame,
01058 .capabilities = CODEC_CAP_SUBFRAMES,
01059 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
01060 };