avfilter/vf_threshold: add slice threading
This commit is contained in:
@@ -68,12 +68,59 @@ static int query_formats(AVFilterContext *ctx)
|
|||||||
return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct ThreadData {
|
||||||
|
AVFrame *in;
|
||||||
|
AVFrame *threshold;
|
||||||
|
AVFrame *min;
|
||||||
|
AVFrame *max;
|
||||||
|
AVFrame *out;
|
||||||
|
} ThreadData;
|
||||||
|
|
||||||
|
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
||||||
|
{
|
||||||
|
ThresholdContext *s = ctx->priv;
|
||||||
|
ThreadData *td = arg;
|
||||||
|
AVFrame *min = td->min;
|
||||||
|
AVFrame *max = td->max;
|
||||||
|
AVFrame *threshold = td->threshold;
|
||||||
|
AVFrame *in = td->in;
|
||||||
|
AVFrame *out = td->out;
|
||||||
|
|
||||||
|
for (int p = 0; p < s->nb_planes; p++) {
|
||||||
|
const int h = s->height[p];
|
||||||
|
const int slice_start = (h * jobnr) / nb_jobs;
|
||||||
|
const int slice_end = (h * (jobnr+1)) / nb_jobs;
|
||||||
|
|
||||||
|
if (!(s->planes & (1 << p))) {
|
||||||
|
av_image_copy_plane(out->data[p] + slice_start * out->linesize[p],
|
||||||
|
out->linesize[p],
|
||||||
|
in->data[p] + slice_start * in->linesize[p],
|
||||||
|
in->linesize[p],
|
||||||
|
s->width[p] * s->bpc,
|
||||||
|
slice_end - slice_start);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
s->threshold(in->data[p] + slice_start * in->linesize[p],
|
||||||
|
threshold->data[p] + slice_start * threshold->linesize[p],
|
||||||
|
min->data[p] + slice_start * min->linesize[p],
|
||||||
|
max->data[p] + slice_start * max->linesize[p],
|
||||||
|
out->data[p] + slice_start * out->linesize[p],
|
||||||
|
in->linesize[p], threshold->linesize[p],
|
||||||
|
min->linesize[p], max->linesize[p],
|
||||||
|
out->linesize[p],
|
||||||
|
s->width[p], slice_end - slice_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int process_frame(FFFrameSync *fs)
|
static int process_frame(FFFrameSync *fs)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = fs->parent;
|
AVFilterContext *ctx = fs->parent;
|
||||||
ThresholdContext *s = fs->opaque;
|
ThresholdContext *s = fs->opaque;
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
AVFrame *out, *in, *threshold, *min, *max;
|
AVFrame *out, *in, *threshold, *min, *max;
|
||||||
|
ThreadData td;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
|
if ((ret = ff_framesync_get_frame(&s->fs, 0, &in, 0)) < 0 ||
|
||||||
@@ -87,29 +134,18 @@ static int process_frame(FFFrameSync *fs)
|
|||||||
if (!out)
|
if (!out)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
} else {
|
} else {
|
||||||
int p;
|
|
||||||
|
|
||||||
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
||||||
if (!out)
|
if (!out)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
av_frame_copy_props(out, in);
|
av_frame_copy_props(out, in);
|
||||||
|
|
||||||
for (p = 0; p < s->nb_planes; p++) {
|
td.out = out;
|
||||||
if (!(s->planes & (1 << p))) {
|
td.in = in;
|
||||||
av_image_copy_plane(out->data[p], out->linesize[p],
|
td.threshold = threshold;
|
||||||
in->data[p], in->linesize[p],
|
td.min = min;
|
||||||
s->width[p] * s->bpc,
|
td.max = max;
|
||||||
s->height[p]);
|
ctx->internal->execute(ctx, filter_slice, &td, NULL,
|
||||||
continue;
|
FFMIN(s->height[2], ff_filter_get_nb_threads(ctx)));
|
||||||
}
|
|
||||||
s->threshold(in->data[p], threshold->data[p],
|
|
||||||
min->data[p], max->data[p],
|
|
||||||
out->data[p],
|
|
||||||
in->linesize[p], threshold->linesize[p],
|
|
||||||
min->linesize[p], max->linesize[p],
|
|
||||||
out->linesize[p],
|
|
||||||
s->width[p], s->height[p]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
|
out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
|
||||||
@@ -324,5 +360,5 @@ AVFilter ff_vf_threshold = {
|
|||||||
.activate = activate,
|
.activate = activate,
|
||||||
.inputs = inputs,
|
.inputs = inputs,
|
||||||
.outputs = outputs,
|
.outputs = outputs,
|
||||||
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user