Andreas Huber | 3c01bb6 | 2009-12-10 12:28:22 -0800 | [diff] [blame] | 1 | /* |
| 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 Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 17 | #include "AACDecoder.h" |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 18 | #define LOG_TAG "AACDecoder" |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 19 | |
| 20 | #include "../../include/ESDS.h" |
| 21 | |
| 22 | #include "pvmp4audiodecoder_api.h" |
| 23 | |
Andreas Huber | 9c5d62a | 2010-12-15 14:07:50 -0800 | [diff] [blame] | 24 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 25 | #include <media/stagefright/MediaBufferGroup.h> |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 26 | #include <media/stagefright/MediaDefs.h> |
| 27 | #include <media/stagefright/MetaData.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | AACDecoder::AACDecoder(const sp<MediaSource> &source) |
| 32 | : mSource(source), |
| 33 | mStarted(false), |
| 34 | mBufferGroup(NULL), |
| 35 | mConfig(new tPVMP4AudioDecoderExternal), |
| 36 | mDecoderBuf(NULL), |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 37 | mAnchorTimeUs(0), |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 38 | mNumSamplesOutput(0), |
| 39 | mInputBuffer(NULL) { |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 40 | |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 41 | 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 Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 57 | } |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 58 | mMeta->setCString(kKeyDecoderComponent, "AACDecoder"); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 59 | |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 60 | mInitCheck = initCheck(); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 61 | } |
| 62 | |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 63 | status_t AACDecoder::initCheck() { |
| 64 | memset(mConfig, 0, sizeof(tPVMP4AudioDecoderExternal)); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 65 | mConfig->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED; |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 66 | mConfig->aacPlusEnabled = 1; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 67 | |
Andreas Huber | 749c570 | 2010-02-26 09:47:28 -0800 | [diff] [blame] | 68 | // The software decoder doesn't properly support mono output on |
| 69 | // AACplus files. Always output stereo. |
| 70 | mConfig->desiredChannels = 2; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 71 | |
| 72 | UInt32 memRequirements = PVMP4AudioDecoderGetMemRequirements(); |
| 73 | mDecoderBuf = malloc(memRequirements); |
| 74 | |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 75 | 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 Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 80 | |
| 81 | uint32_t type; |
| 82 | const void *data; |
| 83 | size_t size; |
Andreas Huber | dd0359f | 2010-01-05 11:13:08 -0800 | [diff] [blame] | 84 | sp<MetaData> meta = mSource->getFormat(); |
| 85 | if (meta->findData(kKeyESDS, &type, &data, &size)) { |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 86 | ESDS esds((const char *)data, size); |
Andreas Huber | 9c5d62a | 2010-12-15 14:07:50 -0800 | [diff] [blame] | 87 | CHECK_EQ(esds.InitCheck(), (status_t)OK); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 88 | |
| 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 Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 97 | |
Andreas Huber | 3c78a1b | 2010-05-13 09:15:21 -0700 | [diff] [blame] | 98 | if (PVMP4AudioDecoderConfig(mConfig, mDecoderBuf) |
| 99 | != MP4AUDEC_SUCCESS) { |
| 100 | return ERROR_UNSUPPORTED; |
| 101 | } |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 102 | } |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | AACDecoder::~AACDecoder() { |
| 107 | if (mStarted) { |
| 108 | stop(); |
| 109 | } |
| 110 | |
| 111 | delete mConfig; |
| 112 | mConfig = NULL; |
| 113 | } |
| 114 | |
| 115 | status_t AACDecoder::start(MetaData *params) { |
| 116 | CHECK(!mStarted); |
| 117 | |
| 118 | mBufferGroup = new MediaBufferGroup; |
| 119 | mBufferGroup->add_buffer(new MediaBuffer(4096 * 2)); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 120 | |
| 121 | mSource->start(); |
| 122 | |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 123 | mAnchorTimeUs = 0; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 124 | mNumSamplesOutput = 0; |
| 125 | mStarted = true; |
James Dong | 40da64f | 2010-09-17 20:50:07 -0700 | [diff] [blame] | 126 | mNumDecodedBuffers = 0; |
| 127 | mUpsamplingFactor = 2; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 128 | |
| 129 | return OK; |
| 130 | } |
| 131 | |
| 132 | status_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 | |
| 153 | sp<MetaData> AACDecoder::getFormat() { |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 154 | return mMeta; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | status_t AACDecoder::read( |
| 158 | MediaBuffer **out, const ReadOptions *options) { |
| 159 | status_t err; |
| 160 | |
| 161 | *out = NULL; |
| 162 | |
| 163 | int64_t seekTimeUs; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 164 | ReadOptions::SeekMode mode; |
| 165 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 166 | CHECK(seekTimeUs >= 0); |
| 167 | |
| 168 | mNumSamplesOutput = 0; |
| 169 | |
| 170 | if (mInputBuffer) { |
| 171 | mInputBuffer->release(); |
| 172 | mInputBuffer = NULL; |
| 173 | } |
Andreas Huber | ad3fcfe | 2010-09-28 13:13:38 -0700 | [diff] [blame] | 174 | |
| 175 | // Make sure that the next buffer output does not still |
| 176 | // depend on fragments from the last one decoded. |
| 177 | PVMP4AudioDecoderResetBuffer(mDecoderBuf); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 178 | } 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 Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 189 | int64_t timeUs; |
| 190 | if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) { |
| 191 | mAnchorTimeUs = timeUs; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 192 | mNumSamplesOutput = 0; |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 193 | } else { |
| 194 | // We must have a new timestamp after seeking. |
| 195 | CHECK(seekTimeUs < 0); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | MediaBuffer *buffer; |
Andreas Huber | 9c5d62a | 2010-12-15 14:07:50 -0800 | [diff] [blame] | 200 | CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), (status_t)OK); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 201 | |
| 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 Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 211 | mConfig->pOutputBuffer_plus = &mConfig->pOutputBuffer[2048]; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 212 | mConfig->repositionFlag = false; |
| 213 | |
Andreas Huber | fe1dee8 | 2010-03-17 11:29:35 -0700 | [diff] [blame] | 214 | Int decoderErr = PVMP4AudioDecodeFrame(mConfig, mDecoderBuf); |
| 215 | |
James Dong | 40da64f | 2010-09-17 20:50:07 -0700 | [diff] [blame] | 216 | /* |
| 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 Dong | 0c5f2ec | 2010-07-18 17:57:01 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 272 | size_t numOutBytes = |
| 273 | mConfig->frameLength * sizeof(int16_t) * mConfig->desiredChannels; |
James Dong | 40da64f | 2010-09-17 20:50:07 -0700 | [diff] [blame] | 274 | if (mUpsamplingFactor == 2) { |
James Dong | a7c1407 | 2010-07-08 20:56:13 -0700 | [diff] [blame] | 275 | if (mConfig->desiredChannels == 1) { |
| 276 | memcpy(&mConfig->pOutputBuffer[1024], &mConfig->pOutputBuffer[2048], numOutBytes * 2); |
| 277 | } |
| 278 | numOutBytes *= 2; |
| 279 | } |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 280 | |
Andreas Huber | fe1dee8 | 2010-03-17 11:29:35 -0700 | [diff] [blame] | 281 | if (decoderErr != MP4AUDEC_SUCCESS) { |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 282 | LOGW("AAC decoder returned error %d, substituting silence", decoderErr); |
Andreas Huber | fe1dee8 | 2010-03-17 11:29:35 -0700 | [diff] [blame] | 283 | |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 284 | memset(buffer->data(), 0, numOutBytes); |
Andreas Huber | fe1dee8 | 2010-03-17 11:29:35 -0700 | [diff] [blame] | 285 | |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 286 | // Discard input buffer. |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 287 | mInputBuffer->release(); |
| 288 | mInputBuffer = NULL; |
Andreas Huber | 3f26cad | 2010-04-12 09:57:05 -0700 | [diff] [blame] | 289 | |
| 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 Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | buffer->meta_data()->setInt64( |
| 307 | kKeyTime, |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 308 | mAnchorTimeUs |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 309 | + (mNumSamplesOutput * 1000000) / mConfig->samplingRate); |
| 310 | |
Andreas Huber | 3d6576e | 2010-12-14 16:05:16 -0800 | [diff] [blame] | 311 | mNumSamplesOutput += mConfig->frameLength * mUpsamplingFactor; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 312 | |
| 313 | *out = buffer; |
| 314 | |
| 315 | return OK; |
| 316 | } |
| 317 | |
| 318 | } // namespace android |