1 /*
2 * Copyright@ Samsung Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * file exynos5_format_v4l2.c
19 * author Sangwoo, Park(sw5771.park@samsung.com)
20 * date 2011/06/02
21 *
22 * Revision History:
23 * - 2010/06/03 : Sangwoo, Park(sw5771.park@samsung.com)
24 * Initial version
25 *
26 * - 2011/12/07 : Hyeonmyeong Choi( hyeon.choi@samsung.com)
27 * Add V4L2_PIX_FMT_YVU420M
28 *
29 * - 2012/03/06 : shinwon lee(shinwon.lee@samsung.com)
30 * Merge to libexynosutils
31 *
32 */
33
34 #ifndef __EXYNOS5_FORMAT_V4L2_H__
35 #define __EXYNOS5_FORMAT_V4L2_H__
36
37 //---------------------------------------------------------//
38 // Include
39 //---------------------------------------------------------//
40 #include <hardware/hardware.h>
41 #include "exynos_format.h"
42 #include "s5p_fimc_v4l2.h"
43 #include <utils/Log.h>
44 #include "videodev2.h"
45 #include "videodev2_exynos_media.h"
46
HAL_PIXEL_FORMAT_2_V4L2_PIX(int hal_pixel_format)47 int HAL_PIXEL_FORMAT_2_V4L2_PIX(
48 int hal_pixel_format)
49 {
50 int v4l2_pixel_format = -1;
51
52 switch (hal_pixel_format) {
53 case HAL_PIXEL_FORMAT_RGBA_8888:
54 case HAL_PIXEL_FORMAT_RGBX_8888:
55 case HAL_PIXEL_FORMAT_CUSTOM_ARGB_8888:
56 v4l2_pixel_format = V4L2_PIX_FMT_RGB32;
57 break;
58
59 case HAL_PIXEL_FORMAT_RGB_888:
60 v4l2_pixel_format = V4L2_PIX_FMT_RGB24;
61 break;
62
63 case HAL_PIXEL_FORMAT_RGB_565:
64 v4l2_pixel_format = V4L2_PIX_FMT_RGB565;
65 break;
66
67 case HAL_PIXEL_FORMAT_BGRA_8888:
68 v4l2_pixel_format = V4L2_PIX_FMT_BGR32;
69 break;
70
71 case HAL_PIXEL_FORMAT_EXYNOS_YV12:
72 v4l2_pixel_format = V4L2_PIX_FMT_YVU420M;
73 break;
74
75 case HAL_PIXEL_FORMAT_YCbCr_420_P:
76 v4l2_pixel_format = V4L2_PIX_FMT_YUV420M;
77 break;
78
79 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
80 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP:
81 v4l2_pixel_format = V4L2_PIX_FMT_NV61;
82 break;
83
84 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
85 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP:
86 v4l2_pixel_format = V4L2_PIX_FMT_NV12M;
87 break;
88
89 case HAL_PIXEL_FORMAT_YCbCr_422_I:
90 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I:
91 v4l2_pixel_format = V4L2_PIX_FMT_YUYV;
92 break;
93
94 case HAL_PIXEL_FORMAT_YCbCr_422_P:
95 v4l2_pixel_format = V4L2_PIX_FMT_YUV422P;
96 break;
97
98 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
99 case HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I:
100 v4l2_pixel_format = V4L2_PIX_FMT_UYVY;
101 break;
102
103 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
104 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP:
105 v4l2_pixel_format = V4L2_PIX_FMT_NV16;
106 break;
107
108 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
109 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP:
110 v4l2_pixel_format = V4L2_PIX_FMT_NV21M;
111 break;
112
113 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED:
114 v4l2_pixel_format = V4L2_PIX_FMT_NV12MT_16X16;
115 break;
116
117 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED:
118 v4l2_pixel_format = V4L2_PIX_FMT_NV12MT_16X16;
119 break;
120
121 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I:
122 v4l2_pixel_format = V4L2_PIX_FMT_YVYU;
123 break;
124
125 case HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I:
126 v4l2_pixel_format = V4L2_PIX_FMT_VYUY;
127 break;
128
129 default:
130 ALOGE("%s::unmatched HAL_PIXEL_FORMAT color_space(0x%x)\n",
131 __func__, hal_pixel_format);
132 break;
133 }
134
135 return v4l2_pixel_format;
136 }
137
V4L2_PIX_2_HAL_PIXEL_FORMAT(int v4l2_pixel_format)138 int V4L2_PIX_2_HAL_PIXEL_FORMAT(
139 int v4l2_pixel_format)
140 {
141 int hal_pixel_format = -1;
142
143 switch (v4l2_pixel_format) {
144 case V4L2_PIX_FMT_RGB32:
145 hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
146 break;
147
148 case V4L2_PIX_FMT_RGB24:
149 hal_pixel_format = HAL_PIXEL_FORMAT_RGB_888;
150 break;
151
152 case V4L2_PIX_FMT_RGB565:
153 hal_pixel_format = HAL_PIXEL_FORMAT_RGB_565;
154 break;
155
156 case V4L2_PIX_FMT_BGR32:
157 hal_pixel_format = HAL_PIXEL_FORMAT_BGRA_8888;
158 break;
159
160 case V4L2_PIX_FMT_YUV420:
161 case V4L2_PIX_FMT_YUV420M:
162 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_P;
163 break;
164
165 case V4L2_PIX_FMT_YVU420:
166 case V4L2_PIX_FMT_YVU420M:
167 hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YV12;
168 break;
169
170 case V4L2_PIX_FMT_NV16:
171 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP;
172 break;
173
174 case V4L2_PIX_FMT_NV12:
175 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_SP;
176 break;
177
178 case V4L2_PIX_FMT_NV12M:
179 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP;
180 break;
181
182 case V4L2_PIX_FMT_YUYV:
183 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I;
184 break;
185
186 case V4L2_PIX_FMT_YUV422P:
187 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_422_P;
188 break;
189
190 case V4L2_PIX_FMT_UYVY:
191 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I;
192 break;
193
194 case V4L2_PIX_FMT_NV21:
195 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP;
196 break;
197 case V4L2_PIX_FMT_NV12MT_16X16:
198
199 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED;
200 break;
201
202 case V4L2_PIX_FMT_NV61:
203 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP;
204 break;
205
206 case V4L2_PIX_FMT_YVYU:
207 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I;
208 break;
209
210 case V4L2_PIX_FMT_VYUY:
211 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I;
212 break;
213
214 default:
215 ALOGE("%s::unmatched V4L2_PIX color_space(%d)\n",
216 __func__, v4l2_pixel_format);
217 break;
218 }
219
220 return hal_pixel_format;
221 }
222
NUM_PLANES(int hal_pixel_format)223 int NUM_PLANES(int hal_pixel_format)
224 {
225 switch(hal_pixel_format) {
226 case HAL_PIXEL_FORMAT_EXYNOS_YV12:
227 case HAL_PIXEL_FORMAT_YCbCr_420_P:
228 return 3;
229 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
230 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
231 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP:
232 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP:
233 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED:
234 return 2;
235 break;
236 case HAL_PIXEL_FORMAT_YCbCr_420_I:
237 case HAL_PIXEL_FORMAT_CbYCrY_420_I:
238 return 1;
239 default:
240 return 1;
241 }
242 }
243
FRAME_SIZE(int hal_pixel_format,int width,int height)244 unsigned int FRAME_SIZE(
245 int hal_pixel_format,
246 int width,
247 int height)
248 {
249 unsigned int frame_size = 0;
250 unsigned int size = 0;
251
252 switch (hal_pixel_format) {
253 // 16bpp
254 case HAL_PIXEL_FORMAT_RGB_565:
255 frame_size = GET_16BPP_FRAME_SIZE(width, height);
256 break;
257
258 // 24bpp
259 case HAL_PIXEL_FORMAT_RGB_888:
260 frame_size = GET_24BPP_FRAME_SIZE(width, height);
261 break;
262
263 // 32bpp
264 case HAL_PIXEL_FORMAT_RGBA_8888:
265 case HAL_PIXEL_FORMAT_BGRA_8888:
266 case HAL_PIXEL_FORMAT_RGBX_8888:
267 case HAL_PIXEL_FORMAT_CUSTOM_ARGB_8888:
268 frame_size = GET_32BPP_FRAME_SIZE(width, height);
269 break;
270
271 // 12bpp
272 case HAL_PIXEL_FORMAT_EXYNOS_YV12:
273 case HAL_PIXEL_FORMAT_YCbCr_420_P:
274 case HAL_PIXEL_FORMAT_YCbCr_420_I:
275 case HAL_PIXEL_FORMAT_CbYCrY_420_I:
276 size = ALIGN(width, 16) * ALIGN(height, 16);
277 frame_size = size + 2 * ALIGN(width >> 1, 8) * ALIGN(height >> 1, 8);
278 break;
279
280 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
281 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
282 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP:
283 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP:
284 size = ALIGN(ALIGN(width, 16) * ALIGN(height, 16), 2048);
285 frame_size = size + ALIGN(ALIGN(width, 16) * ALIGN(height >> 1, 8), 2048);
286 break;
287
288 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED:
289 frame_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height))
290 + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height >> 1));
291 break;
292
293 // 16bpp
294 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
295 case HAL_PIXEL_FORMAT_YCbCr_422_I:
296 case HAL_PIXEL_FORMAT_YCbCr_422_P:
297 case HAL_PIXEL_FORMAT_CbYCrY_422_I:
298 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
299 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP:
300 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP:
301 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I:
302 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I:
303 case HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I:
304 case HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I:
305 frame_size = GET_16BPP_FRAME_SIZE(width, height);
306 break;
307
308 default:
309 ALOGD("%s::no matching source colorformat(0x%x), width(%d), h(%d) fail\n",
310 __func__, hal_pixel_format, width, height);
311 break;
312 }
313
314 return frame_size;
315 }
316
PLANAR_FRAME_SIZE(int hal_pixel_format,int width,int height,unsigned int * luma_size,unsigned int * chroma_size)317 int PLANAR_FRAME_SIZE(int hal_pixel_format, int width, int height,
318 unsigned int *luma_size, unsigned int *chroma_size)
319 {
320 unsigned int size = 0;
321
322 switch(hal_pixel_format) {
323 case HAL_PIXEL_FORMAT_EXYNOS_YV12:
324 case HAL_PIXEL_FORMAT_YCbCr_420_P:
325 size = width * height;
326 *luma_size = size;
327 *chroma_size = size >> 2;
328 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP:
329 case HAL_PIXEL_FORMAT_YCbCr_420_I:
330 case HAL_PIXEL_FORMAT_CbYCrY_420_I:
331 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
332 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP:
333 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP:
334 size = width * height;
335 *luma_size = size;
336 *chroma_size = (size >> 2) << 1;
337 break;
338 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED:
339 *luma_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height));
340 *chroma_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) *
341 ALIGN_TO_32B(height >> 1));
342 break;
343 default:
344 *luma_size = FRAME_SIZE(hal_pixel_format, width, height);
345 }
346 return 0;
347 }
348
349
V4L2_PIX_2_YUV_INFO(unsigned int v4l2_pixel_format,unsigned int * bpp,unsigned int * planes)350 int V4L2_PIX_2_YUV_INFO(unsigned int v4l2_pixel_format, unsigned int * bpp, unsigned int * planes)
351 {
352 switch (v4l2_pixel_format) {
353 case V4L2_PIX_FMT_NV12:
354 *bpp = 12;
355 *planes = 1;
356 break;
357 case V4L2_PIX_FMT_NV21:
358 case V4L2_PIX_FMT_NV21X:
359 case V4L2_PIX_FMT_NV12M:
360 case V4L2_PIX_FMT_NV21M:
361 case V4L2_PIX_FMT_NV12MT_16X16:
362 *bpp = 12;
363 *planes = 2;
364 break;
365 case V4L2_PIX_FMT_YUV420:
366 case V4L2_PIX_FMT_YUV420M:
367 case V4L2_PIX_FMT_YVU420M:
368 *bpp = 12;
369 *planes = 3;
370 break;
371 case V4L2_PIX_FMT_YUYV:
372 case V4L2_PIX_FMT_YVYU:
373 case V4L2_PIX_FMT_UYVY:
374 case V4L2_PIX_FMT_VYUY:
375 *bpp = 16;
376 *planes = 1;
377 break;
378 case V4L2_PIX_FMT_NV16:
379 case V4L2_PIX_FMT_NV61:
380 case V4L2_PIX_FMT_NV16X:
381 case V4L2_PIX_FMT_NV61X:
382 *bpp = 16;
383 *planes = 2;
384 break;
385 case V4L2_PIX_FMT_YUV422P:
386 *bpp = 16;
387 *planes = 3;
388 break;
389 default:
390 return -1;
391 break;
392 }
393
394 return 0;
395 }
396
get_yuv_bpp(unsigned int v4l2_pixel_format)397 unsigned int get_yuv_bpp(unsigned int v4l2_pixel_format)
398 {
399 unsigned int bpp, planes;
400
401 if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0)
402 bpp = -1;
403
404 return bpp;
405 }
406
get_yuv_planes(unsigned int v4l2_pixel_format)407 unsigned int get_yuv_planes(unsigned int v4l2_pixel_format)
408 {
409 unsigned int bpp, planes;
410
411 if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0)
412 planes = -1;
413
414 return planes;
415 }
416 #endif
417