4#include <freerdp/freerdp.h>
5#include <freerdp/codec/color.h>
6#include <freerdp/codec/av1.h>
8static void* allocRGB(uint32_t format, uint32_t width, uint32_t height, uint32_t* pstride)
10 const size_t bpp = FreeRDPGetBytesPerPixel(format);
11 const size_t stride = bpp * width + 32;
12 WINPR_ASSERT(pstride);
13 *pstride = WINPR_ASSERTING_INT_CAST(uint32_t, stride);
15 uint8_t* rgb = calloc(stride, height);
19 for (
size_t x = 0; x < height; x++)
21 if (winpr_RAND(&rgb[x * stride], width * bpp) < 0)
30static BOOL testEncodeDecode(uint32_t format, uint32_t width, uint32_t height)
36 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
37 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
41 if (!freerdp_av1_context_reset(enc, width, height))
43 if (!freerdp_av1_context_reset(dec, width, height))
48 src = allocRGB(format, width, height, &stride);
49 out = allocRGB(format, width, height, &ostride);
50 if (!src || !out || (stride < width) || (stride != ostride))
53 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
54 uint8_t* dst =
nullptr;
56 if (freerdp_av1_compress(enc, src, format, stride, width, height, &rect, &dst, &dstsize,
59 if ((dstsize == 0) || !dst)
65 rc = freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect, 1) >=
69 freerdp_av1_context_free(enc);
70 freerdp_av1_context_free(dec);
71 free_h264_metablock(&meta);
80static BOOL testSequence(uint32_t width, uint32_t height, uint32_t profile)
82 const uint32_t format = PIXEL_FORMAT_BGRX32;
83 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
84 const uint32_t stride = width * bpp;
86 uint8_t* src = calloc(stride, height);
87 uint8_t* out = calloc(stride, height);
89 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
90 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
91 if (!src || !out || !enc || !dec)
94 if (!freerdp_av1_context_reset(enc, width, height) ||
95 !freerdp_av1_context_reset(dec, width, height))
97 if (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_PROFILE, profile))
100 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
101 for (uint32_t frame = 0; frame < 8; frame++)
103 const uint32_t barLeft = frame * (width / 16);
104 for (uint32_t y = 0; y < height; y++)
106 for (uint32_t x = 0; x < width; x++)
108 const BOOL bar = (x >= barLeft) && (x < barLeft + width / 8);
109 const BYTE r = bar ? 0xE0 : (BYTE)(x * 255 / width);
110 const BYTE g = bar ? 0x30 : (BYTE)(y * 255 / height);
111 const BYTE b = bar ? 0x40 : 0x80;
112 if (!FreeRDPWriteColor(&src[y * stride + x * bpp], format,
113 FreeRDPGetColor(format, r, g, b, 0xFF)))
118 uint8_t* dst =
nullptr;
119 uint32_t dstsize = 0;
120 if (freerdp_av1_compress(enc, src, format, stride, width, height, &rect, &dst, &dstsize,
123 if (freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect,
126 (void)fprintf(stderr,
"[%s] frame %" PRIu32
" did not decode\n", __func__, frame);
131 for (uint32_t y = 0; y < height; y++)
133 for (uint32_t x = 0; x < width; x++)
141 FreeRDPSplitColor(FreeRDPReadColor(&src[y * stride + x * bpp], format), format, &r1,
142 &g1, &b1,
nullptr,
nullptr);
143 FreeRDPSplitColor(FreeRDPReadColor(&out[y * stride + x * bpp], format), format, &r2,
144 &g2, &b2,
nullptr,
nullptr);
145 sum += (UINT64)abs(r1 - r2) + (UINT64)abs(g1 - g2) + (UINT64)abs(b1 - b2);
148 const double mean = (double)sum / (3.0 * width * height);
149 (void)fprintf(stderr,
150 "[%s] %" PRIu32
"x%" PRIu32
" profile %" PRIu32
" frame %" PRIu32
": %" PRIu32
151 " bytes, mean colour difference %.2f\n",
152 __func__, width, height, profile, frame, dstsize, mean);
159 freerdp_av1_context_free(enc);
160 freerdp_av1_context_free(dec);
161 free_h264_metablock(&meta);
171static BOOL testYUV444Colours(uint32_t width, uint32_t height)
173 const uint32_t format = PIXEL_FORMAT_BGRX32;
174 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
175 const uint32_t stride = width * bpp;
177 uint8_t* src = calloc(stride, height);
178 uint8_t* out = calloc(stride, height);
180 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
181 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
182 if (!src || !out || !enc || !dec)
185 if (!freerdp_av1_context_reset(enc, width, height) ||
186 !freerdp_av1_context_reset(dec, width, height))
188 if (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_PROFILE, 1) ||
189 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL,
191 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_BITRATE, 100000))
194 for (uint32_t y = 0; y < height; y++)
196 for (uint32_t x = 0; x < width; x++)
198 const BOOL first = ((x % 2) == 0) && ((y % 2) == 0);
199 const uint32_t color = first ? FreeRDPGetColor(format, 0xA0, 0x60, 0x60, 0xFF)
200 : FreeRDPGetColor(format, 0x60, 0x60, 0xA0, 0xFF);
201 if (!FreeRDPWriteColor(&src[y * stride + x * bpp], format, color))
206 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
207 uint8_t* dst =
nullptr;
208 uint32_t dstsize = 0;
209 if (freerdp_av1_compress(enc, src, format, stride, width, height, &rect, &dst, &dstsize,
212 if (freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect, 1) < 0)
216 for (uint32_t y = 0; y < height; y++)
218 for (uint32_t x = 0; x < width; x++)
226 FreeRDPSplitColor(FreeRDPReadColor(&src[y * stride + x * bpp], format), format, &r1,
227 &g1, &b1,
nullptr,
nullptr);
228 FreeRDPSplitColor(FreeRDPReadColor(&out[y * stride + x * bpp], format), format, &r2,
229 &g2, &b2,
nullptr,
nullptr);
230 maxDiff = MAX(maxDiff, abs(r1 - r2));
231 maxDiff = MAX(maxDiff, abs(g1 - g2));
232 maxDiff = MAX(maxDiff, abs(b1 - b2));
236 (void)fprintf(stderr,
"[%s] %" PRIu32
"x%" PRIu32
": maximum colour difference %d\n", __func__,
237 width, height, maxDiff);
241 freerdp_av1_context_free(enc);
242 freerdp_av1_context_free(dec);
243 free_h264_metablock(&meta);
251static BOOL testTuning(uint32_t width, uint32_t height)
257 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
258 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
264 FREERDP_AV1_CONTEXT_OPTION option;
268 { FREERDP_AV1_CONTEXT_OPTION_CQ_LEVEL, 28, 64 },
269 { FREERDP_AV1_CONTEXT_OPTION_THREADS, 2, 65 },
270 { FREERDP_AV1_CONTEXT_OPTION_SPEED, 10, 13 },
272 if (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL, FREERDP_AV1_Q))
274 for (
size_t x = 0; x < ARRAYSIZE(options); x++)
276 if (freerdp_av1_context_set_option(enc, options[x].option, options[x].bad) ||
277 !freerdp_av1_context_set_option(enc, options[x].option, options[x].value) ||
278 (freerdp_av1_context_get_option(enc, options[x].option) != options[x].value))
282 if (!freerdp_av1_context_reset(enc, width, height) ||
283 !freerdp_av1_context_reset(dec, width, height))
285 if (freerdp_av1_context_get_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL) !=
290 uint32_t ostride = 0;
291 src = allocRGB(PIXEL_FORMAT_BGRA32, width, height, &stride);
292 out = allocRGB(PIXEL_FORMAT_BGRA32, width, height, &ostride);
293 if (!src || !out || (stride != ostride))
296 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
297 for (
size_t frame = 0; frame < 3; frame++)
302 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_CQ_LEVEL, 32))
305 (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_BITRATE, 500) ||
306 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL,
308 (freerdp_av1_context_get_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL) !=
311 uint8_t* dst =
nullptr;
312 uint32_t dstsize = 0;
313 if ((freerdp_av1_compress(enc, src, PIXEL_FORMAT_BGRA32, stride, width, height, &rect, &dst,
314 &dstsize, &meta) < 0) ||
315 (dstsize == 0) || !dst)
317 free_h264_metablock(&meta);
318 if (freerdp_av1_decompress(dec, dst, dstsize, out, PIXEL_FORMAT_BGRA32, stride, width,
319 height, &rect, 1) < 0)
325 freerdp_av1_context_free(enc);
326 freerdp_av1_context_free(dec);
327 free_h264_metablock(&meta);
333int TestFreeRDPCodecAV1(
int argc,
char* argv[])
338#if !defined(WITH_LIBAOM)
339 (void)fprintf(stderr,
"[%s] skipping, no AV1 encoder support compiled in\n", __func__);
342 const UINT32 width = 124;
343 const UINT32 height = 54;
344 const UINT32 formats[] = { PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_RGB16 };
346 for (
size_t x = 0; x < ARRAYSIZE(formats); x++)
348 if (!testEncodeDecode(formats[x], width, height))
352 if (!testYUV444Colours(64, 64))
355 if (!testSequence(1280, 720, 0) || !testSequence(1280, 720, 1))
358 if (!testTuning(128, 64))