aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Haas <git@haasn.dev>2025-08-07 22:04:27 +0200
committerNiklas Haas <ffmpeg@haasn.dev>2025-08-08 11:29:27 +0000
commit3091bca3edb339a4d2ec8c64e52fa7e67a7d5005 (patch)
tree904ddb6a682110544a36349235cd3086fd8c862e
parent6627c8ea4b4e4e0f9312d3243c099eb5df1a89e7 (diff)
downloadffmpeg-3091bca3edb339a4d2ec8c64e52fa7e67a7d5005.tar.gz
avfilter/vf_libplacebo: skip rendering fully invisible planes
Sometimes, one input fully obscures another. In this case, we can skip actually rendering any input below the obscuring one. The reason I don't simply start the main render loop at `idx_start` will become apparent in the following commit. We can't use pl_frame_is_cropped() on this dummy frame, but we need to determine the reference frame before we can map the real output, so to resolve this conflict, we just reimplement the crop detection logic using the output link dimensions.
-rw-r--r--libavfilter/vf_libplacebo.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c
index caeebc25be..e62114199b 100644
--- a/libavfilter/vf_libplacebo.c
+++ b/libavfilter/vf_libplacebo.c
@@ -16,6 +16,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <math.h>
+
#include "libavutil/avassert.h"
#include "libavutil/eval.h"
#include "libavutil/fifo.h"
@@ -913,10 +915,39 @@ static int output_frame(AVFilterContext *ctx, int64_t pts)
pl_options opts = s->opts;
AVFilterLink *outlink = ctx->outputs[0];
const AVPixFmtDescriptor *outdesc = av_pix_fmt_desc_get(outlink->format);
+ const double target_pts = pts * av_q2d(outlink->time_base);
struct pl_frame target;
const AVFrame *ref = NULL;
AVFrame *out;
+ /* Count the number of visible inputs, by excluding frames which are fully
+ * obscured or which have no frames in the mix */
+ int idx_start = 0, nb_visible = 0;
+ for (int i = 0; i < s->nb_inputs; i++) {
+ LibplaceboInput *in = &s->inputs[i];
+ struct pl_frame dummy;
+ if (in->qstatus != PL_QUEUE_OK || !in->mix.num_frames)
+ continue;
+ const struct pl_frame *cur = pl_frame_mix_current(&in->mix);
+ update_crops(ctx, in, &dummy, target_pts);
+ const int x0 = roundf(FFMIN(dummy.crop.x0, dummy.crop.x1)),
+ y0 = roundf(FFMIN(dummy.crop.y0, dummy.crop.y1)),
+ x1 = roundf(FFMAX(dummy.crop.x0, dummy.crop.x1)),
+ y1 = roundf(FFMAX(dummy.crop.y0, dummy.crop.y1));
+
+ /* If an opaque frame covers entire the output, disregard all lower layers */
+ const bool cropped = x0 > 0 || y0 > 0 || x1 < outlink->w || y1 < outlink->h;
+ if (!cropped && cur->repr.alpha == PL_ALPHA_NONE) {
+ idx_start = i;
+ nb_visible = 0;
+ }
+ nb_visible++;
+ }
+
+ /* It should be impossible to call output_frame() without at least one
+ * valid nonempty frame mix */
+ av_assert1(nb_visible > 0);
+
/* Use the first active input as metadata reference */
for (int i = 0; i < s->nb_inputs; i++) {
const LibplaceboInput *in = &s->inputs[i];
@@ -989,7 +1020,8 @@ static int output_frame(AVFilterContext *ctx, int64_t pts)
struct pl_frame orig_target = target;
bool use_linear_compositor = false;
- if (s->linear_tex && target.color.transfer != PL_COLOR_TRC_LINEAR && !s->disable_linear) {
+ if (s->linear_tex && target.color.transfer != PL_COLOR_TRC_LINEAR &&
+ !s->disable_linear && nb_visible > 1) {
target = (struct pl_frame) {
.num_planes = 1,
.planes[0] = {
@@ -1017,10 +1049,10 @@ static int output_frame(AVFilterContext *ctx, int64_t pts)
FilterLink *il = ff_filter_link(ctx->inputs[i]);
FilterLink *ol = ff_filter_link(outlink);
int high_fps = av_cmp_q(il->frame_rate, ol->frame_rate) >= 0;
- if (in->qstatus != PL_QUEUE_OK || !in->mix.num_frames)
+ if (in->qstatus != PL_QUEUE_OK || !in->mix.num_frames || i < idx_start)
continue;
opts->params.skip_caching_single_frame = high_fps;
- update_crops(ctx, in, &target, out->pts * av_q2d(outlink->time_base));
+ update_crops(ctx, in, &target, target_pts);
pl_render_image_mix(in->renderer, &in->mix, &target, &opts->params);
/* Force straight output and set correct blend mode */