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