Home | History | Annotate | Download | only in h264enc
      1 /*
      2  *
      3  * Copyright 2010 Samsung Electronics S.LSI Co. LTD
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 /*
     19  * @file        SEC_OMX_H264enc.c
     20  * @brief
     21  * @author      SeungBeom Kim (sbcrux.kim (at) samsung.com)
     22  * @version     1.0
     23  * @history
     24  *   2010.7.15 : Create
     25  */
     26 
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <string.h>
     30 
     31 #include "SEC_OMX_Macros.h"
     32 #include "SEC_OMX_Basecomponent.h"
     33 #include "SEC_OMX_Baseport.h"
     34 #include "SEC_OMX_Venc.h"
     35 #include "library_register.h"
     36 #include "SEC_OMX_H264enc.h"
     37 #include "SsbSipMfcApi.h"
     38 #include "color_space_convertor.h"
     39 
     40 #undef  SEC_LOG_TAG
     41 #define SEC_LOG_TAG    "SEC_H264_ENC"
     42 #define SEC_LOG_OFF
     43 #include "SEC_OSAL_Log.h"
     44 
     45 
     46 /* H.264 Encoder Supported Levels & profiles */
     47 SEC_OMX_VIDEO_PROFILELEVEL supportedAVCProfileLevels[] ={
     48     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1},
     49     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1b},
     50     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel11},
     51     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel12},
     52     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13},
     53     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel2},
     54     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel21},
     55     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel22},
     56     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel3},
     57     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel31},
     58     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel32},
     59     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel4},
     60 
     61     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1},
     62     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1b},
     63     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel11},
     64     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel12},
     65     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel13},
     66     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel2},
     67     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel21},
     68     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel22},
     69     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel3},
     70     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel31},
     71     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel32},
     72     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel4},
     73 
     74     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel1},
     75     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel1b},
     76     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel11},
     77     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel12},
     78     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel13},
     79     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel2},
     80     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel21},
     81     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel22},
     82     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel3},
     83     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel31},
     84     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel32},
     85     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel4}};
     86 
     87 
     88 OMX_U32 OMXAVCProfileToProfileIDC(OMX_VIDEO_AVCPROFILETYPE profile)
     89 {
     90     OMX_U32 ret = 0; //default OMX_VIDEO_AVCProfileMain
     91 
     92     if (profile == OMX_VIDEO_AVCProfileMain)
     93         ret = 0;
     94     else if (profile == OMX_VIDEO_AVCProfileHigh)
     95         ret = 1;
     96     else if (profile == OMX_VIDEO_AVCProfileBaseline)
     97         ret = 2;
     98 
     99     return ret;
    100 }
    101 
    102 OMX_U32 OMXAVCLevelToLevelIDC(OMX_VIDEO_AVCLEVELTYPE level)
    103 {
    104     OMX_U32 ret = 40; //default OMX_VIDEO_AVCLevel4
    105 
    106     if (level == OMX_VIDEO_AVCLevel1)
    107         ret = 10;
    108     else if (level == OMX_VIDEO_AVCLevel1b)
    109         ret = 9;
    110     else if (level == OMX_VIDEO_AVCLevel11)
    111         ret = 11;
    112     else if (level == OMX_VIDEO_AVCLevel12)
    113         ret = 12;
    114     else if (level == OMX_VIDEO_AVCLevel13)
    115         ret = 13;
    116     else if (level == OMX_VIDEO_AVCLevel2)
    117         ret = 20;
    118     else if (level == OMX_VIDEO_AVCLevel21)
    119         ret = 21;
    120     else if (level == OMX_VIDEO_AVCLevel22)
    121         ret = 22;
    122     else if (level == OMX_VIDEO_AVCLevel3)
    123         ret = 30;
    124     else if (level == OMX_VIDEO_AVCLevel31)
    125         ret = 31;
    126     else if (level == OMX_VIDEO_AVCLevel32)
    127         ret = 32;
    128     else if (level == OMX_VIDEO_AVCLevel4)
    129         ret = 40;
    130 
    131     return ret;
    132 }
    133 
    134 OMX_U8 *FindDelimiter(OMX_U8 *pBuffer, OMX_U32 size)
    135 {
    136     int i;
    137 
    138     for (i = 0; i < size - 3; i++) {
    139         if ((pBuffer[i] == 0x00)   &&
    140             (pBuffer[i+1] == 0x00) &&
    141             (pBuffer[i+2] == 0x00) &&
    142             (pBuffer[i+3] == 0x01))
    143             return (pBuffer + i);
    144     }
    145 
    146     return NULL;
    147 }
    148 
    149 void H264PrintParams(SSBSIP_MFC_ENC_H264_PARAM h264Arg)
    150 {
    151     SEC_OSAL_Log(SEC_LOG_TRACE, "SourceWidth             : %d\n", h264Arg.SourceWidth);
    152     SEC_OSAL_Log(SEC_LOG_TRACE, "SourceHeight            : %d\n", h264Arg.SourceHeight);
    153     SEC_OSAL_Log(SEC_LOG_TRACE, "ProfileIDC              : %d\n", h264Arg.ProfileIDC);
    154     SEC_OSAL_Log(SEC_LOG_TRACE, "LevelIDC                : %d\n", h264Arg.LevelIDC);
    155     SEC_OSAL_Log(SEC_LOG_TRACE, "IDRPeriod               : %d\n", h264Arg.IDRPeriod);
    156     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberReferenceFrames   : %d\n", h264Arg.NumberReferenceFrames);
    157     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberRefForPframes     : %d\n", h264Arg.NumberRefForPframes);
    158     SEC_OSAL_Log(SEC_LOG_TRACE, "SliceMode               : %d\n", h264Arg.SliceMode);
    159     SEC_OSAL_Log(SEC_LOG_TRACE, "SliceArgument           : %d\n", h264Arg.SliceArgument);
    160     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberBFrames           : %d\n", h264Arg.NumberBFrames);
    161     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterDisable       : %d\n", h264Arg.LoopFilterDisable);
    162     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterAlphaC0Offset : %d\n", h264Arg.LoopFilterAlphaC0Offset);
    163     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterBetaOffset    : %d\n", h264Arg.LoopFilterBetaOffset);
    164     SEC_OSAL_Log(SEC_LOG_TRACE, "SymbolMode              : %d\n", h264Arg.SymbolMode);
    165     SEC_OSAL_Log(SEC_LOG_TRACE, "PictureInterlace        : %d\n", h264Arg.PictureInterlace);
    166     SEC_OSAL_Log(SEC_LOG_TRACE, "Transform8x8Mode        : %d\n", h264Arg.Transform8x8Mode);
    167     SEC_OSAL_Log(SEC_LOG_TRACE, "RandomIntraMBRefresh    : %d\n", h264Arg.RandomIntraMBRefresh);
    168     SEC_OSAL_Log(SEC_LOG_TRACE, "PadControlOn            : %d\n", h264Arg.PadControlOn);
    169     SEC_OSAL_Log(SEC_LOG_TRACE, "LumaPadVal              : %d\n", h264Arg.LumaPadVal);
    170     SEC_OSAL_Log(SEC_LOG_TRACE, "CbPadVal                : %d\n", h264Arg.CbPadVal);
    171     SEC_OSAL_Log(SEC_LOG_TRACE, "CrPadVal                : %d\n", h264Arg.CrPadVal);
    172     SEC_OSAL_Log(SEC_LOG_TRACE, "EnableFRMRateControl    : %d\n", h264Arg.EnableFRMRateControl);
    173     SEC_OSAL_Log(SEC_LOG_TRACE, "EnableMBRateControl     : %d\n", h264Arg.EnableMBRateControl);
    174     SEC_OSAL_Log(SEC_LOG_TRACE, "FrameRate               : %d\n", h264Arg.FrameRate);
    175     SEC_OSAL_Log(SEC_LOG_TRACE, "Bitrate                 : %d\n", h264Arg.Bitrate);
    176     SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp                 : %d\n", h264Arg.FrameQp);
    177     SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMax               : %d\n", h264Arg.QSCodeMax);
    178     SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMin               : %d\n", h264Arg.QSCodeMin);
    179     SEC_OSAL_Log(SEC_LOG_TRACE, "CBRPeriodRf             : %d\n", h264Arg.CBRPeriodRf);
    180     SEC_OSAL_Log(SEC_LOG_TRACE, "DarkDisable             : %d\n", h264Arg.DarkDisable);
    181     SEC_OSAL_Log(SEC_LOG_TRACE, "SmoothDisable           : %d\n", h264Arg.SmoothDisable);
    182     SEC_OSAL_Log(SEC_LOG_TRACE, "StaticDisable           : %d\n", h264Arg.StaticDisable);
    183     SEC_OSAL_Log(SEC_LOG_TRACE, "ActivityDisable         : %d\n", h264Arg.ActivityDisable);
    184 }
    185 
    186 void Set_H264ENC_Param(SSBSIP_MFC_ENC_H264_PARAM *pH264Arg, SEC_OMX_BASECOMPONENT *pSECComponent)
    187 {
    188     SEC_OMX_BASEPORT          *pSECInputPort = NULL;
    189     SEC_OMX_BASEPORT          *pSECOutputPort = NULL;
    190     SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    191 
    192     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    193     pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    194     pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    195 
    196     pH264Arg->codecType    = H264_ENC;
    197     pH264Arg->SourceWidth  = pSECOutputPort->portDefinition.format.video.nFrameWidth;
    198     pH264Arg->SourceHeight = pSECOutputPort->portDefinition.format.video.nFrameHeight;
    199     pH264Arg->IDRPeriod    = pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].nPFrames + 1;
    200     pH264Arg->SliceMode    = 0;
    201     pH264Arg->RandomIntraMBRefresh = 0;
    202     pH264Arg->EnableFRMRateControl = 1;        // 0: Disable, 1: Frame level RC
    203     pH264Arg->Bitrate      = pSECOutputPort->portDefinition.format.video.nBitrate;
    204     pH264Arg->FrameQp      = 20;
    205     pH264Arg->FrameQp_P    = 20;
    206     pH264Arg->QSCodeMax    = 30;
    207     pH264Arg->QSCodeMin    = 10;
    208     pH264Arg->CBRPeriodRf  = 100;
    209     pH264Arg->PadControlOn = 0;             // 0: disable, 1: enable
    210     pH264Arg->LumaPadVal   = 0;
    211     pH264Arg->CbPadVal     = 0;
    212     pH264Arg->CrPadVal     = 0;
    213 
    214     pH264Arg->ProfileIDC   = OMXAVCProfileToProfileIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eProfile); //0;  //(OMX_VIDEO_AVCProfileMain)
    215     pH264Arg->LevelIDC     = OMXAVCLevelToLevelIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eLevel);       //40; //(OMX_VIDEO_AVCLevel4)
    216     pH264Arg->FrameQp_B    = 20;
    217     pH264Arg->FrameRate    = (pSECInputPort->portDefinition.format.video.xFramerate) >> 16;
    218     pH264Arg->SliceArgument = 0;          // Slice mb/byte size number
    219     pH264Arg->NumberBFrames = 0;            // 0 ~ 2
    220     pH264Arg->NumberReferenceFrames = 1;
    221     pH264Arg->NumberRefForPframes   = 1;
    222     pH264Arg->LoopFilterDisable     = 1;    // 1: Loop Filter Disable, 0: Filter Enable
    223     pH264Arg->LoopFilterAlphaC0Offset = 0;
    224     pH264Arg->LoopFilterBetaOffset    = 0;
    225     pH264Arg->SymbolMode       = 0;         // 0: CAVLC, 1: CABAC
    226     pH264Arg->PictureInterlace = 0;
    227     pH264Arg->Transform8x8Mode = 0;         // 0: 4x4, 1: allow 8x8
    228     pH264Arg->EnableMBRateControl = 0;        // 0: Disable, 1:MB level RC
    229     pH264Arg->DarkDisable     = 1;
    230     pH264Arg->SmoothDisable   = 1;
    231     pH264Arg->StaticDisable   = 1;
    232     pH264Arg->ActivityDisable = 1;
    233 
    234     switch ((SEC_OMX_COLOR_FORMATTYPE)pSECInputPort->portDefinition.format.video.eColorFormat) {
    235     case OMX_COLOR_FormatYUV420SemiPlanar:
    236         pH264Arg->FrameMap = NV12_LINEAR;
    237         break;
    238     case OMX_SEC_COLOR_FormatNV12TPhysicalAddress:
    239     default:
    240         pH264Arg->FrameMap = NV12_TILE;
    241         break;
    242     }
    243 
    244 #ifdef USE_ANDROID_EXTENSION
    245     if (pSECInputPort->bStoreMetaDataInBuffer != OMX_FALSE) {
    246         SEC_OMX_DATA *pInputData = &pSECComponent->processData[INPUT_PORT_INDEX];
    247         if(isMetadataBufferTypeGrallocSource(pInputData->dataBuffer) == OMX_TRUE)
    248             pH264Arg->FrameMap = NV12_LINEAR;
    249         else
    250             pH264Arg->FrameMap = NV12_TILE;
    251     }
    252 #endif
    253 
    254 /*
    255     SEC_OSAL_Log(SEC_LOG_TRACE, "pSECPort->eControlRate: 0x%x", pSECOutputPort->eControlRate);
    256     switch (pSECOutputPort->eControlRate) {
    257     case OMX_Video_ControlRateVariable:
    258         SEC_OSAL_Log(SEC_LOG_TRACE, "Video Encode VBR");
    259         pH264Arg->EnableFRMRateControl = 0;        // 0: Disable, 1: Frame level RC
    260         pH264Arg->EnableMBRateControl  = 0;        // 0: Disable, 1:MB level RC
    261         pH264Arg->CBRPeriodRf  = 100;
    262         break;
    263     case OMX_Video_ControlRateConstant:
    264         SEC_OSAL_Log(SEC_LOG_TRACE, "Video Encode CBR");
    265         pH264Arg->EnableFRMRateControl = 1;        // 0: Disable, 1: Frame level RC
    266         pH264Arg->EnableMBRateControl  = 1;        // 0: Disable, 1:MB level RC
    267         pH264Arg->CBRPeriodRf  = 10;
    268         break;
    269     case OMX_Video_ControlRateDisable:
    270     default: //Android default
    271         SEC_OSAL_Log(SEC_LOG_TRACE, "Video Encode VBR");
    272         pH264Arg->EnableFRMRateControl = 0;
    273         pH264Arg->EnableMBRateControl  = 0;
    274         pH264Arg->CBRPeriodRf  = 100;
    275         break;
    276     }
    277 */
    278     H264PrintParams(*pH264Arg);
    279 }
    280 
    281 OMX_ERRORTYPE SEC_MFC_H264Enc_GetParameter(
    282     OMX_IN OMX_HANDLETYPE hComponent,
    283     OMX_IN OMX_INDEXTYPE  nParamIndex,
    284     OMX_INOUT OMX_PTR     pComponentParameterStructure)
    285 {
    286     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    287     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    288     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    289 
    290     FunctionIn();
    291 
    292     if (hComponent == NULL || pComponentParameterStructure == NULL) {
    293         ret = OMX_ErrorBadParameter;
    294         goto EXIT;
    295     }
    296     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    297     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    298     if (ret != OMX_ErrorNone) {
    299         goto EXIT;
    300     }
    301     if (pOMXComponent->pComponentPrivate == NULL) {
    302         ret = OMX_ErrorBadParameter;
    303         goto EXIT;
    304     }
    305 
    306     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    307     if (pSECComponent->currentState == OMX_StateInvalid ) {
    308         ret = OMX_StateInvalid;
    309         goto EXIT;
    310     }
    311 
    312     switch (nParamIndex) {
    313     case OMX_IndexParamVideoAvc:
    314     {
    315         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = (OMX_VIDEO_PARAM_AVCTYPE *)pComponentParameterStructure;
    316         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = NULL;
    317         SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    318 
    319         ret = SEC_OMX_Check_SizeVersion(pDstAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    320         if (ret != OMX_ErrorNone) {
    321             goto EXIT;
    322         }
    323 
    324         if (pDstAVCComponent->nPortIndex >= ALL_PORT_NUM) {
    325             ret = OMX_ErrorBadPortIndex;
    326             goto EXIT;
    327         }
    328 
    329         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    330         pSrcAVCComponent = &pH264Enc->AVCComponent[pDstAVCComponent->nPortIndex];
    331 
    332         SEC_OSAL_Memcpy(pDstAVCComponent, pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    333     }
    334         break;
    335     case OMX_IndexParamStandardComponentRole:
    336     {
    337         OMX_PARAM_COMPONENTROLETYPE *pComponentRole = (OMX_PARAM_COMPONENTROLETYPE *)pComponentParameterStructure;
    338         ret = SEC_OMX_Check_SizeVersion(pComponentRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
    339         if (ret != OMX_ErrorNone) {
    340             goto EXIT;
    341         }
    342 
    343         SEC_OSAL_Strcpy((char *)pComponentRole->cRole, SEC_OMX_COMPONENT_H264_ENC_ROLE);
    344     }
    345         break;
    346     case OMX_IndexParamVideoProfileLevelQuerySupported:
    347     {
    348         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pDstProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)pComponentParameterStructure;
    349         SEC_OMX_VIDEO_PROFILELEVEL *pProfileLevel = NULL;
    350         OMX_U32 maxProfileLevelNum = 0;
    351 
    352         ret = SEC_OMX_Check_SizeVersion(pDstProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    353         if (ret != OMX_ErrorNone) {
    354             goto EXIT;
    355         }
    356 
    357         if (pDstProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    358             ret = OMX_ErrorBadPortIndex;
    359             goto EXIT;
    360         }
    361 
    362         pProfileLevel = supportedAVCProfileLevels;
    363         maxProfileLevelNum = sizeof(supportedAVCProfileLevels) / sizeof(SEC_OMX_VIDEO_PROFILELEVEL);
    364 
    365         if (pDstProfileLevel->nProfileIndex >= maxProfileLevelNum) {
    366             ret = OMX_ErrorNoMore;
    367             goto EXIT;
    368         }
    369 
    370         pProfileLevel += pDstProfileLevel->nProfileIndex;
    371         pDstProfileLevel->eProfile = pProfileLevel->profile;
    372         pDstProfileLevel->eLevel = pProfileLevel->level;
    373     }
    374         break;
    375     case OMX_IndexParamVideoProfileLevelCurrent:
    376     {
    377         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pDstProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)pComponentParameterStructure;
    378         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = NULL;
    379         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    380 
    381         ret = SEC_OMX_Check_SizeVersion(pDstProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    382         if (ret != OMX_ErrorNone) {
    383             goto EXIT;
    384         }
    385 
    386         if (pDstProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    387             ret = OMX_ErrorBadPortIndex;
    388             goto EXIT;
    389         }
    390 
    391         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    392         pSrcAVCComponent = &pH264Enc->AVCComponent[pDstProfileLevel->nPortIndex];
    393 
    394         pDstProfileLevel->eProfile = pSrcAVCComponent->eProfile;
    395         pDstProfileLevel->eLevel = pSrcAVCComponent->eLevel;
    396     }
    397         break;
    398     case OMX_IndexParamVideoErrorCorrection:
    399     {
    400         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pDstErrorCorrectionType = (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *)pComponentParameterStructure;
    401         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pSrcErrorCorrectionType = NULL;
    402         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    403 
    404         ret = SEC_OMX_Check_SizeVersion(pDstErrorCorrectionType, sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
    405         if (ret != OMX_ErrorNone) {
    406             goto EXIT;
    407         }
    408 
    409         if (pDstErrorCorrectionType->nPortIndex != OUTPUT_PORT_INDEX) {
    410             ret = OMX_ErrorBadPortIndex;
    411             goto EXIT;
    412         }
    413 
    414         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    415         pSrcErrorCorrectionType = &pH264Enc->errorCorrectionType[OUTPUT_PORT_INDEX];
    416 
    417         pDstErrorCorrectionType->bEnableHEC = pSrcErrorCorrectionType->bEnableHEC;
    418         pDstErrorCorrectionType->bEnableResync = pSrcErrorCorrectionType->bEnableResync;
    419         pDstErrorCorrectionType->nResynchMarkerSpacing = pSrcErrorCorrectionType->nResynchMarkerSpacing;
    420         pDstErrorCorrectionType->bEnableDataPartitioning = pSrcErrorCorrectionType->bEnableDataPartitioning;
    421         pDstErrorCorrectionType->bEnableRVLC = pSrcErrorCorrectionType->bEnableRVLC;
    422     }
    423         break;
    424     default:
    425         ret = SEC_OMX_VideoEncodeGetParameter(hComponent, nParamIndex, pComponentParameterStructure);
    426         break;
    427     }
    428 EXIT:
    429     FunctionOut();
    430 
    431     return ret;
    432 }
    433 
    434 OMX_ERRORTYPE SEC_MFC_H264Enc_SetParameter(
    435     OMX_IN OMX_HANDLETYPE hComponent,
    436     OMX_IN OMX_INDEXTYPE  nIndex,
    437     OMX_IN OMX_PTR        pComponentParameterStructure)
    438 {
    439     OMX_ERRORTYPE           ret = OMX_ErrorNone;
    440     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    441     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    442 
    443     FunctionIn();
    444 
    445     if (hComponent == NULL || pComponentParameterStructure == NULL) {
    446         ret = OMX_ErrorBadParameter;
    447         goto EXIT;
    448     }
    449     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    450     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    451     if (ret != OMX_ErrorNone) {
    452         goto EXIT;
    453     }
    454     if (pOMXComponent->pComponentPrivate == NULL) {
    455         ret = OMX_ErrorBadParameter;
    456         goto EXIT;
    457     }
    458 
    459     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    460     if (pSECComponent->currentState == OMX_StateInvalid ) {
    461         ret = OMX_StateInvalid;
    462         goto EXIT;
    463     }
    464 
    465     switch (nIndex) {
    466     case OMX_IndexParamVideoAvc:
    467     {
    468         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = NULL;
    469         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = (OMX_VIDEO_PARAM_AVCTYPE *)pComponentParameterStructure;
    470         SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    471 
    472         ret = SEC_OMX_Check_SizeVersion(pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    473         if (ret != OMX_ErrorNone) {
    474             goto EXIT;
    475         }
    476 
    477         if (pSrcAVCComponent->nPortIndex >= ALL_PORT_NUM) {
    478             ret = OMX_ErrorBadPortIndex;
    479             goto EXIT;
    480         }
    481 
    482         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    483         pDstAVCComponent = &pH264Enc->AVCComponent[pSrcAVCComponent->nPortIndex];
    484 
    485         SEC_OSAL_Memcpy(pDstAVCComponent, pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    486     }
    487         break;
    488     case OMX_IndexParamStandardComponentRole:
    489     {
    490         OMX_PARAM_COMPONENTROLETYPE *pComponentRole = (OMX_PARAM_COMPONENTROLETYPE*)pComponentParameterStructure;
    491 
    492         ret = SEC_OMX_Check_SizeVersion(pComponentRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
    493         if (ret != OMX_ErrorNone) {
    494             goto EXIT;
    495         }
    496 
    497         if ((pSECComponent->currentState != OMX_StateLoaded) && (pSECComponent->currentState != OMX_StateWaitForResources)) {
    498             ret = OMX_ErrorIncorrectStateOperation;
    499             goto EXIT;
    500         }
    501 
    502         if (!SEC_OSAL_Strcmp((char*)pComponentRole->cRole, SEC_OMX_COMPONENT_H264_ENC_ROLE)) {
    503             pSECComponent->pSECPort[OUTPUT_PORT_INDEX].portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
    504         } else {
    505             ret = OMX_ErrorBadParameter;
    506             goto EXIT;
    507         }
    508     }
    509         break;
    510     case OMX_IndexParamVideoProfileLevelCurrent:
    511     {
    512         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pSrcProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)pComponentParameterStructure;
    513         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = NULL;
    514         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    515 
    516         ret = SEC_OMX_Check_SizeVersion(pSrcProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    517         if (ret != OMX_ErrorNone) {
    518             goto EXIT;
    519         }
    520 
    521         if (pSrcProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    522             ret = OMX_ErrorBadPortIndex;
    523             goto EXIT;
    524         }
    525 
    526         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    527 
    528         pDstAVCComponent = &pH264Enc->AVCComponent[pSrcProfileLevel->nPortIndex];
    529         pDstAVCComponent->eProfile = pSrcProfileLevel->eProfile;
    530         pDstAVCComponent->eLevel = pSrcProfileLevel->eLevel;
    531     }
    532         break;
    533     case OMX_IndexParamVideoErrorCorrection:
    534     {
    535         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pSrcErrorCorrectionType = (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *)pComponentParameterStructure;
    536         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pDstErrorCorrectionType = NULL;
    537         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    538 
    539         ret = SEC_OMX_Check_SizeVersion(pSrcErrorCorrectionType, sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
    540         if (ret != OMX_ErrorNone) {
    541             goto EXIT;
    542         }
    543 
    544         if (pSrcErrorCorrectionType->nPortIndex != OUTPUT_PORT_INDEX) {
    545             ret = OMX_ErrorBadPortIndex;
    546             goto EXIT;
    547         }
    548 
    549         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    550         pDstErrorCorrectionType = &pH264Enc->errorCorrectionType[OUTPUT_PORT_INDEX];
    551 
    552         pDstErrorCorrectionType->bEnableHEC = pSrcErrorCorrectionType->bEnableHEC;
    553         pDstErrorCorrectionType->bEnableResync = pSrcErrorCorrectionType->bEnableResync;
    554         pDstErrorCorrectionType->nResynchMarkerSpacing = pSrcErrorCorrectionType->nResynchMarkerSpacing;
    555         pDstErrorCorrectionType->bEnableDataPartitioning = pSrcErrorCorrectionType->bEnableDataPartitioning;
    556         pDstErrorCorrectionType->bEnableRVLC = pSrcErrorCorrectionType->bEnableRVLC;
    557     }
    558         break;
    559     default:
    560         ret = SEC_OMX_VideoEncodeSetParameter(hComponent, nIndex, pComponentParameterStructure);
    561         break;
    562     }
    563 EXIT:
    564     FunctionOut();
    565 
    566     return ret;
    567 }
    568 
    569 OMX_ERRORTYPE SEC_MFC_H264Enc_GetConfig(
    570     OMX_HANDLETYPE hComponent,
    571     OMX_INDEXTYPE nIndex,
    572     OMX_PTR pComponentConfigStructure)
    573 {
    574     OMX_ERRORTYPE           ret = OMX_ErrorNone;
    575     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    576     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    577 
    578     FunctionIn();
    579 
    580     if (hComponent == NULL) {
    581         ret = OMX_ErrorBadParameter;
    582         goto EXIT;
    583     }
    584     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    585     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    586     if (ret != OMX_ErrorNone) {
    587         goto EXIT;
    588     }
    589 
    590     if (pOMXComponent->pComponentPrivate == NULL) {
    591         ret = OMX_ErrorBadParameter;
    592         goto EXIT;
    593     }
    594     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    595 
    596     if (pComponentConfigStructure == NULL) {
    597         ret = OMX_ErrorBadParameter;
    598         goto EXIT;
    599     }
    600     if (pSECComponent->currentState == OMX_StateInvalid) {
    601         ret = OMX_ErrorInvalidState;
    602         goto EXIT;
    603     }
    604 
    605     switch (nIndex) {
    606     default:
    607         ret = SEC_OMX_GetConfig(hComponent, nIndex, pComponentConfigStructure);
    608         break;
    609     }
    610 
    611 EXIT:
    612     FunctionOut();
    613 
    614     return ret;
    615 }
    616 
    617 OMX_ERRORTYPE SEC_MFC_H264Enc_SetConfig(
    618     OMX_HANDLETYPE hComponent,
    619     OMX_INDEXTYPE nIndex,
    620     OMX_PTR pComponentConfigStructure)
    621 {
    622     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    623     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    624     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    625 
    626     FunctionIn();
    627 
    628     if (hComponent == NULL || pComponentConfigStructure == NULL) {
    629         ret = OMX_ErrorBadParameter;
    630         goto EXIT;
    631     }
    632     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    633     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    634     if (ret != OMX_ErrorNone) {
    635         goto EXIT;
    636     }
    637     if (pOMXComponent->pComponentPrivate == NULL) {
    638         ret = OMX_ErrorBadParameter;
    639         goto EXIT;
    640     }
    641 
    642     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    643     if (pSECComponent->currentState == OMX_StateInvalid) {
    644         ret = OMX_ErrorInvalidState;
    645         goto EXIT;
    646     }
    647 
    648     switch (nIndex) {
    649     default:
    650         ret = SEC_OMX_SetConfig(hComponent, nIndex, pComponentConfigStructure);
    651         break;
    652     }
    653 
    654 EXIT:
    655     FunctionOut();
    656 
    657     return ret;
    658 }
    659 
    660 OMX_ERRORTYPE SEC_MFC_H264Enc_GetExtensionIndex(
    661     OMX_IN OMX_HANDLETYPE  hComponent,
    662     OMX_IN OMX_STRING      cParameterName,
    663     OMX_OUT OMX_INDEXTYPE *pIndexType)
    664 {
    665     OMX_ERRORTYPE           ret = OMX_ErrorNone;
    666     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    667     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    668 
    669     FunctionIn();
    670 
    671     if (hComponent == NULL) {
    672         ret = OMX_ErrorBadParameter;
    673         goto EXIT;
    674     }
    675     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    676     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    677     if (ret != OMX_ErrorNone) {
    678         goto EXIT;
    679     }
    680 
    681     if (pOMXComponent->pComponentPrivate == NULL) {
    682         ret = OMX_ErrorBadParameter;
    683         goto EXIT;
    684     }
    685     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    686 
    687     if ((cParameterName == NULL) || (pIndexType == NULL)) {
    688         ret = OMX_ErrorBadParameter;
    689         goto EXIT;
    690     }
    691     if (pSECComponent->currentState == OMX_StateInvalid) {
    692         ret = OMX_ErrorInvalidState;
    693         goto EXIT;
    694     }
    695 
    696 #ifdef USE_ANDROID_EXTENSION
    697     if (SEC_OSAL_Strcmp(cParameterName, SEC_INDEX_PARAM_STORE_METADATA_BUFFER) == 0) {
    698         *pIndexType = OMX_IndexParamStoreMetaDataBuffer;
    699         ret = OMX_ErrorNone;
    700     } else {
    701         ret = SEC_OMX_GetExtensionIndex(hComponent, cParameterName, pIndexType);
    702     }
    703 #else
    704     ret = SEC_OMX_GetExtensionIndex(hComponent, cParameterName, pIndexType);
    705 #endif
    706 
    707 EXIT:
    708     FunctionOut();
    709 
    710     return ret;
    711 }
    712 
    713 OMX_ERRORTYPE SEC_MFC_H264Enc_ComponentRoleEnum(OMX_HANDLETYPE hComponent, OMX_U8 *cRole, OMX_U32 nIndex)
    714 {
    715     OMX_ERRORTYPE            ret = OMX_ErrorNone;
    716     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    717     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    718 
    719     FunctionIn();
    720 
    721     if ((hComponent == NULL) || (cRole == NULL)) {
    722         ret = OMX_ErrorBadParameter;
    723         goto EXIT;
    724     }
    725     if (nIndex == (MAX_COMPONENT_ROLE_NUM-1)) {
    726         SEC_OSAL_Strcpy((char *)cRole, SEC_OMX_COMPONENT_H264_ENC_ROLE);
    727         ret = OMX_ErrorNone;
    728     } else {
    729         ret = OMX_ErrorNoMore;
    730     }
    731 
    732 EXIT:
    733     FunctionOut();
    734 
    735     return ret;
    736 }
    737 
    738 OMX_ERRORTYPE SEC_MFC_EncodeThread(OMX_HANDLETYPE hComponent)
    739 {
    740     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    741     OMX_COMPONENTTYPE     *pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    742     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    743     SEC_H264ENC_HANDLE    *pH264Enc = NULL;
    744 
    745     FunctionIn();
    746 
    747     if (hComponent == NULL) {
    748         ret = OMX_ErrorBadParameter;
    749         goto EXIT;
    750     }
    751 
    752     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    753     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    754 
    755     while (pH264Enc->NBEncThread.bExitEncodeThread == OMX_FALSE) {
    756         SEC_OSAL_SemaphoreWait(pH264Enc->NBEncThread.hEncFrameStart);
    757 
    758         if (pH264Enc->NBEncThread.bExitEncodeThread == OMX_FALSE) {
    759             pH264Enc->hMFCH264Handle.returnCodec = SsbSipMfcEncExe(pH264Enc->hMFCH264Handle.hMFCHandle);
    760             SEC_OSAL_SemaphorePost(pH264Enc->NBEncThread.hEncFrameEnd);
    761         }
    762     }
    763 
    764 EXIT:
    765     FunctionOut();
    766     SEC_OSAL_ThreadExit(NULL);
    767 
    768     return ret;
    769 }
    770 
    771 /* MFC Init */
    772 OMX_ERRORTYPE SEC_MFC_H264Enc_Init(OMX_COMPONENTTYPE *pOMXComponent)
    773 {
    774     OMX_ERRORTYPE              ret = OMX_ErrorNone;
    775     SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    776     SEC_OMX_BASEPORT          *pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    777     SEC_OMX_BASEPORT          *pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    778     SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    779     OMX_PTR                    hMFCHandle = NULL;
    780     OMX_S32                    returnCodec = 0;
    781 
    782     FunctionIn();
    783 
    784     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    785     pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_FALSE;
    786     pSECComponent->bUseFlagEOF = OMX_FALSE;
    787     pSECComponent->bSaveFlagEOS = OMX_FALSE;
    788 
    789     /* MFC(Multi Function Codec) encoder and CMM(Codec Memory Management) driver open */
    790     SSBIP_MFC_BUFFER_TYPE buf_type = CACHE;
    791     hMFCHandle = (OMX_PTR)SsbSipMfcEncOpen(&buf_type);
    792     if (hMFCHandle == NULL) {
    793         ret = OMX_ErrorInsufficientResources;
    794         goto EXIT;
    795     }
    796     pH264Enc->hMFCH264Handle.hMFCHandle = hMFCHandle;
    797 
    798     SsbSipMfcEncSetSize(hMFCHandle, H264_ENC,
    799                         pSECOutputPort->portDefinition.format.video.nFrameWidth,
    800                         pSECOutputPort->portDefinition.format.video.nFrameHeight);
    801 
    802     /* Allocate encoder's input buffer */
    803     returnCodec = SsbSipMfcEncGetInBuf(hMFCHandle, &(pH264Enc->hMFCH264Handle.inputInfo));
    804     if (returnCodec != MFC_RET_OK) {
    805         ret = OMX_ErrorInsufficientResources;
    806         goto EXIT;
    807     }
    808     pH264Enc->MFCEncInputBuffer[0].YPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.YPhyAddr;
    809     pH264Enc->MFCEncInputBuffer[0].CPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.CPhyAddr;
    810     pH264Enc->MFCEncInputBuffer[0].YVirAddr = pH264Enc->hMFCH264Handle.inputInfo.YVirAddr;
    811     pH264Enc->MFCEncInputBuffer[0].CVirAddr = pH264Enc->hMFCH264Handle.inputInfo.CVirAddr;
    812     pH264Enc->MFCEncInputBuffer[0].YBufferSize = pH264Enc->hMFCH264Handle.inputInfo.YSize;
    813     pH264Enc->MFCEncInputBuffer[0].CBufferSize = pH264Enc->hMFCH264Handle.inputInfo.CSize;
    814     pH264Enc->MFCEncInputBuffer[0].YDataSize = 0;
    815     pH264Enc->MFCEncInputBuffer[0].CDataSize = 0;
    816     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.YVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.YVirAddr);
    817     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.CVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.CVirAddr);
    818 
    819     returnCodec = SsbSipMfcEncGetInBuf(hMFCHandle, &(pH264Enc->hMFCH264Handle.inputInfo));
    820     if (returnCodec != MFC_RET_OK) {
    821         ret = OMX_ErrorInsufficientResources;
    822         goto EXIT;
    823     }
    824     pH264Enc->MFCEncInputBuffer[1].YPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.YPhyAddr;
    825     pH264Enc->MFCEncInputBuffer[1].CPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.CPhyAddr;
    826     pH264Enc->MFCEncInputBuffer[1].YVirAddr = pH264Enc->hMFCH264Handle.inputInfo.YVirAddr;
    827     pH264Enc->MFCEncInputBuffer[1].CVirAddr = pH264Enc->hMFCH264Handle.inputInfo.CVirAddr;
    828     pH264Enc->MFCEncInputBuffer[1].YBufferSize = pH264Enc->hMFCH264Handle.inputInfo.YSize;
    829     pH264Enc->MFCEncInputBuffer[1].CBufferSize = pH264Enc->hMFCH264Handle.inputInfo.CSize;
    830     pH264Enc->MFCEncInputBuffer[1].YDataSize = 0;
    831     pH264Enc->MFCEncInputBuffer[1].CDataSize = 0;
    832     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.YVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.YVirAddr);
    833     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.CVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.CVirAddr);
    834 
    835     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr = pH264Enc->MFCEncInputBuffer[0].YPhyAddr;
    836     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr = pH264Enc->MFCEncInputBuffer[0].CPhyAddr;
    837     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YVirAddr = pH264Enc->MFCEncInputBuffer[0].YVirAddr;
    838     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CVirAddr = pH264Enc->MFCEncInputBuffer[0].CVirAddr;
    839     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YSize = pH264Enc->MFCEncInputBuffer[0].YBufferSize;
    840     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CSize = pH264Enc->MFCEncInputBuffer[0].CBufferSize;
    841 
    842     pH264Enc->indexInputBuffer = 0;
    843     pH264Enc->bFirstFrame = OMX_TRUE;
    844 
    845     pH264Enc->NBEncThread.bExitEncodeThread = OMX_FALSE;
    846     pH264Enc->NBEncThread.bEncoderRun = OMX_FALSE;
    847     SEC_OSAL_SemaphoreCreate(&(pH264Enc->NBEncThread.hEncFrameStart));
    848     SEC_OSAL_SemaphoreCreate(&(pH264Enc->NBEncThread.hEncFrameEnd));
    849     if (OMX_ErrorNone == SEC_OSAL_ThreadCreate(&pH264Enc->NBEncThread.hNBEncodeThread,
    850                                                 SEC_MFC_EncodeThread,
    851                                                 pOMXComponent)) {
    852         pH264Enc->hMFCH264Handle.returnCodec = MFC_RET_OK;
    853     }
    854 
    855     SEC_OSAL_Memset(pSECComponent->timeStamp, -19771003, sizeof(OMX_TICKS) * MAX_TIMESTAMP);
    856     SEC_OSAL_Memset(pSECComponent->nFlags, 0, sizeof(OMX_U32) * MAX_FLAGS);
    857     pH264Enc->hMFCH264Handle.indexTimestamp = 0;
    858 
    859 EXIT:
    860     FunctionOut();
    861 
    862     return ret;
    863 }
    864 
    865 /* MFC Terminate */
    866 OMX_ERRORTYPE SEC_MFC_H264Enc_Terminate(OMX_COMPONENTTYPE *pOMXComponent)
    867 {
    868     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    869     SEC_OMX_BASECOMPONENT *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    870     SEC_H264ENC_HANDLE    *pH264Enc = NULL;
    871     OMX_PTR                hMFCHandle = NULL;
    872 
    873     FunctionIn();
    874 
    875     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    876 
    877     if (pH264Enc->NBEncThread.hNBEncodeThread != NULL) {
    878         pH264Enc->NBEncThread.bExitEncodeThread = OMX_TRUE;
    879         SEC_OSAL_SemaphorePost(pH264Enc->NBEncThread.hEncFrameStart);
    880         SEC_OSAL_ThreadTerminate(pH264Enc->NBEncThread.hNBEncodeThread);
    881         pH264Enc->NBEncThread.hNBEncodeThread = NULL;
    882     }
    883 
    884     if(pH264Enc->NBEncThread.hEncFrameEnd != NULL) {
    885         SEC_OSAL_SemaphoreTerminate(pH264Enc->NBEncThread.hEncFrameEnd);
    886         pH264Enc->NBEncThread.hEncFrameEnd = NULL;
    887     }
    888 
    889     if(pH264Enc->NBEncThread.hEncFrameStart != NULL) {
    890         SEC_OSAL_SemaphoreTerminate(pH264Enc->NBEncThread.hEncFrameStart);
    891         pH264Enc->NBEncThread.hEncFrameStart = NULL;
    892     }
    893 
    894     hMFCHandle = pH264Enc->hMFCH264Handle.hMFCHandle;
    895     if (hMFCHandle != NULL) {
    896         SsbSipMfcEncClose(hMFCHandle);
    897         hMFCHandle = pH264Enc->hMFCH264Handle.hMFCHandle = NULL;
    898     }
    899 
    900 EXIT:
    901     FunctionOut();
    902 
    903     return ret;
    904 }
    905 
    906 OMX_ERRORTYPE SEC_MFC_H264_Encode(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
    907 {
    908     OMX_ERRORTYPE              ret = OMX_ErrorNone;
    909     SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    910     SEC_H264ENC_HANDLE        *pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    911     SSBSIP_MFC_ENC_INPUT_INFO *pInputInfo = &pH264Enc->hMFCH264Handle.inputInfo;
    912     SSBSIP_MFC_ENC_OUTPUT_INFO outputInfo;
    913     SEC_OMX_BASEPORT          *pSECPort = NULL;
    914     MFC_ENC_ADDR_INFO          addrInfo;
    915     OMX_U32                    oneFrameSize = pInputData->dataLen;
    916 
    917     FunctionIn();
    918 
    919     if (pH264Enc->hMFCH264Handle.bConfiguredMFC == OMX_FALSE) {
    920         Set_H264ENC_Param(&(pH264Enc->hMFCH264Handle.mfcVideoAvc), pSECComponent);
    921         pH264Enc->hMFCH264Handle.returnCodec = SsbSipMfcEncInit(pH264Enc->hMFCH264Handle.hMFCHandle, &(pH264Enc->hMFCH264Handle.mfcVideoAvc));
    922         if (pH264Enc->hMFCH264Handle.returnCodec != MFC_RET_OK) {
    923             ret = OMX_ErrorInsufficientResources;
    924             goto EXIT;
    925         }
    926 
    927         pH264Enc->hMFCH264Handle.returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
    928         if (pH264Enc->hMFCH264Handle.returnCodec != MFC_RET_OK)
    929         {
    930             SEC_OSAL_Log(SEC_LOG_TRACE, "%s - SsbSipMfcEncGetOutBuf Failed\n", __func__);
    931             ret = OMX_ErrorUndefined;
    932             goto EXIT;
    933         } else {
    934             char *p = NULL;
    935             int iSpsSize = 0;
    936             int iPpsSize = 0;
    937 
    938             p = FindDelimiter((OMX_U8 *)outputInfo.StrmVirAddr + 4, outputInfo.headerSize - 4);
    939 
    940             iSpsSize = (unsigned int)p - (unsigned int)outputInfo.StrmVirAddr;
    941             pH264Enc->hMFCH264Handle.headerData.pHeaderSPS = (OMX_PTR)outputInfo.StrmVirAddr;
    942             pH264Enc->hMFCH264Handle.headerData.SPSLen = iSpsSize;
    943 
    944             iPpsSize = outputInfo.headerSize - iSpsSize;
    945             pH264Enc->hMFCH264Handle.headerData.pHeaderPPS = outputInfo.StrmVirAddr + iSpsSize;
    946             pH264Enc->hMFCH264Handle.headerData.PPSLen = iPpsSize;
    947         }
    948 
    949         /* SEC_OSAL_Memcpy((void*)(pOutputData->dataBuffer), (const void*)(outputInfo.StrmVirAddr), outputInfo.headerSize); */
    950         pOutputData->dataBuffer = outputInfo.StrmVirAddr;
    951         pOutputData->allocSize = outputInfo.headerSize;
    952         pOutputData->dataLen = outputInfo.headerSize;
    953         pOutputData->timeStamp = 0;
    954         pOutputData->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
    955         pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
    956 
    957         pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_TRUE;
    958 
    959         ret = OMX_ErrorInputDataEncodeYet;
    960         goto EXIT;
    961     }
    962 
    963     if ((pInputData->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) &&
    964         (pSECComponent->bUseFlagEOF == OMX_FALSE)) {
    965         pSECComponent->bUseFlagEOF = OMX_TRUE;
    966     }
    967 
    968     if (oneFrameSize <= 0) {
    969         pOutputData->timeStamp = pInputData->timeStamp;
    970         pOutputData->nFlags = pInputData->nFlags;
    971 
    972         ret = OMX_ErrorNone;
    973         goto EXIT;
    974     }
    975 
    976     pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    977     if (((pInputData->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS) ||
    978         (pSECComponent->getAllDelayBuffer == OMX_TRUE)){
    979         /* Dummy input data for get out encoded last frame */
    980         pInputInfo->YPhyAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].YPhyAddr;
    981         pInputInfo->CPhyAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].CPhyAddr;
    982         pInputInfo->YVirAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].YVirAddr;
    983         pInputInfo->CVirAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].CVirAddr;
    984     } else if (pSECPort->portDefinition.format.video.eColorFormat == OMX_SEC_COLOR_FormatNV12TPhysicalAddress) {
    985         SEC_OSAL_Memcpy(&addrInfo.pAddrY, pInputData->dataBuffer, sizeof(addrInfo.pAddrY));
    986         SEC_OSAL_Memcpy(&addrInfo.pAddrC, pInputData->dataBuffer + sizeof(addrInfo.pAddrY), sizeof(addrInfo.pAddrC));
    987         pInputInfo->YPhyAddr = addrInfo.pAddrY;
    988         pInputInfo->CPhyAddr = addrInfo.pAddrC;
    989 #ifdef USE_ANDROID_EXTENSION
    990     } else if (pSECPort->bStoreMetaDataInBuffer != OMX_FALSE) {
    991         ret = preprocessMetaDataInBuffers(pOMXComponent, pInputData->dataBuffer, pInputInfo);
    992         if (ret != OMX_ErrorNone)
    993             goto EXIT;
    994 #endif
    995     } else {
    996         /* Real input data */
    997         pInputInfo->YPhyAddr = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr;
    998         pInputInfo->CPhyAddr = pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr;
    999     }
   1000 
   1001     pSECComponent->timeStamp[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->timeStamp;
   1002     pSECComponent->nFlags[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->nFlags;
   1003 
   1004     if ((pH264Enc->hMFCH264Handle.returnCodec == MFC_RET_OK) &&
   1005         (pH264Enc->bFirstFrame == OMX_FALSE)) {
   1006         OMX_S32 indexTimestamp = 0;
   1007 
   1008         /* wait for mfc encode done */
   1009         if (pH264Enc->NBEncThread.bEncoderRun != OMX_FALSE) {
   1010             SEC_OSAL_SemaphoreWait(pH264Enc->NBEncThread.hEncFrameEnd);
   1011             pH264Enc->NBEncThread.bEncoderRun = OMX_FALSE;
   1012         }
   1013 
   1014         pH264Enc->hMFCH264Handle.returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
   1015         if ((SsbSipMfcEncGetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_GETCONF_FRAME_TAG, &indexTimestamp) != MFC_RET_OK) ||
   1016             (((indexTimestamp < 0) || (indexTimestamp >= MAX_TIMESTAMP)))){
   1017             pOutputData->timeStamp = pInputData->timeStamp;
   1018             pOutputData->nFlags = pInputData->nFlags;
   1019         } else {
   1020             pOutputData->timeStamp = pSECComponent->timeStamp[indexTimestamp];
   1021             pOutputData->nFlags = pSECComponent->nFlags[indexTimestamp];
   1022         }
   1023 
   1024         if (pH264Enc->hMFCH264Handle.returnCodec == MFC_RET_OK) {
   1025             /** Fill Output Buffer **/
   1026             pOutputData->dataBuffer = outputInfo.StrmVirAddr;
   1027             pOutputData->allocSize = outputInfo.dataSize;
   1028             pOutputData->dataLen = outputInfo.dataSize;
   1029             pOutputData->usedDataLen = 0;
   1030 
   1031             pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
   1032             if (outputInfo.frameType == MFC_FRAME_TYPE_I_FRAME)
   1033                     pOutputData->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
   1034 
   1035             SEC_OSAL_Log(SEC_LOG_TRACE, "MFC Encode OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
   1036 
   1037             ret = OMX_ErrorNone;
   1038         } else {
   1039             SEC_OSAL_Log(SEC_LOG_ERROR, "%s: SsbSipMfcEncGetOutBuf failed, ret:%d", __FUNCTION__, pH264Enc->hMFCH264Handle.returnCodec);
   1040             ret = OMX_ErrorUndefined;
   1041             goto EXIT;
   1042         }
   1043 
   1044         if (pSECComponent->getAllDelayBuffer == OMX_TRUE) {
   1045             ret = OMX_ErrorInputDataEncodeYet;
   1046         }
   1047         if ((pInputData->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS) {
   1048             pInputData->nFlags = (pOutputData->nFlags & (~OMX_BUFFERFLAG_EOS));
   1049             pSECComponent->getAllDelayBuffer = OMX_TRUE;
   1050             ret = OMX_ErrorInputDataEncodeYet;
   1051         }
   1052         if ((pOutputData->nFlags & OMX_BUFFERFLAG_EOS) == OMX_BUFFERFLAG_EOS) {
   1053             pSECComponent->getAllDelayBuffer = OMX_FALSE;
   1054             pOutputData->dataLen = 0;
   1055             pOutputData->usedDataLen = 0;
   1056             SEC_OSAL_Log(SEC_LOG_TRACE, "OMX_BUFFERFLAG_EOS!!!");
   1057             ret = OMX_ErrorNone;
   1058         }
   1059     }
   1060     if (pH264Enc->hMFCH264Handle.returnCodec != MFC_RET_OK) {
   1061         SEC_OSAL_Log(SEC_LOG_ERROR, "In %s : SsbSipMfcEncExe Failed!!!\n", __func__);
   1062         ret = OMX_ErrorUndefined;
   1063     }
   1064 
   1065     pH264Enc->hMFCH264Handle.returnCodec = SsbSipMfcEncSetInBuf(pH264Enc->hMFCH264Handle.hMFCHandle, pInputInfo);
   1066     if (pH264Enc->hMFCH264Handle.returnCodec != MFC_RET_OK) {
   1067         SEC_OSAL_Log(SEC_LOG_TRACE, "Error : SsbSipMfcEncSetInBuf() \n");
   1068         ret = OMX_ErrorUndefined;
   1069         goto EXIT;
   1070     } else {
   1071         pH264Enc->indexInputBuffer++;
   1072         pH264Enc->indexInputBuffer %= MFC_INPUT_BUFFER_NUM_MAX;
   1073         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].YPhyAddr;
   1074         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].CPhyAddr;
   1075         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YVirAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].YVirAddr;
   1076         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CVirAddr = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].CVirAddr;
   1077         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YSize = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].YBufferSize;
   1078         pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CSize = pH264Enc->MFCEncInputBuffer[pH264Enc->indexInputBuffer].CBufferSize;
   1079     }
   1080 
   1081     SsbSipMfcEncSetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_SETCONF_FRAME_TAG, &(pH264Enc->hMFCH264Handle.indexTimestamp));
   1082 
   1083     /* mfc encode start */
   1084     SEC_OSAL_SemaphorePost(pH264Enc->NBEncThread.hEncFrameStart);
   1085     pH264Enc->NBEncThread.bEncoderRun = OMX_TRUE;
   1086     pH264Enc->hMFCH264Handle.indexTimestamp++;
   1087     pH264Enc->hMFCH264Handle.indexTimestamp %= MAX_TIMESTAMP;
   1088     pH264Enc->bFirstFrame = OMX_FALSE;
   1089 
   1090 EXIT:
   1091     FunctionOut();
   1092 
   1093     return ret;
   1094 }
   1095 
   1096 /* MFC Encode */
   1097 OMX_ERRORTYPE SEC_MFC_H264Enc_bufferProcess(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
   1098 {
   1099     OMX_ERRORTYPE ret = OMX_ErrorNone;
   1100     SEC_OMX_BASECOMPONENT   *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
   1101     SEC_H264ENC_HANDLE      *pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
   1102     SEC_OMX_BASEPORT        *pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
   1103     SEC_OMX_BASEPORT        *pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
   1104     OMX_BOOL                 endOfFrame = OMX_FALSE;
   1105     OMX_BOOL                 flagEOS = OMX_FALSE;
   1106 
   1107     FunctionIn();
   1108 
   1109     if ((!CHECK_PORT_ENABLED(pSECInputPort)) || (!CHECK_PORT_ENABLED(pSECOutputPort)) ||
   1110             (!CHECK_PORT_POPULATED(pSECInputPort)) || (!CHECK_PORT_POPULATED(pSECOutputPort))) {
   1111         ret = OMX_ErrorNone;
   1112         goto EXIT;
   1113     }
   1114     if (OMX_FALSE == SEC_Check_BufferProcess_State(pSECComponent)) {
   1115         ret = OMX_ErrorNone;
   1116         goto EXIT;
   1117     }
   1118 
   1119     ret = SEC_MFC_H264_Encode(pOMXComponent, pInputData, pOutputData);
   1120     if (ret != OMX_ErrorNone) {
   1121         if (ret == OMX_ErrorInputDataEncodeYet) {
   1122             pOutputData->usedDataLen = 0;
   1123             pOutputData->remainDataLen = pOutputData->dataLen;
   1124         } else {
   1125             pSECComponent->pCallbacks->EventHandler((OMX_HANDLETYPE)pOMXComponent,
   1126                                             pSECComponent->callbackData,
   1127                                             OMX_EventError, ret, 0, NULL);
   1128         }
   1129     } else {
   1130         pInputData->usedDataLen += pInputData->dataLen;
   1131         pInputData->remainDataLen = pInputData->dataLen - pInputData->usedDataLen;
   1132         pInputData->dataLen -= pInputData->usedDataLen;
   1133         pInputData->usedDataLen = 0;
   1134 
   1135         /* pOutputData->usedDataLen = 0; */
   1136         pOutputData->remainDataLen = pOutputData->dataLen - pOutputData->usedDataLen;
   1137     }
   1138 
   1139 EXIT:
   1140     FunctionOut();
   1141 
   1142     return ret;
   1143 }
   1144 
   1145 OSCL_EXPORT_REF OMX_ERRORTYPE SEC_OMX_ComponentInit(OMX_HANDLETYPE hComponent, OMX_STRING componentName)
   1146 {
   1147     OMX_ERRORTYPE            ret = OMX_ErrorNone;
   1148     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
   1149     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
   1150     SEC_OMX_BASEPORT        *pSECPort = NULL;
   1151     SEC_H264ENC_HANDLE      *pH264Enc = NULL;
   1152     int i = 0;
   1153 
   1154     FunctionIn();
   1155 
   1156     if ((hComponent == NULL) || (componentName == NULL)) {
   1157         ret = OMX_ErrorBadParameter;
   1158         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
   1159         goto EXIT;
   1160     }
   1161     if (SEC_OSAL_Strcmp(SEC_OMX_COMPONENT_H264_ENC, componentName) != 0) {
   1162         ret = OMX_ErrorBadParameter;
   1163         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, componentName:%s, Line:%d", componentName, __LINE__);
   1164         goto EXIT;
   1165     }
   1166 
   1167     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
   1168     ret = SEC_OMX_VideoEncodeComponentInit(pOMXComponent);
   1169     if (ret != OMX_ErrorNone) {
   1170         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_Error, Line:%d", __LINE__);
   1171         goto EXIT;
   1172     }
   1173     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
   1174     pSECComponent->codecType = HW_VIDEO_CODEC;
   1175 
   1176     pSECComponent->componentName = (OMX_STRING)SEC_OSAL_Malloc(MAX_OMX_COMPONENT_NAME_SIZE);
   1177     if (pSECComponent->componentName == NULL) {
   1178         SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
   1179         ret = OMX_ErrorInsufficientResources;
   1180         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
   1181         goto EXIT;
   1182     }
   1183     SEC_OSAL_Memset(pSECComponent->componentName, 0, MAX_OMX_COMPONENT_NAME_SIZE);
   1184 
   1185     pH264Enc = SEC_OSAL_Malloc(sizeof(SEC_H264ENC_HANDLE));
   1186     if (pH264Enc == NULL) {
   1187         SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
   1188         ret = OMX_ErrorInsufficientResources;
   1189         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
   1190         goto EXIT;
   1191     }
   1192     SEC_OSAL_Memset(pH264Enc, 0, sizeof(SEC_H264ENC_HANDLE));
   1193     pSECComponent->hCodecHandle = (OMX_HANDLETYPE)pH264Enc;
   1194 
   1195     SEC_OSAL_Strcpy(pSECComponent->componentName, SEC_OMX_COMPONENT_H264_ENC);
   1196     /* Set componentVersion */
   1197     pSECComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
   1198     pSECComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
   1199     pSECComponent->componentVersion.s.nRevision     = REVISION_NUMBER;
   1200     pSECComponent->componentVersion.s.nStep         = STEP_NUMBER;
   1201     /* Set specVersion */
   1202     pSECComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
   1203     pSECComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
   1204     pSECComponent->specVersion.s.nRevision     = REVISION_NUMBER;
   1205     pSECComponent->specVersion.s.nStep         = STEP_NUMBER;
   1206 
   1207     /* Android CapabilityFlags */
   1208     pSECComponent->capabilityFlags.iIsOMXComponentMultiThreaded                   = OMX_TRUE;
   1209     pSECComponent->capabilityFlags.iOMXComponentSupportsExternalInputBufferAlloc  = OMX_TRUE;
   1210     pSECComponent->capabilityFlags.iOMXComponentSupportsExternalOutputBufferAlloc = OMX_TRUE;
   1211     pSECComponent->capabilityFlags.iOMXComponentSupportsMovableInputBuffers       = OMX_FALSE;
   1212     pSECComponent->capabilityFlags.iOMXComponentSupportsPartialFrames             = OMX_FALSE;
   1213     pSECComponent->capabilityFlags.iOMXComponentUsesNALStartCodes                 = OMX_TRUE;
   1214     pSECComponent->capabilityFlags.iOMXComponentCanHandleIncompleteFrames         = OMX_TRUE;
   1215     pSECComponent->capabilityFlags.iOMXComponentUsesFullAVCFrames                 = OMX_TRUE;
   1216 
   1217     /* Input port */
   1218     pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
   1219     pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
   1220     pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
   1221     pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
   1222     pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_INPUT_BUFFER_SIZE;
   1223     pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
   1224     SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
   1225     SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "raw/video");
   1226     pSECPort->portDefinition.format.video.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
   1227     pSECPort->portDefinition.bEnabled = OMX_TRUE;
   1228 
   1229     /* Output port */
   1230     pSECPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
   1231     pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
   1232     pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
   1233     pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
   1234     pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
   1235     pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
   1236     SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
   1237     SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "video/avc");
   1238     pSECPort->portDefinition.format.video.eColorFormat = OMX_COLOR_FormatUnused;
   1239     pSECPort->portDefinition.bEnabled = OMX_TRUE;
   1240 
   1241     for(i = 0; i < ALL_PORT_NUM; i++) {
   1242         INIT_SET_SIZE_VERSION(&pH264Enc->AVCComponent[i], OMX_VIDEO_PARAM_AVCTYPE);
   1243         pH264Enc->AVCComponent[i].nPortIndex = i;
   1244         pH264Enc->AVCComponent[i].eProfile   = OMX_VIDEO_AVCProfileBaseline;
   1245         pH264Enc->AVCComponent[i].eLevel     = OMX_VIDEO_AVCLevel31;
   1246     }
   1247 
   1248     pOMXComponent->GetParameter      = &SEC_MFC_H264Enc_GetParameter;
   1249     pOMXComponent->SetParameter      = &SEC_MFC_H264Enc_SetParameter;
   1250     pOMXComponent->GetConfig         = &SEC_MFC_H264Enc_GetConfig;
   1251     pOMXComponent->SetConfig         = &SEC_MFC_H264Enc_SetConfig;
   1252     pOMXComponent->GetExtensionIndex = &SEC_MFC_H264Enc_GetExtensionIndex;
   1253     pOMXComponent->ComponentRoleEnum = &SEC_MFC_H264Enc_ComponentRoleEnum;
   1254     pOMXComponent->ComponentDeInit   = &SEC_OMX_ComponentDeinit;
   1255 
   1256     pSECComponent->sec_mfc_componentInit      = &SEC_MFC_H264Enc_Init;
   1257     pSECComponent->sec_mfc_componentTerminate = &SEC_MFC_H264Enc_Terminate;
   1258     pSECComponent->sec_mfc_bufferProcess      = &SEC_MFC_H264Enc_bufferProcess;
   1259     pSECComponent->sec_checkInputFrame        = NULL;
   1260 
   1261     pSECComponent->currentState = OMX_StateLoaded;
   1262 
   1263     ret = OMX_ErrorNone;
   1264 
   1265 EXIT:
   1266     FunctionOut();
   1267 
   1268     return ret;
   1269 }
   1270 
   1271 OMX_ERRORTYPE SEC_OMX_ComponentDeinit(OMX_HANDLETYPE hComponent)
   1272 {
   1273     OMX_ERRORTYPE            ret = OMX_ErrorNone;
   1274     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
   1275     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
   1276     SEC_H264ENC_HANDLE      *pH264Enc = NULL;
   1277 
   1278     FunctionIn();
   1279 
   1280     if (hComponent == NULL) {
   1281         ret = OMX_ErrorBadParameter;
   1282         goto EXIT;
   1283     }
   1284     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
   1285     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
   1286 
   1287     SEC_OSAL_Free(pSECComponent->componentName);
   1288     pSECComponent->componentName = NULL;
   1289 
   1290     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
   1291     if (pH264Enc != NULL) {
   1292         SEC_OSAL_Free(pH264Enc);
   1293         pH264Enc = pSECComponent->hCodecHandle = NULL;
   1294     }
   1295 
   1296     ret = SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
   1297     if (ret != OMX_ErrorNone) {
   1298         goto EXIT;
   1299     }
   1300 
   1301     ret = OMX_ErrorNone;
   1302 
   1303 EXIT:
   1304     FunctionOut();
   1305 
   1306     return ret;
   1307 }
   1308