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