Home | History | Annotate | Download | only in src
      1 
      2 /*
      3  * Copyright (C) Texas Instruments - http://www.ti.com/
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Lesser General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2.1 of the License, or (at your option) any later version.
      9  *
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  *
     17  * You should have received a copy of the GNU Lesser General Public
     18  * License along with this library; if not, write to the Free Software
     19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     20  */
     21 /* =============================================================================
     22 *             Texas Instruments OMAP(TM) Platform Software
     23 *  (c) Copyright Texas Instruments, Incorporated.  All Rights Reserved.
     24 *
     25 *  Use of this software is controlled by the terms and conditions found
     26 *  in the license agreement under which this software has been supplied.
     27 * ============================================================================*/
     28 /**
     29 * @file OMX_VideoEnc_Utils.c
     30 *
     31 * This file implements OMX Component for MPEG-4 encoder that
     32 * is fully compliant with the OMX specification 1.5.
     33 *
     34 * @path  $(CSLPATH)\src
     35 *
     36 * @rev  0.1
     37 */
     38 /* ---------------------------------------------------------------------------*/
     39 /* =============================================================================
     40 *!
     41 *! Revision History
     42 *! =============================================================================
     43 *!
     44 *! 02-Feb-2006 mf: Revisions appear in reverse chronological order;
     45 *! that is, newest first.  The date format is dd-Mon-yyyy.
     46 * ============================================================================*/
     47 
     48 /* ------compilation control switches ----------------------------------------*/
     49 /******************************************************************************
     50 *  INCLUDE FILES
     51 *******************************************************************************/
     52 /* ----- system and platform files -------------------------------------------*/
     53 #ifdef UNDER_CE
     54     #include <windows.h>
     55     #include <omx_core.h>
     56     #include <stdlib.h>
     57     #include <pthread.h>
     58 #else
     59     #include <wchar.h>
     60     #include <unistd.h>
     61     #include <sys/types.h>
     62     #include <sys/wait.h>
     63     #include <sys/types.h>
     64     #include <sys/select.h>
     65     #include <sys/stat.h>
     66     #include <dlfcn.h>
     67     #include <malloc.h>
     68     #include <memory.h>
     69     #include <fcntl.h>
     70 #endif
     71 
     72 #include <dbapi.h>
     73 #include <string.h>
     74 #include <stdlib.h>
     75 #include <stdio.h>
     76 
     77 /*------- Program Header Files -----------------------------------------------*/
     78 #include "OMX_VideoEnc_Utils.h"
     79 #include "OMX_VideoEnc_Thread.h"
     80 #include "OMX_VideoEnc_DSP.h"
     81 
     82 #define DSP_MMU_FAULT_HANDLING
     83 
     84 // We cannot request the same MHz for all resolutions.
     85 // we have to change this implementation once we modify
     86 // opencore to request the correct level based on resolution/bitrate/etc
     87 #define VIDEO_ENCODER_MHZ (400 - 45 + 2)
     88 
     89 /* H264 Specific */
     90 #define SPS_CODE_PREFIX 0x07
     91 #define PPS_CODE_PREFIX 0x08
     92 
     93 #ifdef UNDER_CE
     94     HINSTANCE g_hLcmlDllHandle = NULL;
     95 #endif
     96 #ifdef UNDER_CE
     97 void sleep(DWORD Duration)
     98 {
     99     Sleep(Duration);
    100 }
    101 #endif
    102 /*******************************************************************************
    103 *  EXTERNAL REFERENCES NOTE : only use if not found in header file
    104 *******************************************************************************/
    105 /*--------data declarations --------------------------------------------------*/
    106 /*--------function prototypes ------------------------------------------------*/
    107 
    108 /*******************************************************************************
    109 *  PUBLIC DECLARATIONS Defined here, used elsewhere
    110 *******************************************************************************/
    111 /*--------data declarations --------------------------------------------------*/
    112 
    113 
    114 /*--------function prototypes ------------------------------------------------*/
    115 
    116 /*******************************************************************************
    117 *  PRIVATE DECLARATIONS Defined here, used only here
    118 *******************************************************************************/
    119 /*--------data declarations --------------------------------------------------*/
    120 struct DSP_UUID H264VESOCKET_TI_UUID = {
    121     0x63A3581A, 0x09D7, 0x4AD0, 0x80, 0xB8, {
    122     0x5F, 0x2C, 0x4D, 0x4D, 0x59, 0xC9
    123     }
    124 };
    125 
    126 struct DSP_UUID MP4VESOCKET_TI_UUID = {
    127     0x98c2e8d8, 0x4644, 0x11d6, 0x81, 0x18, {
    128         0x00, 0xb0, 0xd0, 0x8d, 0x72, 0x9f
    129     }
    130 };
    131 
    132 struct DSP_UUID USN_UUID = {
    133     0x79A3C8B3, 0x95F2, 0x403F, 0x9A, 0x4B, {
    134         0xCF, 0x80, 0x57, 0x73, 0x05, 0x41
    135     }
    136 };
    137 
    138 OMX_U32 VIDENC_STRUCT_H264DEFBITRATE [VIDENC_MAXBITRATES][2] = {
    139 /*1*/    {176 * 144, 128000},     /*128KBps*/
    140 /*2*/    {320 * 240, 400000},     /*400KBps*/
    141 /*3*/    {352 * 288, 500000},     /*500kBps*/
    142 /*4*/    {640 * 480, 1500000},    /*1.5MBps*/
    143 /*5*/    {720 * 480, 2000000},    /*2MBps*/
    144 /*6*/    {720 * 576, 3000000},    /*3MBps*/
    145 /*7*/    {1280 * 720, 3000000},   /*3MBps*/
    146 };
    147 
    148 OMX_U32 VIDENC_STRUCT_MPEG4DEFBITRATE [VIDENC_MAXBITRATES][2] = {
    149 /*1*/    {176 * 144, 128000},     /*128KBps*/
    150 /*2*/    {320 * 240, 400000},     /*400KBps*/
    151 /*3*/    {352 * 288, 500000},     /*500kBps*/
    152 /*4*/    {640 * 480, 1500000},    /*1.5MBps*/
    153 /*5*/    {720 * 480, 2000000},    /*2MBps*/
    154 /*6*/    {720 * 576, 3000000},    /*3MBps*/
    155 /*7*/    {1280 * 720, 3000000},   /*3MBps*/
    156 };
    157 
    158 OMX_U32 VIDENC_STRUCT_H263DEFBITRATE [VIDENC_MAXBITRATES][2] = {
    159 /*1*/    {176 * 144, 128000},     /*128KBps*/
    160 /*2*/    {320 * 240, 400000},     /*400KBps*/
    161 /*3*/    {352 * 288, 500000},     /*500kBps*/
    162 /*4*/    {640 * 480, 1500000},    /*1.5MBps*/
    163 /*5*/    {720 * 480, 2000000},    /*2MBps*/
    164 /*6*/    {720 * 576, 3000000},    /*3MBps*/
    165 /*7*/    {1280 * 720, 3000000},   /*3MBps*/
    166 };
    167 /*--------macro definitions ---------------------------------------------------*/
    168 
    169 static const int iQ16_Const = 1 << 16;
    170 static const float fQ16_Const = (float)(1 << 16);
    171 
    172 static float Q16Tof(int nQ16)
    173 {
    174     return nQ16 / fQ16_Const;
    175 }
    176 
    177 
    178 
    179 /*-----------------------------------------------------------------------------*/
    180 /**
    181   * ListCreate()
    182   *
    183   * Function call in OMX_ComponentInit(). Creates the List Head of the Component Memory List.
    184   *
    185   * @param pListHead VIDENC_NODE double pointer with the List Header of the Memory List.
    186   *
    187   * @retval OMX_ErrorNone
    188   *               OMX_ErrorInsufficientResources if the malloc fails
    189   *
    190   **/
    191 /*-----------------------------------------------------------------------------*/
    192 OMX_ERRORTYPE OMX_VIDENC_ListCreate(struct OMX_TI_Debug *dbg, struct VIDENC_NODE** pListHead)
    193 {
    194     OMX_ERRORTYPE eError = OMX_ErrorNone;
    195 
    196     *pListHead = (VIDENC_NODE*)malloc(sizeof(VIDENC_NODE)); /* need to malloc!!! */
    197     if (*pListHead == NULL)
    198     {
    199         OMX_TRACE4(*dbg, "malloc() out of memory error\n");
    200         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
    201     }
    202 
    203     OMX_TRACE1(*dbg, "Create MemoryListHeader[%p]\n", *pListHead);
    204     memset(*pListHead, 0x0, sizeof(VIDENC_NODE));
    205 
    206 OMX_CONF_CMD_BAIL:
    207     return eError;
    208 }
    209 
    210 /*-----------------------------------------------------------------------------*/
    211 /**
    212   * ListAdd()
    213   *
    214   * Called inside VIDENC_MALLOC Macro to add a new node to Component Memory List
    215   *
    216   * @param pListHead VIDENC_NODE Points List Header of the Memory List.
    217   *                pData OMX_PTR points to the new allocated data.
    218   * @retval OMX_ErrorNone
    219   *               OMX_ErrorInsufficientResources if the malloc fails
    220   *
    221   **/
    222 /*-----------------------------------------------------------------------------*/
    223 
    224 OMX_ERRORTYPE OMX_VIDENC_ListAdd(struct OMX_TI_Debug *dbg, struct VIDENC_NODE* pListHead, OMX_PTR pData)
    225 {
    226     OMX_ERRORTYPE eError = OMX_ErrorNone;
    227     VIDENC_NODE* pTmp = NULL;
    228     VIDENC_NODE* pNewNode = NULL;
    229     pNewNode = (VIDENC_NODE*)malloc(sizeof(VIDENC_NODE)); /* need to malloc!!! */
    230     if (pNewNode == NULL)
    231     {
    232         OMX_TRACE4(*dbg, "malloc() out of memory error\n");
    233         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
    234     }
    235     memset(pNewNode, 0x0, sizeof(VIDENC_NODE));
    236     pNewNode->pData = pData;
    237     pNewNode->pNext = NULL;
    238     OMX_TRACE1(*dbg, "Add MemoryNode[%p] -> [%p]\n", pNewNode, pNewNode->pData);
    239     pTmp = pListHead;
    240 
    241     while (pTmp->pNext != NULL)
    242     {
    243         pTmp = pTmp->pNext;
    244     }
    245     pTmp->pNext = pNewNode;
    246 
    247 OMX_CONF_CMD_BAIL:
    248     return eError;
    249 }
    250 
    251 /*-----------------------------------------------------------------------------*/
    252 /**
    253   * ListRemove()
    254   *
    255   * Called inside VIDENC_FREE Macro remove  node from Component Memory List and free the memory pointed by the node.
    256   *
    257   * @param pListHead VIDENC_NODE Points List Header of the Memory List.
    258   *                pData OMX_PTR points to the new allocated data.
    259   * @retval OMX_ErrorNone
    260   *
    261   *
    262   **/
    263 /*-----------------------------------------------------------------------------*/
    264 
    265 OMX_ERRORTYPE OMX_VIDENC_ListRemove(struct OMX_TI_Debug *dbg, struct VIDENC_NODE* pListHead,
    266                                     OMX_PTR pData)
    267 {
    268     OMX_ERRORTYPE eError = OMX_ErrorNone;
    269     VIDENC_NODE* pNode = NULL;
    270     VIDENC_NODE* pTmp = NULL;
    271 
    272     pNode = pListHead;
    273 
    274     while (pNode->pNext != NULL)
    275     {
    276         if (pNode->pNext->pData == pData)
    277         {
    278             pTmp = pNode->pNext;
    279             pNode->pNext = pTmp->pNext;
    280             OMX_TRACE1(*dbg, "Remove MemoryNode[%p] -> [%p]\n", pTmp, pTmp->pData);
    281             free(pTmp->pData);
    282             free(pTmp);
    283             pTmp = NULL;
    284             break;
    285             /* VIDENC_ListPrint2(pListHead); */
    286         }
    287         pNode = pNode->pNext;
    288     }
    289     return eError;
    290 }
    291 
    292 /*-----------------------------------------------------------------------------*/
    293 /**
    294   * ListDestroy()
    295   *
    296   * Called inside OMX_ComponentDeInit()  Remove all nodes and free all the memory in the Component Memory List.
    297   *
    298   * @param pListHead VIDENC_NODE Points List Header of the Memory List.
    299   *
    300   * @retval OMX_ErrorNone
    301   *
    302   *
    303   **/
    304 /*-----------------------------------------------------------------------------*/
    305 
    306 OMX_ERRORTYPE OMX_VIDENC_ListDestroy(struct OMX_TI_Debug *dbg, struct VIDENC_NODE* pListHead)
    307 {
    308     OMX_ERRORTYPE eError = OMX_ErrorNone;
    309     VIDENC_NODE* pTmp = NULL;
    310     VIDENC_NODE* pNode = NULL;
    311     pNode = pListHead;
    312 
    313     while (pNode->pNext != NULL)
    314     {
    315         pTmp = pNode->pNext;
    316         pNode->pNext=pTmp->pNext;
    317         if (pTmp->pData != NULL)
    318         {
    319             OMX_TRACE0(*dbg, "Remove MemoryNode[%p] -> [%p]\n", pTmp, pTmp->pData);
    320             free(pTmp->pData);
    321             pTmp->pData = NULL;
    322         }
    323         free(pTmp);
    324         pTmp = NULL;
    325     }
    326 
    327     OMX_TRACE1(*dbg, "Destroy MemoryListHeader[%p]\n", pListHead);
    328     free(pListHead);
    329     return eError;
    330 }
    331 
    332 
    333 
    334 /*---------------------------------------------------------------------------------------*/
    335 /**
    336   *  OMX_VIDENC_EmptyDataPipes Wait until all buffers are processed
    337   *
    338   * @param pComponentPrivate pointer to the private video encoder structure
    339   *
    340   * @return None
    341   *
    342  **/
    343 /*---------------------------------------------------------------------------------------*/
    344 void OMX_VIDENC_EmptyDataPipes (VIDENC_COMPONENT_PRIVATE *pComponentPrivate)
    345 {
    346     pthread_mutex_lock(&bufferReturned_mutex);
    347     while (pComponentPrivate->EmptythisbufferCount != pComponentPrivate->EmptybufferdoneCount ||
    348            pComponentPrivate->FillthisbufferCount  != pComponentPrivate->FillbufferdoneCount) {
    349         pthread_cond_wait(&bufferReturned_condition, &bufferReturned_mutex);
    350     }
    351     pthread_mutex_unlock(&bufferReturned_mutex);
    352     ALOGI("Video encoder has returned all buffers");
    353 }
    354 
    355 void OMX_VIDENC_IncrementBufferCountByOne(OMX_U32 *count)
    356 {
    357     pthread_mutex_lock(&bufferReturned_mutex);
    358     (*count)++;
    359     pthread_mutex_unlock(&bufferReturned_mutex);
    360 }
    361 
    362 void OMX_VIDENC_SignalIfAllBuffersAreReturned(VIDENC_COMPONENT_PRIVATE *pComponentPrivate)
    363 {
    364     pthread_mutex_lock(&bufferReturned_mutex);
    365     if ((pComponentPrivate->EmptythisbufferCount == pComponentPrivate->EmptybufferdoneCount) &&
    366         (pComponentPrivate->FillthisbufferCount  == pComponentPrivate->FillbufferdoneCount)) {
    367         pthread_cond_broadcast(&bufferReturned_condition);
    368         ALOGI("Sending pthread signal that video encoder has returned all buffers to app");
    369     }
    370     pthread_mutex_unlock(&bufferReturned_mutex);
    371 }
    372 
    373 /*---------------------------------------------------------------------------------------*/
    374 /**
    375   *  OMX_VIDENC_HandleError() will handle the error and pass the component to Invalid
    376   *  State, and send the event to the client.
    377   * @param eError - OMX_ERRORTYPE that occur.
    378   *
    379   * @retval OMX_NoError              Success, ready to roll
    380   *         OMX_Error_BadParameter   The input parameter pointer is null
    381  **/
    382 /*---------------------------------------------------------------------------------------*/
    383 OMX_ERRORTYPE OMX_VIDENC_HandleError(VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
    384                                      OMX_ERRORTYPE eErrorCmp)
    385 {
    386     OMX_ERRORTYPE eError = OMX_ErrorNone;
    387     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
    388 
    389     pComponentPrivate->bHandlingFatalError = OMX_TRUE;
    390 
    391     OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    392                              OMX_EventError,
    393                              eErrorCmp,
    394                              OMX_TI_ErrorSevere,
    395                              NULL);
    396 
    397     switch (eErrorCmp)
    398     {
    399         case OMX_ErrorBadParameter:
    400         case OMX_ErrorPortUnresponsiveDuringAllocation:
    401         case OMX_ErrorUnsupportedIndex:
    402         case OMX_ErrorInsufficientResources:
    403             goto OMX_CONF_CMD_BAIL;
    404         default:
    405             ;
    406     }
    407 
    408     pComponentPrivate->bHideEvents = OMX_TRUE;
    409 
    410     eError = eErrorCmp;
    411     pComponentPrivate->eState = OMX_StateInvalid;
    412 
    413     OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    414                              OMX_EventError,
    415                              OMX_ErrorInvalidState,
    416                              OMX_TI_ErrorCritical,
    417                              NULL);
    418 
    419 OMX_CONF_CMD_BAIL:
    420     if (pComponentPrivate)
    421         pComponentPrivate->bHandlingFatalError = OMX_FALSE;
    422     return eError;
    423 }
    424 
    425 /*---------------------------------------------------------------------------------------*/
    426 /**
    427   *  OMX_VIDENC_HandleLcmlEvent() will handle the event from the LCML
    428  *  thread.
    429   * @param eError - OMX_ERRORTYPE that occur.
    430   *
    431   * @retval OMX_NoError              Success, ready to roll
    432   *         OMX_Error_BadParameter   The input parameter pointer is null
    433  **/
    434 /*---------------------------------------------------------------------------------------*/
    435 OMX_ERRORTYPE OMX_VIDENC_HandleLcmlEvent(VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
    436                                          TUsnCodecEvent eEvent, void* argsCb [])
    437 {
    438     OMX_ERRORTYPE eError = OMX_ErrorNone;
    439 
    440     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
    441 
    442     switch(eEvent)
    443     {
    444     case EMMCodecDspMessageRecieved:
    445         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecDspMessageRecieved\n");
    446         break;
    447     case EMMCodecBufferProcessed:
    448         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecBufferProcessed\n");
    449         break;
    450     case EMMCodecProcessingStarted:
    451         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecProcessingStarted\n");
    452         break;
    453     case EMMCodecProcessingPaused:
    454         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecProcessingPaused\n");
    455         break;
    456     case EMMCodecProcessingStoped:
    457         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecProcessingStoped\n");
    458         break;
    459     case EMMCodecProcessingEof:
    460         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecProcessingEof\n");
    461         break;
    462     case EMMCodecBufferNotProcessed:
    463         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecBufferNotProcessed\n");
    464         break;
    465     case EMMCodecAlgCtrlAck:
    466         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecAlgCtrlAck\n");
    467         break;
    468     case EMMCodecStrmCtrlAck:
    469         OMX_PRDSP1(pComponentPrivate->dbg, "EMMCodecStrmCtrlAck\n");
    470         break;
    471     case EMMCodecInternalError:
    472         OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecInternalError\n");
    473 #ifdef DSP_MMU_FAULT_HANDLING
    474         if((argsCb[4] == (void *)USN_ERR_UNKNOWN_MSG) && (argsCb[5] == (void*)NULL))
    475         {
    476             OMX_VIDENC_SET_ERROR_BAIL(eError, OMX_ErrorInvalidState, pComponentPrivate);
    477         }
    478         else
    479         {
    480            OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    481                                    OMX_EventError,
    482                                    OMX_ErrorHardware,
    483                                    OMX_TI_ErrorCritical,
    484                                    "Error Hardware\n");
    485             eError = OMX_ErrorHardware;
    486         }
    487 #else
    488            OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    489                                    OMX_EventError,
    490                                    OMX_ErrorHardware,
    491                                    OMX_TI_ErrorCritical,
    492                                    "Error Hardware\n");
    493            eError = OMX_ErrorHardware;
    494 #endif
    495         break;
    496     case EMMCodecInitError:
    497         OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecInitError\n");
    498 #ifdef DSP_MMU_FAULT_HANDLING
    499         if((argsCb[4] == (void *)USN_ERR_UNKNOWN_MSG) && (argsCb[5] == (void*)NULL))
    500         {
    501             OMX_VIDENC_SET_ERROR_BAIL(eError, OMX_ErrorInvalidState, pComponentPrivate);
    502         }
    503         else
    504         {
    505            OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    506                                    OMX_EventError,
    507                                    OMX_ErrorHardware,
    508                                    OMX_TI_ErrorCritical,
    509                                    "Error Hardware\n");
    510             eError = OMX_ErrorHardware;
    511         }
    512 #else
    513        OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    514                                    OMX_EventError,
    515                                    OMX_ErrorHardware,
    516                                    OMX_TI_ErrorCritical,
    517                                    "Error Hardware\n");
    518         eError = OMX_ErrorHardware;
    519 #endif
    520         break;
    521     case EMMCodecDspError:
    522         OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecDspError\n");
    523 #ifdef DSP_MMU_FAULT_HANDLING
    524         if((argsCb[4] == (void *)NULL) && (argsCb[5] == (void*)NULL))
    525         {
    526             OMX_VIDENC_SET_ERROR_BAIL(eError, OMX_ErrorInvalidState, pComponentPrivate);
    527         }
    528         else
    529         {
    530        OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    531                                    OMX_EventError,
    532                                    OMX_ErrorHardware,
    533                                    OMX_TI_ErrorCritical,
    534                                    "Error Hardware\n");
    535             eError = OMX_ErrorHardware;
    536         }
    537 #else
    538        OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    539                                    OMX_EventError,
    540                                    OMX_ErrorHardware,
    541                                    OMX_TI_ErrorCritical,
    542                                    "Error Hardware\n");
    543         eError = OMX_ErrorHardware;
    544 #endif
    545         break;
    546     }
    547 
    548 OMX_CONF_CMD_BAIL:
    549     return eError;
    550 }
    551 
    552 /*-----------------------------------------------------------------------------*/
    553 /**
    554   * Disable Port()
    555   *
    556   * Called by component thread, handles commands sent by the app.
    557   *
    558   * @param
    559   *
    560   * @retval OMX_ErrorNone                  success, ready to roll
    561   *
    562   **/
    563 /*-----------------------------------------------------------------------------*/
    564 OMX_ERRORTYPE OMX_VIDENC_HandleCommandDisablePort (VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
    565                                                    OMX_U32 nParam1)
    566 {
    567     OMX_U8 i = 0;
    568     OMX_BOOL bFlushFlag;
    569     OMX_COMPONENTTYPE* pHandle = NULL;
    570     VIDENC_NODE* pMemoryListHead = NULL;
    571     OMX_ERRORTYPE eError = OMX_ErrorNone;
    572     VIDEOENC_PORT_TYPE* pCompPortIn = NULL;
    573     VIDEOENC_PORT_TYPE* pCompPortOut = NULL;
    574     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
    575     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
    576 
    577     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
    578 
    579     pHandle         = (OMX_COMPONENTTYPE*)pComponentPrivate->pHandle;
    580     pCompPortIn     = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
    581     pCompPortOut    = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
    582     pPortDefIn      = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
    583     pPortDefOut     = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
    584     pMemoryListHead = pComponentPrivate->pMemoryListHead;
    585 
    586     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pHandle, pPortDefIn, pPortDefOut);
    587 
    588     if (nParam1 == VIDENC_INPUT_PORT || nParam1 == (OMX_U32)-1)
    589     {
    590         /* Flush the DSP side before sending buffers back to the client */
    591         bFlushFlag = OMX_FALSE;
    592         for (i = 0; i < pPortDefIn->nBufferCountActual; i++)
    593         {
    594             if(pCompPortIn->pBufferPrivate[i]->eBufferOwner != VIDENC_BUFFER_WITH_CLIENT)
    595             {
    596                 bFlushFlag = OMX_TRUE;
    597             }
    598         }
    599 
    600         if(bFlushFlag == OMX_TRUE)
    601         {
    602             eError = OMX_VIDENC_HandleCommandFlush(pComponentPrivate, VIDENC_INPUT_PORT, OMX_TRUE);
    603             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRBUFFER3,
    604                                   "Flush command failed (%x)\n", eError);
    605             bFlushFlag = OMX_FALSE;
    606         }
    607 
    608         /*Return buffer to client*/
    609         if (pCompPortIn->hTunnelComponent == NULL)
    610         {
    611             for (i = 0; i < pPortDefIn->nBufferCountActual; i++)
    612             {
    613                 if (pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
    614                     pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
    615                 {
    616                     pCompPortIn->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
    617 #ifdef __PERF_INSTRUMENTATION__
    618                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    619                                       PREF(pCompPortIn->pBufferPrivate[i]->pBufferHdr, pBuffer),
    620                                       0,
    621                                       PERF_ModuleHLMM);
    622 #endif
    623                     pComponentPrivate->sCbData.EmptyBufferDone(pComponentPrivate->pHandle,
    624                                                                pComponentPrivate->pHandle->pApplicationPrivate,
    625                                                                pCompPortIn->pBufferPrivate[i]->pBufferHdr);
    626                     OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->EmptybufferdoneCount);
    627                     OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
    628                 }
    629             }
    630         }
    631         else
    632         {
    633             for (i = 0; i < pPortDefIn->nBufferCountActual; i++)
    634             {
    635                 if (pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
    636                     pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
    637                 {
    638                     pCompPortIn->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
    639 #ifdef __PERF_INSTRUMENTATION__
    640                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    641                                       PREF(pCompPortIn->pBufferPrivate[i]->pBufferHdr, pBuffer),
    642                                       0,
    643                                       PERF_ModuleLLMM);
    644 #endif
    645                     eError = OMX_FillThisBuffer(pCompPortIn->hTunnelComponent,
    646                                                 pCompPortIn->pBufferPrivate[i]->pBufferHdr);
    647                     OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRBUFFER4,
    648                                           "FillThisBuffer failed (%x)\n", eError);
    649                 }
    650             }
    651         }
    652     }
    653     if (nParam1 == VIDENC_OUTPUT_PORT || nParam1 == (OMX_U32)-1)
    654     {
    655         /* Flush the DSP side before sending buffers back to the client */
    656         bFlushFlag = OMX_FALSE;
    657         for (i = 0; i < pPortDefOut->nBufferCountActual; i++)
    658         {
    659             if(pCompPortOut->pBufferPrivate[i]->eBufferOwner != VIDENC_BUFFER_WITH_CLIENT)
    660             {
    661                 bFlushFlag = OMX_TRUE;
    662             }
    663         }
    664 
    665         if(bFlushFlag == OMX_TRUE)
    666         {
    667             eError = OMX_VIDENC_HandleCommandFlush(pComponentPrivate, VIDENC_OUTPUT_PORT, OMX_TRUE);
    668             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRBUFFER3,
    669                                   "Flush command failed (%x)\n", eError);
    670             bFlushFlag = OMX_FALSE;
    671         }
    672 
    673         /*Return buffer to client*/
    674         if (pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->hTunnelComponent == NULL)
    675         {
    676             for (i = 0; i < pPortDefOut->nBufferCountActual; i++)
    677             {
    678                 if (pCompPortOut->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
    679                     pCompPortOut->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
    680                 {
    681 
    682                     pCompPortOut->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
    683 #ifdef __PERF_INSTRUMENTATION__
    684                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    685                                       pCompPortOut->pBufferPrivate[i]->pBufferHdr ?
    686                                       pCompPortOut->pBufferPrivate[i]->pBufferHdr->pBuffer :
    687                                       NULL,
    688                                       pCompPortOut->pBufferPrivate[i]->pBufferHdr ?
    689                                       pCompPortOut->pBufferPrivate[i]->pBufferHdr->nFilledLen :
    690                                       0,
    691                                       PERF_ModuleHLMM);
    692 #endif
    693                     OMX_CONF_CIRCULAR_BUFFER_MOVE_HEAD(pCompPortOut->pBufferPrivate[i]->pBufferHdr,
    694                                                        pComponentPrivate->sCircularBuffer,
    695                                                        pComponentPrivate);
    696                     /* trigger event handler if we are supposed to */
    697                     if (pCompPortOut->pBufferPrivate[i]->pBufferHdr->hMarkTargetComponent == pComponentPrivate->pHandle &&
    698                         pCompPortOut->pBufferPrivate[i]->pBufferHdr->pMarkData)
    699                     {
    700                         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    701                                                  OMX_EventMark,
    702                                                  0x0,
    703                                                  0x0,
    704                                                  pCompPortOut->pBufferPrivate[i]->pBufferHdr->pMarkData);
    705                     }
    706                     pComponentPrivate->sCbData.FillBufferDone(pComponentPrivate->pHandle,
    707                                                               pComponentPrivate->pHandle->pApplicationPrivate,
    708                                                               pCompPortOut->pBufferPrivate[i]->pBufferHdr);
    709                     OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->FillbufferdoneCount);
    710                     OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
    711                 }
    712             }
    713         }
    714         else
    715         {
    716          /* If tunneled with VPP  - NOT Implemented*/
    717 #ifdef __PERF_INSTRUMENTATION__
    718                 PERF_SendingFrame(pComponentPrivate->pPERFcomp,
    719                                   PREF(pCompPortOut->pBufferPrivate[i]->pBufferHdr,pBuffer),
    720                                   PREF(pCompPortOut->pBufferPrivate[i]->pBufferHdr,nFilledLen),
    721                                   PERF_ModuleLLMM);
    722 #endif
    723         }
    724     }
    725 
    726     OMX_PRBUFFER2(pComponentPrivate->dbg, "Flushing Pipes!\n");
    727     OMX_VIDENC_EmptyDataPipes (pComponentPrivate);
    728 
    729     /*while (1)
    730     {*/
    731 
    732         if (nParam1 == VIDENC_INPUT_PORT)
    733         {
    734             while ((pPortDefIn->bPopulated))
    735             {
    736                 /*Send event*/
    737 #ifndef UNDER_CE
    738     pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    739     pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->videoe_mutex_app);
    740     pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    741 #else
    742         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    743 #endif
    744         break;
    745                 }
    746                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    747                              OMX_EventCmdComplete,
    748                              OMX_CommandPortDisable,
    749                              VIDENC_INPUT_PORT,
    750                              NULL);
    751 
    752             }
    753 
    754         else if (nParam1 == VIDENC_OUTPUT_PORT)
    755         {
    756             while ((pPortDefOut->bPopulated))
    757             {
    758                 /*Send event*/
    759 #ifndef UNDER_CE
    760     pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    761     pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->videoe_mutex_app);
    762     pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    763 #else
    764         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    765 #endif
    766         break;
    767                 }
    768         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    769                              OMX_EventCmdComplete,
    770                              OMX_CommandPortDisable,
    771                              VIDENC_OUTPUT_PORT,
    772                              NULL);
    773 
    774             }
    775 
    776         else if (nParam1 == (OMX_U32)-1)
    777         {
    778             while ((pPortDefIn->bPopulated) || (pPortDefOut->bPopulated))
    779             {
    780         /*Send events*/
    781 #ifndef UNDER_CE
    782     pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    783     pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->videoe_mutex_app);
    784     pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    785 #else
    786         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    787 #endif
    788         break;
    789                 }
    790         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    791                              OMX_EventCmdComplete,
    792                              OMX_CommandPortDisable,
    793                              VIDENC_INPUT_PORT,
    794                              NULL);
    795                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    796                              OMX_EventCmdComplete,
    797                              OMX_CommandPortDisable,
    798                              VIDENC_OUTPUT_PORT,
    799                              NULL);
    800 
    801 
    802         }
    803 
    804 
    805 OMX_CONF_CMD_BAIL:
    806     return eError;
    807 }
    808 
    809 /*-----------------------------------------------------------------------------*/
    810 /**
    811   * Enable Port()
    812   *
    813   * Called by component thread, handles commands sent by the app.
    814   *
    815   * @param
    816   *
    817   * @retval OMX_ErrorNone                  success, ready to roll
    818   *
    819   **/
    820 /*-----------------------------------------------------------------------------*/
    821 
    822 OMX_ERRORTYPE OMX_VIDENC_HandleCommandEnablePort (VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
    823                                                   OMX_U32 nParam1)
    824 {
    825     OMX_U32 nTimeout = 0x0;
    826     OMX_COMPONENTTYPE* pHandle = NULL;
    827     VIDENC_NODE* pMemoryListHead = NULL;
    828     OMX_ERRORTYPE eError = OMX_ErrorNone;
    829     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
    830     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
    831 
    832     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
    833 
    834     pHandle = (OMX_COMPONENTTYPE*)pComponentPrivate->pHandle;
    835     pPortDefIn      = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
    836     pPortDefOut     = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
    837     pMemoryListHead = pComponentPrivate->pMemoryListHead;
    838 
    839     nTimeout = 0x0;
    840     /*while(1)
    841     {*/
    842         if(nParam1 == VIDENC_INPUT_PORT)
    843         {
    844         if (pComponentPrivate->eState != OMX_StateLoaded)
    845         {
    846         pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    847             while (!pPortDefIn->bPopulated)
    848                {
    849 #ifndef UNDER_CE
    850         pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->videoe_mutex_app);
    851 #else
    852         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    853 #endif
    854 
    855                 }
    856     }
    857     pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    858         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    859                                              OMX_EventCmdComplete,
    860                                              OMX_CommandPortEnable,
    861                                              VIDENC_INPUT_PORT,
    862                                              NULL);
    863 
    864 
    865         }
    866         else if(nParam1 == VIDENC_OUTPUT_PORT)
    867         {
    868         if (pComponentPrivate->eState != OMX_StateLoaded)
    869         {
    870             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    871             while(!pPortDefOut->bPopulated)
    872             {
    873 #ifndef UNDER_CE
    874         pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->videoe_mutex_app);
    875 #else
    876         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    877 #endif
    878 
    879                 }
    880         }
    881             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    882                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    883                                          OMX_EventCmdComplete,
    884                                          OMX_CommandPortEnable,
    885                                          VIDENC_OUTPUT_PORT,
    886                                          NULL);
    887 
    888 
    889         }
    890         else if(nParam1 == (OMX_U32)-1)
    891         {
    892         if (pComponentPrivate->eState != OMX_StateLoaded)
    893         {
    894             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    895             while(!pPortDefOut->bPopulated && !pPortDefIn->bPopulated)
    896             {
    897 #ifndef UNDER_CE
    898             pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->videoe_mutex_app);
    899 #else
    900         OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
    901 #endif
    902       break;
    903                 }
    904         }
    905             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    906                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    907                                          OMX_EventCmdComplete,
    908                                          OMX_CommandPortEnable,
    909                                          VIDENC_INPUT_PORT,
    910                                          NULL);
    911                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
    912                                         OMX_EventCmdComplete,
    913                                         OMX_CommandPortEnable,
    914                                         VIDENC_OUTPUT_PORT,
    915                                         NULL);
    916 
    917 
    918         }
    919 
    920 
    921 OMX_CONF_CMD_BAIL:
    922     return eError;
    923 }
    924 
    925 /*----------------------------------------------------------------------------*/
    926 /**
    927   * OMX_OMX_VIDENC_HandleCommandFlush()
    928   *
    929   * Called by component thread, handles commands sent by the app.
    930   *
    931   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
    932   *
    933   * @retval OMX_ErrorNone                  success, ready to roll
    934   *         OMX_ErrorInsufficientResources if the malloc fails
    935   **/
    936 /*----------------------------------------------------------------------------*/
    937 
    938 OMX_ERRORTYPE OMX_VIDENC_HandleCommandFlush(VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
    939                                             OMX_U32 nParam1,
    940                                             OMX_BOOL bInternalFlush)
    941 {
    942     OMX_U16 i = 0;
    943     OMX_U32 aParam[3] = {0};
    944     OMX_COMPONENTTYPE* pHandle = NULL;
    945     VIDENC_NODE* pMemoryListHead = NULL;
    946     OMX_ERRORTYPE eError = OMX_ErrorNone;
    947     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
    948     VIDEOENC_PORT_TYPE* pCompPortIn = NULL;
    949     VIDEOENC_PORT_TYPE* pCompPortOut = NULL;
    950     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
    951     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
    952 
    953     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
    954 
    955     pLcmlHandle     = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
    956     pCompPortIn     = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
    957     pCompPortOut    = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
    958     pPortDefIn      = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
    959     pPortDefOut     = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
    960     pHandle         = (OMX_COMPONENTTYPE*)pComponentPrivate->pHandle;
    961     pMemoryListHead = pComponentPrivate->pMemoryListHead;
    962 
    963     if (nParam1 == VIDENC_INPUT_PORT || nParam1 == (OMX_U32)-1)
    964     {
    965         aParam[0] = USN_STRMCMD_FLUSH;
    966         aParam[1] = VIDENC_INPUT_PORT;
    967         aParam[2] = 0x0;
    968 
    969         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
    970                                     EMMCodecControlStrmCtrl,
    971                                     (void*)aParam);
    972         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
    973                               "DSP Input flush failed (%x).\n", eError);
    974 
    975 #ifndef UNDER_CE
    976         pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
    977         while (pComponentPrivate->bFlushComplete == OMX_FALSE)
    978         {
    979             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->videoe_mutex_app);
    980         }
    981         pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
    982 #else
    983         while (pComponentPrivate->bFlushComplete == OMX_FALSE)
    984         {
    985             sched_yield();
    986         }
    987 #endif
    988 
    989         pComponentPrivate->bFlushComplete = OMX_FALSE;
    990 
    991         if (pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->hTunnelComponent == NULL)
    992         {
    993             for (i = 0; i < pPortDefIn->nBufferCountActual; i++)
    994             {
    995                 if (pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
    996                     pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
    997                 {
    998 
    999                     pCompPortIn->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   1000 #ifdef __PERF_INSTRUMENTATION__
   1001                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1002                                       PREF(pCompPortIn->pBufferPrivate[i]->pBufferHdr,pBuffer),
   1003                                       0,
   1004                                       PERF_ModuleHLMM);
   1005 #endif
   1006                     pComponentPrivate->sCbData.EmptyBufferDone(pComponentPrivate->pHandle,
   1007                                                                pComponentPrivate->pHandle->pApplicationPrivate,
   1008                                                                pCompPortIn->pBufferPrivate[i]->pBufferHdr);
   1009                     OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->EmptybufferdoneCount);
   1010                     OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   1011                 }
   1012             }
   1013         }
   1014         else
   1015         {
   1016             for (i = 0; i < pPortDefIn->nBufferCountActual; i++)
   1017             {
   1018                 if (pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
   1019                     pCompPortIn->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
   1020                 {
   1021 
   1022                     pCompPortIn->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
   1023 #ifdef __PERF_INSTRUMENTATION__
   1024                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1025                                       PREF(pCompPortOut->pBufferPrivate[i]->pBufferHdr,pBuffer),
   1026                                       0,
   1027                                       PERF_ModuleLLMM);
   1028 #endif
   1029                     eError = OMX_FillThisBuffer(pCompPortIn->hTunnelComponent,
   1030                                                 pCompPortIn->pBufferPrivate[i]->pBufferHdr);
   1031                     OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRBUFFER4,
   1032                                           "FillThisBuffer failed (%x)\n", eError);
   1033                 }
   1034             }
   1035         }
   1036         if (bInternalFlush == OMX_FALSE)
   1037         {
   1038             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1039                                      OMX_EventCmdComplete,
   1040                                      OMX_CommandFlush,
   1041                                      VIDENC_INPUT_PORT,
   1042                                      NULL);
   1043         }
   1044     }
   1045     if (nParam1 == VIDENC_OUTPUT_PORT || nParam1 == (OMX_U32)-1)
   1046     {
   1047         aParam[0] = USN_STRMCMD_FLUSH;
   1048         aParam[1] = VIDENC_OUTPUT_PORT;
   1049         aParam[2] = 0x0;
   1050 
   1051         eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1052                                    EMMCodecControlStrmCtrl,
   1053                                    (void*)aParam);
   1054         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1055                               "DSP Output flush failed (%x).\n", eError);
   1056 #ifndef UNDER_CE
   1057         pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   1058         while (pComponentPrivate->bFlushComplete == OMX_FALSE)
   1059         {
   1060             pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->videoe_mutex_app);
   1061         }
   1062         pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   1063 #else
   1064         while (pComponentPrivate->bFlushComplete == OMX_FALSE)
   1065         {
   1066             sched_yield();
   1067         }
   1068 #endif
   1069 
   1070         pComponentPrivate->bFlushComplete = OMX_FALSE;
   1071 
   1072         if (pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->hTunnelComponent == NULL)
   1073         {
   1074             for (i = 0; i < pPortDefOut->nBufferCountActual; i++)
   1075             {
   1076                 if (pCompPortOut->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT ||
   1077                     pCompPortOut->pBufferPrivate[i]->eBufferOwner == VIDENC_BUFFER_WITH_DSP)
   1078                 {
   1079 
   1080                     pCompPortOut->pBufferPrivate[i]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   1081 #ifdef __PERF_INSTRUMENTATION__
   1082                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1083                                       PREF(pCompPortOut->pBufferPrivate[i]->pBufferHdr,pBuffer),
   1084                                       PREF(pCompPortOut->pBufferPrivate[i]->pBufferHdr,nFilledLen),
   1085                                       PERF_ModuleHLMM);
   1086 #endif
   1087                     /*Copy Buffer Data to be propagated*/
   1088                     OMX_CONF_CIRCULAR_BUFFER_MOVE_HEAD(pCompPortOut->pBufferPrivate[i]->pBufferHdr,
   1089                                                        pComponentPrivate->sCircularBuffer,
   1090                                                        pComponentPrivate);
   1091                      /* trigger event handler if we are supposed to */
   1092                     if (pCompPortOut->pBufferPrivate[i]->pBufferHdr->hMarkTargetComponent == pComponentPrivate->pHandle &&
   1093                         pCompPortOut->pBufferPrivate[i]->pBufferHdr->pMarkData)
   1094                     {
   1095                         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1096                                                  OMX_EventMark,
   1097                                                  0x0,
   1098                                                  0x0,
   1099                                                  pCompPortOut->pBufferPrivate[i]->pBufferHdr->pMarkData);
   1100                     }
   1101                     pComponentPrivate->sCbData.FillBufferDone(pComponentPrivate->pHandle,
   1102                                                               pComponentPrivate->pHandle->pApplicationPrivate,
   1103                                                               pCompPortOut->pBufferPrivate[i]->pBufferHdr);
   1104                     OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->FillbufferdoneCount);
   1105                     OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   1106                 }
   1107             }
   1108         }
   1109         else
   1110         {
   1111             /* If tunneled with VPP  - NOT Implemented*/
   1112         }
   1113         if (bInternalFlush == OMX_FALSE)
   1114         {
   1115             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1116                                      OMX_EventCmdComplete,
   1117                                      OMX_CommandFlush,
   1118                                      VIDENC_OUTPUT_PORT,
   1119                                      NULL);
   1120         }
   1121     }
   1122 
   1123 OMX_CONF_CMD_BAIL:
   1124     return eError;
   1125 }
   1126 
   1127 /*----------------------------------------------------------------------------*/
   1128 /**
   1129   * OMX_VIDENC_HandleCommand()
   1130   *
   1131   * Called by component thread, handles commands sent by the app.
   1132   *
   1133   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   1134   *
   1135   * @retval OMX_ErrorNone                  success, ready to roll
   1136   *         OMX_ErrorInsufficientResources if the malloc fails
   1137   **/
   1138 /*----------------------------------------------------------------------------*/
   1139 OMX_ERRORTYPE OMX_VIDENC_HandleCommandStateSet (VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
   1140                                                 OMX_U32 nParam1)
   1141 {
   1142     VIDENC_NODE* pMemoryListHead = NULL;
   1143     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1144     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1145     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1146 
   1147     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   1148 
   1149     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   1150     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   1151     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   1152 
   1153     switch (nParam1)
   1154     {
   1155     case OMX_StateIdle:
   1156         eError = OMX_VIDENC_HandleCommandStateSetIdle (pComponentPrivate);
   1157         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRSTATE3,
   1158                               "Failed to move to Idle state (%x).\n", eError);
   1159         break;
   1160     case OMX_StateExecuting:
   1161         eError = OMX_VIDENC_HandleCommandStateSetExecuting (pComponentPrivate);
   1162         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRSTATE3,
   1163                               "Failed to move to Execute state (%x).\n", eError);
   1164         break;
   1165     case OMX_StateLoaded:
   1166         eError = OMX_VIDENC_HandleCommandStateSetLoaded (pComponentPrivate);
   1167         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRSTATE3,
   1168                               "Failed to move to Loaded state (%x).\n", eError);
   1169         break;
   1170     case OMX_StatePause:
   1171         eError = OMX_VIDENC_HandleCommandStateSetPause (pComponentPrivate);
   1172         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRSTATE3,
   1173                               "Failed to move to Pause state (%x).\n", eError);
   1174         break;
   1175     case OMX_StateInvalid:
   1176             if (pComponentPrivate->eState == OMX_StateInvalid)
   1177             {
   1178                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1179                                          OMX_EventError,
   1180                                          OMX_ErrorSameState,
   1181                                          OMX_TI_ErrorSevere,
   1182                                          NULL);
   1183             }
   1184             else
   1185             {
   1186                 pComponentPrivate->eState = OMX_StateInvalid;
   1187                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1188                                          OMX_EventError,
   1189                                          OMX_ErrorInvalidState,
   1190                                          OMX_TI_ErrorSevere,
   1191                                          NULL);
   1192                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1193                                          OMX_EventCmdComplete,
   1194                                          OMX_CommandStateSet,
   1195                                          OMX_StateInvalid,
   1196                                          NULL);
   1197             }
   1198             break;
   1199         case OMX_StateWaitForResources:
   1200             if (pComponentPrivate->eState == OMX_StateWaitForResources)
   1201             {
   1202                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1203                                          OMX_EventError,
   1204                                          OMX_ErrorSameState,
   1205                                          OMX_TI_ErrorMinor,
   1206                                          NULL);
   1207             }
   1208             else if (pComponentPrivate->eState == OMX_StateLoaded)
   1209             {
   1210                 pComponentPrivate->eState = OMX_StateWaitForResources;
   1211                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1212                                          OMX_EventCmdComplete,
   1213                                          OMX_CommandStateSet,
   1214                                          OMX_StateWaitForResources,
   1215                                          NULL);
   1216             }
   1217             else
   1218             {
   1219                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1220                                          OMX_EventError,
   1221                                          OMX_ErrorIncorrectStateTransition,
   1222                                          OMX_TI_ErrorMinor,
   1223                                          NULL);
   1224             }
   1225             break;
   1226         case OMX_StateMax:
   1227             break;
   1228     } /* End of Switch */
   1229 
   1230 OMX_CONF_CMD_BAIL:
   1231     return eError;
   1232 }
   1233 
   1234 /*----------------------------------------------------------------------------*/
   1235 /**
   1236   * OMX_VIDENC_HandleCommandStateSet()
   1237   *
   1238   * Called by component thread, handles commands sent by the app.
   1239   *
   1240   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   1241   *
   1242   * @retval OMX_ErrorNone                  success, ready to roll
   1243   *         OMX_ErrorInsufficientResources if the malloc fails
   1244   **/
   1245 /*----------------------------------------------------------------------------*/
   1246 OMX_ERRORTYPE OMX_VIDENC_HandleCommandStateSetIdle(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   1247 {
   1248     OMX_U8 nCount = 0;
   1249     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1250     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   1251     VIDEOENC_PORT_TYPE* pCompPortIn = NULL;
   1252     VIDEOENC_PORT_TYPE* pCompPortOut = NULL;
   1253     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1254     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1255 
   1256 
   1257     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   1258 
   1259     pCompPortIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT];
   1260     pCompPortOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
   1261     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   1262     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   1263 
   1264 
   1265     switch (pComponentPrivate->eState)
   1266     {
   1267         case OMX_StateIdle:
   1268             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1269                                      OMX_EventError,
   1270                                      OMX_ErrorSameState,
   1271                                      OMX_TI_ErrorMinor,
   1272                                      NULL);
   1273             break;
   1274         case OMX_StateInvalid:
   1275             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1276                                      OMX_EventError,
   1277                                      OMX_ErrorIncorrectStateTransition,
   1278                                      OMX_TI_ErrorMajor,
   1279                                      NULL);
   1280             break;
   1281         case OMX_StateLoaded:
   1282         case OMX_StateWaitForResources:
   1283 #ifdef __PERF_INSTRUMENTATION__
   1284                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   1285                               PERF_BoundaryStart | PERF_BoundarySetup);
   1286 #endif
   1287         if ( pPortDefIn->bEnabled == OMX_TRUE || pPortDefOut->bEnabled == OMX_TRUE )
   1288         {
   1289             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   1290             while ( (!pPortDefIn->bPopulated) || (!pPortDefOut->bPopulated))
   1291             {
   1292 #ifndef UNDER_CE
   1293                 pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->videoe_mutex_app);
   1294 #else
   1295             OMX_WaitForEvent(&(pComponentPrivate->InLoaded_event));
   1296 #endif
   1297             }
   1298             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   1299         }
   1300             /* Make sure the DSP node has been deleted first to cover
   1301                    any idle->loaded->idle or idle->wfr->idle combinations
   1302                    */
   1303             if (pComponentPrivate->bCodecStarted == OMX_TRUE ||
   1304                 pComponentPrivate->bCodecLoaded == OMX_TRUE)
   1305             {
   1306             OMX_PRDSP2(pComponentPrivate->dbg, "Attempting to destroy the node...\n");
   1307                 pLcmlHandle = NULL;
   1308                 pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1309                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1310                                            EMMCodecControlDestroy,
   1311                                            NULL);
   1312             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP3,
   1313                                   "Failed to destroy socket node (%x).\n", eError);
   1314 
   1315 
   1316                 /*Unload LCML */
   1317                 if(pComponentPrivate->pModLcml != NULL)
   1318                 {
   1319 #ifndef UNDER_CE
   1320                     dlclose(pComponentPrivate->pModLcml);
   1321 #else
   1322                     FreeLibrary(pComponentPrivate->pModLcml);
   1323                 FreeLibrary(g_hLcmlDllHandle);
   1324                 g_hLcmlDllHandle = NULL;
   1325 
   1326 #endif
   1327                    pComponentPrivate->pModLcml = NULL;
   1328                    pComponentPrivate->pLCML = NULL;
   1329                 }
   1330 
   1331                 if (pComponentPrivate->sps) {
   1332                     free(pComponentPrivate->sps);
   1333                     pComponentPrivate->sps = NULL;
   1334                     pComponentPrivate->spsLen = 0;
   1335                 }
   1336 
   1337                 pComponentPrivate->bCodecStarted = OMX_FALSE;
   1338                 pComponentPrivate->bCodecLoaded = OMX_FALSE;
   1339             }
   1340 
   1341             eError = OMX_VIDENC_InitLCML(pComponentPrivate);
   1342         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1343                               "Failed to initialize LCML (%x).\n", eError);
   1344 
   1345 #ifdef __PERF_INSTRUMENTATION__
   1346             pComponentPrivate->nLcml_nCntIp = 0;
   1347             pComponentPrivate->nLcml_nCntOpReceived = 0;
   1348 #endif
   1349             if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   1350             {
   1351                 eError = OMX_VIDENC_InitDSP_H264Enc(pComponentPrivate);
   1352             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1353                                   "Failed to initialize H264 SN (%x).\n", eError);
   1354             }
   1355             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   1356                      pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   1357             {
   1358                 eError = OMX_VIDENC_InitDSP_Mpeg4Enc(pComponentPrivate);
   1359             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1360                                   "Failed to initialize MPEG4 SN (%x).\n", eError);
   1361             }
   1362             else
   1363             {
   1364             OMX_PRSTATE4(pComponentPrivate->dbg, "Unsupported compression format (%d)\n",
   1365                          pPortDefOut->format.video.eCompressionFormat);
   1366                 OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   1367             }
   1368 
   1369 #ifdef RESOURCE_MANAGER_ENABLED
   1370 
   1371         OMX_PRMGR2(pComponentPrivate->dbg, "Setting CallBack In Video Encoder component\n");
   1372              pComponentPrivate->cRMCallBack.RMPROXY_Callback = (void*)OMX_VIDENC_ResourceManagerCallBack;
   1373             switch (pPortDefOut->format.video.eCompressionFormat)
   1374             {
   1375                 case OMX_VIDEO_CodingAVC:
   1376                      switch(pComponentPrivate->pH264->eLevel)
   1377                      {
   1378                         case OMX_VIDEO_AVCLevel1:
   1379                         case OMX_VIDEO_AVCLevel1b:
   1380                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1381                                                          RMProxy_RequestResource,
   1382                                                          OMX_H264_Encode_COMPONENT,
   1383                                                          VIDEO_ENCODER_MHZ,
   1384                                                          3456,
   1385                                                          &(pComponentPrivate->cRMCallBack));
   1386 
   1387                             break;
   1388                         case OMX_VIDEO_AVCLevel11:
   1389                         case OMX_VIDEO_AVCLevel12:
   1390                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1391                                                          RMProxy_RequestResource,
   1392                                                          OMX_H264_Encode_COMPONENT,
   1393                                                          VIDEO_ENCODER_MHZ,
   1394                                                          3456,
   1395                                                          &(pComponentPrivate->cRMCallBack));
   1396                             break;
   1397                         case OMX_VIDEO_AVCLevel13:
   1398                         case OMX_VIDEO_AVCLevel2:
   1399                         case OMX_VIDEO_AVCLevel21:
   1400                         case OMX_VIDEO_AVCLevel22:
   1401                         case OMX_VIDEO_AVCLevel3:
   1402                          default:
   1403                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1404                                                             RMProxy_RequestResource,
   1405                                                             OMX_H264_Encode_COMPONENT,
   1406                                                             VIDEO_ENCODER_MHZ,
   1407                                                             3456,
   1408                                                             &(pComponentPrivate->cRMCallBack));
   1409                     }
   1410                     break;
   1411                 case OMX_VIDEO_CodingMPEG4:
   1412                     switch(pComponentPrivate->pMpeg4->eLevel)
   1413                     {
   1414                         case 0:
   1415                         case 1:
   1416                         case 100:
   1417                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1418                                                          RMProxy_RequestResource,
   1419                                                          OMX_MPEG4_Encode_COMPONENT,
   1420                                                          VIDEO_ENCODER_MHZ,
   1421                                                          3456,
   1422                                                          &(pComponentPrivate->cRMCallBack));
   1423                             break;
   1424                         case 2:
   1425                         case 3:
   1426                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1427                                                          RMProxy_RequestResource,
   1428                                                          OMX_MPEG4_Encode_COMPONENT,
   1429                                                          VIDEO_ENCODER_MHZ,
   1430                                                          3456,
   1431                                                          &(pComponentPrivate->cRMCallBack));
   1432                             break;
   1433                         case 4:
   1434                         default:
   1435                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1436                                                          RMProxy_RequestResource,
   1437                                                          OMX_MPEG4_Encode_COMPONENT,
   1438                                                          VIDEO_ENCODER_MHZ,
   1439                                                          3456,
   1440                                                          &(pComponentPrivate->cRMCallBack));
   1441                     }
   1442                     break;
   1443                 case OMX_VIDEO_CodingH263:
   1444                     switch(pComponentPrivate->pH263->eLevel)
   1445                     {
   1446                         case OMX_VIDEO_H263Level10:
   1447                         case OMX_VIDEO_H263Level40:
   1448                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1449                                                          RMProxy_RequestResource,
   1450                                                          OMX_H263_Encode_COMPONENT,
   1451                                                          VIDEO_ENCODER_MHZ,
   1452                                                          3456,
   1453                                                          &(pComponentPrivate->cRMCallBack));
   1454                             break;
   1455                         case OMX_VIDEO_H263Level20:
   1456                         case OMX_VIDEO_H263Level30:
   1457                         default:
   1458                             eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1459                                                          RMProxy_RequestResource,
   1460                                                          OMX_H263_Encode_COMPONENT,
   1461                                                          VIDEO_ENCODER_MHZ,
   1462                                                          3456,
   1463                                                          &(pComponentPrivate->cRMCallBack));
   1464                     }
   1465                     break;
   1466                 default:
   1467                 OMX_PRSTATE4(pComponentPrivate->dbg, "Unsupported compression format (%d)\n",
   1468                              pPortDefOut->format.video.eCompressionFormat);
   1469                     OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   1470             }
   1471 
   1472             /* Resource Manager Proxy Calls */
   1473                 if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   1474                 {
   1475                     /* TODO: Disable RM Send for now */
   1476                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H264_Encode_COMPONENT, 0); */
   1477             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Idle from Loaded\n");
   1478                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1479                                                  RMProxy_StateSet,
   1480                                                  OMX_H264_Encode_COMPONENT,
   1481                                                  OMX_StateIdle,
   1482                                                  3456,
   1483                                                  NULL);
   1484                 }
   1485                 else if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   1486                 {
   1487                     /* TODO: Disable RM Send for now */
   1488                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_MPEG4_Encode_COMPONENT, 0); */
   1489             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Idle from Loaded\n");
   1490                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1491                                                  RMProxy_StateSet,
   1492                                                  OMX_MPEG4_Encode_COMPONENT,
   1493                                                  OMX_StateIdle,
   1494                                                  3456,
   1495                                                  NULL);
   1496                 }
   1497                 else if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   1498                 {
   1499                     /* TODO: Disable RM Send for now */
   1500                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H263_Encode_COMPONENT, 0); */
   1501             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Idle from Loaded\n");
   1502                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1503                                                  RMProxy_StateSet,
   1504                                                  OMX_H263_Encode_COMPONENT,
   1505                                                  OMX_StateIdle,
   1506                                                  3456,
   1507                                                  NULL);
   1508                 }
   1509         if (eError != OMX_ErrorNone)
   1510         {
   1511                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1512                                              OMX_EventError,
   1513                                              OMX_ErrorHardware,
   1514                                              OMX_TI_ErrorSevere,
   1515                                              NULL);
   1516                 }
   1517 
   1518            if (eError == OMX_ErrorNone) {
   1519 
   1520                pComponentPrivate->eState = OMX_StateIdle;
   1521 #ifdef __PERF_INSTRUMENTATION__
   1522                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   1523                             PERF_BoundaryComplete | PERF_BoundarySetup);
   1524 #endif
   1525 
   1526                /* Decrement reference count with signal enabled */
   1527                if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1528                      return OMX_ErrorUndefined;
   1529                }
   1530 
   1531                OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1532                                          OMX_EventCmdComplete,
   1533                                          OMX_CommandStateSet,
   1534                                          OMX_StateIdle,
   1535                                          NULL);
   1536             }
   1537             else if (eError == OMX_ErrorInsufficientResources)
   1538             {
   1539                 pComponentPrivate->eState = OMX_StateWaitForResources;
   1540                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1541                                          OMX_EventError,
   1542                                          OMX_ErrorInsufficientResources,
   1543                                          OMX_TI_ErrorMajor,
   1544                                          NULL);
   1545             }
   1546 #else /* WinCE MM will not use Linux RM, so do this... */
   1547             pComponentPrivate->eState = OMX_StateIdle;
   1548 #ifdef __PERF_INSTRUMENTATION__
   1549             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1550                           PERF_BoundaryComplete | PERF_BoundarySetup);
   1551 #endif
   1552 
   1553             /* Decrement reference count with signal enabled */
   1554             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1555                 return OMX_ErrorUndefined;
   1556             }
   1557 
   1558             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1559                                      OMX_EventCmdComplete,
   1560                                      OMX_CommandStateSet,
   1561                                      OMX_StateIdle,
   1562                                      NULL);
   1563 #endif
   1564             break;
   1565         case OMX_StateExecuting:
   1566         case OMX_StatePause:
   1567             pLcmlHandle = NULL;
   1568 #ifdef __PERF_INSTRUMENTATION__
   1569             PERF_Boundary(pComponentPrivate->pPERF,
   1570                           PERF_BoundaryComplete | PERF_BoundarySteadyState);
   1571 #endif
   1572 
   1573             pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1574             eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1575                                        MMCodecControlStop,
   1576                                        NULL);
   1577         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP3,
   1578                               "Failed to stop socket node (%x).\n", eError);
   1579             pComponentPrivate->bCodecStarted = OMX_FALSE;
   1580         OMX_PRDSP2(pComponentPrivate->dbg, "MMCodecControlStop called...\n");
   1581 
   1582 #ifndef UNDER_CE
   1583             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   1584         while (pComponentPrivate->bDSPStopAck == OMX_FALSE)
   1585         {
   1586                 pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->videoe_mutex_app);
   1587             }
   1588             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   1589 #else
   1590         while (pComponentPrivate->bDSPStopAck == OMX_FALSE)
   1591         {
   1592                 sched_yield();
   1593             }
   1594 #endif
   1595             pComponentPrivate->bDSPStopAck = OMX_FALSE;
   1596 
   1597             for (nCount = 0; nCount < pPortDefIn->nBufferCountActual; nCount++)
   1598             {
   1599                 OMX_PRBUFFER2(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [OWNER = %d]\n",
   1600                               nCount,
   1601                               VIDENC_INPUT_PORT,
   1602                               pCompPortIn->pBufferPrivate[nCount]->pBufferHdr,
   1603                               pCompPortIn->pBufferPrivate[nCount]->eBufferOwner);
   1604 
   1605                 if (pCompPortIn->pBufferPrivate[nCount]->eBufferOwner == VIDENC_BUFFER_WITH_DSP ||
   1606                     pCompPortIn->pBufferPrivate[nCount]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT)
   1607                 {
   1608                     OMX_PRBUFFER1(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [SEND BACK TO SUPPLIER]\n",
   1609                                   nCount,
   1610                                   VIDENC_INPUT_PORT,
   1611                                   pCompPortIn->pBufferPrivate[nCount]->pBufferHdr);
   1612 
   1613                     if (pCompPortIn->hTunnelComponent == NULL)
   1614                     {
   1615 
   1616                         pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->nFilledLen = 0;
   1617                         OMX_PRBUFFER1(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [memset %lu bytes]\n",
   1618                                       nCount,
   1619                                       VIDENC_INPUT_PORT,
   1620                                       pCompPortIn->pBufferPrivate[nCount]->pBufferHdr,
   1621                                       pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->nAllocLen);
   1622 
   1623                         memset(pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->pBuffer,
   1624                                0x0,
   1625                                pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->nAllocLen);
   1626 
   1627                         pCompPortIn->pBufferPrivate[nCount]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   1628 #ifdef __PERF_INSTRUMENTATION__
   1629                         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1630                                           pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->pBuffer,
   1631                                           0,
   1632                                           PERF_ModuleHLMM);
   1633 #endif
   1634                         pComponentPrivate->sCbData.EmptyBufferDone(pComponentPrivate->pHandle,
   1635                                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   1636                                                                    pCompPortIn->pBufferPrivate[nCount]->pBufferHdr);
   1637                         OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->EmptybufferdoneCount);
   1638                         OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   1639                     }
   1640                     else
   1641                     {
   1642                         pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->nFilledLen = 0;
   1643                         pCompPortIn->pBufferPrivate[nCount]->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
   1644 #ifdef __PERF_INSTRUMENTATION__
   1645                         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1646                                           pCompPortIn->pBufferPrivate[nCount]->pBufferHdr->pBuffer,
   1647                                           0,
   1648                                           PERF_ModuleLLMM);
   1649 #endif
   1650                         eError = OMX_FillThisBuffer(pCompPortIn->hTunnelComponent,
   1651                                                     pCompPortIn->pBufferPrivate[nCount]->pBufferHdr);
   1652                     OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRBUFFER4,
   1653                                           "FillThisBuffer failed (%x).\n", eError);
   1654                     }
   1655                 }
   1656             }
   1657 
   1658             for (nCount = 0; nCount < pPortDefOut->nBufferCountActual; nCount++)
   1659             {
   1660                 OMX_PRBUFFER2(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [OWNER = %d]\n",
   1661                               nCount,
   1662                               VIDENC_OUTPUT_PORT,
   1663                               pCompPortOut->pBufferPrivate[nCount]->pBufferHdr,
   1664                               pCompPortOut->pBufferPrivate[nCount]->eBufferOwner);
   1665 
   1666                 if (pCompPortOut->pBufferPrivate[nCount]->eBufferOwner == VIDENC_BUFFER_WITH_DSP ||
   1667                     pCompPortOut->pBufferPrivate[nCount]->eBufferOwner == VIDENC_BUFFER_WITH_COMPONENT)
   1668                 {
   1669 
   1670                     if (pCompPortOut->hTunnelComponent == NULL)
   1671                     {
   1672                         OMX_PRBUFFER1(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [memset %lu bytes]\n",
   1673                                       nCount,
   1674                                       VIDENC_OUTPUT_PORT,
   1675                                       pCompPortOut->pBufferPrivate[nCount]->pBufferHdr,
   1676                                       pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->nAllocLen);
   1677 
   1678                         memset(pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->pBuffer,
   1679                                0x0,
   1680                                pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->nAllocLen);
   1681                     }
   1682 
   1683                     OMX_PRBUFFER1(pComponentPrivate->dbg, "Buffer[%d]:port[%d] -> %p [SEND BACK TO SUPPLIER]\n", nCount,
   1684                                   VIDENC_OUTPUT_PORT,
   1685                                   pCompPortOut->pBufferPrivate[nCount]->pBufferHdr);
   1686 
   1687                     pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->nFilledLen = 0;
   1688                     pCompPortOut->pBufferPrivate[nCount]->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   1689 #ifdef __PERF_INSTRUMENTATION__
   1690                     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   1691                                       pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->pBuffer,
   1692                                       pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->nFilledLen,
   1693                                       PERF_ModuleHLMM);
   1694 #endif
   1695                     /*Propagate pBufferHeader Data*/
   1696                     OMX_CONF_CIRCULAR_BUFFER_MOVE_HEAD(pCompPortOut->pBufferPrivate[nCount]->pBufferHdr,
   1697                                                    pComponentPrivate->sCircularBuffer,
   1698                                                    pComponentPrivate);
   1699                     /* trigger event handler if we are supposed to */
   1700                     if (pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->hMarkTargetComponent == pComponentPrivate->pHandle &&
   1701                         pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->pMarkData)
   1702                     {
   1703                         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1704                                                  OMX_EventMark,
   1705                                                  0x0,
   1706                                                  0x0,
   1707                                                  pCompPortOut->pBufferPrivate[nCount]->pBufferHdr->pMarkData);
   1708                     }
   1709                     pComponentPrivate->sCbData.FillBufferDone(pComponentPrivate->pHandle,
   1710                                                               pComponentPrivate->pHandle->pApplicationPrivate,
   1711                                                               pCompPortOut->pBufferPrivate[nCount]->pBufferHdr);
   1712                     OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->FillbufferdoneCount);
   1713                     OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   1714                 }
   1715             }
   1716 
   1717 #ifdef RESOURCE_MANAGER_ENABLED /* Resource Manager Proxy Calls */
   1718                 if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   1719                 {
   1720                     /* TODO: Disable RM Send for now */
   1721                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H264_Encode_COMPONENT, 0); */
   1722                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1723                                                  RMProxy_StateSet,
   1724                                                  OMX_H264_Encode_COMPONENT,
   1725                                                  OMX_StateIdle,
   1726                                                  3456,
   1727                                                  NULL);
   1728                 }
   1729                 else if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   1730                 {
   1731                     /* TODO: Disable RM Send for now */
   1732                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_MPEG4_Encode_COMPONENT, 0); */
   1733                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1734                                                  RMProxy_StateSet,
   1735                                                  OMX_MPEG4_Encode_COMPONENT,
   1736                                                  OMX_StateIdle,
   1737                                                  3456,
   1738                                                  NULL);
   1739                 }
   1740                 else if (pCompPortOut->pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   1741                 {
   1742                     /* TODO: Disable RM Send for now */
   1743                     /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H263_Encode_COMPONENT, 0); */
   1744             OMX_PRMGR2(pComponentPrivate->dbg, "Setting Idle state from Executing to RMProxy\n");
   1745                     eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1746                                                  RMProxy_StateSet,
   1747                                                  OMX_H263_Encode_COMPONENT,
   1748                                                  OMX_StateIdle,
   1749                                                  3456,
   1750                                                  NULL);
   1751                 }
   1752         if (eError != OMX_ErrorNone)
   1753         {
   1754                     OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1755                                              OMX_EventError,
   1756                                              OMX_ErrorHardware,
   1757                                              OMX_TI_ErrorMajor,
   1758                                              NULL);
   1759                 }
   1760 
   1761 #endif
   1762         OMX_PRBUFFER2(pComponentPrivate->dbg, "Flushing Pipes!\n");
   1763         OMX_VIDENC_EmptyDataPipes (pComponentPrivate);
   1764 
   1765         pComponentPrivate->eState = OMX_StateIdle;
   1766 
   1767         /* Decrement reference count with signal enabled */
   1768         if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1769              return OMX_ErrorUndefined;
   1770         }
   1771 
   1772         OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1773                                  OMX_EventCmdComplete,
   1774                                  OMX_CommandStateSet,
   1775                                  OMX_StateIdle,
   1776                                  NULL);
   1777         break;
   1778         default:
   1779             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1780                                      OMX_EventError,
   1781                                      OMX_ErrorIncorrectStateTransition,
   1782                                      OMX_TI_ErrorMinor,
   1783                                      NULL);
   1784     }
   1785 OMX_CONF_CMD_BAIL:
   1786     return eError;
   1787 }
   1788 
   1789 /*----------------------------------------------------------------------------*/
   1790 /**
   1791   * OMX_VIDENC_HandleCommandStateSet()
   1792   *
   1793   * Called by component thread, handles commands sent by the app.
   1794   *
   1795   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   1796   *
   1797   * @retval OMX_ErrorNone                  success, ready to roll
   1798   *         OMX_ErrorInsufficientResources if the malloc fails
   1799   **/
   1800 /*----------------------------------------------------------------------------*/
   1801 OMX_ERRORTYPE OMX_VIDENC_HandleCommandStateSetExecuting(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   1802 {
   1803     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1804     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   1805     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1806     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1807 
   1808     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   1809 
   1810     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   1811     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   1812 
   1813     switch (pComponentPrivate->eState)
   1814     {
   1815         case OMX_StateExecuting:
   1816             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate, OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor, NULL);
   1817             break;
   1818         case OMX_StateIdle:
   1819             OMX_CONF_CIRCULAR_BUFFER_RESTART(pComponentPrivate->sCircularBuffer);
   1820         case OMX_StatePause:
   1821             if (pComponentPrivate->bCodecStarted == OMX_FALSE)
   1822             {
   1823                 pLcmlHandle = NULL;
   1824                 pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1825                 pLcmlHandle->pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pComponentPrivate;
   1826 
   1827             OMX_PRDSP2(pComponentPrivate->dbg, "Starting the codec...\n");
   1828                 eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1829                                            EMMCodecControlStart,
   1830                                            NULL);
   1831             OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1832                                   "Failed to start socket node (%x).\n", eError);
   1833 
   1834                 pComponentPrivate->bCodecStarted = OMX_TRUE;
   1835             }
   1836 
   1837 #ifdef RESOURCE_MANAGER_ENABLED /* Resource Manager Proxy Calls */
   1838             if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   1839             {
   1840                 /* TODO: Disable RM Send for now */
   1841             OMX_PRMGR2(pComponentPrivate->dbg, "Setting executing state to RMProxy\n");
   1842                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H264_Encode_COMPONENT, 0); */
   1843                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1844                                              RMProxy_StateSet,
   1845                                              OMX_H264_Encode_COMPONENT,
   1846                                              OMX_StateExecuting,
   1847                                              3456,
   1848                                              NULL);
   1849             }
   1850             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   1851             {
   1852                 /* TODO: Disable RM Send for now */
   1853             OMX_PRMGR2(pComponentPrivate->dbg, "Setting executing state to RMProxy\n");
   1854                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_MPEG4_Encode_COMPONENT, 0); */
   1855                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1856                                              RMProxy_StateSet,
   1857                                              OMX_MPEG4_Encode_COMPONENT,
   1858                                              OMX_StateExecuting,
   1859                                              3456,
   1860                                              NULL);
   1861 
   1862             }
   1863             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   1864             {
   1865                 /* TODO: Disable RM Send for now */
   1866                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H263_Encode_COMPONENT, 0); */
   1867             OMX_PRMGR2(pComponentPrivate->dbg, "Setting executing state to RMProxy\n");
   1868                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   1869                                              RMProxy_StateSet,
   1870                                              OMX_H263_Encode_COMPONENT,
   1871                                              OMX_StateExecuting,
   1872                                              3456,
   1873                                              NULL);
   1874             }
   1875             if (eError != OMX_ErrorNone)
   1876             {
   1877                 pComponentPrivate->eState = OMX_StateWaitForResources;
   1878                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1879                                          OMX_EventError,
   1880                                          OMX_ErrorHardware,
   1881                                          OMX_TI_ErrorMajor,
   1882                                          NULL);
   1883             }
   1884 #endif
   1885             pComponentPrivate->eState = OMX_StateExecuting;
   1886 #ifdef __PERF_INSTRUMENTATION__
   1887             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1888                           PERF_BoundaryStart | PERF_BoundarySteadyState);
   1889 #endif
   1890 
   1891             /* Decrement reference count with signal enabled */
   1892             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1893                  return OMX_ErrorUndefined;
   1894             }
   1895 
   1896             /*Send state change notificaiton to Application*/
   1897             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1898                                      OMX_EventCmdComplete,
   1899                                      OMX_CommandStateSet,
   1900                                      OMX_StateExecuting,
   1901                                      NULL);
   1902             break;
   1903         default:
   1904             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1905                                      OMX_EventError,
   1906                                      OMX_ErrorIncorrectStateTransition,
   1907                                      OMX_TI_ErrorMinor,
   1908                                      NULL);
   1909     }
   1910 OMX_CONF_CMD_BAIL:
   1911     return eError;
   1912 }
   1913 
   1914 /*----------------------------------------------------------------------------*/
   1915 /**
   1916   * OMX_VIDENC_HandleCommandStateSetPause()
   1917   *
   1918   * Called by component thread, handles commands sent by the app.
   1919   *
   1920   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   1921   *
   1922   * @retval OMX_ErrorNone                  success, ready to roll
   1923   *         OMX_ErrorInsufficientResources if the malloc fails
   1924   **/
   1925 /*----------------------------------------------------------------------------*/
   1926 OMX_ERRORTYPE OMX_VIDENC_HandleCommandStateSetPause (VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   1927 {
   1928     OMX_ERRORTYPE eError = OMX_ErrorNone;
   1929     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   1930     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   1931     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   1932 
   1933     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   1934 
   1935     pLcmlHandle     = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1936     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   1937     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   1938 
   1939 
   1940     switch (pComponentPrivate->eState)
   1941     {
   1942         case OMX_StatePause:
   1943             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1944                                      OMX_EventError,
   1945                                      OMX_ErrorSameState,
   1946                                      OMX_TI_ErrorMinor,
   1947                                      NULL);
   1948             break;
   1949         case OMX_StateIdle:
   1950         case OMX_StateExecuting:
   1951             pLcmlHandle = NULL;
   1952 #ifdef __PERF_INSTRUMENTATION__
   1953             PERF_Boundary(pComponentPrivate->pPERFcomp,
   1954                           PERF_BoundaryComplete | PERF_BoundarySteadyState);
   1955 #endif
   1956             pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   1957             eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   1958                                        EMMCodecControlPause,
   1959                                    NULL);
   1960         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg, OMX_PRDSP4,
   1961                               "Failed to pause socket node (%x).\n", eError);
   1962 
   1963 
   1964         pComponentPrivate->bCodecStarted = OMX_FALSE;
   1965         OMX_PRDSP2(pComponentPrivate->dbg, "MMCodecControlPaused called...\n");
   1966 
   1967 
   1968 #ifndef UNDER_CE
   1969             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   1970         while (pComponentPrivate->bDSPStopAck == OMX_FALSE)
   1971         {
   1972                 pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->videoe_mutex_app);
   1973             }
   1974             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   1975 #else
   1976         while (pComponentPrivate->bDSPStopAck == OMX_FALSE)
   1977         {
   1978                 sched_yield();
   1979             }
   1980 #endif
   1981 
   1982             pComponentPrivate->bDSPStopAck = OMX_FALSE;
   1983 
   1984             pComponentPrivate->eState = OMX_StatePause;
   1985 
   1986             /* Decrement reference count with signal enabled */
   1987             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   1988                  return OMX_ErrorUndefined;
   1989             }
   1990 
   1991             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1992                                      OMX_EventCmdComplete,
   1993                                      OMX_CommandStateSet,
   1994                                      OMX_StatePause,
   1995                                      NULL);
   1996             break;
   1997         default:
   1998             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   1999                                      OMX_EventError,
   2000                                      OMX_ErrorIncorrectStateTransition,
   2001                                      OMX_TI_ErrorMinor,
   2002                                      NULL);
   2003     }
   2004 
   2005 OMX_CONF_CMD_BAIL:
   2006     return eError;
   2007 }
   2008 
   2009 /*----------------------------------------------------------------------------*/
   2010 /**
   2011   * OMX_VIDENC_HandleCommandStateSetLoaded()
   2012   *
   2013   * Called by component thread, handles commands sent by the app.
   2014   *
   2015   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   2016   *
   2017   * @retval OMX_ErrorNone                  success, ready to roll
   2018   *         OMX_ErrorInsufficientResources if the malloc fails
   2019   **/
   2020 /*----------------------------------------------------------------------------*/
   2021 OMX_ERRORTYPE OMX_VIDENC_HandleCommandStateSetLoaded (VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   2022 {
   2023     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2024     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2025     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   2026     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2027 
   2028     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   2029 
   2030     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   2031     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   2032 
   2033     switch (pComponentPrivate->eState)
   2034     {
   2035         case OMX_StateLoaded:
   2036             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2037                                      OMX_EventError,
   2038                                      OMX_ErrorSameState,
   2039                                      OMX_TI_ErrorMinor,
   2040                                      NULL);
   2041             break;
   2042         case OMX_StateWaitForResources:
   2043         OMX_PRSTATE2(pComponentPrivate->dbg, "Transitioning from WFR to Loaded\n");
   2044 #ifdef RESOURCE_MANAGER_ENABLED
   2045             if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   2046             {
   2047                 /* TODO: Disable RM Send for now */
   2048                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H264_Encode_COMPONENT, 0); */
   2049             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Loaded from WFR\n");
   2050                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2051                                              RMProxy_StateSet,
   2052                                              OMX_H264_Encode_COMPONENT,
   2053                                              OMX_StateLoaded,
   2054                                              3456,
   2055                                              NULL);
   2056             }
   2057             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   2058             {
   2059                 /* TODO: Disable RM Send for now */
   2060                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_MPEG4_Encode_COMPONENT, 0); */
   2061             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Loaded from WFR\n");
   2062                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2063                                              RMProxy_StateSet,
   2064                                              OMX_MPEG4_Encode_COMPONENT,
   2065                                              OMX_StateLoaded,
   2066                                              3456,
   2067                                              NULL);
   2068             }
   2069             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   2070             {
   2071                 /* TODO: Disable RM Send for now */
   2072                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H263_Encode_COMPONENT, 0); */
   2073             OMX_PRMGR2(pComponentPrivate->dbg, "RMProxy_SendCommand: Setting state to Loaded from WFR\n");
   2074                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2075                                              RMProxy_StateSet,
   2076                                              OMX_H263_Encode_COMPONENT,
   2077                                              OMX_StateLoaded,
   2078                                              3456,
   2079                                              NULL);
   2080             }
   2081             if (eError != OMX_ErrorNone)
   2082             {
   2083                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2084                                          OMX_EventError,
   2085                                          OMX_ErrorHardware,
   2086                                          OMX_TI_ErrorMajor,
   2087                                          NULL);
   2088                 break;
   2089             }
   2090 #endif
   2091             pComponentPrivate->eState = OMX_StateLoaded;
   2092             /* Decrement reference count with signal enabled */
   2093             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   2094                 return OMX_ErrorUndefined;
   2095             }
   2096 
   2097             #ifdef __PERF_INSTRUMENTATION__
   2098                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   2099                               PERF_BoundaryComplete | PERF_BoundaryCleanup);
   2100             #endif
   2101             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2102                                      OMX_EventCmdComplete,
   2103                                      OMX_CommandStateSet,
   2104                                      OMX_StateLoaded,
   2105                                      NULL);
   2106             break;
   2107         case OMX_StateIdle:
   2108             OMX_PRSTATE2(pComponentPrivate->dbg, "Transitioning from Idle to Loaded\n");
   2109             pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   2110     #ifdef __PERF_INSTRUMENTATION__
   2111             PERF_Boundary(pComponentPrivate->pPERFcomp,
   2112                           PERF_BoundaryStart | PERF_BoundaryCleanup);
   2113     #endif
   2114             pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   2115             while ( (pPortDefIn->bPopulated) || (pPortDefOut->bPopulated))
   2116             {
   2117     #ifndef UNDER_CE
   2118                     pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->videoe_mutex_app);
   2119     #else
   2120                     OMX_WaitForEvent(&(pComponentPrivate->InIdle_event));
   2121     #endif
   2122             }
   2123             pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   2124 
   2125     #ifdef RESOURCE_MANAGER_ENABLED /* Resource Manager Proxy Calls */
   2126             if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   2127             {
   2128                 /* TODO: Disable RM Send for now */
   2129                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H264_Encode_COMPONENT, 0); */
   2130                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2131                                              RMProxy_FreeResource,
   2132                                              OMX_H264_Encode_COMPONENT,
   2133                                              0,
   2134                                              3456,
   2135                                              NULL);
   2136             }
   2137             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   2138             {
   2139                 /* TODO: Disable RM Send for now */
   2140                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_MPEG4_Encode_COMPONENT, 0); */
   2141                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2142                                              RMProxy_FreeResource,
   2143                                              OMX_MPEG4_Encode_COMPONENT,
   2144                                              0,
   2145                                              3456,
   2146                                              NULL);
   2147             }
   2148             else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   2149             {
   2150                 /* TODO: Disable RM Send for now */
   2151                 /* eError = RMProxy_SendCommand(pHandle, RMProxy_RequestResource, OMX_H263_Encode_COMPONENT, 0); */
   2152                 eError = RMProxy_NewSendCommand(pComponentPrivate->pHandle,
   2153                                              RMProxy_FreeResource,
   2154                                              OMX_H263_Encode_COMPONENT,
   2155                                              0,
   2156                                              3456,
   2157                                              NULL);
   2158             }
   2159             if (eError != OMX_ErrorNone)
   2160             {
   2161                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2162                                          OMX_EventError,
   2163                                          OMX_ErrorHardware,
   2164                                          OMX_TI_ErrorMajor,
   2165                                          NULL);
   2166             }
   2167     #endif
   2168              /* Make sure the DSP node has been deleted */
   2169              if (pComponentPrivate->bCodecStarted == OMX_TRUE || pComponentPrivate->bCodecLoaded == OMX_TRUE)
   2170              {
   2171                  OMX_TRACE2(pComponentPrivate->dbg, "LCML_ControlCodec EMMCodecControlDestroy\n");
   2172                  eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   2173                                              EMMCodecControlDestroy,
   2174                                              NULL);
   2175                  OMX_CONF_BAIL_IF_ERROR(eError);
   2176                  OMX_TRACE2(pComponentPrivate->dbg,"Atempting to Unload LCML");
   2177                  /*Unload LCML */
   2178                  if(pComponentPrivate->pModLcml != NULL)
   2179                  {
   2180                      OMX_TRACE2(pComponentPrivate->dbg,"Unloading LCML");
   2181                      dlclose(pComponentPrivate->pModLcml);
   2182                      pComponentPrivate->pModLcml = NULL;
   2183                      pComponentPrivate->pLCML = NULL;
   2184                  }
   2185 
   2186                  pComponentPrivate->bCodecStarted = OMX_FALSE;
   2187                  pComponentPrivate->bCodecLoaded = OMX_FALSE;
   2188              }
   2189 
   2190              OMX_CONF_BAIL_IF_ERROR(eError);
   2191 
   2192 #ifdef __KHRONOS_CONF__
   2193             pComponentPrivate->bPassingIdleToLoaded = OMX_FALSE;
   2194 #endif
   2195             pComponentPrivate->eState = OMX_StateLoaded;
   2196 
   2197 #ifdef __PERF_INSTRUMENTATION__
   2198                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   2199                               PERF_BoundaryComplete | PERF_BoundaryCleanup);
   2200 #endif
   2201 
   2202             /* Decrement reference count with signal enabled */
   2203             if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
   2204                  return OMX_ErrorUndefined;
   2205             }
   2206 
   2207             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2208                                      OMX_EventCmdComplete,
   2209                                      OMX_CommandStateSet,
   2210                                      OMX_StateLoaded,
   2211                                      NULL);
   2212             break;
   2213         default:
   2214             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   2215                                      OMX_EventError,
   2216                                      OMX_ErrorIncorrectStateTransition,
   2217                                      OMX_TI_ErrorMinor,
   2218                                      NULL);
   2219     }
   2220 OMX_CONF_CMD_BAIL:
   2221     return eError;
   2222 }
   2223 
   2224 
   2225 /*---------------------------------------------------------------------------------------*/
   2226 /**
   2227   * OMX_OMX_VIDENC_Process_FreeOutBuf()
   2228   *
   2229   * Called by component thread, handles free output buffers from app.
   2230   *
   2231   * @param pComponentPrivate private component structure for this instance of the component
   2232   *
   2233   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   2234   *
   2235   * @retval OMX_ErrorNone                  success, ready to roll
   2236   *         OMX_ErrorInsufficientResources if the malloc fails
   2237   **/
   2238 /*---------------------------------------------------------------------------------------*/
   2239 
   2240 OMX_ERRORTYPE OMX_VIDENC_Process_FreeOutBuf(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   2241 {
   2242     int nRet = -1;
   2243     void *pUalgOutParams = NULL;
   2244     VIDENC_NODE* pMemoryListHead = NULL;
   2245     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2246     OMX_BUFFERHEADERTYPE* pBufHead = NULL;
   2247     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2248      VIDENC_BUFFER_PRIVATE* pBufferPrivate = NULL;
   2249     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2250 
   2251     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   2252 
   2253     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   2254     pLcmlHandle = (LCML_DSP_INTERFACE*)(((VIDENC_COMPONENT_PRIVATE*)pComponentPrivate)->pLCML);
   2255     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   2256 
   2257 #ifndef UNDER_CE
   2258     if (pthread_mutex_lock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
   2259     {
   2260         OMX_TRACE4(pComponentPrivate->dbg, "pthread_mutex_lock() failed.\n");
   2261         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2262     }
   2263     nRet = read(pComponentPrivate->nFree_oPipe[0], &pBufHead, sizeof(pBufHead));
   2264     if ((nRet == -1) || !pBufHead || !pBufHead->pOutputPortPrivate)
   2265     {
   2266         pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex));
   2267         OMX_ERROR4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2268         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2269     }
   2270 
   2271     pBufferPrivate = pBufHead->pOutputPortPrivate;
   2272 
   2273     pBufferPrivate->bReadFromPipe = OMX_TRUE;
   2274     if (pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
   2275     {
   2276         OMX_TRACE4(pComponentPrivate->dbg, "pthread_mutex_unlock() failed.\n");
   2277         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2278     }
   2279 #else
   2280     nRet = read(pComponentPrivate->nFree_oPipe[0], &pBufHead, sizeof(pBufHead));
   2281     if ((nRet == -1) || (pBufHead == NULL) || (pBufHead->pOutputPortPrivate == NULL))
   2282     {
   2283         OMX_ERROR4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2284         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2285     }
   2286     if (pBufHead != NULL)
   2287     {
   2288         pBufferPrivate = pBufHead->pOutputPortPrivate;
   2289     }
   2290     pBufferPrivate->bReadFromPipe = OMX_TRUE;
   2291 #endif
   2292 
   2293 #ifdef __PERF_INSTRUMENTATION__
   2294     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2295                       PREF(pBufHead,pBuffer),
   2296                       0,
   2297                       PERF_ModuleCommonLayer);
   2298 #endif
   2299 
   2300 if (pBufferPrivate->eBufferOwner == VIDENC_BUFFER_WITH_DSP ||
   2301         pBufferPrivate->eBufferOwner == VIDENC_BUFFER_WITH_CLIENT)
   2302     {
   2303 goto EXIT;
   2304 
   2305 }
   2306 
   2307     if(!pBufferPrivate || !pLcmlHandle || !pPortDefOut)
   2308         goto EXIT;
   2309 
   2310     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   2311     {
   2312             pUalgOutParams =(H264VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam;
   2313         OMX_PRBUFFER1(pComponentPrivate->dbg, " %p \n", (void*)pBufHead);
   2314             pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_DSP;
   2315             eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   2316                                       EMMCodecOuputBuffer,
   2317                                       pBufHead->pBuffer,
   2318                                       pBufHead->nAllocLen,
   2319                                       0,
   2320                                       (OMX_U8*)pUalgOutParams,
   2321                                       sizeof(H264VE_GPP_SN_UALGOutputParams),
   2322                                       (void*)pBufHead);
   2323             if (eError != OMX_ErrorNone)
   2324         {
   2325             OMX_PRDSP4(pComponentPrivate->dbg, "LCML QueueBuffer failed: %x\n", eError);
   2326             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2327         }
   2328 
   2329     }
   2330     else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   2331              pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   2332     {
   2333         pUalgOutParams = (MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam;
   2334         OMX_PRBUFFER1(pComponentPrivate->dbg, " %p\n", (void*)pBufHead);
   2335         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_DSP;
   2336         eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   2337                                   EMMCodecOuputBuffer,
   2338                                   pBufHead->pBuffer,
   2339                                   pBufHead->nAllocLen,
   2340                                   0,
   2341                                   (OMX_U8*)pUalgOutParams,
   2342                                   sizeof(MP4VE_GPP_SN_UALGOutputParams),
   2343                                   (void*)pBufHead);
   2344         if (eError != OMX_ErrorNone)
   2345         {
   2346             OMX_PRDSP4(pComponentPrivate->dbg, "LCML QueueBuffer failed: %x\n", eError);
   2347             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2348         }
   2349     }
   2350     else
   2351     {
   2352         OMX_PRBUFFER4(pComponentPrivate->dbg, "Unsupported compression format (%d)\n",
   2353                       pPortDefOut->format.video.eCompressionFormat);
   2354         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   2355     }
   2356     EXIT:
   2357 OMX_CONF_CMD_BAIL:
   2358     return eError;
   2359 }
   2360 
   2361 /*---------------------------------------------------------------------------------------*/
   2362 /**
   2363   * OMX_VIDENC_Process_FilledInBuf()
   2364   *
   2365   * Called by component thread, handles filled input buffers from app.
   2366   *
   2367   * @param pComponentPrivate private component structure for this instance of the component
   2368   *
   2369   * @param phandle LCML_DSP_INTERFACE handle for this instance of the component
   2370   *
   2371   * @retval OMX_ErrorNone                  success, ready to roll
   2372   *         OMX_ErrorInsufficientResources if the malloc fails
   2373   **/
   2374 /*---------------------------------------------------------------------------------------*/
   2375 
   2376 OMX_ERRORTYPE OMX_VIDENC_Process_FilledInBuf(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   2377 {
   2378     OMX_U8 i = 0;
   2379     int nRet = -1;
   2380     void* pUalgInpParams = NULL;
   2381     VIDENC_NODE* pMemoryListHead = NULL;
   2382     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2383     OMX_BUFFERHEADERTYPE* pBufHead = NULL;
   2384     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2385     VIDENC_BUFFER_PRIVATE* pBufferPrivate = NULL;
   2386     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   2387     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2388     VIDEOENC_PORT_TYPE* pCompPortOut            = NULL;
   2389 
   2390     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   2391 
   2392     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   2393     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   2394     pCompPortOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
   2395     pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   2396     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   2397 
   2398     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pLcmlHandle, pPortDefIn, 1);
   2399 
   2400 #ifndef UNDER_CE
   2401     if (pthread_mutex_lock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
   2402     {
   2403         OMX_TRACE4(pComponentPrivate->dbg, "pthread_mutex_lock() failed.\n");
   2404         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2405     }
   2406     nRet = read(pComponentPrivate->nFilled_iPipe[0], &(pBufHead), sizeof(pBufHead));
   2407     if ((nRet == -1) || !pBufHead || !pBufHead->pInputPortPrivate)
   2408     {
   2409         pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex));
   2410         OMX_TRACE4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2411         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2412     }
   2413 
   2414     if (pBufHead != NULL)
   2415     {
   2416     pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pInputPortPrivate;
   2417    	}
   2418     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, pBufferPrivate, 1);
   2419     pBufferPrivate->bReadFromPipe = OMX_TRUE;
   2420 
   2421     if (pthread_mutex_unlock(&(pComponentPrivate->mVideoEncodeBufferMutex)) != 0)
   2422     {
   2423         OMX_TRACE4(pComponentPrivate->dbg, "pthread_mutex_unlock() failed.\n");
   2424         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2425     }
   2426 #else
   2427     nRet = read(pComponentPrivate->nFilled_iPipe[0], &(pBufHead), sizeof(pBufHead));
   2428     if (nRet == -1)
   2429     {
   2430         OMX_TRACE4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
   2431         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2432     }
   2433     pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pInputPortPrivate;
   2434     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, pBufferPrivate, 1);
   2435     pBufferPrivate->bReadFromPipe = OMX_TRUE;
   2436 #endif
   2437 
   2438 #ifdef __PERF_INSTRUMENTATION__
   2439     /*For Steady State Instumentation*/
   2440     #if 0
   2441     if ((pComponentPrivate->nLcml_nCntIp == 1))
   2442     {
   2443         PERF_Boundary(pComponentPrivate->pPERFcomp,
   2444                       PERF_BoundaryStart | PERF_BoundarySteadyState);
   2445     }
   2446     #endif
   2447     PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   2448                       PREF(pBufHead,pBuffer),
   2449                       pPortDefIn->nBufferSize,
   2450                       PERF_ModuleCommonLayer);
   2451 #endif
   2452 
   2453     if (pBufferPrivate->eBufferOwner == VIDENC_BUFFER_WITH_DSP ||
   2454         pBufferPrivate->eBufferOwner == VIDENC_BUFFER_WITH_CLIENT)
   2455     {
   2456         goto EXIT;
   2457     }
   2458 
   2459 
   2460 
   2461     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   2462     {
   2463 
   2464         pUalgInpParams = (H264VE_GPP_SN_UALGInputParams*)pBufferPrivate->pUalgParam;
   2465         OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pUalgInpParams, 1, 1);
   2466 
   2467         /*< must be followed for all video encoders*/
   2468         /*  size of this structure             */
   2469         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.size = sizeof(H264VE_GPP_SN_UALGInputParams);
   2470         /*  Input frame height                 */
   2471         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.inputHeight = pPortDefIn->format.video.nFrameHeight;
   2472         /*  Input frame width                  */
   2473         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.inputWidth  = pPortDefIn->format.video.nFrameWidth;
   2474         /*  Reference or input frame rate*1000 */
   2475         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.refFrameRate = (OMX_U32)(Q16Tof(pPortDefIn->format.video.xFramerate)*1000.0);
   2476         /*  Target frame rate * 1000           */
   2477         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.targetFrameRate = pComponentPrivate->nTargetFrameRate;
   2478         /*  Target bit rate in bits per second */
   2479         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.targetBitRate = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig->nEncodeBitrate;
   2480         /*  I frame interval e.g. 30           */
   2481         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.intraFrameInterval = pComponentPrivate->nIntraFrameInterval;
   2482         /*  XDM_ENCODE_AU, XDM_GENERATE_HEADER */
   2483         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.generateHeader = 0;
   2484         /*  DEFAULT(0): use imagewidth as pitch
   2485         *  else use given capture width for
   2486         *  pitch provided it is greater than
   2487         *  image width.*/
   2488         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.captureWidth = 0;
   2489         /*  Force given frame as I or IDR (in H.264)  frame    */
   2490         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.videncDynamicParams.forceIFrame = pComponentPrivate->bForceIFrame;
   2491 
   2492 
   2493 
   2494         /*< initial QP of I frames Range[-1,51]. -1 is for auto initialization.*/
   2495         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.qpIntra = 0x0000001c;
   2496         /*< initial QP of P frames Range[-1,51]. -1 is for auto initialization.*/
   2497         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.qpInter = 0x0000001c;
   2498         /*< Maximum QP to be used  Range[0,51]*/
   2499         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.qpMax = 0x00000033;
   2500         /*< Minimum QP to be used  Range[0,51]*/
   2501         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.qpMin = 0x00000000;
   2502         /*< Controls enable/disable loop filter, See IH264VENC_LoopFilterParams for more details*/
   2503         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.lfDisableIdc = 0x00000000;
   2504         /*< enable/disable Quarter Pel Interpolation*/
   2505         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.quartPelDisable = 0x00000000;
   2506         /*< Adaptive Intra Refesh MB Period: Period at which intra macro blocks should be insterted in a frame*/
   2507         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.airMbPeriod = pComponentPrivate->nAIRRate;
   2508         /*< Maximum number of macro block in a slice <minimum value is 8>*/
   2509         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.maxMBsPerSlice = 0;
   2510         /*< Maximum number of bytes in a slice */
   2511         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.maxBytesPerSlice = 0;
   2512         /*< Row number from which slice needs to be intra coded*/
   2513         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceRefreshRowStartNumber = 0;
   2514         /*< Number of rows to be coded as intra slice*/
   2515         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceRefreshRowNumber = 0;
   2516         /*< alpha offset for loop filter [-12, 12] even number*/
   2517         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.filterOffsetA = 0;
   2518         /*< beta offset for loop filter [-12, 12] even number*/
   2519         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.filterOffsetB = 0 ;
   2520         /*< Limits the maximum frame number in the bit-stream to (1<< (log2MaxFNumMinus4 + 4)) Range[0,12]*/
   2521         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.log2MaxFNumMinus4 = 0;
   2522         /*< Specifies offset to be added to luma QP for addressing QPC values table for chroma components. Valid value is between -12 and 12, (inclusive)*/
   2523         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.chromaQPIndexOffset = 0;
   2524         /*< Controls the intra macroblock coding in P slices [0,1]*/
   2525         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.constrainedIntraPredEnable = 0;
   2526         /*< Picture Order count type Valid values 0, 2*/
   2527         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.picOrderCountType = 0;
   2528         /*< enable/Disable Multiple Motion vector per MB, valid values are [1, 4] [For DM6446, allowed value is only 1]*/
   2529         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.maxMVperMB = pComponentPrivate->maxMVperMB;
   2530         /*< See IH264VENC_Intra4x4Params for more details*/
   2531         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.intra4x4EnableIdc = pComponentPrivate->intra4x4EnableIdc;
   2532         /*< enable/Disable Motion vector access*/
   2533         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.mvDataEnable = 0;
   2534         /*< Enable/Disable Hierarchical P Frame (non-reference P frame) Coding. [Not useful for DM6446]*/
   2535         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.hierCodingEnable = 0; /* should be 1; */
   2536         /*< Signals the type of stream generated with Call-back*/
   2537         if (pComponentPrivate->AVCNALFormat == VIDENC_AVC_NAL_UNIT)
   2538         {
   2539             ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.streamFormat = IH264_BYTE_STREAM;
   2540         }
   2541         else
   2542         {
   2543            ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.streamFormat = IH264_NALU_STREAM;
   2544         }
   2545         /*< Mechanism to do intra Refresh, see IH264VENC_IntraRefreshMethods for valid values*/
   2546         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.intraRefreshMethod = IH264_INTRAREFRESH_NONE;
   2547         /* Enable Perceptual Quantization a.k.a. Perceptual Rate Control*/
   2548         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.perceptualQuant = 0;
   2549         /* Enable Scene Change Detection*/
   2550         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sceneChangeDet = 0;
   2551         /*< Function pointer of the call-back function to be used by Encoder*/
   2552         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.pfNalUnitCallBack = NULL;
   2553 
   2554         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.pContext = NULL;
   2555 
   2556         /*< Following Parameter are related to Arbitrary Slice Ordering (ASO)*/
   2557         /*< Number of valid enteries in asoSliceOrder array valid range is [0,8],
   2558         //!< where 0 and 1 doesn't have any effect*/
   2559         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.numSliceASO = pComponentPrivate->numSliceASO;
   2560         /*!< Array containing the order of slices in which they should
   2561         //!< be present in bit-stream. vaild enteries are [0, any entry lesser than numSlicesASO]*/
   2562         for( i=0; i<MAXNUMSLCGPS;i++)
   2563             ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.asoSliceOrder[i] = pComponentPrivate->asoSliceOrder[i];
   2564 
   2565         /*< Following Parameter are related to Flexible macro block ordering (FMO)*/
   2566         /*< Total Number of slice groups, valid enteries are [0,8]*/
   2567         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.numSliceGroups = pComponentPrivate->numSliceGroups;
   2568         /*< Slice GroupMapType : For Valid enteries see IH264VENC_SliceGroupMapType*/
   2569         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceGroupMapType = pComponentPrivate->sliceGroupMapType;
   2570         /*< Slice Group Change Direction Flag: Only valid when sliceGroupMapType
   2571         //!< is equal to IH264_RASTER_SCAN_SLICE_GRP.
   2572         //!< For valid values refer IH264VENC_SliceGroupChangeDirection*/
   2573         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeDirectionFlag = pComponentPrivate->sliceGroupChangeDirectionFlag;
   2574         /*< Slice Group Change Rate: Only valid when sliceGroupMapType
   2575         //!< is equal to IH264_RASTER_SCAN_SLICE_GRP.
   2576         //!< valid values are : [0, factor of number of Mbs in a row]*/
   2577         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeRate = pComponentPrivate->sliceGroupChangeRate;
   2578         /*< Slice Group Change Cycle: Only valid when sliceGroupMapType
   2579         //!< is equal to IH264_RASTER_SCAN_SLICE_GRP.
   2580         //!< Valid values can be 0 to numMbsRowsInPicture, also constrained
   2581         //!< by sliceGroupChangeRate*sliceGroupChangeCycle < totalMbsInFrame*/
   2582         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeCycle = pComponentPrivate->sliceGroupChangeCycle;
   2583         /*< This field is useful in case of sliceGroupMapType equal to either
   2584         //!< IH264_INTERLEAVED_SLICE_GRP or IH264_FOREGRND_WITH_LEFTOVER_SLICE_GRP
   2585         //!< In both cases it has different meaning:
   2586         //!< In case of IH264_INTERLEAVED_SLICE_GRP:
   2587         //!< The i-th entery in this array is used to specify the number of consecutive
   2588         //!< slice group macroblocks to be assigned to the i-th slice group in
   2589         //!< raster scan order of slice group macroblock units.
   2590         //!< Valid values are 0 to totalMbsInFrame again constrained by sum of all the elements
   2591         //!< shouldn't exceed totalMbsInFrame
   2592         //!< In case of IH264_FOREGRND_WITH_LEFTOVER_SLICE_GRP:
   2593         //!< First entry in the array specify the start position of foreground region in terms
   2594         //!< of macroblock number, valid values are [0, totalMbsInFrame-1]
   2595         //!< Second entry in the array specify the end position of foreground region in terms
   2596         //!< of macroblock number, valid values are [0, totalMbsInFrame-1] with following constrains:
   2597         //!< endPos > startPos && endPos%mbsInOneRow > startPos%mbsInOneRow*/
   2598         for( i=0; i<MAXNUMSLCGPS;i++)
   2599         {
   2600             ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->H264VENC_TI_DYNAMICPARAMS.sliceGroupParams[i] = pComponentPrivate->sliceGroupParams[i];
   2601         }
   2602 
   2603         ((H264VE_GPP_SN_UALGInputParams*)pUalgInpParams)->ulFrameIndex = pComponentPrivate->nFrameCnt;
   2604 
   2605         pComponentPrivate->bForceIFrame = 0;
   2606         ++pComponentPrivate->nFrameCnt;
   2607 
   2608         printH264UAlgInParam(pUalgInpParams, 0, &pComponentPrivate->dbg);
   2609         OMX_CONF_CIRCULAR_BUFFER_MOVE_TAIL(pBufHead,
   2610                                            pComponentPrivate->sCircularBuffer,
   2611                                            pComponentPrivate);
   2612 
   2613        /*Send Buffer to LCML*/
   2614         OMX_PRBUFFER1(pComponentPrivate->dbg, " %p\n", (void*)pBufHead);
   2615         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_DSP;
   2616         eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
   2617                                   EMMCodecInputBuffer,
   2618                                   pBufHead->pBuffer,
   2619                                   pBufHead->nAllocLen,
   2620                                   pBufHead->nFilledLen,
   2621                                   (OMX_U8*)pUalgInpParams,
   2622                                   sizeof(H264VE_GPP_SN_UALGInputParams),
   2623                                   (OMX_U8*)pBufHead);
   2624         if (eError != OMX_ErrorNone)
   2625         {
   2626             OMX_PRDSP4(pComponentPrivate->dbg, "LCML QueueBuffer failed: %x\n", eError);
   2627             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorHardware);
   2628         }
   2629 
   2630     }
   2631     else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   2632              pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   2633     {
   2634         eError = OMX_VIDENC_Queue_Mpeg4_Buffer(pComponentPrivate, pBufHead);
   2635     }
   2636     else
   2637     {
   2638         OMX_PRBUFFER4(pComponentPrivate->dbg, "Unsupported compression format (%d)\n",
   2639                       pPortDefOut->format.video.eCompressionFormat);
   2640         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   2641     }
   2642 OMX_CONF_CMD_BAIL:
   2643 EXIT:
   2644     return eError;
   2645 }
   2646 
   2647 OMX_ERRORTYPE OMX_VIDENC_Queue_Mpeg4_Buffer(VIDENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BUFFERHEADERTYPE* pBufHead)
   2648 {
   2649     MP4VE_GPP_SN_UALGInputParams* pUalgInpParams = NULL;
   2650     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2651     VIDENC_BUFFER_PRIVATE* pBufferPrivate;
   2652     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   2653     VIDEOENC_PORT_TYPE* pCompPortOut = NULL;
   2654     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2655 
   2656     pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pInputPortPrivate;
   2657     pCompPortOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
   2658     pPortDefOut = pCompPortOut->pPortDef;
   2659     pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   2660 
   2661     pUalgInpParams = (MP4VE_GPP_SN_UALGInputParams*)pBufferPrivate->pUalgParam;
   2662     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pUalgInpParams, 1, 1);
   2663 
   2664     pUalgInpParams->ulFrameIndex         = pComponentPrivate->nFrameCnt;
   2665     pUalgInpParams->ulTargetFrameRate    = pComponentPrivate->nTargetFrameRate;
   2666     pUalgInpParams->ulTargetBitRate      = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig->nEncodeBitrate;
   2667     pUalgInpParams->ulGenerateHeader     = 0;
   2668     pUalgInpParams->ulForceIFrame        = pComponentPrivate->bForceIFrame;
   2669     pUalgInpParams->ulResyncInterval     = pCompPortOut->pErrorCorrectionType->nResynchMarkerSpacing;
   2670     if(pCompPortOut->pErrorCorrectionType->bEnableHEC)
   2671         pUalgInpParams->ulHecInterval    = 3;
   2672     else
   2673     pUalgInpParams->ulHecInterval        = 0;
   2674     pUalgInpParams->ulAIRRate            = pCompPortOut->pIntraRefreshType->nAirRef;
   2675     pUalgInpParams->ulMIRRate            = pComponentPrivate->nMIRRate;
   2676     pUalgInpParams->ulfCode              = 5;
   2677     pUalgInpParams->ulHalfPel            = 1;
   2678     pUalgInpParams->ul4MV                = 0;
   2679     pUalgInpParams->ulIntraFrameInterval = pComponentPrivate->nIntraFrameInterval;
   2680 
   2681     /*Set nQPI Value*/
   2682     if (pUalgInpParams->ulForceIFrame == OMX_TRUE)
   2683     {
   2684         pUalgInpParams->ulQPIntra = pComponentPrivate->nQPI;
   2685     }
   2686     else
   2687     {
   2688         pUalgInpParams->ulQPIntra    = 0;
   2689     }
   2690 
   2691     /*Set segment mode params*/
   2692     if (pComponentPrivate->bMVDataEnable)
   2693     {
   2694         pUalgInpParams->ul4MV                 =1;
   2695         pUalgInpParams->uluseUMV              =1;
   2696         pUalgInpParams->ulMVDataEnable        =1;
   2697     }
   2698     else
   2699     {
   2700         pUalgInpParams->ul4MV                 =0;
   2701         pUalgInpParams->uluseUMV              =0;
   2702         pUalgInpParams->ulMVDataEnable        =0;
   2703     }
   2704     if (pComponentPrivate->bResyncDataEnable)
   2705         pUalgInpParams->ulResyncDataEnable    =1;
   2706     else
   2707         pUalgInpParams->ulResyncDataEnable    =0;
   2708     /* Reset bForceMPEG4IFrame to zero */
   2709     pComponentPrivate->bForceIFrame = OMX_FALSE;
   2710 
   2711     /*Set ulACPred Value*/
   2712     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   2713     {
   2714         pUalgInpParams->ulACPred         = pComponentPrivate->pMpeg4->bACPred;
   2715     }
   2716     else
   2717     {
   2718         pUalgInpParams->ulACPred        = 0;
   2719     }
   2720     pUalgInpParams->ulQPInter           = 8;
   2721     pUalgInpParams->ulLastFrame         = 0;
   2722     pUalgInpParams->ulcapturewidth      = 0;
   2723     pUalgInpParams->ulQpMax             = 31;
   2724     pUalgInpParams->ulQpMin             = 2;
   2725     ++pComponentPrivate->nFrameCnt;
   2726 
   2727     if(pComponentPrivate->bRequestVOLHeader == OMX_TRUE)
   2728     {
   2729         /*In the case of Mpeg4 we have to send an extra Buffer to LCML requesting for VOL Header*/
   2730         memcpy(pComponentPrivate->pTempUalgInpParams,pUalgInpParams,sizeof(MP4VE_GPP_SN_UALGInputParams));
   2731         pComponentPrivate->pTempUalgInpParams->ulGenerateHeader = 1;
   2732         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_DSP;
   2733         eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
   2734                                   EMMCodecInputBuffer,
   2735                                   pComponentPrivate->pTempUalgInpParams,/*send any buffer*/
   2736                                   1,
   2737                                   0,
   2738                                   pComponentPrivate->pTempUalgInpParams,
   2739                                   sizeof(MP4VE_GPP_SN_UALGInputParams),
   2740                                   (OMX_U8*)pBufHead);
   2741         if (eError != OMX_ErrorNone)
   2742         {
   2743             OMX_PRDSP2(pComponentPrivate->dbg, "LCML QueueBuffer failed: %x\n", eError);
   2744         }
   2745         pComponentPrivate->bRequestVOLHeader = OMX_FALSE;
   2746     }
   2747 
   2748     OMX_PRDSP1(pComponentPrivate->dbg,
   2749                "TargetFrameRate -> %d\n\
   2750                TargetBitRate   -> %d\n\
   2751                QPI             -> %d\n", pComponentPrivate->nTargetFrameRate,
   2752                (int)pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateTypeConfig->nEncodeBitrate,
   2753                pComponentPrivate->nQPI);
   2754 
   2755     printMpeg4UAlgInParam(pUalgInpParams, 0, &pComponentPrivate->dbg);
   2756     OMX_CONF_CIRCULAR_BUFFER_MOVE_TAIL(pBufHead,
   2757                                            pComponentPrivate->sCircularBuffer,
   2758                                            pComponentPrivate);
   2759 
   2760         /*Send Buffer to LCML*/
   2761         OMX_PRBUFFER1(pComponentPrivate->dbg, " %p\n", (void*)pBufHead);
   2762         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_DSP;
   2763         eError = LCML_QueueBuffer(pLcmlHandle->pCodecinterfacehandle,
   2764                                   EMMCodecInputBuffer,
   2765                                   pBufHead->pBuffer,
   2766                                   pBufHead->nAllocLen,
   2767                                   pBufHead->nFilledLen,
   2768                                   (OMX_U8*)pUalgInpParams,
   2769                                   sizeof(MP4VE_GPP_SN_UALGInputParams),
   2770                                   (OMX_U8*)pBufHead);
   2771     if (eError != OMX_ErrorNone)
   2772     {
   2773         OMX_PRDSP4(pComponentPrivate->dbg, "LCML QueueBuffer failed: %x\n", eError);
   2774     }
   2775 
   2776 OMX_CONF_CMD_BAIL:
   2777 
   2778     return eError;
   2779 }
   2780 
   2781 
   2782 /*---------------------------------------------------------------------------------------*/
   2783 /**
   2784   * OMX_VIDENC_Process_FilledOutBuf()
   2785   *
   2786   * Called by component thread, handles filled output buffers from DSP.
   2787   *
   2788   * @param pComponentPrivate private component structure for this instance of the component
   2789   *
   2790   * @retval OMX_ErrorNone                  success, ready to roll
   2791   *         OMX_ErrorInsufficientResources if the malloc fails
   2792   **/
   2793 /*---------------------------------------------------------------------------------------*/
   2794 
   2795 OMX_ERRORTYPE OMX_VIDENC_Process_FilledOutBuf(VIDENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BUFFERHEADERTYPE* pBufHead)
   2796 {
   2797     OMX_ERRORTYPE eError = OMX_ErrorNone;
   2798     VIDENC_BUFFER_PRIVATE* pBufferPrivate = NULL;
   2799     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   2800     OMX_OTHER_EXTRADATATYPE_1_1_2* pExtraDataType;
   2801     H264VE_GPP_SN_UALGOutputParams* pSNPrivateParams;
   2802     OMX_U8* pTemp;
   2803     OMX_U32* pIndexNal;
   2804 
   2805     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   2806 
   2807     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   2808 
   2809    if (pComponentPrivate->bCodecStarted == OMX_TRUE)
   2810     {
   2811 
   2812         pBufferPrivate = pBufHead->pOutputPortPrivate;
   2813         pSNPrivateParams = (H264VE_GPP_SN_UALGOutputParams*)(pBufferPrivate->pUalgParam);
   2814         pBufHead->nFilledLen = ((H264VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->ulBitstreamSize;
   2815 
   2816         if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   2817         {
   2818             pBufHead->nFlags &= ~OMX_BUFFERFLAG_CODECCONFIG;
   2819 
   2820         /*Copy Buffer Data to be propagated*/
   2821         if((pComponentPrivate->AVCNALFormat == VIDENC_AVC_NAL_SLICE) &&
   2822                 (pSNPrivateParams->ulNALUnitsPerFrame != (pSNPrivateParams->ulNALUnitIndex+1)) &&
   2823                 (pSNPrivateParams->ulNALUnitsPerFrame != 0))
   2824         {
   2825 
   2826             pBufHead->pMarkData = NULL;
   2827             pBufHead->hMarkTargetComponent = NULL;
   2828             pBufHead->nTickCount = pComponentPrivate->sCircularBuffer.pHead->nTickCount;
   2829             pBufHead->nTimeStamp = pComponentPrivate->sCircularBuffer.pHead->nTimeStamp;
   2830             pBufHead->nFlags = 0;
   2831         }
   2832             else
   2833             {
   2834                 OMX_CONF_CIRCULAR_BUFFER_MOVE_HEAD(pBufHead, pComponentPrivate->sCircularBuffer,
   2835                                                    pComponentPrivate);
   2836             pBufHead->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
   2837         }
   2838 
   2839 
   2840             /* Set lFrameType*/
   2841             if (((H264VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->lFrameType == OMX_LFRAMETYPE_H264 ||
   2842                  ((H264VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->lFrameType == OMX_LFRAMETYPE_IDR_H264)
   2843             {
   2844                 /* IDR Frame */
   2845                 OMX_S32 nalType = pBufHead->pBuffer[0] & 0x1F;
   2846                 if (nalType == SPS_CODE_PREFIX || nalType == PPS_CODE_PREFIX) {
   2847                     /* Need to drop subsequent SPS or PPS NAL unit since opencore does not
   2848                      * correctly handle storage */
   2849                     if (!pComponentPrivate->bSentFirstSpsPps) {
   2850                         if (nalType == SPS_CODE_PREFIX) {
   2851                             // Save SPS and send it along with PPS later in a single buffer
   2852                             // Workaround to send a 0-length buffer first.
   2853                             // Ideally, we should not send a buffer at all.
   2854                             pComponentPrivate->sps = malloc(4 + pBufHead->nFilledLen);
   2855                             pComponentPrivate->spsLen = 4 + pBufHead->nFilledLen;
   2856                             memcpy(pComponentPrivate->sps, "\x00\x00\x00\x01", 4);
   2857                             memcpy(pComponentPrivate->sps + 4, pBufHead->pBuffer, pBufHead->nFilledLen);
   2858                             pBufHead->nFilledLen = 0;
   2859                         }
   2860 
   2861                         /* we can assume here that PPS always comes second */
   2862                         if (nalType == PPS_CODE_PREFIX) {
   2863                             pComponentPrivate->bSentFirstSpsPps = OMX_TRUE;
   2864                             if (pComponentPrivate->sps == NULL ||
   2865                                 pComponentPrivate->spsLen == 0) {
   2866                                 OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   2867                             }
   2868                             memmove(pBufHead->pBuffer + pComponentPrivate->spsLen + 4,
   2869                                     pBufHead->pBuffer, pBufHead->nFilledLen);
   2870                             memmove(pBufHead->pBuffer,
   2871                                     pComponentPrivate->sps, pComponentPrivate->spsLen);
   2872                             memcpy(pBufHead->pBuffer + pComponentPrivate->spsLen, "\x00\x00\x00\x01", 4);
   2873                             pBufHead->nFilledLen += pComponentPrivate->spsLen + 4;
   2874                             pBufHead->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
   2875                             free(pComponentPrivate->sps);
   2876                             pComponentPrivate->sps = NULL;
   2877                             pComponentPrivate->spsLen = 0;
   2878                         }
   2879                     } else {
   2880                         pBufHead->nFilledLen = 0;
   2881                     }
   2882                 }
   2883 
   2884                 pBufHead->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
   2885             }
   2886 
   2887             /* if NAL frame mode */
   2888             if (pComponentPrivate->AVCNALFormat == VIDENC_AVC_NAL_FRAME)
   2889             {
   2890 
   2891                 /*H264VE_GPP_SN_UALGOutputParams* pSNPrivateParams;*/
   2892                 int nNalSlices;
   2893 
   2894                 pBufHead->nFlags |= OMX_BUFFERFLAG_EXTRADATA;
   2895 
   2896                 pTemp = pBufHead->pBuffer + pBufHead->nOffset + pBufHead->nFilledLen + 3;
   2897                 pExtraDataType = (OMX_OTHER_EXTRADATATYPE_1_1_2*) (((OMX_U32) pTemp) & ~3);
   2898                 pIndexNal = (OMX_U32*)(pExtraDataType->data);
   2899                 /*pSNPrivateParams = (H264VE_GPP_SN_UALGOutputParams*)(pBufferPrivate->pUalgParam);*/
   2900 
   2901                 pExtraDataType->nVersion.s.nVersionMajor = 1;
   2902                 pExtraDataType->nVersion.s.nVersionMinor = 1;
   2903                 pExtraDataType->nVersion.s.nRevision = 2;
   2904                 pExtraDataType->nPortIndex = VIDENC_OUTPUT_PORT;
   2905                 pExtraDataType->eType = OMX_ExtraDataQuantization;
   2906                 pExtraDataType->nDataSize = (1+pSNPrivateParams->ulNALUnitsPerFrame)*sizeof(OMX_U32);
   2907 
   2908                 *pIndexNal = pSNPrivateParams->ulNALUnitsPerFrame;
   2909                 pIndexNal++;
   2910                 for (nNalSlices = 0; (OMX_U32)nNalSlices < pSNPrivateParams->ulNALUnitsPerFrame; nNalSlices++, pIndexNal++)
   2911                 {
   2912 
   2913                     *pIndexNal = (OMX_U32)(pSNPrivateParams->ulNALUnitsSizes[nNalSlices]);
   2914                 }
   2915 
   2916                 pTemp = (OMX_U8*)((((OMX_U32)pIndexNal)+3) & ~3);
   2917                 pExtraDataType->nSize = (OMX_U32)pTemp-(OMX_U32)pExtraDataType;
   2918 
   2919                 pExtraDataType = (OMX_OTHER_EXTRADATATYPE_1_1_2*) pTemp;
   2920                 pExtraDataType->nSize = (sizeof(OMX_OTHER_EXTRADATATYPE_1_1_2)+3) & ~3;
   2921                 pExtraDataType->nVersion.s.nVersionMajor = 1;
   2922                 pExtraDataType->nVersion.s.nVersionMinor = 1;
   2923                 pExtraDataType->nVersion.s.nRevision = 2;
   2924                 pExtraDataType->nPortIndex = VIDENC_OUTPUT_PORT;
   2925                 pExtraDataType->eType = OMX_ExtraDataNone;
   2926                 pExtraDataType->nDataSize = 0;
   2927             }
   2928         }
   2929         else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   2930                  pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   2931         {
   2932             pBufHead->nFlags &= ~OMX_BUFFERFLAG_CODECCONFIG;
   2933 
   2934             /*We ignore the first Mpeg4 buffer which contains VOL Header since we did not add it to the circular list*/
   2935             if(pComponentPrivate->bWaitingForVOLHeaderBuffer == OMX_FALSE)
   2936             {
   2937                 OMX_CONF_CIRCULAR_BUFFER_MOVE_HEAD(pBufHead, pComponentPrivate->sCircularBuffer,
   2938                                                    pComponentPrivate);
   2939             }
   2940             else
   2941             {
   2942                 pComponentPrivate->bWaitingForVOLHeaderBuffer = OMX_FALSE;
   2943                 pBufHead->nFlags |= OMX_BUFFERFLAG_CODECCONFIG;
   2944             }
   2945 
   2946             pBufHead->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
   2947 
   2948             /* Set cFrameType*/
   2949             if (((MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->cFrameType == OMX_CFRAMETYPE_MPEG4)
   2950             {
   2951                 /* I-VOP Frame */
   2952                 pBufHead->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
   2953             }
   2954             VIDENC_MPEG4_SEGMENTMODE_METADATA* pMetaData;
   2955             /*copy MPEG4 segment mode meta data */
   2956             pMetaData=(VIDENC_MPEG4_SEGMENTMODE_METADATA*)pBufferPrivate->pMetaData;
   2957             if (pComponentPrivate->bMVDataEnable==OMX_TRUE)
   2958             {
   2959                pMetaData->mvDataSize=((MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->mvDataSize;
   2960                pMetaData->pMVData=((MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->MVData;
   2961             }
   2962             if (pComponentPrivate->bResyncDataEnable==OMX_TRUE)
   2963             {
   2964                pMetaData->pResyncData=((MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->ResyncData;
   2965                pMetaData->numPackets=((MP4VE_GPP_SN_UALGOutputParams*)pBufferPrivate->pUalgParam)->numPackets;
   2966             }
   2967         }
   2968         else
   2969         {
   2970             OMX_PRBUFFER4(pComponentPrivate->dbg, "Unsupported compression format (%d)\n",
   2971                           pPortDefOut->format.video.eCompressionFormat);
   2972             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   2973         }
   2974 
   2975         if (pBufHead->nFlags & OMX_BUFFERFLAG_EOS)
   2976         {
   2977             /* trigger event handler */
   2978             OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,  OMX_EventBufferFlag, 0x1, pBufHead->nFlags, NULL);
   2979             /* clear flag */
   2980             pComponentPrivate->nFlags = 0;
   2981         }
   2982 
   2983         if (pBufHead->pMarkData != NULL)
   2984         {
   2985             /* trigger event handler if we are supposed to */
   2986             if (pBufHead->hMarkTargetComponent == pComponentPrivate->pHandle &&
   2987                 pBufHead->pMarkData)
   2988             {
   2989                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,  OMX_EventMark, 0x0, 0x0,
   2990                                         pBufHead->pMarkData);
   2991             }
   2992         }
   2993         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   2994 #ifdef __PERF_INSTRUMENTATION__
   2995         PERF_SendingBuffer(pComponentPrivate->pPERFcomp,
   2996                            pBufHead->pBuffer,
   2997                            pBufHead->nFilledLen,
   2998                            PERF_ModuleHLMM);
   2999 #endif
   3000         pComponentPrivate->sCbData.FillBufferDone(pComponentPrivate->pHandle,
   3001                                                   pComponentPrivate->pHandle->pApplicationPrivate,
   3002                                                   pBufHead);
   3003         OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->FillbufferdoneCount);
   3004         OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   3005     }
   3006 OMX_CONF_CMD_BAIL:
   3007     return eError;
   3008 }
   3009 
   3010 /*---------------------------------------------------------------------------------------*/
   3011 /**
   3012   * OMX_VIDENC_Process_FreeInBuf()
   3013   *
   3014   * Called by component thread, handles free input buffers from DSP.
   3015   *
   3016   * @param pComponentPrivate private component structure for this instance of the component
   3017   *
   3018   * @retval OMX_ErrorNone                  success, ready to roll
   3019   *         OMX_ErrorInsufficientResources if the malloc fails
   3020   **/
   3021 /*---------------------------------------------------------------------------------------*/
   3022 
   3023 OMX_ERRORTYPE OMX_VIDENC_Process_FreeInBuf(VIDENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BUFFERHEADERTYPE* pBufHead)
   3024 {
   3025     VIDENC_NODE* pMemoryListHead = NULL;
   3026     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3027     OMX_HANDLETYPE hTunnelComponent = NULL;
   3028     VIDENC_BUFFER_PRIVATE* pBufferPrivate = NULL;
   3029 
   3030     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   3031 
   3032     hTunnelComponent = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->hTunnelComponent;
   3033     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   3034 
   3035     /*pBufHead is checked for NULL*/
   3036     OMX_DBG_CHECK_CMD(pComponentPrivate->dbg, pBufHead, 1, 1);
   3037     pBufferPrivate = pBufHead->pInputPortPrivate;
   3038 
   3039     if (hTunnelComponent != NULL)
   3040     {
   3041         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_TUNNELEDCOMP;
   3042 #ifdef __PERF_INSTRUMENTATION__
   3043         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   3044                           PREF(pBufHead,pBuffer),
   3045                           pBufHead->nFilledLen,
   3046                           PERF_ModuleLLMM);
   3047 #endif
   3048 
   3049         eError = OMX_FillThisBuffer(hTunnelComponent, pBufHead);
   3050         OMX_DBG_BAIL_IF_ERROR(eError, pComponentPrivate->dbg,
   3051                               OMX_PRBUFFER4, "FillThisBuffer failed (%x)", eError);
   3052     }
   3053     else
   3054     {
   3055         pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_CLIENT;
   3056 #ifdef __PERF_INSTRUMENTATION__
   3057         PERF_SendingFrame(pComponentPrivate->pPERFcomp,
   3058                           PREF(pBufHead,pBuffer),
   3059                           0,
   3060                           PERF_ModuleHLMM);
   3061 #endif
   3062         pComponentPrivate->sCbData.EmptyBufferDone(pComponentPrivate->pHandle,
   3063                                                    pComponentPrivate->pHandle->pApplicationPrivate,
   3064                                                    pBufHead);
   3065         OMX_VIDENC_IncrementBufferCountByOne(&pComponentPrivate->EmptybufferdoneCount);
   3066         OMX_VIDENC_SignalIfAllBuffersAreReturned(pComponentPrivate);
   3067    }
   3068 
   3069 OMX_CONF_CMD_BAIL:
   3070     return eError;
   3071 }
   3072 /*---------------------------------------------------------------------------------------*/
   3073 /**
   3074   *  Function to initialize LCML
   3075   *
   3076   *
   3077   *
   3078   * @retval OMX_NoError              Success, ready to roll
   3079   *
   3080   **/
   3081 /*---------------------------------------------------------------------------------------*/
   3082 
   3083 OMX_ERRORTYPE OMX_VIDENC_InitLCML(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   3084 {
   3085     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3086     OMX_HANDLETYPE hLCML = NULL;
   3087     VIDENC_NODE* pMemoryListHead = NULL;
   3088 #ifdef UNDER_CE
   3089     typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
   3090     LPFNDLLFUNC1 fpGetHandle1;
   3091 #else
   3092     void* pMyLCML = NULL;
   3093     fpo   fpGetHandle = NULL;
   3094     char* error = NULL;
   3095 #endif
   3096 
   3097     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   3098 
   3099 #ifndef UNDER_CE
   3100     pMyLCML = dlopen("libLCML.so", RTLD_LAZY);
   3101     pComponentPrivate->pModLcml = pMyLCML;
   3102     if (!pMyLCML)
   3103     {
   3104         OMX_ERROR5(pComponentPrivate->dbg, "Could not open LCML library\n");
   3105         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3106     }
   3107 
   3108     fpGetHandle = dlsym(pMyLCML, "GetHandle");
   3109     if ((error = dlerror()) != NULL)
   3110     {
   3111         OMX_ERROR4(pComponentPrivate->dbg, "No GetHandle in LCML library\n");
   3112         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3113     }
   3114 
   3115     eError = (*fpGetHandle)(&hLCML);
   3116     if (eError != OMX_ErrorNone)
   3117     {
   3118         OMX_ERROR5(pComponentPrivate->dbg, "Error While Getting LCML Handle (%x)...\n", eError);
   3119        OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3120     }
   3121 
   3122     pComponentPrivate->pLCML = (LCML_DSP_INTERFACE*)hLCML;
   3123     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   3124     pComponentPrivate->pLCML->pComponentPrivate = (VIDENC_COMPONENT_PRIVATE *)pComponentPrivate;
   3125 
   3126 #else
   3127     g_hLcmlDllHandle = LoadLibraryEx(TEXT("oaf_bml.dll"), NULL, 0);
   3128     if (g_hLcmlDllHandle == NULL)
   3129     {
   3130         OMX_ERROR5(pComponentPrivate->dbg, "BML Load Failed!!!, %d\n", GetLastError());
   3131         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3132     }
   3133     fpGetHandle1 = (LPFNDLLFUNC1)GetProcAddress(g_hLcmlDllHandle,TEXT("GetHandle"));
   3134     if (!fpGetHandle1)
   3135     {
   3136         FreeLibrary(g_hLcmlDllHandle);
   3137         g_hLcmlDllHandle = NULL;
   3138         OMX_ERROR4(pComponentPrivate->dbg, "No GetHandle in BML\n");
   3139         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3140     }
   3141     eError = fpGetHandle1(&hLCML);
   3142     if (eError != OMX_ErrorNone)
   3143     {
   3144         FreeLibrary(g_hLcmlDllHandle);
   3145         g_hLcmlDllHandle = NULL;
   3146         OMX_ERROR5(pComponentPrivate->dbg, "Error While Getting LCML Handle (%x)...\n", eError);
   3147         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   3148     }
   3149     (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML = (LCML_DSP_INTERFACE*)hLCML;
   3150     pComponentPrivate->pLCML->pComponentPrivate = (VIDENC_COMPONENT_PRIVATE *)pComponentPrivate;
   3151 #endif
   3152 OMX_CONF_CMD_BAIL:
   3153     return eError;
   3154 }
   3155 
   3156 /*---------------------------------------------------------------------------------------*/
   3157 /**
   3158   *  Function to fill DSP structures via LCML
   3159   *
   3160   *
   3161   *
   3162   * @retval OMX_NoError              Success, ready to roll
   3163   *
   3164   **/
   3165 /*---------------------------------------------------------------------------------------*/
   3166 
   3167 OMX_ERRORTYPE OMX_VIDENC_InitDSP_H264Enc(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   3168 {
   3169     OMX_U16 nArr[100];
   3170     OMX_U32* pTmp = NULL;
   3171     LCML_CALLBACKTYPE sCb;
   3172     LCML_DSP* pLcmlDSP = NULL;
   3173     VIDENC_NODE* pMemoryListHead = NULL;
   3174     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3175     OMX_VIDEO_PARAM_AVCTYPE* pH264 = NULL;
   3176     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   3177     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   3178     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   3179     OMX_VIDEO_PARAM_BITRATETYPE* pVidParamBitrate = NULL;
   3180     OMX_VIDEO_PARAM_QUANTIZATIONTYPE* pQuantization = NULL;
   3181     H264VE_GPP_SN_Obj_CreatePhase* pCreatePhaseArgs = NULL;
   3182     /* OMX_VIDEO_CONFIG_AVCINTRAPERIOD* pH264IntraPeriod = NULL; */
   3183     OMX_VIDEO_PARAM_MOTIONVECTORTYPE* pMotionVector = NULL;
   3184     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   3185 
   3186     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   3187     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   3188     pH264 = pComponentPrivate->pH264;
   3189     pVidParamBitrate = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateType;
   3190     pQuantization = pComponentPrivate->pQuantization;
   3191     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   3192     /* pH264IntraPeriod = pComponentPrivate->pH264IntraPeriod; */
   3193     pMotionVector = pComponentPrivate->pMotionVector;
   3194     pComponentPrivate->bErrorLcmlHandle = OMX_FALSE;
   3195 
   3196     pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   3197     pLcmlDSP = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
   3198     pLcmlDSP = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
   3199 
   3200     pLcmlDSP->In_BufInfo.nBuffers           = pPortDefIn->nBufferCountActual;
   3201     pLcmlDSP->In_BufInfo.nSize              = pComponentPrivate->nInBufferSize;
   3202     pLcmlDSP->In_BufInfo.DataTrMethod       = DMM_METHOD;
   3203 
   3204     pLcmlDSP->Out_BufInfo.nBuffers          = pPortDefOut->nBufferCountActual;
   3205     pLcmlDSP->Out_BufInfo.nSize             = pComponentPrivate->nOutBufferSize;
   3206     pLcmlDSP->Out_BufInfo.DataTrMethod      = DMM_METHOD;
   3207 
   3208     pLcmlDSP->NodeInfo.nNumOfDLLs           = OMX_H264ENC_NUM_DLLS;
   3209     pLcmlDSP->NodeInfo.AllUUIDs[0].uuid     = &H264VESOCKET_TI_UUID;
   3210     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[0].DllName,H264_ENC_NODE_DLL);
   3211     pLcmlDSP->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
   3212 
   3213     pLcmlDSP->NodeInfo.AllUUIDs[1].uuid     = &H264VESOCKET_TI_UUID;
   3214     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[1].DllName,H264_ENC_NODE_DLL);
   3215     pLcmlDSP->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
   3216 
   3217     pLcmlDSP->NodeInfo.AllUUIDs[2].uuid     = &USN_UUID;
   3218     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[2].DllName,USN_DLL);
   3219     pLcmlDSP->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
   3220 
   3221     pLcmlDSP->SegID                         = 0;
   3222     pLcmlDSP->Timeout                       = -1;
   3223     pLcmlDSP->Alignment                     = 0;
   3224     pLcmlDSP->Priority                      = 5;
   3225 
   3226     #ifdef GPP_PRIVATE_NODE_HEAP
   3227         if ((pPortDefIn->format.video.nFrameWidth <= 176) &&
   3228             (pPortDefIn->format.video.nFrameHeight <= 144))
   3229         {
   3230             pLcmlDSP->ProfileID = 0;
   3231         }
   3232         else if ((pPortDefIn->format.video.nFrameWidth <= 352) &&
   3233                  (pPortDefIn->format.video.nFrameHeight <= 288))
   3234         {
   3235             pLcmlDSP->ProfileID = 1;
   3236         }
   3237         else
   3238         {
   3239             pLcmlDSP->ProfileID = 2;
   3240         }
   3241     #else
   3242         pLcmlDSP->ProfileID = 0xff; /* Use DSP node heap */
   3243     #endif
   3244 
   3245     /* pLcmlDSP->buffindx                      = 999; */
   3246 
   3247     VIDENC_MALLOC(pCreatePhaseArgs,
   3248                   sizeof(H264VE_GPP_SN_Obj_CreatePhase),
   3249                   H264VE_GPP_SN_Obj_CreatePhase,
   3250                   pMemoryListHead,
   3251                   pComponentPrivate->dbg);
   3252 
   3253     pCreatePhaseArgs->usNumStreams            = 2;
   3254     pCreatePhaseArgs->usStreamId              = VIDENC_INPUT_PORT;
   3255     pCreatePhaseArgs->usBuffTypeInStream      = 0;
   3256     pCreatePhaseArgs->usMaxBuffsInStream      = (OMX_U16)pPortDefIn->nBufferCountActual;
   3257     pCreatePhaseArgs->usStreamId2             = VIDENC_OUTPUT_PORT;
   3258     pCreatePhaseArgs->usBuffTypeInStream2     = 0;
   3259     pCreatePhaseArgs->usMaxBuffsInStream2     = (OMX_U16)pPortDefOut->nBufferCountActual;
   3260 
   3261     pCreatePhaseArgs->ulWidth                 = pPortDefIn->format.video.nFrameWidth;
   3262     pCreatePhaseArgs->ulHeight                = pPortDefIn->format.video.nFrameHeight;
   3263     pCreatePhaseArgs->ulTargetBitRate         = pPortDefOut->format.video.nBitrate;
   3264     pCreatePhaseArgs->ulBitstreamBuffSize     = pComponentPrivate->nOutBufferSize;
   3265     pCreatePhaseArgs->ulFrameRate             = (unsigned int)(Q16Tof(pPortDefIn->format.video.xFramerate)*1000.0);
   3266 
   3267     /* set run-time frame and bit rates to create-time values */
   3268     pComponentPrivate->nTargetFrameRate       = pCreatePhaseArgs->ulFrameRate;
   3269     pComponentPrivate->nPrevTargetFrameRate   = 0;
   3270     pComponentPrivate->bSentFirstSpsPps       = OMX_FALSE;
   3271 
   3272     if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar)
   3273     {
   3274         pCreatePhaseArgs->ucYUVFormat         = 0;
   3275     }
   3276     else if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatCbYCrY) /*422 LE UYVY*/
   3277     {
   3278         pCreatePhaseArgs->ucYUVFormat         = 2;
   3279     }
   3280     else if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatYCbYCr) /*422 BE YUYV */
   3281     {
   3282         pCreatePhaseArgs->ucYUVFormat         = 1;
   3283     }
   3284     else
   3285     {
   3286         OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported YUV format.\n");
   3287         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3288     }
   3289 
   3290     pCreatePhaseArgs->ucUnrestrictedMV        = pComponentPrivate->ucUnrestrictedMV;
   3291     pCreatePhaseArgs->ucNumRefFrames          = 1;
   3292 
   3293     if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateVariable)
   3294     {
   3295         pCreatePhaseArgs->ucRateControlAlgorithm  = 0;
   3296     }
   3297     else if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateConstant)
   3298     {
   3299         pCreatePhaseArgs->ucRateControlAlgorithm  = 1;
   3300     }
   3301     else if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateDisable)
   3302     {
   3303         pCreatePhaseArgs->ucRateControlAlgorithm  = 2;
   3304     }
   3305     else
   3306     {
   3307         OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported rate control algorithm.\n");
   3308         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3309     }
   3310 
   3311     pCreatePhaseArgs->ucIDREnable             = 1;
   3312 
   3313     if (pComponentPrivate->bDeblockFilter == OMX_FALSE)
   3314     {
   3315         pCreatePhaseArgs->ucDeblockingEnable  = 0;
   3316     }
   3317     else if (pComponentPrivate->bDeblockFilter == OMX_TRUE)
   3318     {
   3319         pCreatePhaseArgs->ucDeblockingEnable  = 1;
   3320     }
   3321     else
   3322     {
   3323         OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported deblocking setting.\n");
   3324         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter);
   3325     }
   3326 
   3327     pCreatePhaseArgs->ucMVRange               = (pMotionVector->sXSearchRange > pMotionVector->sYSearchRange ? pMotionVector->sXSearchRange : pMotionVector->sYSearchRange);
   3328     pCreatePhaseArgs->ucQPIFrame              = 28;
   3329     pCreatePhaseArgs->ucProfile               = 66;
   3330     pCreatePhaseArgs->ulIntraFramePeriod      = pCreatePhaseArgs->ulFrameRate > 15000 ? 29 : 14;
   3331 
   3332     if (pH264->eLevel == OMX_VIDEO_AVCLevel1b)
   3333     {
   3334         pCreatePhaseArgs->ucLevel = 9;
   3335     }
   3336     else if (pH264->eLevel == OMX_VIDEO_AVCLevel1)
   3337     {
   3338         pCreatePhaseArgs->ucLevel = 10;
   3339     }
   3340     else if (pH264->eLevel == OMX_VIDEO_AVCLevel11)
   3341     {
   3342         pCreatePhaseArgs->ucLevel = 11;
   3343     }
   3344     else if (pH264->eLevel == OMX_VIDEO_AVCLevel12)
   3345     {
   3346         pCreatePhaseArgs->ucLevel = 12;
   3347     }
   3348     else if (pH264->eLevel == OMX_VIDEO_AVCLevel13)
   3349     {
   3350         pCreatePhaseArgs->ucLevel = 13;
   3351     }
   3352     else if (pH264->eLevel == OMX_VIDEO_AVCLevel2)
   3353     {
   3354         pCreatePhaseArgs->ucLevel = 20;
   3355     }
   3356     else if (pH264->eLevel == OMX_VIDEO_AVCLevel21)
   3357     {
   3358         pCreatePhaseArgs->ucLevel = 21;
   3359     }
   3360     else if (pH264->eLevel == OMX_VIDEO_AVCLevel22)
   3361     {
   3362         pCreatePhaseArgs->ucLevel = 22;
   3363     }
   3364     else if (pH264->eLevel == OMX_VIDEO_AVCLevel3)
   3365     {
   3366         pCreatePhaseArgs->ucLevel = 30;
   3367         if ((pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar) &&
   3368             (pPortDefIn->format.video.nFrameWidth == 320) &&
   3369             (pPortDefIn->format.video.nFrameHeight == 240))
   3370         {
   3371             pCreatePhaseArgs->ucQPIFrame = 0;
   3372         }
   3373     }
   3374     else
   3375     {
   3376         OMX_DBG_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting,
   3377                                pComponentPrivate->dbg, OMX_PRDSP2,
   3378                                "Unsupported level.\n");
   3379     }
   3380 
   3381     /* override parameters for VGA & D1 encoding */
   3382     if ((pPortDefIn->format.video.nFrameWidth >= 640 ||
   3383         pPortDefIn->format.video.nFrameHeight >= 480) &&
   3384         pCreatePhaseArgs->ulFrameRate > 15000)
   3385     {
   3386         pComponentPrivate->maxMVperMB = 1;
   3387         pComponentPrivate->intra4x4EnableIdc = INTRA4x4_ISLICES;
   3388         pComponentPrivate->nIntraFrameInterval = 30;
   3389         pComponentPrivate->nAIRRate = 0;
   3390         /* Encoding preset = 4 enables DSP side optimizations for high resolutions */
   3391         pComponentPrivate->nEncodingPreset = 4;
   3392         pCreatePhaseArgs->ulIntraFramePeriod = 0;
   3393         /* Constant bit rate control enabled */
   3394         pCreatePhaseArgs->ucRateControlAlgorithm = 1;
   3395         pCreatePhaseArgs->ucLevel = 30;
   3396     }
   3397     /* Ensure frame rate update interval, which forces IDR frames, is same as I-Slice interval */
   3398     pComponentPrivate->nFrameRateUpdateInterval = pComponentPrivate->nIntraFrameInterval;
   3399     pCreatePhaseArgs->usNalCallback = pComponentPrivate->AVCNALFormat;
   3400     pCreatePhaseArgs->ulEncodingPreset = pComponentPrivate->nEncodingPreset;
   3401     pCreatePhaseArgs->ulRcAlgo = 0;
   3402     pCreatePhaseArgs->endArgs = END_OF_CR_PHASE_ARGS;
   3403     printH264CreateParams(pCreatePhaseArgs, &pComponentPrivate->dbg);
   3404 
   3405     pTmp = memcpy (nArr, pCreatePhaseArgs, sizeof(H264VE_GPP_SN_Obj_CreatePhase));
   3406     if (pTmp == NULL)
   3407     {
   3408         OMX_TRACE4(pComponentPrivate->dbg, "memcpy() out of memory error.\n");
   3409         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
   3410     }
   3411     pLcmlDSP->pCrPhArgs = nArr;
   3412     sCb.LCML_Callback = (void *)OMX_VIDENC_LCML_Callback;
   3413 
   3414     eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   3415                               NULL,
   3416                               &pLcmlHandle,
   3417                               NULL,
   3418                               &sCb);
   3419     if (eError != OMX_ErrorNone)
   3420     {
   3421         OMX_PRDSP4(pComponentPrivate->dbg, "LCML_InitMMCodec Failed!...\n");
   3422         /*TODO: Validate eError from LCML_InitMMCodec for ResourceExhaustionTest */
   3423         pComponentPrivate->bErrorLcmlHandle = OMX_TRUE;
   3424         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
   3425     }
   3426     pComponentPrivate->bCodecLoaded = OMX_TRUE;
   3427     VIDENC_FREE(pCreatePhaseArgs, pMemoryListHead,
   3428                 pComponentPrivate->dbg);
   3429 OMX_CONF_CMD_BAIL:
   3430     return eError;
   3431 }
   3432 
   3433 /*---------------------------------------------------------------------------------------*/
   3434 /**
   3435   *  Function to fill DSP structures via LCML
   3436   *
   3437   *
   3438   *
   3439   * @retval OMX_NoError              Success, ready to roll
   3440   *
   3441   **/
   3442 /*---------------------------------------------------------------------------------------*/
   3443 
   3444 OMX_ERRORTYPE OMX_VIDENC_InitDSP_Mpeg4Enc(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   3445 {
   3446     OMX_U16 nArr[100];
   3447     OMX_U32* pTmp = NULL;
   3448     LCML_CALLBACKTYPE sCb;
   3449     LCML_DSP* pLcmlDSP = NULL;
   3450     VIDENC_NODE* pMemoryListHead = NULL;
   3451     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3452     LCML_DSP_INTERFACE* pLcmlHandle = NULL;
   3453     OMX_VIDEO_PARAM_H263TYPE* pH263 = NULL;
   3454     OMX_VIDEO_PARAM_MPEG4TYPE* pMpeg4 = NULL;
   3455     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
   3456     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   3457     MP4VE_GPP_SN_Obj_CreatePhase* pCreatePhaseArgs = NULL;
   3458     OMX_VIDEO_PARAM_BITRATETYPE* pVidParamBitrate = NULL;
   3459     OMX_VIDEO_PARAM_QUANTIZATIONTYPE* pQuantization = NULL;
   3460     VIDEOENC_PORT_TYPE* pCompPortOut            = NULL;
   3461 
   3462     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   3463 
   3464     pPortDefIn = pComponentPrivate->pCompPort[VIDENC_INPUT_PORT]->pPortDef;
   3465     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   3466     pCompPortOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT];
   3467     pVidParamBitrate = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pBitRateType;
   3468     pQuantization = pComponentPrivate->pQuantization;
   3469     pH263 = pComponentPrivate->pH263;
   3470     pMpeg4 = pComponentPrivate->pMpeg4;
   3471     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   3472 
   3473     pComponentPrivate->bErrorLcmlHandle = OMX_FALSE;
   3474 
   3475     pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
   3476     pLcmlDSP    = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
   3477 
   3478     pLcmlDSP->In_BufInfo.nBuffers           = pPortDefIn->nBufferCountActual;
   3479     pLcmlDSP->In_BufInfo.nSize              = pComponentPrivate->nInBufferSize;
   3480     pLcmlDSP->In_BufInfo.DataTrMethod       = DMM_METHOD;
   3481 
   3482     pLcmlDSP->Out_BufInfo.nBuffers          = pPortDefOut->nBufferCountActual;
   3483     pLcmlDSP->Out_BufInfo.nSize             = pComponentPrivate->nOutBufferSize;
   3484     pLcmlDSP->Out_BufInfo.DataTrMethod      = DMM_METHOD;
   3485 
   3486     pLcmlDSP->NodeInfo.nNumOfDLLs           = OMX_MP4ENC_NUM_DLLS;
   3487     pLcmlDSP->NodeInfo.AllUUIDs[0].uuid     = &MP4VESOCKET_TI_UUID;
   3488     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[0].DllName,MP4_ENC_NODE_DLL);
   3489     pLcmlDSP->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
   3490 
   3491     pLcmlDSP->NodeInfo.AllUUIDs[1].uuid     = &MP4VESOCKET_TI_UUID;
   3492     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[1].DllName,MP4_ENC_NODE_DLL);
   3493     pLcmlDSP->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
   3494 
   3495     pLcmlDSP->NodeInfo.AllUUIDs[2].uuid     = &USN_UUID;
   3496     strcpy ((char *)pLcmlDSP->NodeInfo.AllUUIDs[2].DllName,USN_DLL);
   3497     pLcmlDSP->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
   3498 
   3499     pLcmlDSP->SegID     = 0;
   3500     pLcmlDSP->Timeout   = -1;
   3501     pLcmlDSP->Alignment = 0;
   3502     pLcmlDSP->Priority  = 5;
   3503 
   3504     #ifdef GPP_PRIVATE_NODE_HEAP
   3505         if ((pPortDefIn->format.video.nFrameWidth <= 176) &&
   3506             (pPortDefIn->format.video.nFrameHeight <= 144))
   3507         {
   3508             pLcmlDSP->ProfileID = 0;
   3509         }
   3510         else if ((pPortDefIn->format.video.nFrameWidth <= 352) &&
   3511                  (pPortDefIn->format.video.nFrameHeight <= 288))
   3512         {
   3513             pLcmlDSP->ProfileID = 1;
   3514         }
   3515         else if ((pPortDefIn->format.video.nFrameWidth <= 640) &&
   3516                  (pPortDefIn->format.video.nFrameHeight <= 480))
   3517         {
   3518             pLcmlDSP->ProfileID = 2;
   3519         }
   3520         else if ((pPortDefIn->format.video.nFrameWidth <= 720) &&
   3521                  (pPortDefIn->format.video.nFrameHeight <= 480))
   3522         {
   3523             pLcmlDSP->ProfileID = 3;
   3524         }
   3525         else if ((pPortDefIn->format.video.nFrameWidth <= 720) &&
   3526                  (pPortDefIn->format.video.nFrameHeight <= 576))
   3527         {
   3528             pLcmlDSP->ProfileID = 4;
   3529         }
   3530         else
   3531         {
   3532             pLcmlDSP->ProfileID = 4;
   3533         }
   3534     #else
   3535         pLcmlDSP->ProfileID = 0xff; /* Use DSP node heap */
   3536     #endif
   3537 
   3538     /* pLcmlDSP->buffindx = 999; */
   3539 
   3540     VIDENC_MALLOC(pCreatePhaseArgs,
   3541                   sizeof(MP4VE_GPP_SN_Obj_CreatePhase),
   3542                   MP4VE_GPP_SN_Obj_CreatePhase,
   3543                   pMemoryListHead,
   3544                   pComponentPrivate->dbg);
   3545 
   3546     pCreatePhaseArgs->ucUnrestrictedMV        = pComponentPrivate->ucUnrestrictedMV;
   3547     pCreatePhaseArgs->ucProfile               = 1;
   3548 
   3549     pCreatePhaseArgs->usNumStreams            = 2;
   3550     pCreatePhaseArgs->usStreamId              = 0;
   3551     pCreatePhaseArgs->usBuffTypeInStream      = 0;
   3552     pCreatePhaseArgs->usMaxBuffsInStream      = (OMX_U16)pPortDefIn->nBufferCountActual;
   3553     pCreatePhaseArgs->usStreamId2             = 1;
   3554     pCreatePhaseArgs->usBuffTypeInStream2     = 0;
   3555     pCreatePhaseArgs->usMaxBuffsInStream2     = (OMX_U16)pPortDefOut->nBufferCountActual;
   3556 
   3557     pCreatePhaseArgs->ulWidth                 = pPortDefIn->format.video.nFrameWidth;
   3558     pCreatePhaseArgs->ulHeight                = pPortDefIn->format.video.nFrameHeight;
   3559     pCreatePhaseArgs->ulTargetBitRate         = pPortDefOut->format.video.nBitrate;
   3560     pCreatePhaseArgs->ulVBVSize               = pComponentPrivate->nVBVSize;
   3561 
   3562     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   3563     {
   3564         pCreatePhaseArgs->ulGOBHeadersInterval    = pH263->nGOBHeaderInterval;
   3565     }
   3566     else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   3567     {
   3568         pCreatePhaseArgs->ulGOBHeadersInterval    = 0;
   3569     }
   3570 
   3571     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingH263)
   3572     {
   3573         pCreatePhaseArgs->ucIsMPEG4 = 0;
   3574     }
   3575     else if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   3576     {
   3577         pCreatePhaseArgs->ucIsMPEG4 = 1;
   3578         /*Initialize variables for the generation of VOL Header*/
   3579         pComponentPrivate->bRequestVOLHeader = OMX_TRUE;
   3580         pComponentPrivate->bWaitingForVOLHeaderBuffer = OMX_TRUE;
   3581         pComponentPrivate->bWaitingVOLHeaderCallback = OMX_TRUE;
   3582     }
   3583     else
   3584     {
   3585         OMX_PRDSP4(pComponentPrivate->dbg, "Unsupported video format (%d).\n",
   3586                    pPortDefOut->format.video.eCompressionFormat);
   3587         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3588     }
   3589 
   3590     if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar)
   3591     {
   3592         pCreatePhaseArgs->ucYUVFormat         = 0;
   3593     }
   3594     else if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatCbYCrY) /*422 LE UYVY*/
   3595     {
   3596         pCreatePhaseArgs->ucYUVFormat         = 2;
   3597     }
   3598     else if (pPortDefIn->format.video.eColorFormat == OMX_COLOR_FormatYCbYCr) /*422 BE YUYV */
   3599     {
   3600         pCreatePhaseArgs->ucYUVFormat         = 1;
   3601     }
   3602     else
   3603     {
   3604             OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported YUV format.\n");
   3605         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3606     }
   3607     if(pCompPortOut->pErrorCorrectionType->bEnableHEC)
   3608         pCreatePhaseArgs->ucHEC                   = 1;
   3609     else
   3610         pCreatePhaseArgs->ucHEC                   = 0;/**/
   3611 
   3612     if(pCompPortOut->pErrorCorrectionType->bEnableResync)
   3613         pCreatePhaseArgs->ucResyncMarker          = 1;
   3614     else
   3615         pCreatePhaseArgs->ucResyncMarker          = 0;/**/
   3616 
   3617     if(pCompPortOut->pErrorCorrectionType->bEnableDataPartitioning)
   3618         pCreatePhaseArgs->ucDataPartitioning      = 1;
   3619     else
   3620         pCreatePhaseArgs->ucDataPartitioning      = 0;/**/
   3621 
   3622     if(pCompPortOut->pErrorCorrectionType->bEnableRVLC)
   3623         pCreatePhaseArgs->ucReversibleVLC      = 1;
   3624     else
   3625         pCreatePhaseArgs->ucReversibleVLC         = 0;/**/
   3626 
   3627     pCreatePhaseArgs->ucFrameRate             = (OMX_U8) Q16Tof(pPortDefIn->format.video.xFramerate);
   3628 
   3629    /* set run-time frame and bit rates to create-time values */
   3630     pComponentPrivate->nTargetFrameRate       = pCreatePhaseArgs->ucFrameRate;
   3631     pComponentPrivate->nPrevTargetFrameRate   = 0;
   3632     pComponentPrivate->nTargetBitRate         = pCreatePhaseArgs->ulTargetBitRate;
   3633 
   3634      if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateConstant)
   3635     {
   3636         pCreatePhaseArgs->ucRateControlAlgorithm  = IVIDEO_LOW_DELAY;
   3637     }
   3638     else if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateVariable)
   3639     {
   3640         pCreatePhaseArgs->ucRateControlAlgorithm  = IVIDEO_STORAGE;
   3641     }
   3642     else if (pVidParamBitrate->eControlRate == OMX_Video_ControlRateDisable)
   3643     {
   3644         pCreatePhaseArgs->ucRateControlAlgorithm  = IVIDEO_NONE;
   3645     }
   3646     else
   3647     {
   3648         OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported rate control algorithm.\n");
   3649     OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3650     }
   3651 
   3652     pCreatePhaseArgs->ucQPFirstIFrame         = (OMX_U8)pQuantization->nQpI;
   3653 
   3654     if (pCreatePhaseArgs->ucIsMPEG4 == 1)
   3655     {
   3656 #ifdef __KHRONOS_CONF_1_1__
   3657         if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level0)
   3658         {
   3659             pCreatePhaseArgs->ucLevel = 0;
   3660         }
   3661         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level1)
   3662         {
   3663             pCreatePhaseArgs->ucLevel = 1;
   3664         }
   3665         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level2)
   3666         {
   3667             pCreatePhaseArgs->ucLevel = 2;
   3668         }
   3669         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level3)
   3670         {
   3671             pCreatePhaseArgs->ucLevel = 3;
   3672         }
   3673         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level4a ||
   3674                  pMpeg4->eLevel == OMX_VIDEO_MPEG4Level4)
   3675         {
   3676             pCreatePhaseArgs->ucLevel = 4;
   3677         }
   3678         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level5)
   3679         {
   3680             pCreatePhaseArgs->ucLevel = 5;
   3681         }
   3682         else if (pMpeg4->eLevel == OMX_VIDEO_MPEG4Level0b)
   3683         {
   3684             pCreatePhaseArgs->ucLevel = 100;
   3685         }
   3686         else
   3687         {
   3688                 OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported level.\n");
   3689             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3690         }
   3691 #else
   3692         pCreatePhaseArgs->ucLevel = pMpeg4->eLevel;
   3693 #endif
   3694         pCreatePhaseArgs->enableH263AnnexI  = 0;
   3695         pCreatePhaseArgs->enableH263AnnexJ  = 0;
   3696         pCreatePhaseArgs->enableH263AnnexT  = 0;
   3697 
   3698     }
   3699     else
   3700     {
   3701         if (pH263->eLevel == OMX_VIDEO_H263Level10)
   3702         {
   3703             pCreatePhaseArgs->ucLevel = 10;
   3704         }
   3705         else if (pH263->eLevel == OMX_VIDEO_H263Level20)
   3706         {
   3707             pCreatePhaseArgs->ucLevel = 20;
   3708         }
   3709         else if (pH263->eLevel == OMX_VIDEO_H263Level30)
   3710         {
   3711             pCreatePhaseArgs->ucLevel = 30;
   3712         }
   3713         else if (pH263->eLevel == OMX_VIDEO_H263Level40)
   3714         {
   3715             pCreatePhaseArgs->ucLevel = 40;
   3716         }
   3717         else if (pH263->eLevel == OMX_VIDEO_H263Level45)
   3718         {
   3719             pCreatePhaseArgs->ucLevel = 45;
   3720         }
   3721         else if (pH263->eLevel == OMX_VIDEO_H263Level50)
   3722         {
   3723             pCreatePhaseArgs->ucLevel = 50;
   3724         }
   3725         else if (pH263->eLevel == OMX_VIDEO_H263Level60)
   3726         {
   3727             pCreatePhaseArgs->ucLevel = 60;
   3728         }
   3729         else if (pH263->eLevel == OMX_VIDEO_H263Level70)
   3730         {
   3731             pCreatePhaseArgs->ucLevel = 70;
   3732         }
   3733         else
   3734         {
   3735             OMX_PRDSP2(pComponentPrivate->dbg, "Unsupported level.\n");
   3736             OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUnsupportedSetting);
   3737         }
   3738 
   3739         pCreatePhaseArgs->enableH263AnnexI  = 0;
   3740         pCreatePhaseArgs->enableH263AnnexJ  = 0;
   3741         pCreatePhaseArgs->enableH263AnnexT  = 0;
   3742     }
   3743     pCreatePhaseArgs->ulMaxDelay              = 300;
   3744     #ifndef MODE_3410
   3745     pCreatePhaseArgs->ulVbvParamEnable        = 0;
   3746     pCreatePhaseArgs->ulH263SliceMode         = 0;
   3747     #endif
   3748     pCreatePhaseArgs->ulUseGOV                = 0;
   3749     if (pPortDefOut->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   3750         pCreatePhaseArgs->ulUseVOS            = 1;//needed to generate VOL Header
   3751     else
   3752         pCreatePhaseArgs->ulUseVOS            = 0;
   3753     pCreatePhaseArgs->endArgs                 = END_OF_CR_PHASE_ARGS;
   3754     pTmp = memcpy(nArr, pCreatePhaseArgs, sizeof(MP4VE_GPP_SN_Obj_CreatePhase));
   3755     if (pTmp == NULL)
   3756     {
   3757         OMX_TRACE4(pComponentPrivate->dbg, "memcpy() out of memory error.\n");
   3758         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
   3759     }
   3760 
   3761     pLcmlDSP->pCrPhArgs = nArr;
   3762     printMpeg4Params(pCreatePhaseArgs, &pComponentPrivate->dbg);
   3763 
   3764     sCb.LCML_Callback = (void *)OMX_VIDENC_LCML_Callback;
   3765     eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
   3766                               NULL,
   3767                               &pLcmlHandle,
   3768                               NULL,
   3769                               &sCb);
   3770 
   3771     if (eError != OMX_ErrorNone)
   3772     {
   3773         OMX_PRDSP4(pComponentPrivate->dbg, "LCML_InitMMCodec Failed!...\n");
   3774         /*TODO: Validate eError from LCML_InitMMCodec for ResourceExhaustionTest */
   3775         pComponentPrivate->bErrorLcmlHandle = OMX_TRUE;
   3776         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorInsufficientResources);
   3777     }
   3778     pComponentPrivate->bCodecLoaded = OMX_TRUE;
   3779     VIDENC_FREE(pCreatePhaseArgs, pMemoryListHead,
   3780                 pComponentPrivate->dbg);
   3781 
   3782 OMX_CONF_CMD_BAIL:
   3783     return eError;
   3784 }
   3785 /*----------------------------------------------------------------------------*/
   3786 /**
   3787   *  OMX_VIDENC_Allocate_DSPResources()
   3788   *
   3789   *
   3790   *
   3791   *
   3792   * @param
   3793   * @param
   3794   * @param
   3795   *
   3796   * @retval OMX_NoError              Success, ready to roll
   3797   *         OMX_Error_BadParameter   The input parameter pointer is null
   3798   **/
   3799 /*----------------------------------------------------------------------------*/
   3800 
   3801 OMX_ERRORTYPE OMX_VIDENC_Allocate_DSPResources(VIDENC_COMPONENT_PRIVATE* pComponentPrivate,
   3802                                                OMX_IN OMX_U32 nPortIndex)
   3803 {
   3804     char* pTemp = NULL;
   3805     OMX_U32 nBufferCnt = -1;
   3806     VIDENC_NODE* pMemoryListHead = NULL;
   3807     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3808     VIDEOENC_PORT_TYPE* pCompPort = NULL;
   3809     OMX_VIDEO_CODINGTYPE eCompressionFormat = -1;
   3810     OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
   3811 
   3812     OMX_CONF_CHECK_CMD(pComponentPrivate, 1, 1);
   3813 
   3814     pMemoryListHead = pComponentPrivate->pMemoryListHead;
   3815     pCompPort = pComponentPrivate->pCompPort[nPortIndex];
   3816     nBufferCnt = pComponentPrivate->pCompPort[nPortIndex]->nBufferCnt;
   3817     pPortDefOut = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   3818     eCompressionFormat = pPortDefOut->format.video.eCompressionFormat;
   3819 
   3820     if (nPortIndex == VIDENC_INPUT_PORT)
   3821     {
   3822         if (eCompressionFormat == OMX_VIDEO_CodingAVC)
   3823         {
   3824             H264VE_GPP_SN_UALGInputParams* pUalgParam;
   3825 
   3826             VIDENC_MALLOC(pUalgParam,
   3827                           sizeof(H264VE_GPP_SN_UALGInputParams) + 256,
   3828                           H264VE_GPP_SN_UALGInputParams,
   3829                           pMemoryListHead,
   3830                           pComponentPrivate->dbg);
   3831 
   3832             pTemp = (char*)pUalgParam;
   3833             pTemp += 128;
   3834             pUalgParam = (H264VE_GPP_SN_UALGInputParams*)pTemp;
   3835             pCompPort->pBufferPrivate[nBufferCnt]->pUalgParam = pUalgParam;
   3836         }
   3837         else if (eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   3838                  eCompressionFormat == OMX_VIDEO_CodingH263)
   3839         {
   3840             MP4VE_GPP_SN_UALGInputParams* pUalgParam;
   3841 
   3842             VIDENC_MALLOC(pUalgParam,
   3843                           sizeof(MP4VE_GPP_SN_UALGInputParams) + 256,
   3844                           MP4VE_GPP_SN_UALGInputParams,
   3845                           pMemoryListHead,
   3846                           pComponentPrivate->dbg);
   3847             pTemp = (char*)pUalgParam;
   3848             pTemp += 128;
   3849             pUalgParam = (MP4VE_GPP_SN_UALGInputParams*)pTemp;
   3850             pCompPort->pBufferPrivate[nBufferCnt]->pUalgParam = pUalgParam;
   3851             if(eCompressionFormat == OMX_VIDEO_CodingMPEG4)
   3852             {/*Structure needed to send the request for VOLHeader to SN*/
   3853                 VIDENC_MALLOC(pComponentPrivate->pTempUalgInpParams,
   3854                               sizeof(MP4VE_GPP_SN_UALGInputParams) + 256,
   3855                               MP4VE_GPP_SN_UALGInputParams,
   3856                               pMemoryListHead,
   3857                               pComponentPrivate->dbg);
   3858                 pTemp = (char*)pComponentPrivate->pTempUalgInpParams;
   3859                 pTemp += 128;
   3860                 pComponentPrivate->pTempUalgInpParams = (MP4VE_GPP_SN_UALGInputParams*)pTemp;
   3861             }
   3862         }
   3863     }
   3864     else if (nPortIndex == VIDENC_OUTPUT_PORT)
   3865     {
   3866         if (eCompressionFormat == OMX_VIDEO_CodingAVC)
   3867         {
   3868             H264VE_GPP_SN_UALGOutputParams* pUalgParam;
   3869 
   3870             VIDENC_MALLOC(pUalgParam,
   3871                           sizeof(H264VE_GPP_SN_UALGOutputParams) + 256,
   3872                           H264VE_GPP_SN_UALGOutputParams,
   3873                           pMemoryListHead,
   3874                           pComponentPrivate->dbg);
   3875             pTemp = (char*)pUalgParam;
   3876             pTemp += 128;
   3877             pUalgParam = (H264VE_GPP_SN_UALGOutputParams*)pTemp;
   3878             pCompPort->pBufferPrivate[nBufferCnt]->pUalgParam = pUalgParam;
   3879         }
   3880         else if (eCompressionFormat == OMX_VIDEO_CodingMPEG4 ||
   3881                  eCompressionFormat == OMX_VIDEO_CodingH263)
   3882         {
   3883             MP4VE_GPP_SN_UALGOutputParams* pUalgParam;
   3884 
   3885             VIDENC_MALLOC(pUalgParam,
   3886                           sizeof(MP4VE_GPP_SN_UALGOutputParams) + 256,
   3887                           MP4VE_GPP_SN_UALGOutputParams,
   3888                           pMemoryListHead,
   3889                           pComponentPrivate->dbg);
   3890             pTemp = (char*)pUalgParam;
   3891             pTemp += 128;
   3892             pUalgParam = (MP4VE_GPP_SN_UALGOutputParams*)pTemp;
   3893             pCompPort->pBufferPrivate[nBufferCnt]->pUalgParam = pUalgParam;
   3894         }
   3895     }
   3896 OMX_CONF_CMD_BAIL:
   3897     return eError;
   3898 }
   3899 
   3900 /*---------------------------------------------------------------------------------------*/
   3901 /**
   3902   *  Callback() function will be called LCML component to write the msg
   3903   *
   3904   * @param msgBuffer                 This buffer will be returned by the LCML
   3905   *
   3906   * @retval OMX_NoError              Success, ready to roll
   3907   *         OMX_Error_BadParameter   The input parameter pointer is null
   3908  **/
   3909 /*---------------------------------------------------------------------------------------*/
   3910 OMX_ERRORTYPE OMX_VIDENC_LCML_Callback(TUsnCodecEvent event,void* argsCb [10])
   3911 {
   3912     int nRet = -1;
   3913     OMX_COMPONENTTYPE* pHandle = NULL;
   3914     OMX_ERRORTYPE eError = OMX_ErrorNone;
   3915     OMX_BUFFERHEADERTYPE* pBufHead = NULL;
   3916     VIDENC_BUFFER_PRIVATE* pBufferPrivate = NULL;
   3917     LCML_DSP_INTERFACE* pLcmlDspInterface = NULL;
   3918     TUsnCodecEvent eEvent = (TUsnCodecEvent)event;
   3919     VIDENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
   3920 
   3921     OMX_CONF_CHECK_CMD(argsCb, 1, 1);
   3922 
   3923     if (argsCb[6])
   3924     {
   3925         pLcmlDspInterface = (LCML_DSP_INTERFACE*)argsCb[6];
   3926         pComponentPrivate = (VIDENC_COMPONENT_PRIVATE*)pLcmlDspInterface->pComponentPrivate;
   3927         pHandle = (OMX_COMPONENTTYPE *)pComponentPrivate->pHandle;
   3928     }
   3929     else
   3930     {
   3931         OMXDBG_PRINT(stderr, DSP, 5, 0, "No LCML handle\n");
   3932         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorBadParameter);
   3933     }
   3934 
   3935 
   3936     if (eEvent == EMMCodecBufferProcessed)
   3937     {
   3938         if (((OMX_S32)argsCb[0]) == EMMCodecOuputBuffer)
   3939         {
   3940             pBufHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
   3941             pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pOutputPortPrivate;
   3942 #ifdef __PERF_INSTRUMENTATION__
   3943             PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
   3944                                PREF(pBufHead,pBuffer),
   3945                                PREF(pBufHead,nFilledLen),
   3946                                PERF_ModuleCommonLayer);
   3947 
   3948             pComponentPrivate->nLcml_nCntOpReceived++;
   3949 
   3950             if ((pComponentPrivate->nLcml_nCntIp >= 1) &&
   3951                 (pComponentPrivate->nLcml_nCntOpReceived == 1))
   3952             {
   3953                 PERF_Boundary(pComponentPrivate->pPERFcomp,
   3954                               PERF_BoundaryStart | PERF_BoundarySteadyState);
   3955             }
   3956 #endif
   3957             OMX_PRDSP1(pComponentPrivate->dbg, " [OUT] -> %p\n", pBufHead);
   3958             if(pBufHead->nFilledLen > pBufHead->nAllocLen) {
   3959                 ALOGD("VE Warning!!! Output buffer overflow.");
   3960             }
   3961             pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
   3962             if (pComponentPrivate->bCodecStarted == OMX_TRUE)
   3963             {
   3964                 OMX_PRDSP1(pComponentPrivate->dbg, "Enters OMX_VIDENC_Process_FilledOutBuf\n");
   3965               eError = OMX_VIDENC_Process_FilledOutBuf(pComponentPrivate, pBufHead);
   3966               if (eError != OMX_ErrorNone)
   3967               {
   3968                 OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   3969                            OMX_EventError,
   3970                            OMX_ErrorUndefined,
   3971                            OMX_TI_ErrorCritical,
   3972                            NULL);
   3973                 OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
   3974               }
   3975             }
   3976         }
   3977         if ((int)argsCb [0] == EMMCodecInputBuffer)
   3978         {
   3979             pBufHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
   3980             pBufferPrivate = (VIDENC_BUFFER_PRIVATE*)pBufHead->pInputPortPrivate;
   3981 
   3982 #ifdef __PERF_INSTRUMENTATION__
   3983             PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
   3984                                PREF(pBufHead,pBuffer),
   3985                                0,
   3986                                PERF_ModuleCommonLayer);
   3987 #endif
   3988 
   3989             OMX_PRDSP1(pComponentPrivate->dbg, " [IN] -> %p\n", pBufHead);
   3990             pBufferPrivate->eBufferOwner = VIDENC_BUFFER_WITH_COMPONENT;
   3991             /*we should ignore the callback asociated to the VOL Header request*/
   3992             if (pComponentPrivate->bCodecStarted == OMX_TRUE && pComponentPrivate->bWaitingVOLHeaderCallback == OMX_FALSE)
   3993             {
   3994                 OMX_PRDSP1(pComponentPrivate->dbg, "Enters OMX_VIDENC_Process_FreeInBuf\n");
   3995                eError = OMX_VIDENC_Process_FreeInBuf(pComponentPrivate, pBufHead);
   3996                if (eError != OMX_ErrorNone)
   3997                {
   3998                  OMX_VIDENC_EVENT_HANDLER(pComponentPrivate,
   3999                             OMX_EventError,
   4000                             OMX_ErrorUndefined,
   4001                             OMX_TI_ErrorCritical,
   4002                             NULL);
   4003                  OMX_VIDENC_BAIL_IF_ERROR(eError, pComponentPrivate);
   4004                }
   4005                 OMX_PRDSP1(pComponentPrivate->dbg, "Exits OMX_VIDENC_Process_FreeInBuf\n");
   4006             }
   4007             else if(pComponentPrivate->bWaitingVOLHeaderCallback == OMX_TRUE)
   4008             {
   4009                 pComponentPrivate->bWaitingVOLHeaderCallback = OMX_FALSE;
   4010             }
   4011         }
   4012     }
   4013 
   4014     if(eEvent == EMMCodecProcessingPaused ||
   4015        eEvent == EMMCodecProcessingStoped)
   4016     {
   4017         if (pComponentPrivate != NULL)
   4018         {
   4019             pComponentPrivate->bDSPStopAck = OMX_TRUE;
   4020             #ifndef UNDER_CE
   4021                 pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   4022                 pthread_cond_signal(&pComponentPrivate->stop_cond);
   4023                 pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   4024             #endif
   4025         }
   4026     }
   4027 
   4028     if(eEvent == EMMCodecStrmCtrlAck)
   4029     {
   4030         if ((int)argsCb [0] == USN_ERR_NONE)
   4031         {
   4032             pComponentPrivate->bFlushComplete = OMX_TRUE;
   4033             #ifndef UNDER_CE
   4034                 pthread_mutex_lock(&pComponentPrivate->videoe_mutex_app);
   4035                 pthread_cond_signal(&pComponentPrivate->flush_cond);
   4036                 pthread_mutex_unlock(&pComponentPrivate->videoe_mutex_app);
   4037             #endif
   4038         }
   4039     }
   4040    nRet = OMX_VIDENC_HandleLcmlEvent(pComponentPrivate, eEvent, argsCb);
   4041     if (nRet == -1)
   4042     {
   4043         OMX_ERROR4(pComponentPrivate->dbg, "LCML Event Handler failed.\n");
   4044         OMX_CONF_SET_ERROR_BAIL(eError, OMX_ErrorUndefined);
   4045     }
   4046 
   4047 OMX_CONF_CMD_BAIL:
   4048     return eError;
   4049 }
   4050 
   4051 
   4052 #ifdef UNDER_CE
   4053 /* ================================================================================= */
   4054 /**
   4055 * @fns Sleep replace for WIN CE
   4056 */
   4057 /* ================================================================================ */
   4058 int OMX_CreateEvent(OMX_Event *event){
   4059     int ret = OMX_ErrorNone;
   4060     HANDLE createdEvent = NULL;
   4061     if (event == NULL)
   4062     {
   4063         ret = OMX_ErrorBadParameter;
   4064         goto EXIT;
   4065     }
   4066     event->event  = CreateEvent(NULL, TRUE, FALSE, NULL);
   4067     if(event->event == NULL)
   4068         ret = (int)GetLastError();
   4069 EXIT:
   4070     return ret;
   4071 }
   4072 
   4073 int OMX_SignalEvent(OMX_Event *event){
   4074      int ret = OMX_ErrorNone;
   4075     if (event == NULL)
   4076     {
   4077         ret = OMX_ErrorBadParameter;
   4078         goto EXIT;
   4079      }
   4080      SetEvent(event->event);
   4081      ret = (int)GetLastError();
   4082 EXIT:
   4083     return ret;
   4084 }
   4085 
   4086 int OMX_WaitForEvent(OMX_Event *event) {
   4087      int ret = OMX_ErrorNone;
   4088     if (event == NULL)
   4089     {
   4090         ret = OMX_ErrorBadParameter;
   4091         goto EXIT;
   4092      }
   4093      WaitForSingleObject(event->event, INFINITE);
   4094      ret = (int)GetLastError();
   4095 EXIT:
   4096      return ret;
   4097 }
   4098 
   4099 int OMX_DestroyEvent(OMX_Event *event) {
   4100      int ret = OMX_ErrorNone;
   4101     if (event == NULL)
   4102     {
   4103         ret = OMX_ErrorBadParameter;
   4104         goto EXIT;
   4105      }
   4106      CloseHandle(event->event);
   4107 EXIT:
   4108      return ret;
   4109 }
   4110 #endif
   4111 
   4112 #ifdef RESOURCE_MANAGER_ENABLED
   4113 /*-----------------------------------------------------------------------------*/
   4114 /**
   4115   * OMX_VIDENC_ResourceManagerCallBack()
   4116   *
   4117   * Called from Resource Manager()
   4118   *
   4119  *
   4120   *
   4121   **/
   4122 /*-----------------------------------------------------------------------------*/
   4123 void OMX_VIDENC_ResourceManagerCallBack(RMPROXY_COMMANDDATATYPE cbData)
   4124 {
   4125     OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
   4126     OMX_STATETYPE state = OMX_StateIdle;
   4127     OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
   4128     VIDENC_COMPONENT_PRIVATE *pCompPrivate = NULL;
   4129 
   4130     pCompPrivate = (VIDENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
   4131     OMX_PRMGR2(pCompPrivate->dbg, "OMX_VIDENC_ResourceManagerCallBack\n");
   4132     OMX_PRMGR2(pCompPrivate->dbg, "Arguments:\ncbData.RM_Error = %dcbData.RM_Cmd = %d\n", *(cbData.RM_Error), cbData.RM_Cmd);
   4133     if (*(cbData.RM_Error) == OMX_ErrorResourcesPreempted)
   4134     {
   4135         if (pCompPrivate->eState== OMX_StateExecuting ||
   4136             pCompPrivate->eState == OMX_StatePause)
   4137         {
   4138 
   4139         pCompPrivate->sCbData.EventHandler (
   4140                             pHandle, pHandle->pApplicationPrivate,
   4141                             OMX_EventError,
   4142                            OMX_ErrorResourcesPreempted,OMX_TI_ErrorMinor,
   4143                             "Componentn Preempted\n");
   4144 
   4145             OMX_PRSTATE2(pCompPrivate->dbg, "Send command to Idle from RM CallBack\n");
   4146         OMX_SendCommand(pHandle, Cmd, state, NULL);
   4147         pCompPrivate->bPreempted = 1;
   4148 
   4149         }
   4150     }
   4151     else if (*(cbData.RM_Error) == OMX_RmProxyCallback_ResourcesAcquired)
   4152     {
   4153         pCompPrivate->sCbData.EventHandler (
   4154                             pHandle, pHandle->pApplicationPrivate,
   4155                             OMX_EventResourcesAcquired, 0,0,
   4156                             NULL);
   4157         OMX_PRSTATE2(pCompPrivate->dbg, "Send command to Executing from RM CallBack\n");
   4158         OMX_SendCommand(pHandle, Cmd, OMX_StateExecuting, NULL);
   4159     }
   4160 }
   4161 #endif
   4162 
   4163 void CalculateBufferSize(OMX_PARAM_PORTDEFINITIONTYPE* pCompPort, VIDENC_COMPONENT_PRIVATE* pCompPrivate)
   4164 {
   4165 
   4166     if(pCompPort->nPortIndex == VIDENC_INPUT_PORT) {
   4167         if (pCompPort->format.video.eColorFormat == OMX_COLOR_FormatYUV420Planar)
   4168         {
   4169             pCompPort->nBufferSize = pCompPort->format.video.nFrameWidth *
   4170                                     pCompPort->format.video.nFrameHeight * 1.5;
   4171         }
   4172         else
   4173         {
   4174             pCompPort->nBufferSize = pCompPort->format.video.nFrameWidth *
   4175                                     pCompPort->format.video.nFrameHeight * 2;
   4176         }
   4177     }
   4178     else {
   4179         if (pCompPort->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC)
   4180         {
   4181             pCompPort->nBufferSize = GetMaxAVCBufferSize(pCompPort->format.video.nFrameWidth, pCompPort->format.video.nFrameHeight);
   4182         }
   4183         else
   4184         {/*coding Mpeg4 or H263*/
   4185             pCompPort->nBufferSize = pCompPort->format.video.nFrameWidth *
   4186                                     pCompPort->format.video.nFrameHeight / 2;
   4187         }
   4188         pCompPort->nBufferSize += 256;
   4189     }
   4190 }
   4191 
   4192 OMX_U32 GetMaxAVCBufferSize(OMX_U32 width, OMX_U32 height)
   4193 {
   4194     OMX_U32 MaxCPB;
   4195     OMX_U32 nMacroBlocks;
   4196 
   4197     /* Calculate output buffer size based on max possible CPB for the resolution
   4198        Output bitrate may not be set yet, so only resolution is taken into account */
   4199 
   4200     nMacroBlocks = (width * height) / 256;
   4201 
   4202     /* Following values are set based on Annex A of AVC Standard */
   4203     if(nMacroBlocks <= 99) {
   4204         MaxCPB = 500;
   4205     }
   4206     else if(nMacroBlocks <= 396) {
   4207         MaxCPB = 2000;
   4208     }
   4209     else if(nMacroBlocks <= 792) {
   4210         MaxCPB = 4000;
   4211     }
   4212     else if(nMacroBlocks <= 1620) {
   4213         /* Note - Max bitrate in this case is assumed to max 4 Mbps to limit the buffer size
   4214            If bitrate in this particular case could be higher than 4 Mbps, increase MxCPB value */
   4215         MaxCPB = 4000;
   4216     }
   4217     else
   4218         MaxCPB = 14000;
   4219 
   4220     /* MaxCPB are in units of 1200 bits i.e. 150 bytes */
   4221     /* Return  buffer size in bytes*/
   4222     /*Last patch to improve the performance*/
   4223     /*return (150 * MaxCPB);*/
   4224     return (width * height) / 2;
   4225 }
   4226 OMX_U32 OMX_VIDENC_GetDefaultBitRate(VIDENC_COMPONENT_PRIVATE* pComponentPrivate)
   4227 {
   4228     OMX_PARAM_PORTDEFINITIONTYPE* pPortDef;
   4229     OMX_U32 bitrate;
   4230     int nCount;
   4231 
   4232     pPortDef = pComponentPrivate->pCompPort[VIDENC_OUTPUT_PORT]->pPortDef;
   4233     for ( nCount = 0; nCount < VIDENC_MAXBITRATES; nCount++ ) {
   4234         if (pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingAVC) {
   4235             bitrate = VIDENC_STRUCT_H264DEFBITRATE [nCount][1];
   4236             if ((pPortDef->format.video.nFrameWidth * pPortDef->format.video.nFrameHeight)
   4237                 <= VIDENC_STRUCT_H264DEFBITRATE[nCount][0]) {
   4238                 break;
   4239             }
   4240         }
   4241         else if (pPortDef->format.video.eCompressionFormat == OMX_VIDEO_CodingMPEG4) {
   4242             bitrate = VIDENC_STRUCT_MPEG4DEFBITRATE [nCount][1];
   4243             if ((pPortDef->format.video.nFrameWidth * pPortDef->format.video.nFrameHeight)
   4244                 <= VIDENC_STRUCT_MPEG4DEFBITRATE[nCount][0]) {
   4245                 break;
   4246             }
   4247         }
   4248         else {
   4249             bitrate = VIDENC_STRUCT_H263DEFBITRATE [nCount][1];
   4250             if ((pPortDef->format.video.nFrameWidth * pPortDef->format.video.nFrameHeight)
   4251                 <= VIDENC_STRUCT_H263DEFBITRATE[nCount][0]) {
   4252                 break;
   4253             }
   4254         }
   4255     }
   4256 
   4257     return bitrate;
   4258 }
   4259 
   4260 
   4261 void printMpeg4Params(MP4VE_GPP_SN_Obj_CreatePhase* pCreatePhaseArgs,
   4262                       struct OMX_TI_Debug *dbg)
   4263 {
   4264     OMX_PRDSP2(*dbg, "\nusNumStreams = %d\n", pCreatePhaseArgs->usNumStreams);
   4265     OMX_PRDSP2(*dbg, "usStreamId = %d\n", pCreatePhaseArgs->usStreamId);
   4266     OMX_PRDSP2(*dbg, "usBuffTypeInStream = %d\n", pCreatePhaseArgs->usBuffTypeInStream);
   4267     OMX_PRDSP2(*dbg, "usMaxBuffsInStream = %d\n", pCreatePhaseArgs->usMaxBuffsInStream);
   4268     OMX_PRDSP2(*dbg, "usStreamId2 = %d\n", pCreatePhaseArgs->usStreamId2);
   4269     OMX_PRDSP2(*dbg, "usBuffTypeInStream2 = %d\n", pCreatePhaseArgs->usBuffTypeInStream2);
   4270     OMX_PRDSP2(*dbg, "usMaxBuffsInStream2 = %d\n", pCreatePhaseArgs->usMaxBuffsInStream2);
   4271 
   4272     OMX_PRDSP2(*dbg, "ulWidth = %d\n", pCreatePhaseArgs->ulWidth);
   4273     OMX_PRDSP2(*dbg, "ulHeight = %d\n", pCreatePhaseArgs->ulHeight);
   4274     OMX_PRDSP2(*dbg, "ulTargetBitRate = %d\n", pCreatePhaseArgs->ulTargetBitRate);
   4275     OMX_PRDSP2(*dbg, "ulVBVSize = %d\n", pCreatePhaseArgs->ulVBVSize);
   4276     OMX_PRDSP2(*dbg, "ulGOBHeadersInterval = %d\n", pCreatePhaseArgs->ulGOBHeadersInterval);
   4277 
   4278     OMX_PRDSP2(*dbg, "ucIsMPEG4 = %d\n", pCreatePhaseArgs->ucIsMPEG4);
   4279     OMX_PRDSP2(*dbg, "ucYUVFormat = %d\n", pCreatePhaseArgs->ucYUVFormat);
   4280     OMX_PRDSP2(*dbg, "ucHEC = %d\n", pCreatePhaseArgs->ucHEC);
   4281     OMX_PRDSP2(*dbg, "ucResyncMarker = %d\n", pCreatePhaseArgs->ucResyncMarker);
   4282     OMX_PRDSP2(*dbg, "ucDataPartitioning = %d\n", pCreatePhaseArgs->ucDataPartitioning);
   4283     OMX_PRDSP2(*dbg, "ucReversibleVLC = %d\n", pCreatePhaseArgs->ucReversibleVLC);
   4284     OMX_PRDSP2(*dbg, "ucUnrestrictedMV = %d\n", pCreatePhaseArgs->ucUnrestrictedMV);
   4285     OMX_PRDSP2(*dbg, "ucFrameRate = %d\n", pCreatePhaseArgs->ucFrameRate);
   4286     OMX_PRDSP2(*dbg, "ucRateControlAlgorithm = %d\n", pCreatePhaseArgs->ucRateControlAlgorithm);
   4287     OMX_PRDSP2(*dbg, "ucQPFirstIFrame = %d\n", pCreatePhaseArgs->ucQPFirstIFrame);
   4288     OMX_PRDSP2(*dbg, "ucProfile = %d\n", pCreatePhaseArgs->ucProfile);
   4289     OMX_PRDSP2(*dbg, "ucLevel = %d\n", pCreatePhaseArgs->ucLevel);
   4290     OMX_PRDSP2(*dbg, "ulMaxDelay = %d\n", pCreatePhaseArgs->ulMaxDelay);
   4291     /*
   4292     OMX_PRDSP2(*dbg, "ulVbvParamEnable = %d\n", pCreatePhaseArgs->ulVbvParamEnable);
   4293     OMX_PRDSP2(*dbg, "ulH263SliceMode = %d\n", pCreatePhaseArgs->ulH263SliceMode);
   4294     */
   4295     OMX_PRDSP2(*dbg, "ulUseGOV = %d\n", pCreatePhaseArgs->ulUseGOV);
   4296     OMX_PRDSP2(*dbg, "ulUseVOS = %d\n", pCreatePhaseArgs->ulUseVOS);
   4297     OMX_PRDSP2(*dbg, "enableH263AnnexI = %d\n", pCreatePhaseArgs->enableH263AnnexI);
   4298     OMX_PRDSP2(*dbg, "enableH263AnnexJ = %d\n", pCreatePhaseArgs->enableH263AnnexJ);
   4299     OMX_PRDSP2(*dbg, "enableH263AnnexT = %d\n", pCreatePhaseArgs->enableH263AnnexT);
   4300 }
   4301 void printH264CreateParams(H264VE_GPP_SN_Obj_CreatePhase* pCreatePhaseArgs, struct OMX_TI_Debug *dbg)
   4302 {
   4303     OMX_PRDSP2(*dbg, "\nusNumStreams = %d\n", pCreatePhaseArgs->usNumStreams);
   4304     OMX_PRDSP2(*dbg, "usStreamId = %d\n", pCreatePhaseArgs->usStreamId);
   4305     OMX_PRDSP2(*dbg, "usBuffTypeInStream = %d\n", pCreatePhaseArgs->usBuffTypeInStream);
   4306     OMX_PRDSP2(*dbg, "usMaxBuffsInStream = %d\n", pCreatePhaseArgs->usMaxBuffsInStream);
   4307     OMX_PRDSP2(*dbg, "usStreamId2 = %d\n", pCreatePhaseArgs->usStreamId2);
   4308     OMX_PRDSP2(*dbg, "usBuffTypeInStream2 = %d\n", pCreatePhaseArgs->usBuffTypeInStream2);
   4309     OMX_PRDSP2(*dbg, "usMaxBuffsInStream2 = %d\n", pCreatePhaseArgs->usMaxBuffsInStream2);
   4310 
   4311     OMX_PRDSP2(*dbg, "ulWidth = %d\n", pCreatePhaseArgs->ulWidth);
   4312     OMX_PRDSP2(*dbg, "ulHeight = %d\n", pCreatePhaseArgs->ulHeight);
   4313     OMX_PRDSP2(*dbg, "ulTargetBitRate = %d\n", pCreatePhaseArgs->ulTargetBitRate);
   4314     OMX_PRDSP2(*dbg, "ulBitstreamBuffSize = %d\n", pCreatePhaseArgs->ulBitstreamBuffSize);
   4315     OMX_PRDSP2(*dbg, "ulIntraFramePeriod = %d\n", pCreatePhaseArgs->ulIntraFramePeriod);
   4316     OMX_PRDSP2(*dbg, "ulFrameRate = %d\n", pCreatePhaseArgs->ulFrameRate);
   4317 
   4318     OMX_PRDSP2(*dbg, "ucYUVFormat = %d\n", pCreatePhaseArgs->ucYUVFormat);
   4319     OMX_PRDSP2(*dbg, "ucUnrestrictedMV = %d\n", pCreatePhaseArgs->ucUnrestrictedMV);
   4320     OMX_PRDSP2(*dbg, "ucNumRefFrames = %d\n", pCreatePhaseArgs->ucNumRefFrames);
   4321     OMX_PRDSP2(*dbg, "ucRateControlAlgorithm = %d\n", pCreatePhaseArgs->ucRateControlAlgorithm);
   4322     OMX_PRDSP2(*dbg, "ucIDREnable = %d\n", pCreatePhaseArgs->ucIDREnable);
   4323     OMX_PRDSP2(*dbg, "ucDeblockingEnable = %d\n", pCreatePhaseArgs->ucDeblockingEnable);
   4324     OMX_PRDSP2(*dbg, "ucMVRange = %d\n", pCreatePhaseArgs->ucMVRange);
   4325     OMX_PRDSP2(*dbg, "ucQPIFrame = %d\n", pCreatePhaseArgs->ucQPIFrame);
   4326     OMX_PRDSP2(*dbg, "ucProfile = %d\n", pCreatePhaseArgs->ucProfile);
   4327     OMX_PRDSP2(*dbg, "ucLevel = %d\n", pCreatePhaseArgs->ucLevel);
   4328 
   4329     OMX_PRDSP2(*dbg, "usNalCallback = %d\n", pCreatePhaseArgs->usNalCallback);
   4330     OMX_PRDSP2(*dbg, "ulEncodingPreset = %d\n", pCreatePhaseArgs->ulEncodingPreset);
   4331     OMX_PRDSP2(*dbg, "ulRcAlgo = %d\n", pCreatePhaseArgs->ulRcAlgo);
   4332 }
   4333 
   4334 void printMpeg4UAlgInParam(MP4VE_GPP_SN_UALGInputParams* pUalgInpParams, int printAlways, struct OMX_TI_Debug *dbg)
   4335 {
   4336     static int printed=0;
   4337 
   4338     if(printAlways || !printed)
   4339     {
   4340         printed++;
   4341         OMX_PRDSP2(*dbg, "\nulFrameIndex = %u\n", pUalgInpParams->ulFrameIndex);
   4342         OMX_PRDSP2(*dbg, "ulTargetFrameRate = %u\n", pUalgInpParams->ulTargetFrameRate);
   4343         OMX_PRDSP2(*dbg, "ulTargetBitRate = %u\n", pUalgInpParams->ulTargetBitRate);
   4344         OMX_PRDSP2(*dbg, "ulIntraFrameInterval = %u\n", pUalgInpParams->ulIntraFrameInterval);
   4345         OMX_PRDSP2(*dbg, "ulGenerateHeader = %u\n", pUalgInpParams->ulGenerateHeader);
   4346         OMX_PRDSP2(*dbg, "ulForceIFrame = %u\n", pUalgInpParams->ulForceIFrame);
   4347         OMX_PRDSP2(*dbg, "ulResyncInterval = %u\n", pUalgInpParams->ulResyncInterval);
   4348         OMX_PRDSP2(*dbg, "ulHecInterval = %u\n", pUalgInpParams->ulHecInterval);
   4349         OMX_PRDSP2(*dbg, "ulAIRRate = %u\n", pUalgInpParams->ulAIRRate);
   4350         OMX_PRDSP2(*dbg, "ulMIRRate = %u\n", pUalgInpParams->ulMIRRate);
   4351         OMX_PRDSP2(*dbg, "ulQPIntra = %u\n", pUalgInpParams->ulQPIntra);
   4352         OMX_PRDSP2(*dbg, "ulfCode = %u\n", pUalgInpParams->ulfCode);
   4353         OMX_PRDSP2(*dbg, "ulHalfPel = %u\n", pUalgInpParams->ulHalfPel);
   4354         OMX_PRDSP2(*dbg, "ulACPred = %u\n", pUalgInpParams->ulACPred);
   4355         OMX_PRDSP2(*dbg, "ul4MV = %u\n", pUalgInpParams->ul4MV);
   4356         OMX_PRDSP2(*dbg, "uluseUMV = %u\n", pUalgInpParams->uluseUMV);
   4357         OMX_PRDSP2(*dbg, "ulMVDataEnable = %u\n", pUalgInpParams->ulMVDataEnable);
   4358         OMX_PRDSP2(*dbg, "ulResyncDataEnable = %u\n", pUalgInpParams->ulResyncDataEnable);
   4359         OMX_PRDSP2(*dbg, "ulQPInter = %u\n", pUalgInpParams->ulQPInter);
   4360         OMX_PRDSP2(*dbg, "ulLastFrame = %u\n", pUalgInpParams->ulLastFrame);
   4361         OMX_PRDSP2(*dbg, "ulcapturewidth = %u\n", pUalgInpParams->ulcapturewidth);
   4362         OMX_PRDSP2(*dbg, "ulQpMax = %u\n", pUalgInpParams->ulQpMax);
   4363         OMX_PRDSP2(*dbg, "ulQpMin = %u\n", pUalgInpParams->ulQpMin);
   4364     }
   4365 }
   4366 
   4367 
   4368 void printH264UAlgInParam(H264VE_GPP_SN_UALGInputParams* pUalgInpParams, int printAlways, struct OMX_TI_Debug *dbg)
   4369 {
   4370     static int printed=0;
   4371 
   4372     if(printAlways || !printed)
   4373     {
   4374         printed++;
   4375         OMX_PRDSP2(*dbg, "\nqpIntra = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.qpIntra);
   4376         OMX_PRDSP2(*dbg, "qpInter = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.qpInter);
   4377         OMX_PRDSP2(*dbg, "qpMax = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.qpMax);
   4378         OMX_PRDSP2(*dbg, "qpMin = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.qpMin);
   4379         OMX_PRDSP2(*dbg, "lfDisableIdc = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.lfDisableIdc);
   4380         OMX_PRDSP2(*dbg, "quartPelDisable = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.quartPelDisable);
   4381         OMX_PRDSP2(*dbg, "airMbPeriod = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.airMbPeriod);
   4382         OMX_PRDSP2(*dbg, "maxMBsPerSlice = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.maxMBsPerSlice);
   4383         OMX_PRDSP2(*dbg, "maxBytesPerSlice = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.maxBytesPerSlice);
   4384         OMX_PRDSP2(*dbg, "sliceRefreshRowStartNumber = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceRefreshRowStartNumber);
   4385         OMX_PRDSP2(*dbg, "sliceRefreshRowNumber = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceRefreshRowNumber);
   4386         OMX_PRDSP2(*dbg, "filterOffsetA = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.filterOffsetA);
   4387         OMX_PRDSP2(*dbg, "filterOffsetB = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.filterOffsetB);
   4388         OMX_PRDSP2(*dbg, "log2MaxFNumMinus4 = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.log2MaxFNumMinus4);
   4389         OMX_PRDSP2(*dbg, "chromaQPIndexOffset = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.chromaQPIndexOffset);
   4390         OMX_PRDSP2(*dbg, "constrainedIntraPredEnable = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.constrainedIntraPredEnable);
   4391         OMX_PRDSP2(*dbg, "picOrderCountType = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.picOrderCountType);
   4392         OMX_PRDSP2(*dbg, "maxMVperMB = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.maxMVperMB);
   4393         OMX_PRDSP2(*dbg, "intra4x4EnableIdc = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.intra4x4EnableIdc);
   4394         OMX_PRDSP2(*dbg, "mvDataEnable = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.mvDataEnable);
   4395         OMX_PRDSP2(*dbg, "hierCodingEnable = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.hierCodingEnable);
   4396         OMX_PRDSP2(*dbg, "streamFormat = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.streamFormat);
   4397         OMX_PRDSP2(*dbg, "intraRefreshMethod = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.intraRefreshMethod);
   4398         OMX_PRDSP2(*dbg, "perceptualQuant = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.perceptualQuant);
   4399         OMX_PRDSP2(*dbg, "sceneChangeDet = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sceneChangeDet);
   4400         OMX_PRDSP2(*dbg, "numSliceASO = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.numSliceASO);
   4401         OMX_PRDSP2(*dbg, "numSliceGroups = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.numSliceGroups);
   4402         OMX_PRDSP2(*dbg, "sliceGroupMapType = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceGroupMapType);
   4403         OMX_PRDSP2(*dbg, "sliceGroupChangeDirectionFlag = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeDirectionFlag);
   4404         OMX_PRDSP2(*dbg, "sliceGroupChangeRate = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeRate);
   4405         OMX_PRDSP2(*dbg, "sliceGroupChangeCycle = %lu\n", pUalgInpParams->H264VENC_TI_DYNAMICPARAMS.sliceGroupChangeCycle);
   4406         OMX_PRDSP2(*dbg, "ulFrameIndex = %lu\n", pUalgInpParams->ulFrameIndex);
   4407     }
   4408 }
   4409 
   4410 OMX_ERRORTYPE IsResolutionPlayable (OMX_U32 width, OMX_U32 height)
   4411 {
   4412     if (width  > WVGA_MAX_WIDTH || height > WVGA_MAX_HEIGHT)
   4413     {
   4414         return OMX_ErrorBadParameter;
   4415     }
   4416     return OMX_ErrorNone;
   4417 
   4418 }
   4419 
   4420 OMX_ERRORTYPE AddStateTransition(VIDENC_COMPONENT_PRIVATE* pComponentPrivate) {
   4421 
   4422     OMX_ERRORTYPE eError = OMX_ErrorNone;
   4423 
   4424     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   4425        return OMX_ErrorUndefined;
   4426     }
   4427 
   4428     /* Increment state change request reference count */
   4429     pComponentPrivate->nPendingStateChangeRequests++;
   4430 
   4431     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   4432        return OMX_ErrorUndefined;
   4433     }
   4434 
   4435     return eError;
   4436 }
   4437 
   4438 OMX_ERRORTYPE RemoveStateTransition(VIDENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BOOL bEnableSignal) {
   4439     OMX_ERRORTYPE eError = OMX_ErrorNone;
   4440 
   4441      /* Decrement state change request reference count*/
   4442     if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
   4443        return OMX_ErrorUndefined;
   4444     }
   4445 
   4446     pComponentPrivate->nPendingStateChangeRequests--;
   4447 
   4448    /* If there are no more pending requests, signal the thread waiting on this*/
   4449     if(!pComponentPrivate->nPendingStateChangeRequests && bEnableSignal) {
   4450        pthread_cond_signal(&(pComponentPrivate->StateChangeCondition));
   4451     }
   4452 
   4453     if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
   4454        return OMX_ErrorUndefined;
   4455     }
   4456 
   4457     return eError;
   4458 }
   4459