1 /*--------------------------------------------------------------------------
2 Copyright (c) 2014-2017, 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 The Linux Foundation 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_swvenc_mpeg4.h"
29 
30 /* def: StoreMetaDataInBuffersParams */
31 #include <media/hardware/HardwareAPI.h>
32 
33 /* def: VENUS_BUFFER_SIZE, VENUS_Y_STRIDE etc */
34 #include <media/msm_media_info.h>
35 
36 /* def: private_handle_t*/
37 #include <gralloc_priv.h>
38 
39 
40 /*----------------------------------------------------------------------------
41  * Preprocessor Definitions and Constants
42  * -------------------------------------------------------------------------*/
43 #define OMX_SPEC_VERSION 0x00000101
44 #define OMX_INIT_STRUCT(_s_, _name_)             \
45     memset((_s_), 0x0, sizeof(_name_));          \
46     (_s_)->nSize = sizeof(_name_);               \
47     (_s_)->nVersion.nVersion = OMX_SPEC_VERSION
48 
49 #define ENTER_FUNC() DEBUG_PRINT_HIGH("ENTERING: %s",__FUNCTION__)
50 #define EXIT_FUNC()  DEBUG_PRINT_HIGH("EXITING: %s",__FUNCTION__)
51 #define RETURN(x)    EXIT_FUNC(); return x;
52 #define ALIGN(value,alignment) (((value) + (alignment-1)) & (~(alignment-1)))
53 
54 #define BUFFER_LOG_LOC "/data/vendor/media"
55 
56 /* factory function executed by the core to create instances */
get_omx_component_factory_fn(void)57 void *get_omx_component_factory_fn(void)
58 {
59     RETURN((new omx_venc));
60 }
61 
omx_venc()62 omx_venc::omx_venc()
63 {
64     ENTER_FUNC();
65 
66     char property_value[PROPERTY_VALUE_MAX] = {0};
67 
68     memset(&m_debug,0,sizeof(m_debug));
69 
70     property_value[0] = '\0';
71     property_get("vendor.vidc.debug.level", property_value, "1");
72     debug_level = atoi(property_value);
73 
74     property_value[0] = '\0';
75     property_get("vendor.vidc.enc.log.in", property_value, "0");
76     m_debug.in_buffer_log = atoi(property_value);
77 
78     property_value[0] = '\0';
79     property_get("vendor.vidc.enc.log.out", property_value, "0");
80     m_debug.out_buffer_log = atoi(property_value);
81 
82     snprintf(m_debug.log_loc, PROPERTY_VALUE_MAX, "%s", BUFFER_LOG_LOC);
83     property_value[0] = '\0';
84     property_get("vendor.vidc.log.loc", property_value, "");
85     if (*property_value)
86     {
87        strlcpy(m_debug.log_loc, property_value, PROPERTY_VALUE_MAX);
88     }
89 
90     memset(meta_buffer_hdr,0,sizeof(meta_buffer_hdr));
91     meta_mode_enable = false;
92     memset(meta_buffer_hdr,0,sizeof(meta_buffer_hdr));
93     memset(meta_buffers,0,sizeof(meta_buffers));
94     memset(opaque_buffer_hdr,0,sizeof(opaque_buffer_hdr));
95     mUseProxyColorFormat = false;
96     get_syntaxhdr_enable = false;
97     m_bSeqHdrRequested = false;
98     set_format = false;
99 
100     EXIT_FUNC();
101 }
102 
~omx_venc()103 omx_venc::~omx_venc()
104 {
105     ENTER_FUNC();
106     get_syntaxhdr_enable = false;
107     EXIT_FUNC();
108 }
109 
component_init(OMX_STRING role)110 OMX_ERRORTYPE omx_venc::component_init(OMX_STRING role)
111 {
112     ENTER_FUNC();
113 
114     OMX_ERRORTYPE eRet = OMX_ErrorNone;
115     SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
116     SWVENC_CALLBACK callBackInfo;
117     OMX_VIDEO_CODINGTYPE codec_type;
118     SWVENC_PROPERTY Prop;
119     int fds[2];
120 
121     strlcpy((char *)m_nkind,role,OMX_MAX_STRINGNAME_SIZE);
122     secure_session = false;
123 
124     if (!strncmp( (char *)m_nkind,"OMX.qcom.video.encoder.mpeg4sw",
125                   OMX_MAX_STRINGNAME_SIZE))
126     {
127         strlcpy((char *)m_cRole, "video_encoder.mpeg4",\
128                 OMX_MAX_STRINGNAME_SIZE);
129         codec_type = OMX_VIDEO_CodingMPEG4;
130         m_codec = SWVENC_CODEC_MPEG4;
131     }
132     else if (!strncmp( (char *)m_nkind,"OMX.qcom.video.encoder.h263sw",
133                   OMX_MAX_STRINGNAME_SIZE))
134     {
135         strlcpy((char *)m_cRole, "video_encoder.h263",\
136                 OMX_MAX_STRINGNAME_SIZE);
137         codec_type = OMX_VIDEO_CodingH263;
138         m_codec = SWVENC_CODEC_H263;
139     }
140     else
141     {
142         DEBUG_PRINT_ERROR("ERROR: Unknown Component");
143         eRet = OMX_ErrorInvalidComponentName;
144         RETURN(eRet);
145     }
146 
147 #ifdef ENABLE_GET_SYNTAX_HDR
148     get_syntaxhdr_enable = true;
149     DEBUG_PRINT_HIGH("Get syntax header enabled");
150 #endif
151 
152     callBackInfo.pfn_empty_buffer_done    = swvenc_empty_buffer_done_cb;
153     callBackInfo.pfn_fill_buffer_done     = swvenc_fill_buffer_done_cb;
154     callBackInfo.pfn_event_notification   = swvenc_handle_event_cb;
155     callBackInfo.p_client                 = (void*)this;
156 
157     SWVENC_STATUS sRet = swvenc_init(&m_hSwVenc, m_codec, &callBackInfo);
158     if (sRet != SWVENC_S_SUCCESS)
159     {
160         DEBUG_PRINT_ERROR("swvenc_init returned %d, ret insufficient resources",
161          sRet);
162         RETURN(OMX_ErrorInsufficientResources);
163     }
164 
165     m_stopped = true;
166 
167     //Intialise the OMX layer variables
168     memset(&m_pCallbacks,0,sizeof(OMX_CALLBACKTYPE));
169 
170     OMX_INIT_STRUCT(&m_sPortParam, OMX_PORT_PARAM_TYPE);
171     m_sPortParam.nPorts = 0x2;
172     m_sPortParam.nStartPortNumber = (OMX_U32) PORT_INDEX_IN;
173 
174     OMX_INIT_STRUCT(&m_sPortParam_audio, OMX_PORT_PARAM_TYPE);
175     m_sPortParam_audio.nPorts = 0;
176     m_sPortParam_audio.nStartPortNumber = 0;
177 
178     OMX_INIT_STRUCT(&m_sPortParam_img, OMX_PORT_PARAM_TYPE);
179     m_sPortParam_img.nPorts = 0;
180     m_sPortParam_img.nStartPortNumber = 0;
181 
182     OMX_INIT_STRUCT(&m_sParamBitrate, OMX_VIDEO_PARAM_BITRATETYPE);
183     m_sParamBitrate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
184     m_sParamBitrate.eControlRate = OMX_Video_ControlRateVariableSkipFrames;
185     m_sParamBitrate.nTargetBitrate = 64000;
186 
187     OMX_INIT_STRUCT(&m_sConfigBitrate, OMX_VIDEO_CONFIG_BITRATETYPE);
188     m_sConfigBitrate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
189     m_sConfigBitrate.nEncodeBitrate = 64000;
190 
191     OMX_INIT_STRUCT(&m_sConfigFramerate, OMX_CONFIG_FRAMERATETYPE);
192     m_sConfigFramerate.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
193     m_sConfigFramerate.xEncodeFramerate = 30 << 16;
194 
195     OMX_INIT_STRUCT(&m_sConfigIntraRefreshVOP, OMX_CONFIG_INTRAREFRESHVOPTYPE);
196     m_sConfigIntraRefreshVOP.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
197     m_sConfigIntraRefreshVOP.IntraRefreshVOP = OMX_FALSE;
198 
199     OMX_INIT_STRUCT(&m_sConfigFrameRotation, OMX_CONFIG_ROTATIONTYPE);
200     m_sConfigFrameRotation.nPortIndex = (OMX_U32) PORT_INDEX_IN;
201     m_sConfigFrameRotation.nRotation = 0;
202 
203     OMX_INIT_STRUCT(&m_sSessionQuantization, OMX_VIDEO_PARAM_QUANTIZATIONTYPE);
204     m_sSessionQuantization.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
205     m_sSessionQuantization.nQpI = 9;
206     m_sSessionQuantization.nQpP = 6;
207     m_sSessionQuantization.nQpB = 2;
208 
209     OMX_INIT_STRUCT(&m_sSessionQPRange, OMX_QCOM_VIDEO_PARAM_QPRANGETYPE);
210     m_sSessionQPRange.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
211     m_sSessionQPRange.minQP = 2;
212 
213     OMX_INIT_STRUCT(&m_sParamProfileLevel, OMX_VIDEO_PARAM_PROFILELEVELTYPE);
214     m_sParamProfileLevel.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
215 
216     OMX_INIT_STRUCT(&m_sIntraperiod, QOMX_VIDEO_INTRAPERIODTYPE);
217     m_sIntraperiod.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
218     m_sIntraperiod.nPFrames = (m_sConfigFramerate.xEncodeFramerate * 2) - 1;
219 
220     OMX_INIT_STRUCT(&m_sErrorCorrection, OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE);
221     m_sErrorCorrection.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
222     m_sErrorCorrection.bEnableDataPartitioning = OMX_FALSE;
223     m_sErrorCorrection.bEnableHEC = OMX_FALSE;
224     m_sErrorCorrection.bEnableResync = OMX_FALSE;
225     m_sErrorCorrection.bEnableRVLC = OMX_FALSE;
226     m_sErrorCorrection.nResynchMarkerSpacing = 0;
227 
228     OMX_INIT_STRUCT(&m_sIntraRefresh, OMX_VIDEO_PARAM_INTRAREFRESHTYPE);
229     m_sIntraRefresh.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
230     m_sIntraRefresh.eRefreshMode = OMX_VIDEO_IntraRefreshMax;
231 
232     if (codec_type == OMX_VIDEO_CodingMPEG4)
233     {
234         m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_MPEG4ProfileSimple;
235         m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_MPEG4Level0;
236     } else if (codec_type == OMX_VIDEO_CodingH263)
237     {
238         m_sParamProfileLevel.eProfile = (OMX_U32) OMX_VIDEO_H263ProfileBaseline;
239         m_sParamProfileLevel.eLevel = (OMX_U32) OMX_VIDEO_H263Level10;
240     }
241 
242     /* set the profile and level */
243     Ret = swvenc_set_profile_level(m_sParamProfileLevel.eProfile,
244                 m_sParamProfileLevel.eLevel);
245     if (Ret != SWVENC_S_SUCCESS)
246     {
247        DEBUG_PRINT_ERROR("%s, swvenc_set_rc_mode failed (%d)",
248          __FUNCTION__, Ret);
249        RETURN(OMX_ErrorUndefined);
250     }
251 
252     // Initialize the video parameters for input port
253     OMX_INIT_STRUCT(&m_sInPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
254     m_sInPortDef.nPortIndex= (OMX_U32) PORT_INDEX_IN;
255     m_sInPortDef.bEnabled = OMX_TRUE;
256     m_sInPortDef.bPopulated = OMX_FALSE;
257     m_sInPortDef.eDomain = OMX_PortDomainVideo;
258     m_sInPortDef.eDir = OMX_DirInput;
259     m_sInPortDef.format.video.cMIMEType = (char *)"YUV420";
260     m_sInPortDef.format.video.nFrameWidth = OMX_CORE_QCIF_WIDTH;
261     m_sInPortDef.format.video.nFrameHeight = OMX_CORE_QCIF_HEIGHT;
262     m_sInPortDef.format.video.nStride = OMX_CORE_QCIF_WIDTH;
263     m_sInPortDef.format.video.nSliceHeight = OMX_CORE_QCIF_HEIGHT;
264     m_sInPortDef.format.video.nBitrate = 64000;
265     m_sInPortDef.format.video.xFramerate = 15 << 16;
266     m_sInPortDef.format.video.eColorFormat = (OMX_COLOR_FORMATTYPE)
267         QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
268     m_sInPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingUnused;
269 
270     /* set the frame size */
271     Prop.id = SWVENC_PROPERTY_ID_FRAME_SIZE;
272     Prop.info.frame_size.height = m_sInPortDef.format.video.nFrameHeight;
273     Prop.info.frame_size.width  = m_sInPortDef.format.video.nFrameWidth;
274 
275     Ret = swvenc_setproperty(m_hSwVenc, &Prop);
276     if (Ret != SWVENC_S_SUCCESS)
277     {
278        DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
279          __FUNCTION__, Ret);
280        RETURN(OMX_ErrorUnsupportedSetting);
281     }
282 
283     /* set the frame attributes */
284     Prop.id = SWVENC_PROPERTY_ID_FRAME_ATTRIBUTES;
285     Prop.info.frame_attributes.stride_luma = m_sInPortDef.format.video.nStride;
286     Prop.info.frame_attributes.stride_chroma = m_sInPortDef.format.video.nStride;
287     Prop.info.frame_attributes.offset_luma = 0;
288     Prop.info.frame_attributes.offset_chroma =
289       (m_sInPortDef.format.video.nSliceHeight * m_sInPortDef.format.video.nStride);
290     Prop.info.frame_attributes.size = (Prop.info.frame_attributes.offset_chroma * 3) >> 1;
291 
292     Ret = swvenc_setproperty(m_hSwVenc, &Prop);
293     if (Ret != SWVENC_S_SUCCESS)
294     {
295        DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
296          __FUNCTION__, Ret);
297        RETURN(OMX_ErrorUndefined);
298     }
299 
300     Ret = swvenc_get_buffer_req(&m_sInPortDef.nBufferCountMin,
301               &m_sInPortDef.nBufferCountActual,
302               &m_sInPortDef.nBufferSize,
303               &m_sInPortDef.nBufferAlignment,
304               PORT_INDEX_IN);
305     if (Ret != SWVENC_S_SUCCESS)
306     {
307        DEBUG_PRINT_ERROR("ERROR: %s, swvenc_get_buffer_req failed (%d)", __FUNCTION__,
308           Ret);
309        RETURN(OMX_ErrorUndefined);
310     }
311 
312     // Initialize the video parameters for output port
313     OMX_INIT_STRUCT(&m_sOutPortDef, OMX_PARAM_PORTDEFINITIONTYPE);
314     m_sOutPortDef.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
315     m_sOutPortDef.bEnabled = OMX_TRUE;
316     m_sOutPortDef.bPopulated = OMX_FALSE;
317     m_sOutPortDef.eDomain = OMX_PortDomainVideo;
318     m_sOutPortDef.eDir = OMX_DirOutput;
319     m_sOutPortDef.format.video.nFrameWidth = OMX_CORE_QCIF_WIDTH;
320     m_sOutPortDef.format.video.nFrameHeight = OMX_CORE_QCIF_HEIGHT;
321     m_sOutPortDef.format.video.nBitrate = 64000;
322     m_sOutPortDef.format.video.xFramerate = 15 << 16;
323     m_sOutPortDef.format.video.eColorFormat =  OMX_COLOR_FormatUnused;
324     if (codec_type == OMX_VIDEO_CodingMPEG4)
325     {
326         m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingMPEG4;
327     }
328     else if (codec_type == OMX_VIDEO_CodingH263)
329     {
330         m_sOutPortDef.format.video.eCompressionFormat =  OMX_VIDEO_CodingH263;
331     }
332 
333     Ret = swvenc_get_buffer_req(&m_sOutPortDef.nBufferCountMin,
334               &m_sOutPortDef.nBufferCountActual,
335               &m_sOutPortDef.nBufferSize,
336               &m_sOutPortDef.nBufferAlignment,
337               PORT_INDEX_OUT);
338     if (Ret != SWVENC_S_SUCCESS)
339     {
340        DEBUG_PRINT_ERROR("ERROR: %s, swvenc_get_buffer_req failed (%d)", __FUNCTION__,
341           Ret);
342        RETURN(OMX_ErrorUndefined);
343     }
344 
345     // Initialize the video color format for input port
346     OMX_INIT_STRUCT(&m_sInPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
347     m_sInPortFormat.nPortIndex = (OMX_U32) PORT_INDEX_IN;
348     m_sInPortFormat.nIndex = 0;
349     m_sInPortFormat.eColorFormat = (OMX_COLOR_FORMATTYPE)
350         QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
351     m_sInPortFormat.eCompressionFormat = OMX_VIDEO_CodingUnused;
352 
353     // Initialize the compression format for output port
354     OMX_INIT_STRUCT(&m_sOutPortFormat, OMX_VIDEO_PARAM_PORTFORMATTYPE);
355     m_sOutPortFormat.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
356     m_sOutPortFormat.nIndex = 0;
357     m_sOutPortFormat.eColorFormat = OMX_COLOR_FormatUnused;
358     if (codec_type == OMX_VIDEO_CodingMPEG4)
359     {
360         m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingMPEG4;
361     } else if (codec_type == OMX_VIDEO_CodingH263)
362     {
363         m_sOutPortFormat.eCompressionFormat =  OMX_VIDEO_CodingH263;
364     }
365 
366     // mandatory Indices for kronos test suite
367     OMX_INIT_STRUCT(&m_sPriorityMgmt, OMX_PRIORITYMGMTTYPE);
368 
369     OMX_INIT_STRUCT(&m_sInBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
370     m_sInBufSupplier.nPortIndex = (OMX_U32) PORT_INDEX_IN;
371 
372     OMX_INIT_STRUCT(&m_sOutBufSupplier, OMX_PARAM_BUFFERSUPPLIERTYPE);
373     m_sOutBufSupplier.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
374 
375     OMX_INIT_STRUCT(&m_sParamInitqp, QOMX_EXTNINDEX_VIDEO_INITIALQP);
376     m_sParamInitqp.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
377 
378     // mp4 specific init
379     OMX_INIT_STRUCT(&m_sParamMPEG4, OMX_VIDEO_PARAM_MPEG4TYPE);
380     m_sParamMPEG4.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
381     m_sParamMPEG4.eProfile = OMX_VIDEO_MPEG4ProfileSimple;
382     m_sParamMPEG4.eLevel = OMX_VIDEO_MPEG4Level0;
383     m_sParamMPEG4.nSliceHeaderSpacing = 0;
384     m_sParamMPEG4.bSVH = OMX_FALSE;
385     m_sParamMPEG4.bGov = OMX_FALSE;
386     // 2 second intra period for default outport fps
387     m_sParamMPEG4.nPFrames = (m_sOutPortFormat.xFramerate * 2 - 1);
388     m_sParamMPEG4.bACPred = OMX_TRUE;
389     // delta = 2 @ 15 fps
390     m_sParamMPEG4.nTimeIncRes = 30;
391     // pframe and iframe
392     m_sParamMPEG4.nAllowedPictureTypes = 2;
393     // number of video packet headers per vop
394     m_sParamMPEG4.nHeaderExtension = 1;
395     m_sParamMPEG4.bReversibleVLC = OMX_FALSE;
396 
397     // h263 specific init
398     OMX_INIT_STRUCT(&m_sParamH263, OMX_VIDEO_PARAM_H263TYPE);
399     m_sParamH263.nPortIndex = (OMX_U32) PORT_INDEX_OUT;
400     // 2 second intra period for default outport fps
401     m_sParamH263.nPFrames = (m_sOutPortFormat.xFramerate * 2 - 1);
402     m_sParamH263.nBFrames = 0;
403     m_sParamH263.eProfile = OMX_VIDEO_H263ProfileBaseline;
404     m_sParamH263.eLevel = OMX_VIDEO_H263Level10;
405     m_sParamH263.bPLUSPTYPEAllowed = OMX_FALSE;
406     m_sParamH263.nAllowedPictureTypes = 2;
407     m_sParamH263.bForceRoundingTypeToZero = OMX_TRUE;
408     m_sParamH263.nPictureHeaderRepetition = 0;
409     m_sParamH263.nGOBHeaderInterval = 1;
410 
411     m_state                   = OMX_StateLoaded;
412     m_sExtraData = 0;
413 
414     m_capability.max_height = OMX_CORE_FWVGA_HEIGHT;
415     m_capability.max_width = OMX_CORE_FWVGA_WIDTH;
416     m_capability.min_height = 32;
417     m_capability.min_width = 32;
418 
419     if (eRet == OMX_ErrorNone)
420     {
421         if (pipe(fds))
422         {
423             DEBUG_PRINT_ERROR("ERROR: pipe creation failed");
424             eRet = OMX_ErrorInsufficientResources;
425         }
426         else
427         {
428             if ((fds[0] == 0) || (fds[1] == 0))
429             {
430                 if (pipe(fds))
431                 {
432                     DEBUG_PRINT_ERROR("ERROR: pipe creation failed");
433                     eRet = OMX_ErrorInsufficientResources;
434                 }
435             }
436             if (eRet == OMX_ErrorNone)
437             {
438                 m_pipe_in = fds[0];
439                 m_pipe_out = fds[1];
440 
441                 if (pthread_create(&msg_thread_id,0, message_thread_enc, this) < 0) {
442                     eRet = OMX_ErrorInsufficientResources;
443                     msg_thread_created = false;
444                 }
445                 else {
446                     msg_thread_created = true;
447                 }
448             }
449         }
450     }
451 
452     DEBUG_PRINT_HIGH("Component_init return value = 0x%x", eRet);
453 
454     EXIT_FUNC();
455 
456     RETURN(eRet);
457 }
458 
set_parameter(OMX_IN OMX_HANDLETYPE hComp,OMX_IN OMX_INDEXTYPE paramIndex,OMX_IN OMX_PTR paramData)459 OMX_ERRORTYPE  omx_venc::set_parameter
460 (
461     OMX_IN OMX_HANDLETYPE hComp,
462     OMX_IN OMX_INDEXTYPE  paramIndex,
463     OMX_IN OMX_PTR        paramData
464 )
465 {
466     ENTER_FUNC();
467 
468     OMX_ERRORTYPE eRet = OMX_ErrorNone;
469     SWVENC_STATUS Ret  = SWVENC_S_SUCCESS;
470     SWVENC_PROPERTY Prop;
471     bool bResult;
472     unsigned int stride, scanlines;
473 
474     (void)hComp;
475 
476     if (m_state == OMX_StateInvalid)
477     {
478         DEBUG_PRINT_ERROR("ERROR: Set Param in Invalid State");
479         RETURN(OMX_ErrorInvalidState);
480     }
481     if (paramData == NULL)
482     {
483         DEBUG_PRINT_ERROR("ERROR: Get Param in Invalid paramData");
484         RETURN(OMX_ErrorBadParameter);
485     }
486 
487     /* set_parameter can be called in loaded state or disabled port */
488     if ( (m_state == OMX_StateLoaded) ||
489          (m_sInPortDef.bEnabled == OMX_FALSE) ||
490          (m_sOutPortDef.bEnabled == OMX_FALSE)
491        )
492     {
493         DEBUG_PRINT_LOW("Set Parameter called in valid state");
494     }
495     else
496     {
497         DEBUG_PRINT_ERROR("ERROR: Set Parameter called in Invalid State");
498         RETURN(OMX_ErrorIncorrectStateOperation);
499     }
500 
501     switch ((int)paramIndex)
502     {
503         case OMX_IndexParamPortDefinition:
504         {
505             OMX_PARAM_PORTDEFINITIONTYPE *portDefn;
506             portDefn = (OMX_PARAM_PORTDEFINITIONTYPE *) paramData;
507             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPortDefinition H= %d, W = %d",
508                     (int)portDefn->format.video.nFrameHeight,
509                     (int)portDefn->format.video.nFrameWidth);
510 
511             if (PORT_INDEX_IN == portDefn->nPortIndex)
512             {
513                 if (!dev_is_video_session_supported(portDefn->format.video.nFrameWidth,
514                             portDefn->format.video.nFrameHeight))
515                 {
516                     DEBUG_PRINT_ERROR("video session not supported");
517                     omx_report_unsupported_setting();
518                     RETURN(OMX_ErrorUnsupportedSetting);
519                 }
520                 DEBUG_PRINT_LOW("i/p actual cnt requested = %u", portDefn->nBufferCountActual);
521                 DEBUG_PRINT_LOW("i/p min cnt requested = %u", portDefn->nBufferCountMin);
522                 DEBUG_PRINT_LOW("i/p buffersize requested = %u", portDefn->nBufferSize);
523                 if (portDefn->nBufferCountMin > portDefn->nBufferCountActual)
524                 {
525                     DEBUG_PRINT_ERROR("ERROR: (In_PORT) Min buffers (%u) > actual count (%u)",
526                             portDefn->nBufferCountMin, portDefn->nBufferCountActual);
527                     RETURN(OMX_ErrorUnsupportedSetting);
528                 }
529 
530                 /* set the frame size */
531                 Prop.id = SWVENC_PROPERTY_ID_FRAME_SIZE;
532                 Prop.info.frame_size.height = portDefn->format.video.nFrameHeight;
533                 Prop.info.frame_size.width  = portDefn->format.video.nFrameWidth;
534 
535                 Ret = swvenc_setproperty(m_hSwVenc, &Prop);
536                 if (Ret != SWVENC_S_SUCCESS)
537                 {
538                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
539                      __FUNCTION__, Ret);
540                    RETURN(OMX_ErrorUnsupportedSetting);
541                 }
542 
543                 /* set the input frame-rate */
544                 if (portDefn->format.video.xFramerate != 0)
545                 {
546                    Ret = swvenc_set_frame_rate(portDefn->format.video.xFramerate >> 16);
547                    if (Ret != SWVENC_S_SUCCESS)
548                    {
549                       DEBUG_PRINT_ERROR("%s, swvenc_set_frame_rate failed (%d)",
550                         __FUNCTION__, Ret);
551                       RETURN(OMX_ErrorUnsupportedSetting);
552                    }
553                 }
554 
555                 /* set the frame attributes */
556                 stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, portDefn->format.video.nFrameWidth);
557                 scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, portDefn->format.video.nSliceHeight);
558                 Prop.id = SWVENC_PROPERTY_ID_FRAME_ATTRIBUTES;
559                 Prop.info.frame_attributes.stride_luma = stride;
560                 Prop.info.frame_attributes.stride_chroma = stride;
561                 Prop.info.frame_attributes.offset_luma = 0;
562                 Prop.info.frame_attributes.offset_chroma = scanlines * stride;
563                 Prop.info.frame_attributes.size =
564                   VENUS_BUFFER_SIZE(COLOR_FMT_NV12,
565                      portDefn->format.video.nFrameWidth,
566                      portDefn->format.video.nFrameHeight);
567 
568                 Ret = swvenc_setproperty(m_hSwVenc, &Prop);
569                 if (Ret != SWVENC_S_SUCCESS)
570                 {
571                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
572                      __FUNCTION__, Ret);
573                    RETURN(OMX_ErrorUnsupportedSetting);
574                 }
575 
576                 DEBUG_PRINT_LOW("i/p previous actual cnt = %u", m_sInPortDef.nBufferCountActual);
577                 DEBUG_PRINT_LOW("i/p previous min cnt = %u", m_sInPortDef.nBufferCountMin);
578                 DEBUG_PRINT_LOW("i/p previous buffersize = %u", m_sInPortDef.nBufferSize);
579 
580                 memcpy(&m_sInPortDef, portDefn,sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
581 
582                 /* update the input buffer requirement */
583                 Ret = swvenc_get_buffer_req(&m_sInPortDef.nBufferCountMin,
584                         &m_sInPortDef.nBufferCountActual,
585                         &m_sInPortDef.nBufferSize,
586                         &m_sInPortDef.nBufferAlignment,
587                         portDefn->nPortIndex);
588                 if (Ret != SWVENC_S_SUCCESS)
589                 {
590                    DEBUG_PRINT_ERROR("ERROR: %s, swvenc_get_buffer_req failed (%d)", __FUNCTION__,
591                       Ret);
592                    RETURN(OMX_ErrorUndefined);
593                 }
594 
595                 if (portDefn->nBufferCountActual > m_sInPortDef.nBufferCountActual)
596                 {
597                    m_sInPortDef.nBufferCountActual = portDefn->nBufferCountActual;
598                 }
599                 if (portDefn->nBufferSize > m_sInPortDef.nBufferSize)
600                 {
601                    m_sInPortDef.nBufferSize = portDefn->nBufferSize;
602                 }
603 
604                 DEBUG_PRINT_LOW("i/p new actual cnt = %u", m_sInPortDef.nBufferCountActual);
605                 DEBUG_PRINT_LOW("i/p new min cnt = %u", m_sInPortDef.nBufferCountMin);
606                 DEBUG_PRINT_LOW("i/p new buffersize = %u", m_sInPortDef.nBufferSize);
607             }
608             else if (PORT_INDEX_OUT == portDefn->nPortIndex)
609             {
610                 DEBUG_PRINT_LOW("o/p actual cnt requested = %u", portDefn->nBufferCountActual);
611                 DEBUG_PRINT_LOW("o/p min cnt requested = %u", portDefn->nBufferCountMin);
612                 DEBUG_PRINT_LOW("o/p buffersize requested = %u", portDefn->nBufferSize);
613                 if (portDefn->nBufferCountMin > portDefn->nBufferCountActual)
614                 {
615                     DEBUG_PRINT_ERROR("ERROR: (Out_PORT) Min buffers (%u) > actual count (%u)",
616                             portDefn->nBufferCountMin, portDefn->nBufferCountActual);
617                     RETURN(OMX_ErrorUnsupportedSetting);
618                 }
619 
620                 /* set the output bit-rate */
621                 Ret = swvenc_set_bit_rate(portDefn->format.video.nBitrate);
622                 if (Ret != SWVENC_S_SUCCESS)
623                 {
624                    DEBUG_PRINT_ERROR("%s, swvenc_set_bit_rate failed (%d)",
625                      __FUNCTION__, Ret);
626                    RETURN(OMX_ErrorUnsupportedSetting);
627                 }
628 
629                 DEBUG_PRINT_LOW("o/p previous actual cnt = %u", m_sOutPortDef.nBufferCountActual);
630                 DEBUG_PRINT_LOW("o/p previous min cnt = %u", m_sOutPortDef.nBufferCountMin);
631                 DEBUG_PRINT_LOW("o/p previous buffersize = %u", m_sOutPortDef.nBufferSize);
632 
633                 /* set the buffer requirement */
634                 bResult = dev_set_buf_req(&portDefn->nBufferCountMin,
635                   &portDefn->nBufferCountActual,
636                   &portDefn->nBufferSize,
637                   portDefn->nPortIndex);
638                 if (bResult != true)
639                 {
640                    DEBUG_PRINT_ERROR("%s, dev_set_buf_req failed",
641                      __FUNCTION__);
642                    RETURN(OMX_ErrorUnsupportedSetting);
643                 }
644                 memcpy(&m_sOutPortDef, portDefn,sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
645 
646                 /* update the output buffer requirement */
647                 Ret = swvenc_get_buffer_req(&m_sOutPortDef.nBufferCountMin,
648                         &m_sOutPortDef.nBufferCountActual,
649                         &m_sOutPortDef.nBufferSize,
650                         &m_sOutPortDef.nBufferAlignment,
651                         portDefn->nPortIndex);
652                 if (Ret != SWVENC_S_SUCCESS)
653                 {
654                    DEBUG_PRINT_ERROR("ERROR: %s, swvenc_get_buffer_req failed (%d)", __FUNCTION__,
655                       Ret);
656                    RETURN(OMX_ErrorUndefined);
657                 }
658 
659                 if (portDefn->nBufferCountActual > m_sOutPortDef.nBufferCountActual)
660                 {
661                    m_sOutPortDef.nBufferCountActual = portDefn->nBufferCountActual;
662                 }
663                 if (portDefn->nBufferSize > m_sOutPortDef.nBufferSize)
664                 {
665                    m_sOutPortDef.nBufferSize = portDefn->nBufferSize;
666                 }
667 
668                 DEBUG_PRINT_LOW("o/p new actual cnt = %u", m_sOutPortDef.nBufferCountActual);
669                 DEBUG_PRINT_LOW("o/p new min cnt = %u", m_sOutPortDef.nBufferCountMin);
670                 DEBUG_PRINT_LOW("o/p new buffersize = %u", m_sOutPortDef.nBufferSize);
671             }
672             else
673             {
674                 DEBUG_PRINT_ERROR("ERROR: Set_parameter: Bad Port idx %d",
675                         (int)portDefn->nPortIndex);
676                 eRet = OMX_ErrorBadPortIndex;
677             }
678             m_sConfigFramerate.xEncodeFramerate = portDefn->format.video.xFramerate;
679             m_sConfigBitrate.nEncodeBitrate = portDefn->format.video.nBitrate;
680             m_sParamBitrate.nTargetBitrate = portDefn->format.video.nBitrate;
681             break;
682         }
683 
684         case OMX_IndexParamVideoPortFormat:
685         {
686             OMX_VIDEO_PARAM_PORTFORMATTYPE *portFmt =
687                 (OMX_VIDEO_PARAM_PORTFORMATTYPE *)paramData;
688             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoPortFormat %d",
689                     portFmt->eColorFormat);
690             SWVENC_COLOR_FORMAT color_format;
691 
692             /* set the driver with the corresponding values */
693             if (PORT_INDEX_IN == portFmt->nPortIndex)
694             {
695                 if (portFmt->eColorFormat ==
696                     ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatAndroidOpaque))
697                 {
698                     /* meta_mode = 2 (kMetadataBufferTypeGrallocSource) */
699                     m_sInPortFormat.eColorFormat =
700                         (OMX_COLOR_FORMATTYPE) QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
701                     color_format = SWVENC_COLOR_FORMAT_NV12;
702                     if (!mUseProxyColorFormat)
703                     {
704                        if (!c2d_conv.init())
705                        {
706                           DEBUG_PRINT_ERROR("C2D init failed");
707                           return OMX_ErrorUnsupportedSetting;
708                        }
709                        DEBUG_PRINT_ERROR("C2D init is successful");
710                     }
711                     mUseProxyColorFormat = true;
712                     m_input_msg_id = OMX_COMPONENT_GENERATE_ETB_OPQ;
713                 }
714                 else
715                 {
716                     m_sInPortFormat.eColorFormat = portFmt->eColorFormat;
717                     if ((portFmt->eColorFormat == OMX_COLOR_FormatYUV420SemiPlanar) ||
718                         (portFmt->eColorFormat ==
719                          ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m)))
720                     {
721                         color_format = SWVENC_COLOR_FORMAT_NV12;
722                     }
723                     else if (portFmt->eColorFormat ==
724                              ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatYVU420SemiPlanar))
725                     {
726                         color_format = SWVENC_COLOR_FORMAT_NV21;
727                     }
728                     else
729                     {
730                         DEBUG_PRINT_ERROR("%s: OMX_IndexParamVideoPortFormat %d invalid",
731                                           __FUNCTION__,
732                                           portFmt->eColorFormat);
733                         RETURN(OMX_ErrorBadParameter);
734                     }
735                     m_input_msg_id = OMX_COMPONENT_GENERATE_ETB;
736                     mUseProxyColorFormat = false;
737                 }
738                 m_sInPortDef.format.video.eColorFormat = m_sInPortFormat.eColorFormat;
739                 /* set the input color format */
740                 Prop.id = SWVENC_PROPERTY_ID_COLOR_FORMAT;
741                 Prop.info.color_format = color_format;
742                 Ret = swvenc_setproperty(m_hSwVenc, &Prop);
743                 if (Ret != SWVENC_S_SUCCESS)
744                 {
745                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
746                      __FUNCTION__, Ret);
747                    RETURN(OMX_ErrorUnsupportedSetting);
748                 }
749 
750                 /* set the input frame-rate */
751                 if (portFmt->xFramerate != 0)
752                 {
753                    Ret = swvenc_set_frame_rate(portFmt->xFramerate >> 16);
754                    if (Ret != SWVENC_S_SUCCESS)
755                    {
756                       DEBUG_PRINT_ERROR("%s, swvenc_set_frame_rate failed (%d)",
757                         __FUNCTION__, Ret);
758                       //RETURN(OMX_ErrorUnsupportedSetting);
759                    }
760                    m_sInPortFormat.xFramerate = portFmt->xFramerate;
761                 }
762             }
763             break;
764         }
765 
766         case OMX_IndexParamVideoInit:
767         {
768             OMX_PORT_PARAM_TYPE* pParam = (OMX_PORT_PARAM_TYPE*)(paramData);
769             DEBUG_PRINT_LOW("Set OMX_IndexParamVideoInit called");
770             break;
771         }
772 
773         case OMX_IndexParamVideoBitrate:
774         {
775             OMX_VIDEO_PARAM_BITRATETYPE* pParam = (OMX_VIDEO_PARAM_BITRATETYPE*)paramData;
776             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoBitrate");
777 
778             if (m_max_allowed_bitrate_check)
779             {
780                //TBD: to add bitrate check
781             }
782 
783             /* set the output bit-rate */
784             Ret = swvenc_set_bit_rate(pParam->nTargetBitrate);
785             if (Ret != SWVENC_S_SUCCESS)
786             {
787                DEBUG_PRINT_ERROR("%s, swvenc_set_bit_rate failed (%d)",
788                  __FUNCTION__, Ret);
789                RETURN(OMX_ErrorUnsupportedSetting);
790             }
791 
792             /* set the RC-mode */
793             Ret = swvenc_set_rc_mode(pParam->eControlRate);
794             if (Ret != SWVENC_S_SUCCESS)
795             {
796                DEBUG_PRINT_ERROR("%s, swvenc_set_rc_mode failed (%d)",
797                  __FUNCTION__, Ret);
798                RETURN(OMX_ErrorUnsupportedSetting);
799             }
800 
801             m_sParamBitrate.nTargetBitrate = pParam->nTargetBitrate;
802             m_sParamBitrate.eControlRate = pParam->eControlRate;
803             m_sConfigBitrate.nEncodeBitrate = pParam->nTargetBitrate;
804             m_sInPortDef.format.video.nBitrate = pParam->nTargetBitrate;
805             m_sOutPortDef.format.video.nBitrate = pParam->nTargetBitrate;
806             DEBUG_PRINT_LOW("bitrate = %u", m_sOutPortDef.format.video.nBitrate);
807             break;
808         }
809 
810         case OMX_IndexParamVideoMpeg4:
811         {
812             OMX_VIDEO_PARAM_MPEG4TYPE* pParam = (OMX_VIDEO_PARAM_MPEG4TYPE*)paramData;
813 
814             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoMpeg4");
815 
816             if (pParam->nBFrames)
817             {
818                 DEBUG_PRINT_ERROR("Warning: B frames not supported");
819             }
820 
821             /* set the intra period */
822             Ret = swvenc_set_intra_period(pParam->nPFrames,pParam->nBFrames);
823             if (Ret != SWVENC_S_SUCCESS)
824             {
825                DEBUG_PRINT_ERROR("%s, swvenc_set_intra_period failed (%d)",
826                  __FUNCTION__, Ret);
827                RETURN(OMX_ErrorUnsupportedSetting);
828             }
829 
830             memcpy(&m_sParamMPEG4,pParam, sizeof(struct OMX_VIDEO_PARAM_MPEG4TYPE));
831             m_sIntraperiod.nPFrames = m_sParamMPEG4.nPFrames;
832             m_sIntraperiod.nBFrames = m_sParamMPEG4.nBFrames;
833             break;
834         }
835 
836         case OMX_IndexParamVideoH263:
837         {
838             OMX_VIDEO_PARAM_H263TYPE* pParam = (OMX_VIDEO_PARAM_H263TYPE*)paramData;
839 
840             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoH263");
841 
842             /* set the intra period */
843             Ret = swvenc_set_intra_period(pParam->nPFrames,pParam->nBFrames);
844             if (Ret != SWVENC_S_SUCCESS)
845             {
846                DEBUG_PRINT_ERROR("%s, swvenc_set_intra_period failed (%d)",
847                  __FUNCTION__, Ret);
848                RETURN(OMX_ErrorUnsupportedSetting);
849             }
850 
851             memcpy(&m_sParamH263,pParam, sizeof(struct OMX_VIDEO_PARAM_H263TYPE));
852             m_sIntraperiod.nPFrames = m_sParamH263.nPFrames;
853             m_sIntraperiod.nBFrames = m_sParamH263.nBFrames;
854             break;
855         }
856 
857         case OMX_IndexParamVideoProfileLevelCurrent:
858         {
859             OMX_VIDEO_PARAM_PROFILELEVELTYPE* pParam = (OMX_VIDEO_PARAM_PROFILELEVELTYPE*)paramData;
860 
861             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoProfileLevelCurrent");
862 
863             /* set the profile and level */
864             Ret = swvenc_set_profile_level(pParam->eProfile,pParam->eLevel);
865             if (Ret != SWVENC_S_SUCCESS)
866             {
867                DEBUG_PRINT_ERROR("%s, swvenc_set_rc_mode failed (%d)",
868                  __FUNCTION__, Ret);
869                RETURN(OMX_ErrorUnsupportedSetting);
870             }
871 
872 
873             m_sParamProfileLevel.eProfile = pParam->eProfile;
874             m_sParamProfileLevel.eLevel = pParam->eLevel;
875 
876             if (SWVENC_CODEC_MPEG4 == m_codec)
877             {
878                 m_sParamMPEG4.eProfile = (OMX_VIDEO_MPEG4PROFILETYPE)m_sParamProfileLevel.eProfile;
879                 m_sParamMPEG4.eLevel = (OMX_VIDEO_MPEG4LEVELTYPE)m_sParamProfileLevel.eLevel;
880                 DEBUG_PRINT_LOW("MPEG4 profile = %d, level = %d", m_sParamMPEG4.eProfile,
881                         m_sParamMPEG4.eLevel);
882             }
883             else if (SWVENC_CODEC_H263 == m_codec)
884             {
885                 m_sParamH263.eProfile = (OMX_VIDEO_H263PROFILETYPE)m_sParamProfileLevel.eProfile;
886                 m_sParamH263.eLevel = (OMX_VIDEO_H263LEVELTYPE)m_sParamProfileLevel.eLevel;
887                 DEBUG_PRINT_LOW("H263 profile = %d, level = %d", m_sParamH263.eProfile,
888                         m_sParamH263.eLevel);
889             }
890             break;
891         }
892 
893         case OMX_IndexParamStandardComponentRole:
894         {
895             OMX_PARAM_COMPONENTROLETYPE *comp_role;
896             comp_role = (OMX_PARAM_COMPONENTROLETYPE *) paramData;
897             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamStandardComponentRole %s",
898                     comp_role->cRole);
899 
900             if ((m_state == OMX_StateLoaded)&&
901                     !BITMASK_PRESENT(&m_flags,OMX_COMPONENT_IDLE_PENDING))
902             {
903                 DEBUG_PRINT_LOW("Set Parameter called in valid state");
904             }
905             else
906             {
907                 DEBUG_PRINT_ERROR("Set Parameter called in Invalid State");
908                 RETURN(OMX_ErrorIncorrectStateOperation);
909             }
910 
911             if (SWVENC_CODEC_MPEG4 == m_codec)
912             {
913                 if (!strncmp((const char*)comp_role->cRole,"video_encoder.mpeg4",OMX_MAX_STRINGNAME_SIZE))
914                 {
915                     strlcpy((char*)m_cRole,"video_encoder.mpeg4",OMX_MAX_STRINGNAME_SIZE);
916                 }
917                 else
918                 {
919                     DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown Index %s", comp_role->cRole);
920                     eRet = OMX_ErrorUnsupportedSetting;
921                 }
922             }
923             else if (SWVENC_CODEC_H263 == m_codec)
924             {
925                 if (!strncmp((const char*)comp_role->cRole,"video_encoder.h263",OMX_MAX_STRINGNAME_SIZE))
926                 {
927                     strlcpy((char*)m_cRole,"video_encoder.h263",OMX_MAX_STRINGNAME_SIZE);
928                 }
929                 else
930                 {
931                     DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown Index %s", comp_role->cRole);
932                     eRet =OMX_ErrorUnsupportedSetting;
933                 }
934             }
935             else
936             {
937                 DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown param %s", m_nkind);
938                 eRet = OMX_ErrorInvalidComponentName;
939             }
940             break;
941         }
942 
943         case OMX_IndexParamPriorityMgmt:
944         {
945             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPriorityMgmt");
946             if (m_state != OMX_StateLoaded) {
947                 DEBUG_PRINT_ERROR("ERROR: Set Parameter called in Invalid State");
948                 RETURN(OMX_ErrorIncorrectStateOperation);
949             }
950             OMX_PRIORITYMGMTTYPE *priorityMgmtype = (OMX_PRIORITYMGMTTYPE*) paramData;
951             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamPriorityMgmt %u",
952                     priorityMgmtype->nGroupID);
953 
954             DEBUG_PRINT_LOW("set_parameter: priorityMgmtype %u",
955                     priorityMgmtype->nGroupPriority);
956 
957             m_sPriorityMgmt.nGroupID = priorityMgmtype->nGroupID;
958             m_sPriorityMgmt.nGroupPriority = priorityMgmtype->nGroupPriority;
959 
960             break;
961         }
962 
963         case OMX_IndexParamCompBufferSupplier:
964         {
965             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamCompBufferSupplier");
966             OMX_PARAM_BUFFERSUPPLIERTYPE *bufferSupplierType = (OMX_PARAM_BUFFERSUPPLIERTYPE*) paramData;
967             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamCompBufferSupplier %d",
968                     bufferSupplierType->eBufferSupplier);
969             if ( (bufferSupplierType->nPortIndex == 0) ||
970                  (bufferSupplierType->nPortIndex ==1)
971                )
972             {
973                 m_sInBufSupplier.eBufferSupplier = bufferSupplierType->eBufferSupplier;
974             }
975             else
976             {
977                 eRet = OMX_ErrorBadPortIndex;
978             }
979 
980             break;
981 
982         }
983 
984         case OMX_IndexParamVideoQuantization:
985         {
986             // this is applicable only for RC-off case
987             DEBUG_PRINT_LOW("set_parameter: OMX_IndexParamVideoQuantization");
988             OMX_VIDEO_PARAM_QUANTIZATIONTYPE *session_qp = (OMX_VIDEO_PARAM_QUANTIZATIONTYPE*) paramData;
989             if (session_qp->nPortIndex == PORT_INDEX_OUT)
990             {
991                 Prop.id = SWVENC_PROPERTY_ID_QP;
992                 Prop.info.qp.qp_i = session_qp->nQpI;
993                 Prop.info.qp.qp_p = session_qp->nQpP;
994                 Prop.info.qp.qp_b = session_qp->nQpB;
995 
996                 Ret = swvenc_setproperty(m_hSwVenc, &Prop);
997                 if (Ret != SWVENC_S_SUCCESS)
998                 {
999                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1000                      __FUNCTION__, Ret);
1001                    RETURN(OMX_ErrorUnsupportedSetting);
1002                 }
1003 
1004                 m_sSessionQuantization.nQpI = session_qp->nQpI;
1005                 m_sSessionQuantization.nQpP = session_qp->nQpP;
1006                 m_sSessionQuantization.nQpB = session_qp->nQpB;
1007             }
1008             else
1009             {
1010                 DEBUG_PRINT_ERROR("ERROR: Unsupported port Index for Session QP setting");
1011                 eRet = OMX_ErrorBadPortIndex;
1012             }
1013             break;
1014         }
1015 
1016         case OMX_QcomIndexParamVideoQPRange:
1017         {
1018             DEBUG_PRINT_LOW("set_parameter: OMX_QcomIndexParamVideoQPRange");
1019             OMX_QCOM_VIDEO_PARAM_QPRANGETYPE *qp_range = (OMX_QCOM_VIDEO_PARAM_QPRANGETYPE*) paramData;
1020             if (qp_range->nPortIndex == PORT_INDEX_OUT)
1021             {
1022                 if ( (qp_range->minQP > 255) ||
1023                      (qp_range->maxQP > 255)
1024                    )
1025                 {
1026                    DEBUG_PRINT_ERROR("ERROR: Out of range QP");
1027                    eRet = OMX_ErrorBadParameter;
1028                 }
1029 
1030                 Prop.id = SWVENC_PROPERTY_ID_QP_RANGE;
1031                 Prop.info.qp_range.min_qp_packed =
1032                  (qp_range->minQP << 16) | (qp_range->minQP) | (qp_range->minQP << 8);
1033                 Prop.info.qp_range.max_qp_packed =
1034                  (qp_range->maxQP << 16) | (qp_range->maxQP) | (qp_range->maxQP << 8);
1035 
1036                 Ret = swvenc_setproperty(m_hSwVenc, &Prop);
1037                 if (Ret != SWVENC_S_SUCCESS)
1038                 {
1039                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1040                      __FUNCTION__, Ret);
1041                    RETURN(OMX_ErrorUnsupportedSetting);
1042                 }
1043 
1044                 m_sSessionQPRange.minQP= qp_range->minQP;
1045                 m_sSessionQPRange.maxQP= qp_range->maxQP;
1046             }
1047             else
1048             {
1049                 DEBUG_PRINT_ERROR("ERROR: Unsupported port Index for QP range setting");
1050                 eRet = OMX_ErrorBadPortIndex;
1051             }
1052             break;
1053         }
1054 
1055         case OMX_QcomIndexPortDefn:
1056         {
1057             OMX_QCOM_PARAM_PORTDEFINITIONTYPE* pParam =
1058                 (OMX_QCOM_PARAM_PORTDEFINITIONTYPE*)paramData;
1059             DEBUG_PRINT_LOW("set_parameter: OMX_QcomIndexPortDefn");
1060             if (pParam->nPortIndex == (OMX_U32)PORT_INDEX_IN)
1061             {
1062                 if (pParam->nMemRegion > OMX_QCOM_MemRegionInvalid &&
1063                         pParam->nMemRegion < OMX_QCOM_MemRegionMax)
1064                 {
1065                     m_use_input_pmem = OMX_TRUE;
1066                 }
1067                 else
1068                 {
1069                     m_use_input_pmem = OMX_FALSE;
1070                 }
1071             }
1072             else if (pParam->nPortIndex == (OMX_U32)PORT_INDEX_OUT)
1073             {
1074                 if (pParam->nMemRegion > OMX_QCOM_MemRegionInvalid &&
1075                         pParam->nMemRegion < OMX_QCOM_MemRegionMax)
1076                 {
1077                     m_use_output_pmem = OMX_TRUE;
1078                 }
1079                 else
1080                 {
1081                     m_use_output_pmem = OMX_FALSE;
1082                 }
1083             }
1084             else
1085             {
1086                 DEBUG_PRINT_ERROR("ERROR: SetParameter called on unsupported Port Index for QcomPortDefn");
1087                 RETURN(OMX_ErrorBadPortIndex);
1088             }
1089             break;
1090         }
1091 
1092         case OMX_IndexParamVideoErrorCorrection:
1093         {
1094             DEBUG_PRINT_LOW("OMX_IndexParamVideoErrorCorrection");
1095             OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE* pParam =
1096                 (OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE*)paramData;
1097 
1098             /* HEC */
1099             if (m_codec == SWVENC_CODEC_MPEG4)
1100             {
1101                Prop.id = SWVENC_PROPERTY_ID_MPEG4_HEC;
1102                Prop.info.mpeg4_hec = pParam->bEnableHEC;
1103 
1104                Ret = swvenc_setproperty(m_hSwVenc, &Prop);
1105                if (Ret != SWVENC_S_SUCCESS)
1106                {
1107                   DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1108                     __FUNCTION__, Ret);
1109                   RETURN(OMX_ErrorUndefined);
1110                }
1111 
1112                /* Data partitioning */
1113                Prop.id = SWVENC_PROPERTY_ID_MPEG4_DP;
1114                Prop.info.mpeg4_dp = pParam->bEnableDataPartitioning;
1115 
1116                Ret = swvenc_setproperty(m_hSwVenc, &Prop);
1117                if (Ret != SWVENC_S_SUCCESS)
1118                {
1119                   DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1120                     __FUNCTION__, Ret);
1121                   RETURN(OMX_ErrorUndefined);
1122                }
1123             }
1124 
1125             /* RVLC */
1126             if (pParam->bEnableRVLC)
1127             {
1128                DEBUG_PRINT_ERROR("%s, RVLC not support", __FUNCTION__);
1129             }
1130 
1131             /* Re-sync Marker */
1132             Prop.id = SWVENC_PROPERTY_ID_SLICE_CONFIG;
1133             if ( (m_codec != SWVENC_CODEC_H263) && (pParam->bEnableDataPartitioning) )
1134             {
1135                DEBUG_PRINT_ERROR("DataPartioning are not Supported for this codec");
1136                break;
1137             }
1138             if ( (m_codec != SWVENC_CODEC_H263) && (pParam->nResynchMarkerSpacing) )
1139             {
1140                Prop.info.slice_config.mode = SWVENC_SLICE_MODE_BYTE;
1141                Prop.info.slice_config.size = pParam->nResynchMarkerSpacing;
1142             }
1143             else if ( (SWVENC_CODEC_H263 == m_codec) && (pParam->bEnableResync) )
1144             {
1145                Prop.info.slice_config.mode = SWVENC_SLICE_MODE_GOB;
1146                Prop.info.slice_config.size = 0;
1147                Ret = swvenc_setproperty(m_hSwVenc, &Prop);
1148                if (Ret != SWVENC_S_SUCCESS)
1149                {
1150                   DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1151                     __FUNCTION__, Ret);
1152                   RETURN(OMX_ErrorUndefined);
1153                }
1154             }
1155             else
1156             {
1157                Prop.info.slice_config.mode = SWVENC_SLICE_MODE_OFF;
1158                Prop.info.slice_config.size = 0;
1159             }
1160 
1161             memcpy(&m_sErrorCorrection,pParam, sizeof(m_sErrorCorrection));
1162             break;
1163         }
1164 
1165         case OMX_IndexParamVideoIntraRefresh:
1166         {
1167             DEBUG_PRINT_LOW("set_param:OMX_IndexParamVideoIntraRefresh");
1168             OMX_VIDEO_PARAM_INTRAREFRESHTYPE* pParam =
1169                 (OMX_VIDEO_PARAM_INTRAREFRESHTYPE*)paramData;
1170 
1171             Ret = swvenc_set_intra_refresh(pParam);
1172             if (Ret != SWVENC_S_SUCCESS)
1173             {
1174                DEBUG_PRINT_ERROR("%s, swvenc_set_intra_refresh failed (%d)",
1175                  __FUNCTION__, Ret);
1176                RETURN(OMX_ErrorUnsupportedSetting);
1177             }
1178 
1179             memcpy(&m_sIntraRefresh, pParam, sizeof(m_sIntraRefresh));
1180             break;
1181         }
1182 
1183         case OMX_QcomIndexParamVideoMetaBufferMode:
1184         {
1185             StoreMetaDataInBuffersParams *pParam =
1186                 (StoreMetaDataInBuffersParams*)paramData;
1187             DEBUG_PRINT_HIGH("set_parameter:OMX_QcomIndexParamVideoMetaBufferMode: "
1188                     "port_index = %u, meta_mode = %d", pParam->nPortIndex, pParam->bStoreMetaData);
1189 
1190             if (pParam->nPortIndex == PORT_INDEX_IN)
1191             {
1192                 if (pParam->bStoreMetaData != meta_mode_enable)
1193                 {
1194                     meta_mode_enable = pParam->bStoreMetaData;
1195                     if (!meta_mode_enable)
1196                     {
1197                         Ret = swvenc_get_buffer_req(&m_sOutPortDef.nBufferCountMin,
1198                                  &m_sOutPortDef.nBufferCountActual,
1199                                  &m_sOutPortDef.nBufferSize,
1200                                  &m_sOutPortDef.nBufferAlignment,
1201                                  m_sOutPortDef.nPortIndex);
1202                         if (Ret != SWVENC_S_SUCCESS)
1203                         {
1204                            DEBUG_PRINT_ERROR("ERROR: %s, swvenc_get_buffer_req failed (%d)", __FUNCTION__,
1205                               Ret);
1206                            eRet = OMX_ErrorUndefined;
1207                            break;
1208                         }
1209                     }
1210                 }
1211             }
1212             else if (pParam->nPortIndex == PORT_INDEX_OUT && secure_session)
1213             {
1214                 if (pParam->bStoreMetaData != meta_mode_enable)
1215                 {
1216                     meta_mode_enable = pParam->bStoreMetaData;
1217                 }
1218             }
1219             else
1220             {
1221                 if (pParam->bStoreMetaData)
1222                 {
1223                     DEBUG_PRINT_ERROR("set_parameter: metamode is "
1224                             "valid for input port only");
1225                     eRet = OMX_ErrorUnsupportedIndex;
1226                 }
1227             }
1228         }
1229         break;
1230 
1231         case OMX_QcomIndexParamIndexExtraDataType:
1232         {
1233             DEBUG_PRINT_HIGH("set_parameter: OMX_QcomIndexParamIndexExtraDataType");
1234             QOMX_INDEXEXTRADATATYPE *pParam = (QOMX_INDEXEXTRADATATYPE *)paramData;
1235             OMX_U32 mask = 0;
1236 
1237             if (pParam->nIndex == (OMX_INDEXTYPE)OMX_ExtraDataVideoEncoderSliceInfo)
1238             {
1239                 if (pParam->nPortIndex == PORT_INDEX_OUT)
1240                 {
1241                     mask = VEN_EXTRADATA_SLICEINFO;
1242 
1243                     DEBUG_PRINT_HIGH("SliceInfo extradata %s",
1244                             ((pParam->bEnabled == OMX_TRUE) ? "enabled" : "disabled"));
1245                 }
1246                 else
1247                 {
1248                     DEBUG_PRINT_ERROR("set_parameter: Slice information is "
1249                             "valid for output port only");
1250                     eRet = OMX_ErrorUnsupportedIndex;
1251                     break;
1252                 }
1253             }
1254             else if (pParam->nIndex == (OMX_INDEXTYPE)OMX_ExtraDataVideoEncoderMBInfo)
1255             {
1256                 if (pParam->nPortIndex == PORT_INDEX_OUT)
1257                 {
1258                     mask = VEN_EXTRADATA_MBINFO;
1259 
1260                     DEBUG_PRINT_HIGH("MBInfo extradata %s",
1261                             ((pParam->bEnabled == OMX_TRUE) ? "enabled" : "disabled"));
1262                 }
1263                 else
1264                 {
1265                     DEBUG_PRINT_ERROR("set_parameter: MB information is "
1266                             "valid for output port only");
1267                     eRet = OMX_ErrorUnsupportedIndex;
1268                     break;
1269                 }
1270             }
1271             else
1272             {
1273                 DEBUG_PRINT_ERROR("set_parameter: unsupported extrdata index (%x)",
1274                         pParam->nIndex);
1275                 eRet = OMX_ErrorUnsupportedIndex;
1276                 break;
1277             }
1278 
1279 
1280             if (pParam->bEnabled == OMX_TRUE)
1281             {
1282                 m_sExtraData |= mask;
1283             }
1284             else
1285             {
1286                 m_sExtraData &= ~mask;
1287             }
1288 
1289             #if 0
1290             // TBD: add setprop to swvenc once the support is added
1291             if (handle->venc_set_param((OMX_PTR)!!(m_sExtraData & mask),
1292                         (OMX_INDEXTYPE)pParam->nIndex) != true)
1293             {
1294                 DEBUG_PRINT_ERROR("ERROR: Setting Extradata (%x) failed", pParam->nIndex);
1295                 RETURN(OMX_ErrorUnsupportedSetting);
1296             }
1297             else
1298             #endif
1299             {
1300                 m_sOutPortDef.nPortIndex = PORT_INDEX_OUT;
1301                 bResult = dev_get_buf_req(&m_sOutPortDef.nBufferCountMin,
1302                         &m_sOutPortDef.nBufferCountActual,
1303                         &m_sOutPortDef.nBufferSize,
1304                         m_sOutPortDef.nPortIndex);
1305                 if (false == bResult)
1306                 {
1307                    DEBUG_PRINT_ERROR("dev_get_buf_req failed");
1308                    eRet = OMX_ErrorUndefined;
1309                    break;
1310                 }
1311 
1312                 DEBUG_PRINT_HIGH("updated out_buf_req: buffer cnt=%u, "
1313                         "count min=%u, buffer size=%u",
1314                         m_sOutPortDef.nBufferCountActual,
1315                         m_sOutPortDef.nBufferCountMin,
1316                         m_sOutPortDef.nBufferSize);
1317             }
1318             break;
1319         }
1320 
1321         case OMX_QcomIndexParamVideoMaxAllowedBitrateCheck:
1322         {
1323             QOMX_EXTNINDEX_PARAMTYPE* pParam =
1324                 (QOMX_EXTNINDEX_PARAMTYPE*)paramData;
1325             if (pParam->nPortIndex == PORT_INDEX_OUT)
1326             {
1327                 m_max_allowed_bitrate_check =
1328                     ((pParam->bEnable == OMX_TRUE) ? true : false);
1329                 DEBUG_PRINT_HIGH("set_parameter: max allowed bitrate check %s",
1330                         ((pParam->bEnable == OMX_TRUE) ? "enabled" : "disabled"));
1331             }
1332             else
1333             {
1334                 DEBUG_PRINT_ERROR("ERROR: OMX_QcomIndexParamVideoMaxAllowedBitrateCheck "
1335                         " called on wrong port(%u)", pParam->nPortIndex);
1336                 RETURN(OMX_ErrorBadPortIndex);
1337             }
1338             break;
1339         }
1340 
1341         case OMX_QcomIndexEnableSliceDeliveryMode:
1342         {
1343             QOMX_EXTNINDEX_PARAMTYPE* pParam =
1344                 (QOMX_EXTNINDEX_PARAMTYPE*)paramData;
1345             if (pParam->nPortIndex == PORT_INDEX_OUT)
1346             {
1347                 //TBD: add setprop to swvenc once the support is added
1348                 #if 0
1349                 if (!handle->venc_set_param(paramData,
1350                             (OMX_INDEXTYPE)OMX_QcomIndexEnableSliceDeliveryMode)) {
1351                     DEBUG_PRINT_ERROR("ERROR: Request for setting slice delivery mode failed");
1352                     RETURN( OMX_ErrorUnsupportedSetting;
1353                 }
1354                 #endif
1355                 {
1356                     DEBUG_PRINT_ERROR("ERROR: Request for setting slice delivery mode failed");
1357                     RETURN(OMX_ErrorUnsupportedSetting);
1358                 }
1359             }
1360             else
1361             {
1362                 DEBUG_PRINT_ERROR("ERROR: OMX_QcomIndexEnableSliceDeliveryMode "
1363                         "called on wrong port(%u)", pParam->nPortIndex);
1364                 RETURN(OMX_ErrorBadPortIndex);
1365             }
1366             break;
1367         }
1368 
1369         case OMX_QcomIndexEnableH263PlusPType:
1370         {
1371             QOMX_EXTNINDEX_PARAMTYPE* pParam =
1372                 (QOMX_EXTNINDEX_PARAMTYPE*)paramData;
1373             DEBUG_PRINT_LOW("OMX_QcomIndexEnableH263PlusPType");
1374             if (pParam->nPortIndex == PORT_INDEX_OUT)
1375             {
1376                 DEBUG_PRINT_ERROR("ERROR: Request for setting PlusPType failed");
1377                 RETURN(OMX_ErrorUnsupportedSetting);
1378             }
1379             else
1380             {
1381                 DEBUG_PRINT_ERROR("ERROR: OMX_QcomIndexEnableH263PlusPType "
1382                         "called on wrong port(%u)", pParam->nPortIndex);
1383                 RETURN(OMX_ErrorBadPortIndex);
1384             }
1385             break;
1386         }
1387 
1388         case OMX_QcomIndexParamPeakBitrate:
1389         {
1390             DEBUG_PRINT_ERROR("ERROR: Setting peak bitrate");
1391             RETURN(OMX_ErrorUnsupportedSetting);
1392             break;
1393         }
1394 
1395         case QOMX_IndexParamVideoInitialQp:
1396         {
1397             // TBD: applicable to RC-on case only
1398             DEBUG_PRINT_ERROR("ERROR: Setting Initial QP for RC-on case");
1399             RETURN(OMX_ErrorNone);
1400             break;
1401         }
1402 
1403 
1404         case OMX_QcomIndexParamSetMVSearchrange:
1405         {
1406             DEBUG_PRINT_ERROR("ERROR: Setting Searchrange");
1407             RETURN(OMX_ErrorUnsupportedSetting);
1408             break;
1409         }
1410 
1411         default:
1412         {
1413             DEBUG_PRINT_ERROR("ERROR: Setparameter: unknown param %d", paramIndex);
1414             eRet = OMX_ErrorUnsupportedIndex;
1415             break;
1416         }
1417     }
1418 
1419     RETURN(eRet);
1420 }
1421 
set_config(OMX_IN OMX_HANDLETYPE hComp,OMX_IN OMX_INDEXTYPE configIndex,OMX_IN OMX_PTR configData)1422 OMX_ERRORTYPE  omx_venc::set_config
1423 (
1424    OMX_IN OMX_HANDLETYPE      hComp,
1425    OMX_IN OMX_INDEXTYPE configIndex,
1426    OMX_IN OMX_PTR        configData
1427 )
1428 {
1429     ENTER_FUNC();
1430 
1431     SWVENC_STATUS SwStatus;
1432 
1433     (void)hComp;
1434 
1435     if (configData == NULL)
1436     {
1437         DEBUG_PRINT_ERROR("ERROR: param is null");
1438         RETURN(OMX_ErrorBadParameter);
1439     }
1440 
1441     if (m_state == OMX_StateInvalid)
1442     {
1443         DEBUG_PRINT_ERROR("ERROR: config called in Invalid state");
1444         RETURN(OMX_ErrorIncorrectStateOperation);
1445     }
1446 
1447     switch ((int)configIndex)
1448     {
1449         case OMX_IndexConfigVideoBitrate:
1450         {
1451             OMX_VIDEO_CONFIG_BITRATETYPE* pParam =
1452                 reinterpret_cast<OMX_VIDEO_CONFIG_BITRATETYPE*>(configData);
1453             DEBUG_PRINT_HIGH("set_config(): OMX_IndexConfigVideoBitrate (%u)", pParam->nEncodeBitrate);
1454 
1455             if (pParam->nPortIndex == PORT_INDEX_OUT)
1456             {
1457                 SwStatus = swvenc_set_bit_rate(pParam->nEncodeBitrate);
1458                 if (SwStatus != SWVENC_S_SUCCESS)
1459                 {
1460                    DEBUG_PRINT_ERROR("%s, swvenc_set_bit_rate failed (%d)",
1461                      __FUNCTION__, SwStatus);
1462                    RETURN(OMX_ErrorUnsupportedSetting);
1463                 }
1464 
1465                 m_sConfigBitrate.nEncodeBitrate = pParam->nEncodeBitrate;
1466                 m_sParamBitrate.nTargetBitrate = pParam->nEncodeBitrate;
1467                 m_sOutPortDef.format.video.nBitrate = pParam->nEncodeBitrate;
1468             }
1469             else
1470             {
1471                 DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1472                 RETURN(OMX_ErrorBadPortIndex);
1473             }
1474             break;
1475         }
1476         case OMX_IndexConfigVideoFramerate:
1477         {
1478             OMX_CONFIG_FRAMERATETYPE* pParam =
1479                 reinterpret_cast<OMX_CONFIG_FRAMERATETYPE*>(configData);
1480             DEBUG_PRINT_HIGH("set_config(): OMX_IndexConfigVideoFramerate (0x%x)", pParam->xEncodeFramerate);
1481 
1482             if (pParam->nPortIndex == PORT_INDEX_OUT)
1483             {
1484                 SwStatus = swvenc_set_frame_rate(pParam->xEncodeFramerate >> 16);
1485                 if (SwStatus != SWVENC_S_SUCCESS)
1486                 {
1487                    DEBUG_PRINT_ERROR("%s, swvenc_set_frame_rate failed (%d)",
1488                      __FUNCTION__, SwStatus);
1489                    RETURN(OMX_ErrorUnsupportedSetting);
1490                 }
1491 
1492                 m_sConfigFramerate.xEncodeFramerate = pParam->xEncodeFramerate;
1493                 m_sOutPortDef.format.video.xFramerate = pParam->xEncodeFramerate;
1494                 m_sOutPortFormat.xFramerate = pParam->xEncodeFramerate;
1495             }
1496             else
1497             {
1498                 DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1499                 RETURN(OMX_ErrorBadPortIndex);
1500             }
1501             break;
1502         }
1503         case QOMX_IndexConfigVideoIntraperiod:
1504         {
1505             QOMX_VIDEO_INTRAPERIODTYPE* pParam =
1506                 reinterpret_cast<QOMX_VIDEO_INTRAPERIODTYPE*>(configData);
1507             DEBUG_PRINT_HIGH("set_config(): QOMX_IndexConfigVideoIntraperiod");
1508 
1509             if (pParam->nPortIndex == PORT_INDEX_OUT)
1510             {
1511                 if (pParam->nBFrames > 0)
1512                 {
1513                     DEBUG_PRINT_ERROR("B frames not supported");
1514                     RETURN(OMX_ErrorUnsupportedSetting);
1515                 }
1516 
1517                 DEBUG_PRINT_HIGH("Old: P/B frames = %u/%u, New: P/B frames = %u/%u",
1518                         m_sIntraperiod.nPFrames, m_sIntraperiod.nBFrames,
1519                         pParam->nPFrames, pParam->nBFrames);
1520                 if (m_sIntraperiod.nBFrames != pParam->nBFrames)
1521                 {
1522                     DEBUG_PRINT_HIGH("Dynamically changing B-frames not supported");
1523                     RETURN(OMX_ErrorUnsupportedSetting);
1524                 }
1525 
1526                 /* set the intra period */
1527                 SwStatus = swvenc_set_intra_period(pParam->nPFrames,pParam->nBFrames);
1528                 if (SwStatus != SWVENC_S_SUCCESS)
1529                 {
1530                    DEBUG_PRINT_ERROR("%s, swvenc_set_intra_period failed (%d)",
1531                      __FUNCTION__, SwStatus);
1532                    RETURN(OMX_ErrorUnsupportedSetting);
1533                 }
1534 
1535                 m_sIntraperiod.nPFrames = pParam->nPFrames;
1536                 m_sIntraperiod.nBFrames = pParam->nBFrames;
1537                 m_sIntraperiod.nIDRPeriod = pParam->nIDRPeriod;
1538 
1539                 if (m_sOutPortFormat.eCompressionFormat == OMX_VIDEO_CodingMPEG4)
1540                 {
1541                     m_sParamMPEG4.nPFrames = pParam->nPFrames;
1542                     if (m_sParamMPEG4.eProfile != OMX_VIDEO_MPEG4ProfileSimple)
1543                     {
1544                         m_sParamMPEG4.nBFrames = pParam->nBFrames;
1545                     }
1546                     else
1547                     {
1548                         m_sParamMPEG4.nBFrames = 0;
1549                     }
1550                 }
1551                 else if (m_sOutPortFormat.eCompressionFormat == OMX_VIDEO_CodingH263)
1552                 {
1553                     m_sParamH263.nPFrames = pParam->nPFrames;
1554                 }
1555             }
1556             else
1557             {
1558                 DEBUG_PRINT_ERROR("ERROR: (QOMX_IndexConfigVideoIntraperiod) Unsupported port index: %u", pParam->nPortIndex);
1559                 RETURN(OMX_ErrorBadPortIndex);
1560             }
1561 
1562             break;
1563         }
1564         case OMX_IndexConfigVideoIntraVOPRefresh:
1565         {
1566             OMX_CONFIG_INTRAREFRESHVOPTYPE* pParam =
1567                 reinterpret_cast<OMX_CONFIG_INTRAREFRESHVOPTYPE*>(configData);
1568             DEBUG_PRINT_HIGH("set_config(): OMX_IndexConfigVideoIntraVOPRefresh");
1569 
1570             if (pParam->nPortIndex == PORT_INDEX_OUT)
1571             {
1572 
1573                 SWVENC_PROPERTY Prop;
1574 
1575                 Prop.id = SWVENC_PROPERTY_ID_IFRAME_REQUEST;
1576 
1577                 SwStatus = swvenc_setproperty(m_hSwVenc, &Prop);
1578                 if (SwStatus != SWVENC_S_SUCCESS)
1579                 {
1580                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1581                      __FUNCTION__, SwStatus);
1582                    RETURN(OMX_ErrorUnsupportedSetting);
1583                 }
1584 
1585                 m_sConfigIntraRefreshVOP.IntraRefreshVOP = pParam->IntraRefreshVOP;
1586             }
1587             else
1588             {
1589                 DEBUG_PRINT_ERROR("ERROR: Unsupported port index: %u", pParam->nPortIndex);
1590                 RETURN(OMX_ErrorBadPortIndex);
1591             }
1592             break;
1593         }
1594         case OMX_IndexConfigCommonRotate:
1595         {
1596             DEBUG_PRINT_ERROR("ERROR: OMX_IndexConfigCommonRotate not supported");
1597             RETURN(OMX_ErrorUnsupportedSetting);
1598             break;
1599         }
1600         default:
1601             DEBUG_PRINT_ERROR("ERROR: unsupported index %d", (int) configIndex);
1602             RETURN(OMX_ErrorUnsupportedSetting);
1603             break;
1604     }
1605 
1606     EXIT_FUNC();
1607 
1608     RETURN(OMX_ErrorNone);
1609 }
1610 
component_deinit(OMX_IN OMX_HANDLETYPE hComp)1611 OMX_ERRORTYPE  omx_venc::component_deinit(OMX_IN OMX_HANDLETYPE hComp)
1612 {
1613     ENTER_FUNC();
1614 
1615     OMX_U32 i = 0;
1616     DEBUG_PRINT_HIGH("omx_venc(): Inside component_deinit()");
1617 
1618     (void)hComp;
1619 
1620     if (OMX_StateLoaded != m_state)
1621     {
1622         DEBUG_PRINT_ERROR("WARNING:Rxd DeInit,OMX not in LOADED state %d",
1623                 m_state);
1624     }
1625     if (m_out_mem_ptr)
1626     {
1627         DEBUG_PRINT_LOW("Freeing the Output Memory");
1628         for (i=0; i< m_sOutPortDef.nBufferCountActual; i++ )
1629         {
1630             free_output_buffer (&m_out_mem_ptr[i]);
1631         }
1632         free(m_out_mem_ptr);
1633         m_out_mem_ptr = NULL;
1634     }
1635 
1636     /* Check if the input buffers have to be cleaned up */
1637     if ( m_inp_mem_ptr && !meta_mode_enable )
1638     {
1639         DEBUG_PRINT_LOW("Freeing the Input Memory");
1640         for (i=0; i<m_sInPortDef.nBufferCountActual; i++)
1641         {
1642             free_input_buffer (&m_inp_mem_ptr[i]);
1643         }
1644 
1645         free(m_inp_mem_ptr);
1646         m_inp_mem_ptr = NULL;
1647     }
1648 
1649     /* Reset counters in msg queues */
1650     m_ftb_q.m_size=0;
1651     m_cmd_q.m_size=0;
1652     m_etb_q.m_size=0;
1653     m_ftb_q.m_read = m_ftb_q.m_write =0;
1654     m_cmd_q.m_read = m_cmd_q.m_write =0;
1655     m_etb_q.m_read = m_etb_q.m_write =0;
1656 
1657     /* Clear the strong reference */
1658     DEBUG_PRINT_HIGH("Calling swvenc_deinit()");
1659     swvenc_deinit(m_hSwVenc);
1660 
1661     DEBUG_PRINT_HIGH("OMX_Venc:Component Deinit");
1662 
1663     RETURN(OMX_ErrorNone);
1664 }
1665 
dev_stop(void)1666 OMX_U32 omx_venc::dev_stop(void)
1667 {
1668     ENTER_FUNC();
1669 
1670     SWVENC_STATUS Ret;
1671 
1672     if (false == m_stopped)
1673     {
1674        Ret = swvenc_stop(m_hSwVenc);
1675        if (Ret != SWVENC_S_SUCCESS)
1676        {
1677           DEBUG_PRINT_ERROR("%s, swvenc_stop failed (%d)",
1678             __FUNCTION__, Ret);
1679           RETURN(-1);
1680        }
1681        set_format = false;
1682        m_stopped = true;
1683 
1684        /* post STOP_DONE event as start is synchronus */
1685        post_event (0, OMX_ErrorNone, OMX_COMPONENT_GENERATE_STOP_DONE);
1686     }
1687 
1688     RETURN(0);
1689 }
1690 
dev_pause(void)1691 OMX_U32 omx_venc::dev_pause(void)
1692 {
1693     ENTER_FUNC();
1694     // nothing to be done for sw encoder
1695 
1696     RETURN(true);
1697 }
1698 
dev_resume(void)1699 OMX_U32 omx_venc::dev_resume(void)
1700 {
1701     ENTER_FUNC();
1702     // nothing to be done for sw encoder
1703 
1704     RETURN(true);
1705 }
1706 
dev_start(void)1707 OMX_U32 omx_venc::dev_start(void)
1708 {
1709    ENTER_FUNC();
1710    SWVENC_STATUS Ret;
1711    Ret = swvenc_start(m_hSwVenc);
1712    if (Ret != SWVENC_S_SUCCESS)
1713    {
1714       DEBUG_PRINT_ERROR("%s, swvenc_start failed (%d)",
1715         __FUNCTION__, Ret);
1716       RETURN(-1);
1717    }
1718 
1719    m_stopped = false;
1720 
1721    RETURN(0);
1722 }
1723 
dev_flush(unsigned port)1724 OMX_U32 omx_venc::dev_flush(unsigned port)
1725 {
1726    ENTER_FUNC();
1727    SWVENC_STATUS Ret;
1728 
1729    (void)port;
1730    Ret = swvenc_flush(m_hSwVenc);
1731    if (Ret != SWVENC_S_SUCCESS)
1732    {
1733       DEBUG_PRINT_ERROR("%s, swvenc_flush failed (%d)",
1734         __FUNCTION__, Ret);
1735       RETURN(-1);
1736    }
1737 
1738    RETURN(0);
1739 }
1740 
dev_start_done(void)1741 OMX_U32 omx_venc::dev_start_done(void)
1742 {
1743    ENTER_FUNC();
1744 
1745    /* post START_DONE event as start is synchronus */
1746    post_event (0, OMX_ErrorNone, OMX_COMPONENT_GENERATE_START_DONE);
1747 
1748    RETURN(0);
1749 }
1750 
dev_set_message_thread_id(pthread_t tid)1751 OMX_U32 omx_venc::dev_set_message_thread_id(pthread_t tid)
1752 {
1753     ENTER_FUNC();
1754 
1755     // nothing to be done for sw encoder
1756     (void)tid;
1757 
1758     RETURN(true);
1759 }
1760 
dev_use_buf(void * buf_addr,unsigned port,unsigned index)1761 bool omx_venc::dev_use_buf(void *buf_addr,unsigned port,unsigned index)
1762 {
1763     ENTER_FUNC();
1764 
1765     (void)buf_addr;
1766     (void)port;
1767     (void)index;
1768 
1769     RETURN(true);
1770 }
1771 
dev_free_buf(void * buf_addr,unsigned port)1772 bool omx_venc::dev_free_buf(void *buf_addr,unsigned port)
1773 {
1774     ENTER_FUNC();
1775 
1776     (void)buf_addr;
1777     (void)port;
1778 
1779     RETURN(true);
1780 }
1781 
dev_empty_buf(void * buffer,void * pmem_data_buf,unsigned index,unsigned fd)1782 bool omx_venc::dev_empty_buf
1783 (
1784     void *buffer,
1785     void *pmem_data_buf,
1786     unsigned index,
1787     unsigned fd
1788 )
1789 {
1790     ENTER_FUNC();
1791 
1792     SWVENC_STATUS Ret;
1793     SWVENC_IPBUFFER ipbuffer;
1794     OMX_BUFFERHEADERTYPE *bufhdr = (OMX_BUFFERHEADERTYPE *)buffer;
1795     unsigned int size = 0, filled_length, offset = 0;
1796     SWVENC_COLOR_FORMAT color_format;
1797     SWVENC_PROPERTY prop;
1798 
1799     (void)pmem_data_buf;
1800     (void)index;
1801 
1802     if (meta_mode_enable)
1803     {
1804         LEGACY_CAM_METADATA_TYPE *meta_buf = NULL;
1805         meta_buf = (LEGACY_CAM_METADATA_TYPE *)bufhdr->pBuffer;
1806         if(m_sInPortDef.format.video.eColorFormat == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatAndroidOpaque))
1807         {
1808             DEBUG_PRINT_LOW("dev_empty_buf: color_format is QOMX_COLOR_FormatAndroidOpaque");
1809             set_format = true;
1810         }
1811         if(!meta_buf)
1812         {
1813             if (!bufhdr->nFilledLen && (bufhdr->nFlags & OMX_BUFFERFLAG_EOS))
1814             {
1815                 ipbuffer.p_buffer= bufhdr->pBuffer;
1816                 ipbuffer.size = bufhdr->nAllocLen;
1817                 ipbuffer.filled_length = bufhdr->nFilledLen;
1818                 DEBUG_PRINT_LOW("dev_empty_buf: empty EOS buffer");
1819             }
1820             else
1821             {
1822                 return false;
1823             }
1824         }
1825         else
1826         {
1827             if (meta_buf->buffer_type == LEGACY_CAM_SOURCE)
1828             {
1829                 offset = meta_buf->meta_handle->data[1];
1830                 size = meta_buf->meta_handle->data[2];
1831                 if (set_format && (meta_buf->meta_handle->numFds + meta_buf->meta_handle->numInts > 5))
1832                 {
1833                     m_sInPortFormat.eColorFormat = (OMX_COLOR_FORMATTYPE)meta_buf->meta_handle->data[5];
1834                 }
1835                 ipbuffer.p_buffer = (unsigned char *)mmap(NULL, size, PROT_READ|PROT_WRITE,MAP_SHARED, fd, offset);
1836                 if (ipbuffer.p_buffer == MAP_FAILED)
1837                 {
1838                     DEBUG_PRINT_ERROR("mmap() failed for fd %d of size %d",fd,size);
1839                     RETURN(OMX_ErrorBadParameter);
1840                 }
1841                 ipbuffer.size = size;
1842                 ipbuffer.filled_length = size;
1843             }
1844             else if (meta_buf->buffer_type == kMetadataBufferTypeGrallocSource)
1845             {
1846                 VideoGrallocMetadata *meta_buf = (VideoGrallocMetadata *)bufhdr->pBuffer;
1847                 private_handle_t *handle = (private_handle_t *)meta_buf->pHandle;
1848                 size = handle->size;
1849                 if(set_format)
1850                 {
1851                     DEBUG_PRINT_LOW("color format = 0x%x",handle->format);
1852                     if (((OMX_COLOR_FORMATTYPE)handle->format) != m_sInPortFormat.eColorFormat)
1853                     {
1854                         if(handle->format == HAL_PIXEL_FORMAT_NV12_ENCODEABLE)
1855                         {
1856                             m_sInPortFormat.eColorFormat = (OMX_COLOR_FORMATTYPE)
1857                                 QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
1858                         }
1859                         else
1860                         {
1861                             DEBUG_PRINT_ERROR("%s: OMX_IndexParamVideoPortFormat 0x%x invalid",
1862                                               __FUNCTION__,handle->format);
1863                             RETURN(OMX_ErrorBadParameter);
1864                         }
1865                     }
1866                 }
1867                 ipbuffer.p_buffer = (unsigned char *)mmap(NULL, size, PROT_READ|PROT_WRITE,MAP_SHARED, fd, offset);
1868                 if (ipbuffer.p_buffer == MAP_FAILED)
1869                 {
1870                     DEBUG_PRINT_ERROR("mmap() failed for fd %d of size %d",fd,size);
1871                     RETURN(OMX_ErrorBadParameter);
1872                 }
1873                 ipbuffer.size = size;
1874                 ipbuffer.filled_length = size;
1875             }
1876             else
1877             {
1878                 //handles the use case for surface encode
1879                 ipbuffer.p_buffer = bufhdr->pBuffer;
1880                 ipbuffer.size = bufhdr->nAllocLen;
1881                 ipbuffer.filled_length = bufhdr->nFilledLen;
1882             }
1883             if (set_format)
1884             {
1885                 set_format = false;
1886                 m_sInPortDef.format.video.eColorFormat = m_sInPortFormat.eColorFormat;
1887                 Ret = swvenc_set_color_format(m_sInPortFormat.eColorFormat);
1888                 if (Ret != SWVENC_S_SUCCESS)
1889                 {
1890                     DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1891                         __FUNCTION__, Ret);
1892                     RETURN(OMX_ErrorUnsupportedSetting);
1893                 }
1894             }
1895         }
1896     }
1897     else
1898     {
1899         ipbuffer.p_buffer = bufhdr->pBuffer;
1900         ipbuffer.size = bufhdr->nAllocLen;
1901         ipbuffer.filled_length = bufhdr->nFilledLen;
1902     }
1903     ipbuffer.flags = 0;
1904     if (bufhdr->nFlags & OMX_BUFFERFLAG_EOS)
1905     {
1906       ipbuffer.flags |= SWVENC_FLAG_EOS;
1907     }
1908     ipbuffer.timestamp = bufhdr->nTimeStamp;
1909     ipbuffer.p_client_data = (unsigned char *)bufhdr;
1910 
1911     DEBUG_PRINT_LOW("ETB: p_buffer (%p) size (%d) filled_len (%d) flags (0x%X) timestamp (%lld) clientData (%p)",
1912       ipbuffer.p_buffer,
1913       ipbuffer.size,
1914       ipbuffer.filled_length,
1915       (unsigned int)ipbuffer.flags,
1916       ipbuffer.timestamp,
1917       ipbuffer.p_client_data);
1918 
1919     Ret = swvenc_emptythisbuffer(m_hSwVenc, &ipbuffer);
1920     if (Ret != SWVENC_S_SUCCESS)
1921     {
1922        DEBUG_PRINT_ERROR("%s, swvenc_emptythisbuffer failed (%d)",
1923          __FUNCTION__, Ret);
1924        RETURN(false);
1925     }
1926 
1927     if (m_debug.in_buffer_log)
1928     {
1929        swvenc_input_log_buffers((const char*)ipbuffer.p_buffer, ipbuffer.filled_length);
1930     }
1931 
1932     RETURN(true);
1933 }
1934 
dev_fill_buf(void * buffer,void * pmem_data_buf,unsigned index,unsigned fd)1935 bool omx_venc::dev_fill_buf
1936 (
1937     void *buffer,
1938     void *pmem_data_buf,
1939     unsigned index,
1940     unsigned fd
1941 )
1942 {
1943     ENTER_FUNC();
1944 
1945     SWVENC_STATUS Ret;
1946 
1947     SWVENC_OPBUFFER opbuffer;
1948     OMX_BUFFERHEADERTYPE *bufhdr = (OMX_BUFFERHEADERTYPE *)buffer;
1949 
1950     (void)pmem_data_buf;
1951     (void)index;
1952     (void)fd;
1953 
1954     opbuffer.p_buffer = bufhdr->pBuffer;
1955     opbuffer.size = bufhdr->nAllocLen;
1956     opbuffer.filled_length = bufhdr->nFilledLen;
1957     opbuffer.flags = bufhdr->nFlags;
1958     opbuffer.p_client_data = (unsigned char *)bufhdr;
1959 
1960     DEBUG_PRINT_LOW("FTB: p_buffer (%p) size (%d) filled_len (%d) flags (0x%X) timestamp (%lld) clientData (%p)",
1961       opbuffer.p_buffer,
1962       opbuffer.size,
1963       opbuffer.filled_length,
1964       opbuffer.flags,
1965       opbuffer.timestamp,
1966       opbuffer.p_client_data);
1967 
1968     if ( false == m_bSeqHdrRequested)
1969     {
1970       if (dev_get_seq_hdr(opbuffer.p_buffer, opbuffer.size, &opbuffer.filled_length) == 0)
1971       {
1972          bufhdr->nFilledLen = opbuffer.filled_length;
1973          bufhdr->nOffset = 0;
1974          bufhdr->nTimeStamp = 0;
1975          bufhdr->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1976 
1977          DEBUG_PRINT_LOW("sending FBD with codec config");
1978          m_bSeqHdrRequested = true;
1979          post_event ((unsigned long)bufhdr,0,OMX_COMPONENT_GENERATE_FBD);
1980       }
1981       else
1982       {
1983          DEBUG_PRINT_ERROR("ERROR: couldn't get sequence header");
1984          post_event(OMX_EventError,OMX_ErrorUndefined,OMX_COMPONENT_GENERATE_EVENT);
1985       }
1986     }
1987     else
1988     {
1989        Ret = swvenc_fillthisbuffer(m_hSwVenc, &opbuffer);
1990        if (Ret != SWVENC_S_SUCCESS)
1991        {
1992           DEBUG_PRINT_ERROR("%s, swvenc_fillthisbuffer failed (%d)",
1993             __FUNCTION__, Ret);
1994           RETURN(false);
1995        }
1996     }
1997 
1998     RETURN(true);
1999 }
2000 
dev_get_seq_hdr(void * buffer,unsigned size,unsigned * hdrlen)2001 bool omx_venc::dev_get_seq_hdr
2002 (
2003    void *buffer,
2004    unsigned size,
2005    unsigned *hdrlen
2006 )
2007 {
2008    ENTER_FUNC();
2009 
2010    SWVENC_STATUS Ret;
2011    SWVENC_OPBUFFER Buffer;
2012 
2013    Buffer.p_buffer = (unsigned char*) buffer;
2014    Buffer.size = size;
2015 
2016    Ret = swvenc_getsequenceheader(m_hSwVenc, &Buffer);
2017    if (Ret != SWVENC_S_SUCCESS)
2018    {
2019       DEBUG_PRINT_ERROR("%s, swvenc_flush failed (%d)",
2020         __FUNCTION__, Ret);
2021       RETURN(-1);
2022    }
2023 
2024    *hdrlen = Buffer.filled_length;
2025 
2026    RETURN(0);
2027 }
2028 
dev_get_capability_ltrcount(OMX_U32 * min,OMX_U32 * max,OMX_U32 * step_size)2029 bool omx_venc::dev_get_capability_ltrcount
2030 (
2031    OMX_U32 *min,
2032    OMX_U32 *max,
2033    OMX_U32 *step_size
2034 )
2035 {
2036     ENTER_FUNC();
2037 
2038     (void)min;
2039     (void)max;
2040     (void)step_size;
2041 
2042     DEBUG_PRINT_ERROR("Get Capability LTR Count is not supported");
2043 
2044     RETURN(false);
2045 }
2046 
dev_get_performance_level(OMX_U32 * perflevel)2047 bool omx_venc::dev_get_performance_level(OMX_U32 *perflevel)
2048 {
2049     ENTER_FUNC();
2050 
2051     (void)perflevel;
2052     DEBUG_PRINT_ERROR("Get performance level is not supported");
2053 
2054     RETURN(false);
2055 }
2056 
dev_get_vui_timing_info(OMX_U32 * enabled)2057 bool omx_venc::dev_get_vui_timing_info(OMX_U32 *enabled)
2058 {
2059     ENTER_FUNC();
2060 
2061     (void)enabled;
2062     DEBUG_PRINT_ERROR("Get vui timing information is not supported");
2063 
2064     RETURN(false);
2065 }
2066 
dev_get_vqzip_sei_info(OMX_U32 * enabled)2067 bool omx_venc::dev_get_vqzip_sei_info(OMX_U32 *enabled)
2068 {
2069     ENTER_FUNC();
2070 
2071     (void)enabled;
2072     DEBUG_PRINT_ERROR("Get vqzip sei info is not supported");
2073 
2074     RETURN(false);
2075 }
2076 
dev_get_peak_bitrate(OMX_U32 * peakbitrate)2077 bool omx_venc::dev_get_peak_bitrate(OMX_U32 *peakbitrate)
2078 {
2079     //TBD: store the peak bitrate in class and return here;
2080     ENTER_FUNC();
2081 
2082     (void)peakbitrate;
2083     DEBUG_PRINT_ERROR("Get peak bitrate is not supported");
2084 
2085     RETURN(false);
2086 }
2087 
dev_get_batch_size(OMX_U32 * size)2088 bool omx_venc::dev_get_batch_size(OMX_U32 *size)
2089 {
2090     ENTER_FUNC();
2091 
2092     (void)size;
2093 
2094     DEBUG_PRINT_ERROR("Get batch size is not supported");
2095 
2096     RETURN(false);
2097 }
2098 
dev_loaded_start()2099 bool omx_venc::dev_loaded_start()
2100 {
2101    ENTER_FUNC();
2102    RETURN(true);
2103 }
2104 
dev_loaded_stop()2105 bool omx_venc::dev_loaded_stop()
2106 {
2107    ENTER_FUNC();
2108    RETURN(true);
2109 }
2110 
dev_loaded_start_done()2111 bool omx_venc::dev_loaded_start_done()
2112 {
2113    ENTER_FUNC();
2114    RETURN(true);
2115 }
2116 
dev_loaded_stop_done()2117 bool omx_venc::dev_loaded_stop_done()
2118 {
2119    ENTER_FUNC();
2120    RETURN(true);
2121 }
2122 
dev_get_buf_req(OMX_U32 * min_buff_count,OMX_U32 * actual_buff_count,OMX_U32 * buff_size,OMX_U32 port)2123 bool omx_venc::dev_get_buf_req(OMX_U32 *min_buff_count,
2124         OMX_U32 *actual_buff_count,
2125         OMX_U32 *buff_size,
2126         OMX_U32 port)
2127 {
2128    ENTER_FUNC();
2129 
2130    bool bRet = true;
2131    OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2132 
2133    if (PORT_INDEX_IN == port)
2134    {
2135      PortDef = &m_sInPortDef;
2136    }
2137    else if (PORT_INDEX_OUT == port)
2138    {
2139      PortDef = &m_sOutPortDef;
2140    }
2141    else
2142    {
2143      DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2144      bRet = false;
2145    }
2146 
2147    if (true == bRet)
2148    {
2149       *min_buff_count = PortDef->nBufferCountMin;
2150       *actual_buff_count = PortDef->nBufferCountActual;
2151       *buff_size = PortDef->nBufferSize;
2152    }
2153 
2154    RETURN(true);
2155 }
2156 
dev_set_buf_req(OMX_U32 const * min_buff_count,OMX_U32 const * actual_buff_count,OMX_U32 const * buff_size,OMX_U32 port)2157 bool omx_venc::dev_set_buf_req
2158 (
2159    OMX_U32 const *min_buff_count,
2160    OMX_U32 const *actual_buff_count,
2161    OMX_U32 const *buff_size,
2162    OMX_U32 port
2163 )
2164 {
2165    ENTER_FUNC();
2166 
2167    SWVENC_STATUS Ret;
2168    OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2169 
2170    (void)min_buff_count;
2171    if (PORT_INDEX_IN == port)
2172    {
2173      PortDef = &m_sInPortDef;
2174    }
2175    else if (PORT_INDEX_OUT == port)
2176    {
2177      PortDef = &m_sOutPortDef;
2178    }
2179    else
2180    {
2181      DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2182      RETURN(false);
2183    }
2184 
2185    if (*actual_buff_count < PortDef->nBufferCountMin)
2186    {
2187       DEBUG_PRINT_ERROR("ERROR: %s, (actual,min) buffer count (%d, %d)",
2188          __FUNCTION__, *actual_buff_count, PortDef->nBufferCountMin);
2189       RETURN(false);
2190    }
2191    if (false == meta_mode_enable)
2192    {
2193       if (*buff_size < PortDef->nBufferSize)
2194       {
2195           DEBUG_PRINT_ERROR("ERROR: %s, (new,old) buffer count (%d, %d)",
2196              __FUNCTION__, *actual_buff_count, PortDef->nBufferCountMin);
2197           RETURN(false);
2198       }
2199    }
2200 
2201    RETURN(true);
2202 }
2203 
dev_is_video_session_supported(OMX_U32 width,OMX_U32 height)2204 bool omx_venc::dev_is_video_session_supported(OMX_U32 width, OMX_U32 height)
2205 {
2206    ENTER_FUNC();
2207 
2208    if ( (width * height < m_capability.min_width *  m_capability.min_height) ||
2209         (width * height > m_capability.max_width *  m_capability.max_height)
2210       )
2211    {
2212        DEBUG_PRINT_ERROR(
2213          "Unsupported Resolution WxH = (%u)x(%u) Supported Range = min (%d)x(%d) - max (%d)x(%d)",
2214          width, height,
2215          m_capability.min_width, m_capability.min_height,
2216          m_capability.max_width, m_capability.max_height);
2217        RETURN(false);
2218    }
2219 
2220    RETURN(true);
2221 }
2222 
dev_buffer_ready_to_queue(OMX_BUFFERHEADERTYPE * buffer)2223 bool omx_venc::dev_buffer_ready_to_queue(OMX_BUFFERHEADERTYPE *buffer)
2224 {
2225    ENTER_FUNC();
2226 
2227    (void)buffer;
2228    RETURN(true);
2229 }
dev_handle_output_extradata(void * buffer,int fd)2230 int omx_venc::dev_handle_output_extradata(void *buffer, int fd)
2231 {
2232    ENTER_FUNC();
2233 
2234    (void)buffer;
2235    (void)fd;
2236 
2237    RETURN(true);
2238 }
2239 
dev_handle_input_extradata(void * buffer,int fd,int index)2240 int omx_venc::dev_handle_input_extradata(void *buffer, int fd, int index)
2241 {
2242    ENTER_FUNC();
2243 
2244    (void)buffer;
2245    (void)fd;
2246    (void)index;
2247 
2248    RETURN(true);
2249 }
2250 
dev_set_extradata_cookie(void * buffer)2251 void omx_venc::dev_set_extradata_cookie(void *buffer)
2252 {
2253    ENTER_FUNC();
2254 
2255    (void)buffer;
2256 }
2257 
dev_set_format(int color)2258 int omx_venc::dev_set_format(int color)
2259 {
2260    ENTER_FUNC();
2261 
2262    (void)color;
2263 
2264    RETURN(true);
2265     //return handle->venc_set_format(color);
2266 }
2267 
dev_color_align(OMX_BUFFERHEADERTYPE * buffer,OMX_U32 width,OMX_U32 height)2268 bool omx_venc::dev_color_align(OMX_BUFFERHEADERTYPE *buffer,
2269                 OMX_U32 width, OMX_U32 height)
2270 {
2271     ENTER_FUNC();
2272 
2273     if(secure_session) {
2274         DEBUG_PRINT_ERROR("Cannot align colors in secure session.");
2275         RETURN(OMX_FALSE);
2276     }
2277     return swvenc_color_align(buffer, width,height);
2278 }
2279 
is_secure_session()2280 bool omx_venc::is_secure_session()
2281 {
2282     ENTER_FUNC();
2283 
2284     RETURN(secure_session);
2285 }
2286 
dev_get_output_log_flag()2287 bool omx_venc::dev_get_output_log_flag()
2288 {
2289     ENTER_FUNC();
2290 
2291     RETURN(m_debug.out_buffer_log == 1);
2292 }
2293 
dev_output_log_buffers(const char * buffer,int bufferlen)2294 int omx_venc::dev_output_log_buffers(const char *buffer, int bufferlen)
2295 {
2296     ENTER_FUNC();
2297 
2298     if (m_debug.out_buffer_log && !m_debug.outfile)
2299     {
2300         int size = 0;
2301         int width = m_sInPortDef.format.video.nFrameWidth;
2302         int height = m_sInPortDef.format.video.nFrameHeight;
2303         if(SWVENC_CODEC_MPEG4 == m_codec)
2304         {
2305            size = snprintf(m_debug.outfile_name, PROPERTY_VALUE_MAX,
2306               "%s/output_enc_%d_%d_%p.m4v",
2307               m_debug.log_loc, width, height, this);
2308         }
2309         else if(SWVENC_CODEC_H263 == m_codec)
2310         {
2311            size = snprintf(m_debug.outfile_name, PROPERTY_VALUE_MAX,
2312               "%s/output_enc_%d_%d_%p.263",
2313               m_debug.log_loc, width, height, this);
2314         }
2315         if ((size > PROPERTY_VALUE_MAX) || (size < 0))
2316         {
2317            DEBUG_PRINT_ERROR("Failed to open output file: %s for logging as size:%d",
2318                               m_debug.outfile_name, size);
2319            RETURN(-1);
2320         }
2321         DEBUG_PRINT_LOW("output filename = %s", m_debug.outfile_name);
2322         m_debug.outfile = fopen(m_debug.outfile_name, "ab");
2323         if (!m_debug.outfile)
2324         {
2325            DEBUG_PRINT_ERROR("Failed to open output file: %s for logging errno:%d",
2326                              m_debug.outfile_name, errno);
2327            m_debug.outfile_name[0] = '\0';
2328            RETURN(-1);
2329         }
2330     }
2331     if (m_debug.outfile && buffer && bufferlen)
2332     {
2333         DEBUG_PRINT_LOW("%s buffer length: %d", __func__, bufferlen);
2334         fwrite(buffer, bufferlen, 1, m_debug.outfile);
2335     }
2336 
2337     RETURN(0);
2338 }
2339 
swvenc_input_log_buffers(const char * buffer,int bufferlen)2340 int omx_venc::swvenc_input_log_buffers(const char *buffer, int bufferlen)
2341 {
2342    int width = m_sInPortDef.format.video.nFrameWidth;
2343    int height = m_sInPortDef.format.video.nFrameHeight;
2344    int stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
2345    int scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
2346    char *temp = (char*)buffer;
2347 
2348    if (!m_debug.infile)
2349    {
2350        int size = snprintf(m_debug.infile_name, PROPERTY_VALUE_MAX,
2351                       "%s/input_enc_%d_%d_%p.yuv",
2352                       m_debug.log_loc, width, height, this);
2353        if ((size > PROPERTY_VALUE_MAX) || (size < 0))
2354        {
2355            DEBUG_PRINT_ERROR("Failed to open input file: %s for logging size:%d",
2356                               m_debug.infile_name, size);
2357            RETURN(-1);
2358        }
2359        DEBUG_PRINT_LOW("input filename = %s", m_debug.infile_name);
2360        m_debug.infile = fopen (m_debug.infile_name, "ab");
2361        if (!m_debug.infile)
2362        {
2363            DEBUG_PRINT_HIGH("Failed to open input file: %s for logging",
2364               m_debug.infile_name);
2365            m_debug.infile_name[0] = '\0';
2366            RETURN(-1);
2367        }
2368    }
2369    if (m_debug.infile && buffer && bufferlen)
2370    {
2371        DEBUG_PRINT_LOW("%s buffer length: %d", __func__, bufferlen);
2372        for (int i = 0; i < height; i++)
2373        {
2374           fwrite(temp, width, 1, m_debug.infile);
2375           temp += stride;
2376        }
2377        temp = (char*)(buffer + (stride * scanlines));
2378        for(int i = 0; i < height/2; i++)
2379        {
2380           fwrite(temp, width, 1, m_debug.infile);
2381           temp += stride;
2382       }
2383    }
2384 
2385    RETURN(0);
2386 }
2387 
dev_extradata_log_buffers(char * buffer)2388 int omx_venc::dev_extradata_log_buffers(char *buffer)
2389 {
2390    ENTER_FUNC();
2391 
2392    (void)buffer;
2393 
2394    RETURN(true);
2395     //return handle->venc_extradata_log_buffers(buffer);
2396 }
2397 
swvenc_get_buffer_req(OMX_U32 * min_buff_count,OMX_U32 * actual_buff_count,OMX_U32 * buff_size,OMX_U32 * buff_alignment,OMX_U32 port)2398 SWVENC_STATUS omx_venc::swvenc_get_buffer_req
2399 (
2400    OMX_U32 *min_buff_count,
2401    OMX_U32 *actual_buff_count,
2402    OMX_U32 *buff_size,
2403    OMX_U32 *buff_alignment,
2404    OMX_U32 port
2405 )
2406 {
2407     ENTER_FUNC();
2408 
2409     SWVENC_PROPERTY Prop;
2410     SWVENC_STATUS Ret;
2411     OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2412 
2413     Prop.id = SWVENC_PROPERTY_ID_BUFFER_REQ;
2414     if (PORT_INDEX_IN == port)
2415     {
2416       Prop.info.buffer_req.type = SWVENC_BUFFER_INPUT;
2417     }
2418     else if (PORT_INDEX_OUT == port)
2419     {
2420       Prop.info.buffer_req.type = SWVENC_BUFFER_OUTPUT;
2421     }
2422     else
2423     {
2424       DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2425       RETURN(SWVENC_S_INVALID_PARAMETERS);
2426     }
2427 
2428     Ret = swvenc_getproperty(m_hSwVenc, &Prop);
2429     if (Ret != SWVENC_S_SUCCESS)
2430     {
2431        DEBUG_PRINT_ERROR("ERROR: %s, swvenc_setproperty failed (%d)", __FUNCTION__,
2432           Ret);
2433        RETURN(SWVENC_S_INVALID_PARAMETERS);
2434     }
2435 
2436     *buff_size = Prop.info.buffer_req.size;
2437     *min_buff_count = Prop.info.buffer_req.mincount;
2438     *actual_buff_count = Prop.info.buffer_req.mincount;
2439     *buff_alignment = Prop.info.buffer_req.alignment;
2440 
2441     RETURN(Ret);
2442 }
2443 
swvenc_empty_buffer_done_cb(SWVENC_HANDLE swvenc,SWVENC_IPBUFFER * p_ipbuffer,void * p_client)2444 SWVENC_STATUS omx_venc::swvenc_empty_buffer_done_cb
2445 (
2446     SWVENC_HANDLE    swvenc,
2447     SWVENC_IPBUFFER *p_ipbuffer,
2448     void            *p_client
2449 )
2450 {
2451     ENTER_FUNC();
2452 
2453     (void)swvenc;
2454     SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2455     omx_venc *omx = reinterpret_cast<omx_venc*>(p_client);
2456 
2457     if (p_ipbuffer == NULL)
2458     {
2459         eRet = SWVENC_S_FAILURE;
2460     }
2461     else
2462     {
2463         omx->swvenc_empty_buffer_done(p_ipbuffer);
2464     }
2465     return eRet;
2466 }
2467 
swvenc_empty_buffer_done(SWVENC_IPBUFFER * p_ipbuffer)2468 SWVENC_STATUS omx_venc::swvenc_empty_buffer_done
2469 (
2470     SWVENC_IPBUFFER *p_ipbuffer
2471 )
2472 {
2473     SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2474     OMX_ERRORTYPE error = OMX_ErrorNone;
2475     OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2476 
2477     //omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2478     omxhdr = (OMX_BUFFERHEADERTYPE*)p_ipbuffer->p_client_data;
2479 
2480     DEBUG_PRINT_LOW("EBD: clientData (%p)", p_ipbuffer->p_client_data);
2481 
2482     if ( (omxhdr == NULL) ||
2483          ( ((OMX_U32)(omxhdr - m_inp_mem_ptr) >m_sInPortDef.nBufferCountActual) &&
2484            ((OMX_U32)(omxhdr - meta_buffer_hdr) >m_sInPortDef.nBufferCountActual)
2485          )
2486        )
2487     {
2488         omxhdr = NULL;
2489         error = OMX_ErrorUndefined;
2490     }
2491 
2492     if (omxhdr != NULL)
2493     {
2494         // unmap the input buffer->pBuffer
2495         omx_release_meta_buffer(omxhdr);
2496 #ifdef _ANDROID_ICS_
2497         if (meta_mode_enable)
2498         {
2499            LEGACY_CAM_METADATA_TYPE *meta_buf = NULL;
2500            unsigned int size = 0;
2501            meta_buf = (LEGACY_CAM_METADATA_TYPE *)omxhdr->pBuffer;
2502            if (meta_buf)
2503            {
2504               if (meta_buf->buffer_type == LEGACY_CAM_SOURCE)
2505               {
2506                   size = meta_buf->meta_handle->data[2];
2507               }
2508               else if (meta_buf->buffer_type == kMetadataBufferTypeGrallocSource)
2509               {
2510                   VideoGrallocMetadata *meta_buf = (VideoGrallocMetadata *)omxhdr->pBuffer;
2511                   private_handle_t *handle = (private_handle_t *)meta_buf->pHandle;
2512                   size = handle->size;
2513               }
2514            }
2515            int status = munmap(p_ipbuffer->p_buffer, size);
2516            DEBUG_PRINT_HIGH("Unmapped pBuffer <%p> size <%d> status <%d>", p_ipbuffer->p_buffer, size, status);
2517         }
2518 #endif
2519         post_event ((unsigned long)omxhdr,error,OMX_COMPONENT_GENERATE_EBD);
2520     }
2521 
2522     RETURN(eRet);
2523 }
2524 
swvenc_fill_buffer_done_cb(SWVENC_HANDLE swvenc,SWVENC_OPBUFFER * p_opbuffer,void * p_client)2525 SWVENC_STATUS omx_venc::swvenc_fill_buffer_done_cb
2526 (
2527     SWVENC_HANDLE    swvenc,
2528     SWVENC_OPBUFFER *p_opbuffer,
2529     void            *p_client
2530 )
2531 {
2532     ENTER_FUNC();
2533 
2534     SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2535     OMX_ERRORTYPE error = OMX_ErrorNone;
2536     OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2537     omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2538 
2539     (void)swvenc;
2540 
2541     if (p_opbuffer != NULL)
2542     {
2543         omxhdr = (OMX_BUFFERHEADERTYPE*)p_opbuffer->p_client_data;
2544     }
2545 
2546     if ( (p_opbuffer != NULL) &&
2547          ((OMX_U32)(omxhdr - omx->m_out_mem_ptr)  < omx->m_sOutPortDef.nBufferCountActual)
2548        )
2549     {
2550         DEBUG_PRINT_LOW("FBD: clientData (%p) buffer (%p) filled_lengh (%d) flags (0x%x) ts (%lld)",
2551           p_opbuffer->p_client_data,
2552           p_opbuffer->p_buffer,
2553           p_opbuffer->filled_length,
2554           p_opbuffer->flags,
2555           p_opbuffer->timestamp);
2556 
2557         if (p_opbuffer->filled_length <=  omxhdr->nAllocLen)
2558         {
2559             omxhdr->pBuffer = p_opbuffer->p_buffer;
2560             omxhdr->nFilledLen = p_opbuffer->filled_length;
2561             omxhdr->nOffset = 0;
2562             omxhdr->nTimeStamp = p_opbuffer->timestamp;
2563             omxhdr->nFlags = 0;
2564             if (SWVENC_FRAME_TYPE_I == p_opbuffer->frame_type)
2565             {
2566                omxhdr->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
2567             }
2568             if (SWVENC_FLAG_EOS & p_opbuffer->flags)
2569             {
2570                omxhdr->nFlags |= OMX_BUFFERFLAG_EOS;
2571             }
2572             if(omxhdr->nFilledLen)
2573             {
2574                omxhdr->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
2575             }
2576             DEBUG_PRINT_LOW("o/p flag = 0x%x", omxhdr->nFlags);
2577 
2578             /* Use buffer case */
2579             if (omx->output_use_buffer && !omx->m_use_output_pmem)
2580             {
2581                 DEBUG_PRINT_LOW("memcpy() for o/p Heap UseBuffer");
2582                 memcpy( omxhdr->pBuffer,
2583                         (p_opbuffer->p_buffer),
2584                         p_opbuffer->filled_length );
2585             }
2586         }
2587         else
2588         {
2589             omxhdr->nFilledLen = 0;
2590         }
2591 
2592     }
2593     else
2594     {
2595         omxhdr = NULL;
2596         error = OMX_ErrorUndefined;
2597     }
2598 
2599     omx->post_event ((unsigned long)omxhdr,error,OMX_COMPONENT_GENERATE_FBD);
2600 
2601     RETURN(eRet);
2602 }
2603 
swvenc_handle_event_cb(SWVENC_HANDLE swvenc,SWVENC_EVENT event,void * p_client)2604 SWVENC_STATUS omx_venc::swvenc_handle_event_cb
2605 (
2606     SWVENC_HANDLE swvenc,
2607     SWVENC_EVENT  event,
2608     void         *p_client
2609 )
2610 {
2611     ENTER_FUNC();
2612 
2613     SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2614     omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2615 
2616     OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2617 
2618     (void)swvenc;
2619 
2620     if (omx == NULL || p_client == NULL)
2621     {
2622         DEBUG_PRINT_ERROR("ERROR: %s invalid i/p params", __FUNCTION__);
2623         RETURN(SWVENC_S_NULL_POINTER);
2624     }
2625 
2626     DEBUG_PRINT_LOW("swvenc_handle_event_cb - event = %d", event);
2627 
2628     switch (event)
2629     {
2630         case SWVENC_EVENT_FLUSH_DONE:
2631         {
2632            DEBUG_PRINT_ERROR("SWVENC_EVENT_FLUSH_DONE input_flush_progress %d output_flush_progress %d",
2633             omx->input_flush_progress, omx->output_flush_progress);
2634            if (omx->input_flush_progress)
2635            {
2636                omx->post_event ((unsigned)NULL, SWVENC_S_SUCCESS,
2637                   OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH);
2638            }
2639            if (omx->output_flush_progress)
2640            {
2641                omx->post_event ((unsigned)NULL, SWVENC_S_SUCCESS,
2642                   OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH);
2643            }
2644            break;
2645         }
2646 
2647         case SWVENC_EVENT_FATAL_ERROR:
2648         {
2649            DEBUG_PRINT_ERROR("ERROR: SWVENC_EVENT_FATAL_ERROR");
2650            omx->omx_report_error();
2651            break;
2652         }
2653 
2654         default:
2655             DEBUG_PRINT_HIGH("Unknown event received : %d", event);
2656             break;
2657     }
2658 
2659     RETURN(eRet);
2660 }
2661 
swvenc_set_rc_mode(OMX_VIDEO_CONTROLRATETYPE eControlRate)2662 SWVENC_STATUS omx_venc::swvenc_set_rc_mode
2663 (
2664     OMX_VIDEO_CONTROLRATETYPE eControlRate
2665 )
2666 {
2667     ENTER_FUNC();
2668 
2669     SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2670     SWVENC_RC_MODE rc_mode;
2671     SWVENC_PROPERTY Prop;
2672 
2673     switch (eControlRate)
2674     {
2675         case OMX_Video_ControlRateDisable:
2676             rc_mode = SWVENC_RC_MODE_NONE;
2677             break;
2678         case OMX_Video_ControlRateVariableSkipFrames:
2679             rc_mode = SWVENC_RC_MODE_VBR_VFR;
2680             break;
2681         case OMX_Video_ControlRateVariable:
2682             rc_mode = SWVENC_RC_MODE_VBR_CFR;
2683             break;
2684         case OMX_Video_ControlRateConstantSkipFrames:
2685             rc_mode = SWVENC_RC_MODE_CBR_VFR;
2686             break;
2687         case OMX_Video_ControlRateConstant:
2688             rc_mode = SWVENC_RC_MODE_CBR_CFR;
2689             break;
2690         default:
2691             DEBUG_PRINT_ERROR("ERROR: UNKNOWN RC MODE");
2692             Ret = SWVENC_S_FAILURE;
2693             break;
2694     }
2695 
2696     if (SWVENC_S_SUCCESS == Ret)
2697     {
2698         Prop.id = SWVENC_PROPERTY_ID_RC_MODE;
2699         Prop.info.rc_mode = rc_mode;
2700         Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2701         if (Ret != SWVENC_S_SUCCESS)
2702         {
2703            DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2704              __FUNCTION__, Ret);
2705            RETURN(SWVENC_S_FAILURE);
2706         }
2707     }
2708 
2709     RETURN(Ret);
2710 }
2711 
swvenc_set_profile_level(OMX_U32 eProfile,OMX_U32 eLevel)2712 SWVENC_STATUS omx_venc::swvenc_set_profile_level
2713 (
2714     OMX_U32 eProfile,
2715     OMX_U32 eLevel
2716 )
2717 {
2718     ENTER_FUNC();
2719 
2720     SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2721     SWVENC_PROPERTY Prop;
2722     SWVENC_PROFILE Profile;
2723     SWVENC_LEVEL Level;
2724 
2725     /* set the profile */
2726     if (SWVENC_CODEC_MPEG4 == m_codec)
2727     {
2728        switch (eProfile)
2729        {
2730           case OMX_VIDEO_MPEG4ProfileSimple:
2731              Profile.mpeg4 = SWVENC_PROFILE_MPEG4_SIMPLE;
2732              break;
2733           case OMX_VIDEO_MPEG4ProfileAdvancedSimple:
2734              Profile.mpeg4 = SWVENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
2735              break;
2736           default:
2737              DEBUG_PRINT_ERROR("ERROR: UNKNOWN PROFILE");
2738              Ret = SWVENC_S_FAILURE;
2739              break;
2740        }
2741        switch (eLevel)
2742        {
2743           case OMX_VIDEO_MPEG4Level0:
2744              Level.mpeg4 = SWVENC_LEVEL_MPEG4_0;
2745              break;
2746           case OMX_VIDEO_MPEG4Level0b:
2747              Level.mpeg4 = SWVENC_LEVEL_MPEG4_0B;
2748              break;
2749           case OMX_VIDEO_MPEG4Level1:
2750              Level.mpeg4 = SWVENC_LEVEL_MPEG4_1;
2751              break;
2752           case OMX_VIDEO_MPEG4Level2:
2753              Level.mpeg4 = SWVENC_LEVEL_MPEG4_2;
2754              break;
2755           case OMX_VIDEO_MPEG4Level3:
2756              Level.mpeg4 = SWVENC_LEVEL_MPEG4_3;
2757              break;
2758           case OMX_VIDEO_MPEG4Level4:
2759              Level.mpeg4 = SWVENC_LEVEL_MPEG4_4;
2760              break;
2761           case OMX_VIDEO_MPEG4Level4a:
2762              Level.mpeg4 = SWVENC_LEVEL_MPEG4_4A;
2763              break;
2764           case OMX_VIDEO_MPEG4Level5:
2765              Level.mpeg4 = SWVENC_LEVEL_MPEG4_5;
2766              break;
2767           default:
2768              DEBUG_PRINT_ERROR("ERROR: UNKNOWN LEVEL");
2769              Ret = SWVENC_S_FAILURE;
2770              break;
2771        }
2772     }
2773     else if (SWVENC_CODEC_H263 == m_codec)
2774     {
2775        switch (eProfile)
2776        {
2777           case OMX_VIDEO_H263ProfileBaseline:
2778              Profile.h263 = SWVENC_PROFILE_H263_BASELINE;
2779              break;
2780           default:
2781              DEBUG_PRINT_ERROR("ERROR: UNKNOWN PROFILE");
2782              Ret = SWVENC_S_FAILURE;
2783              break;
2784        }
2785        switch (eLevel)
2786        {
2787           case OMX_VIDEO_H263Level10:
2788              Level.h263 = SWVENC_LEVEL_H263_10;
2789              break;
2790           case OMX_VIDEO_H263Level20:
2791              Level.h263 = SWVENC_LEVEL_H263_20;
2792              break;
2793           case OMX_VIDEO_H263Level30:
2794              Level.h263 = SWVENC_LEVEL_H263_30;
2795              break;
2796           case OMX_VIDEO_H263Level40:
2797              Level.h263 = SWVENC_LEVEL_H263_40;
2798              break;
2799           case OMX_VIDEO_H263Level50:
2800              Level.h263 = SWVENC_LEVEL_H263_50;
2801              break;
2802           case OMX_VIDEO_H263Level60:
2803              Level.h263 = SWVENC_LEVEL_H263_60;
2804              break;
2805           case OMX_VIDEO_H263Level70:
2806              Level.h263 = SWVENC_LEVEL_H263_70;
2807              break;
2808           default:
2809              DEBUG_PRINT_ERROR("ERROR: UNKNOWN LEVEL");
2810              Ret = SWVENC_S_FAILURE;
2811              break;
2812        }
2813     }
2814     else
2815     {
2816       DEBUG_PRINT_ERROR("ERROR: UNSUPPORTED CODEC");
2817       Ret = SWVENC_S_FAILURE;
2818     }
2819 
2820     if (SWVENC_S_SUCCESS == Ret)
2821     {
2822        Prop.id = SWVENC_PROPERTY_ID_PROFILE;
2823        Prop.info.profile = Profile;
2824 
2825        /* set the profile */
2826        Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2827        if (Ret != SWVENC_S_SUCCESS)
2828        {
2829           DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2830             __FUNCTION__, Ret);
2831           RETURN(SWVENC_S_FAILURE);
2832        }
2833 
2834        /* set the level */
2835        Prop.id = SWVENC_PROPERTY_ID_LEVEL;
2836        Prop.info.level = Level;
2837 
2838        Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2839        if (Ret != SWVENC_S_SUCCESS)
2840        {
2841           DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2842             __FUNCTION__, Ret);
2843           RETURN(SWVENC_S_FAILURE);
2844        }
2845     }
2846 
2847     RETURN(Ret);
2848 }
2849 
swvenc_set_intra_refresh(OMX_VIDEO_PARAM_INTRAREFRESHTYPE * IntraRefresh)2850 SWVENC_STATUS omx_venc::swvenc_set_intra_refresh
2851 (
2852     OMX_VIDEO_PARAM_INTRAREFRESHTYPE *IntraRefresh
2853 )
2854 {
2855    ENTER_FUNC();
2856 
2857    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2858    SWVENC_IR_CONFIG ir_config;
2859    SWVENC_PROPERTY Prop;
2860 
2861    switch (IntraRefresh->eRefreshMode)
2862    {
2863       case OMX_VIDEO_IntraRefreshCyclic:
2864         Prop.info.ir_config.mode = SWVENC_IR_MODE_CYCLIC;
2865         break;
2866       case OMX_VIDEO_IntraRefreshAdaptive:
2867          Prop.info.ir_config.mode = SWVENC_IR_MODE_ADAPTIVE;
2868         break;
2869       case OMX_VIDEO_IntraRefreshBoth:
2870          Prop.info.ir_config.mode = SWVENC_IR_MODE_CYCLIC_ADAPTIVE;
2871         break;
2872       case OMX_VIDEO_IntraRefreshRandom:
2873          Prop.info.ir_config.mode = SWVENC_IR_MODE_RANDOM;
2874         break;
2875       default:
2876          DEBUG_PRINT_ERROR("ERROR: UNKNOWN INTRA REFRESH MODE");
2877          Ret = SWVENC_S_FAILURE;
2878          break;
2879    }
2880 
2881    if (SWVENC_S_SUCCESS == Ret)
2882    {
2883        Prop.id = SWVENC_PROPERTY_ID_IR_CONFIG;
2884        Prop.info.ir_config.cir_mbs = IntraRefresh->nCirMBs;
2885 
2886        Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2887        if (Ret != SWVENC_S_SUCCESS)
2888        {
2889           DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2890             __FUNCTION__, Ret);
2891           Ret = SWVENC_S_FAILURE;
2892        }
2893    }
2894 
2895    RETURN(Ret);
2896 }
2897 
swvenc_set_frame_rate(OMX_U32 nFrameRate)2898 SWVENC_STATUS omx_venc::swvenc_set_frame_rate
2899 (
2900     OMX_U32 nFrameRate
2901 )
2902 {
2903    ENTER_FUNC();
2904 
2905    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2906    SWVENC_PROPERTY Prop;
2907 
2908    Prop.id = SWVENC_PROPERTY_ID_FRAME_RATE;
2909    Prop.info.frame_rate = nFrameRate;
2910 
2911    Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2912    if (Ret != SWVENC_S_SUCCESS)
2913    {
2914       DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2915         __FUNCTION__, Ret);
2916       Ret = SWVENC_S_FAILURE;
2917    }
2918 
2919    RETURN(Ret);
2920 }
2921 
swvenc_set_bit_rate(OMX_U32 nTargetBitrate)2922 SWVENC_STATUS omx_venc::swvenc_set_bit_rate
2923 (
2924     OMX_U32 nTargetBitrate
2925 )
2926 {
2927    ENTER_FUNC();
2928 
2929    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2930    SWVENC_PROPERTY Prop;
2931 
2932    Prop.id = SWVENC_PROPERTY_ID_TARGET_BITRATE;
2933    Prop.info.target_bitrate = nTargetBitrate;
2934 
2935    Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2936    if (Ret != SWVENC_S_SUCCESS)
2937    {
2938       DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2939         __FUNCTION__, Ret);
2940       Ret = SWVENC_S_FAILURE;
2941    }
2942 
2943    RETURN(Ret);
2944 }
2945 
swvenc_set_intra_period(OMX_U32 nPFrame,OMX_U32 nBFrame)2946 SWVENC_STATUS omx_venc::swvenc_set_intra_period
2947 (
2948     OMX_U32 nPFrame,
2949     OMX_U32 nBFrame
2950 )
2951 {
2952    ENTER_FUNC();
2953 
2954    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2955    SWVENC_PROPERTY Prop;
2956 
2957    Prop.id = SWVENC_PROPERTY_ID_INTRA_PERIOD;
2958    Prop.info.intra_period.pframes = nPFrame;
2959    Prop.info.intra_period.bframes = nBFrame;
2960 
2961    Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2962    if (Ret != SWVENC_S_SUCCESS)
2963    {
2964       DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2965         __FUNCTION__, Ret);
2966       Ret = SWVENC_S_FAILURE;
2967    }
2968 
2969    RETURN(Ret);
2970 }
2971 
swvenc_color_align(OMX_BUFFERHEADERTYPE * buffer,OMX_U32 width,OMX_U32 height)2972 bool omx_venc::swvenc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
2973                         OMX_U32 height)
2974 {
2975      OMX_U32 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width),
2976             y_scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height),
2977             uv_stride = VENUS_UV_STRIDE(COLOR_FMT_NV12, width),
2978             uv_scanlines = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height),
2979             src_chroma_offset = width * height;
2980 
2981     if (buffer->nAllocLen >= VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height)) {
2982         OMX_U8* src_buf = buffer->pBuffer, *dst_buf = buffer->pBuffer;
2983         //Do chroma first, so that we can convert it in-place
2984         src_buf += width * height;
2985         dst_buf += y_stride * y_scanlines;
2986         for (int line = height / 2 - 1; line >= 0; --line) {
2987             memmove(dst_buf + line * uv_stride,
2988                     src_buf + line * width,
2989                     width);
2990         }
2991 
2992         dst_buf = src_buf = buffer->pBuffer;
2993         //Copy the Y next
2994         for (int line = height - 1; line > 0; --line) {
2995             memmove(dst_buf + line * y_stride,
2996                     src_buf + line * width,
2997                     width);
2998         }
2999     } else {
3000         DEBUG_PRINT_ERROR("Failed to align Chroma. from %u to %u : \
3001                 Insufficient bufferLen=%u v/s Required=%u",
3002                 (unsigned int)(width*height), (unsigned int)src_chroma_offset, (unsigned int)buffer->nAllocLen,
3003                 VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height));
3004         return false;
3005     }
3006 
3007     return true;
3008 }
3009 
swvenc_set_color_format(OMX_COLOR_FORMATTYPE color_format)3010 SWVENC_STATUS omx_venc::swvenc_set_color_format
3011 (
3012    OMX_COLOR_FORMATTYPE color_format
3013 )
3014 {
3015     ENTER_FUNC();
3016     SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
3017     SWVENC_COLOR_FORMAT swvenc_color_format;
3018     SWVENC_PROPERTY Prop;
3019     if ((color_format == OMX_COLOR_FormatYUV420SemiPlanar) ||
3020          (color_format == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m)))
3021     {
3022         swvenc_color_format = SWVENC_COLOR_FORMAT_NV12;
3023     }
3024     else if (color_format == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatYVU420SemiPlanar))
3025     {
3026         swvenc_color_format = SWVENC_COLOR_FORMAT_NV21;
3027     }
3028     else
3029     {
3030         DEBUG_PRINT_ERROR("%s: color_format %d invalid",__FUNCTION__,color_format);
3031         RETURN(SWVENC_S_FAILURE);
3032     }
3033     /* set the input color format */
3034     Prop.id = SWVENC_PROPERTY_ID_COLOR_FORMAT;
3035     Prop.info.color_format = swvenc_color_format;
3036     Ret = swvenc_setproperty(m_hSwVenc, &Prop);
3037     if (Ret != SWVENC_S_SUCCESS)
3038     {
3039         DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
3040             __FUNCTION__, Ret);
3041         Ret = SWVENC_S_FAILURE;
3042     }
3043     RETURN(Ret);
3044 }
3045