1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2016 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <linux/videodev2.h>
19 
20 #include <log/log.h>
21 #include <system/graphics.h>
22 
23 #include <exynos_format.h> // hardware/smasung_slsi/exynos/include
24 
25 #include "acrylic_internal.h"
26 
27 #define V4L2_PIX_FMT_NV12N             v4l2_fourcc('N', 'N', '1', '2')
28 #define V4L2_PIX_FMT_NV12N_10B         v4l2_fourcc('B', 'N', '1', '2')
29 #define V4L2_PIX_FMT_YUV420N           v4l2_fourcc('Y', 'N', '1', '2')
30 #define V4L2_PIX_FMT_NV12M_S10B        v4l2_fourcc('B', 'M', '1', '2')
31 #define V4L2_PIX_FMT_NV21M_S10B        v4l2_fourcc('B', 'M', '2', '1')
32 #define V4L2_PIX_FMT_NV16M_S10B        v4l2_fourcc('B', 'M', '1', '6')
33 #define V4L2_PIX_FMT_NV61M_S10B        v4l2_fourcc('B', 'M', '6', '1')
34 #define V4L2_PIX_FMT_NV12M_P010        v4l2_fourcc('P', 'M', '1', '2')
35 #define V4L2_PIX_FMT_NV21M_P010        v4l2_fourcc('P', 'M', '2', '1')
36 #define V4L2_PIX_FMT_NV16M_P210        v4l2_fourcc('P', 'M', '1', '6')
37 #define V4L2_PIX_FMT_NV61M_P210        v4l2_fourcc('P', 'M', '6', '1')
38 #define V4L2_PIX_FMT_NV12_P010         v4l2_fourcc('P', 'N', '1', '2')
39 #define V4L2_PIX_FMT_ARGB2101010       v4l2_fourcc('A', 'B', '1', '0')
40 #define V4L2_PIX_FMT_ABGR2101010       v4l2_fourcc('A', 'R', '1', '0')
41 #define V4L2_PIX_FMT_RGBA1010102       v4l2_fourcc('R', 'A', '1', '0')
42 #define V4L2_PIX_FMT_BGRA1010102       v4l2_fourcc('B', 'A', '1', '0')
43 
44 /* 12 Y/CbCr 4:2:0 SBWC */
45 #define V4L2_PIX_FMT_NV12M_SBWC_8B     v4l2_fourcc('M', '1', 'S', '8')
46 #define V4L2_PIX_FMT_NV12M_SBWC_10B    v4l2_fourcc('M', '1', 'S', '1')
47 /* 21 Y/CrCb 4:2:0 SBWC */
48 #define V4L2_PIX_FMT_NV21M_SBWC_8B     v4l2_fourcc('M', '2', 'S', '8')
49 #define V4L2_PIX_FMT_NV21M_SBWC_10B    v4l2_fourcc('M', '2', 'S', '1')
50 /* 12 Y/CbCr 4:2:0 SBWC single */
51 #define V4L2_PIX_FMT_NV12N_SBWC_8B     v4l2_fourcc('N', '1', 'S', '8')
52 #define V4L2_PIX_FMT_NV12N_SBWC_10B    v4l2_fourcc('N', '1', 'S', '1')
53 /* 12 Y/CbCr 4:2:0 SBWC Lossy */
54 #define V4L2_PIX_FMT_NV12M_SBWCL_8B    v4l2_fourcc('M', '1', 'L', '8')
55 #define V4L2_PIX_FMT_NV12M_SBWCL_10B   v4l2_fourcc('M', '1', 'L', '1')
56 /* 12 Y/CbCr 4:2:0 SBWC Lossy single */
57 #define V4L2_PIX_FMT_NV12N_SBWCL_8B    v4l2_fourcc('N', '1', 'L', '8')
58 #define V4L2_PIX_FMT_NV12N_SBWCL_10B   v4l2_fourcc('N', '1', 'L', '1')
59 
60 
61 static uint32_t __halfmt_to_v4l2_rgb[][2] = {
62     {HAL_PIXEL_FORMAT_RGBA_8888,                    V4L2_PIX_FMT_ABGR32   },
63     {HAL_PIXEL_FORMAT_BGRA_8888,                    V4L2_PIX_FMT_ARGB32   },
64     {HAL_PIXEL_FORMAT_RGBX_8888,                    V4L2_PIX_FMT_XBGR32   },
65     {HAL_PIXEL_FORMAT_RGB_888,                      V4L2_PIX_FMT_RGB24    },
66     {HAL_PIXEL_FORMAT_RGB_565,                      V4L2_PIX_FMT_RGB565   },
67 };
68 
69 // The V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_BGR32 are deprecated in V4L2.
70 // But the legacy mscl driver and libhwcutils requires them.
71 static uint32_t __halfmt_to_v4l2_rgb_deprecated[][2] = {
72     {HAL_PIXEL_FORMAT_RGBA_8888,                    V4L2_PIX_FMT_RGB32       },
73     {HAL_PIXEL_FORMAT_BGRA_8888,                    V4L2_PIX_FMT_BGR32       },
74     {HAL_PIXEL_FORMAT_RGBX_8888,                    V4L2_PIX_FMT_RGB32       },
75     {HAL_PIXEL_FORMAT_RGB_888,                      V4L2_PIX_FMT_RGB24       },
76     {HAL_PIXEL_FORMAT_RGB_565,                      V4L2_PIX_FMT_RGB565      },
77     {HAL_PIXEL_FORMAT_RGBA_1010102,                 V4L2_PIX_FMT_ABGR2101010 },
78 };
79 
80 static uint32_t __halfmt_to_v4l2_ycbcr[][2] = {
81     {HAL_PIXEL_FORMAT_YV12,                           V4L2_PIX_FMT_YVU420         },
82     {HAL_PIXEL_FORMAT_EXYNOS_YV12_M,                  V4L2_PIX_FMT_YVU420M        },
83     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,             V4L2_PIX_FMT_YUV420         },
84     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN,            V4L2_PIX_FMT_YUV420N        },
85     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M,           V4L2_PIX_FMT_YUV420M        },
86     {HAL_PIXEL_FORMAT_YCrCb_420_SP,                   V4L2_PIX_FMT_NV21           },
87     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M,          V4L2_PIX_FMT_NV21M          },
88     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL,     V4L2_PIX_FMT_NV21M          },
89     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,            V4L2_PIX_FMT_NV12           },
90     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN,           V4L2_PIX_FMT_NV12N          },
91     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M,          V4L2_PIX_FMT_NV12M          },
92     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV,     V4L2_PIX_FMT_NV12M          },
93     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B,      V4L2_PIX_FMT_NV12N_10B      },
94     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B,     V4L2_PIX_FMT_NV12M_S10B     },
95     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M,            V4L2_PIX_FMT_NV12M_P010     },
96     {HAL_PIXEL_FORMAT_YCBCR_P010,                     V4L2_PIX_FMT_NV12_P010      },
97     {HAL_PIXEL_FORMAT_YCbCr_422_I,                    V4L2_PIX_FMT_YUYV           },
98     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I,             V4L2_PIX_FMT_YVYU           },
99     {HAL_PIXEL_FORMAT_YCbCr_422_SP,                   V4L2_PIX_FMT_NV16           },
100     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC,     V4L2_PIX_FMT_NV12M_SBWC_8B  },
101     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC, V4L2_PIX_FMT_NV12M_SBWC_10B },
102     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_SBWC,     V4L2_PIX_FMT_NV21M_SBWC_8B  },
103     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_10B_SBWC, V4L2_PIX_FMT_NV21M_SBWC_10B },
104     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,      V4L2_PIX_FMT_NV12N_SBWC_8B  },
105     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC,  V4L2_PIX_FMT_NV12N_SBWC_10B },
106 };
107 
halfmt_to_v4l2_ycbcr(uint32_t halfmt)108 static uint32_t halfmt_to_v4l2_ycbcr(uint32_t halfmt)
109 {
110     for (size_t i = 0 ; i < ARRSIZE(__halfmt_to_v4l2_ycbcr); i++) {
111         if (__halfmt_to_v4l2_ycbcr[i][0] == halfmt)
112             return __halfmt_to_v4l2_ycbcr[i][1];
113     }
114 
115     ALOGE("Unable to find the proper v4l2 format for HAL format %#x", halfmt);
116 
117     return 0; // it is alright to return 0 for an error because a fmt identifier is 4cc value
118 }
119 
halfmt_to_v4l2(uint32_t halfmt)120 uint32_t halfmt_to_v4l2(uint32_t halfmt)
121 {
122     for (size_t i = 0 ; i < ARRSIZE(__halfmt_to_v4l2_rgb); i++) {
123         if (__halfmt_to_v4l2_rgb[i][0] == halfmt)
124             return __halfmt_to_v4l2_rgb[i][1];
125     }
126 
127     return halfmt_to_v4l2_ycbcr(halfmt);
128 }
129 
halfmt_to_v4l2_deprecated(uint32_t halfmt)130 uint32_t halfmt_to_v4l2_deprecated(uint32_t halfmt)
131 {
132     for (size_t i = 0 ; i < ARRSIZE(__halfmt_to_v4l2_rgb_deprecated); i++) {
133         if (__halfmt_to_v4l2_rgb_deprecated[i][0] == halfmt)
134             return __halfmt_to_v4l2_rgb_deprecated[i][1];
135     }
136 
137     return halfmt_to_v4l2_ycbcr(halfmt);
138 }
139 
140 
141 static struct {
142     uint32_t fmt;                   // HAL_PIXEL_FORMAT that describe how pixels are stored in memory
143     uint8_t  bufcnt;                // the number of buffer to describe @fmt
144     uint8_t  subfactor;             // Horizontal (upper 4 bits)and vertical (lower 4 bits) chroma subsampling factor
145     uint8_t  bpp[MAX_HW2D_PLANES];  // bits in a buffer per pixel
146     uint32_t equivalent;            // The equivalent format on a single buffer without H/W constraints
147     uint8_t  planecnt;              // the number of planes to describe @fmt
148 } __halfmt_plane_bpp[] = {
149     {HAL_PIXEL_FORMAT_RGBA_8888,                        1, 0x11, {32, 0, 0, 0}, HAL_PIXEL_FORMAT_RGBA_8888,                     1},
150     {HAL_PIXEL_FORMAT_BGRA_8888,                        1, 0x11, {32, 0, 0, 0}, HAL_PIXEL_FORMAT_BGRA_8888,                     1},
151     {HAL_PIXEL_FORMAT_RGBA_1010102,                     1, 0x11, {32, 0, 0, 0}, HAL_PIXEL_FORMAT_RGBA_1010102,                  1},
152     {HAL_PIXEL_FORMAT_RGBX_8888,                        1, 0x11, {32, 0, 0, 0}, HAL_PIXEL_FORMAT_RGBX_8888,                     1},
153     {HAL_PIXEL_FORMAT_RGB_888,                          1, 0x11, {24, 0, 0, 0}, HAL_PIXEL_FORMAT_RGB_888,                       1},
154     {HAL_PIXEL_FORMAT_RGB_565,                          1, 0x11, {16, 0, 0, 0}, HAL_PIXEL_FORMAT_RGB_565,                       1},
155     {HAL_PIXEL_FORMAT_YCbCr_422_I,                      1, 0x21, {16, 0, 0, 0}, HAL_PIXEL_FORMAT_YCbCr_422_I,                   1},
156     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I,               1, 0x21, {16, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I,            1},
157     {HAL_PIXEL_FORMAT_YCbCr_422_SP,                     1, 0x21, {16, 0, 0, 0}, HAL_PIXEL_FORMAT_YCbCr_422_SP,                  2},
158     {HAL_PIXEL_FORMAT_YV12,                             1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_YV12,                          2},
159     {HAL_PIXEL_FORMAT_EXYNOS_YV12_M,                    3, 0x22, { 8, 2, 2, 0}, HAL_PIXEL_FORMAT_YV12,                          2},
160     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,               1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,            2},
161     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN,              1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,            2},
162     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M,             3, 0x22, { 8, 2, 2, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P,            2},
163     {HAL_PIXEL_FORMAT_YCrCb_420_SP,                     1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_YCrCb_420_SP,                  2},
164     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M,            2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_YCrCb_420_SP,                  2},
165     {HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL,       2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_YCrCb_420_SP,                  2},
166     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,              1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
167     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN,             1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
168     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED,       1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
169     {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP,                   1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
170     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M,            2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
171     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV,       2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
172     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED,      2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP,           2},
173     {HAL_PIXEL_FORMAT_YCBCR_P010,                       1, 0x22, {24, 0, 0, 0}, HAL_PIXEL_FORMAT_YCBCR_P010,                    2},
174     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M,              2, 0x22, {16, 8, 0, 0}, HAL_PIXEL_FORMAT_YCBCR_P010,                    2},
175     {HAL_PIXEL_FORMAT_GOOGLE_NV12_SP_10B,               1, 0x22, {24, 0, 0, 0}, HAL_PIXEL_FORMAT_YCBCR_P010,                    2},
176     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_SBWC,       2, 0x22, { 8, 4, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,     2},
177     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,        1, 0x22, {12, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_SBWC,     2},
178     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_10B_SBWC,   2, 0x22, {16, 8, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC, 2},
179     {HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC,    1, 0x22, {24, 0, 0, 0}, HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_10B_SBWC, 2},
180 };
181 
182 #define MFC_PAD_SIZE                256
183 #define MFC_2B_PAD_SIZE             (MFC_PAD_SIZE / 4)
184 #define MFC_ALIGN(v)                (((v) + 15) & ~15)
185 
186 #define NV12_MFC_Y_PAYLOAD(w, h)    (MFC_ALIGN(w) * MFC_ALIGN(h))
187 #define NV12_MFC_C_PAYLOAD(w, h)    (MFC_ALIGN(w) * MFC_ALIGN(h) / 2)
188 #define NV12_MFC_PAYLOAD(w, h)      (NV12_MFC_Y_PAYLOAD(w, h) + MFC_PAD_SIZE + (MFC_ALIGN(w) * (h) / 2))
189 
halfmt_plane_length(uint32_t fmt,unsigned int plane,uint32_t width,uint32_t height)190 size_t halfmt_plane_length(uint32_t fmt, unsigned int plane, uint32_t width, uint32_t height)
191 {
192     for (size_t i = 0; i < ARRSIZE(__halfmt_plane_bpp); i++) {
193         if (__halfmt_plane_bpp[i].fmt == fmt) {
194             LOGASSERT(plane < __halfmt_plane_bpp[i].bufcnt,
195                       "Plane count of HAL format %#x is %u but %d plane is requested", fmt,
196                       __halfmt_plane_bpp[i].bufcnt, plane);
197             if (plane < __halfmt_plane_bpp[i].bufcnt)
198                 return (__halfmt_plane_bpp[i].bpp[plane] * width * height) / 8;
199         }
200     }
201 
202     LOGASSERT(1, "Unable to find HAL format %#x with plane %d", fmt, plane);
203 
204     return 0;
205 }
206 
halfmt_bpp(uint32_t fmt)207 unsigned int halfmt_bpp(uint32_t fmt)
208 {
209     for (size_t i = 0 ; i < ARRSIZE(__halfmt_plane_bpp); i++) {
210         if (__halfmt_plane_bpp[i].fmt == fmt)
211                 return __halfmt_plane_bpp[i].bpp[0] + __halfmt_plane_bpp[i].bpp[1] + __halfmt_plane_bpp[i].bpp[2];
212     }
213 
214     LOGASSERT(1, "Unable to find HAL format %#x", fmt);
215 
216     return 0;
217 }
218 
219 #define DEFINE_HALFMT_PROPERTY_GETTER(rettype, funcname, member)    \
220     rettype funcname(uint32_t fmt)                                  \
221     {                                                               \
222         for (size_t i = 0 ; i < ARRSIZE(__halfmt_plane_bpp); i++) { \
223             if (__halfmt_plane_bpp[i].fmt == fmt)                   \
224                 return __halfmt_plane_bpp[i].member;                \
225         }                                                           \
226         LOGASSERT(1, "Unable to find HAL format %#x", fmt);         \
227         return 0;                                                   \
228     }
229 
230 DEFINE_HALFMT_PROPERTY_GETTER(unsigned int, halfmt_buf_count, bufcnt)
231 DEFINE_HALFMT_PROPERTY_GETTER(uint8_t, halfmt_chroma_subsampling, subfactor)
232 DEFINE_HALFMT_PROPERTY_GETTER(uint32_t, find_format_equivalent, equivalent)
233 DEFINE_HALFMT_PROPERTY_GETTER(uint8_t, halfmt_plane_count, planecnt)
234 
235 static struct {
236     int32_t  hal;
237     uint32_t v4l2;
238 } __haldataspace_to_v4l2[] = {
239     {HAL_DATASPACE_STANDARD_BT709 | HAL_DATASPACE_RANGE_FULL,        V4L2_COLORSPACE_SRGB},
240     {HAL_DATASPACE_STANDARD_BT709 | HAL_DATASPACE_RANGE_LIMITED,     V4L2_COLORSPACE_REC709},
241     {HAL_DATASPACE_STANDARD_BT601_625 | HAL_DATASPACE_RANGE_FULL,    V4L2_COLORSPACE_JPEG},
242     {HAL_DATASPACE_STANDARD_BT601_525 | HAL_DATASPACE_RANGE_FULL,    V4L2_COLORSPACE_JPEG},
243     {HAL_DATASPACE_STANDARD_BT601_625 | HAL_DATASPACE_RANGE_LIMITED, V4L2_COLORSPACE_SMPTE170M},
244     {HAL_DATASPACE_STANDARD_BT601_525 | HAL_DATASPACE_RANGE_LIMITED, V4L2_COLORSPACE_SMPTE170M},
245     {HAL_DATASPACE_STANDARD_BT2020 | HAL_DATASPACE_RANGE_LIMITED,    V4L2_COLORSPACE_BT2020},
246     {HAL_DATASPACE_STANDARD_FILM | HAL_DATASPACE_RANGE_FULL,         V4L2_COLORSPACE_SRGB},
247     {HAL_DATASPACE_STANDARD_FILM | HAL_DATASPACE_RANGE_LIMITED,      V4L2_COLORSPACE_REC709},
248     {HAL_DATASPACE_SRGB,                                             V4L2_COLORSPACE_SRGB},
249     {HAL_DATASPACE_JFIF,                                             V4L2_COLORSPACE_JPEG},
250     {HAL_DATASPACE_BT601_525,                                        V4L2_COLORSPACE_SMPTE170M},
251     {HAL_DATASPACE_BT601_625,                                        V4L2_COLORSPACE_SMPTE170M},
252     {HAL_DATASPACE_BT709,                                            V4L2_COLORSPACE_REC709},
253 };
254 
255 #define HAL_DATASPACE_LEGACY_TYPE_MASK  ((1 << HAL_DATASPACE_STANDARD_SHIFT) - 1)
256 
haldataspace_to_v4l2(int dataspace,uint32_t width,uint32_t height)257 uint32_t haldataspace_to_v4l2(int dataspace, uint32_t width, uint32_t height)
258 {
259     // if legacy type, discard upper bits above 15th
260     if ((dataspace & HAL_DATASPACE_LEGACY_TYPE_MASK) != 0) {
261         dataspace &= HAL_DATASPACE_LEGACY_TYPE_MASK;
262     } else {
263         dataspace &= ~HAL_DATASPACE_LEGACY_TYPE_MASK;
264         // if Y value range is not specified, force it to the limited.
265         if ((dataspace & HAL_DATASPACE_RANGE_MASK) == 0)
266             dataspace |= HAL_DATASPACE_RANGE_LIMITED;
267         // if colorspace is not specified, force it to 709 or 601 according to the resolution
268         if ((dataspace & HAL_DATASPACE_STANDARD_MASK) == 0)
269             dataspace |= ((width * height) < (1280 * 720))
270                          ? HAL_DATASPACE_STANDARD_BT601_625
271                          : HAL_DATASPACE_STANDARD_BT709;
272         // discard transfer function type values because it is not required during color space conviersion
273         dataspace &= ~HAL_DATASPACE_TRANSFER_MASK;
274     }
275     for (size_t i = 0 ; i < ARRSIZE(__haldataspace_to_v4l2); i++) {
276         if (__haldataspace_to_v4l2[i].hal == dataspace)
277             return __haldataspace_to_v4l2[i].v4l2;
278     }
279 
280     LOGASSERT(1, "Unable to find HAL dataspace value %#x", dataspace);
281 
282     return V4L2_COLORSPACE_DEFAULT;
283 }
284