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;
69static 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)
76 const BYTE* pYUVPoint[3];
78 WINPR_ASSERT(pYUVData);
79 WINPR_ASSERT(iStride);
81 WINPR_ASSERT(pDstData);
83 const INT32 width = rect->right - rect->left;
84 const INT32 height = rect->bottom - rect->top;
85 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
86 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
88 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
89 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top / 2 * iStride[1] + rect->left / 2;
90 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top / 2 * iStride[2] + rect->left / 2;
92 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
93 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
95 return (prims->YUV420ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
96 &roi) == PRIMITIVES_SUCCESS);
99static 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)
106 const BYTE* pYUVPoint[3];
108 WINPR_ASSERT(pYUVData);
109 WINPR_ASSERT(iStride);
111 WINPR_ASSERT(pDstData);
113 const INT32 width = rect->right - rect->left;
114 const INT32 height = rect->bottom - rect->top;
115 BYTE* pDstPoint = pDstData + 1ULL * rect->top * nDstStep +
116 1ULL * rect->left * FreeRDPGetBytesPerPixel(DstFormat);
118 pYUVPoint[0] = pYUVData[0] + 1ULL * rect->top * iStride[0] + rect->left;
119 pYUVPoint[1] = pYUVData[1] + 1ULL * rect->top * iStride[1] + rect->left;
120 pYUVPoint[2] = pYUVData[2] + 1ULL * rect->top * iStride[2] + rect->left;
122 roi.width = WINPR_ASSERTING_INT_CAST(uint32_t, width);
123 roi.height = WINPR_ASSERTING_INT_CAST(uint32_t, height);
125 return (prims->YUV444ToRGB_8u_P3AC4R(pYUVPoint, iStride, pDstPoint, nDstStep, DstFormat,
126 &roi) == PRIMITIVES_SUCCESS);
129static void CALLBACK yuv420_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
132 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
133 WINPR_UNUSED(instance);
137 if (!avc420_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
138 param->dest, param->DstFormat))
139 WLog_WARN(TAG,
"avc420_yuv_to_rgb failed");
142static void CALLBACK yuv444_process_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
145 YUV_PROCESS_WORK_PARAM* param = (YUV_PROCESS_WORK_PARAM*)context;
146 WINPR_UNUSED(instance);
150 if (!avc444_yuv_to_rgb(param->pYUVData, param->iStride, ¶m->rect, param->nDstStep,
151 param->dest, param->DstFormat))
152 WLog_WARN(TAG,
"avc444_yuv_to_rgb failed");
155BOOL yuv_context_reset(YUV_CONTEXT* WINPR_RESTRICT context, UINT32 width, UINT32 height)
158 WINPR_ASSERT(context);
160 context->width = width;
161 context->height = height;
163 context->heightStep = height;
165 if (context->useThreads)
167 context->heightStep = 16;
173 const size_t pw = (width + YUV_TILE_SIZE - width % YUV_TILE_SIZE) / 16;
174 const size_t ph = (height + YUV_TILE_SIZE - height % YUV_TILE_SIZE) / 16;
176 const size_t count = pw * ph;
178 context->work_object_count = 0;
179 if (context->encoder)
181 void* tmp = winpr_aligned_recalloc(context->work_enc_params, count,
182 sizeof(YUV_ENCODE_WORK_PARAM), 32);
185 memset(tmp, 0, count *
sizeof(YUV_ENCODE_WORK_PARAM));
187 context->work_enc_params = tmp;
191 void* tmp = winpr_aligned_recalloc(context->work_dec_params, count,
192 sizeof(YUV_PROCESS_WORK_PARAM), 32);
195 memset(tmp, 0, count *
sizeof(YUV_PROCESS_WORK_PARAM));
197 context->work_dec_params = tmp;
199 void* ctmp = winpr_aligned_recalloc(context->work_combined_params, count,
200 sizeof(YUV_COMBINE_WORK_PARAM), 32);
203 memset(ctmp, 0, count *
sizeof(YUV_COMBINE_WORK_PARAM));
205 context->work_combined_params = ctmp;
209 winpr_aligned_recalloc((
void*)context->work_objects, count,
sizeof(PTP_WORK), 32);
212 memset(wtmp, 0, count *
sizeof(PTP_WORK));
214 context->work_objects = (PTP_WORK*)wtmp;
215 context->work_object_count = WINPR_ASSERTING_INT_CAST(uint32_t, count);
222YUV_CONTEXT* yuv_context_new(BOOL encoder, UINT32 ThreadingFlags)
226 if (!primitives_get())
229 YUV_CONTEXT* ret = winpr_aligned_calloc(1,
sizeof(*ret), 32);
233 ret->encoder = encoder;
234 if (!(ThreadingFlags & THREADING_FLAGS_DISABLE_THREADS))
236 GetNativeSystemInfo(&sysInfos);
237 ret->useThreads = (sysInfos.dwNumberOfProcessors > 1);
243void yuv_context_free(YUV_CONTEXT* context)
247 if (context->useThreads)
249 winpr_aligned_free((
void*)context->work_objects);
250 winpr_aligned_free(context->work_combined_params);
251 winpr_aligned_free(context->work_enc_params);
252 winpr_aligned_free(context->work_dec_params);
254 winpr_aligned_free(context);
257static 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)
263 YUV_PROCESS_WORK_PARAM current = WINPR_C_ARRAY_INIT;
266 WINPR_ASSERT(context);
267 WINPR_ASSERT(pYUVData);
268 WINPR_ASSERT(iStride);
271 current.context = context;
272 current.DstFormat = DstFormat;
273 current.pYUVData[0] = pYUVData[0];
274 current.pYUVData[1] = pYUVData[1];
275 current.pYUVData[2] = pYUVData[2];
276 current.iStride[0] = iStride[0];
277 current.iStride[1] = iStride[1];
278 current.iStride[2] = iStride[2];
279 current.nDstStep = nDstStep;
281 current.rect = *rect;
285static BOOL submit_object(PTP_WORK* WINPR_RESTRICT work_object, PTP_WORK_CALLBACK cb,
286 const void* WINPR_RESTRICT param, YUV_CONTEXT* WINPR_RESTRICT context)
299 *work_object =
nullptr;
301 if (!param || !context)
304 *work_object = CreateThreadpoolWork(cb, cnv.pv,
nullptr);
308 SubmitThreadpoolWork(*work_object);
312static void free_objects(PTP_WORK* work_objects, UINT32 waitCount)
314 WINPR_ASSERT(work_objects || (waitCount == 0));
316 for (UINT32 i = 0; i < waitCount; i++)
318 PTP_WORK cur = work_objects[i];
319 work_objects[i] =
nullptr;
324 WaitForThreadpoolWorkCallbacks(cur, FALSE);
325 CloseThreadpoolWork(cur);
329static BOOL intersects(UINT32 pos,
const RECTANGLE_16* WINPR_RESTRICT regionRects,
330 UINT32 numRegionRects)
332 WINPR_ASSERT(regionRects || (numRegionRects == 0));
334 for (UINT32 x = pos + 1; x < numRegionRects; x++)
339 if (rectangles_intersects(what, rect))
341 WLog_WARN(TAG,
"YUV decoder: intersecting rectangles, aborting");
349static RECTANGLE_16 clamp(YUV_CONTEXT* WINPR_RESTRICT context,
350 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 srcHeight)
352 WINPR_ASSERT(context);
356 const UINT32 height = MIN(context->height, srcHeight);
358 c.top = WINPR_ASSERTING_INT_CAST(UINT16, height);
359 if (c.bottom > height)
360 c.bottom = WINPR_ASSERTING_INT_CAST(UINT16, height);
364static 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)
371 UINT32 waitCount = 0;
374 WINPR_ASSERT(context);
376 WINPR_ASSERT(pYUVData);
377 WINPR_ASSERT(iStride);
379 WINPR_ASSERT(regionRects || (numRegionRects == 0));
381 if (context->encoder)
383 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
387 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
389 for (UINT32 y = 0; y < numRegionRects; y++)
391 const RECTANGLE_16 rect = clamp(context, ®ionRects[y], yuvHeight);
392 YUV_PROCESS_WORK_PARAM current =
393 pool_decode_param(&rect, context, pYUVData, iStride, DstFormat, dest, nDstStep);
394 cb(
nullptr, ¤t,
nullptr);
400 for (UINT32 x = 0; x < numRegionRects; x++)
402 RECTANGLE_16 r = clamp(context, ®ionRects[x], yuvHeight);
404 if (intersects(x, regionRects, numRegionRects))
407 while (r.left < r.right)
410 y.right = MIN(r.right, r.left + YUV_TILE_SIZE);
412 while (y.top < y.bottom)
416 if (context->work_object_count <= waitCount)
418 free_objects(context->work_objects, context->work_object_count);
422 YUV_PROCESS_WORK_PARAM* cur = &context->work_dec_params[waitCount];
423 z.bottom = MIN(z.bottom, z.top + YUV_TILE_SIZE);
424 if (rectangle_is_empty(&z))
426 *cur = pool_decode_param(&z, context, pYUVData, iStride, DstFormat, dest, nDstStep);
427 if (!submit_object(&context->work_objects[waitCount], cb, cur, context))
430 y.top += YUV_TILE_SIZE;
433 r.left += YUV_TILE_SIZE;
438 free_objects(context->work_objects, context->work_object_count);
442static inline BOOL check_rect(
const YUV_CONTEXT* WINPR_RESTRICT yuv,
443 const RECTANGLE_16* WINPR_RESTRICT rect, UINT32 nDstWidth,
450 if ((rect->right > yuv->width) || (rect->left > yuv->width))
453 if ((rect->top > yuv->height) || (rect->bottom > yuv->height))
457 if ((rect->right > nDstWidth) || (rect->left > nDstWidth))
460 if ((rect->bottom > nDstHeight) || (rect->top > nDstHeight))
466static void CALLBACK yuv444_combine_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
469 YUV_COMBINE_WORK_PARAM* param = (YUV_COMBINE_WORK_PARAM*)context;
473 YUV_CONTEXT* yuv = param->context;
483 const UINT32 alignedWidth =
484 MIN(yuv->width + ((yuv->width % 32 != 0) ? 32 - yuv->width % 32 : 0), param->iStride[0]);
485 const UINT32 alignedHeight =
486 yuv->height + ((yuv->height % 16 != 0) ? 16 - yuv->height % 16 : 0);
488 WINPR_UNUSED(instance);
491 if (!check_rect(param->context, rect, yuv->width, yuv->height))
494 if (prims->YUV420CombineToYUV444(param->type, param->pYUVData, param->iStride, alignedWidth,
495 alignedHeight, param->pYUVDstData, param->iDstStride,
496 rect) != PRIMITIVES_SUCCESS)
497 WLog_WARN(TAG,
"YUV420CombineToYUV444 failed");
500static inline YUV_COMBINE_WORK_PARAM
501pool_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])
505 YUV_COMBINE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
508 WINPR_ASSERT(context);
509 WINPR_ASSERT(pYUVData);
510 WINPR_ASSERT(iStride);
511 WINPR_ASSERT(pYUVDstData);
512 WINPR_ASSERT(iDstStride);
514 current.context = context;
515 current.pYUVData[0] = pYUVData[0];
516 current.pYUVData[1] = pYUVData[1];
517 current.pYUVData[2] = pYUVData[2];
518 current.pYUVDstData[0] = pYUVDstData[0];
519 current.pYUVDstData[1] = pYUVDstData[1];
520 current.pYUVDstData[2] = pYUVDstData[2];
521 current.iStride[0] = iStride[0];
522 current.iStride[1] = iStride[1];
523 current.iStride[2] = iStride[2];
524 current.iDstStride[0] = iDstStride[0];
525 current.iDstStride[1] = iDstStride[1];
526 current.iDstStride[2] = iDstStride[2];
527 current.type = WINPR_ASSERTING_INT_CAST(avc444_frame_type, type);
528 current.rect = *rect;
532static 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)
538 UINT32 waitCount = 0;
539 PTP_WORK_CALLBACK cb = yuv444_combine_work_callback;
542 WINPR_ASSERT(context);
543 WINPR_ASSERT(pYUVData);
544 WINPR_ASSERT(iStride);
545 WINPR_ASSERT(pYUVDstData);
546 WINPR_ASSERT(iDstStride);
547 WINPR_ASSERT(regionRects || (numRegionRects == 0));
549 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
551 for (UINT32 y = 0; y < numRegionRects; y++)
553 YUV_COMBINE_WORK_PARAM current = pool_decode_rect_param(
554 ®ionRects[y], context, type, pYUVData, iStride, pYUVDstData, iDstStride);
555 cb(
nullptr, ¤t,
nullptr);
561 for (UINT32 x = 0; x < numRegionRects; x++)
563 YUV_COMBINE_WORK_PARAM* current =
nullptr;
565 if (context->work_object_count <= waitCount)
567 free_objects(context->work_objects, context->work_object_count);
570 current = &context->work_combined_params[waitCount];
571 *current = pool_decode_rect_param(®ionRects[x], context, type, pYUVData, iStride,
572 pYUVDstData, iDstStride);
574 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
581 free_objects(context->work_objects, context->work_object_count);
585BOOL 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)
592 const BYTE* pYUVCDstData[3];
594 WINPR_ASSERT(context);
595 WINPR_ASSERT(pYUVData);
596 WINPR_ASSERT(iStride);
597 WINPR_ASSERT(pYUVDstData);
598 WINPR_ASSERT(iDstStride);
600 WINPR_ASSERT(regionRects || (numRegionRects == 0));
602 if (context->encoder)
604 WLog_ERR(TAG,
"YUV context set up for encoding, can not decode with it, aborting");
607 if (!pool_decode_rect(context, type, pYUVData, iStride, pYUVDstData, iDstStride, regionRects,
611 pYUVCDstData[0] = pYUVDstData[0];
612 pYUVCDstData[1] = pYUVDstData[1];
613 pYUVCDstData[2] = pYUVDstData[2];
614 return pool_decode(context, yuv444_process_work_callback, pYUVCDstData, iDstStride,
615 srcYuvHeight, DstFormat, dest, nDstStep, regionRects, numRegionRects);
618BOOL 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)
624 return pool_decode(context, yuv420_process_work_callback, pYUVData, iStride, yuvHeight,
625 DstFormat, dest, nDstStep, regionRects, numRegionRects);
628static void CALLBACK yuv420_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
632 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
635 const BYTE* src =
nullptr;
637 WINPR_UNUSED(instance);
641 roi.width = param->rect.right - param->rect.left;
642 roi.height = param->rect.bottom - param->rect.top;
643 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
644 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
646 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
647 pYUVData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
648 param->rect.left / 2;
649 pYUVData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
650 param->rect.left / 2;
652 if (prims->RGBToYUV420_8u_P3AC4R(src, param->SrcFormat, param->nSrcStep, pYUVData,
653 param->iStride, &roi) != PRIMITIVES_SUCCESS)
655 WLog_ERR(TAG,
"error when decoding lines");
659static void CALLBACK yuv444v1_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
663 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
665 BYTE* pYUVLumaData[3];
666 BYTE* pYUVChromaData[3];
667 const BYTE* src =
nullptr;
669 WINPR_UNUSED(instance);
673 roi.width = param->rect.right - param->rect.left;
674 roi.height = param->rect.bottom - param->rect.top;
675 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
676 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
678 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
679 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
680 param->rect.left / 2;
681 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
682 param->rect.left / 2;
684 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
685 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
686 param->rect.left / 2;
687 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
688 param->rect.left / 2;
689 if (prims->RGBToAVC444YUV(src, param->SrcFormat, param->nSrcStep, pYUVLumaData, param->iStride,
690 pYUVChromaData, param->iStride, &roi) != PRIMITIVES_SUCCESS)
692 WLog_ERR(TAG,
"error when decoding lines");
696static void CALLBACK yuv444v2_encode_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context,
700 YUV_ENCODE_WORK_PARAM* param = (YUV_ENCODE_WORK_PARAM*)context;
702 BYTE* pYUVLumaData[3];
703 BYTE* pYUVChromaData[3];
704 const BYTE* src =
nullptr;
706 WINPR_UNUSED(instance);
710 roi.width = param->rect.right - param->rect.left;
711 roi.height = param->rect.bottom - param->rect.top;
712 src = param->pSrcData + 1ULL * param->nSrcStep * param->rect.top +
713 1ULL * param->rect.left * FreeRDPGetBytesPerPixel(param->SrcFormat);
715 param->pYUVLumaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
716 pYUVLumaData[1] = param->pYUVLumaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
717 param->rect.left / 2;
718 pYUVLumaData[2] = param->pYUVLumaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
719 param->rect.left / 2;
721 param->pYUVChromaData[0] + 1ULL * param->rect.top * param->iStride[0] + param->rect.left;
722 pYUVChromaData[1] = param->pYUVChromaData[1] + 1ULL * param->rect.top / 2 * param->iStride[1] +
723 param->rect.left / 2;
724 pYUVChromaData[2] = param->pYUVChromaData[2] + 1ULL * param->rect.top / 2 * param->iStride[2] +
725 param->rect.left / 2;
726 if (prims->RGBToAVC444YUVv2(src, param->SrcFormat, param->nSrcStep, pYUVLumaData,
727 param->iStride, pYUVChromaData, param->iStride,
728 &roi) != PRIMITIVES_SUCCESS)
730 WLog_ERR(TAG,
"error when decoding lines");
734static inline YUV_ENCODE_WORK_PARAM
735pool_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[])
740 YUV_ENCODE_WORK_PARAM current = WINPR_C_ARRAY_INIT;
743 WINPR_ASSERT(context);
744 WINPR_ASSERT(pSrcData);
745 WINPR_ASSERT(iStride);
746 WINPR_ASSERT(pYUVLumaData);
748 current.context = context;
749 current.pSrcData = pSrcData;
750 current.SrcFormat = SrcFormat;
751 current.nSrcStep = nSrcStep;
752 current.pYUVLumaData[0] = pYUVLumaData[0];
753 current.pYUVLumaData[1] = pYUVLumaData[1];
754 current.pYUVLumaData[2] = pYUVLumaData[2];
757 current.pYUVChromaData[0] = pYUVChromaData[0];
758 current.pYUVChromaData[1] = pYUVChromaData[1];
759 current.pYUVChromaData[2] = pYUVChromaData[2];
761 current.iStride[0] = iStride[0];
762 current.iStride[1] = iStride[1];
763 current.iStride[2] = iStride[2];
765 current.rect = *rect;
770static uint32_t getSteps(uint32_t height, uint32_t step)
772 const uint32_t steps = (height + step / 2 + 1) / step;
778static 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)
786 UINT32 waitCount = 0;
788 WINPR_ASSERT(context);
790 WINPR_ASSERT(pSrcData);
791 WINPR_ASSERT(iStride);
792 WINPR_ASSERT(regionRects || (numRegionRects == 0));
794 if (!context->encoder)
797 WLog_ERR(TAG,
"YUV context set up for decoding, can not encode with it, aborting");
801 if (!context->useThreads || (primitives_flags(prims) & PRIM_FLAGS_HAVE_EXTGPU))
803 for (UINT32 x = 0; x < numRegionRects; x++)
805 YUV_ENCODE_WORK_PARAM current =
806 pool_encode_fill(®ionRects[x], context, pSrcData, nSrcStep, SrcFormat, iStride,
807 pYUVLumaData, pYUVChromaData);
808 cb(
nullptr, ¤t,
nullptr);
814 for (UINT32 x = 0; x < numRegionRects; x++)
817 const UINT32 height = rect->bottom - rect->top;
818 const UINT32 steps = getSteps(height, context->heightStep);
820 for (UINT32 y = 0; y < steps; y++)
823 YUV_ENCODE_WORK_PARAM* current =
nullptr;
825 if (context->work_object_count <= waitCount)
827 free_objects(context->work_objects, context->work_object_count);
831 current = &context->work_enc_params[waitCount];
832 r.top += y * context->heightStep;
833 *current = pool_encode_fill(&r, context, pSrcData, nSrcStep, SrcFormat, iStride,
834 pYUVLumaData, pYUVChromaData);
835 if (!submit_object(&context->work_objects[waitCount], cb, current, context))
843 free_objects(context->work_objects, context->work_object_count);
847BOOL 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)
852 if (!context || !pSrcData || !iStride || !pYUVData || !regionRects)
855 return pool_encode(context, yuv420_encode_work_callback, pSrcData, nSrcStep, SrcFormat, iStride,
856 pYUVData,
nullptr, regionRects, numRegionRects);
859BOOL 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)
865 PTP_WORK_CALLBACK cb =
nullptr;
869 cb = yuv444v1_encode_work_callback;
872 cb = yuv444v2_encode_work_callback;
878 return pool_encode(context, cb, pSrcData, nSrcStep, SrcFormat, iStride, pYUVLumaData,
879 pYUVChromaData, regionRects, numRegionRects);