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 AVERROR_INVALIDDATA;
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 AVERROR_INVALIDDATA;
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 != wc->samples)
00794 return AVERROR_INVALIDDATA;
00795
00796 if (!s->samples) {
00797 *got_frame_ptr = 0;
00798 return 0;
00799 }
00800 } else {
00801 s->samples = wc->samples;
00802 }
00803 s->frame_flags = AV_RL32(buf); buf += 4;
00804 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
00805 samples = (uint8_t*)samples + bpp * wc->ch_offset;
00806 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
00807
00808 s->stereo = !(s->frame_flags & WV_MONO);
00809 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
00810 s->joint = s->frame_flags & WV_JOINT_STEREO;
00811 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
00812 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
00813 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
00814 s->hybrid_maxclip = (( 1LL << (orig_bpp - 1)) - 1) >> s->post_shift;
00815 s->hybrid_minclip = ((-1LL << (orig_bpp - 1))) >> s->post_shift;
00816 s->CRC = AV_RL32(buf); buf += 4;
00817 if (wc->mkv_mode)
00818 buf += 4;
00819
00820 wc->ch_offset += 1 + s->stereo;
00821
00822
00823 while (buf < buf_end) {
00824 id = *buf++;
00825 size = *buf++;
00826 if (id & WP_IDF_LONG) {
00827 size |= (*buf++) << 8;
00828 size |= (*buf++) << 16;
00829 }
00830 size <<= 1;
00831 ssize = size;
00832 if (id & WP_IDF_ODD)
00833 size--;
00834 if (size < 0) {
00835 av_log(avctx, AV_LOG_ERROR, "Got incorrect block %02X with size %i\n", id, size);
00836 break;
00837 }
00838 if (buf + ssize > buf_end) {
00839 av_log(avctx, AV_LOG_ERROR, "Block size %i is out of bounds\n", size);
00840 break;
00841 }
00842 if (id & WP_IDF_IGNORE) {
00843 buf += ssize;
00844 continue;
00845 }
00846 switch (id & WP_IDF_MASK) {
00847 case WP_ID_DECTERMS:
00848 if (size > MAX_TERMS) {
00849 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
00850 s->terms = 0;
00851 buf += ssize;
00852 continue;
00853 }
00854 s->terms = size;
00855 for (i = 0; i < s->terms; i++) {
00856 s->decorr[s->terms - i - 1].value = (*buf & 0x1F) - 5;
00857 s->decorr[s->terms - i - 1].delta = *buf >> 5;
00858 buf++;
00859 }
00860 got_terms = 1;
00861 break;
00862 case WP_ID_DECWEIGHTS:
00863 if (!got_terms) {
00864 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00865 continue;
00866 }
00867 weights = size >> s->stereo_in;
00868 if (weights > MAX_TERMS || weights > s->terms) {
00869 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
00870 buf += ssize;
00871 continue;
00872 }
00873 for (i = 0; i < weights; i++) {
00874 t = (int8_t)(*buf++);
00875 s->decorr[s->terms - i - 1].weightA = t << 3;
00876 if (s->decorr[s->terms - i - 1].weightA > 0)
00877 s->decorr[s->terms - i - 1].weightA +=
00878 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
00879 if (s->stereo_in) {
00880 t = (int8_t)(*buf++);
00881 s->decorr[s->terms - i - 1].weightB = t << 3;
00882 if (s->decorr[s->terms - i - 1].weightB > 0)
00883 s->decorr[s->terms - i - 1].weightB +=
00884 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
00885 }
00886 }
00887 got_weights = 1;
00888 break;
00889 case WP_ID_DECSAMPLES:
00890 if (!got_terms) {
00891 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
00892 continue;
00893 }
00894 t = 0;
00895 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
00896 if (s->decorr[i].value > 8) {
00897 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00898 s->decorr[i].samplesA[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00899 if (s->stereo_in) {
00900 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00901 s->decorr[i].samplesB[1] = wp_exp2(AV_RL16(buf)); buf += 2;
00902 t += 4;
00903 }
00904 t += 4;
00905 } else if (s->decorr[i].value < 0) {
00906 s->decorr[i].samplesA[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00907 s->decorr[i].samplesB[0] = wp_exp2(AV_RL16(buf)); buf += 2;
00908 t += 4;
00909 } else {
00910 for (j = 0; j < s->decorr[i].value; j++) {
00911 s->decorr[i].samplesA[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00912 if (s->stereo_in)
00913 s->decorr[i].samplesB[j] = wp_exp2(AV_RL16(buf)); buf += 2;
00914 }
00915 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
00916 }
00917 }
00918 got_samples = 1;
00919 break;
00920 case WP_ID_ENTROPY:
00921 if (size != 6 * (s->stereo_in + 1)) {
00922 av_log(avctx, AV_LOG_ERROR, "Entropy vars size should be %i, "
00923 "got %i", 6 * (s->stereo_in + 1), size);
00924 buf += ssize;
00925 continue;
00926 }
00927 for (j = 0; j <= s->stereo_in; j++) {
00928 for (i = 0; i < 3; i++) {
00929 s->ch[j].median[i] = wp_exp2(AV_RL16(buf));
00930 buf += 2;
00931 }
00932 }
00933 got_entropy = 1;
00934 break;
00935 case WP_ID_HYBRID:
00936 if (s->hybrid_bitrate) {
00937 for (i = 0; i <= s->stereo_in; i++) {
00938 s->ch[i].slow_level = wp_exp2(AV_RL16(buf));
00939 buf += 2;
00940 size -= 2;
00941 }
00942 }
00943 for (i = 0; i < (s->stereo_in + 1); i++) {
00944 s->ch[i].bitrate_acc = AV_RL16(buf) << 16;
00945 buf += 2;
00946 size -= 2;
00947 }
00948 if (size > 0) {
00949 for (i = 0; i < (s->stereo_in + 1); i++) {
00950 s->ch[i].bitrate_delta = wp_exp2((int16_t)AV_RL16(buf));
00951 buf += 2;
00952 }
00953 } else {
00954 for (i = 0; i < (s->stereo_in + 1); i++)
00955 s->ch[i].bitrate_delta = 0;
00956 }
00957 got_hybrid = 1;
00958 break;
00959 case WP_ID_INT32INFO:
00960 if (size != 4) {
00961 av_log(avctx, AV_LOG_ERROR, "Invalid INT32INFO, size = %i, sent_bits = %i\n", size, *buf);
00962 buf += ssize;
00963 continue;
00964 }
00965 if (buf[0])
00966 s->extra_bits = buf[0];
00967 else if (buf[1])
00968 s->shift = buf[1];
00969 else if (buf[2]){
00970 s->and = s->or = 1;
00971 s->shift = buf[2];
00972 } else if(buf[3]) {
00973 s->and = 1;
00974 s->shift = buf[3];
00975 }
00976
00977
00978
00979 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
00980 s->post_shift += 8;
00981 s->shift -= 8;
00982 s->hybrid_maxclip >>= 8;
00983 s->hybrid_minclip >>= 8;
00984 }
00985 buf += 4;
00986 break;
00987 case WP_ID_FLOATINFO:
00988 if (size != 4) {
00989 av_log(avctx, AV_LOG_ERROR, "Invalid FLOATINFO, size = %i\n", size);
00990 buf += ssize;
00991 continue;
00992 }
00993 s->float_flag = buf[0];
00994 s->float_shift = buf[1];
00995 s->float_max_exp = buf[2];
00996 buf += 4;
00997 got_float = 1;
00998 break;
00999 case WP_ID_DATA:
01000 s->sc.offset = buf - orig_buf;
01001 s->sc.size = size * 8;
01002 init_get_bits(&s->gb, buf, size * 8);
01003 s->data_size = size * 8;
01004 buf += size;
01005 got_bs = 1;
01006 break;
01007 case WP_ID_EXTRABITS:
01008 if (size <= 4) {
01009 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
01010 size);
01011 buf += size;
01012 continue;
01013 }
01014 s->extra_sc.offset = buf - orig_buf;
01015 s->extra_sc.size = size * 8;
01016 init_get_bits(&s->gb_extra_bits, buf, size * 8);
01017 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
01018 buf += size;
01019 s->got_extra_bits = 1;
01020 break;
01021 case WP_ID_CHANINFO:
01022 if (size <= 1) {
01023 av_log(avctx, AV_LOG_ERROR, "Insufficient channel information\n");
01024 return AVERROR_INVALIDDATA;
01025 }
01026 chan = *buf++;
01027 switch (size - 2) {
01028 case 0: chmask = *buf; break;
01029 case 1: chmask = AV_RL16(buf); break;
01030 case 2: chmask = AV_RL24(buf); break;
01031 case 3: chmask = AV_RL32(buf); break;
01032 case 5:
01033 chan |= (buf[1] & 0xF) << 8;
01034 chmask = AV_RL24(buf + 2);
01035 break;
01036 default:
01037 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
01038 size);
01039 chan = avctx->channels;
01040 chmask = avctx->channel_layout;
01041 }
01042 if (chan != avctx->channels) {
01043 av_log(avctx, AV_LOG_ERROR,
01044 "Block reports total %d channels, "
01045 "decoder believes it's %d channels\n",
01046 chan, avctx->channels);
01047 return AVERROR_INVALIDDATA;
01048 }
01049 if (!avctx->channel_layout)
01050 avctx->channel_layout = chmask;
01051 buf += size - 1;
01052 break;
01053 default:
01054 buf += size;
01055 }
01056 if (id & WP_IDF_ODD)
01057 buf++;
01058 }
01059
01060 if (!got_terms) {
01061 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
01062 return AVERROR_INVALIDDATA;
01063 }
01064 if (!got_weights) {
01065 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
01066 return AVERROR_INVALIDDATA;
01067 }
01068 if (!got_samples) {
01069 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
01070 return AVERROR_INVALIDDATA;
01071 }
01072 if (!got_entropy) {
01073 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
01074 return AVERROR_INVALIDDATA;
01075 }
01076 if (s->hybrid && !got_hybrid) {
01077 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
01078 return AVERROR_INVALIDDATA;
01079 }
01080 if (!got_bs) {
01081 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
01082 return AVERROR_INVALIDDATA;
01083 }
01084 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLT) {
01085 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
01086 return AVERROR_INVALIDDATA;
01087 }
01088 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLT) {
01089 const int size = get_bits_left(&s->gb_extra_bits);
01090 const int wanted = s->samples * s->extra_bits << s->stereo_in;
01091 if (size < wanted) {
01092 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
01093 s->got_extra_bits = 0;
01094 }
01095 }
01096
01097 if (s->stereo_in) {
01098 if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01099 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01100 else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01101 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01102 else
01103 samplecount = wv_unpack_stereo(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01104
01105 if (samplecount < 0)
01106 return samplecount;
01107
01108 samplecount >>= 1;
01109 } else {
01110 const int channel_stride = avctx->channels;
01111
01112 if (avctx->sample_fmt == AV_SAMPLE_FMT_S16)
01113 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S16);
01114 else if (avctx->sample_fmt == AV_SAMPLE_FMT_S32)
01115 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_S32);
01116 else
01117 samplecount = wv_unpack_mono(s, &s->gb, samples, AV_SAMPLE_FMT_FLT);
01118
01119 if (samplecount < 0)
01120 return samplecount;
01121
01122 if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
01123 int16_t *dst = (int16_t*)samples + 1;
01124 int16_t *src = (int16_t*)samples;
01125 int cnt = samplecount;
01126 while (cnt--) {
01127 *dst = *src;
01128 src += channel_stride;
01129 dst += channel_stride;
01130 }
01131 } else if (s->stereo && avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
01132 int32_t *dst = (int32_t*)samples + 1;
01133 int32_t *src = (int32_t*)samples;
01134 int cnt = samplecount;
01135 while (cnt--) {
01136 *dst = *src;
01137 src += channel_stride;
01138 dst += channel_stride;
01139 }
01140 } else if (s->stereo) {
01141 float *dst = (float*)samples + 1;
01142 float *src = (float*)samples;
01143 int cnt = samplecount;
01144 while (cnt--) {
01145 *dst = *src;
01146 src += channel_stride;
01147 dst += channel_stride;
01148 }
01149 }
01150 }
01151
01152 *got_frame_ptr = 1;
01153
01154 return samplecount * bpp;
01155 }
01156
01157 static void wavpack_decode_flush(AVCodecContext *avctx)
01158 {
01159 WavpackContext *s = avctx->priv_data;
01160 int i;
01161
01162 for (i = 0; i < s->fdec_num; i++)
01163 wv_reset_saved_context(s->fdec[i]);
01164 }
01165
01166 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
01167 int *got_frame_ptr, AVPacket *avpkt)
01168 {
01169 WavpackContext *s = avctx->priv_data;
01170 const uint8_t *buf = avpkt->data;
01171 int buf_size = avpkt->size;
01172 int frame_size, ret, frame_flags;
01173 int samplecount = 0;
01174
01175 if (avpkt->size < 12 + s->multichannel * 4)
01176 return AVERROR_INVALIDDATA;
01177
01178 s->block = 0;
01179 s->ch_offset = 0;
01180
01181
01182 if (s->mkv_mode) {
01183 s->samples = AV_RL32(buf); buf += 4;
01184 frame_flags = AV_RL32(buf);
01185 } else {
01186 if (s->multichannel) {
01187 s->samples = AV_RL32(buf + 4);
01188 frame_flags = AV_RL32(buf + 8);
01189 } else {
01190 s->samples = AV_RL32(buf);
01191 frame_flags = AV_RL32(buf + 4);
01192 }
01193 }
01194 if (s->samples <= 0) {
01195 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
01196 s->samples);
01197 return AVERROR_INVALIDDATA;
01198 }
01199
01200 if (frame_flags & 0x80) {
01201 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
01202 } else if ((frame_flags & 0x03) <= 1) {
01203 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
01204 } else {
01205 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
01206 }
01207
01208
01209 s->frame.nb_samples = s->samples;
01210 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
01211 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01212 return ret;
01213 }
01214
01215 while (buf_size > 0) {
01216 if (!s->multichannel) {
01217 frame_size = buf_size;
01218 } else {
01219 if (!s->mkv_mode) {
01220 frame_size = AV_RL32(buf) - 12; buf += 4; buf_size -= 4;
01221 } else {
01222 if (buf_size < 12)
01223 break;
01224 frame_size = AV_RL32(buf + 8) + 12;
01225 }
01226 }
01227 if (frame_size < 0 || frame_size > buf_size) {
01228 av_log(avctx, AV_LOG_ERROR, "Block %d has invalid size (size %d "
01229 "vs. %d bytes left)\n", s->block, frame_size, buf_size);
01230 wavpack_decode_flush(avctx);
01231 return AVERROR_INVALIDDATA;
01232 }
01233 if ((samplecount = wavpack_decode_block(avctx, s->block,
01234 s->frame.data[0], got_frame_ptr,
01235 buf, frame_size)) < 0) {
01236 wavpack_decode_flush(avctx);
01237 return samplecount;
01238 }
01239 s->block++;
01240 buf += frame_size; buf_size -= frame_size;
01241 }
01242
01243 if (*got_frame_ptr)
01244 *(AVFrame *)data = s->frame;
01245
01246 return avpkt->size;
01247 }
01248
01249 AVCodec ff_wavpack_decoder = {
01250 .name = "wavpack",
01251 .type = AVMEDIA_TYPE_AUDIO,
01252 .id = CODEC_ID_WAVPACK,
01253 .priv_data_size = sizeof(WavpackContext),
01254 .init = wavpack_decode_init,
01255 .close = wavpack_decode_end,
01256 .decode = wavpack_decode_frame,
01257 .flush = wavpack_decode_flush,
01258 .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01259 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
01260 };