Debian Codegraph @

extern/adpcm/ymz_codec.c

raw
1 /*
2 Encode and decode algorithms for
3 YMZ280B / AICA ADPCM.
4
5 The only difference between YMZ280B and AICA ADPCM is that the nibbles are swapped.
6
7 2019 by superctr.
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <math.h>
15
16 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
17
18 static inline int16_t ymz_step(uint8_t step, int16_t* history, int16_t* step_size)
19 {
20 static const int step_table[8] = {
21 230, 230, 230, 230, 307, 409, 512, 614
22 };
23
24 int sign = step & 8;
25 int delta = step & 7;
26 int diff = ((1+(delta<<1)) * *step_size) >> 3;
27 int newval = *history;
28 int nstep = (step_table[delta] * *step_size) >> 8;
29 // Only found in the official AICA encoder
30 // but it's possible all chips (including ADPCM-B) does this.
31 diff = CLAMP(diff, 0, 32767);
32 if (sign > 0)
33 newval -= diff;
34 else
35 newval += diff;
36 //*step_size = CLAMP(nstep, 511, 32767);
37 *step_size = CLAMP(nstep, 127, 24576);
38 *history = newval = CLAMP(newval, -32768, 32767);
39 return newval;
40 }
41
42 void ymz_encode(int16_t *buffer,uint8_t *outbuffer,long len)
43 {
44 long i;
45 int16_t step_size = 127;
46 int16_t history = 0;
47 uint8_t buf_sample = 0, nibble = 0;
48 unsigned int adpcm_sample;
49
50 for(i=0;i<len;i++)
51 {
52 // we remove a few bits of accuracy to reduce some noise.
53 int step = ((*buffer++) & -8) - history;
54 adpcm_sample = (abs(step)<<16) / (step_size<<14);
55 adpcm_sample = CLAMP(adpcm_sample, 0, 7);
56 if(step < 0)
57 adpcm_sample |= 8;
58 if(nibble)
59 *outbuffer++ = buf_sample | (adpcm_sample&15);
60 else
61 buf_sample = (adpcm_sample&15)<<4;
62 nibble^=1;
63 ymz_step(adpcm_sample, &history, &step_size);
64 }
65 }
66
67 void ymz_decode(uint8_t *buffer,int16_t *outbuffer,long len)
68 {
69 long i;
70
71 int16_t step_size = 127;
72 int16_t history = 0;
73 uint8_t nibble = 0;
74
75 for(i=0;i<len;i++)
76 {
77 int8_t step = (*(int8_t*)buffer)<<nibble;
78 step >>= 4;
79 if(nibble)
80 buffer++;
81 nibble^=4;
82 history = history * 254 / 256; // High pass
83 *outbuffer++ = ymz_step(step, &history, &step_size);
84 }
85 }
86
87 void aica_encode(int16_t *buffer,uint8_t *outbuffer,long len)
88 {
89 long i;
90 int16_t step_size = 127;
91 int16_t history = 0;
92 uint8_t buf_sample = 0, nibble = 0;
93 unsigned int adpcm_sample;
94
95 for(i=0;i<len;i++)
96 {
97 // we remove a few bits of accuracy to reduce some noise.
98 int step = ((*buffer++) & -8) - history;
99 adpcm_sample = (abs(step)<<16) / (step_size<<14);
100 adpcm_sample = CLAMP(adpcm_sample, 0, 7);
101 if(step < 0)
102 adpcm_sample |= 8;
103 if(!nibble)
104 *outbuffer++ = buf_sample | (adpcm_sample<<4);
105 else
106 buf_sample = (adpcm_sample&15);
107 nibble^=1;
108 ymz_step(adpcm_sample, &history, &step_size);
109 }
110 }
111
112 void aica_decode(uint8_t *buffer,int16_t *outbuffer,long len)
113 {
114 long i;
115
116 int16_t step_size = 127;
117 int16_t history = 0;
118 uint8_t nibble = 4;
119
120 for(i=0;i<len;i++)
121 {
122 int8_t step = (*(int8_t*)buffer)<<nibble;
123 step >>= 4;
124 if(!nibble)
125 buffer++;
126 nibble^=4;
127 history = history * 254 / 256; // High pass
128 *outbuffer++ = ymz_step(step, &history, &step_size);
129 }
130 }