21#include <winpr/sysinfo.h>
23#include <freerdp/codec/av1.h>
24#include <freerdp/primitives.h>
25#include <freerdp/log.h>
26#include <freerdp/codec/region.h>
28#define TAG FREERDP_TAG("codec.av1")
30#if defined(WITH_LIBAOM)
33#include <aom/aom_decoder.h>
34#include <aom/aom_encoder.h>
36#include <aom/aom_image.h>
43#if defined(WITH_DAV1D)
44#include <dav1d/dav1d.h>
47#if defined(WITH_AV1_WEBCODECS)
48#include "av1_webcodecs.h"
51#if defined(WITH_AV1_VIDEOTOOLBOX)
52#include "av1_videotoolbox.h"
55#if defined(WITH_LIBAOM) || defined(WITH_DAV1D)
57#if defined(WITH_LIBYUV)
61struct S_FREERDP_AV1_CONTEXT
66#if defined(WITH_LIBAOM)
68 aom_codec_enc_cfg_t ecfg;
69 aom_codec_pts_t framecount;
70 aom_enc_frame_flags_t eflags;
71 aom_codec_flags_t flags;
72 aom_codec_iface_t* iface;
87#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
88 aom_codec_dec_cfg_t dcfg;
90#if defined(WITH_DAV1D)
93#if defined(WITH_AV1_WEBCODECS)
96#if defined(WITH_AV1_VIDEOTOOLBOX)
106#if defined(WITH_LIBAOM)
107static void av1_aom_destroy(FREERDP_AV1_CONTEXT* av1)
109 const aom_codec_err_t rc = aom_codec_destroy(&av1->ctx);
110 if (rc != AOM_CODEC_OK)
111 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_destroy: %s", aom_codec_err_to_string(rc));
117#define AV1_AOM_RT_SPEED 10
120#define AV1_AOM_MAX_THREADS 4
122static unsigned int av1_aom_threads(
void)
125 GetNativeSystemInfo(&sysInfo);
126 if (sysInfo.dwNumberOfProcessors < 1)
128 if (sysInfo.dwNumberOfProcessors > AV1_AOM_MAX_THREADS)
129 return AV1_AOM_MAX_THREADS;
130 return sysInfo.dwNumberOfProcessors;
135static BOOL av1_aom_apply_controls(FREERDP_AV1_CONTEXT* av1)
143 { AOME_SET_CPUUSED, (int)av1->speed,
"AOME_SET_CPUUSED" },
145 { AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN,
"AV1E_SET_TUNE_CONTENT" },
146 { AV1E_SET_ROW_MT, 1,
"AV1E_SET_ROW_MT" },
148 { AOME_SET_CQ_LEVEL, (int)av1->cqLevel,
"AOME_SET_CQ_LEVEL" },
151 const bool quality = (av1->ecfg.rc_end_usage == AOM_Q) || (av1->ecfg.rc_end_usage == AOM_CQ);
152 for (
size_t x = 0; x < ARRAYSIZE(controls); x++)
154 if ((controls[x].
id == AOME_SET_CQ_LEVEL) && (!quality || (av1->cqLevel == 0)))
156 const aom_codec_err_t rc = aom_codec_control(&av1->ctx, controls[x].id, controls[x].value);
157 if (rc != AOM_CODEC_OK)
159 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_control(%s, %d): %s", controls[x].name,
160 controls[x].value, aom_codec_err_to_string(rc));
170static BOOL av1_aom_damage(
const FREERDP_AV1_CONTEXT* av1,
const RECTANGLE_16* regionRect,
173 if (av1->fullFrameNext || !regionRect)
176 const UINT32 left = regionRect->left & ~1u;
177 const UINT32 top = regionRect->top & ~1u;
178 const UINT32 right = MIN(width, (regionRect->right + 1u) & ~1u);
179 const UINT32 bottom = MIN(height, (regionRect->bottom + 1u) & ~1u);
180 if ((left >= right) || (top >= bottom))
182 if ((left == 0) && (top == 0) && (right == width) && (bottom == height))
185 damage->left = (UINT16)left;
186 damage->top = (UINT16)top;
187 damage->right = (UINT16)right;
188 damage->bottom = (UINT16)bottom;
194BOOL freerdp_av1_context_set_option(FREERDP_AV1_CONTEXT* av1, FREERDP_AV1_CONTEXT_OPTION option,
197#if defined(WITH_LIBAOM)
200 case FREERDP_AV1_CONTEXT_OPTION_PROFILE:
204 av1->ecfg.g_profile = value;
207 case FREERDP_AV1_CONTEXT_OPTION_RATECONTROL:
212 case FREERDP_AV1_VBR:
213 case FREERDP_AV1_CBR:
218 WLog_Print(av1->log, WLOG_WARN,
219 "Unknown FREERDP_AV1_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
224 av1->ecfg.rc_end_usage = WINPR_ASSERTING_INT_CAST(
enum aom_rc_mode, value);
226 case FREERDP_AV1_CONTEXT_OPTION_BITRATE:
229 av1->ecfg.rc_target_bitrate = value;
231 case FREERDP_AV1_CONTEXT_OPTION_USAGETYPE:
234 av1->ecfg.g_usage = value;
236 case FREERDP_AV1_CONTEXT_OPTION_CQ_LEVEL:
237 if (!av1->encoder || (value > 63))
239 av1->cqLevel = value;
241 case FREERDP_AV1_CONTEXT_OPTION_THREADS:
242 if (!av1->encoder || (value > 64))
244 av1->ecfg.g_threads = (value > 0) ? value : av1_aom_threads();
246 case FREERDP_AV1_CONTEXT_OPTION_SPEED:
247 if (!av1->encoder || (value > 12))
252 WLog_Print(av1->log, WLOG_ERROR,
"Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32
"]",
257 if (!av1->initialized)
259 return freerdp_av1_context_reset(av1, av1->ecfg.g_w, av1->ecfg.g_h);
261 WLog_Print(av1->log, WLOG_WARN,
"AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
262 WINPR_UNUSED(option);
268UINT32 freerdp_av1_context_get_option(FREERDP_AV1_CONTEXT* av1, FREERDP_AV1_CONTEXT_OPTION option)
270#if defined(WITH_LIBAOM)
273 case FREERDP_AV1_CONTEXT_OPTION_PROFILE:
276 return av1->ecfg.g_profile;
277 case FREERDP_AV1_CONTEXT_OPTION_RATECONTROL:
280 return av1->ecfg.rc_end_usage;
281 case FREERDP_AV1_CONTEXT_OPTION_BITRATE:
284 return av1->ecfg.rc_target_bitrate;
285 case FREERDP_AV1_CONTEXT_OPTION_USAGETYPE:
288 return av1->ecfg.g_usage;
289 case FREERDP_AV1_CONTEXT_OPTION_CQ_LEVEL:
293 case FREERDP_AV1_CONTEXT_OPTION_THREADS:
296 return av1->ecfg.g_threads;
297 case FREERDP_AV1_CONTEXT_OPTION_SPEED:
302 WLog_Print(av1->log, WLOG_ERROR,
"Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32
"]",
307 WINPR_UNUSED(option);
312#if defined(WITH_LIBAOM)
315#define AV1_STATS_INTERVAL_NS (5ull * 1000ull * 1000ull * 1000ull)
317static void av1_encoder_stats(FREERDP_AV1_CONTEXT* av1, UINT64 startNS,
size_t bytes)
321 if (!WLog_IsLevelActive(av1->log, WLOG_DEBUG))
324 const UINT64 endNS = winpr_GetTickCount64NS();
325 const UINT64 encodeNS = endNS - startNS;
326 if (av1->stats.frames == 0)
327 av1->stats.startNS = startNS;
329 av1->stats.encodeNS += encodeNS;
330 av1->stats.bytes += bytes;
331 if (encodeNS > av1->stats.maxEncodeNS)
332 av1->stats.maxEncodeNS = encodeNS;
334 const UINT64 elapsedNS = endNS - av1->stats.startNS;
335 if (elapsedNS < AV1_STATS_INTERVAL_NS)
338 const double seconds = (double)elapsedNS / 1e9;
339 WLog_Print(av1->log, WLOG_DEBUG,
340 "AV1 %" PRIu32
"x%" PRIu32
" %s: %" PRIu32
" frames in %.1f s (%.1f fps), "
341 "encode avg %.1f ms max %.1f ms, %.0f kbit/s",
342 av1->yuvWidth, av1->yuvHeight, (av1->ecfg.g_profile == 1) ?
"I444" :
"I420",
343 av1->stats.frames, seconds, av1->stats.frames / seconds,
344 (
double)av1->stats.encodeNS / av1->stats.frames / 1e6,
345 (
double)av1->stats.maxEncodeNS / 1e6,
346 (
double)av1->stats.bytes * 8.0 / 1000.0 / seconds);
348 av1->stats.frames = 0;
349 av1->stats.encodeNS = 0;
350 av1->stats.maxEncodeNS = 0;
351 av1->stats.bytes = 0;
355INT32 freerdp_av1_compress(FREERDP_AV1_CONTEXT* av1,
const BYTE* pSrcData, DWORD SrcFormat,
356 UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
357 const RECTANGLE_16* regionRect, BYTE** ppDstData, UINT32* pDstSize,
360 *ppDstData =
nullptr;
365 WLog_Print(av1->log, WLOG_ERROR,
"av1->encoder: %d", av1->encoder);
369 if ((nSrcWidth == 0) || (nSrcWidth > UINT16_MAX) || (nSrcHeight == 0) ||
370 (nSrcHeight > UINT16_MAX))
372 WLog_Print(av1->log, WLOG_ERROR,
"%" PRIu32
"x%" PRIu32
" out of range, maximum %dx%d",
373 nSrcWidth, nSrcHeight, UINT16_MAX, UINT16_MAX);
377#if defined(WITH_LIBAOM)
378 const UINT64 startNS = winpr_GetTickCount64NS();
382 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
386 if (av1->yuvWidth != nSrcWidth)
388 WLog_Print(av1->log, WLOG_ERROR,
"av1->yuvWidth[%" PRIu32
"] != nSrcWidth[%" PRIu32
"]",
389 av1->yuvWidth, nSrcWidth);
393 if (av1->yuvHeight != nSrcHeight)
395 WLog_Print(av1->log, WLOG_ERROR,
"av1->yuvHeight[%" PRIu32
"] != nSrcHeight[%" PRIu32
"]",
396 av1->yuvHeight, nSrcHeight);
402 const BOOL partial = av1_aom_damage(av1, regionRect, nSrcWidth, nSrcHeight, &damage);
407 damage.right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
408 damage.bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
411 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
414 WLog_Print(av1->log, WLOG_ERROR,
"Unsupported source format %s",
415 FreeRDPGetColorFormatName(SrcFormat));
419 const prim_size_t roi = { .width = damage.right - damage.left,
420 .height = damage.bottom - damage.top };
421 const UINT32 chromaShift = (av1->ecfg.g_profile == 0) ? 1 : 0;
422 const size_t chromaX = damage.left >> chromaShift;
423 const size_t chromaY = damage.top >> chromaShift;
424 const BYTE* pSrc = &pSrcData[1ull * damage.top * nSrcStep + 1ull * damage.left * bpp];
425 BYTE* pDst[3] = { &av1->yuvdata[0][1ull * damage.top * av1->yuvStride[0] + damage.left],
426 &av1->yuvdata[1][chromaY * av1->yuvStride[1] + chromaX],
427 &av1->yuvdata[2][chromaY * av1->yuvStride[2] + chromaX] };
429 aom_image_t buffer = WINPR_C_ARRAY_INIT;
431 aom_image_t* img = &buffer;
433#if defined(WITH_LIBYUV)
437 if ((SrcFormat == PIXEL_FORMAT_BGRA32) || (SrcFormat == PIXEL_FORMAT_BGRX32))
439 switch (av1->ecfg.g_profile)
442 img->fmt = AOM_IMG_FMT_I420;
443 img->x_chroma_shift = 1;
444 img->y_chroma_shift = 1;
445 rec = ARGBToI420(pSrc, nSrcStep, pDst[0], av1->yuvStride[0], pDst[1],
446 av1->yuvStride[1], pDst[2],
447 WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]),
448 WINPR_ASSERTING_INT_CAST(
int, roi.width),
449 WINPR_ASSERTING_INT_CAST(
int, roi.height));
452 img->fmt = AOM_IMG_FMT_I444;
453 rec = ARGBToI444(pSrc, nSrcStep, pDst[0], av1->yuvStride[0], pDst[1],
454 av1->yuvStride[1], pDst[2],
455 WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]),
456 WINPR_ASSERTING_INT_CAST(
int, roi.width),
457 WINPR_ASSERTING_INT_CAST(
int, roi.height));
460 WLog_Print(av1->log, WLOG_ERROR,
"Unsupoorted AV1 profile %" PRIu32,
461 av1->ecfg.g_profile);
468 switch (av1->ecfg.g_profile)
471 img->fmt = AOM_IMG_FMT_I420;
472 img->x_chroma_shift = 1;
473 img->y_chroma_shift = 1;
474 rec = primitives->RGBToYUV420_8u_P3AC4R(pSrc, SrcFormat, nSrcStep, pDst,
475 av1->yuvStride, &roi);
478 img->fmt = AOM_IMG_FMT_I444;
480 primitives->
RGBToI444_8u(pSrc, SrcFormat, nSrcStep, pDst, av1->yuvStride, &roi);
483 WLog_Print(av1->log, WLOG_ERROR,
"Unsupoorted AV1 profile %" PRIu32,
484 av1->ecfg.g_profile);
490 WLog_Print(av1->log, WLOG_ERROR,
"RGB to YUV conversion failed: %d", rec);
491 av1->fullFrameNext =
true;
494 av1->fullFrameNext =
false;
496 img->d_w = img->r_w = img->w = nSrcWidth;
497 img->d_h = img->r_h = img->h = nSrcHeight;
498 img->stride[0] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[0]);
499 img->stride[1] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[1]);
500 img->stride[2] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]);
501 img->planes[0] = av1->yuvdata[0];
502 img->planes[1] = av1->yuvdata[1];
503 img->planes[2] = av1->yuvdata[2];
505 const aom_codec_err_t rc = aom_codec_encode(&av1->ctx, img, ++av1->framecount, 1, av1->eflags);
508 if (rc != AOM_CODEC_OK)
510 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_encode: %s", aom_codec_err_to_string(rc));
514 aom_codec_iter_t iter =
nullptr;
515 const aom_codec_cx_pkt_t* pkt =
nullptr;
516 while ((pkt = aom_codec_get_cx_data(&av1->ctx, &iter)) !=
nullptr)
518 if (pkt->kind != AOM_CODEC_CX_FRAME_PKT)
521 if (pkt->data.frame.sz > UINT32_MAX)
523 *ppDstData = pkt->data.frame.buf;
524 *pDstSize = WINPR_ASSERTING_INT_CAST(UINT32, pkt->data.frame.sz);
529 av1_encoder_stats(av1, startNS, *pDstSize);
534 rect->right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
535 rect->bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
537 return allocate_h264_metablock(10, rect, meta, 1);
539 WLog_Print(av1->log, WLOG_ERROR,
"AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
540 WINPR_UNUSED(pSrcData);
541 WINPR_UNUSED(SrcFormat);
542 WINPR_UNUSED(nSrcStep);
543 WINPR_UNUSED(nSrcWidth);
544 WINPR_UNUSED(nSrcHeight);
545 WINPR_UNUSED(regionRect);
551static BOOL isRectValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rect)
554 if (rect->left > width)
556 if (rect->right > width)
558 if (rect->left >= rect->right)
560 if (rect->top > height)
562 if (rect->bottom > height)
564 if (rect->top >= rect->bottom)
569static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height,
const RECTANGLE_16* rects,
572 WINPR_ASSERT(rects || (count == 0));
573 for (
size_t x = 0; x < count; x++)
576 if (!isRectValid(width, height, rect))
578 char buffer[64] = WINPR_C_ARRAY_INIT;
579 WLog_Print(log, WLOG_WARN,
580 "Rectangle %" PRIuz
" %s outside of bounding frame %" PRIu32
"x%" PRIu32, x,
581 rectangle_to_string(rect, buffer,
sizeof(buffer)), width, height);
588#if defined(WITH_DAV1D) || defined(WITH_AV1_VIDEOTOOLBOX)
590static INT32 av1_convert_planar(FREERDP_AV1_CONTEXT* av1, BOOL i444,
const BYTE*
const planes[3],
591 const UINT32 strides[3], UINT32 dw, UINT32 dh, BYTE* pDstData,
592 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
594 UINT32 numRegionRect)
599 const UINT32 nWidth = (dw < nDstWidth) ? dw : nDstWidth;
600 const UINT32 nHeight = (dh < nDstHeight) ? dh : nDstHeight;
601 const prim_size_t roi = { .width = nWidth, .height = nHeight };
603 if (!areRectsValid(av1->log, roi.width, roi.height, regionRects, numRegionRect))
607#if defined(WITH_LIBYUV)
611 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
613 const int stride0 = WINPR_ASSERTING_INT_CAST(
int, strides[0]);
614 const int stride1 = WINPR_ASSERTING_INT_CAST(
int, strides[1]);
615 const int stride2 = WINPR_ASSERTING_INT_CAST(
int, strides[2]);
616 const int iWidth = WINPR_ASSERTING_INT_CAST(
int, nWidth);
617 const int iHeight = WINPR_ASSERTING_INT_CAST(
int, nHeight);
618 const int iDstStep = WINPR_ASSERTING_INT_CAST(
int, nDstStep);
621 rec = J444ToARGB(planes[0], stride0, planes[1], stride1, planes[2], stride2, pDstData,
622 iDstStep, iWidth, iHeight);
624 rec = J420ToARGB(planes[0], stride0, planes[1], stride1, planes[2], stride2, pDstData,
625 iDstStep, iWidth, iHeight);
633 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
637 const BYTE* pSrc[] = { planes[0], planes[1], planes[2] };
639 rec = primitives->
I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat, &roi);
641 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep, DstFormat,
646 WLog_Print(av1->log, WLOG_ERROR,
"YUV to RGB conversion failed: %d", rec);
653#if defined(WITH_DAV1D)
654static void av1_dav1d_data_free(WINPR_ATTR_UNUSED
const uint8_t* buf,
655 WINPR_ATTR_UNUSED
void* cookie)
660static INT32 av1_dav1d_convert(FREERDP_AV1_CONTEXT* av1,
const Dav1dPicture* pic, BYTE* pDstData,
661 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
663 UINT32 numRegionRect)
667 WLog_Print(av1->log, WLOG_ERROR,
"dav1d picture bpc %d not supported", pic->p.bpc);
670 if ((pic->p.layout != DAV1D_PIXEL_LAYOUT_I420) && (pic->p.layout != DAV1D_PIXEL_LAYOUT_I444))
672 WLog_Print(av1->log, WLOG_ERROR,
"dav1d picture layout %u not supported", pic->p.layout);
676 const BYTE*
const planes[] = { pic->data[0], pic->data[1], pic->data[2] };
677 const UINT32 strides[] = { WINPR_ASSERTING_INT_CAST(UINT32, pic->stride[0]),
678 WINPR_ASSERTING_INT_CAST(UINT32, pic->stride[1]),
679 WINPR_ASSERTING_INT_CAST(UINT32, pic->stride[1]) };
680 return av1_convert_planar(av1, pic->p.layout == DAV1D_PIXEL_LAYOUT_I444, planes, strides,
681 WINPR_ASSERTING_INT_CAST(UINT32, pic->p.w),
682 WINPR_ASSERTING_INT_CAST(UINT32, pic->p.h), pDstData, DstFormat,
683 nDstStep, nDstWidth, nDstHeight, regionRects, numRegionRect);
687static INT32 av1_dav1d_drain_one(FREERDP_AV1_CONTEXT* av1, BYTE* pDstData, DWORD DstFormat,
688 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
691 Dav1dPicture pic = WINPR_C_ARRAY_INIT;
692 const int prc = dav1d_get_picture(av1->dav1d, &pic);
693 if (prc == DAV1D_ERR(EAGAIN))
697 WLog_Print(av1->log, WLOG_WARN,
"dav1d_get_picture: %d", prc);
701 const INT32 rc = av1_dav1d_convert(av1, &pic, pDstData, DstFormat, nDstStep, nDstWidth,
702 nDstHeight, regionRects, numRegionRect);
703 dav1d_picture_unref(&pic);
704 return (rc == TRUE) ? TRUE : rc;
708INT32 freerdp_av1_decompress(FREERDP_AV1_CONTEXT* av1,
const BYTE* pSrcData, UINT32 SrcSize,
709 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
711 UINT32 numRegionRect)
716 WLog_Print(av1->log, WLOG_ERROR,
"av1->encoder: %d", av1->encoder);
720 if (!areRectsValid(av1->log, nDstWidth, nDstHeight, regionRects, numRegionRect))
723#if defined(WITH_AV1_VIDEOTOOLBOX)
724 if (av1->vt && av1->vtStream)
727 const INT32 vrc = av1_vt_decode(av1->vt, pSrcData, SrcSize, &pic);
729 return av1_convert_planar(av1, pic.
i444, pic.planes, pic.strides, pic.width, pic.height,
730 pDstData, DstFormat, nDstStep, nDstWidth, nDstHeight,
731 regionRects, numRegionRect);
735 if (vrc == AV1_VT_INVALID)
739 av1->vtStream =
false;
740 if (vrc != AV1_VT_UNSUPPORTED)
744 WLog_Print(av1->log, WLOG_WARN,
745 "VideoToolbox AV1 decode failed (%" PRId32
"), software decoder from the "
746 "next sequence header",
754#if defined(WITH_AV1_WEBCODECS)
755 if (av1->webcodecs > 0)
757 const INT32 wrc = av1_wc_decode(av1->webcodecs, pSrcData, SrcSize, pDstData, DstFormat,
758 nDstStep, nDstWidth, nDstHeight);
762 if (wrc == AV1_WC_UNSUPPORTED)
763 WLog_Print(av1->log, WLOG_DEBUG,
"surface format needs dav1d instead of WebCodecs");
765 WLog_Print(av1->log, WLOG_WARN,
766 "WebCodecs AV1 decode failed (%" PRId32
"), using dav1d", wrc);
767 av1_wc_close(av1->webcodecs);
772#if defined(WITH_DAV1D)
773 Dav1dData data = WINPR_C_ARRAY_INIT;
774 const int wrc = dav1d_data_wrap(&data, pSrcData, SrcSize, av1_dav1d_data_free,
nullptr);
777 WLog_Print(av1->log, WLOG_WARN,
"dav1d_data_wrap: %d", wrc);
783 const int src = dav1d_send_data(av1->dav1d, &data);
784 if ((src < 0) && (src != DAV1D_ERR(EAGAIN)))
786 WLog_Print(av1->log, WLOG_WARN,
"dav1d_send_data: %d", src);
787 dav1d_data_unref(&data);
791 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
792 nDstHeight, regionRects, numRegionRect);
793 if ((rc != TRUE) && (rc != FALSE))
795 dav1d_data_unref(&data);
799 dav1d_data_unref(&data);
804 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
805 nDstHeight, regionRects, numRegionRect);
813#elif defined(WITH_LIBAOM)
814 const aom_codec_err_t rc = aom_codec_decode(&av1->ctx, pSrcData, SrcSize,
nullptr);
815 if (rc != AOM_CODEC_OK)
817 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_decode: %s", aom_codec_err_to_string(rc));
821 aom_image_t* img =
nullptr;
822 aom_codec_iter_t iter =
nullptr;
823 while ((img = aom_codec_get_frame(&av1->ctx, &iter)) !=
nullptr)
828 const UINT32 nWidth = (img->d_w < nDstWidth) ? img->d_w : nDstWidth;
829 const UINT32 nHeight = (img->d_h < nDstHeight) ? img->d_h : nDstHeight;
830 const prim_size_t roi = { .width = nWidth, .height = nHeight };
832 if (!areRectsValid(av1->log, roi.width, roi.height, regionRects, numRegionRect))
836#if defined(WITH_LIBYUV)
840 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
844 case AOM_IMG_FMT_I420:
845 rec = J420ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
846 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
849 case AOM_IMG_FMT_I444:
850 rec = J444ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
851 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
855 WLog_Print(av1->log, WLOG_ERROR,
"img->fmt %d not supported", img->fmt);
862 const BYTE* pSrc[] = { img->planes[0], img->planes[1], img->planes[2] };
863 const UINT32 strides[] = { img->stride[0], img->stride[1], img->stride[2] };
868 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
874 case AOM_IMG_FMT_I420:
875 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep,
878 case AOM_IMG_FMT_I444:
879 rec = primitives->
I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat,
883 WLog_Print(av1->log, WLOG_ERROR,
"img->fmt %d not supported", img->fmt);
889 WLog_Print(av1->log, WLOG_ERROR,
"RGB to YUV conversion failed: %d", rec);
898BOOL freerdp_av1_context_reset(FREERDP_AV1_CONTEXT* av1, UINT32 width, UINT32 height)
902#if defined(WITH_LIBAOM)
903 av1->ecfg.g_w = width;
904 av1->ecfg.g_h = height;
906 if (av1->initialized)
908 const aom_codec_err_t rc = aom_codec_destroy(&av1->ctx);
909 av1->initialized =
false;
910 if (rc != AOM_CODEC_OK)
912 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_destroy: %s",
913 aom_codec_err_to_string(rc));
918 const aom_codec_err_t rc =
919 aom_codec_enc_init(&av1->ctx, av1->iface, &av1->ecfg, av1->flags);
920 if (rc != AOM_CODEC_OK)
922 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_enc_init: %s", aom_codec_err_to_string(rc));
928 av1->initialized =
true;
929 av1->fullFrameNext =
true;
931 if (!av1_aom_apply_controls(av1))
934 WLog_Print(av1->log, WLOG_ERROR,
935 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
941#if defined(WITH_AV1_VIDEOTOOLBOX)
944 av1->vt = av1_vt_new(av1->log);
945 WLog_Print(av1->log, WLOG_INFO,
"AV1 decoder: %s",
946 av1->vt ?
"VideoToolbox (hardware), software as fallback"
947 :
"software (no VideoToolbox AV1 decoder)");
950 av1_vt_reset(av1->vt);
951 av1->vtStream = (av1->vt !=
nullptr);
953#if defined(WITH_AV1_WEBCODECS)
954 if (av1->webcodecs == 0)
956 av1->webcodecs = av1_wc_open();
957 WLog_Print(av1->log, WLOG_INFO,
"AV1 decoder: %s",
958 av1->webcodecs > 0 ?
"WebCodecs (browser), dav1d as fallback" :
"dav1d");
961#if defined(WITH_DAV1D)
962 if (av1->initialized)
964 dav1d_close(&av1->dav1d);
965 av1->initialized =
false;
968 Dav1dSettings settings = WINPR_C_ARRAY_INIT;
969 dav1d_default_settings(&settings);
972 settings.n_threads = 1;
973 settings.max_frame_delay = 1;
975 const int rc = dav1d_open(&av1->dav1d, &settings);
978 WLog_Print(av1->log, WLOG_WARN,
"dav1d_open: %d", rc);
981 av1->initialized =
true;
982#elif defined(WITH_LIBAOM)
984 av1->dcfg.h = height;
985 av1->dcfg.allow_lowbitdepth = 1;
986 const aom_codec_err_t rc =
987 aom_codec_dec_init(&av1->ctx, av1->iface, &av1->dcfg, av1->flags);
988 if (rc != AOM_CODEC_OK)
990 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_dec_init: %s", aom_codec_err_to_string(rc));
993 av1->initialized =
true;
995 WLog_Print(av1->log, WLOG_WARN,
996 "This build does not support AV1 decoding. Recompile with '-DWITH_DAV1D=ON' "
997 "or '-DWITH_AOM=ON'");
1002 av1->yuvWidth = width;
1003 av1->yuvStride[0] = width;
1005 const size_t pad = av1->yuvStride[0] % 16;
1007 av1->yuvStride[0] += 16 - pad;
1008 av1->yuvStride[0] *= 3;
1009 av1->yuvStride[1] = width;
1010 av1->yuvStride[2] = width;
1012 av1->yuvHeight = height;
1013 winpr_aligned_free(av1->yuvdata[0]);
1014 winpr_aligned_free(av1->yuvdata[1]);
1015 winpr_aligned_free(av1->yuvdata[2]);
1016 av1->yuvdata[0] = winpr_aligned_malloc(1ull * av1->yuvStride[0] * av1->yuvHeight, 64);
1017 av1->yuvdata[1] = winpr_aligned_malloc(1ull * av1->yuvStride[1] * av1->yuvHeight, 64);
1018 av1->yuvdata[2] = winpr_aligned_malloc(1ull * av1->yuvStride[2] * av1->yuvHeight, 64);
1019 return av1->yuvdata[0] !=
nullptr;
1022void freerdp_av1_context_free(FREERDP_AV1_CONTEXT* av1)
1027 if (av1->initialized)
1031#if defined(WITH_LIBAOM)
1032 av1_aom_destroy(av1);
1037#if defined(WITH_DAV1D)
1038 dav1d_close(&av1->dav1d);
1039#elif defined(WITH_LIBAOM)
1040 av1_aom_destroy(av1);
1044#if defined(WITH_AV1_WEBCODECS)
1045 av1_wc_close(av1->webcodecs);
1047#if defined(WITH_AV1_VIDEOTOOLBOX)
1048 av1_vt_free(av1->vt);
1050 winpr_aligned_free(av1->yuvdata[0]);
1051 winpr_aligned_free(av1->yuvdata[1]);
1052 winpr_aligned_free(av1->yuvdata[2]);
1056FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
1058 FREERDP_AV1_CONTEXT* ctx = calloc(1,
sizeof(FREERDP_AV1_CONTEXT));
1061 ctx->encoder = Compressor;
1062 ctx->log = WLog_Get(TAG);
1068#if defined(WITH_LIBAOM)
1069 ctx->iface = aom_codec_av1_cx();
1072 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_av1_cx() nullptr");
1076 const aom_codec_err_t rc =
1077 aom_codec_enc_config_default(ctx->iface, &ctx->ecfg, AOM_USAGE_REALTIME);
1078 if (rc != AOM_CODEC_OK)
1080 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_enc_config_default() %s",
1081 aom_codec_err_to_string(rc));
1084 ctx->ecfg.g_threads = av1_aom_threads();
1085 ctx->speed = AV1_AOM_RT_SPEED;
1087 WLog_Print(ctx->log, WLOG_ERROR,
1088 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
1092#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
1095 ctx->iface = aom_codec_av1_dx();
1098 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_av1_dx() nullptr");
1107 freerdp_av1_context_free(ctx);
1112struct S_FREERDP_AV1_CONTEXT
1118BOOL freerdp_av1_context_set_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1119 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option,
1120 WINPR_ATTR_UNUSED UINT32 value)
1125UINT32 freerdp_av1_context_get_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1126 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option)
1131INT32 freerdp_av1_compress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1132 WINPR_ATTR_UNUSED
const BYTE* pSrcData,
1133 WINPR_ATTR_UNUSED DWORD SrcFormat, WINPR_ATTR_UNUSED UINT32 nSrcStep,
1134 WINPR_ATTR_UNUSED UINT32 nSrcWidth, WINPR_ATTR_UNUSED UINT32 nSrcHeight,
1136 WINPR_ATTR_UNUSED BYTE** ppDstData, WINPR_ATTR_UNUSED UINT32* pDstSize,
1140 WLog_Print(av1->log, WLOG_ERROR,
1141 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1142 "'-DWITH_DAV1D=ON'");
1146INT32 freerdp_av1_decompress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1147 WINPR_ATTR_UNUSED
const BYTE* pSrcData,
1148 WINPR_ATTR_UNUSED UINT32 SrcSize, WINPR_ATTR_UNUSED BYTE* pDstData,
1149 WINPR_ATTR_UNUSED DWORD DstFormat, WINPR_ATTR_UNUSED UINT32 nDstStep,
1150 WINPR_ATTR_UNUSED UINT32 nDstWidth,
1151 WINPR_ATTR_UNUSED UINT32 nDstHeight,
1153 WINPR_ATTR_UNUSED UINT32 numRegionRect)
1156 WLog_Print(av1->log, WLOG_ERROR,
1157 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1158 "'-DWITH_DAV1D=ON'");
1162BOOL freerdp_av1_context_reset(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1163 WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height)
1166 WLog_Print(av1->log, WLOG_WARN,
1167 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1168 "'-DWITH_DAV1D=ON'");
1172void freerdp_av1_context_free(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1)
1177FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
1179 FREERDP_AV1_CONTEXT* ctx = calloc(1,
sizeof(FREERDP_AV1_CONTEXT));
1182 ctx->Compressor = Compressor;
1183 ctx->log = WLog_Get(TAG);
WINPR_ATTR_NODISCARD fn_RGBToYUV444_8u_P3AC4R_t RGBToI444_8u
WINPR_ATTR_NODISCARD fn_YUV444ToRGB_8u_P3AC4R_t I444ToRGB_8u
Convert a plain YUV444 picture (e.g. decoded AV1) to RGB. Unlike YUV444ToRGB_8u_P3AC4R this does not ...