Coverage Report

Created: 2025-07-12 06:31

/src/libwebp/sharpyuv/sharpyuv_sse2.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2022 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
// Speed-critical functions for Sharp YUV.
11
//
12
// Author: Skal (pascal.massimino@gmail.com)
13
14
#include "sharpyuv/sharpyuv_dsp.h"
15
16
#if defined(WEBP_USE_SSE2)
17
#include <emmintrin.h>
18
19
#include <stdlib.h>
20
21
#include "src/dsp/cpu.h"
22
#include "src/webp/types.h"
23
24
23.7M
static uint16_t clip_SSE2(int v, int max) {
25
23.7M
  return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
26
23.7M
}
27
28
static uint64_t SharpYuvUpdateY_SSE2(const uint16_t* ref, const uint16_t* src,
29
464k
                                     uint16_t* dst, int len, int bit_depth) {
30
464k
  const int max_y = (1 << bit_depth) - 1;
31
464k
  uint64_t diff = 0;
32
464k
  uint32_t tmp[4];
33
464k
  int i;
34
464k
  const __m128i zero = _mm_setzero_si128();
35
464k
  const __m128i max = _mm_set1_epi16(max_y);
36
464k
  const __m128i one = _mm_set1_epi16(1);
37
464k
  __m128i sum = zero;
38
39
11.7M
  for (i = 0; i + 8 <= len; i += 8) {
40
11.3M
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
41
11.3M
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
42
11.3M
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
43
11.3M
    const __m128i D = _mm_sub_epi16(A, B);       // diff_y
44
11.3M
    const __m128i E = _mm_cmpgt_epi16(zero, D);  // sign (-1 or 0)
45
11.3M
    const __m128i F = _mm_add_epi16(C, D);       // new_y
46
11.3M
    const __m128i G = _mm_or_si128(E, one);      // -1 or 1
47
11.3M
    const __m128i H = _mm_max_epi16(_mm_min_epi16(F, max), zero);
48
11.3M
    const __m128i I = _mm_madd_epi16(D, G);      // sum(abs(...))
49
11.3M
    _mm_storeu_si128((__m128i*)(dst + i), H);
50
11.3M
    sum = _mm_add_epi32(sum, I);
51
11.3M
  }
52
464k
  _mm_storeu_si128((__m128i*)tmp, sum);
53
464k
  diff = tmp[3] + tmp[2] + tmp[1] + tmp[0];
54
1.25M
  for (; i < len; ++i) {
55
792k
    const int diff_y = ref[i] - src[i];
56
792k
    const int new_y = (int)dst[i] + diff_y;
57
792k
    dst[i] = clip_SSE2(new_y, max_y);
58
792k
    diff += (uint64_t)abs(diff_y);
59
792k
  }
60
464k
  return diff;
61
464k
}
62
63
static void SharpYuvUpdateRGB_SSE2(const int16_t* ref, const int16_t* src,
64
464k
                                   int16_t* dst, int len) {
65
464k
  int i = 0;
66
8.92M
  for (i = 0; i + 8 <= len; i += 8) {
67
8.45M
    const __m128i A = _mm_loadu_si128((const __m128i*)(ref + i));
68
8.45M
    const __m128i B = _mm_loadu_si128((const __m128i*)(src + i));
69
8.45M
    const __m128i C = _mm_loadu_si128((const __m128i*)(dst + i));
70
8.45M
    const __m128i D = _mm_sub_epi16(A, B);   // diff_uv
71
8.45M
    const __m128i E = _mm_add_epi16(C, D);   // new_uv
72
8.45M
    _mm_storeu_si128((__m128i*)(dst + i), E);
73
8.45M
  }
74
1.30M
  for (; i < len; ++i) {
75
844k
    const int diff_uv = ref[i] - src[i];
76
844k
    dst[i] += diff_uv;
77
844k
  }
78
464k
}
79
80
static void SharpYuvFilterRow16_SSE2(const int16_t* A, const int16_t* B,
81
                                     int len, const uint16_t* best_y,
82
2.78M
                                     uint16_t* out, int bit_depth) {
83
2.78M
  const int max_y = (1 << bit_depth) - 1;
84
2.78M
  int i;
85
2.78M
  const __m128i kCst8 = _mm_set1_epi16(8);
86
2.78M
  const __m128i max = _mm_set1_epi16(max_y);
87
2.78M
  const __m128i zero = _mm_setzero_si128();
88
18.1M
  for (i = 0; i + 8 <= len; i += 8) {
89
15.3M
    const __m128i a0 = _mm_loadu_si128((const __m128i*)(A + i + 0));
90
15.3M
    const __m128i a1 = _mm_loadu_si128((const __m128i*)(A + i + 1));
91
15.3M
    const __m128i b0 = _mm_loadu_si128((const __m128i*)(B + i + 0));
92
15.3M
    const __m128i b1 = _mm_loadu_si128((const __m128i*)(B + i + 1));
93
15.3M
    const __m128i a0b1 = _mm_add_epi16(a0, b1);
94
15.3M
    const __m128i a1b0 = _mm_add_epi16(a1, b0);
95
15.3M
    const __m128i a0a1b0b1 = _mm_add_epi16(a0b1, a1b0);  // A0+A1+B0+B1
96
15.3M
    const __m128i a0a1b0b1_8 = _mm_add_epi16(a0a1b0b1, kCst8);
97
15.3M
    const __m128i a0b1_2 = _mm_add_epi16(a0b1, a0b1);    // 2*(A0+B1)
98
15.3M
    const __m128i a1b0_2 = _mm_add_epi16(a1b0, a1b0);    // 2*(A1+B0)
99
15.3M
    const __m128i c0 = _mm_srai_epi16(_mm_add_epi16(a0b1_2, a0a1b0b1_8), 3);
100
15.3M
    const __m128i c1 = _mm_srai_epi16(_mm_add_epi16(a1b0_2, a0a1b0b1_8), 3);
101
15.3M
    const __m128i d0 = _mm_add_epi16(c1, a0);
102
15.3M
    const __m128i d1 = _mm_add_epi16(c0, a1);
103
15.3M
    const __m128i e0 = _mm_srai_epi16(d0, 1);
104
15.3M
    const __m128i e1 = _mm_srai_epi16(d1, 1);
105
15.3M
    const __m128i f0 = _mm_unpacklo_epi16(e0, e1);
106
15.3M
    const __m128i f1 = _mm_unpackhi_epi16(e0, e1);
107
15.3M
    const __m128i g0 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
108
15.3M
    const __m128i g1 = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 8));
109
15.3M
    const __m128i h0 = _mm_add_epi16(g0, f0);
110
15.3M
    const __m128i h1 = _mm_add_epi16(g1, f1);
111
15.3M
    const __m128i i0 = _mm_max_epi16(_mm_min_epi16(h0, max), zero);
112
15.3M
    const __m128i i1 = _mm_max_epi16(_mm_min_epi16(h1, max), zero);
113
15.3M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), i0);
114
15.3M
    _mm_storeu_si128((__m128i*)(out + 2 * i + 8), i1);
115
15.3M
  }
116
14.2M
  for (; i < len; ++i) {
117
    //   (9 * A0 + 3 * A1 + 3 * B0 + B1 + 8) >> 4 =
118
    // = (8 * A0 + 2 * (A1 + B0) + (A0 + A1 + B0 + B1 + 8)) >> 4
119
    // We reuse the common sub-expressions.
120
11.4M
    const int a0b1 = A[i + 0] + B[i + 1];
121
11.4M
    const int a1b0 = A[i + 1] + B[i + 0];
122
11.4M
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
123
11.4M
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
124
11.4M
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
125
11.4M
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
126
11.4M
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
127
11.4M
  }
128
2.78M
}
129
130
0
static WEBP_INLINE __m128i s16_to_s32(__m128i in) {
131
0
  return _mm_srai_epi32(_mm_unpacklo_epi16(in, in), 16);
132
0
}
133
134
static void SharpYuvFilterRow32_SSE2(const int16_t* A, const int16_t* B,
135
                                     int len, const uint16_t* best_y,
136
0
                                     uint16_t* out, int bit_depth) {
137
0
  const int max_y = (1 << bit_depth) - 1;
138
0
  int i;
139
0
  const __m128i kCst8 = _mm_set1_epi32(8);
140
0
  const __m128i max = _mm_set1_epi16(max_y);
141
0
  const __m128i zero = _mm_setzero_si128();
142
0
  for (i = 0; i + 4 <= len; i += 4) {
143
0
    const __m128i a0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 0)));
144
0
    const __m128i a1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(A + i + 1)));
145
0
    const __m128i b0 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 0)));
146
0
    const __m128i b1 = s16_to_s32(_mm_loadl_epi64((const __m128i*)(B + i + 1)));
147
0
    const __m128i a0b1 = _mm_add_epi32(a0, b1);
148
0
    const __m128i a1b0 = _mm_add_epi32(a1, b0);
149
0
    const __m128i a0a1b0b1 = _mm_add_epi32(a0b1, a1b0);  // A0+A1+B0+B1
150
0
    const __m128i a0a1b0b1_8 = _mm_add_epi32(a0a1b0b1, kCst8);
151
0
    const __m128i a0b1_2 = _mm_add_epi32(a0b1, a0b1);  // 2*(A0+B1)
152
0
    const __m128i a1b0_2 = _mm_add_epi32(a1b0, a1b0);  // 2*(A1+B0)
153
0
    const __m128i c0 = _mm_srai_epi32(_mm_add_epi32(a0b1_2, a0a1b0b1_8), 3);
154
0
    const __m128i c1 = _mm_srai_epi32(_mm_add_epi32(a1b0_2, a0a1b0b1_8), 3);
155
0
    const __m128i d0 = _mm_add_epi32(c1, a0);
156
0
    const __m128i d1 = _mm_add_epi32(c0, a1);
157
0
    const __m128i e0 = _mm_srai_epi32(d0, 1);
158
0
    const __m128i e1 = _mm_srai_epi32(d1, 1);
159
0
    const __m128i f0 = _mm_unpacklo_epi32(e0, e1);
160
0
    const __m128i f1 = _mm_unpackhi_epi32(e0, e1);
161
0
    const __m128i g = _mm_loadu_si128((const __m128i*)(best_y + 2 * i + 0));
162
0
    const __m128i h_16 = _mm_add_epi16(g, _mm_packs_epi32(f0, f1));
163
0
    const __m128i final = _mm_max_epi16(_mm_min_epi16(h_16, max), zero);
164
0
    _mm_storeu_si128((__m128i*)(out + 2 * i + 0), final);
165
0
  }
166
0
  for (; i < len; ++i) {
167
    //   (9 * A0 + 3 * A1 + 3 * B0 + B1 + 8) >> 4 =
168
    // = (8 * A0 + 2 * (A1 + B0) + (A0 + A1 + B0 + B1 + 8)) >> 4
169
    // We reuse the common sub-expressions.
170
0
    const int a0b1 = A[i + 0] + B[i + 1];
171
0
    const int a1b0 = A[i + 1] + B[i + 0];
172
0
    const int a0a1b0b1 = a0b1 + a1b0 + 8;
173
0
    const int v0 = (8 * A[i + 0] + 2 * a1b0 + a0a1b0b1) >> 4;
174
0
    const int v1 = (8 * A[i + 1] + 2 * a0b1 + a0a1b0b1) >> 4;
175
0
    out[2 * i + 0] = clip_SSE2(best_y[2 * i + 0] + v0, max_y);
176
0
    out[2 * i + 1] = clip_SSE2(best_y[2 * i + 1] + v1, max_y);
177
0
  }
178
0
}
179
180
static void SharpYuvFilterRow_SSE2(const int16_t* A, const int16_t* B, int len,
181
                                   const uint16_t* best_y, uint16_t* out,
182
2.78M
                                   int bit_depth) {
183
2.78M
  if (bit_depth <= 10) {
184
2.78M
    SharpYuvFilterRow16_SSE2(A, B, len, best_y, out, bit_depth);
185
2.78M
  } else {
186
0
    SharpYuvFilterRow32_SSE2(A, B, len, best_y, out, bit_depth);
187
0
  }
188
2.78M
}
189
190
//------------------------------------------------------------------------------
191
192
extern void InitSharpYuvSSE2(void);
193
194
2.17k
WEBP_TSAN_IGNORE_FUNCTION void InitSharpYuvSSE2(void) {
195
2.17k
  SharpYuvUpdateY = SharpYuvUpdateY_SSE2;
196
2.17k
  SharpYuvUpdateRGB = SharpYuvUpdateRGB_SSE2;
197
2.17k
  SharpYuvFilterRow = SharpYuvFilterRow_SSE2;
198
2.17k
}
199
#else  // !WEBP_USE_SSE2
200
201
extern void InitSharpYuvSSE2(void);
202
203
void InitSharpYuvSSE2(void) {}
204
205
#endif  // WEBP_USE_SSE2