2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "AACEncoder"
19 #include <utils/Log.h>
21 #include "AACEncoder.h"
23 #include "cmnMemory.h"
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>
33 AACEncoder::AACEncoder(const sp<MediaSource> &source, const sp<MetaData> &meta)
44 status_t AACEncoder::initCheck() {
45 CHECK(mApiHandle == NULL && mEncoderHandle == NULL);
46 CHECK(mMeta->findInt32(kKeySampleRate, &mSampleRate));
47 CHECK(mMeta->findInt32(kKeyChannelCount, &mChannels));
48 CHECK(mMeta->findInt32(kKeyBitRate, &mBitRate));
50 mApiHandle = new VO_AUDIO_CODECAPI;
53 if (VO_ERR_NONE != voGetAACEncAPI(mApiHandle)) {
54 LOGE("Failed to get api handle");
58 mMemOperator = new VO_MEM_OPERATOR;
59 CHECK(mMemOperator != NULL);
60 mMemOperator->Alloc = cmnMemAlloc;
61 mMemOperator->Copy = cmnMemCopy;
62 mMemOperator->Free = cmnMemFree;
63 mMemOperator->Set = cmnMemSet;
64 mMemOperator->Check = cmnMemCheck;
66 VO_CODEC_INIT_USERDATA userData;
67 memset(&userData, 0, sizeof(userData));
68 userData.memflag = VO_IMF_USERMEMOPERATOR;
69 userData.memData = (VO_PTR) mMemOperator;
70 if (VO_ERR_NONE != mApiHandle->Init(&mEncoderHandle, VO_AUDIO_CodingAAC, &userData)) {
71 LOGE("Failed to init AAC encoder");
74 if (OK != setAudioSpecificConfigData()) {
75 LOGE("Failed to configure AAC encoder");
79 // Configure AAC encoder$
81 memset(¶ms, 0, sizeof(params));
82 params.sampleRate = mSampleRate;
83 params.bitRate = mBitRate;
84 params.nChannels = mChannels;
85 params.adtsUsed = 0; // For MP4 file, don't use adts format$
86 if (VO_ERR_NONE != mApiHandle->SetParam(mEncoderHandle, VO_PID_AAC_ENCPARAM, ¶ms)) {
87 LOGE("Failed to set AAC encoder parameters");
94 static status_t getSampleRateTableIndex(int32_t sampleRate, int32_t &index) {
95 static const int32_t kSampleRateTable[] = {
96 96000, 88200, 64000, 48000, 44100, 32000,
97 24000, 22050, 16000, 12000, 11025, 8000
99 const int32_t tableSize = sizeof(kSampleRateTable) / sizeof(kSampleRateTable[0]);
100 for (int32_t i = 0; i < tableSize; ++i) {
101 if (sampleRate == kSampleRateTable[i]) {
107 LOGE("Sampling rate %d bps is not supported", sampleRate);
108 return UNKNOWN_ERROR;
111 status_t AACEncoder::setAudioSpecificConfigData() {
112 LOGV("setAudioSpecificConfigData: %d hz, %d bps, and %d channels",
113 mSampleRate, mBitRate, mChannels);
116 CHECK_EQ(OK, getSampleRateTableIndex(mSampleRate, index));
117 if (mChannels > 2 || mChannels <= 0) {
118 LOGE("Unsupported number of channels(%d)", mChannels);
119 return UNKNOWN_ERROR;
122 // OMX_AUDIO_AACObjectLC
123 mAudioSpecificConfigData[0] = ((0x02 << 3) | (index >> 1));
124 mAudioSpecificConfigData[1] = ((index & 0x01) << 7) | (mChannels << 3);
128 AACEncoder::~AACEncoder() {
134 status_t AACEncoder::start(MetaData *params) {
136 LOGW("Call start() when encoder already started");
140 mBufferGroup = new MediaBufferGroup;
141 mBufferGroup->add_buffer(new MediaBuffer(2048));
143 CHECK_EQ(OK, initCheck());
145 mNumInputSamples = 0;
148 mSource->start(params);
155 status_t AACEncoder::stop() {
157 LOGW("Call stop() when encoder has not started");
162 mInputBuffer->release();
171 if (mEncoderHandle) {
172 CHECK_EQ(VO_ERR_NONE, mApiHandle->Uninit(mEncoderHandle));
173 mEncoderHandle = NULL;
183 sp<MetaData> AACEncoder::getFormat() {
184 sp<MetaData> srcFormat = mSource->getFormat();
186 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
189 if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
190 mMeta->setInt64(kKeyDuration, durationUs);
193 mMeta->setCString(kKeyDecoderComponent, "AACEncoder");
198 status_t AACEncoder::read(
199 MediaBuffer **out, const ReadOptions *options) {
205 ReadOptions::SeekMode mode;
206 CHECK(options == NULL || !options->getSeekTo(&seekTimeUs, &mode));
209 CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
210 uint8_t *outPtr = (uint8_t *)buffer->data();
211 bool readFromSource = false;
212 int64_t wallClockTimeUs = -1;
214 if (mFrameCount == 0) {
215 memcpy(outPtr, mAudioSpecificConfigData, 2);
216 buffer->set_range(0, 2);
217 buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
221 } else if (mFrameCount == 1) {
222 buffer->meta_data()->setInt32(kKeyIsCodecConfig, false);
225 while (mNumInputSamples < kNumSamplesPerFrame) {
226 if (mInputBuffer == NULL) {
227 if (mSource->read(&mInputBuffer, options) != OK) {
228 if (mNumInputSamples == 0) {
230 return ERROR_END_OF_STREAM;
232 memset(&mInputFrame[mNumInputSamples],
234 sizeof(int16_t) * (kNumSamplesPerFrame - mNumInputSamples));
235 mNumInputSamples = 0;
239 size_t align = mInputBuffer->range_length() % sizeof(int16_t);
243 if (mInputBuffer->meta_data()->findInt64(kKeyDriftTime, &timeUs)) {
244 wallClockTimeUs = timeUs;
246 if (mInputBuffer->meta_data()->findInt64(kKeyAnchorTime, &timeUs)) {
247 mAnchorTimeUs = timeUs;
249 readFromSource = true;
251 readFromSource = false;
254 (kNumSamplesPerFrame - mNumInputSamples) * sizeof(int16_t);
256 if (copy > mInputBuffer->range_length()) {
257 copy = mInputBuffer->range_length();
260 memcpy(&mInputFrame[mNumInputSamples],
261 (const uint8_t *) mInputBuffer->data()
262 + mInputBuffer->range_offset(),
265 mInputBuffer->set_range(
266 mInputBuffer->range_offset() + copy,
267 mInputBuffer->range_length() - copy);
269 if (mInputBuffer->range_length() == 0) {
270 mInputBuffer->release();
273 mNumInputSamples += copy / sizeof(int16_t);
274 if (mNumInputSamples >= kNumSamplesPerFrame) {
275 mNumInputSamples %= kNumSamplesPerFrame;
280 VO_CODECBUFFER inputData;
281 memset(&inputData, 0, sizeof(inputData));
282 inputData.Buffer = (unsigned char*) mInputFrame;
283 inputData.Length = kNumSamplesPerFrame * sizeof(int16_t);
284 CHECK(VO_ERR_NONE == mApiHandle->SetInputData(mEncoderHandle,&inputData));
286 VO_CODECBUFFER outputData;
287 memset(&outputData, 0, sizeof(outputData));
288 VO_AUDIO_OUTPUTINFO outputInfo;
289 memset(&outputInfo, 0, sizeof(outputInfo));
291 VO_U32 ret = VO_ERR_NONE;
292 outputData.Buffer = outPtr;
293 outputData.Length = buffer->size();
294 ret = mApiHandle->GetOutputData(mEncoderHandle, &outputData, &outputInfo);
295 CHECK(ret == VO_ERR_NONE || ret == VO_ERR_INPUT_BUFFER_SMALL);
296 CHECK(outputData.Length != 0);
297 buffer->set_range(0, outputData.Length);
299 int64_t mediaTimeUs =
300 ((mFrameCount - 1) * 1000000LL * kNumSamplesPerFrame) / mSampleRate;
301 buffer->meta_data()->setInt64(kKeyTime, mAnchorTimeUs + mediaTimeUs);
302 if (readFromSource && wallClockTimeUs != -1) {
303 buffer->meta_data()->setInt64(kKeyDriftTime, mediaTimeUs - wallClockTimeUs);
311 } // namespace android