SoftMP3.cpp revision c71a99131ca97bd3bdc5b78473d06e613e96e073
1/*
2 * Copyright (C) 2011 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//#define LOG_NDEBUG 0
18#define LOG_TAG "SoftMP3"
19#include <utils/Log.h>
20
21#include "SoftMP3.h"
22
23#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/MediaDefs.h>
25
26#include "include/pvmp3decoder_api.h"
27
28namespace android {
29
30template<class T>
31static void InitOMXParams(T *params) {
32    params->nSize = sizeof(T);
33    params->nVersion.s.nVersionMajor = 1;
34    params->nVersion.s.nVersionMinor = 0;
35    params->nVersion.s.nRevision = 0;
36    params->nVersion.s.nStep = 0;
37}
38
39SoftMP3::SoftMP3(
40        const char *name,
41        const OMX_CALLBACKTYPE *callbacks,
42        OMX_PTR appData,
43        OMX_COMPONENTTYPE **component)
44    : SimpleSoftOMXComponent(name, callbacks, appData, component),
45      mConfig(new tPVMP3DecoderExternal),
46      mDecoderBuf(NULL),
47      mAnchorTimeUs(0),
48      mNumFramesOutput(0),
49      mNumChannels(2),
50      mSamplingRate(44100),
51      mSignalledError(false),
52      mSawInputEos(false),
53      mSignalledOutputEos(false),
54      mOutputPortSettingsChange(NONE) {
55    initPorts();
56    initDecoder();
57}
58
59SoftMP3::~SoftMP3() {
60    if (mDecoderBuf != NULL) {
61        free(mDecoderBuf);
62        mDecoderBuf = NULL;
63    }
64
65    delete mConfig;
66    mConfig = NULL;
67}
68
69void SoftMP3::initPorts() {
70    OMX_PARAM_PORTDEFINITIONTYPE def;
71    InitOMXParams(&def);
72
73    def.nPortIndex = 0;
74    def.eDir = OMX_DirInput;
75    def.nBufferCountMin = kNumBuffers;
76    def.nBufferCountActual = def.nBufferCountMin;
77    def.nBufferSize = 8192;
78    def.bEnabled = OMX_TRUE;
79    def.bPopulated = OMX_FALSE;
80    def.eDomain = OMX_PortDomainAudio;
81    def.bBuffersContiguous = OMX_FALSE;
82    def.nBufferAlignment = 1;
83
84    def.format.audio.cMIMEType =
85        const_cast<char *>(MEDIA_MIMETYPE_AUDIO_MPEG);
86
87    def.format.audio.pNativeRender = NULL;
88    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
89    def.format.audio.eEncoding = OMX_AUDIO_CodingMP3;
90
91    addPort(def);
92
93    def.nPortIndex = 1;
94    def.eDir = OMX_DirOutput;
95    def.nBufferCountMin = kNumBuffers;
96    def.nBufferCountActual = def.nBufferCountMin;
97    def.nBufferSize = kOutputBufferSize;
98    def.bEnabled = OMX_TRUE;
99    def.bPopulated = OMX_FALSE;
100    def.eDomain = OMX_PortDomainAudio;
101    def.bBuffersContiguous = OMX_FALSE;
102    def.nBufferAlignment = 2;
103
104    def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
105    def.format.audio.pNativeRender = NULL;
106    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
107    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
108
109    addPort(def);
110}
111
112void SoftMP3::initDecoder() {
113    mConfig->equalizerType = flat;
114    mConfig->crcEnabled = false;
115
116    uint32_t memRequirements = pvmp3_decoderMemRequirements();
117    mDecoderBuf = malloc(memRequirements);
118
119    pvmp3_InitDecoder(mConfig, mDecoderBuf);
120    mIsFirst = true;
121}
122
123OMX_ERRORTYPE SoftMP3::internalGetParameter(
124        OMX_INDEXTYPE index, OMX_PTR params) {
125    switch (index) {
126        case OMX_IndexParamAudioPcm:
127        {
128            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
129                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
130
131            if (pcmParams->nPortIndex > 1) {
132                return OMX_ErrorUndefined;
133            }
134
135            pcmParams->eNumData = OMX_NumericalDataSigned;
136            pcmParams->eEndian = OMX_EndianBig;
137            pcmParams->bInterleaved = OMX_TRUE;
138            pcmParams->nBitPerSample = 16;
139            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
140            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
141            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
142
143            pcmParams->nChannels = mNumChannels;
144            pcmParams->nSamplingRate = mSamplingRate;
145
146            return OMX_ErrorNone;
147        }
148
149        default:
150            return SimpleSoftOMXComponent::internalGetParameter(index, params);
151    }
152}
153
154OMX_ERRORTYPE SoftMP3::internalSetParameter(
155        OMX_INDEXTYPE index, const OMX_PTR params) {
156    switch (index) {
157        case OMX_IndexParamStandardComponentRole:
158        {
159            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
160                (const OMX_PARAM_COMPONENTROLETYPE *)params;
161
162            if (strncmp((const char *)roleParams->cRole,
163                        "audio_decoder.mp3",
164                        OMX_MAX_STRINGNAME_SIZE - 1)) {
165                return OMX_ErrorUndefined;
166            }
167
168            return OMX_ErrorNone;
169        }
170
171        case OMX_IndexParamAudioPcm:
172        {
173            const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
174                (const OMX_AUDIO_PARAM_PCMMODETYPE *)params;
175
176            if (pcmParams->nPortIndex != 1) {
177                return OMX_ErrorUndefined;
178            }
179
180            mNumChannels = pcmParams->nChannels;
181            mSamplingRate = pcmParams->nSamplingRate;
182
183            return OMX_ErrorNone;
184        }
185
186        default:
187            return SimpleSoftOMXComponent::internalSetParameter(index, params);
188    }
189}
190
191void SoftMP3::onQueueFilled(OMX_U32 portIndex) {
192    if (mSignalledError || mOutputPortSettingsChange != NONE) {
193        return;
194    }
195
196    List<BufferInfo *> &inQueue = getPortQueue(0);
197    List<BufferInfo *> &outQueue = getPortQueue(1);
198
199    while ((!inQueue.empty() || (mSawInputEos && !mSignalledOutputEos)) && !outQueue.empty()) {
200        BufferInfo *inInfo = NULL;
201        OMX_BUFFERHEADERTYPE *inHeader = NULL;
202        if (!inQueue.empty()) {
203            inInfo = *inQueue.begin();
204            inHeader = inInfo->mHeader;
205        }
206
207        BufferInfo *outInfo = *outQueue.begin();
208        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
209        outHeader->nFlags = 0;
210
211        if (inHeader) {
212            if (inHeader->nOffset == 0 && inHeader->nFilledLen) {
213                mAnchorTimeUs = inHeader->nTimeStamp;
214                mNumFramesOutput = 0;
215            }
216
217            if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
218                mSawInputEos = true;
219            }
220
221            mConfig->pInputBuffer =
222                inHeader->pBuffer + inHeader->nOffset;
223
224            mConfig->inputBufferCurrentLength = inHeader->nFilledLen;
225        } else {
226            mConfig->pInputBuffer = NULL;
227            mConfig->inputBufferCurrentLength = 0;
228        }
229        mConfig->inputBufferMaxLength = 0;
230        mConfig->inputBufferUsedLength = 0;
231
232        mConfig->outputFrameSize = kOutputBufferSize / sizeof(int16_t);
233
234        mConfig->pOutputBuffer =
235            reinterpret_cast<int16_t *>(outHeader->pBuffer);
236
237        ERROR_CODE decoderErr;
238        if ((decoderErr = pvmp3_framedecoder(mConfig, mDecoderBuf))
239                != NO_DECODING_ERROR) {
240            ALOGV("mp3 decoder returned error %d", decoderErr);
241
242            if (decoderErr != NO_ENOUGH_MAIN_DATA_ERROR
243                        && decoderErr != SIDE_INFO_ERROR) {
244                ALOGE("mp3 decoder returned error %d", decoderErr);
245
246                notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
247                mSignalledError = true;
248                return;
249            }
250
251            if (mConfig->outputFrameSize == 0) {
252                mConfig->outputFrameSize = kOutputBufferSize / sizeof(int16_t);
253            }
254
255            if (decoderErr == NO_ENOUGH_MAIN_DATA_ERROR && mSawInputEos) {
256                if (!mIsFirst) {
257                    // pad the end of the stream with 529 samples, since that many samples
258                    // were trimmed off the beginning when decoding started
259                    outHeader->nOffset = 0;
260                    outHeader->nFilledLen = kPVMP3DecoderDelay * mNumChannels * sizeof(int16_t);
261
262                    memset(outHeader->pBuffer, 0, outHeader->nFilledLen);
263                }
264                outHeader->nFlags = OMX_BUFFERFLAG_EOS;
265                mSignalledOutputEos = true;
266            } else {
267                // This is recoverable, just ignore the current frame and
268                // play silence instead.
269                memset(outHeader->pBuffer,
270                       0,
271                       mConfig->outputFrameSize * sizeof(int16_t));
272
273                if (inHeader) {
274                    mConfig->inputBufferUsedLength = inHeader->nFilledLen;
275                }
276            }
277        } else if (mConfig->samplingRate != mSamplingRate
278                || mConfig->num_channels != mNumChannels) {
279            mSamplingRate = mConfig->samplingRate;
280            mNumChannels = mConfig->num_channels;
281
282            notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
283            mOutputPortSettingsChange = AWAITING_DISABLED;
284            return;
285        }
286
287        if (mIsFirst) {
288            mIsFirst = false;
289            // The decoder delay is 529 samples, so trim that many samples off
290            // the start of the first output buffer. This essentially makes this
291            // decoder have zero delay, which the rest of the pipeline assumes.
292            outHeader->nOffset =
293                kPVMP3DecoderDelay * mNumChannels * sizeof(int16_t);
294
295            outHeader->nFilledLen =
296                mConfig->outputFrameSize * sizeof(int16_t) - outHeader->nOffset;
297        } else if (!mSignalledOutputEos) {
298            outHeader->nOffset = 0;
299            outHeader->nFilledLen = mConfig->outputFrameSize * sizeof(int16_t);
300        }
301
302        outHeader->nTimeStamp =
303            mAnchorTimeUs
304                + (mNumFramesOutput * 1000000ll) / mConfig->samplingRate;
305
306        if (inHeader) {
307            CHECK_GE(inHeader->nFilledLen, mConfig->inputBufferUsedLength);
308
309            inHeader->nOffset += mConfig->inputBufferUsedLength;
310            inHeader->nFilledLen -= mConfig->inputBufferUsedLength;
311
312
313            if (inHeader->nFilledLen == 0) {
314                inInfo->mOwnedByUs = false;
315                inQueue.erase(inQueue.begin());
316                inInfo = NULL;
317                notifyEmptyBufferDone(inHeader);
318                inHeader = NULL;
319            }
320        }
321
322        mNumFramesOutput += mConfig->outputFrameSize / mNumChannels;
323
324        outInfo->mOwnedByUs = false;
325        outQueue.erase(outQueue.begin());
326        outInfo = NULL;
327        notifyFillBufferDone(outHeader);
328        outHeader = NULL;
329    }
330}
331
332void SoftMP3::onPortFlushCompleted(OMX_U32 portIndex) {
333    if (portIndex == 0) {
334        // Make sure that the next buffer output does not still
335        // depend on fragments from the last one decoded.
336        pvmp3_InitDecoder(mConfig, mDecoderBuf);
337        mIsFirst = true;
338    }
339}
340
341void SoftMP3::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
342    if (portIndex != 1) {
343        return;
344    }
345
346    switch (mOutputPortSettingsChange) {
347        case NONE:
348            break;
349
350        case AWAITING_DISABLED:
351        {
352            CHECK(!enabled);
353            mOutputPortSettingsChange = AWAITING_ENABLED;
354            break;
355        }
356
357        default:
358        {
359            CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
360            CHECK(enabled);
361            mOutputPortSettingsChange = NONE;
362            break;
363        }
364    }
365}
366
367void SoftMP3::onReset() {
368    pvmp3_InitDecoder(mConfig, mDecoderBuf);
369    mIsFirst = true;
370    mSignalledError = false;
371    mSawInputEos = false;
372    mSignalledOutputEos = false;
373    mOutputPortSettingsChange = NONE;
374}
375
376}  // namespace android
377
378android::SoftOMXComponent *createSoftOMXComponent(
379        const char *name, const OMX_CALLBACKTYPE *callbacks,
380        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
381    return new android::SoftMP3(name, callbacks, appData, component);
382}
383