1#include <winpr/sysinfo.h>
2#include <winpr/assert.h>
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>
12#define TAG FREERDP_TAG("codec")
19 const BYTE* pYUVData[3];
25} YUV_PROCESS_WORK_PARAM;
30 const BYTE* pYUVData[3];
35 avc444_frame_type type;
36} YUV_COMBINE_WORK_PARAM;
48 BYTE* pYUVLumaData[3];
49 BYTE* pYUVChromaData[3];
51} YUV_ENCODE_WORK_PARAM;
60 UINT32 work_object_count;
61 PTP_WORK* work_objects;
62 YUV_ENCODE_WORK_PARAM* work_enc_params;
63 YUV_PROCESS_WORK_PARAM* work_dec_params;
64 YUV_COMBINE_WORK_PARAM* work_combined_params;
67static inline BOOL avc420_yuv_to_rgb(
const BYTE* WINPR_RESTRICT pYUVData[3],
68 const UINT32 iStride[3],
69 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstStep,
70 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat)
74 const BYTE* pYUVPoint[3];
76 WINPR_ASSERT(pYUVData);
77 WINPR_ASSERT(iStride);
79 WINPR_ASSERT(pDstData);
81 const INT32 width = rect->right - rect->left;
82 const INT32 height = rect->bottom - rect->top;
83 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
84 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
86 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
87 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top / 2 * iStride[1] + rect->left / 2;
88 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top / 2 * iStride[2] + rect->left / 2;
90 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
91 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
93 return (prims->YUV420ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
94 &roi) == PRIMITIVES_SUCCESS);
97static inline BOOL avc444_yuv_to_rgb(
const BYTE* WINPR_RESTRICT pYUVData[3],
98 const UINT32 iStride[3],
99 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstStep,
100 BYTE* WINPR_RESTRICT pDstData, DWORD DstFormat)
104 const BYTE* pYUVPoint[3];
106 WINPR_ASSERT(pYUVData);
107 WINPR_ASSERT(iStride);
109 WINPR_ASSERT(pDstData);
111 const INT32 width = rect->right - rect->left;
112 const INT32 height = rect->bottom - rect->top;
113 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
114 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
116 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
117 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top * iStride[1] + rect->left;
118 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top * iStride[2] + rect->left;
120 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
121 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
123 return (prims->YUV444ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
124 &roi) == PRIMITIVES_SUCCESS);
127static void CALLBACK yuv420_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
130 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
131 WINPR_UNUSED(instance);
135 if (!avc420_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
136 param->dest, param->DstFormat))
137 WLog_WARN(TAG,
"avc420_yuv_to_rgb failed");
140static void CALLBACK yuv444_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
143 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
144 WINPR_UNUSED(instance);
148 if (!avc444_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
149 param->dest, param->DstFormat))
150 WLog_WARN(TAG,
"avc444_yuv_to_rgb failed");
153BOOL yuv_context_reset(YUV_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
156 WINPR_ASSERT(context);
158 context->width = width;
159 context->height = height;
161 context->heightStep = height;
163 if (context->useThreads)
165 context->heightStep = 16;
171 const size_t pw = (width + TILE_SIZE - width % TILE_SIZE) / 16;
172 const size_t ph = (height + TILE_SIZE - height % TILE_SIZE) / 16;
174 const size_t count = pw * ph;
176 context->work_object_count = 0;
177 if (context->encoder)
179 void* tmp = winpr_aligned_recalloc(context->work_enc_params, count,
180 sizeof(YUV_ENCODE_WORK_PARAM), 32);
183 memset(tmp, 0, count *
sizeof(YUV_ENCODE_WORK_PARAM));
185 context->work_enc_params = tmp;
189 void* tmp = winpr_aligned_recalloc(context->work_dec_params, count,
190 sizeof(YUV_PROCESS_WORK_PARAM), 32);
193 memset(tmp, 0, count *
sizeof(YUV_PROCESS_WORK_PARAM));
195 context->work_dec_params = tmp;
197 void* ctmp = winpr_aligned_recalloc(context->work_combined_params, count,
198 sizeof(YUV_COMBINE_WORK_PARAM), 32);
201 memset(ctmp, 0, count *
sizeof(YUV_COMBINE_WORK_PARAM));
203 context->work_combined_params = ctmp;
207 winpr_aligned_recalloc((
void*)context->work_objects, count,
sizeof(PTP_WORK), 32);
210 memset(wtmp, 0, count *
sizeof(PTP_WORK));
212 context->work_objects = (PTP_WORK*)wtmp;
213 context->work_object_count = WINPR_ASSERTING_INT_CAST(uint32_t, count);
220YUV_CONTEXT* yuv_context_new(BOOL encoder, UINT32 ThreadingFlags)
224 if (!primitives_get())
227 YUV_CONTEXT* ret = winpr_aligned_calloc(1,
sizeof(*ret), 32);
231 ret->encoder = encoder;
232 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
234 GetNativeSystemInfo(&sysInfos);
235 ret->useThreads = (sysInfos.dwNumberOfProcessors > 1);
241void yuv_context_free(YUV_CONTEXT* context)
245 if (context->useThreads)
247 winpr_aligned_free((
void*)context->work_objects);
248 winpr_aligned_free(context->work_combined_params);
249 winpr_aligned_free(context->work_enc_params);
250 winpr_aligned_free(context->work_dec_params);
252 winpr_aligned_free(context);
255static inline YUV_PROCESS_WORK_PARAM pool_decode_param(
const RECTANGLE_16* WINPR_RESTRICT rect,
256 YUV_CONTEXT* WINPR_RESTRICT context,
257 const BYTE* WINPR_RESTRICT pYUVData[3],
258 const UINT32 iStride[3], UINT32 DstFormat,
259 BYTE* WINPR_RESTRICT dest, UINT32 nDstStep)
261 YUV_PROCESS_WORK_PARAM current = WINPR_C_ARRAY_INIT;
264 WINPR_ASSERT(context);
265 WINPR_ASSERT(pYUVData);
266 WINPR_ASSERT(iStride);
269 current.context = context;
270 current.DstFormat = DstFormat;
271 current.pYUVData[0] = pYUVData[0];
272 current.pYUVData[1] = pYUVData[1];
273 current.pYUVData[2] = pYUVData[2];
274 current.iStride[0] = iStride[0];
275 current.iStride[1] = iStride[1];
276 current.iStride[2] = iStride[2];
277 current.nDstStep = nDstStep;
279 current.rect = *rect;
283static BOOL submit_object(PTP_WORK* WINPR_RESTRICT work_object, PTP_WORK_CALLBACK cb,
284 const void* WINPR_RESTRICT param, YUV_CONTEXT* WINPR_RESTRICT context)
297 *work_object =
nullptr;
299 if (!param || !context)
302 *work_object = CreateThreadpoolWork(cb, cnv.pv,
nullptr);
306 SubmitThreadpoolWork(*work_object);
310static void free_objects(PTP_WORK* work_objects, UINT32 waitCount)
312 WINPR_ASSERT(work_objects || (waitCount == 0));
314 for (UINT32 i = 0; i < waitCount; i++)
316 PTP_WORK cur = work_objects[i];
317 work_objects[i] =
nullptr;
322 WaitForThreadpoolWorkCallbacks(cur, FALSE);
323 CloseThreadpoolWork(cur);
327static BOOL intersects(UINT32 pos,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
328 UINT32 numRegionRects)
330 WINPR_ASSERT(regionRects || (numRegionRects == 0));
332 for (UINT32 x = pos + 1; x < numRegionRects; x++)
337 if (rectangles_intersects(what, rect))
339 WLog_WARN(TAG,
"YUV decoder: intersecting rectangles, aborting");
347static RECTANGLE_16 clamp(YUV_CONTEXT* WINPR_RESTRICT context,
348 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 srcHeight)
350 WINPR_ASSERT(context);
354 const UINT32 height = MIN(context->height, srcHeight);
356 c.top = WINPR_ASSERTING_INT_CAST(UINT16, height);
357 if (c.bottom > height)
358 c.bottom = WINPR_ASSERTING_INT_CAST(UINT16, height);
362static BOOL pool_decode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
363 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
364 UINT32 yuvHeight, UINT32 DstFormat, BYTE* WINPR_RESTRICT dest,
365 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
366 UINT32 numRegionRects)
369 UINT32 waitCount = 0;
372 WINPR_ASSERT(context);
374 WINPR_ASSERT(pYUVData);
375 WINPR_ASSERT(iStride);
377 WINPR_ASSERT(regionRects || (numRegionRects == 0));
379 if (context->encoder)
381 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
385 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
387 for (UINT32 y = 0; y < numRegionRects; y++)
389 const RECTANGLE_16 rect = clamp(context, ®ionRects[y], yuvHeight);
390 YUV_PROCESS_WORK_PARAM current =
391 pool_decode_param(&rect, context, pYUVData, iStride, DstFormat, dest, nDstStep);
392 cb(
nullptr, ¤t,
nullptr);
398 for (UINT32 x = 0; x < numRegionRects; x++)
400 RECTANGLE_16 r = clamp(context, ®ionRects[x], yuvHeight);
402 if (intersects(x, regionRects, numRegionRects))
405 while (r.left < r.right)
408 y.right = MIN(r.right, r.left + TILE_SIZE);
410 while (y.top < y.bottom)
414 if (context->work_object_count <= waitCount)
416 free_objects(context->work_objects, context->work_object_count);
420 YUV_PROCESS_WORK_PARAM* cur = &context->work_dec_params[waitCount];
421 z.bottom = MIN(z.bottom, z.top + TILE_SIZE);
422 if (rectangle_is_empty(&z))
424 *cur = pool_decode_param(&z, context, pYUVData, iStride, DstFormat, dest, nDstStep);
425 if (!submit_object(&context->work_objects[waitCount], cb, cur, context))
436 free_objects(context->work_objects, context->work_object_count);
440static inline BOOL check_rect(
const YUV_CONTEXT* WINPR_RESTRICT yuv,
441 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstWidth,
448 if ((rect->right > yuv->width) || (rect->left > yuv->width))
451 if ((rect->top > yuv->height) || (rect->bottom > yuv->height))
455 if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
458 if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
464static void CALLBACK yuv444_combine_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
467 YUV_COMBINE_WORK_PARAM* param = (YUV_COMBINE_WORK_PARAM*)context;
471 YUV_CONTEXT* yuv = param->context;
477 const UINT32 alignedWidth = yuv->width + ((yuv->width % 32 != 0) ? 32 - yuv->width % 32 : 0);
478 const UINT32 alignedHeight =
479 yuv->height + ((yuv->height % 16 != 0) ? 16 - yuv->height % 16 : 0);
481 WINPR_UNUSED(instance);
484 if (!check_rect(param->context, rect, yuv->width, yuv->height))
487 if (prims->YUV420CombineToYUV444(param->type, param->pYUVData, param->iStride, alignedWidth,
488 alignedHeight, param->pYUVDstData, param->iDstStride,
489 rect) != PRIMITIVES_SUCCESS)
490 WLog_WARN(TAG,
"YUV420CombineToYUV444 failed");
493static inline YUV_COMBINE_WORK_PARAM
494pool_decode_rect_param(
const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
495 BYTE type,
const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
496 BYTE* WINPR_RESTRICT pYUVDstData[3],
const UINT32 iDstStride[3])
498 YUV_COMBINE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
501 WINPR_ASSERT(context);
502 WINPR_ASSERT(pYUVData);
503 WINPR_ASSERT(iStride);
504 WINPR_ASSERT(pYUVDstData);
505 WINPR_ASSERT(iDstStride);
507 current.context = context;
508 current.pYUVData[0] = pYUVData[0];
509 current.pYUVData[1] = pYUVData[1];
510 current.pYUVData[2] = pYUVData[2];
511 current.pYUVDstData[0] = pYUVDstData[0];
512 current.pYUVDstData[1] = pYUVDstData[1];
513 current.pYUVDstData[2] = pYUVDstData[2];
514 current.iStride[0] = iStride[0];
515 current.iStride[1] = iStride[1];
516 current.iStride[2] = iStride[2];
517 current.iDstStride[0] = iDstStride[0];
518 current.iDstStride[1] = iDstStride[1];
519 current.iDstStride[2] = iDstStride[2];
520 current.type = WINPR_ASSERTING_INT_CAST(avc444_frame_type, type);
521 current.rect = *rect;
525static BOOL pool_decode_rect(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
526 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
527 BYTE* WINPR_RESTRICT pYUVDstData[3],
const UINT32 iDstStride[3],
528 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
531 UINT32 waitCount = 0;
532 PTP_WORK_CALLBACK cb = yuv444_combine_work_callback;
535 WINPR_ASSERT(context);
536 WINPR_ASSERT(pYUVData);
537 WINPR_ASSERT(iStride);
538 WINPR_ASSERT(pYUVDstData);
539 WINPR_ASSERT(iDstStride);
540 WINPR_ASSERT(regionRects || (numRegionRects == 0));
542 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
544 for (UINT32 y = 0; y < numRegionRects; y++)
546 YUV_COMBINE_WORK_PARAM current = pool_decode_rect_param(
547 ®ionRects[y], context, type, pYUVData, iStride, pYUVDstData, iDstStride);
548 cb(
nullptr, ¤t,
nullptr);
554 for (waitCount = 0; waitCount < numRegionRects; waitCount++)
556 YUV_COMBINE_WORK_PARAM* current =
nullptr;
558 if (context->work_object_count <= waitCount)
560 free_objects(context->work_objects, context->work_object_count);
563 current = &context->work_combined_params[waitCount];
564 *current = pool_decode_rect_param(®ionRects[waitCount], context, type, pYUVData, iStride,
565 pYUVDstData, iDstStride);
567 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
573 free_objects(context->work_objects, context->work_object_count);
577BOOL yuv444_context_decode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
578 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
579 UINT32 srcYuvHeight, BYTE* WINPR_RESTRICT pYUVDstData[3],
580 const UINT32 iDstStride[3], DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
581 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
582 UINT32 numRegionRects)
584 const BYTE* pYUVCDstData[3];
586 WINPR_ASSERT(context);
587 WINPR_ASSERT(pYUVData);
588 WINPR_ASSERT(iStride);
589 WINPR_ASSERT(pYUVDstData);
590 WINPR_ASSERT(iDstStride);
592 WINPR_ASSERT(regionRects || (numRegionRects == 0));
594 if (context->encoder)
596 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
599 if (!pool_decode_rect(context, type, pYUVData, iStride, pYUVDstData, iDstStride, regionRects,
603 pYUVCDstData[0] = pYUVDstData[0];
604 pYUVCDstData[1] = pYUVDstData[1];
605 pYUVCDstData[2] = pYUVDstData[2];
606 return pool_decode(context, yuv444_process_work_callback, pYUVCDstData, iDstStride,
607 srcYuvHeight, DstFormat, dest, nDstStep, regionRects, numRegionRects);
610BOOL yuv420_context_decode(YUV_CONTEXT* WINPR_RESTRICT context,
611 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
612 UINT32 yuvHeight, DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
613 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
614 UINT32 numRegionRects)
616 return pool_decode(context, yuv420_process_work_callback, pYUVData, iStride, yuvHeight,
617 DstFormat, dest, nDstStep, regionRects, numRegionRects);
620static void CALLBACK yuv420_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
624 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
627 const BYTE* src =
nullptr;
629 WINPR_UNUSED(instance);
633 roi.width = param->rect.right - param->rect.left;
634 roi.height = param->rect.bottom - param->rect.top;
635 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
636 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
638 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
639 pYUVData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
640 param->rect.left / 2;
641 pYUVData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
642 param->rect.left / 2;
644 if (prims->RGBToYUV420_8u_P3AC4R(src, param->SrcFormat, param->nSrcStep, pYUVData,
645 param->iStride, &roi) != PRIMITIVES_SUCCESS)
647 WLog_ERR(TAG,
"error when decoding lines");
651static void CALLBACK yuv444v1_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
655 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
657 BYTE* pYUVLumaData[3];
658 BYTE* pYUVChromaData[3];
659 const BYTE* src =
nullptr;
661 WINPR_UNUSED(instance);
665 roi.width = param->rect.right - param->rect.left;
666 roi.height = param->rect.bottom - param->rect.top;
667 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
668 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
670 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
671 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
672 param->rect.left / 2;
673 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
674 param->rect.left / 2;
676 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
677 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
678 param->rect.left / 2;
679 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
680 param->rect.left / 2;
681 if (prims->RGBToAVC444YUV(src, param->SrcFormat, param->nSrcStep, pYUVLumaData, param->iStride,
682 pYUVChromaData, param->iStride, &roi) != PRIMITIVES_SUCCESS)
684 WLog_ERR(TAG,
"error when decoding lines");
688static void CALLBACK yuv444v2_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
692 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
694 BYTE* pYUVLumaData[3];
695 BYTE* pYUVChromaData[3];
696 const BYTE* src =
nullptr;
698 WINPR_UNUSED(instance);
702 roi.width = param->rect.right - param->rect.left;
703 roi.height = param->rect.bottom - param->rect.top;
704 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
705 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
707 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
708 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
709 param->rect.left / 2;
710 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
711 param->rect.left / 2;
713 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
714 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
715 param->rect.left / 2;
716 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
717 param->rect.left / 2;
718 if (prims->RGBToAVC444YUVv2(src, param->SrcFormat, param->nSrcStep, pYUVLumaData,
719 param->iStride, pYUVChromaData, param->iStride,
720 &roi) != PRIMITIVES_SUCCESS)
722 WLog_ERR(TAG,
"error when decoding lines");
726static inline YUV_ENCODE_WORK_PARAM
727pool_encode_fill(
const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
728 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
729 const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
730 BYTE* WINPR_RESTRICT pYUVChromaData[])
732 YUV_ENCODE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
735 WINPR_ASSERT(context);
736 WINPR_ASSERT(pSrcData);
737 WINPR_ASSERT(iStride);
738 WINPR_ASSERT(pYUVLumaData);
740 current.context = context;
741 current.pSrcData = pSrcData;
742 current.SrcFormat = SrcFormat;
743 current.nSrcStep = nSrcStep;
744 current.pYUVLumaData[0] = pYUVLumaData[0];
745 current.pYUVLumaData[1] = pYUVLumaData[1];
746 current.pYUVLumaData[2] = pYUVLumaData[2];
749 current.pYUVChromaData[0] = pYUVChromaData[0];
750 current.pYUVChromaData[1] = pYUVChromaData[1];
751 current.pYUVChromaData[2] = pYUVChromaData[2];
753 current.iStride[0] = iStride[0];
754 current.iStride[1] = iStride[1];
755 current.iStride[2] = iStride[2];
757 current.rect = *rect;
762static uint32_t getSteps(uint32_t height, uint32_t step)
764 const uint32_t steps = (height + step / 2 + 1) / step;
770static BOOL pool_encode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
771 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
772 const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
773 BYTE* WINPR_RESTRICT pYUVChromaData[],
774 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
778 UINT32 waitCount = 0;
780 WINPR_ASSERT(context);
782 WINPR_ASSERT(pSrcData);
783 WINPR_ASSERT(iStride);
784 WINPR_ASSERT(regionRects || (numRegionRects == 0));
786 if (!context->encoder)
789 WLog_ERR(TAG,
"YUV context set up for decoding, can not encode with it, aborting");
793 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
795 for (UINT32 x = 0; x < numRegionRects; x++)
797 YUV_ENCODE_WORK_PARAM current =
798 pool_encode_fill(®ionRects[x], context, pSrcData, nSrcStep, SrcFormat, iStride,
799 pYUVLumaData, pYUVChromaData);
800 cb(
nullptr, ¤t,
nullptr);
806 for (UINT32 x = 0; x < numRegionRects; x++)
809 const UINT32 height = rect->bottom - rect->top;
810 const UINT32 steps = getSteps(height, context->heightStep);
815 for (UINT32 x = 0; x < numRegionRects; x++)
818 const UINT32 height = rect->bottom - rect->top;
819 const UINT32 steps = getSteps(height, context->heightStep);
821 for (UINT32 y = 0; y < steps; y++)
824 YUV_ENCODE_WORK_PARAM* current =
nullptr;
826 if (context->work_object_count <= waitCount)
828 free_objects(context->work_objects, context->work_object_count);
832 current = &context->work_enc_params[waitCount];
833 r.top += y * context->heightStep;
834 *current = pool_encode_fill(&r, context, pSrcData, nSrcStep, SrcFormat, iStride,
835 pYUVLumaData, pYUVChromaData);
836 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
844 free_objects(context->work_objects, context->work_object_count);
848BOOL yuv420_context_encode(YUV_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT pSrcData,
849 UINT32 nSrcStep, UINT32 SrcFormat,
const UINT32 iStride[3],
850 BYTE* WINPR_RESTRICT pYUVData[3],
851 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
853 if (!context || !pSrcData || !iStride || !pYUVData || !regionRects)
856 return pool_encode(context, yuv420_encode_work_callback, pSrcData, nSrcStep, SrcFormat, iStride,
857 pYUVData,
nullptr, regionRects, numRegionRects);
860BOOL yuv444_context_encode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE version,
861 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
862 const UINT32 iStride[3], BYTE* WINPR_RESTRICT pYUVLumaData[3],
863 BYTE* WINPR_RESTRICT pYUVChromaData[3],
864 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
866 PTP_WORK_CALLBACK cb =
nullptr;
870 cb = yuv444v1_encode_work_callback;
873 cb = yuv444v2_encode_work_callback;
879 return pool_encode(context, cb, pSrcData, nSrcStep, SrcFormat, iStride, pYUVLumaData,
880 pYUVChromaData, regionRects, numRegionRects);