1/*--------------------------------------------------------------------------
2Copyright (c) 2012-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
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
45
46struct msm_venc_switch {
47    unsigned char    status;
48};
49
50struct msm_venc_allocatorproperty {
51    unsigned long     mincount;
52    unsigned long     actualcount;
53    unsigned long     datasize;
54    unsigned long     suffixsize;
55    unsigned long     alignment;
56    unsigned long     bufpoolid;
57};
58
59struct msm_venc_basecfg {
60    unsigned long    input_width;
61    unsigned long    input_height;
62    unsigned long    dvs_width;
63    unsigned long    dvs_height;
64    unsigned long    codectype;
65    unsigned long    fps_num;
66    unsigned long    fps_den;
67    unsigned long    targetbitrate;
68    unsigned long    inputformat;
69};
70
71struct msm_venc_profile {
72    unsigned long    profile;
73};
74struct msm_venc_profilelevel {
75    unsigned long    level;
76};
77
78struct msm_venc_sessionqp {
79    unsigned long    iframeqp;
80    unsigned long    pframqp;
81    unsigned long    bframqp;
82};
83
84struct msm_venc_qprange {
85    unsigned long    maxqp;
86    unsigned long    minqp;
87};
88struct msm_venc_intraperiod {
89    unsigned long    num_pframes;
90    unsigned long    num_bframes;
91};
92struct msm_venc_seqheader {
93    unsigned char *hdrbufptr;
94    unsigned long    bufsize;
95    unsigned long    hdrlen;
96};
97
98struct msm_venc_capability {
99    unsigned long    codec_types;
100    unsigned long    maxframe_width;
101    unsigned long    maxframe_height;
102    unsigned long    maxtarget_bitrate;
103    unsigned long    maxframe_rate;
104    unsigned long    input_formats;
105    unsigned char    dvs;
106};
107
108struct msm_venc_entropycfg {
109    unsigned longentropysel;
110    unsigned long    cabacmodel;
111};
112
113struct msm_venc_dbcfg {
114    unsigned long    db_mode;
115    unsigned long    slicealpha_offset;
116    unsigned long    slicebeta_offset;
117};
118
119struct msm_venc_intrarefresh {
120    unsigned long    irmode;
121    unsigned long    mbcount;
122};
123
124struct msm_venc_multiclicecfg {
125    unsigned long    mslice_mode;
126    unsigned long    mslice_size;
127};
128
129struct msm_venc_bufferflush {
130    unsigned long    flush_mode;
131};
132
133struct msm_venc_ratectrlcfg {
134    unsigned long    rcmode;
135};
136
137struct    msm_venc_voptimingcfg {
138    unsigned long    voptime_resolution;
139};
140struct msm_venc_framerate {
141    unsigned long    fps_denominator;
142    unsigned long    fps_numerator;
143};
144
145struct msm_venc_targetbitrate {
146    unsigned long    target_bitrate;
147};
148
149
150struct msm_venc_rotation {
151    unsigned long    rotation;
152};
153
154struct msm_venc_timeout {
155    unsigned long    millisec;
156};
157
158struct msm_venc_headerextension {
159    unsigned long    header_extension;
160};
161
162struct msm_venc_video_capability {
163    unsigned int min_width;
164    unsigned int max_width;
165    unsigned int min_height;
166    unsigned int max_height;
167};
168
169struct msm_venc_idrperiod {
170    unsigned long idrperiod;
171};
172
173struct msm_venc_slice_delivery {
174    unsigned long enable;
175};
176
177enum v4l2_ports {
178    CAPTURE_PORT,
179    OUTPUT_PORT,
180    MAX_PORT
181};
182
183struct extradata_buffer_info {
184    int buffer_size;
185    char* uaddr;
186    int count;
187    int size;
188    int allocated;
189#ifdef USE_ION
190    struct venc_ion ion;
191#endif
192};
193
194class venc_dev
195{
196    public:
197        venc_dev(class omx_venc *venc_class); //constructor
198        ~venc_dev(); //des
199
200        static void* async_venc_message_thread (void *);
201        bool venc_open(OMX_U32);
202        void venc_close();
203        unsigned venc_stop(void);
204        unsigned venc_pause(void);
205        unsigned venc_start(void);
206        unsigned venc_flush(unsigned);
207#ifdef _ANDROID_ICS_
208        bool venc_set_meta_mode(bool);
209#endif
210        unsigned venc_resume(void);
211        unsigned venc_start_done(void);
212        unsigned venc_stop_done(void);
213        unsigned venc_set_message_thread_id(pthread_t);
214        bool venc_use_buf(void*, unsigned,unsigned);
215        bool venc_free_buf(void*, unsigned);
216        bool venc_empty_buf(void *, void *,unsigned,unsigned);
217        bool venc_fill_buf(void *, void *,unsigned,unsigned);
218
219        bool venc_get_buf_req(unsigned long *,unsigned long *,
220                unsigned long *,unsigned long);
221        bool venc_set_buf_req(unsigned long *,unsigned long *,
222                unsigned long *,unsigned long);
223        bool venc_set_param(void *,OMX_INDEXTYPE);
224        bool venc_set_config(void *configData, OMX_INDEXTYPE index);
225        bool venc_get_profile_level(OMX_U32 *eProfile,OMX_U32 *eLevel);
226        bool venc_get_seq_hdr(void *, unsigned, unsigned *);
227        bool venc_loaded_start(void);
228        bool venc_loaded_stop(void);
229        bool venc_loaded_start_done(void);
230        bool venc_loaded_stop_done(void);
231        bool venc_is_video_session_supported(unsigned long width, unsigned long height);
232        bool venc_color_align(OMX_BUFFERHEADERTYPE *buffer, OMX_U32 width,
233                        OMX_U32 height);
234
235        OMX_U32 m_nDriver_fd;
236        bool m_profile_set;
237        bool m_level_set;
238        int num_planes;
239        int etb, ebd, ftb, fbd;
240        struct recon_buffer {
241            unsigned char* virtual_address;
242            int pmem_fd;
243            int size;
244            int alignment;
245            int offset;
246#ifdef USE_ION
247            int ion_device_fd;
248            struct ion_allocation_data alloc_data;
249            struct ion_fd_data ion_alloc_fd;
250#endif
251        };
252
253        int stopped;
254        int resume_in_stopped;
255        bool m_max_allowed_bitrate_check;
256        pthread_t m_tid;
257        bool async_thread_created;
258        class omx_venc *venc_handle;
259        OMX_ERRORTYPE allocate_extradata();
260        void free_extradata();
261        bool handle_extradata(void *, int);
262        int venc_set_format(int);
263    private:
264        struct msm_venc_basecfg             m_sVenc_cfg;
265        struct msm_venc_ratectrlcfg         rate_ctrl;
266        struct msm_venc_targetbitrate       bitrate;
267        struct msm_venc_intraperiod         intra_period;
268        struct msm_venc_profile             codec_profile;
269        struct msm_venc_profilelevel        profile_level;
270        struct msm_venc_switch              set_param;
271        struct msm_venc_voptimingcfg        time_inc;
272        struct msm_venc_allocatorproperty   m_sInput_buff_property;
273        struct msm_venc_allocatorproperty   m_sOutput_buff_property;
274        struct msm_venc_sessionqp           session_qp;
275        struct msm_venc_multiclicecfg       multislice;
276        struct msm_venc_entropycfg          entropy;
277        struct msm_venc_dbcfg               dbkfilter;
278        struct msm_venc_intrarefresh        intra_refresh;
279        struct msm_venc_headerextension     hec;
280        struct msm_venc_voptimingcfg        voptimecfg;
281        struct msm_venc_video_capability    capability;
282        struct msm_venc_idrperiod           idrperiod;
283        struct msm_venc_slice_delivery      slice_mode;
284
285        bool venc_set_profile_level(OMX_U32 eProfile,OMX_U32 eLevel);
286        bool venc_set_intra_period(OMX_U32 nPFrames, OMX_U32 nBFrames);
287        bool venc_set_target_bitrate(OMX_U32 nTargetBitrate, OMX_U32 config);
288        bool venc_set_ratectrl_cfg(OMX_VIDEO_CONTROLRATETYPE eControlRate);
289        bool venc_set_session_qp(OMX_U32 i_frame_qp, OMX_U32 p_frame_qp,OMX_U32 b_frame_qp);
290        bool venc_set_encode_framerate(OMX_U32 encode_framerate, OMX_U32 config);
291        bool venc_set_intra_vop_refresh(OMX_BOOL intra_vop_refresh);
292        bool venc_set_color_format(OMX_COLOR_FORMATTYPE color_format);
293        bool venc_validate_profile_level(OMX_U32 *eProfile, OMX_U32 *eLevel);
294        bool venc_set_multislice_cfg(OMX_INDEXTYPE codec, OMX_U32 slicesize);
295        bool venc_set_entropy_config(OMX_BOOL enable, OMX_U32 i_cabac_level);
296        bool venc_set_inloop_filter(OMX_VIDEO_AVCLOOPFILTERTYPE loop_filter);
297        bool venc_set_intra_refresh (OMX_VIDEO_INTRAREFRESHTYPE intrarefresh, OMX_U32 nMBs);
298        bool venc_set_error_resilience(OMX_VIDEO_PARAM_ERRORCORRECTIONTYPE* error_resilience);
299        bool venc_set_voptiming_cfg(OMX_U32 nTimeIncRes);
300        void venc_config_print();
301        bool venc_set_slice_delivery_mode(OMX_U32 enable);
302        bool venc_set_extradata(OMX_U32 extra_data);
303        bool venc_set_idr_period(OMX_U32 nPFrames, OMX_U32 nIDRPeriod);
304        bool venc_reconfig_reqbufs();
305
306#ifdef MAX_RES_1080P
307        OMX_U32 pmem_free();
308        OMX_U32 pmem_allocate(OMX_U32 size, OMX_U32 alignment, OMX_U32 count);
309        OMX_U32 venc_allocate_recon_buffers();
310        inline int clip2(int x) {
311            x = x -1;
312            x = x | x >> 1;
313            x = x | x >> 2;
314            x = x | x >> 4;
315            x = x | x >> 16;
316            x = x + 1;
317            return x;
318        }
319#endif
320        int metadatamode;
321        bool streaming[MAX_PORT];
322        bool extradata;
323        struct extradata_buffer_info extradata_info;
324
325        pthread_mutex_t pause_resume_mlock;
326        pthread_cond_t pause_resume_cond;
327        bool paused;
328        int color_format;
329};
330
331enum instance_state {
332    MSM_VIDC_CORE_UNINIT_DONE = 0x0001,
333    MSM_VIDC_CORE_INIT,
334    MSM_VIDC_CORE_INIT_DONE,
335    MSM_VIDC_OPEN,
336    MSM_VIDC_OPEN_DONE,
337    MSM_VIDC_LOAD_RESOURCES,
338    MSM_VIDC_LOAD_RESOURCES_DONE,
339    MSM_VIDC_START,
340    MSM_VIDC_START_DONE,
341    MSM_VIDC_STOP,
342    MSM_VIDC_STOP_DONE,
343    MSM_VIDC_RELEASE_RESOURCES,
344    MSM_VIDC_RELEASE_RESOURCES_DONE,
345    MSM_VIDC_CLOSE,
346    MSM_VIDC_CLOSE_DONE,
347    MSM_VIDC_CORE_UNINIT,
348};
349#endif
350
351