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