FreeRDP
Loading...
Searching...
No Matches
TestFreeRDPCodecAV1.c
1
2#include <stdlib.h>
3
4#include <freerdp/freerdp.h>
5#include <freerdp/codec/color.h>
6#include <freerdp/codec/av1.h>
7
8static void* allocRGB(uint32_t format, uint32_t width, uint32_t height, uint32_t* pstride)
9{
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);
14
15 uint8_t* rgb = calloc(stride, height);
16 if (!rgb)
17 return nullptr;
18
19 for (size_t x = 0; x < height; x++)
20 {
21 if (winpr_RAND(&rgb[x * stride], width * bpp) < 0)
22 {
23 free(rgb);
24 return nullptr;
25 }
26 }
27 return rgb;
28}
29
30static BOOL testEncodeDecode(uint32_t format, uint32_t width, uint32_t height)
31{
32 BOOL rc = FALSE;
33 void* src = nullptr;
34 void* out = nullptr;
35 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
36 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
37 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
38 if (!enc || !dec)
39 goto fail;
40
41 if (!freerdp_av1_context_reset(enc, width, height))
42 goto fail;
43 if (!freerdp_av1_context_reset(dec, width, height))
44 goto fail;
45
46 uint32_t stride = 0;
47 uint32_t ostride = 0;
48 src = allocRGB(format, width, height, &stride);
49 out = allocRGB(format, width, height, &ostride);
50 if (!src || !out || (stride < width) || (stride != ostride))
51 goto fail;
52
53 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
54 uint8_t* dst = nullptr;
55 uint32_t dstsize = 0;
56 if (freerdp_av1_compress(enc, src, format, stride, width, height, &rect, &dst, &dstsize,
57 &meta) < 0)
58 goto fail;
59 if ((dstsize == 0) || !dst)
60 goto fail;
61
62 /* AV1 is a lossy codec, so this only checks that the bitstream produced by the
63 * encoder (always libaom) round-trips through whichever decoder backend is active
64 * (dav1d or libaom) without error. It does not compare decoded pixels. */
65 rc = freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect, 1) >=
66 0;
67
68fail:
69 freerdp_av1_context_free(enc);
70 freerdp_av1_context_free(dec);
71 free_h264_metablock(&meta);
72 free(src);
73 free(out);
74 return rc;
75}
76
77/* A short stream at a desktop size, a bar moving over a gradient: every frame decodes, through
78 * whichever backend is active (a hardware decoder too: they don't take tiny pictures), and the
79 * picture stays close to the source. */
80static BOOL testSequence(uint32_t width, uint32_t height, uint32_t profile)
81{
82 const uint32_t format = PIXEL_FORMAT_BGRX32;
83 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
84 const uint32_t stride = width * bpp;
85 BOOL rc = FALSE;
86 uint8_t* src = calloc(stride, height);
87 uint8_t* out = calloc(stride, height);
88 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
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)
92 goto fail;
93
94 if (!freerdp_av1_context_reset(enc, width, height) ||
95 !freerdp_av1_context_reset(dec, width, height))
96 goto fail;
97 if (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_PROFILE, profile))
98 goto fail;
99
100 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
101 for (uint32_t frame = 0; frame < 8; frame++)
102 {
103 const uint32_t barLeft = frame * (width / 16);
104 for (uint32_t y = 0; y < height; y++)
105 {
106 for (uint32_t x = 0; x < width; x++)
107 {
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)))
114 goto fail;
115 }
116 }
117
118 uint8_t* dst = nullptr;
119 uint32_t dstsize = 0;
120 if (freerdp_av1_compress(enc, src, format, stride, width, height, &rect, &dst, &dstsize,
121 &meta) < 0)
122 goto fail;
123 if (freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect,
124 1) < 0)
125 {
126 (void)fprintf(stderr, "[%s] frame %" PRIu32 " did not decode\n", __func__, frame);
127 goto fail;
128 }
129
130 UINT64 sum = 0;
131 for (uint32_t y = 0; y < height; y++)
132 {
133 for (uint32_t x = 0; x < width; x++)
134 {
135 BYTE r1 = 0;
136 BYTE g1 = 0;
137 BYTE b1 = 0;
138 BYTE r2 = 0;
139 BYTE g2 = 0;
140 BYTE b2 = 0;
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);
146 }
147 }
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);
153 if (mean > 8.0)
154 goto fail;
155 }
156 rc = TRUE;
157
158fail:
159 freerdp_av1_context_free(enc);
160 freerdp_av1_context_free(dec);
161 free_h264_metablock(&meta);
162 free(src);
163 free(out);
164 return rc;
165}
166
167/* Encodes a picture, whose chroma differs within each 2x2 block, in YUV444 (AV1 profile 1)
168 * and checks the colours of the decoded picture. A YUV444 conversion, that treats the first
169 * chroma sample of a 2x2 block as the average of the block (as done for AVC444), corrupts
170 * the first pixel of each 2x2 block. */
171static BOOL testYUV444Colours(uint32_t width, uint32_t height)
172{
173 const uint32_t format = PIXEL_FORMAT_BGRX32;
174 const uint32_t bpp = FreeRDPGetBytesPerPixel(format);
175 const uint32_t stride = width * bpp;
176 BOOL rc = FALSE;
177 uint8_t* src = calloc(stride, height);
178 uint8_t* out = calloc(stride, height);
179 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
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)
183 goto fail;
184
185 if (!freerdp_av1_context_reset(enc, width, height) ||
186 !freerdp_av1_context_reset(dec, width, height))
187 goto fail;
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,
190 FREERDP_AV1_VBR) ||
191 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_BITRATE, 100000))
192 goto fail;
193
194 for (uint32_t y = 0; y < height; y++)
195 {
196 for (uint32_t x = 0; x < width; x++)
197 {
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))
202 goto fail;
203 }
204 }
205
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,
210 &meta) < 0)
211 goto fail;
212 if (freerdp_av1_decompress(dec, dst, dstsize, out, format, stride, width, height, &rect, 1) < 0)
213 goto fail;
214
215 int maxDiff = 0;
216 for (uint32_t y = 0; y < height; y++)
217 {
218 for (uint32_t x = 0; x < width; x++)
219 {
220 BYTE r1 = 0;
221 BYTE g1 = 0;
222 BYTE b1 = 0;
223 BYTE r2 = 0;
224 BYTE g2 = 0;
225 BYTE b2 = 0;
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));
233 }
234 }
235
236 (void)fprintf(stderr, "[%s] %" PRIu32 "x%" PRIu32 ": maximum colour difference %d\n", __func__,
237 width, height, maxDiff);
238 rc = maxDiff <= 32;
239
240fail:
241 freerdp_av1_context_free(enc);
242 freerdp_av1_context_free(dec);
243 free_h264_metablock(&meta);
244 free(src);
245 free(out);
246 return rc;
247}
248
249/* The tuning options: kept before the first reset, checked, applied by it, and a frame comes
250 * out in constant quality; out of range values are refused. */
251static BOOL testTuning(uint32_t width, uint32_t height)
252{
253 BOOL rc = FALSE;
254 void* src = nullptr;
255 void* out = nullptr;
256 RDPGFX_H264_METABLOCK meta = WINPR_C_ARRAY_INIT;
257 FREERDP_AV1_CONTEXT* enc = freerdp_av1_context_new(TRUE);
258 FREERDP_AV1_CONTEXT* dec = freerdp_av1_context_new(FALSE);
259 if (!enc || !dec)
260 goto fail;
261
262 const struct
263 {
264 FREERDP_AV1_CONTEXT_OPTION option;
265 UINT32 value;
266 UINT32 bad;
267 } options[] = {
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 },
271 };
272 if (!freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL, FREERDP_AV1_Q))
273 goto fail;
274 for (size_t x = 0; x < ARRAYSIZE(options); x++)
275 {
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))
279 goto fail;
280 }
281
282 if (!freerdp_av1_context_reset(enc, width, height) ||
283 !freerdp_av1_context_reset(dec, width, height))
284 goto fail;
285 if (freerdp_av1_context_get_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL) !=
286 FREERDP_AV1_Q)
287 goto fail;
288
289 uint32_t stride = 0;
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))
294 goto fail;
295
296 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
297 for (size_t frame = 0; frame < 3; frame++)
298 {
299 /* the next frames after a change once the encoder runs: it starts again, the last one
300 * in constrained quality, the quality up to a bit rate */
301 if ((frame == 1) &&
302 !freerdp_av1_context_set_option(enc, FREERDP_AV1_CONTEXT_OPTION_CQ_LEVEL, 32))
303 goto fail;
304 if ((frame == 2) &&
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,
307 FREERDP_AV1_CQ) ||
308 (freerdp_av1_context_get_option(enc, FREERDP_AV1_CONTEXT_OPTION_RATECONTROL) !=
309 FREERDP_AV1_CQ)))
310 goto fail;
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)
316 goto fail;
317 free_h264_metablock(&meta);
318 if (freerdp_av1_decompress(dec, dst, dstsize, out, PIXEL_FORMAT_BGRA32, stride, width,
319 height, &rect, 1) < 0)
320 goto fail;
321 }
322 rc = TRUE;
323
324fail:
325 freerdp_av1_context_free(enc);
326 freerdp_av1_context_free(dec);
327 free_h264_metablock(&meta);
328 free(src);
329 free(out);
330 return rc;
331}
332
333int TestFreeRDPCodecAV1(int argc, char* argv[])
334{
335 WINPR_UNUSED(argc);
336 WINPR_UNUSED(argv);
337
338#if !defined(WITH_LIBAOM)
339 (void)fprintf(stderr, "[%s] skipping, no AV1 encoder support compiled in\n", __func__);
340 return 0;
341#else
342 const UINT32 width = 124;
343 const UINT32 height = 54;
344 const UINT32 formats[] = { PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_RGB16 };
345
346 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
347 {
348 if (!testEncodeDecode(formats[x], width, height))
349 return -1;
350 }
351
352 if (!testYUV444Colours(64, 64))
353 return -1;
354
355 if (!testSequence(1280, 720, 0) || !testSequence(1280, 720, 1))
356 return -1;
357
358 if (!testTuning(128, 64))
359 return -1;
360
361 return 0;
362#endif
363}