FreeRDP
Loading...
Searching...
No Matches
TestFreeRDPCodecH264.c
1
2#include <math.h>
3#include <string.h>
4#include <stdlib.h>
5#include <errno.h>
6
7#include <winpr/sysinfo.h>
8
9#include <freerdp/freerdp.h>
10#include <freerdp/codec/color.h>
11
12static const char* print_ns(UINT64 start, UINT64 end, char* buffer, size_t len)
13{
14 const UINT64 diff = end - start;
15 const unsigned ns = diff % 1000;
16 const unsigned us = (diff / 1000) % 1000;
17 const unsigned ms = (diff / 1000000) % 1000;
18 const unsigned s = (diff / 1000000000ULL) % 1000;
19 (void)_snprintf(buffer, len, "%03u %03u %03u %03uns", s, ms, us, ns);
20 return buffer;
21}
22
23static BOOL testContextOptions(BOOL compressor, uint32_t width, uint32_t height)
24{
25 BOOL rc = FALSE;
26 const UINT64 start = winpr_GetUnixTimeNS();
27 H264_CONTEXT* h264 = h264_context_new(FALSE);
28 if (!h264)
29 return FALSE;
30
31 struct optpair_s
32 {
33 H264_CONTEXT_OPTION opt;
34 uint32_t val;
35 };
36 const struct optpair_s optpair[] = { { H264_CONTEXT_OPTION_RATECONTROL, H264_RATECONTROL_VBR },
37 { H264_CONTEXT_OPTION_BITRATE, 2323 },
38 { H264_CONTEXT_OPTION_FRAMERATE, 23 },
39 { H264_CONTEXT_OPTION_QP, 21 },
40 { H264_CONTEXT_OPTION_USAGETYPE, 23 } };
41 for (size_t x = 0; x < ARRAYSIZE(optpair); x++)
42 {
43 const struct optpair_s* cur = &optpair[x];
44 if (!h264_context_set_option(h264, cur->opt, cur->val))
45 goto fail;
46 }
47 if (!h264_context_reset(h264, width, height))
48 goto fail;
49
50 rc = TRUE;
51fail:
52 h264_context_free(h264);
53 const UINT64 end = winpr_GetUnixTimeNS();
54
55 char buffer[64] = { 0 };
56 printf("[%s] %" PRIu32 "x%" PRIu32 " took %s\n", __func__, width, height,
57 print_ns(start, end, buffer, sizeof(buffer)));
58 return rc;
59}
60
61static void* allocRGB(uint32_t format, uint32_t width, uint32_t height, uint32_t* pstride)
62{
63 const size_t bpp = FreeRDPGetBytesPerPixel(format);
64 const size_t stride = bpp * width + 32;
65 WINPR_ASSERT(pstride);
66 *pstride = WINPR_ASSERTING_INT_CAST(uint32_t, stride);
67
68 uint8_t* rgb = calloc(stride, height);
69 if (!rgb)
70 return NULL;
71
72 for (size_t x = 0; x < height; x++)
73 {
74 winpr_RAND(&rgb[x * stride], width * bpp);
75 }
76 return rgb;
77}
78
79static BOOL compareRGB(const uint8_t* src, const uint8_t* dst, uint32_t format, size_t width,
80 size_t stride, size_t height)
81{
82 const size_t bpp = FreeRDPGetBytesPerPixel(format);
83 for (size_t y = 0; y < height; y++)
84 {
85 const uint8_t* csrc = &src[y * stride];
86 const uint8_t* cdst = &dst[y * stride];
87 const int rc = memcmp(csrc, cdst, width * bpp);
88 // TODO: Both, AVC420 encoding and decoding are lossy.
89 // TODO: Find a proper error margin to check for
90#if 0
91 if (rc != 0)
92 return FALSE;
93#endif
94 }
95 return TRUE;
96}
97
98static BOOL testEncode(uint32_t format, uint32_t width, uint32_t height)
99{
100 BOOL rc = FALSE;
101 void* src = NULL;
102 void* out = NULL;
103 RDPGFX_H264_METABLOCK meta = { 0 };
104 H264_CONTEXT* h264 = h264_context_new(TRUE);
105 H264_CONTEXT* h264dec = h264_context_new(FALSE);
106 if (!h264 || !h264dec)
107 goto fail;
108
109 if (!h264_context_reset(h264, width, height))
110 goto fail;
111 if (!h264_context_reset(h264dec, width, height))
112 goto fail;
113
114 uint32_t stride = 0;
115 uint32_t ostride = 0;
116 src = allocRGB(format, width, height, &stride);
117 out = allocRGB(format, width, height, &ostride);
118 if (!src || !out || (stride < width) || (stride != ostride))
119 goto fail;
120
121 const RECTANGLE_16 rect = { .left = 0, .top = 0, .right = width, .bottom = height };
122 uint32_t dstsize = 0;
123 uint8_t* dst = NULL;
124 if (avc420_compress(h264, src, format, stride, width, height, &rect, &dst, &dstsize, &meta) < 0)
125 goto fail;
126 if ((dstsize == 0) || !dst)
127 goto fail;
128
129 if (avc420_decompress(h264dec, dst, dstsize, out, format, stride, width, height, &rect, 1) < 0)
130 goto fail;
131
132 rc = compareRGB(src, out, format, width, stride, height);
133fail:
134 h264_context_free(h264);
135 h264_context_free(h264dec);
136 free_h264_metablock(&meta);
137 free(src);
138 free(out);
139 return rc;
140}
141
142int TestFreeRDPCodecH264(int argc, char* argv[])
143{
144 WINPR_UNUSED(argc);
145 WINPR_UNUSED(argv);
146
147 UINT32 width = 124;
148 UINT32 height = 54;
149 const UINT32 formats[] = {
150 PIXEL_FORMAT_ABGR15, PIXEL_FORMAT_ARGB15, PIXEL_FORMAT_BGR15, PIXEL_FORMAT_BGR16,
151 PIXEL_FORMAT_BGR24, PIXEL_FORMAT_RGB15, PIXEL_FORMAT_RGB16, PIXEL_FORMAT_RGB24,
152 PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_ARGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_XRGB32,
153 PIXEL_FORMAT_BGRA32, PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_BGRX32, PIXEL_FORMAT_RGBX32,
154 };
155
156 if (argc == 3)
157 {
158 errno = 0;
159 width = strtoul(argv[1], NULL, 0);
160 height = strtoul(argv[2], NULL, 0);
161 if ((errno != 0) || (width == 0) || (height == 0))
162 {
163 char buffer[128] = { 0 };
164 (void)fprintf(stderr, "%s failed: width=%" PRIu32 ", height=%" PRIu32 ", errno=%s\n",
165 __func__, width, height, winpr_strerror(errno, buffer, sizeof(buffer)));
166 return -1;
167 }
168 }
169
170#if !defined(WITH_MEDIACODEC) && !defined(WITH_MEDIA_FOUNDATION) && !defined(WITH_OPENH264) && \
171 !defined(WITH_VIDEO_FFMPEG)
172 (void)fprintf(stderr, "[%s] skipping, no H264 encoder/decoder support compiled in\n", __func__);
173 return 0;
174#endif
175
176 if (!testContextOptions(FALSE, width, height))
177 return -1;
178 if (!testContextOptions(TRUE, width, height))
179 return -1;
180
181 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
182 {
183 const UINT32 SrcFormat = formats[x];
184 for (size_t y = 0; y < ARRAYSIZE(formats); y++)
185 {
186 if (!testEncode(SrcFormat, width, height))
187 return -1;
188 }
189 }
190
191 return 0;
192}