Showing the changes to libavfilter (1 of 1 file). Show all
Unified · Split
| @@ -47,6 +47,8 @@ typedef struct BlackDetectContext { · 46 unmodified lines | ||
| 47 | 47 | unsigned int nb_black_pixels; ///< number of black pixels counted so far |
| 48 | 48 | AVRational time_base; |
| 49 | 49 | int depth; |
| 50 | + int nb_threads; | |
| 51 | + unsigned int *counter; | |
| 50 | 52 | } BlackDetectContext; |
| 51 | 53 | |
| 52 | 54 | #define OFFSET(x) offsetof(BlackDetectContext, x) |
| @@ -112,8 +114,12 @@ static int config_input(AVFilterLink *inlink) · 59 unmodified lines | ||
| 112 | 114 | const int factor = (1 << (depth - 8)); |
| 113 | 115 | |
| 114 | 116 | s->depth = depth; |
| 117 | + s->nb_threads = ff_filter_get_nb_threads(ctx); | |
| 115 | 118 | s->time_base = inlink->time_base; |
| 116 | 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 | 124 | s->pixel_black_th_i = ff_fmt_is_in(inlink->format, yuvj_formats) ? |
| 119 | 125 | // 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 | |
| 144 | −static int filter_frame(AVFilterLink *inlink, AVFrame *picref) | |
| 150 | +static int black_counter(AVFilterContext *ctx, void *arg, | |
| 151 | + int jobnr, int nb_jobs) | |
| 145 | 152 | { |
| 146 | − AVFilterContext *ctx = inlink->dst; | |
| 147 | 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 | 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++) { | |
| 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; | |
| 157 | 172 | } |
| 158 | 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++) { | |
| 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; | |
| 165 | 180 | } |
| 166 | 181 | } |
| 167 | 182 | |
| 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 | + | |
| 168 | 200 | picture_black_ratio = (double)s->nb_black_pixels / (inlink->w * inlink->h); |
| 169 | 201 | |
| 170 | 202 | av_log(ctx, AV_LOG_DEBUG, |
| @@ -199,6 +231,8 @@ static av_cold void uninit(AVFilterContext *ctx) · 28 unmodified lines | ||
| 199 | 231 | { |
| 200 | 232 | BlackDetectContext *s = ctx->priv; |
| 201 | 233 | |
| 234 | + av_freep(&s->counter); | |
| 235 | + | |
| 202 | 236 | if (s->black_started) { |
| 203 | 237 | // FIXME: black_end should be set to last_picref_pts + last_picref_duration |
| 204 | 238 | s->black_end = s->last_picref_pts; |
| @@ -233,4 +267,5 @@ AVFilter ff_vf_blackdetect = { · 28 unmodified lines | ||
| 233 | 267 | .outputs = blackdetect_outputs, |
| 234 | 268 | .uninit = uninit, |
| 235 | 269 | .priv_class = &blackdetect_class, |
| 270 | + .flags = AVFILTER_FLAG_SLICE_THREADS, | |
| 236 | 271 | }; |