VideoEditorAudioDecoder.cpp revision 32ed3f4dad00f8a65f7e6b38402c70d5341c57eb
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            // Number of bytes per sample
442            pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
443            pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 8000;
444            pDecoderContext->mAudioStreamHandler->m_nbChannels = 1;
445            break;
446
447        case M4AD_kTypeAMRWB:
448            // StageFright parameters
449            mime = MEDIA_MIMETYPE_AUDIO_AMR_WB;
450
451            pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 160;
452            // Number of bytes per sample
453            pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
454            pDecoderContext->mAudioStreamHandler->m_samplingFrequency = 16000;
455            pDecoderContext->mAudioStreamHandler->m_nbChannels = 1;
456            break;
457
458        case M4AD_kTypeAAC:
459            // Reject ADTS & ADIF (or any incorrect type)
460            VIDEOEDITOR_CHECK(M4DA_StreamTypeAudioAac ==
461                pDecoderContext->mAudioStreamHandler->\
462                m_basicProperties.m_streamType,M4ERR_PARAMETER);
463
464            // StageFright parameters
465            mime = MEDIA_MIMETYPE_AUDIO_AAC;
466
467            decoderMetaData->setData(kKeyESDS, kTypeESDS,
468                pStreamHandler->m_basicProperties.m_pESDSInfo,
469                pStreamHandler->m_basicProperties.m_ESDSInfoSize);
470
471            // Engine parameters
472            // Retrieve sampling frequency and number of channels from the DSI
473            err = VideoEditorAudioDecoder_parse_AAC_DSI(
474                (M4OSA_Int8*)pStreamHandler->m_basicProperties.\
475                    m_pDecoderSpecificInfo,
476                pStreamHandler->m_basicProperties.m_decoderSpecificInfoSize,
477                &aacProperties);
478
479            VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
480            pDecoderContext->mAudioStreamHandler->m_byteFrameLength = 1024;
481            // Number of bytes per sample
482            pDecoderContext->mAudioStreamHandler->m_byteSampleSize = 2;
483            pDecoderContext->mAudioStreamHandler->m_samplingFrequency =
484                aacProperties.aSampFreq;
485            pDecoderContext->mAudioStreamHandler->m_nbChannels =
486                aacProperties.aNumChan;
487
488            // Copy the stream properties into userdata
489            if( M4OSA_NULL != pUserData ) {
490                memcpy((void *)pUserData,
491                    (void *)&aacProperties,
492                    sizeof(AAC_DEC_STREAM_PROPS));
493            }
494            break;
495
496        case M4AD_kTypeMP3:
497            // StageFright parameters
498            mime = MEDIA_MIMETYPE_AUDIO_MPEG;
499            break;
500
501        default:
502            VIDEOEDITOR_CHECK(!"AudioDecoder_open : incorrect input format",
503                M4ERR_STATE);
504            break;
505    }
506    decoderMetaData->setCString(kKeyMIMEType, mime);
507    decoderMetaData->setInt32(kKeySampleRate,
508        (int32_t)pDecoderContext->mAudioStreamHandler->m_samplingFrequency);
509    decoderMetaData->setInt32(kKeyChannelCount,
510        pDecoderContext->mAudioStreamHandler->m_nbChannels);
511    decoderMetaData->setInt64(kKeyDuration,
512        (int64_t)pDecoderContext->mAudioStreamHandler->\
513        m_basicProperties.m_duration);
514
515    // Create the decoder source
516    pDecoderContext->mDecoderSource = VideoEditorAudioDecoderSource::Create(
517        decoderMetaData);
518    VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoderSource.get(),
519        M4ERR_STATE);
520
521    // Connect to the OMX client
522    result = pDecoderContext->mClient.connect();
523    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
524
525    // Create the OMX codec
526#ifdef VIDEOEDITOR_FORCECODEC
527    codecFlags |= OMXCodec::VIDEOEDITOR_FORCECODEC;
528#endif /* VIDEOEDITOR_FORCECODEC */
529
530    pDecoderContext->mDecoder = OMXCodec::Create(pDecoderContext->\
531        mClient.interface(),
532        decoderMetaData, false, pDecoderContext->mDecoderSource, NULL,
533            codecFlags);
534    VIDEOEDITOR_CHECK(NULL != pDecoderContext->mDecoder.get(), M4ERR_STATE);
535
536    // Get the output channels, the decoder might overwrite the input metadata
537    pDecoderContext->mDecoder->getFormat()->findInt32(kKeyChannelCount,
538        &pDecoderContext->mNbOutputChannels);
539    LOGV("VideoEditorAudioDecoder_create : output chan %d",
540        pDecoderContext->mNbOutputChannels);
541
542    // Start the decoder
543    result = pDecoderContext->mDecoder->start();
544    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
545
546    *pContext = pDecoderContext;
547    LOGV("VideoEditorAudioDecoder_create : DONE");
548
549cleanUp:
550    if( M4NO_ERROR == err ) {
551        LOGV("VideoEditorAudioDecoder_create no error");
552    } else {
553        VideoEditorAudioDecoder_destroy(pDecoderContext);
554        *pContext = M4OSA_NULL;
555        LOGV("VideoEditorAudioDecoder_create ERROR 0x%X", err);
556    }
557    return err;
558}
559
560M4OSA_ERR VideoEditorAudioDecoder_create_AAC(M4AD_Context* pContext,
561        M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
562
563    return VideoEditorAudioDecoder_create(
564        M4AD_kTypeAAC, pContext, pStreamHandler,pUserData);
565}
566
567
568M4OSA_ERR VideoEditorAudioDecoder_create_AMRNB(M4AD_Context* pContext,
569        M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
570
571    return VideoEditorAudioDecoder_create(
572        M4AD_kTypeAMRNB, pContext, pStreamHandler, pUserData);
573}
574
575
576M4OSA_ERR VideoEditorAudioDecoder_create_AMRWB(M4AD_Context* pContext,
577        M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
578
579    return VideoEditorAudioDecoder_create(
580        M4AD_kTypeAMRWB, pContext, pStreamHandler, pUserData);
581}
582
583
584M4OSA_ERR VideoEditorAudioDecoder_create_MP3(M4AD_Context* pContext,
585        M4_AudioStreamHandler* pStreamHandler, void* pUserData) {
586
587    return VideoEditorAudioDecoder_create(
588        M4AD_kTypeMP3, pContext, pStreamHandler, pUserData);
589}
590
591M4OSA_ERR VideoEditorAudioDecoder_processInputBuffer(
592        M4AD_Context pContext, M4AD_Buffer* pInputBuffer) {
593    M4OSA_ERR err = M4NO_ERROR;
594    VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
595    MediaBuffer* buffer = NULL;
596    int32_t nbBuffer = 0;
597
598    LOGV("VideoEditorAudioDecoder_processInputBuffer begin");
599    // Input parameters check
600    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
601
602
603    pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
604
605    if( M4OSA_NULL != pInputBuffer ) {
606        buffer = new MediaBuffer((size_t)pInputBuffer->m_bufferSize);
607        memcpy((void *)((M4OSA_Int8*)buffer->data() + buffer->range_offset()),
608            (void *)pInputBuffer->m_dataAddress, pInputBuffer->m_bufferSize);
609    }
610    nbBuffer = pDecoderContext->mDecoderSource->storeBuffer(buffer);
611
612cleanUp:
613    if( M4NO_ERROR == err ) {
614        LOGV("VideoEditorAudioDecoder_processInputBuffer no error");
615    } else {
616        LOGV("VideoEditorAudioDecoder_processInputBuffer ERROR 0x%X", err);
617    }
618    LOGV("VideoEditorAudioDecoder_processInputBuffer end");
619    return err;
620}
621
622M4OSA_ERR VideoEditorAudioDecoder_processOutputBuffer(M4AD_Context pContext,
623        MediaBuffer* buffer, M4AD_Buffer* pOuputBuffer) {
624    M4OSA_ERR err = M4NO_ERROR;
625    VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
626    int32_t i32Tmp = 0;
627    int64_t i64Tmp = 0;
628    status_t result = OK;
629
630    LOGV("VideoEditorAudioDecoder_processOutputBuffer begin");
631    // Input parameters check
632    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
633    VIDEOEDITOR_CHECK(M4OSA_NULL != buffer, M4ERR_PARAMETER);
634    VIDEOEDITOR_CHECK(M4OSA_NULL != pOuputBuffer, M4ERR_PARAMETER);
635
636    pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
637
638    // Process the returned data
639    if( 0 == buffer->range_length() ) {
640        // Decoder has no data yet, nothing unusual
641        goto cleanUp;
642    }
643
644    pDecoderContext->mNbOutputFrames++;
645
646    if( pDecoderContext->mAudioStreamHandler->m_nbChannels ==
647        (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) {
648        // Just copy the PCMs
649        pOuputBuffer->m_bufferSize = (M4OSA_UInt32)buffer->range_length();
650        memcpy((void *)pOuputBuffer->m_dataAddress,
651            (void *)(((M4OSA_MemAddr8)buffer->data())+buffer->range_offset()),
652            buffer->range_length());
653    } else if( pDecoderContext->mAudioStreamHandler->m_nbChannels <
654        (M4OSA_UInt32)pDecoderContext->mNbOutputChannels ) {
655        // The decoder forces stereo output, downsample
656        pOuputBuffer->m_bufferSize = (M4OSA_UInt32)(buffer->range_length()/2);
657        M4OSA_Int16* pDataIn  = ((M4OSA_Int16*)buffer->data()) +
658            buffer->range_offset();
659        M4OSA_Int16* pDataOut = (M4OSA_Int16*)pOuputBuffer->m_dataAddress;
660        M4OSA_Int16* pDataEnd = pDataIn + \
661            (buffer->range_length()/sizeof(M4OSA_Int16));
662        while( pDataIn < pDataEnd ) {
663            *pDataOut = *pDataIn;
664            pDataIn+=2;
665            pDataOut++;
666        }
667    } else {
668        // The decoder forces mono output, not supported
669        VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
670    }
671
672cleanUp:
673    // Release the buffer
674    buffer->release();
675    if( M4NO_ERROR == err ) {
676        LOGV("VideoEditorAudioDecoder_processOutputBuffer no error");
677    } else {
678        pOuputBuffer->m_bufferSize = 0;
679        LOGV("VideoEditorAudioDecoder_processOutputBuffer ERROR 0x%X", err);
680    }
681    LOGV("VideoEditorAudioDecoder_processOutputBuffer end");
682    return err;
683}
684
685M4OSA_ERR VideoEditorAudioDecoder_step(M4AD_Context pContext,
686        M4AD_Buffer* pInputBuffer, M4AD_Buffer* pOutputBuffer,
687        M4OSA_Bool bJump) {
688    M4OSA_ERR err = M4NO_ERROR;
689    VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
690    status_t result = OK;
691    MediaBuffer* outputBuffer = NULL;
692
693    LOGV("VideoEditorAudioDecoder_step begin");
694    // Input parameters check
695    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
696
697    pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
698    pDecoderContext->mNbInputFrames++;
699
700    // Push the input buffer to the decoder source
701    err = VideoEditorAudioDecoder_processInputBuffer(pDecoderContext,
702        pInputBuffer);
703    VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
704
705    // Read
706    result = pDecoderContext->mDecoder->read(&outputBuffer, NULL);
707    if(OK != result) {
708        LOGE("VideoEditorAudioDecoder_step  result = %d",result);
709
710    }
711    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
712
713    // Convert the PCM buffer
714    err = VideoEditorAudioDecoder_processOutputBuffer(pDecoderContext,
715        outputBuffer, pOutputBuffer);
716    VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);
717
718cleanUp:
719    if( M4NO_ERROR == err ) {
720        LOGV("VideoEditorAudioDecoder_step no error");
721    } else {
722        LOGV("VideoEditorAudioDecoder_step ERROR 0x%X", err);
723    }
724    LOGV("VideoEditorAudioDecoder_step end");
725    return err;
726}
727
728M4OSA_ERR VideoEditorAudioDecoder_getVersion(M4_VersionInfo* pVersionInfo) {
729    M4OSA_ERR err = M4NO_ERROR;
730
731    LOGV("VideoEditorAudioDecoder_getVersion begin");
732    // Input parameters check
733    VIDEOEDITOR_CHECK(M4OSA_NULL != pVersionInfo, M4ERR_PARAMETER);
734
735    pVersionInfo->m_major      = VIDEOEDITOR_AUDIO_DECODER_VERSION_MAJOR;
736    pVersionInfo->m_minor      = VIDEOEDITOR_AUDIO_DECODER_VERSION_MINOR;
737    pVersionInfo->m_revision   = VIDEOEDITOR_AUDIO_DECODER_VERSION_REV;
738    pVersionInfo->m_structSize = sizeof(M4_VersionInfo);
739
740cleanUp:
741    if( M4NO_ERROR == err ) {
742        LOGV("VideoEditorAudioDecoder_getVersion no error");
743    } else {
744        LOGV("VideoEditorAudioDecoder_getVersion ERROR 0x%X", err);
745    }
746    LOGV("VideoEditorAudioDecoder_getVersion end");
747    return err;
748}
749
750M4OSA_ERR VideoEditorAudioDecoder_setOption(M4AD_Context pContext,
751        M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) {
752
753    M4OSA_ERR err = M4NO_ERROR;
754    VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
755
756    LOGV("VideoEditorAudioDecoder_setOption begin 0x%X", optionID);
757    // Input parameters check
758    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
759
760    pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
761
762    switch( optionID ) {
763        case M4AD_kOptionID_UserParam:
764            LOGV("VideoEditorAudioDecodersetOption UserParam is not supported");
765            err = M4ERR_NOT_IMPLEMENTED;
766            break;
767        default:
768            LOGV("VideoEditorAudioDecoder_setOption  unsupported optionId 0x%X",
769                optionID);
770            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID);
771            break;
772    }
773
774cleanUp:
775    if( ((M4OSA_UInt32)M4NO_ERROR == err) || ((M4OSA_UInt32)M4ERR_NOT_IMPLEMENTED == err) ) {
776        LOGV("VideoEditorAudioDecoder_setOption error 0x%X", err);
777    } else {
778        LOGV("VideoEditorAudioDecoder_setOption ERROR 0x%X", err);
779    }
780    LOGV("VideoEditorAudioDecoder_setOption end");
781    return err;
782}
783
784M4OSA_ERR VideoEditorAudioDecoder_getOption(M4AD_Context pContext,
785        M4OSA_UInt32 optionID, M4OSA_DataOption optionValue) {
786
787    M4OSA_ERR err = M4NO_ERROR;
788    VideoEditorAudioDecoder_Context* pDecoderContext = M4OSA_NULL;
789
790    LOGV("VideoEditorAudioDecoder_getOption begin: optionID 0x%X", optionID);
791    // Input parameters check
792    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
793
794    pDecoderContext = (VideoEditorAudioDecoder_Context*)pContext;
795
796    switch( optionID ) {
797        default:
798            LOGV("VideoEditorAudioDecoder_getOption unsupported optionId 0x%X",
799                optionID);
800            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID);
801            break;
802    }
803
804cleanUp:
805    if( M4NO_ERROR == err ) {
806        LOGV("VideoEditorAudioDecoder_getOption no error");
807    } else {
808        LOGV("VideoEditorAudioDecoder_getOption ERROR 0x%X", err);
809    }
810    LOGV("VideoEditorAudioDecoder_getOption end");
811    return err;
812}
813
814M4OSA_ERR VideoEditorAudioDecoder_getInterface(M4AD_Type decoderType,
815        M4AD_Type* pDecoderType, M4AD_Interface** pDecoderInterface) {
816
817    M4OSA_ERR err = M4NO_ERROR;
818
819    // Input parameters check
820    VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderType, M4ERR_PARAMETER);
821    VIDEOEDITOR_CHECK(M4OSA_NULL != pDecoderInterface, M4ERR_PARAMETER);
822
823    LOGV("VideoEditorAudioDecoder_getInterface begin %d 0x%x 0x%x",
824        decoderType, pDecoderType, pDecoderInterface);
825
826    SAFE_MALLOC(*pDecoderInterface, M4AD_Interface, 1,
827        "VideoEditorAudioDecoder");
828
829    *pDecoderType = decoderType;
830
831    switch( decoderType ) {
832        case M4AD_kTypeAMRNB:
833            (*pDecoderInterface)->m_pFctCreateAudioDec =
834                VideoEditorAudioDecoder_create_AMRNB;
835            break;
836        case M4AD_kTypeAMRWB:
837            (*pDecoderInterface)->m_pFctCreateAudioDec =
838                VideoEditorAudioDecoder_create_AMRWB;
839            break;
840        case M4AD_kTypeAAC:
841            (*pDecoderInterface)->m_pFctCreateAudioDec =
842                VideoEditorAudioDecoder_create_AAC;
843            break;
844        case M4AD_kTypeMP3:
845            (*pDecoderInterface)->m_pFctCreateAudioDec =
846                VideoEditorAudioDecoder_create_MP3;
847            break;
848        default:
849            LOGV("VEAD_getInterface ERROR: unsupported type %d", decoderType);
850            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
851        break;
852    }
853    (*pDecoderInterface)->m_pFctDestroyAudioDec   =
854        VideoEditorAudioDecoder_destroy;
855    (*pDecoderInterface)->m_pFctResetAudioDec     = M4OSA_NULL;
856    (*pDecoderInterface)->m_pFctStartAudioDec     = M4OSA_NULL;
857    (*pDecoderInterface)->m_pFctStepAudioDec      =
858        VideoEditorAudioDecoder_step;
859    (*pDecoderInterface)->m_pFctGetVersionAudioDec =
860        VideoEditorAudioDecoder_getVersion;
861    (*pDecoderInterface)->m_pFctSetOptionAudioDec =
862        VideoEditorAudioDecoder_setOption;
863    (*pDecoderInterface)->m_pFctGetOptionAudioDec =
864        VideoEditorAudioDecoder_getOption;
865
866cleanUp:
867    if( M4NO_ERROR == err ) {
868        LOGV("VideoEditorAudioDecoder_getInterface no error");
869    } else {
870        *pDecoderInterface = M4OSA_NULL;
871        LOGV("VideoEditorAudioDecoder_getInterface ERROR 0x%X", err);
872    }
873    LOGV("VideoEditorAudioDecoder_getInterface end");
874    return err;
875}
876
877
878extern "C" {
879
880M4OSA_ERR VideoEditorAudioDecoder_getInterface_AAC(M4AD_Type* pDecoderType,
881        M4AD_Interface** pDecoderInterface) {
882    LOGV("TEST: AAC VideoEditorAudioDecoder_getInterface no error");
883    return VideoEditorAudioDecoder_getInterface(
884        M4AD_kTypeAAC, pDecoderType, pDecoderInterface);
885}
886
887M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRNB(M4AD_Type* pDecoderType,
888        M4AD_Interface** pDecoderInterface) {
889    LOGV("TEST: AMR VideoEditorAudioDecoder_getInterface no error");
890    return VideoEditorAudioDecoder_getInterface(
891        M4AD_kTypeAMRNB, pDecoderType, pDecoderInterface);
892}
893
894M4OSA_ERR VideoEditorAudioDecoder_getInterface_AMRWB(M4AD_Type* pDecoderType,
895        M4AD_Interface** pDecoderInterface) {
896
897    return VideoEditorAudioDecoder_getInterface(
898        M4AD_kTypeAMRWB, pDecoderType, pDecoderInterface);
899}
900
901M4OSA_ERR VideoEditorAudioDecoder_getInterface_MP3(M4AD_Type* pDecoderType,
902        M4AD_Interface** pDecoderInterface) {
903
904    return VideoEditorAudioDecoder_getInterface(
905        M4AD_kTypeMP3, pDecoderType, pDecoderInterface);
906}
907
908}  // extern "C"
909
910}  // namespace android
911