1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: PC Chen <pc.chen@mediatek.com>
5  *         Tiffany Lin <tiffany.lin@mediatek.com>
6  */
7 
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
11 
12 #include "mtk_vcodec_drv.h"
13 #include "mtk_vcodec_dec.h"
14 #include "mtk_vcodec_intr.h"
15 #include "mtk_vcodec_util.h"
16 #include "vdec_drv_if.h"
17 #include "mtk_vcodec_dec_pm.h"
18 
19 #define DFT_CFG_WIDTH	MTK_VDEC_MIN_W
20 #define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H
21 
22 static const struct mtk_video_fmt *
mtk_vdec_find_format(struct v4l2_format * f,const struct mtk_vcodec_dec_pdata * dec_pdata)23 mtk_vdec_find_format(struct v4l2_format *f,
24 		     const struct mtk_vcodec_dec_pdata *dec_pdata)
25 {
26 	const struct mtk_video_fmt *fmt;
27 	unsigned int k;
28 
29 	for (k = 0; k < dec_pdata->num_formats; k++) {
30 		fmt = &dec_pdata->vdec_formats[k];
31 		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
32 			return fmt;
33 	}
34 
35 	return NULL;
36 }
37 
mtk_vdec_get_q_data(struct mtk_vcodec_ctx * ctx,enum v4l2_buf_type type)38 static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
39 					      enum v4l2_buf_type type)
40 {
41 	if (V4L2_TYPE_IS_OUTPUT(type))
42 		return &ctx->q_data[MTK_Q_DATA_SRC];
43 
44 	return &ctx->q_data[MTK_Q_DATA_DST];
45 }
46 
vidioc_try_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)47 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
48 				struct v4l2_decoder_cmd *cmd)
49 {
50 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
51 
52 	/* Use M2M stateless helper if relevant */
53 	if (ctx->dev->vdec_pdata->uses_stateless_api)
54 		return v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv,
55 								cmd);
56 	else
57 		return v4l2_m2m_ioctl_try_decoder_cmd(file, priv, cmd);
58 }
59 
60 
vidioc_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)61 static int vidioc_decoder_cmd(struct file *file, void *priv,
62 				struct v4l2_decoder_cmd *cmd)
63 {
64 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
65 	struct vb2_queue *src_vq, *dst_vq;
66 	int ret;
67 
68 	ret = vidioc_try_decoder_cmd(file, priv, cmd);
69 	if (ret)
70 		return ret;
71 
72 	/* Use M2M stateless helper if relevant */
73 	if (ctx->dev->vdec_pdata->uses_stateless_api)
74 		return v4l2_m2m_ioctl_stateless_decoder_cmd(file, priv, cmd);
75 
76 	mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
77 	dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
78 				V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
79 	switch (cmd->cmd) {
80 	case V4L2_DEC_CMD_STOP:
81 		src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
82 				V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
83 		if (!vb2_is_streaming(src_vq)) {
84 			mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
85 			return 0;
86 		}
87 		if (!vb2_is_streaming(dst_vq)) {
88 			mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
89 			return 0;
90 		}
91 		v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);
92 		v4l2_m2m_try_schedule(ctx->m2m_ctx);
93 		break;
94 
95 	case V4L2_DEC_CMD_START:
96 		vb2_clear_last_buffer_dequeued(dst_vq);
97 		break;
98 
99 	default:
100 		return -EINVAL;
101 	}
102 
103 	return 0;
104 }
105 
mtk_vdec_unlock(struct mtk_vcodec_ctx * ctx)106 void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
107 {
108 	mutex_unlock(&ctx->dev->dec_mutex);
109 }
110 
mtk_vdec_lock(struct mtk_vcodec_ctx * ctx)111 void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
112 {
113 	mutex_lock(&ctx->dev->dec_mutex);
114 }
115 
mtk_vcodec_dec_release(struct mtk_vcodec_ctx * ctx)116 void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
117 {
118 	vdec_if_deinit(ctx);
119 	ctx->state = MTK_STATE_FREE;
120 }
121 
mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx * ctx)122 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
123 {
124 	struct mtk_q_data *q_data;
125 
126 	ctx->dev->vdec_pdata->init_vdec_params(ctx);
127 
128 	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
129 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
130 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
131 	INIT_WORK(&ctx->decode_work, ctx->dev->vdec_pdata->worker);
132 	ctx->colorspace = V4L2_COLORSPACE_REC709;
133 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
134 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
135 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
136 
137 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];
138 	memset(q_data, 0, sizeof(struct mtk_q_data));
139 	q_data->visible_width = DFT_CFG_WIDTH;
140 	q_data->visible_height = DFT_CFG_HEIGHT;
141 	q_data->fmt = ctx->dev->vdec_pdata->default_out_fmt;
142 	q_data->field = V4L2_FIELD_NONE;
143 
144 	q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
145 	q_data->bytesperline[0] = 0;
146 
147 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
148 	memset(q_data, 0, sizeof(struct mtk_q_data));
149 	q_data->visible_width = DFT_CFG_WIDTH;
150 	q_data->visible_height = DFT_CFG_HEIGHT;
151 	q_data->coded_width = DFT_CFG_WIDTH;
152 	q_data->coded_height = DFT_CFG_HEIGHT;
153 	q_data->fmt = ctx->dev->vdec_pdata->default_cap_fmt;
154 	q_data->field = V4L2_FIELD_NONE;
155 
156 	v4l_bound_align_image(&q_data->coded_width,
157 				MTK_VDEC_MIN_W,
158 				MTK_VDEC_MAX_W, 4,
159 				&q_data->coded_height,
160 				MTK_VDEC_MIN_H,
161 				MTK_VDEC_MAX_H, 5, 6);
162 
163 	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
164 	q_data->bytesperline[0] = q_data->coded_width;
165 	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
166 	q_data->bytesperline[1] = q_data->coded_width;
167 }
168 
vidioc_vdec_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)169 static int vidioc_vdec_qbuf(struct file *file, void *priv,
170 			    struct v4l2_buffer *buf)
171 {
172 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
173 
174 	if (ctx->state == MTK_STATE_ABORT) {
175 		mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
176 				ctx->id);
177 		return -EIO;
178 	}
179 
180 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
181 }
182 
vidioc_vdec_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)183 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
184 			     struct v4l2_buffer *buf)
185 {
186 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
187 
188 	if (ctx->state == MTK_STATE_ABORT) {
189 		mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
190 				ctx->id);
191 		return -EIO;
192 	}
193 
194 	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
195 }
196 
vidioc_vdec_querycap(struct file * file,void * priv,struct v4l2_capability * cap)197 static int vidioc_vdec_querycap(struct file *file, void *priv,
198 				struct v4l2_capability *cap)
199 {
200 	strscpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
201 	strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
202 	strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
203 
204 	return 0;
205 }
206 
vidioc_vdec_subscribe_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)207 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
208 				     const struct v4l2_event_subscription *sub)
209 {
210 	switch (sub->type) {
211 	case V4L2_EVENT_EOS:
212 		return v4l2_event_subscribe(fh, sub, 2, NULL);
213 	case V4L2_EVENT_SOURCE_CHANGE:
214 		return v4l2_src_change_event_subscribe(fh, sub);
215 	default:
216 		return v4l2_ctrl_subscribe_event(fh, sub);
217 	}
218 }
219 
vidioc_try_fmt(struct v4l2_format * f,const struct mtk_video_fmt * fmt)220 static int vidioc_try_fmt(struct v4l2_format *f,
221 			  const struct mtk_video_fmt *fmt)
222 {
223 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
224 
225 	pix_fmt_mp->field = V4L2_FIELD_NONE;
226 
227 	pix_fmt_mp->width =
228 		clamp(pix_fmt_mp->width, MTK_VDEC_MIN_W, MTK_VDEC_MAX_W);
229 	pix_fmt_mp->height =
230 		clamp(pix_fmt_mp->height, MTK_VDEC_MIN_H, MTK_VDEC_MAX_H);
231 
232 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
233 		pix_fmt_mp->num_planes = 1;
234 		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
235 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
236 		int tmp_w, tmp_h;
237 
238 		/*
239 		 * Find next closer width align 64, heign align 64, size align
240 		 * 64 rectangle
241 		 * Note: This only get default value, the real HW needed value
242 		 *       only available when ctx in MTK_STATE_HEADER state
243 		 */
244 		tmp_w = pix_fmt_mp->width;
245 		tmp_h = pix_fmt_mp->height;
246 		v4l_bound_align_image(&pix_fmt_mp->width,
247 					MTK_VDEC_MIN_W,
248 					MTK_VDEC_MAX_W, 6,
249 					&pix_fmt_mp->height,
250 					MTK_VDEC_MIN_H,
251 					MTK_VDEC_MAX_H, 6, 9);
252 
253 		if (pix_fmt_mp->width < tmp_w &&
254 			(pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
255 			pix_fmt_mp->width += 64;
256 		if (pix_fmt_mp->height < tmp_h &&
257 			(pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
258 			pix_fmt_mp->height += 64;
259 
260 		mtk_v4l2_debug(0,
261 			"before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
262 			tmp_w, tmp_h, pix_fmt_mp->width,
263 			pix_fmt_mp->height,
264 			pix_fmt_mp->width * pix_fmt_mp->height);
265 
266 		pix_fmt_mp->num_planes = fmt->num_planes;
267 		pix_fmt_mp->plane_fmt[0].sizeimage =
268 				pix_fmt_mp->width * pix_fmt_mp->height;
269 		pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
270 
271 		if (pix_fmt_mp->num_planes == 2) {
272 			pix_fmt_mp->plane_fmt[1].sizeimage =
273 				(pix_fmt_mp->width * pix_fmt_mp->height) / 2;
274 			pix_fmt_mp->plane_fmt[1].bytesperline =
275 				pix_fmt_mp->width;
276 		}
277 	}
278 
279 	pix_fmt_mp->flags = 0;
280 	return 0;
281 }
282 
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)283 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
284 				struct v4l2_format *f)
285 {
286 	const struct mtk_video_fmt *fmt;
287 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
288 	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
289 
290 	fmt = mtk_vdec_find_format(f, dec_pdata);
291 	if (!fmt) {
292 		f->fmt.pix.pixelformat =
293 			ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
294 		fmt = mtk_vdec_find_format(f, dec_pdata);
295 	}
296 
297 	return vidioc_try_fmt(f, fmt);
298 }
299 
vidioc_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)300 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
301 				struct v4l2_format *f)
302 {
303 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
304 	const struct mtk_video_fmt *fmt;
305 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
306 	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
307 
308 	fmt = mtk_vdec_find_format(f, dec_pdata);
309 	if (!fmt) {
310 		f->fmt.pix.pixelformat =
311 			ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
312 		fmt = mtk_vdec_find_format(f, dec_pdata);
313 	}
314 
315 	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
316 		mtk_v4l2_err("sizeimage of output format must be given");
317 		return -EINVAL;
318 	}
319 
320 	return vidioc_try_fmt(f, fmt);
321 }
322 
vidioc_vdec_g_selection(struct file * file,void * priv,struct v4l2_selection * s)323 static int vidioc_vdec_g_selection(struct file *file, void *priv,
324 			struct v4l2_selection *s)
325 {
326 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
327 	struct mtk_q_data *q_data;
328 
329 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
330 		return -EINVAL;
331 
332 	q_data = &ctx->q_data[MTK_Q_DATA_DST];
333 
334 	switch (s->target) {
335 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
336 		s->r.left = 0;
337 		s->r.top = 0;
338 		s->r.width = ctx->picinfo.pic_w;
339 		s->r.height = ctx->picinfo.pic_h;
340 		break;
341 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
342 		s->r.left = 0;
343 		s->r.top = 0;
344 		s->r.width = ctx->picinfo.buf_w;
345 		s->r.height = ctx->picinfo.buf_h;
346 		break;
347 	case V4L2_SEL_TGT_COMPOSE:
348 		if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
349 			/* set to default value if header info not ready yet*/
350 			s->r.left = 0;
351 			s->r.top = 0;
352 			s->r.width = q_data->visible_width;
353 			s->r.height = q_data->visible_height;
354 		}
355 		break;
356 	default:
357 		return -EINVAL;
358 	}
359 
360 	if (ctx->state < MTK_STATE_HEADER) {
361 		/* set to default value if header info not ready yet*/
362 		s->r.left = 0;
363 		s->r.top = 0;
364 		s->r.width = q_data->visible_width;
365 		s->r.height = q_data->visible_height;
366 		return 0;
367 	}
368 
369 	return 0;
370 }
371 
vidioc_vdec_s_selection(struct file * file,void * priv,struct v4l2_selection * s)372 static int vidioc_vdec_s_selection(struct file *file, void *priv,
373 				struct v4l2_selection *s)
374 {
375 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
376 
377 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
378 		return -EINVAL;
379 
380 	switch (s->target) {
381 	case V4L2_SEL_TGT_COMPOSE:
382 		s->r.left = 0;
383 		s->r.top = 0;
384 		s->r.width = ctx->picinfo.pic_w;
385 		s->r.height = ctx->picinfo.pic_h;
386 		break;
387 	default:
388 		return -EINVAL;
389 	}
390 
391 	return 0;
392 }
393 
vidioc_vdec_s_fmt(struct file * file,void * priv,struct v4l2_format * f)394 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
395 			     struct v4l2_format *f)
396 {
397 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
398 	struct v4l2_pix_format_mplane *pix_mp;
399 	struct mtk_q_data *q_data;
400 	int ret = 0;
401 	const struct mtk_video_fmt *fmt;
402 	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
403 
404 	mtk_v4l2_debug(3, "[%d]", ctx->id);
405 
406 	q_data = mtk_vdec_get_q_data(ctx, f->type);
407 	if (!q_data)
408 		return -EINVAL;
409 
410 	pix_mp = &f->fmt.pix_mp;
411 	/*
412 	 * Setting OUTPUT format after OUTPUT buffers are allocated is invalid
413 	 * if using the stateful API.
414 	 */
415 	if (!dec_pdata->uses_stateless_api &&
416 	    f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
417 	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
418 		mtk_v4l2_err("out_q_ctx buffers already requested");
419 		ret = -EBUSY;
420 	}
421 
422 	/*
423 	 * Setting CAPTURE format after CAPTURE buffers are allocated is
424 	 * invalid.
425 	 */
426 	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
427 	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
428 		mtk_v4l2_err("cap_q_ctx buffers already requested");
429 		ret = -EBUSY;
430 	}
431 
432 	fmt = mtk_vdec_find_format(f, dec_pdata);
433 	if (fmt == NULL) {
434 		if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
435 			f->fmt.pix.pixelformat =
436 				dec_pdata->default_out_fmt->fourcc;
437 			fmt = mtk_vdec_find_format(f, dec_pdata);
438 		} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
439 			f->fmt.pix.pixelformat =
440 				dec_pdata->default_cap_fmt->fourcc;
441 			fmt = mtk_vdec_find_format(f, dec_pdata);
442 		}
443 	}
444 	if (fmt == NULL)
445 		return -EINVAL;
446 
447 	q_data->fmt = fmt;
448 	vidioc_try_fmt(f, q_data->fmt);
449 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
450 		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
451 		q_data->coded_width = pix_mp->width;
452 		q_data->coded_height = pix_mp->height;
453 
454 		ctx->colorspace = pix_mp->colorspace;
455 		ctx->ycbcr_enc = pix_mp->ycbcr_enc;
456 		ctx->quantization = pix_mp->quantization;
457 		ctx->xfer_func = pix_mp->xfer_func;
458 
459 		ctx->current_codec = fmt->fourcc;
460 		if (ctx->state == MTK_STATE_FREE) {
461 			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
462 			if (ret) {
463 				mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
464 					ctx->id, ret);
465 				return -EINVAL;
466 			}
467 			ctx->state = MTK_STATE_INIT;
468 		}
469 	}
470 
471 	/*
472 	 * If using the stateless API, S_FMT should have the effect of setting
473 	 * the CAPTURE queue resolution no matter which queue it was called on.
474 	 */
475 	if (dec_pdata->uses_stateless_api) {
476 		ctx->picinfo.pic_w = pix_mp->width;
477 		ctx->picinfo.pic_h = pix_mp->height;
478 
479 		ret = vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo);
480 		if (ret) {
481 			mtk_v4l2_err("[%d]Error!! Get GET_PARAM_PICTURE_INFO Fail",
482 				     ctx->id);
483 			return -EINVAL;
484 		}
485 
486 		ctx->last_decoded_picinfo = ctx->picinfo;
487 
488 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1) {
489 			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
490 				ctx->picinfo.fb_sz[0] +
491 				ctx->picinfo.fb_sz[1];
492 			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
493 				ctx->picinfo.buf_w;
494 		} else {
495 			ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
496 				ctx->picinfo.fb_sz[0];
497 			ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
498 				ctx->picinfo.buf_w;
499 			ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
500 				ctx->picinfo.fb_sz[1];
501 			ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
502 				ctx->picinfo.buf_w;
503 		}
504 
505 		ctx->q_data[MTK_Q_DATA_DST].coded_width = ctx->picinfo.buf_w;
506 		ctx->q_data[MTK_Q_DATA_DST].coded_height = ctx->picinfo.buf_h;
507 		mtk_v4l2_debug(2, "[%d] vdec_if_init() num_plane = %d wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
508 			       ctx->id, pix_mp->num_planes, ctx->picinfo.buf_w, ctx->picinfo.buf_h,
509 			       ctx->picinfo.pic_w, ctx->picinfo.pic_h,
510 			       ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
511 			       ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
512 	}
513 	return 0;
514 }
515 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)516 static int vidioc_enum_framesizes(struct file *file, void *priv,
517 				struct v4l2_frmsizeenum *fsize)
518 {
519 	int i = 0;
520 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
521 	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
522 
523 	if (fsize->index != 0)
524 		return -EINVAL;
525 
526 	for (i = 0; i < dec_pdata->num_framesizes; ++i) {
527 		if (fsize->pixel_format != dec_pdata->vdec_framesizes[i].fourcc)
528 			continue;
529 
530 		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
531 		fsize->stepwise = dec_pdata->vdec_framesizes[i].stepwise;
532 		if (!(ctx->dev->dec_capability &
533 				VCODEC_CAPABILITY_4K_DISABLED)) {
534 			mtk_v4l2_debug(3, "4K is enabled");
535 			fsize->stepwise.max_width =
536 					VCODEC_DEC_4K_CODED_WIDTH;
537 			fsize->stepwise.max_height =
538 					VCODEC_DEC_4K_CODED_HEIGHT;
539 		}
540 		mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
541 				ctx->dev->dec_capability,
542 				fsize->stepwise.min_width,
543 				fsize->stepwise.max_width,
544 				fsize->stepwise.step_width,
545 				fsize->stepwise.min_height,
546 				fsize->stepwise.max_height,
547 				fsize->stepwise.step_height);
548 		return 0;
549 	}
550 
551 	return -EINVAL;
552 }
553 
vidioc_enum_fmt(struct v4l2_fmtdesc * f,void * priv,bool output_queue)554 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, void *priv,
555 			   bool output_queue)
556 {
557 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
558 	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
559 	const struct mtk_video_fmt *fmt;
560 	int i, j = 0;
561 
562 	for (i = 0; i < dec_pdata->num_formats; i++) {
563 		if (output_queue &&
564 		    dec_pdata->vdec_formats[i].type != MTK_FMT_DEC)
565 			continue;
566 		if (!output_queue &&
567 		    dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME)
568 			continue;
569 
570 		if (j == f->index)
571 			break;
572 		++j;
573 	}
574 
575 	if (i == dec_pdata->num_formats)
576 		return -EINVAL;
577 
578 	fmt = &dec_pdata->vdec_formats[i];
579 	f->pixelformat = fmt->fourcc;
580 	f->flags = fmt->flags;
581 
582 	return 0;
583 }
584 
vidioc_vdec_enum_fmt_vid_cap(struct file * file,void * priv,struct v4l2_fmtdesc * f)585 static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv,
586 					struct v4l2_fmtdesc *f)
587 {
588 	return vidioc_enum_fmt(f, priv, false);
589 }
590 
vidioc_vdec_enum_fmt_vid_out(struct file * file,void * priv,struct v4l2_fmtdesc * f)591 static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv,
592 					struct v4l2_fmtdesc *f)
593 {
594 	return vidioc_enum_fmt(f, priv, true);
595 }
596 
vidioc_vdec_g_fmt(struct file * file,void * priv,struct v4l2_format * f)597 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
598 			     struct v4l2_format *f)
599 {
600 	struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
601 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
602 	struct vb2_queue *vq;
603 	struct mtk_q_data *q_data;
604 
605 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
606 	if (!vq) {
607 		mtk_v4l2_err("no vb2 queue for type=%d", f->type);
608 		return -EINVAL;
609 	}
610 
611 	q_data = mtk_vdec_get_q_data(ctx, f->type);
612 
613 	pix_mp->field = V4L2_FIELD_NONE;
614 	pix_mp->colorspace = ctx->colorspace;
615 	pix_mp->ycbcr_enc = ctx->ycbcr_enc;
616 	pix_mp->quantization = ctx->quantization;
617 	pix_mp->xfer_func = ctx->xfer_func;
618 
619 	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
620 	    (ctx->state >= MTK_STATE_HEADER)) {
621 		/* Until STREAMOFF is called on the CAPTURE queue
622 		 * (acknowledging the event), the driver operates as if
623 		 * the resolution hasn't changed yet.
624 		 * So we just return picinfo yet, and update picinfo in
625 		 * stop_streaming hook function
626 		 */
627 		q_data->sizeimage[0] = ctx->picinfo.fb_sz[0];
628 		q_data->sizeimage[1] = ctx->picinfo.fb_sz[1];
629 		q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
630 		q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
631 		q_data->coded_width = ctx->picinfo.buf_w;
632 		q_data->coded_height = ctx->picinfo.buf_h;
633 		ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc;
634 
635 		/*
636 		 * Width and height are set to the dimensions
637 		 * of the movie, the buffer is bigger and
638 		 * further processing stages should crop to this
639 		 * rectangle.
640 		 */
641 		pix_mp->width = q_data->coded_width;
642 		pix_mp->height = q_data->coded_height;
643 
644 		/*
645 		 * Set pixelformat to the format in which mt vcodec
646 		 * outputs the decoded frame
647 		 */
648 		pix_mp->num_planes = q_data->fmt->num_planes;
649 		pix_mp->pixelformat = q_data->fmt->fourcc;
650 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
651 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
652 		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
653 		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
654 
655 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
656 		/*
657 		 * This is run on OUTPUT
658 		 * The buffer contains compressed image
659 		 * so width and height have no meaning.
660 		 * Assign value here to pass v4l2-compliance test
661 		 */
662 		pix_mp->width = q_data->visible_width;
663 		pix_mp->height = q_data->visible_height;
664 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
665 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
666 		pix_mp->pixelformat = q_data->fmt->fourcc;
667 		pix_mp->num_planes = q_data->fmt->num_planes;
668 	} else {
669 		pix_mp->width = q_data->coded_width;
670 		pix_mp->height = q_data->coded_height;
671 		pix_mp->num_planes = q_data->fmt->num_planes;
672 		pix_mp->pixelformat = q_data->fmt->fourcc;
673 		pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
674 		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
675 		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
676 		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
677 
678 		mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
679 				ctx->id, f->type, ctx->state);
680 	}
681 
682 	return 0;
683 }
684 
vb2ops_vdec_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])685 int vb2ops_vdec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
686 			    unsigned int *nplanes, unsigned int sizes[],
687 			    struct device *alloc_devs[])
688 {
689 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
690 	struct mtk_q_data *q_data;
691 	unsigned int i;
692 
693 	q_data = mtk_vdec_get_q_data(ctx, vq->type);
694 
695 	if (q_data == NULL) {
696 		mtk_v4l2_err("vq->type=%d err\n", vq->type);
697 		return -EINVAL;
698 	}
699 
700 	if (*nplanes) {
701 		for (i = 0; i < *nplanes; i++) {
702 			if (sizes[i] < q_data->sizeimage[i])
703 				return -EINVAL;
704 		}
705 	} else {
706 		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
707 			*nplanes = q_data->fmt->num_planes;
708 		else
709 			*nplanes = 1;
710 
711 		for (i = 0; i < *nplanes; i++)
712 			sizes[i] = q_data->sizeimage[i];
713 	}
714 
715 	mtk_v4l2_debug(1,
716 			"[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
717 			ctx->id, vq->type, *nplanes, *nbuffers,
718 			sizes[0], sizes[1]);
719 
720 	return 0;
721 }
722 
vb2ops_vdec_buf_prepare(struct vb2_buffer * vb)723 int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
724 {
725 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
726 	struct mtk_q_data *q_data;
727 	int i;
728 
729 	mtk_v4l2_debug(3, "[%d] (%d) id=%d",
730 			ctx->id, vb->vb2_queue->type, vb->index);
731 
732 	q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
733 
734 	for (i = 0; i < q_data->fmt->num_planes; i++) {
735 		if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
736 			mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
737 				i, vb2_plane_size(vb, i),
738 				q_data->sizeimage[i]);
739 		}
740 	}
741 
742 	return 0;
743 }
744 
vb2ops_vdec_buf_finish(struct vb2_buffer * vb)745 void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
746 {
747 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
748 	struct vb2_v4l2_buffer *vb2_v4l2;
749 	struct mtk_video_dec_buf *buf;
750 	bool buf_error;
751 
752 	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
753 	buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
754 	mutex_lock(&ctx->lock);
755 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
756 		buf->queued_in_v4l2 = false;
757 		buf->queued_in_vb2 = false;
758 	}
759 	buf_error = buf->error;
760 	mutex_unlock(&ctx->lock);
761 
762 	if (buf_error) {
763 		mtk_v4l2_err("Unrecoverable error on buffer.");
764 		ctx->state = MTK_STATE_ABORT;
765 	}
766 }
767 
vb2ops_vdec_buf_init(struct vb2_buffer * vb)768 int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
769 {
770 	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
771 					struct vb2_v4l2_buffer, vb2_buf);
772 	struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
773 					struct mtk_video_dec_buf, m2m_buf.vb);
774 
775 	if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
776 		buf->used = false;
777 		buf->queued_in_v4l2 = false;
778 	}
779 
780 	return 0;
781 }
782 
vb2ops_vdec_start_streaming(struct vb2_queue * q,unsigned int count)783 int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
784 {
785 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
786 
787 	if (ctx->state == MTK_STATE_FLUSH)
788 		ctx->state = MTK_STATE_HEADER;
789 
790 	return 0;
791 }
792 
vb2ops_vdec_stop_streaming(struct vb2_queue * q)793 void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
794 {
795 	struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL;
796 	struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
797 	int ret;
798 
799 	mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
800 			ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
801 
802 	if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
803 		while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
804 			if (src_buf != &ctx->empty_flush_buf.vb) {
805 				struct media_request *req =
806 					src_buf->vb2_buf.req_obj.req;
807 				v4l2_m2m_buf_done(src_buf,
808 						VB2_BUF_STATE_ERROR);
809 				if (req)
810 					v4l2_ctrl_request_complete(req, &ctx->ctrl_hdl);
811 			}
812 		}
813 		return;
814 	}
815 
816 	if (ctx->state >= MTK_STATE_HEADER) {
817 
818 		/* Until STREAMOFF is called on the CAPTURE queue
819 		 * (acknowledging the event), the driver operates
820 		 * as if the resolution hasn't changed yet, i.e.
821 		 * VIDIOC_G_FMT< etc. return previous resolution.
822 		 * So we update picinfo here
823 		 */
824 		ctx->picinfo = ctx->last_decoded_picinfo;
825 
826 		mtk_v4l2_debug(2,
827 				"[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
828 				ctx->id, ctx->last_decoded_picinfo.pic_w,
829 				ctx->last_decoded_picinfo.pic_h,
830 				ctx->picinfo.pic_w, ctx->picinfo.pic_h,
831 				ctx->last_decoded_picinfo.buf_w,
832 				ctx->last_decoded_picinfo.buf_h);
833 
834 		ret = ctx->dev->vdec_pdata->flush_decoder(ctx);
835 		if (ret)
836 			mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
837 	}
838 	ctx->state = MTK_STATE_FLUSH;
839 
840 	while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
841 		vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
842 		if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
843 			vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
844 		v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
845 	}
846 
847 }
848 
m2mops_vdec_device_run(void * priv)849 static void m2mops_vdec_device_run(void *priv)
850 {
851 	struct mtk_vcodec_ctx *ctx = priv;
852 	struct mtk_vcodec_dev *dev = ctx->dev;
853 
854 	queue_work(dev->decode_workqueue, &ctx->decode_work);
855 }
856 
m2mops_vdec_job_ready(void * m2m_priv)857 static int m2mops_vdec_job_ready(void *m2m_priv)
858 {
859 	struct mtk_vcodec_ctx *ctx = m2m_priv;
860 
861 	mtk_v4l2_debug(3, "[%d]", ctx->id);
862 
863 	if (ctx->state == MTK_STATE_ABORT)
864 		return 0;
865 
866 	if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
867 	    (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
868 		return 0;
869 
870 	if (ctx->state != MTK_STATE_HEADER)
871 		return 0;
872 
873 	return 1;
874 }
875 
m2mops_vdec_job_abort(void * priv)876 static void m2mops_vdec_job_abort(void *priv)
877 {
878 	struct mtk_vcodec_ctx *ctx = priv;
879 
880 	ctx->state = MTK_STATE_ABORT;
881 }
882 
883 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
884 	.device_run	= m2mops_vdec_device_run,
885 	.job_ready	= m2mops_vdec_job_ready,
886 	.job_abort	= m2mops_vdec_job_abort,
887 };
888 
889 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
890 	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
891 	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
892 	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
893 	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
894 	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
895 
896 	.vidioc_qbuf		= vidioc_vdec_qbuf,
897 	.vidioc_dqbuf		= vidioc_vdec_dqbuf,
898 
899 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
900 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
901 
902 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
903 	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
904 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
905 	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
906 
907 	.vidioc_create_bufs		= v4l2_m2m_ioctl_create_bufs,
908 
909 	.vidioc_enum_fmt_vid_cap	= vidioc_vdec_enum_fmt_vid_cap,
910 	.vidioc_enum_fmt_vid_out	= vidioc_vdec_enum_fmt_vid_out,
911 	.vidioc_enum_framesizes	= vidioc_enum_framesizes,
912 
913 	.vidioc_querycap		= vidioc_vdec_querycap,
914 	.vidioc_subscribe_event		= vidioc_vdec_subscribe_evt,
915 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
916 	.vidioc_g_selection             = vidioc_vdec_g_selection,
917 	.vidioc_s_selection             = vidioc_vdec_s_selection,
918 
919 	.vidioc_decoder_cmd = vidioc_decoder_cmd,
920 	.vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
921 };
922 
mtk_vcodec_dec_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)923 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
924 			   struct vb2_queue *dst_vq)
925 {
926 	struct mtk_vcodec_ctx *ctx = priv;
927 	int ret = 0;
928 
929 	mtk_v4l2_debug(3, "[%d]", ctx->id);
930 
931 	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
932 	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
933 	src_vq->drv_priv	= ctx;
934 	src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
935 	src_vq->ops		= ctx->dev->vdec_pdata->vdec_vb2_ops;
936 	src_vq->mem_ops		= &vb2_dma_contig_memops;
937 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
938 	src_vq->lock		= &ctx->dev->dev_mutex;
939 	src_vq->dev             = &ctx->dev->plat_dev->dev;
940 
941 	ret = vb2_queue_init(src_vq);
942 	if (ret) {
943 		mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
944 		return ret;
945 	}
946 	dst_vq->type		= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
947 	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
948 	dst_vq->drv_priv	= ctx;
949 	dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
950 	dst_vq->ops		= ctx->dev->vdec_pdata->vdec_vb2_ops;
951 	dst_vq->mem_ops		= &vb2_dma_contig_memops;
952 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
953 	dst_vq->lock		= &ctx->dev->dev_mutex;
954 	dst_vq->dev             = &ctx->dev->plat_dev->dev;
955 
956 	ret = vb2_queue_init(dst_vq);
957 	if (ret)
958 		mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
959 
960 	return ret;
961 }
962