1 /*
2 * Monkey's Audio lossless audio decoder
3 * Copyright (c) 2007 Benjamin Zores <ben@geexbox.org>
4 * based upon libdemac from Dave Chapman.
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
23 #include <inttypes.h>
24
35
36 /**
37 * @file
38 * Monkey's Audio lossless audio decoder
39 */
40
41 #define MAX_CHANNELS 2
42 #define MAX_BYTESPERSAMPLE 3
43
44 #define APE_FRAMECODE_MONO_SILENCE 1
45 #define APE_FRAMECODE_STEREO_SILENCE 3
46 #define APE_FRAMECODE_PSEUDO_STEREO 4
47
48 #define HISTORY_SIZE 512
49 #define PREDICTOR_ORDER 8
50 /** Total size of all predictor histories */
51 #define PREDICTOR_SIZE 50
52
53 #define YDELAYA (18 + PREDICTOR_ORDER*4)
54 #define YDELAYB (18 + PREDICTOR_ORDER*3)
55 #define XDELAYA (18 + PREDICTOR_ORDER*2)
56 #define XDELAYB (18 + PREDICTOR_ORDER)
57
58 #define YADAPTCOEFFSA 18
59 #define XADAPTCOEFFSA 14
60 #define YADAPTCOEFFSB 10
61 #define XADAPTCOEFFSB 5
62
63 /**
64 * Possible compression levels
65 * @{
66 */
73 };
74 /** @} */
75
76 #define APE_FILTER_LEVELS 3
77
78 /** Filter orders depending on compression level */
80 { 0, 0, 0 },
81 { 16, 0, 0 },
82 { 64, 0, 0 },
83 { 32, 256, 0 },
84 { 16, 256, 1280 }
85 };
86
87 /** Filter fraction bits depending on compression level */
89 { 0, 0, 0 },
90 { 11, 0, 0 },
91 { 11, 0, 0 },
92 { 10, 13, 0 },
93 { 11, 13, 15 }
94 };
95
96
97 /** Filters applied to the decoded data */
99 int16_t *
coeffs;
///< actual coefficients used in filtering
100 int16_t *
adaptcoeffs;
///< adaptive filter coefficients used for correcting of actual filter coefficients
102 int16_t *
delay;
///< filtered values
103
106
111
113 uint32_t
low;
///< low end of interval
114 uint32_t
range;
///< length of interval
115 uint32_t
help;
///< bytes_to_follow resp. intermediate value
116 unsigned int buffer;
///< buffer for input/output
118
119 /** Filter histories */
122
124
127
131
134
135 /** Decoder context */
142 int samples;
///< samples left to decode in current frame
144
145 int fileversion;
///< codec version, very important in decoding process
147 int fset;
///< which filter set to use (calculated from compression level)
148 int flags;
///< global decoder flags
149
150 uint32_t
CRC;
///< frame CRC
153
158
160
166
171
173
179
182
192
199
201 {
203 int i;
204
207
211
212 return 0;
213 }
214
216 {
218 int i;
219
223 }
227 }
230 case 8:
232 break;
233 case 16:
235 break;
236 case 24:
238 break;
239 default:
241 "%d bits per coded sample", s->
bps);
243 }
249
258 }
262 break;
265 filter_alloc_fail);
266 }
267
280 } else {
283 }
284
291 } else {
294 }
295
299
300 return 0;
301 filter_alloc_fail:
304 }
305
306 /**
307 * @name APE range decoding functions
308 * @{
309 */
310
312 #define TOP_VALUE ((unsigned int)1 << (CODE_BITS-1))
313 #define SHIFT_BITS (CODE_BITS - 9)
314 #define EXTRA_BITS ((CODE_BITS-2) % 8 + 1)
315 #define BOTTOM_VALUE (TOP_VALUE >> 8)
316
317 /** Start the decoder */
319 {
323 }
324
325 /** Perform normalization */
327 {
333 } else {
335 }
338 }
339 }
340
341 /**
342 * Calculate culmulative frequency for next symbol. Does NO update!
343 * @param ctx decoder context
344 * @param tot_f is the total frequency or (code_value)1<<shift
345 * @return the culmulative frequency
346 */
348 {
352 }
353
354 /**
355 * Decode value with given size in bits
356 * @param ctx decoder context
357 * @param shift number of bits to decode
358 */
360 {
364 }
365
366
367 /**
368 * Update decoding state
369 * @param ctx decoder context
370 * @param sy_f the interval length (frequency of the symbol)
371 * @param lt_f the lower end (frequency sum of < symbols)
372 */
374 {
377 }
378
379 /** Decode n bits (n <= 16) without modelling */
381 {
384 return sym;
385 }
386
387
388 #define MODEL_ELEMENTS 64
389
390 /**
391 * Fixed probabilities for symbols in Monkey Audio version 3.97
392 */
394 0, 14824, 28224, 39348, 47855, 53994, 58171, 60926,
395 62682, 63786, 64463, 64878, 65126, 65276, 65365, 65419,
396 65450, 65469, 65480, 65487, 65491, 65493,
397 };
398
399 /**
400 * Probability ranges for symbols in Monkey Audio version 3.97
401 */
403 14824, 13400, 11124, 8507, 6139, 4177, 2755, 1756,
404 1104, 677, 415, 248, 150, 89, 54, 31,
405 19, 11, 7, 4, 2,
406 };
407
408 /**
409 * Fixed probabilities for symbols in Monkey Audio version 3.98
410 */
412 0, 19578, 36160, 48417, 56323, 60899, 63265, 64435,
413 64971, 65232, 65351, 65416, 65447, 65466, 65476, 65482,
414 65485, 65488, 65490, 65491, 65492, 65493,
415 };
416
417 /**
418 * Probability ranges for symbols in Monkey Audio version 3.98
419 */
421 19578, 16582, 12257, 7906, 4576, 2366, 1170, 536,
422 261, 119, 65, 31, 19, 10, 6, 3,
423 3, 2, 1, 1, 1,
424 };
425
426 /**
427 * Decode symbol
428 * @param ctx decoder context
429 * @param counts probability range start position
430 * @param counts_diff probability range widths
431 */
433 const uint16_t counts[],
434 const uint16_t counts_diff[])
435 {
436 int symbol, cf;
437
439
440 if(cf > 65492){
441 symbol= cf - 65535 + 63;
443 if(cf > 65535)
445 return symbol;
446 }
447 /* figure out the symbol inefficiently; a binary search would be much better */
448 for (symbol = 0; counts[symbol + 1] <= cf; symbol++);
449
451
452 return symbol;
453 }
454 /** @} */ // group rangecoder
455
457 {
458 int lim = rice->
k ? (1 << (rice->
k + 4)) : 0;
459 rice->
ksum += ((x + 1) / 2) - ((rice->
ksum + 16) >> 5);
460
461 if (rice->
ksum < lim)
463 else if (rice->
ksum >= (1 << (rice->
k + 5)))
465 }
466
468 {
469 unsigned int x;
470
472
473 if (k)
475
476 return x;
477 }
478
481 {
482 unsigned int x, overflow;
483
485
487 while (overflow >= 16) {
488 overflow -= 16;
490 }
491 }
492
494 x = overflow;
496 x = (overflow << rice->
k) +
get_bits(gb, rice->
k);
497 } else {
500 }
501 rice->
ksum += x - (rice->
ksum + 8 >> 4);
502 if (rice->
ksum < (rice->
k ? 1 << (rice->
k + 4) : 0))
504 else if (rice->
ksum >= (1 << (rice->
k + 5)) && rice->
k < 24)
506
507 /* Convert to signed */
508 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
509 }
510
512 {
513 unsigned int x, overflow;
514 int tmpk;
515
517
520 overflow = 0;
521 } else
522 tmpk = (rice->
k < 1) ? 0 : rice->
k - 1;
523
525 if (tmpk > 23) {
528 }
530 } else if (tmpk <= 31) {
533 } else {
536 }
537 x += overflow << tmpk;
538
540
541 /* Convert to signed */
542 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
543 }
544
546 {
547 unsigned int x, overflow;
548 int base, pivot;
549
550 pivot = rice->
ksum >> 5;
551 if (pivot == 0)
552 pivot = 1;
553
555
559 }
560
561 if (pivot < 0x10000) {
564 } else {
565 int base_hi = pivot, base_lo;
566 int bbits = 0;
567
568 while (base_hi & ~0xFFFF) {
569 base_hi >>= 1;
570 bbits++;
571 }
576
577 base = (base_hi << bbits) + base_lo;
578 }
579
580 x = base + overflow * pivot;
581
583
584 /* Convert to signed */
585 return ((x >> 1) ^ ((x & 1) - 1)) + 1;
586 }
587
590 {
591 int i;
592 int ksummax, ksummin;
593
595 for (i = 0; i < 5; i++) {
597 rice->
ksum += out[i];
598 }
601 return;
602 for (; i < 64; i++) {
604 rice->
ksum += out[i];
607 return;
608 }
609 ksummax = 1 << rice->
k + 7;
610 ksummin = rice->
k ? (1 << rice->
k + 6) : 0;
611 for (; i < blockstodecode; i++) {
613 rice->
ksum += out[i] - out[i - 64];
614 while (rice->
ksum < ksummin) {
616 ksummin = rice->
k ? ksummin >> 1 : 0;
617 ksummax >>= 1;
618 }
619 while (rice->
ksum >= ksummax) {
622 return;
623 ksummax <<= 1;
624 ksummin = ksummin ? ksummin << 1 : 128;
625 }
626 }
627
628 for (i = 0; i < blockstodecode; i++)
629 out[i] = ((out[i] >> 1) ^ ((out[i] & 1) - 1)) + 1;
630 }
631
633 {
635 blockstodecode);
636 }
637
639 {
641 blockstodecode);
643 blockstodecode);
644 }
645
647 {
649
650 while (blockstodecode--)
652 }
653
655 {
658 int blocks = blockstodecode;
659
660 while (blockstodecode--)
662 while (blocks--)
664 }
665
667 {
669
670 while (blockstodecode--)
672 }
673
675 {
678 int blocks = blockstodecode;
679
680 while (blockstodecode--)
683 // because of some implementation peculiarities we need to backpedal here
686 while (blocks--)
688 }
689
691 {
694
695 while (blockstodecode--) {
698 }
699 }
700
702 {
704
705 while (blockstodecode--)
707 }
708
710 {
713
714 while (blockstodecode--) {
717 }
718 }
719
721 {
722 /* Read the CRC */
726 ctx->
CRC = bytestream_get_be32(&ctx->
ptr);
727 } else {
729 }
730
731 /* Read the frame flags if they exist */
734 ctx->
CRC &= ~0x80000000;
735
739 }
740
741 /* Initialize the rice structs */
746
748 /* The first 8 bits of input are ignored. */
750
752 }
753
754 return 0;
755 }
756
758 375,
759 };
760
762 64, 115, 64,
763 };
764
766 740, 0
767 };
768
770 360, 317, -109, 98
771 };
772
774 {
776
777 /* Zero the history buffers */
780
781 /* Initialize and zero the coefficients */
788 } else {
793 }
794 } else {
797 }
804 }
805
809
811 }
812
813 /** Get inverse sign of integer (-1 for positive, 1 for negative and 0 for zero) */
815 return (x < 0) - (x > 0);
816 }
817
820 const int delayA)
821 {
823
829 }
830
831 predictionA = p->
buf[delayA] * 2 - p->
buf[delayA - 1];
833
834 if ((decoded ^ predictionA) > 0)
836 else
838
840
842 }
843
846 const int delayA, const int delayB,
848 {
849 int32_t predictionA, predictionB, sign;
851
859 return predictionA;
860 }
862 d1 = (p->
buf[delayA] - p->
buf[delayA - 1]) << 1;
863 d0 = p->
buf[delayA] + ((p->
buf[delayA - 2] - p->
buf[delayA - 1]) << 3);
864 d3 = p->
buf[delayB] * 2 - p->
buf[delayB - 1];
866
870
875
882
885
887 }
888
890 {
891 int i, j;
893 int32_t coeffs[256], delay[256];
894
895 memset(coeffs, 0, order * sizeof(*coeffs));
896 for (i = 0; i < order; i++)
897 delay[i] = buffer[i];
898 for (i = order; i <
length; i++) {
899 dotprod = 0;
901 for (j = 0; j < order; j++) {
902 dotprod += delay[j] * coeffs[j];
903 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
904 }
905 buffer[i] -= dotprod >>
shift;
906 for (j = 0; j < order - 1; j++)
907 delay[j] = delay[j + 1];
908 delay[order - 1] = buffer[i];
909 }
910 }
911
913 {
914 int i, j;
916 int32_t coeffs[8] = { 0 }, delay[8] = { 0 };
917
918 for (i = 0; i <
length; i++) {
919 dotprod = 0;
921 for (j = 7; j >= 0; j--) {
922 dotprod += delay[j] * coeffs[j];
923 coeffs[j] += ((delay[j] >> 31) | 1) * sign;
924 }
925 for (j = 7; j > 0; j--)
926 delay[j] = delay[j - 1];
927 delay[0] = buffer[i];
928 buffer[i] -= dotprod >> 9;
929 }
930 }
931
933 {
938
940 start = 16;
944 int order = 128,
shift2 = 11;
945
947 order <<= 1;
952 }
953 start = order;
956 }
957
958 while (count--) {
959 int X = *decoded0,
Y = *decoded1;
962 decoded0++;
964 decoded1++;
965 } else {
968 decoded0++;
971 decoded1++;
972 }
973
974 /* Combined */
977
978 /* Have we filled the history buffer? */
983 }
984 }
985 }
986
988 {
992
994 start = 16;
997 int order = 128,
shift2 = 11;
998
1000 order <<= 1;
1004 }
1005 start = order;
1007 }
1008
1009 while (count--) {
1012 decoded0++;
1013 } else {
1016 decoded0++;
1017 }
1018
1019 /* Combined */
1022
1023 /* Have we filled the history buffer? */
1028 }
1029 }
1030 }
1031
1034 const int delayA)
1035 {
1038
1040 d0 = p->
buf[delayA ];
1041 d1 = p->
buf[delayA ] - p->
buf[delayA - 1];
1042 d2 = p->
buf[delayA - 1] - p->
buf[delayA - 2];
1043 d3 = p->
buf[delayA - 2] - p->
buf[delayA - 3];
1044
1049
1052
1058
1060 }
1061
1063 {
1067
1069
1070 while (count--) {
1071 /* Predictor Y */
1072 int Y = *decoded1, X = *decoded0;
1074 decoded0++;
1076 decoded1++;
1077
1078 /* Combined */
1080
1081 /* Have we filled the history buffer? */
1086 }
1087 }
1088 }
1089
1091 {
1094
1096
1097 while (count--) {
1099 decoded0++;
1100
1102
1103 /* Have we filled the history buffer? */
1108 }
1109 }
1110 }
1111
1114 const int delayA, const int delayB,
1115 const int adaptA, const int adaptB)
1116 {
1117 int32_t predictionA, predictionB, sign;
1118
1121 p->
buf[delayA - 1] = p->
buf[delayA] - p->
buf[delayA - 1];
1123
1128
1129 /* Apply a scaled first-order filter compression */
1132 p->
buf[delayB - 1] = p->
buf[delayB] - p->
buf[delayB - 1];
1135
1141
1142 p->
lastA[
filter] = decoded + ((predictionA + (predictionB >> 1)) >> 10);
1144
1155
1157 }
1158
1160 {
1164
1166
1167 while (count--) {
1168 /* Predictor Y */
1171 decoded0++;
1174 decoded1++;
1175
1176 /* Combined */
1178
1179 /* Have we filled the history buffer? */
1184 }
1185 }
1186 }
1187
1189 {
1192 int32_t predictionA, currentA,
A, sign;
1193
1195
1196 currentA = p->
lastA[0];
1197
1198 while (count--) {
1199 A = *decoded0;
1200
1203
1208
1209 currentA = A + (predictionA >> 10);
1210
1213
1219
1221
1222 /* Have we filled the history buffer? */
1227 }
1228
1230 *(decoded0++) = p->
filterA[0];
1231 }
1232
1233 p->
lastA[0] = currentA;
1234 }
1235
1237 {
1242
1246 }
1247
1249 {
1252 }
1253
1256 {
1257 int res;
1258 int absres;
1259
1260 while (count--) {
1261 /* round fixedpoint scalar product */
1266 res = (res + (1 << (fracbits - 1))) >> fracbits;
1268 *data++ = res;
1269
1270 /* Update the output history */
1271 *f->
delay++ = av_clip_int16(res);
1272
1273 if (version < 3980) {
1274 /* Version ??? to < 3.98 files (untested) */
1275 f->
adaptcoeffs[0] = (res == 0) ? 0 : ((res >> 28) & 8) - 4;
1278 } else {
1279 /* Version 3.98 and later files */
1280
1281 /* Update the adaption coefficients */
1282 absres =
FFABS(res);
1283 if (absres)
1284 *f->
adaptcoeffs = ((res & (-1<<31)) ^ (-1<<30)) >>
1285 (25 + (absres <= f->
avg*3) + (absres <= f->avg*4/3));
1286 else
1288
1289 f->
avg += (absres - f->
avg) / 16;
1290
1294 }
1295
1297
1298 /* Have we filled the history buffer? */
1304 }
1305 }
1306 }
1307
1310 int count,
int order,
int fracbits)
1311 {
1313 if (data1)
1315 }
1316
1319 {
1320 int i;
1321
1324 break;
1328 }
1329 }
1330
1332 {
1337
1340 break;
1343 }
1344 return 0;
1345 }
1346
1348 {
1350 /* We are pure silence, so we're done. */
1352 return;
1353 }
1354
1356
1357 /* Now apply the predictor decoding */
1359
1360 /* Pseudo-stereo - just copy left channel to right channel */
1363 }
1364 }
1365
1367 {
1371
1373 /* We are pure silence, so we're done. */
1375 return;
1376 }
1377
1379
1380 /* Now apply the predictor decoding */
1382
1383 /* Decorrelate and scale to output depth */
1384 while (count--) {
1385 left = *decoded1 - (*decoded0 / 2);
1386 right = left + *decoded0;
1387
1388 *(decoded0++) = left;
1389 *(decoded1++) = right;
1390 }
1391 }
1392
1394 int *got_frame_ptr,
AVPacket *avpkt)
1395 {
1400 int16_t *sample16;
1403 int blockstodecode;
1404
1405 /* this should never be negative, but bad things will happen if it is, so
1406 check it just to make sure. */
1408
1410 uint32_t nblocks,
offset;
1411 int buf_size;
1412
1414 *got_frame_ptr = 0;
1415 return 0;
1416 }
1417 if (avpkt->
size < 8) {
1420 }
1421 buf_size = avpkt->
size & ~3;
1422 if (buf_size != avpkt->
size) {
1424 "extra bytes at the end will be skipped.\n");
1425 }
1426 if (s->
fileversion < 3950)
// previous versions overread two bytes
1427 buf_size += 2;
1432 buf_size >> 2);
1433 memset(s->
data + (buf_size & ~3), 0, buf_size & 3);
1436
1437 nblocks = bytestream_get_be32(&s->
ptr);
1438 offset = bytestream_get_be32(&s->
ptr);
1440 if (offset > 3) {
1444 }
1448 }
1450 } else {
1455 else
1457 }
1458
1459 if (!nblocks || nblocks > INT_MAX) {
1461 nblocks);
1463 }
1465
1466 /* Initialize the frame decoder */
1470 }
1471 }
1472
1474 *got_frame_ptr = 0;
1476 }
1477
1479 // for old files coefficients were not interleaved,
1480 // so we need to decode all of them at once
1483
1484 /* reallocate decoded sample buffer if needed */
1492
1493 /* get output buffer */
1497
1499
1502 else
1504 emms_c();
1505
1510 }
1511
1513 case 8:
1514 for (ch = 0; ch < s->
channels; ch++) {
1516 for (i = 0; i < blockstodecode; i++)
1517 *sample8++ = (s->
decoded[ch][i] + 0x80) & 0xff;
1518 }
1519 break;
1520 case 16:
1521 for (ch = 0; ch < s->
channels; ch++) {
1522 sample16 = (int16_t *)frame->
data[ch];
1523 for (i = 0; i < blockstodecode; i++)
1524 *sample16++ = s->
decoded[ch][i];
1525 }
1526 break;
1527 case 24:
1528 for (ch = 0; ch < s->
channels; ch++) {
1530 for (i = 0; i < blockstodecode; i++)
1531 *sample24++ = s->
decoded[ch][i] << 8;
1532 }
1533 break;
1534 }
1535
1537
1538 *got_frame_ptr = 1;
1539
1541 }
1542
1544 {
1547 }
1548
1549 #define OFFSET(x) offsetof(APEContext, x)
1550 #define PAR (AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM)
1553 {
"all",
"no maximum. decode all samples for each packet at once", 0,
AV_OPT_TYPE_CONST, { .i64 = INT_MAX }, INT_MIN, INT_MAX,
PAR,
"max_samples" },
1555 };
1556
1562 };
1563
1580 };