summaryrefslogtreecommitdiff
path: root/audio_codec/libamr/amr_decode.c (plain)
blob: 319f31e9b12fe15f9bf92459f443aa82acc6b0a7
1
2#include "sp_dec.h"
3
4#include "../../amadec/adec-armdec-mgt.h"
5#include <android/log.h>
6#include "interf_dec.h"
7#include "dec_if.h"
8
9#define LOG_TAG "AmrDecoder"
10#define amr_print(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
11
12#define DefaultReadSize 2*1024
13#define DefaultOutBufSize 16*1024
14
15
16
17typedef void (*_AMR_DECODE_FRAME)(void*, short*, short *outlen, char *inbuf, int *consume);
18
19static _AMR_DECODE_FRAME amr_decode_frame_fun;
20static int SampleRateOut = 0;
21static void * destate = NULL;
22static const short amrnb_block_size[16] = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
23
24#if 0
25int amr_read(unsigned char *pBuffer, int n)
26{
27 int i;
28 i = read_buffer(pBuffer, n);
29 return i;
30}
31#endif
32
33void * Decoder_Init()
34{
35 if (SampleRateOut == 8000) {
36 return (void *)Decoder_Interface_init();
37 } else {
38 return (void *)D_IF_init();
39 }
40}
41void amrnb_decode_frame(void* destate, short* pOutBuffer, short *outlen, char *inbuf, int *consume)
42{
43 enum Mode dec_mode;
44 int read_size;
45 unsigned char analysis[32];
46 char *tpbuf;
47 tpbuf = inbuf;
48
49 memset(analysis, 0, 32);
50 //amr_read(analysis, 1);
51 analysis[0] = *tpbuf++;
52
53 dec_mode = (analysis[0] >> 3) & 0x000F;
54 read_size = amrnb_block_size[dec_mode];
55
56 //note: the logic modification of this time refer from <opencore_souce code>:
57 // when (dec_mode==15) and (read_size==0), ie,dec_mode==RX_NO_DATA
58 // still need to send data to decoder:
59 //amr_read(&analysis[1], read_size );
60 memcpy(&analysis[1], tpbuf, read_size);
61 *consume = read_size + 1;
62 /* call decoder */
63 Decoder_Interface_Decode(destate, analysis, pOutBuffer, 0);
64}
65
66//note:maybe this decoder logic has some problem and too old now ,but can not find where
67// its souce comes from, so just let maitain current situations!!:
68void amrwb_decode_frame(void* destate, short* pOutBuffer, short *outlen, char *inbuf, int *consume)
69{
70 unsigned char serial[61];
71 short mode;
72 char *tpbuf;
73 tpbuf = inbuf;
74
75 static const short amrwb_block_size[16] = {18, 24, 33, 37, 41, 47, 51, 59, 61, 6, 6, 0, 0, 0, 1, 1};
76 //amr_read(serial, 1);
77 serial[0] = *tpbuf++;
78
79 mode = (short)((serial[0] >> 3) & 0x0F);
80
81 if (amrwb_block_size[mode] - 1 > 0) {
82 //amr_read(&serial[1], amrwb_block_size[mode] - 1 );
83 memcpy(&serial[1], tpbuf, amrwb_block_size[mode] - 1);
84 tpbuf += amrwb_block_size[mode] - 1;
85 D_IF_decode(destate, serial, pOutBuffer, 0);
86 } else {
87 *outlen = -1;
88 amr_print("[%s %d]decoder err!\n", __FUNCTION__, __LINE__);
89 // empty frame??? need clean history???
90 // memset(pOutBuffer, 0, 320*2);
91 }
92 *consume += tpbuf - inbuf;
93}
94//static int amr_decode_frame(unsigned char *buf, int maxlen, struct frame_fmt *fmt)
95int audio_dec_decode(audio_decoder_operations_t *adec_ops, char *outbuf, int *outlen, char *inbuf, int inlen)
96{
97 //amr_print("[%s:%d]enter into amr_decoder\n", __FUNCTION__,__LINE__);
98 int res = 0;
99 int consume = 0;
100 short synth[320];
101 int outputSample;
102 short out_ret = 0;
103 memset(synth, 0, 320 * sizeof(short));
104 if (SampleRateOut == 8000) {
105 outputSample = 160;
106 } else {
107 outputSample = 160 * 2;
108 }
109 amr_decode_frame_fun(destate, synth, &out_ret, inbuf, &consume);
110
111 if (out_ret < 0) {
112 *outlen = 0;
113 return consume;
114 }
115 memcpy(outbuf, (char*)synth, outputSample * 2);
116 *outlen = outputSample * 2;
117 return consume;
118}
119
120
121//static int amr_decode_init(struct frame_fmt * fmt)
122int audio_dec_init(audio_decoder_operations_t *adec_ops)
123{
124 //struct audio_info *real_data;
125 //real_data = (struct audio_info *)fmt->private_data;
126 //amr_print("\n\n[%s]BuildDate--%s BuildTime--%s", __FUNCTION__, __DATE__, __TIME__);
127 if (adec_ops->samplerate == 16000) {
128 SampleRateOut = 16000;
129 } else {
130 SampleRateOut = 8000;
131 }
132 destate = NULL;
133 destate = Decoder_Init();
134 if (SampleRateOut == 8000) {
135 amr_decode_frame_fun = amrnb_decode_frame;
136 } else {
137 amr_decode_frame_fun = amrwb_decode_frame;
138 }
139 //fmt->valid=CHANNEL_VALID | SAMPLE_RATE_VALID | DATA_WIDTH_VALID;
140 //fmt->sample_rate = SampleRateOut;
141 //fmt->channel_num = real_data->channels;
142 //fmt->data_width = 16;
143 adec_ops->nInBufSize = DefaultReadSize;
144 adec_ops->nOutBufSize = DefaultOutBufSize;
145 amr_print("amr %s ,sr %d,ch num %d\n", (SampleRateOut == 8000) ? "NB" : "WB", adec_ops->samplerate, adec_ops->channels);
146 return 0;
147}
148
149void Decoder_exit(void *st)
150{
151 if (SampleRateOut == 8000) {
152 Decoder_Interface_exit(st);
153 } else {
154 D_IF_exit(st);
155 }
156}
157
158//static int amr_decode_release(void)
159int audio_dec_release(audio_decoder_operations_t *adec_ops)
160{
161 Decoder_exit(destate);
162 return 0;
163}
164
165int audio_dec_getinfo(audio_decoder_operations_t *adec_ops, void *pAudioInfo)
166{
167 return 0;
168}
169
170#if 0
171static struct codec_type amr_codec = {
172 .name = "amr",
173 .init = amr_decode_init,
174 .release = amr_decode_release,
175 .decode_frame = amr_decode_frame,
176};
177
178
179void __used amr_codec_init(void)
180{
181 amr_print("register amr lib \n");
182 register_codec(&amr_codec);
183}
184
185CODEC_INIT(amr_codec_init);
186#endif
187