1/*--------------------------------------------------------------------------
2Copyright (c) 2010-2013, 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#ifndef __OMX_VENC_DEV__
29#define __OMX_VENC_DEV__
30
31#include "OMX_Types.h"
32#include "OMX_Core.h"
33#include "OMX_QCOMExtns.h"
34#include "qc_omx_component.h"
35#include "omx_video_common.h"
36#include <linux/msm_vidc_enc.h>
37#include <pthread.h>
38#include <linux/videodev2.h>
39#include <poll.h>
40#define TIMEOUT 5000
41#define MAX_RECON_BUFFERS 4
42
43void* async_venc_message_thread (void *);
44
45class venc_dev
46{
47    public:
48        venc_dev(class omx_venc *venc_class); //constructor
49        ~venc_dev(); //des
50
51        bool venc_open(OMX_U32);
52        void venc_close();
53        unsigned venc_stop(void);
54        unsigned venc_pause(void);
55        unsigned venc_start(void);
56        unsigned venc_flush(unsigned);
57#ifdef _ANDROID_ICS_
58        bool venc_set_meta_mode(bool);
59#endif
60        unsigned venc_resume(void);
61        unsigned venc_start_done(void);
62        unsigned venc_set_message_thread_id(pthread_t);
63        bool venc_use_buf(void*, unsigned,unsigned);
64        bool venc_free_buf(void*, unsigned);
65        bool venc_empty_buf(void *, void *,unsigned,unsigned);
66        bool venc_fill_buf(void *, void *,unsigned,unsigned);
67
68        bool venc_get_buf_req(unsigned long *,unsigned long *,
69                unsigned long *,unsigned long);
70        bool venc_set_buf_req(unsigned long *,unsigned long *,
71                unsigned long *,unsigned long);
72        bool venc_set_param(void *,OMX_INDEXTYPE);
73        bool venc_set_config(void *configData, OMX_INDEXTYPE index);
74        bool venc_get_profile_level(OMX_U32 *eProfile,OMX_U32 *eLevel);
75        bool venc_max_allowed_bitrate_check(OMX_U32 nTargetBitrate);
76        bool venc_get_seq_hdr(void *, unsigned, unsigned *);
77        bool venc_loaded_start(void);
78        bool venc_loaded_stop(void);
79        bool venc_loaded_start_done(void);
80        bool venc_loaded_stop_done(void);
81        bool venc_get_output_log_flag();
82        int venc_output_log_buffers(const char *buffer_addr, int buffer_len);
83        int venc_input_log_buffers(OMX_BUFFERHEADERTYPE *buffer, void* pmem_data_buf, int framelen);
84        int venc_extradata_log_buffers(char *buffer_addr);
85        bool venc_get_capability_ltrcount(OMX_U32 *, OMX_U32 *, OMX_U32 *);
86        OMX_U32 m_nDriver_fd;
87        bool m_profile_set;
88        bool m_level_set;
89        pthread_mutex_t loaded_start_stop_mlock;
90        pthread_cond_t loaded_start_stop_cond;
91        struct venc_debug_cap m_debug;
92
93        struct recon_buffer {
94            unsigned char* virtual_address;
95            int pmem_fd;
96            int size;
97            int alignment;
98            int offset;
99#ifdef USE_ION
100            int ion_device_fd;
101            struct ion_allocation_data alloc_data;
102            struct ion_fd_data ion_alloc_fd;
103#endif
104        };
105
106        recon_buffer recon_buff[MAX_RECON_BUFFERS];
107        int recon_buffers_count;
108        bool m_max_allowed_bitrate_check;
109        int m_eProfile;
110        int m_eLevel;
111        int etb_count;
112    private:
113        struct venc_basecfg             m_sVenc_cfg;
114        struct venc_ratectrlcfg         rate_ctrl;
115        struct venc_targetbitrate       bitrate;
116        struct venc_intraperiod         intra_period;
117        struct venc_profile             codec_profile;
118        struct ven_profilelevel         profile_level;
119        struct venc_switch              set_param;
120        struct venc_voptimingcfg        time_inc;
121        struct venc_allocatorproperty   m_sInput_buff_property;
122        struct venc_allocatorproperty   m_sOutput_buff_property;
123        struct venc_sessionqp           session_qp;
124        struct venc_qprange             qp_range;
125        struct venc_multiclicecfg       multislice;
126        struct venc_entropycfg          entropy;
127        struct venc_dbcfg               dbkfilter;
128        struct venc_intrarefresh        intra_refresh;
129        struct venc_headerextension     hec;
130        struct venc_voptimingcfg        voptimecfg;
131        struct venc_seqheader           seqhdr;
132        struct venc_ltrmode             ltrmode;
133        struct venc_ltrcount            ltrcount;
134        struct venc_ltrperiod           ltrperiod;
135
136        bool venc_set_profile_level(OMX_U32 eProfile,OMX_U32 eLevel);
137        bool venc_set_intra_period(OMX_U32 nPFrames, OMX_U32 nBFrames);
138        bool venc_set_target_bitrate(OMX_U32 nTargetBitrate, OMX_U32 config);
139        bool venc_set_ratectrl_cfg(OMX_VIDEO_CONTROLRATETYPE eControlRate);
140        bool venc_set_qp_range(OMX_U32 min_qp, OMX_U32 max_qp);
141        bool venc_set_session_qp(OMX_U32 i_frame_qp, OMX_U32 p_frame_qp);
142        bool venc_set_extradata(OMX_U32 extra_data);
143        bool venc_set_encode_framerate(OMX_U32 encode_framerate, OMX_U32 config);
144        bool venc_set_intra_vop_refresh(OMX_BOOL intra_vop_refresh);
145        bool venc_set_color_format(OMX_COLOR_FORMATTYPE color_format);
146        bool venc_validate_profile_level(OMX_U32 *eProfile, OMX_U32 *eLevel);
147        bool venc_set_multislice_cfg(OMX_INDEXTYPE codec, OMX_U32 slicesize);
148        bool venc_set_entropy_config(OMX_BOOL enable, OMX_U32 i_cabac_level);
149        bool venc_set_inloop_filter(OMX_VIDEO_AVCLOOPFILTERTYPE loop_filter);
150        bool venc_set_intra_refresh (OMX_VIDEO_INTRAREFRESHTYPE intrarefresh, OMX_U32 nMBs);
151        bool venc_set_error_resilience(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE* error_resilience);
152        bool venc_set_voptiming_cfg(OMX_U32 nTimeIncRes);
153        void venc_config_print();
154        bool venc_set_slice_delivery_mode(OMX_BOOL enable);
155        bool venc_set_plusptype(OMX_BOOL enable);
156        bool venc_set_ltrmode(QOMX_VIDEO_LTRMODETYPE mode);
157        bool venc_set_ltrcount(OMX_U32 count);
158        bool venc_set_ltrperiod(OMX_U32 period);
159        bool venc_set_ltruse(OMX_U32 id, OMX_U32 frames);
160        bool venc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width, OMX_U32 height);
161#ifdef MAX_RES_1080P
162        OMX_U32 pmem_free();
163        OMX_U32 pmem_allocate(OMX_U32 size, OMX_U32 alignment, OMX_U32 count);
164        OMX_U32 venc_allocate_recon_buffers();
165        inline int clip2(int x) {
166            x = x -1;
167            x = x | x >> 1;
168            x = x | x >> 2;
169            x = x | x >> 4;
170            x = x | x >> 16;
171            x = x + 1;
172            return x;
173        }
174#endif
175};
176
177#endif
178