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 | |
James Dong | 69ad25e | 2011-02-25 17:55:42 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MP3Decoder" |
| 19 | |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 20 | #include "MP3Decoder.h" |
| 21 | |
| 22 | #include "include/pvmp3decoder_api.h" |
| 23 | |
| 24 | #include <media/stagefright/MediaBufferGroup.h> |
| 25 | #include <media/stagefright/MediaDebug.h> |
| 26 | #include <media/stagefright/MediaDefs.h> |
| 27 | #include <media/stagefright/MetaData.h> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | MP3Decoder::MP3Decoder(const sp<MediaSource> &source) |
| 32 | : mSource(source), |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 33 | mNumChannels(0), |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 34 | mStarted(false), |
| 35 | mBufferGroup(NULL), |
| 36 | mConfig(new tPVMP3DecoderExternal), |
| 37 | mDecoderBuf(NULL), |
| 38 | mAnchorTimeUs(0), |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 39 | mNumFramesOutput(0), |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 40 | mInputBuffer(NULL) { |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 41 | init(); |
| 42 | } |
| 43 | |
| 44 | void MP3Decoder::init() { |
| 45 | sp<MetaData> srcFormat = mSource->getFormat(); |
| 46 | |
| 47 | int32_t sampleRate; |
| 48 | CHECK(srcFormat->findInt32(kKeyChannelCount, &mNumChannels)); |
| 49 | CHECK(srcFormat->findInt32(kKeySampleRate, &sampleRate)); |
| 50 | |
| 51 | mMeta = new MetaData; |
| 52 | mMeta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW); |
| 53 | mMeta->setInt32(kKeyChannelCount, mNumChannels); |
| 54 | mMeta->setInt32(kKeySampleRate, sampleRate); |
| 55 | |
| 56 | int64_t durationUs; |
| 57 | if (srcFormat->findInt64(kKeyDuration, &durationUs)) { |
| 58 | mMeta->setInt64(kKeyDuration, durationUs); |
| 59 | } |
| 60 | |
| 61 | mMeta->setCString(kKeyDecoderComponent, "MP3Decoder"); |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | MP3Decoder::~MP3Decoder() { |
| 65 | if (mStarted) { |
| 66 | stop(); |
| 67 | } |
| 68 | |
| 69 | delete mConfig; |
| 70 | mConfig = NULL; |
| 71 | } |
| 72 | |
| 73 | status_t MP3Decoder::start(MetaData *params) { |
| 74 | CHECK(!mStarted); |
| 75 | |
| 76 | mBufferGroup = new MediaBufferGroup; |
| 77 | mBufferGroup->add_buffer(new MediaBuffer(4608 * 2)); |
| 78 | |
| 79 | mConfig->equalizerType = flat; |
Andreas Huber | 43595bc | 2010-03-18 10:51:09 -0700 | [diff] [blame] | 80 | mConfig->crcEnabled = false; |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 81 | |
| 82 | uint32_t memRequirements = pvmp3_decoderMemRequirements(); |
| 83 | mDecoderBuf = malloc(memRequirements); |
| 84 | |
| 85 | pvmp3_InitDecoder(mConfig, mDecoderBuf); |
| 86 | |
| 87 | mSource->start(); |
| 88 | |
| 89 | mAnchorTimeUs = 0; |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 90 | mNumFramesOutput = 0; |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 91 | mStarted = true; |
| 92 | |
| 93 | return OK; |
| 94 | } |
| 95 | |
| 96 | status_t MP3Decoder::stop() { |
| 97 | CHECK(mStarted); |
| 98 | |
| 99 | if (mInputBuffer) { |
| 100 | mInputBuffer->release(); |
| 101 | mInputBuffer = NULL; |
| 102 | } |
| 103 | |
| 104 | free(mDecoderBuf); |
| 105 | mDecoderBuf = NULL; |
| 106 | |
| 107 | delete mBufferGroup; |
| 108 | mBufferGroup = NULL; |
| 109 | |
| 110 | mSource->stop(); |
| 111 | |
| 112 | mStarted = false; |
| 113 | |
| 114 | return OK; |
| 115 | } |
| 116 | |
| 117 | sp<MetaData> MP3Decoder::getFormat() { |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 118 | return mMeta; |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | status_t MP3Decoder::read( |
| 122 | MediaBuffer **out, const ReadOptions *options) { |
| 123 | status_t err; |
| 124 | |
| 125 | *out = NULL; |
| 126 | |
| 127 | int64_t seekTimeUs; |
Andreas Huber | 6624c9f | 2010-07-20 15:04:28 -0700 | [diff] [blame] | 128 | ReadOptions::SeekMode mode; |
| 129 | if (options && options->getSeekTo(&seekTimeUs, &mode)) { |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 130 | CHECK(seekTimeUs >= 0); |
| 131 | |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 132 | mNumFramesOutput = 0; |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 133 | |
| 134 | if (mInputBuffer) { |
| 135 | mInputBuffer->release(); |
| 136 | mInputBuffer = NULL; |
| 137 | } |
Andreas Huber | ad3fcfe | 2010-09-28 13:13:38 -0700 | [diff] [blame] | 138 | |
| 139 | // Make sure that the next buffer output does not still |
| 140 | // depend on fragments from the last one decoded. |
| 141 | pvmp3_InitDecoder(mConfig, mDecoderBuf); |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 142 | } else { |
| 143 | seekTimeUs = -1; |
| 144 | } |
| 145 | |
| 146 | if (mInputBuffer == NULL) { |
| 147 | err = mSource->read(&mInputBuffer, options); |
| 148 | |
| 149 | if (err != OK) { |
| 150 | return err; |
| 151 | } |
| 152 | |
| 153 | int64_t timeUs; |
| 154 | if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) { |
| 155 | mAnchorTimeUs = timeUs; |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 156 | mNumFramesOutput = 0; |
Andreas Huber | 8f65821 | 2009-12-07 16:32:37 -0800 | [diff] [blame] | 157 | } else { |
| 158 | // We must have a new timestamp after seeking. |
| 159 | CHECK(seekTimeUs < 0); |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | MediaBuffer *buffer; |
| 164 | CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK); |
| 165 | |
| 166 | mConfig->pInputBuffer = |
| 167 | (uint8_t *)mInputBuffer->data() + mInputBuffer->range_offset(); |
| 168 | |
| 169 | mConfig->inputBufferCurrentLength = mInputBuffer->range_length(); |
| 170 | mConfig->inputBufferMaxLength = 0; |
| 171 | mConfig->inputBufferUsedLength = 0; |
| 172 | |
| 173 | mConfig->outputFrameSize = buffer->size() / sizeof(int16_t); |
| 174 | mConfig->pOutputBuffer = static_cast<int16_t *>(buffer->data()); |
| 175 | |
Andreas Huber | ff0c5c1 | 2010-02-24 13:19:04 -0800 | [diff] [blame] | 176 | ERROR_CODE decoderErr; |
| 177 | if ((decoderErr = pvmp3_framedecoder(mConfig, mDecoderBuf)) |
| 178 | != NO_DECODING_ERROR) { |
| 179 | LOGV("mp3 decoder returned error %d", decoderErr); |
Andreas Huber | 5c1fe31 | 2010-01-12 11:57:24 -0800 | [diff] [blame] | 180 | |
James Dong | 69ad25e | 2011-02-25 17:55:42 -0800 | [diff] [blame] | 181 | if (decoderErr != NO_ENOUGH_MAIN_DATA_ERROR || |
| 182 | mConfig->outputFrameSize == 0) { |
| 183 | |
| 184 | if (mConfig->outputFrameSize == 0) { |
| 185 | LOGE("Output frame size is 0"); |
| 186 | } |
Andreas Huber | ff0c5c1 | 2010-02-24 13:19:04 -0800 | [diff] [blame] | 187 | buffer->release(); |
| 188 | buffer = NULL; |
Andreas Huber | afce215 | 2010-01-11 14:21:26 -0800 | [diff] [blame] | 189 | |
Andreas Huber | ff0c5c1 | 2010-02-24 13:19:04 -0800 | [diff] [blame] | 190 | mInputBuffer->release(); |
| 191 | mInputBuffer = NULL; |
| 192 | |
| 193 | return UNKNOWN_ERROR; |
| 194 | } |
| 195 | |
| 196 | // This is recoverable, just ignore the current frame and |
| 197 | // play silence instead. |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 198 | memset(buffer->data(), 0, mConfig->outputFrameSize * sizeof(int16_t)); |
Andreas Huber | ff0c5c1 | 2010-02-24 13:19:04 -0800 | [diff] [blame] | 199 | mConfig->inputBufferUsedLength = mInputBuffer->range_length(); |
Andreas Huber | afce215 | 2010-01-11 14:21:26 -0800 | [diff] [blame] | 200 | } |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 201 | |
| 202 | buffer->set_range( |
| 203 | 0, mConfig->outputFrameSize * sizeof(int16_t)); |
| 204 | |
| 205 | mInputBuffer->set_range( |
| 206 | mInputBuffer->range_offset() + mConfig->inputBufferUsedLength, |
| 207 | mInputBuffer->range_length() - mConfig->inputBufferUsedLength); |
| 208 | |
| 209 | if (mInputBuffer->range_length() == 0) { |
| 210 | mInputBuffer->release(); |
| 211 | mInputBuffer = NULL; |
| 212 | } |
| 213 | |
| 214 | buffer->meta_data()->setInt64( |
| 215 | kKeyTime, |
| 216 | mAnchorTimeUs |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 217 | + (mNumFramesOutput * 1000000) / mConfig->samplingRate); |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 218 | |
Andreas Huber | b64af9a | 2010-06-23 11:11:58 -0700 | [diff] [blame] | 219 | mNumFramesOutput += mConfig->outputFrameSize / mNumChannels; |
Andreas Huber | 250f243 | 2009-12-07 14:22:35 -0800 | [diff] [blame] | 220 | |
| 221 | *out = buffer; |
| 222 | |
| 223 | return OK; |
| 224 | } |
| 225 | |
| 226 | } // namespace android |