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 <stdarg.h>
11
12 #include "uvoice_os.h"
13 #include "uvoice_types.h"
14 #include "uvoice_player.h"
15 #include "uvoice_recorder.h"
16
17 #include "uvoice_config.h"
18 #include "uvoice_common.h"
19 #include "uvoice_play.h"
20 #include "uvoice_record.h"
21 #include "uvoice_codec.h"
22
23
24 #define MP3_DECODER_BUFF_SIZE 1200
25 #define AAC_DECODER_BUFF_SIZE 4096
26 #define M4A_DECODER_BUFF_SIZE 6144
27
28
mp3_decoder_create(media_decoder_t * mdecoder)29 static int mp3_decoder_create(media_decoder_t *mdecoder)
30 {
31 if (!mdecoder) {
32 M_LOGE("mdecoder null !\n");
33 return -1;
34 }
35
36 mdecoder->input_size = MP3_DECODER_BUFF_SIZE;
37
38 #ifdef DECODER_MAD_ENABLE
39 if (mdecoder->stere_enable)
40 mdecoder->buffer_out_size = 1152 * sizeof(int16_t) * 2;
41 else
42 mdecoder->buffer_out_size = 1152 * sizeof(int16_t);
43 #else
44 mdecoder->buffer_out_size = 1152 * sizeof(int16_t) * 2;
45 #endif
46
47 mdecoder->buffer_out = snd_zalloc(
48 mdecoder->buffer_out_size, AFM_EXTN);
49 if (!mdecoder->buffer_out) {
50 M_LOGE("alloc out buffer failed !\n");
51 goto __exit;
52 }
53
54 #ifdef DECODER_MAD_ENABLE
55 if (mad_decoder_create(mdecoder)) {
56 M_LOGE("create mad decoder failed !\n");
57 goto __exit1;
58 }
59 #elif (DECODER_PV_MP3_ENABLE == 1)
60 if (pvmp3_decoder_create(mdecoder)) {
61 M_LOGE("create pvmp3 decoder failed !\n");
62 goto __exit1;
63 }
64 #elif defined(DECODER_HELIX_MP3_ENABLE)
65 if (helixmp3_decoder_create(mdecoder)) {
66 M_LOGE("create helixmp3 decoder failed !\n");
67 goto __exit1;
68 }
69 #else
70 M_LOGW("MP3 decoder not enable !\n");
71 goto __exit1;
72 #endif
73
74 return 0;
75
76 __exit1:
77 mdecoder->buffer_out_size = 0;
78 snd_free(mdecoder->buffer_out);
79 mdecoder->buffer_out = NULL;
80 __exit:
81 mdecoder->input_size = 0;
82 return -1;
83 }
84
mp3_decoder_release(media_decoder_t * mdecoder)85 static int mp3_decoder_release(media_decoder_t *mdecoder)
86 {
87 if (!mdecoder) {
88 M_LOGE("mdecoder null !\n");
89 return -1;
90 }
91
92 #ifdef DECODER_MAD_ENABLE
93 mad_decoder_release(mdecoder);
94 #elif (DECODER_PV_MP3_ENABLE == 1)
95 pvmp3_decoder_release(mdecoder);
96 #elif defined(DECODER_HELIX_MP3_ENABLE)
97 helixmp3_decoder_release(mdecoder);
98 #endif
99
100 snd_free(mdecoder->buffer_out);
101 mdecoder->buffer_out = NULL;
102 mdecoder->buffer_out_size = 0;
103 mdecoder->input_size = 0;
104
105 return 0;
106 }
107
aac_decoder_create(media_decoder_t * mdecoder)108 static int aac_decoder_create(media_decoder_t *mdecoder)
109 {
110 if (!mdecoder) {
111 M_LOGE("mdecoder null !\n");
112 return -1;
113 }
114
115 mdecoder->input_size = AAC_DECODER_BUFF_SIZE;
116
117 #ifndef DECODER_FAAD2_AAC_ENABLE
118 mdecoder->buffer_out_size = 2048 * sizeof(int16_t);
119 mdecoder->buffer_out = snd_zalloc(
120 mdecoder->buffer_out_size, AFM_EXTN);
121 if (!mdecoder->buffer_out) {
122 M_LOGE("alloc out buffer failed !\n");
123 mdecoder->input_size = 0;
124 goto __exit;
125 }
126 #endif
127
128 #ifdef DECODER_FAAD2_AAC_ENABLE
129 if (faad2aac_decoder_create(mdecoder)) {
130 M_LOGE("create faad2 aac decoder failed !\n");
131 goto __exit;
132 }
133 #elif defined(DECODER_HELIX_AAC_ENABLE)
134 if (helixaac_decoder_create(mdecoder)) {
135 M_LOGE("create helix aac decoder failed !\n");
136 goto __exit1;
137 }
138 #else
139 M_LOGE("AAC decoder not enable !\n");
140 goto __exit1;
141 #endif
142
143 return 0;
144
145 __exit1:
146 mdecoder->buffer_out_size = 0;
147 snd_free(mdecoder->buffer_out);
148 mdecoder->buffer_out = NULL;
149 __exit:
150 mdecoder->input_size = 0;
151 return -1;
152 }
153
aac_decoder_release(media_decoder_t * mdecoder)154 static int aac_decoder_release(media_decoder_t *mdecoder)
155 {
156 if (!mdecoder) {
157 M_LOGE("mdecoder null !\n");
158 return -1;
159 }
160
161 #ifdef DECODER_FAAD2_AAC_ENABLE
162 faad2aac_decoder_release(mdecoder);
163 #elif defined(DECODER_HELIX_AAC_ENABLE)
164 helixaac_decoder_release(mdecoder);
165 #endif
166
167 #ifndef DECODER_FAAD2_AAC_ENABLE
168 snd_free(mdecoder->buffer_out);
169 mdecoder->buffer_out = NULL;
170 mdecoder->buffer_out_size = 0;
171 #endif
172 mdecoder->input_size = 0;
173
174 return 0;
175 }
176
m4a_decoder_create(media_decoder_t * mdecoder)177 static int m4a_decoder_create(media_decoder_t *mdecoder)
178 {
179 if (!mdecoder) {
180 M_LOGE("mdecoder null !\n");
181 return -1;
182 }
183
184 mdecoder->input_size = M4A_DECODER_BUFF_SIZE;
185
186 #ifndef DECODER_FAAD2_M4A_ENABLE
187 if (mdecoder->stere_enable)
188 mdecoder->buffer_out_size = 1024 * sizeof(int16_t) * 2;
189 else
190 mdecoder->buffer_out_size = 1024 * sizeof(int16_t);
191
192 mdecoder->buffer_out = snd_zalloc(
193 mdecoder->buffer_out_size, AFM_EXTN);
194 if (!mdecoder->buffer_out) {
195 M_LOGE("alloc out buffer failed !\n");
196 mdecoder->input_size = 0;
197 goto __exit;
198 }
199 #endif
200
201 #ifdef DECODER_FAAD2_M4A_ENABLE
202 if (faad2m4a_decoder_create(mdecoder)) {
203 M_LOGE("create faad2m4a decoder failed !\n");
204 goto __exit;
205 }
206 #elif defined(DECODER_HELIX_M4A_ENABLE)
207 if (helixm4a_decoder_create(mdecoder)) {
208 M_LOGE("create helixm4a decoder failed !\n");
209 goto __exit1;
210 }
211 #elif (DECODER_PV_M4A_ENABLE == 1)
212 if (pvm4a_decoder_create(mdecoder)) {
213 M_LOGE("create pvm4a decoder failed !\n");
214 goto __exit1;
215 }
216 printf("func %s, line %d: mdecoder->action is 0x%p\n", __FUNCTION__, __LINE__, mdecoder->action);
217 #else
218 M_LOGE("M4A decoder not enable !\n");
219 goto __exit1;
220 #endif
221
222 M_LOGD("M4A decoder create\n");
223 return 0;
224
225 __exit1:
226 mdecoder->buffer_out_size = 0;
227 snd_free(mdecoder->buffer_out);
228 mdecoder->buffer_out = NULL;
229 __exit:
230 mdecoder->input_size = 0;
231 return -1;
232 }
233
m4a_decoder_release(media_decoder_t * mdecoder)234 static int m4a_decoder_release(media_decoder_t *mdecoder)
235 {
236 if (!mdecoder) {
237 M_LOGE("mdecoder null !\n");
238 return -1;
239 }
240
241 #ifdef DECODER_FAAD2_M4A_ENABLE
242 faad2m4a_decoder_release(mdecoder);
243 #elif defined(DECODER_HELIX_M4A_ENABLE)
244 helixm4a_decoder_release(mdecoder);
245 #elif (DECODER_PV_M4A_ENABLE == 1)
246 pvm4a_decoder_release(mdecoder);
247 #endif
248
249 snd_free(mdecoder->buffer_out);
250 #ifndef DECODER_FAAD2_M4A_ENABLE
251 mdecoder->buffer_out = NULL;
252 mdecoder->buffer_out_size = 0;
253 #endif
254 mdecoder->input_size = 0;
255
256 M_LOGD("M4A decoder release\n");
257 return 0;
258 }
259
media_decoder_reset(media_decoder_t * mdecoder)260 int media_decoder_reset(media_decoder_t *mdecoder)
261 {
262 if (!mdecoder) {
263 M_LOGE("mdecoder null !\n");
264 return -1;
265 }
266
267 if (mdecoder->reset && mdecoder->reset(mdecoder))
268 M_LOGW("reset decoder failed\n");
269
270 mdecoder->unproc_size = 0;
271 mdecoder->running = 0;
272
273 return 0;
274 }
275
media_decoder_create(media_format_t format)276 media_decoder_t *media_decoder_create(media_format_t format)
277 {
278 media_decoder_t *mdecoder = snd_zalloc(
279 sizeof(media_decoder_t), AFM_EXTN);
280 if (!mdecoder) {
281 M_LOGE("alloc mdecoder failed !\n");
282 return NULL;
283 }
284
285 mdecoder->stere_enable = PLAYER_STERE_ENABLE;
286
287 switch (format) {
288 case MEDIA_FMT_MP3:
289 if (mp3_decoder_create(mdecoder)) {
290 M_LOGE("create mp3 decoder failed !\n");
291 snd_free(mdecoder);
292 return NULL;
293 }
294 mdecoder->initialized = 1;
295 break;
296 case MEDIA_FMT_WAV:
297 if (wave_decoder_create(mdecoder)) {
298 M_LOGE("create wav decoder failed !\n");
299 snd_free(mdecoder);
300 return NULL;
301 }
302 mdecoder->initialized = 1;
303 break;
304 case MEDIA_FMT_AAC:
305 if (aac_decoder_create(mdecoder)) {
306 M_LOGE("create aac decoder failed !\n");
307 snd_free(mdecoder);
308 return NULL;
309 }
310 mdecoder->initialized = 1;
311 break;
312 case MEDIA_FMT_M4A:
313 if (m4a_decoder_create(mdecoder)) {
314 M_LOGE("create m4a decoder failed !\n");
315 snd_free(mdecoder);
316 return NULL;
317 }
318 mdecoder->initialized = 1;
319 break;
320 case MEDIA_FMT_SPX:
321 #ifdef DECODER_SPEEX_ENABLE
322 if (spx_decoder_create(mdecoder)) {
323 M_LOGE("create spx decoder failed !\n");
324 snd_free(mdecoder);
325 return NULL;
326 }
327 mdecoder->initialized = 1;
328 break;
329 #else
330 M_LOGE("spx decoder not enable !\n");
331 snd_free(mdecoder);
332 return NULL;
333 #endif
334 case MEDIA_FMT_OGG:
335 #ifdef DECODER_OGG_ENABLE
336 if (ogg_decoder_create(mdecoder)) {
337 M_LOGE("create ogg decoder failed !\n");
338 snd_free(mdecoder);
339 return NULL;
340 }
341 mdecoder->initialized = 1;
342 break;
343 #else
344 M_LOGE("ogg decoder not enable !\n");
345 snd_free(mdecoder);
346 return NULL;
347 #endif
348 case MEDIA_FMT_WMA:
349 #ifdef DECODER_WMA_ENABLE
350 if (wma_decoder_create(mdecoder)) {
351 M_LOGE("create wma decoder failed !\n");
352 snd_free(mdecoder);
353 return NULL;
354 }
355 mdecoder->initialized = 1;
356 break;
357 #else
358 M_LOGE("wma decoder not enable!\n");
359 snd_free(mdecoder);
360 return NULL;
361 #endif
362 case MEDIA_FMT_OPS:
363 #ifdef DECODER_OPUS_ENABLE
364 if (opus_decode_create(mdecoder)) {
365 M_LOGE("create opus decoder failed !\n");
366 snd_free(mdecoder);
367 return NULL;
368 }
369 mdecoder->initialized = 1;
370 break;
371 #else
372 M_LOGE("opus decoder not enable !\n");
373 snd_free(mdecoder);
374 return NULL;
375 #endif
376 case MEDIA_FMT_FLAC:
377 #ifdef DECODER_FLAC_ENABLE
378 if (flac_decoder_create(mdecoder)) {
379 M_LOGE("create flac decoder failed !\n");
380 snd_free(mdecoder);
381 return NULL;
382 }
383 mdecoder->initialized = 1;
384 break;
385 #else
386 M_LOGE("flac decoder not enable !\n");
387 snd_free(mdecoder);
388 return NULL;
389 #endif
390 case MEDIA_FMT_AMR:
391 #ifdef DECODER_AMR_ENABLE
392 if (amr_decoder_create(mdecoder)) {
393 M_LOGE("create amr decoder failed !\n");
394 snd_free(mdecoder);
395 return NULL;
396 }
397 mdecoder->initialized = 1;
398 break;
399 #else
400 M_LOGE("amr decoder not enable !\n");
401 snd_free(mdecoder);
402 return NULL;
403 #endif
404 case MEDIA_FMT_AMRWB:
405 #ifdef DECODER_AMRWB_ENABLE
406 if (amrwb_decoder_create(mdecoder)) {
407 M_LOGE("create amrwb decoder failed !\n");
408 snd_free(mdecoder);
409 return NULL;
410 }
411 mdecoder->initialized = 1;
412 break;
413 #else
414 M_LOGE("amrwb decoder not enable !\n");
415 snd_free(mdecoder);
416 return NULL;
417 #endif
418 case MEDIA_FMT_PCM:
419 mdecoder->input_size = 2048;
420 break;
421 default:
422 M_LOGE("unknown format %d\n", format);
423 snd_free(mdecoder);
424 return NULL;
425 }
426
427 mdecoder->format = format;
428 return mdecoder;
429 }
430
media_decoder_release(media_decoder_t * mdecoder)431 int media_decoder_release(media_decoder_t *mdecoder)
432 {
433 if (!mdecoder) {
434 M_LOGE("mdecoder null !\n");
435 return -1;
436 }
437
438 switch (mdecoder->format) {
439 case MEDIA_FMT_MP3:
440 mp3_decoder_release(mdecoder);
441 break;
442 case MEDIA_FMT_WAV:
443 wave_decoder_release(mdecoder);
444 break;
445 case MEDIA_FMT_AAC:
446 aac_decoder_release(mdecoder);
447 break;
448 case MEDIA_FMT_M4A:
449 m4a_decoder_release(mdecoder);
450 break;
451 case MEDIA_FMT_SPX:
452 #ifdef DECODER_SPEEX_ENABLE
453 spx_decoder_release(mdecoder);
454 #endif
455 break;
456
457 case MEDIA_FMT_OGG:
458 #ifdef DECODER_OGG_ENABLE
459 ogg_decoder_release(mdecoder);
460 #endif
461 break;
462
463 case MEDIA_FMT_WMA:
464 #ifdef DECODER_WMA_ENABLE
465 wma_decoder_release(mdecoder);
466 #endif
467 break;
468
469 case MEDIA_FMT_OPS:
470 #ifdef DECODER_OPUS_ENABLE
471 opus_decode_release(mdecoder);
472 #endif
473 break;
474
475 case MEDIA_FMT_FLAC:
476 #ifdef DECODER_FLAC_ENABLE
477 flac_decoder_release(mdecoder);
478 #endif
479 break;
480
481 case MEDIA_FMT_AMR:
482 #ifdef DECODER_AMR_ENABLE
483 amr_decoder_release(mdecoder);
484 #endif
485 break;
486
487 case MEDIA_FMT_AMRWB:
488 #ifdef DECODER_AMRWB_ENABLE
489 amrwb_decoder_release(mdecoder);
490 #endif
491 break;
492 default:
493 break;
494 }
495
496 snd_free(mdecoder);
497 return 0;
498 }
499
media_encoder_header_gen(media_encoder_t * mencoder,media_pcminfo_t * pcminfo)500 int media_encoder_header_gen(media_encoder_t *mencoder,
501 media_pcminfo_t *pcminfo)
502 {
503 if (!mencoder) {
504 M_LOGE("mencoder null !\n");
505 return -1;
506 }
507
508 if (!pcminfo) {
509 M_LOGE("pcminfo null !\n");
510 return -1;
511 }
512
513 if (mencoder->header_gen &&
514 mencoder->header_gen(mencoder, pcminfo)) {
515 M_LOGE("generate header failed !\n");
516 return -1;
517 }
518
519 return 0;
520 }
521
media_encoder_create(media_format_t format)522 media_encoder_t *media_encoder_create(media_format_t format)
523 {
524 media_encoder_t *mencoder = snd_zalloc(
525 sizeof(media_encoder_t), AFM_EXTN);
526 if (!mencoder) {
527 M_LOGE("alloc media encoder failed !\n");
528 return NULL;
529 }
530
531 switch (format) {
532 case MEDIA_FMT_WAV:
533 if (wave_encoder_create(mencoder)) {
534 M_LOGE("create wave encoder failed !\n");
535 snd_free(mencoder);
536 return NULL;
537 }
538 mencoder->initialized = 1;
539 break;
540
541 case MEDIA_FMT_SPX:
542 #ifdef ENCODER_SPEEX_ENABLE
543 if (spx_encoder_create(mencoder)) {
544 M_LOGE("create speex encoder failed !\n");
545 snd_free(mencoder);
546 return NULL;
547 }
548 mencoder->initialized = 1;
549 break;
550 #else
551 M_LOGE("speex encoder not enable !\n");
552 snd_free(mencoder);
553 return NULL;
554 #endif
555
556 case MEDIA_FMT_OPS:
557 #ifdef ENCODER_OPUS_ENABLE
558 if (opus_encode_create(mencoder)) {
559 M_LOGE("create opus encoder failed !\n");
560 snd_free(mencoder);
561 return NULL;
562 }
563 mencoder->initialized = 1;
564 break;
565 #else
566 M_LOGE("opus encoder not enable !\n");
567 snd_free(mencoder);
568 return NULL;
569 #endif
570
571 case MEDIA_FMT_AMR:
572 #ifdef ENCODER_AMR_ENABLE
573 if (amr_encoder_create(mencoder)) {
574 M_LOGE("create amr encoder failed !\n");
575 snd_free(mencoder);
576 return NULL;
577 }
578 mencoder->initialized = 1;
579 break;
580 #else
581 M_LOGE("amr encoder not enable !\n");
582 snd_free(mencoder);
583 return NULL;
584 #endif
585
586 case MEDIA_FMT_AMRWB:
587 #ifdef ENCODER_AMRWB_ENABLE
588 if (amrwb_encoder_create(mencoder)) {
589 M_LOGE("create amrwb encoder failed !\n");
590 snd_free(mencoder);
591 return NULL;
592 }
593 mencoder->initialized = 1;
594 break;
595 #else
596 M_LOGE("amrwb encoder not enable !\n");
597 snd_free(mencoder);
598 return NULL;
599 #endif
600
601 case MEDIA_FMT_AAC:
602 #ifdef ENCODER_FAAC_ENABLE
603 if (faac_encoder_create(mencoder)) {
604 M_LOGE("create faac encoder failed !\n");
605 snd_free(mencoder);
606 return NULL;
607 }
608 mencoder->initialized = 1;
609 break;
610 #else
611 M_LOGE("faac encoder not enable !\n");
612 snd_free(mencoder);
613 return NULL;
614 #endif
615
616 case MEDIA_FMT_MP3:
617 #ifdef ENCODER_LAME_ENABLE
618 if (lame_encoder_create(mencoder)) {
619 M_LOGE("create lame encoder failed !\n");
620 snd_free(mencoder->buffer_out);
621 snd_free(mencoder);
622 return NULL;
623 }
624 mencoder->initialized = 1;
625 break;
626 #else
627 M_LOGE("MP3 encoder not enable !\n");
628 snd_free(mencoder);
629 return NULL;
630 #endif
631 default:
632 break;
633 }
634
635 mencoder->format = format;
636
637 return mencoder;
638 }
639
media_encoder_release(media_encoder_t * mencoder)640 int media_encoder_release(media_encoder_t *mencoder)
641 {
642 if (!mencoder) {
643 M_LOGW("mencoder null\n");
644 return -1;
645 }
646
647 switch (mencoder->format) {
648 case MEDIA_FMT_WAV:
649 wave_encoder_release(mencoder);
650 mencoder->initialized = 0;
651 break;
652
653 case MEDIA_FMT_SPX:
654 #ifdef ENCODER_SPEEX_ENABLE
655 spx_encoder_release(mencoder);
656 mencoder->initialized = 0;
657 #endif
658 break;
659
660 case MEDIA_FMT_MP3:
661 #ifdef ENCODER_LAME_ENABLE
662 lame_encoder_release(mencoder);
663 mencoder->initialized = 0;
664 #endif
665 break;
666
667 case MEDIA_FMT_AAC:
668 #ifdef ENCODER_FAAC_ENABLE
669 faac_encoder_release(mencoder);
670 mencoder->initialized = 0;
671 #endif
672 break;
673
674 case MEDIA_FMT_AMR:
675 #ifdef ENCODER_AMR_ENABLE
676 amr_encoder_release(mencoder);
677 mencoder->initialized = 0;
678 #endif
679 break;
680
681 case MEDIA_FMT_AMRWB:
682 #ifdef ENCODER_AMRWB_ENABLE
683 amrwb_encoder_release(mencoder);
684 mencoder->initialized = 0;
685 #endif
686 break;
687
688 case MEDIA_FMT_OPS:
689 #ifdef ENCODER_OPUS_ENABLE
690 opus_encode_release(mencoder);
691 mencoder->initialized = 0;
692 #endif
693 break;
694
695 default:
696 break;
697 }
698
699 snd_free(mencoder);
700 return 0;
701 }
702
703