ADPCM Codecs
Loading...
Searching...
No Matches
adpcm.h
1#pragma once
2
3#include <assert.h>
4#include <stdint.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <limits.h>
9#include <new>
10#include "config-adpcm.h"
11
12// define some qulifiers used by the code base
13
14#define av_assert(X) assert(X)
15
16#define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
17#define FFMAX(a, b) ((a) > (b) ? (a) : (b))
18#define FFMIN(a, b) ((a) > (b) ? (b) : (a))
19#define FFSWAP(type, a, b) \
20 do { \
21 type SWAP_tmp = b; \
22 b = a; \
23 a = SWAP_tmp; \
24 } while (0)
25#define FFALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
26
27#define AVERROR(X) X
28
29// Some define used by logging
30#define AV_LOG_ERROR 16
31#define AV_LOG_WARNING 24
32
33#define av_log(A, B, ...) printf(__VA_ARGS__);
34
35#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
36
37#define AV_NUM_DATA_POINTERS 8
38
43enum AVCodecID {
44 /* various ADPCM codecs */
45 AV_CODEC_ID_ADPCM_IMA_QT = 0x11000,
46 AV_CODEC_ID_ADPCM_IMA_WAV,
47 AV_CODEC_ID_ADPCM_IMA_DK3,
48 AV_CODEC_ID_ADPCM_IMA_DK4,
49 AV_CODEC_ID_ADPCM_IMA_WS,
50 AV_CODEC_ID_ADPCM_IMA_SMJPEG,
51 AV_CODEC_ID_ADPCM_MS,
52 AV_CODEC_ID_ADPCM_4XM,
53 AV_CODEC_ID_ADPCM_XA,
54 AV_CODEC_ID_ADPCM_EA,
55 AV_CODEC_ID_ADPCM_CT,
56 AV_CODEC_ID_ADPCM_SWF,
57 AV_CODEC_ID_ADPCM_YAMAHA,
58 AV_CODEC_ID_ADPCM_SBPRO_4,
59 AV_CODEC_ID_ADPCM_SBPRO_3,
60 AV_CODEC_ID_ADPCM_SBPRO_2,
61 AV_CODEC_ID_ADPCM_THP,
62 AV_CODEC_ID_ADPCM_IMA_AMV,
63 AV_CODEC_ID_ADPCM_EA_R1,
64 AV_CODEC_ID_ADPCM_EA_R3,
65 AV_CODEC_ID_ADPCM_EA_R2,
66 AV_CODEC_ID_ADPCM_IMA_EA_SEAD,
67 AV_CODEC_ID_ADPCM_IMA_EA_EACS,
68 AV_CODEC_ID_ADPCM_EA_XAS,
69 AV_CODEC_ID_ADPCM_EA_MAXIS_XA,
70 AV_CODEC_ID_ADPCM_IMA_ISS,
71 AV_CODEC_ID_ADPCM_G722,
72 AV_CODEC_ID_ADPCM_IMA_APC,
73 AV_CODEC_ID_ADPCM_AFC,
74 AV_CODEC_ID_ADPCM_IMA_OKI,
75 AV_CODEC_ID_ADPCM_DTK,
76 AV_CODEC_ID_ADPCM_IMA_RAD,
77 AV_CODEC_ID_ADPCM_THP_LE,
78 AV_CODEC_ID_ADPCM_PSX,
79 AV_CODEC_ID_ADPCM_AICA,
80 AV_CODEC_ID_ADPCM_IMA_DAT4,
81 AV_CODEC_ID_ADPCM_MTAF,
82 AV_CODEC_ID_ADPCM_AGM,
83 AV_CODEC_ID_ADPCM_ARGO,
84 AV_CODEC_ID_ADPCM_IMA_SSI,
85 AV_CODEC_ID_ADPCM_ZORK,
86 AV_CODEC_ID_ADPCM_IMA_APM,
87 AV_CODEC_ID_ADPCM_IMA_ALP,
88 AV_CODEC_ID_ADPCM_IMA_MTF,
89 AV_CODEC_ID_ADPCM_IMA_CUNNING,
90 AV_CODEC_ID_ADPCM_IMA_MOFLEX,
91 AV_CODEC_ID_ADPCM_IMA_ACORN,
92 AV_CODEC_ID_ADPCM_XMD,
93};
94
95namespace adpcm_ffmpeg {
96
97enum av_errors {
98 AV_OK = 0,
99 AVERROR_INVALIDDATA,
100 AVERROR_PATCHWELCOME,
101 AVERROR_INVALID,
102 AVERROR_MEMORY,
103};
104
105
106
107enum AVSampleFormat {
108 AV_SAMPLE_FMT_S16,
109
110 AV_SAMPLE_FMT_S16P,
111
112 AV_SAMPLE_FMT_NB
114};
115
122struct AVPacket {
123 uint8_t *data;
124 int size;
125};
126
130struct AVFrame {
143 uint8_t *data[AV_NUM_DATA_POINTERS];
164 int16_t **extended_data;
165};
166
168 int nibble;
169 int prev;
170};
171
173 uint32_t ssd;
174 int path;
175 int sample1;
176 int sample2;
177 int step;
178};
179
186 int predictor;
187 int16_t step_index;
188 int step;
189 /* for encoding */
190 int prev_sample;
191
192 /* MS version */
193 int sample1;
194 int sample2;
195 int coeff1;
196 int coeff2;
197 int idelta;
198};
199
201 // AVClass *class;
202 int block_size;
203
204 ADPCMChannelStatus status[6];
205 TrellisPath *paths;
206 TrellisNode *node_buf;
207 TrellisNode **nodep_buf;
208 uint8_t *trellis_hash;
209};
210
216 int trellis;
222 uint8_t *extradata;
223 int extradata_size;
224 // Codec *codec;
225 enum AVCodecID codec_id;
226 void *priv_data;
227 int nb_channels;
228 // bits per sample/pixel from the demuxer
229 int bits_per_coded_sample;
230 int sample_rate;
231 int sample_fmt;
232};
233
239
240static const uint8_t ff_adpcm_ima_block_sizes[4] = {4, 12, 4, 20};
241static const uint8_t ff_adpcm_ima_block_samples[4] = {16, 32, 8, 32};
242
243/* ff_adpcm_step_table[] and ff_adpcm_index_table[] are from the ADPCM
244 reference source */
245static const int8_t ff_adpcm_index_table[16] = {
246 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8,
247};
248
253static const int16_t ff_adpcm_step_table[89] = {
254 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
255 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
256 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
257 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
258 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
259 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
260 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
261 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
262 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767};
263
264/* These are for MS-ADPCM */
265/* ff_adpcm_AdaptationTable[], ff_adpcm_AdaptCoeff1[], and
266 ff_adpcm_AdaptCoeff2[] are from libsndfile */
267static const int16_t ff_adpcm_AdaptationTable[] = {230, 230, 230, 230, 307, 409,
268 512, 614, 768, 614, 512, 409,
269 307, 230, 230, 230};
270
272static const uint8_t ff_adpcm_AdaptCoeff1[] = {64, 128, 0, 48, 60, 115, 98};
273
275static const int8_t ff_adpcm_AdaptCoeff2[] = {0, -64, 0, 16, 0, -52, -58};
276
277static const int16_t ff_adpcm_yamaha_indexscale[] = {
278 230, 230, 230, 230, 307, 409, 512, 614,
279 230, 230, 230, 230, 307, 409, 512, 614};
280
281static const int8_t ff_adpcm_yamaha_difflookup[] = {
282 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9, -11, -13, -15};
283
284/* These are for CD-ROM XA ADPCM */
285static const int8_t xa_adpcm_table[5][2] = {
286 {0, 0}, {60, 0}, {115, -52}, {98, -55}, {122, -60}};
287
288static const int16_t afc_coeffs[2][16] = {
289 {0, 2048, 0, 1024, 4096, 3584, 3072, 4608, 4200, 4800, 5120, 2048, 1024,
290 -1024, -1024, -2048},
291 {0, 0, 2048, 1024, -2048, -1536, -1024, -2560, -2248, -2300, -3072, -2048,
292 -1024, 1024, 0, 0}};
293
294static const int16_t ea_adpcm_table[] = {0, 240, 460, 392, 0, 0, -208,
295 -220, 0, 1, 3, 4, 7, 8,
296 10, 11, 0, -1, -3, -4};
297
298/*
299 * Dumped from the binaries:
300 * - FantasticJourney.exe - 0x794D2, DGROUP:0x47A4D2
301 * - BigRaceUSA.exe - 0x9B8AA, DGROUP:0x49C4AA
302 * - Timeshock!.exe - 0x8506A, DGROUP:0x485C6A
303 */
304static const int8_t ima_cunning_index_table[9] = {-1, -1, -1, -1, 1,
305 2, 3, 4, -1};
306
307/*
308 * Dumped from the binaries:
309 * - FantasticJourney.exe - 0x79458, DGROUP:0x47A458
310 * - BigRaceUSA.exe - 0x9B830, DGROUP:0x49C430
311 * - Timeshock!.exe - 0x84FF0, DGROUP:0x485BF0
312 */
313static const int16_t ima_cunning_step_table[61] = {
314 1, 1, 1, 1, 2, 2, 3, 3, 4, 5, 6,
315 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40,
316 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
317 320, 384, 448, 512, 640, 768, 896, 1024, 1280, 1536, 1792,
318 2048, 2560, 3072, 3584, 4096, 5120, 6144, 7168, 8192, 10240, 12288,
319 14336, 16384, 20480, 24576, 28672, 0};
320
321static const int8_t adpcm_index_table2[4] = {
322 -1,
323 2,
324 -1,
325 2,
326};
327
328static const int8_t adpcm_index_table3[8] = {
329 -1, -1, 1, 2, -1, -1, 1, 2,
330};
331
332static const int8_t adpcm_index_table5[32] = {
333 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
334 -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16,
335};
336
337static const int8_t *const adpcm_index_tables[4] = {
338 &adpcm_index_table2[0],
339 &adpcm_index_table3[0],
340 &ff_adpcm_index_table[0],
341 &adpcm_index_table5[0],
342};
343
344static const int16_t mtaf_stepsize[32][16] = {
345 {
346 1,
347 5,
348 9,
349 13,
350 16,
351 20,
352 24,
353 28,
354 -1,
355 -5,
356 -9,
357 -13,
358 -16,
359 -20,
360 -24,
361 -28,
362 },
363 {
364 2,
365 6,
366 11,
367 15,
368 20,
369 24,
370 29,
371 33,
372 -2,
373 -6,
374 -11,
375 -15,
376 -20,
377 -24,
378 -29,
379 -33,
380 },
381 {
382 2,
383 7,
384 13,
385 18,
386 23,
387 28,
388 34,
389 39,
390 -2,
391 -7,
392 -13,
393 -18,
394 -23,
395 -28,
396 -34,
397 -39,
398 },
399 {
400 3,
401 9,
402 15,
403 21,
404 28,
405 34,
406 40,
407 46,
408 -3,
409 -9,
410 -15,
411 -21,
412 -28,
413 -34,
414 -40,
415 -46,
416 },
417 {
418 3,
419 11,
420 18,
421 26,
422 33,
423 41,
424 48,
425 56,
426 -3,
427 -11,
428 -18,
429 -26,
430 -33,
431 -41,
432 -48,
433 -56,
434 },
435 {
436 4,
437 13,
438 22,
439 31,
440 40,
441 49,
442 58,
443 67,
444 -4,
445 -13,
446 -22,
447 -31,
448 -40,
449 -49,
450 -58,
451 -67,
452 },
453 {
454 5,
455 16,
456 26,
457 37,
458 48,
459 59,
460 69,
461 80,
462 -5,
463 -16,
464 -26,
465 -37,
466 -48,
467 -59,
468 -69,
469 -80,
470 },
471 {
472 6,
473 19,
474 31,
475 44,
476 57,
477 70,
478 82,
479 95,
480 -6,
481 -19,
482 -31,
483 -44,
484 -57,
485 -70,
486 -82,
487 -95,
488 },
489 {
490 7,
491 22,
492 38,
493 53,
494 68,
495 83,
496 99,
497 114,
498 -7,
499 -22,
500 -38,
501 -53,
502 -68,
503 -83,
504 -99,
505 -114,
506 },
507 {
508 9,
509 27,
510 45,
511 63,
512 81,
513 99,
514 117,
515 135,
516 -9,
517 -27,
518 -45,
519 -63,
520 -81,
521 -99,
522 -117,
523 -135,
524 },
525 {
526 10,
527 32,
528 53,
529 75,
530 96,
531 118,
532 139,
533 161,
534 -10,
535 -32,
536 -53,
537 -75,
538 -96,
539 -118,
540 -139,
541 -161,
542 },
543 {
544 12,
545 38,
546 64,
547 90,
548 115,
549 141,
550 167,
551 193,
552 -12,
553 -38,
554 -64,
555 -90,
556 -115,
557 -141,
558 -167,
559 -193,
560 },
561 {
562 15,
563 45,
564 76,
565 106,
566 137,
567 167,
568 198,
569 228,
570 -15,
571 -45,
572 -76,
573 -106,
574 -137,
575 -167,
576 -198,
577 -228,
578 },
579 {
580 18,
581 54,
582 91,
583 127,
584 164,
585 200,
586 237,
587 273,
588 -18,
589 -54,
590 -91,
591 -127,
592 -164,
593 -200,
594 -237,
595 -273,
596 },
597 {
598 21,
599 65,
600 108,
601 152,
602 195,
603 239,
604 282,
605 326,
606 -21,
607 -65,
608 -108,
609 -152,
610 -195,
611 -239,
612 -282,
613 -326,
614 },
615 {
616 25,
617 77,
618 129,
619 181,
620 232,
621 284,
622 336,
623 388,
624 -25,
625 -77,
626 -129,
627 -181,
628 -232,
629 -284,
630 -336,
631 -388,
632 },
633 {
634 30,
635 92,
636 153,
637 215,
638 276,
639 338,
640 399,
641 461,
642 -30,
643 -92,
644 -153,
645 -215,
646 -276,
647 -338,
648 -399,
649 -461,
650 },
651 {
652 36,
653 109,
654 183,
655 256,
656 329,
657 402,
658 476,
659 549,
660 -36,
661 -109,
662 -183,
663 -256,
664 -329,
665 -402,
666 -476,
667 -549,
668 },
669 {
670 43,
671 130,
672 218,
673 305,
674 392,
675 479,
676 567,
677 654,
678 -43,
679 -130,
680 -218,
681 -305,
682 -392,
683 -479,
684 -567,
685 -654,
686 },
687 {
688 52,
689 156,
690 260,
691 364,
692 468,
693 572,
694 676,
695 780,
696 -52,
697 -156,
698 -260,
699 -364,
700 -468,
701 -572,
702 -676,
703 -780,
704 },
705 {
706 62,
707 186,
708 310,
709 434,
710 558,
711 682,
712 806,
713 930,
714 -62,
715 -186,
716 -310,
717 -434,
718 -558,
719 -682,
720 -806,
721 -930,
722 },
723 {
724 73,
725 221,
726 368,
727 516,
728 663,
729 811,
730 958,
731 1106,
732 -73,
733 -221,
734 -368,
735 -516,
736 -663,
737 -811,
738 -958,
739 -1106,
740 },
741 {
742 87,
743 263,
744 439,
745 615,
746 790,
747 966,
748 1142,
749 1318,
750 -87,
751 -263,
752 -439,
753 -615,
754 -790,
755 -966,
756 -1142,
757 -1318,
758 },
759 {
760 104,
761 314,
762 523,
763 733,
764 942,
765 1152,
766 1361,
767 1571,
768 -104,
769 -314,
770 -523,
771 -733,
772 -942,
773 -1152,
774 -1361,
775 -1571,
776 },
777 {
778 124,
779 374,
780 623,
781 873,
782 1122,
783 1372,
784 1621,
785 1871,
786 -124,
787 -374,
788 -623,
789 -873,
790 -1122,
791 -1372,
792 -1621,
793 -1871,
794 },
795 {
796 148,
797 445,
798 743,
799 1040,
800 1337,
801 1634,
802 1932,
803 2229,
804 -148,
805 -445,
806 -743,
807 -1040,
808 -1337,
809 -1634,
810 -1932,
811 -2229,
812 },
813 {
814 177,
815 531,
816 885,
817 1239,
818 1593,
819 1947,
820 2301,
821 2655,
822 -177,
823 -531,
824 -885,
825 -1239,
826 -1593,
827 -1947,
828 -2301,
829 -2655,
830 },
831 {
832 210,
833 632,
834 1053,
835 1475,
836 1896,
837 2318,
838 2739,
839 3161,
840 -210,
841 -632,
842 -1053,
843 -1475,
844 -1896,
845 -2318,
846 -2739,
847 -3161,
848 },
849 {
850 251,
851 753,
852 1255,
853 1757,
854 2260,
855 2762,
856 3264,
857 3766,
858 -251,
859 -753,
860 -1255,
861 -1757,
862 -2260,
863 -2762,
864 -3264,
865 -3766,
866 },
867 {
868 299,
869 897,
870 1495,
871 2093,
872 2692,
873 3290,
874 3888,
875 4486,
876 -299,
877 -897,
878 -1495,
879 -2093,
880 -2692,
881 -3290,
882 -3888,
883 -4486,
884 },
885 {
886 356,
887 1068,
888 1781,
889 2493,
890 3206,
891 3918,
892 4631,
893 5343,
894 -356,
895 -1068,
896 -1781,
897 -2493,
898 -3206,
899 -3918,
900 -4631,
901 -5343,
902 },
903 {
904 424,
905 1273,
906 2121,
907 2970,
908 3819,
909 4668,
910 5516,
911 6365,
912 -424,
913 -1273,
914 -2121,
915 -2970,
916 -3819,
917 -4668,
918 -5516,
919 -6365,
920 },
921};
922
923static const int16_t oki_step_table[49] = {
924 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50,
925 55, 60, 66, 73, 80, 88, 97, 107, 118, 130, 143, 157, 173,
926 190, 209, 230, 253, 279, 307, 337, 371, 408, 449, 494, 544, 598,
927 658, 724, 796, 876, 963, 1060, 1166, 1282, 1411, 1552};
928
929// padded to zero where table size is less then 16
930static const int8_t swf_index_tables[4][16] = {
931 /*2*/ {-1, 2},
932 /*3*/ {-1, -1, 2, 4},
933 /*4*/ {-1, -1, -1, -1, 2, 4, 6, 8},
934 /*5*/ {-1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16}};
935
936static const int8_t zork_index_table[8] = {
937 -1, -1, -1, 1, 4, 7, 10, 12,
938};
939
940static const int8_t mtf_index_table[16] = {
941 8, 6, 4, 2, -1, -1, -1, -1, -1, -1, -1, -1, 2, 4, 6, 8,
942};
943
944/* end of tables */
945
946inline int av_clip(int a, int amin, int amax) {
947 // #if defined(HAVE_AV_CONFIG_H) && defined(ASSERT_LEVEL) && ASSERT_LEVEL >=
948 // 2
949 assert(amin <= amax);
950 // #endif
951 if (a < amin)
952 return amin;
953 else if (a > amax)
954 return amax;
955 else
956 return a;
957}
958
962template <typename T>
963inline bool allocArray(T *&ptr, size_t n) {
964 ptr = new T[n];
965 return ptr != nullptr;
966}
967
969template <typename T>
970inline void freeArray(T *&ptr) {
971 delete[] ptr;
972 ptr = nullptr;
973}
974
975
981inline int16_t av_clip_int16(int a) {
982 if ((a + 0x8000U) & ~0xFFFF)
983 return (a >> 31) ^ 0x7FFF;
984 else
985 return a;
986}
987
994inline int av_clip_intp2(int a, int p) {
995 if (((unsigned)a + (1 << p)) & ~((2 << p) - 1))
996 return (a >> 31) ^ ((1 << p) - 1);
997 else
998 return a;
999}
1000
1007inline unsigned av_clip_uintp2(int a, int p) {
1008 if (a & ~((1 << p) - 1))
1009 return (~a) >> 31 & ((1 << p) - 1);
1010 else
1011 return a;
1012}
1013
1021inline unsigned av_mod_uintp2(unsigned a, unsigned p) {
1022 return a & ((1U << p) - 1);
1023}
1024
1025#ifndef sign_extend
1026inline int sign_extend(int val, unsigned bits) {
1027 unsigned shift = 8 * sizeof(int) - bits;
1028 union {
1029 unsigned u;
1030 int s;
1031 } v = {(unsigned)val << shift};
1032 return v.s >> shift;
1033}
1034#endif
1035
1036
1037
1038
1039}
Definition adpcm.h:185
Definition adpcm.h:234
int vqa_version
Definition adpcm.h:236
int has_status
Definition adpcm.h:237
Definition adpcm.h:200
C API context for encoder and decoder.
Definition adpcm.h:215
int frame_size
Number of samples per channel in an audio frame.
Definition adpcm.h:218
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs.
Definition adpcm.h:221
This structure provides the uncompressed PCM data.
Definition adpcm.h:130
int16_t ** extended_data
Definition adpcm.h:164
int nb_samples
Definition adpcm.h:148
uint8_t * data[AV_NUM_DATA_POINTERS]
Definition adpcm.h:143
This structure stores compressed data. It is typically exported by demuxers and then passed as input ...
Definition adpcm.h:122
Definition adpcm.h:172
Definition adpcm.h:167