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