2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #define BITSTREAM_READER_LE
24 #include "libavutil/channel_layout.h"
29 #include "bytestream.h"
33 * WavPack lossless audio decoder
36 #define WV_HEADER_SIZE 32
38 #define WV_MONO 0x00000004
39 #define WV_JOINT_STEREO 0x00000010
40 #define WV_FALSE_STEREO 0x40000000
42 #define WV_HYBRID_MODE 0x00000008
43 #define WV_HYBRID_SHAPE 0x00000008
44 #define WV_HYBRID_BITRATE 0x00000200
45 #define WV_HYBRID_BALANCE 0x00000400
47 #define WV_FLT_SHIFT_ONES 0x01
48 #define WV_FLT_SHIFT_SAME 0x02
49 #define WV_FLT_SHIFT_SENT 0x04
50 #define WV_FLT_ZERO_SENT 0x08
51 #define WV_FLT_ZERO_SIGN 0x10
53 #define WV_MAX_SAMPLES 131072
79 typedef struct SavedContext {
88 typedef struct Decorr {
97 typedef struct WvChannel {
99 int slow_level, error_limit;
100 int bitrate_acc, bitrate_delta;
103 typedef struct WavpackFrameContext {
104 AVCodecContext *avctx;
106 int stereo, stereo_in;
111 uint32_t crc_extra_bits;
112 GetBitContext gb_extra_bits;
113 int data_size; // in bits
116 Decorr decorr[MAX_TERMS];
117 int zero, one, zeroes;
121 int hybrid, hybrid_bitrate;
122 int hybrid_maxclip, hybrid_minclip;
128 SavedContext sc, extra_sc;
129 } WavpackFrameContext;
131 #define WV_MAX_FRAME_DECODERS 14
133 typedef struct WavpackContext {
134 AVCodecContext *avctx;
136 WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
145 // exponent table copied from WavPack source
146 static const uint8_t wp_exp2_table[256] = {
147 0x00, 0x01, 0x01, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x08, 0x09, 0x0a, 0x0b,
148 0x0b, 0x0c, 0x0d, 0x0e, 0x0e, 0x0f, 0x10, 0x10, 0x11, 0x12, 0x13, 0x13, 0x14, 0x15, 0x16, 0x16,
149 0x17, 0x18, 0x19, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1d, 0x1e, 0x1f, 0x20, 0x20, 0x21, 0x22, 0x23,
150 0x24, 0x24, 0x25, 0x26, 0x27, 0x28, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
151 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3a, 0x3b, 0x3c, 0x3d,
152 0x3e, 0x3f, 0x40, 0x41, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x48, 0x49, 0x4a, 0x4b,
153 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a,
154 0x5b, 0x5c, 0x5d, 0x5e, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
155 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
156 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x87, 0x88, 0x89, 0x8a,
157 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
158 0x9c, 0x9d, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad,
159 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0,
160 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc8, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4,
161 0xd6, 0xd7, 0xd8, 0xd9, 0xdb, 0xdc, 0xdd, 0xde, 0xe0, 0xe1, 0xe2, 0xe4, 0xe5, 0xe6, 0xe8, 0xe9,
162 0xea, 0xec, 0xed, 0xee, 0xf0, 0xf1, 0xf2, 0xf4, 0xf5, 0xf6, 0xf8, 0xf9, 0xfa, 0xfc, 0xfd, 0xff
165 static const uint8_t wp_log2_table [] = {
166 0x00, 0x01, 0x03, 0x04, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x10, 0x11, 0x12, 0x14, 0x15,
167 0x16, 0x18, 0x19, 0x1a, 0x1c, 0x1d, 0x1e, 0x20, 0x21, 0x22, 0x24, 0x25, 0x26, 0x28, 0x29, 0x2a,
168 0x2c, 0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3d, 0x3e,
169 0x3f, 0x41, 0x42, 0x43, 0x44, 0x45, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4d, 0x4e, 0x4f, 0x50, 0x51,
170 0x52, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63,
171 0x64, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x74, 0x75,
172 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
173 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
174 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
175 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb2,
176 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc0,
177 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcb, 0xcc, 0xcd, 0xce,
178 0xcf, 0xd0, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd8, 0xd9, 0xda, 0xdb,
179 0xdc, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe4, 0xe5, 0xe6, 0xe7, 0xe7,
180 0xe8, 0xe9, 0xea, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xee, 0xef, 0xf0, 0xf1, 0xf1, 0xf2, 0xf3, 0xf4,
181 0xf4, 0xf5, 0xf6, 0xf7, 0xf7, 0xf8, 0xf9, 0xf9, 0xfa, 0xfb, 0xfc, 0xfc, 0xfd, 0xfe, 0xff, 0xff
184 static av_always_inline int wp_exp2(int16_t val)
193 res = wp_exp2_table[val & 0xFF] | 0x100;
195 res = (val > 9) ? (res << (val - 9)) : (res >> (9 - val));
196 return neg ? -res : res;
199 static av_always_inline int wp_log2(int32_t val)
208 bits = av_log2(val) + 1;
210 return (bits << 8) + wp_log2_table[(val << (9 - bits)) & 0xFF];
212 return (bits << 8) + wp_log2_table[(val >> (bits - 9)) & 0xFF];
215 #define LEVEL_DECAY(a) ((a + 0x80) >> 8)
217 // macros for manipulating median values
218 #define GET_MED(n) ((c->median[n] >> 4) + 1)
219 #define DEC_MED(n) c->median[n] -= ((c->median[n] + (128 >> n) - 2) / (128 >> n)) * 2
220 #define INC_MED(n) c->median[n] += ((c->median[n] + (128 >> n) ) / (128 >> n)) * 5
222 // macros for applying weight
223 #define UPDATE_WEIGHT_CLIP(weight, delta, samples, in) \
224 if (samples && in) { \
225 if ((samples ^ in) < 0) { \
227 if (weight < -1024) \
236 static av_always_inline int get_tail(GetBitContext *gb, int k)
243 e = (1 << (p + 1)) - k - 1;
244 res = p ? get_bits(gb, p) : 0;
246 res = (res << 1) - e + get_bits1(gb);
250 static void update_error_limit(WavpackFrameContext *ctx)
254 for (i = 0; i <= ctx->stereo_in; i++) {
255 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
256 br[i] = ctx->ch[i].bitrate_acc >> 16;
257 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
259 if (ctx->stereo_in && ctx->hybrid_bitrate) {
260 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
261 if (balance > br[0]) {
264 } else if (-balance > br[0]) {
268 br[1] = br[0] + balance;
269 br[0] = br[0] - balance;
272 for (i = 0; i <= ctx->stereo_in; i++) {
273 if (ctx->hybrid_bitrate) {
274 if (sl[i] - br[i] > -0x100)
275 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
277 ctx->ch[i].error_limit = 0;
279 ctx->ch[i].error_limit = wp_exp2(br[i]);
284 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
285 int channel, int *last)
288 int sign, base, add, ret;
289 WvChannel *c = &ctx->ch[channel];
293 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
294 !ctx->zero && !ctx->one) {
298 c->slow_level -= LEVEL_DECAY(c->slow_level);
302 t = get_unary_0_33(gb);
304 if (get_bits_left(gb) < t - 1)
306 t = get_bits(gb, t - 1) | (1 << (t - 1));
308 if (get_bits_left(gb) < 0)
313 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
314 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
315 c->slow_level -= LEVEL_DECAY(c->slow_level);
325 t = get_unary_0_33(gb);
326 if (get_bits_left(gb) < 0)
329 t2 = get_unary_0_33(gb);
331 if (get_bits_left(gb) < 0)
335 if (get_bits_left(gb) < t2 - 1)
337 t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
348 ctx->zero = !ctx->one;
351 if (ctx->hybrid && !channel)
352 update_error_limit(ctx);
356 add = GET_MED(0) - 1;
360 add = GET_MED(1) - 1;
364 base = GET_MED(0) + GET_MED(1);
365 add = GET_MED(2) - 1;
370 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
371 add = GET_MED(2) - 1;
376 if (!c->error_limit) {
377 if (add >= 0x2000000U) {
378 av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
381 ret = base + get_tail(gb, add);
382 if (get_bits_left(gb) <= 0)
385 int mid = (base * 2 + add + 1) >> 1;
386 while (add > c->error_limit) {
387 if (get_bits_left(gb) <= 0)
393 add = mid - base - 1;
394 mid = (base * 2 + add + 1) >> 1;
398 sign = get_bits1(gb);
399 if (ctx->hybrid_bitrate)
400 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
401 return sign ? ~ret : ret;
408 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
416 if (s->got_extra_bits &&
417 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
418 S |= get_bits(&s->gb_extra_bits, s->extra_bits);
419 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
423 bit = (S & s->and) | s->or;
424 bit = ((S + bit) << s->shift) - bit;
427 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
429 return bit << s->post_shift;
432 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
440 int exp = s->float_max_exp;
442 if (s->got_extra_bits) {
443 const int max_bits = 1 + 23 + 8 + 1;
444 const int left_bits = get_bits_left(&s->gb_extra_bits);
446 if (left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
451 S <<= s->float_shift;
455 if (S >= 0x1000000) {
456 if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
457 S = get_bits(&s->gb_extra_bits, 23);
462 int shift = 23 - av_log2(S);
463 exp = s->float_max_exp;
470 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
471 (s->got_extra_bits &&
472 (s->float_flag & WV_FLT_SHIFT_SAME) &&
473 get_bits1(&s->gb_extra_bits))) {
474 S |= (1 << shift) - 1;
475 } else if (s->got_extra_bits &&
476 (s->float_flag & WV_FLT_SHIFT_SENT)) {
477 S |= get_bits(&s->gb_extra_bits, shift);
481 exp = s->float_max_exp;
487 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
488 if (get_bits1(&s->gb_extra_bits)) {
489 S = get_bits(&s->gb_extra_bits, 23);
490 if (s->float_max_exp >= 25)
491 exp = get_bits(&s->gb_extra_bits, 8);
492 sign = get_bits1(&s->gb_extra_bits);
494 if (s->float_flag & WV_FLT_ZERO_SIGN)
495 sign = get_bits1(&s->gb_extra_bits);
500 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
502 value.u = (sign << 31) | (exp << 23) | S;
506 static void wv_reset_saved_context(WavpackFrameContext *s)
509 s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
512 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
513 uint32_t crc_extra_bits)
516 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
517 return AVERROR_INVALIDDATA;
519 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
520 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
521 return AVERROR_INVALIDDATA;
527 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
528 void *dst_l, void *dst_r, const int type)
532 int A, B, L, L2, R, R2;
534 uint32_t crc = s->sc.crc;
535 uint32_t crc_extra_bits = s->extra_sc.crc;
536 int16_t *dst16_l = dst_l;
537 int16_t *dst16_r = dst_r;
538 int32_t *dst32_l = dst_l;
539 int32_t *dst32_r = dst_r;
540 float *dstfl_l = dst_l;
541 float *dstfl_r = dst_r;
543 s->one = s->zero = s->zeroes = 0;
545 L = wv_get_value(s, gb, 0, &last);
548 R = wv_get_value(s, gb, 1, &last);
551 for (i = 0; i < s->terms; i++) {
552 t = s->decorr[i].value;
556 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
557 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
559 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
560 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
562 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
563 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
566 A = s->decorr[i].samplesA[pos];
567 B = s->decorr[i].samplesB[pos];
570 if (type != AV_SAMPLE_FMT_S16P) {
571 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
572 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
574 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
575 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
578 s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
580 s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
581 s->decorr[i].samplesA[j] = L = L2;
582 s->decorr[i].samplesB[j] = R = R2;
583 } else if (t == -1) {
584 if (type != AV_SAMPLE_FMT_S16P)
585 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
587 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
588 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
590 if (type != AV_SAMPLE_FMT_S16P)
591 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
593 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
594 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
596 s->decorr[i].samplesA[0] = R;
598 if (type != AV_SAMPLE_FMT_S16P)
599 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
601 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
602 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
606 R2 = s->decorr[i].samplesA[0];
607 s->decorr[i].samplesA[0] = R;
610 if (type != AV_SAMPLE_FMT_S16P)
611 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
613 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
614 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
616 s->decorr[i].samplesB[0] = L;
621 L += (R -= (L >> 1));
622 crc = (crc * 3 + L) * 3 + R;
624 if (type == AV_SAMPLE_FMT_FLTP) {
625 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
626 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
627 } else if (type == AV_SAMPLE_FMT_S32P) {
628 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
629 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
631 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
632 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
635 } while (!last && count < s->samples);
637 wv_reset_saved_context(s);
638 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
639 wv_check_crc(s, crc, crc_extra_bits))
640 return AVERROR_INVALIDDATA;
645 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
646 void *dst, const int type)
652 uint32_t crc = s->sc.crc;
653 uint32_t crc_extra_bits = s->extra_sc.crc;
654 int16_t *dst16 = dst;
655 int32_t *dst32 = dst;
658 s->one = s->zero = s->zeroes = 0;
660 T = wv_get_value(s, gb, 0, &last);
664 for (i = 0; i < s->terms; i++) {
665 t = s->decorr[i].value;
668 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
670 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
671 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
674 A = s->decorr[i].samplesA[pos];
677 if (type != AV_SAMPLE_FMT_S16P)
678 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
680 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
682 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
683 s->decorr[i].samplesA[j] = T = S;
688 if (type == AV_SAMPLE_FMT_FLTP) {
689 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
690 } else if (type == AV_SAMPLE_FMT_S32P) {
691 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
693 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
696 } while (!last && count < s->samples);
698 wv_reset_saved_context(s);
699 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
700 wv_check_crc(s, crc, crc_extra_bits))
701 return AVERROR_INVALIDDATA;
706 static av_cold int wv_alloc_frame_context(WavpackContext *c)
708 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
711 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
712 if (!c->fdec[c->fdec_num])
715 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
716 wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
721 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
723 WavpackContext *s = avctx->priv_data;
727 if (avctx->channels <= 2 && !avctx->channel_layout)
728 avctx->channel_layout = (avctx->channels == 2) ? AV_CH_LAYOUT_STEREO
731 s->multichannel = avctx->channels > 2;
738 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
740 WavpackContext *s = avctx->priv_data;
743 for (i = 0; i < s->fdec_num; i++)
744 av_freep(&s->fdec[i]);
750 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
751 uint8_t **data, const uint8_t *buf, int buf_size)
753 WavpackContext *wc = avctx->priv_data;
754 WavpackFrameContext *s;
756 void *samples_l, *samples_r;
758 int got_terms = 0, got_weights = 0, got_samples = 0,
759 got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
760 int i, j, id, size, ssize, weights, t;
761 int bpp, chan, chmask, orig_bpp;
763 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
764 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
765 return AVERROR_INVALIDDATA;
768 s = wc->fdec[block_no];
770 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
772 return AVERROR_INVALIDDATA;
775 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
776 memset(s->ch, 0, sizeof(s->ch));
778 s->and = s->or = s->shift = 0;
779 s->got_extra_bits = 0;
781 bytestream2_init(&gb, buf, buf_size);
783 s->samples = bytestream2_get_le32(&gb);
784 if (s->samples != wc->samples) {
785 av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
786 "a sequence: %d and %d\n", wc->samples, s->samples);
787 return AVERROR_INVALIDDATA;
789 s->frame_flags = bytestream2_get_le32(&gb);
790 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
791 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
793 s->stereo = !(s->frame_flags & WV_MONO);
794 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
795 s->joint = s->frame_flags & WV_JOINT_STEREO;
796 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
797 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
798 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
799 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
800 s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
801 s->CRC = bytestream2_get_le32(&gb);
803 if (wc->ch_offset + s->stereo >= avctx->channels) {
804 av_log(avctx, AV_LOG_ERROR, "too many channels\n");
808 samples_l = data[wc->ch_offset];
810 samples_r = data[wc->ch_offset + 1];
812 wc->ch_offset += 1 + s->stereo;
814 // parse metadata blocks
815 while (bytestream2_get_bytes_left(&gb)) {
816 id = bytestream2_get_byte(&gb);
817 size = bytestream2_get_byte(&gb);
818 if (id & WP_IDF_LONG) {
819 size |= (bytestream2_get_byte(&gb)) << 8;
820 size |= (bytestream2_get_byte(&gb)) << 16;
822 size <<= 1; // size is specified in words
827 av_log(avctx, AV_LOG_ERROR,
828 "Got incorrect block %02X with size %i\n", id, size);
831 if (bytestream2_get_bytes_left(&gb) < ssize) {
832 av_log(avctx, AV_LOG_ERROR,
833 "Block size %i is out of bounds\n", size);
836 if (id & WP_IDF_IGNORE) {
837 bytestream2_skip(&gb, ssize);
840 switch (id & WP_IDF_MASK) {
842 if (size > MAX_TERMS) {
843 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
845 bytestream2_skip(&gb, ssize);
849 for (i = 0; i < s->terms; i++) {
850 uint8_t val = bytestream2_get_byte(&gb);
851 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
852 s->decorr[s->terms - i - 1].delta = val >> 5;
856 case WP_ID_DECWEIGHTS:
858 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
861 weights = size >> s->stereo_in;
862 if (weights > MAX_TERMS || weights > s->terms) {
863 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
864 bytestream2_skip(&gb, ssize);
867 for (i = 0; i < weights; i++) {
868 t = (int8_t)bytestream2_get_byte(&gb);
869 s->decorr[s->terms - i - 1].weightA = t << 3;
870 if (s->decorr[s->terms - i - 1].weightA > 0)
871 s->decorr[s->terms - i - 1].weightA +=
872 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
874 t = (int8_t)bytestream2_get_byte(&gb);
875 s->decorr[s->terms - i - 1].weightB = t << 3;
876 if (s->decorr[s->terms - i - 1].weightB > 0)
877 s->decorr[s->terms - i - 1].weightB +=
878 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
883 case WP_ID_DECSAMPLES:
885 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
889 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
890 if (s->decorr[i].value > 8) {
891 s->decorr[i].samplesA[0] =
892 wp_exp2(bytestream2_get_le16(&gb));
893 s->decorr[i].samplesA[1] =
894 wp_exp2(bytestream2_get_le16(&gb));
897 s->decorr[i].samplesB[0] =
898 wp_exp2(bytestream2_get_le16(&gb));
899 s->decorr[i].samplesB[1] =
900 wp_exp2(bytestream2_get_le16(&gb));
904 } else if (s->decorr[i].value < 0) {
905 s->decorr[i].samplesA[0] =
906 wp_exp2(bytestream2_get_le16(&gb));
907 s->decorr[i].samplesB[0] =
908 wp_exp2(bytestream2_get_le16(&gb));
911 for (j = 0; j < s->decorr[i].value; j++) {
912 s->decorr[i].samplesA[j] =
913 wp_exp2(bytestream2_get_le16(&gb));
915 s->decorr[i].samplesB[j] =
916 wp_exp2(bytestream2_get_le16(&gb));
919 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
925 if (size != 6 * (s->stereo_in + 1)) {
926 av_log(avctx, AV_LOG_ERROR,
927 "Entropy vars size should be %i, got %i",
928 6 * (s->stereo_in + 1), size);
929 bytestream2_skip(&gb, ssize);
932 for (j = 0; j <= s->stereo_in; j++)
933 for (i = 0; i < 3; i++) {
934 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
939 if (s->hybrid_bitrate) {
940 for (i = 0; i <= s->stereo_in; i++) {
941 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
945 for (i = 0; i < (s->stereo_in + 1); i++) {
946 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
950 for (i = 0; i < (s->stereo_in + 1); i++) {
951 s->ch[i].bitrate_delta =
952 wp_exp2((int16_t)bytestream2_get_le16(&gb));
955 for (i = 0; i < (s->stereo_in + 1); i++)
956 s->ch[i].bitrate_delta = 0;
960 case WP_ID_INT32INFO: {
963 av_log(avctx, AV_LOG_ERROR,
964 "Invalid INT32INFO, size = %i\n",
966 bytestream2_skip(&gb, ssize - 4);
969 bytestream2_get_buffer(&gb, val, 4);
971 s->extra_bits = val[0];
981 /* original WavPack decoder forces 32-bit lossy sound to be treated
982 * as 24-bit one in order to have proper clipping */
983 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
986 s->hybrid_maxclip >>= 8;
987 s->hybrid_minclip >>= 8;
991 case WP_ID_FLOATINFO:
993 av_log(avctx, AV_LOG_ERROR,
994 "Invalid FLOATINFO, size = %i\n", size);
995 bytestream2_skip(&gb, ssize);
998 s->float_flag = bytestream2_get_byte(&gb);
999 s->float_shift = bytestream2_get_byte(&gb);
1000 s->float_max_exp = bytestream2_get_byte(&gb);
1002 bytestream2_skip(&gb, 1);
1005 s->sc.offset = bytestream2_tell(&gb);
1006 s->sc.size = size * 8;
1007 init_get_bits(&s->gb, gb.buffer, size * 8);
1008 s->data_size = size * 8;
1009 bytestream2_skip(&gb, size);
1012 case WP_ID_EXTRABITS:
1014 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
1016 bytestream2_skip(&gb, size);
1019 s->extra_sc.offset = bytestream2_tell(&gb);
1020 s->extra_sc.size = size * 8;
1021 init_get_bits(&s->gb_extra_bits, gb.buffer, size * 8);
1022 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
1023 bytestream2_skip(&gb, size);
1024 s->got_extra_bits = 1;
1026 case WP_ID_CHANINFO:
1028 av_log(avctx, AV_LOG_ERROR,
1029 "Insufficient channel information\n");
1030 return AVERROR_INVALIDDATA;
1032 chan = bytestream2_get_byte(&gb);
1035 chmask = bytestream2_get_byte(&gb);
1038 chmask = bytestream2_get_le16(&gb);
1041 chmask = bytestream2_get_le24(&gb);
1044 chmask = bytestream2_get_le32(&gb);
1047 bytestream2_skip(&gb, 1);
1048 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1049 chmask = bytestream2_get_le16(&gb);
1052 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1054 chan = avctx->channels;
1055 chmask = avctx->channel_layout;
1057 if (chan != avctx->channels) {
1058 av_log(avctx, AV_LOG_ERROR,
1059 "Block reports total %d channels, "
1060 "decoder believes it's %d channels\n",
1061 chan, avctx->channels);
1062 return AVERROR_INVALIDDATA;
1064 if (!avctx->channel_layout)
1065 avctx->channel_layout = chmask;
1068 bytestream2_skip(&gb, size);
1070 if (id & WP_IDF_ODD)
1071 bytestream2_skip(&gb, 1);
1075 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1076 return AVERROR_INVALIDDATA;
1079 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1080 return AVERROR_INVALIDDATA;
1083 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1084 return AVERROR_INVALIDDATA;
1087 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1088 return AVERROR_INVALIDDATA;
1090 if (s->hybrid && !got_hybrid) {
1091 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1092 return AVERROR_INVALIDDATA;
1095 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1096 return AVERROR_INVALIDDATA;
1098 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
1099 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1100 return AVERROR_INVALIDDATA;
1102 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
1103 const int size = get_bits_left(&s->gb_extra_bits);
1104 const int wanted = s->samples * s->extra_bits << s->stereo_in;
1105 if (size < wanted) {
1106 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1107 s->got_extra_bits = 0;
1112 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1116 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1121 memcpy(samples_r, samples_l, bpp * s->samples);
1127 static void wavpack_decode_flush(AVCodecContext *avctx)
1129 WavpackContext *s = avctx->priv_data;
1132 for (i = 0; i < s->fdec_num; i++)
1133 wv_reset_saved_context(s->fdec[i]);
1136 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
1137 int *got_frame_ptr, AVPacket *avpkt)
1139 WavpackContext *s = avctx->priv_data;
1140 const uint8_t *buf = avpkt->data;
1141 int buf_size = avpkt->size;
1142 AVFrame *frame = data;
1143 int frame_size, ret, frame_flags;
1145 if (avpkt->size <= WV_HEADER_SIZE)
1146 return AVERROR_INVALIDDATA;
1151 /* determine number of samples */
1152 s->samples = AV_RL32(buf + 20);
1153 frame_flags = AV_RL32(buf + 24);
1154 if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1155 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1157 return AVERROR_INVALIDDATA;
1160 if (frame_flags & 0x80) {
1161 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1162 } else if ((frame_flags & 0x03) <= 1) {
1163 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1165 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1166 avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
1169 /* get output buffer */
1170 frame->nb_samples = s->samples + 1;
1171 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1173 frame->nb_samples = s->samples;
1175 while (buf_size > 0) {
1176 if (buf_size <= WV_HEADER_SIZE)
1178 frame_size = AV_RL32(buf + 4) - 12;
1181 if (frame_size <= 0 || frame_size > buf_size) {
1182 av_log(avctx, AV_LOG_ERROR,
1183 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1184 s->block, frame_size, buf_size);
1185 wavpack_decode_flush(avctx);
1186 return AVERROR_INVALIDDATA;
1188 if ((ret = wavpack_decode_block(avctx, s->block,
1189 frame->extended_data,
1190 buf, frame_size)) < 0) {
1191 wavpack_decode_flush(avctx);
1196 buf_size -= frame_size;
1204 AVCodec ff_wavpack_decoder = {
1206 .type = AVMEDIA_TYPE_AUDIO,
1207 .id = AV_CODEC_ID_WAVPACK,
1208 .priv_data_size = sizeof(WavpackContext),
1209 .init = wavpack_decode_init,
1210 .close = wavpack_decode_end,
1211 .decode = wavpack_decode_frame,
1212 .flush = wavpack_decode_flush,
1213 .capabilities = CODEC_CAP_DR1,
1214 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),