tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

yuv.h (8467B)


      1 // Copyright 2010 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // inline YUV<->RGB conversion function
     11 //
     12 // The exact naming is Y'CbCr, following the ITU-R BT.601 standard.
     13 // More information at: https://en.wikipedia.org/wiki/YCbCr
     14 // Y = 0.2568 * R + 0.5041 * G + 0.0979 * B + 16
     15 // U = -0.1482 * R - 0.2910 * G + 0.4392 * B + 128
     16 // V = 0.4392 * R - 0.3678 * G - 0.0714 * B + 128
     17 // We use 16bit fixed point operations for RGB->YUV conversion (YUV_FIX).
     18 //
     19 // For the Y'CbCr to RGB conversion, the BT.601 specification reads:
     20 //   R = 1.164 * (Y-16) + 1.596 * (V-128)
     21 //   G = 1.164 * (Y-16) - 0.813 * (V-128) - 0.392 * (U-128)
     22 //   B = 1.164 * (Y-16)                   + 2.017 * (U-128)
     23 // where Y is in the [16,235] range, and U/V in the [16,240] range.
     24 //
     25 // The fixed-point implementation used here is:
     26 //  R = (19077 . y             + 26149 . v - 14234) >> 6
     27 //  G = (19077 . y -  6419 . u - 13320 . v +  8708) >> 6
     28 //  B = (19077 . y + 33050 . u             - 17685) >> 6
     29 // where the '.' operator is the mulhi_epu16 variant:
     30 //   a . b = ((a << 8) * b) >> 16
     31 // that preserves 8 bits of fractional precision before final descaling.
     32 
     33 // Author: Skal (pascal.massimino@gmail.com)
     34 
     35 #ifndef WEBP_DSP_YUV_H_
     36 #define WEBP_DSP_YUV_H_
     37 
     38 #include "src/dec/vp8_dec.h"
     39 #include "src/dsp/cpu.h"
     40 #include "src/dsp/dsp.h"
     41 #include "src/webp/types.h"
     42 
     43 //------------------------------------------------------------------------------
     44 // YUV -> RGB conversion
     45 
     46 #ifdef __cplusplus
     47 extern "C" {
     48 #endif
     49 
     50 enum {
     51  YUV_FIX = 16,                    // fixed-point precision for RGB->YUV
     52  YUV_HALF = 1 << (YUV_FIX - 1),
     53 
     54  YUV_FIX2 = 6,                   // fixed-point precision for YUV->RGB
     55  YUV_MASK2 = (256 << YUV_FIX2) - 1
     56 };
     57 
     58 //------------------------------------------------------------------------------
     59 // slower on x86 by ~7-8%, but bit-exact with the SSE2/NEON version
     60 
     61 static WEBP_INLINE int MultHi(int v, int coeff) {   // _mm_mulhi_epu16 emulation
     62  return (v * coeff) >> 8;
     63 }
     64 
     65 static WEBP_INLINE int VP8Clip8(int v) {
     66  return ((v & ~YUV_MASK2) == 0) ? (v >> YUV_FIX2) : (v < 0) ? 0 : 255;
     67 }
     68 
     69 static WEBP_INLINE int VP8YUVToR(int y, int v) {
     70  return VP8Clip8(MultHi(y, 19077) + MultHi(v, 26149) - 14234);
     71 }
     72 
     73 static WEBP_INLINE int VP8YUVToG(int y, int u, int v) {
     74  return VP8Clip8(MultHi(y, 19077) - MultHi(u, 6419) - MultHi(v, 13320) + 8708);
     75 }
     76 
     77 static WEBP_INLINE int VP8YUVToB(int y, int u) {
     78  return VP8Clip8(MultHi(y, 19077) + MultHi(u, 33050) - 17685);
     79 }
     80 
     81 static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v,
     82                                    uint8_t* const rgb) {
     83  rgb[0] = VP8YUVToR(y, v);
     84  rgb[1] = VP8YUVToG(y, u, v);
     85  rgb[2] = VP8YUVToB(y, u);
     86 }
     87 
     88 static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v,
     89                                    uint8_t* const bgr) {
     90  bgr[0] = VP8YUVToB(y, u);
     91  bgr[1] = VP8YUVToG(y, u, v);
     92  bgr[2] = VP8YUVToR(y, v);
     93 }
     94 
     95 static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v,
     96                                       uint8_t* const rgb) {
     97  const int r = VP8YUVToR(y, v);      // 5 usable bits
     98  const int g = VP8YUVToG(y, u, v);   // 6 usable bits
     99  const int b = VP8YUVToB(y, u);      // 5 usable bits
    100  const int rg = (r & 0xf8) | (g >> 5);
    101  const int gb = ((g << 3) & 0xe0) | (b >> 3);
    102 #if (WEBP_SWAP_16BIT_CSP == 1)
    103  rgb[0] = gb;
    104  rgb[1] = rg;
    105 #else
    106  rgb[0] = rg;
    107  rgb[1] = gb;
    108 #endif
    109 }
    110 
    111 static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v,
    112                                         uint8_t* const argb) {
    113  const int r = VP8YUVToR(y, v);        // 4 usable bits
    114  const int g = VP8YUVToG(y, u, v);     // 4 usable bits
    115  const int b = VP8YUVToB(y, u);        // 4 usable bits
    116  const int rg = (r & 0xf0) | (g >> 4);
    117  const int ba = (b & 0xf0) | 0x0f;     // overwrite the lower 4 bits
    118 #if (WEBP_SWAP_16BIT_CSP == 1)
    119  argb[0] = ba;
    120  argb[1] = rg;
    121 #else
    122  argb[0] = rg;
    123  argb[1] = ba;
    124 #endif
    125 }
    126 
    127 //-----------------------------------------------------------------------------
    128 // Alpha handling variants
    129 
    130 static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
    131                                     uint8_t* const argb) {
    132  argb[0] = 0xff;
    133  VP8YuvToRgb(y, u, v, argb + 1);
    134 }
    135 
    136 static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
    137                                     uint8_t* const bgra) {
    138  VP8YuvToBgr(y, u, v, bgra);
    139  bgra[3] = 0xff;
    140 }
    141 
    142 static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
    143                                     uint8_t* const rgba) {
    144  VP8YuvToRgb(y, u, v, rgba);
    145  rgba[3] = 0xff;
    146 }
    147 
    148 //-----------------------------------------------------------------------------
    149 // SSE2 extra functions (mostly for upsampling_sse2.c)
    150 
    151 #if defined(WEBP_USE_SSE2)
    152 
    153 // Process 32 pixels and store the result (16b, 24b or 32b per pixel) in *dst.
    154 void VP8YuvToRgba32_SSE2(const uint8_t* WEBP_RESTRICT y,
    155                         const uint8_t* WEBP_RESTRICT u,
    156                         const uint8_t* WEBP_RESTRICT v,
    157                         uint8_t* WEBP_RESTRICT dst);
    158 void VP8YuvToRgb32_SSE2(const uint8_t* WEBP_RESTRICT y,
    159                        const uint8_t* WEBP_RESTRICT u,
    160                        const uint8_t* WEBP_RESTRICT v,
    161                        uint8_t* WEBP_RESTRICT dst);
    162 void VP8YuvToBgra32_SSE2(const uint8_t* WEBP_RESTRICT y,
    163                         const uint8_t* WEBP_RESTRICT u,
    164                         const uint8_t* WEBP_RESTRICT v,
    165                         uint8_t* WEBP_RESTRICT dst);
    166 void VP8YuvToBgr32_SSE2(const uint8_t* WEBP_RESTRICT y,
    167                        const uint8_t* WEBP_RESTRICT u,
    168                        const uint8_t* WEBP_RESTRICT v,
    169                        uint8_t* WEBP_RESTRICT dst);
    170 void VP8YuvToArgb32_SSE2(const uint8_t* WEBP_RESTRICT y,
    171                         const uint8_t* WEBP_RESTRICT u,
    172                         const uint8_t* WEBP_RESTRICT v,
    173                         uint8_t* WEBP_RESTRICT dst);
    174 void VP8YuvToRgba444432_SSE2(const uint8_t* WEBP_RESTRICT y,
    175                             const uint8_t* WEBP_RESTRICT u,
    176                             const uint8_t* WEBP_RESTRICT v,
    177                             uint8_t* WEBP_RESTRICT dst);
    178 void VP8YuvToRgb56532_SSE2(const uint8_t* WEBP_RESTRICT y,
    179                           const uint8_t* WEBP_RESTRICT u,
    180                           const uint8_t* WEBP_RESTRICT v,
    181                           uint8_t* WEBP_RESTRICT dst);
    182 
    183 #endif    // WEBP_USE_SSE2
    184 
    185 //-----------------------------------------------------------------------------
    186 // SSE41 extra functions (mostly for upsampling_sse41.c)
    187 
    188 #if defined(WEBP_USE_SSE41)
    189 
    190 // Process 32 pixels and store the result (16b, 24b or 32b per pixel) in *dst.
    191 void VP8YuvToRgb32_SSE41(const uint8_t* WEBP_RESTRICT y,
    192                         const uint8_t* WEBP_RESTRICT u,
    193                         const uint8_t* WEBP_RESTRICT v,
    194                         uint8_t* WEBP_RESTRICT dst);
    195 void VP8YuvToBgr32_SSE41(const uint8_t* WEBP_RESTRICT y,
    196                         const uint8_t* WEBP_RESTRICT u,
    197                         const uint8_t* WEBP_RESTRICT v,
    198                         uint8_t* WEBP_RESTRICT dst);
    199 
    200 #endif    // WEBP_USE_SSE41
    201 
    202 //------------------------------------------------------------------------------
    203 // RGB -> YUV conversion
    204 
    205 // Stub functions that can be called with various rounding values:
    206 static WEBP_INLINE int VP8ClipUV(int uv, int rounding) {
    207  uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2);
    208  return ((uv & ~0xff) == 0) ? uv : (uv < 0) ? 0 : 255;
    209 }
    210 
    211 static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) {
    212  const int luma = 16839 * r + 33059 * g + 6420 * b;
    213  return (luma + rounding + (16 << YUV_FIX)) >> YUV_FIX;  // no need to clip
    214 }
    215 
    216 static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) {
    217  const int u = -9719 * r - 19081 * g + 28800 * b;
    218  return VP8ClipUV(u, rounding);
    219 }
    220 
    221 static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) {
    222  const int v = +28800 * r - 24116 * g - 4684 * b;
    223  return VP8ClipUV(v, rounding);
    224 }
    225 
    226 #ifdef __cplusplus
    227 }    // extern "C"
    228 #endif
    229 
    230 #endif  // WEBP_DSP_YUV_H_