1 #include <sys/ioctl.h>
2 #include <sys/mman.h>
3 #include <linux/videodev2.h>
4 
5 #include "libscaler-v4l2.h"
6 #include "libscalerblend-v4l2.h"
7 
DevSetCtrl()8 bool CScalerBlendV4L2::DevSetCtrl()
9 {
10     struct v4l2_control ctrl;
11 
12     if (!SetCtrl())
13         return false;
14 
15     /* Blending related ctls */
16     if (!TestFlag(m_fStatus, SCF_SRC_BLEND))
17         return false;
18 
19     ctrl.id = V4L2_CID_2D_BLEND_OP;
20     ctrl.value = m_SrcBlndCfg.blop;
21     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
22         SC_LOGERR("Failed S_CTRL V4L2_CID_2D_BLEND_OP");
23         return false;
24     }
25 
26     if (m_SrcBlndCfg.globalalpha.enable) {
27         ctrl.id = V4L2_CID_GLOBAL_ALPHA;
28         ctrl.value = m_SrcBlndCfg.globalalpha.val;
29         if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
30                SC_LOGERR("Failed S_CTRL V4L2_CID_GLOBAL_ALPHA");
31                return false;
32         }
33     } else {
34         ctrl.id = V4L2_CID_GLOBAL_ALPHA;
35         ctrl.value = 0xff;
36         if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
37                 SC_LOGERR("Failed S_CTRL V4L2_CID_GLOBAL_ALPHA 0xff");
38                 return false;
39         }
40     }
41 
42     if (m_SrcBlndCfg.cscspec.enable) {
43         bool is_bt709 = (m_SrcBlndCfg.cscspec.space == COLORSPACE_REC709)? true : false;
44 
45         ctrl.id = V4L2_CID_CSC_EQ;
46         ctrl.value = is_bt709;
47         if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
48             SC_LOGERR("Failed S_CTRL V4L2_CID_CSC_EQ - %d",
49                                                    m_SrcBlndCfg.cscspec.space);
50             return false;
51         }
52 
53         ctrl.id = V4L2_CID_CSC_RANGE;
54         ctrl.value = m_SrcBlndCfg.cscspec.wide;
55         if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
56             SC_LOGERR("Failed S_CTRL V4L2_CID_CSC_RANGE - %d",
57                                                    m_SrcBlndCfg.cscspec.wide);
58             return false;
59         }
60     }
61 
62     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_FMT;
63     ctrl.value = m_SrcBlndCfg.srcblendfmt;
64     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
65         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_FMT - %d",
66                                                   m_SrcBlndCfg.srcblendfmt);
67         return false;
68     }
69 
70     ctrl.id = V4L2_CID_2D_SRC_BLEND_FMT_PREMULTI;
71     ctrl.value = m_SrcBlndCfg.srcblendpremulti;
72     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
73         SC_LOGERR("Failed V4L2_CID_2D_BLEND_FMT_PREMULTI - %d",
74                                                    m_SrcBlndCfg.srcblendpremulti);
75         return false;
76     }
77 
78     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_STRIDE;
79     ctrl.value = m_SrcBlndCfg.srcblendstride;
80     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
81         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_STRIDE - %d",
82                                                    m_SrcBlndCfg.srcblendstride);
83         return false;
84     }
85 
86     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_H_POS;
87     ctrl.value = m_SrcBlndCfg.srcblendhpos;
88     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
89         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_H_POS with degree %d",
90                                                    m_SrcBlndCfg.srcblendhpos);
91         return false;
92     }
93 
94     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_V_POS;
95     ctrl.value = m_SrcBlndCfg.srcblendvpos;
96     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
97         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_V_POS - %d",
98                                                    m_SrcBlndCfg.srcblendvpos);
99         return false;
100     }
101 
102     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_WIDTH;
103     ctrl.value = m_SrcBlndCfg.srcblendwidth;
104     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
105         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_WIDTH with degree %d",
106                                                    m_SrcBlndCfg.srcblendwidth);
107         return false;
108     }
109 
110     ctrl.id = V4L2_CID_2D_SRC_BLEND_SET_HEIGHT;
111     ctrl.value = m_SrcBlndCfg.srcblendheight;
112     if (ioctl(m_fdScaler, VIDIOC_S_CTRL, &ctrl) < 0) {
113         SC_LOGERR("Failed V4L2_CID_2D_SRC_BLEND_SET_HEIGHT - %d",
114                                                    m_SrcBlndCfg.srcblendheight);
115         return false;
116     }
117 
118     ClearFlag(m_fStatus, SCF_SRC_BLEND);
119     return true;
120 }
121 
GetCustomAlphaBlendFmt(int32_t & src_color_space,unsigned int srcblendfmt)122 void CScalerBlendV4L2::GetCustomAlphaBlendFmt(int32_t &src_color_space,
123                                                      unsigned int srcblendfmt) {
124 
125     if (src_color_space == V4L2_PIX_FMT_NV12M) {
126         if (srcblendfmt == V4L2_PIX_FMT_RGB32)
127             src_color_space = V4L2_PIX_FMT_NV12M_RGB32;
128         else if (srcblendfmt == V4L2_PIX_FMT_BGR32)
129             src_color_space = V4L2_PIX_FMT_NV12M_BGR32;
130         else if (srcblendfmt == V4L2_PIX_FMT_RGB565)
131             src_color_space = V4L2_PIX_FMT_NV12M_RGB565;
132         else if (srcblendfmt == V4L2_PIX_FMT_RGB444)
133             src_color_space = V4L2_PIX_FMT_NV12M_RGB444;
134         else if (srcblendfmt == V4L2_PIX_FMT_RGB555X)
135             src_color_space = V4L2_PIX_FMT_NV12M_RGB555X;
136     } else if (src_color_space == V4L2_PIX_FMT_NV12) {
137             if (srcblendfmt == V4L2_PIX_FMT_RGB32)
138             src_color_space = V4L2_PIX_FMT_NV12_RGB32;
139     } else if (src_color_space == V4L2_PIX_FMT_NV12N) {
140             if (srcblendfmt == V4L2_PIX_FMT_RGB32)
141             src_color_space = V4L2_PIX_FMT_NV12N_RGB32;
142     } else if (src_color_space == V4L2_PIX_FMT_NV12MT_16X16) {
143             if (srcblendfmt == V4L2_PIX_FMT_RGB32)
144             src_color_space = V4L2_PIX_FMT_NV12MT_16X16_RGB32;
145     } else if (src_color_space == V4L2_PIX_FMT_NV21M) {
146         if (srcblendfmt == V4L2_PIX_FMT_RGB32)
147             src_color_space = V4L2_PIX_FMT_NV21M_RGB32;
148         else if (srcblendfmt == V4L2_PIX_FMT_BGR32)
149             src_color_space = V4L2_PIX_FMT_NV21M_BGR32;
150     } else if (src_color_space == V4L2_PIX_FMT_NV21) {
151         if (srcblendfmt == V4L2_PIX_FMT_RGB32)
152             src_color_space = V4L2_PIX_FMT_NV21_RGB32;
153     } else if (src_color_space == V4L2_PIX_FMT_YVU420) {
154         if (srcblendfmt == V4L2_PIX_FMT_RGB32)
155             src_color_space = V4L2_PIX_FMT_YVU420_RGB32;
156     } else {
157         src_color_space = -1;
158     }
159 }
160 
CScalerBlendV4L2(int dev_num,int allow_drm)161 CScalerBlendV4L2::CScalerBlendV4L2(int dev_num, int allow_drm) : CScalerV4L2(dev_num, allow_drm){
162 
163 }
164 
~CScalerBlendV4L2()165 CScalerBlendV4L2::~CScalerBlendV4L2(){
166 
167 }
168