From: Raja Rathour via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Raja Rathour <imraja729@gmail.com>
Subject: [FFmpeg-devel] [PATCH v6] avfilter/vf_blackframe: add slice threading
Date: Tue, 30 Dec 2025 23:23:44 +0530
Message-ID: <20251230175442.71079-1-imraja729@gmail.com> (raw)
In-Reply-To: <20251230173156.62073-1-imraja729@gmail.com>
v6: Rebased on master to resolve automated Patchwork application failures.
This patch adds slice threading support to the blackframe filter,
improving performance on multi-core systems.
Changes in v5:
- Removed meta-comments regarding C90 compliance as requested.
- Removed redundant null checks for ThreadData pointers.
- Renamed the thread results array to 'nb_black_pixels_per_slice' for clarity.
Signed-off-by: Raja Rathour <imraja729@gmail.com>
---
libavfilter/vf_blackframe.c | 73 ++++++++++++++++---------------------
1 file changed, 31 insertions(+), 42 deletions(-)
diff --git a/libavfilter/vf_blackframe.c b/libavfilter/vf_blackframe.c
index cb566d103d..fb531b8a4c 100644
--- a/libavfilter/vf_blackframe.c
+++ b/libavfilter/vf_blackframe.c
@@ -39,19 +39,20 @@
typedef struct BlackFrameContext {
const AVClass *class;
- int bamount; ///< black amount
- int bthresh; ///< black threshold
- unsigned int frame; ///< frame number
- unsigned int nblack; ///< number of black pixels counted so far
- unsigned int last_keyframe; ///< frame number of the last received key-frame
+ int bamount;
+ int bthresh;
+ unsigned int frame;
+ unsigned int nblack;
+ unsigned int last_keyframe;
} BlackFrameContext;
typedef struct ThreadData {
- const uint8_t *data; // Pointer to the image data
- int linesize; // How wide is the memory line
- int bthresh; // The black threshold
- int width; // Image width
- unsigned int *counts; // POINTER to the array where threads write results
+ const uint8_t *data;
+ int linesize;
+ int bthresh;
+ int width;
+ int height; // Added height for cleaner slice math
+ unsigned int *counts;
} ThreadData;
static const enum AVPixelFormat pix_fmts[] = {
@@ -67,25 +68,21 @@ static const enum AVPixelFormat pix_fmts[] = {
static int blackframe_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
{
ThreadData *td = arg;
- // Calculate vertical slice
- int slice_start = (ctx->inputs[0]->h * jobnr) / nb_jobs;
- int slice_end = (ctx->inputs[0]->h * (jobnr+1)) / nb_jobs;
-
- // Safety check for pointers
- if (!td || !td->data || !td->counts) return 0;
-
- const uint8_t *p = td->data + slice_start * td->linesize;
- unsigned int local_nblack = 0;
+ 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++)
- local_nblack += p[x] < td->bthresh;
+ black_pixels_count += p[x] < td->bthresh;
p += td->linesize;
}
- // Save my private count
- td->counts[jobnr] = local_nblack;
+ td->counts[jobnr] = black_pixels_count;
return 0;
}
@@ -97,39 +94,31 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
AVDictionary **metadata;
char buf[32];
ThreadData td;
+ int nb_threads, nb_jobs, i;
+ unsigned int *nb_black_pixels_per_slice;
+
+ nb_threads = ff_filter_get_nb_threads(ctx);
- // 1. Get thread count
- int nb_threads = ff_filter_get_nb_threads(ctx);
-
- // Allocate memory for thread results
- unsigned int *thread_counts = av_calloc(nb_threads, sizeof(*thread_counts));
- if (!thread_counts) {
- av_log(ctx, AV_LOG_ERROR, "Failed to allocate thread_counts\n");
+ nb_black_pixels_per_slice = av_calloc(nb_threads, sizeof(*nb_black_pixels_per_slice));
+ if (!nb_black_pixels_per_slice)
return AVERROR(ENOMEM);
- }
- // 3. Prepare the data package
td.data = frame->data[0];
td.linesize = frame->linesize[0];
td.width = inlink->w;
+ td.height = inlink->h;
td.bthresh = s->bthresh;
- td.counts = thread_counts;
+ td.counts = nb_black_pixels_per_slice;
- // 4. Run the threads!
- // We calculate the exact number of jobs we are about to run
- int nb_jobs = FFMIN(frame->height, nb_threads);
+ nb_jobs = FFMIN(td.height, nb_threads);
ff_filter_execute(ctx, blackframe_slice, &td, NULL, nb_jobs);
- // 5. THE REDUCE STEP: Sum up the results
s->nblack = 0;
- // Only sum up the jobs that actually ran
- for (int i = 0; i < nb_jobs; i++) {
- s->nblack += thread_counts[i];
+ for (i = 0; i < nb_jobs; i++) {
+ s->nblack += nb_black_pixels_per_slice[i];
}
- // --- FROM HERE DOWN, THE CODE IS THE SAME AS THE ORIGINAL ---
-
if (frame->flags & AV_FRAME_FLAG_KEY)
s->last_keyframe = s->frame;
@@ -149,7 +138,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
s->frame++;
s->nblack = 0;
- av_free(thread_counts);
+ av_free(nb_black_pixels_per_slice);
return ff_filter_frame(inlink->dst->outputs[0], frame);
}
--
2.48.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
next prev parent reply other threads:[~2025-12-30 17:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-23 16:44 [FFmpeg-devel] [PATCH v4] " Raja Rathour via ffmpeg-devel
2025-12-27 19:53 ` [FFmpeg-devel] " Michael Niedermayer via ffmpeg-devel
2025-12-30 17:31 ` [FFmpeg-devel] [PATCH v5] " Raja Rathour via ffmpeg-devel
2025-12-30 17:53 ` Raja Rathour via ffmpeg-devel [this message]
2025-12-30 19:06 ` [FFmpeg-devel] Re: [PATCH v6] " Michael Niedermayer via ffmpeg-devel
2025-12-30 19:40 ` [FFmpeg-devel] [PATCH v7] " Raja Rathour via ffmpeg-devel
2025-12-31 2:06 ` [FFmpeg-devel] " Michael Niedermayer via ffmpeg-devel
2026-01-01 6:39 ` [FFmpeg-devel] [PATCH v8] " Raja Rathour via ffmpeg-devel
2026-01-04 22:15 ` [FFmpeg-devel] " Marton Balint via ffmpeg-devel
2026-01-07 14:38 ` Raja Rathour via ffmpeg-devel
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=20251230175442.71079-1-imraja729@gmail.com \
--to=ffmpeg-devel@ffmpeg.org \
--cc=imraja729@gmail.com \
/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