1 /*--------------------------------------------------------------------------
2 Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright
9       notice, this list of conditions and the following disclaimer in the
10       documentation and/or other materials provided with the distribution.
11     * Neither the name of Code Aurora nor
12       the names of its contributors may be used to endorse or promote
13       products derived from this software without specific prior written
14       permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 --------------------------------------------------------------------------*/
28 #include "omx_video_encoder.h"
29 #include <string.h>
30 #include "video_encoder_device.h"
31 #include <stdio.h>
32 #ifdef _ANDROID_ICS_
33 #include <media/hardware/HardwareAPI.h>
34 #endif
35 #ifdef _ANDROID_
36 #include <cutils/properties.h>
37 #endif
38 #ifndef _ANDROID_
39 #include <glib.h>
40 #define strlcpy g_strlcpy
41 #endif
42 /*----------------------------------------------------------------------------
43 * Preprocessor Definitions and Constants
44 * -------------------------------------------------------------------------*/
45 
46 #define OMX_SPEC_VERSION 0x00000101
47 #define OMX_INIT_STRUCT(_s_, _name_)            \
48    memset((_s_), 0x0, sizeof(_name_));          \
49    (_s_)->nSize = sizeof(_name_);               \
50    (_s_)->nVersion.nVersion = OMX_SPEC_VERSION
51 
52 extern int m_pipe;
53 
54 // factory function executed by the core to create instances
get_omx_component_factory_fn(void)55 void *get_omx_component_factory_fn(void)
56 {
57   return(new omx_venc);
58 }
59 
60 //constructor
61 
omx_venc()62 omx_venc::omx_venc()
63 {
64 #ifdef _ANDROID_ICS_
65   meta_mode_enable = false;
66   memset(meta_buffer_hdr,0,sizeof(meta_buffer_hdr));
67   memset(meta_buffers,0,sizeof(meta_buffers));
68   memset(opaque_buffer_hdr,0,sizeof(opaque_buffer_hdr));
69   mUseProxyColorFormat = false;
70 #endif
71 }
72 
~omx_venc()73 omx_venc::~omx_venc()
74 {
75   //nothing to do
76 }
77 
78 /* ======================================================================
79 FUNCTION
80   omx_venc::ComponentInit
81 
82 DESCRIPTION
83   Initialize the component.
84 
85 PARAMETERS
86   ctxt -- Context information related to the self.
87   id   -- Event identifier. This could be any of the following:
88           1. Command completion event
89           2. Buffer done callback event
90           3. Frame done callback event
91 
92 RETURN VALUE
93   None.
94 
95 ========================================================================== */
component_init(OMX_STRING role)96 OMX_ERRORTYPE omx_venc::component_init(OMX_STRING role)
97 {
98 
99   OMX_ERRORTYPE eRet = OMX_ErrorNone;
100 
101   int fds[2];
102   int r;
103 
104   OMX_VIDEO_CODINGTYPE codec_type;
105 
106   DEBUG_PRINT_HIGH("\n omx_venc(): Inside component_init()");
107   // Copy the role information which provides the decoder m_nkind
108   strlcpy((char *)m_nkind,role,OMX_MAX_STRINGNAME_SIZE);
109 
110   if(!strncmp((char *)m_nkind,"OMX.qcom.video.encoder.mpeg4",\
111               OMX_MAX_STRINGNAME_SIZE))
112   {
113     strlcpy((char *)m_cRole, "video_encoder.mpeg4",\
114             OMX_MAX_STRINGNAME_SIZE);
115     codec_type = OMX_VIDEO_CodingMPEG4;
116   }
117   else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.h263",\
118                    OMX_MAX_STRINGNAME_SIZE))
119   {
120     strlcpy((char *)m_cRole, "video_encoder.h263",OMX_MAX_STRINGNAME_SIZE);
121     codec_type = OMX_VIDEO_CodingH263;
122   }
123   else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.avc",\
124                    OMX_MAX_STRINGNAME_SIZE))
125   {
126     strlcpy((char *)m_cRole, "video_encoder.avc",OMX_MAX_STRINGNAME_SIZE);
127     codec_type = OMX_VIDEO_CodingAVC;
128   }
129   else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.avc.secure",\
130                    OMX_MAX_STRINGNAME_SIZE))
131   {
132     strlcpy((char *)m_cRole, "video_encoder.avc",OMX_MAX_STRINGNAME_SIZE);
133     codec_type = OMX_VIDEO_CodingAVC;
134     secure_session = true;
135   }
136   else
137   {
138     DEBUG_PRINT_ERROR("\nERROR: Unknown Component\n");
139     eRet = OMX_ErrorInvalidComponentName;
140   }
141 
142 
143   if(eRet != OMX_ErrorNone)
144   {
145     return eRet;
146   }
147 
148   handle = new venc_dev(this);
149 
150   if(handle == NULL)
151   {
152     DEBUG_PRINT_ERROR("\nERROR: handle is NULL");
153     return OMX_ErrorInsufficientResources;
154   }
155 
156   if(handle->venc_open(codec_type) != true)
157   {
158     DEBUG_PRINT_ERROR("\nERROR: venc_open failed");
159     return OMX_ErrorInsufficientResources;
160   }
161 
162   //Intialise the OMX layer variables
163   memset(&m_pCallbacks,0,sizeof(OMX_CALLBACKTYPE));
164 
165   OMX_INIT_STRUCT(&m_sPortParam, OMX_PORT_PARAM_TYPE);
166   m_sPortParam.nPorts = 0x2;
167   m_sPortParam.nStartPortNumber = (OMX_U32) PORT_INDEX_IN;
168 
169   OMX_INIT_STRUCT(&m_sPortParam_audio, OMX_PORT_PARAM_TYPE);
170   m_sPortParam_audio.nPorts = 0;
171   m_sPortParam_audio.nStartPortNumber = 0;
172 
173   OMX_INIT_STRUCT(&m_sPortParam_img, OMX_PORT_PARAM_TYPE);
174   m_sPortParam_img.nPorts = 0;
175   m_sPortParam_img.nStartPortNumber = 0;
176 
177   OMX_INIT_STRUCT(&m_sParamBitrate, OMX_VIDEO_PARAM_BITRATETYPE);
178   m_sParamBitrate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
179   m_sParamBitrate.eControlRate = OMX_Video_ControlRateVariableSkipFrames;
180   m_sParamBitrate.nTargetBitrate = 64000;
181 
182   OMX_INIT_STRUCT(&m_sConfigBitrate, OMX_VIDEO_CONFIG_BITRATETYPE);
183   m_sConfigBitrate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
184   m_sConfigBitrate.nEncodeBitrate = 64000;
185 
186   OMX_INIT_STRUCT(&m_sConfigFramerate, OMX_CONFIG_FRAMERATETYPE);
187   m_sConfigFramerate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
188   m_sConfigFramerate.xEncodeFramerate = 30 << 16;
189 
190   OMX_INIT_STRUCT(&m_sConfigIntraRefreshVOP, OMX_CONFIG_INTRAREFRESHVOPTYPE);
191   m_sConfigIntraRefreshVOP.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
192   m_sConfigIntraRefreshVOP.IntraRefreshVOP = OMX_FALSE;
193 
194   OMX_INIT_STRUCT(&m_sConfigFrameRotation, OMX_CONFIG_ROTATIONTYPE);
195   m_sConfigFrameRotation.nPortIndex = (OMX_U32) PORT_INDEX_IN;
196   m_sConfigFrameRotation.nRotation = 0;
197 
198   OMX_INIT_STRUCT(&m_sSessionQuantization, OMX_VIDEO_PARAM_QUANTIZATIONTYPE);
199   m_sSessionQuantization.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
200   m_sSessionQuantization.nQpI = 9;
201   m_sSessionQuantization.nQpP = 6;
202   m_sSessionQuantization.nQpB = 2;
203 
204   OMX_INIT_STRUCT(&m_sAVCSliceFMO, OMX_VIDEO_PARAM_AVCSLICEFMO);
205   m_sAVCSliceFMO.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
206   m_sAVCSliceFMO.eSliceMode = OMX_VIDEO_SLICEMODE_AVCDefault;
207   m_sAVCSliceFMO.nNumSliceGroups = 0;
208   m_sAVCSliceFMO.nSliceGroupMapType = 0;
209   OMX_INIT_STRUCT(&m_sParamProfileLevel, OMX_VIDEO_PARAM_PROFILELEVELTYPE);
210   m_sParamProfileLevel.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
211 
212   OMX_INIT_STRUCT(&m_sIntraperiod, QOMX_VIDEO_INTRAPERIODTYPE);
213   m_sIntraperiod.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
214   m_sIntraperiod.nPFrames = (m_sConfigFramerate.xEncodeFramerate * 2) - 1;
215 
216   OMX_INIT_STRUCT(&m_sErrorCorrection, OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE);
217   m_sErrorCorrection.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
218   m_sErrorCorrection.bEnableDataPartitioning = OMX_FALSE;
219   m_sErrorCorrection.bEnableHEC = OMX_FALSE;
220   m_sErrorCorrection.bEnableResync = OMX_FALSE;
221   m_sErrorCorrection.bEnableRVLC = OMX_FALSE;
222   m_sErrorCorrection.nResynchMarkerSpacing = 0;
223 
224   OMX_INIT_STRUCT(&m_sIntraRefresh, OMX_VIDEO_PARAM_INTRAREFRESHTYPE);
225   m_sIntraRefresh.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
226   m_sIntraRefresh.eRefreshMode = OMX_VIDEO_IntraRefreshMax;
227 
228   if(codec_type == OMX_VIDEO_CodingMPEG4)
229   {
230     m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_MPEG4ProfileSimple;
231     m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_MPEG4Level0;
232   }
233   else if(codec_type == OMX_VIDEO_CodingH263)
234   {
235     m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_H263ProfileBaseline;
236     m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_H263Level10;
237   }
238   else if(codec_type == OMX_VIDEO_CodingAVC)
239   {
240     m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_AVCProfileBaseline;
241     m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_AVCLevel1;
242   }
243 
244   // Initialize the video parameters for input port
245   OMX_INIT_STRUCT(&m_sInPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
246   m_sInPortDef.nPortIndex= (OMX_U32) PORT_INDEX_IN;
247   m_sInPortDef.bEnabled = OMX_TRUE;
248   m_sInPortDef.bPopulated = OMX_FALSE;
249   m_sInPortDef.eDomain = OMX_PortDomainVideo;
250   m_sInPortDef.eDir = OMX_DirInput;
251   m_sInPortDef.format.video.cMIMEType = "YUV420";
252   m_sInPortDef.format.video.nFrameWidth = OMX_CORE_QCIF_WIDTH;
253   m_sInPortDef.format.video.nFrameHeight = OMX_CORE_QCIF_HEIGHT;
254   m_sInPortDef.format.video.nStride = OMX_CORE_QCIF_WIDTH;
255   m_sInPortDef.format.video.nSliceHeight = OMX_CORE_QCIF_HEIGHT;
256   m_sInPortDef.format.video.nBitrate = 64000;
257   m_sInPortDef.format.video.xFramerate = 15 << 16;
258   m_sInPortDef.format.video.eColorFormat =  OMX_COLOR_FormatYUV420SemiPlanar;
259   m_sInPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingUnused;
260 
261   if(dev_get_buf_req(&m_sInPortDef.nBufferCountMin,
262                      &m_sInPortDef.nBufferCountActual,
263                      &m_sInPortDef.nBufferSize,
264                      m_sInPortDef.nPortIndex) != true)
265   {
266     eRet = OMX_ErrorUndefined;
267 
268   }
269 
270   // Initialize the video parameters for output port
271   OMX_INIT_STRUCT(&m_sOutPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
272   m_sOutPortDef.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
273   m_sOutPortDef.bEnabled = OMX_TRUE;
274   m_sOutPortDef.bPopulated = OMX_FALSE;
275   m_sOutPortDef.eDomain = OMX_PortDomainVideo;
276   m_sOutPortDef.eDir = OMX_DirOutput;
277   m_sOutPortDef.format.video.nFrameWidth = OMX_CORE_QCIF_WIDTH;
278   m_sOutPortDef.format.video.nFrameHeight = OMX_CORE_QCIF_HEIGHT;
279   m_sOutPortDef.format.video.nBitrate = 64000;
280   m_sOutPortDef.format.video.xFramerate = 15 << 16;
281   m_sOutPortDef.format.video.eColorFormat =  OMX_COLOR_FormatUnused;
282   if(codec_type == OMX_VIDEO_CodingMPEG4)
283   {
284     m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingMPEG4;
285   }
286   else if(codec_type == OMX_VIDEO_CodingH263)
287   {
288     m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingH263;
289   }
290   else
291   {
292     m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingAVC;
293   }
294   if(dev_get_buf_req(&m_sOutPortDef.nBufferCountMin,
295                      &m_sOutPortDef.nBufferCountActual,
296                      &m_sOutPortDef.nBufferSize,
297                      m_sOutPortDef.nPortIndex) != true)
298   {
299     eRet = OMX_ErrorUndefined;
300   }
301 
302   // Initialize the video color format for input port
303   OMX_INIT_STRUCT(&m_sInPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
304   m_sInPortFormat.nPortIndex = (OMX_U32) PORT_INDEX_IN;
305   m_sInPortFormat.nIndex = 0;
306   m_sInPortFormat.eColorFormat =  OMX_COLOR_FormatYUV420SemiPlanar;
307   m_sInPortFormat.eCompressionFormat = OMX_VIDEO_CodingUnused;
308 
309 
310   // Initialize the compression format for output port
311   OMX_INIT_STRUCT(&m_sOutPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
312   m_sOutPortFormat.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
313   m_sOutPortFormat.nIndex = 0;
314   m_sOutPortFormat.eColorFormat = OMX_COLOR_FormatUnused;
315   if(codec_type == OMX_VIDEO_CodingMPEG4)
316   {
317     m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingMPEG4;
318   }
319   else if(codec_type == OMX_VIDEO_CodingH263)
320   {
321     m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingH263;
322   }
323   else
324   {
325     m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingAVC;
326   }
327 
328   // mandatory Indices for kronos test suite
329   OMX_INIT_STRUCT(&m_sPriorityMgmt, OMX_PRIORITYMGMTTYPE);
330 
331   OMX_INIT_STRUCT(&m_sInBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
332   m_sInBufSupplier.nPortIndex = (OMX_U32) PORT_INDEX_IN;
333 
334   OMX_INIT_STRUCT(&m_sOutBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
335   m_sOutBufSupplier.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
336 
337 
338   // mp4 specific init
339   OMX_INIT_STRUCT(&m_sParamMPEG4, OMX_VIDEO_PARAM_MPEG4TYPE);
340   m_sParamMPEG4.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
341   m_sParamMPEG4.eProfile = OMX_VIDEO_MPEG4ProfileSimple;
342   m_sParamMPEG4.eLevel = OMX_VIDEO_MPEG4Level0;
343   m_sParamMPEG4.nSliceHeaderSpacing = 0;
344   m_sParamMPEG4.bSVH = OMX_FALSE;
345   m_sParamMPEG4.bGov = OMX_FALSE;
346   m_sParamMPEG4.nPFrames = (m_sOutPortFormat.xFramerate * 2 - 1);  // 2 second intra period for default outport fps
347   m_sParamMPEG4.bACPred = OMX_TRUE;
348   m_sParamMPEG4.nTimeIncRes = 30; // delta = 2 @ 15 fps
349   m_sParamMPEG4.nAllowedPictureTypes = 2; // pframe and iframe
350   m_sParamMPEG4.nHeaderExtension = 1; // number of video packet headers per vop
351   m_sParamMPEG4.bReversibleVLC = OMX_FALSE;
352 
353   // h263 specific init
354   OMX_INIT_STRUCT(&m_sParamH263, OMX_VIDEO_PARAM_H263TYPE);
355   m_sParamH263.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
356   m_sParamH263.nPFrames = (m_sOutPortFormat.xFramerate * 2 - 1); // 2 second intra period for default outport fps
357   m_sParamH263.nBFrames = 0;
358   m_sParamH263.eProfile = OMX_VIDEO_H263ProfileBaseline;
359   m_sParamH263.eLevel = OMX_VIDEO_H263Level10;
360   m_sParamH263.bPLUSPTYPEAllowed = OMX_FALSE;
361   m_sParamH263.nAllowedPictureTypes = 2;
362   m_sParamH263.bForceRoundingTypeToZero = OMX_TRUE;
363   m_sParamH263.nPictureHeaderRepetition = 0;
364   m_sParamH263.nGOBHeaderInterval = 1;
365 
366   // h264 specific init
367   OMX_INIT_STRUCT(&m_sParamAVC, OMX_VIDEO_PARAM_AVCTYPE);
368   m_sParamAVC.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
369   m_sParamAVC.nSliceHeaderSpacing = 0;
370   m_sParamAVC.nPFrames = (m_sOutPortFormat.xFramerate * 2 - 1); // 2 second intra period for default outport fps
371   m_sParamAVC.nBFrames = 0;
372   m_sParamAVC.bUseHadamard = OMX_FALSE;
373   m_sParamAVC.nRefFrames = 1;
374   m_sParamAVC.nRefIdx10ActiveMinus1 = 1;
375   m_sParamAVC.nRefIdx11ActiveMinus1 = 0;
376   m_sParamAVC.bEnableUEP = OMX_FALSE;
377   m_sParamAVC.bEnableFMO = OMX_FALSE;
378   m_sParamAVC.bEnableASO = OMX_FALSE;
379   m_sParamAVC.bEnableRS = OMX_FALSE;
380   m_sParamAVC.eProfile = OMX_VIDEO_AVCProfileBaseline;
381   m_sParamAVC.eLevel = OMX_VIDEO_AVCLevel1;
382   m_sParamAVC.nAllowedPictureTypes = 2;
383   m_sParamAVC.bFrameMBsOnly = OMX_FALSE;
384   m_sParamAVC.bMBAFF = OMX_FALSE;
385   m_sParamAVC.bEntropyCodingCABAC = OMX_FALSE;
386   m_sParamAVC.bWeightedPPrediction = OMX_FALSE;
387   m_sParamAVC.nWeightedBipredicitonMode = 0;
388   m_sParamAVC.bconstIpred = OMX_FALSE;
389   m_sParamAVC.bDirect8x8Inference = OMX_FALSE;
390   m_sParamAVC.bDirectSpatialTemporal = OMX_FALSE;
391   m_sParamAVC.nCabacInitIdc = 0;
392   m_sParamAVC.eLoopFilterMode = OMX_VIDEO_AVCLoopFilterEnable;
393 
394   m_state                   = OMX_StateLoaded;
395   m_sExtraData = 0;
396   m_sDebugSliceinfo = 0;
397 
398   // For H264 enable some parameters in VUI by default
399   if (codec_type == OMX_VIDEO_CodingAVC)
400   {
401     QOMX_VUI_BITSTREAM_RESTRICT parm;
402     OMX_INIT_STRUCT(&parm, QOMX_VUI_BITSTREAM_RESTRICT);
403     parm.bEnable = OMX_TRUE;
404     if (set_parameter(NULL, (OMX_INDEXTYPE)OMX_QcomIndexParamEnableVUIStreamRestrictFlag,
405                      (OMX_PTR)&parm)) {
406       // Don't treat this as a fatal error
407       DEBUG_PRINT_ERROR("Unable to set EnableVUIStreamRestrictFlag as default");
408     }
409   }
410 
411 #ifdef _ANDROID_
412   char value[PROPERTY_VALUE_MAX] = {0};
413   property_get("vidc.venc.debug.sliceinfo", value, "0");
414   m_sDebugSliceinfo = (OMX_U32)atoi(value);
415   DEBUG_PRINT_HIGH("vidc.venc.debug.sliceinfo value is %d", m_sDebugSliceinfo);
416 #endif
417 
418   if(eRet == OMX_ErrorNone)
419   {
420     if(pipe(fds))
421     {
422       DEBUG_PRINT_ERROR("ERROR: pipe creation failed\n");
423       eRet = OMX_ErrorInsufficientResources;
424     }
425     else
426     {
427       if(fds[0] == 0 || fds[1] == 0)
428       {
429         if(pipe(fds))
430         {
431           DEBUG_PRINT_ERROR("ERROR: pipe creation failed\n");
432           eRet = OMX_ErrorInsufficientResources;
433         }
434       }
435       if(eRet == OMX_ErrorNone)
436       {
437         m_pipe_in = fds[0];
438         m_pipe_out = fds[1];
439       }
440     }
441     r = pthread_create(&msg_thread_id,0,message_thread,this);
442 
443     if(r < 0)
444     {
445       eRet = OMX_ErrorInsufficientResources;
446     }
447     else
448     {
449       r = pthread_create(&async_thread_id,0,async_venc_message_thread,this);
450       if(r < 0)
451       {
452         eRet = OMX_ErrorInsufficientResources;
453       }
454     }
455   }
456 
457   DEBUG_PRINT_HIGH("\n Component_init return value = 0x%x", eRet);
458   return eRet;
459 }
460 
461 
462 /* ======================================================================
463 FUNCTION
464   omx_venc::Setparameter
465 
466 DESCRIPTION
467   OMX Set Parameter method implementation.
468 
469 PARAMETERS
470   <TBD>.
471 
472 RETURN VALUE
473   OMX Error None if successful.
474 
475 ========================================================================== */
set_parameter(OMX_IN OMX_HANDLETYPE hComp,OMX_IN OMX_INDEXTYPE paramIndex,OMX_IN OMX_PTR paramData)476 OMX_ERRORTYPE  omx_venc::set_parameter(OMX_IN OMX_HANDLETYPE     hComp,
477                                        OMX_IN OMX_INDEXTYPE paramIndex,
478                                        OMX_IN OMX_PTR        paramData)
479 {
480   OMX_ERRORTYPE eRet = OMX_ErrorNone;
481 
482 
483   if(m_state == OMX_StateInvalid)
484   {
485     DEBUG_PRINT_ERROR("ERROR: Set Param in Invalid State\n");
486     return OMX_ErrorInvalidState;
487   }
488   if(paramData == NULL)
489   {
490     DEBUG_PRINT_ERROR("ERROR: Get Param in Invalid paramData \n");
491     return OMX_ErrorBadParameter;
492   }
493 
494   /*set_parameter can be called in loaded state
495   or disabled port */
496   if(m_state == OMX_StateLoaded
497      || m_sInPortDef.bEnabled == OMX_FALSE
498      || m_sOutPortDef.bEnabled == OMX_FALSE)
499   {
500     DEBUG_PRINT_LOW("Set Parameter called in valid state");
501   }
502   else
503   {
504     DEBUG_PRINT_ERROR("ERROR: Set Parameter called in Invalid State\n");
505     return OMX_ErrorIncorrectStateOperation;
506   }
507 
508   switch(paramIndex)
509   {
510   case OMX_IndexParamPortDefinition:
511     {
512       OMX_PARAM_PORTDEFINITIONTYPE *portDefn;
513       portDefn = (OMX_PARAM_PORTDEFINITIONTYPE *) paramData;
514       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPortDefinition H= %d, W = %d\n",
515            (int)portDefn->format.video.nFrameHeight,
516            (int)portDefn->format.video.nFrameWidth);
517 
518       if(PORT_INDEX_IN == portDefn->nPortIndex)
519       {
520         DEBUG_PRINT_LOW("\n i/p actual cnt requested = %d\n", portDefn->nBufferCountActual);
521         DEBUG_PRINT_LOW("\n i/p min cnt requested = %d\n", portDefn->nBufferCountMin);
522         DEBUG_PRINT_LOW("\n i/p buffersize requested = %d\n", portDefn->nBufferSize);
523         if(handle->venc_set_param(paramData,OMX_IndexParamPortDefinition) != true)
524         {
525           DEBUG_PRINT_ERROR("\nERROR: venc_set_param input failed");
526           return OMX_ErrorUnsupportedSetting;
527         }
528 
529         DEBUG_PRINT_LOW("\n i/p previous actual cnt = %d\n", m_sInPortDef.nBufferCountActual);
530         DEBUG_PRINT_LOW("\n i/p previous min cnt = %d\n", m_sInPortDef.nBufferCountMin);
531         memcpy(&m_sInPortDef, portDefn,sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
532 
533 #ifdef _ANDROID_ICS_
534         if (portDefn->format.video.eColorFormat == (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FormatAndroidOpaque) {
535             m_sInPortDef.format.video.eColorFormat =
536                 OMX_COLOR_FormatYUV420SemiPlanar;
537             if(secure_session) {
538               secure_color_format = (int) QOMX_COLOR_FormatAndroidOpaque;
539               mUseProxyColorFormat = false;
540               m_input_msg_id = OMX_COMPONENT_GENERATE_ETB;
541             } else if(!mUseProxyColorFormat){
542               if (!c2d_conv.init()) {
543                 DEBUG_PRINT_ERROR("\n C2D init failed");
544                 return OMX_ErrorUnsupportedSetting;
545               }
546               DEBUG_PRINT_ERROR("\nC2D init is successful");
547               mUseProxyColorFormat = true;
548               m_input_msg_id = OMX_COMPONENT_GENERATE_ETB_OPQ;
549             }
550         } else
551           mUseProxyColorFormat = false;
552 #endif
553         /*Query Input Buffer Requirements*/
554         dev_get_buf_req   (&m_sInPortDef.nBufferCountMin,
555                            &m_sInPortDef.nBufferCountActual,
556                            &m_sInPortDef.nBufferSize,
557                            m_sInPortDef.nPortIndex);
558 
559         /*Query ouput Buffer Requirements*/
560         dev_get_buf_req   (&m_sOutPortDef.nBufferCountMin,
561                            &m_sOutPortDef.nBufferCountActual,
562                            &m_sOutPortDef.nBufferSize,
563                            m_sOutPortDef.nPortIndex);
564         m_sInPortDef.nBufferCountActual = portDefn->nBufferCountActual;
565       }
566       else if(PORT_INDEX_OUT == portDefn->nPortIndex)
567       {
568         DEBUG_PRINT_LOW("\n o/p actual cnt requested = %d\n", portDefn->nBufferCountActual);
569         DEBUG_PRINT_LOW("\n o/p min cnt requested = %d\n", portDefn->nBufferCountMin);
570         DEBUG_PRINT_LOW("\n o/p buffersize requested = %d\n", portDefn->nBufferSize);
571         if(handle->venc_set_param(paramData,OMX_IndexParamPortDefinition) != true)
572         {
573           DEBUG_PRINT_ERROR("\nERROR: venc_set_param output failed");
574           return OMX_ErrorUnsupportedSetting;
575         }
576         memcpy(&m_sOutPortDef,portDefn,sizeof(struct OMX_PARAM_PORTDEFINITIONTYPE));
577         update_profile_level(); //framerate , bitrate
578 
579         DEBUG_PRINT_LOW("\n o/p previous actual cnt = %d\n", m_sOutPortDef.nBufferCountActual);
580         DEBUG_PRINT_LOW("\n o/p previous min cnt = %d\n", m_sOutPortDef.nBufferCountMin);
581         m_sOutPortDef.nBufferCountActual = portDefn->nBufferCountActual;
582       }
583       else
584       {
585         DEBUG_PRINT_ERROR("ERROR: Set_parameter: Bad Port idx %d",
586                     (int)portDefn->nPortIndex);
587         eRet = OMX_ErrorBadPortIndex;
588       }
589       m_sConfigFramerate.xEncodeFramerate = portDefn->format.video.xFramerate;
590       m_sConfigBitrate.nEncodeBitrate = portDefn->format.video.nBitrate;
591       m_sParamBitrate.nTargetBitrate = portDefn->format.video.nBitrate;
592     }
593     break;
594 
595   case OMX_IndexParamVideoPortFormat:
596     {
597       OMX_VIDEO_PARAM_PORTFORMATTYPE *portFmt =
598       (OMX_VIDEO_PARAM_PORTFORMATTYPE *)paramData;
599       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoPortFormat %d\n",
600                   portFmt->eColorFormat);
601       //set the driver with the corresponding values
602       if(PORT_INDEX_IN == portFmt->nPortIndex)
603       {
604         if(handle->venc_set_param(paramData,OMX_IndexParamVideoPortFormat) != true)
605         {
606           return OMX_ErrorUnsupportedSetting;
607         }
608 
609         DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoPortFormat %d\n",
610             portFmt->eColorFormat);
611         update_profile_level(); //framerate
612 
613 #ifdef _ANDROID_ICS_
614         if (portFmt->eColorFormat ==
615             (OMX_COLOR_FORMATTYPE)QOMX_COLOR_FormatAndroidOpaque) {
616             m_sInPortFormat.eColorFormat = OMX_COLOR_FormatYUV420SemiPlanar;
617             if(secure_session) {
618               secure_color_format = (int) QOMX_COLOR_FormatAndroidOpaque;
619               mUseProxyColorFormat = false;
620               m_input_msg_id = OMX_COMPONENT_GENERATE_ETB;
621             } else if(!mUseProxyColorFormat){
622               if (!c2d_conv.init()) {
623                 DEBUG_PRINT_ERROR("\n C2D init failed");
624                 return OMX_ErrorUnsupportedSetting;
625               }
626               DEBUG_PRINT_ERROR("\nC2D init is successful");
627               mUseProxyColorFormat = true;
628               m_input_msg_id = OMX_COMPONENT_GENERATE_ETB_OPQ;
629             }
630         }
631         else
632 #endif
633         {
634             m_sInPortFormat.eColorFormat = portFmt->eColorFormat;
635             m_input_msg_id = OMX_COMPONENT_GENERATE_ETB;
636             mUseProxyColorFormat = false;
637         }
638         m_sInPortFormat.xFramerate = portFmt->xFramerate;
639       }
640       //TODO if no use case for O/P port,delet m_sOutPortFormat
641     }
642     break;
643   case OMX_IndexParamVideoInit:
644     {  //TODO, do we need this index set param
645       OMX_PORT_PARAM_TYPE* pParam = (OMX_PORT_PARAM_TYPE*)(paramData);
646       DEBUG_PRINT_LOW("\n Set OMX_IndexParamVideoInit called");
647       break;
648     }
649 
650   case OMX_IndexParamVideoBitrate:
651     {
652       OMX_VIDEO_PARAM_BITRATETYPE* pParam = (OMX_VIDEO_PARAM_BITRATETYPE*)paramData;
653       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoBitrate");
654       if(handle->venc_set_param(paramData,OMX_IndexParamVideoBitrate) != true)
655       {
656         return OMX_ErrorUnsupportedSetting;
657       }
658       m_sParamBitrate.nTargetBitrate = pParam->nTargetBitrate;
659       m_sParamBitrate.eControlRate = pParam->eControlRate;
660       update_profile_level(); //bitrate
661       m_sConfigBitrate.nEncodeBitrate = pParam->nTargetBitrate;
662 	  m_sInPortDef.format.video.nBitrate = pParam->nTargetBitrate;
663       m_sOutPortDef.format.video.nBitrate = pParam->nTargetBitrate;
664       DEBUG_PRINT_LOW("\nbitrate = %u", m_sOutPortDef.format.video.nBitrate);
665       break;
666     }
667   case OMX_IndexParamVideoMpeg4:
668     {
669       OMX_VIDEO_PARAM_MPEG4TYPE* pParam = (OMX_VIDEO_PARAM_MPEG4TYPE*)paramData;
670       OMX_VIDEO_PARAM_MPEG4TYPE mp4_param;
671       memcpy(&mp4_param, pParam, sizeof(struct OMX_VIDEO_PARAM_MPEG4TYPE));
672       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoMpeg4");
673       if(pParam->eProfile == OMX_VIDEO_MPEG4ProfileAdvancedSimple)
674       {
675 #ifdef MAX_RES_1080P
676         if(pParam->nBFrames)
677         {
678           DEBUG_PRINT_HIGH("INFO: Only 1 Bframe is supported");
679           mp4_param.nBFrames = 1;
680         }
681 #else
682         if(pParam->nBFrames)
683         {
684           DEBUG_PRINT_ERROR("Warning: B frames not supported\n");
685           mp4_param.nBFrames = 0;
686         }
687 #endif
688       }
689       else
690       {
691         if(pParam->nBFrames)
692         {
693           DEBUG_PRINT_ERROR("Warning: B frames not supported\n");
694           mp4_param.nBFrames = 0;
695         }
696       }
697       if(handle->venc_set_param(&mp4_param,OMX_IndexParamVideoMpeg4) != true)
698       {
699         return OMX_ErrorUnsupportedSetting;
700       }
701       memcpy(&m_sParamMPEG4,pParam, sizeof(struct OMX_VIDEO_PARAM_MPEG4TYPE));
702       break;
703     }
704   case OMX_IndexParamVideoH263:
705     {
706       OMX_VIDEO_PARAM_H263TYPE* pParam = (OMX_VIDEO_PARAM_H263TYPE*)paramData;
707       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoH263");
708       if(handle->venc_set_param(paramData,OMX_IndexParamVideoH263) != true)
709       {
710         return OMX_ErrorUnsupportedSetting;
711       }
712       memcpy(&m_sParamH263,pParam, sizeof(struct OMX_VIDEO_PARAM_H263TYPE));
713       break;
714     }
715   case OMX_IndexParamVideoAvc:
716     {
717       OMX_VIDEO_PARAM_AVCTYPE* pParam = (OMX_VIDEO_PARAM_AVCTYPE*)paramData;
718       OMX_VIDEO_PARAM_AVCTYPE avc_param;
719       memcpy(&avc_param, pParam, sizeof( struct OMX_VIDEO_PARAM_AVCTYPE));
720       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoAvc");
721 
722       if((pParam->eProfile == OMX_VIDEO_AVCProfileHigh)||
723          (pParam->eProfile == OMX_VIDEO_AVCProfileMain))
724       {
725 #ifdef MAX_RES_1080P
726         if(pParam->nBFrames)
727         {
728           DEBUG_PRINT_HIGH("INFO: Only 1 Bframe is supported");
729           avc_param.nBFrames = 1;
730         }
731         if(pParam->nRefFrames != 2)
732         {
733           DEBUG_PRINT_ERROR("Warning: 2 RefFrames are needed, changing RefFrames from %d to 2", pParam->nRefFrames);
734           avc_param.nRefFrames = 2;
735         }
736 #else
737        if(pParam->nBFrames)
738        {
739          DEBUG_PRINT_ERROR("Warning: B frames not supported\n");
740          avc_param.nBFrames = 0;
741        }
742        if(pParam->nRefFrames != 1)
743        {
744          DEBUG_PRINT_ERROR("Warning: Only 1 RefFrame is supported, changing RefFrame from %d to 1)", pParam->nRefFrames);
745          avc_param.nRefFrames = 1;
746        }
747 #endif
748       }
749       else
750       {
751        if(pParam->nRefFrames != 1)
752        {
753          DEBUG_PRINT_ERROR("Warning: Only 1 RefFrame is supported, changing RefFrame from %d to 1)", pParam->nRefFrames);
754          avc_param.nRefFrames = 1;
755        }
756        if(pParam->nBFrames)
757        {
758          DEBUG_PRINT_ERROR("Warning: B frames not supported\n");
759          avc_param.nBFrames = 0;
760        }
761       }
762       if(handle->venc_set_param(&avc_param,OMX_IndexParamVideoAvc) != true)
763       {
764         return OMX_ErrorUnsupportedSetting;
765       }
766 
767       memcpy(&m_sParamAVC,pParam, sizeof(struct OMX_VIDEO_PARAM_AVCTYPE));
768       break;
769     }
770   case OMX_IndexParamVideoProfileLevelCurrent:
771     {
772       OMX_VIDEO_PARAM_PROFILELEVELTYPE* pParam = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)paramData;
773       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoProfileLevelCurrent");
774       if(handle->venc_set_param(pParam,OMX_IndexParamVideoProfileLevelCurrent) != true)
775       {
776         DEBUG_PRINT_ERROR("set_parameter: OMX_IndexParamVideoProfileLevelCurrent failed for Profile: %d "
777                           "Level :%d", pParam->eProfile, pParam->eLevel);
778         return OMX_ErrorUnsupportedSetting;
779       }
780       m_sParamProfileLevel.eProfile = pParam->eProfile;
781       m_sParamProfileLevel.eLevel = pParam->eLevel;
782 
783       if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.mpeg4",\
784           OMX_MAX_STRINGNAME_SIZE))
785       {
786           m_sParamMPEG4.eProfile = (OMX_VIDEO_MPEG4PROFILETYPE)m_sParamProfileLevel.eProfile;
787           m_sParamMPEG4.eLevel = (OMX_VIDEO_MPEG4LEVELTYPE)m_sParamProfileLevel.eLevel;
788           DEBUG_PRINT_LOW("\n MPEG4 profile = %d, level = %d", m_sParamMPEG4.eProfile,
789               m_sParamMPEG4.eLevel);
790       }
791       else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.h263",\
792           OMX_MAX_STRINGNAME_SIZE))
793       {
794           m_sParamH263.eProfile = (OMX_VIDEO_H263PROFILETYPE)m_sParamProfileLevel.eProfile;
795           m_sParamH263.eLevel = (OMX_VIDEO_H263LEVELTYPE)m_sParamProfileLevel.eLevel;
796           DEBUG_PRINT_LOW("\n H263 profile = %d, level = %d", m_sParamH263.eProfile,
797               m_sParamH263.eLevel);
798       }
799       else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.avc",\
800           OMX_MAX_STRINGNAME_SIZE))
801       {
802           m_sParamAVC.eProfile = (OMX_VIDEO_AVCPROFILETYPE)m_sParamProfileLevel.eProfile;
803           m_sParamAVC.eLevel = (OMX_VIDEO_AVCLEVELTYPE)m_sParamProfileLevel.eLevel;
804           DEBUG_PRINT_LOW("\n AVC profile = %d, level = %d", m_sParamAVC.eProfile,
805               m_sParamAVC.eLevel);
806       }
807       break;
808     }
809   case OMX_IndexParamStandardComponentRole:
810     {
811       OMX_PARAM_COMPONENTROLETYPE *comp_role;
812       comp_role = (OMX_PARAM_COMPONENTROLETYPE *) paramData;
813       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamStandardComponentRole %s\n",
814                   comp_role->cRole);
815 
816       if((m_state == OMX_StateLoaded)&&
817           !BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
818       {
819          DEBUG_PRINT_LOW("Set Parameter called in valid state");
820       }
821       else
822       {
823          DEBUG_PRINT_ERROR("Set Parameter called in Invalid State\n");
824          return OMX_ErrorIncorrectStateOperation;
825       }
826 
827       if(!strncmp((char*)m_nkind, "OMX.qcom.video.encoder.avc",OMX_MAX_STRINGNAME_SIZE))
828       {
829         if(!strncmp((char*)comp_role->cRole,"video_encoder.avc",OMX_MAX_STRINGNAME_SIZE))
830         {
831           strlcpy((char*)m_cRole,"video_encoder.avc",OMX_MAX_STRINGNAME_SIZE);
832         }
833         else
834         {
835           DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown Index %s\n", comp_role->cRole);
836           eRet =OMX_ErrorUnsupportedSetting;
837         }
838       }
839       else if(!strncmp((char*)m_nkind, "OMX.qcom.video.encoder.mpeg4",OMX_MAX_STRINGNAME_SIZE))
840       {
841         if(!strncmp((const char*)comp_role->cRole,"video_encoder.mpeg4",OMX_MAX_STRINGNAME_SIZE))
842         {
843           strlcpy((char*)m_cRole,"video_encoder.mpeg4",OMX_MAX_STRINGNAME_SIZE);
844         }
845         else
846         {
847           DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown Index %s\n", comp_role->cRole);
848           eRet = OMX_ErrorUnsupportedSetting;
849         }
850       }
851       else if(!strncmp((char*)m_nkind, "OMX.qcom.video.encoder.h263",OMX_MAX_STRINGNAME_SIZE))
852       {
853         if(!strncmp((const char*)comp_role->cRole,"video_encoder.h263",OMX_MAX_STRINGNAME_SIZE))
854         {
855           strlcpy((char*)m_cRole,"video_encoder.h263",OMX_MAX_STRINGNAME_SIZE);
856         }
857         else
858         {
859           DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown Index %s\n", comp_role->cRole);
860           eRet =OMX_ErrorUnsupportedSetting;
861         }
862       }
863       else
864       {
865         DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown param %s\n", m_nkind);
866         eRet = OMX_ErrorInvalidComponentName;
867       }
868       break;
869     }
870 
871   case OMX_IndexParamPriorityMgmt:
872     {
873       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPriorityMgmt");
874       if(m_state != OMX_StateLoaded)
875       {
876         DEBUG_PRINT_ERROR("ERROR: Set Parameter called in Invalid State\n");
877         return OMX_ErrorIncorrectStateOperation;
878       }
879       OMX_PRIORITYMGMTTYPE *priorityMgmtype = (OMX_PRIORITYMGMTTYPE*) paramData;
880       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPriorityMgmt %d\n",
881                   priorityMgmtype->nGroupID);
882 
883       DEBUG_PRINT_LOW("set_parameter: priorityMgmtype %d\n",
884                   priorityMgmtype->nGroupPriority);
885 
886       m_sPriorityMgmt.nGroupID = priorityMgmtype->nGroupID;
887       m_sPriorityMgmt.nGroupPriority = priorityMgmtype->nGroupPriority;
888 
889       break;
890     }
891 
892   case OMX_IndexParamCompBufferSupplier:
893     {
894       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamCompBufferSupplier");
895       OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplierType = (OMX_PARAM_BUFFERSUPPLIERTYPE*) paramData;
896       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamCompBufferSupplier %d\n",
897                   bufferSupplierType->eBufferSupplier);
898       if(bufferSupplierType->nPortIndex == 0 || bufferSupplierType->nPortIndex ==1)
899         m_sInBufSupplier.eBufferSupplier = bufferSupplierType->eBufferSupplier;
900 
901       else
902 
903         eRet = OMX_ErrorBadPortIndex;
904 
905       break;
906 
907     }
908   case OMX_IndexParamVideoQuantization:
909     {
910       DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoQuantization\n");
911       OMX_VIDEO_PARAM_QUANTIZATIONTYPE *session_qp = (OMX_VIDEO_PARAM_QUANTIZATIONTYPE*) paramData;
912       if(session_qp->nPortIndex == PORT_INDEX_OUT)
913       {
914         if(handle->venc_set_param(paramData, OMX_IndexParamVideoQuantization) != true)
915         {
916           return OMX_ErrorUnsupportedSetting;
917         }
918         m_sSessionQuantization.nQpI = session_qp->nQpI;
919         m_sSessionQuantization.nQpP = session_qp->nQpP;
920       }
921       else
922       {
923         DEBUG_PRINT_ERROR("\nERROR: Unsupported port Index for Session QP setting\n");
924         eRet = OMX_ErrorBadPortIndex;
925       }
926       break;
927     }
928 
929   case OMX_QcomIndexPortDefn:
930     {
931       OMX_QCOM_PARAM_PORTDEFINITIONTYPE* pParam =
932           (OMX_QCOM_PARAM_PORTDEFINITIONTYPE*)paramData;
933       DEBUG_PRINT_LOW("set_parameter: OMX_QcomIndexPortDefn");
934       if(pParam->nPortIndex == (OMX_U32)PORT_INDEX_IN)
935       {
936         if(pParam->nMemRegion > OMX_QCOM_MemRegionInvalid &&
937           pParam->nMemRegion < OMX_QCOM_MemRegionMax)
938         {
939           m_use_input_pmem = OMX_TRUE;
940         }
941         else
942         {
943           m_use_input_pmem = OMX_FALSE;
944         }
945       }
946       else if (pParam->nPortIndex == (OMX_U32)PORT_INDEX_OUT)
947       {
948         if(pParam->nMemRegion > OMX_QCOM_MemRegionInvalid &&
949           pParam->nMemRegion < OMX_QCOM_MemRegionMax)
950         {
951           m_use_output_pmem = OMX_TRUE;
952         }
953         else
954         {
955           m_use_output_pmem = OMX_FALSE;
956         }
957       }
958       else
959       {
960         DEBUG_PRINT_ERROR("ERROR: SetParameter called on unsupported Port Index for QcomPortDefn");
961         return OMX_ErrorBadPortIndex;
962       }
963       break;
964     }
965 
966   case OMX_IndexParamVideoErrorCorrection:
967     {
968 	    DEBUG_PRINT_LOW("OMX_IndexParamVideoErrorCorrection\n");
969       OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE* pParam =
970           (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE*)paramData;
971       if(!handle->venc_set_param(paramData, OMX_IndexParamVideoErrorCorrection))
972       {
973         DEBUG_PRINT_ERROR("\nERROR: Request for setting Error Resilience failed");
974         return OMX_ErrorUnsupportedSetting;
975       }
976       memcpy(&m_sErrorCorrection,pParam, sizeof(m_sErrorCorrection));
977       break;
978     }
979   case OMX_IndexParamVideoIntraRefresh:
980     {
981       DEBUG_PRINT_LOW("set_param:OMX_IndexParamVideoIntraRefresh\n");
982       OMX_VIDEO_PARAM_INTRAREFRESHTYPE* pParam =
983           (OMX_VIDEO_PARAM_INTRAREFRESHTYPE*)paramData;
984       if(!handle->venc_set_param(paramData,OMX_IndexParamVideoIntraRefresh))
985       {
986         DEBUG_PRINT_ERROR("\nERROR: Request for setting intra refresh failed");
987         return OMX_ErrorUnsupportedSetting;
988       }
989       memcpy(&m_sIntraRefresh, pParam, sizeof(m_sIntraRefresh));
990       break;
991     }
992 #ifdef _ANDROID_ICS_
993   case OMX_QcomIndexParamVideoMetaBufferMode:
994     {
995       StoreMetaDataInBuffersParams *pParam =
996         (StoreMetaDataInBuffersParams*)paramData;
997       if(pParam->nPortIndex == PORT_INDEX_IN)
998       {
999         if(pParam->bStoreMetaData != meta_mode_enable)
1000         {
1001           if(!handle->venc_set_meta_mode(pParam->bStoreMetaData))
1002           {
1003             DEBUG_PRINT_ERROR("\nERROR: set Metabuffer mode %d fail",
1004                          pParam->bStoreMetaData);
1005             return OMX_ErrorUnsupportedSetting;
1006           }
1007           meta_mode_enable = pParam->bStoreMetaData;
1008           if(meta_mode_enable) {
1009             m_sInPortDef.nBufferCountActual = 4;
1010             if(handle->venc_set_param(&m_sInPortDef,OMX_IndexParamPortDefinition) != true)
1011             {
1012               DEBUG_PRINT_ERROR("\nERROR: venc_set_param input failed");
1013               return OMX_ErrorUnsupportedSetting;
1014             }
1015           } else {
1016             /*TODO: reset encoder driver Meta mode*/
1017             dev_get_buf_req   (&m_sOutPortDef.nBufferCountMin,
1018                                &m_sOutPortDef.nBufferCountActual,
1019                                &m_sOutPortDef.nBufferSize,
1020                                m_sOutPortDef.nPortIndex);
1021           }
1022         }
1023       } else {
1024           DEBUG_PRINT_ERROR("set_parameter: metamode is "
1025              "valid for input port only");
1026           eRet = OMX_ErrorUnsupportedSetting;
1027       }
1028       break;
1029     }
1030 #endif
1031 #ifndef MAX_RES_720P
1032   case OMX_QcomIndexParamIndexExtraDataType:
1033     {
1034       DEBUG_PRINT_LOW("set_parameter: OMX_QcomIndexParamIndexExtraDataType");
1035       QOMX_INDEXEXTRADATATYPE *pParam = (QOMX_INDEXEXTRADATATYPE *)paramData;
1036       if (pParam->nIndex == (OMX_INDEXTYPE)OMX_ExtraDataVideoEncoderSliceInfo)
1037       {
1038         if (pParam->nPortIndex == PORT_INDEX_OUT)
1039         {
1040           if (pParam->bEnabled == OMX_TRUE)
1041             m_sExtraData |= VEN_EXTRADATA_SLICEINFO;
1042           else
1043             m_sExtraData &= ~VEN_EXTRADATA_SLICEINFO;
1044           DEBUG_PRINT_HIGH("set_param: m_sExtraData=%x", m_sExtraData);
1045           if(handle->venc_set_param(&m_sExtraData,
1046               (OMX_INDEXTYPE)OMX_ExtraDataVideoEncoderSliceInfo) != true)
1047           {
1048             DEBUG_PRINT_ERROR("ERROR: Setting "
1049                "OMX_QcomIndexParamIndexExtraDataType failed");
1050             return OMX_ErrorUnsupportedSetting;
1051           }
1052           else
1053           {
1054             m_sOutPortDef.nPortIndex = PORT_INDEX_OUT;
1055             dev_get_buf_req(&m_sOutPortDef.nBufferCountMin,
1056                             &m_sOutPortDef.nBufferCountActual,
1057                             &m_sOutPortDef.nBufferSize,
1058                              m_sOutPortDef.nPortIndex);
1059             DEBUG_PRINT_HIGH("updated out_buf_req: buffer cnt=%d, "
1060                 "count min=%d, buffer size=%d",
1061                 m_sOutPortDef.nBufferCountActual,
1062                 m_sOutPortDef.nBufferCountMin,
1063                 m_sOutPortDef.nBufferSize);
1064           }
1065         }
1066         else
1067         {
1068           DEBUG_PRINT_ERROR("set_parameter: slice information is "
1069               "valid for output port only");
1070           eRet = OMX_ErrorUnsupportedIndex;
1071         }
1072       }
1073       else
1074       {
1075         DEBUG_PRINT_ERROR("set_parameter: unsupported index (%x), "
1076             "only slice information extradata is supported", pParam->nIndex);
1077         eRet = OMX_ErrorUnsupportedIndex;
1078       }
1079       break;
1080     }
1081 #endif
1082   case OMX_QcomIndexParamVideoMaxAllowedBitrateCheck:
1083     {
1084       QOMX_EXTNINDEX_PARAMTYPE* pParam =
1085          (QOMX_EXTNINDEX_PARAMTYPE*)paramData;
1086       if(pParam->nPortIndex == PORT_INDEX_OUT)
1087       {
1088         handle->m_max_allowed_bitrate_check =
1089            ((pParam->bEnable == OMX_TRUE) ? true : false);
1090         DEBUG_PRINT_HIGH("set_parameter: max allowed bitrate check %s",
1091            ((pParam->bEnable == OMX_TRUE) ? "enabled" : "disabled"));
1092       }
1093       else
1094       {
1095         DEBUG_PRINT_ERROR("ERROR: OMX_QcomIndexParamVideoMaxAllowedBitrateCheck "
1096            " called on wrong port(%d)", pParam->nPortIndex);
1097         return OMX_ErrorBadPortIndex;
1098       }
1099       break;
1100     }
1101 #ifdef MAX_RES_1080P
1102   case OMX_QcomIndexEnableSliceDeliveryMode:
1103     {
1104       QOMX_EXTNINDEX_PARAMTYPE* pParam =
1105          (QOMX_EXTNINDEX_PARAMTYPE*)paramData;
1106       if(pParam->nPortIndex == PORT_INDEX_OUT)
1107       {
1108         if(!handle->venc_set_param(paramData,
1109               (OMX_INDEXTYPE)OMX_QcomIndexEnableSliceDeliveryMode))
1110         {
1111           DEBUG_PRINT_ERROR("ERROR: Request for setting slice delivery mode failed");
1112           return OMX_ErrorUnsupportedSetting;
1113         }
1114       }
1115       else
1116       {
1117         DEBUG_PRINT_ERROR("ERROR: OMX_QcomIndexEnableSliceDeliveryMode "
1118            "called on wrong port(%d)", pParam->nPortIndex);
1119         return OMX_ErrorBadPortIndex;
1120       }
1121       break;
1122     }
1123 #endif
1124   case OMX_QcomIndexParamSequenceHeaderWithIDR:
1125     {
1126       if(!handle->venc_set_param(paramData,
1127             (OMX_INDEXTYPE)OMX_QcomIndexParamSequenceHeaderWithIDR))
1128       {
1129         DEBUG_PRINT_ERROR("ERROR: Request for setting inband sps/pps failed");
1130         return OMX_ErrorUnsupportedSetting;
1131       }
1132       break;
1133     }
1134   case OMX_QcomIndexParamEnableVUIStreamRestrictFlag:
1135     {
1136       if(!handle->venc_set_param(paramData,
1137             (OMX_INDEXTYPE)OMX_QcomIndexParamEnableVUIStreamRestrictFlag))
1138       {
1139         DEBUG_PRINT_ERROR("ERROR: Request for enabling bitstream_restrict "
1140                         "flag in VUI failed");
1141         return OMX_ErrorUnsupportedSetting;
1142       }
1143       break;
1144     }
1145   case OMX_IndexParamVideoSliceFMO:
1146   default:
1147     {
1148       DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown param %d\n", paramIndex);
1149       eRet = OMX_ErrorUnsupportedIndex;
1150       break;
1151     }
1152   }
1153   return eRet;
1154 }
1155 
update_profile_level()1156 bool omx_venc::update_profile_level()
1157 {
1158   OMX_U32 eProfile, eLevel;
1159 
1160   if(!handle->venc_get_profile_level(&eProfile,&eLevel))
1161   {
1162     DEBUG_PRINT_ERROR("\nFailed to update the profile_level\n");
1163     return false;
1164   }
1165 
1166   m_sParamProfileLevel.eProfile = (OMX_VIDEO_MPEG4PROFILETYPE)eProfile;
1167   m_sParamProfileLevel.eLevel = (OMX_VIDEO_MPEG4LEVELTYPE)eLevel;
1168 
1169   if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.mpeg4",\
1170               OMX_MAX_STRINGNAME_SIZE))
1171   {
1172     m_sParamMPEG4.eProfile = (OMX_VIDEO_MPEG4PROFILETYPE)eProfile;
1173     m_sParamMPEG4.eLevel = (OMX_VIDEO_MPEG4LEVELTYPE)eLevel;
1174     DEBUG_PRINT_LOW("\n MPEG4 profile = %d, level = %d", m_sParamMPEG4.eProfile,
1175                     m_sParamMPEG4.eLevel);
1176   }
1177   else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.h263",\
1178                    OMX_MAX_STRINGNAME_SIZE))
1179   {
1180     m_sParamH263.eProfile = (OMX_VIDEO_H263PROFILETYPE)eProfile;
1181     m_sParamH263.eLevel = (OMX_VIDEO_H263LEVELTYPE)eLevel;
1182     DEBUG_PRINT_LOW("\n H263 profile = %d, level = %d", m_sParamH263.eProfile,
1183                     m_sParamH263.eLevel);
1184   }
1185   else if(!strncmp((char *)m_nkind, "OMX.qcom.video.encoder.avc",\
1186                    OMX_MAX_STRINGNAME_SIZE))
1187   {
1188     m_sParamAVC.eProfile = (OMX_VIDEO_AVCPROFILETYPE)eProfile;
1189     m_sParamAVC.eLevel = (OMX_VIDEO_AVCLEVELTYPE)eLevel;
1190     DEBUG_PRINT_LOW("\n AVC profile = %d, level = %d", m_sParamAVC.eProfile,
1191                     m_sParamAVC.eLevel);
1192   }
1193   return true;
1194 }
1195 /* ======================================================================
1196 FUNCTION
1197   omx_video::SetConfig
1198 
1199 DESCRIPTION
1200   OMX Set Config method implementation
1201 
1202 PARAMETERS
1203   <TBD>.
1204 
1205 RETURN VALUE
1206   OMX Error None if successful.
1207 ========================================================================== */
set_config(OMX_IN OMX_HANDLETYPE hComp,OMX_IN OMX_INDEXTYPE configIndex,OMX_IN OMX_PTR configData)1208 OMX_ERRORTYPE  omx_venc::set_config(OMX_IN OMX_HANDLETYPE      hComp,
1209                                      OMX_IN OMX_INDEXTYPE configIndex,
1210                                      OMX_IN OMX_PTR        configData)
1211 {
1212   if(configData == NULL)
1213   {
1214     DEBUG_PRINT_ERROR("ERROR: param is null");
1215     return OMX_ErrorBadParameter;
1216   }
1217 
1218   if(m_state == OMX_StateInvalid)
1219   {
1220     DEBUG_PRINT_ERROR("ERROR: config called in Invalid state");
1221     return OMX_ErrorIncorrectStateOperation;
1222   }
1223 
1224   // params will be validated prior to venc_init
1225   switch(configIndex)
1226   {
1227   case OMX_IndexConfigVideoBitrate:
1228     {
1229       OMX_VIDEO_CONFIG_BITRATETYPE* pParam =
1230         reinterpret_cast<OMX_VIDEO_CONFIG_BITRATETYPE*>(configData);
1231       DEBUG_PRINT_LOW("\n omx_venc:: set_config(): OMX_IndexConfigVideoBitrate");
1232 
1233       if(pParam->nPortIndex == PORT_INDEX_OUT)
1234       {
1235         if(handle->venc_set_config(configData, OMX_IndexConfigVideoBitrate) != true)
1236         {
1237           DEBUG_PRINT_ERROR("ERROR: Setting OMX_IndexConfigVideoBitrate failed");
1238           return OMX_ErrorUnsupportedSetting;
1239         }
1240 
1241         m_sConfigBitrate.nEncodeBitrate = pParam->nEncodeBitrate;
1242         m_sParamBitrate.nTargetBitrate = pParam->nEncodeBitrate;
1243         m_sOutPortDef.format.video.nBitrate = pParam->nEncodeBitrate;
1244       }
1245       else
1246       {
1247         DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1248         return OMX_ErrorBadPortIndex;
1249       }
1250       break;
1251     }
1252   case OMX_IndexConfigVideoFramerate:
1253     {
1254       OMX_CONFIG_FRAMERATETYPE* pParam =
1255         reinterpret_cast<OMX_CONFIG_FRAMERATETYPE*>(configData);
1256       DEBUG_PRINT_LOW("\n omx_venc:: set_config(): OMX_IndexConfigVideoFramerate");
1257 
1258       if(pParam->nPortIndex == PORT_INDEX_OUT)
1259       {
1260         if(handle->venc_set_config(configData, OMX_IndexConfigVideoFramerate) != true)
1261         {
1262           DEBUG_PRINT_ERROR("ERROR: Setting OMX_IndexConfigVideoFramerate failed");
1263           return OMX_ErrorUnsupportedSetting;
1264         }
1265 
1266         m_sConfigFramerate.xEncodeFramerate = pParam->xEncodeFramerate;
1267         m_sOutPortDef.format.video.xFramerate = pParam->xEncodeFramerate;
1268         m_sOutPortFormat.xFramerate = pParam->xEncodeFramerate;
1269       }
1270       else
1271       {
1272         DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1273         return OMX_ErrorBadPortIndex;
1274       }
1275 
1276       break;
1277     }
1278   case QOMX_IndexConfigVideoIntraperiod:
1279     {
1280       QOMX_VIDEO_INTRAPERIODTYPE* pParam =
1281         reinterpret_cast<QOMX_VIDEO_INTRAPERIODTYPE*>(configData);
1282 
1283       if(pParam->nPortIndex == PORT_INDEX_OUT)
1284       {
1285 #ifdef MAX_RES_720P
1286         if(pParam->nBFrames > 0)
1287         {
1288           DEBUG_PRINT_ERROR("B frames not supported\n");
1289           return OMX_ErrorUnsupportedSetting;
1290         }
1291 #endif
1292         if(handle->venc_set_config(configData, (OMX_INDEXTYPE) QOMX_IndexConfigVideoIntraperiod) != true)
1293         {
1294           DEBUG_PRINT_ERROR("ERROR: Setting QOMX_IndexConfigVideoIntraperiod failed");
1295           return OMX_ErrorUnsupportedSetting;
1296         }
1297         m_sIntraperiod.nPFrames = pParam->nPFrames;
1298         m_sIntraperiod.nBFrames = pParam->nBFrames;
1299         m_sIntraperiod.nIDRPeriod = pParam->nIDRPeriod;
1300 
1301         if(m_sOutPortFormat.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
1302         {
1303           m_sParamMPEG4.nPFrames = pParam->nPFrames;
1304           if(m_sParamMPEG4.eProfile != OMX_VIDEO_MPEG4ProfileSimple)
1305             m_sParamMPEG4.nBFrames = pParam->nBFrames;
1306           else
1307             m_sParamMPEG4.nBFrames = 0;
1308         }
1309         else if(m_sOutPortFormat.eCompressionFormat == OMX_VIDEO_CodingH263)
1310         {
1311           m_sParamH263.nPFrames = pParam->nPFrames;
1312         }
1313         else
1314         {
1315           m_sParamAVC.nPFrames = pParam->nPFrames;
1316           if(m_sParamAVC.eProfile != OMX_VIDEO_AVCProfileBaseline)
1317             m_sParamAVC.nBFrames = pParam->nBFrames;
1318           else
1319             m_sParamAVC.nBFrames = 0;
1320         }
1321       }
1322       else
1323       {
1324         DEBUG_PRINT_ERROR("ERROR: (QOMX_IndexConfigVideoIntraperiod) Unsupported port index: %u", pParam->nPortIndex);
1325         return OMX_ErrorBadPortIndex;
1326       }
1327 
1328       break;
1329     }
1330 
1331   case OMX_IndexConfigVideoIntraVOPRefresh:
1332     {
1333       OMX_CONFIG_INTRAREFRESHVOPTYPE* pParam =
1334         reinterpret_cast<OMX_CONFIG_INTRAREFRESHVOPTYPE*>(configData);
1335 
1336       if(pParam->nPortIndex == PORT_INDEX_OUT)
1337       {
1338         if(handle->venc_set_config(configData,
1339             OMX_IndexConfigVideoIntraVOPRefresh) != true)
1340         {
1341           DEBUG_PRINT_ERROR("ERROR: Setting OMX_IndexConfigVideoIntraVOPRefresh failed");
1342           return OMX_ErrorUnsupportedSetting;
1343         }
1344 
1345         m_sConfigIntraRefreshVOP.IntraRefreshVOP = pParam->IntraRefreshVOP;
1346       }
1347       else
1348       {
1349         DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1350         return OMX_ErrorBadPortIndex;
1351       }
1352 
1353       break;
1354     }
1355   case OMX_IndexConfigCommonRotate:
1356     {
1357       OMX_CONFIG_ROTATIONTYPE *pParam =
1358          reinterpret_cast<OMX_CONFIG_ROTATIONTYPE*>(configData);
1359       OMX_S32 nRotation;
1360 
1361       if(pParam->nPortIndex != PORT_INDEX_IN){
1362            DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1363            return OMX_ErrorBadPortIndex;
1364       }
1365       if( pParam->nRotation == 0   ||
1366           pParam->nRotation == 90  ||
1367           pParam->nRotation == 180 ||
1368           pParam->nRotation == 270 ) {
1369           DEBUG_PRINT_HIGH("\nset_config: Rotation Angle %u", pParam->nRotation);
1370       } else {
1371           DEBUG_PRINT_ERROR("ERROR: un supported Rotation %u", pParam->nRotation);
1372           return OMX_ErrorUnsupportedSetting;
1373       }
1374       nRotation = pParam->nRotation - m_sConfigFrameRotation.nRotation;
1375       if(nRotation < 0)
1376         nRotation = -nRotation;
1377       if(nRotation == 90 || nRotation == 270) {
1378           DEBUG_PRINT_HIGH("\nset_config: updating device Dims");
1379           if(handle->venc_set_config(configData,
1380              OMX_IndexConfigCommonRotate) != true) {
1381              DEBUG_PRINT_ERROR("ERROR: Set OMX_IndexConfigCommonRotate failed");
1382              return OMX_ErrorUnsupportedSetting;
1383           } else {
1384                   OMX_U32 nFrameWidth;
1385 
1386                   DEBUG_PRINT_HIGH("\nset_config: updating port Dims");
1387 
1388                   nFrameWidth = m_sInPortDef.format.video.nFrameWidth;
1389 	          m_sInPortDef.format.video.nFrameWidth =
1390                       m_sInPortDef.format.video.nFrameHeight;
1391                   m_sInPortDef.format.video.nFrameHeight = nFrameWidth;
1392 
1393                   m_sOutPortDef.format.video.nFrameWidth  =
1394                       m_sInPortDef.format.video.nFrameWidth;
1395                   m_sOutPortDef.format.video.nFrameHeight =
1396                       m_sInPortDef.format.video.nFrameHeight;
1397                   m_sConfigFrameRotation.nRotation = pParam->nRotation;
1398            }
1399        } else {
1400           m_sConfigFrameRotation.nRotation = pParam->nRotation;
1401       }
1402       break;
1403     }
1404   case OMX_QcomIndexConfigVideoFramePackingArrangement:
1405     {
1406       if(m_sOutPortFormat.eCompressionFormat == OMX_VIDEO_CodingAVC)
1407       {
1408         OMX_QCOM_FRAME_PACK_ARRANGEMENT *configFmt =
1409           (OMX_QCOM_FRAME_PACK_ARRANGEMENT *) configData;
1410         extra_data_handle.set_frame_pack_data(configFmt);
1411       }
1412       else
1413       {
1414         DEBUG_PRINT_ERROR("ERROR: FramePackingData not supported for non AVC compression");
1415       }
1416       break;
1417     }
1418   default:
1419     DEBUG_PRINT_ERROR("ERROR: unsupported index %d", (int) configIndex);
1420     break;
1421   }
1422 
1423   return OMX_ErrorNone;
1424 }
1425 
1426 /* ======================================================================
1427 FUNCTION
1428   omx_venc::ComponentDeInit
1429 
1430 DESCRIPTION
1431   Destroys the component and release memory allocated to the heap.
1432 
1433 PARAMETERS
1434   <TBD>.
1435 
1436 RETURN VALUE
1437   OMX Error None if everything successful.
1438 
1439 ========================================================================== */
component_deinit(OMX_IN OMX_HANDLETYPE hComp)1440 OMX_ERRORTYPE  omx_venc::component_deinit(OMX_IN OMX_HANDLETYPE hComp)
1441 {
1442   OMX_U32 i = 0;
1443   DEBUG_PRINT_HIGH("\n omx_venc(): Inside component_deinit()");
1444   if(OMX_StateLoaded != m_state)
1445   {
1446     DEBUG_PRINT_ERROR("WARNING:Rxd DeInit,OMX not in LOADED state %d\n",\
1447                       m_state);
1448   }
1449   if(m_out_mem_ptr)
1450   {
1451     DEBUG_PRINT_LOW("Freeing the Output Memory\n");
1452     for(i=0; i< m_sOutPortDef.nBufferCountActual; i++ )
1453     {
1454       free_output_buffer (&m_out_mem_ptr[i]);
1455     }
1456     free(m_out_mem_ptr);
1457     m_out_mem_ptr = NULL;
1458   }
1459 
1460   /*Check if the input buffers have to be cleaned up*/
1461   if(m_inp_mem_ptr
1462 #ifdef _ANDROID_ICS_
1463      && !meta_mode_enable
1464 #endif
1465      )
1466   {
1467     DEBUG_PRINT_LOW("Freeing the Input Memory\n");
1468     for(i=0; i<m_sInPortDef.nBufferCountActual; i++ )
1469     {
1470       free_input_buffer (&m_inp_mem_ptr[i]);
1471     }
1472 
1473 
1474     free(m_inp_mem_ptr);
1475     m_inp_mem_ptr = NULL;
1476   }
1477 
1478   // Reset counters in mesg queues
1479   m_ftb_q.m_size=0;
1480   m_cmd_q.m_size=0;
1481   m_etb_q.m_size=0;
1482   m_ftb_q.m_read = m_ftb_q.m_write =0;
1483   m_cmd_q.m_read = m_cmd_q.m_write =0;
1484   m_etb_q.m_read = m_etb_q.m_write =0;
1485 
1486 #ifdef _ANDROID_
1487   // Clear the strong reference
1488   DEBUG_PRINT_HIGH("Calling m_heap_ptr.clear()\n");
1489   m_heap_ptr.clear();
1490 #endif // _ANDROID_
1491   DEBUG_PRINT_HIGH("Calling venc_close()\n");
1492   handle->venc_close();
1493   DEBUG_PRINT_HIGH("Deleting HANDLE[%p]\n", handle);
1494   delete (handle);
1495   DEBUG_PRINT_HIGH("OMX_Venc:Component Deinit\n");
1496   return OMX_ErrorNone;
1497 }
1498 
1499 
dev_stop(void)1500 OMX_U32 omx_venc::dev_stop( void)
1501 {
1502   return handle->venc_stop();
1503 }
1504 
1505 
dev_pause(void)1506 OMX_U32 omx_venc::dev_pause(void)
1507 {
1508   return handle->venc_pause();
1509 }
1510 
dev_start(void)1511 OMX_U32 omx_venc::dev_start(void)
1512 {
1513   return handle->venc_start();
1514 }
1515 
dev_flush(unsigned port)1516 OMX_U32 omx_venc::dev_flush(unsigned port)
1517 {
1518   return handle->venc_flush(port);
1519 }
dev_resume(void)1520 OMX_U32 omx_venc::dev_resume(void)
1521 {
1522   return handle->venc_resume();
1523 }
1524 
dev_start_done(void)1525 OMX_U32 omx_venc::dev_start_done(void)
1526 {
1527   return handle->venc_start_done();
1528 }
1529 
dev_stop_done(void)1530 OMX_U32 omx_venc::dev_stop_done(void)
1531 {
1532   return handle->venc_stop_done();
1533 }
1534 
dev_use_buf(void * buf_addr,unsigned port,unsigned index)1535 bool omx_venc::dev_use_buf(void *buf_addr,unsigned port,unsigned index)
1536 {
1537   return handle->venc_use_buf(buf_addr,port,index);
1538 }
1539 
dev_free_buf(void * buf_addr,unsigned port)1540 bool omx_venc::dev_free_buf(void *buf_addr,unsigned port)
1541 {
1542   return handle->venc_free_buf(buf_addr,port);
1543 }
1544 
dev_empty_buf(void * buffer,void * pmem_data_buf,unsigned index,unsigned fd)1545 bool omx_venc::dev_empty_buf(void *buffer, void *pmem_data_buf,unsigned index,unsigned fd)
1546 {
1547   return  handle->venc_empty_buf(buffer, pmem_data_buf,index,fd);
1548 }
1549 
dev_fill_buf(void * buffer,void * pmem_data_buf,unsigned index,unsigned fd)1550 bool omx_venc::dev_fill_buf(void *buffer, void *pmem_data_buf,unsigned index,unsigned fd)
1551 {
1552   return handle->venc_fill_buf(buffer, pmem_data_buf,index,fd);
1553 }
1554 
dev_get_seq_hdr(void * buffer,unsigned size,unsigned * hdrlen)1555 bool omx_venc::dev_get_seq_hdr(void *buffer, unsigned size, unsigned *hdrlen)
1556 {
1557   return handle->venc_get_seq_hdr(buffer, size, hdrlen);
1558 }
1559 
dev_loaded_start()1560 bool omx_venc::dev_loaded_start()
1561 {
1562   return handle->venc_loaded_start();
1563 }
1564 
dev_loaded_stop()1565 bool omx_venc::dev_loaded_stop()
1566 {
1567   return handle->venc_loaded_stop();
1568 }
1569 
dev_loaded_start_done()1570 bool omx_venc::dev_loaded_start_done()
1571 {
1572   return handle->venc_loaded_start_done();
1573 }
1574 
dev_loaded_stop_done()1575 bool omx_venc::dev_loaded_stop_done()
1576 {
1577   return handle->venc_loaded_stop_done();
1578 }
1579 
dev_get_buf_req(OMX_U32 * min_buff_count,OMX_U32 * actual_buff_count,OMX_U32 * buff_size,OMX_U32 port)1580 bool omx_venc::dev_get_buf_req(OMX_U32 *min_buff_count,
1581                                OMX_U32 *actual_buff_count,
1582                                OMX_U32 *buff_size,
1583                                OMX_U32 port)
1584 {
1585   return handle->venc_get_buf_req(min_buff_count,
1586                                   actual_buff_count,
1587                                   buff_size,
1588                                   port);
1589 
1590 }
1591 
dev_set_buf_req(OMX_U32 * min_buff_count,OMX_U32 * actual_buff_count,OMX_U32 * buff_size,OMX_U32 port)1592 bool omx_venc::dev_set_buf_req(OMX_U32 *min_buff_count,
1593                                OMX_U32 *actual_buff_count,
1594                                OMX_U32 *buff_size,
1595                                OMX_U32 port)
1596 {
1597   return handle->venc_set_buf_req(min_buff_count,
1598                                   actual_buff_count,
1599                                   buff_size,
1600                                   port);
1601 
1602 }
1603 
async_message_process(void * context,void * message)1604 int omx_venc::async_message_process (void *context, void* message)
1605 {
1606   omx_video* omx = NULL;
1607   struct venc_msg *m_sVenc_msg = NULL;
1608   OMX_BUFFERHEADERTYPE* omxhdr = NULL;
1609   struct venc_buffer *temp_buff = NULL;
1610 
1611   if(context == NULL || message == NULL)
1612   {
1613     DEBUG_PRINT_ERROR("\nERROR: omx_venc::async_message_process invalid i/p params");
1614     return -1;
1615   }
1616   m_sVenc_msg = (struct venc_msg *)message;
1617 
1618   omx = reinterpret_cast<omx_video*>(context);
1619 
1620   if(m_sVenc_msg->statuscode != VEN_S_SUCCESS)
1621   {
1622     DEBUG_PRINT_ERROR("\nERROR: async_msg_process() - Error statuscode = %d\n",
1623         m_sVenc_msg->statuscode);
1624     omx->omx_report_error();
1625   }
1626 
1627   DEBUG_PRINT_LOW("\n omx_venc::async_message_process- msgcode = %d\n",
1628                m_sVenc_msg->msgcode);
1629   switch(m_sVenc_msg->msgcode)
1630   {
1631 
1632   case VEN_MSG_START:
1633     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1634                      OMX_COMPONENT_GENERATE_START_DONE);
1635     break;
1636 
1637   case VEN_MSG_STOP:
1638     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1639                      OMX_COMPONENT_GENERATE_STOP_DONE);
1640     break;
1641 
1642   case VEN_MSG_RESUME:
1643     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1644                      OMX_COMPONENT_GENERATE_RESUME_DONE);
1645     break;
1646 
1647   case VEN_MSG_PAUSE:
1648     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1649                      OMX_COMPONENT_GENERATE_PAUSE_DONE);
1650 
1651     break;
1652 
1653   case VEN_MSG_FLUSH_INPUT_DONE:
1654 
1655     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1656                      OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH);
1657     break;
1658   case VEN_MSG_FLUSH_OUPUT_DONE:
1659     omx->post_event (NULL,m_sVenc_msg->statuscode,\
1660                      OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH);
1661     break;
1662   case VEN_MSG_INPUT_BUFFER_DONE:
1663     omxhdr = (OMX_BUFFERHEADERTYPE* )\
1664              m_sVenc_msg->buf.clientdata;
1665 
1666     if(omxhdr == NULL ||
1667        (((OMX_U32)(omxhdr - omx->m_inp_mem_ptr) > omx->m_sInPortDef.nBufferCountActual) &&
1668         ((OMX_U32)(omxhdr - omx->meta_buffer_hdr) > omx->m_sInPortDef.nBufferCountActual)))
1669     {
1670       omxhdr = NULL;
1671       m_sVenc_msg->statuscode = VEN_S_EFAIL;
1672     }
1673 
1674 #ifdef _ANDROID_ICS_
1675       omx->omx_release_meta_buffer(omxhdr);
1676 #endif
1677     omx->post_event ((unsigned int)omxhdr,m_sVenc_msg->statuscode,
1678                      OMX_COMPONENT_GENERATE_EBD);
1679     break;
1680   case VEN_MSG_OUTPUT_BUFFER_DONE:
1681 
1682     omxhdr = (OMX_BUFFERHEADERTYPE*)m_sVenc_msg->buf.clientdata;
1683 
1684     if( (omxhdr != NULL) &&
1685         ((OMX_U32)(omxhdr - omx->m_out_mem_ptr)  < omx->m_sOutPortDef.nBufferCountActual))
1686     {
1687       if(m_sVenc_msg->buf.len <=  omxhdr->nAllocLen)
1688       {
1689         omxhdr->nFilledLen = m_sVenc_msg->buf.len;
1690         omxhdr->nOffset = m_sVenc_msg->buf.offset;
1691         omxhdr->nTimeStamp = m_sVenc_msg->buf.timestamp;
1692         DEBUG_PRINT_LOW("\n o/p TS = %u", (OMX_U32)m_sVenc_msg->buf.timestamp);
1693         omxhdr->nFlags = m_sVenc_msg->buf.flags;
1694 
1695         /*Use buffer case*/
1696         if(omx->output_use_buffer && !omx->m_use_output_pmem)
1697         {
1698           DEBUG_PRINT_LOW("\n memcpy() for o/p Heap UseBuffer");
1699           memcpy(omxhdr->pBuffer,
1700                  (m_sVenc_msg->buf.ptrbuffer),
1701                   m_sVenc_msg->buf.len);
1702         }
1703       }
1704       else
1705       {
1706         omxhdr->nFilledLen = 0;
1707       }
1708 
1709     }
1710     else
1711     {
1712       omxhdr = NULL;
1713       m_sVenc_msg->statuscode = VEN_S_EFAIL;
1714     }
1715 
1716     omx->post_event ((unsigned int)omxhdr,m_sVenc_msg->statuscode,
1717                      OMX_COMPONENT_GENERATE_FBD);
1718     break;
1719   case VEN_MSG_NEED_OUTPUT_BUFFER:
1720     //TBD what action needs to be done here??
1721     break;
1722   default:
1723     break;
1724   }
1725   return 0;
1726 }
is_secure_session()1727 bool omx_venc::is_secure_session()
1728 {
1729   return secure_session;
1730 }
1731