Coverage Report

Created: 2025-07-01 06:46

/src/FreeRDP/libfreerdp/primitives/neon/prim_YUV_neon.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Optimized YUV/RGB conversion operations
4
 *
5
 * Copyright 2014 Thomas Erbesdobler
6
 * Copyright 2016-2017 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2016-2017 Norbert Federa <norbert.federa@thincast.com>
8
 * Copyright 2016-2017 Thincast Technologies GmbH
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 *     http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22
23
#include <freerdp/config.h>
24
25
#include <winpr/sysinfo.h>
26
#include <winpr/crt.h>
27
#include <freerdp/types.h>
28
#include <freerdp/primitives.h>
29
30
#include "prim_internal.h"
31
#include "prim_YUV.h"
32
33
#if defined(NEON_INTRINSICS_ENABLED)
34
#include <arm_neon.h>
35
36
static primitives_t* generic = NULL;
37
38
static INLINE uint8x8_t neon_YUV2R_single(uint16x8_t C, int16x8_t D, int16x8_t E)
39
{
40
  /* R = (256 * Y + 403 * (V - 128)) >> 8 */
41
  const int32x4_t Ch = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(C)));
42
  const int32x4_t e403h = vmull_n_s16(vget_high_s16(E), 403);
43
  const int32x4_t cehm = vaddq_s32(Ch, e403h);
44
  const int32x4_t ceh = vshrq_n_s32(cehm, 8);
45
46
  const int32x4_t Cl = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(C)));
47
  const int32x4_t e403l = vmull_n_s16(vget_low_s16(E), 403);
48
  const int32x4_t celm = vaddq_s32(Cl, e403l);
49
  const int32x4_t cel = vshrq_n_s32(celm, 8);
50
  const int16x8_t ce = vcombine_s16(vqmovn_s32(cel), vqmovn_s32(ceh));
51
  return vqmovun_s16(ce);
52
}
53
54
static INLINE uint8x8x2_t neon_YUV2R(uint16x8x2_t C, int16x8x2_t D, int16x8x2_t E)
55
{
56
  uint8x8x2_t res = { { neon_YUV2R_single(C.val[0], D.val[0], E.val[0]),
57
                      neon_YUV2R_single(C.val[1], D.val[1], E.val[1]) } };
58
  return res;
59
}
60
61
static INLINE uint8x8_t neon_YUV2G_single(uint16x8_t C, int16x8_t D, int16x8_t E)
62
{
63
  /* G = (256L * Y -  48 * (U - 128) - 120 * (V - 128)) >> 8 */
64
  const int16x8_t d48 = vmulq_n_s16(D, 48);
65
  const int16x8_t e120 = vmulq_n_s16(E, 120);
66
  const int32x4_t deh = vaddl_s16(vget_high_s16(d48), vget_high_s16(e120));
67
  const int32x4_t Ch = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(C)));
68
  const int32x4_t cdeh32m = vsubq_s32(Ch, deh);
69
  const int32x4_t cdeh32 = vshrq_n_s32(cdeh32m, 8);
70
  const int16x4_t cdeh = vqmovn_s32(cdeh32);
71
72
  const int32x4_t del = vaddl_s16(vget_low_s16(d48), vget_low_s16(e120));
73
  const int32x4_t Cl = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(C)));
74
  const int32x4_t cdel32m = vsubq_s32(Cl, del);
75
  const int32x4_t cdel32 = vshrq_n_s32(cdel32m, 8);
76
  const int16x4_t cdel = vqmovn_s32(cdel32);
77
  const int16x8_t cde = vcombine_s16(cdel, cdeh);
78
  return vqmovun_s16(cde);
79
}
80
81
static INLINE uint8x8x2_t neon_YUV2G(uint16x8x2_t C, int16x8x2_t D, int16x8x2_t E)
82
{
83
  uint8x8x2_t res = { { neon_YUV2G_single(C.val[0], D.val[0], E.val[0]),
84
                      neon_YUV2G_single(C.val[1], D.val[1], E.val[1]) } };
85
  return res;
86
}
87
88
static INLINE uint8x8_t neon_YUV2B_single(uint16x8_t C, int16x8_t D, int16x8_t E)
89
{
90
  /* B = (256L * Y + 475 * (U - 128)) >> 8*/
91
  const int32x4_t Ch = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(C)));
92
  const int32x4_t d475h = vmull_n_s16(vget_high_s16(D), 475);
93
  const int32x4_t cdhm = vaddq_s32(Ch, d475h);
94
  const int32x4_t cdh = vshrq_n_s32(cdhm, 8);
95
96
  const int32x4_t Cl = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(C)));
97
  const int32x4_t d475l = vmull_n_s16(vget_low_s16(D), 475);
98
  const int32x4_t cdlm = vaddq_s32(Cl, d475l);
99
  const int32x4_t cdl = vshrq_n_s32(cdlm, 8);
100
  const int16x8_t cd = vcombine_s16(vqmovn_s32(cdl), vqmovn_s32(cdh));
101
  return vqmovun_s16(cd);
102
}
103
104
static INLINE uint8x8x2_t neon_YUV2B(uint16x8x2_t C, int16x8x2_t D, int16x8x2_t E)
105
{
106
  uint8x8x2_t res = { { neon_YUV2B_single(C.val[0], D.val[0], E.val[0]),
107
                      neon_YUV2B_single(C.val[1], D.val[1], E.val[1]) } };
108
  return res;
109
}
110
111
static inline void neon_store_bgrx(BYTE* WINPR_RESTRICT pRGB, uint8x8_t r, uint8x8_t g, uint8x8_t b,
112
                                   uint8_t rPos, uint8_t gPos, uint8_t bPos, uint8_t aPos)
113
{
114
  uint8x8x4_t bgrx = vld4_u8(pRGB);
115
  bgrx.val[rPos] = r;
116
  bgrx.val[gPos] = g;
117
  bgrx.val[bPos] = b;
118
  vst4_u8(pRGB, bgrx);
119
}
120
121
static INLINE void neon_YuvToRgbPixel(BYTE* pRGB, uint8x8x2_t Y, int16x8x2_t D, int16x8x2_t E,
122
                                      const uint8_t rPos, const uint8_t gPos, const uint8_t bPos,
123
                                      const uint8_t aPos)
124
{
125
  /* Y * 256 == Y << 8  */
126
  const uint16x8x2_t C = { { vshlq_n_u16(vmovl_u8(Y.val[0]), 8),
127
                           vshlq_n_u16(vmovl_u8(Y.val[1]), 8) } };
128
129
  const uint8x8x2_t r = neon_YUV2R(C, D, E);
130
  const uint8x8x2_t g = neon_YUV2G(C, D, E);
131
  const uint8x8x2_t b = neon_YUV2B(C, D, E);
132
133
  neon_store_bgrx(pRGB, r.val[0], g.val[0], b.val[0], rPos, gPos, bPos, aPos);
134
  neon_store_bgrx(pRGB + sizeof(uint8x8x4_t), r.val[1], g.val[1], b.val[1], rPos, gPos, bPos,
135
                  aPos);
136
}
137
138
static inline int16x8x2_t loadUV(const BYTE* WINPR_RESTRICT pV, size_t x)
139
{
140
  const uint8x8_t Vraw = vld1_u8(&pV[x / 2]);
141
  const int16x8_t V = vreinterpretq_s16_u16(vmovl_u8(Vraw));
142
  const int16x8_t c128 = vdupq_n_s16(128);
143
  const int16x8_t E = vsubq_s16(V, c128);
144
  return vzipq_s16(E, E);
145
}
146
147
static INLINE void neon_write_pixel(BYTE* pRGB, BYTE Y, BYTE U, BYTE V, const uint8_t rPos,
148
                                    const uint8_t gPos, const uint8_t bPos, const uint8_t aPos)
149
{
150
  const BYTE r = YUV2R(Y, U, V);
151
  const BYTE g = YUV2G(Y, U, V);
152
  const BYTE b = YUV2B(Y, U, V);
153
154
  pRGB[rPos] = r;
155
  pRGB[gPos] = g;
156
  pRGB[bPos] = b;
157
}
158
159
static INLINE void neon_YUV420ToX_DOUBLE_ROW(const BYTE* WINPR_RESTRICT pY[2],
160
                                             const BYTE* WINPR_RESTRICT pU,
161
                                             const BYTE* WINPR_RESTRICT pV,
162
                                             BYTE* WINPR_RESTRICT pRGB[2], size_t width,
163
                                             const uint8_t rPos, const uint8_t gPos,
164
                                             const uint8_t bPos, const uint8_t aPos)
165
{
166
  UINT32 x = 0;
167
168
  for (; x < width - width % 16; x += 16)
169
  {
170
    const uint8x16_t Y0raw = vld1q_u8(&pY[0][x]);
171
    const uint8x8x2_t Y0 = { { vget_low_u8(Y0raw), vget_high_u8(Y0raw) } };
172
    const int16x8x2_t D = loadUV(pU, x);
173
    const int16x8x2_t E = loadUV(pV, x);
174
    neon_YuvToRgbPixel(&pRGB[0][4ULL * x], Y0, D, E, rPos, gPos, bPos, aPos);
175
176
    const uint8x16_t Y1raw = vld1q_u8(&pY[1][x]);
177
    const uint8x8x2_t Y1 = { { vget_low_u8(Y1raw), vget_high_u8(Y1raw) } };
178
    neon_YuvToRgbPixel(&pRGB[1][4ULL * x], Y1, D, E, rPos, gPos, bPos, aPos);
179
  }
180
181
  for (; x < width - width % 2; x += 2)
182
  {
183
    const BYTE U = pU[x / 2];
184
    const BYTE V = pV[x / 2];
185
186
    neon_write_pixel(&pRGB[0][4 * x], pY[0][x], U, V, rPos, gPos, bPos, aPos);
187
    neon_write_pixel(&pRGB[0][4 * (1ULL + x)], pY[0][1ULL + x], U, V, rPos, gPos, bPos, aPos);
188
    neon_write_pixel(&pRGB[1][4 * x], pY[1][x], U, V, rPos, gPos, bPos, aPos);
189
    neon_write_pixel(&pRGB[1][4 * (1ULL + x)], pY[1][1ULL + x], U, V, rPos, gPos, bPos, aPos);
190
  }
191
192
  for (; x < width; x++)
193
  {
194
    const BYTE U = pU[x / 2];
195
    const BYTE V = pV[x / 2];
196
197
    neon_write_pixel(&pRGB[0][4 * x], pY[0][x], U, V, rPos, gPos, bPos, aPos);
198
    neon_write_pixel(&pRGB[1][4 * x], pY[1][x], U, V, rPos, gPos, bPos, aPos);
199
  }
200
}
201
202
static INLINE void neon_YUV420ToX_SINGLE_ROW(const BYTE* WINPR_RESTRICT pY,
203
                                             const BYTE* WINPR_RESTRICT pU,
204
                                             const BYTE* WINPR_RESTRICT pV,
205
                                             BYTE* WINPR_RESTRICT pRGB, size_t width,
206
                                             const uint8_t rPos, const uint8_t gPos,
207
                                             const uint8_t bPos, const uint8_t aPos)
208
{
209
  UINT32 x = 0;
210
211
  for (; x < width - width % 16; x += 16)
212
  {
213
    const uint8x16_t Y0raw = vld1q_u8(&pY[x]);
214
    const uint8x8x2_t Y0 = { { vget_low_u8(Y0raw), vget_high_u8(Y0raw) } };
215
    const int16x8x2_t D = loadUV(pU, x);
216
    const int16x8x2_t E = loadUV(pV, x);
217
    neon_YuvToRgbPixel(&pRGB[4ULL * x], Y0, D, E, rPos, gPos, bPos, aPos);
218
  }
219
220
  for (; x < width - width % 2; x += 2)
221
  {
222
    const BYTE U = pU[x / 2];
223
    const BYTE V = pV[x / 2];
224
225
    neon_write_pixel(&pRGB[4 * x], pY[x], U, V, rPos, gPos, bPos, aPos);
226
    neon_write_pixel(&pRGB[4 * (1ULL + x)], pY[1ULL + x], U, V, rPos, gPos, bPos, aPos);
227
  }
228
  for (; x < width; x++)
229
  {
230
    const BYTE U = pU[x / 2];
231
    const BYTE V = pV[x / 2];
232
233
    neon_write_pixel(&pRGB[4 * x], pY[x], U, V, rPos, gPos, bPos, aPos);
234
  }
235
}
236
237
static INLINE pstatus_t neon_YUV420ToX(const BYTE* WINPR_RESTRICT pSrc[3], const UINT32 srcStep[3],
238
                                       BYTE* WINPR_RESTRICT pDst, UINT32 dstStep,
239
                                       const prim_size_t* WINPR_RESTRICT roi, const uint8_t rPos,
240
                                       const uint8_t gPos, const uint8_t bPos, const uint8_t aPos)
241
{
242
  const UINT32 nWidth = roi->width;
243
  const UINT32 nHeight = roi->height;
244
245
  WINPR_ASSERT(nHeight > 0);
246
  UINT32 y = 0;
247
  for (; y < (nHeight - 1); y += 2)
248
  {
249
    const uint8_t* pY[2] = { pSrc[0] + y * srcStep[0], pSrc[0] + (1ULL + y) * srcStep[0] };
250
    const uint8_t* pU = pSrc[1] + (y / 2) * srcStep[1];
251
    const uint8_t* pV = pSrc[2] + (y / 2) * srcStep[2];
252
    uint8_t* pRGB[2] = { pDst + y * dstStep, pDst + (1ULL + y) * dstStep };
253
254
    neon_YUV420ToX_DOUBLE_ROW(pY, pU, pV, pRGB, nWidth, rPos, gPos, bPos, aPos);
255
  }
256
  for (; y < nHeight; y++)
257
  {
258
    const uint8_t* pY = pSrc[0] + y * srcStep[0];
259
    const uint8_t* pU = pSrc[1] + (y / 2) * srcStep[1];
260
    const uint8_t* pV = pSrc[2] + (y / 2) * srcStep[2];
261
    uint8_t* pRGB = pDst + y * dstStep;
262
263
    neon_YUV420ToX_SINGLE_ROW(pY, pU, pV, pRGB, nWidth, rPos, gPos, bPos, aPos);
264
  }
265
  return PRIMITIVES_SUCCESS;
266
}
267
268
static pstatus_t neon_YUV420ToRGB_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc[3],
269
                                            const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
270
                                            UINT32 dstStep, UINT32 DstFormat,
271
                                            const prim_size_t* WINPR_RESTRICT roi)
272
{
273
  switch (DstFormat)
274
  {
275
    case PIXEL_FORMAT_BGRA32:
276
    case PIXEL_FORMAT_BGRX32:
277
      return neon_YUV420ToX(pSrc, srcStep, pDst, dstStep, roi, 2, 1, 0, 3);
278
279
    case PIXEL_FORMAT_RGBA32:
280
    case PIXEL_FORMAT_RGBX32:
281
      return neon_YUV420ToX(pSrc, srcStep, pDst, dstStep, roi, 0, 1, 2, 3);
282
283
    case PIXEL_FORMAT_ARGB32:
284
    case PIXEL_FORMAT_XRGB32:
285
      return neon_YUV420ToX(pSrc, srcStep, pDst, dstStep, roi, 1, 2, 3, 0);
286
287
    case PIXEL_FORMAT_ABGR32:
288
    case PIXEL_FORMAT_XBGR32:
289
      return neon_YUV420ToX(pSrc, srcStep, pDst, dstStep, roi, 3, 2, 1, 0);
290
291
    default:
292
      return generic->YUV420ToRGB_8u_P3AC4R(pSrc, srcStep, pDst, dstStep, DstFormat, roi);
293
  }
294
}
295
296
static inline int16x8_t loadUVreg(uint8x8_t Vraw)
297
{
298
  const int16x8_t V = vreinterpretq_s16_u16(vmovl_u8(Vraw));
299
  const int16x8_t c128 = vdupq_n_s16(128);
300
  const int16x8_t E = vsubq_s16(V, c128);
301
  return E;
302
}
303
304
static inline int16x8x2_t loadUV444(uint8x16_t Vld)
305
{
306
  const uint8x8x2_t V = { { vget_low_u8(Vld), vget_high_u8(Vld) } };
307
  const int16x8x2_t res = { {
308
    loadUVreg(V.val[0]),
309
    loadUVreg(V.val[1]),
310
  } };
311
  return res;
312
}
313
314
static inline void avgUV(BYTE U[2][2])
315
{
316
  const BYTE u00 = U[0][0];
317
  const INT16 umul = (INT16)u00 << 2;
318
  const INT16 sum = (INT16)U[0][1] + U[1][0] + U[1][1];
319
  const INT16 wavg = umul - sum;
320
  const BYTE val = CONDITIONAL_CLIP(wavg, u00);
321
  U[0][0] = val;
322
}
323
324
static inline void neon_avgUV(uint8x16_t pU[2])
325
{
326
  /* put even and odd values into different registers.
327
   * U 0/0 is in lower half */
328
  const uint8x16x2_t usplit = vuzpq_u8(pU[0], pU[1]);
329
  const uint8x16_t ueven = usplit.val[0];
330
  const uint8x16_t uodd = usplit.val[1];
331
332
  const uint8x8_t u00 = vget_low_u8(ueven);
333
  const uint8x8_t u01 = vget_low_u8(uodd);
334
  const uint8x8_t u10 = vget_high_u8(ueven);
335
  const uint8x8_t u11 = vget_high_u8(uodd);
336
337
  /* Create sum of U01 + U10 + U11 */
338
  const uint16x8_t uoddsum = vaddl_u8(u01, u10);
339
  const uint16x8_t usum = vaddq_u16(uoddsum, vmovl_u8(u11));
340
341
  /* U00 * 4 */
342
  const uint16x8_t umul = vshll_n_u8(u00, 2);
343
344
  /* U00 - (U01 + U10 + U11) */
345
  const int16x8_t wavg = vsubq_s16(vreinterpretq_s16_u16(umul), vreinterpretq_s16_u16(usum));
346
  const uint8x8_t avg = vqmovun_s16(wavg);
347
348
  /* abs(u00 - avg) */
349
  const uint8x8_t absdiff = vabd_u8(avg, u00);
350
351
  /* (diff < 30) ? u00 : avg */
352
  const uint8x8_t mask = vclt_u8(absdiff, vdup_n_u8(30));
353
354
  /* out1 = u00 & mask */
355
  const uint8x8_t out1 = vand_u8(u00, mask);
356
357
  /* invmask = ~mask */
358
  const uint8x8_t notmask = vmvn_u8(mask);
359
360
  /* out2 = avg & invmask */
361
  const uint8x8_t out2 = vand_u8(avg, notmask);
362
363
  /* out = out1 | out2 */
364
  const uint8x8_t out = vorr_u8(out1, out2);
365
366
  const uint8x8x2_t ua = vzip_u8(out, u01);
367
  const uint8x16_t u = vcombine_u8(ua.val[0], ua.val[1]);
368
  pU[0] = u;
369
}
370
371
static INLINE pstatus_t neon_YUV444ToX_SINGLE_ROW(const BYTE* WINPR_RESTRICT pY,
372
                                                  const BYTE* WINPR_RESTRICT pU,
373
                                                  const BYTE* WINPR_RESTRICT pV,
374
                                                  BYTE* WINPR_RESTRICT pRGB, size_t width,
375
                                                  const uint8_t rPos, const uint8_t gPos,
376
                                                  const uint8_t bPos, const uint8_t aPos)
377
{
378
  WINPR_ASSERT(width % 2 == 0);
379
380
  size_t x = 0;
381
382
  for (; x < width - width % 16; x += 16)
383
  {
384
    uint8x16_t U = vld1q_u8(&pU[x]);
385
    uint8x16_t V = vld1q_u8(&pV[x]);
386
    const uint8x16_t Y0raw = vld1q_u8(&pY[x]);
387
    const uint8x8x2_t Y0 = { { vget_low_u8(Y0raw), vget_high_u8(Y0raw) } };
388
    const int16x8x2_t D0 = loadUV444(U);
389
    const int16x8x2_t E0 = loadUV444(V);
390
    neon_YuvToRgbPixel(&pRGB[4ULL * x], Y0, D0, E0, rPos, gPos, bPos, aPos);
391
  }
392
393
  for (; x < width; x += 2)
394
  {
395
    BYTE* rgb = &pRGB[x * 4];
396
397
    for (size_t j = 0; j < 2; j++)
398
    {
399
      const BYTE y = pY[x + j];
400
      const BYTE u = pU[x + j];
401
      const BYTE v = pV[x + j];
402
403
      neon_write_pixel(&rgb[4 * (j)], y, u, v, rPos, gPos, bPos, aPos);
404
    }
405
  }
406
407
  return PRIMITIVES_SUCCESS;
408
}
409
410
static INLINE pstatus_t neon_YUV444ToX_DOUBLE_ROW(const BYTE* WINPR_RESTRICT pY[2],
411
                                                  const BYTE* WINPR_RESTRICT pU[2],
412
                                                  const BYTE* WINPR_RESTRICT pV[2],
413
                                                  BYTE* WINPR_RESTRICT pRGB[2], size_t width,
414
                                                  const uint8_t rPos, const uint8_t gPos,
415
                                                  const uint8_t bPos, const uint8_t aPos)
416
{
417
  WINPR_ASSERT(width % 2 == 0);
418
419
  size_t x = 0;
420
421
  for (; x < width - width % 16; x += 16)
422
  {
423
    uint8x16_t U[2] = { vld1q_u8(&pU[0][x]), vld1q_u8(&pU[1][x]) };
424
    neon_avgUV(U);
425
426
    uint8x16_t V[2] = { vld1q_u8(&pV[0][x]), vld1q_u8(&pV[1][x]) };
427
    neon_avgUV(V);
428
429
    const uint8x16_t Y0raw = vld1q_u8(&pY[0][x]);
430
    const uint8x8x2_t Y0 = { { vget_low_u8(Y0raw), vget_high_u8(Y0raw) } };
431
    const int16x8x2_t D0 = loadUV444(U[0]);
432
    const int16x8x2_t E0 = loadUV444(V[0]);
433
    neon_YuvToRgbPixel(&pRGB[0][4ULL * x], Y0, D0, E0, rPos, gPos, bPos, aPos);
434
435
    const uint8x16_t Y1raw = vld1q_u8(&pY[1][x]);
436
    const uint8x8x2_t Y1 = { { vget_low_u8(Y1raw), vget_high_u8(Y1raw) } };
437
    const int16x8x2_t D1 = loadUV444(U[1]);
438
    const int16x8x2_t E1 = loadUV444(V[1]);
439
    neon_YuvToRgbPixel(&pRGB[1][4ULL * x], Y1, D1, E1, rPos, gPos, bPos, aPos);
440
  }
441
442
  for (; x < width; x += 2)
443
  {
444
    BYTE* rgb[2] = { &pRGB[0][x * 4], &pRGB[1][x * 4] };
445
    BYTE U[2][2] = { { pU[0][x], pU[0][x + 1] }, { pU[1][x], pU[1][x + 1] } };
446
    avgUV(U);
447
448
    BYTE V[2][2] = { { pV[0][x], pV[0][x + 1] }, { pV[1][x], pV[1][x + 1] } };
449
    avgUV(V);
450
451
    for (size_t i = 0; i < 2; i++)
452
    {
453
      for (size_t j = 0; j < 2; j++)
454
      {
455
        const BYTE y = pY[i][x + j];
456
        const BYTE u = U[i][j];
457
        const BYTE v = V[i][j];
458
459
        neon_write_pixel(&rgb[i][4 * (j)], y, u, v, rPos, gPos, bPos, aPos);
460
      }
461
    }
462
  }
463
464
  return PRIMITIVES_SUCCESS;
465
}
466
467
static INLINE pstatus_t neon_YUV444ToX(const BYTE* WINPR_RESTRICT pSrc[3], const UINT32 srcStep[3],
468
                                       BYTE* WINPR_RESTRICT pDst, UINT32 dstStep,
469
                                       const prim_size_t* WINPR_RESTRICT roi, const uint8_t rPos,
470
                                       const uint8_t gPos, const uint8_t bPos, const uint8_t aPos)
471
{
472
  WINPR_ASSERT(roi);
473
  const UINT32 nWidth = roi->width;
474
  const UINT32 nHeight = roi->height;
475
476
  size_t y = 0;
477
  for (; y < nHeight - nHeight % 2; y += 2)
478
  {
479
    const uint8_t* WINPR_RESTRICT pY[2] = { pSrc[0] + y * srcStep[0],
480
                                          pSrc[0] + (y + 1) * srcStep[0] };
481
    const uint8_t* WINPR_RESTRICT pU[2] = { pSrc[1] + y * srcStep[1],
482
                                          pSrc[1] + (y + 1) * srcStep[1] };
483
    const uint8_t* WINPR_RESTRICT pV[2] = { pSrc[2] + y * srcStep[2],
484
                                          pSrc[2] + (y + 1) * srcStep[2] };
485
486
    uint8_t* WINPR_RESTRICT pRGB[2] = { &pDst[y * dstStep], &pDst[(y + 1) * dstStep] };
487
488
    const pstatus_t rc =
489
        neon_YUV444ToX_DOUBLE_ROW(pY, pU, pV, pRGB, nWidth, rPos, gPos, bPos, aPos);
490
    if (rc != PRIMITIVES_SUCCESS)
491
      return rc;
492
  }
493
  for (; y < nHeight; y++)
494
  {
495
    const uint8_t* WINPR_RESTRICT pY = pSrc[0] + y * srcStep[0];
496
    const uint8_t* WINPR_RESTRICT pU = pSrc[1] + y * srcStep[1];
497
    const uint8_t* WINPR_RESTRICT pV = pSrc[2] + y * srcStep[2];
498
    uint8_t* WINPR_RESTRICT pRGB = &pDst[y * dstStep];
499
500
    const pstatus_t rc =
501
        neon_YUV444ToX_SINGLE_ROW(pY, pU, pV, pRGB, nWidth, rPos, gPos, bPos, aPos);
502
    if (rc != PRIMITIVES_SUCCESS)
503
      return rc;
504
  }
505
506
  return PRIMITIVES_SUCCESS;
507
}
508
509
static pstatus_t neon_YUV444ToRGB_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc[3],
510
                                            const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
511
                                            UINT32 dstStep, UINT32 DstFormat,
512
                                            const prim_size_t* WINPR_RESTRICT roi)
513
{
514
  switch (DstFormat)
515
  {
516
    case PIXEL_FORMAT_BGRA32:
517
    case PIXEL_FORMAT_BGRX32:
518
      return neon_YUV444ToX(pSrc, srcStep, pDst, dstStep, roi, 2, 1, 0, 3);
519
520
    case PIXEL_FORMAT_RGBA32:
521
    case PIXEL_FORMAT_RGBX32:
522
      return neon_YUV444ToX(pSrc, srcStep, pDst, dstStep, roi, 0, 1, 2, 3);
523
524
    case PIXEL_FORMAT_ARGB32:
525
    case PIXEL_FORMAT_XRGB32:
526
      return neon_YUV444ToX(pSrc, srcStep, pDst, dstStep, roi, 1, 2, 3, 0);
527
528
    case PIXEL_FORMAT_ABGR32:
529
    case PIXEL_FORMAT_XBGR32:
530
      return neon_YUV444ToX(pSrc, srcStep, pDst, dstStep, roi, 3, 2, 1, 0);
531
532
    default:
533
      return generic->YUV444ToRGB_8u_P3AC4R(pSrc, srcStep, pDst, dstStep, DstFormat, roi);
534
  }
535
}
536
537
static pstatus_t neon_LumaToYUV444(const BYTE* WINPR_RESTRICT pSrcRaw[3], const UINT32 srcStep[3],
538
                                   BYTE* WINPR_RESTRICT pDstRaw[3], const UINT32 dstStep[3],
539
                                   const RECTANGLE_16* WINPR_RESTRICT roi)
540
{
541
  const UINT32 nWidth = roi->right - roi->left;
542
  const UINT32 nHeight = roi->bottom - roi->top;
543
  const UINT32 halfWidth = (nWidth + 1) / 2;
544
  const UINT32 halfHeight = (nHeight + 1) / 2;
545
  const UINT32 evenY = 0;
546
  const BYTE* pSrc[3] = { pSrcRaw[0] + roi->top * srcStep[0] + roi->left,
547
                        pSrcRaw[1] + roi->top / 2 * srcStep[1] + roi->left / 2,
548
                        pSrcRaw[2] + roi->top / 2 * srcStep[2] + roi->left / 2 };
549
  BYTE* pDst[3] = { pDstRaw[0] + roi->top * dstStep[0] + roi->left,
550
                  pDstRaw[1] + roi->top * dstStep[1] + roi->left,
551
                  pDstRaw[2] + roi->top * dstStep[2] + roi->left };
552
553
  /* Y data is already here... */
554
  /* B1 */
555
  for (UINT32 y = 0; y < nHeight; y++)
556
  {
557
    const BYTE* Ym = pSrc[0] + srcStep[0] * y;
558
    BYTE* pY = pDst[0] + dstStep[0] * y;
559
    memcpy(pY, Ym, nWidth);
560
  }
561
562
  /* The first half of U, V are already here part of this frame. */
563
  /* B2 and B3 */
564
  for (UINT32 y = 0; y < halfHeight; y++)
565
  {
566
    const UINT32 val2y = (2 * y + evenY);
567
    const BYTE* Um = pSrc[1] + srcStep[1] * y;
568
    const BYTE* Vm = pSrc[2] + srcStep[2] * y;
569
    BYTE* pU = pDst[1] + dstStep[1] * val2y;
570
    BYTE* pV = pDst[2] + dstStep[2] * val2y;
571
    BYTE* pU1 = pU + dstStep[1];
572
    BYTE* pV1 = pV + dstStep[2];
573
574
    UINT32 x = 0;
575
    for (; x + 16 < halfWidth; x += 16)
576
    {
577
      {
578
        const uint8x16_t u = vld1q_u8(Um);
579
        uint8x16x2_t u2x;
580
        u2x.val[0] = u;
581
        u2x.val[1] = u;
582
        vst2q_u8(pU, u2x);
583
        vst2q_u8(pU1, u2x);
584
        Um += 16;
585
        pU += 32;
586
        pU1 += 32;
587
      }
588
      {
589
        const uint8x16_t v = vld1q_u8(Vm);
590
        uint8x16x2_t v2x;
591
        v2x.val[0] = v;
592
        v2x.val[1] = v;
593
        vst2q_u8(pV, v2x);
594
        vst2q_u8(pV1, v2x);
595
        Vm += 16;
596
        pV += 32;
597
        pV1 += 32;
598
      }
599
    }
600
601
    for (; x < halfWidth; x++)
602
    {
603
      const BYTE u = *Um++;
604
      const BYTE v = *Vm++;
605
      *pU++ = u;
606
      *pU++ = u;
607
      *pU1++ = u;
608
      *pU1++ = u;
609
      *pV++ = v;
610
      *pV++ = v;
611
      *pV1++ = v;
612
      *pV1++ = v;
613
    }
614
  }
615
616
  return PRIMITIVES_SUCCESS;
617
}
618
619
static pstatus_t neon_ChromaV1ToYUV444(const BYTE* WINPR_RESTRICT pSrcRaw[3],
620
                                       const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDstRaw[3],
621
                                       const UINT32 dstStep[3],
622
                                       const RECTANGLE_16* WINPR_RESTRICT roi)
623
{
624
  const UINT32 mod = 16;
625
  UINT32 uY = 0;
626
  UINT32 vY = 0;
627
  const UINT32 nWidth = roi->right - roi->left;
628
  const UINT32 nHeight = roi->bottom - roi->top;
629
  const UINT32 halfWidth = (nWidth) / 2;
630
  const UINT32 halfHeight = (nHeight) / 2;
631
  const UINT32 oddY = 1;
632
  const UINT32 evenY = 0;
633
  const UINT32 oddX = 1;
634
  /* The auxiliary frame is aligned to multiples of 16x16.
635
   * We need the padded height for B4 and B5 conversion. */
636
  const UINT32 padHeigth = nHeight + 16 - nHeight % 16;
637
  const UINT32 halfPad = halfWidth % 16;
638
  const BYTE* pSrc[3] = { pSrcRaw[0] + roi->top * srcStep[0] + roi->left,
639
                        pSrcRaw[1] + roi->top / 2 * srcStep[1] + roi->left / 2,
640
                        pSrcRaw[2] + roi->top / 2 * srcStep[2] + roi->left / 2 };
641
  BYTE* pDst[3] = { pDstRaw[0] + roi->top * dstStep[0] + roi->left,
642
                  pDstRaw[1] + roi->top * dstStep[1] + roi->left,
643
                  pDstRaw[2] + roi->top * dstStep[2] + roi->left };
644
645
  /* The second half of U and V is a bit more tricky... */
646
  /* B4 and B5 */
647
  for (UINT32 y = 0; y < padHeigth; y++)
648
  {
649
    const BYTE* Ya = pSrc[0] + srcStep[0] * y;
650
    BYTE* pX;
651
652
    if ((y) % mod < (mod + 1) / 2)
653
    {
654
      const UINT32 pos = (2 * uY++ + oddY);
655
656
      if (pos >= nHeight)
657
        continue;
658
659
      pX = pDst[1] + dstStep[1] * pos;
660
    }
661
    else
662
    {
663
      const UINT32 pos = (2 * vY++ + oddY);
664
665
      if (pos >= nHeight)
666
        continue;
667
668
      pX = pDst[2] + dstStep[2] * pos;
669
    }
670
671
    memcpy(pX, Ya, nWidth);
672
  }
673
674
  /* B6 and B7 */
675
  for (UINT32 y = 0; y < halfHeight; y++)
676
  {
677
    const UINT32 val2y = (y * 2 + evenY);
678
    const BYTE* Ua = pSrc[1] + srcStep[1] * y;
679
    const BYTE* Va = pSrc[2] + srcStep[2] * y;
680
    BYTE* pU = pDst[1] + dstStep[1] * val2y;
681
    BYTE* pV = pDst[2] + dstStep[2] * val2y;
682
683
    UINT32 x = 0;
684
    for (; x < halfWidth - halfPad; x += 16)
685
    {
686
      {
687
        uint8x16x2_t u = vld2q_u8(&pU[2 * x]);
688
        u.val[1] = vld1q_u8(&Ua[x]);
689
        vst2q_u8(&pU[2 * x], u);
690
      }
691
      {
692
        uint8x16x2_t v = vld2q_u8(&pV[2 * x]);
693
        v.val[1] = vld1q_u8(&Va[x]);
694
        vst2q_u8(&pV[2 * x], v);
695
      }
696
    }
697
698
    for (; x < halfWidth; x++)
699
    {
700
      const UINT32 val2x1 = (x * 2 + oddX);
701
      pU[val2x1] = Ua[x];
702
      pV[val2x1] = Va[x];
703
    }
704
  }
705
706
  return PRIMITIVES_SUCCESS;
707
}
708
709
static pstatus_t neon_ChromaV2ToYUV444(const BYTE* WINPR_RESTRICT pSrc[3], const UINT32 srcStep[3],
710
                                       UINT32 nTotalWidth, UINT32 nTotalHeight,
711
                                       BYTE* WINPR_RESTRICT pDst[3], const UINT32 dstStep[3],
712
                                       const RECTANGLE_16* WINPR_RESTRICT roi)
713
{
714
  const UINT32 nWidth = roi->right - roi->left;
715
  const UINT32 nHeight = roi->bottom - roi->top;
716
  const UINT32 halfWidth = (nWidth + 1) / 2;
717
  const UINT32 halfPad = halfWidth % 16;
718
  const UINT32 halfHeight = (nHeight + 1) / 2;
719
  const UINT32 quaterWidth = (nWidth + 3) / 4;
720
  const UINT32 quaterPad = quaterWidth % 16;
721
722
  /* B4 and B5: odd UV values for width/2, height */
723
  for (UINT32 y = 0; y < nHeight; y++)
724
  {
725
    const UINT32 yTop = y + roi->top;
726
    const BYTE* pYaU = pSrc[0] + srcStep[0] * yTop + roi->left / 2;
727
    const BYTE* pYaV = pYaU + nTotalWidth / 2;
728
    BYTE* pU = pDst[1] + dstStep[1] * yTop + roi->left;
729
    BYTE* pV = pDst[2] + dstStep[2] * yTop + roi->left;
730
731
    UINT32 x = 0;
732
    for (; x < halfWidth - halfPad; x += 16)
733
    {
734
      {
735
        uint8x16x2_t u = vld2q_u8(&pU[2 * x]);
736
        u.val[1] = vld1q_u8(&pYaU[x]);
737
        vst2q_u8(&pU[2 * x], u);
738
      }
739
      {
740
        uint8x16x2_t v = vld2q_u8(&pV[2 * x]);
741
        v.val[1] = vld1q_u8(&pYaV[x]);
742
        vst2q_u8(&pV[2 * x], v);
743
      }
744
    }
745
746
    for (; x < halfWidth; x++)
747
    {
748
      const UINT32 odd = 2 * x + 1;
749
      pU[odd] = pYaU[x];
750
      pV[odd] = pYaV[x];
751
    }
752
  }
753
754
  /* B6 - B9 */
755
  for (UINT32 y = 0; y < halfHeight; y++)
756
  {
757
    const BYTE* pUaU = pSrc[1] + srcStep[1] * (y + roi->top / 2) + roi->left / 4;
758
    const BYTE* pUaV = pUaU + nTotalWidth / 4;
759
    const BYTE* pVaU = pSrc[2] + srcStep[2] * (y + roi->top / 2) + roi->left / 4;
760
    const BYTE* pVaV = pVaU + nTotalWidth / 4;
761
    BYTE* pU = pDst[1] + dstStep[1] * (2 * y + 1 + roi->top) + roi->left;
762
    BYTE* pV = pDst[2] + dstStep[2] * (2 * y + 1 + roi->top) + roi->left;
763
764
    UINT32 x = 0;
765
    for (; x < quaterWidth - quaterPad; x += 16)
766
    {
767
      {
768
        uint8x16x4_t u = vld4q_u8(&pU[4 * x]);
769
        u.val[0] = vld1q_u8(&pUaU[x]);
770
        u.val[2] = vld1q_u8(&pVaU[x]);
771
        vst4q_u8(&pU[4 * x], u);
772
      }
773
      {
774
        uint8x16x4_t v = vld4q_u8(&pV[4 * x]);
775
        v.val[0] = vld1q_u8(&pUaV[x]);
776
        v.val[2] = vld1q_u8(&pVaV[x]);
777
        vst4q_u8(&pV[4 * x], v);
778
      }
779
    }
780
781
    for (; x < quaterWidth; x++)
782
    {
783
      pU[4 * x + 0] = pUaU[x];
784
      pV[4 * x + 0] = pUaV[x];
785
      pU[4 * x + 2] = pVaU[x];
786
      pV[4 * x + 2] = pVaV[x];
787
    }
788
  }
789
790
  return PRIMITIVES_SUCCESS;
791
}
792
793
static pstatus_t neon_YUV420CombineToYUV444(avc444_frame_type type,
794
                                            const BYTE* WINPR_RESTRICT pSrc[3],
795
                                            const UINT32 srcStep[3], UINT32 nWidth, UINT32 nHeight,
796
                                            BYTE* WINPR_RESTRICT pDst[3], const UINT32 dstStep[3],
797
                                            const RECTANGLE_16* WINPR_RESTRICT roi)
798
{
799
  if (!pSrc || !pSrc[0] || !pSrc[1] || !pSrc[2])
800
    return -1;
801
802
  if (!pDst || !pDst[0] || !pDst[1] || !pDst[2])
803
    return -1;
804
805
  if (!roi)
806
    return -1;
807
808
  switch (type)
809
  {
810
    case AVC444_LUMA:
811
      return neon_LumaToYUV444(pSrc, srcStep, pDst, dstStep, roi);
812
813
    case AVC444_CHROMAv1:
814
      return neon_ChromaV1ToYUV444(pSrc, srcStep, pDst, dstStep, roi);
815
816
    case AVC444_CHROMAv2:
817
      return neon_ChromaV2ToYUV444(pSrc, srcStep, nWidth, nHeight, pDst, dstStep, roi);
818
819
    default:
820
      return -1;
821
  }
822
}
823
#endif
824
825
void primitives_init_YUV_neon_int(primitives_t* WINPR_RESTRICT prims)
826
0
{
827
#if defined(NEON_INTRINSICS_ENABLED)
828
  generic = primitives_get_generic();
829
  WLog_VRB(PRIM_TAG, "NEON optimizations");
830
  prims->YUV420ToRGB_8u_P3AC4R = neon_YUV420ToRGB_8u_P3AC4R;
831
  prims->YUV444ToRGB_8u_P3AC4R = neon_YUV444ToRGB_8u_P3AC4R;
832
  prims->YUV420CombineToYUV444 = neon_YUV420CombineToYUV444;
833
#else
834
0
  WLog_VRB(PRIM_TAG, "undefined WITH_SIMD or neon intrinsics not available");
835
0
  WINPR_UNUSED(prims);
836
0
#endif
837
0
}