blob: 8f7f5ca95530704e8764a0d6f78fc73f166bb239
1 | #define LOG_TAG "AdpcmDecoder" |
2 | |
3 | #include <stdio.h> |
4 | #include <stdint.h> |
5 | #include "adpcm.h" |
6 | #include "../../amadec/adec-armdec-mgt.h" |
7 | #include "../../amadec/audio-dec.h" |
8 | #include <android/log.h> |
9 | #include <sys/time.h> |
10 | #include <stdint.h> |
11 | #include <string.h> |
12 | #define PRINTF(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) |
13 | |
14 | |
15 | typedef struct { |
16 | int ValidDataLen; |
17 | int UsedDataLen; |
18 | unsigned char *BufStart; |
19 | unsigned char *pcur; |
20 | } pcm_read_ctl_t; |
21 | |
22 | |
23 | static int pcm_read_init(pcm_read_ctl_t *pcm_read_ctx, unsigned char* inbuf, int size) |
24 | { |
25 | pcm_read_ctx->ValidDataLen = size; |
26 | pcm_read_ctx->UsedDataLen = 0; |
27 | pcm_read_ctx->BufStart = inbuf; |
28 | pcm_read_ctx->pcur = inbuf; |
29 | return 0; |
30 | } |
31 | |
32 | static int pcm_read(pcm_read_ctl_t *pcm_read_ctx, unsigned char* outbuf, int size) |
33 | { |
34 | int bytes_read = 0; |
35 | if (size <= pcm_read_ctx->ValidDataLen) { |
36 | memcpy(outbuf, pcm_read_ctx->pcur, size); |
37 | pcm_read_ctx->ValidDataLen -= size; |
38 | pcm_read_ctx->UsedDataLen += size; |
39 | pcm_read_ctx->pcur += size; |
40 | bytes_read = size; |
41 | } |
42 | return bytes_read; |
43 | } |
44 | |
45 | struct t_wave_buf { |
46 | void *addr; |
47 | unsigned size; |
48 | }; |
49 | static unsigned wave_timestamplen = 0; |
50 | static unsigned wave_timestamp = 0; |
51 | |
52 | static int adpcm_step[89] = { |
53 | 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, |
54 | 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, |
55 | 50, 55, 60, 66, 73, 80, 88, 97, 107, 118, |
56 | 130, 143, 157, 173, 190, 209, 230, 253, 279, 307, |
57 | 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, |
58 | 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, |
59 | 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358, |
60 | 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, |
61 | 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 |
62 | }; |
63 | |
64 | static int adpcm_index[16] = { |
65 | -1, -1, -1, -1, 2, 4, 6, 8, |
66 | -1, -1, -1, -1, 2, 4, 6, 8 |
67 | }; |
68 | |
69 | // useful macros |
70 | // clamp a number between 0 and 88 |
71 | #define CLAMP_0_TO_88(x) if (x < 0) x = 0; else if (x > 88) x = 88; |
72 | // clamp a number within a signed 16-bit range |
73 | #define CLAMP_S16(x) if (x < -32768) x = -32768; \ |
74 | else if (x > 32767) x = 32767; |
75 | // clamp a number above 16 |
76 | #define CLAMP_ABOVE_16(x) if (x < 16) x = 16; |
77 | // sign extend a 16-bit value |
78 | #define SE_16BIT(x) if (x & 0x8000) x -= 0x10000; |
79 | // sign extend a 4-bit value |
80 | #define SE_4BIT(x) if (x & 0x8) x -= 0x10; |
81 | static t_adpcm_output_buf_manager g_mgr; |
82 | static int block_align = 0; |
83 | static int offset = 0; |
84 | //extern unsigned char buffer[1024*64]; |
85 | static unsigned char *pwavebuf = NULL; |
86 | static struct t_wave_buf wave_decoder_buffer[] = {{0, 0}, {0, 0}}; // 0 - stream, 1 - pcm |
87 | |
88 | |
89 | static int adpcm_init(aml_audio_dec_t *audec) |
90 | { |
91 | audio_decoder_operations_t *adec_ops = (audio_decoder_operations_t *)audec->adec_ops; |
92 | PRINTF("[%s]audec->format/%d adec_ops->samplerate/%d adec_ops->channels/%d\n", |
93 | __FUNCTION__, audec->format, adec_ops->samplerate, adec_ops->channels); |
94 | |
95 | wave_decoder_buffer[0].addr = malloc(WAVE_BLOCK_SIZE); |
96 | if (wave_decoder_buffer[0].addr == 0) { |
97 | PRINTF("[%s %d]Error: malloc adpcm buffer failed!\n", __FUNCTION__, __LINE__); |
98 | return -1; |
99 | } |
100 | wave_decoder_buffer[0].size = WAVE_BLOCK_SIZE; |
101 | wave_decoder_buffer[1].addr = malloc(WAVE_BLOCK_SIZE * 4 * 4); |
102 | if (wave_decoder_buffer[1].addr == 0) { |
103 | PRINTF("[%s %d]Error: malloc adpcm buffer failed!\n", __FUNCTION__, __LINE__); |
104 | return -1; |
105 | } |
106 | adec_ops->nInBufSize = WAVE_BLOCK_SIZE; |
107 | adec_ops->nOutBufSize = 0; |
108 | wave_decoder_buffer[1].size = WAVE_BLOCK_SIZE * 4 * 4; // 2byte, 2ch, compress ratio 4 |
109 | g_mgr.start = 0; |
110 | g_mgr.size = 0; |
111 | g_mgr.bps = audec->data_width; |
112 | g_mgr.ch = adec_ops->channels; |
113 | g_mgr.sr = adec_ops->samplerate; |
114 | g_mgr.wr = 0; |
115 | g_mgr.last_rd = 0; |
116 | g_mgr.totalSample = 0; |
117 | g_mgr.totalSamplePlayed = 0; |
118 | g_mgr.totalSampleDecoded = 0; |
119 | g_mgr.last_pts = 0; |
120 | g_mgr.blk = 0; |
121 | |
122 | block_align = audec->block_align; |
123 | wave_timestamplen = 0; |
124 | PRINTF("[%s %d]block_align/%d audec->codec_id/0x%x\n", __FUNCTION__, __LINE__, block_align, audec->codec_id); |
125 | return 0; |
126 | } |
127 | |
128 | #define CHECK_DATA_ENOUGH_SUB(Ctl,NeedBytes,UsedSetIfNo) { \ |
129 | if((Ctl)->ValidDataLen < (NeedBytes)){ \ |
130 | PRINTF("[%s %d]NOTE--> no enough data\n",__FUNCTION__,__LINE__);\ |
131 | (Ctl)->UsedDataLen-=(UsedSetIfNo); \ |
132 | return -1; \ |
133 | } \ |
134 | } |
135 | |
136 | #define CHECK_DATA_ENOUGH_SET(Ctl,NeedBytes,UsedSetIfNo) { \ |
137 | if((Ctl)->ValidDataLen < (NeedBytes)){ \ |
138 | PRINTF("[%s %d]NOTE--> no enough data\n",__FUNCTION__,__LINE__);\ |
139 | (Ctl)->UsedDataLen=(UsedSetIfNo); \ |
140 | return -1; \ |
141 | } \ |
142 | } |
143 | |
144 | static int refill(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char* buf, int len) |
145 | { |
146 | static unsigned refill_timestamp_len = 0; |
147 | unsigned char *pbuf = buf; |
148 | unsigned char tmp_a = 0; |
149 | unsigned char tmp_p = 0; |
150 | unsigned char tmp_t = 0; |
151 | unsigned char tmp_s = 0; |
152 | int len_bak = len; |
153 | int tmp = 0; |
154 | if (wave_timestamplen == 0) { // when no apts found |
155 | unsigned char timestamp[4] = {0}; |
156 | unsigned char block_length[4] = {0}; |
157 | |
158 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, 4, 0) |
159 | pcm_read(pcm_read_ctx, &tmp_a, 1); |
160 | pcm_read(pcm_read_ctx, &tmp_p, 1); |
161 | pcm_read(pcm_read_ctx, &tmp_t, 1); |
162 | pcm_read(pcm_read_ctx, &tmp_s, 1); |
163 | |
164 | if (tmp_a == 'A' && tmp_p == 'P' && tmp_t == 'T' && tmp_s == 'S') { |
165 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, 8, 0) |
166 | pcm_read(pcm_read_ctx, timestamp, 4); |
167 | wave_timestamp = (timestamp[0] << 24) | (timestamp[1] << 16) | (timestamp[2] << 8) | (timestamp[3]); |
168 | pcm_read(pcm_read_ctx, block_length, 4); |
169 | wave_timestamplen = (block_length[0] << 24) | (block_length[1] << 16) | (block_length[2] << 8) | (block_length[3]); |
170 | refill_timestamp_len = wave_timestamplen; |
171 | |
172 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, refill_timestamp_len, 0) |
173 | |
174 | } else if (tmp_a == 'R' && tmp_p == 'I' && tmp_t == 'F' && tmp_s == 'F') { |
175 | if ((audec->codec_id == CODEC_ID_ADPCM_IMA_WAV) || (audec->codec_id == CODEC_ID_ADPCM_MS)) { |
176 | tmp = len; |
177 | while (tmp) { |
178 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, 8, 0) |
179 | pcm_read(pcm_read_ctx, ×tamp[0], 1); |
180 | tmp --; |
181 | if (timestamp[0] == 'd') { |
182 | pcm_read(pcm_read_ctx, ×tamp[1], 3); |
183 | tmp -= 3; |
184 | if ((timestamp[0] == 'd') && (timestamp[1] == 'a') && (timestamp[2] == 't') && (timestamp[3] == 'a')) { |
185 | break; |
186 | } |
187 | } |
188 | } |
189 | pcm_read(pcm_read_ctx, timestamp, 4); |
190 | wave_timestamplen = 0; |
191 | wave_timestamp = 0xffffffff; |
192 | |
193 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, len, 0) |
194 | } else { |
195 | *pbuf++ = tmp_a; |
196 | *pbuf++ = tmp_p; |
197 | *pbuf++ = tmp_t; |
198 | *pbuf++ = tmp_s; |
199 | len -= 4; |
200 | wave_timestamplen = 0; |
201 | wave_timestamp = 0xffffffff; |
202 | |
203 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, len, 0) |
204 | } |
205 | } else { |
206 | *pbuf++ = tmp_a; |
207 | *pbuf++ = tmp_p; |
208 | *pbuf++ = tmp_t; |
209 | *pbuf++ = tmp_s; |
210 | len -= 4; |
211 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, len, 0) |
212 | wave_timestamplen = 0; |
213 | wave_timestamp = 0xffffffff; |
214 | } |
215 | } |
216 | |
217 | if (wave_timestamplen) { |
218 | pcm_read(pcm_read_ctx, pbuf, refill_timestamp_len); |
219 | return refill_timestamp_len; |
220 | } else { |
221 | pcm_read(pcm_read_ctx, pbuf, len); |
222 | return len_bak; |
223 | } |
224 | |
225 | } |
226 | |
227 | /*IMA ADPCM*/ |
228 | #define le2me_16(x) (x) |
229 | #define MS_IMA_ADPCM_PREAMBLE_SIZE 4 |
230 | #define LE_16(x) (le2me_16(*(unsigned short *)(x))) |
231 | |
232 | static void decode_nibbles(unsigned short *output, |
233 | int output_size, int channels, |
234 | int predictor_l, int index_l, |
235 | int predictor_r, int index_r) |
236 | { |
237 | int step[2]; |
238 | int predictor[2]; |
239 | int index[2]; |
240 | int diff; |
241 | int i; |
242 | int sign; |
243 | int delta; |
244 | int channel_number = 0; |
245 | |
246 | step[0] = adpcm_step[index_l]; |
247 | step[1] = adpcm_step[index_r]; |
248 | predictor[0] = predictor_l; |
249 | predictor[1] = predictor_r; |
250 | index[0] = index_l; |
251 | index[1] = index_r; |
252 | |
253 | for (i = 0; i < output_size; i++) { |
254 | delta = output[i]; |
255 | |
256 | index[channel_number] += adpcm_index[delta]; |
257 | CLAMP_0_TO_88(index[channel_number]); |
258 | |
259 | sign = delta & 8; |
260 | delta = delta & 7; |
261 | |
262 | diff = step[channel_number] >> 3; |
263 | if (delta & 4) { |
264 | diff += step[channel_number]; |
265 | } |
266 | if (delta & 2) { |
267 | diff += step[channel_number] >> 1; |
268 | } |
269 | if (delta & 1) { |
270 | diff += step[channel_number] >> 2; |
271 | } |
272 | |
273 | if (sign) { |
274 | predictor[channel_number] -= diff; |
275 | } else { |
276 | predictor[channel_number] += diff; |
277 | } |
278 | |
279 | CLAMP_S16(predictor[channel_number]); |
280 | output[i] = predictor[channel_number]; |
281 | step[channel_number] = adpcm_step[index[channel_number]]; |
282 | |
283 | // toggle channel |
284 | channel_number ^= channels - 1; |
285 | |
286 | } |
287 | } |
288 | |
289 | static int ima_adpcm_decode_block(unsigned short *output, |
290 | unsigned char *input, int channels, int block_size) |
291 | { |
292 | int predictor_l = 0; |
293 | int predictor_r = 0; |
294 | int index_l = 0; |
295 | int index_r = 0; |
296 | int i; |
297 | int channel_counter; |
298 | int channel_index; |
299 | int channel_index_l; |
300 | int channel_index_r; |
301 | |
302 | predictor_l = LE_16(&input[0]); |
303 | SE_16BIT(predictor_l); |
304 | index_l = input[2]; |
305 | if (channels == 2) { |
306 | predictor_r = LE_16(&input[4]); |
307 | SE_16BIT(predictor_r); |
308 | index_r = input[6]; |
309 | } |
310 | |
311 | if (channels == 1) |
312 | for (i = 0; i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) { |
313 | output[i * 2 + 0] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] & 0x0F; |
314 | output[i * 2 + 1] = input[MS_IMA_ADPCM_PREAMBLE_SIZE + i] >> 4; |
315 | } |
316 | else { |
317 | // encoded as 8 nibbles (4 bytes) per channel; switch channel every |
318 | // 4th byte |
319 | channel_counter = 0; |
320 | channel_index_l = 0; |
321 | channel_index_r = 1; |
322 | channel_index = channel_index_l; |
323 | for (i = 0; |
324 | i < (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels); i++) { |
325 | output[channel_index + 0] = |
326 | input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] & 0x0F; |
327 | output[channel_index + 2] = |
328 | input[MS_IMA_ADPCM_PREAMBLE_SIZE * 2 + i] >> 4; |
329 | channel_index += 4; |
330 | channel_counter++; |
331 | if (channel_counter == 4) { |
332 | channel_index_l = channel_index; |
333 | channel_index = channel_index_r; |
334 | } else if (channel_counter == 8) { |
335 | channel_index_r = channel_index; |
336 | channel_index = channel_index_l; |
337 | channel_counter = 0; |
338 | } |
339 | } |
340 | } |
341 | |
342 | decode_nibbles(output, |
343 | (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2, |
344 | channels, |
345 | predictor_l, index_l, |
346 | predictor_r, index_r); |
347 | |
348 | return (block_size - MS_IMA_ADPCM_PREAMBLE_SIZE * channels) * 2; |
349 | } |
350 | #define MSADPCM_ADAPT_COEFF_COUNT 7 |
351 | static int AdaptationTable [] = { |
352 | 230, 230, 230, 230, 307, 409, 512, 614, |
353 | 768, 614, 512, 409, 307, 230, 230, 230 |
354 | } ; |
355 | |
356 | /* TODO : The first 7 coef's are are always hardcode and must |
357 | appear in the actual WAVE file. They should be read in |
358 | in case a sound program added extras to the list. */ |
359 | |
360 | static int AdaptCoeff1 [MSADPCM_ADAPT_COEFF_COUNT] = { |
361 | 256, 512, 0, 192, 240, 460, 392 |
362 | } ; |
363 | |
364 | static int AdaptCoeff2 [MSADPCM_ADAPT_COEFF_COUNT] = { |
365 | 0, -256, 0, 64, 0, -208, -232 |
366 | } ; |
367 | |
368 | static int ms_adpcm_decode_block(short *pcm_buf, unsigned char *buf, int channel, int block) |
369 | { |
370 | int sampleblk = 2036; |
371 | short bpred[2]; |
372 | short idelta[2]; |
373 | int blockindx = 0; |
374 | int sampleindx = 0; |
375 | short bytecode = 0; |
376 | int predict = 0; |
377 | int current = 0; |
378 | int delta = 0; |
379 | int i = 0; |
380 | int j = 0; |
381 | short s0 = 0; |
382 | short s1 = 0; |
383 | short s2 = 0; |
384 | short s3 = 0; |
385 | short s4 = 0; |
386 | short s5 = 0; |
387 | |
388 | //sampleblk = sample_block; |
389 | j = 0; |
390 | if (channel == 1) { |
391 | bpred[0] = buf[0]; |
392 | bpred[1] = 0; |
393 | if (bpred[0] >= 7) { |
394 | //printf("sync error\n"); |
395 | //goto _exit; |
396 | } |
397 | idelta[0] = buf[1] | buf[2] << 8; |
398 | idelta[1] = 0; |
399 | |
400 | s1 = buf[3] | buf[4] << 8; |
401 | s0 = buf[5] | buf[6] << 8; |
402 | |
403 | blockindx = 7; |
404 | sampleindx = 2; |
405 | } else if (channel == 2) { |
406 | bpred[0] = buf[0]; |
407 | bpred[1] = buf[1]; |
408 | if (bpred[0] >= 7 || bpred[1] >= 7) { |
409 | //printf("sync error\n"); |
410 | //goto _exit; |
411 | } |
412 | idelta[0] = buf[2] | buf[3] << 8; |
413 | idelta[1] = buf[4] | buf[5] << 8; |
414 | |
415 | s2 = buf[6] | buf[7] << 8; |
416 | s3 = buf[8] | buf[9] << 8; |
417 | s0 = buf[10] | buf[11] << 8; |
418 | s1 = buf[12] | buf[13] << 8; |
419 | blockindx = 14; |
420 | sampleindx = 4; |
421 | } |
422 | |
423 | /*-------------------------------------------------------- |
424 | This was left over from a time when calculations were done |
425 | as ints rather than shorts. Keep this around as a reminder |
426 | in case I ever find a file which decodes incorrectly. |
427 | |
428 | if (chan_idelta [0] & 0x8000) |
429 | chan_idelta [0] -= 0x10000 ; |
430 | if (chan_idelta [1] & 0x8000) |
431 | chan_idelta [1] -= 0x10000 ; |
432 | --------------------------------------------------------*/ |
433 | |
434 | /* Pull apart the packed 4 bit samples and store them in their |
435 | ** correct sample positions. |
436 | */ |
437 | |
438 | /* Decode the encoded 4 bit samples. */ |
439 | int chan; |
440 | |
441 | for (i = channel * 2;/*i<channel*sampleblk&&*/(blockindx < block); i++) { |
442 | if (sampleindx <= i) { |
443 | if (blockindx < block) { |
444 | bytecode = buf[blockindx++]; |
445 | |
446 | |
447 | if (channel == 1) { |
448 | s2 = (bytecode >> 4) & 0x0f; |
449 | s3 = bytecode & 0x0f; |
450 | } else if (channel == 2) { |
451 | s4 = (bytecode >> 4) & 0x0f; |
452 | s5 = bytecode & 0x0f; |
453 | } |
454 | sampleindx++; |
455 | sampleindx++; |
456 | |
457 | } |
458 | } |
459 | chan = (channel > 1) ? (i % 2) : 0; |
460 | |
461 | if (channel == 1) { |
462 | bytecode = s2 & 0x0f; |
463 | } else if (channel == 2) { |
464 | bytecode = s4 & 0x0f; |
465 | } |
466 | /* Compute next Adaptive Scale Factor (ASF) */ |
467 | delta = idelta[chan]; |
468 | |
469 | /* => / 256 => FIXED_POINT_ADAPTATION_BASE == 256 */ |
470 | idelta[chan] = (AdaptationTable[bytecode] * delta) >> 8; |
471 | |
472 | if (idelta[chan] < 16) { |
473 | idelta[chan] = 16; |
474 | } |
475 | if (bytecode & 0x8) { |
476 | bytecode -= 0x10; |
477 | } |
478 | /* => / 256 => FIXED_POINT_COEFF_BASE == 256 */ |
479 | |
480 | if (channel == 1) { |
481 | predict = s1 * AdaptCoeff1[bpred[chan]]; |
482 | predict += s0 * AdaptCoeff2[bpred[chan]]; |
483 | } else if (channel == 2) { |
484 | predict = s2 * AdaptCoeff1[bpred[chan]]; |
485 | predict += s0 * AdaptCoeff2[bpred[chan]]; |
486 | } |
487 | |
488 | predict >>= 8; |
489 | current = bytecode * delta + predict; |
490 | #if 1 |
491 | if (current > 32767) { |
492 | current = 32767 ; |
493 | } else if (current < -32768) { |
494 | current = -32768 ; |
495 | } |
496 | #else |
497 | current = _min(current, 32767); |
498 | current = _max(current, -32768); |
499 | #endif |
500 | if (channel == 1) { |
501 | s2 = current; |
502 | } else if (channel == 2) { |
503 | s4 = current; |
504 | } |
505 | |
506 | pcm_buf[j++] = s0; |
507 | |
508 | if (channel == 1) { |
509 | s0 = s1; |
510 | s1 = s2; |
511 | s2 = s3; |
512 | } else if (channel == 2) { |
513 | s0 = s1; |
514 | s1 = s2; |
515 | s2 = s3; |
516 | s3 = s4; |
517 | s4 = s5; |
518 | } |
519 | } |
520 | |
521 | if (channel == 1) { |
522 | pcm_buf[j++] = s0; |
523 | pcm_buf[j++] = s1; |
524 | } else if (channel == 2) { |
525 | pcm_buf[j++] = s0; |
526 | pcm_buf[j++] = s1; |
527 | pcm_buf[j++] = s2; |
528 | pcm_buf[j++] = s3; |
529 | } |
530 | |
531 | return j; |
532 | } |
533 | |
534 | |
535 | /* |
536 | * u-law, A-law and linear PCM conversions. |
537 | */ |
538 | |
539 | #define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */ |
540 | #define QUANT_MASK (0xf) /* Quantization field mask. */ |
541 | #define NSEGS (8) /* Number of A-law segments. */ |
542 | #define SEG_SHIFT (4) /* Left shift for segment number. */ |
543 | #define SEG_MASK (0x70) /* Segment field mask. */ |
544 | //static short seg_end[8] = {0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF}; |
545 | #define BIAS (0x84) /* Bias for linear code. */ |
546 | |
547 | /* |
548 | * alaw2linear() - Convert an A-law value to 16-bit linear PCM |
549 | * |
550 | */ |
551 | int alaw2linear(unsigned char a_val) |
552 | { |
553 | int t; |
554 | int seg; |
555 | a_val ^= 0x55; |
556 | t = (a_val & QUANT_MASK) << 4; |
557 | seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT; |
558 | switch (seg) { |
559 | case 0: |
560 | t += 8; |
561 | break; |
562 | case 1: |
563 | t += 0x108; |
564 | break; |
565 | default: |
566 | t += 0x108; |
567 | t <<= seg - 1; |
568 | } |
569 | return ((a_val & SIGN_BIT) ? t : -t); |
570 | } |
571 | /* |
572 | * ulaw2linear() - Convert a u-law value to 16-bit linear PCM |
573 | * |
574 | * First, a biased linear code is derived from the code word. An unbiased |
575 | * output can then be obtained by subtracting 33 from the biased code. |
576 | * |
577 | * Note that this function expects to be passed the complement of the |
578 | * original code word. This is in keeping with ISDN conventions. |
579 | */ |
580 | int ulaw2linear(unsigned char u_val) |
581 | { |
582 | int t; |
583 | /* Complement to obtain normal u-law value. */ |
584 | u_val = ~u_val; |
585 | /* |
586 | * Extract and bias the quantization bits. Then |
587 | * shift up by the segment number and subtract out the bias. |
588 | */ |
589 | t = ((u_val & QUANT_MASK) << 3) + BIAS; |
590 | t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT; |
591 | return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS)); |
592 | } |
593 | |
594 | int runalawdecoder(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
595 | { |
596 | int i = 0; |
597 | int tmp = 0; |
598 | short *pcm_buf = (short*)wave_decoder_buffer[1].addr; |
599 | |
600 | tmp = refill(audec, pcm_read_ctx, pwavebuf, WAVE_BLOCK_SIZE); |
601 | if (tmp < 0) { |
602 | return -1; |
603 | } |
604 | for (i = 0; i < tmp; i++) { |
605 | pcm_buf[i] = alaw2linear(pwavebuf[i]); |
606 | } |
607 | memcpy(buf, (char*)pcm_buf, 2 * WAVE_BLOCK_SIZE); |
608 | return (WAVE_BLOCK_SIZE) * 2; |
609 | } |
610 | int runulawdecoder(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
611 | { |
612 | int i = 0; |
613 | short *pcm_buf = (short*)wave_decoder_buffer[1].addr; |
614 | int tmp = 0; |
615 | |
616 | tmp = refill(audec, pcm_read_ctx, pwavebuf, WAVE_BLOCK_SIZE); |
617 | if (tmp < 0) { |
618 | return -1; |
619 | } |
620 | for (i = 0; i < tmp; i++) { |
621 | pcm_buf[i] = ulaw2linear(pwavebuf[i]); |
622 | } |
623 | memcpy(buf, (char*)pcm_buf, 2 * WAVE_BLOCK_SIZE); |
624 | return (WAVE_BLOCK_SIZE) * 2; |
625 | |
626 | } |
627 | int runimaadpcmdecoder(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
628 | { |
629 | short *pcm_buf = (short*)wave_decoder_buffer[1].addr; |
630 | int Output_Size = 0; |
631 | int tmp = 0; |
632 | char buffer[5]; |
633 | unsigned block_size = 0; |
634 | int UsedDataLenSave = 0; |
635 | if (!block_align) { |
636 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, 4, 0) |
637 | pcm_read(pcm_read_ctx, buffer, 4); |
638 | while (1) { |
639 | if ((buffer[0] == 0x11) && (buffer[1] == 0x22) && (buffer[2] == 0x33) && (buffer[3] == 0x44)) { //sync word |
640 | break; |
641 | } |
642 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, 1, 3) |
643 | pcm_read(pcm_read_ctx, &buffer[4], 1); |
644 | memmove(buffer, &buffer[1], 4); |
645 | } |
646 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, 2, 4) |
647 | pcm_read(pcm_read_ctx, buffer, 2); |
648 | |
649 | block_size = (buffer[0] << 8) | buffer[1]; |
650 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, block_size, 6) |
651 | } else { |
652 | block_size = block_align; |
653 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, block_size, 0) |
654 | } |
655 | |
656 | if (block_size < 4) { |
657 | PRINTF("[%s %d]imaadpcm block align not valid: %d\n", __FUNCTION__, __LINE__, block_size); |
658 | return 0; |
659 | } |
660 | |
661 | UsedDataLenSave = pcm_read_ctx->UsedDataLen; |
662 | tmp = refill(audec, pcm_read_ctx, pwavebuf, block_size); |
663 | if (tmp < 0) { |
664 | pcm_read_ctx->UsedDataLen = UsedDataLenSave; |
665 | return -1; |
666 | } |
667 | |
668 | if (tmp != block_size) { |
669 | PRINTF("[%s %d]imaadpcm: data missalign\n", __FUNCTION__, __LINE__); |
670 | } |
671 | Output_Size = ima_adpcm_decode_block((unsigned short *)pcm_buf, pwavebuf, g_mgr.ch, block_size); |
672 | memcpy(buf, (char*)pcm_buf, 2 * Output_Size); |
673 | return Output_Size * 2; |
674 | |
675 | } |
676 | |
677 | int runmsadpcmdecoder(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
678 | { |
679 | short *pcm_buf = (short*)wave_decoder_buffer[1].addr; |
680 | int Output_Size = 0; |
681 | unsigned tmp = 0; |
682 | char buffer[5]; |
683 | unsigned block_size = 0; |
684 | int UsedDataLenSave = 0; |
685 | if (!block_align) { |
686 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, 4, 0) |
687 | pcm_read(pcm_read_ctx, buffer, 4); |
688 | while (1) { |
689 | if ((buffer[0] == 0x11) && (buffer[1] == 0x22) && (buffer[2] == 0x33) && (buffer[3] == 0x44)) { //sync word |
690 | break; |
691 | } |
692 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, 1, 3) |
693 | pcm_read(pcm_read_ctx, &buffer[4], 1); |
694 | memmove(buffer, &buffer[1], 4); |
695 | } |
696 | |
697 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, 2, 4) |
698 | pcm_read(pcm_read_ctx, buffer, 2); |
699 | block_size = (buffer[0] << 8) | buffer[1]; |
700 | CHECK_DATA_ENOUGH_SUB(pcm_read_ctx, block_size, 6) |
701 | } else { |
702 | block_size = block_align; |
703 | CHECK_DATA_ENOUGH_SET(pcm_read_ctx, block_size, 0) |
704 | } |
705 | |
706 | if (block_size < 4) { |
707 | PRINTF("[%s %d]msadpcm block align not valid: %d\n", __FUNCTION__, __LINE__, block_size); |
708 | return 0; |
709 | } |
710 | |
711 | UsedDataLenSave = pcm_read_ctx->UsedDataLen; |
712 | tmp = refill(audec, pcm_read_ctx, pwavebuf, block_size); |
713 | if (tmp < 0) { |
714 | pcm_read_ctx->UsedDataLen = UsedDataLenSave; |
715 | return -1; |
716 | |
717 | } |
718 | if (tmp != block_size) { |
719 | PRINTF("[%s %d]msadpcm: data missalign\n", __FUNCTION__, __LINE__); |
720 | } |
721 | |
722 | Output_Size = ms_adpcm_decode_block(pcm_buf, pwavebuf, g_mgr.ch, block_size); |
723 | Output_Size = Output_Size - Output_Size % g_mgr.ch; |
724 | memcpy(buf, (char*)pcm_buf, 2 * Output_Size); |
725 | return Output_Size * 2; |
726 | |
727 | } |
728 | |
729 | |
730 | enum SampleFormat { |
731 | SAMPLE_FMT_NONE = -1, |
732 | SAMPLE_FMT_U8, ///< unsigned 8 bits |
733 | SAMPLE_FMT_S16, ///< signed 16 bits |
734 | SAMPLE_FMT_S32, ///< signed 32 bits |
735 | SAMPLE_FMT_FLT, ///< float |
736 | SAMPLE_FMT_DBL, ///< double |
737 | SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if dynamically linking to libavcodec |
738 | }; |
739 | |
740 | int runpcmdecoder(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
741 | { |
742 | int i/*, j*/; |
743 | short *pcm_buf = (short*)wave_decoder_buffer[1].addr; |
744 | int tmp = 0; |
745 | offset = 0; |
746 | if (g_mgr.bps == SAMPLE_FMT_U8) { |
747 | tmp = refill(audec, pcm_read_ctx, pwavebuf, WAVE_BLOCK_SIZE); |
748 | if (tmp < 0) { |
749 | return -1; |
750 | } |
751 | for (i = 0; i < tmp;) { |
752 | pcm_buf[i] = (pwavebuf[i] - 0x80) << 8; |
753 | i++; |
754 | pcm_buf[i] = (pwavebuf[i] - 0x80) << 8; |
755 | i++; |
756 | pcm_buf[i] = (pwavebuf[i] - 0x80) << 8; |
757 | i++; |
758 | pcm_buf[i] = (pwavebuf[i] - 0x80) << 8; |
759 | i++; |
760 | } |
761 | return (WAVE_BLOCK_SIZE) * 2; |
762 | } else { |
763 | if (refill(audec, pcm_read_ctx, pwavebuf, WAVE_BLOCK_SIZE) < 0) { |
764 | return -1; |
765 | }; |
766 | return ((WAVE_BLOCK_SIZE >> 1) * 2); |
767 | } |
768 | } |
769 | |
770 | static int adpcm_decode_frame(aml_audio_dec_t *audec, pcm_read_ctl_t *pcm_read_ctx, unsigned char *buf, int len) |
771 | { |
772 | int buf_size = 0; |
773 | pwavebuf = (unsigned char*)wave_decoder_buffer[0].addr; |
774 | |
775 | switch (audec->codec_id) { |
776 | case CODEC_ID_PCM_ALAW: |
777 | buf_size = runalawdecoder(audec, pcm_read_ctx, buf, len); |
778 | break; |
779 | |
780 | case CODEC_ID_PCM_MULAW: |
781 | buf_size = runulawdecoder(audec, pcm_read_ctx, buf, len); |
782 | break; |
783 | |
784 | case CODEC_ID_ADPCM_IMA_WAV: |
785 | buf_size = runimaadpcmdecoder(audec, pcm_read_ctx, buf, len); |
786 | break; |
787 | |
788 | case CODEC_ID_ADPCM_MS: |
789 | buf_size = runmsadpcmdecoder(audec, pcm_read_ctx, buf, len); |
790 | break; |
791 | default: |
792 | buf_size = runpcmdecoder(audec, pcm_read_ctx, buf, len); |
793 | break; |
794 | } |
795 | return buf_size; |
796 | } |
797 | |
798 | static int adpcm_decode_release(void) |
799 | { |
800 | if (wave_decoder_buffer[0].addr) { |
801 | free(wave_decoder_buffer[0].addr); |
802 | wave_decoder_buffer[0].addr = 0; |
803 | wave_decoder_buffer[0].size = 0; |
804 | } |
805 | if (wave_decoder_buffer[1].addr) { |
806 | free(wave_decoder_buffer[1].addr); |
807 | wave_decoder_buffer[1].addr = 0; |
808 | wave_decoder_buffer[1].size = 0; |
809 | } |
810 | return 0; |
811 | } |
812 | |
813 | int audio_dec_decode(audio_decoder_operations_t *adec_ops, char *outbuf, int *outlen, char *inbuf, int inlen) |
814 | { |
815 | aml_audio_dec_t *audec = (aml_audio_dec_t *)(adec_ops->priv_data); |
816 | pcm_read_ctl_t pcm_read_ctl = {0}; |
817 | pcm_read_init(&pcm_read_ctl, inbuf, inlen); |
818 | *outlen = adpcm_decode_frame(audec, &pcm_read_ctl, outbuf, *outlen); |
819 | return pcm_read_ctl.UsedDataLen; |
820 | |
821 | } |
822 | int audio_dec_init(audio_decoder_operations_t *adec_ops) |
823 | { |
824 | aml_audio_dec_t *audec = (aml_audio_dec_t *)(adec_ops->priv_data); |
825 | //PRINTF("\n\n[%s]BuildDate--%s BuildTime--%s", __FUNCTION__, __DATE__, __TIME__); |
826 | adpcm_init(audec); |
827 | return 0; |
828 | } |
829 | |
830 | int audio_dec_release(audio_decoder_operations_t *adec_ops) |
831 | { |
832 | adpcm_decode_release(); |
833 | return 0; |
834 | } |
835 | |
836 | int audio_dec_getinfo(audio_decoder_operations_t *adec_ops, void *pAudioInfo) |
837 | { |
838 | return 0; |
839 | } |
840 | |
841 | |
842 |