2 * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3 * Copyright (C) 2017 Linaro Ltd.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/types.h>
16 #include <media/v4l2-ctrls.h>
21 #define BITRATE_MIN 32000
22 #define BITRATE_MAX 160000000
23 #define BITRATE_DEFAULT 1000000
24 #define BITRATE_DEFAULT_PEAK (BITRATE_DEFAULT * 2)
25 #define BITRATE_STEP 100
26 #define SLICE_BYTE_SIZE_MAX 1024
27 #define SLICE_BYTE_SIZE_MIN 1024
28 #define SLICE_MB_SIZE_MAX 300
29 #define INTRA_REFRESH_MBS_MAX 300
30 #define AT_SLICE_BOUNDARY \
31 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED_AT_SLICE_BOUNDARY
33 static int venc_calc_bpframes(u32 gop_size
, u32 conseq_b
, u32
*bf
, u32
*pf
)
35 u32 half
= (gop_size
- 1) >> 1;
51 for (; b
<= gop_size
- 1; b
++, p
--) {
57 if (ratio
== conseq_b
) {
69 if (b
+ p
+ 1 != gop_size
)
78 static int venc_op_s_ctrl(struct v4l2_ctrl
*ctrl
)
80 struct venus_inst
*inst
= ctrl_to_inst(ctrl
);
81 struct venc_controls
*ctr
= &inst
->controls
.enc
;
86 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
87 ctr
->bitrate_mode
= ctrl
->val
;
89 case V4L2_CID_MPEG_VIDEO_BITRATE
:
90 ctr
->bitrate
= ctrl
->val
;
92 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
93 ctr
->bitrate_peak
= ctrl
->val
;
95 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
:
96 ctr
->h264_entropy_mode
= ctrl
->val
;
98 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
:
99 ctr
->profile
.mpeg4
= ctrl
->val
;
101 case V4L2_CID_MPEG_VIDEO_H264_PROFILE
:
102 ctr
->profile
.h264
= ctrl
->val
;
104 case V4L2_CID_MPEG_VIDEO_VP8_PROFILE
:
105 ctr
->profile
.vpx
= ctrl
->val
;
107 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
:
108 ctr
->level
.mpeg4
= ctrl
->val
;
110 case V4L2_CID_MPEG_VIDEO_H264_LEVEL
:
111 ctr
->level
.h264
= ctrl
->val
;
113 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
:
114 ctr
->h264_i_qp
= ctrl
->val
;
116 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
:
117 ctr
->h264_p_qp
= ctrl
->val
;
119 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
:
120 ctr
->h264_b_qp
= ctrl
->val
;
122 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP
:
123 ctr
->h264_min_qp
= ctrl
->val
;
125 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP
:
126 ctr
->h264_max_qp
= ctrl
->val
;
128 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
:
129 ctr
->multi_slice_mode
= ctrl
->val
;
131 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
:
132 ctr
->multi_slice_max_bytes
= ctrl
->val
;
134 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
:
135 ctr
->multi_slice_max_mb
= ctrl
->val
;
137 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
:
138 ctr
->h264_loop_filter_alpha
= ctrl
->val
;
140 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
:
141 ctr
->h264_loop_filter_beta
= ctrl
->val
;
143 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
:
144 ctr
->h264_loop_filter_mode
= ctrl
->val
;
146 case V4L2_CID_MPEG_VIDEO_HEADER_MODE
:
147 ctr
->header_mode
= ctrl
->val
;
149 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
:
151 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
152 ret
= venc_calc_bpframes(ctrl
->val
, ctr
->num_b_frames
, &bframes
,
157 ctr
->gop_size
= ctrl
->val
;
159 case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
:
160 ctr
->h264_i_period
= ctrl
->val
;
162 case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
:
163 ctr
->vp8_min_qp
= ctrl
->val
;
165 case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
:
166 ctr
->vp8_max_qp
= ctrl
->val
;
168 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
169 ret
= venc_calc_bpframes(ctr
->gop_size
, ctrl
->val
, &bframes
,
174 ctr
->num_b_frames
= bframes
;
183 static const struct v4l2_ctrl_ops venc_ctrl_ops
= {
184 .s_ctrl
= venc_op_s_ctrl
,
187 int venc_ctrl_init(struct venus_inst
*inst
)
191 ret
= v4l2_ctrl_handler_init(&inst
->ctrl_handler
, 27);
195 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
196 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
197 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
,
198 ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
) |
199 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
)),
200 V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
);
202 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
203 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE
,
204 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC
,
205 0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC
);
207 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
208 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE
,
209 V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY
,
210 ~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
) |
211 (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE
)),
212 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE
);
214 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
215 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL
,
216 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5
,
217 0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0
);
219 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
220 V4L2_CID_MPEG_VIDEO_H264_PROFILE
,
221 V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
,
222 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE
) |
223 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE
) |
224 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN
) |
225 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
) |
226 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH
) |
227 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH
)),
228 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH
);
230 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
231 V4L2_CID_MPEG_VIDEO_H264_LEVEL
,
232 V4L2_MPEG_VIDEO_H264_LEVEL_5_1
,
233 0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0
);
235 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
236 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE
,
238 0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED
);
240 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
241 V4L2_CID_MPEG_VIDEO_HEADER_MODE
,
242 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
243 1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME
,
244 V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE
);
246 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
247 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE
,
248 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES
,
249 0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE
);
251 v4l2_ctrl_new_std_menu(&inst
->ctrl_handler
, &venc_ctrl_ops
,
252 V4L2_CID_MPEG_VIDEO_VP8_PROFILE
,
253 V4L2_MPEG_VIDEO_VP8_PROFILE_3
,
254 0, V4L2_MPEG_VIDEO_VP8_PROFILE_0
);
256 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
257 V4L2_CID_MPEG_VIDEO_BITRATE
, BITRATE_MIN
, BITRATE_MAX
,
258 BITRATE_STEP
, BITRATE_DEFAULT
);
260 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
261 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
, BITRATE_MIN
, BITRATE_MAX
,
262 BITRATE_STEP
, BITRATE_DEFAULT_PEAK
);
264 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
265 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP
, 1, 51, 1, 26);
267 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
268 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP
, 1, 51, 1, 28);
270 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
271 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP
, 1, 51, 1, 30);
273 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
274 V4L2_CID_MPEG_VIDEO_H264_MIN_QP
, 1, 51, 1, 1);
276 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
277 V4L2_CID_MPEG_VIDEO_H264_MAX_QP
, 1, 51, 1, 51);
279 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
280 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES
, SLICE_BYTE_SIZE_MIN
,
281 SLICE_BYTE_SIZE_MAX
, 1, SLICE_BYTE_SIZE_MIN
);
283 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
284 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB
, 1,
285 SLICE_MB_SIZE_MAX
, 1, 1);
287 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
288 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA
, -6, 6, 1, 0);
290 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
291 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA
, -6, 6, 1, 0);
293 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
294 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB
,
295 0, INTRA_REFRESH_MBS_MAX
, 1, 0);
297 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
298 V4L2_CID_MPEG_VIDEO_GOP_SIZE
, 0, (1 << 16) - 1, 1, 12);
300 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
301 V4L2_CID_MPEG_VIDEO_VPX_MIN_QP
, 1, 128, 1, 1);
303 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
304 V4L2_CID_MPEG_VIDEO_VPX_MAX_QP
, 1, 128, 1, 128);
306 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
307 V4L2_CID_MPEG_VIDEO_B_FRAMES
, 0, 4, 1, 0);
309 v4l2_ctrl_new_std(&inst
->ctrl_handler
, &venc_ctrl_ops
,
310 V4L2_CID_MPEG_VIDEO_H264_I_PERIOD
, 0, (1 << 16) - 1, 1, 0);
312 ret
= inst
->ctrl_handler
.error
;
316 ret
= v4l2_ctrl_handler_setup(&inst
->ctrl_handler
);
322 v4l2_ctrl_handler_free(&inst
->ctrl_handler
);
326 void venc_ctrl_deinit(struct venus_inst
*inst
)
328 v4l2_ctrl_handler_free(&inst
->ctrl_handler
);