OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / media / libstagefright / codecs / aacdec / AACDecoder.cpp
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
17 #include "AACDecoder.h"
18 #define LOG_TAG "AACDecoder"
19
20 #include "../../include/ESDS.h"
21
22 #include "pvmp4audiodecoder_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 AACDecoder::AACDecoder(const sp<MediaSource> &source)
32     : mSource(source),
33       mStarted(false),
34       mBufferGroup(NULL),
35       mConfig(new tPVMP4AudioDecoderExternal),
36       mDecoderBuf(NULL),
37       mAnchorTimeUs(0),
38       mNumSamplesOutput(0),
39       mInputBuffer(NULL) {
40
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);
57     }
58     mMeta->setCString(kKeyDecoderComponent, "AACDecoder");
59
60     mInitCheck = initCheck();
61 }
62
63 status_t AACDecoder::initCheck() {
64     memset(mConfig, 0, sizeof(tPVMP4AudioDecoderExternal));
65     mConfig->outputFormat = OUTPUTFORMAT_16PCM_INTERLEAVED;
66     mConfig->aacPlusEnabled = 1;
67
68     // The software decoder doesn't properly support mono output on
69     // AACplus files. Always output stereo.
70     mConfig->desiredChannels = 2;
71
72     UInt32 memRequirements = PVMP4AudioDecoderGetMemRequirements();
73     mDecoderBuf = malloc(memRequirements);
74
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     }
80
81     uint32_t type;
82     const void *data;
83     size_t size;
84     sp<MetaData> meta = mSource->getFormat();
85     if (meta->findData(kKeyESDS, &type, &data, &size)) {
86         ESDS esds((const char *)data, size);
87         CHECK_EQ(esds.InitCheck(), OK);
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;
97
98         if (PVMP4AudioDecoderConfig(mConfig, mDecoderBuf)
99                 != MP4AUDEC_SUCCESS) {
100             return ERROR_UNSUPPORTED;
101         }
102     }
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));
120
121     mSource->start();
122
123     mAnchorTimeUs = 0;
124     mNumSamplesOutput = 0;
125     mStarted = true;
126     mNumDecodedBuffers = 0;
127     mUpsamplingFactor = 2;
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() {
154     return mMeta;
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;
164     ReadOptions::SeekMode mode;
165     if (options && options->getSeekTo(&seekTimeUs, &mode)) {
166         CHECK(seekTimeUs >= 0);
167
168         mNumSamplesOutput = 0;
169
170         if (mInputBuffer) {
171             mInputBuffer->release();
172             mInputBuffer = NULL;
173         }
174
175         // Make sure that the next buffer output does not still
176         // depend on fragments from the last one decoded.
177         PVMP4AudioDecoderResetBuffer(mDecoderBuf);
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
189         int64_t timeUs;
190         if (mInputBuffer->meta_data()->findInt64(kKeyTime, &timeUs)) {
191             mAnchorTimeUs = timeUs;
192             mNumSamplesOutput = 0;
193         } else {
194             // We must have a new timestamp after seeking.
195             CHECK(seekTimeUs < 0);
196         }
197     }
198
199     MediaBuffer *buffer;
200     CHECK_EQ(mBufferGroup->acquire_buffer(&buffer), OK);
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());
211     mConfig->pOutputBuffer_plus = &mConfig->pOutputBuffer[2048];
212     mConfig->repositionFlag = false;
213
214     Int decoderErr = PVMP4AudioDecodeFrame(mConfig, mDecoderBuf);
215
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         }
270     }
271
272     size_t numOutBytes =
273         mConfig->frameLength * sizeof(int16_t) * mConfig->desiredChannels;
274     if (mUpsamplingFactor == 2) {
275         if (mConfig->desiredChannels == 1) {
276             memcpy(&mConfig->pOutputBuffer[1024], &mConfig->pOutputBuffer[2048], numOutBytes * 2);
277         }
278         numOutBytes *= 2;
279     }
280
281     if (decoderErr != MP4AUDEC_SUCCESS) {
282         LOGW("AAC decoder returned error %d, substituting silence", decoderErr);
283
284         memset(buffer->data(), 0, numOutBytes);
285
286         // Discard input buffer.
287         mInputBuffer->release();
288         mInputBuffer = NULL;
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         }
304     }
305
306     buffer->meta_data()->setInt64(
307             kKeyTime,
308             mAnchorTimeUs
309                 + (mNumSamplesOutput * 1000000) / mConfig->samplingRate);
310
311     mNumSamplesOutput += mConfig->frameLength;
312
313     *out = buffer;
314
315     return OK;
316 }
317
318 }  // namespace android