Debian Codegraph @

extern/adpcm/oki_codec.c

raw
1 /*
2 Encode and decode algorithms for
3 OKI ADPCM
4
5 Only difference between MSM6295 and MSM6258 is that the nibbles are swapped.
6 MSM6295 reads from MSB to LSB. MSM6258 reads from LSB to MSB.
7
8 Dialogic 'VOX' PCM reads from MSB to LSB, therefore should use the MSM6295 functions.
9
10 2019-2022 by superctr.
11 */
12
13 //#define OKI_HIGHPASS // Enable highpass filtering. Useful for files containing multiple samples.
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <math.h>
20
21 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
22
23 static const uint16_t oki_step_table[49] = {
24 16, 17, 19, 21, 23, 25, 28, 31,
25 34, 37, 41, 45, 50, 55, 60, 66,
26 73, 80, 88, 97, 107,118,130,143,
27 157,173,190,209,230,253,279,307,
28 337,371,408,449,494,544,598,658,
29 724,796,876,963,1060,1166,1282,1411,1552
30 };
31
32 static inline int16_t oki_step(uint8_t step, int16_t* history, uint8_t* step_hist)
33 {
34 static const int8_t delta_table[16] = {
35 1,3,5,7,9,11,13,15, -1,-3,-5,-7,-9,-11,-13,-15
36 };
37 static const int8_t adjust_table[8] = {
38 -1,-1,-1,-1,2,4,6,8
39 };
40
41 uint16_t step_size = oki_step_table[*step_hist];
42 int16_t delta = step_size >> 3;
43 if(step & 1)
44 delta += step_size >> 2;
45 if(step & 2)
46 delta += step_size >> 1;
47 if(step & 4)
48 delta += step_size;
49 if(step & 8)
50 delta = -delta;
51
52 #ifdef OKI_HIGHPASS
53 int32_t out = ((delta << 8) + (*history * 245)) >> 8;
54 #else
55 int16_t out = *history + delta;
56 #endif
57 *history = out = CLAMP(out, -2048, 2047); // Saturate output
58 int8_t adjusted_step = *step_hist + adjust_table[step & 7];
59 *step_hist = CLAMP(adjusted_step, 0, 48);
60
61 return out;
62 }
63
64 static inline uint8_t oki_encode_step(int16_t input, int16_t* history, uint8_t *step_hist)
65 {
66 int bit;
67 uint16_t step_size = oki_step_table[*step_hist];
68 int16_t delta = input - *history;
69 uint8_t adpcm_sample = (delta < 0) ? 8 : 0;
70 delta = abs(delta);
71 for(bit=3; bit--; )
72 {
73 if(delta >= step_size)
74 {
75 adpcm_sample |= (1 << bit);
76 delta -= step_size;
77 }
78 step_size >>= 1;
79 }
80 oki_step(adpcm_sample, history, step_hist);
81 return adpcm_sample;
82 }
83
84 void oki_encode(int16_t *buffer,uint8_t *outbuffer,long len)
85 {
86 long i;
87
88 int16_t history = 0;
89 uint8_t step_hist = 0;
90 uint8_t buf_sample = 0, nibble = 0;
91
92 for(i=0;i<len;i++)
93 {
94 int16_t sample = *buffer++;
95 if(sample < 0x7ff8) // round up
96 sample += 8;
97 sample >>= 4;
98 int step = oki_encode_step(sample, &history, &step_hist);
99 if(nibble)
100 *outbuffer++ = buf_sample | (step&15);
101 else
102 buf_sample = (step&15)<<4;
103 nibble^=1;
104 }
105 }
106
107 void oki_decode(uint8_t *buffer,int16_t *outbuffer,long len)
108 {
109 long i;
110
111 int16_t history = 0;
112 uint8_t step_hist = 0;
113 uint8_t nibble = 0;
114
115 for(i=0;i<len;i++)
116 {
117 int8_t step = (*(int8_t*)buffer) << nibble;
118 step >>= 4;
119 if(nibble)
120 buffer++;
121 nibble^=4;
122 *outbuffer++ = oki_step(step, &history, &step_hist) << 4;
123 }
124 }
125
126 void oki6258_encode(int16_t *buffer,uint8_t *outbuffer,long len)
127 {
128 long i;
129
130 int16_t history = 0;
131 uint8_t step_hist = 0;
132 uint8_t buf_sample = 0, nibble = 0;
133
134 for(i=0;i<len;i++)
135 {
136 int16_t sample = *buffer++;
137 if(sample < 0x7ff8) // round up
138 sample += 8;
139 sample >>= 4;
140 int step = oki_encode_step(sample, &history, &step_hist);
141 if(nibble)
142 *outbuffer++ = buf_sample | ((step&15)<<4);
143 else
144 buf_sample = step&15;
145 nibble^=1;
146 }
147 }
148
149 void oki6258_decode(uint8_t *buffer,int16_t *outbuffer,long len)
150 {
151 long i;
152
153 int16_t history = 0;
154 uint8_t step_hist = 0;
155 uint8_t nibble = 4;
156
157 for(i=0;i<len;i++)
158 {
159 int8_t step = (*(int8_t*)buffer) << nibble;
160 step >>= 4;
161 if(!nibble)
162 buffer++;
163 nibble^=4;
164 *outbuffer++ = oki_step(step, &history, &step_hist) << 4;
165 }
166 }