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