2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
4 * Copyright (c) 2020 David Bryant
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/channel_layout.h"
25 #define BITSTREAM_READER_LE
27 #include "bytestream.h"
37 * WavPack lossless audio decoder
40 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000))
43 #define PTABLE_BINS (1<<PTABLE_BITS)
44 #define PTABLE_MASK (PTABLE_BINS-1)
47 #define DOWN 0x00010000
51 #define VALUE_ONE (1 << PRECISION)
52 #define PRECISION_USE 12
56 #define MAX_HISTORY_BITS 5
57 #define MAX_HISTORY_BINS (1 << MAX_HISTORY_BITS)
58 #define MAX_BIN_BYTES 1280 // for value_lookup, per bin (2k - 512 - 256)
61 MODULATION_PCM, // pulse code modulation
62 MODULATION_DSD // pulse density modulation (aka DSD)
65 typedef struct WavpackFrameContext {
66 AVCodecContext *avctx;
68 int stereo, stereo_in;
73 uint32_t crc_extra_bits;
74 GetBitContext gb_extra_bits;
77 Decorr decorr[MAX_TERMS];
78 int zero, one, zeroes;
82 int hybrid, hybrid_bitrate;
83 int hybrid_maxclip, hybrid_minclip;
90 int ptable [PTABLE_BINS];
91 uint8_t value_lookup_buffer[MAX_HISTORY_BINS*MAX_BIN_BYTES];
92 uint16_t summed_probabilities[MAX_HISTORY_BINS][256];
93 uint8_t probabilities[MAX_HISTORY_BINS][256];
94 uint8_t *value_lookup[MAX_HISTORY_BINS];
95 } WavpackFrameContext;
97 #define WV_MAX_FRAME_DECODERS 14
99 typedef struct WavpackContext {
100 AVCodecContext *avctx;
102 WavpackFrameContext *fdec[WV_MAX_FRAME_DECODERS];
110 ThreadFrame curr_frame, prev_frame;
111 Modulation modulation;
115 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
117 static av_always_inline unsigned get_tail(GetBitContext *gb, int k)
124 e = (1 << (p + 1)) - k - 1;
125 res = get_bitsz(gb, p);
127 res = (res << 1) - e + get_bits1(gb);
131 static int update_error_limit(WavpackFrameContext *ctx)
135 for (i = 0; i <= ctx->stereo_in; i++) {
136 if (ctx->ch[i].bitrate_acc > UINT_MAX - ctx->ch[i].bitrate_delta)
137 return AVERROR_INVALIDDATA;
138 ctx->ch[i].bitrate_acc += ctx->ch[i].bitrate_delta;
139 br[i] = ctx->ch[i].bitrate_acc >> 16;
140 sl[i] = LEVEL_DECAY(ctx->ch[i].slow_level);
142 if (ctx->stereo_in && ctx->hybrid_bitrate) {
143 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
144 if (balance > br[0]) {
147 } else if (-balance > br[0]) {
151 br[1] = br[0] + balance;
152 br[0] = br[0] - balance;
155 for (i = 0; i <= ctx->stereo_in; i++) {
156 if (ctx->hybrid_bitrate) {
157 if (sl[i] - br[i] > -0x100)
158 ctx->ch[i].error_limit = wp_exp2(sl[i] - br[i] + 0x100);
160 ctx->ch[i].error_limit = 0;
162 ctx->ch[i].error_limit = wp_exp2(br[i]);
169 static int wv_get_value(WavpackFrameContext *ctx, GetBitContext *gb,
170 int channel, int *last)
173 int sign, base, add, ret;
174 WvChannel *c = &ctx->ch[channel];
178 if ((ctx->ch[0].median[0] < 2U) && (ctx->ch[1].median[0] < 2U) &&
179 !ctx->zero && !ctx->one) {
183 c->slow_level -= LEVEL_DECAY(c->slow_level);
187 t = get_unary_0_33(gb);
189 if (t >= 32 || get_bits_left(gb) < t - 1)
191 t = get_bits_long(gb, t - 1) | (1 << (t - 1));
193 if (get_bits_left(gb) < 0)
198 memset(ctx->ch[0].median, 0, sizeof(ctx->ch[0].median));
199 memset(ctx->ch[1].median, 0, sizeof(ctx->ch[1].median));
200 c->slow_level -= LEVEL_DECAY(c->slow_level);
210 t = get_unary_0_33(gb);
211 if (get_bits_left(gb) < 0)
214 t2 = get_unary_0_33(gb);
216 if (get_bits_left(gb) < 0)
220 if (t2 >= 32 || get_bits_left(gb) < t2 - 1)
222 t += get_bits_long(gb, t2 - 1) | (1 << (t2 - 1));
233 ctx->zero = !ctx->one;
236 if (ctx->hybrid && !channel) {
237 if (update_error_limit(ctx) < 0)
243 add = GET_MED(0) - 1;
247 add = GET_MED(1) - 1;
251 base = GET_MED(0) + GET_MED(1);
252 add = GET_MED(2) - 1;
257 base = GET_MED(0) + GET_MED(1) + GET_MED(2) * (t - 2U);
258 add = GET_MED(2) - 1;
263 if (!c->error_limit) {
264 if (add >= 0x2000000U) {
265 av_log(ctx->avctx, AV_LOG_ERROR, "k %d is too large\n", add);
268 ret = base + get_tail(gb, add);
269 if (get_bits_left(gb) <= 0)
272 int mid = (base * 2U + add + 1) >> 1;
273 while (add > c->error_limit) {
274 if (get_bits_left(gb) <= 0)
277 add -= (mid - (unsigned)base);
280 add = mid - (unsigned)base - 1;
281 mid = (base * 2U + add + 1) >> 1;
285 sign = get_bits1(gb);
286 if (ctx->hybrid_bitrate)
287 c->slow_level += wp_log2(ret) - LEVEL_DECAY(c->slow_level);
288 return sign ? ~ret : ret;
291 ret = get_bits_left(gb);
293 av_log(ctx->avctx, AV_LOG_ERROR, "Too few bits (%d) left\n", ret);
299 static inline int wv_get_value_integer(WavpackFrameContext *s, uint32_t *crc,
305 S *= 1 << s->extra_bits;
307 if (s->got_extra_bits &&
308 get_bits_left(&s->gb_extra_bits) >= s->extra_bits) {
309 S |= get_bits_long(&s->gb_extra_bits, s->extra_bits);
310 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
314 bit = (S & s->and) | s->or;
315 bit = ((S + bit) << s->shift) - bit;
318 bit = av_clip(bit, s->hybrid_minclip, s->hybrid_maxclip);
320 return bit << s->post_shift;
323 static float wv_get_value_float(WavpackFrameContext *s, uint32_t *crc, int S)
331 int exp = s->float_max_exp;
333 if (s->got_extra_bits) {
334 const int max_bits = 1 + 23 + 8 + 1;
335 const int left_bits = get_bits_left(&s->gb_extra_bits);
337 if (left_bits + 8 * AV_INPUT_BUFFER_PADDING_SIZE < max_bits)
342 S *= 1U << s->float_shift;
346 if (S >= 0x1000000U) {
347 if (s->got_extra_bits && get_bits1(&s->gb_extra_bits))
348 S = get_bits(&s->gb_extra_bits, 23);
353 int shift = 23 - av_log2(S);
354 exp = s->float_max_exp;
361 if ((s->float_flag & WV_FLT_SHIFT_ONES) ||
362 (s->got_extra_bits &&
363 (s->float_flag & WV_FLT_SHIFT_SAME) &&
364 get_bits1(&s->gb_extra_bits))) {
365 S |= (1 << shift) - 1;
366 } else if (s->got_extra_bits &&
367 (s->float_flag & WV_FLT_SHIFT_SENT)) {
368 S |= get_bits(&s->gb_extra_bits, shift);
372 exp = s->float_max_exp;
378 if (s->got_extra_bits && (s->float_flag & WV_FLT_ZERO_SENT)) {
379 if (get_bits1(&s->gb_extra_bits)) {
380 S = get_bits(&s->gb_extra_bits, 23);
381 if (s->float_max_exp >= 25)
382 exp = get_bits(&s->gb_extra_bits, 8);
383 sign = get_bits1(&s->gb_extra_bits);
385 if (s->float_flag & WV_FLT_ZERO_SIGN)
386 sign = get_bits1(&s->gb_extra_bits);
391 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
393 value.u = (sign << 31) | (exp << 23) | S;
397 static inline int wv_check_crc(WavpackFrameContext *s, uint32_t crc,
398 uint32_t crc_extra_bits)
401 av_log(s->avctx, AV_LOG_ERROR, "CRC error\n");
402 return AVERROR_INVALIDDATA;
404 if (s->got_extra_bits && crc_extra_bits != s->crc_extra_bits) {
405 av_log(s->avctx, AV_LOG_ERROR, "Extra bits CRC error\n");
406 return AVERROR_INVALIDDATA;
412 static void init_ptable(int *table, int rate_i, int rate_s)
414 int value = 0x808000, rate = rate_i << 8;
416 for (int c = (rate + 128) >> 8; c--;)
417 value += (DOWN - value) >> DECAY;
419 for (int i = 0; i < PTABLE_BINS/2; i++) {
421 table[PTABLE_BINS-1-i] = 0x100ffff - value;
423 if (value > 0x010000) {
424 rate += (rate * rate_s + 128) >> 8;
426 for (int c = (rate + 64) >> 7; c--;)
427 value += (DOWN - value) >> DECAY;
433 int32_t value, fltr0, fltr1, fltr2, fltr3, fltr4, fltr5, fltr6, factor;
437 static int wv_unpack_dsd_high(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
439 uint32_t checksum = 0xFFFFFFFF;
440 uint8_t *dst_l = dst_left, *dst_r = dst_right;
441 int total_samples = s->samples, stereo = dst_r ? 1 : 0;
442 DSDfilters filters[2], *sp = filters;
444 uint32_t low, high, value;
446 if (bytestream2_get_bytes_left(&s->gbyte) < (stereo ? 20 : 13))
447 return AVERROR_INVALIDDATA;
449 rate_i = bytestream2_get_byte(&s->gbyte);
450 rate_s = bytestream2_get_byte(&s->gbyte);
452 if (rate_s != RATE_S)
453 return AVERROR_INVALIDDATA;
455 init_ptable(s->ptable, rate_i, rate_s);
457 for (int channel = 0; channel < stereo + 1; channel++) {
458 DSDfilters *sp = filters + channel;
460 sp->fltr1 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
461 sp->fltr2 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
462 sp->fltr3 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
463 sp->fltr4 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
464 sp->fltr5 = bytestream2_get_byte(&s->gbyte) << (PRECISION - 8);
466 sp->factor = bytestream2_get_byte(&s->gbyte) & 0xff;
467 sp->factor |= (bytestream2_get_byte(&s->gbyte) << 8) & 0xff00;
468 sp->factor = (int32_t)((uint32_t)sp->factor << 16) >> 16;
471 value = bytestream2_get_be32(&s->gbyte);
475 while (total_samples--) {
478 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
481 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
484 int32_t *pp = s->ptable + ((sp[0].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
485 uint32_t split = low + ((high - low) >> 8) * (*pp >> 16);
487 if (value <= split) {
489 *pp += (UP - *pp) >> DECAY;
493 *pp += (DOWN - *pp) >> DECAY;
497 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
498 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
499 high = (high << 8) | 0xff;
503 sp[0].value += sp[0].fltr6 * 8;
504 sp[0].byte = (sp[0].byte << 1) | (sp[0].fltr0 & 1);
505 sp[0].factor += (((sp[0].value ^ sp[0].fltr0) >> 31) | 1) &
506 ((sp[0].value ^ (sp[0].value - (sp[0].fltr6 * 16))) >> 31);
507 sp[0].fltr1 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr1) >> 6;
508 sp[0].fltr2 += ((sp[0].fltr0 & VALUE_ONE) - sp[0].fltr2) >> 4;
509 sp[0].fltr3 += (sp[0].fltr2 - sp[0].fltr3) >> 4;
510 sp[0].fltr4 += (sp[0].fltr3 - sp[0].fltr4) >> 4;
511 sp[0].value = (sp[0].fltr4 - sp[0].fltr5) >> 4;
512 sp[0].fltr5 += sp[0].value;
513 sp[0].fltr6 += (sp[0].value - sp[0].fltr6) >> 3;
514 sp[0].value = sp[0].fltr1 - sp[0].fltr5 + ((sp[0].fltr6 * sp[0].factor) >> 2);
519 pp = s->ptable + ((sp[1].value >> (PRECISION - PRECISION_USE)) & PTABLE_MASK);
520 split = low + ((high - low) >> 8) * (*pp >> 16);
522 if (value <= split) {
524 *pp += (UP - *pp) >> DECAY;
528 *pp += (DOWN - *pp) >> DECAY;
532 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
533 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
534 high = (high << 8) | 0xff;
538 sp[1].value += sp[1].fltr6 * 8;
539 sp[1].byte = (sp[1].byte << 1) | (sp[1].fltr0 & 1);
540 sp[1].factor += (((sp[1].value ^ sp[1].fltr0) >> 31) | 1) &
541 ((sp[1].value ^ (sp[1].value - (sp[1].fltr6 * 16))) >> 31);
542 sp[1].fltr1 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr1) >> 6;
543 sp[1].fltr2 += ((sp[1].fltr0 & VALUE_ONE) - sp[1].fltr2) >> 4;
544 sp[1].fltr3 += (sp[1].fltr2 - sp[1].fltr3) >> 4;
545 sp[1].fltr4 += (sp[1].fltr3 - sp[1].fltr4) >> 4;
546 sp[1].value = (sp[1].fltr4 - sp[1].fltr5) >> 4;
547 sp[1].fltr5 += sp[1].value;
548 sp[1].fltr6 += (sp[1].value - sp[1].fltr6) >> 3;
549 sp[1].value = sp[1].fltr1 - sp[1].fltr5 + ((sp[1].fltr6 * sp[1].factor) >> 2);
552 checksum += (checksum << 1) + (*dst_l = sp[0].byte & 0xff);
553 sp[0].factor -= (sp[0].factor + 512) >> 10;
557 checksum += (checksum << 1) + (*dst_r = filters[1].byte & 0xff);
558 filters[1].factor -= (filters[1].factor + 512) >> 10;
563 if (wv_check_crc(s, checksum, 0)) {
564 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
565 return AVERROR_INVALIDDATA;
567 memset(dst_left, 0x69, s->samples * 4);
570 memset(dst_right, 0x69, s->samples * 4);
576 static int wv_unpack_dsd_fast(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
578 uint8_t *dst_l = dst_left, *dst_r = dst_right;
579 uint8_t history_bits, max_probability;
580 int total_summed_probabilities = 0;
581 int total_samples = s->samples;
582 uint8_t *vlb = s->value_lookup_buffer;
583 int history_bins, p0, p1, chan;
584 uint32_t checksum = 0xFFFFFFFF;
585 uint32_t low, high, value;
587 if (!bytestream2_get_bytes_left(&s->gbyte))
588 return AVERROR_INVALIDDATA;
590 history_bits = bytestream2_get_byte(&s->gbyte);
592 if (!bytestream2_get_bytes_left(&s->gbyte) || history_bits > MAX_HISTORY_BITS)
593 return AVERROR_INVALIDDATA;
595 history_bins = 1 << history_bits;
596 max_probability = bytestream2_get_byte(&s->gbyte);
598 if (max_probability < 0xff) {
599 uint8_t *outptr = (uint8_t *)s->probabilities;
600 uint8_t *outend = outptr + sizeof(*s->probabilities) * history_bins;
602 while (outptr < outend && bytestream2_get_bytes_left(&s->gbyte)) {
603 int code = bytestream2_get_byte(&s->gbyte);
605 if (code > max_probability) {
606 int zcount = code - max_probability;
608 while (outptr < outend && zcount--)
618 if (outptr < outend ||
619 (bytestream2_get_bytes_left(&s->gbyte) && bytestream2_get_byte(&s->gbyte)))
620 return AVERROR_INVALIDDATA;
621 } else if (bytestream2_get_bytes_left(&s->gbyte) > (int)sizeof(*s->probabilities) * history_bins) {
622 bytestream2_get_buffer(&s->gbyte, (uint8_t *)s->probabilities,
623 sizeof(*s->probabilities) * history_bins);
625 return AVERROR_INVALIDDATA;
628 for (p0 = 0; p0 < history_bins; p0++) {
629 int32_t sum_values = 0;
631 for (int i = 0; i < 256; i++)
632 s->summed_probabilities[p0][i] = sum_values += s->probabilities[p0][i];
635 total_summed_probabilities += sum_values;
637 if (total_summed_probabilities > history_bins * MAX_BIN_BYTES)
638 return AVERROR_INVALIDDATA;
640 s->value_lookup[p0] = vlb;
642 for (int i = 0; i < 256; i++) {
643 int c = s->probabilities[p0][i];
651 if (bytestream2_get_bytes_left(&s->gbyte) < 4)
652 return AVERROR_INVALIDDATA;
655 low = 0; high = 0xffffffff;
656 value = bytestream2_get_be32(&s->gbyte);
661 while (total_samples--) {
662 unsigned int mult, index, code;
664 if (!s->summed_probabilities[p0][255])
665 return AVERROR_INVALIDDATA;
667 mult = (high - low) / s->summed_probabilities[p0][255];
670 if (bytestream2_get_bytes_left(&s->gbyte) >= 4)
671 value = bytestream2_get_be32(&s->gbyte);
675 mult = high / s->summed_probabilities[p0][255];
678 return AVERROR_INVALIDDATA;
681 index = (value - low) / mult;
683 if (index >= s->summed_probabilities[p0][255])
684 return AVERROR_INVALIDDATA;
687 if ((*dst_l = code = s->value_lookup[p0][index]))
688 low += s->summed_probabilities[p0][code-1] * mult;
692 if ((code = s->value_lookup[p0][index]))
693 low += s->summed_probabilities[p0][code-1] * mult;
707 high = low + s->probabilities[p0][code] * mult - 1;
708 checksum += (checksum << 1) + code;
711 p0 = code & (history_bins-1);
714 p1 = code & (history_bins-1);
717 while (DSD_BYTE_READY(high, low) && bytestream2_get_bytes_left(&s->gbyte)) {
718 value = (value << 8) | bytestream2_get_byte(&s->gbyte);
719 high = (high << 8) | 0xff;
724 if (wv_check_crc(s, checksum, 0)) {
725 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
726 return AVERROR_INVALIDDATA;
728 memset(dst_left, 0x69, s->samples * 4);
731 memset(dst_right, 0x69, s->samples * 4);
737 static int wv_unpack_dsd_copy(WavpackFrameContext *s, uint8_t *dst_left, uint8_t *dst_right)
739 uint8_t *dst_l = dst_left, *dst_r = dst_right;
740 int total_samples = s->samples;
741 uint32_t checksum = 0xFFFFFFFF;
743 if (bytestream2_get_bytes_left(&s->gbyte) != total_samples * (dst_r ? 2 : 1))
744 return AVERROR_INVALIDDATA;
746 while (total_samples--) {
747 checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&s->gbyte));
751 checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&s->gbyte));
756 if (wv_check_crc(s, checksum, 0)) {
757 if (s->avctx->err_recognition & AV_EF_CRCCHECK)
758 return AVERROR_INVALIDDATA;
760 memset(dst_left, 0x69, s->samples * 4);
763 memset(dst_right, 0x69, s->samples * 4);
769 static inline int wv_unpack_stereo(WavpackFrameContext *s, GetBitContext *gb,
770 void *dst_l, void *dst_r, const int type)
774 int A, B, L, L2, R, R2;
776 uint32_t crc = 0xFFFFFFFF;
777 uint32_t crc_extra_bits = 0xFFFFFFFF;
778 int16_t *dst16_l = dst_l;
779 int16_t *dst16_r = dst_r;
780 int32_t *dst32_l = dst_l;
781 int32_t *dst32_r = dst_r;
782 float *dstfl_l = dst_l;
783 float *dstfl_r = dst_r;
785 s->one = s->zero = s->zeroes = 0;
787 L = wv_get_value(s, gb, 0, &last);
790 R = wv_get_value(s, gb, 1, &last);
793 for (i = 0; i < s->terms; i++) {
794 t = s->decorr[i].value;
798 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
799 B = 2U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1];
801 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
802 B = (int)(3U * s->decorr[i].samplesB[0] - s->decorr[i].samplesB[1]) >> 1;
804 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
805 s->decorr[i].samplesB[1] = s->decorr[i].samplesB[0];
808 A = s->decorr[i].samplesA[pos];
809 B = s->decorr[i].samplesB[pos];
812 if (type != AV_SAMPLE_FMT_S16P) {
813 L2 = L + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
814 R2 = R + ((s->decorr[i].weightB * (int64_t)B + 512) >> 10);
816 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
817 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)B + 512) >> 10);
820 s->decorr[i].weightA -= ((((L ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
822 s->decorr[i].weightB -= ((((R ^ B) >> 30) & 2) - 1) * s->decorr[i].delta;
823 s->decorr[i].samplesA[j] = L = L2;
824 s->decorr[i].samplesB[j] = R = R2;
825 } else if (t == -1) {
826 if (type != AV_SAMPLE_FMT_S16P)
827 L2 = L + ((s->decorr[i].weightA * (int64_t)s->decorr[i].samplesA[0] + 512) >> 10);
829 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)s->decorr[i].samplesA[0] + 512) >> 10);
830 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, s->decorr[i].samplesA[0], L);
832 if (type != AV_SAMPLE_FMT_S16P)
833 R2 = R + ((s->decorr[i].weightB * (int64_t)L2 + 512) >> 10);
835 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)L2 + 512) >> 10);
836 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, L2, R);
838 s->decorr[i].samplesA[0] = R;
840 if (type != AV_SAMPLE_FMT_S16P)
841 R2 = R + ((s->decorr[i].weightB * (int64_t)s->decorr[i].samplesB[0] + 512) >> 10);
843 R2 = R + (unsigned)((int)(s->decorr[i].weightB * (unsigned)s->decorr[i].samplesB[0] + 512) >> 10);
844 UPDATE_WEIGHT_CLIP(s->decorr[i].weightB, s->decorr[i].delta, s->decorr[i].samplesB[0], R);
848 R2 = s->decorr[i].samplesA[0];
849 s->decorr[i].samplesA[0] = R;
852 if (type != AV_SAMPLE_FMT_S16P)
853 L2 = L + ((s->decorr[i].weightA * (int64_t)R2 + 512) >> 10);
855 L2 = L + (unsigned)((int)(s->decorr[i].weightA * (unsigned)R2 + 512) >> 10);
856 UPDATE_WEIGHT_CLIP(s->decorr[i].weightA, s->decorr[i].delta, R2, L);
858 s->decorr[i].samplesB[0] = L;
862 if (type == AV_SAMPLE_FMT_S16P) {
863 if (FFABS((int64_t)L) + FFABS((int64_t)R) > (1<<19)) {
864 av_log(s->avctx, AV_LOG_ERROR, "sample %d %d too large\n", L, R);
865 return AVERROR_INVALIDDATA;
871 L += (unsigned)(R -= (unsigned)(L >> 1));
872 crc = (crc * 3 + L) * 3 + R;
874 if (type == AV_SAMPLE_FMT_FLTP) {
875 *dstfl_l++ = wv_get_value_float(s, &crc_extra_bits, L);
876 *dstfl_r++ = wv_get_value_float(s, &crc_extra_bits, R);
877 } else if (type == AV_SAMPLE_FMT_S32P) {
878 *dst32_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
879 *dst32_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
881 *dst16_l++ = wv_get_value_integer(s, &crc_extra_bits, L);
882 *dst16_r++ = wv_get_value_integer(s, &crc_extra_bits, R);
885 } while (!last && count < s->samples);
887 if (last && count < s->samples) {
888 int size = av_get_bytes_per_sample(type);
889 memset((uint8_t*)dst_l + count*size, 0, (s->samples-count)*size);
890 memset((uint8_t*)dst_r + count*size, 0, (s->samples-count)*size);
893 if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
894 wv_check_crc(s, crc, crc_extra_bits))
895 return AVERROR_INVALIDDATA;
900 static inline int wv_unpack_mono(WavpackFrameContext *s, GetBitContext *gb,
901 void *dst, const int type)
907 uint32_t crc = 0xFFFFFFFF;
908 uint32_t crc_extra_bits = 0xFFFFFFFF;
909 int16_t *dst16 = dst;
910 int32_t *dst32 = dst;
913 s->one = s->zero = s->zeroes = 0;
915 T = wv_get_value(s, gb, 0, &last);
919 for (i = 0; i < s->terms; i++) {
920 t = s->decorr[i].value;
923 A = 2U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1];
925 A = (int)(3U * s->decorr[i].samplesA[0] - s->decorr[i].samplesA[1]) >> 1;
926 s->decorr[i].samplesA[1] = s->decorr[i].samplesA[0];
929 A = s->decorr[i].samplesA[pos];
932 if (type != AV_SAMPLE_FMT_S16P)
933 S = T + ((s->decorr[i].weightA * (int64_t)A + 512) >> 10);
935 S = T + (unsigned)((int)(s->decorr[i].weightA * (unsigned)A + 512) >> 10);
937 s->decorr[i].weightA -= ((((T ^ A) >> 30) & 2) - 1) * s->decorr[i].delta;
938 s->decorr[i].samplesA[j] = T = S;
943 if (type == AV_SAMPLE_FMT_FLTP) {
944 *dstfl++ = wv_get_value_float(s, &crc_extra_bits, S);
945 } else if (type == AV_SAMPLE_FMT_S32P) {
946 *dst32++ = wv_get_value_integer(s, &crc_extra_bits, S);
948 *dst16++ = wv_get_value_integer(s, &crc_extra_bits, S);
951 } while (!last && count < s->samples);
953 if (last && count < s->samples) {
954 int size = av_get_bytes_per_sample(type);
955 memset((uint8_t*)dst + count*size, 0, (s->samples-count)*size);
958 if (s->avctx->err_recognition & AV_EF_CRCCHECK) {
959 int ret = wv_check_crc(s, crc, crc_extra_bits);
960 if (ret < 0 && s->avctx->err_recognition & AV_EF_EXPLODE)
967 static av_cold int wv_alloc_frame_context(WavpackContext *c)
969 if (c->fdec_num == WV_MAX_FRAME_DECODERS)
972 c->fdec[c->fdec_num] = av_mallocz(sizeof(**c->fdec));
973 if (!c->fdec[c->fdec_num])
976 c->fdec[c->fdec_num - 1]->avctx = c->avctx;
982 static int init_thread_copy(AVCodecContext *avctx)
984 WavpackContext *s = avctx->priv_data;
987 s->curr_frame.f = av_frame_alloc();
988 s->prev_frame.f = av_frame_alloc();
993 static int update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
995 WavpackContext *fsrc = src->priv_data;
996 WavpackContext *fdst = dst->priv_data;
1002 ff_thread_release_buffer(dst, &fdst->curr_frame);
1003 if (fsrc->curr_frame.f->data[0]) {
1004 if ((ret = ff_thread_ref_frame(&fdst->curr_frame, &fsrc->curr_frame)) < 0)
1012 static av_cold int wavpack_decode_init(AVCodecContext *avctx)
1014 WavpackContext *s = avctx->priv_data;
1020 avctx->internal->allocate_progress = 1;
1022 s->curr_frame.f = av_frame_alloc();
1023 s->prev_frame.f = av_frame_alloc();
1025 // the DSD to PCM context is shared (and used serially) between all decoding threads
1026 s->dsdctx = av_calloc(avctx->channels, sizeof(DSDContext));
1028 if (!s->curr_frame.f || !s->prev_frame.f || !s->dsdctx)
1029 return AVERROR(ENOMEM);
1031 for (int i = 0; i < avctx->channels; i++)
1032 memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
1039 static av_cold int wavpack_decode_end(AVCodecContext *avctx)
1041 WavpackContext *s = avctx->priv_data;
1043 for (int i = 0; i < s->fdec_num; i++)
1044 av_freep(&s->fdec[i]);
1047 ff_thread_release_buffer(avctx, &s->curr_frame);
1048 av_frame_free(&s->curr_frame.f);
1050 ff_thread_release_buffer(avctx, &s->prev_frame);
1051 av_frame_free(&s->prev_frame.f);
1053 if (!avctx->internal->is_copy)
1054 av_freep(&s->dsdctx);
1059 static int wavpack_decode_block(AVCodecContext *avctx, int block_no,
1060 const uint8_t *buf, int buf_size)
1062 WavpackContext *wc = avctx->priv_data;
1063 WavpackFrameContext *s;
1065 void *samples_l = NULL, *samples_r = NULL;
1067 int got_terms = 0, got_weights = 0, got_samples = 0,
1068 got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1070 int i, j, id, size, ssize, weights, t;
1071 int bpp, chan = 0, orig_bpp, sample_rate = 0, rate_x = 1, dsd_mode = 0;
1073 uint64_t chmask = 0;
1075 if (block_no >= wc->fdec_num && wv_alloc_frame_context(wc) < 0) {
1076 av_log(avctx, AV_LOG_ERROR, "Error creating frame decode context\n");
1077 return AVERROR_INVALIDDATA;
1080 s = wc->fdec[block_no];
1082 av_log(avctx, AV_LOG_ERROR, "Context for block %d is not present\n",
1084 return AVERROR_INVALIDDATA;
1087 memset(s->decorr, 0, MAX_TERMS * sizeof(Decorr));
1088 memset(s->ch, 0, sizeof(s->ch));
1090 s->and = s->or = s->shift = 0;
1091 s->got_extra_bits = 0;
1093 bytestream2_init(&gb, buf, buf_size);
1095 s->samples = bytestream2_get_le32(&gb);
1096 if (s->samples != wc->samples) {
1097 av_log(avctx, AV_LOG_ERROR, "Mismatching number of samples in "
1098 "a sequence: %d and %d\n", wc->samples, s->samples);
1099 return AVERROR_INVALIDDATA;
1101 s->frame_flags = bytestream2_get_le32(&gb);
1102 bpp = av_get_bytes_per_sample(avctx->sample_fmt);
1103 orig_bpp = ((s->frame_flags & 0x03) + 1) << 3;
1104 multiblock = (s->frame_flags & WV_SINGLE_BLOCK) != WV_SINGLE_BLOCK;
1106 s->stereo = !(s->frame_flags & WV_MONO);
1107 s->stereo_in = (s->frame_flags & WV_FALSE_STEREO) ? 0 : s->stereo;
1108 s->joint = s->frame_flags & WV_JOINT_STEREO;
1109 s->hybrid = s->frame_flags & WV_HYBRID_MODE;
1110 s->hybrid_bitrate = s->frame_flags & WV_HYBRID_BITRATE;
1111 s->post_shift = bpp * 8 - orig_bpp + ((s->frame_flags >> 13) & 0x1f);
1112 if (s->post_shift < 0 || s->post_shift > 31) {
1113 return AVERROR_INVALIDDATA;
1115 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
1116 s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
1117 s->CRC = bytestream2_get_le32(&gb);
1119 // parse metadata blocks
1120 while (bytestream2_get_bytes_left(&gb)) {
1121 id = bytestream2_get_byte(&gb);
1122 size = bytestream2_get_byte(&gb);
1123 if (id & WP_IDF_LONG)
1124 size |= (bytestream2_get_le16u(&gb)) << 8;
1125 size <<= 1; // size is specified in words
1127 if (id & WP_IDF_ODD)
1130 av_log(avctx, AV_LOG_ERROR,
1131 "Got incorrect block %02X with size %i\n", id, size);
1134 if (bytestream2_get_bytes_left(&gb) < ssize) {
1135 av_log(avctx, AV_LOG_ERROR,
1136 "Block size %i is out of bounds\n", size);
1139 switch (id & WP_IDF_MASK) {
1140 case WP_ID_DECTERMS:
1141 if (size > MAX_TERMS) {
1142 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation terms\n");
1144 bytestream2_skip(&gb, ssize);
1148 for (i = 0; i < s->terms; i++) {
1149 uint8_t val = bytestream2_get_byte(&gb);
1150 s->decorr[s->terms - i - 1].value = (val & 0x1F) - 5;
1151 s->decorr[s->terms - i - 1].delta = val >> 5;
1155 case WP_ID_DECWEIGHTS:
1157 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1160 weights = size >> s->stereo_in;
1161 if (weights > MAX_TERMS || weights > s->terms) {
1162 av_log(avctx, AV_LOG_ERROR, "Too many decorrelation weights\n");
1163 bytestream2_skip(&gb, ssize);
1166 for (i = 0; i < weights; i++) {
1167 t = (int8_t)bytestream2_get_byte(&gb);
1168 s->decorr[s->terms - i - 1].weightA = t * (1 << 3);
1169 if (s->decorr[s->terms - i - 1].weightA > 0)
1170 s->decorr[s->terms - i - 1].weightA +=
1171 (s->decorr[s->terms - i - 1].weightA + 64) >> 7;
1173 t = (int8_t)bytestream2_get_byte(&gb);
1174 s->decorr[s->terms - i - 1].weightB = t * (1 << 3);
1175 if (s->decorr[s->terms - i - 1].weightB > 0)
1176 s->decorr[s->terms - i - 1].weightB +=
1177 (s->decorr[s->terms - i - 1].weightB + 64) >> 7;
1182 case WP_ID_DECSAMPLES:
1184 av_log(avctx, AV_LOG_ERROR, "No decorrelation terms met\n");
1188 for (i = s->terms - 1; (i >= 0) && (t < size); i--) {
1189 if (s->decorr[i].value > 8) {
1190 s->decorr[i].samplesA[0] =
1191 wp_exp2(bytestream2_get_le16(&gb));
1192 s->decorr[i].samplesA[1] =
1193 wp_exp2(bytestream2_get_le16(&gb));
1196 s->decorr[i].samplesB[0] =
1197 wp_exp2(bytestream2_get_le16(&gb));
1198 s->decorr[i].samplesB[1] =
1199 wp_exp2(bytestream2_get_le16(&gb));
1203 } else if (s->decorr[i].value < 0) {
1204 s->decorr[i].samplesA[0] =
1205 wp_exp2(bytestream2_get_le16(&gb));
1206 s->decorr[i].samplesB[0] =
1207 wp_exp2(bytestream2_get_le16(&gb));
1210 for (j = 0; j < s->decorr[i].value; j++) {
1211 s->decorr[i].samplesA[j] =
1212 wp_exp2(bytestream2_get_le16(&gb));
1214 s->decorr[i].samplesB[j] =
1215 wp_exp2(bytestream2_get_le16(&gb));
1218 t += s->decorr[i].value * 2 * (s->stereo_in + 1);
1224 if (size != 6 * (s->stereo_in + 1)) {
1225 av_log(avctx, AV_LOG_ERROR,
1226 "Entropy vars size should be %i, got %i.\n",
1227 6 * (s->stereo_in + 1), size);
1228 bytestream2_skip(&gb, ssize);
1231 for (j = 0; j <= s->stereo_in; j++)
1232 for (i = 0; i < 3; i++) {
1233 s->ch[j].median[i] = wp_exp2(bytestream2_get_le16(&gb));
1238 if (s->hybrid_bitrate) {
1239 for (i = 0; i <= s->stereo_in; i++) {
1240 s->ch[i].slow_level = wp_exp2(bytestream2_get_le16(&gb));
1244 for (i = 0; i < (s->stereo_in + 1); i++) {
1245 s->ch[i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1249 for (i = 0; i < (s->stereo_in + 1); i++) {
1250 s->ch[i].bitrate_delta =
1251 wp_exp2((int16_t)bytestream2_get_le16(&gb));
1254 for (i = 0; i < (s->stereo_in + 1); i++)
1255 s->ch[i].bitrate_delta = 0;
1259 case WP_ID_INT32INFO: {
1262 av_log(avctx, AV_LOG_ERROR,
1263 "Invalid INT32INFO, size = %i\n",
1265 bytestream2_skip(&gb, ssize - 4);
1268 bytestream2_get_buffer(&gb, val, 4);
1270 av_log(avctx, AV_LOG_ERROR,
1271 "Invalid INT32INFO, extra_bits = %d (> 30)\n", val[0]);
1273 } else if (val[0]) {
1274 s->extra_bits = val[0];
1275 } else if (val[1]) {
1277 } else if (val[2]) {
1280 } else if (val[3]) {
1284 if (s->shift > 31) {
1285 av_log(avctx, AV_LOG_ERROR,
1286 "Invalid INT32INFO, shift = %d (> 31)\n", s->shift);
1287 s->and = s->or = s->shift = 0;
1290 /* original WavPack decoder forces 32-bit lossy sound to be treated
1291 * as 24-bit one in order to have proper clipping */
1292 if (s->hybrid && bpp == 4 && s->post_shift < 8 && s->shift > 8) {
1295 s->hybrid_maxclip >>= 8;
1296 s->hybrid_minclip >>= 8;
1300 case WP_ID_FLOATINFO:
1302 av_log(avctx, AV_LOG_ERROR,
1303 "Invalid FLOATINFO, size = %i\n", size);
1304 bytestream2_skip(&gb, ssize);
1307 s->float_flag = bytestream2_get_byte(&gb);
1308 s->float_shift = bytestream2_get_byte(&gb);
1309 s->float_max_exp = bytestream2_get_byte(&gb);
1310 if (s->float_shift > 31) {
1311 av_log(avctx, AV_LOG_ERROR,
1312 "Invalid FLOATINFO, shift = %d (> 31)\n", s->float_shift);
1317 bytestream2_skip(&gb, 1);
1320 if ((ret = init_get_bits8(&s->gb, gb.buffer, size)) < 0)
1322 bytestream2_skip(&gb, size);
1325 case WP_ID_DSD_DATA:
1327 av_log(avctx, AV_LOG_ERROR, "Invalid DSD_DATA, size = %i\n",
1329 bytestream2_skip(&gb, ssize);
1332 rate_x = 1 << bytestream2_get_byte(&gb);
1333 dsd_mode = bytestream2_get_byte(&gb);
1334 if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1335 av_log(avctx, AV_LOG_ERROR, "Invalid DSD encoding mode: %d\n",
1337 return AVERROR_INVALIDDATA;
1339 bytestream2_init(&s->gbyte, gb.buffer, size-2);
1340 bytestream2_skip(&gb, size-2);
1343 case WP_ID_EXTRABITS:
1345 av_log(avctx, AV_LOG_ERROR, "Invalid EXTRABITS, size = %i\n",
1347 bytestream2_skip(&gb, size);
1350 if ((ret = init_get_bits8(&s->gb_extra_bits, gb.buffer, size)) < 0)
1352 s->crc_extra_bits = get_bits_long(&s->gb_extra_bits, 32);
1353 bytestream2_skip(&gb, size);
1354 s->got_extra_bits = 1;
1356 case WP_ID_CHANINFO:
1358 av_log(avctx, AV_LOG_ERROR,
1359 "Insufficient channel information\n");
1360 return AVERROR_INVALIDDATA;
1362 chan = bytestream2_get_byte(&gb);
1365 chmask = bytestream2_get_byte(&gb);
1368 chmask = bytestream2_get_le16(&gb);
1371 chmask = bytestream2_get_le24(&gb);
1374 chmask = bytestream2_get_le32(&gb);
1377 size = bytestream2_get_byte(&gb);
1378 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1380 if (avctx->channels != chan)
1381 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1382 " instead of %i.\n", chan, avctx->channels);
1383 chmask = bytestream2_get_le24(&gb);
1386 size = bytestream2_get_byte(&gb);
1387 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1389 if (avctx->channels != chan)
1390 av_log(avctx, AV_LOG_WARNING, "%i channels signalled"
1391 " instead of %i.\n", chan, avctx->channels);
1392 chmask = bytestream2_get_le32(&gb);
1395 av_log(avctx, AV_LOG_ERROR, "Invalid channel info size %d\n",
1397 chan = avctx->channels;
1398 chmask = avctx->channel_layout;
1401 case WP_ID_SAMPLE_RATE:
1403 av_log(avctx, AV_LOG_ERROR, "Invalid custom sample rate.\n");
1404 return AVERROR_INVALIDDATA;
1406 sample_rate = bytestream2_get_le24(&gb);
1409 bytestream2_skip(&gb, size);
1411 if (id & WP_IDF_ODD)
1412 bytestream2_skip(&gb, 1);
1417 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation terms\n");
1418 return AVERROR_INVALIDDATA;
1421 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation weights\n");
1422 return AVERROR_INVALIDDATA;
1425 av_log(avctx, AV_LOG_ERROR, "No block with decorrelation samples\n");
1426 return AVERROR_INVALIDDATA;
1429 av_log(avctx, AV_LOG_ERROR, "No block with entropy info\n");
1430 return AVERROR_INVALIDDATA;
1432 if (s->hybrid && !got_hybrid) {
1433 av_log(avctx, AV_LOG_ERROR, "Hybrid config not found\n");
1434 return AVERROR_INVALIDDATA;
1436 if (!got_float && avctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
1437 av_log(avctx, AV_LOG_ERROR, "Float information not found\n");
1438 return AVERROR_INVALIDDATA;
1440 if (s->got_extra_bits && avctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
1441 const int size = get_bits_left(&s->gb_extra_bits);
1442 const int wanted = s->samples * s->extra_bits << s->stereo_in;
1443 if (size < wanted) {
1444 av_log(avctx, AV_LOG_ERROR, "Too small EXTRABITS\n");
1445 s->got_extra_bits = 0;
1450 if (!got_pcm && !got_dsd) {
1451 av_log(avctx, AV_LOG_ERROR, "Packed samples not found\n");
1452 return AVERROR_INVALIDDATA;
1455 if ((got_pcm && wc->modulation != MODULATION_PCM) ||
1456 (got_dsd && wc->modulation != MODULATION_DSD)) {
1457 av_log(avctx, AV_LOG_ERROR, "Invalid PCM/DSD mix encountered\n");
1458 return AVERROR_INVALIDDATA;
1461 if (!wc->ch_offset) {
1462 int sr = (s->frame_flags >> 23) & 0xf;
1465 av_log(avctx, AV_LOG_ERROR, "Custom sample rate missing.\n");
1466 return AVERROR_INVALIDDATA;
1468 avctx->sample_rate = sample_rate * rate_x;
1470 avctx->sample_rate = wv_rates[sr] * rate_x;
1474 avctx->channels = chan;
1476 avctx->channel_layout = chmask;
1478 avctx->channels = s->stereo ? 2 : 1;
1479 avctx->channel_layout = s->stereo ? AV_CH_LAYOUT_STEREO :
1483 ff_thread_release_buffer(avctx, &wc->prev_frame);
1484 FFSWAP(ThreadFrame, wc->curr_frame, wc->prev_frame);
1486 /* get output buffer */
1487 wc->curr_frame.f->nb_samples = s->samples;
1488 if ((ret = ff_thread_get_buffer(avctx, &wc->curr_frame, AV_GET_BUFFER_FLAG_REF)) < 0)
1491 wc->frame = wc->curr_frame.f;
1492 ff_thread_finish_setup(avctx);
1495 if (wc->ch_offset + s->stereo >= avctx->channels) {
1496 av_log(avctx, AV_LOG_WARNING, "Too many channels coded in a packet.\n");
1497 return ((avctx->err_recognition & AV_EF_EXPLODE) || !wc->ch_offset) ? AVERROR_INVALIDDATA : 0;
1500 samples_l = wc->frame->extended_data[wc->ch_offset];
1502 samples_r = wc->frame->extended_data[wc->ch_offset + 1];
1504 wc->ch_offset += 1 + s->stereo;
1508 if (dsd_mode == 3) {
1509 ret = wv_unpack_dsd_high(s, samples_l, samples_r);
1510 } else if (dsd_mode == 1) {
1511 ret = wv_unpack_dsd_fast(s, samples_l, samples_r);
1513 ret = wv_unpack_dsd_copy(s, samples_l, samples_r);
1516 ret = wv_unpack_stereo(s, &s->gb, samples_l, samples_r, avctx->sample_fmt);
1522 if (dsd_mode == 3) {
1523 ret = wv_unpack_dsd_high(s, samples_l, NULL);
1524 } else if (dsd_mode == 1) {
1525 ret = wv_unpack_dsd_fast(s, samples_l, NULL);
1527 ret = wv_unpack_dsd_copy(s, samples_l, NULL);
1530 ret = wv_unpack_mono(s, &s->gb, samples_l, avctx->sample_fmt);
1536 memcpy(samples_r, samples_l, bpp * s->samples);
1542 static void wavpack_decode_flush(AVCodecContext *avctx)
1544 WavpackContext *s = avctx->priv_data;
1546 if (!avctx->internal->is_copy) {
1547 for (int i = 0; i < avctx->channels; i++)
1548 memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
1552 static int dsd_channel(AVCodecContext *avctx, void *frmptr, int jobnr, int threadnr)
1554 WavpackContext *s = avctx->priv_data;
1555 AVFrame *frame = frmptr;
1557 ff_dsd2pcm_translate (&s->dsdctx [jobnr], s->samples, 0,
1558 (uint8_t *)frame->extended_data[jobnr], 4,
1559 (float *)frame->extended_data[jobnr], 1);
1564 static int wavpack_decode_frame(AVCodecContext *avctx, void *data,
1565 int *got_frame_ptr, AVPacket *avpkt)
1567 WavpackContext *s = avctx->priv_data;
1568 const uint8_t *buf = avpkt->data;
1569 int buf_size = avpkt->size;
1570 int frame_size, ret, frame_flags;
1572 if (avpkt->size <= WV_HEADER_SIZE)
1573 return AVERROR_INVALIDDATA;
1579 /* determine number of samples */
1580 s->samples = AV_RL32(buf + 20);
1581 frame_flags = AV_RL32(buf + 24);
1582 if (s->samples <= 0 || s->samples > WV_MAX_SAMPLES) {
1583 av_log(avctx, AV_LOG_ERROR, "Invalid number of samples: %d\n",
1585 return AVERROR_INVALIDDATA;
1588 s->modulation = (frame_flags & WV_DSD_DATA) ? MODULATION_DSD : MODULATION_PCM;
1590 if (frame_flags & (WV_FLOAT_DATA | WV_DSD_DATA)) {
1591 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
1592 } else if ((frame_flags & 0x03) <= 1) {
1593 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
1595 avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
1596 avctx->bits_per_raw_sample = ((frame_flags & 0x03) + 1) << 3;
1599 while (buf_size > WV_HEADER_SIZE) {
1600 frame_size = AV_RL32(buf + 4) - 12;
1603 if (frame_size <= 0 || frame_size > buf_size) {
1604 av_log(avctx, AV_LOG_ERROR,
1605 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1606 s->block, frame_size, buf_size);
1607 ret = AVERROR_INVALIDDATA;
1610 if ((ret = wavpack_decode_block(avctx, s->block, buf, frame_size)) < 0)
1614 buf_size -= frame_size;
1617 if (s->ch_offset != avctx->channels) {
1618 av_log(avctx, AV_LOG_ERROR, "Not enough channels coded in a packet.\n");
1619 ret = AVERROR_INVALIDDATA;
1623 ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1624 ff_thread_release_buffer(avctx, &s->prev_frame);
1626 if (s->modulation == MODULATION_DSD)
1627 avctx->execute2(avctx, dsd_channel, s->frame, NULL, avctx->channels);
1629 ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1631 if ((ret = av_frame_ref(data, s->frame)) < 0)
1640 ff_thread_await_progress(&s->prev_frame, INT_MAX, 0);
1641 ff_thread_release_buffer(avctx, &s->prev_frame);
1642 ff_thread_report_progress(&s->curr_frame, INT_MAX, 0);
1648 AVCodec ff_wavpack_decoder = {
1650 .long_name = NULL_IF_CONFIG_SMALL("WavPack"),
1651 .type = AVMEDIA_TYPE_AUDIO,
1652 .id = AV_CODEC_ID_WAVPACK,
1653 .priv_data_size = sizeof(WavpackContext),
1654 .init = wavpack_decode_init,
1655 .close = wavpack_decode_end,
1656 .decode = wavpack_decode_frame,
1657 .flush = wavpack_decode_flush,
1658 .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
1659 .update_thread_context = ONLY_IF_THREADS_ENABLED(update_thread_context),
1660 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
1661 AV_CODEC_CAP_SLICE_THREADS