X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fvf_colorlevels.c;h=9085e409745749532927ab01c814d994154e807d;hb=3a370868dc33061a20d1fd99274e65167d7a78ac;hp=5385a5e75425ab2ed1fcebf2b2c5eaf846c1f9c7;hpb=c6892f59eb0e9f2a9ec1f55b21a5841a60540e1f;p=ffmpeg diff --git a/libavfilter/vf_colorlevels.c b/libavfilter/vf_colorlevels.c index 5385a5e7542..9085e409745 100644 --- a/libavfilter/vf_colorlevels.c +++ b/libavfilter/vf_colorlevels.c @@ -40,15 +40,18 @@ typedef struct Range { typedef struct ColorLevelsContext { const AVClass *class; Range range[4]; + int nb_comp; int bpp; int step; uint8_t rgba_map[4]; int linesize; + + int (*colorlevels_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); } ColorLevelsContext; #define OFFSET(x) offsetof(ColorLevelsContext, x) -#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM static const AVOption colorlevels_options[] = { { "rimin", "set input red black point", OFFSET(range[R].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS }, { "gimin", "set input green black point", OFFSET(range[G].in_min), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS }, @@ -90,6 +93,74 @@ static int query_formats(AVFilterContext *ctx) return ff_set_common_formats(ctx, fmts_list); } +typedef struct ThreadData { + const uint8_t *srcrow; + uint8_t *dstrow; + int dst_linesize; + int src_linesize; + + float coeff[4]; + + int h; + + int imin[4]; + int omin[4]; +} ThreadData; + +#define LOAD_COMMON \ + ColorLevelsContext *s = ctx->priv; \ + const ThreadData *td = arg; \ + const int process_h = td->h; \ + const int slice_start = (process_h * jobnr ) / nb_jobs; \ + const int slice_end = (process_h * (jobnr+1)) / nb_jobs; \ + const uint8_t *srcrow = td->srcrow; \ + uint8_t *dstrow = td->dstrow; \ + const int step = s->step; + +static int colorlevels_slice_8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + LOAD_COMMON + + for (int comp = 0; comp < s->nb_comp; comp++) { + const uint8_t offset = s->rgba_map[comp]; + const int imin = td->imin[comp]; + const int omin = td->omin[comp]; + const float coeff = td->coeff[comp]; + + for (int y = slice_start; y < slice_end; y++) { + const uint8_t *src = srcrow + y * td->src_linesize; + uint8_t *dst = dstrow + y * td->dst_linesize; + + for (int x = 0; x < s->linesize; x += step) + dst[x + offset] = av_clip_uint8((src[x + offset] - imin) * coeff + omin); + } + } + + return 0; +} + +static int colorlevels_slice_16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + LOAD_COMMON + + for (int comp = 0; comp < s->nb_comp; comp++) { + const uint8_t offset = s->rgba_map[comp]; + const int imin = td->imin[comp]; + const int omin = td->omin[comp]; + const float coeff = td->coeff[comp]; + + for (int y = slice_start; y < slice_end; y++) { + const uint16_t *src = (const uint16_t *)(srcrow + y * td->src_linesize); + uint16_t *dst = (uint16_t *)(dstrow + y * td->dst_linesize); + + for (int x = 0; x < s->linesize; x += step) + dst[x + offset] = av_clip_uint16((src[x + offset] - imin) * coeff + omin); + } + } + + return 0; +} + static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; @@ -98,10 +169,14 @@ static int config_input(AVFilterLink *inlink) s->nb_comp = desc->nb_components; s->bpp = desc->comp[0].depth >> 3; - s->step = (av_get_padded_bits_per_pixel(desc) >> 3) / s->bpp; + s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->bpp == 2)); s->linesize = inlink->w * s->step; ff_fill_rgba_map(s->rgba_map, inlink->format); + s->colorlevels_slice = colorlevels_slice_8; + if (s->bpp == 2) + s->colorlevels_slice = colorlevels_slice_16; + return 0; } @@ -111,8 +186,8 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) ColorLevelsContext *s = ctx->priv; AVFilterLink *outlink = ctx->outputs[0]; const int step = s->step; + ThreadData td; AVFrame *out; - int x, y, i; if (av_frame_is_writable(in)) { out = in; @@ -125,25 +200,30 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) av_frame_copy_props(out, in); } + td.h = inlink->h; + td.dst_linesize = out->linesize[0]; + td.src_linesize = in->linesize[0]; + td.srcrow = in->data[0]; + td.dstrow = out->data[0]; + switch (s->bpp) { case 1: - for (i = 0; i < s->nb_comp; i++) { + for (int i = 0; i < s->nb_comp; i++) { Range *r = &s->range[i]; const uint8_t offset = s->rgba_map[i]; const uint8_t *srcrow = in->data[0]; - uint8_t *dstrow = out->data[0]; int imin = lrint(r->in_min * UINT8_MAX); int imax = lrint(r->in_max * UINT8_MAX); int omin = lrint(r->out_min * UINT8_MAX); int omax = lrint(r->out_max * UINT8_MAX); - double coeff; + float coeff; if (imin < 0) { imin = UINT8_MAX; - for (y = 0; y < inlink->h; y++) { + for (int y = 0; y < inlink->h; y++) { const uint8_t *src = srcrow; - for (x = 0; x < s->linesize; x += step) + for (int x = 0; x < s->linesize; x += step) imin = FFMIN(imin, src[x + offset]); srcrow += in->linesize[0]; } @@ -151,46 +231,39 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) if (imax < 0) { srcrow = in->data[0]; imax = 0; - for (y = 0; y < inlink->h; y++) { + for (int y = 0; y < inlink->h; y++) { const uint8_t *src = srcrow; - for (x = 0; x < s->linesize; x += step) + for (int x = 0; x < s->linesize; x += step) imax = FFMAX(imax, src[x + offset]); srcrow += in->linesize[0]; } } - srcrow = in->data[0]; coeff = (omax - omin) / (double)(imax - imin); - for (y = 0; y < inlink->h; y++) { - const uint8_t *src = srcrow; - uint8_t *dst = dstrow; - - for (x = 0; x < s->linesize; x += step) - dst[x + offset] = av_clip_uint8((src[x + offset] - imin) * coeff + omin); - dstrow += out->linesize[0]; - srcrow += in->linesize[0]; - } + + td.coeff[i] = coeff; + td.imin[i] = imin; + td.omin[i] = omin; } break; case 2: - for (i = 0; i < s->nb_comp; i++) { + for (int i = 0; i < s->nb_comp; i++) { Range *r = &s->range[i]; const uint8_t offset = s->rgba_map[i]; const uint8_t *srcrow = in->data[0]; - uint8_t *dstrow = out->data[0]; int imin = lrint(r->in_min * UINT16_MAX); int imax = lrint(r->in_max * UINT16_MAX); int omin = lrint(r->out_min * UINT16_MAX); int omax = lrint(r->out_max * UINT16_MAX); - double coeff; + float coeff; if (imin < 0) { imin = UINT16_MAX; - for (y = 0; y < inlink->h; y++) { + for (int y = 0; y < inlink->h; y++) { const uint16_t *src = (const uint16_t *)srcrow; - for (x = 0; x < s->linesize; x += step) + for (int x = 0; x < s->linesize; x += step) imin = FFMIN(imin, src[x + offset]); srcrow += in->linesize[0]; } @@ -198,29 +271,27 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) if (imax < 0) { srcrow = in->data[0]; imax = 0; - for (y = 0; y < inlink->h; y++) { + for (int y = 0; y < inlink->h; y++) { const uint16_t *src = (const uint16_t *)srcrow; - for (x = 0; x < s->linesize; x += step) + for (int x = 0; x < s->linesize; x += step) imax = FFMAX(imax, src[x + offset]); srcrow += in->linesize[0]; } } - srcrow = in->data[0]; coeff = (omax - omin) / (double)(imax - imin); - for (y = 0; y < inlink->h; y++) { - const uint16_t *src = (const uint16_t*)srcrow; - uint16_t *dst = (uint16_t *)dstrow; - - for (x = 0; x < s->linesize; x += step) - dst[x + offset] = av_clip_uint16((src[x + offset] - imin) * coeff + omin); - dstrow += out->linesize[0]; - srcrow += in->linesize[0]; - } + + td.coeff[i] = coeff; + td.imin[i] = imin; + td.omin[i] = omin; } + break; } + ctx->internal->execute(ctx, s->colorlevels_slice, &td, NULL, + FFMIN(inlink->h, ff_filter_get_nb_threads(ctx))); + if (in != out) av_frame_free(&in); return ff_filter_frame(outlink, out); @@ -252,5 +323,6 @@ AVFilter ff_vf_colorlevels = { .query_formats = query_formats, .inputs = colorlevels_inputs, .outputs = colorlevels_outputs, - .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, };