1 /*
2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
4 * Copyright (c) 2020 David Bryant
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
25
26 #define BITSTREAM_READER_LE
36
37 /**
38 * @file
39 * WavPack lossless audio decoder
40 */
41
42 #define DSD_BYTE_READY(low,high) (!(((low) ^ (high)) & 0xff000000))
43
45 #define PTABLE_BINS (1<<PTABLE_BITS)
46 #define PTABLE_MASK (PTABLE_BINS-1)
47
49 #define DOWN 0x00010000
51
53 #define VALUE_ONE (1 << PRECISION)
54 #define PRECISION_USE 12
55
57
58 #define MAX_HISTORY_BITS 5
59 #define MAX_HISTORY_BINS (1 << MAX_HISTORY_BITS)
60 #define MAX_BIN_BYTES 1280 // for value_lookup, per bin (2k - 512 - 256)
61
66
90
98
99 #define WV_MAX_FRAME_DECODERS 14
100
103
106
110
114
119
120 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
121
123 {
124 int p, e, res;
125
126 if (k < 1)
127 return 0;
129 e = (1LL << (p + 1)) - k - 1;
131 if (res >= e)
133 return res;
134 }
135
137 {
139
140 for (
i = 0;
i <=
ctx->stereo_in;
i++) {
141 if (
ctx->ch[
i].bitrate_acc > UINT_MAX -
ctx->ch[
i].bitrate_delta)
143 ctx->ch[
i].bitrate_acc +=
ctx->ch[
i].bitrate_delta;
144 br[
i] =
ctx->ch[
i].bitrate_acc >> 16;
146 }
147 if (
ctx->stereo_in &&
ctx->hybrid_bitrate) {
148 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
149 if (balance > br[0]) {
150 br[1] = br[0] * 2;
151 br[0] = 0;
152 } else if (-balance > br[0]) {
153 br[0] *= 2;
154 br[1] = 0;
155 } else {
156 br[1] = br[0] + balance;
157 br[0] = br[0] - balance;
158 }
159 }
160 for (
i = 0;
i <=
ctx->stereo_in;
i++) {
161 if (
ctx->hybrid_bitrate) {
162 if (sl[
i] - br[
i] > -0x100)
164 else
165 ctx->ch[
i].error_limit = 0;
166 } else {
168 }
169 }
170
171 return 0;
172 }
173
176 {
180
181 *last = 0;
182
183 if ((
ctx->ch[0].median[0] < 2
U) && (
ctx->ch[1].median[0] < 2
U) &&
184 !
ctx->zero && !
ctx->one) {
189 return 0;
190 }
191 } else {
193 if (t >= 2) {
197 } else {
200 }
203 memset(
ctx->ch[0].median, 0,
sizeof(
ctx->ch[0].median));
204 memset(
ctx->ch[1].median, 0,
sizeof(
ctx->ch[1].median));
206 return 0;
207 }
208 }
209 }
210
212 t = 0;
214 } else {
218 if (t == 16) {
224 } else {
228 }
229 }
230
233 t = (t >> 1) + 1;
234 } else {
236 t >>= 1;
237 }
239 }
240
244 }
245
246 if (!t) {
250 } else if (t == 1) {
255 } else if (t == 2) {
261 } else {
267 }
268 if (!
c->error_limit) {
272 } else {
273 int mid = (
base * 2
U +
add + 1) >> 1;
274 while (
add >
c->error_limit) {
280 } else
281 add = mid - (unsigned)
base - 1;
283 }
285 }
287 if (
ctx->hybrid_bitrate)
290
295 }
296 *last = 1;
297 return 0;
298 }
299
302 {
304
306 S *= 1 <<
s->extra_bits;
307
308 if (
s->got_extra_bits &&
311 *crc = *crc * 9 + (
S & 0xffff) * 3 + ((
unsigned)
S >> 16);
312 }
313 }
314
315 bit = (
S &
s->and) |
s->or;
317
320
321 return bit <<
s->post_shift;
322 }
323
325 {
326 union {
330
331 unsigned int sign;
332 int exp =
s->float_max_exp;
333
334 if (
s->got_extra_bits) {
335 const int max_bits = 1 + 23 + 8 + 1;
337
339 return 0.0;
340 }
341
343 S *= 1
U <<
s->float_shift;
345 if (sign)
347 if (
S >= 0x1000000U) {
348 if (
s->got_extra_bits &&
get_bits1(&
s->gb_extra_bits))
350 else
355 exp =
s->float_max_exp;
359
363 (
s->got_extra_bits &&
367 }
else if (
s->got_extra_bits &&
370 }
371 }
372 } else {
373 exp =
s->float_max_exp;
374 }
376 } else {
377 sign = 0;
382 if (
s->float_max_exp >= 25)
385 } else {
388 }
389 }
390 }
391
392 *crc = *crc * 27 +
S * 9 +
exp * 3 + sign;
393
394 value.u = (sign << 31) | (
exp << 23) |
S;
396 }
397
399 uint32_t crc_extra_bits)
400 {
404 }
405 if (
s->got_extra_bits && crc_extra_bits !=
s->crc_extra_bits) {
408 }
409
410 return 0;
411 }
412
414 {
415 int value = 0x808000, rate = rate_i << 8;
416
417 for (
int c = (rate + 128) >> 8;
c--;)
419
423
424 if (
value > 0x010000) {
425 rate += (rate * rate_s + 128) >> 8;
426
427 for (
int c = (rate + 64) >> 7;
c--;)
429 }
430 }
431 }
432
437
439 {
440 uint32_t checksum = 0xFFFFFFFF;
441 uint8_t *dst_l = dst_left, *dst_r = dst_right;
442 int total_samples =
s->samples, stereo = dst_r ? 1 : 0;
444 int rate_i, rate_s;
445 uint32_t low, high,
value;
446
449
450 rate_i = bytestream2_get_byte(&
s->gbyte);
451 rate_s = bytestream2_get_byte(&
s->gbyte);
452
455
457
460
461 sp->fltr1 = bytestream2_get_byte(&
s->gbyte) << (
PRECISION - 8);
462 sp->fltr2 = bytestream2_get_byte(&
s->gbyte) << (
PRECISION - 8);
463 sp->fltr3 = bytestream2_get_byte(&
s->gbyte) << (
PRECISION - 8);
464 sp->fltr4 = bytestream2_get_byte(&
s->gbyte) << (
PRECISION - 8);
465 sp->fltr5 = bytestream2_get_byte(&
s->gbyte) << (
PRECISION - 8);
467 sp->factor = bytestream2_get_byte(&
s->gbyte) & 0xff;
468 sp->factor |= (bytestream2_get_byte(&
s->gbyte) << 8) & 0xff00;
469 sp->factor = (
int32_t)((uint32_t)
sp->factor << 16) >> 16;
470 }
471
472 value = bytestream2_get_be32(&
s->gbyte);
473 high = 0xffffffff;
474 low = 0x0;
475
476 while (total_samples--) {
477 int bitcount = 8;
478
479 sp[0].value =
sp[0].fltr1 -
sp[0].fltr5 + ((
sp[0].fltr6 *
sp[0].factor) >> 2);
480
481 if (stereo)
482 sp[1].value =
sp[1].fltr1 -
sp[1].fltr5 + ((
sp[1].fltr6 *
sp[1].factor) >> 2);
483
484 while (bitcount--) {
486 uint32_t
split = low + ((high - low) >> 8) * (*pp >> 16);
487
492 } else {
496 }
497
501 value = (
value << 8) | bytestream2_get_byte(&
s->gbyte);
502 high = (high << 8) | 0xff;
503 low <<= 8;
504 }
505
506 sp[0].value +=
sp[0].fltr6 * 8;
507 sp[0].byte = (
sp[0].byte << 1) | (
sp[0].fltr0 & 1);
508 sp[0].factor += (((
sp[0].value ^
sp[0].fltr0) >> 31) | 1) &
512 sp[0].fltr3 += (
sp[0].fltr2 -
sp[0].fltr3) >> 4;
513 sp[0].fltr4 += (
sp[0].fltr3 -
sp[0].fltr4) >> 4;
514 sp[0].value = (
sp[0].fltr4 -
sp[0].fltr5) >> 4;
515 sp[0].fltr5 +=
sp[0].value;
516 sp[0].fltr6 += (
sp[0].value -
sp[0].fltr6) >> 3;
517 sp[0].value =
sp[0].fltr1 -
sp[0].fltr5 + ((
sp[0].fltr6 *
sp[0].factor) >> 2);
518
519 if (!stereo)
520 continue;
521
523 split = low + ((high - low) >> 8) * (*pp >> 16);
524
529 } else {
533 }
534
538 value = (
value << 8) | bytestream2_get_byte(&
s->gbyte);
539 high = (high << 8) | 0xff;
540 low <<= 8;
541 }
542
543 sp[1].value +=
sp[1].fltr6 * 8;
544 sp[1].byte = (
sp[1].byte << 1) | (
sp[1].fltr0 & 1);
545 sp[1].factor += (((
sp[1].value ^
sp[1].fltr0) >> 31) | 1) &
549 sp[1].fltr3 += (
sp[1].fltr2 -
sp[1].fltr3) >> 4;
550 sp[1].fltr4 += (
sp[1].fltr3 -
sp[1].fltr4) >> 4;
551 sp[1].value = (
sp[1].fltr4 -
sp[1].fltr5) >> 4;
552 sp[1].fltr5 +=
sp[1].value;
553 sp[1].fltr6 += (
sp[1].value -
sp[1].fltr6) >> 3;
554 sp[1].value =
sp[1].fltr1 -
sp[1].fltr5 + ((
sp[1].fltr6 *
sp[1].factor) >> 2);
555 }
556
557 checksum += (checksum << 1) + (*dst_l =
sp[0].
byte & 0xff);
558 sp[0].factor -= (
sp[0].factor + 512) >> 10;
559 dst_l += 4;
560
561 if (stereo) {
562 checksum += (checksum << 1) + (*dst_r =
filters[1].
byte & 0xff);
564 dst_r += 4;
565 }
566 }
567
571
572 memset(dst_left, 0x69,
s->samples * 4);
573
574 if (dst_r)
575 memset(dst_right, 0x69,
s->samples * 4);
576 }
577
578 return 0;
579 }
580
582 {
583 uint8_t *dst_l = dst_left, *dst_r = dst_right;
584 uint8_t history_bits, max_probability;
585 int total_summed_probabilities = 0;
586 int total_samples =
s->samples;
587 uint8_t *vlb =
s->value_lookup_buffer;
588 int history_bins, p0, p1, chan;
589 uint32_t checksum = 0xFFFFFFFF;
590 uint32_t low, high,
value;
591
594
595 history_bits = bytestream2_get_byte(&
s->gbyte);
596
599
600 history_bins = 1 << history_bits;
601 max_probability = bytestream2_get_byte(&
s->gbyte);
602
603 if (max_probability < 0xff) {
604 uint8_t *outptr = (uint8_t *)
s->probabilities;
605 uint8_t *outend = outptr +
sizeof(*
s->probabilities) * history_bins;
606
608 int code = bytestream2_get_byte(&
s->gbyte);
609
610 if (
code > max_probability) {
611 int zcount =
code - max_probability;
612
613 while (outptr < outend && zcount--)
614 *outptr++ = 0;
617 }
618 else {
619 break;
620 }
621 }
622
623 if (outptr < outend ||
628 sizeof(*
s->probabilities) * history_bins);
629 } else {
631 }
632
633 for (p0 = 0; p0 < history_bins; p0++) {
635
636 for (
int i = 0;
i < 256;
i++)
637 s->summed_probabilities[p0][
i] = sum_values +=
s->probabilities[p0][
i];
638
640 total_summed_probabilities += sum_values;
641
642 if (total_summed_probabilities > history_bins *
MAX_BIN_BYTES)
644
645 s->value_lookup[p0] = vlb;
646
647 for (
int i = 0;
i < 256;
i++) {
648 int c =
s->probabilities[p0][
i];
649
652 }
653 }
654 }
655
658
659 chan = p0 = p1 = 0;
660 low = 0; high = 0xffffffff;
661 value = bytestream2_get_be32(&
s->gbyte);
662
663 if (dst_r)
664 total_samples *= 2;
665
666 while (total_samples--) {
668
669 if (!
s->summed_probabilities[p0][255])
671
672 mult = (high - low) /
s->summed_probabilities[p0][255];
673
676 value = bytestream2_get_be32(&
s->gbyte);
677
678 low = 0;
679 high = 0xffffffff;
680 mult = high /
s->summed_probabilities[p0][255];
681
684 }
685
687
688 if (
index >=
s->summed_probabilities[p0][255])
690
691 if (!dst_r) {
692 if ((*dst_l =
code =
s->value_lookup[p0][
index]))
693 low +=
s->summed_probabilities[p0][
code-1] *
mult;
694
695 dst_l += 4;
696 } else {
698 low +=
s->summed_probabilities[p0][
code-1] *
mult;
699
702 dst_r += 4;
703 }
704 else {
706 dst_l += 4;
707 }
708
709 chan ^= 1;
710 }
711
712 high = low +
s->probabilities[p0][
code] *
mult - 1;
713 checksum += (checksum << 1) +
code;
714
715 if (!dst_r) {
716 p0 =
code & (history_bins-1);
717 } else {
718 p0 = p1;
719 p1 =
code & (history_bins-1);
720 }
721
723 value = (
value << 8) | bytestream2_get_byte(&
s->gbyte);
724 high = (high << 8) | 0xff;
725 low <<= 8;
726 }
727 }
728
732
733 memset(dst_left, 0x69,
s->samples * 4);
734
735 if (dst_r)
736 memset(dst_right, 0x69,
s->samples * 4);
737 }
738
739 return 0;
740 }
741
743 {
744 uint8_t *dst_l = dst_left, *dst_r = dst_right;
745 int total_samples =
s->samples;
746 uint32_t checksum = 0xFFFFFFFF;
747
750
751 while (total_samples--) {
752 checksum += (checksum << 1) + (*dst_l = bytestream2_get_byte(&
s->gbyte));
753 dst_l += 4;
754
755 if (dst_r) {
756 checksum += (checksum << 1) + (*dst_r = bytestream2_get_byte(&
s->gbyte));
757 dst_r += 4;
758 }
759 }
760
764
765 memset(dst_left, 0x69,
s->samples * 4);
766
767 if (dst_r)
768 memset(dst_right, 0x69,
s->samples * 4);
769 }
770
771 return 0;
772 }
773
775 void *dst_l,
void *dst_r,
const int type)
776 {
778 int last, t;
781 uint32_t crc = 0xFFFFFFFF;
782 uint32_t crc_extra_bits = 0xFFFFFFFF;
783 int16_t *dst16_l = dst_l;
784 int16_t *dst16_r = dst_r;
787 float *dstfl_l = dst_l;
788 float *dstfl_r = dst_r;
789
790 s->one =
s->zero =
s->zeroes = 0;
791 do {
793 if (last)
794 break;
796 if (last)
797 break;
798 for (
i = 0;
i <
s->terms;
i++) {
799 t =
s->decorr[
i].value;
800 if (t > 0) {
801 if (t > 8) {
802 if (t & 1) {
803 A = 2
U *
s->decorr[
i].samplesA[0] -
s->decorr[
i].samplesA[1];
804 B = 2
U *
s->decorr[
i].samplesB[0] -
s->decorr[
i].samplesB[1];
805 } else {
806 A = (
int)(3
U *
s->decorr[
i].samplesA[0] -
s->decorr[
i].samplesA[1]) >> 1;
807 B = (
int)(3
U *
s->decorr[
i].samplesB[0] -
s->decorr[
i].samplesB[1]) >> 1;
808 }
809 s->decorr[
i].samplesA[1] =
s->decorr[
i].samplesA[0];
810 s->decorr[
i].samplesB[1] =
s->decorr[
i].samplesB[0];
811 j = 0;
812 } else {
813 A =
s->decorr[
i].samplesA[
pos];
814 B =
s->decorr[
i].samplesB[
pos];
816 }
818 L2 =
L + ((
s->decorr[
i].weightA * (int64_t)
A + 512) >> 10);
819 R2 =
R + ((
s->decorr[
i].weightB * (int64_t)
B + 512) >> 10);
820 } else {
821 L2 =
L + (unsigned)((
int)(
s->decorr[
i].weightA * (unsigned)
A + 512) >> 10);
822 R2 =
R + (unsigned)((
int)(
s->decorr[
i].weightB * (unsigned)
B + 512) >> 10);
823 }
825 s->decorr[
i].weightA -= ((((
L ^
A) >> 30) & 2) - 1) *
s->decorr[
i].delta;
827 s->decorr[
i].weightB -= ((((
R ^
B) >> 30) & 2) - 1) *
s->decorr[
i].delta;
828 s->decorr[
i].samplesA[j] =
L =
L2;
829 s->decorr[
i].samplesB[j] =
R =
R2;
830 } else if (t == -1) {
832 L2 =
L + ((
s->decorr[
i].weightA * (int64_t)
s->decorr[
i].samplesA[0] + 512) >> 10);
833 else
834 L2 =
L + (unsigned)((
int)(
s->decorr[
i].weightA * (unsigned)
s->decorr[
i].samplesA[0] + 512) >> 10);
838 R2 =
R + ((
s->decorr[
i].weightB * (int64_t)
L2 + 512) >> 10);
839 else
840 R2 =
R + (unsigned)((
int)(
s->decorr[
i].weightB * (unsigned)
L2 + 512) >> 10);
843 s->decorr[
i].samplesA[0] =
R;
844 } else {
846 R2 =
R + ((
s->decorr[
i].weightB * (int64_t)
s->decorr[
i].samplesB[0] + 512) >> 10);
847 else
848 R2 =
R + (unsigned)((
int)(
s->decorr[
i].weightB * (unsigned)
s->decorr[
i].samplesB[0] + 512) >> 10);
851
852 if (t == -3) {
853 R2 =
s->decorr[
i].samplesA[0];
854 s->decorr[
i].samplesA[0] =
R;
855 }
856
858 L2 =
L + ((
s->decorr[
i].weightA * (int64_t)
R2 + 512) >> 10);
859 else
860 L2 =
L + (unsigned)((
int)(
s->decorr[
i].weightA * (unsigned)
R2 + 512) >> 10);
863 s->decorr[
i].samplesB[0] =
L;
864 }
865 }
866
871 }
872 }
873
876 L += (unsigned)(
R -= (
unsigned)(
L >> 1));
877 crc = (crc * 3 +
L) * 3 +
R;
878
885 } else {
888 }
889 count++;
890 }
while (!last && count < s->
samples);
891
892 if (last && count < s->
samples) {
894 memset((uint8_t*)dst_l + count*
size, 0, (
s->samples-count)*
size);
895 memset((uint8_t*)dst_r + count*
size, 0, (
s->samples-count)*
size);
896 }
897
901
902 return 0;
903 }
904
906 void *dst,
const int type)
907 {
909 int last, t;
912 uint32_t crc = 0xFFFFFFFF;
913 uint32_t crc_extra_bits = 0xFFFFFFFF;
914 int16_t *dst16 = dst;
916 float *dstfl = dst;
917
918 s->one =
s->zero =
s->zeroes = 0;
919 do {
922 if (last)
923 break;
924 for (
i = 0;
i <
s->terms;
i++) {
925 t =
s->decorr[
i].value;
926 if (t > 8) {
927 if (t & 1)
928 A = 2
U *
s->decorr[
i].samplesA[0] -
s->decorr[
i].samplesA[1];
929 else
930 A = (
int)(3
U *
s->decorr[
i].samplesA[0] -
s->decorr[
i].samplesA[1]) >> 1;
931 s->decorr[
i].samplesA[1] =
s->decorr[
i].samplesA[0];
932 j = 0;
933 } else {
934 A =
s->decorr[
i].samplesA[
pos];
936 }
938 S =
T + ((
s->decorr[
i].weightA * (int64_t)
A + 512) >> 10);
939 else
940 S =
T + (unsigned)((
int)(
s->decorr[
i].weightA * (unsigned)
A + 512) >> 10);
942 s->decorr[
i].weightA -= ((((
T ^
A) >> 30) & 2) - 1) *
s->decorr[
i].delta;
943 s->decorr[
i].samplesA[j] =
T =
S;
944 }
947
952 } else {
954 }
955 count++;
956 }
while (!last && count < s->
samples);
957
958 if (last && count < s->
samples) {
960 memset((uint8_t*)dst + count*
size, 0, (
s->samples-count)*
size);
961 }
962
967 }
968
969 return 0;
970 }
971
973 {
975 return -1;
976
978 if (!
c->fdec[
c->fdec_num])
979 return -1;
981 c->fdec[
c->fdec_num - 1]->avctx =
c->avctx;
982
983 return 0;
984 }
985
987 {
989
993
995 return 0;
996
997 if (
channels > INT_MAX /
sizeof(*
s->dsdctx))
999
1005
1007 memset(
s->dsdctx[
i].buf, 0x69,
sizeof(
s->dsdctx[
i].buf));
1008
1009 return 0;
1010 }
1011
1012 #if HAVE_THREADS
1014 {
1018
1020 return 0;
1021
1026 }
1027
1036 }
1037
1038 return 0;
1039 }
1040 #endif
1041
1043 {
1045
1047
1049
1052
1053 if (!
s->curr_frame.f || !
s->prev_frame.f)
1055
1057
1058 return 0;
1059 }
1060
1062 {
1064
1065 for (
int i = 0;
i <
s->fdec_num;
i++)
1068
1071
1074
1076
1077 return 0;
1078 }
1079
1081 const uint8_t *buf, int buf_size)
1082 {
1087 void *samples_l =
NULL, *samples_r =
NULL;
1089 int got_terms = 0, got_weights = 0, got_samples = 0,
1090 got_entropy = 0, got_pcm = 0, got_float = 0, got_hybrid = 0;
1091 int got_dsd = 0;
1093 int bpp, chan = 0, orig_bpp,
sample_rate = 0, rate_x = 1, dsd_mode = 0;
1094 int multiblock;
1095 uint64_t chmask = 0;
1096
1100 }
1101
1102 s = wc->
fdec[block_no];
1105 block_no);
1107 }
1108
1110 memset(
s->ch, 0,
sizeof(
s->ch));
1112 s->and =
s->or =
s->shift = 0;
1113 s->got_extra_bits = 0;
1114
1116
1117 s->samples = bytestream2_get_le32(&gb);
1120 "a sequence: %d and %d\n", wc->
samples,
s->samples);
1122 }
1123 s->frame_flags = bytestream2_get_le32(&gb);
1124
1127 else if ((
s->frame_flags & 0x03) <= 1)
1129 else
1131
1134
1136 orig_bpp = ((
s->frame_flags & 0x03) + 1) << 3;
1138
1139 s->stereo = !(
s->frame_flags &
WV_MONO);
1144 s->post_shift = bpp * 8 - orig_bpp + ((
s->frame_flags >> 13) & 0x1f);
1145 if (
s->post_shift < 0 ||
s->post_shift > 31) {
1147 }
1148 s->hybrid_maxclip = ((1LL << (orig_bpp - 1)) - 1);
1149 s->hybrid_minclip = ((-1UL << (orig_bpp - 1)));
1150 s->CRC = bytestream2_get_le32(&gb);
1151
1152 // parse metadata blocks
1154 id = bytestream2_get_byte(&gb);
1155 size = bytestream2_get_byte(&gb);
1157 size |= (bytestream2_get_le16u(&gb)) << 8;
1158 size <<= 1;
// size is specified in words
1164 "Got incorrect block %02X with size %i\n",
id,
size);
1165 break;
1166 }
1169 "Block size %i is out of bounds\n",
size);
1170 break;
1171 }
1178 continue;
1179 }
1181 for (
i = 0;
i <
s->terms;
i++) {
1182 uint8_t
val = bytestream2_get_byte(&gb);
1183 s->decorr[
s->terms -
i - 1].value = (
val & 0x1F) - 5;
1184 s->decorr[
s->terms -
i - 1].delta =
val >> 5;
1185 }
1186 got_terms = 1;
1187 break;
1189 if (!got_terms) {
1191 continue;
1192 }
1197 continue;
1198 }
1200 t = (int8_t)bytestream2_get_byte(&gb);
1201 s->decorr[
s->terms -
i - 1].weightA = t * (1 << 3);
1202 if (
s->decorr[
s->terms -
i - 1].weightA > 0)
1203 s->decorr[
s->terms -
i - 1].weightA +=
1204 (
s->decorr[
s->terms -
i - 1].weightA + 64) >> 7;
1206 t = (int8_t)bytestream2_get_byte(&gb);
1207 s->decorr[
s->terms -
i - 1].weightB = t * (1 << 3);
1208 if (
s->decorr[
s->terms -
i - 1].weightB > 0)
1209 s->decorr[
s->terms -
i - 1].weightB +=
1210 (
s->decorr[
s->terms -
i - 1].weightB + 64) >> 7;
1211 }
1212 }
1213 got_weights = 1;
1214 break;
1216 if (!got_terms) {
1218 continue;
1219 }
1220 t = 0;
1221 for (
i =
s->terms - 1; (
i >= 0) && (t <
size);
i--) {
1222 if (
s->decorr[
i].value > 8) {
1223 s->decorr[
i].samplesA[0] =
1224 wp_exp2(bytestream2_get_le16(&gb));
1225 s->decorr[
i].samplesA[1] =
1226 wp_exp2(bytestream2_get_le16(&gb));
1227
1229 s->decorr[
i].samplesB[0] =
1230 wp_exp2(bytestream2_get_le16(&gb));
1231 s->decorr[
i].samplesB[1] =
1232 wp_exp2(bytestream2_get_le16(&gb));
1233 t += 4;
1234 }
1235 t += 4;
1236 }
else if (
s->decorr[
i].value < 0) {
1237 s->decorr[
i].samplesA[0] =
1238 wp_exp2(bytestream2_get_le16(&gb));
1239 s->decorr[
i].samplesB[0] =
1240 wp_exp2(bytestream2_get_le16(&gb));
1241 t += 4;
1242 } else {
1243 for (j = 0; j <
s->decorr[
i].value; j++) {
1244 s->decorr[
i].samplesA[j] =
1245 wp_exp2(bytestream2_get_le16(&gb));
1247 s->decorr[
i].samplesB[j] =
1248 wp_exp2(bytestream2_get_le16(&gb));
1249 }
1250 }
1251 t +=
s->decorr[
i].value * 2 * (
s->stereo_in + 1);
1252 }
1253 }
1254 got_samples = 1;
1255 break;
1257 if (
size != 6 * (
s->stereo_in + 1)) {
1259 "Entropy vars size should be %i, got %i.\n",
1260 6 * (
s->stereo_in + 1),
size);
1262 continue;
1263 }
1264 for (j = 0; j <=
s->stereo_in; j++)
1265 for (
i = 0;
i < 3;
i++) {
1266 s->ch[j].median[
i] =
wp_exp2(bytestream2_get_le16(&gb));
1267 }
1268 got_entropy = 1;
1269 break;
1271 if (
s->hybrid_bitrate) {
1272 for (
i = 0;
i <=
s->stereo_in;
i++) {
1273 s->ch[
i].slow_level =
wp_exp2(bytestream2_get_le16(&gb));
1275 }
1276 }
1277 for (
i = 0;
i < (
s->stereo_in + 1);
i++) {
1278 s->ch[
i].bitrate_acc = bytestream2_get_le16(&gb) << 16;
1280 }
1282 for (
i = 0;
i < (
s->stereo_in + 1);
i++) {
1283 s->ch[
i].bitrate_delta =
1284 wp_exp2((int16_t)bytestream2_get_le16(&gb));
1285 }
1286 } else {
1287 for (
i = 0;
i < (
s->stereo_in + 1);
i++)
1288 s->ch[
i].bitrate_delta = 0;
1289 }
1290 got_hybrid = 1;
1291 break;
1296 "Invalid INT32INFO, size = %i\n",
1299 continue;
1300 }
1304 "Invalid INT32INFO, extra_bits = %d (> 30)\n",
val[0]);
1305 continue;
1306 } else {
1307 s->extra_bits =
val[0];
1308 }
1314 }
1318 }
1319 if (
s->shift > 31) {
1321 "Invalid INT32INFO, shift = %d (> 31)\n",
s->shift);
1322 s->and =
s->or =
s->shift = 0;
1323 continue;
1324 }
1325 /* original WavPack decoder forces 32-bit lossy sound to be treated
1326 * as 24-bit one in order to have proper clipping */
1327 if (
s->hybrid && bpp == 4 &&
s->post_shift < 8 &&
s->shift > 8) {
1330 s->hybrid_maxclip >>= 8;
1331 s->hybrid_minclip >>= 8;
1332 }
1333 break;
1334 }
1338 "Invalid FLOATINFO, size = %i\n",
size);
1340 continue;
1341 }
1342 s->float_flag = bytestream2_get_byte(&gb);
1343 s->float_shift = bytestream2_get_byte(&gb);
1344 s->float_max_exp = bytestream2_get_byte(&gb);
1345 if (
s->float_shift > 31) {
1347 "Invalid FLOATINFO, shift = %d (> 31)\n",
s->float_shift);
1349 continue;
1350 }
1351 got_float = 1;
1353 break;
1358 got_pcm = 1;
1359 break;
1365 continue;
1366 }
1367 rate_x = bytestream2_get_byte(&gb);
1368 if (rate_x > 30)
1370 rate_x = 1 << rate_x;
1371 dsd_mode = bytestream2_get_byte(&gb);
1372 if (dsd_mode && dsd_mode != 1 && dsd_mode != 3) {
1374 dsd_mode);
1376 }
1379 got_dsd = 1;
1380 break;
1386 continue;
1387 }
1392 s->got_extra_bits = 1;
1393 break;
1397 "Insufficient channel information\n");
1399 }
1400 chan = bytestream2_get_byte(&gb);
1402 case 0:
1403 chmask = bytestream2_get_byte(&gb);
1404 break;
1405 case 1:
1406 chmask = bytestream2_get_le16(&gb);
1407 break;
1408 case 2:
1409 chmask = bytestream2_get_le24(&gb);
1410 break;
1411 case 3:
1412 chmask = bytestream2_get_le32(&gb);
1413 break;
1414 case 4:
1415 size = bytestream2_get_byte(&gb);
1416 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1417 chan += 1;
1421 chmask = bytestream2_get_le24(&gb);
1422 break;
1423 case 5:
1424 size = bytestream2_get_byte(&gb);
1425 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
1426 chan += 1;
1430 chmask = bytestream2_get_le32(&gb);
1431 break;
1432 default:
1435 }
1436 break;
1441 }
1443 break;
1444 default:
1446 }
1449 }
1450
1451 if (got_pcm) {
1452 if (!got_terms) {
1455 }
1456 if (!got_weights) {
1459 }
1460 if (!got_samples) {
1463 }
1464 if (!got_entropy) {
1467 }
1468 if (
s->hybrid && !got_hybrid) {
1471 }
1475 }
1478 const int wanted =
s->samples *
s->extra_bits <<
s->stereo_in;
1479 if (
size < wanted) {
1481 s->got_extra_bits = 0;
1482 }
1483 }
1484 }
1485
1486 if (!got_pcm && !got_dsd) {
1489 }
1490
1495 }
1496
1499 int new_samplerate;
1500 int sr = (
s->frame_flags >> 23) & 0
xf;
1505 }
1507 } else
1509
1510 if (new_samplerate * (uint64_t)rate_x > INT_MAX)
1512 new_samplerate *= rate_x;
1513
1514 if (multiblock) {
1515 if (chmask) {
1520 }
1521 } else {
1526 }
1527 }
1528 } else {
1530 }
1531
1532 /* clear DSD state if stream properties change */
1536 !!got_dsd != !!wc->
dsdctx) {
1541 }
1543 }
1548
1551
1552 /* get output buffer */
1558
1561 }
1562
1566 }
1567
1571
1573
1575 if (got_dsd) {
1576 if (dsd_mode == 3) {
1578 } else if (dsd_mode == 1) {
1580 } else {
1582 }
1583 } else {
1585 }
1588 } else {
1589 if (got_dsd) {
1590 if (dsd_mode == 3) {
1592 } else if (dsd_mode == 1) {
1594 } else {
1596 }
1597 } else {
1599 }
1602
1604 memcpy(samples_r, samples_l, bpp *
s->samples);
1605 }
1606
1607 return 0;
1608 }
1609
1611 {
1613
1615 }
1616
1618 {
1621
1623 (uint8_t *)
frame->extended_data[jobnr], 4,
1624 (
float *)
frame->extended_data[jobnr], 1);
1625
1626 return 0;
1627 }
1628
1630 int *got_frame_ptr,
AVPacket *avpkt)
1631 {
1633 const uint8_t *buf = avpkt->
data;
1634 int buf_size = avpkt->
size;
1636
1639
1643
1644 /* determine number of samples */
1646 frame_flags =
AV_RL32(buf + 24);
1651 }
1652
1654
1657 buf += 20;
1658 buf_size -= 20;
1659 if (frame_size <= 0 || frame_size > buf_size) {
1661 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1665 }
1671 }
1672
1677 }
1678
1681
1684
1686
1689
1690 *got_frame_ptr = 1;
1691
1693
1699 }
1700
1702 }
1703
1705 .
p.
name =
"wavpack",
1719 };