/* * 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); } } }