Debian Codegraph @

android/app/jni/extern/adpcm-xq-s/adpcm-lib.c

raw
1 ////////////////////////////////////////////////////////////////////////////
2 // **** ADPCM-XQ **** //
3 // Xtreme Quality ADPCM Encoder/Decoder //
4 // Copyright (c) 2022 David Bryant. //
5 // All Rights Reserved. //
6 // Distributed under the BSD Software License (see license.txt) //
7 ////////////////////////////////////////////////////////////////////////////
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "adpcm-lib.h"
13
14 /* This module encodes and decodes 4-bit ADPCM (DVI/IMA varient). ADPCM data is divided
15 * into independently decodable blocks that can be relatively small. The most common
16 * configuration is to store 505 samples into a 256 byte block, although other sizes are
17 * permitted as long as the number of samples is one greater than a multiple of 8. When
18 * multiple channels are present, they are interleaved in the data with an 8-sample
19 * interval.
20 */
21
22 /********************************* 4-bit ADPCM encoder ********************************/
23
24 #define CLIP(data, min, max) \
25 if ((data) > (max)) data = max; \
26 else if ((data) < (min)) data = min;
27
28 /* step table */
29 static const uint16_t step_table[89] = {
30 7, 8, 9, 10, 11, 12, 13, 14,
31 16, 17, 19, 21, 23, 25, 28, 31,
32 34, 37, 41, 45, 50, 55, 60, 66,
33 73, 80, 88, 97, 107, 118, 130, 143,
34 157, 173, 190, 209, 230, 253, 279, 307,
35 337, 371, 408, 449, 494, 544, 598, 658,
36 724, 796, 876, 963, 1060, 1166, 1282, 1411,
37 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024,
38 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484,
39 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
40 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794,
41 32767
42 };
43
44 /* step index tables */
45 static const int index_table[] = {
46 /* adpcm data size is 4 */
47 -1, -1, -1, -1, 2, 4, 6, 8
48 };
49
50 struct adpcm_channel {
51 int32_t pcmdata; // current PCM value
52 int32_t error, weight, history [2]; // for noise shaping
53 int8_t index; // current index into step size table
54 };
55
56 struct adpcm_context {
57 struct adpcm_channel channels [2];
58 int num_channels, lookahead, noise_shaping;
59 };
60
61 /* Create ADPCM encoder context with given number of channels.
62 * The returned pointer is used for subsequent calls. Note that
63 * even though an ADPCM encoder could be set up to encode frames
64 * independently, we use a context so that we can use previous
65 * data to improve quality (this encoder might not be optimal
66 * for encoding independent frames).
67 */
68
69 void *adpcm_create_context (int num_channels, int lookahead, int noise_shaping, int32_t initial_deltas [2])
70 {
71 struct adpcm_context *pcnxt = malloc (sizeof (struct adpcm_context));
72 int ch, i;
73
74 memset (pcnxt, 0, sizeof (struct adpcm_context));
75 pcnxt->noise_shaping = noise_shaping;
76 pcnxt->num_channels = num_channels;
77 pcnxt->lookahead = lookahead;
78
79 // given the supplied initial deltas, search for and store the closest index
80
81 for (ch = 0; ch < num_channels; ++ch)
82 for (i = 0; i <= 88; i++)
83 if (i == 88 || initial_deltas [ch] < ((int32_t) step_table [i] + step_table [i+1]) / 2) {
84 pcnxt->channels [ch].index = i;
85 break;
86 }
87
88 return pcnxt;
89 }
90
91 /* Free the ADPCM encoder context.
92 */
93
94 void adpcm_free_context (void *p)
95 {
96 struct adpcm_context *pcnxt = (struct adpcm_context *) p;
97
98 free (pcnxt);
99 }
100
101 static void set_decode_parameters (struct adpcm_context *pcnxt, int32_t *init_pcmdata, int8_t *init_index)
102 {
103 int ch;
104
105 for (ch = 0; ch < pcnxt->num_channels; ch++) {
106 pcnxt->channels[ch].pcmdata = init_pcmdata[ch];
107 pcnxt->channels[ch].index = init_index[ch];
108 }
109 }
110
111 static void get_decode_parameters (struct adpcm_context *pcnxt, int32_t *init_pcmdata, int8_t *init_index)
112 {
113 int ch;
114
115 for (ch = 0; ch < pcnxt->num_channels; ch++) {
116 init_pcmdata[ch] = pcnxt->channels[ch].pcmdata;
117 init_index[ch] = pcnxt->channels[ch].index;
118 }
119 }
120
121 static double minimum_error (const struct adpcm_channel *pchan, int nch, int32_t csample, const int16_t *sample, int depth, int *best_nibble)
122 {
123 int32_t delta = csample - pchan->pcmdata;
124 struct adpcm_channel chan = *pchan;
125 uint16_t step = step_table[chan.index];
126 uint16_t trial_delta = (step >> 3);
127 int nibble, nibble2;
128 double min_error;
129
130 if (delta < 0) {
131 int mag = (-delta << 2) / step;
132 nibble = 0x8 | (mag > 7 ? 7 : mag);
133 }
134 else {
135 int mag = (delta << 2) / step;
136 nibble = mag > 7 ? 7 : mag;
137 }
138
139 if (nibble & 1) trial_delta += (step >> 2);
140 if (nibble & 2) trial_delta += (step >> 1);
141 if (nibble & 4) trial_delta += step;
142
143 if (nibble & 8)
144 chan.pcmdata -= trial_delta;
145 else
146 chan.pcmdata += trial_delta;
147
148 CLIP(chan.pcmdata, -32768, 32767);
149 if (best_nibble) *best_nibble = nibble;
150 min_error = (double) (chan.pcmdata - csample) * (chan.pcmdata - csample);
151
152 if (depth) {
153 chan.index += index_table[nibble & 0x07];
154 CLIP(chan.index, 0, 88);
155 min_error += minimum_error (&chan, nch, sample [nch], sample + nch, depth - 1, NULL);
156 }
157 else
158 return min_error;
159
160 for (nibble2 = 0; nibble2 <= 0xF; ++nibble2) {
161 double error;
162
163 if (nibble2 == nibble)
164 continue;
165
166 chan = *pchan;
167 trial_delta = (step >> 3);
168
169 if (nibble2 & 1) trial_delta += (step >> 2);
170 if (nibble2 & 2) trial_delta += (step >> 1);
171 if (nibble2 & 4) trial_delta += step;
172
173 if (nibble2 & 8)
174 chan.pcmdata -= trial_delta;
175 else
176 chan.pcmdata += trial_delta;
177
178 CLIP(chan.pcmdata, -32768, 32767);
179
180 error = (double) (chan.pcmdata - csample) * (chan.pcmdata - csample);
181
182 if (error < min_error) {
183 chan.index += index_table[nibble2 & 0x07];
184 CLIP(chan.index, 0, 88);
185 error += minimum_error (&chan, nch, sample [nch], sample + nch, depth - 1, NULL);
186
187 if (error < min_error) {
188 if (best_nibble) *best_nibble = nibble2;
189 min_error = error;
190 }
191 }
192 }
193
194 return min_error;
195 }
196
197 static uint8_t encode_sample (struct adpcm_context *pcnxt, int ch, const int16_t *sample, int num_samples)
198 {
199 struct adpcm_channel *pchan = pcnxt->channels + ch;
200 int32_t csample = *sample;
201 int depth = num_samples - 1, nibble;
202 uint16_t step = step_table[pchan->index];
203 uint16_t trial_delta = (step >> 3);
204
205 if (pcnxt->noise_shaping == NOISE_SHAPING_DYNAMIC) {
206 int32_t sam = (3 * pchan->history [0] - pchan->history [1]) >> 1;
207 int32_t temp = csample - (((pchan->weight * sam) + 512) >> 10);
208 int32_t shaping_weight;
209
210 if (sam && temp) pchan->weight -= (((sam ^ temp) >> 29) & 4) - 2;
211 pchan->history [1] = pchan->history [0];
212 pchan->history [0] = csample;
213
214 shaping_weight = (pchan->weight < 256) ? 1024 : 1536 - (pchan->weight * 2);
215 temp = -((shaping_weight * pchan->error + 512) >> 10);
216
217 if (shaping_weight < 0 && temp) {
218 if (temp == pchan->error)
219 temp = (temp < 0) ? temp + 1 : temp - 1;
220
221 pchan->error = -csample;
222 csample += temp;
223 }
224 else
225 pchan->error = -(csample += temp);
226 }
227 else if (pcnxt->noise_shaping == NOISE_SHAPING_STATIC)
228 pchan->error = -(csample -= pchan->error);
229
230 if (depth > pcnxt->lookahead)
231 depth = pcnxt->lookahead;
232
233 minimum_error (pchan, pcnxt->num_channels, csample, sample, depth, &nibble);
234
235 if (nibble & 1) trial_delta += (step >> 2);
236 if (nibble & 2) trial_delta += (step >> 1);
237 if (nibble & 4) trial_delta += step;
238
239 if (nibble & 8)
240 pchan->pcmdata -= trial_delta;
241 else
242 pchan->pcmdata += trial_delta;
243
244 pchan->index += index_table[nibble & 0x07];
245 CLIP(pchan->index, 0, 88);
246 CLIP(pchan->pcmdata, -32768, 32767);
247
248 if (pcnxt->noise_shaping)
249 pchan->error += pchan->pcmdata;
250
251 return nibble;
252 }
253
254 static void encode_chunks (struct adpcm_context *pcnxt, uint8_t **outbuf, size_t *outbufsize, const int16_t **inbuf, int inbufcount)
255 {
256 const int16_t *pcmbuf;
257 int chunks, ch, i;
258
259 chunks = (inbufcount - 1) / 8;
260 *outbufsize += (chunks * 4) * pcnxt->num_channels;
261
262 while (chunks--)
263 {
264 for (ch = 0; ch < pcnxt->num_channels; ch++)
265 {
266 pcmbuf = *inbuf + ch;
267
268 for (i = 0; i < 4; i++) {
269 **outbuf = encode_sample (pcnxt, ch, pcmbuf, chunks * 8 + (3 - i) * 2 + 2);
270 pcmbuf += pcnxt->num_channels;
271 **outbuf |= encode_sample (pcnxt, ch, pcmbuf, chunks * 8 + (3 - i) * 2 + 1) << 4;
272 pcmbuf += pcnxt->num_channels;
273 (*outbuf)++;
274 }
275 }
276
277 *inbuf += 8 * pcnxt->num_channels;
278 }
279 }
280
281 /* Encode a block of 16-bit PCM data into 4-bit ADPCM.
282 *
283 * Parameters:
284 * p the context returned by adpcm_begin()
285 * outbuf destination buffer
286 * outbufsize pointer to variable where the number of bytes written
287 * will be stored
288 * inbuf source PCM samples
289 * inbufcount number of composite PCM samples provided (note: this is
290 * the total number of 16-bit samples divided by the number
291 * of channels)
292 *
293 * Returns 1 (for success as there is no error checking)
294 */
295
296 int adpcm_encode_block (void *p, uint8_t *outbuf, size_t *outbufsize, const int16_t *inbuf, int inbufcount)
297 {
298 struct adpcm_context *pcnxt = (struct adpcm_context *) p;
299 int32_t init_pcmdata[2];
300 int8_t init_index[2];
301 int ch;
302
303 *outbufsize = 0;
304
305 if (!inbufcount)
306 return 1;
307
308 get_decode_parameters(pcnxt, init_pcmdata, init_index);
309
310 for (ch = 0; ch < pcnxt->num_channels; ch++) {
311 init_pcmdata[ch] = *inbuf++;
312 outbuf[0] = init_pcmdata[ch];
313 outbuf[1] = init_pcmdata[ch] >> 8;
314 outbuf[2] = init_index[ch];
315 outbuf[3] = 0;
316
317 outbuf += 4;
318 *outbufsize += 4;
319 }
320
321 set_decode_parameters(pcnxt, init_pcmdata, init_index);
322 encode_chunks (pcnxt, &outbuf, outbufsize, &inbuf, inbufcount);
323
324 return 1;
325 }
326
327 /********************************* 4-bit ADPCM decoder ********************************/
328
329 /* Decode the block of ADPCM data into PCM. This requires no context because ADPCM blocks
330 * are indeppendently decodable. This assumes that a single entire block is always decoded;
331 * it must be called multiple times for multiple blocks and cannot resume in the middle of a
332 * block.
333 *
334 * Parameters:
335 * outbuf destination for interleaved PCM samples
336 * inbuf source ADPCM block
337 * inbufsize size of source ADPCM block
338 * channels number of channels in block (must be determined from other context)
339 *
340 * Returns number of converted composite samples (total samples divided by number of channels)
341 */
342
343 int adpcm_decode_block (int16_t *outbuf, const uint8_t *inbuf, size_t inbufsize, size_t outbufsize)
344 {
345 int samples = 1, chunks;
346 int32_t pcmdata[2];
347 int8_t index[2];
348
349 if (inbufsize < 4)
350 return 0;
351
352 *outbuf++ = pcmdata[0] = (int16_t) (inbuf [0] | (inbuf [1] << 8));
353 index[0] = inbuf [2];
354
355 if (index [0] < 0 || index [0] > 88 || inbuf [3]) // sanitize the input a little...
356 return 0;
357
358 inbufsize -= 4;
359 inbuf += 4;
360
361 chunks = inbufsize / 4;
362 samples += chunks * 8;
363
364 while (chunks--) {
365 int i;
366
367 for (i = 0; i < 4; ++i) {
368 uint16_t step = step_table [index [0]], delta = step >> 3;
369
370 if (*inbuf & 1) delta += (step >> 2);
371 if (*inbuf & 2) delta += (step >> 1);
372 if (*inbuf & 4) delta += step;
373
374 if (*inbuf & 8)
375 pcmdata[0] -= delta;
376 else
377 pcmdata[0] += delta;
378
379 index[0] += index_table [*inbuf & 0x7];
380 CLIP(index[0], 0, 88);
381 CLIP(pcmdata[0], -32768, 32767);
382 outbuf [i * 2] = pcmdata[0];
383
384 step = step_table [index [0]]; delta = step >> 3;
385
386 if (*inbuf & 0x10) delta += (step >> 2);
387 if (*inbuf & 0x20) delta += (step >> 1);
388 if (*inbuf & 0x40) delta += step;
389
390 if (*inbuf & 0x80)
391 pcmdata[0] -= delta;
392 else
393 pcmdata[0] += delta;
394
395 index[0] += index_table [(*inbuf >> 4) & 0x7];
396 CLIP(index[0], 0, 88);
397 CLIP(pcmdata[0], -32768, 32767);
398 outbuf [(i * 2 + 1)] = pcmdata[0];
399
400 inbuf++;
401 }
402
403 outbuf += 8;
404 }
405
406 return samples;
407 }
408