blob: 208431c670e3444f6c3bcee321ffe58ced1762d2 [file] [log] [blame]
Andreas Huber3c01bb62009-12-10 12:28:22 -08001/*
2 * Copyright (C) 2009 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
Andreas Huberdacaa732009-12-07 09:56:32 -080017#include "AACDecoder.h"
James Donga7c14072010-07-08 20:56:13 -070018#define LOG_TAG "AACDecoder"
Andreas Huberdacaa732009-12-07 09:56:32 -080019
20#include "../../include/ESDS.h"
21
22#include "pvmp4audiodecoder_api.h"
23
Andreas Huber9c5d62a2010-12-15 14:07:50 -080024#include <media/stagefright/foundation/ADebug.h>
Andreas Huberdacaa732009-12-07 09:56:32 -080025#include <media/stagefright/MediaBufferGroup.h>
Andreas Huberdacaa732009-12-07 09:56:32 -080026#include <media/stagefright/MediaDefs.h>
27#include <media/stagefright/MetaData.h>
28
29namespace android {
30
31AACDecoder::AACDecoder(const sp<MediaSource> &source)
32 : mSource(source),
33 mStarted(false),
34 mBufferGroup(NULL),
35 mConfig(new tPVMP4AudioDecoderExternal),
36 mDecoderBuf(NULL),
Andreas Huber8f658212009-12-07 16:32:37 -080037 mAnchorTimeUs(0),
Andreas Huberdacaa732009-12-07 09:56:32 -080038 mNumSamplesOutput(0),
39 mInputBuffer(NULL) {
Andreas Huberdacaa732009-12-07 09:56:32 -080040
James Donga7c14072010-07-08 20:56:13 -070041 sp<MetaData> srcFormat = mSource->getFormat();
42
43 int32_t sampleRate;
44 CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate));
45
46 mMeta = new MetaData;
47 mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
48
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);
53
54 int64_t durationUs;
55 if (srcFormat->findInt64(kKeyDuration, &durationUs)) {
56 mMeta->setInt64(kKeyDuration, durationUs);
Andreas Huberdacaa732009-12-07 09:56:32 -080057 }
James Donga7c14072010-07-08 20:56:13 -070058 mMeta->setCString(kKeyDecoderComponent, "AACDecoder");
Andreas Huberdacaa732009-12-07 09:56:32 -080059
James Donga7c14072010-07-08 20:56:13 -070060 mInitCheck = initCheck();
Andreas Huberdacaa732009-12-07 09:56:32 -080061}
62
James Donga7c14072010-07-08 20:56:13 -070063status_t AACDecoder::initCheck() {
64 memset(mConfig, 0, sizeof(tPVMP4AudioDecoderExternal));
Andreas Huberdacaa732009-12-07 09:56:32 -080065 mConfig->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED;
James Donga7c14072010-07-08 20:56:13 -070066 mConfig->aacPlusEnabled = 1;
Andreas Huberdacaa732009-12-07 09:56:32 -080067
Andreas Huber749c5702010-02-26 09:47:28 -080068 // The software decoder doesn't properly support mono output on
69 // AACplus files. Always output stereo.
70 mConfig->desiredChannels = 2;
Andreas Huberdacaa732009-12-07 09:56:32 -080071
72 UInt32 memRequirements = PVMP4AudioDecoderGetMemRequirements();
73 mDecoderBuf = malloc(memRequirements);
74
James Donga7c14072010-07-08 20:56:13 -070075 status_t err = PVMP4AudioDecoderInitLibrary(mConfig, mDecoderBuf);
76 if (err != MP4AUDEC_SUCCESS) {
77 LOGE("Failed to initialize MP4 audio decoder");
78 return UNKNOWN_ERROR;
79 }
Andreas Huberdacaa732009-12-07 09:56:32 -080080
81 uint32_t type;
82 const void *data;
83 size_t size;
Andreas Huberdd0359f2010-01-05 11:13:08 -080084 sp<MetaData> meta = mSource->getFormat();
85 if (meta->findData(kKeyESDS, &type, &data, &size)) {
Andreas Huberdacaa732009-12-07 09:56:32 -080086 ESDS esds((const char *)data, size);
Andreas Huber9c5d62a2010-12-15 14:07:50 -080087 CHECK_EQ(esds.InitCheck(), (status_t)OK);
Andreas Huberdacaa732009-12-07 09:56:32 -080088
89 const void *codec_specific_data;
90 size_t codec_specific_data_size;
91 esds.getCodecSpecificInfo(
92 &codec_specific_data, &codec_specific_data_size);
93
94 mConfig->pInputBuffer = (UChar *)codec_specific_data;
95 mConfig->inputBufferCurrentLength = codec_specific_data_size;
96 mConfig->inputBufferMaxLength = 0;
Andreas Huberdacaa732009-12-07 09:56:32 -080097
Andreas Huber3c78a1b2010-05-13 09:15:21 -070098 if (PVMP4AudioDecoderConfig(mConfig, mDecoderBuf)
99 != MP4AUDEC_SUCCESS) {
100 return ERROR_UNSUPPORTED;
101 }
Andreas Huberdacaa732009-12-07 09:56:32 -0800102 }
James Donga7c14072010-07-08 20:56:13 -0700103 return OK;
104}
105
106AACDecoder::~AACDecoder() {
107 if (mStarted) {
108 stop();
109 }
110
111 delete mConfig;
112 mConfig = NULL;
113}
114
115status_t AACDecoder::start(MetaData *params) {
116 CHECK(!mStarted);
117
118 mBufferGroup = new MediaBufferGroup;
119 mBufferGroup->add_buffer(new MediaBuffer(4096 * 2));
Andreas Huberdacaa732009-12-07 09:56:32 -0800120
121 mSource->start();
122
Andreas Huber8f658212009-12-07 16:32:37 -0800123 mAnchorTimeUs = 0;
Andreas Huberdacaa732009-12-07 09:56:32 -0800124 mNumSamplesOutput = 0;
125 mStarted = true;
James Dong40da64f2010-09-17 20:50:07 -0700126 mNumDecodedBuffers = 0;
127 mUpsamplingFactor = 2;
Andreas Huberdacaa732009-12-07 09:56:32 -0800128
129 return OK;
130}
131
132status_t AACDecoder::stop() {
133 CHECK(mStarted);
134
135 if (mInputBuffer) {
136 mInputBuffer->release();
137 mInputBuffer = NULL;
138 }
139
140 free(mDecoderBuf);
141 mDecoderBuf = NULL;
142
143 delete mBufferGroup;
144 mBufferGroup = NULL;
145
146 mSource->stop();
147
148 mStarted = false;
149
150 return OK;
151}
152
153sp<MetaData> AACDecoder::getFormat() {
James Donga7c14072010-07-08 20:56:13 -0700154 return mMeta;
Andreas Huberdacaa732009-12-07 09:56:32 -0800155}
156
157status_t AACDecoder::read(
158 MediaBuffer **out, const ReadOptions *options) {
159 status_t err;
160
161 *out = NULL;
162
163 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -0700164 ReadOptions::SeekMode mode;
165 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huberdacaa732009-12-07 09:56:32 -0800166 CHECK(seekTimeUs >= 0);
167
168 mNumSamplesOutput = 0;
169
170 if (mInputBuffer) {
171 mInputBuffer->release();
172 mInputBuffer = NULL;
173 }
Andreas Huberad3fcfe2010-09-28 13:13:38 -0700174
175 // Make sure that the next buffer output does not still
176 // depend on fragments from the last one decoded.
177 PVMP4AudioDecoderResetBuffer(mDecoderBuf);
Andreas Huberdacaa732009-12-07 09:56:32 -0800178 } else {
179 seekTimeUs = -1;
180 }
181
182 if (mInputBuffer == NULL) {
183 err = mSource->read(&mInputBuffer, options);
184
185 if (err != OK) {
186 return err;
187 }
188
Andreas Huber8f658212009-12-07 16:32:37 -0800189 int64_t timeUs;
190 if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
191 mAnchorTimeUs = timeUs;
Andreas Huberdacaa732009-12-07 09:56:32 -0800192 mNumSamplesOutput = 0;
Andreas Huber8f658212009-12-07 16:32:37 -0800193 } else {
194 // We must have a new timestamp after seeking.
195 CHECK(seekTimeUs < 0);
Andreas Huberdacaa732009-12-07 09:56:32 -0800196 }
197 }
198
199 MediaBuffer *buffer;
Andreas Huber9c5d62a2010-12-15 14:07:50 -0800200 CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), (status_t)OK);
Andreas Huberdacaa732009-12-07 09:56:32 -0800201
202 mConfig->pInputBuffer =
203 (UChar *)mInputBuffer->data() + mInputBuffer->range_offset();
204
205 mConfig->inputBufferCurrentLength = mInputBuffer->range_length();
206 mConfig->inputBufferMaxLength = 0;
207 mConfig->inputBufferUsedLength = 0;
208 mConfig->remainderBits = 0;
209
210 mConfig->pOutputBuffer = static_cast<Int16 *>(buffer->data());
James Donga7c14072010-07-08 20:56:13 -0700211 mConfig->pOutputBuffer_plus = &mConfig->pOutputBuffer[2048];
Andreas Huberdacaa732009-12-07 09:56:32 -0800212 mConfig->repositionFlag = false;
213
Andreas Huberfe1dee82010-03-17 11:29:35 -0700214 Int decoderErr = PVMP4AudioDecodeFrame(mConfig, mDecoderBuf);
215
James Dong40da64f2010-09-17 20:50:07 -0700216 /*
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.
221 *
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.
226 *
227 * Thus, we could not say for sure whether a stream is
228 * AAC+/eAAC+ until the first data frame is decoded.
229 */
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);
235
236 CHECK(mNumDecodedBuffers > 0);
237 if (mNumDecodedBuffers == 1) {
238 mUpsamplingFactor = mConfig->aacPlusUpsamplingFactor;
239 // Check on the sampling rate to see whether it is changed.
240 int32_t sampleRate;
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);
246 buffer->release();
247 mInputBuffer->release();
248 mInputBuffer = NULL;
249 return INFO_FORMAT_CHANGED;
250 }
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;
259 }
260 } else {
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;
267 }
268 }
269 }
James Dong0c5f2ec2010-07-18 17:57:01 -0700270 }
271
Andreas Huber3f26cad2010-04-12 09:57:05 -0700272 size_t numOutBytes =
273 mConfig->frameLength * sizeof(int16_t) * mConfig->desiredChannels;
James Dong40da64f2010-09-17 20:50:07 -0700274 if (mUpsamplingFactor == 2) {
James Donga7c14072010-07-08 20:56:13 -0700275 if (mConfig->desiredChannels == 1) {
276 memcpy(&mConfig->pOutputBuffer[1024], &mConfig->pOutputBuffer[2048], numOutBytes * 2);
277 }
278 numOutBytes *= 2;
279 }
Andreas Huber3f26cad2010-04-12 09:57:05 -0700280
Andreas Huberfe1dee82010-03-17 11:29:35 -0700281 if (decoderErr != MP4AUDEC_SUCCESS) {
Andreas Huber3f26cad2010-04-12 09:57:05 -0700282 LOGW("AAC decoder returned error %d, substituting silence", decoderErr);
Andreas Huberfe1dee82010-03-17 11:29:35 -0700283
Andreas Huber3f26cad2010-04-12 09:57:05 -0700284 memset(buffer->data(), 0, numOutBytes);
Andreas Huberfe1dee82010-03-17 11:29:35 -0700285
Andreas Huber3f26cad2010-04-12 09:57:05 -0700286 // Discard input buffer.
Andreas Huberdacaa732009-12-07 09:56:32 -0800287 mInputBuffer->release();
288 mInputBuffer = NULL;
Andreas Huber3f26cad2010-04-12 09:57:05 -0700289
290 // fall through
291 }
292
293 buffer->set_range(0, numOutBytes);
294
295 if (mInputBuffer != NULL) {
296 mInputBuffer->set_range(
297 mInputBuffer->range_offset() + mConfig->inputBufferUsedLength,
298 mInputBuffer->range_length() - mConfig->inputBufferUsedLength);
299
300 if (mInputBuffer->range_length() == 0) {
301 mInputBuffer->release();
302 mInputBuffer = NULL;
303 }
Andreas Huberdacaa732009-12-07 09:56:32 -0800304 }
305
306 buffer->meta_data()->setInt64(
307 kKeyTime,
Andreas Huber8f658212009-12-07 16:32:37 -0800308 mAnchorTimeUs
Andreas Huberdacaa732009-12-07 09:56:32 -0800309 + (mNumSamplesOutput * 1000000) / mConfig->samplingRate);
310
Andreas Huber3d6576e2010-12-14 16:05:16 -0800311 mNumSamplesOutput += mConfig->frameLength * mUpsamplingFactor;
Andreas Huberdacaa732009-12-07 09:56:32 -0800312
313 *out = buffer;
314
315 return OK;
316}
317
318} // namespace android