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_TYPE:
320 		ctr->intra_refresh_type = ctrl->val;
321 		break;
322 	case V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD:
323 		ctr->intra_refresh_period = ctrl->val;
324 		break;
325 	case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM:
326 		if (ctr->profile.h264 != V4L2_MPEG_VIDEO_H264_PROFILE_HIGH &&
327 		    ctr->profile.h264 != V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_HIGH)
328 			return -EINVAL;
329 
330 		/*
331 		 * In video firmware, 8x8 transform is supported only for
332 		 * high profile(HP) and constrained high profile(CHP).
333 		 * If client wants to disable 8x8 transform for HP/CHP,
334 		 * it is better to set profile as main profile(MP).
335 		 * Because there is no difference between HP and MP
336 		 * if we disable 8x8 transform for HP.
337 		 */
338 
339 		if (ctrl->val == 0)
340 			return -EINVAL;
341 
342 		ctr->h264_8x8_transform = ctrl->val;
343 		break;
344 	default:
345 		return -EINVAL;
346 	}
347 
348 	return 0;
349 }
350 
351 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
352 	.s_ctrl = venc_op_s_ctrl,
353 };
354 
355 int venc_ctrl_init(struct venus_inst *inst)
356 {
357 	int ret;
358 
359 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 58);
360 	if (ret)
361 		return ret;
362 
363 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
364 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
365 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
366 		~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
367 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) |
368 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CQ)),
369 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
370 
371 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
372 		V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
373 		V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
374 		0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
375 
376 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
377 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
378 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY,
379 		~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE) |
380 		  (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE)),
381 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
382 
383 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
384 		V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
385 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
386 		0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0);
387 
388 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
389 		V4L2_CID_MPEG_VIDEO_HEVC_PROFILE,
390 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10,
391 		~((1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN) |
392 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE) |
393 		  (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10)),
394 		V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN);
395 
396 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
397 		V4L2_CID_MPEG_VIDEO_HEVC_LEVEL,
398 		V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2,
399 		0, V4L2_MPEG_VIDEO_HEVC_LEVEL_1);
400 
401 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
402 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
403 		V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
404 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
405 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
406 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
407 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
408 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH) |
409 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH)),
410 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
411 
412 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
413 		V4L2_CID_MPEG_VIDEO_H264_LEVEL,
414 		V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
415 		0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0);
416 
417 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
418 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
419 		AT_SLICE_BOUNDARY,
420 		0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED);
421 
422 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
423 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
424 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
425 		~((1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE) |
426 		(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME)),
427 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME);
428 
429 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
430 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
431 		V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES,
432 		0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
433 
434 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
435 		V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
436 		V4L2_MPEG_VIDEO_VP8_PROFILE_3,
437 		0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
438 
439 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
440 		V4L2_CID_MPEG_VIDEO_BITRATE, BITRATE_MIN, BITRATE_MAX,
441 		BITRATE_STEP, BITRATE_DEFAULT);
442 
443 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
444 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, BITRATE_MIN, BITRATE_MAX,
445 		BITRATE_STEP, BITRATE_DEFAULT_PEAK);
446 
447 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
448 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
449 
450 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
451 			  V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
452 
453 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
454 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
455 
456 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
457 			  V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 1, 51, 1, 1);
458 
459 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
460 			  V4L2_CID_MPEG_VIDEO_H264_I_FRAME_MIN_QP, 1, 51, 1, 1);
461 
462 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
463 			  V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM, 0, 1, 1, 1);
464 
465 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
466 			  V4L2_CID_MPEG_VIDEO_H264_P_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_B_FRAME_MIN_QP, 1, 51, 1, 1);
470 
471 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
472 			  V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 1, 51, 1, 51);
473 
474 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
475 			  V4L2_CID_MPEG_VIDEO_H264_I_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_P_FRAME_MAX_QP, 1, 51, 1, 51);
479 
480 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
481 			  V4L2_CID_MPEG_VIDEO_H264_B_FRAME_MAX_QP, 1, 51, 1, 51);
482 
483 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
484 			  V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP, 1, 63, 1, 26);
485 
486 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
487 			  V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP, 1, 63, 1, 28);
488 
489 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
490 			  V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP, 1, 63, 1, 30);
491 
492 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
493 			  V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP, 1, 63, 1, 1);
494 
495 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
496 			  V4L2_CID_MPEG_VIDEO_HEVC_I_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_P_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_B_FRAME_MIN_QP, 1, 63, 1, 1);
503 
504 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
505 			  V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP, 1, 63, 1, 63);
506 
507 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
508 			  V4L2_CID_MPEG_VIDEO_HEVC_I_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_P_FRAME_MAX_QP, 1, 63, 1, 63);
512 
513 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
514 			  V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_MAX_QP, 1, 63, 1, 63);
515 
516 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
517 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, SLICE_BYTE_SIZE_MIN,
518 		SLICE_BYTE_SIZE_MAX, 1, SLICE_BYTE_SIZE_MIN);
519 
520 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
521 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1,
522 		SLICE_MB_SIZE_MAX, 1, 1);
523 
524 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
525 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
526 
527 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
528 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
529 
530 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
531 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30);
532 
533 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
534 		V4L2_CID_MPEG_VIDEO_VPX_MIN_QP, 1, 128, 1, 1);
535 
536 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
537 		V4L2_CID_MPEG_VIDEO_VPX_MAX_QP, 1, 128, 1, 128);
538 
539 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
540 		V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
541 
542 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
543 		V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, (1 << 16) - 1, 1, 0);
544 
545 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
546 			  V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
547 
548 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
549 			  V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1);
550 
551 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
552 			  V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY, 0, 100, 1, 0);
553 
554 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
555 			       V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE,
556 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT,
557 			       ~((1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED) |
558 			       (1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT)),
559 			       V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED);
560 
561 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
562 			  V4L2_CID_MPEG_VIDEO_BASELAYER_PRIORITY_ID, 0,
563 			  6, 1, 0);
564 
565 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
566 			  V4L2_CID_MPEG_VIDEO_AU_DELIMITER, 0, 1, 1, 0);
567 
568 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
569 			  V4L2_CID_MPEG_VIDEO_USE_LTR_FRAMES, 0,
570 			  ((1 << MAX_LTR_FRAME_COUNT) - 1), 0, 0);
571 
572 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
573 			  V4L2_CID_MPEG_VIDEO_LTR_COUNT, 0,
574 			  MAX_LTR_FRAME_COUNT, 1, 0);
575 
576 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
577 			  V4L2_CID_MPEG_VIDEO_FRAME_LTR_INDEX, 0,
578 			  (MAX_LTR_FRAME_COUNT - 1), 1, 0);
579 
580 	v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops,
581 				   V4L2_CID_COLORIMETRY_HDR10_CLL_INFO,
582 				   v4l2_ctrl_ptr_create(NULL));
583 
584 	v4l2_ctrl_new_std_compound(&inst->ctrl_handler, &venc_ctrl_ops,
585 				   V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY,
586 				   v4l2_ctrl_ptr_create(NULL));
587 
588 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
589 			       V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE,
590 			       V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_CYCLIC,
591 			       0, V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD_TYPE_RANDOM);
592 
593 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
594 			  V4L2_CID_MPEG_VIDEO_INTRA_REFRESH_PERIOD, 0,
595 			  ((4096 * 2304) >> 8), 1, 0);
596 
597 	ret = inst->ctrl_handler.error;
598 	if (ret)
599 		goto err;
600 
601 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
602 	if (ret)
603 		goto err;
604 
605 	return 0;
606 err:
607 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
608 	return ret;
609 }
610 
611 void venc_ctrl_deinit(struct venus_inst *inst)
612 {
613 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
614 }
615