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