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"
30 #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 int get_tail(GetBitContext *gb, int k)
95 e = (1 << (p + 1)) - k - 1;
96 res = p ? get_bits(gb, p) : 0;
98 res = (res << 1) - e + get_bits1(gb);
102 static void update_error_limit(WavpackFrameContext *ctx)
106 for (i = 0; i <= ctx->stereo_in; i++) {
107 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
108 br[i] = ctx->ch[i].bitrate_acc >> 16;
109 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
111 if (ctx->stereo_in && ctx->hybrid_bitrate) {
112 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
113 if (balance > br[0]) {
116 } else if (-balance > br[0]) {
120 br[1] = br[0] + balance;
121 br[0] = br[0] - balance;
124 for (i = 0; i <= ctx->stereo_in; i++) {
125 if (ctx->hybrid_bitrate) {
126 if (sl[i] - br[i] > -0x100)
127 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
129 ctx->ch[i].error_limit = 0;
131 ctx->ch[i].error_limit = wp_exp2(br[i]);
136 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
137 int channel, int *last)
140 int sign, base, add, ret;
141 WvChannel *c = &ctx->ch[channel];
145 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
146 !ctx->zero && !ctx->one) {
150 c->slow_level -= LEVEL_DECAY(c->slow_level);
154 t = get_unary_0_33(gb);
156 if (get_bits_left(gb) < t - 1)
158 t = get_bits(gb, t - 1) | (1 << (t - 1));
160 if (get_bits_left(gb) < 0)
165 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
166 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
167 c->slow_level -= LEVEL_DECAY(c->slow_level);
177 t = get_unary_0_33(gb);
178 if (get_bits_left(gb) < 0)
181 t2 = get_unary_0_33(gb);
183 if (get_bits_left(gb) < 0)
187 if (get_bits_left(gb) < t2 - 1)
189 t += get_bits(gb, t2 - 1) | (1 << (t2 - 1));
200 ctx->zero = !ctx->one;
203 if (ctx->hybrid && !channel)
204 update_error_limit(ctx);
208 add = GET_MED(0) - 1;
212 add = GET_MED(1) - 1;
216 base = GET_MED(0) + GET_MED(1);
217 add = GET_MED(2) - 1;
222 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2);
223 add = GET_MED(2) - 1;
228 if (!c->error_limit) {
229 if (add >= 0x2000000U) {
230 av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
233 ret = base + get_tail(gb, add);
234 if (get_bits_left(gb) <= 0)
237 int mid = (base * 2 + add + 1) >> 1;
238 while (add > c->error_limit) {
239 if (get_bits_left(gb) <= 0)
245 add = mid - base - 1;
246 mid = (base * 2 + add + 1) >> 1;
250 sign = get_bits1(gb);
251 if (ctx->hybrid_bitrate)
252 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
253 return sign ? ~ret : ret;
260 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
268 if (s->got_extra_bits &&
269 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
270 S |= get_bits(&s->gb_extra_bits, s->extra_bits);
271 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
275 bit = (S & s->and) | s->or;
276 bit = ((S + bit) << s->shift) - bit;
279 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
281 return bit << s->post_shift;
284 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
292 int exp = s->float_max_exp;
294 if (s->got_extra_bits) {
295 const int max_bits = 1 + 23 + 8 + 1;
296 const int left_bits = get_bits_left(&s->gb_extra_bits);
298 if (left_bits + 8 * FF_INPUT_BUFFER_PADDING_SIZE < max_bits)
303 S <<= s->float_shift;
307 if (S >= 0x1000000) {
308 if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
309 S = get_bits(&s->gb_extra_bits, 23);
314 int shift = 23 - av_log2(S);
315 exp = s->float_max_exp;
322 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
323 (s->got_extra_bits &&
324 (s->float_flag & WV_FLT_SHIFT_SAME) &&
325 get_bits1(&s->gb_extra_bits))) {
326 S |= (1 << shift) - 1;
327 } else if (s->got_extra_bits &&
328 (s->float_flag & WV_FLT_SHIFT_SENT)) {
329 S |= get_bits(&s->gb_extra_bits, shift);
333 exp = s->float_max_exp;
339 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
340 if (get_bits1(&s->gb_extra_bits)) {
341 S = get_bits(&s->gb_extra_bits, 23);
342 if (s->float_max_exp >= 25)
343 exp = get_bits(&s->gb_extra_bits, 8);
344 sign = get_bits1(&s->gb_extra_bits);
346 if (s->float_flag & WV_FLT_ZERO_SIGN)
347 sign = get_bits1(&s->gb_extra_bits);
352 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
354 value.u = (sign << 31) | (exp << 23) | S;
358 static void wv_reset_saved_context(WavpackFrameContext *s)
361 s->sc.crc = s->extra_sc.crc = 0xFFFFFFFF;
364 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
365 uint32_t crc_extra_bits)
368 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
369 return AVERROR_INVALIDDATA;
371 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
372 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
373 return AVERROR_INVALIDDATA;
379 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
380 void *dst_l, void *dst_r, const int type)
384 int A, B, L, L2, R, R2;
386 uint32_t crc = s->sc.crc;
387 uint32_t crc_extra_bits = s->extra_sc.crc;
388 int16_t *dst16_l = dst_l;
389 int16_t *dst16_r = dst_r;
390 int32_t *dst32_l = dst_l;
391 int32_t *dst32_r = dst_r;
392 float *dstfl_l = dst_l;
393 float *dstfl_r = dst_r;
395 s->one = s->zero = s->zeroes = 0;
397 L = wv_get_value(s, gb, 0, &last);
400 R = wv_get_value(s, gb, 1, &last);
403 for (i = 0; i < s->terms; i++) {
404 t = s->decorr[i].value;
408 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
409 B = 2 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
411 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
412 B = (3 * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
414 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
415 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
418 A = s->decorr[i].samplesA[pos];
419 B = s->decorr[i].samplesB[pos];
422 if (type != AV_SAMPLE_FMT_S16P) {
423 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
424 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
426 L2 = L + ((s->decorr[i].weightA * A + 512) >> 10);
427 R2 = R + ((s->decorr[i].weightB * B + 512) >> 10);
430 s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
432 s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
433 s->decorr[i].samplesA[j] = L = L2;
434 s->decorr[i].samplesB[j] = R = R2;
435 } else if (t == -1) {
436 if (type != AV_SAMPLE_FMT_S16P)
437 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
439 L2 = L + ((s->decorr[i].weightA * s->decorr[i].samplesA[0] + 512) >> 10);
440 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
442 if (type != AV_SAMPLE_FMT_S16P)
443 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
445 R2 = R + ((s->decorr[i].weightB * L2 + 512) >> 10);
446 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
448 s->decorr[i].samplesA[0] = R;
450 if (type != AV_SAMPLE_FMT_S16P)
451 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
453 R2 = R + ((s->decorr[i].weightB * s->decorr[i].samplesB[0] + 512) >> 10);
454 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
458 R2 = s->decorr[i].samplesA[0];
459 s->decorr[i].samplesA[0] = R;
462 if (type != AV_SAMPLE_FMT_S16P)
463 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
465 L2 = L + ((s->decorr[i].weightA * R2 + 512) >> 10);
466 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
468 s->decorr[i].samplesB[0] = L;
473 L += (R -= (L >> 1));
474 crc = (crc * 3 + L) * 3 + R;
476 if (type == AV_SAMPLE_FMT_FLTP) {
477 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
478 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
479 } else if (type == AV_SAMPLE_FMT_S32P) {
480 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
481 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
483 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
484 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
487 } while (!last && count < s->samples);
489 wv_reset_saved_context(s);
491 if (last && count < s->samples) {
492 int size = av_get_bytes_per_sample(type);
493 memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
494 memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
497 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
498 wv_check_crc(s, crc, crc_extra_bits))
499 return AVERROR_INVALIDDATA;
504 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
505 void *dst, const int type)
511 uint32_t crc = s->sc.crc;
512 uint32_t crc_extra_bits = s->extra_sc.crc;
513 int16_t *dst16 = dst;
514 int32_t *dst32 = dst;
517 s->one = s->zero = s->zeroes = 0;
519 T = wv_get_value(s, gb, 0, &last);
523 for (i = 0; i < s->terms; i++) {
524 t = s->decorr[i].value;
527 A = 2 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
529 A = (3 * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
530 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
533 A = s->decorr[i].samplesA[pos];
536 if (type != AV_SAMPLE_FMT_S16P)
537 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
539 S = T + ((s->decorr[i].weightA * A + 512) >> 10);
541 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
542 s->decorr[i].samplesA[j] = T = S;
547 if (type == AV_SAMPLE_FMT_FLTP) {
548 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
549 } else if (type == AV_SAMPLE_FMT_S32P) {
550 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
552 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
555 } while (!last && count < s->samples);
557 wv_reset_saved_context(s);
559 if (last && count < s->samples) {
560 int size = av_get_bytes_per_sample(type);
561 memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
564 if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
565 int ret = wv_check_crc(s, crc, crc_extra_bits);
566 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
573 static av_cold int wv_alloc_frame_context(WavpackContext *c)
575 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
578 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
579 if (!c->fdec[c->fdec_num])
582 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
583 wv_reset_saved_context(c->fdec[c->fdec_num - 1]);
588 static int init_thread_copy(AVCodecContext *avctx)
590 WavpackContext *s = avctx->priv_data;
595 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
597 WavpackContext *s = avctx->priv_data;
606 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
608 WavpackContext *s = avctx->priv_data;
611 for (i = 0; i < s->fdec_num; i++)
612 av_freep(&s->fdec[i]);
618 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
619 AVFrame *frame, const uint8_t *buf, int buf_size)
621 WavpackContext *wc = avctx->priv_data;
622 ThreadFrame tframe = { .f = frame };
623 WavpackFrameContext *s;
625 void *samples_l, *samples_r;
627 int got_terms = 0, got_weights = 0, got_samples = 0,
628 got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
629 int i, j, id, size, ssize, weights, t;
630 int bpp, chan = 0, chmask = 0, orig_bpp, sample_rate = 0;
633 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
634 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
635 return AVERROR_INVALIDDATA;
638 s = wc->fdec[block_no];
640 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
642 return AVERROR_INVALIDDATA;
645 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
646 memset(s->ch, 0, sizeof(s->ch));
648 s->and = s->or = s->shift = 0;
649 s->got_extra_bits = 0;
651 bytestream2_init(&gb, buf, buf_size);
653 s->samples = bytestream2_get_le32(&gb);
654 if (s->samples != wc->samples) {
655 av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
656 "a sequence: %d and %d\n", wc->samples, s->samples);
657 return AVERROR_INVALIDDATA;
659 s->frame_flags = bytestream2_get_le32(&gb);
660 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
661 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
662 multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
664 s->stereo = !(s->frame_flags & WV_MONO);
665 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
666 s->joint = s->frame_flags & WV_JOINT_STEREO;
667 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
668 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
669 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
670 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
671 s->hybrid_minclip = ((-1LL << (orig_bpp - 1)));
672 s->CRC = bytestream2_get_le32(&gb);
674 // parse metadata blocks
675 while (bytestream2_get_bytes_left(&gb)) {
676 id = bytestream2_get_byte(&gb);
677 size = bytestream2_get_byte(&gb);
678 if (id & WP_IDF_LONG) {
679 size |= (bytestream2_get_byte(&gb)) << 8;
680 size |= (bytestream2_get_byte(&gb)) << 16;
682 size <<= 1; // size is specified in words
687 av_log(avctx, AV_LOG_ERROR,
688 "Got incorrect block %02X with size %i\n", id, size);
691 if (bytestream2_get_bytes_left(&gb) < ssize) {
692 av_log(avctx, AV_LOG_ERROR,
693 "Block size %i is out of bounds\n", size);
696 switch (id & WP_IDF_MASK) {
698 if (size > MAX_TERMS) {
699 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
701 bytestream2_skip(&gb, ssize);
705 for (i = 0; i < s->terms; i++) {
706 uint8_t val = bytestream2_get_byte(&gb);
707 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
708 s->decorr[s->terms - i - 1].delta = val >> 5;
712 case WP_ID_DECWEIGHTS:
714 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
717 weights = size >> s->stereo_in;
718 if (weights > MAX_TERMS || weights > s->terms) {
719 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
720 bytestream2_skip(&gb, ssize);
723 for (i = 0; i < weights; i++) {
724 t = (int8_t)bytestream2_get_byte(&gb);
725 s->decorr[s->terms - i - 1].weightA = t << 3;
726 if (s->decorr[s->terms - i - 1].weightA > 0)
727 s->decorr[s->terms - i - 1].weightA +=
728 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
730 t = (int8_t)bytestream2_get_byte(&gb);
731 s->decorr[s->terms - i - 1].weightB = t << 3;
732 if (s->decorr[s->terms - i - 1].weightB > 0)
733 s->decorr[s->terms - i - 1].weightB +=
734 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
739 case WP_ID_DECSAMPLES:
741 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
745 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
746 if (s->decorr[i].value > 8) {
747 s->decorr[i].samplesA[0] =
748 wp_exp2(bytestream2_get_le16(&gb));
749 s->decorr[i].samplesA[1] =
750 wp_exp2(bytestream2_get_le16(&gb));
753 s->decorr[i].samplesB[0] =
754 wp_exp2(bytestream2_get_le16(&gb));
755 s->decorr[i].samplesB[1] =
756 wp_exp2(bytestream2_get_le16(&gb));
760 } else if (s->decorr[i].value < 0) {
761 s->decorr[i].samplesA[0] =
762 wp_exp2(bytestream2_get_le16(&gb));
763 s->decorr[i].samplesB[0] =
764 wp_exp2(bytestream2_get_le16(&gb));
767 for (j = 0; j < s->decorr[i].value; j++) {
768 s->decorr[i].samplesA[j] =
769 wp_exp2(bytestream2_get_le16(&gb));
771 s->decorr[i].samplesB[j] =
772 wp_exp2(bytestream2_get_le16(&gb));
775 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
781 if (size != 6 * (s->stereo_in + 1)) {
782 av_log(avctx, AV_LOG_ERROR,
783 "Entropy vars size should be %i, got %i.\n",
784 6 * (s->stereo_in + 1), size);
785 bytestream2_skip(&gb, ssize);
788 for (j = 0; j <= s->stereo_in; j++)
789 for (i = 0; i < 3; i++) {
790 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
795 if (s->hybrid_bitrate) {
796 for (i = 0; i <= s->stereo_in; i++) {
797 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
801 for (i = 0; i < (s->stereo_in + 1); i++) {
802 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
806 for (i = 0; i < (s->stereo_in + 1); i++) {
807 s->ch[i].bitrate_delta =
808 wp_exp2((int16_t)bytestream2_get_le16(&gb));
811 for (i = 0; i < (s->stereo_in + 1); i++)
812 s->ch[i].bitrate_delta = 0;
816 case WP_ID_INT32INFO: {
819 av_log(avctx, AV_LOG_ERROR,
820 "Invalid INT32INFO, size = %i\n",
822 bytestream2_skip(&gb, ssize - 4);
825 bytestream2_get_buffer(&gb, val, 4);
827 s->extra_bits = val[0];
837 /* original WavPack decoder forces 32-bit lossy sound to be treated
838 * as 24-bit one in order to have proper clipping */
839 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
842 s->hybrid_maxclip >>= 8;
843 s->hybrid_minclip >>= 8;
847 case WP_ID_FLOATINFO:
849 av_log(avctx, AV_LOG_ERROR,
850 "Invalid FLOATINFO, size = %i\n", size);
851 bytestream2_skip(&gb, ssize);
854 s->float_flag = bytestream2_get_byte(&gb);
855 s->float_shift = bytestream2_get_byte(&gb);
856 s->float_max_exp = bytestream2_get_byte(&gb);
858 bytestream2_skip(&gb, 1);
861 s->sc.offset = bytestream2_tell(&gb);
862 s->sc.size = size * 8;
863 if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
865 s->data_size = size * 8;
866 bytestream2_skip(&gb, size);
869 case WP_ID_EXTRABITS:
871 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
873 bytestream2_skip(&gb, size);
876 s->extra_sc.offset = bytestream2_tell(&gb);
877 s->extra_sc.size = size * 8;
878 if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
880 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
881 bytestream2_skip(&gb, size);
882 s->got_extra_bits = 1;
886 av_log(avctx, AV_LOG_ERROR,
887 "Insufficient channel information\n");
888 return AVERROR_INVALIDDATA;
890 chan = bytestream2_get_byte(&gb);
893 chmask = bytestream2_get_byte(&gb);
896 chmask = bytestream2_get_le16(&gb);
899 chmask = bytestream2_get_le24(&gb);
902 chmask = bytestream2_get_le32(&gb);
905 bytestream2_skip(&gb, 1);
906 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
907 chmask = bytestream2_get_le16(&gb);
910 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
912 chan = avctx->channels;
913 chmask = avctx->channel_layout;
916 case WP_ID_SAMPLE_RATE:
918 av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
919 return AVERROR_INVALIDDATA;
921 sample_rate = bytestream2_get_le24(&gb);
924 bytestream2_skip(&gb, size);
927 bytestream2_skip(&gb, 1);
931 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
932 return AVERROR_INVALIDDATA;
935 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
936 return AVERROR_INVALIDDATA;
939 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
940 return AVERROR_INVALIDDATA;
943 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
944 return AVERROR_INVALIDDATA;
946 if (s->hybrid && !got_hybrid) {
947 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
948 return AVERROR_INVALIDDATA;
951 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
952 return AVERROR_INVALIDDATA;
954 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
955 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
956 return AVERROR_INVALIDDATA;
958 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
959 const int size = get_bits_left(&s->gb_extra_bits);
960 const int wanted = s->samples * s->extra_bits << s->stereo_in;
962 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
963 s->got_extra_bits = 0;
967 if (!wc->ch_offset) {
968 int sr = (s->frame_flags >> 23) & 0xf;
971 av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
972 return AVERROR_INVALIDDATA;
974 avctx->sample_rate = sample_rate;
976 avctx->sample_rate = wv_rates[sr];
980 avctx->channels = chan;
982 avctx->channel_layout = chmask;
984 avctx->channels = s->stereo ? 2 : 1;
985 avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :
989 /* get output buffer */
990 frame->nb_samples = s->samples + 1;
991 if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
993 frame->nb_samples = s->samples;
996 if (wc->ch_offset + s->stereo >= avctx->channels) {
997 av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
998 return (avctx->err_recognition & AV_EF_EXPLODE) ? AVERROR_INVALIDDATA : 0;
1001 samples_l = frame->extended_data[wc->ch_offset];
1003 samples_r = frame->extended_data[wc->ch_offset + 1];
1005 wc->ch_offset += 1 + s->stereo;
1008 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1012 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1017 memcpy(samples_r, samples_l, bpp * s->samples);
1023 static void wavpack_decode_flush(AVCodecContext *avctx)
1025 WavpackContext *s = avctx->priv_data;
1028 for (i = 0; i < s->fdec_num; i++)
1029 wv_reset_saved_context(s->fdec[i]);
1032 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
1033 int *got_frame_ptr, AVPacket *avpkt)
1035 WavpackContext *s = avctx->priv_data;
1036 const uint8_t *buf = avpkt->data;
1037 int buf_size = avpkt->size;
1038 AVFrame *frame = data;
1039 int frame_size, ret, frame_flags;
1041 if (avpkt->size <= WV_HEADER_SIZE)
1042 return AVERROR_INVALIDDATA;
1047 /* determine number of samples */
1048 s->samples = AV_RL32(buf + 20);
1049 frame_flags = AV_RL32(buf + 24);
1050 if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1051 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1053 return AVERROR_INVALIDDATA;
1056 if (frame_flags & 0x80) {
1057 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1058 } else if ((frame_flags & 0x03) <= 1) {
1059 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1061 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1062 avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
1065 while (buf_size > 0) {
1066 if (buf_size <= WV_HEADER_SIZE)
1068 frame_size = AV_RL32(buf + 4) - 12;
1071 if (frame_size <= 0 || frame_size > buf_size) {
1072 av_log(avctx, AV_LOG_ERROR,
1073 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1074 s->block, frame_size, buf_size);
1075 wavpack_decode_flush(avctx);
1076 return AVERROR_INVALIDDATA;
1078 if ((ret = wavpack_decode_block(avctx, s->block,
1079 frame, buf, frame_size)) < 0) {
1080 wavpack_decode_flush(avctx);
1085 buf_size -= frame_size;
1088 if (s->ch_offset != avctx->channels) {
1089 av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1090 return AVERROR_INVALIDDATA;
1098 AVCodec ff_wavpack_decoder = {
1100 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1101 .type = AVMEDIA_TYPE_AUDIO,
1102 .id = AV_CODEC_ID_WAVPACK,
1103 .priv_data_size = sizeof(WavpackContext),
1104 .init = wavpack_decode_init,
1105 .close = wavpack_decode_end,
1106 .decode = wavpack_decode_frame,
1107 .flush = wavpack_decode_flush,
1108 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
1109 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,