Debian Codegraph @

test/testyuv.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "SDL.h"
17 #include "SDL_test_font.h"
18 #include "testyuv_cvt.h"
19
20 /* 422 (YUY2, etc) formats are the largest */
21 #define MAX_YUV_SURFACE_SIZE(W, H, P) (H * 4 * (W + P + 1) / 2)
22
23 /* Return true if the YUV format is packed pixels */
24 static SDL_bool is_packed_yuv_format(Uint32 format)
25 {
26 return format == SDL_PIXELFORMAT_YUY2 || format == SDL_PIXELFORMAT_UYVY || format == SDL_PIXELFORMAT_YVYU;
27 }
28
29 /* Create a surface with a good pattern for verifying YUV conversion */
30 static SDL_Surface *generate_test_pattern(int pattern_size)
31 {
32 SDL_Surface *pattern = SDL_CreateRGBSurfaceWithFormat(0, pattern_size, pattern_size, 0, SDL_PIXELFORMAT_RGB24);
33
34 if (pattern) {
35 int i, x, y;
36 Uint8 *p, c;
37 const int thickness = 2; /* Important so 2x2 blocks of color are the same, to avoid Cr/Cb interpolation over pixels */
38
39 /* R, G, B in alternating horizontal bands */
40 for (y = 0; y < pattern->h; y += thickness) {
41 for (i = 0; i < thickness; ++i) {
42 p = (Uint8 *)pattern->pixels + (y + i) * pattern->pitch + ((y / thickness) % 3);
43 for (x = 0; x < pattern->w; ++x) {
44 *p = 0xFF;
45 p += 3;
46 }
47 }
48 }
49
50 /* Black and white in alternating vertical bands */
51 c = 0xFF;
52 for (x = 1 * thickness; x < pattern->w; x += 2 * thickness) {
53 for (i = 0; i < thickness; ++i) {
54 p = (Uint8 *)pattern->pixels + (x + i) * 3;
55 for (y = 0; y < pattern->h; ++y) {
56 SDL_memset(p, c, 3);
57 p += pattern->pitch;
58 }
59 }
60 if (c) {
61 c = 0x00;
62 } else {
63 c = 0xFF;
64 }
65 }
66 }
67 return pattern;
68 }
69
70 static SDL_bool verify_yuv_data(Uint32 format, const Uint8 *yuv, int yuv_pitch, SDL_Surface *surface)
71 {
72 const int tolerance = 20;
73 const int size = (surface->h * surface->pitch);
74 Uint8 *rgb;
75 SDL_bool result = SDL_FALSE;
76
77 rgb = (Uint8 *)SDL_malloc(size);
78 if (!rgb) {
79 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory");
80 return SDL_FALSE;
81 }
82
83 if (SDL_ConvertPixels(surface->w, surface->h, format, yuv, yuv_pitch, surface->format->format, rgb, surface->pitch) == 0) {
84 int x, y;
85 result = SDL_TRUE;
86 for (y = 0; y < surface->h; ++y) {
87 const Uint8 *actual = rgb + y * surface->pitch;
88 const Uint8 *expected = (const Uint8 *)surface->pixels + y * surface->pitch;
89 for (x = 0; x < surface->w; ++x) {
90 int deltaR = (int)actual[0] - expected[0];
91 int deltaG = (int)actual[1] - expected[1];
92 int deltaB = (int)actual[2] - expected[2];
93 int distance = (deltaR * deltaR + deltaG * deltaG + deltaB * deltaB);
94 if (distance > tolerance) {
95 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Pixel at %d,%d was 0x%.2x,0x%.2x,0x%.2x, expected 0x%.2x,0x%.2x,0x%.2x, distance = %d\n", x, y, actual[0], actual[1], actual[2], expected[0], expected[1], expected[2], distance);
96 result = SDL_FALSE;
97 }
98 actual += 3;
99 expected += 3;
100 }
101 }
102 } else {
103 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(format), SDL_GetPixelFormatName(surface->format->format), SDL_GetError());
104 }
105 SDL_free(rgb);
106
107 return result;
108 }
109
110 static int run_automated_tests(int pattern_size, int extra_pitch)
111 {
112 const Uint32 formats[] = {
113 SDL_PIXELFORMAT_YV12,
114 SDL_PIXELFORMAT_IYUV,
115 SDL_PIXELFORMAT_NV12,
116 SDL_PIXELFORMAT_NV21,
117 SDL_PIXELFORMAT_YUY2,
118 SDL_PIXELFORMAT_UYVY,
119 SDL_PIXELFORMAT_YVYU
120 };
121 int i, j;
122 SDL_Surface *pattern = generate_test_pattern(pattern_size);
123 const int yuv_len = MAX_YUV_SURFACE_SIZE(pattern->w, pattern->h, extra_pitch);
124 Uint8 *yuv1 = (Uint8 *)SDL_malloc(yuv_len);
125 Uint8 *yuv2 = (Uint8 *)SDL_malloc(yuv_len);
126 int yuv1_pitch, yuv2_pitch;
127 int result = -1;
128
129 if (!pattern || !yuv1 || !yuv2) {
130 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't allocate test surfaces");
131 goto done;
132 }
133
134 /* Verify conversion from YUV formats */
135 for (i = 0; i < SDL_arraysize(formats); ++i) {
136 if (!ConvertRGBtoYUV(formats[i], pattern->pixels, pattern->pitch, yuv1, pattern->w, pattern->h, SDL_GetYUVConversionModeForResolution(pattern->w, pattern->h), 0, 100)) {
137 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "ConvertRGBtoYUV() doesn't support converting to %s\n", SDL_GetPixelFormatName(formats[i]));
138 goto done;
139 }
140 yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w);
141 if (!verify_yuv_data(formats[i], yuv1, yuv1_pitch, pattern)) {
142 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to RGB\n", SDL_GetPixelFormatName(formats[i]));
143 goto done;
144 }
145 }
146
147 /* Verify conversion to YUV formats */
148 for (i = 0; i < SDL_arraysize(formats); ++i) {
149 yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
150 if (SDL_ConvertPixels(pattern->w, pattern->h, pattern->format->format, pattern->pixels, pattern->pitch, formats[i], yuv1, yuv1_pitch) < 0) {
151 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
152 goto done;
153 }
154 if (!verify_yuv_data(formats[i], yuv1, yuv1_pitch, pattern)) {
155 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from RGB to %s\n", SDL_GetPixelFormatName(formats[i]));
156 goto done;
157 }
158 }
159
160 /* Verify conversion between YUV formats */
161 for (i = 0; i < SDL_arraysize(formats); ++i) {
162 for (j = 0; j < SDL_arraysize(formats); ++j) {
163 yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
164 yuv2_pitch = CalculateYUVPitch(formats[j], pattern->w) + extra_pitch;
165 if (SDL_ConvertPixels(pattern->w, pattern->h, pattern->format->format, pattern->pixels, pattern->pitch, formats[i], yuv1, yuv1_pitch) < 0) {
166 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
167 goto done;
168 }
169 if (SDL_ConvertPixels(pattern->w, pattern->h, formats[i], yuv1, yuv1_pitch, formats[j], yuv2, yuv2_pitch) < 0) {
170 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
171 goto done;
172 }
173 if (!verify_yuv_data(formats[j], yuv2, yuv2_pitch, pattern)) {
174 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]));
175 goto done;
176 }
177 }
178 }
179
180 /* Verify conversion between YUV formats in-place */
181 for (i = 0; i < SDL_arraysize(formats); ++i) {
182 for (j = 0; j < SDL_arraysize(formats); ++j) {
183 if (is_packed_yuv_format(formats[i]) != is_packed_yuv_format(formats[j])) {
184 /* Can't change plane vs packed pixel layout in-place */
185 continue;
186 }
187
188 yuv1_pitch = CalculateYUVPitch(formats[i], pattern->w) + extra_pitch;
189 yuv2_pitch = CalculateYUVPitch(formats[j], pattern->w) + extra_pitch;
190 if (SDL_ConvertPixels(pattern->w, pattern->h, pattern->format->format, pattern->pixels, pattern->pitch, formats[i], yuv1, yuv1_pitch) < 0) {
191 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(pattern->format->format), SDL_GetPixelFormatName(formats[i]), SDL_GetError());
192 goto done;
193 }
194 if (SDL_ConvertPixels(pattern->w, pattern->h, formats[i], yuv1, yuv1_pitch, formats[j], yuv1, yuv2_pitch) < 0) {
195 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't convert %s to %s: %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]), SDL_GetError());
196 goto done;
197 }
198 if (!verify_yuv_data(formats[j], yuv1, yuv2_pitch, pattern)) {
199 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed conversion from %s to %s\n", SDL_GetPixelFormatName(formats[i]), SDL_GetPixelFormatName(formats[j]));
200 goto done;
201 }
202 }
203 }
204
205 result = 0;
206
207 done:
208 SDL_free(yuv1);
209 SDL_free(yuv2);
210 SDL_FreeSurface(pattern);
211 return result;
212 }
213
214 int main(int argc, char **argv)
215 {
216 struct
217 {
218 SDL_bool enable_intrinsics;
219 int pattern_size;
220 int extra_pitch;
221 } automated_test_params[] = {
222 /* Test: even width and height */
223 { SDL_FALSE, 2, 0 },
224 { SDL_FALSE, 4, 0 },
225 /* Test: odd width and height */
226 { SDL_FALSE, 1, 0 },
227 { SDL_FALSE, 3, 0 },
228 /* Test: even width and height, extra pitch */
229 { SDL_FALSE, 2, 3 },
230 { SDL_FALSE, 4, 3 },
231 /* Test: odd width and height, extra pitch */
232 { SDL_FALSE, 1, 3 },
233 { SDL_FALSE, 3, 3 },
234 /* Test: even width and height with intrinsics */
235 { SDL_TRUE, 32, 0 },
236 /* Test: odd width and height with intrinsics */
237 { SDL_TRUE, 33, 0 },
238 { SDL_TRUE, 37, 0 },
239 /* Test: even width and height with intrinsics, extra pitch */
240 { SDL_TRUE, 32, 3 },
241 /* Test: odd width and height with intrinsics, extra pitch */
242 { SDL_TRUE, 33, 3 },
243 { SDL_TRUE, 37, 3 },
244 };
245 int arg = 1;
246 const char *filename;
247 SDL_Surface *original;
248 SDL_Surface *converted;
249 SDL_Window *window;
250 SDL_Renderer *renderer;
251 SDL_Texture *output[3];
252 const char *titles[3] = { "ORIGINAL", "SOFTWARE", "HARDWARE" };
253 char title[128];
254 const char *yuv_name;
255 const char *yuv_mode;
256 Uint32 rgb_format = SDL_PIXELFORMAT_RGBX8888;
257 Uint32 yuv_format = SDL_PIXELFORMAT_YV12;
258 int current = 0;
259 int pitch;
260 Uint8 *raw_yuv;
261 Uint32 then, now, i, iterations = 100;
262 SDL_bool should_run_automated_tests = SDL_FALSE;
263
264 while (argv[arg] && *argv[arg] == '-') {
265 if (SDL_strcmp(argv[arg], "--jpeg") == 0) {
266 SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_JPEG);
267 } else if (SDL_strcmp(argv[arg], "--bt601") == 0) {
268 SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_BT601);
269 } else if (SDL_strcmp(argv[arg], "--bt709") == 0) {
270 SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_BT709);
271 } else if (SDL_strcmp(argv[arg], "--auto") == 0) {
272 SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_AUTOMATIC);
273 } else if (SDL_strcmp(argv[arg], "--yv12") == 0) {
274 yuv_format = SDL_PIXELFORMAT_YV12;
275 } else if (SDL_strcmp(argv[arg], "--iyuv") == 0) {
276 yuv_format = SDL_PIXELFORMAT_IYUV;
277 } else if (SDL_strcmp(argv[arg], "--yuy2") == 0) {
278 yuv_format = SDL_PIXELFORMAT_YUY2;
279 } else if (SDL_strcmp(argv[arg], "--uyvy") == 0) {
280 yuv_format = SDL_PIXELFORMAT_UYVY;
281 } else if (SDL_strcmp(argv[arg], "--yvyu") == 0) {
282 yuv_format = SDL_PIXELFORMAT_YVYU;
283 } else if (SDL_strcmp(argv[arg], "--nv12") == 0) {
284 yuv_format = SDL_PIXELFORMAT_NV12;
285 } else if (SDL_strcmp(argv[arg], "--nv21") == 0) {
286 yuv_format = SDL_PIXELFORMAT_NV21;
287 } else if (SDL_strcmp(argv[arg], "--rgb555") == 0) {
288 rgb_format = SDL_PIXELFORMAT_RGB555;
289 } else if (SDL_strcmp(argv[arg], "--rgb565") == 0) {
290 rgb_format = SDL_PIXELFORMAT_RGB565;
291 } else if (SDL_strcmp(argv[arg], "--rgb24") == 0) {
292 rgb_format = SDL_PIXELFORMAT_RGB24;
293 } else if (SDL_strcmp(argv[arg], "--argb") == 0) {
294 rgb_format = SDL_PIXELFORMAT_ARGB8888;
295 } else if (SDL_strcmp(argv[arg], "--abgr") == 0) {
296 rgb_format = SDL_PIXELFORMAT_ABGR8888;
297 } else if (SDL_strcmp(argv[arg], "--rgba") == 0) {
298 rgb_format = SDL_PIXELFORMAT_RGBA8888;
299 } else if (SDL_strcmp(argv[arg], "--bgra") == 0) {
300 rgb_format = SDL_PIXELFORMAT_BGRA8888;
301 } else if (SDL_strcmp(argv[arg], "--automated") == 0) {
302 should_run_automated_tests = SDL_TRUE;
303 } else {
304 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s [--jpeg|--bt601|-bt709|--auto] [--yv12|--iyuv|--yuy2|--uyvy|--yvyu|--nv12|--nv21] [--rgb555|--rgb565|--rgb24|--argb|--abgr|--rgba|--bgra] [image_filename]\n", argv[0]);
305 return 1;
306 }
307 ++arg;
308 }
309
310 /* Run automated tests */
311 if (should_run_automated_tests) {
312 for (i = 0; i < SDL_arraysize(automated_test_params); ++i) {
313 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Running automated test, pattern size %d, extra pitch %d, intrinsics %s\n",
314 automated_test_params[i].pattern_size,
315 automated_test_params[i].extra_pitch,
316 automated_test_params[i].enable_intrinsics ? "enabled" : "disabled");
317 if (run_automated_tests(automated_test_params[i].pattern_size, automated_test_params[i].extra_pitch) < 0) {
318 return 2;
319 }
320 }
321 return 0;
322 }
323
324 if (argv[arg]) {
325 filename = argv[arg];
326 } else {
327 filename = "testyuv.bmp";
328 }
329 original = SDL_ConvertSurfaceFormat(SDL_LoadBMP(filename), SDL_PIXELFORMAT_RGB24, 0);
330 if (!original) {
331 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", filename, SDL_GetError());
332 return 3;
333 }
334
335 raw_yuv = SDL_calloc(1, MAX_YUV_SURFACE_SIZE(original->w, original->h, 0));
336 ConvertRGBtoYUV(yuv_format, original->pixels, original->pitch, raw_yuv, original->w, original->h,
337 SDL_GetYUVConversionModeForResolution(original->w, original->h),
338 0, 100);
339 pitch = CalculateYUVPitch(yuv_format, original->w);
340
341 converted = SDL_CreateRGBSurfaceWithFormat(0, original->w, original->h, 0, rgb_format);
342 if (!converted) {
343 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create converted surface: %s\n", SDL_GetError());
344 return 3;
345 }
346
347 then = SDL_GetTicks();
348 for (i = 0; i < iterations; ++i) {
349 SDL_ConvertPixels(original->w, original->h, yuv_format, raw_yuv, pitch, rgb_format, converted->pixels, converted->pitch);
350 }
351 now = SDL_GetTicks();
352 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%" SDL_PRIu32 " iterations in %" SDL_PRIu32 " ms, %.2fms each\n", iterations, (now - then), (float)(now - then) / iterations);
353
354 window = SDL_CreateWindow("YUV test",
355 SDL_WINDOWPOS_UNDEFINED,
356 SDL_WINDOWPOS_UNDEFINED,
357 original->w, original->h,
358 0);
359 if (!window) {
360 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
361 return 4;
362 }
363
364 renderer = SDL_CreateRenderer(window, -1, 0);
365 if (!renderer) {
366 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
367 return 4;
368 }
369
370 output[0] = SDL_CreateTextureFromSurface(renderer, original);
371 output[1] = SDL_CreateTextureFromSurface(renderer, converted);
372 output[2] = SDL_CreateTexture(renderer, yuv_format, SDL_TEXTUREACCESS_STREAMING, original->w, original->h);
373 if (!output[0] || !output[1] || !output[2]) {
374 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
375 return 5;
376 }
377 SDL_UpdateTexture(output[2], NULL, raw_yuv, pitch);
378
379 yuv_name = SDL_GetPixelFormatName(yuv_format);
380 if (SDL_strncmp(yuv_name, "SDL_PIXELFORMAT_", 16) == 0) {
381 yuv_name += 16;
382 }
383
384 switch (SDL_GetYUVConversionModeForResolution(original->w, original->h)) {
385 case SDL_YUV_CONVERSION_JPEG:
386 yuv_mode = "JPEG";
387 break;
388 case SDL_YUV_CONVERSION_BT601:
389 yuv_mode = "BT.601";
390 break;
391 case SDL_YUV_CONVERSION_BT709:
392 yuv_mode = "BT.709";
393 break;
394 default:
395 yuv_mode = "UNKNOWN";
396 break;
397 }
398
399 {
400 int done = 0;
401 while (!done) {
402 SDL_Event event;
403 while (SDL_PollEvent(&event) > 0) {
404 if (event.type == SDL_QUIT) {
405 done = 1;
406 }
407 if (event.type == SDL_KEYDOWN) {
408 if (event.key.keysym.sym == SDLK_ESCAPE) {
409 done = 1;
410 } else if (event.key.keysym.sym == SDLK_LEFT) {
411 --current;
412 } else if (event.key.keysym.sym == SDLK_RIGHT) {
413 ++current;
414 }
415 }
416 if (event.type == SDL_MOUSEBUTTONDOWN) {
417 if (event.button.x < (original->w / 2)) {
418 --current;
419 } else {
420 ++current;
421 }
422 }
423 }
424
425 /* Handle wrapping */
426 if (current < 0) {
427 current += SDL_arraysize(output);
428 }
429 if (current >= SDL_arraysize(output)) {
430 current -= SDL_arraysize(output);
431 }
432
433 SDL_RenderClear(renderer);
434 SDL_RenderCopy(renderer, output[current], NULL, NULL);
435 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
436 if (current == 0) {
437 SDLTest_DrawString(renderer, 4, 4, titles[current]);
438 } else {
439 (void)SDL_snprintf(title, sizeof(title), "%s %s %s", titles[current], yuv_name, yuv_mode);
440 SDLTest_DrawString(renderer, 4, 4, title);
441 }
442 SDL_RenderPresent(renderer);
443 SDL_Delay(10);
444 }
445 }
446 SDL_Quit();
447 return 0;
448 }
449
450 /* vi: set ts=4 sw=4 expandtab: */