1 /*
2 * WavPack lossless audio decoder
3 * Copyright (c) 2006,2011 Konstantin Shishkov
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #define BITSTREAM_READER_LE
23
32
33 /**
34 * @file
35 * WavPack lossless audio decoder
36 */
37
44
72
73 #define WV_MAX_FRAME_DECODERS 14
74
77
80
85
86 #define LEVEL_DECAY(a) (((a) + 0x80) >> 8)
87
89 {
90 int p, e, res;
91
92 if (k < 1)
93 return 0;
95 e = (1 << (p + 1)) - k - 1;
97 if (res >= e)
99 return res;
100 }
101
103 {
104 int i, br[2], sl[2];
105
110 }
112 int balance = (sl[1] - sl[0] + br[1] + 1) >> 1;
113 if (balance > br[0]) {
114 br[1] = br[0] << 1;
115 br[0] = 0;
116 } else if (-balance > br[0]) {
117 br[0] <<= 1;
118 br[1] = 0;
119 } else {
120 br[1] = br[0] + balance;
121 br[0] = br[0] - balance;
122 }
123 }
126 if (sl[i] - br[i] > -0x100)
128 else
130 } else {
132 }
133 }
134 }
135
137 int channel, int *last)
138 {
140 int sign, base, add,
ret;
142
143 *last = 0;
144
151 return 0;
152 }
153 } else {
155 if (t >= 2) {
157 goto error;
158 t =
get_bits(gb, t - 1) | (1 << (t - 1));
159 } else {
161 goto error;
162 }
168 return 0;
169 }
170 }
171 }
172
174 t = 0;
176 } else {
179 goto error;
180 if (t == 16) {
182 if (t2 < 2) {
184 goto error;
186 } else {
188 goto error;
189 t +=
get_bits(gb, t2 - 1) | (1 << (t2 - 1));
190 }
191 }
192
195 t = (t >> 1) + 1;
196 } else {
198 t >>= 1;
199 }
201 }
202
203 if (ctx->
hybrid && !channel)
205
206 if (!t) {
207 base = 0;
210 } else if (t == 1) {
215 } else if (t == 2) {
221 } else {
227 }
229 if (add >= 0x2000000U) {
231 goto error;
232 }
235 goto error;
236 } else {
237 int mid = (base * 2 + add + 1) >> 1;
240 goto error;
242 add -= (mid - base);
243 base = mid;
244 } else
245 add = mid - base - 1;
246 mid = (base * 2 + add + 1) >> 1;
247 }
248 ret = mid;
249 }
253 return sign ? ~ret :
ret;
254
255 error:
257 if (ret <= 0) {
259 }
260 *last = 1;
261 return 0;
262 }
263
266 {
267 int bit;
268
271
275 *crc = *crc * 9 + (S & 0xffff) * 3 + ((unsigned)S >> 16);
276 }
277 }
278
279 bit = (S & s->
and) | s->
or;
280 bit = ((S + bit) << s->
shift) - bit;
281
284
286 }
287
289 {
290 union {
291 float f;
294
295 unsigned int sign;
297
299 const int max_bits = 1 + 23 + 8 + 1;
301
303 return 0.0;
304 }
305
306 if (S) {
308 sign = S < 0;
309 if (sign)
311 if (S >= 0x1000000) {
314 else
315 S = 0;
316 exp = 255;
317 } else if (exp) {
320 if (exp <= shift)
321 shift = --exp;
323
324 if (shift) {
330 S |= (1 <<
shift) - 1;
334 }
335 }
336 } else {
338 }
339 S &= 0x7fffff;
340 } else {
341 sign = 0;
342 exp = 0;
349 } else {
352 }
353 }
354 }
355
356 *crc = *crc * 27 + S * 9 + exp * 3 + sign;
357
358 value.u = (sign << 31) | (exp << 23) |
S;
360 }
361
363 {
366 }
367
369 uint32_t crc_extra_bits)
370 {
374 }
378 }
379
380 return 0;
381 }
382
384 void *dst_l,
void *dst_r,
const int type)
385 {
387 int last, t;
390 uint32_t crc = s->
sc.
crc;
392 int16_t *dst16_l = dst_l;
393 int16_t *dst16_r = dst_r;
396 float *dstfl_l = dst_l;
397 float *dstfl_r = dst_r;
398
400 do {
402 if (last)
403 break;
405 if (last)
406 break;
407 for (i = 0; i < s->
terms; i++) {
409 if (t > 0) {
410 if (t > 8) {
411 if (t & 1) {
414 } else {
417 }
420 j = 0;
421 } else {
424 j = (pos + t) & 7;
425 }
429 } else {
432 }
433 if (A && L)
435 if (B && R)
439 } else if (t == -1) {
442 else
445 L = L2;
448 else
453 } else {
456 else
460
461 if (t == -3) {
464 }
465
468 else
471 L = L2;
473 }
474 }
475 pos = (pos + 1) & 7;
477 L += (R -= (L >> 1));
478 crc = (crc * 3 +
L) * 3 + R;
479
486 } else {
489 }
490 count++;
492
494
499 }
500
504
505 return 0;
506 }
507
509 void *dst,
const int type)
510 {
512 int last, t;
515 uint32_t crc = s->
sc.
crc;
517 int16_t *dst16 = dst;
519 float *dstfl = dst;
520
522 do {
524 S = 0;
525 if (last)
526 break;
527 for (i = 0; i < s->
terms; i++) {
529 if (t > 8) {
530 if (t & 1)
532 else
535 j = 0;
536 } else {
538 j = (pos + t) & 7;
539 }
542 else
544 if (A && T)
547 }
548 pos = (pos + 1) & 7;
550
555 } else {
557 }
558 count++;
560
562
566 }
567
572 }
573
574 return 0;
575 }
576
578 {
580 return -1;
581
584 return -1;
588
589 return 0;
590 }
591
593 {
596 return 0;
597 }
598
600 {
602
604
606
607 return 0;
608 }
609
611 {
613 int i;
614
618
619 return 0;
620 }
621
624 {
629 void *samples_l =
NULL, *samples_r =
NULL;
631 int got_terms = 0, got_weights = 0, got_samples = 0,
632 got_entropy = 0, got_bs = 0, got_float = 0, got_hybrid = 0;
633 int i, j,
id,
size, ssize, weights, t;
634 int bpp, chan = 0, chmask = 0, orig_bpp,
sample_rate = 0;
635 int multiblock;
636
640 }
641
642 s = wc->
fdec[block_no];
643 if (!s) {
645 block_no);
647 }
648
650 memset(s->
ch, 0,
sizeof(s->
ch));
654
656
657 s->
samples = bytestream2_get_le32(&gb);
662 }
667
676 s->
CRC = bytestream2_get_le32(&gb);
677
678 // parse metadata blocks
680 id = bytestream2_get_byte(&gb);
681 size = bytestream2_get_byte(&gb);
683 size |= (bytestream2_get_byte(&gb)) << 8;
684 size |= (bytestream2_get_byte(&gb)) << 16;
685 }
686 size <<= 1; // size is specified in words
689 size--;
690 if (size < 0) {
692 "Got incorrect block %02X with size %i\n", id, size);
693 break;
694 }
697 "Block size %i is out of bounds\n", size);
698 break;
699 }
706 continue;
707 }
709 for (i = 0; i < s->
terms; i++) {
713 }
714 got_terms = 1;
715 break;
717 if (!got_terms) {
719 continue;
720 }
725 continue;
726 }
727 for (i = 0; i < weights; i++) {
728 t = (int8_t)bytestream2_get_byte(&gb);
734 t = (int8_t)bytestream2_get_byte(&gb);
739 }
740 }
741 got_weights = 1;
742 break;
744 if (!got_terms) {
746 continue;
747 }
748 t = 0;
749 for (i = s->
terms - 1; (i >= 0) && (t < size); i--) {
752 wp_exp2(bytestream2_get_le16(&gb));
754 wp_exp2(bytestream2_get_le16(&gb));
755
758 wp_exp2(bytestream2_get_le16(&gb));
760 wp_exp2(bytestream2_get_le16(&gb));
761 t += 4;
762 }
763 t += 4;
766 wp_exp2(bytestream2_get_le16(&gb));
768 wp_exp2(bytestream2_get_le16(&gb));
769 t += 4;
770 } else {
773 wp_exp2(bytestream2_get_le16(&gb));
776 wp_exp2(bytestream2_get_le16(&gb));
777 }
778 }
780 }
781 }
782 got_samples = 1;
783 break;
787 "Entropy vars size should be %i, got %i.\n",
790 continue;
791 }
793 for (i = 0; i < 3; i++) {
795 }
796 got_entropy = 1;
797 break;
802 size -= 2;
803 }
804 }
805 for (i = 0; i < (s->
stereo_in + 1); i++) {
807 size -= 2;
808 }
809 if (size > 0) {
810 for (i = 0; i < (s->
stereo_in + 1); i++) {
812 wp_exp2((int16_t)bytestream2_get_le16(&gb));
813 }
814 } else {
817 }
818 got_hybrid = 1;
819 break;
822 if (size != 4) {
824 "Invalid INT32INFO, size = %i\n",
825 size);
827 continue;
828 }
830 if (val[0]) {
832 } else if (val[1]) {
834 } else if (val[2]) {
837 } else if (val[3]) {
840 }
841 /* original WavPack decoder forces 32-bit lossy sound to be treated
842 * as 24-bit one in order to have proper clipping */
848 }
849 break;
850 }
852 if (size != 4) {
854 "Invalid FLOATINFO, size = %i\n", size);
856 continue;
857 }
861 got_float = 1;
863 break;
871 got_bs = 1;
872 break;
874 if (size <= 4) {
876 size);
878 continue;
879 }
887 break;
889 if (size <= 1) {
891 "Insufficient channel information\n");
893 }
894 chan = bytestream2_get_byte(&gb);
895 switch (size - 2) {
896 case 0:
897 chmask = bytestream2_get_byte(&gb);
898 break;
899 case 1:
900 chmask = bytestream2_get_le16(&gb);
901 break;
902 case 2:
903 chmask = bytestream2_get_le24(&gb);
904 break;
905 case 3:
906 chmask = bytestream2_get_le32(&gb);
907 break;
908 case 5:
909 size = bytestream2_get_byte(&gb);
912 " instead of %i.\n", size, avctx->
channels);
913 chan |= (bytestream2_get_byte(&gb) & 0xF) << 8;
914 chmask = bytestream2_get_le16(&gb);
915 break;
916 default:
918 size);
921 }
922 break;
924 if (size != 3) {
927 }
928 sample_rate = bytestream2_get_le24(&gb);
929 break;
930 default:
932 }
933 if (id & WP_IDF_ODD)
935 }
936
937 if (!got_terms) {
940 }
941 if (!got_weights) {
944 }
945 if (!got_samples) {
948 }
949 if (!got_entropy) {
952 }
953 if (s->
hybrid && !got_hybrid) {
956 }
957 if (!got_bs) {
960 }
964 }
968 if (size < wanted) {
971 }
972 }
973
976 if (sr == 0xf) {
977 if (!sample_rate) {
980 }
982 } else
984
985 if (multiblock) {
986 if (chan)
988 if (chmask)
990 } else {
994 }
995
996 /* get output buffer */
1001 }
1002
1006 }
1007
1011
1013
1016 if (ret < 0)
1018 } else {
1020 if (ret < 0)
1022
1024 memcpy(samples_r, samples_l, bpp * s->
samples);
1025 }
1026
1027 return 0;
1028 }
1029
1031 {
1033 int i;
1034
1037 }
1038
1040 int *got_frame_ptr,
AVPacket *avpkt)
1041 {
1044 int buf_size = avpkt->
size;
1047
1050
1053
1054 /* determine number of samples */
1056 frame_flags =
AV_RL32(buf + 24);
1061 }
1062
1063 if (frame_flags & 0x80) {
1065 } else if ((frame_flags & 0x03) <= 1) {
1067 } else {
1070 }
1071
1072 while (buf_size > 0) {
1074 break;
1075 frame_size =
AV_RL32(buf + 4) - 12;
1076 buf += 20;
1077 buf_size -= 20;
1078 if (frame_size <= 0 || frame_size > buf_size) {
1080 "Block %d has invalid size (size %d vs. %d bytes left)\n",
1081 s->
block, frame_size, buf_size);
1084 }
1086 frame, buf, frame_size)) < 0) {
1089 }
1093 }
1094
1098 }
1099
1100 *got_frame_ptr = 1;
1101
1103 }
1104
1117 };