Files
mcde/lib/libDtChat/test_video_render.c
T
lotherk 09656fef62 dtchat: scale and convert I420 for the call window (T7.2)
media_render.c: dc_i420_scale resizes Y/U/V bilinearly (chroma at half size), so a 640x360 call fits a tile or a full-screen window; dc_i420_to_xrgb converts to 0x00RRGGBB for an XImage with the BT.601 limited-range matrix (Y 16..235, Cb/Cr 16..240) and an optional pixel stride. test_video_render: white/black/red solids, 1:1 identity, 2x up then down with PSNR > 28 dB, corners of the upscale, odd sizes with rounded chroma and stride padding untouched; standalone and ASan/UBSan clean. In-tree build pending: the next make mcde-host carries it.
2026-11-17 08:21:37 +00:00

154 lines
3.5 KiB
C

/*
* test_video_render.c -- tests for I420 scaling and XRGB conversion (T7.2)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
static void
solid(unsigned char *i420, int w, int h, int y, int u, int v)
{
int cw = (w + 1) / 2;
int ch = (h + 1) / 2;
memset(i420, y, (size_t)w * h);
memset(i420 + (size_t)w * h, u, (size_t)cw * ch);
memset(i420 + (size_t)w * h + (size_t)cw * ch, v, (size_t)cw * ch);
}
static void
gradient(unsigned char *i420, int w, int h)
{
int cw = (w + 1) / 2;
int ch = (h + 1) / 2;
int x, y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++)
i420[(size_t)y * w + x] =
(unsigned char)(16 + (x * 3 + y * 2) % 220);
for (y = 0; y < ch; y++)
for (x = 0; x < cw; x++) {
i420[(size_t)w * h + (size_t)y * cw + x] =
(unsigned char)(100 + (x & 31));
i420[(size_t)w * h + (size_t)cw * ch +
(size_t)y * cw + x] =
(unsigned char)(140 - (y & 31));
}
}
static double
psnr(const unsigned char *a, const unsigned char *b, size_t n)
{
double mse = 0;
size_t i;
for (i = 0; i < n; i++) {
double d = (double)a[i] - (double)b[i];
mse += d * d;
}
mse /= (double)n;
return mse == 0 ? 99.0 : 10.0 * log10(255.0 * 255.0 / mse);
}
static void
test_colors(void)
{
unsigned char i420[8 * 8 * 3 / 2];
uint32_t out[8 * 8];
solid(i420, 8, 8, 235, 128, 128);
dc_i420_to_xrgb(i420, 8, 8, out, 0);
CHECK(out[0] == 0x00FCFCFC || (out[0] & 0xff) >= 250);
CHECK(((out[0] >> 16) & 0xff) == ((out[0]) & 0xff));
solid(i420, 8, 8, 16, 128, 128);
dc_i420_to_xrgb(i420, 8, 8, out, 0);
CHECK(out[0] == 0);
solid(i420, 8, 8, 81, 90, 240);
dc_i420_to_xrgb(i420, 8, 8, out, 0);
CHECK(((out[0] >> 16) & 0xff) > 200);
CHECK(((out[0] >> 8) & 0xff) < 40);
CHECK((out[0] & 0xff) < 40);
}
static void
test_scale(void)
{
unsigned char src[64 * 48 * 3 / 2];
unsigned char big[128 * 96 * 3 / 2];
unsigned char back[64 * 48 * 3 / 2];
unsigned char same[64 * 48 * 3 / 2];
gradient(src, 64, 48);
/* 1:1 leaves every byte alone */
dc_i420_scale(src, 64, 48, same, 64, 48);
CHECK(memcmp(src, same, sizeof(src)) == 0);
/* up and down again: a smooth image comes back close */
dc_i420_scale(src, 64, 48, big, 128, 96);
dc_i420_scale(big, 128, 96, back, 64, 48);
CHECK(psnr(src, back, sizeof(src)) > 28.0);
/* the corners of the upscaled Y plane follow the source */
CHECK(big[0] == src[0]);
CHECK(big[(size_t)127] == src[63]);
CHECK(big[(size_t)95 * 128] == src[(size_t)47 * 64]);
CHECK(big[(size_t)95 * 128 + 127] == src[(size_t)47 * 64 + 63]);
/* odd sizes do not crash; I420 rounds the chroma up to even sizes */
{
unsigned char small[5 * 3 + 3 * 2 * 2];
unsigned char odd[7 * 5 + 4 * 3 * 2];
memset(small, 0x40, sizeof(small));
dc_i420_scale(small, 5, 3, odd, 7, 5);
CHECK(odd[0] == 0x40);
}
/* a stride leaves the padding alone */
{
uint32_t pixels[4 * 6];
int i;
solid(src, 4, 4, 235, 128, 128);
for (i = 0; i < 4 * 6; i++)
pixels[i] = 0xdeadbeef;
dc_i420_to_xrgb(src, 4, 4, pixels, 6);
CHECK(pixels[0] != 0xdeadbeef);
CHECK(pixels[4] == 0xdeadbeef);
CHECK(pixels[6] != 0xdeadbeef);
CHECK(pixels[5] == 0xdeadbeef);
}
}
int
main(void)
{
test_colors();
test_scale();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_video_render: all ok\n");
return 0;
}