2 * Copyright (C) 2009 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 #include "AACDecoder.h"
18 #define LOG_TAG "AACDecoder"
20 #include "../../include/ESDS.h"
22 #include "pvmp4audiodecoder_api.h"
24 #include <media/stagefright/MediaBufferGroup.h>
25 #include <media/stagefright/MediaDebug.h>
26 #include <media/stagefright/MediaDefs.h>
27 #include <media/stagefright/MetaData.h>
31 AACDecoder::AACDecoder(const sp<MediaSource> &source)
35 mConfig(new tPVMP4AudioDecoderExternal),
41 sp<MetaData> srcFormat = mSource->getFormat();
44 CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate));
47 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
49 // We'll always output stereo, regardless of how many channels are
50 // present in the input due to decoder limitations.
51 mMeta->setInt32(kKeyChannelCount, 2);
52 mMeta->setInt32(kKeySampleRate, sampleRate);
55 if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
56 mMeta->setInt64(kKeyDuration, durationUs);
58 mMeta->setCString(kKeyDecoderComponent, "AACDecoder");
60 mInitCheck = initCheck();
63 status_t AACDecoder::initCheck() {
64 memset(mConfig, 0, sizeof(tPVMP4AudioDecoderExternal));
65 mConfig->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED;
66 mConfig->aacPlusEnabled = 1;
68 // The software decoder doesn't properly support mono output on
69 // AACplus files. Always output stereo.
70 mConfig->desiredChannels = 2;
72 UInt32 memRequirements = PVMP4AudioDecoderGetMemRequirements();
73 mDecoderBuf = malloc(memRequirements);
75 status_t err = PVMP4AudioDecoderInitLibrary(mConfig, mDecoderBuf);
76 if (err != MP4AUDEC_SUCCESS) {
77 LOGE("Failed to initialize MP4 audio decoder");
84 sp<MetaData> meta = mSource->getFormat();
85 if (meta->findData(kKeyESDS, &type, &data, &size)) {
86 ESDS esds((const char *)data, size);
87 CHECK_EQ(esds.InitCheck(), OK);
89 const void *codec_specific_data;
90 size_t codec_specific_data_size;
91 esds.getCodecSpecificInfo(
92 &codec_specific_data, &codec_specific_data_size);
94 mConfig->pInputBuffer = (UChar *)codec_specific_data;
95 mConfig->inputBufferCurrentLength = codec_specific_data_size;
96 mConfig->inputBufferMaxLength = 0;
98 if (PVMP4AudioDecoderConfig(mConfig, mDecoderBuf)
99 != MP4AUDEC_SUCCESS) {
100 return ERROR_UNSUPPORTED;
106 AACDecoder::~AACDecoder() {
115 status_t AACDecoder::start(MetaData *params) {
118 mBufferGroup = new MediaBufferGroup;
119 mBufferGroup->add_buffer(new MediaBuffer(4096 * 2));
124 mNumSamplesOutput = 0;
126 mNumDecodedBuffers = 0;
127 mUpsamplingFactor = 2;
132 status_t AACDecoder::stop() {
136 mInputBuffer->release();
153 sp<MetaData> AACDecoder::getFormat() {
157 status_t AACDecoder::read(
158 MediaBuffer **out, const ReadOptions *options) {
164 ReadOptions::SeekMode mode;
165 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
166 CHECK(seekTimeUs >= 0);
168 mNumSamplesOutput = 0;
171 mInputBuffer->release();
175 // Make sure that the next buffer output does not still
176 // depend on fragments from the last one decoded.
177 PVMP4AudioDecoderResetBuffer(mDecoderBuf);
182 if (mInputBuffer == NULL) {
183 err = mSource->read(&mInputBuffer, options);
190 if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
191 mAnchorTimeUs = timeUs;
192 mNumSamplesOutput = 0;
194 // We must have a new timestamp after seeking.
195 CHECK(seekTimeUs < 0);
200 CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
202 mConfig->pInputBuffer =
203 (UChar *)mInputBuffer->data() + mInputBuffer->range_offset();
205 mConfig->inputBufferCurrentLength = mInputBuffer->range_length();
206 mConfig->inputBufferMaxLength = 0;
207 mConfig->inputBufferUsedLength = 0;
208 mConfig->remainderBits = 0;
210 mConfig->pOutputBuffer = static_cast<Int16 *>(buffer->data());
211 mConfig->pOutputBuffer_plus = &mConfig->pOutputBuffer[2048];
212 mConfig->repositionFlag = false;
214 Int decoderErr = PVMP4AudioDecodeFrame(mConfig, mDecoderBuf);
217 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
218 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
219 * rate system and the sampling rate in the final output is actually
220 * doubled compared with the core AAC decoder sampling rate.
222 * Explicit signalling is done by explicitly defining SBR audio object
223 * type in the bitstream. Implicit signalling is done by embedding
224 * SBR content in AAC extension payload specific to SBR, and hence
225 * requires an AAC decoder to perform pre-checks on actual audio frames.
227 * Thus, we could not say for sure whether a stream is
228 * AAC+/eAAC+ until the first data frame is decoded.
230 if (++mNumDecodedBuffers <= 2) {
231 LOGV("audio/extended audio object type: %d + %d",
232 mConfig->audioObjectType, mConfig->extendedAudioObjectType);
233 LOGV("aac+ upsampling factor: %d desired channels: %d",
234 mConfig->aacPlusUpsamplingFactor, mConfig->desiredChannels);
236 CHECK(mNumDecodedBuffers > 0);
237 if (mNumDecodedBuffers == 1) {
238 mUpsamplingFactor = mConfig->aacPlusUpsamplingFactor;
239 // Check on the sampling rate to see whether it is changed.
241 CHECK(mMeta->findInt32(kKeySampleRate, &sampleRate));
242 if (mConfig->samplingRate != sampleRate) {
243 mMeta->setInt32(kKeySampleRate, mConfig->samplingRate);
244 LOGW("Sample rate was %d Hz, but now is %d Hz",
245 sampleRate, mConfig->samplingRate);
247 mInputBuffer->release();
249 return INFO_FORMAT_CHANGED;
251 } else { // mNumDecodedBuffers == 2
252 if (mConfig->extendedAudioObjectType == MP4AUDIO_AAC_LC ||
253 mConfig->extendedAudioObjectType == MP4AUDIO_LTP) {
254 if (mUpsamplingFactor == 2) {
255 // The stream turns out to be not aacPlus mode anyway
256 LOGW("Disable AAC+/eAAC+ since extended audio object type is %d",
257 mConfig->extendedAudioObjectType);
258 mConfig->aacPlusEnabled = 0;
261 if (mUpsamplingFactor == 1) {
262 // aacPlus mode does not buy us anything, but to cause
263 // 1. CPU load to increase, and
264 // 2. a half speed of decoding
265 LOGW("Disable AAC+/eAAC+ since upsampling factor is 1");
266 mConfig->aacPlusEnabled = 0;
273 mConfig->frameLength * sizeof(int16_t) * mConfig->desiredChannels;
274 if (mUpsamplingFactor == 2) {
275 if (mConfig->desiredChannels == 1) {
276 memcpy(&mConfig->pOutputBuffer[1024], &mConfig->pOutputBuffer[2048], numOutBytes * 2);
281 if (decoderErr != MP4AUDEC_SUCCESS) {
282 LOGW("AAC decoder returned error %d, substituting silence", decoderErr);
284 memset(buffer->data(), 0, numOutBytes);
286 // Discard input buffer.
287 mInputBuffer->release();
293 buffer->set_range(0, numOutBytes);
295 if (mInputBuffer != NULL) {
296 mInputBuffer->set_range(
297 mInputBuffer->range_offset() + mConfig->inputBufferUsedLength,
298 mInputBuffer->range_length() - mConfig->inputBufferUsedLength);
300 if (mInputBuffer->range_length() == 0) {
301 mInputBuffer->release();
306 buffer->meta_data()->setInt64(
309 + (mNumSamplesOutput * 1000000) / mConfig->samplingRate);
311 mNumSamplesOutput += mConfig->frameLength;
318 } // namespace android