Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2011 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 *************************************************************************
     18 * @file   VideoEditorAudioDecoder.cpp
     19 * @brief  StageFright shell Audio Decoder
     20 *************************************************************************
     21 */
     22 
     23 #define LOG_NDEBUG 1
     24 #define LOG_TAG "VIDEOEDITOR_AUDIODECODER"
     25 
     26 #include "M4OSA_Debug.h"
     27 #include "VideoEditorAudioDecoder.h"
     28 #include "VideoEditorUtils.h"
     29 #include "M4MCS_InternalTypes.h"
     30 
     31 #include "utils/Log.h"
     32 #include "utils/Vector.h"
     33 #include <media/stagefright/foundation/ADebug.h>
     34 #include <media/stagefright/MediaSource.h>
     35 #include <media/stagefright/MediaDefs.h>
     36 #include <media/stagefright/MetaData.h>
     37 #include <media/stagefright/OMXClient.h>
     38 #include <media/stagefright/OMXCodec.h>
     39 
     40 /********************
     41  *   DEFINITIONS    *
     42  ********************/
     43 // Version
     44 #define VIDEOEDITOR_AUDIO_DECODER_VERSION_MAJOR 1
     45 #define VIDEOEDITOR_AUDIO_DECODER_VERSION_MINOR 0
     46 #define VIDEOEDITOR_AUDIO_DECODER_VERSION_REV   0
     47 
     48 // Force using software decoder as engine does not support prefetch
     49 #define VIDEOEDITOR_FORCECODEC kSoftwareCodecsOnly
     50 
     51 namespace android {
     52 
     53 struct VideoEditorAudioDecoderSource : public MediaSource {
     54     public:
     55         static sp<VideoEditorAudioDecoderSource> Create(
     56                 const sp<MetaData>& format, void *decoderShellContext);
     57         virtual status_t start(MetaData *params = NULL);
     58         virtual status_t stop();
     59         virtual sp<MetaData> getFormat();
     60         virtual status_t read(MediaBuffer **buffer,
     61         const ReadOptions *options = NULL);
     62         virtual void storeBuffer(MediaBuffer *buffer);
     63 
     64     protected:
     65         virtual ~VideoEditorAudioDecoderSource();
     66 
     67     private:
     68         enum State {
     69             CREATED,
     70             STARTED,
     71             ERROR
     72         };
     73         VideoEditorAudioDecoderSource(const sp<MetaData>& format,
     74          void *decoderShellContext);
     75         sp<MetaData> mFormat;
     76         Vector<MediaBuffer*> mBuffers;
     77         Mutex mLock;  // protects mBuffers
     78         bool mIsEOS;
     79         State mState;
     80         void* mDecShellContext;
     81         // Don't call me.
     82         VideoEditorAudioDecoderSource(const VideoEditorAudioDecoderSource&);
     83         VideoEditorAudioDecoderSource& operator=(
     84             const VideoEditorAudioDecoderSource &);
     85 };
     86 
     87 /**
     88  ******************************************************************************
     89  * structure VideoEditorAudioDecoder_Context
     90  * @brief    This structure defines the context of the StageFright audio decoder
     91  *           shell
     92  ******************************************************************************
     93 */
     94 
     95 typedef struct {
     96     M4AD_Type                          mDecoderType;
     97     M4_AudioStreamHandler*             mAudioStreamHandler;
     98     sp<VideoEditorAudioDecoderSource>  mDecoderSource;
     99     OMXClient                          mClient;
    100     sp<MediaSource>                    mDecoder;
    101     int32_t                            mNbOutputChannels;
    102     uint32_t                           mNbInputFrames;
    103     uint32_t                           mNbOutputFrames;
    104     M4READER_DataInterface  *m_pReader;
    105     M4_AccessUnit* m_pNextAccessUnitToDecode;
    106     M4OSA_ERR readerErrCode;
    107     int32_t timeStampMs;
    108 
    109 } VideoEditorAudioDecoder_Context;
    110 
    111 sp<VideoEditorAudioDecoderSource> VideoEditorAudioDecoderSource::Create(
    112         const sp<MetaData>& format, void *decoderShellContext) {
    113 
    114     sp<VideoEditorAudioDecoderSource> aSource =
    115         new VideoEditorAudioDecoderSource(format, decoderShellContext);
    116 
    117     return aSource;
    118 }
    119 
    120 VideoEditorAudioDecoderSource::VideoEditorAudioDecoderSource(
    121         const sp<MetaData>& format, void* decoderShellContext):
    122         mFormat(format),
    123         mIsEOS(false),
    124         mState(CREATED),
    125         mDecShellContext(decoderShellContext) {
    126 }
    127 
    128 VideoEditorAudioDecoderSource::~VideoEditorAudioDecoderSource() {
    129 
    130     if( STARTED == mState ) {
    131         stop();
    132     }
    133 }
    134 
    135 status_t VideoEditorAudioDecoderSource::start(MetaData *meta) {
    136     status_t err = OK;
    137 
    138     if( CREATED != mState ) {
    139         ALOGV("VideoEditorAudioDecoderSource::start: invalid state %d", mState);
    140         return UNKNOWN_ERROR;
    141     }
    142 
    143     mState = STARTED;
    144 
    145 cleanUp:
    146     ALOGV("VideoEditorAudioDecoderSource::start END (0x%x)", err);
    147     return err;
    148 }
    149 
    150 status_t VideoEditorAudioDecoderSource::stop() {
    151     Mutex::Autolock autolock(mLock);
    152     status_t err = OK;
    153 
    154     ALOGV("VideoEditorAudioDecoderSource::stop begin");
    155 
    156     if( STARTED != mState ) {
    157         ALOGV("VideoEditorAudioDecoderSource::stop: invalid state %d", mState);
    158         return UNKNOWN_ERROR;
    159     }
    160 
    161     if (!mBuffers.empty()) {
    162         int n = mBuffers.size();
    163         for (int i = 0; i < n; i++) {
    164             mBuffers.itemAt(i)->release();
    165         }
    166         ALOGW("VideoEditorAudioDecoderSource::stop : %d buffer remained", n);
    167         mBuffers.clear();
    168     }
    169 
    170     mState = CREATED;
    171 
    172     ALOGV("VideoEditorAudioDecoderSource::stop END (0x%x)", err);
    173     return err;
    174 }
    175 
    176 sp<MetaData> VideoEditorAudioDecoderSource::getFormat() {
    177 
    178     ALOGV("VideoEditorAudioDecoderSource::getFormat");
    179     return mFormat;
    180 }
    181 
    182 static MediaBuffer* readBufferFromReader(
    183         VideoEditorAudioDecoder_Context* pDecContext) {
    184     M4OSA_ERR lerr = M4NO_ERROR;
    185     M4_AccessUnit* pAccessUnit = pDecContext->m_pNextAccessUnitToDecode;
    186 
    187     // Get next AU from reader.
    188     lerr = pDecContext->m_pReader->m_pFctGetNextAu(
    189                pDecContext->m_pReader->m_readerContext,
    190                (M4_StreamHandler*)pDecContext->mAudioStreamHandler,
    191                pAccessUnit);
    192 
    193     if (lerr == M4WAR_NO_MORE_AU) {
    194         ALOGV("readBufferFromReader : EOS");
    195         return NULL;
    196     }
    197 
    198     pDecContext->timeStampMs = pAccessUnit->m_CTS;
    199 
    200     MediaBuffer* newBuffer = new MediaBuffer((size_t)pAccessUnit->m_size);
    201     memcpy((void *)((M4OSA_Int8*)newBuffer->data() + newBuffer->range_offset()),
    202         (void *)pAccessUnit->m_dataAddress, pAccessUnit->m_size);
    203     newBuffer->meta_data()->setInt64(kKeyTime, (pAccessUnit->m_CTS * 1000LL));
    204     return newBuffer;
    205 }
    206 
    207 status_t VideoEditorAudioDecoderSource::read(MediaBuffer **buffer,
    208         const ReadOptions *options) {
    209     Mutex::Autolock autolock(mLock);
    210     MediaSource::ReadOptions readOptions;
    211 
    212     VideoEditorAudioDecoder_Context* pDecContext =
    213      (VideoEditorAudioDecoder_Context *)mDecShellContext;
    214 
    215     if ( STARTED != mState ) {
    216         ALOGV("VideoEditorAudioDecoderSource::read invalid state %d", mState);
    217         return UNKNOWN_ERROR;
    218     }
    219 
    220     // Get a buffer from the reader if we don't have any
    221     if(mBuffers.empty()) {
    222         MediaBuffer* newBuffer = readBufferFromReader(pDecContext);
    223         if (!newBuffer) {
    224             *buffer = NULL;
    225             pDecContext->readerErrCode = M4WAR_NO_MORE_AU;
    226             return ERROR_END_OF_STREAM;
    227         }
    228         mBuffers.push(newBuffer);
    229     }
    230     *buffer = mBuffers.itemAt(0);
    231     mBuffers.removeAt(0);
    232 
    233     return OK;
    234 }
    235 
    236 void VideoEditorAudioDecoderSource::storeBuffer(MediaBuffer *buffer) {
    237     Mutex::Autolock autolock(mLock);
    238     VideoEditorAudioDecoder_Context* pDecContext =
    239      (VideoEditorAudioDecoder_Context *)mDecShellContext;
    240 
    241     ALOGV("VideoEditorAudioDecoderSource::storeBuffer begin");
    242 
    243     // If the user didn't give us a buffer, get it from the reader.
    244     if(buffer == NULL) {
    245         MediaBuffer* newBuffer = readBufferFromReader(pDecContext);
    246         if (!newBuffer) {
    247             pDecContext->readerErrCode = M4WAR_NO_MORE_AU;
    248             return;
    249         }
    250         buffer = newBuffer;
    251     }
    252 
    253     mBuffers.push(buffer);
    254     ALOGV("VideoEditorAudioDecoderSource::storeBuffer END");
    255 }
    256 
    257 /********************
    258  *      TOOLS       *
    259  ********************/
    260 
    261 M4OSA_ERR VideoEditorAudioDecoder_getBits(M4OSA_Int8* pData,
    262         M4OSA_UInt32 dataSize, M4OSA_UInt8 nbBits, M4OSA_Int32* pResult,
    263         M4OSA_UInt32* pOffset) {
    264 
    265     M4OSA_ERR err = M4NO_ERROR;
    266     M4OSA_UInt32 startByte = 0;
    267     M4OSA_UInt32 startBit = 0;
    268     M4OSA_UInt32 endByte = 0;
    269     M4OSA_UInt32 endBit = 0;
    270     M4OSA_UInt32 currentByte = 0;
    271     M4OSA_UInt32 result = 0;
    272     M4OSA_UInt32 ui32Tmp = 0;
    273     M4OSA_UInt32 ui32Mask = 0;
    274 
    275     // Input parameters check
    276     VIDEOEDITOR_CHECK(M4OSA_NULL != pData, M4ERR_PARAMETER);
    277     VIDEOEDITOR_CHECK(M4OSA_NULL != pOffset, M4ERR_PARAMETER);
    278     VIDEOEDITOR_CHECK(32 >= nbBits, M4ERR_PARAMETER);
    279     VIDEOEDITOR_CHECK((*pOffset + nbBits) <= 8*dataSize, M4ERR_PARAMETER);
    280 
    281     ALOGV("VideoEditorAudioDecoder_getBits begin");
    282 
    283     startByte   = (*pOffset) >> 3;
    284     endByte     = (*pOffset + nbBits) >> 3;
    285     startBit    = (*pOffset) % 8;
    286     endBit      = (*pOffset + nbBits) % 8;
    287     currentByte = startByte;
    288 
    289     // Extract the requested nunber of bits from memory
    290     while( currentByte <= endByte) {
    291         ui32Mask = 0x000000FF;
    292         if( currentByte == startByte ) {
    293             ui32Mask >>= startBit;
    294         }
    295         ui32Tmp = ui32Mask & ((M4OSA_UInt32)pData[currentByte]);
    296         if( currentByte == endByte ) {
    297             ui32Tmp >>= (8-endBit);
    298             result <<= endBit;
    299         } else {
    300             result <<= 8;
    301         }
    302         result |= ui32Tmp;
    303         currentByte++;
    304     }
    305 
    306     *pResult = result;
    307     *pOffset += nbBits;
    308 
    309 cleanUp:
    310     if( M4NO_ERROR == err ) {
    311         ALOGV("VideoEditorAudioDecoder_getBits no error");
    312     } else {
    313         ALOGV("VideoEditorAudioDecoder_getBits ERROR 0x%X", err);
    314     }
    315     ALOGV("VideoEditorAudioDecoder_getBits end");
    316     return err;
    317 }
    318 
    319 
    320 #define FREQ_TABLE_SIZE 16
    321 const M4OSA_UInt32 AD_AAC_FREQ_TABLE[FREQ_TABLE_SIZE] =
    322     {96000, 88200, 64000, 48000, 44100,
    323     32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0};
    324 
    325 
    326 M4OSA_ERR VideoEditorAudioDecoder_parse_AAC_DSI(M4OSA_Int8* pDSI,
    327         M4OSA_UInt32 dsiSize, AAC_DEC_STREAM_PROPS* pProperties) {
    328 
    329     M4OSA_ERR err = M4NO_ERROR;
    330     M4OSA_UInt32 offset = 0;
    331     M4OSA_Int32 result = 0;
    332 
    333     ALOGV("VideoEditorAudioDecoder_parse_AAC_DSI begin");
    334 
    335     // Input parameters check
    336     VIDEOEDITOR_CHECK(M4OSA_NULL != pDSI, M4ERR_PARAMETER);
    337     VIDEOEDITOR_CHECK(M4OSA_NULL != pProperties, M4ERR_PARAMETER);
    338 
    339     // Get the object type
    340     err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 5, &result, &offset);
    341     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    342     switch( result ) {
    343         case 2:
    344             /* Audio Object Type is 2 (AAC Low Complexity) */
    345             pProperties->aPSPresent  = 0;
    346             pProperties->aSBRPresent = 0;
    347             break;
    348         case 5:
    349             /* Audio Object Type is 5 (Spectral Band Replication) */
    350             pProperties->aPSPresent  = 0;
    351             pProperties->aSBRPresent = 1;
    352             break;
    353         case 29:
    354             /* Audio Object Type is 29 (Parametric Stereo) */
    355             pProperties->aPSPresent  = 1;
    356             pProperties->aSBRPresent = 1;
    357             break;
    358         default:
    359             ALOGV("parse_AAC_DSI ERROR : object type %d is not supported",
    360                 result);
    361             VIDEOEDITOR_CHECK(!"invalid AAC object type", M4ERR_BAD_OPTION_ID);
    362             break;
    363     }
    364     pProperties->aAudioObjectType = (M4OSA_Int32)result;
    365 
    366     // Get the frequency index
    367     err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 4, &result, &offset);
    368     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    369     VIDEOEDITOR_CHECK((0 <= result) && (FREQ_TABLE_SIZE > result),
    370         M4ERR_PARAMETER);
    371     pProperties->aSampFreq = AD_AAC_FREQ_TABLE[result];
    372     pProperties->aExtensionSampFreq = 0;
    373 
    374     // Get the number of channels
    375     err = VideoEditorAudioDecoder_getBits(pDSI, dsiSize, 4, &result, &offset);
    376     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    377     pProperties->aNumChan = (M4OSA_UInt32)result;
    378 
    379     // Set the max PCM samples per channel
    380     pProperties->aMaxPCMSamplesPerCh = (pProperties->aSBRPresent) ? 2048 : 1024;
    381 
    382 cleanUp:
    383     if( M4NO_ERROR == err ) {
    384         ALOGV("VideoEditorAudioDecoder_parse_AAC_DSI no error");
    385     } else {
    386         ALOGV("VideoEditorAudioDecoder_parse_AAC_DSI ERROR 0x%X", err);
    387     }
    388     ALOGV("VideoEditorAudioDecoder_parse_AAC_DSI end");
    389     return err;
    390 }
    391 
    392 /********************
    393  * ENGINE INTERFACE *
    394  ********************/
    395 
    396 M4OSA_ERR VideoEditorAudioDecoder_destroy(M4AD_Context pContext) {
    397     M4OSA_ERR err = M4NO_ERROR;
    398     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    399 
    400     ALOGV("VideoEditorAudioDecoder_destroy begin");
    401     // Input parameters check
    402     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    403 
    404     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    405 
    406     // Stop the graph
    407     if( M4OSA_NULL != pDecoderContext->mDecoder.get() ) {
    408         pDecoderContext->mDecoder->stop();
    409     }
    410 
    411     // Destroy the graph
    412     pDecoderContext->mDecoderSource.clear();
    413     pDecoderContext->mDecoder.clear();
    414     pDecoderContext->mClient.disconnect();
    415 
    416     SAFE_FREE(pDecoderContext);
    417     pContext = M4OSA_NULL;
    418     ALOGV("VideoEditorAudioDecoder_destroy : DONE");
    419 
    420 cleanUp:
    421     if( M4NO_ERROR == err ) {
    422         ALOGV("VideoEditorAudioDecoder_destroy no error");
    423     } else {
    424         ALOGV("VideoEditorAudioDecoder_destroy ERROR 0x%X", err);
    425     }
    426     ALOGV("VideoEditorAudioDecoder_destroy : end");
    427     return err;
    428 }
    429 
    430 M4OSA_ERR VideoEditorAudioDecoder_create(M4AD_Type decoderType,
    431         M4AD_Context* pContext, M4_AudioStreamHandler* pStreamHandler,
    432         void* pUserData) {
    433     M4OSA_ERR err = M4NO_ERROR;
    434     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    435     AAC_DEC_STREAM_PROPS aacProperties;
    436     status_t result = OK;
    437     sp<MetaData> decoderMetaData = NULL;
    438     const char* mime = NULL;
    439     uint32_t codecFlags = 0;
    440 
    441     ALOGV("VideoEditorAudioDecoder_create begin: decoderType %d", decoderType);
    442 
    443     // Input parameters check
    444     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext,       M4ERR_PARAMETER);
    445     VIDEOEDITOR_CHECK(M4OSA_NULL != pStreamHandler, M4ERR_PARAMETER);
    446 
    447     // Context allocation & initialization
    448     SAFE_MALLOC(pDecoderContext, VideoEditorAudioDecoder_Context, 1,
    449         "AudioDecoder");
    450     pDecoderContext->mDecoderType = decoderType;
    451     pDecoderContext->mAudioStreamHandler = pStreamHandler;
    452 
    453     pDecoderContext->mNbInputFrames  = 0;
    454     pDecoderContext->mNbOutputFrames = 0;
    455     pDecoderContext->readerErrCode = M4NO_ERROR;
    456     pDecoderContext->timeStampMs = -1;
    457 
    458     ALOGV("VideoEditorAudioDecoder_create : maxAUSize %d",
    459         pDecoderContext->mAudioStreamHandler->m_basicProperties.m_maxAUSize);
    460 
    461     // Create the meta data for the decoder
    462     decoderMetaData = new MetaData;
    463     switch( pDecoderContext->mDecoderType ) {
    464         case M4AD_kTypeAMRNB:
    465             // StageFright parameters
    466             mime = MEDIA_MIMETYPE_AUDIO_AMR_NB;
    467             // Engine parameters
    468             pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 160;
    469             // Number of bytes per sample
    470             pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
    471             pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 8000;
    472             pDecoderContext->mAudioStreamHandler->m_nbChannels = 1;
    473             break;
    474 
    475         case M4AD_kTypeAMRWB:
    476             // StageFright parameters
    477             mime = MEDIA_MIMETYPE_AUDIO_AMR_WB;
    478 
    479             pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 160;
    480             // Number of bytes per sample
    481             pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
    482             pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 16000;
    483             pDecoderContext->mAudioStreamHandler->m_nbChannels = 1;
    484             break;
    485 
    486         case M4AD_kTypeAAC:
    487             // Reject ADTS & ADIF (or any incorrect type)
    488             VIDEOEDITOR_CHECK(M4DA_StreamTypeAudioAac ==
    489                 pDecoderContext->mAudioStreamHandler->\
    490                 m_basicProperties.m_streamType,M4ERR_PARAMETER);
    491 
    492             // StageFright parameters
    493             mime = MEDIA_MIMETYPE_AUDIO_AAC;
    494 
    495             decoderMetaData->setData(kKeyESDS, kTypeESDS,
    496                 pStreamHandler->m_basicProperties.m_pESDSInfo,
    497                 pStreamHandler->m_basicProperties.m_ESDSInfoSize);
    498 
    499             // Engine parameters
    500             // Retrieve sampling frequency and number of channels from the DSI
    501             err = VideoEditorAudioDecoder_parse_AAC_DSI(
    502                 (M4OSA_Int8*)pStreamHandler->m_basicProperties.\
    503                     m_pDecoderSpecificInfo,
    504                 pStreamHandler->m_basicProperties.m_decoderSpecificInfoSize,
    505                 &aacProperties);
    506 
    507             VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    508             pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 1024;
    509             // Number of bytes per sample
    510             pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
    511             pDecoderContext->mAudioStreamHandler->m_samplingFrequency =
    512                 aacProperties.aSampFreq;
    513             pDecoderContext->mAudioStreamHandler->m_nbChannels =
    514                 aacProperties.aNumChan;
    515 
    516             // Copy the stream properties into userdata
    517             if( M4OSA_NULL != pUserData ) {
    518                 memcpy((void *)pUserData,
    519                     (void *)&aacProperties,
    520                     sizeof(AAC_DEC_STREAM_PROPS));
    521             }
    522             break;
    523 
    524         case M4AD_kTypeMP3:
    525             // StageFright parameters
    526             mime = MEDIA_MIMETYPE_AUDIO_MPEG;
    527             break;
    528 
    529         default:
    530             VIDEOEDITOR_CHECK(!"AudioDecoder_open : incorrect input format",
    531                 M4ERR_STATE);
    532             break;
    533     }
    534     decoderMetaData->setCString(kKeyMIMEType, mime);
    535     decoderMetaData->setInt32(kKeySampleRate,
    536         (int32_t)pDecoderContext->mAudioStreamHandler->m_samplingFrequency);
    537     decoderMetaData->setInt32(kKeyChannelCount,
    538         pDecoderContext->mAudioStreamHandler->m_nbChannels);
    539     decoderMetaData->setInt64(kKeyDuration,
    540         (int64_t)pDecoderContext->mAudioStreamHandler->\
    541         m_basicProperties.m_duration);
    542 
    543     // Create the decoder source
    544     pDecoderContext->mDecoderSource = VideoEditorAudioDecoderSource::Create(
    545         decoderMetaData, (void *)pDecoderContext);
    546     VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoderSource.get(),
    547         M4ERR_STATE);
    548 
    549     // Connect to the OMX client
    550     result = pDecoderContext->mClient.connect();
    551     VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
    552 
    553     // Create the OMX codec
    554 #ifdef VIDEOEDITOR_FORCECODEC
    555     codecFlags |= OMXCodec::VIDEOEDITOR_FORCECODEC;
    556 #endif /* VIDEOEDITOR_FORCECODEC */
    557 
    558     pDecoderContext->mDecoder = OMXCodec::Create(pDecoderContext->\
    559         mClient.interface(),
    560         decoderMetaData, false, pDecoderContext->mDecoderSource, NULL,
    561             codecFlags);
    562     VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoder.get(), M4ERR_STATE);
    563 
    564     // Get the output channels, the decoder might overwrite the input metadata
    565     pDecoderContext->mDecoder->getFormat()->findInt32(kKeyChannelCount,
    566         &pDecoderContext->mNbOutputChannels);
    567     ALOGV("VideoEditorAudioDecoder_create : output chan %d",
    568         pDecoderContext->mNbOutputChannels);
    569 
    570     // Start the decoder
    571     result = pDecoderContext->mDecoder->start();
    572     VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
    573 
    574     *pContext = pDecoderContext;
    575     ALOGV("VideoEditorAudioDecoder_create : DONE");
    576 
    577 cleanUp:
    578     if( M4NO_ERROR == err ) {
    579         ALOGV("VideoEditorAudioDecoder_create no error");
    580     } else {
    581         VideoEditorAudioDecoder_destroy(pDecoderContext);
    582         *pContext = M4OSA_NULL;
    583         ALOGV("VideoEditorAudioDecoder_create ERROR 0x%X", err);
    584     }
    585     return err;
    586 }
    587 
    588 M4OSA_ERR VideoEditorAudioDecoder_create_AAC(M4AD_Context* pContext,
    589         M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
    590 
    591     return VideoEditorAudioDecoder_create(
    592         M4AD_kTypeAAC, pContext, pStreamHandler,pUserData);
    593 }
    594 
    595 
    596 M4OSA_ERR VideoEditorAudioDecoder_create_AMRNB(M4AD_Context* pContext,
    597         M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
    598 
    599     return VideoEditorAudioDecoder_create(
    600         M4AD_kTypeAMRNB, pContext, pStreamHandler, pUserData);
    601 }
    602 
    603 
    604 M4OSA_ERR VideoEditorAudioDecoder_create_AMRWB(M4AD_Context* pContext,
    605         M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
    606 
    607     return VideoEditorAudioDecoder_create(
    608         M4AD_kTypeAMRWB, pContext, pStreamHandler, pUserData);
    609 }
    610 
    611 
    612 M4OSA_ERR VideoEditorAudioDecoder_create_MP3(M4AD_Context* pContext,
    613         M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
    614 
    615     return VideoEditorAudioDecoder_create(
    616         M4AD_kTypeMP3, pContext, pStreamHandler, pUserData);
    617 }
    618 
    619 M4OSA_ERR VideoEditorAudioDecoder_processInputBuffer(
    620         M4AD_Context pContext, M4AD_Buffer* pInputBuffer) {
    621     M4OSA_ERR err = M4NO_ERROR;
    622     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    623     MediaBuffer* buffer = NULL;
    624 
    625     ALOGV("VideoEditorAudioDecoder_processInputBuffer begin");
    626     // Input parameters check
    627     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    628 
    629 
    630     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    631 
    632     if( M4OSA_NULL != pInputBuffer ) {
    633         buffer = new MediaBuffer((size_t)pInputBuffer->m_bufferSize);
    634         memcpy((void *)((M4OSA_Int8*)buffer->data() + buffer->range_offset()),
    635             (void *)pInputBuffer->m_dataAddress, pInputBuffer->m_bufferSize);
    636         buffer->meta_data()->setInt64(kKeyTime, pInputBuffer->m_timeStampUs);
    637     }
    638     pDecoderContext->mDecoderSource->storeBuffer(buffer);
    639 
    640 cleanUp:
    641     if( M4NO_ERROR == err ) {
    642         ALOGV("VideoEditorAudioDecoder_processInputBuffer no error");
    643     } else {
    644         ALOGV("VideoEditorAudioDecoder_processInputBuffer ERROR 0x%X", err);
    645     }
    646     ALOGV("VideoEditorAudioDecoder_processInputBuffer end");
    647     return err;
    648 }
    649 
    650 M4OSA_ERR VideoEditorAudioDecoder_processOutputBuffer(M4AD_Context pContext,
    651         MediaBuffer* buffer, M4AD_Buffer* pOuputBuffer) {
    652     M4OSA_ERR err = M4NO_ERROR;
    653     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    654     int32_t i32Tmp = 0;
    655     int64_t i64Tmp = 0;
    656     status_t result = OK;
    657 
    658     ALOGV("VideoEditorAudioDecoder_processOutputBuffer begin");
    659     // Input parameters check
    660     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    661     VIDEOEDITOR_CHECK(M4OSA_NULL != buffer, M4ERR_PARAMETER);
    662     VIDEOEDITOR_CHECK(M4OSA_NULL != pOuputBuffer, M4ERR_PARAMETER);
    663 
    664     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    665 
    666     // Process the returned data
    667     if( 0 == buffer->range_length() ) {
    668         // Decoder has no data yet, nothing unusual
    669         goto cleanUp;
    670     }
    671 
    672     pDecoderContext->mNbOutputFrames++;
    673 
    674     if( pDecoderContext->mAudioStreamHandler->m_nbChannels ==
    675         (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) {
    676         // Just copy the PCMs
    677         pOuputBuffer->m_bufferSize = (M4OSA_UInt32)buffer->range_length();
    678         memcpy((void *)pOuputBuffer->m_dataAddress,
    679             (void *)(((M4OSA_MemAddr8)buffer->data())+buffer->range_offset()),
    680             buffer->range_length());
    681     } else if( pDecoderContext->mAudioStreamHandler->m_nbChannels <
    682         (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) {
    683         // The decoder forces stereo output, downsample
    684         pOuputBuffer->m_bufferSize = (M4OSA_UInt32)(buffer->range_length()/2);
    685         M4OSA_Int16* pDataIn  = ((M4OSA_Int16*)buffer->data()) +
    686             buffer->range_offset();
    687         M4OSA_Int16* pDataOut = (M4OSA_Int16*)pOuputBuffer->m_dataAddress;
    688         M4OSA_Int16* pDataEnd = pDataIn + \
    689             (buffer->range_length()/sizeof(M4OSA_Int16));
    690         while( pDataIn < pDataEnd ) {
    691             *pDataOut = *pDataIn;
    692             pDataIn+=2;
    693             pDataOut++;
    694         }
    695     } else {
    696         // The decoder forces mono output, not supported
    697         VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
    698     }
    699 
    700 cleanUp:
    701     // Release the buffer
    702     buffer->release();
    703     if( M4NO_ERROR == err ) {
    704         ALOGV("VideoEditorAudioDecoder_processOutputBuffer no error");
    705     } else {
    706         pOuputBuffer->m_bufferSize = 0;
    707         ALOGV("VideoEditorAudioDecoder_processOutputBuffer ERROR 0x%X", err);
    708     }
    709     ALOGV("VideoEditorAudioDecoder_processOutputBuffer end");
    710     return err;
    711 }
    712 
    713 M4OSA_ERR VideoEditorAudioDecoder_step(M4AD_Context pContext,
    714         M4AD_Buffer* pInputBuffer, M4AD_Buffer* pOutputBuffer,
    715         M4OSA_Bool bJump) {
    716     M4OSA_ERR err = M4NO_ERROR;
    717     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    718     status_t result = OK;
    719     MediaBuffer* outputBuffer = NULL;
    720 
    721     ALOGV("VideoEditorAudioDecoder_step begin");
    722     // Input parameters check
    723     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    724 
    725     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    726     pDecoderContext->mNbInputFrames++;
    727 
    728     // Push the input buffer to the decoder source
    729     err = VideoEditorAudioDecoder_processInputBuffer(pDecoderContext,
    730         pInputBuffer);
    731     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    732 
    733     // Read
    734     result = pDecoderContext->mDecoder->read(&outputBuffer, NULL);
    735     if (INFO_FORMAT_CHANGED == result) {
    736         ALOGV("VideoEditorAudioDecoder_step: Audio decoder \
    737          returned INFO_FORMAT_CHANGED");
    738         CHECK(outputBuffer == NULL);
    739         sp<MetaData> meta = pDecoderContext->mDecoder->getFormat();
    740         int32_t sampleRate, channelCount;
    741 
    742         CHECK(meta->findInt32(kKeySampleRate, &sampleRate));
    743         CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
    744         ALOGV("VideoEditorAudioDecoder_step: samplingFreq = %d", sampleRate);
    745         ALOGV("VideoEditorAudioDecoder_step: channelCnt = %d", channelCount);
    746         pDecoderContext->mAudioStreamHandler->m_samplingFrequency =
    747          (uint32_t)sampleRate;
    748         pDecoderContext->mAudioStreamHandler->m_nbChannels =
    749          (uint32_t)channelCount;
    750         pDecoderContext->mNbOutputChannels = channelCount;
    751 
    752         return M4WAR_INFO_FORMAT_CHANGE;
    753     } else if (ERROR_END_OF_STREAM == result) {
    754         ALOGV("VideoEditorAudioDecoder_step: Audio decoder \
    755          returned ERROR_END_OF_STREAM");
    756         pDecoderContext->readerErrCode = M4WAR_NO_MORE_AU;
    757         return M4WAR_NO_MORE_AU;
    758     } else if (OK != result) {
    759         return M4ERR_STATE;
    760     }
    761 
    762     // Convert the PCM buffer
    763     err = VideoEditorAudioDecoder_processOutputBuffer(pDecoderContext,
    764         outputBuffer, pOutputBuffer);
    765     VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
    766 
    767 cleanUp:
    768     if( M4NO_ERROR == err ) {
    769         ALOGV("VideoEditorAudioDecoder_step no error");
    770     } else {
    771         ALOGV("VideoEditorAudioDecoder_step ERROR 0x%X", err);
    772     }
    773     ALOGV("VideoEditorAudioDecoder_step end");
    774     return err;
    775 }
    776 
    777 M4OSA_ERR VideoEditorAudioDecoder_getVersion(M4_VersionInfo* pVersionInfo) {
    778     M4OSA_ERR err = M4NO_ERROR;
    779 
    780     ALOGV("VideoEditorAudioDecoder_getVersion begin");
    781     // Input parameters check
    782     VIDEOEDITOR_CHECK(M4OSA_NULL != pVersionInfo, M4ERR_PARAMETER);
    783 
    784     pVersionInfo->m_major      = VIDEOEDITOR_AUDIO_DECODER_VERSION_MAJOR;
    785     pVersionInfo->m_minor      = VIDEOEDITOR_AUDIO_DECODER_VERSION_MINOR;
    786     pVersionInfo->m_revision   = VIDEOEDITOR_AUDIO_DECODER_VERSION_REV;
    787     pVersionInfo->m_structSize = sizeof(M4_VersionInfo);
    788 
    789 cleanUp:
    790     if( M4NO_ERROR == err ) {
    791         ALOGV("VideoEditorAudioDecoder_getVersion no error");
    792     } else {
    793         ALOGV("VideoEditorAudioDecoder_getVersion ERROR 0x%X", err);
    794     }
    795     ALOGV("VideoEditorAudioDecoder_getVersion end");
    796     return err;
    797 }
    798 
    799 M4OSA_ERR VideoEditorAudioDecoder_setOption(M4AD_Context pContext,
    800         M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) {
    801 
    802     M4OSA_ERR err = M4NO_ERROR;
    803     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    804 
    805     ALOGV("VideoEditorAudioDecoder_setOption begin 0x%X", optionID);
    806     // Input parameters check
    807     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    808 
    809     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    810 
    811     switch( optionID ) {
    812         case M4AD_kOptionID_UserParam:
    813             ALOGV("VideoEditorAudioDecodersetOption UserParam is not supported");
    814             err = M4ERR_NOT_IMPLEMENTED;
    815             break;
    816 
    817         case M4AD_kOptionID_3gpReaderInterface:
    818             ALOGV("VideoEditorAudioDecodersetOption 3gpReaderInterface");
    819             pDecoderContext->m_pReader =
    820              (M4READER_DataInterface *)optionValue;
    821             break;
    822 
    823         case M4AD_kOptionID_AudioAU:
    824             ALOGV("VideoEditorAudioDecodersetOption AudioAU");
    825             pDecoderContext->m_pNextAccessUnitToDecode =
    826              (M4_AccessUnit *)optionValue;
    827             break;
    828 
    829         default:
    830             ALOGV("VideoEditorAudioDecoder_setOption  unsupported optionId 0x%X",
    831                 optionID);
    832             VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID);
    833             break;
    834     }
    835 
    836 cleanUp:
    837     if( ((M4OSA_UInt32)M4NO_ERROR == err) || ((M4OSA_UInt32)M4ERR_NOT_IMPLEMENTED == err) ) {
    838         ALOGV("VideoEditorAudioDecoder_setOption error 0x%X", err);
    839     } else {
    840         ALOGV("VideoEditorAudioDecoder_setOption ERROR 0x%X", err);
    841     }
    842     ALOGV("VideoEditorAudioDecoder_setOption end");
    843     return err;
    844 }
    845 
    846 M4OSA_ERR VideoEditorAudioDecoder_getOption(M4AD_Context pContext,
    847         M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) {
    848 
    849     M4OSA_ERR err = M4NO_ERROR;
    850     VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
    851 
    852     ALOGV("VideoEditorAudioDecoder_getOption begin: optionID 0x%X", optionID);
    853     // Input parameters check
    854     VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    855 
    856     pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
    857 
    858     switch( optionID ) {
    859 
    860         case M4AD_kOptionID_GetAudioAUErrCode:
    861             *(uint32_t *)optionValue = pDecoderContext->readerErrCode;
    862             break;
    863 
    864         case M4AD_kOptionID_AudioNbChannels:
    865             *(uint32_t *)optionValue =
    866              pDecoderContext->mAudioStreamHandler->m_nbChannels;
    867             break;
    868 
    869         case M4AD_kOptionID_AudioSampFrequency:
    870             *(uint32_t *)optionValue =
    871              pDecoderContext->mAudioStreamHandler->m_samplingFrequency;
    872             break;
    873 
    874         case M4AD_kOptionID_AuCTS:
    875             *(uint32_t *)optionValue = pDecoderContext->timeStampMs;
    876             break;
    877 
    878         default:
    879             ALOGV("VideoEditorAudioDecoder_getOption unsupported optionId 0x%X",
    880                 optionID);
    881             VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID);
    882             break;
    883     }
    884 
    885 cleanUp:
    886     if( M4NO_ERROR == err ) {
    887         ALOGV("VideoEditorAudioDecoder_getOption no error");
    888     } else {
    889         ALOGV("VideoEditorAudioDecoder_getOption ERROR 0x%X", err);
    890     }
    891     ALOGV("VideoEditorAudioDecoder_getOption end");
    892     return err;
    893 }
    894 
    895 M4OSA_ERR VideoEditorAudioDecoder_getInterface(M4AD_Type decoderType,
    896         M4AD_Type* pDecoderType, M4AD_Interface** pDecoderInterface) {
    897 
    898     M4OSA_ERR err = M4NO_ERROR;
    899 
    900     // Input parameters check
    901     VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderType, M4ERR_PARAMETER);
    902     VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderInterface, M4ERR_PARAMETER);
    903 
    904     ALOGV("VideoEditorAudioDecoder_getInterface begin %d 0x%x 0x%x",
    905         decoderType, pDecoderType, pDecoderInterface);
    906 
    907     SAFE_MALLOC(*pDecoderInterface, M4AD_Interface, 1,
    908         "VideoEditorAudioDecoder");
    909 
    910     *pDecoderType = decoderType;
    911 
    912     switch( decoderType ) {
    913         case M4AD_kTypeAMRNB:
    914             (*pDecoderInterface)->m_pFctCreateAudioDec =
    915                 VideoEditorAudioDecoder_create_AMRNB;
    916             break;
    917         case M4AD_kTypeAMRWB:
    918             (*pDecoderInterface)->m_pFctCreateAudioDec =
    919                 VideoEditorAudioDecoder_create_AMRWB;
    920             break;
    921         case M4AD_kTypeAAC:
    922             (*pDecoderInterface)->m_pFctCreateAudioDec =
    923                 VideoEditorAudioDecoder_create_AAC;
    924             break;
    925         case M4AD_kTypeMP3:
    926             (*pDecoderInterface)->m_pFctCreateAudioDec =
    927                 VideoEditorAudioDecoder_create_MP3;
    928             break;
    929         default:
    930             ALOGV("VEAD_getInterface ERROR: unsupported type %d", decoderType);
    931             VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
    932         break;
    933     }
    934     (*pDecoderInterface)->m_pFctDestroyAudioDec   =
    935         VideoEditorAudioDecoder_destroy;
    936     (*pDecoderInterface)->m_pFctResetAudioDec     = M4OSA_NULL;
    937     (*pDecoderInterface)->m_pFctStartAudioDec     = M4OSA_NULL;
    938     (*pDecoderInterface)->m_pFctStepAudioDec      =
    939         VideoEditorAudioDecoder_step;
    940     (*pDecoderInterface)->m_pFctGetVersionAudioDec =
    941         VideoEditorAudioDecoder_getVersion;
    942     (*pDecoderInterface)->m_pFctSetOptionAudioDec =
    943         VideoEditorAudioDecoder_setOption;
    944     (*pDecoderInterface)->m_pFctGetOptionAudioDec =
    945         VideoEditorAudioDecoder_getOption;
    946 
    947 cleanUp:
    948     if( M4NO_ERROR == err ) {
    949         ALOGV("VideoEditorAudioDecoder_getInterface no error");
    950     } else {
    951         *pDecoderInterface = M4OSA_NULL;
    952         ALOGV("VideoEditorAudioDecoder_getInterface ERROR 0x%X", err);
    953     }
    954     ALOGV("VideoEditorAudioDecoder_getInterface end");
    955     return err;
    956 }
    957 
    958 
    959 extern "C" {
    960 
    961 M4OSA_ERR VideoEditorAudioDecoder_getInterface_AAC(M4AD_Type* pDecoderType,
    962         M4AD_Interface** pDecoderInterface) {
    963     ALOGV("TEST: AAC VideoEditorAudioDecoder_getInterface no error");
    964     return VideoEditorAudioDecoder_getInterface(
    965         M4AD_kTypeAAC, pDecoderType, pDecoderInterface);
    966 }
    967 
    968 M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRNB(M4AD_Type* pDecoderType,
    969         M4AD_Interface** pDecoderInterface) {
    970     ALOGV("TEST: AMR VideoEditorAudioDecoder_getInterface no error");
    971     return VideoEditorAudioDecoder_getInterface(
    972         M4AD_kTypeAMRNB, pDecoderType, pDecoderInterface);
    973 }
    974 
    975 M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRWB(M4AD_Type* pDecoderType,
    976         M4AD_Interface** pDecoderInterface) {
    977 
    978     return VideoEditorAudioDecoder_getInterface(
    979         M4AD_kTypeAMRWB, pDecoderType, pDecoderInterface);
    980 }
    981 
    982 M4OSA_ERR VideoEditorAudioDecoder_getInterface_MP3(M4AD_Type* pDecoderType,
    983         M4AD_Interface** pDecoderInterface) {
    984 
    985     return VideoEditorAudioDecoder_getInterface(
    986         M4AD_kTypeMP3, pDecoderType, pDecoderInterface);
    987 }
    988 
    989 }  // extern "C"
    990 
    991 }  // namespace android
    992