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 },
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;
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;
}
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;
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];
}
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];
}
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);
{ NULL }
};
-AVFilter ff_vf_colorlevels = {
+const AVFilter ff_vf_colorlevels = {
.name = "colorlevels",
.description = NULL_IF_CONFIG_SMALL("Adjust the color levels."),
.priv_size = sizeof(ColorLevelsContext),
.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,
};