From 9bef5d3ece33eac348bd5badcf26b9a6fd84e769 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Sun, 2 Aug 2026 00:13:10 +0100 Subject: [PATCH 01/30] upipe_blit: merge the background copy and the blit into a single pass When the input picture is not writable (always the case with dup_uref_before_blit), the pipe did a full-frame ubuf_pic_copy() and then re-wrote the subpicture rectangles a second time with uref_pic_blit(), with the blend reading back the pixels it had just copied. Add _bg variants of the ubuf_pic_blit primitives taking a separate background ubuf, so the blend reads the underlying pixels from the original frame and writes every pixel of the destination rectangle exactly once (dest = blend(bg, src)). With bg == NULL the kernels behave exactly as before and the old names remain as wrappers. In _upipe_blit_prepare(), allocate the output picture, copy only the four bands around the bottom-most subpicture rectangle and compose the rectangle in one fused pass; higher z-index subpictures still blit in place into the now-writable picture. Any failure falls back to the previous full-copy-then-blit path. --- include/upipe/ubuf_pic.h | 237 +++++++++++++++++++++++++++++---- lib/upipe-modules/upipe_blit.c | 70 +++++++++- 2 files changed, 277 insertions(+), 30 deletions(-) diff --git a/include/upipe/ubuf_pic.h b/include/upipe/ubuf_pic.h index 2009375b2..f5a1ac297 100644 --- a/include/upipe/ubuf_pic.h +++ b/include/upipe/ubuf_pic.h @@ -336,13 +336,26 @@ static inline int ubuf_pic_resize(struct ubuf *ubuf, int hskip, int vskip, new_hsize, new_vsize); } -/** @This blits a picture ubuf to another ubuf. +/** @This blits a picture ubuf to another ubuf, optionally reading the + * underlying pixels from a third (background) ubuf. + * + * When bg is NULL, the blending reads the underlying pixels from dest and + * pixels not covered by the subpicture are left untouched (in-place blit). + * When bg is not NULL, the underlying pixels are read from bg and every pixel + * of the destination rectangle is written, so dest may be freshly allocated; + * this merges the copy of the background and the blit of the subpicture in a + * single pass. * * @param dest destination ubuf + * @param bg background ubuf, or NULL to blend in place into dest * @param src source ubuf * @param dest_hoffset number of pixels to seek at the beginning of each line of * dest * @param dest_voffset number of lines to seek at the beginning of dest + * @param bg_hoffset number of pixels to skip at the beginning of each line of + * bg (ignored if bg is NULL) + * @param bg_voffset number of lines to skip at the beginning of bg (ignored if + * bg is NULL) * @param src_hoffset number of pixels to skip at the beginning of each line of * src * @param src_voffset number of lines to skip at the beginning of src @@ -358,15 +371,25 @@ static inline int ubuf_pic_resize(struct ubuf *ubuf, int hskip, int vskip, * their alpha value is more than this value * @return an error code */ -static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, +static inline int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, const uint8_t *alpha_plane, int alpha_stride, const int alpha, const int threshold) { - if (alpha_plane == NULL && alpha < threshold && threshold != 0xff) - return UBASE_ERR_NONE; /* nothing to do */ + if (alpha_plane == NULL && alpha < threshold && threshold != 0xff) { + if (bg == NULL) + return UBASE_ERR_NONE; /* nothing to do */ + /* the subpicture is invisible: plain copy of the background */ + return ubuf_pic_blit_alpha_bg(dest, NULL, bg, + dest_hoffset, dest_voffset, 0, 0, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + NULL, 0, 0xff, 0); + } uint8_t src_macropixel; UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) @@ -374,6 +397,12 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) if (unlikely(dest_macropixel != src_macropixel)) return UBASE_ERR_INVALID; + if (bg != NULL) { + uint8_t bg_macropixel; + UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) + if (unlikely(bg_macropixel != dest_macropixel)) + return UBASE_ERR_INVALID; + } #define MAX_PLANES 3 @@ -414,11 +443,33 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, src_macropixel_size != dest_macropixel_size)) return UBASE_ERR_INVALID; + size_t bg_stride = dest_stride; + if (bg != NULL) { + uint8_t bg_hsub, bg_vsub, bg_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, + &bg_hsub, &bg_vsub, &bg_macropixel_size)) + if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || + bg_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + } + uint8_t *dest_buffer; + const uint8_t *bg_buffer; const uint8_t *in[MAX_PLANES]; UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize, &dest_buffer)) + if (bg != NULL) { + err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + &bg_buffer); + if (unlikely(!ubase_check(err))) { + ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + } else + bg_buffer = dest_buffer; for (unsigned i = 0; i < in_planes; i++) { err = ubuf_pic_plane_read(src, in_chroma[i], src_hoffset, src_voffset, @@ -428,6 +479,9 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, ubuf_pic_plane_unmap(src, in_chroma[i - 1], src_hoffset, src_voffset, extract_hsize, extract_vsize); + if (bg != NULL) + ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize); ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize); return err; @@ -452,13 +506,15 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, for (int j = 0; j < plane_hsize; j++) { for (int p = 0; p < in_planes; p++) dest_buffer[j * in_planes + p] = - (dest_buffer[j * in_planes + p] * (0xff - alpha) + + (bg_buffer[j * in_planes + p] * (0xff - alpha) + in[p][j] * alpha) / 0xff; } } else if (threshold != 0xff) { /* This is an on/off blending - * if alpha is over the threshold, we use the subpicture pixel. + * if alpha is over the threshold, we use the subpicture pixel, + * otherwise the background pixel (left untouched when + * blending in place). */ if (alpha == 0xff) { for (int j = 0; j < plane_hsize; j++) { @@ -466,6 +522,10 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, if (a > threshold) { for (int p = 0; p < in_planes; p++) dest_buffer[j * in_planes + p] = in[p][j]; + } else if (bg != NULL) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + bg_buffer[j * in_planes + p]; } } } else { @@ -474,6 +534,10 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, if (a > threshold) { for (int p = 0; p < in_planes; p++) dest_buffer[j * in_planes + p] = in[p][j]; + } else if (bg != NULL) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + bg_buffer[j * in_planes + p]; } } } @@ -484,7 +548,7 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, const uint8_t a = alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub]; for (int p = 0; p < in_planes; p++) dest_buffer[j * in_planes + p] = - (dest_buffer[j * in_planes + p] * (0xff - a) + + (bg_buffer[j * in_planes + p] * (0xff - a) + in[p][j] * a) / 0xff; } @@ -493,13 +557,14 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, const uint8_t a = (uint16_t)alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub] * (uint16_t)alpha / 0xff; for (int p = 0; p < in_planes; p++) dest_buffer[j * in_planes + p] = - (dest_buffer[j * in_planes + p] * (0xff - a) + + (bg_buffer[j * in_planes + p] * (0xff - a) + in[p][j] * a) / 0xff; } } } dest_buffer += dest_stride; + bg_buffer += bg_stride; for (int p = 0; p < in_planes; p++) in[p] += src_stride; } @@ -507,6 +572,10 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, err = ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize); + if (bg != NULL) + UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, + bg_voffset, extract_hsize, + extract_vsize)) for (int p = 0; p < in_planes; p++) UBASE_RETURN(ubuf_pic_plane_unmap(src, in_chroma[p], src_hoffset, src_voffset, extract_hsize, @@ -519,16 +588,44 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, return UBASE_ERR_NONE; } -/** @see ubuf_pic_blit_alpha */ -static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, +/** @This blits a picture ubuf to another ubuf in place. + * + * @see ubuf_pic_blit_alpha_bg + */ +static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, + int dest_hoffset, int dest_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const uint8_t *alpha_plane, int alpha_stride, + const int alpha, const int threshold) +{ + return ubuf_pic_blit_alpha_bg(dest, NULL, src, + dest_hoffset, dest_voffset, 0, 0, + src_hoffset, src_voffset, + extract_hsize, extract_vsize, + alpha_plane, alpha_stride, alpha, threshold); +} + +/** @see ubuf_pic_blit_alpha_bg */ +static inline int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, const uint8_t *alpha_plane, int alpha_stride, const int alpha, const int threshold) { - if (alpha_plane == NULL && alpha < threshold && threshold != 0x3ff) - return UBASE_ERR_NONE; /* nothing to do */ + if (alpha_plane == NULL && alpha < threshold && threshold != 0x3ff) { + if (bg == NULL) + return UBASE_ERR_NONE; /* nothing to do */ + /* the subpicture is invisible: plain copy of the background */ + return ubuf_pic_blit_alpha10_bg(dest, NULL, bg, + dest_hoffset, dest_voffset, 0, 0, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + NULL, 0, 0x3ff, 0); + } uint8_t src_macropixel; UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) @@ -536,6 +633,12 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) if (unlikely(dest_macropixel != src_macropixel)) return UBASE_ERR_INVALID; + if (bg != NULL) { + uint8_t bg_macropixel; + UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) + if (unlikely(bg_macropixel != dest_macropixel)) + return UBASE_ERR_INVALID; + } const char *chroma; ubuf_pic_foreach_plane(dest, chroma) { @@ -554,15 +657,42 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, src_macropixel_size != dest_macropixel_size)) return UBASE_ERR_INVALID; + size_t bg_stride = dest_stride; + if (bg != NULL) { + uint8_t bg_hsub, bg_vsub, bg_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, + &bg_hsub, &bg_vsub, &bg_macropixel_size)) + if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || + bg_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + } + uint8_t *dest_buffer; + const uint8_t *bg_buffer; const uint8_t *src_buffer; UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize, &dest_buffer)) - int err = ubuf_pic_plane_read(src, chroma, src_hoffset, src_voffset, + int err; + if (bg != NULL) { + err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, extract_hsize, extract_vsize, - &src_buffer); + &bg_buffer); + if (unlikely(!ubase_check(err))) { + ubuf_pic_plane_unmap(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + } else + bg_buffer = dest_buffer; + err = ubuf_pic_plane_read(src, chroma, src_hoffset, src_voffset, + extract_hsize, extract_vsize, + &src_buffer); if (unlikely(!ubase_check(err))) { + if (bg != NULL) + ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize); ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize); @@ -575,6 +705,7 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, for (int i = 0; i < plane_vsize; i++) { uint16_t *real_dst = (uint16_t *)dest_buffer; + const uint16_t *real_bg = (const uint16_t *)bg_buffer; const uint16_t *real_src = (const uint16_t *)src_buffer; const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); @@ -582,21 +713,25 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, memcpy(dest_buffer, src_buffer, plane_hsize); } else if (!alpha_plane) { for (int j = 0; j < plane_hsize/2; j++) { - real_dst[j] = (real_dst[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; + real_dst[j] = (real_bg[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; } } else if (threshold != 0x3ff) { /* This is an on/off blending - * if alpha is over the threshold, we use the subpicture pixel. + * if alpha is over the threshold, we use the subpicture pixel, + * otherwise the background pixel (left untouched when + * blending in place). */ if (alpha == 0x3ff) { for (int j = 0; j < plane_hsize/2; j++) { const uint16_t a = real_alpha[j * src_hsub]; if (a > threshold) real_dst[j] = real_src[j]; + else if (bg != NULL) real_dst[j] = real_bg[j]; } } else { for (int j = 0; j < plane_hsize/2; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; if (a > threshold) real_dst[j] = real_src[j]; + else if (bg != NULL) real_dst[j] = real_bg[j]; } } } else { @@ -604,22 +739,27 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, if (alpha == 0x3ff) { for (int j = 0; j < plane_hsize/2; j++) { const uint16_t a = real_alpha[j * src_hsub]; - real_dst[j] = (real_dst[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; + real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } } else { for (int j = 0; j < plane_hsize/2; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; - real_dst[j] = (real_dst[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; + real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } } } dest_buffer += dest_stride; + bg_buffer += bg_stride; src_buffer += src_stride; } err = ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, extract_hsize, extract_vsize); + if (bg != NULL) + UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize)) UBASE_RETURN(ubuf_pic_plane_unmap(src, chroma, src_hoffset, src_voffset, extract_hsize, extract_vsize)) @@ -628,13 +768,41 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, return UBASE_ERR_NONE; } -/** @This blits a picture ubuf to another ubuf. +/** @see ubuf_pic_blit_alpha_bg */ +static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, + int dest_hoffset, int dest_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const uint8_t *alpha_plane, int alpha_stride, + const int alpha, const int threshold) +{ + return ubuf_pic_blit_alpha10_bg(dest, NULL, src, + dest_hoffset, dest_voffset, 0, 0, + src_hoffset, src_voffset, + extract_hsize, extract_vsize, + alpha_plane, alpha_stride, + alpha, threshold); +} + +/** @This blits a picture ubuf to another ubuf, optionally reading the + * underlying pixels from a third (background) ubuf. + * + * When bg is NULL, the blending reads the underlying pixels from dest + * (in-place blit). When bg is not NULL, the underlying pixels are read from + * bg and every pixel of the destination rectangle is written, so dest may be + * freshly allocated; this merges the copy of the background and the blit of + * the subpicture in a single pass. * * @param dest destination ubuf + * @param bg background ubuf, or NULL to blend in place into dest * @param src source ubuf * @param dest_hoffset number of pixels to seek at the beginning of each line of * dest * @param dest_voffset number of lines to seek at the beginning of dest + * @param bg_hoffset number of pixels to skip at the beginning of each line of + * bg (ignored if bg is NULL) + * @param bg_voffset number of lines to skip at the beginning of bg (ignored if + * bg is NULL) * @param src_hoffset number of pixels to skip at the beginning of each line of * src * @param src_voffset number of lines to skip at the beginning of src @@ -644,8 +812,10 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, * @param threshold threshold parameter for alpha * @return an error code */ -static inline int ubuf_pic_blit(struct ubuf *dest, struct ubuf *src, +static inline int ubuf_pic_blit_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, const int alpha, const int threshold) @@ -673,12 +843,16 @@ static inline int ubuf_pic_blit(struct ubuf *dest, struct ubuf *src, /* Perform blit. */ if (i == 0) - ret = ubuf_pic_blit_alpha(dest, src, dest_hoffset, dest_voffset, + ret = ubuf_pic_blit_alpha_bg(dest, bg, src, + dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, src_hoffset, src_voffset, extract_hsize, extract_vsize, alpha_plane, alpha_stride, alpha, threshold); else - ret = ubuf_pic_blit_alpha10(dest, src, dest_hoffset, dest_voffset, + ret = ubuf_pic_blit_alpha10_bg(dest, bg, src, + dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, src_hoffset, src_voffset, extract_hsize, extract_vsize, alpha_plane, alpha_stride, alpha, threshold); @@ -688,8 +862,8 @@ static inline int ubuf_pic_blit(struct ubuf *dest, struct ubuf *src, } /* Fallback to no alpha plane. */ - ret = ubuf_pic_blit_alpha(dest, src, dest_hoffset, dest_voffset, - src_hoffset, src_voffset, + ret = ubuf_pic_blit_alpha_bg(dest, bg, src, dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, src_hoffset, src_voffset, extract_hsize, extract_vsize, NULL, 0, alpha, threshold); @@ -700,6 +874,21 @@ static inline int ubuf_pic_blit(struct ubuf *dest, struct ubuf *src, return ret; } +/** @This blits a picture ubuf to another ubuf in place. + * + * @see ubuf_pic_blit_bg + */ +static inline int ubuf_pic_blit(struct ubuf *dest, struct ubuf *src, + int dest_hoffset, int dest_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const int alpha, const int threshold) +{ + return ubuf_pic_blit_bg(dest, NULL, src, dest_hoffset, dest_voffset, 0, 0, + src_hoffset, src_voffset, + extract_hsize, extract_vsize, alpha, threshold); +} + /** @This copies a picture ubuf to a newly allocated ubuf, and doesn't deal * with the old ubuf or a dictionary. * diff --git a/lib/upipe-modules/upipe_blit.c b/lib/upipe-modules/upipe_blit.c index ae87b4ce8..f65af3af8 100644 --- a/lib/upipe-modules/upipe_blit.c +++ b/lib/upipe-modules/upipe_blit.c @@ -1086,24 +1086,82 @@ static int _upipe_blit_prepare(struct upipe *upipe, struct upump **upump_p) } } + /* Subpipe whose blit was merged with the copy, or NULL */ + struct upipe_blit_sub *fused = NULL; + if (!writable) { if (unlikely(!upipe_blit->ubuf_mgr)) { upipe_warn(upipe, "no ubuf manager set, dropping..."); uref_free(uref); return UBASE_ERR_BUSY; } - struct ubuf *ubuf = ubuf_pic_copy(upipe_blit->ubuf_mgr, - uref->ubuf, 0, 0, -1, -1); - if (unlikely(ubuf == NULL)) { - uref_free(uref); - return UBASE_ERR_ALLOC;; + struct ubuf *old = uref->ubuf; + struct ubuf *ubuf = NULL; + size_t hsize, vsize; + + /* Merge the copy and the blit of the bottom-most subpicture in a + * single pass: copy the four bands around the subpicture rectangle + * and compose the rectangle directly from the background and the + * subpicture, so that each pixel is written only once. */ + if (ubase_check(ubuf_pic_size(old, &hsize, &vsize, NULL))) { + ulist_foreach (&upipe_blit->subs, uchain) { + struct upipe_blit_sub *sub = upipe_blit_sub_from_uchain(uchain); + if (sub->ubuf != NULL && + sub->hposition + sub->hsize <= hsize && + sub->vposition + sub->vsize <= vsize) { + fused = sub; + break; + } + } + } + + if (fused != NULL) { + int w = hsize, h = vsize; + int hp = fused->hposition, vp = fused->vposition; + int hs = fused->hsize, vs = fused->vsize; + ubuf = ubuf_pic_alloc(upipe_blit->ubuf_mgr, w, h); + if (ubuf != NULL) { + int err = UBASE_ERR_NONE; + if (vp > 0) + err = ubuf_pic_blit(ubuf, old, 0, 0, 0, 0, + w, vp, 0xff, 0); + if (ubase_check(err) && vp + vs < h) + err = ubuf_pic_blit(ubuf, old, 0, vp + vs, 0, vp + vs, + w, h - vp - vs, 0xff, 0); + if (ubase_check(err) && hp > 0) + err = ubuf_pic_blit(ubuf, old, 0, vp, 0, vp, + hp, vs, 0xff, 0); + if (ubase_check(err) && hp + hs < w) + err = ubuf_pic_blit(ubuf, old, hp + hs, vp, hp + hs, vp, + w - hp - hs, vs, 0xff, 0); + if (ubase_check(err)) + err = ubuf_pic_blit_bg(ubuf, old, fused->ubuf, + hp, vp, hp, vp, 0, 0, hs, vs, + fused->alpha, + fused->alpha_threshold); + if (unlikely(!ubase_check(err))) { + /* Fall back to a full copy followed by a blit */ + ubuf_free(ubuf); + ubuf = NULL; + } + } + } + + if (ubuf == NULL) { + fused = NULL; + ubuf = ubuf_pic_copy(upipe_blit->ubuf_mgr, old, 0, 0, -1, -1); + if (unlikely(ubuf == NULL)) { + uref_free(uref); + return UBASE_ERR_ALLOC; + } } uref_attach_ubuf(uref, ubuf); } ulist_foreach (&upipe_blit->subs, uchain) { struct upipe_blit_sub *sub = upipe_blit_sub_from_uchain(uchain); - upipe_blit_sub_work(upipe_blit_sub_to_upipe(sub), uref); + if (sub != fused) + upipe_blit_sub_work(upipe_blit_sub_to_upipe(sub), uref); } upipe_blit_output(upipe, uref, upump_p); From b9590108dbb5c7037bcbe89c111afc6672d4f9d6 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 1 Jul 2026 13:46:30 +0200 Subject: [PATCH 02/30] tests: add new program to test ubuf_pic_blit --- tests/Build.mk | 4 + tests/ubuf_pic_blit_test.c | 1379 ++++++++++++++++++++++++++++++++++++ 2 files changed, 1383 insertions(+) create mode 100644 tests/ubuf_pic_blit_test.c diff --git a/tests/Build.mk b/tests/Build.mk index 032462eb4..38a45b932 100644 --- a/tests/Build.mk +++ b/tests/Build.mk @@ -30,6 +30,10 @@ tests += ubuf_block_mem_test ubuf_block_mem_test-src = ubuf_block_mem_test.c ubuf_block_mem_test-libs = libupipe +tests += ubuf_pic_blit_test +ubuf_pic_blit_test-src = ubuf_pic_blit_test.c +ubuf_pic_blit_test-libs = libupipe + tests += ubuf_pic_clear_test ubuf_pic_clear_test-src = ubuf_pic_clear_test.c ubuf_pic_clear_test-libs = libupipe diff --git a/tests/ubuf_pic_blit_test.c b/tests/ubuf_pic_blit_test.c new file mode 100644 index 000000000..6bfb2d2c4 --- /dev/null +++ b/tests/ubuf_pic_blit_test.c @@ -0,0 +1,1379 @@ +/* + * Copyright (C) 2026 Open Broadcast Systems + * + * Authors: James Darnley + * + * SPDX-License-Identifier: MIT + */ + +/** @file + * @short unit tests for picture blit + */ + +#undef NDEBUG + +#include "upipe/umem.h" +#include "upipe/umem_alloc.h" +#include "upipe/ubuf.h" +#include "upipe/ubuf_pic.h" +#include "upipe/ubuf_pic_mem.h" + +#include +#include +#include +#include + +#define UBUF_POOL_DEPTH 1 +#define UBUF_PREPEND 2 +#define UBUF_APPEND 2 +#define UBUF_ALIGN 16 +#define UBUF_ALIGN_HOFFSET 0 + +/** @internal @This fills a rectangular region of a plane with a diagonal + * gradient: 0 at the top-left corner (0, 0), rising to 255 at the bottom-right + * corner (hsize, vsize). The offset and size are expressed in luma samples. */ +static void fill_plane_gradient(struct ubuf *ubuf, const char *chroma, + int hoffset, int voffset, int hsize, int vsize) +{ + uint8_t macropixel; + ubase_assert(ubuf_pic_size(ubuf, NULL, NULL, ¯opixel)); + + size_t stride; + uint8_t hsub, vsub, macropixel_size; + ubase_assert(ubuf_pic_plane_size(ubuf, chroma, &stride, &hsub, &vsub, + ¯opixel_size)); + int hoctets = hsize * macropixel_size / hsub / macropixel; + int rows = vsize / vsub; + + uint8_t *buffer; + ubase_assert(ubuf_pic_plane_write(ubuf, chroma, hoffset, voffset, + hsize, vsize, &buffer)); + for (int y = 0; y < rows; y++) { + for (int x = 0; x < hoctets; x++) + buffer[x] = 255 * (x + y) / (hoctets + rows); + buffer += stride; + } + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, hoffset, voffset, + hsize, vsize)); +} + +/** @internal @This fills a rectangular region of a 16-bit plane with a diagonal + * gradient: 0 at the top-left corner (0, 0), rising to 1023 at the bottom-right + * corner (hsize, vsize). The offset and size are expressed in luma samples. */ +static void fill_plane_gradient16(struct ubuf *ubuf, const char *chroma, + int hoffset, int voffset, int hsize, int vsize) +{ + uint8_t macropixel; + ubase_assert(ubuf_pic_size(ubuf, NULL, NULL, ¯opixel)); + + size_t stride; + uint8_t hsub, vsub, macropixel_size; + ubase_assert(ubuf_pic_plane_size(ubuf, chroma, &stride, &hsub, &vsub, + ¯opixel_size)); + int cols = hsize * macropixel_size / hsub / macropixel / 2; + int rows = vsize / vsub; + + uint8_t *buffer; + ubase_assert(ubuf_pic_plane_write(ubuf, chroma, hoffset, voffset, + hsize, vsize, &buffer)); + for (int y = 0; y < rows; y++) { + uint16_t *words = (uint16_t *)buffer; + for (int x = 0; x < cols; x++) + words[x] = 1023 * (x + y) / (cols + rows); + buffer += stride; + } + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, hoffset, voffset, + hsize, vsize)); +} + +/** @internal @This reads the first octet of a plane. */ +static uint8_t read_octet(struct ubuf *ubuf, const char *chroma) +{ + const uint8_t *buffer; + ubase_assert(ubuf_pic_plane_read(ubuf, chroma, 0, 0, -1, -1, &buffer)); + uint8_t value = buffer[0]; + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, 0, 0, -1, -1)); + return value; +} + +/** @internal @This reads the first 16-bit sample of a plane (host byte order). */ +static uint16_t read_word(struct ubuf *ubuf, const char *chroma) +{ + const uint8_t *buffer; + ubase_assert(ubuf_pic_plane_read(ubuf, chroma, 0, 0, -1, -1, &buffer)); + uint16_t value = ((const uint16_t *)buffer)[0]; + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, 0, 0, -1, -1)); + return value; +} + +/** @internal @This computes the value ubuf_pic_blit() is expected to leave in + * a destination octet, replicating exactly the integer arithmetic of + * ubuf_pic_blit_alpha() for the case where the source carries an "a8" alpha + * plane. + * + * @param dst destination octet value + * @param src source octet value + * @param a8 alpha plane octet value for this pixel + * @param alpha alpha multiplier passed to ubuf_pic_blit() + * @param threshold threshold passed to ubuf_pic_blit() + * @return the octet value expected in dst after the blit + */ +static uint8_t expected_blit(uint8_t dst, uint8_t src, uint8_t a8, + int alpha, int threshold) +{ + /* threshold == 0: straight copy (memcpy path). */ + if (threshold == 0) + return src; + + /* alpha == 0xff with threshold in ]0, 0xff[: on/off blending that uses the + * alpha plane sample directly, without scaling. */ + if (alpha == 0xff && threshold != 0xff) { + if (a8 > threshold) + return src; + else + return dst; + } + + /* alpha != 0xff with threshold in ]0, 0xff[: on/off blending that scales the + * alpha plane sample by the multiplier first. */ + if (alpha != 0xff && threshold != 0xff) { + uint8_t a = (uint8_t)((uint16_t)a8 * (uint16_t)alpha / 0xff); + if (a > threshold) + return src; + else + return dst; + } + + /* alpha == 0xff with threshold == 0xff: smooth (per-pixel) blending that + * uses the alpha plane sample directly. */ + if (alpha == 0xff && threshold == 0xff) { + return (uint8_t)((dst * (0xff - a8) + src * a8) / 0xff); + } else { + /* alpha != 0xff with threshold == 0xff: smooth blending with the scaled + * alpha plane sample. */ + uint8_t a = (uint8_t)((uint16_t)a8 * (uint16_t)alpha / 0xff); + return (uint8_t)((dst * (0xff - a) + src * a) / 0xff); + } +} + +/** @internal @This computes the value ubuf_pic_blit() is expected to leave in a + * destination 16-bit sample, replicating exactly the integer arithmetic of + * ubuf_pic_blit_alpha10() for the case where the source carries an "a10l" alpha + * plane. The saturation values are 10-bit (0x3ff) instead of 8-bit (0xff). + * + * @param dst destination sample value + * @param src source sample value + * @param a10 alpha plane sample value for this pixel + * @param alpha alpha multiplier passed to ubuf_pic_blit() + * @param threshold threshold passed to ubuf_pic_blit() + * @return the sample value expected in dst after the blit + */ +static uint16_t expected_blit10(uint16_t dst, uint16_t src, uint16_t a10, + int alpha, int threshold) +{ + /* threshold == 0: straight copy (memcpy path). */ + if (threshold == 0) + return src; + + /* alpha == 0x3ff with threshold in ]0, 0x3ff[: on/off blending that uses the + * alpha plane sample directly, without scaling. */ + if (alpha == 0x3ff && threshold != 0x3ff) { + if (a10 > threshold) + return src; + else + return dst; + } + + /* alpha != 0x3ff with threshold in ]0, 0x3ff[: on/off blending that scales + * the alpha plane sample by the multiplier first. */ + if (alpha != 0x3ff && threshold != 0x3ff) { + uint16_t a = (uint16_t)(a10 * alpha / 0x3ff); + if (a > threshold) + return src; + else + return dst; + } + + /* alpha == 0x3ff with threshold == 0x3ff: smooth (per-pixel) blending that + * uses the alpha plane sample directly. */ + if (alpha == 0x3ff && threshold == 0x3ff) { + return (uint16_t)((dst * (0x3ff - a10) + src * a10) / 0x3ff); + } else { + /* alpha != 0x3ff with threshold == 0x3ff: smooth blending with the + * scaled alpha plane sample. */ + uint16_t a = (uint16_t)(a10 * alpha / 0x3ff); + return (uint16_t)((dst * (0x3ff - a) + src * a) / 0x3ff); + } +} + +/** @internal @This computes the value ubuf_pic_blit() is expected to leave in + * a destination octet when the source carries no alpha plane, i.e. when the + * fallback call to ubuf_pic_blit_alpha() passes alpha_plane == NULL. The alpha + * multiplier is then applied uniformly to the whole plane. + * + * @param dst destination octet value + * @param src source octet value + * @param alpha alpha multiplier passed to ubuf_pic_blit() + * @param threshold threshold passed to ubuf_pic_blit() + * @return the octet value expected in dst after the blit + */ +static uint8_t expected_blit_noalpha(uint8_t dst, uint8_t src, + int alpha, int threshold) +{ + /* "nothing to do" early return: dst is left untouched. */ + if (alpha < threshold && threshold != 0xff) + return dst; + + /* alpha == 0xff or threshold == 0: straight copy (memcpy path). */ + if (alpha == 0xff || threshold == 0) + return src; + + /* Otherwise a uniform blend with the scalar alpha. */ + return (uint8_t)((dst * (0xff - alpha) + src * alpha) / 0xff); +} + +static uint16_t expected_blit10_noalpha(uint16_t dst, uint16_t src, + int alpha, int threshold) +{ + /* "nothing to do" early return: dst is left untouched. */ + if (alpha < threshold && threshold != 0x3ff) + return dst; + + /* alpha == 0x3ff or threshold == 0: straight copy (memcpy path). */ + if (alpha == 0x3ff || threshold == 0) + return src; + + /* Otherwise a uniform blend with the scalar alpha. */ + return (uint16_t)((dst * (0x3ff - alpha) + src * alpha) / 0x3ff); +} + +/** @internal @This checks the result of a yuv420p blit whose source carries an + * "a8" alpha plane. It reads that plane and compares every sampled dst octet on + * the y8/u8/v8 planes against expected_blit(), returning the number of checks + * performed. */ +static unsigned long check_yuv420p_blit(struct ubuf *dst, struct ubuf *src, + int alpha, int threshold) +{ + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride, a_stride; + const uint8_t *buffer; + const uint8_t *a_buffer; + unsigned long checks = 0; + + ubase_assert(ubuf_pic_plane_size(src, "a8", &a_stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(src, "a8", 0, 0, 64, 64, &a_buffer)); + + /* y8: dst's 16 blended with src's 235 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "y8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t a8 = a_buffer[y * a_stride + x]; + uint8_t got = buffer[y * stride + x]; + uint8_t exp = expected_blit(16, 235, a8, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y8", 0, 0, 64, 64)); + + /* u8: dst's 128 blended with src's 64 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "u8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t a8 = a_buffer[y * a_stride + x]; + uint8_t got = buffer[(y / 2) * stride + (x / 2)]; + uint8_t exp = expected_blit(128, 64, a8, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on u8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u8", 0, 0, 64, 64)); + + /* v8: dst's 128 blended with src's 192 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "v8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "v8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t a8 = a_buffer[y * a_stride + x]; + uint8_t got = buffer[(y / 2) * stride + (x / 2)]; + uint8_t exp = expected_blit(128, 192, a8, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on v8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "v8", 0, 0, 64, 64)); + + ubase_assert(ubuf_pic_plane_unmap(src, "a8", 0, 0, 64, 64)); + return checks; +} + +/** @internal @This checks the result of a yuv420p10le blit whose source carries + * an "a10l" alpha plane. It reads that plane and compares every sampled dst word + * on the y10l/u10l/v10l planes against expected_blit10(), returning the number + * of checks performed. */ +static unsigned long check_yuv420p10le_blit(struct ubuf *dst, struct ubuf *src, + int alpha, int threshold) +{ + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride, a_stride; + const uint8_t *buffer; + const uint8_t *a_buffer; + unsigned long checks = 0; + + ubase_assert(ubuf_pic_plane_size(src, "a10l", &a_stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(src, "a10l", 0, 0, 64, 64, &a_buffer)); + + /* y10l: dst's 64 blended with src's 940 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "y10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t a10 = ((const uint16_t *)(a_buffer + y * a_stride))[x]; + uint16_t got = ((const uint16_t *)(buffer + y * stride))[x]; + uint16_t exp = expected_blit10(64, 940, a10, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y10l", 0, 0, 64, 64)); + + /* u10l: dst's 512 blended with src's 256 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "u10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t a10 = ((const uint16_t *)(a_buffer + y * a_stride))[x]; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10(512, 256, a10, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on u10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u10l", 0, 0, 64, 64)); + + /* v10l: dst's 512 blended with src's 768 by the alpha plane sample. */ + ubase_assert(ubuf_pic_plane_size(dst, "v10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "v10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t a10 = ((const uint16_t *)(a_buffer + y * a_stride))[x]; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10(512, 768, a10, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on v10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "v10l", 0, 0, 64, 64)); + + ubase_assert(ubuf_pic_plane_unmap(src, "a10l", 0, 0, 64, 64)); + return checks; +} + +int main(int argc, char **argv) +{ + struct umem_mgr *umem_mgr = umem_alloc_mgr_alloc(); + assert(umem_mgr != NULL); + + struct ubuf_mgr *dst_mgr, *src_mgr; + struct ubuf *dst, *src; + unsigned long checks; + int alpha, threshold; + + /* 8-bit alpha blending: "a8" alpha plane, saturation value 0xff. */ + + /* dst: single luma plane. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y8", 1, 1, 1)); + + /* src: luma plane plus an 8-bit alpha plane. */ + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a8", 1, 1, 1)); + + dst = ubuf_pic_alloc(dst_mgr, 1, 1); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 1, 1); + assert(src != NULL); + + checks = 0; + + /* The copy branch (threshold == 0) is a plain memcpy of src, independent + * of both the alpha plane and the alpha multiplier, so a handful of + * representative values suffice. It is tested here rather than in the + * sweep below, where it would otherwise be repeated for every (a, alpha) + * pair without adding coverage. */ + { + static const int copy_a[] = { 0, 1, 128, 254, 255 }; + static const int copy_alpha[] = { 0, 128, 255 }; + for (size_t i = 0; i < UBASE_ARRAY_SIZE(copy_a); i++) { + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 1, 1, + (const uint8_t []){ 255 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 0, 0, 1, 1, + (const uint8_t []){ copy_a[i] }, 1)); + for (size_t k = 0; k < UBASE_ARRAY_SIZE(copy_alpha); k++) { + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 1, 1, + (const uint8_t []){ 0 }, 1)); + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1, 1, + copy_alpha[k], 0)); + uint8_t got = read_octet(dst, "y8"); + uint8_t exp = expected_blit(0, 255, copy_a[i], copy_alpha[k], 0); + assert(got == exp); + checks++; + } + } + } + + /* For every possible alpha plane value, exercise ubuf_pic_blit() with a + * spread of alpha/threshold values covering its remaining branches: + * - 0 < threshold < 0xff -> on/off blending, both with alpha == 0xff + * (raw alpha plane) and alpha != 0xff + * (scaled alpha plane) + * - threshold == 0xff -> smooth blending, again both alpha cases + * The values a-1, a and a+1 straddle the threshold comparison boundary. + * threshold == 0 (copy) is dropped from the sweep and tested above. */ + for (int a = 0; a <= 255; a++) { + /* src is constant across the inner loop: opaque luma, alpha = a. */ + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 1, 1, + (const uint8_t []){ 255 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 0, 0, 1, 1, + (const uint8_t []){ a }, 1)); + + const int alpha_candidates[] = { 0, 255, a-32, a-1, a, a+1, a+32 }; + const int threshold_candidates[] = { 255, a-32, a-1, a, a+1, a+32 }; + + for (size_t ai = 0; ai < UBASE_ARRAY_SIZE(alpha_candidates); ai++) { + alpha = alpha_candidates[ai]; + if (alpha < 0 || alpha > 255) + continue; + + for (size_t ti = 0; ti < UBASE_ARRAY_SIZE(threshold_candidates); ti++) { + threshold = threshold_candidates[ti]; + if (threshold < 0 || threshold > 255) + continue; + + /* dst is reset before every call since blit overwrites it. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 1, 1, + (const uint8_t []){ 0 }, 1)); + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1, 1, + alpha, threshold)); + + uint8_t got = read_octet(dst, "y8"); + uint8_t exp = expected_blit(0, 255, a, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch: a=%d alpha=%d threshold=%d: " + "got %u, expected %u\n", + a, alpha, threshold, got, exp); + assert(got == exp); + checks++; + } + } + } + + printf("%lu 8-bit blit checks passed\n", checks); + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + /* 10-bit alpha blending: "a10l" alpha plane, saturation value 0x3ff. + * Identical in structure to the 8-bit section above, but the samples are + * 16-bit little-endian and the range spans 0 to 1023. */ + + /* dst: single 10-bit luma plane. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y10l", 1, 1, 2)); + + /* src: 10-bit luma plane plus a 10-bit alpha plane. */ + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a10l", 1, 1, 2)); + + dst = ubuf_pic_alloc(dst_mgr, 1, 1); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 1, 1); + assert(src != NULL); + + checks = 0; + + /* Copy branch (threshold == 0), tested separately as in the 8-bit case. */ + { + static const int copy_a[] = { 0, 1, 512, 1022, 1023 }; + static const int copy_alpha[] = { 0, 512, 1023 }; + for (size_t i = 0; i < UBASE_ARRAY_SIZE(copy_a); i++) { + ubase_assert(ubuf_pic_plane_set_color(src, "y10l", 0, 0, 1, 1, + (const uint8_t []){ 0xff, 0x3 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 0, 0, 1, 1, + (const uint8_t []){ copy_a[i] & 0xff, copy_a[i] >> 8 }, 2)); + for (size_t k = 0; k < UBASE_ARRAY_SIZE(copy_alpha); k++) { + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 1, 1, + (const uint8_t []){ 0, 0 }, 2)); + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1, 1, + copy_alpha[k], 0)); + uint16_t got = read_word(dst, "y10l"); + uint16_t exp = expected_blit10(0, 1023, copy_a[i], + copy_alpha[k], 0); + assert(got == exp); + checks++; + } + } + } + + for (int a = 0; a <= 1023; a++) { + /* src is constant across the inner loop: opaque luma, alpha = a. */ + ubase_assert(ubuf_pic_plane_set_color(src, "y10l", 0, 0, 1, 1, + (const uint8_t []){ 0xff, 0x3 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 0, 0, 1, 1, + (const uint8_t []){ a & 0xff, a >> 8 }, 2)); + + const int alpha_candidates[] = { 0, 1023, a-32, a-1, a, a+1, a+32 }; + const int threshold_candidates[] = { 1023, a-32, a-1, a, a+1, a+32 }; + + for (size_t ai = 0; ai < UBASE_ARRAY_SIZE(alpha_candidates); ai++) { + alpha = alpha_candidates[ai]; + if (alpha < 0 || alpha > 1023) + continue; + + for (size_t ti = 0; ti < UBASE_ARRAY_SIZE(threshold_candidates); ti++) { + threshold = threshold_candidates[ti]; + if (threshold < 0 || threshold > 1023) + continue; + + /* dst is reset before every call since blit overwrites it. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 1, 1, + (const uint8_t []){ 0, 0 }, 2)); + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1, 1, + alpha, threshold)); + + uint16_t got = read_word(dst, "y10l"); + uint16_t exp = expected_blit10(0, 1023, a, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch: a=%d alpha=%d threshold=%d: " + "got %u, expected %u\n", + a, alpha, threshold, got, exp); + assert(got == exp); + checks++; + } + } + } + + printf("%lu 10-bit blit checks passed\n", checks); + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + /* No alpha plane, uniform blend: src has no "a8"/"a10l" plane, so + * ubuf_pic_blit() falls back to ubuf_pic_blit_alpha() with + * alpha_plane == NULL. With threshold 0xff the scalar alpha is applied + * uniformly across the plane: dst = (dst*(0xff-alpha) + src*alpha)/0xff. + * With dst = 0 and src = 255 the result is simply alpha. */ + + /* dst: single luma plane. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y8", 1, 1, 1)); + + /* src: luma plane only, no alpha plane. */ + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + + dst = ubuf_pic_alloc(dst_mgr, 1, 1); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 1, 1); + assert(src != NULL); + + checks = 0; + + for (alpha = 0; alpha <= 255; alpha++) { + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 1, 1, + (const uint8_t []){ 255 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 1, 1, + (const uint8_t []){ 0 }, 1)); + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1, 1, alpha, 255)); + + uint8_t got = read_octet(dst, "y8"); + uint8_t exp = expected_blit_noalpha(0, 255, alpha, 255); + if (got != exp) + fprintf(stderr, "mismatch: alpha=%d threshold=255: " + "got %u, expected %u\n", alpha, got, exp); + assert(got == exp); + checks++; + } + + printf("%lu no-alpha-plane blit checks passed\n", checks); + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + /* TODO: add a new section to test 10-bit blending without an alpha plane + * once that code can be run. (ubuf_pic_blit()'s no-alpha-plane fallback + * currently always calls the 8-bit ubuf_pic_blit_alpha(), so the 10-bit + * ubuf_pic_blit_alpha10() NULL path is not reachable through + * ubuf_pic_blit().) */ + + /* 64x64 yuv420p pictures. src additionally carries a full-resolution 8-bit + * alpha plane that is transparent (0) everywhere except an opaque (255) + * 32x32 square whose top-left corner is at (16, 16). A single on/off blit + * (alpha 255, threshold 20) copies that square into dst; the result is not + * checked here. */ + + /* dst: yuv420p. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "u8", 2, 2, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "v8", 2, 2, 1)); + + /* src: yuv420p plus a full-resolution 8-bit alpha plane. */ + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u8", 2, 2, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v8", 2, 2, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a8", 1, 1, 1)); + + dst = ubuf_pic_alloc(dst_mgr, 64, 64); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 64, 64); + assert(src != NULL); + + /* dst: neutral black in limited-range YUV. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + /* src: a flat colour, opaque only inside the 32x32 square at (16, 16). */ + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 64, 64, + (const uint8_t []){ 235 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "u8", 0, 0, 64, 64, + (const uint8_t []){ 64 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "v8", 0, 0, 64, 64, + (const uint8_t []){ 192 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 0, 0, 64, 64, + (const uint8_t []){ 0 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 16, 16, 32, 32, + (const uint8_t []){ 255 }, 1)); + + /* Check a 3x3 grid of samples on each plane. The alpha is opaque only + * inside the 32x32 square [16, 48) x [16, 48); with alpha 255 and threshold + * 20 the blit copies src there and leaves dst everywhere else, so only + * (32, 32) falls inside the square. On the subsampled chroma planes a luma + * point (x, y) maps to chroma sample (x/2, y/2). */ + alpha = 255; + threshold = 20; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + checks = check_yuv420p_blit(dst, src, alpha, threshold); + printf("%lu yuv420p blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Second pass: reset dst to its original values and blit the same src + * again, this time with a reduced alpha of 128 (same threshold of 20). + * With alpha != 0xff the plane value is scaled (a = raw * 128 / 255) before + * the threshold test, exercising the on/off path the first blit skipped. + * Inside the square the effective alpha is 128, still above 20, so the + * copied region matches the first pass. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + alpha = 128; + threshold = 20; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + checks = check_yuv420p_blit(dst, src, alpha, threshold); + printf("%lu yuv420p blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Third pass: reset dst, then overwrite src's alpha plane with a diagonal + * gradient (0 at the top-left, rising towards the bottom-right). Blit with + * alpha 255 and threshold 255: threshold 255 selects smooth per-pixel + * blending, so each dst sample becomes a weighted mix of dst and src, + * (dst * (255 - a) + src * a) / 255, using the local gradient alpha a. + * Rather than recomputing the gradient, the check reads the alpha the blit + * actually saw straight from the "a8" plane. All the sample points sit on + * even coordinates, so the subsampled chroma planes read the same alpha + * sample as luma. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + fill_plane_gradient(src, "a8", 0, 0, 64, 64); + + alpha = 255; + threshold = 255; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + checks = check_yuv420p_blit(dst, src, alpha, threshold); + printf("%lu yuv420p gradient blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Fourth pass: the same gradient blit, but with alpha 128. Threshold 255 + * still selects smooth blending; with alpha != 0xff the gradient value is + * scaled first (a = raw * 128 / 255), so each dst sample becomes + * (dst * (255 - a) + src * a) / 255. As before the check reads the raw a8 + * value the blit saw and lets expected_blit apply the same scaling. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + fill_plane_gradient(src, "a8", 0, 0, 64, 64); + + alpha = 128; + threshold = 255; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + checks = check_yuv420p_blit(dst, src, alpha, threshold); + printf("%lu yuv420p gradient blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Fifth pass: a source with no alpha plane. Rebuild src with only + * y8/u8/v8 (freeing the alpha-carrying one), reset dst, then blit with + * alpha 128 and threshold 1. With no alpha plane the blit applies the + * scalar alpha uniformly, so every sample is a blend + * (dst * (255 - 128) + src * 128) / 255. */ + ubuf_free(src); + ubuf_mgr_release(src_mgr); + + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u8", 2, 2, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v8", 2, 2, 1)); + + src = ubuf_pic_alloc(src_mgr, 64, 64); + assert(src != NULL); + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 64, 64, + (const uint8_t []){ 235 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "u8", 0, 0, 64, 64, + (const uint8_t []){ 64 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "v8", 0, 0, 64, 64, + (const uint8_t []){ 192 }, 1)); + + /* dst: neutral black in limited-range YUV. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + alpha = 128; + threshold = 1; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + checks = 0; + { + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride; + const uint8_t *buffer; + + /* y8: uniform blend of dst's 16 with src's 235. */ + ubase_assert(ubuf_pic_plane_size(dst, "y8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t got = buffer[y * stride + x]; + uint8_t exp = expected_blit_noalpha(16, 235, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y8", 0, 0, 64, 64)); + + /* u8: uniform blend of dst's 128 with src's 64. */ + ubase_assert(ubuf_pic_plane_size(dst, "u8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t got = buffer[(y / 2) * stride + (x / 2)]; + uint8_t exp = expected_blit_noalpha(128, 64, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on u8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u8", 0, 0, 64, 64)); + + /* v8: uniform blend of dst's 128 with src's 192. */ + ubase_assert(ubuf_pic_plane_size(dst, "v8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "v8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t got = buffer[(y / 2) * stride + (x / 2)]; + uint8_t exp = expected_blit_noalpha(128, 192, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on v8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "v8", 0, 0, 64, 64)); + printf("%lu yuv420p no-alpha blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + } + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + /* 64x64 yuv420p10le pictures: the 10-bit analogue of the yuv420p case + * above. The sample values are scaled up by four for the two extra bits, + * and the alpha square is fully opaque (1023). A single on/off blit (alpha + * 1023, threshold 80) copies the 32x32 square at (16, 16) into dst. */ + + /* dst: yuv420p10le. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "u10l", 2, 2, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "v10l", 2, 2, 2)); + + /* src: yuv420p10le plus a full-resolution 10-bit alpha plane. */ + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u10l", 2, 2, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v10l", 2, 2, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a10l", 1, 1, 2)); + + dst = ubuf_pic_alloc(dst_mgr, 64, 64); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 64, 64); + assert(src != NULL); + + /* dst: neutral black in limited-range YUV. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + /* src: a flat colour, opaque only inside the 32x32 square at (16, 16). */ + ubase_assert(ubuf_pic_plane_set_color(src, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0xac, 0x3 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x1 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x3 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 16, 16, 32, 32, + (const uint8_t []){ 0xff, 0x3 }, 2)); + + /* Check the same 3x3 grid on each plane. As before only (32, 32) is inside + * the opaque square; on the subsampled chroma planes a luma point (x, y) + * maps to sample (x/2, y/2). The samples are 16-bit. */ + alpha = 1023; + threshold = 80; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + checks = check_yuv420p10le_blit(dst, src, alpha, threshold); + printf("%lu yuv420p10le blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Second pass: reset dst and blit the same src again with a reduced alpha + * of 512 (same threshold of 80). With alpha != 0x3ff the plane value is + * scaled (a = raw * 512 / 1023) before the threshold test. Inside the + * square the effective alpha is 512, still above 80, so the copied region + * matches the first pass. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + alpha = 512; + threshold = 80; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + checks = check_yuv420p10le_blit(dst, src, alpha, threshold); + printf("%lu yuv420p10le blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Replace src's alpha with a diagonal gradient for the remaining passes. */ + fill_plane_gradient16(src, "a10l", 0, 0, 64, 64); + + /* Third pass: reset dst and blit with alpha 1023 and threshold 1023. + * Threshold 1023 selects smooth per-pixel blending; with alpha == 0x3ff the + * gradient alpha is used directly, so each dst sample becomes + * (dst * (1023 - a) + src * a) / 1023. The check reads the alpha the blit + * saw straight from the "a10l" plane. All the sample points sit on even + * coordinates, so the subsampled chroma planes read the same alpha sample + * as luma. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + alpha = 1023; + threshold = 1023; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + checks = check_yuv420p10le_blit(dst, src, alpha, threshold); + printf("%lu yuv420p10le gradient blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Fourth pass: the same gradient blit with alpha 512. Threshold 1023 still + * selects smooth blending; with alpha != 0x3ff the gradient value is scaled + * first (a = raw * 512 / 1023) before the blend. The check again reads the + * raw a10l value and lets expected_blit10 apply the scaling. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + alpha = 512; + threshold = 1023; + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + checks = check_yuv420p10le_blit(dst, src, alpha, threshold); + printf("%lu yuv420p10le gradient blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + + /* Fifth pass: a 10-bit source with no alpha plane. Rebuild src with only + * y10l/u10l/v10l (freeing the alpha-carrying one), reset dst, then blit + * with alpha 1023 and threshold 0. threshold 0 forces the memcpy path, so + * with no alpha plane dst becomes an exact copy of src everywhere. */ + ubuf_free(src); + ubuf_mgr_release(src_mgr); + + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u10l", 2, 2, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v10l", 2, 2, 2)); + + src = ubuf_pic_alloc(src_mgr, 64, 64); + assert(src != NULL); + ubase_assert(ubuf_pic_plane_set_color(src, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0xac, 0x3 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x1 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x3 }, 2)); + + /* dst: neutral black in limited-range YUV. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + checks = 0; + { + alpha = 1023; + threshold = 0; + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride; + const uint8_t *buffer; + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + /* y10l: a straight copy of src's 940. */ + ubase_assert(ubuf_pic_plane_size(dst, "y10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + y * stride))[x]; + uint16_t exp = expected_blit10_noalpha(64, 940, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y10l", 0, 0, 64, 64)); + + /* u10l: a straight copy of src's 256. */ + ubase_assert(ubuf_pic_plane_size(dst, "u10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10_noalpha(512, 256, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on u10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u10l", 0, 0, 64, 64)); + + /* v10l: a straight copy of src's 768. */ + ubase_assert(ubuf_pic_plane_size(dst, "v10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "v10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10_noalpha(512, 768, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on v10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "v10l", 0, 0, 64, 64)); + printf("%lu yuv420p10le no-alpha blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + } + + /* Sixth pass: the same no-alpha 10-bit source, but blit with alpha 512 and + * threshold 1. This case is EXPECTED TO FAIL. When src has no alpha plane + * ubuf_pic_blit always falls back to the 8-bit ubuf_pic_blit_alpha, which + * blends the 10-bit little-endian buffer one byte at a time. With alpha 512 + * the 8-bit weight (0xff - alpha) is negative and the per-byte results + * overflow, so the recombined 16-bit samples (e.g. 1560 for luma) do not + * match expected_blit10_noalpha's ideal blend of + * (dst * (1023 - 512) + src * 512) / 1023. The asserts below are commented + * out so the mismatches are reported without aborting the run. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 64, 64, + (const uint8_t []){ 0x40, 0 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 64, 64, + (const uint8_t []){ 0, 0x2 }, 2)); + + checks = 0; + { + alpha = 512; + threshold = 1; + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride; + const uint8_t *buffer; + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + /* y10l: uniform blend of dst's 64 with src's 940. */ + ubase_assert(ubuf_pic_plane_size(dst, "y10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + y * stride))[x]; + uint16_t exp = expected_blit10_noalpha(64, 940, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + /* assert(got == exp); expected to fail; see note above */ + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y10l", 0, 0, 64, 64)); + + /* u10l: uniform blend of dst's 512 with src's 256. */ + ubase_assert(ubuf_pic_plane_size(dst, "u10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10_noalpha(512, 256, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on u10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + /* assert(got == exp); expected to fail; see note above */ + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u10l", 0, 0, 64, 64)); + + /* v10l: uniform blend of dst's 512 with src's 768. */ + ubase_assert(ubuf_pic_plane_size(dst, "v10l", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "v10l", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint16_t got = ((const uint16_t *)(buffer + (y / 2) * stride))[x / 2]; + uint16_t exp = expected_blit10_noalpha(512, 768, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on v10l at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + /* assert(got == exp); expected to fail; see note above */ + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "v10l", 0, 0, 64, 64)); + printf("%lu yuv420p10le no-alpha blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + } + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + /* 64x64 pictures with a semi-planar nv12 dst and a planar yuv420p src. nv12 + * keeps luma in a full-resolution "y8" plane and interleaves the U and V + * chroma samples in a single "u8v8" plane (macropixel_size 2, subsampled + * 2x2), while yuv420p keeps them in separate "u8"/"v8" planes. */ + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "u8v8", 2, 2, 2)); + + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u8", 2, 2, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v8", 2, 2, 1)); + + dst = ubuf_pic_alloc(dst_mgr, 64, 64); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 64, 64); + assert(src != NULL); + + /* dst: neutral black in limited-range YUV (chroma 128 for both U and V). */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + /* src: a flat colour (no alpha plane). */ + ubase_assert(ubuf_pic_plane_set_color(src, "y8", 0, 0, 64, 64, + (const uint8_t []){ 235 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "u8", 0, 0, 64, 64, + (const uint8_t []){ 64 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "v8", 0, 0, 64, 64, + (const uint8_t []){ 192 }, 1)); + + /* Blit with alpha 255 and threshold 0: threshold 0 is a straight copy, so + * dst takes src across the whole 64x64 area regardless of the alpha plane. + * The same 3x3 grid is checked on each plane as in the first yuv420p test; + * on the subsampled chroma planes a luma point (x, y) maps to sample + * (x/2, y/2). */ + checks = 0; + { + alpha = 255; + threshold = 0; + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride; + const uint8_t *buffer; + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + /* y8: the copy replaces dst's 16 with src's 235 everywhere. */ + ubase_assert(ubuf_pic_plane_size(dst, "y8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t got = buffer[y * stride + x]; + uint8_t exp = expected_blit_noalpha(16, 235, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + assert(got == exp); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y8", 0, 0, 64, 64)); + + /* u8v8: the interleaved chroma plane. Each 2-byte sample holds U at + * offset 0 and V at offset 1; the copy takes src's U = 64 and V = 192. */ + ubase_assert(ubuf_pic_plane_size(dst, "u8v8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u8v8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + const uint8_t *sample = buffer + (y / 2) * stride + (x / 2) * 2; + uint8_t got_u = sample[0]; + uint8_t got_v = sample[1]; + uint8_t exp_u = expected_blit_noalpha(128, 64, alpha, threshold); + uint8_t exp_v = expected_blit_noalpha(128, 192, alpha, threshold); + if (got_u != exp_u) + fprintf(stderr, "mismatch on u8v8 U at (%d, %d): got %u, expected %u\n", + x, y, got_u, exp_u); + assert(got_u == exp_u); + checks++; + if (got_v != exp_v) + fprintf(stderr, "mismatch on u8v8 V at (%d, %d): got %u, expected %u\n", + x, y, got_v, exp_v); + assert(got_v == exp_v); + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u8v8", 0, 0, 64, 64)); + printf("%lu nv12 blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + } + + /* Second nv12 pass: the same no-alpha src, but blit with alpha 128 and + * threshold 0. This case is EXPECTED TO FAIL on the chroma plane. y8 is a + * single plane, so threshold 0 gives a plain memcpy and dst takes src's + * 235. But nv12's u8v8 is filled from src's two separate u8/v8 planes, and + * that de-planarising path blends even when threshold is 0: each byte + * becomes (dst * (255 - 128) + src * 128) / 255 rather than a straight + * copy, so the chroma samples do not match expected_blit_noalpha's + * threshold-0 copy. The asserts below are commented out so the mismatches + * are reported without aborting the run. */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 64, 64, + (const uint8_t []){ 16 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8v8", 0, 0, 64, 64, + (const uint8_t []){ 128 }, 1)); + + checks = 0; + { + alpha = 128; + threshold = 0; + static const struct { int x, y; } points[] = { + { 8, 8 }, { 32, 8 }, { 56, 8 }, + { 8, 32 }, { 32, 32 }, { 56, 32 }, + { 8, 56 }, { 32, 56 }, { 56, 56 }, + }; + size_t stride; + const uint8_t *buffer; + + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 64, 64, alpha, threshold)); + + /* y8: a single plane, so the copy replaces dst's 16 with src's 235. */ + ubase_assert(ubuf_pic_plane_size(dst, "y8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "y8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + uint8_t got = buffer[y * stride + x]; + uint8_t exp = expected_blit_noalpha(16, 235, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on y8 at (%d, %d): got %u, expected %u\n", + x, y, got, exp); + /* assert(got == exp); expected to fail; see note above */ + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "y8", 0, 0, 64, 64)); + + /* u8v8: the interleaved chroma plane, filled from src's separate u8/v8 + * planes. The de-planarising path blends here even at threshold 0. */ + ubase_assert(ubuf_pic_plane_size(dst, "u8v8", &stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_read(dst, "u8v8", 0, 0, 64, 64, &buffer)); + for (size_t i = 0; i < UBASE_ARRAY_SIZE(points); i++) { + int x = points[i].x, y = points[i].y; + const uint8_t *sample = buffer + (y / 2) * stride + (x / 2) * 2; + uint8_t got_u = sample[0]; + uint8_t got_v = sample[1]; + uint8_t exp_u = expected_blit_noalpha(128, 64, alpha, threshold); + uint8_t exp_v = expected_blit_noalpha(128, 192, alpha, threshold); + if (got_u != exp_u) + fprintf(stderr, "mismatch on u8v8 U at (%d, %d): got %u, expected %u\n", + x, y, got_u, exp_u); + /* assert(got_u == exp_u); expected to fail; see note above */ + checks++; + if (got_v != exp_v) + fprintf(stderr, "mismatch on u8v8 V at (%d, %d): got %u, expected %u\n", + x, y, got_v, exp_v); + /* assert(got_v == exp_v); expected to fail; see note above */ + checks++; + } + ubase_assert(ubuf_pic_plane_unmap(dst, "u8v8", 0, 0, 64, 64)); + printf("%lu nv12 blit checks passed (alpha %d, threshold %d)\n", + checks, alpha, threshold); + } + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + + umem_mgr_release(umem_mgr); + return 0; +} From fb8ae035e9940fad1bd1e56c31c10082c38429de Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 8 Jul 2026 12:19:47 +0200 Subject: [PATCH 03/30] ubuf_pic_blit_test: add 1920x1080 benchmarks Take run count on the command line and repeatedly call ubuf_pic_blit for yuv422p and yuv422p10le pictures. Currently only tests threshold copy using alpha plane. --- tests/ubuf_pic_blit_test.c | 153 +++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/tests/ubuf_pic_blit_test.c b/tests/ubuf_pic_blit_test.c index 6bfb2d2c4..6ecaccd5a 100644 --- a/tests/ubuf_pic_blit_test.c +++ b/tests/ubuf_pic_blit_test.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #define UBUF_POOL_DEPTH 1 #define UBUF_PREPEND 2 @@ -1374,6 +1376,157 @@ int main(int argc, char **argv) ubuf_mgr_release(src_mgr); ubuf_mgr_release(dst_mgr); + /* Benchmark: repeatedly blit a full 1920x1080 yuv422p picture through a + * rectangular alpha mask and report the throughput. */ + int calls = 0; + if (argc > 1) + calls = atoi(argv[1]); + + /* First benchmark: 8-bit yuv422p. */ + if (calls > 0) { + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "u8", 2, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "v8", 2, 1, 1)); + + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y8", 1, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u8", 2, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v8", 2, 1, 1)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a8", 1, 1, 1)); + + dst = ubuf_pic_alloc(dst_mgr, 1920, 1080); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 1920, 1080); + assert(src != NULL); + + /* dst: middle gray (all planes 128). */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y8", 0, 0, 1920, 1080, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u8", 0, 0, 1920, 1080, + (const uint8_t []){ 128 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v8", 0, 0, 1920, 1080, + (const uint8_t []){ 128 }, 1)); + + /* src: gradients on the colour planes; alpha zero everywhere except a + * 1280x120 opaque rectangle three quarters down and horizontally + * centered. */ + fill_plane_gradient(src, "y8", 0, 0, 1920, 1080); + fill_plane_gradient(src, "u8", 0, 0, 1920, 1080); + fill_plane_gradient(src, "v8", 0, 0, 1920, 1080); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 0, 0, 1920, 1080, + (const uint8_t []){ 0 }, 1)); + ubase_assert(ubuf_pic_plane_set_color(src, "a8", 320, 810, 1280, 120, + (const uint8_t []){ 255 }, 1)); + + { + struct timespec start, end; + + alpha = 255; + threshold = 80; + + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < calls; i++) { + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1920, 1080, + alpha, threshold)); + if ((i + 1) % 1000 == 0) { + clock_gettime(CLOCK_MONOTONIC, &end); + double ms = (end.tv_sec - start.tv_sec) * 1000.0 + + (end.tv_nsec - start.tv_nsec) / 1000000.0; + printf("1000 yuv422p blits: %.3f ms (%.1f fps)\n", + ms, 1000.0 / (ms / 1000.0)); + start = end; + } + } + } + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + } + + /* Benchmark: repeatedly blit a full 1920x1080 yuv422p10le picture through a + * rectangular alpha mask and report the throughput. */ + if (calls > 0) { + dst_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(dst_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "u10l", 2, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(dst_mgr, "v10l", 2, 1, 2)); + + src_mgr = ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "y10l", 1, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "u10l", 2, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "v10l", 2, 1, 2)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, "a10l", 1, 1, 2)); + + dst = ubuf_pic_alloc(dst_mgr, 1920, 1080); + assert(dst != NULL); + src = ubuf_pic_alloc(src_mgr, 1920, 1080); + assert(src != NULL); + + /* dst: middle gray (all planes 512 == 0x200). */ + ubase_assert(ubuf_pic_plane_set_color(dst, "y10l", 0, 0, 1920, 1080, + (const uint8_t []){ 0x00, 0x02 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "u10l", 0, 0, 1920, 1080, + (const uint8_t []){ 0x00, 0x02 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(dst, "v10l", 0, 0, 1920, 1080, + (const uint8_t []){ 0x00, 0x02 }, 2)); + + /* src: gradients on the colour planes; alpha zero everywhere except a + * 1280x120 opaque rectangle three quarters down and horizontally + * centered. */ + fill_plane_gradient16(src, "y10l", 0, 0, 1920, 1080); + fill_plane_gradient16(src, "u10l", 0, 0, 1920, 1080); + fill_plane_gradient16(src, "v10l", 0, 0, 1920, 1080); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 0, 0, 1920, 1080, + (const uint8_t []){ 0x00, 0x00 }, 2)); + ubase_assert(ubuf_pic_plane_set_color(src, "a10l", 320, 810, 1280, 120, + (const uint8_t []){ 0xff, 0x03 }, 2)); + + { + struct timespec start, end; + + alpha = 1023; + threshold = 80; + + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < calls; i++) { + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, 1920, 1080, + alpha, threshold)); + if ((i + 1) % 1000 == 0) { + clock_gettime(CLOCK_MONOTONIC, &end); + double ms = (end.tv_sec - start.tv_sec) * 1000.0 + + (end.tv_nsec - start.tv_nsec) / 1000000.0; + printf("1000 yuv422p10le blits: %.3f ms (%.1f fps)\n", + ms, 1000.0 / (ms / 1000.0)); + start = end; + } + } + } + + ubuf_free(src); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(dst_mgr); + } + umem_mgr_release(umem_mgr); return 0; } From 24c0c21587cf705a00534609e9d73390ea6d063f Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 5 Aug 2026 22:16:28 +0100 Subject: [PATCH 04/30] upipe: move ubuf_pic_blit out of header Reworked on top of the background blit: the _bg functions carry the implementation, so those are what moves to ubuf_pic.c. The in-place entry points (ubuf_pic_blit, ubuf_pic_blit_alpha and ubuf_pic_blit_alpha10) stay in the header as the one-line wrappers they already were. --- include/upipe/ubuf_pic.h | 426 +-------------------------------- lib/upipe/ubuf_pic.c | 504 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 510 insertions(+), 420 deletions(-) diff --git a/include/upipe/ubuf_pic.h b/include/upipe/ubuf_pic.h index f5a1ac297..b58b39aa4 100644 --- a/include/upipe/ubuf_pic.h +++ b/include/upipe/ubuf_pic.h @@ -371,222 +371,14 @@ static inline int ubuf_pic_resize(struct ubuf *ubuf, int hskip, int vskip, * their alpha value is more than this value * @return an error code */ -static inline int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, +int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, struct ubuf *src, int dest_hoffset, int dest_voffset, int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, const uint8_t *alpha_plane, int alpha_stride, - const int alpha, const int threshold) -{ - if (alpha_plane == NULL && alpha < threshold && threshold != 0xff) { - if (bg == NULL) - return UBASE_ERR_NONE; /* nothing to do */ - /* the subpicture is invisible: plain copy of the background */ - return ubuf_pic_blit_alpha_bg(dest, NULL, bg, - dest_hoffset, dest_voffset, 0, 0, - bg_hoffset, bg_voffset, - extract_hsize, extract_vsize, - NULL, 0, 0xff, 0); - } - - uint8_t src_macropixel; - UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) - uint8_t dest_macropixel; - UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) - if (unlikely(dest_macropixel != src_macropixel)) - return UBASE_ERR_INVALID; - if (bg != NULL) { - uint8_t bg_macropixel; - UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) - if (unlikely(bg_macropixel != dest_macropixel)) - return UBASE_ERR_INVALID; - } - -#define MAX_PLANES 3 - - const char *chroma; - ubuf_pic_foreach_plane(dest, chroma) { - const char *in_chroma[MAX_PLANES] = { chroma }; - unsigned in_planes = 1; - int err; - - size_t dest_stride; - uint8_t dest_hsub, dest_vsub, dest_macropixel_size; - UBASE_RETURN(ubuf_pic_plane_size(dest, chroma, - &dest_stride, &dest_hsub, &dest_vsub, - &dest_macropixel_size)) - - size_t src_stride; - uint8_t src_hsub, src_vsub, src_macropixel_size; - err = ubuf_pic_plane_size(src, in_chroma[0], &src_stride, &src_hsub, - &src_vsub, &src_macropixel_size); - if (!ubase_check(err)) { - if (!strcmp(chroma, "u8v8")) { - in_chroma[0] = "u8"; - in_chroma[1] = "v8"; - in_planes = 2; - } else - return err; - - for (unsigned i = 0; i < in_planes; i++) { - UBASE_RETURN(ubuf_pic_plane_size(src, in_chroma[i], &src_stride, - &src_hsub, &src_vsub, - &src_macropixel_size)); - if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || - src_macropixel_size != - dest_macropixel_size / in_planes)) - return UBASE_ERR_INVALID; - } - } else if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || - src_macropixel_size != dest_macropixel_size)) - return UBASE_ERR_INVALID; - - size_t bg_stride = dest_stride; - if (bg != NULL) { - uint8_t bg_hsub, bg_vsub, bg_macropixel_size; - UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, - &bg_hsub, &bg_vsub, &bg_macropixel_size)) - if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || - bg_macropixel_size != dest_macropixel_size)) - return UBASE_ERR_INVALID; - } - - uint8_t *dest_buffer; - const uint8_t *bg_buffer; - const uint8_t *in[MAX_PLANES]; - UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize, &dest_buffer)) - if (bg != NULL) { - err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, - extract_hsize, extract_vsize, - &bg_buffer); - if (unlikely(!ubase_check(err))) { - ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - return err; - } - } else - bg_buffer = dest_buffer; - for (unsigned i = 0; i < in_planes; i++) { - err = - ubuf_pic_plane_read(src, in_chroma[i], src_hoffset, src_voffset, - extract_hsize, extract_vsize, &in[i]); - if (unlikely(!ubase_check(err))) { - for (; i; i--) - ubuf_pic_plane_unmap(src, in_chroma[i - 1], src_hoffset, - src_voffset, extract_hsize, - extract_vsize); - if (bg != NULL) - ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, - extract_hsize, extract_vsize); - ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - return err; - } - } - - int plane_hsize = extract_hsize / src_hsub / src_macropixel * - src_macropixel_size; - int plane_vsize = extract_vsize / src_vsub; - - for (int i = 0; i < plane_vsize; i++) { - if ((!alpha_plane && alpha == 0xff) || threshold == 0) { - if (in_planes == 1) - memcpy(dest_buffer, in[0], plane_hsize); - else { - for (int j = 0; j < plane_hsize; j++) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = in[p][j]; - } - } - } else if (!alpha_plane) { - for (int j = 0; j < plane_hsize; j++) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = - (bg_buffer[j * in_planes + p] * (0xff - alpha) + - in[p][j] * alpha) / - 0xff; - } - } else if (threshold != 0xff) { - /* This is an on/off blending - * if alpha is over the threshold, we use the subpicture pixel, - * otherwise the background pixel (left untouched when - * blending in place). - */ - if (alpha == 0xff) { - for (int j = 0; j < plane_hsize; j++) { - const uint8_t a = alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub]; - if (a > threshold) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = in[p][j]; - } else if (bg != NULL) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = - bg_buffer[j * in_planes + p]; - } - } - } else { - for (int j = 0; j < plane_hsize; j++) { - const uint8_t a = (uint16_t)alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub] * (uint16_t)alpha / 0xff; - if (a > threshold) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = in[p][j]; - } else if (bg != NULL) { - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = - bg_buffer[j * in_planes + p]; - } - } - } - } else { - /* smooth and slow blending */ - if (alpha == 0xff) { - for (int j = 0; j < plane_hsize; j++) { - const uint8_t a = alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub]; - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = - (bg_buffer[j * in_planes + p] * (0xff - a) + - in[p][j] * a) / - 0xff; - } - } else { - for (int j = 0; j < plane_hsize; j++) { - const uint8_t a = (uint16_t)alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub] * (uint16_t)alpha / 0xff; - for (int p = 0; p < in_planes; p++) - dest_buffer[j * in_planes + p] = - (bg_buffer[j * in_planes + p] * (0xff - a) + - in[p][j] * a) / - 0xff; - } - } - } - dest_buffer += dest_stride; - bg_buffer += bg_stride; - for (int p = 0; p < in_planes; p++) - in[p] += src_stride; - } - - err = ubuf_pic_plane_unmap(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - if (bg != NULL) - UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, - bg_voffset, extract_hsize, - extract_vsize)) - for (int p = 0; p < in_planes; p++) - UBASE_RETURN(ubuf_pic_plane_unmap(src, in_chroma[p], src_hoffset, - src_voffset, extract_hsize, - extract_vsize)) - UBASE_RETURN(err) - } - -#undef MAX_PLANES - - return UBASE_ERR_NONE; -} + const int alpha, const int threshold); /** @This blits a picture ubuf to another ubuf in place. * @@ -607,166 +399,14 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, } /** @see ubuf_pic_blit_alpha_bg */ -static inline int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, +int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, struct ubuf *src, int dest_hoffset, int dest_voffset, int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, const uint8_t *alpha_plane, int alpha_stride, - const int alpha, const int threshold) -{ - if (alpha_plane == NULL && alpha < threshold && threshold != 0x3ff) { - if (bg == NULL) - return UBASE_ERR_NONE; /* nothing to do */ - /* the subpicture is invisible: plain copy of the background */ - return ubuf_pic_blit_alpha10_bg(dest, NULL, bg, - dest_hoffset, dest_voffset, 0, 0, - bg_hoffset, bg_voffset, - extract_hsize, extract_vsize, - NULL, 0, 0x3ff, 0); - } - - uint8_t src_macropixel; - UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) - uint8_t dest_macropixel; - UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) - if (unlikely(dest_macropixel != src_macropixel)) - return UBASE_ERR_INVALID; - if (bg != NULL) { - uint8_t bg_macropixel; - UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) - if (unlikely(bg_macropixel != dest_macropixel)) - return UBASE_ERR_INVALID; - } - - const char *chroma; - ubuf_pic_foreach_plane(dest, chroma) { - size_t src_stride; - uint8_t src_hsub, src_vsub, src_macropixel_size; - UBASE_RETURN(ubuf_pic_plane_size(src, chroma, &src_stride, - &src_hsub, &src_vsub, &src_macropixel_size)) - - size_t dest_stride; - uint8_t dest_hsub, dest_vsub, dest_macropixel_size; - UBASE_RETURN(ubuf_pic_plane_size(dest, chroma, - &dest_stride, &dest_hsub, &dest_vsub, - &dest_macropixel_size)) - - if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || - src_macropixel_size != dest_macropixel_size)) - return UBASE_ERR_INVALID; - - size_t bg_stride = dest_stride; - if (bg != NULL) { - uint8_t bg_hsub, bg_vsub, bg_macropixel_size; - UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, - &bg_hsub, &bg_vsub, &bg_macropixel_size)) - if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || - bg_macropixel_size != dest_macropixel_size)) - return UBASE_ERR_INVALID; - } - - uint8_t *dest_buffer; - const uint8_t *bg_buffer; - const uint8_t *src_buffer; - UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize, &dest_buffer)) - int err; - if (bg != NULL) { - err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, - extract_hsize, extract_vsize, - &bg_buffer); - if (unlikely(!ubase_check(err))) { - ubuf_pic_plane_unmap(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - return err; - } - } else - bg_buffer = dest_buffer; - err = ubuf_pic_plane_read(src, chroma, src_hoffset, src_voffset, - extract_hsize, extract_vsize, - &src_buffer); - if (unlikely(!ubase_check(err))) { - if (bg != NULL) - ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, - extract_hsize, extract_vsize); - ubuf_pic_plane_unmap(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - return err; - } - - int plane_hsize = extract_hsize / src_hsub / src_macropixel * - src_macropixel_size; - int plane_vsize = extract_vsize / src_vsub; - - for (int i = 0; i < plane_vsize; i++) { - uint16_t *real_dst = (uint16_t *)dest_buffer; - const uint16_t *real_bg = (const uint16_t *)bg_buffer; - const uint16_t *real_src = (const uint16_t *)src_buffer; - const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); - - if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { - memcpy(dest_buffer, src_buffer, plane_hsize); - } else if (!alpha_plane) { - for (int j = 0; j < plane_hsize/2; j++) { - real_dst[j] = (real_bg[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; - } - } else if (threshold != 0x3ff) { - /* This is an on/off blending - * if alpha is over the threshold, we use the subpicture pixel, - * otherwise the background pixel (left untouched when - * blending in place). - */ - if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize/2; j++) { - const uint16_t a = real_alpha[j * src_hsub]; - if (a > threshold) real_dst[j] = real_src[j]; - else if (bg != NULL) real_dst[j] = real_bg[j]; - } - } else { - for (int j = 0; j < plane_hsize/2; j++) { - const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; - if (a > threshold) real_dst[j] = real_src[j]; - else if (bg != NULL) real_dst[j] = real_bg[j]; - } - } - } else { - /* smooth and slow blending */ - if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize/2; j++) { - const uint16_t a = real_alpha[j * src_hsub]; - real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; - } - } else { - for (int j = 0; j < plane_hsize/2; j++) { - const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; - real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; - } - } - } - dest_buffer += dest_stride; - bg_buffer += bg_stride; - src_buffer += src_stride; - } - - err = ubuf_pic_plane_unmap(dest, chroma, - dest_hoffset, dest_voffset, - extract_hsize, extract_vsize); - if (bg != NULL) - UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, - bg_hoffset, bg_voffset, - extract_hsize, extract_vsize)) - UBASE_RETURN(ubuf_pic_plane_unmap(src, chroma, - src_hoffset, src_voffset, - extract_hsize, extract_vsize)) - UBASE_RETURN(err) - } - return UBASE_ERR_NONE; -} + const int alpha, const int threshold); /** @see ubuf_pic_blit_alpha_bg */ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, @@ -812,67 +452,13 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, * @param threshold threshold parameter for alpha * @return an error code */ -static inline int ubuf_pic_blit_bg(struct ubuf *dest, struct ubuf *bg, +int ubuf_pic_blit_bg(struct ubuf *dest, struct ubuf *bg, struct ubuf *src, int dest_hoffset, int dest_voffset, int bg_hoffset, int bg_voffset, int src_hoffset, int src_voffset, int extract_hsize, int extract_vsize, - const int alpha, const int threshold) -{ - const uint8_t *alpha_plane = NULL; - size_t alpha_stride = 0; - int ret; - - static const char *alpha_plane_names[] = { "a8", "a10l" }; - const char *apn; - for (size_t i = 0; i < UBASE_ARRAY_SIZE(alpha_plane_names); i++) { - apn = alpha_plane_names[i]; - - /* Check for the existence of the given alpha plane. */ - ret = ubuf_pic_plane_read(src, apn, 0, 0, -1, -1, &alpha_plane); - /* Continue to the next if it isn't found. */ - if (!ubase_check(ret)) - continue; - - /* Get the stride. */ - ret = ubuf_pic_plane_size(src, apn, &alpha_stride, NULL, NULL, NULL); - /* Jump out of the loop if there is an error. */ - if (!ubase_check(ret)) - goto end; - - /* Perform blit. */ - if (i == 0) - ret = ubuf_pic_blit_alpha_bg(dest, bg, src, - dest_hoffset, dest_voffset, - bg_hoffset, bg_voffset, - src_hoffset, src_voffset, - extract_hsize, extract_vsize, - alpha_plane, alpha_stride, alpha, threshold); - else - ret = ubuf_pic_blit_alpha10_bg(dest, bg, src, - dest_hoffset, dest_voffset, - bg_hoffset, bg_voffset, - src_hoffset, src_voffset, - extract_hsize, extract_vsize, - alpha_plane, alpha_stride, alpha, threshold); - - /* Jump out of the loop when it is done. */ - goto end; - } - - /* Fallback to no alpha plane. */ - ret = ubuf_pic_blit_alpha_bg(dest, bg, src, dest_hoffset, dest_voffset, - bg_hoffset, bg_voffset, src_hoffset, src_voffset, - extract_hsize, extract_vsize, - NULL, 0, alpha, threshold); - -end: - if (alpha_plane) - ubuf_pic_plane_unmap(src, apn, 0, 0, -1, -1); - - return ret; -} + const int alpha, const int threshold); /** @This blits a picture ubuf to another ubuf in place. * diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index 5767a998b..b56fa63b5 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -21,6 +21,510 @@ #include #endif +/** @This blits a picture ubuf to another ubuf, optionally reading the + * underlying pixels from a third (background) ubuf. + * + * When bg is NULL, the blending reads the underlying pixels from dest and + * pixels not covered by the subpicture are left untouched (in-place blit). + * When bg is not NULL, the underlying pixels are read from bg and every pixel + * of the destination rectangle is written, so dest may be freshly allocated; + * this merges the copy of the background and the blit of the subpicture in a + * single pass. + * + * @param dest destination ubuf + * @param bg background ubuf, or NULL to blend in place into dest + * @param src source ubuf + * @param dest_hoffset number of pixels to seek at the beginning of each line of + * dest + * @param dest_voffset number of lines to seek at the beginning of dest + * @param bg_hoffset number of pixels to skip at the beginning of each line of + * bg (ignored if bg is NULL) + * @param bg_voffset number of lines to skip at the beginning of bg (ignored if + * bg is NULL) + * @param src_hoffset number of pixels to skip at the beginning of each line of + * src + * @param src_voffset number of lines to skip at the beginning of src + * @param extract_hsize horizontal size to copy + * @param extract_vsize vertical size to copy + * @param alpha_plane pointer to alpha plane buffer, if any + * @param alpha_stride horizontal stride of the alpha plane buffer + * @param alpha alpha multiplier + * @param threshold alpha blending method + * 0 means ignore alpha + * 255 means blends src and dest together using alpha levels (slow) + * Any value in between means using the src pixels if and only if + * their alpha value is more than this value + * @return an error code + */ +int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, + int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const uint8_t *alpha_plane, int alpha_stride, + const int alpha, const int threshold) +{ + if (alpha_plane == NULL && alpha < threshold && threshold != 0xff) { + if (bg == NULL) + return UBASE_ERR_NONE; /* nothing to do */ + /* the subpicture is invisible: plain copy of the background */ + return ubuf_pic_blit_alpha_bg(dest, NULL, bg, + dest_hoffset, dest_voffset, 0, 0, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + NULL, 0, 0xff, 0); + } + + uint8_t src_macropixel; + UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) + uint8_t dest_macropixel; + UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) + if (unlikely(dest_macropixel != src_macropixel)) + return UBASE_ERR_INVALID; + if (bg != NULL) { + uint8_t bg_macropixel; + UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) + if (unlikely(bg_macropixel != dest_macropixel)) + return UBASE_ERR_INVALID; + } + +#define MAX_PLANES 3 + + const char *chroma; + ubuf_pic_foreach_plane(dest, chroma) { + const char *in_chroma[MAX_PLANES] = { chroma }; + unsigned in_planes = 1; + int err; + + size_t dest_stride; + uint8_t dest_hsub, dest_vsub, dest_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(dest, chroma, + &dest_stride, &dest_hsub, &dest_vsub, + &dest_macropixel_size)) + + size_t src_stride; + uint8_t src_hsub, src_vsub, src_macropixel_size; + err = ubuf_pic_plane_size(src, in_chroma[0], &src_stride, &src_hsub, + &src_vsub, &src_macropixel_size); + if (!ubase_check(err)) { + if (!strcmp(chroma, "u8v8")) { + in_chroma[0] = "u8"; + in_chroma[1] = "v8"; + in_planes = 2; + } else + return err; + + for (unsigned i = 0; i < in_planes; i++) { + UBASE_RETURN(ubuf_pic_plane_size(src, in_chroma[i], &src_stride, + &src_hsub, &src_vsub, + &src_macropixel_size)); + if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || + src_macropixel_size != + dest_macropixel_size / in_planes)) + return UBASE_ERR_INVALID; + } + } else if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || + src_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + + size_t bg_stride = dest_stride; + if (bg != NULL) { + uint8_t bg_hsub, bg_vsub, bg_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, + &bg_hsub, &bg_vsub, &bg_macropixel_size)) + if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || + bg_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + } + + uint8_t *dest_buffer; + const uint8_t *bg_buffer; + const uint8_t *in[MAX_PLANES]; + UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize, &dest_buffer)) + if (bg != NULL) { + err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + &bg_buffer); + if (unlikely(!ubase_check(err))) { + ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + } else + bg_buffer = dest_buffer; + for (unsigned i = 0; i < in_planes; i++) { + err = + ubuf_pic_plane_read(src, in_chroma[i], src_hoffset, src_voffset, + extract_hsize, extract_vsize, &in[i]); + if (unlikely(!ubase_check(err))) { + for (; i; i--) + ubuf_pic_plane_unmap(src, in_chroma[i - 1], src_hoffset, + src_voffset, extract_hsize, + extract_vsize); + if (bg != NULL) + ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize); + ubuf_pic_plane_unmap(dest, chroma, dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + } + + int plane_hsize = extract_hsize / src_hsub / src_macropixel * + src_macropixel_size; + int plane_vsize = extract_vsize / src_vsub; + + for (int i = 0; i < plane_vsize; i++) { + if ((!alpha_plane && alpha == 0xff) || threshold == 0) { + if (in_planes == 1) + memcpy(dest_buffer, in[0], plane_hsize); + else { + for (int j = 0; j < plane_hsize; j++) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = in[p][j]; + } + } + } else if (!alpha_plane) { + for (int j = 0; j < plane_hsize; j++) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + (bg_buffer[j * in_planes + p] * (0xff - alpha) + + in[p][j] * alpha) / + 0xff; + } + } else if (threshold != 0xff) { + /* This is an on/off blending + * if alpha is over the threshold, we use the subpicture pixel, + * otherwise the background pixel (left untouched when + * blending in place). + */ + if (alpha == 0xff) { + for (int j = 0; j < plane_hsize; j++) { + const uint8_t a = alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub]; + if (a > threshold) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = in[p][j]; + } else if (bg != NULL) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + bg_buffer[j * in_planes + p]; + } + } + } else { + for (int j = 0; j < plane_hsize; j++) { + const uint8_t a = (uint16_t)alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub] * (uint16_t)alpha / 0xff; + if (a > threshold) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = in[p][j]; + } else if (bg != NULL) { + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + bg_buffer[j * in_planes + p]; + } + } + } + } else { + /* smooth and slow blending */ + if (alpha == 0xff) { + for (int j = 0; j < plane_hsize; j++) { + const uint8_t a = alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub]; + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + (bg_buffer[j * in_planes + p] * (0xff - a) + + in[p][j] * a) / + 0xff; + } + } else { + for (int j = 0; j < plane_hsize; j++) { + const uint8_t a = (uint16_t)alpha_plane[alpha_stride * (i * src_vsub) + j * src_hsub] * (uint16_t)alpha / 0xff; + for (int p = 0; p < in_planes; p++) + dest_buffer[j * in_planes + p] = + (bg_buffer[j * in_planes + p] * (0xff - a) + + in[p][j] * a) / + 0xff; + } + } + } + dest_buffer += dest_stride; + bg_buffer += bg_stride; + for (int p = 0; p < in_planes; p++) + in[p] += src_stride; + } + + err = ubuf_pic_plane_unmap(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + if (bg != NULL) + UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, + bg_voffset, extract_hsize, + extract_vsize)) + for (int p = 0; p < in_planes; p++) + UBASE_RETURN(ubuf_pic_plane_unmap(src, in_chroma[p], src_hoffset, + src_voffset, extract_hsize, + extract_vsize)) + UBASE_RETURN(err) + } + +#undef MAX_PLANES + + return UBASE_ERR_NONE; +} + +/** @see ubuf_pic_blit_alpha_bg */ +int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, + int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const uint8_t *alpha_plane, int alpha_stride, + const int alpha, const int threshold) +{ + if (alpha_plane == NULL && alpha < threshold && threshold != 0x3ff) { + if (bg == NULL) + return UBASE_ERR_NONE; /* nothing to do */ + /* the subpicture is invisible: plain copy of the background */ + return ubuf_pic_blit_alpha10_bg(dest, NULL, bg, + dest_hoffset, dest_voffset, 0, 0, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + NULL, 0, 0x3ff, 0); + } + + uint8_t src_macropixel; + UBASE_RETURN(ubuf_pic_size(src, NULL, NULL, &src_macropixel)) + uint8_t dest_macropixel; + UBASE_RETURN(ubuf_pic_size(dest, NULL, NULL, &dest_macropixel)) + if (unlikely(dest_macropixel != src_macropixel)) + return UBASE_ERR_INVALID; + if (bg != NULL) { + uint8_t bg_macropixel; + UBASE_RETURN(ubuf_pic_size(bg, NULL, NULL, &bg_macropixel)) + if (unlikely(bg_macropixel != dest_macropixel)) + return UBASE_ERR_INVALID; + } + + const char *chroma; + ubuf_pic_foreach_plane(dest, chroma) { + size_t src_stride; + uint8_t src_hsub, src_vsub, src_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(src, chroma, &src_stride, + &src_hsub, &src_vsub, &src_macropixel_size)) + + size_t dest_stride; + uint8_t dest_hsub, dest_vsub, dest_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(dest, chroma, + &dest_stride, &dest_hsub, &dest_vsub, + &dest_macropixel_size)) + + if (unlikely(src_hsub != dest_hsub || src_vsub != dest_vsub || + src_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + + size_t bg_stride = dest_stride; + if (bg != NULL) { + uint8_t bg_hsub, bg_vsub, bg_macropixel_size; + UBASE_RETURN(ubuf_pic_plane_size(bg, chroma, &bg_stride, + &bg_hsub, &bg_vsub, &bg_macropixel_size)) + if (unlikely(bg_hsub != dest_hsub || bg_vsub != dest_vsub || + bg_macropixel_size != dest_macropixel_size)) + return UBASE_ERR_INVALID; + } + + uint8_t *dest_buffer; + const uint8_t *bg_buffer; + const uint8_t *src_buffer; + UBASE_RETURN(ubuf_pic_plane_write(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize, &dest_buffer)) + int err; + if (bg != NULL) { + err = ubuf_pic_plane_read(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize, + &bg_buffer); + if (unlikely(!ubase_check(err))) { + ubuf_pic_plane_unmap(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + } else + bg_buffer = dest_buffer; + err = ubuf_pic_plane_read(src, chroma, src_hoffset, src_voffset, + extract_hsize, extract_vsize, + &src_buffer); + if (unlikely(!ubase_check(err))) { + if (bg != NULL) + ubuf_pic_plane_unmap(bg, chroma, bg_hoffset, bg_voffset, + extract_hsize, extract_vsize); + ubuf_pic_plane_unmap(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + return err; + } + + int plane_hsize = extract_hsize / src_hsub / src_macropixel * + src_macropixel_size; + int plane_vsize = extract_vsize / src_vsub; + + for (int i = 0; i < plane_vsize; i++) { + uint16_t *real_dst = (uint16_t *)dest_buffer; + const uint16_t *real_bg = (const uint16_t *)bg_buffer; + const uint16_t *real_src = (const uint16_t *)src_buffer; + const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); + + if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { + memcpy(dest_buffer, src_buffer, plane_hsize); + } else if (!alpha_plane) { + for (int j = 0; j < plane_hsize/2; j++) { + real_dst[j] = (real_bg[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; + } + } else if (threshold != 0x3ff) { + /* This is an on/off blending + * if alpha is over the threshold, we use the subpicture pixel, + * otherwise the background pixel (left untouched when + * blending in place). + */ + if (alpha == 0x3ff) { + for (int j = 0; j < plane_hsize/2; j++) { + const uint16_t a = real_alpha[j * src_hsub]; + if (a > threshold) real_dst[j] = real_src[j]; + else if (bg != NULL) real_dst[j] = real_bg[j]; + } + } else { + for (int j = 0; j < plane_hsize/2; j++) { + const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; + if (a > threshold) real_dst[j] = real_src[j]; + else if (bg != NULL) real_dst[j] = real_bg[j]; + } + } + } else { + /* smooth and slow blending */ + if (alpha == 0x3ff) { + for (int j = 0; j < plane_hsize/2; j++) { + const uint16_t a = real_alpha[j * src_hsub]; + real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; + } + } else { + for (int j = 0; j < plane_hsize/2; j++) { + const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; + real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; + } + } + } + dest_buffer += dest_stride; + bg_buffer += bg_stride; + src_buffer += src_stride; + } + + err = ubuf_pic_plane_unmap(dest, chroma, + dest_hoffset, dest_voffset, + extract_hsize, extract_vsize); + if (bg != NULL) + UBASE_RETURN(ubuf_pic_plane_unmap(bg, chroma, + bg_hoffset, bg_voffset, + extract_hsize, extract_vsize)) + UBASE_RETURN(ubuf_pic_plane_unmap(src, chroma, + src_hoffset, src_voffset, + extract_hsize, extract_vsize)) + UBASE_RETURN(err) + } + return UBASE_ERR_NONE; +} + +/** @This blits a picture ubuf to another ubuf, optionally reading the + * underlying pixels from a third (background) ubuf. + * + * When bg is NULL, the blending reads the underlying pixels from dest + * (in-place blit). When bg is not NULL, the underlying pixels are read from + * bg and every pixel of the destination rectangle is written, so dest may be + * freshly allocated; this merges the copy of the background and the blit of + * the subpicture in a single pass. + * + * @param dest destination ubuf + * @param bg background ubuf, or NULL to blend in place into dest + * @param src source ubuf + * @param dest_hoffset number of pixels to seek at the beginning of each line of + * dest + * @param dest_voffset number of lines to seek at the beginning of dest + * @param bg_hoffset number of pixels to skip at the beginning of each line of + * bg (ignored if bg is NULL) + * @param bg_voffset number of lines to skip at the beginning of bg (ignored if + * bg is NULL) + * @param src_hoffset number of pixels to skip at the beginning of each line of + * src + * @param src_voffset number of lines to skip at the beginning of src + * @param extract_hsize horizontal size to copy + * @param extract_vsize vertical size to copy + * @param alpha alpha multiplier + * @param threshold threshold parameter for alpha + * @return an error code + */ +int ubuf_pic_blit_bg(struct ubuf *dest, struct ubuf *bg, + struct ubuf *src, + int dest_hoffset, int dest_voffset, + int bg_hoffset, int bg_voffset, + int src_hoffset, int src_voffset, + int extract_hsize, int extract_vsize, + const int alpha, const int threshold) +{ + const uint8_t *alpha_plane = NULL; + size_t alpha_stride = 0; + int ret; + + static const char *alpha_plane_names[] = { "a8", "a10l" }; + const char *apn; + for (size_t i = 0; i < UBASE_ARRAY_SIZE(alpha_plane_names); i++) { + apn = alpha_plane_names[i]; + + /* Check for the existence of the given alpha plane. */ + ret = ubuf_pic_plane_read(src, apn, 0, 0, -1, -1, &alpha_plane); + /* Continue to the next if it isn't found. */ + if (!ubase_check(ret)) + continue; + + /* Get the stride. */ + ret = ubuf_pic_plane_size(src, apn, &alpha_stride, NULL, NULL, NULL); + /* Jump out of the loop if there is an error. */ + if (!ubase_check(ret)) + goto end; + + /* Perform blit. */ + if (i == 0) + ret = ubuf_pic_blit_alpha_bg(dest, bg, src, + dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, + src_hoffset, src_voffset, + extract_hsize, extract_vsize, + alpha_plane, alpha_stride, alpha, threshold); + else + ret = ubuf_pic_blit_alpha10_bg(dest, bg, src, + dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, + src_hoffset, src_voffset, + extract_hsize, extract_vsize, + alpha_plane, alpha_stride, alpha, threshold); + + /* Jump out of the loop when it is done. */ + goto end; + } + + /* Fallback to no alpha plane. */ + ret = ubuf_pic_blit_alpha_bg(dest, bg, src, dest_hoffset, dest_voffset, + bg_hoffset, bg_voffset, src_hoffset, src_voffset, + extract_hsize, extract_vsize, + NULL, 0, alpha, threshold); + +end: + if (alpha_plane) + ubuf_pic_plane_unmap(src, apn, 0, 0, -1, -1); + + return ret; +} + /** @This clears (part of) the specified plane, depending on plane type * and size (set U/V chroma to 0x80 instead of 0 for instance) * From 5a8b7060906445d87a67d2556dc2e03b59d86048 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 5 Aug 2026 22:17:15 +0100 Subject: [PATCH 05/30] ubuf_pic: remove divides from the loops in ubuf_pic_blit_alpha10 --- lib/upipe/ubuf_pic.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index b56fa63b5..324d45bc0 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -367,7 +367,7 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, } int plane_hsize = extract_hsize / src_hsub / src_macropixel * - src_macropixel_size; + src_macropixel_size / 2; int plane_vsize = extract_vsize / src_vsub; for (int i = 0; i < plane_vsize; i++) { @@ -377,9 +377,9 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { - memcpy(dest_buffer, src_buffer, plane_hsize); + memcpy(dest_buffer, src_buffer, plane_hsize*2); } else if (!alpha_plane) { - for (int j = 0; j < plane_hsize/2; j++) { + for (int j = 0; j < plane_hsize; j++) { real_dst[j] = (real_bg[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; } } else if (threshold != 0x3ff) { @@ -389,13 +389,13 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, * blending in place). */ if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize/2; j++) { + for (int j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub]; if (a > threshold) real_dst[j] = real_src[j]; else if (bg != NULL) real_dst[j] = real_bg[j]; } } else { - for (int j = 0; j < plane_hsize/2; j++) { + for (int j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; if (a > threshold) real_dst[j] = real_src[j]; else if (bg != NULL) real_dst[j] = real_bg[j]; @@ -404,12 +404,12 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, } else { /* smooth and slow blending */ if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize/2; j++) { + for (int j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub]; real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } } else { - for (int j = 0; j < plane_hsize/2; j++) { + for (int j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } From e791261e4d9a96ebc87e43f2cfd9c7ff1c5bf359 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 5 Aug 2026 22:17:40 +0100 Subject: [PATCH 06/30] ubuf_pic: prefer size_t array indicies in ubuf_pic_blit_alpha10 --- lib/upipe/ubuf_pic.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index 324d45bc0..5c631b148 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -366,11 +366,11 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, return err; } - int plane_hsize = extract_hsize / src_hsub / src_macropixel * + size_t plane_hsize = extract_hsize / src_hsub / src_macropixel * src_macropixel_size / 2; - int plane_vsize = extract_vsize / src_vsub; + size_t plane_vsize = extract_vsize / src_vsub; - for (int i = 0; i < plane_vsize; i++) { + for (size_t i = 0; i < plane_vsize; i++) { uint16_t *real_dst = (uint16_t *)dest_buffer; const uint16_t *real_bg = (const uint16_t *)bg_buffer; const uint16_t *real_src = (const uint16_t *)src_buffer; @@ -379,7 +379,7 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { memcpy(dest_buffer, src_buffer, plane_hsize*2); } else if (!alpha_plane) { - for (int j = 0; j < plane_hsize; j++) { + for (size_t j = 0; j < plane_hsize; j++) { real_dst[j] = (real_bg[j] * (0x3ff - alpha) + real_src[j] * alpha) / 0x3ff; } } else if (threshold != 0x3ff) { @@ -389,13 +389,13 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, * blending in place). */ if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize; j++) { + for (size_t j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub]; if (a > threshold) real_dst[j] = real_src[j]; else if (bg != NULL) real_dst[j] = real_bg[j]; } } else { - for (int j = 0; j < plane_hsize; j++) { + for (size_t j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; if (a > threshold) real_dst[j] = real_src[j]; else if (bg != NULL) real_dst[j] = real_bg[j]; @@ -404,12 +404,12 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, } else { /* smooth and slow blending */ if (alpha == 0x3ff) { - for (int j = 0; j < plane_hsize; j++) { + for (size_t j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub]; real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } } else { - for (int j = 0; j < plane_hsize; j++) { + for (size_t j = 0; j < plane_hsize; j++) { const uint16_t a = real_alpha[j * src_hsub] * alpha / 0x3ff; real_dst[j] = (real_bg[j] * (0x3ff - a) + real_src[j] * a) / 0x3ff; } From 4409202d64bc5f0c8742f49dba598aac6f4241b9 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Wed, 5 Aug 2026 22:23:07 +0100 Subject: [PATCH 07/30] ubuf_pic: add SIMD optimized functions for threshold copy Squashed from the SIMD half of the original series: the 10-bit and 8-bit intrinsic kernels, their replacement by external asm, and the two commits that removed the intrinsic versions again. Only the asm survived that series, so only the asm is carried over here. The kernels compute dst = alpha > threshold ? src : bg. The background comes from its own pointer so that a freshly allocated destination can be composed in a single pass; callers blending in place pass bg == dst, which makes writing the background samples a no-op. For the same reason the scalar tails now store every sample instead of skipping the ones below the threshold. blit8_threshold_hsub2_avx2 was missing the XOR of the threshold with 0x80 that the other byte kernels do: the packed signed compare then tested alpha - 128 against the raw threshold, so with a threshold below 128 the source pixel was only selected for an alpha above threshold + 128. The unit test only exercised alpha 0 and 255, which land on the right side of the comparison either way. --- lib/upipe/Build.mk | 5 + lib/upipe/ubuf_pic.c | 20 +- lib/upipe/ubuf_pic_blit_x86.h | 129 ++++++++++++ lib/upipe/x86/ubuf_pic_blit.asm | 343 ++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 25 +++ 5 files changed, 521 insertions(+), 1 deletion(-) create mode 100644 lib/upipe/ubuf_pic_blit_x86.h create mode 100644 lib/upipe/x86/ubuf_pic_blit.asm create mode 100644 lib/upipe/x86/ubuf_pic_blit.h diff --git a/lib/upipe/Build.mk b/lib/upipe/Build.mk index 109606200..885e5911c 100644 --- a/lib/upipe/Build.mk +++ b/lib/upipe/Build.mk @@ -129,6 +129,7 @@ libupipe-src = \ ubuf_mem.c \ ubuf_mem_common.c \ ubuf_pic.c \ + ubuf_pic_blit_x86.h \ ubuf_pic_common.c \ ubuf_pic_mem.c \ ubuf_sound_common.c \ @@ -163,6 +164,10 @@ libupipe-src = \ utrace.c \ uuri.c +libupipe-src += \ + $(if $(have_x86asm),x86/ubuf_pic_blit.asm) \ + $(if $(have_x86asm),x86/ubuf_pic_blit.h) + libupipe-ldlibs = -lm include/upipe/config.h: config.h diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index 5c631b148..d36a6670d 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -18,7 +18,7 @@ #include #if defined(__GNUC__) && defined(__x86_64__) -#include +#include "ubuf_pic_blit_x86.h" #endif /** @This blits a picture ubuf to another ubuf, optionally reading the @@ -178,6 +178,17 @@ int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, int plane_vsize = extract_vsize / src_vsub; for (int i = 0; i < plane_vsize; i++) { + +#if defined(__GNUC__) && defined(__x86_64__) + if (in_planes == 1 && blit8_handle_cases(dest_buffer, bg_buffer, + in[0], + alpha_plane ? alpha_plane + alpha_stride * (i * src_vsub) + : NULL, + alpha, threshold, plane_hsize, src_hsub)) + /* Nothing to do */; + else +#endif + if ((!alpha_plane && alpha == 0xff) || threshold == 0) { if (in_planes == 1) memcpy(dest_buffer, in[0], plane_hsize); @@ -376,6 +387,13 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, const uint16_t *real_src = (const uint16_t *)src_buffer; const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); +#if defined(__GNUC__) && defined(__x86_64__) + if (blit10_handle_cases(real_dst, real_bg, real_src, real_alpha, + alpha, threshold, plane_hsize, src_hsub)) + /* Nothing to do */; + else +#endif + if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { memcpy(dest_buffer, src_buffer, plane_hsize*2); } else if (!alpha_plane) { diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h new file mode 100644 index 000000000..d8a3c2297 --- /dev/null +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -0,0 +1,129 @@ +#include +#include +#include + +#include "x86/ubuf_pic_blit.h" + +/* + * @This handle particular blit cases. Exists to avoid more nested ifs. + * + * @param dst destination pointer + * @param bg background pointer, equal to dst when blending in place + * @param src source pointer + * @param alpha alpha plane pointer + * @param alpha_multiplier alpha multiplier + * @param threshold alpha blending method + * @param hsize width in samples + * @param hsub source and destination plane horizontal subsampling + * + * TODO: whole planes + */ + +static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, + const uint8_t *src, const uint8_t *alpha, int alpha_multiplier, + int threshold, size_t hsize, uint8_t hsub) +{ + /* Duplicating a cases not handled here to mirror the logic in caller. */ + if ((!alpha && alpha_multiplier == 0xff) || threshold == 0) { + /* Caller does a memcpy */ + return false; + } + else if (!alpha) { + /* Caller does a blend */ + return false; + } + else if (hsize < 32) { + /* Let caller handle small blits */ + return false; + } + + else if (threshold != 0xff && alpha_multiplier == 0xff) { + if (hsub == 1) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit8_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit8_threshold_hsub1_ssse3(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + + if (hsub == 2) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit8_threshold_hsub2_ssse3(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + return false; + } + return false; +} + +/* + * @This handle particular blit cases. Exists to avoid more nested ifs. + * + * @param dst destination pointer + * @param bg background pointer, equal to dst when blending in place + * @param src source pointer + * @param alpha alpha plane pointer + * @param alpha_multiplier alpha multiplier + * @param threshold alpha blending method + * @param hsize width in samples + * @param hsub source and destination plane horizontal subsampling + * + * TODO: whole planes + */ + +static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, + const uint16_t *src, const uint16_t *alpha, int alpha_multiplier, + int threshold, size_t hsize, uint8_t hsub) +{ + /* Duplicating a cases not handled here to mirror the logic in caller. */ + if ((!alpha && alpha_multiplier == 0x3ff) || threshold == 0) { + /* Caller does a memcpy */ + return false; + } + else if (!alpha) { + /* Caller does a blend */ + return false; + } + else if (hsize < 16) { + /* Let caller handle small blits */ + return false; + } + + else if (threshold != 0x3ff && alpha_multiplier == 0x3ff) { + if (hsub == 1) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit10_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit10_threshold_hsub1_ssse3(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + + if (hsub == 2) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit10_threshold_hsub2_ssse3(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + return false; + } + return false; +} diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm new file mode 100644 index 000000000..c804d75e5 --- /dev/null +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -0,0 +1,343 @@ +%use smartalign +ALIGNMODE p6 + +%include "x86inc.asm" + +SECTION_RODATA 16 + +blit10_threshold_splat: times 8 db 0, 1 +blit10_alpha_shuffle: db 0,1, 4,5, 8,9, 12,13 + times 8 db -1 +blit8_alpha_shuffle: db 0, 2, 4, 6, 8, 10, 12, 14 + times 8 db -1 + +SECTION .text + +; Every kernel computes dst = alpha > threshold ? src : bg. The background is a +; separate pointer so that dst can be a freshly allocated picture; callers +; blending in place pass bg == dst, which makes the write of the background +; samples a no-op. + +%macro SCALAR_TAIL_B 3-4 1 ; loop counter, temp1, temp2, optional subsampling + .loop_scalar: + + movzx %2d, byte [alpha_q + %4 * %1q] ; load alpha sample + movzx %3d, byte [bg_q + %1q] ; load background sample + cmp %2d, threshold_d ; compare with threshold + jle .no_copy ; jump if less than or equal + movzx %3d, byte [src_q + %1q] ; load source sample instead + .no_copy: + mov [dst_q + %1q], %3b ; write to destination + add %1q, 1 ; increment + cmp hsize_q, %1q ; compare + jne .loop_scalar ; jump if not equal +%endmacro + +%macro SCALAR_TAIL_W 3-4 1 ; loop counter, temp1, temp2, optional subsampling + .loop_scalar: + movzx %2d, word [alpha_q + %4 * 2*%1q] + movzx %3d, word [bg_q + 2*%1q] + cmp %2d, threshold_d + jle .no_copy + movzx %3d, word [src_q + 2*%1q] + .no_copy: + mov [dst_q + 2*%1q], %3w + add %1q, 1 + cmp hsize_q, %1q + jne .loop_scalar +%endmacro + +INIT_XMM ssse3 + +%define xor_reg m0 +%define threshold m1 +%define bg m2 +%define src m3 +%define alp m4 + +cglobal blit8_threshold_hsub1, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ + mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte + pxor m5, m5 + movd threshold, threshold_d + movd xor_reg, r6d + pshufb threshold, m5 ; Splat threshold to every byte + pshufd xor_reg, xor_reg, 0 ; Splat 0x80 to every byte + mov r6d, mmsize + pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + ALIGN 16 + .loop_simd: + movu bg, [bg_q + r6q - mmsize] + movu src, [src_q + r6q - mmsize] + movu alp, [alpha_q + r6q - mmsize] + mov r7q, r6q + pxor alp, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + pcmpgtb alp, threshold + mova m5, alp + pand src, alp + pandn m5, bg + por m5, src + movu [dst_q + r6q - mmsize], m5 + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8 + .ret: +RET + +%define xor_reg m0 +%define threshold m1 +%define shuffle m2 +%define bg m3 +%define src m4 +%define alp m5 + +cglobal blit8_threshold_hsub2, 6, 9, 7, dst_, bg_, src_, alpha_, hsize_, threshold_ + mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte + pxor m5, m5 + movd threshold, threshold_d + movd xor_reg, r6d + movq shuffle, [blit8_alpha_shuffle] + pshufb threshold, m5 ; Splat threshold to every byte + pshufd xor_reg, xor_reg, 0 ; Splat 0x80 to every byte + mov r6d, mmsize + pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + ALIGN 16 + .loop_simd: + movu bg, [bg_q + r6q - mmsize] + movu src, [src_q + r6q - mmsize] + movu m5, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + ; Need to compress every second byte + pshufb m5, shuffle + pshufb m6, shuffle + punpcklqdq alp, m5, m6 + mov r7q, r6q + pxor alp, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + pcmpgtb alp, threshold + mova m6, alp + pand src, alp + pandn m6, bg + por m6, src + movu [dst_q + r6q - mmsize], m6 + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8, 2 + .ret: +RET + +%define threshold m0 +%define bg m1 +%define src m2 +%define alp m3 + +cglobal blit10_threshold_hsub1, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ + movd threshold, threshold_d + mov r6d, mmsize/2 + pshufb threshold, [blit10_threshold_splat] ; Splat threshold to every word + ALIGN 16 + .loop_simd: + movu bg, [bg_q + 2*r6q - mmsize] + movu src, [src_q + 2*r6q - mmsize] + movu alp, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + pcmpgtw alp, threshold + mova m5, alp + pand src, alp + pandn m5, bg + por m5, src + movu [dst_q + 2*r6q - mmsize], m5 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8 + .ret: +RET + +%define shuffle m0 +%define threshold m1 +%define bg m2 +%define src m3 +%define alp m4 + +cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ + movd threshold, threshold_d + mov r6d, mmsize/2 + pshufb threshold, [blit10_threshold_splat] ; Splat threshold to every word + movq shuffle, [blit10_alpha_shuffle] ; Load shuffle constants to compress words + ALIGN 16 + .loop_simd: + movu bg, [bg_q + 2*r6q - mmsize] + movu src, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 4*r6q - 2*mmsize] + movu m5, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + ; Need to compress every second word + pshufb m4, shuffle + pshufb m5, shuffle + punpcklqdq alp, m4, m5 + pcmpgtw alp, threshold + mova m5, alp + pand src, alp + pandn m5, bg + por m5, src + movu [dst_q + 2*r6q - mmsize], m5 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8, 2 + .ret: +RET + +INIT_YMM avx2 + +%define xor_reg m0 +%define threshold m1 +%define bg m2 +%define src m3 +%define alp m4 + +cglobal blit8_threshold_hsub1, 6, 9, 5, dst_, bg_, src_, alpha_, hsize_, threshold_ + mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte + movd xmm %+ threshold, threshold_d + movd xmm %+ xor_reg, r6d + vpbroadcastb threshold, xmm %+ threshold ; Broadcast threshold to every byte + vpbroadcastd xor_reg, xmm %+ xor_reg ; Broadcast 0x80 to every byte + mov r6d, mmsize + pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + ALIGN 16 + .loop_simd: + pxor alp, xor_reg, [alpha_q + r6q - mmsize] ; Packed compare bytes is for signed bytes so need to do a trick + mov r7q, r6q + pcmpgtb alp, threshold + pand src, alp, [src_q + r6q - mmsize] + pandn bg, alp, [bg_q + r6q - mmsize] + por bg, src + movu [dst_q + r6q - mmsize], bg + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8 + .ret: +RET + +%define xor_reg m0 +%define threshold m1 +%define shuffle m2 +%define bg m3 +%define src m4 +%define alp m5 + +cglobal blit8_threshold_hsub2, 6, 9, 7, dst_, bg_, src_, alpha_, hsize_, threshold_ + mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte + movd xmm %+ threshold, threshold_d + VBROADCASTI128 shuffle, [blit8_alpha_shuffle] + movd xmm %+ xor_reg, r6d + vpbroadcastb threshold, xmm %+ threshold ; Broadcast threshold to every byte + vpbroadcastd xor_reg, xmm %+ xor_reg ; Broadcast 0x80 to every byte + mov r6d, mmsize + pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + ALIGN 16 + .loop_simd: + movu m5, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + mov r7q, r6q + ; Need to compress every second byte + pshufb m5, shuffle + pshufb m6, shuffle + punpcklqdq alp, m5, m6 + vpermq alp, alp, q3120 ; Permute to get correct order of qwords + pxor alp, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick + pcmpgtb alp, threshold + pand src, alp, [src_q + r6q - mmsize] + pandn bg, alp, [bg_q + r6q - mmsize] + por bg, src + movu [dst_q + r6q - mmsize], bg + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8, 2 + .ret: +RET + +%define threshold m0 +%define bg m1 +%define src m2 +%define alp m3 + +cglobal blit10_threshold_hsub1, 6, 9, 4, dst_, bg_, src_, alpha_, hsize_, threshold_ + movd xmm %+ threshold, threshold_d + mov r6d, mmsize/2 + vpbroadcastw threshold, xmm %+ threshold ; Broadcast threshold to every word + ALIGN 16 + .loop_simd: + movu alp, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + pcmpgtw alp, threshold + pand src, alp, [src_q + 2*r6q - mmsize] + pandn bg, alp, [bg_q + 2*r6q - mmsize] + por bg, src + movu [dst_q + 2*r6q - mmsize], bg + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8 + .ret: +RET + +%define shuffle m0 +%define threshold m1 +%define bg m2 +%define src m3 +%define alp m4 + +cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ + movd xmm %+ threshold, threshold_d + VBROADCASTI128 shuffle, [blit10_alpha_shuffle] ; Load shuffle constants to compress words + mov r6d, mmsize/2 + vpbroadcastw threshold, xmm %+ threshold ; Broadcast threshold to every word + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 4*r6q - 2*mmsize] + movu m5, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + ; Need to compress every second word + pshufb m4, shuffle + pshufb m5, shuffle + punpcklqdq alp, m4, m5 + vpermq alp, alp, q3120 ; Permute to get correct order of qwords + pcmpgtw alp, threshold + pand src, alp, [src_q + 2*r6q - mmsize] + pandn bg, alp, [bg_q + 2*r6q - mmsize] + por bg, src + movu [dst_q + 2*r6q - mmsize], bg + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8, 2 + .ret: +RET diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h new file mode 100644 index 000000000..bd6b627e7 --- /dev/null +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -0,0 +1,25 @@ +/* dst = alpha > threshold ? src : bg. Pass bg == dst to blend in place. */ +void upipe_blit8_threshold_hsub1_ssse3(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_threshold_hsub2_ssse3(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub1_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub2_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_threshold_hsub1_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_threshold_hsub2_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); From 3a2c1287515fa9e4cd948516df1cf1f551c1b60f Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 5 Aug 2026 22:27:41 +0100 Subject: [PATCH 08/30] ubuf_pic_blit_test: check whole planes, in place and against a background The existing checks sample a 3x3 grid of a 64x64 picture, which leaves most of a line unverified and never exercises the background form of the blit at all. Blit a whole single-plane picture instead and compare every sample against the scalar reference, once composed out of a separate background and once blended in place. The widths run either side of the point where the SIMD kernels take over and are mostly not whole vectors, so the vector loops and their scalar tails are both covered, for hsub 1 and 2 at 8 and 10 bits. The source, alpha and background planes carry a pattern in which neighbouring samples differ, which is what catches a kernel that loses the order of the samples in a vector or mis-compacts a subsampled alpha plane. --- tests/ubuf_pic_blit_test.c | 266 +++++++++++++++++++++++++++++++++++++ 1 file changed, 266 insertions(+) diff --git a/tests/ubuf_pic_blit_test.c b/tests/ubuf_pic_blit_test.c index 6ecaccd5a..0eef883c5 100644 --- a/tests/ubuf_pic_blit_test.c +++ b/tests/ubuf_pic_blit_test.c @@ -393,6 +393,221 @@ static unsigned long check_yuv420p10le_blit(struct ubuf *dst, struct ubuf *src, return checks; } +/** @internal @This fills a rectangular region of a plane with a pattern whose + * neighbouring samples all differ, so that a blit which mixes up the order of + * the samples in a vector, or the compaction of a subsampled alpha plane, + * cannot pass unnoticed. */ +static void fill_plane_pattern(struct ubuf *ubuf, const char *chroma, + int hsize, int vsize, unsigned seed) +{ + uint8_t macropixel; + ubase_assert(ubuf_pic_size(ubuf, NULL, NULL, ¯opixel)); + + size_t stride; + uint8_t hsub, vsub, macropixel_size; + ubase_assert(ubuf_pic_plane_size(ubuf, chroma, &stride, &hsub, &vsub, + ¯opixel_size)); + int cols = hsize * macropixel_size / hsub / macropixel; + int rows = vsize / vsub; + + uint8_t *buffer; + ubase_assert(ubuf_pic_plane_write(ubuf, chroma, 0, 0, hsize, vsize, + &buffer)); + for (int y = 0; y < rows; y++) { + for (int x = 0; x < cols; x++) + buffer[x] = (x * 37 + y * 101 + seed) & 0xff; + buffer += stride; + } + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, 0, 0, hsize, vsize)); +} + +/** @internal @This is fill_plane_pattern() for 16-bit samples, kept inside the + * 10-bit range. */ +static void fill_plane_pattern16(struct ubuf *ubuf, const char *chroma, + int hsize, int vsize, unsigned seed) +{ + uint8_t macropixel; + ubase_assert(ubuf_pic_size(ubuf, NULL, NULL, ¯opixel)); + + size_t stride; + uint8_t hsub, vsub, macropixel_size; + ubase_assert(ubuf_pic_plane_size(ubuf, chroma, &stride, &hsub, &vsub, + ¯opixel_size)); + int cols = hsize * macropixel_size / hsub / macropixel / 2; + int rows = vsize / vsub; + + uint8_t *buffer; + ubase_assert(ubuf_pic_plane_write(ubuf, chroma, 0, 0, hsize, vsize, + &buffer)); + for (int y = 0; y < rows; y++) { + uint16_t *words = (uint16_t *)buffer; + for (int x = 0; x < cols; x++) + words[x] = (x * 37 + y * 101 + seed) & 0x3ff; + buffer += stride; + } + ubase_assert(ubuf_pic_plane_unmap(ubuf, chroma, 0, 0, hsize, vsize)); +} + +/** @internal @This compares every sample of a blitted plane against + * expected_blit()/expected_blit10() applied to the background and source + * samples the blit was given, and returns the number of checks performed. */ +static unsigned long check_blit_plane(struct ubuf *dst, struct ubuf *bg, + struct ubuf *src, const char *chroma, + const char *alpha_chroma, bool ten_bit, + int hsize, int vsize, + int alpha, int threshold, + const char *what) +{ + size_t dst_stride, bg_stride, src_stride, a_stride; + uint8_t hsub, vsub, macropixel_size; + const uint8_t *dst_buffer, *bg_buffer, *src_buffer, *a_buffer; + unsigned long checks = 0; + + ubase_assert(ubuf_pic_plane_size(dst, chroma, &dst_stride, &hsub, &vsub, + ¯opixel_size)); + ubase_assert(ubuf_pic_plane_size(bg, chroma, &bg_stride, NULL, NULL, NULL)); + ubase_assert(ubuf_pic_plane_size(src, chroma, &src_stride, NULL, NULL, + NULL)); + ubase_assert(ubuf_pic_plane_size(src, alpha_chroma, &a_stride, NULL, NULL, + NULL)); + ubase_assert(ubuf_pic_plane_read(dst, chroma, 0, 0, hsize, vsize, + &dst_buffer)); + ubase_assert(ubuf_pic_plane_read(bg, chroma, 0, 0, hsize, vsize, + &bg_buffer)); + ubase_assert(ubuf_pic_plane_read(src, chroma, 0, 0, hsize, vsize, + &src_buffer)); + ubase_assert(ubuf_pic_plane_read(src, alpha_chroma, 0, 0, hsize, vsize, + &a_buffer)); + + for (int y = 0; y < vsize / vsub; y++) { + for (int x = 0; x < hsize / hsub; x++) { + if (ten_bit) { + uint16_t a = + ((const uint16_t *)(a_buffer + y * a_stride))[x * hsub]; + uint16_t b = + ((const uint16_t *)(bg_buffer + y * bg_stride))[x]; + uint16_t s = + ((const uint16_t *)(src_buffer + y * src_stride))[x]; + uint16_t got = + ((const uint16_t *)(dst_buffer + y * dst_stride))[x]; + uint16_t exp = expected_blit10(b, s, a, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on %s %s at (%d, %d) hsize %d " + "alpha %d threshold %d: got %u, expected %u\n", + chroma, what, x, y, hsize, alpha, threshold, + got, exp); + assert(got == exp); + } else { + uint8_t a = a_buffer[y * a_stride + x * hsub]; + uint8_t b = bg_buffer[y * bg_stride + x]; + uint8_t s = src_buffer[y * src_stride + x]; + uint8_t got = dst_buffer[y * dst_stride + x]; + uint8_t exp = expected_blit(b, s, a, alpha, threshold); + if (got != exp) + fprintf(stderr, "mismatch on %s %s at (%d, %d) hsize %d " + "alpha %d threshold %d: got %u, expected %u\n", + chroma, what, x, y, hsize, alpha, threshold, + got, exp); + assert(got == exp); + } + checks++; + } + } + + ubase_assert(ubuf_pic_plane_unmap(src, alpha_chroma, 0, 0, hsize, vsize)); + ubase_assert(ubuf_pic_plane_unmap(src, chroma, 0, 0, hsize, vsize)); + ubase_assert(ubuf_pic_plane_unmap(bg, chroma, 0, 0, hsize, vsize)); + ubase_assert(ubuf_pic_plane_unmap(dst, chroma, 0, 0, hsize, vsize)); + return checks; +} + +/** @internal @This blits a whole single-plane picture twice: once composing + * into a fresh destination out of a separate background, the way upipe_blit + * does when the input picture is not writable, and once blending in place. The + * two must agree with the same scalar reference. + * + * The plane geometry is what picks the code path: hsub selects between the + * hsub1 and hsub2 kernels, the sample size between the 8-bit and 10-bit ones, + * and the width decides how much of a line the vector loop covers and how much + * is left to the scalar tail. + * + * @return the number of samples checked + */ +static unsigned long check_blit_geometry(struct umem_mgr *umem_mgr, + const char *chroma, + const char *alpha_chroma, + uint8_t hsub, bool ten_bit, + int hsize, int vsize, + int alpha, int threshold) +{ + uint8_t macropixel_size = ten_bit ? 2 : 1; + unsigned long checks = 0; + + struct ubuf_mgr *pic_mgr = + ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(pic_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(pic_mgr, chroma, hsub, 1, + macropixel_size)); + + struct ubuf_mgr *src_mgr = + ubuf_pic_mem_mgr_alloc(UBUF_POOL_DEPTH, UBUF_POOL_DEPTH, umem_mgr, 1, + UBUF_PREPEND, UBUF_APPEND, + UBUF_PREPEND, UBUF_APPEND, + UBUF_ALIGN, UBUF_ALIGN_HOFFSET); + assert(src_mgr != NULL); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, chroma, hsub, 1, + macropixel_size)); + ubase_assert(ubuf_pic_mem_mgr_add_plane(src_mgr, alpha_chroma, 1, 1, + macropixel_size)); + + struct ubuf *dst = ubuf_pic_alloc(pic_mgr, hsize, vsize); + assert(dst != NULL); + struct ubuf *bg = ubuf_pic_alloc(pic_mgr, hsize, vsize); + assert(bg != NULL); + struct ubuf *src = ubuf_pic_alloc(src_mgr, hsize, vsize); + assert(src != NULL); + + if (ten_bit) { + fill_plane_pattern16(bg, chroma, hsize, vsize, 11); + fill_plane_pattern16(src, chroma, hsize, vsize, 173); + fill_plane_pattern16(src, alpha_chroma, hsize, vsize, 61); + /* A destination value that appears nowhere else, so that a sample the + * blit fails to write is reported instead of matching by luck. */ + ubase_assert(ubuf_pic_plane_set_color(dst, chroma, 0, 0, hsize, vsize, + (const uint8_t []){ 0x55, 0x01 }, 2)); + } else { + fill_plane_pattern(bg, chroma, hsize, vsize, 11); + fill_plane_pattern(src, chroma, hsize, vsize, 173); + fill_plane_pattern(src, alpha_chroma, hsize, vsize, 61); + ubase_assert(ubuf_pic_plane_set_color(dst, chroma, 0, 0, hsize, vsize, + (const uint8_t []){ 0x5a }, 1)); + } + + /* Compose out of the background into the untouched destination. */ + ubase_assert(ubuf_pic_blit_bg(dst, bg, src, 0, 0, 0, 0, 0, 0, + hsize, vsize, alpha, threshold)); + checks += check_blit_plane(dst, bg, src, chroma, alpha_chroma, ten_bit, + hsize, vsize, alpha, threshold, "background"); + + /* Then the same blit in place, over a copy of that background. */ + ubase_assert(ubuf_pic_blit(dst, bg, 0, 0, 0, 0, hsize, vsize, + ten_bit ? 0x3ff : 0xff, 0)); + ubase_assert(ubuf_pic_blit(dst, src, 0, 0, 0, 0, hsize, vsize, + alpha, threshold)); + checks += check_blit_plane(dst, bg, src, chroma, alpha_chroma, ten_bit, + hsize, vsize, alpha, threshold, "in place"); + + ubuf_free(src); + ubuf_free(bg); + ubuf_free(dst); + ubuf_mgr_release(src_mgr); + ubuf_mgr_release(pic_mgr); + return checks; +} + int main(int argc, char **argv) { struct umem_mgr *umem_mgr = umem_alloc_mgr_alloc(); @@ -1376,6 +1591,57 @@ int main(int argc, char **argv) ubuf_mgr_release(src_mgr); ubuf_mgr_release(dst_mgr); + /* Whole-plane blits over a single-plane picture, checked sample by sample + * both when composing out of a separate background and when blending in + * place. The widths straddle the point where the SIMD kernels take over + * (32 samples for 8-bit, 16 for 10-bit) and are mostly not multiples of a + * vector, so their scalar tails run as well. */ + { + static const struct { + const char *chroma, *alpha_chroma; + uint8_t hsub; + bool ten_bit; + int widths[9]; + } geometries[] = { + { "y8", "a8", 1, false, { 2, 30, 32, 34, 40, 62, 64, 70, 100 } }, + { "u8", "a8", 2, false, { 4, 60, 64, 68, 80, 124, 128, 140, 200 } }, + { "y10l", "a10l", 1, true, { 2, 14, 16, 18, 24, 30, 34, 50, 100 } }, + { "u10l", "a10l", 2, true, { 4, 28, 32, 36, 48, 60, 68, 100, 200 } }, + }; + /* threshold 0 is the copy and the saturated threshold is the smooth + * blending; everything else is the on/off blending the kernels + * implement. A non saturated alpha multiplier scales the alpha plane + * sample first, which they leave to the caller. */ + static const struct { int alpha8, threshold8, alpha10, threshold10; } + cases[] = { + { 0xff, 0, 0x3ff, 0 }, + { 0xff, 20, 0x3ff, 80 }, + { 0xff, 80, 0x3ff, 300 }, + { 0xff, 128, 0x3ff, 512 }, + { 0xff, 200, 0x3ff, 1000 }, + { 0xff, 254, 0x3ff, 1022 }, + { 0xff, 0xff, 0x3ff, 0x3ff }, + { 200, 80, 800, 300 }, + { 128, 0xff, 512, 0x3ff }, + }; + + checks = 0; + for (size_t g = 0; g < UBASE_ARRAY_SIZE(geometries); g++) + for (size_t w = 0; w < UBASE_ARRAY_SIZE(geometries[g].widths); w++) + for (size_t c = 0; c < UBASE_ARRAY_SIZE(cases); c++) { + alpha = geometries[g].ten_bit ? cases[c].alpha10 + : cases[c].alpha8; + threshold = geometries[g].ten_bit ? cases[c].threshold10 + : cases[c].threshold8; + checks += check_blit_geometry(umem_mgr, + geometries[g].chroma, geometries[g].alpha_chroma, + geometries[g].hsub, geometries[g].ten_bit, + geometries[g].widths[w], 4, alpha, threshold); + } + printf("%lu whole-plane background and in-place blit checks passed\n", + checks); + } + /* Benchmark: repeatedly blit a full 1920x1080 yuv422p picture through a * rectangular alpha mask and report the throughput. */ int calls = 0; From 77001f08244e523fed3b753f5b8c09d50865308a Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 12 Aug 2026 03:03:53 +0100 Subject: [PATCH 09/30] upipe_blit: keep the subpicture across a flow definition change The sub dropped its stored ubuf whenever a flow definition was set on it. That was harmless while every subpicture of a flow had the same geometry, because the output helper deduplicates identical flow definitions and none reached the sub. With the size and padding attributes changing per subpicture a flow definition arrives with every one of them, so the picture being blitted onto each frame was freed as the next one was negotiated and there was nothing left to blit. Keep the geometry the ubuf was accepted under alongside it, and blit with that. The geometry read from the flow definition is now only what the next input is validated against, so the two no longer have to describe the same subpicture. A change of the main input's flow format does still invalidate the subpictures, since they were scaled and placed for the previous format, so drop them where that is handled instead. --- lib/upipe-modules/upipe_blit.c | 50 ++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/upipe-modules/upipe_blit.c b/lib/upipe-modules/upipe_blit.c index f65af3af8..da632e47b 100644 --- a/lib/upipe-modules/upipe_blit.c +++ b/lib/upipe-modules/upipe_blit.c @@ -175,6 +175,20 @@ struct upipe_blit_sub { /** computed vertical position */ uint64_t vposition; + /* The geometry above is what the next input is validated against, and a + * new flow definition replaces it. The ubuf keeps the geometry it was + * accepted under, so it stays blittable while the flow definition of the + * subpicture that follows it is being negotiated. */ + + /** horizontal size of ubuf */ + uint64_t ubuf_hsize; + /** vertical size of ubuf */ + uint64_t ubuf_vsize; + /** horizontal position of ubuf */ + uint64_t ubuf_hposition; + /** vertical position of ubuf */ + uint64_t ubuf_vposition; + /** flow format urequests */ struct uchain flow_format_requests; @@ -217,6 +231,8 @@ static struct upipe *upipe_blit_sub_alloc(struct upipe_mgr *mgr, sub->loffset_r = sub->roffset_r = sub->toffset_r = sub->boffset_r = 0; sub->ubuf = NULL; sub->hsize = sub->vsize = sub->hposition = sub->vposition = UINT64_MAX; + sub->ubuf_hsize = sub->ubuf_vsize = + sub->ubuf_hposition = sub->ubuf_vposition = UINT64_MAX; ulist_init(&sub->flow_format_requests); upipe_throw_ready(upipe); @@ -237,8 +253,9 @@ static void upipe_blit_sub_work(struct upipe *upipe, struct uref *uref) if (unlikely(sub->ubuf == NULL)) return; - int err = uref_pic_blit(uref, sub->ubuf, sub->hposition, sub->vposition, - 0, 0, sub->hsize, sub->vsize, sub->alpha, + int err = uref_pic_blit(uref, sub->ubuf, + sub->ubuf_hposition, sub->ubuf_vposition, + 0, 0, sub->ubuf_hsize, sub->ubuf_vsize, sub->alpha, sub->alpha_threshold); if (unlikely(!ubase_check(err))) { upipe_warn(upipe, "unable to blit picture"); @@ -281,6 +298,13 @@ static void upipe_blit_sub_input(struct upipe *upipe, struct uref *uref, ubuf_free(sub->ubuf); sub->ubuf = uref_detach_ubuf(uref); + /* Keep the geometry it was accepted under: the one above is replaced as + * soon as the next subpicture's flow definition is negotiated, which + * happens while this one is still being blitted. */ + sub->ubuf_hsize = sub->hsize; + sub->ubuf_vsize = sub->vsize; + sub->ubuf_hposition = sub->hposition; + sub->ubuf_vposition = sub->vposition; uref_free(uref); } @@ -571,8 +595,9 @@ static int upipe_blit_sub_set_flow_def(struct upipe *upipe, uref_pic_get_hposition(flow_def, &sub->hposition); uref_pic_get_vposition(flow_def, &sub->vposition); - ubuf_free(sub->ubuf); - sub->ubuf = NULL; + /* The subpicture keeps being blitted until the next one arrives, and with + * a geometry per subpicture a flow definition arrives with every one of + * them, so dropping it here would leave nothing to blit. */ return UBASE_ERR_NONE; } @@ -749,6 +774,8 @@ static int _upipe_blit_sub_flush(struct upipe *upipe) struct upipe_blit_sub *sub = upipe_blit_sub_from_upipe(upipe); ubuf_free(sub->ubuf); sub->ubuf = NULL; + sub->ubuf_hsize = sub->ubuf_vsize = + sub->ubuf_hposition = sub->ubuf_vposition = UINT64_MAX; return UBASE_ERR_NONE; } @@ -1040,6 +1067,13 @@ static int upipe_blit_set_flow_def(struct upipe *upipe, struct uref *flow_def) struct uchain *uchain; ulist_foreach (&upipe_blit->subs, uchain) { struct upipe_blit_sub *sub = upipe_blit_sub_from_uchain(uchain); + /* The subpictures were scaled and placed for the previous format, so + * unlike a change of their own flow definition this does invalidate + * them. */ + ubuf_free(sub->ubuf); + sub->ubuf = NULL; + sub->ubuf_hsize = sub->ubuf_vsize = + sub->ubuf_hposition = sub->ubuf_vposition = UINT64_MAX; upipe_blit_sub_provide_flow_format(upipe_blit_sub_to_upipe(sub)); } return UBASE_ERR_NONE; @@ -1107,8 +1141,8 @@ static int _upipe_blit_prepare(struct upipe *upipe, struct upump **upump_p) ulist_foreach (&upipe_blit->subs, uchain) { struct upipe_blit_sub *sub = upipe_blit_sub_from_uchain(uchain); if (sub->ubuf != NULL && - sub->hposition + sub->hsize <= hsize && - sub->vposition + sub->vsize <= vsize) { + sub->ubuf_hposition + sub->ubuf_hsize <= hsize && + sub->ubuf_vposition + sub->ubuf_vsize <= vsize) { fused = sub; break; } @@ -1117,8 +1151,8 @@ static int _upipe_blit_prepare(struct upipe *upipe, struct upump **upump_p) if (fused != NULL) { int w = hsize, h = vsize; - int hp = fused->hposition, vp = fused->vposition; - int hs = fused->hsize, vs = fused->vsize; + int hp = fused->ubuf_hposition, vp = fused->ubuf_vposition; + int hs = fused->ubuf_hsize, vs = fused->ubuf_vsize; ubuf = ubuf_pic_alloc(upipe_blit->ubuf_mgr, w, h); if (ubuf != NULL) { int err = UBASE_ERR_NONE; From 61cbd7031fd4de4f829aab7524c45e81052700ec Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 12 Aug 2026 04:06:28 +0100 Subject: [PATCH 10/30] ubuf_pic_blit: add 10 bit alpha blend kernels The alpha levels are used rather than keyed on when the threshold is the maximum, and that path had no SIMD, so a burnt-in subtitle spent its time in a per sample multiply, add and divide by 0x3ff, the last of which the compiler turns into a 64 bit magic multiply. The numerator is a dot product of (bg, src) with (0x3ff - a, a), which is one pmaddwd on interleaved pairs; the inputs are all below 0x400 so the 32 bit accumulators cannot overflow and the results pack back to words without saturating. The divide has an exact truncating shift form over the range the numerator can take, so the kernels stay bit exact with the C. --- lib/upipe/ubuf_pic_blit_x86.h | 27 ++++++ lib/upipe/x86/ubuf_pic_blit.asm | 167 ++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 16 +++ 3 files changed, 210 insertions(+) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index d8a3c2297..e8904e1cf 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -125,5 +125,32 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, } return false; } + + else if (threshold == 0x3ff && alpha_multiplier == 0x3ff) { + if (hsub == 1) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit10_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit10_blend_hsub1_ssse3(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + + if (hsub == 2) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + return true; + } + if (__builtin_cpu_supports("ssse3")) { + upipe_blit10_blend_hsub2_ssse3(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + return false; + } return false; } diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index c804d75e5..5ea0dbcc3 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -47,6 +47,76 @@ SECTION .text jne .loop_scalar %endmacro +; The blend kernels compute, per 10 bit sample, +; +; dst = (bg * (0x3ff - a) + src * a) / 0x3ff +; +; matching the C reference bit for bit. Two things make that cheap. +; +; The numerator is a dot product of (bg, src) with (0x3ff - a, a), which is what +; pmaddwd does: it multiplies signed 16 bit pairs and sums each adjacent pair +; into 32 bits. Every input is below 0x400, so the products and their sum stay +; well inside a signed 32 bit lane, and the results are below 0x400 again, so +; the pack back to words never saturates. +; +; The division has an exact shift form over the range the numerator can take, +; 0 to 0x3ff * 0x3ff. Writing N = 1023q + r with r <= 1022, +; +; (N + (N >> 10) + 1) >> 10 == q +; +; because N >> 10 is at least q - 1, so the sum reaches 1024q, and at most +; q + r/1024, so it stays below 1024(q + 1); the tightest case is r = 1022, +; which leaves 1023.998. It truncates, like the C does. + +%macro SCALAR_TAIL_BLEND_W 4-5 1 ; counter, temp1, temp2, temp3, optional subsampling + .loop_scalar: + movzx %2d, word [alpha_q + %5 * 2*%1q] ; load alpha sample + movzx %3d, word [bg_q + 2*%1q] ; load background sample + movzx %4d, word [src_q + 2*%1q] ; load source sample + imul %4d, %2d ; src * a + neg %2d + add %2d, 0x3ff ; 0x3ff - a + imul %3d, %2d ; bg * (0x3ff - a) + add %4d, %3d ; the numerator + mov %2d, %4d + shr %2d, 10 + add %4d, %2d ; N + (N >> 10) + add %4d, 1 + shr %4d, 10 ; / 0x3ff + mov [dst_q + 2*%1q], %4w + add %1q, 1 + cmp hsize_q, %1q + jne .loop_scalar +%endmacro + +%macro BLEND10_CONSTANTS 0 + pcmpeqw m0, m0 + psrlw m0, 6 ; 0x3ff in every word + pcmpeqd m1, m1 + psrld m1, 31 ; 1 in every dword +%endmacro + +; Takes bg in m2, src in m3 and the already expanded alpha in m4, leaves the +; finished samples in m6 and clobbers m2, m5 and m7. +%macro BLEND10 0 + psubw m5, m0, m4 ; 0x3ff - a + punpcklwd m6, m2, m3 ; (bg, src) pairs, low half + punpckhwd m2, m2, m3 ; and high half + punpcklwd m7, m5, m4 ; the matching (0x3ff - a, a) pairs + punpckhwd m5, m5, m4 + pmaddwd m6, m7 ; bg * (0x3ff - a) + src * a + pmaddwd m2, m5 + psrld m7, m6, 10 + paddd m6, m7 + paddd m6, m1 + psrld m6, 10 ; / 0x3ff + psrld m5, m2, 10 + paddd m2, m5 + paddd m2, m1 + psrld m2, 10 + packssdw m6, m2 ; back to words +%endmacro + INIT_XMM ssse3 %define xor_reg m0 @@ -201,6 +271,54 @@ cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, thresh .ret: RET +cglobal blit10_blend_hsub1, 5, 11, 8, dst_, bg_, src_, alpha_, hsize_ + BLEND10_CONSTANTS + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10 + .ret: +RET + +cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ + BLEND10_CONSTANTS + movq m8, [blit10_alpha_shuffle] ; Load shuffle constants to compress words + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 4*r6q - 2*mmsize] + movu m5, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + ; Need to compress every second word + pshufb m4, m8 + pshufb m5, m8 + punpcklqdq m4, m4, m5 + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +RET + INIT_YMM avx2 %define xor_reg m0 @@ -341,3 +459,52 @@ cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, thresh SCALAR_TAIL_W r7, r6, r8, 2 .ret: RET + +cglobal blit10_blend_hsub1, 5, 11, 8, dst_, bg_, src_, alpha_, hsize_ + BLEND10_CONSTANTS + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10 + .ret: +RET + +cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ + BLEND10_CONSTANTS + VBROADCASTI128 m8, [blit10_alpha_shuffle] ; Load shuffle constants to compress words + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 4*r6q - 2*mmsize] + movu m5, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + ; Need to compress every second word + pshufb m4, m8 + pshufb m5, m8 + punpcklqdq m4, m4, m5 + vpermq m4, m4, q3120 ; Permute to get correct order of qwords + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +RET diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index bd6b627e7..9c7ba9833 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -23,3 +23,19 @@ void upipe_blit10_threshold_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_p void upipe_blit10_threshold_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); + +/* dst = (bg * (0x3ff - alpha) + src * alpha) / 0x3ff, i.e. the maximum + * threshold, blending with the alpha levels. Pass bg == dst to blend in + * place. There is no threshold argument: it is implied by the method. */ +void upipe_blit10_blend_hsub1_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_hsub2_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); From b86e369c8634be8bd3bcc6a18b6d403354baea23 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 12 Aug 2026 04:14:36 +0100 Subject: [PATCH 11/30] tests: the saturated threshold has kernels too now --- tests/ubuf_pic_blit_test.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/ubuf_pic_blit_test.c b/tests/ubuf_pic_blit_test.c index 0eef883c5..886034165 100644 --- a/tests/ubuf_pic_blit_test.c +++ b/tests/ubuf_pic_blit_test.c @@ -1608,10 +1608,12 @@ int main(int argc, char **argv) { "y10l", "a10l", 1, true, { 2, 14, 16, 18, 24, 30, 34, 50, 100 } }, { "u10l", "a10l", 2, true, { 4, 28, 32, 36, 48, 60, 68, 100, 200 } }, }; - /* threshold 0 is the copy and the saturated threshold is the smooth - * blending; everything else is the on/off blending the kernels - * implement. A non saturated alpha multiplier scales the alpha plane - * sample first, which they leave to the caller. */ + /* threshold 0 is the copy, which the caller does itself. A threshold + * between that and the saturated one is the on/off blending, and the + * saturated one is the smooth blending; both have kernels, except that + * smooth blending only has them for 10 bit. A non saturated alpha + * multiplier scales the alpha plane sample first, which the kernels + * leave to the caller whichever the method. */ static const struct { int alpha8, threshold8, alpha10, threshold10; } cases[] = { { 0xff, 0, 0x3ff, 0 }, From b757fb3874baec4e70f66a9ee82afe2a53c7de74 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 12 Aug 2026 04:25:58 +0100 Subject: [PATCH 12/30] checkasm: cover the 10 bit alpha blend kernels The whole-plane test in ubuf_pic_blit_test already checks them against the reference, but through ubuf_pic_blit, so it cannot bench them or say which instruction set it just exercised. Call the kernels directly instead, over a width that is not a multiple of any vector so the scalar tail runs, with a destination prefilled with a value the blend cannot produce and a guard past the end of the line. --- tests/checkasm/Build.mk | 4 +- tests/checkasm/checkasm.c | 1 + tests/checkasm/checkasm.h | 1 + tests/checkasm/ubuf_pic_blit.c | 119 +++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tests/checkasm/ubuf_pic_blit.c diff --git a/tests/checkasm/Build.mk b/tests/checkasm/Build.mk index 205db0192..e8117a469 100644 --- a/tests/checkasm/Build.mk +++ b/tests/checkasm/Build.mk @@ -8,6 +8,7 @@ checkasm-src = \ planar8_input.c \ sdi_input.c \ timer.h \ + ubuf_pic_blit.c \ uyvy_input.c \ v210_input.c @@ -26,4 +27,5 @@ $(builddir)/checkasm: \ $(top_builddir)/lib/upipe-hbrmt/sdienc.o \ $(top_builddir)/lib/upipe-hbrmt/sdidec.o \ $(top_builddir)/lib/upipe-hbrmt/x86/sdienc.o \ - $(top_builddir)/lib/upipe-hbrmt/x86/sdidec.o + $(top_builddir)/lib/upipe-hbrmt/x86/sdidec.o \ + $(top_builddir)/lib/upipe/x86/ubuf_pic_blit.o diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c index 44493069a..34d8a6703 100644 --- a/tests/checkasm/checkasm.c +++ b/tests/checkasm/checkasm.c @@ -80,6 +80,7 @@ static const struct { { "sdi_input", checkasm_check_sdi_input }, { "uyvy_input", checkasm_check_uyvy_input }, { "v210_input", checkasm_check_v210_input }, + { "ubuf_pic_blit", checkasm_check_ubuf_pic_blit }, { NULL, NULL } }; diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h index 673564622..b16b49430 100644 --- a/tests/checkasm/checkasm.h +++ b/tests/checkasm/checkasm.h @@ -83,6 +83,7 @@ void checkasm_check_planar8_input(void); void checkasm_check_sdi_input(void); void checkasm_check_uyvy_input(void); void checkasm_check_v210_input(void); +void checkasm_check_ubuf_pic_blit(void); struct CheckasmPerf; diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c new file mode 100644 index 000000000..fc1475286 --- /dev/null +++ b/tests/checkasm/ubuf_pic_blit.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2026 Open Broadcast Systems Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include "checkasm.h" + +#ifdef HAVE_X86ASM +#include "lib/upipe/x86/ubuf_pic_blit.h" +#endif + +/* Not a multiple of any vector width, so the scalar tail runs too, and at least + * mmsize/2 samples for the widest kernel, which the dispatcher guarantees by + * declining anything under 16. */ +#define NUM_SAMPLES 517 +/* Samples past the end are left alone by the reference, so they catch a kernel + * writing beyond the line. */ +#define GUARD 64 + +static void randomize_buffers(uint16_t *bg, uint16_t *src, uint16_t *alpha, + size_t hsize, uint8_t hsub) +{ + for (size_t i = 0; i < hsize; i++) { + bg[i] = rnd() & 0x3ff; + src[i] = rnd() & 0x3ff; + } + for (size_t i = 0; i < hsize * hsub; i++) + alpha[i] = rnd() & 0x3ff; +} + +/* The blending the kernels replace, as ubuf_pic_blit_alpha10_bg() spells it for + * a saturated threshold and a saturated alpha multiplier. */ +#define BLEND10_REF(name, hsub) \ +static void name(uint16_t *dst, const uint16_t *bg, const uint16_t *src, \ + const uint16_t *alpha, size_t hsize) \ +{ \ + for (size_t j = 0; j < hsize; j++) { \ + const uint16_t a = alpha[j * (hsub)]; \ + dst[j] = (bg[j] * (0x3ff - a) + src[j] * a) / 0x3ff; \ + } \ +} + +BLEND10_REF(blit10_blend_hsub1_ref, 1) +BLEND10_REF(blit10_blend_hsub2_ref, 2) + +static void check_blend10(void (*func)(uint16_t *, const uint16_t *, + const uint16_t *, const uint16_t *, + size_t), + const char *name, uint8_t hsub) +{ + if (!check_func(func, "%s", name)) + return; + + uint16_t bg[NUM_SAMPLES]; + uint16_t src[NUM_SAMPLES]; + uint16_t alpha[NUM_SAMPLES * 2]; + uint16_t dst0[NUM_SAMPLES + GUARD]; + uint16_t dst1[NUM_SAMPLES + GUARD]; + + declare_func(void, uint16_t *dst, const uint16_t *bg, const uint16_t *src, + const uint16_t *alpha, size_t hsize); + + randomize_buffers(bg, src, alpha, NUM_SAMPLES, hsub); + /* A value the blend cannot produce, so a sample left unwritten is caught + * rather than matching by luck. */ + memset(dst0, 0xa5, sizeof dst0); + memset(dst1, 0xa5, sizeof dst1); + + call_ref(dst0, bg, src, alpha, NUM_SAMPLES); + call_new(dst1, bg, src, alpha, NUM_SAMPLES); + if (memcmp(dst0, dst1, sizeof dst0)) + fail(); + + bench_new(dst1, bg, src, alpha, NUM_SAMPLES); +} + +void checkasm_check_ubuf_pic_blit(void) +{ + void (*blend_hsub1)(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t) = blit10_blend_hsub1_ref; + void (*blend_hsub2)(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t) = blit10_blend_hsub2_ref; + +#ifdef HAVE_X86ASM + int cpu_flags = av_get_cpu_flags(); + + if (cpu_flags & AV_CPU_FLAG_SSSE3) { + blend_hsub1 = upipe_blit10_blend_hsub1_ssse3; + blend_hsub2 = upipe_blit10_blend_hsub2_ssse3; + } + if (cpu_flags & AV_CPU_FLAG_AVX2) { + blend_hsub1 = upipe_blit10_blend_hsub1_avx2; + blend_hsub2 = upipe_blit10_blend_hsub2_avx2; + } +#endif + + check_blend10(blend_hsub1, "blit10_blend_hsub1", 1); + report("blit10_blend_hsub1"); + + check_blend10(blend_hsub2, "blit10_blend_hsub2", 2); + report("blit10_blend_hsub2"); +} From 5072c0b37d3953ddde8380f4b071f0e692a7557c Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 12 Aug 2026 13:24:09 +0100 Subject: [PATCH 13/30] ubuf_pic_blit: drop the SSSE3 kernels Leaves AVX2 as the only vector path; anything older falls back to C. --- lib/upipe/ubuf_pic_blit_x86.h | 24 ---- lib/upipe/x86/ubuf_pic_blit.asm | 203 -------------------------------- lib/upipe/x86/ubuf_pic_blit.h | 18 --- tests/checkasm/ubuf_pic_blit.c | 4 - 4 files changed, 249 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index e8904e1cf..d1f83a357 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -43,10 +43,6 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, upipe_blit8_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit8_threshold_hsub1_ssse3(dst, bg, src, alpha, hsize, threshold); - return true; - } return false; } @@ -55,10 +51,6 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit8_threshold_hsub2_ssse3(dst, bg, src, alpha, hsize, threshold); - return true; - } return false; } return false; @@ -105,10 +97,6 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit10_threshold_hsub1_ssse3(dst, bg, src, alpha, hsize, threshold); - return true; - } return false; } @@ -117,10 +105,6 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit10_threshold_hsub2_ssse3(dst, bg, src, alpha, hsize, threshold); - return true; - } return false; } return false; @@ -132,10 +116,6 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_blend_hsub1_avx2(dst, bg, src, alpha, hsize); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit10_blend_hsub1_ssse3(dst, bg, src, alpha, hsize); - return true; - } return false; } @@ -144,10 +124,6 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; } - if (__builtin_cpu_supports("ssse3")) { - upipe_blit10_blend_hsub2_ssse3(dst, bg, src, alpha, hsize); - return true; - } return false; } return false; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 5ea0dbcc3..066a56d27 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -5,7 +5,6 @@ ALIGNMODE p6 SECTION_RODATA 16 -blit10_threshold_splat: times 8 db 0, 1 blit10_alpha_shuffle: db 0,1, 4,5, 8,9, 12,13 times 8 db -1 blit8_alpha_shuffle: db 0, 2, 4, 6, 8, 10, 12, 14 @@ -117,208 +116,6 @@ SECTION .text packssdw m6, m2 ; back to words %endmacro -INIT_XMM ssse3 - -%define xor_reg m0 -%define threshold m1 -%define bg m2 -%define src m3 -%define alp m4 - -cglobal blit8_threshold_hsub1, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ - mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte - pxor m5, m5 - movd threshold, threshold_d - movd xor_reg, r6d - pshufb threshold, m5 ; Splat threshold to every byte - pshufd xor_reg, xor_reg, 0 ; Splat 0x80 to every byte - mov r6d, mmsize - pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick - ALIGN 16 - .loop_simd: - movu bg, [bg_q + r6q - mmsize] - movu src, [src_q + r6q - mmsize] - movu alp, [alpha_q + r6q - mmsize] - mov r7q, r6q - pxor alp, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick - pcmpgtb alp, threshold - mova m5, alp - pand src, alp - pandn m5, bg - por m5, src - movu [dst_q + r6q - mmsize], m5 - lea r6q, [r6q + mmsize] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_B r7, r6, r8 - .ret: -RET - -%define xor_reg m0 -%define threshold m1 -%define shuffle m2 -%define bg m3 -%define src m4 -%define alp m5 - -cglobal blit8_threshold_hsub2, 6, 9, 7, dst_, bg_, src_, alpha_, hsize_, threshold_ - mov r6d, 0x01010101 * 0x80 ; Splat 0x80 to every byte - pxor m5, m5 - movd threshold, threshold_d - movd xor_reg, r6d - movq shuffle, [blit8_alpha_shuffle] - pshufb threshold, m5 ; Splat threshold to every byte - pshufd xor_reg, xor_reg, 0 ; Splat 0x80 to every byte - mov r6d, mmsize - pxor threshold, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick - ALIGN 16 - .loop_simd: - movu bg, [bg_q + r6q - mmsize] - movu src, [src_q + r6q - mmsize] - movu m5, [alpha_q + 2*r6q - 2*mmsize] - movu m6, [alpha_q + 2*r6q - 1*mmsize] - ; Need to compress every second byte - pshufb m5, shuffle - pshufb m6, shuffle - punpcklqdq alp, m5, m6 - mov r7q, r6q - pxor alp, xor_reg ; Packed compare bytes is for signed bytes so need to do a trick - pcmpgtb alp, threshold - mova m6, alp - pand src, alp - pandn m6, bg - por m6, src - movu [dst_q + r6q - mmsize], m6 - lea r6q, [r6q + mmsize] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_B r7, r6, r8, 2 - .ret: -RET - -%define threshold m0 -%define bg m1 -%define src m2 -%define alp m3 - -cglobal blit10_threshold_hsub1, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ - movd threshold, threshold_d - mov r6d, mmsize/2 - pshufb threshold, [blit10_threshold_splat] ; Splat threshold to every word - ALIGN 16 - .loop_simd: - movu bg, [bg_q + 2*r6q - mmsize] - movu src, [src_q + 2*r6q - mmsize] - movu alp, [alpha_q + 2*r6q - mmsize] - mov r7q, r6q - pcmpgtw alp, threshold - mova m5, alp - pand src, alp - pandn m5, bg - por m5, src - movu [dst_q + 2*r6q - mmsize], m5 - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_W r7, r6, r8 - .ret: -RET - -%define shuffle m0 -%define threshold m1 -%define bg m2 -%define src m3 -%define alp m4 - -cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, threshold_ - movd threshold, threshold_d - mov r6d, mmsize/2 - pshufb threshold, [blit10_threshold_splat] ; Splat threshold to every word - movq shuffle, [blit10_alpha_shuffle] ; Load shuffle constants to compress words - ALIGN 16 - .loop_simd: - movu bg, [bg_q + 2*r6q - mmsize] - movu src, [src_q + 2*r6q - mmsize] - movu m4, [alpha_q + 4*r6q - 2*mmsize] - movu m5, [alpha_q + 4*r6q - 1*mmsize] - mov r7q, r6q - ; Need to compress every second word - pshufb m4, shuffle - pshufb m5, shuffle - punpcklqdq alp, m4, m5 - pcmpgtw alp, threshold - mova m5, alp - pand src, alp - pandn m5, bg - por m5, src - movu [dst_q + 2*r6q - mmsize], m5 - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_W r7, r6, r8, 2 - .ret: -RET - -cglobal blit10_blend_hsub1, 5, 11, 8, dst_, bg_, src_, alpha_, hsize_ - BLEND10_CONSTANTS - mov r6d, mmsize/2 - ALIGN 16 - .loop_simd: - movu m2, [bg_q + 2*r6q - mmsize] - movu m3, [src_q + 2*r6q - mmsize] - movu m4, [alpha_q + 2*r6q - mmsize] - mov r7q, r6q - BLEND10 - movu [dst_q + 2*r6q - mmsize], m6 - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_BLEND_W r7, r8, r9, r10 - .ret: -RET - -cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ - BLEND10_CONSTANTS - movq m8, [blit10_alpha_shuffle] ; Load shuffle constants to compress words - mov r6d, mmsize/2 - ALIGN 16 - .loop_simd: - movu m2, [bg_q + 2*r6q - mmsize] - movu m3, [src_q + 2*r6q - mmsize] - movu m4, [alpha_q + 4*r6q - 2*mmsize] - movu m5, [alpha_q + 4*r6q - 1*mmsize] - mov r7q, r6q - ; Need to compress every second word - pshufb m4, m8 - pshufb m5, m8 - punpcklqdq m4, m4, m5 - BLEND10 - movu [dst_q + 2*r6q - mmsize], m6 - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 - .ret: -RET - INIT_YMM avx2 %define xor_reg m0 diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 9c7ba9833..4168b4c61 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -1,16 +1,4 @@ /* dst = alpha > threshold ? src : bg. Pass bg == dst to blend in place. */ -void upipe_blit8_threshold_hsub1_ssse3(uint8_t *dst_plane, const uint8_t *bg_plane, - const uint8_t *src_plane, - const uint8_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit8_threshold_hsub2_ssse3(uint8_t *dst_plane, const uint8_t *bg_plane, - const uint8_t *src_plane, - const uint8_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit10_threshold_hsub1_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, - const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit10_threshold_hsub2_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, - const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize, int threshold); void upipe_blit8_threshold_hsub1_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, int threshold); @@ -27,12 +15,6 @@ void upipe_blit10_threshold_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_p /* dst = (bg * (0x3ff - alpha) + src * alpha) / 0x3ff, i.e. the maximum * threshold, blending with the alpha levels. Pass bg == dst to blend in * place. There is no threshold argument: it is implied by the method. */ -void upipe_blit10_blend_hsub1_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, - const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize); -void upipe_blit10_blend_hsub2_ssse3(uint16_t *dst_plane, const uint16_t *bg_plane, - const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize); void upipe_blit10_blend_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index fc1475286..1f027319c 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -101,10 +101,6 @@ void checkasm_check_ubuf_pic_blit(void) #ifdef HAVE_X86ASM int cpu_flags = av_get_cpu_flags(); - if (cpu_flags & AV_CPU_FLAG_SSSE3) { - blend_hsub1 = upipe_blit10_blend_hsub1_ssse3; - blend_hsub2 = upipe_blit10_blend_hsub2_ssse3; - } if (cpu_flags & AV_CPU_FLAG_AVX2) { blend_hsub1 = upipe_blit10_blend_hsub1_avx2; blend_hsub2 = upipe_blit10_blend_hsub2_avx2; From afb2e71c7e0933368a6e21e50905950aa2d9a112 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Mon, 17 Aug 2026 12:19:30 +0200 Subject: [PATCH 14/30] ubuf_pic_blit: add 8 bit alpha blend functions --- lib/upipe/ubuf_pic_blit_x86.h | 19 +++++ lib/upipe/x86/ubuf_pic_blit.asm | 142 ++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 7 ++ 3 files changed, 168 insertions(+) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index d1f83a357..2d6983aca 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -55,6 +55,25 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, } return false; } + + else if (threshold == 0xff && alpha_multiplier == 0xff) { + if (hsub == 1) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit8_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + + if (hsub == 2) { + if (__builtin_cpu_supports("avx2")) { + upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + return false; + } return false; } diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 066a56d27..feb53fbda 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -46,6 +46,27 @@ SECTION .text jne .loop_scalar %endmacro +%macro SCALAR_TAIL_BLEND_B 4-5 1 ; counter, temp1, temp2, temp3, optional subsampling + .loop_scalar: + movzx %2d, byte [alpha_q + %5 * %1q] ; load alpha sample + movzx %3d, byte [bg_q + %1q] ; load background sample + movzx %4d, byte [src_q + %1q] ; load source sample + imul %4d, %2d ; src * a + neg %2d + add %2d, 0xff ; 0xff - a + imul %3d, %2d ; bg * (0xff - a) + add %4d, %3d ; the numerator + mov %2d, %4d + shr %2d, 8 + add %4d, %2d ; N + (N >> 8) + add %4d, 1 + shr %4d, 8 ; / 0xff + mov [dst_q + %1q], %4b + add %1q, 1 + cmp hsize_q, %1q + jne .loop_scalar +%endmacro + ; The blend kernels compute, per 10 bit sample, ; ; dst = (bg * (0x3ff - a) + src * a) / 0x3ff @@ -257,6 +278,127 @@ cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, thresh .ret: RET +%define zero m0 +%define pw1 m1 +%define pw255 m2 + +cglobal blit8_blend_hsub1, 5, 10, 11, dst_, bg_, src_, alpha_, hsize_ + pxor zero, zero + pcmpeqw pw255, pw255 + mov r6d, mmsize + psrlw pw1, pw255, 15 + psrlw pw255, 8 + ALIGN 16 + .loop_simd: + ; Load and unpack alpha + movu m3, [alpha_q + r6q - mmsize] + punpckhbw m4, m3, zero + punpcklbw m3, zero + mov r7q, r6q + ; Load and unpack src + movu m5, [src_q + r6q - mmsize] + punpckhbw m6, m5, zero + punpcklbw m5, zero + ; 255-a + psubw m8, pw255, m4 + psubw m7, pw255, m3 + ; Load and unpack bg + movu m9, [bg_q + r6q - mmsize] + punpckhbw m10, m9, zero + punpcklbw m9, zero + ; src*a + pmullw m6, m4 + pmullw m5, m3 + ; bg*(255-a) + pmullw m10, m8 + pmullw m9, m7 + ; bg*(255-a) + src*a + paddw m4, m10, m6 + paddw m3, m9, m5 + ; /255 -> (t + 1 + (t>>8)) >> 8 + paddw m6, m4, pw1 + paddw m5, m3, pw1 + psrlw m4, 8 + psrlw m3, 8 + paddw m6, m4 + paddw m5, m3 + psrlw m6, 8 + psrlw m5, 8 + packuswb m5, m6 + movu [dst_q + r6q - mmsize], m5 + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6 + .ret: +RET + +%define shuffle m11 + +cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ + pxor zero, zero + pcmpeqw pw255, pw255 + mov r6d, mmsize + psrlw pw1, pw255, 15 + psrlw pw255, 8 + VBROADCASTI128 shuffle, [blit8_alpha_shuffle] + ALIGN 16 + .loop_simd: + ; Load and unpack alpha + movu m5, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + ; Need to compress every second byte + pshufb m5, shuffle + pshufb m6, shuffle + punpcklqdq m3, m5, m6 + vpermq m3, m3, q3120 ; Permute to get correct order of qwords + punpckhbw m4, m3, zero + punpcklbw m3, zero + mov r7q, r6q + ; Load and unpack src + movu m5, [src_q + r6q - mmsize] + punpckhbw m6, m5, zero + punpcklbw m5, zero + ; 255-a + psubw m8, pw255, m4 + psubw m7, pw255, m3 + ; Load and unpack bg + movu m9, [bg_q + r6q - mmsize] + punpckhbw m10, m9, zero + punpcklbw m9, zero + ; src*a + pmullw m6, m4 + pmullw m5, m3 + ; bg*(255-a) + pmullw m10, m8 + pmullw m9, m7 + ; bg*(255-a) + src*a + paddw m4, m10, m6 + paddw m3, m9, m5 + ; /255 -> (t + 1 + (t>>8)) >> 8 + paddw m6, m4, pw1 + paddw m5, m3, pw1 + psrlw m4, 8 + psrlw m3, 8 + paddw m6, m4 + paddw m5, m3 + psrlw m6, 8 + psrlw m5, 8 + packuswb m5, m6 + movu [dst_q + r6q - mmsize], m5 + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 + .ret: +RET + cglobal blit10_blend_hsub1, 5, 11, 8, dst_, bg_, src_, alpha_, hsize_ BLEND10_CONSTANTS mov r6d, mmsize/2 diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 4168b4c61..d0736c45a 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -12,6 +12,13 @@ void upipe_blit10_threshold_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_p const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_blend_hsub1_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize); +void upipe_blit8_blend_hsub2_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize); + /* dst = (bg * (0x3ff - alpha) + src * alpha) / 0x3ff, i.e. the maximum * threshold, blending with the alpha levels. Pass bg == dst to blend in * place. There is no threshold argument: it is implied by the method. */ From 5191b91fd47bd9574f1bc8daa462d63dbaba3d5f Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Tue, 6 Oct 2026 07:44:47 +0100 Subject: [PATCH 15/30] checkasm: test the 8 bit blit blend kernels blit8_blend_hsub1/2 had no test. The same shape as the 10 bit one: an odd length so that the scalar tail runs, a guard past the end, and the extremes of alpha and of the samples. --- tests/checkasm/ubuf_pic_blit.c | 64 ++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index 1f027319c..401a6d651 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -91,12 +91,68 @@ static void check_blend10(void (*func)(uint16_t *, const uint16_t *, bench_new(dst1, bg, src, alpha, NUM_SAMPLES); } +/* The 8 bit blending, as ubuf_pic_blit_alpha8_bg() spells it for a saturated + * threshold and a saturated alpha multiplier. */ +#define BLEND8_REF(name, hsub) \ +static void name(uint8_t *dst, const uint8_t *bg, const uint8_t *src, \ + const uint8_t *alpha, size_t hsize) \ +{ \ + for (size_t j = 0; j < hsize; j++) { \ + const uint8_t a = alpha[j * (hsub)]; \ + dst[j] = (bg[j] * (0xff - a) + src[j] * a) / 0xff; \ + } \ +} + +BLEND8_REF(blit8_blend_hsub1_ref, 1) +BLEND8_REF(blit8_blend_hsub2_ref, 2) + +static void check_blend8(void (*func)(uint8_t *, const uint8_t *, + const uint8_t *, const uint8_t *, + size_t), + const char *name, uint8_t hsub) +{ + if (!check_func(func, "%s", name)) + return; + + uint8_t bg[NUM_SAMPLES]; + uint8_t src[NUM_SAMPLES]; + uint8_t alpha[NUM_SAMPLES * 2]; + uint8_t dst0[NUM_SAMPLES + GUARD]; + uint8_t dst1[NUM_SAMPLES + GUARD]; + + declare_func(void, uint8_t *dst, const uint8_t *bg, const uint8_t *src, + const uint8_t *alpha, size_t hsize); + + for (size_t i = 0; i < NUM_SAMPLES; i++) { + bg[i] = rnd(); + src[i] = rnd(); + } + /* the extremes of alpha and of the samples too */ + for (size_t i = 0; i < NUM_SAMPLES * hsub; i++) + alpha[i] = i < 8 ? (i & 1 ? 0xff : 0) : rnd(); + bg[0] = src[1] = 0; + bg[1] = src[0] = 0xff; + memset(dst0, 0xa5, sizeof dst0); + memset(dst1, 0xa5, sizeof dst1); + + call_ref(dst0, bg, src, alpha, NUM_SAMPLES); + call_new(dst1, bg, src, alpha, NUM_SAMPLES); + if (memcmp(dst0, dst1, sizeof dst0)) + fail(); + + bench_new(dst1, bg, src, alpha, NUM_SAMPLES); +} + void checkasm_check_ubuf_pic_blit(void) { void (*blend_hsub1)(uint16_t *, const uint16_t *, const uint16_t *, const uint16_t *, size_t) = blit10_blend_hsub1_ref; void (*blend_hsub2)(uint16_t *, const uint16_t *, const uint16_t *, const uint16_t *, size_t) = blit10_blend_hsub2_ref; + void (*blend8_hsub1)(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t) = blit8_blend_hsub1_ref; + void (*blend8_hsub2)(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t) = blit8_blend_hsub2_ref; #ifdef HAVE_X86ASM int cpu_flags = av_get_cpu_flags(); @@ -104,6 +160,8 @@ void checkasm_check_ubuf_pic_blit(void) if (cpu_flags & AV_CPU_FLAG_AVX2) { blend_hsub1 = upipe_blit10_blend_hsub1_avx2; blend_hsub2 = upipe_blit10_blend_hsub2_avx2; + blend8_hsub1 = upipe_blit8_blend_hsub1_avx2; + blend8_hsub2 = upipe_blit8_blend_hsub2_avx2; } #endif @@ -112,4 +170,10 @@ void checkasm_check_ubuf_pic_blit(void) check_blend10(blend_hsub2, "blit10_blend_hsub2", 2); report("blit10_blend_hsub2"); + + check_blend8(blend8_hsub1, "blit8_blend_hsub1", 1); + report("blit8_blend_hsub1"); + + check_blend8(blend8_hsub2, "blit8_blend_hsub2", 2); + report("blit8_blend_hsub2"); } From bac02a84fc6ee442f7ad7214d968815f5631a46d Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Tue, 6 Oct 2026 07:44:47 +0100 Subject: [PATCH 16/30] ubuf_pic_blit: 8 bit blend with pmaddubsw and pmulhuw With the weights (a, 0xff - a) unsigned and the samples flipped to signed, one pmaddubsw gives N - 0x7f80 for each pair of samples, and adding 0x7f81 and a pmulhuw by 0x101 divides by 0xff exactly (checked over every (a, src, bg)). 14 vector ops per 32 samples instead of 23. --- lib/upipe/x86/ubuf_pic_blit.asm | 141 ++++++++++++++------------------ 1 file changed, 60 insertions(+), 81 deletions(-) diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index feb53fbda..2c66c85a3 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -9,6 +9,9 @@ blit10_alpha_shuffle: db 0,1, 4,5, 8,9, 12,13 times 8 db -1 blit8_alpha_shuffle: db 0, 2, 4, 6, 8, 10, 12, 14 times 8 db -1 +blit8_blend_bias: dw 0x7f81 +blit8_blend_101: dw 0x101 +blit8_blend_80: db 0x80 SECTION .text @@ -278,54 +281,59 @@ cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, thresh .ret: RET -%define zero m0 -%define pw1 m1 -%define pw255 m2 +; The 8 bit blend kernels compute dst = (bg * (0xff - a) + src * a) / 0xff, +; matching the C bit for bit, with one multiply-add per pair of samples. +; +; pmaddubsw multiplies unsigned bytes by signed bytes and sums adjacent pairs: +; with the weights (a, 0xff - a) unsigned and the samples flipped to signed, +; (src - 0x80, bg - 0x80), it gives N - 0x7f80 for the numerator N, which +; lies between -0x7f80 and 0x7e81, so the sum never saturates. Adding 0x7f81 +; leaves N + 1 as an unsigned word, and the division is then one pmulhuw: +; (N + 1) * 0x101 >> 16 == N / 0xff for every N up to 0xff * 0xff (checked +; over all of them). + +%define pb_80 m0 +%define pw_bias m1 +%define pw_101 m2 +%define pb_ff m10 + +%macro BLEND8_CONSTANTS 0 + vpbroadcastb pb_80, [blit8_blend_80] + vpbroadcastw pw_bias, [blit8_blend_bias] + vpbroadcastw pw_101, [blit8_blend_101] + pcmpeqb pb_ff, pb_ff +%endmacro + +; Takes alpha in m3, loads src and bg at r6q, leaves the finished samples in +; m3 and clobbers m4 to m8. +%macro BLEND8 0 + pxor m4, m3, pb_ff ; 0xff - a + punpckhbw m5, m3, m4 ; (a, 0xff - a) pairs, high half + punpcklbw m3, m4 ; and low half + movu m6, [src_q + r6q - mmsize] + movu m7, [bg_q + r6q - mmsize] + pxor m6, pb_80 ; src - 0x80 + pxor m7, pb_80 ; bg - 0x80 + punpckhbw m8, m6, m7 ; the matching (src, bg) pairs + punpcklbw m6, m7 + pmaddubsw m3, m6 ; N - 0x7f80 + pmaddubsw m5, m8 + paddw m3, pw_bias ; N + 1 + paddw m5, pw_bias + pmulhuw m3, pw_101 ; / 0xff + pmulhuw m5, pw_101 + packuswb m3, m5 +%endmacro cglobal blit8_blend_hsub1, 5, 10, 11, dst_, bg_, src_, alpha_, hsize_ - pxor zero, zero - pcmpeqw pw255, pw255 + BLEND8_CONSTANTS mov r6d, mmsize - psrlw pw1, pw255, 15 - psrlw pw255, 8 ALIGN 16 .loop_simd: - ; Load and unpack alpha movu m3, [alpha_q + r6q - mmsize] - punpckhbw m4, m3, zero - punpcklbw m3, zero mov r7q, r6q - ; Load and unpack src - movu m5, [src_q + r6q - mmsize] - punpckhbw m6, m5, zero - punpcklbw m5, zero - ; 255-a - psubw m8, pw255, m4 - psubw m7, pw255, m3 - ; Load and unpack bg - movu m9, [bg_q + r6q - mmsize] - punpckhbw m10, m9, zero - punpcklbw m9, zero - ; src*a - pmullw m6, m4 - pmullw m5, m3 - ; bg*(255-a) - pmullw m10, m8 - pmullw m9, m7 - ; bg*(255-a) + src*a - paddw m4, m10, m6 - paddw m3, m9, m5 - ; /255 -> (t + 1 + (t>>8)) >> 8 - paddw m6, m4, pw1 - paddw m5, m3, pw1 - psrlw m4, 8 - psrlw m3, 8 - paddw m6, m4 - paddw m5, m3 - psrlw m6, 8 - psrlw m5, 8 - packuswb m5, m6 - movu [dst_q + r6q - mmsize], m5 + BLEND8 + movu [dst_q + r6q - mmsize], m3 lea r6q, [r6q + mmsize] cmp hsize_q, r6q jae .loop_simd @@ -339,56 +347,21 @@ RET %define shuffle m11 cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ - pxor zero, zero - pcmpeqw pw255, pw255 - mov r6d, mmsize - psrlw pw1, pw255, 15 - psrlw pw255, 8 + BLEND8_CONSTANTS VBROADCASTI128 shuffle, [blit8_alpha_shuffle] + mov r6d, mmsize ALIGN 16 .loop_simd: - ; Load and unpack alpha + ; Load alpha and compress every second byte movu m5, [alpha_q + 2*r6q - 2*mmsize] movu m6, [alpha_q + 2*r6q - 1*mmsize] - ; Need to compress every second byte pshufb m5, shuffle pshufb m6, shuffle punpcklqdq m3, m5, m6 vpermq m3, m3, q3120 ; Permute to get correct order of qwords - punpckhbw m4, m3, zero - punpcklbw m3, zero mov r7q, r6q - ; Load and unpack src - movu m5, [src_q + r6q - mmsize] - punpckhbw m6, m5, zero - punpcklbw m5, zero - ; 255-a - psubw m8, pw255, m4 - psubw m7, pw255, m3 - ; Load and unpack bg - movu m9, [bg_q + r6q - mmsize] - punpckhbw m10, m9, zero - punpcklbw m9, zero - ; src*a - pmullw m6, m4 - pmullw m5, m3 - ; bg*(255-a) - pmullw m10, m8 - pmullw m9, m7 - ; bg*(255-a) + src*a - paddw m4, m10, m6 - paddw m3, m9, m5 - ; /255 -> (t + 1 + (t>>8)) >> 8 - paddw m6, m4, pw1 - paddw m5, m3, pw1 - psrlw m4, 8 - psrlw m3, 8 - paddw m6, m4 - paddw m5, m3 - psrlw m6, 8 - psrlw m5, 8 - packuswb m5, m6 - movu [dst_q + r6q - mmsize], m5 + BLEND8 + movu [dst_q + r6q - mmsize], m3 lea r6q, [r6q + mmsize] cmp hsize_q, r6q jae .loop_simd @@ -399,6 +372,12 @@ cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ .ret: RET +%undef pb_80 +%undef pw_bias +%undef pw_101 +%undef pb_ff +%undef shuffle + cglobal blit10_blend_hsub1, 5, 11, 8, dst_, bg_, src_, alpha_, hsize_ BLEND10_CONSTANTS mov r6d, mmsize/2 From 9a2adaec7169b187d1f04e7544e7ecdc312090a2 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Mon, 28 Sep 2026 15:02:40 -0700 Subject: [PATCH 17/30] tests: checkasm test for the blit threshold kernels Only the blend kernels had one. The threshold kernels are checked at a multiple of every vector width, with a short tail, with a tail long enough to need both alpha vectors of a subsampled kernel, and at the smallest width the dispatcher passes on, with guard samples after the line. --- tests/checkasm/ubuf_pic_blit.c | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index 401a6d651..a160b213b 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -143,8 +143,84 @@ static void check_blend8(void (*func)(uint8_t *, const uint8_t *, bench_new(dst1, bg, src, alpha, NUM_SAMPLES); } +/* The selection the threshold kernels replace. */ +#define THRESHOLD_REF(name, type, hsub) \ +static void name(type *dst, const type *bg, const type *src, \ + const type *alpha, size_t hsize, int threshold) \ +{ \ + for (size_t j = 0; j < hsize; j++) \ + dst[j] = alpha[j * (hsub)] > threshold ? src[j] : bg[j]; \ +} + +THRESHOLD_REF(blit8_threshold_hsub1_ref, uint8_t, 1) +THRESHOLD_REF(blit8_threshold_hsub2_ref, uint8_t, 2) +THRESHOLD_REF(blit10_threshold_hsub1_ref, uint16_t, 1) +THRESHOLD_REF(blit10_threshold_hsub2_ref, uint16_t, 2) + +/* A multiple of every vector width, the short tail above, a tail long enough + * to need both alpha vectors of a subsampled kernel, and the smallest width + * the dispatcher passes on. */ +#define THRESHOLD_MAX 562 + +#define CHECK_THRESHOLD(bits, type, max, min_hsize) \ +static void check_threshold##bits(void (*func)(type *, const type *, \ + const type *, const type *, \ + size_t, int), \ + const char *name) \ +{ \ + static const size_t widths[] = { 512, NUM_SAMPLES, THRESHOLD_MAX, \ + min_hsize }; \ + \ + if (!check_func(func, "%s", name)) \ + return; \ + \ + type bg[THRESHOLD_MAX]; \ + type src[THRESHOLD_MAX]; \ + type alpha[THRESHOLD_MAX * 2]; \ + type dst0[THRESHOLD_MAX + GUARD]; \ + type dst1[THRESHOLD_MAX + GUARD]; \ + \ + declare_func(void, type *dst, const type *bg, const type *src, \ + const type *alpha, size_t hsize, int threshold); \ + \ + for (size_t i = 0; i < THRESHOLD_MAX; i++) { \ + bg[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + for (size_t i = 0; i < THRESHOLD_MAX * 2; i++) \ + alpha[i] = rnd() & (max); \ + /* neither 0 nor the maximum, which the dispatcher sends elsewhere */ \ + int threshold = 1 + rnd() % ((max) - 1); \ + \ + for (size_t w = 0; w < sizeof(widths) / sizeof(widths[0]); w++) { \ + memset(dst0, 0xa5, sizeof dst0); \ + memset(dst1, 0xa5, sizeof dst1); \ + call_ref(dst0, bg, src, alpha, widths[w], threshold); \ + call_new(dst1, bg, src, alpha, widths[w], threshold); \ + if (memcmp(dst0, dst1, sizeof dst0)) \ + fail(); \ + } \ + \ + bench_new(dst1, bg, src, alpha, NUM_SAMPLES, threshold); \ +} + +CHECK_THRESHOLD(8, uint8_t, 0xff, 32) +CHECK_THRESHOLD(10, uint16_t, 0x3ff, 16) + void checkasm_check_ubuf_pic_blit(void) { + void (*threshold8_hsub1)(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t, int) = + blit8_threshold_hsub1_ref; + void (*threshold8_hsub2)(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t, int) = + blit8_threshold_hsub2_ref; + void (*threshold10_hsub1)(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = + blit10_threshold_hsub1_ref; + void (*threshold10_hsub2)(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = + blit10_threshold_hsub2_ref; void (*blend_hsub1)(uint16_t *, const uint16_t *, const uint16_t *, const uint16_t *, size_t) = blit10_blend_hsub1_ref; void (*blend_hsub2)(uint16_t *, const uint16_t *, const uint16_t *, @@ -162,6 +238,10 @@ void checkasm_check_ubuf_pic_blit(void) blend_hsub2 = upipe_blit10_blend_hsub2_avx2; blend8_hsub1 = upipe_blit8_blend_hsub1_avx2; blend8_hsub2 = upipe_blit8_blend_hsub2_avx2; + threshold8_hsub1 = upipe_blit8_threshold_hsub1_avx2; + threshold8_hsub2 = upipe_blit8_threshold_hsub2_avx2; + threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx2; + threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx2; } #endif @@ -176,4 +256,16 @@ void checkasm_check_ubuf_pic_blit(void) check_blend8(blend8_hsub2, "blit8_blend_hsub2", 2); report("blit8_blend_hsub2"); + + check_threshold8(threshold8_hsub1, "blit8_threshold_hsub1"); + report("blit8_threshold_hsub1"); + + check_threshold8(threshold8_hsub2, "blit8_threshold_hsub2"); + report("blit8_threshold_hsub2"); + + check_threshold10(threshold10_hsub1, "blit10_threshold_hsub1"); + report("blit10_threshold_hsub1"); + + check_threshold10(threshold10_hsub2, "blit10_threshold_hsub2"); + report("blit10_threshold_hsub2"); } From 78103e8315c7737929c76d0317df9636ef2f3b9d Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Mon, 28 Sep 2026 15:03:19 -0700 Subject: [PATCH 18/30] ubuf_pic_blit: add avx512 threshold kernels The mask registers remove most of the work the avx2 kernels do around the compare: vpcmpub and vpcmpuw compare unsigned, so the +0x80 bias disappears, and a merge-masked load selects the source samples in place of pand/pandn/por. The subsampled kernels no longer need the two pshufb, the punpcklqdq and the vpermq that undid avx2's lane split. 10-bit compresses the alpha with a single vpermt2w taking the second vector straight from memory, 8-bit with vpmovwb and vinserti64x4, both of which cross lanes on their own. The trailing samples are predicated rather than handled by a scalar loop. Masked loads suppress faults on masked-out samples, so the tail never reads past the end of the row and the kernels accept any hsize; the callers still gate on the avx2 minimum for now. --- lib/upipe/ubuf_pic_blit_x86.h | 16 +++ lib/upipe/x86/ubuf_pic_blit.asm | 198 ++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 14 +++ tests/checkasm/ubuf_pic_blit.c | 8 ++ 4 files changed, 236 insertions(+) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index 2d6983aca..9c0b1d637 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -39,6 +39,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold != 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { + if (__builtin_cpu_supports("avx512bw")) { + upipe_blit8_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit8_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; @@ -47,6 +51,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, } if (hsub == 2) { + if (__builtin_cpu_supports("avx512bw")) { + upipe_blit8_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; @@ -112,6 +120,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold != 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { + if (__builtin_cpu_supports("avx512bw")) { + upipe_blit10_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit10_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; @@ -120,6 +132,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, } if (hsub == 2) { + if (__builtin_cpu_supports("avx512bw")) { + upipe_blit10_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 2c66c85a3..d821e571e 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -9,6 +9,10 @@ blit10_alpha_shuffle: db 0,1, 4,5, 8,9, 12,13 times 8 db -1 blit8_alpha_shuffle: db 0, 2, 4, 6, 8, 10, 12, 14 times 8 db -1 +; vpermt2w indices selecting the even words of two concatenated zmm registers +blit10_alpha_permute: + dw 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 + dw 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62 blit8_blend_bias: dw 0x7f81 blit8_blend_101: dw 0x101 blit8_blend_80: db 0x80 @@ -426,3 +430,197 @@ cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 .ret: RET + +; The avx512 kernels replace the +0x80 bias and the pand/pandn/por select with an +; unsigned compare into a mask register and a merge-masked load, and predicate the +; trailing samples instead of falling back to scalar code. Masked loads suppress +; faults on masked-out samples, so the tail never reads past the end of the row +; and any hsize is accepted. + +; Build k2, the predicate for the trailing hsize_q samples, and leave -1 in r6q +; for the alpha predicates below. %1 is the mask width: q for byte samples, +; d for word samples. +%macro TAIL_MASK 1 + mov r6q, -1 + bzhi r7q, r6q, hsize_q + kmov%1 k2, r7 %+ %1 +%endmacro + +; Build k3 and k4, the predicates for the two alpha vectors feeding the trailing +; samples of a subsampled kernel. %2 is the number of alpha samples per vector. +%macro TAIL_MASK_ALPHA 2 + add hsize_q, hsize_q ; alpha samples for the trailing destination samples + bzhi r7q, r6q, hsize_q ; bzhi leaves every bit set when the count exceeds the width + kmov%1 k3, r7 %+ %1 + sub hsize_q, %2 + jle %%lo_only + bzhi r7q, r6q, hsize_q + kmov%1 k4, r7 %+ %1 + jmp %%done + %%lo_only: + kxor%1 k4, k4, k4 + %%done: +%endmacro + +INIT_ZMM avx512 + +%define threshold m0 +%define blend m1 +%define alp m2 + +cglobal blit8_threshold_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcastb threshold, threshold_d + mov r6q, hsize_q + and r6q, -mmsize ; samples handled by the vector loop + add dst_q, r6q + add bg_q, r6q + add src_q, r6q + add alpha_q, r6q + neg r6q ; count the index up to zero + jz .tail + ALIGN 16 + .loop_simd: + vpcmpub k1, threshold, [alpha_q + r6q], 1 ; threshold < alpha + movu blend, [bg_q + r6q] + vmovdqu8 blend{k1}, [src_q + r6q] + movu [dst_q + r6q], blend + add r6q, mmsize + jl .loop_simd + .tail: + and hsize_q, mmsize-1 + jz .ret + TAIL_MASK q + vmovdqu8 alp{k2}{z}, [alpha_q] + vpcmpub k1, threshold, alp, 1 + kandq k1, k1, k2 ; a zero threshold would otherwise select samples past the row + vmovdqu8 blend{k2}{z}, [bg_q] + vmovdqu8 blend{k1}, [src_q] + vmovdqu8 [dst_q]{k2}, blend + .ret: +RET + +%define threshold m0 +%define blend m1 +%define alp m2 +%define alp2 m3 + +cglobal blit8_threshold_hsub2, 6, 8, 4, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcastb threshold, threshold_d + mov r6q, hsize_q + and r6q, -mmsize + add dst_q, r6q + add bg_q, r6q + add src_q, r6q + lea alpha_q, [alpha_q + 2*r6q] + neg r6q + jz .tail + ALIGN 16 + .loop_simd: + movu alp, [alpha_q + 2*r6q] + movu alp2, [alpha_q + 2*r6q + mmsize] + ; vpmovwb keeps the low byte of every word, i.e. every second alpha sample + vpmovwb ymm %+ alp, alp + vpmovwb ymm %+ alp2, alp2 + vinserti64x4 alp, alp, ymm %+ alp2, 1 + vpcmpub k1, threshold, alp, 1 + movu blend, [bg_q + r6q] + vmovdqu8 blend{k1}, [src_q + r6q] + movu [dst_q + r6q], blend + add r6q, mmsize + jl .loop_simd + .tail: + and hsize_q, mmsize-1 + jz .ret + TAIL_MASK q + TAIL_MASK_ALPHA q, mmsize + vmovdqu8 alp{k3}{z}, [alpha_q] + vmovdqu8 alp2{k4}{z}, [alpha_q + mmsize] + vpmovwb ymm %+ alp, alp + vpmovwb ymm %+ alp2, alp2 + vinserti64x4 alp, alp, ymm %+ alp2, 1 + vpcmpub k1, threshold, alp, 1 + kandq k1, k1, k2 + vmovdqu8 blend{k2}{z}, [bg_q] + vmovdqu8 blend{k1}, [src_q] + vmovdqu8 [dst_q]{k2}, blend + .ret: +RET + +%define threshold m0 +%define blend m1 +%define alp m2 + +cglobal blit10_threshold_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcastw threshold, threshold_d + mov r6q, hsize_q + and r6q, -(mmsize/2) + lea dst_q, [dst_q + 2*r6q] + lea bg_q, [bg_q + 2*r6q] + lea src_q, [src_q + 2*r6q] + lea alpha_q, [alpha_q + 2*r6q] + neg r6q + jz .tail + ALIGN 16 + .loop_simd: + vpcmpuw k1, threshold, [alpha_q + 2*r6q], 1 + movu blend, [bg_q + 2*r6q] + vmovdqu16 blend{k1}, [src_q + 2*r6q] + movu [dst_q + 2*r6q], blend + add r6q, mmsize/2 + jl .loop_simd + .tail: + and hsize_q, mmsize/2-1 + jz .ret + TAIL_MASK d + vmovdqu16 alp{k2}{z}, [alpha_q] + vpcmpuw k1, threshold, alp, 1 + kandd k1, k1, k2 + vmovdqu16 blend{k2}{z}, [bg_q] + vmovdqu16 blend{k1}, [src_q] + vmovdqu16 [dst_q]{k2}, blend + .ret: +RET + +%define threshold m0 +%define blend m1 +%define alp m2 +%define alp2 m3 +%define alp_idx m4 + +cglobal blit10_threshold_hsub2, 6, 8, 5, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcastw threshold, threshold_d + movu alp_idx, [blit10_alpha_permute] + mov r6q, hsize_q + and r6q, -(mmsize/2) + lea dst_q, [dst_q + 2*r6q] + lea bg_q, [bg_q + 2*r6q] + lea src_q, [src_q + 2*r6q] + lea alpha_q, [alpha_q + 4*r6q] + neg r6q + jz .tail + ALIGN 16 + .loop_simd: + movu alp, [alpha_q + 4*r6q] + ; gather the even words of both alpha vectors in one pass + vpermt2w alp, alp_idx, [alpha_q + 4*r6q + mmsize] + vpcmpuw k1, threshold, alp, 1 + movu blend, [bg_q + 2*r6q] + vmovdqu16 blend{k1}, [src_q + 2*r6q] + movu [dst_q + 2*r6q], blend + add r6q, mmsize/2 + jl .loop_simd + .tail: + and hsize_q, mmsize/2-1 + jz .ret + TAIL_MASK d + TAIL_MASK_ALPHA d, mmsize/2 + vmovdqu16 alp{k3}{z}, [alpha_q] + vmovdqu16 alp2{k4}{z}, [alpha_q + mmsize] + vpermt2w alp, alp_idx, alp2 + vpcmpuw k1, threshold, alp, 1 + kandd k1, k1, k2 + vmovdqu16 blend{k2}{z}, [bg_q] + vmovdqu16 blend{k1}, [src_q] + vmovdqu16 [dst_q]{k2}, blend + .ret: +RET diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index d0736c45a..d73d48c6d 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -28,3 +28,17 @@ void upipe_blit10_blend_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_plane void upipe_blit10_blend_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); + +/* The avx512 kernels predicate their tail, so they accept any hsize. */ +void upipe_blit8_threshold_hsub1_avx512(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_threshold_hsub2_avx512(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub1_avx512(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_hsub2_avx512(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index a160b213b..fa50ded76 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -243,6 +243,14 @@ void checkasm_check_ubuf_pic_blit(void) threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx2; threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx2; } +#ifdef AV_CPU_FLAG_AVX512 + if (cpu_flags & AV_CPU_FLAG_AVX512) { + threshold8_hsub1 = upipe_blit8_threshold_hsub1_avx512; + threshold8_hsub2 = upipe_blit8_threshold_hsub2_avx512; + threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx512; + threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx512; + } +#endif #endif check_blend10(blend_hsub1, "blit10_blend_hsub1", 1); From 5edd0cdae95115df1e4016e28e5dad70c535d954 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 04:10:43 +0100 Subject: [PATCH 19/30] ubuf_pic_blit: in place avx512 threshold kernels When the subpicture is blitted in place (bg == dst), the selected source samples are stored under the compare mask and the background is never loaded; a vector with no selected sample is not written at all. These kernels are dispatched when bg == dst. Measured on Zen 4 (EPYC 8324P) and Raptor Cove at a 1920x256 strip with text alpha, they are 29-37% faster than avx2 at every depth and subsampling. Also measured and not kept: the in place version without the skip, an avx2 variant selecting with one saturating add or subtract into vpblendvb (up to 16% slower on Raptor Cove at 517 samples, parity at frame size), and an avx512icl vpermt2b alpha gather for 8 bit hsub2 (3-5% at 517 samples, nothing at frame size). The fused avx512 hsub1 kernels are 7-10% slower than avx2 at frame size on Zen 4. Two candidates, checkasm-only, separate the two suspects: _blendm selects with a register blend after plain loads, _ymm is the current kernel at 256 bits. The threshold checks now cover both sides of 128 and the extremes, and a frame sized benchmark (1920 x 256, text-like alpha, aligned planes) runs every kernel at a threshold below and above the middle. --- lib/upipe/ubuf_pic_blit_x86.h | 20 +- lib/upipe/x86/ubuf_pic_blit.asm | 192 +++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 29 +++ tests/checkasm/ubuf_pic_blit.c | 317 ++++++++++++++++++++++++++++++-- 4 files changed, 543 insertions(+), 15 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index 9c0b1d637..a2a04a6ea 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -40,7 +40,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold != 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { if (__builtin_cpu_supports("avx512bw")) { - upipe_blit8_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit8_threshold_inplace_hsub1_avx512(dst, src, alpha, hsize, threshold); + else + upipe_blit8_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); return true; } if (__builtin_cpu_supports("avx2")) { @@ -52,7 +55,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, if (hsub == 2) { if (__builtin_cpu_supports("avx512bw")) { - upipe_blit8_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit8_threshold_inplace_hsub2_avx512(dst, src, alpha, hsize, threshold); + else + upipe_blit8_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); return true; } if (__builtin_cpu_supports("avx2")) { @@ -121,7 +127,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold != 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { if (__builtin_cpu_supports("avx512bw")) { - upipe_blit10_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit10_threshold_inplace_hsub1_avx512(dst, src, alpha, hsize, threshold); + else + upipe_blit10_threshold_hsub1_avx512(dst, bg, src, alpha, hsize, threshold); return true; } if (__builtin_cpu_supports("avx2")) { @@ -133,7 +142,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, if (hsub == 2) { if (__builtin_cpu_supports("avx512bw")) { - upipe_blit10_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit10_threshold_inplace_hsub2_avx512(dst, src, alpha, hsize, threshold); + else + upipe_blit10_threshold_hsub2_avx512(dst, bg, src, alpha, hsize, threshold); return true; } if (__builtin_cpu_supports("avx2")) { diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index d821e571e..81e6853b0 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -624,3 +624,195 @@ cglobal blit10_threshold_hsub2, 6, 8, 5, dst_, bg_, src_, alpha_, hsize_, thresh vmovdqu16 [dst_q]{k2}, blend .ret: RET + +; In place, bg == dst: the selected source samples are stored under the compare +; mask and the others are not touched, so the background is never loaded, and a +; vector with no selected sample is neither loaded nor stored, which leaves the +; cache lines of transparent areas clean. + +%macro INPLACE_STORE 3 ; mask width q or d, sample bits 8 or 16, offset + kortest%1 k1, k1 + jz .next + movu m1, [src_q + %3] + vmovdqu%2 [dst_q + %3]{k1}, m1 +%endmacro + +cglobal blit8_threshold_inplace_hsub1, 5, 8, 2, dst_, src_, alpha_, hsize_, threshold_ + vpbroadcastb m0, threshold_d + mov r5q, hsize_q + and r5q, -mmsize + add dst_q, r5q + add src_q, r5q + add alpha_q, r5q + neg r5q + jz .tail + ALIGN 16 + .loop_simd: + vpcmpub k1, m0, [alpha_q + r5q], 1 ; threshold < alpha + INPLACE_STORE q, 8, r5q + .next: + add r5q, mmsize + jl .loop_simd + .tail: + and hsize_q, mmsize-1 + jz .ret + TAIL_MASK q + vmovdqu8 m1{k2}{z}, [alpha_q] + vpcmpub k1, m0, m1, 1 + kandq k1, k1, k2 + vmovdqu8 m1{k1}{z}, [src_q] + vmovdqu8 [dst_q]{k1}, m1 + .ret: +RET + +cglobal blit8_threshold_inplace_hsub2, 5, 8, 3, dst_, src_, alpha_, hsize_, threshold_ + vpbroadcastb m0, threshold_d + mov r5q, hsize_q + and r5q, -mmsize + add dst_q, r5q + add src_q, r5q + lea alpha_q, [alpha_q + 2*r5q] + neg r5q + jz .tail + ALIGN 16 + .loop_simd: + movu m1, [alpha_q + 2*r5q] + movu m2, [alpha_q + 2*r5q + mmsize] + vpmovwb ym1, m1 + vpmovwb ym2, m2 + vinserti64x4 m1, m1, ym2, 1 + vpcmpub k1, m0, m1, 1 + INPLACE_STORE q, 8, r5q + .next: + add r5q, mmsize + jl .loop_simd + .tail: + and hsize_q, mmsize-1 + jz .ret + TAIL_MASK q + TAIL_MASK_ALPHA q, mmsize + vmovdqu8 m1{k3}{z}, [alpha_q] + vmovdqu8 m2{k4}{z}, [alpha_q + mmsize] + vpmovwb ym1, m1 + vpmovwb ym2, m2 + vinserti64x4 m1, m1, ym2, 1 + vpcmpub k1, m0, m1, 1 + kandq k1, k1, k2 + vmovdqu8 m1{k1}{z}, [src_q] + vmovdqu8 [dst_q]{k1}, m1 + .ret: +RET + +cglobal blit10_threshold_inplace_hsub1, 5, 8, 2, dst_, src_, alpha_, hsize_, threshold_ + vpbroadcastw m0, threshold_d + mov r5q, hsize_q + and r5q, -(mmsize/2) + lea dst_q, [dst_q + 2*r5q] + lea src_q, [src_q + 2*r5q] + lea alpha_q, [alpha_q + 2*r5q] + neg r5q + jz .tail + ALIGN 16 + .loop_simd: + vpcmpuw k1, m0, [alpha_q + 2*r5q], 1 + INPLACE_STORE d, 16, 2*r5q + .next: + add r5q, mmsize/2 + jl .loop_simd + .tail: + and hsize_q, mmsize/2-1 + jz .ret + TAIL_MASK d + vmovdqu16 m1{k2}{z}, [alpha_q] + vpcmpuw k1, m0, m1, 1 + kandd k1, k1, k2 + vmovdqu16 m1{k1}{z}, [src_q] + vmovdqu16 [dst_q]{k1}, m1 + .ret: +RET + +cglobal blit10_threshold_inplace_hsub2, 5, 8, 4, dst_, src_, alpha_, hsize_, threshold_ + vpbroadcastw m0, threshold_d + movu m3, [blit10_alpha_permute] + mov r5q, hsize_q + and r5q, -(mmsize/2) + lea dst_q, [dst_q + 2*r5q] + lea src_q, [src_q + 2*r5q] + lea alpha_q, [alpha_q + 4*r5q] + neg r5q + jz .tail + ALIGN 16 + .loop_simd: + movu m1, [alpha_q + 4*r5q] + vpermt2w m1, m3, [alpha_q + 4*r5q + mmsize] + vpcmpuw k1, m0, m1, 1 + INPLACE_STORE d, 16, 2*r5q + .next: + add r5q, mmsize/2 + jl .loop_simd + .tail: + and hsize_q, mmsize/2-1 + jz .ret + TAIL_MASK d + TAIL_MASK_ALPHA d, mmsize/2 + vmovdqu16 m1{k3}{z}, [alpha_q] + vmovdqu16 m2{k4}{z}, [alpha_q + mmsize] + vpermt2w m1, m3, m2 + vpcmpuw k1, m0, m1, 1 + kandd k1, k1, k2 + vmovdqu16 m1{k1}{z}, [src_q] + vmovdqu16 [dst_q]{k1}, m1 + .ret: +RET + + +; Candidates for the fused hsub1 kernels, which lose to avx2 once the planes +; overflow the cache: _blendm selects with a register blend after plain loads, +; no masked load in the loop; _ymm is the current kernel at 256 bits. + +; %1 8 or 10, %2 blendm or ymm, %3 mask width, %4 b or w, %5 bytes per sample, +; %6 bits of the vmovdqu element +%macro THRESHOLD_HSUB1_FUSED 6 +cglobal blit%1_threshold_%2_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcast%4 m0, threshold_d + mov r6q, hsize_q + and r6q, -(mmsize/%5) + lea dst_q, [dst_q + %5*r6q] + lea bg_q, [bg_q + %5*r6q] + lea src_q, [src_q + %5*r6q] + lea alpha_q, [alpha_q + %5*r6q] + neg r6q + jz .tail + ALIGN 16 + .loop_simd: + vpcmpu%4 k1, m0, [alpha_q + %5*r6q], 1 + movu m1, [bg_q + %5*r6q] +%ifidn %2, blendm + movu m2, [src_q + %5*r6q] + vpblendm%4 m1{k1}, m1, m2 +%else + vmovdqu%6 m1{k1}, [src_q + %5*r6q] +%endif + movu [dst_q + %5*r6q], m1 + add r6q, mmsize/%5 + jl .loop_simd + .tail: + and hsize_q, mmsize/%5-1 + jz .ret + TAIL_MASK %3 + vmovdqu%6 m2{k2}{z}, [alpha_q] + vpcmpu%4 k1, m0, m2, 1 + kand%3 k1, k1, k2 + vmovdqu%6 m1{k2}{z}, [bg_q] + vmovdqu%6 m1{k1}, [src_q] + vmovdqu%6 [dst_q]{k2}, m1 + .ret: +RET +%endmacro + +THRESHOLD_HSUB1_FUSED 8, blendm, q, b, 1, 8 +THRESHOLD_HSUB1_FUSED 10, blendm, d, w, 2, 16 + +INIT_YMM avx512 +THRESHOLD_HSUB1_FUSED 8, ymm, d, b, 1, 8 +THRESHOLD_HSUB1_FUSED 10, ymm, w, w, 2, 16 diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index d73d48c6d..9a1e90b86 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -42,3 +42,32 @@ void upipe_blit10_threshold_hsub1_avx512(uint16_t *dst_plane, const uint16_t *bg void upipe_blit10_threshold_hsub2_avx512(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); + +/* In place: dst = alpha > threshold ? src : dst, leaving a vector with no + * selected sample unwritten. */ +void upipe_blit8_threshold_inplace_hsub1_avx512(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit8_threshold_inplace_hsub2_avx512(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit10_threshold_inplace_hsub1_avx512(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit10_threshold_inplace_hsub2_avx512(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, + int threshold); + +/* Candidates for the avx512 hsub1 threshold kernels, same contract. */ +void upipe_blit8_threshold_blendm_hsub1_avx512(uint8_t *dst_plane, + const uint8_t *bg_plane, const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_blendm_hsub1_avx512(uint16_t *dst_plane, + const uint16_t *bg_plane, const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit8_threshold_ymm_hsub1_avx512(uint8_t *dst_plane, + const uint8_t *bg_plane, const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize, int threshold); +void upipe_blit10_threshold_ymm_hsub1_avx512(uint16_t *dst_plane, + const uint16_t *bg_plane, const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize, int threshold); diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index fa50ded76..a8e69a697 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -18,6 +18,8 @@ #include #include +#include +#include #include #include "checkasm.h" @@ -162,6 +164,13 @@ THRESHOLD_REF(blit10_threshold_hsub2_ref, uint16_t, 2) * the dispatcher passes on. */ #define THRESHOLD_MAX 562 +/* Neither 0 nor the maximum, which the dispatcher sends elsewhere: the + * extremes, a random one in each half and the two around the signed byte + * boundary. */ +#define THRESHOLDS(max) { \ + 1, 1 + rnd() % ((max) / 2), 127, 128, \ + (max) / 2 + 1 + rnd() % ((max) / 2 - 1), (max) - 1 } + #define CHECK_THRESHOLD(bits, type, max, min_hsize) \ static void check_threshold##bits(void (*func)(type *, const type *, \ const type *, const type *, \ @@ -189,24 +198,219 @@ static void check_threshold##bits(void (*func)(type *, const type *, \ } \ for (size_t i = 0; i < THRESHOLD_MAX * 2; i++) \ alpha[i] = rnd() & (max); \ - /* neither 0 nor the maximum, which the dispatcher sends elsewhere */ \ - int threshold = 1 + rnd() % ((max) - 1); \ + const int thresholds[] = THRESHOLDS(max); \ \ - for (size_t w = 0; w < sizeof(widths) / sizeof(widths[0]); w++) { \ - memset(dst0, 0xa5, sizeof dst0); \ - memset(dst1, 0xa5, sizeof dst1); \ - call_ref(dst0, bg, src, alpha, widths[w], threshold); \ - call_new(dst1, bg, src, alpha, widths[w], threshold); \ - if (memcmp(dst0, dst1, sizeof dst0)) \ - fail(); \ - } \ + for (size_t t = 0; t < sizeof(thresholds) / sizeof(thresholds[0]); t++) \ + for (size_t w = 0; w < sizeof(widths) / sizeof(widths[0]); w++) { \ + memset(dst0, 0xa5, sizeof dst0); \ + memset(dst1, 0xa5, sizeof dst1); \ + call_ref(dst0, bg, src, alpha, widths[w], thresholds[t]); \ + call_new(dst1, bg, src, alpha, widths[w], thresholds[t]); \ + if (memcmp(dst0, dst1, sizeof dst0)) \ + fail(); \ + } \ \ - bench_new(dst1, bg, src, alpha, NUM_SAMPLES, threshold); \ + bench_new(dst1, bg, src, alpha, NUM_SAMPLES, thresholds[1]); \ } CHECK_THRESHOLD(8, uint8_t, 0xff, 32) CHECK_THRESHOLD(10, uint16_t, 0x3ff, 16) +/* In place, the background being the destination. */ +#define INPLACE_REF(name, type, hsub) \ +static void name(type *dst, const type *src, const type *alpha, \ + size_t hsize, int threshold) \ +{ \ + for (size_t j = 0; j < hsize; j++) \ + if (alpha[j * (hsub)] > threshold) \ + dst[j] = src[j]; \ +} + +INPLACE_REF(blit8_inplace_hsub1_ref, uint8_t, 1) +INPLACE_REF(blit8_inplace_hsub2_ref, uint8_t, 2) +INPLACE_REF(blit10_inplace_hsub1_ref, uint16_t, 1) +INPLACE_REF(blit10_inplace_hsub2_ref, uint16_t, 2) + +#define CHECK_INPLACE(bits, type, max, min_hsize) \ +static void check_inplace##bits(void (*func)(type *, const type *, \ + const type *, size_t, int), \ + const char *name) \ +{ \ + static const size_t widths[] = { 512, NUM_SAMPLES, THRESHOLD_MAX, \ + min_hsize }; \ + \ + if (!check_func(func, "%s", name)) \ + return; \ + \ + type bg[THRESHOLD_MAX + GUARD]; \ + type src[THRESHOLD_MAX]; \ + type alpha[THRESHOLD_MAX * 2]; \ + type dst0[THRESHOLD_MAX + GUARD]; \ + type dst1[THRESHOLD_MAX + GUARD]; \ + \ + declare_func(void, type *dst, const type *src, const type *alpha, \ + size_t hsize, int threshold); \ + \ + memset(bg, 0xa5, sizeof bg); \ + for (size_t i = 0; i < THRESHOLD_MAX; i++) { \ + bg[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + for (size_t i = 0; i < THRESHOLD_MAX * 2; i++) \ + alpha[i] = rnd() & (max); \ + const int thresholds[] = THRESHOLDS(max); \ + \ + for (size_t t = 0; t < sizeof(thresholds) / sizeof(thresholds[0]); t++) \ + for (size_t w = 0; w < sizeof(widths) / sizeof(widths[0]); w++) { \ + memcpy(dst0, bg, sizeof dst0); \ + memcpy(dst1, bg, sizeof dst1); \ + call_ref(dst0, src, alpha, widths[w], thresholds[t]); \ + call_new(dst1, src, alpha, widths[w], thresholds[t]); \ + if (memcmp(dst0, dst1, sizeof dst0)) \ + fail(); \ + } \ + \ + bench_new(dst1, src, alpha, NUM_SAMPLES, thresholds[1]); \ +} + +CHECK_INPLACE(8, uint8_t, 0xff, 32) +CHECK_INPLACE(10, uint16_t, 0x3ff, 16) + +/* A subtitle strip a frame wide, whose planes overflow the L2 cache, and + * alpha as text gives it: half of the 64 sample blocks transparent, the + * others runs of transparent and opaque samples. The buffers are 64 byte + * aligned, as the picture planes are. Checked at a threshold on each side + * of the middle, timed at the low one; C is checked but not timed. */ +#define FRAME_SAMPLES (1920 * 256) +#define FRAME_THRESHOLD(max) ((max) / 13) + +#define TEXT_ALPHA(bits, type, max) \ +static void text_alpha##bits(type *alpha, size_t n) \ +{ \ + for (size_t b = 0; b < n; b += 64) { \ + size_t end = b + 64 < n ? b + 64 : n; \ + if (rnd() & 1) { \ + for (size_t i = b; i < end; i++) \ + alpha[i] = 0; \ + continue; \ + } \ + type a = 0; \ + for (size_t i = b; i < end; ) { \ + size_t run = 2 + rnd() % 11; \ + for (; run && i < end; run--, i++) \ + alpha[i] = a; \ + a = a ? 0 : (max); \ + } \ + } \ +} + +TEXT_ALPHA(8, uint8_t, 0xff) +TEXT_ALPHA(10, uint16_t, 0x3ff) + +#define CHECK_FRAME(bits, type, max) \ +static void check_frame##bits(void (*func)(type *, const type *, \ + const type *, const type *, \ + size_t, int), \ + void (*ref)(type *, const type *, \ + const type *, const type *, \ + size_t, int), \ + const char *name, uint8_t hsub) \ +{ \ + if (!check_func(func, "%s_frame", name)) \ + return; \ + \ + const size_t n = FRAME_SAMPLES; \ + const int threshold = FRAME_THRESHOLD(max); \ + type *bg = aligned_alloc(64, n * sizeof(type)); \ + type *src = aligned_alloc(64, n * sizeof(type)); \ + type *alpha = aligned_alloc(64, 2 * n * sizeof(type)); \ + type *dst0 = aligned_alloc(64, n * sizeof(type)); \ + type *dst1 = aligned_alloc(64, n * sizeof(type)); \ + \ + declare_func(void, type *dst, const type *bg, const type *src, \ + const type *alpha, size_t hsize, int threshold); \ + \ + if (!bg || !src || !alpha || !dst0 || !dst1) { \ + fail(); \ + } else { \ + for (size_t i = 0; i < n; i++) { \ + bg[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + text_alpha##bits(alpha, n * hsub); \ + \ + call_ref(dst0, bg, src, alpha, n, (max) - threshold); \ + call_new(dst1, bg, src, alpha, n, (max) - threshold); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + call_ref(dst0, bg, src, alpha, n, threshold); \ + call_new(dst1, bg, src, alpha, n, threshold); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + \ + if (func != ref) \ + bench_new(dst1, bg, src, alpha, n, threshold); \ + } \ + \ + free(bg); \ + free(src); \ + free(alpha); \ + free(dst0); \ + free(dst1); \ +} \ + \ +static void check_frame_inplace##bits(void (*func)(type *, const type *, \ + const type *, size_t, \ + int), \ + void (*ref)(type *, const type *, \ + const type *, size_t, \ + int), \ + const char *name, uint8_t hsub) \ +{ \ + if (!check_func(func, "%s_frame", name)) \ + return; \ + \ + const size_t n = FRAME_SAMPLES; \ + const int threshold = FRAME_THRESHOLD(max); \ + type *src = aligned_alloc(64, n * sizeof(type)); \ + type *alpha = aligned_alloc(64, 2 * n * sizeof(type)); \ + type *dst0 = aligned_alloc(64, n * sizeof(type)); \ + type *dst1 = aligned_alloc(64, n * sizeof(type)); \ + \ + declare_func(void, type *dst, const type *src, const type *alpha, \ + size_t hsize, int threshold); \ + \ + if (!src || !alpha || !dst0 || !dst1) { \ + fail(); \ + } else { \ + for (size_t i = 0; i < n; i++) { \ + dst0[i] = dst1[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + text_alpha##bits(alpha, n * hsub); \ + \ + call_ref(dst0, src, alpha, n, (max) - threshold); \ + call_new(dst1, src, alpha, n, (max) - threshold); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + call_ref(dst0, src, alpha, n, threshold); \ + call_new(dst1, src, alpha, n, threshold); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + \ + if (func != ref) \ + bench_new(dst1, src, alpha, n, threshold); \ + } \ + \ + free(src); \ + free(alpha); \ + free(dst0); \ + free(dst1); \ +} + +CHECK_FRAME(8, uint8_t, 0xff) +CHECK_FRAME(10, uint16_t, 0x3ff) + void checkasm_check_ubuf_pic_blit(void) { void (*threshold8_hsub1)(uint8_t *, const uint8_t *, const uint8_t *, @@ -229,6 +433,20 @@ void checkasm_check_ubuf_pic_blit(void) const uint8_t *, size_t) = blit8_blend_hsub1_ref; void (*blend8_hsub2)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, size_t) = blit8_blend_hsub2_ref; + /* [hsub - 1] */ + void (*inplace8[2])(uint8_t *, const uint8_t *, const uint8_t *, size_t, + int) = { + blit8_inplace_hsub1_ref, blit8_inplace_hsub2_ref }; + void (*inplace10[2])(uint16_t *, const uint16_t *, const uint16_t *, + size_t, int) = { + blit10_inplace_hsub1_ref, blit10_inplace_hsub2_ref }; + /* candidates for the avx512 hsub1 kernels: blendm, ymm */ + void (*candidate8[2])(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t, int) = { + blit8_threshold_hsub1_ref, blit8_threshold_hsub1_ref }; + void (*candidate10[2])(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = { + blit10_threshold_hsub1_ref, blit10_threshold_hsub1_ref }; #ifdef HAVE_X86ASM int cpu_flags = av_get_cpu_flags(); @@ -249,6 +467,14 @@ void checkasm_check_ubuf_pic_blit(void) threshold8_hsub2 = upipe_blit8_threshold_hsub2_avx512; threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx512; threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx512; + inplace8[0] = upipe_blit8_threshold_inplace_hsub1_avx512; + inplace8[1] = upipe_blit8_threshold_inplace_hsub2_avx512; + inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx512; + inplace10[1] = upipe_blit10_threshold_inplace_hsub2_avx512; + candidate8[0] = upipe_blit8_threshold_blendm_hsub1_avx512; + candidate8[1] = upipe_blit8_threshold_ymm_hsub1_avx512; + candidate10[0] = upipe_blit10_threshold_blendm_hsub1_avx512; + candidate10[1] = upipe_blit10_threshold_ymm_hsub1_avx512; } #endif #endif @@ -276,4 +502,73 @@ void checkasm_check_ubuf_pic_blit(void) check_threshold10(threshold10_hsub2, "blit10_threshold_hsub2"); report("blit10_threshold_hsub2"); + + static const char *const candidate_names[2] = { "blendm", "ymm" }; + char name[64]; + + for (int h = 0; h < 2; h++) { + snprintf(name, sizeof name, "blit8_threshold_inplace_hsub%d", h + 1); + check_inplace8(inplace8[h], name); + report("%s", name); + snprintf(name, sizeof name, "blit10_threshold_inplace_hsub%d", h + 1); + check_inplace10(inplace10[h], name); + report("%s", name); + } + + for (int c = 0; c < 2; c++) { + snprintf(name, sizeof name, "blit8_threshold_%s_hsub1", + candidate_names[c]); + check_threshold8(candidate8[c], name); + report("%s", name); + snprintf(name, sizeof name, "blit10_threshold_%s_hsub1", + candidate_names[c]); + check_threshold10(candidate10[c], name); + report("%s", name); + } + + /* frame sized, text alpha */ + void (*const ref8[2])(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t, int) = { + blit8_threshold_hsub1_ref, blit8_threshold_hsub2_ref }; + void (*const ref10[2])(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = { + blit10_threshold_hsub1_ref, blit10_threshold_hsub2_ref }; + void (*const ref_inplace8[2])(uint8_t *, const uint8_t *, const uint8_t *, + size_t, int) = { + blit8_inplace_hsub1_ref, blit8_inplace_hsub2_ref }; + void (*const ref_inplace10[2])(uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = { + blit10_inplace_hsub1_ref, blit10_inplace_hsub2_ref }; + void (*const fused8[2])(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t, int) = { + threshold8_hsub1, threshold8_hsub2 }; + void (*const fused10[2])(uint16_t *, const uint16_t *, const uint16_t *, + const uint16_t *, size_t, int) = { + threshold10_hsub1, threshold10_hsub2 }; + + for (int h = 0; h < 2; h++) { + snprintf(name, sizeof name, "blit8_threshold_hsub%d", h + 1); + check_frame8(fused8[h], ref8[h], name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit10_threshold_hsub%d", h + 1); + check_frame10(fused10[h], ref10[h], name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit8_threshold_inplace_hsub%d", h + 1); + check_frame_inplace8(inplace8[h], ref_inplace8[h], name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit10_threshold_inplace_hsub%d", h + 1); + check_frame_inplace10(inplace10[h], ref_inplace10[h], name, h + 1); + report("%s_frame", name); + } + + for (int c = 0; c < 2; c++) { + snprintf(name, sizeof name, "blit8_threshold_%s_hsub1", + candidate_names[c]); + check_frame8(candidate8[c], ref8[0], name, 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit10_threshold_%s_hsub1", + candidate_names[c]); + check_frame10(candidate10[c], ref10[0], name, 1); + report("%s_frame", name); + } } From ffdcdc1445b801fc36ce436fd6522bd9555b8441 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 04:17:19 +0100 Subject: [PATCH 20/30] ubuf_pic_blit: fused avx512 hsub1 threshold kernels at 256 bits On Zen 4 with planes that overflow the cache (1920x256, text alpha) the zmm fused hsub1 kernels lose 8% to avx2, with a masked load or with a register blend after plain loads alike; at 256 bits they are level with avx2 (8 bit +0.7%, 10 bit -1.2%) and still 1.3-1.7x faster from L1. The hsub2 and in place kernels stay at 512 bits. The candidates go. --- lib/upipe/x86/ubuf_pic_blit.asm | 136 +++++++------------------------- lib/upipe/x86/ubuf_pic_blit.h | 17 +--- tests/checkasm/ubuf_pic_blit.c | 34 -------- 3 files changed, 31 insertions(+), 156 deletions(-) diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 81e6853b0..aba96b7f4 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -464,41 +464,6 @@ RET INIT_ZMM avx512 -%define threshold m0 -%define blend m1 -%define alp m2 - -cglobal blit8_threshold_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ - vpbroadcastb threshold, threshold_d - mov r6q, hsize_q - and r6q, -mmsize ; samples handled by the vector loop - add dst_q, r6q - add bg_q, r6q - add src_q, r6q - add alpha_q, r6q - neg r6q ; count the index up to zero - jz .tail - ALIGN 16 - .loop_simd: - vpcmpub k1, threshold, [alpha_q + r6q], 1 ; threshold < alpha - movu blend, [bg_q + r6q] - vmovdqu8 blend{k1}, [src_q + r6q] - movu [dst_q + r6q], blend - add r6q, mmsize - jl .loop_simd - .tail: - and hsize_q, mmsize-1 - jz .ret - TAIL_MASK q - vmovdqu8 alp{k2}{z}, [alpha_q] - vpcmpub k1, threshold, alp, 1 - kandq k1, k1, k2 ; a zero threshold would otherwise select samples past the row - vmovdqu8 blend{k2}{z}, [bg_q] - vmovdqu8 blend{k1}, [src_q] - vmovdqu8 [dst_q]{k2}, blend - .ret: -RET - %define threshold m0 %define blend m1 %define alp m2 @@ -546,41 +511,6 @@ cglobal blit8_threshold_hsub2, 6, 8, 4, dst_, bg_, src_, alpha_, hsize_, thresho .ret: RET -%define threshold m0 -%define blend m1 -%define alp m2 - -cglobal blit10_threshold_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ - vpbroadcastw threshold, threshold_d - mov r6q, hsize_q - and r6q, -(mmsize/2) - lea dst_q, [dst_q + 2*r6q] - lea bg_q, [bg_q + 2*r6q] - lea src_q, [src_q + 2*r6q] - lea alpha_q, [alpha_q + 2*r6q] - neg r6q - jz .tail - ALIGN 16 - .loop_simd: - vpcmpuw k1, threshold, [alpha_q + 2*r6q], 1 - movu blend, [bg_q + 2*r6q] - vmovdqu16 blend{k1}, [src_q + 2*r6q] - movu [dst_q + 2*r6q], blend - add r6q, mmsize/2 - jl .loop_simd - .tail: - and hsize_q, mmsize/2-1 - jz .ret - TAIL_MASK d - vmovdqu16 alp{k2}{z}, [alpha_q] - vpcmpuw k1, threshold, alp, 1 - kandd k1, k1, k2 - vmovdqu16 blend{k2}{z}, [bg_q] - vmovdqu16 blend{k1}, [src_q] - vmovdqu16 [dst_q]{k2}, blend - .ret: -RET - %define threshold m0 %define blend m1 %define alp m2 @@ -766,53 +696,45 @@ cglobal blit10_threshold_inplace_hsub2, 5, 8, 4, dst_, src_, alpha_, hsize_, thr RET -; Candidates for the fused hsub1 kernels, which lose to avx2 once the planes -; overflow the cache: _blendm selects with a register blend after plain loads, -; no masked load in the loop; _ymm is the current kernel at 256 bits. - -; %1 8 or 10, %2 blendm or ymm, %3 mask width, %4 b or w, %5 bytes per sample, -; %6 bits of the vmovdqu element -%macro THRESHOLD_HSUB1_FUSED 6 -cglobal blit%1_threshold_%2_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ - vpbroadcast%4 m0, threshold_d +; The fused hsub1 kernels at 256 bits: once the planes overflow the cache, zmm +; loses about 8% to avx2 on Zen 4 (masked load or register blend alike) while +; ymm stays level with it, and is still faster from L1. +; +; %1 8 or 10, %2 mask width, %3 b or w, %4 bytes per sample, %5 bits of the +; vmovdqu element +%macro THRESHOLD_HSUB1_FUSED 5 +cglobal blit%1_threshold_hsub1, 6, 8, 3, dst_, bg_, src_, alpha_, hsize_, threshold_ + vpbroadcast%3 m0, threshold_d mov r6q, hsize_q - and r6q, -(mmsize/%5) - lea dst_q, [dst_q + %5*r6q] - lea bg_q, [bg_q + %5*r6q] - lea src_q, [src_q + %5*r6q] - lea alpha_q, [alpha_q + %5*r6q] + and r6q, -(mmsize/%4) + lea dst_q, [dst_q + %4*r6q] + lea bg_q, [bg_q + %4*r6q] + lea src_q, [src_q + %4*r6q] + lea alpha_q, [alpha_q + %4*r6q] neg r6q jz .tail ALIGN 16 .loop_simd: - vpcmpu%4 k1, m0, [alpha_q + %5*r6q], 1 - movu m1, [bg_q + %5*r6q] -%ifidn %2, blendm - movu m2, [src_q + %5*r6q] - vpblendm%4 m1{k1}, m1, m2 -%else - vmovdqu%6 m1{k1}, [src_q + %5*r6q] -%endif - movu [dst_q + %5*r6q], m1 - add r6q, mmsize/%5 + vpcmpu%3 k1, m0, [alpha_q + %4*r6q], 1 + movu m1, [bg_q + %4*r6q] + vmovdqu%5 m1{k1}, [src_q + %4*r6q] + movu [dst_q + %4*r6q], m1 + add r6q, mmsize/%4 jl .loop_simd .tail: - and hsize_q, mmsize/%5-1 + and hsize_q, mmsize/%4-1 jz .ret - TAIL_MASK %3 - vmovdqu%6 m2{k2}{z}, [alpha_q] - vpcmpu%4 k1, m0, m2, 1 - kand%3 k1, k1, k2 - vmovdqu%6 m1{k2}{z}, [bg_q] - vmovdqu%6 m1{k1}, [src_q] - vmovdqu%6 [dst_q]{k2}, m1 + TAIL_MASK %2 + vmovdqu%5 m2{k2}{z}, [alpha_q] + vpcmpu%3 k1, m0, m2, 1 + kand%2 k1, k1, k2 ; a zero threshold would otherwise select samples past the row + vmovdqu%5 m1{k2}{z}, [bg_q] + vmovdqu%5 m1{k1}, [src_q] + vmovdqu%5 [dst_q]{k2}, m1 .ret: RET %endmacro -THRESHOLD_HSUB1_FUSED 8, blendm, q, b, 1, 8 -THRESHOLD_HSUB1_FUSED 10, blendm, d, w, 2, 16 - INIT_YMM avx512 -THRESHOLD_HSUB1_FUSED 8, ymm, d, b, 1, 8 -THRESHOLD_HSUB1_FUSED 10, ymm, w, w, 2, 16 +THRESHOLD_HSUB1_FUSED 8, d, b, 1, 8 +THRESHOLD_HSUB1_FUSED 10, w, w, 2, 16 diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 9a1e90b86..9684e0e5f 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -29,7 +29,8 @@ void upipe_blit10_blend_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); -/* The avx512 kernels predicate their tail, so they accept any hsize. */ +/* The avx512 kernels predicate their tail, so they accept any hsize. The hsub1 + * ones work in 256 bits. */ void upipe_blit8_threshold_hsub1_avx512(uint8_t *dst_plane, const uint8_t *bg_plane, const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, int threshold); @@ -57,17 +58,3 @@ void upipe_blit10_threshold_inplace_hsub1_avx512(uint16_t *dst_plane, void upipe_blit10_threshold_inplace_hsub2_avx512(uint16_t *dst_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); - -/* Candidates for the avx512 hsub1 threshold kernels, same contract. */ -void upipe_blit8_threshold_blendm_hsub1_avx512(uint8_t *dst_plane, - const uint8_t *bg_plane, const uint8_t *src_plane, - const uint8_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit10_threshold_blendm_hsub1_avx512(uint16_t *dst_plane, - const uint16_t *bg_plane, const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit8_threshold_ymm_hsub1_avx512(uint8_t *dst_plane, - const uint8_t *bg_plane, const uint8_t *src_plane, - const uint8_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit10_threshold_ymm_hsub1_avx512(uint16_t *dst_plane, - const uint16_t *bg_plane, const uint16_t *src_plane, - const uint16_t *alpha_plane, size_t hsize, int threshold); diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index a8e69a697..d2fd0758d 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -440,13 +440,6 @@ void checkasm_check_ubuf_pic_blit(void) void (*inplace10[2])(uint16_t *, const uint16_t *, const uint16_t *, size_t, int) = { blit10_inplace_hsub1_ref, blit10_inplace_hsub2_ref }; - /* candidates for the avx512 hsub1 kernels: blendm, ymm */ - void (*candidate8[2])(uint8_t *, const uint8_t *, const uint8_t *, - const uint8_t *, size_t, int) = { - blit8_threshold_hsub1_ref, blit8_threshold_hsub1_ref }; - void (*candidate10[2])(uint16_t *, const uint16_t *, const uint16_t *, - const uint16_t *, size_t, int) = { - blit10_threshold_hsub1_ref, blit10_threshold_hsub1_ref }; #ifdef HAVE_X86ASM int cpu_flags = av_get_cpu_flags(); @@ -471,10 +464,6 @@ void checkasm_check_ubuf_pic_blit(void) inplace8[1] = upipe_blit8_threshold_inplace_hsub2_avx512; inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx512; inplace10[1] = upipe_blit10_threshold_inplace_hsub2_avx512; - candidate8[0] = upipe_blit8_threshold_blendm_hsub1_avx512; - candidate8[1] = upipe_blit8_threshold_ymm_hsub1_avx512; - candidate10[0] = upipe_blit10_threshold_blendm_hsub1_avx512; - candidate10[1] = upipe_blit10_threshold_ymm_hsub1_avx512; } #endif #endif @@ -503,7 +492,6 @@ void checkasm_check_ubuf_pic_blit(void) check_threshold10(threshold10_hsub2, "blit10_threshold_hsub2"); report("blit10_threshold_hsub2"); - static const char *const candidate_names[2] = { "blendm", "ymm" }; char name[64]; for (int h = 0; h < 2; h++) { @@ -515,17 +503,6 @@ void checkasm_check_ubuf_pic_blit(void) report("%s", name); } - for (int c = 0; c < 2; c++) { - snprintf(name, sizeof name, "blit8_threshold_%s_hsub1", - candidate_names[c]); - check_threshold8(candidate8[c], name); - report("%s", name); - snprintf(name, sizeof name, "blit10_threshold_%s_hsub1", - candidate_names[c]); - check_threshold10(candidate10[c], name); - report("%s", name); - } - /* frame sized, text alpha */ void (*const ref8[2])(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, size_t, int) = { @@ -560,15 +537,4 @@ void checkasm_check_ubuf_pic_blit(void) check_frame_inplace10(inplace10[h], ref_inplace10[h], name, h + 1); report("%s_frame", name); } - - for (int c = 0; c < 2; c++) { - snprintf(name, sizeof name, "blit8_threshold_%s_hsub1", - candidate_names[c]); - check_frame8(candidate8[c], ref8[0], name, 1); - report("%s_frame", name); - snprintf(name, sizeof name, "blit10_threshold_%s_hsub1", - candidate_names[c]); - check_frame10(candidate10[c], ref10[0], name, 1); - report("%s_frame", name); - } } From eaa4fbadc216b83d0ef59bb616f3943e1a5817cd Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 04:56:02 +0100 Subject: [PATCH 21/30] ubuf_pic_blit: in place avx2 threshold and blend kernels that skip, avx512 blend in place In place (bg == dst), a vector with nothing to change need not be loaded or stored, which leaves the cache lines of transparent areas clean; that skip was most of the avx512 threshold kernels' gain. - avx2 threshold in place: a vector with no sample over the threshold is skipped. Dispatched when bg == dst. - avx2 blend in place: a vector whose alpha is all 0 is skipped, one whose alpha is all at the maximum takes the source without the blend (the same samples). Dispatched when bg == dst. DVB and TTML subtitles blend. - avx512 blend in place, the same at 512 bits, checkasm-only until benchmarked against the avx2 one. checkasm checks the blend in place at random and at subtitle-like alpha (transparent, opaque and glyph edge blocks), and benchmarks the blend fused and in place at frame size with that alpha. BLEND8 takes the background base as an argument. --- lib/upipe/ubuf_pic_blit_x86.h | 40 ++- lib/upipe/x86/ubuf_pic_blit.asm | 422 +++++++++++++++++++++++++++++++- lib/upipe/x86/ubuf_pic_blit.h | 32 +++ tests/checkasm/ubuf_pic_blit.c | 257 ++++++++++++++++++- 4 files changed, 738 insertions(+), 13 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index a2a04a6ea..74658ba81 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -47,7 +47,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - upipe_blit8_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit8_threshold_inplace_hsub1_avx2(dst, src, alpha, hsize, threshold); + else + upipe_blit8_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -62,7 +65,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit8_threshold_inplace_hsub2_avx2(dst, src, alpha, hsize, threshold); + else + upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -73,7 +79,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold == 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { if (__builtin_cpu_supports("avx2")) { - upipe_blit8_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + if (dst == bg) + upipe_blit8_blend_inplace_hsub1_avx2(dst, src, alpha, hsize); + else + upipe_blit8_blend_hsub1_avx2(dst, bg, src, alpha, hsize); return true; } return false; @@ -81,7 +90,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, if (hsub == 2) { if (__builtin_cpu_supports("avx2")) { - upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + if (dst == bg) + upipe_blit8_blend_inplace_hsub2_avx2(dst, src, alpha, hsize); + else + upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; } return false; @@ -134,7 +146,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - upipe_blit10_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit10_threshold_inplace_hsub1_avx2(dst, src, alpha, hsize, threshold); + else + upipe_blit10_threshold_hsub1_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -149,7 +164,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + if (dst == bg) + upipe_blit10_threshold_inplace_hsub2_avx2(dst, src, alpha, hsize, threshold); + else + upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -160,7 +178,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold == 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { if (__builtin_cpu_supports("avx2")) { - upipe_blit10_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + if (dst == bg) + upipe_blit10_blend_inplace_hsub1_avx2(dst, src, alpha, hsize); + else + upipe_blit10_blend_hsub1_avx2(dst, bg, src, alpha, hsize); return true; } return false; @@ -168,7 +189,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, if (hsub == 2) { if (__builtin_cpu_supports("avx2")) { - upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + if (dst == bg) + upipe_blit10_blend_inplace_hsub2_avx2(dst, src, alpha, hsize); + else + upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; } return false; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index aba96b7f4..8f7381fbf 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -285,6 +285,141 @@ cglobal blit10_threshold_hsub2, 6, 9, 6, dst_, bg_, src_, alpha_, hsize_, thresh .ret: RET +; In place, bg == dst: a vector with no sample over the threshold is neither +; loaded nor stored, which leaves the cache lines of transparent areas clean. +; The scalar tails read the background from the destination. + +cglobal blit8_threshold_inplace_hsub1, 5, 9, 5, dst_, src_, alpha_, hsize_, threshold_ + %define bg_q dst_q + mov r6d, 0x01010101 * 0x80 + movd xm1, threshold_d + movd xm0, r6d + vpbroadcastb m1, xm1 + vpbroadcastd m0, xm0 + mov r6d, mmsize + pxor m1, m0 ; signed compare, as above + ALIGN 16 + .loop_simd: + pxor m4, m0, [alpha_q + r6q - mmsize] + mov r7q, r6q + pcmpgtb m4, m1 + ptest m4, m4 + jz .next + pand m3, m4, [src_q + r6q - mmsize] + pandn m2, m4, [dst_q + r6q - mmsize] + por m2, m3 + movu [dst_q + r6q - mmsize], m2 + .next: + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8 + .ret: +RET + +cglobal blit8_threshold_inplace_hsub2, 5, 9, 7, dst_, src_, alpha_, hsize_, threshold_ + %define bg_q dst_q + mov r6d, 0x01010101 * 0x80 + movd xm1, threshold_d + VBROADCASTI128 m5, [blit8_alpha_shuffle] + movd xm0, r6d + vpbroadcastb m1, xm1 + vpbroadcastd m0, xm0 + mov r6d, mmsize + pxor m1, m0 + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + mov r7q, r6q + pshufb m4, m5 + pshufb m6, m5 + punpcklqdq m4, m4, m6 + vpermq m4, m4, q3120 + pxor m4, m0 + pcmpgtb m4, m1 + ptest m4, m4 + jz .next + pand m3, m4, [src_q + r6q - mmsize] + pandn m2, m4, [dst_q + r6q - mmsize] + por m2, m3 + movu [dst_q + r6q - mmsize], m2 + .next: + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_B r7, r6, r8, 2 + .ret: +RET + +cglobal blit10_threshold_inplace_hsub1, 5, 9, 4, dst_, src_, alpha_, hsize_, threshold_ + %define bg_q dst_q + movd xm0, threshold_d + mov r6d, mmsize/2 + vpbroadcastw m0, xm0 + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + pcmpgtw m3, m0 + ptest m3, m3 + jz .next + pand m2, m3, [src_q + 2*r6q - mmsize] + pandn m1, m3, [dst_q + 2*r6q - mmsize] + por m1, m2 + movu [dst_q + 2*r6q - mmsize], m1 + .next: + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8 + .ret: +RET + +cglobal blit10_threshold_inplace_hsub2, 5, 9, 6, dst_, src_, alpha_, hsize_, threshold_ + %define bg_q dst_q + movd xm0, threshold_d + VBROADCASTI128 m5, [blit10_alpha_shuffle] + mov r6d, mmsize/2 + vpbroadcastw m0, xm0 + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + 4*r6q - 2*mmsize] + movu m4, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + pshufb m3, m5 + pshufb m4, m5 + punpcklqdq m3, m3, m4 + vpermq m3, m3, q3120 + pcmpgtw m3, m0 + ptest m3, m3 + jz .next + pand m2, m3, [src_q + 2*r6q - mmsize] + pandn m1, m3, [dst_q + 2*r6q - mmsize] + por m1, m2 + movu [dst_q + 2*r6q - mmsize], m1 + .next: + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_W r7, r6, r8, 2 + .ret: +RET + +%undef bg_q + ; The 8 bit blend kernels compute dst = (bg * (0xff - a) + src * a) / 0xff, ; matching the C bit for bit, with one multiply-add per pair of samples. ; @@ -308,14 +443,14 @@ RET pcmpeqb pb_ff, pb_ff %endmacro -; Takes alpha in m3, loads src and bg at r6q, leaves the finished samples in -; m3 and clobbers m4 to m8. -%macro BLEND8 0 +; Takes alpha in m3, loads src and the background (at %1, bg_q by default) at +; r6q, leaves the finished samples in m3 and clobbers m4 to m8. +%macro BLEND8 0-1 bg_q pxor m4, m3, pb_ff ; 0xff - a punpckhbw m5, m3, m4 ; (a, 0xff - a) pairs, high half punpcklbw m3, m4 ; and low half movu m6, [src_q + r6q - mmsize] - movu m7, [bg_q + r6q - mmsize] + movu m7, [%1 + r6q - mmsize] pxor m6, pb_80 ; src - 0x80 pxor m7, pb_80 ; bg - 0x80 punpckhbw m8, m6, m7 ; the matching (src, bg) pairs @@ -376,6 +511,72 @@ cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ .ret: RET +; In place, bg == dst: a vector whose alpha is all 0 keeps the background and is +; neither loaded nor stored, and one whose alpha is all 0xff takes the source +; without the blend, which gives the same samples. + +%macro BLEND8_INPLACE 0 ; alpha in m3 + ptest m3, m3 + jz .next + pcmpeqb m4, m3, pb_ff + pmovmskb r8d, m4 + cmp r8d, -1 + je .opaque + BLEND8 dst_q + movu [dst_q + r6q - mmsize], m3 + jmp .next +.opaque: + movu m4, [src_q + r6q - mmsize] + movu [dst_q + r6q - mmsize], m4 +.next: +%endmacro + +cglobal blit8_blend_inplace_hsub1, 4, 10, 11, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND8_CONSTANTS + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + r6q - mmsize] + mov r7q, r6q + BLEND8_INPLACE + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6 + .ret: +RET + +cglobal blit8_blend_inplace_hsub2, 4, 10, 12, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND8_CONSTANTS + VBROADCASTI128 shuffle, [blit8_alpha_shuffle] + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m5, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + pshufb m5, shuffle + pshufb m6, shuffle + punpcklqdq m3, m5, m6 + vpermq m3, m3, q3120 + mov r7q, r6q + BLEND8_INPLACE + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 + .ret: +RET + +%undef bg_q + %undef pb_80 %undef pw_bias %undef pw_101 @@ -431,6 +632,72 @@ cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ .ret: RET +; In place, as the 8 bit ones: all 0 alpha skipped, all 0x3ff alpha copied. + +%macro BLEND10_INPLACE 0 ; alpha in m4 + ptest m4, m4 + jz .next + pcmpeqw m5, m4, m0 + pmovmskb r8d, m5 + cmp r8d, -1 + je .opaque + movu m2, [dst_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + jmp .next +.opaque: + movu m3, [src_q + 2*r6q - mmsize] + movu [dst_q + 2*r6q - mmsize], m3 +.next: +%endmacro + +cglobal blit10_blend_inplace_hsub1, 4, 11, 8, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND10_CONSTANTS + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND10_INPLACE + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10 + .ret: +RET + +cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND10_CONSTANTS + VBROADCASTI128 m8, [blit10_alpha_shuffle] + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 4*r6q - 2*mmsize] + movu m5, [alpha_q + 4*r6q - 1*mmsize] + mov r7q, r6q + pshufb m4, m8 + pshufb m5, m8 + punpcklqdq m4, m4, m5 + vpermq m4, m4, q3120 + BLEND10_INPLACE + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +RET + +%undef bg_q + ; The avx512 kernels replace the +0x80 bias and the pand/pandn/por select with an ; unsigned compare into a mask register and a merge-masked load, and predicate the ; trailing samples instead of falling back to scalar code. Masked loads suppress @@ -738,3 +1005,150 @@ RET INIT_YMM avx512 THRESHOLD_HSUB1_FUSED 8, d, b, 1, 8 THRESHOLD_HSUB1_FUSED 10, w, w, 2, 16 + +; The blend in place at 512 bits: the same arithmetic, with the all 0 and all +; maximum alpha tests on mask registers. The tail is scalar. + +INIT_ZMM avx512 + +%define pb_80 m0 +%define pw_bias m1 +%define pw_101 m2 +%define pb_ff m10 + +%macro BLEND8_INPLACE_ZMM 0 ; alpha in m3 + vptestmb k1, m3, m3 + kortestq k1, k1 + jz .next + vpcmpeqb k1, m3, pb_ff + kortestq k1, k1 + jc .opaque + BLEND8 dst_q + movu [dst_q + r6q - mmsize], m3 + jmp .next +.opaque: + movu m4, [src_q + r6q - mmsize] + movu [dst_q + r6q - mmsize], m4 +.next: +%endmacro + +%macro BLEND8_CONSTANTS_ZMM 0 + vpbroadcastb pb_80, [blit8_blend_80] + vpbroadcastw pw_bias, [blit8_blend_bias] + vpbroadcastw pw_101, [blit8_blend_101] + vpternlogd pb_ff, pb_ff, pb_ff, 0xff +%endmacro + +cglobal blit8_blend_inplace_hsub1, 4, 10, 11, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND8_CONSTANTS_ZMM + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + r6q - mmsize] + mov r7q, r6q + BLEND8_INPLACE_ZMM + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6 + .ret: +RET + +cglobal blit8_blend_inplace_hsub2, 4, 10, 11, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND8_CONSTANTS_ZMM + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m5, [alpha_q + 2*r6q - 2*mmsize] + movu m6, [alpha_q + 2*r6q - 1*mmsize] + vpmovwb ym3, m5 ; the low byte of every word + vpmovwb ym6, m6 + vinserti64x4 m3, m3, ym6, 1 + mov r7q, r6q + BLEND8_INPLACE_ZMM + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 + .ret: +RET + +%undef pb_80 +%undef pw_bias +%undef pw_101 +%undef pb_ff + +%macro BLEND10_CONSTANTS_ZMM 0 + vpternlogd m0, m0, m0, 0xff + psrlw m0, 6 ; 0x3ff in every word + vpternlogd m1, m1, m1, 0xff + psrld m1, 31 ; 1 in every dword +%endmacro + +%macro BLEND10_INPLACE_ZMM 0 ; alpha in m4 + vptestmw k1, m4, m4 + kortestd k1, k1 + jz .next + vpcmpeqw k1, m4, m0 + kortestd k1, k1 + jc .opaque + movu m2, [dst_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + BLEND10 + movu [dst_q + 2*r6q - mmsize], m6 + jmp .next +.opaque: + movu m3, [src_q + 2*r6q - mmsize] + movu [dst_q + 2*r6q - mmsize], m3 +.next: +%endmacro + +cglobal blit10_blend_inplace_hsub1, 4, 11, 8, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND10_CONSTANTS_ZMM + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND10_INPLACE_ZMM + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10 + .ret: +RET + +cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ + %define bg_q dst_q + BLEND10_CONSTANTS_ZMM + movu m8, [blit10_alpha_permute] + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m4, [alpha_q + 4*r6q - 2*mmsize] + vpermt2w m4, m8, [alpha_q + 4*r6q - mmsize] + mov r7q, r6q + BLEND10_INPLACE_ZMM + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +RET + +%undef bg_q diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 9684e0e5f..389a4a774 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -58,3 +58,35 @@ void upipe_blit10_threshold_inplace_hsub1_avx512(uint16_t *dst_plane, void upipe_blit10_threshold_inplace_hsub2_avx512(uint16_t *dst_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); + +/* In place, bg == dst, avx2: a vector with nothing to change is neither loaded + * nor stored. */ +void upipe_blit8_threshold_inplace_hsub1_avx2(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit8_threshold_inplace_hsub2_avx2(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit10_threshold_inplace_hsub1_avx2(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, + int threshold); +void upipe_blit10_threshold_inplace_hsub2_avx2(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, + int threshold); + +/* Blend in place, bg == dst: all transparent vectors skipped, all opaque ones + * copied. */ +#define UPIPE_BLIT_BLEND_INPLACE(cpu) \ +void upipe_blit8_blend_inplace_hsub1_##cpu(uint8_t *dst_plane, \ + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); \ +void upipe_blit8_blend_inplace_hsub2_##cpu(uint8_t *dst_plane, \ + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); \ +void upipe_blit10_blend_inplace_hsub1_##cpu(uint16_t *dst_plane, \ + const uint16_t *src_plane, const uint16_t *alpha_plane, \ + size_t hsize); \ +void upipe_blit10_blend_inplace_hsub2_##cpu(uint16_t *dst_plane, \ + const uint16_t *src_plane, const uint16_t *alpha_plane, \ + size_t hsize); + +UPIPE_BLIT_BLEND_INPLACE(avx2) +UPIPE_BLIT_BLEND_INPLACE(avx512) diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index d2fd0758d..ca3ad00b4 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -411,6 +411,199 @@ static void check_frame_inplace##bits(void (*func)(type *, const type *, \ CHECK_FRAME(8, uint8_t, 0xff) CHECK_FRAME(10, uint16_t, 0x3ff) +/* Blend in place, the background being the destination. */ +#define BLEND_INPLACE_REF(name, type, max, hsub) \ +static void name(type *dst, const type *src, const type *alpha, \ + size_t hsize) \ +{ \ + for (size_t j = 0; j < hsize; j++) { \ + const unsigned a = alpha[j * (hsub)]; \ + dst[j] = (dst[j] * ((max) - a) + src[j] * a) / (max); \ + } \ +} + +BLEND_INPLACE_REF(blit8_blend_inplace_hsub1_ref, uint8_t, 0xff, 1) +BLEND_INPLACE_REF(blit8_blend_inplace_hsub2_ref, uint8_t, 0xff, 2) +BLEND_INPLACE_REF(blit10_blend_inplace_hsub1_ref, uint16_t, 0x3ff, 1) +BLEND_INPLACE_REF(blit10_blend_inplace_hsub2_ref, uint16_t, 0x3ff, 2) + +/* Alpha as a rendered subtitle gives it, per 64 sample block: transparent, + * opaque (a box or a glyph's body), or glyph edges, runs of transparent, + * opaque and partial samples. */ +#define SUB_ALPHA(bits, type, max) \ +static void sub_alpha##bits(type *alpha, size_t n) \ +{ \ + for (size_t b = 0; b < n; b += 64) { \ + size_t end = b + 64 < n ? b + 64 : n; \ + unsigned kind = rnd() % 10; \ + for (size_t i = b; i < end; ) { \ + if (kind < 4) { \ + alpha[i++] = 0; \ + continue; \ + } \ + if (kind < 7) { \ + alpha[i++] = (max); \ + continue; \ + } \ + size_t run = 2 + rnd() % 11; \ + unsigned what = rnd() % 3; \ + type a = what == 0 ? 0 : what == 1 ? (max) : \ + 1 + rnd() % ((max) - 1); \ + for (; run && i < end; run--, i++) \ + alpha[i] = a; \ + } \ + } \ +} + +SUB_ALPHA(8, uint8_t, 0xff) +SUB_ALPHA(10, uint16_t, 0x3ff) + +#define CHECK_BLEND_INPLACE(bits, type, max, min_hsize) \ +static void check_blend_inplace##bits(void (*func)(type *, const type *, \ + const type *, size_t), \ + const char *name) \ +{ \ + static const size_t widths[] = { 512, NUM_SAMPLES, THRESHOLD_MAX, \ + min_hsize }; \ + \ + if (!check_func(func, "%s", name)) \ + return; \ + \ + type bg[THRESHOLD_MAX + GUARD]; \ + type src[THRESHOLD_MAX]; \ + type alpha[THRESHOLD_MAX * 2]; \ + type dst0[THRESHOLD_MAX + GUARD]; \ + type dst1[THRESHOLD_MAX + GUARD]; \ + \ + declare_func(void, type *dst, const type *src, const type *alpha, \ + size_t hsize); \ + \ + memset(bg, 0xa5, sizeof bg); \ + for (size_t i = 0; i < THRESHOLD_MAX; i++) { \ + bg[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + \ + /* random alpha, then subtitle alpha, which is left for the bench */ \ + for (int pattern = 0; pattern < 2; pattern++) { \ + if (pattern) \ + sub_alpha##bits(alpha, THRESHOLD_MAX * 2); \ + else \ + for (size_t i = 0; i < THRESHOLD_MAX * 2; i++) \ + alpha[i] = rnd() & (max); \ + for (size_t w = 0; w < sizeof(widths) / sizeof(widths[0]); w++) { \ + memcpy(dst0, bg, sizeof dst0); \ + memcpy(dst1, bg, sizeof dst1); \ + call_ref(dst0, src, alpha, widths[w]); \ + call_new(dst1, src, alpha, widths[w]); \ + if (memcmp(dst0, dst1, sizeof dst0)) \ + fail(); \ + } \ + } \ + \ + bench_new(dst1, src, alpha, NUM_SAMPLES); \ +} + +CHECK_BLEND_INPLACE(8, uint8_t, 0xff, 32) +CHECK_BLEND_INPLACE(10, uint16_t, 0x3ff, 16) + +/* The blend at frame size with subtitle alpha, fused and in place. */ +#define CHECK_FRAME_BLEND(bits, type, max) \ +static void check_frame_blend##bits(void (*func)(type *, const type *, \ + const type *, const type *,\ + size_t), \ + void (*ref)(type *, const type *, \ + const type *, const type *, \ + size_t), \ + const char *name, uint8_t hsub) \ +{ \ + if (!check_func(func, "%s_frame", name)) \ + return; \ + \ + const size_t n = FRAME_SAMPLES; \ + type *bg = aligned_alloc(64, n * sizeof(type)); \ + type *src = aligned_alloc(64, n * sizeof(type)); \ + type *alpha = aligned_alloc(64, 2 * n * sizeof(type)); \ + type *dst0 = aligned_alloc(64, n * sizeof(type)); \ + type *dst1 = aligned_alloc(64, n * sizeof(type)); \ + \ + declare_func(void, type *dst, const type *bg, const type *src, \ + const type *alpha, size_t hsize); \ + \ + if (!bg || !src || !alpha || !dst0 || !dst1) { \ + fail(); \ + } else { \ + for (size_t i = 0; i < n; i++) { \ + bg[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + sub_alpha##bits(alpha, n * hsub); \ + \ + call_ref(dst0, bg, src, alpha, n); \ + call_new(dst1, bg, src, alpha, n); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + \ + if (func != ref) \ + bench_new(dst1, bg, src, alpha, n); \ + } \ + \ + free(bg); \ + free(src); \ + free(alpha); \ + free(dst0); \ + free(dst1); \ +} \ + \ +static void check_frame_blend_inplace##bits(void (*func)(type *, \ + const type *, \ + const type *, \ + size_t), \ + void (*ref)(type *, \ + const type *, \ + const type *, \ + size_t), \ + const char *name, uint8_t hsub) \ +{ \ + if (!check_func(func, "%s_frame", name)) \ + return; \ + \ + const size_t n = FRAME_SAMPLES; \ + type *src = aligned_alloc(64, n * sizeof(type)); \ + type *alpha = aligned_alloc(64, 2 * n * sizeof(type)); \ + type *dst0 = aligned_alloc(64, n * sizeof(type)); \ + type *dst1 = aligned_alloc(64, n * sizeof(type)); \ + \ + declare_func(void, type *dst, const type *src, const type *alpha, \ + size_t hsize); \ + \ + if (!src || !alpha || !dst0 || !dst1) { \ + fail(); \ + } else { \ + for (size_t i = 0; i < n; i++) { \ + dst0[i] = dst1[i] = rnd() & (max); \ + src[i] = rnd() & (max); \ + } \ + sub_alpha##bits(alpha, n * hsub); \ + \ + call_ref(dst0, src, alpha, n); \ + call_new(dst1, src, alpha, n); \ + if (memcmp(dst0, dst1, n * sizeof(type))) \ + fail(); \ + \ + if (func != ref) \ + bench_new(dst1, src, alpha, n); \ + } \ + \ + free(src); \ + free(alpha); \ + free(dst0); \ + free(dst1); \ +} + +CHECK_FRAME_BLEND(8, uint8_t, 0xff) +CHECK_FRAME_BLEND(10, uint16_t, 0x3ff) + void checkasm_check_ubuf_pic_blit(void) { void (*threshold8_hsub1)(uint8_t *, const uint8_t *, const uint8_t *, @@ -440,6 +633,12 @@ void checkasm_check_ubuf_pic_blit(void) void (*inplace10[2])(uint16_t *, const uint16_t *, const uint16_t *, size_t, int) = { blit10_inplace_hsub1_ref, blit10_inplace_hsub2_ref }; + void (*blend_inplace8[2])(uint8_t *, const uint8_t *, const uint8_t *, + size_t) = { + blit8_blend_inplace_hsub1_ref, blit8_blend_inplace_hsub2_ref }; + void (*blend_inplace10[2])(uint16_t *, const uint16_t *, const uint16_t *, + size_t) = { + blit10_blend_inplace_hsub1_ref, blit10_blend_inplace_hsub2_ref }; #ifdef HAVE_X86ASM int cpu_flags = av_get_cpu_flags(); @@ -453,6 +652,14 @@ void checkasm_check_ubuf_pic_blit(void) threshold8_hsub2 = upipe_blit8_threshold_hsub2_avx2; threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx2; threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx2; + inplace8[0] = upipe_blit8_threshold_inplace_hsub1_avx2; + inplace8[1] = upipe_blit8_threshold_inplace_hsub2_avx2; + inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx2; + inplace10[1] = upipe_blit10_threshold_inplace_hsub2_avx2; + blend_inplace8[0] = upipe_blit8_blend_inplace_hsub1_avx2; + blend_inplace8[1] = upipe_blit8_blend_inplace_hsub2_avx2; + blend_inplace10[0] = upipe_blit10_blend_inplace_hsub1_avx2; + blend_inplace10[1] = upipe_blit10_blend_inplace_hsub2_avx2; } #ifdef AV_CPU_FLAG_AVX512 if (cpu_flags & AV_CPU_FLAG_AVX512) { @@ -464,6 +671,10 @@ void checkasm_check_ubuf_pic_blit(void) inplace8[1] = upipe_blit8_threshold_inplace_hsub2_avx512; inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx512; inplace10[1] = upipe_blit10_threshold_inplace_hsub2_avx512; + blend_inplace8[0] = upipe_blit8_blend_inplace_hsub1_avx512; + blend_inplace8[1] = upipe_blit8_blend_inplace_hsub2_avx512; + blend_inplace10[0] = upipe_blit10_blend_inplace_hsub1_avx512; + blend_inplace10[1] = upipe_blit10_blend_inplace_hsub2_avx512; } #endif #endif @@ -501,9 +712,15 @@ void checkasm_check_ubuf_pic_blit(void) snprintf(name, sizeof name, "blit10_threshold_inplace_hsub%d", h + 1); check_inplace10(inplace10[h], name); report("%s", name); + snprintf(name, sizeof name, "blit8_blend_inplace_hsub%d", h + 1); + check_blend_inplace8(blend_inplace8[h], name); + report("%s", name); + snprintf(name, sizeof name, "blit10_blend_inplace_hsub%d", h + 1); + check_blend_inplace10(blend_inplace10[h], name); + report("%s", name); } - /* frame sized, text alpha */ + /* frame sized, text alpha for the threshold, subtitle alpha for the blend */ void (*const ref8[2])(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, size_t, int) = { blit8_threshold_hsub1_ref, blit8_threshold_hsub2_ref }; @@ -537,4 +754,42 @@ void checkasm_check_ubuf_pic_blit(void) check_frame_inplace10(inplace10[h], ref_inplace10[h], name, h + 1); report("%s_frame", name); } + + void (*const blend_ref8[2])(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t) = { + blit8_blend_hsub1_ref, blit8_blend_hsub2_ref }; + void (*const blend_ref10[2])(uint16_t *, const uint16_t *, + const uint16_t *, const uint16_t *, + size_t) = { + blit10_blend_hsub1_ref, blit10_blend_hsub2_ref }; + void (*const blend_inplace_ref8[2])(uint8_t *, const uint8_t *, + const uint8_t *, size_t) = { + blit8_blend_inplace_hsub1_ref, blit8_blend_inplace_hsub2_ref }; + void (*const blend_inplace_ref10[2])(uint16_t *, const uint16_t *, + const uint16_t *, size_t) = { + blit10_blend_inplace_hsub1_ref, blit10_blend_inplace_hsub2_ref }; + void (*const blend_fused8[2])(uint8_t *, const uint8_t *, const uint8_t *, + const uint8_t *, size_t) = { + blend8_hsub1, blend8_hsub2 }; + void (*const blend_fused10[2])(uint16_t *, const uint16_t *, + const uint16_t *, const uint16_t *, + size_t) = { + blend_hsub1, blend_hsub2 }; + + for (int h = 0; h < 2; h++) { + snprintf(name, sizeof name, "blit8_blend_hsub%d", h + 1); + check_frame_blend8(blend_fused8[h], blend_ref8[h], name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit10_blend_hsub%d", h + 1); + check_frame_blend10(blend_fused10[h], blend_ref10[h], name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit8_blend_inplace_hsub%d", h + 1); + check_frame_blend_inplace8(blend_inplace8[h], blend_inplace_ref8[h], + name, h + 1); + report("%s_frame", name); + snprintf(name, sizeof name, "blit10_blend_inplace_hsub%d", h + 1); + check_frame_blend_inplace10(blend_inplace10[h], blend_inplace_ref10[h], + name, h + 1); + report("%s_frame", name); + } } From 545e590c2452b23967c40ff4aa41bf9f0881df7c Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 05:01:42 +0100 Subject: [PATCH 22/30] ubuf_pic_blit: avx512 blend in place, rows shorter than one vector The loop runs once before its test, which the avx2 kernels rely on the dispatcher's minimum for; at 512 bits that minimum is half a vector, and the first vector wrote past the row. Such a row goes to the scalar tail. Found by checkasm at the minimum widths. --- lib/upipe/x86/ubuf_pic_blit.asm | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 8f7381fbf..835bd8701 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -1007,7 +1007,8 @@ THRESHOLD_HSUB1_FUSED 8, d, b, 1, 8 THRESHOLD_HSUB1_FUSED 10, w, w, 2, 16 ; The blend in place at 512 bits: the same arithmetic, with the all 0 and all -; maximum alpha tests on mask registers. The tail is scalar. +; maximum alpha tests on mask registers. The tail is scalar, and a row shorter +; than one vector, which the avx2 minimum allows, is all tail. INIT_ZMM avx512 @@ -1043,6 +1044,9 @@ cglobal blit8_blend_inplace_hsub1, 4, 10, 11, dst_, src_, alpha_, hsize_ %define bg_q dst_q BLEND8_CONSTANTS_ZMM mov r6d, mmsize + xor r7d, r7d + cmp hsize_q, r6q + jb .tail ALIGN 16 .loop_simd: movu m3, [alpha_q + r6q - mmsize] @@ -1054,6 +1058,7 @@ cglobal blit8_blend_inplace_hsub1, 4, 10, 11, dst_, src_, alpha_, hsize_ cmp r7q, hsize_q jae .ret ALIGN 16 + .tail: SCALAR_TAIL_BLEND_B r7, r8, r9, r6 .ret: RET @@ -1062,6 +1067,9 @@ cglobal blit8_blend_inplace_hsub2, 4, 10, 11, dst_, src_, alpha_, hsize_ %define bg_q dst_q BLEND8_CONSTANTS_ZMM mov r6d, mmsize + xor r7d, r7d + cmp hsize_q, r6q + jb .tail ALIGN 16 .loop_simd: movu m5, [alpha_q + 2*r6q - 2*mmsize] @@ -1077,6 +1085,7 @@ cglobal blit8_blend_inplace_hsub2, 4, 10, 11, dst_, src_, alpha_, hsize_ cmp r7q, hsize_q jae .ret ALIGN 16 + .tail: SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 .ret: RET @@ -1115,6 +1124,9 @@ cglobal blit10_blend_inplace_hsub1, 4, 11, 8, dst_, src_, alpha_, hsize_ %define bg_q dst_q BLEND10_CONSTANTS_ZMM mov r6d, mmsize/2 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail ALIGN 16 .loop_simd: movu m4, [alpha_q + 2*r6q - mmsize] @@ -1126,6 +1138,7 @@ cglobal blit10_blend_inplace_hsub1, 4, 11, 8, dst_, src_, alpha_, hsize_ cmp r7q, hsize_q jae .ret ALIGN 16 + .tail: SCALAR_TAIL_BLEND_W r7, r8, r9, r10 .ret: RET @@ -1135,6 +1148,9 @@ cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ BLEND10_CONSTANTS_ZMM movu m8, [blit10_alpha_permute] mov r6d, mmsize/2 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail ALIGN 16 .loop_simd: movu m4, [alpha_q + 4*r6q - 2*mmsize] @@ -1147,6 +1163,7 @@ cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ cmp r7q, hsize_q jae .ret ALIGN 16 + .tail: SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 .ret: RET From 73da751377f27aefd3e93b6023394281d104050f Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 05:12:22 +0100 Subject: [PATCH 23/30] ubuf_pic_blit: dispatch the in place kernels where they win Measured at frame size (1920x256, subtitle and text alpha) against the plain kernels: - Zen 4: the avx512 blend in place is 0.57-0.85 of the avx2 blend, and 0.75-0.89 of the avx2 blend in place, at every depth and subsampling. Dispatched for bg == dst on avx512bw. - Raptor Cove: the avx2 in place kernels win at hsub1 (threshold 1.47x/1.26x, blend 1.12x/1.18x) and lose at hsub2 (0.91-0.995x). Dispatched at hsub1 only; the hsub2 ones go. --- lib/upipe/ubuf_pic_blit_x86.h | 36 +++++---- lib/upipe/x86/ubuf_pic_blit.asm | 129 ++------------------------------ lib/upipe/x86/ubuf_pic_blit.h | 36 ++++----- tests/checkasm/ubuf_pic_blit.c | 4 - 4 files changed, 39 insertions(+), 166 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index 74658ba81..ae52f50a8 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -65,10 +65,7 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - if (dst == bg) - upipe_blit8_threshold_inplace_hsub2_avx2(dst, src, alpha, hsize, threshold); - else - upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -78,6 +75,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold == 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { + if (dst == bg && __builtin_cpu_supports("avx512bw")) { + upipe_blit8_blend_inplace_hsub1_avx512(dst, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { if (dst == bg) upipe_blit8_blend_inplace_hsub1_avx2(dst, src, alpha, hsize); @@ -89,11 +90,12 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, } if (hsub == 2) { + if (dst == bg && __builtin_cpu_supports("avx512bw")) { + upipe_blit8_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { - if (dst == bg) - upipe_blit8_blend_inplace_hsub2_avx2(dst, src, alpha, hsize); - else - upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; } return false; @@ -164,10 +166,7 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, return true; } if (__builtin_cpu_supports("avx2")) { - if (dst == bg) - upipe_blit10_threshold_inplace_hsub2_avx2(dst, src, alpha, hsize, threshold); - else - upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); return true; } return false; @@ -177,6 +176,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold == 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { + if (dst == bg && __builtin_cpu_supports("avx512bw")) { + upipe_blit10_blend_inplace_hsub1_avx512(dst, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { if (dst == bg) upipe_blit10_blend_inplace_hsub1_avx2(dst, src, alpha, hsize); @@ -188,11 +191,12 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, } if (hsub == 2) { + if (dst == bg && __builtin_cpu_supports("avx512bw")) { + upipe_blit10_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { - if (dst == bg) - upipe_blit10_blend_inplace_hsub2_avx2(dst, src, alpha, hsize); - else - upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); + upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; } return false; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 835bd8701..0bf11a9ca 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -287,7 +287,8 @@ RET ; In place, bg == dst: a vector with no sample over the threshold is neither ; loaded nor stored, which leaves the cache lines of transparent areas clean. -; The scalar tails read the background from the destination. +; The scalar tails read the background from the destination. Only hsub1: at +; hsub2 the skip lost to the plain kernel on Raptor Cove. cglobal blit8_threshold_inplace_hsub1, 5, 9, 5, dst_, src_, alpha_, hsize_, threshold_ %define bg_q dst_q @@ -320,44 +321,6 @@ cglobal blit8_threshold_inplace_hsub1, 5, 9, 5, dst_, src_, alpha_, hsize_, thre .ret: RET -cglobal blit8_threshold_inplace_hsub2, 5, 9, 7, dst_, src_, alpha_, hsize_, threshold_ - %define bg_q dst_q - mov r6d, 0x01010101 * 0x80 - movd xm1, threshold_d - VBROADCASTI128 m5, [blit8_alpha_shuffle] - movd xm0, r6d - vpbroadcastb m1, xm1 - vpbroadcastd m0, xm0 - mov r6d, mmsize - pxor m1, m0 - ALIGN 16 - .loop_simd: - movu m4, [alpha_q + 2*r6q - 2*mmsize] - movu m6, [alpha_q + 2*r6q - 1*mmsize] - mov r7q, r6q - pshufb m4, m5 - pshufb m6, m5 - punpcklqdq m4, m4, m6 - vpermq m4, m4, q3120 - pxor m4, m0 - pcmpgtb m4, m1 - ptest m4, m4 - jz .next - pand m3, m4, [src_q + r6q - mmsize] - pandn m2, m4, [dst_q + r6q - mmsize] - por m2, m3 - movu [dst_q + r6q - mmsize], m2 - .next: - lea r6q, [r6q + mmsize] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_B r7, r6, r8, 2 - .ret: -RET - cglobal blit10_threshold_inplace_hsub1, 5, 9, 4, dst_, src_, alpha_, hsize_, threshold_ %define bg_q dst_q movd xm0, threshold_d @@ -385,39 +348,6 @@ cglobal blit10_threshold_inplace_hsub1, 5, 9, 4, dst_, src_, alpha_, hsize_, thr .ret: RET -cglobal blit10_threshold_inplace_hsub2, 5, 9, 6, dst_, src_, alpha_, hsize_, threshold_ - %define bg_q dst_q - movd xm0, threshold_d - VBROADCASTI128 m5, [blit10_alpha_shuffle] - mov r6d, mmsize/2 - vpbroadcastw m0, xm0 - ALIGN 16 - .loop_simd: - movu m3, [alpha_q + 4*r6q - 2*mmsize] - movu m4, [alpha_q + 4*r6q - 1*mmsize] - mov r7q, r6q - pshufb m3, m5 - pshufb m4, m5 - punpcklqdq m3, m3, m4 - vpermq m3, m3, q3120 - pcmpgtw m3, m0 - ptest m3, m3 - jz .next - pand m2, m3, [src_q + 2*r6q - mmsize] - pandn m1, m3, [dst_q + 2*r6q - mmsize] - por m1, m2 - movu [dst_q + 2*r6q - mmsize], m1 - .next: - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_W r7, r6, r8, 2 - .ret: -RET - %undef bg_q ; The 8 bit blend kernels compute dst = (bg * (0xff - a) + src * a) / 0xff, @@ -513,7 +443,8 @@ RET ; In place, bg == dst: a vector whose alpha is all 0 keeps the background and is ; neither loaded nor stored, and one whose alpha is all 0xff takes the source -; without the blend, which gives the same samples. +; without the blend, which gives the same samples. Only hsub1, as the threshold +; ones. %macro BLEND8_INPLACE 0 ; alpha in m3 ptest m3, m3 @@ -550,31 +481,6 @@ cglobal blit8_blend_inplace_hsub1, 4, 10, 11, dst_, src_, alpha_, hsize_ .ret: RET -cglobal blit8_blend_inplace_hsub2, 4, 10, 12, dst_, src_, alpha_, hsize_ - %define bg_q dst_q - BLEND8_CONSTANTS - VBROADCASTI128 shuffle, [blit8_alpha_shuffle] - mov r6d, mmsize - ALIGN 16 - .loop_simd: - movu m5, [alpha_q + 2*r6q - 2*mmsize] - movu m6, [alpha_q + 2*r6q - 1*mmsize] - pshufb m5, shuffle - pshufb m6, shuffle - punpcklqdq m3, m5, m6 - vpermq m3, m3, q3120 - mov r7q, r6q - BLEND8_INPLACE - lea r6q, [r6q + mmsize] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 - .ret: -RET - %undef bg_q %undef pb_80 @@ -632,7 +538,7 @@ cglobal blit10_blend_hsub2, 5, 11, 9, dst_, bg_, src_, alpha_, hsize_ .ret: RET -; In place, as the 8 bit ones: all 0 alpha skipped, all 0x3ff alpha copied. +; In place, as the 8 bit one: all 0 alpha skipped, all 0x3ff alpha copied. %macro BLEND10_INPLACE 0 ; alpha in m4 ptest m4, m4 @@ -671,31 +577,6 @@ cglobal blit10_blend_inplace_hsub1, 4, 11, 8, dst_, src_, alpha_, hsize_ .ret: RET -cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ - %define bg_q dst_q - BLEND10_CONSTANTS - VBROADCASTI128 m8, [blit10_alpha_shuffle] - mov r6d, mmsize/2 - ALIGN 16 - .loop_simd: - movu m4, [alpha_q + 4*r6q - 2*mmsize] - movu m5, [alpha_q + 4*r6q - 1*mmsize] - mov r7q, r6q - pshufb m4, m8 - pshufb m5, m8 - punpcklqdq m4, m4, m5 - vpermq m4, m4, q3120 - BLEND10_INPLACE - lea r6q, [r6q + mmsize/2] - cmp hsize_q, r6q - jae .loop_simd - cmp r7q, hsize_q - jae .ret - ALIGN 16 - SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 - .ret: -RET - %undef bg_q ; The avx512 kernels replace the +0x80 bias and the pand/pandn/por select with an diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 389a4a774..b370c5a87 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -59,34 +59,26 @@ void upipe_blit10_threshold_inplace_hsub2_avx512(uint16_t *dst_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); -/* In place, bg == dst, avx2: a vector with nothing to change is neither loaded - * nor stored. */ +/* In place, bg == dst, avx2, hsub1: a vector with nothing to change is neither + * loaded nor stored. */ void upipe_blit8_threshold_inplace_hsub1_avx2(uint8_t *dst_plane, const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit8_threshold_inplace_hsub2_avx2(uint8_t *dst_plane, - const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize, - int threshold); void upipe_blit10_threshold_inplace_hsub1_avx2(uint16_t *dst_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, int threshold); -void upipe_blit10_threshold_inplace_hsub2_avx2(uint16_t *dst_plane, - const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize, - int threshold); +void upipe_blit8_blend_inplace_hsub1_avx2(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_inplace_hsub1_avx2(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); /* Blend in place, bg == dst: all transparent vectors skipped, all opaque ones * copied. */ -#define UPIPE_BLIT_BLEND_INPLACE(cpu) \ -void upipe_blit8_blend_inplace_hsub1_##cpu(uint8_t *dst_plane, \ - const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); \ -void upipe_blit8_blend_inplace_hsub2_##cpu(uint8_t *dst_plane, \ - const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); \ -void upipe_blit10_blend_inplace_hsub1_##cpu(uint16_t *dst_plane, \ - const uint16_t *src_plane, const uint16_t *alpha_plane, \ - size_t hsize); \ -void upipe_blit10_blend_inplace_hsub2_##cpu(uint16_t *dst_plane, \ - const uint16_t *src_plane, const uint16_t *alpha_plane, \ - size_t hsize); - -UPIPE_BLIT_BLEND_INPLACE(avx2) -UPIPE_BLIT_BLEND_INPLACE(avx512) +void upipe_blit8_blend_inplace_hsub1_avx512(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); +void upipe_blit8_blend_inplace_hsub2_avx512(uint8_t *dst_plane, + const uint8_t *src_plane, const uint8_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_inplace_hsub1_avx512(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_inplace_hsub2_avx512(uint16_t *dst_plane, + const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index ca3ad00b4..1de9d3606 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -653,13 +653,9 @@ void checkasm_check_ubuf_pic_blit(void) threshold10_hsub1 = upipe_blit10_threshold_hsub1_avx2; threshold10_hsub2 = upipe_blit10_threshold_hsub2_avx2; inplace8[0] = upipe_blit8_threshold_inplace_hsub1_avx2; - inplace8[1] = upipe_blit8_threshold_inplace_hsub2_avx2; inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx2; - inplace10[1] = upipe_blit10_threshold_inplace_hsub2_avx2; blend_inplace8[0] = upipe_blit8_blend_inplace_hsub1_avx2; - blend_inplace8[1] = upipe_blit8_blend_inplace_hsub2_avx2; blend_inplace10[0] = upipe_blit10_blend_inplace_hsub1_avx2; - blend_inplace10[1] = upipe_blit10_blend_inplace_hsub2_avx2; } #ifdef AV_CPU_FLAG_AVX512 if (cpu_flags & AV_CPU_FLAG_AVX512) { From eaaa974baaca9b933923dedc168dc1cdbb107f35 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 9 Oct 2026 13:01:47 +0100 Subject: [PATCH 24/30] ubuf_pic_blit: kmovw from a 32 bit register The 10 bit hsub1 kernel at 256 bits built its tail mask with kmovw from r7w, which nasm 3 accepts but nasm 2.14 rejects: kmovw takes a 32 bit register. --- lib/upipe/x86/ubuf_pic_blit.asm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 0bf11a9ca..d128f71fe 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -587,11 +587,15 @@ RET ; Build k2, the predicate for the trailing hsize_q samples, and leave -1 in r6q ; for the alpha predicates below. %1 is the mask width: q for byte samples, -; d for word samples. +; d for word samples, w for the words of a ymm. %macro TAIL_MASK 1 mov r6q, -1 bzhi r7q, r6q, hsize_q - kmov%1 k2, r7 %+ %1 +%ifidn %1, q + kmovq k2, r7q +%else + kmov%1 k2, r7d ; kmovw takes a 32 bit register, which nasm 2 insists on +%endif %endmacro ; Build k3 and k4, the predicates for the two alpha vectors feeding the trailing From 59c5abf4b01fd32843dbdb1c0d6a4226bf6c203a Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Fri, 16 Oct 2026 18:41:29 +0200 Subject: [PATCH 25/30] ubuf_pic_blit: detect avx512 once __builtin_cpu_supports ran for every row. Read the avx512 features once in a constructor instead. gcc before 8 has no avx512 feature names for it, so the avx512 kernels are not used with those compilers. --- lib/upipe/ubuf_pic_blit_x86.h | 46 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index ae52f50a8..a56419530 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -2,8 +2,38 @@ #include #include +#include + #include "x86/ubuf_pic_blit.h" +/* gcc < 8 has no avx512 names for __builtin_cpu_supports: the avx512 + * kernels are not used there */ +#if defined(__clang__) || __GNUC__ >= 8 +#define BLIT_CPU_AVX512_NAMES 1 +#endif + +static inline bool has_avx512_support(void) +{ +#ifdef BLIT_CPU_AVX512_NAMES + return __builtin_cpu_supports("avx512f") + && __builtin_cpu_supports("avx512cd") + && __builtin_cpu_supports("avx512bw") + && __builtin_cpu_supports("avx512dq") + && __builtin_cpu_supports("avx512vl"); +#else + return false; +#endif +} + +/* __builtin_cpu_supports is too slow to run per row */ +static bool blit_avx512; + +__attribute__((constructor)) static void blit_avx512_init(void) +{ + __builtin_cpu_init(); + blit_avx512 = has_avx512_support(); +} + /* * @This handle particular blit cases. Exists to avoid more nested ifs. * @@ -39,7 +69,7 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold != 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { - if (__builtin_cpu_supports("avx512bw")) { + if (blit_avx512) { if (dst == bg) upipe_blit8_threshold_inplace_hsub1_avx512(dst, src, alpha, hsize, threshold); else @@ -57,7 +87,7 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, } if (hsub == 2) { - if (__builtin_cpu_supports("avx512bw")) { + if (blit_avx512) { if (dst == bg) upipe_blit8_threshold_inplace_hsub2_avx512(dst, src, alpha, hsize, threshold); else @@ -75,7 +105,7 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, else if (threshold == 0xff && alpha_multiplier == 0xff) { if (hsub == 1) { - if (dst == bg && __builtin_cpu_supports("avx512bw")) { + if (dst == bg && blit_avx512) { upipe_blit8_blend_inplace_hsub1_avx512(dst, src, alpha, hsize); return true; } @@ -90,7 +120,7 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, } if (hsub == 2) { - if (dst == bg && __builtin_cpu_supports("avx512bw")) { + if (dst == bg && blit_avx512) { upipe_blit8_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); return true; } @@ -140,7 +170,7 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold != 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { - if (__builtin_cpu_supports("avx512bw")) { + if (blit_avx512) { if (dst == bg) upipe_blit10_threshold_inplace_hsub1_avx512(dst, src, alpha, hsize, threshold); else @@ -158,7 +188,7 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, } if (hsub == 2) { - if (__builtin_cpu_supports("avx512bw")) { + if (blit_avx512) { if (dst == bg) upipe_blit10_threshold_inplace_hsub2_avx512(dst, src, alpha, hsize, threshold); else @@ -176,7 +206,7 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, else if (threshold == 0x3ff && alpha_multiplier == 0x3ff) { if (hsub == 1) { - if (dst == bg && __builtin_cpu_supports("avx512bw")) { + if (dst == bg && blit_avx512) { upipe_blit10_blend_inplace_hsub1_avx512(dst, src, alpha, hsize); return true; } @@ -191,7 +221,7 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, } if (hsub == 2) { - if (dst == bg && __builtin_cpu_supports("avx512bw")) { + if (dst == bg && blit_avx512) { upipe_blit10_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); return true; } From bfd34be303c2ece5ce4521cf059458947ddc6f55 Mon Sep 17 00:00:00 2001 From: James Darnley Date: Fri, 26 Aug 2022 18:11:31 +0200 Subject: [PATCH 26/30] x86inc: add the avx512icl cpuflag As in FFmpeg's x86inc, avx512icl implies avx512. --- x86/x86inc.asm | 53 +++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/x86/x86inc.asm b/x86/x86inc.asm index 01c35e3a4..251ee797d 100644 --- a/x86/x86inc.asm +++ b/x86/x86inc.asm @@ -817,32 +817,33 @@ BRANCH_INSTR jz, je, jnz, jne, jl, jle, jnl, jnle, jg, jge, jng, jnge, ja, jae, ; cpuflags -%assign cpuflags_mmx (1<<0) -%assign cpuflags_mmx2 (1<<1) | cpuflags_mmx -%assign cpuflags_3dnow (1<<2) | cpuflags_mmx -%assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow -%assign cpuflags_sse (1<<4) | cpuflags_mmx2 -%assign cpuflags_sse2 (1<<5) | cpuflags_sse -%assign cpuflags_sse2slow (1<<6) | cpuflags_sse2 -%assign cpuflags_lzcnt (1<<7) | cpuflags_sse2 -%assign cpuflags_sse3 (1<<8) | cpuflags_sse2 -%assign cpuflags_ssse3 (1<<9) | cpuflags_sse3 -%assign cpuflags_sse4 (1<<10)| cpuflags_ssse3 -%assign cpuflags_sse42 (1<<11)| cpuflags_sse4 -%assign cpuflags_aesni (1<<12)| cpuflags_sse42 -%assign cpuflags_avx (1<<13)| cpuflags_sse42 -%assign cpuflags_xop (1<<14)| cpuflags_avx -%assign cpuflags_fma4 (1<<15)| cpuflags_avx -%assign cpuflags_fma3 (1<<16)| cpuflags_avx -%assign cpuflags_bmi1 (1<<17)| cpuflags_avx|cpuflags_lzcnt -%assign cpuflags_bmi2 (1<<18)| cpuflags_bmi1 -%assign cpuflags_avx2 (1<<19)| cpuflags_fma3|cpuflags_bmi2 -%assign cpuflags_avx512 (1<<20)| cpuflags_avx2 ; F, CD, BW, DQ, VL - -%assign cpuflags_cache32 (1<<21) -%assign cpuflags_cache64 (1<<22) -%assign cpuflags_aligned (1<<23) ; not a cpu feature, but a function variant -%assign cpuflags_atom (1<<24) +%assign cpuflags_mmx (1<<0) +%assign cpuflags_mmx2 (1<<1) | cpuflags_mmx +%assign cpuflags_3dnow (1<<2) | cpuflags_mmx +%assign cpuflags_3dnowext (1<<3) | cpuflags_3dnow +%assign cpuflags_sse (1<<4) | cpuflags_mmx2 +%assign cpuflags_sse2 (1<<5) | cpuflags_sse +%assign cpuflags_sse2slow (1<<6) | cpuflags_sse2 +%assign cpuflags_lzcnt (1<<7) | cpuflags_sse2 +%assign cpuflags_sse3 (1<<8) | cpuflags_sse2 +%assign cpuflags_ssse3 (1<<9) | cpuflags_sse3 +%assign cpuflags_sse4 (1<<10)| cpuflags_ssse3 +%assign cpuflags_sse42 (1<<11)| cpuflags_sse4 +%assign cpuflags_aesni (1<<12)| cpuflags_sse42 +%assign cpuflags_avx (1<<13)| cpuflags_sse42 +%assign cpuflags_xop (1<<14)| cpuflags_avx +%assign cpuflags_fma4 (1<<15)| cpuflags_avx +%assign cpuflags_fma3 (1<<16)| cpuflags_avx +%assign cpuflags_bmi1 (1<<17)| cpuflags_avx|cpuflags_lzcnt +%assign cpuflags_bmi2 (1<<18)| cpuflags_bmi1 +%assign cpuflags_avx2 (1<<19)| cpuflags_fma3|cpuflags_bmi2 +%assign cpuflags_avx512 (1<<20)| cpuflags_avx2 ; F, CD, BW, DQ, VL +%assign cpuflags_avx512icl (1<<25)| cpuflags_avx512 + +%assign cpuflags_cache32 (1<<21) +%assign cpuflags_cache64 (1<<22) +%assign cpuflags_aligned (1<<23) ; not a cpu feature, but a function variant +%assign cpuflags_atom (1<<24) ; Returns a boolean value expressing whether or not the specified cpuflag is enabled. %define cpuflag(x) (((((cpuflags & (cpuflags_ %+ x)) ^ (cpuflags_ %+ x)) - 1) >> 31) & 1) From 5f3c956e5f30ac641c8ce5dd59003f5e91851de1 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Sun, 1 Nov 2026 17:17:55 +0000 Subject: [PATCH 27/30] ubuf_pic_blit: blit10_blend hsub1/hsub2 AVX512-VNNI in ymm Per half, vpdpwssd into an accumulator of 1 gives M = N + 1, and (M * 1025) >> 20 equals (M + (M >> 10)) >> 10, the BLEND10 result (N + (N >> 10) + 1) >> 10, for every N from 0 to 0x3ff * 0x3ff (checked exhaustively; M * 1025 stays below 2^31): 12 ops a vector instead of 16. hsub2 gathers alpha with one vpermt2w instead of pshufb x2, punpcklqdq and vpermq. ymm: on Zen 4 a zmm op takes two of the four 256 bit pipes. Dispatched for hsub1 when dst != bg (in place keeps the avx512 kernel), and for hsub2 when the in place avx512 kernel is not taken. checkasm 158/158 (ubuf_pic_blit 52/52 on seeds 1, 2, 3, 77, 9999). EPYC 8324P (Zen 4c), checkasm --bench min of 5, cycles: blit10_blend_hsub1 190.8 -> 184.2 (-3.5%) blit10_blend_hsub1_frame 179341 -> 168332 (-6.1%) blit10_blend_hsub2 276.7 -> 205.2 (-25.8%) blit10_blend_hsub2_frame 253926 -> 188260 (-25.9%) --- lib/upipe/ubuf_pic_blit_x86.h | 26 +++++++++++ lib/upipe/x86/ubuf_pic_blit.asm | 77 +++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 7 +++ tests/checkasm/ubuf_pic_blit.c | 6 +++ 4 files changed, 116 insertions(+) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index a56419530..a39d8d0bc 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -25,13 +25,31 @@ static inline bool has_avx512_support(void) #endif } +static inline bool has_avx512icl_support(void) +{ +#ifdef BLIT_CPU_AVX512_NAMES + return has_avx512_support() + && __builtin_cpu_supports("avx512vnni") + && __builtin_cpu_supports("avx512ifma") + && __builtin_cpu_supports("avx512vbmi") + && __builtin_cpu_supports("avx512vbmi2") + && __builtin_cpu_supports("avx512vpopcntdq") + && __builtin_cpu_supports("avx512bitalg") + && __builtin_cpu_supports("gfni"); +#else + return false; +#endif +} + /* __builtin_cpu_supports is too slow to run per row */ static bool blit_avx512; +static bool blit_avx512icl; __attribute__((constructor)) static void blit_avx512_init(void) { __builtin_cpu_init(); blit_avx512 = has_avx512_support(); + blit_avx512icl = has_avx512icl_support(); } /* @@ -210,6 +228,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_blend_inplace_hsub1_avx512(dst, src, alpha, hsize); return true; } + if (dst != bg && blit_avx512icl) { + upipe_blit10_blend_hsub1_avx512icl(dst, bg, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { if (dst == bg) upipe_blit10_blend_inplace_hsub1_avx2(dst, src, alpha, hsize); @@ -225,6 +247,10 @@ static bool blit10_handle_cases(uint16_t *dst, const uint16_t *bg, upipe_blit10_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); return true; } + if (blit_avx512icl) { + upipe_blit10_blend_hsub2_avx512icl(dst, bg, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit10_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index d128f71fe..7462a6e44 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -16,6 +16,7 @@ blit10_alpha_permute: blit8_blend_bias: dw 0x7f81 blit8_blend_101: dw 0x101 blit8_blend_80: db 0x80 +blit10_blend_1025: dd 1025 SECTION .text @@ -1054,3 +1055,79 @@ cglobal blit10_blend_inplace_hsub2, 4, 11, 9, dst_, src_, alpha_, hsize_ RET %undef bg_q + +; blit10_blend with AVX512-VNNI, in ymm (the same bytes per cycle as zmm on Zen 4, +; where a zmm op takes two of the four 256 bit pipes). Per half, vpdpwssd into +; an accumulator of 1 gives M = N + 1, and (M * 1025) >> 20 equals the +; (M + (M >> 10)) >> 10 of BLEND10 for every M from 1 to 0x3ff * 0x3ff + 1 +; (checked exhaustively), so N / 0x3ff takes 3 ops a half instead of 5: 12 a +; vector instead of 16. hsub2 gathers its alpha with one vpermt2w. +INIT_YMM avx512icl + +; bg in m2, src in m3, alpha in m4 -> m6; m0 = 0x3ff words, m1 = 1 dwords, +; m9 = 1025 dwords; clobbers m2, m3, m5 and m7 +%macro BLEND10_VNNI 0 + psubw m5, m0, m4 ; 0x3ff - a + punpcklwd m6, m2, m3 ; (bg, src) pairs, low half + punpckhwd m2, m2, m3 ; and high half + punpcklwd m7, m5, m4 ; the matching (0x3ff - a, a) pairs + punpckhwd m5, m5, m4 + mova m3, m1 + vpdpwssd m3, m6, m7 ; N + 1 + mova m6, m1 + vpdpwssd m6, m2, m5 + pmulld m3, m9 + pmulld m6, m9 + psrld m3, 20 ; / 0x3ff + psrld m6, 20 + packssdw m6, m3, m6 +%endmacro + +%macro BLEND10_VNNI_CONSTANTS 0 + BLEND10_CONSTANTS_ZMM ; pcmpeq to a vector needs the VEX form + vpbroadcastd m9, [blit10_blend_1025] +%endmacro + +cglobal blit10_blend_hsub1, 5, 11, 10, dst_, bg_, src_, alpha_, hsize_ + BLEND10_VNNI_CONSTANTS + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND10_VNNI + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10 + .ret: +RET + +cglobal blit10_blend_hsub2, 5, 11, 10, dst_, bg_, src_, alpha_, hsize_ + BLEND10_VNNI_CONSTANTS + movu m8, [blit10_alpha_permute] ; ymm: the even words of two registers + mov r6d, mmsize/2 + ALIGN 16 + .loop_simd: + movu m2, [bg_q + 2*r6q - mmsize] + movu m3, [src_q + 2*r6q - mmsize] + movu m4, [alpha_q + 4*r6q - 2*mmsize] + vpermt2w m4, m8, [alpha_q + 4*r6q - mmsize] + mov r7q, r6q + BLEND10_VNNI + movu [dst_q + 2*r6q - mmsize], m6 + lea r6q, [r6q + mmsize/2] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +RET diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index b370c5a87..6d8749a9e 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -28,6 +28,13 @@ void upipe_blit10_blend_hsub1_avx2(uint16_t *dst_plane, const uint16_t *bg_plane void upipe_blit10_blend_hsub2_avx2(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); +/* the same with AVX512-VNNI, in 256 bits */ +void upipe_blit10_blend_hsub1_avx512icl(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); +void upipe_blit10_blend_hsub2_avx512icl(uint16_t *dst_plane, const uint16_t *bg_plane, + const uint16_t *src_plane, + const uint16_t *alpha_plane, size_t hsize); /* The avx512 kernels predicate their tail, so they accept any hsize. The hsub1 * ones work in 256 bits. */ diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index 1de9d3606..02202bcd1 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -673,6 +673,12 @@ void checkasm_check_ubuf_pic_blit(void) blend_inplace10[1] = upipe_blit10_blend_inplace_hsub2_avx512; } #endif +#ifdef AV_CPU_FLAG_AVX512ICL + if (cpu_flags & AV_CPU_FLAG_AVX512ICL) { + blend_hsub1 = upipe_blit10_blend_hsub1_avx512icl; + blend_hsub2 = upipe_blit10_blend_hsub2_avx512icl; + } +#endif #endif check_blend10(blend_hsub1, "blit10_blend_hsub1", 1); From 0403cfb20b8db131b1b2320592c18fc34406d2ab Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Sun, 1 Nov 2026 17:18:13 +0000 Subject: [PATCH 28/30] ubuf_pic_blit: blit8_blend_hsub2 in ymm with a vpermt2b alpha gather One vpermt2b (AVX512-VBMI) takes the even alpha bytes of two registers instead of pshufb x2, punpcklqdq and vpermq: 17 ops a vector instead of 20. The zmm blit8_threshold_hsub2 and blit8_blend_inplace_hsub2 keep their vpmovwb gathers: they are built for plain avx512, without VBMI, and an ICL variant would save about one op. checkasm 160/160 (ubuf_pic_blit 54/54 on seeds 1, 2, 3, 77, 9999). EPYC 8324P (Zen 4c), checkasm --bench min of 5, cycles: blit8_blend_hsub2 126.7 -> 108.6 (-14%) blit8_blend_hsub2_frame 109544 -> 94141 (-14%) --- lib/upipe/ubuf_pic_blit_x86.h | 4 ++++ lib/upipe/x86/ubuf_pic_blit.asm | 40 +++++++++++++++++++++++++++++++++ lib/upipe/x86/ubuf_pic_blit.h | 4 ++++ tests/checkasm/ubuf_pic_blit.c | 1 + 4 files changed, 49 insertions(+) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index a39d8d0bc..e27c87291 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -142,6 +142,10 @@ static bool blit8_handle_cases(uint8_t *dst, const uint8_t *bg, upipe_blit8_blend_inplace_hsub2_avx512(dst, src, alpha, hsize); return true; } + if (blit_avx512icl) { + upipe_blit8_blend_hsub2_avx512icl(dst, bg, src, alpha, hsize); + return true; + } if (__builtin_cpu_supports("avx2")) { upipe_blit8_blend_hsub2_avx2(dst, bg, src, alpha, hsize); return true; diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 7462a6e44..3ff02b2cc 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -17,6 +17,13 @@ blit8_blend_bias: dw 0x7f81 blit8_blend_101: dw 0x101 blit8_blend_80: db 0x80 blit10_blend_1025: dd 1025 +; vpermt2b indices of the even bytes of two registers (ymm uses the first 32) +blit8_even_bytes: +%assign i 0 +%rep 64 + db 2*i +%assign i i+1 +%endrep SECTION .text @@ -1131,3 +1138,36 @@ cglobal blit10_blend_hsub2, 5, 11, 10, dst_, bg_, src_, alpha_, hsize_ SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 .ret: RET + +; blit8_blend_hsub2 in ymm with the alpha gathered by one vpermt2b (AVX512-VBMI) +; instead of pshufb x2, punpcklqdq and vpermq: 17 ops a vector instead of 20. +%define pb_80 m0 +%define pw_bias m1 +%define pw_101 m2 +%define pb_ff m10 + +cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ + BLEND8_CONSTANTS_ZMM ; vpternlogd: pcmpeq to a vector needs VEX + movu m11, [blit8_even_bytes] + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + 2*r6q - 2*mmsize] + vpermt2b m3, m11, [alpha_q + 2*r6q - mmsize] + mov r7q, r6q + BLEND8 + movu [dst_q + r6q - mmsize], m3 + lea r6q, [r6q + mmsize] + cmp hsize_q, r6q + jae .loop_simd + cmp r7q, hsize_q + jae .ret + ALIGN 16 + SCALAR_TAIL_BLEND_B r7, r8, r9, r6, 2 + .ret: +RET + +%undef pb_80 +%undef pw_bias +%undef pw_101 +%undef pb_ff diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index 6d8749a9e..f40d4c584 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -35,6 +35,10 @@ void upipe_blit10_blend_hsub1_avx512icl(uint16_t *dst_plane, const uint16_t *bg_ void upipe_blit10_blend_hsub2_avx512icl(uint16_t *dst_plane, const uint16_t *bg_plane, const uint16_t *src_plane, const uint16_t *alpha_plane, size_t hsize); +/* blit8_blend_hsub2 with a one-op alpha gather (AVX512-VBMI), in 256 bits */ +void upipe_blit8_blend_hsub2_avx512icl(uint8_t *dst_plane, const uint8_t *bg_plane, + const uint8_t *src_plane, + const uint8_t *alpha_plane, size_t hsize); /* The avx512 kernels predicate their tail, so they accept any hsize. The hsub1 * ones work in 256 bits. */ diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index 02202bcd1..d253587d7 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -677,6 +677,7 @@ void checkasm_check_ubuf_pic_blit(void) if (cpu_flags & AV_CPU_FLAG_AVX512ICL) { blend_hsub1 = upipe_blit10_blend_hsub1_avx512icl; blend_hsub2 = upipe_blit10_blend_hsub2_avx512icl; + blend8_hsub2 = upipe_blit8_blend_hsub2_avx512icl; } #endif #endif From 0b84cd105b21ef206d87bbf85f87b56e17947af9 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Thu, 5 Nov 2026 12:14:39 +0100 Subject: [PATCH 29/30] ubuf_pic_blit: use the asm kernels only when they are built The blit dispatch was compiled in for any x86-64 gcc build, but the kernels are only assembled when nasm is available, so a build without it failed to link. Test HAVE_X86ASM as well. The kernels use x86-64 registers, so assemble them, and link them into checkasm, on x86-64 only. ubuf_pic_plane_set_color's SSE2 path got through the blit header, so include it directly. --- lib/upipe/Build.mk | 4 ++-- lib/upipe/ubuf_pic.c | 9 +++++++-- tests/checkasm/Build.mk | 2 +- tests/checkasm/ubuf_pic_blit.c | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/upipe/Build.mk b/lib/upipe/Build.mk index 885e5911c..074903025 100644 --- a/lib/upipe/Build.mk +++ b/lib/upipe/Build.mk @@ -165,8 +165,8 @@ libupipe-src = \ uuri.c libupipe-src += \ - $(if $(have_x86asm),x86/ubuf_pic_blit.asm) \ - $(if $(have_x86asm),x86/ubuf_pic_blit.h) + $(if $(and $(have_x86asm),$(have_x86_64)),x86/ubuf_pic_blit.asm) \ + $(if $(and $(have_x86asm),$(have_x86_64)),x86/ubuf_pic_blit.h) libupipe-ldlibs = -lm diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index d36a6670d..e410480c4 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -12,12 +12,17 @@ * This file defines the picture-specific API to access buffers. */ +#include "config.h" #include "upipe/ubuf_pic.h" #include #include #if defined(__GNUC__) && defined(__x86_64__) +#include +#endif + +#if defined(__GNUC__) && defined(__x86_64__) && defined(HAVE_X86ASM) #include "ubuf_pic_blit_x86.h" #endif @@ -179,7 +184,7 @@ int ubuf_pic_blit_alpha_bg(struct ubuf *dest, struct ubuf *bg, for (int i = 0; i < plane_vsize; i++) { -#if defined(__GNUC__) && defined(__x86_64__) +#if defined(__GNUC__) && defined(__x86_64__) && defined(HAVE_X86ASM) if (in_planes == 1 && blit8_handle_cases(dest_buffer, bg_buffer, in[0], alpha_plane ? alpha_plane + alpha_stride * (i * src_vsub) @@ -387,7 +392,7 @@ int ubuf_pic_blit_alpha10_bg(struct ubuf *dest, struct ubuf *bg, const uint16_t *real_src = (const uint16_t *)src_buffer; const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); -#if defined(__GNUC__) && defined(__x86_64__) +#if defined(__GNUC__) && defined(__x86_64__) && defined(HAVE_X86ASM) if (blit10_handle_cases(real_dst, real_bg, real_src, real_alpha, alpha, threshold, plane_hsize, src_hsub)) /* Nothing to do */; diff --git a/tests/checkasm/Build.mk b/tests/checkasm/Build.mk index e8117a469..3f084b4a5 100644 --- a/tests/checkasm/Build.mk +++ b/tests/checkasm/Build.mk @@ -28,4 +28,4 @@ $(builddir)/checkasm: \ $(top_builddir)/lib/upipe-hbrmt/sdidec.o \ $(top_builddir)/lib/upipe-hbrmt/x86/sdienc.o \ $(top_builddir)/lib/upipe-hbrmt/x86/sdidec.o \ - $(top_builddir)/lib/upipe/x86/ubuf_pic_blit.o + $(if $(have_x86_64),$(top_builddir)/lib/upipe/x86/ubuf_pic_blit.o) diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index d253587d7..71d1da9c8 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -24,7 +24,7 @@ #include "checkasm.h" -#ifdef HAVE_X86ASM +#if defined(HAVE_X86ASM) && defined(__x86_64__) #include "lib/upipe/x86/ubuf_pic_blit.h" #endif @@ -640,7 +640,7 @@ void checkasm_check_ubuf_pic_blit(void) size_t) = { blit10_blend_inplace_hsub1_ref, blit10_blend_inplace_hsub2_ref }; -#ifdef HAVE_X86ASM +#if defined(HAVE_X86ASM) && defined(__x86_64__) int cpu_flags = av_get_cpu_flags(); if (cpu_flags & AV_CPU_FLAG_AVX2) { From 7737294b830760fa13a2e349872ea58512060396 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Thu, 5 Nov 2026 12:38:30 +0100 Subject: [PATCH 30/30] ubuf_pic_blit: add licence headers to the blit files The x86 kernels, their dispatch header and the checkasm test had no SPDX-License-Identifier (the checkasm test carried a GPL notice). Use the MIT licence of the rest of the blit code. --- lib/upipe/ubuf_pic_blit_x86.h | 9 +++++++++ lib/upipe/x86/ubuf_pic_blit.asm | 9 +++++++++ lib/upipe/x86/ubuf_pic_blit.h | 9 +++++++++ tests/checkasm/ubuf_pic_blit.c | 16 +++------------- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h index e27c87291..5d87a1f8a 100644 --- a/lib/upipe/ubuf_pic_blit_x86.h +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2026 Open Broadcast Systems Ltd + * + * Authors: James Darnley + * Kieran Kunhya + * + * SPDX-License-Identifier: MIT + */ + #include #include #include diff --git a/lib/upipe/x86/ubuf_pic_blit.asm b/lib/upipe/x86/ubuf_pic_blit.asm index 3ff02b2cc..7264debb9 100644 --- a/lib/upipe/x86/ubuf_pic_blit.asm +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -1,3 +1,12 @@ +;****************************************************************************** +;* Copyright (C) 2026 Open Broadcast Systems Ltd +;* +;* Authors: James Darnley +;* Kieran Kunhya +;* +;* SPDX-License-Identifier: MIT +;****************************************************************************** + %use smartalign ALIGNMODE p6 diff --git a/lib/upipe/x86/ubuf_pic_blit.h b/lib/upipe/x86/ubuf_pic_blit.h index f40d4c584..de485afc0 100644 --- a/lib/upipe/x86/ubuf_pic_blit.h +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -1,3 +1,12 @@ +/* + * Copyright (C) 2026 Open Broadcast Systems Ltd + * + * Authors: James Darnley + * Kieran Kunhya + * + * SPDX-License-Identifier: MIT + */ + /* dst = alpha > threshold ? src : bg. Pass bg == dst to blend in place. */ void upipe_blit8_threshold_hsub1_avx2(uint8_t *dst_plane, const uint8_t *bg_plane, const uint8_t *src_plane, diff --git a/tests/checkasm/ubuf_pic_blit.c b/tests/checkasm/ubuf_pic_blit.c index 71d1da9c8..9bf48bde4 100644 --- a/tests/checkasm/ubuf_pic_blit.c +++ b/tests/checkasm/ubuf_pic_blit.c @@ -1,19 +1,9 @@ /* - * Copyright (c) 2026 Open Broadcast Systems Ltd. + * Copyright (C) 2026 Open Broadcast Systems Ltd * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * Authors: Kieran Kunhya * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: MIT */ #include