SoftAAC2.cpp revision cc9833b5db0e96f12daddb90a747fd146627377d
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        // Only send out port settings changed event if both sample rate
297        // and numChannels are valid.
298        if (mStreamInfo->sampleRate && mStreamInfo->numChannels) {
299            maybeConfigureDownmix();
300            ALOGI("Initially configuring decoder: %d Hz, %d channels",
301                mStreamInfo->sampleRate,
302                mStreamInfo->numChannels);
303
304            notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
305            mOutputPortSettingsChange = AWAITING_DISABLED;
306        }
307
308        return;
309    }
310
311    while (!inQueue.empty() && !outQueue.empty()) {
312        BufferInfo *inInfo = *inQueue.begin();
313        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
314
315        BufferInfo *outInfo = *outQueue.begin();
316        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
317
318        if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
319            inQueue.erase(inQueue.begin());
320            inInfo->mOwnedByUs = false;
321            notifyEmptyBufferDone(inHeader);
322
323            // flush out the decoder's delayed data by calling DecodeFrame one more time, with
324            // the AACDEC_FLUSH flag set
325            INT_PCM *outBuffer =
326                    reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
327            AAC_DECODER_ERROR decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
328                                                                  outBuffer,
329                                                                  outHeader->nAllocLen,
330                                                                  AACDEC_FLUSH);
331            if (decoderErr != AAC_DEC_OK) {
332                mSignalledError = true;
333                notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
334                return;
335            }
336
337            outHeader->nFilledLen =
338                    mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
339            outHeader->nFlags = OMX_BUFFERFLAG_EOS;
340
341            outQueue.erase(outQueue.begin());
342            outInfo->mOwnedByUs = false;
343            notifyFillBufferDone(outHeader);
344            return;
345        }
346
347        if (inHeader->nOffset == 0) {
348            mAnchorTimeUs = inHeader->nTimeStamp;
349            mNumSamplesOutput = 0;
350        }
351
352        size_t adtsHeaderSize = 0;
353        if (mIsADTS) {
354            // skip 30 bits, aac_frame_length follows.
355            // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
356
357            const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset;
358
359            bool signalError = false;
360            if (inHeader->nFilledLen < 7) {
361                ALOGE("Audio data too short to contain even the ADTS header. "
362                      "Got %ld bytes.", inHeader->nFilledLen);
363                hexdump(adtsHeader, inHeader->nFilledLen);
364                signalError = true;
365            } else {
366                bool protectionAbsent = (adtsHeader[1] & 1);
367
368                unsigned aac_frame_length =
369                    ((adtsHeader[3] & 3) << 11)
370                    | (adtsHeader[4] << 3)
371                    | (adtsHeader[5] >> 5);
372
373                if (inHeader->nFilledLen < aac_frame_length) {
374                    ALOGE("Not enough audio data for the complete frame. "
375                          "Got %ld bytes, frame size according to the ADTS "
376                          "header is %u bytes.",
377                          inHeader->nFilledLen, aac_frame_length);
378                    hexdump(adtsHeader, inHeader->nFilledLen);
379                    signalError = true;
380                } else {
381                    adtsHeaderSize = (protectionAbsent ? 7 : 9);
382
383                    inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize;
384                    inBufferLength[0] = aac_frame_length - adtsHeaderSize;
385
386                    inHeader->nOffset += adtsHeaderSize;
387                    inHeader->nFilledLen -= adtsHeaderSize;
388                }
389            }
390
391            if (signalError) {
392                mSignalledError = true;
393
394                notify(OMX_EventError,
395                       OMX_ErrorStreamCorrupt,
396                       ERROR_MALFORMED,
397                       NULL);
398
399                return;
400            }
401        } else {
402            inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
403            inBufferLength[0] = inHeader->nFilledLen;
404        }
405
406        // Fill and decode
407        INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
408        bytesValid[0] = inBufferLength[0];
409
410        int prevSampleRate = mStreamInfo->sampleRate;
411        int prevNumChannels = mStreamInfo->numChannels;
412
413        AAC_DECODER_ERROR decoderErr = AAC_DEC_NOT_ENOUGH_BITS;
414        while (bytesValid[0] > 0 && decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
415            aacDecoder_Fill(mAACDecoder,
416                            inBuffer,
417                            inBufferLength,
418                            bytesValid);
419
420            decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
421                                                outBuffer,
422                                                outHeader->nAllocLen,
423                                                0 /* flags */);
424
425            if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
426                ALOGW("Not enough bits, bytesValid %d", bytesValid[0]);
427            }
428        }
429
430        /*
431         * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
432         * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
433         * rate system and the sampling rate in the final output is actually
434         * doubled compared with the core AAC decoder sampling rate.
435         *
436         * Explicit signalling is done by explicitly defining SBR audio object
437         * type in the bitstream. Implicit signalling is done by embedding
438         * SBR content in AAC extension payload specific to SBR, and hence
439         * requires an AAC decoder to perform pre-checks on actual audio frames.
440         *
441         * Thus, we could not say for sure whether a stream is
442         * AAC+/eAAC+ until the first data frame is decoded.
443         */
444        if (mInputBufferCount <= 2) {
445            if (mStreamInfo->sampleRate != prevSampleRate ||
446                mStreamInfo->numChannels != prevNumChannels) {
447                maybeConfigureDownmix();
448                ALOGI("Reconfiguring decoder: %d Hz, %d channels",
449                      mStreamInfo->sampleRate,
450                      mStreamInfo->numChannels);
451
452                // We're going to want to revisit this input buffer, but
453                // may have already advanced the offset. Undo that if
454                // necessary.
455                inHeader->nOffset -= adtsHeaderSize;
456                inHeader->nFilledLen += adtsHeaderSize;
457
458                notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
459                mOutputPortSettingsChange = AWAITING_DISABLED;
460                return;
461            }
462        } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
463            ALOGW("Invalid AAC stream");
464            mSignalledError = true;
465            notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
466            return;
467        }
468
469        size_t numOutBytes =
470            mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
471
472        if (decoderErr == AAC_DEC_OK) {
473            UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
474            inHeader->nFilledLen -= inBufferUsedLength;
475            inHeader->nOffset += inBufferUsedLength;
476        } else {
477            ALOGW("AAC decoder returned error %d, substituting silence",
478                  decoderErr);
479
480            memset(outHeader->pBuffer + outHeader->nOffset, 0, numOutBytes);
481
482            // Discard input buffer.
483            inHeader->nFilledLen = 0;
484
485            aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
486
487            // fall through
488        }
489
490        if (decoderErr == AAC_DEC_OK || mNumSamplesOutput > 0) {
491            // We'll only output data if we successfully decoded it or
492            // we've previously decoded valid data, in the latter case
493            // (decode failed) we'll output a silent frame.
494            if (mIsFirst) {
495                mIsFirst = false;
496                // the first decoded frame should be discarded to account for decoder delay
497                numOutBytes = 0;
498            }
499
500            outHeader->nFilledLen = numOutBytes;
501            outHeader->nFlags = 0;
502
503            outHeader->nTimeStamp =
504                mAnchorTimeUs
505                    + (mNumSamplesOutput * 1000000ll) / mStreamInfo->sampleRate;
506
507            mNumSamplesOutput += mStreamInfo->frameSize;
508
509            outInfo->mOwnedByUs = false;
510            outQueue.erase(outQueue.begin());
511            outInfo = NULL;
512            notifyFillBufferDone(outHeader);
513            outHeader = NULL;
514        }
515
516        if (inHeader->nFilledLen == 0) {
517            inInfo->mOwnedByUs = false;
518            inQueue.erase(inQueue.begin());
519            inInfo = NULL;
520            notifyEmptyBufferDone(inHeader);
521            inHeader = NULL;
522        }
523
524        if (decoderErr == AAC_DEC_OK) {
525            ++mInputBufferCount;
526        }
527    }
528}
529
530void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) {
531    if (portIndex == 0) {
532        // Make sure that the next buffer output does not still
533        // depend on fragments from the last one decoded.
534        aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
535        mIsFirst = true;
536    }
537}
538
539void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
540    if (portIndex != 1) {
541        return;
542    }
543
544    switch (mOutputPortSettingsChange) {
545        case NONE:
546            break;
547
548        case AWAITING_DISABLED:
549        {
550            CHECK(!enabled);
551            mOutputPortSettingsChange = AWAITING_ENABLED;
552            break;
553        }
554
555        default:
556        {
557            CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
558            CHECK(enabled);
559            mOutputPortSettingsChange = NONE;
560            break;
561        }
562    }
563}
564
565}  // namespace android
566
567android::SoftOMXComponent *createSoftOMXComponent(
568        const char *name, const OMX_CALLBACKTYPE *callbacks,
569        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
570    return new android::SoftAAC2(name, callbacks, appData, component);
571}
572