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")
14#if !defined(YUV_TILE_SIZE)
15#error "YUV_TILE_SIZE must be defined to the size of a single YUV decoder block"
21 const BYTE* pYUVData[3];
27} YUV_PROCESS_WORK_PARAM;
32 const BYTE* pYUVData[3];
37 avc444_frame_type type;
38} YUV_COMBINE_WORK_PARAM;
50 BYTE* pYUVLumaData[3];
51 BYTE* pYUVChromaData[3];
53} YUV_ENCODE_WORK_PARAM;
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;
70static 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)
77 const BYTE* pYUVPoint[3];
79 WINPR_ASSERT(pYUVData);
80 WINPR_ASSERT(iStride);
82 WINPR_ASSERT(pDstData);
84 const INT32 width = rect->right - rect->left;
85 const INT32 height = rect->bottom - rect->top;
86 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
87 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
89 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
90 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top / 2 * iStride[1] + rect->left / 2;
91 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top / 2 * iStride[2] + rect->left / 2;
93 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
94 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
96 return (prims->YUV420ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
97 &roi) == PRIMITIVES_SUCCESS);
101static 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)
108 const BYTE* pYUVPoint[3];
110 WINPR_ASSERT(pYUVData);
111 WINPR_ASSERT(iStride);
113 WINPR_ASSERT(pDstData);
115 const INT32 width = rect->right - rect->left;
116 const INT32 height = rect->bottom - rect->top;
117 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
118 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
120 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
121 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top * iStride[1] + rect->left;
122 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top * iStride[2] + rect->left;
124 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
125 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
127 return (prims->YUV444ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
128 &roi) == PRIMITIVES_SUCCESS);
131static void CALLBACK yuv420_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
134 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
135 WINPR_UNUSED(instance);
139 if (!avc420_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
140 param->dest, param->DstFormat))
141 WLog_WARN(TAG,
"avc420_yuv_to_rgb failed");
144static void CALLBACK yuv444_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
147 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
148 WINPR_UNUSED(instance);
152 if (!avc444_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
153 param->dest, param->DstFormat))
154 WLog_WARN(TAG,
"avc444_yuv_to_rgb failed");
157BOOL yuv_context_reset(YUV_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
160 WINPR_ASSERT(context);
162 context->width = width;
163 context->height = height;
165 context->heightStep = height;
167 if (context->useThreads)
169 context->heightStep = 16;
175 const size_t pw = (width + YUV_TILE_SIZE - width % YUV_TILE_SIZE) / 16;
176 const size_t ph = (height + YUV_TILE_SIZE - height % YUV_TILE_SIZE) / 16;
178 const size_t count = pw * ph;
180 context->work_object_count = 0;
181 if (context->encoder)
183 void* tmp = winpr_aligned_recalloc(context->work_enc_params, count,
184 sizeof(YUV_ENCODE_WORK_PARAM), 32);
187 memset(tmp, 0, count *
sizeof(YUV_ENCODE_WORK_PARAM));
189 context->work_enc_params = tmp;
193 void* tmp = winpr_aligned_recalloc(context->work_dec_params, count,
194 sizeof(YUV_PROCESS_WORK_PARAM), 32);
197 memset(tmp, 0, count *
sizeof(YUV_PROCESS_WORK_PARAM));
199 context->work_dec_params = tmp;
201 void* ctmp = winpr_aligned_recalloc(context->work_combined_params, count,
202 sizeof(YUV_COMBINE_WORK_PARAM), 32);
205 memset(ctmp, 0, count *
sizeof(YUV_COMBINE_WORK_PARAM));
207 context->work_combined_params = ctmp;
211 winpr_aligned_recalloc((
void*)context->work_objects, count,
sizeof(PTP_WORK), 32);
214 memset(wtmp, 0, count *
sizeof(PTP_WORK));
216 context->work_objects = (PTP_WORK*)wtmp;
217 context->work_object_count = WINPR_ASSERTING_INT_CAST(uint32_t, count);
224YUV_CONTEXT* yuv_context_new(BOOL encoder, UINT32 ThreadingFlags)
228 if (!primitives_get())
231 YUV_CONTEXT* ret = winpr_aligned_calloc(1,
sizeof(*ret), 32);
235 ret->encoder = encoder;
236 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
238 GetNativeSystemInfo(&sysInfos);
239 ret->useThreads = (sysInfos.dwNumberOfProcessors > 1);
245void yuv_context_free(YUV_CONTEXT* context)
249 if (context->useThreads)
251 winpr_aligned_free((
void*)context->work_objects);
252 winpr_aligned_free(context->work_combined_params);
253 winpr_aligned_free(context->work_enc_params);
254 winpr_aligned_free(context->work_dec_params);
256 winpr_aligned_free(context);
260static inline YUV_PROCESS_WORK_PARAM pool_decode_param(
const RECTANGLE_16* WINPR_RESTRICT rect,
261 YUV_CONTEXT* WINPR_RESTRICT context,
262 const BYTE* WINPR_RESTRICT pYUVData[3],
263 const UINT32 iStride[3], UINT32 DstFormat,
264 BYTE* WINPR_RESTRICT dest, UINT32 nDstStep)
266 YUV_PROCESS_WORK_PARAM current = WINPR_C_ARRAY_INIT;
269 WINPR_ASSERT(context);
270 WINPR_ASSERT(pYUVData);
271 WINPR_ASSERT(iStride);
274 current.context = context;
275 current.DstFormat = DstFormat;
276 current.pYUVData[0] = pYUVData[0];
277 current.pYUVData[1] = pYUVData[1];
278 current.pYUVData[2] = pYUVData[2];
279 current.iStride[0] = iStride[0];
280 current.iStride[1] = iStride[1];
281 current.iStride[2] = iStride[2];
282 current.nDstStep = nDstStep;
284 current.rect = *rect;
289static BOOL submit_object(PTP_WORK* WINPR_RESTRICT work_object, PTP_WORK_CALLBACK cb,
290 const void* WINPR_RESTRICT param, YUV_CONTEXT* WINPR_RESTRICT context)
303 *work_object =
nullptr;
305 if (!param || !context)
308 *work_object = CreateThreadpoolWork(cb, cnv.pv,
nullptr);
312 SubmitThreadpoolWork(*work_object);
316static void free_objects(PTP_WORK* work_objects, UINT32 waitCount)
318 WINPR_ASSERT(work_objects || (waitCount == 0));
320 for (UINT32 i = 0; i < waitCount; i++)
322 PTP_WORK cur = work_objects[i];
323 work_objects[i] =
nullptr;
328 WaitForThreadpoolWorkCallbacks(cur, FALSE);
329 CloseThreadpoolWork(cur);
334static BOOL intersects(UINT32 pos,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
335 UINT32 numRegionRects)
337 WINPR_ASSERT(regionRects || (numRegionRects == 0));
339 for (UINT32 x = pos + 1; x < numRegionRects; x++)
344 if (rectangles_intersects(what, rect))
346 WLog_WARN(TAG,
"YUV decoder: intersecting rectangles, aborting");
355static RECTANGLE_16 clamp(YUV_CONTEXT* WINPR_RESTRICT context,
356 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 srcHeight)
358 WINPR_ASSERT(context);
362 const UINT32 height = MIN(context->height, srcHeight);
364 c.top = WINPR_ASSERTING_INT_CAST(UINT16, height);
365 if (c.bottom > height)
366 c.bottom = WINPR_ASSERTING_INT_CAST(UINT16, height);
371static BOOL pool_decode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
372 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
373 UINT32 yuvHeight, UINT32 DstFormat, BYTE* WINPR_RESTRICT dest,
374 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
375 UINT32 numRegionRects)
378 UINT32 waitCount = 0;
381 WINPR_ASSERT(context);
383 WINPR_ASSERT(pYUVData);
384 WINPR_ASSERT(iStride);
386 WINPR_ASSERT(regionRects || (numRegionRects == 0));
388 if (context->encoder)
390 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
394 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
396 for (UINT32 y = 0; y < numRegionRects; y++)
398 const RECTANGLE_16 rect = clamp(context, ®ionRects[y], yuvHeight);
399 YUV_PROCESS_WORK_PARAM current =
400 pool_decode_param(&rect, context, pYUVData, iStride, DstFormat, dest, nDstStep);
401 cb(
nullptr, ¤t,
nullptr);
407 for (UINT32 x = 0; x < numRegionRects; x++)
409 RECTANGLE_16 r = clamp(context, ®ionRects[x], yuvHeight);
411 if (intersects(x, regionRects, numRegionRects))
414 while (r.left < r.right)
417 y.right = MIN(r.right, r.left + YUV_TILE_SIZE);
419 while (y.top < y.bottom)
423 if (context->work_object_count <= waitCount)
425 free_objects(context->work_objects, context->work_object_count);
429 YUV_PROCESS_WORK_PARAM* cur = &context->work_dec_params[waitCount];
430 z.bottom = MIN(z.bottom, z.top + YUV_TILE_SIZE);
431 if (rectangle_is_empty(&z))
433 *cur = pool_decode_param(&z, context, pYUVData, iStride, DstFormat, dest, nDstStep);
434 if (!submit_object(&context->work_objects[waitCount], cb, cur, context))
437 y.top += YUV_TILE_SIZE;
440 r.left += YUV_TILE_SIZE;
445 free_objects(context->work_objects, context->work_object_count);
450static inline BOOL check_rect(
const YUV_CONTEXT* WINPR_RESTRICT yuv,
451 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstWidth,
458 if ((rect->right > yuv->width) || (rect->left > yuv->width))
461 if ((rect->top > yuv->height) || (rect->bottom > yuv->height))
465 if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
468 if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
474static void CALLBACK yuv444_combine_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
477 YUV_COMBINE_WORK_PARAM* param = (YUV_COMBINE_WORK_PARAM*)context;
481 YUV_CONTEXT* yuv = param->context;
491 const UINT32 alignedWidth =
492 MIN(yuv->width + ((yuv->width % 32 != 0) ? 32 - yuv->width % 32 : 0), param->iStride[0]);
493 const UINT32 alignedHeight =
494 yuv->height + ((yuv->height % 16 != 0) ? 16 - yuv->height % 16 : 0);
496 WINPR_UNUSED(instance);
499 if (!check_rect(param->context, rect, yuv->width, yuv->height))
502 if (prims->YUV420CombineToYUV444(param->type, param->pYUVData, param->iStride, alignedWidth,
503 alignedHeight, param->pYUVDstData, param->iDstStride,
504 rect) != PRIMITIVES_SUCCESS)
505 WLog_WARN(TAG,
"YUV420CombineToYUV444 failed");
509static inline YUV_COMBINE_WORK_PARAM
510pool_decode_rect_param(
const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
511 BYTE type,
const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
512 BYTE* WINPR_RESTRICT pYUVDstData[3],
const UINT32 iDstStride[3])
514 YUV_COMBINE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
517 WINPR_ASSERT(context);
518 WINPR_ASSERT(pYUVData);
519 WINPR_ASSERT(iStride);
520 WINPR_ASSERT(pYUVDstData);
521 WINPR_ASSERT(iDstStride);
523 current.context = context;
524 current.pYUVData[0] = pYUVData[0];
525 current.pYUVData[1] = pYUVData[1];
526 current.pYUVData[2] = pYUVData[2];
527 current.pYUVDstData[0] = pYUVDstData[0];
528 current.pYUVDstData[1] = pYUVDstData[1];
529 current.pYUVDstData[2] = pYUVDstData[2];
530 current.iStride[0] = iStride[0];
531 current.iStride[1] = iStride[1];
532 current.iStride[2] = iStride[2];
533 current.iDstStride[0] = iDstStride[0];
534 current.iDstStride[1] = iDstStride[1];
535 current.iDstStride[2] = iDstStride[2];
536 current.type = WINPR_ASSERTING_INT_CAST(avc444_frame_type, type);
537 current.rect = *rect;
542static BOOL pool_decode_rect(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
543 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
544 BYTE* WINPR_RESTRICT pYUVDstData[3],
const UINT32 iDstStride[3],
545 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
548 UINT32 waitCount = 0;
549 PTP_WORK_CALLBACK cb = yuv444_combine_work_callback;
552 WINPR_ASSERT(context);
553 WINPR_ASSERT(pYUVData);
554 WINPR_ASSERT(iStride);
555 WINPR_ASSERT(pYUVDstData);
556 WINPR_ASSERT(iDstStride);
557 WINPR_ASSERT(regionRects || (numRegionRects == 0));
559 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
561 for (UINT32 y = 0; y < numRegionRects; y++)
563 YUV_COMBINE_WORK_PARAM current = pool_decode_rect_param(
564 ®ionRects[y], context, type, pYUVData, iStride, pYUVDstData, iDstStride);
565 cb(
nullptr, ¤t,
nullptr);
571 for (UINT32 x = 0; x < numRegionRects; x++)
573 YUV_COMBINE_WORK_PARAM* current =
nullptr;
575 if (context->work_object_count <= waitCount)
577 free_objects(context->work_objects, context->work_object_count);
580 current = &context->work_combined_params[waitCount];
581 *current = pool_decode_rect_param(®ionRects[x], context, type, pYUVData, iStride,
582 pYUVDstData, iDstStride);
584 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
591 free_objects(context->work_objects, context->work_object_count);
595BOOL yuv444_context_decode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE type,
596 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
597 UINT32 srcYuvHeight, BYTE* WINPR_RESTRICT pYUVDstData[3],
598 const UINT32 iDstStride[3], DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
599 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
600 UINT32 numRegionRects)
602 const BYTE* pYUVCDstData[3];
604 WINPR_ASSERT(context);
605 WINPR_ASSERT(pYUVData);
606 WINPR_ASSERT(iStride);
607 WINPR_ASSERT(pYUVDstData);
608 WINPR_ASSERT(iDstStride);
610 WINPR_ASSERT(regionRects || (numRegionRects == 0));
612 if (context->encoder)
614 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
617 if (!pool_decode_rect(context, type, pYUVData, iStride, pYUVDstData, iDstStride, regionRects,
621 pYUVCDstData[0] = pYUVDstData[0];
622 pYUVCDstData[1] = pYUVDstData[1];
623 pYUVCDstData[2] = pYUVDstData[2];
624 return pool_decode(context, yuv444_process_work_callback, pYUVCDstData, iDstStride,
625 srcYuvHeight, DstFormat, dest, nDstStep, regionRects, numRegionRects);
628BOOL yuv420_context_decode(YUV_CONTEXT* WINPR_RESTRICT context,
629 const BYTE* WINPR_RESTRICT pYUVData[3],
const UINT32 iStride[3],
630 UINT32 yuvHeight, DWORD DstFormat, BYTE* WINPR_RESTRICT dest,
631 UINT32 nDstStep,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
632 UINT32 numRegionRects)
634 return pool_decode(context, yuv420_process_work_callback, pYUVData, iStride, yuvHeight,
635 DstFormat, dest, nDstStep, regionRects, numRegionRects);
638static void CALLBACK yuv420_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
642 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
645 const BYTE* src =
nullptr;
647 WINPR_UNUSED(instance);
651 roi.width = param->rect.right - param->rect.left;
652 roi.height = param->rect.bottom - param->rect.top;
653 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
654 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
656 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
657 pYUVData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
658 param->rect.left / 2;
659 pYUVData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
660 param->rect.left / 2;
662 if (prims->RGBToYUV420_8u_P3AC4R(src, param->SrcFormat, param->nSrcStep, pYUVData,
663 param->iStride, &roi) != PRIMITIVES_SUCCESS)
665 WLog_ERR(TAG,
"error when decoding lines");
669static void CALLBACK yuv444v1_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
673 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
675 BYTE* pYUVLumaData[3];
676 BYTE* pYUVChromaData[3];
677 const BYTE* src =
nullptr;
679 WINPR_UNUSED(instance);
683 roi.width = param->rect.right - param->rect.left;
684 roi.height = param->rect.bottom - param->rect.top;
685 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
686 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
688 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
689 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
690 param->rect.left / 2;
691 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
692 param->rect.left / 2;
694 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
695 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
696 param->rect.left / 2;
697 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
698 param->rect.left / 2;
699 if (prims->RGBToAVC444YUV(src, param->SrcFormat, param->nSrcStep, pYUVLumaData, param->iStride,
700 pYUVChromaData, param->iStride, &roi) != PRIMITIVES_SUCCESS)
702 WLog_ERR(TAG,
"error when decoding lines");
706static void CALLBACK yuv444v2_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
710 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
712 BYTE* pYUVLumaData[3];
713 BYTE* pYUVChromaData[3];
714 const BYTE* src =
nullptr;
716 WINPR_UNUSED(instance);
720 roi.width = param->rect.right - param->rect.left;
721 roi.height = param->rect.bottom - param->rect.top;
722 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
723 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
725 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
726 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
727 param->rect.left / 2;
728 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
729 param->rect.left / 2;
731 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
732 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
733 param->rect.left / 2;
734 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
735 param->rect.left / 2;
736 if (prims->RGBToAVC444YUVv2(src, param->SrcFormat, param->nSrcStep, pYUVLumaData,
737 param->iStride, pYUVChromaData, param->iStride,
738 &roi) != PRIMITIVES_SUCCESS)
740 WLog_ERR(TAG,
"error when decoding lines");
745static inline YUV_ENCODE_WORK_PARAM
746pool_encode_fill(
const RECTANGLE_16* WINPR_RESTRICT rect, YUV_CONTEXT* WINPR_RESTRICT context,
747 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
748 const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
749 BYTE* WINPR_RESTRICT pYUVChromaData[])
751 YUV_ENCODE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
754 WINPR_ASSERT(context);
755 WINPR_ASSERT(pSrcData);
756 WINPR_ASSERT(iStride);
757 WINPR_ASSERT(pYUVLumaData);
759 current.context = context;
760 current.pSrcData = pSrcData;
761 current.SrcFormat = SrcFormat;
762 current.nSrcStep = nSrcStep;
763 current.pYUVLumaData[0] = pYUVLumaData[0];
764 current.pYUVLumaData[1] = pYUVLumaData[1];
765 current.pYUVLumaData[2] = pYUVLumaData[2];
768 current.pYUVChromaData[0] = pYUVChromaData[0];
769 current.pYUVChromaData[1] = pYUVChromaData[1];
770 current.pYUVChromaData[2] = pYUVChromaData[2];
772 current.iStride[0] = iStride[0];
773 current.iStride[1] = iStride[1];
774 current.iStride[2] = iStride[2];
776 current.rect = *rect;
782static uint32_t getSteps(uint32_t height, uint32_t step)
784 const uint32_t steps = (height + step / 2 + 1) / step;
791static BOOL pool_encode(YUV_CONTEXT* WINPR_RESTRICT context, PTP_WORK_CALLBACK cb,
792 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
793 const UINT32 iStride[], BYTE* WINPR_RESTRICT pYUVLumaData[],
794 BYTE* WINPR_RESTRICT pYUVChromaData[],
795 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
799 UINT32 waitCount = 0;
801 WINPR_ASSERT(context);
803 WINPR_ASSERT(pSrcData);
804 WINPR_ASSERT(iStride);
805 WINPR_ASSERT(regionRects || (numRegionRects == 0));
807 if (!context->encoder)
810 WLog_ERR(TAG,
"YUV context set up for decoding, can not encode with it, aborting");
814 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
816 for (UINT32 x = 0; x < numRegionRects; x++)
818 YUV_ENCODE_WORK_PARAM current =
819 pool_encode_fill(®ionRects[x], context, pSrcData, nSrcStep, SrcFormat, iStride,
820 pYUVLumaData, pYUVChromaData);
821 cb(
nullptr, ¤t,
nullptr);
827 for (UINT32 x = 0; x < numRegionRects; x++)
830 const UINT32 height = rect->bottom - rect->top;
831 const UINT32 steps = getSteps(height, context->heightStep);
833 for (UINT32 y = 0; y < steps; y++)
836 YUV_ENCODE_WORK_PARAM* current =
nullptr;
838 if (context->work_object_count <= waitCount)
840 free_objects(context->work_objects, context->work_object_count);
844 current = &context->work_enc_params[waitCount];
845 r.top += y * context->heightStep;
846 *current = pool_encode_fill(&r, context, pSrcData, nSrcStep, SrcFormat, iStride,
847 pYUVLumaData, pYUVChromaData);
848 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
856 free_objects(context->work_objects, context->work_object_count);
860BOOL yuv420_context_encode(YUV_CONTEXT* WINPR_RESTRICT context,
const BYTE* WINPR_RESTRICT pSrcData,
861 UINT32 nSrcStep, UINT32 SrcFormat,
const UINT32 iStride[3],
862 BYTE* WINPR_RESTRICT pYUVData[3],
863 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
865 if (!context || !pSrcData || !iStride || !pYUVData || !regionRects)
868 return pool_encode(context, yuv420_encode_work_callback, pSrcData, nSrcStep, SrcFormat, iStride,
869 pYUVData,
nullptr, regionRects, numRegionRects);
872BOOL yuv444_context_encode(YUV_CONTEXT* WINPR_RESTRICT context, BYTE version,
873 const BYTE* WINPR_RESTRICT pSrcData, UINT32 nSrcStep, UINT32 SrcFormat,
874 const UINT32 iStride[3], BYTE* WINPR_RESTRICT pYUVLumaData[3],
875 BYTE* WINPR_RESTRICT pYUVChromaData[3],
876 const RECTANGLE_16* WINPR_RESTRICT regionRects, UINT32 numRegionRects)
878 PTP_WORK_CALLBACK cb =
nullptr;
882 cb = yuv444v1_encode_work_callback;
885 cb = yuv444v2_encode_work_callback;
891 return pool_encode(context, cb, pSrcData, nSrcStep, SrcFormat, iStride, pYUVLumaData,
892 pYUVChromaData, regionRects, numRegionRects);