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 
     39 #undef  SEC_LOG_TAG
     40 #define SEC_LOG_TAG    "SEC_H264_ENC"
     41 #define SEC_LOG_OFF
     42 #include "SEC_OSAL_Log.h"
     43 
     44 
     45 /* H.264 Encoder Supported Levels & profiles */
     46 SEC_OMX_VIDEO_PROFILELEVEL supportedAVCProfileLevels[] ={
     47     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1},
     48     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel1b},
     49     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel11},
     50     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel12},
     51     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel13},
     52     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel2},
     53     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel21},
     54     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel22},
     55     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel3},
     56     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel31},
     57     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel32},
     58     {OMX_VIDEO_AVCProfileBaseline, OMX_VIDEO_AVCLevel4},
     59 
     60     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1},
     61     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel1b},
     62     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel11},
     63     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel12},
     64     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel13},
     65     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel2},
     66     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel21},
     67     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel22},
     68     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel3},
     69     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel31},
     70     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel32},
     71     {OMX_VIDEO_AVCProfileMain, OMX_VIDEO_AVCLevel4},
     72 
     73     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel1},
     74     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel1b},
     75     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel11},
     76     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel12},
     77     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel13},
     78     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel2},
     79     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel21},
     80     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel22},
     81     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel3},
     82     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel31},
     83     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel32},
     84     {OMX_VIDEO_AVCProfileHigh, OMX_VIDEO_AVCLevel4}};
     85 
     86 
     87 OMX_U32 OMXAVCProfileToProfileIDC(OMX_VIDEO_AVCPROFILETYPE profile)
     88 {
     89     OMX_U32 ret = 0; //default OMX_VIDEO_AVCProfileMain
     90 
     91     if (profile == OMX_VIDEO_AVCProfileMain)
     92         ret = 0;
     93     else if (profile == OMX_VIDEO_AVCProfileHigh)
     94         ret = 1;
     95     else if (profile == OMX_VIDEO_AVCProfileBaseline)
     96         ret = 2;
     97 
     98     return ret;
     99 }
    100 
    101 OMX_U32 OMXAVCLevelToLevelIDC(OMX_VIDEO_AVCLEVELTYPE level)
    102 {
    103     OMX_U32 ret = 40; //default OMX_VIDEO_AVCLevel4
    104 
    105     if (level == OMX_VIDEO_AVCLevel1)
    106         ret = 10;
    107     else if (level == OMX_VIDEO_AVCLevel1b)
    108         ret = 9;
    109     else if (level == OMX_VIDEO_AVCLevel11)
    110         ret = 11;
    111     else if (level == OMX_VIDEO_AVCLevel12)
    112         ret = 12;
    113     else if (level == OMX_VIDEO_AVCLevel13)
    114         ret = 13;
    115     else if (level == OMX_VIDEO_AVCLevel2)
    116         ret = 20;
    117     else if (level == OMX_VIDEO_AVCLevel21)
    118         ret = 21;
    119     else if (level == OMX_VIDEO_AVCLevel22)
    120         ret = 22;
    121     else if (level == OMX_VIDEO_AVCLevel3)
    122         ret = 30;
    123     else if (level == OMX_VIDEO_AVCLevel31)
    124         ret = 31;
    125     else if (level == OMX_VIDEO_AVCLevel32)
    126         ret = 32;
    127     else if (level == OMX_VIDEO_AVCLevel4)
    128         ret = 40;
    129 
    130     return ret;
    131 }
    132 
    133 OMX_U8 *FindDelimiter(OMX_U8 *pBuffer, OMX_U32 size)
    134 {
    135     int i;
    136 
    137     for (i = 0; i < size - 3; i++) {
    138         if ((pBuffer[i] == 0x00)   &&
    139             (pBuffer[i+1] == 0x00) &&
    140             (pBuffer[i+2] == 0x00) &&
    141             (pBuffer[i+3] == 0x01))
    142             return (pBuffer + i);
    143     }
    144 
    145     return NULL;
    146 }
    147 
    148 void H264PrintParams(SSBSIP_MFC_ENC_H264_PARAM h264Arg)
    149 {
    150     SEC_OSAL_Log(SEC_LOG_TRACE, "SourceWidth             : %d\n", h264Arg.SourceWidth);
    151     SEC_OSAL_Log(SEC_LOG_TRACE, "SourceHeight            : %d\n", h264Arg.SourceHeight);
    152     SEC_OSAL_Log(SEC_LOG_TRACE, "ProfileIDC              : %d\n", h264Arg.ProfileIDC);
    153     SEC_OSAL_Log(SEC_LOG_TRACE, "LevelIDC                : %d\n", h264Arg.LevelIDC);
    154     SEC_OSAL_Log(SEC_LOG_TRACE, "IDRPeriod               : %d\n", h264Arg.IDRPeriod);
    155     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberReferenceFrames   : %d\n", h264Arg.NumberReferenceFrames);
    156     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberRefForPframes     : %d\n", h264Arg.NumberRefForPframes);
    157     SEC_OSAL_Log(SEC_LOG_TRACE, "SliceMode               : %d\n", h264Arg.SliceMode);
    158     SEC_OSAL_Log(SEC_LOG_TRACE, "SliceArgument           : %d\n", h264Arg.SliceArgument);
    159     SEC_OSAL_Log(SEC_LOG_TRACE, "NumberBFrames           : %d\n", h264Arg.NumberBFrames);
    160     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterDisable       : %d\n", h264Arg.LoopFilterDisable);
    161     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterAlphaC0Offset : %d\n", h264Arg.LoopFilterAlphaC0Offset);
    162     SEC_OSAL_Log(SEC_LOG_TRACE, "LoopFilterBetaOffset    : %d\n", h264Arg.LoopFilterBetaOffset);
    163     SEC_OSAL_Log(SEC_LOG_TRACE, "SymbolMode              : %d\n", h264Arg.SymbolMode);
    164     SEC_OSAL_Log(SEC_LOG_TRACE, "PictureInterlace        : %d\n", h264Arg.PictureInterlace);
    165     SEC_OSAL_Log(SEC_LOG_TRACE, "Transform8x8Mode        : %d\n", h264Arg.Transform8x8Mode);
    166     SEC_OSAL_Log(SEC_LOG_TRACE, "RandomIntraMBRefresh    : %d\n", h264Arg.RandomIntraMBRefresh);
    167     SEC_OSAL_Log(SEC_LOG_TRACE, "PadControlOn            : %d\n", h264Arg.PadControlOn);
    168     SEC_OSAL_Log(SEC_LOG_TRACE, "LumaPadVal              : %d\n", h264Arg.LumaPadVal);
    169     SEC_OSAL_Log(SEC_LOG_TRACE, "CbPadVal                : %d\n", h264Arg.CbPadVal);
    170     SEC_OSAL_Log(SEC_LOG_TRACE, "CrPadVal                : %d\n", h264Arg.CrPadVal);
    171     SEC_OSAL_Log(SEC_LOG_TRACE, "EnableFRMRateControl    : %d\n", h264Arg.EnableFRMRateControl);
    172     SEC_OSAL_Log(SEC_LOG_TRACE, "EnableMBRateControl     : %d\n", h264Arg.EnableMBRateControl);
    173     SEC_OSAL_Log(SEC_LOG_TRACE, "FrameRate               : %d\n", h264Arg.FrameRate);
    174     SEC_OSAL_Log(SEC_LOG_TRACE, "Bitrate                 : %d\n", h264Arg.Bitrate);
    175     SEC_OSAL_Log(SEC_LOG_TRACE, "FrameQp                 : %d\n", h264Arg.FrameQp);
    176     SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMax               : %d\n", h264Arg.QSCodeMax);
    177     SEC_OSAL_Log(SEC_LOG_TRACE, "QSCodeMin               : %d\n", h264Arg.QSCodeMin);
    178     SEC_OSAL_Log(SEC_LOG_TRACE, "CBRPeriodRf             : %d\n", h264Arg.CBRPeriodRf);
    179     SEC_OSAL_Log(SEC_LOG_TRACE, "DarkDisable             : %d\n", h264Arg.DarkDisable);
    180     SEC_OSAL_Log(SEC_LOG_TRACE, "SmoothDisable           : %d\n", h264Arg.SmoothDisable);
    181     SEC_OSAL_Log(SEC_LOG_TRACE, "StaticDisable           : %d\n", h264Arg.StaticDisable);
    182     SEC_OSAL_Log(SEC_LOG_TRACE, "ActivityDisable         : %d\n", h264Arg.ActivityDisable);
    183 }
    184 
    185 void Set_H264ENC_Param(SSBSIP_MFC_ENC_H264_PARAM *pH264Arg, SEC_OMX_BASECOMPONENT *pSECComponent)
    186 {
    187     SEC_OMX_BASEPORT          *pSECInputPort = NULL;
    188     SEC_OMX_BASEPORT          *pSECOutputPort = NULL;
    189     SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    190 
    191     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    192     pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    193     pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    194 
    195     pH264Arg->codecType    = H264_ENC;
    196     pH264Arg->SourceWidth  = pSECOutputPort->portDefinition.format.video.nFrameWidth;
    197     pH264Arg->SourceHeight = pSECOutputPort->portDefinition.format.video.nFrameHeight;
    198     pH264Arg->IDRPeriod    = pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].nPFrames + 1;
    199     pH264Arg->SliceMode    = 0;
    200     pH264Arg->RandomIntraMBRefresh = 0;
    201     pH264Arg->EnableFRMRateControl = 1;        // 0: Disable, 1: Frame level RC
    202     pH264Arg->Bitrate      = pSECOutputPort->portDefinition.format.video.nBitrate;
    203     pH264Arg->FrameQp      = 20;
    204     pH264Arg->FrameQp_P    = 20;
    205     pH264Arg->QSCodeMax    = 30;
    206     pH264Arg->QSCodeMin    = 10;
    207     pH264Arg->CBRPeriodRf  = 100;
    208     pH264Arg->PadControlOn = 0;             // 0: disable, 1: enable
    209     pH264Arg->LumaPadVal   = 0;
    210     pH264Arg->CbPadVal     = 0;
    211     pH264Arg->CrPadVal     = 0;
    212 
    213     pH264Arg->ProfileIDC   = OMXAVCProfileToProfileIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eProfile); //0;  //(OMX_VIDEO_AVCProfileMain)
    214     pH264Arg->LevelIDC     = OMXAVCLevelToLevelIDC(pH264Enc->AVCComponent[OUTPUT_PORT_INDEX].eLevel);       //40; //(OMX_VIDEO_AVCLevel4)
    215     pH264Arg->FrameQp_B    = 20;
    216     pH264Arg->FrameRate    = (pSECInputPort->portDefinition.format.video.xFramerate) >> 16;
    217     pH264Arg->SliceArgument = 0;          // Slice mb/byte size number
    218     pH264Arg->NumberBFrames = 0;            // 0 ~ 2
    219     pH264Arg->NumberReferenceFrames = 1;
    220     pH264Arg->NumberRefForPframes   = 1;
    221     pH264Arg->LoopFilterDisable     = 1;    // 1: Loop Filter Disable, 0: Filter Enable
    222     pH264Arg->LoopFilterAlphaC0Offset = 0;
    223     pH264Arg->LoopFilterBetaOffset    = 0;
    224     pH264Arg->SymbolMode       = 0;         // 0: CAVLC, 1: CABAC
    225     pH264Arg->PictureInterlace = 0;
    226     pH264Arg->Transform8x8Mode = 0;         // 0: 4x4, 1: allow 8x8
    227     pH264Arg->EnableMBRateControl = 0;        // 0: Disable, 1:MB level RC
    228     pH264Arg->DarkDisable     = 1;
    229     pH264Arg->SmoothDisable   = 1;
    230     pH264Arg->StaticDisable   = 1;
    231     pH264Arg->ActivityDisable = 1;
    232 
    233     SEC_OSAL_Log(SEC_LOG_TRACE, "pSECPort->eControlRate: 0x%x", pSECOutputPort->eControlRate);
    234     switch (pSECOutputPort->eControlRate) {
    235     case OMX_Video_ControlRateDisable:
    236         /* TBD */
    237         break;
    238     case OMX_Video_ControlRateVariable:
    239         /* TBD */
    240         break;
    241     default:
    242         break;
    243     }
    244 
    245     H264PrintParams(*pH264Arg);
    246 }
    247 
    248 OMX_ERRORTYPE SEC_MFC_H264Enc_GetParameter(
    249     OMX_IN OMX_HANDLETYPE hComponent,
    250     OMX_IN OMX_INDEXTYPE  nParamIndex,
    251     OMX_INOUT OMX_PTR     pComponentParameterStructure)
    252 {
    253     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    254     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    255     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    256 
    257     FunctionIn();
    258 
    259     if (hComponent == NULL || pComponentParameterStructure == NULL) {
    260         ret = OMX_ErrorBadParameter;
    261         goto EXIT;
    262     }
    263     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    264     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    265     if (ret != OMX_ErrorNone) {
    266         goto EXIT;
    267     }
    268     if (pOMXComponent->pComponentPrivate == NULL) {
    269         ret = OMX_ErrorBadParameter;
    270         goto EXIT;
    271     }
    272 
    273     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    274     if (pSECComponent->currentState == OMX_StateInvalid ) {
    275         ret = OMX_StateInvalid;
    276         goto EXIT;
    277     }
    278 
    279     switch (nParamIndex) {
    280     case OMX_IndexParamVideoAvc:
    281     {
    282         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = (OMX_VIDEO_PARAM_AVCTYPE *)pComponentParameterStructure;
    283         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = NULL;
    284         SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    285 
    286         ret = SEC_OMX_Check_SizeVersion(pDstAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    287         if (ret != OMX_ErrorNone) {
    288             goto EXIT;
    289         }
    290 
    291         if (pDstAVCComponent->nPortIndex >= ALL_PORT_NUM) {
    292             ret = OMX_ErrorBadPortIndex;
    293             goto EXIT;
    294         }
    295 
    296         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    297         pSrcAVCComponent = &pH264Enc->AVCComponent[pDstAVCComponent->nPortIndex];
    298 
    299         SEC_OSAL_Memcpy(pDstAVCComponent, pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    300     }
    301         break;
    302     case OMX_IndexParamStandardComponentRole:
    303     {
    304         OMX_PARAM_COMPONENTROLETYPE *pComponentRole = (OMX_PARAM_COMPONENTROLETYPE *)pComponentParameterStructure;
    305         ret = SEC_OMX_Check_SizeVersion(pComponentRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
    306         if (ret != OMX_ErrorNone) {
    307             goto EXIT;
    308         }
    309 
    310         SEC_OSAL_Strcpy((char *)pComponentRole->cRole, SEC_OMX_COMPOMENT_H264_ENC_ROLE);
    311     }
    312         break;
    313     case OMX_IndexParamVideoProfileLevelQuerySupported:
    314     {
    315         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pDstProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)pComponentParameterStructure;
    316         SEC_OMX_VIDEO_PROFILELEVEL *pProfileLevel = NULL;
    317         OMX_U32 maxProfileLevelNum = 0;
    318 
    319         ret = SEC_OMX_Check_SizeVersion(pDstProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    320         if (ret != OMX_ErrorNone) {
    321             goto EXIT;
    322         }
    323 
    324         if (pDstProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    325             ret = OMX_ErrorBadPortIndex;
    326             goto EXIT;
    327         }
    328 
    329         pProfileLevel = supportedAVCProfileLevels;
    330         maxProfileLevelNum = sizeof(supportedAVCProfileLevels) / sizeof(SEC_OMX_VIDEO_PROFILELEVEL);
    331 
    332         if (pDstProfileLevel->nProfileIndex >= maxProfileLevelNum) {
    333             ret = OMX_ErrorNoMore;
    334             goto EXIT;
    335         }
    336 
    337         pProfileLevel += pDstProfileLevel->nProfileIndex;
    338         pDstProfileLevel->eProfile = pProfileLevel->profile;
    339         pDstProfileLevel->eLevel = pProfileLevel->level;
    340     }
    341         break;
    342     case OMX_IndexParamVideoProfileLevelCurrent:
    343     {
    344         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pDstProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)pComponentParameterStructure;
    345         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = NULL;
    346         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    347 
    348         ret = SEC_OMX_Check_SizeVersion(pDstProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    349         if (ret != OMX_ErrorNone) {
    350             goto EXIT;
    351         }
    352 
    353         if (pDstProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    354             ret = OMX_ErrorBadPortIndex;
    355             goto EXIT;
    356         }
    357 
    358         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    359         pSrcAVCComponent = &pH264Enc->AVCComponent[pDstProfileLevel->nPortIndex];
    360 
    361         pDstProfileLevel->eProfile = pSrcAVCComponent->eProfile;
    362         pDstProfileLevel->eLevel = pSrcAVCComponent->eLevel;
    363     }
    364         break;
    365     case OMX_IndexParamVideoErrorCorrection:
    366     {
    367         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pDstErrorCorrectionType = (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *)pComponentParameterStructure;
    368         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pSrcErrorCorrectionType = NULL;
    369         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    370 
    371         ret = SEC_OMX_Check_SizeVersion(pDstErrorCorrectionType, sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
    372         if (ret != OMX_ErrorNone) {
    373             goto EXIT;
    374         }
    375 
    376         if (pDstErrorCorrectionType->nPortIndex != OUTPUT_PORT_INDEX) {
    377             ret = OMX_ErrorBadPortIndex;
    378             goto EXIT;
    379         }
    380 
    381         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    382         pSrcErrorCorrectionType = &pH264Enc->errorCorrectionType[OUTPUT_PORT_INDEX];
    383 
    384         pDstErrorCorrectionType->bEnableHEC = pSrcErrorCorrectionType->bEnableHEC;
    385         pDstErrorCorrectionType->bEnableResync = pSrcErrorCorrectionType->bEnableResync;
    386         pDstErrorCorrectionType->nResynchMarkerSpacing = pSrcErrorCorrectionType->nResynchMarkerSpacing;
    387         pDstErrorCorrectionType->bEnableDataPartitioning = pSrcErrorCorrectionType->bEnableDataPartitioning;
    388         pDstErrorCorrectionType->bEnableRVLC = pSrcErrorCorrectionType->bEnableRVLC;
    389     }
    390         break;
    391     default:
    392         ret = SEC_OMX_VideoEncodeGetParameter(hComponent, nParamIndex, pComponentParameterStructure);
    393         break;
    394     }
    395 EXIT:
    396     FunctionOut();
    397 
    398     return ret;
    399 }
    400 
    401 OMX_ERRORTYPE SEC_MFC_H264Enc_SetParameter(
    402     OMX_IN OMX_HANDLETYPE hComponent,
    403     OMX_IN OMX_INDEXTYPE  nIndex,
    404     OMX_IN OMX_PTR        pComponentParameterStructure)
    405 {
    406     OMX_ERRORTYPE           ret = OMX_ErrorNone;
    407     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    408     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    409 
    410     FunctionIn();
    411 
    412     if (hComponent == NULL || pComponentParameterStructure == NULL) {
    413         ret = OMX_ErrorBadParameter;
    414         goto EXIT;
    415     }
    416     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    417     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    418     if (ret != OMX_ErrorNone) {
    419         goto EXIT;
    420     }
    421     if (pOMXComponent->pComponentPrivate == NULL) {
    422         ret = OMX_ErrorBadParameter;
    423         goto EXIT;
    424     }
    425 
    426     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    427     if (pSECComponent->currentState == OMX_StateInvalid ) {
    428         ret = OMX_StateInvalid;
    429         goto EXIT;
    430     }
    431 
    432     switch (nIndex) {
    433     case OMX_IndexParamVideoAvc:
    434     {
    435         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = NULL;
    436         OMX_VIDEO_PARAM_AVCTYPE *pSrcAVCComponent = (OMX_VIDEO_PARAM_AVCTYPE *)pComponentParameterStructure;
    437         SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    438 
    439         ret = SEC_OMX_Check_SizeVersion(pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    440         if (ret != OMX_ErrorNone) {
    441             goto EXIT;
    442         }
    443 
    444         if (pSrcAVCComponent->nPortIndex >= ALL_PORT_NUM) {
    445             ret = OMX_ErrorBadPortIndex;
    446             goto EXIT;
    447         }
    448 
    449         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    450         pDstAVCComponent = &pH264Enc->AVCComponent[pSrcAVCComponent->nPortIndex];
    451 
    452         SEC_OSAL_Memcpy(pDstAVCComponent, pSrcAVCComponent, sizeof(OMX_VIDEO_PARAM_AVCTYPE));
    453     }
    454         break;
    455     case OMX_IndexParamStandardComponentRole:
    456     {
    457         OMX_PARAM_COMPONENTROLETYPE *pComponentRole = (OMX_PARAM_COMPONENTROLETYPE*)pComponentParameterStructure;
    458 
    459         ret = SEC_OMX_Check_SizeVersion(pComponentRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
    460         if (ret != OMX_ErrorNone) {
    461             goto EXIT;
    462         }
    463 
    464         if ((pSECComponent->currentState != OMX_StateLoaded) && (pSECComponent->currentState != OMX_StateWaitForResources)) {
    465             ret = OMX_ErrorIncorrectStateOperation;
    466             goto EXIT;
    467         }
    468 
    469         if (!SEC_OSAL_Strcmp((char*)pComponentRole->cRole, SEC_OMX_COMPOMENT_H264_ENC_ROLE)) {
    470             pSECComponent->pSECPort[OUTPUT_PORT_INDEX].portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
    471         } else {
    472             ret = OMX_ErrorBadParameter;
    473             goto EXIT;
    474         }
    475     }
    476         break;
    477     case OMX_IndexParamVideoProfileLevelCurrent:
    478     {
    479         OMX_VIDEO_PARAM_PROFILELEVELTYPE *pSrcProfileLevel = (OMX_VIDEO_PARAM_PROFILELEVELTYPE *)pComponentParameterStructure;
    480         OMX_VIDEO_PARAM_AVCTYPE *pDstAVCComponent = NULL;
    481         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    482 
    483         ret = SEC_OMX_Check_SizeVersion(pSrcProfileLevel, sizeof(OMX_VIDEO_PARAM_PROFILELEVELTYPE));
    484         if (ret != OMX_ErrorNone) {
    485             goto EXIT;
    486         }
    487 
    488         if (pSrcProfileLevel->nPortIndex >= ALL_PORT_NUM) {
    489             ret = OMX_ErrorBadPortIndex;
    490             goto EXIT;
    491         }
    492 
    493         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    494 
    495         pDstAVCComponent = &pH264Enc->AVCComponent[pSrcProfileLevel->nPortIndex];
    496         pDstAVCComponent->eProfile = pSrcProfileLevel->eProfile;
    497         pDstAVCComponent->eLevel = pSrcProfileLevel->eLevel;
    498     }
    499         break;
    500     case OMX_IndexParamVideoErrorCorrection:
    501     {
    502         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pSrcErrorCorrectionType = (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *)pComponentParameterStructure;
    503         OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE *pDstErrorCorrectionType = NULL;
    504         SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    505 
    506         ret = SEC_OMX_Check_SizeVersion(pSrcErrorCorrectionType, sizeof(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE));
    507         if (ret != OMX_ErrorNone) {
    508             goto EXIT;
    509         }
    510 
    511         if (pSrcErrorCorrectionType->nPortIndex != OUTPUT_PORT_INDEX) {
    512             ret = OMX_ErrorBadPortIndex;
    513             goto EXIT;
    514         }
    515 
    516         pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    517         pDstErrorCorrectionType = &pH264Enc->errorCorrectionType[OUTPUT_PORT_INDEX];
    518 
    519         pDstErrorCorrectionType->bEnableHEC = pSrcErrorCorrectionType->bEnableHEC;
    520         pDstErrorCorrectionType->bEnableResync = pSrcErrorCorrectionType->bEnableResync;
    521         pDstErrorCorrectionType->nResynchMarkerSpacing = pSrcErrorCorrectionType->nResynchMarkerSpacing;
    522         pDstErrorCorrectionType->bEnableDataPartitioning = pSrcErrorCorrectionType->bEnableDataPartitioning;
    523         pDstErrorCorrectionType->bEnableRVLC = pSrcErrorCorrectionType->bEnableRVLC;
    524     }
    525         break;
    526     default:
    527         ret = SEC_OMX_VideoEncodeSetParameter(hComponent, nIndex, pComponentParameterStructure);
    528         break;
    529     }
    530 EXIT:
    531     FunctionOut();
    532 
    533     return ret;
    534 }
    535 
    536 OMX_ERRORTYPE SEC_MFC_H264Enc_SetConfig(
    537     OMX_HANDLETYPE hComponent,
    538     OMX_INDEXTYPE nIndex,
    539     OMX_PTR pComponentConfigStructure)
    540 {
    541     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    542     OMX_COMPONENTTYPE     *pOMXComponent = NULL;
    543     SEC_OMX_BASECOMPONENT *pSECComponent = NULL;
    544 
    545     FunctionIn();
    546 
    547     if (hComponent == NULL || pComponentConfigStructure == NULL) {
    548         ret = OMX_ErrorBadParameter;
    549         goto EXIT;
    550     }
    551     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    552     ret = SEC_OMX_Check_SizeVersion(pOMXComponent, sizeof(OMX_COMPONENTTYPE));
    553     if (ret != OMX_ErrorNone) {
    554         goto EXIT;
    555     }
    556     if (pOMXComponent->pComponentPrivate == NULL) {
    557         ret = OMX_ErrorBadParameter;
    558         goto EXIT;
    559     }
    560 
    561     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    562     if (pSECComponent->currentState == OMX_StateInvalid) {
    563         ret = OMX_ErrorInvalidState;
    564         goto EXIT;
    565     }
    566 
    567     switch (nIndex) {
    568     default:
    569         ret = SEC_OMX_SetConfig(hComponent, nIndex, pComponentConfigStructure);
    570         break;
    571     }
    572 
    573 EXIT:
    574     FunctionOut();
    575 
    576     return ret;
    577 }
    578 
    579 OMX_ERRORTYPE SEC_MFC_H264Enc_ComponentRoleEnum(OMX_HANDLETYPE hComponent, OMX_U8 *cRole, OMX_U32 nIndex)
    580 {
    581     OMX_ERRORTYPE            ret = OMX_ErrorNone;
    582     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    583     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    584 
    585     FunctionIn();
    586 
    587     if ((hComponent == NULL) || (cRole == NULL)) {
    588         ret = OMX_ErrorBadParameter;
    589         goto EXIT;
    590     }
    591     if (nIndex == (MAX_COMPONENT_ROLE_NUM-1)) {
    592         SEC_OSAL_Strcpy((char *)cRole, SEC_OMX_COMPOMENT_H264_ENC_ROLE);
    593         ret = OMX_ErrorNone;
    594     } else {
    595         ret = OMX_ErrorNoMore;
    596     }
    597 
    598 EXIT:
    599     FunctionOut();
    600 
    601     return ret;
    602 }
    603 
    604 /* MFC Init */
    605 OMX_ERRORTYPE SEC_MFC_H264Enc_Init(OMX_COMPONENTTYPE *pOMXComponent)
    606 {
    607     OMX_ERRORTYPE              ret = OMX_ErrorNone;
    608     SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    609     SEC_H264ENC_HANDLE        *pH264Enc = NULL;
    610     OMX_PTR                    hMFCHandle = NULL;
    611     OMX_S32                    returnCodec = 0;
    612 
    613     FunctionIn();
    614 
    615     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    616     pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_FALSE;
    617     pSECComponent->bUseFlagEOF = OMX_FALSE;
    618     pSECComponent->bSaveFlagEOS = OMX_FALSE;
    619 
    620     /* MFC(Multi Function Codec) encoder and CMM(Codec Memory Management) driver open */
    621     hMFCHandle = (OMX_PTR)SsbSipMfcEncOpen();
    622     if (hMFCHandle == NULL) {
    623         ret = OMX_ErrorInsufficientResources;
    624         goto EXIT;
    625     }
    626     pH264Enc->hMFCH264Handle.hMFCHandle = hMFCHandle;
    627 
    628     Set_H264ENC_Param(&(pH264Enc->hMFCH264Handle.mfcVideoAvc), pSECComponent);
    629 
    630     returnCodec = SsbSipMfcEncInit(hMFCHandle, &(pH264Enc->hMFCH264Handle.mfcVideoAvc));
    631     if (returnCodec != MFC_RET_OK) {
    632         ret = OMX_ErrorInsufficientResources;
    633         goto EXIT;
    634     }
    635 
    636     /* Allocate encoder's input buffer */
    637     returnCodec = SsbSipMfcEncGetInBuf(hMFCHandle, &(pH264Enc->hMFCH264Handle.inputInfo));
    638     if (returnCodec != MFC_RET_OK) {
    639         ret = OMX_ErrorInsufficientResources;
    640         goto EXIT;
    641     }
    642 
    643     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.YVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.YVirAddr);
    644     SEC_OSAL_Log(SEC_LOG_TRACE, "pH264Enc->hMFCH264Handle.inputInfo.CVirAddr : 0x%x", pH264Enc->hMFCH264Handle.inputInfo.CVirAddr);
    645 
    646     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.YPhyAddr;
    647     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CPhyAddr = pH264Enc->hMFCH264Handle.inputInfo.CPhyAddr;
    648     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YVirAddr = pH264Enc->hMFCH264Handle.inputInfo.YVirAddr;
    649     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CVirAddr = pH264Enc->hMFCH264Handle.inputInfo.CVirAddr;
    650     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.YSize = pH264Enc->hMFCH264Handle.inputInfo.YSize;
    651     pSECComponent->processData[INPUT_PORT_INDEX].specificBufferHeader.CSize = pH264Enc->hMFCH264Handle.inputInfo.CSize;
    652 
    653     SEC_OSAL_Memset(pSECComponent->timeStamp, -19771003, sizeof(OMX_TICKS) * MAX_TIMESTAMP);
    654     SEC_OSAL_Memset(pSECComponent->nFlags, 0, sizeof(OMX_U32) * MAX_FLAGS);
    655     pH264Enc->hMFCH264Handle.indexTimestamp = 0;
    656 
    657 EXIT:
    658     FunctionOut();
    659 
    660     return ret;
    661 }
    662 
    663 /* MFC Terminate */
    664 OMX_ERRORTYPE SEC_MFC_H264Enc_Terminate(OMX_COMPONENTTYPE *pOMXComponent)
    665 {
    666     OMX_ERRORTYPE          ret = OMX_ErrorNone;
    667     SEC_OMX_BASECOMPONENT *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    668     SEC_H264ENC_HANDLE    *pH264Enc = NULL;
    669     OMX_PTR                hMFCHandle = NULL;
    670 
    671     FunctionIn();
    672 
    673     pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    674     hMFCHandle = pH264Enc->hMFCH264Handle.hMFCHandle;
    675 
    676     if (hMFCHandle != NULL) {
    677         SsbSipMfcEncClose(hMFCHandle);
    678         hMFCHandle = pH264Enc->hMFCH264Handle.hMFCHandle = NULL;
    679     }
    680 
    681 EXIT:
    682     FunctionOut();
    683 
    684     return ret;
    685 }
    686 
    687 OMX_ERRORTYPE SEC_MFC_H264_Encode(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
    688 {
    689     OMX_ERRORTYPE              ret = OMX_ErrorNone;
    690     SEC_OMX_BASECOMPONENT     *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    691     SEC_H264ENC_HANDLE        *pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    692     SSBSIP_MFC_ENC_INPUT_INFO *pInputInfo = &pH264Enc->hMFCH264Handle.inputInfo;
    693     SSBSIP_MFC_ENC_OUTPUT_INFO outputInfo;
    694     SEC_OMX_BASEPORT          *pSECPort = NULL;
    695     MFC_ENC_ADDR_INFO          addrInfo;
    696     OMX_U32                    oneFrameSize = pInputData->dataLen;
    697     OMX_S32                    returnCodec = 0;
    698 
    699     FunctionIn();
    700 
    701     if (pH264Enc->hMFCH264Handle.bConfiguredMFC == OMX_FALSE) {
    702         returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
    703         if (returnCodec != MFC_RET_OK)
    704         {
    705             SEC_OSAL_Log(SEC_LOG_TRACE, "%s - SsbSipMfcEncGetOutBuf Failed\n", __func__);
    706             ret = OMX_ErrorUndefined;
    707             goto EXIT;
    708         } else {
    709             char *p = NULL;
    710             int iSpsSize = 0;
    711             int iPpsSize = 0;
    712 
    713             p = FindDelimiter((OMX_U8 *)outputInfo.StrmVirAddr + 4, outputInfo.headerSize - 4);
    714 
    715             iSpsSize = (unsigned int)p - (unsigned int)outputInfo.StrmVirAddr;
    716             pH264Enc->hMFCH264Handle.headerData.pHeaderSPS = (OMX_PTR)outputInfo.StrmVirAddr;
    717             pH264Enc->hMFCH264Handle.headerData.SPSLen = iSpsSize;
    718 
    719             iPpsSize = outputInfo.headerSize - iSpsSize;
    720             pH264Enc->hMFCH264Handle.headerData.pHeaderPPS = outputInfo.StrmVirAddr + iSpsSize;
    721             pH264Enc->hMFCH264Handle.headerData.PPSLen = iPpsSize;
    722         }
    723 
    724         /* SEC_OSAL_Memcpy((void*)(pOutputData->dataBuffer), (const void*)(outputInfo.StrmVirAddr), outputInfo.headerSize); */
    725         pOutputData->dataBuffer = outputInfo.StrmVirAddr;
    726         pOutputData->allocSize = outputInfo.headerSize;
    727         pOutputData->dataLen = outputInfo.headerSize;
    728         pOutputData->timeStamp = pInputData->timeStamp;
    729         pOutputData->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
    730         pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
    731 
    732         pH264Enc->hMFCH264Handle.bConfiguredMFC = OMX_TRUE;
    733 
    734         ret = OMX_ErrorNone;
    735         goto EXIT;
    736     }
    737 
    738     if ((pInputData->nFlags & OMX_BUFFERFLAG_ENDOFFRAME) &&
    739         (pSECComponent->bUseFlagEOF == OMX_FALSE)) {
    740         pSECComponent->bUseFlagEOF = OMX_TRUE;
    741     }
    742 
    743     pSECComponent->timeStamp[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->timeStamp;
    744     pSECComponent->nFlags[pH264Enc->hMFCH264Handle.indexTimestamp] = pInputData->nFlags;
    745     SsbSipMfcEncSetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_SETCONF_FRAME_TAG, &(pH264Enc->hMFCH264Handle.indexTimestamp));
    746     pH264Enc->hMFCH264Handle.indexTimestamp++;
    747     if (pH264Enc->hMFCH264Handle.indexTimestamp >= MAX_TIMESTAMP)
    748         pH264Enc->hMFCH264Handle.indexTimestamp = 0;
    749 
    750     if (oneFrameSize <= 0) {
    751         pOutputData->timeStamp = pInputData->timeStamp;
    752         pOutputData->nFlags = pInputData->nFlags;
    753 
    754         ret = OMX_ErrorNone;
    755         goto EXIT;
    756     }
    757 
    758     pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    759     if (pSECPort->portDefinition.format.video.eColorFormat == SEC_OMX_COLOR_FormatNV12PhysicalAddress) {
    760 #define USE_FIMC_FRAME_BUFFER
    761 #ifdef USE_FIMC_FRAME_BUFFER
    762         SEC_OSAL_Memcpy(&addrInfo.pAddrY, pInputData->dataBuffer, sizeof(addrInfo.pAddrY));
    763         SEC_OSAL_Memcpy(&addrInfo.pAddrC, pInputData->dataBuffer + sizeof(addrInfo.pAddrY), sizeof(addrInfo.pAddrC));
    764         pInputInfo->YPhyAddr = addrInfo.pAddrY;
    765         pInputInfo->CPhyAddr = addrInfo.pAddrC;
    766         ret = SsbSipMfcEncSetInBuf(pH264Enc->hMFCH264Handle.hMFCHandle, pInputInfo);
    767         if (ret != MFC_RET_OK) {
    768             SEC_OSAL_Log(SEC_LOG_TRACE, "Error : SsbSipMfcEncSetInBuf() \n");
    769             ret = OMX_ErrorUndefined;
    770             goto EXIT;
    771         }
    772 #else
    773         OMX_U32 width, height;
    774 
    775         width = pSECPort->portDefinition.format.video.nFrameWidth;
    776         height = pSECPort->portDefinition.format.video.nFrameHeight;
    777 
    778         SEC_OSAL_Memcpy(pInputInfo->YVirAddr, pInputData->dataBuffer, ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)));
    779         SEC_OSAL_Memcpy(pInputInfo->CVirAddr, pInputData->dataBuffer + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)), ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height / 2)));
    780 #endif
    781     }
    782 
    783     returnCodec = SsbSipMfcEncExe(pH264Enc->hMFCH264Handle.hMFCHandle);
    784     if (returnCodec == MFC_RET_OK) {
    785         OMX_S32 indexTimestamp = 0;
    786 
    787         returnCodec = SsbSipMfcEncGetOutBuf(pH264Enc->hMFCH264Handle.hMFCHandle, &outputInfo);
    788         if ((SsbSipMfcEncGetConfig(pH264Enc->hMFCH264Handle.hMFCHandle, MFC_ENC_GETCONF_FRAME_TAG, &indexTimestamp) != MFC_RET_OK) ||
    789             (((indexTimestamp < 0) || (indexTimestamp > MAX_TIMESTAMP)))){
    790             pOutputData->timeStamp = pInputData->timeStamp;
    791             pOutputData->nFlags = pInputData->nFlags;
    792         } else {
    793             pOutputData->timeStamp = pSECComponent->timeStamp[indexTimestamp];
    794             pOutputData->nFlags = pSECComponent->nFlags[indexTimestamp];
    795         }
    796 
    797         if (returnCodec == MFC_RET_OK) {
    798             /** Fill Output Buffer **/
    799             pOutputData->dataBuffer = outputInfo.StrmVirAddr;
    800             pOutputData->allocSize = outputInfo.dataSize;
    801             pOutputData->dataLen = outputInfo.dataSize;
    802             pOutputData->usedDataLen = 0;
    803 
    804             pOutputData->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
    805             if (outputInfo.frameType == MFC_FRAME_TYPE_I_FRAME)
    806                     pOutputData->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
    807 
    808             SEC_OSAL_Log(SEC_LOG_TRACE, "MFC Encode OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
    809 
    810             ret = OMX_ErrorNone;
    811         }
    812     }
    813 
    814     if (returnCodec != MFC_RET_OK) {
    815         SEC_OSAL_Log(SEC_LOG_ERROR, "In %s : SsbSipMfcEncExe OR SsbSipMfcEncGetOutBuf Failed!!!\n", __func__);
    816         ret = OMX_ErrorUndefined;
    817     }
    818 
    819 EXIT:
    820     FunctionOut();
    821 
    822     return ret;
    823 }
    824 
    825 /* MFC Encode */
    826 OMX_ERRORTYPE SEC_MFC_H264Enc_bufferProcess(OMX_COMPONENTTYPE *pOMXComponent, SEC_OMX_DATA *pInputData, SEC_OMX_DATA *pOutputData)
    827 {
    828     OMX_ERRORTYPE ret = OMX_ErrorNone;
    829     SEC_OMX_BASECOMPONENT   *pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    830     SEC_H264ENC_HANDLE      *pH264Enc = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
    831     SEC_OMX_BASEPORT        *pSECInputPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    832     SEC_OMX_BASEPORT        *pSECOutputPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    833     OMX_BOOL                 endOfFrame = OMX_FALSE;
    834     OMX_BOOL                 flagEOS = OMX_FALSE;
    835 
    836     FunctionIn();
    837 
    838     if ((!CHECK_PORT_ENABLED(pSECInputPort)) || (!CHECK_PORT_ENABLED(pSECOutputPort)) ||
    839             (!CHECK_PORT_POPULATED(pSECInputPort)) || (!CHECK_PORT_POPULATED(pSECOutputPort))) {
    840         ret = OMX_ErrorNone;
    841         goto EXIT;
    842     }
    843     if (OMX_FALSE == SEC_Check_BufferProcess_State(pSECComponent)) {
    844         ret = OMX_ErrorNone;
    845         goto EXIT;
    846     }
    847 
    848     ret = SEC_MFC_H264_Encode(pOMXComponent, pInputData, pOutputData);
    849     if (ret != OMX_ErrorNone) {
    850         pSECComponent->pCallbacks->EventHandler((OMX_HANDLETYPE)pOMXComponent,
    851                                         pSECComponent->callbackData,
    852                                         OMX_EventError, ret, 0, NULL);
    853     } else {
    854         pInputData->usedDataLen += pInputData->dataLen;
    855         pInputData->remainDataLen = pInputData->dataLen - pInputData->usedDataLen;
    856         pInputData->dataLen -= pInputData->usedDataLen;
    857         pInputData->usedDataLen = 0;
    858 
    859         /* pOutputData->usedDataLen = 0; */
    860         pOutputData->remainDataLen = pOutputData->dataLen - pOutputData->usedDataLen;
    861     }
    862 
    863 EXIT:
    864     FunctionOut();
    865 
    866     return ret;
    867 }
    868 
    869 OSCL_EXPORT_REF OMX_ERRORTYPE SEC_OMX_ComponentInit(OMX_HANDLETYPE hComponent, OMX_STRING componentName)
    870 {
    871     OMX_ERRORTYPE            ret = OMX_ErrorNone;
    872     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    873     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    874     SEC_OMX_BASEPORT        *pSECPort = NULL;
    875     SEC_H264ENC_HANDLE      *pH264Enc = NULL;
    876     int i = 0;
    877 
    878     FunctionIn();
    879 
    880     if ((hComponent == NULL) || (componentName == NULL)) {
    881         ret = OMX_ErrorBadParameter;
    882         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, Line:%d", __LINE__);
    883         goto EXIT;
    884     }
    885     if (SEC_OSAL_Strcmp(SEC_OMX_COMPOMENT_H264_ENC, componentName) != 0) {
    886         ret = OMX_ErrorBadParameter;
    887         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorBadParameter, componentName:%s, Line:%d", componentName, __LINE__);
    888         goto EXIT;
    889     }
    890 
    891     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
    892     ret = SEC_OMX_VideoEncodeComponentInit(pOMXComponent);
    893     if (ret != OMX_ErrorNone) {
    894         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_Error, Line:%d", __LINE__);
    895         goto EXIT;
    896     }
    897     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
    898     pSECComponent->codecType = HW_VIDEO_CODEC;
    899 
    900     pSECComponent->componentName = (OMX_STRING)SEC_OSAL_Malloc(MAX_OMX_COMPONENT_NAME_SIZE);
    901     if (pSECComponent->componentName == NULL) {
    902         SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
    903         ret = OMX_ErrorInsufficientResources;
    904         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
    905         goto EXIT;
    906     }
    907     SEC_OSAL_Memset(pSECComponent->componentName, 0, MAX_OMX_COMPONENT_NAME_SIZE);
    908 
    909     pH264Enc = SEC_OSAL_Malloc(sizeof(SEC_H264ENC_HANDLE));
    910     if (pH264Enc == NULL) {
    911         SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
    912         ret = OMX_ErrorInsufficientResources;
    913         SEC_OSAL_Log(SEC_LOG_ERROR, "OMX_ErrorInsufficientResources, Line:%d", __LINE__);
    914         goto EXIT;
    915     }
    916     SEC_OSAL_Memset(pH264Enc, 0, sizeof(SEC_H264ENC_HANDLE));
    917     pSECComponent->hCodecHandle = (OMX_HANDLETYPE)pH264Enc;
    918 
    919     SEC_OSAL_Strcpy(pSECComponent->componentName, SEC_OMX_COMPOMENT_H264_ENC);
    920     /* Set componentVersion */
    921     pSECComponent->componentVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    922     pSECComponent->componentVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    923     pSECComponent->componentVersion.s.nRevision     = REVISION_NUMBER;
    924     pSECComponent->componentVersion.s.nStep         = STEP_NUMBER;
    925     /* Set specVersion */
    926     pSECComponent->specVersion.s.nVersionMajor = VERSIONMAJOR_NUMBER;
    927     pSECComponent->specVersion.s.nVersionMinor = VERSIONMINOR_NUMBER;
    928     pSECComponent->specVersion.s.nRevision     = REVISION_NUMBER;
    929     pSECComponent->specVersion.s.nStep         = STEP_NUMBER;
    930 
    931     /* Android CapabilityFlags */
    932     pSECComponent->capabilityFlags.iIsOMXComponentMultiThreaded                   = OMX_TRUE;
    933     pSECComponent->capabilityFlags.iOMXComponentSupportsExternalInputBufferAlloc  = OMX_TRUE;
    934     pSECComponent->capabilityFlags.iOMXComponentSupportsExternalOutputBufferAlloc = OMX_TRUE;
    935     pSECComponent->capabilityFlags.iOMXComponentSupportsMovableInputBuffers       = OMX_FALSE;
    936     pSECComponent->capabilityFlags.iOMXComponentSupportsPartialFrames             = OMX_FALSE;
    937     pSECComponent->capabilityFlags.iOMXComponentUsesNALStartCodes                 = OMX_TRUE;
    938     pSECComponent->capabilityFlags.iOMXComponentCanHandleIncompleteFrames         = OMX_TRUE;
    939     pSECComponent->capabilityFlags.iOMXComponentUsesFullAVCFrames                 = OMX_TRUE;
    940 
    941     /* Input port */
    942     pSECPort = &pSECComponent->pSECPort[INPUT_PORT_INDEX];
    943     pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    944     pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    945     pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
    946     pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_INPUT_BUFFER_SIZE;
    947     pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused;
    948     SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
    949     SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "raw/video");
    950     pSECPort->portDefinition.format.video.eColorFormat = SEC_OMX_COLOR_FormatNV12PhysicalAddress;
    951     pSECPort->portDefinition.bEnabled = OMX_TRUE;
    952 
    953     /* Output port */
    954     pSECPort = &pSECComponent->pSECPort[OUTPUT_PORT_INDEX];
    955     pSECPort->portDefinition.format.video.nFrameWidth = DEFAULT_FRAME_WIDTH;
    956     pSECPort->portDefinition.format.video.nFrameHeight= DEFAULT_FRAME_HEIGHT;
    957     pSECPort->portDefinition.format.video.nStride = 0; /*DEFAULT_FRAME_WIDTH;*/
    958     pSECPort->portDefinition.nBufferSize = DEFAULT_VIDEO_OUTPUT_BUFFER_SIZE;
    959     pSECPort->portDefinition.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
    960     SEC_OSAL_Memset(pSECPort->portDefinition.format.video.cMIMEType, 0, MAX_OMX_MIMETYPE_SIZE);
    961     SEC_OSAL_Strcpy(pSECPort->portDefinition.format.video.cMIMEType, "video/avc");
    962     pSECPort->portDefinition.format.video.eColorFormat = OMX_COLOR_FormatUnused;
    963     pSECPort->portDefinition.bEnabled = OMX_TRUE;
    964 
    965     for(i = 0; i < ALL_PORT_NUM; i++) {
    966         INIT_SET_SIZE_VERSION(&pH264Enc->AVCComponent[i], OMX_VIDEO_PARAM_AVCTYPE);
    967         pH264Enc->AVCComponent[i].nPortIndex = i;
    968         pH264Enc->AVCComponent[i].eProfile   = OMX_VIDEO_AVCProfileBaseline;
    969         pH264Enc->AVCComponent[i].eLevel     = OMX_VIDEO_AVCLevel4;
    970     }
    971 
    972     pOMXComponent->GetParameter      = &SEC_MFC_H264Enc_GetParameter;
    973     pOMXComponent->SetParameter      = &SEC_MFC_H264Enc_SetParameter;
    974     pOMXComponent->SetConfig         = &SEC_MFC_H264Enc_SetConfig;
    975     pOMXComponent->ComponentRoleEnum = &SEC_MFC_H264Enc_ComponentRoleEnum;
    976     pOMXComponent->ComponentDeInit   = &SEC_OMX_ComponentDeinit;
    977 
    978     pSECComponent->sec_mfc_componentInit      = &SEC_MFC_H264Enc_Init;
    979     pSECComponent->sec_mfc_componentTerminate = &SEC_MFC_H264Enc_Terminate;
    980     pSECComponent->sec_mfc_bufferProcess      = &SEC_MFC_H264Enc_bufferProcess;
    981     pSECComponent->sec_checkInputFrame        = NULL;
    982 
    983     pSECComponent->currentState = OMX_StateLoaded;
    984 
    985     ret = OMX_ErrorNone;
    986 
    987 EXIT:
    988     FunctionOut();
    989 
    990     return ret;
    991 }
    992 
    993 OMX_ERRORTYPE SEC_OMX_ComponentDeinit(OMX_HANDLETYPE hComponent)
    994 {
    995     OMX_ERRORTYPE            ret = OMX_ErrorNone;
    996     OMX_COMPONENTTYPE       *pOMXComponent = NULL;
    997     SEC_OMX_BASECOMPONENT   *pSECComponent = NULL;
    998     SEC_H264ENC_HANDLE      *pH264Dec = NULL;
    999 
   1000     FunctionIn();
   1001 
   1002     if (hComponent == NULL) {
   1003         ret = OMX_ErrorBadParameter;
   1004         goto EXIT;
   1005     }
   1006     pOMXComponent = (OMX_COMPONENTTYPE *)hComponent;
   1007     pSECComponent = (SEC_OMX_BASECOMPONENT *)pOMXComponent->pComponentPrivate;
   1008 
   1009     SEC_OSAL_Free(pSECComponent->componentName);
   1010     pSECComponent->componentName = NULL;
   1011 
   1012     pH264Dec = (SEC_H264ENC_HANDLE *)pSECComponent->hCodecHandle;
   1013     if (pH264Dec != NULL) {
   1014         SEC_OSAL_Free(pH264Dec);
   1015         pH264Dec = pSECComponent->hCodecHandle = NULL;
   1016     }
   1017 
   1018     ret = SEC_OMX_VideoEncodeComponentDeinit(pOMXComponent);
   1019     if (ret != OMX_ErrorNone) {
   1020         goto EXIT;
   1021     }
   1022 
   1023     ret = OMX_ErrorNone;
   1024 
   1025 EXIT:
   1026     FunctionOut();
   1027 
   1028     return ret;
   1029 }
   1030