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