2 * Rockchip RK3288 VPU codec driver
4 * Copyright (C) 2014 Rockchip Electronics Co., Ltd.
5 * Hertz Wong <hertz.wong@rock-chips.com>
7 * Copyright (C) 2014 Google, Inc.
8 * Tomasz Figa <tfiga@chromium.org>
10 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
12 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include "rk3288_vpu_common.h"
26 #include <linux/module.h>
27 #include <linux/version.h>
28 #include <linux/videodev2.h>
30 #include <media/v4l2-ctrls.h>
31 #include <media/v4l2-event.h>
32 #include <media/videobuf2-core.h>
33 #include <media/videobuf2-dma-sg.h>
35 #include "rk3288_vpu_dec.h"
36 #include "rk3288_vpu_hw.h"
38 #define DEF_SRC_FMT_DEC V4L2_PIX_FMT_H264_SLICE
39 #define DEF_DST_FMT_DEC V4L2_PIX_FMT_NV12
41 #define RK3288_DEC_MIN_WIDTH 48U
42 #define RK3288_DEC_MAX_WIDTH 3840U
43 #define RK3288_DEC_MIN_HEIGHT 48U
44 #define RK3288_DEC_MAX_HEIGHT 2160U
46 #define RK3288_H264_MAX_SLICES_PER_FRAME 16
48 static struct rk3288_vpu_fmt formats[] = {
50 .name = "4:2:0 1 plane Y/CbCr",
51 .fourcc = V4L2_PIX_FMT_NV12,
52 .codec_mode = RK_VPU_CODEC_NONE,
57 .name = "Slices of H264 Encoded Stream",
58 .fourcc = V4L2_PIX_FMT_H264_SLICE,
59 .codec_mode = RK_VPU_CODEC_H264D,
63 .name = "Frames of VP8 Encoded Stream",
64 .fourcc = V4L2_PIX_FMT_VP8_FRAME,
65 .codec_mode = RK_VPU_CODEC_VP8D,
70 static struct rk3288_vpu_fmt *find_format(u32 fourcc, bool bitstream)
76 for (i = 0; i < ARRAY_SIZE(formats); i++) {
77 if (formats[i].fourcc == fourcc &&
78 !!bitstream == (formats[i].codec_mode != RK_VPU_CODEC_NONE))
85 /* Indices of controls that need to be accessed directly. */
87 RK3288_VPU_DEC_CTRL_H264_SPS,
88 RK3288_VPU_DEC_CTRL_H264_PPS,
89 RK3288_VPU_DEC_CTRL_H264_SCALING_MATRIX,
90 RK3288_VPU_DEC_CTRL_H264_SLICE_PARAM,
91 RK3288_VPU_DEC_CTRL_H264_DECODE_PARAM,
92 RK3288_VPU_DEC_CTRL_VP8_FRAME_HDR,
95 static struct rk3288_vpu_control controls[] = {
96 /* H264 slice-based interface. */
97 [RK3288_VPU_DEC_CTRL_H264_SPS] = {
98 .id = V4L2_CID_MPEG_VIDEO_H264_SPS,
99 .type = V4L2_CTRL_TYPE_PRIVATE,
100 .name = "H264 SPS Parameters",
101 .elem_size = sizeof(struct v4l2_ctrl_h264_sps),
102 .max_stores = VIDEO_MAX_FRAME,
105 [RK3288_VPU_DEC_CTRL_H264_PPS] = {
106 .id = V4L2_CID_MPEG_VIDEO_H264_PPS,
107 .type = V4L2_CTRL_TYPE_PRIVATE,
108 .name = "H264 PPS Parameters",
109 .elem_size = sizeof(struct v4l2_ctrl_h264_pps),
110 .max_stores = VIDEO_MAX_FRAME,
113 [RK3288_VPU_DEC_CTRL_H264_SCALING_MATRIX] = {
114 .id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX,
115 .type = V4L2_CTRL_TYPE_PRIVATE,
116 .name = "H264 Scaling Matrix",
117 .elem_size = sizeof(struct v4l2_ctrl_h264_scaling_matrix),
118 .max_stores = VIDEO_MAX_FRAME,
121 [RK3288_VPU_DEC_CTRL_H264_SLICE_PARAM] = {
122 .id = V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM,
123 .type = V4L2_CTRL_TYPE_PRIVATE,
124 .name = "H264 Slice Parameters",
125 .max_stores = VIDEO_MAX_FRAME,
126 .elem_size = sizeof(struct v4l2_ctrl_h264_slice_param),
127 .dims = { RK3288_H264_MAX_SLICES_PER_FRAME, },
130 [RK3288_VPU_DEC_CTRL_H264_DECODE_PARAM] = {
131 .id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM,
132 .type = V4L2_CTRL_TYPE_PRIVATE,
133 .name = "H264 Decode Parameters",
134 .max_stores = VIDEO_MAX_FRAME,
135 .elem_size = sizeof(struct v4l2_ctrl_h264_decode_param),
138 [RK3288_VPU_DEC_CTRL_VP8_FRAME_HDR] = {
139 .id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR,
140 .type = V4L2_CTRL_TYPE_PRIVATE,
141 .name = "VP8 Frame Header Parameters",
142 .max_stores = VIDEO_MAX_FRAME,
143 .elem_size = sizeof(struct v4l2_ctrl_vp8_frame_hdr),
148 static inline const void *get_ctrl_ptr(struct rk3288_vpu_ctx *ctx, unsigned id)
150 struct v4l2_ctrl *ctrl = ctx->ctrls[id];
152 return ctrl->p_cur.p;
155 /* Query capabilities of the device */
156 static int vidioc_querycap(struct file *file, void *priv,
157 struct v4l2_capability *cap)
159 struct rk3288_vpu_dev *dev = video_drvdata(file);
163 strlcpy(cap->driver, RK3288_VPU_DEC_NAME, sizeof(cap->driver));
164 strlcpy(cap->card, dev->pdev->name, sizeof(cap->card));
165 strlcpy(cap->bus_info, "platform:" RK3288_VPU_NAME,
166 sizeof(cap->bus_info));
169 * This is only a mem-to-mem video device. The capture and output
170 * device capability flags are left only for backward compatibility
171 * and are scheduled for removal.
173 cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
174 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
181 static int vidioc_enum_framesizes(struct file *file, void *prov,
182 struct v4l2_frmsizeenum *fsize)
184 struct v4l2_frmsize_stepwise *s = &fsize->stepwise;
185 struct rk3288_vpu_fmt *fmt;
187 if (fsize->index != 0) {
188 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
193 fmt = find_format(fsize->pixel_format, true);
195 vpu_debug(0, "unsupported bitstream format (%08x)\n",
196 fsize->pixel_format);
200 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
202 s->min_width = RK3288_DEC_MIN_WIDTH;
203 s->max_width = RK3288_DEC_MAX_WIDTH;
204 s->step_width = MB_DIM;
205 s->min_height = RK3288_DEC_MIN_HEIGHT;
206 s->max_height = RK3288_DEC_MAX_HEIGHT;
207 s->step_height = MB_DIM;
212 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool out)
214 struct rk3288_vpu_fmt *fmt;
219 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
220 if (out && formats[i].codec_mode == RK_VPU_CODEC_NONE)
222 else if (!out && (formats[i].codec_mode != RK_VPU_CODEC_NONE))
227 strlcpy(f->description, fmt->name,
228 sizeof(f->description));
229 f->pixelformat = fmt->fourcc;
232 if (formats[i].codec_mode != RK_VPU_CODEC_NONE)
233 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
248 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
249 struct v4l2_fmtdesc *f)
251 return vidioc_enum_fmt(f, false);
254 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
255 struct v4l2_fmtdesc *f)
257 return vidioc_enum_fmt(f, true);
260 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
262 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
266 vpu_debug(4, "f->type = %d\n", f->type);
269 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
270 f->fmt.pix_mp = ctx->dst_fmt;
273 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
274 f->fmt.pix_mp = ctx->src_fmt;
278 vpu_err("invalid buf type\n");
287 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
289 struct rk3288_vpu_fmt *fmt;
290 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
296 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
297 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
299 fmt = find_format(pix_fmt_mp->pixelformat, true);
301 vpu_err("failed to try output format\n");
305 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
306 vpu_err("sizeimage of output format must be given\n");
310 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
313 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
314 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
316 fmt = find_format(pix_fmt_mp->pixelformat, false);
318 vpu_err("failed to try capture format\n");
322 if (fmt->num_planes != pix_fmt_mp->num_planes) {
323 vpu_err("plane number mismatches on capture format\n");
327 /* Limit to hardware min/max. */
328 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
329 RK3288_DEC_MIN_WIDTH, RK3288_DEC_MAX_WIDTH);
330 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
331 RK3288_DEC_MIN_HEIGHT, RK3288_DEC_MAX_HEIGHT);
333 /* Round up to macroblocks. */
334 pix_fmt_mp->width = round_up(pix_fmt_mp->width, MB_DIM);
335 pix_fmt_mp->height = round_up(pix_fmt_mp->height, MB_DIM);
339 vpu_err("invalid buf type\n");
348 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
350 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
351 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
352 unsigned int mb_width, mb_height;
353 struct rk3288_vpu_fmt *fmt;
360 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
361 /* Change not allowed if any queue is streaming. */
362 if (vb2_is_streaming(&ctx->vq_src)
363 || vb2_is_streaming(&ctx->vq_dst)) {
368 * Pixel format change is not allowed when the other queue has
371 if (vb2_is_busy(&ctx->vq_dst)
372 && pix_fmt_mp->pixelformat != ctx->src_fmt.pixelformat) {
377 ret = vidioc_try_fmt(file, priv, f);
381 ctx->vpu_src_fmt = find_format(pix_fmt_mp->pixelformat, true);
382 ctx->src_fmt = *pix_fmt_mp;
385 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
387 * Change not allowed if this queue is streaming.
389 * NOTE: We allow changes with source queue streaming
390 * to support resolution change in decoded stream.
392 if (vb2_is_streaming(&ctx->vq_dst)) {
397 * Pixel format change is not allowed when the other queue has
400 if (vb2_is_busy(&ctx->vq_src)
401 && pix_fmt_mp->pixelformat != ctx->dst_fmt.pixelformat) {
406 ret = vidioc_try_fmt(file, priv, f);
410 fmt = find_format(pix_fmt_mp->pixelformat, false);
411 ctx->vpu_dst_fmt = fmt;
413 mb_width = MB_WIDTH(pix_fmt_mp->width);
414 mb_height = MB_HEIGHT(pix_fmt_mp->height);
416 vpu_debug(0, "CAPTURE codec mode: %d\n", fmt->codec_mode);
417 vpu_debug(0, "fmt - w: %d, h: %d, mb - w: %d, h: %d\n",
418 pix_fmt_mp->width, pix_fmt_mp->height,
419 mb_width, mb_height);
421 for (i = 0; i < fmt->num_planes; ++i) {
422 pix_fmt_mp->plane_fmt[i].bytesperline =
423 mb_width * MB_DIM * fmt->depth[i] / 8;
424 pix_fmt_mp->plane_fmt[i].sizeimage =
425 pix_fmt_mp->plane_fmt[i].bytesperline
426 * mb_height * MB_DIM;
428 * All of multiplanar formats we support have chroma
429 * planes subsampled by 2.
432 pix_fmt_mp->plane_fmt[i].sizeimage /= 2;
435 ctx->dst_fmt = *pix_fmt_mp;
439 vpu_err("invalid buf type\n");
449 static int vidioc_reqbufs(struct file *file, void *priv,
450 struct v4l2_requestbuffers *reqbufs)
452 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
457 switch (reqbufs->type) {
458 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
459 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
461 vpu_err("error in vb2_reqbufs() for E(S)\n");
466 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
467 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
469 vpu_err("error in vb2_reqbufs() for E(D)\n");
475 vpu_err("invalid buf type\n");
486 static int vidioc_querybuf(struct file *file, void *priv,
487 struct v4l2_buffer *buf)
489 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
495 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
496 ret = vb2_querybuf(&ctx->vq_dst, buf);
498 vpu_err("error in vb2_querybuf() for E(D)\n");
502 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
505 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
506 ret = vb2_querybuf(&ctx->vq_src, buf);
508 vpu_err("error in vb2_querybuf() for E(S)\n");
514 vpu_err("invalid buf type\n");
526 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
528 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
534 for (i = 0; i < buf->length; i++)
535 vpu_debug(4, "plane[%d]->length %d bytesused %d\n",
536 i, buf->m.planes[i].length,
537 buf->m.planes[i].bytesused);
540 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
541 ret = vb2_qbuf(&ctx->vq_src, buf);
542 vpu_debug(4, "OUTPUT_MPLANE : vb2_qbuf return %d\n", ret);
545 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
546 ret = vb2_qbuf(&ctx->vq_dst, buf);
547 vpu_debug(4, "CAPTURE_MPLANE: vb2_qbuf return %d\n", ret);
559 /* Dequeue a buffer */
560 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
562 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
568 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
569 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
572 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
573 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
585 /* Export DMA buffer */
586 static int vidioc_expbuf(struct file *file, void *priv,
587 struct v4l2_exportbuffer *eb)
589 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
595 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
596 ret = vb2_expbuf(&ctx->vq_src, eb);
599 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
600 ret = vb2_expbuf(&ctx->vq_dst, eb);
613 static int vidioc_streamon(struct file *file, void *priv,
614 enum v4l2_buf_type type)
616 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
622 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
623 ret = vb2_streamon(&ctx->vq_src, type);
626 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
627 ret = vb2_streamon(&ctx->vq_dst, type);
639 /* Stream off, which equals to a pause */
640 static int vidioc_streamoff(struct file *file, void *priv,
641 enum v4l2_buf_type type)
643 struct rk3288_vpu_ctx *ctx = fh_to_ctx(priv);
649 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
650 ret = vb2_streamoff(&ctx->vq_src, type);
653 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
654 ret = vb2_streamoff(&ctx->vq_dst, type);
666 static void rk3288_vpu_dec_set_dpb(struct rk3288_vpu_ctx *ctx,
667 struct v4l2_ctrl *ctrl)
669 struct v4l2_ctrl_h264_decode_param *dec_param = ctrl->p_new.p;
670 const struct v4l2_h264_dpb_entry *new_dpb_entry;
671 u8 *dpb_map = ctx->run.h264d.dpb_map;
672 struct v4l2_h264_dpb_entry *cur_dpb_entry;
673 DECLARE_BITMAP(used, ARRAY_SIZE(ctx->run.h264d.dpb)) = { 0, };
674 DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
677 BUILD_BUG_ON(ARRAY_SIZE(ctx->run.h264d.dpb) !=
678 ARRAY_SIZE(dec_param->dpb));
680 /* Disable all entries by default. */
681 for (j = 0; j < ARRAY_SIZE(ctx->run.h264d.dpb); ++j) {
682 cur_dpb_entry = &ctx->run.h264d.dpb[j];
684 cur_dpb_entry->flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
687 /* Try to match new DPB entries with existing ones by their POCs. */
688 for (i = 0; i < ARRAY_SIZE(dec_param->dpb); ++i) {
689 new_dpb_entry = &dec_param->dpb[i];
691 if (!(new_dpb_entry->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
695 * To cut off some comparisons, iterate only on target DPB
696 * entries which are not used yet.
698 for_each_clear_bit(j, used, ARRAY_SIZE(ctx->run.h264d.dpb)) {
699 cur_dpb_entry = &ctx->run.h264d.dpb[j];
701 if (new_dpb_entry->top_field_order_cnt ==
702 cur_dpb_entry->top_field_order_cnt &&
703 new_dpb_entry->bottom_field_order_cnt ==
704 cur_dpb_entry->bottom_field_order_cnt) {
705 memcpy(cur_dpb_entry, new_dpb_entry,
706 sizeof(*cur_dpb_entry));
713 if (j == ARRAY_SIZE(ctx->run.h264d.dpb))
717 /* For entries that could not be matched, use remaining free slots. */
718 for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
719 new_dpb_entry = &dec_param->dpb[i];
721 j = find_first_zero_bit(used, ARRAY_SIZE(ctx->run.h264d.dpb));
723 * Both arrays are of the same sizes, so there is no way
724 * we can end up with no space in target array, unless
725 * something is buggy.
727 if (WARN_ON(j >= ARRAY_SIZE(ctx->run.h264d.dpb)))
730 cur_dpb_entry = &ctx->run.h264d.dpb[j];
731 memcpy(cur_dpb_entry, new_dpb_entry, sizeof(*cur_dpb_entry));
737 * Verify that reference picture lists are in range, since they
738 * will be indexing dpb_map[] when programming the hardware.
740 * Fallback to 0 should be safe, as we will get at most corrupt
741 * decoding result, without any serious side effects. Moreover,
742 * even if entry 0 is unused, the hardware programming code will
743 * handle this properly.
745 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b0); ++i)
746 if (dec_param->ref_pic_list_b0[i]
747 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
748 dec_param->ref_pic_list_b0[i] = 0;
749 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b1); ++i)
750 if (dec_param->ref_pic_list_b1[i]
751 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
752 dec_param->ref_pic_list_b1[i] = 0;
753 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_p0); ++i)
754 if (dec_param->ref_pic_list_p0[i]
755 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
756 dec_param->ref_pic_list_p0[i] = 0;
759 static int rk3288_vpu_dec_s_ctrl(struct v4l2_ctrl *ctrl)
761 struct rk3288_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
762 struct rk3288_vpu_dev *dev = ctx->dev;
767 vpu_debug(4, "ctrl id %d\n", ctrl->id);
770 case V4L2_CID_MPEG_VIDEO_H264_SPS:
771 case V4L2_CID_MPEG_VIDEO_H264_PPS:
772 case V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX:
773 case V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM:
774 case V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR:
775 /* These controls are used directly. */
778 case V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM:
781 rk3288_vpu_dec_set_dpb(ctx, ctrl);
785 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
786 ctrl->id, ctrl->val);
795 static const struct v4l2_ctrl_ops rk3288_vpu_dec_ctrl_ops = {
796 .s_ctrl = rk3288_vpu_dec_s_ctrl,
799 static const struct v4l2_ioctl_ops rk3288_vpu_dec_ioctl_ops = {
800 .vidioc_querycap = vidioc_querycap,
801 .vidioc_enum_framesizes = vidioc_enum_framesizes,
802 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
803 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
804 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
805 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
806 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
807 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
808 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
809 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
810 .vidioc_reqbufs = vidioc_reqbufs,
811 .vidioc_querybuf = vidioc_querybuf,
812 .vidioc_qbuf = vidioc_qbuf,
813 .vidioc_dqbuf = vidioc_dqbuf,
814 .vidioc_expbuf = vidioc_expbuf,
815 .vidioc_streamon = vidioc_streamon,
816 .vidioc_streamoff = vidioc_streamoff,
819 static int rk3288_vpu_queue_setup(struct vb2_queue *vq,
820 const struct v4l2_format *fmt,
821 unsigned int *buf_count,
822 unsigned int *plane_count,
823 unsigned int psize[], void *allocators[])
825 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
831 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
832 *plane_count = ctx->vpu_src_fmt->num_planes;
837 if (*buf_count > VIDEO_MAX_FRAME)
838 *buf_count = VIDEO_MAX_FRAME;
840 psize[0] = ctx->src_fmt.plane_fmt[0].sizeimage;
841 allocators[0] = ctx->dev->alloc_ctx;
842 vpu_debug(0, "output psize[%d]: %d\n", 0, psize[0]);
845 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
846 *plane_count = ctx->vpu_dst_fmt->num_planes;
851 if (*buf_count > VIDEO_MAX_FRAME)
852 *buf_count = VIDEO_MAX_FRAME;
854 psize[0] = round_up(ctx->dst_fmt.plane_fmt[0].sizeimage, 8);
855 allocators[0] = ctx->dev->alloc_ctx;
857 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE)
858 /* Add space for appended motion vectors. */
859 psize[0] += 64 * MB_WIDTH(ctx->dst_fmt.width)
860 * MB_HEIGHT(ctx->dst_fmt.height);
862 vpu_debug(0, "capture psize[%d]: %d\n", 0, psize[0]);
866 vpu_err("invalid queue type: %d\n", vq->type);
875 static int rk3288_vpu_buf_init(struct vb2_buffer *vb)
877 struct vb2_queue *vq = vb->vb2_queue;
878 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
882 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
883 ctx->dst_bufs[vb->v4l2_buf.index] = vb;
890 static void rk3288_vpu_buf_cleanup(struct vb2_buffer *vb)
892 struct vb2_queue *vq = vb->vb2_queue;
893 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
897 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
898 ctx->dst_bufs[vb->v4l2_buf.index] = NULL;
903 static int rk3288_vpu_buf_prepare(struct vb2_buffer *vb)
905 struct vb2_queue *vq = vb->vb2_queue;
906 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
913 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
914 vpu_debug(4, "plane size: %ld, dst size: %d\n",
915 vb2_plane_size(vb, 0),
916 ctx->src_fmt.plane_fmt[0].sizeimage);
918 if (vb2_plane_size(vb, 0)
919 < ctx->src_fmt.plane_fmt[0].sizeimage) {
920 vpu_err("plane size is too small for output\n");
925 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
926 for (i = 0; i < ctx->vpu_dst_fmt->num_planes; ++i) {
927 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", i,
928 vb2_plane_size(vb, i),
929 ctx->dst_fmt.plane_fmt[i].sizeimage);
931 if (vb2_plane_size(vb, i)
932 < ctx->dst_fmt.plane_fmt[i].sizeimage) {
933 vpu_err("size of plane %d is too small for capture\n",
939 if (i != ctx->vpu_dst_fmt->num_planes)
944 vpu_err("invalid queue type: %d\n", vq->type);
953 static int rk3288_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
956 struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
957 struct rk3288_vpu_dev *dev = ctx->dev;
962 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
963 ret = rk3288_vpu_init(ctx);
965 vpu_err("rk3288_vpu_init failed\n");
969 ready = vb2_is_streaming(&ctx->vq_src);
970 } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
971 ready = vb2_is_streaming(&ctx->vq_dst);
975 rk3288_vpu_try_context(dev, ctx);
982 static void rk3288_vpu_stop_streaming(struct vb2_queue *q)
985 struct rk3288_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
986 struct rk3288_vpu_dev *dev = ctx->dev;
987 struct rk3288_vpu_buf *b;
993 spin_lock_irqsave(&dev->irqlock, flags);
995 list_del_init(&ctx->list);
998 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
999 list_splice_init(&ctx->dst_queue, &queue);
1002 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1003 list_splice_init(&ctx->src_queue, &queue);
1010 spin_unlock_irqrestore(&dev->irqlock, flags);
1012 wait_event(dev->run_wq, dev->current_ctx != ctx);
1014 while (!list_empty(&queue)) {
1015 b = list_first_entry(&queue, struct rk3288_vpu_buf, list);
1016 for (i = 0; i < b->b.num_planes; i++)
1017 vb2_set_plane_payload(&b->b, i, 0);
1018 vb2_buffer_done(&b->b, VB2_BUF_STATE_ERROR);
1022 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1023 rk3288_vpu_deinit(ctx);
1028 static void rk3288_vpu_buf_queue(struct vb2_buffer *vb)
1030 struct vb2_queue *vq = vb->vb2_queue;
1031 struct rk3288_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1032 struct rk3288_vpu_dev *dev = ctx->dev;
1033 struct rk3288_vpu_buf *vpu_buf;
1034 unsigned long flags;
1039 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1040 vpu_buf = vb_to_buf(vb);
1042 /* Mark destination as available for use by VPU */
1043 spin_lock_irqsave(&dev->irqlock, flags);
1045 list_add_tail(&vpu_buf->list, &ctx->dst_queue);
1047 spin_unlock_irqrestore(&dev->irqlock, flags);
1050 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1051 vpu_buf = vb_to_buf(vb);
1053 spin_lock_irqsave(&dev->irqlock, flags);
1055 list_add_tail(&vpu_buf->list, &ctx->src_queue);
1057 spin_unlock_irqrestore(&dev->irqlock, flags);
1061 vpu_err("unsupported buffer type (%d)\n", vq->type);
1064 if (vb2_is_streaming(&ctx->vq_src) && vb2_is_streaming(&ctx->vq_dst))
1065 rk3288_vpu_try_context(dev, ctx);
1070 static struct vb2_ops rk3288_vpu_dec_qops = {
1071 .queue_setup = rk3288_vpu_queue_setup,
1072 .wait_prepare = vb2_ops_wait_prepare,
1073 .wait_finish = vb2_ops_wait_finish,
1074 .buf_init = rk3288_vpu_buf_init,
1075 .buf_prepare = rk3288_vpu_buf_prepare,
1076 .buf_cleanup = rk3288_vpu_buf_cleanup,
1077 .start_streaming = rk3288_vpu_start_streaming,
1078 .stop_streaming = rk3288_vpu_stop_streaming,
1079 .buf_queue = rk3288_vpu_buf_queue,
1082 struct vb2_ops *get_dec_queue_ops(void)
1084 return &rk3288_vpu_dec_qops;
1087 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1089 return &rk3288_vpu_dec_ioctl_ops;
1092 static void rk3288_vpu_dec_prepare_run(struct rk3288_vpu_ctx *ctx)
1094 struct v4l2_buffer *src = &ctx->run.src->b.v4l2_buf;
1096 v4l2_ctrl_apply_store(&ctx->ctrl_handler, src->config_store);
1098 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE) {
1099 ctx->run.h264d.sps = get_ctrl_ptr(ctx,
1100 RK3288_VPU_DEC_CTRL_H264_SPS);
1101 ctx->run.h264d.pps = get_ctrl_ptr(ctx,
1102 RK3288_VPU_DEC_CTRL_H264_PPS);
1103 ctx->run.h264d.scaling_matrix = get_ctrl_ptr(ctx,
1104 RK3288_VPU_DEC_CTRL_H264_SCALING_MATRIX);
1105 ctx->run.h264d.slice_param = get_ctrl_ptr(ctx,
1106 RK3288_VPU_DEC_CTRL_H264_SLICE_PARAM);
1107 ctx->run.h264d.decode_param = get_ctrl_ptr(ctx,
1108 RK3288_VPU_DEC_CTRL_H264_DECODE_PARAM);
1109 } else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP8_FRAME) {
1110 ctx->run.vp8d.frame_hdr = get_ctrl_ptr(ctx,
1111 RK3288_VPU_DEC_CTRL_VP8_FRAME_HDR);
1115 static void rk3288_vpu_dec_run_done(struct rk3288_vpu_ctx *ctx,
1116 enum vb2_buffer_state result)
1118 struct v4l2_plane_pix_format *plane_fmts = ctx->dst_fmt.plane_fmt;
1119 struct vb2_buffer *dst = &ctx->run.dst->b;
1122 if (result != VB2_BUF_STATE_DONE) {
1123 /* Assume no payload after failed run. */
1124 for (i = 0; i < dst->num_planes; ++i)
1125 vb2_set_plane_payload(dst, i, 0);
1129 for (i = 0; i < dst->num_planes; ++i)
1130 vb2_set_plane_payload(dst, i, plane_fmts[i].sizeimage);
1133 static const struct rk3288_vpu_run_ops rk3288_vpu_dec_run_ops = {
1134 .prepare_run = rk3288_vpu_dec_prepare_run,
1135 .run_done = rk3288_vpu_dec_run_done,
1138 int rk3288_vpu_dec_init(struct rk3288_vpu_ctx *ctx)
1140 ctx->vpu_src_fmt = find_format(DEF_SRC_FMT_DEC, false);
1141 ctx->vpu_dst_fmt = find_format(DEF_DST_FMT_DEC, true);
1143 ctx->run_ops = &rk3288_vpu_dec_run_ops;
1145 return rk3288_vpu_ctrls_setup(ctx, &rk3288_vpu_dec_ctrl_ops,
1146 controls, ARRAY_SIZE(controls), NULL);
1149 void rk3288_vpu_dec_exit(struct rk3288_vpu_ctx *ctx)
1151 rk3288_vpu_ctrls_delete(ctx);