• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "audio_aac_adp.h"
16 #include "audio_dl_adp.h"
17 
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 #include <aacdec.h>
23 #include <aacenc.h>
24 
25 #define HI_AUDIO_ASSERT(x)  \
26     {                       \
27         if ((x) != HI_TRUE) \
28             return -1;      \
29     }
30 
31 #define aac_bitrate_sel(is_mono, mono_bitrate, stereo_bitrate) ((is_mono) ? (mono_bitrate) : (stereo_bitrate))
32 
33 #define AAC_ENC_LIB_NAME            "libaacenc.so"
34 #define AAC_DEC_LIB_NAME            "libaacdec.so"
35 
36 #define AACENC_BITRATE_16K          16000
37 #define AACENC_BITRATE_24K          24000
38 #define AACENC_BITRATE_32K          32000
39 #define AACENC_BITRATE_44K          44000
40 #define AACENC_BITRATE_48K          48000
41 #define AACENC_BITRATE_64K          64000
42 #define AACENC_BITRATE_96K          96000
43 #define AACENC_BITRATE_128K         128000
44 #define AACENC_BITRATE_132K         132000
45 #define AACENC_BITRATE_144K         144000
46 #define AACENC_BITRATE_192K         192000
47 #define AACENC_BITRATE_256K         256000
48 #define AACENC_BITRATE_265K         265000
49 #define AACENC_BITRATE_288K         288000
50 #define AACENC_BITRATE_320K         320000
51 
52 #define AACENC_CHANNEL_SINGLE       1
53 #define AACENC_CHANNEL_STEREO       2
54 
55 #define DUMP_PATH_NAME_MAX_BYTES    256
56 #define DUMP_MAX_TIMES              10000
57 
58 // aac enc lib
59 typedef HI_S32 (*pHI_AACENC_GetVersion_Callback)(AACENC_VERSION_S *pVersion);
60 typedef HI_S32 (*pAACInitDefaultConfig_Callback)(AACENC_CONFIG *pstConfig);
61 typedef HI_S32 (*pAACEncoderOpen_Callback)(AAC_ENCODER_S **phAacPlusEnc, const AACENC_CONFIG *pstConfig);
62 typedef HI_S32 (*pAACEncoderFrame_Callback)(AAC_ENCODER_S *hAacPlusEnc, HI_S16 *ps16PcmBuf, HI_U8 *pu8Outbuf,
63     HI_S32 *ps32NumOutBytes);
64 typedef HI_VOID (*pAACEncoderClose_Callback)(AAC_ENCODER_S *hAacPlusEnc);
65 
66 // aac dec lib
67 typedef HI_S32 (*pHI_AACDEC_GetVersion_Callback)(AACDEC_VERSION_S *pVersion);
68 typedef HAACDecoder (*pAACInitDecoder_Callback)(AACDECTransportType enTranType);
69 typedef HI_VOID (*pAACFreeDecoder_Callback)(HAACDecoder hAACDecoder);
70 typedef HI_S32 (*pAACSetRawMode_Callback)(HAACDecoder hAACDecoder, HI_S32 nChans, HI_S32 sampRate);
71 typedef HI_S32 (*pAACDecodeFindSyncHeader_Callback)(HAACDecoder hAACDecoder, HI_U8 **ppInbufPtr, HI_S32 *pBytesLeft);
72 typedef HI_S32 (*pAACDecodeFrame_Callback)(HAACDecoder hAACDecoder, HI_U8 **ppInbufPtr, HI_S32 *pBytesLeft,
73     HI_S16 *pOutPcm);
74 typedef HI_S32 (*pAACGetLastFrameInfo_Callback)(HAACDecoder hAACDecoder, AACFrameInfo *aacFrameInfo);
75 typedef HI_S32 (*pAACDecoderSetEosFlag_Callback)(HAACDecoder hAACDecoder, HI_S32 s32Eosflag);
76 typedef HI_S32 (*pAACFlushCodec_Callback)(HAACDecoder hAACDecoder);
77 
78 typedef struct {
79     HI_S32 s32OpenCnt;
80     HI_VOID *pLibHandle;
81 
82     pHI_AACENC_GetVersion_Callback pHI_AACENC_GetVersion;
83     pAACInitDefaultConfig_Callback pAACInitDefaultConfig;
84     pAACEncoderOpen_Callback pAACEncoderOpen;
85     pAACEncoderFrame_Callback pAACEncoderFrame;
86     pAACEncoderClose_Callback pAACEncoderClose;
87 } AACENC_FUN_S;
88 
89 typedef struct {
90     HI_S32 s32OpenCnt;
91     HI_VOID *pLibHandle;
92 
93     pHI_AACDEC_GetVersion_Callback pHI_AACDEC_GetVersion;
94     pAACInitDecoder_Callback pAACInitDecoder;
95     pAACFreeDecoder_Callback pAACFreeDecoder;
96     pAACSetRawMode_Callback pAACSetRawMode;
97     pAACDecodeFindSyncHeader_Callback pAACDecodeFindSyncHeader;
98     pAACDecodeFrame_Callback pAACDecodeFrame;
99     pAACGetLastFrameInfo_Callback pAACGetLastFrameInfo;
100     pAACDecoderSetEosFlag_Callback pAACDecoderSetEosFlag;
101     pAACFlushCodec_Callback pAACFlushCodec;
102 } AACDEC_FUN_S;
103 
104 #ifdef HI_AAC_USE_STATIC_MODULE_REGISTER
105 static HI_S32 g_aenc_open_flag = 0;
106 static HI_S32 g_adec_open_flag = 0;
107 #endif
108 
109 static HI_S32 g_AacEncHandle = 0;
110 static HI_S32 g_AacDecHandle = 0;
111 
112 static AACENC_FUN_S g_stAacEncFunc = { 0 };
113 static AACDEC_FUN_S g_stAacDecFunc = { 0 };
114 
115 #if defined(HI_AAC_USE_STATIC_MODULE_REGISTER) && defined(HI_AAC_HAVE_SBR_LIB)
aac_init_sbr_enc_lib(hi_void)116 static hi_s32 aac_init_sbr_enc_lib(hi_void)
117 {
118     hi_s32 ret;
119     hi_void *sbr_enc_handle = HI_AAC_SBRENC_GetHandle();
120     ret = AACEncoderRegisterModule(sbr_enc_handle);
121     if (ret != HI_SUCCESS) {
122         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "init SbrEnc lib fail!\n");
123         return HI_ERR_AENC_NOT_SUPPORT;
124     }
125     return ret;
126 }
127 
aac_init_sbr_dec_lib(hi_void)128 static hi_s32 aac_init_sbr_dec_lib(hi_void)
129 {
130     hi_s32 ret;
131     hi_void *sbr_dec_handle = HI_AAC_SBRDEC_GetHandle();
132     ret = AACDecoderRegisterModule(sbr_dec_handle);
133     if (ret != HI_SUCCESS) {
134         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "init SbrDec lib fail!\n");
135         return HI_ERR_ADEC_NOT_SUPPORT;
136     }
137     return ret;
138 }
139 #endif
140 
141 #ifndef HI_AAC_USE_STATIC_MODULE_REGISTER
aac_enc_lib_dlsym(AACENC_FUN_S * aac_enc_func)142 static hi_s32 aac_enc_lib_dlsym(AACENC_FUN_S *aac_enc_func)
143 {
144     hi_s32 ret;
145 
146     ret = Audio_Dlsym((hi_void **)&(aac_enc_func->pHI_AACENC_GetVersion), aac_enc_func->pLibHandle,
147         "HI_AACENC_GetVersion");
148     if (ret != HI_SUCCESS) {
149         return HI_ERR_AENC_NOT_SUPPORT;
150     }
151 
152     ret = Audio_Dlsym((hi_void **)&(aac_enc_func->pAACInitDefaultConfig), aac_enc_func->pLibHandle,
153         "AACInitDefaultConfig");
154     if (ret != HI_SUCCESS) {
155         return HI_ERR_AENC_NOT_SUPPORT;
156     }
157 
158     ret = Audio_Dlsym((hi_void **)&(aac_enc_func->pAACEncoderOpen), aac_enc_func->pLibHandle, "AACEncoderOpen");
159     if (ret != HI_SUCCESS) {
160         return HI_ERR_AENC_NOT_SUPPORT;
161     }
162 
163     ret = Audio_Dlsym((hi_void **)&(aac_enc_func->pAACEncoderFrame), aac_enc_func->pLibHandle, "AACEncoderFrame");
164     if (ret != HI_SUCCESS) {
165         return HI_ERR_AENC_NOT_SUPPORT;
166     }
167 
168     ret = Audio_Dlsym((hi_void **)&(aac_enc_func->pAACEncoderClose), aac_enc_func->pLibHandle, "AACEncoderClose");
169     if (ret != HI_SUCCESS) {
170         return HI_ERR_AENC_NOT_SUPPORT;
171     }
172 
173     return HI_SUCCESS;
174 }
175 #endif
176 
InitAacAencLib(void)177 static HI_S32 InitAacAencLib(void)
178 {
179 #ifdef HI_AAC_USE_STATIC_MODULE_REGISTER
180     g_stAacEncFunc.pHI_AACENC_GetVersion = HI_AACENC_GetVersion;
181     g_stAacEncFunc.pAACInitDefaultConfig = AACInitDefaultConfig;
182     g_stAacEncFunc.pAACEncoderOpen = AACEncoderOpen;
183     g_stAacEncFunc.pAACEncoderFrame = AACEncoderFrame;
184     g_stAacEncFunc.pAACEncoderClose = AACEncoderClose;
185 #ifdef HI_AAC_HAVE_SBR_LIB
186     if (g_aenc_open_flag == 0) {
187         if (aac_init_sbr_enc_lib() != HI_SUCCESS) {
188             return HI_ERR_AENC_NOT_SUPPORT;
189         }
190         g_aenc_open_flag = 1;
191     }
192 #endif
193 #else
194     if (g_stAacEncFunc.s32OpenCnt == 0) {
195         hi_s32 ret;
196         AACENC_FUN_S aac_enc_func = { 0 };
197 
198         ret = Audio_Dlopen(&(aac_enc_func.pLibHandle), AAC_ENC_LIB_NAME);
199         if (ret != HI_SUCCESS) {
200             printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "load aenc lib fail!\n");
201             return HI_ERR_AENC_NOT_SUPPORT;
202         }
203 
204         ret = aac_enc_lib_dlsym(&aac_enc_func);
205         if (ret != HI_SUCCESS) {
206             printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "find symbol error!\n");
207             Audio_Dlclose(aac_enc_func.pLibHandle);
208             aac_enc_func.pLibHandle = HI_NULL;
209             return HI_ERR_AENC_NOT_SUPPORT;
210         }
211 
212         (hi_void)memcpy_s(&g_stAacEncFunc, sizeof(g_stAacEncFunc), &aac_enc_func, sizeof(aac_enc_func));
213     }
214     g_stAacEncFunc.s32OpenCnt++;
215 #endif
216     return HI_SUCCESS;
217 }
218 
DeInitAacAencLib(HI_VOID)219 static HI_VOID DeInitAacAencLib(HI_VOID)
220 {
221     HI_S32 s32Ret;
222 
223     if (g_stAacEncFunc.s32OpenCnt != 0) {
224         g_stAacEncFunc.s32OpenCnt--;
225     }
226 
227     if (g_stAacEncFunc.s32OpenCnt == 0) {
228 #ifndef HI_AAC_USE_STATIC_MODULE_REGISTER
229         if (g_stAacEncFunc.pLibHandle != HI_NULL) {
230             Audio_Dlclose(g_stAacEncFunc.pLibHandle);
231         }
232 #endif
233         s32Ret = memset_s(&g_stAacEncFunc, sizeof(AACENC_FUN_S), 0, sizeof(AACENC_FUN_S));
234         if (s32Ret != EOK) {
235             printf("%s: memset_s failed, ret:0x%x.\n", __FUNCTION__, s32Ret);
236             return;
237         }
238     }
239 
240     return;
241 }
242 
AACInitDefaultConfig_Adp(AACENC_CONFIG * pstConfig)243 static HI_S32 AACInitDefaultConfig_Adp(AACENC_CONFIG *pstConfig)
244 {
245     if (g_stAacEncFunc.pAACInitDefaultConfig == NULL) {
246         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
247         return HI_ERR_AENC_NOT_SUPPORT;
248     }
249     return g_stAacEncFunc.pAACInitDefaultConfig(pstConfig);
250 }
251 
AACEncoderOpen_Adp(AAC_ENCODER_S ** phAacPlusEnc,const AACENC_CONFIG * pstConfig)252 static HI_S32 AACEncoderOpen_Adp(AAC_ENCODER_S **phAacPlusEnc, const AACENC_CONFIG *pstConfig)
253 {
254     if (g_stAacEncFunc.pAACEncoderOpen == NULL) {
255         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
256         return HI_ERR_AENC_NOT_SUPPORT;
257     }
258     return g_stAacEncFunc.pAACEncoderOpen(phAacPlusEnc, pstConfig);
259 }
260 
AACEncoderFrame_Adp(AAC_ENCODER_S * hAacPlusEnc,HI_S16 * ps16PcmBuf,HI_U8 * pu8Outbuf,HI_S32 * ps32NumOutBytes)261 static HI_S32 AACEncoderFrame_Adp(AAC_ENCODER_S *hAacPlusEnc, HI_S16 *ps16PcmBuf,
262     HI_U8 *pu8Outbuf, HI_S32 *ps32NumOutBytes)
263 {
264     if (g_stAacEncFunc.pAACEncoderFrame == NULL) {
265         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
266         return HI_ERR_AENC_NOT_SUPPORT;
267     }
268     return g_stAacEncFunc.pAACEncoderFrame(hAacPlusEnc, ps16PcmBuf, pu8Outbuf, ps32NumOutBytes);
269 }
270 
AACEncoderClose_Adp(AAC_ENCODER_S * hAacPlusEnc)271 static HI_VOID AACEncoderClose_Adp(AAC_ENCODER_S *hAacPlusEnc)
272 {
273     if (g_stAacEncFunc.pAACEncoderClose == NULL) {
274         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
275         return;
276     }
277     return g_stAacEncFunc.pAACEncoderClose(hAacPlusEnc);
278 }
279 
280 #ifndef HI_AAC_USE_STATIC_MODULE_REGISTER
aac_dec_lib_dlsym(AACDEC_FUN_S * aac_dec_func)281 static hi_s32 aac_dec_lib_dlsym(AACDEC_FUN_S *aac_dec_func)
282 {
283     hi_s32 ret;
284 
285     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pHI_AACDEC_GetVersion), aac_dec_func->pLibHandle,
286         "HI_AACDEC_GetVersion");
287     if (ret != HI_SUCCESS) {
288         return HI_ERR_ADEC_NOT_SUPPORT;
289     }
290 
291     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACInitDecoder), aac_dec_func->pLibHandle, "AACInitDecoder");
292     if (ret != HI_SUCCESS) {
293         return HI_ERR_ADEC_NOT_SUPPORT;
294     }
295 
296     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACFreeDecoder), aac_dec_func->pLibHandle, "AACFreeDecoder");
297     if (ret != HI_SUCCESS) {
298         return HI_ERR_ADEC_NOT_SUPPORT;
299     }
300 
301     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACSetRawMode), aac_dec_func->pLibHandle, "AACSetRawMode");
302     if (ret != HI_SUCCESS) {
303         return HI_ERR_ADEC_NOT_SUPPORT;
304     }
305 
306     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACDecodeFindSyncHeader), aac_dec_func->pLibHandle,
307         "AACDecodeFindSyncHeader");
308     if (ret != HI_SUCCESS) {
309         return HI_ERR_ADEC_NOT_SUPPORT;
310     }
311 
312     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACDecodeFrame), aac_dec_func->pLibHandle, "AACDecodeFrame");
313     if (ret != HI_SUCCESS) {
314         return HI_ERR_ADEC_NOT_SUPPORT;
315     }
316 
317     ret =
318         Audio_Dlsym((hi_void **)&(aac_dec_func->pAACGetLastFrameInfo), aac_dec_func->pLibHandle, "AACGetLastFrameInfo");
319     if (ret != HI_SUCCESS) {
320         return HI_ERR_ADEC_NOT_SUPPORT;
321     }
322 
323     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACDecoderSetEosFlag), aac_dec_func->pLibHandle,
324         "AACDecoderSetEosFlag");
325     if (ret != HI_SUCCESS) {
326         return HI_ERR_ADEC_NOT_SUPPORT;
327     }
328 
329     ret = Audio_Dlsym((hi_void **)&(aac_dec_func->pAACFlushCodec), aac_dec_func->pLibHandle, "AACFlushCodec");
330     if (ret != HI_SUCCESS) {
331         return HI_ERR_ADEC_NOT_SUPPORT;
332     }
333 
334     return HI_SUCCESS;
335 }
336 #endif
337 
InitAacAdecLib(void)338 static HI_S32 InitAacAdecLib(void)
339 {
340 #ifdef HI_AAC_USE_STATIC_MODULE_REGISTER
341     g_stAacDecFunc.pHI_AACDEC_GetVersion = HI_AACDEC_GetVersion;
342     g_stAacDecFunc.pAACInitDecoder = AACInitDecoder;
343     g_stAacDecFunc.pAACFreeDecoder = AACFreeDecoder;
344     g_stAacDecFunc.pAACSetRawMode = AACSetRawMode;
345     g_stAacDecFunc.pAACDecodeFindSyncHeader = AACDecodeFindSyncHeader;
346     g_stAacDecFunc.pAACDecodeFrame = AACDecodeFrame;
347     g_stAacDecFunc.pAACGetLastFrameInfo = AACGetLastFrameInfo;
348     g_stAacDecFunc.pAACDecoderSetEosFlag = AACDecoderSetEosFlag;
349     g_stAacDecFunc.pAACFlushCodec = AACFlushCodec;
350 #ifdef HI_AAC_HAVE_SBR_LIB
351     if (g_adec_open_flag == 0) {
352         if (aac_init_sbr_dec_lib() != HI_SUCCESS) {
353             return HI_ERR_ADEC_NOT_SUPPORT;
354         }
355         g_adec_open_flag = 1;
356     }
357 #endif
358 #else
359 
360     if (g_stAacDecFunc.s32OpenCnt == 0) {
361         hi_s32 ret;
362         AACDEC_FUN_S aac_dec_func = { 0 };
363 
364         ret = Audio_Dlopen(&(aac_dec_func.pLibHandle), AAC_DEC_LIB_NAME);
365         if (ret != HI_SUCCESS) {
366             printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "load aenc lib fail!\n");
367             return HI_ERR_ADEC_NOT_SUPPORT;
368         }
369 
370         ret = aac_dec_lib_dlsym(&aac_dec_func);
371         if (ret != HI_SUCCESS) {
372             printf("[func]:%s [line]:%d [info]:%s\n", __FUNCTION__, __LINE__, "find symbol error!\n");
373             Audio_Dlclose(aac_dec_func.pLibHandle);
374             aac_dec_func.pLibHandle = HI_NULL;
375             return HI_ERR_ADEC_NOT_SUPPORT;
376         }
377 
378         (hi_void)memcpy_s(&g_stAacDecFunc, sizeof(g_stAacDecFunc), &aac_dec_func, sizeof(aac_dec_func));
379     }
380     g_stAacDecFunc.s32OpenCnt++;
381 #endif
382 
383     return HI_SUCCESS;
384 }
385 
DeInitAacAdecLib(HI_VOID)386 static HI_VOID DeInitAacAdecLib(HI_VOID)
387 {
388     HI_S32 s32Ret;
389 
390     if (g_stAacDecFunc.s32OpenCnt != 0) {
391         g_stAacDecFunc.s32OpenCnt--;
392     }
393 
394     if (g_stAacDecFunc.s32OpenCnt == 0) {
395 #ifdef HI_AAC_USE_STATIC_MODULE_REGISTER
396         if (g_stAacDecFunc.pLibHandle != HI_NULL) {
397             Audio_Dlclose(g_stAacDecFunc.pLibHandle);
398         }
399 #endif
400         s32Ret = memset_s(&g_stAacDecFunc, sizeof(AACDEC_FUN_S), 0, sizeof(AACDEC_FUN_S));
401         if (s32Ret != EOK) {
402             printf("%s: memset_s failed, ret:0x%x.\n", __FUNCTION__, s32Ret);
403             return;
404         }
405     }
406 
407     return;
408 }
409 
AACInitDecoder_Adp(AACDECTransportType enTranType)410 static HAACDecoder AACInitDecoder_Adp(AACDECTransportType enTranType)
411 {
412     if (g_stAacDecFunc.pAACInitDecoder == NULL) {
413         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
414         return HI_NULL;
415     }
416     return g_stAacDecFunc.pAACInitDecoder(enTranType);
417 }
418 
AACFreeDecoder_Adp(HAACDecoder hAACDecoder)419 static HI_VOID AACFreeDecoder_Adp(HAACDecoder hAACDecoder)
420 {
421     if (g_stAacDecFunc.pAACFreeDecoder == NULL) {
422         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
423         return;
424     }
425     return g_stAacDecFunc.pAACFreeDecoder(hAACDecoder);
426 }
427 
AACDecodeFindSyncHeader_Adp(HAACDecoder hAACDecoder,HI_U8 ** ppInbufPtr,HI_S32 * pBytesLeft)428 static HI_S32 AACDecodeFindSyncHeader_Adp(HAACDecoder hAACDecoder, HI_U8 **ppInbufPtr, HI_S32 *pBytesLeft)
429 {
430     if (g_stAacDecFunc.pAACDecodeFindSyncHeader == NULL) {
431         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
432         return HI_ERR_ADEC_NOT_SUPPORT;
433     }
434     return g_stAacDecFunc.pAACDecodeFindSyncHeader(hAACDecoder, ppInbufPtr, pBytesLeft);
435 }
436 
AACDecodeFrame_Adp(HAACDecoder hAACDecoder,HI_U8 ** ppInbufPtr,HI_S32 * pBytesLeft,HI_S16 * pOutPcm)437 static HI_S32 AACDecodeFrame_Adp(HAACDecoder hAACDecoder, HI_U8 **ppInbufPtr, HI_S32 *pBytesLeft, HI_S16 *pOutPcm)
438 {
439     if (g_stAacDecFunc.pAACDecodeFrame == NULL) {
440         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
441         return HI_ERR_ADEC_NOT_SUPPORT;
442     }
443     return g_stAacDecFunc.pAACDecodeFrame(hAACDecoder, ppInbufPtr, pBytesLeft, pOutPcm);
444 }
445 
AACGetLastFrameInfo_Adp(HAACDecoder hAACDecoder,AACFrameInfo * aacFrameInfo)446 static HI_S32 AACGetLastFrameInfo_Adp(HAACDecoder hAACDecoder, AACFrameInfo *aacFrameInfo)
447 {
448     if (g_stAacDecFunc.pAACGetLastFrameInfo == NULL) {
449         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "call aac function fail!\n");
450         return HI_ERR_ADEC_NOT_SUPPORT;
451     }
452     return g_stAacDecFunc.pAACGetLastFrameInfo(hAACDecoder, aacFrameInfo);
453 }
454 
AencCheckAACAttr(const AENC_ATTR_AAC_S * pstAACAttr)455 static HI_S32 AencCheckAACAttr(const AENC_ATTR_AAC_S *pstAACAttr)
456 {
457     if (pstAACAttr->enBitWidth != AUDIO_BIT_WIDTH_16) {
458         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "invalid bitwidth for AAC encoder");
459         return HI_ERR_AENC_ILLEGAL_PARAM;
460     }
461 
462     if (pstAACAttr->enSoundMode >= AUDIO_SOUND_MODE_BUTT) {
463         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "invalid sound mode for AAC encoder");
464         return HI_ERR_AENC_ILLEGAL_PARAM;
465     }
466 
467     if ((pstAACAttr->enAACType == AAC_TYPE_EAACPLUS) && (pstAACAttr->enSoundMode != AUDIO_SOUND_MODE_STEREO)) {
468         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "invalid sound mode for AAC encoder");
469         return HI_ERR_AENC_ILLEGAL_PARAM;
470     }
471 
472     if (pstAACAttr->enTransType == AAC_TRANS_TYPE_ADTS) {
473         if ((pstAACAttr->enAACType == AAC_TYPE_AACLD) || (pstAACAttr->enAACType == AAC_TYPE_AACELD)) {
474             printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__,
475                 "AACLD or AACELD not support AAC_TRANS_TYPE_ADTS");
476             return HI_ERR_AENC_ILLEGAL_PARAM;
477         }
478     }
479 
480     return HI_SUCCESS;
481 }
482 
aenc_get_aaclc_bitrate(const AACENC_CONFIG * config,hi_s32 * min_rate,hi_s32 * max_rate,hi_s32 * recommemd_rate)483 static hi_s32 aenc_get_aaclc_bitrate(const AACENC_CONFIG *config, hi_s32 *min_rate,
484                                      hi_s32 *max_rate, hi_s32 *recommemd_rate)
485 {
486     hi_bool chn_single = (config->nChannelsIn == AACENC_CHANNEL_SINGLE) ? HI_TRUE : HI_FALSE;
487 
488     if (config->sampleRate == AUDIO_SAMPLE_RATE_32000) {
489         *min_rate = AACENC_BITRATE_32K;
490         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_192K, AACENC_BITRATE_320K);
491         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_128K);
492     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_44100) {
493         *min_rate = AACENC_BITRATE_48K;
494         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_265K, AACENC_BITRATE_320K);
495         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
496     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_48000) {
497         *min_rate = AACENC_BITRATE_48K;
498         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_288K, AACENC_BITRATE_320K);
499         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
500     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_16000) {
501         *min_rate = AACENC_BITRATE_24K;
502         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_96K, AACENC_BITRATE_192K);
503         *recommemd_rate = AACENC_BITRATE_48K;
504     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_8000) {
505         *min_rate = AACENC_BITRATE_16K;
506         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
507         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_24K, AACENC_BITRATE_32K);
508     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_24000) {
509         *min_rate = AACENC_BITRATE_32K;
510         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_144K, AACENC_BITRATE_288K);
511         *recommemd_rate = AACENC_BITRATE_48K;
512     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_22050) {
513         *min_rate = AACENC_BITRATE_32K;
514         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_132K, AACENC_BITRATE_265K);
515         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_48K);
516     } else {
517         printf("AACLC invalid samplerate(%d)\n", config->sampleRate);
518         return HI_ERR_AENC_ILLEGAL_PARAM;
519     }
520 
521     return HI_SUCCESS;
522 }
523 
aenc_check_aaclc_config(const AACENC_CONFIG * config)524 static hi_s32 aenc_check_aaclc_config(const AACENC_CONFIG *config)
525 {
526     hi_s32 min_bit_rate = 0;
527     hi_s32 max_bit_rate = 0;
528     hi_s32 recommemd_rate = 0;
529     hi_s32 ret;
530 
531     if (config->coderFormat == AACLC) {
532         if (config->nChannelsOut != config->nChannelsIn) {
533             printf("AACLC nChannelsOut(%d) in not equal to nChannelsIn(%d)\n",
534                 config->nChannelsOut, config->nChannelsIn);
535             return HI_ERR_AENC_ILLEGAL_PARAM;
536         }
537 
538         ret = aenc_get_aaclc_bitrate(config, &min_bit_rate, &max_bit_rate, &recommemd_rate);
539         if (ret != HI_SUCCESS) {
540             return ret;
541         }
542 
543         if ((config->bitRate < min_bit_rate) || (config->bitRate > max_bit_rate)) {
544             printf("AACLC %d Hz bitRate(%d) should be %d ~ %d, recommend %d\n",
545                 config->sampleRate, config->bitRate, min_bit_rate, max_bit_rate, recommemd_rate);
546             return HI_ERR_AENC_ILLEGAL_PARAM;
547         }
548     } else {
549         return HI_ERR_AENC_ILLEGAL_PARAM;
550     }
551 
552     return HI_SUCCESS;
553 }
554 
aenc_get_eaac_bitrate(const AACENC_CONFIG * config,hi_s32 * min_rate,hi_s32 * max_rate,hi_s32 * recommemd_rate)555 static hi_s32 aenc_get_eaac_bitrate(const AACENC_CONFIG *config, hi_s32 *min_rate,
556                                     hi_s32 *max_rate, hi_s32 *recommemd_rate)
557 {
558     hi_bool chn_single = (config->nChannelsIn == AACENC_CHANNEL_SINGLE) ? HI_TRUE : HI_FALSE;
559 
560     if (config->sampleRate == AUDIO_SAMPLE_RATE_32000) {
561         *min_rate = AACENC_BITRATE_32K;
562         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
563         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
564     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_44100) {
565         *min_rate = AACENC_BITRATE_32K;
566         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
567         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
568     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_48000) {
569         *min_rate = AACENC_BITRATE_32K;
570         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
571         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
572     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_16000) {
573         *min_rate = AACENC_BITRATE_24K;
574         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
575         *recommemd_rate = AACENC_BITRATE_32K;
576     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_22050) {
577         *min_rate = AACENC_BITRATE_32K;
578         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
579         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
580     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_24000) {
581         *min_rate = AACENC_BITRATE_32K;
582         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
583         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
584     } else {
585         printf("EAAC invalid samplerate(%d)\n", config->sampleRate);
586         return HI_ERR_AENC_ILLEGAL_PARAM;
587     }
588 
589     return HI_SUCCESS;
590 }
591 
aenc_check_eaac_config(const AACENC_CONFIG * config)592 static hi_s32 aenc_check_eaac_config(const AACENC_CONFIG *config)
593 {
594     hi_s32 min_bit_rate = 0;
595     hi_s32 max_bit_rate = 0;
596     hi_s32 recommemd_rate = 0;
597     hi_s32 ret;
598 
599     if (config->coderFormat == EAAC) {
600         if (config->nChannelsOut != config->nChannelsIn) {
601             printf("EAAC nChannelsOut(%d) is not equal to nChannelsIn(%d)\n",
602                 config->nChannelsOut, config->nChannelsIn);
603             return HI_ERR_AENC_ILLEGAL_PARAM;
604         }
605 
606         ret = aenc_get_eaac_bitrate(config, &min_bit_rate, &max_bit_rate, &recommemd_rate);
607         if (ret != HI_SUCCESS) {
608             return ret;
609         }
610 
611         if ((config->bitRate < min_bit_rate) || (config->bitRate > max_bit_rate)) {
612             printf("EAAC %d Hz bitRate(%d) should be %d ~ %d, recommend %d\n",
613                 config->sampleRate, config->bitRate, min_bit_rate, max_bit_rate, recommemd_rate);
614             return HI_ERR_AENC_ILLEGAL_PARAM;
615         }
616     } else {
617         return HI_ERR_AENC_ILLEGAL_PARAM;
618     }
619 
620     return HI_SUCCESS;
621 }
622 
aenc_get_eaacplus_bitrate(const AACENC_CONFIG * config,hi_s32 * min_rate,hi_s32 * max_rate,hi_s32 * recommemd_rate)623 static hi_s32 aenc_get_eaacplus_bitrate(const AACENC_CONFIG *config, hi_s32 *min_rate,
624                                         hi_s32 *max_rate, hi_s32 *recommemd_rate)
625 {
626     if (config->sampleRate == AUDIO_SAMPLE_RATE_32000) {
627         *min_rate = AACENC_BITRATE_16K;
628         *max_rate = AACENC_BITRATE_64K;
629         *recommemd_rate = AACENC_BITRATE_32K;
630     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_44100) {
631         *min_rate = AACENC_BITRATE_16K;
632         *max_rate = AACENC_BITRATE_64K;
633         *recommemd_rate = AACENC_BITRATE_48K;
634     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_48000) {
635         *min_rate = AACENC_BITRATE_16K;
636         *max_rate = AACENC_BITRATE_64K;
637         *recommemd_rate = AACENC_BITRATE_48K;
638     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_16000) {
639         *min_rate = AACENC_BITRATE_16K;
640         *max_rate = AACENC_BITRATE_48K;
641         *recommemd_rate = AACENC_BITRATE_32K;
642     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_22050) {
643         *min_rate = AACENC_BITRATE_16K;
644         *max_rate = AACENC_BITRATE_64K;
645         *recommemd_rate = AACENC_BITRATE_32K;
646     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_24000) {
647         *min_rate = AACENC_BITRATE_16K;
648         *max_rate = AACENC_BITRATE_64K;
649         *recommemd_rate = AACENC_BITRATE_32K;
650     } else {
651         printf("EAACPLUS invalid samplerate(%d)\n", config->sampleRate);
652         return HI_ERR_AENC_ILLEGAL_PARAM;
653     }
654 
655     return HI_SUCCESS;
656 }
657 
aenc_check_eaacplus_config(const AACENC_CONFIG * config)658 static hi_s32 aenc_check_eaacplus_config(const AACENC_CONFIG *config)
659 {
660     hi_s32 min_bit_rate = 0;
661     hi_s32 max_bit_rate = 0;
662     hi_s32 recommemd_rate = 0;
663     hi_s32 ret;
664 
665     if (config->coderFormat == EAACPLUS) {
666         if ((config->nChannelsOut != AACENC_CHANNEL_STEREO) ||
667             (config->nChannelsIn != AACENC_CHANNEL_STEREO)) {
668             printf("EAACPLUS nChannelsOut(%d) and nChannelsIn(%d) should be 2\n",
669                 config->nChannelsOut, config->nChannelsIn);
670             return HI_ERR_AENC_ILLEGAL_PARAM;
671         }
672 
673         ret = aenc_get_eaacplus_bitrate(config, &min_bit_rate, &max_bit_rate, &recommemd_rate);
674         if (ret != HI_SUCCESS) {
675             return ret;
676         }
677 
678         if ((config->bitRate < min_bit_rate) || (config->bitRate > max_bit_rate)) {
679             printf("EAACPLUS %d Hz bitRate(%d) should be %d ~ %d, recommend %d\n",
680                 config->sampleRate, config->bitRate, min_bit_rate, max_bit_rate, recommemd_rate);
681             return HI_ERR_AENC_ILLEGAL_PARAM;
682         }
683     } else {
684         return HI_ERR_AENC_ILLEGAL_PARAM;
685     }
686 
687     return HI_SUCCESS;
688 }
689 
aenc_get_aacld_bitrate(const AACENC_CONFIG * config,hi_s32 * min_rate,hi_s32 * max_rate,hi_s32 * recommemd_rate)690 static hi_s32 aenc_get_aacld_bitrate(const AACENC_CONFIG *config, hi_s32 *min_rate,
691                                      hi_s32 *max_rate, hi_s32 *recommemd_rate)
692 {
693     hi_bool chn_single = (config->nChannelsIn == AACENC_CHANNEL_SINGLE) ? HI_TRUE : HI_FALSE;
694 
695     if (config->sampleRate == AUDIO_SAMPLE_RATE_32000) {
696         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_64K);
697         *max_rate = AACENC_BITRATE_320K;
698         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
699     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_44100) {
700         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_44K);
701         *max_rate = AACENC_BITRATE_320K;
702         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_128K, AACENC_BITRATE_256K);
703     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_48000) {
704         *min_rate = AACENC_BITRATE_64K;
705         *max_rate = AACENC_BITRATE_320K;
706         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_128K, AACENC_BITRATE_256K);
707     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_16000) {
708         *min_rate = (chn_single) ? AACENC_BITRATE_24K : AACENC_BITRATE_32K;
709         *max_rate = (chn_single) ? AACENC_BITRATE_192K : AACENC_BITRATE_320K;
710         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
711     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_8000) {
712         *min_rate = AACENC_BITRATE_16K;
713         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_96K, AACENC_BITRATE_192K);
714         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_24K, AACENC_BITRATE_48K);
715     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_24000) {
716         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_32K, AACENC_BITRATE_48K);
717         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_256K, AACENC_BITRATE_320K);
718         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
719     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_22050) {
720         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_32K, AACENC_BITRATE_48K);
721         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_256K, AACENC_BITRATE_320K);
722         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
723     } else {
724         printf("AACLD invalid samplerate(%d)\n", config->sampleRate);
725         return HI_ERR_AENC_ILLEGAL_PARAM;
726     }
727 
728     return HI_SUCCESS;
729 }
730 
aenc_check_aacld_config(const AACENC_CONFIG * config)731 static hi_s32 aenc_check_aacld_config(const AACENC_CONFIG *config)
732 {
733     hi_s32 min_bit_rate = 0;
734     hi_s32 max_bit_rate = 0;
735     hi_s32 recommemd_rate = 0;
736     hi_s32 ret;
737 
738     if (config->coderFormat == AACLD) {
739         if (config->nChannelsOut != config->nChannelsIn) {
740             printf("AACLD nChannelsOut(%d) in not equal to nChannelsIn(%d)\n",
741                 config->nChannelsOut, config->nChannelsIn);
742             return HI_ERR_AENC_ILLEGAL_PARAM;
743         }
744 
745         ret = aenc_get_aacld_bitrate(config, &min_bit_rate, &max_bit_rate, &recommemd_rate);
746         if (ret != HI_SUCCESS) {
747             return ret;
748         }
749 
750         if ((config->bitRate < min_bit_rate) || (config->bitRate > max_bit_rate)) {
751             printf("AACLD %d Hz bitRate(%d) should be %d ~ %d, recommend %d\n",
752                 config->sampleRate, config->bitRate, min_bit_rate, max_bit_rate, recommemd_rate);
753             return HI_ERR_AENC_ILLEGAL_PARAM;
754         }
755     } else {
756         return HI_ERR_AENC_ILLEGAL_PARAM;
757     }
758 
759     return HI_SUCCESS;
760 }
761 
aenc_get_aaceld_bitrate(const AACENC_CONFIG * config,hi_s32 * min_rate,hi_s32 * max_rate,hi_s32 * recommemd_rate)762 static hi_s32 aenc_get_aaceld_bitrate(const AACENC_CONFIG *config, hi_s32 *min_rate,
763                                       hi_s32 *max_rate, hi_s32 *recommemd_rate)
764 {
765     hi_bool chn_single = (config->nChannelsIn == AACENC_CHANNEL_SINGLE) ? HI_TRUE : HI_FALSE;
766 
767     if (config->sampleRate == AUDIO_SAMPLE_RATE_32000) {
768         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_32K, AACENC_BITRATE_64K);
769         *max_rate = AACENC_BITRATE_320K;
770         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
771     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_44100) {
772         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_96K, AACENC_BITRATE_192K);
773         *max_rate = AACENC_BITRATE_320K;
774         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_128K, AACENC_BITRATE_256K);
775     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_48000) {
776         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_96K, AACENC_BITRATE_192K);
777         *max_rate = AACENC_BITRATE_320K;
778         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_128K, AACENC_BITRATE_256K);
779     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_16000) {
780         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_16K, AACENC_BITRATE_32K);
781         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_256K, AACENC_BITRATE_320K);
782         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
783     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_8000) {
784         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_32K, AACENC_BITRATE_64K);
785         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_96K, AACENC_BITRATE_192K);
786         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_32K, AACENC_BITRATE_64K);
787     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_24000) {
788         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_24K, AACENC_BITRATE_32K);
789         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_256K, AACENC_BITRATE_320K);
790         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_64K, AACENC_BITRATE_128K);
791     } else if (config->sampleRate == AUDIO_SAMPLE_RATE_22050) {
792         *min_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_24K, AACENC_BITRATE_32K);
793         *max_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_256K, AACENC_BITRATE_320K);
794         *recommemd_rate = aac_bitrate_sel(chn_single, AACENC_BITRATE_48K, AACENC_BITRATE_96K);
795     } else {
796         printf("AACELD invalid samplerate(%d)\n", config->sampleRate);
797         return HI_ERR_AENC_ILLEGAL_PARAM;
798     }
799 
800     return HI_SUCCESS;
801 }
802 
aenc_check_aaceld_config(const AACENC_CONFIG * config)803 static hi_s32 aenc_check_aaceld_config(const AACENC_CONFIG *config)
804 {
805     hi_s32 min_bit_rate = 0;
806     hi_s32 max_bit_rate = 0;
807     hi_s32 recommemd_rate = 0;
808     hi_s32 ret;
809 
810     if (config->coderFormat == AACELD) {
811         if (config->nChannelsOut != config->nChannelsIn) {
812             printf("AACELD nChannelsOut(%d) in not equal to nChannelsIn(%d)\n",
813                 config->nChannelsOut, config->nChannelsIn);
814             return HI_ERR_AENC_ILLEGAL_PARAM;
815         }
816 
817         ret = aenc_get_aaceld_bitrate(config, &min_bit_rate, &max_bit_rate, &recommemd_rate);
818         if (ret != HI_SUCCESS) {
819             return ret;
820         }
821 
822         if ((config->bitRate < min_bit_rate) || (config->bitRate > max_bit_rate)) {
823             printf("AACELD %d Hz bitRate(%d) should be %d ~ %d, recommend %d\n",
824                 config->sampleRate, config->bitRate, min_bit_rate, max_bit_rate, recommemd_rate);
825             return HI_ERR_AENC_ILLEGAL_PARAM;
826         }
827     } else {
828         return HI_ERR_AENC_ILLEGAL_PARAM;
829     }
830 
831     return HI_SUCCESS;
832 }
833 
aenc_aac_check_coder_format(AuEncoderFormat coderFormat)834 static hi_s32 aenc_aac_check_coder_format(AuEncoderFormat coderFormat)
835 {
836     if (coderFormat != AACLC && coderFormat != EAAC &&
837         coderFormat != EAACPLUS && coderFormat != AACLD &&
838         coderFormat != AACELD) {
839         printf("aacenc coderFormat(%d) invalid\n", coderFormat);
840         return HI_ERR_AENC_ILLEGAL_PARAM;
841     }
842 
843     return HI_SUCCESS;
844 }
845 
aenc_aac_check_config(const AACENC_CONFIG * config)846 static hi_s32 aenc_aac_check_config(const AACENC_CONFIG *config)
847 {
848     hi_s32 ret;
849 
850     if (config == NULL) {
851         printf("[func]:%s [line]:%d [info]:%s\n", __FUNCTION__, __LINE__, "config is null");
852         return HI_ERR_AENC_NULL_PTR;
853     }
854 
855     if (aenc_aac_check_coder_format(config->coderFormat) != HI_SUCCESS) {
856         return HI_ERR_AENC_ILLEGAL_PARAM;
857     }
858 
859     if (config->quality != AU_QualityExcellent && config->quality != AU_QualityHigh &&
860         config->quality != AU_QualityMedium && config->quality != AU_QualityLow) {
861         printf("aacenc quality(%d) invalid\n", config->quality);
862         return HI_ERR_AENC_ILLEGAL_PARAM;
863     }
864 
865     if (config->bitsPerSample != 16) { /* 16: 16bit */
866         printf("aacenc bitsPerSample(%d) should be 16\n", config->bitsPerSample);
867         return HI_ERR_AENC_ILLEGAL_PARAM;
868     }
869 
870     if ((config->transtype < 0) || (config->transtype > AACENC_LATM_MCP1)) {
871         printf("invalid transtype(%d), not in [0, 2]\n", config->transtype);
872         return HI_ERR_AENC_ILLEGAL_PARAM;
873     }
874 
875     if ((config->bandWidth != 0) && ((config->bandWidth < 1000) || /* 1000: band width */
876         (config->bandWidth > config->sampleRate / 2))) { /* 2: half */
877         printf("AAC bandWidth(%d) should be 0, or 1000 ~ %d\n",
878             config->bandWidth, config->sampleRate / 2); /* 2: half */
879         return HI_ERR_AENC_ILLEGAL_PARAM;
880     }
881 
882     if (config->coderFormat == AACLC) {
883         ret = aenc_check_aaclc_config(config);
884     } else if (config->coderFormat == EAAC) {
885         ret = aenc_check_eaac_config(config);
886     } else if (config->coderFormat == EAACPLUS) {
887         ret = aenc_check_eaacplus_config(config);
888     } else if (config->coderFormat == AACLD) {
889         ret = aenc_check_aacld_config(config);
890     } else if (config->coderFormat == AACELD) {
891         ret = aenc_check_aaceld_config(config);
892     } else {
893         ret = HI_FAILURE;
894     }
895 
896     return ret;
897 }
898 
aenc_init_aac_config(AACENC_CONFIG * config,const AENC_ATTR_AAC_S * attr)899 static hi_s32 aenc_init_aac_config(AACENC_CONFIG *config, const AENC_ATTR_AAC_S *attr)
900 {
901     hi_s32 ret;
902 
903     /* set default config to encoder */
904     ret = AACInitDefaultConfig_Adp(config);
905     if (ret) {
906         printf("[Func]:%s [Line]:%d ret:0x%x.#########\n", __FUNCTION__, __LINE__, ret);
907         return ret;
908     }
909 
910     config->coderFormat = (AuEncoderFormat)attr->enAACType;
911     config->bitRate = attr->enBitRate;
912     config->bitsPerSample = 8 * (1 << (HI_U32)(attr->enBitWidth)); /* 8: 1byte = 8bits */
913     config->sampleRate = attr->enSmpRate;
914     config->bandWidth = attr->s16BandWidth;
915     config->transtype = (AACENCTransportType)attr->enTransType;
916 
917     if (attr->enSoundMode == AUDIO_SOUND_MODE_MONO && attr->enAACType != AAC_TYPE_EAACPLUS) {
918         config->nChannelsIn = AACENC_CHANNEL_SINGLE;
919         config->nChannelsOut = AACENC_CHANNEL_SINGLE;
920     } else {
921         config->nChannelsIn = AACENC_CHANNEL_STEREO;
922         config->nChannelsOut = AACENC_CHANNEL_STEREO;
923     }
924 
925     config->quality = AU_QualityHigh;
926 
927     ret = aenc_aac_check_config(config);
928     if (ret) {
929         printf("[Func]:%s [Line]:%d #########\n", __FUNCTION__, __LINE__);
930         return HI_ERR_AENC_ILLEGAL_PARAM;
931     }
932 
933     return HI_SUCCESS;
934 }
935 
OpenAACEncoder(HI_VOID * pEncoderAttr,HI_VOID ** ppEncoder)936 static HI_S32 OpenAACEncoder(HI_VOID *pEncoderAttr, HI_VOID **ppEncoder)
937 {
938     AENC_AAC_ENCODER_S *pstEncoder = NULL;
939     AENC_ATTR_AAC_S *pstAttr = NULL;
940     HI_S32 s32Ret;
941     AACENC_CONFIG config = {0};
942 
943     HI_AUDIO_ASSERT(pEncoderAttr != NULL);
944     HI_AUDIO_ASSERT(ppEncoder != NULL);
945 
946     /* check attribute of encoder */
947     pstAttr = (AENC_ATTR_AAC_S *)pEncoderAttr;
948     s32Ret = AencCheckAACAttr(pstAttr);
949     if (s32Ret) {
950         printf("[Func]:%s [Line]:%d s32Ret:0x%x.#########\n", __FUNCTION__, __LINE__, s32Ret);
951         return s32Ret;
952     }
953 
954     /* allocate memory for encoder */
955     pstEncoder = (AENC_AAC_ENCODER_S *)malloc(sizeof(AENC_AAC_ENCODER_S));
956     if (pstEncoder == NULL) {
957         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "no memory");
958         return HI_ERR_AENC_NOMEM;
959     }
960     (hi_void)memset_s(pstEncoder, sizeof(AENC_AAC_ENCODER_S), 0, sizeof(AENC_AAC_ENCODER_S));
961 
962     *ppEncoder = (HI_VOID *)pstEncoder;
963 
964     s32Ret = aenc_init_aac_config(&config, pstAttr);
965     if (s32Ret) {
966         free(pstEncoder);
967         pstEncoder = NULL;
968         printf("[Func]:%s [Line]:%d s32Ret:0x%x.#########\n", __FUNCTION__, __LINE__, s32Ret);
969         return s32Ret;
970     }
971 
972     /* create encoder */
973     s32Ret = AACEncoderOpen_Adp(&pstEncoder->pstAACState, &config);
974     if (s32Ret) {
975         free(pstEncoder);
976         pstEncoder = NULL;
977         printf("[Func]:%s [Line]:%d s32Ret:0x%x.#########\n", __FUNCTION__, __LINE__, s32Ret);
978         return s32Ret;
979     }
980 
981     s32Ret = memcpy_s(&pstEncoder->stAACAttr, sizeof(pstEncoder->stAACAttr), pstAttr, sizeof(AENC_ATTR_AAC_S));
982     if (s32Ret != EOK) {
983         free(pstEncoder);
984         pstEncoder = NULL;
985         printf("%s: memcpy_s failed, ret:0x%x.\n", __FUNCTION__, s32Ret);
986         return s32Ret;
987     }
988 
989     return HI_SUCCESS;
990 }
991 
encoder_get_water_line(const AENC_AAC_ENCODER_S * encoder,hi_u32 * water_line)992 static hi_s32 encoder_get_water_line(const AENC_AAC_ENCODER_S *encoder, hi_u32 *water_line)
993 {
994     if (encoder->stAACAttr.enAACType == AAC_TYPE_AACLC) {
995         *water_line = AACLC_SAMPLES_PER_FRAME;
996     } else if (encoder->stAACAttr.enAACType == AAC_TYPE_EAAC ||
997         encoder->stAACAttr.enAACType == AAC_TYPE_EAACPLUS) {
998         *water_line = AACPLUS_SAMPLES_PER_FRAME;
999     } else if (encoder->stAACAttr.enAACType == AAC_TYPE_AACLD ||
1000         encoder->stAACAttr.enAACType == AAC_TYPE_AACELD) {
1001         *water_line = AACLD_SAMPLES_PER_FRAME;
1002     } else {
1003         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "invalid AAC coder type");
1004         return HI_ERR_AENC_ILLEGAL_PARAM;
1005     }
1006 
1007     return HI_SUCCESS;
1008 }
1009 
EncodeAACFrm(HI_VOID * pEncoder,const AUDIO_FRAME_S * pstData,HI_U8 * pu8Outbuf,HI_U32 * pu32OutLen)1010 static HI_S32 EncodeAACFrm(HI_VOID *pEncoder, const AUDIO_FRAME_S *pstData, HI_U8 *pu8Outbuf, HI_U32 *pu32OutLen)
1011 {
1012     HI_S32 s32Ret, i;
1013     AENC_AAC_ENCODER_S *pstEncoder = NULL;
1014     HI_U32 u32PtNums, u32WaterLine;
1015     HI_S16 aData[AACENC_BLOCKSIZE * 2 * MAX_CHANNELS]; /* 2: stereo */
1016     HI_S16 s16Len;
1017 
1018     HI_AUDIO_ASSERT(pEncoder != NULL);
1019     HI_AUDIO_ASSERT(pstData != NULL);
1020     HI_AUDIO_ASSERT(pu8Outbuf != NULL);
1021     HI_AUDIO_ASSERT(pu32OutLen != NULL);
1022 
1023     pstEncoder = (AENC_AAC_ENCODER_S *)pEncoder;
1024 
1025     if (pstEncoder->stAACAttr.enSoundMode == AUDIO_SOUND_MODE_STEREO &&
1026         pstData->enSoundmode != AUDIO_SOUND_MODE_STEREO) {
1027         /* whether the sound mode of frame and channel is match  */
1028         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__,
1029             "AAC encode receive a frame which not match its Soundmode");
1030         return HI_ERR_AENC_ILLEGAL_PARAM;
1031     }
1032 
1033     /* WaterLine, equals to the frame sample frame of protocol */
1034     s32Ret = encoder_get_water_line(pstEncoder, &u32WaterLine);
1035     if (s32Ret != HI_SUCCESS) {
1036         return s32Ret;
1037     }
1038 
1039     /* calculate point number */
1040     u32PtNums = pstData->u32Len / (pstData->enBitwidth + 1);
1041 
1042     /* if frame sample larger than protocol sample, reject to receive, or buffer will be overflow */
1043     if (u32PtNums != u32WaterLine) {
1044         printf("[Func]:%s [Line]:%d [Info]:invalid u32PtNums%d for AACType:%d\n", __FUNCTION__, __LINE__, u32PtNums,
1045             pstEncoder->stAACAttr.enAACType);
1046         return HI_ERR_AENC_ILLEGAL_PARAM;
1047     }
1048 
1049     /* AAC encoder need interleaved data,here change LLLRRR to LRLRLR.
1050        AACLC will encode 1024*2 point, and AACplus encode 2048*2 point */
1051     if (AUDIO_SOUND_MODE_STEREO == pstEncoder->stAACAttr.enSoundMode) {
1052         s16Len = u32WaterLine;
1053         for (i = s16Len - 1; i >= 0; i--) {
1054             aData[AACENC_CHANNEL_STEREO * i] = *((HI_S16 *)pstData->u64VirAddr[0] + i);
1055             aData[AACENC_CHANNEL_STEREO * i + 1] = *((HI_S16 *)pstData->u64VirAddr[1] + i);
1056         }
1057     } else { /* if inbuf is momo, copy left to right */
1058         HI_S16 *temp = (HI_S16 *)pstData->u64VirAddr[0];
1059 
1060         s16Len = u32WaterLine;
1061         for (i = s16Len - 1; i >= 0; i--) {
1062             aData[i] = *(temp + i);
1063         }
1064     }
1065 
1066     s32Ret = AACEncoderFrame_Adp(pstEncoder->pstAACState, aData, pu8Outbuf, (HI_S32 *)pu32OutLen);
1067     if (s32Ret != HI_SUCCESS) {
1068         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "AAC encode failed");
1069     }
1070 
1071     return s32Ret;
1072 }
1073 
CloseAACEncoder(HI_VOID * pEncoder)1074 static HI_S32 CloseAACEncoder(HI_VOID *pEncoder)
1075 {
1076     AENC_AAC_ENCODER_S *pstEncoder = NULL;
1077 
1078     HI_AUDIO_ASSERT(pEncoder != NULL);
1079 
1080     pstEncoder = (AENC_AAC_ENCODER_S *)pEncoder;
1081     AACEncoderClose_Adp(pstEncoder->pstAACState);
1082 
1083     free(pstEncoder);
1084 
1085     return HI_SUCCESS;
1086 }
1087 
OpenAACDecoder(HI_VOID * pDecoderAttr,HI_VOID ** ppDecoder)1088 static HI_S32 OpenAACDecoder(HI_VOID *pDecoderAttr, HI_VOID **ppDecoder)
1089 {
1090     ADEC_AAC_DECODER_S *pstDecoder = NULL;
1091     ADEC_ATTR_AAC_S *pstAttr = NULL;
1092     HI_S32 s32Ret;
1093 
1094     HI_AUDIO_ASSERT(pDecoderAttr != NULL);
1095     HI_AUDIO_ASSERT(ppDecoder != NULL);
1096 
1097     /* allocate memory for decoder */
1098     pstDecoder = (ADEC_AAC_DECODER_S *)malloc(sizeof(ADEC_AAC_DECODER_S));
1099     if (pstDecoder == NULL) {
1100         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "no memory");
1101         return HI_ERR_ADEC_NOMEM;
1102     }
1103     s32Ret = memset_s(pstDecoder, sizeof(ADEC_AAC_DECODER_S), 0, sizeof(ADEC_AAC_DECODER_S));
1104     if (s32Ret != EOK) {
1105         free(pstDecoder);
1106         pstDecoder = NULL;
1107         printf("%s: memset_s failed, ret:0x%x.\n", __FUNCTION__, s32Ret);
1108         return s32Ret;
1109     }
1110     *ppDecoder = (HI_VOID *)pstDecoder;
1111 
1112     pstAttr = (ADEC_ATTR_AAC_S *)pDecoderAttr;
1113     /* create decoder */
1114     pstDecoder->pstAACState = AACInitDecoder_Adp((AACDECTransportType)pstAttr->enTransType);
1115     if (!pstDecoder->pstAACState) {
1116         free(pstDecoder);
1117         pstDecoder = NULL;
1118         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "AACInitDecoder failed");
1119         return HI_ERR_ADEC_DECODER_ERR;
1120     }
1121 
1122     s32Ret = memcpy_s(&pstDecoder->stAACAttr, sizeof(pstDecoder->stAACAttr), pstAttr, sizeof(ADEC_ATTR_AAC_S));
1123     if (s32Ret != EOK) {
1124         free(pstDecoder);
1125         pstDecoder = NULL;
1126         printf("%s: memcpy_s failed, ret:0x%x.\n", __FUNCTION__, s32Ret);
1127         return s32Ret;
1128     }
1129 
1130     return HI_SUCCESS;
1131 }
1132 
DecodeAACFrm(HI_VOID * pDecoder,HI_U8 ** pu8Inbuf,HI_S32 * ps32LeftByte,HI_U16 * pu16Outbuf,HI_U32 * pu32OutLen,HI_U32 * pu32Chns)1133 static HI_S32 DecodeAACFrm(HI_VOID *pDecoder, HI_U8 **pu8Inbuf, HI_S32 *ps32LeftByte,
1134     HI_U16 *pu16Outbuf, HI_U32 *pu32OutLen, HI_U32 *pu32Chns)
1135 {
1136     HI_S32 s32Ret;
1137     ADEC_AAC_DECODER_S *pstDecoder = NULL;
1138     HI_S32 s32Samples, s32FrmLen, s32SampleBytes;
1139     AACFrameInfo aacFrameInfo;
1140 
1141     HI_AUDIO_ASSERT(pDecoder != NULL);
1142     HI_AUDIO_ASSERT(pu8Inbuf != NULL);
1143     HI_AUDIO_ASSERT(ps32LeftByte != NULL);
1144     HI_AUDIO_ASSERT(pu16Outbuf != NULL);
1145     HI_AUDIO_ASSERT(pu32OutLen != NULL);
1146     HI_AUDIO_ASSERT(pu32Chns != NULL);
1147 
1148     *pu32Chns = 1; /* voice encoder only one channel */
1149 
1150     pstDecoder = (ADEC_AAC_DECODER_S *)pDecoder;
1151 
1152     s32FrmLen = AACDecodeFindSyncHeader_Adp(pstDecoder->pstAACState, pu8Inbuf, ps32LeftByte);
1153     if (s32FrmLen < 0) {
1154         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "AAC decoder can't find sync header");
1155         return HI_ERR_ADEC_BUF_LACK;
1156     }
1157 
1158     /* Notes: pInbuf will updated */
1159     s32Ret = AACDecodeFrame_Adp(pstDecoder->pstAACState, pu8Inbuf, ps32LeftByte, (HI_S16 *)(HI_UINTPTR_T)pu16Outbuf);
1160     if (s32Ret) {
1161         printf("aac decoder failed!, s32Ret:0x%x\n", s32Ret);
1162         return s32Ret;
1163     }
1164 
1165     AACGetLastFrameInfo_Adp(pstDecoder->pstAACState, &aacFrameInfo);
1166     aacFrameInfo.nChans = ((aacFrameInfo.nChans != 0) ? aacFrameInfo.nChans : 1);
1167     /* samples per frame of one sound track */
1168     s32Samples = aacFrameInfo.outputSamps / aacFrameInfo.nChans;
1169 
1170     if ((s32Samples != AACLC_SAMPLES_PER_FRAME) && (s32Samples != AACPLUS_SAMPLES_PER_FRAME) &&
1171         (s32Samples != AACLD_SAMPLES_PER_FRAME)) {
1172         printf("aac decoder failed!\n");
1173         return HI_ERR_ADEC_DECODER_ERR;
1174     }
1175 
1176     s32SampleBytes = s32Samples * sizeof(HI_U16);
1177     *pu32Chns = aacFrameInfo.nChans;
1178     *pu32OutLen = s32SampleBytes;
1179 
1180     return s32Ret;
1181 }
1182 
GetAACFrmInfo(HI_VOID * pDecoder,HI_VOID * pInfo)1183 static HI_S32 GetAACFrmInfo(HI_VOID *pDecoder, HI_VOID *pInfo)
1184 {
1185     ADEC_AAC_DECODER_S *pstDecoder = NULL;
1186     AACFrameInfo aacFrameInfo;
1187     AAC_FRAME_INFO_S *pstAacFrm = NULL;
1188 
1189     HI_AUDIO_ASSERT(pDecoder != NULL);
1190     HI_AUDIO_ASSERT(pInfo != NULL);
1191 
1192     pstDecoder = (ADEC_AAC_DECODER_S *)pDecoder;
1193     AACGetLastFrameInfo_Adp(pstDecoder->pstAACState, &aacFrameInfo);
1194 
1195     pstAacFrm = (AAC_FRAME_INFO_S *)pInfo;
1196     pstAacFrm->s32Samplerate = aacFrameInfo.sampRateOut;
1197     pstAacFrm->s32BitRate = aacFrameInfo.bitRate;
1198     pstAacFrm->s32Profile = aacFrameInfo.profile;
1199     pstAacFrm->s32TnsUsed = aacFrameInfo.tnsUsed;
1200     pstAacFrm->s32PnsUsed = aacFrameInfo.pnsUsed;
1201     pstAacFrm->s32ChnNum = aacFrameInfo.nChans;
1202 
1203     return HI_SUCCESS;
1204 }
1205 
1206 
CloseAACDecoder(HI_VOID * pDecoder)1207 static HI_S32 CloseAACDecoder(HI_VOID *pDecoder)
1208 {
1209     ADEC_AAC_DECODER_S *pstDecoder = NULL;
1210 
1211     HI_AUDIO_ASSERT(pDecoder != NULL);
1212     pstDecoder = (ADEC_AAC_DECODER_S *)pDecoder;
1213     AACFreeDecoder_Adp(pstDecoder->pstAACState);
1214     free(pstDecoder);
1215 
1216     return HI_SUCCESS;
1217 }
1218 
ResetAACDecoder(HI_VOID * pDecoder)1219 static HI_S32 ResetAACDecoder(HI_VOID *pDecoder)
1220 {
1221     ADEC_AAC_DECODER_S *pstDecoder = NULL;
1222 
1223     HI_AUDIO_ASSERT(pDecoder != NULL);
1224     pstDecoder = (ADEC_AAC_DECODER_S *)pDecoder;
1225     AACFreeDecoder_Adp(pstDecoder->pstAACState);
1226 
1227     /* create decoder */
1228     pstDecoder->pstAACState = AACInitDecoder_Adp((AACDECTransportType)pstDecoder->stAACAttr.enTransType);
1229     if (pstDecoder->pstAACState == NULL) {
1230         printf("[Func]:%s [Line]:%d [Info]:%s\n", __FUNCTION__, __LINE__, "AACResetDecoder failed");
1231         return HI_ERR_ADEC_DECODER_ERR;
1232     }
1233 
1234     return HI_SUCCESS;
1235 }
1236 
HI_MPI_AENC_AacInit(HI_VOID)1237 HI_S32 HI_MPI_AENC_AacInit(HI_VOID)
1238 {
1239     HI_S32 s32Ret;
1240     HI_S32 s32Handle = 0;
1241     AENC_ENCODER_S stAac;
1242 
1243     s32Ret = InitAacAencLib();
1244     if (s32Ret) {
1245         return s32Ret;
1246     }
1247 
1248     stAac.enType = PT_AAC;
1249     s32Ret = snprintf_s(stAac.aszName, sizeof(stAac.aszName), sizeof(stAac.aszName) - 1, "Aac");
1250     if (s32Ret <= EOK) {
1251         printf("\n snprintf_s fail! ret = 0x%x", s32Ret);
1252         return s32Ret;
1253     }
1254     stAac.u32MaxFrmLen = MAX_AAC_MAINBUF_SIZE;
1255     stAac.pfnOpenEncoder = OpenAACEncoder;
1256     stAac.pfnEncodeFrm = EncodeAACFrm;
1257     stAac.pfnCloseEncoder = CloseAACEncoder;
1258     s32Ret = HI_MPI_AENC_RegisterEncoder(&s32Handle, &stAac);
1259     if (s32Ret) {
1260         return s32Ret;
1261     }
1262     g_AacEncHandle = s32Handle;
1263 
1264     return HI_SUCCESS;
1265 }
1266 
HI_MPI_AENC_AacDeInit(HI_VOID)1267 HI_S32 HI_MPI_AENC_AacDeInit(HI_VOID)
1268 {
1269     HI_S32 s32Ret;
1270     s32Ret = HI_MPI_AENC_UnRegisterEncoder(g_AacEncHandle);
1271     if (s32Ret) {
1272         return s32Ret;
1273     }
1274 
1275     DeInitAacAencLib();
1276 
1277     return HI_SUCCESS;
1278 }
1279 
HI_MPI_ADEC_AacInit(HI_VOID)1280 HI_S32 HI_MPI_ADEC_AacInit(HI_VOID)
1281 {
1282     HI_S32 s32Ret;
1283     HI_S32 s32Handle = 0;
1284     ADEC_DECODER_S stAac;
1285 
1286     s32Ret = InitAacAdecLib();
1287     if (s32Ret) {
1288         return s32Ret;
1289     }
1290 
1291     stAac.enType = PT_AAC;
1292     s32Ret = snprintf_s(stAac.aszName, sizeof(stAac.aszName), sizeof(stAac.aszName) - 1, "Aac");
1293     if (s32Ret <= EOK) {
1294         printf("\n snprintf_s fail! ret = 0x%x", s32Ret);
1295         return s32Ret;
1296     }
1297     stAac.pfnOpenDecoder = OpenAACDecoder;
1298     stAac.pfnDecodeFrm = DecodeAACFrm;
1299     stAac.pfnGetFrmInfo = GetAACFrmInfo;
1300     stAac.pfnCloseDecoder = CloseAACDecoder;
1301     stAac.pfnResetDecoder = ResetAACDecoder;
1302     s32Ret = HI_MPI_ADEC_RegisterDecoder(&s32Handle, &stAac);
1303     if (s32Ret) {
1304         return s32Ret;
1305     }
1306     g_AacDecHandle = s32Handle;
1307 
1308     return HI_SUCCESS;
1309 }
1310 
HI_MPI_ADEC_AacDeInit(HI_VOID)1311 HI_S32 HI_MPI_ADEC_AacDeInit(HI_VOID)
1312 {
1313     HI_S32 s32Ret;
1314     s32Ret = HI_MPI_ADEC_UnRegisterDecoder(g_AacDecHandle);
1315     if (s32Ret) {
1316         return s32Ret;
1317     }
1318 
1319     DeInitAacAdecLib();
1320 
1321     return HI_SUCCESS;
1322 }
1323