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 <linux/videodev2.h>
45 #include <linux/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         v4l2_pixel_format = V4L2_PIX_FMT_RGB32;
56         break;
57 
58     case HAL_PIXEL_FORMAT_RGB_888:
59         v4l2_pixel_format = V4L2_PIX_FMT_RGB24;
60         break;
61 
62     case HAL_PIXEL_FORMAT_RGB_565:
63         v4l2_pixel_format = V4L2_PIX_FMT_RGB565;
64         break;
65 
66     case HAL_PIXEL_FORMAT_BGRA_8888:
67         v4l2_pixel_format = V4L2_PIX_FMT_BGR32;
68         break;
69 
70     case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
71         v4l2_pixel_format = V4L2_PIX_FMT_YVU420M;
72         break;
73 
74     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
75         v4l2_pixel_format = V4L2_PIX_FMT_YUV420M;
76         break;
77 
78     case HAL_PIXEL_FORMAT_YV12:
79         v4l2_pixel_format = V4L2_PIX_FMT_YVU420;
80         break;
81 
82     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
83         v4l2_pixel_format = V4L2_PIX_FMT_YUV420;
84         break;
85 
86     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN:
87         v4l2_pixel_format = V4L2_PIX_FMT_YUV420N;
88         break;
89 
90     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
91         v4l2_pixel_format = V4L2_PIX_FMT_NV16;
92         break;
93 
94     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP:
95         v4l2_pixel_format = V4L2_PIX_FMT_NV12;
96         break;
97 
98     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
99         v4l2_pixel_format = V4L2_PIX_FMT_NV12N;
100         break;
101 
102     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
103     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV:
104     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
105         v4l2_pixel_format = V4L2_PIX_FMT_NV12M;
106         break;
107 
108     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B:
109         v4l2_pixel_format = V4L2_PIX_FMT_NV12N_10B;
110         break;
111 
112     case HAL_PIXEL_FORMAT_YCbCr_422_I:
113         v4l2_pixel_format = V4L2_PIX_FMT_YUYV;
114         break;
115 
116     case HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I:
117         v4l2_pixel_format = V4L2_PIX_FMT_UYVY;
118         break;
119 
120     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP:
121         v4l2_pixel_format = V4L2_PIX_FMT_NV61;
122         break;
123 
124     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
125     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
126         v4l2_pixel_format = V4L2_PIX_FMT_NV21M;
127         break;
128 
129     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
130         v4l2_pixel_format = V4L2_PIX_FMT_NV21;
131         break;
132 
133    case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
134 #ifdef USES_FIMC
135 	v4l2_pixel_format = V4L2_PIX_FMT_NV12MT;
136 #else
137 	v4l2_pixel_format = V4L2_PIX_FMT_NV12MT_16X16;
138 #endif
139 	break;
140 
141    case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED:
142         v4l2_pixel_format = V4L2_PIX_FMT_NV12NT;
143         break;
144 
145    case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I:
146         v4l2_pixel_format = V4L2_PIX_FMT_YVYU;
147         break;
148 
149    case HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I:
150         v4l2_pixel_format = V4L2_PIX_FMT_VYUY;
151         break;
152 
153    case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
154         v4l2_pixel_format = V4L2_PIX_FMT_NV12M_P010;
155         break;
156 
157     default:
158         ALOGE("%s::unmatched HAL_PIXEL_FORMAT color_space(0x%x)\n",
159                 __func__, hal_pixel_format);
160         break;
161     }
162 
163     return v4l2_pixel_format;
164 }
165 
V4L2_PIX_2_HAL_PIXEL_FORMAT(int v4l2_pixel_format)166 int V4L2_PIX_2_HAL_PIXEL_FORMAT(
167     int v4l2_pixel_format)
168 {
169     int hal_pixel_format = -1;
170 
171     switch (v4l2_pixel_format) {
172     case V4L2_PIX_FMT_RGB32:
173         hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_8888;
174         break;
175 
176     case V4L2_PIX_FMT_RGB24:
177         hal_pixel_format = HAL_PIXEL_FORMAT_RGB_888;
178         break;
179 
180     case V4L2_PIX_FMT_RGB565:
181         hal_pixel_format = HAL_PIXEL_FORMAT_RGB_565;
182         break;
183 
184     case V4L2_PIX_FMT_BGR32:
185         hal_pixel_format = HAL_PIXEL_FORMAT_BGRA_8888;
186         break;
187 
188     case V4L2_PIX_FMT_YUV420:
189         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P;
190 	break;
191 
192     case V4L2_PIX_FMT_YUV420N:
193         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN;
194 	break;
195 
196     case V4L2_PIX_FMT_YUV420M:
197         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M;
198         break;
199 
200     case V4L2_PIX_FMT_YVU420:
201         hal_pixel_format = HAL_PIXEL_FORMAT_YV12;
202         break;
203 
204     case V4L2_PIX_FMT_YVU420M:
205         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YV12_M;
206         break;
207 
208     case V4L2_PIX_FMT_NV16:
209         hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_422_SP;
210         break;
211 
212     case V4L2_PIX_FMT_NV12:
213         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP;
214         break;
215 
216     case V4L2_PIX_FMT_NV12N:
217         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN;
218         break;
219 
220     case V4L2_PIX_FMT_NV12M:
221         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M;
222         break;
223 
224     case V4L2_PIX_FMT_NV21M:
225         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M;
226         break;
227 
228     case V4L2_PIX_FMT_YUYV:
229         hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_422_I;
230         break;
231 
232     case V4L2_PIX_FMT_UYVY:
233         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I;
234         break;
235 
236     case V4L2_PIX_FMT_NV21:
237         hal_pixel_format = HAL_PIXEL_FORMAT_YCrCb_420_SP;
238         break;
239 
240     case V4L2_PIX_FMT_NV12MT:
241     case V4L2_PIX_FMT_NV12MT_16X16:
242         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED;
243         break;
244 
245     case V4L2_PIX_FMT_NV12NT:
246         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED;
247         break;
248 
249     case V4L2_PIX_FMT_NV61:
250         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP;
251         break;
252 
253     case V4L2_PIX_FMT_YVYU:
254         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I;
255         break;
256 
257     case V4L2_PIX_FMT_VYUY:
258         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I;
259         break;
260 
261     case V4L2_PIX_FMT_NV12N_10B:
262         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B;
263         break;
264 
265     case V4L2_PIX_FMT_NV12M_P010:
266         hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M;
267         break;
268 
269     default:
270         ALOGE("%s::unmatched V4L2_PIX color_space(%d)\n",
271                 __func__, v4l2_pixel_format);
272         break;
273     }
274 
275     return hal_pixel_format;
276 }
277 
NUM_PLANES(int hal_pixel_format)278 int NUM_PLANES(int hal_pixel_format)
279 {
280     switch(hal_pixel_format) {
281     case HAL_PIXEL_FORMAT_EXYNOS_YV12_M:
282     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M:
283 	return 3;
284     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M:
285     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV:
286     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B:
287     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B:
288     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M:
289     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M:
290     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL:
291     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED:
292 	return 2;
293 	break;
294     case HAL_PIXEL_FORMAT_YV12:
295     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
296     case HAL_PIXEL_FORMAT_YCbCr_422_I:
297     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
298     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP:
299     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
300     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I:
301     case HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I:
302     case HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I:
303     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP:
304     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED:
305     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
306     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN:
307         return 1;
308     default:
309 	return 1;
310     }
311 }
312 
FRAME_SIZE(int hal_pixel_format,int width,int height)313 unsigned int FRAME_SIZE(
314     int hal_pixel_format,
315     int width,
316     int height)
317 {
318     unsigned int frame_size = 0;
319     unsigned int size       = 0;
320 
321     switch (hal_pixel_format) {
322     // 16bpp
323     case HAL_PIXEL_FORMAT_RGB_565:
324         frame_size = GET_16BPP_FRAME_SIZE(width, height);
325         break;
326 
327     // 24bpp
328     case HAL_PIXEL_FORMAT_RGB_888:
329         frame_size = GET_24BPP_FRAME_SIZE(width, height);
330         break;
331 
332     // 32bpp
333     case HAL_PIXEL_FORMAT_RGBA_8888:
334     case HAL_PIXEL_FORMAT_BGRA_8888:
335     case HAL_PIXEL_FORMAT_RGBX_8888:
336         frame_size = GET_32BPP_FRAME_SIZE(width, height);
337         break;
338 
339     // 12bpp
340     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P:
341     case HAL_PIXEL_FORMAT_YV12:
342         size = ALIGN(width, 16) * height;
343         frame_size = size + ALIGN(width / 2, 16) * height;
344         break;
345 
346 
347     case HAL_PIXEL_FORMAT_YCrCb_420_SP:
348     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP:
349         frame_size = width * height * 3 / 2;
350         break;
351 
352     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN:
353     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_TILED:
354         frame_size = (ALIGN_UP(width, 16) * ALIGN_UP(height, 16) + 256) +
355                     (ALIGN_UP((ALIGN_UP(width, 16) * (ALIGN_UP(height, 16) / 2) + 256), 16));
356         break;
357 
358     case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_PN:
359         frame_size = (ALIGN_UP(width, 16) * ALIGN_UP(height, 16) + 256) +
360                     2 * (ALIGN_UP((ALIGN_UP(width / 2, 16) * (ALIGN_UP(height, 16) / 2) + 256), 16));
361         break;
362 
363     // 16bpp
364     case HAL_PIXEL_FORMAT_YCbCr_422_SP:
365     case HAL_PIXEL_FORMAT_YCbCr_422_I:
366     case HAL_PIXEL_FORMAT_EXYNOS_CbYCrY_422_I:
367     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_SP:
368     case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_422_I:
369     case HAL_PIXEL_FORMAT_EXYNOS_CrYCbY_422_I:
370         frame_size = GET_16BPP_FRAME_SIZE(width, height);
371         break;
372 
373     default:
374         ALOGD("%s::no matching source colorformat(0x%x), width(%d), h(%d) fail\n",
375                 __func__, hal_pixel_format, width, height);
376         break;
377     }
378 
379     return frame_size;
380 }
381 
V4L2_PIX_2_YUV_INFO(unsigned int v4l2_pixel_format,unsigned int * bpp,unsigned int * planes)382 int V4L2_PIX_2_YUV_INFO(unsigned int v4l2_pixel_format, unsigned int * bpp, unsigned int * planes)
383 {
384     switch (v4l2_pixel_format) {
385     case V4L2_PIX_FMT_NV12:
386     case V4L2_PIX_FMT_NV21:
387     case V4L2_PIX_FMT_NV12N:
388     case V4L2_PIX_FMT_NV12NT:
389     case V4L2_PIX_FMT_YUV420:
390     case V4L2_PIX_FMT_YVU420:
391     case V4L2_PIX_FMT_YUV420N:
392         *bpp    = 12;
393         *planes = 1;
394         break;
395 
396     case V4L2_PIX_FMT_NV12M:
397     case V4L2_PIX_FMT_NV21M:
398     case V4L2_PIX_FMT_NV12MT:
399     case V4L2_PIX_FMT_NV12MT_16X16:
400         *bpp    = 12;
401         *planes = 2;
402         break;
403 
404     case V4L2_PIX_FMT_YUV420M:
405     case V4L2_PIX_FMT_YVU420M:
406         *bpp    = 12;
407         *planes = 3;
408         break;
409 
410     case V4L2_PIX_FMT_YUYV:
411     case V4L2_PIX_FMT_YVYU:
412     case V4L2_PIX_FMT_UYVY:
413     case V4L2_PIX_FMT_VYUY:
414     case V4L2_PIX_FMT_NV16:
415     case V4L2_PIX_FMT_NV61:
416         *bpp    = 16;
417         *planes = 1;
418         break;
419         break;
420     default:
421         return -1;
422         break;
423     }
424 
425     return 0;
426 }
427 
get_yuv_bpp(unsigned int v4l2_pixel_format)428 int get_yuv_bpp(unsigned int v4l2_pixel_format)
429 {
430     unsigned int bpp, planes;
431 
432     if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0)
433         bpp = -1;
434 
435     return bpp;
436 }
437 
get_yuv_planes(unsigned int v4l2_pixel_format)438 int get_yuv_planes(unsigned int v4l2_pixel_format)
439 {
440     unsigned int bpp, planes;
441 
442     if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0)
443         planes = -1;
444 
445     return planes;
446 }
447 #endif
448