2 * Rockchip 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 "rockchip_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 "rockchip_vpu_dec.h"
36 #include "rockchip_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 ROCKCHIP_DEC_MIN_WIDTH 48U
42 #define ROCKCHIP_DEC_MAX_WIDTH 3840U
43 #define ROCKCHIP_DEC_MIN_HEIGHT 48U
44 #define ROCKCHIP_DEC_MAX_HEIGHT 2160U
46 #define ROCKCHIP_H264_MAX_SLICES_PER_FRAME 16
48 static struct rockchip_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 = RK3288_VPU_CODEC_H264D,
63 .name = "Frames of VP8 Encoded Stream",
64 .fourcc = V4L2_PIX_FMT_VP8_FRAME,
65 .codec_mode = RK3288_VPU_CODEC_VP8D,
70 static struct rockchip_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 ROCKCHIP_VPU_DEC_CTRL_H264_SPS,
88 ROCKCHIP_VPU_DEC_CTRL_H264_PPS,
89 ROCKCHIP_VPU_DEC_CTRL_H264_SCALING_MATRIX,
90 ROCKCHIP_VPU_DEC_CTRL_H264_SLICE_PARAM,
91 ROCKCHIP_VPU_DEC_CTRL_H264_DECODE_PARAM,
92 ROCKCHIP_VPU_DEC_CTRL_VP8_FRAME_HDR,
95 static struct rockchip_vpu_control controls[] = {
96 /* H264 slice-based interface. */
97 [ROCKCHIP_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 [ROCKCHIP_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 [ROCKCHIP_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 [ROCKCHIP_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 = { ROCKCHIP_H264_MAX_SLICES_PER_FRAME, },
130 [ROCKCHIP_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 [ROCKCHIP_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 rockchip_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 rockchip_vpu_dev *dev = video_drvdata(file);
163 strlcpy(cap->driver, ROCKCHIP_VPU_DEC_NAME, sizeof(cap->driver));
164 strlcpy(cap->card, dev->pdev->name, sizeof(cap->card));
165 strlcpy(cap->bus_info, "platform:" ROCKCHIP_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->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
174 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
175 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
182 static int vidioc_enum_framesizes(struct file *file, void *prov,
183 struct v4l2_frmsizeenum *fsize)
185 struct v4l2_frmsize_stepwise *s = &fsize->stepwise;
186 struct rockchip_vpu_fmt *fmt;
188 if (fsize->index != 0) {
189 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
194 fmt = find_format(fsize->pixel_format, true);
196 vpu_debug(0, "unsupported bitstream format (%08x)\n",
197 fsize->pixel_format);
201 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
203 s->min_width = ROCKCHIP_DEC_MIN_WIDTH;
204 s->max_width = ROCKCHIP_DEC_MAX_WIDTH;
205 s->step_width = MB_DIM;
206 s->min_height = ROCKCHIP_DEC_MIN_HEIGHT;
207 s->max_height = ROCKCHIP_DEC_MAX_HEIGHT;
208 s->step_height = MB_DIM;
213 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool out)
215 struct rockchip_vpu_fmt *fmt;
220 for (i = 0; i < ARRAY_SIZE(formats); ++i) {
221 if (out && formats[i].codec_mode == RK_VPU_CODEC_NONE)
223 else if (!out && (formats[i].codec_mode != RK_VPU_CODEC_NONE))
228 strlcpy(f->description, fmt->name,
229 sizeof(f->description));
230 f->pixelformat = fmt->fourcc;
233 if (formats[i].codec_mode != RK_VPU_CODEC_NONE)
234 f->flags |= V4L2_FMT_FLAG_COMPRESSED;
249 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
250 struct v4l2_fmtdesc *f)
252 return vidioc_enum_fmt(f, false);
255 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
256 struct v4l2_fmtdesc *f)
258 return vidioc_enum_fmt(f, true);
261 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
263 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
267 vpu_debug(4, "f->type = %d\n", f->type);
270 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
271 f->fmt.pix_mp = ctx->dst_fmt;
274 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
275 f->fmt.pix_mp = ctx->src_fmt;
279 vpu_err("invalid buf type\n");
288 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
290 struct rockchip_vpu_fmt *fmt;
291 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
297 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
298 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
300 fmt = find_format(pix_fmt_mp->pixelformat, true);
302 vpu_err("failed to try output format\n");
306 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
307 vpu_err("sizeimage of output format must be given\n");
311 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
314 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
315 vpu_debug(4, "%s\n", fmt2str(f->fmt.pix_mp.pixelformat, str));
317 fmt = find_format(pix_fmt_mp->pixelformat, false);
319 vpu_err("failed to try capture format\n");
323 if (fmt->num_planes != pix_fmt_mp->num_planes) {
324 vpu_err("plane number mismatches on capture format\n");
328 /* Limit to hardware min/max. */
329 pix_fmt_mp->width = clamp(pix_fmt_mp->width,
330 ROCKCHIP_DEC_MIN_WIDTH, ROCKCHIP_DEC_MAX_WIDTH);
331 pix_fmt_mp->height = clamp(pix_fmt_mp->height,
332 ROCKCHIP_DEC_MIN_HEIGHT, ROCKCHIP_DEC_MAX_HEIGHT);
334 /* Round up to macroblocks. */
335 pix_fmt_mp->width = round_up(pix_fmt_mp->width, MB_DIM);
336 pix_fmt_mp->height = round_up(pix_fmt_mp->height, MB_DIM);
340 vpu_err("invalid buf type\n");
349 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
351 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
352 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
353 unsigned int mb_width, mb_height;
354 struct rockchip_vpu_fmt *fmt;
361 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
362 /* Change not allowed if any queue is streaming. */
363 if (vb2_is_streaming(&ctx->vq_src)
364 || vb2_is_streaming(&ctx->vq_dst)) {
369 * Pixel format change is not allowed when the other queue has
372 if (vb2_is_busy(&ctx->vq_dst)
373 && pix_fmt_mp->pixelformat != ctx->src_fmt.pixelformat) {
378 ret = vidioc_try_fmt(file, priv, f);
382 ctx->vpu_src_fmt = find_format(pix_fmt_mp->pixelformat, true);
383 ctx->src_fmt = *pix_fmt_mp;
386 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
388 * Change not allowed if this queue is streaming.
390 * NOTE: We allow changes with source queue streaming
391 * to support resolution change in decoded stream.
393 if (vb2_is_streaming(&ctx->vq_dst)) {
398 * Pixel format change is not allowed when the other queue has
401 if (vb2_is_busy(&ctx->vq_src)
402 && pix_fmt_mp->pixelformat != ctx->dst_fmt.pixelformat) {
407 ret = vidioc_try_fmt(file, priv, f);
411 fmt = find_format(pix_fmt_mp->pixelformat, false);
412 ctx->vpu_dst_fmt = fmt;
414 mb_width = MB_WIDTH(pix_fmt_mp->width);
415 mb_height = MB_HEIGHT(pix_fmt_mp->height);
417 vpu_debug(0, "CAPTURE codec mode: %d\n", fmt->codec_mode);
418 vpu_debug(0, "fmt - w: %d, h: %d, mb - w: %d, h: %d\n",
419 pix_fmt_mp->width, pix_fmt_mp->height,
420 mb_width, mb_height);
422 for (i = 0; i < fmt->num_planes; ++i) {
423 pix_fmt_mp->plane_fmt[i].bytesperline =
424 mb_width * MB_DIM * fmt->depth[i] / 8;
425 pix_fmt_mp->plane_fmt[i].sizeimage =
426 pix_fmt_mp->plane_fmt[i].bytesperline
427 * mb_height * MB_DIM;
429 * All of multiplanar formats we support have chroma
430 * planes subsampled by 2.
433 pix_fmt_mp->plane_fmt[i].sizeimage /= 2;
436 ctx->dst_fmt = *pix_fmt_mp;
440 vpu_err("invalid buf type\n");
450 static int vidioc_reqbufs(struct file *file, void *priv,
451 struct v4l2_requestbuffers *reqbufs)
453 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
458 switch (reqbufs->type) {
459 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
460 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
462 vpu_err("error in vb2_reqbufs() for E(S)\n");
467 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
468 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
470 vpu_err("error in vb2_reqbufs() for E(D)\n");
476 vpu_err("invalid buf type\n");
487 static int vidioc_querybuf(struct file *file, void *priv,
488 struct v4l2_buffer *buf)
490 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
496 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
497 ret = vb2_querybuf(&ctx->vq_dst, buf);
499 vpu_err("error in vb2_querybuf() for E(D)\n");
503 buf->m.planes[0].m.mem_offset += DST_QUEUE_OFF_BASE;
506 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
507 ret = vb2_querybuf(&ctx->vq_src, buf);
509 vpu_err("error in vb2_querybuf() for E(S)\n");
515 vpu_err("invalid buf type\n");
527 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
529 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
535 for (i = 0; i < buf->length; i++)
536 vpu_debug(4, "plane[%d]->length %d bytesused %d\n",
537 i, buf->m.planes[i].length,
538 buf->m.planes[i].bytesused);
541 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
542 ret = vb2_qbuf(&ctx->vq_src, buf);
543 vpu_debug(4, "OUTPUT_MPLANE : vb2_qbuf return %d\n", ret);
546 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
547 ret = vb2_qbuf(&ctx->vq_dst, buf);
548 vpu_debug(4, "CAPTURE_MPLANE: vb2_qbuf return %d\n", ret);
560 /* Dequeue a buffer */
561 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
563 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
569 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
570 ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
573 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
574 ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
586 /* Export DMA buffer */
587 static int vidioc_expbuf(struct file *file, void *priv,
588 struct v4l2_exportbuffer *eb)
590 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
596 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
597 ret = vb2_expbuf(&ctx->vq_src, eb);
600 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
601 ret = vb2_expbuf(&ctx->vq_dst, eb);
614 static int vidioc_streamon(struct file *file, void *priv,
615 enum v4l2_buf_type type)
617 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
623 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
624 ret = vb2_streamon(&ctx->vq_src, type);
627 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
628 ret = vb2_streamon(&ctx->vq_dst, type);
640 /* Stream off, which equals to a pause */
641 static int vidioc_streamoff(struct file *file, void *priv,
642 enum v4l2_buf_type type)
644 struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
650 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
651 ret = vb2_streamoff(&ctx->vq_src, type);
654 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
655 ret = vb2_streamoff(&ctx->vq_dst, type);
667 static void rockchip_vpu_dec_set_dpb(struct rockchip_vpu_ctx *ctx,
668 struct v4l2_ctrl *ctrl)
670 struct v4l2_ctrl_h264_decode_param *dec_param = ctrl->p_new.p;
671 const struct v4l2_h264_dpb_entry *new_dpb_entry;
672 u8 *dpb_map = ctx->run.h264d.dpb_map;
673 struct v4l2_h264_dpb_entry *cur_dpb_entry;
674 DECLARE_BITMAP(used, ARRAY_SIZE(ctx->run.h264d.dpb)) = { 0, };
675 DECLARE_BITMAP(new, ARRAY_SIZE(dec_param->dpb)) = { 0, };
678 BUILD_BUG_ON(ARRAY_SIZE(ctx->run.h264d.dpb) !=
679 ARRAY_SIZE(dec_param->dpb));
681 /* Disable all entries by default. */
682 for (j = 0; j < ARRAY_SIZE(ctx->run.h264d.dpb); ++j) {
683 cur_dpb_entry = &ctx->run.h264d.dpb[j];
685 cur_dpb_entry->flags &= ~V4L2_H264_DPB_ENTRY_FLAG_ACTIVE;
688 /* Try to match new DPB entries with existing ones by their POCs. */
689 for (i = 0; i < ARRAY_SIZE(dec_param->dpb); ++i) {
690 new_dpb_entry = &dec_param->dpb[i];
692 if (!(new_dpb_entry->flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
696 * To cut off some comparisons, iterate only on target DPB
697 * entries which are not used yet.
699 for_each_clear_bit(j, used, ARRAY_SIZE(ctx->run.h264d.dpb)) {
700 cur_dpb_entry = &ctx->run.h264d.dpb[j];
702 if (new_dpb_entry->top_field_order_cnt ==
703 cur_dpb_entry->top_field_order_cnt &&
704 new_dpb_entry->bottom_field_order_cnt ==
705 cur_dpb_entry->bottom_field_order_cnt) {
706 memcpy(cur_dpb_entry, new_dpb_entry,
707 sizeof(*cur_dpb_entry));
714 if (j == ARRAY_SIZE(ctx->run.h264d.dpb))
718 /* For entries that could not be matched, use remaining free slots. */
719 for_each_set_bit(i, new, ARRAY_SIZE(dec_param->dpb)) {
720 new_dpb_entry = &dec_param->dpb[i];
722 j = find_first_zero_bit(used, ARRAY_SIZE(ctx->run.h264d.dpb));
724 * Both arrays are of the same sizes, so there is no way
725 * we can end up with no space in target array, unless
726 * something is buggy.
728 if (WARN_ON(j >= ARRAY_SIZE(ctx->run.h264d.dpb)))
731 cur_dpb_entry = &ctx->run.h264d.dpb[j];
732 memcpy(cur_dpb_entry, new_dpb_entry, sizeof(*cur_dpb_entry));
738 * Verify that reference picture lists are in range, since they
739 * will be indexing dpb_map[] when programming the hardware.
741 * Fallback to 0 should be safe, as we will get at most corrupt
742 * decoding result, without any serious side effects. Moreover,
743 * even if entry 0 is unused, the hardware programming code will
744 * handle this properly.
746 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b0); ++i)
747 if (dec_param->ref_pic_list_b0[i]
748 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
749 dec_param->ref_pic_list_b0[i] = 0;
750 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_b1); ++i)
751 if (dec_param->ref_pic_list_b1[i]
752 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
753 dec_param->ref_pic_list_b1[i] = 0;
754 for (i = 0; i < ARRAY_SIZE(dec_param->ref_pic_list_p0); ++i)
755 if (dec_param->ref_pic_list_p0[i]
756 >= ARRAY_SIZE(ctx->run.h264d.dpb_map))
757 dec_param->ref_pic_list_p0[i] = 0;
760 static int rockchip_vpu_dec_s_ctrl(struct v4l2_ctrl *ctrl)
762 struct rockchip_vpu_ctx *ctx = ctrl_to_ctx(ctrl);
763 struct rockchip_vpu_dev *dev = ctx->dev;
768 vpu_debug(4, "ctrl id %d\n", ctrl->id);
771 case V4L2_CID_MPEG_VIDEO_H264_SPS:
772 case V4L2_CID_MPEG_VIDEO_H264_PPS:
773 case V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX:
774 case V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM:
775 case V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR:
776 /* These controls are used directly. */
779 case V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM:
782 rockchip_vpu_dec_set_dpb(ctx, ctrl);
786 v4l2_err(&dev->v4l2_dev, "Invalid control, id=%d, val=%d\n",
787 ctrl->id, ctrl->val);
796 static const struct v4l2_ctrl_ops rockchip_vpu_dec_ctrl_ops = {
797 .s_ctrl = rockchip_vpu_dec_s_ctrl,
800 static const struct v4l2_ioctl_ops rockchip_vpu_dec_ioctl_ops = {
801 .vidioc_querycap = vidioc_querycap,
802 .vidioc_enum_framesizes = vidioc_enum_framesizes,
803 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
804 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
805 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
806 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
807 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
808 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
809 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
810 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
811 .vidioc_reqbufs = vidioc_reqbufs,
812 .vidioc_querybuf = vidioc_querybuf,
813 .vidioc_qbuf = vidioc_qbuf,
814 .vidioc_dqbuf = vidioc_dqbuf,
815 .vidioc_expbuf = vidioc_expbuf,
816 .vidioc_streamon = vidioc_streamon,
817 .vidioc_streamoff = vidioc_streamoff,
820 static int rockchip_vpu_queue_setup(struct vb2_queue *vq,
822 unsigned int *buf_count,
823 unsigned int *plane_count,
824 unsigned int psize[], void *allocators[])
826 struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
832 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
833 *plane_count = ctx->vpu_src_fmt->num_planes;
838 if (*buf_count > VIDEO_MAX_FRAME)
839 *buf_count = VIDEO_MAX_FRAME;
841 psize[0] = ctx->src_fmt.plane_fmt[0].sizeimage;
842 allocators[0] = ctx->dev->alloc_ctx;
843 vpu_debug(0, "output psize[%d]: %d\n", 0, psize[0]);
846 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
847 *plane_count = ctx->vpu_dst_fmt->num_planes;
852 if (*buf_count > VIDEO_MAX_FRAME)
853 *buf_count = VIDEO_MAX_FRAME;
855 psize[0] = round_up(ctx->dst_fmt.plane_fmt[0].sizeimage, 8);
856 allocators[0] = ctx->dev->alloc_ctx;
858 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE)
859 /* Add space for appended motion vectors. */
860 psize[0] += 64 * MB_WIDTH(ctx->dst_fmt.width)
861 * MB_HEIGHT(ctx->dst_fmt.height);
863 vpu_debug(0, "capture psize[%d]: %d\n", 0, psize[0]);
867 vpu_err("invalid queue type: %d\n", vq->type);
876 static int rockchip_vpu_buf_init(struct vb2_buffer *vb)
878 struct vb2_queue *vq = vb->vb2_queue;
879 struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
883 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
884 ctx->dst_bufs[vb->index] = vb;
891 static void rockchip_vpu_buf_cleanup(struct vb2_buffer *vb)
893 struct vb2_queue *vq = vb->vb2_queue;
894 struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
898 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
899 ctx->dst_bufs[vb->index] = NULL;
904 static int rockchip_vpu_buf_prepare(struct vb2_buffer *vb)
906 struct vb2_queue *vq = vb->vb2_queue;
907 struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
914 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
915 vpu_debug(4, "plane size: %ld, dst size: %d\n",
916 vb2_plane_size(vb, 0),
917 ctx->src_fmt.plane_fmt[0].sizeimage);
919 if (vb2_plane_size(vb, 0)
920 < ctx->src_fmt.plane_fmt[0].sizeimage) {
921 vpu_err("plane size is too small for output\n");
926 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
927 for (i = 0; i < ctx->vpu_dst_fmt->num_planes; ++i) {
928 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", i,
929 vb2_plane_size(vb, i),
930 ctx->dst_fmt.plane_fmt[i].sizeimage);
932 if (vb2_plane_size(vb, i)
933 < ctx->dst_fmt.plane_fmt[i].sizeimage) {
934 vpu_err("size of plane %d is too small for capture\n",
940 if (i != ctx->vpu_dst_fmt->num_planes)
945 vpu_err("invalid queue type: %d\n", vq->type);
954 static int rockchip_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
957 struct rockchip_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
958 struct rockchip_vpu_dev *dev = ctx->dev;
963 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
964 ret = rockchip_vpu_init(ctx);
966 vpu_err("rockchip_vpu_init failed\n");
970 ready = vb2_is_streaming(&ctx->vq_src);
971 } else if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
972 ready = vb2_is_streaming(&ctx->vq_dst);
976 rockchip_vpu_try_context(dev, ctx);
983 static void rockchip_vpu_stop_streaming(struct vb2_queue *q)
986 struct rockchip_vpu_ctx *ctx = fh_to_ctx(q->drv_priv);
987 struct rockchip_vpu_dev *dev = ctx->dev;
988 struct rockchip_vpu_buf *b;
994 spin_lock_irqsave(&dev->irqlock, flags);
996 list_del_init(&ctx->list);
999 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1000 list_splice_init(&ctx->dst_queue, &queue);
1003 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1004 list_splice_init(&ctx->src_queue, &queue);
1011 spin_unlock_irqrestore(&dev->irqlock, flags);
1013 wait_event(dev->run_wq, dev->current_ctx != ctx);
1015 while (!list_empty(&queue)) {
1016 b = list_first_entry(&queue, struct rockchip_vpu_buf, list);
1017 for (i = 0; i < b->vb.vb2_buf.num_planes; i++)
1018 vb2_set_plane_payload(&b->vb.vb2_buf, i, 0);
1019 vb2_buffer_done(&b->vb.vb2_buf, VB2_BUF_STATE_ERROR);
1023 if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1024 rockchip_vpu_deinit(ctx);
1029 static void rockchip_vpu_buf_queue(struct vb2_buffer *vb)
1031 struct vb2_queue *vq = vb->vb2_queue;
1032 struct rockchip_vpu_ctx *ctx = fh_to_ctx(vq->drv_priv);
1033 struct rockchip_vpu_dev *dev = ctx->dev;
1034 struct rockchip_vpu_buf *vpu_buf;
1035 unsigned long flags;
1040 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1041 vpu_buf = vb_to_buf(vb);
1043 /* Mark destination as available for use by VPU */
1044 spin_lock_irqsave(&dev->irqlock, flags);
1046 list_add_tail(&vpu_buf->list, &ctx->dst_queue);
1048 spin_unlock_irqrestore(&dev->irqlock, flags);
1051 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1052 vpu_buf = vb_to_buf(vb);
1054 spin_lock_irqsave(&dev->irqlock, flags);
1056 list_add_tail(&vpu_buf->list, &ctx->src_queue);
1058 spin_unlock_irqrestore(&dev->irqlock, flags);
1062 vpu_err("unsupported buffer type (%d)\n", vq->type);
1065 if (vb2_is_streaming(&ctx->vq_src) && vb2_is_streaming(&ctx->vq_dst))
1066 rockchip_vpu_try_context(dev, ctx);
1071 static struct vb2_ops rockchip_vpu_dec_qops = {
1072 .queue_setup = rockchip_vpu_queue_setup,
1073 .wait_prepare = vb2_ops_wait_prepare,
1074 .wait_finish = vb2_ops_wait_finish,
1075 .buf_init = rockchip_vpu_buf_init,
1076 .buf_prepare = rockchip_vpu_buf_prepare,
1077 .buf_cleanup = rockchip_vpu_buf_cleanup,
1078 .start_streaming = rockchip_vpu_start_streaming,
1079 .stop_streaming = rockchip_vpu_stop_streaming,
1080 .buf_queue = rockchip_vpu_buf_queue,
1083 struct vb2_ops *get_dec_queue_ops(void)
1085 return &rockchip_vpu_dec_qops;
1088 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1090 return &rockchip_vpu_dec_ioctl_ops;
1093 static void rockchip_vpu_dec_prepare_run(struct rockchip_vpu_ctx *ctx)
1095 struct vb2_v4l2_buffer *src = to_vb2_v4l2_buffer(&ctx->run.src->vb.vb2_buf);
1097 v4l2_ctrl_apply_store(&ctx->ctrl_handler, src->config_store);
1099 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE) {
1100 ctx->run.h264d.sps = get_ctrl_ptr(ctx,
1101 ROCKCHIP_VPU_DEC_CTRL_H264_SPS);
1102 ctx->run.h264d.pps = get_ctrl_ptr(ctx,
1103 ROCKCHIP_VPU_DEC_CTRL_H264_PPS);
1104 ctx->run.h264d.scaling_matrix = get_ctrl_ptr(ctx,
1105 ROCKCHIP_VPU_DEC_CTRL_H264_SCALING_MATRIX);
1106 ctx->run.h264d.slice_param = get_ctrl_ptr(ctx,
1107 ROCKCHIP_VPU_DEC_CTRL_H264_SLICE_PARAM);
1108 ctx->run.h264d.decode_param = get_ctrl_ptr(ctx,
1109 ROCKCHIP_VPU_DEC_CTRL_H264_DECODE_PARAM);
1110 } else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP8_FRAME) {
1111 ctx->run.vp8d.frame_hdr = get_ctrl_ptr(ctx,
1112 ROCKCHIP_VPU_DEC_CTRL_VP8_FRAME_HDR);
1116 static void rockchip_vpu_dec_run_done(struct rockchip_vpu_ctx *ctx,
1117 enum vb2_buffer_state result)
1119 struct v4l2_plane_pix_format *plane_fmts = ctx->dst_fmt.plane_fmt;
1120 struct vb2_buffer *dst = &ctx->run.dst->vb.vb2_buf;
1123 if (result != VB2_BUF_STATE_DONE) {
1124 /* Assume no payload after failed run. */
1125 for (i = 0; i < dst->num_planes; ++i)
1126 vb2_set_plane_payload(dst, i, 0);
1130 for (i = 0; i < dst->num_planes; ++i)
1131 vb2_set_plane_payload(dst, i, plane_fmts[i].sizeimage);
1134 static const struct rockchip_vpu_run_ops rockchip_vpu_dec_run_ops = {
1135 .prepare_run = rockchip_vpu_dec_prepare_run,
1136 .run_done = rockchip_vpu_dec_run_done,
1139 int rockchip_vpu_dec_init(struct rockchip_vpu_ctx *ctx)
1141 ctx->vpu_src_fmt = find_format(DEF_SRC_FMT_DEC, false);
1142 ctx->vpu_dst_fmt = find_format(DEF_DST_FMT_DEC, true);
1144 ctx->run_ops = &rockchip_vpu_dec_run_ops;
1146 return rockchip_vpu_ctrls_setup(ctx, &rockchip_vpu_dec_ctrl_ops,
1147 controls, ARRAY_SIZE(controls), NULL);
1150 void rockchip_vpu_dec_exit(struct rockchip_vpu_ctx *ctx)
1152 rockchip_vpu_ctrls_delete(ctx);