summaryrefslogtreecommitdiff
path: root/drivers/media/platform/amphion/vpu_v4l2.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/media/platform/amphion/vpu_v4l2.c')
-rw-r--r--drivers/media/platform/amphion/vpu_v4l2.c199
1 files changed, 142 insertions, 57 deletions
diff --git a/drivers/media/platform/amphion/vpu_v4l2.c b/drivers/media/platform/amphion/vpu_v4l2.c
index b779e0ba916c..6773b885597c 100644
--- a/drivers/media/platform/amphion/vpu_v4l2.c
+++ b/drivers/media/platform/amphion/vpu_v4l2.c
@@ -65,18 +65,11 @@ unsigned int vpu_get_buffer_state(struct vb2_v4l2_buffer *vbuf)
void vpu_v4l2_set_error(struct vpu_inst *inst)
{
- struct vb2_queue *src_q;
- struct vb2_queue *dst_q;
-
vpu_inst_lock(inst);
dev_err(inst->dev, "some error occurs in codec\n");
if (inst->fh.m2m_ctx) {
- src_q = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
- dst_q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
- src_q->error = 1;
- dst_q->error = 1;
- wake_up(&src_q->done_wq);
- wake_up(&dst_q->done_wq);
+ vb2_queue_error(v4l2_m2m_get_src_vq(inst->fh.m2m_ctx));
+ vb2_queue_error(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx));
}
vpu_inst_unlock(inst);
}
@@ -140,51 +133,136 @@ bool vpu_is_source_empty(struct vpu_inst *inst)
return true;
}
-const struct vpu_format *vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
+static int vpu_init_format(struct vpu_inst *inst, struct vpu_format *fmt)
+{
+ const struct vpu_format *info;
+
+ info = vpu_helper_find_format(inst, fmt->type, fmt->pixfmt);
+ if (!info) {
+ info = vpu_helper_enum_format(inst, fmt->type, 0);
+ if (!info)
+ return -EINVAL;
+ }
+ memcpy(fmt, info, sizeof(*fmt));
+
+ return 0;
+}
+
+static int vpu_calc_fmt_bytesperline(struct v4l2_format *f, struct vpu_format *fmt)
{
struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
- u32 type = f->type;
+ int i;
+
+ if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+ for (i = 0; i < fmt->comp_planes; i++)
+ fmt->bytesperline[i] = 0;
+ return 0;
+ }
+ if (pixmp->num_planes == fmt->comp_planes) {
+ for (i = 0; i < fmt->comp_planes; i++)
+ fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline;
+ return 0;
+ }
+ if (pixmp->num_planes > 1)
+ return -EINVAL;
+
+ /*amphion vpu only support nv12 and nv12 tiled,
+ * so the bytesperline of luma and chroma should be same
+ */
+ for (i = 0; i < fmt->comp_planes; i++)
+ fmt->bytesperline[i] = pixmp->plane_fmt[0].bytesperline;
+
+ return 0;
+}
+
+static int vpu_calc_fmt_sizeimage(struct vpu_inst *inst, struct vpu_format *fmt)
+{
u32 stride = 1;
- u32 bytesperline;
- u32 sizeimage;
- const struct vpu_format *fmt;
- const struct vpu_core_resources *res;
int i;
- fmt = vpu_helper_find_format(inst, type, pixmp->pixelformat);
- if (!fmt) {
- fmt = vpu_helper_enum_format(inst, type, 0);
- if (!fmt)
- return NULL;
- pixmp->pixelformat = fmt->pixfmt;
+ if (!(fmt->flags & V4L2_FMT_FLAG_COMPRESSED)) {
+ const struct vpu_core_resources *res = vpu_get_resource(inst);
+
+ if (res)
+ stride = res->stride;
}
- res = vpu_get_resource(inst);
- if (res)
- stride = res->stride;
- if (pixmp->width)
- pixmp->width = vpu_helper_valid_frame_width(inst, pixmp->width);
- if (pixmp->height)
- pixmp->height = vpu_helper_valid_frame_height(inst, pixmp->height);
+ for (i = 0; i < fmt->comp_planes; i++) {
+ fmt->sizeimage[i] = vpu_helper_get_plane_size(fmt->pixfmt,
+ fmt->width,
+ fmt->height,
+ i,
+ stride,
+ fmt->field != V4L2_FIELD_NONE ? 1 : 0,
+ &fmt->bytesperline[i]);
+ fmt->sizeimage[i] = max_t(u32, fmt->sizeimage[i], PAGE_SIZE);
+ if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+ fmt->sizeimage[i] = clamp_val(fmt->sizeimage[i], SZ_128K, SZ_8M);
+ fmt->bytesperline[i] = 0;
+ }
+ }
+
+ return 0;
+}
+
+u32 vpu_get_fmt_plane_size(struct vpu_format *fmt, u32 plane_no)
+{
+ u32 size;
+ int i;
+
+ if (plane_no >= fmt->mem_planes)
+ return 0;
+
+ if (fmt->comp_planes == fmt->mem_planes)
+ return fmt->sizeimage[plane_no];
+ if (plane_no < fmt->mem_planes - 1)
+ return fmt->sizeimage[plane_no];
+
+ size = fmt->sizeimage[plane_no];
+ for (i = fmt->mem_planes; i < fmt->comp_planes; i++)
+ size += fmt->sizeimage[i];
+
+ return size;
+}
+
+int vpu_try_fmt_common(struct vpu_inst *inst, struct v4l2_format *f, struct vpu_format *fmt)
+{
+ struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
+ int i;
+ int ret;
+
+ fmt->pixfmt = pixmp->pixelformat;
+ fmt->type = f->type;
+ ret = vpu_init_format(inst, fmt);
+ if (ret < 0)
+ return ret;
+
+ fmt->width = pixmp->width;
+ fmt->height = pixmp->height;
+ if (fmt->width)
+ fmt->width = vpu_helper_valid_frame_width(inst, fmt->width);
+ if (fmt->height)
+ fmt->height = vpu_helper_valid_frame_height(inst, fmt->height);
+ fmt->field = pixmp->field == V4L2_FIELD_ANY ? V4L2_FIELD_NONE : pixmp->field;
+ vpu_calc_fmt_bytesperline(f, fmt);
+ vpu_calc_fmt_sizeimage(inst, fmt);
+ if ((fmt->flags & V4L2_FMT_FLAG_COMPRESSED) && pixmp->plane_fmt[0].sizeimage)
+ fmt->sizeimage[0] = clamp_val(pixmp->plane_fmt[0].sizeimage, SZ_128K, SZ_8M);
+
+ pixmp->pixelformat = fmt->pixfmt;
+ pixmp->width = fmt->width;
+ pixmp->height = fmt->height;
pixmp->flags = fmt->flags;
- pixmp->num_planes = fmt->num_planes;
- if (pixmp->field == V4L2_FIELD_ANY)
- pixmp->field = V4L2_FIELD_NONE;
+ pixmp->num_planes = fmt->mem_planes;
+ pixmp->field = fmt->field;
+ memset(pixmp->reserved, 0, sizeof(pixmp->reserved));
for (i = 0; i < pixmp->num_planes; i++) {
- bytesperline = max_t(s32, pixmp->plane_fmt[i].bytesperline, 0);
- sizeimage = vpu_helper_get_plane_size(pixmp->pixelformat,
- pixmp->width,
- pixmp->height,
- i,
- stride,
- pixmp->field > V4L2_FIELD_NONE ? 1 : 0,
- &bytesperline);
- sizeimage = max_t(s32, pixmp->plane_fmt[i].sizeimage, sizeimage);
- pixmp->plane_fmt[i].bytesperline = bytesperline;
- pixmp->plane_fmt[i].sizeimage = sizeimage;
+ pixmp->plane_fmt[i].bytesperline = fmt->bytesperline[i];
+ pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(fmt, i);
+ memset(pixmp->plane_fmt[i].reserved, 0, sizeof(pixmp->plane_fmt[i].reserved));
}
- return fmt;
+ return 0;
}
static bool vpu_check_ready(struct vpu_inst *inst, u32 type)
@@ -249,8 +327,12 @@ int vpu_process_capture_buffer(struct vpu_inst *inst)
struct vb2_v4l2_buffer *vpu_next_src_buf(struct vpu_inst *inst)
{
- struct vb2_v4l2_buffer *src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
+ struct vb2_v4l2_buffer *src_buf = NULL;
+
+ if (!inst->fh.m2m_ctx)
+ return NULL;
+ src_buf = v4l2_m2m_next_src_buf(inst->fh.m2m_ctx);
if (!src_buf || vpu_get_buffer_state(src_buf) == VPU_BUF_STATE_IDLE)
return NULL;
@@ -273,7 +355,7 @@ void vpu_skip_frame(struct vpu_inst *inst, int count)
enum vb2_buffer_state state;
int i = 0;
- if (count <= 0)
+ if (count <= 0 || !inst->fh.m2m_ctx)
return;
while (i < count) {
@@ -389,10 +471,10 @@ static int vpu_vb2_queue_setup(struct vb2_queue *vq,
cur_fmt = vpu_get_format(inst, vq->type);
if (*plane_count) {
- if (*plane_count != cur_fmt->num_planes)
+ if (*plane_count != cur_fmt->mem_planes)
return -EINVAL;
- for (i = 0; i < cur_fmt->num_planes; i++) {
- if (psize[i] < cur_fmt->sizeimage[i])
+ for (i = 0; i < cur_fmt->mem_planes; i++) {
+ if (psize[i] < vpu_get_fmt_plane_size(cur_fmt, i))
return -EINVAL;
}
return 0;
@@ -402,9 +484,9 @@ static int vpu_vb2_queue_setup(struct vb2_queue *vq,
*buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_out);
else
*buf_count = max_t(unsigned int, *buf_count, inst->min_buffer_cap);
- *plane_count = cur_fmt->num_planes;
- for (i = 0; i < cur_fmt->num_planes; i++)
- psize[i] = cur_fmt->sizeimage[i];
+ *plane_count = cur_fmt->mem_planes;
+ for (i = 0; i < cur_fmt->mem_planes; i++)
+ psize[i] = vpu_get_fmt_plane_size(cur_fmt, i);
return 0;
}
@@ -434,8 +516,8 @@ static int vpu_vb2_buf_prepare(struct vb2_buffer *vb)
u32 i;
cur_fmt = vpu_get_format(inst, vb->type);
- for (i = 0; i < cur_fmt->num_planes; i++) {
- if (vpu_get_vb_length(vb, i) < cur_fmt->sizeimage[i]) {
+ for (i = 0; i < cur_fmt->mem_planes; i++) {
+ if (vpu_get_vb_length(vb, i) < vpu_get_fmt_plane_size(cur_fmt, i)) {
dev_dbg(inst->dev, "[%d] %s buf[%d] is invalid\n",
inst->id, vpu_type_name(vb->type), vb->index);
vpu_set_buffer_state(vbuf, VPU_BUF_STATE_ERROR);
@@ -603,10 +685,6 @@ static int vpu_v4l2_release(struct vpu_inst *inst)
inst->workqueue = NULL;
}
- if (inst->fh.m2m_ctx) {
- v4l2_m2m_ctx_release(inst->fh.m2m_ctx);
- inst->fh.m2m_ctx = NULL;
- }
v4l2_ctrl_handler_free(&inst->ctrl_handler);
mutex_destroy(&inst->lock);
v4l2_fh_del(&inst->fh);
@@ -689,6 +767,13 @@ int vpu_v4l2_close(struct file *file)
vpu_trace(vpu->dev, "tgid = %d, pid = %d, inst = %p\n", inst->tgid, inst->pid, inst);
+ vpu_inst_lock(inst);
+ if (inst->fh.m2m_ctx) {
+ v4l2_m2m_ctx_release(inst->fh.m2m_ctx);
+ inst->fh.m2m_ctx = NULL;
+ }
+ vpu_inst_unlock(inst);
+
call_void_vop(inst, release);
vpu_inst_unregister(inst);
vpu_inst_put(inst);