OMX_JpegEnc_Utils.c revision bca73818821dde513097a412ae6f2539d927e588
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            pComponentPrivate->nInPortIn   = pComponentPrivate->nInPortOut   = 0;
1519            pComponentPrivate->nOutPortIn = pComponentPrivate->nOutPortOut = 0;
1520
1521
1522#ifdef RESOURCE_MANAGER_ENABLED
1523            eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle,  3456, NULL);
1524            if (eError != OMX_ErrorNone) {
1525                OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Loaded ->Idle\n");
1526
1527                pComponentPrivate->cbInfo.EventHandler(pHandle,
1528                                                       pHandle->pApplicationPrivate,
1529                                                       OMX_EventError,
1530                                                       OMX_ErrorInsufficientResources,
1531                                                       OMX_TI_ErrorSevere,
1532                                                       NULL);
1533                break;
1534            }
1535#endif
1536
1537#ifdef __PERF_INSTRUMENTATION__
1538            PERF_Boundary(pComponentPrivate->pPERFcomp,
1539                          PERF_BoundaryComplete | PERF_BoundarySetup);
1540#endif
1541
1542            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1543                              pComponentPrivate->pHandle->pApplicationPrivate,
1544                          OMX_EventCmdComplete,
1545                          OMX_CommandStateSet,
1546                          pComponentPrivate->nCurState,
1547                          NULL);
1548            break;
1549
1550        }
1551        else if ( pComponentPrivate->nCurState == OMX_StateExecuting ||
1552                  pComponentPrivate->nCurState == OMX_StatePause ) {
1553#ifdef __PERF_INSTRUMENTATION__
1554            PERF_Boundary(pComponentPrivate->pPERFcomp,
1555                          PERF_BoundaryComplete | PERF_BoundarySteadyState);
1556#endif
1557
1558          pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1559          pComponentPrivate->bDSPStopAck = OMX_FALSE;
1560          OMX_PRDSP2(pComponentPrivate->dbg, "bDSPStopAck is %d\n", pComponentPrivate->bDSPStopAck);
1561          eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,MMCodecControlStop,NULL);
1562          pComponentPrivate->nApp_nBuf= 1;
1563         /* HandleJpegEncCommandFlush(pComponentPrivate, -1); */
1564          /*
1565          if ( pComponentPrivate->isLCMLActive ==1 ) {
1566              LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
1567              dlclose(pComponentPrivate->pDllHandle);
1568              pComponentPrivate->isLCMLActive = 0;
1569          }
1570          */
1571
1572#ifdef RESOURCE_MANAGER_ENABLED
1573
1574        eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateIdle, 3456, NULL);
1575        if (eError != OMX_ErrorNone) {
1576	        OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available Executing ->Idle\n");
1577            pComponentPrivate->nCurState = OMX_StateWaitForResources;
1578            pComponentPrivate->cbInfo.EventHandler(pHandle,
1579                                                   pHandle->pApplicationPrivate,
1580                                                   OMX_EventCmdComplete,
1581                                                   OMX_CommandStateSet,
1582                                                   pComponentPrivate->nCurState,
1583                                                   NULL);
1584            break;
1585        }
1586#endif
1587          pComponentPrivate->ExeToIdleFlag |= JPEGE_BUFFERBACK;
1588
1589        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc: before stop lock\n");
1590        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
1591        /*
1592        while ((pComponentPrivate->ExeToIdleFlag & 0x3) != JPEGE_IDLEREADY) {
1593            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1594            pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
1595        }
1596        */
1597        while (pComponentPrivate->bDSPStopAck == OMX_FALSE) {
1598            OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1599            pthread_cond_wait(&pComponentPrivate->stop_cond, &pComponentPrivate->jpege_mutex);
1600        }
1601        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
1602
1603        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:got STOP ack from DSP\n");
1604
1605        int i;
1606        for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef->nBufferCountActual); i ++) {
1607            JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
1608            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
1609
1610            pBuffHead = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[i]->pBufferHdr;
1611            pBuffPrivate = pBuffHead->pInputPortPrivate;
1612
1613            OMX_PRBUFFER1(pComponentPrivate->dbg, "JPEG enc:: owner %d \n", pBuffPrivate->eBufferOwner);
1614            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
1615                if (pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufSupplier != OMX_BufferSupplyInput) {
1616                    if(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent == NULL){
1617                        OMX_PRBUFFER2(pComponentPrivate->dbg, "Sending buffer to app\n");
1618                        OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
1619                        pComponentPrivate->nInPortOut ++;
1620                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1621                        pComponentPrivate->cbInfo.EmptyBufferDone(
1622                                   pComponentPrivate->pHandle,
1623                                   pComponentPrivate->pHandle->pApplicationPrivate,
1624                                   pBuffHead);
1625                }
1626                else{
1627                    OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending beffer to tunnel, pHandle=%p\n", pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent);
1628                    pBuffHead->nFilledLen = 0;
1629                    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1630                    eError = OMX_FillThisBuffer(pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent,
1631                                                pBuffHead);
1632                }
1633            }
1634        }
1635    }
1636
1637        OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all input buffers\n");
1638
1639        for (i = 0; i < (int)(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef->nBufferCountActual); i ++) {
1640            JPEGENC_BUFFER_PRIVATE    *pBuffPrivate = NULL;
1641            OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
1642
1643            pBuffHead = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufferPrivate[i]->pBufferHdr;
1644            pBuffPrivate = pBuffHead->pOutputPortPrivate;
1645
1646            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer %p owner %d \n", pBuffHead, pBuffPrivate->eBufferOwner);
1647            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
1648                if (pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pBufSupplier != OMX_BufferSupplyOutput) {
1649                    if(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent == NULL){
1650                        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to app\n");
1651                        OMX_PRDSP2(pComponentPrivate->dbg, "Handle error from DSP/bridge\n");
1652                        pComponentPrivate->nOutPortOut ++;
1653                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1654                        pComponentPrivate->cbInfo.FillBufferDone(
1655                                   pComponentPrivate->pHandle,
1656                                   pComponentPrivate->pHandle->pApplicationPrivate,
1657                                   pBuffHead);
1658                    }
1659                    else{
1660                        OMX_PRBUFFER2(pComponentPrivate->dbg, "JPEG enc:: Sending OUTPUT buffer to Tunnel component\n");
1661                        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
1662                    eError = OMX_EmptyThisBuffer(pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->hTunnelComponent,
1663                                                pBuffHead);
1664                    }
1665                }
1666            }
1667        }
1668        OMX_PRBUFFER2(pComponentPrivate->dbg, "returned all output buffers\n");
1669
1670         pComponentPrivate->nCurState = OMX_StateIdle;
1671         pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1672               pComponentPrivate->pHandle->pApplicationPrivate,
1673               OMX_EventCmdComplete,
1674               OMX_CommandStateSet,
1675               pComponentPrivate->nCurState,
1676               NULL);
1677             pComponentPrivate->ExeToIdleFlag = 0;
1678    }
1679        else {
1680         OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
1681         pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
1682                                                    pComponentPrivate->pHandle->pApplicationPrivate,
1683                            OMX_EventError,
1684                                                    OMX_ErrorIncorrectStateTransition,
1685                                                    OMX_TI_ErrorMinor,
1686                                                    "Invalid State");
1687        }
1688    break;
1689
1690    case OMX_StateExecuting:
1691
1692        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateExecuting \n");
1693        OMX_PRBUFFER2(pComponentPrivate->dbg, "In exec in %lu out %lu\n", pComponentPrivate->nInPortIn, pComponentPrivate->nOutPortOut);
1694        if ( pComponentPrivate->nCurState == OMX_StateExecuting ) {
1695            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1696                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1697                                                   OMX_EventError,
1698                                                   OMX_ErrorSameState,
1699                                                   OMX_TI_ErrorMinor,
1700                                                   NULL);
1701        }
1702        else if ( pComponentPrivate->nCurState == OMX_StateIdle || pComponentPrivate->nCurState == OMX_StatePause ) {
1703
1704#ifdef __PERF_INSTRUMENTATION__
1705            PERF_Boundary(pComponentPrivate->pPERFcomp,
1706                          PERF_BoundaryStart | PERF_BoundarySteadyState);
1707#endif
1708
1709#if 1
1710        eError = SendDynamicParam(pComponentPrivate);
1711            if (eError != OMX_ErrorNone ) {
1712                OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
1713                goto EXIT;
1714        }
1715#endif
1716
1717
1718
1719
1720        OMX_PRDSP2(pComponentPrivate->dbg, "after SendDynamicParam\n");
1721        pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1722        eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlStart,NULL);
1723
1724#ifdef RESOURCE_MANAGER_ENABLED
1725
1726    eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateExecuting, 3456, NULL);
1727    if (eError != OMX_ErrorNone) {
1728        OMX_PRMGR4(pComponentPrivate->dbg, "Resources not available\n");
1729        pComponentPrivate->nCurState = OMX_StateWaitForResources;
1730        pComponentPrivate->cbInfo.EventHandler(pHandle,
1731                                               pHandle->pApplicationPrivate,
1732                                               OMX_EventCmdComplete,
1733                                               OMX_CommandStateSet,
1734                                               pComponentPrivate->nCurState,
1735                                               NULL);
1736        break;
1737        }
1738#endif
1739
1740        pComponentPrivate->nCurState = OMX_StateExecuting;
1741        OMX_PRSTATE2(pComponentPrivate->dbg, "State has been set to Executing\n");
1742#ifdef __PERF_INSTRUMENTATION__
1743            PERF_Boundary(pComponentPrivate->pPERFcomp,
1744                          PERF_BoundarySteadyState| PERF_BoundaryComplete);
1745#endif
1746
1747        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1748                                               pComponentPrivate->pHandle->pApplicationPrivate,
1749                                               OMX_EventCmdComplete,
1750                                               OMX_CommandStateSet,
1751                                               pComponentPrivate->nCurState,
1752                                               NULL);
1753        } else {
1754            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1755                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1756                                                   OMX_EventError,
1757                                                   OMX_ErrorIncorrectStateTransition,
1758                                                   OMX_TI_ErrorMinor,
1759                                                   NULL);
1760        }
1761        break;
1762
1763    case OMX_StatePause:
1764        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StatePause\n");
1765
1766        pComponentPrivate->nToState = OMX_StatePause;
1767#ifdef __PERF_INSTRUMENTATION__
1768                PERF_Boundary(pComponentPrivate->pPERFcomp,
1769                              PERF_BoundaryComplete | PERF_BoundarySteadyState);
1770#endif
1771
1772        if ( pComponentPrivate->nCurState == OMX_StatePause ) {
1773            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1774                                                   OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
1775        } else if ( pComponentPrivate->nCurState == OMX_StateExecuting || pComponentPrivate->nCurState == OMX_StateIdle ) {
1776
1777            pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1778            eError = LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlPause,NULL);
1779            if ( eError != OMX_ErrorNone ) {
1780                OMX_PRDSP4(pComponentPrivate->dbg, "Error during EMMCodecControlPause. Error: %d.\n", eError );
1781                goto EXIT;
1782            }
1783            /*
1784            pComponentPrivate->nCurState = OMX_StatePause;
1785            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1786                                                   OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
1787                                                   */
1788        } else {
1789            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
1790                                                   OMX_EventError, OMX_ErrorIncorrectStateTransition, OMX_TI_ErrorMinor , NULL);
1791            OMX_PRSTATE4(pComponentPrivate->dbg, "Error: Invalid State Given by Application\n");
1792        }
1793        break;
1794
1795
1796    case OMX_StateInvalid:
1797        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateInvalid::\n");
1798        if ( pComponentPrivate->nCurState == OMX_StateInvalid ) {
1799            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle, pComponentPrivate->pHandle->pApplicationPrivate,
1800                                                   OMX_EventError, OMX_ErrorSameState, OMX_TI_ErrorMinor , NULL);
1801        }
1802        if ( pComponentPrivate->nCurState != OMX_StateLoaded ) {
1803            OMX_PRBUFFER2(pComponentPrivate->dbg, "HandleJpegEncInternalFlush \n");
1804            eError = HandleJpegEncInternalFlush(pComponentPrivate, nParam1);
1805            }
1806
1807            pComponentPrivate->nCurState = OMX_StateInvalid;
1808
1809            if(pComponentPrivate->nToState == OMX_StateInvalid){ /*if the IL client call directly send to invalid state*/
1810                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1811                                       pComponentPrivate->pHandle->pApplicationPrivate,
1812                                       OMX_EventCmdComplete,
1813                                       OMX_CommandStateSet,
1814                                       pComponentPrivate->nCurState,
1815                                       NULL);
1816            }
1817            else{ /*When the component go to invalid state by it self*/
1818                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1819                                       pComponentPrivate->pHandle->pApplicationPrivate,
1820                                       OMX_EventError,
1821                                       OMX_ErrorInvalidState,
1822                                       OMX_TI_ErrorSevere,
1823                                       NULL);
1824            }
1825        break;
1826
1827    case OMX_StateLoaded:
1828        if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
1829            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1830                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1831                                                   OMX_EventError,
1832                                                   OMX_ErrorSameState,
1833                                                   OMX_TI_ErrorMinor ,
1834                                                   NULL);
1835
1836        } else if ( pComponentPrivate->nCurState == OMX_StateIdle ||
1837                    pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
1838            /* Ports have to be unpopulated before transition completes */
1839            OMX_PRSTATE2(pComponentPrivate->dbg, "from idle to loaded\n");
1840
1841            pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1842            if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
1843                pLcmlHandle =(LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
1844                OMX_PRDSP2(pComponentPrivate->dbg, "try to close library again %p\n", pComponentPrivate->pLCML);
1845                LCML_ControlCodec(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,EMMCodecControlDestroy,NULL);
1846                OMX_PRDSP2(pComponentPrivate->dbg, "after close library again %p\n", pComponentPrivate->pLCML);
1847                pComponentPrivate->pLCML = NULL;
1848                dlclose(pComponentPrivate->pDllHandle);
1849                pComponentPrivate->isLCMLActive = 0;
1850
1851            }
1852            OMX_PRDSP2(pComponentPrivate->dbg, "after release LCML\n");
1853#ifdef __PERF_INSTRUMENTATION__
1854            PERF_Boundary(pComponentPrivate->pPERFcomp,
1855                          PERF_BoundaryStart | PERF_BoundaryCleanup);
1856#endif
1857
1858#ifdef UNDER_CE
1859            nTimeout=0x0;
1860
1861                while ( 1 )
1862                {
1863                    if ( (pPortDefOut->bPopulated == OMX_FALSE) )
1864                    {
1865                        OMX_PRDSP2(pComponentPrivate->dbg, "Thread Sending Cmd EMMCodecControlDestroy\n");
1866
1867
1868                        break;
1869                    } else if ( nTimeout++ > JPEGENC_TIMEOUT )
1870
1871                    {
1872                        OMX_PRBUFFER4(pComponentPrivate->dbg, "Timeout ...\n");
1873                        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1874                                       pComponentPrivate->pHandle->pApplicationPrivate,
1875                                       OMX_EventError,
1876                                       OMX_ErrorPortUnresponsiveDuringAllocation,
1877                                       OMX_TI_ErrorMajor,
1878                                       "Not response Port -Loaded");
1879                        OMX_PRBUFFER4(pComponentPrivate->dbg, "Not all ports are unpopulated!\n");
1880                        break;
1881                    }
1882                    /* Sleep for a while, so the application thread can allocate buffers */
1883            sched_yield();
1884            }
1885#else
1886
1887            pthread_mutex_lock(&pComponentPrivate->jpege_mutex_app);
1888            while ( pPortDefIn->bPopulated || pPortDefOut->bPopulated) {
1889                OMX_PRBUFFER0(pComponentPrivate->dbg, "%d in cond wait\n", __LINE__);
1890                pthread_cond_wait(&pComponentPrivate->unpopulate_cond, &pComponentPrivate->jpege_mutex_app);
1891            }
1892            pthread_mutex_unlock(&pComponentPrivate->jpege_mutex_app);
1893#endif
1894
1895#ifdef __PERF_INSTRUMENTATION__
1896                        PERF_Boundary(pComponentPrivate->pPERFcomp,
1897                                      PERF_BoundaryComplete | PERF_BoundaryCleanup);
1898#endif
1899
1900#ifdef RESOURCE_MANAGER_ENABLED
1901            if (pComponentPrivate->nCurState != OMX_StateWaitForResources) {
1902                eError= RMProxy_NewSendCommand(pHandle,  RMProxy_FreeResource, OMX_JPEG_Encoder_COMPONENT, 0, 3456, NULL);
1903                if (eError != OMX_ErrorNone) {
1904                    OMX_PRMGR4(pComponentPrivate->dbg, "Cannot Free Resources\n");
1905                    pComponentPrivate->cbInfo.EventHandler(pHandle,
1906                                                           pHandle->pApplicationPrivate,
1907                                                           OMX_EventError,
1908                                                           OMX_ErrorUndefined,
1909                                                           OMX_TI_ErrorMajor,
1910                                                           NULL);
1911                    break;
1912                }
1913            }
1914#endif
1915
1916            pComponentPrivate->nCurState = OMX_StateLoaded;
1917
1918            if ((pComponentPrivate->nCurState == OMX_StateIdle) &&
1919                 (pComponentPrivate->bPreempted == 1 )){
1920
1921                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1922                                                       pComponentPrivate->pHandle->pApplicationPrivate,
1923                                                       OMX_EventError,
1924                                                       OMX_ErrorResourcesLost,
1925                                                       OMX_TI_ErrorSevere,
1926                                                       NULL);
1927                pComponentPrivate->bPreempted = 0;
1928
1929            }
1930            else {
1931                pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1932                                                       pComponentPrivate->pHandle->pApplicationPrivate,
1933                                                       OMX_EventCmdComplete,
1934                                                       OMX_CommandStateSet,
1935                                                       OMX_StateLoaded,
1936                                                       NULL);
1937            }
1938
1939        }
1940        else {
1941
1942            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1943                                                   pComponentPrivate->pHandle->pApplicationPrivate,
1944                                                   OMX_EventError,
1945                                                   OMX_ErrorIncorrectStateTransition,
1946                                                   OMX_TI_ErrorMinor,
1947                                                   NULL);
1948        }
1949        break;
1950
1951    case OMX_StateWaitForResources:
1952        if ( pComponentPrivate->nCurState == OMX_StateWaitForResources ) {
1953            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1954                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
1955                                                                            OMX_EventError,
1956                                                                            OMX_ErrorSameState,
1957                                                                            OMX_TI_ErrorMinor,
1958                                                                            NULL);
1959        } else if ( pComponentPrivate->nCurState == OMX_StateLoaded ) {
1960
1961#ifdef RESOURCE_MANAGER_ENABLED
1962            eError= RMProxy_NewSendCommand(pHandle, RMProxy_StateSet, OMX_JPEG_Encoder_COMPONENT, OMX_StateWaitForResources, 3456, NULL);
1963            if (eError != OMX_ErrorNone) {
1964                OMX_PRMGR4(pComponentPrivate->dbg, "RMProxy_NewSendCommand(OMX_StateWaitForResources) failed\n");
1965                pComponentPrivate->cbInfo.EventHandler(pHandle,
1966                                                       pHandle->pApplicationPrivate,
1967                                                       OMX_EventError,
1968                                                       OMX_ErrorUndefined,
1969                                                       OMX_TI_ErrorSevere,
1970                                                       NULL);
1971                break;
1972            }
1973#endif
1974
1975            pComponentPrivate->nCurState = OMX_StateWaitForResources;
1976            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1977                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
1978                                                                            OMX_EventCmdComplete,
1979                                                                            OMX_CommandStateSet,
1980                                                                            pComponentPrivate->nCurState,
1981                                                                            NULL);
1982        } else {
1983            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
1984                                                                            pComponentPrivate->pHandle->pApplicationPrivate,
1985                                                                            OMX_EventError,
1986                                                                            OMX_ErrorIncorrectStateTransition,
1987                                                                            OMX_TI_ErrorMinor,
1988                                                                            NULL);
1989        }
1990        break;
1991
1992    case OMX_StateMax:
1993        OMX_PRSTATE2(pComponentPrivate->dbg, "HandleCommand: Cmd OMX_StateMax::\n");
1994        break;
1995    } /* End of Switch */
1996
1997
1998    EXIT:
1999    return eError;
2000}
2001
2002
2003OMX_ERRORTYPE HandleJpegEncFreeOutputBufferFromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
2004{
2005
2006    OMX_ERRORTYPE eError = OMX_ErrorNone;
2007    OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
2008    /* IUALG_Buf *ptParam = NULL; */
2009    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
2010    LCML_DSP_INTERFACE* pLcmlHandle = NULL;
2011    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2012    int ret;
2013
2014    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeOutputBufferFromApp function\n");
2015
2016    pLcmlHandle = (LCML_DSP_INTERFACE *)(pComponentPrivate->pLCML);
2017    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
2018
2019    ret = read(pComponentPrivate->free_outBuf_Q[0], &pBuffHead, sizeof(pBuffHead));
2020    if ( ret == -1 ) {
2021        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
2022        goto EXIT;
2023    }
2024
2025    if (pBuffHead != NULL) {
2026        pBuffPrivate = pBuffHead->pOutputPortPrivate;
2027    } else {
2028        OMX_PRBUFFER4(pComponentPrivate->dbg, "Error while reading pBuffHead from the pipe\n");
2029        goto EXIT;
2030    }
2031
2032    if (pComponentPrivate->nCurState != OMX_StatePause || pComponentPrivate->nToState != OMX_StatePause) {
2033
2034   if ((pComponentPrivate->nCurState != OMX_StateExecuting ) ||
2035       (pComponentPrivate->nToState == OMX_StateIdle) ||
2036         (pPortDefOut->bEnabled == OMX_FALSE)) {
2037         if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2038            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2039            pComponentPrivate->nOutPortOut ++;
2040            OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
2041                    pComponentPrivate->nInPortIn,
2042                    pComponentPrivate->nInPortOut,
2043                    pComponentPrivate->nOutPortIn,
2044                    pComponentPrivate->nOutPortOut);
2045            OMX_PRBUFFER2(pComponentPrivate->dbg, "FillBufferDone (incorrect state %d) %p\n", pComponentPrivate->nCurState, pBuffHead);
2046            pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
2047                    pComponentPrivate->pHandle->pApplicationPrivate,
2048                    pBuffHead);
2049         }
2050        goto EXIT;
2051    }
2052    }
2053
2054    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncFreeOutputBufferFromApp) %lu %lu %lu %lu\n",
2055                    pComponentPrivate->nInPortIn,
2056                    pComponentPrivate->nInPortOut,
2057                    pComponentPrivate->nOutPortIn,
2058                    pComponentPrivate->nOutPortOut);
2059
2060#ifdef __PERF_INSTRUMENTATION__
2061    PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2062                      pBuffHead->pBuffer,
2063                      0,
2064                      PERF_ModuleCommonLayer);
2065#endif
2066
2067    /* ptParam =  (IUALG_Buf *)pBuffPrivate->pUALGParams; */
2068    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
2069
2070#ifdef __JPEG_OMX_PPLIB_ENABLED__
2071    if (pComponentPrivate->pOutParams != NULL)
2072    {
2073        pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams - PADDING_128_BYTE;
2074        OMX_FREE(pComponentPrivate->pOutParams);
2075    }
2076    OMX_MALLOC(pComponentPrivate->pOutParams,sizeof(PPLIB_UALGRunTimeParam_t) + PADDING_256_BYTE);
2077    pComponentPrivate->pOutParams = (OMX_U8*)pComponentPrivate->pOutParams + PADDING_128_BYTE;
2078
2079    if (pComponentPrivate->pOutParams != NULL)
2080    {
2081    }
2082    else
2083    {
2084        goto EXIT;
2085    }
2086
2087    eError = SendDynamicPPLibParam(pComponentPrivate,pComponentPrivate->pOutParams);
2088       if (eError != OMX_ErrorNone ) {
2089           goto EXIT;
2090       }
2091
2092
2093       eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2094                                 EMMCodecOuputBuffer,
2095                                 pBuffHead->pBuffer,
2096                                 pPortDefOut->nBufferSize,
2097                                 0,
2098                                 (OMX_U8 *)pComponentPrivate->pOutParams,
2099                                 sizeof(PPLIB_UALGRunTimeParam_t),
2100                                 (OMX_U8 *)  pBuffHead);
2101
2102#else
2103    eError = LCML_QueueBuffer(((LCML_DSP_INTERFACE*)pLcmlHandle)->pCodecinterfacehandle,
2104                              EMMCodecOuputBuffer,
2105                              pBuffHead->pBuffer,
2106                              pPortDefOut->nBufferSize,
2107                              0,
2108                              NULL,
2109                              0,
2110                              (OMX_U8 *)  pBuffHead);
2111#endif
2112
2113    OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
2114
2115    EXIT:
2116    return eError;
2117}
2118
2119OMX_U32 CalculateParamsSize (JPEGENC_COMPONENT_PRIVATE *pComponentPrivate) {
2120
2121    OMX_U32 i = 0;
2122
2123    i+= 4; /* 4 bytes for the size of the whole array */
2124
2125    /* Set Custom Quantization Table */
2126    if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
2127        i+=4;  /* 4 bytes for the Quantization table TAG */
2128        i+=4;  /* 4 bytes for the size of this TAG */
2129        i+=256; /* 256 bytes for the Quantization table data*/
2130    }
2131
2132    /* Set Custom Huffman Table */
2133    if (pComponentPrivate->bSetHuffmanTable) {
2134        i+=4;  /* 4 bytes for the Huffman table TAG */
2135        i+=4;  /* 4 bytes for the size of this TAG */
2136        if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
2137            i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)));
2138        }
2139        else {
2140            i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE);
2141        }
2142    }
2143
2144    /* handle APP0 marker (JFIF)*/
2145    if(pComponentPrivate->sAPP0.bMarkerEnabled) {
2146        i+=4;  /* 4 bytes for the Number of buffers TAG */
2147        i+=4;  /* 4 bytes for the size of this TAG */
2148        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2149        i+=4;  /* 4 bytes for the buffer0  TAG */
2150
2151        /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
2152        if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
2153        || pComponentPrivate->sAPP0.nMarkerSize <= 0) {
2154            i+=4;   /* 4 bytes for the size of this TAG */
2155            i+=4;   /* 4 bytes for the actual data of this TAG */
2156        }
2157        else {
2158            i+=4;   /* 4 bytes for the size of this TAG */
2159            i += (pComponentPrivate->sAPP0.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2160            if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
2161                i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2162            }
2163        }
2164        /* if thumbnail is set, configure it accordingly */
2165        if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
2166            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2167            i+=4;  /* 4 bytes for the size of this TAG */
2168            i+=4;  /* 4 bytes for the actual data for this TAG */
2169
2170            i+=4;  /* 4 bytes for the THUMB W TAG */
2171            i+=4;  /* 4 bytes for the size of this TAG */
2172            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2173
2174            i+=4;  /* 4 bytes for the THUMB H TAG */
2175            i+=4;  /* 4 bytes for the size of this TAG */
2176            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2177        }
2178    }
2179
2180    /* handle APP1 marker (EXIF)*/
2181    if (pComponentPrivate->sAPP1.bMarkerEnabled) {
2182        i+=4;  /* 4 bytes for the Number of buffers TAG */
2183        i+=4;  /* 4 bytes for the size of this TAG */
2184        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2185        i+=4;  /* 4 bytes for the buffer0  TAG */
2186
2187    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2188    	if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
2189            i+=4;   /* 4 bytes for the size of this TAG */
2190            i+=4;   /* 4 bytes for the actual data of this TAG */
2191            i+=4;   /* 4 bytes for the actual data of this TAG */
2192    	}
2193    	else {
2194    	    i+=4;   /* 4 bytes for the size of this TAG */
2195                i += (pComponentPrivate->sAPP1.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2196                if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
2197                    i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2198                }
2199    	}
2200
2201    	/* if thumbnail is set, configure it accordingly */
2202    	if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
2203            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2204            i+=4;  /* 4 bytes for the size of this TAG */
2205            i+=4;  /* 4 bytes for the actual data for this TAG */
2206
2207            i+=4;  /* 4 bytes for the THUMB W TAG */
2208            i+=4;  /* 4 bytes for the size of this TAG */
2209            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2210
2211            i+=4;  /* 4 bytes for the THUMB H TAG */
2212            i+=4;  /* 4 bytes for the size of this TAG */
2213            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2214    	}
2215    }
2216
2217
2218    /* handle APP5 marker */
2219    if (pComponentPrivate->sAPP5.bMarkerEnabled) {
2220        i+=4;  /* 4 bytes for the Number of buffers TAG */
2221        i+=4;  /* 4 bytes for the size of this TAG */
2222        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2223        i+=4;  /* 4 bytes for the buffer0  TAG */
2224
2225    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2226    	if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
2227            i+=4;   /* 4 bytes for the size of this TAG */
2228            i+=4;   /* 4 bytes for the actual data of this TAG */
2229            i+=4;   /* 4 bytes for the actual data of this TAG */
2230    	}
2231    	else {
2232    	    i+=4;   /* 4 bytes for the size of this TAG */
2233                i += (pComponentPrivate->sAPP5.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2234                if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
2235                    i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2236                }
2237    	}
2238
2239    	/* if thumbnail is set, configure it accordingly */
2240    	if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
2241            i+=4;  /* 4 bytes for the THUMB INDEX TAG */
2242            i+=4;  /* 4 bytes for the size of this TAG */
2243            i+=4;  /* 4 bytes for the actual data for this TAG */
2244
2245            i+=4;  /* 4 bytes for the THUMB W TAG */
2246            i+=4;  /* 4 bytes for the size of this TAG */
2247            i+=4;  /* 4 bytes for the actual data for this TAG (width value) */
2248
2249            i+=4;  /* 4 bytes for the THUMB H TAG */
2250            i+=4;  /* 4 bytes for the size of this TAG */
2251            i+=4;  /* 4 bytes for the actual data for this TAG (height value) */
2252    	}
2253    }
2254    /* handle APP13 marker */
2255    if (pComponentPrivate->sAPP13.bMarkerEnabled) {
2256        i+=4;  /* 4 bytes for the Number of buffers TAG */
2257        i+=4;  /* 4 bytes for the size of this TAG */
2258        i+=4;  /* 4 bytes for the actual number of buffers (just 1 buffer) */
2259        i+=4;  /* 4 bytes for the buffer0  TAG */
2260
2261    	/* if explicity specified by application, set the marker from algo, otherwise set it from application */
2262    	if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
2263            i+=4;   /* 4 bytes for the size of this TAG */
2264            i+=4;   /* 4 bytes for the actual data of this TAG */
2265            i+=4;   /* 4 bytes for the actual data of this TAG */
2266    	}
2267    	else {
2268            i+=4;   /* 4 bytes for the size of this TAG */
2269            i += (pComponentPrivate->sAPP13.nMarkerSize/4)*4;  /* x bytes for the actual buffer data for this TAG */
2270            if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
2271                i +=4;  /* 4 extra bytes if the size is not divisible by 4*/
2272            }
2273    	}
2274    }
2275
2276    /* comment flag needed */
2277    i+=4;   /* 4 bytes for the size of this TAG */
2278
2279    /* handle CommentFlag */
2280    if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
2281        i+=4;  /* 4 bytes for the Comment TAG */
2282        i+=4;  /* 4 bytes for the size of this TAG */
2283            /* comment string upper limit is 256 bytes */
2284            i+=256;
2285    }
2286    else {
2287        i+=4;  /* 4 bytes for the Comment TAG */
2288        i+=4;  /* 4 bytes for the size of this TAG */
2289    }
2290
2291    return i;
2292}
2293
2294static OMX_ERRORTYPE SetJpegEncInPortParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_U32* new_params)
2295{
2296    OMX_ERRORTYPE eError = OMX_ErrorNone;
2297    int i = 1;
2298
2299    /* Set Custom Quantization Table */
2300    if (pComponentPrivate->bSetLumaQuantizationTable && pComponentPrivate->bSetChromaQuantizationTable) {
2301        new_params[i++] = DYNPARAMS_QUANTTABLE;
2302        new_params[i++] = 256; /* 2 tables * 64 entries * 2(16bit entries) */
2303        OMX_U16 *temp = (OMX_U16 *)&new_params[i];
2304        int j, k;
2305        for (j = 0; j < 64; j++) {
2306            temp[j] = pComponentPrivate->pCustomLumaQuantTable->nQuantizationMatrix[j];
2307        }
2308        for (k = 0; k < 64; k++, j++) {
2309            temp[j] = pComponentPrivate->pCustomChromaQuantTable->nQuantizationMatrix[k];
2310        }
2311        i += 64; /* 256 / 4 */
2312    }
2313
2314    /* Set Custom Huffman Table */
2315    if (pComponentPrivate->bSetHuffmanTable) {
2316        new_params[i++] = DYNPARAMS_HUFFMANTABLE;
2317        new_params[i++] = sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE); /* 2572 % 4 = 0 */
2318
2319        memcpy((OMX_U8 *)(&new_params[i]), &(pComponentPrivate->pHuffmanTable->sHuffmanTable), sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE));
2320        if (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4) {
2321            i += (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) + (4 - (sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE) % 4)))/4 ;
2322        }
2323        else {
2324           i += sizeof(JPEGENC_CUSTOM_HUFFMAN_TABLE)/4;
2325        }
2326    }
2327
2328    /* handle APP0 marker (JFIF)*/
2329    if(pComponentPrivate->sAPP0.bMarkerEnabled) {
2330        new_params[i++] = APP0_NUMBUF;
2331        new_params[i++] = 4;
2332        new_params[i++] = 1;
2333
2334        /* set default APP0 BUFFER */
2335        new_params[i++] = APP0_BUFFER;
2336
2337        /* if thumbnail is set, or if explicity specified by application, set the marker from algo, otherwise set it from application */
2338        if ((pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0)
2339        	|| pComponentPrivate->sAPP0.nMarkerSize <= 0) {
2340            new_params[i++] = 4;
2341            new_params[i++] = 0;
2342        }
2343        else {
2344            new_params[i++] = pComponentPrivate->sAPP0.nMarkerSize;
2345            memcpy(new_params + i, pComponentPrivate->sAPP0.pMarkerBuffer, pComponentPrivate->sAPP0.nMarkerSize);
2346            i += pComponentPrivate->sAPP0.nMarkerSize / 4;
2347            if (pComponentPrivate->sAPP0.nMarkerSize % 4) {
2348            	i ++;
2349            }
2350        }
2351
2352        /* if thumbnail is set, configure it accordingly */
2353        if (pComponentPrivate->sAPP0.nThumbnailWidth > 0 && pComponentPrivate->sAPP0.nThumbnailHeight > 0) {
2354            new_params[i++] = APP0_THUMB_INDEX;
2355            new_params[i++] = 4;
2356            new_params[i++] = 1;
2357
2358            new_params[i++] = APP0_THUMB_W;
2359            new_params[i++] = 4;
2360            new_params[i++] = pComponentPrivate->sAPP0.nThumbnailWidth;
2361
2362            new_params[i++] = APP0_THUMB_H;
2363            new_params[i++] = 4;
2364            new_params[i++] = pComponentPrivate->sAPP0.nThumbnailHeight;
2365        }
2366    }
2367
2368    /* handle APP1 marker (EXIF)*/
2369    if(pComponentPrivate->sAPP1.bMarkerEnabled) {
2370        new_params[i++] = APP1_NUMBUF;
2371        new_params[i++] = 4;
2372        new_params[i++] = 1;
2373
2374        /* set default APP1 BUFFER */
2375        new_params[i++] = APP1_BUFFER;
2376
2377        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2378        if (pComponentPrivate->sAPP1.nMarkerSize <= 0) {
2379            new_params[i++] = 8;
2380            new_params[i++] = 0;
2381            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2382        }
2383        else {
2384            new_params[i++] = pComponentPrivate->sAPP1.nMarkerSize;
2385            memcpy(new_params + i, pComponentPrivate->sAPP1.pMarkerBuffer, pComponentPrivate->sAPP1.nMarkerSize);
2386            i += pComponentPrivate->sAPP1.nMarkerSize / 4;
2387            if (pComponentPrivate->sAPP1.nMarkerSize % 4) {
2388            	i ++;
2389            }
2390        }
2391
2392        /* if thumbnail is set, configure it accordingly */
2393        if (pComponentPrivate->sAPP1.nThumbnailWidth > 0 && pComponentPrivate->sAPP1.nThumbnailHeight > 0) {
2394            new_params[i++] = APP1_THUMB_INDEX;
2395            new_params[i++] = 4;
2396            new_params[i++] = 1;
2397
2398            new_params[i++] = APP1_THUMB_W;
2399            new_params[i++] = 4;
2400            new_params[i++] = pComponentPrivate->sAPP1.nThumbnailWidth;
2401
2402            new_params[i++] = APP1_THUMB_H;
2403            new_params[i++] = 4;
2404            new_params[i++] = pComponentPrivate->sAPP1.nThumbnailHeight;
2405        }
2406    }
2407
2408
2409    /* handle APP5 marker */
2410    if(pComponentPrivate->sAPP5.bMarkerEnabled) {
2411        new_params[i++] = APP5_NUMBUF;
2412        new_params[i++] = 4;
2413        new_params[i++] = 1;
2414
2415        /* set default APP5 BUFFER */
2416        new_params[i++] = APP5_BUFFER;
2417
2418        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2419        if (pComponentPrivate->sAPP5.nMarkerSize <= 0) {
2420            new_params[i++] = 8;
2421            new_params[i++] = 0;
2422            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2423        }
2424        else {
2425            new_params[i++] = pComponentPrivate->sAPP5.nMarkerSize;
2426            memcpy(new_params + i, pComponentPrivate->sAPP5.pMarkerBuffer, pComponentPrivate->sAPP5.nMarkerSize);
2427            i += pComponentPrivate->sAPP5.nMarkerSize / 4;
2428            if (pComponentPrivate->sAPP5.nMarkerSize % 4) {
2429            	i ++;
2430            }
2431        }
2432
2433        /* if thumbnail is set, configure it accordingly */
2434        if (pComponentPrivate->sAPP5.nThumbnailWidth > 0 && pComponentPrivate->sAPP5.nThumbnailHeight > 0) {
2435            new_params[i++] = APP5_THUMB_INDEX;
2436            new_params[i++] = 4;
2437            new_params[i++] = 1;
2438
2439            new_params[i++] = APP5_THUMB_W;
2440            new_params[i++] = 4;
2441            new_params[i++] = pComponentPrivate->sAPP5.nThumbnailWidth;
2442
2443            new_params[i++] = APP5_THUMB_H;
2444            new_params[i++] = 4;
2445            new_params[i++] = pComponentPrivate->sAPP5.nThumbnailHeight;
2446        }
2447    }
2448
2449
2450    /* handle APP13 marker */
2451    if(pComponentPrivate->sAPP13.bMarkerEnabled) {
2452        new_params[i++] = APP13_NUMBUF;
2453        new_params[i++] = 4;
2454        new_params[i++] = 1;
2455
2456        /* set default APP13 BUFFER */
2457        new_params[i++] = APP13_BUFFER;
2458
2459        /* if explicity specified by application, set the marker from algo, otherwise set it from application */
2460        if (pComponentPrivate->sAPP13.nMarkerSize <= 0) {
2461            new_params[i++] = 8;
2462            new_params[i++] = 0;
2463            new_params[i++] = 'F' | 'F' << 8 | 'F' << 16 | 'F' << 24;
2464        }
2465        else {
2466            new_params[i++] = pComponentPrivate->sAPP13.nMarkerSize;
2467            memcpy(new_params + i, pComponentPrivate->sAPP13.pMarkerBuffer, pComponentPrivate->sAPP13.nMarkerSize);
2468            i += pComponentPrivate->sAPP13.nMarkerSize / 4;
2469            if (pComponentPrivate->sAPP13.nMarkerSize % 4) {
2470            	i ++;
2471            }
2472        }
2473    }
2474
2475    new_params[i++] = COMMENT_BUFFER;
2476
2477    /* handle CommentFlag */
2478    if (pComponentPrivate->nCommentFlag == 1 && pComponentPrivate->pString_Comment) {
2479        new_params[i++] = strlen((char *)pComponentPrivate->pString_Comment)  + 4 ;
2480        new_params[i++] = 0;
2481        strncpy((char *)(new_params+i), (char *)pComponentPrivate->pString_Comment, 255);
2482    }
2483    else {
2484        new_params[i++] = 4;
2485        new_params[i++] = 0;
2486    }
2487
2488    /* now that we know the final size of the buffer, we can set it accordingly */
2489    new_params[0] = i * sizeof(OMX_U32);
2490
2491/*
2492    printf("=========DUMP of new_params array=============\n");
2493    int j;
2494    for (j=0; j< i*4; j++) {
2495    printf("new_params[%d] = 0x%x\n", j, *(((unsigned char *)new_params)+j));
2496    }
2497    printf("================================\n");
2498*/
2499    return eError;
2500}
2501
2502OMX_ERRORTYPE SetJpegEncInParams(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate)
2503{
2504    OMX_ERRORTYPE eError = OMX_ErrorNone;
2505    OMX_U8 *p = NULL;
2506    OMX_U32 params_size;
2507
2508    if (pComponentPrivate->InParams.pInParams) {
2509        p = (OMX_U8 *)pComponentPrivate->InParams.pInParams;
2510        p -= 128;
2511        OMX_FREE(p);
2512        pComponentPrivate->InParams.pInParams = NULL;
2513    }
2514
2515   /*alloc enough memory for params array*/
2516    params_size = CalculateParamsSize(pComponentPrivate);
2517    OMX_MALLOC(p, params_size + 256);
2518    p += 128;
2519    pComponentPrivate->InParams.pInParams = (OMX_U32 *)p;
2520    p = NULL;
2521    eError = SetJpegEncInPortParams(pComponentPrivate, pComponentPrivate->InParams.pInParams);
2522
2523EXIT:
2524    return eError;
2525}
2526
2527OMX_ERRORTYPE HandleJpegEncDataBuf_FromApp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate )
2528{
2529    OMX_ERRORTYPE eError = OMX_ErrorNone;
2530    OMX_BUFFERHEADERTYPE* pBuffHead = NULL;
2531    LCML_DSP_INTERFACE* pLcmlHandle = NULL;
2532    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
2533    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefOut = NULL;
2534    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2535    int ret;
2536
2537    OMX_CHECK_PARAM(pComponentPrivate);
2538
2539    pLcmlHandle = (LCML_DSP_INTERFACE*)pComponentPrivate->pLCML;
2540    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
2541    pPortDefOut = pComponentPrivate->pCompPort[JPEGENC_OUT_PORT]->pPortDef;
2542
2543    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleDataBuf_FromApp function\n");
2544    ret = read(pComponentPrivate->filled_inpBuf_Q[0], &(pBuffHead), sizeof(pBuffHead));
2545    if ( ret == -1 ) {
2546        OMX_PRCOMM4(pComponentPrivate->dbg, "Error while reading from the pipe\n");
2547    }
2548
2549    if (pBuffHead != NULL) {
2550        pBuffPrivate = pBuffHead->pInputPortPrivate;
2551    } else {
2552        eError = OMX_ErrorInsufficientResources;
2553          goto EXIT;
2554        }
2555
2556   if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2557        /* already returned to client */
2558        OMX_PRBUFFER4(pComponentPrivate->dbg, "this buffer %p already returned to client\n", pBuffHead);
2559        goto EXIT;
2560    }
2561
2562   if ((pComponentPrivate->nCurState != OMX_StateExecuting) ||
2563       (pComponentPrivate->nToState == OMX_StateIdle) ||
2564        (pPortDefIn->bEnabled == OMX_FALSE)) {
2565            if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2566                pComponentPrivate->nInPortOut ++;
2567                pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2568                OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty input buffer) %lu %lu %lu %lu\n",
2569                    pComponentPrivate->nInPortIn,
2570                    pComponentPrivate->nInPortOut,
2571                    pComponentPrivate->nOutPortIn,
2572                    pComponentPrivate->nOutPortOut);
2573                pComponentPrivate->cbInfo.EmptyBufferDone(
2574                           pComponentPrivate->pHandle,
2575                           pComponentPrivate->pHandle->pApplicationPrivate,
2576                           pBuffHead);
2577             }
2578             goto EXIT;
2579    }
2580
2581    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (HandleJpegEncDataBuf_FromApp) %lu %lu %lu %lu\n",
2582                    pComponentPrivate->nInPortIn,
2583                    pComponentPrivate->nInPortOut,
2584                    pComponentPrivate->nOutPortIn,
2585                    pComponentPrivate->nOutPortOut);
2586
2587#ifdef __PERF_INSTRUMENTATION__
2588    PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2589                      pBuffHead->pBuffer,
2590                      pBuffHead->nFilledLen,
2591                      PERF_ModuleCommonLayer);
2592#endif
2593
2594    if ((pBuffHead->nFlags == OMX_BUFFERFLAG_EOS) && (pBuffHead->nAllocLen == 0)) {
2595            OMX_PRBUFFER2(pComponentPrivate->dbg, "BufferFlag Set!!\n");
2596            pComponentPrivate->nFlags = OMX_BUFFERFLAG_EOS;
2597        pBuffHead->nFlags = 0;
2598    }
2599#if 0
2600    eError = SendDynamicParam(pComponentPrivate);
2601    if (eError != OMX_ErrorNone ) {
2602            OMX_PRDSP4(pComponentPrivate->dbg, "SETSTATUS failed...  %x\n", eError);
2603            goto EXIT;
2604    }
2605#endif
2606
2607    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nAllocLen = %d\n",(int)pBuffHead->nAllocLen);
2608    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->pBuffer = %p\n",pBuffHead->pBuffer);
2609    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead->nFilledLen = %d\n",(int)pBuffHead->nFilledLen);
2610    OMX_PRBUFFER1(pComponentPrivate->dbg, "pBuffHead = %p\n",pBuffHead);
2611
2612    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_DSP;
2613
2614    OMX_PRDSP2(pComponentPrivate->dbg, "Input: before queue buffer %p\n", pBuffHead);
2615        eError = LCML_QueueBuffer(
2616                                  pLcmlHandle->pCodecinterfacehandle,
2617                                  EMMCodecInputBuffer,
2618                                  pBuffHead->pBuffer,
2619                                  pPortDefIn->nBufferSize,
2620                                  pBuffHead->nFilledLen,
2621                                  (OMX_U8 *) pComponentPrivate->InParams.pInParams,
2622                                  pComponentPrivate->InParams.pInParams[0],
2623                                  (OMX_U8 *)pBuffHead);
2624
2625    OMX_PRDSP2(pComponentPrivate->dbg, "Input: after queue buffer %p\n", pBuffHead);
2626
2627    if ( eError ) {
2628        eError = OMX_ErrorInsufficientResources;
2629        OMX_PRDSP4(pComponentPrivate->dbg, "OMX_ErrorInsufficientResources\n");
2630        goto EXIT;
2631    }
2632    OMX_PRINT1(pComponentPrivate->dbg, "Error is %x\n",eError);
2633    EXIT:
2634
2635    return eError;
2636}
2637
2638
2639OMX_ERRORTYPE HandleJpegEncDataBuf_FromDsp(JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead)
2640{
2641
2642    OMX_ERRORTYPE eError                 = OMX_ErrorNone;
2643    OMX_BUFFERHEADERTYPE* pInpBuf        = NULL;
2644    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2645
2646    OMX_CHECK_PARAM(pComponentPrivate);
2647
2648    pInpBuf = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pBufferPrivate[0]->pBufferHdr;
2649
2650    pBuffPrivate = pBuffHead->pOutputPortPrivate;
2651
2652        if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2653           OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
2654           pComponentPrivate->nOutPortOut --;
2655           OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return output buffer) %lu %lu %lu %lu\n",
2656                                        pComponentPrivate->nInPortIn,
2657                                        pComponentPrivate->nInPortOut,
2658                                        pComponentPrivate->nOutPortIn,
2659                                        pComponentPrivate->nOutPortOut);
2660
2661           goto EXIT;
2662        }
2663
2664#ifdef __PERF_INSTRUMENTATION__
2665        PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2666                          pBuffHead->pBuffer,
2667                          pBuffHead->nFilledLen,
2668                          PERF_ModuleHLMM);
2669#endif
2670    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2671
2672        if (pBuffHead->pMarkData) {
2673           OMX_PRBUFFER2(pComponentPrivate->dbg, "get Mark buffer %p %p %p\n", pBuffHead->pMarkData, pBuffHead->hMarkTargetComponent, pComponentPrivate->pHandle);
2674        }
2675
2676        if (pBuffHead->pMarkData && pBuffHead->hMarkTargetComponent == pComponentPrivate->pHandle) {
2677           OMX_PRBUFFER2(pComponentPrivate->dbg, "send OMX_MarkEvent\n");
2678           pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2679                                                pComponentPrivate->pHandle->pApplicationPrivate,
2680                                                OMX_EventMark,
2681                                                JPEGENC_OUT_PORT,
2682                                                0,
2683                                                pBuffHead->pMarkData);
2684        }
2685
2686    OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (return empty output buffer) %lu %lu %lu %lu\n",
2687                    pComponentPrivate->nInPortIn,
2688                    pComponentPrivate->nInPortOut,
2689                    pComponentPrivate->nOutPortIn,
2690                    pComponentPrivate->nOutPortOut);
2691    OMX_PRBUFFER1(pComponentPrivate->dbg, "Output: before fillbufferdone %p\n", pBuffHead);
2692        pComponentPrivate->cbInfo.FillBufferDone(pComponentPrivate->pHandle,
2693                        pComponentPrivate->pHandle->pApplicationPrivate,
2694                        pBuffHead);
2695
2696    if ( pComponentPrivate->nFlags & OMX_BUFFERFLAG_EOS )   {
2697
2698        pBuffHead->nFlags |= OMX_BUFFERFLAG_EOS;
2699        pComponentPrivate->cbInfo.EventHandler (pComponentPrivate->pHandle,
2700                                                pComponentPrivate->pHandle->pApplicationPrivate,
2701                                                OMX_EventBufferFlag,
2702                                                JPEGENC_OUT_PORT,
2703                                                OMX_BUFFERFLAG_EOS,
2704                                                NULL);
2705
2706        pComponentPrivate->nFlags = 0;
2707    }
2708
2709    EXIT:
2710    return eError;
2711}
2712
2713
2714
2715OMX_ERRORTYPE HandleJpegEncFreeDataBuf( JPEGENC_COMPONENT_PRIVATE *pComponentPrivate, OMX_BUFFERHEADERTYPE* pBuffHead )
2716{
2717    OMX_ERRORTYPE eError = OMX_ErrorNone;
2718    OMX_PARAM_PORTDEFINITIONTYPE* pPortDefIn = NULL;
2719    JPEGENC_BUFFER_PRIVATE* pBuffPrivate = NULL;
2720    OMX_HANDLETYPE hTunnelComponent = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->hTunnelComponent;
2721
2722    OMX_CHECK_PARAM(pComponentPrivate);
2723    OMX_PRINT1(pComponentPrivate->dbg, "Inside HandleFreeDataBuf function \n");
2724    pPortDefIn = pComponentPrivate->pCompPort[JPEGENC_INP_PORT]->pPortDef;
2725
2726    /* pBuffHead->nAllocLen = pPortDefIn->nBufferSize; */
2727    pBuffPrivate = pBuffHead->pInputPortPrivate;
2728
2729    OMX_PRCOMM2(pComponentPrivate->dbg, "hTunnelComponent = %p\n" ,hTunnelComponent );
2730    OMX_PRINT1(pComponentPrivate->dbg, "pComponentPrivate->pHandle = %p\n",pComponentPrivate->pHandle);
2731
2732    if (pBuffPrivate->eBufferOwner == JPEGENC_BUFFER_CLIENT) {
2733        OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer %p already at the client side\n", pBuffHead);
2734        pComponentPrivate->nInPortOut --;
2735        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (FromDsp escape return input buffer) %lu %lu %lu %lu\n",
2736                                        pComponentPrivate->nInPortIn,
2737                                        pComponentPrivate->nInPortOut,
2738                                        pComponentPrivate->nOutPortIn,
2739                                        pComponentPrivate->nOutPortOut);
2740
2741           goto EXIT;
2742    }
2743
2744    if(hTunnelComponent != NULL)
2745    {
2746
2747#ifdef __PERF_INSTRUMENTATION__
2748            PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2749                              pBuffHead->pBuffer,
2750                              0,
2751                              PERF_ModuleLLMM);
2752#endif
2753    pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_TUNNEL_COMPONENT;
2754        eError = OMX_FillThisBuffer(hTunnelComponent, pBuffHead);
2755
2756    }
2757    else {
2758
2759#ifdef __PERF_INSTRUMENTATION__
2760            PERF_SendingFrame(pComponentPrivate->pPERFcomp,
2761                              pBuffHead->pBuffer,
2762                              0,
2763                              PERF_ModuleHLMM);
2764#endif
2765        pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_CLIENT;
2766        OMX_PRBUFFER2(pComponentPrivate->dbg, "before emptybufferdone in HandleJpegEncFreeDataBuf %p\n", pBuffHead);
2767        pComponentPrivate->cbInfo.EmptyBufferDone(
2768                 pComponentPrivate->pHandle,
2769                 pComponentPrivate->pHandle->pApplicationPrivate,
2770                 pBuffHead);
2771    }
2772
2773    EXIT:
2774    return eError;
2775
2776}
2777
2778
2779/* -------------------------------------------------------------------*/
2780/**
2781  *  Callback() function will be called LCML component to write the msg
2782  *
2783  * @param msgBuffer                 This buffer will be returned by the LCML
2784  *
2785  * @retval OMX_NoError              Success, ready to roll
2786  *         OMX_Error_BadParameter   The input parameter pointer is null
2787 **/
2788/*-------------------------------------------------------------------*/
2789OMX_ERRORTYPE JpegEncLCML_Callback (TUsnCodecEvent event,void * argsCb [10])
2790{
2791    OMX_ERRORTYPE eError = OMX_ErrorNone;
2792    JPEGENC_BUFFER_PRIVATE *pBuffPrivate = NULL;
2793    JPEG_PORT_TYPE *pPortType = NULL;
2794    int i;
2795
2796    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
2797    OMX_COMPONENTTYPE *pHandle;
2798
2799    if ( ((LCML_DSP_INTERFACE*)argsCb[6] ) != NULL ) {
2800        pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE*)((LCML_DSP_INTERFACE*)argsCb[6])->pComponentPrivate;
2801        pHandle = (OMX_COMPONENTTYPE *)pComponentPrivate->pHandle;
2802    }
2803    else {
2804        OMXDBG_PRINT(stderr, DSP, 5, 0, "wrong in LCML callback, exit\n");
2805	 goto EXIT;
2806    }
2807    OMX_PRDSP0(pComponentPrivate->dbg, "Event = %d\n", event);
2808
2809    if ( event == EMMCodecBufferProcessed ) {
2810    if ( (int)argsCb [0] == EMMCodecOuputBuffer ) {
2811        OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
2812        pBuffPrivate = pBuffHead->pOutputPortPrivate;
2813
2814        pComponentPrivate->nOutPortOut ++;
2815#ifdef __PERF_INSTRUMENTATION__
2816        PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
2817                           PREF(pBuffHead,pBuffer),
2818                           PREF(pBuffHead,nFilledLen),
2819                           PERF_ModuleCommonLayer);
2820#endif
2821        OMX_PRDSP1(pComponentPrivate->dbg, "argsCb[8] is %d\n", (int)(argsCb[8]));
2822        pBuffHead->nFilledLen = (OMX_U32) argsCb[8];
2823
2824        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (LCML for output buffer %p) %lu %lu %lu %lu\n", pBuffHead,
2825                    pComponentPrivate->nInPortIn,
2826                    pComponentPrivate->nInPortOut,
2827                    pComponentPrivate->nOutPortIn,
2828                    pComponentPrivate->nOutPortOut);
2829
2830        pPortType = pComponentPrivate->pCompPort[JPEGENC_INP_PORT];
2831        for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
2832            if (pComponentPrivate->nOutPortOut > 10) {
2833                OMX_PRBUFFER1(pComponentPrivate->dbg, "pPortType->sBufferFlagTrack[i].buffer_id %lu\n", pPortType->sBufferFlagTrack[i].buffer_id);
2834            }
2835            if (pPortType->sBufferFlagTrack[i].buffer_id == pComponentPrivate->nOutPortOut) {
2836                OMX_PRBUFFER1(pComponentPrivate->dbg, "output buffer %lu has flag %lx\n",
2837                           pPortType->sBufferFlagTrack[i].buffer_id,
2838                           pPortType->sBufferFlagTrack[i].flag);
2839                pBuffHead->nFlags = pPortType->sBufferFlagTrack[i].flag;
2840                pPortType->sBufferFlagTrack[i].flag = 0;
2841                pPortType->sBufferFlagTrack[i].buffer_id = 0xFFFFFFFF;
2842                break;
2843            }
2844        }
2845        for (i = 0; i < (int)(pPortType->pPortDef->nBufferCountActual); i ++) {
2846            if (pPortType->sBufferMarkTrack[i].buffer_id == pComponentPrivate->nInPortOut) {
2847                OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer ID %lu has mark (output port)\n", pPortType->sBufferMarkTrack[i].buffer_id);
2848                pBuffHead->pMarkData = pPortType->sBufferMarkTrack[i].pMarkData;
2849                pBuffHead->hMarkTargetComponent = pPortType->sBufferMarkTrack[i].hMarkTargetComponent;
2850                pPortType->sBufferMarkTrack[i].buffer_id = 0xFFFFFFFF;
2851                break;
2852            }
2853        }
2854
2855        OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
2856        if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2857            OMX_PRBUFFER2(pComponentPrivate->dbg, "return output buffer %p from LCML_Callback (%d)\n",
2858                           pBuffHead,
2859                           pBuffPrivate->eBufferOwner);
2860            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
2861            OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Filled (output) Data from DSP %p\n", pBuffHead);
2862            eError = HandleJpegEncDataBuf_FromDsp(pComponentPrivate, pBuffHead);
2863        }
2864    }
2865
2866    if ((int) argsCb [0] == EMMCodecInputBuffer ) {
2867        OMX_BUFFERHEADERTYPE* pBuffHead = (OMX_BUFFERHEADERTYPE*)argsCb[7];
2868        pBuffPrivate = pBuffHead->pInputPortPrivate;
2869
2870       pComponentPrivate->nInPortOut ++;
2871        OMX_PRBUFFER2(pComponentPrivate->dbg, "buffer summary (LCML for InputBuffer %p) %lu %lu %lu %lu\n", pBuffHead,
2872                    pComponentPrivate->nInPortIn,
2873                    pComponentPrivate->nInPortOut,
2874                    pComponentPrivate->nOutPortIn,
2875                    pComponentPrivate->nOutPortOut);
2876#ifdef __PERF_INSTRUMENTATION__
2877	PERF_ReceivedFrame(pComponentPrivate->pPERFcomp,
2878			   PREF(pBuffHead,pBuffer),
2879			   0,
2880			   PERF_ModuleCommonLayer);
2881#endif
2882	OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodec Args -> %x, %p\n", (int)argsCb[1] , (void *)(argsCb[5]));
2883        if (pBuffPrivate->eBufferOwner != JPEGENC_BUFFER_CLIENT) {
2884            OMX_PRBUFFER2(pComponentPrivate->dbg, "return input buffer %p from LCML_Callback (%d)\n",
2885                           pBuffHead,
2886                           pBuffPrivate->eBufferOwner);
2887            pBuffPrivate->eBufferOwner = JPEGENC_BUFFER_COMPONENT_OUT;
2888            OMX_PRBUFFER2(pComponentPrivate->dbg, "LCML_Callback - Emptied (input) Data from DSP %p\n", pBuffHead);
2889            eError = HandleJpegEncFreeDataBuf(pComponentPrivate, pBuffHead);
2890        }
2891    }
2892    goto EXIT;
2893    } /* end     if ( event == EMMCodecBufferProcessed ) */
2894
2895    if ( event == EMMCodecProcessingStoped ) {
2896        OMX_PRDSP2(pComponentPrivate->dbg, "Entering To EMMCodecProcessingStoped \n");
2897        OMX_PRBUFFER1(pComponentPrivate->dbg, "buffer summary (Stopped) %lu %lu %lu %lu\n",
2898                    pComponentPrivate->nInPortIn,
2899                    pComponentPrivate->nInPortOut,
2900                    pComponentPrivate->nOutPortIn,
2901                    pComponentPrivate->nOutPortOut);
2902        pComponentPrivate->bDSPStopAck = OMX_TRUE;
2903        OMX_PRSTATE2(pComponentPrivate->dbg, "to state is %d\n", pComponentPrivate->nToState);
2904
2905
2906        /* if (pComponentPrivate->nToState == OMX_StateIdle) { */
2907            pComponentPrivate->ExeToIdleFlag |= JPEGE_DSPSTOP;
2908        /* } */
2909
2910        OMX_TRACE1(pComponentPrivate->dbg, "before stop signal\n");
2911
2912        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
2913        pthread_cond_signal(&pComponentPrivate->stop_cond);
2914        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
2915
2916        OMX_TRACE1(pComponentPrivate->dbg, "after stop signal\n");
2917
2918        goto EXIT;
2919    }
2920
2921    if ( event == EMMCodecDspError ) {
2922
2923       OMX_PRDSP4(pComponentPrivate->dbg, "in EMMCodecDspError EMMCodec Args -> %x, %x\n", (int)argsCb[4] , (int)argsCb[5]);
2924       if ((int)argsCb[4] != 0x1 || (int)argsCb[5] != 0x500) {
2925	   OMX_PRDSP4(pComponentPrivate->dbg, "DSP Error %x %x\n", (int)(argsCb[4]), (int)(argsCb[5]));
2926           pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2927                                     pComponentPrivate->pHandle->pApplicationPrivate,
2928                                     OMX_EventError,
2929                                     OMX_ErrorHardware,
2930                                     OMX_TI_ErrorCritical,
2931                                     NULL);
2932
2933            pComponentPrivate->nCurState = OMX_StateInvalid;
2934            pComponentPrivate->cbInfo.EventHandler(pHandle,
2935                                                   pHandle->pApplicationPrivate,
2936                                                   OMX_EventError,
2937                                                   OMX_ErrorInvalidState,
2938                                                   OMX_TI_ErrorCritical,
2939                                                   "DSP Hardware Error");
2940           goto EXIT;
2941       }
2942#ifdef DSP_MMU_FAULT_HANDLING
2943        /* Cheking for MMU_fault */
2944        if((argsCb[4] == (void *)NULL) && (argsCb[5] == (void*)NULL)) {
2945            //JPEGENC_DPRINT("DSP MMU_Fault");
2946            pComponentPrivate->nCurState = OMX_StateInvalid;
2947            pComponentPrivate->cbInfo.EventHandler(pHandle,
2948                                                   pHandle->pApplicationPrivate,
2949                                                   OMX_EventError,
2950                                                   OMX_ErrorInvalidState,
2951                                                   OMX_TI_ErrorCritical,
2952                                                   "DSP MMU FAULT");
2953        }
2954#endif
2955    }
2956    if (event == EMMCodecInternalError) {
2957        eError = OMX_ErrorHardware;
2958        OMX_PRDSP4(pComponentPrivate->dbg, "JPEG-E: EMMCodecInternalError\n");
2959        pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
2960                                               pComponentPrivate->pHandle->pApplicationPrivate,
2961                                               OMX_EventError,
2962                                               OMX_ErrorHardware,
2963                                               OMX_TI_ErrorCritical,
2964                                               NULL);
2965        goto EXIT;
2966    }
2967    if ( event == EMMCodecProcessingPaused ) {
2968        OMX_PRDSP2(pComponentPrivate->dbg, "ENTERING TO EMMCodecProcessingPaused JPEG Encoder\n");
2969        if (pComponentPrivate != NULL) {
2970            pComponentPrivate->bDSPStopAck = OMX_TRUE;
2971            pComponentPrivate->nCurState = OMX_StatePause;
2972            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,pComponentPrivate->pHandle->pApplicationPrivate,
2973                                                   OMX_EventCmdComplete, OMX_CommandStateSet, pComponentPrivate->nCurState, NULL);
2974        }
2975    }
2976    if (event == EMMCodecStrmCtrlAck) {
2977        OMX_PRDSP2(pComponentPrivate->dbg, "EMMCodecStrmCtrlAck\n");
2978        if ((int)argsCb [0] == USN_ERR_NONE) {
2979            OMX_PRDSP2(pComponentPrivate->dbg, "Callback: no error\n");
2980            pComponentPrivate->bFlushComplete = OMX_TRUE;
2981            pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
2982            pthread_cond_signal(&pComponentPrivate->flush_cond);
2983            pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
2984        }
2985    }
2986    if (event == EMMCodecAlgCtrlAck) {
2987        OMX_PRDSP2(pComponentPrivate->dbg, "jpeg-enc: EMMCodecAlgCtrlAck\n");
2988        pComponentPrivate->bAckFromSetStatus = 1;
2989        /*
2990        pthread_mutex_lock(&pComponentPrivate->jpege_mutex);
2991        pthread_cond_signal(&pComponentPrivate->control_cond);
2992        pthread_mutex_unlock(&pComponentPrivate->jpege_mutex);
2993        */
2994    }
2995
2996EXIT:
2997    OMX_PRDSP1(pComponentPrivate->dbg, "Exiting the LCML_Callback function\n");
2998    return eError;
2999}
3000/*-------------------------------------------------------------------*/
3001/**
3002  * IsTIOMXComponent()
3003  *
3004  * Check if the component is TI component.
3005  *
3006  * @param hTunneledComp Component Tunnel Pipe
3007  *
3008  * @retval OMX_TRUE   Input is a TI component.
3009  *         OMX_FALSE  Input is a not a TI component.
3010  *
3011  **/
3012/*-------------------------------------------------------------------*/
3013OMX_BOOL IsTIOMXComponent(OMX_HANDLETYPE hComp)
3014{
3015    OMX_ERRORTYPE eError = OMX_ErrorNone;
3016    OMX_STRING pTunnelcComponentName = NULL;
3017    OMX_VERSIONTYPE* pTunnelComponentVersion = NULL;
3018    OMX_VERSIONTYPE* pSpecVersion = NULL;
3019    OMX_UUIDTYPE* pComponentUUID = NULL;
3020    char *pSubstring = NULL;
3021    OMX_BOOL bResult = OMX_TRUE;
3022
3023    OMX_MALLOC(pTunnelcComponentName, 128);
3024    OMX_MALLOC(pTunnelComponentVersion, sizeof(OMX_VERSIONTYPE));
3025    OMX_MALLOC(pSpecVersion, sizeof(OMX_VERSIONTYPE));
3026    OMX_MALLOC(pComponentUUID, sizeof(OMX_UUIDTYPE));
3027
3028    eError = OMX_GetComponentVersion (hComp, pTunnelcComponentName, pTunnelComponentVersion, pSpecVersion, pComponentUUID);
3029
3030    /* Check if tunneled component is a TI component */
3031    pSubstring = strstr(pTunnelcComponentName, "OMX.TI.");
3032    if(pSubstring == NULL) {
3033        bResult = OMX_FALSE;
3034    }
3035
3036EXIT:
3037    OMX_FREE(pTunnelcComponentName);
3038    OMX_FREE(pTunnelComponentVersion);
3039    OMX_FREE(pSpecVersion);
3040    OMX_FREE(pComponentUUID);
3041    return bResult;
3042} /* End of IsTIOMXComponent */
3043
3044#ifdef RESOURCE_MANAGER_ENABLED
3045/* ========================================================================== */
3046/**
3047 *  ResourceManagerCallback() - handle callbacks from Resource Manager
3048 * @param cbData    Resource Manager Command Data Structure
3049 * @return: void
3050  **/
3051/* ========================================================================== */
3052
3053void ResourceManagerCallback(RMPROXY_COMMANDDATATYPE cbData)
3054{
3055    OMX_COMMANDTYPE Cmd = OMX_CommandStateSet;
3056    OMX_COMPONENTTYPE *pHandle = (OMX_COMPONENTTYPE *)cbData.hComponent;
3057    JPEGENC_COMPONENT_PRIVATE *pComponentPrivate = NULL;
3058    OMX_ERRORTYPE RM_Error = *(cbData.RM_Error);
3059
3060    pComponentPrivate = (JPEGENC_COMPONENT_PRIVATE *)pHandle->pComponentPrivate;
3061    OMX_PRINT1(pComponentPrivate->dbg, "RM_Error = %x\n", RM_Error);
3062
3063    if (RM_Error == OMX_RmProxyCallback_ResourcesPreempted) {
3064
3065        pComponentPrivate->bPreempted = 1;
3066
3067        if (pComponentPrivate->nCurState == OMX_StateExecuting ||
3068            pComponentPrivate->nCurState == OMX_StatePause) {
3069
3070            pComponentPrivate->cbInfo.EventHandler(pComponentPrivate->pHandle,
3071                                                   pComponentPrivate->pHandle->pApplicationPrivate,
3072                                                   OMX_EventError,
3073                                                   OMX_ErrorResourcesPreempted,
3074                                                   OMX_TI_ErrorSevere,
3075                                                   NULL);
3076
3077            pComponentPrivate->nToState = OMX_StateIdle;
3078            OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to IDLE State.\n");
3079        }
3080        else if (pComponentPrivate->nCurState == OMX_StateIdle){
3081            pComponentPrivate->nToState = OMX_StateLoaded;
3082            OMX_PRSTATE2(pComponentPrivate->dbg, "Component Preempted. Going to LOADED State.\n");
3083        }
3084
3085#ifdef __PERF_INSTRUMENTATION__
3086        PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
3087#endif
3088
3089        write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
3090        write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
3091
3092    }
3093    else if (RM_Error == OMX_RmProxyCallback_ResourcesAcquired ){
3094
3095        if (pComponentPrivate->nCurState == OMX_StateWaitForResources) /* Wait for Resource Response */
3096        {
3097            pComponentPrivate->cbInfo.EventHandler (
3098                            pHandle, pHandle->pApplicationPrivate,
3099                            OMX_EventResourcesAcquired, 0,0,
3100                            NULL);
3101
3102            pComponentPrivate->nToState = OMX_StateIdle;
3103
3104#ifdef __PERF_INSTRUMENTATION__
3105            PERF_SendingCommand(pComponentPrivate->pPERF, Cmd, pComponentPrivate->nToState, PERF_ModuleComponent);
3106#endif
3107
3108            write (pComponentPrivate->nCmdPipe[1], &Cmd, sizeof(Cmd));
3109            write (pComponentPrivate->nCmdDataPipe[1], &(pComponentPrivate->nToState) ,sizeof(OMX_U32));
3110            OMX_PRMGR2(pComponentPrivate->dbg, "OMX_RmProxyCallback_ResourcesAcquired.\n");
3111        }
3112	}
3113	else if (RM_Error == OMX_RmProxyCallback_FatalError){
3114
3115		/* Deinitialize the component...no error should be returned from
3116		 * this function. It should clean the system as much as possible */
3117		if ( pComponentPrivate->pLCML != NULL && pComponentPrivate->isLCMLActive) {
3118                	LCML_ControlCodec(((LCML_DSP_INTERFACE*)pComponentPrivate->pLCML)->pCodecinterfacehandle, EMMCodecControlDestroy, NULL);
3119                     	dlclose(pComponentPrivate->pDllHandle);
3120                	pComponentPrivate->pLCML = NULL;
3121                	pComponentPrivate->isLCMLActive = 0;
3122		}
3123
3124		pComponentPrivate->nCurState = OMX_StateInvalid;
3125		pComponentPrivate->cbInfo.EventHandler(pHandle,
3126											   pHandle->pApplicationPrivate,
3127											   OMX_EventError,
3128											   OMX_ErrorInvalidState,
3129											   OMX_TI_ErrorSevere,
3130											   NULL);
3131	}
3132}
3133#endif
3134
3135void LinkedList_Create(LinkedList *LinkedList) {
3136    LinkedList->pRoot = NULL;
3137    pthread_mutex_init(&LinkedList->lock, NULL);
3138}
3139
3140void LinkedList_AddElement(LinkedList *LinkedList, void *pValue) {
3141    /* create new node and fill the value */
3142    Node *pNewNode = (Node *)malloc(sizeof(Node));
3143    pNewNode->pValue = (void *)pValue;
3144    /*printf("LinkedList:::: Pointer=%p has been added.\n", pNewNode->pValue); */
3145    /* add new node on the root to implement quick FIFO */
3146    /* modify new node pointers */
3147
3148    pthread_mutex_lock(&LinkedList->lock);
3149    if(LinkedList->pRoot == NULL) {
3150        pNewNode->pNextNode = NULL;
3151    }
3152    else {
3153         pNewNode->pNextNode = LinkedList->pRoot;
3154    }
3155    /*modify root */
3156    LinkedList->pRoot = pNewNode;
3157    pthread_mutex_unlock(&LinkedList->lock);
3158}
3159
3160void LinkedList_FreeElement(LinkedList *LinkedList, void *pValue) {
3161    pthread_mutex_lock(&LinkedList->lock);
3162    Node *pNode = LinkedList->pRoot;
3163    Node *pPastNode = NULL;
3164    while (pNode != NULL) {
3165        if (pNode->pValue == pValue) {
3166            Node *pTempNode = pNode->pNextNode;
3167            if(pPastNode == NULL) {
3168                LinkedList->pRoot = pTempNode;
3169            }
3170            else {
3171                pPastNode->pNextNode = pTempNode;
3172            }
3173            /*printf("LinkedList:::: Pointer=%p has been freed\n", pNode->pValue); */
3174            free(pNode->pValue);
3175            pNode->pValue = NULL;
3176            free(pNode);
3177            pNode = NULL;
3178            break;
3179        }
3180        pPastNode = pNode;
3181        pNode = pNode->pNextNode;
3182    }
3183    pthread_mutex_unlock(&LinkedList->lock);
3184}
3185
3186void LinkedList_FreeAll(LinkedList *LinkedList) {
3187    Node *pTempNode;
3188    int nodes = 0;
3189    pthread_mutex_lock(&LinkedList->lock);
3190    while (LinkedList->pRoot != NULL) {
3191        pTempNode = LinkedList->pRoot->pNextNode;
3192        /*printf("LinkedList:::: Pointer=%p has been freed\n", LinkedList->pRoot->pValue); */
3193        if(LinkedList->pRoot->pValue != NULL) {
3194            free(LinkedList->pRoot->pValue);
3195            LinkedList->pRoot->pValue = NULL;
3196        }
3197        free(LinkedList->pRoot);
3198        LinkedList->pRoot = pTempNode;
3199        nodes++;
3200    }
3201    pthread_mutex_unlock(&LinkedList->lock);
3202    /*printf("==================No. of deleted nodes: %d=======================================\n\n", nodes); */
3203}
3204
3205void LinkedList_DisplayAll(LinkedList *LinkedList) {
3206    Node *pNode = LinkedList->pRoot;
3207    int nodes = 0;
3208    printf("\n================== Displaying contents of linked list=%p=====================\n", LinkedList);
3209    printf("root->\n");
3210    while (pNode != NULL) {
3211        printf("[Value=%p, NextNode=%p]->\n", pNode->pValue, pNode->pNextNode);
3212        pNode = pNode->pNextNode;
3213        nodes++;
3214    }
3215    printf("==================No. of existing nodes: %d=======================================\n\n", nodes);
3216}
3217
3218void LinkedList_Destroy(LinkedList *LinkedList) {
3219    pthread_mutex_destroy(&LinkedList->lock);
3220}
3221