upsampling_mips_dsp_r2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2014 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(s): Branimir Vasic (branimir.vasic@imgtec.com)
  13. // Djordje Pesut (djordje.pesut@imgtec.com)
  14. #include "./dsp.h"
  15. #if defined(WEBP_USE_MIPS_DSP_R2)
  16. #include <assert.h>
  17. #include "./yuv.h"
  18. #define YUV_TO_RGB(Y, U, V, R, G, B) do { \
  19. const int t1 = MultHi(Y, 19077); \
  20. const int t2 = MultHi(V, 13320); \
  21. R = MultHi(V, 26149); \
  22. G = MultHi(U, 6419); \
  23. B = MultHi(U, 33050); \
  24. R = t1 + R; \
  25. G = t1 - G; \
  26. B = t1 + B; \
  27. R = R - 14234; \
  28. G = G - t2 + 8708; \
  29. B = B - 17685; \
  30. __asm__ volatile ( \
  31. "shll_s.w %[" #R "], %[" #R "], 17 \n\t" \
  32. "shll_s.w %[" #G "], %[" #G "], 17 \n\t" \
  33. "shll_s.w %[" #B "], %[" #B "], 17 \n\t" \
  34. "precrqu_s.qb.ph %[" #R "], %[" #R "], $zero \n\t" \
  35. "precrqu_s.qb.ph %[" #G "], %[" #G "], $zero \n\t" \
  36. "precrqu_s.qb.ph %[" #B "], %[" #B "], $zero \n\t" \
  37. "srl %[" #R "], %[" #R "], 24 \n\t" \
  38. "srl %[" #G "], %[" #G "], 24 \n\t" \
  39. "srl %[" #B "], %[" #B "], 24 \n\t" \
  40. : [R]"+r"(R), [G]"+r"(G), [B]"+r"(B) \
  41. : \
  42. ); \
  43. } while (0)
  44. #if !defined(WEBP_REDUCE_CSP)
  45. static WEBP_INLINE void YuvToRgb(int y, int u, int v, uint8_t* const rgb) {
  46. int r, g, b;
  47. YUV_TO_RGB(y, u, v, r, g, b);
  48. rgb[0] = r;
  49. rgb[1] = g;
  50. rgb[2] = b;
  51. }
  52. static WEBP_INLINE void YuvToBgr(int y, int u, int v, uint8_t* const bgr) {
  53. int r, g, b;
  54. YUV_TO_RGB(y, u, v, r, g, b);
  55. bgr[0] = b;
  56. bgr[1] = g;
  57. bgr[2] = r;
  58. }
  59. static WEBP_INLINE void YuvToRgb565(int y, int u, int v, uint8_t* const rgb) {
  60. int r, g, b;
  61. YUV_TO_RGB(y, u, v, r, g, b);
  62. {
  63. const int rg = (r & 0xf8) | (g >> 5);
  64. const int gb = ((g << 3) & 0xe0) | (b >> 3);
  65. #if (WEBP_SWAP_16BIT_CSP == 1)
  66. rgb[0] = gb;
  67. rgb[1] = rg;
  68. #else
  69. rgb[0] = rg;
  70. rgb[1] = gb;
  71. #endif
  72. }
  73. }
  74. static WEBP_INLINE void YuvToRgba4444(int y, int u, int v,
  75. uint8_t* const argb) {
  76. int r, g, b;
  77. YUV_TO_RGB(y, u, v, r, g, b);
  78. {
  79. const int rg = (r & 0xf0) | (g >> 4);
  80. const int ba = (b & 0xf0) | 0x0f; // overwrite the lower 4 bits
  81. #if (WEBP_SWAP_16BIT_CSP == 1)
  82. argb[0] = ba;
  83. argb[1] = rg;
  84. #else
  85. argb[0] = rg;
  86. argb[1] = ba;
  87. #endif
  88. }
  89. }
  90. #endif // WEBP_REDUCE_CSP
  91. //-----------------------------------------------------------------------------
  92. // Alpha handling variants
  93. #if !defined(WEBP_REDUCE_CSP)
  94. static WEBP_INLINE void YuvToArgb(uint8_t y, uint8_t u, uint8_t v,
  95. uint8_t* const argb) {
  96. int r, g, b;
  97. YUV_TO_RGB(y, u, v, r, g, b);
  98. argb[0] = 0xff;
  99. argb[1] = r;
  100. argb[2] = g;
  101. argb[3] = b;
  102. }
  103. #endif // WEBP_REDUCE_CSP
  104. static WEBP_INLINE void YuvToBgra(uint8_t y, uint8_t u, uint8_t v,
  105. uint8_t* const bgra) {
  106. int r, g, b;
  107. YUV_TO_RGB(y, u, v, r, g, b);
  108. bgra[0] = b;
  109. bgra[1] = g;
  110. bgra[2] = r;
  111. bgra[3] = 0xff;
  112. }
  113. static WEBP_INLINE void YuvToRgba(uint8_t y, uint8_t u, uint8_t v,
  114. uint8_t* const rgba) {
  115. int r, g, b;
  116. YUV_TO_RGB(y, u, v, r, g, b);
  117. rgba[0] = r;
  118. rgba[1] = g;
  119. rgba[2] = b;
  120. rgba[3] = 0xff;
  121. }
  122. //------------------------------------------------------------------------------
  123. // Fancy upsampler
  124. #ifdef FANCY_UPSAMPLING
  125. // Given samples laid out in a square as:
  126. // [a b]
  127. // [c d]
  128. // we interpolate u/v as:
  129. // ([9*a + 3*b + 3*c + d 3*a + 9*b + 3*c + d] + [8 8]) / 16
  130. // ([3*a + b + 9*c + 3*d a + 3*b + 3*c + 9*d] [8 8]) / 16
  131. // We process u and v together stashed into 32bit (16bit each).
  132. #define LOAD_UV(u, v) ((u) | ((v) << 16))
  133. #define UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \
  134. static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \
  135. const uint8_t* top_u, const uint8_t* top_v, \
  136. const uint8_t* cur_u, const uint8_t* cur_v, \
  137. uint8_t* top_dst, uint8_t* bottom_dst, int len) { \
  138. int x; \
  139. const int last_pixel_pair = (len - 1) >> 1; \
  140. uint32_t tl_uv = LOAD_UV(top_u[0], top_v[0]); /* top-left sample */ \
  141. uint32_t l_uv = LOAD_UV(cur_u[0], cur_v[0]); /* left-sample */ \
  142. assert(top_y != NULL); \
  143. { \
  144. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  145. FUNC(top_y[0], uv0 & 0xff, (uv0 >> 16), top_dst); \
  146. } \
  147. if (bottom_y != NULL) { \
  148. const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  149. FUNC(bottom_y[0], uv0 & 0xff, (uv0 >> 16), bottom_dst); \
  150. } \
  151. for (x = 1; x <= last_pixel_pair; ++x) { \
  152. const uint32_t t_uv = LOAD_UV(top_u[x], top_v[x]); /* top sample */ \
  153. const uint32_t uv = LOAD_UV(cur_u[x], cur_v[x]); /* sample */ \
  154. /* precompute invariant values associated with first and second diagonals*/\
  155. const uint32_t avg = tl_uv + t_uv + l_uv + uv + 0x00080008u; \
  156. const uint32_t diag_12 = (avg + 2 * (t_uv + l_uv)) >> 3; \
  157. const uint32_t diag_03 = (avg + 2 * (tl_uv + uv)) >> 3; \
  158. { \
  159. const uint32_t uv0 = (diag_12 + tl_uv) >> 1; \
  160. const uint32_t uv1 = (diag_03 + t_uv) >> 1; \
  161. FUNC(top_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
  162. top_dst + (2 * x - 1) * XSTEP); \
  163. FUNC(top_y[2 * x - 0], uv1 & 0xff, (uv1 >> 16), \
  164. top_dst + (2 * x - 0) * XSTEP); \
  165. } \
  166. if (bottom_y != NULL) { \
  167. const uint32_t uv0 = (diag_03 + l_uv) >> 1; \
  168. const uint32_t uv1 = (diag_12 + uv) >> 1; \
  169. FUNC(bottom_y[2 * x - 1], uv0 & 0xff, (uv0 >> 16), \
  170. bottom_dst + (2 * x - 1) * XSTEP); \
  171. FUNC(bottom_y[2 * x + 0], uv1 & 0xff, (uv1 >> 16), \
  172. bottom_dst + (2 * x + 0) * XSTEP); \
  173. } \
  174. tl_uv = t_uv; \
  175. l_uv = uv; \
  176. } \
  177. if (!(len & 1)) { \
  178. { \
  179. const uint32_t uv0 = (3 * tl_uv + l_uv + 0x00020002u) >> 2; \
  180. FUNC(top_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
  181. top_dst + (len - 1) * XSTEP); \
  182. } \
  183. if (bottom_y != NULL) { \
  184. const uint32_t uv0 = (3 * l_uv + tl_uv + 0x00020002u) >> 2; \
  185. FUNC(bottom_y[len - 1], uv0 & 0xff, (uv0 >> 16), \
  186. bottom_dst + (len - 1) * XSTEP); \
  187. } \
  188. } \
  189. }
  190. // All variants implemented.
  191. UPSAMPLE_FUNC(UpsampleRgbaLinePair, YuvToRgba, 4)
  192. UPSAMPLE_FUNC(UpsampleBgraLinePair, YuvToBgra, 4)
  193. #if !defined(WEBP_REDUCE_CSP)
  194. UPSAMPLE_FUNC(UpsampleRgbLinePair, YuvToRgb, 3)
  195. UPSAMPLE_FUNC(UpsampleBgrLinePair, YuvToBgr, 3)
  196. UPSAMPLE_FUNC(UpsampleArgbLinePair, YuvToArgb, 4)
  197. UPSAMPLE_FUNC(UpsampleRgba4444LinePair, YuvToRgba4444, 2)
  198. UPSAMPLE_FUNC(UpsampleRgb565LinePair, YuvToRgb565, 2)
  199. #endif // WEBP_REDUCE_CSP
  200. #undef LOAD_UV
  201. #undef UPSAMPLE_FUNC
  202. //------------------------------------------------------------------------------
  203. // Entry point
  204. extern void WebPInitUpsamplersMIPSdspR2(void);
  205. WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersMIPSdspR2(void) {
  206. WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePair;
  207. WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePair;
  208. WebPUpsamplers[MODE_rgbA] = UpsampleRgbaLinePair;
  209. WebPUpsamplers[MODE_bgrA] = UpsampleBgraLinePair;
  210. #if !defined(WEBP_REDUCE_CSP)
  211. WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair;
  212. WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair;
  213. WebPUpsamplers[MODE_ARGB] = UpsampleArgbLinePair;
  214. WebPUpsamplers[MODE_RGBA_4444] = UpsampleRgba4444LinePair;
  215. WebPUpsamplers[MODE_RGB_565] = UpsampleRgb565LinePair;
  216. WebPUpsamplers[MODE_Argb] = UpsampleArgbLinePair;
  217. WebPUpsamplers[MODE_rgbA_4444] = UpsampleRgba4444LinePair;
  218. #endif // WEBP_REDUCE_CSP
  219. }
  220. #endif // FANCY_UPSAMPLING
  221. //------------------------------------------------------------------------------
  222. // YUV444 converter
  223. #define YUV444_FUNC(FUNC_NAME, FUNC, XSTEP) \
  224. static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \
  225. uint8_t* dst, int len) { \
  226. int i; \
  227. for (i = 0; i < len; ++i) FUNC(y[i], u[i], v[i], &dst[i * XSTEP]); \
  228. }
  229. YUV444_FUNC(Yuv444ToRgba, YuvToRgba, 4)
  230. YUV444_FUNC(Yuv444ToBgra, YuvToBgra, 4)
  231. #if !defined(WEBP_REDUCE_CSP)
  232. YUV444_FUNC(Yuv444ToRgb, YuvToRgb, 3)
  233. YUV444_FUNC(Yuv444ToBgr, YuvToBgr, 3)
  234. YUV444_FUNC(Yuv444ToArgb, YuvToArgb, 4)
  235. YUV444_FUNC(Yuv444ToRgba4444, YuvToRgba4444, 2)
  236. YUV444_FUNC(Yuv444ToRgb565, YuvToRgb565, 2)
  237. #endif // WEBP_REDUCE_CSP
  238. #undef YUV444_FUNC
  239. //------------------------------------------------------------------------------
  240. // Entry point
  241. extern void WebPInitYUV444ConvertersMIPSdspR2(void);
  242. WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444ConvertersMIPSdspR2(void) {
  243. WebPYUV444Converters[MODE_RGBA] = Yuv444ToRgba;
  244. WebPYUV444Converters[MODE_BGRA] = Yuv444ToBgra;
  245. WebPYUV444Converters[MODE_rgbA] = Yuv444ToRgba;
  246. WebPYUV444Converters[MODE_bgrA] = Yuv444ToBgra;
  247. #if !defined(WEBP_REDUCE_CSP)
  248. WebPYUV444Converters[MODE_RGB] = Yuv444ToRgb;
  249. WebPYUV444Converters[MODE_BGR] = Yuv444ToBgr;
  250. WebPYUV444Converters[MODE_ARGB] = Yuv444ToArgb;
  251. WebPYUV444Converters[MODE_RGBA_4444] = Yuv444ToRgba4444;
  252. WebPYUV444Converters[MODE_RGB_565] = Yuv444ToRgb565;
  253. WebPYUV444Converters[MODE_Argb] = Yuv444ToArgb;
  254. WebPYUV444Converters[MODE_rgbA_4444] = Yuv444ToRgba4444;
  255. #endif // WEBP_REDUCE_CSP
  256. }
  257. #else // !WEBP_USE_MIPS_DSP_R2
  258. WEBP_DSP_INIT_STUB(WebPInitYUV444ConvertersMIPSdspR2)
  259. #endif // WEBP_USE_MIPS_DSP_R2
  260. #if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_MIPS_DSP_R2))
  261. WEBP_DSP_INIT_STUB(WebPInitUpsamplersMIPSdspR2)
  262. #endif