brintos

brintos / linux-shallow public Read only

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