Coverage Report

Created: 2026-11-10 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/yuv.c
Line
Count
Source
1
#include <winpr/sysinfo.h>
2
#include <winpr/assert.h>
3
#include <winpr/cast.h>
4
#include <winpr/pool.h>
5
6
#include <freerdp/settings.h>
7
#include <freerdp/codec/region.h>
8
#include <freerdp/primitives.h>
9
#include <freerdp/log.h>
10
#include <freerdp/codec/yuv.h>
11
12
#define TAG FREERDP_TAG("codec")
13
14
#if !defined(YUV_TILE_SIZE)
15
#error "YUV_TILE_SIZE must be defined to the size of a single YUV decoder block"
16
#endif
17
18
typedef struct
19
{
20
  YUV_CONTEXT* context;
21
  const BYTE* pYUVData[3];
22
  UINT32 iStride[3];
23
  DWORD DstFormat;
24
  BYTE* dest;
25
  UINT32 nDstStep;
26
  RECTANGLE_16 rect;
27
} YUV_PROCESS_WORK_PARAM;
28
29
typedef struct
30
{
31
  YUV_CONTEXT* context;
32
  const BYTE* pYUVData[3];
33
  UINT32 iStride[3];
34
  BYTE* pYUVDstData[3];
35
  UINT32 iDstStride[3];
36
  RECTANGLE_16 rect;
37
  avc444_frame_type type;
38
} YUV_COMBINE_WORK_PARAM;
39
40
typedef struct
41
{
42
  YUV_CONTEXT* context;
43
  const BYTE* pSrcData;
44
45
  DWORD SrcFormat;
46
  UINT32 nSrcStep;
47
  RECTANGLE_16 rect;
48
  BYTE version;
49
50
  BYTE* pYUVLumaData[3];
51
  BYTE* pYUVChromaData[3];
52
  UINT32 iStride[3];
53
} YUV_ENCODE_WORK_PARAM;
54
55
struct S_YUV_CONTEXT
56
{
57
  UINT32 width, height;
58
  BOOL useThreads;
59
  BOOL encoder;
60
  UINT32 heightStep;
61
62
  UINT32 work_object_count;
63
  PTP_WORK* work_objects;
64
  YUV_ENCODE_WORK_PARAM* work_enc_params;
65
  YUV_PROCESS_WORK_PARAM* work_dec_params;
66
  YUV_COMBINE_WORK_PARAM* work_combined_params;
67
};
68
69
WINPR_ATTR_NODISCARD
70
static inline BOOL avc420_yuv_to_rgb(const BYTE* WINPR_RESTRICT pYUVData[3],
71
                                     const UINT32 iStride[3],
72
                                     const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstStep,
73
                                     BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat)
74
0
{
75
0
  primitives_t* prims = primitives_get();
76
0
  prim_size_t roi;
77
0
  const BYTE* pYUVPoint[3];
78
79
0
  WINPR_ASSERT(pYUVData);
80
0
  WINPR_ASSERT(iStride);
81
0
  WINPR_ASSERT(rect);
82
0
  WINPR_ASSERT(pDstData);
83
84
0
  const INT32 width = rect->right - rect->left;
85
0
  const INT32 height = rect->bottom - rect->top;
86
0
  BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
87
0
                    1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
88
89
0
  pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
90
0
  pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top / 2 * iStride[1] + rect->left / 2;
91
0
  pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top / 2 * iStride[2] + rect->left / 2;
92
93
0
  roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
94
0
  roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
95
96
0
  return (prims->YUV420ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
97
0
                                       &roi) == PRIMITIVES_SUCCESS);
98
0
}
99
100
WINPR_ATTR_NODISCARD
101
static inline BOOL avc444_yuv_to_rgb(const BYTE* WINPR_RESTRICT pYUVData[3],
102
                                     const UINT32 iStride[3],
103
                                     const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstStep,
104
                                     BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat)
105
0
{
106
0
  primitives_t* prims = primitives_get();
107
0
  prim_size_t roi;
108
0
  const BYTE* pYUVPoint[3];
109
110
0
  WINPR_ASSERT(pYUVData);
111
0
  WINPR_ASSERT(iStride);
112
0
  WINPR_ASSERT(rect);
113
0
  WINPR_ASSERT(pDstData);
114
115
0
  const INT32 width = rect->right - rect->left;
116
0
  const INT32 height = rect->bottom - rect->top;
117
0
  BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
118
0
                    1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
119
120
0
  pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
121
0
  pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top * iStride[1] + rect->left;
122
0
  pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top * iStride[2] + rect->left;
123
124
0
  roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
125
0
  roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
126
127
0
  return (prims->YUV444ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
128
0
                                       &roi) == PRIMITIVES_SUCCESS);
129
0
}
130
131
static void CALLBACK yuv420_process_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
132
                                                  PTP_WORK work)
133
0
{
134
0
  YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
135
0
  WINPR_UNUSED(instance);
136
0
  WINPR_UNUSED(work);
137
0
  WINPR_ASSERT(param);
138
139
0
  if (!avc420_yuv_to_rgb(param->pYUVData, param->iStride, &param->rect, param->nDstStep,
140
0
                         param->dest, param->DstFormat))
141
0
    WLog_WARN(TAG, "avc420_yuv_to_rgb failed");
142
0
}
143
144
static void CALLBACK yuv444_process_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
145
                                                  PTP_WORK work)
146
0
{
147
0
  YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
148
0
  WINPR_UNUSED(instance);
149
0
  WINPR_UNUSED(work);
150
0
  WINPR_ASSERT(param);
151
152
0
  if (!avc444_yuv_to_rgb(param->pYUVData, param->iStride, &param->rect, param->nDstStep,
153
0
                         param->dest, param->DstFormat))
154
0
    WLog_WARN(TAG, "avc444_yuv_to_rgb failed");
155
0
}
156
157
BOOL yuv_context_reset(YUV_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
158
0
{
159
0
  BOOL rc = FALSE;
160
0
  WINPR_ASSERT(context);
161
162
0
  if (width > UINT16_MAX)
163
0
  {
164
0
    WLog_ERR(TAG, "YUV::width %" PRIu32 " > %d", width, UINT16_MAX);
165
0
    return FALSE;
166
0
  }
167
168
0
  if (height > UINT16_MAX)
169
0
  {
170
0
    WLog_ERR(TAG, "YUV::height %" PRIu32 " > %d", height, UINT16_MAX);
171
0
    return FALSE;
172
0
  }
173
174
0
  context->width = width;
175
0
  context->height = height;
176
177
0
  context->heightStep = height;
178
179
0
  if (context->useThreads)
180
0
  {
181
0
    context->heightStep = 16;
182
    /* Preallocate workers for 16x16 tiles.
183
     * this is overallocation for most cases.
184
     *
185
     * ~2MB total for a 4k resolution, so negligible.
186
     */
187
0
    const size_t pw = (1ull * width + YUV_TILE_SIZE - width % YUV_TILE_SIZE) / 16;
188
0
    const size_t ph = (1ull * height + YUV_TILE_SIZE - height % YUV_TILE_SIZE) / 16;
189
190
0
    const size_t count = pw * ph;
191
192
0
    context->work_object_count = 0;
193
0
    if (context->encoder)
194
0
    {
195
0
      void* tmp = winpr_aligned_recalloc(context->work_enc_params, count,
196
0
                                         sizeof(YUV_ENCODE_WORK_PARAM), 32);
197
0
      if (!tmp)
198
0
        goto fail;
199
0
      memset(tmp, 0, count * sizeof(YUV_ENCODE_WORK_PARAM));
200
201
0
      context->work_enc_params = tmp;
202
0
    }
203
0
    else
204
0
    {
205
0
      void* tmp = winpr_aligned_recalloc(context->work_dec_params, count,
206
0
                                         sizeof(YUV_PROCESS_WORK_PARAM), 32);
207
0
      if (!tmp)
208
0
        goto fail;
209
0
      memset(tmp, 0, count * sizeof(YUV_PROCESS_WORK_PARAM));
210
211
0
      context->work_dec_params = tmp;
212
213
0
      void* ctmp = winpr_aligned_recalloc(context->work_combined_params, count,
214
0
                                          sizeof(YUV_COMBINE_WORK_PARAM), 32);
215
0
      if (!ctmp)
216
0
        goto fail;
217
0
      memset(ctmp, 0, count * sizeof(YUV_COMBINE_WORK_PARAM));
218
219
0
      context->work_combined_params = ctmp;
220
0
    }
221
222
0
    void* wtmp =
223
0
        winpr_aligned_recalloc((void*)context->work_objects, count, sizeof(PTP_WORK), 32);
224
0
    if (!wtmp)
225
0
      goto fail;
226
0
    memset(wtmp, 0, count * sizeof(PTP_WORK));
227
228
0
    context->work_objects = (PTP_WORK*)wtmp;
229
0
    context->work_object_count = WINPR_ASSERTING_INT_CAST(uint32_t, count);
230
0
  }
231
0
  rc = TRUE;
232
0
fail:
233
0
  return rc;
234
0
}
235
236
YUV_CONTEXT* yuv_context_new(BOOL encoder, UINT32 ThreadingFlags)
237
0
{
238
0
  SYSTEM_INFO sysInfos = WINPR_C_ARRAY_INIT;
239
  /** do it here to avoid a race condition between threads */
240
0
  if (!primitives_get())
241
0
    return nullptr;
242
243
0
  YUV_CONTEXT* ret = winpr_aligned_calloc(1, sizeof(*ret), 32);
244
0
  if (!ret)
245
0
    return nullptr;
246
247
0
  ret->encoder = encoder;
248
0
  if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
249
0
  {
250
0
    GetNativeSystemInfo(&sysInfos);
251
0
    ret->useThreads = (sysInfos.dwNumberOfProcessors > 1);
252
0
  }
253
254
0
  return ret;
255
0
}
256
257
void yuv_context_free(YUV_CONTEXT* context)
258
0
{
259
0
  if (!context)
260
0
    return;
261
0
  if (context->useThreads)
262
0
  {
263
0
    winpr_aligned_free((void*)context->work_objects);
264
0
    winpr_aligned_free(context->work_combined_params);
265
0
    winpr_aligned_free(context->work_enc_params);
266
0
    winpr_aligned_free(context->work_dec_params);
267
0
  }
268
0
  winpr_aligned_free(context);
269
0
}
270
271
WINPR_ATTR_NODISCARD
272
static inline YUV_PROCESS_WORK_PARAM pool_decode_param(const RECTANGLE_16* WINPR_RESTRICT rect,
273
                                                       YUV_CONTEXT* WINPR_RESTRICT context,
274
                                                       const BYTE* WINPR_RESTRICT pYUVData[3],
275
                                                       const UINT32 iStride[3], UINT32 DstFormat,
276
                                                       BYTE* WINPR_RESTRICT dest, UINT32 nDstStep)
277
0
{
278
0
  YUV_PROCESS_WORK_PARAM current = WINPR_C_ARRAY_INIT;
279
280
0
  WINPR_ASSERT(rect);
281
0
  WINPR_ASSERT(context);
282
0
  WINPR_ASSERT(pYUVData);
283
0
  WINPR_ASSERT(iStride);
284
0
  WINPR_ASSERT(dest);
285
286
0
  current.context = context;
287
0
  current.DstFormat = DstFormat;
288
0
  current.pYUVData[0] = pYUVData[0];
289
0
  current.pYUVData[1] = pYUVData[1];
290
0
  current.pYUVData[2] = pYUVData[2];
291
0
  current.iStride[0] = iStride[0];
292
0
  current.iStride[1] = iStride[1];
293
0
  current.iStride[2] = iStride[2];
294
0
  current.nDstStep = nDstStep;
295
0
  current.dest = dest;
296
0
  current.rect = *rect;
297
0
  return current;
298
0
}
299
300
WINPR_ATTR_NODISCARD
301
static BOOL submit_object(PTP_WORK* WINPR_RESTRICT work_object, PTP_WORK_CALLBACK cb,
302
                          const void* WINPR_RESTRICT param, YUV_CONTEXT* WINPR_RESTRICT context)
303
0
{
304
0
  union
305
0
  {
306
0
    const void* cpv;
307
0
    void* pv;
308
0
  } cnv;
309
310
0
  cnv.cpv = param;
311
312
0
  if (!work_object)
313
0
    return FALSE;
314
315
0
  *work_object = nullptr;
316
317
0
  if (!param || !context)
318
0
    return FALSE;
319
320
0
  *work_object = CreateThreadpoolWork(cb, cnv.pv, nullptr);
321
0
  if (!*work_object)
322
0
    return FALSE;
323
324
0
  SubmitThreadpoolWork(*work_object);
325
0
  return TRUE;
326
0
}
327
328
static void free_objects(PTP_WORK* work_objects, UINT32 waitCount)
329
0
{
330
0
  WINPR_ASSERT(work_objects || (waitCount == 0));
331
332
0
  for (UINT32 i = 0; i < waitCount; i++)
333
0
  {
334
0
    PTP_WORK cur = work_objects[i];
335
0
    work_objects[i] = nullptr;
336
337
0
    if (!cur)
338
0
      continue;
339
340
0
    WaitForThreadpoolWorkCallbacks(cur, FALSE);
341
0
    CloseThreadpoolWork(cur);
342
0
  }
343
0
}
344
345
WINPR_ATTR_NODISCARD
346
static BOOL intersects(UINT32 pos, const RECTANGLE_16* WINPR_RESTRICT regionRects,
347
                       UINT32 numRegionRects)
348
0
{
349
0
  WINPR_ASSERT(regionRects || (numRegionRects == 0));
350
351
0
  for (UINT32 x = pos + 1; x < numRegionRects; x++)
352
0
  {
353
0
    const RECTANGLE_16* what = &regionRects[pos];
354
0
    const RECTANGLE_16* rect = &regionRects[x];
355
356
0
    if (rectangles_intersects(what, rect))
357
0
    {
358
0
      WLog_WARN(TAG, "YUV decoder: intersecting rectangles, aborting");
359
0
      return TRUE;
360
0
    }
361
0
  }
362
363
0
  return FALSE;
364
0
}
365
366
WINPR_ATTR_NODISCARD
367
static RECTANGLE_16 clamp(YUV_CONTEXT* WINPR_RESTRICT context,
368
                          const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 srcHeight)
369
0
{
370
0
  WINPR_ASSERT(context);
371
0
  WINPR_ASSERT(rect);
372
373
0
  RECTANGLE_16 c = *rect;
374
0
  const UINT32 height = MIN(context->height, srcHeight);
375
0
  if (c.top > height)
376
0
    c.top = WINPR_ASSERTING_INT_CAST(UINT16, height);
377
0
  if (c.bottom > height)
378
0
    c.bottom = WINPR_ASSERTING_INT_CAST(UINT16, height);
379
0
  return c;
380
0
}
381
382
WINPR_ATTR_NODISCARD
383
static BOOL pool_decode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
384
                        const BYTE* WINPR_RESTRICT pYUVData[3], const UINT32 iStride[3],
385
                        UINT32 yuvHeight, UINT32 DstFormat, BYTE* WINPR_RESTRICT dest,
386
                        UINT32 nDstStep, const RECTANGLE_16* WINPR_RESTRICT regionRects,
387
                        UINT32 numRegionRects)
388
0
{
389
0
  BOOL rc = FALSE;
390
0
  UINT32 waitCount = 0;
391
0
  primitives_t* prims = primitives_get();
392
393
0
  WINPR_ASSERT(context);
394
0
  WINPR_ASSERT(cb);
395
0
  WINPR_ASSERT(pYUVData);
396
0
  WINPR_ASSERT(iStride);
397
0
  WINPR_ASSERT(dest);
398
0
  WINPR_ASSERT(regionRects || (numRegionRects == 0));
399
400
0
  if (context->encoder)
401
0
  {
402
0
    WLog_ERR(TAG, "YUV context set up for encoding, can not decode with it, aborting");
403
0
    return FALSE;
404
0
  }
405
406
0
  if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
407
0
  {
408
0
    for (UINT32 y = 0; y < numRegionRects; y++)
409
0
    {
410
0
      const RECTANGLE_16 rect = clamp(context, &regionRects[y], yuvHeight);
411
0
      YUV_PROCESS_WORK_PARAM current =
412
0
          pool_decode_param(&rect, context, pYUVData, iStride, DstFormat, dest, nDstStep);
413
0
      cb(nullptr, &current, nullptr);
414
0
    }
415
0
    return TRUE;
416
0
  }
417
418
  /* case where we use threads */
419
0
  for (UINT32 x = 0; x < numRegionRects; x++)
420
0
  {
421
0
    RECTANGLE_16 r = clamp(context, &regionRects[x], yuvHeight);
422
423
0
    if (intersects(x, regionRects, numRegionRects))
424
0
      continue;
425
426
0
    while (r.left < r.right)
427
0
    {
428
0
      RECTANGLE_16 y = r;
429
0
      y.right = MIN(r.right, r.left + YUV_TILE_SIZE);
430
431
0
      while (y.top < y.bottom)
432
0
      {
433
0
        RECTANGLE_16 z = y;
434
435
0
        if (context->work_object_count <= waitCount)
436
0
        {
437
0
          free_objects(context->work_objects, context->work_object_count);
438
0
          waitCount = 0;
439
0
        }
440
441
0
        YUV_PROCESS_WORK_PARAM* cur = &context->work_dec_params[waitCount];
442
0
        z.bottom = MIN(z.bottom, z.top + YUV_TILE_SIZE);
443
0
        if (rectangle_is_empty(&z))
444
0
          continue;
445
0
        *cur = pool_decode_param(&z, context, pYUVData, iStride, DstFormat, dest, nDstStep);
446
0
        if (!submit_object(&context->work_objects[waitCount], cb, cur, context))
447
0
          goto fail;
448
0
        waitCount++;
449
0
        y.top += YUV_TILE_SIZE;
450
0
      }
451
452
0
      r.left += YUV_TILE_SIZE;
453
0
    }
454
0
  }
455
0
  rc = TRUE;
456
0
fail:
457
0
  free_objects(context->work_objects, context->work_object_count);
458
0
  return rc;
459
0
}
460
461
WINPR_ATTR_NODISCARD
462
static inline BOOL check_rect(const YUV_CONTEXT* WINPR_RESTRICT yuv,
463
                              const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstWidth,
464
                              UINT32 nDstHeight)
465
0
{
466
0
  WINPR_ASSERT(yuv);
467
0
  WINPR_ASSERT(rect);
468
469
  /* Check, if the output rectangle is valid in decoded h264 frame. */
470
0
  if ((rect->right > yuv->width) || (rect->left > yuv->width))
471
0
    return FALSE;
472
473
0
  if ((rect->top > yuv->height) || (rect->bottom > yuv->height))
474
0
    return FALSE;
475
476
  /* Check, if the output rectangle is valid in destination buffer. */
477
0
  if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
478
0
    return FALSE;
479
480
0
  if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
481
0
    return FALSE;
482
483
0
  return TRUE;
484
0
}
485
486
static void CALLBACK yuv444_combine_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
487
                                                  PTP_WORK work)
488
0
{
489
0
  YUV_COMBINE_WORK_PARAM* param = (YUV_COMBINE_WORK_PARAM*)context;
490
0
  primitives_t* prims = primitives_get();
491
492
0
  WINPR_ASSERT(param);
493
0
  YUV_CONTEXT* yuv = param->context;
494
0
  WINPR_ASSERT(yuv);
495
496
0
  const RECTANGLE_16* rect = &param->rect;
497
0
  WINPR_ASSERT(rect);
498
499
  /* For AVC444v2 the combine reads the V (and second half U) samples from the
500
   * decoded auxiliary plane at a fixed nTotalWidth offset. A server may send a
501
   * frame smaller than the surface, so cap the aligned width at the Y plane
502
   * stride to keep that offset inside the decoded plane. */
503
0
  const UINT32 alignedWidth =
504
0
      MIN(yuv->width + ((yuv->width % 32 != 0) ? 32 - yuv->width % 32 : 0), param->iStride[0]);
505
0
  const UINT32 alignedHeight =
506
0
      yuv->height + ((yuv->height % 16 != 0) ? 16 - yuv->height % 16 : 0);
507
508
0
  WINPR_UNUSED(instance);
509
0
  WINPR_UNUSED(work);
510
511
0
  if (!check_rect(param->context, rect, yuv->width, yuv->height))
512
0
    return;
513
514
0
  if (prims->YUV420CombineToYUV444(param->type, param->pYUVData, param->iStride, alignedWidth,
515
0
                                   alignedHeight, param->pYUVDstData, param->iDstStride,
516
0
                                   rect) != PRIMITIVES_SUCCESS)
517
0
    WLog_WARN(TAG, "YUV420CombineToYUV444 failed");
518
0
}
519
520
WINPR_ATTR_NODISCARD
521
static inline YUV_COMBINE_WORK_PARAM
522
pool_decode_rect_param(const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
523
                       BYTE type, const BYTE* WINPR_RESTRICT pYUVData[3], const UINT32 iStride[3],
524
                       BYTE* WINPR_RESTRICT pYUVDstData[3], const UINT32 iDstStride[3])
525
0
{
526
0
  YUV_COMBINE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
527
528
0
  WINPR_ASSERT(rect);
529
0
  WINPR_ASSERT(context);
530
0
  WINPR_ASSERT(pYUVData);
531
0
  WINPR_ASSERT(iStride);
532
0
  WINPR_ASSERT(pYUVDstData);
533
0
  WINPR_ASSERT(iDstStride);
534
535
0
  current.context = context;
536
0
  current.pYUVData[0] = pYUVData[0];
537
0
  current.pYUVData[1] = pYUVData[1];
538
0
  current.pYUVData[2] = pYUVData[2];
539
0
  current.pYUVDstData[0] = pYUVDstData[0];
540
0
  current.pYUVDstData[1] = pYUVDstData[1];
541
0
  current.pYUVDstData[2] = pYUVDstData[2];
542
0
  current.iStride[0] = iStride[0];
543
0
  current.iStride[1] = iStride[1];
544
0
  current.iStride[2] = iStride[2];
545
0
  current.iDstStride[0] = iDstStride[0];
546
0
  current.iDstStride[1] = iDstStride[1];
547
0
  current.iDstStride[2] = iDstStride[2];
548
0
  current.type = WINPR_ASSERTING_INT_CAST(avc444_frame_type, type);
549
0
  current.rect = *rect;
550
0
  return current;
551
0
}
552
553
WINPR_ATTR_NODISCARD
554
static BOOL pool_decode_rect(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
555
                             const BYTE* WINPR_RESTRICT pYUVData[3], const UINT32 iStride[3],
556
                             BYTE* WINPR_RESTRICT pYUVDstData[3], const UINT32 iDstStride[3],
557
                             const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
558
0
{
559
0
  BOOL rc = FALSE;
560
0
  UINT32 waitCount = 0;
561
0
  PTP_WORK_CALLBACK cb = yuv444_combine_work_callback;
562
0
  primitives_t* prims = primitives_get();
563
564
0
  WINPR_ASSERT(context);
565
0
  WINPR_ASSERT(pYUVData);
566
0
  WINPR_ASSERT(iStride);
567
0
  WINPR_ASSERT(pYUVDstData);
568
0
  WINPR_ASSERT(iDstStride);
569
0
  WINPR_ASSERT(regionRects || (numRegionRects == 0));
570
571
0
  if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
572
0
  {
573
0
    for (UINT32 y = 0; y < numRegionRects; y++)
574
0
    {
575
0
      YUV_COMBINE_WORK_PARAM current = pool_decode_rect_param(
576
0
          &regionRects[y], context, type, pYUVData, iStride, pYUVDstData, iDstStride);
577
0
      cb(nullptr, &current, nullptr);
578
0
    }
579
0
    return TRUE;
580
0
  }
581
582
  /* case where we use threads */
583
0
  for (UINT32 x = 0; x < numRegionRects; x++)
584
0
  {
585
0
    YUV_COMBINE_WORK_PARAM* current = nullptr;
586
587
0
    if (context->work_object_count <= waitCount)
588
0
    {
589
0
      free_objects(context->work_objects, context->work_object_count);
590
0
      waitCount = 0;
591
0
    }
592
0
    current = &context->work_combined_params[waitCount];
593
0
    *current = pool_decode_rect_param(&regionRects[x], context, type, pYUVData, iStride,
594
0
                                      pYUVDstData, iDstStride);
595
596
0
    if (!submit_object(&context->work_objects[waitCount], cb, current, context))
597
0
      goto fail;
598
0
    waitCount++;
599
0
  }
600
601
0
  rc = TRUE;
602
0
fail:
603
0
  free_objects(context->work_objects, context->work_object_count);
604
0
  return rc;
605
0
}
606
607
BOOL yuv444_context_decode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
608
                           const BYTE* WINPR_RESTRICT pYUVData[3], const UINT32 iStride[3],
609
                           UINT32 srcYuvHeight, BYTE* WINPR_RESTRICT pYUVDstData[3],
610
                           const UINT32 iDstStride[3], DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
611
                           UINT32 nDstStep, const RECTANGLE_16* WINPR_RESTRICT regionRects,
612
                           UINT32 numRegionRects)
613
0
{
614
0
  const BYTE* pYUVCDstData[3];
615
616
0
  WINPR_ASSERT(context);
617
0
  WINPR_ASSERT(pYUVData);
618
0
  WINPR_ASSERT(iStride);
619
0
  WINPR_ASSERT(pYUVDstData);
620
0
  WINPR_ASSERT(iDstStride);
621
0
  WINPR_ASSERT(dest);
622
0
  WINPR_ASSERT(regionRects || (numRegionRects == 0));
623
624
0
  if (context->encoder)
625
0
  {
626
0
    WLog_ERR(TAG, "YUV context set up for encoding, can not decode with it, aborting");
627
0
    return FALSE;
628
0
  }
629
0
  if (!pool_decode_rect(context, type, pYUVData, iStride, pYUVDstData, iDstStride, regionRects,
630
0
                        numRegionRects))
631
0
    return FALSE;
632
633
0
  pYUVCDstData[0] = pYUVDstData[0];
634
0
  pYUVCDstData[1] = pYUVDstData[1];
635
0
  pYUVCDstData[2] = pYUVDstData[2];
636
0
  return pool_decode(context, yuv444_process_work_callback, pYUVCDstData, iDstStride,
637
0
                     srcYuvHeight, DstFormat, dest, nDstStep, regionRects, numRegionRects);
638
0
}
639
640
BOOL yuv420_context_decode(YUV_CONTEXT* WINPR_RESTRICT context,
641
                           const BYTE* WINPR_RESTRICT pYUVData[3], const UINT32 iStride[3],
642
                           UINT32 yuvHeight, DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
643
                           UINT32 nDstStep, const RECTANGLE_16* WINPR_RESTRICT regionRects,
644
                           UINT32 numRegionRects)
645
0
{
646
0
  return pool_decode(context, yuv420_process_work_callback, pYUVData, iStride, yuvHeight,
647
0
                     DstFormat, dest, nDstStep, regionRects, numRegionRects);
648
0
}
649
650
static void CALLBACK yuv420_encode_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
651
                                                 PTP_WORK work)
652
0
{
653
0
  prim_size_t roi;
654
0
  YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
655
0
  primitives_t* prims = primitives_get();
656
0
  BYTE* pYUVData[3];
657
0
  const BYTE* src = nullptr;
658
659
0
  WINPR_UNUSED(instance);
660
0
  WINPR_UNUSED(work);
661
0
  WINPR_ASSERT(param);
662
663
0
  roi.width = param->rect.right - param->rect.left;
664
0
  roi.height = param->rect.bottom - param->rect.top;
665
0
  src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
666
0
        1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
667
0
  pYUVData[0] =
668
0
      param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
669
0
  pYUVData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
670
0
                param->rect.left / 2;
671
0
  pYUVData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
672
0
                param->rect.left / 2;
673
674
0
  if (prims->RGBToYUV420_8u_P3AC4R(src, param->SrcFormat, param->nSrcStep, pYUVData,
675
0
                                   param->iStride, &roi) != PRIMITIVES_SUCCESS)
676
0
  {
677
0
    WLog_ERR(TAG, "error when decoding lines");
678
0
  }
679
0
}
680
681
static void CALLBACK yuv444v1_encode_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
682
                                                   PTP_WORK work)
683
0
{
684
0
  prim_size_t roi;
685
0
  YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
686
0
  primitives_t* prims = primitives_get();
687
0
  BYTE* pYUVLumaData[3] = WINPR_C_ARRAY_INIT;
688
0
  BYTE* pYUVChromaData[3] = WINPR_C_ARRAY_INIT;
689
0
  const BYTE* src = nullptr;
690
691
0
  WINPR_UNUSED(instance);
692
0
  WINPR_UNUSED(work);
693
0
  WINPR_ASSERT(param);
694
695
0
  roi.width = param->rect.right - param->rect.left;
696
0
  roi.height = param->rect.bottom - param->rect.top;
697
0
  {
698
0
    const size_t offset = 1ULL * param->nSrcStep * param->rect.top +
699
0
                          1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
700
0
    src = &param->pSrcData[offset];
701
0
  }
702
0
  {
703
0
    const size_t offset = 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
704
0
    pYUVLumaData[0] = &param->pYUVLumaData[0][offset];
705
0
  }
706
0
  {
707
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[1] + param->rect.left / 2;
708
0
    pYUVLumaData[1] = &param->pYUVLumaData[1][offset];
709
0
  }
710
0
  {
711
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[2] + param->rect.left / 2;
712
0
    pYUVLumaData[2] = &param->pYUVLumaData[2][offset];
713
0
  }
714
0
  {
715
0
    const size_t offset = 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
716
0
    pYUVChromaData[0] = &param->pYUVChromaData[0][offset];
717
0
  }
718
0
  {
719
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[1] + param->rect.left / 2;
720
0
    pYUVChromaData[1] = &param->pYUVChromaData[1][offset];
721
0
  }
722
0
  {
723
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[2] + param->rect.left / 2;
724
0
    pYUVChromaData[2] = &param->pYUVChromaData[2][offset];
725
0
  }
726
0
  if (prims->RGBToAVC444YUV(src, param->SrcFormat, param->nSrcStep, pYUVLumaData, param->iStride,
727
0
                            pYUVChromaData, param->iStride, &roi) != PRIMITIVES_SUCCESS)
728
0
  {
729
0
    WLog_ERR(TAG, "error when decoding lines");
730
0
  }
731
0
}
732
733
static void CALLBACK yuv444v2_encode_work_callback(PTP_CALLBACK_INSTANCE instance, void* context,
734
                                                   PTP_WORK work)
735
0
{
736
0
  prim_size_t roi;
737
0
  YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
738
0
  primitives_t* prims = primitives_get();
739
0
  BYTE* pYUVLumaData[3];
740
0
  BYTE* pYUVChromaData[3];
741
0
  const BYTE* src = nullptr;
742
743
0
  WINPR_UNUSED(instance);
744
0
  WINPR_UNUSED(work);
745
0
  WINPR_ASSERT(param);
746
747
0
  roi.width = param->rect.right - param->rect.left;
748
0
  roi.height = param->rect.bottom - param->rect.top;
749
0
  {
750
0
    const size_t offset = 1ULL * param->nSrcStep * param->rect.top +
751
0
                          1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
752
0
    src = &param->pSrcData[offset];
753
0
  }
754
0
  {
755
0
    const size_t offset = 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
756
0
    pYUVLumaData[0] = &param->pYUVLumaData[0][offset];
757
0
  }
758
0
  {
759
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[1] + param->rect.left / 2;
760
0
    pYUVLumaData[1] = &param->pYUVLumaData[1][offset];
761
0
  }
762
0
  {
763
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[2] + param->rect.left / 2;
764
0
    pYUVLumaData[2] = &param->pYUVLumaData[2][offset];
765
0
  }
766
0
  {
767
0
    const size_t offset = 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
768
0
    pYUVChromaData[0] = &param->pYUVChromaData[0][offset];
769
0
  }
770
0
  {
771
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[1] + param->rect.left / 2;
772
0
    pYUVChromaData[1] = &param->pYUVChromaData[1][offset];
773
0
  }
774
0
  {
775
0
    const size_t offset = 1ULL * param->rect.top / 2 * param->iStride[2] + param->rect.left / 2;
776
0
    pYUVChromaData[2] = &param->pYUVChromaData[2][offset];
777
0
  }
778
0
  if (prims->RGBToAVC444YUVv2(src, param->SrcFormat, param->nSrcStep, pYUVLumaData,
779
0
                              param->iStride, pYUVChromaData, param->iStride,
780
0
                              &roi) != PRIMITIVES_SUCCESS)
781
0
  {
782
0
    WLog_ERR(TAG, "error when decoding lines");
783
0
  }
784
0
}
785
786
WINPR_ATTR_NODISCARD
787
static inline YUV_ENCODE_WORK_PARAM
788
pool_encode_fill(const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
789
                 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
790
                 const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
791
                 BYTE* WINPR_RESTRICT pYUVChromaData[])
792
0
{
793
0
  YUV_ENCODE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
794
795
0
  WINPR_ASSERT(rect);
796
0
  WINPR_ASSERT(context);
797
0
  WINPR_ASSERT(pSrcData);
798
0
  WINPR_ASSERT(iStride);
799
0
  WINPR_ASSERT(pYUVLumaData);
800
801
0
  current.context = context;
802
0
  current.pSrcData = pSrcData;
803
0
  current.SrcFormat = SrcFormat;
804
0
  current.nSrcStep = nSrcStep;
805
0
  current.pYUVLumaData[0] = pYUVLumaData[0];
806
0
  current.pYUVLumaData[1] = pYUVLumaData[1];
807
0
  current.pYUVLumaData[2] = pYUVLumaData[2];
808
0
  if (pYUVChromaData)
809
0
  {
810
0
    current.pYUVChromaData[0] = pYUVChromaData[0];
811
0
    current.pYUVChromaData[1] = pYUVChromaData[1];
812
0
    current.pYUVChromaData[2] = pYUVChromaData[2];
813
0
  }
814
0
  current.iStride[0] = iStride[0];
815
0
  current.iStride[1] = iStride[1];
816
0
  current.iStride[2] = iStride[2];
817
818
0
  current.rect = *rect;
819
820
0
  return current;
821
0
}
822
823
WINPR_ATTR_NODISCARD
824
static uint32_t getSteps(uint32_t height, uint32_t step)
825
0
{
826
0
  const uint32_t steps = (height + step / 2 + 1) / step;
827
0
  if (steps < 1)
828
0
    return 1;
829
0
  return steps;
830
0
}
831
832
WINPR_ATTR_NODISCARD
833
static BOOL pool_encode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
834
                        const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
835
                        const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
836
                        BYTE* WINPR_RESTRICT pYUVChromaData[],
837
                        const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
838
0
{
839
0
  BOOL rc = FALSE;
840
0
  primitives_t* prims = primitives_get();
841
0
  UINT32 waitCount = 0;
842
843
0
  WINPR_ASSERT(context);
844
0
  WINPR_ASSERT(cb);
845
0
  WINPR_ASSERT(pSrcData);
846
0
  WINPR_ASSERT(iStride);
847
0
  WINPR_ASSERT(regionRects || (numRegionRects == 0));
848
849
0
  if (!context->encoder)
850
0
  {
851
852
0
    WLog_ERR(TAG, "YUV context set up for decoding, can not encode with it, aborting");
853
0
    return FALSE;
854
0
  }
855
856
0
  if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
857
0
  {
858
0
    for (UINT32 x = 0; x < numRegionRects; x++)
859
0
    {
860
0
      YUV_ENCODE_WORK_PARAM current =
861
0
          pool_encode_fill(&regionRects[x], context, pSrcData, nSrcStep, SrcFormat, iStride,
862
0
                           pYUVLumaData, pYUVChromaData);
863
0
      cb(nullptr, &current, nullptr);
864
0
    }
865
0
    return TRUE;
866
0
  }
867
868
  /* case where we use threads */
869
0
  for (UINT32 x = 0; x < numRegionRects; x++)
870
0
  {
871
0
    const RECTANGLE_16* rect = &regionRects[x];
872
0
    const UINT32 height = rect->bottom - rect->top;
873
0
    const UINT32 steps = getSteps(height, context->heightStep);
874
875
0
    for (UINT32 y = 0; y < steps; y++)
876
0
    {
877
0
      RECTANGLE_16 r = *rect;
878
0
      YUV_ENCODE_WORK_PARAM* current = nullptr;
879
880
0
      if (context->work_object_count <= waitCount)
881
0
      {
882
0
        free_objects(context->work_objects, context->work_object_count);
883
0
        waitCount = 0;
884
0
      }
885
886
0
      current = &context->work_enc_params[waitCount];
887
0
      r.top += y * context->heightStep;
888
      /* Limit the slice to its own rows. Without this every slice converts everything from
889
       * its first row down to the bottom of the region, so the same rows are converted over
890
       * and over (about 34 times for a 1080p frame and 16 row slices). getSteps rounds to the
891
       * nearest slice count, so the last slice has to reach the bottom of the region whatever
892
       * its height is. */
893
0
      if ((y + 1) < steps)
894
0
        r.bottom = (UINT16)MIN((UINT32)rect->bottom, (UINT32)r.top + context->heightStep);
895
0
      *current = pool_encode_fill(&r, context, pSrcData, nSrcStep, SrcFormat, iStride,
896
0
                                  pYUVLumaData, pYUVChromaData);
897
0
      if (!submit_object(&context->work_objects[waitCount], cb, current, context))
898
0
        goto fail;
899
0
      waitCount++;
900
0
    }
901
0
  }
902
903
0
  rc = TRUE;
904
0
fail:
905
0
  free_objects(context->work_objects, context->work_object_count);
906
0
  return rc;
907
0
}
908
909
BOOL yuv420_context_encode(YUV_CONTEXT* WINPR_RESTRICT context, const BYTE* WINPR_RESTRICT pSrcData,
910
                           UINT32 nSrcStep, UINT32 SrcFormat, const UINT32 iStride[3],
911
                           BYTE* WINPR_RESTRICT pYUVData[3],
912
                           const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
913
0
{
914
0
  if (!context || !pSrcData || !iStride || !pYUVData || !regionRects)
915
0
    return FALSE;
916
917
0
  return pool_encode(context, yuv420_encode_work_callback, pSrcData, nSrcStep, SrcFormat, iStride,
918
0
                     pYUVData, nullptr, regionRects, numRegionRects);
919
0
}
920
921
BOOL yuv444_context_encode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE version,
922
                           const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
923
                           const UINT32 iStride[3], BYTE* WINPR_RESTRICT pYUVLumaData[3],
924
                           BYTE* WINPR_RESTRICT pYUVChromaData[3],
925
                           const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
926
0
{
927
0
  switch (version)
928
0
  {
929
0
    case 1:
930
0
      return pool_encode(context, yuv444v1_encode_work_callback, pSrcData, nSrcStep,
931
0
                         SrcFormat, iStride, pYUVLumaData, pYUVChromaData, regionRects,
932
0
                         numRegionRects);
933
0
    case 2:
934
0
      break;
935
0
    default:
936
0
      return FALSE;
937
0
  }
938
939
0
  if (!context || !regionRects)
940
0
    return FALSE;
941
942
  /* AVC444v2 puts the chroma detail of a row into the two halves of the auxiliary view
943
   * (MS-RDPEGFX 3.3.8.3.3): U into the left half at x / 2, V into the right half at
944
   * width / 2 + x / 2. RGBToAVC444YUVv2 places the right half at roi->width / 2 from the
945
   * left one, which is only width / 2 when the region spans the whole width; for a narrower
946
   * region (the shadow server encodes what changed) it wrote the chroma detail into the wrong
947
   * columns, and the client showed smeared, blocky colours. Convert whole rows: which parts of
948
   * the frame are sent is still decided by the change detection afterwards. */
949
0
  RECTANGLE_16 stackRects[8] = WINPR_C_ARRAY_INIT;
950
0
  RECTANGLE_16* rows = stackRects;
951
0
  if (numRegionRects > ARRAYSIZE(stackRects))
952
0
  {
953
0
    rows = calloc(numRegionRects, sizeof(RECTANGLE_16));
954
0
    if (!rows)
955
0
      return FALSE;
956
0
  }
957
958
0
  for (UINT32 x = 0; x < numRegionRects; x++)
959
0
  {
960
0
    rows[x] = regionRects[x];
961
0
    rows[x].left = 0;
962
0
    rows[x].right = (UINT16)MIN(UINT16_MAX, MAX(context->width, regionRects[x].right));
963
0
  }
964
965
0
  const BOOL rc =
966
0
      pool_encode(context, yuv444v2_encode_work_callback, pSrcData, nSrcStep, SrcFormat, iStride,
967
0
                  pYUVLumaData, pYUVChromaData, rows, numRegionRects);
968
0
  if (rows != stackRects)
969
0
    free(rows);
970
0
  return rc;
971
0
}