From ac5dab41cabfaad811a4895eda023f69a6323511 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 26 Aug 2026 18:53:53 +0100 Subject: [PATCH 1/5] upipe_blit: scale the bottom padding by the bottom padding The vertical scaling of the destination rectangle derived bpad from tpad, so an asymmetric padding put the picture at the wrong size and position. Only subpictures whose flow definition carries pic padding attributes are affected; for everything else both are zero. --- lib/upipe-modules/upipe_blit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/upipe-modules/upipe_blit.c b/lib/upipe-modules/upipe_blit.c index ae87b4ce8..aae88e3e1 100644 --- a/lib/upipe-modules/upipe_blit.c +++ b/lib/upipe-modules/upipe_blit.c @@ -414,7 +414,7 @@ static int upipe_blit_sub_provide_flow_format(struct upipe *upipe) if (src_vsize) { tpad = tpad * dest_vsize / src_vsize; - bpad = tpad * dest_vsize / src_vsize; + bpad = bpad * dest_vsize / src_vsize; vsize -= tpad + bpad; vsize -= vsize % vround; vposition += (dest_vsize - vsize - tpad - bpad) / 2 + tpad; From a9d0abf1702b96f295b1fce13751748ad75feb2a Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Thu, 5 Nov 2026 13:36:06 +0100 Subject: [PATCH 2/5] avcdec: skip subtitle rects without a bitmap A rect of another type, or a bitmap rect without its index or palette plane, was composed from a NULL pointer. Skip it with a warning. --- lib/upipe-av/upipe_avcodec_decode.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/upipe-av/upipe_avcodec_decode.c b/lib/upipe-av/upipe_avcodec_decode.c index 3c56a1688..7b00e18ac 100644 --- a/lib/upipe-av/upipe_avcodec_decode.c +++ b/lib/upipe-av/upipe_avcodec_decode.c @@ -1011,6 +1011,13 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, /* Decode palettized to bgra */ for (int i = 0; i < sub->num_rects; i++) { AVSubtitleRect *r = sub->rects[i]; + + if (unlikely(r->type != SUBTITLE_BITMAP || + r->data[0] == NULL || r->data[1] == NULL)) { + upipe_warn_va(upipe, "skipping non-bitmap subtitle rect %i", i); + continue; + } + int x = r->x, y = r->y, w = r->w, h = r->h; if (x >= width) { From 725f55ed8db877e906f4134952c1353152765b97 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Tue, 4 Aug 2026 23:27:34 +0100 Subject: [PATCH 3/5] upipe_blit: scale the paddings by the fitted size The padding attributes of a subpicture flow were scaled by dest_size / src_size, but the source is fitted into the destination rectangle by DAR, so when their aspect ratios differ the suggested size and position were wrong in the non-fitted axis. Scale them by the fitted size instead. The vertical position was also rounded by the horizontal rounding parameter. --- lib/upipe-modules/upipe_blit.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/upipe-modules/upipe_blit.c b/lib/upipe-modules/upipe_blit.c index aae88e3e1..95058894e 100644 --- a/lib/upipe-modules/upipe_blit.c +++ b/lib/upipe-modules/upipe_blit.c @@ -402,9 +402,12 @@ static int upipe_blit_sub_provide_flow_format(struct upipe *upipe) vsize = dest_vsize; } + /* Scale the paddings by the same factor as the DAR-fitted source + * (hsize/vsize still hold the fitted full size here), so the picture + * keeps its size and place within it. */ if (src_hsize) { - lpad = lpad * dest_hsize / src_hsize; - rpad = rpad * dest_hsize / src_hsize; + lpad = lpad * hsize / src_hsize; + rpad = rpad * hsize / src_hsize; hsize -= lpad + rpad; hsize -= hsize % hround; hposition += (dest_hsize - hsize - lpad - rpad) / 2 + lpad; @@ -413,12 +416,12 @@ static int upipe_blit_sub_provide_flow_format(struct upipe *upipe) } if (src_vsize) { - tpad = tpad * dest_vsize / src_vsize; - bpad = bpad * dest_vsize / src_vsize; + tpad = tpad * vsize / src_vsize; + bpad = bpad * vsize / src_vsize; vsize -= tpad + bpad; vsize -= vsize % vround; vposition += (dest_vsize - vsize - tpad - bpad) / 2 + tpad; - vposition -= vposition % hround; + vposition -= vposition % vround; uref_pic_set_vposition(uref, vposition); } From 510c9ab4d26f7b6afce31921b6135bdcc6bf2355 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Tue, 4 Aug 2026 23:27:34 +0100 Subject: [PATCH 4/5] avcdec: crop subtitles to the bounding box of their rects The subpicture was composed on a cleared full canvas, but everything downstream works on the whole picture for every frame the subtitle stays on screen, so the transparent part of the canvas was rescaled and alpha blended at every frame. Crop to the bounding box of the rects instead, and describe where the box sits in the canvas with padding attributes: flow format providers such as upipe_blit reconstruct the virtual canvas from size + padding, so the cropped bitmap gets the same scale factor and screen position as the full canvas would. An empty subtitle keeps a minimal transparent picture, since it must still replace the previous subpicture downstream. Offsets and sizes are kept even for the chroma siting of a possible 4:2:0 conversion, and rects with negative coordinates are dropped like the ones past the canvas - they used to write before the start of the plane. --- lib/upipe-av/upipe_avcodec_decode.c | 72 +++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/lib/upipe-av/upipe_avcodec_decode.c b/lib/upipe-av/upipe_avcodec_decode.c index 7b00e18ac..395e34c8d 100644 --- a/lib/upipe-av/upipe_avcodec_decode.c +++ b/lib/upipe-av/upipe_avcodec_decode.c @@ -910,6 +910,54 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, return; } + /* Crop to the bounding box of the rects: the subpicture is rescaled and + * blended downstream for every frame it stays on screen, and the canvas + * is mostly transparent. The paddings describe where the box sits in + * the canvas, so flow format providers (@see upipe_blit) keep the scale + * and position of the full canvas. */ + int crop_x0 = width, crop_y0 = height, crop_x1 = 0, crop_y1 = 0; + for (int i = 0; i < sub->num_rects; i++) { + const AVSubtitleRect *r = sub->rects[i]; + if (r->type != SUBTITLE_BITMAP || + r->data[0] == NULL || r->data[1] == NULL) + continue; + int x = r->x, y = r->y, w = r->w, h = r->h; + if (x < 0 || y < 0 || x >= width || y >= height) + continue; + if (x + w > width) + w = width - x; + if (y + h > height) + h = height - y; + if (x < crop_x0) + crop_x0 = x; + if (y < crop_y0) + crop_y0 = y; + if (x + w > crop_x1) + crop_x1 = x + w; + if (y + h > crop_y1) + crop_y1 = y + h; + } + if (crop_x1 <= crop_x0 || crop_y1 <= crop_y0) { + /* No bitmap: a minimal transparent picture still replaces the + * previous subpicture downstream. */ + crop_x0 = crop_y0 = 0; + crop_x1 = width < 16 ? width : 16; + crop_y1 = height < 16 ? height : 16; + } else { + /* Keep offsets and sizes even for the chroma siting of a possible + * 4:2:0 conversion downstream. */ + crop_x0 &= ~1; + crop_y0 &= ~1; + crop_x1 += crop_x1 & 1; + crop_y1 += crop_y1 & 1; + if (crop_x1 > width) + crop_x1 = width; + if (crop_y1 > height) + crop_y1 = height; + } + const int crop_hsize = crop_x1 - crop_x0; + const int crop_vsize = crop_y1 - crop_y0; + struct uref *flow_def = uref_dup(upipe_avcdec->flow_def_provided); if (unlikely(flow_def == NULL)) { upipe_throw_fatal(upipe, UBASE_ERR_INVALID); @@ -917,10 +965,14 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, } if (unlikely( !ubase_check(uref_pic_flow_set_align(flow_def, alignment)) || - !ubase_check(uref_pic_flow_set_hsize(flow_def, width)) || - !ubase_check(uref_pic_flow_set_vsize(flow_def, height)) || - !ubase_check(uref_pic_flow_set_hsize_visible(flow_def, width)) || - !ubase_check(uref_pic_flow_set_vsize_visible(flow_def, height)) + !ubase_check(uref_pic_flow_set_hsize(flow_def, crop_hsize)) || + !ubase_check(uref_pic_flow_set_vsize(flow_def, crop_vsize)) || + !ubase_check(uref_pic_flow_set_hsize_visible(flow_def, crop_hsize)) || + !ubase_check(uref_pic_flow_set_vsize_visible(flow_def, crop_vsize)) || + !ubase_check(uref_pic_set_lpadding(flow_def, crop_x0)) || + !ubase_check(uref_pic_set_rpadding(flow_def, width - crop_x1)) || + !ubase_check(uref_pic_set_tpadding(flow_def, crop_y0)) || + !ubase_check(uref_pic_set_bpadding(flow_def, height - crop_y1)) )) { upipe_throw_fatal(upipe, UBASE_ERR_INVALID); return; @@ -972,7 +1024,8 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, } /* Allocate a ubuf */ - struct ubuf *ubuf = ubuf_pic_alloc(upipe_avcdec->ubuf_mgr, width, height); + struct ubuf *ubuf = ubuf_pic_alloc(upipe_avcdec->ubuf_mgr, + crop_hsize, crop_vsize); if (unlikely(ubuf == NULL)) { uref_free(flow_def); upipe_throw_fatal(upipe, UBASE_ERR_ALLOC); @@ -1020,12 +1073,12 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, int x = r->x, y = r->y, w = r->w, h = r->h; - if (x >= width) { + if (x < 0 || x >= width) { upipe_warn_va(upipe, "x (%i/%i) is out of the picture", x, width); continue; } - if (y >= height) { + if (y < 0 || y >= height) { upipe_warn_va(upipe, "y (%i/%i) is out of the picture", y, height); continue; @@ -1043,7 +1096,10 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, } /* 4 octets per pixel */ - uint8_t *dst = buf + stride * y + 4 * x; + uint8_t *dst = buf + stride * (y - crop_y0) + 4 * (x - crop_x0); + + /* Source row stride; may exceed the (possibly cropped) blit width, + * so advance src by the real stride rather than by w. */ uint8_t *src = r->data[0]; uint8_t *palette = r->data[1]; const int src_stride = r->linesize[0]; From 58fd78be34e8136f9ae4fe83abc7c1e801354dc1 Mon Sep 17 00:00:00 2001 From: Kieran Kunhya Date: Wed, 5 Aug 2026 14:52:38 +0100 Subject: [PATCH 5/5] avcdec: crop subtitles to the visible pixels, not to the rects The bounding box of the rects crops nothing for teletext: the libzvbi decoder renders the whole page into a single rect, whose transparent part is then rescaled and blended for every frame the page stays on screen - the very thing the box was meant to avoid. The rects are no bound on what is visible, so scan the pixels: an index of the palette is visible if its alpha is not zero, and the box is the union of the boxes of the visible pixels. The box can now be tighter than a rect, so clip the rects to it when composing; the rows and columns kept by the rounding to even offsets and sizes are transparent and blend as before. Subtitle pages come out a couple of rows tall instead of a full page, and dvb subtitles lose the transparent part of their regions as well. The dimensions of a teletext subpicture are no longer constant, so a downstream scaler is reconfigured once per page. --- lib/upipe-av/upipe_avcodec_decode.c | 103 ++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 14 deletions(-) diff --git a/lib/upipe-av/upipe_avcodec_decode.c b/lib/upipe-av/upipe_avcodec_decode.c index 395e34c8d..4a581ce34 100644 --- a/lib/upipe-av/upipe_avcodec_decode.c +++ b/lib/upipe-av/upipe_avcodec_decode.c @@ -855,6 +855,60 @@ static void upipe_avcdec_build_flow_def_sub(struct upipe *upipe, upipe_avcdec_store_flow_def(upipe, flow_def); } +/** @internal @This finds the bounding box of the visible pixels of a + * palettized subtitle rect. + * + * @param r subtitle rect + * @param w width to scan + * @param h height to scan + * @param x0_p filled in with the left edge of the box, in rect coordinates + * @param y0_p filled in with the top edge of the box + * @param x1_p filled in with the right edge of the box, excluded + * @param y1_p filled in with the bottom edge of the box, excluded + * @return false if no pixel of the rect is visible + */ +static bool upipe_avcdec_sub_rect_bbox(const AVSubtitleRect *r, int w, int h, + int *x0_p, int *y0_p, + int *x1_p, int *y1_p) +{ + const uint8_t *src = r->data[0]; + const uint8_t *palette = r->data[1]; + const int src_stride = r->linesize[0]; + /* the palette has the octet order of the output pixels */ +#ifdef UBASE_BIGENDIAN + const int a = 0; +#else + const int a = 3; +#endif + bool visible[256] = { false }; + for (int i = 0; i < r->nb_colors && i < 256; i++) + visible[i] = palette[4 * i + a] != 0; + + int x0 = w, y0 = h, x1 = 0, y1 = 0; + for (int i = 0; i < h; i++) { + const uint8_t *line = src + (size_t)src_stride * i; + for (int j = 0; j < w; j++) { + if (!visible[line[j]]) + continue; + if (j < x0) + x0 = j; + if (j >= x1) + x1 = j + 1; + if (i < y0) + y0 = i; + y1 = i + 1; + } + } + if (x1 <= x0) + return false; + + *x0_p = x0; + *y0_p = y0; + *x1_p = x1; + *y1_p = y1; + return true; +} + /** @internal @This outputs subtitles. * * @param upipe description structure of the pipe @@ -910,11 +964,13 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, return; } - /* Crop to the bounding box of the rects: the subpicture is rescaled and - * blended downstream for every frame it stays on screen, and the canvas - * is mostly transparent. The paddings describe where the box sits in - * the canvas, so flow format providers (@see upipe_blit) keep the scale - * and position of the full canvas. */ + /* Crop to the bounding box of the visible pixels: the subpicture is + * rescaled and blended downstream for every frame it stays on screen, and + * the canvas is mostly transparent. The paddings describe where the box + * sits in the canvas, so flow format providers (@see upipe_blit) keep the + * scale and position of the full canvas. The rects are no bound on what + * is visible - teletext renders the whole page into a single rect - so + * scan the pixels. */ int crop_x0 = width, crop_y0 = height, crop_x1 = 0, crop_y1 = 0; for (int i = 0; i < sub->num_rects; i++) { const AVSubtitleRect *r = sub->rects[i]; @@ -928,17 +984,20 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, w = width - x; if (y + h > height) h = height - y; - if (x < crop_x0) - crop_x0 = x; - if (y < crop_y0) - crop_y0 = y; - if (x + w > crop_x1) - crop_x1 = x + w; - if (y + h > crop_y1) - crop_y1 = y + h; + int x0, y0, x1, y1; + if (!upipe_avcdec_sub_rect_bbox(r, w, h, &x0, &y0, &x1, &y1)) + continue; + if (x + x0 < crop_x0) + crop_x0 = x + x0; + if (y + y0 < crop_y0) + crop_y0 = y + y0; + if (x + x1 > crop_x1) + crop_x1 = x + x1; + if (y + y1 > crop_y1) + crop_y1 = y + y1; } if (crop_x1 <= crop_x0 || crop_y1 <= crop_y0) { - /* No bitmap: a minimal transparent picture still replaces the + /* Nothing visible: a minimal transparent picture still replaces the * previous subpicture downstream. */ crop_x0 = crop_y0 = 0; crop_x1 = width < 16 ? width : 16; @@ -1095,6 +1154,21 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, h = height - y; } + /* The crop box only covers the visible pixels, so it may be + * tighter than the rect. */ + const int skip_x = crop_x0 > x ? crop_x0 - x : 0; + const int skip_y = crop_y0 > y ? crop_y0 - y : 0; + if (x + w > crop_x1) + w = crop_x1 - x; + if (y + h > crop_y1) + h = crop_y1 - y; + w -= skip_x; + h -= skip_y; + if (w <= 0 || h <= 0) + continue; + x += skip_x; + y += skip_y; + /* 4 octets per pixel */ uint8_t *dst = buf + stride * (y - crop_y0) + 4 * (x - crop_x0); @@ -1103,6 +1177,7 @@ static void upipe_avcdec_output_sub(struct upipe *upipe, AVSubtitle *sub, uint8_t *src = r->data[0]; uint8_t *palette = r->data[1]; const int src_stride = r->linesize[0]; + src += (size_t)src_stride * skip_y + skip_x; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) {