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 (at) samsung.com) 20 * date 2011/06/02 21 * 22 * Revision History: 23 * - 2010/06/03 : Sangwoo, Park(sw5771.park (at) samsung.com) 24 * Initial version 25 * 26 * - 2011/12/07 : Hyeonmyeong Choi( hyeon.choi (at) samsung.com) 27 * Add V4L2_PIX_FMT_YVU420M 28 * 29 * - 2012/03/06 : shinwon lee(shinwon.lee (at) 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 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_RGBA_5551: 72 v4l2_pixel_format = V4L2_PIX_FMT_RGB555X; 73 break; 74 75 case HAL_PIXEL_FORMAT_RGBA_4444: 76 v4l2_pixel_format = V4L2_PIX_FMT_RGB444; 77 break; 78 79 case HAL_PIXEL_FORMAT_EXYNOS_YV12: 80 v4l2_pixel_format = V4L2_PIX_FMT_YVU420M; 81 break; 82 83 case HAL_PIXEL_FORMAT_YCbCr_420_P: 84 v4l2_pixel_format = V4L2_PIX_FMT_YUV420M; 85 break; 86 87 case HAL_PIXEL_FORMAT_YCbCr_422_SP: 88 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP: 89 v4l2_pixel_format = V4L2_PIX_FMT_NV61; 90 break; 91 92 case HAL_PIXEL_FORMAT_YCbCr_420_SP: 93 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP: 94 v4l2_pixel_format = V4L2_PIX_FMT_NV12M; 95 break; 96 97 case HAL_PIXEL_FORMAT_YCbCr_422_I: 98 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I: 99 v4l2_pixel_format = V4L2_PIX_FMT_YUYV; 100 break; 101 102 case HAL_PIXEL_FORMAT_YCbCr_422_P: 103 v4l2_pixel_format = V4L2_PIX_FMT_YUV422P; 104 break; 105 106 case HAL_PIXEL_FORMAT_CbYCrY_422_I: 107 case HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I: 108 v4l2_pixel_format = V4L2_PIX_FMT_UYVY; 109 break; 110 111 case HAL_PIXEL_FORMAT_YCrCb_422_SP: 112 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP: 113 v4l2_pixel_format = V4L2_PIX_FMT_NV16; 114 break; 115 116 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: 117 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: 118 v4l2_pixel_format = V4L2_PIX_FMT_NV21M; 119 break; 120 121 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: 122 v4l2_pixel_format = V4L2_PIX_FMT_NV12MT_16X16; 123 break; 124 125 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: 126 v4l2_pixel_format = V4L2_PIX_FMT_NV12MT_16X16; 127 break; 128 129 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I: 130 v4l2_pixel_format = V4L2_PIX_FMT_YVYU; 131 break; 132 133 case HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I: 134 v4l2_pixel_format = V4L2_PIX_FMT_VYUY; 135 break; 136 137 default: 138 ALOGE("%s::unmatched HAL_PIXEL_FORMAT color_space(0x%x)\n", 139 __func__, hal_pixel_format); 140 break; 141 } 142 143 return v4l2_pixel_format; 144 } 145 146 int V4L2_PIX_2_HAL_PIXEL_FORMAT( 147 int v4l2_pixel_format) 148 { 149 int hal_pixel_format = -1; 150 151 switch (v4l2_pixel_format) { 152 case V4L2_PIX_FMT_RGB32: 153 hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_8888; 154 break; 155 156 case V4L2_PIX_FMT_RGB24: 157 hal_pixel_format = HAL_PIXEL_FORMAT_RGB_888; 158 break; 159 160 case V4L2_PIX_FMT_RGB565: 161 hal_pixel_format = HAL_PIXEL_FORMAT_RGB_565; 162 break; 163 164 case V4L2_PIX_FMT_BGR32: 165 hal_pixel_format = HAL_PIXEL_FORMAT_BGRA_8888; 166 break; 167 168 case V4L2_PIX_FMT_RGB555X: 169 hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_5551; 170 break; 171 172 case V4L2_PIX_FMT_RGB444: 173 hal_pixel_format = HAL_PIXEL_FORMAT_RGBA_4444; 174 break; 175 176 case V4L2_PIX_FMT_YUV420: 177 case V4L2_PIX_FMT_YUV420M: 178 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_P; 179 break; 180 181 case V4L2_PIX_FMT_YVU420: 182 case V4L2_PIX_FMT_YVU420M: 183 hal_pixel_format = HAL_PIXEL_FORMAT_EXYNOS_YV12; 184 break; 185 186 case V4L2_PIX_FMT_NV16: 187 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP; 188 break; 189 190 case V4L2_PIX_FMT_NV12: 191 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_SP; 192 break; 193 194 case V4L2_PIX_FMT_NV12M: 195 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP; 196 break; 197 198 case V4L2_PIX_FMT_YUYV: 199 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I; 200 break; 201 202 case V4L2_PIX_FMT_YUV422P: 203 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_422_P; 204 break; 205 206 case V4L2_PIX_FMT_UYVY: 207 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I; 208 break; 209 210 case V4L2_PIX_FMT_NV21: 211 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP; 212 break; 213 case V4L2_PIX_FMT_NV12MT_16X16: 214 215 hal_pixel_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED; 216 break; 217 218 case V4L2_PIX_FMT_NV61: 219 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP; 220 break; 221 222 case V4L2_PIX_FMT_YVYU: 223 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I; 224 break; 225 226 case V4L2_PIX_FMT_VYUY: 227 hal_pixel_format = HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I; 228 break; 229 230 default: 231 ALOGE("%s::unmatched V4L2_PIX color_space(%d)\n", 232 __func__, v4l2_pixel_format); 233 break; 234 } 235 236 return hal_pixel_format; 237 } 238 239 int NUM_PLANES(int hal_pixel_format) 240 { 241 switch(hal_pixel_format) { 242 case HAL_PIXEL_FORMAT_EXYNOS_YV12: 243 case HAL_PIXEL_FORMAT_YCbCr_420_P: 244 return 3; 245 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: 246 case HAL_PIXEL_FORMAT_YCbCr_420_SP: 247 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP: 248 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: 249 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: 250 return 2; 251 break; 252 case HAL_PIXEL_FORMAT_YCbCr_420_I: 253 case HAL_PIXEL_FORMAT_CbYCrY_420_I: 254 return 1; 255 default: 256 return 1; 257 } 258 } 259 260 unsigned int FRAME_SIZE( 261 int hal_pixel_format, 262 int width, 263 int height) 264 { 265 unsigned int frame_size = 0; 266 unsigned int size = 0; 267 268 switch (hal_pixel_format) { 269 // 16bpp 270 case HAL_PIXEL_FORMAT_RGB_565: 271 case HAL_PIXEL_FORMAT_RGBA_5551: 272 case HAL_PIXEL_FORMAT_RGBA_4444: 273 frame_size = GET_16BPP_FRAME_SIZE(width, height); 274 break; 275 276 // 24bpp 277 case HAL_PIXEL_FORMAT_RGB_888: 278 frame_size = GET_24BPP_FRAME_SIZE(width, height); 279 break; 280 281 // 32bpp 282 case HAL_PIXEL_FORMAT_RGBA_8888: 283 case HAL_PIXEL_FORMAT_BGRA_8888: 284 case HAL_PIXEL_FORMAT_RGBX_8888: 285 case HAL_PIXEL_FORMAT_CUSTOM_ARGB_8888: 286 frame_size = GET_32BPP_FRAME_SIZE(width, height); 287 break; 288 289 // 12bpp 290 case HAL_PIXEL_FORMAT_EXYNOS_YV12: 291 case HAL_PIXEL_FORMAT_YCbCr_420_P: 292 case HAL_PIXEL_FORMAT_YCbCr_420_I: 293 case HAL_PIXEL_FORMAT_CbYCrY_420_I: 294 size = ALIGN(width, 16) * ALIGN(height, 16); 295 frame_size = size + 2 * ALIGN(width >> 1, 8) * ALIGN(height >> 1, 8); 296 break; 297 298 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: 299 case HAL_PIXEL_FORMAT_YCbCr_420_SP: 300 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP: 301 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: 302 size = ALIGN(ALIGN(width, 16) * ALIGN(height, 16), 2048); 303 frame_size = size + ALIGN(ALIGN(width, 16) * ALIGN(height >> 1, 8), 2048); 304 break; 305 306 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: 307 frame_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)) 308 + ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height >> 1)); 309 break; 310 311 // 16bpp 312 case HAL_PIXEL_FORMAT_YCbCr_422_SP: 313 case HAL_PIXEL_FORMAT_YCbCr_422_I: 314 case HAL_PIXEL_FORMAT_YCbCr_422_P: 315 case HAL_PIXEL_FORMAT_CbYCrY_422_I: 316 case HAL_PIXEL_FORMAT_YCrCb_422_SP: 317 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_SP: 318 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_SP: 319 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_422_I: 320 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_422_I: 321 case HAL_PIXEL_FORMAT_CUSTOM_CbYCrY_422_I: 322 case HAL_PIXEL_FORMAT_CUSTOM_CrYCbY_422_I: 323 frame_size = GET_16BPP_FRAME_SIZE(width, height); 324 break; 325 326 default: 327 ALOGD("%s::no matching source colorformat(0x%x), width(%d), h(%d) fail\n", 328 __func__, hal_pixel_format, width, height); 329 break; 330 } 331 332 return frame_size; 333 } 334 335 int PLANAR_FRAME_SIZE(int hal_pixel_format, int width, int height, 336 unsigned int *luma_size, unsigned int *chroma_size) 337 { 338 unsigned int size = 0; 339 340 switch(hal_pixel_format) { 341 case HAL_PIXEL_FORMAT_EXYNOS_YV12: 342 case HAL_PIXEL_FORMAT_YCbCr_420_P: 343 size = width * height; 344 *luma_size = size; 345 *chroma_size = size >> 2; 346 case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP: 347 case HAL_PIXEL_FORMAT_YCbCr_420_I: 348 case HAL_PIXEL_FORMAT_CbYCrY_420_I: 349 case HAL_PIXEL_FORMAT_YCbCr_420_SP: 350 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP: 351 case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: 352 size = width * height; 353 *luma_size = size; 354 *chroma_size = (size >> 2) << 1; 355 break; 356 case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: 357 *luma_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * ALIGN_TO_32B(height)); 358 *chroma_size = ALIGN_TO_8KB(ALIGN_TO_128B(width) * 359 ALIGN_TO_32B(height >> 1)); 360 break; 361 default: 362 *luma_size = FRAME_SIZE(hal_pixel_format, width, height); 363 } 364 return 0; 365 } 366 367 368 int V4L2_PIX_2_YUV_INFO(unsigned int v4l2_pixel_format, unsigned int * bpp, unsigned int * planes) 369 { 370 switch (v4l2_pixel_format) { 371 case V4L2_PIX_FMT_NV12: 372 *bpp = 12; 373 *planes = 1; 374 break; 375 case V4L2_PIX_FMT_NV21: 376 case V4L2_PIX_FMT_NV21X: 377 case V4L2_PIX_FMT_NV12M: 378 case V4L2_PIX_FMT_NV21M: 379 case V4L2_PIX_FMT_NV12MT_16X16: 380 *bpp = 12; 381 *planes = 2; 382 break; 383 case V4L2_PIX_FMT_YUV420: 384 case V4L2_PIX_FMT_YUV420M: 385 case V4L2_PIX_FMT_YVU420M: 386 *bpp = 12; 387 *planes = 3; 388 break; 389 case V4L2_PIX_FMT_YUYV: 390 case V4L2_PIX_FMT_YVYU: 391 case V4L2_PIX_FMT_UYVY: 392 case V4L2_PIX_FMT_VYUY: 393 *bpp = 16; 394 *planes = 1; 395 break; 396 case V4L2_PIX_FMT_NV16: 397 case V4L2_PIX_FMT_NV61: 398 case V4L2_PIX_FMT_NV16X: 399 case V4L2_PIX_FMT_NV61X: 400 *bpp = 16; 401 *planes = 2; 402 break; 403 case V4L2_PIX_FMT_YUV422P: 404 *bpp = 16; 405 *planes = 3; 406 break; 407 default: 408 return -1; 409 break; 410 } 411 412 return 0; 413 } 414 415 unsigned int get_yuv_bpp(unsigned int v4l2_pixel_format) 416 { 417 unsigned int bpp, planes; 418 419 if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0) 420 bpp = -1; 421 422 return bpp; 423 } 424 425 unsigned int get_yuv_planes(unsigned int v4l2_pixel_format) 426 { 427 unsigned int bpp, planes; 428 429 if (V4L2_PIX_2_YUV_INFO(v4l2_pixel_format, &bpp, &planes) < 0) 430 planes = -1; 431 432 return planes; 433 } 434 #endif 435