-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathPlanarConversionTransform.cs
More file actions
288 lines (230 loc) · 8.13 KB
/
Copy pathPlanarConversionTransform.cs
File metadata and controls
288 lines (230 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright © Clinton Ingram and Contributors
// SPDX-License-Identifier: MIT
using System;
using System.Numerics;
#if HWINTRINSICS
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
#endif
using static PhotoSauce.MagicScaler.MathUtil;
namespace PhotoSauce.MagicScaler.Transforms;
internal sealed class PlanarConversionTransform : ChainedPixelSource
{
private const int chromaOffset = -128;
private readonly float coeffCb0, coeffCb1, coeffCr0, coeffCr1;
private PixelSource sourceCb, sourceCr;
private RentedBuffer<byte> lineBuff;
public override PixelFormat Format { get; }
public PlanarConversionTransform(PixelSource srcY, PixelSource srcCb, PixelSource srcCr, in Matrix4x4 yccMatrix) : base(srcY)
{
if (srcCb.Width != srcY.Width || srcCb.Height != srcY.Height) throw new ArgumentException("Chroma plane incorrect size", nameof(srcCb));
if (srcCr.Width != srcY.Width || srcCr.Height != srcY.Height) throw new ArgumentException("Chroma plane incorrect size", nameof(srcCr));
if (srcCb.Format.BitsPerPixel != srcY.Format.BitsPerPixel) throw new ArgumentException("Chroma plane incorrect format", nameof(srcCb));
if (srcCr.Format.BitsPerPixel != srcY.Format.BitsPerPixel) throw new ArgumentException("Chroma plane incorrect format", nameof(srcCr));
if (!Matrix3x3C.Invert((Matrix3x3C)yccMatrix, out var matrix)) throw new ArgumentException("Invalid YCC matrix", nameof(yccMatrix));
if (srcCb.Format.Range == PixelValueRange.Video)
matrix *= (double)byte.MaxValue / VideoChromaScale;
coeffCb0 = (float)matrix.M23;
coeffCb1 = (float)matrix.M22;
coeffCr0 = (float)matrix.M32;
coeffCr1 = (float)matrix.M31;
sourceCb = srcCb;
sourceCr = srcCr;
Format = srcY.Format.BitsPerPixel == 8 ? PixelFormat.Bgr24 : PixelFormat.Bgrx128Float;
int bufferStride = BufferStride;
if (HWIntrinsics.IsAvxSupported)
bufferStride = PowerOfTwoCeiling(bufferStride, HWIntrinsics.VectorCount<byte>());
lineBuff = BufferPool.RentAligned<byte>(bufferStride * 3);
}
public override void ReInit(PixelSource newSource)
{
if (newSource is PlanarPixelSource plsrc)
{
base.ReInit(plsrc.SourceY);
reInit(ref sourceCb, plsrc.SourceCb);
reInit(ref sourceCr, plsrc.SourceCr);
return;
}
base.ReInit(newSource);
}
private static void reInit(ref PixelSource cursrc, PixelSource newsrc)
{
if (cursrc is ChainedPixelSource chain && chain.Passthrough)
{
chain.ReInit(newsrc);
return;
}
cursrc.Dispose();
cursrc = newsrc;
}
protected override unsafe void CopyPixelsInternal(in PixelArea prc, int cbStride, int cbBufferSize, byte* pbBuffer)
{
var buffspan = lineBuff.Span;
if (buffspan.IsEmpty) ThrowHelper.ThrowObjectDisposed(nameof(PlanarConversionTransform));
fixed (byte* bstart = buffspan)
{
uint cb = (uint)DivCeiling(prc.Width * PrevSource.Format.BitsPerPixel, 8);
uint bstride = (uint)buffspan.Length / 3u;
for (int y = 0; y < prc.Height; y++)
{
var lrc = prc.Slice(y, 1);
Profiler.PauseTiming();
PrevSource.CopyPixels(lrc, (int)bstride, (int)bstride, bstart);
sourceCb.CopyPixels(lrc, (int)bstride, (int)bstride, bstart + bstride);
sourceCr.CopyPixels(lrc, (int)bstride, (int)bstride, bstart + bstride * 2);
Profiler.ResumeTiming();
byte* op = pbBuffer + y * cbStride;
if (Format.NumericRepresentation == PixelNumericRepresentation.Float)
#if HWINTRINSICS
if (HWIntrinsics.IsSupported && cb >= (uint)Vector128<byte>.Count * 4)
copyPixelsIntrinsic(bstart, op, bstride, cb);
else
#endif
copyPixelsFloat(bstart, op, bstride, cb);
else
copyPixelsByte(bstart, op, bstride, cb);
}
}
}
private unsafe void copyPixelsByte(byte* bstart, byte* opstart, uint bstride, uint cb)
{
byte* op = opstart;
byte* ip0 = bstart, ip1 = bstart + bstride, ip2 = bstart + bstride * 2, ipe = ip0 + cb;
int c0 = Fix15(coeffCb0);
int c1 = Fix15(coeffCb1);
int c2 = Fix15(coeffCr0);
int c3 = Fix15(coeffCr1);
while (ip0 < ipe)
{
int i0 = *ip0++ * UQ15One;
int i1 = *ip1++ + chromaOffset;
int i2 = *ip2++ + chromaOffset;
byte o0 = UnFix15ToByte(i0 + i1 * c0);
byte o1 = UnFix15ToByte(i0 + i1 * c1 + i2 * c2);
byte o2 = UnFix15ToByte(i0 + i2 * c3);
op[0] = o0;
op[1] = o1;
op[2] = o2;
op += 3;
}
}
private unsafe void copyPixelsFloat(byte* bstart, byte* opstart, uint bstride, uint cb)
{
float* op = (float*)opstart;
float* ip0 = (float*)bstart, ip1 = (float*)(bstart + bstride), ip2 = (float*)(bstart + bstride * 2), ipe = (float*)(bstart + cb);
float c0 = coeffCb0;
float c1 = coeffCb1;
float c2 = coeffCr0;
float c3 = coeffCr1;
float fzero = Vector4.Zero.X;
while (ip0 < ipe)
{
float f0 = *ip0++;
float f1 = *ip1++;
float f2 = *ip2++;
op[0] = f0 + f1 * c0;
op[1] = f0 + f1 * c1 + f2 * c2;
op[2] = f0 + f2 * c3;
op[3] = fzero;
op += 4;
}
}
#if HWINTRINSICS
private unsafe void copyPixelsIntrinsic(byte* bstart, byte* opstart, uint bstride, uint cb)
{
uint stride = bstride / sizeof(float);
float* op = (float*)opstart;
float* ip = (float*)bstart, ipe = (float*)(bstart + cb);
if (Avx2.IsSupported)
{
var vc0 = Vector256.Create(coeffCb0);
var vc1 = Vector256.Create(coeffCb1);
var vc2 = Vector256.Create(coeffCr0);
var vc3 = Vector256.Create(coeffCr1);
var vmaskp = Vector256.Create(0, 2, 4, 6, 1, 3, 5, 7);
ipe -= Vector256<float>.Count;
LoopTop:
do
{
var viy = Avx.LoadVector256(ip);
var vib = Avx.LoadVector256(ip + stride);
var vir = Avx.LoadVector256(ip + stride * 2);
ip += Vector256<float>.Count;
viy = Avx2.PermuteVar8x32(viy, vmaskp);
vib = Avx2.PermuteVar8x32(vib, vmaskp);
vir = Avx2.PermuteVar8x32(vir, vmaskp);
var vt0 = HWIntrinsics.MultiplyAdd(viy, vib, vc0);
var vt1 = HWIntrinsics.MultiplyAdd(HWIntrinsics.MultiplyAdd(viy, vib, vc1), vir, vc2);
var vt2 = HWIntrinsics.MultiplyAdd(viy, vir, vc3);
var vt3 = Vector256<float>.Zero;
var vte = Avx.UnpackLow(vt0, vt2);
var vto = Avx.UnpackLow(vt1, vt3);
Avx.Store(op, Avx.UnpackLow(vte, vto));
Avx.Store(op + Vector256<float>.Count, Avx.UnpackHigh(vte, vto));
vte = Avx.UnpackHigh(vt0, vt2);
vto = Avx.UnpackHigh(vt1, vt3);
Avx.Store(op + Vector256<float>.Count * 2, Avx.UnpackLow(vte, vto));
Avx.Store(op + Vector256<float>.Count * 3, Avx.UnpackHigh(vte, vto));
op += Vector256<float>.Count * 4;
}
while (ip <= ipe);
if (ip < ipe + Vector256<float>.Count)
{
nuint offs = UnsafeUtil.ByteOffset(ipe, ip);
ip = UnsafeUtil.SubtractOffset(ip, offs);
op = UnsafeUtil.SubtractOffset(op, offs * 4);
goto LoopTop;
}
}
else
{
var vc0 = Vector128.Create(coeffCb0);
var vc1 = Vector128.Create(coeffCb1);
var vc2 = Vector128.Create(coeffCr0);
var vc3 = Vector128.Create(coeffCr1);
ipe -= Vector128<float>.Count;
LoopTop:
do
{
var viy = Sse.LoadVector128(ip);
var vib = Sse.LoadVector128(ip + stride);
var vir = Sse.LoadVector128(ip + stride * 2);
ip += Vector128<float>.Count;
var vt0 = HWIntrinsics.MultiplyAdd(viy, vib, vc0);
var vt1 = HWIntrinsics.MultiplyAdd(HWIntrinsics.MultiplyAdd(viy, vib, vc1), vir, vc2);
var vt2 = HWIntrinsics.MultiplyAdd(viy, vir, vc3);
var vt3 = Vector128<float>.Zero;
var vte = Sse.UnpackLow(vt0, vt2);
var vto = Sse.UnpackLow(vt1, vt3);
Sse.Store(op, Sse.UnpackLow(vte, vto));
Sse.Store(op + Vector128<float>.Count, Sse.UnpackHigh(vte, vto));
vte = Sse.UnpackHigh(vt0, vt2);
vto = Sse.UnpackHigh(vt1, vt3);
Sse.Store(op + Vector128<float>.Count * 2, Sse.UnpackLow(vte, vto));
Sse.Store(op + Vector128<float>.Count * 3, Sse.UnpackHigh(vte, vto));
op += Vector128<float>.Count * 4;
}
while (ip <= ipe);
if (ip < ipe + Vector128<float>.Count)
{
nuint offs = UnsafeUtil.ByteOffset(ipe, ip);
ip = UnsafeUtil.SubtractOffset(ip, offs);
op = UnsafeUtil.SubtractOffset(op, offs * 4);
goto LoopTop;
}
}
}
#endif
protected override void Dispose(bool disposing)
{
if (disposing)
{
sourceCb.Dispose();
sourceCr.Dispose();
lineBuff.Dispose();
lineBuff = default;
}
base.Dispose(disposing);
}
public override string ToString() => nameof(PlanarConversionTransform);
}