19 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/version.h>
23 #include <linux/videodev2.h>
34 #define DEF_SRC_FMT_ENC V4L2_PIX_FMT_NV12MT
35 #define DEF_DST_FMT_ENC V4L2_PIX_FMT_H264
39 .name =
"4:2:0 2 Planes 16x16 Tiles",
46 .name =
"4:2:0 2 Planes 64x32 Tiles",
53 .name =
"4:2:0 2 Planes Y/CbCr",
60 .name =
"4:2:0 2 Planes Y/CrCb",
67 .name =
"H264 Encoded Stream",
74 .name =
"MPEG4 Encoded Stream",
81 .name =
"H263 Encoded Stream",
89 #define NUM_FORMATS ARRAY_SIZE(formats)
107 .maximum = (1 << 16) - 1,
123 .maximum = (1 << 16) - 1,
131 .maximum = (1 << 30) - 1,
139 .maximum = (1 << 16) - 1,
146 .name =
"Padding Control Enable",
155 .name =
"Padding Color YUV Value",
157 .maximum = (1 << 25) - 1,
173 .maximum = (1 << 30) - 1,
180 .name =
"Rate Control Reaction Coeff.",
182 .maximum = (1 << 16) - 1,
189 .name =
"Force frame type",
199 .maximum = (1 << 16) - 1,
207 .maximum = (1 << 16) - 1,
222 .name =
"Frame Skip Enable",
231 .name =
"Fixed Target Bit Enable",
307 .name =
"The Number of Ref. Pic for P",
372 .name =
"H263 I-Frame QP value",
381 .name =
"H263 Minimum QP value",
390 .name =
"H263 Maximum QP value",
399 .name =
"H263 P frame QP value",
408 .name =
"H263 B frame QP value",
417 .name =
"MPEG4 I-Frame QP value",
426 .name =
"MPEG4 Minimum QP value",
435 .name =
"MPEG4 Maximum QP value",
444 .name =
"MPEG4 P frame QP value",
453 .name =
"MPEG4 B frame QP value",
462 .name =
"H264 Dark Reg Adaptive RC",
471 .name =
"H264 Smooth Reg Adaptive RC",
480 .name =
"H264 Static Reg Adaptive RC",
489 .name =
"H264 Activity Reg Adaptive RC",
515 .maximum = (1 << 16) - 1,
523 .maximum = (1 << 16) - 1,
539 .maximum = (1 << 16) - 1,
561 #define NUM_CTRLS ARRAY_SIZE(controls)
562 static const char *
const *mfc51_get_menu(
u32 id)
564 static const char *
const mfc51_video_frame_skip[] = {
570 static const char *
const mfc51_video_force_frame[] = {
578 return mfc51_video_frame_skip;
580 return mfc51_video_force_frame;
587 mfc_debug(2,
"src=%d, dst=%d, state=%d\n",
605 static void cleanup_ref_queue(
struct s5p_mfc_ctx *ctx)
608 unsigned long mb_y_addr, mb_c_addr;
614 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 0);
615 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 1);
621 mfc_debug(2,
"enc src count: %d, enc ref count: %d\n",
627 static int enc_pre_seq_start(
struct s5p_mfc_ctx *ctx)
637 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
638 dst_size = vb2_plane_size(dst_mb->
b, 0);
641 spin_unlock_irqrestore(&dev->
irqlock, flags);
645 static int enc_post_seq_start(
struct s5p_mfc_ctx *ctx)
658 vb2_set_plane_payload(dst_mb->
b, 0,
661 spin_unlock_irqrestore(&dev->
irqlock, flags);
667 if (s5p_mfc_ctx_ready(ctx))
674 get_enc_dpb_count, dev);
679 static int enc_pre_frame_start(
struct s5p_mfc_ctx *ctx)
685 unsigned long src_y_addr, src_c_addr,
dst_addr;
690 src_y_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b, 0);
691 src_c_addr = vb2_dma_contig_plane_dma_addr(src_mb->
b, 1);
694 spin_unlock_irqrestore(&dev->
irqlock, flags);
698 dst_addr = vb2_dma_contig_plane_dma_addr(dst_mb->
b, 0);
699 dst_size = vb2_plane_size(dst_mb->
b, 0);
702 spin_unlock_irqrestore(&dev->
irqlock, flags);
707 static int enc_post_frame_start(
struct s5p_mfc_ctx *ctx)
711 unsigned long enc_y_addr, enc_c_addr;
712 unsigned long mb_y_addr, mb_c_addr;
714 unsigned int strm_size;
719 mfc_debug(2,
"Encoded slice type: %d", slice_type);
720 mfc_debug(2,
"Encoded stream size: %d", strm_size);
724 if (slice_type >= 0) {
726 &enc_y_addr, &enc_c_addr);
728 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 0);
729 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 1);
730 if ((enc_y_addr == mb_y_addr) &&
731 (enc_c_addr == mb_c_addr)) {
740 mb_y_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 0);
741 mb_c_addr = vb2_dma_contig_plane_dma_addr(mb_entry->
b, 1);
742 if ((enc_y_addr == mb_y_addr) &&
743 (enc_c_addr == mb_c_addr)) {
761 mfc_debug(2,
"enc src count: %d, enc ref count: %d\n",
770 switch (slice_type) {
781 vb2_set_plane_payload(mb_entry->
b, 0, strm_size);
784 spin_unlock_irqrestore(&dev->
irqlock, flags);
791 .pre_seq_start = enc_pre_seq_start,
792 .post_seq_start = enc_post_seq_start,
793 .pre_frame_start = enc_pre_frame_start,
794 .post_frame_start = enc_post_frame_start,
798 static int vidioc_querycap(
struct file *
file,
void *
priv,
806 cap->
version = KERNEL_VERSION(1, 0, 0);
826 else if (!mplane && formats[i].
num_planes > 1)
844 static int vidioc_enum_fmt_vid_cap(
struct file *
file,
void *pirv,
847 return vidioc_enum_fmt(f,
false,
false);
850 static int vidioc_enum_fmt_vid_cap_mplane(
struct file *
file,
void *pirv,
853 return vidioc_enum_fmt(f,
true,
false);
856 static int vidioc_enum_fmt_vid_out(
struct file *
file,
void *prov,
859 return vidioc_enum_fmt(f,
false,
true);
862 static int vidioc_enum_fmt_vid_out_mplane(
struct file *
file,
void *prov,
865 return vidioc_enum_fmt(f,
true,
true);
876 pix_fmt_mp->
width = 0;
904 static int vidioc_try_fmt(
struct file *file,
void *priv,
struct v4l2_format *f)
912 mfc_err(
"failed to try output format\n");
916 if (pix_fmt_mp->
plane_fmt[0].sizeimage == 0) {
917 mfc_err(
"must be set encoding output size\n");
926 mfc_err(
"failed to try output format\n");
931 mfc_err(
"failed to try output format\n");
935 &pix_fmt_mp->
height, 4, 1080, 1, 0);
943 static int vidioc_s_fmt(
struct file *file,
void *priv,
struct v4l2_format *f)
951 ret = vidioc_try_fmt(file, priv, f);
962 mfc_err(
"failed to set capture format\n");
969 pix_fmt_mp->
plane_fmt[0].bytesperline = 0;
979 mfc_err(
"Error getting instance from hardware\n");
989 mfc_err(
"failed to set output format\n");
995 mfc_err(
"Not supported format.\n");
999 mfc_err(
"Not supported format.\n");
1004 mfc_err(
"failed to set output format\n");
1012 mfc_debug(2,
"fmt - w: %d, h: %d, ctx - w: %d, h: %d\n",
1025 mfc_err(
"invalid buf type\n");
1033 static int vidioc_reqbufs(
struct file *file,
void *priv,
1046 mfc_err(
"invalid capture state: %d\n",
1052 mfc_err(
"error in vb2_reqbufs() for E(D)\n");
1059 alloc_codec_buffers, ctx);
1061 mfc_err(
"Failed to allocate encoding buffers\n");
1069 mfc_err(
"invalid output state: %d\n",
1075 mfc_err(
"error in vb2_reqbufs() for E(S)\n");
1080 mfc_err(
"invalid buf type\n");
1086 static int vidioc_querybuf(
struct file *file,
void *priv,
1103 mfc_err(
"error in vb2_querybuf() for E(D)\n");
1110 mfc_err(
"error in vb2_querybuf() for E(S)\n");
1114 mfc_err(
"invalid buf type\n");
1121 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1126 mfc_err(
"Call on QBUF after unrecoverable error\n");
1131 mfc_err(
"Call on QBUF after EOS command\n");
1142 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
1151 mfc_err(
"Call on DQBUF after unrecoverable error\n");
1159 && list_empty(&ctx->
vq_dst.done_list))
1169 static int vidioc_streamon(
struct file *file,
void *priv,
1182 static int vidioc_streamoff(
struct file *file,
void *priv,
1315 switch (ctrl->
val) {
1343 mfc_err(
"Level number is wrong\n");
1351 mfc_err(
"Level number is wrong\n");
1442 switch (ctrl->
val) {
1460 ctrl->
id, ctrl->
val);
1467 .s_ctrl = s5p_mfc_enc_s_ctrl,
1470 static int vidioc_s_parm(
struct file *file,
void *priv,
1481 mfc_err(
"Setting FPS is only possible for the output queue\n");
1487 static int vidioc_g_parm(
struct file *file,
void *priv,
1498 mfc_err(
"Setting FPS is only possible for the output queue\n");
1510 unsigned long flags;
1514 if (cmd->
flags != 0)
1517 if (!ctx->
vq_src.streaming)
1522 mfc_debug(2,
"EOS: empty src queue, entering finishing state");
1524 spin_unlock_irqrestore(&dev->
irqlock, flags);
1527 mfc_debug(2,
"EOS: marking last buffer of stream");
1534 spin_unlock_irqrestore(&dev->
irqlock, flags);
1544 static int vidioc_subscribe_event(
struct v4l2_fh *fh,
1547 switch (sub->
type) {
1561 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
1562 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
1563 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
1564 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
1565 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
1566 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
1587 mfc_err(
"invalid plane number for the format\n");
1591 if (!vb2_dma_contig_plane_dma_addr(vb, i)) {
1592 mfc_err(
"failed to get plane cookie\n");
1595 mfc_debug(2,
"index: %d, plane[%d] cookie: 0x%08zx",
1597 vb2_dma_contig_plane_dma_addr(vb, i));
1602 static int s5p_mfc_queue_setup(
struct vb2_queue *vq,
1604 unsigned int *buf_count,
unsigned int *plane_count,
1605 unsigned int psize[],
void *allocators[])
1616 *plane_count = ctx->
dst_fmt->num_planes;
1627 *plane_count = ctx->
src_fmt->num_planes;
1655 static void s5p_mfc_unlock(
struct vb2_queue *
q)
1663 static void s5p_mfc_lock(
struct vb2_queue *
q)
1671 static int s5p_mfc_buf_init(
struct vb2_buffer *vb)
1679 ret = check_vb_with_fmt(ctx->
dst_fmt, vb);
1685 vb2_dma_contig_plane_dma_addr(vb, 0);
1688 ret = check_vb_with_fmt(ctx->
src_fmt, vb);
1694 vb2_dma_contig_plane_dma_addr(vb, 0);
1696 vb2_dma_contig_plane_dma_addr(vb, 1);
1705 static int s5p_mfc_buf_prepare(
struct vb2_buffer *vb)
1712 ret = check_vb_with_fmt(ctx->
dst_fmt, vb);
1715 mfc_debug(2,
"plane size: %ld, dst size: %d\n",
1718 mfc_err(
"plane size is too small for capture\n");
1722 ret = check_vb_with_fmt(ctx->
src_fmt, vb);
1725 mfc_debug(2,
"plane size: %ld, luma size: %d\n",
1727 mfc_debug(2,
"plane size: %ld, chroma size: %d\n",
1729 if (vb2_plane_size(vb, 0) < ctx->
luma_size ||
1731 mfc_err(
"plane size is too small for output\n");
1741 static int s5p_mfc_start_streaming(
struct vb2_queue *
q,
unsigned int count)
1748 if (s5p_mfc_ctx_ready(ctx))
1754 static int s5p_mfc_stop_streaming(
struct vb2_queue *q)
1756 unsigned long flags;
1776 cleanup_ref_queue(ctx);
1782 spin_unlock_irqrestore(&dev->
irqlock, flags);
1786 static void s5p_mfc_buf_queue(
struct vb2_buffer *vb)
1791 unsigned long flags;
1796 cleanup_ref_queue(ctx);
1806 spin_unlock_irqrestore(&dev->
irqlock, flags);
1813 spin_unlock_irqrestore(&dev->
irqlock, flags);
1815 mfc_err(
"unsupported buffer type (%d)\n", vq->
type);
1817 if (s5p_mfc_ctx_ready(ctx))
1822 static struct vb2_ops s5p_mfc_enc_qops = {
1823 .queue_setup = s5p_mfc_queue_setup,
1824 .wait_prepare = s5p_mfc_unlock,
1825 .wait_finish = s5p_mfc_lock,
1826 .buf_init = s5p_mfc_buf_init,
1827 .buf_prepare = s5p_mfc_buf_prepare,
1828 .start_streaming = s5p_mfc_start_streaming,
1829 .stop_streaming = s5p_mfc_stop_streaming,
1830 .buf_queue = s5p_mfc_buf_queue,
1835 return &encoder_codec_ops;
1840 return &s5p_mfc_enc_qops;
1845 return &s5p_mfc_enc_ioctl_ops;
1848 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1849 && V4L2_CTRL_DRIVER_PRIV(x))
1858 mfc_err(
"v4l2_ctrl_handler_init failed\n");
1864 cfg.
ops = &s5p_mfc_enc_ctrl_ops;
1865 cfg.
id = controls[
i].
id;
1876 cfg.
qmenu = mfc51_get_menu(cfg.
id);
1887 &s5p_mfc_enc_ctrl_ops, controls[i].
id,
1893 &s5p_mfc_enc_ctrl_ops, controls[i].
id,
1900 mfc_err(
"Adding control (%d) failed\n", i);
1903 if (controls[i].is_volatile && ctx->
ctrls[i])