Coverage Report

Created: 2025-08-26 06:37

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