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 #include "libavutil/channel_layout.h"
24 #define BITSTREAM_READER_LE
26 #include "bytestream.h"
35 * WavPack lossless audio decoder
38 typedef struct SavedContext {
45 typedef struct WavpackFrameContext {
46 AVCodecContext *avctx;
48 int stereo, stereo_in;
53 uint32_t crc_extra_bits;
54 GetBitContext gb_extra_bits;
55 int data_size; // in bits
58 Decorr decorr[MAX_TERMS];
59 int zero, one, zeroes;
63 int hybrid, hybrid_bitrate;
64 int hybrid_maxclip, hybrid_minclip;
70 SavedContext sc, extra_sc;
71 } WavpackFrameContext;
73 #define WV_MAX_FRAME_DECODERS 14
75 typedef struct WavpackContext {
76 AVCodecContext *avctx;
78 WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
86 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
88 static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
95 e = (1 << (p + 1)) - k - 1;
96 res = get_bitsz(gb, p);
98 res = (res << 1) - e + get_bits1(gb);
102 static int update_error_limit(WavpackFrameContext *ctx)
106 for (i = 0; i <= ctx->stereo_in; i++) {
107 if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
108 return AVERROR_INVALIDDATA;
109 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
110 br[i] = ctx->ch[i].bitrate_acc >> 16;
111 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
113 if (ctx->stereo_in && ctx->hybrid_bitrate) {
114 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
115 if (balance > br[0]) {
118 } else if (-balance > br[0]) {
122 br[1] = br[0] + balance;
123 br[0] = br[0] - balance;
126 for (i = 0; i <= ctx->stereo_in; i++) {
127 if (ctx->hybrid_bitrate) {
128 if (sl[i] - br[i] > -0x100)
129 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
131 ctx->ch[i].error_limit = 0;
133 ctx->ch[i].error_limit = wp_exp2(br[i]);
140 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
141 int channel, int *last)
144 int sign, base, add, ret;
145 WvChannel *c = &ctx->ch[channel];
149 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
150 !ctx->zero && !ctx->one) {
154 c->slow_level -= LEVEL_DECAY(c->slow_level);
158 t = get_unary_0_33(gb);
160 if (t >= 32 || get_bits_left(gb) < t - 1)
162 t = get_bits_long(gb, t - 1) | (1 << (t - 1));
164 if (get_bits_left(gb) < 0)
169 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
170 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
171 c->slow_level -= LEVEL_DECAY(c->slow_level);
181 t = get_unary_0_33(gb);
182 if (get_bits_left(gb) < 0)
185 t2 = get_unary_0_33(gb);
187 if (get_bits_left(gb) < 0)
191 if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
193 t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
204 ctx->zero = !ctx->one;
207 if (ctx->hybrid && !channel) {
208 if (update_error_limit(ctx) < 0)
214 add = GET_MED(0) - 1;
218 add = GET_MED(1) - 1;
222 base = GET_MED(0) + GET_MED(1);
223 add = GET_MED(2) - 1;
228 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
229 add = GET_MED(2) - 1;
234 if (!c->error_limit) {
235 if (add >= 0x2000000U) {
236 av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
239 ret = base + get_tail(gb, add);
240 if (get_bits_left(gb) <= 0)
243 int mid = (base * 2U + add + 1) >> 1;
244 while (add > c->error_limit) {
245 if (get_bits_left(gb) <= 0)
248 add -= (mid - (unsigned)base);
251 add = mid - (unsigned)base - 1;
252 mid = (base * 2U + add + 1) >> 1;
256 sign = get_bits1(gb);
257 if (ctx->hybrid_bitrate)
258 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
259 return sign ? ~ret : ret;
262 ret = get_bits_left(gb);
264 av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
270 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
276 S *= 1 << s->extra_bits;
278 if (s->got_extra_bits &&
279 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
280 S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
281 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
285 bit = (S & s->and) | s->or;
286 bit = ((S + bit) << s->shift) - bit;
289 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
291 return bit << s->post_shift;
294 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
302 int exp = s->float_max_exp;
304 if (s->got_extra_bits) {
305 const int max_bits = 1 + 23 + 8 + 1;
306 const int left_bits = get_bits_left(&s->gb_extra_bits);
308 if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
313 S *= 1U << s->float_shift;
317 if (S >= 0x1000000U) {
318 if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
319 S = get_bits(&s->gb_extra_bits, 23);
324 int shift = 23 - av_log2(S);
325 exp = s->float_max_exp;
332 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
333 (s->got_extra_bits &&
334 (s->float_flag & WV_FLT_SHIFT_SAME) &&
335 get_bits1(&s->gb_extra_bits))) {
336 S |= (1 << shift) - 1;
337 } else if (s->got_extra_bits &&
338 (s->float_flag & WV_FLT_SHIFT_SENT)) {
339 S |= get_bits(&s->gb_extra_bits, shift);
343 exp = s->float_max_exp;
349 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
350 if (get_bits1(&s->gb_extra_bits)) {
351 S = get_bits(&s->gb_extra_bits, 23);
352 if (s->float_max_exp >= 25)
353 exp = get_bits(&s->gb_extra_bits, 8);
354 sign = get_bits1(&s->gb_extra_bits);
356 if (s->float_flag & WV_FLT_ZERO_SIGN)
357 sign = get_bits1(&s->gb_extra_bits);
362 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
364 value.u = (sign << 31) | (exp << 23) | S;
368 static void wv_reset_saved_context(WavpackFrameContext *s)
371 s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
374 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
375 uint32_t crc_extra_bits)
378 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
379 return AVERROR_INVALIDDATA;
381 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
382 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
383 return AVERROR_INVALIDDATA;
389 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
390 void *dst_l, void *dst_r, const int type)
394 int A, B, L, L2, R, R2;
396 uint32_t crc = s->sc.crc;
397 uint32_t crc_extra_bits = s->extra_sc.crc;
398 int16_t *dst16_l = dst_l;
399 int16_t *dst16_r = dst_r;
400 int32_t *dst32_l = dst_l;
401 int32_t *dst32_r = dst_r;
402 float *dstfl_l = dst_l;
403 float *dstfl_r = dst_r;
405 s->one = s->zero = s->zeroes = 0;
407 L = wv_get_value(s, gb, 0, &last);
410 R = wv_get_value(s, gb, 1, &last);
413 for (i = 0; i < s->terms; i++) {
414 t = s->decorr[i].value;
418 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
419 B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
421 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
422 B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
424 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
425 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
428 A = s->decorr[i].samplesA[pos];
429 B = s->decorr[i].samplesB[pos];
432 if (type != AV_SAMPLE_FMT_S16P) {
433 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
434 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
436 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
437 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
440 s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
442 s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
443 s->decorr[i].samplesA[j] = L = L2;
444 s->decorr[i].samplesB[j] = R = R2;
445 } else if (t == -1) {
446 if (type != AV_SAMPLE_FMT_S16P)
447 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
449 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
450 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
452 if (type != AV_SAMPLE_FMT_S16P)
453 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
455 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
456 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
458 s->decorr[i].samplesA[0] = R;
460 if (type != AV_SAMPLE_FMT_S16P)
461 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
463 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
464 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
468 R2 = s->decorr[i].samplesA[0];
469 s->decorr[i].samplesA[0] = R;
472 if (type != AV_SAMPLE_FMT_S16P)
473 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
475 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
476 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
478 s->decorr[i].samplesB[0] = L;
482 if (type == AV_SAMPLE_FMT_S16P) {
483 if (FFABS((int64_t)L) + FFABS((int64_t)R) > (1<<19)) {
484 av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
485 return AVERROR_INVALIDDATA;
491 L += (unsigned)(R -= (unsigned)(L >> 1));
492 crc = (crc * 3 + L) * 3 + R;
494 if (type == AV_SAMPLE_FMT_FLTP) {
495 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
496 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
497 } else if (type == AV_SAMPLE_FMT_S32P) {
498 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
499 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
501 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
502 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
505 } while (!last && count < s->samples);
507 wv_reset_saved_context(s);
509 if (last && count < s->samples) {
510 int size = av_get_bytes_per_sample(type);
511 memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
512 memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
515 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
516 wv_check_crc(s, crc, crc_extra_bits))
517 return AVERROR_INVALIDDATA;
522 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
523 void *dst, const int type)
529 uint32_t crc = s->sc.crc;
530 uint32_t crc_extra_bits = s->extra_sc.crc;
531 int16_t *dst16 = dst;
532 int32_t *dst32 = dst;
535 s->one = s->zero = s->zeroes = 0;
537 T = wv_get_value(s, gb, 0, &last);
541 for (i = 0; i < s->terms; i++) {
542 t = s->decorr[i].value;
545 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
547 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
548 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
551 A = s->decorr[i].samplesA[pos];
554 if (type != AV_SAMPLE_FMT_S16P)
555 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
557 S = T + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
559 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
560 s->decorr[i].samplesA[j] = T = S;
565 if (type == AV_SAMPLE_FMT_FLTP) {
566 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
567 } else if (type == AV_SAMPLE_FMT_S32P) {
568 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
570 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
573 } while (!last && count < s->samples);
575 wv_reset_saved_context(s);
577 if (last && count < s->samples) {
578 int size = av_get_bytes_per_sample(type);
579 memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
582 if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
583 int ret = wv_check_crc(s, crc, crc_extra_bits);
584 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
591 static av_cold int wv_alloc_frame_context(WavpackContext *c)
593 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
596 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
597 if (!c->fdec[c->fdec_num])
600 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
601 wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
607 static int init_thread_copy(AVCodecContext *avctx)
609 WavpackContext *s = avctx->priv_data;
615 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
617 WavpackContext *s = avctx->priv_data;
626 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
628 WavpackContext *s = avctx->priv_data;
631 for (i = 0; i < s->fdec_num; i++)
632 av_freep(&s->fdec[i]);
638 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
639 AVFrame *frame, const uint8_t *buf, int buf_size)
641 WavpackContext *wc = avctx->priv_data;
642 ThreadFrame tframe = { .f = frame };
643 WavpackFrameContext *s;
645 void *samples_l = NULL, *samples_r = NULL;
647 int got_terms = 0, got_weights = 0, got_samples = 0,
648 got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
649 int i, j, id, size, ssize, weights, t;
650 int bpp, chan = 0, chmask = 0, orig_bpp, sample_rate = 0;
653 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
654 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
655 return AVERROR_INVALIDDATA;
658 s = wc->fdec[block_no];
660 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
662 return AVERROR_INVALIDDATA;
665 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
666 memset(s->ch, 0, sizeof(s->ch));
668 s->and = s->or = s->shift = 0;
669 s->got_extra_bits = 0;
671 bytestream2_init(&gb, buf, buf_size);
673 s->samples = bytestream2_get_le32(&gb);
674 if (s->samples != wc->samples) {
675 av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
676 "a sequence: %d and %d\n", wc->samples, s->samples);
677 return AVERROR_INVALIDDATA;
679 s->frame_flags = bytestream2_get_le32(&gb);
680 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
681 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
682 multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
684 s->stereo = !(s->frame_flags & WV_MONO);
685 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
686 s->joint = s->frame_flags & WV_JOINT_STEREO;
687 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
688 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
689 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
690 if (s->post_shift < 0 || s->post_shift > 31) {
691 return AVERROR_INVALIDDATA;
693 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
694 s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
695 s->CRC = bytestream2_get_le32(&gb);
697 // parse metadata blocks
698 while (bytestream2_get_bytes_left(&gb)) {
699 id = bytestream2_get_byte(&gb);
700 size = bytestream2_get_byte(&gb);
701 if (id & WP_IDF_LONG) {
702 size |= (bytestream2_get_byte(&gb)) << 8;
703 size |= (bytestream2_get_byte(&gb)) << 16;
705 size <<= 1; // size is specified in words
710 av_log(avctx, AV_LOG_ERROR,
711 "Got incorrect block %02X with size %i\n", id, size);
714 if (bytestream2_get_bytes_left(&gb) < ssize) {
715 av_log(avctx, AV_LOG_ERROR,
716 "Block size %i is out of bounds\n", size);
719 switch (id & WP_IDF_MASK) {
721 if (size > MAX_TERMS) {
722 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
724 bytestream2_skip(&gb, ssize);
728 for (i = 0; i < s->terms; i++) {
729 uint8_t val = bytestream2_get_byte(&gb);
730 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
731 s->decorr[s->terms - i - 1].delta = val >> 5;
735 case WP_ID_DECWEIGHTS:
737 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
740 weights = size >> s->stereo_in;
741 if (weights > MAX_TERMS || weights > s->terms) {
742 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
743 bytestream2_skip(&gb, ssize);
746 for (i = 0; i < weights; i++) {
747 t = (int8_t)bytestream2_get_byte(&gb);
748 s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
749 if (s->decorr[s->terms - i - 1].weightA > 0)
750 s->decorr[s->terms - i - 1].weightA +=
751 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
753 t = (int8_t)bytestream2_get_byte(&gb);
754 s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
755 if (s->decorr[s->terms - i - 1].weightB > 0)
756 s->decorr[s->terms - i - 1].weightB +=
757 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
762 case WP_ID_DECSAMPLES:
764 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
768 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
769 if (s->decorr[i].value > 8) {
770 s->decorr[i].samplesA[0] =
771 wp_exp2(bytestream2_get_le16(&gb));
772 s->decorr[i].samplesA[1] =
773 wp_exp2(bytestream2_get_le16(&gb));
776 s->decorr[i].samplesB[0] =
777 wp_exp2(bytestream2_get_le16(&gb));
778 s->decorr[i].samplesB[1] =
779 wp_exp2(bytestream2_get_le16(&gb));
783 } else if (s->decorr[i].value < 0) {
784 s->decorr[i].samplesA[0] =
785 wp_exp2(bytestream2_get_le16(&gb));
786 s->decorr[i].samplesB[0] =
787 wp_exp2(bytestream2_get_le16(&gb));
790 for (j = 0; j < s->decorr[i].value; j++) {
791 s->decorr[i].samplesA[j] =
792 wp_exp2(bytestream2_get_le16(&gb));
794 s->decorr[i].samplesB[j] =
795 wp_exp2(bytestream2_get_le16(&gb));
798 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
804 if (size != 6 * (s->stereo_in + 1)) {
805 av_log(avctx, AV_LOG_ERROR,
806 "Entropy vars size should be %i, got %i.\n",
807 6 * (s->stereo_in + 1), size);
808 bytestream2_skip(&gb, ssize);
811 for (j = 0; j <= s->stereo_in; j++)
812 for (i = 0; i < 3; i++) {
813 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
818 if (s->hybrid_bitrate) {
819 for (i = 0; i <= s->stereo_in; i++) {
820 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
824 for (i = 0; i < (s->stereo_in + 1); i++) {
825 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
829 for (i = 0; i < (s->stereo_in + 1); i++) {
830 s->ch[i].bitrate_delta =
831 wp_exp2((int16_t)bytestream2_get_le16(&gb));
834 for (i = 0; i < (s->stereo_in + 1); i++)
835 s->ch[i].bitrate_delta = 0;
839 case WP_ID_INT32INFO: {
842 av_log(avctx, AV_LOG_ERROR,
843 "Invalid INT32INFO, size = %i\n",
845 bytestream2_skip(&gb, ssize - 4);
848 bytestream2_get_buffer(&gb, val, 4);
850 av_log(avctx, AV_LOG_ERROR,
851 "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
854 s->extra_bits = val[0];
865 av_log(avctx, AV_LOG_ERROR,
866 "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
867 s->and = s->or = s->shift = 0;
870 /* original WavPack decoder forces 32-bit lossy sound to be treated
871 * as 24-bit one in order to have proper clipping */
872 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
875 s->hybrid_maxclip >>= 8;
876 s->hybrid_minclip >>= 8;
880 case WP_ID_FLOATINFO:
882 av_log(avctx, AV_LOG_ERROR,
883 "Invalid FLOATINFO, size = %i\n", size);
884 bytestream2_skip(&gb, ssize);
887 s->float_flag = bytestream2_get_byte(&gb);
888 s->float_shift = bytestream2_get_byte(&gb);
889 s->float_max_exp = bytestream2_get_byte(&gb);
890 if (s->float_shift > 31) {
891 av_log(avctx, AV_LOG_ERROR,
892 "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
897 bytestream2_skip(&gb, 1);
900 s->sc.offset = bytestream2_tell(&gb);
901 s->sc.size = size * 8;
902 if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
904 s->data_size = size * 8;
905 bytestream2_skip(&gb, size);
908 case WP_ID_EXTRABITS:
910 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
912 bytestream2_skip(&gb, size);
915 s->extra_sc.offset = bytestream2_tell(&gb);
916 s->extra_sc.size = size * 8;
917 if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
919 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
920 bytestream2_skip(&gb, size);
921 s->got_extra_bits = 1;
925 av_log(avctx, AV_LOG_ERROR,
926 "Insufficient channel information\n");
927 return AVERROR_INVALIDDATA;
929 chan = bytestream2_get_byte(&gb);
932 chmask = bytestream2_get_byte(&gb);
935 chmask = bytestream2_get_le16(&gb);
938 chmask = bytestream2_get_le24(&gb);
941 chmask = bytestream2_get_le32(&gb);
944 size = bytestream2_get_byte(&gb);
945 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
947 if (avctx->channels != chan)
948 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
949 " instead of %i.\n", chan, avctx->channels);
950 chmask = bytestream2_get_le24(&gb);
953 size = bytestream2_get_byte(&gb);
954 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
956 if (avctx->channels != chan)
957 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
958 " instead of %i.\n", chan, avctx->channels);
959 chmask = bytestream2_get_le32(&gb);
962 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
964 chan = avctx->channels;
965 chmask = avctx->channel_layout;
968 case WP_ID_SAMPLE_RATE:
970 av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
971 return AVERROR_INVALIDDATA;
973 sample_rate = bytestream2_get_le24(&gb);
976 bytestream2_skip(&gb, size);
979 bytestream2_skip(&gb, 1);
983 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
984 return AVERROR_INVALIDDATA;
987 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
988 return AVERROR_INVALIDDATA;
991 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
992 return AVERROR_INVALIDDATA;
995 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
996 return AVERROR_INVALIDDATA;
998 if (s->hybrid && !got_hybrid) {
999 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1000 return AVERROR_INVALIDDATA;
1003 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1004 return AVERROR_INVALIDDATA;
1006 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
1007 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1008 return AVERROR_INVALIDDATA;
1010 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
1011 const int size = get_bits_left(&s->gb_extra_bits);
1012 const int wanted = s->samples * s->extra_bits << s->stereo_in;
1013 if (size < wanted) {
1014 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1015 s->got_extra_bits = 0;
1019 if (!wc->ch_offset) {
1020 int sr = (s->frame_flags >> 23) & 0xf;
1023 av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
1024 return AVERROR_INVALIDDATA;
1026 avctx->sample_rate = sample_rate;
1028 avctx->sample_rate = wv_rates[sr];
1032 avctx->channels = chan;
1034 avctx->channel_layout = chmask;
1036 avctx->channels = s->stereo ? 2 : 1;
1037 avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :
1041 /* get output buffer */
1042 frame->nb_samples = s->samples + 1;
1043 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
1045 frame->nb_samples = s->samples;
1048 if (wc->ch_offset + s->stereo >= avctx->channels) {
1049 av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
1050 return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
1053 samples_l = frame->extended_data[wc->ch_offset];
1055 samples_r = frame->extended_data[wc->ch_offset + 1];
1057 wc->ch_offset += 1 + s->stereo;
1060 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1064 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1069 memcpy(samples_r, samples_l, bpp * s->samples);
1075 static void wavpack_decode_flush(AVCodecContext *avctx)
1077 WavpackContext *s = avctx->priv_data;
1080 for (i = 0; i < s->fdec_num; i++)
1081 wv_reset_saved_context(s->fdec[i]);
1084 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
1085 int *got_frame_ptr, AVPacket *avpkt)
1087 WavpackContext *s = avctx->priv_data;
1088 const uint8_t *buf = avpkt->data;
1089 int buf_size = avpkt->size;
1090 AVFrame *frame = data;
1091 int frame_size, ret, frame_flags;
1093 if (avpkt->size <= WV_HEADER_SIZE)
1094 return AVERROR_INVALIDDATA;
1099 /* determine number of samples */
1100 s->samples = AV_RL32(buf + 20);
1101 frame_flags = AV_RL32(buf + 24);
1102 if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1103 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1105 return AVERROR_INVALIDDATA;
1108 if (frame_flags & 0x80) {
1109 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1110 } else if ((frame_flags & 0x03) <= 1) {
1111 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1113 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1114 avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
1117 while (buf_size > 0) {
1118 if (buf_size <= WV_HEADER_SIZE)
1120 frame_size = AV_RL32(buf + 4) - 12;
1123 if (frame_size <= 0 || frame_size > buf_size) {
1124 av_log(avctx, AV_LOG_ERROR,
1125 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1126 s->block, frame_size, buf_size);
1127 wavpack_decode_flush(avctx);
1128 return AVERROR_INVALIDDATA;
1130 if ((ret = wavpack_decode_block(avctx, s->block,
1131 frame, buf, frame_size)) < 0) {
1132 wavpack_decode_flush(avctx);
1137 buf_size -= frame_size;
1140 if (s->ch_offset != avctx->channels) {
1141 av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1142 return AVERROR_INVALIDDATA;
1150 AVCodec ff_wavpack_decoder = {
1152 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1153 .type = AVMEDIA_TYPE_AUDIO,
1154 .id = AV_CODEC_ID_WAVPACK,
1155 .priv_data_size = sizeof(WavpackContext),
1156 .init = wavpack_decode_init,
1157 .close = wavpack_decode_end,
1158 .decode = wavpack_decode_frame,
1159 .flush = wavpack_decode_flush,
1160 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
1161 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,