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