diff --git a/include/upipe/ubuf_pic.h b/include/upipe/ubuf_pic.h index 2009375b2..b58b39aa4 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,6 +371,19 @@ 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 */ +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); + +/** @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, @@ -365,161 +391,24 @@ static inline int ubuf_pic_blit_alpha(struct ubuf *dest, struct ubuf *src, 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 */ - - 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; - -#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; - - uint8_t *dest_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)) - 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); - 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] = - (dest_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 == 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 { - 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 { - /* 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] = - (dest_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] = - (dest_buffer[j * in_planes + p] * (0xff - a) + - in[p][j] * a) / - 0xff; - } - } - } - dest_buffer += dest_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); - 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; + 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 */ +/** @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); + +/** @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, @@ -527,114 +416,33 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, 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 */ - - 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; - - 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; - - uint8_t *dest_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, - extract_hsize, extract_vsize, - &src_buffer); - if (unlikely(!ubase_check(err))) { - 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_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_dst[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 == 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 { - 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 { - /* 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_dst[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; - } - } - } - dest_buffer += dest_stride; - src_buffer += src_stride; - } - - err = ubuf_pic_plane_unmap(dest, chroma, - dest_hoffset, dest_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; + 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. +/** @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,60 +452,27 @@ static inline int ubuf_pic_blit_alpha10(struct ubuf *dest, struct ubuf *src, * @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); + +/** @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) { - 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(dest, src, dest_hoffset, dest_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, - 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(dest, src, dest_hoffset, dest_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; + 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 diff --git a/lib/upipe-modules/upipe_blit.c b/lib/upipe-modules/upipe_blit.c index ae87b4ce8..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; @@ -1086,24 +1120,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->ubuf_hposition + sub->ubuf_hsize <= hsize && + sub->ubuf_vposition + sub->ubuf_vsize <= vsize) { + fused = sub; + break; + } + } + } + + if (fused != NULL) { + int w = hsize, h = 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; + 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); diff --git a/lib/upipe/Build.mk b/lib/upipe/Build.mk index 109606200..074903025 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 $(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 include/upipe/config.h: config.h diff --git a/lib/upipe/ubuf_pic.c b/lib/upipe/ubuf_pic.c index 5767a998b..e410480c4 100644 --- a/lib/upipe/ubuf_pic.c +++ b/lib/upipe/ubuf_pic.c @@ -12,6 +12,7 @@ * This file defines the picture-specific API to access buffers. */ +#include "config.h" #include "upipe/ubuf_pic.h" #include @@ -21,6 +22,532 @@ #include #endif +#if defined(__GNUC__) && defined(__x86_64__) && defined(HAVE_X86ASM) +#include "ubuf_pic_blit_x86.h" +#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 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) + : 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); + 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; + } + + size_t plane_hsize = extract_hsize / src_hsub / src_macropixel * + src_macropixel_size / 2; + size_t plane_vsize = extract_vsize / src_vsub; + + 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; + const uint16_t *real_alpha = (const uint16_t *)(alpha_plane + alpha_stride * i * src_vsub); + +#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 */; + else +#endif + + if ((!alpha_plane && alpha == 0x3ff) || threshold == 0) { + memcpy(dest_buffer, src_buffer, plane_hsize*2); + } else if (!alpha_plane) { + 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) { + /* 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 (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 (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]; + } + } + } else { + /* smooth and slow blending */ + if (alpha == 0x3ff) { + 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 (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; + } + } + } + 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) * diff --git a/lib/upipe/ubuf_pic_blit_x86.h b/lib/upipe/ubuf_pic_blit_x86.h new file mode 100644 index 000000000..5d87a1f8a --- /dev/null +++ b/lib/upipe/ubuf_pic_blit_x86.h @@ -0,0 +1,276 @@ +/* + * Copyright (C) 2026 Open Broadcast Systems Ltd + * + * Authors: James Darnley + * Kieran Kunhya + * + * SPDX-License-Identifier: MIT + */ + +#include +#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 +} + +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(); +} + +/* + * @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 (blit_avx512) { + 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")) { + 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; + } + + if (hsub == 2) { + if (blit_avx512) { + 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")) { + upipe_blit8_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + return false; + } + + else if (threshold == 0xff && alpha_multiplier == 0xff) { + if (hsub == 1) { + if (dst == bg && blit_avx512) { + 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); + else + upipe_blit8_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + + if (hsub == 2) { + if (dst == bg && blit_avx512) { + 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; + } + 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 (blit_avx512) { + 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")) { + 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; + } + + if (hsub == 2) { + if (blit_avx512) { + 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")) { + upipe_blit10_threshold_hsub2_avx2(dst, bg, src, alpha, hsize, threshold); + return true; + } + return false; + } + return false; + } + + else if (threshold == 0x3ff && alpha_multiplier == 0x3ff) { + if (hsub == 1) { + if (dst == bg && blit_avx512) { + 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); + else + upipe_blit10_blend_hsub1_avx2(dst, bg, src, alpha, hsize); + return true; + } + return false; + } + + if (hsub == 2) { + if (dst == bg && blit_avx512) { + 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; + } + 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..7264debb9 --- /dev/null +++ b/lib/upipe/x86/ubuf_pic_blit.asm @@ -0,0 +1,1182 @@ +;****************************************************************************** +;* Copyright (C) 2026 Open Broadcast Systems Ltd +;* +;* Authors: James Darnley +;* Kieran Kunhya +;* +;* SPDX-License-Identifier: MIT +;****************************************************************************** + +%use smartalign +ALIGNMODE p6 + +%include "x86inc.asm" + +SECTION_RODATA 16 + +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 +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 + +; 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 + +%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 +; +; 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_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 + +; 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. 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 + 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 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 + +%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. +; +; 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 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, [%1 + 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_ + BLEND8_CONSTANTS + mov r6d, mmsize + ALIGN 16 + .loop_simd: + movu m3, [alpha_q + 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 + .ret: +RET + +%define shuffle m11 + +cglobal blit8_blend_hsub2, 5, 10, 12, dst_, bg_, src_, alpha_, hsize_ + BLEND8_CONSTANTS + VBROADCASTI128 shuffle, [blit8_alpha_shuffle] + mov r6d, mmsize + ALIGN 16 + .loop_simd: + ; Load alpha and compress every second byte + 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 ; Permute to get correct order of qwords + 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 + +; 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. Only hsub1, as the threshold +; ones. + +%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 + +%undef bg_q + +%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 + 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 + +; 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 + 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 + +%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 +; 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, w for the words of a ymm. +%macro TAIL_MASK 1 + mov r6q, -1 + bzhi r7q, r6q, hsize_q +%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 +; 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 +%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 +%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 + +; 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 + + +; 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/%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%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/%4-1 + jz .ret + 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 + +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, and a row shorter +; than one vector, which the avx2 minimum allows, is all tail. + +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 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail + 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 + .tail: + 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 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail + 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 + .tail: + 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 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail + 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 + .tail: + 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 + xor r7d, r7d + cmp hsize_q, r6q + jb .tail + 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 + .tail: + SCALAR_TAIL_BLEND_W r7, r8, r9, r10, 2 + .ret: +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 + +; 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 new file mode 100644 index 000000000..de485afc0 --- /dev/null +++ b/lib/upipe/x86/ubuf_pic_blit.h @@ -0,0 +1,104 @@ +/* + * 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, + 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); + +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. */ +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); +/* 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); +/* 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. */ +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); + +/* 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); + +/* 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_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_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. */ +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/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/checkasm/Build.mk b/tests/checkasm/Build.mk index 205db0192..3f084b4a5 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 \ + $(if $(have_x86_64),$(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..9bf48bde4 --- /dev/null +++ b/tests/checkasm/ubuf_pic_blit.c @@ -0,0 +1,788 @@ +/* + * Copyright (C) 2026 Open Broadcast Systems Ltd + * + * Authors: Kieran Kunhya + * + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include +#include +#include + +#include "checkasm.h" + +#if defined(HAVE_X86ASM) && defined(__x86_64__) +#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); +} + +/* 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); +} + +/* 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 + +/* 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 *, \ + 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); \ + 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++) { \ + 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, 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) + +/* 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 *, + 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 *, + 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; + /* [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 }; + 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 }; + +#if defined(HAVE_X86ASM) && defined(__x86_64__) + int cpu_flags = av_get_cpu_flags(); + + 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; + 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; + inplace8[0] = upipe_blit8_threshold_inplace_hsub1_avx2; + inplace10[0] = upipe_blit10_threshold_inplace_hsub1_avx2; + blend_inplace8[0] = upipe_blit8_blend_inplace_hsub1_avx2; + blend_inplace10[0] = upipe_blit10_blend_inplace_hsub1_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; + 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; + 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 +#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; + blend8_hsub2 = upipe_blit8_blend_hsub2_avx512icl; + } +#endif +#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"); + + check_blend8(blend8_hsub1, "blit8_blend_hsub1", 1); + report("blit8_blend_hsub1"); + + 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"); + + 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); + 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 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 }; + 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); + } + + 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); + } +} diff --git a/tests/ubuf_pic_blit_test.c b/tests/ubuf_pic_blit_test.c new file mode 100644 index 000000000..886034165 --- /dev/null +++ b/tests/ubuf_pic_blit_test.c @@ -0,0 +1,1800 @@ +/* + * 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 +#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; +} + +/** @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(); + 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); + + /* 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, 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 }, + { 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; + 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; +} 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)