1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2017 Linaro Ltd.
6 #include <linux/types.h>
7 #include <media/v4l2-ctrls.h>
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
24 static int venc_calc_bpframes(u32 gop_size
, u32 conseq_b
, u32
*bf
, u32
*pf
)
26 u32 half
= (gop_size
- 1) >> 1;
42 for (; b
<= gop_size
- 1; b
++, p
--) {
48 if (ratio
== conseq_b
) {
60 if (b
+ p
+ 1 != gop_size
)
69 static int venc_op_s_ctrl(struct v4l2_ctrl
*ctrl
)
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
;
80 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
81 ctr
->bitrate_mode
= ctrl
->val
;
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
;
91 ret
= hfi_session_set_property(inst
, ptype
, &brate
);
93 mutex_unlock(&inst
->lock
);
97 mutex_unlock(&inst
->lock
);
99 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
100 ctr
->bitrate_peak
= ctrl
->val
;
102 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
:
103 ctr
->h264_entropy_mode
= ctrl
->val
;
105 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
:
106 ctr
->profile
.mpeg4
= ctrl
->val
;
108 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
109 ctr
->profile
.h264
= ctrl
->val
;
111 case V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
:
112 ctr
->profile
.hevc
= ctrl
->val
;
114 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE
:
115 ctr
->profile
.vp8
= ctrl
->val
;
117 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
:
118 ctr
->level
.mpeg4
= ctrl
->val
;
120 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
121 ctr
->level
.h264
= ctrl
->val
;
123 case V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
:
124 ctr
->level
.hevc
= ctrl
->val
;
126 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
127 ctr
->h264_i_qp
= ctrl
->val
;
129 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
130 ctr
->h264_p_qp
= ctrl
->val
;
132 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
:
133 ctr
->h264_b_qp
= ctrl
->val
;
135 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
136 ctr
->h264_min_qp
= ctrl
->val
;
138 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
139 ctr
->h264_max_qp
= ctrl
->val
;
141 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
142 ctr
->multi_slice_mode
= ctrl
->val
;
144 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
145 ctr
->multi_slice_max_bytes
= ctrl
->val
;
147 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
148 ctr
->multi_slice_max_mb
= ctrl
->val
;
150 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
151 ctr
->h264_loop_filter_alpha
= ctrl
->val
;
153 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
154 ctr
->h264_loop_filter_beta
= ctrl
->val
;
156 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
157 ctr
->h264_loop_filter_mode
= ctrl
->val
;
159 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
160 ctr
->header_mode
= ctrl
->val
;
162 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
164 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
165 ret
= venc_calc_bpframes(ctrl
->val
, ctr
->num_b_frames
, &bframes
,
170 ctr
->gop_size
= ctrl
->val
;
172 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
:
173 ctr
->h264_i_period
= ctrl
->val
;
175 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
:
176 ctr
->vp8_min_qp
= ctrl
->val
;
178 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
:
179 ctr
->vp8_max_qp
= ctrl
->val
;
181 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
182 ret
= venc_calc_bpframes(ctr
->gop_size
, ctrl
->val
, &bframes
,
187 ctr
->num_b_frames
= bframes
;
189 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
:
190 mutex_lock(&inst
->lock
);
191 if (inst
->streamon_out
&& inst
->streamon_cap
) {
192 ptype
= HFI_PROPERTY_CONFIG_VENC_REQUEST_SYNC_FRAME
;
193 ret
= hfi_session_set_property(inst
, ptype
, &en
);
196 mutex_unlock(&inst
->lock
);
200 mutex_unlock(&inst
->lock
);
202 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
:
203 ctr
->rc_enable
= ctrl
->val
;
205 case V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY
:
206 ctr
->const_quality
= ctrl
->val
;
208 case V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE
:
209 ctr
->frame_skip_mode
= ctrl
->val
;
218 static const struct v4l2_ctrl_ops venc_ctrl_ops
= {
219 .s_ctrl
= venc_op_s_ctrl
,
222 int venc_ctrl_init(struct venus_inst
*inst
)
226 ret
= v4l2_ctrl_handler_init(&inst
->ctrl_handler
, 33);
230 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
231 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
232 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
,
233 ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
) |
234 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
) |
235 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CQ
)),
236 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
);
238 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
239 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
,
240 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC
,
241 0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
);
243 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
244 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
,
245 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY
,
246 ~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
) |
247 (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
)),
248 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
);
250 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
251 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
,
252 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
,
253 0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
);
255 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
256 V4L2_CID_MPEG_VIDEO_HEVC_PROFILE
,
257 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10
,
258 ~((1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
) |
259 (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_STILL_PICTURE
) |
260 (1 << V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10
)),
261 V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN
);
263 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
264 V4L2_CID_MPEG_VIDEO_HEVC_LEVEL
,
265 V4L2_MPEG_VIDEO_HEVC_LEVEL_6_2
,
266 0, V4L2_MPEG_VIDEO_HEVC_LEVEL_1
);
268 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
269 V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
270 V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
,
271 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
272 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE
) |
273 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
274 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
) |
275 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH
) |
276 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
)),
277 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
);
279 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
280 V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
281 V4L2_MPEG_VIDEO_H264_LEVEL_5_1
,
282 0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0
);
284 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
285 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
287 0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
);
289 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
290 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
291 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
292 1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
293 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
);
295 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
296 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
297 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_MAX_BYTES
,
298 0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
300 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
301 V4L2_CID_MPEG_VIDEO_VP8_PROFILE
,
302 V4L2_MPEG_VIDEO_VP8_PROFILE_3
,
303 0, V4L2_MPEG_VIDEO_VP8_PROFILE_0
);
305 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
306 V4L2_CID_MPEG_VIDEO_BITRATE
, BITRATE_MIN
, BITRATE_MAX
,
307 BITRATE_STEP
, BITRATE_DEFAULT
);
309 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
310 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
, BITRATE_MIN
, BITRATE_MAX
,
311 BITRATE_STEP
, BITRATE_DEFAULT_PEAK
);
313 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
314 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 1, 51, 1, 26);
316 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
317 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 1, 51, 1, 28);
319 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
320 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
, 1, 51, 1, 30);
322 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
323 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 1, 51, 1, 1);
325 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
326 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 1, 51, 1, 51);
328 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
329 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, SLICE_BYTE_SIZE_MIN
,
330 SLICE_BYTE_SIZE_MAX
, 1, SLICE_BYTE_SIZE_MIN
);
332 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
333 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1,
334 SLICE_MB_SIZE_MAX
, 1, 1);
336 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
337 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, -6, 6, 1, 0);
339 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
340 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, -6, 6, 1, 0);
342 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
343 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
,
344 0, INTRA_REFRESH_MBS_MAX
, 1, 0);
346 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
347 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 0, (1 << 16) - 1, 1, 30);
349 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
350 V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
, 1, 128, 1, 1);
352 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
353 V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
, 1, 128, 1, 128);
355 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
356 V4L2_CID_MPEG_VIDEO_B_FRAMES
, 0, 4, 1, 0);
358 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
359 V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
, 0, (1 << 16) - 1, 1, 0);
361 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
362 V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME
, 0, 0, 0, 0);
364 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
365 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
, 0, 1, 1, 1);
367 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
368 V4L2_CID_MPEG_VIDEO_CONSTANT_QUALITY
, 0, 100, 1, 0);
370 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
371 V4L2_CID_MPEG_VIDEO_FRAME_SKIP_MODE
,
372 V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT
,
373 ~((1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED
) |
374 (1 << V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_BUF_LIMIT
)),
375 V4L2_MPEG_VIDEO_FRAME_SKIP_MODE_DISABLED
);
377 ret
= inst
->ctrl_handler
.error
;
381 ret
= v4l2_ctrl_handler_setup(&inst
->ctrl_handler
);
387 v4l2_ctrl_handler_free(&inst
->ctrl_handler
);
391 void venc_ctrl_deinit(struct venus_inst
*inst
)
393 v4l2_ctrl_handler_free(&inst
->ctrl_handler
);