Coverage Report

Created: 2026-08-31 06:29

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