SoftAVCEncoder.cpp revision a32d5435d9585794b72dd12546054f13adb845f2
1/*
2 * Copyright (C) 2012 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//#define LOG_NDEBUG 0
18#define LOG_TAG "SoftAVCEncoder"
19#include <utils/Log.h>
20
21#include "avcenc_api.h"
22#include "avcenc_int.h"
23#include "OMX_Video.h"
24
25#include <HardwareAPI.h>
26#include <MetadataBufferType.h>
27#include <media/stagefright/foundation/ADebug.h>
28#include <media/stagefright/MediaDefs.h>
29#include <media/stagefright/MediaErrors.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/Utils.h>
32#include <ui/Rect.h>
33#include <ui/GraphicBufferMapper.h>
34
35#include "SoftAVCEncoder.h"
36
37#if LOG_NDEBUG
38#define UNUSED_UNLESS_VERBOSE(x) (void)(x)
39#else
40#define UNUSED_UNLESS_VERBOSE(x)
41#endif
42
43namespace android {
44
45template<class T>
46static void InitOMXParams(T *params) {
47    params->nSize = sizeof(T);
48    params->nVersion.s.nVersionMajor = 1;
49    params->nVersion.s.nVersionMinor = 0;
50    params->nVersion.s.nRevision = 0;
51    params->nVersion.s.nStep = 0;
52}
53
54typedef struct LevelConversion {
55    OMX_U32 omxLevel;
56    AVCLevel avcLevel;
57} LevelConcersion;
58
59static LevelConversion ConversionTable[] = {
60    { OMX_VIDEO_AVCLevel1,  AVC_LEVEL1_B },
61    { OMX_VIDEO_AVCLevel1b, AVC_LEVEL1   },
62    { OMX_VIDEO_AVCLevel11, AVC_LEVEL1_1 },
63    { OMX_VIDEO_AVCLevel12, AVC_LEVEL1_2 },
64    { OMX_VIDEO_AVCLevel13, AVC_LEVEL1_3 },
65    { OMX_VIDEO_AVCLevel2,  AVC_LEVEL2 },
66#if 0
67    // encoding speed is very poor if video
68    // resolution is higher than CIF
69    { OMX_VIDEO_AVCLevel21, AVC_LEVEL2_1 },
70    { OMX_VIDEO_AVCLevel22, AVC_LEVEL2_2 },
71    { OMX_VIDEO_AVCLevel3,  AVC_LEVEL3   },
72    { OMX_VIDEO_AVCLevel31, AVC_LEVEL3_1 },
73    { OMX_VIDEO_AVCLevel32, AVC_LEVEL3_2 },
74    { OMX_VIDEO_AVCLevel4,  AVC_LEVEL4   },
75    { OMX_VIDEO_AVCLevel41, AVC_LEVEL4_1 },
76    { OMX_VIDEO_AVCLevel42, AVC_LEVEL4_2 },
77    { OMX_VIDEO_AVCLevel5,  AVC_LEVEL5   },
78    { OMX_VIDEO_AVCLevel51, AVC_LEVEL5_1 },
79#endif
80};
81
82static status_t ConvertOmxAvcLevelToAvcSpecLevel(
83        OMX_U32 omxLevel, AVCLevel *avcLevel) {
84    for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]);
85        i < n; ++i) {
86        if (omxLevel == ConversionTable[i].omxLevel) {
87            *avcLevel = ConversionTable[i].avcLevel;
88            return OK;
89        }
90    }
91
92    ALOGE("ConvertOmxAvcLevelToAvcSpecLevel: %d level not supported",
93            (int32_t)omxLevel);
94
95    return BAD_VALUE;
96}
97
98static status_t ConvertAvcSpecLevelToOmxAvcLevel(
99    AVCLevel avcLevel, OMX_U32 *omxLevel) {
100    for (size_t i = 0, n = sizeof(ConversionTable)/sizeof(ConversionTable[0]);
101        i < n; ++i) {
102        if (avcLevel == ConversionTable[i].avcLevel) {
103            *omxLevel = ConversionTable[i].omxLevel;
104            return OK;
105        }
106    }
107
108    ALOGE("ConvertAvcSpecLevelToOmxAvcLevel: %d level not supported",
109            (int32_t) avcLevel);
110
111    return BAD_VALUE;
112}
113
114static void* MallocWrapper(
115        void * /* userData */, int32_t size, int32_t /* attrs */) {
116    void *ptr = malloc(size);
117    if (ptr)
118        memset(ptr, 0, size);
119    return ptr;
120}
121
122static void FreeWrapper(void * /* userData */, void* ptr) {
123    free(ptr);
124}
125
126static int32_t DpbAllocWrapper(void *userData,
127        unsigned int sizeInMbs, unsigned int numBuffers) {
128    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
129    CHECK(encoder != NULL);
130    return encoder->allocOutputBuffers(sizeInMbs, numBuffers);
131}
132
133static int32_t BindFrameWrapper(
134        void *userData, int32_t index, uint8_t **yuv) {
135    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
136    CHECK(encoder != NULL);
137    return encoder->bindOutputBuffer(index, yuv);
138}
139
140static void UnbindFrameWrapper(void *userData, int32_t index) {
141    SoftAVCEncoder *encoder = static_cast<SoftAVCEncoder *>(userData);
142    CHECK(encoder != NULL);
143    return encoder->unbindOutputBuffer(index);
144}
145
146SoftAVCEncoder::SoftAVCEncoder(
147            const char *name,
148            const OMX_CALLBACKTYPE *callbacks,
149            OMX_PTR appData,
150            OMX_COMPONENTTYPE **component)
151    : SoftVideoEncoderOMXComponent(name, callbacks, appData, component),
152      mVideoWidth(176),
153      mVideoHeight(144),
154      mVideoFrameRate(30),
155      mVideoBitRate(192000),
156      mVideoColorFormat(OMX_COLOR_FormatYUV420Planar),
157      mStoreMetaDataInBuffers(false),
158      mIDRFrameRefreshIntervalInSec(1),
159      mAVCEncProfile(AVC_BASELINE),
160      mAVCEncLevel(AVC_LEVEL2),
161      mNumInputFrames(-1),
162      mPrevTimestampUs(-1),
163      mStarted(false),
164      mSawInputEOS(false),
165      mSignalledError(false),
166      mHandle(new tagAVCHandle),
167      mEncParams(new tagAVCEncParam),
168      mInputFrameData(NULL),
169      mSliceGroup(NULL) {
170
171    initPorts();
172    ALOGI("Construct SoftAVCEncoder");
173}
174
175SoftAVCEncoder::~SoftAVCEncoder() {
176    ALOGV("Destruct SoftAVCEncoder");
177    releaseEncoder();
178    List<BufferInfo *> &outQueue = getPortQueue(1);
179    List<BufferInfo *> &inQueue = getPortQueue(0);
180    CHECK(outQueue.empty());
181    CHECK(inQueue.empty());
182}
183
184OMX_ERRORTYPE SoftAVCEncoder::initEncParams() {
185    CHECK(mHandle != NULL);
186    memset(mHandle, 0, sizeof(tagAVCHandle));
187    mHandle->AVCObject = NULL;
188    mHandle->userData = this;
189    mHandle->CBAVC_DPBAlloc = DpbAllocWrapper;
190    mHandle->CBAVC_FrameBind = BindFrameWrapper;
191    mHandle->CBAVC_FrameUnbind = UnbindFrameWrapper;
192    mHandle->CBAVC_Malloc = MallocWrapper;
193    mHandle->CBAVC_Free = FreeWrapper;
194
195    CHECK(mEncParams != NULL);
196    memset(mEncParams, 0, sizeof(*mEncParams));
197    mEncParams->rate_control = AVC_ON;
198    mEncParams->initQP = 0;
199    mEncParams->init_CBP_removal_delay = 1600;
200
201    mEncParams->intramb_refresh = 0;
202    mEncParams->auto_scd = AVC_ON;
203    mEncParams->out_of_band_param_set = AVC_ON;
204    mEncParams->poc_type = 2;
205    mEncParams->log2_max_poc_lsb_minus_4 = 12;
206    mEncParams->delta_poc_zero_flag = 0;
207    mEncParams->offset_poc_non_ref = 0;
208    mEncParams->offset_top_bottom = 0;
209    mEncParams->num_ref_in_cycle = 0;
210    mEncParams->offset_poc_ref = NULL;
211
212    mEncParams->num_ref_frame = 1;
213    mEncParams->num_slice_group = 1;
214    mEncParams->fmo_type = 0;
215
216    mEncParams->db_filter = AVC_ON;
217    mEncParams->disable_db_idc = 0;
218
219    mEncParams->alpha_offset = 0;
220    mEncParams->beta_offset = 0;
221    mEncParams->constrained_intra_pred = AVC_OFF;
222
223    mEncParams->data_par = AVC_OFF;
224    mEncParams->fullsearch = AVC_OFF;
225    mEncParams->search_range = 16;
226    mEncParams->sub_pel = AVC_OFF;
227    mEncParams->submb_pred = AVC_OFF;
228    mEncParams->rdopt_mode = AVC_OFF;
229    mEncParams->bidir_pred = AVC_OFF;
230
231    mEncParams->use_overrun_buffer = AVC_OFF;
232
233    if (mVideoColorFormat != OMX_COLOR_FormatYUV420Planar
234            || mStoreMetaDataInBuffers) {
235        // Color conversion is needed.
236        free(mInputFrameData);
237        mInputFrameData =
238            (uint8_t *) malloc((mVideoWidth * mVideoHeight * 3 ) >> 1);
239        CHECK(mInputFrameData != NULL);
240    }
241
242    // PV's AVC encoder requires the video dimension of multiple
243    if (mVideoWidth % 16 != 0 || mVideoHeight % 16 != 0) {
244        ALOGE("Video frame size %dx%d must be a multiple of 16",
245            mVideoWidth, mVideoHeight);
246        return OMX_ErrorBadParameter;
247    }
248
249    mEncParams->width = mVideoWidth;
250    mEncParams->height = mVideoHeight;
251    mEncParams->bitrate = mVideoBitRate;
252    mEncParams->frame_rate = 1000 * mVideoFrameRate;  // In frames/ms!
253    mEncParams->CPB_size = (uint32_t) (mVideoBitRate >> 1);
254
255    int32_t nMacroBlocks = ((((mVideoWidth + 15) >> 4) << 4) *
256            (((mVideoHeight + 15) >> 4) << 4)) >> 8;
257    CHECK(mSliceGroup == NULL);
258    mSliceGroup = (uint32_t *) malloc(sizeof(uint32_t) * nMacroBlocks);
259    CHECK(mSliceGroup != NULL);
260    for (int ii = 0, idx = 0; ii < nMacroBlocks; ++ii) {
261        mSliceGroup[ii] = idx++;
262        if (idx >= mEncParams->num_slice_group) {
263            idx = 0;
264        }
265    }
266    mEncParams->slice_group = mSliceGroup;
267
268    // Set IDR frame refresh interval
269    if (mIDRFrameRefreshIntervalInSec < 0) {
270        mEncParams->idr_period = -1;
271    } else if (mIDRFrameRefreshIntervalInSec == 0) {
272        mEncParams->idr_period = 1;  // All I frames
273    } else {
274        mEncParams->idr_period =
275            (mIDRFrameRefreshIntervalInSec * mVideoFrameRate);
276    }
277
278    // Set profile and level
279    mEncParams->profile = mAVCEncProfile;
280    mEncParams->level = mAVCEncLevel;
281
282    return OMX_ErrorNone;
283}
284
285OMX_ERRORTYPE SoftAVCEncoder::initEncoder() {
286    CHECK(!mStarted);
287
288    OMX_ERRORTYPE errType = OMX_ErrorNone;
289    if (OMX_ErrorNone != (errType = initEncParams())) {
290        ALOGE("Failed to initialized encoder params");
291        mSignalledError = true;
292        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
293        return errType;
294    }
295
296    AVCEnc_Status err;
297    err = PVAVCEncInitialize(mHandle, mEncParams, NULL, NULL);
298    if (err != AVCENC_SUCCESS) {
299        ALOGE("Failed to initialize the encoder: %d", err);
300        mSignalledError = true;
301        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
302        return OMX_ErrorUndefined;
303    }
304
305    mNumInputFrames = -2;  // 1st two buffers contain SPS and PPS
306    mSpsPpsHeaderReceived = false;
307    mReadyForNextFrame = true;
308    mIsIDRFrame = false;
309    mStarted = true;
310
311    return OMX_ErrorNone;
312}
313
314OMX_ERRORTYPE SoftAVCEncoder::releaseEncoder() {
315    if (!mStarted) {
316        return OMX_ErrorNone;
317    }
318
319    PVAVCCleanUpEncoder(mHandle);
320    releaseOutputBuffers();
321
322    free(mInputFrameData);
323    mInputFrameData = NULL;
324
325    free(mSliceGroup);
326    mSliceGroup = NULL;
327
328    delete mEncParams;
329    mEncParams = NULL;
330
331    delete mHandle;
332    mHandle = NULL;
333
334    mStarted = false;
335
336    return OMX_ErrorNone;
337}
338
339void SoftAVCEncoder::releaseOutputBuffers() {
340    for (size_t i = 0; i < mOutputBuffers.size(); ++i) {
341        MediaBuffer *buffer = mOutputBuffers.editItemAt(i);
342        buffer->setObserver(NULL);
343        buffer->release();
344    }
345    mOutputBuffers.clear();
346}
347
348void SoftAVCEncoder::initPorts() {
349    OMX_PARAM_PORTDEFINITIONTYPE def;
350    InitOMXParams(&def);
351
352    const size_t kInputBufferSize = (mVideoWidth * mVideoHeight * 3) >> 1;
353
354    // 31584 is PV's magic number.  Not sure why.
355    const size_t kOutputBufferSize =
356            (kInputBufferSize > 31584) ? kInputBufferSize: 31584;
357
358    def.nPortIndex = 0;
359    def.eDir = OMX_DirInput;
360    def.nBufferCountMin = kNumBuffers;
361    def.nBufferCountActual = def.nBufferCountMin;
362    def.nBufferSize = kInputBufferSize;
363    def.bEnabled = OMX_TRUE;
364    def.bPopulated = OMX_FALSE;
365    def.eDomain = OMX_PortDomainVideo;
366    def.bBuffersContiguous = OMX_FALSE;
367    def.nBufferAlignment = 1;
368
369    def.format.video.cMIMEType = const_cast<char *>("video/raw");
370    def.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
371    def.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar;
372    def.format.video.xFramerate = (mVideoFrameRate << 16);  // Q16 format
373    def.format.video.nBitrate = mVideoBitRate;
374    def.format.video.nFrameWidth = mVideoWidth;
375    def.format.video.nFrameHeight = mVideoHeight;
376    def.format.video.nStride = mVideoWidth;
377    def.format.video.nSliceHeight = mVideoHeight;
378
379    addPort(def);
380
381    def.nPortIndex = 1;
382    def.eDir = OMX_DirOutput;
383    def.nBufferCountMin = kNumBuffers;
384    def.nBufferCountActual = def.nBufferCountMin;
385    def.nBufferSize = kOutputBufferSize;
386    def.bEnabled = OMX_TRUE;
387    def.bPopulated = OMX_FALSE;
388    def.eDomain = OMX_PortDomainVideo;
389    def.bBuffersContiguous = OMX_FALSE;
390    def.nBufferAlignment = 2;
391
392    def.format.video.cMIMEType = const_cast<char *>("video/avc");
393    def.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
394    def.format.video.eColorFormat = OMX_COLOR_FormatUnused;
395    def.format.video.xFramerate = (0 << 16);  // Q16 format
396    def.format.video.nBitrate = mVideoBitRate;
397    def.format.video.nFrameWidth = mVideoWidth;
398    def.format.video.nFrameHeight = mVideoHeight;
399    def.format.video.nStride = mVideoWidth;
400    def.format.video.nSliceHeight = mVideoHeight;
401
402    addPort(def);
403}
404
405OMX_ERRORTYPE SoftAVCEncoder::internalGetParameter(
406        OMX_INDEXTYPE index, OMX_PTR params) {
407    switch (index) {
408        case OMX_IndexParamVideoErrorCorrection:
409        {
410            return OMX_ErrorNotImplemented;
411        }
412
413        case OMX_IndexParamVideoBitrate:
414        {
415            OMX_VIDEO_PARAM_BITRATETYPE *bitRate =
416                (OMX_VIDEO_PARAM_BITRATETYPE *) params;
417
418            if (bitRate->nPortIndex != 1) {
419                return OMX_ErrorUndefined;
420            }
421
422            bitRate->eControlRate = OMX_Video_ControlRateVariable;
423            bitRate->nTargetBitrate = mVideoBitRate;
424            return OMX_ErrorNone;
425        }
426
427        case OMX_IndexParamVideoPortFormat:
428        {
429            OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams =
430                (OMX_VIDEO_PARAM_PORTFORMATTYPE *)params;
431
432            if (formatParams->nPortIndex > 1) {
433                return OMX_ErrorUndefined;
434            }
435
436            if (formatParams->nIndex > 2) {
437                return OMX_ErrorNoMore;
438            }
439
440            if (formatParams->nPortIndex == 0) {
441                formatParams->eCompressionFormat = OMX_VIDEO_CodingUnused;
442                if (formatParams->nIndex == 0) {
443                    formatParams->eColorFormat = OMX_COLOR_FormatYUV420Planar;
444                } else if (formatParams->nIndex == 1) {
445                    formatParams->eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
446                } else {
447                    formatParams->eColorFormat = OMX_COLOR_FormatAndroidOpaque;
448                }
449            } else {
450                formatParams->eCompressionFormat = OMX_VIDEO_CodingAVC;
451                formatParams->eColorFormat = OMX_COLOR_FormatUnused;
452            }
453
454            return OMX_ErrorNone;
455        }
456
457        case OMX_IndexParamVideoAvc:
458        {
459            OMX_VIDEO_PARAM_AVCTYPE *avcParams =
460                (OMX_VIDEO_PARAM_AVCTYPE *)params;
461
462            if (avcParams->nPortIndex != 1) {
463                return OMX_ErrorUndefined;
464            }
465
466            avcParams->eProfile = OMX_VIDEO_AVCProfileBaseline;
467            OMX_U32 omxLevel = AVC_LEVEL2;
468            if (OMX_ErrorNone !=
469                ConvertAvcSpecLevelToOmxAvcLevel(mAVCEncLevel, &omxLevel)) {
470                return OMX_ErrorUndefined;
471            }
472
473            avcParams->eLevel = (OMX_VIDEO_AVCLEVELTYPE) omxLevel;
474            avcParams->nRefFrames = 1;
475            avcParams->nBFrames = 0;
476            avcParams->bUseHadamard = OMX_TRUE;
477            avcParams->nAllowedPictureTypes =
478                    (OMX_VIDEO_PictureTypeI | OMX_VIDEO_PictureTypeP);
479            avcParams->nRefIdx10ActiveMinus1 = 0;
480            avcParams->nRefIdx11ActiveMinus1 = 0;
481            avcParams->bWeightedPPrediction = OMX_FALSE;
482            avcParams->bEntropyCodingCABAC = OMX_FALSE;
483            avcParams->bconstIpred = OMX_FALSE;
484            avcParams->bDirect8x8Inference = OMX_FALSE;
485            avcParams->bDirectSpatialTemporal = OMX_FALSE;
486            avcParams->nCabacInitIdc = 0;
487            return OMX_ErrorNone;
488        }
489
490        case OMX_IndexParamVideoProfileLevelQuerySupported:
491        {
492            OMX_VIDEO_PARAM_PROFILELEVELTYPE *profileLevel =
493                (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)params;
494
495            if (profileLevel->nPortIndex != 1) {
496                return OMX_ErrorUndefined;
497            }
498
499            const size_t size =
500                    sizeof(ConversionTable) / sizeof(ConversionTable[0]);
501
502            if (profileLevel->nProfileIndex >= size) {
503                return OMX_ErrorNoMore;
504            }
505
506            profileLevel->eProfile = OMX_VIDEO_AVCProfileBaseline;
507            profileLevel->eLevel = ConversionTable[profileLevel->nProfileIndex].omxLevel;
508
509            return OMX_ErrorNone;
510        }
511
512        default:
513            return SimpleSoftOMXComponent::internalGetParameter(index, params);
514    }
515}
516
517OMX_ERRORTYPE SoftAVCEncoder::internalSetParameter(
518        OMX_INDEXTYPE index, const OMX_PTR params) {
519    int32_t indexFull = index;
520
521    switch (indexFull) {
522        case OMX_IndexParamVideoErrorCorrection:
523        {
524            return OMX_ErrorNotImplemented;
525        }
526
527        case OMX_IndexParamVideoBitrate:
528        {
529            OMX_VIDEO_PARAM_BITRATETYPE *bitRate =
530                (OMX_VIDEO_PARAM_BITRATETYPE *) params;
531
532            if (bitRate->nPortIndex != 1 ||
533                bitRate->eControlRate != OMX_Video_ControlRateVariable) {
534                return OMX_ErrorUndefined;
535            }
536
537            mVideoBitRate = bitRate->nTargetBitrate;
538            return OMX_ErrorNone;
539        }
540
541        case OMX_IndexParamPortDefinition:
542        {
543            OMX_PARAM_PORTDEFINITIONTYPE *def =
544                (OMX_PARAM_PORTDEFINITIONTYPE *)params;
545            if (def->nPortIndex > 1) {
546                return OMX_ErrorUndefined;
547            }
548
549            if (def->nPortIndex == 0) {
550                if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingUnused ||
551                    (def->format.video.eColorFormat != OMX_COLOR_FormatYUV420Planar &&
552                     def->format.video.eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar &&
553                     def->format.video.eColorFormat != OMX_COLOR_FormatAndroidOpaque)) {
554                    return OMX_ErrorUndefined;
555                }
556            } else {
557                if (def->format.video.eCompressionFormat != OMX_VIDEO_CodingAVC ||
558                    (def->format.video.eColorFormat != OMX_COLOR_FormatUnused)) {
559                    return OMX_ErrorUndefined;
560                }
561            }
562
563            OMX_ERRORTYPE err = SimpleSoftOMXComponent::internalSetParameter(index, params);
564            if (OMX_ErrorNone != err) {
565                return err;
566            }
567
568            if (def->nPortIndex == 0) {
569                mVideoWidth = def->format.video.nFrameWidth;
570                mVideoHeight = def->format.video.nFrameHeight;
571                mVideoFrameRate = def->format.video.xFramerate >> 16;
572                mVideoColorFormat = def->format.video.eColorFormat;
573
574                OMX_PARAM_PORTDEFINITIONTYPE *portDef =
575                    &editPortInfo(0)->mDef;
576                portDef->format.video.nFrameWidth = mVideoWidth;
577                portDef->format.video.nFrameHeight = mVideoHeight;
578                portDef->format.video.nStride = portDef->format.video.nFrameWidth;
579                portDef->format.video.nSliceHeight = portDef->format.video.nFrameHeight;
580                portDef->format.video.xFramerate = def->format.video.xFramerate;
581                portDef->format.video.eColorFormat =
582                    (OMX_COLOR_FORMATTYPE) mVideoColorFormat;
583                portDef->nBufferSize =
584                    (portDef->format.video.nStride * portDef->format.video.nSliceHeight * 3) / 2;
585                portDef = &editPortInfo(1)->mDef;
586                portDef->format.video.nFrameWidth = mVideoWidth;
587                portDef->format.video.nFrameHeight = mVideoHeight;
588            } else {
589                mVideoBitRate = def->format.video.nBitrate;
590            }
591
592            return OMX_ErrorNone;
593        }
594
595        case OMX_IndexParamStandardComponentRole:
596        {
597            const OMX_PARAM_COMPONENTROLETYPE *roleParams =
598                (const OMX_PARAM_COMPONENTROLETYPE *)params;
599
600            if (strncmp((const char *)roleParams->cRole,
601                        "video_encoder.avc",
602                        OMX_MAX_STRINGNAME_SIZE - 1)) {
603                return OMX_ErrorUndefined;
604            }
605
606            return OMX_ErrorNone;
607        }
608
609        case OMX_IndexParamVideoPortFormat:
610        {
611            const OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams =
612                (const OMX_VIDEO_PARAM_PORTFORMATTYPE *)params;
613
614            if (formatParams->nPortIndex > 1) {
615                return OMX_ErrorUndefined;
616            }
617
618            if (formatParams->nIndex > 2) {
619                return OMX_ErrorNoMore;
620            }
621
622            if (formatParams->nPortIndex == 0) {
623                if (formatParams->eCompressionFormat != OMX_VIDEO_CodingUnused ||
624                    ((formatParams->nIndex == 0 &&
625                      formatParams->eColorFormat != OMX_COLOR_FormatYUV420Planar) ||
626                    (formatParams->nIndex == 1 &&
627                     formatParams->eColorFormat != OMX_COLOR_FormatYUV420SemiPlanar) ||
628                    (formatParams->nIndex == 2 &&
629                     formatParams->eColorFormat != OMX_COLOR_FormatAndroidOpaque) )) {
630                    return OMX_ErrorUndefined;
631                }
632                mVideoColorFormat = formatParams->eColorFormat;
633            } else {
634                if (formatParams->eCompressionFormat != OMX_VIDEO_CodingAVC ||
635                    formatParams->eColorFormat != OMX_COLOR_FormatUnused) {
636                    return OMX_ErrorUndefined;
637                }
638            }
639
640            return OMX_ErrorNone;
641        }
642
643        case OMX_IndexParamVideoAvc:
644        {
645            OMX_VIDEO_PARAM_AVCTYPE *avcType =
646                (OMX_VIDEO_PARAM_AVCTYPE *)params;
647
648            if (avcType->nPortIndex != 1) {
649                return OMX_ErrorUndefined;
650            }
651
652            // PV's AVC encoder only supports baseline profile
653            if (avcType->eProfile != OMX_VIDEO_AVCProfileBaseline ||
654                avcType->nRefFrames != 1 ||
655                avcType->nBFrames != 0 ||
656                avcType->bUseHadamard != OMX_TRUE ||
657                (avcType->nAllowedPictureTypes & OMX_VIDEO_PictureTypeB) != 0 ||
658                avcType->nRefIdx10ActiveMinus1 != 0 ||
659                avcType->nRefIdx11ActiveMinus1 != 0 ||
660                avcType->bWeightedPPrediction != OMX_FALSE ||
661                avcType->bEntropyCodingCABAC != OMX_FALSE ||
662                avcType->bconstIpred != OMX_FALSE ||
663                avcType->bDirect8x8Inference != OMX_FALSE ||
664                avcType->bDirectSpatialTemporal != OMX_FALSE ||
665                avcType->nCabacInitIdc != 0) {
666                return OMX_ErrorUndefined;
667            }
668
669            if (OK != ConvertOmxAvcLevelToAvcSpecLevel(avcType->eLevel, &mAVCEncLevel)) {
670                return OMX_ErrorUndefined;
671            }
672
673            return OMX_ErrorNone;
674        }
675
676        case kStoreMetaDataExtensionIndex:
677        {
678            StoreMetaDataInBuffersParams *storeParams =
679                    (StoreMetaDataInBuffersParams*)params;
680            if (storeParams->nPortIndex != 0) {
681                ALOGE("%s: StoreMetadataInBuffersParams.nPortIndex not zero!",
682                        __FUNCTION__);
683                return OMX_ErrorUndefined;
684            }
685
686            mStoreMetaDataInBuffers = storeParams->bStoreMetaData;
687            ALOGV("StoreMetaDataInBuffers set to: %s",
688                    mStoreMetaDataInBuffers ? " true" : "false");
689
690            if (mStoreMetaDataInBuffers) {
691                mVideoColorFormat = OMX_COLOR_FormatAndroidOpaque;
692            }
693
694            return OMX_ErrorNone;
695        }
696
697        default:
698            return SimpleSoftOMXComponent::internalSetParameter(index, params);
699    }
700}
701
702void SoftAVCEncoder::onQueueFilled(OMX_U32 /* portIndex */) {
703    if (mSignalledError || mSawInputEOS) {
704        return;
705    }
706
707    if (!mStarted) {
708        if (OMX_ErrorNone != initEncoder()) {
709            return;
710        }
711    }
712
713    List<BufferInfo *> &inQueue = getPortQueue(0);
714    List<BufferInfo *> &outQueue = getPortQueue(1);
715
716    while (!mSawInputEOS && !inQueue.empty() && !outQueue.empty()) {
717        BufferInfo *inInfo = *inQueue.begin();
718        OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader;
719        BufferInfo *outInfo = *outQueue.begin();
720        OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader;
721
722        outHeader->nTimeStamp = 0;
723        outHeader->nFlags = 0;
724        outHeader->nOffset = 0;
725        outHeader->nFilledLen = 0;
726        outHeader->nOffset = 0;
727
728        uint8_t *outPtr = (uint8_t *) outHeader->pBuffer;
729        uint32_t dataLength = outHeader->nAllocLen;
730
731        if (!mSpsPpsHeaderReceived && mNumInputFrames < 0) {
732            // 4 bytes are reserved for holding the start code 0x00000001
733            // of the sequence parameter set at the beginning.
734            outPtr += 4;
735            dataLength -= 4;
736        }
737
738        int32_t type;
739        AVCEnc_Status encoderStatus = AVCENC_SUCCESS;
740
741        // Combine SPS and PPS and place them in the very first output buffer
742        // SPS and PPS are separated by start code 0x00000001
743        // Assume that we have exactly one SPS and exactly one PPS.
744        while (!mSpsPpsHeaderReceived && mNumInputFrames <= 0) {
745            encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type);
746            if (encoderStatus == AVCENC_WRONG_STATE) {
747                mSpsPpsHeaderReceived = true;
748                CHECK_EQ(0, mNumInputFrames);  // 1st video frame is 0
749                outHeader->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
750                outQueue.erase(outQueue.begin());
751                outInfo->mOwnedByUs = false;
752                notifyFillBufferDone(outHeader);
753                return;
754            } else {
755                switch (type) {
756                    case AVC_NALTYPE_SPS:
757                        ++mNumInputFrames;
758                        memcpy((uint8_t *)outHeader->pBuffer, "\x00\x00\x00\x01", 4);
759                        outHeader->nFilledLen = 4 + dataLength;
760                        outPtr += (dataLength + 4);  // 4 bytes for next start code
761                        dataLength = outHeader->nAllocLen - outHeader->nFilledLen;
762                        break;
763                    default:
764                        CHECK_EQ(AVC_NALTYPE_PPS, type);
765                        ++mNumInputFrames;
766                        memcpy((uint8_t *) outHeader->pBuffer + outHeader->nFilledLen,
767                                "\x00\x00\x00\x01", 4);
768                        outHeader->nFilledLen += (dataLength + 4);
769                        outPtr += (dataLength + 4);
770                        break;
771                }
772            }
773        }
774
775        // Get next input video frame
776        if (mReadyForNextFrame) {
777            // Save the input buffer info so that it can be
778            // passed to an output buffer
779            InputBufferInfo info;
780            info.mTimeUs = inHeader->nTimeStamp;
781            info.mFlags = inHeader->nFlags;
782            mInputBufferInfoVec.push(info);
783            mPrevTimestampUs = inHeader->nTimeStamp;
784
785            if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) {
786                mSawInputEOS = true;
787            }
788
789            if (inHeader->nFilledLen > 0) {
790                AVCFrameIO videoInput;
791                memset(&videoInput, 0, sizeof(videoInput));
792                videoInput.height = ((mVideoHeight  + 15) >> 4) << 4;
793                videoInput.pitch = ((mVideoWidth + 15) >> 4) << 4;
794                videoInput.coding_timestamp = (inHeader->nTimeStamp + 500) / 1000;  // in ms
795                const uint8_t *inputData = NULL;
796                if (mStoreMetaDataInBuffers) {
797                    if (inHeader->nFilledLen != 8) {
798                        ALOGE("MetaData buffer is wrong size! "
799                                "(got %u bytes, expected 8)", inHeader->nFilledLen);
800                        mSignalledError = true;
801                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
802                        return;
803                    }
804                    inputData =
805                        extractGraphicBuffer(
806                                mInputFrameData, (mVideoWidth * mVideoHeight * 3) >> 1,
807                                inHeader->pBuffer + inHeader->nOffset, inHeader->nFilledLen,
808                                mVideoWidth, mVideoHeight);
809                    if (inputData == NULL) {
810                        ALOGE("Unable to extract gralloc buffer in metadata mode");
811                        mSignalledError = true;
812                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
813                        return;
814                    }
815                    // TODO: Verify/convert pixel format enum
816                } else {
817                    inputData = (const uint8_t *)inHeader->pBuffer + inHeader->nOffset;
818                    if (mVideoColorFormat != OMX_COLOR_FormatYUV420Planar) {
819                        ConvertYUV420SemiPlanarToYUV420Planar(
820                            inputData, mInputFrameData, mVideoWidth, mVideoHeight);
821                        inputData = mInputFrameData;
822                    }
823                }
824
825                CHECK(inputData != NULL);
826                videoInput.YCbCr[0] = (uint8_t *)inputData;
827                videoInput.YCbCr[1] = videoInput.YCbCr[0] + videoInput.height * videoInput.pitch;
828                videoInput.YCbCr[2] = videoInput.YCbCr[1] +
829                    ((videoInput.height * videoInput.pitch) >> 2);
830                videoInput.disp_order = mNumInputFrames;
831
832                encoderStatus = PVAVCEncSetInput(mHandle, &videoInput);
833                if (encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR) {
834                    mReadyForNextFrame = false;
835                    ++mNumInputFrames;
836                    if (encoderStatus == AVCENC_NEW_IDR) {
837                        mIsIDRFrame = 1;
838                    }
839                } else {
840                    if (encoderStatus < AVCENC_SUCCESS) {
841                        ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__);
842                        mSignalledError = true;
843                        notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
844                        return;
845                    } else {
846                        ALOGV("encoderStatus = %d at line %d", encoderStatus, __LINE__);
847                        inQueue.erase(inQueue.begin());
848                        inInfo->mOwnedByUs = false;
849                        notifyEmptyBufferDone(inHeader);
850                        return;
851                    }
852                }
853            }
854        }
855
856        // Encode an input video frame
857        CHECK(encoderStatus == AVCENC_SUCCESS || encoderStatus == AVCENC_NEW_IDR);
858        dataLength = outHeader->nAllocLen;  // Reset the output buffer length
859        if (inHeader->nFilledLen > 0) {
860            if (outHeader->nAllocLen >= 4) {
861                memcpy(outPtr, "\x00\x00\x00\x01", 4);
862                outPtr += 4;
863                dataLength -= 4;
864            }
865            encoderStatus = PVAVCEncodeNAL(mHandle, outPtr, &dataLength, &type);
866            dataLength = outPtr + dataLength - outHeader->pBuffer;
867            if (encoderStatus == AVCENC_SUCCESS) {
868                CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle));
869            } else if (encoderStatus == AVCENC_PICTURE_READY) {
870                CHECK(NULL == PVAVCEncGetOverrunBuffer(mHandle));
871                if (mIsIDRFrame) {
872                    outHeader->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
873                    mIsIDRFrame = false;
874                }
875                mReadyForNextFrame = true;
876                AVCFrameIO recon;
877                if (PVAVCEncGetRecon(mHandle, &recon) == AVCENC_SUCCESS) {
878                    PVAVCEncReleaseRecon(mHandle, &recon);
879                }
880            } else {
881                dataLength = 0;
882                mReadyForNextFrame = true;
883            }
884
885            if (encoderStatus < AVCENC_SUCCESS) {
886                ALOGE("encoderStatus = %d at line %d", encoderStatus, __LINE__);
887                mSignalledError = true;
888                notify(OMX_EventError, OMX_ErrorUndefined, 0, 0);
889                return;
890            }
891        } else {
892            dataLength = 0;
893        }
894
895        inQueue.erase(inQueue.begin());
896        inInfo->mOwnedByUs = false;
897        notifyEmptyBufferDone(inHeader);
898
899        outQueue.erase(outQueue.begin());
900        CHECK(!mInputBufferInfoVec.empty());
901        InputBufferInfo *inputBufInfo = mInputBufferInfoVec.begin();
902        outHeader->nTimeStamp = inputBufInfo->mTimeUs;
903        outHeader->nFlags |= (inputBufInfo->mFlags | OMX_BUFFERFLAG_ENDOFFRAME);
904        if (mSawInputEOS) {
905            outHeader->nFlags |= OMX_BUFFERFLAG_EOS;
906        }
907        outHeader->nFilledLen = dataLength;
908        outInfo->mOwnedByUs = false;
909        notifyFillBufferDone(outHeader);
910        mInputBufferInfoVec.erase(mInputBufferInfoVec.begin());
911    }
912}
913
914int32_t SoftAVCEncoder::allocOutputBuffers(
915        unsigned int sizeInMbs, unsigned int numBuffers) {
916    CHECK(mOutputBuffers.isEmpty());
917    size_t frameSize = (sizeInMbs << 7) * 3;
918    for (unsigned int i = 0; i <  numBuffers; ++i) {
919        MediaBuffer *buffer = new MediaBuffer(frameSize);
920        buffer->setObserver(this);
921        mOutputBuffers.push(buffer);
922    }
923
924    return 1;
925}
926
927void SoftAVCEncoder::unbindOutputBuffer(int32_t index) {
928    CHECK(index >= 0);
929}
930
931int32_t SoftAVCEncoder::bindOutputBuffer(int32_t index, uint8_t **yuv) {
932    CHECK(index >= 0);
933    CHECK(index < (int32_t) mOutputBuffers.size());
934    *yuv = (uint8_t *) mOutputBuffers[index]->data();
935
936    return 1;
937}
938
939void SoftAVCEncoder::signalBufferReturned(MediaBuffer *buffer) {
940    UNUSED_UNLESS_VERBOSE(buffer);
941    ALOGV("signalBufferReturned: %p", buffer);
942}
943
944}  // namespace android
945
946android::SoftOMXComponent *createSoftOMXComponent(
947        const char *name, const OMX_CALLBACKTYPE *callbacks,
948        OMX_PTR appData, OMX_COMPONENTTYPE **component) {
949    return new android::SoftAVCEncoder(name, callbacks, appData, component);
950}
951