1 // Copyright 2011 Google Inc. 2 // 3 // This code is licensed under the same terms as WebM: 4 // Software License Agreement: http://www.webmproject.org/license/software/ 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ 6 // ----------------------------------------------------------------------------- 7 // 8 // SSE2 version of YUV to RGB upsampling functions. 9 // 10 // Author: somnath (at) google.com (Somnath Banerjee) 11 12 #if defined(__SSE2__) || defined(_MSC_VER) 13 14 #include <assert.h> 15 #include <emmintrin.h> 16 #include <string.h> 17 #include "webpi.h" 18 #include "yuv.h" 19 20 #if defined(__cplusplus) || defined(c_plusplus) 21 extern "C" { 22 #endif 23 24 // We compute (9*a + 3*b + 3*c + d + 8) / 16 as follows 25 // u = (9*a + 3*b + 3*c + d + 8) / 16 26 // = (a + (a + 3*b + 3*c + d) / 8 + 1) / 2 27 // = (a + m + 1) / 2 28 // where m = (a + 3*b + 3*c + d) / 8 29 // = ((a + b + c + d) / 2 + b + c) / 4 30 // 31 // Let's say k = (a + b + c + d) / 4. 32 // We can compute k as 33 // k = (s + t + 1) / 2 - ((a^d) | (b^c) | (s^t)) & 1 34 // where s = (a + d + 1) / 2 and t = (b + c + 1) / 2 35 // 36 // Then m can be written as 37 // m = (k + t + 1) / 2 - (((b^c) & (s^t)) | (k^t)) & 1 38 39 // Computes out = (k + in + 1) / 2 - ((ij & (s^t)) | (k^in)) & 1 40 #define GET_M(ij, in, out) do { \ 41 const __m128i tmp0 = _mm_avg_epu8(k, (in)); /* (k + in + 1) / 2 */ \ 42 const __m128i tmp1 = _mm_and_si128((ij), st); /* (ij) & (s^t) */ \ 43 const __m128i tmp2 = _mm_xor_si128(k, (in)); /* (k^in) */ \ 44 const __m128i tmp3 = _mm_or_si128(tmp1, tmp2); /* ((ij) & (s^t)) | (k^in) */\ 45 const __m128i tmp4 = _mm_and_si128(tmp3, one); /* & 1 -> lsb_correction */ \ 46 (out) = _mm_sub_epi8(tmp0, tmp4); /* (k + in + 1) / 2 - lsb_correction */ \ 47 } while (0) 48 49 // pack and store two alterning pixel rows 50 #define PACK_AND_STORE(a, b, da, db, out) do { \ 51 const __m128i ta = _mm_avg_epu8(a, da); /* (9a + 3b + 3c + d + 8) / 16 */ \ 52 const __m128i tb = _mm_avg_epu8(b, db); /* (3a + 9b + c + 3d + 8) / 16 */ \ 53 const __m128i t1 = _mm_unpacklo_epi8(ta, tb); \ 54 const __m128i t2 = _mm_unpackhi_epi8(ta, tb); \ 55 _mm_store_si128(((__m128i*)(out)) + 0, t1); \ 56 _mm_store_si128(((__m128i*)(out)) + 1, t2); \ 57 } while (0) 58 59 // Loads 17 pixels each from rows r1 and r2 and generates 32 pixels. 60 #define UPSAMPLE_32PIXELS(r1, r2, out) { \ 61 const __m128i one = _mm_set1_epi8(1); \ 62 const __m128i a = _mm_loadu_si128((__m128i*)&(r1)[0]); \ 63 const __m128i b = _mm_loadu_si128((__m128i*)&(r1)[1]); \ 64 const __m128i c = _mm_loadu_si128((__m128i*)&(r2)[0]); \ 65 const __m128i d = _mm_loadu_si128((__m128i*)&(r2)[1]); \ 66 \ 67 const __m128i s = _mm_avg_epu8(a, d); /* s = (a + d + 1) / 2 */ \ 68 const __m128i t = _mm_avg_epu8(b, c); /* t = (b + c + 1) / 2 */ \ 69 const __m128i st = _mm_xor_si128(s, t); /* st = s^t */ \ 70 \ 71 const __m128i ad = _mm_xor_si128(a, d); /* ad = a^d */ \ 72 const __m128i bc = _mm_xor_si128(b, c); /* bc = b^c */ \ 73 \ 74 const __m128i t1 = _mm_or_si128(ad, bc); /* (a^d) | (b^c) */ \ 75 const __m128i t2 = _mm_or_si128(t1, st); /* (a^d) | (b^c) | (s^t) */ \ 76 const __m128i t3 = _mm_and_si128(t2, one); /* (a^d) | (b^c) | (s^t) & 1 */ \ 77 const __m128i t4 = _mm_avg_epu8(s, t); \ 78 const __m128i k = _mm_sub_epi8(t4, t3); /* k = (a + b + c + d) / 4 */ \ 79 __m128i diag1, diag2; \ 80 \ 81 GET_M(bc, t, diag1); /* diag1 = (a + 3b + 3c + d) / 8 */ \ 82 GET_M(ad, s, diag2); /* diag2 = (3a + b + c + 3d) / 8 */ \ 83 \ 84 /* pack the alternate pixels */ \ 85 PACK_AND_STORE(a, b, diag1, diag2, &(out)[0 * 32]); \ 86 PACK_AND_STORE(c, d, diag2, diag1, &(out)[2 * 32]); \ 87 } 88 89 // Turn the macro into a function for reducing code-size when non-critical 90 static void Upsample32Pixels(const uint8_t r1[], const uint8_t r2[], 91 uint8_t* const out) { 92 UPSAMPLE_32PIXELS(r1, r2, out); 93 } 94 95 #define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out) { \ 96 uint8_t r1[17], r2[17]; \ 97 memcpy(r1, (tb), (num_pixels)); \ 98 memcpy(r2, (bb), (num_pixels)); \ 99 /* replicate last byte */ \ 100 memset(r1 + (num_pixels), r1[(num_pixels) - 1], 17 - (num_pixels)); \ 101 memset(r2 + (num_pixels), r2[(num_pixels) - 1], 17 - (num_pixels)); \ 102 /* using the shared function instead of the macro saves ~3k code size */ \ 103 Upsample32Pixels(r1, r2, out); \ 104 } 105 106 #define CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, uv, \ 107 top_dst, bottom_dst, cur_x, num_pixels) { \ 108 int n; \ 109 if (top_y) { \ 110 for (n = 0; n < (num_pixels); ++n) { \ 111 FUNC(top_y[(cur_x) + n], (uv)[n], (uv)[32 + n], \ 112 top_dst + ((cur_x) + n) * XSTEP); \ 113 } \ 114 } \ 115 if (bottom_y) { \ 116 for (n = 0; n < (num_pixels); ++n) { \ 117 FUNC(bottom_y[(cur_x) + n], (uv)[64 + n], (uv)[64 + 32 + n], \ 118 bottom_dst + ((cur_x) + n) * XSTEP); \ 119 } \ 120 } \ 121 } 122 123 #define SSE2_UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \ 124 static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \ 125 const uint8_t* top_u, const uint8_t* top_v, \ 126 const uint8_t* cur_u, const uint8_t* cur_v, \ 127 uint8_t* top_dst, uint8_t* bottom_dst, int len) { \ 128 int b; \ 129 /* 16 byte aligned array to cache reconstructed u and v */ \ 130 uint8_t uv_buf[4 * 32 + 15]; \ 131 uint8_t* const r_uv = (uint8_t*)((uintptr_t)(uv_buf + 15) & ~15); \ 132 const int uv_len = (len + 1) >> 1; \ 133 /* 17 pixels must be read-able for each block */ \ 134 const int num_blocks = (uv_len - 1) >> 4; \ 135 const int leftover = uv_len - num_blocks * 16; \ 136 const int last_pos = 1 + 32 * num_blocks; \ 137 \ 138 const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1; \ 139 const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1; \ 140 \ 141 assert(len > 0); \ 142 /* Treat the first pixel in regular way */ \ 143 if (top_y) { \ 144 const int u0 = (top_u[0] + u_diag) >> 1; \ 145 const int v0 = (top_v[0] + v_diag) >> 1; \ 146 FUNC(top_y[0], u0, v0, top_dst); \ 147 } \ 148 if (bottom_y) { \ 149 const int u0 = (cur_u[0] + u_diag) >> 1; \ 150 const int v0 = (cur_v[0] + v_diag) >> 1; \ 151 FUNC(bottom_y[0], u0, v0, bottom_dst); \ 152 } \ 153 \ 154 for (b = 0; b < num_blocks; ++b) { \ 155 UPSAMPLE_32PIXELS(top_u, cur_u, r_uv + 0 * 32); \ 156 UPSAMPLE_32PIXELS(top_v, cur_v, r_uv + 1 * 32); \ 157 CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, r_uv, top_dst, bottom_dst, \ 158 32 * b + 1, 32) \ 159 top_u += 16; \ 160 cur_u += 16; \ 161 top_v += 16; \ 162 cur_v += 16; \ 163 } \ 164 \ 165 UPSAMPLE_LAST_BLOCK(top_u, cur_u, leftover, r_uv + 0 * 32); \ 166 UPSAMPLE_LAST_BLOCK(top_v, cur_v, leftover, r_uv + 1 * 32); \ 167 CONVERT2RGB(FUNC, XSTEP, top_y, bottom_y, r_uv, top_dst, bottom_dst, \ 168 last_pos, len - last_pos); \ 169 } 170 171 // SSE2 variants of the fancy upsampler. 172 SSE2_UPSAMPLE_FUNC(UpsampleRgbLinePairSSE2, VP8YuvToRgb, 3) 173 SSE2_UPSAMPLE_FUNC(UpsampleBgrLinePairSSE2, VP8YuvToBgr, 3) 174 SSE2_UPSAMPLE_FUNC(UpsampleRgbaLinePairSSE2, VP8YuvToRgba, 4) 175 SSE2_UPSAMPLE_FUNC(UpsampleBgraLinePairSSE2, VP8YuvToBgra, 4) 176 // These two don't erase the alpha value 177 SSE2_UPSAMPLE_FUNC(UpsampleRgbKeepAlphaLinePairSSE2, VP8YuvToRgb, 4) 178 SSE2_UPSAMPLE_FUNC(UpsampleBgrKeepAlphaLinePairSSE2, VP8YuvToBgr, 4) 179 180 #undef GET_M 181 #undef PACK_AND_STORE 182 #undef UPSAMPLE_32PIXELS 183 #undef UPSAMPLE_LAST_BLOCK 184 #undef CONVERT2RGB 185 #undef SSE2_UPSAMPLE_FUNC 186 187 //----------------------------------------------------------------------------- 188 189 void WebPInitUpsamplersSSE2(void) { 190 WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePairSSE2; 191 WebPUpsamplers[MODE_RGBA] = UpsampleRgbaLinePairSSE2; 192 WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePairSSE2; 193 WebPUpsamplers[MODE_BGRA] = UpsampleBgraLinePairSSE2; 194 195 WebPUpsamplersKeepAlpha[MODE_RGB] = UpsampleRgbLinePairSSE2; 196 WebPUpsamplersKeepAlpha[MODE_RGBA] = UpsampleRgbKeepAlphaLinePairSSE2; 197 WebPUpsamplersKeepAlpha[MODE_BGR] = UpsampleBgrLinePairSSE2; 198 WebPUpsamplersKeepAlpha[MODE_BGRA] = UpsampleBgrKeepAlphaLinePairSSE2; 199 } 200 201 #if defined(__cplusplus) || defined(c_plusplus) 202 } // extern "C" 203 #endif 204 205 #endif //__SSE2__ || _MSC_VER 206