1041 lines · 29.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * Copyright (c) 2016 MediaTek Inc. * Author: PC Chen <pc.chen@mediatek.com> *         Tiffany Lin <tiffany.lin@mediatek.com> */ #include <media/v4l2-event.h>#include <media/v4l2-mem2mem.h>#include <media/videobuf2-dma-contig.h> #include "mtk_vcodec_dec_drv.h"#include "mtk_vcodec_dec.h"#include "vdec_drv_if.h"#include "mtk_vcodec_dec_pm.h" #define DFT_CFG_WIDTH	MTK_VDEC_MIN_W#define DFT_CFG_HEIGHT	MTK_VDEC_MIN_H static const struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f,		     const struct mtk_vcodec_dec_pdata *dec_pdata){	const struct mtk_video_fmt *fmt;	unsigned int k; 	for (k = 0; k < *dec_pdata->num_formats; k++) {		fmt = &dec_pdata->vdec_formats[k];		if (fmt->fourcc == f->fmt.pix_mp.pixelformat)			return fmt;	} 	return NULL;} static bool mtk_vdec_get_cap_fmt(struct mtk_vcodec_dec_ctx *ctx, int format_index){	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;	const struct mtk_video_fmt *fmt;	struct mtk_q_data *q_data;	int num_frame_count = 0, i;	bool ret = false; 	fmt = &dec_pdata->vdec_formats[format_index];	for (i = 0; i < *dec_pdata->num_formats; i++) {		if (dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME)			continue; 		num_frame_count++;	} 	if (num_frame_count == 1 || (!ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MM21))		return true; 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];	switch (q_data->fmt->fourcc) {	case V4L2_PIX_FMT_H264_SLICE:		if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110R)			ret = true;		break;	case V4L2_PIX_FMT_VP9_FRAME:	case V4L2_PIX_FMT_AV1_FRAME:	case V4L2_PIX_FMT_HEVC_SLICE:		if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110T)			ret = true;		break;	default:		break;	} 	return ret;} static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_dec_ctx *ctx,					      enum v4l2_buf_type type){	if (V4L2_TYPE_IS_OUTPUT(type))		return &ctx->q_data[MTK_Q_DATA_SRC]; 	return &ctx->q_data[MTK_Q_DATA_DST];} static int stateful_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	return v4l2_m2m_ioctl_try_decoder_cmd(file, priv, cmd);} static int stateful_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	struct vb2_queue *src_vq, *dst_vq;	int ret; 	ret = stateful_try_decoder_cmd(file, priv, cmd);	if (ret)		return ret; 	mtk_v4l2_vdec_dbg(1, ctx, "decoder cmd=%u", cmd->cmd);	dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,				V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);	switch (cmd->cmd) {	case V4L2_DEC_CMD_STOP:		src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,				V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);		if (!vb2_is_streaming(src_vq)) {			mtk_v4l2_vdec_dbg(1, ctx, "Output stream is off. No need to flush.");			return 0;		}		if (!vb2_is_streaming(dst_vq)) {			mtk_v4l2_vdec_dbg(1, ctx, "Capture stream is off. No need to flush.");			return 0;		}		v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);		v4l2_m2m_try_schedule(ctx->m2m_ctx);		break; 	case V4L2_DEC_CMD_START:		vb2_clear_last_buffer_dequeued(dst_vq);		break; 	default:		return -EINVAL;	} 	return 0;} static int stateless_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	return v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, cmd);} static int stateless_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	int ret; 	ret = v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, cmd);	if (ret)		return ret; 	mtk_v4l2_vdec_dbg(3, ctx, "decoder cmd=%u", cmd->cmd);	switch (cmd->cmd) {	case V4L2_DEC_CMD_FLUSH:		/*		 * If the flag of the output buffer is equals V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF,		 * this command will prevent dequeueing the capture buffer containing the last		 * decoded frame. Or do nothing		 */		break;	default:		mtk_v4l2_vdec_err(ctx, "invalid stateless decoder cmd=%u", cmd->cmd);		return -EINVAL;	} 	return 0;} static int vidioc_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 	if (ctx->dev->vdec_pdata->uses_stateless_api)		return stateless_try_decoder_cmd(file, priv, cmd); 	return stateful_try_decoder_cmd(file, priv, cmd);} static int vidioc_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 	if (ctx->dev->vdec_pdata->uses_stateless_api)		return stateless_decoder_cmd(file, priv, cmd); 	return stateful_decoder_cmd(file, priv, cmd);} void mtk_vdec_unlock(struct mtk_vcodec_dec_ctx *ctx){	mutex_unlock(&ctx->dev->dec_mutex[ctx->hw_id]);} void mtk_vdec_lock(struct mtk_vcodec_dec_ctx *ctx){	mutex_lock(&ctx->dev->dec_mutex[ctx->hw_id]);} void mtk_vcodec_dec_release(struct mtk_vcodec_dec_ctx *ctx){	vdec_if_deinit(ctx);	ctx->state = MTK_STATE_FREE;} void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_dec_ctx *ctx){	struct mtk_q_data *q_data; 	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;	ctx->fh.m2m_ctx = ctx->m2m_ctx;	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;	INIT_WORK(&ctx->decode_work, ctx->dev->vdec_pdata->worker);	ctx->colorspace = V4L2_COLORSPACE_REC709;	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; 	q_data = &ctx->q_data[MTK_Q_DATA_SRC];	memset(q_data, 0, sizeof(struct mtk_q_data));	q_data->visible_width = DFT_CFG_WIDTH;	q_data->visible_height = DFT_CFG_HEIGHT;	q_data->fmt = ctx->dev->vdec_pdata->default_out_fmt;	q_data->field = V4L2_FIELD_NONE; 	q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;	q_data->bytesperline[0] = 0; 	q_data = &ctx->q_data[MTK_Q_DATA_DST];	memset(q_data, 0, sizeof(struct mtk_q_data));	q_data->visible_width = DFT_CFG_WIDTH;	q_data->visible_height = DFT_CFG_HEIGHT;	q_data->coded_width = DFT_CFG_WIDTH;	q_data->coded_height = DFT_CFG_HEIGHT;	q_data->fmt = ctx->dev->vdec_pdata->default_cap_fmt;	q_data->field = V4L2_FIELD_NONE; 	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;	q_data->bytesperline[0] = q_data->coded_width;	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;	q_data->bytesperline[1] = q_data->coded_width;} static int vidioc_vdec_qbuf(struct file *file, void *priv,			    struct v4l2_buffer *buf){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 	if (ctx->state == MTK_STATE_ABORT) {		mtk_v4l2_vdec_err(ctx, "[%d] Call on QBUF after unrecoverable error", ctx->id);		return -EIO;	} 	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);} static int vidioc_vdec_dqbuf(struct file *file, void *priv,			     struct v4l2_buffer *buf){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 	if (ctx->state == MTK_STATE_ABORT) {		mtk_v4l2_vdec_err(ctx, "[%d] Call on DQBUF after unrecoverable error", ctx->id);		return -EIO;	} 	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);} static int vidioc_vdec_querycap(struct file *file, void *priv,				struct v4l2_capability *cap){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	struct device *dev = &ctx->dev->plat_dev->dev; 	strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));	snprintf(cap->card, sizeof(cap->card), "MT%d video decoder", ctx->dev->chip_name); 	return 0;} static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,				     const struct v4l2_event_subscription *sub){	struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(fh); 	if (ctx->dev->vdec_pdata->uses_stateless_api)		return v4l2_ctrl_subscribe_event(fh, sub); 	switch (sub->type) {	case V4L2_EVENT_EOS:		return v4l2_event_subscribe(fh, sub, 2, NULL);	case V4L2_EVENT_SOURCE_CHANGE:		return v4l2_src_change_event_subscribe(fh, sub);	default:		return v4l2_ctrl_subscribe_event(fh, sub);	}} static int vidioc_try_fmt(struct mtk_vcodec_dec_ctx *ctx, struct v4l2_format *f,			  const struct mtk_video_fmt *fmt){	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;	const struct v4l2_frmsize_stepwise *frmsize; 	pix_fmt_mp->field = V4L2_FIELD_NONE; 	/* Always apply frame size constraints from the coded side */	if (V4L2_TYPE_IS_OUTPUT(f->type))		frmsize = &fmt->frmsize;	else		frmsize = &ctx->q_data[MTK_Q_DATA_SRC].fmt->frmsize; 	pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width);	pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height); 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {		pix_fmt_mp->num_planes = 1;		pix_fmt_mp->plane_fmt[0].bytesperline = 0;	} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {		int tmp_w, tmp_h; 		/*		 * Find next closer width align 64, height align 64, size align		 * 64 rectangle		 * Note: This only get default value, the real HW needed value		 *       only available when ctx in MTK_STATE_HEADER state		 */		tmp_w = pix_fmt_mp->width;		tmp_h = pix_fmt_mp->height;		v4l_bound_align_image(&pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width, 6,				      &pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height, 6,				      9); 		if (pix_fmt_mp->width < tmp_w &&		    (pix_fmt_mp->width + 64) <= frmsize->max_width)			pix_fmt_mp->width += 64;		if (pix_fmt_mp->height < tmp_h &&		    (pix_fmt_mp->height + 64) <= frmsize->max_height)			pix_fmt_mp->height += 64; 		mtk_v4l2_vdec_dbg(0, ctx,				  "before resize wxh=%dx%d, after resize wxh=%dx%d, sizeimage=%d",				  tmp_w, tmp_h, pix_fmt_mp->width, pix_fmt_mp->height,				  pix_fmt_mp->width * pix_fmt_mp->height); 		pix_fmt_mp->num_planes = fmt->num_planes;		pix_fmt_mp->plane_fmt[0].sizeimage =				pix_fmt_mp->width * pix_fmt_mp->height;		pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width; 		if (pix_fmt_mp->num_planes == 2) {			pix_fmt_mp->plane_fmt[1].sizeimage =				(pix_fmt_mp->width * pix_fmt_mp->height) / 2;			pix_fmt_mp->plane_fmt[1].bytesperline =				pix_fmt_mp->width;		}	} 	pix_fmt_mp->flags = 0;	return 0;} static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,				struct v4l2_format *f){	const struct mtk_video_fmt *fmt;	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 	fmt = mtk_vdec_find_format(f, dec_pdata);	if (!fmt) {		f->fmt.pix.pixelformat =			ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;		fmt = mtk_vdec_find_format(f, dec_pdata);	} 	return vidioc_try_fmt(ctx, f, fmt);} static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,				struct v4l2_format *f){	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;	const struct mtk_video_fmt *fmt;	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 	fmt = mtk_vdec_find_format(f, dec_pdata);	if (!fmt) {		f->fmt.pix.pixelformat =			ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;		fmt = mtk_vdec_find_format(f, dec_pdata);	} 	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {		mtk_v4l2_vdec_err(ctx, "sizeimage of output format must be given");		return -EINVAL;	} 	return vidioc_try_fmt(ctx, f, fmt);} static int vidioc_vdec_g_selection(struct file *file, void *priv,			struct v4l2_selection *s){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	struct mtk_q_data *q_data; 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)		return -EINVAL; 	q_data = &ctx->q_data[MTK_Q_DATA_DST]; 	switch (s->target) {	case V4L2_SEL_TGT_COMPOSE_DEFAULT:		s->r.left = 0;		s->r.top = 0;		s->r.width = ctx->picinfo.pic_w;		s->r.height = ctx->picinfo.pic_h;		break;	case V4L2_SEL_TGT_COMPOSE_BOUNDS:		s->r.left = 0;		s->r.top = 0;		s->r.width = ctx->picinfo.buf_w;		s->r.height = ctx->picinfo.buf_h;		break;	case V4L2_SEL_TGT_COMPOSE:		if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {			/* set to default value if header info not ready yet*/			s->r.left = 0;			s->r.top = 0;			s->r.width = q_data->visible_width;			s->r.height = q_data->visible_height;		}		break;	default:		return -EINVAL;	} 	if (ctx->state < MTK_STATE_HEADER) {		/* set to default value if header info not ready yet*/		s->r.left = 0;		s->r.top = 0;		s->r.width = q_data->visible_width;		s->r.height = q_data->visible_height;		return 0;	} 	return 0;} static int vidioc_vdec_s_selection(struct file *file, void *priv,				struct v4l2_selection *s){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)		return -EINVAL; 	switch (s->target) {	case V4L2_SEL_TGT_COMPOSE:		s->r.left = 0;		s->r.top = 0;		s->r.width = ctx->picinfo.pic_w;		s->r.height = ctx->picinfo.pic_h;		break;	default:		return -EINVAL;	} 	return 0;} static int vidioc_vdec_s_fmt(struct file *file, void *priv,			     struct v4l2_format *f){	struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);	struct v4l2_pix_format_mplane *pix_mp;	struct mtk_q_data *q_data;	int ret = 0;	const struct mtk_video_fmt *fmt;	const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 	mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id); 	q_data = mtk_vdec_get_q_data(ctx, f->type);	if (!q_data)		return -EINVAL; 	pix_mp = &f->fmt.pix_mp;	/*	 * Setting OUTPUT format after OUTPUT buffers are allocated is invalid	 * if using the stateful API.	 */	if (!dec_pdata->uses_stateless_api &&	    f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {		mtk_v4l2_vdec_err(ctx, "out_q_ctx buffers already requested");		ret = -EBUSY;	} 	/*	 * Setting CAPTURE format after CAPTURE buffers are allocated is	 * invalid.	 */	if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {		mtk_v4l2_vdec_err(ctx, "cap_q_ctx buffers already requested");		ret = -EBUSY;	}

Lines 1–500 of 1041.