yuv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // YUV->RGB conversion functions
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./yuv.h"
  14. #include <stdlib.h>
  15. #if defined(WEBP_YUV_USE_TABLE)
  16. static int done = 0;
  17. static WEBP_INLINE uint8_t clip(int v, int max_value) {
  18. return v < 0 ? 0 : v > max_value ? max_value : v;
  19. }
  20. int16_t VP8kVToR[256], VP8kUToB[256];
  21. int32_t VP8kVToG[256], VP8kUToG[256];
  22. uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN];
  23. uint8_t VP8kClip4Bits[YUV_RANGE_MAX - YUV_RANGE_MIN];
  24. WEBP_TSAN_IGNORE_FUNCTION void VP8YUVInit(void) {
  25. int i;
  26. if (done) {
  27. return;
  28. }
  29. #ifndef USE_YUVj
  30. for (i = 0; i < 256; ++i) {
  31. VP8kVToR[i] = (89858 * (i - 128) + YUV_HALF) >> YUV_FIX;
  32. VP8kUToG[i] = -22014 * (i - 128) + YUV_HALF;
  33. VP8kVToG[i] = -45773 * (i - 128);
  34. VP8kUToB[i] = (113618 * (i - 128) + YUV_HALF) >> YUV_FIX;
  35. }
  36. for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) {
  37. const int k = ((i - 16) * 76283 + YUV_HALF) >> YUV_FIX;
  38. VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255);
  39. VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15);
  40. }
  41. #else
  42. for (i = 0; i < 256; ++i) {
  43. VP8kVToR[i] = (91881 * (i - 128) + YUV_HALF) >> YUV_FIX;
  44. VP8kUToG[i] = -22554 * (i - 128) + YUV_HALF;
  45. VP8kVToG[i] = -46802 * (i - 128);
  46. VP8kUToB[i] = (116130 * (i - 128) + YUV_HALF) >> YUV_FIX;
  47. }
  48. for (i = YUV_RANGE_MIN; i < YUV_RANGE_MAX; ++i) {
  49. const int k = i;
  50. VP8kClip[i - YUV_RANGE_MIN] = clip(k, 255);
  51. VP8kClip4Bits[i - YUV_RANGE_MIN] = clip((k + 8) >> 4, 15);
  52. }
  53. #endif
  54. done = 1;
  55. }
  56. #else
  57. WEBP_TSAN_IGNORE_FUNCTION void VP8YUVInit(void) {}
  58. #endif // WEBP_YUV_USE_TABLE
  59. //-----------------------------------------------------------------------------
  60. // Plain-C version
  61. #define ROW_FUNC(FUNC_NAME, FUNC, XSTEP) \
  62. static void FUNC_NAME(const uint8_t* y, \
  63. const uint8_t* u, const uint8_t* v, \
  64. uint8_t* dst, int len) { \
  65. const uint8_t* const end = dst + (len & ~1) * XSTEP; \
  66. while (dst != end) { \
  67. FUNC(y[0], u[0], v[0], dst); \
  68. FUNC(y[1], u[0], v[0], dst + XSTEP); \
  69. y += 2; \
  70. ++u; \
  71. ++v; \
  72. dst += 2 * XSTEP; \
  73. } \
  74. if (len & 1) { \
  75. FUNC(y[0], u[0], v[0], dst); \
  76. } \
  77. } \
  78. // All variants implemented.
  79. ROW_FUNC(YuvToRgbRow, VP8YuvToRgb, 3)
  80. ROW_FUNC(YuvToBgrRow, VP8YuvToBgr, 3)
  81. ROW_FUNC(YuvToRgbaRow, VP8YuvToRgba, 4)
  82. ROW_FUNC(YuvToBgraRow, VP8YuvToBgra, 4)
  83. ROW_FUNC(YuvToArgbRow, VP8YuvToArgb, 4)
  84. ROW_FUNC(YuvToRgba4444Row, VP8YuvToRgba4444, 2)
  85. ROW_FUNC(YuvToRgb565Row, VP8YuvToRgb565, 2)
  86. #undef ROW_FUNC
  87. // Main call for processing a plane with a WebPSamplerRowFunc function:
  88. void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
  89. const uint8_t* u, const uint8_t* v, int uv_stride,
  90. uint8_t* dst, int dst_stride,
  91. int width, int height, WebPSamplerRowFunc func) {
  92. int j;
  93. for (j = 0; j < height; ++j) {
  94. func(y, u, v, dst, width);
  95. y += y_stride;
  96. if (j & 1) {
  97. u += uv_stride;
  98. v += uv_stride;
  99. }
  100. dst += dst_stride;
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Main call
  105. WebPSamplerRowFunc WebPSamplers[MODE_LAST];
  106. extern void WebPInitSamplersSSE2(void);
  107. extern void WebPInitSamplersMIPS32(void);
  108. extern void WebPInitSamplersMIPSdspR2(void);
  109. static volatile VP8CPUInfo yuv_last_cpuinfo_used =
  110. (VP8CPUInfo)&yuv_last_cpuinfo_used;
  111. WEBP_TSAN_IGNORE_FUNCTION void WebPInitSamplers(void) {
  112. if (yuv_last_cpuinfo_used == VP8GetCPUInfo) return;
  113. WebPSamplers[MODE_RGB] = YuvToRgbRow;
  114. WebPSamplers[MODE_RGBA] = YuvToRgbaRow;
  115. WebPSamplers[MODE_BGR] = YuvToBgrRow;
  116. WebPSamplers[MODE_BGRA] = YuvToBgraRow;
  117. WebPSamplers[MODE_ARGB] = YuvToArgbRow;
  118. WebPSamplers[MODE_RGBA_4444] = YuvToRgba4444Row;
  119. WebPSamplers[MODE_RGB_565] = YuvToRgb565Row;
  120. WebPSamplers[MODE_rgbA] = YuvToRgbaRow;
  121. WebPSamplers[MODE_bgrA] = YuvToBgraRow;
  122. WebPSamplers[MODE_Argb] = YuvToArgbRow;
  123. WebPSamplers[MODE_rgbA_4444] = YuvToRgba4444Row;
  124. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  125. if (VP8GetCPUInfo != NULL) {
  126. #if defined(WEBP_USE_SSE2)
  127. if (VP8GetCPUInfo(kSSE2)) {
  128. WebPInitSamplersSSE2();
  129. }
  130. #endif // WEBP_USE_SSE2
  131. #if defined(WEBP_USE_MIPS32)
  132. if (VP8GetCPUInfo(kMIPS32)) {
  133. WebPInitSamplersMIPS32();
  134. }
  135. #endif // WEBP_USE_MIPS32
  136. #if defined(WEBP_USE_MIPS_DSP_R2)
  137. if (VP8GetCPUInfo(kMIPSdspR2)) {
  138. WebPInitSamplersMIPSdspR2();
  139. }
  140. #endif // WEBP_USE_MIPS_DSP_R2
  141. }
  142. yuv_last_cpuinfo_used = VP8GetCPUInfo;
  143. }
  144. //-----------------------------------------------------------------------------
  145. // ARGB -> YUV converters
  146. static void ConvertARGBToY(const uint32_t* argb, uint8_t* y, int width) {
  147. int i;
  148. for (i = 0; i < width; ++i) {
  149. const uint32_t p = argb[i];
  150. y[i] = VP8RGBToY((p >> 16) & 0xff, (p >> 8) & 0xff, (p >> 0) & 0xff,
  151. YUV_HALF);
  152. }
  153. }
  154. void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
  155. int src_width, int do_store) {
  156. // No rounding. Last pixel is dealt with separately.
  157. const int uv_width = src_width >> 1;
  158. int i;
  159. for (i = 0; i < uv_width; ++i) {
  160. const uint32_t v0 = argb[2 * i + 0];
  161. const uint32_t v1 = argb[2 * i + 1];
  162. // VP8RGBToU/V expects four accumulated pixels. Hence we need to
  163. // scale r/g/b value by a factor 2. We just shift v0/v1 one bit less.
  164. const int r = ((v0 >> 15) & 0x1fe) + ((v1 >> 15) & 0x1fe);
  165. const int g = ((v0 >> 7) & 0x1fe) + ((v1 >> 7) & 0x1fe);
  166. const int b = ((v0 << 1) & 0x1fe) + ((v1 << 1) & 0x1fe);
  167. const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
  168. const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
  169. if (do_store) {
  170. u[i] = tmp_u;
  171. v[i] = tmp_v;
  172. } else {
  173. // Approximated average-of-four. But it's an acceptable diff.
  174. u[i] = (u[i] + tmp_u + 1) >> 1;
  175. v[i] = (v[i] + tmp_v + 1) >> 1;
  176. }
  177. }
  178. if (src_width & 1) { // last pixel
  179. const uint32_t v0 = argb[2 * i + 0];
  180. const int r = (v0 >> 14) & 0x3fc;
  181. const int g = (v0 >> 6) & 0x3fc;
  182. const int b = (v0 << 2) & 0x3fc;
  183. const int tmp_u = VP8RGBToU(r, g, b, YUV_HALF << 2);
  184. const int tmp_v = VP8RGBToV(r, g, b, YUV_HALF << 2);
  185. if (do_store) {
  186. u[i] = tmp_u;
  187. v[i] = tmp_v;
  188. } else {
  189. u[i] = (u[i] + tmp_u + 1) >> 1;
  190. v[i] = (v[i] + tmp_v + 1) >> 1;
  191. }
  192. }
  193. }
  194. //-----------------------------------------------------------------------------
  195. static void ConvertRGB24ToY(const uint8_t* rgb, uint8_t* y, int width) {
  196. int i;
  197. for (i = 0; i < width; ++i, rgb += 3) {
  198. y[i] = VP8RGBToY(rgb[0], rgb[1], rgb[2], YUV_HALF);
  199. }
  200. }
  201. static void ConvertBGR24ToY(const uint8_t* bgr, uint8_t* y, int width) {
  202. int i;
  203. for (i = 0; i < width; ++i, bgr += 3) {
  204. y[i] = VP8RGBToY(bgr[2], bgr[1], bgr[0], YUV_HALF);
  205. }
  206. }
  207. void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
  208. uint8_t* u, uint8_t* v, int width) {
  209. int i;
  210. for (i = 0; i < width; i += 1, rgb += 4) {
  211. const int r = rgb[0], g = rgb[1], b = rgb[2];
  212. u[i] = VP8RGBToU(r, g, b, YUV_HALF << 2);
  213. v[i] = VP8RGBToV(r, g, b, YUV_HALF << 2);
  214. }
  215. }
  216. //-----------------------------------------------------------------------------
  217. #define MAX_Y ((1 << 10) - 1) // 10b precision over 16b-arithmetic
  218. static uint16_t clip_y(int v) {
  219. return (v < 0) ? 0 : (v > MAX_Y) ? MAX_Y : (uint16_t)v;
  220. }
  221. static uint64_t SharpYUVUpdateY_C(const uint16_t* ref, const uint16_t* src,
  222. uint16_t* dst, int len) {
  223. uint64_t diff = 0;
  224. int i;
  225. for (i = 0; i < len; ++i) {
  226. const int diff_y = ref[i] - src[i];
  227. const int new_y = (int)dst[i] + diff_y;
  228. dst[i] = clip_y(new_y);
  229. diff += (uint64_t)abs(diff_y);
  230. }
  231. return diff;
  232. }
  233. static void SharpYUVUpdateRGB_C(const int16_t* ref, const int16_t* src,
  234. int16_t* dst, int len) {
  235. int i;
  236. for (i = 0; i < len; ++i) {
  237. const int diff_uv = ref[i] - src[i];
  238. dst[i] += diff_uv;
  239. }
  240. }
  241. static void SharpYUVFilterRow_C(const int16_t* A, const int16_t* B, int len,
  242. const uint16_t* best_y, uint16_t* out) {
  243. int i;
  244. for (i = 0; i < len; ++i, ++A, ++B) {
  245. const int v0 = (A[0] * 9 + A[1] * 3 + B[0] * 3 + B[1] + 8) >> 4;
  246. const int v1 = (A[1] * 9 + A[0] * 3 + B[1] * 3 + B[0] + 8) >> 4;
  247. out[2 * i + 0] = clip_y(best_y[2 * i + 0] + v0);
  248. out[2 * i + 1] = clip_y(best_y[2 * i + 1] + v1);
  249. }
  250. }
  251. #undef MAX_Y
  252. //-----------------------------------------------------------------------------
  253. void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
  254. void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
  255. void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
  256. uint8_t* u, uint8_t* v, int width);
  257. void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
  258. void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
  259. int src_width, int do_store);
  260. uint64_t (*WebPSharpYUVUpdateY)(const uint16_t* ref, const uint16_t* src,
  261. uint16_t* dst, int len);
  262. void (*WebPSharpYUVUpdateRGB)(const int16_t* ref, const int16_t* src,
  263. int16_t* dst, int len);
  264. void (*WebPSharpYUVFilterRow)(const int16_t* A, const int16_t* B, int len,
  265. const uint16_t* best_y, uint16_t* out);
  266. static volatile VP8CPUInfo rgba_to_yuv_last_cpuinfo_used =
  267. (VP8CPUInfo)&rgba_to_yuv_last_cpuinfo_used;
  268. extern void WebPInitConvertARGBToYUVSSE2(void);
  269. extern void WebPInitSharpYUVSSE2(void);
  270. WEBP_TSAN_IGNORE_FUNCTION void WebPInitConvertARGBToYUV(void) {
  271. if (rgba_to_yuv_last_cpuinfo_used == VP8GetCPUInfo) return;
  272. WebPConvertARGBToY = ConvertARGBToY;
  273. WebPConvertARGBToUV = WebPConvertARGBToUV_C;
  274. WebPConvertRGB24ToY = ConvertRGB24ToY;
  275. WebPConvertBGR24ToY = ConvertBGR24ToY;
  276. WebPConvertRGBA32ToUV = WebPConvertRGBA32ToUV_C;
  277. WebPSharpYUVUpdateY = SharpYUVUpdateY_C;
  278. WebPSharpYUVUpdateRGB = SharpYUVUpdateRGB_C;
  279. WebPSharpYUVFilterRow = SharpYUVFilterRow_C;
  280. if (VP8GetCPUInfo != NULL) {
  281. #if defined(WEBP_USE_SSE2)
  282. if (VP8GetCPUInfo(kSSE2)) {
  283. WebPInitConvertARGBToYUVSSE2();
  284. WebPInitSharpYUVSSE2();
  285. }
  286. #endif // WEBP_USE_SSE2
  287. }
  288. rgba_to_yuv_last_cpuinfo_used = VP8GetCPUInfo;
  289. }