upsampling.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright 2011 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 to RGB upsampling functions.
  11. //
  12. // Author: somnath@google.com (Somnath Banerjee)
  13. #include "./dsp.h"
  14. #include "./yuv.h"
  15. #include <assert.h>
  16. //------------------------------------------------------------------------------
  17. // Fancy upsampler
  18. #ifdef FANCY_UPSAMPLING
  19. // Fancy upsampling functions to convert YUV to RGB
  20. WebPUpsampleLinePairFunc WebPUpsamplers[MODE_LAST];
  21. // Given samples laid out in a square as:
  22. // [a b]
  23. // [c d]
  24. // we interpolate u/v as:
  25. // ([9*a + 3*b + 3*c + d 3*a + 9*b + 3*c + d] + [8 8]) / 16
  26. // ([3*a + b + 9*c + 3*d a + 3*b + 3*c + 9*d] [8 8]) / 16
  27. // We process u and v together stashed into 32bit (16bit each).
  28. #define LOAD_UV(u, v) ((u) | ((v) << 16))
  29. #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
  30. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
  31. const uint8_t* top_u, const uint8_t* top_v, \
  32. const uint8_t* cur_u, const uint8_t* cur_v, \
  33. uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
  34. int x; \
  35. const int last_pixel_pair = (len - 1) >> 1; \
  36. uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]); /* top-left sample */ \
  37. uint32_t l_uv = LOAD_UV(cur_u[0], cur_v[0]); /* left-sample */ \
  38. assert(top_y != NULL); \
  39. { \
  40. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  41. FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \
  42. } \
  43. if (bottom_y != NULL) { \
  44. const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  45. FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst); \
  46. } \
  47. for (x = 1; x <= last_pixel_pair; ++x) { \
  48. const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]); /* top sample */ \
  49. const uint32_t uv = LOAD_UV(cur_u[x], cur_v[x]); /* sample */ \
  50. /* precompute invariant values associated with first and second diagonals*/\
  51. const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u; \
  52. const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3; \
  53. const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3; \
  54. { \
  55. const uint32_t uv0 = (diag_12 + tl_uv) >> 1; \
  56. const uint32_t uv1 = (diag_03 + t_uv) >> 1; \
  57. FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
  58. top_dst + (2 * x - 1) * (XSTEP)); \
  59. FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16), \
  60. top_dst + (2 * x - 0) * (XSTEP)); \
  61. } \
  62. if (bottom_y != NULL) { \
  63. const uint32_t uv0 = (diag_03 + l_uv) >> 1; \
  64. const uint32_t uv1 = (diag_12 + uv) >> 1; \
  65. FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
  66. bottom_dst + (2 * x - 1) * (XSTEP)); \
  67. FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16), \
  68. bottom_dst + (2 * x + 0) * (XSTEP)); \
  69. } \
  70. tl_uv = t_uv; \
  71. l_uv = uv; \
  72. } \
  73. if (!(len & 1)) { \
  74. { \
  75. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  76. FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
  77. top_dst + (len - 1) * (XSTEP)); \
  78. } \
  79. if (bottom_y != NULL) { \
  80. const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  81. FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
  82. bottom_dst + (len - 1) * (XSTEP)); \
  83. } \
  84. } \
  85. }
  86. // All variants implemented.
  87. #if !WEBP_NEON_OMIT_C_CODE
  88. UPSAMPLE_FUNC(UpsampleRgbaLinePair_C, VP8YuvToRgba, 4)
  89. UPSAMPLE_FUNC(UpsampleBgraLinePair_C, VP8YuvToBgra, 4)
  90. #if !defined(WEBP_REDUCE_CSP)
  91. UPSAMPLE_FUNC(UpsampleArgbLinePair_C, VP8YuvToArgb, 4)
  92. UPSAMPLE_FUNC(UpsampleRgbLinePair_C, VP8YuvToRgb, 3)
  93. UPSAMPLE_FUNC(UpsampleBgrLinePair_C, VP8YuvToBgr, 3)
  94. UPSAMPLE_FUNC(UpsampleRgba4444LinePair_C, VP8YuvToRgba4444, 2)
  95. UPSAMPLE_FUNC(UpsampleRgb565LinePair_C, VP8YuvToRgb565, 2)
  96. #else
  97. static void EmptyUpsampleFunc(const uint8_t* top_y, const uint8_t* bottom_y,
  98. const uint8_t* top_u, const uint8_t* top_v,
  99. const uint8_t* cur_u, const uint8_t* cur_v,
  100. uint8_t* top_dst, uint8_t* bottom_dst, int len) {
  101. (void)top_y;
  102. (void)bottom_y;
  103. (void)top_u;
  104. (void)top_v;
  105. (void)cur_u;
  106. (void)cur_v;
  107. (void)top_dst;
  108. (void)bottom_dst;
  109. (void)len;
  110. assert(0); // COLORSPACE SUPPORT NOT COMPILED
  111. }
  112. #define UpsampleArgbLinePair_C EmptyUpsampleFunc
  113. #define UpsampleRgbLinePair_C EmptyUpsampleFunc
  114. #define UpsampleBgrLinePair_C EmptyUpsampleFunc
  115. #define UpsampleRgba4444LinePair_C EmptyUpsampleFunc
  116. #define UpsampleRgb565LinePair_C EmptyUpsampleFunc
  117. #endif // WEBP_REDUCE_CSP
  118. #endif
  119. #undef LOAD_UV
  120. #undef UPSAMPLE_FUNC
  121. #endif // FANCY_UPSAMPLING
  122. //------------------------------------------------------------------------------
  123. #if !defined(FANCY_UPSAMPLING)
  124. #define DUAL_SAMPLE_FUNC(FUNC_NAME, FUNC) \
  125. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bot_y, \
  126. const uint8_t* top_u, const uint8_t* top_v, \
  127. const uint8_t* bot_u, const uint8_t* bot_v, \
  128. uint8_t* top_dst, uint8_t* bot_dst, int len) { \
  129. const int half_len = len >> 1; \
  130. int x; \
  131. assert(top_dst != NULL); \
  132. { \
  133. for (x = 0; x < half_len; ++x) { \
  134. FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x + 0); \
  135. FUNC(top_y[2 * x + 1], top_u[x], top_v[x], top_dst + 8 * x + 4); \
  136. } \
  137. if (len & 1) FUNC(top_y[2 * x + 0], top_u[x], top_v[x], top_dst + 8 * x); \
  138. } \
  139. if (bot_dst != NULL) { \
  140. for (x = 0; x < half_len; ++x) { \
  141. FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x + 0); \
  142. FUNC(bot_y[2 * x + 1], bot_u[x], bot_v[x], bot_dst + 8 * x + 4); \
  143. } \
  144. if (len & 1) FUNC(bot_y[2 * x + 0], bot_u[x], bot_v[x], bot_dst + 8 * x); \
  145. } \
  146. }
  147. DUAL_SAMPLE_FUNC(DualLineSamplerBGRA, VP8YuvToBgra)
  148. DUAL_SAMPLE_FUNC(DualLineSamplerARGB, VP8YuvToArgb)
  149. #undef DUAL_SAMPLE_FUNC
  150. #endif // !FANCY_UPSAMPLING
  151. WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last) {
  152. WebPInitUpsamplers();
  153. #ifdef FANCY_UPSAMPLING
  154. return WebPUpsamplers[alpha_is_last ? MODE_BGRA : MODE_ARGB];
  155. #else
  156. return (alpha_is_last ? DualLineSamplerBGRA : DualLineSamplerARGB);
  157. #endif
  158. }
  159. //------------------------------------------------------------------------------
  160. // YUV444 converter
  161. #define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP) \
  162. extern void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
  163. uint8_t* dst, int len); \
  164. void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
  165. uint8_t* dst, int len) { \
  166. int i; \
  167. for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * (XSTEP)]); \
  168. }
  169. YUV444_FUNC(WebPYuv444ToRgba_C, VP8YuvToRgba, 4)
  170. YUV444_FUNC(WebPYuv444ToBgra_C, VP8YuvToBgra, 4)
  171. #if !defined(WEBP_REDUCE_CSP)
  172. YUV444_FUNC(WebPYuv444ToRgb_C, VP8YuvToRgb, 3)
  173. YUV444_FUNC(WebPYuv444ToBgr_C, VP8YuvToBgr, 3)
  174. YUV444_FUNC(WebPYuv444ToArgb_C, VP8YuvToArgb, 4)
  175. YUV444_FUNC(WebPYuv444ToRgba4444_C, VP8YuvToRgba4444, 2)
  176. YUV444_FUNC(WebPYuv444ToRgb565_C, VP8YuvToRgb565, 2)
  177. #else
  178. static void EmptyYuv444Func(const uint8_t* y,
  179. const uint8_t* u, const uint8_t* v,
  180. uint8_t* dst, int len) {
  181. (void)y;
  182. (void)u;
  183. (void)v;
  184. (void)dst;
  185. (void)len;
  186. }
  187. #define WebPYuv444ToRgb_C EmptyYuv444Func
  188. #define WebPYuv444ToBgr_C EmptyYuv444Func
  189. #define WebPYuv444ToArgb_C EmptyYuv444Func
  190. #define WebPYuv444ToRgba4444_C EmptyYuv444Func
  191. #define WebPYuv444ToRgb565_C EmptyYuv444Func
  192. #endif // WEBP_REDUCE_CSP
  193. #undef YUV444_FUNC
  194. WebPYUV444Converter WebPYUV444Converters[MODE_LAST];
  195. extern void WebPInitYUV444ConvertersMIPSdspR2(void);
  196. extern void WebPInitYUV444ConvertersSSE2(void);
  197. extern void WebPInitYUV444ConvertersSSE41(void);
  198. WEBP_DSP_INIT_FUNC(WebPInitYUV444Converters) {
  199. WebPYUV444Converters[MODE_RGBA] = WebPYuv444ToRgba_C;
  200. WebPYUV444Converters[MODE_BGRA] = WebPYuv444ToBgra_C;
  201. WebPYUV444Converters[MODE_RGB] = WebPYuv444ToRgb_C;
  202. WebPYUV444Converters[MODE_BGR] = WebPYuv444ToBgr_C;
  203. WebPYUV444Converters[MODE_ARGB] = WebPYuv444ToArgb_C;
  204. WebPYUV444Converters[MODE_RGBA_4444] = WebPYuv444ToRgba4444_C;
  205. WebPYUV444Converters[MODE_RGB_565] = WebPYuv444ToRgb565_C;
  206. WebPYUV444Converters[MODE_rgbA] = WebPYuv444ToRgba_C;
  207. WebPYUV444Converters[MODE_bgrA] = WebPYuv444ToBgra_C;
  208. WebPYUV444Converters[MODE_Argb] = WebPYuv444ToArgb_C;
  209. WebPYUV444Converters[MODE_rgbA_4444] = WebPYuv444ToRgba4444_C;
  210. if (VP8GetCPUInfo != NULL) {
  211. #if defined(WEBP_HAVE_SSE2)
  212. if (VP8GetCPUInfo(kSSE2)) {
  213. WebPInitYUV444ConvertersSSE2();
  214. }
  215. #endif
  216. #if defined(WEBP_HAVE_SSE41)
  217. if (VP8GetCPUInfo(kSSE4_1)) {
  218. WebPInitYUV444ConvertersSSE41();
  219. }
  220. #endif
  221. #if defined(WEBP_USE_MIPS_DSP_R2)
  222. if (VP8GetCPUInfo(kMIPSdspR2)) {
  223. WebPInitYUV444ConvertersMIPSdspR2();
  224. }
  225. #endif
  226. }
  227. }
  228. //------------------------------------------------------------------------------
  229. // Main calls
  230. extern void WebPInitUpsamplersSSE2(void);
  231. extern void WebPInitUpsamplersSSE41(void);
  232. extern void WebPInitUpsamplersNEON(void);
  233. extern void WebPInitUpsamplersMIPSdspR2(void);
  234. extern void WebPInitUpsamplersMSA(void);
  235. WEBP_DSP_INIT_FUNC(WebPInitUpsamplers) {
  236. #ifdef FANCY_UPSAMPLING
  237. #if !WEBP_NEON_OMIT_C_CODE
  238. WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair_C;
  239. WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair_C;
  240. WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair_C;
  241. WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair_C;
  242. WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair_C;
  243. WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair_C;
  244. WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair_C;
  245. WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair_C;
  246. WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair_C;
  247. WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair_C;
  248. WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair_C;
  249. #endif
  250. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  251. if (VP8GetCPUInfo != NULL) {
  252. #if defined(WEBP_HAVE_SSE2)
  253. if (VP8GetCPUInfo(kSSE2)) {
  254. WebPInitUpsamplersSSE2();
  255. }
  256. #endif
  257. #if defined(WEBP_HAVE_SSE41)
  258. if (VP8GetCPUInfo(kSSE4_1)) {
  259. WebPInitUpsamplersSSE41();
  260. }
  261. #endif
  262. #if defined(WEBP_USE_MIPS_DSP_R2)
  263. if (VP8GetCPUInfo(kMIPSdspR2)) {
  264. WebPInitUpsamplersMIPSdspR2();
  265. }
  266. #endif
  267. #if defined(WEBP_USE_MSA)
  268. if (VP8GetCPUInfo(kMSA)) {
  269. WebPInitUpsamplersMSA();
  270. }
  271. #endif
  272. }
  273. #if defined(WEBP_HAVE_NEON)
  274. if (WEBP_NEON_OMIT_C_CODE ||
  275. (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
  276. WebPInitUpsamplersNEON();
  277. }
  278. #endif
  279. assert(WebPUpsamplers[MODE_RGBA] != NULL);
  280. assert(WebPUpsamplers[MODE_BGRA] != NULL);
  281. assert(WebPUpsamplers[MODE_rgbA] != NULL);
  282. assert(WebPUpsamplers[MODE_bgrA] != NULL);
  283. #if !defined(WEBP_REDUCE_CSP) || !WEBP_NEON_OMIT_C_CODE
  284. assert(WebPUpsamplers[MODE_RGB] != NULL);
  285. assert(WebPUpsamplers[MODE_BGR] != NULL);
  286. assert(WebPUpsamplers[MODE_ARGB] != NULL);
  287. assert(WebPUpsamplers[MODE_RGBA_4444] != NULL);
  288. assert(WebPUpsamplers[MODE_RGB_565] != NULL);
  289. assert(WebPUpsamplers[MODE_Argb] != NULL);
  290. assert(WebPUpsamplers[MODE_rgbA_4444] != NULL);
  291. #endif
  292. #endif // FANCY_UPSAMPLING
  293. }
  294. //------------------------------------------------------------------------------