SoftAAC2.cpp revision 2965f4eb7dceaf1173f0e2d93c11c28293aeead7
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_NDEBUG 0
18#define LOG_TAG "SoftAAC2"
19#include <utils/Log.h>
20
21#include "SoftAAC2.h"
22#include <OMX_AudioExt.h>
23#include <OMX_IndexExt.h>
24
25#include <cutils/properties.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/hexdump.h>
28#include <media/stagefright/MediaErrors.h>
29
30#include <math.h>
31
32#define FILEREAD_MAX_LAYERS 2
33
34#define DRC_DEFAULT_MOBILE_REF_LEVEL 64  /* 64*-0.25dB = -16 dB below full scale for mobile conf */
35#define DRC_DEFAULT_MOBILE_DRC_CUT   127 /* maximum compression of dynamic range for mobile conf */
36#define DRC_DEFAULT_MOBILE_DRC_BOOST 127 /* maximum compression of dynamic range for mobile conf */
37#define DRC_DEFAULT_MOBILE_DRC_HEAVY 1   /* switch for heavy compression for mobile conf */
38#define DRC_DEFAULT_MOBILE_ENC_LEVEL -1 /* encoder target level; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */
39#define MAX_CHANNEL_COUNT            8  /* maximum number of audio channels that can be decoded */
40// names of properties that can be used to override the default DRC settings
41#define PROP_DRC_OVERRIDE_REF_LEVEL  "aac_drc_reference_level"
42#define PROP_DRC_OVERRIDE_CUT        "aac_drc_cut"
43#define PROP_DRC_OVERRIDE_BOOST      "aac_drc_boost"
44#define PROP_DRC_OVERRIDE_HEAVY      "aac_drc_heavy"
45#define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level"
46
47namespace android {
48
49template<class T>
50static void InitOMXParams(T *params) {
51    params->nSize = sizeof(T);
52    params->nVersion.s.nVersionMajor = 1;
53    params->nVersion.s.nVersionMinor = 0;
54    params->nVersion.s.nRevision = 0;
55    params->nVersion.s.nStep = 0;
56}
57
58SoftAAC2::SoftAAC2(
59        const char *name,
60        const OMX_CALLBACKTYPE *callbacks,
61        OMX_PTR appData,
62        OMX_COMPONENTTYPE **component)
63    : SimpleSoftOMXComponent(name, callbacks, appData, component),
64      mAACDecoder(NULL),
65      mStreamInfo(NULL),
66      mIsADTS(false),
67      mInputBufferCount(0),
68      mOutputBufferCount(0),
69      mSignalledError(false),
70      mLastInHeader(NULL),
71      mOutputPortSettingsChange(NONE) {
72    initPorts();
73    CHECK_EQ(initDecoder(), (status_t)OK);
74}
75
76SoftAAC2::~SoftAAC2() {
77    aacDecoder_Close(mAACDecoder);
78    delete mOutputDelayRingBuffer;
79}
80
81void SoftAAC2::initPorts() {
82    OMX_PARAM_PORTDEFINITIONTYPE def;
83    InitOMXParams(&def);
84
85    def.nPortIndex = 0;
86    def.eDir = OMX_DirInput;
87    def.nBufferCountMin = kNumInputBuffers;
88    def.nBufferCountActual = def.nBufferCountMin;
89    def.nBufferSize = 8192;
90    def.bEnabled = OMX_TRUE;
91    def.bPopulated = OMX_FALSE;
92    def.eDomain = OMX_PortDomainAudio;
93    def.bBuffersContiguous = OMX_FALSE;
94    def.nBufferAlignment = 1;
95
96    def.format.audio.cMIMEType = const_cast<char *>("audio/aac");
97    def.format.audio.pNativeRender = NULL;
98    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
99    def.format.audio.eEncoding = OMX_AUDIO_CodingAAC;
100
101    addPort(def);
102
103    def.nPortIndex = 1;
104    def.eDir = OMX_DirOutput;
105    def.nBufferCountMin = kNumOutputBuffers;
106    def.nBufferCountActual = def.nBufferCountMin;
107    def.nBufferSize = 4096 * MAX_CHANNEL_COUNT;
108    def.bEnabled = OMX_TRUE;
109    def.bPopulated = OMX_FALSE;
110    def.eDomain = OMX_PortDomainAudio;
111    def.bBuffersContiguous = OMX_FALSE;
112    def.nBufferAlignment = 2;
113
114    def.format.audio.cMIMEType = const_cast<char *>("audio/raw");
115    def.format.audio.pNativeRender = NULL;
116    def.format.audio.bFlagErrorConcealment = OMX_FALSE;
117    def.format.audio.eEncoding = OMX_AUDIO_CodingPCM;
118
119    addPort(def);
120}
121
122status_t SoftAAC2::initDecoder() {
123    ALOGV("initDecoder()");
124    status_t status = UNKNOWN_ERROR;
125    mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1);
126    if (mAACDecoder != NULL) {
127        mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder);
128        if (mStreamInfo != NULL) {
129            status = OK;
130        }
131    }
132
133    mEndOfInput = false;
134    mEndOfOutput = false;
135    mOutputDelayCompensated = 0;
136    mOutputDelayRingBufferSize = 2048 * MAX_CHANNEL_COUNT * kNumDelayBlocksMax;
137    mOutputDelayRingBuffer = new short[mOutputDelayRingBufferSize];
138    mOutputDelayRingBufferWritePos = 0;
139    mOutputDelayRingBufferReadPos = 0;
140    mOutputDelayRingBufferFilled = 0;
141
142    if (mAACDecoder == NULL) {
143        ALOGE("AAC decoder is null. TODO: Can not call aacDecoder_SetParam in the following code");
144    }
145
146    //aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE, 0);
147
148    //init DRC wrapper
149    mDrcWrap.setDecoderHandle(mAACDecoder);
150    mDrcWrap.submitStreamData(mStreamInfo);
151
152    // for streams that contain metadata, use the mobile profile DRC settings unless overridden by platform properties
153    // TODO: change the DRC settings depending on audio output device type (HDMI, loadspeaker, headphone)
154    char value[PROPERTY_VALUE_MAX];
155    //  DRC_PRES_MODE_WRAP_DESIRED_TARGET
156    if (property_get(PROP_DRC_OVERRIDE_REF_LEVEL, value, NULL)) {
157        unsigned refLevel = atoi(value);
158        ALOGV("AAC decoder using desired DRC target reference level of %d instead of %d", refLevel,
159                DRC_DEFAULT_MOBILE_REF_LEVEL);
160        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, refLevel);
161    } else {
162        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, DRC_DEFAULT_MOBILE_REF_LEVEL);
163    }
164    //  DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
165    if (property_get(PROP_DRC_OVERRIDE_CUT, value, NULL)) {
166        unsigned cut = atoi(value);
167        ALOGV("AAC decoder using desired DRC attenuation factor of %d instead of %d", cut,
168                DRC_DEFAULT_MOBILE_DRC_CUT);
169        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, cut);
170    } else {
171        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, DRC_DEFAULT_MOBILE_DRC_CUT);
172    }
173    //  DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
174    if (property_get(PROP_DRC_OVERRIDE_BOOST, value, NULL)) {
175        unsigned boost = atoi(value);
176        ALOGV("AAC decoder using desired DRC boost factor of %d instead of %d", boost,
177                DRC_DEFAULT_MOBILE_DRC_BOOST);
178        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, boost);
179    } else {
180        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, DRC_DEFAULT_MOBILE_DRC_BOOST);
181    }
182    //  DRC_PRES_MODE_WRAP_DESIRED_HEAVY
183    if (property_get(PROP_DRC_OVERRIDE_HEAVY, value, NULL)) {
184        unsigned heavy = atoi(value);
185        ALOGV("AAC decoder using desried DRC heavy compression switch of %d instead of %d", heavy,
186                DRC_DEFAULT_MOBILE_DRC_HEAVY);
187        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, heavy);
188    } else {
189        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, DRC_DEFAULT_MOBILE_DRC_HEAVY);
190    }
191    // DRC_PRES_MODE_WRAP_ENCODER_TARGET
192    if (property_get(PROP_DRC_OVERRIDE_ENC_LEVEL, value, NULL)) {
193        unsigned encoderRefLevel = atoi(value);
194        ALOGV("AAC decoder using encoder-side DRC reference level of %d instead of %d",
195                encoderRefLevel, DRC_DEFAULT_MOBILE_ENC_LEVEL);
196        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, encoderRefLevel);
197    } else {
198        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, DRC_DEFAULT_MOBILE_ENC_LEVEL);
199    }
200
201    return status;
202}
203
204OMX_ERRORTYPE SoftAAC2::internalGetParameter(
205        OMX_INDEXTYPE index, OMX_PTR params) {
206    switch (index) {
207        case OMX_IndexParamAudioAac:
208        {
209            OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
210                (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
211
212            if (aacParams->nPortIndex != 0) {
213                return OMX_ErrorUndefined;
214            }
215
216            aacParams->nBitRate = 0;
217            aacParams->nAudioBandWidth = 0;
218            aacParams->nAACtools = 0;
219            aacParams->nAACERtools = 0;
220            aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
221
222            aacParams->eAACStreamFormat =
223                mIsADTS
224                    ? OMX_AUDIO_AACStreamFormatMP4ADTS
225                    : OMX_AUDIO_AACStreamFormatMP4FF;
226
227            aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
228
229            if (!isConfigured()) {
230                aacParams->nChannels = 1;
231                aacParams->nSampleRate = 44100;
232                aacParams->nFrameLength = 0;
233            } else {
234                aacParams->nChannels = mStreamInfo->numChannels;
235                aacParams->nSampleRate = mStreamInfo->sampleRate;
236                aacParams->nFrameLength = mStreamInfo->frameSize;
237            }
238
239            return OMX_ErrorNone;
240        }
241
242        case OMX_IndexParamAudioPcm:
243        {
244            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
245                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
246
247            if (pcmParams->nPortIndex != 1) {
248                return OMX_ErrorUndefined;
249            }
250
251            pcmParams->eNumData = OMX_NumericalDataSigned;
252            pcmParams->eEndian = OMX_EndianBig;
253            pcmParams->bInterleaved = OMX_TRUE;
254            pcmParams->nBitPerSample = 16;
255            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
256            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
257            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
258            pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF;
259            pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE;
260            pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS;
261            pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS;
262
263            if (!isConfigured()) {
264                pcmParams->nChannels = 1;
265                pcmParams->nSamplingRate = 44100;
266            } else {
267                pcmParams->nChannels = mStreamInfo->numChannels;
268                pcmParams->nSamplingRate = mStreamInfo->sampleRate;
269            }
270
271            return OMX_ErrorNone;
272        }
273
274        default:
275            return SimpleSoftOMXComponent::internalGetParameter(index, params);
276    }
277}
278
279OMX_ERRORTYPE SoftAAC2::internalSetParameter(
280        OMX_INDEXTYPE index, const OMX_PTR params) {
281    switch ((int)index) {
282        case OMX_IndexParamStandardComponentRole:
283        {
284            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
285                (const OMX_PARAM_COMPONENTROLETYPE *)params;
286
287            if (strncmp((const char *)roleParams->cRole,
288                        "audio_decoder.aac",
289                        OMX_MAX_STRINGNAME_SIZE - 1)) {
290                return OMX_ErrorUndefined;
291            }
292
293            return OMX_ErrorNone;
294        }
295
296        case OMX_IndexParamAudioAac:
297        {
298            const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
299                (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
300
301            if (aacParams->nPortIndex != 0) {
302                return OMX_ErrorUndefined;
303            }
304
305            if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) {
306                mIsADTS = false;
307            } else if (aacParams->eAACStreamFormat
308                        == OMX_AUDIO_AACStreamFormatMP4ADTS) {
309                mIsADTS = true;
310            } else {
311                return OMX_ErrorUndefined;
312            }
313
314            return OMX_ErrorNone;
315        }
316
317        case OMX_IndexParamAudioAndroidAacPresentation:
318        {
319            const OMX_AUDIO_PARAM_ANDROID_AACPRESENTATIONTYPE *aacPresParams =
320                    (const OMX_AUDIO_PARAM_ANDROID_AACPRESENTATIONTYPE *)params;
321            // for the following parameters of the OMX_AUDIO_PARAM_AACPROFILETYPE structure,
322            // a value of -1 implies the parameter is not set by the application:
323            //   nMaxOutputChannels     uses default platform properties, see configureDownmix()
324            //   nDrcCut                uses default platform properties, see initDecoder()
325            //   nDrcBoost                idem
326            //   nHeavyCompression        idem
327            //   nTargetReferenceLevel    idem
328            //   nEncodedTargetLevel      idem
329            if (aacPresParams->nMaxOutputChannels >= 0) {
330                int max;
331                if (aacPresParams->nMaxOutputChannels >= 8) { max = 8; }
332                else if (aacPresParams->nMaxOutputChannels >= 6) { max = 6; }
333                else if (aacPresParams->nMaxOutputChannels >= 2) { max = 2; }
334                else {
335                    // -1 or 0: disable downmix,  1: mono
336                    max = aacPresParams->nMaxOutputChannels;
337                }
338                ALOGV("set nMaxOutputChannels=%d", max);
339                aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, max);
340            }
341            bool updateDrcWrapper = false;
342            if (aacPresParams->nDrcBoost >= 0) {
343                ALOGV("set nDrcBoost=%d", aacPresParams->nDrcBoost);
344                mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR,
345                        aacPresParams->nDrcBoost);
346                updateDrcWrapper = true;
347            }
348            if (aacPresParams->nDrcCut >= 0) {
349                ALOGV("set nDrcCut=%d", aacPresParams->nDrcCut);
350                mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, aacPresParams->nDrcCut);
351                updateDrcWrapper = true;
352            }
353            if (aacPresParams->nHeavyCompression >= 0) {
354                ALOGV("set nHeavyCompression=%d", aacPresParams->nHeavyCompression);
355                mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY,
356                        aacPresParams->nHeavyCompression);
357                updateDrcWrapper = true;
358            }
359            if (aacPresParams->nTargetReferenceLevel >= 0) {
360                ALOGV("set nTargetReferenceLevel=%d", aacPresParams->nTargetReferenceLevel);
361                mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET,
362                        aacPresParams->nTargetReferenceLevel);
363                updateDrcWrapper = true;
364            }
365            if (aacPresParams->nEncodedTargetLevel >= 0) {
366                ALOGV("set nEncodedTargetLevel=%d", aacPresParams->nEncodedTargetLevel);
367                mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET,
368                        aacPresParams->nEncodedTargetLevel);
369                updateDrcWrapper = true;
370            }
371            if (aacPresParams->nPCMLimiterEnable >= 0) {
372                aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE,
373                        (aacPresParams->nPCMLimiterEnable != 0));
374            }
375            if (updateDrcWrapper) {
376                mDrcWrap.update();
377            }
378
379            return OMX_ErrorNone;
380        }
381
382        case OMX_IndexParamAudioPcm:
383        {
384            const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
385                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
386
387            if (pcmParams->nPortIndex != 1) {
388                return OMX_ErrorUndefined;
389            }
390
391            return OMX_ErrorNone;
392        }
393
394        default:
395            return SimpleSoftOMXComponent::internalSetParameter(index, params);
396    }
397}
398
399bool SoftAAC2::isConfigured() const {
400    return mInputBufferCount > 0;
401}
402
403void SoftAAC2::configureDownmix() const {
404    char value[PROPERTY_VALUE_MAX];
405    if (!(property_get("media.aac_51_output_enabled", value, NULL)
406            && (!strcmp(value, "1") || !strcasecmp(value, "true")))) {
407        ALOGI("limiting to stereo output");
408        aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, 2);
409        // By default, the decoder creates a 5.1 channel downmix signal
410        // for seven and eight channel input streams. To enable 6.1 and 7.1 channel output
411        // use aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, -1)
412    }
413}
414
415bool SoftAAC2::outputDelayRingBufferPutSamples(INT_PCM *samples, int32_t numSamples) {
416    if (numSamples == 0) {
417        return true;
418    }
419    if (outputDelayRingBufferSpaceLeft() < numSamples) {
420        ALOGE("RING BUFFER WOULD OVERFLOW");
421        return false;
422    }
423    if (mOutputDelayRingBufferWritePos + numSamples <= mOutputDelayRingBufferSize
424            && (mOutputDelayRingBufferReadPos <= mOutputDelayRingBufferWritePos
425                    || mOutputDelayRingBufferReadPos > mOutputDelayRingBufferWritePos + numSamples)) {
426        // faster memcopy loop without checks, if the preconditions allow this
427        for (int32_t i = 0; i < numSamples; i++) {
428            mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos++] = samples[i];
429        }
430
431        if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
432            mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
433        }
434    } else {
435        ALOGV("slow SoftAAC2::outputDelayRingBufferPutSamples()");
436
437        for (int32_t i = 0; i < numSamples; i++) {
438            mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos] = samples[i];
439            mOutputDelayRingBufferWritePos++;
440            if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
441                mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
442            }
443        }
444    }
445    mOutputDelayRingBufferFilled += numSamples;
446    return true;
447}
448
449int32_t SoftAAC2::outputDelayRingBufferGetSamples(INT_PCM *samples, int32_t numSamples) {
450
451    if (numSamples > mOutputDelayRingBufferFilled) {
452        ALOGE("RING BUFFER WOULD UNDERRUN");
453        return -1;
454    }
455
456    if (mOutputDelayRingBufferReadPos + numSamples <= mOutputDelayRingBufferSize
457            && (mOutputDelayRingBufferWritePos < mOutputDelayRingBufferReadPos
458                    || mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferReadPos + numSamples)) {
459        // faster memcopy loop without checks, if the preconditions allow this
460        if (samples != 0) {
461            for (int32_t i = 0; i < numSamples; i++) {
462                samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos++];
463            }
464        } else {
465            mOutputDelayRingBufferReadPos += numSamples;
466        }
467        if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
468            mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
469        }
470    } else {
471        ALOGV("slow SoftAAC2::outputDelayRingBufferGetSamples()");
472
473        for (int32_t i = 0; i < numSamples; i++) {
474            if (samples != 0) {
475                samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos];
476            }
477            mOutputDelayRingBufferReadPos++;
478            if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
479                mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
480            }
481        }
482    }
483    mOutputDelayRingBufferFilled -= numSamples;
484    return numSamples;
485}
486
487int32_t SoftAAC2::outputDelayRingBufferSamplesAvailable() {
488    return mOutputDelayRingBufferFilled;
489}
490
491int32_t SoftAAC2::outputDelayRingBufferSpaceLeft() {
492    return mOutputDelayRingBufferSize - outputDelayRingBufferSamplesAvailable();
493}
494
495
496void SoftAAC2::onQueueFilled(OMX_U32 /* portIndex */) {
497    if (mSignalledError || mOutputPortSettingsChange != NONE) {
498        return;
499    }
500
501    UCHAR* inBuffer[FILEREAD_MAX_LAYERS];
502    UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0};
503    UINT bytesValid[FILEREAD_MAX_LAYERS] = {0};
504
505    List<BufferInfo *> &inQueue = getPortQueue(0);
506    List<BufferInfo *> &outQueue = getPortQueue(1);
507
508    while ((!inQueue.empty() || mEndOfInput) && !outQueue.empty()) {
509        if (!inQueue.empty()) {
510            INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
511            BufferInfo *inInfo = *inQueue.begin();
512            OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
513
514            mEndOfInput = (inHeader->nFlags & OMX_BUFFERFLAG_EOS) != 0;
515
516            if (mInputBufferCount == 0 && !(inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG)) {
517                ALOGE("first buffer should have OMX_BUFFERFLAG_CODECCONFIG set");
518                inHeader->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
519            }
520            if ((inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) != 0) {
521                BufferInfo *inInfo = *inQueue.begin();
522                OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
523
524                inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
525                inBufferLength[0] = inHeader->nFilledLen;
526
527                AAC_DECODER_ERROR decoderErr =
528                    aacDecoder_ConfigRaw(mAACDecoder,
529                                         inBuffer,
530                                         inBufferLength);
531
532                if (decoderErr != AAC_DEC_OK) {
533                    ALOGW("aacDecoder_ConfigRaw decoderErr = 0x%4.4x", decoderErr);
534                    mSignalledError = true;
535                    notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
536                    return;
537                }
538
539                mInputBufferCount++;
540                mOutputBufferCount++; // fake increase of outputBufferCount to keep the counters aligned
541
542                inInfo->mOwnedByUs = false;
543                inQueue.erase(inQueue.begin());
544                mLastInHeader = NULL;
545                inInfo = NULL;
546                notifyEmptyBufferDone(inHeader);
547                inHeader = NULL;
548
549                configureDownmix();
550                // Only send out port settings changed event if both sample rate
551                // and numChannels are valid.
552                if (mStreamInfo->sampleRate && mStreamInfo->numChannels) {
553                    ALOGI("Initially configuring decoder: %d Hz, %d channels",
554                        mStreamInfo->sampleRate,
555                        mStreamInfo->numChannels);
556
557                    notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
558                    mOutputPortSettingsChange = AWAITING_DISABLED;
559                }
560                return;
561            }
562
563            if (inHeader->nFilledLen == 0) {
564                inInfo->mOwnedByUs = false;
565                inQueue.erase(inQueue.begin());
566                mLastInHeader = NULL;
567                inInfo = NULL;
568                notifyEmptyBufferDone(inHeader);
569                inHeader = NULL;
570                continue;
571            }
572
573            if (mIsADTS) {
574                size_t adtsHeaderSize = 0;
575                // skip 30 bits, aac_frame_length follows.
576                // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
577
578                const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset;
579
580                bool signalError = false;
581                if (inHeader->nFilledLen < 7) {
582                    ALOGE("Audio data too short to contain even the ADTS header. "
583                            "Got %d bytes.", inHeader->nFilledLen);
584                    hexdump(adtsHeader, inHeader->nFilledLen);
585                    signalError = true;
586                } else {
587                    bool protectionAbsent = (adtsHeader[1] & 1);
588
589                    unsigned aac_frame_length =
590                        ((adtsHeader[3] & 3) << 11)
591                        | (adtsHeader[4] << 3)
592                        | (adtsHeader[5] >> 5);
593
594                    if (inHeader->nFilledLen < aac_frame_length) {
595                        ALOGE("Not enough audio data for the complete frame. "
596                                "Got %d bytes, frame size according to the ADTS "
597                                "header is %u bytes.",
598                                inHeader->nFilledLen, aac_frame_length);
599                        hexdump(adtsHeader, inHeader->nFilledLen);
600                        signalError = true;
601                    } else {
602                        adtsHeaderSize = (protectionAbsent ? 7 : 9);
603
604                        inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize;
605                        inBufferLength[0] = aac_frame_length - adtsHeaderSize;
606
607                        inHeader->nOffset += adtsHeaderSize;
608                        inHeader->nFilledLen -= adtsHeaderSize;
609                    }
610                }
611
612                if (signalError) {
613                    mSignalledError = true;
614                    notify(OMX_EventError, OMX_ErrorStreamCorrupt, ERROR_MALFORMED, NULL);
615                    return;
616                }
617
618                // insert buffer size and time stamp
619                mBufferSizes.add(inBufferLength[0]);
620                if (mLastInHeader != inHeader) {
621                    mBufferTimestamps.add(inHeader->nTimeStamp);
622                    mLastInHeader = inHeader;
623                } else {
624                    int64_t currentTime = mBufferTimestamps.top();
625                    currentTime += mStreamInfo->aacSamplesPerFrame *
626                            1000000ll / mStreamInfo->sampleRate;
627                    mBufferTimestamps.add(currentTime);
628                }
629            } else {
630                inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
631                inBufferLength[0] = inHeader->nFilledLen;
632                mLastInHeader = inHeader;
633                mBufferTimestamps.add(inHeader->nTimeStamp);
634                mBufferSizes.add(inHeader->nFilledLen);
635            }
636
637            // Fill and decode
638            bytesValid[0] = inBufferLength[0];
639
640            INT prevSampleRate = mStreamInfo->sampleRate;
641            INT prevNumChannels = mStreamInfo->numChannels;
642
643            aacDecoder_Fill(mAACDecoder,
644                            inBuffer,
645                            inBufferLength,
646                            bytesValid);
647
648            // run DRC check
649            mDrcWrap.submitStreamData(mStreamInfo);
650            mDrcWrap.update();
651
652            UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
653            inHeader->nFilledLen -= inBufferUsedLength;
654            inHeader->nOffset += inBufferUsedLength;
655
656            AAC_DECODER_ERROR decoderErr;
657            do {
658                if (outputDelayRingBufferSpaceLeft() <
659                        (mStreamInfo->frameSize * mStreamInfo->numChannels)) {
660                    ALOGV("skipping decode: not enough space left in ringbuffer");
661                    break;
662                }
663
664                int numconsumed = mStreamInfo->numTotalBytes + mStreamInfo->numBadBytes;
665                decoderErr = aacDecoder_DecodeFrame(mAACDecoder,
666                                           tmpOutBuffer,
667                                           2048 * MAX_CHANNEL_COUNT,
668                                           0 /* flags */);
669
670                numconsumed = (mStreamInfo->numTotalBytes + mStreamInfo->numBadBytes) - numconsumed;
671                if (numconsumed != 0) {
672                    mDecodedSizes.add(numconsumed);
673                }
674
675                if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
676                    break;
677                }
678
679                if (decoderErr != AAC_DEC_OK) {
680                    ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
681                }
682
683                if (bytesValid[0] != 0) {
684                    ALOGE("bytesValid[0] != 0 should never happen");
685                    mSignalledError = true;
686                    notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
687                    return;
688                }
689
690                size_t numOutBytes =
691                    mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
692
693                if (decoderErr == AAC_DEC_OK) {
694                    if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
695                            mStreamInfo->frameSize * mStreamInfo->numChannels)) {
696                        mSignalledError = true;
697                        notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
698                        return;
699                    }
700                } else {
701                    ALOGW("AAC decoder returned error 0x%4.4x, substituting silence", decoderErr);
702
703                    memset(tmpOutBuffer, 0, numOutBytes); // TODO: check for overflow
704
705                    if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
706                            mStreamInfo->frameSize * mStreamInfo->numChannels)) {
707                        mSignalledError = true;
708                        notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
709                        return;
710                    }
711
712                    // Discard input buffer.
713                    if (inHeader) {
714                        inHeader->nFilledLen = 0;
715                    }
716
717                    aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
718
719                    // fall through
720                }
721
722                /*
723                 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
724                 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
725                 * rate system and the sampling rate in the final output is actually
726                 * doubled compared with the core AAC decoder sampling rate.
727                 *
728                 * Explicit signalling is done by explicitly defining SBR audio object
729                 * type in the bitstream. Implicit signalling is done by embedding
730                 * SBR content in AAC extension payload specific to SBR, and hence
731                 * requires an AAC decoder to perform pre-checks on actual audio frames.
732                 *
733                 * Thus, we could not say for sure whether a stream is
734                 * AAC+/eAAC+ until the first data frame is decoded.
735                 */
736                if (mInputBufferCount <= 2 || mOutputBufferCount > 1) { // TODO: <= 1
737                    if (mStreamInfo->sampleRate != prevSampleRate ||
738                        mStreamInfo->numChannels != prevNumChannels) {
739                        ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
740                              prevSampleRate, mStreamInfo->sampleRate,
741                              prevNumChannels, mStreamInfo->numChannels);
742
743                        notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
744                        mOutputPortSettingsChange = AWAITING_DISABLED;
745
746                        if (inHeader && inHeader->nFilledLen == 0) {
747                            inInfo->mOwnedByUs = false;
748                            mInputBufferCount++;
749                            inQueue.erase(inQueue.begin());
750                            mLastInHeader = NULL;
751                            inInfo = NULL;
752                            notifyEmptyBufferDone(inHeader);
753                            inHeader = NULL;
754                        }
755                        return;
756                    }
757                } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
758                    ALOGW("Invalid AAC stream");
759                    mSignalledError = true;
760                    notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
761                    return;
762                }
763                if (inHeader && inHeader->nFilledLen == 0) {
764                    inInfo->mOwnedByUs = false;
765                    mInputBufferCount++;
766                    inQueue.erase(inQueue.begin());
767                    mLastInHeader = NULL;
768                    inInfo = NULL;
769                    notifyEmptyBufferDone(inHeader);
770                    inHeader = NULL;
771                } else {
772                    ALOGV("inHeader->nFilledLen = %d", inHeader ? inHeader->nFilledLen : 0);
773                }
774            } while (decoderErr == AAC_DEC_OK);
775        }
776
777        int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
778
779        if (!mEndOfInput && mOutputDelayCompensated < outputDelay) {
780            // discard outputDelay at the beginning
781            int32_t toCompensate = outputDelay - mOutputDelayCompensated;
782            int32_t discard = outputDelayRingBufferSamplesAvailable();
783            if (discard > toCompensate) {
784                discard = toCompensate;
785            }
786            int32_t discarded = outputDelayRingBufferGetSamples(0, discard);
787            mOutputDelayCompensated += discarded;
788            continue;
789        }
790
791        if (mEndOfInput) {
792            while (mOutputDelayCompensated > 0) {
793                // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
794                INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
795
796                 // run DRC check
797                 mDrcWrap.submitStreamData(mStreamInfo);
798                 mDrcWrap.update();
799
800                AAC_DECODER_ERROR decoderErr =
801                    aacDecoder_DecodeFrame(mAACDecoder,
802                                           tmpOutBuffer,
803                                           2048 * MAX_CHANNEL_COUNT,
804                                           AACDEC_FLUSH);
805                if (decoderErr != AAC_DEC_OK) {
806                    ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
807                }
808
809                int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
810                if (tmpOutBufferSamples > mOutputDelayCompensated) {
811                    tmpOutBufferSamples = mOutputDelayCompensated;
812                }
813                outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
814                mOutputDelayCompensated -= tmpOutBufferSamples;
815            }
816        }
817
818        while (!outQueue.empty()
819                && outputDelayRingBufferSamplesAvailable()
820                        >= mStreamInfo->frameSize * mStreamInfo->numChannels) {
821            BufferInfo *outInfo = *outQueue.begin();
822            OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
823
824            if (outHeader->nOffset != 0) {
825                ALOGE("outHeader->nOffset != 0 is not handled");
826                mSignalledError = true;
827                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
828                return;
829            }
830
831            INT_PCM *outBuffer =
832                    reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
833            int samplesize = mStreamInfo->numChannels * sizeof(int16_t);
834            if (outHeader->nOffset
835                    + mStreamInfo->frameSize * samplesize
836                    > outHeader->nAllocLen) {
837                ALOGE("buffer overflow");
838                mSignalledError = true;
839                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
840                return;
841
842            }
843
844            int available = outputDelayRingBufferSamplesAvailable();
845            int numSamples = outHeader->nAllocLen / sizeof(int16_t);
846            if (numSamples > available) {
847                numSamples = available;
848            }
849            int64_t currentTime = 0;
850            if (available) {
851
852                int numFrames = numSamples / (mStreamInfo->frameSize * mStreamInfo->numChannels);
853                numSamples = numFrames * (mStreamInfo->frameSize * mStreamInfo->numChannels);
854
855                ALOGV("%d samples available (%d), or %d frames",
856                        numSamples, available, numFrames);
857                int64_t *nextTimeStamp = &mBufferTimestamps.editItemAt(0);
858                currentTime = *nextTimeStamp;
859                int32_t *currentBufLeft = &mBufferSizes.editItemAt(0);
860                for (int i = 0; i < numFrames; i++) {
861                    int32_t decodedSize = mDecodedSizes.itemAt(0);
862                    mDecodedSizes.removeAt(0);
863                    ALOGV("decoded %d of %d", decodedSize, *currentBufLeft);
864                    if (*currentBufLeft > decodedSize) {
865                        // adjust/interpolate next time stamp
866                        *currentBufLeft -= decodedSize;
867                        *nextTimeStamp += mStreamInfo->aacSamplesPerFrame *
868                                1000000ll / mStreamInfo->sampleRate;
869                        ALOGV("adjusted nextTimeStamp/size to %lld/%d",
870                                *nextTimeStamp, *currentBufLeft);
871                    } else {
872                        // move to next timestamp in list
873                        if (mBufferTimestamps.size() > 0) {
874                            mBufferTimestamps.removeAt(0);
875                            nextTimeStamp = &mBufferTimestamps.editItemAt(0);
876                            mBufferSizes.removeAt(0);
877                            currentBufLeft = &mBufferSizes.editItemAt(0);
878                            ALOGV("moved to next time/size: %lld/%d",
879                                    *nextTimeStamp, *currentBufLeft);
880                        }
881                        // try to limit output buffer size to match input buffers
882                        // (e.g when an input buffer contained 4 "sub" frames, output
883                        // at most 4 decoded units in the corresponding output buffer)
884                        // This is optional. Remove the next three lines to fill the output
885                        // buffer with as many units as available.
886                        numFrames = i + 1;
887                        numSamples = numFrames * mStreamInfo->frameSize * mStreamInfo->numChannels;
888                        break;
889                    }
890                }
891
892                ALOGV("getting %d from ringbuffer", numSamples);
893                int32_t ns = outputDelayRingBufferGetSamples(outBuffer, numSamples);
894                if (ns != numSamples) {
895                    ALOGE("not a complete frame of samples available");
896                    mSignalledError = true;
897                    notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
898                    return;
899                }
900            }
901
902            outHeader->nFilledLen = numSamples * sizeof(int16_t);
903
904            if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) {
905                outHeader->nFlags = OMX_BUFFERFLAG_EOS;
906                mEndOfOutput = true;
907            } else {
908                outHeader->nFlags = 0;
909            }
910
911            outHeader->nTimeStamp = currentTime;
912
913            mOutputBufferCount++;
914            outInfo->mOwnedByUs = false;
915            outQueue.erase(outQueue.begin());
916            outInfo = NULL;
917            ALOGV("out timestamp %lld / %d", outHeader->nTimeStamp, outHeader->nFilledLen);
918            notifyFillBufferDone(outHeader);
919            outHeader = NULL;
920        }
921
922        if (mEndOfInput) {
923            if (outputDelayRingBufferSamplesAvailable() > 0
924                    && outputDelayRingBufferSamplesAvailable()
925                            < mStreamInfo->frameSize * mStreamInfo->numChannels) {
926                ALOGE("not a complete frame of samples available");
927                mSignalledError = true;
928                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
929                return;
930            }
931
932            if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) {
933                if (!mEndOfOutput) {
934                    // send empty block signaling EOS
935                    mEndOfOutput = true;
936                    BufferInfo *outInfo = *outQueue.begin();
937                    OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
938
939                    if (outHeader->nOffset != 0) {
940                        ALOGE("outHeader->nOffset != 0 is not handled");
941                        mSignalledError = true;
942                        notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
943                        return;
944                    }
945
946                    INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer
947                            + outHeader->nOffset);
948                    int32_t ns = 0;
949                    outHeader->nFilledLen = 0;
950                    outHeader->nFlags = OMX_BUFFERFLAG_EOS;
951
952                    outHeader->nTimeStamp = mBufferTimestamps.itemAt(0);
953                    mBufferTimestamps.clear();
954                    mBufferSizes.clear();
955                    mDecodedSizes.clear();
956
957                    mOutputBufferCount++;
958                    outInfo->mOwnedByUs = false;
959                    outQueue.erase(outQueue.begin());
960                    outInfo = NULL;
961                    notifyFillBufferDone(outHeader);
962                    outHeader = NULL;
963                }
964                break; // if outQueue not empty but no more output
965            }
966        }
967    }
968}
969
970void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) {
971    if (portIndex == 0) {
972        // Make sure that the next buffer output does not still
973        // depend on fragments from the last one decoded.
974        // drain all existing data
975        drainDecoder();
976        mBufferTimestamps.clear();
977        mBufferSizes.clear();
978        mDecodedSizes.clear();
979        mLastInHeader = NULL;
980    } else {
981        while (outputDelayRingBufferSamplesAvailable() > 0) {
982            int32_t ns = outputDelayRingBufferGetSamples(0,
983                    mStreamInfo->frameSize * mStreamInfo->numChannels);
984            if (ns != mStreamInfo->frameSize * mStreamInfo->numChannels) {
985                ALOGE("not a complete frame of samples available");
986            }
987            mOutputBufferCount++;
988        }
989        mOutputDelayRingBufferReadPos = mOutputDelayRingBufferWritePos;
990    }
991}
992
993void SoftAAC2::drainDecoder() {
994    int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
995
996    // flush decoder until outputDelay is compensated
997    while (mOutputDelayCompensated > 0) {
998        // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
999        INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
1000
1001        // run DRC check
1002        mDrcWrap.submitStreamData(mStreamInfo);
1003        mDrcWrap.update();
1004
1005        AAC_DECODER_ERROR decoderErr =
1006            aacDecoder_DecodeFrame(mAACDecoder,
1007                                   tmpOutBuffer,
1008                                   2048 * MAX_CHANNEL_COUNT,
1009                                   AACDEC_FLUSH);
1010        if (decoderErr != AAC_DEC_OK) {
1011            ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
1012        }
1013
1014        int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
1015        if (tmpOutBufferSamples > mOutputDelayCompensated) {
1016            tmpOutBufferSamples = mOutputDelayCompensated;
1017        }
1018        outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
1019
1020        mOutputDelayCompensated -= tmpOutBufferSamples;
1021    }
1022}
1023
1024void SoftAAC2::onReset() {
1025    drainDecoder();
1026    // reset the "configured" state
1027    mInputBufferCount = 0;
1028    mOutputBufferCount = 0;
1029    mOutputDelayCompensated = 0;
1030    mOutputDelayRingBufferWritePos = 0;
1031    mOutputDelayRingBufferReadPos = 0;
1032    mOutputDelayRingBufferFilled = 0;
1033    mEndOfInput = false;
1034    mEndOfOutput = false;
1035    mBufferTimestamps.clear();
1036    mBufferSizes.clear();
1037    mDecodedSizes.clear();
1038    mLastInHeader = NULL;
1039
1040    // To make the codec behave the same before and after a reset, we need to invalidate the
1041    // streaminfo struct. This does that:
1042    mStreamInfo->sampleRate = 0; // TODO: mStreamInfo is read only
1043
1044    mSignalledError = false;
1045    mOutputPortSettingsChange = NONE;
1046}
1047
1048void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
1049    if (portIndex != 1) {
1050        return;
1051    }
1052
1053    switch (mOutputPortSettingsChange) {
1054        case NONE:
1055            break;
1056
1057        case AWAITING_DISABLED:
1058        {
1059            CHECK(!enabled);
1060            mOutputPortSettingsChange = AWAITING_ENABLED;
1061            break;
1062        }
1063
1064        default:
1065        {
1066            CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
1067            CHECK(enabled);
1068            mOutputPortSettingsChange = NONE;
1069            break;
1070        }
1071    }
1072}
1073
1074}  // namespace android
1075
1076android::SoftOMXComponent *createSoftOMXComponent(
1077        const char *name, const OMX_CALLBACKTYPE *callbacks,
1078        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
1079    return new android::SoftAAC2(name, callbacks, appData, component);
1080}
1081