Skip to content

Commit 29317da

Browse files
Merge branch 'main' into sn/nullable/various
2 parents 0e40d71 + 5de22f4 commit 29317da

File tree

3 files changed

+81
-152
lines changed

3 files changed

+81
-152
lines changed

src/ImageSharp/Formats/Webp/Lossy/LossyUtils.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal static class LossyUtils
1414
{
1515
// Note: method name in libwebp reference implementation is called VP8SSE16x16.
1616
[MethodImpl(InliningOptions.ShortMethod)]
17-
public static int Vp8_Sse16X16(Span<byte> a, Span<byte> b)
17+
public static int Vp8_Sse16x16(Span<byte> a, Span<byte> b)
1818
{
1919
if (Avx2.IsSupported)
2020
{
@@ -31,7 +31,7 @@ public static int Vp8_Sse16X16(Span<byte> a, Span<byte> b)
3131

3232
// Note: method name in libwebp reference implementation is called VP8SSE16x8.
3333
[MethodImpl(InliningOptions.ShortMethod)]
34-
public static int Vp8_Sse16X8(Span<byte> a, Span<byte> b)
34+
public static int Vp8_Sse16x8(Span<byte> a, Span<byte> b)
3535
{
3636
if (Avx2.IsSupported)
3737
{
@@ -48,7 +48,7 @@ public static int Vp8_Sse16X8(Span<byte> a, Span<byte> b)
4848

4949
// Note: method name in libwebp reference implementation is called VP8SSE4x4.
5050
[MethodImpl(InliningOptions.ShortMethod)]
51-
public static int Vp8_Sse4X4(Span<byte> a, Span<byte> b)
51+
public static int Vp8_Sse4x4(Span<byte> a, Span<byte> b)
5252
{
5353
if (Avx2.IsSupported)
5454
{
@@ -77,8 +77,8 @@ public static int Vp8_Sse4X4(Span<byte> a, Span<byte> b)
7777
Vector256<byte> b01s = Avx2.UnpackLow(b01.AsByte(), Vector256<byte>.Zero);
7878

7979
// subtract, square and accumulate.
80-
Vector256<byte> d0 = Avx2.SubtractSaturate(a01s, b01s);
81-
Vector256<int> e0 = Avx2.MultiplyAddAdjacent(d0.AsInt16(), d0.AsInt16());
80+
Vector256<short> d0 = Avx2.SubtractSaturate(a01s.AsInt16(), b01s.AsInt16());
81+
Vector256<int> e0 = Avx2.MultiplyAddAdjacent(d0, d0);
8282

8383
return Numerics.ReduceSum(e0);
8484
}
@@ -110,10 +110,10 @@ public static int Vp8_Sse4X4(Span<byte> a, Span<byte> b)
110110
Vector128<byte> b23s = Sse2.UnpackLow(b23.AsByte(), Vector128<byte>.Zero);
111111

112112
// subtract, square and accumulate.
113-
Vector128<byte> d0 = Sse2.SubtractSaturate(a01s, b01s);
114-
Vector128<byte> d1 = Sse2.SubtractSaturate(a23s, b23s);
115-
Vector128<int> e0 = Sse2.MultiplyAddAdjacent(d0.AsInt16(), d0.AsInt16());
116-
Vector128<int> e1 = Sse2.MultiplyAddAdjacent(d1.AsInt16(), d1.AsInt16());
113+
Vector128<short> d0 = Sse2.SubtractSaturate(a01s.AsInt16(), b01s.AsInt16());
114+
Vector128<short> d1 = Sse2.SubtractSaturate(a23s.AsInt16(), b23s.AsInt16());
115+
Vector128<int> e0 = Sse2.MultiplyAddAdjacent(d0, d0);
116+
Vector128<int> e1 = Sse2.MultiplyAddAdjacent(d1, d1);
117117
Vector128<int> sum = Sse2.Add(e0, e1);
118118

119119
return Numerics.ReduceSum(sum);
@@ -126,18 +126,16 @@ public static int Vp8_Sse4X4(Span<byte> a, Span<byte> b)
126126
public static int Vp8_SseNxN(Span<byte> a, Span<byte> b, int w, int h)
127127
{
128128
int count = 0;
129-
int aOffset = 0;
130-
int bOffset = 0;
129+
int offset = 0;
131130
for (int y = 0; y < h; y++)
132131
{
133132
for (int x = 0; x < w; x++)
134133
{
135-
int diff = a[aOffset + x] - b[bOffset + x];
134+
int diff = a[offset + x] - b[offset + x];
136135
count += diff * diff;
137136
}
138137

139-
aOffset += WebpConstants.Bps;
140-
bOffset += WebpConstants.Bps;
138+
offset += WebpConstants.Bps;
141139
}
142140

143141
return count;

src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void PickBestIntra16(Vp8EncIterator it, ref Vp8ModeScore rd, Vp8Se
5353
rdCur.Nz = (uint)ReconstructIntra16(it, dqm, rdCur, tmpDst, mode);
5454

5555
// Measure RD-score.
56-
rdCur.D = LossyUtils.Vp8_Sse16X16(src, tmpDst);
56+
rdCur.D = LossyUtils.Vp8_Sse16x16(src, tmpDst);
5757
rdCur.SD = tlambda != 0 ? Mult8B(tlambda, LossyUtils.Vp8Disto16X16(src, tmpDst, WeightY, scratch)) : 0;
5858
rdCur.H = WebpConstants.Vp8FixedCostsI16[mode];
5959
rdCur.R = it.GetCostLuma16(rdCur, proba, res);
@@ -145,7 +145,7 @@ public static bool PickBestIntra4(Vp8EncIterator it, ref Vp8ModeScore rd, Vp8Seg
145145
rdTmp.Nz = (uint)ReconstructIntra4(it, dqm, tmpLevels, src, tmpDst, mode);
146146

147147
// Compute RD-score.
148-
rdTmp.D = LossyUtils.Vp8_Sse4X4(src, tmpDst);
148+
rdTmp.D = LossyUtils.Vp8_Sse4x4(src, tmpDst);
149149
rdTmp.SD = tlambda != 0 ? Mult8B(tlambda, LossyUtils.Vp8Disto4X4(src, tmpDst, WeightY, scratch)) : 0;
150150
rdTmp.H = modeCosts[mode];
151151

@@ -235,7 +235,7 @@ public static void PickBestUv(Vp8EncIterator it, ref Vp8ModeScore rd, Vp8Segment
235235
rdUv.Nz = (uint)ReconstructUv(it, dqm, rdUv, tmpDst, mode);
236236

237237
// Compute RD-score
238-
rdUv.D = LossyUtils.Vp8_Sse16X8(src, tmpDst);
238+
rdUv.D = LossyUtils.Vp8_Sse16x8(src, tmpDst);
239239
rdUv.SD = 0; // not calling TDisto here: it tends to flatten areas.
240240
rdUv.H = WebpConstants.Vp8FixedCostsUv[mode];
241241
rdUv.R = it.GetCostUv(rdUv, proba, res);
@@ -389,7 +389,7 @@ public static void RefineUsingDistortion(Vp8EncIterator it, Vp8SegmentInfo[] seg
389389
for (mode = 0; mode < WebpConstants.NumPredModes; ++mode)
390390
{
391391
Span<byte> reference = it.YuvP.AsSpan(Vp8Encoding.Vp8I16ModeOffsets[mode]);
392-
long score = (LossyUtils.Vp8_Sse16X16(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsI16[mode] * lambdaDi16);
392+
long score = (LossyUtils.Vp8_Sse16x16(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsI16[mode] * lambdaDi16);
393393

394394
if (mode > 0 && WebpConstants.Vp8FixedCostsI16[mode] > bitLimit)
395395
{
@@ -436,7 +436,7 @@ public static void RefineUsingDistortion(Vp8EncIterator it, Vp8SegmentInfo[] seg
436436
for (mode = 0; mode < WebpConstants.NumBModes; ++mode)
437437
{
438438
Span<byte> reference = it.YuvP.AsSpan(Vp8Encoding.Vp8I4ModeOffsets[mode]);
439-
long score = (LossyUtils.Vp8_Sse4X4(src, reference) * WebpConstants.RdDistoMult) + (modeCosts[mode] * lambdaDi4);
439+
long score = (LossyUtils.Vp8_Sse4x4(src, reference) * WebpConstants.RdDistoMult) + (modeCosts[mode] * lambdaDi4);
440440
if (score < bestI4Score)
441441
{
442442
bestI4Mode = mode;
@@ -485,7 +485,7 @@ public static void RefineUsingDistortion(Vp8EncIterator it, Vp8SegmentInfo[] seg
485485
for (mode = 0; mode < WebpConstants.NumPredModes; ++mode)
486486
{
487487
Span<byte> reference = it.YuvP.AsSpan(Vp8Encoding.Vp8UvModeOffsets[mode]);
488-
long score = (LossyUtils.Vp8_Sse16X8(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsUv[mode] * lambdaDuv);
488+
long score = (LossyUtils.Vp8_Sse16x8(src, reference) * WebpConstants.RdDistoMult) + (WebpConstants.Vp8FixedCostsUv[mode] * lambdaDuv);
489489
if (score < bestUvScore)
490490
{
491491
bestMode = mode;

0 commit comments

Comments
 (0)