blob: 0ba42ffdc1839e4caaa746ea054d7884bd1bf783 [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
James Dong69ad25e2011-02-25 17:55:42 -080017//#define LOG_NDEBUG 0
18#define LOG_TAG "MP3Decoder"
19
Andreas Huber250f2432009-12-07 14:22:35 -080020#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
29namespace android {
30
31MP3Decoder::MP3Decoder(const sp<MediaSource> &source)
32 : mSource(source),
Andreas Huberb64af9a2010-06-23 11:11:58 -070033 mNumChannels(0),
Andreas Huber250f2432009-12-07 14:22:35 -080034 mStarted(false),
35 mBufferGroup(NULL),
36 mConfig(new tPVMP3DecoderExternal),
37 mDecoderBuf(NULL),
38 mAnchorTimeUs(0),
Andreas Huberb64af9a2010-06-23 11:11:58 -070039 mNumFramesOutput(0),
Andreas Huber250f2432009-12-07 14:22:35 -080040 mInputBuffer(NULL) {
Andreas Huberb64af9a2010-06-23 11:11:58 -070041 init();
42}
43
44void 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 Huber250f2432009-12-07 14:22:35 -080062}
63
64MP3Decoder::~MP3Decoder() {
65 if (mStarted) {
66 stop();
67 }
68
69 delete mConfig;
70 mConfig = NULL;
71}
72
73status_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 Huber43595bc2010-03-18 10:51:09 -070080 mConfig->crcEnabled = false;
Andreas Huber250f2432009-12-07 14:22:35 -080081
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 Huberb64af9a2010-06-23 11:11:58 -070090 mNumFramesOutput = 0;
Andreas Huber250f2432009-12-07 14:22:35 -080091 mStarted = true;
92
93 return OK;
94}
95
96status_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
117sp<MetaData> MP3Decoder::getFormat() {
Andreas Huberb64af9a2010-06-23 11:11:58 -0700118 return mMeta;
Andreas Huber250f2432009-12-07 14:22:35 -0800119}
120
121status_t MP3Decoder::read(
122 MediaBuffer **out, const ReadOptions *options) {
123 status_t err;
124
125 *out = NULL;
126
127 int64_t seekTimeUs;
Andreas Huber6624c9f2010-07-20 15:04:28 -0700128 ReadOptions::SeekMode mode;
129 if (options && options->getSeekTo(&seekTimeUs, &mode)) {
Andreas Huber250f2432009-12-07 14:22:35 -0800130 CHECK(seekTimeUs >= 0);
131
Andreas Huberb64af9a2010-06-23 11:11:58 -0700132 mNumFramesOutput = 0;
Andreas Huber250f2432009-12-07 14:22:35 -0800133
134 if (mInputBuffer) {
135 mInputBuffer->release();
136 mInputBuffer = NULL;
137 }
Andreas Huberad3fcfe2010-09-28 13:13:38 -0700138
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 Huber250f2432009-12-07 14:22:35 -0800142 } 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 Huberb64af9a2010-06-23 11:11:58 -0700156 mNumFramesOutput = 0;
Andreas Huber8f658212009-12-07 16:32:37 -0800157 } else {
158 // We must have a new timestamp after seeking.
159 CHECK(seekTimeUs < 0);
Andreas Huber250f2432009-12-07 14:22:35 -0800160 }
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 Huberff0c5c12010-02-24 13:19:04 -0800176 ERROR_CODE decoderErr;
177 if ((decoderErr = pvmp3_framedecoder(mConfig, mDecoderBuf))
178 != NO_DECODING_ERROR) {
179 LOGV("mp3 decoder returned error %d", decoderErr);
Andreas Huber5c1fe312010-01-12 11:57:24 -0800180
James Dong69ad25e2011-02-25 17:55:42 -0800181 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 Huberff0c5c12010-02-24 13:19:04 -0800187 buffer->release();
188 buffer = NULL;
Andreas Huberafce2152010-01-11 14:21:26 -0800189
Andreas Huberff0c5c12010-02-24 13:19:04 -0800190 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 Huberb64af9a2010-06-23 11:11:58 -0700198 memset(buffer->data(), 0, mConfig->outputFrameSize * sizeof(int16_t));
Andreas Huberff0c5c12010-02-24 13:19:04 -0800199 mConfig->inputBufferUsedLength = mInputBuffer->range_length();
Andreas Huberafce2152010-01-11 14:21:26 -0800200 }
Andreas Huber250f2432009-12-07 14:22:35 -0800201
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 Huberb64af9a2010-06-23 11:11:58 -0700217 + (mNumFramesOutput * 1000000) / mConfig->samplingRate);
Andreas Huber250f2432009-12-07 14:22:35 -0800218
Andreas Huberb64af9a2010-06-23 11:11:58 -0700219 mNumFramesOutput += mConfig->outputFrameSize / mNumChannels;
Andreas Huber250f2432009-12-07 14:22:35 -0800220
221 *out = buffer;
222
223 return OK;
224}
225
226} // namespace android