Commit | Line | Data |
---|---|---|
4e855a6e TL |
1 | /* |
2 | * Copyright (c) 2016 MediaTek Inc. | |
3 | * Author: PC Chen <pc.chen@mediatek.com> | |
4 | * Tiffany Lin <tiffany.lin@mediatek.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | */ | |
15 | ||
16 | #include <media/v4l2-event.h> | |
17 | #include <media/v4l2-mem2mem.h> | |
18 | #include <media/videobuf2-dma-contig.h> | |
19 | #include <soc/mediatek/smi.h> | |
20 | ||
21 | #include "mtk_vcodec_drv.h" | |
22 | #include "mtk_vcodec_enc.h" | |
23 | #include "mtk_vcodec_intr.h" | |
24 | #include "mtk_vcodec_util.h" | |
25 | #include "venc_drv_if.h" | |
26 | ||
27 | #define MTK_VENC_MIN_W 160U | |
28 | #define MTK_VENC_MIN_H 128U | |
29 | #define MTK_VENC_MAX_W 1920U | |
30 | #define MTK_VENC_MAX_H 1088U | |
31 | #define DFT_CFG_WIDTH MTK_VENC_MIN_W | |
32 | #define DFT_CFG_HEIGHT MTK_VENC_MIN_H | |
33 | #define MTK_MAX_CTRLS_HINT 20 | |
34 | #define OUT_FMT_IDX 0 | |
35 | #define CAP_FMT_IDX 4 | |
36 | ||
37 | ||
38 | static void mtk_venc_worker(struct work_struct *work); | |
39 | ||
40 | static struct mtk_video_fmt mtk_video_formats[] = { | |
41 | { | |
42 | .fourcc = V4L2_PIX_FMT_NV12M, | |
43 | .type = MTK_FMT_FRAME, | |
44 | .num_planes = 2, | |
45 | }, | |
46 | { | |
47 | .fourcc = V4L2_PIX_FMT_NV21M, | |
48 | .type = MTK_FMT_FRAME, | |
49 | .num_planes = 2, | |
50 | }, | |
51 | { | |
52 | .fourcc = V4L2_PIX_FMT_YUV420M, | |
53 | .type = MTK_FMT_FRAME, | |
54 | .num_planes = 3, | |
55 | }, | |
56 | { | |
57 | .fourcc = V4L2_PIX_FMT_YVU420M, | |
58 | .type = MTK_FMT_FRAME, | |
59 | .num_planes = 3, | |
60 | }, | |
61 | { | |
62 | .fourcc = V4L2_PIX_FMT_H264, | |
63 | .type = MTK_FMT_ENC, | |
64 | .num_planes = 1, | |
65 | }, | |
66 | { | |
67 | .fourcc = V4L2_PIX_FMT_VP8, | |
68 | .type = MTK_FMT_ENC, | |
69 | .num_planes = 1, | |
70 | }, | |
71 | }; | |
72 | ||
73 | #define NUM_FORMATS ARRAY_SIZE(mtk_video_formats) | |
74 | ||
75 | static const struct mtk_codec_framesizes mtk_venc_framesizes[] = { | |
76 | { | |
77 | .fourcc = V4L2_PIX_FMT_H264, | |
78 | .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16, | |
79 | MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 }, | |
80 | }, | |
81 | { | |
82 | .fourcc = V4L2_PIX_FMT_VP8, | |
83 | .stepwise = { MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16, | |
84 | MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16 }, | |
85 | }, | |
86 | }; | |
87 | ||
88 | #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes) | |
89 | ||
90 | static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl) | |
91 | { | |
92 | struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl); | |
93 | struct mtk_enc_params *p = &ctx->enc_params; | |
94 | int ret = 0; | |
95 | ||
96 | switch (ctrl->id) { | |
97 | case V4L2_CID_MPEG_VIDEO_BITRATE: | |
98 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_BITRATE val = %d", | |
99 | ctrl->val); | |
100 | p->bitrate = ctrl->val; | |
101 | ctx->param_change |= MTK_ENCODE_PARAM_BITRATE; | |
102 | break; | |
103 | case V4L2_CID_MPEG_VIDEO_B_FRAMES: | |
104 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_B_FRAMES val = %d", | |
105 | ctrl->val); | |
106 | p->num_b_frame = ctrl->val; | |
107 | break; | |
108 | case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: | |
109 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE val = %d", | |
110 | ctrl->val); | |
111 | p->rc_frame = ctrl->val; | |
112 | break; | |
113 | case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: | |
114 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_MAX_QP val = %d", | |
115 | ctrl->val); | |
116 | p->h264_max_qp = ctrl->val; | |
117 | break; | |
118 | case V4L2_CID_MPEG_VIDEO_HEADER_MODE: | |
119 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_HEADER_MODE val = %d", | |
120 | ctrl->val); | |
121 | p->seq_hdr_mode = ctrl->val; | |
122 | break; | |
123 | case V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE: | |
124 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE val = %d", | |
125 | ctrl->val); | |
126 | p->rc_mb = ctrl->val; | |
127 | break; | |
128 | case V4L2_CID_MPEG_VIDEO_H264_PROFILE: | |
129 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_PROFILE val = %d", | |
130 | ctrl->val); | |
131 | p->h264_profile = ctrl->val; | |
132 | break; | |
133 | case V4L2_CID_MPEG_VIDEO_H264_LEVEL: | |
134 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_LEVEL val = %d", | |
135 | ctrl->val); | |
136 | p->h264_level = ctrl->val; | |
137 | break; | |
138 | case V4L2_CID_MPEG_VIDEO_H264_I_PERIOD: | |
139 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_H264_I_PERIOD val = %d", | |
140 | ctrl->val); | |
141 | p->intra_period = ctrl->val; | |
142 | ctx->param_change |= MTK_ENCODE_PARAM_INTRA_PERIOD; | |
143 | break; | |
144 | case V4L2_CID_MPEG_VIDEO_GOP_SIZE: | |
145 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_GOP_SIZE val = %d", | |
146 | ctrl->val); | |
147 | p->gop_size = ctrl->val; | |
148 | ctx->param_change |= MTK_ENCODE_PARAM_GOP_SIZE; | |
149 | break; | |
150 | case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: | |
151 | mtk_v4l2_debug(2, "V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME"); | |
152 | p->force_intra = 1; | |
153 | ctx->param_change |= MTK_ENCODE_PARAM_FORCE_INTRA; | |
154 | break; | |
155 | default: | |
156 | ret = -EINVAL; | |
157 | break; | |
158 | } | |
159 | ||
160 | return ret; | |
161 | } | |
162 | ||
163 | static const struct v4l2_ctrl_ops mtk_vcodec_enc_ctrl_ops = { | |
164 | .s_ctrl = vidioc_venc_s_ctrl, | |
165 | }; | |
166 | ||
167 | static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue) | |
168 | { | |
169 | struct mtk_video_fmt *fmt; | |
170 | int i, j = 0; | |
171 | ||
172 | for (i = 0; i < NUM_FORMATS; ++i) { | |
173 | if (output_queue && mtk_video_formats[i].type != MTK_FMT_FRAME) | |
174 | continue; | |
175 | if (!output_queue && mtk_video_formats[i].type != MTK_FMT_ENC) | |
176 | continue; | |
177 | ||
178 | if (j == f->index) { | |
179 | fmt = &mtk_video_formats[i]; | |
180 | f->pixelformat = fmt->fourcc; | |
181 | memset(f->reserved, 0, sizeof(f->reserved)); | |
182 | return 0; | |
183 | } | |
184 | ++j; | |
185 | } | |
186 | ||
187 | return -EINVAL; | |
188 | } | |
189 | ||
190 | static int vidioc_enum_framesizes(struct file *file, void *fh, | |
191 | struct v4l2_frmsizeenum *fsize) | |
192 | { | |
193 | int i = 0; | |
194 | ||
195 | if (fsize->index != 0) | |
196 | return -EINVAL; | |
197 | ||
198 | for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) { | |
199 | if (fsize->pixel_format != mtk_venc_framesizes[i].fourcc) | |
200 | continue; | |
201 | ||
202 | fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; | |
203 | fsize->stepwise = mtk_venc_framesizes[i].stepwise; | |
204 | return 0; | |
205 | } | |
206 | ||
207 | return -EINVAL; | |
208 | } | |
209 | ||
210 | static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv, | |
211 | struct v4l2_fmtdesc *f) | |
212 | { | |
213 | return vidioc_enum_fmt(f, false); | |
214 | } | |
215 | ||
216 | static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov, | |
217 | struct v4l2_fmtdesc *f) | |
218 | { | |
219 | return vidioc_enum_fmt(f, true); | |
220 | } | |
221 | ||
222 | static int vidioc_venc_querycap(struct file *file, void *priv, | |
223 | struct v4l2_capability *cap) | |
224 | { | |
225 | strlcpy(cap->driver, MTK_VCODEC_ENC_NAME, sizeof(cap->driver)); | |
226 | strlcpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info)); | |
227 | strlcpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card)); | |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
232 | static int vidioc_venc_s_parm(struct file *file, void *priv, | |
233 | struct v4l2_streamparm *a) | |
234 | { | |
235 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
236 | ||
237 | if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) | |
238 | return -EINVAL; | |
239 | ||
240 | ctx->enc_params.framerate_num = | |
241 | a->parm.output.timeperframe.denominator; | |
242 | ctx->enc_params.framerate_denom = | |
243 | a->parm.output.timeperframe.numerator; | |
244 | ctx->param_change |= MTK_ENCODE_PARAM_FRAMERATE; | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | static int vidioc_venc_g_parm(struct file *file, void *priv, | |
250 | struct v4l2_streamparm *a) | |
251 | { | |
252 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
253 | ||
254 | if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) | |
255 | return -EINVAL; | |
256 | ||
257 | a->parm.output.timeperframe.denominator = | |
258 | ctx->enc_params.framerate_num; | |
259 | a->parm.output.timeperframe.numerator = | |
260 | ctx->enc_params.framerate_denom; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx, | |
266 | enum v4l2_buf_type type) | |
267 | { | |
268 | if (V4L2_TYPE_IS_OUTPUT(type)) | |
269 | return &ctx->q_data[MTK_Q_DATA_SRC]; | |
270 | ||
271 | return &ctx->q_data[MTK_Q_DATA_DST]; | |
272 | } | |
273 | ||
274 | static struct mtk_video_fmt *mtk_venc_find_format(struct v4l2_format *f) | |
275 | { | |
276 | struct mtk_video_fmt *fmt; | |
277 | unsigned int k; | |
278 | ||
279 | for (k = 0; k < NUM_FORMATS; k++) { | |
280 | fmt = &mtk_video_formats[k]; | |
281 | if (fmt->fourcc == f->fmt.pix.pixelformat) | |
282 | return fmt; | |
283 | } | |
284 | ||
285 | return NULL; | |
286 | } | |
287 | ||
288 | /* V4L2 specification suggests the driver corrects the format struct if any of | |
289 | * the dimensions is unsupported | |
290 | */ | |
291 | static int vidioc_try_fmt(struct v4l2_format *f, struct mtk_video_fmt *fmt) | |
292 | { | |
293 | struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; | |
294 | int i; | |
295 | ||
296 | pix_fmt_mp->field = V4L2_FIELD_NONE; | |
297 | ||
298 | if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { | |
299 | pix_fmt_mp->num_planes = 1; | |
300 | pix_fmt_mp->plane_fmt[0].bytesperline = 0; | |
301 | } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { | |
302 | int tmp_w, tmp_h; | |
303 | ||
304 | pix_fmt_mp->height = clamp(pix_fmt_mp->height, | |
305 | MTK_VENC_MIN_H, | |
306 | MTK_VENC_MAX_H); | |
307 | pix_fmt_mp->width = clamp(pix_fmt_mp->width, | |
308 | MTK_VENC_MIN_W, | |
309 | MTK_VENC_MAX_W); | |
310 | ||
311 | /* find next closer width align 16, heign align 32, size align | |
312 | * 64 rectangle | |
313 | */ | |
314 | tmp_w = pix_fmt_mp->width; | |
315 | tmp_h = pix_fmt_mp->height; | |
316 | v4l_bound_align_image(&pix_fmt_mp->width, | |
317 | MTK_VENC_MIN_W, | |
318 | MTK_VENC_MAX_W, 4, | |
319 | &pix_fmt_mp->height, | |
320 | MTK_VENC_MIN_H, | |
321 | MTK_VENC_MAX_H, 5, 6); | |
322 | ||
323 | if (pix_fmt_mp->width < tmp_w && | |
324 | (pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W) | |
325 | pix_fmt_mp->width += 16; | |
326 | if (pix_fmt_mp->height < tmp_h && | |
327 | (pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H) | |
328 | pix_fmt_mp->height += 32; | |
329 | ||
330 | mtk_v4l2_debug(0, | |
8d4eb2ac | 331 | "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d %d", |
4e855a6e TL |
332 | tmp_w, tmp_h, pix_fmt_mp->width, |
333 | pix_fmt_mp->height, | |
8d4eb2ac TL |
334 | pix_fmt_mp->plane_fmt[0].sizeimage, |
335 | pix_fmt_mp->plane_fmt[1].sizeimage); | |
4e855a6e TL |
336 | |
337 | pix_fmt_mp->num_planes = fmt->num_planes; | |
338 | pix_fmt_mp->plane_fmt[0].sizeimage = | |
339 | pix_fmt_mp->width * pix_fmt_mp->height + | |
340 | ((ALIGN(pix_fmt_mp->width, 16) * 2) * 16); | |
341 | pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width; | |
342 | ||
343 | if (pix_fmt_mp->num_planes == 2) { | |
344 | pix_fmt_mp->plane_fmt[1].sizeimage = | |
345 | (pix_fmt_mp->width * pix_fmt_mp->height) / 2 + | |
346 | (ALIGN(pix_fmt_mp->width, 16) * 16); | |
347 | pix_fmt_mp->plane_fmt[2].sizeimage = 0; | |
348 | pix_fmt_mp->plane_fmt[1].bytesperline = | |
349 | pix_fmt_mp->width; | |
350 | pix_fmt_mp->plane_fmt[2].bytesperline = 0; | |
351 | } else if (pix_fmt_mp->num_planes == 3) { | |
352 | pix_fmt_mp->plane_fmt[1].sizeimage = | |
353 | pix_fmt_mp->plane_fmt[2].sizeimage = | |
354 | (pix_fmt_mp->width * pix_fmt_mp->height) / 4 + | |
355 | ((ALIGN(pix_fmt_mp->width, 16) / 2) * 16); | |
356 | pix_fmt_mp->plane_fmt[1].bytesperline = | |
357 | pix_fmt_mp->plane_fmt[2].bytesperline = | |
358 | pix_fmt_mp->width / 2; | |
359 | } | |
360 | } | |
361 | ||
362 | for (i = 0; i < pix_fmt_mp->num_planes; i++) | |
363 | memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0, | |
364 | sizeof(pix_fmt_mp->plane_fmt[0].reserved)); | |
365 | ||
366 | pix_fmt_mp->flags = 0; | |
367 | memset(&pix_fmt_mp->reserved, 0x0, | |
368 | sizeof(pix_fmt_mp->reserved)); | |
369 | ||
370 | return 0; | |
371 | } | |
372 | ||
373 | static void mtk_venc_set_param(struct mtk_vcodec_ctx *ctx, | |
374 | struct venc_enc_param *param) | |
375 | { | |
376 | struct mtk_q_data *q_data_src = &ctx->q_data[MTK_Q_DATA_SRC]; | |
377 | struct mtk_enc_params *enc_params = &ctx->enc_params; | |
378 | ||
379 | switch (q_data_src->fmt->fourcc) { | |
380 | case V4L2_PIX_FMT_YUV420M: | |
381 | param->input_yuv_fmt = VENC_YUV_FORMAT_I420; | |
382 | break; | |
383 | case V4L2_PIX_FMT_YVU420M: | |
384 | param->input_yuv_fmt = VENC_YUV_FORMAT_YV12; | |
385 | break; | |
386 | case V4L2_PIX_FMT_NV12M: | |
387 | param->input_yuv_fmt = VENC_YUV_FORMAT_NV12; | |
388 | break; | |
389 | case V4L2_PIX_FMT_NV21M: | |
390 | param->input_yuv_fmt = VENC_YUV_FORMAT_NV21; | |
391 | break; | |
392 | default: | |
393 | mtk_v4l2_err("Unsupport fourcc =%d", q_data_src->fmt->fourcc); | |
394 | break; | |
395 | } | |
396 | param->h264_profile = enc_params->h264_profile; | |
397 | param->h264_level = enc_params->h264_level; | |
398 | ||
399 | /* Config visible resolution */ | |
400 | param->width = q_data_src->visible_width; | |
401 | param->height = q_data_src->visible_height; | |
402 | /* Config coded resolution */ | |
403 | param->buf_width = q_data_src->coded_width; | |
404 | param->buf_height = q_data_src->coded_height; | |
405 | param->frm_rate = enc_params->framerate_num / | |
406 | enc_params->framerate_denom; | |
407 | param->intra_period = enc_params->intra_period; | |
408 | param->gop_size = enc_params->gop_size; | |
409 | param->bitrate = enc_params->bitrate; | |
410 | ||
411 | mtk_v4l2_debug(0, | |
412 | "fmt 0x%x, P/L %d/%d, w/h %d/%d, buf %d/%d, fps/bps %d/%d, gop %d, i_period %d", | |
413 | param->input_yuv_fmt, param->h264_profile, | |
414 | param->h264_level, param->width, param->height, | |
415 | param->buf_width, param->buf_height, | |
416 | param->frm_rate, param->bitrate, | |
417 | param->gop_size, param->intra_period); | |
418 | } | |
419 | ||
420 | static int vidioc_venc_s_fmt_cap(struct file *file, void *priv, | |
421 | struct v4l2_format *f) | |
422 | { | |
423 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
424 | struct vb2_queue *vq; | |
425 | struct mtk_q_data *q_data; | |
426 | int i, ret; | |
427 | struct mtk_video_fmt *fmt; | |
428 | ||
429 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); | |
430 | if (!vq) { | |
431 | mtk_v4l2_err("fail to get vq"); | |
432 | return -EINVAL; | |
433 | } | |
434 | ||
435 | if (vb2_is_busy(vq)) { | |
436 | mtk_v4l2_err("queue busy"); | |
437 | return -EBUSY; | |
438 | } | |
439 | ||
440 | q_data = mtk_venc_get_q_data(ctx, f->type); | |
441 | if (!q_data) { | |
442 | mtk_v4l2_err("fail to get q data"); | |
443 | return -EINVAL; | |
444 | } | |
445 | ||
446 | fmt = mtk_venc_find_format(f); | |
447 | if (!fmt) { | |
448 | f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc; | |
449 | fmt = mtk_venc_find_format(f); | |
450 | } | |
451 | ||
452 | q_data->fmt = fmt; | |
453 | ret = vidioc_try_fmt(f, q_data->fmt); | |
454 | if (ret) | |
455 | return ret; | |
456 | ||
457 | q_data->coded_width = f->fmt.pix_mp.width; | |
458 | q_data->coded_height = f->fmt.pix_mp.height; | |
459 | q_data->field = f->fmt.pix_mp.field; | |
460 | ||
461 | for (i = 0; i < f->fmt.pix_mp.num_planes; i++) { | |
462 | struct v4l2_plane_pix_format *plane_fmt; | |
463 | ||
464 | plane_fmt = &f->fmt.pix_mp.plane_fmt[i]; | |
465 | q_data->bytesperline[i] = plane_fmt->bytesperline; | |
466 | q_data->sizeimage[i] = plane_fmt->sizeimage; | |
467 | } | |
468 | ||
469 | if (ctx->state == MTK_STATE_FREE) { | |
470 | ret = venc_if_init(ctx, q_data->fmt->fourcc); | |
471 | if (ret) { | |
472 | mtk_v4l2_err("venc_if_init failed=%d, codec type=%x", | |
473 | ret, q_data->fmt->fourcc); | |
474 | return -EBUSY; | |
475 | } | |
476 | ctx->state = MTK_STATE_INIT; | |
477 | } | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
482 | static int vidioc_venc_s_fmt_out(struct file *file, void *priv, | |
483 | struct v4l2_format *f) | |
484 | { | |
485 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
486 | struct vb2_queue *vq; | |
487 | struct mtk_q_data *q_data; | |
488 | int ret, i; | |
489 | struct mtk_video_fmt *fmt; | |
4e855a6e TL |
490 | struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; |
491 | ||
492 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); | |
493 | if (!vq) { | |
494 | mtk_v4l2_err("fail to get vq"); | |
495 | return -EINVAL; | |
496 | } | |
497 | ||
498 | if (vb2_is_busy(vq)) { | |
499 | mtk_v4l2_err("queue busy"); | |
500 | return -EBUSY; | |
501 | } | |
502 | ||
503 | q_data = mtk_venc_get_q_data(ctx, f->type); | |
504 | if (!q_data) { | |
505 | mtk_v4l2_err("fail to get q data"); | |
506 | return -EINVAL; | |
507 | } | |
508 | ||
509 | fmt = mtk_venc_find_format(f); | |
510 | if (!fmt) { | |
511 | f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc; | |
512 | fmt = mtk_venc_find_format(f); | |
513 | } | |
514 | ||
515 | pix_fmt_mp->height = clamp(pix_fmt_mp->height, | |
516 | MTK_VENC_MIN_H, | |
517 | MTK_VENC_MAX_H); | |
518 | pix_fmt_mp->width = clamp(pix_fmt_mp->width, | |
519 | MTK_VENC_MIN_W, | |
520 | MTK_VENC_MAX_W); | |
521 | ||
522 | q_data->visible_width = f->fmt.pix_mp.width; | |
523 | q_data->visible_height = f->fmt.pix_mp.height; | |
524 | q_data->fmt = fmt; | |
525 | ret = vidioc_try_fmt(f, q_data->fmt); | |
526 | if (ret) | |
527 | return ret; | |
528 | ||
529 | q_data->coded_width = f->fmt.pix_mp.width; | |
530 | q_data->coded_height = f->fmt.pix_mp.height; | |
531 | ||
4e855a6e TL |
532 | q_data->field = f->fmt.pix_mp.field; |
533 | ctx->colorspace = f->fmt.pix_mp.colorspace; | |
534 | ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc; | |
535 | ctx->quantization = f->fmt.pix_mp.quantization; | |
536 | ctx->xfer_func = f->fmt.pix_mp.xfer_func; | |
537 | ||
538 | for (i = 0; i < f->fmt.pix_mp.num_planes; i++) { | |
539 | struct v4l2_plane_pix_format *plane_fmt; | |
540 | ||
541 | plane_fmt = &f->fmt.pix_mp.plane_fmt[i]; | |
542 | q_data->bytesperline[i] = plane_fmt->bytesperline; | |
543 | q_data->sizeimage[i] = plane_fmt->sizeimage; | |
544 | } | |
545 | ||
546 | return 0; | |
547 | } | |
548 | ||
549 | static int vidioc_venc_g_fmt(struct file *file, void *priv, | |
550 | struct v4l2_format *f) | |
551 | { | |
552 | struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp; | |
553 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
554 | struct vb2_queue *vq; | |
555 | struct mtk_q_data *q_data; | |
556 | int i; | |
557 | ||
558 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); | |
559 | if (!vq) | |
560 | return -EINVAL; | |
561 | ||
562 | q_data = mtk_venc_get_q_data(ctx, f->type); | |
563 | ||
564 | pix->width = q_data->coded_width; | |
565 | pix->height = q_data->coded_height; | |
566 | pix->pixelformat = q_data->fmt->fourcc; | |
567 | pix->field = q_data->field; | |
568 | pix->num_planes = q_data->fmt->num_planes; | |
569 | for (i = 0; i < pix->num_planes; i++) { | |
570 | pix->plane_fmt[i].bytesperline = q_data->bytesperline[i]; | |
571 | pix->plane_fmt[i].sizeimage = q_data->sizeimage[i]; | |
572 | memset(&(pix->plane_fmt[i].reserved[0]), 0x0, | |
573 | sizeof(pix->plane_fmt[i].reserved)); | |
574 | } | |
575 | ||
576 | pix->flags = 0; | |
577 | pix->colorspace = ctx->colorspace; | |
578 | pix->ycbcr_enc = ctx->ycbcr_enc; | |
579 | pix->quantization = ctx->quantization; | |
580 | pix->xfer_func = ctx->xfer_func; | |
581 | ||
582 | return 0; | |
583 | } | |
584 | ||
585 | static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv, | |
586 | struct v4l2_format *f) | |
587 | { | |
588 | struct mtk_video_fmt *fmt; | |
589 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
590 | ||
591 | fmt = mtk_venc_find_format(f); | |
592 | if (!fmt) { | |
593 | f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc; | |
594 | fmt = mtk_venc_find_format(f); | |
595 | } | |
596 | f->fmt.pix_mp.colorspace = ctx->colorspace; | |
597 | f->fmt.pix_mp.ycbcr_enc = ctx->ycbcr_enc; | |
598 | f->fmt.pix_mp.quantization = ctx->quantization; | |
599 | f->fmt.pix_mp.xfer_func = ctx->xfer_func; | |
600 | ||
601 | return vidioc_try_fmt(f, fmt); | |
602 | } | |
603 | ||
604 | static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, | |
605 | struct v4l2_format *f) | |
606 | { | |
607 | struct mtk_video_fmt *fmt; | |
608 | ||
609 | fmt = mtk_venc_find_format(f); | |
610 | if (!fmt) { | |
611 | f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc; | |
612 | fmt = mtk_venc_find_format(f); | |
613 | } | |
614 | if (!f->fmt.pix_mp.colorspace) { | |
615 | f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709; | |
616 | f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; | |
617 | f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT; | |
618 | f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT; | |
619 | } | |
620 | ||
621 | return vidioc_try_fmt(f, fmt); | |
622 | } | |
623 | ||
3a549bee TL |
624 | static int vidioc_venc_g_selection(struct file *file, void *priv, |
625 | struct v4l2_selection *s) | |
626 | { | |
627 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
628 | struct mtk_q_data *q_data; | |
629 | ||
630 | if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) | |
631 | return -EINVAL; | |
632 | ||
633 | q_data = mtk_venc_get_q_data(ctx, s->type); | |
634 | if (!q_data) | |
635 | return -EINVAL; | |
636 | ||
637 | switch (s->target) { | |
638 | case V4L2_SEL_TGT_CROP_DEFAULT: | |
639 | case V4L2_SEL_TGT_CROP_BOUNDS: | |
640 | s->r.top = 0; | |
641 | s->r.left = 0; | |
642 | s->r.width = q_data->coded_width; | |
643 | s->r.height = q_data->coded_height; | |
644 | break; | |
645 | case V4L2_SEL_TGT_CROP: | |
646 | s->r.top = 0; | |
647 | s->r.left = 0; | |
648 | s->r.width = q_data->visible_width; | |
649 | s->r.height = q_data->visible_height; | |
650 | break; | |
651 | default: | |
652 | return -EINVAL; | |
653 | } | |
654 | ||
655 | return 0; | |
656 | } | |
657 | ||
658 | static int vidioc_venc_s_selection(struct file *file, void *priv, | |
659 | struct v4l2_selection *s) | |
660 | { | |
661 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
662 | struct mtk_q_data *q_data; | |
663 | ||
664 | if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) | |
665 | return -EINVAL; | |
666 | ||
667 | q_data = mtk_venc_get_q_data(ctx, s->type); | |
668 | if (!q_data) | |
669 | return -EINVAL; | |
670 | ||
671 | switch (s->target) { | |
672 | case V4L2_SEL_TGT_CROP: | |
673 | /* Only support crop from (0,0) */ | |
674 | s->r.top = 0; | |
675 | s->r.left = 0; | |
676 | s->r.width = min(s->r.width, q_data->coded_width); | |
677 | s->r.height = min(s->r.height, q_data->coded_height); | |
678 | q_data->visible_width = s->r.width; | |
679 | q_data->visible_height = s->r.height; | |
680 | break; | |
681 | default: | |
682 | return -EINVAL; | |
683 | } | |
684 | return 0; | |
685 | } | |
686 | ||
4e855a6e TL |
687 | static int vidioc_venc_qbuf(struct file *file, void *priv, |
688 | struct v4l2_buffer *buf) | |
689 | { | |
690 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
691 | ||
692 | if (ctx->state == MTK_STATE_ABORT) { | |
693 | mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error", | |
694 | ctx->id); | |
695 | return -EIO; | |
696 | } | |
697 | ||
698 | return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); | |
699 | } | |
700 | ||
701 | static int vidioc_venc_dqbuf(struct file *file, void *priv, | |
702 | struct v4l2_buffer *buf) | |
703 | { | |
704 | struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv); | |
705 | ||
706 | if (ctx->state == MTK_STATE_ABORT) { | |
707 | mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error", | |
708 | ctx->id); | |
709 | return -EIO; | |
710 | } | |
711 | ||
712 | return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); | |
713 | } | |
714 | ||
715 | const struct v4l2_ioctl_ops mtk_venc_ioctl_ops = { | |
716 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, | |
717 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, | |
718 | ||
719 | .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, | |
720 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, | |
721 | .vidioc_qbuf = vidioc_venc_qbuf, | |
722 | .vidioc_dqbuf = vidioc_venc_dqbuf, | |
723 | ||
724 | .vidioc_querycap = vidioc_venc_querycap, | |
725 | .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane, | |
726 | .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane, | |
727 | .vidioc_enum_framesizes = vidioc_enum_framesizes, | |
728 | ||
729 | .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane, | |
730 | .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane, | |
731 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, | |
732 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, | |
733 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, | |
734 | ||
735 | .vidioc_s_parm = vidioc_venc_s_parm, | |
736 | .vidioc_g_parm = vidioc_venc_g_parm, | |
737 | .vidioc_s_fmt_vid_cap_mplane = vidioc_venc_s_fmt_cap, | |
738 | .vidioc_s_fmt_vid_out_mplane = vidioc_venc_s_fmt_out, | |
739 | ||
740 | .vidioc_g_fmt_vid_cap_mplane = vidioc_venc_g_fmt, | |
741 | .vidioc_g_fmt_vid_out_mplane = vidioc_venc_g_fmt, | |
742 | ||
743 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, | |
744 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, | |
3a549bee TL |
745 | |
746 | .vidioc_g_selection = vidioc_venc_g_selection, | |
747 | .vidioc_s_selection = vidioc_venc_s_selection, | |
4e855a6e TL |
748 | }; |
749 | ||
750 | static int vb2ops_venc_queue_setup(struct vb2_queue *vq, | |
751 | unsigned int *nbuffers, | |
752 | unsigned int *nplanes, | |
556f4408 HV |
753 | unsigned int sizes[], |
754 | struct device *alloc_devs[]) | |
4e855a6e TL |
755 | { |
756 | struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq); | |
757 | struct mtk_q_data *q_data; | |
758 | unsigned int i; | |
759 | ||
760 | q_data = mtk_venc_get_q_data(ctx, vq->type); | |
761 | ||
762 | if (q_data == NULL) | |
763 | return -EINVAL; | |
764 | ||
765 | if (*nplanes) { | |
556f4408 | 766 | for (i = 0; i < *nplanes; i++) |
4e855a6e TL |
767 | if (sizes[i] < q_data->sizeimage[i]) |
768 | return -EINVAL; | |
4e855a6e TL |
769 | } else { |
770 | *nplanes = q_data->fmt->num_planes; | |
556f4408 | 771 | for (i = 0; i < *nplanes; i++) |
4e855a6e | 772 | sizes[i] = q_data->sizeimage[i]; |
4e855a6e TL |
773 | } |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
778 | static int vb2ops_venc_buf_prepare(struct vb2_buffer *vb) | |
779 | { | |
780 | struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | |
781 | struct mtk_q_data *q_data; | |
782 | int i; | |
783 | ||
784 | q_data = mtk_venc_get_q_data(ctx, vb->vb2_queue->type); | |
785 | ||
786 | for (i = 0; i < q_data->fmt->num_planes; i++) { | |
787 | if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) { | |
788 | mtk_v4l2_err("data will not fit into plane %d (%lu < %d)", | |
789 | i, vb2_plane_size(vb, i), | |
790 | q_data->sizeimage[i]); | |
791 | return -EINVAL; | |
792 | } | |
793 | } | |
794 | ||
795 | return 0; | |
796 | } | |
797 | ||
798 | static void vb2ops_venc_buf_queue(struct vb2_buffer *vb) | |
799 | { | |
800 | struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | |
801 | struct vb2_v4l2_buffer *vb2_v4l2 = | |
802 | container_of(vb, struct vb2_v4l2_buffer, vb2_buf); | |
803 | ||
804 | struct mtk_video_enc_buf *mtk_buf = | |
805 | container_of(vb2_v4l2, struct mtk_video_enc_buf, vb); | |
806 | ||
807 | if ((vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) && | |
808 | (ctx->param_change != MTK_ENCODE_PARAM_NONE)) { | |
809 | mtk_v4l2_debug(1, "[%d] Before id=%d encode parameter change %x", | |
810 | ctx->id, | |
811 | mtk_buf->vb.vb2_buf.index, | |
812 | ctx->param_change); | |
813 | mtk_buf->param_change = ctx->param_change; | |
814 | mtk_buf->enc_params = ctx->enc_params; | |
815 | ctx->param_change = MTK_ENCODE_PARAM_NONE; | |
816 | } | |
817 | ||
818 | v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb)); | |
819 | } | |
820 | ||
821 | static int vb2ops_venc_start_streaming(struct vb2_queue *q, unsigned int count) | |
822 | { | |
823 | struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q); | |
824 | struct venc_enc_param param; | |
825 | int ret; | |
826 | int i; | |
827 | ||
828 | /* Once state turn into MTK_STATE_ABORT, we need stop_streaming | |
829 | * to clear it | |
830 | */ | |
831 | if ((ctx->state == MTK_STATE_ABORT) || (ctx->state == MTK_STATE_FREE)) { | |
832 | ret = -EIO; | |
833 | goto err_set_param; | |
834 | } | |
835 | ||
836 | /* Do the initialization when both start_streaming have been called */ | |
837 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { | |
838 | if (!vb2_start_streaming_called(&ctx->m2m_ctx->cap_q_ctx.q)) | |
839 | return 0; | |
840 | } else { | |
841 | if (!vb2_start_streaming_called(&ctx->m2m_ctx->out_q_ctx.q)) | |
842 | return 0; | |
843 | } | |
844 | ||
845 | mtk_venc_set_param(ctx, ¶m); | |
846 | ret = venc_if_set_param(ctx, VENC_SET_PARAM_ENC, ¶m); | |
847 | if (ret) { | |
848 | mtk_v4l2_err("venc_if_set_param failed=%d", ret); | |
849 | ctx->state = MTK_STATE_ABORT; | |
850 | goto err_set_param; | |
851 | } | |
852 | ctx->param_change = MTK_ENCODE_PARAM_NONE; | |
853 | ||
854 | if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) && | |
855 | (ctx->enc_params.seq_hdr_mode != | |
856 | V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE)) { | |
857 | ret = venc_if_set_param(ctx, | |
858 | VENC_SET_PARAM_PREPEND_HEADER, | |
859 | NULL); | |
860 | if (ret) { | |
861 | mtk_v4l2_err("venc_if_set_param failed=%d", ret); | |
862 | ctx->state = MTK_STATE_ABORT; | |
863 | goto err_set_param; | |
864 | } | |
865 | ctx->state = MTK_STATE_HEADER; | |
866 | } | |
867 | ||
868 | return 0; | |
869 | ||
870 | err_set_param: | |
871 | for (i = 0; i < q->num_buffers; ++i) { | |
872 | if (q->bufs[i]->state == VB2_BUF_STATE_ACTIVE) { | |
873 | mtk_v4l2_debug(0, "[%d] id=%d, type=%d, %d -> VB2_BUF_STATE_QUEUED", | |
874 | ctx->id, i, q->type, | |
875 | (int)q->bufs[i]->state); | |
876 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(q->bufs[i]), | |
877 | VB2_BUF_STATE_QUEUED); | |
878 | } | |
879 | } | |
880 | ||
881 | return ret; | |
882 | } | |
883 | ||
884 | static void vb2ops_venc_stop_streaming(struct vb2_queue *q) | |
885 | { | |
886 | struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q); | |
887 | struct vb2_buffer *src_buf, *dst_buf; | |
888 | int ret; | |
889 | ||
890 | mtk_v4l2_debug(2, "[%d]-> type=%d", ctx->id, q->type); | |
891 | ||
892 | if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { | |
893 | while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) { | |
894 | dst_buf->planes[0].bytesused = 0; | |
895 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), | |
896 | VB2_BUF_STATE_ERROR); | |
897 | } | |
898 | } else { | |
899 | while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) | |
900 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf), | |
901 | VB2_BUF_STATE_ERROR); | |
902 | } | |
903 | ||
904 | if ((q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE && | |
905 | vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q)) || | |
906 | (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && | |
907 | vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q))) { | |
908 | mtk_v4l2_debug(1, "[%d]-> q type %d out=%d cap=%d", | |
909 | ctx->id, q->type, | |
910 | vb2_is_streaming(&ctx->m2m_ctx->out_q_ctx.q), | |
911 | vb2_is_streaming(&ctx->m2m_ctx->cap_q_ctx.q)); | |
912 | return; | |
913 | } | |
914 | ||
915 | /* Release the encoder if both streams are stopped. */ | |
916 | ret = venc_if_deinit(ctx); | |
917 | if (ret) | |
918 | mtk_v4l2_err("venc_if_deinit failed=%d", ret); | |
919 | ||
920 | ctx->state = MTK_STATE_FREE; | |
921 | } | |
922 | ||
923 | static struct vb2_ops mtk_venc_vb2_ops = { | |
924 | .queue_setup = vb2ops_venc_queue_setup, | |
925 | .buf_prepare = vb2ops_venc_buf_prepare, | |
926 | .buf_queue = vb2ops_venc_buf_queue, | |
927 | .wait_prepare = vb2_ops_wait_prepare, | |
928 | .wait_finish = vb2_ops_wait_finish, | |
929 | .start_streaming = vb2ops_venc_start_streaming, | |
930 | .stop_streaming = vb2ops_venc_stop_streaming, | |
931 | }; | |
932 | ||
933 | static int mtk_venc_encode_header(void *priv) | |
934 | { | |
935 | struct mtk_vcodec_ctx *ctx = priv; | |
936 | int ret; | |
16060f7e TL |
937 | struct vb2_buffer *src_buf, *dst_buf; |
938 | struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2; | |
4e855a6e TL |
939 | struct mtk_vcodec_mem bs_buf; |
940 | struct venc_done_result enc_result; | |
941 | ||
942 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); | |
943 | if (!dst_buf) { | |
944 | mtk_v4l2_debug(1, "No dst buffer"); | |
945 | return -EINVAL; | |
946 | } | |
947 | ||
948 | bs_buf.va = vb2_plane_vaddr(dst_buf, 0); | |
949 | bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); | |
950 | bs_buf.size = (size_t)dst_buf->planes[0].length; | |
951 | ||
952 | mtk_v4l2_debug(1, | |
953 | "[%d] buf id=%d va=0x%p dma_addr=0x%llx size=%zu", | |
954 | ctx->id, | |
955 | dst_buf->index, bs_buf.va, | |
956 | (u64)bs_buf.dma_addr, | |
957 | bs_buf.size); | |
958 | ||
959 | ret = venc_if_encode(ctx, | |
960 | VENC_START_OPT_ENCODE_SEQUENCE_HEADER, | |
961 | NULL, &bs_buf, &enc_result); | |
962 | ||
963 | if (ret) { | |
964 | dst_buf->planes[0].bytesused = 0; | |
965 | ctx->state = MTK_STATE_ABORT; | |
966 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), | |
967 | VB2_BUF_STATE_ERROR); | |
968 | mtk_v4l2_err("venc_if_encode failed=%d", ret); | |
969 | return -EINVAL; | |
970 | } | |
16060f7e TL |
971 | src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); |
972 | if (src_buf) { | |
973 | src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf); | |
974 | dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf); | |
975 | dst_buf->timestamp = src_buf->timestamp; | |
976 | dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode; | |
977 | } else { | |
978 | mtk_v4l2_err("No timestamp for the header buffer."); | |
979 | } | |
4e855a6e TL |
980 | |
981 | ctx->state = MTK_STATE_HEADER; | |
982 | dst_buf->planes[0].bytesused = enc_result.bs_size; | |
983 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), VB2_BUF_STATE_DONE); | |
984 | ||
985 | return 0; | |
986 | } | |
987 | ||
988 | static int mtk_venc_param_change(struct mtk_vcodec_ctx *ctx) | |
989 | { | |
990 | struct venc_enc_param enc_prm; | |
991 | struct vb2_buffer *vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx); | |
992 | struct vb2_v4l2_buffer *vb2_v4l2 = | |
993 | container_of(vb, struct vb2_v4l2_buffer, vb2_buf); | |
994 | struct mtk_video_enc_buf *mtk_buf = | |
995 | container_of(vb2_v4l2, struct mtk_video_enc_buf, vb); | |
996 | ||
997 | int ret = 0; | |
998 | ||
999 | memset(&enc_prm, 0, sizeof(enc_prm)); | |
1000 | if (mtk_buf->param_change == MTK_ENCODE_PARAM_NONE) | |
1001 | return 0; | |
1002 | ||
1003 | if (mtk_buf->param_change & MTK_ENCODE_PARAM_BITRATE) { | |
1004 | enc_prm.bitrate = mtk_buf->enc_params.bitrate; | |
1005 | mtk_v4l2_debug(1, "[%d] id=%d, change param br=%d", | |
1006 | ctx->id, | |
1007 | mtk_buf->vb.vb2_buf.index, | |
1008 | enc_prm.bitrate); | |
1009 | ret |= venc_if_set_param(ctx, | |
1010 | VENC_SET_PARAM_ADJUST_BITRATE, | |
1011 | &enc_prm); | |
1012 | } | |
1013 | if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FRAMERATE) { | |
1014 | enc_prm.frm_rate = mtk_buf->enc_params.framerate_num / | |
1015 | mtk_buf->enc_params.framerate_denom; | |
1016 | mtk_v4l2_debug(1, "[%d] id=%d, change param fr=%d", | |
1017 | ctx->id, | |
1018 | mtk_buf->vb.vb2_buf.index, | |
1019 | enc_prm.frm_rate); | |
1020 | ret |= venc_if_set_param(ctx, | |
1021 | VENC_SET_PARAM_ADJUST_FRAMERATE, | |
1022 | &enc_prm); | |
1023 | } | |
1024 | if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_GOP_SIZE) { | |
1025 | enc_prm.gop_size = mtk_buf->enc_params.gop_size; | |
1026 | mtk_v4l2_debug(1, "change param intra period=%d", | |
1027 | enc_prm.gop_size); | |
1028 | ret |= venc_if_set_param(ctx, | |
1029 | VENC_SET_PARAM_GOP_SIZE, | |
1030 | &enc_prm); | |
1031 | } | |
1032 | if (!ret && mtk_buf->param_change & MTK_ENCODE_PARAM_FORCE_INTRA) { | |
1033 | mtk_v4l2_debug(1, "[%d] id=%d, change param force I=%d", | |
1034 | ctx->id, | |
1035 | mtk_buf->vb.vb2_buf.index, | |
1036 | mtk_buf->enc_params.force_intra); | |
1037 | if (mtk_buf->enc_params.force_intra) | |
1038 | ret |= venc_if_set_param(ctx, | |
1039 | VENC_SET_PARAM_FORCE_INTRA, | |
1040 | NULL); | |
1041 | } | |
1042 | ||
1043 | mtk_buf->param_change = MTK_ENCODE_PARAM_NONE; | |
1044 | ||
1045 | if (ret) { | |
1046 | ctx->state = MTK_STATE_ABORT; | |
1047 | mtk_v4l2_err("venc_if_set_param %d failed=%d", | |
1048 | mtk_buf->param_change, ret); | |
1049 | return -1; | |
1050 | } | |
1051 | ||
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | /* | |
1056 | * v4l2_m2m_streamoff() holds dev_mutex and waits mtk_venc_worker() | |
1057 | * to call v4l2_m2m_job_finish(). | |
1058 | * If mtk_venc_worker() tries to acquire dev_mutex, it will deadlock. | |
1059 | * So this function must not try to acquire dev->dev_mutex. | |
1060 | * This means v4l2 ioctls and mtk_venc_worker() can run at the same time. | |
1061 | * mtk_venc_worker() should be carefully implemented to avoid bugs. | |
1062 | */ | |
1063 | static void mtk_venc_worker(struct work_struct *work) | |
1064 | { | |
1065 | struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx, | |
1066 | encode_work); | |
1067 | struct vb2_buffer *src_buf, *dst_buf; | |
1068 | struct venc_frm_buf frm_buf; | |
1069 | struct mtk_vcodec_mem bs_buf; | |
1070 | struct venc_done_result enc_result; | |
1071 | int ret, i; | |
16060f7e | 1072 | struct vb2_v4l2_buffer *dst_vb2_v4l2, *src_vb2_v4l2; |
4e855a6e TL |
1073 | |
1074 | /* check dst_buf, dst_buf may be removed in device_run | |
1075 | * to stored encdoe header so we need check dst_buf and | |
1076 | * call job_finish here to prevent recursion | |
1077 | */ | |
1078 | dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); | |
1079 | if (!dst_buf) { | |
1080 | v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx); | |
1081 | return; | |
1082 | } | |
1083 | ||
1084 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); | |
1085 | memset(&frm_buf, 0, sizeof(frm_buf)); | |
1086 | for (i = 0; i < src_buf->num_planes ; i++) { | |
1087 | frm_buf.fb_addr[i].va = vb2_plane_vaddr(src_buf, i); | |
1088 | frm_buf.fb_addr[i].dma_addr = | |
1089 | vb2_dma_contig_plane_dma_addr(src_buf, i); | |
1090 | frm_buf.fb_addr[i].size = | |
1091 | (size_t)src_buf->planes[i].length; | |
1092 | } | |
1093 | bs_buf.va = vb2_plane_vaddr(dst_buf, 0); | |
1094 | bs_buf.dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); | |
1095 | bs_buf.size = (size_t)dst_buf->planes[0].length; | |
1096 | ||
1097 | mtk_v4l2_debug(2, | |
e0f80d8d | 1098 | "Framebuf VA=%p PA=%llx Size=0x%zx;VA=%p PA=0x%llx Size=0x%zx;VA=%p PA=0x%llx Size=%zu", |
4e855a6e TL |
1099 | frm_buf.fb_addr[0].va, |
1100 | (u64)frm_buf.fb_addr[0].dma_addr, | |
1101 | frm_buf.fb_addr[0].size, | |
1102 | frm_buf.fb_addr[1].va, | |
1103 | (u64)frm_buf.fb_addr[1].dma_addr, | |
1104 | frm_buf.fb_addr[1].size, | |
1105 | frm_buf.fb_addr[2].va, | |
1106 | (u64)frm_buf.fb_addr[2].dma_addr, | |
1107 | frm_buf.fb_addr[2].size); | |
1108 | ||
1109 | ret = venc_if_encode(ctx, VENC_START_OPT_ENCODE_FRAME, | |
1110 | &frm_buf, &bs_buf, &enc_result); | |
1111 | ||
16060f7e TL |
1112 | src_vb2_v4l2 = to_vb2_v4l2_buffer(src_buf); |
1113 | dst_vb2_v4l2 = to_vb2_v4l2_buffer(dst_buf); | |
1114 | ||
1115 | dst_buf->timestamp = src_buf->timestamp; | |
1116 | dst_vb2_v4l2->timecode = src_vb2_v4l2->timecode; | |
1117 | ||
4e855a6e | 1118 | if (enc_result.is_key_frm) |
16060f7e | 1119 | dst_vb2_v4l2->flags |= V4L2_BUF_FLAG_KEYFRAME; |
4e855a6e TL |
1120 | |
1121 | if (ret) { | |
1122 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf), | |
1123 | VB2_BUF_STATE_ERROR); | |
1124 | dst_buf->planes[0].bytesused = 0; | |
1125 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), | |
1126 | VB2_BUF_STATE_ERROR); | |
1127 | mtk_v4l2_err("venc_if_encode failed=%d", ret); | |
1128 | } else { | |
1129 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf), | |
1130 | VB2_BUF_STATE_DONE); | |
1131 | dst_buf->planes[0].bytesused = enc_result.bs_size; | |
1132 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), | |
1133 | VB2_BUF_STATE_DONE); | |
1134 | mtk_v4l2_debug(2, "venc_if_encode bs size=%d", | |
1135 | enc_result.bs_size); | |
1136 | } | |
1137 | ||
1138 | v4l2_m2m_job_finish(ctx->dev->m2m_dev_enc, ctx->m2m_ctx); | |
1139 | ||
1140 | mtk_v4l2_debug(1, "<=== src_buf[%d] dst_buf[%d] venc_if_encode ret=%d Size=%u===>", | |
1141 | src_buf->index, dst_buf->index, ret, | |
1142 | enc_result.bs_size); | |
1143 | } | |
1144 | ||
1145 | static void m2mops_venc_device_run(void *priv) | |
1146 | { | |
1147 | struct mtk_vcodec_ctx *ctx = priv; | |
1148 | ||
1149 | if ((ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc == V4L2_PIX_FMT_H264) && | |
1150 | (ctx->state != MTK_STATE_HEADER)) { | |
1151 | /* encode h264 sps/pps header */ | |
1152 | mtk_venc_encode_header(ctx); | |
1153 | queue_work(ctx->dev->encode_workqueue, &ctx->encode_work); | |
1154 | return; | |
1155 | } | |
1156 | ||
1157 | mtk_venc_param_change(ctx); | |
1158 | queue_work(ctx->dev->encode_workqueue, &ctx->encode_work); | |
1159 | } | |
1160 | ||
1161 | static int m2mops_venc_job_ready(void *m2m_priv) | |
1162 | { | |
1163 | struct mtk_vcodec_ctx *ctx = m2m_priv; | |
1164 | ||
1165 | if (ctx->state == MTK_STATE_ABORT || ctx->state == MTK_STATE_FREE) { | |
1166 | mtk_v4l2_debug(3, "[%d]Not ready: state=0x%x.", | |
1167 | ctx->id, ctx->state); | |
1168 | return 0; | |
1169 | } | |
1170 | ||
1171 | return 1; | |
1172 | } | |
1173 | ||
1174 | static void m2mops_venc_job_abort(void *priv) | |
1175 | { | |
1176 | struct mtk_vcodec_ctx *ctx = priv; | |
1177 | ||
1178 | ctx->state = MTK_STATE_ABORT; | |
1179 | } | |
1180 | ||
1181 | static void m2mops_venc_lock(void *m2m_priv) | |
1182 | { | |
1183 | struct mtk_vcodec_ctx *ctx = m2m_priv; | |
1184 | ||
1185 | mutex_lock(&ctx->dev->dev_mutex); | |
1186 | } | |
1187 | ||
1188 | static void m2mops_venc_unlock(void *m2m_priv) | |
1189 | { | |
1190 | struct mtk_vcodec_ctx *ctx = m2m_priv; | |
1191 | ||
1192 | mutex_unlock(&ctx->dev->dev_mutex); | |
1193 | } | |
1194 | ||
1195 | const struct v4l2_m2m_ops mtk_venc_m2m_ops = { | |
1196 | .device_run = m2mops_venc_device_run, | |
1197 | .job_ready = m2mops_venc_job_ready, | |
1198 | .job_abort = m2mops_venc_job_abort, | |
1199 | .lock = m2mops_venc_lock, | |
1200 | .unlock = m2mops_venc_unlock, | |
1201 | }; | |
1202 | ||
1203 | void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx) | |
1204 | { | |
1205 | struct mtk_q_data *q_data; | |
1206 | ||
1207 | ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex; | |
1208 | ctx->fh.m2m_ctx = ctx->m2m_ctx; | |
1209 | ctx->fh.ctrl_handler = &ctx->ctrl_hdl; | |
1210 | INIT_WORK(&ctx->encode_work, mtk_venc_worker); | |
1211 | ||
1212 | ctx->colorspace = V4L2_COLORSPACE_REC709; | |
1213 | ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; | |
1214 | ctx->quantization = V4L2_QUANTIZATION_DEFAULT; | |
1215 | ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; | |
1216 | ||
1217 | q_data = &ctx->q_data[MTK_Q_DATA_SRC]; | |
1218 | memset(q_data, 0, sizeof(struct mtk_q_data)); | |
1219 | q_data->visible_width = DFT_CFG_WIDTH; | |
1220 | q_data->visible_height = DFT_CFG_HEIGHT; | |
1221 | q_data->coded_width = DFT_CFG_WIDTH; | |
1222 | q_data->coded_height = DFT_CFG_HEIGHT; | |
1223 | q_data->field = V4L2_FIELD_NONE; | |
1224 | ||
1225 | q_data->fmt = &mtk_video_formats[OUT_FMT_IDX]; | |
1226 | ||
1227 | v4l_bound_align_image(&q_data->coded_width, | |
1228 | MTK_VENC_MIN_W, | |
1229 | MTK_VENC_MAX_W, 4, | |
1230 | &q_data->coded_height, | |
1231 | MTK_VENC_MIN_H, | |
1232 | MTK_VENC_MAX_H, 5, 6); | |
1233 | ||
1234 | if (q_data->coded_width < DFT_CFG_WIDTH && | |
1235 | (q_data->coded_width + 16) <= MTK_VENC_MAX_W) | |
1236 | q_data->coded_width += 16; | |
1237 | if (q_data->coded_height < DFT_CFG_HEIGHT && | |
1238 | (q_data->coded_height + 32) <= MTK_VENC_MAX_H) | |
1239 | q_data->coded_height += 32; | |
1240 | ||
8d4eb2ac TL |
1241 | q_data->sizeimage[0] = |
1242 | q_data->coded_width * q_data->coded_height+ | |
1243 | ((ALIGN(q_data->coded_width, 16) * 2) * 16); | |
4e855a6e | 1244 | q_data->bytesperline[0] = q_data->coded_width; |
8d4eb2ac TL |
1245 | q_data->sizeimage[1] = |
1246 | (q_data->coded_width * q_data->coded_height) / 2 + | |
1247 | (ALIGN(q_data->coded_width, 16) * 16); | |
4e855a6e TL |
1248 | q_data->bytesperline[1] = q_data->coded_width; |
1249 | ||
1250 | q_data = &ctx->q_data[MTK_Q_DATA_DST]; | |
1251 | memset(q_data, 0, sizeof(struct mtk_q_data)); | |
1252 | q_data->coded_width = DFT_CFG_WIDTH; | |
1253 | q_data->coded_height = DFT_CFG_HEIGHT; | |
1254 | q_data->fmt = &mtk_video_formats[CAP_FMT_IDX]; | |
1255 | q_data->field = V4L2_FIELD_NONE; | |
1256 | ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] = | |
1257 | DFT_CFG_WIDTH * DFT_CFG_HEIGHT; | |
1258 | ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 0; | |
1259 | ||
1260 | } | |
1261 | ||
1262 | int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx) | |
1263 | { | |
1264 | const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops; | |
1265 | struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl; | |
1266 | ||
1267 | v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT); | |
1268 | ||
1269 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_BITRATE, | |
1270 | 1, 4000000, 1, 4000000); | |
1271 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_B_FRAMES, | |
1272 | 0, 2, 1, 0); | |
1273 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, | |
1274 | 0, 1, 1, 1); | |
1275 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_MAX_QP, | |
1276 | 0, 51, 1, 51); | |
1277 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, | |
1278 | 0, 65535, 1, 0); | |
1279 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE, | |
1280 | 0, 65535, 1, 0); | |
1281 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_MB_RC_ENABLE, | |
1282 | 0, 1, 1, 0); | |
1283 | v4l2_ctrl_new_std(handler, ops, V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, | |
1284 | 0, 0, 0, 0); | |
1285 | v4l2_ctrl_new_std_menu(handler, ops, | |
1286 | V4L2_CID_MPEG_VIDEO_HEADER_MODE, | |
1287 | V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, | |
1288 | 0, V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE); | |
1289 | v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE, | |
1290 | V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, | |
158d6071 | 1291 | 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); |
4e855a6e TL |
1292 | v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, |
1293 | V4L2_MPEG_VIDEO_H264_LEVEL_4_2, | |
1294 | 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0); | |
1295 | if (handler->error) { | |
1296 | mtk_v4l2_err("Init control handler fail %d", | |
1297 | handler->error); | |
1298 | return handler->error; | |
1299 | } | |
1300 | ||
1301 | v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); | |
1302 | ||
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | int mtk_vcodec_enc_queue_init(void *priv, struct vb2_queue *src_vq, | |
1307 | struct vb2_queue *dst_vq) | |
1308 | { | |
1309 | struct mtk_vcodec_ctx *ctx = priv; | |
1310 | int ret; | |
1311 | ||
1312 | /* Note: VB2_USERPTR works with dma-contig because mt8173 | |
1313 | * support iommu | |
1314 | * https://patchwork.kernel.org/patch/8335461/ | |
1315 | * https://patchwork.kernel.org/patch/7596181/ | |
1316 | */ | |
1317 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; | |
1318 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; | |
1319 | src_vq->drv_priv = ctx; | |
1320 | src_vq->buf_struct_size = sizeof(struct mtk_video_enc_buf); | |
1321 | src_vq->ops = &mtk_venc_vb2_ops; | |
1322 | src_vq->mem_ops = &vb2_dma_contig_memops; | |
1323 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; | |
1324 | src_vq->lock = &ctx->dev->dev_mutex; | |
556f4408 | 1325 | src_vq->dev = &ctx->dev->plat_dev->dev; |
4e855a6e TL |
1326 | |
1327 | ret = vb2_queue_init(src_vq); | |
1328 | if (ret) | |
1329 | return ret; | |
1330 | ||
1331 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1332 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; | |
1333 | dst_vq->drv_priv = ctx; | |
1334 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); | |
1335 | dst_vq->ops = &mtk_venc_vb2_ops; | |
1336 | dst_vq->mem_ops = &vb2_dma_contig_memops; | |
1337 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; | |
1338 | dst_vq->lock = &ctx->dev->dev_mutex; | |
556f4408 | 1339 | dst_vq->dev = &ctx->dev->plat_dev->dev; |
4e855a6e TL |
1340 | |
1341 | return vb2_queue_init(dst_vq); | |
1342 | } | |
1343 | ||
1344 | int mtk_venc_unlock(struct mtk_vcodec_ctx *ctx) | |
1345 | { | |
1346 | struct mtk_vcodec_dev *dev = ctx->dev; | |
1347 | ||
1348 | mutex_unlock(&dev->enc_mutex); | |
1349 | return 0; | |
1350 | } | |
1351 | ||
1352 | int mtk_venc_lock(struct mtk_vcodec_ctx *ctx) | |
1353 | { | |
1354 | struct mtk_vcodec_dev *dev = ctx->dev; | |
1355 | ||
1356 | mutex_lock(&dev->enc_mutex); | |
1357 | return 0; | |
1358 | } | |
1359 | ||
1360 | void mtk_vcodec_enc_release(struct mtk_vcodec_ctx *ctx) | |
1361 | { | |
ad34f541 TL |
1362 | int ret = venc_if_deinit(ctx); |
1363 | ||
1364 | if (ret) | |
1365 | mtk_v4l2_err("venc_if_deinit failed=%d", ret); | |
1366 | ||
1367 | ctx->state = MTK_STATE_FREE; | |
4e855a6e | 1368 | } |