Path: blob/21.2-virgl/src/util/format/u_format_yuv.h
7202 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,17* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19* USE OR OTHER DEALINGS IN THE SOFTWARE.20*21* The above copyright notice and this permission notice (including the22* next paragraph) shall be included in all copies or substantial portions23* of the Software.24*25**************************************************************************/262728/**29* @file30* YUV colorspace conversion.31*32* @author Brian Paul <[email protected]>33* @author Michal Krol <[email protected]>34* @author Jose Fonseca <[email protected]>35*36* See also:37* - http://www.fourcc.org/fccyvrgb.php38* - http://msdn.microsoft.com/en-us/library/ms89307839* - http://en.wikipedia.org/wiki/YUV40*/414243#ifndef U_FORMAT_YUV_H_44#define U_FORMAT_YUV_H_454647#include "pipe/p_compiler.h"48#include "util/u_math.h"495051/*52* TODO: Ensure we use consistent and right floating formulas, with enough53* precision in the coefficients.54*/5556static inline void57util_format_rgb_float_to_yuv(float r, float g, float b,58uint8_t *y, uint8_t *u, uint8_t *v)59{60const float _r = SATURATE(r);61const float _g = SATURATE(g);62const float _b = SATURATE(b);6364const float scale = 255.0f;6566const int _y = scale * ( (0.257f * _r) + (0.504f * _g) + (0.098f * _b));67const int _u = scale * (-(0.148f * _r) - (0.291f * _g) + (0.439f * _b));68const int _v = scale * ( (0.439f * _r) - (0.368f * _g) - (0.071f * _b));6970*y = _y + 16;71*u = _u + 128;72*v = _v + 128;73}747576static inline void77util_format_yuv_to_rgb_float(uint8_t y, uint8_t u, uint8_t v,78float *r, float *g, float *b)79{80const int _y = y - 16;81const int _u = u - 128;82const int _v = v - 128;8384const float y_factor = 255.0f / 219.0f;8586const float scale = 1.0f / 255.0f;8788*r = scale * (y_factor * _y + 1.596f * _v);89*g = scale * (y_factor * _y - 0.391f * _u - 0.813f * _v);90*b = scale * (y_factor * _y + 2.018f * _u );91}929394static inline void95util_format_rgb_8unorm_to_yuv(uint8_t r, uint8_t g, uint8_t b,96uint8_t *y, uint8_t *u, uint8_t *v)97{98*y = (( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16;99*u = (( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128;100*v = (( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128;101}102103104static inline void105util_format_yuv_to_rgb_8unorm(uint8_t y, uint8_t u, uint8_t v,106uint8_t *r, uint8_t *g, uint8_t *b)107{108const int _y = y - 16;109const int _u = u - 128;110const int _v = v - 128;111112const int _r = (298 * _y + 409 * _v + 128) >> 8;113const int _g = (298 * _y - 100 * _u - 208 * _v + 128) >> 8;114const int _b = (298 * _y + 516 * _u + 128) >> 8;115116*r = CLAMP(_r, 0, 255);117*g = CLAMP(_g, 0, 255);118*b = CLAMP(_b, 0, 255);119}120121122123void124util_format_uyvy_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,125const uint8_t *restrict src_row, unsigned src_stride,126unsigned width, unsigned height);127128void129util_format_uyvy_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,130const uint8_t *restrict src_row, unsigned src_stride,131unsigned width, unsigned height);132133void134util_format_uyvy_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,135const float *restrict src_row, unsigned src_stride,136unsigned width, unsigned height);137138void139util_format_uyvy_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,140const uint8_t *restrict src_row, unsigned src_stride,141unsigned width, unsigned height);142143void144util_format_uyvy_fetch_rgba(void *restrict dst, const uint8_t *restrict src,145unsigned i, unsigned j);146147void148util_format_yuyv_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,149const uint8_t *restrict src_row, unsigned src_stride,150unsigned width, unsigned height);151152void153util_format_yuyv_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,154const uint8_t *restrict src_row, unsigned src_stride,155unsigned width, unsigned height);156157void158util_format_yuyv_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,159const float *restrict src_row, unsigned src_stride,160unsigned width, unsigned height);161162void163util_format_yuyv_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,164const uint8_t *restrict src_row, unsigned src_stride,165unsigned width, unsigned height);166167void168util_format_yuyv_fetch_rgba(void *restrict dst, const uint8_t *restrict src,169unsigned i, unsigned j);170171void172util_format_r8g8_b8g8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,173const uint8_t *restrict src_row, unsigned src_stride,174unsigned width, unsigned height);175176void177util_format_r8g8_b8g8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,178const uint8_t *restrict src_row, unsigned src_stride,179unsigned width, unsigned height);180181void182util_format_r8g8_b8g8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,183const float *restrict src_row, unsigned src_stride,184unsigned width, unsigned height);185186void187util_format_r8g8_b8g8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,188const uint8_t *restrict src_row, unsigned src_stride,189unsigned width, unsigned height);190191void192util_format_r8g8_b8g8_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,193unsigned i, unsigned j);194195void196util_format_g8r8_g8b8_unorm_unpack_rgba_float(void *restrict dst_row, unsigned dst_stride,197const uint8_t *restrict src_row, unsigned src_stride,198unsigned width, unsigned height);199200void201util_format_g8r8_g8b8_unorm_unpack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,202const uint8_t *restrict src_row, unsigned src_stride,203unsigned width, unsigned height);204205void206util_format_g8r8_g8b8_unorm_pack_rgba_float(uint8_t *restrict dst_row, unsigned dst_stride,207const float *restrict src_row, unsigned src_stride,208unsigned width, unsigned height);209210void211util_format_g8r8_g8b8_unorm_pack_rgba_8unorm(uint8_t *restrict dst_row, unsigned dst_stride,212const uint8_t *restrict src_row, unsigned src_stride,213unsigned width, unsigned height);214215void216util_format_g8r8_g8b8_unorm_fetch_rgba(void *restrict dst, const uint8_t *restrict src,217unsigned i, unsigned j);218219#endif /* U_FORMAT_YUV_H_ */220221222