History

avfilter/vf_blackdetect: add slice threading support

Paul B Mahol committed 5 years ago

Commit 0959f951 parent 44e27d91 file · +48 −13Browse files

Unified · Split

libavfilter/vf_blackdetect.c+48−13
@@ -47,6 +47,8 @@ typedef struct BlackDetectContext { · 46 unmodified lines
4747 unsigned int nb_black_pixels; ///< number of black pixels counted so far
4848 AVRational time_base;
4949 int depth;
50+ int nb_threads;
51+ unsigned int *counter;
5052 } BlackDetectContext;
5153
5254 #define OFFSET(x) offsetof(BlackDetectContext, x)
@@ -112,8 +114,12 @@ static int config_input(AVFilterLink *inlink) · 59 unmodified lines
112114 const int factor = (1 << (depth - 8));
113115
114116 s->depth = depth;
117+ s->nb_threads = ff_filter_get_nb_threads(ctx);
115118 s->time_base = inlink->time_base;
116119 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);
120+ s->counter = av_calloc(s->nb_threads, sizeof(*s->counter));
121+ if (!s->counter)
122+ return AVERROR(ENOMEM);
117123
118124 s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
119125 // luminance_minimum_value + pixel_black_th * luminance_range_size
@@ -141,30 +147,56 @@ static void check_black_end(AVFilterContext *ctx) · 21 unmodified lines
141147 }
142148 }
143149
144−static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
150+static int black_counter(AVFilterContext *ctx, void *arg,
151+ int jobnr, int nb_jobs)
145152 {
146− AVFilterContext *ctx = inlink->dst;
147153 BlackDetectContext *s = ctx->priv;
148− double picture_black_ratio = 0;
154+ const unsigned int threshold = s->pixel_black_th_i;
155+ unsigned int *counterp = &s->counter[jobnr];
156+ AVFrame *in = arg;
157+ const int linesize = in->linesize[0];
158+ const int w = in->width;
159+ const int h = in->height;
160+ const int start = (h * jobnr) / nb_jobs;
161+ const int end = (h * (jobnr+1)) / nb_jobs;
162+ const int size = end - start;
163+ unsigned int counter = 0;
149164
150165 if (s->depth == 8) {
151− const uint8_t *p = picref->data[0];
166+ const uint8_t *p = in->data[0] + start * linesize;
152167
153− for (int i = 0; i < inlink->h; i++) {
154− for (int x = 0; x < inlink->w; x++)
155− s->nb_black_pixels += p[x] <= s->pixel_black_th_i;
156− p += picref->linesize[0];
168+ for (int i = 0; i < size; i++) {
169+ for (int x = 0; x < w; x++)
170+ counter += p[x] <= threshold;
171+ p += linesize;
157172 }
158173 } else {
159− const uint16_t *p = (const uint16_t *)picref->data[0];
174+ const uint16_t *p = (const uint16_t *)(in->data[0] + start * linesize);
160175
161− for (int i = 0; i < inlink->h; i++) {
162− for (int x = 0; x < inlink->w; x++)
163− s->nb_black_pixels += p[x] <= s->pixel_black_th_i;
164− p += picref->linesize[0] / 2;
176+ for (int i = 0; i < size; i++) {
177+ for (int x = 0; x < w; x++)
178+ counter += p[x] <= threshold;
179+ p += linesize / 2;
165180 }
166181 }
167182
183+ *counterp = counter;
184+
185+ return 0;
186+}
187+
188+static int filter_frame(AVFilterLink *inlink, AVFrame *picref)
189+{
190+ AVFilterContext *ctx = inlink->dst;
191+ BlackDetectContext *s = ctx->priv;
192+ double picture_black_ratio = 0;
193+
194+ ctx->internal->execute(ctx, black_counter, picref, NULL,
195+ FFMIN(inlink->h, s->nb_threads));
196+
197+ for (int i = 0; i < s->nb_threads; i++)
198+ s->nb_black_pixels += s->counter[i];
199+
168200 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
169201
170202 av_log(ctx, AV_LOG_DEBUG,
@@ -199,6 +231,8 @@ static av_cold void uninit(AVFilterContext *ctx) · 28 unmodified lines
199231 {
200232 BlackDetectContext *s = ctx->priv;
201233
234+ av_freep(&s->counter);
235+
202236 if (s->black_started) {
203237 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
204238 s->black_end = s->last_picref_pts;
@@ -233,4 +267,5 @@ AVFilter ff_vf_blackdetect = { · 28 unmodified lines
233267 .outputs = blackdetect_outputs,
234268 .uninit = uninit,
235269 .priv_class = &blackdetect_class,
270+ .flags = AVFILTER_FLAG_SLICE_THREADS,
236271 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t