SoftAAC2.cpp revision ed83915060af3eaaf62feef2eb2eec1e7d431891
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SoftAAC2"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include "SoftAAC2.h"
22
23#include <cutils/properties.h>
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/foundation/hexdump.h>
26#include <media/stagefright/MediaErrors.h>
27
28#include <math.h>
29
30#define FILEREAD_MAX_LAYERS 2
31
32#define DRC_DEFAULT_MOBILE_REF_LEVEL 64  /* 64*-0.25dB = -16 dB below full scale for mobile conf */
33#define DRC_DEFAULT_MOBILE_DRC_CUT   127 /* maximum compression of dynamic range for mobile conf */
34#define DRC_DEFAULT_MOBILE_DRC_BOOST 127 /* maximum compression of dynamic range for mobile conf */
35#define DRC_DEFAULT_MOBILE_DRC_HEAVY 1   /* switch for heavy compression for mobile conf */
36#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) */
37#define MAX_CHANNEL_COUNT            8  /* maximum number of audio channels that can be decoded */
38// names of properties that can be used to override the default DRC settings
39#define PROP_DRC_OVERRIDE_REF_LEVEL  "aac_drc_reference_level"
40#define PROP_DRC_OVERRIDE_CUT        "aac_drc_cut"
41#define PROP_DRC_OVERRIDE_BOOST      "aac_drc_boost"
42#define PROP_DRC_OVERRIDE_HEAVY      "aac_drc_heavy"
43#define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level"
44
45namespace android {
46
47template<class T>
48static void InitOMXParams(T *params) {
49    params->nSize = sizeof(T);
50    params->nVersion.s.nVersionMajor = 1;
51    params->nVersion.s.nVersionMinor = 0;
52    params->nVersion.s.nRevision = 0;
53    params->nVersion.s.nStep = 0;
54}
55
56SoftAAC2::SoftAAC2(
57        const char *name,
58        const OMX_CALLBACKTYPE *callbacks,
59        OMX_PTR appData,
60        OMX_COMPONENTTYPE **component)
61    : SimpleSoftOMXComponent(name, callbacks, appData, component),
62      mAACDecoder(NULL),
63      mStreamInfo(NULL),
64      mIsADTS(false),
65      mInputBufferCount(0),
66      mOutputBufferCount(0),
67      mSignalledError(false),
68      mOutputPortSettingsChange(NONE) {
69    for (unsigned int i = 0; i < kNumDelayBlocksMax; i++) {
70        mAnchorTimeUs[i] = 0;
71    }
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    status_t status = UNKNOWN_ERROR;
124    mAACDecoder = aacDecoder_Open(TT_MP4_ADIF, /* num layers */ 1);
125    if (mAACDecoder != NULL) {
126        mStreamInfo = aacDecoder_GetStreamInfo(mAACDecoder);
127        if (mStreamInfo != NULL) {
128            status = OK;
129        }
130    }
131
132    mEndOfInput = false;
133    mEndOfOutput = false;
134    mOutputDelayCompensated = 0;
135    mOutputDelayRingBufferSize = 2048 * MAX_CHANNEL_COUNT * kNumDelayBlocksMax;
136    mOutputDelayRingBuffer = new short[mOutputDelayRingBufferSize];
137    mOutputDelayRingBufferWritePos = 0;
138    mOutputDelayRingBufferReadPos = 0;
139
140    if (mAACDecoder == NULL) {
141        ALOGE("AAC decoder is null. TODO: Can not call aacDecoder_SetParam in the following code");
142    }
143
144    //aacDecoder_SetParam(mAACDecoder, AAC_PCM_LIMITER_ENABLE, 0);
145
146    //init DRC wrapper
147    mDrcWrap.setDecoderHandle(mAACDecoder);
148    mDrcWrap.submitStreamData(mStreamInfo);
149
150    // for streams that contain metadata, use the mobile profile DRC settings unless overridden by platform properties
151    // TODO: change the DRC settings depending on audio output device type (HDMI, loadspeaker, headphone)
152    char value[PROPERTY_VALUE_MAX];
153    //  DRC_PRES_MODE_WRAP_DESIRED_TARGET
154    if (property_get(PROP_DRC_OVERRIDE_REF_LEVEL, value, NULL)) {
155        unsigned refLevel = atoi(value);
156        ALOGV("AAC decoder using desired DRC target reference level of %d instead of %d", refLevel,
157                DRC_DEFAULT_MOBILE_REF_LEVEL);
158        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, refLevel);
159    } else {
160        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_TARGET, DRC_DEFAULT_MOBILE_REF_LEVEL);
161    }
162    //  DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR
163    if (property_get(PROP_DRC_OVERRIDE_CUT, value, NULL)) {
164        unsigned cut = atoi(value);
165        ALOGV("AAC decoder using desired DRC attenuation factor of %d instead of %d", cut,
166                DRC_DEFAULT_MOBILE_DRC_CUT);
167        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, cut);
168    } else {
169        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_ATT_FACTOR, DRC_DEFAULT_MOBILE_DRC_CUT);
170    }
171    //  DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR
172    if (property_get(PROP_DRC_OVERRIDE_BOOST, value, NULL)) {
173        unsigned boost = atoi(value);
174        ALOGV("AAC decoder using desired DRC boost factor of %d instead of %d", boost,
175                DRC_DEFAULT_MOBILE_DRC_BOOST);
176        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, boost);
177    } else {
178        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_BOOST_FACTOR, DRC_DEFAULT_MOBILE_DRC_BOOST);
179    }
180    //  DRC_PRES_MODE_WRAP_DESIRED_HEAVY
181    if (property_get(PROP_DRC_OVERRIDE_HEAVY, value, NULL)) {
182        unsigned heavy = atoi(value);
183        ALOGV("AAC decoder using desried DRC heavy compression switch of %d instead of %d", heavy,
184                DRC_DEFAULT_MOBILE_DRC_HEAVY);
185        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, heavy);
186    } else {
187        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_DESIRED_HEAVY, DRC_DEFAULT_MOBILE_DRC_HEAVY);
188    }
189    // DRC_PRES_MODE_WRAP_ENCODER_TARGET
190    if (property_get(PROP_DRC_OVERRIDE_ENC_LEVEL, value, NULL)) {
191        unsigned encoderRefLevel = atoi(value);
192        ALOGV("AAC decoder using encoder-side DRC reference level of %d instead of %d",
193                encoderRefLevel, DRC_DEFAULT_MOBILE_ENC_LEVEL);
194        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, encoderRefLevel);
195    } else {
196        mDrcWrap.setParam(DRC_PRES_MODE_WRAP_ENCODER_TARGET, DRC_DEFAULT_MOBILE_ENC_LEVEL);
197    }
198
199    return status;
200}
201
202OMX_ERRORTYPE SoftAAC2::internalGetParameter(
203        OMX_INDEXTYPE index, OMX_PTR params) {
204    switch (index) {
205        case OMX_IndexParamAudioAac:
206        {
207            OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
208                (OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
209
210            if (aacParams->nPortIndex != 0) {
211                return OMX_ErrorUndefined;
212            }
213
214            aacParams->nBitRate = 0;
215            aacParams->nAudioBandWidth = 0;
216            aacParams->nAACtools = 0;
217            aacParams->nAACERtools = 0;
218            aacParams->eAACProfile = OMX_AUDIO_AACObjectMain;
219
220            aacParams->eAACStreamFormat =
221                mIsADTS
222                    ? OMX_AUDIO_AACStreamFormatMP4ADTS
223                    : OMX_AUDIO_AACStreamFormatMP4FF;
224
225            aacParams->eChannelMode = OMX_AUDIO_ChannelModeStereo;
226
227            if (!isConfigured()) {
228                aacParams->nChannels = 1;
229                aacParams->nSampleRate = 44100;
230                aacParams->nFrameLength = 0;
231            } else {
232                aacParams->nChannels = mStreamInfo->numChannels;
233                aacParams->nSampleRate = mStreamInfo->sampleRate;
234                aacParams->nFrameLength = mStreamInfo->frameSize;
235            }
236
237            return OMX_ErrorNone;
238        }
239
240        case OMX_IndexParamAudioPcm:
241        {
242            OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
243                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
244
245            if (pcmParams->nPortIndex != 1) {
246                return OMX_ErrorUndefined;
247            }
248
249            pcmParams->eNumData = OMX_NumericalDataSigned;
250            pcmParams->eEndian = OMX_EndianBig;
251            pcmParams->bInterleaved = OMX_TRUE;
252            pcmParams->nBitPerSample = 16;
253            pcmParams->ePCMMode = OMX_AUDIO_PCMModeLinear;
254            pcmParams->eChannelMapping[0] = OMX_AUDIO_ChannelLF;
255            pcmParams->eChannelMapping[1] = OMX_AUDIO_ChannelRF;
256            pcmParams->eChannelMapping[2] = OMX_AUDIO_ChannelCF;
257            pcmParams->eChannelMapping[3] = OMX_AUDIO_ChannelLFE;
258            pcmParams->eChannelMapping[4] = OMX_AUDIO_ChannelLS;
259            pcmParams->eChannelMapping[5] = OMX_AUDIO_ChannelRS;
260
261            if (!isConfigured()) {
262                pcmParams->nChannels = 1;
263                pcmParams->nSamplingRate = 44100;
264            } else {
265                pcmParams->nChannels = mStreamInfo->numChannels;
266                pcmParams->nSamplingRate = mStreamInfo->sampleRate;
267            }
268
269            return OMX_ErrorNone;
270        }
271
272        default:
273            return SimpleSoftOMXComponent::internalGetParameter(index, params);
274    }
275}
276
277OMX_ERRORTYPE SoftAAC2::internalSetParameter(
278        OMX_INDEXTYPE index, const OMX_PTR params) {
279    switch (index) {
280        case OMX_IndexParamStandardComponentRole:
281        {
282            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
283                (const OMX_PARAM_COMPONENTROLETYPE *)params;
284
285            if (strncmp((const char *)roleParams->cRole,
286                        "audio_decoder.aac",
287                        OMX_MAX_STRINGNAME_SIZE - 1)) {
288                return OMX_ErrorUndefined;
289            }
290
291            return OMX_ErrorNone;
292        }
293
294        case OMX_IndexParamAudioAac:
295        {
296            const OMX_AUDIO_PARAM_AACPROFILETYPE *aacParams =
297                (const OMX_AUDIO_PARAM_AACPROFILETYPE *)params;
298
299            if (aacParams->nPortIndex != 0) {
300                return OMX_ErrorUndefined;
301            }
302
303            if (aacParams->eAACStreamFormat == OMX_AUDIO_AACStreamFormatMP4FF) {
304                mIsADTS = false;
305            } else if (aacParams->eAACStreamFormat
306                        == OMX_AUDIO_AACStreamFormatMP4ADTS) {
307                mIsADTS = true;
308            } else {
309                return OMX_ErrorUndefined;
310            }
311
312            return OMX_ErrorNone;
313        }
314
315        case OMX_IndexParamAudioPcm:
316        {
317            const OMX_AUDIO_PARAM_PCMMODETYPE *pcmParams =
318                (OMX_AUDIO_PARAM_PCMMODETYPE *)params;
319
320            if (pcmParams->nPortIndex != 1) {
321                return OMX_ErrorUndefined;
322            }
323
324            return OMX_ErrorNone;
325        }
326
327        default:
328            return SimpleSoftOMXComponent::internalSetParameter(index, params);
329    }
330}
331
332bool SoftAAC2::isConfigured() const {
333    return mInputBufferCount > 0;
334}
335
336void SoftAAC2::configureDownmix() const {
337    char value[PROPERTY_VALUE_MAX];
338    if (!(property_get("media.aac_51_output_enabled", value, NULL)
339            && (!strcmp(value, "1") || !strcasecmp(value, "true")))) {
340        ALOGI("limiting to stereo output");
341        aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, 2);
342        // By default, the decoder creates a 5.1 channel downmix signal
343        // for seven and eight channel input streams. To enable 6.1 and 7.1 channel output
344        // use aacDecoder_SetParam(mAACDecoder, AAC_PCM_MAX_OUTPUT_CHANNELS, -1)
345    }
346}
347
348bool SoftAAC2::outputDelayRingBufferPutSamples(INT_PCM *samples, int32_t numSamples) {
349    if (mOutputDelayRingBufferWritePos + numSamples <= mOutputDelayRingBufferSize
350            && (mOutputDelayRingBufferReadPos <= mOutputDelayRingBufferWritePos
351                    || mOutputDelayRingBufferReadPos > mOutputDelayRingBufferWritePos + numSamples)) {
352        // faster memcopy loop without checks, if the preconditions allow this
353        for (int32_t i = 0; i < numSamples; i++) {
354            mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos++] = samples[i];
355        }
356
357        if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
358            mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
359        }
360        if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) {
361            ALOGE("RING BUFFER OVERFLOW");
362            return false;
363        }
364    } else {
365        ALOGV("slow SoftAAC2::outputDelayRingBufferPutSamples()");
366
367        for (int32_t i = 0; i < numSamples; i++) {
368            mOutputDelayRingBuffer[mOutputDelayRingBufferWritePos] = samples[i];
369            mOutputDelayRingBufferWritePos++;
370            if (mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferSize) {
371                mOutputDelayRingBufferWritePos -= mOutputDelayRingBufferSize;
372            }
373            if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) {
374                ALOGE("RING BUFFER OVERFLOW");
375                return false;
376            }
377        }
378    }
379    return true;
380}
381
382int32_t SoftAAC2::outputDelayRingBufferGetSamples(INT_PCM *samples, int32_t numSamples) {
383    if (mOutputDelayRingBufferReadPos + numSamples <= mOutputDelayRingBufferSize
384            && (mOutputDelayRingBufferWritePos < mOutputDelayRingBufferReadPos
385                    || mOutputDelayRingBufferWritePos >= mOutputDelayRingBufferReadPos + numSamples)) {
386        // faster memcopy loop without checks, if the preconditions allow this
387        if (samples != 0) {
388            for (int32_t i = 0; i < numSamples; i++) {
389                samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos++];
390            }
391        } else {
392            mOutputDelayRingBufferReadPos += numSamples;
393        }
394        if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
395            mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
396        }
397    } else {
398        ALOGV("slow SoftAAC2::outputDelayRingBufferGetSamples()");
399
400        for (int32_t i = 0; i < numSamples; i++) {
401            if (mOutputDelayRingBufferWritePos == mOutputDelayRingBufferReadPos) {
402                ALOGE("RING BUFFER UNDERRUN");
403                return -1;
404            }
405            if (samples != 0) {
406                samples[i] = mOutputDelayRingBuffer[mOutputDelayRingBufferReadPos];
407            }
408            mOutputDelayRingBufferReadPos++;
409            if (mOutputDelayRingBufferReadPos >= mOutputDelayRingBufferSize) {
410                mOutputDelayRingBufferReadPos -= mOutputDelayRingBufferSize;
411            }
412        }
413    }
414    return numSamples;
415}
416
417int32_t SoftAAC2::outputDelayRingBufferSamplesAvailable() {
418    int32_t available = mOutputDelayRingBufferWritePos - mOutputDelayRingBufferReadPos;
419    if (available < 0) {
420        available += mOutputDelayRingBufferSize;
421    }
422    if (available < 0) {
423        ALOGE("FATAL RING BUFFER ERROR");
424        return 0;
425    }
426    return available;
427}
428
429int32_t SoftAAC2::outputDelayRingBufferSamplesLeft() {
430    return mOutputDelayRingBufferSize - outputDelayRingBufferSamplesAvailable();
431}
432
433void SoftAAC2::onQueueFilled(OMX_U32 portIndex) {
434    if (mSignalledError || mOutputPortSettingsChange != NONE) {
435        return;
436    }
437
438    UCHAR* inBuffer[FILEREAD_MAX_LAYERS];
439    UINT inBufferLength[FILEREAD_MAX_LAYERS] = {0};
440    UINT bytesValid[FILEREAD_MAX_LAYERS] = {0};
441
442    List<BufferInfo *> &inQueue = getPortQueue(0);
443    List<BufferInfo *> &outQueue = getPortQueue(1);
444
445    if (portIndex == 0 && mInputBufferCount == 0) {
446        BufferInfo *inInfo = *inQueue.begin();
447        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
448
449        inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
450        inBufferLength[0] = inHeader->nFilledLen;
451
452        AAC_DECODER_ERROR decoderErr =
453            aacDecoder_ConfigRaw(mAACDecoder,
454                                 inBuffer,
455                                 inBufferLength);
456
457        if (decoderErr != AAC_DEC_OK) {
458            ALOGW("aacDecoder_ConfigRaw decoderErr = 0x%4.4x", decoderErr);
459            mSignalledError = true;
460            notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
461            return;
462        }
463
464        mInputBufferCount++;
465        mOutputBufferCount++; // fake increase of outputBufferCount to keep the counters aligned
466
467        inInfo->mOwnedByUs = false;
468        inQueue.erase(inQueue.begin());
469        inInfo = NULL;
470        notifyEmptyBufferDone(inHeader);
471        inHeader = NULL;
472
473        configureDownmix();
474        // Only send out port settings changed event if both sample rate
475        // and numChannels are valid.
476        if (mStreamInfo->sampleRate && mStreamInfo->numChannels) {
477            ALOGI("Initially configuring decoder: %d Hz, %d channels",
478                mStreamInfo->sampleRate,
479                mStreamInfo->numChannels);
480
481            notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
482            mOutputPortSettingsChange = AWAITING_DISABLED;
483        }
484
485        return;
486    }
487
488    while ((!inQueue.empty() || mEndOfInput) && !outQueue.empty()) {
489        if (!inQueue.empty()) {
490            INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
491            BufferInfo *inInfo = *inQueue.begin();
492            OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
493
494            if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
495                mEndOfInput = true;
496            } else {
497                mEndOfInput = false;
498            }
499            if (inHeader->nOffset == 0) { // TODO: does nOffset != 0 happen?
500                mAnchorTimeUs[mInputBufferCount % kNumDelayBlocksMax] =
501                        inHeader->nTimeStamp;
502            }
503
504            if (inHeader->nFilledLen == 0) {
505                inInfo->mOwnedByUs = false;
506                inQueue.erase(inQueue.begin());
507                inInfo = NULL;
508                notifyEmptyBufferDone(inHeader);
509                inHeader = NULL;
510            } else {
511                if (mIsADTS) {
512                    size_t adtsHeaderSize = 0;
513                    // skip 30 bits, aac_frame_length follows.
514                    // ssssssss ssssiiip ppffffPc ccohCCll llllllll lll?????
515
516                    const uint8_t *adtsHeader = inHeader->pBuffer + inHeader->nOffset;
517
518                    bool signalError = false;
519                    if (inHeader->nFilledLen < 7) {
520                        ALOGE("Audio data too short to contain even the ADTS header. "
521                                "Got %d bytes.", inHeader->nFilledLen);
522                        hexdump(adtsHeader, inHeader->nFilledLen);
523                        signalError = true;
524                    } else {
525                        bool protectionAbsent = (adtsHeader[1] & 1);
526
527                        unsigned aac_frame_length =
528                            ((adtsHeader[3] & 3) << 11)
529                            | (adtsHeader[4] << 3)
530                            | (adtsHeader[5] >> 5);
531
532                        if (inHeader->nFilledLen < aac_frame_length) {
533                            ALOGE("Not enough audio data for the complete frame. "
534                                    "Got %d bytes, frame size according to the ADTS "
535                                    "header is %u bytes.",
536                                    inHeader->nFilledLen, aac_frame_length);
537                            hexdump(adtsHeader, inHeader->nFilledLen);
538                            signalError = true;
539                        } else {
540                            adtsHeaderSize = (protectionAbsent ? 7 : 9);
541
542                            inBuffer[0] = (UCHAR *)adtsHeader + adtsHeaderSize;
543                            inBufferLength[0] = aac_frame_length - adtsHeaderSize;
544
545                            inHeader->nOffset += adtsHeaderSize;
546                            inHeader->nFilledLen -= adtsHeaderSize;
547                        }
548                    }
549
550                    if (signalError) {
551                        mSignalledError = true;
552
553                        notify(OMX_EventError,
554                               OMX_ErrorStreamCorrupt,
555                               ERROR_MALFORMED,
556                               NULL);
557
558                        return;
559                    }
560                } else {
561                    inBuffer[0] = inHeader->pBuffer + inHeader->nOffset;
562                    inBufferLength[0] = inHeader->nFilledLen;
563                }
564
565                // Fill and decode
566                bytesValid[0] = inBufferLength[0];
567
568                INT prevSampleRate = mStreamInfo->sampleRate;
569                INT prevNumChannels = mStreamInfo->numChannels;
570
571                aacDecoder_Fill(mAACDecoder,
572                                inBuffer,
573                                inBufferLength,
574                                bytesValid);
575
576                 // run DRC check
577                 mDrcWrap.submitStreamData(mStreamInfo);
578                 mDrcWrap.update();
579
580                AAC_DECODER_ERROR decoderErr =
581                    aacDecoder_DecodeFrame(mAACDecoder,
582                                           tmpOutBuffer,
583                                           2048 * MAX_CHANNEL_COUNT,
584                                           0 /* flags */);
585
586                if (decoderErr != AAC_DEC_OK) {
587                    ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
588                }
589
590                if (decoderErr == AAC_DEC_NOT_ENOUGH_BITS) {
591                    ALOGE("AAC_DEC_NOT_ENOUGH_BITS should never happen");
592                    mSignalledError = true;
593                    notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
594                    return;
595                }
596
597                if (bytesValid[0] != 0) {
598                    ALOGE("bytesValid[0] != 0 should never happen");
599                    mSignalledError = true;
600                    notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
601                    return;
602                }
603
604                size_t numOutBytes =
605                    mStreamInfo->frameSize * sizeof(int16_t) * mStreamInfo->numChannels;
606
607                if (decoderErr == AAC_DEC_OK) {
608                    if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
609                            mStreamInfo->frameSize * mStreamInfo->numChannels)) {
610                        mSignalledError = true;
611                        notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
612                        return;
613                    }
614                    UINT inBufferUsedLength = inBufferLength[0] - bytesValid[0];
615                    inHeader->nFilledLen -= inBufferUsedLength;
616                    inHeader->nOffset += inBufferUsedLength;
617                } else {
618                    ALOGW("AAC decoder returned error 0x%4.4x, substituting silence", decoderErr);
619
620                    memset(tmpOutBuffer, 0, numOutBytes); // TODO: check for overflow
621
622                    if (!outputDelayRingBufferPutSamples(tmpOutBuffer,
623                            mStreamInfo->frameSize * mStreamInfo->numChannels)) {
624                        mSignalledError = true;
625                        notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
626                        return;
627                    }
628
629                    // Discard input buffer.
630                    inHeader->nFilledLen = 0;
631
632                    aacDecoder_SetParam(mAACDecoder, AAC_TPDEC_CLEAR_BUFFER, 1);
633
634                    // fall through
635                }
636
637                /*
638                 * AAC+/eAAC+ streams can be signalled in two ways: either explicitly
639                 * or implicitly, according to MPEG4 spec. AAC+/eAAC+ is a dual
640                 * rate system and the sampling rate in the final output is actually
641                 * doubled compared with the core AAC decoder sampling rate.
642                 *
643                 * Explicit signalling is done by explicitly defining SBR audio object
644                 * type in the bitstream. Implicit signalling is done by embedding
645                 * SBR content in AAC extension payload specific to SBR, and hence
646                 * requires an AAC decoder to perform pre-checks on actual audio frames.
647                 *
648                 * Thus, we could not say for sure whether a stream is
649                 * AAC+/eAAC+ until the first data frame is decoded.
650                 */
651                if (mOutputBufferCount > 1) {
652                    if (mStreamInfo->sampleRate != prevSampleRate ||
653                        mStreamInfo->numChannels != prevNumChannels) {
654                        ALOGE("can not reconfigure AAC output");
655                        mSignalledError = true;
656                        notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
657                        return;
658                    }
659                }
660                if (mInputBufferCount <= 2) { // TODO: <= 1
661                    if (mStreamInfo->sampleRate != prevSampleRate ||
662                        mStreamInfo->numChannels != prevNumChannels) {
663                        ALOGI("Reconfiguring decoder: %d->%d Hz, %d->%d channels",
664                              prevSampleRate, mStreamInfo->sampleRate,
665                              prevNumChannels, mStreamInfo->numChannels);
666
667                        notify(OMX_EventPortSettingsChanged, 1, 0, NULL);
668                        mOutputPortSettingsChange = AWAITING_DISABLED;
669
670                        if (inHeader->nFilledLen == 0) {
671                            inInfo->mOwnedByUs = false;
672                            mInputBufferCount++;
673                            inQueue.erase(inQueue.begin());
674                            inInfo = NULL;
675                            notifyEmptyBufferDone(inHeader);
676                            inHeader = NULL;
677                        }
678                        return;
679                    }
680                } else if (!mStreamInfo->sampleRate || !mStreamInfo->numChannels) {
681                    ALOGW("Invalid AAC stream");
682                    mSignalledError = true;
683                    notify(OMX_EventError, OMX_ErrorUndefined, decoderErr, NULL);
684                    return;
685                }
686                if (inHeader->nFilledLen == 0) {
687                    inInfo->mOwnedByUs = false;
688                    mInputBufferCount++;
689                    inQueue.erase(inQueue.begin());
690                    inInfo = NULL;
691                    notifyEmptyBufferDone(inHeader);
692                    inHeader = NULL;
693                } else {
694                    ALOGW("inHeader->nFilledLen = %d", inHeader->nFilledLen);
695                }
696            }
697        }
698
699        int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
700
701        if (!mEndOfInput && mOutputDelayCompensated < outputDelay) {
702            // discard outputDelay at the beginning
703            int32_t toCompensate = outputDelay - mOutputDelayCompensated;
704            int32_t discard = outputDelayRingBufferSamplesAvailable();
705            if (discard > toCompensate) {
706                discard = toCompensate;
707            }
708            int32_t discarded = outputDelayRingBufferGetSamples(0, discard);
709            mOutputDelayCompensated += discarded;
710            continue;
711        }
712
713        if (mEndOfInput) {
714            while (mOutputDelayCompensated > 0) {
715                // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
716                INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
717
718                 // run DRC check
719                 mDrcWrap.submitStreamData(mStreamInfo);
720                 mDrcWrap.update();
721
722                AAC_DECODER_ERROR decoderErr =
723                    aacDecoder_DecodeFrame(mAACDecoder,
724                                           tmpOutBuffer,
725                                           2048 * MAX_CHANNEL_COUNT,
726                                           AACDEC_FLUSH);
727                if (decoderErr != AAC_DEC_OK) {
728                    ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
729                }
730
731                int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
732                if (tmpOutBufferSamples > mOutputDelayCompensated) {
733                    tmpOutBufferSamples = mOutputDelayCompensated;
734                }
735                outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
736                mOutputDelayCompensated -= tmpOutBufferSamples;
737            }
738        }
739
740        while (!outQueue.empty()
741                && outputDelayRingBufferSamplesAvailable()
742                        >= mStreamInfo->frameSize * mStreamInfo->numChannels) {
743            BufferInfo *outInfo = *outQueue.begin();
744            OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
745
746            if (outHeader->nOffset != 0) {
747                ALOGE("outHeader->nOffset != 0 is not handled");
748                mSignalledError = true;
749                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
750                return;
751            }
752
753            INT_PCM *outBuffer =
754                    reinterpret_cast<INT_PCM *>(outHeader->pBuffer + outHeader->nOffset);
755            if (outHeader->nOffset
756                    + mStreamInfo->frameSize * mStreamInfo->numChannels * sizeof(int16_t)
757                    > outHeader->nAllocLen) {
758                ALOGE("buffer overflow");
759                mSignalledError = true;
760                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
761                return;
762
763            }
764            int32_t ns = outputDelayRingBufferGetSamples(outBuffer,
765                    mStreamInfo->frameSize * mStreamInfo->numChannels); // TODO: check for overflow
766            if (ns != mStreamInfo->frameSize * mStreamInfo->numChannels) {
767                ALOGE("not a complete frame of samples available");
768                mSignalledError = true;
769                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
770                return;
771            }
772
773            outHeader->nFilledLen = mStreamInfo->frameSize * mStreamInfo->numChannels
774                    * sizeof(int16_t);
775            if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) {
776                outHeader->nFlags = OMX_BUFFERFLAG_EOS;
777                mEndOfOutput = true;
778            } else {
779                outHeader->nFlags = 0;
780            }
781
782            outHeader->nTimeStamp = mAnchorTimeUs[mOutputBufferCount
783                    % kNumDelayBlocksMax];
784
785            mOutputBufferCount++;
786            outInfo->mOwnedByUs = false;
787            outQueue.erase(outQueue.begin());
788            outInfo = NULL;
789            notifyFillBufferDone(outHeader);
790            outHeader = NULL;
791        }
792
793        if (mEndOfInput) {
794            if (outputDelayRingBufferSamplesAvailable() > 0
795                    && outputDelayRingBufferSamplesAvailable()
796                            < mStreamInfo->frameSize * mStreamInfo->numChannels) {
797                ALOGE("not a complete frame of samples available");
798                mSignalledError = true;
799                notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
800                return;
801            }
802
803            if (mEndOfInput && !outQueue.empty() && outputDelayRingBufferSamplesAvailable() == 0) {
804                if (!mEndOfOutput) {
805                    // send empty block signaling EOS
806                    mEndOfOutput = true;
807                    BufferInfo *outInfo = *outQueue.begin();
808                    OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
809
810                    if (outHeader->nOffset != 0) {
811                        ALOGE("outHeader->nOffset != 0 is not handled");
812                        mSignalledError = true;
813                        notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL);
814                        return;
815                    }
816
817                    INT_PCM *outBuffer = reinterpret_cast<INT_PCM *>(outHeader->pBuffer
818                            + outHeader->nOffset);
819                    int32_t ns = 0;
820                    outHeader->nFilledLen = 0;
821                    outHeader->nFlags = OMX_BUFFERFLAG_EOS;
822
823                    outHeader->nTimeStamp = mAnchorTimeUs[mOutputBufferCount
824                            % kNumDelayBlocksMax];
825
826                    mOutputBufferCount++;
827                    outInfo->mOwnedByUs = false;
828                    outQueue.erase(outQueue.begin());
829                    outInfo = NULL;
830                    notifyFillBufferDone(outHeader);
831                    outHeader = NULL;
832                }
833                break; // if outQueue not empty but no more output
834            }
835        }
836    }
837}
838
839void SoftAAC2::onPortFlushCompleted(OMX_U32 portIndex) {
840    if (portIndex == 0) {
841        // Make sure that the next buffer output does not still
842        // depend on fragments from the last one decoded.
843        // drain all existing data
844        drainDecoder();
845    } else {
846        while (outputDelayRingBufferSamplesAvailable() > 0) {
847            int32_t ns = outputDelayRingBufferGetSamples(0,
848                    mStreamInfo->frameSize * mStreamInfo->numChannels);
849            if (ns != mStreamInfo->frameSize * mStreamInfo->numChannels) {
850                ALOGE("not a complete frame of samples available");
851            }
852            mOutputBufferCount++;
853        }
854        mOutputDelayRingBufferReadPos = mOutputDelayRingBufferWritePos;
855    }
856}
857
858void SoftAAC2::drainDecoder() {
859    int32_t outputDelay = mStreamInfo->outputDelay * mStreamInfo->numChannels;
860
861    // flush decoder until outputDelay is compensated
862    while (mOutputDelayCompensated > 0) {
863        // a buffer big enough for MAX_CHANNEL_COUNT channels of decoded HE-AAC
864        INT_PCM tmpOutBuffer[2048 * MAX_CHANNEL_COUNT];
865
866        // run DRC check
867        mDrcWrap.submitStreamData(mStreamInfo);
868        mDrcWrap.update();
869
870        AAC_DECODER_ERROR decoderErr =
871            aacDecoder_DecodeFrame(mAACDecoder,
872                                   tmpOutBuffer,
873                                   2048 * MAX_CHANNEL_COUNT,
874                                   AACDEC_FLUSH);
875        if (decoderErr != AAC_DEC_OK) {
876            ALOGW("aacDecoder_DecodeFrame decoderErr = 0x%4.4x", decoderErr);
877        }
878
879        int32_t tmpOutBufferSamples = mStreamInfo->frameSize * mStreamInfo->numChannels;
880        if (tmpOutBufferSamples > mOutputDelayCompensated) {
881            tmpOutBufferSamples = mOutputDelayCompensated;
882        }
883        outputDelayRingBufferPutSamples(tmpOutBuffer, tmpOutBufferSamples);
884
885        mOutputDelayCompensated -= tmpOutBufferSamples;
886    }
887}
888
889void SoftAAC2::onReset() {
890    drainDecoder();
891    // reset the "configured" state
892    mInputBufferCount = 0;
893    mOutputBufferCount = 0;
894    mOutputDelayCompensated = 0;
895    mOutputDelayRingBufferWritePos = 0;
896    mOutputDelayRingBufferReadPos = 0;
897    mEndOfInput = false;
898    mEndOfOutput = false;
899
900    // To make the codec behave the same before and after a reset, we need to invalidate the
901    // streaminfo struct. This does that:
902    mStreamInfo->sampleRate = 0; // TODO: mStreamInfo is read only
903
904    mSignalledError = false;
905    mOutputPortSettingsChange = NONE;
906}
907
908void SoftAAC2::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) {
909    if (portIndex != 1) {
910        return;
911    }
912
913    switch (mOutputPortSettingsChange) {
914        case NONE:
915            break;
916
917        case AWAITING_DISABLED:
918        {
919            CHECK(!enabled);
920            mOutputPortSettingsChange = AWAITING_ENABLED;
921            break;
922        }
923
924        default:
925        {
926            CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED);
927            CHECK(enabled);
928            mOutputPortSettingsChange = NONE;
929            break;
930        }
931    }
932}
933
934}  // namespace android
935
936android::SoftOMXComponent *createSoftOMXComponent(
937        const char *name, const OMX_CALLBACKTYPE *callbacks,
938        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
939    return new android::SoftAAC2(name, callbacks, appData, component);
940}
941