SoftAAC2.cpp revision bf2461ecc71c0aacf8c03fcdaf0dc46bc8285c7f
1/*
2 * Copyright (C) 2012 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_TAG "SoftAAC2"
18#include <utils/Log.h>
19
20#include "SoftAAC2.h"
21
22#include <media/stagefright/foundation/ADebug.h>
23#include <media/stagefright/foundation/hexdump.h>
24
25#define FILEREAD_MAX_LAYERS 2
26
27namespace android {
28
29template<class T>
30static void InitOMXParams(T *params) {
31    params->nSize = sizeof(T);
32    params->nVersion.s.nVersionMajor = 1;
33    params->nVersion.s.nVersionMinor = 0;
34    params->nVersion.s.nRevision = 0;
35    params->nVersion.s.nStep = 0;
36}
37
38SoftAAC2::SoftAAC2(
39        const char *name,
40        const OMX_CALLBACKTYPE *callbacks,
41        OMX_PTR appData,
42        OMX_COMPONENTTYPE **component)
43    : SimpleSoftOMXComponent(name, callbacks, appData, component),
44      mAACDecoder(NULL),
45      mStreamInfo(NULL),
46      mIsADTS(false),
47      mInputBufferCount(0),
48      mSignalledError(false),
49      mAnchorTimeUs(0),
50      mNumSamplesOutput(0),
51      mOutputPortSettingsChange(NONE) {
52    initPorts();
53    CHECK_EQ(initDecoder(), (status_t)OK);
54}
55
56SoftAAC2::~SoftAAC2() {
57    aacDecoder_Close(mAACDecoder);
58}
59
60void SoftAAC2::initPorts() {
61    OMX_PARAM_PORTDEFINITIONTYPE def;
62    InitOMXParams(&def);
63
64    def.nPortIndex = 0;
65    def.eDir = OMX_DirInput;
66    def.nBufferCountMin = kNumInputBuffers;
67    def.nBufferCountActual = def.nBufferCountMin;
68    def.nBufferSize = 8192;
69    def.bEnabled = OMX_TRUE;
70    def.bPopulated = OMX_FALSE;
71    def.eDomain = OMX_PortDomainAudio;
72    def.bBuffersContiguous = OMX_FALSE;
73    def.nBufferAlignment = 1;
74
75    def.format.audio.cMIMEType = const_cast<char *>("audio/aac");
76    def.format.audio.pNativeRender = NULL;
77    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
78    def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
79
80    addPort(def);
81
82    def.nPortIndex = 1;
83    def.eDir = OMX_DirOutput;
84    def.nBufferCountMin = kNumOutputBuffers;
85    def.nBufferCountActual = def.nBufferCountMin;
86    def.nBufferSize = 8192 * 2;
87    def.bEnabled = OMX_TRUE;
88    def.bPopulated = OMX_FALSE;
89    def.eDomain = OMX_PortDomainAudio;
90    def.bBuffersContiguous = OMX_FALSE;
91    def.nBufferAlignment = 2;
92
93    def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
94    def.format.audio.pNativeRender = NULL;
95    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
96    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
97
98    addPort(def);
99}
100
101status_t SoftAAC2::initDecoder() {
102    status_t status = UNKNOWN_ERROR;
103    mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1);
104    if (mAACDecoder != NULL) {
105        mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder);
106        if (mStreamInfo != NULL) {
107            status = OK;
108        }
109    }
110    mIsFirst = true;
111    return status;
112}
113
114OMX_ERRORTYPE SoftAAC2::internalGetParameter(
115        OMX_INDEXTYPE index, OMX_PTR params) {
116    switch (index) {
117        case OMX_IndexParamAudioAac:
118        {
119            OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
120                (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
121
122            if (aacParams->nPortIndex != 0) {
123                return OMX_ErrorUndefined;
124            }
125
126            aacParams->nBitRate = 0;
127            aacParams->nAudioBandWidth = 0;
128            aacParams->nAACtools = 0;
129            aacParams->nAACERtools = 0;
130            aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
131
132            aacParams->eAACStreamFormat =
133                mIsADTS
134                    ? OMX_AUDIO_AACStreamFormatMP4ADTS
135                    : OMX_AUDIO_AACStreamFormatMP4FF;
136
137            aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
138
139            if (!isConfigured()) {
140                aacParams->nChannels = 1;
141                aacParams->nSampleRate = 44100;
142                aacParams->nFrameLength = 0;
143            } else {
144                aacParams->nChannels = mStreamInfo->numChannels;
145                aacParams->nSampleRate = mStreamInfo->sampleRate;
146                aacParams->nFrameLength = mStreamInfo->frameSize;
147            }
148
149            return OMX_ErrorNone;
150        }
151
152        case OMX_IndexParamAudioPcm:
153        {
154            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
155                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
156
157            if (pcmParams->nPortIndex != 1) {
158                return OMX_ErrorUndefined;
159            }
160
161            pcmParams->eNumData = OMX_NumericalDataSigned;
162            pcmParams->eEndian = OMX_EndianBig;
163            pcmParams->bInterleaved = OMX_TRUE;
164            pcmParams->nBitPerSample = 16;
165            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
166            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
167            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
168            pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF;
169            pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE;
170            pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS;
171            pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS;
172
173            if (!isConfigured()) {
174                pcmParams->nChannels = 1;
175                pcmParams->nSamplingRate = 44100;
176            } else {
177                pcmParams->nChannels = mStreamInfo->numChannels;
178                pcmParams->nSamplingRate = mStreamInfo->sampleRate;
179            }
180
181            return OMX_ErrorNone;
182        }
183
184        default:
185            return SimpleSoftOMXComponent::internalGetParameter(index, params);
186    }
187
188}
189
190OMX_ERRORTYPE SoftAAC2::internalSetParameter(
191        OMX_INDEXTYPE index, const OMX_PTR params) {
192    switch (index) {
193        case OMX_IndexParamStandardComponentRole:
194        {
195            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
196                (const OMX_PARAM_COMPONENTROLETYPE *)params;
197
198            if (strncmp((const char *)roleParams->cRole,
199                        "audio_decoder.aac",
200                        OMX_MAX_STRINGNAME_SIZE - 1)) {
201                return OMX_ErrorUndefined;
202            }
203
204            return OMX_ErrorNone;
205        }
206
207        case OMX_IndexParamAudioAac:
208        {
209            const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
210                (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
211
212            if (aacParams->nPortIndex != 0) {
213                return OMX_ErrorUndefined;
214            }
215
216            if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) {
217                mIsADTS = false;
218            } else if (aacParams->eAACStreamFormat
219                        == OMX_AUDIO_AACStreamFormatMP4ADTS) {
220                mIsADTS = true;
221            } else {
222                return OMX_ErrorUndefined;
223            }
224
225            return OMX_ErrorNone;
226        }
227
228        case OMX_IndexParamAudioPcm:
229        {
230            const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
231                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
232
233            if (pcmParams->nPortIndex != 1) {
234                return OMX_ErrorUndefined;
235            }
236
237            return OMX_ErrorNone;
238        }
239
240        default:
241            return SimpleSoftOMXComponent::internalSetParameter(index, params);
242    }
243}
244
245bool SoftAAC2::isConfigured() const {
246    return mInputBufferCount > 0;
247}
248
249void SoftAAC2::onQueueFilled(OMX_U32 portIndex) {
250    if (mSignalledError || mOutputPortSettingsChange != NONE) {
251        return;
252    }
253
254    UCHAR* inBuffer[FILEREAD_MAX_LAYERS];
255    UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0};
256    UINT bytesValid[FILEREAD_MAX_LAYERS] = {0};
257
258    List<BufferInfo *> &inQueue = getPortQueue(0);
259    List<BufferInfo *> &outQueue = getPortQueue(1);
260
261    if (portIndex == 0 && mInputBufferCount == 0) {
262        ++mInputBufferCount;
263        BufferInfo *info = *inQueue.begin();
264        OMX_BUFFERHEADERTYPE *header = info->mHeader;
265
266        inBuffer[0] = header->pBuffer + header->nOffset;
267        inBufferLength[0] = header->nFilledLen;
268
269        AAC_DECODER_ERROR decoderErr =
270            aacDecoder_ConfigRaw(mAACDecoder,
271                                 inBuffer,
272                                 inBufferLength);
273
274        if (decoderErr != AAC_DEC_OK) {
275            mSignalledError = true;
276            notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
277            return;
278        }
279        inQueue.erase(inQueue.begin());
280        info->mOwnedByUs = false;
281        notifyEmptyBufferDone(header);
282
283        ALOGI("Configuring decoder: %d Hz, %d channels",
284              mStreamInfo->sampleRate,
285              mStreamInfo->numChannels);
286        notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
287        mOutputPortSettingsChange = AWAITING_DISABLED;
288        return;
289    }
290
291    while (!inQueue.empty() && !outQueue.empty()) {
292        BufferInfo *inInfo = *inQueue.begin();
293        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
294
295        BufferInfo *outInfo = *outQueue.begin();
296        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
297
298        if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
299            inQueue.erase(inQueue.begin());
300            inInfo->mOwnedByUs = false;
301            notifyEmptyBufferDone(inHeader);
302
303            // flush out the decoder's delayed data by calling DecodeFrame one more time, with
304            // the AACDEC_FLUSH flag set
305            INT_PCM *outBuffer =
306                    reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
307            AAC_DECODER_ERROR decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
308                                                                  outBuffer,
309                                                                  outHeader->nAllocLen,
310                                                                  AACDEC_FLUSH);
311            if (decoderErr != AAC_DEC_OK) {
312                mSignalledError = true;
313                notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
314                return;
315            }
316
317            outHeader->nFilledLen =
318                    mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
319            outHeader->nFlags = OMX_BUFFERFLAG_EOS;
320
321            outQueue.erase(outQueue.begin());
322            outInfo->mOwnedByUs = false;
323            notifyFillBufferDone(outHeader);
324            return;
325        }
326
327        if (inHeader->nOffset == 0) {
328            mAnchorTimeUs = inHeader->nTimeStamp;
329            mNumSamplesOutput = 0;
330        }
331
332        size_t adtsHeaderSize = 0;
333        if (mIsADTS) {
334            // skip 30 bits, aac_frame_length follows.
335            // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
336
337            const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset;
338
339            CHECK_GE(inHeader->nFilledLen, 7);
340
341            bool protectionAbsent = (adtsHeader[1] & 1);
342
343            unsigned aac_frame_length =
344                ((adtsHeader[3] & 3) << 11)
345                | (adtsHeader[4] << 3)
346                | (adtsHeader[5] >> 5);
347
348            CHECK_GE(inHeader->nFilledLen, aac_frame_length);
349
350            adtsHeaderSize = (protectionAbsent ? 7 : 9);
351
352            inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize;
353            inBufferLength[0] = aac_frame_length - adtsHeaderSize;
354
355            inHeader->nOffset += adtsHeaderSize;
356            inHeader->nFilledLen -= adtsHeaderSize;
357        } else {
358            inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
359            inBufferLength[0] = inHeader->nFilledLen;
360        }
361
362        // Fill and decode
363        INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
364        bytesValid[0] = inBufferLength[0];
365
366        int prevSampleRate = mStreamInfo->sampleRate;
367        int prevNumChannels = mStreamInfo->numChannels;
368
369        AAC_DECODER_ERROR decoderErr = AAC_DEC_NOT_ENOUGH_BITS;
370        while (bytesValid[0] > 0 && decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
371            aacDecoder_Fill(mAACDecoder,
372                            inBuffer,
373                            inBufferLength,
374                            bytesValid);
375
376            decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
377                                                outBuffer,
378                                                outHeader->nAllocLen,
379                                                0 /* flags */);
380
381        }
382
383        /*
384         * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
385         * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
386         * rate system and the sampling rate in the final output is actually
387         * doubled compared with the core AAC decoder sampling rate.
388         *
389         * Explicit signalling is done by explicitly defining SBR audio object
390         * type in the bitstream. Implicit signalling is done by embedding
391         * SBR content in AAC extension payload specific to SBR, and hence
392         * requires an AAC decoder to perform pre-checks on actual audio frames.
393         *
394         * Thus, we could not say for sure whether a stream is
395         * AAC+/eAAC+ until the first data frame is decoded.
396         */
397        if (mInputBufferCount <= 2) {
398            if (mStreamInfo->sampleRate != prevSampleRate ||
399                mStreamInfo->numChannels != prevNumChannels) {
400                ALOGI("Reconfiguring decoder: %d Hz, %d channels",
401                      mStreamInfo->sampleRate,
402                      mStreamInfo->numChannels);
403
404                // We're going to want to revisit this input buffer, but
405                // may have already advanced the offset. Undo that if
406                // necessary.
407                inHeader->nOffset -= adtsHeaderSize;
408                inHeader->nFilledLen += adtsHeaderSize;
409
410                notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
411                mOutputPortSettingsChange = AWAITING_DISABLED;
412                return;
413            }
414        } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
415            ALOGW("Invalid AAC stream");
416            mSignalledError = true;
417            notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
418            return;
419        }
420
421        size_t numOutBytes =
422            mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
423
424        if (decoderErr == AAC_DEC_OK) {
425            UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
426            inHeader->nFilledLen -= inBufferUsedLength;
427            inHeader->nOffset += inBufferUsedLength;
428        } else {
429            ALOGW("AAC decoder returned error %d, substituting silence",
430                  decoderErr);
431
432            memset(outHeader->pBuffer + outHeader->nOffset, 0, numOutBytes);
433
434            // Discard input buffer.
435            inHeader->nFilledLen = 0;
436
437            aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
438
439            // fall through
440        }
441
442        if (decoderErr == AAC_DEC_OK || mNumSamplesOutput > 0) {
443            // We'll only output data if we successfully decoded it or
444            // we've previously decoded valid data, in the latter case
445            // (decode failed) we'll output a silent frame.
446            if (mIsFirst) {
447                mIsFirst = false;
448                // the first decoded frame should be discarded to account for decoder delay
449                numOutBytes = 0;
450            }
451
452            outHeader->nFilledLen = numOutBytes;
453            outHeader->nFlags = 0;
454
455            outHeader->nTimeStamp =
456                mAnchorTimeUs
457                    + (mNumSamplesOutput * 1000000ll) / mStreamInfo->sampleRate;
458
459            mNumSamplesOutput += mStreamInfo->frameSize;
460
461            outInfo->mOwnedByUs = false;
462            outQueue.erase(outQueue.begin());
463            outInfo = NULL;
464            notifyFillBufferDone(outHeader);
465            outHeader = NULL;
466        }
467
468        if (inHeader->nFilledLen == 0) {
469            inInfo->mOwnedByUs = false;
470            inQueue.erase(inQueue.begin());
471            inInfo = NULL;
472            notifyEmptyBufferDone(inHeader);
473            inHeader = NULL;
474        }
475
476        if (decoderErr == AAC_DEC_OK) {
477            ++mInputBufferCount;
478        }
479    }
480}
481
482void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) {
483    if (portIndex == 0) {
484        // Make sure that the next buffer output does not still
485        // depend on fragments from the last one decoded.
486        aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
487        mIsFirst = true;
488    }
489}
490
491void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
492    if (portIndex != 1) {
493        return;
494    }
495
496    switch (mOutputPortSettingsChange) {
497        case NONE:
498            break;
499
500        case AWAITING_DISABLED:
501        {
502            CHECK(!enabled);
503            mOutputPortSettingsChange = AWAITING_ENABLED;
504            break;
505        }
506
507        default:
508        {
509            CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
510            CHECK(enabled);
511            mOutputPortSettingsChange = NONE;
512            break;
513        }
514    }
515}
516
517}  // namespace android
518
519android::SoftOMXComponent *createSoftOMXComponent(
520        const char *name, const OMX_CALLBACKTYPE *callbacks,
521        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
522    return new android::SoftAAC2(name, callbacks, appData, component);
523}
524