Coverage Report

Created: 2023-09-25 06:56

/src/FreeRDP/libfreerdp/primitives/prim_YUV.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Generic YUV/RGB conversion operations
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015-2017 Armin Novak <armin.novak@thincast.com>
7
 * Copyright 2015-2017 Norbert Federa <norbert.federa@thincast.com>
8
 * Copyright 2015-2017 Vic Lee
9
 * Copyright 2015-2017 Thincast Technologies GmbH
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 *     http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23
24
#include <winpr/wtypes.h>
25
26
#include <freerdp/config.h>
27
28
#include <freerdp/types.h>
29
#include <freerdp/primitives.h>
30
#include <freerdp/codec/color.h>
31
#include "prim_internal.h"
32
33
static pstatus_t general_LumaToYUV444(const BYTE* const WINPR_RESTRICT pSrcRaw[3],
34
                                      const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDstRaw[3],
35
                                      const UINT32 dstStep[3],
36
                                      const RECTANGLE_16* WINPR_RESTRICT roi)
37
0
{
38
0
  const UINT32 nWidth = roi->right - roi->left;
39
0
  const UINT32 nHeight = roi->bottom - roi->top;
40
0
  const UINT32 halfWidth = (nWidth + 1) / 2;
41
0
  const UINT32 halfHeight = (nHeight + 1) / 2;
42
0
  const UINT32 oddY = 1;
43
0
  const UINT32 evenY = 0;
44
0
  const UINT32 oddX = 1;
45
0
  const UINT32 evenX = 0;
46
0
  const BYTE* pSrc[3] = { pSrcRaw[0] + roi->top * srcStep[0] + roi->left,
47
0
                        pSrcRaw[1] + roi->top / 2 * srcStep[1] + roi->left / 2,
48
0
                        pSrcRaw[2] + roi->top / 2 * srcStep[2] + roi->left / 2 };
49
0
  BYTE* pDst[3] = { pDstRaw[0] + roi->top * dstStep[0] + roi->left,
50
0
                  pDstRaw[1] + roi->top * dstStep[1] + roi->left,
51
0
                  pDstRaw[2] + roi->top * dstStep[2] + roi->left };
52
53
  /* Y data is already here... */
54
  /* B1 */
55
0
  for (UINT32 y = 0; y < nHeight; y++)
56
0
  {
57
0
    const BYTE* Ym = pSrc[0] + srcStep[0] * y;
58
0
    BYTE* pY = pDst[0] + dstStep[0] * y;
59
0
    memcpy(pY, Ym, nWidth);
60
0
  }
61
62
  /* The first half of U, V are already here part of this frame. */
63
  /* B2 and B3 */
64
0
  for (UINT32 y = 0; y < halfHeight; y++)
65
0
  {
66
0
    const UINT32 val2y = (2 * y + evenY);
67
0
    const UINT32 val2y1 = val2y + oddY;
68
0
    const BYTE* Um = pSrc[1] + srcStep[1] * y;
69
0
    const BYTE* Vm = pSrc[2] + srcStep[2] * y;
70
0
    BYTE* pU = pDst[1] + dstStep[1] * val2y;
71
0
    BYTE* pV = pDst[2] + dstStep[2] * val2y;
72
0
    BYTE* pU1 = pDst[1] + dstStep[1] * val2y1;
73
0
    BYTE* pV1 = pDst[2] + dstStep[2] * val2y1;
74
75
0
    for (UINT32 x = 0; x < halfWidth; x++)
76
0
    {
77
0
      const UINT32 val2x = 2 * x + evenX;
78
0
      const UINT32 val2x1 = val2x + oddX;
79
0
      pU[val2x] = Um[x];
80
0
      pV[val2x] = Vm[x];
81
0
      pU[val2x1] = Um[x];
82
0
      pV[val2x1] = Vm[x];
83
0
      pU1[val2x] = Um[x];
84
0
      pV1[val2x] = Vm[x];
85
0
      pU1[val2x1] = Um[x];
86
0
      pV1[val2x1] = Vm[x];
87
0
    }
88
0
  }
89
90
0
  return PRIMITIVES_SUCCESS;
91
0
}
92
93
static pstatus_t general_ChromaFilter(BYTE* WINPR_RESTRICT pDst[3], const UINT32 dstStep[3],
94
                                      const RECTANGLE_16* WINPR_RESTRICT roi)
95
0
{
96
0
  const UINT32 oddY = 1;
97
0
  const UINT32 evenY = 0;
98
0
  const UINT32 nWidth = roi->right - roi->left;
99
0
  const UINT32 nHeight = roi->bottom - roi->top;
100
0
  const UINT32 halfHeight = (nHeight + 1) / 2;
101
0
  const UINT32 halfWidth = (nWidth + 1) / 2;
102
0
  UINT32 x, y;
103
104
  /* Filter */
105
0
  for (y = roi->top; y < halfHeight + roi->top; y++)
106
0
  {
107
0
    const UINT32 val2y = (y * 2 + evenY);
108
0
    const UINT32 val2y1 = val2y + oddY;
109
0
    BYTE* pU1 = pDst[1] + dstStep[1] * val2y1;
110
0
    BYTE* pV1 = pDst[2] + dstStep[2] * val2y1;
111
0
    BYTE* pU = pDst[1] + dstStep[1] * val2y;
112
0
    BYTE* pV = pDst[2] + dstStep[2] * val2y;
113
114
0
    if (val2y1 > nHeight)
115
0
      continue;
116
117
0
    for (x = roi->left; x < halfWidth + roi->left; x++)
118
0
    {
119
0
      const UINT32 val2x = (x * 2);
120
0
      const UINT32 val2x1 = val2x + 1;
121
0
      const BYTE inU = pU[val2x];
122
0
      const BYTE inV = pV[val2x];
123
0
      const INT32 up = inU * 4;
124
0
      const INT32 vp = inV * 4;
125
0
      INT32 u2020;
126
0
      INT32 v2020;
127
128
0
      if (val2x1 > nWidth)
129
0
        continue;
130
131
0
      u2020 = up - pU[val2x1] - pU1[val2x] - pU1[val2x1];
132
0
      v2020 = vp - pV[val2x1] - pV1[val2x] - pV1[val2x1];
133
134
0
      pU[val2x] = CONDITIONAL_CLIP(u2020, inU);
135
0
      pV[val2x] = CONDITIONAL_CLIP(v2020, inV);
136
0
    }
137
0
  }
138
139
0
  return PRIMITIVES_SUCCESS;
140
0
}
141
142
static pstatus_t general_ChromaV1ToYUV444(const BYTE* const WINPR_RESTRICT pSrcRaw[3],
143
                                          const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDstRaw[3],
144
                                          const UINT32 dstStep[3],
145
                                          const RECTANGLE_16* WINPR_RESTRICT roi)
146
0
{
147
0
  const UINT32 mod = 16;
148
0
  UINT32 uY = 0;
149
0
  UINT32 vY = 0;
150
0
  UINT32 x, y;
151
0
  const UINT32 nWidth = roi->right - roi->left;
152
0
  const UINT32 nHeight = roi->bottom - roi->top;
153
0
  const UINT32 halfWidth = (nWidth) / 2;
154
0
  const UINT32 halfHeight = (nHeight) / 2;
155
0
  const UINT32 oddY = 1;
156
0
  const UINT32 evenY = 0;
157
0
  const UINT32 oddX = 1;
158
  /* The auxilary frame is aligned to multiples of 16x16.
159
   * We need the padded height for B4 and B5 conversion. */
160
0
  const UINT32 padHeigth = nHeight + 16 - nHeight % 16;
161
0
  const BYTE* pSrc[3] = { pSrcRaw[0] + roi->top * srcStep[0] + roi->left,
162
0
                        pSrcRaw[1] + roi->top / 2 * srcStep[1] + roi->left / 2,
163
0
                        pSrcRaw[2] + roi->top / 2 * srcStep[2] + roi->left / 2 };
164
0
  BYTE* pDst[3] = { pDstRaw[0] + roi->top * dstStep[0] + roi->left,
165
0
                  pDstRaw[1] + roi->top * dstStep[1] + roi->left,
166
0
                  pDstRaw[2] + roi->top * dstStep[2] + roi->left };
167
168
  /* The second half of U and V is a bit more tricky... */
169
  /* B4 and B5 */
170
0
  for (y = 0; y < padHeigth; y++)
171
0
  {
172
0
    const BYTE* Ya = pSrc[0] + srcStep[0] * y;
173
0
    BYTE* pX;
174
175
0
    if ((y) % mod < (mod + 1) / 2)
176
0
    {
177
0
      const UINT32 pos = (2 * uY++ + oddY);
178
179
0
      if (pos >= nHeight)
180
0
        continue;
181
182
0
      pX = pDst[1] + dstStep[1] * pos;
183
0
    }
184
0
    else
185
0
    {
186
0
      const UINT32 pos = (2 * vY++ + oddY);
187
188
0
      if (pos >= nHeight)
189
0
        continue;
190
191
0
      pX = pDst[2] + dstStep[2] * pos;
192
0
    }
193
194
0
    memcpy(pX, Ya, nWidth);
195
0
  }
196
197
  /* B6 and B7 */
198
0
  for (y = 0; y < halfHeight; y++)
199
0
  {
200
0
    const UINT32 val2y = (y * 2 + evenY);
201
0
    const BYTE* Ua = pSrc[1] + srcStep[1] * y;
202
0
    const BYTE* Va = pSrc[2] + srcStep[2] * y;
203
0
    BYTE* pU = pDst[1] + dstStep[1] * val2y;
204
0
    BYTE* pV = pDst[2] + dstStep[2] * val2y;
205
206
0
    for (x = 0; x < halfWidth; x++)
207
0
    {
208
0
      const UINT32 val2x1 = (x * 2 + oddX);
209
0
      pU[val2x1] = Ua[x];
210
0
      pV[val2x1] = Va[x];
211
0
    }
212
0
  }
213
214
  /* Filter */
215
0
  return general_ChromaFilter(pDst, dstStep, roi);
216
0
}
217
218
static pstatus_t general_ChromaV2ToYUV444(const BYTE* const WINPR_RESTRICT pSrc[3],
219
                                          const UINT32 srcStep[3], UINT32 nTotalWidth,
220
                                          UINT32 nTotalHeight, BYTE* WINPR_RESTRICT pDst[3],
221
                                          const UINT32 dstStep[3],
222
                                          const RECTANGLE_16* WINPR_RESTRICT roi)
223
0
{
224
0
  UINT32 x, y;
225
0
  const UINT32 nWidth = roi->right - roi->left;
226
0
  const UINT32 nHeight = roi->bottom - roi->top;
227
0
  const UINT32 halfWidth = (nWidth + 1) / 2;
228
0
  const UINT32 halfHeight = (nHeight + 1) / 2;
229
0
  const UINT32 quaterWidth = (nWidth + 3) / 4;
230
231
  /* B4 and B5: odd UV values for width/2, height */
232
0
  for (y = 0; y < nHeight; y++)
233
0
  {
234
0
    const UINT32 yTop = y + roi->top;
235
0
    const BYTE* pYaU = pSrc[0] + srcStep[0] * yTop + roi->left / 2;
236
0
    const BYTE* pYaV = pYaU + nTotalWidth / 2;
237
0
    BYTE* pU = pDst[1] + dstStep[1] * yTop + roi->left;
238
0
    BYTE* pV = pDst[2] + dstStep[2] * yTop + roi->left;
239
240
0
    for (x = 0; x < halfWidth; x++)
241
0
    {
242
0
      const UINT32 odd = 2 * x + 1;
243
0
      pU[odd] = *pYaU++;
244
0
      pV[odd] = *pYaV++;
245
0
    }
246
0
  }
247
248
  /* B6 - B9 */
249
0
  for (y = 0; y < halfHeight; y++)
250
0
  {
251
0
    const BYTE* pUaU = pSrc[1] + srcStep[1] * (y + roi->top / 2) + roi->left / 4;
252
0
    const BYTE* pUaV = pUaU + nTotalWidth / 4;
253
0
    const BYTE* pVaU = pSrc[2] + srcStep[2] * (y + roi->top / 2) + roi->left / 4;
254
0
    const BYTE* pVaV = pVaU + nTotalWidth / 4;
255
0
    BYTE* pU = pDst[1] + dstStep[1] * (2 * y + 1 + roi->top) + roi->left;
256
0
    BYTE* pV = pDst[2] + dstStep[2] * (2 * y + 1 + roi->top) + roi->left;
257
258
0
    for (x = 0; x < quaterWidth; x++)
259
0
    {
260
0
      pU[4 * x + 0] = *pUaU++;
261
0
      pV[4 * x + 0] = *pUaV++;
262
0
      pU[4 * x + 2] = *pVaU++;
263
0
      pV[4 * x + 2] = *pVaV++;
264
0
    }
265
0
  }
266
267
0
  return general_ChromaFilter(pDst, dstStep, roi);
268
0
}
269
270
static pstatus_t general_YUV420CombineToYUV444(avc444_frame_type type,
271
                                               const BYTE* const WINPR_RESTRICT pSrc[3],
272
                                               const UINT32 srcStep[3], UINT32 nWidth,
273
                                               UINT32 nHeight, BYTE* WINPR_RESTRICT pDst[3],
274
                                               const UINT32 dstStep[3],
275
                                               const RECTANGLE_16* WINPR_RESTRICT roi)
276
0
{
277
0
  if (!pSrc || !pSrc[0] || !pSrc[1] || !pSrc[2])
278
0
    return -1;
279
280
0
  if (!pDst || !pDst[0] || !pDst[1] || !pDst[2])
281
0
    return -1;
282
283
0
  if (!roi)
284
0
    return -1;
285
286
0
  switch (type)
287
0
  {
288
0
    case AVC444_LUMA:
289
0
      return general_LumaToYUV444(pSrc, srcStep, pDst, dstStep, roi);
290
291
0
    case AVC444_CHROMAv1:
292
0
      return general_ChromaV1ToYUV444(pSrc, srcStep, pDst, dstStep, roi);
293
294
0
    case AVC444_CHROMAv2:
295
0
      return general_ChromaV2ToYUV444(pSrc, srcStep, nWidth, nHeight, pDst, dstStep, roi);
296
297
0
    default:
298
0
      return -1;
299
0
  }
300
0
}
301
302
static pstatus_t
303
general_YUV444SplitToYUV420(const BYTE* const WINPR_RESTRICT pSrc[3], const UINT32 srcStep[3],
304
                            BYTE* WINPR_RESTRICT pMainDst[3], const UINT32 dstMainStep[3],
305
                            BYTE* WINPR_RESTRICT pAuxDst[3], const UINT32 dstAuxStep[3],
306
                            const prim_size_t* WINPR_RESTRICT roi)
307
0
{
308
0
  UINT32 x, y, uY = 0, vY = 0;
309
0
  UINT32 halfWidth, halfHeight;
310
  /* The auxilary frame is aligned to multiples of 16x16.
311
   * We need the padded height for B4 and B5 conversion. */
312
0
  const UINT32 padHeigth = roi->height + 16 - roi->height % 16;
313
0
  halfWidth = (roi->width + 1) / 2;
314
0
  halfHeight = (roi->height + 1) / 2;
315
316
  /* B1 */
317
0
  for (y = 0; y < roi->height; y++)
318
0
  {
319
0
    const BYTE* pSrcY = pSrc[0] + y * srcStep[0];
320
0
    BYTE* pY = pMainDst[0] + y * dstMainStep[0];
321
0
    memcpy(pY, pSrcY, roi->width);
322
0
  }
323
324
  /* B2 and B3 */
325
0
  for (y = 0; y < halfHeight; y++)
326
0
  {
327
0
    const BYTE* pSrcU = pSrc[1] + 2 * y * srcStep[1];
328
0
    const BYTE* pSrcV = pSrc[2] + 2 * y * srcStep[2];
329
0
    const BYTE* pSrcU1 = pSrc[1] + (2 * y + 1) * srcStep[1];
330
0
    const BYTE* pSrcV1 = pSrc[2] + (2 * y + 1) * srcStep[2];
331
0
    BYTE* pU = pMainDst[1] + y * dstMainStep[1];
332
0
    BYTE* pV = pMainDst[2] + y * dstMainStep[2];
333
334
0
    for (x = 0; x < halfWidth; x++)
335
0
    {
336
      /* Filter */
337
0
      const INT32 u = pSrcU[2 * x] + pSrcU[2 * x + 1] + pSrcU1[2 * x] + pSrcU1[2 * x + 1];
338
0
      const INT32 v = pSrcV[2 * x] + pSrcV[2 * x + 1] + pSrcV1[2 * x] + pSrcV1[2 * x + 1];
339
0
      pU[x] = CLIP(u / 4L);
340
0
      pV[x] = CLIP(v / 4L);
341
0
    }
342
0
  }
343
344
  /* B4 and B5 */
345
0
  for (y = 0; y < padHeigth; y++)
346
0
  {
347
0
    BYTE* pY = pAuxDst[0] + y * dstAuxStep[0];
348
349
0
    if (y % 16 < 8)
350
0
    {
351
0
      const UINT32 pos = (2 * uY++ + 1);
352
0
      const BYTE* pSrcU = pSrc[1] + pos * srcStep[1];
353
354
0
      if (pos >= roi->height)
355
0
        continue;
356
357
0
      memcpy(pY, pSrcU, roi->width);
358
0
    }
359
0
    else
360
0
    {
361
0
      const UINT32 pos = (2 * vY++ + 1);
362
0
      const BYTE* pSrcV = pSrc[2] + pos * srcStep[2];
363
364
0
      if (pos >= roi->height)
365
0
        continue;
366
367
0
      memcpy(pY, pSrcV, roi->width);
368
0
    }
369
0
  }
370
371
  /* B6 and B7 */
372
0
  for (y = 0; y < halfHeight; y++)
373
0
  {
374
0
    const BYTE* pSrcU = pSrc[1] + 2 * y * srcStep[1];
375
0
    const BYTE* pSrcV = pSrc[2] + 2 * y * srcStep[2];
376
0
    BYTE* pU = pAuxDst[1] + y * dstAuxStep[1];
377
0
    BYTE* pV = pAuxDst[2] + y * dstAuxStep[2];
378
379
0
    for (x = 0; x < halfWidth; x++)
380
0
    {
381
0
      pU[x] = pSrcU[2 * x + 1];
382
0
      pV[x] = pSrcV[2 * x + 1];
383
0
    }
384
0
  }
385
386
0
  return PRIMITIVES_SUCCESS;
387
0
}
388
389
static pstatus_t general_YUV444ToRGB_8u_P3AC4R_general(const BYTE* const WINPR_RESTRICT pSrc[3],
390
                                                       const UINT32 srcStep[3],
391
                                                       BYTE* WINPR_RESTRICT pDst, UINT32 dstStep,
392
                                                       UINT32 DstFormat,
393
                                                       const prim_size_t* WINPR_RESTRICT roi)
394
0
{
395
0
  const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
396
0
  fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
397
398
0
  WINPR_ASSERT(pSrc);
399
0
  WINPR_ASSERT(pDst);
400
0
  WINPR_ASSERT(roi);
401
402
0
  const UINT32 nWidth = roi->width;
403
0
  const UINT32 nHeight = roi->height;
404
405
0
  for (UINT32 y = 0; y < nHeight; y++)
406
0
  {
407
0
    const BYTE* pY = pSrc[0] + y * srcStep[0];
408
0
    const BYTE* pU = pSrc[1] + y * srcStep[1];
409
0
    const BYTE* pV = pSrc[2] + y * srcStep[2];
410
0
    BYTE* pRGB = pDst + y * dstStep;
411
412
0
    for (UINT32 x = 0; x < nWidth; x++)
413
0
    {
414
0
      const BYTE Y = pY[x];
415
0
      const BYTE U = pU[x];
416
0
      const BYTE V = pV[x];
417
0
      const BYTE r = YUV2R(Y, U, V);
418
0
      const BYTE g = YUV2G(Y, U, V);
419
0
      const BYTE b = YUV2B(Y, U, V);
420
0
      pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
421
0
    }
422
0
  }
423
424
0
  return PRIMITIVES_SUCCESS;
425
0
}
426
427
static pstatus_t general_YUV444ToRGB_8u_P3AC4R_BGRX(const BYTE* const WINPR_RESTRICT pSrc[3],
428
                                                    const UINT32 srcStep[3],
429
                                                    BYTE* WINPR_RESTRICT pDst, UINT32 dstStep,
430
                                                    UINT32 DstFormat,
431
                                                    const prim_size_t* WINPR_RESTRICT roi)
432
0
{
433
0
  const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
434
435
0
  WINPR_ASSERT(pSrc);
436
0
  WINPR_ASSERT(pDst);
437
0
  WINPR_ASSERT(roi);
438
439
0
  const UINT32 nWidth = roi->width;
440
0
  const UINT32 nHeight = roi->height;
441
442
0
  for (UINT32 y = 0; y < nHeight; y++)
443
0
  {
444
0
    const BYTE* pY = pSrc[0] + y * srcStep[0];
445
0
    const BYTE* pU = pSrc[1] + y * srcStep[1];
446
0
    const BYTE* pV = pSrc[2] + y * srcStep[2];
447
0
    BYTE* pRGB = pDst + y * dstStep;
448
449
0
    for (UINT32 x = 0; x < nWidth; x++)
450
0
    {
451
0
      const BYTE Y = pY[x];
452
0
      const BYTE U = pU[x];
453
0
      const BYTE V = pV[x];
454
0
      const BYTE r = YUV2R(Y, U, V);
455
0
      const BYTE g = YUV2G(Y, U, V);
456
0
      const BYTE b = YUV2B(Y, U, V);
457
0
      pRGB = writePixelBGRX(pRGB, formatSize, DstFormat, r, g, b, 0);
458
0
    }
459
0
  }
460
461
0
  return PRIMITIVES_SUCCESS;
462
0
}
463
464
static pstatus_t general_YUV444ToRGB_8u_P3AC4R(const BYTE* const WINPR_RESTRICT pSrc[3],
465
                                               const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
466
                                               UINT32 dstStep, UINT32 DstFormat,
467
                                               const prim_size_t* WINPR_RESTRICT roi)
468
0
{
469
0
  switch (DstFormat)
470
0
  {
471
0
    case PIXEL_FORMAT_BGRA32:
472
0
    case PIXEL_FORMAT_BGRX32:
473
0
      return general_YUV444ToRGB_8u_P3AC4R_BGRX(pSrc, srcStep, pDst, dstStep, DstFormat, roi);
474
475
0
    default:
476
0
      return general_YUV444ToRGB_8u_P3AC4R_general(pSrc, srcStep, pDst, dstStep, DstFormat,
477
0
                                                   roi);
478
0
  }
479
0
}
480
/**
481
 * | R |   ( | 256     0    403 | |    Y    | )
482
 * | G | = ( | 256   -48   -120 | | U - 128 | ) >> 8
483
 * | B |   ( | 256   475      0 | | V - 128 | )
484
 */
485
static pstatus_t general_YUV420ToRGB_8u_P3AC4R(const BYTE* const WINPR_RESTRICT pSrc[3],
486
                                               const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
487
                                               UINT32 dstStep, UINT32 DstFormat,
488
                                               const prim_size_t* WINPR_RESTRICT roi)
489
0
{
490
0
  UINT32 x, y;
491
0
  UINT32 dstPad;
492
0
  UINT32 srcPad[3];
493
0
  BYTE Y, U, V;
494
0
  UINT32 halfWidth;
495
0
  UINT32 halfHeight;
496
0
  const BYTE* pY;
497
0
  const BYTE* pU;
498
0
  const BYTE* pV;
499
0
  BYTE* pRGB = pDst;
500
0
  UINT32 nWidth, nHeight;
501
0
  UINT32 lastRow, lastCol;
502
0
  const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
503
0
  fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
504
0
  pY = pSrc[0];
505
0
  pU = pSrc[1];
506
0
  pV = pSrc[2];
507
0
  lastCol = roi->width & 0x01;
508
0
  lastRow = roi->height & 0x01;
509
0
  nWidth = (roi->width + 1) & ~0x0001;
510
0
  nHeight = (roi->height + 1) & ~0x0001;
511
0
  halfWidth = nWidth / 2;
512
0
  halfHeight = nHeight / 2;
513
0
  srcPad[0] = (srcStep[0] - nWidth);
514
0
  srcPad[1] = (srcStep[1] - halfWidth);
515
0
  srcPad[2] = (srcStep[2] - halfWidth);
516
0
  dstPad = (dstStep - (nWidth * 4));
517
518
0
  for (y = 0; y < halfHeight;)
519
0
  {
520
0
    if (++y == halfHeight)
521
0
      lastRow <<= 1;
522
523
0
    for (x = 0; x < halfWidth;)
524
0
    {
525
0
      BYTE r;
526
0
      BYTE g;
527
0
      BYTE b;
528
529
0
      if (++x == halfWidth)
530
0
        lastCol <<= 1;
531
532
0
      U = *pU++;
533
0
      V = *pV++;
534
      /* 1st pixel */
535
0
      Y = *pY++;
536
0
      r = YUV2R(Y, U, V);
537
0
      g = YUV2G(Y, U, V);
538
0
      b = YUV2B(Y, U, V);
539
0
      pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
540
541
      /* 2nd pixel */
542
0
      if (!(lastCol & 0x02))
543
0
      {
544
0
        Y = *pY++;
545
0
        r = YUV2R(Y, U, V);
546
0
        g = YUV2G(Y, U, V);
547
0
        b = YUV2B(Y, U, V);
548
0
        pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
549
0
      }
550
0
      else
551
0
      {
552
0
        pY++;
553
0
        pRGB += formatSize;
554
0
        lastCol >>= 1;
555
0
      }
556
0
    }
557
558
0
    pY += srcPad[0];
559
0
    pU -= halfWidth;
560
0
    pV -= halfWidth;
561
0
    pRGB += dstPad;
562
563
0
    if (lastRow & 0x02)
564
0
      break;
565
566
0
    for (x = 0; x < halfWidth;)
567
0
    {
568
0
      BYTE r;
569
0
      BYTE g;
570
0
      BYTE b;
571
572
0
      if (++x == halfWidth)
573
0
        lastCol <<= 1;
574
575
0
      U = *pU++;
576
0
      V = *pV++;
577
      /* 3rd pixel */
578
0
      Y = *pY++;
579
0
      r = YUV2R(Y, U, V);
580
0
      g = YUV2G(Y, U, V);
581
0
      b = YUV2B(Y, U, V);
582
0
      pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
583
584
      /* 4th pixel */
585
0
      if (!(lastCol & 0x02))
586
0
      {
587
0
        Y = *pY++;
588
0
        r = YUV2R(Y, U, V);
589
0
        g = YUV2G(Y, U, V);
590
0
        b = YUV2B(Y, U, V);
591
0
        pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
592
0
      }
593
0
      else
594
0
      {
595
0
        pY++;
596
0
        pRGB += formatSize;
597
0
        lastCol >>= 1;
598
0
      }
599
0
    }
600
601
0
    pY += srcPad[0];
602
0
    pU += srcPad[1];
603
0
    pV += srcPad[2];
604
0
    pRGB += dstPad;
605
0
  }
606
607
0
  return PRIMITIVES_SUCCESS;
608
0
}
609
610
/**
611
 * | Y |    ( |  54   183     18 | | R | )        |  0  |
612
 * | U | =  ( | -29   -99    128 | | G | ) >> 8 + | 128 |
613
 * | V |    ( | 128  -116    -12 | | B | )        | 128 |
614
 */
615
static INLINE BYTE RGB2Y(BYTE R, BYTE G, BYTE B)
616
0
{
617
0
  return (54 * R + 183 * G + 18 * B) >> 8;
618
0
}
619
620
static INLINE BYTE RGB2U(BYTE R, BYTE G, BYTE B)
621
0
{
622
0
  return ((-29 * R - 99 * G + 128 * B) >> 8) + 128;
623
0
}
624
625
static INLINE BYTE RGB2V(INT32 R, INT32 G, INT32 B)
626
0
{
627
0
  return ((128 * R - 116 * G - 12 * B) >> 8) + 128;
628
0
}
629
630
static pstatus_t general_RGBToYUV444_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc, UINT32 SrcFormat,
631
                                               const UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
632
                                               UINT32 dstStep[3],
633
                                               const prim_size_t* WINPR_RESTRICT roi)
634
0
{
635
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
636
0
  UINT32 x, y;
637
0
  UINT32 nWidth, nHeight;
638
0
  nWidth = roi->width;
639
0
  nHeight = roi->height;
640
641
0
  for (y = 0; y < nHeight; y++)
642
0
  {
643
0
    const BYTE* pRGB = pSrc + y * srcStep;
644
0
    BYTE* pY = pDst[0] + y * dstStep[0];
645
0
    BYTE* pU = pDst[1] + y * dstStep[1];
646
0
    BYTE* pV = pDst[2] + y * dstStep[2];
647
648
0
    for (x = 0; x < nWidth; x++)
649
0
    {
650
0
      BYTE B, G, R;
651
0
      const UINT32 color = FreeRDPReadColor(&pRGB[x * bpp], SrcFormat);
652
0
      FreeRDPSplitColor(color, SrcFormat, &R, &G, &B, NULL, NULL);
653
0
      pY[x] = RGB2Y(R, G, B);
654
0
      pU[x] = RGB2U(R, G, B);
655
0
      pV[x] = RGB2V(R, G, B);
656
0
    }
657
0
  }
658
659
0
  return PRIMITIVES_SUCCESS;
660
0
}
661
662
static INLINE pstatus_t general_RGBToYUV420_BGRX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
663
                                                 BYTE* WINPR_RESTRICT pDst[3],
664
                                                 const UINT32 dstStep[3],
665
                                                 const prim_size_t* WINPR_RESTRICT roi)
666
0
{
667
0
  UINT32 x, y, i;
668
0
  size_t x1 = 0, x2 = 4, x3 = srcStep, x4 = srcStep + 4;
669
0
  size_t y1 = 0, y2 = 1, y3 = dstStep[0], y4 = dstStep[0] + 1;
670
0
  UINT32 max_x = roi->width - 1;
671
0
  UINT32 max_y = roi->height - 1;
672
673
0
  for (y = i = 0; y < roi->height; y += 2, i++)
674
0
  {
675
0
    const BYTE* src = pSrc + y * srcStep;
676
0
    BYTE* ydst = pDst[0] + y * dstStep[0];
677
0
    BYTE* udst = pDst[1] + i * dstStep[1];
678
0
    BYTE* vdst = pDst[2] + i * dstStep[2];
679
680
0
    for (x = 0; x < roi->width; x += 2)
681
0
    {
682
0
      BYTE R, G, B;
683
0
      INT32 Ra, Ga, Ba;
684
      /* row 1, pixel 1 */
685
0
      Ba = B = *(src + x1 + 0);
686
0
      Ga = G = *(src + x1 + 1);
687
0
      Ra = R = *(src + x1 + 2);
688
0
      ydst[y1] = RGB2Y(R, G, B);
689
690
0
      if (x < max_x)
691
0
      {
692
        /* row 1, pixel 2 */
693
0
        Ba += B = *(src + x2 + 0);
694
0
        Ga += G = *(src + x2 + 1);
695
0
        Ra += R = *(src + x2 + 2);
696
0
        ydst[y2] = RGB2Y(R, G, B);
697
0
      }
698
699
0
      if (y < max_y)
700
0
      {
701
        /* row 2, pixel 1 */
702
0
        Ba += B = *(src + x3 + 0);
703
0
        Ga += G = *(src + x3 + 1);
704
0
        Ra += R = *(src + x3 + 2);
705
0
        ydst[y3] = RGB2Y(R, G, B);
706
707
0
        if (x < max_x)
708
0
        {
709
          /* row 2, pixel 2 */
710
0
          Ba += B = *(src + x4 + 0);
711
0
          Ga += G = *(src + x4 + 1);
712
0
          Ra += R = *(src + x4 + 2);
713
0
          ydst[y4] = RGB2Y(R, G, B);
714
0
        }
715
0
      }
716
717
0
      Ba >>= 2;
718
0
      Ga >>= 2;
719
0
      Ra >>= 2;
720
0
      *udst++ = RGB2U(Ra, Ga, Ba);
721
0
      *vdst++ = RGB2V(Ra, Ga, Ba);
722
0
      ydst += 2;
723
0
      src += 8;
724
0
    }
725
0
  }
726
727
0
  return PRIMITIVES_SUCCESS;
728
0
}
729
730
static INLINE pstatus_t general_RGBToYUV420_RGBX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
731
                                                 BYTE* WINPR_RESTRICT pDst[3],
732
                                                 const UINT32 dstStep[3],
733
                                                 const prim_size_t* WINPR_RESTRICT roi)
734
0
{
735
0
  UINT32 x, y, i;
736
0
  size_t x1 = 0, x2 = 4, x3 = srcStep, x4 = srcStep + 4;
737
0
  size_t y1 = 0, y2 = 1, y3 = dstStep[0], y4 = dstStep[0] + 1;
738
0
  UINT32 max_x = roi->width - 1;
739
0
  UINT32 max_y = roi->height - 1;
740
741
0
  for (y = i = 0; y < roi->height; y += 2, i++)
742
0
  {
743
0
    const BYTE* src = pSrc + y * srcStep;
744
0
    BYTE* ydst = pDst[0] + y * dstStep[0];
745
0
    BYTE* udst = pDst[1] + i * dstStep[1];
746
0
    BYTE* vdst = pDst[2] + i * dstStep[2];
747
748
0
    for (x = 0; x < roi->width; x += 2)
749
0
    {
750
0
      BYTE R, G, B;
751
0
      INT32 Ra, Ga, Ba;
752
      /* row 1, pixel 1 */
753
0
      Ra = R = *(src + x1 + 0);
754
0
      Ga = G = *(src + x1 + 1);
755
0
      Ba = B = *(src + x1 + 2);
756
0
      ydst[y1] = RGB2Y(R, G, B);
757
758
0
      if (x < max_x)
759
0
      {
760
        /* row 1, pixel 2 */
761
0
        Ra += R = *(src + x2 + 0);
762
0
        Ga += G = *(src + x2 + 1);
763
0
        Ba += B = *(src + x2 + 2);
764
0
        ydst[y2] = RGB2Y(R, G, B);
765
0
      }
766
767
0
      if (y < max_y)
768
0
      {
769
        /* row 2, pixel 1 */
770
0
        Ra += R = *(src + x3 + 0);
771
0
        Ga += G = *(src + x3 + 1);
772
0
        Ba += B = *(src + x3 + 2);
773
0
        ydst[y3] = RGB2Y(R, G, B);
774
775
0
        if (x < max_x)
776
0
        {
777
          /* row 2, pixel 2 */
778
0
          Ra += R = *(src + x4 + 0);
779
0
          Ga += G = *(src + x4 + 1);
780
0
          Ba += B = *(src + x4 + 2);
781
0
          ydst[y4] = RGB2Y(R, G, B);
782
0
        }
783
0
      }
784
785
0
      Ba >>= 2;
786
0
      Ga >>= 2;
787
0
      Ra >>= 2;
788
0
      *udst++ = RGB2U(Ra, Ga, Ba);
789
0
      *vdst++ = RGB2V(Ra, Ga, Ba);
790
0
      ydst += 2;
791
0
      src += 8;
792
0
    }
793
0
  }
794
795
0
  return PRIMITIVES_SUCCESS;
796
0
}
797
798
static INLINE pstatus_t general_RGBToYUV420_ANY(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
799
                                                UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
800
                                                const UINT32 dstStep[3],
801
                                                const prim_size_t* WINPR_RESTRICT roi)
802
0
{
803
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
804
0
  UINT32 x, y, i;
805
0
  size_t x1 = 0, x2 = bpp, x3 = srcStep, x4 = srcStep + bpp;
806
0
  size_t y1 = 0, y2 = 1, y3 = dstStep[0], y4 = dstStep[0] + 1;
807
0
  UINT32 max_x = roi->width - 1;
808
0
  UINT32 max_y = roi->height - 1;
809
810
0
  for (y = i = 0; y < roi->height; y += 2, i++)
811
0
  {
812
0
    const BYTE* src = pSrc + y * srcStep;
813
0
    BYTE* ydst = pDst[0] + y * dstStep[0];
814
0
    BYTE* udst = pDst[1] + i * dstStep[1];
815
0
    BYTE* vdst = pDst[2] + i * dstStep[2];
816
817
0
    for (x = 0; x < roi->width; x += 2)
818
0
    {
819
0
      BYTE R, G, B;
820
0
      INT32 Ra, Ga, Ba;
821
0
      UINT32 color;
822
      /* row 1, pixel 1 */
823
0
      color = FreeRDPReadColor(src + x1, srcFormat);
824
0
      FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
825
0
      Ra = R;
826
0
      Ga = G;
827
0
      Ba = B;
828
0
      ydst[y1] = RGB2Y(R, G, B);
829
830
0
      if (x < max_x)
831
0
      {
832
        /* row 1, pixel 2 */
833
0
        color = FreeRDPReadColor(src + x2, srcFormat);
834
0
        FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
835
0
        Ra += R;
836
0
        Ga += G;
837
0
        Ba += B;
838
0
        ydst[y2] = RGB2Y(R, G, B);
839
0
      }
840
841
0
      if (y < max_y)
842
0
      {
843
        /* row 2, pixel 1 */
844
0
        color = FreeRDPReadColor(src + x3, srcFormat);
845
0
        FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
846
0
        Ra += R;
847
0
        Ga += G;
848
0
        Ba += B;
849
0
        ydst[y3] = RGB2Y(R, G, B);
850
851
0
        if (x < max_x)
852
0
        {
853
          /* row 2, pixel 2 */
854
0
          color = FreeRDPReadColor(src + x4, srcFormat);
855
0
          FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
856
0
          Ra += R;
857
0
          Ga += G;
858
0
          Ba += B;
859
0
          ydst[y4] = RGB2Y(R, G, B);
860
0
        }
861
0
      }
862
863
0
      Ra >>= 2;
864
0
      Ga >>= 2;
865
0
      Ba >>= 2;
866
0
      *udst++ = RGB2U(Ra, Ga, Ba);
867
0
      *vdst++ = RGB2V(Ra, Ga, Ba);
868
0
      ydst += 2;
869
0
      src += 2 * bpp;
870
0
    }
871
0
  }
872
873
0
  return PRIMITIVES_SUCCESS;
874
0
}
875
876
static pstatus_t general_RGBToYUV420_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
877
                                               UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
878
                                               const UINT32 dstStep[3],
879
                                               const prim_size_t* WINPR_RESTRICT roi)
880
0
{
881
0
  switch (srcFormat)
882
0
  {
883
0
    case PIXEL_FORMAT_BGRA32:
884
0
    case PIXEL_FORMAT_BGRX32:
885
0
      return general_RGBToYUV420_BGRX(pSrc, srcStep, pDst, dstStep, roi);
886
887
0
    case PIXEL_FORMAT_RGBA32:
888
0
    case PIXEL_FORMAT_RGBX32:
889
0
      return general_RGBToYUV420_RGBX(pSrc, srcStep, pDst, dstStep, roi);
890
891
0
    default:
892
0
      return general_RGBToYUV420_ANY(pSrc, srcFormat, srcStep, pDst, dstStep, roi);
893
0
  }
894
0
}
895
896
static INLINE void general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(
897
    const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
898
    BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
899
    BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
900
    BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
901
0
{
902
0
  for (UINT32 x = 0; x < width; x += 2)
903
0
  {
904
0
    const BOOL lastX = (x + 1) >= width;
905
0
    BYTE Y1e, Y2e, U1e, V1e, U2e, V2e;
906
0
    BYTE Y1o, Y2o, U1o, V1o, U2o, V2o;
907
    /* Read 4 pixels, 2 from even, 2 from odd lines */
908
0
    {
909
0
      const BYTE b = *srcEven++;
910
0
      const BYTE g = *srcEven++;
911
0
      const BYTE r = *srcEven++;
912
0
      srcEven++;
913
0
      Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
914
0
      U1e = U2e = U1o = U2o = RGB2U(r, g, b);
915
0
      V1e = V2e = V1o = V2o = RGB2V(r, g, b);
916
0
    }
917
918
0
    if (!lastX)
919
0
    {
920
0
      const BYTE b = *srcEven++;
921
0
      const BYTE g = *srcEven++;
922
0
      const BYTE r = *srcEven++;
923
0
      srcEven++;
924
0
      Y2e = RGB2Y(r, g, b);
925
0
      U2e = RGB2U(r, g, b);
926
0
      V2e = RGB2V(r, g, b);
927
0
    }
928
929
0
    if (b1Odd)
930
0
    {
931
0
      const BYTE b = *srcOdd++;
932
0
      const BYTE g = *srcOdd++;
933
0
      const BYTE r = *srcOdd++;
934
0
      srcOdd++;
935
0
      Y1o = Y2o = RGB2Y(r, g, b);
936
0
      U1o = U2o = RGB2U(r, g, b);
937
0
      V1o = V2o = RGB2V(r, g, b);
938
0
    }
939
940
0
    if (b1Odd && !lastX)
941
0
    {
942
0
      const BYTE b = *srcOdd++;
943
0
      const BYTE g = *srcOdd++;
944
0
      const BYTE r = *srcOdd++;
945
0
      srcOdd++;
946
0
      Y2o = RGB2Y(r, g, b);
947
0
      U2o = RGB2U(r, g, b);
948
0
      V2o = RGB2V(r, g, b);
949
0
    }
950
951
    /* We have 4 Y pixels, so store them. */
952
0
    *b1Even++ = Y1e;
953
0
    *b1Even++ = Y2e;
954
955
0
    if (b1Odd)
956
0
    {
957
0
      *b1Odd++ = Y1o;
958
0
      *b1Odd++ = Y2o;
959
0
    }
960
961
    /* 2x 2y pixel in luma UV plane use averaging
962
     */
963
0
    {
964
0
      const BYTE Uavg = ((UINT16)U1e + (UINT16)U2e + (UINT16)U1o + (UINT16)U2o) / 4;
965
0
      const BYTE Vavg = ((UINT16)V1e + (UINT16)V2e + (UINT16)V1o + (UINT16)V2o) / 4;
966
0
      *b2++ = Uavg;
967
0
      *b3++ = Vavg;
968
0
    }
969
970
    /* UV from 2x, 2y+1 */
971
0
    if (b1Odd)
972
0
    {
973
0
      *b4++ = U1o;
974
0
      *b5++ = V1o;
975
976
0
      if (!lastX)
977
0
      {
978
0
        *b4++ = U2o;
979
0
        *b5++ = V2o;
980
0
      }
981
0
    }
982
983
    /* UV from 2x+1, 2y */
984
0
    if (!lastX)
985
0
    {
986
0
      *b6++ = U2e;
987
0
      *b7++ = V2e;
988
0
    }
989
0
  }
990
0
}
991
992
static INLINE pstatus_t general_RGBToAVC444YUV_BGRX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
993
                                                    BYTE* WINPR_RESTRICT pDst1[3],
994
                                                    const UINT32 dst1Step[3],
995
                                                    BYTE* WINPR_RESTRICT pDst2[3],
996
                                                    const UINT32 dst2Step[3],
997
                                                    const prim_size_t* WINPR_RESTRICT roi)
998
0
{
999
  /**
1000
   * Note:
1001
   * Read information in function general_RGBToAVC444YUV_ANY below !
1002
   */
1003
0
  UINT32 y;
1004
0
  const BYTE* pMaxSrc = pSrc + (roi->height - 1) * srcStep;
1005
1006
0
  for (y = 0; y < roi->height; y += 2)
1007
0
  {
1008
0
    const BOOL last = (y >= (roi->height - 1));
1009
0
    const BYTE* srcEven = y < roi->height ? pSrc + y * srcStep : pMaxSrc;
1010
0
    const BYTE* srcOdd = !last ? pSrc + (y + 1) * srcStep : pMaxSrc;
1011
0
    const UINT32 i = y >> 1;
1012
0
    const UINT32 n = (i & ~7) + i;
1013
0
    BYTE* b1Even = pDst1[0] + y * dst1Step[0];
1014
0
    BYTE* b1Odd = !last ? (b1Even + dst1Step[0]) : NULL;
1015
0
    BYTE* b2 = pDst1[1] + (y / 2) * dst1Step[1];
1016
0
    BYTE* b3 = pDst1[2] + (y / 2) * dst1Step[2];
1017
0
    BYTE* b4 = pDst2[0] + dst2Step[0] * n;
1018
0
    BYTE* b5 = b4 + 8 * dst2Step[0];
1019
0
    BYTE* b6 = pDst2[1] + (y / 2) * dst2Step[1];
1020
0
    BYTE* b7 = pDst2[2] + (y / 2) * dst2Step[2];
1021
0
    general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(srcEven, srcOdd, b1Even, b1Odd, b2, b3, b4, b5, b6,
1022
0
                                           b7, roi->width);
1023
0
  }
1024
1025
0
  return PRIMITIVES_SUCCESS;
1026
0
}
1027
1028
static INLINE void general_RGBToAVC444YUV_RGBX_DOUBLE_ROW(
1029
    const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
1030
    BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
1031
    BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1032
    BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
1033
0
{
1034
0
  for (UINT32 x = 0; x < width; x += 2)
1035
0
  {
1036
0
    const BOOL lastX = (x + 1) >= width;
1037
0
    BYTE Y1e, Y2e, U1e, V1e, U2e, V2e;
1038
0
    BYTE Y1o, Y2o, U1o, V1o, U2o, V2o;
1039
    /* Read 4 pixels, 2 from even, 2 from odd lines */
1040
0
    {
1041
0
      const BYTE r = *srcEven++;
1042
0
      const BYTE g = *srcEven++;
1043
0
      const BYTE b = *srcEven++;
1044
0
      srcEven++;
1045
0
      Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
1046
0
      U1e = U2e = U1o = U2o = RGB2U(r, g, b);
1047
0
      V1e = V2e = V1o = V2o = RGB2V(r, g, b);
1048
0
    }
1049
1050
0
    if (!lastX)
1051
0
    {
1052
0
      const BYTE r = *srcEven++;
1053
0
      const BYTE g = *srcEven++;
1054
0
      const BYTE b = *srcEven++;
1055
0
      srcEven++;
1056
0
      Y2e = RGB2Y(r, g, b);
1057
0
      U2e = RGB2U(r, g, b);
1058
0
      V2e = RGB2V(r, g, b);
1059
0
    }
1060
1061
0
    if (b1Odd)
1062
0
    {
1063
0
      const BYTE r = *srcOdd++;
1064
0
      const BYTE g = *srcOdd++;
1065
0
      const BYTE b = *srcOdd++;
1066
0
      srcOdd++;
1067
0
      Y1o = Y2o = RGB2Y(r, g, b);
1068
0
      U1o = U2o = RGB2U(r, g, b);
1069
0
      V1o = V2o = RGB2V(r, g, b);
1070
0
    }
1071
1072
0
    if (b1Odd && !lastX)
1073
0
    {
1074
0
      const BYTE r = *srcOdd++;
1075
0
      const BYTE g = *srcOdd++;
1076
0
      const BYTE b = *srcOdd++;
1077
0
      srcOdd++;
1078
0
      Y2o = RGB2Y(r, g, b);
1079
0
      U2o = RGB2U(r, g, b);
1080
0
      V2o = RGB2V(r, g, b);
1081
0
    }
1082
1083
    /* We have 4 Y pixels, so store them. */
1084
0
    *b1Even++ = Y1e;
1085
0
    *b1Even++ = Y2e;
1086
1087
0
    if (b1Odd)
1088
0
    {
1089
0
      *b1Odd++ = Y1o;
1090
0
      *b1Odd++ = Y2o;
1091
0
    }
1092
1093
    /* 2x 2y pixel in luma UV plane use averaging
1094
     */
1095
0
    {
1096
0
      const BYTE Uavg = ((UINT16)U1e + (UINT16)U2e + (UINT16)U1o + (UINT16)U2o) / 4;
1097
0
      const BYTE Vavg = ((UINT16)V1e + (UINT16)V2e + (UINT16)V1o + (UINT16)V2o) / 4;
1098
0
      *b2++ = Uavg;
1099
0
      *b3++ = Vavg;
1100
0
    }
1101
1102
    /* UV from 2x, 2y+1 */
1103
0
    if (b1Odd)
1104
0
    {
1105
0
      *b4++ = U1o;
1106
0
      *b5++ = V1o;
1107
1108
0
      if (!lastX)
1109
0
      {
1110
0
        *b4++ = U2o;
1111
0
        *b5++ = V2o;
1112
0
      }
1113
0
    }
1114
1115
    /* UV from 2x+1, 2y */
1116
0
    if (!lastX)
1117
0
    {
1118
0
      *b6++ = U2e;
1119
0
      *b7++ = V2e;
1120
0
    }
1121
0
  }
1122
0
}
1123
1124
static INLINE pstatus_t general_RGBToAVC444YUV_RGBX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
1125
                                                    BYTE* WINPR_RESTRICT pDst1[3],
1126
                                                    const UINT32 dst1Step[3],
1127
                                                    BYTE* WINPR_RESTRICT pDst2[3],
1128
                                                    const UINT32 dst2Step[3],
1129
                                                    const prim_size_t* WINPR_RESTRICT roi)
1130
0
{
1131
  /**
1132
   * Note:
1133
   * Read information in function general_RGBToAVC444YUV_ANY below !
1134
   */
1135
0
  const BYTE* pMaxSrc = pSrc + (roi->height - 1) * srcStep;
1136
1137
0
  for (UINT32 y = 0; y < roi->height; y += 2)
1138
0
  {
1139
0
    const BOOL last = (y >= (roi->height - 1));
1140
0
    const BYTE* srcEven = y < roi->height ? pSrc + y * srcStep : pMaxSrc;
1141
0
    const BYTE* srcOdd = !last ? pSrc + (y + 1) * srcStep : pMaxSrc;
1142
0
    const UINT32 i = y >> 1;
1143
0
    const UINT32 n = (i & ~7) + i;
1144
0
    BYTE* b1Even = pDst1[0] + y * dst1Step[0];
1145
0
    BYTE* b1Odd = !last ? (b1Even + dst1Step[0]) : NULL;
1146
0
    BYTE* b2 = pDst1[1] + (y / 2) * dst1Step[1];
1147
0
    BYTE* b3 = pDst1[2] + (y / 2) * dst1Step[2];
1148
0
    BYTE* b4 = pDst2[0] + dst2Step[0] * n;
1149
0
    BYTE* b5 = b4 + 8 * dst2Step[0];
1150
0
    BYTE* b6 = pDst2[1] + (y / 2) * dst2Step[1];
1151
0
    BYTE* b7 = pDst2[2] + (y / 2) * dst2Step[2];
1152
0
    general_RGBToAVC444YUV_RGBX_DOUBLE_ROW(srcEven, srcOdd, b1Even, b1Odd, b2, b3, b4, b5, b6,
1153
0
                                           b7, roi->width);
1154
0
  }
1155
1156
0
  return PRIMITIVES_SUCCESS;
1157
0
}
1158
1159
static INLINE void general_RGBToAVC444YUV_ANY_DOUBLE_ROW(
1160
    const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd, UINT32 srcFormat,
1161
    BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
1162
    BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1163
    BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
1164
0
{
1165
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
1166
0
  for (UINT32 x = 0; x < width; x += 2)
1167
0
  {
1168
0
    const BOOL lastX = (x + 1) >= width;
1169
0
    BYTE Y1e, Y2e, U1e, V1e, U2e, V2e;
1170
0
    BYTE Y1o, Y2o, U1o, V1o, U2o, V2o;
1171
    /* Read 4 pixels, 2 from even, 2 from odd lines */
1172
0
    {
1173
0
      BYTE r, g, b;
1174
0
      const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1175
0
      srcEven += bpp;
1176
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1177
0
      Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
1178
0
      U1e = U2e = U1o = U2o = RGB2U(r, g, b);
1179
0
      V1e = V2e = V1o = V2o = RGB2V(r, g, b);
1180
0
    }
1181
1182
0
    if (!lastX)
1183
0
    {
1184
0
      BYTE r, g, b;
1185
0
      const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1186
0
      srcEven += bpp;
1187
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1188
0
      Y2e = RGB2Y(r, g, b);
1189
0
      U2e = RGB2U(r, g, b);
1190
0
      V2e = RGB2V(r, g, b);
1191
0
    }
1192
1193
0
    if (b1Odd)
1194
0
    {
1195
0
      BYTE r, g, b;
1196
0
      const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1197
0
      srcOdd += bpp;
1198
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1199
0
      Y1o = Y2o = RGB2Y(r, g, b);
1200
0
      U1o = U2o = RGB2U(r, g, b);
1201
0
      V1o = V2o = RGB2V(r, g, b);
1202
0
    }
1203
1204
0
    if (b1Odd && !lastX)
1205
0
    {
1206
0
      BYTE r, g, b;
1207
0
      const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1208
0
      srcOdd += bpp;
1209
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1210
0
      Y2o = RGB2Y(r, g, b);
1211
0
      U2o = RGB2U(r, g, b);
1212
0
      V2o = RGB2V(r, g, b);
1213
0
    }
1214
1215
    /* We have 4 Y pixels, so store them. */
1216
0
    *b1Even++ = Y1e;
1217
0
    *b1Even++ = Y2e;
1218
1219
0
    if (b1Odd)
1220
0
    {
1221
0
      *b1Odd++ = Y1o;
1222
0
      *b1Odd++ = Y2o;
1223
0
    }
1224
1225
    /* 2x 2y pixel in luma UV plane use averaging
1226
     */
1227
0
    {
1228
0
      const BYTE Uavg = ((UINT16)U1e + (UINT16)U2e + (UINT16)U1o + (UINT16)U2o) / 4;
1229
0
      const BYTE Vavg = ((UINT16)V1e + (UINT16)V2e + (UINT16)V1o + (UINT16)V2o) / 4;
1230
0
      *b2++ = Uavg;
1231
0
      *b3++ = Vavg;
1232
0
    }
1233
1234
    /* UV from 2x, 2y+1 */
1235
0
    if (b1Odd)
1236
0
    {
1237
0
      *b4++ = U1o;
1238
0
      *b5++ = V1o;
1239
1240
0
      if (!lastX)
1241
0
      {
1242
0
        *b4++ = U2o;
1243
0
        *b5++ = V2o;
1244
0
      }
1245
0
    }
1246
1247
    /* UV from 2x+1, 2y */
1248
0
    if (!lastX)
1249
0
    {
1250
0
      *b6++ = U2e;
1251
0
      *b7++ = V2e;
1252
0
    }
1253
0
  }
1254
0
}
1255
1256
static INLINE pstatus_t general_RGBToAVC444YUV_ANY(
1257
    const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat, UINT32 srcStep,
1258
    BYTE* WINPR_RESTRICT pDst1[3], const UINT32 dst1Step[3], BYTE* WINPR_RESTRICT pDst2[3],
1259
    const UINT32 dst2Step[3], const prim_size_t* WINPR_RESTRICT roi)
1260
0
{
1261
  /**
1262
   * Note: According to [MS-RDPEGFX 2.2.4.4 RFX_AVC420_BITMAP_STREAM] the
1263
   * width and height of the MPEG-4 AVC/H.264 codec bitstream MUST be aligned
1264
   * to a multiple of 16.
1265
   * Hence the passed destination YUV420/CHROMA420 buffers must have been
1266
   * allocated accordingly !!
1267
   */
1268
  /**
1269
   * [MS-RDPEGFX 3.3.8.3.2 YUV420p Stream Combination] defines the following "Bx areas":
1270
   *
1271
   * YUV420 frame (main view):
1272
   * B1:  From Y444 all pixels
1273
   * B2:  From U444 all pixels in even rows with even columns
1274
   * B3:  From V444 all pixels in even rows with even columns
1275
   *
1276
   * Chroma420 frame (auxillary view):
1277
   * B45: From U444 and V444 all pixels from all odd rows
1278
   *      (The odd U444 and V444 rows must be interleaved in 8-line blocks in B45 !!!)
1279
   * B6:  From U444 all pixels in even rows with odd columns
1280
   * B7:  From V444 all pixels in even rows with odd columns
1281
   *
1282
   * Microsoft's horrible unclear description in MS-RDPEGFX translated to pseudo code looks like
1283
   * this:
1284
   *
1285
   * for (y = 0; y < fullHeight; y++)
1286
   * {
1287
   *     for (x = 0; x < fullWidth; x++)
1288
   *     {
1289
   *         B1[x,y] = Y444[x,y];
1290
   *     }
1291
   *  }
1292
   *
1293
   * for (y = 0; y < halfHeight; y++)
1294
   * {
1295
   *     for (x = 0; x < halfWidth; x++)
1296
   *     {
1297
   *         B2[x,y] = U444[2 * x,     2 * y];
1298
   *         B3[x,y] = V444[2 * x,     2 * y];
1299
   *         B6[x,y] = U444[2 * x + 1, 2 * y];
1300
   *         B7[x,y] = V444[2 * x + 1, 2 * y];
1301
   *     }
1302
   *  }
1303
   *
1304
   * for (y = 0; y < halfHeight; y++)
1305
   * {
1306
   *     yU  = (y / 8) * 16;   // identify first row of correct 8-line U block in B45
1307
   *     yU += (y % 8);        // add offset rows in destination block
1308
   *     yV  = yU + 8;         // the corresponding v line is always 8 rows ahead
1309
   *
1310
   *     for (x = 0; x < fullWidth; x++)
1311
   *     {
1312
   *         B45[x,yU] = U444[x, 2 * y + 1];
1313
   *         B45[x,yV] = V444[x, 2 * y + 1];
1314
   *     }
1315
   *  }
1316
   *
1317
   */
1318
0
  const BYTE* pMaxSrc = pSrc + (roi->height - 1) * srcStep;
1319
1320
0
  for (UINT32 y = 0; y < roi->height; y += 2)
1321
0
  {
1322
0
    const BOOL last = (y >= (roi->height - 1));
1323
0
    const BYTE* srcEven = y < roi->height ? pSrc + y * srcStep : pMaxSrc;
1324
0
    const BYTE* srcOdd = !last ? pSrc + (y + 1) * srcStep : pMaxSrc;
1325
0
    const UINT32 i = y >> 1;
1326
0
    const UINT32 n = (i & ~7) + i;
1327
0
    BYTE* b1Even = pDst1[0] + y * dst1Step[0];
1328
0
    BYTE* b1Odd = !last ? (b1Even + dst1Step[0]) : NULL;
1329
0
    BYTE* b2 = pDst1[1] + (y / 2) * dst1Step[1];
1330
0
    BYTE* b3 = pDst1[2] + (y / 2) * dst1Step[2];
1331
0
    BYTE* b4 = pDst2[0] + dst2Step[0] * n;
1332
0
    BYTE* b5 = b4 + 8 * dst2Step[0];
1333
0
    BYTE* b6 = pDst2[1] + (y / 2) * dst2Step[1];
1334
0
    BYTE* b7 = pDst2[2] + (y / 2) * dst2Step[2];
1335
0
    general_RGBToAVC444YUV_ANY_DOUBLE_ROW(srcEven, srcOdd, srcFormat, b1Even, b1Odd, b2, b3, b4,
1336
0
                                          b5, b6, b7, roi->width);
1337
0
  }
1338
1339
0
  return PRIMITIVES_SUCCESS;
1340
0
}
1341
1342
static INLINE pstatus_t general_RGBToAVC444YUV(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
1343
                                               UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
1344
                                               const UINT32 dst1Step[3],
1345
                                               BYTE* WINPR_RESTRICT pDst2[3],
1346
                                               const UINT32 dst2Step[3],
1347
                                               const prim_size_t* WINPR_RESTRICT roi)
1348
0
{
1349
0
  if (!pSrc || !pDst1 || !dst1Step || !pDst2 || !dst2Step)
1350
0
    return -1;
1351
1352
0
  if (!pDst1[0] || !pDst1[1] || !pDst1[2])
1353
0
    return -1;
1354
1355
0
  if (!dst1Step[0] || !dst1Step[1] || !dst1Step[2])
1356
0
    return -1;
1357
1358
0
  if (!pDst2[0] || !pDst2[1] || !pDst2[2])
1359
0
    return -1;
1360
1361
0
  if (!dst2Step[0] || !dst2Step[1] || !dst2Step[2])
1362
0
    return -1;
1363
1364
0
  switch (srcFormat)
1365
0
  {
1366
0
    case PIXEL_FORMAT_BGRA32:
1367
0
    case PIXEL_FORMAT_BGRX32:
1368
0
      return general_RGBToAVC444YUV_BGRX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
1369
0
                                         roi);
1370
1371
0
    case PIXEL_FORMAT_RGBA32:
1372
0
    case PIXEL_FORMAT_RGBX32:
1373
0
      return general_RGBToAVC444YUV_RGBX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
1374
0
                                         roi);
1375
1376
0
    default:
1377
0
      return general_RGBToAVC444YUV_ANY(pSrc, srcFormat, srcStep, pDst1, dst1Step, pDst2,
1378
0
                                        dst2Step, roi);
1379
0
  }
1380
1381
0
  return !PRIMITIVES_SUCCESS;
1382
0
}
1383
1384
static INLINE void general_RGBToAVC444YUVv2_ANY_DOUBLE_ROW(
1385
    const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd, UINT32 srcFormat,
1386
    BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
1387
    BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
1388
    BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
1389
    BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
1390
    BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
1391
    BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width)
1392
0
{
1393
0
  const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
1394
1395
0
  for (UINT32 x = 0; x < width; x += 2)
1396
0
  {
1397
0
    BYTE Ya, Ua, Va;
1398
0
    BYTE Yb, Ub, Vb;
1399
0
    BYTE Yc, Uc, Vc;
1400
0
    BYTE Yd, Ud, Vd;
1401
0
    {
1402
0
      BYTE b, g, r;
1403
0
      const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1404
0
      srcEven += bpp;
1405
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1406
0
      Ya = RGB2Y(r, g, b);
1407
0
      Ua = RGB2U(r, g, b);
1408
0
      Va = RGB2V(r, g, b);
1409
0
    }
1410
1411
0
    if (x < width - 1)
1412
0
    {
1413
0
      BYTE b, g, r;
1414
0
      const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1415
0
      srcEven += bpp;
1416
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1417
0
      Yb = RGB2Y(r, g, b);
1418
0
      Ub = RGB2U(r, g, b);
1419
0
      Vb = RGB2V(r, g, b);
1420
0
    }
1421
0
    else
1422
0
    {
1423
0
      Yb = Ya;
1424
0
      Ub = Ua;
1425
0
      Vb = Va;
1426
0
    }
1427
1428
0
    if (srcOdd)
1429
0
    {
1430
0
      BYTE b, g, r;
1431
0
      const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1432
0
      srcOdd += bpp;
1433
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1434
0
      Yc = RGB2Y(r, g, b);
1435
0
      Uc = RGB2U(r, g, b);
1436
0
      Vc = RGB2V(r, g, b);
1437
0
    }
1438
0
    else
1439
0
    {
1440
0
      Yc = Ya;
1441
0
      Uc = Ua;
1442
0
      Vc = Va;
1443
0
    }
1444
1445
0
    if (srcOdd && (x < width - 1))
1446
0
    {
1447
0
      BYTE b, g, r;
1448
0
      const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1449
0
      srcOdd += bpp;
1450
0
      FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1451
0
      Yd = RGB2Y(r, g, b);
1452
0
      Ud = RGB2U(r, g, b);
1453
0
      Vd = RGB2V(r, g, b);
1454
0
    }
1455
0
    else
1456
0
    {
1457
0
      Yd = Ya;
1458
0
      Ud = Ua;
1459
0
      Vd = Va;
1460
0
    }
1461
1462
    /* Y [b1] */
1463
0
    *yLumaDstEven++ = Ya;
1464
1465
0
    if (x < width - 1)
1466
0
      *yLumaDstEven++ = Yb;
1467
1468
0
    if (srcOdd)
1469
0
      *yLumaDstOdd++ = Yc;
1470
1471
0
    if (srcOdd && (x < width - 1))
1472
0
      *yLumaDstOdd++ = Yd;
1473
1474
    /* 2x 2y [b2,b3] */
1475
0
    *uLumaDst++ = (Ua + Ub + Uc + Ud) / 4;
1476
0
    *vLumaDst++ = (Va + Vb + Vc + Vd) / 4;
1477
1478
    /* 2x+1, y [b4,b5] even */
1479
0
    if (x < width - 1)
1480
0
    {
1481
0
      *yEvenChromaDst1++ = Ub;
1482
0
      *yEvenChromaDst2++ = Vb;
1483
0
    }
1484
1485
0
    if (srcOdd)
1486
0
    {
1487
      /* 2x+1, y [b4,b5] odd */
1488
0
      if (x < width - 1)
1489
0
      {
1490
0
        *yOddChromaDst1++ = Ud;
1491
0
        *yOddChromaDst2++ = Vd;
1492
0
      }
1493
1494
      /* 4x 2y+1 [b6, b7] */
1495
0
      if (x % 4 == 0)
1496
0
      {
1497
0
        *uChromaDst1++ = Uc;
1498
0
        *uChromaDst2++ = Vc;
1499
0
      }
1500
      /* 4x+2 2y+1 [b8, b9] */
1501
0
      else
1502
0
      {
1503
0
        *vChromaDst1++ = Uc;
1504
0
        *vChromaDst2++ = Vc;
1505
0
      }
1506
0
    }
1507
0
  }
1508
0
}
1509
1510
static INLINE pstatus_t general_RGBToAVC444YUVv2_ANY(
1511
    const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat, UINT32 srcStep,
1512
    BYTE* WINPR_RESTRICT pDst1[3], const UINT32 dst1Step[3], BYTE* WINPR_RESTRICT pDst2[3],
1513
    const UINT32 dst2Step[3], const prim_size_t* WINPR_RESTRICT roi)
1514
0
{
1515
  /**
1516
   * Note: According to [MS-RDPEGFX 2.2.4.4 RFX_AVC420_BITMAP_STREAM] the
1517
   * width and height of the MPEG-4 AVC/H.264 codec bitstream MUST be aligned
1518
   * to a multiple of 16.
1519
   * Hence the passed destination YUV420/CHROMA420 buffers must have been
1520
   * allocated accordingly !!
1521
   */
1522
  /**
1523
   * [MS-RDPEGFX 3.3.8.3.3 YUV420p Stream Combination for YUV444v2 mode] defines the following "Bx
1524
   * areas":
1525
   *
1526
   * YUV420 frame (main view):
1527
   * B1:  From Y444 all pixels
1528
   * B2:  From U444 all pixels in even rows with even rows and columns
1529
   * B3:  From V444 all pixels in even rows with even rows and columns
1530
   *
1531
   * Chroma420 frame (auxillary view):
1532
   * B45: From U444 and V444 all pixels from all odd columns
1533
   * B67: From U444 and V444 every 4th pixel in odd rows
1534
   * B89:  From U444 and V444 every 4th pixel (initial offset of 2) in odd rows
1535
   *
1536
   * Chroma Bxy areas correspond to the left and right half of the YUV420 plane.
1537
   * for (y = 0; y < fullHeight; y++)
1538
   * {
1539
   *     for (x = 0; x < fullWidth; x++)
1540
   *     {
1541
   *         B1[x,y] = Y444[x,y];
1542
   *     }
1543
   *
1544
   *     for (x = 0; x < halfWidth; x++)
1545
   *     {
1546
   *         B4[x,y] = U444[2 * x, 2 * y];
1547
   *         B5[x,y] = V444[2 * x, 2 * y];
1548
   *     }
1549
   *  }
1550
   *
1551
   * for (y = 0; y < halfHeight; y++)
1552
   * {
1553
   *     for (x = 0; x < halfWidth; x++)
1554
   *     {
1555
   *         B2[x,y] = U444[2 * x,     2 * y];
1556
   *         B3[x,y] = V444[2 * x,     2 * y];
1557
   *         B6[x,y] = U444[4 * x,     2 * y + 1];
1558
   *         B7[x,y] = V444[4 * x,     2 * y + 1];
1559
   *         B8[x,y] = V444[4 * x + 2, 2 * y + 1];
1560
   *         B9[x,y] = V444[4 * x + 2, 2 * y] + 1;
1561
   *     }
1562
   *  }
1563
   *
1564
   */
1565
0
  if (roi->height < 1 || roi->width < 1)
1566
0
    return !PRIMITIVES_SUCCESS;
1567
1568
0
  for (UINT32 y = 0; y < roi->height; y += 2)
1569
0
  {
1570
0
    const BYTE* srcEven = (pSrc + y * srcStep);
1571
0
    const BYTE* srcOdd = (y < roi->height - 1) ? (srcEven + srcStep) : NULL;
1572
0
    BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
1573
0
    BYTE* dstLumaYOdd = (dstLumaYEven + dst1Step[0]);
1574
0
    BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
1575
0
    BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
1576
0
    BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
1577
0
    BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
1578
0
    BYTE* dstOddChromaY1 = dstEvenChromaY1 + dst2Step[0];
1579
0
    BYTE* dstOddChromaY2 = dstEvenChromaY2 + dst2Step[0];
1580
0
    BYTE* dstChromaU1 = (pDst2[1] + (y / 2) * dst2Step[1]);
1581
0
    BYTE* dstChromaV1 = (pDst2[2] + (y / 2) * dst2Step[2]);
1582
0
    BYTE* dstChromaU2 = dstChromaU1 + roi->width / 4;
1583
0
    BYTE* dstChromaV2 = dstChromaV1 + roi->width / 4;
1584
0
    general_RGBToAVC444YUVv2_ANY_DOUBLE_ROW(
1585
0
        srcEven, srcOdd, srcFormat, dstLumaYEven, dstLumaYOdd, dstLumaU, dstLumaV,
1586
0
        dstEvenChromaY1, dstEvenChromaY2, dstOddChromaY1, dstOddChromaY2, dstChromaU1,
1587
0
        dstChromaU2, dstChromaV1, dstChromaV2, roi->width);
1588
0
  }
1589
1590
0
  return PRIMITIVES_SUCCESS;
1591
0
}
1592
1593
static INLINE void general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
1594
    const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
1595
    BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
1596
    BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
1597
    BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
1598
    BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
1599
    BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
1600
    BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width)
1601
0
{
1602
0
  for (UINT32 x = 0; x < width; x += 2)
1603
0
  {
1604
0
    BYTE Ya, Ua, Va;
1605
0
    BYTE Yb, Ub, Vb;
1606
0
    BYTE Yc, Uc, Vc;
1607
0
    BYTE Yd, Ud, Vd;
1608
0
    {
1609
0
      const BYTE b = *srcEven++;
1610
0
      const BYTE g = *srcEven++;
1611
0
      const BYTE r = *srcEven++;
1612
0
      srcEven++;
1613
0
      Ya = RGB2Y(r, g, b);
1614
0
      Ua = RGB2U(r, g, b);
1615
0
      Va = RGB2V(r, g, b);
1616
0
    }
1617
1618
0
    if (x < width - 1)
1619
0
    {
1620
0
      const BYTE b = *srcEven++;
1621
0
      const BYTE g = *srcEven++;
1622
0
      const BYTE r = *srcEven++;
1623
0
      srcEven++;
1624
0
      Yb = RGB2Y(r, g, b);
1625
0
      Ub = RGB2U(r, g, b);
1626
0
      Vb = RGB2V(r, g, b);
1627
0
    }
1628
0
    else
1629
0
    {
1630
0
      Yb = Ya;
1631
0
      Ub = Ua;
1632
0
      Vb = Va;
1633
0
    }
1634
1635
0
    if (srcOdd)
1636
0
    {
1637
0
      const BYTE b = *srcOdd++;
1638
0
      const BYTE g = *srcOdd++;
1639
0
      const BYTE r = *srcOdd++;
1640
0
      srcOdd++;
1641
0
      Yc = RGB2Y(r, g, b);
1642
0
      Uc = RGB2U(r, g, b);
1643
0
      Vc = RGB2V(r, g, b);
1644
0
    }
1645
0
    else
1646
0
    {
1647
0
      Yc = Ya;
1648
0
      Uc = Ua;
1649
0
      Vc = Va;
1650
0
    }
1651
1652
0
    if (srcOdd && (x < width - 1))
1653
0
    {
1654
0
      const BYTE b = *srcOdd++;
1655
0
      const BYTE g = *srcOdd++;
1656
0
      const BYTE r = *srcOdd++;
1657
0
      srcOdd++;
1658
0
      Yd = RGB2Y(r, g, b);
1659
0
      Ud = RGB2U(r, g, b);
1660
0
      Vd = RGB2V(r, g, b);
1661
0
    }
1662
0
    else
1663
0
    {
1664
0
      Yd = Ya;
1665
0
      Ud = Ua;
1666
0
      Vd = Va;
1667
0
    }
1668
1669
    /* Y [b1] */
1670
0
    *yLumaDstEven++ = Ya;
1671
1672
0
    if (x < width - 1)
1673
0
      *yLumaDstEven++ = Yb;
1674
1675
0
    if (srcOdd)
1676
0
      *yLumaDstOdd++ = Yc;
1677
1678
0
    if (srcOdd && (x < width - 1))
1679
0
      *yLumaDstOdd++ = Yd;
1680
1681
    /* 2x 2y [b2,b3] */
1682
0
    *uLumaDst++ = (Ua + Ub + Uc + Ud) / 4;
1683
0
    *vLumaDst++ = (Va + Vb + Vc + Vd) / 4;
1684
1685
    /* 2x+1, y [b4,b5] even */
1686
0
    if (x < width - 1)
1687
0
    {
1688
0
      *yEvenChromaDst1++ = Ub;
1689
0
      *yEvenChromaDst2++ = Vb;
1690
0
    }
1691
1692
0
    if (srcOdd)
1693
0
    {
1694
      /* 2x+1, y [b4,b5] odd */
1695
0
      if (x < width - 1)
1696
0
      {
1697
0
        *yOddChromaDst1++ = Ud;
1698
0
        *yOddChromaDst2++ = Vd;
1699
0
      }
1700
1701
      /* 4x 2y+1 [b6, b7] */
1702
0
      if (x % 4 == 0)
1703
0
      {
1704
0
        *uChromaDst1++ = Uc;
1705
0
        *uChromaDst2++ = Vc;
1706
0
      }
1707
      /* 4x+2 2y+1 [b8, b9] */
1708
0
      else
1709
0
      {
1710
0
        *vChromaDst1++ = Uc;
1711
0
        *vChromaDst2++ = Vc;
1712
0
      }
1713
0
    }
1714
0
  }
1715
0
}
1716
1717
static INLINE pstatus_t general_RGBToAVC444YUVv2_BGRX(const BYTE* WINPR_RESTRICT pSrc,
1718
                                                      UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
1719
                                                      const UINT32 dst1Step[3],
1720
                                                      BYTE* WINPR_RESTRICT pDst2[3],
1721
                                                      const UINT32 dst2Step[3],
1722
                                                      const prim_size_t* WINPR_RESTRICT roi)
1723
0
{
1724
0
  if (roi->height < 1 || roi->width < 1)
1725
0
    return !PRIMITIVES_SUCCESS;
1726
1727
0
  for (UINT32 y = 0; y < roi->height; y += 2)
1728
0
  {
1729
0
    const BYTE* srcEven = (pSrc + y * srcStep);
1730
0
    const BYTE* srcOdd = (y < roi->height - 1) ? (srcEven + srcStep) : NULL;
1731
0
    BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
1732
0
    BYTE* dstLumaYOdd = (dstLumaYEven + dst1Step[0]);
1733
0
    BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
1734
0
    BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
1735
0
    BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
1736
0
    BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
1737
0
    BYTE* dstOddChromaY1 = dstEvenChromaY1 + dst2Step[0];
1738
0
    BYTE* dstOddChromaY2 = dstEvenChromaY2 + dst2Step[0];
1739
0
    BYTE* dstChromaU1 = (pDst2[1] + (y / 2) * dst2Step[1]);
1740
0
    BYTE* dstChromaV1 = (pDst2[2] + (y / 2) * dst2Step[2]);
1741
0
    BYTE* dstChromaU2 = dstChromaU1 + roi->width / 4;
1742
0
    BYTE* dstChromaV2 = dstChromaV1 + roi->width / 4;
1743
0
    general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
1744
0
        srcEven, srcOdd, dstLumaYEven, dstLumaYOdd, dstLumaU, dstLumaV, dstEvenChromaY1,
1745
0
        dstEvenChromaY2, dstOddChromaY1, dstOddChromaY2, dstChromaU1, dstChromaU2, dstChromaV1,
1746
0
        dstChromaV2, roi->width);
1747
0
  }
1748
1749
0
  return PRIMITIVES_SUCCESS;
1750
0
}
1751
1752
static INLINE pstatus_t general_RGBToAVC444YUVv2(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
1753
                                                 UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
1754
                                                 const UINT32 dst1Step[3],
1755
                                                 BYTE* WINPR_RESTRICT pDst2[3],
1756
                                                 const UINT32 dst2Step[3],
1757
                                                 const prim_size_t* WINPR_RESTRICT roi)
1758
0
{
1759
0
  switch (srcFormat)
1760
0
  {
1761
0
    case PIXEL_FORMAT_BGRA32:
1762
0
    case PIXEL_FORMAT_BGRX32:
1763
0
      return general_RGBToAVC444YUVv2_BGRX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
1764
0
                                           roi);
1765
1766
0
    default:
1767
0
      return general_RGBToAVC444YUVv2_ANY(pSrc, srcFormat, srcStep, pDst1, dst1Step, pDst2,
1768
0
                                          dst2Step, roi);
1769
0
  }
1770
1771
0
  return !PRIMITIVES_SUCCESS;
1772
0
}
1773
1774
void primitives_init_YUV(primitives_t* WINPR_RESTRICT prims)
1775
0
{
1776
0
  prims->YUV420ToRGB_8u_P3AC4R = general_YUV420ToRGB_8u_P3AC4R;
1777
0
  prims->YUV444ToRGB_8u_P3AC4R = general_YUV444ToRGB_8u_P3AC4R;
1778
0
  prims->RGBToYUV420_8u_P3AC4R = general_RGBToYUV420_8u_P3AC4R;
1779
0
  prims->RGBToYUV444_8u_P3AC4R = general_RGBToYUV444_8u_P3AC4R;
1780
0
  prims->YUV420CombineToYUV444 = general_YUV420CombineToYUV444;
1781
0
  prims->YUV444SplitToYUV420 = general_YUV444SplitToYUV420;
1782
0
  prims->RGBToAVC444YUV = general_RGBToAVC444YUV;
1783
0
  prims->RGBToAVC444YUVv2 = general_RGBToAVC444YUVv2;
1784
0
}