| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "src/dsp/dsp.h" |
| 15 |
|
| 16 |
#if defined(WEBP_USE_SSE41) |
| 17 |
|
| 18 |
#include <assert.h> |
| 19 |
#include <smmintrin.h> |
| 20 |
#include <string.h> |
| 21 |
#include "src/dsp/yuv.h" |
| 22 |
|
| 23 |
#ifdef FANCY_UPSAMPLING |
| 24 |
|
| 25 |
#if !defined(WEBP_REDUCE_CSP) |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define GET_M(ij, in, out) do { \ |
| 44 |
const __m128i tmp0 = _mm_avg_epu8(k, (in)); \ |
| 45 |
const __m128i tmp1 = _mm_and_si128((ij), st); \ |
| 46 |
const __m128i tmp2 = _mm_xor_si128(k, (in)); \ |
| 47 |
const __m128i tmp3 = _mm_or_si128(tmp1, tmp2); \ |
| 48 |
const __m128i tmp4 = _mm_and_si128(tmp3, one); \ |
| 49 |
(out) = _mm_sub_epi8(tmp0, tmp4); \ |
| 50 |
} while (0) |
| 51 |
|
| 52 |
|
| 53 |
#define PACK_AND_STORE(a, b, da, db, out) do { \ |
| 54 |
const __m128i t_a = _mm_avg_epu8(a, da); \ |
| 55 |
const __m128i t_b = _mm_avg_epu8(b, db); \ |
| 56 |
const __m128i t_1 = _mm_unpacklo_epi8(t_a, t_b); \ |
| 57 |
const __m128i t_2 = _mm_unpackhi_epi8(t_a, t_b); \ |
| 58 |
_mm_store_si128(((__m128i*)(out)) + 0, t_1); \ |
| 59 |
_mm_store_si128(((__m128i*)(out)) + 1, t_2); \ |
| 60 |
} while (0) |
| 61 |
|
| 62 |
|
| 63 |
#define UPSAMPLE_32PIXELS(r1, r2, out) do { \ |
| 64 |
const __m128i one = _mm_set1_epi8(1); \ |
| 65 |
const __m128i a = _mm_loadu_si128((const __m128i*)&(r1)[0]); \ |
| 66 |
const __m128i b = _mm_loadu_si128((const __m128i*)&(r1)[1]); \ |
| 67 |
const __m128i c = _mm_loadu_si128((const __m128i*)&(r2)[0]); \ |
| 68 |
const __m128i d = _mm_loadu_si128((const __m128i*)&(r2)[1]); \ |
| 69 |
\ |
| 70 |
const __m128i s = _mm_avg_epu8(a, d); \ |
| 71 |
const __m128i t = _mm_avg_epu8(b, c); \ |
| 72 |
const __m128i st = _mm_xor_si128(s, t); \ |
| 73 |
\ |
| 74 |
const __m128i ad = _mm_xor_si128(a, d); \ |
| 75 |
const __m128i bc = _mm_xor_si128(b, c); \ |
| 76 |
\ |
| 77 |
const __m128i t1 = _mm_or_si128(ad, bc); \ |
| 78 |
const __m128i t2 = _mm_or_si128(t1, st); \ |
| 79 |
const __m128i t3 = _mm_and_si128(t2, one); \ |
| 80 |
const __m128i t4 = _mm_avg_epu8(s, t); \ |
| 81 |
const __m128i k = _mm_sub_epi8(t4, t3); \ |
| 82 |
__m128i diag1, diag2; \ |
| 83 |
\ |
| 84 |
GET_M(bc, t, diag1); \ |
| 85 |
GET_M(ad, s, diag2); \ |
| 86 |
\ |
| 87 |
\ |
| 88 |
PACK_AND_STORE(a, b, diag1, diag2, (out) + 0); \ |
| 89 |
PACK_AND_STORE(c, d, diag2, diag1, (out) + 2 * 32); \ |
| 90 |
} while (0) |
| 91 |
|
| 92 |
|
| 93 |
static void Upsample32Pixels_SSE41(const uint8_t r1[], const uint8_t r2[], |
| 94 |
uint8_t* const out) { |
| 95 |
UPSAMPLE_32PIXELS(r1, r2, out); |
| 96 |
} |
| 97 |
|
| 98 |
#define UPSAMPLE_LAST_BLOCK(tb, bb, num_pixels, out) { \ |
| 99 |
uint8_t r1[17], r2[17]; \ |
| 100 |
memcpy(r1, (tb), (num_pixels)); \ |
| 101 |
memcpy(r2, (bb), (num_pixels)); \ |
| 102 |
\ |
| 103 |
memset(r1 + (num_pixels), r1[(num_pixels) - 1], 17 - (num_pixels)); \ |
| 104 |
memset(r2 + (num_pixels), r2[(num_pixels) - 1], 17 - (num_pixels)); \ |
| 105 |
\ |
| 106 |
Upsample32Pixels_SSE41(r1, r2, out); \ |
| 107 |
} |
| 108 |
|
| 109 |
#define CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, \ |
| 110 |
top_dst, bottom_dst, cur_x) do { \ |
| 111 |
FUNC##32_SSE41((top_y) + (cur_x), r_u, r_v, (top_dst) + (cur_x) * (XSTEP)); \ |
| 112 |
if ((bottom_y) != NULL) { \ |
| 113 |
FUNC##32_SSE41((bottom_y) + (cur_x), r_u + 64, r_v + 64, \ |
| 114 |
(bottom_dst) + (cur_x) * (XSTEP)); \ |
| 115 |
} \ |
| 116 |
} while (0) |
| 117 |
|
| 118 |
#define SSE4_UPSAMPLE_FUNC(FUNC_NAME, FUNC, XSTEP) \ |
| 119 |
static void FUNC_NAME(const uint8_t* top_y, const uint8_t* bottom_y, \ |
| 120 |
const uint8_t* top_u, const uint8_t* top_v, \ |
| 121 |
const uint8_t* cur_u, const uint8_t* cur_v, \ |
| 122 |
uint8_t* top_dst, uint8_t* bottom_dst, int len) { \ |
| 123 |
int uv_pos, pos; \ |
| 124 |
\ |
| 125 |
uint8_t uv_buf[14 * 32 + 15] = { 0 }; \ |
| 126 |
uint8_t* const r_u = (uint8_t*)((uintptr_t)(uv_buf + 15) & ~15); \ |
| 127 |
uint8_t* const r_v = r_u + 32; \ |
| 128 |
\ |
| 129 |
assert(top_y != NULL); \ |
| 130 |
{ \ |
| 131 |
const int u_diag = ((top_u[0] + cur_u[0]) >> 1) + 1; \ |
| 132 |
const int v_diag = ((top_v[0] + cur_v[0]) >> 1) + 1; \ |
| 133 |
const int u0_t = (top_u[0] + u_diag) >> 1; \ |
| 134 |
const int v0_t = (top_v[0] + v_diag) >> 1; \ |
| 135 |
FUNC(top_y[0], u0_t, v0_t, top_dst); \ |
| 136 |
if (bottom_y != NULL) { \ |
| 137 |
const int u0_b = (cur_u[0] + u_diag) >> 1; \ |
| 138 |
const int v0_b = (cur_v[0] + v_diag) >> 1; \ |
| 139 |
FUNC(bottom_y[0], u0_b, v0_b, bottom_dst); \ |
| 140 |
} \ |
| 141 |
} \ |
| 142 |
\ |
| 143 |
for (pos = 1, uv_pos = 0; pos + 32 + 1 <= len; pos += 32, uv_pos += 16) { \ |
| 144 |
UPSAMPLE_32PIXELS(top_u + uv_pos, cur_u + uv_pos, r_u); \ |
| 145 |
UPSAMPLE_32PIXELS(top_v + uv_pos, cur_v + uv_pos, r_v); \ |
| 146 |
CONVERT2RGB_32(FUNC, XSTEP, top_y, bottom_y, top_dst, bottom_dst, pos); \ |
| 147 |
} \ |
| 148 |
if (len > 1) { \ |
| 149 |
const int left_over = ((len + 1) >> 1) - (pos >> 1); \ |
| 150 |
uint8_t* const tmp_top_dst = r_u + 4 * 32; \ |
| 151 |
uint8_t* const tmp_bottom_dst = tmp_top_dst + 4 * 32; \ |
| 152 |
uint8_t* const tmp_top = tmp_bottom_dst + 4 * 32; \ |
| 153 |
uint8_t* const tmp_bottom = (bottom_y == NULL) ? NULL : tmp_top + 32; \ |
| 154 |
assert(left_over > 0); \ |
| 155 |
UPSAMPLE_LAST_BLOCK(top_u + uv_pos, cur_u + uv_pos, left_over, r_u); \ |
| 156 |
UPSAMPLE_LAST_BLOCK(top_v + uv_pos, cur_v + uv_pos, left_over, r_v); \ |
| 157 |
memcpy(tmp_top, top_y + pos, len - pos); \ |
| 158 |
if (bottom_y != NULL) memcpy(tmp_bottom, bottom_y + pos, len - pos); \ |
| 159 |
CONVERT2RGB_32(FUNC, XSTEP, tmp_top, tmp_bottom, tmp_top_dst, \ |
| 160 |
tmp_bottom_dst, 0); \ |
| 161 |
memcpy(top_dst + pos * (XSTEP), tmp_top_dst, (len - pos) * (XSTEP)); \ |
| 162 |
if (bottom_y != NULL) { \ |
| 163 |
memcpy(bottom_dst + pos * (XSTEP), tmp_bottom_dst, \ |
| 164 |
(len - pos) * (XSTEP)); \ |
| 165 |
} \ |
| 166 |
} \ |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
SSE4_UPSAMPLE_FUNC(UpsampleRgbLinePair_SSE41, VP8YuvToRgb, 3) |
| 171 |
SSE4_UPSAMPLE_FUNC(UpsampleBgrLinePair_SSE41, VP8YuvToBgr, 3) |
| 172 |
|
| 173 |
#undef GET_M |
| 174 |
#undef PACK_AND_STORE |
| 175 |
#undef UPSAMPLE_32PIXELS |
| 176 |
#undef UPSAMPLE_LAST_BLOCK |
| 177 |
#undef CONVERT2RGB |
| 178 |
#undef CONVERT2RGB_32 |
| 179 |
#undef SSE4_UPSAMPLE_FUNC |
| 180 |
|
| 181 |
#endif |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
extern WebPUpsampleLinePairFunc WebPUpsamplers[]; |
| 187 |
|
| 188 |
extern void WebPInitUpsamplersSSE41(void); |
| 189 |
|
| 190 |
WEBP_TSAN_IGNORE_FUNCTION void WebPInitUpsamplersSSE41(void) { |
| 191 |
#if !defined(WEBP_REDUCE_CSP) |
| 192 |
WebPUpsamplers[MODE_RGB] = UpsampleRgbLinePair_SSE41; |
| 193 |
WebPUpsamplers[MODE_BGR] = UpsampleBgrLinePair_SSE41; |
| 194 |
#endif |
| 195 |
} |
| 196 |
|
| 197 |
#endif |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
extern WebPYUV444Converter WebPYUV444Converters[]; |
| 202 |
extern void WebPInitYUV444ConvertersSSE41(void); |
| 203 |
|
| 204 |
#define YUV444_FUNC(FUNC_NAME, CALL, CALL_C, XSTEP) \ |
| 205 |
extern void CALL_C(const uint8_t* y, const uint8_t* u, const uint8_t* v, \ |
| 206 |
uint8_t* dst, int len); \ |
| 207 |
static void FUNC_NAME(const uint8_t* y, const uint8_t* u, const uint8_t* v, \ |
| 208 |
uint8_t* dst, int len) { \ |
| 209 |
int i; \ |
| 210 |
const int max_len = len & ~31; \ |
| 211 |
for (i = 0; i < max_len; i += 32) { \ |
| 212 |
CALL(y + i, u + i, v + i, dst + i * (XSTEP)); \ |
| 213 |
} \ |
| 214 |
if (i < len) { \ |
| 215 |
CALL_C(y + i, u + i, v + i, dst + i * (XSTEP), len - i); \ |
| 216 |
} \ |
| 217 |
} |
| 218 |
|
| 219 |
#if !defined(WEBP_REDUCE_CSP) |
| 220 |
YUV444_FUNC(Yuv444ToRgb_SSE41, VP8YuvToRgb32_SSE41, WebPYuv444ToRgb_C, 3) |
| 221 |
YUV444_FUNC(Yuv444ToBgr_SSE41, VP8YuvToBgr32_SSE41, WebPYuv444ToBgr_C, 3) |
| 222 |
#endif |
| 223 |
|
| 224 |
WEBP_TSAN_IGNORE_FUNCTION void WebPInitYUV444ConvertersSSE41(void) { |
| 225 |
#if !defined(WEBP_REDUCE_CSP) |
| 226 |
WebPYUV444Converters[MODE_RGB] = Yuv444ToRgb_SSE41; |
| 227 |
WebPYUV444Converters[MODE_BGR] = Yuv444ToBgr_SSE41; |
| 228 |
#endif |
| 229 |
} |
| 230 |
|
| 231 |
#else |
| 232 |
|
| 233 |
WEBP_DSP_INIT_STUB(WebPInitYUV444ConvertersSSE41) |
| 234 |
|
| 235 |
#endif |
| 236 |
|
| 237 |
#if !(defined(FANCY_UPSAMPLING) && defined(WEBP_USE_SSE41)) |
| 238 |
WEBP_DSP_INIT_STUB(WebPInitUpsamplersSSE41) |
| 239 |
#endif |