FreeRDP
Loading...
Searching...
No Matches
av1.c
1
21#include <winpr/sysinfo.h>
22
23#include <freerdp/codec/av1.h>
24#include <freerdp/primitives.h>
25#include <freerdp/log.h>
26#include <freerdp/codec/region.h>
27
28#define TAG FREERDP_TAG("codec.av1")
29
30#if defined(WITH_LIBAOM)
31#include <aom/aom.h>
32
33#include <aom/aom_decoder.h>
34#include <aom/aom_encoder.h>
35
36#include <aom/aom_image.h>
37#include <aom/aomcx.h>
38#include <aom/aomdx.h>
39#endif
40
41#if defined(WITH_DAV1D)
42#include <dav1d/dav1d.h>
43#endif
44
45#if defined(WITH_AV1_WEBCODECS)
46#include "av1_webcodecs.h"
47#endif
48
49#if defined(WITH_LIBAOM) || defined(WITH_DAV1D)
50
51#if defined(WITH_LIBYUV)
52#include <libyuv.h>
53#endif
54
55struct S_FREERDP_AV1_CONTEXT
56{
57 wLog* log;
58 bool encoder;
59 bool initialized;
60#if defined(WITH_LIBAOM)
61 aom_codec_ctx_t ctx;
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;
67 /* the next frame converts the whole surface to YUV (after a reset or a failure) */
68 bool fullFrameNext;
69 /* encoder statistics, see av1_encoder_stats() */
70 struct
71 {
72 UINT64 startNS;
73 UINT64 encodeNS;
74 UINT64 maxEncodeNS;
75 UINT64 bytes;
76 UINT32 frames;
77 } stats;
78#endif
79#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
80 aom_codec_dec_cfg_t dcfg;
81#endif
82#if defined(WITH_DAV1D)
83 Dav1dContext* dav1d;
84#endif
85#if defined(WITH_AV1_WEBCODECS)
86 int webcodecs; /* VideoDecoder handle, 0 if unused */
87#endif
88 BYTE* yuvdata[3];
89 UINT32 yuvWidth;
90 UINT32 yuvStride[3];
91 UINT32 yuvHeight;
92};
93
94#if defined(WITH_LIBAOM)
95static void av1_aom_destroy(FREERDP_AV1_CONTEXT* av1)
96{
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));
100}
101
102/* Speed preset for AOM_USAGE_REALTIME, 0 (slowest) to 12 (fastest). libaom defaults to 0 and
103 * raises it to 7 only for frames of 1080 lines or more, so smaller desktops got the slowest
104 * search. */
105#define AV1_AOM_RT_SPEED 10
106
107/* libaom encodes on one thread by default; more than 4 did not make 1080p faster. */
108#define AV1_AOM_MAX_THREADS 4
109
110static unsigned int av1_aom_threads(void)
111{
112 SYSTEM_INFO sysInfo = WINPR_C_ARRAY_INIT;
113 GetNativeSystemInfo(&sysInfo);
114 if (sysInfo.dwNumberOfProcessors < 1)
115 return 1;
116 if (sysInfo.dwNumberOfProcessors > AV1_AOM_MAX_THREADS)
117 return AV1_AOM_MAX_THREADS;
118 return sysInfo.dwNumberOfProcessors;
119}
120
121/* aom_codec_enc_init() starts from the defaults again, so apply these after every init. */
122WINPR_ATTR_NODISCARD
123static BOOL av1_aom_apply_controls(FREERDP_AV1_CONTEXT* av1)
124{
125 const struct
126 {
127 int id;
128 int value;
129 const char* name;
130 } controls[] = {
131 { AOME_SET_CPUUSED, AV1_AOM_RT_SPEED, "AOME_SET_CPUUSED" },
132 /* screen content tools (palette, intra block copy): sharp text at a lower bit rate */
133 { AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN, "AV1E_SET_TUNE_CONTENT" },
134 { AV1E_SET_ROW_MT, 1, "AV1E_SET_ROW_MT" },
135 };
136
137 for (size_t x = 0; x < ARRAYSIZE(controls); x++)
138 {
139 const aom_codec_err_t rc = aom_codec_control(&av1->ctx, controls[x].id, controls[x].value);
140 if (rc != AOM_CODEC_OK)
141 {
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));
144 return FALSE;
145 }
146 }
147 return TRUE;
148}
149
150/* Clip the damaged rectangle to the frame and round it out to even coordinates, so the
151 * I420 chroma samples it touches are whole. FALSE: encode the whole frame. */
152WINPR_ATTR_NODISCARD
153static BOOL av1_aom_damage(const FREERDP_AV1_CONTEXT* av1, const RECTANGLE_16* regionRect,
154 UINT32 width, UINT32 height, RECTANGLE_16* damage)
155{
156 if (av1->fullFrameNext || !regionRect)
157 return FALSE;
158
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))
164 return FALSE;
165 if ((left == 0) && (top == 0) && (right == width) && (bottom == height))
166 return FALSE;
167
168 damage->left = (UINT16)left;
169 damage->top = (UINT16)top;
170 damage->right = (UINT16)right;
171 damage->bottom = (UINT16)bottom;
172 return TRUE;
173}
174
175WINPR_ATTR_NODISCARD
176static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles,
177 RDPGFX_H264_METABLOCK* meta, size_t count)
178{
179 /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */
180 if (!meta || (QP > UINT8_MAX))
181 {
182 free(rectangles);
183 return FALSE;
184 }
185
186 meta->regionRects = rectangles;
187 if (count == 0)
188 return TRUE;
189
190 if (count > UINT32_MAX)
191 return FALSE;
192
193 meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY));
194
195 if (!meta->quantQualityVals || !meta->regionRects)
196 return FALSE;
197 meta->numRegionRects = (UINT32)count;
198 for (size_t x = 0; x < count; x++)
199 {
200 RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x];
201 cur->qp = (UINT8)QP;
202
203 /* qpVal bit 6 and 7 are flags, so mask them out here.
204 * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */
205 cur->qualityVal = 100 - (QP & 0x3F);
206 }
207 return TRUE;
208}
209
210#endif
211
212BOOL freerdp_av1_context_set_option(FREERDP_AV1_CONTEXT* av1, FREERDP_AV1_CONTEXT_OPTION option,
213 UINT32 value)
214{
215#if defined(WITH_LIBAOM)
216 switch (option)
217 {
218 case FREERDP_AV1_CONTEXT_OPTION_PROFILE:
219 if (!av1->encoder)
220 return FALSE;
221
222 av1->ecfg.g_profile = value;
223 break;
224
225 case FREERDP_AV1_CONTEXT_OPTION_RATECONTROL:
226 if (!av1->encoder)
227 return FALSE;
228 switch (value)
229 {
230 case FREERDP_AV1_VBR:
231 case FREERDP_AV1_CBR:
232 case FREERDP_AV1_CQ:
233 case FREERDP_AV1_Q:
234 break;
235 default:
236 WLog_Print(av1->log, WLOG_WARN,
237 "Unknown FREERDP_AV1_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
238 "]",
239 value);
240 return FALSE;
241 }
242 av1->ecfg.rc_end_usage = value;
243 break;
244 case FREERDP_AV1_CONTEXT_OPTION_BITRATE:
245 if (!av1->encoder)
246 return FALSE;
247 av1->ecfg.rc_target_bitrate = value;
248 break;
249 case FREERDP_AV1_CONTEXT_OPTION_USAGETYPE:
250 if (!av1->encoder)
251 return FALSE;
252 av1->ecfg.g_usage = value;
253 break;
254 default:
255 WLog_Print(av1->log, WLOG_ERROR, "Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32 "]",
256 option);
257 return TRUE;
258 }
259 return freerdp_av1_context_reset(av1, av1->ecfg.g_w, av1->ecfg.g_h);
260#else
261 WLog_Print(av1->log, WLOG_WARN, "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
262 WINPR_UNUSED(option);
263 WINPR_UNUSED(value);
264 return FALSE;
265#endif
266}
267
268UINT32 freerdp_av1_context_get_option(FREERDP_AV1_CONTEXT* av1, FREERDP_AV1_CONTEXT_OPTION option)
269{
270#if defined(WITH_LIBAOM)
271 switch (option)
272 {
273 case FREERDP_AV1_CONTEXT_OPTION_PROFILE:
274 if (!av1->encoder)
275 return 0;
276 return av1->ecfg.g_profile;
277 case FREERDP_AV1_CONTEXT_OPTION_RATECONTROL:
278 if (!av1->encoder)
279 return 0;
280 return av1->ecfg.rc_end_usage;
281 case FREERDP_AV1_CONTEXT_OPTION_BITRATE:
282 if (!av1->encoder)
283 return 0;
284 return av1->ecfg.rc_target_bitrate;
285 case FREERDP_AV1_CONTEXT_OPTION_USAGETYPE:
286 if (!av1->encoder)
287 return 0;
288 return av1->ecfg.g_usage;
289 default:
290 WLog_Print(av1->log, WLOG_ERROR, "Unknown FREERDP_AV1_CONTEXT_OPTION[0x%08" PRIx32 "]",
291 option);
292 return 0;
293 }
294#else
295 WINPR_UNUSED(option);
296 return 0;
297#endif
298}
299
300#if defined(WITH_LIBAOM)
301/* Encoder statistics, logged every few seconds at debug level to com.freerdp.codec.av1
302 * (e.g. with WLOG_LEVEL=DEBUG) */
303#define AV1_STATS_INTERVAL_NS (5ull * 1000ull * 1000ull * 1000ull)
304
305static void av1_encoder_stats(FREERDP_AV1_CONTEXT* av1, UINT64 startNS, size_t bytes)
306{
307 WINPR_ASSERT(av1);
308
309 if (!WLog_IsLevelActive(av1->log, WLOG_DEBUG))
310 return;
311
312 const UINT64 endNS = winpr_GetTickCount64NS();
313 const UINT64 encodeNS = endNS - startNS;
314 if (av1->stats.frames == 0)
315 av1->stats.startNS = startNS;
316 av1->stats.frames++;
317 av1->stats.encodeNS += encodeNS;
318 av1->stats.bytes += bytes;
319 if (encodeNS > av1->stats.maxEncodeNS)
320 av1->stats.maxEncodeNS = encodeNS;
321
322 const UINT64 elapsedNS = endNS - av1->stats.startNS;
323 if (elapsedNS < AV1_STATS_INTERVAL_NS)
324 return;
325
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);
335
336 av1->stats.frames = 0;
337 av1->stats.encodeNS = 0;
338 av1->stats.maxEncodeNS = 0;
339 av1->stats.bytes = 0;
340}
341#endif
342
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,
347{
348 *ppDstData = nullptr;
349 *pDstSize = 0;
350
351 if (!av1->encoder)
352 {
353 WLog_Print(av1->log, WLOG_ERROR, "av1->encoder: %d", av1->encoder);
354 return -1;
355 }
356
357#if defined(WITH_LIBAOM)
358 const UINT64 startNS = winpr_GetTickCount64NS();
359 primitives_t* primitives = primitives_get();
360 if (!primitives)
361 {
362 WLog_Print(av1->log, WLOG_ERROR, "primitives_get(): nullptr");
363 return -1;
364 }
365
366 if (av1->yuvWidth != nSrcWidth)
367 {
368 WLog_Print(av1->log, WLOG_ERROR, "av1->yuvWidth[%" PRIu32 "] != nSrcWidth[%" PRIu32 "]",
369 av1->yuvWidth, nSrcWidth);
370 return -1;
371 }
372
373 if (av1->yuvHeight != nSrcHeight)
374 {
375 WLog_Print(av1->log, WLOG_ERROR, "av1->yuvHeight[%" PRIu32 "] != nSrcHeight[%" PRIu32 "]",
376 av1->yuvHeight, nSrcHeight);
377 return -1;
378 }
379
380 /* Only the damaged part is converted: the YUV planes still hold the previous frame. */
381 RECTANGLE_16 damage = WINPR_C_ARRAY_INIT;
382 const BOOL partial = av1_aom_damage(av1, regionRect, nSrcWidth, nSrcHeight, &damage);
383 if (!partial)
384 {
385 damage.left = 0;
386 damage.top = 0;
387 damage.right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
388 damage.bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
389 }
390
391 const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
392 if (bpp == 0)
393 {
394 WLog_Print(av1->log, WLOG_ERROR, "Unsupported source format %s",
395 FreeRDPGetColorFormatName(SrcFormat));
396 return -1;
397 }
398
399 const prim_size_t roi = { .width = damage.right - damage.left,
400 .height = damage.bottom - damage.top };
401 const UINT32 chromaShift = (av1->ecfg.g_profile == 0) ? 1 : 0;
402 const size_t chromaX = damage.left >> chromaShift;
403 const size_t chromaY = damage.top >> chromaShift;
404 const BYTE* pSrc = &pSrcData[1ull * damage.top * nSrcStep + 1ull * damage.left * bpp];
405 BYTE* pDst[3] = { &av1->yuvdata[0][1ull * damage.top * av1->yuvStride[0] + damage.left],
406 &av1->yuvdata[1][chromaY * av1->yuvStride[1] + chromaX],
407 &av1->yuvdata[2][chromaY * av1->yuvStride[2] + chromaX] };
408
409 aom_image_t buffer = WINPR_C_ARRAY_INIT;
410
411 aom_image_t* img = &buffer;
412 pstatus_t rec = -1;
413#if defined(WITH_LIBYUV)
414 /* ARGBToI420()/ARGBToI444() always read pSrcData as packed BGRA8888 regardless of
415 * SrcFormat, so the fast path only applies for that layout; anything else must go
416 * through the format-aware primitives conversion below. */
417 if ((SrcFormat == PIXEL_FORMAT_BGRA32) || (SrcFormat == PIXEL_FORMAT_BGRX32))
418 {
419 switch (av1->ecfg.g_profile)
420 {
421 case 0:
422 img->fmt = AOM_IMG_FMT_I420;
423 img->x_chroma_shift = 1;
424 img->y_chroma_shift = 1;
425 rec = ARGBToI420(pSrc, nSrcStep, pDst[0], av1->yuvStride[0], pDst[1],
426 av1->yuvStride[1], pDst[2],
427 WINPR_ASSERTING_INT_CAST(int, av1->yuvStride[2]),
428 WINPR_ASSERTING_INT_CAST(int, roi.width),
429 WINPR_ASSERTING_INT_CAST(int, roi.height));
430 break;
431 case 1:
432 img->fmt = AOM_IMG_FMT_I444;
433 rec = ARGBToI444(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));
438 break;
439 default:
440 WLog_Print(av1->log, WLOG_ERROR, "Unsupoorted AV1 profile %" PRIu32,
441 av1->ecfg.g_profile);
442 return -1;
443 }
444 }
445 else
446#endif
447 {
448 switch (av1->ecfg.g_profile)
449 {
450 case 0:
451 img->fmt = AOM_IMG_FMT_I420;
452 img->x_chroma_shift = 1;
453 img->y_chroma_shift = 1;
454 rec = primitives->RGBToYUV420_8u_P3AC4R(pSrc, SrcFormat, nSrcStep, pDst,
455 av1->yuvStride, &roi);
456 break;
457 case 1:
458 img->fmt = AOM_IMG_FMT_I444;
459 rec =
460 primitives->RGBToI444_8u(pSrc, SrcFormat, nSrcStep, pDst, av1->yuvStride, &roi);
461 break;
462 default:
463 WLog_Print(av1->log, WLOG_ERROR, "Unsupoorted AV1 profile %" PRIu32,
464 av1->ecfg.g_profile);
465 return -1;
466 }
467 }
468 if (rec != 0)
469 {
470 WLog_Print(av1->log, WLOG_ERROR, "RGB to YUV conversion failed: %d", rec);
471 av1->fullFrameNext = true;
472 return -1;
473 }
474 av1->fullFrameNext = false;
475 img->bit_depth = 8;
476 img->d_w = img->r_w = img->w = nSrcWidth;
477 img->d_h = img->r_h = img->h = nSrcHeight;
478 img->stride[0] = WINPR_ASSERTING_INT_CAST(int, av1->yuvStride[0]);
479 img->stride[1] = WINPR_ASSERTING_INT_CAST(int, av1->yuvStride[1]);
480 img->stride[2] = WINPR_ASSERTING_INT_CAST(int, av1->yuvStride[2]);
481 img->planes[0] = av1->yuvdata[0];
482 img->planes[1] = av1->yuvdata[1];
483 img->planes[2] = av1->yuvdata[2];
484
485 const aom_codec_err_t rc = aom_codec_encode(&av1->ctx, img, ++av1->framecount, 1, av1->eflags);
486 aom_img_free(img);
487
488 if (rc != AOM_CODEC_OK)
489 {
490 WLog_Print(av1->log, WLOG_WARN, "aom_codec_encode: %s", aom_codec_err_to_string(rc));
491 return -1;
492 }
493
494 aom_codec_iter_t iter = nullptr;
495 const aom_codec_cx_pkt_t* pkt = nullptr;
496 while ((pkt = aom_codec_get_cx_data(&av1->ctx, &iter)) != nullptr)
497 {
498 if (pkt->kind != AOM_CODEC_CX_FRAME_PKT)
499 continue;
500
501 *ppDstData = pkt->data.frame.buf;
502 *pDstSize = pkt->data.frame.sz;
503 };
504 if (*pDstSize == 0)
505 return 0;
506
507 av1_encoder_stats(av1, startNS, *pDstSize);
508
509 RECTANGLE_16* rect = calloc(1, sizeof(RECTANGLE_16));
510 if (rect)
511 {
512 rect->right = nSrcWidth;
513 rect->bottom = nSrcHeight;
514 }
515 return allocate_h264_metablock(10, rect, meta, 1);
516#else
517 WLog_Print(av1->log, WLOG_ERROR, "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
518 WINPR_UNUSED(pSrcData);
519 WINPR_UNUSED(SrcFormat);
520 WINPR_UNUSED(nSrcStep);
521 WINPR_UNUSED(nSrcWidth);
522 WINPR_UNUSED(nSrcHeight);
523 WINPR_UNUSED(regionRect);
524 WINPR_UNUSED(meta);
525 return -1;
526#endif
527}
528
529static BOOL isRectValid(UINT32 width, UINT32 height, const RECTANGLE_16* rect)
530{
531 WINPR_ASSERT(rect);
532 if (rect->left > width)
533 return FALSE;
534 if (rect->right > width)
535 return FALSE;
536 if (rect->left >= rect->right)
537 return FALSE;
538 if (rect->top > height)
539 return FALSE;
540 if (rect->bottom > height)
541 return FALSE;
542 if (rect->top >= rect->bottom)
543 return FALSE;
544 return TRUE;
545}
546
547static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height, const RECTANGLE_16* rects,
548 UINT32 count)
549{
550 WINPR_ASSERT(rects || (count == 0));
551 for (size_t x = 0; x < count; x++)
552 {
553 const RECTANGLE_16* rect = &rects[x];
554 if (!isRectValid(width, height, rect))
555 {
556 char buffer[64] = WINPR_C_ARRAY_INIT;
557 WLog_Print(log, WLOG_WARN,
558 "Rectangle %" PRIuz " %s outside of bounding frame %" PRIu32 "x%" PRIu32, x,
559 rectangle_to_string(rect, buffer, sizeof(buffer)), width, height);
560 return FALSE;
561 }
562 }
563 return TRUE;
564}
565
566#if defined(WITH_DAV1D)
567static void av1_dav1d_data_free(WINPR_ATTR_UNUSED const uint8_t* buf,
568 WINPR_ATTR_UNUSED void* cookie)
569{
570 /* pSrcData is owned by the caller of freerdp_av1_decompress(), dav1d must not free it. */
571}
572
573static INT32 av1_dav1d_convert(FREERDP_AV1_CONTEXT* av1, const Dav1dPicture* pic, BYTE* pDstData,
574 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
575 UINT32 nDstHeight, const RECTANGLE_16* regionRects,
576 UINT32 numRegionRect)
577{
578 if (pic->p.bpc != 8)
579 {
580 WLog_Print(av1->log, WLOG_ERROR, "dav1d picture bpc %d not supported", pic->p.bpc);
581 return -1;
582 }
583
584 /* The decoded frame may be smaller than the destination surface, so
585 * bound the converted region to it. Otherwise the colour conversion
586 * reads past the decoded image planes. */
587 const UINT32 dw = (UINT32)pic->p.w;
588 const UINT32 dh = (UINT32)pic->p.h;
589 const UINT32 nWidth = (dw < nDstWidth) ? dw : nDstWidth;
590 const UINT32 nHeight = (dh < nDstHeight) ? dh : nDstHeight;
591 const prim_size_t roi = { .width = nWidth, .height = nHeight };
592
593 if (!areRectsValid(av1->log, roi.width, roi.height, regionRects, numRegionRect))
594 return -2;
595
596 const UINT32 strides[] = { (UINT32)pic->stride[0], (UINT32)pic->stride[1],
597 (UINT32)pic->stride[1] };
598
599 pstatus_t rec = -1;
600#if defined(WITH_LIBYUV)
601 /* J420ToARGB()/J444ToARGB() always write pDstData as packed BGRA8888 regardless of
602 * DstFormat, so the fast path only applies for that layout; anything else must go
603 * through the format-aware primitives conversion below. */
604 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
605 {
606 const uint8_t* y = pic->data[0];
607 const uint8_t* u = pic->data[1];
608 const uint8_t* v = pic->data[2];
609
610 const int stride0 = WINPR_ASSERTING_INT_CAST(int, strides[0]);
611 const int stride1 = WINPR_ASSERTING_INT_CAST(int, strides[1]);
612 const int stride2 = WINPR_ASSERTING_INT_CAST(int, strides[2]);
613 const int iWidth = WINPR_ASSERTING_INT_CAST(int, nWidth);
614 const int iHeight = WINPR_ASSERTING_INT_CAST(int, nHeight);
615 const int iDstStep = WINPR_ASSERTING_INT_CAST(int, nDstStep);
616
617 switch (pic->p.layout)
618 {
619 case DAV1D_PIXEL_LAYOUT_I420:
620 rec = J420ToARGB(y, stride0, u, stride1, v, stride2, pDstData, iDstStep, iWidth,
621 iHeight);
622 break;
623 case DAV1D_PIXEL_LAYOUT_I444:
624 rec = J444ToARGB(y, stride0, u, stride1, v, stride2, pDstData, iDstStep, iWidth,
625 iHeight);
626 break;
627 default:
628 WLog_Print(av1->log, WLOG_ERROR, "dav1d picture layout %d not supported",
629 pic->p.layout);
630 return -1;
631 }
632 }
633 else
634#endif
635 {
636 primitives_t* primitives = primitives_get();
637 if (!primitives)
638 {
639 WLog_Print(av1->log, WLOG_ERROR, "primitives_get(): nullptr");
640 return -1;
641 }
642
643 const BYTE* pSrc[] = { pic->data[0], pic->data[1], pic->data[2] };
644 switch (pic->p.layout)
645 {
646 case DAV1D_PIXEL_LAYOUT_I420:
647 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep,
648 DstFormat, &roi);
649 break;
650 case DAV1D_PIXEL_LAYOUT_I444:
651 rec = primitives->I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat, &roi);
652 break;
653 default:
654 WLog_Print(av1->log, WLOG_ERROR, "dav1d picture layout %d not supported",
655 pic->p.layout);
656 return -1;
657 }
658 }
659 if (rec != 0)
660 {
661 WLog_Print(av1->log, WLOG_ERROR, "RGB to YUV conversion failed: %d", rec);
662 return -1;
663 }
664 return TRUE;
665}
666
667/* Return: TRUE if a picture was converted, FALSE if none was ready (EAGAIN), <0 on error. */
668static INT32 av1_dav1d_drain_one(FREERDP_AV1_CONTEXT* av1, BYTE* pDstData, DWORD DstFormat,
669 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
670 const RECTANGLE_16* regionRects, UINT32 numRegionRect)
671{
672 Dav1dPicture pic = WINPR_C_ARRAY_INIT;
673 const int prc = dav1d_get_picture(av1->dav1d, &pic);
674 if (prc == DAV1D_ERR(EAGAIN))
675 return FALSE;
676 if (prc < 0)
677 {
678 WLog_Print(av1->log, WLOG_WARN, "dav1d_get_picture: %d", prc);
679 return -1;
680 }
681
682 const INT32 rc = av1_dav1d_convert(av1, &pic, pDstData, DstFormat, nDstStep, nDstWidth,
683 nDstHeight, regionRects, numRegionRect);
684 dav1d_picture_unref(&pic);
685 return (rc == TRUE) ? TRUE : rc;
686}
687#endif
688
689INT32 freerdp_av1_decompress(FREERDP_AV1_CONTEXT* av1, const BYTE* pSrcData, UINT32 SrcSize,
690 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
691 UINT32 nDstHeight, const RECTANGLE_16* regionRects,
692 UINT32 numRegionRect)
693{
694 WINPR_ASSERT(av1);
695 if (av1->encoder)
696 {
697 WLog_Print(av1->log, WLOG_ERROR, "av1->encoder: %d", av1->encoder);
698 return -1;
699 }
700
701 if (!areRectsValid(av1->log, nDstWidth, nDstHeight, regionRects, numRegionRect))
702 return -2;
703
704#if defined(WITH_AV1_WEBCODECS)
705 if (av1->webcodecs > 0)
706 {
707 const INT32 wrc = av1_wc_decode(av1->webcodecs, pSrcData, SrcSize, pDstData, DstFormat,
708 nDstStep, nDstWidth, nDstHeight);
709 if (wrc >= 0)
710 return TRUE;
711 /* fall back to the software decoder for the rest of the session */
712 if (wrc == AV1_WC_UNSUPPORTED)
713 WLog_Print(av1->log, WLOG_DEBUG, "surface format needs dav1d instead of WebCodecs");
714 else
715 WLog_Print(av1->log, WLOG_WARN,
716 "WebCodecs AV1 decode failed (%" PRId32 "), using dav1d", wrc);
717 av1_wc_close(av1->webcodecs);
718 av1->webcodecs = 0;
719 }
720#endif
721
722#if defined(WITH_DAV1D)
723 Dav1dData data = WINPR_C_ARRAY_INIT;
724 const int wrc = dav1d_data_wrap(&data, pSrcData, SrcSize, av1_dav1d_data_free, nullptr);
725 if (wrc < 0)
726 {
727 WLog_Print(av1->log, WLOG_WARN, "dav1d_data_wrap: %d", wrc);
728 return -1;
729 }
730
731 while (data.sz > 0)
732 {
733 const int src = dav1d_send_data(av1->dav1d, &data);
734 if ((src < 0) && (src != DAV1D_ERR(EAGAIN)))
735 {
736 WLog_Print(av1->log, WLOG_WARN, "dav1d_send_data: %d", src);
737 dav1d_data_unref(&data);
738 return -1;
739 }
740
741 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
742 nDstHeight, regionRects, numRegionRect);
743 if ((rc != TRUE) && (rc != FALSE))
744 {
745 dav1d_data_unref(&data);
746 return rc;
747 }
748 }
749
750 /* Drain any picture the decoder buffered internally before it consumed the last input. */
751 for (;;)
752 {
753 const INT32 rc = av1_dav1d_drain_one(av1, pDstData, DstFormat, nDstStep, nDstWidth,
754 nDstHeight, regionRects, numRegionRect);
755 if (rc == FALSE)
756 break;
757 if (rc != TRUE)
758 return rc;
759 }
760
761 return TRUE;
762#elif defined(WITH_LIBAOM)
763 const aom_codec_err_t rc = aom_codec_decode(&av1->ctx, pSrcData, SrcSize, nullptr);
764 if (rc != AOM_CODEC_OK)
765 {
766 WLog_Print(av1->log, WLOG_WARN, "aom_codec_decode: %s", aom_codec_err_to_string(rc));
767 return -1;
768 }
769
770 aom_image_t* img = nullptr;
771 aom_codec_iter_t iter = nullptr;
772 while ((img = aom_codec_get_frame(&av1->ctx, &iter)) != nullptr)
773 {
774 /* The decoded frame may be smaller than the destination surface, so
775 * bound the converted region to it. Otherwise the colour conversion
776 * reads past the decoded image planes. */
777 const UINT32 nWidth = (img->d_w < nDstWidth) ? img->d_w : nDstWidth;
778 const UINT32 nHeight = (img->d_h < nDstHeight) ? img->d_h : nDstHeight;
779 const prim_size_t roi = { .width = nWidth, .height = nHeight };
780
781 if (!areRectsValid(av1->log, roi.width, roi.height, regionRects, numRegionRect))
782 return -2;
783
784 pstatus_t rec = -1;
785#if defined(WITH_LIBYUV)
786 /* J420ToARGB()/J444ToARGB() always write pDstData as packed BGRA8888 regardless of
787 * DstFormat, so the fast path only applies for that layout; anything else must go
788 * through the format-aware primitives conversion below. */
789 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
790 {
791 switch (img->fmt)
792 {
793 case AOM_IMG_FMT_I420:
794 rec = J420ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
795 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
796 nHeight);
797 break;
798 case AOM_IMG_FMT_I444:
799 rec = J444ToARGB(img->planes[0], img->stride[0], img->planes[1], img->stride[1],
800 img->planes[2], img->stride[2], pDstData, nDstStep, nWidth,
801 nHeight);
802 break;
803 default:
804 WLog_Print(av1->log, WLOG_ERROR, "img->fmt %d not supported", img->fmt);
805 return -1;
806 }
807 }
808 else
809#endif
810 {
811 const BYTE* pSrc[] = { img->planes[0], img->planes[1], img->planes[2] };
812 const UINT32 strides[] = { img->stride[0], img->stride[1], img->stride[2] };
813
814 primitives_t* primitives = primitives_get();
815 if (!primitives)
816 {
817 WLog_Print(av1->log, WLOG_ERROR, "primitives_get(): nullptr");
818 return FALSE;
819 }
820
821 switch (img->fmt)
822 {
823 case AOM_IMG_FMT_I420:
824 rec = primitives->YUV420ToRGB_8u_P3AC4R(pSrc, strides, pDstData, nDstStep,
825 DstFormat, &roi);
826 break;
827 case AOM_IMG_FMT_I444:
828 rec = primitives->I444ToRGB_8u(pSrc, strides, pDstData, nDstStep, DstFormat,
829 &roi);
830 break;
831 default:
832 WLog_Print(av1->log, WLOG_ERROR, "img->fmt %d not supported", img->fmt);
833 return -1;
834 }
835 }
836 if (rec != 0)
837 {
838 WLog_Print(av1->log, WLOG_ERROR, "RGB to YUV conversion failed: %d", rec);
839 return -1;
840 }
841 }
842
843 return TRUE;
844#endif
845}
846
847BOOL freerdp_av1_context_reset(FREERDP_AV1_CONTEXT* av1, UINT32 width, UINT32 height)
848{
849 if (av1->encoder)
850 {
851#if defined(WITH_LIBAOM)
852 av1->ecfg.g_w = width;
853 av1->ecfg.g_h = height;
854
855 if (av1->initialized)
856 {
857 const aom_codec_err_t rc = aom_codec_destroy(&av1->ctx);
858 av1->initialized = false;
859 if (rc != AOM_CODEC_OK)
860 {
861 WLog_Print(av1->log, WLOG_WARN, "aom_codec_destroy: %s",
862 aom_codec_err_to_string(rc));
863 return FALSE;
864 }
865 }
866
867 const aom_codec_err_t rc =
868 aom_codec_enc_init(&av1->ctx, av1->iface, &av1->ecfg, av1->flags);
869 if (rc != AOM_CODEC_OK)
870 {
871 WLog_Print(av1->log, WLOG_WARN, "aom_codec_enc_init: %s", aom_codec_err_to_string(rc));
872 return FALSE;
873 }
874
875 av1->framecount = 0;
876 av1->eflags = 0; // AOM_EFLAG_FORCE_KF;
877 av1->initialized = true;
878 av1->fullFrameNext = true;
879
880 if (!av1_aom_apply_controls(av1))
881 return FALSE;
882#else
883 WLog_Print(av1->log, WLOG_ERROR,
884 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
885 return FALSE;
886#endif
887 }
888 else
889 {
890#if defined(WITH_AV1_WEBCODECS)
891 if (av1->webcodecs == 0)
892 {
893 av1->webcodecs = av1_wc_open();
894 WLog_Print(av1->log, WLOG_INFO, "AV1 decoder: %s",
895 av1->webcodecs > 0 ? "WebCodecs (browser), dav1d as fallback" : "dav1d");
896 }
897#endif
898#if defined(WITH_DAV1D)
899 if (av1->initialized)
900 {
901 dav1d_close(&av1->dav1d);
902 av1->initialized = false;
903 }
904
905 Dav1dSettings settings = WINPR_C_ARRAY_INIT;
906 dav1d_default_settings(&settings);
907 /* Single-threaded: freerdp_av1_decompress() needs its picture back synchronously,
908 * not held back by frame-parallel decoding. */
909 settings.n_threads = 1;
910 settings.max_frame_delay = 1;
911
912 const int rc = dav1d_open(&av1->dav1d, &settings);
913 if (rc < 0)
914 {
915 WLog_Print(av1->log, WLOG_WARN, "dav1d_open: %d", rc);
916 return FALSE;
917 }
918 av1->initialized = true;
919#elif defined(WITH_LIBAOM)
920 av1->dcfg.w = width;
921 av1->dcfg.h = height;
922 av1->dcfg.allow_lowbitdepth = 1;
923 const aom_codec_err_t rc =
924 aom_codec_dec_init(&av1->ctx, av1->iface, &av1->dcfg, av1->flags);
925 if (rc != AOM_CODEC_OK)
926 {
927 WLog_Print(av1->log, WLOG_WARN, "aom_codec_dec_init: %s", aom_codec_err_to_string(rc));
928 return FALSE;
929 }
930 av1->initialized = true;
931#else
932 WLog_Print(av1->log, WLOG_WARN,
933 "This build does not support AV1 decoding. Recompile with '-DWITH_DAV1D=ON' "
934 "or '-DWITH_AOM=ON'");
935 return FALSE;
936#endif
937 }
938
939 av1->yuvWidth = width;
940 av1->yuvStride[0] = width;
941
942 const size_t pad = av1->yuvStride[0] % 16;
943 if (pad != 0)
944 av1->yuvStride[0] += 16 - pad;
945 av1->yuvStride[0] *= 3;
946 av1->yuvStride[1] = width;
947 av1->yuvStride[2] = width;
948
949 av1->yuvHeight = height;
950 winpr_aligned_free(av1->yuvdata[0]);
951 winpr_aligned_free(av1->yuvdata[1]);
952 winpr_aligned_free(av1->yuvdata[2]);
953 av1->yuvdata[0] = winpr_aligned_malloc(1ull * av1->yuvStride[0] * av1->yuvHeight, 64);
954 av1->yuvdata[1] = winpr_aligned_malloc(1ull * av1->yuvStride[1] * av1->yuvHeight, 64);
955 av1->yuvdata[2] = winpr_aligned_malloc(1ull * av1->yuvStride[2] * av1->yuvHeight, 64);
956 return av1->yuvdata[0] != nullptr;
957}
958
959void freerdp_av1_context_free(FREERDP_AV1_CONTEXT* av1)
960{
961 if (!av1)
962 return;
963
964 if (av1->initialized)
965 {
966 if (av1->encoder)
967 {
968#if defined(WITH_LIBAOM)
969 av1_aom_destroy(av1);
970#endif
971 }
972 else
973 {
974#if defined(WITH_DAV1D)
975 dav1d_close(&av1->dav1d);
976#elif defined(WITH_LIBAOM)
977 av1_aom_destroy(av1);
978#endif
979 }
980 }
981#if defined(WITH_AV1_WEBCODECS)
982 av1_wc_close(av1->webcodecs);
983#endif
984 winpr_aligned_free(av1->yuvdata[0]);
985 winpr_aligned_free(av1->yuvdata[1]);
986 winpr_aligned_free(av1->yuvdata[2]);
987 free(av1);
988}
989
990FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
991{
992 FREERDP_AV1_CONTEXT* ctx = calloc(1, sizeof(FREERDP_AV1_CONTEXT));
993 if (!ctx)
994 return nullptr;
995 ctx->encoder = Compressor;
996 ctx->log = WLog_Get(TAG);
997 if (!ctx->log)
998 goto fail;
999
1000 if (Compressor)
1001 {
1002#if defined(WITH_LIBAOM)
1003 ctx->iface = aom_codec_av1_cx();
1004 if (!ctx->iface)
1005 {
1006 WLog_Print(ctx->log, WLOG_ERROR, "aom_codec_av1_cx() nullptr");
1007 goto fail;
1008 }
1009
1010 const aom_codec_err_t rc =
1011 aom_codec_enc_config_default(ctx->iface, &ctx->ecfg, AOM_USAGE_REALTIME);
1012 if (rc != AOM_CODEC_OK)
1013 {
1014 WLog_Print(ctx->log, WLOG_ERROR, "aom_codec_enc_config_default() %s",
1015 aom_codec_err_to_string(rc));
1016 goto fail;
1017 }
1018 ctx->ecfg.g_threads = av1_aom_threads();
1019#else
1020 WLog_Print(ctx->log, WLOG_ERROR,
1021 "AV1 encoder requires libaom, recompile with '-DWITH_AOM=ON'");
1022 goto fail;
1023#endif
1024 }
1025#if defined(WITH_LIBAOM) && !defined(WITH_DAV1D)
1026 else
1027 {
1028 ctx->iface = aom_codec_av1_dx();
1029 if (!ctx->iface)
1030 {
1031 WLog_Print(ctx->log, WLOG_ERROR, "aom_codec_av1_dx() nullptr");
1032 goto fail;
1033 }
1034 }
1035#endif
1036
1037 return ctx;
1038
1039fail:
1040 freerdp_av1_context_free(ctx);
1041 return nullptr;
1042}
1043#else
1044
1045struct S_FREERDP_AV1_CONTEXT
1046{
1047 BOOL Compressor;
1048 wLog* log;
1049};
1050
1051BOOL freerdp_av1_context_set_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1052 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option,
1053 WINPR_ATTR_UNUSED UINT32 value)
1054{
1055 return FALSE;
1056}
1057
1058UINT32 freerdp_av1_context_get_option(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1059 WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT_OPTION option)
1060{
1061 return 0;
1062}
1063
1064INT32 freerdp_av1_compress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1065 WINPR_ATTR_UNUSED const BYTE* pSrcData,
1066 WINPR_ATTR_UNUSED DWORD SrcFormat, WINPR_ATTR_UNUSED UINT32 nSrcStep,
1067 WINPR_ATTR_UNUSED UINT32 nSrcWidth, WINPR_ATTR_UNUSED UINT32 nSrcHeight,
1068 WINPR_ATTR_UNUSED const RECTANGLE_16* regionRect,
1069 WINPR_ATTR_UNUSED BYTE** ppDstData, WINPR_ATTR_UNUSED UINT32* pDstSize,
1070 WINPR_ATTR_UNUSED RDPGFX_H264_METABLOCK* meta)
1071{
1072 WINPR_ASSERT(av1);
1073 WLog_Print(av1->log, WLOG_ERROR,
1074 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1075 "'-DWITH_DAV1D=ON'");
1076 return -1;
1077}
1078
1079INT32 freerdp_av1_decompress(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1080 WINPR_ATTR_UNUSED const BYTE* pSrcData,
1081 WINPR_ATTR_UNUSED UINT32 SrcSize, WINPR_ATTR_UNUSED BYTE* pDstData,
1082 WINPR_ATTR_UNUSED DWORD DstFormat, WINPR_ATTR_UNUSED UINT32 nDstStep,
1083 WINPR_ATTR_UNUSED UINT32 nDstWidth,
1084 WINPR_ATTR_UNUSED UINT32 nDstHeight,
1085 WINPR_ATTR_UNUSED const RECTANGLE_16* regionRects,
1086 WINPR_ATTR_UNUSED UINT32 numRegionRect)
1087{
1088 WINPR_ASSERT(av1);
1089 WLog_Print(av1->log, WLOG_ERROR,
1090 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1091 "'-DWITH_DAV1D=ON'");
1092 return -1;
1093}
1094
1095BOOL freerdp_av1_context_reset(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1,
1096 WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height)
1097{
1098 WINPR_ASSERT(av1);
1099 WLog_Print(av1->log, WLOG_WARN,
1100 "This build does not support AV1 codec. Recompile with '-DWITH_AOM=ON' or "
1101 "'-DWITH_DAV1D=ON'");
1102 return FALSE;
1103}
1104
1105void freerdp_av1_context_free(WINPR_ATTR_UNUSED FREERDP_AV1_CONTEXT* av1)
1106{
1107 free(av1);
1108}
1109
1110FREERDP_AV1_CONTEXT* freerdp_av1_context_new(BOOL Compressor)
1111{
1112 FREERDP_AV1_CONTEXT* ctx = calloc(1, sizeof(FREERDP_AV1_CONTEXT));
1113 if (!ctx)
1114 return nullptr;
1115 ctx->Compressor = Compressor;
1116 ctx->log = WLog_Get(TAG);
1117 return ctx;
1118}
1119#endif
WINPR_ATTR_NODISCARD fn_RGBToYUV444_8u_P3AC4R_t RGBToI444_8u
Definition primitives.h:305
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 ...
Definition primitives.h:310