From: Raja-89 via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Raja-89 <code@ffmpeg.org>
Subject: [FFmpeg-devel] [PR] avfilter/vf_blackframe: add slice threading using stdatomic (PR #21407)
Date: Wed, 07 Jan 2026 14:21:46 -0000
Message-ID: <176779570667.25.14493060368576644874@4457048688e7> (raw)
PR #21407 opened by Raja-89
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21407
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21407.patch
This patch implements slice threading for the blackframe filter to improve processing performance.
To avoid unnecessary memory allocation and reduce overhead, the implementation uses a single stdatomic atomic integer for the global black pixel count across all threads, as requested during review.
Changes in v9:
- Implemented slice threading using ff_filter_execute.
- Replaced heap allocation (av_calloc) with stdatomic (atomic_uint) for the pixel counter.
- Enabled AVFILTER_FLAG_SLICE_THREADS.
- Updated height comment to Doxygen style (///<) and restored original struct comments.
Signed-off-by: Raja Rathour <imraja729@gmail.com>
>From 87328a4c047ad3dd803208f823c14758c0bb83bb Mon Sep 17 00:00:00 2001
From: Raja Rathour <imraja729@gmail.com>
Date: Wed, 7 Jan 2026 19:46:56 +0530
Subject: [PATCH] avfilter/vf_blackframe: add slice threading using stdatomic
Signed-off-by: Raja Rathour <imraja729@gmail.com>
---
libavfilter/vf_blackframe.c | 58 ++++++++++++++++++++++++++++++-------
1 file changed, 47 insertions(+), 11 deletions(-)
diff --git a/libavfilter/vf_blackframe.c b/libavfilter/vf_blackframe.c
index f0aa53e133..d45fef5d14 100644
--- a/libavfilter/vf_blackframe.c
+++ b/libavfilter/vf_blackframe.c
@@ -29,6 +29,7 @@
#include <stdio.h>
#include <inttypes.h>
+#include <stdatomic.h>
#include "libavutil/internal.h"
#include "libavutil/opt.h"
@@ -41,10 +42,19 @@ typedef struct BlackFrameContext {
int bamount; ///< black amount
int bthresh; ///< black threshold
unsigned int frame; ///< frame number
- unsigned int nblack; ///< number of black pixels counted so far
+ atomic_uint nblack; ///< number of black pixels counted so far
unsigned int last_keyframe; ///< frame number of the last received key-frame
} BlackFrameContext;
+typedef struct ThreadData {
+ const uint8_t *data;
+ int linesize;
+ int bthresh;
+ int width;
+ int height;
+ BlackFrameContext *s;
+} ThreadData;
+
static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NV12,
AV_PIX_FMT_NV21, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV411P,
@@ -55,26 +65,53 @@ static const enum AVPixelFormat pix_fmts[] = {
snprintf(buf, sizeof(buf), format, value); \
av_dict_set(metadata, key, buf, 0)
+static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+ ThreadData *td = arg;
+ int slice_start = (td->height * jobnr) / nb_jobs;
+ int slice_end = (td->height * (jobnr+1)) / nb_jobs;
+ int x, y;
+ const uint8_t *p;
+ unsigned int black_pixels_count = 0;
+
+ p = td->data + slice_start * td->linesize;
+
+ for (y = slice_start; y < slice_end; y++) {
+ for (x = 0; x < td->width; x++)
+ black_pixels_count += p[x] < td->bthresh;
+ p += td->linesize;
+ }
+
+ atomic_fetch_add(&td->s->nblack, black_pixels_count);
+ return 0;
+}
+
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
AVFilterContext *ctx = inlink->dst;
BlackFrameContext *s = ctx->priv;
- int x, i;
+ ThreadData td;
int pblack = 0;
- uint8_t *p = frame->data[0];
+ int nb_threads = ff_filter_get_nb_threads(ctx);
+ int nb_jobs = FFMIN(inlink->h, nb_threads);
AVDictionary **metadata;
char buf[32];
- for (i = 0; i < frame->height; i++) {
- for (x = 0; x < inlink->w; x++)
- s->nblack += p[x] < s->bthresh;
- p += frame->linesize[0];
- }
+ atomic_init(&s->nblack, 0);
+
+ td.data = frame->data[0];
+ td.linesize = frame->linesize[0];
+ td.width = inlink->w;
+ td.height = inlink->h;
+ td.bthresh = s->bthresh;
+ td.s = s;
+
+ ff_filter_execute(ctx, blackframe_slice, &td, NULL, nb_jobs);
if (frame->flags & AV_FRAME_FLAG_KEY)
s->last_keyframe = s->frame;
- pblack = s->nblack * 100 / (inlink->w * inlink->h);
+ pblack = atomic_load(&s->nblack) * 100 / (inlink->w * inlink->h);
if (pblack >= s->bamount) {
metadata = &frame->metadata;
@@ -88,7 +125,6 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
}
s->frame++;
- s->nblack = 0;
return ff_filter_frame(inlink->dst->outputs[0], frame);
}
@@ -118,7 +154,7 @@ const FFFilter ff_vf_blackframe = {
.p.name = "blackframe",
.p.description = NULL_IF_CONFIG_SMALL("Detect frames that are (almost) black."),
.p.priv_class = &blackframe_class,
- .p.flags = AVFILTER_FLAG_METADATA_ONLY,
+ .p.flags = AVFILTER_FLAG_METADATA_ONLY | AVFILTER_FLAG_SLICE_THREADS,
.priv_size = sizeof(BlackFrameContext),
FILTER_INPUTS(avfilter_vf_blackframe_inputs),
FILTER_OUTPUTS(ff_video_default_filterpad),
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
reply other threads:[~2026-01-07 14:22 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=176779570667.25.14493060368576644874@4457048688e7 \
--to=ffmpeg-devel@ffmpeg.org \
--cc=code@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git