summaryrefslogtreecommitdiffstats
path: root/libvideoeditor/vss/stagefrightshells/src/VideoEditorAudioEncoder.cpp
blob: a91f3ee5d71165f874c48b3a4eb405bbd5221d4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
*************************************************************************
* @file   VideoEditorAudioEncoder.cpp
* @brief  StageFright shell Audio Encoder
*************************************************************************
*/

#define LOG_NDEBUG 1
#define LOG_TAG "VIDEOEDITOR_AUDIOENCODER"

#include "M4OSA_Debug.h"
#include "VideoEditorAudioEncoder.h"
#include "VideoEditorUtils.h"

#include "utils/Log.h"
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/OMXClient.h>
#include <media/stagefright/OMXCodec.h>

/*** DEFINITIONS ***/
// Force using software encoder as engine does not support prefetch
#define VIDEOEDITOR_FORCECODEC kSoftwareCodecsOnly

namespace android {
struct VideoEditorAudioEncoderSource : public MediaSource {
    public:
        static sp<VideoEditorAudioEncoderSource> Create(
            const sp<MetaData> &format);
        virtual status_t start(MetaData *params = NULL);
        virtual status_t stop();
        virtual sp<MetaData> getFormat();
        virtual status_t read(MediaBuffer **buffer,
        const ReadOptions *options = NULL);
        virtual int32_t storeBuffer(MediaBuffer *buffer);

    protected:
        virtual ~VideoEditorAudioEncoderSource();

    private:
        struct MediaBufferChain {
            MediaBuffer* buffer;
            MediaBufferChain* nextLink;
        };
        enum State {
            CREATED,
            STARTED,
            ERROR
        };

        MediaBufferChain* mFirstBufferLink;
        MediaBufferChain* mLastBufferLink;
        int32_t mNbBuffer;
        State mState;
        sp<MetaData> mEncFormat;

        VideoEditorAudioEncoderSource(const sp<MetaData> &format);

        // Don't call me.
        VideoEditorAudioEncoderSource(const VideoEditorAudioEncoderSource&);
        VideoEditorAudioEncoderSource& operator=(
            const VideoEditorAudioEncoderSource&);
};

sp<VideoEditorAudioEncoderSource> VideoEditorAudioEncoderSource::Create(
    const sp<MetaData> &format) {

    ALOGV("VideoEditorAudioEncoderSource::Create");
    sp<VideoEditorAudioEncoderSource> aSource =
        new VideoEditorAudioEncoderSource(format);

    return aSource;
}

VideoEditorAudioEncoderSource::VideoEditorAudioEncoderSource(
    const sp<MetaData> &format):
        mFirstBufferLink(NULL),
        mLastBufferLink(NULL),
        mNbBuffer(0),
        mState(CREATED),
        mEncFormat(format) {
    ALOGV("VideoEditorAudioEncoderSource::VideoEditorAudioEncoderSource");
}


VideoEditorAudioEncoderSource::~VideoEditorAudioEncoderSource() {
    ALOGV("VideoEditorAudioEncoderSource::~VideoEditorAudioEncoderSource");

    if( STARTED == mState ) {
        stop();
    }
}

status_t VideoEditorAudioEncoderSource::start(MetaData *meta) {
    status_t err = OK;

    ALOGV("VideoEditorAudioEncoderSource::start");

    if( CREATED != mState ) {
        ALOGV("VideoEditorAudioEncoderSource::start ERROR : invalid state %d",
            mState);
        return UNKNOWN_ERROR;
    }

    mState = STARTED;

cleanUp:
    ALOGV("VideoEditorAudioEncoderSource::start END (0x%x)", err);
    return err;
}

status_t VideoEditorAudioEncoderSource::stop() {
    status_t err = OK;

    ALOGV("VideoEditorAudioEncoderSource::stop");

    if( STARTED != mState ) {
        ALOGV("VideoEditorAudioEncoderSource::stop ERROR: invalid state %d",
            mState);
        return UNKNOWN_ERROR;
    }

    int32_t i = 0;
    MediaBufferChain* tmpLink = NULL;
    while( mFirstBufferLink ) {
        i++;
        tmpLink = mFirstBufferLink;
        mFirstBufferLink = mFirstBufferLink->nextLink;
        delete tmpLink;
    }
    ALOGV("VideoEditorAudioEncoderSource::stop : %d buffer remained", i);
    mFirstBufferLink = NULL;
    mLastBufferLink = NULL;

    mState = CREATED;

    ALOGV("VideoEditorAudioEncoderSource::stop END (0x%x)", err);
    return err;
}

sp<MetaData> VideoEditorAudioEncoderSource::getFormat() {
    ALOGV("VideoEditorAudioEncoderSource::getFormat");
    return mEncFormat;
}

status_t VideoEditorAudioEncoderSource::read(MediaBuffer **buffer,
        const ReadOptions *options) {
    MediaSource::ReadOptions readOptions;
    status_t err = OK;
    MediaBufferChain* tmpLink = NULL;

    ALOGV("VideoEditorAudioEncoderSource::read");

    if ( STARTED != mState ) {
        ALOGV("VideoEditorAudioEncoderSource::read ERROR : invalid state %d",
            mState);
        return UNKNOWN_ERROR;
    }

    if( NULL == mFirstBufferLink ) {
        *buffer = NULL;
        ALOGV("VideoEditorAudioEncoderSource::read : EOS");
        return ERROR_END_OF_STREAM;
    }
    *buffer = mFirstBufferLink->buffer;

    tmpLink = mFirstBufferLink;
    mFirstBufferLink = mFirstBufferLink->nextLink;
    if( NULL == mFirstBufferLink ) {
        mLastBufferLink = NULL;
    }
    delete tmpLink;
    mNbBuffer--;

    ALOGV("VideoEditorAudioEncoderSource::read END (0x%x)", err);
    return err;
}

int32_t VideoEditorAudioEncoderSource::storeBuffer(MediaBuffer *buffer) {
    status_t err = OK;

    ALOGV("VideoEditorAudioEncoderSource::storeBuffer");

    MediaBufferChain* newLink = new MediaBufferChain;
    newLink->buffer = buffer;
    newLink->nextLink = NULL;
    if( NULL != mLastBufferLink ) {
        mLastBufferLink->nextLink = newLink;
    } else {
        mFirstBufferLink = newLink;
    }
    mLastBufferLink = newLink;
    mNbBuffer++;

    ALOGV("VideoEditorAudioEncoderSource::storeBuffer END");
    return mNbBuffer;
}

/********************
 * ENGINE INTERFACE *
 ********************/
/**
 ******************************************************************************
 * structure VideoEditorAudioEncoder_Context
 * @brief    This structure defines the context of the StageFright audio
 *           encoder shell
 ******************************************************************************
*/
typedef struct {
    M4ENCODER_AudioFormat             mFormat;
    M4ENCODER_AudioParams*            mCodecParams;
    M4ENCODER_AudioDecSpecificInfo    mDSI;
    sp<VideoEditorAudioEncoderSource> mEncoderSource;
    OMXClient                         mClient;
    sp<MediaSource>                   mEncoder;
    uint32_t                          mNbInputFrames;
    uint32_t                          mNbOutputFrames;
    int64_t                           mFirstOutputCts;
    int64_t                           mLastOutputCts;
} VideoEditorAudioEncoder_Context;

M4OSA_ERR VideoEditorAudioEncoder_cleanup(M4OSA_Context pContext) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;

    ALOGV("VideoEditorAudioEncoder_cleanup begin");
    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;

    SAFE_FREE(pEncoderContext->mDSI.pInfo);
    SAFE_FREE(pEncoderContext);
    pContext = M4OSA_NULL;

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_cleanup no error");
    } else {
        ALOGV("VideoEditorAudioEncoder_cleanup ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_cleanup end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_init(M4ENCODER_AudioFormat format,
        M4OSA_Context* pContext, M4OSA_Void* pUserData) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;

    ALOGV(" VideoEditorAudioEncoder_init begin: format %d", format);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);

    SAFE_MALLOC(pEncoderContext, VideoEditorAudioEncoder_Context, 1,
        "VideoEditorAudioEncoder");
    pEncoderContext->mFormat = format;

    *pContext = pEncoderContext;

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_init no error");
    } else {
        VideoEditorAudioEncoder_cleanup(pEncoderContext);
        *pContext = M4OSA_NULL;
        ALOGV("VideoEditorAudioEncoder_init ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_init end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_init_AAC(M4OSA_Context* pContext,
        M4OSA_Void* pUserData) {
    return VideoEditorAudioEncoder_init(M4ENCODER_kAAC, pContext, pUserData);
}

M4OSA_ERR VideoEditorAudioEncoder_init_AMRNB(M4OSA_Context* pContext,
        M4OSA_Void* pUserData) {
    return VideoEditorAudioEncoder_init(M4ENCODER_kAMRNB, pContext, pUserData);
}

M4OSA_ERR VideoEditorAudioEncoder_init_MP3(M4OSA_Context* pContext,
        M4OSA_Void* pUserData) {
    return VideoEditorAudioEncoder_init(M4ENCODER_kMP3, pContext, pUserData);
}

M4OSA_ERR VideoEditorAudioEncoder_close(M4OSA_Context pContext) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;

    ALOGV("VideoEditorAudioEncoder_close begin");

    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;

    SAFE_FREE(pEncoderContext->mCodecParams);

    pEncoderContext->mEncoder->stop();
    pEncoderContext->mEncoder.clear();
    pEncoderContext->mClient.disconnect();
    pEncoderContext->mEncoderSource.clear();

    ALOGV("AudioEncoder_close:IN %d frames,OUT %d frames from %lld to %lld",
        pEncoderContext->mNbInputFrames,
        pEncoderContext->mNbOutputFrames, pEncoderContext->mFirstOutputCts,
        pEncoderContext->mLastOutputCts);

    if( pEncoderContext->mNbInputFrames != pEncoderContext->mNbInputFrames ) {
        ALOGV("VideoEditorAudioEncoder_close:some frames were not encoded %d %d",
            pEncoderContext->mNbInputFrames, pEncoderContext->mNbInputFrames);
    }

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_close no error");
    } else {
        ALOGV("VideoEditorAudioEncoder_close ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_close begin end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_open(M4OSA_Context pContext,
        M4ENCODER_AudioParams *pParams, M4ENCODER_AudioDecSpecificInfo *pDSI,
        M4OSA_Context pGrabberContext) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;
    status_t result = OK;
    sp<MetaData> encoderMetadata = NULL;
    const char* mime = NULL;
    int32_t iNbChannel = 0;
    uint32_t codecFlags = 0;

    ALOGV("VideoEditorAudioEncoder_open begin");

    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pParams,  M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pDSI,     M4ERR_PARAMETER);

    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;
    pDSI->pInfo = M4OSA_NULL;
    pDSI->infoSize = 0;

    pEncoderContext->mNbInputFrames  = 0;
    pEncoderContext->mNbOutputFrames = 0;
    pEncoderContext->mFirstOutputCts = -1;
    pEncoderContext->mLastOutputCts  = -1;

    // Allocate & initialize the encoding parameters
    ALOGV("VideoEditorAudioEncoder_open : params F=%d CN=%d BR=%d F=%d",
        pParams->Frequency, pParams->ChannelNum, pParams->Bitrate,
        pParams->Format);
    SAFE_MALLOC(pEncoderContext->mCodecParams, M4ENCODER_AudioParams, 1,
        "VIDEOEDITOR CodecParams");
    pEncoderContext->mCodecParams->Frequency  = pParams->Frequency;
    pEncoderContext->mCodecParams->ChannelNum = pParams->ChannelNum;
    pEncoderContext->mCodecParams->Bitrate    = pParams->Bitrate;
    pEncoderContext->mCodecParams->Format     = pParams->Format;

    // Check output format consistency
    VIDEOEDITOR_CHECK(pEncoderContext->mCodecParams->Format ==
        pEncoderContext->mFormat, M4ERR_PARAMETER);

    /**
     * StageFright graph building
     */
    // Create the meta data for the encoder
    encoderMetadata = new MetaData;
    switch( pEncoderContext->mCodecParams->Format ) {
        case M4ENCODER_kAAC:
        {
            mime = MEDIA_MIMETYPE_AUDIO_AAC;
            break;
        }
        case M4ENCODER_kAMRNB:
        {
            mime = MEDIA_MIMETYPE_AUDIO_AMR_NB;
            break;
        }
        default:
        {
            VIDEOEDITOR_CHECK(!"AudioEncoder_open : incorrect input format",
            M4ERR_PARAMETER);
            break;
        }
    }
    encoderMetadata->setCString(kKeyMIMEType, mime);
    encoderMetadata->setInt32(kKeySampleRate,
        (int32_t)pEncoderContext->mCodecParams->Frequency);
    encoderMetadata->setInt32(kKeyBitRate,
        (int32_t)pEncoderContext->mCodecParams->Bitrate);

    switch( pEncoderContext->mCodecParams->ChannelNum ) {
        case M4ENCODER_kMono:
        {
            iNbChannel = 1;
            break;
        }
        case M4ENCODER_kStereo:
        {
            iNbChannel = 2;
            break;
        }
        default:
        {
            VIDEOEDITOR_CHECK(!"AudioEncoder_open : incorrect channel number",
                M4ERR_STATE);
            break;
        }
    }
    encoderMetadata->setInt32(kKeyChannelCount, iNbChannel);

    // Create the encoder source
    pEncoderContext->mEncoderSource = VideoEditorAudioEncoderSource::Create(
        encoderMetadata);
    VIDEOEDITOR_CHECK(NULL != pEncoderContext->mEncoderSource.get(),
        M4ERR_STATE);

    // Connect to the OMX client
    result = pEncoderContext->mClient.connect();
    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);

    // Create the OMX codec
#ifdef VIDEOEDITOR_FORCECODEC
    codecFlags |= OMXCodec::VIDEOEDITOR_FORCECODEC;
#endif /* VIDEOEDITOR_FORCECODEC */
    // FIXME:
    // We are moving away to use software AACEncoder and instead use OMX-based
    // software AAC audio encoder. We want to use AACEncoder for now. After we
    // fix the interface issue with the OMX-based AAC audio encoder, we should
    // then set the component name back to NULL to allow the system to pick up
    // the right AAC audio encoder.
    pEncoderContext->mEncoder = OMXCodec::Create(
            pEncoderContext->mClient.interface(), encoderMetadata, true,
            pEncoderContext->mEncoderSource, "AACEncoder" /* component name */,
            codecFlags);
    VIDEOEDITOR_CHECK(NULL != pEncoderContext->mEncoder.get(), M4ERR_STATE);

    // Start the graph
    result = pEncoderContext->mEncoder->start();
    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);

    // Get AAC DSI, this code can only work with software encoder
    if( M4ENCODER_kAAC == pEncoderContext->mCodecParams->Format ) {
        int32_t      isCodecConfig = 0;
        MediaBuffer* buffer        = NULL;

        // Read once to get the DSI
        result = pEncoderContext->mEncoder->read(&buffer, NULL);
        VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);
        VIDEOEDITOR_CHECK(buffer->meta_data()->findInt32(kKeyIsCodecConfig,
            &isCodecConfig) && isCodecConfig, M4ERR_STATE);

        // Save the DSI
        pEncoderContext->mDSI.infoSize = (M4OSA_UInt32)buffer->range_length();
        SAFE_MALLOC(pEncoderContext->mDSI.pInfo, M4OSA_Int8,
            pEncoderContext->mDSI.infoSize, "Encoder header");

        memcpy((void *)pEncoderContext->mDSI.pInfo,
            (void *)((M4OSA_MemAddr8)(buffer->data())+buffer->range_offset()),
            pEncoderContext->mDSI.infoSize);

        buffer->release();
        *pDSI = pEncoderContext->mDSI;
    }
    ALOGV("VideoEditorAudioEncoder_open : DONE");

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_open no error");
    } else {
        VideoEditorAudioEncoder_close(pEncoderContext);
        ALOGV("VideoEditorAudioEncoder_open ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_open end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_processInputBuffer(M4OSA_Context pContext,
        M4ENCODER_AudioBuffer* pInBuffer) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;
    M4OSA_Int8* pData = M4OSA_NULL;
    MediaBuffer* buffer = NULL;
    int32_t nbBuffer = 0;

    ALOGV("VideoEditorAudioEncoder_processInputBuffer begin");
    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);

    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;

    switch( pEncoderContext->mCodecParams->ChannelNum ) {
        case M4ENCODER_kMono:
        case M4ENCODER_kStereo:
            // Let the MediaBuffer own the data so we don't have to free it
            buffer = new MediaBuffer((size_t)pInBuffer->pTableBufferSize[0]);
            pData = (M4OSA_Int8*)buffer->data() + buffer->range_offset();
            memcpy((void *)pData, (void *)pInBuffer->pTableBuffer[0],
                pInBuffer->pTableBufferSize[0]);
            break;
        default:
            ALOGV("VEAE_processInputBuffer unsupported channel configuration %d",
                pEncoderContext->mCodecParams->ChannelNum);
            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
            break;
    }

    ALOGV("VideoEditorAudioEncoder_processInputBuffer : store %d bytes",
        buffer->range_length());
    // Push the buffer to the source
    nbBuffer = pEncoderContext->mEncoderSource->storeBuffer(buffer);

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_processInputBuffer no error");
    } else {
        if( NULL != buffer ) {
            buffer->release();
        }
        ALOGV("VideoEditorAudioEncoder_processInputBuffer ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_processInputBuffer end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_processOutputBuffer(M4OSA_Context pContext,
        MediaBuffer* buffer, M4ENCODER_AudioBuffer* pOutBuffer) {

    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;
    M4OSA_UInt32 Cts = 0;
    int32_t i32Tmp = 0;
    int64_t i64Tmp = 0;
    status_t result = OK;

    ALOGV("VideoEditorAudioEncoder_processOutputBuffer begin");
    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext,   M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != buffer,     M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pOutBuffer, M4ERR_PARAMETER);

    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;

    // Process the returned AU
    if( 0 == buffer->range_length() ) {
        // Encoder has no data yet, nothing unusual
        ALOGV("VideoEditorAudioEncoder_processOutputBuffer : buffer is empty");
        pOutBuffer->pTableBufferSize[0] = 0;
        goto cleanUp;
    }
    if( buffer->meta_data()->findInt32(kKeyIsCodecConfig, &i32Tmp) && i32Tmp ) {
        /* This should not happen with software encoder,
         * DSI was retrieved beforehand */
        VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_STATE);
    } else {
        // Check the CTS
        VIDEOEDITOR_CHECK(buffer->meta_data()->findInt64(kKeyTime, &i64Tmp),
            M4ERR_STATE);
        Cts = (M4OSA_Int32)(i64Tmp/1000);

        pEncoderContext->mNbOutputFrames++;
        if( 0 > pEncoderContext->mFirstOutputCts ) {
            pEncoderContext->mFirstOutputCts = i64Tmp;
        }
        pEncoderContext->mLastOutputCts = i64Tmp;

        // Format the AU
        memcpy((void *)pOutBuffer->pTableBuffer[0],
            (void *)((M4OSA_MemAddr8)(buffer->data())+buffer->range_offset()),
            buffer->range_length());
        pOutBuffer->pTableBufferSize[0] = (M4OSA_UInt32)buffer->range_length();
    }

cleanUp:
    // Release the buffer
    buffer->release();
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_processOutputBuffer no error");
    } else {
        ALOGV("VideoEditorAudioEncoder_processOutputBuffer ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_processOutputBuffer end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_step(M4OSA_Context pContext,
        M4ENCODER_AudioBuffer* pInBuffer, M4ENCODER_AudioBuffer* pOutBuffer) {
    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;
    status_t result = OK;
    MediaBuffer* buffer = NULL;

    ALOGV("VideoEditorAudioEncoder_step begin");

    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext,   M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pInBuffer,  M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pOutBuffer, M4ERR_PARAMETER);

    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;
    pEncoderContext->mNbInputFrames++;

    // Push the input buffer to the encoder source
    err = VideoEditorAudioEncoder_processInputBuffer(pEncoderContext,pInBuffer);
    VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);

    // Read
    result = pEncoderContext->mEncoder->read(&buffer, NULL);
    VIDEOEDITOR_CHECK(OK == result, M4ERR_STATE);

    // Provide the encoded AU to the writer
    err = VideoEditorAudioEncoder_processOutputBuffer(pEncoderContext, buffer,
        pOutBuffer);
    VIDEOEDITOR_CHECK(M4NO_ERROR == err, err);

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_step no error");
    } else {
        ALOGV("VideoEditorAudioEncoder_step ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_step end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_getOption(M4OSA_Context pContext,
        M4OSA_OptionID optionID, M4OSA_DataOption* optionValue) {
    M4OSA_ERR err = M4NO_ERROR;
    VideoEditorAudioEncoder_Context* pEncoderContext = M4OSA_NULL;

    ALOGV("VideoEditorAudioEncoder_getOption begin optionID 0x%X", optionID);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pContext, M4ERR_PARAMETER);

    pEncoderContext = (VideoEditorAudioEncoder_Context*)pContext;

    switch( optionID ) {
        default:
            ALOGV("VideoEditorAudioEncoder_getOption: unsupported optionId 0x%X",
                optionID);
            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_BAD_OPTION_ID);
            break;
    }

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_getOption no error");
    } else {
        ALOGV("VideoEditorAudioEncoder_getOption ERROR 0x%X", err);
    }
    ALOGV("VideoEditorAudioEncoder_getOption end");
    return err;
}

M4OSA_ERR VideoEditorAudioEncoder_getInterface(
        M4ENCODER_AudioFormat format, M4ENCODER_AudioFormat* pFormat,
        M4ENCODER_AudioGlobalInterface** pEncoderInterface) {
    M4OSA_ERR err = M4NO_ERROR;

    // Input parameters check
    VIDEOEDITOR_CHECK(M4OSA_NULL != pFormat,           M4ERR_PARAMETER);
    VIDEOEDITOR_CHECK(M4OSA_NULL != pEncoderInterface, M4ERR_PARAMETER);

    ALOGV("VideoEditorAudioEncoder_getInterface 0x%x 0x%x",pFormat,
        pEncoderInterface);
    SAFE_MALLOC(*pEncoderInterface, M4ENCODER_AudioGlobalInterface, 1,
        "AudioEncoder");

    *pFormat = format;

    switch( format ) {
        case M4ENCODER_kAAC:
        {
            (*pEncoderInterface)->pFctInit = VideoEditorAudioEncoder_init_AAC;
            break;
        }
        case M4ENCODER_kAMRNB:
        {
            (*pEncoderInterface)->pFctInit = VideoEditorAudioEncoder_init_AMRNB;
            break;
        }
        case M4ENCODER_kMP3:
        {
            (*pEncoderInterface)->pFctInit = VideoEditorAudioEncoder_init_MP3;
            break;
        }
        default:
        {
            ALOGV("VideoEditorAudioEncoder_getInterface: unsupported format %d",
                format);
            VIDEOEDITOR_CHECK(M4OSA_FALSE, M4ERR_PARAMETER);
        break;
        }
    }
    (*pEncoderInterface)->pFctCleanUp      = VideoEditorAudioEncoder_cleanup;
    (*pEncoderInterface)->pFctOpen         = VideoEditorAudioEncoder_open;
    (*pEncoderInterface)->pFctClose        = VideoEditorAudioEncoder_close;
    (*pEncoderInterface)->pFctStep         = VideoEditorAudioEncoder_step;
    (*pEncoderInterface)->pFctGetOption    = VideoEditorAudioEncoder_getOption;

cleanUp:
    if( M4NO_ERROR == err ) {
        ALOGV("VideoEditorAudioEncoder_getInterface no error");
    } else {
        *pEncoderInterface = M4OSA_NULL;
        ALOGV("VideoEditorAudioEncoder_getInterface ERROR 0x%X", err);
    }
    return err;
}
extern "C" {

M4OSA_ERR VideoEditorAudioEncoder_getInterface_AAC(
        M4ENCODER_AudioFormat* pFormat,
        M4ENCODER_AudioGlobalInterface** pEncoderInterface) {
    return VideoEditorAudioEncoder_getInterface(
        M4ENCODER_kAAC, pFormat, pEncoderInterface);
}

M4OSA_ERR VideoEditorAudioEncoder_getInterface_AMRNB(
        M4ENCODER_AudioFormat* pFormat,
        M4ENCODER_AudioGlobalInterface** pEncoderInterface) {

    return VideoEditorAudioEncoder_getInterface(
        M4ENCODER_kAMRNB, pFormat, pEncoderInterface);
}

M4OSA_ERR VideoEditorAudioEncoder_getInterface_MP3(
        M4ENCODER_AudioFormat* pFormat,
        M4ENCODER_AudioGlobalInterface** pEncoderInterface) {
    ALOGV("VideoEditorAudioEncoder_getInterface_MP3 no error");

    return VideoEditorAudioEncoder_getInterface(
        M4ENCODER_kMP3, pFormat, pEncoderInterface);
}

}  // extern "C"

}  // namespace android