1 /*
2  * Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
3  * Copyright (C) 2017 Linaro Ltd.
4  *
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.
8  *
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.
13  *
14  */
15 #include <linux/types.h>
16 #include <media/v4l2-ctrls.h>
17 
18 #include "core.h"
19 #include "venc.h"
20 
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
32 
33 static int venc_calc_bpframes(u32 gop_size, u32 conseq_b, u32 *bf, u32 *pf)
34 {
35 	u32 half = (gop_size - 1) >> 1;
36 	u32 b, p, ratio;
37 	bool found = false;
38 
39 	if (!gop_size)
40 		return -EINVAL;
41 
42 	*bf = *pf = 0;
43 
44 	if (!conseq_b) {
45 		*pf = gop_size -  1;
46 		return 0;
47 	}
48 
49 	b = p = half;
50 
51 	for (; b <= gop_size - 1; b++, p--) {
52 		if (b % p)
53 			continue;
54 
55 		ratio = b / p;
56 
57 		if (ratio == conseq_b) {
58 			found = true;
59 			break;
60 		}
61 
62 		if (ratio > conseq_b)
63 			break;
64 	}
65 
66 	if (!found)
67 		return -EINVAL;
68 
69 	if (b + p + 1 != gop_size)
70 		return -EINVAL;
71 
72 	*bf = b;
73 	*pf = p;
74 
75 	return 0;
76 }
77 
78 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl)
79 {
80 	struct venus_inst *inst = ctrl_to_inst(ctrl);
81 	struct venc_controls *ctr = &inst->controls.enc;
82 	struct hfi_enable en = { .enable = 1 };
83 	struct hfi_bitrate brate;
84 	u32 bframes;
85 	u32 ptype;
86 	int ret;
87 
88 	switch (ctrl->id) {
89 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
90 		ctr->bitrate_mode = ctrl->val;
91 		break;
92 	case V4L2_CID_MPEG_VIDEO_BITRATE:
93 		ctr->bitrate = ctrl->val;
94 		mutex_lock(&inst->lock);
95 		if (inst->streamon_out && inst->streamon_cap) {
96 			ptype = HFI_PROPERTY_CONFIG_VENC_TARGET_BITRATE;
97 			brate.bitrate = ctr->bitrate;
98 			brate.layer_id = 0;
99 
100 			ret = hfi_session_set_property(inst, ptype, &brate);
101 			if (ret) {
102 				mutex_unlock(&inst->lock);
103 				return ret;
104 			}
105 		}
106 		mutex_unlock(&inst->lock);
107 		break;
108 	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
109 		ctr->bitrate_peak = ctrl->val;
110 		break;
111 	case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE:
112 		ctr->h264_entropy_mode = ctrl->val;
113 		break;
114 	case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE:
115 		ctr->profile.mpeg4 = ctrl->val;
116 		break;
117 	case V4L2_CID_MPEG_VIDEO_H264_PROFILE:
118 		ctr->profile.h264 = ctrl->val;
119 		break;
120 	case V4L2_CID_MPEG_VIDEO_VP8_PROFILE:
121 		ctr->profile.vpx = ctrl->val;
122 		break;
123 	case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL:
124 		ctr->level.mpeg4 = ctrl->val;
125 		break;
126 	case V4L2_CID_MPEG_VIDEO_H264_LEVEL:
127 		ctr->level.h264 = 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_MAX_QP:
142 		ctr->h264_max_qp = ctrl->val;
143 		break;
144 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE:
145 		ctr->multi_slice_mode = ctrl->val;
146 		break;
147 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES:
148 		ctr->multi_slice_max_bytes = ctrl->val;
149 		break;
150 	case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB:
151 		ctr->multi_slice_max_mb = ctrl->val;
152 		break;
153 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA:
154 		ctr->h264_loop_filter_alpha = ctrl->val;
155 		break;
156 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA:
157 		ctr->h264_loop_filter_beta = ctrl->val;
158 		break;
159 	case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE:
160 		ctr->h264_loop_filter_mode = ctrl->val;
161 		break;
162 	case V4L2_CID_MPEG_VIDEO_HEADER_MODE:
163 		ctr->header_mode = ctrl->val;
164 		break;
165 	case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB:
166 		break;
167 	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
168 		ret = venc_calc_bpframes(ctrl->val, ctr->num_b_frames, &bframes,
169 					 &ctr->num_p_frames);
170 		if (ret)
171 			return ret;
172 
173 		ctr->gop_size = ctrl->val;
174 		break;
175 	case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD:
176 		ctr->h264_i_period = ctrl->val;
177 		break;
178 	case V4L2_CID_MPEG_VIDEO_VPX_MIN_QP:
179 		ctr->vp8_min_qp = ctrl->val;
180 		break;
181 	case V4L2_CID_MPEG_VIDEO_VPX_MAX_QP:
182 		ctr->vp8_max_qp = ctrl->val;
183 		break;
184 	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
185 		ret = venc_calc_bpframes(ctr->gop_size, ctrl->val, &bframes,
186 					 &ctr->num_p_frames);
187 		if (ret)
188 			return ret;
189 
190 		ctr->num_b_frames = bframes;
191 		break;
192 	case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME:
193 		mutex_lock(&inst->lock);
194 		if (inst->streamon_out && inst->streamon_cap) {
195 			ptype = HFI_PROPERTY_CONFIG_VENC_REQUEST_SYNC_FRAME;
196 			ret = hfi_session_set_property(inst, ptype, &en);
197 
198 			if (ret) {
199 				mutex_unlock(&inst->lock);
200 				return ret;
201 			}
202 		}
203 		mutex_unlock(&inst->lock);
204 		break;
205 	default:
206 		return -EINVAL;
207 	}
208 
209 	return 0;
210 }
211 
212 static const struct v4l2_ctrl_ops venc_ctrl_ops = {
213 	.s_ctrl = venc_op_s_ctrl,
214 };
215 
216 int venc_ctrl_init(struct venus_inst *inst)
217 {
218 	int ret;
219 
220 	ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 28);
221 	if (ret)
222 		return ret;
223 
224 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
225 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
226 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
227 		~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) |
228 		  (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)),
229 		V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
230 
231 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
232 		V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE,
233 		V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC,
234 		0, V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC);
235 
236 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
237 		V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE,
238 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_CODING_EFFICIENCY,
239 		~((1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE) |
240 		  (1 << V4L2_MPEG_VIDEO_MPEG4_PROFILE_ADVANCED_SIMPLE)),
241 		V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE);
242 
243 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
244 		V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL,
245 		V4L2_MPEG_VIDEO_MPEG4_LEVEL_5,
246 		0, V4L2_MPEG_VIDEO_MPEG4_LEVEL_0);
247 
248 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
249 		V4L2_CID_MPEG_VIDEO_H264_PROFILE,
250 		V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH,
251 		~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
252 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
253 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
254 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) |
255 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH) |
256 		  (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MULTIVIEW_HIGH)),
257 		V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
258 
259 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
260 		V4L2_CID_MPEG_VIDEO_H264_LEVEL,
261 		V4L2_MPEG_VIDEO_H264_LEVEL_5_1,
262 		0, V4L2_MPEG_VIDEO_H264_LEVEL_1_0);
263 
264 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
265 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE,
266 		AT_SLICE_BOUNDARY,
267 		0, V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED);
268 
269 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
270 		V4L2_CID_MPEG_VIDEO_HEADER_MODE,
271 		V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
272 		1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME,
273 		V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE);
274 
275 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
276 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE,
277 		V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES,
278 		0, V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE);
279 
280 	v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops,
281 		V4L2_CID_MPEG_VIDEO_VP8_PROFILE,
282 		V4L2_MPEG_VIDEO_VP8_PROFILE_3,
283 		0, V4L2_MPEG_VIDEO_VP8_PROFILE_0);
284 
285 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
286 		V4L2_CID_MPEG_VIDEO_BITRATE, BITRATE_MIN, BITRATE_MAX,
287 		BITRATE_STEP, BITRATE_DEFAULT);
288 
289 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
290 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, BITRATE_MIN, BITRATE_MAX,
291 		BITRATE_STEP, BITRATE_DEFAULT_PEAK);
292 
293 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
294 		V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26);
295 
296 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
297 		V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28);
298 
299 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
300 		V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30);
301 
302 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
303 		V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 1, 51, 1, 1);
304 
305 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
306 		V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 1, 51, 1, 51);
307 
308 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
309 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, SLICE_BYTE_SIZE_MIN,
310 		SLICE_BYTE_SIZE_MAX, 1, SLICE_BYTE_SIZE_MIN);
311 
312 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
313 		V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1,
314 		SLICE_MB_SIZE_MAX, 1, 1);
315 
316 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
317 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, -6, 6, 1, 0);
318 
319 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
320 		V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, -6, 6, 1, 0);
321 
322 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
323 		V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB,
324 		0, INTRA_REFRESH_MBS_MAX, 1, 0);
325 
326 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
327 		V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30);
328 
329 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
330 		V4L2_CID_MPEG_VIDEO_VPX_MIN_QP, 1, 128, 1, 1);
331 
332 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
333 		V4L2_CID_MPEG_VIDEO_VPX_MAX_QP, 1, 128, 1, 128);
334 
335 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
336 		V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0);
337 
338 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
339 		V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, 0, (1 << 16) - 1, 1, 0);
340 
341 	v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops,
342 			  V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0);
343 
344 	ret = inst->ctrl_handler.error;
345 	if (ret)
346 		goto err;
347 
348 	ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
349 	if (ret)
350 		goto err;
351 
352 	return 0;
353 err:
354 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
355 	return ret;
356 }
357 
358 void venc_ctrl_deinit(struct venus_inst *inst)
359 {
360 	v4l2_ctrl_handler_free(&inst->ctrl_handler);
361 }
362