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