1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2017 Linaro Ltd.
5  */
6 #include <linux/types.h>
7 #include <media/v4l2-ctrls.h>
8 
9 #include "core.h"
10 #include "venc.h"
11 
12 #define BITRATE_MIN		32000
13 #define BITRATE_MAX		160000000
14 #define BITRATE_DEFAULT		1000000
15 #define BITRATE_DEFAULT_PEAK	(BITRATE_DEFAULT * 2)
16 #define BITRATE_STEP		100
17 #define SLICE_BYTE_SIZE_MAX	1024
18 #define SLICE_BYTE_SIZE_MIN	1024
19 #define SLICE_MB_SIZE_MAX	300
20 #define INTRA_REFRESH_MBS_MAX	300
21 #define AT_SLICE_BOUNDARY	\
22 	V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
23 
24 static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf)
25 {
26 	u32 half = (gop_size - 1) >> 1;
27 	u32 b, p, ratio;
28 	bool found = false;
29 
30 	if (!gop_size)
31 		return -EINVAL;
32 
33 	*bf = *pf = 0;
34 
35 	if (!conseq_b) {
36 		*pf = gop_size -  1;
37 		return 0;
38 	}
39 
40 	b = p = half;
41 
42 	for (; b <= gop_size - 1; b++, p--) {
43 		if (b % p)
44 			continue;
45 
46 		ratio = b / p;
47 
48 		if (ratio == conseq_b) {
49 			found = true;
50 			break;
51 		}
52 
53 		if (ratio > conseq_b)
54 			break;
55 	}
56 
57 	if (!found)
58 		return -EINVAL;
59 
60 	if (b + p + 1 != gop_size)
61 		return -EINVAL;
62 
63 	*bf = b;
64 	*pf = p;
65 
66 	return 0;
67 }
68 
69 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
70 {
71 	struct venus_inst *inst = ctrl_to_inst(ctrl);
72 	struct venc_controls *ctr = &inst->controls.enc;
73 	struct hfi_enable en = { .enable = 1 };
74 	struct hfi_bitrate brate;
75 	u32 bframes;
76 	u32 ptype;
77 	int ret;
78 
79 	switch (ctrl->id) {
80 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
81 		ctr->bitrate_mode = ctrl->val;
82 		break;
83 	case V4L2_CID_MPEG_VIDEO_BITRATE:
84 		ctr->bitrate = ctrl->val;
85 		mutex_lock(&inst->lock);
86 		if (inst->streamon_out && inst->streamon_cap) {
87 			ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
88 			brate.bitrate = ctr->bitrate;
89 			brate.layer_id = 0;
90 
91 			ret = hfi_session_set_property(inst, ptype, &brate);
92 			if (ret) {
93 				mutex_unlock(&inst->lock);
94 				return ret;
95 			}
96 		}
97 		mutex_unlock(&inst->lock);
98 		break;
99 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
100 		ctr->bitrate_peak = ctrl->val;
101 		break;
102 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
103 		ctr->h264_entropy_mode = ctrl->val;
104 		break;
105 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
106 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
107 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
108 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
109 		ctr->profile = ctrl->val;
110 		break;
111 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
112 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
113 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
114 		ctr->level = ctrl->val;
115 		break;
116 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
117 		ctr->h264_i_qp = ctrl->val;
118 		break;
119 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
120 		ctr->h264_p_qp = ctrl->val;
121 		break;
122 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
123 		ctr->h264_b_qp = ctrl->val;
124 		break;
125 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
126 		ctr->h264_min_qp = ctrl->val;
127 		break;
128 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
129 		ctr->h264_max_qp = ctrl->val;
130 		break;
131 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
132 		ctr->multi_slice_mode = ctrl->val;
133 		break;
134 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
135 		ctr->multi_slice_max_bytes = ctrl->val;
136 		break;
137 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
138 		ctr->multi_slice_max_mb = ctrl->val;
139 		break;
140 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
141 		ctr->h264_loop_filter_alpha = ctrl->val;
142 		break;
143 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
144 		ctr->h264_loop_filter_beta = ctrl->val;
145 		break;
146 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
147 		ctr->h264_loop_filter_mode = ctrl->val;
148 		break;
149 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
150 		ctr->header_mode = ctrl->val;
151 		break;
152 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
153 		break;
154 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
155 		ret = venc_calc_bpframes(ctrl->val, ctr->num_b_frames, &bframes,
156 					 &ctr->num_p_frames);
157 		if (ret)
158 			return ret;
159 
160 		ctr->gop_size = ctrl->val;
161 		break;
162 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
163 		ctr->h264_i_period = ctrl->val;
164 		break;
165 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
166 		ctr->vp8_min_qp = ctrl->val;
167 		break;
168 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
169 		ctr->vp8_max_qp = ctrl->val;
170 		break;
171 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
172 		ret = venc_calc_bpframes(ctr->gop_size, ctrl->val, &bframes,
173 					 &ctr->num_p_frames);
174 		if (ret)
175 			return ret;
176 
177 		ctr->num_b_frames = bframes;
178 		break;
179 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
180 		mutex_lock(&inst->lock);
181 		if (inst->streamon_out && inst->streamon_cap) {
182 			ptype = HFI_PROPERTY_CONFIG_VENC_REQUEST_SYNC_FRAME;
183 			ret = hfi_session_set_property(inst, ptype, &en);
184 
185 			if (ret) {
186 				mutex_unlock(&inst->lock);
187 				return ret;
188 			}
189 		}
190 		mutex_unlock(&inst->lock);
191 		break;
192 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
193 		ctr->rc_enable = ctrl->val;
194 		break;
195 	case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:
196 		ctr->const_quality = ctrl->val;
197 		break;
198 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
199 		ctr->frame_skip_mode = ctrl->val;
200 		break;
201 	default:
202 		return -EINVAL;
203 	}
204 
205 	return 0;
206 }
207 
208 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
209 	.s_ctrl = venc_op_s_ctrl,
210 };
211 
212 int venc_ctrl_init(struct venus_inst *inst)
213 {
214 	int ret;
215 
216 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 33);
217 	if (ret)
218 		return ret;
219 
220 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
221 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
222 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
223 		~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
224 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) |
225 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)),
226 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
227 
228 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
229 		V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
230 		V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
231 		0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
232 
233 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
234 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
235 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY,
236 		~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE) |
237 		  (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE)),
238 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
239 
240 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
241 		V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
242 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
243 		0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0);
244 
245 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
246 		V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
247 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10,
248 		~((1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN) |
249 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE) |
250 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10)),
251 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN);
252 
253 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
254 		V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
255 		V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2,
256 		0, V4L2_MPEG_VIDEO_HEVC_LEVEL_1);
257 
258 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
259 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
260 		V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
261 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
262 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
263 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
264 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
265 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH) |
266 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH)),
267 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
268 
269 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
270 		V4L2_CID_MPEG_VIDEO_H264_LEVEL,
271 		V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
272 		0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0);
273 
274 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
275 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
276 		AT_SLICE_BOUNDARY,
277 		0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED);
278 
279 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
280 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
281 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
282 		1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
283 		V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
284 
285 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
286 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
287 		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES,
288 		0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
289 
290 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
291 		V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
292 		V4L2_MPEG_VIDEO_VP8_PROFILE_3,
293 		0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
294 
295 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
296 		V4L2_CID_MPEG_VIDEO_BITRATE, BITRATE_MIN, BITRATE_MAX,
297 		BITRATE_STEP, BITRATE_DEFAULT);
298 
299 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
300 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, BITRATE_MIN, BITRATE_MAX,
301 		BITRATE_STEP, BITRATE_DEFAULT_PEAK);
302 
303 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
304 		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
305 
306 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
307 		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
308 
309 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
310 		V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
311 
312 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
313 		V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 1, 51, 1, 1);
314 
315 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
316 		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 1, 51, 1, 51);
317 
318 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
319 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, SLICE_BYTE_SIZE_MIN,
320 		SLICE_BYTE_SIZE_MAX, 1, SLICE_BYTE_SIZE_MIN);
321 
322 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
323 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1,
324 		SLICE_MB_SIZE_MAX, 1, 1);
325 
326 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
327 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
328 
329 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
330 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
331 
332 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
333 		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
334 		0, INTRA_REFRESH_MBS_MAX, 1, 0);
335 
336 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
337 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30);
338 
339 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
340 		V4L2_CID_MPEG_VIDEO_VPX_MIN_QP, 1, 128, 1, 1);
341 
342 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
343 		V4L2_CID_MPEG_VIDEO_VPX_MAX_QP, 1, 128, 1, 128);
344 
345 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
346 		V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
347 
348 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
349 		V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, (1 << 16) - 1, 1, 0);
350 
351 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
352 			  V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
353 
354 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
355 			  V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
356 
357 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
358 			  V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY, 0, 100, 1, 0);
359 
360 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
361 			       V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE,
362 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
363 			       ~((1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED) |
364 			       (1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT)),
365 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED);
366 
367 	ret = inst->ctrl_handler.error;
368 	if (ret)
369 		goto err;
370 
371 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
372 	if (ret)
373 		goto err;
374 
375 	return 0;
376 err:
377 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
378 	return ret;
379 }
380 
381 void venc_ctrl_deinit(struct venus_inst *inst)
382 {
383 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
384 }
385