FreeRDP
Loading...
Searching...
No Matches
av2.c
1
17
18#include <winpr/sysinfo.h>
19
20#include <freerdp/codec/av2.h>
21#include <freerdp/primitives.h>
22#include <freerdp/log.h>
23#include <freerdp/codec/region.h>
24
25#include "h264.h"
26
27#define TAG FREERDP_TAG("codec.av2")
28
29#if defined(WITH_LIBAVM)
30#include <avm/avm.h>
31
32#include <avm/avm_decoder.h>
33#include <avm/avm_image.h>
34#include <avm/avmdx.h>
35#endif
36
37/* The encoder: libavm's, unless it was built without one (CONFIG_AV2_ENCODER=0,
38 * WITH_AVM_ENCODER=OFF), which leaves out its headers and functions; decoding is the same */
39#if defined(WITH_LIBAVM) && defined(WITH_LIBAVM_ENCODER)
40#define AV2_ENCODER_LIBAVM
41#include <avm/avm_encoder.h>
42#include <avm/avmcx.h>
43#endif
44
45/* The decoder: libavm, the reference, when there is one. dav2d 0.1.0 decodes the intra frames
46 * of libavm v1.0.0 bit-exact, but not its inter frames, and rejects its 4:4:4 streams. */
47#if defined(WITH_DAV2D) && !defined(WITH_LIBAVM)
48#define AV2_DECODER_DAV2D
49#include <dav2d/dav2d.h>
50#endif
51
52#if defined(WITH_LIBAVM) || defined(WITH_DAV2D)
53
54#if defined(WITH_LIBYUV)
55#include <libyuv.h>
56#endif
57
58/* AV2 bitstream profiles, [AV2] Annex A, Table A.1 */
59#define AV2_PROFILE_MAIN_420_10_IP0 0
60#define AV2_PROFILE_MAIN_444_10_IP1 4
61
62/* The realtime usage is newer than libavm v1.0.0 */
63#if defined(WITH_LIBAVM) && !defined(AVM_USAGE_REALTIME)
64#define AVM_USAGE_REALTIME AVM_USAGE_GOOD_QUALITY
65#endif
66
67/* The planes of a decoded picture */
68typedef struct
69{
70 const BYTE* data[3];
71 UINT32 stride[3]; /* in bytes */
72 UINT32 width;
73 UINT32 height;
74 UINT32 bpc; /* significant bits per sample */
75 BOOL highbd; /* samples stored in 16 bits instead of 8 */
76 BOOL i444;
77} av2_planes;
78
79struct S_FREERDP_AV2_CONTEXT
80{
81 wLog* log;
82 bool encoder;
83 bool initialized;
84#if defined(WITH_LIBAVM)
85 avm_codec_ctx_t ctx;
86#if defined(AV2_ENCODER_LIBAVM)
87 avm_codec_enc_cfg_t ecfg;
88 avm_codec_pts_t framecount;
89 avm_enc_frame_flags_t eflags;
90 UINT32 qp; /* FREERDP_AV2_CONTEXT_OPTION_QP, 0: libavm's */
91 UINT32 speed; /* FREERDP_AV2_CONTEXT_OPTION_SPEED, UINT32_MAX: by usage */
92 UINT32 tileColumns; /* FREERDP_AV2_CONTEXT_OPTION_TILE_COLUMNS, log2 */
93 UINT32 dpbSize; /* FREERDP_AV2_CONTEXT_OPTION_DPB_SIZE, 0: libavm's */
94#endif
95 avm_codec_flags_t flags;
96 avm_codec_iface_t* iface;
97 UINT32 profile; /* FREERDP_AV2_CONTEXT_OPTION_PROFILE: 0 4:2:0, 1 4:4:4 */
98#endif
99#if defined(WITH_LIBAVM)
100 avm_codec_dec_cfg_t dcfg;
101#endif
102#if defined(AV2_DECODER_DAV2D)
103 Dav2dContext* dav2d;
104#endif
105 BYTE* yuvdata[3];
106 UINT32 yuvWidth;
107 UINT32 yuvStride[3];
108 UINT32 yuvHeight;
109};
110
111#if defined(WITH_LIBAVM)
112static void av2_avm_destroy(FREERDP_AV2_CONTEXT* av2)
113{
114 const avm_codec_err_t rc = avm_codec_destroy(&av2->ctx);
115 if (rc != AVM_CODEC_OK)
116 WLog_Print(av2->log, WLOG_WARN, "avm_codec_destroy: %s", avm_codec_err_to_string(rc));
117}
118#endif
119
120#if defined(AV2_ENCODER_LIBAVM)
121
122WINPR_ATTR_NODISCARD
123static BOOL av2_avm_control(FREERDP_AV2_CONTEXT* av2, int id, const char* name, int value)
124{
125 const avm_codec_err_t rc = avm_codec_control(&av2->ctx, id, value);
126 if (rc != AVM_CODEC_OK)
127 {
128 WLog_Print(av2->log, WLOG_WARN, "avm_codec_control(%s, %d): %s", name, value,
129 avm_codec_err_to_string(rc));
130 return FALSE;
131 }
132 return TRUE;
133}
134#endif
135
136BOOL freerdp_av2_context_set_option(FREERDP_AV2_CONTEXT* av2, FREERDP_AV2_CONTEXT_OPTION option,
137 UINT32 value)
138{
139#if defined(AV2_ENCODER_LIBAVM)
140 switch (option)
141 {
142 case FREERDP_AV2_CONTEXT_OPTION_PROFILE:
143 if (!av2->encoder)
144 return FALSE;
145 if (value > 1)
146 {
147 WLog_Print(av2->log, WLOG_WARN,
148 "Unknown FREERDP_AV2_CONTEXT_OPTION_PROFILE value %" PRIu32, value);
149 return FALSE;
150 }
151
152 av2->profile = value;
153 av2->ecfg.g_profile =
154 (value == 1) ? AV2_PROFILE_MAIN_444_10_IP1 : AV2_PROFILE_MAIN_420_10_IP0;
155 break;
156
157 case FREERDP_AV2_CONTEXT_OPTION_RATECONTROL:
158 if (!av2->encoder)
159 return FALSE;
160 switch (value)
161 {
162 case FREERDP_AV2_VBR:
163 av2->ecfg.rc_end_usage = AVM_VBR;
164 break;
165 case FREERDP_AV2_CBR:
166 av2->ecfg.rc_end_usage = AVM_CBR;
167 break;
168 case FREERDP_AV2_CQ:
169 av2->ecfg.rc_end_usage = AVM_CQ;
170 break;
171 case FREERDP_AV2_Q:
172 av2->ecfg.rc_end_usage = AVM_Q;
173 break;
174 default:
175 WLog_Print(av2->log, WLOG_WARN,
176 "Unknown FREERDP_AV2_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32
177 "]",
178 value);
179 return FALSE;
180 }
181 break;
182 case FREERDP_AV2_CONTEXT_OPTION_BITRATE:
183 if (!av2->encoder)
184 return FALSE;
185 av2->ecfg.rc_target_bitrate = value;
186 break;
187 case FREERDP_AV2_CONTEXT_OPTION_USAGETYPE:
188 if (!av2->encoder)
189 return FALSE;
190 av2->ecfg.g_usage = value;
191 break;
192 case FREERDP_AV2_CONTEXT_OPTION_QP:
193 if (!av2->encoder || (value > 255))
194 return FALSE;
195 av2->qp = value;
196 break;
197 case FREERDP_AV2_CONTEXT_OPTION_THREADS:
198 if (!av2->encoder || (value > 64))
199 return FALSE;
200 av2->ecfg.g_threads = (value > 0) ? value : 1;
201 break;
202 case FREERDP_AV2_CONTEXT_OPTION_SPEED:
203 if (!av2->encoder || ((value > 10) && (value != UINT32_MAX)))
204 return FALSE;
205 av2->speed = value;
206 break;
207 case FREERDP_AV2_CONTEXT_OPTION_TILE_COLUMNS:
208 if (!av2->encoder || (value > 6))
209 return FALSE;
210 av2->tileColumns = value;
211 break;
212 case FREERDP_AV2_CONTEXT_OPTION_DPB_SIZE:
213 if (!av2->encoder || (value > 16))
214 return FALSE;
215 av2->dpbSize = value;
216 break;
217 default:
218 WLog_Print(av2->log, WLOG_ERROR, "Unknown FREERDP_AV2_CONTEXT_OPTION[0x%08" PRIx32 "]",
219 option);
220 return TRUE;
221 }
222 /* before the first reset only kept: the encoder starts once, with all of them */
223 if (!av2->initialized)
224 return TRUE;
225 return freerdp_av2_context_reset(av2, av2->ecfg.g_w, av2->ecfg.g_h);
226#else
227 WLog_Print(av2->log, WLOG_WARN,
228 "AV2 encoder requires libavm with its encoder, recompile with '-DWITH_AVM=ON'");
229 WINPR_UNUSED(option);
230 WINPR_UNUSED(value);
231 return FALSE;
232#endif
233}
234
235UINT32 freerdp_av2_context_get_option(FREERDP_AV2_CONTEXT* av2, FREERDP_AV2_CONTEXT_OPTION option)
236{
237#if defined(AV2_ENCODER_LIBAVM)
238 switch (option)
239 {
240 case FREERDP_AV2_CONTEXT_OPTION_PROFILE:
241 if (!av2->encoder)
242 return 0;
243 return av2->profile;
244 case FREERDP_AV2_CONTEXT_OPTION_RATECONTROL:
245 if (!av2->encoder)
246 return 0;
247 switch (av2->ecfg.rc_end_usage)
248 {
249 case AVM_VBR:
250 return FREERDP_AV2_VBR;
251 case AVM_CBR:
252 return FREERDP_AV2_CBR;
253 case AVM_CQ:
254 return FREERDP_AV2_CQ;
255 case AVM_Q:
256 default:
257 return FREERDP_AV2_Q;
258 }
259 case FREERDP_AV2_CONTEXT_OPTION_BITRATE:
260 if (!av2->encoder)
261 return 0;
262 return av2->ecfg.rc_target_bitrate;
263 case FREERDP_AV2_CONTEXT_OPTION_USAGETYPE:
264 if (!av2->encoder)
265 return 0;
266 return av2->ecfg.g_usage;
267 case FREERDP_AV2_CONTEXT_OPTION_QP:
268 if (!av2->encoder)
269 return 0;
270 return av2->qp;
271 case FREERDP_AV2_CONTEXT_OPTION_THREADS:
272 if (!av2->encoder)
273 return 0;
274 return av2->ecfg.g_threads;
275 case FREERDP_AV2_CONTEXT_OPTION_SPEED:
276 if (!av2->encoder)
277 return 0;
278 return av2->speed;
279 case FREERDP_AV2_CONTEXT_OPTION_TILE_COLUMNS:
280 if (!av2->encoder)
281 return 0;
282 return av2->tileColumns;
283 case FREERDP_AV2_CONTEXT_OPTION_DPB_SIZE:
284 if (!av2->encoder)
285 return 0;
286 return av2->dpbSize;
287 default:
288 WLog_Print(av2->log, WLOG_ERROR, "Unknown FREERDP_AV2_CONTEXT_OPTION[0x%08" PRIx32 "]",
289 option);
290 return 0;
291 }
292#else
293 WINPR_UNUSED(av2);
294 WINPR_UNUSED(option);
295 return 0;
296#endif
297}
298
299INT32 freerdp_av2_compress(FREERDP_AV2_CONTEXT* av2, const BYTE* pSrcData, DWORD SrcFormat,
300 UINT32 nSrcStep, UINT32 nSrcWidth, UINT32 nSrcHeight,
301 const RECTANGLE_16* regionRect, BYTE** ppDstData, UINT32* pDstSize,
303{
304 *ppDstData = nullptr;
305 *pDstSize = 0;
306
307 if (!av2->encoder)
308 {
309 WLog_Print(av2->log, WLOG_ERROR, "av2->encoder: %d", av2->encoder);
310 return -1;
311 }
312
313#if defined(AV2_ENCODER_LIBAVM)
314 primitives_t* primitives = primitives_get();
315 if (!primitives)
316 {
317 WLog_Print(av2->log, WLOG_ERROR, "primitives_get(): nullptr");
318 return -1;
319 }
320
321 if (av2->yuvWidth != nSrcWidth)
322 {
323 WLog_Print(av2->log, WLOG_ERROR, "av2->yuvWidth[%" PRIu32 "] != nSrcWidth[%" PRIu32 "]",
324 av2->yuvWidth, nSrcWidth);
325 return -1;
326 }
327
328 if (av2->yuvHeight != nSrcHeight)
329 {
330 WLog_Print(av2->log, WLOG_ERROR, "av2->yuvHeight[%" PRIu32 "] != nSrcHeight[%" PRIu32 "]",
331 av2->yuvHeight, nSrcHeight);
332 return -1;
333 }
334
335 const prim_size_t roi = { .width = nSrcWidth, .height = nSrcHeight };
336
337 avm_image_t buffer = WINPR_C_ARRAY_INIT;
338
339 avm_image_t* img = &buffer;
340 pstatus_t rec = -1;
341#if defined(WITH_LIBYUV)
342 /* ARGBToI420()/ARGBToI444() always read pSrcData as packed BGRA8888 regardless of
343 * SrcFormat, so the fast path only applies for that layout; anything else must go
344 * through the format-aware primitives conversion below. */
345 if ((SrcFormat == PIXEL_FORMAT_BGRA32) || (SrcFormat == PIXEL_FORMAT_BGRX32))
346 {
347 switch (av2->profile)
348 {
349 case 0:
350 img->fmt = AVM_IMG_FMT_I420;
351 img->x_chroma_shift = 1;
352 img->y_chroma_shift = 1;
353 rec = ARGBToI420(pSrcData, nSrcStep, av2->yuvdata[0], av2->yuvStride[0],
354 av2->yuvdata[1], av2->yuvStride[1], av2->yuvdata[2],
355 WINPR_ASSERTING_INT_CAST(int, av2->yuvStride[2]),
356 WINPR_ASSERTING_INT_CAST(int, roi.width),
357 WINPR_ASSERTING_INT_CAST(int, roi.height));
358 break;
359 case 1:
360 img->fmt = AVM_IMG_FMT_I444;
361 rec = ARGBToI444(pSrcData, nSrcStep, av2->yuvdata[0], av2->yuvStride[0],
362 av2->yuvdata[1], av2->yuvStride[1], av2->yuvdata[2],
363 WINPR_ASSERTING_INT_CAST(int, av2->yuvStride[2]),
364 WINPR_ASSERTING_INT_CAST(int, roi.width),
365 WINPR_ASSERTING_INT_CAST(int, roi.height));
366 break;
367 default:
368 WLog_Print(av2->log, WLOG_ERROR, "Unsupported AV2 profile %" PRIu32, av2->profile);
369 return -1;
370 }
371 }
372 else
373#endif
374 {
375 switch (av2->profile)
376 {
377 case 0:
378 img->fmt = AVM_IMG_FMT_I420;
379 img->x_chroma_shift = 1;
380 img->y_chroma_shift = 1;
381 rec = primitives->RGBToYUV420_8u_P3AC4R(pSrcData, SrcFormat, nSrcStep, av2->yuvdata,
382 av2->yuvStride, &roi);
383 break;
384 case 1:
385 img->fmt = AVM_IMG_FMT_I444;
386 rec = primitives->RGBToI444_8u(pSrcData, SrcFormat, nSrcStep, av2->yuvdata,
387 av2->yuvStride, &roi);
388 break;
389 default:
390 WLog_Print(av2->log, WLOG_ERROR, "Unsupported AV2 profile %" PRIu32, av2->profile);
391 return -1;
392 }
393 }
394 if (rec != 0)
395 {
396 WLog_Print(av2->log, WLOG_ERROR, "RGB to YUV conversion failed: %d", rec);
397 return -1;
398 }
399 img->bit_depth = 8;
400 img->d_w = img->r_w = img->w = roi.width;
401 img->d_h = img->r_h = img->h = roi.height;
402 img->stride[0] = WINPR_ASSERTING_INT_CAST(int, av2->yuvStride[0]);
403 img->stride[1] = WINPR_ASSERTING_INT_CAST(int, av2->yuvStride[1]);
404 img->stride[2] = WINPR_ASSERTING_INT_CAST(int, av2->yuvStride[2]);
405 img->planes[0] = av2->yuvdata[0];
406 img->planes[1] = av2->yuvdata[1];
407 img->planes[2] = av2->yuvdata[2];
408
409 const avm_codec_err_t rc = avm_codec_encode(&av2->ctx, img, ++av2->framecount, 1, av2->eflags);
410 av2->eflags = 0;
411
412 if (rc != AVM_CODEC_OK)
413 {
414 WLog_Print(av2->log, WLOG_WARN, "avm_codec_encode: %s", avm_codec_err_to_string(rc));
415 return -1;
416 }
417
418 avm_codec_iter_t iter = nullptr;
419 const avm_codec_cx_pkt_t* pkt = nullptr;
420 while ((pkt = avm_codec_get_cx_data(&av2->ctx, &iter)) != nullptr)
421 {
422 if (pkt->kind != AVM_CODEC_CX_FRAME_PKT)
423 continue;
424
425 *ppDstData = pkt->data.frame.buf;
426 *pDstSize = WINPR_ASSERTING_INT_CAST(UINT32, pkt->data.frame.sz);
427 }
428 if (*pDstSize == 0)
429 return 0;
430
431 RECTANGLE_16* rect = calloc(1, sizeof(RECTANGLE_16));
432 if (rect)
433 {
434 rect->right = WINPR_ASSERTING_INT_CAST(UINT16, nSrcWidth);
435 rect->bottom = WINPR_ASSERTING_INT_CAST(UINT16, nSrcHeight);
436 }
437 return allocate_h264_metablock(10, rect, meta, 1);
438#else
439 WLog_Print(av2->log, WLOG_ERROR,
440 "AV2 encoder requires libavm with its encoder, recompile with '-DWITH_AVM=ON'");
441 WINPR_UNUSED(pSrcData);
442 WINPR_UNUSED(SrcFormat);
443 WINPR_UNUSED(nSrcStep);
444 WINPR_UNUSED(nSrcWidth);
445 WINPR_UNUSED(nSrcHeight);
446 WINPR_UNUSED(regionRect);
447 WINPR_UNUSED(meta);
448 return -1;
449#endif
450}
451
452static BOOL isRectValid(UINT32 width, UINT32 height, const RECTANGLE_16* rect)
453{
454 WINPR_ASSERT(rect);
455 if (rect->left > width)
456 return FALSE;
457 if (rect->right > width)
458 return FALSE;
459 if (rect->left >= rect->right)
460 return FALSE;
461 if (rect->top > height)
462 return FALSE;
463 if (rect->bottom > height)
464 return FALSE;
465 if (rect->top >= rect->bottom)
466 return FALSE;
467 return TRUE;
468}
469
470static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height, const RECTANGLE_16* rects,
471 UINT32 count)
472{
473 WINPR_ASSERT(rects || (count == 0));
474 for (size_t x = 0; x < count; x++)
475 {
476 const RECTANGLE_16* rect = &rects[x];
477 if (!isRectValid(width, height, rect))
478 {
479 char buffer[64] = WINPR_C_ARRAY_INIT;
480 WLog_Print(log, WLOG_WARN,
481 "Rectangle %" PRIuz " %s outside of bounding frame %" PRIu32 "x%" PRIu32, x,
482 rectangle_to_string(rect, buffer, sizeof(buffer)), width, height);
483 return FALSE;
484 }
485 }
486 return TRUE;
487}
488
489/* AV2's main profiles are 10 bit: a decoder may hand out 16 bit samples, even for an 8 bit
490 * stream. Bring them down to 8 bit into the context's own planes, so that the colour
491 * conversion below always reads 8 bit samples. */
492WINPR_ATTR_NODISCARD
493static BOOL av2_planes_to_8bit(FREERDP_AV2_CONTEXT* av2, av2_planes* planes)
494{
495 WINPR_ASSERT(av2);
496 WINPR_ASSERT(planes);
497
498 if (!planes->highbd)
499 return (planes->bpc == 8);
500 if ((planes->bpc < 8) || (planes->bpc > 16))
501 {
502 WLog_Print(av2->log, WLOG_ERROR, "AV2 picture bpc %" PRIu32 " not supported", planes->bpc);
503 return FALSE;
504 }
505
506 if ((planes->width > av2->yuvWidth) || (planes->height > av2->yuvHeight))
507 {
508 WLog_Print(av2->log, WLOG_ERROR,
509 "AV2 picture %" PRIu32 "x%" PRIu32 " larger than the surface %" PRIu32
510 "x%" PRIu32,
511 planes->width, planes->height, av2->yuvWidth, av2->yuvHeight);
512 return FALSE;
513 }
514
515 const UINT32 shift = planes->bpc - 8;
516 for (size_t p = 0; p < 3; p++)
517 {
518 const UINT32 w = ((p == 0) || planes->i444) ? planes->width : (planes->width + 1) / 2;
519 const UINT32 h = ((p == 0) || planes->i444) ? planes->height : (planes->height + 1) / 2;
520 for (UINT32 y = 0; y < h; y++)
521 {
522 const UINT16* src = WINPR_PACKED_ALIGN_CAST(
523 const UINT16*, &planes->data[p][1ull * y * planes->stride[p]]);
524 BYTE* dst = &av2->yuvdata[p][1ull * y * av2->yuvStride[p]];
525 for (UINT32 x = 0; x < w; x++)
526 {
527 const UINT32 v = src[x] >> shift;
528 dst[x] = (BYTE)MIN(v, UINT8_MAX);
529 }
530 }
531 planes->data[p] = av2->yuvdata[p];
532 planes->stride[p] = av2->yuvStride[p];
533 }
534 planes->bpc = 8;
535 planes->highbd = FALSE;
536 return TRUE;
537}
538
539WINPR_ATTR_NODISCARD
540static INT32 av2_convert(FREERDP_AV2_CONTEXT* av2, av2_planes* planes, BYTE* pDstData,
541 DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
542 const RECTANGLE_16* regionRects, UINT32 numRegionRect)
543{
544 if (!av2_planes_to_8bit(av2, planes))
545 {
546 WLog_Print(av2->log, WLOG_ERROR, "AV2 picture with %" PRIu32 " bit samples not supported",
547 planes->bpc);
548 return -1;
549 }
550
551 /* The decoded frame may be smaller than the destination surface, so
552 * bound the converted region to it. Otherwise the colour conversion
553 * reads past the decoded image planes. */
554 const UINT32 nWidth = (planes->width < nDstWidth) ? planes->width : nDstWidth;
555 const UINT32 nHeight = (planes->height < nDstHeight) ? planes->height : nDstHeight;
556 const prim_size_t roi = { .width = nWidth, .height = nHeight };
557
558 if (!areRectsValid(av2->log, roi.width, roi.height, regionRects, numRegionRect))
559 return -2;
560
561 pstatus_t rec = -1;
562#if defined(WITH_LIBYUV)
563 /* J420ToARGB()/J444ToARGB() always write pDstData as packed BGRA8888 regardless of
564 * DstFormat, so the fast path only applies for that layout; anything else must go
565 * through the format-aware primitives conversion below. */
566 if ((DstFormat == PIXEL_FORMAT_BGRA32) || (DstFormat == PIXEL_FORMAT_BGRX32))
567 {
568 const int stride0 = WINPR_ASSERTING_INT_CAST(int, planes->stride[0]);
569 const int stride1 = WINPR_ASSERTING_INT_CAST(int, planes->stride[1]);
570 const int stride2 = WINPR_ASSERTING_INT_CAST(int, planes->stride[2]);
571 const int iWidth = WINPR_ASSERTING_INT_CAST(int, nWidth);
572 const int iHeight = WINPR_ASSERTING_INT_CAST(int, nHeight);
573 const int iDstStep = WINPR_ASSERTING_INT_CAST(int, nDstStep);
574
575 if (planes->i444)
576 rec = J444ToARGB(planes->data[0], stride0, planes->data[1], stride1, planes->data[2],
577 stride2, pDstData, iDstStep, iWidth, iHeight);
578 else
579 rec = J420ToARGB(planes->data[0], stride0, planes->data[1], stride1, planes->data[2],
580 stride2, pDstData, iDstStep, iWidth, iHeight);
581 }
582 else
583#endif
584 {
585 primitives_t* primitives = primitives_get();
586 if (!primitives)
587 {
588 WLog_Print(av2->log, WLOG_ERROR, "primitives_get(): nullptr");
589 return -1;
590 }
591
592 if (planes->i444)
593 rec = primitives->I444ToRGB_8u(planes->data, planes->stride, pDstData, nDstStep,
594 DstFormat, &roi);
595 else
596 rec = primitives->YUV420ToRGB_8u_P3AC4R(planes->data, planes->stride, pDstData,
597 nDstStep, DstFormat, &roi);
598 }
599 if (rec != 0)
600 {
601 WLog_Print(av2->log, WLOG_ERROR, "YUV to RGB conversion failed: %d", rec);
602 return -1;
603 }
604 return TRUE;
605}
606
607#if defined(AV2_DECODER_DAV2D)
608static void av2_dav2d_data_free(WINPR_ATTR_UNUSED const uint8_t* buf,
609 WINPR_ATTR_UNUSED void* cookie)
610{
611 /* pSrcData is owned by the caller of freerdp_av2_decompress(), dav2d must not free it. */
612}
613
614/* Return: TRUE if a picture was converted, FALSE if none was ready (EAGAIN), <0 on error. */
615static INT32 av2_dav2d_drain_one(FREERDP_AV2_CONTEXT* av2, BYTE* pDstData, DWORD DstFormat,
616 UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight,
617 const RECTANGLE_16* regionRects, UINT32 numRegionRect)
618{
619 Dav2dPicture pic = WINPR_C_ARRAY_INIT;
620 const int prc = dav2d_get_picture(av2->dav2d, &pic);
621 if (prc == DAV2D_ERR(EAGAIN))
622 return FALSE;
623 if (prc < 0)
624 {
625 WLog_Print(av2->log, WLOG_WARN, "dav2d_get_picture: %d", prc);
626 return -1;
627 }
628
629 INT32 rc = -1;
630 switch (pic.p.layout)
631 {
632 case DAV2D_PIXEL_LAYOUT_I420:
633 case DAV2D_PIXEL_LAYOUT_I444:
634 {
635 av2_planes planes = { .data = { pic.data[0], pic.data[1], pic.data[2] },
636 .stride = { (UINT32)pic.stride[0], (UINT32)pic.stride[1],
637 (UINT32)pic.stride[1] },
638 .width = (UINT32)pic.p.w,
639 .height = (UINT32)pic.p.h,
640 .bpc = (UINT32)pic.p.bpc,
641 .highbd = (pic.p.bpc > 8),
642 .i444 = (pic.p.layout == DAV2D_PIXEL_LAYOUT_I444) };
643 rc = av2_convert(av2, &planes, pDstData, DstFormat, nDstStep, nDstWidth, nDstHeight,
644 regionRects, numRegionRect);
645 }
646 break;
647 default:
648 WLog_Print(av2->log, WLOG_ERROR, "dav2d picture layout %u not supported", pic.p.layout);
649 break;
650 }
651 dav2d_picture_unref(&pic);
652 return (rc == TRUE) ? TRUE : rc;
653}
654#endif
655
656INT32 freerdp_av2_decompress(FREERDP_AV2_CONTEXT* av2, const BYTE* pSrcData, UINT32 SrcSize,
657 BYTE* pDstData, DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth,
658 UINT32 nDstHeight, const RECTANGLE_16* regionRects,
659 UINT32 numRegionRect)
660{
661 WINPR_ASSERT(av2);
662 if (av2->encoder)
663 {
664 WLog_Print(av2->log, WLOG_ERROR, "av2->encoder: %d", av2->encoder);
665 return -1;
666 }
667
668 if (!areRectsValid(av2->log, nDstWidth, nDstHeight, regionRects, numRegionRect))
669 return -2;
670
671#if defined(AV2_DECODER_DAV2D)
672 Dav2dData data = WINPR_C_ARRAY_INIT;
673 const int wrc = dav2d_data_wrap(&data, pSrcData, SrcSize, av2_dav2d_data_free, nullptr);
674 if (wrc < 0)
675 {
676 WLog_Print(av2->log, WLOG_WARN, "dav2d_data_wrap: %d", wrc);
677 return -1;
678 }
679
680 while (data.sz > 0)
681 {
682 const int src = dav2d_send_data(av2->dav2d, &data);
683 if ((src < 0) && (src != DAV2D_ERR(EAGAIN)))
684 {
685 WLog_Print(av2->log, WLOG_WARN, "dav2d_send_data: %d", src);
686 dav2d_data_unref(&data);
687 return -1;
688 }
689
690 const INT32 rc = av2_dav2d_drain_one(av2, pDstData, DstFormat, nDstStep, nDstWidth,
691 nDstHeight, regionRects, numRegionRect);
692 if ((rc != TRUE) && (rc != FALSE))
693 {
694 dav2d_data_unref(&data);
695 return rc;
696 }
697 }
698
699 /* Drain any picture the decoder buffered internally before it consumed the last input. */
700 for (;;)
701 {
702 const INT32 rc = av2_dav2d_drain_one(av2, pDstData, DstFormat, nDstStep, nDstWidth,
703 nDstHeight, regionRects, numRegionRect);
704 if (rc == FALSE)
705 break;
706 if (rc != TRUE)
707 return rc;
708 }
709
710 return TRUE;
711#elif defined(WITH_LIBAVM)
712 const avm_codec_err_t rc = avm_codec_decode(&av2->ctx, pSrcData, SrcSize, nullptr);
713 if (rc != AVM_CODEC_OK)
714 {
715 WLog_Print(av2->log, WLOG_WARN, "avm_codec_decode: %s", avm_codec_err_to_string(rc));
716 return -1;
717 }
718
719 avm_image_t* img = nullptr;
720 avm_codec_iter_t iter = nullptr;
721 while ((img = avm_codec_get_frame(&av2->ctx, &iter)) != nullptr)
722 {
723 const avm_img_fmt_t fmt = img->fmt & ~AVM_IMG_FMT_HIGHBITDEPTH;
724 if ((fmt != AVM_IMG_FMT_I420) && (fmt != AVM_IMG_FMT_I444))
725 {
726 WLog_Print(av2->log, WLOG_ERROR, "img->fmt %d not supported", img->fmt);
727 return -1;
728 }
729
730 /* bit_depth is the coded depth; a high bit depth buffer may still carry 8 bit samples */
731 av2_planes planes = { .data = { img->planes[0], img->planes[1], img->planes[2] },
732 .stride = { (UINT32)img->stride[0], (UINT32)img->stride[1],
733 (UINT32)img->stride[2] },
734 .width = img->d_w,
735 .height = img->d_h,
736 .bpc = img->bit_depth,
737 .highbd = (img->fmt & AVM_IMG_FMT_HIGHBITDEPTH) != 0,
738 .i444 = (fmt == AVM_IMG_FMT_I444) };
739
740 const INT32 crc = av2_convert(av2, &planes, pDstData, DstFormat, nDstStep, nDstWidth,
741 nDstHeight, regionRects, numRegionRect);
742 if (crc != TRUE)
743 return crc;
744 }
745
746 return TRUE;
747#endif
748}
749
750#if defined(WITH_LIBAVM)
751/* Decoder threads: with row-mt they share the superblock rows of a tile. Measured on an Apple M5
752 * (libavm 20261106.1, 1080p/1440p screen content): 1.5-1.6x at 4 threads, flat above 3-4. */
753#define AV2_AVM_DECODER_MAX_THREADS 4
754
755static unsigned int av2_avm_decoder_threads(void)
756{
757#if defined(__EMSCRIPTEN__)
758 /* libavm's default, no workers: in the browser it has threads only where the page is built
759 * for them */
760 return 0;
761#else
762 SYSTEM_INFO sysInfo = WINPR_C_ARRAY_INIT;
763 GetNativeSystemInfo(&sysInfo);
764 if (sysInfo.dwNumberOfProcessors < 1)
765 return 1;
766 if (sysInfo.dwNumberOfProcessors > AV2_AVM_DECODER_MAX_THREADS)
767 return AV2_AVM_DECODER_MAX_THREADS;
768 return sysInfo.dwNumberOfProcessors;
769#endif
770}
771#endif
772
773BOOL freerdp_av2_context_reset(FREERDP_AV2_CONTEXT* av2, UINT32 width, UINT32 height)
774{
775 if (av2->encoder)
776 {
777#if defined(AV2_ENCODER_LIBAVM)
778 av2->ecfg.g_w = width;
779 av2->ecfg.g_h = height;
780
781 if (av2->initialized)
782 {
783 const avm_codec_err_t rc = avm_codec_destroy(&av2->ctx);
784 av2->initialized = false;
785 if (rc != AVM_CODEC_OK)
786 {
787 WLog_Print(av2->log, WLOG_WARN, "avm_codec_destroy: %s",
788 avm_codec_err_to_string(rc));
789 return FALSE;
790 }
791 }
792
793 const avm_codec_err_t rc =
794 avm_codec_enc_init(&av2->ctx, av2->iface, &av2->ecfg, av2->flags);
795 if (rc != AVM_CODEC_OK)
796 {
797 WLog_Print(av2->log, WLOG_WARN, "avm_codec_enc_init: %s", avm_codec_err_to_string(rc));
798 return FALSE;
799 }
800 av2->initialized = true;
801
802 /* A remote desktop: screen content, the fastest preset of the usage unless set */
803 int cpuUsed = (av2->ecfg.g_usage == AVM_USAGE_REALTIME) ? 9 : 6;
804 if (av2->speed != UINT32_MAX)
805 cpuUsed = (int)av2->speed;
806 if ((avm_codec_control(&av2->ctx, AVME_SET_CPUUSED, cpuUsed) != AVM_CODEC_OK) &&
807 (cpuUsed > 9))
808 {
809 /* speed 10 is newer than libavm v1.0.0, and realtime only */
810 WLog_Print(av2->log, WLOG_WARN, "AVME_SET_CPUUSED %d refused: 9 instead", cpuUsed);
811 cpuUsed = 9;
812 if (!av2_avm_control(av2, AVME_SET_CPUUSED, "AVME_SET_CPUUSED", cpuUsed))
813 return FALSE;
814 }
815 if (!av2_avm_control(av2, AV2E_SET_TUNE_CONTENT, "AV2E_SET_TUNE_CONTENT",
816 AVM_CONTENT_SCREEN) ||
817 !av2_avm_control(av2, AV2E_SET_ROW_MT, "AV2E_SET_ROW_MT", 1) ||
818 !av2_avm_control(av2, AV2E_SET_TILE_COLUMNS, "AV2E_SET_TILE_COLUMNS",
819 (int)av2->tileColumns))
820 return FALSE;
821 /* Q: the quantizer of every frame; CQ: the best it may get, a bit rate on top */
822 if (((av2->ecfg.rc_end_usage == AVM_Q) || (av2->ecfg.rc_end_usage == AVM_CQ)) &&
823 (av2->qp > 0) && !av2_avm_control(av2, AVME_SET_QP, "AVME_SET_QP", (int)av2->qp))
824 return FALSE;
825
826 /* Reduced reference frame motion vectors: screen content decodes 3-4 % faster for about
827 * the same bytes and quality (Apple M5, libavm 20261106.1). A sequence header field,
828 * set by its key: the encoder_cfg field only counts with init_by_cfg_file. */
829 if (avm_codec_set_option(&av2->ctx, "reduced-ref-frame-mvs-mode", "1") != AVM_CODEC_OK)
830 WLog_Print(av2->log, WLOG_WARN, "reduced-ref-frame-mvs-mode refused: %s",
831 avm_codec_error(&av2->ctx));
832 /* Each reference frame is kept whole, in 16 bits with a border: the setting that
833 * decides most of the encoder's memory. libavm has no control for it, only the
834 * option avmenc's --dpb-size sets, before the first frame. */
835 if (av2->dpbSize > 0)
836 {
837 char dpb[8] = WINPR_C_ARRAY_INIT;
838 (void)_snprintf(dpb, sizeof(dpb), "%" PRIu32, av2->dpbSize);
839 if (avm_codec_set_option(&av2->ctx, "dpb-size", dpb) != AVM_CODEC_OK)
840 WLog_Print(av2->log, WLOG_WARN, "dpb-size %s refused: %s", dpb,
841 avm_codec_error(&av2->ctx));
842 }
843
844 av2->framecount = 0;
845 av2->eflags = AVM_EFLAG_FORCE_KF;
846#else
847 WLog_Print(av2->log, WLOG_ERROR,
848 "AV2 encoder requires libavm with its encoder, recompile with '-DWITH_AVM=ON'");
849 return FALSE;
850#endif
851 }
852 else
853 {
854#if defined(AV2_DECODER_DAV2D)
855 if (av2->initialized)
856 {
857 dav2d_close(&av2->dav2d);
858 av2->initialized = false;
859 }
860
861 Dav2dSettings settings = WINPR_C_ARRAY_INIT;
862 dav2d_default_settings(&settings);
863 /* Single-threaded: freerdp_av2_decompress() needs its picture back synchronously,
864 * not held back by frame-parallel decoding. */
865 settings.n_threads = 1;
866 settings.max_frame_delay = 1;
867
868 const int rc = dav2d_open(&av2->dav2d, &settings);
869 if (rc < 0)
870 {
871 WLog_Print(av2->log, WLOG_WARN, "dav2d_open: %d", rc);
872 return FALSE;
873 }
874 av2->initialized = true;
875#elif defined(WITH_LIBAVM)
876 if (av2->initialized)
877 {
878 av2_avm_destroy(av2);
879 av2->initialized = false;
880 }
881
882 av2->dcfg.w = width;
883 av2->dcfg.h = height;
884 av2->dcfg.threads = av2_avm_decoder_threads();
885 const avm_codec_err_t rc =
886 avm_codec_dec_init(&av2->ctx, av2->iface, &av2->dcfg, av2->flags);
887 if (rc != AVM_CODEC_OK)
888 {
889 WLog_Print(av2->log, WLOG_WARN, "avm_codec_dec_init: %s", avm_codec_err_to_string(rc));
890 return FALSE;
891 }
892 av2->initialized = true;
893 /* Without row-mt libavm starts no worker for a single tile, the threads stay idle. The
894 * workers run inside avm_codec_decode(): the picture still comes out of the same call. */
895 if ((av2->dcfg.threads > 1) &&
896 (avm_codec_control(&av2->ctx, AV2D_SET_ROW_MT, 1) != AVM_CODEC_OK))
897 WLog_Print(av2->log, WLOG_WARN, "AV2D_SET_ROW_MT refused: %s",
898 avm_codec_error(&av2->ctx));
899#else
900 WLog_Print(av2->log, WLOG_WARN,
901 "This build does not support AV2 decoding. Recompile with '-DWITH_DAV2D=ON' "
902 "or '-DWITH_AVM=ON'");
903 return FALSE;
904#endif
905 }
906
907 av2->yuvWidth = width;
908 av2->yuvStride[0] = width;
909
910 const size_t pad = av2->yuvStride[0] % 16;
911 if (pad != 0)
912 av2->yuvStride[0] += 16 - pad;
913 av2->yuvStride[1] = av2->yuvStride[0];
914 av2->yuvStride[2] = av2->yuvStride[0];
915
916 av2->yuvHeight = height;
917 winpr_aligned_free(av2->yuvdata[0]);
918 winpr_aligned_free(av2->yuvdata[1]);
919 winpr_aligned_free(av2->yuvdata[2]);
920 av2->yuvdata[0] = winpr_aligned_malloc(1ull * av2->yuvStride[0] * av2->yuvHeight, 64);
921 av2->yuvdata[1] = winpr_aligned_malloc(1ull * av2->yuvStride[1] * av2->yuvHeight, 64);
922 av2->yuvdata[2] = winpr_aligned_malloc(1ull * av2->yuvStride[2] * av2->yuvHeight, 64);
923 return (av2->yuvdata[0] != nullptr) && (av2->yuvdata[1] != nullptr) &&
924 (av2->yuvdata[2] != nullptr);
925}
926
927void freerdp_av2_context_free(FREERDP_AV2_CONTEXT* av2)
928{
929 if (!av2)
930 return;
931
932 if (av2->initialized)
933 {
934 if (av2->encoder)
935 {
936#if defined(WITH_LIBAVM)
937 av2_avm_destroy(av2);
938#endif
939 }
940 else
941 {
942#if defined(AV2_DECODER_DAV2D)
943 dav2d_close(&av2->dav2d);
944#elif defined(WITH_LIBAVM)
945 av2_avm_destroy(av2);
946#endif
947 }
948 }
949 winpr_aligned_free(av2->yuvdata[0]);
950 winpr_aligned_free(av2->yuvdata[1]);
951 winpr_aligned_free(av2->yuvdata[2]);
952 free(av2);
953}
954
955FREERDP_AV2_CONTEXT* freerdp_av2_context_new(BOOL Compressor)
956{
957 FREERDP_AV2_CONTEXT* ctx = calloc(1, sizeof(FREERDP_AV2_CONTEXT));
958 if (!ctx)
959 return nullptr;
960 ctx->encoder = Compressor;
961 ctx->log = WLog_Get(TAG);
962 if (!ctx->log)
963 goto fail;
964
965 if (Compressor)
966 {
967#if defined(AV2_ENCODER_LIBAVM)
968 ctx->iface = avm_codec_av2_cx();
969 if (!ctx->iface)
970 {
971 WLog_Print(ctx->log, WLOG_ERROR, "avm_codec_av2_cx() nullptr");
972 goto fail;
973 }
974
975 const avm_codec_err_t rc =
976 avm_codec_enc_config_default(ctx->iface, &ctx->ecfg, AVM_USAGE_REALTIME);
977 if (rc != AVM_CODEC_OK)
978 {
979 WLog_Print(ctx->log, WLOG_ERROR, "avm_codec_enc_config_default() %s",
980 avm_codec_err_to_string(rc));
981 goto fail;
982 }
983 /* A frame out for each frame in, and no key frames but the first */
984 ctx->ecfg.g_lag_in_frames = 0;
985 ctx->ecfg.kf_mode = AVM_KF_DISABLED;
986 ctx->ecfg.g_profile = AV2_PROFILE_MAIN_420_10_IP0;
987 ctx->ecfg.g_bit_depth = AVM_BITS_8;
988 ctx->ecfg.g_input_bit_depth = 8;
989 ctx->speed = UINT32_MAX;
990#else
991 WLog_Print(ctx->log, WLOG_ERROR,
992 "AV2 encoder requires libavm with its encoder, recompile with '-DWITH_AVM=ON'");
993 goto fail;
994#endif
995 }
996#if defined(WITH_LIBAVM)
997 else
998 {
999 ctx->iface = avm_codec_av2_dx();
1000 if (!ctx->iface)
1001 {
1002 WLog_Print(ctx->log, WLOG_ERROR, "avm_codec_av2_dx() nullptr");
1003 goto fail;
1004 }
1005 }
1006#endif
1007
1008 return ctx;
1009
1010fail:
1011 freerdp_av2_context_free(ctx);
1012 return nullptr;
1013}
1014#else
1015
1016struct S_FREERDP_AV2_CONTEXT
1017{
1018 BOOL Compressor;
1019 wLog* log;
1020};
1021
1022BOOL freerdp_av2_context_set_option(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2,
1023 WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT_OPTION option,
1024 WINPR_ATTR_UNUSED UINT32 value)
1025{
1026 return FALSE;
1027}
1028
1029UINT32 freerdp_av2_context_get_option(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2,
1030 WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT_OPTION option)
1031{
1032 return 0;
1033}
1034
1035INT32 freerdp_av2_compress(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2,
1036 WINPR_ATTR_UNUSED const BYTE* pSrcData,
1037 WINPR_ATTR_UNUSED DWORD SrcFormat, WINPR_ATTR_UNUSED UINT32 nSrcStep,
1038 WINPR_ATTR_UNUSED UINT32 nSrcWidth, WINPR_ATTR_UNUSED UINT32 nSrcHeight,
1039 WINPR_ATTR_UNUSED const RECTANGLE_16* regionRect,
1040 WINPR_ATTR_UNUSED BYTE** ppDstData, WINPR_ATTR_UNUSED UINT32* pDstSize,
1041 WINPR_ATTR_UNUSED RDPGFX_H264_METABLOCK* meta)
1042{
1043 WINPR_ASSERT(av2);
1044 WLog_Print(av2->log, WLOG_ERROR,
1045 "This build does not support AV2 codec. Recompile with '-DWITH_AVM=ON' or "
1046 "'-DWITH_DAV2D=ON'");
1047 return -1;
1048}
1049
1050INT32 freerdp_av2_decompress(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2,
1051 WINPR_ATTR_UNUSED const BYTE* pSrcData,
1052 WINPR_ATTR_UNUSED UINT32 SrcSize, WINPR_ATTR_UNUSED BYTE* pDstData,
1053 WINPR_ATTR_UNUSED DWORD DstFormat, WINPR_ATTR_UNUSED UINT32 nDstStep,
1054 WINPR_ATTR_UNUSED UINT32 nDstWidth,
1055 WINPR_ATTR_UNUSED UINT32 nDstHeight,
1056 WINPR_ATTR_UNUSED const RECTANGLE_16* regionRects,
1057 WINPR_ATTR_UNUSED UINT32 numRegionRect)
1058{
1059 WINPR_ASSERT(av2);
1060 WLog_Print(av2->log, WLOG_ERROR,
1061 "This build does not support AV2 codec. Recompile with '-DWITH_AVM=ON' or "
1062 "'-DWITH_DAV2D=ON'");
1063 return -1;
1064}
1065
1066BOOL freerdp_av2_context_reset(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2,
1067 WINPR_ATTR_UNUSED UINT32 width, WINPR_ATTR_UNUSED UINT32 height)
1068{
1069 WINPR_ASSERT(av2);
1070 WLog_Print(av2->log, WLOG_WARN,
1071 "This build does not support AV2 codec. Recompile with '-DWITH_AVM=ON' or "
1072 "'-DWITH_DAV2D=ON'");
1073 return FALSE;
1074}
1075
1076void freerdp_av2_context_free(WINPR_ATTR_UNUSED FREERDP_AV2_CONTEXT* av2)
1077{
1078 free(av2);
1079}
1080
1081FREERDP_AV2_CONTEXT* freerdp_av2_context_new(BOOL Compressor)
1082{
1083 FREERDP_AV2_CONTEXT* ctx = calloc(1, sizeof(FREERDP_AV2_CONTEXT));
1084 if (!ctx)
1085 return nullptr;
1086 ctx->Compressor = Compressor;
1087 ctx->log = WLog_Get(TAG);
1088 return ctx;
1089}
1090#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