AACEncoder.cpp revision 8fff6bb0000e43f02c9d04ca90f941fdefdc1356
1/*
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),
39      mInputFrame(NULL),
40      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));
49    CHECK(mChannels <= 2 && mChannels >= 1);
50    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) {
137    if (mStarted) {
138        LOGW("Call start() when encoder already started");
139        return OK;
140    }
141
142    mBufferGroup = new MediaBufferGroup;
143    mBufferGroup->add_buffer(new MediaBuffer(2048));
144
145    CHECK_EQ(OK, initCheck());
146
147    mNumInputSamples = 0;
148    mAnchorTimeUs = 0;
149    mFrameCount = 0;
150
151    mInputFrame = new int16_t[mChannels * kNumSamplesPerFrame];
152    CHECK(mInputFrame != NULL);
153
154    mSource->start(params);
155
156    mStarted = true;
157
158    return OK;
159}
160
161status_t AACEncoder::stop() {
162    if (!mStarted) {
163        LOGW("Call stop() when encoder has not started");
164        return OK;
165    }
166
167    if (mInputBuffer) {
168        mInputBuffer->release();
169        mInputBuffer = NULL;
170    }
171
172    delete mBufferGroup;
173    mBufferGroup = NULL;
174
175    mSource->stop();
176
177    if (mEncoderHandle) {
178        CHECK_EQ(VO_ERR_NONE, mApiHandle->Uninit(mEncoderHandle));
179        mEncoderHandle = NULL;
180    }
181    delete mApiHandle;
182    mApiHandle = NULL;
183
184    mStarted = false;
185    if (mInputFrame) {
186        delete[] mInputFrame;
187        mInputFrame = NULL;
188    }
189
190    return OK;
191}
192
193sp<MetaData> AACEncoder::getFormat() {
194    sp<MetaData> srcFormat = mSource->getFormat();
195
196    mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_AAC);
197
198    int64_t durationUs;
199    if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
200        mMeta->setInt64(kKeyDuration, durationUs);
201    }
202
203    mMeta->setCString(kKeyDecoderComponent, "AACEncoder");
204
205    return mMeta;
206}
207
208status_t AACEncoder::read(
209        MediaBuffer **out, const ReadOptions *options) {
210    status_t err;
211
212    *out = NULL;
213
214    int64_t seekTimeUs;
215    ReadOptions::SeekMode mode;
216    CHECK(options == NULL || !options->getSeekTo(&seekTimeUs, &mode));
217
218    MediaBuffer *buffer;
219    CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
220    uint8_t *outPtr = (uint8_t *)buffer->data();
221    bool readFromSource = false;
222    int64_t wallClockTimeUs = -1;
223
224    if (mFrameCount == 0) {
225        memcpy(outPtr, mAudioSpecificConfigData, 2);
226        buffer->set_range(0, 2);
227        buffer->meta_data()->setInt32(kKeyIsCodecConfig, true);
228        *out = buffer;
229        ++mFrameCount;
230        return OK;
231    } else if (mFrameCount == 1) {
232        buffer->meta_data()->setInt32(kKeyIsCodecConfig, false);
233    }
234
235    const int32_t nSamples = mChannels * kNumSamplesPerFrame;
236    while (mNumInputSamples < nSamples) {
237        if (mInputBuffer == NULL) {
238            if (mSource->read(&mInputBuffer, options) != OK) {
239                if (mNumInputSamples == 0) {
240                    buffer->release();
241                    return ERROR_END_OF_STREAM;
242                }
243                memset(&mInputFrame[mNumInputSamples],
244                       0,
245                       sizeof(int16_t) * (nSamples - mNumInputSamples));
246                mNumInputSamples = 0;
247                break;
248            }
249
250            size_t align = mInputBuffer->range_length() % sizeof(int16_t);
251            CHECK_EQ(align, 0);
252
253            int64_t timeUs;
254            if (mInputBuffer->meta_data()->findInt64(kKeyDriftTime, &timeUs)) {
255                wallClockTimeUs = timeUs;
256            }
257            if (mInputBuffer->meta_data()->findInt64(kKeyAnchorTime, &timeUs)) {
258                mAnchorTimeUs = timeUs;
259            }
260            readFromSource = true;
261        } else {
262            readFromSource = false;
263        }
264        size_t copy = (nSamples - mNumInputSamples) * sizeof(int16_t);
265
266        if (copy > mInputBuffer->range_length()) {
267            copy = mInputBuffer->range_length();
268        }
269
270        memcpy(&mInputFrame[mNumInputSamples],
271               (const uint8_t *) mInputBuffer->data()
272                    + mInputBuffer->range_offset(),
273               copy);
274
275        mInputBuffer->set_range(
276               mInputBuffer->range_offset() + copy,
277               mInputBuffer->range_length() - copy);
278
279        if (mInputBuffer->range_length() == 0) {
280            mInputBuffer->release();
281            mInputBuffer = NULL;
282        }
283        mNumInputSamples += copy / sizeof(int16_t);
284        if (mNumInputSamples >= nSamples) {
285            mNumInputSamples %= nSamples;
286            break;
287        }
288    }
289
290    VO_CODECBUFFER inputData;
291    memset(&inputData, 0, sizeof(inputData));
292    inputData.Buffer = (unsigned char*) mInputFrame;
293    inputData.Length = nSamples * sizeof(int16_t);
294    CHECK(VO_ERR_NONE == mApiHandle->SetInputData(mEncoderHandle,&inputData));
295
296    VO_CODECBUFFER outputData;
297    memset(&outputData, 0, sizeof(outputData));
298    VO_AUDIO_OUTPUTINFO outputInfo;
299    memset(&outputInfo, 0, sizeof(outputInfo));
300
301    VO_U32 ret = VO_ERR_NONE;
302    size_t nOutputBytes = 0;
303    do {
304        outputData.Buffer = outPtr;
305        outputData.Length = buffer->size() - nOutputBytes;
306        ret = mApiHandle->GetOutputData(mEncoderHandle, &outputData, &outputInfo);
307        if (ret == VO_ERR_NONE) {
308            outPtr += outputData.Length;
309            nOutputBytes += outputData.Length;
310        }
311    } while (ret != VO_ERR_INPUT_BUFFER_SMALL);
312    buffer->set_range(0, nOutputBytes);
313
314    int64_t mediaTimeUs =
315        ((mFrameCount - 1) * 1000000LL * kNumSamplesPerFrame) / mSampleRate;
316
317    buffer->meta_data()->setInt64(kKeyTime, mAnchorTimeUs + mediaTimeUs);
318    if (readFromSource && wallClockTimeUs != -1) {
319        buffer->meta_data()->setInt64(kKeyDriftTime, mediaTimeUs - wallClockTimeUs);
320    }
321    ++mFrameCount;
322
323    *out = buffer;
324    return OK;
325}
326
327}  // namespace android
328