yuv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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: http://en.wikipedia.org/wiki/YCbCr
  14. // Y = 0.2569 * R + 0.5044 * G + 0.0979 * B + 16
  15. // U = -0.1483 * R - 0.2911 * G + 0.4394 * B + 128
  16. // V = 0.4394 * R - 0.3679 * G - 0.0715 * 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.391 * (U-128)
  22. // B = 1.164 * (Y-16) + 2.018 * (U-128)
  23. // where Y is in the [16,235] range, and U/V in the [16,240] range.
  24. // In the table-lookup version (WEBP_YUV_USE_TABLE), the common factor
  25. // "1.164 * (Y-16)" can be handled as an offset in the VP8kClip[] table.
  26. // So in this case the formulae should read:
  27. // R = 1.164 * [Y + 1.371 * (V-128) ] - 18.624
  28. // G = 1.164 * [Y - 0.698 * (V-128) - 0.336 * (U-128)] - 18.624
  29. // B = 1.164 * [Y + 1.733 * (U-128)] - 18.624
  30. // once factorized.
  31. // For YUV->RGB conversion, only 14bit fixed precision is used (YUV_FIX2).
  32. // That's the maximum possible for a convenient ARM implementation.
  33. //
  34. // Author: Skal (pascal.massimino@gmail.com)
  35. #ifndef WEBP_DSP_YUV_H_
  36. #define WEBP_DSP_YUV_H_
  37. #include "./dsp.h"
  38. #include "../dec/decode_vp8.h"
  39. // Define the following to use the LUT-based code:
  40. // #define WEBP_YUV_USE_TABLE
  41. #if defined(WEBP_EXPERIMENTAL_FEATURES)
  42. // Do NOT activate this feature for real compression. This is only experimental!
  43. // This flag is for comparison purpose against JPEG's "YUVj" natural colorspace.
  44. // This colorspace is close to Rec.601's Y'CbCr model with the notable
  45. // difference of allowing larger range for luma/chroma.
  46. // See http://en.wikipedia.org/wiki/YCbCr#JPEG_conversion paragraph, and its
  47. // difference with http://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion
  48. // #define USE_YUVj
  49. #endif
  50. //------------------------------------------------------------------------------
  51. // YUV -> RGB conversion
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. enum {
  56. YUV_FIX = 16, // fixed-point precision for RGB->YUV
  57. YUV_HALF = 1 << (YUV_FIX - 1),
  58. YUV_MASK = (256 << YUV_FIX) - 1,
  59. YUV_RANGE_MIN = -227, // min value of r/g/b output
  60. YUV_RANGE_MAX = 256 + 226, // max value of r/g/b output
  61. YUV_FIX2 = 14, // fixed-point precision for YUV->RGB
  62. YUV_HALF2 = 1 << (YUV_FIX2 - 1),
  63. YUV_MASK2 = (256 << YUV_FIX2) - 1
  64. };
  65. // These constants are 14b fixed-point version of ITU-R BT.601 constants.
  66. #define kYScale 19077 // 1.164 = 255 / 219
  67. #define kVToR 26149 // 1.596 = 255 / 112 * 0.701
  68. #define kUToG 6419 // 0.391 = 255 / 112 * 0.886 * 0.114 / 0.587
  69. #define kVToG 13320 // 0.813 = 255 / 112 * 0.701 * 0.299 / 0.587
  70. #define kUToB 33050 // 2.018 = 255 / 112 * 0.886
  71. #define kRCst (-kYScale * 16 - kVToR * 128 + YUV_HALF2)
  72. #define kGCst (-kYScale * 16 + kUToG * 128 + kVToG * 128 + YUV_HALF2)
  73. #define kBCst (-kYScale * 16 - kUToB * 128 + YUV_HALF2)
  74. //------------------------------------------------------------------------------
  75. #if !defined(WEBP_YUV_USE_TABLE)
  76. // slower on x86 by ~7-8%, but bit-exact with the SSE2 version
  77. static WEBP_INLINE int VP8Clip8(int v) {
  78. return ((v & ~YUV_MASK2) == 0) ? (v >> YUV_FIX2) : (v < 0) ? 0 : 255;
  79. }
  80. static WEBP_INLINE int VP8YUVToR(int y, int v) {
  81. return VP8Clip8(kYScale * y + kVToR * v + kRCst);
  82. }
  83. static WEBP_INLINE int VP8YUVToG(int y, int u, int v) {
  84. return VP8Clip8(kYScale * y - kUToG * u - kVToG * v + kGCst);
  85. }
  86. static WEBP_INLINE int VP8YUVToB(int y, int u) {
  87. return VP8Clip8(kYScale * y + kUToB * u + kBCst);
  88. }
  89. static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v,
  90. uint8_t* const rgb) {
  91. rgb[0] = VP8YUVToR(y, v);
  92. rgb[1] = VP8YUVToG(y, u, v);
  93. rgb[2] = VP8YUVToB(y, u);
  94. }
  95. static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v,
  96. uint8_t* const bgr) {
  97. bgr[0] = VP8YUVToB(y, u);
  98. bgr[1] = VP8YUVToG(y, u, v);
  99. bgr[2] = VP8YUVToR(y, v);
  100. }
  101. static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v,
  102. uint8_t* const rgb) {
  103. const int r = VP8YUVToR(y, v); // 5 usable bits
  104. const int g = VP8YUVToG(y, u, v); // 6 usable bits
  105. const int b = VP8YUVToB(y, u); // 5 usable bits
  106. const int rg = (r & 0xf8) | (g >> 5);
  107. const int gb = ((g << 3) & 0xe0) | (b >> 3);
  108. #ifdef WEBP_SWAP_16BIT_CSP
  109. rgb[0] = gb;
  110. rgb[1] = rg;
  111. #else
  112. rgb[0] = rg;
  113. rgb[1] = gb;
  114. #endif
  115. }
  116. static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v,
  117. uint8_t* const argb) {
  118. const int r = VP8YUVToR(y, v); // 4 usable bits
  119. const int g = VP8YUVToG(y, u, v); // 4 usable bits
  120. const int b = VP8YUVToB(y, u); // 4 usable bits
  121. const int rg = (r & 0xf0) | (g >> 4);
  122. const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits
  123. #ifdef WEBP_SWAP_16BIT_CSP
  124. argb[0] = ba;
  125. argb[1] = rg;
  126. #else
  127. argb[0] = rg;
  128. argb[1] = ba;
  129. #endif
  130. }
  131. #else
  132. // Table-based version, not totally equivalent to the SSE2 version.
  133. // Rounding diff is only +/-1 though.
  134. extern int16_t VP8kVToR[256], VP8kUToB[256];
  135. extern int32_t VP8kVToG[256], VP8kUToG[256];
  136. extern uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
  137. extern uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
  138. static WEBP_INLINE void VP8YuvToRgb(int y, int u, int v,
  139. uint8_t* const rgb) {
  140. const int r_off = VP8kVToR[v];
  141. const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
  142. const int b_off = VP8kUToB[u];
  143. rgb[0] = VP8kClip[y + r_off - YUV_RANGE_MIN];
  144. rgb[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
  145. rgb[2] = VP8kClip[y + b_off - YUV_RANGE_MIN];
  146. }
  147. static WEBP_INLINE void VP8YuvToBgr(int y, int u, int v,
  148. uint8_t* const bgr) {
  149. const int r_off = VP8kVToR[v];
  150. const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
  151. const int b_off = VP8kUToB[u];
  152. bgr[0] = VP8kClip[y + b_off - YUV_RANGE_MIN];
  153. bgr[1] = VP8kClip[y + g_off - YUV_RANGE_MIN];
  154. bgr[2] = VP8kClip[y + r_off - YUV_RANGE_MIN];
  155. }
  156. static WEBP_INLINE void VP8YuvToRgb565(int y, int u, int v,
  157. uint8_t* const rgb) {
  158. const int r_off = VP8kVToR[v];
  159. const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
  160. const int b_off = VP8kUToB[u];
  161. const int rg = ((VP8kClip[y + r_off - YUV_RANGE_MIN] & 0xf8) |
  162. (VP8kClip[y + g_off - YUV_RANGE_MIN] >> 5));
  163. const int gb = (((VP8kClip[y + g_off - YUV_RANGE_MIN] << 3) & 0xe0) |
  164. (VP8kClip[y + b_off - YUV_RANGE_MIN] >> 3));
  165. #ifdef WEBP_SWAP_16BIT_CSP
  166. rgb[0] = gb;
  167. rgb[1] = rg;
  168. #else
  169. rgb[0] = rg;
  170. rgb[1] = gb;
  171. #endif
  172. }
  173. static WEBP_INLINE void VP8YuvToRgba4444(int y, int u, int v,
  174. uint8_t* const argb) {
  175. const int r_off = VP8kVToR[v];
  176. const int g_off = (VP8kVToG[v] + VP8kUToG[u]) >> YUV_FIX;
  177. const int b_off = VP8kUToB[u];
  178. const int rg = ((VP8kClip4Bits[y + r_off - YUV_RANGE_MIN] << 4) |
  179. VP8kClip4Bits[y + g_off - YUV_RANGE_MIN]);
  180. const int ba = (VP8kClip4Bits[y + b_off - YUV_RANGE_MIN] << 4) | 0x0f;
  181. #ifdef WEBP_SWAP_16BIT_CSP
  182. argb[0] = ba;
  183. argb[1] = rg;
  184. #else
  185. argb[0] = rg;
  186. argb[1] = ba;
  187. #endif
  188. }
  189. #endif // WEBP_YUV_USE_TABLE
  190. //-----------------------------------------------------------------------------
  191. // Alpha handling variants
  192. static WEBP_INLINE void VP8YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
  193. uint8_t* const argb) {
  194. argb[0] = 0xff;
  195. VP8YuvToRgb(y, u, v, argb + 1);
  196. }
  197. static WEBP_INLINE void VP8YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
  198. uint8_t* const bgra) {
  199. VP8YuvToBgr(y, u, v, bgra);
  200. bgra[3] = 0xff;
  201. }
  202. static WEBP_INLINE void VP8YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
  203. uint8_t* const rgba) {
  204. VP8YuvToRgb(y, u, v, rgba);
  205. rgba[3] = 0xff;
  206. }
  207. // Must be called before everything, to initialize the tables.
  208. void VP8YUVInit(void);
  209. //-----------------------------------------------------------------------------
  210. // SSE2 extra functions (mostly for upsampling_sse2.c)
  211. #if defined(WEBP_USE_SSE2)
  212. // When the following is defined, tables are initialized statically, adding ~12k
  213. // to the binary size. Otherwise, they are initialized at run-time (small cost).
  214. #define WEBP_YUV_USE_SSE2_TABLES
  215. #if defined(FANCY_UPSAMPLING)
  216. // Process 32 pixels and store the result (24b or 32b per pixel) in *dst.
  217. void VP8YuvToRgba32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  218. uint8_t* dst);
  219. void VP8YuvToRgb32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  220. uint8_t* dst);
  221. void VP8YuvToBgra32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  222. uint8_t* dst);
  223. void VP8YuvToBgr32(const uint8_t* y, const uint8_t* u, const uint8_t* v,
  224. uint8_t* dst);
  225. #endif // FANCY_UPSAMPLING
  226. // Must be called to initialize tables before using the functions.
  227. void VP8YUVInitSSE2(void);
  228. #endif // WEBP_USE_SSE2
  229. //------------------------------------------------------------------------------
  230. // RGB -> YUV conversion
  231. // Stub functions that can be called with various rounding values:
  232. static WEBP_INLINE int VP8ClipUV(int uv, int rounding) {
  233. uv = (uv + rounding + (128 << (YUV_FIX + 2))) >> (YUV_FIX + 2);
  234. return ((uv & ~0xff) == 0) ? uv : (uv < 0) ? 0 : 255;
  235. }
  236. #ifndef USE_YUVj
  237. static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) {
  238. const int luma = 16839 * r + 33059 * g + 6420 * b;
  239. return (luma + rounding + (16 << YUV_FIX)) >> YUV_FIX; // no need to clip
  240. }
  241. static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) {
  242. const int u = -9719 * r - 19081 * g + 28800 * b;
  243. return VP8ClipUV(u, rounding);
  244. }
  245. static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) {
  246. const int v = +28800 * r - 24116 * g - 4684 * b;
  247. return VP8ClipUV(v, rounding);
  248. }
  249. #else
  250. // This JPEG-YUV colorspace, only for comparison!
  251. // These are also 16bit precision coefficients from Rec.601, but with full
  252. // [0..255] output range.
  253. static WEBP_INLINE int VP8RGBToY(int r, int g, int b, int rounding) {
  254. const int luma = 19595 * r + 38470 * g + 7471 * b;
  255. return (luma + rounding) >> YUV_FIX; // no need to clip
  256. }
  257. static WEBP_INLINE int VP8RGBToU(int r, int g, int b, int rounding) {
  258. const int u = -11058 * r - 21710 * g + 32768 * b;
  259. return VP8ClipUV(u, rounding);
  260. }
  261. static WEBP_INLINE int VP8RGBToV(int r, int g, int b, int rounding) {
  262. const int v = 32768 * r - 27439 * g - 5329 * b;
  263. return VP8ClipUV(v, rounding);
  264. }
  265. #endif // USE_YUVj
  266. #ifdef __cplusplus
  267. } // extern "C"
  268. #endif
  269. #endif /* WEBP_DSP_YUV_H_ */