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" |
| 18 | |
| 19 | #include "../../include/ESDS.h" |
| 20 | |
| 21 | #include "pvmp4audiodecoder_api.h" |
| 22 | |
| 23 | #include <media/stagefright/MediaBufferGroup.h> |
| 24 | #include <media/stagefright/MediaDebug.h> |
| 25 | #include <media/stagefright/MediaDefs.h> |
| 26 | #include <media/stagefright/MetaData.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | AACDecoder::AACDecoder(const sp<MediaSource> &source) |
| 31 | : mSource(source), |
| 32 | mStarted(false), |
| 33 | mBufferGroup(NULL), |
| 34 | mConfig(new tPVMP4AudioDecoderExternal), |
| 35 | mDecoderBuf(NULL), |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 36 | mAnchorTimeUs(0), |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 37 | mNumSamplesOutput(0), |
| 38 | mInputBuffer(NULL) { |
| 39 | } |
| 40 | |
| 41 | AACDecoder::~AACDecoder() { |
| 42 | if (mStarted) { |
| 43 | stop(); |
| 44 | } |
| 45 | |
| 46 | delete mConfig; |
| 47 | mConfig = NULL; |
| 48 | } |
| 49 | |
| 50 | status_t AACDecoder::start(MetaData *params) { |
| 51 | CHECK(!mStarted); |
| 52 | |
| 53 | mBufferGroup = new MediaBufferGroup; |
| 54 | mBufferGroup->add_buffer(new MediaBuffer(2048 * 2)); |
| 55 | |
| 56 | mConfig->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED; |
| 57 | mConfig->aacPlusUpsamplingFactor = 0; |
| 58 | mConfig->aacPlusEnabled = false; |
| 59 | |
Andreas Huber | 749c570 | 2010-02-26 09:47:28 -0800 | [diff] [blame] | 60 | // The software decoder doesn't properly support mono output on |
| 61 | // AACplus files. Always output stereo. |
| 62 | mConfig->desiredChannels = 2; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 63 | |
| 64 | UInt32 memRequirements = PVMP4AudioDecoderGetMemRequirements(); |
| 65 | mDecoderBuf = malloc(memRequirements); |
| 66 | |
| 67 | CHECK_EQ(PVMP4AudioDecoderInitLibrary(mConfig, mDecoderBuf), |
| 68 | MP4AUDEC_SUCCESS); |
| 69 | |
| 70 | uint32_t type; |
| 71 | const void *data; |
| 72 | size_t size; |
Andreas Huber | dd0359f | 2010-01-05 11:13:08 -0800 | [diff] [blame] | 73 | sp<MetaData> meta = mSource->getFormat(); |
| 74 | if (meta->findData(kKeyESDS, &type, &data, &size)) { |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 75 | ESDS esds((const char *)data, size); |
| 76 | CHECK_EQ(esds.InitCheck(), OK); |
| 77 | |
| 78 | const void *codec_specific_data; |
| 79 | size_t codec_specific_data_size; |
| 80 | esds.getCodecSpecificInfo( |
| 81 | &codec_specific_data, &codec_specific_data_size); |
| 82 | |
| 83 | mConfig->pInputBuffer = (UChar *)codec_specific_data; |
| 84 | mConfig->inputBufferCurrentLength = codec_specific_data_size; |
| 85 | mConfig->inputBufferMaxLength = 0; |
| 86 | mConfig->inputBufferUsedLength = 0; |
| 87 | mConfig->remainderBits = 0; |
| 88 | |
| 89 | mConfig->pOutputBuffer = NULL; |
| 90 | mConfig->pOutputBuffer_plus = NULL; |
| 91 | mConfig->repositionFlag = false; |
| 92 | |
| 93 | CHECK_EQ(PVMP4AudioDecoderConfig(mConfig, mDecoderBuf), |
| 94 | MP4AUDEC_SUCCESS); |
| 95 | } |
| 96 | |
| 97 | mSource->start(); |
| 98 | |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 99 | mAnchorTimeUs = 0; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 100 | mNumSamplesOutput = 0; |
| 101 | mStarted = true; |
| 102 | |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | status_t AACDecoder::stop() { |
| 107 | CHECK(mStarted); |
| 108 | |
| 109 | if (mInputBuffer) { |
| 110 | mInputBuffer->release(); |
| 111 | mInputBuffer = NULL; |
| 112 | } |
| 113 | |
| 114 | free(mDecoderBuf); |
| 115 | mDecoderBuf = NULL; |
| 116 | |
| 117 | delete mBufferGroup; |
| 118 | mBufferGroup = NULL; |
| 119 | |
| 120 | mSource->stop(); |
| 121 | |
| 122 | mStarted = false; |
| 123 | |
| 124 | return OK; |
| 125 | } |
| 126 | |
| 127 | sp<MetaData> AACDecoder::getFormat() { |
| 128 | sp<MetaData> srcFormat = mSource->getFormat(); |
| 129 | |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 130 | int32_t sampleRate; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 131 | CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate)); |
| 132 | |
| 133 | sp<MetaData> meta = new MetaData; |
| 134 | meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
Andreas Huber | 749c570 | 2010-02-26 09:47:28 -0800 | [diff] [blame] | 135 | |
| 136 | // We'll always output stereo, regardless of how many channels are |
| 137 | // present in the input due to decoder limitations. |
| 138 | meta->setInt32(kKeyChannelCount, 2); |
| 139 | |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 140 | meta->setInt32(kKeySampleRate, sampleRate); |
Andreas Huber | 85adf5e | 2009-12-11 11:27:02 -0800 | [diff] [blame] | 141 | |
| 142 | int64_t durationUs; |
| 143 | if (srcFormat->findInt64(kKeyDuration, &durationUs)) { |
| 144 | meta->setInt64(kKeyDuration, durationUs); |
| 145 | } |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 146 | |
| 147 | return meta; |
| 148 | } |
| 149 | |
| 150 | status_t AACDecoder::read( |
| 151 | MediaBuffer **out, const ReadOptions *options) { |
| 152 | status_t err; |
| 153 | |
| 154 | *out = NULL; |
| 155 | |
| 156 | int64_t seekTimeUs; |
| 157 | if (options && options->getSeekTo(&seekTimeUs)) { |
| 158 | CHECK(seekTimeUs >= 0); |
| 159 | |
| 160 | mNumSamplesOutput = 0; |
| 161 | |
| 162 | if (mInputBuffer) { |
| 163 | mInputBuffer->release(); |
| 164 | mInputBuffer = NULL; |
| 165 | } |
| 166 | } else { |
| 167 | seekTimeUs = -1; |
| 168 | } |
| 169 | |
| 170 | if (mInputBuffer == NULL) { |
| 171 | err = mSource->read(&mInputBuffer, options); |
| 172 | |
| 173 | if (err != OK) { |
| 174 | return err; |
| 175 | } |
| 176 | |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 177 | int64_t timeUs; |
| 178 | if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) { |
| 179 | mAnchorTimeUs = timeUs; |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 180 | mNumSamplesOutput = 0; |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 181 | } else { |
| 182 | // We must have a new timestamp after seeking. |
| 183 | CHECK(seekTimeUs < 0); |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | MediaBuffer *buffer; |
| 188 | CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK); |
| 189 | |
| 190 | mConfig->pInputBuffer = |
| 191 | (UChar *)mInputBuffer->data() + mInputBuffer->range_offset(); |
| 192 | |
| 193 | mConfig->inputBufferCurrentLength = mInputBuffer->range_length(); |
| 194 | mConfig->inputBufferMaxLength = 0; |
| 195 | mConfig->inputBufferUsedLength = 0; |
| 196 | mConfig->remainderBits = 0; |
| 197 | |
| 198 | mConfig->pOutputBuffer = static_cast<Int16 *>(buffer->data()); |
| 199 | mConfig->pOutputBuffer_plus = NULL; |
| 200 | mConfig->repositionFlag = false; |
| 201 | |
Andreas Huber | fe1dee8 | 2010-03-17 11:29:35 -0700 | [diff] [blame^] | 202 | Int decoderErr = PVMP4AudioDecodeFrame(mConfig, mDecoderBuf); |
| 203 | |
| 204 | if (decoderErr != MP4AUDEC_SUCCESS) { |
| 205 | LOGE("AAC decoder returned error %d", decoderErr); |
| 206 | |
| 207 | buffer->release(); |
| 208 | buffer = NULL; |
| 209 | |
| 210 | return ERROR_MALFORMED; |
| 211 | } |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 212 | |
| 213 | buffer->set_range( |
| 214 | 0, mConfig->frameLength * sizeof(int16_t) * mConfig->desiredChannels); |
| 215 | |
| 216 | mInputBuffer->set_range( |
| 217 | mInputBuffer->range_offset() + mConfig->inputBufferUsedLength, |
| 218 | mInputBuffer->range_length() - mConfig->inputBufferUsedLength); |
| 219 | |
| 220 | if (mInputBuffer->range_length() == 0) { |
| 221 | mInputBuffer->release(); |
| 222 | mInputBuffer = NULL; |
| 223 | } |
| 224 | |
| 225 | buffer->meta_data()->setInt64( |
| 226 | kKeyTime, |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 227 | mAnchorTimeUs |
Andreas Huber | dacaa73 | 2009-12-07 09:56:32 -0800 | [diff] [blame] | 228 | + (mNumSamplesOutput * 1000000) / mConfig->samplingRate); |
| 229 | |
| 230 | mNumSamplesOutput += mConfig->frameLength; |
| 231 | |
| 232 | *out = buffer; |
| 233 | |
| 234 | return OK; |
| 235 | } |
| 236 | |
| 237 | } // namespace android |