1/*--------------------------------------------------------------------------
2Copyright (c) 2014-2016, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, 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
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26ADVISED 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/misc/media"
55
56/* factory function executed by the core to create instances */
57void *get_omx_component_factory_fn(void)
58{
59    RETURN((new omx_venc));
60}
61
62omx_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("vidc.debug.level", property_value, "1");
72    debug_level = atoi(property_value);
73
74    property_value[0] = '\0';
75    property_get("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("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("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
103omx_venc::~omx_venc()
104{
105    ENTER_FUNC();
106    get_syntaxhdr_enable = false;
107    EXIT_FUNC();
108}
109
110OMX_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
459OMX_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
1422OMX_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
1611OMX_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 m_heap_ptr.clear()");
1659    m_heap_ptr.clear();
1660
1661    DEBUG_PRINT_HIGH("Calling swvenc_deinit()");
1662    swvenc_deinit(m_hSwVenc);
1663
1664    DEBUG_PRINT_HIGH("OMX_Venc:Component Deinit");
1665
1666    RETURN(OMX_ErrorNone);
1667}
1668
1669OMX_U32 omx_venc::dev_stop(void)
1670{
1671    ENTER_FUNC();
1672
1673    SWVENC_STATUS Ret;
1674
1675    if (false == m_stopped)
1676    {
1677       Ret = swvenc_stop(m_hSwVenc);
1678       if (Ret != SWVENC_S_SUCCESS)
1679       {
1680          DEBUG_PRINT_ERROR("%s, swvenc_stop failed (%d)",
1681            __FUNCTION__, Ret);
1682          RETURN(-1);
1683       }
1684       set_format = false;
1685       m_stopped = true;
1686
1687       /* post STOP_DONE event as start is synchronus */
1688       post_event (0, OMX_ErrorNone, OMX_COMPONENT_GENERATE_STOP_DONE);
1689    }
1690
1691    RETURN(0);
1692}
1693
1694OMX_U32 omx_venc::dev_pause(void)
1695{
1696    ENTER_FUNC();
1697    // nothing to be done for sw encoder
1698
1699    RETURN(true);
1700}
1701
1702OMX_U32 omx_venc::dev_resume(void)
1703{
1704    ENTER_FUNC();
1705    // nothing to be done for sw encoder
1706
1707    RETURN(true);
1708}
1709
1710OMX_U32 omx_venc::dev_start(void)
1711{
1712   ENTER_FUNC();
1713   SWVENC_STATUS Ret;
1714   Ret = swvenc_start(m_hSwVenc);
1715   if (Ret != SWVENC_S_SUCCESS)
1716   {
1717      DEBUG_PRINT_ERROR("%s, swvenc_start failed (%d)",
1718        __FUNCTION__, Ret);
1719      RETURN(-1);
1720   }
1721
1722   m_stopped = false;
1723
1724   RETURN(0);
1725}
1726
1727OMX_U32 omx_venc::dev_flush(unsigned port)
1728{
1729   ENTER_FUNC();
1730   SWVENC_STATUS Ret;
1731
1732   (void)port;
1733   Ret = swvenc_flush(m_hSwVenc);
1734   if (Ret != SWVENC_S_SUCCESS)
1735   {
1736      DEBUG_PRINT_ERROR("%s, swvenc_flush failed (%d)",
1737        __FUNCTION__, Ret);
1738      RETURN(-1);
1739   }
1740
1741   RETURN(0);
1742}
1743
1744OMX_U32 omx_venc::dev_start_done(void)
1745{
1746   ENTER_FUNC();
1747
1748   /* post START_DONE event as start is synchronus */
1749   post_event (0, OMX_ErrorNone, OMX_COMPONENT_GENERATE_START_DONE);
1750
1751   RETURN(0);
1752}
1753
1754OMX_U32 omx_venc::dev_set_message_thread_id(pthread_t tid)
1755{
1756    ENTER_FUNC();
1757
1758    // nothing to be done for sw encoder
1759    (void)tid;
1760
1761    RETURN(true);
1762}
1763
1764bool omx_venc::dev_use_buf(void *buf_addr,unsigned port,unsigned index)
1765{
1766    ENTER_FUNC();
1767
1768    (void)buf_addr;
1769    (void)port;
1770    (void)index;
1771
1772    RETURN(true);
1773}
1774
1775bool omx_venc::dev_free_buf(void *buf_addr,unsigned port)
1776{
1777    ENTER_FUNC();
1778
1779    (void)buf_addr;
1780    (void)port;
1781
1782    RETURN(true);
1783}
1784
1785bool omx_venc::dev_empty_buf
1786(
1787    void *buffer,
1788    void *pmem_data_buf,
1789    unsigned index,
1790    unsigned fd
1791)
1792{
1793    ENTER_FUNC();
1794
1795    SWVENC_STATUS Ret;
1796    SWVENC_IPBUFFER ipbuffer;
1797    OMX_BUFFERHEADERTYPE *bufhdr = (OMX_BUFFERHEADERTYPE *)buffer;
1798    unsigned int size = 0, filled_length, offset = 0;
1799    SWVENC_COLOR_FORMAT color_format;
1800    SWVENC_PROPERTY prop;
1801
1802    (void)pmem_data_buf;
1803    (void)index;
1804
1805    if (meta_mode_enable)
1806    {
1807        LEGACY_CAM_METADATA_TYPE *meta_buf = NULL;
1808        meta_buf = (LEGACY_CAM_METADATA_TYPE *)bufhdr->pBuffer;
1809        if(m_sInPortDef.format.video.eColorFormat == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatAndroidOpaque))
1810        {
1811            DEBUG_PRINT_LOW("dev_empty_buf: color_format is QOMX_COLOR_FormatAndroidOpaque");
1812            set_format = true;
1813        }
1814        if(!meta_buf)
1815        {
1816            if (!bufhdr->nFilledLen && (bufhdr->nFlags & OMX_BUFFERFLAG_EOS))
1817            {
1818                ipbuffer.p_buffer= bufhdr->pBuffer;
1819                ipbuffer.size = bufhdr->nAllocLen;
1820                ipbuffer.filled_length = bufhdr->nFilledLen;
1821                DEBUG_PRINT_LOW("dev_empty_buf: empty EOS buffer");
1822            }
1823            else
1824            {
1825                return false;
1826            }
1827        }
1828        else
1829        {
1830            if (meta_buf->buffer_type == LEGACY_CAM_SOURCE)
1831            {
1832                offset = meta_buf->meta_handle->data[1];
1833                size = meta_buf->meta_handle->data[2];
1834                if (set_format && (meta_buf->meta_handle->numFds + meta_buf->meta_handle->numInts > 5))
1835                {
1836                    m_sInPortFormat.eColorFormat = (OMX_COLOR_FORMATTYPE)meta_buf->meta_handle->data[5];
1837                }
1838                ipbuffer.p_buffer = (unsigned char *)mmap(NULL, size, PROT_READ|PROT_WRITE,MAP_SHARED, fd, offset);
1839                if (ipbuffer.p_buffer == MAP_FAILED)
1840                {
1841                    DEBUG_PRINT_ERROR("mmap() failed for fd %d of size %d",fd,size);
1842                    RETURN(OMX_ErrorBadParameter);
1843                }
1844                ipbuffer.size = size;
1845                ipbuffer.filled_length = size;
1846            }
1847            else if (meta_buf->buffer_type == kMetadataBufferTypeGrallocSource)
1848            {
1849                VideoGrallocMetadata *meta_buf = (VideoGrallocMetadata *)bufhdr->pBuffer;
1850                private_handle_t *handle = (private_handle_t *)meta_buf->pHandle;
1851                size = handle->size;
1852                if(set_format)
1853                {
1854                    DEBUG_PRINT_LOW("color format = 0x%x",handle->format);
1855                    if (((OMX_COLOR_FORMATTYPE)handle->format) != m_sInPortFormat.eColorFormat)
1856                    {
1857                        if(handle->format == HAL_PIXEL_FORMAT_NV12_ENCODEABLE)
1858                        {
1859                            m_sInPortFormat.eColorFormat = (OMX_COLOR_FORMATTYPE)
1860                                QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m;
1861                        }
1862                        else
1863                        {
1864                            DEBUG_PRINT_ERROR("%s: OMX_IndexParamVideoPortFormat 0x%x invalid",
1865                                              __FUNCTION__,handle->format);
1866                            RETURN(OMX_ErrorBadParameter);
1867                        }
1868                    }
1869                }
1870                ipbuffer.p_buffer = (unsigned char *)mmap(NULL, size, PROT_READ|PROT_WRITE,MAP_SHARED, fd, offset);
1871                if (ipbuffer.p_buffer == MAP_FAILED)
1872                {
1873                    DEBUG_PRINT_ERROR("mmap() failed for fd %d of size %d",fd,size);
1874                    RETURN(OMX_ErrorBadParameter);
1875                }
1876                ipbuffer.size = size;
1877                ipbuffer.filled_length = size;
1878            }
1879            else
1880            {
1881                //handles the use case for surface encode
1882                ipbuffer.p_buffer = bufhdr->pBuffer;
1883                ipbuffer.size = bufhdr->nAllocLen;
1884                ipbuffer.filled_length = bufhdr->nFilledLen;
1885            }
1886            if (set_format)
1887            {
1888                set_format = false;
1889                m_sInPortDef.format.video.eColorFormat = m_sInPortFormat.eColorFormat;
1890                Ret = swvenc_set_color_format(m_sInPortFormat.eColorFormat);
1891                if (Ret != SWVENC_S_SUCCESS)
1892                {
1893                    DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
1894                        __FUNCTION__, Ret);
1895                    RETURN(OMX_ErrorUnsupportedSetting);
1896                }
1897            }
1898        }
1899    }
1900    else
1901    {
1902        ipbuffer.p_buffer = bufhdr->pBuffer;
1903        ipbuffer.size = bufhdr->nAllocLen;
1904        ipbuffer.filled_length = bufhdr->nFilledLen;
1905    }
1906    ipbuffer.flags = 0;
1907    if (bufhdr->nFlags & OMX_BUFFERFLAG_EOS)
1908    {
1909      ipbuffer.flags |= SWVENC_FLAG_EOS;
1910    }
1911    ipbuffer.timestamp = bufhdr->nTimeStamp;
1912    ipbuffer.p_client_data = (unsigned char *)bufhdr;
1913
1914    DEBUG_PRINT_LOW("ETB: p_buffer (%p) size (%d) filled_len (%d) flags (0x%X) timestamp (%lld) clientData (%p)",
1915      ipbuffer.p_buffer,
1916      ipbuffer.size,
1917      ipbuffer.filled_length,
1918      (unsigned int)ipbuffer.flags,
1919      ipbuffer.timestamp,
1920      ipbuffer.p_client_data);
1921
1922    Ret = swvenc_emptythisbuffer(m_hSwVenc, &ipbuffer);
1923    if (Ret != SWVENC_S_SUCCESS)
1924    {
1925       DEBUG_PRINT_ERROR("%s, swvenc_emptythisbuffer failed (%d)",
1926         __FUNCTION__, Ret);
1927       RETURN(false);
1928    }
1929
1930    if (m_debug.in_buffer_log)
1931    {
1932       swvenc_input_log_buffers((const char*)ipbuffer.p_buffer, ipbuffer.filled_length);
1933    }
1934
1935    RETURN(true);
1936}
1937
1938bool omx_venc::dev_fill_buf
1939(
1940    void *buffer,
1941    void *pmem_data_buf,
1942    unsigned index,
1943    unsigned fd
1944)
1945{
1946    ENTER_FUNC();
1947
1948    SWVENC_STATUS Ret;
1949
1950    SWVENC_OPBUFFER opbuffer;
1951    OMX_BUFFERHEADERTYPE *bufhdr = (OMX_BUFFERHEADERTYPE *)buffer;
1952
1953    (void)pmem_data_buf;
1954    (void)index;
1955    (void)fd;
1956
1957    opbuffer.p_buffer = bufhdr->pBuffer;
1958    opbuffer.size = bufhdr->nAllocLen;
1959    opbuffer.filled_length = bufhdr->nFilledLen;
1960    opbuffer.flags = bufhdr->nFlags;
1961    opbuffer.p_client_data = (unsigned char *)bufhdr;
1962
1963    DEBUG_PRINT_LOW("FTB: p_buffer (%p) size (%d) filled_len (%d) flags (0x%X) timestamp (%lld) clientData (%p)",
1964      opbuffer.p_buffer,
1965      opbuffer.size,
1966      opbuffer.filled_length,
1967      opbuffer.flags,
1968      opbuffer.timestamp,
1969      opbuffer.p_client_data);
1970
1971    if ( false == m_bSeqHdrRequested)
1972    {
1973      if (dev_get_seq_hdr(opbuffer.p_buffer, opbuffer.size, &opbuffer.filled_length) == 0)
1974      {
1975         bufhdr->nFilledLen = opbuffer.filled_length;
1976         bufhdr->nOffset = 0;
1977         bufhdr->nTimeStamp = 0;
1978         bufhdr->nFlags = OMX_BUFFERFLAG_CODECCONFIG;
1979
1980         DEBUG_PRINT_LOW("sending FBD with codec config");
1981         m_bSeqHdrRequested = true;
1982         post_event ((unsigned long)bufhdr,0,OMX_COMPONENT_GENERATE_FBD);
1983      }
1984      else
1985      {
1986         DEBUG_PRINT_ERROR("ERROR: couldn't get sequence header");
1987         post_event(OMX_EventError,OMX_ErrorUndefined,OMX_COMPONENT_GENERATE_EVENT);
1988      }
1989    }
1990    else
1991    {
1992       Ret = swvenc_fillthisbuffer(m_hSwVenc, &opbuffer);
1993       if (Ret != SWVENC_S_SUCCESS)
1994       {
1995          DEBUG_PRINT_ERROR("%s, swvenc_fillthisbuffer failed (%d)",
1996            __FUNCTION__, Ret);
1997          RETURN(false);
1998       }
1999    }
2000
2001    RETURN(true);
2002}
2003
2004bool omx_venc::dev_get_seq_hdr
2005(
2006   void *buffer,
2007   unsigned size,
2008   unsigned *hdrlen
2009)
2010{
2011   ENTER_FUNC();
2012
2013   SWVENC_STATUS Ret;
2014   SWVENC_OPBUFFER Buffer;
2015
2016   Buffer.p_buffer = (unsigned char*) buffer;
2017   Buffer.size = size;
2018
2019   Ret = swvenc_getsequenceheader(m_hSwVenc, &Buffer);
2020   if (Ret != SWVENC_S_SUCCESS)
2021   {
2022      DEBUG_PRINT_ERROR("%s, swvenc_flush failed (%d)",
2023        __FUNCTION__, Ret);
2024      RETURN(-1);
2025   }
2026
2027   *hdrlen = Buffer.filled_length;
2028
2029   RETURN(0);
2030}
2031
2032bool omx_venc::dev_get_capability_ltrcount
2033(
2034   OMX_U32 *min,
2035   OMX_U32 *max,
2036   OMX_U32 *step_size
2037)
2038{
2039    ENTER_FUNC();
2040
2041    (void)min;
2042    (void)max;
2043    (void)step_size;
2044
2045    DEBUG_PRINT_ERROR("Get Capability LTR Count is not supported");
2046
2047    RETURN(false);
2048}
2049
2050bool omx_venc::dev_get_performance_level(OMX_U32 *perflevel)
2051{
2052    ENTER_FUNC();
2053
2054    (void)perflevel;
2055    DEBUG_PRINT_ERROR("Get performance level is not supported");
2056
2057    RETURN(false);
2058}
2059
2060bool omx_venc::dev_get_vui_timing_info(OMX_U32 *enabled)
2061{
2062    ENTER_FUNC();
2063
2064    (void)enabled;
2065    DEBUG_PRINT_ERROR("Get vui timing information is not supported");
2066
2067    RETURN(false);
2068}
2069
2070bool omx_venc::dev_get_vqzip_sei_info(OMX_U32 *enabled)
2071{
2072    ENTER_FUNC();
2073
2074    (void)enabled;
2075    DEBUG_PRINT_ERROR("Get vqzip sei info is not supported");
2076
2077    RETURN(false);
2078}
2079
2080bool omx_venc::dev_get_peak_bitrate(OMX_U32 *peakbitrate)
2081{
2082    //TBD: store the peak bitrate in class and return here;
2083    ENTER_FUNC();
2084
2085    (void)peakbitrate;
2086    DEBUG_PRINT_ERROR("Get peak bitrate is not supported");
2087
2088    RETURN(false);
2089}
2090
2091bool omx_venc::dev_get_batch_size(OMX_U32 *size)
2092{
2093    ENTER_FUNC();
2094
2095    (void)size;
2096
2097    DEBUG_PRINT_ERROR("Get batch size is not supported");
2098
2099    RETURN(false);
2100}
2101
2102bool omx_venc::dev_loaded_start()
2103{
2104   ENTER_FUNC();
2105   RETURN(true);
2106}
2107
2108bool omx_venc::dev_loaded_stop()
2109{
2110   ENTER_FUNC();
2111   RETURN(true);
2112}
2113
2114bool omx_venc::dev_loaded_start_done()
2115{
2116   ENTER_FUNC();
2117   RETURN(true);
2118}
2119
2120bool omx_venc::dev_loaded_stop_done()
2121{
2122   ENTER_FUNC();
2123   RETURN(true);
2124}
2125
2126bool omx_venc::dev_get_buf_req(OMX_U32 *min_buff_count,
2127        OMX_U32 *actual_buff_count,
2128        OMX_U32 *buff_size,
2129        OMX_U32 port)
2130{
2131   ENTER_FUNC();
2132
2133   bool bRet = true;
2134   OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2135
2136   if (PORT_INDEX_IN == port)
2137   {
2138     PortDef = &m_sInPortDef;
2139   }
2140   else if (PORT_INDEX_OUT == port)
2141   {
2142     PortDef = &m_sOutPortDef;
2143   }
2144   else
2145   {
2146     DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2147     bRet = false;
2148   }
2149
2150   if (true == bRet)
2151   {
2152      *min_buff_count = PortDef->nBufferCountMin;
2153      *actual_buff_count = PortDef->nBufferCountActual;
2154      *buff_size = PortDef->nBufferSize;
2155   }
2156
2157   RETURN(true);
2158}
2159
2160bool omx_venc::dev_set_buf_req
2161(
2162   OMX_U32 const *min_buff_count,
2163   OMX_U32 const *actual_buff_count,
2164   OMX_U32 const *buff_size,
2165   OMX_U32 port
2166)
2167{
2168   ENTER_FUNC();
2169
2170   SWVENC_STATUS Ret;
2171   OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2172
2173   (void)min_buff_count;
2174   if (PORT_INDEX_IN == port)
2175   {
2176     PortDef = &m_sInPortDef;
2177   }
2178   else if (PORT_INDEX_OUT == port)
2179   {
2180     PortDef = &m_sOutPortDef;
2181   }
2182   else
2183   {
2184     DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2185     RETURN(false);
2186   }
2187
2188   if (*actual_buff_count < PortDef->nBufferCountMin)
2189   {
2190      DEBUG_PRINT_ERROR("ERROR: %s, (actual,min) buffer count (%d, %d)",
2191         __FUNCTION__, *actual_buff_count, PortDef->nBufferCountMin);
2192      RETURN(false);
2193   }
2194   if (false == meta_mode_enable)
2195   {
2196      if (*buff_size < PortDef->nBufferSize)
2197      {
2198          DEBUG_PRINT_ERROR("ERROR: %s, (new,old) buffer count (%d, %d)",
2199             __FUNCTION__, *actual_buff_count, PortDef->nBufferCountMin);
2200          RETURN(false);
2201      }
2202   }
2203
2204   RETURN(true);
2205}
2206
2207bool omx_venc::dev_is_video_session_supported(OMX_U32 width, OMX_U32 height)
2208{
2209   ENTER_FUNC();
2210
2211   if ( (width * height < m_capability.min_width *  m_capability.min_height) ||
2212        (width * height > m_capability.max_width *  m_capability.max_height)
2213      )
2214   {
2215       DEBUG_PRINT_ERROR(
2216         "Unsupported Resolution WxH = (%u)x(%u) Supported Range = min (%d)x(%d) - max (%d)x(%d)",
2217         width, height,
2218         m_capability.min_width, m_capability.min_height,
2219         m_capability.max_width, m_capability.max_height);
2220       RETURN(false);
2221   }
2222
2223   RETURN(true);
2224}
2225
2226bool omx_venc::dev_buffer_ready_to_queue(OMX_BUFFERHEADERTYPE *buffer)
2227{
2228   ENTER_FUNC();
2229
2230   (void)buffer;
2231   RETURN(true);
2232}
2233int omx_venc::dev_handle_output_extradata(void *buffer, int fd)
2234{
2235   ENTER_FUNC();
2236
2237   (void)buffer;
2238   (void)fd;
2239
2240   RETURN(true);
2241}
2242
2243int omx_venc::dev_handle_input_extradata(void *buffer, int fd, int index)
2244{
2245   ENTER_FUNC();
2246
2247   (void)buffer;
2248   (void)fd;
2249   (void)index;
2250
2251   RETURN(true);
2252}
2253
2254void omx_venc::dev_set_extradata_cookie(void *buffer)
2255{
2256   ENTER_FUNC();
2257
2258   (void)buffer;
2259}
2260
2261int omx_venc::dev_set_format(int color)
2262{
2263   ENTER_FUNC();
2264
2265   (void)color;
2266
2267   RETURN(true);
2268    //return handle->venc_set_format(color);
2269}
2270
2271bool omx_venc::dev_color_align(OMX_BUFFERHEADERTYPE *buffer,
2272                OMX_U32 width, OMX_U32 height)
2273{
2274    ENTER_FUNC();
2275
2276    if(secure_session) {
2277        DEBUG_PRINT_ERROR("Cannot align colors in secure session.");
2278        RETURN(OMX_FALSE);
2279    }
2280    return swvenc_color_align(buffer, width,height);
2281}
2282
2283bool omx_venc::is_secure_session()
2284{
2285    ENTER_FUNC();
2286
2287    RETURN(secure_session);
2288}
2289
2290bool omx_venc::dev_get_output_log_flag()
2291{
2292    ENTER_FUNC();
2293
2294    RETURN(m_debug.out_buffer_log == 1);
2295}
2296
2297int omx_venc::dev_output_log_buffers(const char *buffer, int bufferlen)
2298{
2299    ENTER_FUNC();
2300
2301    if (m_debug.out_buffer_log && !m_debug.outfile)
2302    {
2303        int size = 0;
2304        int width = m_sInPortDef.format.video.nFrameWidth;
2305        int height = m_sInPortDef.format.video.nFrameHeight;
2306        if(SWVENC_CODEC_MPEG4 == m_codec)
2307        {
2308           size = snprintf(m_debug.outfile_name, PROPERTY_VALUE_MAX,
2309              "%s/output_enc_%d_%d_%p.m4v",
2310              m_debug.log_loc, width, height, this);
2311        }
2312        else if(SWVENC_CODEC_H263 == m_codec)
2313        {
2314           size = snprintf(m_debug.outfile_name, PROPERTY_VALUE_MAX,
2315              "%s/output_enc_%d_%d_%p.263",
2316              m_debug.log_loc, width, height, this);
2317        }
2318        if ((size > PROPERTY_VALUE_MAX) || (size < 0))
2319        {
2320           DEBUG_PRINT_ERROR("Failed to open output file: %s for logging as size:%d",
2321                              m_debug.outfile_name, size);
2322           RETURN(-1);
2323        }
2324        DEBUG_PRINT_LOW("output filename = %s", m_debug.outfile_name);
2325        m_debug.outfile = fopen(m_debug.outfile_name, "ab");
2326        if (!m_debug.outfile)
2327        {
2328           DEBUG_PRINT_ERROR("Failed to open output file: %s for logging errno:%d",
2329                             m_debug.outfile_name, errno);
2330           m_debug.outfile_name[0] = '\0';
2331           RETURN(-1);
2332        }
2333    }
2334    if (m_debug.outfile && buffer && bufferlen)
2335    {
2336        DEBUG_PRINT_LOW("%s buffer length: %d", __func__, bufferlen);
2337        fwrite(buffer, bufferlen, 1, m_debug.outfile);
2338    }
2339
2340    RETURN(0);
2341}
2342
2343int omx_venc::swvenc_input_log_buffers(const char *buffer, int bufferlen)
2344{
2345   int width = m_sInPortDef.format.video.nFrameWidth;
2346   int height = m_sInPortDef.format.video.nFrameHeight;
2347   int stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width);
2348   int scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height);
2349   char *temp = (char*)buffer;
2350
2351   if (!m_debug.infile)
2352   {
2353       int size = snprintf(m_debug.infile_name, PROPERTY_VALUE_MAX,
2354                      "%s/input_enc_%d_%d_%p.yuv",
2355                      m_debug.log_loc, width, height, this);
2356       if ((size > PROPERTY_VALUE_MAX) || (size < 0))
2357       {
2358           DEBUG_PRINT_ERROR("Failed to open input file: %s for logging size:%d",
2359                              m_debug.infile_name, size);
2360           RETURN(-1);
2361       }
2362       DEBUG_PRINT_LOW("input filename = %s", m_debug.infile_name);
2363       m_debug.infile = fopen (m_debug.infile_name, "ab");
2364       if (!m_debug.infile)
2365       {
2366           DEBUG_PRINT_HIGH("Failed to open input file: %s for logging",
2367              m_debug.infile_name);
2368           m_debug.infile_name[0] = '\0';
2369           RETURN(-1);
2370       }
2371   }
2372   if (m_debug.infile && buffer && bufferlen)
2373   {
2374       DEBUG_PRINT_LOW("%s buffer length: %d", __func__, bufferlen);
2375       for (int i = 0; i < height; i++)
2376       {
2377          fwrite(temp, width, 1, m_debug.infile);
2378          temp += stride;
2379       }
2380       temp = (char*)(buffer + (stride * scanlines));
2381       for(int i = 0; i < height/2; i++)
2382       {
2383          fwrite(temp, width, 1, m_debug.infile);
2384          temp += stride;
2385      }
2386   }
2387
2388   RETURN(0);
2389}
2390
2391int omx_venc::dev_extradata_log_buffers(char *buffer)
2392{
2393   ENTER_FUNC();
2394
2395   (void)buffer;
2396
2397   RETURN(true);
2398    //return handle->venc_extradata_log_buffers(buffer);
2399}
2400
2401SWVENC_STATUS omx_venc::swvenc_get_buffer_req
2402(
2403   OMX_U32 *min_buff_count,
2404   OMX_U32 *actual_buff_count,
2405   OMX_U32 *buff_size,
2406   OMX_U32 *buff_alignment,
2407   OMX_U32 port
2408)
2409{
2410    ENTER_FUNC();
2411
2412    SWVENC_PROPERTY Prop;
2413    SWVENC_STATUS Ret;
2414    OMX_PARAM_PORTDEFINITIONTYPE *PortDef;
2415
2416    Prop.id = SWVENC_PROPERTY_ID_BUFFER_REQ;
2417    if (PORT_INDEX_IN == port)
2418    {
2419      Prop.info.buffer_req.type = SWVENC_BUFFER_INPUT;
2420    }
2421    else if (PORT_INDEX_OUT == port)
2422    {
2423      Prop.info.buffer_req.type = SWVENC_BUFFER_OUTPUT;
2424    }
2425    else
2426    {
2427      DEBUG_PRINT_ERROR("ERROR: %s, Unsupported parameter", __FUNCTION__);
2428      RETURN(SWVENC_S_INVALID_PARAMETERS);
2429    }
2430
2431    Ret = swvenc_getproperty(m_hSwVenc, &Prop);
2432    if (Ret != SWVENC_S_SUCCESS)
2433    {
2434       DEBUG_PRINT_ERROR("ERROR: %s, swvenc_setproperty failed (%d)", __FUNCTION__,
2435          Ret);
2436       RETURN(SWVENC_S_INVALID_PARAMETERS);
2437    }
2438
2439    *buff_size = Prop.info.buffer_req.size;
2440    *min_buff_count = Prop.info.buffer_req.mincount;
2441    *actual_buff_count = Prop.info.buffer_req.mincount;
2442    *buff_alignment = Prop.info.buffer_req.alignment;
2443
2444    RETURN(Ret);
2445}
2446
2447SWVENC_STATUS omx_venc::swvenc_empty_buffer_done_cb
2448(
2449    SWVENC_HANDLE    swvenc,
2450    SWVENC_IPBUFFER *p_ipbuffer,
2451    void            *p_client
2452)
2453{
2454    ENTER_FUNC();
2455
2456    (void)swvenc;
2457    SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2458    omx_venc *omx = reinterpret_cast<omx_venc*>(p_client);
2459
2460    if (p_ipbuffer == NULL)
2461    {
2462        eRet = SWVENC_S_FAILURE;
2463    }
2464    else
2465    {
2466        omx->swvenc_empty_buffer_done(p_ipbuffer);
2467    }
2468    return eRet;
2469}
2470
2471SWVENC_STATUS omx_venc::swvenc_empty_buffer_done
2472(
2473    SWVENC_IPBUFFER *p_ipbuffer
2474)
2475{
2476    SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2477    OMX_ERRORTYPE error = OMX_ErrorNone;
2478    OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2479
2480    //omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2481    omxhdr = (OMX_BUFFERHEADERTYPE*)p_ipbuffer->p_client_data;
2482
2483    DEBUG_PRINT_LOW("EBD: clientData (%p)", p_ipbuffer->p_client_data);
2484
2485    if ( (omxhdr == NULL) ||
2486         ( ((OMX_U32)(omxhdr - m_inp_mem_ptr) >m_sInPortDef.nBufferCountActual) &&
2487           ((OMX_U32)(omxhdr - meta_buffer_hdr) >m_sInPortDef.nBufferCountActual)
2488         )
2489       )
2490    {
2491        omxhdr = NULL;
2492        error = OMX_ErrorUndefined;
2493    }
2494
2495    if (omxhdr != NULL)
2496    {
2497        // unmap the input buffer->pBuffer
2498        omx_release_meta_buffer(omxhdr);
2499#ifdef _ANDROID_ICS_
2500        if (meta_mode_enable)
2501        {
2502           LEGACY_CAM_METADATA_TYPE *meta_buf = NULL;
2503           unsigned int size = 0;
2504           meta_buf = (LEGACY_CAM_METADATA_TYPE *)omxhdr->pBuffer;
2505           if (meta_buf)
2506           {
2507              if (meta_buf->buffer_type == LEGACY_CAM_SOURCE)
2508              {
2509                  size = meta_buf->meta_handle->data[2];
2510              }
2511              else if (meta_buf->buffer_type == kMetadataBufferTypeGrallocSource)
2512              {
2513                  VideoGrallocMetadata *meta_buf = (VideoGrallocMetadata *)omxhdr->pBuffer;
2514                  private_handle_t *handle = (private_handle_t *)meta_buf->pHandle;
2515                  size = handle->size;
2516              }
2517           }
2518           int status = munmap(p_ipbuffer->p_buffer, size);
2519           DEBUG_PRINT_HIGH("Unmapped pBuffer <%p> size <%d> status <%d>", p_ipbuffer->p_buffer, size, status);
2520        }
2521#endif
2522        post_event ((unsigned long)omxhdr,error,OMX_COMPONENT_GENERATE_EBD);
2523    }
2524
2525    RETURN(eRet);
2526}
2527
2528SWVENC_STATUS omx_venc::swvenc_fill_buffer_done_cb
2529(
2530    SWVENC_HANDLE    swvenc,
2531    SWVENC_OPBUFFER *p_opbuffer,
2532    void            *p_client
2533)
2534{
2535    ENTER_FUNC();
2536
2537    SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2538    OMX_ERRORTYPE error = OMX_ErrorNone;
2539    OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2540    omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2541
2542    (void)swvenc;
2543
2544    if (p_opbuffer != NULL)
2545    {
2546        omxhdr = (OMX_BUFFERHEADERTYPE*)p_opbuffer->p_client_data;
2547    }
2548
2549    if ( (p_opbuffer != NULL) &&
2550         ((OMX_U32)(omxhdr - omx->m_out_mem_ptr)  < omx->m_sOutPortDef.nBufferCountActual)
2551       )
2552    {
2553        DEBUG_PRINT_LOW("FBD: clientData (%p) buffer (%p) filled_lengh (%d) flags (0x%x) ts (%lld)",
2554          p_opbuffer->p_client_data,
2555          p_opbuffer->p_buffer,
2556          p_opbuffer->filled_length,
2557          p_opbuffer->flags,
2558          p_opbuffer->timestamp);
2559
2560        if (p_opbuffer->filled_length <=  omxhdr->nAllocLen)
2561        {
2562            omxhdr->pBuffer = p_opbuffer->p_buffer;
2563            omxhdr->nFilledLen = p_opbuffer->filled_length;
2564            omxhdr->nOffset = 0;
2565            omxhdr->nTimeStamp = p_opbuffer->timestamp;
2566            omxhdr->nFlags = 0;
2567            if (SWVENC_FRAME_TYPE_I == p_opbuffer->frame_type)
2568            {
2569               omxhdr->nFlags |= OMX_BUFFERFLAG_SYNCFRAME;
2570            }
2571            if (SWVENC_FLAG_EOS & p_opbuffer->flags)
2572            {
2573               omxhdr->nFlags |= OMX_BUFFERFLAG_EOS;
2574            }
2575            if(omxhdr->nFilledLen)
2576            {
2577               omxhdr->nFlags |= OMX_BUFFERFLAG_ENDOFFRAME;
2578            }
2579            DEBUG_PRINT_LOW("o/p flag = 0x%x", omxhdr->nFlags);
2580
2581            /* Use buffer case */
2582            if (omx->output_use_buffer && !omx->m_use_output_pmem)
2583            {
2584                DEBUG_PRINT_LOW("memcpy() for o/p Heap UseBuffer");
2585                memcpy( omxhdr->pBuffer,
2586                        (p_opbuffer->p_buffer),
2587                        p_opbuffer->filled_length );
2588            }
2589        }
2590        else
2591        {
2592            omxhdr->nFilledLen = 0;
2593        }
2594
2595    }
2596    else
2597    {
2598        omxhdr = NULL;
2599        error = OMX_ErrorUndefined;
2600    }
2601
2602    omx->post_event ((unsigned long)omxhdr,error,OMX_COMPONENT_GENERATE_FBD);
2603
2604    RETURN(eRet);
2605}
2606
2607SWVENC_STATUS omx_venc::swvenc_handle_event_cb
2608(
2609    SWVENC_HANDLE swvenc,
2610    SWVENC_EVENT  event,
2611    void         *p_client
2612)
2613{
2614    ENTER_FUNC();
2615
2616    SWVENC_STATUS eRet = SWVENC_S_SUCCESS;
2617    omx_video *omx = reinterpret_cast<omx_video*>(p_client);
2618
2619    OMX_BUFFERHEADERTYPE* omxhdr = NULL;
2620
2621    (void)swvenc;
2622
2623    if (omx == NULL || p_client == NULL)
2624    {
2625        DEBUG_PRINT_ERROR("ERROR: %s invalid i/p params", __FUNCTION__);
2626        RETURN(SWVENC_S_NULL_POINTER);
2627    }
2628
2629    DEBUG_PRINT_LOW("swvenc_handle_event_cb - event = %d", event);
2630
2631    switch (event)
2632    {
2633        case SWVENC_EVENT_FLUSH_DONE:
2634        {
2635           DEBUG_PRINT_ERROR("SWVENC_EVENT_FLUSH_DONE input_flush_progress %d output_flush_progress %d",
2636            omx->input_flush_progress, omx->output_flush_progress);
2637           if (omx->input_flush_progress)
2638           {
2639               omx->post_event ((unsigned)NULL, SWVENC_S_SUCCESS,
2640                  OMX_COMPONENT_GENERATE_EVENT_INPUT_FLUSH);
2641           }
2642           if (omx->output_flush_progress)
2643           {
2644               omx->post_event ((unsigned)NULL, SWVENC_S_SUCCESS,
2645                  OMX_COMPONENT_GENERATE_EVENT_OUTPUT_FLUSH);
2646           }
2647           break;
2648        }
2649
2650        case SWVENC_EVENT_FATAL_ERROR:
2651        {
2652           DEBUG_PRINT_ERROR("ERROR: SWVENC_EVENT_FATAL_ERROR");
2653           omx->omx_report_error();
2654           break;
2655        }
2656
2657        default:
2658            DEBUG_PRINT_HIGH("Unknown event received : %d", event);
2659            break;
2660    }
2661
2662    RETURN(eRet);
2663}
2664
2665SWVENC_STATUS omx_venc::swvenc_set_rc_mode
2666(
2667    OMX_VIDEO_CONTROLRATETYPE eControlRate
2668)
2669{
2670    ENTER_FUNC();
2671
2672    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2673    SWVENC_RC_MODE rc_mode;
2674    SWVENC_PROPERTY Prop;
2675
2676    switch (eControlRate)
2677    {
2678        case OMX_Video_ControlRateDisable:
2679            rc_mode = SWVENC_RC_MODE_NONE;
2680            break;
2681        case OMX_Video_ControlRateVariableSkipFrames:
2682            rc_mode = SWVENC_RC_MODE_VBR_VFR;
2683            break;
2684        case OMX_Video_ControlRateVariable:
2685            rc_mode = SWVENC_RC_MODE_VBR_CFR;
2686            break;
2687        case OMX_Video_ControlRateConstantSkipFrames:
2688            rc_mode = SWVENC_RC_MODE_CBR_VFR;
2689            break;
2690        case OMX_Video_ControlRateConstant:
2691            rc_mode = SWVENC_RC_MODE_CBR_CFR;
2692            break;
2693        default:
2694            DEBUG_PRINT_ERROR("ERROR: UNKNOWN RC MODE");
2695            Ret = SWVENC_S_FAILURE;
2696            break;
2697    }
2698
2699    if (SWVENC_S_SUCCESS == Ret)
2700    {
2701        Prop.id = SWVENC_PROPERTY_ID_RC_MODE;
2702        Prop.info.rc_mode = rc_mode;
2703        Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2704        if (Ret != SWVENC_S_SUCCESS)
2705        {
2706           DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2707             __FUNCTION__, Ret);
2708           RETURN(SWVENC_S_FAILURE);
2709        }
2710    }
2711
2712    RETURN(Ret);
2713}
2714
2715SWVENC_STATUS omx_venc::swvenc_set_profile_level
2716(
2717    OMX_U32 eProfile,
2718    OMX_U32 eLevel
2719)
2720{
2721    ENTER_FUNC();
2722
2723    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2724    SWVENC_PROPERTY Prop;
2725    SWVENC_PROFILE Profile;
2726    SWVENC_LEVEL Level;
2727
2728    /* set the profile */
2729    if (SWVENC_CODEC_MPEG4 == m_codec)
2730    {
2731       switch (eProfile)
2732       {
2733          case OMX_VIDEO_MPEG4ProfileSimple:
2734             Profile.mpeg4 = SWVENC_PROFILE_MPEG4_SIMPLE;
2735             break;
2736          case OMX_VIDEO_MPEG4ProfileAdvancedSimple:
2737             Profile.mpeg4 = SWVENC_PROFILE_MPEG4_ADVANCED_SIMPLE;
2738             break;
2739          default:
2740             DEBUG_PRINT_ERROR("ERROR: UNKNOWN PROFILE");
2741             Ret = SWVENC_S_FAILURE;
2742             break;
2743       }
2744       switch (eLevel)
2745       {
2746          case OMX_VIDEO_MPEG4Level0:
2747             Level.mpeg4 = SWVENC_LEVEL_MPEG4_0;
2748             break;
2749          case OMX_VIDEO_MPEG4Level0b:
2750             Level.mpeg4 = SWVENC_LEVEL_MPEG4_0B;
2751             break;
2752          case OMX_VIDEO_MPEG4Level1:
2753             Level.mpeg4 = SWVENC_LEVEL_MPEG4_1;
2754             break;
2755          case OMX_VIDEO_MPEG4Level2:
2756             Level.mpeg4 = SWVENC_LEVEL_MPEG4_2;
2757             break;
2758          case OMX_VIDEO_MPEG4Level3:
2759             Level.mpeg4 = SWVENC_LEVEL_MPEG4_3;
2760             break;
2761          case OMX_VIDEO_MPEG4Level4:
2762             Level.mpeg4 = SWVENC_LEVEL_MPEG4_4;
2763             break;
2764          case OMX_VIDEO_MPEG4Level4a:
2765             Level.mpeg4 = SWVENC_LEVEL_MPEG4_4A;
2766             break;
2767          case OMX_VIDEO_MPEG4Level5:
2768             Level.mpeg4 = SWVENC_LEVEL_MPEG4_5;
2769             break;
2770          default:
2771             DEBUG_PRINT_ERROR("ERROR: UNKNOWN LEVEL");
2772             Ret = SWVENC_S_FAILURE;
2773             break;
2774       }
2775    }
2776    else if (SWVENC_CODEC_H263 == m_codec)
2777    {
2778       switch (eProfile)
2779       {
2780          case OMX_VIDEO_H263ProfileBaseline:
2781             Profile.h263 = SWVENC_PROFILE_H263_BASELINE;
2782             break;
2783          default:
2784             DEBUG_PRINT_ERROR("ERROR: UNKNOWN PROFILE");
2785             Ret = SWVENC_S_FAILURE;
2786             break;
2787       }
2788       switch (eLevel)
2789       {
2790          case OMX_VIDEO_H263Level10:
2791             Level.h263 = SWVENC_LEVEL_H263_10;
2792             break;
2793          case OMX_VIDEO_H263Level20:
2794             Level.h263 = SWVENC_LEVEL_H263_20;
2795             break;
2796          case OMX_VIDEO_H263Level30:
2797             Level.h263 = SWVENC_LEVEL_H263_30;
2798             break;
2799          case OMX_VIDEO_H263Level40:
2800             Level.h263 = SWVENC_LEVEL_H263_40;
2801             break;
2802          case OMX_VIDEO_H263Level50:
2803             Level.h263 = SWVENC_LEVEL_H263_50;
2804             break;
2805          case OMX_VIDEO_H263Level60:
2806             Level.h263 = SWVENC_LEVEL_H263_60;
2807             break;
2808          case OMX_VIDEO_H263Level70:
2809             Level.h263 = SWVENC_LEVEL_H263_70;
2810             break;
2811          default:
2812             DEBUG_PRINT_ERROR("ERROR: UNKNOWN LEVEL");
2813             Ret = SWVENC_S_FAILURE;
2814             break;
2815       }
2816    }
2817    else
2818    {
2819      DEBUG_PRINT_ERROR("ERROR: UNSUPPORTED CODEC");
2820      Ret = SWVENC_S_FAILURE;
2821    }
2822
2823    if (SWVENC_S_SUCCESS == Ret)
2824    {
2825       Prop.id = SWVENC_PROPERTY_ID_PROFILE;
2826       Prop.info.profile = Profile;
2827
2828       /* set the profile */
2829       Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2830       if (Ret != SWVENC_S_SUCCESS)
2831       {
2832          DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2833            __FUNCTION__, Ret);
2834          RETURN(SWVENC_S_FAILURE);
2835       }
2836
2837       /* set the level */
2838       Prop.id = SWVENC_PROPERTY_ID_LEVEL;
2839       Prop.info.level = Level;
2840
2841       Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2842       if (Ret != SWVENC_S_SUCCESS)
2843       {
2844          DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2845            __FUNCTION__, Ret);
2846          RETURN(SWVENC_S_FAILURE);
2847       }
2848    }
2849
2850    RETURN(Ret);
2851}
2852
2853SWVENC_STATUS omx_venc::swvenc_set_intra_refresh
2854(
2855    OMX_VIDEO_PARAM_INTRAREFRESHTYPE *IntraRefresh
2856)
2857{
2858   ENTER_FUNC();
2859
2860   SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2861   SWVENC_IR_CONFIG ir_config;
2862   SWVENC_PROPERTY Prop;
2863
2864   switch (IntraRefresh->eRefreshMode)
2865   {
2866      case OMX_VIDEO_IntraRefreshCyclic:
2867        Prop.info.ir_config.mode = SWVENC_IR_MODE_CYCLIC;
2868        break;
2869      case OMX_VIDEO_IntraRefreshAdaptive:
2870         Prop.info.ir_config.mode = SWVENC_IR_MODE_ADAPTIVE;
2871        break;
2872      case OMX_VIDEO_IntraRefreshBoth:
2873         Prop.info.ir_config.mode = SWVENC_IR_MODE_CYCLIC_ADAPTIVE;
2874        break;
2875      case OMX_VIDEO_IntraRefreshRandom:
2876         Prop.info.ir_config.mode = SWVENC_IR_MODE_RANDOM;
2877        break;
2878      default:
2879         DEBUG_PRINT_ERROR("ERROR: UNKNOWN INTRA REFRESH MODE");
2880         Ret = SWVENC_S_FAILURE;
2881         break;
2882   }
2883
2884   if (SWVENC_S_SUCCESS == Ret)
2885   {
2886       Prop.id = SWVENC_PROPERTY_ID_IR_CONFIG;
2887       Prop.info.ir_config.cir_mbs = IntraRefresh->nCirMBs;
2888
2889       Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2890       if (Ret != SWVENC_S_SUCCESS)
2891       {
2892          DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2893            __FUNCTION__, Ret);
2894          Ret = SWVENC_S_FAILURE;
2895       }
2896   }
2897
2898   RETURN(Ret);
2899}
2900
2901SWVENC_STATUS omx_venc::swvenc_set_frame_rate
2902(
2903    OMX_U32 nFrameRate
2904)
2905{
2906   ENTER_FUNC();
2907
2908   SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2909   SWVENC_PROPERTY Prop;
2910
2911   Prop.id = SWVENC_PROPERTY_ID_FRAME_RATE;
2912   Prop.info.frame_rate = nFrameRate;
2913
2914   Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2915   if (Ret != SWVENC_S_SUCCESS)
2916   {
2917      DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2918        __FUNCTION__, Ret);
2919      Ret = SWVENC_S_FAILURE;
2920   }
2921
2922   RETURN(Ret);
2923}
2924
2925SWVENC_STATUS omx_venc::swvenc_set_bit_rate
2926(
2927    OMX_U32 nTargetBitrate
2928)
2929{
2930   ENTER_FUNC();
2931
2932   SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2933   SWVENC_PROPERTY Prop;
2934
2935   Prop.id = SWVENC_PROPERTY_ID_TARGET_BITRATE;
2936   Prop.info.target_bitrate = nTargetBitrate;
2937
2938   Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2939   if (Ret != SWVENC_S_SUCCESS)
2940   {
2941      DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2942        __FUNCTION__, Ret);
2943      Ret = SWVENC_S_FAILURE;
2944   }
2945
2946   RETURN(Ret);
2947}
2948
2949SWVENC_STATUS omx_venc::swvenc_set_intra_period
2950(
2951    OMX_U32 nPFrame,
2952    OMX_U32 nBFrame
2953)
2954{
2955   ENTER_FUNC();
2956
2957   SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
2958   SWVENC_PROPERTY Prop;
2959
2960   Prop.id = SWVENC_PROPERTY_ID_INTRA_PERIOD;
2961   Prop.info.intra_period.pframes = nPFrame;
2962   Prop.info.intra_period.bframes = nBFrame;
2963
2964   Ret = swvenc_setproperty(m_hSwVenc, &Prop);
2965   if (Ret != SWVENC_S_SUCCESS)
2966   {
2967      DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
2968        __FUNCTION__, Ret);
2969      Ret = SWVENC_S_FAILURE;
2970   }
2971
2972   RETURN(Ret);
2973}
2974
2975bool omx_venc::swvenc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
2976                        OMX_U32 height)
2977{
2978     OMX_U32 y_stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width),
2979            y_scanlines = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height),
2980            uv_stride = VENUS_UV_STRIDE(COLOR_FMT_NV12, width),
2981            uv_scanlines = VENUS_UV_SCANLINES(COLOR_FMT_NV12, height),
2982            src_chroma_offset = width * height;
2983
2984    if (buffer->nAllocLen >= VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height)) {
2985        OMX_U8* src_buf = buffer->pBuffer, *dst_buf = buffer->pBuffer;
2986        //Do chroma first, so that we can convert it in-place
2987        src_buf += width * height;
2988        dst_buf += y_stride * y_scanlines;
2989        for (int line = height / 2 - 1; line >= 0; --line) {
2990            memmove(dst_buf + line * uv_stride,
2991                    src_buf + line * width,
2992                    width);
2993        }
2994
2995        dst_buf = src_buf = buffer->pBuffer;
2996        //Copy the Y next
2997        for (int line = height - 1; line > 0; --line) {
2998            memmove(dst_buf + line * y_stride,
2999                    src_buf + line * width,
3000                    width);
3001        }
3002    } else {
3003        DEBUG_PRINT_ERROR("Failed to align Chroma. from %u to %u : \
3004                Insufficient bufferLen=%u v/s Required=%u",
3005                (unsigned int)(width*height), (unsigned int)src_chroma_offset, (unsigned int)buffer->nAllocLen,
3006                VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height));
3007        return false;
3008    }
3009
3010    return true;
3011}
3012
3013SWVENC_STATUS omx_venc::swvenc_set_color_format
3014(
3015   OMX_COLOR_FORMATTYPE color_format
3016)
3017{
3018    ENTER_FUNC();
3019    SWVENC_STATUS Ret = SWVENC_S_SUCCESS;
3020    SWVENC_COLOR_FORMAT swvenc_color_format;
3021    SWVENC_PROPERTY Prop;
3022    if ((color_format == OMX_COLOR_FormatYUV420SemiPlanar) ||
3023         (color_format == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FORMATYUV420PackedSemiPlanar32m)))
3024    {
3025        swvenc_color_format = SWVENC_COLOR_FORMAT_NV12;
3026    }
3027    else if (color_format == ((OMX_COLOR_FORMATTYPE) QOMX_COLOR_FormatYVU420SemiPlanar))
3028    {
3029        swvenc_color_format = SWVENC_COLOR_FORMAT_NV21;
3030    }
3031    else
3032    {
3033        DEBUG_PRINT_ERROR("%s: color_format %d invalid",__FUNCTION__,color_format);
3034        RETURN(SWVENC_S_FAILURE);
3035    }
3036    /* set the input color format */
3037    Prop.id = SWVENC_PROPERTY_ID_COLOR_FORMAT;
3038    Prop.info.color_format = swvenc_color_format;
3039    Ret = swvenc_setproperty(m_hSwVenc, &Prop);
3040    if (Ret != SWVENC_S_SUCCESS)
3041    {
3042        DEBUG_PRINT_ERROR("%s, swvenc_setproperty failed (%d)",
3043            __FUNCTION__, Ret);
3044        Ret = SWVENC_S_FAILURE;
3045    }
3046    RETURN(Ret);
3047}
3048