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>
41#if defined(WITH_DAV1D)
42#include <dav1d/dav1d.h>
45#if defined(WITH_AV1_WEBCODECS)
46#include "av1_webcodecs.h"
49#if defined(WITH_LIBAOM) || defined(WITH_DAV1D)
51#if defined(WITH_LIBYUV)
55struct S_FREERDP_AV1_CONTEXT
60#if defined(WITH_LIBAOM)
62 aom_codec_enc_cfg_t ecfg;
63 aom_codec_pts_t framecount;
64 aom_enc_frame_flags_t eflags;
65 aom_codec_flags_t flags;
66 aom_codec_iface_t* iface;
79#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
80 aom_codec_dec_cfg_t dcfg;
82#if defined(WITH_DAV1D)
85#if defined(WITH_AV1_WEBCODECS)
94#if defined(WITH_LIBAOM)
95static void av1_aom_destroy(FREERDP_AV1_CONTEXT* av1)
97 const aom_codec_err_t rc = aom_codec_destroy(&av1->ctx);
98 if (rc != AOM_CODEC_OK)
99 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_destroy: %s", aom_codec_err_to_string(rc));
105#define AV1_AOM_RT_SPEED 10
108#define AV1_AOM_MAX_THREADS 4
110static unsigned int av1_aom_threads(
void)
113 GetNativeSystemInfo(&sysInfo);
114 if (sysInfo.dwNumberOfProcessors < 1)
116 if (sysInfo.dwNumberOfProcessors > AV1_AOM_MAX_THREADS)
117 return AV1_AOM_MAX_THREADS;
118 return sysInfo.dwNumberOfProcessors;
123static BOOL av1_aom_apply_controls(FREERDP_AV1_CONTEXT* av1)
131 { AOME_SET_CPUUSED, AV1_AOM_RT_SPEED,
"AOME_SET_CPUUSED" },
133 { AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN,
"AV1E_SET_TUNE_CONTENT" },
134 { AV1E_SET_ROW_MT, 1,
"AV1E_SET_ROW_MT" },
137 for (
size_t x = 0; x < ARRAYSIZE(controls); x++)
139 const aom_codec_err_t rc = aom_codec_control(&av1->ctx, controls[x].id, controls[x].value);
140 if (rc != AOM_CODEC_OK)
142 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_control(%s, %d): %s", controls[x].name,
143 controls[x].value, aom_codec_err_to_string(rc));
153static BOOL av1_aom_damage(
const FREERDP_AV1_CONTEXT* av1,
const RECTANGLE_16* regionRect,
156 if (av1->fullFrameNext || !regionRect)
159 const UINT32 left = regionRect->left & ~1u;
160 const UINT32 top = regionRect->top & ~1u;
161 const UINT32 right = MIN(width, (regionRect->right + 1u) & ~1u);
162 const UINT32 bottom = MIN(height, (regionRect->bottom + 1u) & ~1u);
163 if ((left >= right) || (top >= bottom))
165 if ((left == 0) && (top == 0) && (right == width) && (bottom == height))
168 damage->left = (UINT16)left;
169 damage->top = (UINT16)top;
170 damage->right = (UINT16)right;
171 damage->bottom = (UINT16)bottom;
176static BOOL allocate_h264_metablock(UINT32 QP,
RECTANGLE_16* rectangles,
180 if (!meta || (QP > UINT8_MAX))
186 meta->regionRects = rectangles;
190 if (count > UINT32_MAX)
195 if (!meta->quantQualityVals || !meta->regionRects)
197 meta->numRegionRects = (UINT32)count;
198 for (
size_t x = 0; x < count; x++)
205 cur->qualityVal = 100 - (QP & 0x3F);
212BOOL freerdp_av1_context_set_option(FREERDP_AV1_CONTEXT* av1, FREERDP_AV1_CONTEXT_OPTION option,
215#if defined(WITH_LIBAOM)
218 case FREERDP_AV1_CONTEXT_OPTION_PROFILE:
222 av1->ecfg.g_profile = value;
225 case FREERDP_AV1_CONTEXT_OPTION_RATECONTROL:
230 case FREERDP_AV1_VBR:
231 case FREERDP_AV1_CBR:
236 WLog_Print(av1->log, WLOG_WARN,
237 "Unknown FREERDP_AV1_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
242 av1->ecfg.rc_end_usage = WINPR_ASSERTING_INT_CAST(
enum aom_rc_mode, value);
244 case FREERDP_AV1_CONTEXT_OPTION_BITRATE:
247 av1->ecfg.rc_target_bitrate = value;
249 case FREERDP_AV1_CONTEXT_OPTION_USAGETYPE:
252 av1->ecfg.g_usage = value;
255 WLog_Print(av1->log, WLOG_ERROR,
"Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32
"]",
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;
290 WLog_Print(av1->log, WLOG_ERROR,
"Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32
"]",
295 WINPR_UNUSED(option);
300#if defined(WITH_LIBAOM)
303#define AV1_STATS_INTERVAL_NS (5ull * 1000ull * 1000ull * 1000ull)
305static void av1_encoder_stats(FREERDP_AV1_CONTEXT* av1, UINT64 startNS,
size_t bytes)
309 if (!WLog_IsLevelActive(av1->log, WLOG_DEBUG))
312 const UINT64 endNS = winpr_GetTickCount64NS();
313 const UINT64 encodeNS = endNS - startNS;
314 if (av1->stats.frames == 0)
315 av1->stats.startNS = startNS;
317 av1->stats.encodeNS += encodeNS;
318 av1->stats.bytes += bytes;
319 if (encodeNS > av1->stats.maxEncodeNS)
320 av1->stats.maxEncodeNS = encodeNS;
322 const UINT64 elapsedNS = endNS - av1->stats.startNS;
323 if (elapsedNS < AV1_STATS_INTERVAL_NS)
326 const double seconds = (double)elapsedNS / 1e9;
327 WLog_Print(av1->log, WLOG_DEBUG,
328 "AV1 %" PRIu32
"x%" PRIu32
" %s: %" PRIu32
" frames in %.1f s (%.1f fps), "
329 "encode avg %.1f ms max %.1f ms, %.0f kbit/s",
330 av1->yuvWidth, av1->yuvHeight, (av1->ecfg.g_profile == 1) ?
"I444" :
"I420",
331 av1->stats.frames, seconds, av1->stats.frames / seconds,
332 (double)av1->stats.encodeNS / av1->stats.frames / 1e6,
333 (double)av1->stats.maxEncodeNS / 1e6,
334 (double)av1->stats.bytes * 8.0 / 1000.0 / seconds);
336 av1->stats.frames = 0;
337 av1->stats.encodeNS = 0;
338 av1->stats.maxEncodeNS = 0;
339 av1->stats.bytes = 0;
343INT32 freerdp_av1_compress(FREERDP_AV1_CONTEXT* av1,
const BYTE* pSrcData, DWORD SrcFormat,
344 UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
345 const RECTANGLE_16* regionRect, BYTE** ppDstData, UINT32* pDstSize,
348 *ppDstData =
nullptr;
353 WLog_Print(av1->log, WLOG_ERROR,
"av1->encoder: %d", av1->encoder);
357 if ((nSrcWidth == 0) || (nSrcWidth > UINT16_MAX) || (nSrcHeight == 0) ||
358 (nSrcHeight > UINT16_MAX))
360 WLog_Print(av1->log, WLOG_ERROR,
"%" PRIu32
"x%" PRIu32
" out of range, maximum %dx%d",
361 nSrcWidth, nSrcHeight, UINT16_MAX, UINT16_MAX);
365#if defined(WITH_LIBAOM)
366 const UINT64 startNS = winpr_GetTickCount64NS();
370 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
374 if (av1->yuvWidth != nSrcWidth)
376 WLog_Print(av1->log, WLOG_ERROR,
"av1->yuvWidth[%" PRIu32
"] != nSrcWidth[%" PRIu32
"]",
377 av1->yuvWidth, nSrcWidth);
381 if (av1->yuvHeight != nSrcHeight)
383 WLog_Print(av1->log, WLOG_ERROR,
"av1->yuvHeight[%" PRIu32
"] != nSrcHeight[%" PRIu32
"]",
384 av1->yuvHeight, nSrcHeight);
390 const BOOL partial = av1_aom_damage(av1, regionRect, nSrcWidth, nSrcHeight, &damage);
395 damage.right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
396 damage.bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
399 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
402 WLog_Print(av1->log, WLOG_ERROR,
"Unsupported source format %s",
403 FreeRDPGetColorFormatName(SrcFormat));
407 const prim_size_t roi = { .width = damage.right - damage.left,
408 .height = damage.bottom - damage.top };
409 const UINT32 chromaShift = (av1->ecfg.g_profile == 0) ? 1 : 0;
410 const size_t chromaX = damage.left >> chromaShift;
411 const size_t chromaY = damage.top >> chromaShift;
412 const BYTE* pSrc = &pSrcData[1ull * damage.top * nSrcStep + 1ull * damage.left * bpp];
413 BYTE* pDst[3] = { &av1->yuvdata[0][1ull * damage.top * av1->yuvStride[0] + damage.left],
414 &av1->yuvdata[1][chromaY * av1->yuvStride[1] + chromaX],
415 &av1->yuvdata[2][chromaY * av1->yuvStride[2] + chromaX] };
417 aom_image_t buffer = WINPR_C_ARRAY_INIT;
419 aom_image_t* img = &buffer;
421#if defined(WITH_LIBYUV)
425 if ((SrcFormat == PIXEL_FORMAT_BGRA32) || (SrcFormat == PIXEL_FORMAT_BGRX32))
427 switch (av1->ecfg.g_profile)
430 img->fmt = AOM_IMG_FMT_I420;
431 img->x_chroma_shift = 1;
432 img->y_chroma_shift = 1;
433 rec = ARGBToI420(pSrc, nSrcStep, pDst[0], av1->yuvStride[0], pDst[1],
434 av1->yuvStride[1], pDst[2],
435 WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]),
436 WINPR_ASSERTING_INT_CAST(
int, roi.width),
437 WINPR_ASSERTING_INT_CAST(
int, roi.height));
440 img->fmt = AOM_IMG_FMT_I444;
441 rec = ARGBToI444(pSrc, nSrcStep, pDst[0], av1->yuvStride[0], pDst[1],
442 av1->yuvStride[1], pDst[2],
443 WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]),
444 WINPR_ASSERTING_INT_CAST(
int, roi.width),
445 WINPR_ASSERTING_INT_CAST(
int, roi.height));
448 WLog_Print(av1->log, WLOG_ERROR,
"Unsupoorted AV1 profile %" PRIu32,
449 av1->ecfg.g_profile);
456 switch (av1->ecfg.g_profile)
459 img->fmt = AOM_IMG_FMT_I420;
460 img->x_chroma_shift = 1;
461 img->y_chroma_shift = 1;
462 rec = primitives->RGBToYUV420_8u_P3AC4R(pSrc, SrcFormat, nSrcStep, pDst,
463 av1->yuvStride, &roi);
466 img->fmt = AOM_IMG_FMT_I444;
468 primitives->
RGBToI444_8u(pSrc, SrcFormat, nSrcStep, pDst, av1->yuvStride, &roi);
471 WLog_Print(av1->log, WLOG_ERROR,
"Unsupoorted AV1 profile %" PRIu32,
472 av1->ecfg.g_profile);
478 WLog_Print(av1->log, WLOG_ERROR,
"RGB to YUV conversion failed: %d", rec);
479 av1->fullFrameNext =
true;
482 av1->fullFrameNext =
false;
484 img->d_w = img->r_w = img->w = nSrcWidth;
485 img->d_h = img->r_h = img->h = nSrcHeight;
486 img->stride[0] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[0]);
487 img->stride[1] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[1]);
488 img->stride[2] = WINPR_ASSERTING_INT_CAST(
int, av1->yuvStride[2]);
489 img->planes[0] = av1->yuvdata[0];
490 img->planes[1] = av1->yuvdata[1];
491 img->planes[2] = av1->yuvdata[2];
493 const aom_codec_err_t rc = aom_codec_encode(&av1->ctx, img, ++av1->framecount, 1, av1->eflags);
496 if (rc != AOM_CODEC_OK)
498 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_encode: %s", aom_codec_err_to_string(rc));
502 aom_codec_iter_t iter =
nullptr;
503 const aom_codec_cx_pkt_t* pkt =
nullptr;
504 while ((pkt = aom_codec_get_cx_data(&av1->ctx, &iter)) !=
nullptr)
506 if (pkt->kind != AOM_CODEC_CX_FRAME_PKT)
509 if (pkt->data.frame.sz > UINT32_MAX)
511 *ppDstData = pkt->data.frame.buf;
512 *pDstSize = WINPR_ASSERTING_INT_CAST(UINT32, pkt->data.frame.sz);
517 av1_encoder_stats(av1, startNS, *pDstSize);
522 rect->right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
523 rect->bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
525 return allocate_h264_metablock(10, rect, meta, 1);
527 WLog_Print(av1->log, WLOG_ERROR,
"AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
528 WINPR_UNUSED(pSrcData);
529 WINPR_UNUSED(SrcFormat);
530 WINPR_UNUSED(nSrcStep);
531 WINPR_UNUSED(nSrcWidth);
532 WINPR_UNUSED(nSrcHeight);
533 WINPR_UNUSED(regionRect);
539static BOOL isRectValid(UINT32 width, UINT32 height,
const RECTANGLE_16* rect)
542 if (rect->left > width)
544 if (rect->right > width)
546 if (rect->left >= rect->right)
548 if (rect->top > height)
550 if (rect->bottom > height)
552 if (rect->top >= rect->bottom)
557static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height,
const RECTANGLE_16* rects,
560 WINPR_ASSERT(rects || (count == 0));
561 for (
size_t x = 0; x < count; x++)
564 if (!isRectValid(width, height, rect))
566 char buffer[64] = WINPR_C_ARRAY_INIT;
567 WLog_Print(log, WLOG_WARN,
568 "Rectangle %" PRIuz
" %s outside of bounding frame %" PRIu32
"x%" PRIu32, x,
569 rectangle_to_string(rect, buffer,
sizeof(buffer)), width, height);
576#if defined(WITH_DAV1D)
577static void av1_dav1d_data_free(WINPR_ATTR_UNUSED
const uint8_t* buf,
578 WINPR_ATTR_UNUSED
void* cookie)
583static INT32 av1_dav1d_convert(FREERDP_AV1_CONTEXT* av1,
const Dav1dPicture* pic, BYTE* pDstData,
584 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
586 UINT32 numRegionRect)
590 WLog_Print(av1->log, WLOG_ERROR,
"dav1d picture bpc %d not supported", pic->p.bpc);
597 const UINT32 dw = (UINT32)pic->p.w;
598 const UINT32 dh = (UINT32)pic->p.h;
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))
606 const UINT32 strides[] = { (UINT32)pic->stride[0], (UINT32)pic->stride[1],
607 (UINT32)pic->stride[1] };
610#if defined(WITH_LIBYUV)
614 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
616 const uint8_t* y = pic->data[0];
617 const uint8_t* u = pic->data[1];
618 const uint8_t* v = pic->data[2];
620 const int stride0 = WINPR_ASSERTING_INT_CAST(
int, strides[0]);
621 const int stride1 = WINPR_ASSERTING_INT_CAST(
int, strides[1]);
622 const int stride2 = WINPR_ASSERTING_INT_CAST(
int, strides[2]);
623 const int iWidth = WINPR_ASSERTING_INT_CAST(
int, nWidth);
624 const int iHeight = WINPR_ASSERTING_INT_CAST(
int, nHeight);
625 const int iDstStep = WINPR_ASSERTING_INT_CAST(
int, nDstStep);
627 switch (pic->p.layout)
629 case DAV1D_PIXEL_LAYOUT_I420:
630 rec = J420ToARGB(y, stride0, u, stride1, v, stride2, pDstData, iDstStep, iWidth,
633 case DAV1D_PIXEL_LAYOUT_I444:
634 rec = J444ToARGB(y, stride0, u, stride1, v, stride2, pDstData, iDstStep, iWidth,
638 WLog_Print(av1->log, WLOG_ERROR,
"dav1d picture layout %d not supported",
649 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
653 const BYTE* pSrc[] = { pic->data[0], pic->data[1], pic->data[2] };
654 switch (pic->p.layout)
656 case DAV1D_PIXEL_LAYOUT_I420:
657 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep,
660 case DAV1D_PIXEL_LAYOUT_I444:
661 rec = primitives->
I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat, &roi);
664 WLog_Print(av1->log, WLOG_ERROR,
"dav1d picture layout %u not supported",
671 WLog_Print(av1->log, WLOG_ERROR,
"RGB to YUV conversion failed: %d", rec);
678static INT32 av1_dav1d_drain_one(FREERDP_AV1_CONTEXT* av1, BYTE* pDstData, DWORD DstFormat,
679 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
682 Dav1dPicture pic = WINPR_C_ARRAY_INIT;
683 const int prc = dav1d_get_picture(av1->dav1d, &pic);
684 if (prc == DAV1D_ERR(EAGAIN))
688 WLog_Print(av1->log, WLOG_WARN,
"dav1d_get_picture: %d", prc);
692 const INT32 rc = av1_dav1d_convert(av1, &pic, pDstData, DstFormat, nDstStep, nDstWidth,
693 nDstHeight, regionRects, numRegionRect);
694 dav1d_picture_unref(&pic);
695 return (rc == TRUE) ? TRUE : rc;
699INT32 freerdp_av1_decompress(FREERDP_AV1_CONTEXT* av1,
const BYTE* pSrcData, UINT32 SrcSize,
700 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
702 UINT32 numRegionRect)
707 WLog_Print(av1->log, WLOG_ERROR,
"av1->encoder: %d", av1->encoder);
711 if (!areRectsValid(av1->log, nDstWidth, nDstHeight, regionRects, numRegionRect))
714#if defined(WITH_AV1_WEBCODECS)
715 if (av1->webcodecs > 0)
717 const INT32 wrc = av1_wc_decode(av1->webcodecs, pSrcData, SrcSize, pDstData, DstFormat,
718 nDstStep, nDstWidth, nDstHeight);
722 if (wrc == AV1_WC_UNSUPPORTED)
723 WLog_Print(av1->log, WLOG_DEBUG,
"surface format needs dav1d instead of WebCodecs");
725 WLog_Print(av1->log, WLOG_WARN,
726 "WebCodecs AV1 decode failed (%" PRId32
"), using dav1d", wrc);
727 av1_wc_close(av1->webcodecs);
732#if defined(WITH_DAV1D)
733 Dav1dData data = WINPR_C_ARRAY_INIT;
734 const int wrc = dav1d_data_wrap(&data, pSrcData, SrcSize, av1_dav1d_data_free,
nullptr);
737 WLog_Print(av1->log, WLOG_WARN,
"dav1d_data_wrap: %d", wrc);
743 const int src = dav1d_send_data(av1->dav1d, &data);
744 if ((src < 0) && (src != DAV1D_ERR(EAGAIN)))
746 WLog_Print(av1->log, WLOG_WARN,
"dav1d_send_data: %d", src);
747 dav1d_data_unref(&data);
751 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
752 nDstHeight, regionRects, numRegionRect);
753 if ((rc != TRUE) && (rc != FALSE))
755 dav1d_data_unref(&data);
763 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
764 nDstHeight, regionRects, numRegionRect);
772#elif defined(WITH_LIBAOM)
773 const aom_codec_err_t rc = aom_codec_decode(&av1->ctx, pSrcData, SrcSize,
nullptr);
774 if (rc != AOM_CODEC_OK)
776 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_decode: %s", aom_codec_err_to_string(rc));
780 aom_image_t* img =
nullptr;
781 aom_codec_iter_t iter =
nullptr;
782 while ((img = aom_codec_get_frame(&av1->ctx, &iter)) !=
nullptr)
787 const UINT32 nWidth = (img->d_w < nDstWidth) ? img->d_w : nDstWidth;
788 const UINT32 nHeight = (img->d_h < nDstHeight) ? img->d_h : nDstHeight;
789 const prim_size_t roi = { .width = nWidth, .height = nHeight };
791 if (!areRectsValid(av1->log, roi.width, roi.height, regionRects, numRegionRect))
795#if defined(WITH_LIBYUV)
799 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
803 case AOM_IMG_FMT_I420:
804 rec = J420ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
805 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
808 case AOM_IMG_FMT_I444:
809 rec = J444ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
810 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
814 WLog_Print(av1->log, WLOG_ERROR,
"img->fmt %d not supported", img->fmt);
821 const BYTE* pSrc[] = { img->planes[0], img->planes[1], img->planes[2] };
822 const UINT32 strides[] = { img->stride[0], img->stride[1], img->stride[2] };
827 WLog_Print(av1->log, WLOG_ERROR,
"primitives_get(): nullptr");
833 case AOM_IMG_FMT_I420:
834 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep,
837 case AOM_IMG_FMT_I444:
838 rec = primitives->
I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat,
842 WLog_Print(av1->log, WLOG_ERROR,
"img->fmt %d not supported", img->fmt);
848 WLog_Print(av1->log, WLOG_ERROR,
"RGB to YUV conversion failed: %d", rec);
857BOOL freerdp_av1_context_reset(FREERDP_AV1_CONTEXT* av1, UINT32 width, UINT32 height)
861#if defined(WITH_LIBAOM)
862 av1->ecfg.g_w = width;
863 av1->ecfg.g_h = height;
865 if (av1->initialized)
867 const aom_codec_err_t rc = aom_codec_destroy(&av1->ctx);
868 av1->initialized =
false;
869 if (rc != AOM_CODEC_OK)
871 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_destroy: %s",
872 aom_codec_err_to_string(rc));
877 const aom_codec_err_t rc =
878 aom_codec_enc_init(&av1->ctx, av1->iface, &av1->ecfg, av1->flags);
879 if (rc != AOM_CODEC_OK)
881 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_enc_init: %s", aom_codec_err_to_string(rc));
887 av1->initialized =
true;
888 av1->fullFrameNext =
true;
890 if (!av1_aom_apply_controls(av1))
893 WLog_Print(av1->log, WLOG_ERROR,
894 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
900#if defined(WITH_AV1_WEBCODECS)
901 if (av1->webcodecs == 0)
903 av1->webcodecs = av1_wc_open();
904 WLog_Print(av1->log, WLOG_INFO,
"AV1 decoder: %s",
905 av1->webcodecs > 0 ?
"WebCodecs (browser), dav1d as fallback" :
"dav1d");
908#if defined(WITH_DAV1D)
909 if (av1->initialized)
911 dav1d_close(&av1->dav1d);
912 av1->initialized =
false;
915 Dav1dSettings settings = WINPR_C_ARRAY_INIT;
916 dav1d_default_settings(&settings);
919 settings.n_threads = 1;
920 settings.max_frame_delay = 1;
922 const int rc = dav1d_open(&av1->dav1d, &settings);
925 WLog_Print(av1->log, WLOG_WARN,
"dav1d_open: %d", rc);
928 av1->initialized =
true;
929#elif defined(WITH_LIBAOM)
931 av1->dcfg.h = height;
932 av1->dcfg.allow_lowbitdepth = 1;
933 const aom_codec_err_t rc =
934 aom_codec_dec_init(&av1->ctx, av1->iface, &av1->dcfg, av1->flags);
935 if (rc != AOM_CODEC_OK)
937 WLog_Print(av1->log, WLOG_WARN,
"aom_codec_dec_init: %s", aom_codec_err_to_string(rc));
940 av1->initialized =
true;
942 WLog_Print(av1->log, WLOG_WARN,
943 "This build does not support AV1 decoding. Recompile with '-DWITH_DAV1D=ON' "
944 "or '-DWITH_AOM=ON'");
949 av1->yuvWidth = width;
950 av1->yuvStride[0] = width;
952 const size_t pad = av1->yuvStride[0] % 16;
954 av1->yuvStride[0] += 16 - pad;
955 av1->yuvStride[0] *= 3;
956 av1->yuvStride[1] = width;
957 av1->yuvStride[2] = width;
959 av1->yuvHeight = height;
960 winpr_aligned_free(av1->yuvdata[0]);
961 winpr_aligned_free(av1->yuvdata[1]);
962 winpr_aligned_free(av1->yuvdata[2]);
963 av1->yuvdata[0] = winpr_aligned_malloc(1ull * av1->yuvStride[0] * av1->yuvHeight, 64);
964 av1->yuvdata[1] = winpr_aligned_malloc(1ull * av1->yuvStride[1] * av1->yuvHeight, 64);
965 av1->yuvdata[2] = winpr_aligned_malloc(1ull * av1->yuvStride[2] * av1->yuvHeight, 64);
966 return av1->yuvdata[0] !=
nullptr;
969void freerdp_av1_context_free(FREERDP_AV1_CONTEXT* av1)
974 if (av1->initialized)
978#if defined(WITH_LIBAOM)
979 av1_aom_destroy(av1);
984#if defined(WITH_DAV1D)
985 dav1d_close(&av1->dav1d);
986#elif defined(WITH_LIBAOM)
987 av1_aom_destroy(av1);
991#if defined(WITH_AV1_WEBCODECS)
992 av1_wc_close(av1->webcodecs);
994 winpr_aligned_free(av1->yuvdata[0]);
995 winpr_aligned_free(av1->yuvdata[1]);
996 winpr_aligned_free(av1->yuvdata[2]);
1000FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
1002 FREERDP_AV1_CONTEXT* ctx = calloc(1,
sizeof(FREERDP_AV1_CONTEXT));
1005 ctx->encoder = Compressor;
1006 ctx->log = WLog_Get(TAG);
1012#if defined(WITH_LIBAOM)
1013 ctx->iface = aom_codec_av1_cx();
1016 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_av1_cx() nullptr");
1020 const aom_codec_err_t rc =
1021 aom_codec_enc_config_default(ctx->iface, &ctx->ecfg, AOM_USAGE_REALTIME);
1022 if (rc != AOM_CODEC_OK)
1024 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_enc_config_default() %s",
1025 aom_codec_err_to_string(rc));
1028 ctx->ecfg.g_threads = av1_aom_threads();
1030 WLog_Print(ctx->log, WLOG_ERROR,
1031 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
1035#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
1038 ctx->iface = aom_codec_av1_dx();
1041 WLog_Print(ctx->log, WLOG_ERROR,
"aom_codec_av1_dx() nullptr");
1050 freerdp_av1_context_free(ctx);
1055struct S_FREERDP_AV1_CONTEXT
1061BOOL freerdp_av1_context_set_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1062 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option,
1063 WINPR_ATTR_UNUSED UINT32 value)
1068UINT32 freerdp_av1_context_get_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1069 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option)
1074INT32 freerdp_av1_compress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1075 WINPR_ATTR_UNUSED
const BYTE* pSrcData,
1076 WINPR_ATTR_UNUSED DWORD SrcFormat, WINPR_ATTR_UNUSED UINT32 nSrcStep,
1077 WINPR_ATTR_UNUSED UINT32 nSrcWidth, WINPR_ATTR_UNUSED UINT32 nSrcHeight,
1079 WINPR_ATTR_UNUSED BYTE** ppDstData, WINPR_ATTR_UNUSED UINT32* pDstSize,
1083 WLog_Print(av1->log, WLOG_ERROR,
1084 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1085 "'-DWITH_DAV1D=ON'");
1089INT32 freerdp_av1_decompress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1090 WINPR_ATTR_UNUSED
const BYTE* pSrcData,
1091 WINPR_ATTR_UNUSED UINT32 SrcSize, WINPR_ATTR_UNUSED BYTE* pDstData,
1092 WINPR_ATTR_UNUSED DWORD DstFormat, WINPR_ATTR_UNUSED UINT32 nDstStep,
1093 WINPR_ATTR_UNUSED UINT32 nDstWidth,
1094 WINPR_ATTR_UNUSED UINT32 nDstHeight,
1096 WINPR_ATTR_UNUSED UINT32 numRegionRect)
1099 WLog_Print(av1->log, WLOG_ERROR,
1100 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1101 "'-DWITH_DAV1D=ON'");
1105BOOL freerdp_av1_context_reset(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1106 WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height)
1109 WLog_Print(av1->log, WLOG_WARN,
1110 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1111 "'-DWITH_DAV1D=ON'");
1115void freerdp_av1_context_free(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1)
1120FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
1122 FREERDP_AV1_CONTEXT* ctx = calloc(1,
sizeof(FREERDP_AV1_CONTEXT));
1125 ctx->Compressor = Compressor;
1126 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 ...