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