1/*--------------------------------------------------------------------------
2Copyright (c) 2012-2014, 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
6met:
7    * Redistributions of source code must retain the above copyright
8      notice, this list of conditions and the following disclaimer.
9    * Redistributions in binary form must reproduce the above
10      copyright notice, this list of conditions and the following
11      disclaimer in the documentation and/or other materials provided
12      with the distribution.
13    * Neither the name of The Linux Foundation nor the names of its
14      contributors may be used to endorse or promote products derived
15      from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28--------------------------------------------------------------------------*/
29#ifndef __OMX_VENC_DEV__
30#define __OMX_VENC_DEV__
31
32#include "OMX_Types.h"
33#include "OMX_Core.h"
34#include "OMX_VideoExt.h"
35#include "OMX_QCOMExtns.h"
36#include "qc_omx_component.h"
37#include "omx_video_common.h"
38#include "omx_video_base.h"
39#include "omx_video_encoder.h"
40#include <linux/videodev2.h>
41#include <poll.h>
42
43#define TIMEOUT 5*60*1000
44#define BIT(num) (1 << (num))
45#define MAX_HYB_HIERP_LAYERS 6
46
47enum hier_type {
48    HIER_NONE = 0x0,
49    HIER_P = 0x1,
50    HIER_B = 0x2,
51    HIER_P_HYBRID = 0x3,
52};
53
54struct msm_venc_switch {
55    unsigned char    status;
56};
57
58struct msm_venc_allocatorproperty {
59    unsigned long     mincount;
60    unsigned long     actualcount;
61    unsigned long     datasize;
62    unsigned long     suffixsize;
63    unsigned long     alignment;
64    unsigned long     bufpoolid;
65};
66
67struct msm_venc_basecfg {
68    unsigned long    input_width;
69    unsigned long    input_height;
70    unsigned long    dvs_width;
71    unsigned long    dvs_height;
72    unsigned long    codectype;
73    unsigned long    fps_num;
74    unsigned long    fps_den;
75    unsigned long    targetbitrate;
76    unsigned long    inputformat;
77};
78
79struct msm_venc_profile {
80    unsigned long    profile;
81};
82struct msm_venc_profilelevel {
83    unsigned long    level;
84};
85
86struct msm_venc_sessionqp {
87    unsigned long    iframeqp;
88    unsigned long    pframeqp;
89    unsigned long    bframeqp;
90};
91
92struct msm_venc_initqp {
93    unsigned long    iframeqp;
94    unsigned long    pframeqp;
95    unsigned long    bframeqp;
96    unsigned long    enableinitqp;
97};
98
99struct msm_venc_qprange {
100    unsigned long    maxqp;
101    unsigned long    minqp;
102};
103struct msm_venc_intraperiod {
104    unsigned long    num_pframes;
105    unsigned long    num_bframes;
106};
107struct msm_venc_seqheader {
108    unsigned char *hdrbufptr;
109    unsigned long    bufsize;
110    unsigned long    hdrlen;
111};
112
113struct msm_venc_capability {
114    unsigned long    codec_types;
115    unsigned long    maxframe_width;
116    unsigned long    maxframe_height;
117    unsigned long    maxtarget_bitrate;
118    unsigned long    maxframe_rate;
119    unsigned long    input_formats;
120    unsigned char    dvs;
121};
122
123struct msm_venc_entropycfg {
124    unsigned longentropysel;
125    unsigned long    cabacmodel;
126};
127
128struct msm_venc_dbcfg {
129    unsigned long    db_mode;
130    unsigned long    slicealpha_offset;
131    unsigned long    slicebeta_offset;
132};
133
134struct msm_venc_intrarefresh {
135    unsigned long    irmode;
136    unsigned long    mbcount;
137};
138
139struct msm_venc_multiclicecfg {
140    unsigned long    mslice_mode;
141    unsigned long    mslice_size;
142};
143
144struct msm_venc_bufferflush {
145    unsigned long    flush_mode;
146};
147
148struct msm_venc_ratectrlcfg {
149    unsigned long    rcmode;
150};
151
152struct    msm_venc_voptimingcfg {
153    unsigned long    voptime_resolution;
154};
155struct msm_venc_framerate {
156    unsigned long    fps_denominator;
157    unsigned long    fps_numerator;
158};
159
160struct msm_venc_targetbitrate {
161    unsigned long    target_bitrate;
162};
163
164
165struct msm_venc_rotation {
166    unsigned long    rotation;
167};
168
169struct msm_venc_timeout {
170    unsigned long    millisec;
171};
172
173struct msm_venc_headerextension {
174    unsigned long    header_extension;
175};
176
177struct msm_venc_video_capability {
178    unsigned int min_width;
179    unsigned int max_width;
180    unsigned int min_height;
181    unsigned int max_height;
182};
183
184struct msm_venc_idrperiod {
185    unsigned long idrperiod;
186};
187
188struct msm_venc_slice_delivery {
189    unsigned long enable;
190};
191
192struct msm_venc_hierlayers {
193    unsigned int numlayers;
194    enum hier_type hier_mode;
195};
196
197struct msm_venc_ltrinfo {
198    unsigned int enabled;
199    unsigned int count;
200};
201
202struct msm_venc_perf_level {
203    unsigned int perflevel;
204};
205
206struct msm_venc_vui_timing_info {
207    unsigned int enabled;
208};
209
210struct msm_venc_peak_bitrate {
211    unsigned int peakbitrate;
212};
213
214struct msm_venc_vpx_error_resilience {
215    unsigned int enable;
216};
217
218struct msm_venc_priority {
219    OMX_U32 priority;
220};
221
222enum v4l2_ports {
223    CAPTURE_PORT,
224    OUTPUT_PORT,
225    MAX_PORT
226};
227
228struct extradata_buffer_info {
229    unsigned long buffer_size;
230    char* uaddr;
231    int count;
232    int size;
233    int allocated;
234#ifdef USE_ION
235    struct venc_ion ion;
236#endif
237};
238
239enum rc_modes {
240    RC_VBR_VFR = BIT(0),
241    RC_VBR_CFR = BIT(1),
242    RC_CBR_VFR = BIT(2),
243    RC_CBR_CFR = BIT(3),
244    RC_ALL = (RC_VBR_VFR | RC_VBR_CFR
245        | RC_CBR_VFR | RC_CBR_CFR)
246};
247
248class venc_dev
249{
250    public:
251        venc_dev(class omx_venc *venc_class); //constructor
252        ~venc_dev(); //des
253
254        static void* async_venc_message_thread (void *);
255        bool venc_open(OMX_U32);
256        void venc_close();
257        unsigned venc_stop(void);
258        unsigned venc_pause(void);
259        unsigned venc_start(void);
260        unsigned venc_flush(unsigned);
261#ifdef _ANDROID_ICS_
262        bool venc_set_meta_mode(bool);
263#endif
264        unsigned venc_resume(void);
265        unsigned venc_start_done(void);
266        unsigned venc_stop_done(void);
267        unsigned venc_set_message_thread_id(pthread_t);
268        bool venc_use_buf(void*, unsigned,unsigned);
269        bool venc_free_buf(void*, unsigned);
270        bool venc_empty_buf(void *, void *,unsigned,unsigned);
271        bool venc_fill_buf(void *, void *,unsigned,unsigned);
272
273        bool venc_get_buf_req(OMX_U32 *,OMX_U32 *,
274                OMX_U32 *,OMX_U32);
275        bool venc_set_buf_req(OMX_U32 *,OMX_U32 *,
276                OMX_U32 *,OMX_U32);
277        bool venc_set_param(void *,OMX_INDEXTYPE);
278        bool venc_set_config(void *configData, OMX_INDEXTYPE index);
279        bool venc_get_profile_level(OMX_U32 *eProfile,OMX_U32 *eLevel);
280        bool venc_get_seq_hdr(void *, unsigned, unsigned *);
281        bool venc_loaded_start(void);
282        bool venc_loaded_stop(void);
283        bool venc_loaded_start_done(void);
284        bool venc_loaded_stop_done(void);
285        bool venc_is_video_session_supported(unsigned long width, unsigned long height);
286        bool venc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
287                        OMX_U32 height);
288        bool venc_get_performance_level(OMX_U32 *perflevel);
289        bool venc_get_vui_timing_info(OMX_U32 *enabled);
290        bool venc_get_peak_bitrate(OMX_U32 *peakbitrate);
291        bool venc_get_output_log_flag();
292        int venc_output_log_buffers(const char *buffer_addr, int buffer_len);
293        int venc_input_log_buffers(OMX_BUFFERHEADERTYPE *buffer, int fd, int plane_offset);
294        int venc_extradata_log_buffers(char *buffer_addr);
295
296        struct venc_debug_cap m_debug;
297        OMX_U32 m_nDriver_fd;
298        bool m_profile_set;
299        bool m_level_set;
300        int num_planes;
301        int etb, ebd, ftb, fbd;
302        struct recon_buffer {
303            unsigned char* virtual_address;
304            int pmem_fd;
305            int size;
306            int alignment;
307            int offset;
308#ifdef USE_ION
309            int ion_device_fd;
310            struct ion_allocation_data alloc_data;
311            struct ion_fd_data ion_alloc_fd;
312#endif
313        };
314
315        int stopped;
316        int resume_in_stopped;
317        bool m_max_allowed_bitrate_check;
318        pthread_t m_tid;
319        bool async_thread_created;
320        class omx_venc *venc_handle;
321        OMX_ERRORTYPE allocate_extradata();
322        void free_extradata();
323        int append_mbi_extradata(void *, struct msm_vidc_extradata_header*);
324        bool handle_extradata(void *, int);
325        int venc_set_format(int);
326        bool deinterlace_enabled;
327        bool hw_overload;
328    private:
329        OMX_U32                             m_codec;
330        struct msm_venc_basecfg             m_sVenc_cfg;
331        struct msm_venc_ratectrlcfg         rate_ctrl;
332        struct msm_venc_targetbitrate       bitrate;
333        struct msm_venc_intraperiod         intra_period;
334        struct msm_venc_profile             codec_profile;
335        struct msm_venc_profilelevel        profile_level;
336        struct msm_venc_switch              set_param;
337        struct msm_venc_voptimingcfg        time_inc;
338        struct msm_venc_allocatorproperty   m_sInput_buff_property;
339        struct msm_venc_allocatorproperty   m_sOutput_buff_property;
340        struct msm_venc_sessionqp           session_qp;
341        struct msm_venc_initqp              init_qp;
342        struct msm_venc_qprange             session_qp_range;
343        struct msm_venc_qprange             session_qp_values;
344        struct msm_venc_multiclicecfg       multislice;
345        struct msm_venc_entropycfg          entropy;
346        struct msm_venc_dbcfg               dbkfilter;
347        struct msm_venc_intrarefresh        intra_refresh;
348        struct msm_venc_headerextension     hec;
349        struct msm_venc_voptimingcfg        voptimecfg;
350        struct msm_venc_video_capability    capability;
351        struct msm_venc_idrperiod           idrperiod;
352        struct msm_venc_slice_delivery      slice_mode;
353        struct msm_venc_hierlayers          hier_layers;
354        struct msm_venc_perf_level          performance_level;
355        struct msm_venc_vui_timing_info     vui_timing_info;
356        struct msm_venc_peak_bitrate        peak_bitrate;
357        struct msm_venc_ltrinfo             ltrinfo;
358        struct msm_venc_vpx_error_resilience vpx_err_resilience;
359        struct msm_venc_priority            sess_priority;
360        OMX_U32                             operating_rate;
361
362        bool venc_set_profile_level(OMX_U32 eProfile,OMX_U32 eLevel);
363        bool venc_set_intra_period(OMX_U32 nPFrames, OMX_U32 nBFrames);
364        bool venc_set_target_bitrate(OMX_U32 nTargetBitrate, OMX_U32 config);
365        bool venc_set_ratectrl_cfg(OMX_VIDEO_CONTROLRATETYPE eControlRate);
366        bool venc_set_session_qp(OMX_U32 i_frame_qp, OMX_U32 p_frame_qp,OMX_U32 b_frame_qp);
367        bool venc_set_session_qp_range(OMX_U32 min_qp, OMX_U32 max_qp);
368        bool venc_set_encode_framerate(OMX_U32 encode_framerate, OMX_U32 config);
369        bool venc_set_intra_vop_refresh(OMX_BOOL intra_vop_refresh);
370        bool venc_set_color_format(OMX_COLOR_FORMATTYPE color_format);
371        bool venc_validate_profile_level(OMX_U32 *eProfile, OMX_U32 *eLevel);
372        bool venc_set_multislice_cfg(OMX_INDEXTYPE codec, OMX_U32 slicesize);
373        bool venc_set_entropy_config(OMX_BOOL enable, OMX_U32 i_cabac_level);
374        bool venc_set_inloop_filter(OMX_VIDEO_AVCLOOPFILTERTYPE loop_filter);
375        bool venc_set_intra_refresh (OMX_VIDEO_INTRAREFRESHTYPE intrarefresh, OMX_U32 nMBs);
376        bool venc_set_error_resilience(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE* error_resilience);
377        bool venc_set_voptiming_cfg(OMX_U32 nTimeIncRes);
378        void venc_config_print();
379        bool venc_set_slice_delivery_mode(OMX_U32 enable);
380        bool venc_set_extradata(OMX_U32 extra_data, OMX_BOOL enable);
381        bool venc_set_idr_period(OMX_U32 nPFrames, OMX_U32 nIDRPeriod);
382        bool venc_reconfig_reqbufs();
383        bool venc_set_vpe_rotation(OMX_S32 rotation_angle);
384        bool venc_set_deinterlace(OMX_U32 enable);
385        bool venc_set_ltrmode(OMX_U32 enable, OMX_U32 count);
386	bool venc_enable_initial_qp(QOMX_EXTNINDEX_VIDEO_INITIALQP* initqp);
387        bool venc_set_useltr(OMX_U32 frameIdx);
388        bool venc_set_markltr(OMX_U32 frameIdx);
389        bool venc_set_inband_video_header(OMX_BOOL enable);
390        bool venc_set_au_delimiter(OMX_BOOL enable);
391        bool venc_set_hier_layers(QOMX_VIDEO_HIERARCHICALCODINGTYPE type, OMX_U32 num_layers);
392        bool venc_set_perf_level(QOMX_VIDEO_PERF_LEVEL ePerfLevel);
393        bool venc_set_vui_timing_info(OMX_BOOL enable);
394        bool venc_set_peak_bitrate(OMX_U32 nPeakBitrate);
395        bool venc_set_searchrange();
396        bool venc_set_vpx_error_resilience(OMX_BOOL enable);
397        bool venc_set_perf_mode(OMX_U32 mode);
398        bool venc_set_hybrid_hierp(OMX_U32 layers);
399        bool venc_calibrate_gop();
400        bool venc_validate_hybridhp_params(OMX_U32 layers, OMX_U32 bFrames, OMX_U32 count, int mode);
401        bool venc_set_session_priority(OMX_U32 priority);
402        bool venc_set_operatingrate(OMX_U32 rate);
403
404#ifdef MAX_RES_1080P
405        OMX_U32 pmem_free();
406        OMX_U32 pmem_allocate(OMX_U32 size, OMX_U32 alignment, OMX_U32 count);
407        OMX_U32 venc_allocate_recon_buffers();
408        inline int clip2(int x) {
409            x = x -1;
410            x = x | x >> 1;
411            x = x | x >> 2;
412            x = x | x >> 4;
413            x = x | x >> 16;
414            x = x + 1;
415            return x;
416        }
417#endif
418        int metadatamode;
419        bool streaming[MAX_PORT];
420        bool extradata;
421        struct extradata_buffer_info extradata_info;
422
423        pthread_mutex_t pause_resume_mlock;
424        pthread_cond_t pause_resume_cond;
425        bool paused;
426        int color_format;
427        bool is_searchrange_set;
428        bool enable_mv_narrow_searchrange;
429        int supported_rc_modes;
430        bool camera_mode_enabled;
431};
432
433enum instance_state {
434    MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
435    MSM_VIDC_CORE_INIT,
436    MSM_VIDC_CORE_INIT_DONE,
437    MSM_VIDC_OPEN,
438    MSM_VIDC_OPEN_DONE,
439    MSM_VIDC_LOAD_RESOURCES,
440    MSM_VIDC_LOAD_RESOURCES_DONE,
441    MSM_VIDC_START,
442    MSM_VIDC_START_DONE,
443    MSM_VIDC_STOP,
444    MSM_VIDC_STOP_DONE,
445    MSM_VIDC_RELEASE_RESOURCES,
446    MSM_VIDC_RELEASE_RESOURCES_DONE,
447    MSM_VIDC_CLOSE,
448    MSM_VIDC_CLOSE_DONE,
449    MSM_VIDC_CORE_UNINIT,
450};
451#endif
452
453