1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  *
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <string.h>
10 #include <signal.h>
11 
12 #include "uvoice_os.h"
13 #include "uvoice_types.h"
14 #include "uvoice_player.h"
15 
16 #include "uvoice_common.h"
17 #include "uvoice_play.h"
18 
19 #include "opensource/opus/source/include/opus.h"
20 
21 
22 #define OPUS_FRAME_LEN_MAX		1500
23 #define OPUS_FRAME_SAMPLES_MAX	1280
24 
25 typedef struct {
26 	OpusDecoder *handler;
27 	int rate;
28 	int channels;
29 } opus_decoder_t;
30 
31 static int opus_rate;
32 static int opus_channels;
33 
char_to_int(uint8_t ch[4])34 static uint32_t char_to_int(uint8_t ch[4])
35 {
36     return ((uint32_t)ch[0] << 24) | ((uint32_t)ch[1] << 16) |
37 		((uint32_t)ch[2] << 8) | (uint32_t)ch[3];
38 }
39 
opus_rate_valid(int rate)40 static bool opus_rate_valid(int rate)
41 {
42 	switch (rate) {
43 	case 8000:
44 	case 12000:
45 	case 16000:
46 	case 24000:
47 	case 48000:
48 		return true;
49 	}
50 	return false;
51 }
52 
opus_channels_valid(int channels)53 static bool opus_channels_valid(int channels)
54 {
55 	if (channels == 1 || channels == 2)
56 		return true;
57 	return false;
58 }
59 
opus_decode_reset(void * priv)60 static int opus_decode_reset(void *priv)
61 {
62 	media_decoder_t *mdecoder = (media_decoder_t *)priv;
63 	if (!mdecoder) {
64 		M_LOGE("mdecoder null !\n");
65 		return -1;
66 	}
67 
68 	opus_decoder_t *opus = mdecoder->decoder;
69 	if (!opus) {
70 		M_LOGE("opus decoder null !\n");
71 		return -1;
72 	}
73 
74 
75 	M_LOGI("opus decoder reset\n");
76 	return 0;
77 }
78 
opus_decode_process(void * priv,uint8_t * buffer,int nbytes)79 static int opus_decode_process(void *priv, uint8_t *buffer, int nbytes)
80 {
81 	media_decoder_t *mdecoder = (media_decoder_t *)priv;
82 	opus_decoder_t *opus;
83 
84 	uint8_t *frame_ptr = buffer;
85 	int remaining_len = nbytes;
86 	int frame_len;
87 
88 	int out_samples;
89 	short sample;
90 	int ret;
91 	int i;
92 
93 	if (!mdecoder) {
94 		M_LOGE("mdecoder null !\n");
95 		return -1;
96 	}
97 
98 	char *pcm_out = mdecoder->buffer_out;
99 	short *dec_out = mdecoder->buffer_out;
100 
101 	opus = mdecoder->decoder;
102 	if (!opus) {
103 		M_LOGE("opus decoder null !\n");
104 		return -1;
105 	}
106 
107 	while (1) {
108 		if (remaining_len <= 8) {
109 			mdecoder->unproc_size = remaining_len;
110 			break;
111 		}
112 
113 		frame_len = char_to_int(frame_ptr);
114 		frame_ptr += 8;
115 		remaining_len -= 8;
116 
117 		if (frame_len > remaining_len) {
118 			mdecoder->unproc_size = remaining_len + 8;
119 			break;
120 		}
121 
122 		if (frame_len > OPUS_FRAME_LEN_MAX || frame_len <= 0) {
123 			M_LOGE("frame len %d invalid !\n", frame_len);
124 			return -1;
125 		}
126 
127 		out_samples = opus_decode(opus->handler, frame_ptr,
128 			frame_len,
129 			mdecoder->buffer_out, OPUS_FRAME_SAMPLES_MAX, 0);
130 
131 		frame_ptr += frame_len;
132 		remaining_len -= frame_len;
133 
134 		if (out_samples <= 0) {
135 			M_LOGE("decoder error %d!\n", out_samples);
136 			continue;
137 		}
138 
139 		if (!mdecoder->running) {
140 			if (mdecoder->message) {
141 				media_pcminfo_t pcm_info;
142 				memset(&pcm_info, 0, sizeof(pcm_info));
143 				pcm_info.rate = opus->rate;
144 				pcm_info.frames = out_samples;
145 				if (mdecoder->stere_enable)
146 					pcm_info.channels = opus->channels;
147 				else
148 					pcm_info.channels = 1;
149 
150 				pcm_info.bits = 16;
151 				mdecoder->message(mdecoder->priv,
152 					PLAYER_MSG_PCM_INFO, &pcm_info);
153 			}
154 			mdecoder->running = 1;
155 		}
156 
157 		if (out_samples > OPUS_FRAME_SAMPLES_MAX) {
158 			M_LOGE("out samples %d overrange !\n", out_samples);
159 			return -1;
160 		}
161 
162 		if (out_samples * opus->channels * sizeof(short) >
163 				mdecoder->buffer_out_size) {
164 			M_LOGE("out size %d over buffer length !\n",
165 				out_samples * opus->channels * sizeof(short));
166 			return -1;
167 		}
168 
169 		/*temp = 0;
170 		ret = opus_decoder_ctl(opus->handler, OPUS_GET_SAMPLE_RATE(&temp));
171 		if (ret) {
172 			M_LOGE("get sample rate failed %d!\n", ret);
173 		} else {
174 			M_LOGI("sample rate %d\n", temp);
175 		}*/
176 
177 		for (i = 0; i < out_samples * opus->channels; i++) {
178 			sample = dec_out[i];
179 			pcm_out[2 * i] = sample & 0xff;
180 			pcm_out[2 * i + 1] = (sample >> 8) & 0xff;
181 			if (!mdecoder->stere_enable && opus->channels == 2)
182 				i++;
183 		}
184 
185 		if (mdecoder->output(mdecoder->priv,
186 				mdecoder->buffer_out,
187 				out_samples * opus->channels * sizeof(short))) {
188 			M_LOGE("output failed !\n");
189 			return -1;
190 		}
191 	}
192 
193 	return 0;
194 }
195 
opus_decode_action(void * priv,player_action_t action,void * arg)196 static int opus_decode_action(void *priv, player_action_t action, void *arg)
197 {
198 	media_decoder_t *mdecoder = (media_decoder_t *)priv;
199 	opus_decoder_t *opus;
200 	int ret;
201 
202 	if (!mdecoder) {
203 		M_LOGE("mdecoder null !\n");
204 		return -1;
205 	}
206 
207 	opus = mdecoder->decoder;
208 	if (!opus) {
209 		M_LOGE("opus decoder null !\n");
210 		return -1;
211 	}
212 
213 	if (action == PLAYER_CONFIGURE) {
214 		media_pcminfo_t *pcminfo = arg;
215 		if (!pcminfo) {
216 			M_LOGE("pcm info null !\n");
217 			return -1;
218 		}
219 
220 		if (!opus_rate_valid(pcminfo->rate)) {
221 			M_LOGE("sample rate invalid !\n");
222 			return -1;
223 		}
224 
225 		opus->rate = pcminfo->rate;
226 		opus_rate = opus->rate;
227 
228 		if (!opus_channels_valid(pcminfo->channels)) {
229 			M_LOGE("channels invalid !\n");
230 			return -1;
231 		}
232 		opus->channels = pcminfo->channels;
233 		opus_channels = opus->channels;
234 
235 		M_LOGI("rate %d channels %d\n", opus->rate, opus->channels);
236 	} else if (action == PLAYER_START ||
237 			action == PLAYER_RESUME || action == STREAM_MGR_START) {
238 		int error = -1;
239 		if (!opus_rate_valid(opus->rate)) {
240 			if (opus_rate_valid(opus_rate)) {
241 				opus->rate = opus_rate;
242 			} else {
243 				M_LOGE("no valid rate !\n");
244 				return -1;
245 			}
246 		}
247 
248 		if (!opus_channels_valid(opus->channels)) {
249 			if (opus_channels_valid(opus_channels)) {
250 				opus->channels = opus_channels;
251 			} else {
252 				M_LOGE("no valid channels !\n");
253 				return -1;
254 			}
255 		}
256 
257 		opus->handler = opus_decoder_create(opus->rate,
258 			opus->channels, &error);
259 		if (!opus->handler) {
260 			M_LOGE("create opus decoder failed !\n");
261 			return -1;
262 		}
263 		M_LOGI("opus decoder create, rate %d channels %d\n",
264 			opus->rate, opus->channels);
265 	} else if (action == PLAYER_STOP ||
266 			action == PLAYER_PAUSE || action == STREAM_MGR_STOP) {
267 		if (opus->handler) {
268 			opus_decoder_destroy(opus->handler);
269 			opus->handler = NULL;
270 			M_LOGI("opus decoder release\n");
271 		}
272 	}
273 
274 	return 0;
275 }
276 
opus_decode_create(media_decoder_t * mdecoder)277 int opus_decode_create(media_decoder_t *mdecoder)
278 {
279 	opus_decoder_t *opus;
280 
281 	if (!mdecoder) {
282 		M_LOGE("mdecoder null !\n");
283 		return -1;
284 	}
285 
286 	mdecoder->buffer_out_size =
287 		OPUS_FRAME_SAMPLES_MAX * sizeof(int16_t) * 2;
288 	M_LOGI("out buffer %u\n", mdecoder->buffer_out_size);
289 	mdecoder->buffer_out = snd_zalloc(
290 			mdecoder->buffer_out_size, AFM_EXTN);
291 	if (!mdecoder->buffer_out) {
292 		M_LOGE("alloc out buffer failed !\n");
293 		return -1;
294 	}
295 	mdecoder->input_size = 1024;
296 
297 	opus = snd_zalloc(sizeof(opus_decoder_t), AFM_MAIN);
298 	if (!opus) {
299 		M_LOGE("alloc opus decoder failed !\n");
300 		return -1;
301 	}
302 
303 	mdecoder->decoder = opus;
304 	mdecoder->decode  = opus_decode_process;
305 	mdecoder->action  = opus_decode_action;
306 	mdecoder->reset = opus_decode_reset;
307 
308 	M_LOGI("opus decoder create\n");
309 	return 0;
310 }
311 
opus_decode_release(media_decoder_t * mdecoder)312 int opus_decode_release(media_decoder_t *mdecoder)
313 {
314 	opus_decoder_t *opus;
315 
316 	if (!mdecoder) {
317 		M_LOGE("mdecoder null !\n");
318 		return -1;
319 	}
320 
321 	snd_free(mdecoder->buffer_out);
322 	mdecoder->buffer_out_size = 0;
323 	mdecoder->input_size = 0;
324 
325 	opus = mdecoder->decoder;
326 	if (!opus) {
327 		M_LOGE("opus decoder null !\n");
328 		return -1;
329 	}
330 
331 	snd_free(opus);
332 	mdecoder->decoder = NULL;
333 
334 	M_LOGI("opus decoder release\n");
335 	return 0;
336 }
337 
338