media: mtk-vcodec: Support MT8192 H264 4K encoding

MT8192 H264 support 4k(3840x2176) and Level 5.1 encoding,
add related path according to enc_capability.

Signed-off-by: Irui Wang <irui.wang@mediatek.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
This commit is contained in:
Irui Wang 2021-06-05 04:29:18 +02:00 committed by Mauro Carvalho Chehab
parent 37eeacba7c
commit caf231ac25
2 changed files with 57 additions and 25 deletions

View file

@ -19,23 +19,30 @@
#define MTK_VENC_MIN_W 160U #define MTK_VENC_MIN_W 160U
#define MTK_VENC_MIN_H 128U #define MTK_VENC_MIN_H 128U
#define MTK_VENC_MAX_W 1920U #define MTK_VENC_HD_MAX_W 1920U
#define MTK_VENC_MAX_H 1088U #define MTK_VENC_HD_MAX_H 1088U
#define MTK_VENC_4K_MAX_W 3840U
#define MTK_VENC_4K_MAX_H 2176U
#define DFT_CFG_WIDTH MTK_VENC_MIN_W #define DFT_CFG_WIDTH MTK_VENC_MIN_W
#define DFT_CFG_HEIGHT MTK_VENC_MIN_H #define DFT_CFG_HEIGHT MTK_VENC_MIN_H
#define MTK_MAX_CTRLS_HINT 20 #define MTK_MAX_CTRLS_HINT 20
#define MTK_DEFAULT_FRAMERATE_NUM 1001 #define MTK_DEFAULT_FRAMERATE_NUM 1001
#define MTK_DEFAULT_FRAMERATE_DENOM 30000 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
#define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
static void mtk_venc_worker(struct work_struct *work); static void mtk_venc_worker(struct work_struct *work);
static const struct v4l2_frmsize_stepwise mtk_venc_framesizes = { static const struct v4l2_frmsize_stepwise mtk_venc_hd_framesizes = {
MTK_VENC_MIN_W, MTK_VENC_MAX_W, 16, MTK_VENC_MIN_W, MTK_VENC_HD_MAX_W, 16,
MTK_VENC_MIN_H, MTK_VENC_MAX_H, 16, MTK_VENC_MIN_H, MTK_VENC_HD_MAX_H, 16,
}; };
#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_venc_framesizes) static const struct v4l2_frmsize_stepwise mtk_venc_4k_framesizes = {
MTK_VENC_MIN_W, MTK_VENC_4K_MAX_W, 16,
MTK_VENC_MIN_H, MTK_VENC_4K_MAX_H, 16,
};
static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl) static int vidioc_venc_s_ctrl(struct v4l2_ctrl *ctrl)
{ {
@ -151,17 +158,22 @@ static int vidioc_enum_framesizes(struct file *file, void *fh,
struct v4l2_frmsizeenum *fsize) struct v4l2_frmsizeenum *fsize)
{ {
const struct mtk_video_fmt *fmt; const struct mtk_video_fmt *fmt;
struct mtk_vcodec_ctx *ctx = fh_to_ctx(fh);
if (fsize->index != 0) if (fsize->index != 0)
return -EINVAL; return -EINVAL;
fmt = mtk_venc_find_format(fsize->pixel_format, fmt = mtk_venc_find_format(fsize->pixel_format,
fh_to_ctx(fh)->dev->venc_pdata); ctx->dev->venc_pdata);
if (!fmt) if (!fmt)
return -EINVAL; return -EINVAL;
fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
fsize->stepwise = mtk_venc_framesizes;
if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
fsize->stepwise = mtk_venc_4k_framesizes;
else
fsize->stepwise = mtk_venc_hd_framesizes;
return 0; return 0;
} }
@ -248,7 +260,7 @@ static struct mtk_q_data *mtk_venc_get_q_data(struct mtk_vcodec_ctx *ctx,
/* V4L2 specification suggests the driver corrects the format struct if any of /* V4L2 specification suggests the driver corrects the format struct if any of
* the dimensions is unsupported * the dimensions is unsupported
*/ */
static int vidioc_try_fmt(struct v4l2_format *f, static int vidioc_try_fmt(struct mtk_vcodec_ctx *ctx, struct v4l2_format *f,
const struct mtk_video_fmt *fmt) const struct mtk_video_fmt *fmt)
{ {
struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
@ -260,13 +272,22 @@ static int vidioc_try_fmt(struct v4l2_format *f,
pix_fmt_mp->plane_fmt[0].bytesperline = 0; pix_fmt_mp->plane_fmt[0].bytesperline = 0;
} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
int tmp_w, tmp_h; int tmp_w, tmp_h;
unsigned int max_width, max_height;
if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE) {
max_width = MTK_VENC_4K_MAX_W;
max_height = MTK_VENC_4K_MAX_H;
} else {
max_width = MTK_VENC_HD_MAX_W;
max_height = MTK_VENC_HD_MAX_H;
}
pix_fmt_mp->height = clamp(pix_fmt_mp->height, pix_fmt_mp->height = clamp(pix_fmt_mp->height,
MTK_VENC_MIN_H, MTK_VENC_MIN_H,
MTK_VENC_MAX_H); max_height);
pix_fmt_mp->width = clamp(pix_fmt_mp->width, pix_fmt_mp->width = clamp(pix_fmt_mp->width,
MTK_VENC_MIN_W, MTK_VENC_MIN_W,
MTK_VENC_MAX_W); max_width);
/* find next closer width align 16, heign align 32, size align /* find next closer width align 16, heign align 32, size align
* 64 rectangle * 64 rectangle
@ -275,16 +296,16 @@ static int vidioc_try_fmt(struct v4l2_format *f,
tmp_h = pix_fmt_mp->height; tmp_h = pix_fmt_mp->height;
v4l_bound_align_image(&pix_fmt_mp->width, v4l_bound_align_image(&pix_fmt_mp->width,
MTK_VENC_MIN_W, MTK_VENC_MIN_W,
MTK_VENC_MAX_W, 4, max_width, 4,
&pix_fmt_mp->height, &pix_fmt_mp->height,
MTK_VENC_MIN_H, MTK_VENC_MIN_H,
MTK_VENC_MAX_H, 5, 6); max_height, 5, 6);
if (pix_fmt_mp->width < tmp_w && if (pix_fmt_mp->width < tmp_w &&
(pix_fmt_mp->width + 16) <= MTK_VENC_MAX_W) (pix_fmt_mp->width + 16) <= max_width)
pix_fmt_mp->width += 16; pix_fmt_mp->width += 16;
if (pix_fmt_mp->height < tmp_h && if (pix_fmt_mp->height < tmp_h &&
(pix_fmt_mp->height + 32) <= MTK_VENC_MAX_H) (pix_fmt_mp->height + 32) <= max_height)
pix_fmt_mp->height += 32; pix_fmt_mp->height += 32;
mtk_v4l2_debug(0, mtk_v4l2_debug(0,
@ -405,7 +426,7 @@ static int vidioc_venc_s_fmt_cap(struct file *file, void *priv,
} }
q_data->fmt = fmt; q_data->fmt = fmt;
ret = vidioc_try_fmt(f, q_data->fmt); ret = vidioc_try_fmt(ctx, f, q_data->fmt);
if (ret) if (ret)
return ret; return ret;
@ -467,7 +488,7 @@ static int vidioc_venc_s_fmt_out(struct file *file, void *priv,
f->fmt.pix.pixelformat = fmt->fourcc; f->fmt.pix.pixelformat = fmt->fourcc;
} }
ret = vidioc_try_fmt(f, fmt); ret = vidioc_try_fmt(ctx, f, fmt);
if (ret) if (ret)
return ret; return ret;
@ -545,7 +566,7 @@ static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
f->fmt.pix_mp.quantization = ctx->quantization; f->fmt.pix_mp.quantization = ctx->quantization;
f->fmt.pix_mp.xfer_func = ctx->xfer_func; f->fmt.pix_mp.xfer_func = ctx->xfer_func;
return vidioc_try_fmt(f, fmt); return vidioc_try_fmt(ctx, f, fmt);
} }
static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
@ -567,7 +588,7 @@ static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT; f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
} }
return vidioc_try_fmt(f, fmt); return vidioc_try_fmt(ctx, f, fmt);
} }
static int vidioc_venc_g_selection(struct file *file, void *priv, static int vidioc_venc_g_selection(struct file *file, void *priv,
@ -1171,16 +1192,16 @@ void mtk_vcodec_enc_set_default_params(struct mtk_vcodec_ctx *ctx)
v4l_bound_align_image(&q_data->coded_width, v4l_bound_align_image(&q_data->coded_width,
MTK_VENC_MIN_W, MTK_VENC_MIN_W,
MTK_VENC_MAX_W, 4, MTK_VENC_HD_MAX_W, 4,
&q_data->coded_height, &q_data->coded_height,
MTK_VENC_MIN_H, MTK_VENC_MIN_H,
MTK_VENC_MAX_H, 5, 6); MTK_VENC_HD_MAX_H, 5, 6);
if (q_data->coded_width < DFT_CFG_WIDTH && if (q_data->coded_width < DFT_CFG_WIDTH &&
(q_data->coded_width + 16) <= MTK_VENC_MAX_W) (q_data->coded_width + 16) <= MTK_VENC_HD_MAX_W)
q_data->coded_width += 16; q_data->coded_width += 16;
if (q_data->coded_height < DFT_CFG_HEIGHT && if (q_data->coded_height < DFT_CFG_HEIGHT &&
(q_data->coded_height + 32) <= MTK_VENC_MAX_H) (q_data->coded_height + 32) <= MTK_VENC_HD_MAX_H)
q_data->coded_height += 32; q_data->coded_height += 32;
q_data->sizeimage[0] = q_data->sizeimage[0] =
@ -1210,6 +1231,12 @@ int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
{ {
const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops; const struct v4l2_ctrl_ops *ops = &mtk_vcodec_enc_ctrl_ops;
struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl; struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl;
u8 h264_max_level;
if (ctx->dev->enc_capability & MTK_VENC_4K_CAPABILITY_ENABLE)
h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_5_1;
else
h264_max_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_2;
v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT); v4l2_ctrl_handler_init(handler, MTK_MAX_CTRLS_HINT);
@ -1240,8 +1267,9 @@ int mtk_vcodec_enc_ctrls_setup(struct mtk_vcodec_ctx *ctx)
V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); 0, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH);
v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, v4l2_ctrl_new_std_menu(handler, ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL,
V4L2_MPEG_VIDEO_H264_LEVEL_4_2, h264_max_level,
0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0); 0, V4L2_MPEG_VIDEO_H264_LEVEL_4_0);
if (handler->error) { if (handler->error) {
mtk_v4l2_err("Init control handler fail %d", mtk_v4l2_err("Init control handler fail %d",
handler->error); handler->error);

View file

@ -215,6 +215,10 @@ static unsigned int h264_get_level(struct venc_h264_inst *inst,
return 41; return 41;
case V4L2_MPEG_VIDEO_H264_LEVEL_4_2: case V4L2_MPEG_VIDEO_H264_LEVEL_4_2:
return 42; return 42;
case V4L2_MPEG_VIDEO_H264_LEVEL_5_0:
return 50;
case V4L2_MPEG_VIDEO_H264_LEVEL_5_1:
return 51;
default: default:
mtk_vcodec_debug(inst, "unsupported level %d", level); mtk_vcodec_debug(inst, "unsupported level %d", level);
return 31; return 31;