From 09656fef62cf438891d69e971fe8ca6c673acd63 Mon Sep 17 00:00:00 2001 From: lotherk Date: Tue, 17 Nov 2026 08:21:37 +0000 Subject: [PATCH] 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. --- lib/libDtChat/Makefile.am | 12 ++- lib/libDtChat/dc_internal.h | 10 ++ lib/libDtChat/media_render.c | 147 ++++++++++++++++++++++++++++ lib/libDtChat/test_video_render.c | 153 ++++++++++++++++++++++++++++++ 4 files changed, 318 insertions(+), 4 deletions(-) create mode 100644 lib/libDtChat/media_render.c create mode 100644 lib/libDtChat/test_video_render.c diff --git a/lib/libDtChat/Makefile.am b/lib/libDtChat/Makefile.am index 2a701615..4119f05e 100644 --- a/lib/libDtChat/Makefile.am +++ b/lib/libDtChat/Makefile.am @@ -9,11 +9,11 @@ libDtChatIpc_la_CPPFLAGS = -I$(top_srcdir)/include libDtChatIpc_la_SOURCES = json.c libDtChatIpc_la_LDFLAGS = -version-info 1:0:0 -libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include +libDtChat_la_CPPFLAGS = -I$(top_srcdir)/include $(VPX_CPPFLAGS) libDtChat_la_SOURCES = core.c loop.c store.c conv.c text.c adapter.c instance.c \ irc.c identity.c peers.c local.c filexfer.c pair.c i18n.c media_audio.c \ media_packet.c media_call.c media_video.c media_video_packet.c \ - dc_internal.h + media_render.c dc_internal.h libDtChat_la_LIBADD = libDtChatIpc.la $(CRYPTOLIB) $(LIBTLS) $(OPUSLIB) $(VPXLIB) libDtChat_la_LDFLAGS = -version-info 1:0:0 @@ -22,7 +22,8 @@ libDtChat_la_LDFLAGS = -version-info 1:0:0 noinst_PROGRAMS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \ - test_video_packet test_adapter test_instance fake_adapter + test_video_packet test_video_render test_adapter test_instance \ + fake_adapter test_json_SOURCES = test_json.c test_json_CPPFLAGS = -I$(top_srcdir)/include test_json_LDADD = libDtChatIpc.la @@ -90,7 +91,10 @@ test_video_vp8_LDADD = libDtChat.la libDtChatIpc.la $(VPXLIB) -lm test_video_packet_SOURCES = test_video_packet.c test_video_packet_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) test_video_packet_LDADD = libDtChat.la libDtChatIpc.la $(CRYPTOLIB) $(OPUSLIB) -lm +test_video_render_SOURCES = test_video_render.c +test_video_render_CPPFLAGS = -I$(top_srcdir)/include -I$(srcdir) +test_video_render_LDADD = libDtChat.la libDtChatIpc.la -lm TESTS = test_json test_frame test_loop test_store test_conv test_text test_irc \ test_identity test_peers test_local test_filexfer test_pair test_fuzz \ test_i18n test_audio_opus test_media_packet test_media_call test_video_vp8 \ - test_video_packet test_adapter test_instance + test_video_packet test_video_render test_adapter test_instance diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 16e86949..ad59f5e0 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -527,6 +527,16 @@ int dc_video_reasm_frame(DcVideoReasm *reasm, unsigned char *out, size_t outlen, size_t *len_out, int *keyframe, uint16_t *frame_id); int dc_video_reasm_wants_keyframe(DcVideoReasm *reasm); +/* + * Rendering (T7.2): bilinear I420 scaling and the BT.601 limited-range + * conversion to 0x00RRGGBB pixels for an XImage. STRIDE_PX is in pixels + * and defaults to W. + */ +void dc_i420_scale(const unsigned char *src, int sw, int sh, + unsigned char *dst, int dw, int dh); +void dc_i420_to_xrgb(const unsigned char *i420, int w, int h, uint32_t *out, + int stride_px); + /* * IRC lines (RFC 1459/2812 + IRCv3 tags). dc_irc_parse() copies the tags, * the prefix, the command, up to 15 parameters and the trailing text; diff --git a/lib/libDtChat/media_render.c b/lib/libDtChat/media_render.c new file mode 100644 index 00000000..0f8add45 --- /dev/null +++ b/lib/libDtChat/media_render.c @@ -0,0 +1,147 @@ +/* + * media_render.c -- I420 scaling and conversion for the call window (T7.2) + * + * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. + * + * dc_i420_scale() resizes all three planes bilinearly (the chroma planes at + * half the size), so a 640x360 call fits a 320x180 tile or a full-screen + * 1280x720 window without a toolkit scaler. dc_i420_to_xrgb() converts to + * 0x00RRGGBB pixels for an XImage (XPutImage or XShm), with the BT.601 + * limited-range matrix (Y 16..235, Cb/Cr 16..240). + */ +#include "dc_internal.h" + +#include +#include + +static int +clamp255(int v) +{ + return v < 0 ? 0 : v > 255 ? 255 : v; +} + +static unsigned char +sample_plane(const unsigned char *plane, int pw, int ph, int x, int y) +{ + if (x < 0) + x = 0; + else if (x >= pw) + x = pw - 1; + if (y < 0) + y = 0; + else if (y >= ph) + y = ph - 1; + return plane[(size_t)y * pw + x]; +} + +/* bilinear: sx/sy in a 16.16 fixed point coordinate */ +static unsigned char +bilinear(const unsigned char *plane, int pw, int ph, unsigned int sx, + unsigned int sy) +{ + int x = (int)(sx >> 16); + int y = (int)(sy >> 16); + unsigned int fx = sx & 0xffff; + unsigned int fy = sy & 0xffff; + int a = sample_plane(plane, pw, ph, x, y); + int b = sample_plane(plane, pw, ph, x + 1, y); + int c = sample_plane(plane, pw, ph, x, y + 1); + int d = sample_plane(plane, pw, ph, x + 1, y + 1); + int top = a + (((b - a) * (int)fx) >> 16); + int bottom = c + (((d - c) * (int)fx) >> 16); + + return (unsigned char)(top + (((bottom - top) * (int)fy) >> 16)); +} + +void +dc_i420_scale(const unsigned char *src, int sw, int sh, unsigned char *dst, + int dw, int dh) +{ + const unsigned char *sy; + const unsigned char *su; + const unsigned char *sv; + unsigned char *dy; + unsigned char *du; + unsigned char *dv; + int cw = (sw + 1) / 2; + int ch = (sh + 1) / 2; + int dcw = (dw + 1) / 2; + int dch = (dh + 1) / 2; + int x, y; + + if (src == NULL || dst == NULL || sw <= 0 || sh <= 0 || dw <= 0 || + dh <= 0) + return; + sy = src; + su = sy + (size_t)sw * sh; + sv = su + (size_t)cw * ch; + dy = dst; + du = dy + (size_t)dw * dh; + dv = du + (size_t)dcw * dch; + + for (y = 0; y < dh; y++) { + unsigned int fy = (unsigned int)(((unsigned long long)y * + (unsigned)(sh > 1 ? sh - 1 : 1) << 16) / + (unsigned)(dh > 1 ? dh - 1 : 1)); + + for (x = 0; x < dw; x++) { + unsigned int fx = (unsigned int)(((unsigned long long)x * + (unsigned)(sw > 1 ? sw - 1 : 1) << 16) / + (unsigned)(dw > 1 ? dw - 1 : 1)); + + dy[(size_t)y * dw + x] = bilinear(sy, sw, sh, fx, fy); + } + } + for (y = 0; y < dch; y++) { + unsigned int fy = (unsigned int)(((unsigned long long)y * + (unsigned)(ch > 1 ? ch - 1 : 1) << 16) / + (unsigned)(dch > 1 ? dch - 1 : 1)); + + for (x = 0; x < dcw; x++) { + unsigned int fx = (unsigned int)(((unsigned long long)x * + (unsigned)(cw > 1 ? cw - 1 : 1) << 16) / + (unsigned)(dcw > 1 ? dcw - 1 : 1)); + + du[(size_t)y * dcw + x] = bilinear(su, cw, ch, fx, fy); + dv[(size_t)y * dcw + x] = bilinear(sv, cw, ch, fx, fy); + } + } +} + +void +dc_i420_to_xrgb(const unsigned char *i420, int w, int h, uint32_t *out, + int stride_px) +{ + const unsigned char *yp = i420; + const unsigned char *up; + const unsigned char *vp; + int cw = (w + 1) / 2; + int x, y; + + if (i420 == NULL || out == NULL || w <= 0 || h <= 0) + return; + if (stride_px <= 0) + stride_px = w; + up = yp + (size_t)w * h; + vp = up + (size_t)cw * ((h + 1) / 2); + + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + int yy = yp[(size_t)y * w + x]; + int cb = up[(size_t)(y / 2) * cw + x / 2] - 128; + int cr = vp[(size_t)(y / 2) * cw + x / 2] - 128; + int c = (yy - 16) * 1192; /* 1.164 << 10 */ + int r; + int g; + int b; + + r = (c + 1634 * cr) >> 10; /* 1.596 */ + g = (c - 833 * cr - 400 * cb) >> 10; + b = (c + 2066 * cb) >> 10; /* 2.018 */ + out[(size_t)y * stride_px + x] = + ((uint32_t)clamp255(r) << 16) | + ((uint32_t)clamp255(g) << 8) | + (uint32_t)clamp255(b); + } + } +} diff --git a/lib/libDtChat/test_video_render.c b/lib/libDtChat/test_video_render.c new file mode 100644 index 00000000..0dcd452b --- /dev/null +++ b/lib/libDtChat/test_video_render.c @@ -0,0 +1,153 @@ +/* + * 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 +#include +#include +#include + +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; +}