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
47 unsigned int nb_black_pixels; ///< number of black pixels counted so far47 unsigned int nb_black_pixels; ///< number of black pixels counted so far
48 AVRational time_base;48 AVRational time_base;
49 int depth;49 int depth;
50 int nb_threads;
51 unsigned int *counter;
50} BlackDetectContext;52} BlackDetectContext;
51 53
52#define OFFSET(x) offsetof(BlackDetectContext, x)54#define OFFSET(x) offsetof(BlackDetectContext, x)
@@ -112,8 +114,12 @@ static int config_input(AVFilterLink *inlink) · 59 unmodified lines
112 const int factor = (1 << (depth - 8));114 const int factor = (1 << (depth - 8));
113 115
114 s->depth = depth;116 s->depth = depth;
117 s->nb_threads = ff_filter_get_nb_threads(ctx);
115 s->time_base = inlink->time_base;118 s->time_base = inlink->time_base;
116 s->black_min_duration = s->black_min_duration_time / av_q2d(s->time_base);119 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);
117 123
118 s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?124 s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ?
119 // luminance_minimum_value + pixel_black_th * luminance_range_size125 // luminance_minimum_value + pixel_black_th * luminance_range_size
@@ -141,30 +147,56 @@ static void check_black_end(AVFilterContext *ctx) · 21 unmodified lines
141 }147 }
142}148}
143 149
144static int filter_frame(AVFilterLink *inlink, AVFrame *picref)150static int black_counter(AVFilterContext *ctx, void *arg,
151 int jobnr, int nb_jobs)
145{152{
146 AVFilterContext *ctx = inlink->dst;
147 BlackDetectContext *s = ctx->priv;153 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;
149 164
150 if (s->depth == 8) {165 if (s->depth == 8) {
151 const uint8_t *p = picref->data[0];166 const uint8_t *p = in->data[0] + start * linesize;
152 167
153 for (int i = 0; i < inlink->h; i++) {168 for (int i = 0; i < size; i++) {
154 for (int x = 0; x < inlink->w; x++)169 for (int x = 0; x < w; x++)
155 s->nb_black_pixels += p[x] <= s->pixel_black_th_i;170 counter += p[x] <= threshold;
156 p += picref->linesize[0];171 p += linesize;
157 }172 }
158 } else {173 } 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);
160 175
161 for (int i = 0; i < inlink->h; i++) {176 for (int i = 0; i < size; i++) {
162 for (int x = 0; x < inlink->w; x++)177 for (int x = 0; x < w; x++)
163 s->nb_black_pixels += p[x] <= s->pixel_black_th_i;178 counter += p[x] <= threshold;
164 p += picref->linesize[0] / 2;179 p += linesize / 2;
165 }180 }
166 }181 }
167 182
183 *counterp = counter;
184
185 return 0;
186}
187
188static 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
168 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);200 picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h);
169 201
170 av_log(ctx, AV_LOG_DEBUG,202 av_log(ctx, AV_LOG_DEBUG,
@@ -199,6 +231,8 @@ static av_cold void uninit(AVFilterContext *ctx) · 28 unmodified lines
199{231{
200 BlackDetectContext *s = ctx->priv;232 BlackDetectContext *s = ctx->priv;
201 233
234 av_freep(&s->counter);
235
202 if (s->black_started) {236 if (s->black_started) {
203 // FIXME: black_end should be set to last_picref_pts + last_picref_duration237 // FIXME: black_end should be set to last_picref_pts + last_picref_duration
204 s->black_end = s->last_picref_pts;238 s->black_end = s->last_picref_pts;
@@ -233,4 +267,5 @@ AVFilter ff_vf_blackdetect = { · 28 unmodified lines
233 .outputs = blackdetect_outputs,267 .outputs = blackdetect_outputs,
234 .uninit = uninit,268 .uninit = uninit,
235 .priv_class = &blackdetect_class,269 .priv_class = &blackdetect_class,
270 .flags = AVFILTER_FLAG_SLICE_THREADS,
236};271};

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