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 #include "helpers.h"
12 
13 #define BITRATE_MIN		32000
14 #define BITRATE_MAX		160000000
15 #define BITRATE_DEFAULT		1000000
16 #define BITRATE_DEFAULT_PEAK	(BITRATE_DEFAULT * 2)
17 #define BITRATE_STEP		100
18 #define SLICE_BYTE_SIZE_MAX	1024
19 #define SLICE_BYTE_SIZE_MIN	1024
20 #define SLICE_MB_SIZE_MAX	300
21 #define AT_SLICE_BOUNDARY	\
22 	V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
23 #define MAX_LTR_FRAME_COUNT 4
24 
25 static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf)
26 {
27 	u32 half = (gop_size - 1) >> 1;
28 	u32 b, p, ratio;
29 	bool found = false;
30 
31 	if (!gop_size)
32 		return -EINVAL;
33 
34 	*bf = *pf = 0;
35 
36 	if (!conseq_b) {
37 		*pf = gop_size -  1;
38 		return 0;
39 	}
40 
41 	b = p = half;
42 
43 	for (; b <= gop_size - 1; b++, p--) {
44 		if (b % p)
45 			continue;
46 
47 		ratio = b / p;
48 
49 		if (ratio == conseq_b) {
50 			found = true;
51 			break;
52 		}
53 
54 		if (ratio > conseq_b)
55 			break;
56 	}
57 
58 	if (!found)
59 		return -EINVAL;
60 
61 	if (b + p + 1 != gop_size)
62 		return -EINVAL;
63 
64 	*bf = b;
65 	*pf = p;
66 
67 	return 0;
68 }
69 
70 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
71 {
72 	struct venus_inst *inst = ctrl_to_inst(ctrl);
73 	struct venc_controls *ctr = &inst->controls.enc;
74 	struct hfi_enable en = { .enable = 1 };
75 	struct hfi_bitrate brate;
76 	struct hfi_ltr_use ltr_use;
77 	struct hfi_ltr_mark ltr_mark;
78 	u32 bframes;
79 	u32 ptype;
80 	int ret;
81 
82 	switch (ctrl->id) {
83 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
84 		ctr->bitrate_mode = ctrl->val;
85 		break;
86 	case V4L2_CID_MPEG_VIDEO_BITRATE:
87 		ctr->bitrate = ctrl->val;
88 		mutex_lock(&inst->lock);
89 		if (inst->streamon_out && inst->streamon_cap) {
90 			ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
91 			brate.bitrate = ctr->bitrate;
92 			brate.layer_id = 0;
93 
94 			ret = hfi_session_set_property(inst, ptype, &brate);
95 			if (ret) {
96 				mutex_unlock(&inst->lock);
97 				return ret;
98 			}
99 		}
100 		mutex_unlock(&inst->lock);
101 		break;
102 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
103 		ctr->bitrate_peak = ctrl->val;
104 		break;
105 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
106 		ctr->h264_entropy_mode = ctrl->val;
107 		break;
108 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
109 		ctr->profile.mpeg4 = ctrl->val;
110 		break;
111 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
112 		ctr->profile.h264 = ctrl->val;
113 		break;
114 	case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE:
115 		ctr->profile.hevc = ctrl->val;
116 		break;
117 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
118 		ctr->profile.vp8 = ctrl->val;
119 		break;
120 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
121 		ctr->level.mpeg4 = ctrl->val;
122 		break;
123 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
124 		ctr->level.h264 = ctrl->val;
125 		break;
126 	case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL:
127 		ctr->level.hevc = ctrl->val;
128 		break;
129 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP:
130 		ctr->h264_i_qp = ctrl->val;
131 		break;
132 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP:
133 		ctr->h264_p_qp = ctrl->val;
134 		break;
135 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP:
136 		ctr->h264_b_qp = ctrl->val;
137 		break;
138 	case V4L2_CID_MPEG_VIDEO_H264_MIN_QP:
139 		ctr->h264_min_qp = ctrl->val;
140 		break;
141 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP:
142 		ctr->h264_i_min_qp = ctrl->val;
143 		break;
144 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QP:
145 		ctr->h264_p_min_qp = ctrl->val;
146 		break;
147 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QP:
148 		ctr->h264_b_min_qp = ctrl->val;
149 		break;
150 	case V4L2_CID_MPEG_VIDEO_H264_MAX_QP:
151 		ctr->h264_max_qp = ctrl->val;
152 		break;
153 	case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QP:
154 		ctr->h264_i_max_qp = ctrl->val;
155 		break;
156 	case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QP:
157 		ctr->h264_p_max_qp = ctrl->val;
158 		break;
159 	case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP:
160 		ctr->h264_b_max_qp = ctrl->val;
161 		break;
162 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP:
163 		ctr->hevc_i_qp = ctrl->val;
164 		break;
165 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP:
166 		ctr->hevc_p_qp = ctrl->val;
167 		break;
168 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP:
169 		ctr->hevc_b_qp = ctrl->val;
170 		break;
171 	case V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP:
172 		ctr->hevc_min_qp = ctrl->val;
173 		break;
174 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP:
175 		ctr->hevc_i_min_qp = ctrl->val;
176 		break;
177 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MIN_QP:
178 		ctr->hevc_p_min_qp = ctrl->val;
179 		break;
180 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MIN_QP:
181 		ctr->hevc_b_min_qp = ctrl->val;
182 		break;
183 	case V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP:
184 		ctr->hevc_max_qp = ctrl->val;
185 		break;
186 	case V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP:
187 		ctr->hevc_i_max_qp = ctrl->val;
188 		break;
189 	case V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MAX_QP:
190 		ctr->hevc_p_max_qp = ctrl->val;
191 		break;
192 	case V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QP:
193 		ctr->hevc_b_max_qp = ctrl->val;
194 		break;
195 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
196 		ctr->multi_slice_mode = ctrl->val;
197 		break;
198 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
199 		ctr->multi_slice_max_bytes = ctrl->val;
200 		break;
201 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
202 		ctr->multi_slice_max_mb = ctrl->val;
203 		break;
204 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
205 		ctr->h264_loop_filter_alpha = ctrl->val;
206 		break;
207 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
208 		ctr->h264_loop_filter_beta = ctrl->val;
209 		break;
210 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
211 		ctr->h264_loop_filter_mode = ctrl->val;
212 		break;
213 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
214 		ctr->header_mode = ctrl->val;
215 		mutex_lock(&inst->lock);
216 		if (inst->streamon_out && inst->streamon_cap) {
217 			if (ctrl->val == V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)
218 				en.enable = 0;
219 			else
220 				en.enable = 1;
221 			ptype = HFI_PROPERTY_CONFIG_VENC_SYNC_FRAME_SEQUENCE_HEADER;
222 			ret = hfi_session_set_property(inst, ptype, &en);
223 			if (ret) {
224 				mutex_unlock(&inst->lock);
225 				return ret;
226 			}
227 		}
228 		mutex_unlock(&inst->lock);
229 		break;
230 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
231 		ret = venc_calc_bpframes(ctrl->val, ctr->num_b_frames, &bframes,
232 					 &ctr->num_p_frames);
233 		if (ret)
234 			return ret;
235 
236 		ctr->gop_size = ctrl->val;
237 		break;
238 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
239 		ctr->h264_i_period = ctrl->val;
240 		break;
241 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
242 		ctr->vp8_min_qp = ctrl->val;
243 		break;
244 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
245 		ctr->vp8_max_qp = ctrl->val;
246 		break;
247 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
248 		ret = venc_calc_bpframes(ctr->gop_size, ctrl->val, &bframes,
249 					 &ctr->num_p_frames);
250 		if (ret)
251 			return ret;
252 
253 		ctr->num_b_frames = bframes;
254 		break;
255 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
256 		mutex_lock(&inst->lock);
257 		if (inst->streamon_out && inst->streamon_cap) {
258 			ptype = HFI_PROPERTY_CONFIG_VENC_REQUEST_SYNC_FRAME;
259 			ret = hfi_session_set_property(inst, ptype, &en);
260 
261 			if (ret) {
262 				mutex_unlock(&inst->lock);
263 				return ret;
264 			}
265 		}
266 		mutex_unlock(&inst->lock);
267 		break;
268 	case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE:
269 		ctr->rc_enable = ctrl->val;
270 		break;
271 	case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY:
272 		ctr->const_quality = ctrl->val;
273 		break;
274 	case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE:
275 		ctr->frame_skip_mode = ctrl->val;
276 		break;
277 	case V4L2_CID_MPEG_VIDEO_BASELAYER_PRIORITY_ID:
278 		ctr->base_priority_id = ctrl->val;
279 		break;
280 	case V4L2_CID_MPEG_VIDEO_AU_DELIMITER:
281 		ctr->aud_enable = ctrl->val;
282 		break;
283 	case V4L2_CID_MPEG_VIDEO_LTR_COUNT:
284 		ctr->ltr_count = ctrl->val;
285 		break;
286 	case V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX:
287 		mutex_lock(&inst->lock);
288 		if (inst->streamon_out && inst->streamon_cap) {
289 			ptype = HFI_PROPERTY_CONFIG_VENC_MARKLTRFRAME;
290 			ltr_mark.mark_frame = ctrl->val;
291 			ret = hfi_session_set_property(inst, ptype, &ltr_mark);
292 			if (ret) {
293 				mutex_unlock(&inst->lock);
294 				return ret;
295 			}
296 		}
297 		mutex_unlock(&inst->lock);
298 		break;
299 	case V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES:
300 		mutex_lock(&inst->lock);
301 		if (inst->streamon_out && inst->streamon_cap) {
302 			ptype = HFI_PROPERTY_CONFIG_VENC_USELTRFRAME;
303 			ltr_use.ref_ltr = ctrl->val;
304 			ltr_use.use_constrnt = true;
305 			ltr_use.frames = 0;
306 			ret = hfi_session_set_property(inst, ptype, &ltr_use);
307 			if (ret) {
308 				mutex_unlock(&inst->lock);
309 				return ret;
310 			}
311 		}
312 		mutex_unlock(&inst->lock);
313 		break;
314 	case V4L2_CID_COLORIMETRY_HDR10_CLL_INFO:
315 		ctr->cll = *ctrl->p_new.p_hdr10_cll;
316 		break;
317 	case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:
318 		ctr->mastering = *ctrl->p_new.p_hdr10_mastering;
319 		break;
320 	case V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE:
321 		ctr->intra_refresh_type = ctrl->val;
322 		break;
323 	case V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD:
324 		ctr->intra_refresh_period = ctrl->val;
325 		break;
326 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
327 		if (ctr->profile.h264 != V4L2_MPEG_VIDEO_H264_PROFILE_HIGH &&
328 		    ctr->profile.h264 != V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH)
329 			return -EINVAL;
330 
331 		/*
332 		 * In video firmware, 8x8 transform is supported only for
333 		 * high profile(HP) and constrained high profile(CHP).
334 		 * If client wants to disable 8x8 transform for HP/CHP,
335 		 * it is better to set profile as main profile(MP).
336 		 * Because there is no difference between HP and MP
337 		 * if we disable 8x8 transform for HP.
338 		 */
339 
340 
341 		ctr->h264_8x8_transform = ctrl->val;
342 		break;
343 	default:
344 		return -EINVAL;
345 	}
346 
347 	return 0;
348 }
349 
350 static int venc_op_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
351 {
352 	struct venus_inst *inst = ctrl_to_inst(ctrl);
353 	struct hfi_buffer_requirements bufreq;
354 	enum hfi_version ver = inst->core->res->hfi_version;
355 	int ret;
356 
357 	switch (ctrl->id) {
358 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
359 		ret = venus_helper_get_bufreq(inst, HFI_BUFFER_INPUT, &bufreq);
360 		if (!ret)
361 			ctrl->val = HFI_BUFREQ_COUNT_MIN(&bufreq, ver);
362 		break;
363 	default:
364 		return -EINVAL;
365 	}
366 
367 	return 0;
368 }
369 
370 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
371 	.s_ctrl = venc_op_s_ctrl,
372 	.g_volatile_ctrl = venc_op_g_volatile_ctrl,
373 };
374 
375 int venc_ctrl_init(struct venus_inst *inst)
376 {
377 	int ret;
378 	struct v4l2_ctrl_hdr10_mastering_display p_hdr10_mastering = {
379 		{ 34000, 13250, 7500 },
380 		{ 16000, 34500, 3000 }, 15635, 16450, 10000000, 500,
381 	};
382 	struct v4l2_ctrl_hdr10_cll_info p_hdr10_cll = { 1000, 400 };
383 
384 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 59);
385 	if (ret)
386 		return ret;
387 
388 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
389 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
390 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
391 		~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
392 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) |
393 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)),
394 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
395 
396 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
397 		V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
398 		V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
399 		0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
400 
401 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
402 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
403 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY,
404 		~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE) |
405 		  (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE)),
406 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
407 
408 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
409 		V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
410 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
411 		0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0);
412 
413 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
414 		V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
415 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10,
416 		~((1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN) |
417 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE) |
418 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10)),
419 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN);
420 
421 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
422 		V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
423 		V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2,
424 		0, V4L2_MPEG_VIDEO_HEVC_LEVEL_1);
425 
426 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
427 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
428 		V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
429 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
430 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
431 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
432 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
433 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH) |
434 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH)),
435 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
436 
437 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
438 		V4L2_CID_MPEG_VIDEO_H264_LEVEL,
439 		V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
440 		0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0);
441 
442 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
443 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
444 		AT_SLICE_BOUNDARY,
445 		0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED);
446 
447 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
448 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
449 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
450 		~((1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) |
451 		(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME)),
452 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
453 
454 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
455 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
456 		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES,
457 		0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
458 
459 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
460 		V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
461 		V4L2_MPEG_VIDEO_VP8_PROFILE_3,
462 		0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
463 
464 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
465 			  V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 4, 11, 1, 4);
466 
467 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
468 		V4L2_CID_MPEG_VIDEO_BITRATE, BITRATE_MIN, BITRATE_MAX,
469 		BITRATE_STEP, BITRATE_DEFAULT);
470 
471 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
472 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, BITRATE_MIN, BITRATE_MAX,
473 		BITRATE_STEP, BITRATE_DEFAULT_PEAK);
474 
475 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
476 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
477 
478 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
479 			  V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
480 
481 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
482 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
483 
484 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
485 			  V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 1, 51, 1, 1);
486 
487 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
488 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP, 1, 51, 1, 1);
489 
490 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
491 			  V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM, 0, 1, 1, 1);
492 
493 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
494 			  V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MIN_QP, 1, 51, 1, 1);
495 
496 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
497 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MIN_QP, 1, 51, 1, 1);
498 
499 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
500 			  V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 1, 51, 1, 51);
501 
502 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
503 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MAX_QP, 1, 51, 1, 51);
504 
505 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
506 			  V4L2_CID_MPEG_VIDEO_H264_P_FRAME_MAX_QP, 1, 51, 1, 51);
507 
508 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
509 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP, 1, 51, 1, 51);
510 
511 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
512 			  V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP, 1, 63, 1, 26);
513 
514 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
515 			  V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP, 1, 63, 1, 28);
516 
517 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
518 			  V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP, 1, 63, 1, 30);
519 
520 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
521 			  V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, 1, 63, 1, 1);
522 
523 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
524 			  V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP, 1, 63, 1, 1);
525 
526 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
527 			  V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MIN_QP, 1, 63, 1, 1);
528 
529 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
530 			  V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MIN_QP, 1, 63, 1, 1);
531 
532 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
533 			  V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP, 1, 63, 1, 63);
534 
535 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
536 			  V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP, 1, 63, 1, 63);
537 
538 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
539 			  V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_MAX_QP, 1, 63, 1, 63);
540 
541 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
542 			  V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QP, 1, 63, 1, 63);
543 
544 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
545 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, SLICE_BYTE_SIZE_MIN,
546 		SLICE_BYTE_SIZE_MAX, 1, SLICE_BYTE_SIZE_MIN);
547 
548 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
549 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1,
550 		SLICE_MB_SIZE_MAX, 1, 1);
551 
552 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
553 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
554 
555 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
556 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
557 
558 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
559 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30);
560 
561 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
562 		V4L2_CID_MPEG_VIDEO_VPX_MIN_QP, 1, 128, 1, 1);
563 
564 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
565 		V4L2_CID_MPEG_VIDEO_VPX_MAX_QP, 1, 128, 1, 128);
566 
567 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
568 		V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
569 
570 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
571 		V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, (1 << 16) - 1, 1, 0);
572 
573 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
574 			  V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
575 
576 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
577 			  V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
578 
579 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
580 			  V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY, 0, 100, 1, 0);
581 
582 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
583 			       V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE,
584 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
585 			       ~((1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED) |
586 			       (1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT)),
587 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED);
588 
589 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
590 			  V4L2_CID_MPEG_VIDEO_BASELAYER_PRIORITY_ID, 0,
591 			  6, 1, 0);
592 
593 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
594 			  V4L2_CID_MPEG_VIDEO_AU_DELIMITER, 0, 1, 1, 0);
595 
596 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
597 			  V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES, 0,
598 			  ((1 << MAX_LTR_FRAME_COUNT) - 1), 0, 0);
599 
600 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
601 			  V4L2_CID_MPEG_VIDEO_LTR_COUNT, 0,
602 			  MAX_LTR_FRAME_COUNT, 1, 0);
603 
604 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
605 			  V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX, 0,
606 			  (MAX_LTR_FRAME_COUNT - 1), 1, 0);
607 
608 	v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops,
609 				   V4L2_CID_COLORIMETRY_HDR10_CLL_INFO,
610 				   v4l2_ctrl_ptr_create(&p_hdr10_cll));
611 
612 	v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops,
613 				   V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY,
614 				   v4l2_ctrl_ptr_create((void *)&p_hdr10_mastering));
615 
616 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
617 			       V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE,
618 			       V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC,
619 			       0, V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_RANDOM);
620 
621 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
622 			  V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD, 0,
623 			  ((4096 * 2304) >> 8), 1, 0);
624 
625 	ret = inst->ctrl_handler.error;
626 	if (ret)
627 		goto err;
628 
629 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
630 	if (ret)
631 		goto err;
632 
633 	return 0;
634 err:
635 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
636 	return ret;
637 }
638 
639 void venc_ctrl_deinit(struct venus_inst *inst)
640 {
641 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
642 }
643