blob: 1d5b27332b535a994f061e3dd987e9d72a42894a [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 "aacEncode.h"
31#include "aacenc_lib.h"
32#include <utils/Log.h>
33#include <string.h>
34
35struct aacInfo
36{
37 AACENC_BufDesc inBuff;
38 AACENC_BufDesc outBuff;
39 AACENC_InArgs inArg;
40 AACENC_OutArgs outArg;
41};
42
43aacEncode::aacEncode() {
44 p_aacHandle = NULL;
45 p_aacInfo = NULL;
46 memset(&s_aacConfig, 0, sizeof(s_aacConfig));
47}
48
49aacEncode::~aacEncode() {
50 if(!p_aacHandle) {
51 return;
52 }
53
54 if(aacEncClose((HANDLE_AACENCODER*)(&p_aacHandle)) != AACENC_OK) {
55 ALOGE("aacEncClose Failed");
56 return;
57 }
58}
59
60bool aacEncode::aacConfigure(aacConfigType * p_aacConfig) {
61 if(!p_aacConfig) {
62 return false;
63 }
64
65 memcpy(&s_aacConfig, p_aacConfig, sizeof(s_aacConfig));
66
67 /* Configure AAC encoder here */
68
69 AACENC_ERROR err = AACENC_OK;
70
71 p_aacInfo = (void*)new(aacInfo);
72 if(!p_aacInfo) {
73 ALOGE("Failed to allocate aacInfo");
74 return false;
75 }
76
77 /* Open AAC encoder */
78 err = aacEncOpen((HANDLE_AACENCODER*)(&p_aacHandle),
79 0x01 /* AAC */,
80 s_aacConfig.n_channels);
81
82 if(err != AACENC_OK) {
83 ALOGE("Failed top open AAC encoder");
84 return false;
85 }
86
87 /* Set Bitrate and SampleRate */
88 err = aacEncoder_SetParam((HANDLE_AACENCODER)p_aacHandle,
89 AACENC_BITRATE,
90 s_aacConfig.n_bitrate);
91
92 if(err != AACENC_OK) {
93 ALOGE("Failed to set bitrate param to AAC encoder");
94 return false;
95 }
96
97 err = aacEncoder_SetParam((HANDLE_AACENCODER)p_aacHandle,
98 AACENC_SAMPLERATE,
99 s_aacConfig.n_sampleRate);
100
101 if(err != AACENC_OK) {
102 ALOGE("Failed to set samplerate param to AAC encoder");
103 return false;
104 }
105
106 /* Fix Channel mode and order */
107 /* TODO */
108
109 /* Prefill encode structures */
110 /* TODO */
111
112 return true;
113}
114
115bool aacEncode::aacEncodeFrame(unsigned char * p_inBuffer,
116 unsigned int n_inSize,
117 unsigned char * p_outBuffer,
118 unsigned int n_outSize,
119 unsigned int * p_length) {
120 (void)n_inSize;
121 (void)n_outSize;
122 (void)p_length;
123 if(!p_inBuffer || !p_outBuffer) {
124 ALOGE("No buffers provided for AAC encoder");
125 return false;
126 }
127
128 aacInfo *tempAacInfo = (aacInfo*)p_aacInfo;
129 tempAacInfo->inBuff.bufs = (void**) (&p_inBuffer);
130 tempAacInfo->outBuff.bufs = (void**) (&p_outBuffer);
131
132 AACENC_ERROR err = AACENC_OK;
133
134 if(p_aacHandle) {
135 err = aacEncEncode((HANDLE_AACENCODER)p_aacHandle,
136 &tempAacInfo->inBuff,
137 &tempAacInfo->outBuff,
138 &tempAacInfo->inArg,
139 &tempAacInfo->outArg);
140
141 if(err != AACENC_OK) {
142 ALOGE("Failed to encode buffer");
143 return false;
144 }
145 } else {
146 ALOGE("No encoder available");
147 return false;
148 }
149
150 return true;
151}