blob: 44296b653e402a4f6de00bed1f7e1a7d2cab0af3 [file] [log] [blame]
Indranil Chakraborty36e686a2018-04-05 12:05:02 +05301/*
2 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * */
29
30#include "aacDecode.h"
31#include "aacdecoder_lib.h"
32#include <utils/Log.h>
33#include <string.h>
34
35aacDecode::aacDecode() {
36 p_aacHandle = NULL;
37 p_aacInfo = NULL;
38 memset(&s_aacConfig, 0, sizeof(s_aacConfig));
39}
40
41aacDecode::~aacDecode() {
42 if(!p_aacHandle) {
43 return;
44 }
45
46 aacDecoder_Close((HANDLE_AACDECODER)p_aacHandle);
47 p_aacHandle = NULL;
48}
49
50bool aacDecode::aacConfigure(aacConfigType * p_aacConfig) {
51 if(!p_aacConfig) {
52 return false;
53 }
54
55 memcpy(&s_aacConfig, p_aacConfig, sizeof(s_aacConfig));
56
57 p_aacHandle = aacDecoder_Open(TT_MP4_ADTS, 1);
58
59 if(!p_aacHandle) {
60 ALOGE("Failed to open AAC decoder");
61 return false;
62 }
63
64 p_aacInfo = (void*)aacDecoder_GetStreamInfo((HANDLE_AACDECODER)p_aacHandle);
65
66 if(!p_aacInfo) {
67 ALOGE("Failed to get stream info");
68 return false;
69 }
70
71 /* Configure AAC decoder */
72
73 aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
74 AAC_DRC_REFERENCE_LEVEL,
75 64);
76
77 aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
78 AAC_DRC_ATTENUATION_FACTOR,
79 127);
80
81 aacDecoder_SetParam((HANDLE_AACDECODER)p_aacHandle,
82 AAC_PCM_MAX_OUTPUT_CHANNELS,
83 s_aacConfig.n_channels > 6 ? -1 : s_aacConfig.n_channels);
84 return true;
85}
86
87bool aacDecode::aacDecodeFrame(unsigned char* p_buffer, unsigned int n_size) {
88 if(!p_buffer || n_size < 7) {
89 ALOGE("No/Incorrect buffer provided for AAC decoder");
90 return false;
91 }
92
93 if(!p_aacHandle) {
94 ALOGE("Decoder handle not available");
95 return false;
96 }
97
98 AAC_DECODER_ERROR err = AAC_DEC_UNKNOWN;
99
100 UINT nSize[] = {(UINT)n_size};
101 UINT nOcc[] = {(UINT)n_size};
102 UCHAR* pBuf[] = {p_buffer};
103
104 err = aacDecoder_Fill((HANDLE_AACDECODER)p_aacHandle, pBuf, nSize, nOcc);
105
106 if(err != AAC_DEC_OK || nOcc[0] != 0) {
107 ALOGE("Error in aacDecoder_Fill");
108 return false;
109 }
110
111 /* Fix the alloc length being passed to AAC Decoder since it expects INT_PCM
112 as input buffer but we have it as UINT8. INT_PCM is of type SHORT */
113
114 int factor = sizeof(INT_PCM)/sizeof(unsigned char);
115 int allocLen = (factor == 0) ? n_size : n_size/factor;
116
117 err = aacDecoder_DecodeFrame((HANDLE_AACDECODER)p_aacHandle,
118 (INT_PCM*)p_buffer, allocLen, 0);
119
120 if(err != AAC_DEC_OK) {
121 ALOGE("Failed to decode frame");
122 return false;
123 }
124 return true;
125}