blob: e4ff128a507eb66a5f900a342d4d2597bcaf29af [file] [log] [blame]
James Dong17299ab2010-05-14 15:45:22 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "AACEncoder"
19#include <utils/Log.h>
20
21#include "AACEncoder.h"
22#include "voAAC.h"
23#include "cmnMemory.h"
24
25#include <media/stagefright/MediaBufferGroup.h>
26#include <media/stagefright/MediaDebug.h>
27#include <media/stagefright/MediaDefs.h>
28#include <media/stagefright/MediaErrors.h>
29#include <media/stagefright/MetaData.h>
30
31namespace android {
32
33AACEncoder::AACEncoder(const sp<MediaSource> &source, const sp<MetaData> &meta)
34 : mSource(source),
35 mMeta(meta),
36 mStarted(false),
37 mBufferGroup(NULL),
38 mInputBuffer(NULL),
James Dong36663b22010-11-15 14:24:37 -080039 mInputFrame(NULL),
James Dong17299ab2010-05-14 15:45:22 -070040 mEncoderHandle(NULL),
41 mApiHandle(NULL),
42 mMemOperator(NULL) {
43}
44
45status_t AACEncoder::initCheck() {
46 CHECK(mApiHandle == NULL && mEncoderHandle == NULL);
47 CHECK(mMeta->findInt32(kKeySampleRate, &mSampleRate));
48 CHECK(mMeta->findInt32(kKeyChannelCount, &mChannels));
James Dong36663b22010-11-15 14:24:37 -080049 CHECK(mChannels <= 2 && mChannels >= 1);
James Dong17299ab2010-05-14 15:45:22 -070050 CHECK(mMeta->findInt32(kKeyBitRate, &mBitRate));
51
52 mApiHandle = new VO_AUDIO_CODECAPI;
53 CHECK(mApiHandle);
54
55 if (VO_ERR_NONE != voGetAACEncAPI(mApiHandle)) {
56 LOGE("Failed to get api handle");
57 return UNKNOWN_ERROR;
58 }
59
60 mMemOperator = new VO_MEM_OPERATOR;
61 CHECK(mMemOperator != NULL);
62 mMemOperator->Alloc = cmnMemAlloc;
63 mMemOperator->Copy = cmnMemCopy;
64 mMemOperator->Free = cmnMemFree;
65 mMemOperator->Set = cmnMemSet;
66 mMemOperator->Check = cmnMemCheck;
67
68 VO_CODEC_INIT_USERDATA userData;
69 memset(&userData, 0, sizeof(userData));
70 userData.memflag = VO_IMF_USERMEMOPERATOR;
71 userData.memData = (VO_PTR) mMemOperator;
72 if (VO_ERR_NONE != mApiHandle->Init(&mEncoderHandle, VO_AUDIO_CodingAAC, &userData)) {
73 LOGE("Failed to init AAC encoder");
74 return UNKNOWN_ERROR;
75 }
76 if (OK != setAudioSpecificConfigData()) {
77 LOGE("Failed to configure AAC encoder");
78 return UNKNOWN_ERROR;
79 }
80
81 // Configure AAC encoder$
82 AACENC_PARAM params;
83 memset(&params, 0, sizeof(params));
84 params.sampleRate = mSampleRate;
85 params.bitRate = mBitRate;
86 params.nChannels = mChannels;
87 params.adtsUsed = 0; // For MP4 file, don't use adts format$
88 if (VO_ERR_NONE != mApiHandle->SetParam(mEncoderHandle, VO_PID_AAC_ENCPARAM, &params)) {
89 LOGE("Failed to set AAC encoder parameters");
90 return UNKNOWN_ERROR;
91 }
92
93 return OK;
94}
95
96static status_t getSampleRateTableIndex(int32_t sampleRate, int32_t &index) {
97 static const int32_t kSampleRateTable[] = {
98 96000, 88200, 64000, 48000, 44100, 32000,
99 24000, 22050, 16000, 12000, 11025, 8000
100 };
101 const int32_t tableSize = sizeof(kSampleRateTable) / sizeof(kSampleRateTable[0]);
102 for (int32_t i = 0; i < tableSize; ++i) {
103 if (sampleRate == kSampleRateTable[i]) {
104 index = i;
105 return OK;
106 }
107 }
108
109 LOGE("Sampling rate %d bps is not supported", sampleRate);
110 return UNKNOWN_ERROR;
111}
112
113status_t AACEncoder::setAudioSpecificConfigData() {
114 LOGV("setAudioSpecificConfigData: %d hz, %d bps, and %d channels",
115 mSampleRate, mBitRate, mChannels);
116
117 int32_t index;
118 CHECK_EQ(OK, getSampleRateTableIndex(mSampleRate, index));
119 if (mChannels > 2 || mChannels <= 0) {
120 LOGE("Unsupported number of channels(%d)", mChannels);
121 return UNKNOWN_ERROR;
122 }
123
124 // OMX_AUDIO_AACObjectLC
125 mAudioSpecificConfigData[0] = ((0x02 << 3) | (index >> 1));
126 mAudioSpecificConfigData[1] = ((index & 0x01) << 7) | (mChannels << 3);
127 return OK;
128}
129
130AACEncoder::~AACEncoder() {
131 if (mStarted) {
132 stop();
133 }
134}
135
136status_t AACEncoder::start(MetaData *params) {
James Dongc0ab2a62010-06-29 16:29:19 -0700137 if (mStarted) {
138 LOGW("Call start() when encoder already started");
139 return OK;
140 }
James Dong17299ab2010-05-14 15:45:22 -0700141
142 mBufferGroup = new MediaBufferGroup;
143 mBufferGroup->add_buffer(new MediaBuffer(2048));
144
145 CHECK_EQ(OK, initCheck());
146
James Dong438e4f62010-06-23 16:51:39 -0700147 mNumInputSamples = 0;
148 mAnchorTimeUs = 0;
James Dong17299ab2010-05-14 15:45:22 -0700149 mFrameCount = 0;
James Dong36663b22010-11-15 14:24:37 -0800150
151 mInputFrame = new int16_t[mChannels * kNumSamplesPerFrame];
152 CHECK(mInputFrame != NULL);
153
James Dongd7ef5b62011-01-25 12:37:43 -0800154 status_t err = mSource->start(params);
155 if (err != OK) {
156 LOGE("AudioSource is not available");
157 return err;
158 }
James Dong17299ab2010-05-14 15:45:22 -0700159
160 mStarted = true;
161
162 return OK;
163}
164
165status_t AACEncoder::stop() {
James Dong17299ab2010-05-14 15:45:22 -0700166 if (mInputBuffer) {
167 mInputBuffer->release();
168 mInputBuffer = NULL;
169 }
170
171 delete mBufferGroup;
172 mBufferGroup = NULL;
173
James Dongd7ef5b62011-01-25 12:37:43 -0800174 if (mInputFrame) {
175 delete[] mInputFrame;
176 mInputFrame = NULL;
177 }
James Dong17299ab2010-05-14 15:45:22 -0700178
James Dongd7ef5b62011-01-25 12:37:43 -0800179 if (!mStarted) {
180 LOGW("Call stop() when encoder has not started");
181 return ERROR_END_OF_STREAM;
182 }
183
184 mSource->stop();
James Dong17299ab2010-05-14 15:45:22 -0700185 if (mEncoderHandle) {
186 CHECK_EQ(VO_ERR_NONE, mApiHandle->Uninit(mEncoderHandle));
187 mEncoderHandle = NULL;
188 }
189 delete mApiHandle;
190 mApiHandle = NULL;
191
James Dong99b86242011-03-04 16:24:07 -0800192 delete mMemOperator;
193 mMemOperator = NULL;
194
James Dong17299ab2010-05-14 15:45:22 -0700195 mStarted = false;
196
197 return OK;
198}
199
200sp<MetaData> AACEncoder::getFormat() {
201 sp<MetaData> srcFormat = mSource->getFormat();
202
203 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
204
205 int64_t durationUs;
206 if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
207 mMeta->setInt64(kKeyDuration, durationUs);
208 }
209
210 mMeta->setCString(kKeyDecoderComponent, "AACEncoder");
211
212 return mMeta;
213}
214
215status_t AACEncoder::read(
216 MediaBuffer **out, const ReadOptions *options) {
217 status_t err;
218
219 *out = NULL;
220
221 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -0700222 ReadOptions::SeekMode mode;
223 CHECK(options == NULL || !options->getSeekTo(&seekTimeUs, &mode));
James Dong17299ab2010-05-14 15:45:22 -0700224
225 MediaBuffer *buffer;
226 CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
227 uint8_t *outPtr = (uint8_t *)buffer->data();
James Dong4c238152010-09-01 18:48:35 -0700228 bool readFromSource = false;
James Dong3caa7142010-09-03 12:01:55 -0700229 int64_t wallClockTimeUs = -1;
James Dong17299ab2010-05-14 15:45:22 -0700230
231 if (mFrameCount == 0) {
232 memcpy(outPtr, mAudioSpecificConfigData, 2);
233 buffer->set_range(0, 2);
234 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
235 *out = buffer;
James Dong08c74732010-06-10 12:28:15 -0700236 ++mFrameCount;
James Dong17299ab2010-05-14 15:45:22 -0700237 return OK;
James Dong08c74732010-06-10 12:28:15 -0700238 } else if (mFrameCount == 1) {
James Dong17299ab2010-05-14 15:45:22 -0700239 buffer->meta_data()->setInt32(kKeyIsCodecConfig, false);
240 }
241
James Dong36663b22010-11-15 14:24:37 -0800242 const int32_t nSamples = mChannels * kNumSamplesPerFrame;
243 while (mNumInputSamples < nSamples) {
James Dong438e4f62010-06-23 16:51:39 -0700244 if (mInputBuffer == NULL) {
245 if (mSource->read(&mInputBuffer, options) != OK) {
246 if (mNumInputSamples == 0) {
James Dong53d4e0d2010-07-21 14:51:35 -0700247 buffer->release();
James Dong438e4f62010-06-23 16:51:39 -0700248 return ERROR_END_OF_STREAM;
249 }
250 memset(&mInputFrame[mNumInputSamples],
251 0,
James Dong36663b22010-11-15 14:24:37 -0800252 sizeof(int16_t) * (nSamples - mNumInputSamples));
James Dong438e4f62010-06-23 16:51:39 -0700253 mNumInputSamples = 0;
254 break;
255 }
256
257 size_t align = mInputBuffer->range_length() % sizeof(int16_t);
258 CHECK_EQ(align, 0);
259
260 int64_t timeUs;
James Dong3caa7142010-09-03 12:01:55 -0700261 if (mInputBuffer->meta_data()->findInt64(kKeyDriftTime, &timeUs)) {
262 wallClockTimeUs = timeUs;
263 }
James Dongd015ccf2010-09-08 15:07:21 -0700264 if (mInputBuffer->meta_data()->findInt64(kKeyAnchorTime, &timeUs)) {
James Dong438e4f62010-06-23 16:51:39 -0700265 mAnchorTimeUs = timeUs;
266 }
James Dong4c238152010-09-01 18:48:35 -0700267 readFromSource = true;
268 } else {
269 readFromSource = false;
James Dong17299ab2010-05-14 15:45:22 -0700270 }
James Dong36663b22010-11-15 14:24:37 -0800271 size_t copy = (nSamples - mNumInputSamples) * sizeof(int16_t);
James Dong438e4f62010-06-23 16:51:39 -0700272
273 if (copy > mInputBuffer->range_length()) {
274 copy = mInputBuffer->range_length();
275 }
276
277 memcpy(&mInputFrame[mNumInputSamples],
278 (const uint8_t *) mInputBuffer->data()
279 + mInputBuffer->range_offset(),
280 copy);
281
282 mInputBuffer->set_range(
283 mInputBuffer->range_offset() + copy,
284 mInputBuffer->range_length() - copy);
285
James Dong17299ab2010-05-14 15:45:22 -0700286 if (mInputBuffer->range_length() == 0) {
287 mInputBuffer->release();
288 mInputBuffer = NULL;
James Dong17299ab2010-05-14 15:45:22 -0700289 }
James Dong438e4f62010-06-23 16:51:39 -0700290 mNumInputSamples += copy / sizeof(int16_t);
James Dong36663b22010-11-15 14:24:37 -0800291 if (mNumInputSamples >= nSamples) {
292 mNumInputSamples %= nSamples;
James Dong438e4f62010-06-23 16:51:39 -0700293 break;
294 }
James Dong17299ab2010-05-14 15:45:22 -0700295 }
296
James Dong438e4f62010-06-23 16:51:39 -0700297 VO_CODECBUFFER inputData;
298 memset(&inputData, 0, sizeof(inputData));
299 inputData.Buffer = (unsigned char*) mInputFrame;
James Dong36663b22010-11-15 14:24:37 -0800300 inputData.Length = nSamples * sizeof(int16_t);
James Dong438e4f62010-06-23 16:51:39 -0700301 CHECK(VO_ERR_NONE == mApiHandle->SetInputData(mEncoderHandle,&inputData));
James Dong17299ab2010-05-14 15:45:22 -0700302
303 VO_CODECBUFFER outputData;
304 memset(&outputData, 0, sizeof(outputData));
305 VO_AUDIO_OUTPUTINFO outputInfo;
306 memset(&outputInfo, 0, sizeof(outputInfo));
307
308 VO_U32 ret = VO_ERR_NONE;
James Dong36663b22010-11-15 14:24:37 -0800309 size_t nOutputBytes = 0;
310 do {
311 outputData.Buffer = outPtr;
312 outputData.Length = buffer->size() - nOutputBytes;
313 ret = mApiHandle->GetOutputData(mEncoderHandle, &outputData, &outputInfo);
314 if (ret == VO_ERR_NONE) {
315 outPtr += outputData.Length;
316 nOutputBytes += outputData.Length;
317 }
318 } while (ret != VO_ERR_INPUT_BUFFER_SMALL);
319 buffer->set_range(0, nOutputBytes);
James Dong17299ab2010-05-14 15:45:22 -0700320
James Dong4c238152010-09-01 18:48:35 -0700321 int64_t mediaTimeUs =
322 ((mFrameCount - 1) * 1000000LL * kNumSamplesPerFrame) / mSampleRate;
James Dong36663b22010-11-15 14:24:37 -0800323
James Dong4c238152010-09-01 18:48:35 -0700324 buffer->meta_data()->setInt64(kKeyTime, mAnchorTimeUs + mediaTimeUs);
James Dong3caa7142010-09-03 12:01:55 -0700325 if (readFromSource && wallClockTimeUs != -1) {
James Dong4c238152010-09-01 18:48:35 -0700326 buffer->meta_data()->setInt64(kKeyDriftTime, mediaTimeUs - wallClockTimeUs);
327 }
James Dong08c74732010-06-10 12:28:15 -0700328 ++mFrameCount;
James Dong17299ab2010-05-14 15:45:22 -0700329
330 *out = buffer;
331 return OK;
332}
333
334} // namespace android