yuv.c 11 KB

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