1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2017 Linaro Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15 #include <linux/types.h>
16 #include <media/v4l2-ctrls.h>
17 
18 #include "core.h"
19 #include "venc.h"
20 
21 #define BITRATE_MIN		32000
22 #define BITRATE_MAX		160000000
23 #define BITRATE_DEFAULT		1000000
24 #define BITRATE_DEFAULT_PEAK	(BITRATE_DEFAULT * 2)
25 #define BITRATE_STEP		100
26 #define SLICE_BYTE_SIZE_MAX	1024
27 #define SLICE_BYTE_SIZE_MIN	1024
28 #define SLICE_MB_SIZE_MAX	300
29 #define INTRA_REFRESH_MBS_MAX	300
30 #define AT_SLICE_BOUNDARY	\
31 	V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
32 
33 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
34 {
35 	struct venus_inst *inst = ctrl_to_inst(ctrl);
36 	struct venc_controls *ctr = &inst->controls.enc;
37 
38 	switch (ctrl->id) {
39 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
40 		ctr->bitrate_mode = ctrl->val;
41 		break;
42 	case V4L2_CID_MPEG_VIDEO_BITRATE:
43 		ctr->bitrate = ctrl->val;
44 		break;
45 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
46 		ctr->bitrate_peak = ctrl->val;
47 		break;
48 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
49 		ctr->h264_entropy_mode = ctrl->val;
50 		break;
51 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
52 		ctr->profile.mpeg4 = ctrl->val;
53 		break;
54 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
55 		ctr->profile.h264 = ctrl->val;
56 		break;
57 	case V4L2_CID_MPEG_VIDEO_VPX_PROFILE:
58 		ctr->profile.vpx = ctrl->val;
59 		break;
60 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
61 		ctr->level.mpeg4 = ctrl->val;
62 		break;
63 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
64 		ctr->level.h264 = ctrl->val;
65 		break;
66 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
67 		ctr->h264_i_qp = ctrl->val;
68 		break;
69 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
70 		ctr->h264_p_qp = ctrl->val;
71 		break;
72 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
73 		ctr->h264_b_qp = ctrl->val;
74 		break;
75 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
76 		ctr->h264_min_qp = ctrl->val;
77 		break;
78 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
79 		ctr->h264_max_qp = ctrl->val;
80 		break;
81 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
82 		ctr->multi_slice_mode = ctrl->val;
83 		break;
84 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
85 		ctr->multi_slice_max_bytes = ctrl->val;
86 		break;
87 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
88 		ctr->multi_slice_max_mb = ctrl->val;
89 		break;
90 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
91 		ctr->h264_loop_filter_alpha = ctrl->val;
92 		break;
93 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
94 		ctr->h264_loop_filter_beta = ctrl->val;
95 		break;
96 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
97 		ctr->h264_loop_filter_mode = ctrl->val;
98 		break;
99 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
100 		ctr->header_mode = ctrl->val;
101 		break;
102 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
103 		break;
104 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
105 		ctr->gop_size = ctrl->val;
106 		break;
107 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
108 		ctr->h264_i_period = ctrl->val;
109 		break;
110 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
111 		ctr->vp8_min_qp = ctrl->val;
112 		break;
113 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
114 		ctr->vp8_max_qp = ctrl->val;
115 		break;
116 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
117 		ctr->num_b_frames = ctrl->val;
118 		break;
119 	default:
120 		return -EINVAL;
121 	}
122 
123 	return 0;
124 }
125 
126 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
127 	.s_ctrl = venc_op_s_ctrl,
128 };
129 
130 int venc_ctrl_init(struct venus_inst *inst)
131 {
132 	int ret;
133 
134 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 27);
135 	if (ret)
136 		return ret;
137 
138 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
139 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
140 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
141 		~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
142 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
143 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
144 
145 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
146 		V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
147 		V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
148 		0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
149 
150 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
151 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
152 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY,
153 		~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE) |
154 		  (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE)),
155 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
156 
157 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
158 		V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
159 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
160 		0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0);
161 
162 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
163 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
164 		V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
165 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
166 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
167 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
168 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
169 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH) |
170 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH)),
171 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
172 
173 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
174 		V4L2_CID_MPEG_VIDEO_H264_LEVEL,
175 		V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
176 		0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0);
177 
178 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
179 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
180 		AT_SLICE_BOUNDARY,
181 		0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED);
182 
183 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
184 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
185 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
186 		1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
187 		V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
188 
189 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
190 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
191 		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
192 		0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
193 
194 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
195 		V4L2_CID_MPEG_VIDEO_BITRATE, BITRATE_MIN, BITRATE_MAX,
196 		BITRATE_STEP, BITRATE_DEFAULT);
197 
198 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
199 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, BITRATE_MIN, BITRATE_MAX,
200 		BITRATE_STEP, BITRATE_DEFAULT_PEAK);
201 
202 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
203 		V4L2_CID_MPEG_VIDEO_VPX_PROFILE, 0, 3, 1, 0);
204 
205 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
206 		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
207 
208 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
209 		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
210 
211 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
212 		V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
213 
214 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
215 		V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 1, 51, 1, 1);
216 
217 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
218 		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 1, 51, 1, 51);
219 
220 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
221 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, SLICE_BYTE_SIZE_MIN,
222 		SLICE_BYTE_SIZE_MAX, 1, SLICE_BYTE_SIZE_MIN);
223 
224 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
225 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1,
226 		SLICE_MB_SIZE_MAX, 1, 1);
227 
228 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
229 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
230 
231 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
232 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
233 
234 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
235 		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
236 		0, INTRA_REFRESH_MBS_MAX, 1, 0);
237 
238 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
239 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 12);
240 
241 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
242 		V4L2_CID_MPEG_VIDEO_VPX_MIN_QP, 1, 128, 1, 1);
243 
244 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
245 		V4L2_CID_MPEG_VIDEO_VPX_MAX_QP, 1, 128, 1, 128);
246 
247 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
248 		V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
249 
250 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
251 		V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, (1 << 16) - 1, 1, 0);
252 
253 	ret = inst->ctrl_handler.error;
254 	if (ret)
255 		goto err;
256 
257 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
258 	if (ret)
259 		goto err;
260 
261 	return 0;
262 err:
263 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
264 	return ret;
265 }
266 
267 void venc_ctrl_deinit(struct venus_inst *inst)
268 {
269 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
270 }
271