Commit c022a4a9 authored by Dafna Hirschfeld's avatar Dafna Hirschfeld Committed by Mauro Carvalho Chehab

media: vicodec: add struct for encoder/decoder instance

Add struct 'vicodec_dev_instance' for the fields in vicodec_dev
that have have both decoder and encoder versions.
Signed-off-by: default avatarDafna Hirschfeld <dafna3@gmail.com>
Signed-off-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+samsung@kernel.org>
parent 74799372
...@@ -89,21 +89,21 @@ enum { ...@@ -89,21 +89,21 @@ enum {
V4L2_M2M_DST = 1, V4L2_M2M_DST = 1,
}; };
struct vicodec_dev_instance {
struct video_device vfd;
struct mutex mutex;
spinlock_t lock;
struct v4l2_m2m_dev *m2m_dev;
};
struct vicodec_dev { struct vicodec_dev {
struct v4l2_device v4l2_dev; struct v4l2_device v4l2_dev;
struct video_device enc_vfd; struct vicodec_dev_instance stateful_enc;
struct video_device dec_vfd; struct vicodec_dev_instance stateful_dec;
#ifdef CONFIG_MEDIA_CONTROLLER #ifdef CONFIG_MEDIA_CONTROLLER
struct media_device mdev; struct media_device mdev;
#endif #endif
struct mutex enc_mutex;
struct mutex dec_mutex;
spinlock_t enc_lock;
spinlock_t dec_lock;
struct v4l2_m2m_dev *enc_dev;
struct v4l2_m2m_dev *dec_dev;
}; };
struct vicodec_ctx { struct vicodec_ctx {
...@@ -367,9 +367,9 @@ static void device_run(void *priv) ...@@ -367,9 +367,9 @@ static void device_run(void *priv)
spin_unlock(ctx->lock); spin_unlock(ctx->lock);
if (ctx->is_enc) if (ctx->is_enc)
v4l2_m2m_job_finish(dev->enc_dev, ctx->fh.m2m_ctx); v4l2_m2m_job_finish(dev->stateful_enc.m2m_dev, ctx->fh.m2m_ctx);
else else
v4l2_m2m_job_finish(dev->dec_dev, ctx->fh.m2m_ctx); v4l2_m2m_job_finish(dev->stateful_dec.m2m_dev, ctx->fh.m2m_ctx);
} }
static void job_remove_src_buf(struct vicodec_ctx *ctx, u32 state) static void job_remove_src_buf(struct vicodec_ctx *ctx, u32 state)
...@@ -1504,9 +1504,8 @@ static int queue_init(void *priv, struct vb2_queue *src_vq, ...@@ -1504,9 +1504,8 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
src_vq->ops = &vicodec_qops; src_vq->ops = &vicodec_qops;
src_vq->mem_ops = &vb2_vmalloc_memops; src_vq->mem_ops = &vb2_vmalloc_memops;
src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
src_vq->lock = ctx->is_enc ? &ctx->dev->enc_mutex : src_vq->lock = ctx->is_enc ? &ctx->dev->stateful_enc.mutex :
&ctx->dev->dec_mutex; &ctx->dev->stateful_dec.mutex;
ret = vb2_queue_init(src_vq); ret = vb2_queue_init(src_vq);
if (ret) if (ret)
return ret; return ret;
...@@ -1594,7 +1593,7 @@ static int vicodec_open(struct file *file) ...@@ -1594,7 +1593,7 @@ static int vicodec_open(struct file *file)
goto open_unlock; goto open_unlock;
} }
if (vfd == &dev->enc_vfd) if (vfd == &dev->stateful_enc.vfd)
ctx->is_enc = true; ctx->is_enc = true;
v4l2_fh_init(&ctx->fh, video_devdata(file)); v4l2_fh_init(&ctx->fh, video_devdata(file));
...@@ -1642,13 +1641,13 @@ static int vicodec_open(struct file *file) ...@@ -1642,13 +1641,13 @@ static int vicodec_open(struct file *file)
ctx->state.colorspace = V4L2_COLORSPACE_REC709; ctx->state.colorspace = V4L2_COLORSPACE_REC709;
if (ctx->is_enc) { if (ctx->is_enc) {
ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->enc_dev, ctx, ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_enc.m2m_dev,
&queue_init); ctx, &queue_init);
ctx->lock = &dev->enc_lock; ctx->lock = &dev->stateful_enc.lock;
} else { } else {
ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->dec_dev, ctx, ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_dec.m2m_dev,
&queue_init); ctx, &queue_init);
ctx->lock = &dev->dec_lock; ctx->lock = &dev->stateful_dec.lock;
} }
if (IS_ERR(ctx->fh.m2m_ctx)) { if (IS_ERR(ctx->fh.m2m_ctx)) {
...@@ -1707,19 +1706,57 @@ static const struct v4l2_m2m_ops m2m_ops = { ...@@ -1707,19 +1706,57 @@ static const struct v4l2_m2m_ops m2m_ops = {
.job_ready = job_ready, .job_ready = job_ready,
}; };
static int register_instance(struct vicodec_dev *dev,
struct vicodec_dev_instance *dev_instance,
const char *name, bool is_enc)
{
struct video_device *vfd;
int ret;
spin_lock_init(&dev_instance->lock);
mutex_init(&dev_instance->mutex);
dev_instance->m2m_dev = v4l2_m2m_init(&m2m_ops);
if (IS_ERR(dev_instance->m2m_dev)) {
v4l2_err(&dev->v4l2_dev, "Failed to init vicodec enc device\n");
return PTR_ERR(dev_instance->m2m_dev);
}
dev_instance->vfd = vicodec_videodev;
vfd = &dev_instance->vfd;
vfd->lock = &dev_instance->mutex;
vfd->v4l2_dev = &dev->v4l2_dev;
strscpy(vfd->name, name, sizeof(vfd->name));
vfd->device_caps = V4L2_CAP_STREAMING |
(multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
if (is_enc) {
v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
} else {
v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
}
video_set_drvdata(vfd, dev);
ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register video device '%s'\n", name);
v4l2_m2m_release(dev_instance->m2m_dev);
return ret;
}
v4l2_info(&dev->v4l2_dev, "Device '%s' registered as /dev/video%d\n",
name, vfd->num);
return 0;
}
static int vicodec_probe(struct platform_device *pdev) static int vicodec_probe(struct platform_device *pdev)
{ {
struct vicodec_dev *dev; struct vicodec_dev *dev;
struct video_device *vfd;
int ret; int ret;
dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
if (!dev) if (!dev)
return -ENOMEM; return -ENOMEM;
spin_lock_init(&dev->enc_lock);
spin_lock_init(&dev->dec_lock);
ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
if (ret) if (ret)
return ret; return ret;
...@@ -1733,100 +1770,53 @@ static int vicodec_probe(struct platform_device *pdev) ...@@ -1733,100 +1770,53 @@ static int vicodec_probe(struct platform_device *pdev)
dev->v4l2_dev.mdev = &dev->mdev; dev->v4l2_dev.mdev = &dev->mdev;
#endif #endif
mutex_init(&dev->enc_mutex);
mutex_init(&dev->dec_mutex);
platform_set_drvdata(pdev, dev); platform_set_drvdata(pdev, dev);
dev->enc_dev = v4l2_m2m_init(&m2m_ops); if (register_instance(dev, &dev->stateful_enc,
if (IS_ERR(dev->enc_dev)) { "stateful-encoder", true))
v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
ret = PTR_ERR(dev->enc_dev);
goto unreg_dev; goto unreg_dev;
}
dev->dec_dev = v4l2_m2m_init(&m2m_ops);
if (IS_ERR(dev->dec_dev)) {
v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
ret = PTR_ERR(dev->dec_dev);
goto err_enc_m2m;
}
dev->enc_vfd = vicodec_videodev;
vfd = &dev->enc_vfd;
vfd->lock = &dev->enc_mutex;
vfd->v4l2_dev = &dev->v4l2_dev;
strscpy(vfd->name, "vicodec-enc", sizeof(vfd->name));
vfd->device_caps = V4L2_CAP_STREAMING |
(multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
video_set_drvdata(vfd, dev);
ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); if (register_instance(dev, &dev->stateful_dec,
if (ret) { "stateful-decoder", false))
v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); goto unreg_sf_enc;
goto err_dec_m2m;
}
v4l2_info(&dev->v4l2_dev,
"Device registered as /dev/video%d\n", vfd->num);
dev->dec_vfd = vicodec_videodev;
vfd = &dev->dec_vfd;
vfd->lock = &dev->dec_mutex;
vfd->v4l2_dev = &dev->v4l2_dev;
vfd->device_caps = V4L2_CAP_STREAMING |
(multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
strscpy(vfd->name, "vicodec-dec", sizeof(vfd->name));
v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
video_set_drvdata(vfd, dev);
ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
goto unreg_enc;
}
v4l2_info(&dev->v4l2_dev,
"Device registered as /dev/video%d\n", vfd->num);
#ifdef CONFIG_MEDIA_CONTROLLER #ifdef CONFIG_MEDIA_CONTROLLER
ret = v4l2_m2m_register_media_controller(dev->enc_dev, ret = v4l2_m2m_register_media_controller(dev->stateful_enc.m2m_dev,
&dev->enc_vfd, MEDIA_ENT_F_PROC_VIDEO_ENCODER); &dev->stateful_enc.vfd,
MEDIA_ENT_F_PROC_VIDEO_ENCODER);
if (ret) { if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for enc\n");
goto unreg_m2m; goto unreg_m2m;
} }
ret = v4l2_m2m_register_media_controller(dev->dec_dev, ret = v4l2_m2m_register_media_controller(dev->stateful_dec.m2m_dev,
&dev->dec_vfd, MEDIA_ENT_F_PROC_VIDEO_DECODER); &dev->stateful_dec.vfd,
MEDIA_ENT_F_PROC_VIDEO_DECODER);
if (ret) { if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for dec\n");
goto unreg_m2m_enc_mc; goto unreg_m2m_sf_enc_mc;
} }
ret = media_device_register(&dev->mdev); ret = media_device_register(&dev->mdev);
if (ret) { if (ret) {
v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
goto unreg_m2m_dec_mc; goto unreg_m2m_sf_dec_mc;
} }
#endif #endif
return 0; return 0;
#ifdef CONFIG_MEDIA_CONTROLLER #ifdef CONFIG_MEDIA_CONTROLLER
unreg_m2m_dec_mc: unreg_m2m_sf_dec_mc:
v4l2_m2m_unregister_media_controller(dev->dec_dev); v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
unreg_m2m_enc_mc: unreg_m2m_sf_enc_mc:
v4l2_m2m_unregister_media_controller(dev->enc_dev); v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
unreg_m2m: unreg_m2m:
video_unregister_device(&dev->dec_vfd); video_unregister_device(&dev->stateful_dec.vfd);
v4l2_m2m_release(dev->stateful_dec.m2m_dev);
#endif #endif
unreg_enc: unreg_sf_enc:
video_unregister_device(&dev->enc_vfd); video_unregister_device(&dev->stateful_enc.vfd);
err_dec_m2m: v4l2_m2m_release(dev->stateful_enc.m2m_dev);
v4l2_m2m_release(dev->dec_dev);
err_enc_m2m:
v4l2_m2m_release(dev->enc_dev);
unreg_dev: unreg_dev:
v4l2_device_unregister(&dev->v4l2_dev); v4l2_device_unregister(&dev->v4l2_dev);
...@@ -1841,15 +1831,15 @@ static int vicodec_remove(struct platform_device *pdev) ...@@ -1841,15 +1831,15 @@ static int vicodec_remove(struct platform_device *pdev)
#ifdef CONFIG_MEDIA_CONTROLLER #ifdef CONFIG_MEDIA_CONTROLLER
media_device_unregister(&dev->mdev); media_device_unregister(&dev->mdev);
v4l2_m2m_unregister_media_controller(dev->enc_dev); v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
v4l2_m2m_unregister_media_controller(dev->dec_dev); v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
media_device_cleanup(&dev->mdev); media_device_cleanup(&dev->mdev);
#endif #endif
v4l2_m2m_release(dev->enc_dev); v4l2_m2m_release(dev->stateful_enc.m2m_dev);
v4l2_m2m_release(dev->dec_dev); v4l2_m2m_release(dev->stateful_dec.m2m_dev);
video_unregister_device(&dev->enc_vfd); video_unregister_device(&dev->stateful_enc.vfd);
video_unregister_device(&dev->dec_vfd); video_unregister_device(&dev->stateful_dec.vfd);
v4l2_device_unregister(&dev->v4l2_dev); v4l2_device_unregister(&dev->v4l2_dev);
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment