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