1 /**************************************************************************
2  *
3  * Copyright 2013 Advanced Micro Devices, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #ifndef VID_ENC_COMMON_H
29 #define VID_ENC_COMMON_H
30 
31 #include "util/list.h"
32 #include "util/u_memory.h"
33 
34 #include "vl/vl_defines.h"
35 #include "vl/vl_compositor.h"
36 #include "vl/vl_winsys.h"
37 
38 #include <OMX_Core.h>
39 
40 #define OMX_VID_ENC_BITRATE_MIN 64000
41 #define OMX_VID_ENC_BITRATE_MEDIAN 2000000
42 #define OMX_VID_ENC_BITRATE_MAX 240000000
43 #define OMX_VID_ENC_CONTROL_FRAME_RATE_DEN_DEFAULT 1001
44 #define OMX_VID_ENC_QUANT_I_FRAMES_DEFAULT 0x1c
45 #define OMX_VID_ENC_QUANT_P_FRAMES_DEFAULT 0x1c
46 #define OMX_VID_ENC_QUANT_B_FRAMES_DEFAULT 0x1c
47 #define OMX_VID_ENC_SCALING_WIDTH_DEFAULT 0xffffffff
48 #define OMX_VID_ENC_SCALING_HEIGHT_DEFAULT 0xffffffff
49 #define OMX_VID_ENC_IDR_PERIOD_DEFAULT 1000
50 #define OMX_VID_ENC_P_PERIOD_DEFAULT 3
51 
52 #define OMX_VID_ENC_NUM_SCALING_BUFFERS 4
53 
54 struct encode_task {
55    struct list_head list;
56 
57    struct pipe_video_buffer *buf;
58    unsigned pic_order_cnt;
59    struct pipe_resource *bitstream;
60    void *feedback;
61 };
62 
63 struct input_buf_private {
64    struct list_head tasks;
65 
66    struct pipe_resource *resource;
67    struct pipe_transfer *transfer;
68 };
69 
70 struct output_buf_private {
71    struct pipe_resource *bitstream;
72    struct pipe_transfer *transfer;
73 };
74 
75 #if ENABLE_ST_OMX_BELLAGIO
76 
77 #include <bellagio/omx_base_filter.h>
78 
79 DERIVEDCLASS(vid_enc_PrivateType, omx_base_filter_PrivateType)
80 #define vid_enc_PrivateType_FIELDS omx_base_filter_PrivateType_FIELDS \
81 	struct vl_screen *screen; \
82 	struct pipe_context *s_pipe; \
83 	struct pipe_context *t_pipe; \
84 	struct pipe_video_codec *codec; \
85 	struct list_head free_tasks; \
86 	struct list_head used_tasks; \
87 	struct list_head b_frames; \
88 	struct list_head stacked_tasks; \
89 	OMX_U32 frame_rate; \
90 	OMX_U32 frame_num; \
91 	OMX_U32 pic_order_cnt; \
92 	OMX_U32 ref_idx_l0, ref_idx_l1; \
93 	OMX_BOOL restricted_b_frames; \
94 	OMX_VIDEO_PARAM_BITRATETYPE bitrate; \
95 	OMX_VIDEO_PARAM_QUANTIZATIONTYPE quant; \
96 	OMX_VIDEO_PARAM_PROFILELEVELTYPE profile_level; \
97 	OMX_CONFIG_INTRAREFRESHVOPTYPE force_pic_type; \
98 	struct vl_compositor compositor; \
99 	struct vl_compositor_state cstate; \
100 	struct pipe_video_buffer *scale_buffer[OMX_VID_ENC_NUM_SCALING_BUFFERS]; \
101 	OMX_CONFIG_SCALEFACTORTYPE scale; \
102 	OMX_U32 current_scale_buffer; \
103 	OMX_U32 stacked_frames_num; \
104 	void *copy_y_shader; \
105 	void *copy_uv_shader;
106 ENDCLASS(vid_enc_PrivateType)
107 
108 #else
109 
110 #include <tizprc_decls.h>
111 #include <tizport_decls.h>
112 
113 typedef struct h264e_prc_class h264e_prc_class_t;
114 struct h264e_prc_class
115 {
116    /* Class */
117    const tiz_prc_class_t _;
118    /* NOTE: Class methods might be added in the future */
119 };
120 
121 typedef struct h264e_prc vid_enc_PrivateType;
122 struct h264e_prc
123 {
124    /* Object */
125    const tiz_prc_t _;
126    OMX_BUFFERHEADERTYPE *p_inhdr_;
127    OMX_BUFFERHEADERTYPE *p_outhdr_;
128    OMX_PARAM_PORTDEFINITIONTYPE in_port_def_;
129    OMX_PARAM_PORTDEFINITIONTYPE out_port_def_;
130    struct vl_screen *screen;
131    struct pipe_context *s_pipe;
132    struct pipe_context *t_pipe;
133    struct pipe_video_codec *codec;
134    struct list_head free_tasks;
135    struct list_head used_tasks;
136    struct list_head b_frames;
137    struct list_head stacked_tasks;
138    OMX_U32 frame_rate;
139    OMX_U32 frame_num;
140    OMX_U32 pic_order_cnt;
141    OMX_U32 ref_idx_l0, ref_idx_l1;
142    OMX_BOOL restricted_b_frames;
143    OMX_VIDEO_PARAM_BITRATETYPE bitrate;
144    OMX_VIDEO_PARAM_QUANTIZATIONTYPE quant;
145    OMX_VIDEO_PARAM_PROFILELEVELTYPE profile_level;
146    OMX_CONFIG_INTRAREFRESHVOPTYPE force_pic_type;
147    struct vl_compositor compositor;
148    struct vl_compositor_state cstate;
149    struct pipe_video_buffer *scale_buffer[OMX_VID_ENC_NUM_SCALING_BUFFERS];
150    OMX_CONFIG_SCALEFACTORTYPE scale;
151    OMX_U32 current_scale_buffer;
152    OMX_U32 stacked_frames_num;
153    bool eos_;
154    bool in_port_disabled_;
155    bool out_port_disabled_;
156    void *copy_y_shader;
157    void *copy_uv_shader;
158 };
159 #endif
160 
161 void enc_ReleaseTasks(struct list_head *head);
162 void enc_MoveTasks(struct list_head *from, struct list_head *to);
163 enum pipe_video_profile enc_TranslateOMXProfileToPipe(unsigned omx_profile);
164 unsigned enc_TranslateOMXLevelToPipe(unsigned omx_level);
165 void vid_enc_BufferEncoded_common(vid_enc_PrivateType * priv, OMX_BUFFERHEADERTYPE* input,
166                                   OMX_BUFFERHEADERTYPE* output);
167 struct encode_task *enc_NeedTask_common(vid_enc_PrivateType * priv,
168                                         OMX_VIDEO_PORTDEFINITIONTYPE *def);
169 void enc_ScaleInput_common(vid_enc_PrivateType * priv,
170                                          OMX_VIDEO_PORTDEFINITIONTYPE *def,
171                                          struct pipe_video_buffer **vbuf, unsigned *size);
172 void enc_ControlPicture_common(vid_enc_PrivateType * priv,
173                                struct pipe_h264_enc_picture_desc *picture);
174 void enc_InitCompute_common(vid_enc_PrivateType *priv);
175 void enc_ReleaseCompute_common(vid_enc_PrivateType *priv);
176 OMX_ERRORTYPE enc_LoadImage_common(vid_enc_PrivateType * priv, OMX_VIDEO_PORTDEFINITIONTYPE *def,
177                                    OMX_BUFFERHEADERTYPE *buf,
178                                    struct pipe_video_buffer *vbuf);
179 #endif
180