OMX_JpegEncoder.c revision b44cdb1b44b144d09e8ec950d1e8e4c280064056
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_JpegEncoder.c
30*
31* This file implements OMX Component for JPEG 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*! 22-May-2006 mf: Revisions appear in reverse chronological order;
45*! that is, newest first.  The date format is dd-Mon-yyyy.
46* ================================================================================= */
47
48/****************************************************************
49*  INCLUDE FILES
50****************************************************************/
51
52/* ----- System and Platform Files ----------------------------*/
53#ifdef UNDER_CE
54#include <windows.h>
55#include <oaf_osal.h>
56#include <omx_core.h>
57#else
58#include <unistd.h>
59#include <sys/time.h>
60#include <sys/types.h>
61#include <sys/ioctl.h>
62#include <sys/select.h>
63#include <errno.h>
64#include <pthread.h>
65#endif
66
67#include <string.h>
68#include <fcntl.h>
69#include <stdlib.h>
70#include <stdio.h>
71#include <dbapi.h>
72
73
74/*------- Program Header Files ----------------------------------------*/
75
76#include "OMX_JpegEnc_Utils.h"
77#include "OMX_JpegEnc_CustomCmd.h"
78
79#ifdef RESOURCE_MANAGER_ENABLED
80    #include <ResourceManagerProxyAPI.h>
81#endif
82/*----------------------Global-----------------------------------*/
83OMX_STRING cJPEGencName = "OMX.TI.JPEG.encoder";
84/*--------function prototypes ---------------------------------*/
85static OMX_ERRORTYPE JPEGENC_SetCallbacks (OMX_HANDLETYPE hComp,
86                                   OMX_CALLBACKTYPE* pCallBacks,
87                                   OMX_PTR pAppData);
88
89static OMX_ERRORTYPE JPEGENC_GetComponentVersion (OMX_HANDLETYPE hComp,
90                                          OMX_STRING pComponentName,
91                                          OMX_VERSIONTYPE* pComponentVersion,
92                                          OMX_VERSIONTYPE* pSpecVersion,
93                                          OMX_UUIDTYPE* pComponentUUID);
94
95static OMX_ERRORTYPE JPEGENC_SendCommand (OMX_HANDLETYPE hComponent,
96                                  OMX_COMMANDTYPE Cmd,
97                                  OMX_U32 nParam,
98                                  OMX_PTR pCmdData);
99
100static OMX_ERRORTYPE JPEGENC_GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
101                                   OMX_IN OMX_INDEXTYPE nParamIndex,
102                                   OMX_INOUT OMX_PTR ComponentParameterStructure);
103
104static OMX_ERRORTYPE JPEGENC_SetParameter (OMX_HANDLETYPE hComp,
105                                   OMX_INDEXTYPE nParamIndex,
106                                   OMX_PTR ComponentParameterStructure);
107
108static OMX_ERRORTYPE JPEGENC_GetConfig (OMX_HANDLETYPE hComp,
109                                OMX_INDEXTYPE nConfigIndex,
110                                OMX_PTR ComponentConfigStructure);
111
112static OMX_ERRORTYPE JPEGENC_SetConfig (OMX_HANDLETYPE hComp,
113                                OMX_INDEXTYPE nConfigIndex,
114                                OMX_PTR ComponentConfigStructure);
115
116static OMX_ERRORTYPE JPEGENC_EmptyThisBuffer (OMX_HANDLETYPE hComp,
117                                      OMX_BUFFERHEADERTYPE* pBuffer);
118
119static OMX_ERRORTYPE JPEGENC_FillThisBuffer (OMX_HANDLETYPE hComp,
120                                     OMX_BUFFERHEADERTYPE* pBuffer);
121
122static OMX_ERRORTYPE JPEGENC_GetState (OMX_HANDLETYPE hComp,
123                               OMX_STATETYPE* pState);
124
125static OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest(OMX_IN  OMX_HANDLETYPE hComp,
126                                            OMX_IN  OMX_U32 nPort,
127                                            OMX_IN  OMX_HANDLETYPE hTunneledComp,
128                                            OMX_IN  OMX_U32 nTunneledPort,
129                                            OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup);
130
131
132static OMX_ERRORTYPE JPEGENC_VerifyTunnelConnection(JPEG_PORT_TYPE*pPort,
133                                            OMX_HANDLETYPE hTunneledComp,
134					    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
135					    struct OMX_TI_Debug *dbg);
136
137static OMX_ERRORTYPE JPEGENC_UseBuffer(OMX_IN OMX_HANDLETYPE hComponent,
138                               OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
139                               OMX_IN OMX_U32 nPortIndex,
140                               OMX_IN OMX_PTR pAppPrivate,
141                               OMX_IN OMX_U32 nSizeBytes,
142                               OMX_IN OMX_U8* pBuffer);
143
144static OMX_ERRORTYPE JPEGENC_FreeBuffer(OMX_IN  OMX_HANDLETYPE hComponent,
145                                OMX_IN  OMX_U32 nPortIndex,
146                                OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer);
147
148static OMX_ERRORTYPE JPEGENC_ComponentDeInit(OMX_HANDLETYPE pHandle);
149
150static OMX_ERRORTYPE JPEGENC_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
151                             OMX_INOUT OMX_BUFFERHEADERTYPE** pBuffer,
152                             OMX_IN OMX_U32 nPortIndex,
153                             OMX_IN OMX_PTR pAppPrivate,
154                             OMX_IN OMX_U32 nSizeBytes);
155
156static void JPEGENC_InitBufferFlagTrack(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nPortIndex);
157
158#ifdef KHRONOS_1_1
159static OMX_ERRORTYPE ComponentRoleEnum(
160                OMX_IN OMX_HANDLETYPE hComponent,
161                OMX_OUT OMX_U8 *cRole,
162                OMX_IN OMX_U32 nIndex);
163#endif
164
165static OMX_ERRORTYPE JPEGENC_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent,
166                                                OMX_IN OMX_STRING cParameterName,
167                                                OMX_OUT OMX_INDEXTYPE* pIndexType);
168
169
170/*-------- Function Implementations ---------------------------------*/
171
172
173static OMX_ERRORTYPE JPEGENC_FreeBuffer(OMX_IN  OMX_HANDLETYPE hComponent,
174                                        OMX_IN  OMX_U32 nPortIndex,
175                                        OMX_IN  OMX_BUFFERHEADERTYPE* pBuffer)
176{
177    OMX_COMPONENTTYPE *pHandle                   = NULL;
178    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
179    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef       = NULL;
180    JPEGENC_BUFFER_PRIVATE* pBuffPrivate         = NULL;
181    JPEG_PORT_TYPE* pCompPort                    = NULL;
182    OMX_ERRORTYPE eError                         = OMX_ErrorNone;
183    char* pTemp                                  = NULL;
184
185    OMX_CHECK_PARAM(hComponent);
186    OMX_CHECK_PARAM(pBuffer);
187
188    pHandle = (OMX_COMPONENTTYPE *) hComponent;
189    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *) pHandle->pComponentPrivate;
190    pCompPort = pComponentPrivate->pCompPort[nPortIndex];
191
192    OMX_PRINT1(pComponentPrivate->dbg, "JPEGENC_FreeBuffer %p at port %d\n", pBuffer, (int)nPortIndex);
193
194#ifdef __PERF_INSTRUMENTATION__
195    /* never frees buffer */
196    PERF_SendingFrame(pComponentPrivate->pPERF,
197                      pBuffer->pBuffer,
198                      pBuffer->nFilledLen,
199                      PERF_ModuleMax);
200#endif
201
202    pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
203
204    if ( nPortIndex == pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex ) {
205        pBuffPrivate = pBuffer->pInputPortPrivate;
206    }
207    else if ( nPortIndex == pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex  ) {
208        pBuffPrivate = pBuffer->pOutputPortPrivate;
209    }
210    else {
211            eError = OMX_ErrorBadPortIndex;
212            goto EXIT;
213    }
214
215    if (pBuffPrivate->bAllocByComponent == OMX_TRUE) {
216        if (pBuffer->pBuffer) {
217           pTemp = (char*)pBuffer->pBuffer;
218           pTemp -= 128;
219           OMX_FREE(pTemp);
220           pBuffer->pBuffer = NULL;
221        }
222    }
223
224    OMX_FREE(pBuffer);
225
226    if ( pPortDef->bEnabled &&
227        ((pComponentPrivate->nCurState == OMX_StateIdle && pComponentPrivate->nToState != OMX_StateLoaded) ||
228         pComponentPrivate->nCurState == OMX_StateExecuting ||
229         pComponentPrivate->nCurState == OMX_StatePause) ) {
230        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
231                                      pComponentPrivate->pHandle->pApplicationPrivate,
232                                      OMX_EventError,
233                                      OMX_ErrorPortUnpopulated,
234                                      OMX_TI_ErrorMinor,
235                                      "Port Unpopulated");
236        }
237
238        pCompPort->nBuffCount--;
239    OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEGE: bPopulated %d\n", pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated);
240    if (pCompPort->nBuffCount == 0) {
241        pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated = OMX_FALSE;
242
243        pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
244        pthread_cond_signal(&pComponentPrivate->unpopulate_cond);
245        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
246
247        JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
248    }
249
250    if ((!pPortDef->bEnabled) &&
251        (!pComponentPrivate->pCompPort[nPortIndex]->pPortDef->bPopulated)) {
252        if ((nPortIndex == 0) && (!pComponentPrivate->bInportDisableIncomplete)) {
253            pComponentPrivate->bInportDisableIncomplete = OMX_TRUE;
254            pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
255                                                pComponentPrivate->pHandle->pApplicationPrivate,
256                                                OMX_EventCmdComplete,
257                                                OMX_CommandPortDisable,
258                                                JPEGENC_INP_PORT,
259                                                NULL);
260        }
261        if ((nPortIndex == 1) && (!pComponentPrivate->bOutportDisableIncomplete)) {
262            pComponentPrivate->bOutportDisableIncomplete = OMX_TRUE;
263            pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
264                                                pComponentPrivate->pHandle->pApplicationPrivate,
265                                                OMX_EventCmdComplete,
266                                                OMX_CommandPortDisable,
267                                                JPEGENC_OUT_PORT,
268                                                NULL);
269        }
270    }
271
272EXIT:
273    OMX_PRINT1(pComponentPrivate->dbg, "Exit from FreeBuffer\n");
274
275    return eError;
276}
277
278OMX_ERRORTYPE JPEGENC_UseBuffer(OMX_IN OMX_HANDLETYPE hComponent,
279            OMX_INOUT OMX_BUFFERHEADERTYPE** ppBufferHdr,
280            OMX_IN OMX_U32 nPortIndex,
281            OMX_IN OMX_PTR pAppPrivate,
282            OMX_IN OMX_U32 nSizeBytes,
283            OMX_IN OMX_U8* pBuffer)
284
285{
286    OMX_COMPONENTTYPE *pHandle                      = NULL;
287    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate    = NULL;
288    OMX_PARAM_PORTDEFINITIONTYPE* pPortDef          = NULL;
289    OMX_ERRORTYPE eError                            = OMX_ErrorNone;
290    OMX_HANDLETYPE hTunnelComponent                 = NULL;
291    OMX_U32 nBufferCount                            = -1;
292    OMX_BUFFERHEADERTYPE *pBuffHeader               = NULL;
293    int nInpIndex;
294    int nOutIndex;
295
296
297    OMX_CHECK_PARAM(hComponent);
298    OMX_CHECK_PARAM(ppBufferHdr);
299    OMX_CHECK_PARAM(pBuffer);
300
301    if (nPortIndex < 0 || nPortIndex > 1) {
302        eError = OMX_ErrorBadParameter;
303        goto EXIT;
304    }
305
306    pHandle = (OMX_COMPONENTTYPE *)hComponent;
307    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
308    hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
309    pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
310    nBufferCount = pComponentPrivate->pCompPort[nPortIndex]->nBuffCount;
311
312#ifdef __PERF_INSTRUMENTATION__
313    PERF_ReceivedFrame(pComponentPrivate->pPERF,
314                       pBuffer,
315                       nSizeBytes,
316                       PERF_ModuleLLMM);
317#endif
318
319    pPortDef = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
320
321
322    nInpIndex = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex;
323    nOutIndex = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex;
324
325    if ( (int)nPortIndex == nInpIndex ) {
326        OMX_MALLOC(pBuffHeader, sizeof(OMX_BUFFERHEADERTYPE));
327        OMX_CONF_INIT_STRUCT(pBuffHeader, OMX_BUFFERHEADERTYPE);
328        pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBuffHeader;
329        *ppBufferHdr = pBuffHeader;
330    }
331    else if ( (int)nPortIndex == nOutIndex ) {
332        OMX_MALLOC(pBuffHeader, sizeof(OMX_BUFFERHEADERTYPE));
333        OMX_CONF_INIT_STRUCT(pBuffHeader, OMX_BUFFERHEADERTYPE);
334        pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBuffHeader;
335        *ppBufferHdr = pBuffHeader;
336    }
337    else {
338        eError = OMX_ErrorBadPortIndex;
339        goto EXIT;
340    }
341
342    /* set relevant fields */
343    (*ppBufferHdr)->nSize       = sizeof(OMX_BUFFERHEADERTYPE);
344    (*ppBufferHdr)->nVersion    = pPortDef->nVersion;
345    (*ppBufferHdr)->pBuffer     = pBuffer;
346    (*ppBufferHdr)->nAllocLen   = nSizeBytes;
347    (*ppBufferHdr)->pAppPrivate = pAppPrivate;
348    pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_FALSE;
349
350    /* set direction dependent fields */
351    if (hTunnelComponent != NULL) {
352
353        if ( pPortDef->eDir == OMX_DirInput ) {
354            (*ppBufferHdr)->nInputPortIndex   = nPortIndex;
355            (*ppBufferHdr)->nOutputPortIndex  = pComponentPrivate->pCompPort[nPortIndex]->nTunnelPort;
356            /* pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->pBufferHdr = (*ppBufferHdr); */
357
358        }
359        else {
360            (*ppBufferHdr)->nOutputPortIndex   = nPortIndex;
361            (*ppBufferHdr)->nInputPortIndex    = pComponentPrivate->pCompPort[nPortIndex]->nTunnelPort;
362            /* pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->pBufferHdr = (*ppBufferHdr); */
363        }
364    }
365    else {
366        if (nPortIndex == JPEGENC_INP_PORT) {
367            pBuffHeader->nInputPortIndex  = JPEGENC_INP_PORT;
368            pBuffHeader->nOutputPortIndex = OMX_NOPORT;
369        }
370        else {
371            pBuffHeader->nInputPortIndex  = OMX_NOPORT;
372            pBuffHeader->nOutputPortIndex = JPEGENC_OUT_PORT;
373        }
374    }
375    if (nPortIndex == JPEGENC_INP_PORT) {
376        pBuffHeader->pInputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
377    }
378    else {
379        pBuffHeader->pOutputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
380    }
381
382    if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent != NULL) {
383        pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
384    }
385    else {
386        pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_CLIENT;
387    }
388
389    if ( nPortIndex == pComponentPrivate->pCompPort[nPortIndex]->pPortFormat->nPortIndex ) {
390        pComponentPrivate->pCompPort[nPortIndex]->nBuffCount++;
391        if (pComponentPrivate->pCompPort[nPortIndex]->nBuffCount == pPortDef->nBufferCountActual) {
392            pPortDef->bPopulated = OMX_TRUE;
393
394            pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
395            pthread_cond_signal(&pComponentPrivate->populate_cond);
396            pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
397
398            JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
399        }
400    }
401
402EXIT:
403    return eError;
404
405}
406/*-------------------------------------------------------------------*/
407/**
408  * OMX_ComponentInit() Set the all the function pointers of component
409  *
410  * This method will update the component function pointer to the handle
411  *
412  * @param hComp         handle for this instance of the component
413  *
414  * @retval OMX_NoError              Success, ready to roll
415  *         OMX_ErrorInsufficientResources If the malloc fails
416  **/
417/*-------------------------------------------------------------------*/
418OMX_ERRORTYPE OMX_ComponentInit (OMX_HANDLETYPE hComponent)
419{
420    OMX_COMPONENTTYPE *pHandle  = NULL;
421    OMX_ERRORTYPE eError            = OMX_ErrorNone;
422    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
423    OMX_U8 i = 0;
424
425    /* Allocate memory for component's private data area */
426    OMX_CHECK_PARAM(hComponent);
427
428
429    LinkedList_Create(&AllocList);
430
431    pHandle = (OMX_COMPONENTTYPE *)hComponent;
432    OMX_MALLOC(pHandle->pComponentPrivate, sizeof(JPEGENC_COMPONENT_PRIVATE));
433
434    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
435
436    /* get default settings for debug */
437    OMX_DBG_INIT(pComponentPrivate->dbg, "OMX_DBG_JPGENC");
438    /* Allocate memory for component's private data area */
439    OMX_PRINT1(pComponentPrivate->dbg, "in jpeg-enc OMX_ComponentInit\n");
440
441#ifdef __PERF_INSTRUMENTATION__
442        pComponentPrivate->pPERF = PERF_Create(PERF_FOURCC('J','P','E',' '),
443                                               PERF_ModuleLLMM | PERF_ModuleImageEncode);
444        pComponentPrivate->pPERFcomp = NULL;
445#endif
446
447    /**Assigning address of Component Structure point to place holder inside comp
448    nentprivate structure
449    */
450    ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pHandle = pHandle;
451
452    pComponentPrivate->nCurState = OMX_StateLoaded;
453
454    pHandle->SetCallbacks               = JPEGENC_SetCallbacks;
455    pHandle->GetComponentVersion        = JPEGENC_GetComponentVersion;
456    pHandle->SendCommand                = JPEGENC_SendCommand;
457    pHandle->GetParameter               = JPEGENC_GetParameter;
458    pHandle->SetParameter               = JPEGENC_SetParameter;
459    pHandle->GetConfig                  = JPEGENC_GetConfig;
460    pHandle->SetConfig                  = JPEGENC_SetConfig;
461    pHandle->GetState                   = JPEGENC_GetState;
462    pHandle->EmptyThisBuffer            = JPEGENC_EmptyThisBuffer;
463    pHandle->FillThisBuffer             = JPEGENC_FillThisBuffer;
464    pHandle->ComponentTunnelRequest     = JPEGENC_ComponentTunnelRequest;
465    pHandle->ComponentDeInit            = JPEGENC_ComponentDeInit;
466    pHandle->UseBuffer                  = JPEGENC_UseBuffer    ;
467    pHandle->FreeBuffer                 = JPEGENC_FreeBuffer;
468    pHandle->AllocateBuffer             = JPEGENC_AllocateBuffer;
469    pHandle->GetExtensionIndex = JPEGENC_GetExtensionIndex;
470#ifdef KHRONOS_1_1
471    pHandle->ComponentRoleEnum          = ComponentRoleEnum;
472#endif
473    OMX_PRINT2(pComponentPrivate->dbg, "Inside Component Init JPEG encoder\n");
474    pComponentPrivate->ComponentVersion.s.nVersionMajor = 0x01;
475    pComponentPrivate->ComponentVersion.s.nVersionMinor = 0x00;
476    pComponentPrivate->ComponentVersion.s.nRevision = 0x00;
477    pComponentPrivate->ComponentVersion.s.nStep = 0x00;
478    OMX_MALLOC(pComponentPrivate->cComponentName, COMP_MAX_NAMESIZE+1);
479    strncpy(pComponentPrivate->cComponentName, cJPEGencName, COMP_MAX_NAMESIZE);
480
481    /* Allocate memory for component data structures */
482    OMX_MALLOC(pComponentPrivate->pPortParamType, sizeof(OMX_PORT_PARAM_TYPE));
483    OMX_MALLOC(pComponentPrivate->pPortParamTypeAudio, sizeof(OMX_PORT_PARAM_TYPE));
484    OMX_MALLOC(pComponentPrivate->pPortParamTypeVideo, sizeof(OMX_PORT_PARAM_TYPE));
485    OMX_MALLOC(pComponentPrivate->pPortParamTypeOthers, sizeof(OMX_PORT_PARAM_TYPE));
486    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT], sizeof(JPEG_PORT_TYPE));
487    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT], sizeof(JPEG_PORT_TYPE));
488    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
489    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
490    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
491    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
492    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
493    OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier, sizeof(OMX_PARAM_BUFFERSUPPLIERTYPE));
494    OMX_MALLOC(pComponentPrivate->pPriorityMgmt, sizeof(OMX_PRIORITYMGMTTYPE));
495    OMX_MALLOC(pComponentPrivate->pQualityfactor, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
496    OMX_MALLOC(pComponentPrivate->pCrop, sizeof(OMX_CONFIG_RECTTYPE));
497    OMX_MALLOC(pComponentPrivate->pCustomLumaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
498    OMX_MALLOC(pComponentPrivate->pCustomChromaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
499    OMX_MALLOC(pComponentPrivate->pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
500    for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
501        OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i], sizeof(JPEGENC_BUFFER_PRIVATE));
502    }
503
504    for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
505        pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr = NULL;
506    }
507
508    for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
509        OMX_MALLOC(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i], sizeof(JPEGENC_BUFFER_PRIVATE));
510    }
511
512    for (i = 0;i<NUM_OF_BUFFERSJPEG;i++) {
513        pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr = NULL;
514    }
515
516    pComponentPrivate->nFlags               = 0;
517    pComponentPrivate->isLCMLActive         = 0;
518    pComponentPrivate->nCommentFlag         = 0;
519    /* pComponentPrivate->nThumbnailFlag   = 0; */
520    pComponentPrivate->pMarkData            = NULL;
521    pComponentPrivate->hMarkTargetComponent = NULL;
522    pComponentPrivate->pString_Comment      = NULL;
523    pComponentPrivate->nDRI_Interval        = 0;
524    pComponentPrivate->bSetHuffmanTable = OMX_FALSE;
525    pComponentPrivate->bSetLumaQuantizationTable = OMX_FALSE;
526    pComponentPrivate->bSetChromaQuantizationTable = OMX_FALSE;
527
528    pComponentPrivate->InParams.pInParams = NULL;
529    pComponentPrivate->InParams.size = 0;
530    pComponentPrivate->bPreempted = OMX_FALSE;
531
532    OMX_MALLOC(pComponentPrivate->pDynParams, sizeof(IDMJPGE_TIGEM_DynamicParams)+256);
533    if (!pComponentPrivate->pDynParams) {
534        eError = OMX_ErrorInsufficientResources;
535        goto EXIT;
536    }
537
538    eError = SetJpegEncInParams(pComponentPrivate);
539
540#ifdef KHRONOS_1_1
541    strcpy((char *)pComponentPrivate->componentRole.cRole, "image_encoder.jpeg");
542#endif
543
544    /* Set pPortParamType defaults */
545    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamType, OMX_PORT_PARAM_TYPE);
546    pComponentPrivate->pPortParamType->nPorts = NUM_OF_PORTS;
547    pComponentPrivate->pPortParamType->nStartPortNumber = 0;
548
549    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeAudio, OMX_PORT_PARAM_TYPE);
550    pComponentPrivate->pPortParamTypeAudio->nPorts = 0;
551    pComponentPrivate->pPortParamTypeAudio->nStartPortNumber = -1;
552
553    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeVideo, OMX_PORT_PARAM_TYPE);
554    pComponentPrivate->pPortParamTypeVideo->nPorts = 0;
555    pComponentPrivate->pPortParamTypeVideo->nStartPortNumber = -1;
556
557    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPortParamTypeOthers, OMX_PORT_PARAM_TYPE);
558    pComponentPrivate->pPortParamTypeOthers->nPorts = 0;
559    pComponentPrivate->pPortParamTypeOthers->nStartPortNumber = -1;
560
561    /*Set Quality factor structure */
562/*    OMX_CONF_INIT_STRUCT(pComponentPrivate->pQualityfactor, OMX_IMAGE_PARAM_QFACTORTYPE);*/
563
564    /* Set pInPortDef defaults */
565    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
566    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nPortIndex            = JPEGENC_INP_PORT;
567    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir                  = OMX_DirInput;
568    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual    = NUM_OF_BUFFERSJPEG;
569    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountMin       = 1;
570    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferSize           = 460800; /* 50688; */
571    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bEnabled              = OMX_TRUE;
572    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated            = OMX_FALSE;
573    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDomain               = OMX_PortDomainImage;
574
575    pComponentPrivate->isLCMLActive = 0;
576    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.cMIMEType             = "JPEGENC";
577    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.pNativeRender         = NULL;
578    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nFrameWidth           = 640;  /* 176; */
579    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nFrameHeight          = 480; /* 144; */
580    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nStride               = -1;
581    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.nSliceHeight          = -1;
582    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.bFlagErrorConcealment = OMX_FALSE;
583    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.eCompressionFormat    =  OMX_IMAGE_CodingUnused; /* OMX_IMAGE_CodingJPEG; */
584    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->format.image.eColorFormat          =  OMX_COLOR_FormatYUV420PackedPlanar; /* OMX_COLOR_FormatCbYCrY; */
585
586
587    /* Set pOutPortDef defaults */
588    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
589    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nPortIndex         = JPEGENC_OUT_PORT;
590    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->eDir               = OMX_DirOutput;
591    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountMin    = 1;
592    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual = NUM_OF_BUFFERSJPEG;
593    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferSize        = 460800; /* 50688; */
594    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bEnabled           = OMX_TRUE;
595    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated         = OMX_FALSE;
596    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->eDomain            = OMX_PortDomainImage;
597    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.cMIMEType        = "JPEGENC";
598    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.pNativeRender    = NULL;
599    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nFrameWidth      = 640; /* 176; */
600    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nFrameHeight     = 480; /* 144; */
601    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nStride          = -1;
602    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.nSliceHeight     = -1;
603    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.bFlagErrorConcealment    = OMX_FALSE;
604    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.eCompressionFormat       = OMX_IMAGE_CodingJPEG; /* OMX_IMAGE_CodingUnused; */
605    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->format.image.eColorFormat             =  OMX_COLOR_FormatUnused; /* OMX_COLOR_FormatCbYCrY; */
606
607
608   OMX_CONF_INIT_STRUCT(pComponentPrivate->pCustomLumaQuantTable, OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE);
609    pComponentPrivate->pCustomLumaQuantTable->nPortIndex = JPEGENC_OUT_PORT;
610    pComponentPrivate->pCustomLumaQuantTable->eQuantizationTable = OMX_IMAGE_QuantizationTableLuma;
611
612   OMX_CONF_INIT_STRUCT(pComponentPrivate->pCustomChromaQuantTable, OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE);
613    pComponentPrivate->pCustomChromaQuantTable->nPortIndex = JPEGENC_OUT_PORT;
614    pComponentPrivate->pCustomChromaQuantTable->eQuantizationTable = OMX_IMAGE_QuantizationTableChroma;
615
616   OMX_CONF_INIT_STRUCT(pComponentPrivate->pHuffmanTable, JPEGENC_CUSTOM_HUFFMANTTABLETYPE);
617    pComponentPrivate->pHuffmanTable->nPortIndex = JPEGENC_OUT_PORT;
618
619    /* Set pInPortFormat defaults */
620    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat, OMX_IMAGE_PARAM_PORTFORMATTYPE);
621    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nPortIndex         = JPEGENC_INP_PORT;
622    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->nIndex             = 0x0;
623    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->eCompressionFormat = OMX_IMAGE_CodingUnused; /* OMX_IMAGE_CodingJPEG; */
624    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortFormat->eColorFormat       = OMX_COLOR_FormatYUV420PackedPlanar; /* OMX_COLOR_FormatCbYCrY; */
625
626    /* Set pOutPortFormat defaults */
627    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat, OMX_IMAGE_PARAM_PORTFORMATTYPE);
628    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nPortIndex         = JPEGENC_OUT_PORT;
629    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->nIndex             = 0x0;
630    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->eCompressionFormat = OMX_IMAGE_CodingJPEG; /* OMX_IMAGE_CodingUnused; */
631    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortFormat->eColorFormat       = OMX_COLOR_FormatUnused; /* OMX_COLOR_FormatCbYCrY; */
632
633    /* Set pPriorityMgmt defaults */
634    OMX_CONF_INIT_STRUCT(pComponentPrivate->pPriorityMgmt, OMX_PRIORITYMGMTTYPE);
635    pComponentPrivate->pPriorityMgmt->nGroupPriority = -1;
636    pComponentPrivate->pPriorityMgmt->nGroupID       = -1;
637
638    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCrop, OMX_CONFIG_RECTTYPE);
639    OMX_CONF_INIT_STRUCT(pComponentPrivate->pQualityfactor, OMX_IMAGE_PARAM_QFACTORTYPE);
640    pComponentPrivate->pQualityfactor->nPortIndex = 1;
641    pComponentPrivate->pQualityfactor->nQFactor   = 100;
642
643   pComponentPrivate->sAPP0.bMarkerEnabled = OMX_FALSE;
644   pComponentPrivate->sAPP1.bMarkerEnabled = OMX_FALSE;
645   pComponentPrivate->sAPP5.bMarkerEnabled = OMX_FALSE;
646   pComponentPrivate->sAPP13.bMarkerEnabled = OMX_FALSE;
647
648   pComponentPrivate->sAPP0.pMarkerBuffer = NULL;
649   pComponentPrivate->sAPP1.pMarkerBuffer = NULL;
650   pComponentPrivate->sAPP5.pMarkerBuffer = NULL;
651   pComponentPrivate->sAPP13.pMarkerBuffer = NULL;
652
653    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
654    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier->nPortIndex      = JPEGENC_INP_PORT;
655    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pParamBufSupplier->eBufferSupplier = 0x0;
656
657    /* Set pOutBufSupplier defaults */
658    OMX_CONF_INIT_STRUCT(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
659    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier->nPortIndex      = JPEGENC_OUT_PORT;
660    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pParamBufSupplier->eBufferSupplier = 0x0;
661
662
663
664    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufSupplier= OMX_BufferSupplyOutput;
665    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent = NULL;
666    pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->nBuffCount       = 0;
667
668
669    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent = NULL;
670    pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->nBuffCount       = 0;
671
672    pthread_mutex_init(&pComponentPrivate->jpege_mutex, NULL);
673    pthread_cond_init(&pComponentPrivate->stop_cond, NULL);
674    pthread_cond_init(&pComponentPrivate->flush_cond, NULL);
675    /* pthread_cond_init(&pComponentPrivate->control_cond, NULL); */
676
677    pthread_mutex_init(&pComponentPrivate->jpege_mutex_app, NULL);
678    pthread_cond_init(&pComponentPrivate->populate_cond, NULL);
679    pthread_cond_init(&pComponentPrivate->unpopulate_cond, NULL);
680
681#ifdef RESOURCE_MANAGER_ENABLED
682    /* load the ResourceManagerProxy thread */
683    eError = RMProxy_NewInitalizeEx(OMX_COMPONENTTYPE_IMAGE);
684    if ( eError != OMX_ErrorNone ) {
685        OMX_PRMGR4(pComponentPrivate->dbg, "Error returned from loading ResourceManagerProxy thread\n");
686        goto EXIT;
687    }
688
689#endif
690    eError = JPEGEnc_Start_ComponentThread(pHandle);
691    if (eError) {
692        OMX_PRINT4(pComponentPrivate->dbg, "Error while initializing thread\n");
693        goto EXIT;
694    }
695
696    OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG-ENC: actual buffer %d\n", (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual));
697 EXIT:
698    if(eError != OMX_ErrorNone){
699        /* LinkedList_DisplayAll (&AllocList); */
700        OMX_FREEALL();
701        LinkedList_Destroy(&AllocList);
702    }
703    return eError;
704}
705
706
707/*-------------------------------------------------------------------*/
708/**
709  *  JPEGENC_SetCallbacks() Sets application callbacks to the component
710  *
711  * This method will update application callbacks
712  * the application.
713  *
714  * @param pComponent    handle for this instance of the component
715  * @param pCallBacks    application callbacks
716  * @param pAppData      pointer to application Data
717  *
718  * @retval OMX_NoError              Success, ready to roll
719  *         OMX_Error_BadParameter   The input parameter pointer is null
720  **/
721/*-------------------------------------------------------------------*/
722static OMX_ERRORTYPE JPEGENC_SetCallbacks (OMX_HANDLETYPE pComponent,
723                                   OMX_CALLBACKTYPE* pCallBacks,
724                                   OMX_PTR pAppData)
725{
726    OMX_ERRORTYPE eError        = OMX_ErrorNone;
727    OMX_COMPONENTTYPE *pHandle  = NULL;
728    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
729
730    OMX_CHECK_PARAM(pComponent);
731    OMX_CHECK_PARAM(pCallBacks);
732
733    pHandle = (OMX_COMPONENTTYPE*)pComponent;
734    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
735
736    /*Copy the callbacks of the application to the component private  */
737    memcpy (&(pComponentPrivate->cbInfo), pCallBacks, sizeof(OMX_CALLBACKTYPE));
738    OMX_MEMCPY_CHECK(&(pComponentPrivate->cbInfo));
739
740    /*Copy the application private data to component memory  */
741    pHandle->pApplicationPrivate = pAppData;
742    pComponentPrivate->nCurState = OMX_StateLoaded;
743
744EXIT:
745    return eError;
746}
747
748
749/*-------------------------------------------------------------------*/
750/**
751  *  JPEGENC_GetComponentVersion() Sets application callbacks to the component
752  *
753  * This method will get the component Version.
754  *
755  * @param hComp         handle for this instance of the component
756  * @param pComponentName    application callbacks
757  * @param pComponentVersion
758  * @param pSpecVersion
759  * @param pComponentUUID
760  *
761  * @retval OMX_NoError              Success, ready to roll
762  *         OMX_Error_BadParameter   The input parameter pointer is null
763  **/
764/*-------------------------------------------------------------------*/
765static OMX_ERRORTYPE JPEGENC_GetComponentVersion (OMX_HANDLETYPE hComp,
766                                          OMX_STRING szComponentName,
767                                          OMX_VERSIONTYPE* pComponentVersion,
768                                          OMX_VERSIONTYPE* pSpecVersion,
769                                          OMX_UUIDTYPE* pComponentUUID)
770{
771    OMX_ERRORTYPE          eError            = OMX_ErrorNone;
772    OMX_COMPONENTTYPE    * pHandle           = NULL;
773    JPEGENC_COMPONENT_PRIVATE* pComponentPrivate = NULL;
774    OMX_U8 *pTemp = NULL;
775    if (!hComp || !szComponentName || !pComponentVersion || !pSpecVersion || !pComponentUUID) {
776        eError = OMX_ErrorBadParameter;
777        goto EXIT;
778    }
779    pHandle = (OMX_COMPONENTTYPE*)hComp;
780    if (!pHandle->pComponentPrivate) {
781        eError = OMX_ErrorBadParameter;
782        goto EXIT;
783    }
784    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
785
786    strncpy(szComponentName, pComponentPrivate->cComponentName, COMP_MAX_NAMESIZE);
787    pTemp = memcpy(pComponentVersion, &(pComponentPrivate->ComponentVersion.s), sizeof(pComponentPrivate->ComponentVersion.s));
788    if(pTemp == NULL){
789        eError = OMX_ErrorUndefined;
790        goto EXIT;
791    }
792    pTemp = memcpy(pSpecVersion, &(pComponentPrivate->SpecVersion.s), sizeof(pComponentPrivate->SpecVersion.s));
793    if(pTemp == NULL){
794        eError = OMX_ErrorUndefined;
795        goto EXIT;
796    }
797EXIT:
798    return eError;
799}
800
801/*-------------------------------------------------------------------*/
802/**
803  *  JPEGENC_SendCommand() Send commands to the
804  *
805  * This method will update application callbacks
806  * the application.
807  *
808  * @param phandle       handle for this instance of the component
809  * @param Cmd           OMX Command usually OMX_CommandStateSet
810  * @param nParam
811  *
812  * @retval OMX_NoError              Success, ready to roll
813  *         OMX_Error_BadParameter   The input parameter pointer is null
814  **/
815/*-------------------------------------------------------------------*/
816static OMX_ERRORTYPE JPEGENC_SendCommand (
817             OMX_HANDLETYPE hComponent,
818             OMX_COMMANDTYPE Cmd,
819             OMX_U32 nParam,
820             OMX_PTR pCmdData
821             )
822{
823    OMX_ERRORTYPE                eError         = OMX_ErrorNone;
824    OMX_COMPONENTTYPE            *pHandle       = NULL;
825    JPEGENC_COMPONENT_PRIVATE    *pCompPrivate  = NULL;
826    OMX_PARAM_PORTDEFINITIONTYPE *pPortDefIn    = NULL;
827    OMX_PARAM_PORTDEFINITIONTYPE *pPortDefOut   = NULL;
828    OMX_MARKTYPE *pMarkType = NULL;
829    OMX_COMMANDTYPE eCmd         = -1;
830    int nRet                = 0;
831
832    OMX_CHECK_PARAM(hComponent);
833
834    pHandle      = (OMX_COMPONENTTYPE *)hComponent;
835    pCompPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
836    pPortDefIn   = pCompPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
837    pPortDefOut  = pCompPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
838
839    OMX_PRINT2(pCompPrivate->dbg, "Print nParam = %d\n", (int)nParam);
840
841    if ( pCompPrivate->nCurState == OMX_StateInvalid ) {
842        eError = OMX_ErrorInvalidState;
843        goto EXIT;
844    }
845
846    switch ( Cmd ) {
847    case OMX_CommandStateSet:
848        eCmd = SetState;
849        pCompPrivate->nToState = nParam;
850        break;
851    case OMX_CommandFlush:
852	if ((nParam != 0) && (nParam != 1) && ((int)nParam != -1)) {
853            eError = OMX_ErrorBadPortIndex;
854            goto EXIT;
855        }
856        eCmd = Flush;
857        break;
858    case OMX_CommandPortDisable:
859	if (nParam == JPEGENC_INP_PORT  || (int)nParam == -1) {
860            pPortDefIn->bEnabled = OMX_FALSE;
861            pCompPrivate->bInportDisableIncomplete = OMX_FALSE;
862        }
863        if (nParam == JPEGENC_OUT_PORT  || (int)nParam == -1) {
864            pPortDefOut->bEnabled = OMX_FALSE;
865            pCompPrivate->bOutportDisableIncomplete = OMX_FALSE;
866        }
867        if ((nParam != JPEGENC_INP_PORT) && (nParam != JPEGENC_OUT_PORT) && ((int)nParam != -1))
868        {
869            eError = OMX_ErrorBadParameter;
870            goto EXIT;
871        }
872
873        if (!pPortDefIn->bPopulated) {
874            pCompPrivate->bInportDisableIncomplete = OMX_TRUE;
875            pCompPrivate->cbInfo.EventHandler (pCompPrivate->pHandle,
876            pCompPrivate->pHandle->pApplicationPrivate,
877            OMX_EventCmdComplete,
878            OMX_CommandPortDisable,
879            JPEGENC_INP_PORT,
880            NULL);
881        }
882
883        if (!pPortDefOut->bPopulated) {
884            pCompPrivate->bOutportDisableIncomplete = OMX_TRUE;
885            pCompPrivate->cbInfo.EventHandler (pCompPrivate->pHandle,
886                            pCompPrivate->pHandle->pApplicationPrivate,
887                            OMX_EventCmdComplete,
888                            OMX_CommandPortDisable,
889                            JPEGENC_OUT_PORT,
890                            NULL);
891        }
892        eCmd = OMX_CommandPortDisable;
893        break;
894
895    case OMX_CommandPortEnable:
896	if (nParam == JPEGENC_INP_PORT  || (int)nParam == -1) {
897            pPortDefIn->bEnabled = OMX_TRUE;
898        }
899        if (nParam == JPEGENC_OUT_PORT  || (int)nParam == -1) {
900            pPortDefOut->bEnabled = OMX_TRUE;
901        }
902        if ((nParam != JPEGENC_INP_PORT) && (nParam != JPEGENC_OUT_PORT) && ((int)nParam != -1)) {
903            eError = OMX_ErrorBadParameter;
904            goto EXIT;
905        }
906        eCmd = OMX_CommandPortEnable;
907        break;
908
909    case OMX_CommandMarkBuffer:
910        if ((nParam != JPEGENC_OUT_PORT) && (nParam != JPEGENC_INP_PORT)) {
911            eError = OMX_ErrorBadPortIndex;
912            goto EXIT;
913        }
914        pMarkType = (OMX_MARKTYPE *)pCmdData;
915        pCompPrivate->pMarkData = pMarkType->pMarkData;
916        pCompPrivate->hMarkTargetComponent = pMarkType->hMarkTargetComponent;
917        pCompPrivate->nMarkPort = nParam;
918        OMX_PRBUFFER2(pCompPrivate->dbg, "IN SendCommand, port %d mark %p\n", pCompPrivate->nMarkPort, pCompPrivate->pMarkData);
919        goto EXIT;
920        break;
921    default:
922        break;
923
924    }
925
926    nRet = write(pCompPrivate->nCmdPipe[1], &eCmd, sizeof(eCmd));
927    if (nRet == -1) {
928        OMX_PRCOMM4(pCompPrivate->dbg, "%d :: Error while writing to the pipe.\n", __LINE__);
929        eError = OMX_ErrorUndefined;
930    }
931
932#ifdef __PERF_INSTRUMENTATION__
933        PERF_SendingCommand(pCompPrivate->pPERF,
934                            Cmd,
935                            (Cmd == OMX_CommandMarkBuffer) ? (OMX_U32) pCmdData : nParam,
936                            PERF_ModuleComponent);
937#endif
938
939
940    nRet = write(pCompPrivate->nCmdDataPipe[1], &nParam, sizeof(nParam));
941    if (nRet == -1) {
942        OMX_PRCOMM4(pCompPrivate->dbg, "%d :: Error while writing to the pipe.\n", __LINE__);
943        eError = OMX_ErrorUndefined;
944    }
945    OMX_PRINT1(pCompPrivate->dbg, "Writing into Cmd Pipe\n");
946
947    EXIT:
948
949    return eError;
950}
951
952
953/*-------------------------------------------------------------------*/
954/**
955  *  JPEGENC_GetParameter() Gets Parameters from the Component
956  *
957  * This method will update parameters from the component to the app.
958  *
959  * @param hComp         handle for this instance of the component
960  * @param nParamIndex   Component Index Port
961  * @param ComponentParameterStructure Component Parameter Structure
962  *
963  * @retval OMX_NoError              Success, ready to roll
964  *         OMX_Error_BadParameter   The input parameter pointer is null
965  **/
966/*-------------------------------------------------------------------*/
967static OMX_ERRORTYPE JPEGENC_GetParameter (OMX_IN OMX_HANDLETYPE hComponent,
968                                   OMX_IN OMX_INDEXTYPE nParamIndex,
969                                   OMX_INOUT OMX_PTR pComponentParameterStructure)
970{
971    OMX_COMPONENTTYPE         *pComp             = NULL;
972    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
973    OMX_ERRORTYPE             eError             = OMX_ErrorNone;
974    JPEG_PORT_TYPE            *pInpPortType      = NULL;
975    JPEG_PORT_TYPE            *pOutPortType      = NULL;
976
977    OMX_CHECK_PARAM(hComponent);
978    OMX_CHECK_PARAM(pComponentParameterStructure);
979
980    pComp = (OMX_COMPONENTTYPE *)hComponent;
981    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pComp->pComponentPrivate;
982    OMX_PRINT1(pComponentPrivate->dbg, "Entering function\n");
983
984    if ( pComponentPrivate->nCurState == OMX_StateInvalid ) {
985        eError = OMX_ErrorInvalidState;
986        goto EXIT;
987    }
988    pInpPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
989    pOutPortType = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT];
990
991    switch ( nParamIndex ) {
992    case OMX_IndexParamImageInit:
993        memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamType, sizeof(OMX_PORT_PARAM_TYPE));
994        OMX_MEMCPY_CHECK(pComponentParameterStructure);
995        break;
996    case OMX_IndexParamAudioInit:
997        memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeAudio, sizeof(OMX_PORT_PARAM_TYPE));
998        OMX_MEMCPY_CHECK(pComponentParameterStructure);
999        break;
1000    case OMX_IndexParamVideoInit:
1001        memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeVideo, sizeof(OMX_PORT_PARAM_TYPE));
1002        OMX_MEMCPY_CHECK(pComponentParameterStructure);
1003        break;
1004    case OMX_IndexParamOtherInit:
1005        memcpy(pComponentParameterStructure, pComponentPrivate->pPortParamTypeOthers, sizeof(OMX_PORT_PARAM_TYPE));
1006        OMX_MEMCPY_CHECK(pComponentParameterStructure);
1007        break;
1008    case OMX_IndexParamPortDefinition:
1009    {
1010       OMX_PARAM_PORTDEFINITIONTYPE *pParamPortDef  = NULL;
1011
1012       pParamPortDef = (OMX_PARAM_PORTDEFINITIONTYPE *)pComponentParameterStructure;
1013       OMX_PRINT2(pComponentPrivate->dbg, "index is %d (%d) (%d)\n",
1014		  (int)(pParamPortDef->nPortIndex),
1015		  (int)(pInpPortType->pPortDef->nPortIndex),
1016		  (int)(pOutPortType->pPortDef->nPortIndex));
1017        if (pParamPortDef->nPortIndex == pInpPortType->pPortDef->nPortIndex)
1018        {
1019	    OMX_PRBUFFER2(pComponentPrivate->dbg, "nBufferCountActual %d (0) \n", (int)(pInpPortType->pPortDef->nBufferCountActual));
1020            /* pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated = OMX_FALSE; */
1021            memcpy(pComponentParameterStructure, pInpPortType->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1022            OMX_MEMCPY_CHECK(pComponentParameterStructure);
1023        } else if (pParamPortDef->nPortIndex == pOutPortType->pPortDef->nPortIndex)
1024        {
1025	    OMX_PRBUFFER2(pComponentPrivate->dbg, "nBufferCountActual %d (1)\n", (int)(pOutPortType->pPortDef->nBufferCountActual));
1026            /* pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated = OMX_FALSE; */
1027            memcpy(pComponentParameterStructure, pOutPortType->pPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1028            OMX_MEMCPY_CHECK(pComponentParameterStructure);
1029        } else {
1030            eError = OMX_ErrorBadPortIndex;
1031        }
1032    }
1033        break;
1034
1035    case OMX_IndexParamImagePortFormat:
1036        {
1037        OMX_IMAGE_PARAM_PORTFORMATTYPE *pParamImagePortFormat = NULL;
1038
1039        pParamImagePortFormat = (OMX_IMAGE_PARAM_PORTFORMATTYPE *)pComponentParameterStructure;
1040        if ( pParamImagePortFormat->nPortIndex == pInpPortType->pPortFormat->nPortIndex )
1041        {
1042            if (pParamImagePortFormat->nIndex > pInpPortType->pPortFormat->nIndex ) {
1043                eError = OMX_ErrorNoMore;
1044            } else {
1045                memcpy(pComponentParameterStructure, pInpPortType->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
1046                OMX_MEMCPY_CHECK(pComponentParameterStructure);
1047            }
1048        } else if (pParamImagePortFormat->nPortIndex == pOutPortType->pPortFormat->nPortIndex )
1049        {
1050            if ( pParamImagePortFormat->nIndex > pOutPortType->pPortFormat->nIndex ) {
1051                eError = OMX_ErrorNoMore;
1052            } else {
1053                memcpy(pComponentParameterStructure, pOutPortType->pPortFormat, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
1054                OMX_MEMCPY_CHECK(pComponentParameterStructure);
1055            }
1056        } else {
1057            eError = OMX_ErrorBadPortIndex;
1058        }
1059        }
1060        break;
1061
1062    case OMX_IndexParamPriorityMgmt:
1063        memcpy(pComponentParameterStructure, pComponentPrivate->pPriorityMgmt, sizeof(OMX_PRIORITYMGMTTYPE));
1064        OMX_MEMCPY_CHECK(pComponentParameterStructure);
1065        break;
1066
1067    case OMX_IndexParamCompBufferSupplier:
1068        {
1069        OMX_PARAM_BUFFERSUPPLIERTYPE *pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE *)pComponentParameterStructure;
1070
1071        if ( pBuffSupplierParam->nPortIndex == pOutPortType->pPortDef->nPortIndex)
1072        {
1073            pBuffSupplierParam->eBufferSupplier = pOutPortType->pParamBufSupplier->eBufferSupplier;
1074        } else if ( pBuffSupplierParam->nPortIndex == pInpPortType->pPortDef->nPortIndex )
1075        {
1076            pBuffSupplierParam->eBufferSupplier = pInpPortType->pParamBufSupplier->eBufferSupplier;
1077        } else
1078        {
1079            eError = OMX_ErrorBadPortIndex;
1080            break;
1081        }
1082        }
1083        break;
1084    case OMX_IndexParamQuantizationTable:
1085        {
1086        OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *pQuantTable = (OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *)pComponentParameterStructure;
1087        if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableLuma) {
1088            memcpy(pQuantTable, pComponentPrivate->pCustomLumaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
1089        }
1090        else if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableChroma) {
1091            memcpy(pQuantTable, pComponentPrivate->pCustomChromaQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
1092        }
1093        else { /* wrong eQuantizationTable, return error */
1094           eError = OMX_ErrorBadParameter;
1095        }
1096        break;
1097        }
1098    case OMX_IndexCustomHuffmanTable:
1099        {
1100        JPEGENC_CUSTOM_HUFFMANTTABLETYPE *pHuffmanTable = (JPEGENC_CUSTOM_HUFFMANTTABLETYPE *)pComponentParameterStructure;
1101        memcpy(pHuffmanTable, pComponentPrivate->pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
1102        break;
1103        }
1104    default:
1105        eError = OMX_ErrorUnsupportedIndex;
1106        break;
1107    }
1108
1109EXIT:
1110    OMX_PRINT1(pComponentPrivate->dbg, "Exit function eError = %x\n", eError);
1111    return eError;
1112
1113}
1114
1115
1116/*-------------------------------------------------------------------*/
1117/**
1118  *  JPEGENC_SetParameter() Gets the parameters sent by the Application and sets it to the component.
1119  *
1120  * This method will update component parameters .
1121  *
1122  *
1123  * @param hComp         handle for this instance of the component
1124  * @param nParamIndex    Component Params Index Port
1125  * @param ComponentParameterStructure Component Parameter Structure
1126  *
1127  * @retval OMX_NoError              Success, ready to roll
1128  *         OMX_Error_BadParameter   The input parameter pointer is null
1129  **/
1130/*-------------------------------------------------------------------*/
1131static OMX_ERRORTYPE JPEGENC_SetParameter (OMX_HANDLETYPE hComponent,
1132                                           OMX_INDEXTYPE nParamIndex,
1133                                           OMX_PTR pCompParam)
1134{
1135    OMX_COMPONENTTYPE         * pHandle           = NULL;
1136    JPEGENC_COMPONENT_PRIVATE * pComponentPrivate = NULL;
1137    OMX_ERRORTYPE               eError            = OMX_ErrorNone;
1138    JPEG_PORT_TYPE            *pInpPortType       = NULL;
1139    JPEG_PORT_TYPE            *pOutPortType       = NULL;
1140#ifdef KHRONOS_1_1
1141    OMX_PARAM_COMPONENTROLETYPE  *pRole;
1142#endif
1143
1144    OMX_CHECK_PARAM(hComponent);
1145    OMX_CHECK_PARAM(pCompParam);
1146
1147    pHandle= (OMX_COMPONENTTYPE*)hComponent;
1148    pComponentPrivate = pHandle->pComponentPrivate;
1149
1150    if ( pComponentPrivate->nCurState != OMX_StateLoaded ) {
1151        eError = OMX_ErrorIncorrectStateOperation;
1152        goto EXIT;
1153    }
1154
1155    pInpPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
1156    pOutPortType = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT];
1157
1158    switch ( nParamIndex ) {
1159    case OMX_IndexParamImagePortFormat:
1160    {
1161        OMX_IMAGE_PARAM_PORTFORMATTYPE* pComponentParam = (OMX_IMAGE_PARAM_PORTFORMATTYPE *)pCompParam;
1162        if ( pComponentParam->nPortIndex == pInpPortType->pPortFormat->nPortIndex )
1163        {
1164            memcpy(pInpPortType->pPortFormat, pComponentParam, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
1165            OMX_MEMCPY_CHECK(pInpPortType->pPortFormat);
1166        }
1167        else if ( pComponentParam->nPortIndex == pOutPortType->pPortFormat->nPortIndex ) {
1168            memcpy(pOutPortType->pPortFormat, pComponentParam, sizeof(OMX_IMAGE_PARAM_PORTFORMATTYPE));
1169            OMX_MEMCPY_CHECK(pOutPortType->pPortFormat);
1170        }
1171        else {
1172             eError = OMX_ErrorBadPortIndex;
1173        }
1174        break;
1175    }
1176    case OMX_IndexParamImageInit:
1177    {
1178        memcpy(pComponentPrivate->pPortParamType, (OMX_PORT_PARAM_TYPE*)pCompParam, sizeof(OMX_PORT_PARAM_TYPE));
1179        OMX_MEMCPY_CHECK(pComponentPrivate->pPortParamType);
1180        break;
1181    }
1182    case OMX_IndexParamPortDefinition:
1183    {
1184        OMX_PARAM_PORTDEFINITIONTYPE* pComponentParam = (OMX_PARAM_PORTDEFINITIONTYPE *)pCompParam;
1185        if ( pComponentParam->nPortIndex == pInpPortType->pPortDef->nPortIndex ) {
1186            memcpy(pInpPortType->pPortDef, pComponentParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1187            OMX_MEMCPY_CHECK(pInpPortType->pPortDef);
1188        }
1189        else if ( pComponentParam->nPortIndex == pOutPortType->pPortDef->nPortIndex ) {
1190            memcpy(pOutPortType->pPortDef, pComponentParam, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
1191            OMX_MEMCPY_CHECK(pOutPortType->pPortDef);
1192        }
1193        else {
1194            eError = OMX_ErrorBadPortIndex;
1195        }
1196        break;
1197    }
1198    case OMX_IndexParamPriorityMgmt:
1199    {
1200        memcpy(pComponentPrivate->pPriorityMgmt, (OMX_PRIORITYMGMTTYPE*)pCompParam, sizeof(OMX_PRIORITYMGMTTYPE));
1201        OMX_MEMCPY_CHECK(pComponentPrivate->pPriorityMgmt);
1202        break;
1203    }
1204    case OMX_IndexParamQFactor:
1205    {
1206        memcpy(pComponentPrivate->pQualityfactor, (OMX_IMAGE_PARAM_QFACTORTYPE*)pCompParam, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
1207        OMX_MEMCPY_CHECK(pComponentPrivate->pQualityfactor);
1208        break;
1209    }
1210    case OMX_IndexParamCompBufferSupplier:
1211    {
1212        OMX_PARAM_BUFFERSUPPLIERTYPE *pBuffSupplierParam = (OMX_PARAM_BUFFERSUPPLIERTYPE *)pCompParam;
1213
1214        if ( pBuffSupplierParam->nPortIndex == 1 /*pOutPortType->pBuffSupplierParam->nPortIndex */) {
1215            /* Copy parameters to input port buffer supplier type */
1216            pOutPortType->pBufSupplier = pBuffSupplierParam->eBufferSupplier;
1217        }
1218        else if ( pBuffSupplierParam->nPortIndex == 0 /* pInpPortType->pBuffSupplierParam->nPortIndex */ ) {
1219            pInpPortType->pBufSupplier = pBuffSupplierParam->eBufferSupplier;
1220        }
1221        else {
1222            eError = OMX_ErrorBadPortIndex;
1223            break;
1224        }
1225        break;
1226    }
1227#ifdef KHRONOS_1_1
1228    case OMX_IndexParamStandardComponentRole:
1229        if (pCompParam) {
1230            pRole = (OMX_PARAM_COMPONENTROLETYPE *)pCompParam;
1231            memcpy(&(pComponentPrivate->componentRole), (void *)pRole, sizeof(OMX_PARAM_COMPONENTROLETYPE));
1232        } else {
1233            eError = OMX_ErrorBadParameter;
1234        }
1235        break;
1236#endif
1237   case OMX_IndexParamQuantizationTable:
1238    {
1239        OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *pQuantTable = (OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE *)pCompParam;
1240        if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableLuma) {
1241            memcpy(pComponentPrivate->pCustomLumaQuantTable, pQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
1242            pComponentPrivate->bSetLumaQuantizationTable = OMX_TRUE;
1243            eError = SetJpegEncInParams(pComponentPrivate);
1244        }
1245        else if (pQuantTable->eQuantizationTable == OMX_IMAGE_QuantizationTableChroma) {
1246            memcpy(pComponentPrivate->pCustomChromaQuantTable, pQuantTable, sizeof(OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE));
1247            pComponentPrivate->bSetChromaQuantizationTable = OMX_TRUE;
1248            eError = SetJpegEncInParams(pComponentPrivate);
1249        }
1250        else { /* wrong eQuantizationTable, return error */
1251           eError = OMX_ErrorBadParameter;
1252        }
1253        break;
1254    }
1255   case OMX_IndexCustomHuffmanTable:
1256    {
1257        JPEGENC_CUSTOM_HUFFMANTTABLETYPE *pHuffmanTable = (JPEGENC_CUSTOM_HUFFMANTTABLETYPE *)pCompParam;
1258        if (pHuffmanTable->nPortIndex == pOutPortType->pPortDef->nPortIndex) {
1259            memcpy(pComponentPrivate->pHuffmanTable, pHuffmanTable, sizeof(JPEGENC_CUSTOM_HUFFMANTTABLETYPE));
1260            pComponentPrivate->bSetHuffmanTable = OMX_TRUE;
1261            eError = SetJpegEncInParams(pComponentPrivate);
1262        } else { /* wrong nPortIndex, return error */
1263           eError = OMX_ErrorBadPortIndex;
1264        }
1265        break;
1266    }
1267        break;  default:
1268        eError = OMX_ErrorUnsupportedIndex;
1269        break;
1270    }
1271
1272    EXIT:
1273    return eError;
1274}
1275
1276
1277/*-------------------------------------------------------------------*/
1278/**
1279  *  JPEGENC_GetConfig() Gets Configuration from the Component
1280  *
1281  * This method will update component config structure .(NOT IMPLEMENTED)
1282  *
1283  * @param hComp         handle for this instance of the component
1284  * @param nConfigIndex   Component Config Index Port
1285  * @param ComponentConfigStructure Component Config Structure
1286  *
1287  * @retval OMX_NoError              Success, ready to roll
1288  *         OMX_Error_BadParameter   The input parameter pointer is null
1289  **/
1290/*-------------------------------------------------------------------*/
1291static OMX_ERRORTYPE JPEGENC_GetConfig (OMX_HANDLETYPE hComp,
1292                                        OMX_INDEXTYPE nConfigIndex,
1293                                        OMX_PTR ComponentConfigStructure)
1294{
1295    OMX_ERRORTYPE eError = OMX_ErrorNone;
1296    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE*)hComp;
1297    OMX_CHECK_PARAM(hComp);
1298
1299    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = pHandle->pComponentPrivate;
1300
1301    OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_GetConfig function\n");
1302    switch ( nConfigIndex ) {
1303    case OMX_IndexCustomDebug:
1304	OMX_DBG_GETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1305	break;
1306    default:
1307        eError = OMX_ErrorUnsupportedIndex;
1308        break;
1309    }
1310 EXIT:
1311    return eError;
1312}
1313
1314
1315/*-------------------------------------------------------------------*/
1316/**
1317  *  JPEGENC_SetConfig() Gets Configuration structure to the component.
1318  *
1319  * This method will update application callbacks
1320  * the application.
1321  *
1322  * @param pComp         handle for this instance of the component
1323  * @param pCallBacks    application callbacks
1324  * @param ptr
1325  *
1326  * @retval OMX_NoError              Success, ready to roll
1327  *         OMX_Error_BadParameter   The input parameter pointer is null
1328  **/
1329/*-------------------------------------------------------------------*/
1330
1331static OMX_ERRORTYPE JPEGENC_SetConfig (OMX_HANDLETYPE hComp,
1332                                        OMX_INDEXTYPE nConfigIndex,
1333                                        OMX_PTR ComponentConfigStructure)
1334{
1335    OMX_ERRORTYPE      eError   = OMX_ErrorNone;
1336    OMX_COMPONENTTYPE* pHandle  = NULL;
1337    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate;
1338
1339    OMX_CHECK_PARAM(hComp);
1340
1341    pHandle = (OMX_COMPONENTTYPE *)hComp;
1342
1343    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1344    switch ( nConfigIndex ) {
1345    case OMX_IndexCustomCommentFlag :
1346    {
1347        int *nComment = (int*)ComponentConfigStructure;
1348        if ( nComment == NULL ) {
1349            eError = OMX_ErrorBadParameter;
1350            goto EXIT;
1351        }
1352        ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->nCommentFlag = *nComment;
1353
1354
1355        break;
1356    }
1357
1358	case OMX_IndexCustomAPP0:
1359		{
1360			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
1361			if (pMarkerInfo->nThumbnailHeight < 0 || pMarkerInfo->nThumbnailWidth < 0) {
1362				eError = OMX_ErrorBadParameter;
1363				goto EXIT;
1364			}
1365
1366			if (pComponentPrivate->sAPP0.pMarkerBuffer != NULL) {
1367					OMX_FREE(pComponentPrivate->sAPP0.pMarkerBuffer);
1368			}
1369
1370			memcpy (&pComponentPrivate->sAPP0, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
1371			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP0);
1372			if (pMarkerInfo->pMarkerBuffer != NULL) {
1373				OMX_MALLOC(pComponentPrivate->sAPP0.pMarkerBuffer, pMarkerInfo->nMarkerSize);
1374				memcpy (pComponentPrivate->sAPP0.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
1375				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP0.pMarkerBuffer);
1376			}
1377
1378			eError = SetJpegEncInParams(pComponentPrivate);
1379        break;
1380		}
1381
1382	case OMX_IndexCustomAPP1:
1383		{
1384			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
1385			if (pMarkerInfo->nThumbnailHeight < 0 || pMarkerInfo->nThumbnailWidth < 0) {
1386				eError = OMX_ErrorBadParameter;
1387				goto EXIT;
1388			}
1389
1390			if (pComponentPrivate->sAPP1.pMarkerBuffer != NULL) {
1391				OMX_FREE(pComponentPrivate->sAPP1.pMarkerBuffer);
1392			}
1393
1394			memcpy (&pComponentPrivate->sAPP1, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
1395			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP1);
1396			if (pMarkerInfo->pMarkerBuffer != NULL) {
1397				OMX_MALLOC(pComponentPrivate->sAPP1.pMarkerBuffer, pMarkerInfo->nMarkerSize);
1398				memcpy (pComponentPrivate->sAPP1.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
1399				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP1.pMarkerBuffer);
1400			}
1401
1402			eError = SetJpegEncInParams(pComponentPrivate);
1403			break;
1404		}
1405
1406	case OMX_IndexCustomAPP5:
1407		{
1408			JPEG_APPTHUMB_MARKER *pMarkerInfo = (JPEG_APPTHUMB_MARKER *) ComponentConfigStructure;
1409			if (pMarkerInfo->nThumbnailHeight < 0 || pMarkerInfo->nThumbnailWidth < 0) {
1410				eError = OMX_ErrorBadParameter;
1411				goto EXIT;
1412			}
1413
1414			if (pComponentPrivate->sAPP5.pMarkerBuffer != NULL) {
1415				OMX_FREE(pComponentPrivate->sAPP5.pMarkerBuffer);
1416			}
1417
1418			memcpy (&pComponentPrivate->sAPP5, pMarkerInfo, sizeof(JPEG_APPTHUMB_MARKER));
1419			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP5);
1420			if (pMarkerInfo->pMarkerBuffer != NULL) {
1421				OMX_MALLOC(pComponentPrivate->sAPP5.pMarkerBuffer, pMarkerInfo->nMarkerSize);
1422				memcpy (pComponentPrivate->sAPP5.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
1423				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP5.pMarkerBuffer);
1424			}
1425			eError = SetJpegEncInParams(pComponentPrivate);
1426			break;
1427		}
1428
1429	case OMX_IndexCustomAPP13:
1430		{
1431			JPEG_APP13_MARKER *pMarkerInfo = (JPEG_APP13_MARKER *) ComponentConfigStructure;
1432			if (pComponentPrivate->sAPP13.pMarkerBuffer != NULL) {
1433				OMX_FREE(pComponentPrivate->sAPP13.pMarkerBuffer);
1434			}
1435
1436			memcpy (&pComponentPrivate->sAPP13, pMarkerInfo, sizeof(JPEG_APP13_MARKER));
1437			OMX_MEMCPY_CHECK(&pComponentPrivate->sAPP13);
1438			if (pMarkerInfo->pMarkerBuffer != NULL) {
1439				OMX_MALLOC(pComponentPrivate->sAPP13.pMarkerBuffer, pMarkerInfo->nMarkerSize);
1440				memcpy (pComponentPrivate->sAPP13.pMarkerBuffer, pMarkerInfo->pMarkerBuffer, pMarkerInfo->nMarkerSize);
1441				OMX_MEMCPY_CHECK(pComponentPrivate->sAPP13.pMarkerBuffer);
1442			}
1443
1444			eError = SetJpegEncInParams(pComponentPrivate);
1445			break;
1446		}
1447
1448    case OMX_IndexCustomDRI:
1449        pComponentPrivate->nDRI_Interval = *(OMX_U8 *)ComponentConfigStructure;
1450        break;
1451
1452    case OMX_IndexCustomCommentString :
1453    {
1454        if(((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment == NULL){
1455            OMX_MALLOC(((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment , 256);
1456        }
1457        strncpy((char *)((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pString_Comment, (char *)ComponentConfigStructure, 255);
1458        eError = SetJpegEncInParams(pComponentPrivate);
1459        break;
1460    }
1461    case OMX_IndexCustomInputFrameWidth:
1462    {
1463        OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
1464        int *nWidth= (int*)ComponentConfigStructure;
1465
1466        pPortDefIn = ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pCompPort[JPEGENC_INP_PORT]->pPortDef;
1467        OMX_PRINT1(pComponentPrivate->dbg, "INIT nFrameHeight = %d\n", (int)(pPortDefIn->format.image.nFrameHeight));
1468        OMX_PRINT1(pComponentPrivate->dbg, "INIT nFrameWidth = %d\n", (int)(pPortDefIn->format.image.nFrameWidth));
1469        pPortDefIn->format.image.nFrameWidth = *nWidth;
1470        OMX_PRINT1(pComponentPrivate->dbg, "nFrameWidth = %d\n", (int)(pPortDefIn->format.image.nFrameWidth));
1471#if 0
1472        eError = SendDynamicParam(pComponentPrivate);
1473            if (eError != OMX_ErrorNone ) {
1474                OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1475                goto EXIT;
1476        }
1477#endif
1478        break;
1479    }
1480    case OMX_IndexCustomInputFrameHeight:
1481    {
1482        OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
1483        int *nHeight = (int*)ComponentConfigStructure;
1484
1485        pPortDefIn = ((JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate)->pCompPort[JPEGENC_INP_PORT]->pPortDef;
1486        pPortDefIn->format.image.nFrameHeight = *nHeight;
1487        OMX_PRINT1(pComponentPrivate->dbg, "nFrameHeight = %d\n", (int)(pPortDefIn->format.image.nFrameHeight));
1488#if 0
1489        eError = SendDynamicParam(pComponentPrivate);
1490            if (eError != OMX_ErrorNone ) {
1491                OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1492                goto EXIT;
1493        }
1494#endif
1495        break;
1496    }
1497    case OMX_IndexCustomQFactor:
1498    {
1499        memcpy(pComponentPrivate->pQualityfactor, (OMX_IMAGE_PARAM_QFACTORTYPE*)ComponentConfigStructure, sizeof(OMX_IMAGE_PARAM_QFACTORTYPE));
1500        OMX_MEMCPY_CHECK(pComponentPrivate->pQualityfactor);
1501        break;
1502    }
1503    case OMX_IndexConfigCommonInputCrop :
1504    {
1505        OMX_CONFIG_RECTTYPE *crop = (OMX_CONFIG_RECTTYPE*)ComponentConfigStructure;
1506        if ((crop->nTop != 0) || (crop->nLeft != 0))
1507        {
1508            eError = OMX_ErrorBadParameter;
1509        }
1510        else{
1511            pComponentPrivate->pCrop->nWidth = crop->nWidth;
1512            pComponentPrivate->pCrop->nHeight = crop->nHeight;
1513        }
1514        break;
1515    }
1516    case OMX_IndexCustomDebug:
1517        OMX_DBG_SETCONFIG(pComponentPrivate->dbg, ComponentConfigStructure);
1518	break;
1519    default:
1520        eError = OMX_ErrorUnsupportedIndex;
1521        break;
1522    }
1523
1524    if (pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StatePause) {
1525        eError = SendDynamicParam(pComponentPrivate);
1526            if (eError != OMX_ErrorNone ) {
1527                OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1528                goto EXIT;
1529        }
1530    }
1531
1532EXIT:
1533    OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_SetConfig function\n");
1534    return eError;
1535}
1536
1537
1538/*-------------------------------------------------------------------*/
1539/**
1540  *  JPEGENC_GetState() Gets OMX Component State
1541  *
1542  * This method will return OMX Component State
1543  *
1544  * @param pComponent   handle for this instance of the component
1545  * @param pState       State type
1546  *
1547  *
1548  * @retval OMX_NoError              Success, ready to roll
1549  *         OMX_Error_BadParameter   The input parameter pointer is null
1550  **/
1551/*-------------------------------------------------------------------*/
1552static OMX_ERRORTYPE JPEGENC_GetState (OMX_HANDLETYPE pComponent, OMX_STATETYPE* pState)
1553{
1554    OMX_ERRORTYPE eError        = OMX_ErrorNone;
1555    OMX_ERRORTYPE error         = OMX_ErrorUndefined;
1556    OMX_COMPONENTTYPE *pHandle  = NULL;
1557    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1558    OMX_CHECK_PARAM(pComponent);
1559
1560    pHandle = (OMX_COMPONENTTYPE *)pComponent;
1561    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1562    OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_GetState function\n");
1563    if( !pState )
1564    {
1565        error = OMX_ErrorBadParameter;
1566        goto EXIT;
1567    }
1568
1569    if ( pHandle && pHandle->pComponentPrivate ) {
1570        *pState =  ((JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate)->nCurState;
1571    }
1572    else
1573    {
1574        *pState = OMX_StateLoaded;
1575    }
1576    error = OMX_ErrorNone;
1577
1578EXIT:
1579    return error;
1580}
1581
1582
1583/*-------------------------------------------------------------------*/
1584/**
1585  *  JPEGENC_EmptyThisBuffer() Send Input Buffers
1586  *
1587  * The application uses this macro to send the input buffers filled with data to the input port of the component
1588  *
1589  * @param pComponent    handle for this instance of the component
1590  * @param nPortIndex    PortIndex
1591  * @param pBuffHead     Pointer to data filled
1592  *
1593  * @retval OMX_NoError              Success, ready to roll
1594  *         OMX_Error_BadParameter   The input parameter pointer is null
1595  *     OMX_ErrorInvalidState    Called in a invalid state
1596  *     OMX_ErrorHardware        Error in Writing to the Data pipe
1597  **/
1598/*-------------------------------------------------------------------*/
1599static OMX_ERRORTYPE JPEGENC_EmptyThisBuffer (OMX_HANDLETYPE pComponent,
1600                                              OMX_BUFFERHEADERTYPE* pBuffHead)
1601{
1602    OMX_ERRORTYPE              eError            = OMX_ErrorNone;
1603    OMX_COMPONENTTYPE         *pHandle           = NULL;
1604    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1605    JPEGENC_BUFFER_PRIVATE    *pBuffPrivate;
1606    OMX_HANDLETYPE             hTunnelComponent;
1607    int i;
1608    int ret;
1609
1610    OMX_CHECK_PARAM(pComponent);
1611
1612    pHandle = (OMX_COMPONENTTYPE *)pComponent;
1613    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1614    OMX_PRBUFFER1(pComponentPrivate->dbg, "inside JPEGENC_EmptyThisBuffer\n");
1615    hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
1616    pBuffPrivate = pBuffHead->pInputPortPrivate;
1617
1618    if ( pComponentPrivate->nCurState != OMX_StateExecuting &&
1619        pComponentPrivate->nCurState != OMX_StatePause &&
1620        pComponentPrivate->nCurState != OMX_StateIdle) {
1621        eError= OMX_ErrorIncorrectStateOperation;
1622        goto EXIT;
1623    }
1624
1625    if ( pBuffHead == NULL ) {
1626        eError = OMX_ErrorBadParameter;
1627        goto EXIT;
1628    }
1629
1630    if ( pBuffHead->nSize != sizeof(OMX_BUFFERHEADERTYPE) ) {
1631        eError = OMX_ErrorBadParameter;
1632        OMX_PRBUFFER4(pComponentPrivate->dbg, "JPEG-ENC: buffer header size is not correct\n");
1633        goto EXIT;
1634    }
1635
1636    if (!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bEnabled) {
1637        eError = OMX_ErrorIncorrectStateOperation;
1638        goto EXIT;
1639    }
1640
1641    if ( (pBuffHead->nVersion.s.nVersionMajor != 0x1) ||
1642         (pBuffHead->nVersion.s.nVersionMinor != 0x0) ||
1643         (pBuffHead->nVersion.s.nRevision != 0x0) ||
1644         (pBuffHead->nVersion.s.nStep != 0x0) ) {
1645
1646        eError= OMX_ErrorVersionMismatch;
1647        goto EXIT;
1648    }
1649
1650    if (pBuffHead->nInputPortIndex != 0x0) {
1651            eError = OMX_ErrorBadPortIndex;
1652            goto EXIT;
1653    }
1654
1655#ifdef __PERF_INSTRUMENTATION__
1656        PERF_ReceivedFrame(pComponentPrivate->pPERF,
1657                           pBuffHead->pBuffer,
1658                           pBuffHead->nFilledLen,
1659                           PERF_ModuleHLMM);
1660#endif
1661
1662    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nAllocLen = %lu\n", pBuffHead->nAllocLen);
1663    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nFilledLen = %lu\n", pBuffHead->nFilledLen);
1664
1665    pComponentPrivate->nInPortIn ++;
1666
1667    OMX_PRBUFFER2(pComponentPrivate->dbg, "EmptyThisBuffer nInPortIn %lu\n", pComponentPrivate->nInPortIn);
1668
1669    if (pBuffHead->nFlags == OMX_BUFFERFLAG_EOS) {
1670        OMX_PRBUFFER2(pComponentPrivate->dbg, "END OF STREAM DETECTED\n");
1671        pComponentPrivate->nFlags = OMX_BUFFERFLAG_EOS;
1672         /* pBuffHead->nFlags = 0; */
1673        OMX_PRBUFFER2(pComponentPrivate->dbg, "record EOS flag on buffer ID %lu\n", pComponentPrivate->nInPortIn);
1674        for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
1675            if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].buffer_id == 0xFFFFFFFF)
1676            {
1677                 OMX_PRBUFFER2(pComponentPrivate->dbg, "record buffer id in array %d\n", i);
1678                 pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].flag = pBuffHead->nFlags;
1679                 pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferFlagTrack[i].buffer_id =
1680                 pComponentPrivate->nInPortIn;
1681                 break;
1682             }
1683         }
1684     }
1685
1686     /* mark the first buffer from input port after receiving mark buffer command */
1687     if (pComponentPrivate->nMarkPort == JPEGENC_INP_PORT) {
1688         if (pComponentPrivate->pMarkData) {
1689               OMX_PRBUFFER2(pComponentPrivate->dbg, "get mark buffer command, mark buffer %p\n", pBuffHead);
1690               pBuffHead->pMarkData = pComponentPrivate->pMarkData;
1691               pBuffHead->hMarkTargetComponent = pComponentPrivate->hMarkTargetComponent;
1692               pComponentPrivate->pMarkData = NULL;
1693               pComponentPrivate->hMarkTargetComponent = NULL;
1694         }
1695     }
1696
1697     /* if a buffer from input port is marked, record this port # in the buffer queue */
1698     if (pBuffHead->pMarkData) {
1699	     for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
1700             if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferMarkTrack[i].buffer_id == 0xFFFFFFFF)
1701             {
1702                 JPEGENC_BUFFERMARK_TRACK *pMarkTrack;
1703                 pMarkTrack = &(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->sBufferMarkTrack[i]);
1704                 pMarkTrack->buffer_id = pComponentPrivate->nInPortIn;
1705                 pMarkTrack->pMarkData = pBuffHead->pMarkData;
1706                 pMarkTrack->hMarkTargetComponent = pBuffHead->hMarkTargetComponent;
1707                 OMX_PRBUFFER2(pComponentPrivate->dbg, "mark buffer at ID %lu\n", pComponentPrivate->nInPortIn);
1708                 break;
1709             }
1710         }
1711     }
1712
1713#if 0
1714    eError = SendDynamicParam(pComponentPrivate);
1715    if (eError != OMX_ErrorNone ) {
1716            JOMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1717            goto EXIT;
1718    }
1719#endif
1720
1721     pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_IN;
1722     pBuffPrivate->bReadFromPipe = OMX_FALSE;
1723
1724     OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (get filled input buffer) %lu %lu %lu %lu (%p)\n",
1725                    pComponentPrivate->nInPortIn,
1726                    pComponentPrivate->nInPortOut,
1727                    pComponentPrivate->nOutPortIn,
1728                    pComponentPrivate->nOutPortOut, pBuffHead);
1729
1730     /*Writing the component buffer corresponding to input buffer to infill_q  */
1731     OMX_PRCOMM2(pComponentPrivate->dbg, "EmptyThisBuffer: write to the queue %p \n", pBuffHead);
1732
1733     ret = write (pComponentPrivate->filled_inpBuf_Q[1], &(pBuffHead),sizeof(pBuffHead));
1734
1735     if ( ret == -1 ) {
1736         OMX_PRCOMM4(pComponentPrivate->dbg, "Error in Writing to the Data pipe\n");
1737         eError = OMX_ErrorHardware;
1738         goto EXIT;
1739     }
1740
1741EXIT:
1742    return eError;
1743}
1744
1745
1746/*-------------------------------------------------------------------*/
1747/**
1748  *  JPEGENC_FillThisBuffer() Empty an output Buffer
1749  *
1750  * The component uses this to request the application to
1751  * empty an output buffer which contains the decoded data..
1752  * @param pComponent         handle for this instance of the component
1753  * @param pCallBacks    application callbacks
1754  * @param ptr
1755  *
1756  * @retval OMX_NoError              Success, ready to roll
1757  *         OMX_Error_BadParameter   The input parameter pointer is null
1758  **/
1759/*-------------------------------------------------------------------*/
1760static OMX_ERRORTYPE JPEGENC_FillThisBuffer (OMX_HANDLETYPE pComponent,
1761                                     OMX_BUFFERHEADERTYPE* pBuffHead)
1762{
1763    OMX_ERRORTYPE eError        = OMX_ErrorNone;
1764    int err = 0;
1765    OMX_COMPONENTTYPE *pHandle = NULL;
1766    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
1767    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1768
1769    OMX_CHECK_PARAM(pComponent);
1770    OMX_CHECK_PARAM(pBuffHead);
1771
1772    pHandle = (OMX_COMPONENTTYPE *)pComponent;
1773    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1774    pBuffPrivate = pBuffHead->pOutputPortPrivate;
1775
1776    OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGENC_FillThisBuffer function (%p) (state %d -> %d)\n", pBuffHead, pComponentPrivate->nCurState, pComponentPrivate->nToState);
1777
1778    if ( pComponentPrivate->nCurState != OMX_StateExecuting &&
1779        pComponentPrivate->nCurState != OMX_StatePause &&
1780        pComponentPrivate->nCurState != OMX_StateIdle) {
1781        eError= OMX_ErrorIncorrectStateOperation;
1782        goto EXIT;
1783    }
1784
1785    if ( pBuffHead->nSize != sizeof(OMX_BUFFERHEADERTYPE) ) {
1786        eError = OMX_ErrorBadParameter;
1787        OMX_PRBUFFER4(pComponentPrivate->dbg, "JPEG-ENC: buffer header size is not correct\n");
1788        goto EXIT;
1789    }
1790
1791    if (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bEnabled) {
1792            eError = OMX_ErrorIncorrectStateOperation;
1793            goto EXIT;
1794    }
1795
1796    if ( (pBuffHead->nVersion.s.nVersionMajor != 0x1) ||
1797         (pBuffHead->nVersion.s.nVersionMinor != 0x0) ||
1798         (pBuffHead->nVersion.s.nRevision != 0x0) ||
1799         (pBuffHead->nVersion.s.nStep != 0x0) ) {
1800
1801        eError= OMX_ErrorVersionMismatch;
1802        goto EXIT;
1803    }
1804
1805    if (pBuffHead->nOutputPortIndex != 0x1) {
1806            eError = OMX_ErrorBadPortIndex;
1807            goto EXIT;
1808    }
1809
1810    pBuffHead->nFilledLen = 0;
1811#ifdef __PERF_INSTRUMENTATION__
1812        PERF_ReceivedFrame(pComponentPrivate->pPERF,
1813                           pBuffHead->pBuffer,
1814                           0,
1815                           PERF_ModuleHLMM);
1816#endif
1817
1818    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_IN;
1819    pBuffPrivate->bReadFromPipe = OMX_FALSE;
1820
1821
1822    pComponentPrivate->nOutPortIn ++;
1823
1824    OMX_PRBUFFER2(pComponentPrivate->dbg, "FillThisBuffer nOutPortIn %lu\n", pComponentPrivate->nOutPortIn);
1825
1826    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (get empty output buffer) %lu %lu %lu %lu\n",
1827                    pComponentPrivate->nInPortIn,
1828                    pComponentPrivate->nInPortOut,
1829                    pComponentPrivate->nOutPortIn,
1830                    pComponentPrivate->nOutPortOut);
1831
1832    /*  writing the component structure corresponding to output buffer    */
1833    OMX_PRCOMM2(pComponentPrivate->dbg, "FillThisBuffer: write to the queue %p \n", pBuffHead);
1834    err = write (pComponentPrivate->free_outBuf_Q[1], &(pBuffHead), sizeof (pBuffHead));
1835    if (err == -1) {
1836        eError = OMX_ErrorInsufficientResources;
1837        goto EXIT;
1838    }
1839    OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
1840 EXIT:
1841    return eError;
1842}
1843
1844
1845/*-------------------------------------------------------------------*/
1846/**
1847  * OMX_ComponentDeinit() Deinitialize Component
1848  *
1849  * This method will clean all resources in the component (NOT IMPLEMENTED)
1850  *
1851  * @param pHandle        handle for this instance of the component
1852  *
1853  * @retval OMX_NoError              Success, ready to roll
1854  *         OMX_Error_BadParameter   The input parameter pointer is null
1855  **/
1856/*-------------------------------------------------------------------*/
1857
1858static OMX_ERRORTYPE JPEGENC_ComponentDeInit(OMX_HANDLETYPE hComponent)
1859{
1860	OMX_ERRORTYPE eError        = OMX_ErrorNone;
1861	OMX_COMPONENTTYPE *pHandle  = NULL;
1862	JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
1863	struct OMX_TI_Debug dbg;
1864    OMX_DBG_INIT_BASE(dbg);
1865	OMX_CHECK_PARAM(hComponent);
1866	pHandle = (OMX_COMPONENTTYPE *)hComponent;
1867	pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
1868	memcpy(&dbg, &(pComponentPrivate->dbg), sizeof(dbg));
1869	JPEGEnc_Free_ComponentResources(pComponentPrivate);
1870
1871#ifdef RESOURCE_MANAGER_ENABLED
1872	eError = RMProxy_NewSendCommand(pHandle,  RMProxy_FreeResource, OMX_JPEG_Encoder_COMPONENT, 0, 3456, NULL);
1873	if (eError != OMX_ErrorNone) {
1874		OMX_PRMGR4(dbg, "Cannot Free RMProxy Resources\n");
1875	}
1876
1877	eError = RMProxy_DeinitalizeEx(OMX_COMPONENTTYPE_IMAGE);
1878	if ( eError != OMX_ErrorNone )  {
1879		OMX_PRMGR4(dbg, "Error returned from destroy ResourceManagerProxy thread\n");
1880	}
1881
1882#endif
1883
1884EXIT:
1885#if 0
1886#ifdef __PERF_INSTRUMENTATION__
1887	PERF_Boundary(pComponentPrivate->pPERF,
1888		      PERF_BoundaryComplete | PERF_BoundaryCleanup);
1889	PERF_Done(pComponentPrivate->pPERF);
1890#endif
1891#endif
1892	OMX_DBG_CLOSE(dbg);
1893	return eError;
1894}
1895
1896
1897
1898/*-------------------------------------------------------------------*/
1899/**
1900  *  JPEGENC_VerifyTunnelConnection()
1901  *
1902  *
1903  *
1904  *
1905  * @param
1906  * @param
1907  * @param
1908  *
1909  * @retval OMX_NoError              Success, ready to roll
1910  *         OMX_Error_BadParameter   The input parameter pointer is null
1911  **/
1912/*-------------------------------------------------------------------*/
1913/* TODO: Component Name */
1914
1915OMX_ERRORTYPE JPEGENC_VerifyTunnelConnection (JPEG_PORT_TYPE *pPort,
1916                                              OMX_HANDLETYPE hTunneledComp,
1917                                              OMX_PARAM_PORTDEFINITIONTYPE* pPortDef,
1918                                              struct OMX_TI_Debug *dbg)
1919{
1920   OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
1921   OMX_ERRORTYPE eError = OMX_ErrorNone;
1922   /*sPortDef.format.image.eCompressionFormat = OMX_IMAGE_CodingJPEG;*/
1923
1924   OMX_PRINT1(*dbg, "Inside JPEG JPEGENC_VerifyTunnelConnection..\n");
1925   sPortDef.nSize = sizeof(OMX_PARAM_PORTDEFINITIONTYPE);
1926   sPortDef.nVersion.s.nVersionMajor = 0x1;
1927   sPortDef.nVersion.s.nVersionMinor = 0x0;
1928   sPortDef.nPortIndex = pPort->nTunnelPort;
1929
1930   eError = OMX_GetParameter(hTunneledComp, OMX_IndexParamPortDefinition, &sPortDef);
1931   if (eError != OMX_ErrorNone) {
1932       OMX_ERROR4(*dbg, "error 1\n");
1933       return eError;
1934   }
1935
1936   switch(pPort->pPortDef->eDomain) {
1937       case OMX_PortDomainOther:
1938           if (sPortDef.format.other.eFormat!= pPortDef->format.other.eFormat) {
1939                pPort->hTunnelComponent = 0;
1940                pPort->nTunnelPort      = 0;
1941                return OMX_ErrorPortsNotCompatible;
1942            }
1943            break;
1944        case OMX_PortDomainAudio:
1945            if (sPortDef.format.audio.eEncoding != pPortDef->format.audio.eEncoding) {
1946                pPort->hTunnelComponent = 0;
1947                pPort->nTunnelPort      = 0;
1948                return OMX_ErrorPortsNotCompatible;
1949            }
1950            break;
1951        case OMX_PortDomainVideo:
1952            if (sPortDef.format.video.eCompressionFormat != pPortDef->format.video.eCompressionFormat) {
1953                pPort->hTunnelComponent = 0;
1954                pPort->nTunnelPort      = 0;
1955                return OMX_ErrorPortsNotCompatible;
1956            }
1957            break;
1958        case OMX_PortDomainImage:
1959            if (sPortDef.format.image.eCompressionFormat != pPortDef->format.image.eCompressionFormat) {
1960                pPort->hTunnelComponent = 0;
1961                pPort->nTunnelPort      = 0;
1962                return OMX_ErrorPortsNotCompatible;
1963            }
1964            break;
1965        default:
1966            pPort->hTunnelComponent = 0;
1967            pPort->nTunnelPort      = 0;
1968            return OMX_ErrorPortsNotCompatible; /* Our current port is not set up correctly */
1969   }
1970    return eError;
1971}
1972
1973/*-------------------------------------------------------------------*/
1974/**
1975  *  JPEGENC_ComponentTunnelRequest() Sets application callbacks to the component
1976  *
1977  * This method will update application callbacks
1978  * the application.
1979  *
1980  * @param hComp         handle for this instance of the component
1981  * @param nPortInput
1982  * @param hTunneledComp
1983  * @param nTunneledPort
1984  * @param eDir
1985  * @param pCallbacks
1986  *
1987  * @retval OMX_NoError              Success, ready to roll
1988  *         OMX_Error_BadParameter   The input parameter pointer is null
1989  *
1990static OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest (OMX_HANDLETYPE hComp,
1991                                             OMX_U32 nPortInput,
1992                                             OMX_HANDLETYPE hTunneledComp,
1993                                             OMX_U32 nTunneledPort,
1994                                             OMX_DIRTYPE eDir,
1995                                             OMX_CALLBACKTYPE* pCallbacks)
1996
1997-------------------------------------------------------------------*/
1998
1999OMX_ERRORTYPE JPEGENC_ComponentTunnelRequest(OMX_IN  OMX_HANDLETYPE hComponent,
2000                                     OMX_IN  OMX_U32 nPort,
2001                                     OMX_IN  OMX_HANDLETYPE hTunneledComp,
2002                                     OMX_IN  OMX_U32 nTunneledPort,
2003                                     OMX_INOUT  OMX_TUNNELSETUPTYPE* pTunnelSetup)
2004{
2005    OMX_ERRORTYPE eError            = OMX_ErrorNone;
2006    OMX_COMPONENTTYPE *pHandle  = NULL;
2007    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
2008    OMX_PARAM_BUFFERSUPPLIERTYPE sBufferSupplier;
2009    JPEG_PORT_TYPE *pPortType           = NULL;
2010
2011    OMX_CHECK_PARAM(hComponent);
2012
2013
2014    pHandle = (OMX_COMPONENTTYPE *)hComponent;
2015    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)pHandle->pComponentPrivate;
2016    pPortType = pComponentPrivate->pCompPort[nPort];
2017
2018    OMX_PRBUFFER2(pComponentPrivate->dbg, "nPort = %d nTunneledPort = %d\n",(int)nPort, (int)nTunneledPort);
2019
2020    if (pTunnelSetup == NULL || hTunneledComp == 0)
2021    {
2022        /* cancel previous tunnel */
2023        pPortType->hTunnelComponent = 0;
2024        pPortType->nTunnelPort = 0;
2025        pPortType->pBufSupplier = OMX_BufferSupplyUnspecified;
2026        goto EXIT;
2027    }
2028
2029    if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir != OMX_DirInput &&
2030        pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->eDir != OMX_DirOutput) {
2031        return OMX_ErrorBadParameter;
2032    }
2033
2034     /* Check if the other component is developed by TI */
2035     if(IsTIOMXComponent(hTunneledComp) != OMX_TRUE)
2036    {
2037       eError = OMX_ErrorTunnelingUnsupported;
2038       goto EXIT;
2039    }
2040
2041    pPortType->hTunnelComponent = hTunneledComp;
2042    pPortType->nTunnelPort = nTunneledPort;
2043    OMX_PRCOMM2(pComponentPrivate->dbg, "PP comp = %x, tunneled comp = %x\n",(int)hComponent, (int)pPortType->hTunnelComponent);
2044
2045    if (pPortType->pPortDef->eDir == OMX_DirOutput) {
2046        /* Component is the output (source of data) */
2047        pTunnelSetup->eSupplier = pPortType->pBufSupplier;
2048    }
2049    else {
2050/*      Component is the input (sink of data) */
2051        eError = JPEGENC_VerifyTunnelConnection(pPortType,
2052                                               hTunneledComp,
2053						pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef, &(pComponentPrivate->dbg));
2054        if (OMX_ErrorNone != eError) {
2055            OMX_PRCOMM4(pComponentPrivate->dbg, "PP JPEGENC_VerifyTunnelConnection inside JPEGfailed\n");
2056            /* Invalid connection formats. Return eError */
2057            return OMX_ErrorPortsNotCompatible;
2058        }
2059
2060        /* If specified obey output port's preferences. Otherwise choose output */
2061        pPortType->pBufSupplier = pTunnelSetup->eSupplier;
2062        if (OMX_BufferSupplyUnspecified == pPortType->pBufSupplier) {
2063            pPortType->pBufSupplier = pTunnelSetup->eSupplier = OMX_BufferSupplyOutput;
2064        }
2065
2066        /* Tell the output port who the supplier is */
2067        sBufferSupplier.nSize = sizeof(sBufferSupplier);
2068        sBufferSupplier.nVersion.s.nVersionMajor = 0x1;
2069        sBufferSupplier.nVersion.s.nVersionMinor = 0x0;
2070        sBufferSupplier.nPortIndex = nTunneledPort;
2071        sBufferSupplier.eBufferSupplier = pPortType->pBufSupplier;
2072        eError = OMX_SetParameter(hTunneledComp, OMX_IndexParamCompBufferSupplier, &sBufferSupplier);
2073
2074        eError = OMX_GetParameter(hTunneledComp, OMX_IndexParamCompBufferSupplier, &sBufferSupplier);
2075        if (sBufferSupplier.eBufferSupplier != pPortType->pBufSupplier) {
2076            OMX_PRCOMM4(pComponentPrivate->dbg, "- JPEGENC_SetParameter: OMX_IndexParamCompBufferSupplier failed to change setting\n" );
2077            return OMX_ErrorUndefined;
2078        }
2079    }
2080
2081EXIT:
2082    return eError;
2083}
2084
2085
2086
2087/*-------------------------------------------------------------------*/
2088/**
2089  *  JPEGENC_AllocateBuffer()
2090  *
2091  *
2092  *
2093  *
2094  * @param
2095  * @param
2096  * @param
2097  *
2098  * @retval OMX_NoError              Success, ready to roll
2099  *         OMX_Error_BadParameter   The input parameter pointer is null
2100  **/
2101/*-------------------------------------------------------------------*/
2102OMX_ERRORTYPE JPEGENC_AllocateBuffer(OMX_IN OMX_HANDLETYPE hComponent,
2103                             OMX_INOUT OMX_BUFFERHEADERTYPE** ppBuffHead,
2104                             OMX_IN OMX_U32 nPortIndex,
2105                             OMX_IN OMX_PTR pAppPrivate,
2106                             OMX_IN OMX_U32 nSizeBytes)
2107{
2108    OMX_COMPONENTTYPE            *pHandle           = NULL;
2109    JPEGENC_COMPONENT_PRIVATE    *pComponentPrivate = NULL;
2110    OMX_PARAM_PORTDEFINITIONTYPE *pPortDef          = NULL;
2111    OMX_BUFFERHEADERTYPE         *pBufferHdr        = NULL;
2112    OMX_U32 nBufferCount = -1;
2113    OMX_ERRORTYPE eError = OMX_ErrorNone;
2114    OMX_U8* pTemp = NULL;
2115
2116    pHandle           = (OMX_COMPONENTTYPE *) hComponent;
2117    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
2118    pPortDef          = pComponentPrivate->pCompPort[nPortIndex]->pPortDef;
2119    nBufferCount      = pComponentPrivate->pCompPort[nPortIndex]->nBuffCount;
2120
2121    if (nBufferCount >= pPortDef->nBufferCountActual) {
2122        eError = OMX_ErrorInsufficientResources;
2123        OMX_PRBUFFER4(pComponentPrivate->dbg, " try to allocate more buffers that the port supports\n");
2124        goto EXIT;
2125    }
2126
2127    if(nPortIndex == JPEGENC_INP_PORT) {
2128        OMX_MALLOC(pBufferHdr, sizeof(OMX_BUFFERHEADERTYPE));
2129        OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Input pBufferPrivate = %p\n",pBufferHdr);
2130
2131        OMX_CONF_INIT_STRUCT(pBufferHdr, OMX_BUFFERHEADERTYPE);
2132        pBufferHdr->nOutputPortIndex = OMX_NOPORT;
2133        pBufferHdr->nInputPortIndex = nPortIndex;
2134        pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2135        pBufferHdr->nVersion = pPortDef->nVersion;
2136        pBufferHdr->pAppPrivate = pAppPrivate;
2137        pBufferHdr->nAllocLen = nSizeBytes;
2138        pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_TRUE;
2139        pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBufferHdr;
2140        *ppBuffHead = pBufferHdr;
2141    }
2142    else if(nPortIndex == JPEGENC_OUT_PORT) {
2143        OMX_MALLOC(pBufferHdr, sizeof(OMX_BUFFERHEADERTYPE));
2144        OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Output pBufferPrivate[0] = %p\n", pBufferHdr);
2145        OMX_CONF_INIT_STRUCT(pBufferHdr, OMX_BUFFERHEADERTYPE);
2146        pBufferHdr->nInputPortIndex = OMX_NOPORT;
2147        pBufferHdr->nOutputPortIndex = nPortIndex;
2148        pBufferHdr->nSize = sizeof(OMX_BUFFERHEADERTYPE);
2149        pBufferHdr->nVersion = pPortDef->nVersion;
2150        pBufferHdr->pAppPrivate = pAppPrivate;
2151        pBufferHdr->nAllocLen = nSizeBytes;
2152        pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->bAllocByComponent = OMX_TRUE;
2153        pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[nBufferCount]->pBufferHdr = pBufferHdr;
2154        *ppBuffHead = pBufferHdr;
2155    }
2156    else {
2157        eError = OMX_ErrorBadPortIndex;
2158        goto EXIT;
2159    }
2160
2161    OMX_MALLOC(pBufferHdr->pBuffer, nSizeBytes+256);
2162
2163#ifdef __PERF_INSTRUMENTATION__
2164        PERF_ReceivedFrame(pComponentPrivate->pPERF,
2165                           pBufferHdr->pBuffer,
2166                           pBufferHdr->nFilledLen,
2167                           PERF_ModuleMemory);
2168#endif
2169
2170    if (!pBufferHdr->pBuffer) {
2171        OMX_TRACE4(pComponentPrivate->dbg, "Error: Malloc failed\n");
2172        eError = OMX_ErrorInsufficientResources;
2173        goto EXIT;
2174    }
2175
2176
2177    pTemp = (OMX_U8 *)(pBufferHdr->pBuffer);
2178    pTemp += 128;
2179    pBufferHdr->pBuffer = pTemp;
2180    OMX_PRBUFFER2(pComponentPrivate->dbg, "Allocate Buffer Input pBufferPrivate[0]-pBuffer = %p\n",pBufferHdr->pBuffer);
2181
2182
2183    if (nPortIndex == JPEGENC_INP_PORT) {
2184        pBufferHdr->pInputPortPrivate  = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
2185    }
2186    else {
2187        pBufferHdr->pOutputPortPrivate = pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount];
2188    }
2189
2190    /** The following 6 lines need to be changed **/
2191    if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent != NULL) {
2192        pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
2193    }
2194    else {
2195        pComponentPrivate->pCompPort[nPortIndex]->pBufferPrivate[nBufferCount]->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2196    }
2197
2198
2199   pComponentPrivate->pCompPort[nPortIndex]->nBuffCount++;
2200   OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG-ENC: actual %d ask %lu\n",
2201        pComponentPrivate->pCompPort[nPortIndex]->nBuffCount,
2202        pPortDef->nBufferCountActual);
2203
2204   if (pComponentPrivate->pCompPort[nPortIndex]->nBuffCount == pPortDef->nBufferCountActual) {
2205       pPortDef->bPopulated = OMX_TRUE;
2206
2207       pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
2208       pthread_cond_signal(&pComponentPrivate->populate_cond);
2209       pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
2210
2211       JPEGENC_InitBufferFlagTrack(pComponentPrivate, nPortIndex);
2212       OMX_PRBUFFER2(pComponentPrivate->dbg, " Port [%d] Populated!\n", (int)(nPortIndex));
2213   }
2214
2215EXIT:
2216    OMX_PRINT1(pComponentPrivate->dbg, "Exiting pHandle = %d\n", (int)pHandle);
2217    return eError;
2218}
2219
2220
2221static void JPEGENC_InitBufferFlagTrack(
2222    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate,
2223    OMX_U32 nPortIndex)
2224
2225{
2226    JPEG_PORT_TYPE *pPortType = NULL;
2227    int i;
2228
2229    pPortType = pComponentPrivate->pCompPort[nPortIndex];
2230
2231    /* assume  pPortType->pPortDef->nBufferCountActual <= NUM_OF_BUFFERSJPEG */
2232    for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
2233        pPortType->sBufferFlagTrack[i].flag = 0;
2234        pPortType->sBufferFlagTrack[i].buffer_id = 0xFFFFFFFF;
2235        pPortType->sBufferMarkTrack[i].buffer_id = 0xFFFFFFFF;
2236        pPortType->sBufferMarkTrack[i].pMarkData = NULL;
2237    }
2238}
2239
2240#ifdef KHRONOS_1_1
2241
2242static OMX_ERRORTYPE ComponentRoleEnum(
2243        OMX_IN OMX_HANDLETYPE hComponent,
2244                OMX_OUT OMX_U8 *cRole,
2245                OMX_IN OMX_U32 nIndex)
2246{
2247    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate;
2248
2249    OMX_ERRORTYPE eError = OMX_ErrorNone;
2250    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)(((OMX_COMPONENTTYPE*)hComponent)->pComponentPrivate);
2251
2252    if(nIndex == 0){
2253      /* memcpy(cRole, &(pComponentPrivate->componentRole.cRole), sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE - 1); */
2254      strncpy((char *)cRole, (char *)pComponentPrivate->componentRole.cRole, sizeof(OMX_U8) * OMX_MAX_STRINGNAME_SIZE - 1);
2255    }
2256    else {
2257      eError = OMX_ErrorNoMore;
2258        }
2259
2260    return eError;
2261};
2262#endif
2263
2264
2265/*-------------------------------------------------------------------*/
2266/**
2267  * JPEGENC_GetExtensionIndex()
2268  *
2269  * Return the index corresponding to the string.
2270  *
2271  * @retval OMX_ErrorNone                    Successful operation.
2272  *             OMX_ErrorUnsupportedIndex  If no index corresponding to the string is found
2273  **/
2274/*-------------------------------------------------------------------*/
2275OMX_ERRORTYPE JPEGENC_GetExtensionIndex(OMX_IN OMX_HANDLETYPE hComponent, OMX_IN OMX_STRING cParameterName, OMX_OUT OMX_INDEXTYPE* pIndexType)
2276{
2277    OMX_U16 nIndex;
2278    OMX_ERRORTYPE eError = OMX_ErrorUndefined;
2279    JPEGENC_CUSTOM_PARAM_DEFINITION sJpegEncCustomParams[] = {
2280    {"OMX.TI.JPEG.encoder.Config.HuffmanTable", OMX_IndexCustomHuffmanTable},
2281    {"OMX.TI.JPEG.encoder.Config.CommentFlag", OMX_IndexCustomCommentFlag},
2282    {"OMX.TI.JPEG.encoder.Config.CommentString", OMX_IndexCustomCommentString},
2283    {"OMX.TI.JPEG.encoder.Config.InputFrameWidth", OMX_IndexCustomInputFrameWidth},
2284    {"OMX.TI.JPEG.encoder.Config.InputFrameHeight", OMX_IndexCustomInputFrameHeight},
2285    {"OMX.TI.JPEG.encoder.Config.APP0", OMX_IndexCustomAPP0},
2286    {"OMX.TI.JPEG.encoder.Config.APP1", OMX_IndexCustomAPP1},
2287    {"OMX.TI.JPEG.encoder.Config.APP5", OMX_IndexCustomAPP5},
2288    {"OMX.TI.JPEG.encoder.Config.APP13", OMX_IndexCustomAPP13},
2289    {"OMX.TI.JPEG.encoder.Config.QFactor", OMX_IndexCustomQFactor},
2290    {"OMX.TI.JPEG.encoder.Config.DRI", OMX_IndexCustomDRI},
2291    {"OMX.TI.JPEG.encoder.Config.Debug", OMX_IndexCustomDebug},
2292    {"",0x0}
2293    };
2294
2295    /* Check parameter validity */
2296    OMX_CHECK_PARAM(hComponent);
2297    OMX_CHECK_PARAM(pIndexType);
2298    *pIndexType = OMX_IndexMax;
2299
2300    for (nIndex = 0; strlen((const char*)sJpegEncCustomParams[nIndex].cCustomParamName); nIndex++){
2301        if (!strcmp((const char*)cParameterName, (const char*)(&(sJpegEncCustomParams[nIndex].cCustomParamName)))){
2302            *pIndexType = sJpegEncCustomParams[nIndex].nCustomParamIndex;
2303            eError = OMX_ErrorNone;
2304            break;
2305        }
2306    }
2307
2308    if(*pIndexType == OMX_IndexMax){
2309         eError = OMX_ErrorUnsupportedIndex;
2310    }
2311
2312EXIT:
2313    return eError;
2314}
2315
2316
2317
2318