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