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