OMX_JpegEnc_Utils.c revision 644f5ad9b052d954c568285458ee5cad6aec20c1
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_JpegEnc_Utils.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
54#ifdef UNDER_CE
55#include <windows.h>
56#include <oaf_osal.h>
57#include <omx_core.h>
58#else
59#include <unistd.h>
60#include <sys/types.h>
61#include <malloc.h>
62#include <memory.h>
63#include <sys/time.h>
64#include <sys/types.h>
65#include <sys/stat.h>
66#endif
67
68#include <dbapi.h>
69#include <string.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <fcntl.h>
73
74
75#include <OMX_Types.h>
76#include <OMX_Component.h>
77#include <OMX_Core.h>
78#include <OMX_Index.h>
79#include <OMX_Image.h>
80#include <OMX_Audio.h>
81#include <OMX_Video.h>
82#include <OMX_IVCommon.h>
83#include <OMX_Other.h>
84
85#include <usn.h>
86
87/*------- Program Header Files ----------------------------------------*/
88
89#include "OMX_JpegEnc_Utils.h"
90#include "OMX_JpegEnc_CustomCmd.h"
91
92#ifdef RESOURCE_MANAGER_ENABLED
93    #include <ResourceManagerProxyAPI.h>
94#endif
95
96#define JPEGENC_TIMEOUT 0xFFFFFFFE
97
98static OMX_ERRORTYPE HandleJpegEncInternalFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1);
99
100#ifdef RESOURCE_MANAGER_ENABLED
101void ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData);
102#endif
103
104#ifdef UNDER_CE
105void sleep(DWORD Duration)
106{
107    Sleep(Duration);
108}
109#endif
110
111/*--------function prototypes ---------------------------------*/
112OMX_ERRORTYPE JpegEncLCML_Callback (TUsnCodecEvent event,
113                                    void * args [10]);
114
115/*-------- Function Implementations ---------------------------------*/
116OMX_ERRORTYPE GetJpegEncLCMLHandle(OMX_HANDLETYPE pComponent)
117{
118    OMX_ERRORTYPE eError = OMX_ErrorNone;
119#ifndef UNDER_CE
120    OMX_HANDLETYPE LCML_pHandle;
121
122    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
123    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
124    fpo fpGetHandle ;
125    void *handle = NULL;
126    char *error =NULL;
127
128    OMX_PRDSP1(pComponentPrivate->dbg, "Inside GetLCMLHandle function\n");
129
130    handle = dlopen("libLCML.so", RTLD_LAZY);
131    if ( !handle ) {
132        fputs(dlerror(), stderr);
133        eError = OMX_ErrorComponentNotFound;
134        goto EXIT;
135    }
136
137    fpGetHandle = dlsym (handle, "GetHandle");
138    if ( (error = (char *)dlerror()) != NULL ) {
139        fputs(error, stderr);
140        eError = OMX_ErrorInvalidComponent;
141        goto EXIT;
142    }
143
144    /*  calling gethandle and passing phandle to b filled   */
145    eError = (*fpGetHandle)(&LCML_pHandle);
146
147    if ( eError != OMX_ErrorNone ) {
148        eError = OMX_ErrorUndefined;
149        OMX_PRDSP4(pComponentPrivate->dbg, "eError != OMX_ErrorNone...\n");
150        goto EXIT;
151    }
152
153    OMX_PRDSP2(pComponentPrivate->dbg, "Received LCML Handle\n");
154
155    pComponentPrivate->pDllHandle = handle;
156    pComponentPrivate->pLCML = (void *)LCML_pHandle;
157    pComponentPrivate->pLCML->pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pComponentPrivate;
158#else
159    typedef OMX_ERRORTYPE (*LPFNDLLFUNC1)(OMX_HANDLETYPE);
160    LPFNDLLFUNC1 fpGetHandle1;
161    OMX_HANDLETYPE LCML_pHandle = NULL;
162    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)pComponent;
163    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
164    HINSTANCE hDLL;
165
166    hDLL = LoadLibraryEx(TEXT("OAF_BML.dll"), NULL, 0);
167    if (hDLL == NULL)
168    {
169        eError = OMX_ErrorComponentNotFound;
170        goto EXIT;
171    }
172
173    fpGetHandle1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,TEXT("GetHandle"));
174    if (!fpGetHandle1)
175    {
176
177        FreeLibrary(hDLL);
178        eError = OMX_ErrorComponentNotFound;
179        goto EXIT;
180    }
181
182
183    eError = fpGetHandle1(&LCML_pHandle);
184    if(eError != OMX_ErrorNone) {
185
186        eError = OMX_ErrorUndefined;
187        LCML_pHandle = NULL;
188          goto EXIT;
189    }
190
191    (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML = (LCML_DSP_INTERFACE *)LCML_pHandle;
192    pComponentPrivate->pLCML->pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pComponentPrivate;
193
194#endif
195    OMX_PRINT1(pComponentPrivate->dbg, "Exit\n");
196    EXIT:
197    return eError;
198}
199
200
201/*-----------------------------------------------------------------------------*/
202/**
203  * Disable Port()
204  *
205  * Called by component thread, handles commands sent by the app.
206  *
207  * @param
208  *
209  * @retval OMX_ErrorNone                  success, ready to roll
210  *
211  **/
212/*-----------------------------------------------------------------------------*/
213OMX_ERRORTYPE JpegEncDisablePort (JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_U32 nParam1)
214{
215    OMX_ERRORTYPE eError = OMX_ErrorNone;
216    OMX_CHECK_PARAM(pComponentPrivate);
217
218    OMX_PRINT1(pComponentPrivate->dbg, "Inside DisablePort function\n");
219
220    OMX_PRBUFFER1(pComponentPrivate->dbg, "Inside disable port (%lu) %lu %lu %lu %lu\n",
221            nParam1,
222            pComponentPrivate->nInPortIn,
223            pComponentPrivate->nInPortOut,
224            pComponentPrivate->nOutPortIn,
225            pComponentPrivate->nOutPortOut);
226
227   if (pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StatePause) {
228	if ((nParam1 == JPEGENC_INP_PORT) || (nParam1 == JPEGENC_OUT_PORT) || ((int)nParam1 == -1)) {
229           eError = HandleJpegEncInternalFlush(pComponentPrivate, nParam1);
230       }
231   }
232
233EXIT:
234    OMX_PRINT1(pComponentPrivate->dbg, "Exit form JPEGEnc Disable Port eError is = %x\n",eError);
235    return eError;
236}
237
238
239
240/*-----------------------------------------------------------------------------*/
241/**
242  * Enable Port()
243  *
244  * Called by component thread, handles commands sent by the app.
245  *
246  * @param
247  *
248  * @retval OMX_ErrorNone                  success, ready to roll
249  *
250  **/
251/*-----------------------------------------------------------------------------*/
252
253OMX_ERRORTYPE JpegEncEnablePort (JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_U32 nParam1)
254{
255    OMX_ERRORTYPE eError = OMX_ErrorNone;
256    OMX_CHECK_PARAM(pComponentPrivate);
257
258    OMX_PRINT1(pComponentPrivate->dbg, "Inside EnablePort function\n");
259
260
261    if (nParam1 == 0) {
262        if (pComponentPrivate->nCurState != OMX_StateLoaded) {
263        pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
264        while (!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated) {
265            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
266            pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
267        }
268        }
269        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
270        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
271                                                pComponentPrivate->pHandle->pApplicationPrivate,
272                                                OMX_EventCmdComplete,
273                                                OMX_CommandPortEnable,
274                                                JPEGENC_INP_PORT,
275                                                NULL);
276    } else if (nParam1 == 1) {
277        if (pComponentPrivate->nCurState != OMX_StateLoaded) {
278        pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
279        while (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated) {
280            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
281            pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
282        }
283        }
284        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
285        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
286                                                pComponentPrivate->pHandle->pApplicationPrivate,
287                                                OMX_EventCmdComplete,
288                                                OMX_CommandPortEnable,
289                                                JPEGENC_OUT_PORT,
290                                                NULL);
291    } else if ((int)nParam1 == -1) {
292        if (pComponentPrivate->nCurState != OMX_StateLoaded) {
293        pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
294        while ((!pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->bPopulated) ||
295               (!pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->bPopulated)) {
296            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
297            pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
298        }
299        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
300        }
301        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
302                                                pComponentPrivate->pHandle->pApplicationPrivate,
303                                                OMX_EventCmdComplete,
304                                                OMX_CommandPortEnable,
305                                                JPEGENC_INP_PORT,
306                                                NULL);
307
308        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
309                                                pComponentPrivate->pHandle->pApplicationPrivate,
310                                                OMX_EventCmdComplete,
311                                                OMX_CommandPortEnable,
312                                                JPEGENC_OUT_PORT,
313                                                NULL);
314    }
315EXIT:
316    return eError;
317}
318
319
320
321/*-------------------------------------------------------------------*/
322/**
323  *  JPEGEnc_Start_ComponentThread() Starts Component Thread
324  *
325  *  Creates data pipes, commmand pipes and initializes Component thread
326  *
327  * @param pComponent    handle for this instance of the component
328  *
329  * @retval OMX_NoError              Success, ready to roll
330  * @retval OMX_ErrorInsufficientResources    Insiffucient Resources
331  *
332  **/
333/*-------------------------------------------------------------------*/
334OMX_ERRORTYPE JPEGEnc_Start_ComponentThread(OMX_HANDLETYPE pComponent)
335{
336    OMX_ERRORTYPE eError = OMX_ErrorNone;
337    OMX_COMPONENTTYPE *pHandle = NULL;
338    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
339
340
341    OMX_CHECK_PARAM(pComponent);
342    pHandle = (OMX_COMPONENTTYPE *)pComponent;
343    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
344    OMX_PRINT1(pComponentPrivate->dbg, "Inside JPEGEnc_Start_ComponentThread function\n");
345
346    /* create the pipe used to maintain free output buffers*/
347    eError = pipe (pComponentPrivate->free_outBuf_Q);
348    if ( eError ) {
349        eError = OMX_ErrorInsufficientResources;
350        goto EXIT;
351    }
352
353    /* create the pipe used to maintain filled input buffers*/
354    eError = pipe (pComponentPrivate->filled_inpBuf_Q);
355    if ( eError ) {
356        eError = OMX_ErrorInsufficientResources;
357        goto EXIT;
358    }
359
360    /* create the pipe used to send commands to the thread */
361    eError = pipe (pComponentPrivate->nCmdPipe);
362    if ( eError ) {
363        eError = OMX_ErrorInsufficientResources;
364        goto EXIT;
365    }
366
367    /* create the pipe used to send commands to the thread */
368    eError = pipe (pComponentPrivate->nCmdDataPipe);
369    if ( eError ) {
370        eError = OMX_ErrorInsufficientResources;
371        goto EXIT;
372    }
373
374    /*  No buffers have been sent to dsp    */
375    pComponentPrivate->nNum_dspBuf = 0;
376
377    /* Create the Component Thread */
378    eError = pthread_create (&(pComponentPrivate->ComponentThread), NULL,
379                             OMX_JpegEnc_Thread, pComponent);
380
381
382    if ( eError || !pComponentPrivate->ComponentThread ) {
383        eError = OMX_ErrorInsufficientResources;
384        goto EXIT;
385    }
386
387#ifdef __PERF_INSTRUMENTATION__
388        PERF_ThreadCreated(pComponentPrivate->pPERF,
389                           pComponentPrivate->ComponentThread,
390                           PERF_FOURCC('J','P','E','T'));
391
392#endif
393
394    EXIT:
395    return eError;
396}
397
398
399
400/* -------------------------------------------------------------------------- */
401/**
402* @JPEGEnc_Free_ComponentResources() This function is called by the component during
403* de-init to close component thread, Command pipe, data pipe & LCML pipe.
404*
405* @param pComponent  handle for this instance of the component
406*
407* @pre
408*
409* @post
410*
411* @return none
412*/
413/* -------------------------------------------------------------------------- */
414OMX_ERRORTYPE JPEGEnc_Free_ComponentResources(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
415{
416    OMX_ERRORTYPE eError = OMX_ErrorNone;
417    OMX_ERRORTYPE threadError = OMX_ErrorNone;
418    OMX_ERRORTYPE err = OMX_ErrorNone;
419    int pipeError = 0;
420    int pthreadError = 0;
421    OMX_COMMANDTYPE eCmd = OMX_CustomCommandStopThread;
422    OMX_U32 nParam = 0;
423    struct OMX_TI_Debug dbg;
424
425#ifdef __PERF_INSTRUMENTATION__
426        PERF_Boundary(pComponentPrivate->pPERF,
427                      PERF_BoundaryStart | PERF_BoundaryCleanup);
428#endif
429
430    OMX_DBG_INIT_BASE(dbg);
431    OMX_CHECK_PARAM(pComponentPrivate);
432
433    if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
434    	LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
435    	dlclose(pComponentPrivate->pDllHandle);
436    	pComponentPrivate->pLCML = NULL;
437    	pComponentPrivate->isLCMLActive = 0;
438    }
439
440    pipeError = write(pComponentPrivate->nCmdPipe[1], &eCmd, sizeof(eCmd));
441    if (pipeError == -1) {
442        eError = OMX_ErrorHardware;
443        OMX_PRCOMM4(dbg, "Error while writing to nCmdPipe\n");
444    }
445
446    pipeError = write(pComponentPrivate->nCmdDataPipe[1], &nParam, sizeof(nParam));
447    if (pipeError == -1) {
448        eError = OMX_ErrorHardware;
449        OMX_PRCOMM4(dbg, "Error while writing to nCmdPipe\n");
450    }
451
452    pthreadError = pthread_join (pComponentPrivate->ComponentThread,
453                                 (void*)&threadError);
454    if ( 0 != pthreadError ) {
455        eError = OMX_ErrorHardware;
456        OMX_TRACE4(dbg, "Error while closing Component Thread\n");
457    }
458
459    if ( OMX_ErrorNone != threadError && OMX_ErrorNone != eError ) {
460        eError = OMX_ErrorInsufficientResources;
461        OMX_TRACE4(dbg, "Error while closing Component Thread\n");
462    }
463
464    /*  close the data pipe handles */
465
466    err = close (pComponentPrivate->free_outBuf_Q[0]);
467    if ( 0 != err && OMX_ErrorNone == eError ) {
468        eError = OMX_ErrorHardware;
469        OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
470    }
471
472    err = close (pComponentPrivate->filled_inpBuf_Q[0]);
473    if ( 0 != err && OMX_ErrorNone == eError ) {
474        eError = OMX_ErrorHardware;
475        OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
476    }
477
478    err = close (pComponentPrivate->free_outBuf_Q[1]);
479    if ( 0 != err && OMX_ErrorNone == eError ) {
480        eError = OMX_ErrorHardware;
481        OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
482    }
483
484    err = close (pComponentPrivate->filled_inpBuf_Q[1]);
485    if ( 0 != err && OMX_ErrorNone == eError ) {
486        eError = OMX_ErrorHardware;
487        OMX_PRCOMM4(dbg, "Error while closing data pipe\n");
488    }
489
490    /*  close the command pipe handles  */
491    err = close (pComponentPrivate->nCmdPipe[0]);
492    if ( 0 != err && OMX_ErrorNone == eError ) {
493        eError = OMX_ErrorHardware;
494        OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
495    }
496
497    err = close (pComponentPrivate->nCmdPipe[1]);
498    if ( 0 != err && OMX_ErrorNone == eError ) {
499        eError = OMX_ErrorHardware;
500        OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
501    }
502
503    /*  close the command data pipe handles */
504    err = close (pComponentPrivate->nCmdDataPipe[0]);
505    if ( 0 != err && OMX_ErrorNone == eError ) {
506        eError = OMX_ErrorHardware;
507        OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
508    }
509
510    err = close (pComponentPrivate->nCmdDataPipe[1]);
511    if ( 0 != err && OMX_ErrorNone == eError ) {
512        eError = OMX_ErrorHardware;
513        OMX_PRCOMM4(dbg, "Error while closing cmd pipe\n");
514    }
515
516    pthread_mutex_destroy(&pComponentPrivate->jpege_mutex);
517    pthread_cond_destroy(&pComponentPrivate->stop_cond);
518    pthread_cond_destroy(&pComponentPrivate->flush_cond);
519    /* pthread_cond_destroy(&pComponentPrivate->control_cond); */
520
521    pthread_mutex_destroy(&pComponentPrivate->jpege_mutex_app);
522    pthread_cond_destroy(&pComponentPrivate->populate_cond);
523    pthread_cond_destroy(&pComponentPrivate->unpopulate_cond);
524#ifdef __PERF_INSTRUMENTATION__
525    PERF_Boundary(pComponentPrivate->pPERF,
526    		PERF_BoundaryComplete | PERF_BoundaryCleanup);
527    PERF_Done(pComponentPrivate->pPERF);
528#endif
529
530    /* LinkedList_DisplayAll (&AllocList); */
531    OMX_FREEALL();
532    LinkedList_Destroy(&AllocList);
533
534EXIT:
535    OMX_PRINT1(dbg, "Exiting JPEG FreeComponentresources\n");
536    return eError;
537}
538
539OMX_ERRORTYPE Fill_JpegEncLCMLInitParams(LCML_DSP *lcml_dsp, OMX_U16 arr[], OMX_HANDLETYPE pComponent)
540{
541    OMX_ERRORTYPE eError = OMX_ErrorNone;
542    OMX_COMPONENTTYPE *pHandle = NULL;
543    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
544    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
545    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
546    int outbufsize = 0;
547
548    OMX_U16 *ptCreateString = (OMX_U16*)arr;
549    OMX_U32 *ptCreateStringPPLIB = (OMX_U32*)arr;
550
551    OMX_CHECK_PARAM(pComponent);
552    pHandle = (OMX_COMPONENTTYPE *)pComponent;
553    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
554    OMX_PRINT1(pComponentPrivate->dbg, "Initialize Params\n");
555    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
556    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
557    outbufsize = pPortDefOut->nBufferSize;
558
559    lcml_dsp->In_BufInfo.nBuffers = NUM_OF_BUFFERSJPEG;
560    lcml_dsp->In_BufInfo.nSize = pPortDefIn->nBufferSize;
561    lcml_dsp->In_BufInfo.DataTrMethod = DMM_METHOD;
562
563    lcml_dsp->Out_BufInfo.nBuffers = NUM_OF_BUFFERSJPEG;
564    lcml_dsp->Out_BufInfo.nSize = outbufsize;
565    lcml_dsp->Out_BufInfo.DataTrMethod = DMM_METHOD;
566
567    lcml_dsp->NodeInfo.nNumOfDLLs = OMX_JPEGENC_NUM_DLLS;
568    lcml_dsp->NodeInfo.AllUUIDs[0].uuid = (struct DSP_UUID * )&JPEGESOCKET_TI_UUID;
569    strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[0].DllName,JPEG_ENC_NODE_DLL);
570    lcml_dsp->NodeInfo.AllUUIDs[0].eDllType = DLL_NODEOBJECT;
571
572    lcml_dsp->NodeInfo.AllUUIDs[1].uuid = (struct DSP_UUID * )&JPEGESOCKET_TI_UUID;
573    strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[1].DllName,JPEG_ENC_NODE_DLL);
574    lcml_dsp->NodeInfo.AllUUIDs[1].eDllType = DLL_DEPENDENT;
575
576    lcml_dsp->NodeInfo.AllUUIDs[2].uuid =(struct DSP_UUID * ) &USN_UUID;
577    strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[2].DllName,USN_DLL);
578    lcml_dsp->NodeInfo.AllUUIDs[2].eDllType = DLL_DEPENDENT;
579
580    lcml_dsp->NodeInfo.AllUUIDs[3].uuid =(struct DSP_UUID * ) &CONVERSIONS_UUID;
581    strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[3].DllName,CONVERSIONS_DLL);
582    lcml_dsp->NodeInfo.AllUUIDs[3].eDllType = DLL_DEPENDENT;
583
584#ifdef __JPEG_OMX_PPLIB_ENABLED__
585    lcml_dsp->NodeInfo.AllUUIDs[4].uuid =(struct DSP_UUID * ) &PPLIB_UUID;
586    strcpy ((char *)lcml_dsp->NodeInfo.AllUUIDs[4].DllName,PPLIB_DLL);
587    lcml_dsp->NodeInfo.AllUUIDs[4].eDllType = DLL_DEPENDENT;
588#endif
589    lcml_dsp->SegID = 0;
590    lcml_dsp->Timeout = -1;
591    lcml_dsp->Alignment = 0;
592    lcml_dsp->Priority = 5;
593
594      if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (352*288)) {
595          lcml_dsp->ProfileID = 2 ;
596      }
597      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (640*480)) {
598          lcml_dsp->ProfileID = 2 +1 ; // temporary fix meanwhile SN confirms is there's any problem with VGA profile ID
599      }
600      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (720*576)) {
601          lcml_dsp->ProfileID = 3 ;
602      }
603      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (1*1024*1024)) {
604          lcml_dsp->ProfileID = 4 ;
605      }
606      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (2*1024*1024)) {
607          lcml_dsp->ProfileID = 5 ;
608      }
609      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (3*1024*1024)) {
610          lcml_dsp->ProfileID = 6 ;
611      }
612      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (4*1024*1024)) {
613          lcml_dsp->ProfileID = 7 ;
614      }
615      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (5*1024*1024)) {
616          lcml_dsp->ProfileID = 8 ;
617      }
618      else if ((pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth) <= (6*1024*1024)) {
619          lcml_dsp->ProfileID = 9 ;
620      }
621      else {
622          lcml_dsp->ProfileID = 10;
623      }
624
625    /* CrPhArgs for JpegEnc */
626    ptCreateString[0] = JPGENC_SNTEST_STRMCNT;
627    ptCreateString[1] = JPGENC_SNTEST_INSTRMID; /* Stream ID   */
628    ptCreateString[2] = 0;                      /* Stream based input stream   */
629    ptCreateString[3] = JPGENC_SNTEST_INBUFCNT; /* Number of buffers on input stream   */
630    ptCreateString[4] = JPGENC_SNTEST_OUTSTRMID;/* Stream ID   */
631    ptCreateString[5] = 0;                      /* Stream based input stream   */
632    ptCreateString[6] = JPGENC_SNTEST_OUTBUFCNT;/* Number of buffers on input stream   */
633    ptCreateString[7] = (pPortDefOut->format.image.nFrameWidth > 0) ? pPortDefOut->format.image.nFrameWidth : JPGENC_SNTEST_MAX_WIDTH;
634    ptCreateString[8] = (pPortDefOut->format.image.nFrameHeight > 0) ? pPortDefOut->format.image.nFrameHeight : JPGENC_SNTEST_MAX_HEIGHT;
635
636    /*
637    if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar ) {
638        arr[9] = 1;
639    } else if ( pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY ) {
640        arr[9] = 4;
641    }  else {
642        arr[9] = 4;
643    }
644    */
645    ptCreateString[9] = 1;
646
647    ptCreateString[10] = 320; /* Maximum Horizontal Size of the Thumbnail for App0 marker */
648    ptCreateString[11] = 240; /* Maximum Vertical Size of the Thumbnail for App0 marker */
649    ptCreateString[12] = 320; /* Maximum Horizontal Size of the Thumbnail for App1 marker */
650    ptCreateString[13] = 240; /* Maximum Vertical Size of the Thumbnail for App1 marker */
651    ptCreateString[14] = 320; /* Maximum Horizontal Size of the Thumbnail for App13 marker */
652    ptCreateString[15] = 240; /* Maximum Vertical Size of the Thumbnail for App13 marker */
653    ptCreateString[16] = 0; /* Number of scans is always 0 */
654    if (pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar)
655    {
656    	ptCreateString[16] = 1;
657    }
658
659    ptCreateString[17] = 0;
660    if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888){
661    	ptCreateString[17] = 1; //Convert flag
662    }
663
664    ptCreateString[18] = 320; /* Maximum Horizontal Size of the Thumbnail for App5 marker */
665    ptCreateString[19] = 240; /* Maximum Vertical Size of the Thumbnail for App5 marker */
666
667#ifdef __JPEG_OMX_PPLIB_ENABLED__
668
669    //size
670    ptCreateStringPPLIB[10] = JPEGENC_PPLIB_CREATEPARAM_SIZE;
671
672    //SlibVersion
673    ptCreateStringPPLIB[11] = 0x00000100;
674
675    //MaxInWidth
676    ptCreateStringPPLIB[12] = pPortDefIn->format.image.nFrameWidth;
677
678    //MaxOutWidth
679    ptCreateStringPPLIB[13] = pPortDefIn->format.image.nFrameWidth;
680    //Input Format => 0:RGB24, 1:RGB16, 2:RGB12, 3:RGB8, 4:RGB4, 5:YUV422ILE, 6:YUV422IBE,
681    //                7:422_IN_UY_WS, 8:422_IN_YU_WS, 9:YUV420P, 10:GRAY8, 11:GRAY4, 12:GRAY2_IN, 13:GRAY1
682    if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY
683        ||  pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888
684        ||  pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYCbYCr)
685    {
686        ptCreateStringPPLIB[14] = 5;
687    }
688    else if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatYUV420PackedPlanar)
689    {
690        ptCreateStringPPLIB[14] = 9;
691    }
692    else if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_Format16bitRGB565)
693    {
694        ptCreateStringPPLIB[14] = 1;
695    }
696    else{
697        ptCreateStringPPLIB[14] = 9;
698    }
699
700    //YuvOutputFormat
701    // --> 0 = OFF, 1 = YUV420, 2 = YUV422ILE, 3 = YUV422IBE
702    if (pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatYUV420PackedPlanar)
703    {
704        ptCreateStringPPLIB[15] = 1;
705    }
706    else
707    {
708        ptCreateStringPPLIB[15] = 2;
709    }
710
711    //RGBOuputFormat
712    // --> 0 = OFF, 1 = RGB4, 2 = RGB8, 3 = RGB12, 4 = RGB16, 5 = RGB24, 6 = RGB32,
713    //     7 = GRAY8, 8 = GRAY4, 9 = GRAY2, 10 = GRAY1
714    ptCreateStringPPLIB[16] = 0;
715
716    ptCreateString[34] = END_OF_CR_PHASE_ARGS;
717#else
718    ptCreateString[20] = END_OF_CR_PHASE_ARGS;
719#endif
720    lcml_dsp->pCrPhArgs = ptCreateString;
721    EXIT:
722    return eError;
723}
724
725
726static OMX_ERRORTYPE HandleJpegEncInternalFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
727{
728    OMX_ERRORTYPE eError = OMX_ErrorNone;
729    OMX_U32 aParam[4];
730    LCML_DSP_INTERFACE *pLcmlHandle = NULL;
731
732    OMX_CHECK_PARAM(pComponentPrivate);
733
734    if ( nParam1 == 0x0 ||
735         (int)nParam1 == -1 ) {
736
737        pComponentPrivate->bFlushComplete = OMX_FALSE;
738        aParam[0] = USN_STRMCMD_FLUSH;
739        aParam[1] = 0;
740        aParam[2] = 0;
741        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
742        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
743        if (eError != OMX_ErrorNone) {
744            goto EXIT;
745        }
746
747        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
748        while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
749            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
750            pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
751        }
752        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
753
754        pComponentPrivate->bFlushComplete = OMX_FALSE;
755    }
756    if ( nParam1 == 0x1 ||
757	 (int)nParam1 == -1 ) {
758
759        pComponentPrivate->bFlushComplete = OMX_FALSE;
760        aParam[0] = USN_STRMCMD_FLUSH;
761        aParam[1] = 1;
762        aParam[2] = 0;
763        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
764        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
765        if (eError != OMX_ErrorNone) {
766            goto EXIT;
767        }
768
769        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
770        while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
771            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
772            pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
773        }
774        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
775        pComponentPrivate->bFlushComplete = OMX_FALSE;
776
777        pComponentPrivate->bFlushComplete = OMX_FALSE;
778    }
779
780    EXIT:
781    OMX_PRINT1(pComponentPrivate->dbg, "Exiting HandleCommand FLush Function JEPG Encoder\n");
782    return eError;
783
784}
785
786
787OMX_ERRORTYPE HandleJpegEncCommandFlush(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
788{
789    OMX_ERRORTYPE eError = OMX_ErrorNone;
790    OMX_U32 aParam[4];
791    LCML_DSP_INTERFACE *pLcmlHandle = NULL;
792
793    OMX_CHECK_PARAM(pComponentPrivate);
794
795    if ( nParam1 == 0x0 ||
796         (int)nParam1 == -1 ) {
797
798        pComponentPrivate->bFlushComplete = OMX_FALSE;
799
800        aParam[0] = USN_STRMCMD_FLUSH;
801        aParam[1] = 0;
802        aParam[2] = 0;
803        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
804        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
805        if (eError != OMX_ErrorNone) {
806            goto EXIT;
807        }
808       OMX_PRDSP2(pComponentPrivate->dbg, "sent EMMCodecControlStrmCtrl command\n");
809
810        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
811        while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
812            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
813            pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
814        }
815        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
816
817        pComponentPrivate->bFlushComplete = OMX_FALSE;
818
819        while (pComponentPrivate->nInPortIn > pComponentPrivate->nInPortOut) {
820
821            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
822            JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
823            int ret;
824
825            ret = read(pComponentPrivate->filled_inpBuf_Q[0], &(pBuffHead), sizeof(pBuffHead));
826            if ( ret == -1 ) {
827                OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
828            }
829
830            if (pBuffHead != NULL) {
831                pBuffPrivate = pBuffHead->pInputPortPrivate;
832            }
833
834            pComponentPrivate->nInPortOut ++;
835            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
836            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
837                    pComponentPrivate->nInPortIn,
838                    pComponentPrivate->nInPortOut,
839                    pComponentPrivate->nOutPortIn,
840                    pComponentPrivate->nOutPortOut);
841            OMX_PRBUFFER1(pComponentPrivate->dbg, "before EmptyBufferDone\n");
842            pComponentPrivate->cbInfo.EmptyBufferDone(
843                           pComponentPrivate->pHandle,
844                           pComponentPrivate->pHandle->pApplicationPrivate,
845                           pBuffHead);
846             OMX_PRBUFFER1(pComponentPrivate->dbg, "after EmptyBufferDone\n");
847        }
848#if 0
849        for ( i=0; i < pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual; i++ ) {
850
851#ifdef __PERF_INSTRUMENTATION__
852            PERF_SendingFrame(pComponentPrivate->pPERFcomp,
853                              PREF(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr,pBuffer),
854                              0,
855                              PERF_ModuleHLMM);
856#endif
857          pBuffPrivate = (JPEGENC_BUFFER_PRIVATE*) pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr->pInputPortPrivate;
858          OMX_PRBUFFER2(pComponentPrivate->dbg, "flush input port. buffer owner (%d) %d\n", i, pBuffPrivate->eBufferOwner);
859        }
860#endif
861
862        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (flush input) %lu %lu %lu %lu\n",
863                    pComponentPrivate->nInPortIn,
864                    pComponentPrivate->nInPortOut,
865                    pComponentPrivate->nOutPortIn,
866                    pComponentPrivate->nOutPortOut);
867
868        /* returned all input buffers */
869        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
870                                      pComponentPrivate->pHandle->pApplicationPrivate,
871                      OMX_EventCmdComplete,
872                                      OMX_CommandFlush,
873                                      JPEGENC_INP_PORT,
874                                      NULL);
875
876    }
877    if ( nParam1 == 0x1 ||
878         (int)nParam1 == -1 ) {
879
880        pComponentPrivate->bFlushComplete = OMX_FALSE;
881
882        aParam[0] = USN_STRMCMD_FLUSH;
883        aParam[1] = 1;
884        aParam[2] = 0;
885        pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
886        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStrmCtrl, (void*)aParam);
887        if (eError != OMX_ErrorNone) {
888            goto EXIT;
889        }
890        OMX_PRDSP2(pComponentPrivate->dbg, "(1) sent EMMCodecControlStrmCtrl command\n");
891
892        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
893        while (pComponentPrivate->bFlushComplete == OMX_FALSE) {
894            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
895            pthread_cond_wait(&pComponentPrivate->flush_cond, &pComponentPrivate->jpege_mutex);
896        }
897        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
898        pComponentPrivate->bFlushComplete = OMX_FALSE;
899
900        /* return all output buffers */
901
902#if 0
903        for ( i=0; i < pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual ; i++ ) {
904            OMX_PRBUFFER1(pComponentPrivate->dbg, "BEFORE  FillBufferDone in OMX_CommandFlush\n");
905
906#ifdef __PERF_INSTRUMENTATION__
907                        PERF_SendingFrame(pComponentPrivate->pPERFcomp,
908                                          PREF(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr,pBuffer),
909                                          PREF(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr,nFilledLen),
910                                          PERF_ModuleHLMM);
911#endif
912          pBuffPrivate = (JPEGENC_BUFFER_PRIVATE*) pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr->pOutputPortPrivate;
913          OMX_PRBUFFER2(pComponentPrivate->dbg, "flush output port. buffer owner (%d) %d\n", i, pBuffPrivate->eBufferOwner);
914
915          OMX_PRBUFFER1(pComponentPrivate->dbg, "in flush 1: buffer %d owner %d\n", i, pBuffPrivate->eBufferOwner);
916          if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_COMPONENT_IN) {
917                  OMX_PRBUFFER1(pComponentPrivate->dbg, "return output buffer %p from free_in_pipe (flush)\n",
918                     pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr);
919                  pComponentPrivate->nOutPortOut ++;
920                  pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
921                  pComponentPrivate->cbInfo.FillBufferDone (pComponentPrivate->pHandle,
922                                   pComponentPrivate->pHandle->pApplicationPrivate,
923                                   pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr);
924          }
925
926        }
927#endif
928        while (pComponentPrivate->nOutPortIn > pComponentPrivate->nOutPortOut) {
929            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
930            JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
931            int ret;
932
933            OMX_PRBUFFER1(pComponentPrivate->dbg, "in while loop %lu %lu )\n", pComponentPrivate->nOutPortIn, pComponentPrivate->nOutPortOut);
934            ret = read(pComponentPrivate->free_outBuf_Q[0], &pBuffHead, sizeof(pBuffHead));
935            if ( ret == -1 ) {
936                OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
937                goto EXIT;
938            }
939            OMX_PRCOMM1(pComponentPrivate->dbg, "after read\n");
940            if (pBuffHead != NULL) {
941               pBuffPrivate = pBuffHead->pOutputPortPrivate;
942            }
943
944            pComponentPrivate->nOutPortOut ++;
945            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
946            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
947                    pComponentPrivate->nInPortIn,
948                    pComponentPrivate->nInPortOut,
949                    pComponentPrivate->nOutPortIn,
950                    pComponentPrivate->nOutPortOut);
951             OMX_PRBUFFER1(pComponentPrivate->dbg, "before FillBufferDone\n");
952             pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
953                    pComponentPrivate->pHandle->pApplicationPrivate,
954                    pBuffHead);
955             OMX_PRBUFFER1(pComponentPrivate->dbg, "after FillBufferDone\n");
956        }
957
958        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (flush input) %lu %lu %lu %lu\n",
959                    pComponentPrivate->nInPortIn,
960                    pComponentPrivate->nInPortOut,
961                    pComponentPrivate->nOutPortIn,
962                    pComponentPrivate->nOutPortOut);
963
964        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
965                                               pComponentPrivate->pHandle->pApplicationPrivate,
966                                               OMX_EventCmdComplete,
967                                               OMX_CommandFlush,
968                                               JPEGENC_OUT_PORT,
969                                               NULL);
970    }
971
972    EXIT:
973   OMX_PRINT1(pComponentPrivate->dbg, "Exiting HandleCommand FLush Function JEPG Encoder\n");
974    return eError;
975
976}
977
978OMX_ERRORTYPE SendDynamicParam(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
979{
980    OMX_ERRORTYPE eError = OMX_ErrorNone;
981    IDMJPGE_TIGEM_DynamicParams* pTmpDynParams;
982    OMX_HANDLETYPE pLcmlHandle = NULL;
983    char* pTmp = NULL;
984    OMX_U32 cmdValues[3] = {0, 0, 0};
985    IIMGENC_DynamicParams ptParam;
986    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
987    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
988
989    OMX_CHECK_PARAM(pComponentPrivate);
990
991    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
992    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
993
994    ptParam.nNumAU = 0; /*  XDM_DEFAULT */
995    //ptParam.nSize   =   sizeof(IIMGENC_DynamicParams);
996    ptParam.nSize   =   sizeof(IDMJPGE_TIGEM_DynamicParams) ;
997
998    if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar ) {
999        ptParam.nInputChromaFormat = 1;
1000    }
1001    else if ( pPortDefIn->format.image.eColorFormat ==  OMX_COLOR_FormatCbYCrY
1002    		|| pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format32bitARGB8888
1003            || pPortDefIn->format.image.eColorFormat == OMX_COLOR_Format16bitRGB565) {
1004        ptParam.nInputChromaFormat = 4;
1005    }
1006	else if ( pPortDefIn->format.image.eColorFormat == OMX_COLOR_FormatYCbYCr) {
1007		ptParam.nInputChromaFormat = 9;
1008	}
1009    else {
1010        ptParam.nInputChromaFormat = 1;
1011    }
1012
1013    if (pComponentPrivate->pCrop->nWidth == 0)
1014    {
1015        ptParam.nInputWidth     = pPortDefIn->format.image.nFrameWidth;
1016    }
1017    else
1018    {
1019        ptParam.nInputWidth     = pComponentPrivate->pCrop->nWidth;
1020    }
1021
1022    if (pComponentPrivate->pCrop->nHeight == 0)
1023    {
1024        ptParam.nInputHeight     = pPortDefIn->format.image.nFrameHeight;
1025    }
1026    else
1027    {
1028        ptParam.nInputHeight     = pComponentPrivate->pCrop->nHeight;
1029    }
1030
1031    ptParam.nCaptureWidth   =  pPortDefIn->format.image.nFrameWidth;
1032    ptParam.nGenerateHeader =   0; /*XDM_ENCODE_AU*/
1033    ptParam.qValue          =   pComponentPrivate->pQualityfactor->nQFactor;
1034
1035    OMX_PRDSP1(pComponentPrivate->dbg, "ptParam.qValue %lu\n", ptParam.qValue);
1036
1037
1038    pTmp = (char*)pComponentPrivate->pDynParams;
1039    pTmp += 128;
1040    pTmpDynParams = (IDMJPGE_TIGEM_DynamicParams*)pTmp;
1041
1042    pTmpDynParams->params         = ptParam;
1043    pTmpDynParams->captureHeight = pPortDefIn->format.image.nFrameHeight;
1044    pTmpDynParams->DRI_Interval  = pComponentPrivate->nDRI_Interval;
1045    pTmpDynParams->huffmanTable = NULL;
1046    pTmpDynParams->quantTable     = NULL;
1047
1048    cmdValues[0] = IUALG_CMD_SETSTATUS;
1049    cmdValues[1] = (OMX_U32)(pTmpDynParams);
1050    cmdValues[2] = sizeof(IDMJPGE_TIGEM_DynamicParams) + 128;
1051
1052    pComponentPrivate->bAckFromSetStatus = 0;
1053    pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1054    eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
1055                                               EMMCodecControlAlgCtrl,
1056                                               (void*)&cmdValues);
1057
1058EXIT:
1059    return eError;
1060}
1061
1062#ifdef __JPEG_OMX_PPLIB_ENABLED__
1063OMX_ERRORTYPE SendDynamicPPLibParam(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate,OMX_U32 *ptInputParam)
1064{
1065    OMX_ERRORTYPE eError = OMX_ErrorNone;
1066    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
1067    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
1068
1069    OMX_CHECK_PARAM(pComponentPrivate);
1070    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
1071    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
1072
1073    // PPLIB hardcoded params
1074    OMX_U32 ulOutPitch = 0;
1075    OMX_U32 ulPPLIBVideoGain=64;
1076    OMX_U32 ulPPLIBEnableCropping=0;
1077    OMX_U32 ulPPLIBXstart=0;
1078    OMX_U32 ulPPLIBYstart=0;
1079    OMX_U32 ulPPLIBXsize=0;
1080    OMX_U32 ulPPLIBYsize=0;
1081    OMX_U32 ulPPLIBEnableZoom=0;
1082    OMX_U32 ulPPLIBZoomFactor=0;
1083    OMX_U32 ulPPLIBZoomLimit=0;
1084    OMX_U32 ulPPLIBZoomSpeed=0;
1085    OMX_U32 ulPPLIBLightChroma=0;
1086    OMX_U32 ulPPLIBLockedRatio=1;
1087    OMX_U32 ulPPLIBMirroring=0;
1088    OMX_U32 ulPPLIBRGBrotation=0;
1089    OMX_U32 ulPPLIBYUVRotation=0;
1090    OMX_U32 ulPPLIBIORange=1;
1091    OMX_U32 ulPPLIBDithering=0;
1092
1093    OMX_U32 cOffset = 0;
1094
1095    /* PPLIB_RunTimeParams */
1096    // LgUns size; // size of structure
1097
1098    ptInputParam[0] = JPEGENC_PPLIB_DYNPARM_SIZE; // 252 bytes
1099
1100    // LgUns ulInWidth; // picture buffer width
1101
1102    ptInputParam[1] = pPortDefIn->format.image.nFrameWidth;
1103
1104    // LgUns ulInHeight; // picture buffer height
1105
1106    ptInputParam[2] = pPortDefIn->format.image.nFrameHeight;
1107
1108    // LgUns FrameEnabled[0] (enable instance 1 of VGPOP)
1109
1110    ptInputParam[3] = 1;
1111
1112    // LgUns FrameEnabled[1] (enable instance 2 of VGPOP - not used now)
1113
1114    ptInputParam[4] = 0;
1115
1116    // LgUns EnalbeYUVOutput[0]
1117
1118    ptInputParam[5] = 1;
1119
1120    // LgUns FrameEnabled[1]
1121
1122    ptInputParam[6] = 0;
1123
1124    // LgUns EnalbeRGBOutput[0]
1125
1126    ptInputParam[7] = 0;
1127
1128    // LgUns EnalbeRGBOutput[1]
1129
1130    ptInputParam[8] = 0;
1131
1132    // LgUns FrameInputStartYOffset[0]
1133
1134    ptInputParam[9] = 0;
1135
1136    // LgUns FrameInputStartYOffset[1]
1137
1138    ptInputParam[10] = 0;
1139
1140     cOffset = (pPortDefIn->format.image.nFrameWidth * pPortDefIn->format.image.nFrameHeight);
1141
1142    // LgUns FrameInputStartCOffset[0]
1143
1144    ptInputParam[11] = cOffset;
1145
1146    // LgUns FrameInputStartCOffset[1]
1147
1148    ptInputParam[12] = cOffset;
1149
1150    // LgUns FrameOutputStartYOffset[0]
1151
1152    ptInputParam[13] = 0;
1153
1154    // LgUns FrameOutputStartYOffset[1]
1155
1156    ptInputParam[14] = 0;
1157
1158    if (ulOutPitch > 0) {
1159        if (ulPPLIBYUVRotation == 0 || ulPPLIBYUVRotation == 180) {
1160            cOffset = (pPortDefIn->format.image.nFrameHeight * ulOutPitch);
1161        }
1162        else {
1163            cOffset = (pPortDefIn->format.image.nFrameWidth * ulOutPitch);
1164        }
1165    }
1166    else {
1167        cOffset = (pPortDefIn->format.image.nFrameHeight * pPortDefIn->format.image.nFrameWidth);
1168    }
1169
1170    // LgUns FrameOutputStartCOffset[0]
1171    ptInputParam[15] = cOffset;
1172
1173    // LgUns FrameOutputStartCOffset[1]
1174    ptInputParam[16] = cOffset;
1175
1176    // LgUns FrameOutputRGBOffset[0]
1177    ptInputParam[17] = 0;
1178
1179    // LgUns FrameOutputRGBOffset[1]
1180    ptInputParam[18] = 0;
1181
1182    // LgUns ulOutHeight[0]; // picture buffer height
1183    ptInputParam[19] = pPortDefIn->format.image.nFrameHeight;
1184
1185    // LgUns ulOutHeight[1]; // picture buffer height
1186    ptInputParam[20] = 0;
1187
1188    // LgUns ulOutWidth[0]; // picture buffer width
1189    ptInputParam[21] = pPortDefIn->format.image.nFrameWidth;
1190
1191    // LgUns ulOutWidth[1]; // picture buffer width
1192    ptInputParam[22] = 0;
1193
1194    //Contrast (same as Video Gain)
1195    ptInputParam[23] = ulPPLIBVideoGain;
1196
1197    //Contrast (same as Video Gain)
1198    ptInputParam[24] = ulPPLIBVideoGain;
1199
1200    if (ulPPLIBEnableCropping == 1) {
1201        // Cropping
1202        // LgUns ulInXstart[0]; // Hin active start
1203        ptInputParam[25] = ulPPLIBXstart;
1204
1205        // Cropping
1206        // LgUns ulInXstart[1]; // Hin active start
1207        ptInputParam[26] = 0;
1208
1209        //LgUns ulInYstart[0]; // Vin active start
1210        ptInputParam[27] = ulPPLIBYstart;
1211
1212        //LgUns ulInYstart[1]; // Vin active start
1213        ptInputParam[28] = 0;
1214
1215        // LgUns ulInXsize[0]; // Hin active width
1216        ptInputParam[29] = ulPPLIBXsize;
1217
1218        // LgUns ulInXsize[1]; // Hin active width
1219        ptInputParam[30] = 0;
1220
1221        // LgUns ulInYsize; // Vin active height
1222        ptInputParam[31] = ulPPLIBYsize;
1223
1224        // LgUns ulInYsize; // Vin active height
1225        ptInputParam[32] = 0;
1226    }
1227    else {
1228        // Cropping
1229        // LgUns ulInXstart; // Hin active start
1230        ptInputParam[25] = 0;
1231
1232        // Cropping
1233        // LgUns ulInXstart[1]; // Hin active start
1234        ptInputParam[26] = 0;
1235
1236        //LgUns ulInYstart; // Vin active start
1237        ptInputParam[27] = 0;
1238
1239        //LgUns ulInYstart[1]; // Vin active start
1240        ptInputParam[28] = 0;
1241
1242        // LgUns ulInXsize; // Hin active width
1243        ptInputParam[29] = 0;
1244
1245        // LgUns ulInXsize[1]; // Hin active width
1246        ptInputParam[30] = 0;
1247
1248        // LgUns ulInYsize; // Vin active height
1249        ptInputParam[31] = 0;
1250
1251        // LgUns ulInYsize; // Vin active height
1252        ptInputParam[32] = 0;
1253    }
1254
1255    if (ulPPLIBEnableZoom) {
1256        //Zoom
1257        //LgUns ulZoomFactor; // zooming ratio (/1024)
1258        ptInputParam[33] = ulPPLIBZoomFactor;
1259
1260        //Zoom
1261        //LgUns ulZoomFactor; // zooming ratio (/1024)
1262        ptInputParam[34] = 1024;
1263
1264        // LgUns ulZoomLimit; // zooming ratio limit (/1024)
1265        ptInputParam[35] = ulPPLIBZoomLimit;
1266
1267        // LgUns ulZoomLimit; // zooming ratio limit (/1024)
1268        ptInputParam[36] = 1024;
1269
1270        // LgInt slZoomSpeed; // speed of ratio change
1271        ptInputParam[37] = ulPPLIBZoomSpeed;
1272
1273        // LgInt slZoomSpeed; // speed of ratio change
1274        ptInputParam[38] = 0;
1275    }
1276    else {
1277        //Zoom
1278        //LgUns ulZoomFactor; // zooming ratio (/1024)
1279        ptInputParam[33] = 1024;
1280
1281        //Zoom
1282        //LgUns ulZoomFactor; // zooming ratio (/1024)
1283        ptInputParam[34] = 1024;
1284
1285        // LgUns ulZoomLimit; // zooming ratio limit (/1024)
1286        ptInputParam[35] = 1024;
1287
1288        // LgUns ulZoomLimit; // zooming ratio limit (/1024)
1289        ptInputParam[36] = 1024;
1290
1291        // LgInt slZoomSpeed; // speed of ratio change
1292        ptInputParam[37] = 0;
1293
1294        // LgInt slZoomSpeed; // speed of ratio change
1295        ptInputParam[38] = 0;
1296    }
1297
1298    // LgUns bLightChroma[0]; // Light chrominance process
1299    ptInputParam[39] = ulPPLIBLightChroma;
1300
1301    // LgUns bLightChroma[1]; // Light chrominance process
1302    ptInputParam[40] = ulPPLIBLightChroma;
1303
1304    //Aspect Ration Locked/unlocked
1305    // LgUns bLockedRatio; // keep H/V ratio
1306    ptInputParam[41] = ulPPLIBLockedRatio;
1307
1308    //Aspect Ration Locked/unlocked
1309    // LgUns bLockedRatio; // keep H/V ratio
1310    ptInputParam[42] = ulPPLIBLockedRatio;
1311
1312    //Mirroring and Rotation
1313    // LgUns bMirror; // to mirror the picture
1314    ptInputParam[43] = ulPPLIBMirroring;
1315
1316    //Mirroring and Rotation
1317    // LgUns bMirror; // to mirror the picture
1318    ptInputParam[44] = ulPPLIBMirroring;
1319
1320    // LgUns eRGBrotation; // 0, 90, 180, 270 deg.
1321    ptInputParam[45] = ulPPLIBRGBrotation;
1322
1323    // LgUns eRGBrotation; // 0, 90, 180, 270 deg.
1324    ptInputParam[46] = ulPPLIBRGBrotation;
1325
1326    // LgUns eYUVrotation; // 0, 90, 180, 270 deg.
1327    ptInputParam[47] = ulPPLIBYUVRotation;
1328
1329    // LgUns eYUVrotation; // 0, 90, 180, 270 deg.
1330    ptInputParam[48] = ulPPLIBYUVRotation;
1331
1332    // IO Range and Dithering
1333    // LgUns eIORange; // Input/Output video range
1334    // 0 = VGPOP_IN_16_235_OUT_16_235 (limi range to limi range),
1335    // 1 = VGPOP_IN_00_255_OUT_00_255 (full range to full range),
1336    // 2 = VGPOP_IN_00_255_OUT_16_235 (full range to limi range),
1337    // 3 = VGPOP_IN_16_235_OUT_00_255 (limi range to full range)
1338    ptInputParam[49] = ulPPLIBIORange;
1339
1340    // IO Range and Dithering
1341    // LgUns eIORange; // Input/Output video range
1342    // 0 = VGPOP_IN_16_235_OUT_16_235 (limi range to limi range),
1343    // 1 = VGPOP_IN_00_255_OUT_00_255 (full range to full range),
1344    // 2 = VGPOP_IN_00_255_OUT_16_235 (full range to limi range),
1345    // 3 = VGPOP_IN_16_235_OUT_00_255 (limi range to full range)
1346    ptInputParam[50] = ulPPLIBIORange;
1347
1348    // LgUns bDithering; // ON Enables the dithering
1349    ptInputParam[51] = ulPPLIBDithering;
1350
1351    // LgUns bDithering; // ON Enables the dithering
1352    ptInputParam[52] = ulPPLIBDithering;
1353
1354    // LgUns ulFrameOutputPitch; // ON Enables the dithering
1355    ptInputParam[53] = ulOutPitch;
1356
1357    // LgUns bDithering; // ON Enables the dithering
1358    ptInputParam[54] = ulOutPitch;
1359
1360    // LgUns ulAlphaRGB;
1361    ptInputParam[55] = 0;
1362
1363    // LgUns ulAlphaRGB;
1364    ptInputParam[56] = 0;
1365
1366    // LgUns ulIsFrameGenerated[0]
1367    ptInputParam[57] = 0;
1368
1369    // LgUns ulIsFrameGenerated[1]
1370    ptInputParam[58] = 0;
1371
1372    // LgUns ulYUVFrameSize[0]
1373    ptInputParam[59] = 0;
1374
1375    // LgUns ulYUVFrameSize[1]
1376    ptInputParam[60] = 0;
1377
1378    // LgUns ulRGBFrameSize[0]
1379    ptInputParam[61] = 0;
1380
1381    // LgUns ulRGBFrameSize[1]
1382    ptInputParam[62] = 0;
1383
1384EXIT:
1385    return eError;
1386}
1387#endif
1388
1389/*-------------------------------------------------------------------*/
1390/**
1391  *  HandleCommand() Handle State type commands
1392  *
1393  *  Depending on the State Command received it executes the corresponding code.
1394  *
1395  * @param phandle    handle for this instance of the component
1396  *
1397  * @retval OMX_NoError              Success, ready to roll
1398  * @retval OMX_ErrorInsufficientResources    Insiffucient Resources
1399  * @retval OMX_ErrorInvalidState    Invalid State Change
1400  *
1401  **/
1402/*-------------------------------------------------------------------*/
1403OMX_ERRORTYPE HandleJpegEncCommand (JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32 nParam1)
1404{
1405
1406    OMX_ERRORTYPE eError = OMX_ErrorNone;
1407    OMX_COMPONENTTYPE *pHandle = NULL;
1408    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
1409    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
1410    OMX_HANDLETYPE pLcmlHandle = NULL;
1411    LCML_DSP *lcml_dsp;
1412    OMX_U16 arr[100];
1413    LCML_CALLBACKTYPE cb;
1414#ifdef RESOURCE_MANAGER_ENABLED
1415    OMX_U32 lImageResolution = 0;
1416    OMX_U8 nMHzRM = 0;
1417#endif
1418
1419
1420
1421    OMX_CHECK_PARAM(pComponentPrivate);
1422    OMX_PRINT1(pComponentPrivate->dbg, "JPEGEnc Handlecommand\n");
1423    pHandle = (OMX_COMPONENTTYPE *) pComponentPrivate->pHandle;
1424    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
1425    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
1426
1427    switch ( (OMX_STATETYPE)(nParam1) ) {
1428    case OMX_StateIdle:
1429        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateIdle\n");
1430        OMX_PRSTATE1(pComponentPrivate->dbg, "CHP 1 pComponentPrivate->nCurState  = %d\n",pComponentPrivate->nCurState );
1431        OMX_PRSTATE1(pComponentPrivate->dbg, "In idle in %lu out %lu\n", pComponentPrivate->nInPortIn, pComponentPrivate->nOutPortOut);
1432
1433        if ( pComponentPrivate->nCurState == OMX_StateIdle ) {
1434            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1435                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1436                                                   OMX_EventError,
1437                                                   OMX_ErrorSameState,
1438                                                   OMX_TI_ErrorMinor,
1439                                                   NULL);
1440            break;
1441        }
1442        else if ( pComponentPrivate->nCurState == OMX_StateLoaded ||
1443                  pComponentPrivate->nCurState == OMX_StateWaitForResources) {
1444
1445                      OMX_PRSTATE2(pComponentPrivate->dbg, "state tranc from loaded to idle\n");
1446#ifdef __PERF_INSTRUMENTATION__
1447                  PERF_Boundary(pComponentPrivate->pPERFcomp,
1448                                PERF_BoundaryStart | PERF_BoundarySetup);
1449#endif
1450
1451#ifdef RESOURCE_MANAGER_ENABLED /* Resource Manager Proxy Calls */
1452            pComponentPrivate->rmproxyCallback.RMPROXY_Callback = (void *)ResourceManagerCallback;
1453            lImageResolution = pPortDefIn->format.image.nFrameWidth * pPortDefIn->format.image.nFrameHeight;
1454            OMX_GET_RM_VALUE(lImageResolution, nMHzRM, pComponentPrivate->dbg);
1455            OMX_PRMGR2(pComponentPrivate->dbg, "Value sent to RM = %d\n", nMHzRM);
1456            if (pComponentPrivate->nCurState != OMX_StateWaitForResources) {
1457
1458                eError = RMProxy_NewSendCommand(pHandle, RMProxy_RequestResource, OMX_JPEG_Encoder_COMPONENT, nMHzRM, 3456, &(pComponentPrivate->rmproxyCallback));
1459
1460                if (eError != OMX_ErrorNone) {
1461                    /* resource is not available, need set state to OMX_StateWaitForResources*/
1462                    OMX_PRMGR4(pComponentPrivate->dbg, "Resource is not available\n");
1463
1464                    pComponentPrivate->cbInfo.EventHandler(pHandle,
1465                                                           pHandle->pApplicationPrivate,
1466                                                           OMX_EventError,
1467                                                           OMX_ErrorInsufficientResources,
1468                                                           OMX_TI_ErrorSevere,
1469                                                           NULL);
1470                    eError = OMX_ErrorNone;
1471                    break;
1472                }
1473            }
1474#endif
1475
1476            if ( pPortDefIn->bEnabled == OMX_TRUE && pPortDefOut->bEnabled == OMX_TRUE ) {
1477                pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
1478                while ( (!pPortDefIn->bPopulated) || (!pPortDefOut->bPopulated)) {
1479                    OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1480                    pthread_cond_wait(&pComponentPrivate->populate_cond, &pComponentPrivate->jpege_mutex_app);
1481                }
1482                pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
1483            }
1484
1485            eError =  GetJpegEncLCMLHandle(pHandle);
1486
1487            if ( eError != OMX_ErrorNone ) {
1488                OMX_PRDSP4(pComponentPrivate->dbg, "GetLCMLHandle failed...\n");
1489                goto EXIT;
1490            }
1491            pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1492            lcml_dsp = (((LCML_DSP_INTERFACE*)pLcmlHandle)->dspCodec);
1493            Fill_JpegEncLCMLInitParams(lcml_dsp,arr, pHandle);
1494            cb.LCML_Callback = (void *) JpegEncLCML_Callback;
1495            OMX_PRDSP2(pComponentPrivate->dbg, "Start LCML_InitMMCodec JPEG Phase in JPEG.....\n");
1496
1497            /*  calling initMMCodec to init codec with details filled earlier   */
1498            eError = LCML_InitMMCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle, NULL, &pLcmlHandle, NULL, &cb);
1499            if ( eError != OMX_ErrorNone ) {
1500                OMX_PRDSP4(pComponentPrivate->dbg, "InitMMCodec failed...  %x\n", eError);
1501                printf("Error : InitMMCodec failed...>>>>>>");
1502                pComponentPrivate->cbInfo.EventHandler(pHandle,
1503                                                       pHandle->pApplicationPrivate,
1504                                                       OMX_EventError,
1505                                                       OMX_ErrorHardware,
1506                                                       OMX_TI_ErrorSevere,
1507                                                       NULL);
1508                goto EXIT;
1509            }
1510            pComponentPrivate->isLCMLActive = 1;
1511            OMX_PRDSP2(pComponentPrivate->dbg, "End LCML_InitMMCodec Phase\n");
1512
1513
1514            pComponentPrivate->bFlushComplete = OMX_FALSE;
1515            OMX_PRSTATE2(pComponentPrivate->dbg, "State has been Set to Idle\n");
1516            pComponentPrivate->nCurState = OMX_StateIdle;
1517
1518            /* Decrement reference count with signal enabled */
1519            if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
1520                   return OMX_ErrorUndefined;
1521            }
1522
1523            pComponentPrivate->nInPortIn   = pComponentPrivate->nInPortOut   = 0;
1524            pComponentPrivate->nOutPortIn = pComponentPrivate->nOutPortOut = 0;
1525
1526
1527#ifdef RESOURCE_MANAGER_ENABLED
1528            eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle,  3456, NULL);
1529            if (eError != OMX_ErrorNone) {
1530                OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Loaded ->Idle\n");
1531
1532                pComponentPrivate->cbInfo.EventHandler(pHandle,
1533                                                       pHandle->pApplicationPrivate,
1534                                                       OMX_EventError,
1535                                                       OMX_ErrorInsufficientResources,
1536                                                       OMX_TI_ErrorSevere,
1537                                                       NULL);
1538                break;
1539            }
1540#endif
1541
1542#ifdef __PERF_INSTRUMENTATION__
1543            PERF_Boundary(pComponentPrivate->pPERFcomp,
1544                          PERF_BoundaryComplete | PERF_BoundarySetup);
1545#endif
1546            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1547                              pComponentPrivate->pHandle->pApplicationPrivate,
1548                          OMX_EventCmdComplete,
1549                          OMX_CommandStateSet,
1550                          pComponentPrivate->nCurState,
1551                          NULL);
1552            break;
1553
1554        }
1555        else if ( pComponentPrivate->nCurState == OMX_StateExecuting ||
1556                  pComponentPrivate->nCurState == OMX_StatePause ) {
1557#ifdef __PERF_INSTRUMENTATION__
1558            PERF_Boundary(pComponentPrivate->pPERFcomp,
1559                          PERF_BoundaryComplete | PERF_BoundarySteadyState);
1560#endif
1561
1562          pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1563          pComponentPrivate->bDSPStopAck = OMX_FALSE;
1564          OMX_PRDSP2(pComponentPrivate->dbg, "bDSPStopAck is %d\n", pComponentPrivate->bDSPStopAck);
1565          eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,MMCodecControlStop,NULL);
1566          pComponentPrivate->nApp_nBuf= 1;
1567         /* HandleJpegEncCommandFlush(pComponentPrivate, -1); */
1568          /*
1569          if ( pComponentPrivate->isLCMLActive ==1 ) {
1570              LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
1571              dlclose(pComponentPrivate->pDllHandle);
1572              pComponentPrivate->isLCMLActive = 0;
1573          }
1574          */
1575
1576#ifdef RESOURCE_MANAGER_ENABLED
1577
1578        eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
1579        if (eError != OMX_ErrorNone) {
1580	        OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Executing ->Idle\n");
1581            pComponentPrivate->nCurState = OMX_StateWaitForResources;
1582            pComponentPrivate->cbInfo.EventHandler(pHandle,
1583                                                   pHandle->pApplicationPrivate,
1584                                                   OMX_EventCmdComplete,
1585                                                   OMX_CommandStateSet,
1586                                                   pComponentPrivate->nCurState,
1587                                                   NULL);
1588            break;
1589        }
1590#endif
1591          pComponentPrivate->ExeToIdleFlag |= JPEGE_BUFFERBACK;
1592
1593        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc: before stop lock\n");
1594        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
1595        /*
1596        while ((pComponentPrivate->ExeToIdleFlag & 0x3) != JPEGE_IDLEREADY) {
1597            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1598            pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
1599        }
1600        */
1601        while (pComponentPrivate->bDSPStopAck == OMX_FALSE) {
1602            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1603            pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
1604        }
1605        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
1606
1607        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:got STOP ack from DSP\n");
1608
1609        int i;
1610        for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
1611            JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
1612            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
1613
1614            pBuffHead = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr;
1615            pBuffPrivate = pBuffHead->pInputPortPrivate;
1616
1617            OMX_PRBUFFER1(pComponentPrivate->dbg, "JPEG enc:: owner %d \n", pBuffPrivate->eBufferOwner);
1618            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
1619                if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufSupplier != OMX_BufferSupplyInput) {
1620                    if(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent == NULL){
1621                        OMX_PRBUFFER2(pComponentPrivate->dbg, "Sending buffer to app\n");
1622                        OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
1623                        pComponentPrivate->nInPortOut ++;
1624                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1625                        pComponentPrivate->cbInfo.EmptyBufferDone(
1626                                   pComponentPrivate->pHandle,
1627                                   pComponentPrivate->pHandle->pApplicationPrivate,
1628                                   pBuffHead);
1629                }
1630                else{
1631                    OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending beffer to tunnel, pHandle=%p\n", pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent);
1632                    pBuffHead->nFilledLen = 0;
1633                    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1634                    eError = OMX_FillThisBuffer(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent,
1635                                                pBuffHead);
1636                }
1637            }
1638        }
1639    }
1640
1641        OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all input buffers\n");
1642
1643        for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual); i ++) {
1644            JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
1645            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
1646
1647            pBuffHead = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr;
1648            pBuffPrivate = pBuffHead->pOutputPortPrivate;
1649
1650            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer %p owner %d \n", pBuffHead, pBuffPrivate->eBufferOwner);
1651            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
1652                if (pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufSupplier != OMX_BufferSupplyOutput) {
1653                    if(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent == NULL){
1654                        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to app\n");
1655                        OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
1656                        pComponentPrivate->nOutPortOut ++;
1657                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1658                        pComponentPrivate->cbInfo.FillBufferDone(
1659                                   pComponentPrivate->pHandle,
1660                                   pComponentPrivate->pHandle->pApplicationPrivate,
1661                                   pBuffHead);
1662                    }
1663                    else{
1664                        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to Tunnel component\n");
1665                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1666                    eError = OMX_EmptyThisBuffer(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent,
1667                                                pBuffHead);
1668                    }
1669                }
1670            }
1671        }
1672        OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all output buffers\n");
1673
1674         pComponentPrivate->nCurState = OMX_StateIdle;
1675
1676         /* Decrement reference count with signal enabled */
1677         if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
1678              return OMX_ErrorUndefined;
1679         }
1680
1681         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1682               pComponentPrivate->pHandle->pApplicationPrivate,
1683               OMX_EventCmdComplete,
1684               OMX_CommandStateSet,
1685               pComponentPrivate->nCurState,
1686               NULL);
1687             pComponentPrivate->ExeToIdleFlag = 0;
1688    }
1689        else {
1690         OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
1691         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
1692                                                    pComponentPrivate->pHandle->pApplicationPrivate,
1693                            OMX_EventError,
1694                                                    OMX_ErrorIncorrectStateTransition,
1695                                                    OMX_TI_ErrorMinor,
1696                                                    "Invalid State");
1697        }
1698    break;
1699
1700    case OMX_StateExecuting:
1701
1702        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateExecuting \n");
1703        OMX_PRBUFFER2(pComponentPrivate->dbg, "In exec in %lu out %lu\n", pComponentPrivate->nInPortIn, pComponentPrivate->nOutPortOut);
1704        if ( pComponentPrivate->nCurState == OMX_StateExecuting ) {
1705            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1706                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1707                                                   OMX_EventError,
1708                                                   OMX_ErrorSameState,
1709                                                   OMX_TI_ErrorMinor,
1710                                                   NULL);
1711        }
1712        else if ( pComponentPrivate->nCurState == OMX_StateIdle || pComponentPrivate->nCurState == OMX_StatePause ) {
1713
1714#ifdef __PERF_INSTRUMENTATION__
1715            PERF_Boundary(pComponentPrivate->pPERFcomp,
1716                          PERF_BoundaryStart | PERF_BoundarySteadyState);
1717#endif
1718
1719#if 1
1720        eError = SendDynamicParam(pComponentPrivate);
1721            if (eError != OMX_ErrorNone ) {
1722                OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1723                goto EXIT;
1724        }
1725#endif
1726
1727
1728
1729
1730        OMX_PRDSP2(pComponentPrivate->dbg, "after SendDynamicParam\n");
1731        pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1732        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStart,NULL);
1733
1734#ifdef RESOURCE_MANAGER_ENABLED
1735
1736    eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateExecuting, 3456, NULL);
1737    if (eError != OMX_ErrorNone) {
1738        OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available\n");
1739        pComponentPrivate->nCurState = OMX_StateWaitForResources;
1740        pComponentPrivate->cbInfo.EventHandler(pHandle,
1741                                               pHandle->pApplicationPrivate,
1742                                               OMX_EventCmdComplete,
1743                                               OMX_CommandStateSet,
1744                                               pComponentPrivate->nCurState,
1745                                               NULL);
1746        break;
1747        }
1748#endif
1749
1750        pComponentPrivate->nCurState = OMX_StateExecuting;
1751        OMX_PRSTATE2(pComponentPrivate->dbg, "State has been set to Executing\n");
1752#ifdef __PERF_INSTRUMENTATION__
1753            PERF_Boundary(pComponentPrivate->pPERFcomp,
1754                          PERF_BoundarySteadyState| PERF_BoundaryComplete);
1755#endif
1756         /* Decrement reference count with signal enabled */
1757         if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
1758               return OMX_ErrorUndefined;
1759         }
1760
1761        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1762                                               pComponentPrivate->pHandle->pApplicationPrivate,
1763                                               OMX_EventCmdComplete,
1764                                               OMX_CommandStateSet,
1765                                               pComponentPrivate->nCurState,
1766                                               NULL);
1767        } else {
1768            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1769                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1770                                                   OMX_EventError,
1771                                                   OMX_ErrorIncorrectStateTransition,
1772                                                   OMX_TI_ErrorMinor,
1773                                                   NULL);
1774        }
1775        break;
1776
1777    case OMX_StatePause:
1778        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StatePause\n");
1779
1780        pComponentPrivate->nToState = OMX_StatePause;
1781#ifdef __PERF_INSTRUMENTATION__
1782                PERF_Boundary(pComponentPrivate->pPERFcomp,
1783                              PERF_BoundaryComplete | PERF_BoundarySteadyState);
1784#endif
1785
1786        if ( pComponentPrivate->nCurState == OMX_StatePause ) {
1787            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1788                                                   OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
1789        } else if ( pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StateIdle ) {
1790
1791            pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1792            eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlPause,NULL);
1793            if ( eError != OMX_ErrorNone ) {
1794                OMX_PRDSP4(pComponentPrivate->dbg, "Error during EMMCodecControlPause. Error: %d.\n", eError );
1795                goto EXIT;
1796            }
1797            /*
1798            pComponentPrivate->nCurState = OMX_StatePause;
1799            /* Decrement reference count with signal enabled */
1800            if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
1801                   return OMX_ErrorUndefined;
1802            }
1803
1804            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1805                                                   OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
1806        } else {
1807            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1808                                                   OMX_EventError, OMX_ErrorIncorrectStateTransition, OMX_TI_ErrorMinor , NULL);
1809            OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
1810        }
1811        break;
1812
1813
1814    case OMX_StateInvalid:
1815        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateInvalid::\n");
1816        if ( pComponentPrivate->nCurState == OMX_StateInvalid ) {
1817            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle, pComponentPrivate->pHandle->pApplicationPrivate,
1818                                                   OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
1819        }
1820        if ( pComponentPrivate->nCurState != OMX_StateLoaded ) {
1821            OMX_PRBUFFER2(pComponentPrivate->dbg, "HandleJpegEncInternalFlush \n");
1822            eError = HandleJpegEncInternalFlush(pComponentPrivate, nParam1);
1823            }
1824
1825            pComponentPrivate->nCurState = OMX_StateInvalid;
1826
1827            if(pComponentPrivate->nToState == OMX_StateInvalid){ /*if the IL client call directly send to invalid state*/
1828                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1829                                       pComponentPrivate->pHandle->pApplicationPrivate,
1830                                       OMX_EventCmdComplete,
1831                                       OMX_CommandStateSet,
1832                                       pComponentPrivate->nCurState,
1833                                       NULL);
1834            }
1835            else{ /*When the component go to invalid state by it self*/
1836                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1837                                       pComponentPrivate->pHandle->pApplicationPrivate,
1838                                       OMX_EventError,
1839                                       OMX_ErrorInvalidState,
1840                                       OMX_TI_ErrorSevere,
1841                                       NULL);
1842            }
1843        break;
1844
1845    case OMX_StateLoaded:
1846        if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
1847            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1848                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1849                                                   OMX_EventError,
1850                                                   OMX_ErrorSameState,
1851                                                   OMX_TI_ErrorMinor ,
1852                                                   NULL);
1853
1854        } else if ( pComponentPrivate->nCurState == OMX_StateIdle ||
1855                    pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
1856            /* Ports have to be unpopulated before transition completes */
1857            OMX_PRSTATE2(pComponentPrivate->dbg, "from idle to loaded\n");
1858
1859            pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1860            if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
1861                pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1862                OMX_PRDSP2(pComponentPrivate->dbg, "try to close library again %p\n", pComponentPrivate->pLCML);
1863                LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
1864                OMX_PRDSP2(pComponentPrivate->dbg, "after close library again %p\n", pComponentPrivate->pLCML);
1865                pComponentPrivate->pLCML = NULL;
1866                dlclose(pComponentPrivate->pDllHandle);
1867                pComponentPrivate->isLCMLActive = 0;
1868
1869            }
1870            OMX_PRDSP2(pComponentPrivate->dbg, "after release LCML\n");
1871#ifdef __PERF_INSTRUMENTATION__
1872            PERF_Boundary(pComponentPrivate->pPERFcomp,
1873                          PERF_BoundaryStart | PERF_BoundaryCleanup);
1874#endif
1875
1876#ifdef UNDER_CE
1877            nTimeout=0x0;
1878
1879                while ( 1 )
1880                {
1881                    if ( (pPortDefOut->bPopulated == OMX_FALSE) )
1882                    {
1883                        OMX_PRDSP2(pComponentPrivate->dbg, "Thread Sending Cmd EMMCodecControlDestroy\n");
1884
1885
1886                        break;
1887                    } else if ( nTimeout++ > JPEGENC_TIMEOUT )
1888
1889                    {
1890                        OMX_PRBUFFER4(pComponentPrivate->dbg, "Timeout ...\n");
1891                        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1892                                       pComponentPrivate->pHandle->pApplicationPrivate,
1893                                       OMX_EventError,
1894                                       OMX_ErrorPortUnresponsiveDuringAllocation,
1895                                       OMX_TI_ErrorMajor,
1896                                       "Not response Port -Loaded");
1897                        OMX_PRBUFFER4(pComponentPrivate->dbg, "Not all ports are unpopulated!\n");
1898                        break;
1899                    }
1900                    /* Sleep for a while, so the application thread can allocate buffers */
1901            sched_yield();
1902            }
1903#else
1904
1905            pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
1906            while ( pPortDefIn->bPopulated || pPortDefOut->bPopulated) {
1907                OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1908                pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->jpege_mutex_app);
1909            }
1910            pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
1911#endif
1912
1913#ifdef __PERF_INSTRUMENTATION__
1914                        PERF_Boundary(pComponentPrivate->pPERFcomp,
1915                                      PERF_BoundaryComplete | PERF_BoundaryCleanup);
1916#endif
1917
1918#ifdef RESOURCE_MANAGER_ENABLED
1919            if (pComponentPrivate->nCurState != OMX_StateWaitForResources) {
1920                eError= RMProxy_NewSendCommand(pHandle,  RMProxy_FreeResource, OMX_JPEG_Encoder_COMPONENT, 0, 3456, NULL);
1921                if (eError != OMX_ErrorNone) {
1922                    OMX_PRMGR4(pComponentPrivate->dbg, "Cannot Free Resources\n");
1923                    pComponentPrivate->cbInfo.EventHandler(pHandle,
1924                                                           pHandle->pApplicationPrivate,
1925                                                           OMX_EventError,
1926                                                           OMX_ErrorUndefined,
1927                                                           OMX_TI_ErrorMajor,
1928                                                           NULL);
1929                    break;
1930                }
1931            }
1932#endif
1933
1934            pComponentPrivate->nCurState = OMX_StateLoaded;
1935
1936            if ((pComponentPrivate->nCurState == OMX_StateIdle) &&
1937                 (pComponentPrivate->bPreempted == 1 )){
1938
1939                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1940                                                       pComponentPrivate->pHandle->pApplicationPrivate,
1941                                                       OMX_EventError,
1942                                                       OMX_ErrorResourcesLost,
1943                                                       OMX_TI_ErrorSevere,
1944                                                       NULL);
1945                pComponentPrivate->bPreempted = 0;
1946
1947            }
1948            else {
1949                /* Decrement reference count with signal enabled */
1950                if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
1951                  return OMX_ErrorUndefined;
1952                }
1953
1954                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1955                                                       pComponentPrivate->pHandle->pApplicationPrivate,
1956                                                       OMX_EventCmdComplete,
1957                                                       OMX_CommandStateSet,
1958                                                       OMX_StateLoaded,
1959                                                       NULL);
1960            }
1961
1962        }
1963        else {
1964
1965            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1966                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1967                                                   OMX_EventError,
1968                                                   OMX_ErrorIncorrectStateTransition,
1969                                                   OMX_TI_ErrorMinor,
1970                                                   NULL);
1971        }
1972        break;
1973
1974    case OMX_StateWaitForResources:
1975        if ( pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
1976            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1977                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
1978                                                                            OMX_EventError,
1979                                                                            OMX_ErrorSameState,
1980                                                                            OMX_TI_ErrorMinor,
1981                                                                            NULL);
1982        } else if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
1983
1984#ifdef RESOURCE_MANAGER_ENABLED
1985            eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateWaitForResources, 3456, NULL);
1986            if (eError != OMX_ErrorNone) {
1987                OMX_PRMGR4(pComponentPrivate->dbg, "RMProxy_NewSendCommand(OMX_StateWaitForResources) failed\n");
1988                pComponentPrivate->cbInfo.EventHandler(pHandle,
1989                                                       pHandle->pApplicationPrivate,
1990                                                       OMX_EventError,
1991                                                       OMX_ErrorUndefined,
1992                                                       OMX_TI_ErrorSevere,
1993                                                       NULL);
1994                break;
1995            }
1996#endif
1997
1998            pComponentPrivate->nCurState = OMX_StateWaitForResources;
1999            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2000                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
2001                                                                            OMX_EventCmdComplete,
2002                                                                            OMX_CommandStateSet,
2003                                                                            pComponentPrivate->nCurState,
2004                                                                            NULL);
2005        } else {
2006            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2007                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
2008                                                                            OMX_EventError,
2009                                                                            OMX_ErrorIncorrectStateTransition,
2010                                                                            OMX_TI_ErrorMinor,
2011                                                                            NULL);
2012        }
2013        break;
2014
2015    case OMX_StateMax:
2016        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateMax::\n");
2017        break;
2018    } /* End of Switch */
2019
2020
2021    EXIT:
2022    return eError;
2023}
2024
2025
2026OMX_ERRORTYPE HandleJpegEncFreeOutputBufferFromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
2027{
2028
2029    OMX_ERRORTYPE eError = OMX_ErrorNone;
2030    OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
2031    /* IUALG_Buf *ptParam = NULL; */
2032    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
2033    LCML_DSP_INTERFACE* pLcmlHandle = NULL;
2034    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2035    int ret;
2036
2037    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeOutputBufferFromApp function\n");
2038
2039    pLcmlHandle = (LCML_DSP_INTERFACE *)(pComponentPrivate->pLCML);
2040    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
2041
2042    ret = read(pComponentPrivate->free_outBuf_Q[0], &pBuffHead, sizeof(pBuffHead));
2043    if ( ret == -1 ) {
2044        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
2045        goto EXIT;
2046    }
2047
2048    if (pBuffHead != NULL) {
2049        pBuffPrivate = pBuffHead->pOutputPortPrivate;
2050    } else {
2051        OMX_PRBUFFER4(pComponentPrivate->dbg, "Error while reading pBuffHead from the pipe\n");
2052        goto EXIT;
2053    }
2054
2055    if (pComponentPrivate->nCurState != OMX_StatePause || pComponentPrivate->nToState != OMX_StatePause) {
2056
2057   if ((pComponentPrivate->nCurState != OMX_StateExecuting ) ||
2058       (pComponentPrivate->nToState == OMX_StateIdle) ||
2059         (pPortDefOut->bEnabled == OMX_FALSE)) {
2060         if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2061            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2062            pComponentPrivate->nOutPortOut ++;
2063            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
2064                    pComponentPrivate->nInPortIn,
2065                    pComponentPrivate->nInPortOut,
2066                    pComponentPrivate->nOutPortIn,
2067                    pComponentPrivate->nOutPortOut);
2068            OMX_PRBUFFER2(pComponentPrivate->dbg, "FillBufferDone (incorrect state %d) %p\n", pComponentPrivate->nCurState, pBuffHead);
2069            pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
2070                    pComponentPrivate->pHandle->pApplicationPrivate,
2071                    pBuffHead);
2072         }
2073        goto EXIT;
2074    }
2075    }
2076
2077    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncFreeOutputBufferFromApp) %lu %lu %lu %lu\n",
2078                    pComponentPrivate->nInPortIn,
2079                    pComponentPrivate->nInPortOut,
2080                    pComponentPrivate->nOutPortIn,
2081                    pComponentPrivate->nOutPortOut);
2082
2083#ifdef __PERF_INSTRUMENTATION__
2084    PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2085                      pBuffHead->pBuffer,
2086                      0,
2087                      PERF_ModuleCommonLayer);
2088#endif
2089
2090    /* ptParam =  (IUALG_Buf *)pBuffPrivate->pUALGParams; */
2091    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
2092
2093#ifdef __JPEG_OMX_PPLIB_ENABLED__
2094    if (pComponentPrivate->pOutParams != NULL)
2095    {
2096        pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams - PADDING_128_BYTE;
2097        OMX_FREE(pComponentPrivate->pOutParams);
2098    }
2099    OMX_MALLOC(pComponentPrivate->pOutParams,sizeof(PPLIB_UALGRunTimeParam_t) + PADDING_256_BYTE);
2100    pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams + PADDING_128_BYTE;
2101
2102    if (pComponentPrivate->pOutParams != NULL)
2103    {
2104    }
2105    else
2106    {
2107        goto EXIT;
2108    }
2109
2110    eError = SendDynamicPPLibParam(pComponentPrivate,pComponentPrivate->pOutParams);
2111       if (eError != OMX_ErrorNone ) {
2112           goto EXIT;
2113       }
2114
2115
2116       eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2117                                 EMMCodecOuputBuffer,
2118                                 pBuffHead->pBuffer,
2119                                 pPortDefOut->nBufferSize,
2120                                 0,
2121                                 (OMX_U8 *)pComponentPrivate->pOutParams,
2122                                 sizeof(PPLIB_UALGRunTimeParam_t),
2123                                 (OMX_U8 *)  pBuffHead);
2124
2125#else
2126    eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2127                              EMMCodecOuputBuffer,
2128                              pBuffHead->pBuffer,
2129                              pPortDefOut->nBufferSize,
2130                              0,
2131                              NULL,
2132                              0,
2133                              (OMX_U8 *)  pBuffHead);
2134#endif
2135
2136    OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
2137
2138    EXIT:
2139    return eError;
2140}
2141
2142OMX_U32 CalculateParamsSize (JPEGENC_COMPONENT_PRIVATE *pComponentPrivate) {
2143
2144    OMX_U32 i = 0;
2145
2146    i+= 4; /* 4 bytes for the size of the whole array */
2147
2148    /* Set Custom Quantization Table */
2149    if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
2150        i+=4;  /* 4 bytes for the Quantization table TAG */
2151        i+=4;  /* 4 bytes for the size of this TAG */
2152        i+=256; /* 256 bytes for the Quantization table data*/
2153    }
2154
2155    /* Set Custom Huffman Table */
2156    if (pComponentPrivate->bSetHuffmanTable) {
2157        i+=4;  /* 4 bytes for the Huffman table TAG */
2158        i+=4;  /* 4 bytes for the size of this TAG */
2159        if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
2160            i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)));
2161        }
2162        else {
2163            i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE);
2164        }
2165    }
2166
2167    /* handle APP0 marker (JFIF)*/
2168    if(pComponentPrivate->sAPP0.bMarkerEnabled) {
2169        i+=4;  /* 4 bytes for the Number of buffers TAG */
2170        i+=4;  /* 4 bytes for the size of this TAG */
2171        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2172        i+=4;  /* 4 bytes for the buffer0  TAG */
2173
2174        /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
2175        if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
2176        || pComponentPrivate->sAPP0.nMarkerSize <= 0) {
2177            i+=4;   /* 4 bytes for the size of this TAG */
2178            i+=4;   /* 4 bytes for the actual data of this TAG */
2179        }
2180        else {
2181            i+=4;   /* 4 bytes for the size of this TAG */
2182            i += (pComponentPrivate->sAPP0.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2183            if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
2184                i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2185            }
2186        }
2187        /* if thumbnail is set, configure it accordingly */
2188        if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
2189            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2190            i+=4;  /* 4 bytes for the size of this TAG */
2191            i+=4;  /* 4 bytes for the actual data for this TAG */
2192
2193            i+=4;  /* 4 bytes for the THUMB W TAG */
2194            i+=4;  /* 4 bytes for the size of this TAG */
2195            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2196
2197            i+=4;  /* 4 bytes for the THUMB H TAG */
2198            i+=4;  /* 4 bytes for the size of this TAG */
2199            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2200        }
2201    }
2202
2203    /* handle APP1 marker (EXIF)*/
2204    if (pComponentPrivate->sAPP1.bMarkerEnabled) {
2205        i+=4;  /* 4 bytes for the Number of buffers TAG */
2206        i+=4;  /* 4 bytes for the size of this TAG */
2207        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2208        i+=4;  /* 4 bytes for the buffer0  TAG */
2209
2210    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2211    	if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
2212            i+=4;   /* 4 bytes for the size of this TAG */
2213            i+=4;   /* 4 bytes for the actual data of this TAG */
2214            i+=4;   /* 4 bytes for the actual data of this TAG */
2215    	}
2216    	else {
2217    	    i+=4;   /* 4 bytes for the size of this TAG */
2218                i += (pComponentPrivate->sAPP1.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2219                if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
2220                    i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2221                }
2222    	}
2223
2224    	/* if thumbnail is set, configure it accordingly */
2225    	if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
2226            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2227            i+=4;  /* 4 bytes for the size of this TAG */
2228            i+=4;  /* 4 bytes for the actual data for this TAG */
2229
2230            i+=4;  /* 4 bytes for the THUMB W TAG */
2231            i+=4;  /* 4 bytes for the size of this TAG */
2232            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2233
2234            i+=4;  /* 4 bytes for the THUMB H TAG */
2235            i+=4;  /* 4 bytes for the size of this TAG */
2236            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2237    	}
2238    }
2239
2240
2241    /* handle APP5 marker */
2242    if (pComponentPrivate->sAPP5.bMarkerEnabled) {
2243        i+=4;  /* 4 bytes for the Number of buffers TAG */
2244        i+=4;  /* 4 bytes for the size of this TAG */
2245        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2246        i+=4;  /* 4 bytes for the buffer0  TAG */
2247
2248    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2249    	if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
2250            i+=4;   /* 4 bytes for the size of this TAG */
2251            i+=4;   /* 4 bytes for the actual data of this TAG */
2252            i+=4;   /* 4 bytes for the actual data of this TAG */
2253    	}
2254    	else {
2255    	    i+=4;   /* 4 bytes for the size of this TAG */
2256                i += (pComponentPrivate->sAPP5.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2257                if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
2258                    i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2259                }
2260    	}
2261
2262    	/* if thumbnail is set, configure it accordingly */
2263    	if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
2264            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2265            i+=4;  /* 4 bytes for the size of this TAG */
2266            i+=4;  /* 4 bytes for the actual data for this TAG */
2267
2268            i+=4;  /* 4 bytes for the THUMB W TAG */
2269            i+=4;  /* 4 bytes for the size of this TAG */
2270            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2271
2272            i+=4;  /* 4 bytes for the THUMB H TAG */
2273            i+=4;  /* 4 bytes for the size of this TAG */
2274            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2275    	}
2276    }
2277    /* handle APP13 marker */
2278    if (pComponentPrivate->sAPP13.bMarkerEnabled) {
2279        i+=4;  /* 4 bytes for the Number of buffers TAG */
2280        i+=4;  /* 4 bytes for the size of this TAG */
2281        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2282        i+=4;  /* 4 bytes for the buffer0  TAG */
2283
2284    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2285    	if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
2286            i+=4;   /* 4 bytes for the size of this TAG */
2287            i+=4;   /* 4 bytes for the actual data of this TAG */
2288            i+=4;   /* 4 bytes for the actual data of this TAG */
2289    	}
2290    	else {
2291            i+=4;   /* 4 bytes for the size of this TAG */
2292            i += (pComponentPrivate->sAPP13.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2293            if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
2294                i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2295            }
2296    	}
2297    }
2298
2299    /* comment flag needed */
2300    i+=4;   /* 4 bytes for the size of this TAG */
2301
2302    /* handle CommentFlag */
2303    if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
2304        i+=4;  /* 4 bytes for the Comment TAG */
2305        i+=4;  /* 4 bytes for the size of this TAG */
2306            /* comment string upper limit is 256 bytes */
2307            i+=256;
2308    }
2309    else {
2310        i+=4;  /* 4 bytes for the Comment TAG */
2311        i+=4;  /* 4 bytes for the size of this TAG */
2312    }
2313
2314    return i;
2315}
2316
2317static OMX_ERRORTYPE SetJpegEncInPortParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32* new_params)
2318{
2319    OMX_ERRORTYPE eError = OMX_ErrorNone;
2320    int i = 1;
2321
2322    /* Set Custom Quantization Table */
2323    if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
2324        new_params[i++] = DYNPARAMS_QUANTTABLE;
2325        new_params[i++] = 256; /* 2 tables * 64 entries * 2(16bit entries) */
2326        OMX_U16 *temp = (OMX_U16 *)&new_params[i];
2327        int j, k;
2328        for (j = 0; j < 64; j++) {
2329            temp[j] = pComponentPrivate->pCustomLumaQuantTable->nQuantizationMatrix[j];
2330        }
2331        for (k = 0; k < 64; k++, j++) {
2332            temp[j] = pComponentPrivate->pCustomChromaQuantTable->nQuantizationMatrix[k];
2333        }
2334        i += 64; /* 256 / 4 */
2335    }
2336
2337    /* Set Custom Huffman Table */
2338    if (pComponentPrivate->bSetHuffmanTable) {
2339        new_params[i++] = DYNPARAMS_HUFFMANTABLE;
2340        new_params[i++] = sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE); /* 2572 % 4 = 0 */
2341
2342        memcpy((OMX_U8 *)(&new_params[i]), &(pComponentPrivate->pHuffmanTable->sHuffmanTable), sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE));
2343        if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
2344            i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)))/4 ;
2345        }
2346        else {
2347           i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE)/4;
2348        }
2349    }
2350
2351    /* handle APP0 marker (JFIF)*/
2352    if(pComponentPrivate->sAPP0.bMarkerEnabled) {
2353        new_params[i++] = APP0_NUMBUF;
2354        new_params[i++] = 4;
2355        new_params[i++] = 1;
2356
2357        /* set default APP0 BUFFER */
2358        new_params[i++] = APP0_BUFFER;
2359
2360        /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
2361        if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
2362        	|| pComponentPrivate->sAPP0.nMarkerSize <= 0) {
2363            new_params[i++] = 4;
2364            new_params[i++] = 0;
2365        }
2366        else {
2367            new_params[i++] = pComponentPrivate->sAPP0.nMarkerSize;
2368            memcpy(new_params + i, pComponentPrivate->sAPP0.pMarkerBuffer, pComponentPrivate->sAPP0.nMarkerSize);
2369            i += pComponentPrivate->sAPP0.nMarkerSize / 4;
2370            if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
2371            	i ++;
2372            }
2373        }
2374
2375        /* if thumbnail is set, configure it accordingly */
2376        if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
2377            new_params[i++] = APP0_THUMB_INDEX;
2378            new_params[i++] = 4;
2379            new_params[i++] = 1;
2380
2381            new_params[i++] = APP0_THUMB_W;
2382            new_params[i++] = 4;
2383            new_params[i++] = pComponentPrivate->sAPP0.nThumbnailWidth;
2384
2385            new_params[i++] = APP0_THUMB_H;
2386            new_params[i++] = 4;
2387            new_params[i++] = pComponentPrivate->sAPP0.nThumbnailHeight;
2388        }
2389    }
2390
2391    /* handle APP1 marker (EXIF)*/
2392    if(pComponentPrivate->sAPP1.bMarkerEnabled) {
2393        new_params[i++] = APP1_NUMBUF;
2394        new_params[i++] = 4;
2395        new_params[i++] = 1;
2396
2397        /* set default APP1 BUFFER */
2398        new_params[i++] = APP1_BUFFER;
2399
2400        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2401        if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
2402            new_params[i++] = 8;
2403            new_params[i++] = 0;
2404            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2405        }
2406        else {
2407            new_params[i++] = pComponentPrivate->sAPP1.nMarkerSize;
2408            memcpy(new_params + i, pComponentPrivate->sAPP1.pMarkerBuffer, pComponentPrivate->sAPP1.nMarkerSize);
2409            i += pComponentPrivate->sAPP1.nMarkerSize / 4;
2410            if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
2411            	i ++;
2412            }
2413        }
2414
2415        /* if thumbnail is set, configure it accordingly */
2416        if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
2417            new_params[i++] = APP1_THUMB_INDEX;
2418            new_params[i++] = 4;
2419            new_params[i++] = 1;
2420
2421            new_params[i++] = APP1_THUMB_W;
2422            new_params[i++] = 4;
2423            new_params[i++] = pComponentPrivate->sAPP1.nThumbnailWidth;
2424
2425            new_params[i++] = APP1_THUMB_H;
2426            new_params[i++] = 4;
2427            new_params[i++] = pComponentPrivate->sAPP1.nThumbnailHeight;
2428        }
2429    }
2430
2431
2432    /* handle APP5 marker */
2433    if(pComponentPrivate->sAPP5.bMarkerEnabled) {
2434        new_params[i++] = APP5_NUMBUF;
2435        new_params[i++] = 4;
2436        new_params[i++] = 1;
2437
2438        /* set default APP5 BUFFER */
2439        new_params[i++] = APP5_BUFFER;
2440
2441        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2442        if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
2443            new_params[i++] = 8;
2444            new_params[i++] = 0;
2445            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2446        }
2447        else {
2448            new_params[i++] = pComponentPrivate->sAPP5.nMarkerSize;
2449            memcpy(new_params + i, pComponentPrivate->sAPP5.pMarkerBuffer, pComponentPrivate->sAPP5.nMarkerSize);
2450            i += pComponentPrivate->sAPP5.nMarkerSize / 4;
2451            if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
2452            	i ++;
2453            }
2454        }
2455
2456        /* if thumbnail is set, configure it accordingly */
2457        if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
2458            new_params[i++] = APP5_THUMB_INDEX;
2459            new_params[i++] = 4;
2460            new_params[i++] = 1;
2461
2462            new_params[i++] = APP5_THUMB_W;
2463            new_params[i++] = 4;
2464            new_params[i++] = pComponentPrivate->sAPP5.nThumbnailWidth;
2465
2466            new_params[i++] = APP5_THUMB_H;
2467            new_params[i++] = 4;
2468            new_params[i++] = pComponentPrivate->sAPP5.nThumbnailHeight;
2469        }
2470    }
2471
2472
2473    /* handle APP13 marker */
2474    if(pComponentPrivate->sAPP13.bMarkerEnabled) {
2475        new_params[i++] = APP13_NUMBUF;
2476        new_params[i++] = 4;
2477        new_params[i++] = 1;
2478
2479        /* set default APP13 BUFFER */
2480        new_params[i++] = APP13_BUFFER;
2481
2482        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2483        if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
2484            new_params[i++] = 8;
2485            new_params[i++] = 0;
2486            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2487        }
2488        else {
2489            new_params[i++] = pComponentPrivate->sAPP13.nMarkerSize;
2490            memcpy(new_params + i, pComponentPrivate->sAPP13.pMarkerBuffer, pComponentPrivate->sAPP13.nMarkerSize);
2491            i += pComponentPrivate->sAPP13.nMarkerSize / 4;
2492            if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
2493            	i ++;
2494            }
2495        }
2496    }
2497
2498    new_params[i++] = COMMENT_BUFFER;
2499
2500    /* handle CommentFlag */
2501    if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
2502        new_params[i++] = strlen((char *)pComponentPrivate->pString_Comment)  + 4 ;
2503        new_params[i++] = 0;
2504        strncpy((char *)(new_params+i), (char *)pComponentPrivate->pString_Comment, 255);
2505    }
2506    else {
2507        new_params[i++] = 4;
2508        new_params[i++] = 0;
2509    }
2510
2511    /* now that we know the final size of the buffer, we can set it accordingly */
2512    new_params[0] = i * sizeof(OMX_U32);
2513
2514/*
2515    printf("=========DUMP of new_params array=============\n");
2516    int j;
2517    for (j=0; j< i*4; j++) {
2518    printf("new_params[%d] = 0x%x\n", j, *(((unsigned char *)new_params)+j));
2519    }
2520    printf("================================\n");
2521*/
2522    return eError;
2523}
2524
2525OMX_ERRORTYPE SetJpegEncInParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
2526{
2527    OMX_ERRORTYPE eError = OMX_ErrorNone;
2528    OMX_U8 *p = NULL;
2529    OMX_U32 params_size;
2530
2531    if (pComponentPrivate->InParams.pInParams) {
2532        p = (OMX_U8 *)pComponentPrivate->InParams.pInParams;
2533        p -= 128;
2534        OMX_FREE(p);
2535        pComponentPrivate->InParams.pInParams = NULL;
2536    }
2537
2538   /*alloc enough memory for params array*/
2539    params_size = CalculateParamsSize(pComponentPrivate);
2540    OMX_MALLOC(p, params_size + 256);
2541    p += 128;
2542    pComponentPrivate->InParams.pInParams = (OMX_U32 *)p;
2543    p = NULL;
2544    eError = SetJpegEncInPortParams(pComponentPrivate, pComponentPrivate->InParams.pInParams);
2545
2546EXIT:
2547    return eError;
2548}
2549
2550OMX_ERRORTYPE HandleJpegEncDataBuf_FromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
2551{
2552    OMX_ERRORTYPE eError = OMX_ErrorNone;
2553    OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
2554    LCML_DSP_INTERFACE* pLcmlHandle = NULL;
2555    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
2556    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
2557    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2558    int ret;
2559
2560    OMX_CHECK_PARAM(pComponentPrivate);
2561
2562    pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
2563    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
2564    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
2565
2566    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleDataBuf_FromApp function\n");
2567    ret = read(pComponentPrivate->filled_inpBuf_Q[0], &(pBuffHead), sizeof(pBuffHead));
2568    if ( ret == -1 ) {
2569        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
2570    }
2571
2572    if (pBuffHead != NULL) {
2573        pBuffPrivate = pBuffHead->pInputPortPrivate;
2574    } else {
2575        eError = OMX_ErrorInsufficientResources;
2576          goto EXIT;
2577        }
2578
2579   if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2580        /* already returned to client */
2581        OMX_PRBUFFER4(pComponentPrivate->dbg, "this buffer %p already returned to client\n", pBuffHead);
2582        goto EXIT;
2583    }
2584
2585   if ((pComponentPrivate->nCurState != OMX_StateExecuting) ||
2586       (pComponentPrivate->nToState == OMX_StateIdle) ||
2587        (pPortDefIn->bEnabled == OMX_FALSE)) {
2588            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2589                pComponentPrivate->nInPortOut ++;
2590                pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2591                OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty input buffer) %lu %lu %lu %lu\n",
2592                    pComponentPrivate->nInPortIn,
2593                    pComponentPrivate->nInPortOut,
2594                    pComponentPrivate->nOutPortIn,
2595                    pComponentPrivate->nOutPortOut);
2596                pComponentPrivate->cbInfo.EmptyBufferDone(
2597                           pComponentPrivate->pHandle,
2598                           pComponentPrivate->pHandle->pApplicationPrivate,
2599                           pBuffHead);
2600             }
2601             goto EXIT;
2602    }
2603
2604    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncDataBuf_FromApp) %lu %lu %lu %lu\n",
2605                    pComponentPrivate->nInPortIn,
2606                    pComponentPrivate->nInPortOut,
2607                    pComponentPrivate->nOutPortIn,
2608                    pComponentPrivate->nOutPortOut);
2609
2610#ifdef __PERF_INSTRUMENTATION__
2611    PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2612                      pBuffHead->pBuffer,
2613                      pBuffHead->nFilledLen,
2614                      PERF_ModuleCommonLayer);
2615#endif
2616
2617    if ((pBuffHead->nFlags == OMX_BUFFERFLAG_EOS) && (pBuffHead->nAllocLen == 0)) {
2618            OMX_PRBUFFER2(pComponentPrivate->dbg, "BufferFlag Set!!\n");
2619            pComponentPrivate->nFlags = OMX_BUFFERFLAG_EOS;
2620        pBuffHead->nFlags = 0;
2621    }
2622#if 0
2623    eError = SendDynamicParam(pComponentPrivate);
2624    if (eError != OMX_ErrorNone ) {
2625            OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
2626            goto EXIT;
2627    }
2628#endif
2629
2630    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nAllocLen = %d\n",(int)pBuffHead->nAllocLen);
2631    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->pBuffer = %p\n",pBuffHead->pBuffer);
2632    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nFilledLen = %d\n",(int)pBuffHead->nFilledLen);
2633    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead = %p\n",pBuffHead);
2634
2635    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
2636
2637    OMX_PRDSP2(pComponentPrivate->dbg, "Input: before queue buffer %p\n", pBuffHead);
2638        eError = LCML_QueueBuffer(
2639                                  pLcmlHandle->pCodecinterfacehandle,
2640                                  EMMCodecInputBuffer,
2641                                  pBuffHead->pBuffer,
2642                                  pPortDefIn->nBufferSize,
2643                                  pBuffHead->nFilledLen,
2644                                  (OMX_U8 *) pComponentPrivate->InParams.pInParams,
2645                                  pComponentPrivate->InParams.pInParams[0],
2646                                  (OMX_U8 *)pBuffHead);
2647
2648    OMX_PRDSP2(pComponentPrivate->dbg, "Input: after queue buffer %p\n", pBuffHead);
2649
2650    if ( eError ) {
2651        eError = OMX_ErrorInsufficientResources;
2652        OMX_PRDSP4(pComponentPrivate->dbg, "OMX_ErrorInsufficientResources\n");
2653        goto EXIT;
2654    }
2655    OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
2656    EXIT:
2657
2658    return eError;
2659}
2660
2661
2662OMX_ERRORTYPE HandleJpegEncDataBuf_FromDsp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead)
2663{
2664
2665    OMX_ERRORTYPE eError                 = OMX_ErrorNone;
2666    OMX_BUFFERHEADERTYPE* pInpBuf        = NULL;
2667    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2668
2669    OMX_CHECK_PARAM(pComponentPrivate);
2670
2671    pInpBuf = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[0]->pBufferHdr;
2672
2673    pBuffPrivate = pBuffHead->pOutputPortPrivate;
2674
2675        if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2676           OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
2677           pComponentPrivate->nOutPortOut --;
2678           OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return output buffer) %lu %lu %lu %lu\n",
2679                                        pComponentPrivate->nInPortIn,
2680                                        pComponentPrivate->nInPortOut,
2681                                        pComponentPrivate->nOutPortIn,
2682                                        pComponentPrivate->nOutPortOut);
2683
2684           goto EXIT;
2685        }
2686
2687#ifdef __PERF_INSTRUMENTATION__
2688        PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2689                          pBuffHead->pBuffer,
2690                          pBuffHead->nFilledLen,
2691                          PERF_ModuleHLMM);
2692#endif
2693    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2694
2695        if (pBuffHead->pMarkData) {
2696           OMX_PRBUFFER2(pComponentPrivate->dbg, "get Mark buffer %p %p %p\n", pBuffHead->pMarkData, pBuffHead->hMarkTargetComponent, pComponentPrivate->pHandle);
2697        }
2698
2699        if (pBuffHead->pMarkData && pBuffHead->hMarkTargetComponent == pComponentPrivate->pHandle) {
2700           OMX_PRBUFFER2(pComponentPrivate->dbg, "send OMX_MarkEvent\n");
2701           pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2702                                                pComponentPrivate->pHandle->pApplicationPrivate,
2703                                                OMX_EventMark,
2704                                                JPEGENC_OUT_PORT,
2705                                                0,
2706                                                pBuffHead->pMarkData);
2707        }
2708
2709    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
2710                    pComponentPrivate->nInPortIn,
2711                    pComponentPrivate->nInPortOut,
2712                    pComponentPrivate->nOutPortIn,
2713                    pComponentPrivate->nOutPortOut);
2714    OMX_PRBUFFER1(pComponentPrivate->dbg, "Output: before fillbufferdone %p\n", pBuffHead);
2715        pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
2716                        pComponentPrivate->pHandle->pApplicationPrivate,
2717                        pBuffHead);
2718
2719    if ( pComponentPrivate->nFlags & OMX_BUFFERFLAG_EOS )   {
2720
2721        pBuffHead->nFlags |= OMX_BUFFERFLAG_EOS;
2722        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
2723                                                pComponentPrivate->pHandle->pApplicationPrivate,
2724                                                OMX_EventBufferFlag,
2725                                                JPEGENC_OUT_PORT,
2726                                                OMX_BUFFERFLAG_EOS,
2727                                                NULL);
2728
2729        pComponentPrivate->nFlags = 0;
2730    }
2731
2732    EXIT:
2733    return eError;
2734}
2735
2736
2737
2738OMX_ERRORTYPE HandleJpegEncFreeDataBuf( JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead )
2739{
2740    OMX_ERRORTYPE eError = OMX_ErrorNone;
2741    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
2742    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2743    OMX_HANDLETYPE hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
2744
2745    OMX_CHECK_PARAM(pComponentPrivate);
2746    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeDataBuf function \n");
2747    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
2748
2749    /* pBuffHead->nAllocLen = pPortDefIn->nBufferSize; */
2750    pBuffPrivate = pBuffHead->pInputPortPrivate;
2751
2752    OMX_PRCOMM2(pComponentPrivate->dbg, "hTunnelComponent = %p\n" ,hTunnelComponent );
2753    OMX_PRINT1(pComponentPrivate->dbg, "pComponentPrivate->pHandle = %p\n",pComponentPrivate->pHandle);
2754
2755    if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2756        OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
2757        pComponentPrivate->nInPortOut --;
2758        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return input buffer) %lu %lu %lu %lu\n",
2759                                        pComponentPrivate->nInPortIn,
2760                                        pComponentPrivate->nInPortOut,
2761                                        pComponentPrivate->nOutPortIn,
2762                                        pComponentPrivate->nOutPortOut);
2763
2764           goto EXIT;
2765    }
2766
2767    if(hTunnelComponent != NULL)
2768    {
2769
2770#ifdef __PERF_INSTRUMENTATION__
2771            PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2772                              pBuffHead->pBuffer,
2773                              0,
2774                              PERF_ModuleLLMM);
2775#endif
2776    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
2777        eError = OMX_FillThisBuffer(hTunnelComponent, pBuffHead);
2778
2779    }
2780    else {
2781
2782#ifdef __PERF_INSTRUMENTATION__
2783            PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2784                              pBuffHead->pBuffer,
2785                              0,
2786                              PERF_ModuleHLMM);
2787#endif
2788        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2789        OMX_PRBUFFER2(pComponentPrivate->dbg, "before emptybufferdone in HandleJpegEncFreeDataBuf %p\n", pBuffHead);
2790        pComponentPrivate->cbInfo.EmptyBufferDone(
2791                 pComponentPrivate->pHandle,
2792                 pComponentPrivate->pHandle->pApplicationPrivate,
2793                 pBuffHead);
2794    }
2795
2796    EXIT:
2797    return eError;
2798
2799}
2800
2801
2802/* -------------------------------------------------------------------*/
2803/**
2804  *  Callback() function will be called LCML component to write the msg
2805  *
2806  * @param msgBuffer                 This buffer will be returned by the LCML
2807  *
2808  * @retval OMX_NoError              Success, ready to roll
2809  *         OMX_Error_BadParameter   The input parameter pointer is null
2810 **/
2811/*-------------------------------------------------------------------*/
2812OMX_ERRORTYPE JpegEncLCML_Callback (TUsnCodecEvent event,void * argsCb [10])
2813{
2814    OMX_ERRORTYPE eError = OMX_ErrorNone;
2815    JPEGENC_BUFFER_PRIVATE *pBuffPrivate = NULL;
2816    JPEG_PORT_TYPE *pPortType = NULL;
2817    int i;
2818
2819    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
2820    OMX_COMPONENTTYPE *pHandle;
2821
2822    if ( ((LCML_DSP_INTERFACE*)argsCb[6] ) != NULL ) {
2823        pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)((LCML_DSP_INTERFACE*)argsCb[6])->pComponentPrivate;
2824        pHandle = (OMX_COMPONENTTYPE *)pComponentPrivate->pHandle;
2825    }
2826    else {
2827        OMXDBG_PRINT(stderr, DSP, 5, 0, "wrong in LCML callback, exit\n");
2828	 goto EXIT;
2829    }
2830    OMX_PRDSP0(pComponentPrivate->dbg, "Event = %d\n", event);
2831
2832    if ( event == EMMCodecBufferProcessed ) {
2833    if ( (int)argsCb [0] == EMMCodecOuputBuffer ) {
2834        OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
2835        pBuffPrivate = pBuffHead->pOutputPortPrivate;
2836
2837        pComponentPrivate->nOutPortOut ++;
2838#ifdef __PERF_INSTRUMENTATION__
2839        PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
2840                           PREF(pBuffHead,pBuffer),
2841                           PREF(pBuffHead,nFilledLen),
2842                           PERF_ModuleCommonLayer);
2843#endif
2844        OMX_PRDSP1(pComponentPrivate->dbg, "argsCb[8] is %d\n", (int)(argsCb[8]));
2845        pBuffHead->nFilledLen = (OMX_U32) argsCb[8];
2846
2847        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (LCML for output buffer %p) %lu %lu %lu %lu\n", pBuffHead,
2848                    pComponentPrivate->nInPortIn,
2849                    pComponentPrivate->nInPortOut,
2850                    pComponentPrivate->nOutPortIn,
2851                    pComponentPrivate->nOutPortOut);
2852
2853        pPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
2854        for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
2855            if (pComponentPrivate->nOutPortOut > 10) {
2856                OMX_PRBUFFER1(pComponentPrivate->dbg, "pPortType->sBufferFlagTrack[i].buffer_id %lu\n", pPortType->sBufferFlagTrack[i].buffer_id);
2857            }
2858            if (pPortType->sBufferFlagTrack[i].buffer_id == pComponentPrivate->nOutPortOut) {
2859                OMX_PRBUFFER1(pComponentPrivate->dbg, "output buffer %lu has flag %lx\n",
2860                           pPortType->sBufferFlagTrack[i].buffer_id,
2861                           pPortType->sBufferFlagTrack[i].flag);
2862                pBuffHead->nFlags = pPortType->sBufferFlagTrack[i].flag;
2863                pPortType->sBufferFlagTrack[i].flag = 0;
2864                pPortType->sBufferFlagTrack[i].buffer_id = 0xFFFFFFFF;
2865                break;
2866            }
2867        }
2868        for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
2869            if (pPortType->sBufferMarkTrack[i].buffer_id == pComponentPrivate->nInPortOut) {
2870                OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer ID %lu has mark (output port)\n", pPortType->sBufferMarkTrack[i].buffer_id);
2871                pBuffHead->pMarkData = pPortType->sBufferMarkTrack[i].pMarkData;
2872                pBuffHead->hMarkTargetComponent = pPortType->sBufferMarkTrack[i].hMarkTargetComponent;
2873                pPortType->sBufferMarkTrack[i].buffer_id = 0xFFFFFFFF;
2874                break;
2875            }
2876        }
2877
2878        OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
2879        if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2880            OMX_PRBUFFER2(pComponentPrivate->dbg, "return output buffer %p from LCML_Callback (%d)\n",
2881                           pBuffHead,
2882                           pBuffPrivate->eBufferOwner);
2883            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
2884            OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Filled (output) Data from DSP %p\n", pBuffHead);
2885            eError = HandleJpegEncDataBuf_FromDsp(pComponentPrivate, pBuffHead);
2886        }
2887    }
2888
2889    if ((int) argsCb [0] == EMMCodecInputBuffer ) {
2890        OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
2891        pBuffPrivate = pBuffHead->pInputPortPrivate;
2892
2893       pComponentPrivate->nInPortOut ++;
2894        OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer summary (LCML for InputBuffer %p) %lu %lu %lu %lu\n", pBuffHead,
2895                    pComponentPrivate->nInPortIn,
2896                    pComponentPrivate->nInPortOut,
2897                    pComponentPrivate->nOutPortIn,
2898                    pComponentPrivate->nOutPortOut);
2899#ifdef __PERF_INSTRUMENTATION__
2900	PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
2901			   PREF(pBuffHead,pBuffer),
2902			   0,
2903			   PERF_ModuleCommonLayer);
2904#endif
2905	OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
2906        if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2907            OMX_PRBUFFER2(pComponentPrivate->dbg, "return input buffer %p from LCML_Callback (%d)\n",
2908                           pBuffHead,
2909                           pBuffPrivate->eBufferOwner);
2910            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
2911            OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Emptied (input) Data from DSP %p\n", pBuffHead);
2912            eError = HandleJpegEncFreeDataBuf(pComponentPrivate, pBuffHead);
2913        }
2914    }
2915    goto EXIT;
2916    } /* end     if ( event == EMMCodecBufferProcessed ) */
2917
2918    if ( event == EMMCodecProcessingStoped ) {
2919        OMX_PRDSP2(pComponentPrivate->dbg, "Entering To EMMCodecProcessingStoped \n");
2920        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (Stopped) %lu %lu %lu %lu\n",
2921                    pComponentPrivate->nInPortIn,
2922                    pComponentPrivate->nInPortOut,
2923                    pComponentPrivate->nOutPortIn,
2924                    pComponentPrivate->nOutPortOut);
2925        pComponentPrivate->bDSPStopAck = OMX_TRUE;
2926        OMX_PRSTATE2(pComponentPrivate->dbg, "to state is %d\n", pComponentPrivate->nToState);
2927
2928
2929        /* if (pComponentPrivate->nToState == OMX_StateIdle) { */
2930            pComponentPrivate->ExeToIdleFlag |= JPEGE_DSPSTOP;
2931        /* } */
2932
2933        OMX_TRACE1(pComponentPrivate->dbg, "before stop signal\n");
2934
2935        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
2936        pthread_cond_signal(&pComponentPrivate->stop_cond);
2937        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
2938
2939        OMX_TRACE1(pComponentPrivate->dbg, "after stop signal\n");
2940
2941        goto EXIT;
2942    }
2943
2944    if ( event == EMMCodecDspError ) {
2945
2946       OMX_PRDSP4(pComponentPrivate->dbg, "in EMMCodecDspError EMMCodec Args -> %x, %x\n", (int)argsCb[4] , (int)argsCb[5]);
2947       if ((int)argsCb[4] != 0x1 || (int)argsCb[5] != 0x500) {
2948	   OMX_PRDSP4(pComponentPrivate->dbg, "DSP Error %x %x\n", (int)(argsCb[4]), (int)(argsCb[5]));
2949           pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2950                                     pComponentPrivate->pHandle->pApplicationPrivate,
2951                                     OMX_EventError,
2952                                     OMX_ErrorHardware,
2953                                     OMX_TI_ErrorCritical,
2954                                     NULL);
2955
2956            pComponentPrivate->nCurState = OMX_StateInvalid;
2957            pComponentPrivate->cbInfo.EventHandler(pHandle,
2958                                                   pHandle->pApplicationPrivate,
2959                                                   OMX_EventError,
2960                                                   OMX_ErrorInvalidState,
2961                                                   OMX_TI_ErrorCritical,
2962                                                   "DSP Hardware Error");
2963           goto EXIT;
2964       }
2965#ifdef DSP_MMU_FAULT_HANDLING
2966        /* Cheking for MMU_fault */
2967        if((argsCb[4] == (void *)NULL) && (argsCb[5] == (void*)NULL)) {
2968            //JPEGENC_DPRINT("DSP MMU_Fault");
2969            pComponentPrivate->nCurState = OMX_StateInvalid;
2970            pComponentPrivate->cbInfo.EventHandler(pHandle,
2971                                                   pHandle->pApplicationPrivate,
2972                                                   OMX_EventError,
2973                                                   OMX_ErrorInvalidState,
2974                                                   OMX_TI_ErrorCritical,
2975                                                   "DSP MMU FAULT");
2976        }
2977#endif
2978    }
2979    if (event == EMMCodecInternalError) {
2980        eError = OMX_ErrorHardware;
2981        OMX_PRDSP4(pComponentPrivate->dbg, "JPEG-E: EMMCodecInternalError\n");
2982        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2983                                               pComponentPrivate->pHandle->pApplicationPrivate,
2984                                               OMX_EventError,
2985                                               OMX_ErrorHardware,
2986                                               OMX_TI_ErrorCritical,
2987                                               NULL);
2988        goto EXIT;
2989    }
2990    if ( event == EMMCodecProcessingPaused ) {
2991        OMX_PRDSP2(pComponentPrivate->dbg, "ENTERING TO EMMCodecProcessingPaused JPEG Encoder\n");
2992        if (pComponentPrivate != NULL) {
2993            pComponentPrivate->bDSPStopAck = OMX_TRUE;
2994            pComponentPrivate->nCurState = OMX_StatePause;
2995            /* Decrement reference count with signal enabled */
2996            if(RemoveStateTransition(pComponentPrivate, 1) != OMX_ErrorNone) {
2997                  return OMX_ErrorUndefined;
2998            }
2999            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
3000                                                   OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
3001        }
3002    }
3003    if (event == EMMCodecStrmCtrlAck) {
3004        OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecStrmCtrlAck\n");
3005        if ((int)argsCb [0] == USN_ERR_NONE) {
3006            OMX_PRDSP2(pComponentPrivate->dbg, "Callback: no error\n");
3007            pComponentPrivate->bFlushComplete = OMX_TRUE;
3008            pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
3009            pthread_cond_signal(&pComponentPrivate->flush_cond);
3010            pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
3011        }
3012    }
3013    if (event == EMMCodecAlgCtrlAck) {
3014        OMX_PRDSP2(pComponentPrivate->dbg, "jpeg-enc: EMMCodecAlgCtrlAck\n");
3015        pComponentPrivate->bAckFromSetStatus = 1;
3016        /*
3017        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
3018        pthread_cond_signal(&pComponentPrivate->control_cond);
3019        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
3020        */
3021    }
3022
3023EXIT:
3024    OMX_PRDSP1(pComponentPrivate->dbg, "Exiting the LCML_Callback function\n");
3025    return eError;
3026}
3027/*-------------------------------------------------------------------*/
3028/**
3029  * IsTIOMXComponent()
3030  *
3031  * Check if the component is TI component.
3032  *
3033  * @param hTunneledComp Component Tunnel Pipe
3034  *
3035  * @retval OMX_TRUE   Input is a TI component.
3036  *         OMX_FALSE  Input is a not a TI component.
3037  *
3038  **/
3039/*-------------------------------------------------------------------*/
3040OMX_BOOL IsTIOMXComponent(OMX_HANDLETYPE hComp)
3041{
3042    OMX_ERRORTYPE eError = OMX_ErrorNone;
3043    OMX_STRING pTunnelcComponentName = NULL;
3044    OMX_VERSIONTYPE* pTunnelComponentVersion = NULL;
3045    OMX_VERSIONTYPE* pSpecVersion = NULL;
3046    OMX_UUIDTYPE* pComponentUUID = NULL;
3047    char *pSubstring = NULL;
3048    OMX_BOOL bResult = OMX_TRUE;
3049
3050    OMX_MALLOC(pTunnelcComponentName, 128);
3051    OMX_MALLOC(pTunnelComponentVersion, sizeof(OMX_VERSIONTYPE));
3052    OMX_MALLOC(pSpecVersion, sizeof(OMX_VERSIONTYPE));
3053    OMX_MALLOC(pComponentUUID, sizeof(OMX_UUIDTYPE));
3054
3055    eError = OMX_GetComponentVersion (hComp, pTunnelcComponentName, pTunnelComponentVersion, pSpecVersion, pComponentUUID);
3056
3057    /* Check if tunneled component is a TI component */
3058    pSubstring = strstr(pTunnelcComponentName, "OMX.TI.");
3059    if(pSubstring == NULL) {
3060        bResult = OMX_FALSE;
3061    }
3062
3063EXIT:
3064    OMX_FREE(pTunnelcComponentName);
3065    OMX_FREE(pTunnelComponentVersion);
3066    OMX_FREE(pSpecVersion);
3067    OMX_FREE(pComponentUUID);
3068    return bResult;
3069} /* End of IsTIOMXComponent */
3070
3071#ifdef RESOURCE_MANAGER_ENABLED
3072/* ========================================================================== */
3073/**
3074 *  ResourceManagerCallback() - handle callbacks from Resource Manager
3075 * @param cbData    Resource Manager Command Data Structure
3076 * @return: void
3077  **/
3078/* ========================================================================== */
3079
3080void ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData)
3081{
3082    OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
3083    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
3084    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
3085    OMX_ERRORTYPE RM_Error = *(cbData.RM_Error);
3086
3087    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
3088    OMX_PRINT1(pComponentPrivate->dbg, "RM_Error = %x\n", RM_Error);
3089
3090    if (RM_Error == OMX_RmProxyCallback_ResourcesPreempted) {
3091
3092        pComponentPrivate->bPreempted = 1;
3093
3094        if (pComponentPrivate->nCurState == OMX_StateExecuting ||
3095            pComponentPrivate->nCurState == OMX_StatePause) {
3096
3097            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
3098                                                   pComponentPrivate->pHandle->pApplicationPrivate,
3099                                                   OMX_EventError,
3100                                                   OMX_ErrorResourcesPreempted,
3101                                                   OMX_TI_ErrorSevere,
3102                                                   NULL);
3103
3104            pComponentPrivate->nToState = OMX_StateIdle;
3105            OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to IDLE State.\n");
3106        }
3107        else if (pComponentPrivate->nCurState == OMX_StateIdle){
3108            pComponentPrivate->nToState = OMX_StateLoaded;
3109            OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to LOADED State.\n");
3110        }
3111
3112#ifdef __PERF_INSTRUMENTATION__
3113        PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
3114#endif
3115
3116        write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
3117        write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
3118
3119    }
3120    else if (RM_Error == OMX_RmProxyCallback_ResourcesAcquired ){
3121
3122        if (pComponentPrivate->nCurState == OMX_StateWaitForResources) /* Wait for Resource Response */
3123        {
3124            pComponentPrivate->cbInfo.EventHandler (
3125                            pHandle, pHandle->pApplicationPrivate,
3126                            OMX_EventResourcesAcquired, 0,0,
3127                            NULL);
3128
3129            pComponentPrivate->nToState = OMX_StateIdle;
3130
3131#ifdef __PERF_INSTRUMENTATION__
3132            PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
3133#endif
3134
3135            write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
3136            write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
3137            OMX_PRMGR2(pComponentPrivate->dbg, "OMX_RmProxyCallback_ResourcesAcquired.\n");
3138        }
3139	}
3140	else if (RM_Error == OMX_RmProxyCallback_FatalError){
3141
3142		/* Deinitialize the component...no error should be returned from
3143		 * this function. It should clean the system as much as possible */
3144		if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
3145                	LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle, EMMCodecControlDestroy, NULL);
3146                     	dlclose(pComponentPrivate->pDllHandle);
3147                	pComponentPrivate->pLCML = NULL;
3148                	pComponentPrivate->isLCMLActive = 0;
3149		}
3150
3151		pComponentPrivate->nCurState = OMX_StateInvalid;
3152		pComponentPrivate->cbInfo.EventHandler(pHandle,
3153											   pHandle->pApplicationPrivate,
3154											   OMX_EventError,
3155											   OMX_ErrorInvalidState,
3156											   OMX_TI_ErrorSevere,
3157											   NULL);
3158	}
3159}
3160#endif
3161
3162void LinkedList_Create(LinkedList *LinkedList) {
3163    LinkedList->pRoot = NULL;
3164    pthread_mutex_init(&LinkedList->lock, NULL);
3165}
3166
3167void LinkedList_AddElement(LinkedList *LinkedList, void *pValue) {
3168    /* create new node and fill the value */
3169    Node *pNewNode = (Node *)malloc(sizeof(Node));
3170    pNewNode->pValue = (void *)pValue;
3171    /*printf("LinkedList:::: Pointer=%p has been added.\n", pNewNode->pValue); */
3172    /* add new node on the root to implement quick FIFO */
3173    /* modify new node pointers */
3174
3175    pthread_mutex_lock(&LinkedList->lock);
3176    if(LinkedList->pRoot == NULL) {
3177        pNewNode->pNextNode = NULL;
3178    }
3179    else {
3180         pNewNode->pNextNode = LinkedList->pRoot;
3181    }
3182    /*modify root */
3183    LinkedList->pRoot = pNewNode;
3184    pthread_mutex_unlock(&LinkedList->lock);
3185}
3186
3187void LinkedList_FreeElement(LinkedList *LinkedList, void *pValue) {
3188    pthread_mutex_lock(&LinkedList->lock);
3189    Node *pNode = LinkedList->pRoot;
3190    Node *pPastNode = NULL;
3191    while (pNode != NULL) {
3192        if (pNode->pValue == pValue) {
3193            Node *pTempNode = pNode->pNextNode;
3194            if(pPastNode == NULL) {
3195                LinkedList->pRoot = pTempNode;
3196            }
3197            else {
3198                pPastNode->pNextNode = pTempNode;
3199            }
3200            /*printf("LinkedList:::: Pointer=%p has been freed\n", pNode->pValue); */
3201            free(pNode->pValue);
3202            pNode->pValue = NULL;
3203            free(pNode);
3204            pNode = NULL;
3205            break;
3206        }
3207        pPastNode = pNode;
3208        pNode = pNode->pNextNode;
3209    }
3210    pthread_mutex_unlock(&LinkedList->lock);
3211}
3212
3213void LinkedList_FreeAll(LinkedList *LinkedList) {
3214    Node *pTempNode;
3215    int nodes = 0;
3216    pthread_mutex_lock(&LinkedList->lock);
3217    while (LinkedList->pRoot != NULL) {
3218        pTempNode = LinkedList->pRoot->pNextNode;
3219        /*printf("LinkedList:::: Pointer=%p has been freed\n", LinkedList->pRoot->pValue); */
3220        if(LinkedList->pRoot->pValue != NULL) {
3221            free(LinkedList->pRoot->pValue);
3222            LinkedList->pRoot->pValue = NULL;
3223        }
3224        free(LinkedList->pRoot);
3225        LinkedList->pRoot = pTempNode;
3226        nodes++;
3227    }
3228    pthread_mutex_unlock(&LinkedList->lock);
3229    /*printf("==================No. of deleted nodes: %d=======================================\n\n", nodes); */
3230}
3231
3232void LinkedList_DisplayAll(LinkedList *LinkedList) {
3233    Node *pNode = LinkedList->pRoot;
3234    int nodes = 0;
3235    printf("\n================== Displaying contents of linked list=%p=====================\n", LinkedList);
3236    printf("root->\n");
3237    while (pNode != NULL) {
3238        printf("[Value=%p, NextNode=%p]->\n", pNode->pValue, pNode->pNextNode);
3239        pNode = pNode->pNextNode;
3240        nodes++;
3241    }
3242    printf("==================No. of existing nodes: %d=======================================\n\n", nodes);
3243}
3244
3245void LinkedList_Destroy(LinkedList *LinkedList) {
3246    pthread_mutex_destroy(&LinkedList->lock);
3247}
3248
3249OMX_ERRORTYPE AddStateTransition(JPEGENC_COMPONENT_PRIVATE* pComponentPrivate) {
3250
3251    OMX_ERRORTYPE eError = OMX_ErrorNone;
3252
3253    if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
3254       return OMX_ErrorUndefined;
3255    }
3256
3257    /* Increment state change request reference count */
3258    pComponentPrivate->nPendingStateChangeRequests++;
3259
3260    if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
3261       return OMX_ErrorUndefined;
3262    }
3263
3264    return eError;
3265}
3266
3267OMX_ERRORTYPE RemoveStateTransition(JPEGENC_COMPONENT_PRIVATE* pComponentPrivate, OMX_BOOL bEnableSignal) {
3268    OMX_ERRORTYPE eError = OMX_ErrorNone;
3269
3270     /* Decrement state change request reference count*/
3271    if(pthread_mutex_lock(&pComponentPrivate->mutexStateChangeRequest)) {
3272       return OMX_ErrorUndefined;
3273    }
3274
3275    pComponentPrivate->nPendingStateChangeRequests--;
3276
3277    /* If there are no more pending requests, signal the thread waiting on this*/
3278    if(!pComponentPrivate->nPendingStateChangeRequests && bEnableSignal) {
3279       pthread_cond_signal(&(pComponentPrivate->StateChangeCondition));
3280    }
3281
3282    if(pthread_mutex_unlock(&pComponentPrivate->mutexStateChangeRequest)) {
3283       return OMX_ErrorUndefined;
3284    }
3285
3286    return eError;
3287}
3288
3289
3290