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 unsigned 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_check_crc(WavpackFrameContext *s, uint32_t crc,
00504 uint32_t crc_extra_bits)
00505 {
00506 if (crc != s->CRC) {
00507 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
00508 return AVERROR_INVALIDDATA;
00509 }
00510 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
00511 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
00512 return AVERROR_INVALIDDATA;
00513 }
00514
00515 return 0;
00516 }
00517
00518 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
00519 void *dst, const int type)
00520 {
00521 int i, j, count = 0;
00522 int last, t;
00523 int A, B, L, L2, R, R2;
00524 int pos = s->pos;
00525 uint32_t crc = s->sc.crc;
00526 uint32_t crc_extra_bits = s->extra_sc.crc;
00527 int16_t *dst16 = dst;
00528 int32_t *dst32 = dst;
00529 float *dstfl = dst;
00530 const int channel_pad = s->avctx->channels - 2;
00531
00532 s->one = s->zero = s->zeroes = 0;
00533 do {
00534 L = wv_get_value(s, gb, 0, &last);
00535 if (last)
00536 break;
00537 R = wv_get_value(s, gb, 1, &last);
00538 if (last)
00539 break;
00540 for (i = 0; i < s->terms; i++) {
00541 t = s->decorr[i].value;
00542 if (t > 0) {
00543 if (t > 8) {
00544 if (t & 1) {
00545 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00546 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
00547 } else {
00548 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00549 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
00550 }
00551 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00552 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
00553 j = 0;
00554 } else {
00555 A = s->decorr[i].samplesA[pos];
00556 B = s->decorr[i].samplesB[pos];
00557 j = (pos + t) & 7;
00558 }
00559 if (type != AV_SAMPLE_FMT_S16) {
00560 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00561 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
00562 } else {
00563 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
00564 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
00565 }
00566 if (A && L) s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00567 if (B && R) s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
00568 s->decorr[i].samplesA[j] = L = L2;
00569 s->decorr[i].samplesB[j] = R = R2;
00570 } else if (t == -1) {
00571 if (type != AV_SAMPLE_FMT_S16)
00572 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
00573 else
00574 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
00575 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
00576 L = L2;
00577 if (type != AV_SAMPLE_FMT_S16)
00578 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
00579 else
00580 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
00581 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
00582 R = R2;
00583 s->decorr[i].samplesA[0] = R;
00584 } else {
00585 if (type != AV_SAMPLE_FMT_S16)
00586 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
00587 else
00588 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
00589 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
00590 R = R2;
00591
00592 if (t == -3) {
00593 R2 = s->decorr[i].samplesA[0];
00594 s->decorr[i].samplesA[0] = R;
00595 }
00596
00597 if (type != AV_SAMPLE_FMT_S16)
00598 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
00599 else
00600 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
00601 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
00602 L = L2;
00603 s->decorr[i].samplesB[0] = L;
00604 }
00605 }
00606 pos = (pos + 1) & 7;
00607 if (s->joint)
00608 L += (R -= (L >> 1));
00609 crc = (crc * 3 + L) * 3 + R;
00610
00611 if (type == AV_SAMPLE_FMT_FLT) {
00612 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, L);
00613 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, R);
00614 dstfl += channel_pad;
00615 } else if (type == AV_SAMPLE_FMT_S32) {
00616 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, L);
00617 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, R);
00618 dst32 += channel_pad;
00619 } else {
00620 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, L);
00621 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, R);
00622 dst16 += channel_pad;
00623 }
00624 count++;
00625 } while (!last && count < s->samples);
00626
00627 wv_reset_saved_context(s);
00628 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
00629 wv_check_crc(s, crc, crc_extra_bits))
00630 return AVERROR_INVALIDDATA;
00631
00632 return count * 2;
00633 }
00634
00635 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
00636 void *dst, const int type)
00637 {
00638 int i, j, count = 0;
00639 int last, t;
00640 int A, S, T;
00641 int pos = s->pos;
00642 uint32_t crc = s->sc.crc;
00643 uint32_t crc_extra_bits = s->extra_sc.crc;
00644 int16_t *dst16 = dst;
00645 int32_t *dst32 = dst;
00646 float *dstfl = dst;
00647 const int channel_stride = s->avctx->channels;
00648
00649 s->one = s->zero = s->zeroes = 0;
00650 do {
00651 T = wv_get_value(s, gb, 0, &last);
00652 S = 0;
00653 if (last)
00654 break;
00655 for (i = 0; i < s->terms; i++) {
00656 t = s->decorr[i].value;
00657 if (t > 8) {
00658 if (t & 1)
00659 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
00660 else
00661 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
00662 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
00663 j = 0;
00664 } else {
00665 A = s->decorr[i].samplesA[pos];
00666 j = (pos + t) & 7;
00667 }
00668 if (type != AV_SAMPLE_FMT_S16)
00669 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
00670 else
00671 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
00672 if (A && T)
00673 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
00674 s->decorr[i].samplesA[j] = T = S;
00675 }
00676 pos = (pos + 1) & 7;
00677 crc = crc * 3 + S;
00678
00679 if (type == AV_SAMPLE_FMT_FLT) {
00680 *dstfl = wv_get_value_float(s, &crc_extra_bits, S);
00681 dstfl += channel_stride;
00682 } else if (type == AV_SAMPLE_FMT_S32) {
00683 *dst32 = wv_get_value_integer(s, &crc_extra_bits, S);
00684 dst32 += channel_stride;
00685 } else {
00686 *dst16 = wv_get_value_integer(s, &crc_extra_bits, S);
00687 dst16 += channel_stride;
00688 }
00689 count++;
00690 } while (!last && count < s->samples);
00691
00692 wv_reset_saved_context(s);
00693 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
00694 wv_check_crc(s, crc, crc_extra_bits))
00695 return AVERROR_INVALIDDATA;
00696
00697 return count;
00698 }
00699
00700 static av_cold int wv_alloc_frame_context(WavpackContext *c)
00701 {
00702
00703 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
00704 return -1;
00705
00706 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
00707 if (!c->fdec[c->fdec_num])
00708 return -1;
00709 c->fdec_num++;
00710 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
00711 wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
00712
00713 return 0;
00714 }
00715
00716 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
00717 {
00718 WavpackContext *s = avctx->priv_data;
00719
00720 s->avctx = avctx;
00721 if (avctx->bits_per_coded_sample <= 16)
00722 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00723 else
00724 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00725 if (avctx->channels <= 2 && !avctx->channel_layout)
00726 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO :
00727 AV_CH_LAYOUT_MONO;
00728
00729 s->multichannel = avctx->channels > 2;
00730
00731
00732 s->mkv_mode = 0;
00733 if (s->multichannel && avctx->extradata && avctx->extradata_size == 2) {
00734 int ver = AV_RL16(avctx->extradata);
00735 if (ver >= 0x402 && ver <= 0x410)
00736 s->mkv_mode = 1;
00737 }
00738
00739 s->fdec_num = 0;
00740
00741 avcodec_get_frame_defaults(&s->frame);
00742 avctx->coded_frame = &s->frame;
00743
00744 return 0;
00745 }
00746
00747 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
00748 {
00749 WavpackContext *s = avctx->priv_data;
00750 int i;
00751
00752 for (i = 0; i < s->fdec_num; i++)
00753 av_freep(&s->fdec[i]);
00754 s->fdec_num = 0;
00755
00756 return 0;
00757 }
00758
00759 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
00760 void *data, int *got_frame_ptr,
00761 const uint8_t *buf, int buf_size)
00762 {
00763 WavpackContext *wc = avctx->priv_data;
00764 WavpackFrameContext *s;
00765 void *samples = data;
00766 int samplecount;
00767 int got_terms = 0, got_weights = 0, got_samples = 0,
00768 got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
00769 const uint8_t *orig_buf = buf;
00770 const uint8_t *buf_end = buf + buf_size;
00771 int i, j, id, size, ssize, weights, t;
00772 int bpp, chan, chmask, orig_bpp;
00773
00774 if (buf_size == 0) {
00775 *got_frame_ptr = 0;
00776 return 0;
00777 }
00778
00779 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
00780 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
00781 return -1;
00782 }
00783
00784 s = wc->fdec[block_no];
00785 if (!s) {
00786 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n", block_no);
00787 return -1;
00788 }
00789
00790 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
00791 memset(s->ch, 0, sizeof(s->ch));
00792 s->extra_bits = 0;
00793 s->and = s->or = s->shift = 0;
00794 s->got_extra_bits = 0;
00795
00796 if (!wc->mkv_mode) {
00797 s->samples = AV_RL32(buf); buf += 4;
00798 if (!s->samples) {
00799 *got_frame_ptr = 0;
00800 return 0;
00801 }
00802 } else {
00803 s->samples = wc->samples;
00804 }
00805 s->frame_flags = AV_RL32(buf); buf += 4;
00806 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
00807 samples = (uint8_t*)samples + bpp * wc->ch_offset;
00808 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
00809
00810 s->stereo = !(s->frame_flags & WV_MONO);
00811 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00812 s->joint = s->frame_flags & WV_JOINT_STEREO;
00813 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00814 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00815 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
00816 s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1);
00817 s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
00818 s->CRC = AV_RL32(buf); buf += 4;
00819 if (wc->mkv_mode)
00820 buf += 4;
00821
00822 wc->ch_offset += 1 + s->stereo;
00823
00824
00825 while (buf < buf_end) {
00826 id = *buf++;
00827 size = *buf++;
00828 if (id & WP_IDF_LONG) {
00829 size |= (*buf++) << 8;
00830 size |= (*buf++) << 16;
00831 }
00832 size <<= 1;
00833 ssize = size;
00834 if (id & WP_IDF_ODD)
00835 size--;
00836 if (size < 0) {
00837 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00838 break;
00839 }
00840 if (buf + ssize > buf_end) {
00841 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00842 break;
00843 }
00844 if (id & WP_IDF_IGNORE) {
00845 buf += ssize;
00846 continue;
00847 }
00848 switch (id & WP_IDF_MASK) {
00849 case WP_ID_DECTERMS:
00850 if (size > MAX_TERMS) {
00851 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00852 s->terms = 0;
00853 buf += ssize;
00854 continue;
00855 }
00856 s->terms = size;
00857 for (i = 0; i < s->terms; i++) {
00858 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00859 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00860 buf++;
00861 }
00862 got_terms = 1;
00863 break;
00864 case WP_ID_DECWEIGHTS:
00865 if (!got_terms) {
00866 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00867 continue;
00868 }
00869 weights = size >> s->stereo_in;
00870 if (weights > MAX_TERMS || weights > s->terms) {
00871 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00872 buf += ssize;
00873 continue;
00874 }
00875 for (i = 0; i < weights; i++) {
00876 t = (int8_t)(*buf++);
00877 s->decorr[s->terms - i - 1].weightA = t << 3;
00878 if (s->decorr[s->terms - i - 1].weightA > 0)
00879 s->decorr[s->terms - i - 1].weightA +=
00880 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00881 if (s->stereo_in) {
00882 t = (int8_t)(*buf++);
00883 s->decorr[s->terms - i - 1].weightB = t << 3;
00884 if (s->decorr[s->terms - i - 1].weightB > 0)
00885 s->decorr[s->terms - i - 1].weightB +=
00886 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00887 }
00888 }
00889 got_weights = 1;
00890 break;
00891 case WP_ID_DECSAMPLES:
00892 if (!got_terms) {
00893 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00894 continue;
00895 }
00896 t = 0;
00897 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
00898 if (s->decorr[i].value > 8) {
00899 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00900 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00901 if (s->stereo_in) {
00902 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00903 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00904 t += 4;
00905 }
00906 t += 4;
00907 } else if (s->decorr[i].value < 0) {
00908 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00909 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00910 t += 4;
00911 } else {
00912 for (j = 0; j < s->decorr[i].value; j++) {
00913 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00914 if (s->stereo_in) {
00915 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00916 }
00917 }
00918 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00919 }
00920 }
00921 got_samples = 1;
00922 break;
00923 case WP_ID_ENTROPY:
00924 if (size != 6 * (s->stereo_in + 1)) {
00925 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, "
00926 "got %i", 6 * (s->stereo_in + 1), size);
00927 buf += ssize;
00928 continue;
00929 }
00930 for (j = 0; j <= s->stereo_in; j++) {
00931 for (i = 0; i < 3; i++) {
00932 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00933 buf += 2;
00934 }
00935 }
00936 got_entropy = 1;
00937 break;
00938 case WP_ID_HYBRID:
00939 if (s->hybrid_bitrate) {
00940 for (i = 0; i <= s->stereo_in; i++) {
00941 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00942 buf += 2;
00943 size -= 2;
00944 }
00945 }
00946 for (i = 0; i < (s->stereo_in + 1); i++) {
00947 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00948 buf += 2;
00949 size -= 2;
00950 }
00951 if (size > 0) {
00952 for (i = 0; i < (s->stereo_in + 1); i++) {
00953 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00954 buf += 2;
00955 }
00956 } else {
00957 for (i = 0; i < (s->stereo_in + 1); i++)
00958 s->ch[i].bitrate_delta = 0;
00959 }
00960 got_hybrid = 1;
00961 break;
00962 case WP_ID_INT32INFO:
00963 if (size != 4) {
00964 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00965 buf += ssize;
00966 continue;
00967 }
00968 if (buf[0])
00969 s->extra_bits = buf[0];
00970 else if (buf[1])
00971 s->shift = buf[1];
00972 else if (buf[2]){
00973 s->and = s->or = 1;
00974 s->shift = buf[2];
00975 } else if(buf[3]) {
00976 s->and = 1;
00977 s->shift = buf[3];
00978 }
00979
00980
00981
00982 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
00983 s->post_shift += 8;
00984 s->shift -= 8;
00985 s->hybrid_maxclip >>= 8;
00986 s->hybrid_minclip >>= 8;
00987 }
00988 buf += 4;
00989 break;
00990 case WP_ID_FLOATINFO:
00991 if (size != 4) {
00992 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00993 buf += ssize;
00994 continue;
00995 }
00996 s->float_flag = buf[0];
00997 s->float_shift = buf[1];
00998 s->float_max_exp = buf[2];
00999 buf += 4;
01000 got_float = 1;
01001 break;
01002 case WP_ID_DATA:
01003 s->sc.offset = buf - orig_buf;
01004 s->sc.size = size * 8;
01005 init_get_bits(&s->gb, buf, size * 8);
01006 s->data_size = size * 8;
01007 buf += size;
01008 got_bs = 1;
01009 break;
01010 case WP_ID_EXTRABITS:
01011 if (size <= 4) {
01012 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
01013 size);
01014 buf += size;
01015 continue;
01016 }
01017 s->extra_sc.offset = buf - orig_buf;
01018 s->extra_sc.size = size * 8;
01019 init_get_bits(&s->gb_extra_bits, buf, size * 8);
01020 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
01021 buf += size;
01022 s->got_extra_bits = 1;
01023 break;
01024 case WP_ID_CHANINFO:
01025 if (size <= 1) {
01026 av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
01027 return -1;
01028 }
01029 chan = *buf++;
01030 switch (size - 2) {
01031 case 0: chmask = *buf; break;
01032 case 1: chmask = AV_RL16(buf); break;
01033 case 2: chmask = AV_RL24(buf); break;
01034 case 3: chmask = AV_RL32(buf); break;
01035 case 5:
01036 chan |= (buf[1] & 0xF) << 8;
01037 chmask = AV_RL24(buf + 2);
01038 break;
01039 default:
01040 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
01041 size);
01042 chan = avctx->channels;
01043 chmask = avctx->channel_layout;
01044 }
01045 if (chan != avctx->channels) {
01046 av_log(avctx, AV_LOG_ERROR, "Block reports total %d channels, "
01047 "decoder believes it's %d channels\n", chan,
01048 avctx->channels);
01049 return -1;
01050 }
01051 if (!avctx->channel_layout)
01052 avctx->channel_layout = chmask;
01053 buf += size - 1;
01054 break;
01055 default:
01056 buf += size;
01057 }
01058 if (id & WP_IDF_ODD)
01059 buf++;
01060 }
01061
01062 if (!got_terms) {
01063 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
01064 return -1;
01065 }
01066 if (!got_weights) {
01067 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
01068 return -1;
01069 }
01070 if (!got_samples) {
01071 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
01072 return -1;
01073 }
01074 if (!got_entropy) {
01075 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
01076 return -1;
01077 }
01078 if (s->hybrid && !got_hybrid) {
01079 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
01080 return -1;
01081 }
01082 if (!got_bs) {
01083 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
01084 return -1;
01085 }
01086 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
01087 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
01088 return -1;
01089 }
01090 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
01091 const int size = get_bits_left(&s->gb_extra_bits);
01092 const int wanted = s->samples * s->extra_bits << s->stereo_in;
01093 if (size < wanted) {
01094 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
01095 s->got_extra_bits = 0;
01096 }
01097 }
01098
01099 if (s->stereo_in) {
01100 if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01101 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01102 else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01103 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01104 else
01105 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01106
01107 if (samplecount < 0)
01108 return -1;
01109
01110 samplecount >>= 1;
01111 } else {
01112 const int channel_stride = avctx->channels;
01113
01114 if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01115 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01116 else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01117 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01118 else
01119 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01120
01121 if (samplecount < 0)
01122 return -1;
01123
01124 if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
01125 int16_t *dst = (int16_t*)samples + 1;
01126 int16_t *src = (int16_t*)samples;
01127 int cnt = samplecount;
01128 while (cnt--) {
01129 *dst = *src;
01130 src += channel_stride;
01131 dst += channel_stride;
01132 }
01133 } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
01134 int32_t *dst = (int32_t*)samples + 1;
01135 int32_t *src = (int32_t*)samples;
01136 int cnt = samplecount;
01137 while (cnt--) {
01138 *dst = *src;
01139 src += channel_stride;
01140 dst += channel_stride;
01141 }
01142 } else if (s->stereo) {
01143 float *dst = (float*)samples + 1;
01144 float *src = (float*)samples;
01145 int cnt = samplecount;
01146 while (cnt--) {
01147 *dst = *src;
01148 src += channel_stride;
01149 dst += channel_stride;
01150 }
01151 }
01152 }
01153
01154 *got_frame_ptr = 1;
01155
01156 return samplecount * bpp;
01157 }
01158
01159 static void wavpack_decode_flush(AVCodecContext *avctx)
01160 {
01161 WavpackContext *s = avctx->priv_data;
01162 int i;
01163
01164 for (i = 0; i < s->fdec_num; i++)
01165 wv_reset_saved_context(s->fdec[i]);
01166 }
01167
01168 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
01169 int *got_frame_ptr, AVPacket *avpkt)
01170 {
01171 WavpackContext *s = avctx->priv_data;
01172 const uint8_t *buf = avpkt->data;
01173 int buf_size = avpkt->size;
01174 int frame_size, ret, frame_flags;
01175 int samplecount = 0;
01176
01177 s->block = 0;
01178 s->ch_offset = 0;
01179
01180
01181 if (s->mkv_mode) {
01182 s->samples = AV_RL32(buf); buf += 4;
01183 frame_flags = AV_RL32(buf);
01184 } else {
01185 if (s->multichannel) {
01186 s->samples = AV_RL32(buf + 4);
01187 frame_flags = AV_RL32(buf + 8);
01188 } else {
01189 s->samples = AV_RL32(buf);
01190 frame_flags = AV_RL32(buf + 4);
01191 }
01192 }
01193 if (s->samples <= 0) {
01194 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
01195 s->samples);
01196 return AVERROR(EINVAL);
01197 }
01198
01199 if (frame_flags & 0x80) {
01200 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01201 } else if ((frame_flags & 0x03) <= 1) {
01202 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01203 } else {
01204 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
01205 }
01206
01207
01208 s->frame.nb_samples = s->samples;
01209 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01210 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01211 return ret;
01212 }
01213
01214 while (buf_size > 0) {
01215 if (!s->multichannel) {
01216 frame_size = buf_size;
01217 } else {
01218 if (!s->mkv_mode) {
01219 frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
01220 } else {
01221 if (buf_size < 12)
01222 break;
01223 frame_size = AV_RL32(buf + 8) + 12;
01224 }
01225 }
01226 if (frame_size < 0 || frame_size > buf_size) {
01227 av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d "
01228 "vs. %d bytes left)\n", s->block, frame_size, buf_size);
01229 wavpack_decode_flush(avctx);
01230 return -1;
01231 }
01232 if ((samplecount = wavpack_decode_block(avctx, s->block,
01233 s->frame.data[0], got_frame_ptr,
01234 buf, frame_size)) < 0) {
01235 wavpack_decode_flush(avctx);
01236 return -1;
01237 }
01238 s->block++;
01239 buf += frame_size; buf_size -= frame_size;
01240 }
01241
01242 if (*got_frame_ptr)
01243 *(AVFrame *)data = s->frame;
01244
01245 return avpkt->size;
01246 }
01247
01248 AVCodec ff_wavpack_decoder = {
01249 .name = "wavpack",
01250 .type = AVMEDIA_TYPE_AUDIO,
01251 .id = CODEC_ID_WAVPACK,
01252 .priv_data_size = sizeof(WavpackContext),
01253 .init = wavpack_decode_init,
01254 .close = wavpack_decode_end,
01255 .decode = wavpack_decode_frame,
01256 .flush = wavpack_decode_flush,
01257 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01258 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
01259 };