]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_colorbalance.c
avfilter: Constify all AVFilters
[ffmpeg] / libavfilter / vf_colorbalance.c
index eb870ac2ad6e4809e261413c5ac01fc9848c52f0..4e13ce97b9a9497afde4499dd3d23499a6992250 100644 (file)
@@ -36,9 +36,9 @@ typedef struct ThreadData {
 } ThreadData;
 
 typedef struct Range {
-    double shadows;
-    double midtones;
-    double highlights;
+    float shadows;
+    float midtones;
+    float highlights;
 } Range;
 
 typedef struct ColorBalanceContext {
@@ -46,26 +46,29 @@ typedef struct ColorBalanceContext {
     Range cyan_red;
     Range magenta_green;
     Range yellow_blue;
+    int preserve_lightness;
 
     uint8_t rgba_map[4];
     int depth;
+    int max;
     int step;
 
     int (*color_balance)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
 } ColorBalanceContext;
 
 #define OFFSET(x) offsetof(ColorBalanceContext, 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 colorbalance_options[] = {
-    { "rs", "set red shadows",      OFFSET(cyan_red.shadows),         AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "gs", "set green shadows",    OFFSET(magenta_green.shadows),    AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "bs", "set blue shadows",     OFFSET(yellow_blue.shadows),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "rm", "set red midtones",     OFFSET(cyan_red.midtones),        AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "gm", "set green midtones",   OFFSET(magenta_green.midtones),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "bm", "set blue midtones",    OFFSET(yellow_blue.midtones),     AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "rh", "set red highlights",   OFFSET(cyan_red.highlights),      AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
-    { "bh", "set blue highlights",  OFFSET(yellow_blue.highlights),   AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, FLAGS },
+    { "rs", "set red shadows",      OFFSET(cyan_red.shadows),         AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "gs", "set green shadows",    OFFSET(magenta_green.shadows),    AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "bs", "set blue shadows",     OFFSET(yellow_blue.shadows),      AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "rm", "set red midtones",     OFFSET(cyan_red.midtones),        AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "gm", "set green midtones",   OFFSET(magenta_green.midtones),   AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "bm", "set blue midtones",    OFFSET(yellow_blue.midtones),     AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "rh", "set red highlights",   OFFSET(cyan_red.highlights),      AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "gh", "set green highlights", OFFSET(magenta_green.highlights), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "bh", "set blue highlights",  OFFSET(yellow_blue.highlights),   AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, FLAGS },
+    { "pl", "preserve lightness",   OFFSET(preserve_lightness),       AV_OPT_TYPE_BOOL,  {.i64=0},  0, 1, FLAGS },
     { NULL }
 };
 
@@ -95,26 +98,61 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
-static int get_component(int c, int depth, double l,
-                         double s, double m, double h)
+static float get_component(float v, float l,
+                           float s, float m, float h)
 {
-    const double a = 4, b = 0.333, scale = 0.7;
-    const double max = (1 << depth) - 1;
-    double v = c / max;
+    const float a = 4.f, b = 0.333f, scale = 0.7f;
 
-    l /= max;
-
-    s *= av_clipd((b - l) * a + 0.5, 0, 1) * scale;
-    m *= av_clipd((l - b) * a + 0.5, 0, 1) * av_clipd((1.0 - l - b) * a + 0.5, 0, 1) * scale;
-    h *= av_clipd((l + b - 1) * a + 0.5, 0, 1) * scale;
+    s *= av_clipf((b - l) * a + 0.5f, 0, 1) * scale;
+    m *= av_clipf((l - b) * a + 0.5f, 0, 1) * av_clipf((1.0 - l - b) * a + 0.5f, 0, 1) * scale;
+    h *= av_clipf((l + b - 1) * a + 0.5f, 0, 1) * scale;
 
     v += s;
     v += m;
     v += h;
-    v = av_clipd(v, 0, 1);
-    c = v * max;
 
-    return av_clip_uintp2_c(c, depth);
+    return av_clipf(v, 0, 1);
+}
+
+static float hfun(float n, float h, float s, float l)
+{
+    float a = s * FFMIN(l, 1. - l);
+    float k = fmodf(n + h / 30.f, 12.f);
+
+    return av_clipf(l - a * FFMAX(FFMIN3(k - 3.f, 9.f - k, 1), -1.f), 0, 1);
+}
+
+static void preservel(float *r, float *g, float *b, float l)
+{
+    float max = FFMAX3(*r, *g, *b);
+    float min = FFMIN3(*r, *g, *b);
+    float h, s;
+
+    l *= 0.5;
+
+    if (*r == *g && *g == *b) {
+        h = 0.;
+    } else if (max == *r) {
+        h = 60. * (0. + (*g - *b) / (max - min));
+    } else if (max == *g) {
+        h = 60. * (2. + (*b - *r) / (max - min));
+    } else if (max == *b) {
+        h = 60. * (4. + (*r - *g) / (max - min));
+    } else {
+        h = 0.;
+    }
+    if (h < 0.)
+        h += 360.;
+
+    if (max == 0. || min == 1.) {
+        s = 0.;
+    } else {
+        s = (max - min) / (1. - FFABS(2. * l - 1));
+    }
+
+    *r = hfun(0, h, s, l);
+    *g = hfun(8, h, s, l);
+    *b = hfun(4, h, s, l);
 }
 
 static int color_balance8_p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
@@ -133,19 +171,26 @@ static int color_balance8_p(AVFilterContext *ctx, void *arg, int jobnr, int nb_j
     uint8_t *dstb = out->data[1] + slice_start * out->linesize[1];
     uint8_t *dstr = out->data[2] + slice_start * out->linesize[2];
     uint8_t *dsta = out->data[3] + slice_start * out->linesize[3];
-    const int depth = s->depth;
+    const float max = s->max;
     int i, j;
 
     for (i = slice_start; i < slice_end; i++) {
         for (j = 0; j < out->width; j++) {
-            const int r = srcr[j];
-            const int g = srcg[j];
-            const int b = srcb[j];
-            const int l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
-
-            dstr[j] = get_component(r, depth, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
-            dstg[j] = get_component(g, depth, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
-            dstb[j] = get_component(b, depth, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+            float r = srcr[j] / max;
+            float g = srcg[j] / max;
+            float b = srcb[j] / max;
+            const float l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
+
+            r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
+            g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
+            b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+
+            if (s->preserve_lightness)
+                preservel(&r, &g, &b, l);
+
+            dstr[j] = av_clip_uint8(lrintf(r * max));
+            dstg[j] = av_clip_uint8(lrintf(g * max));
+            dstb[j] = av_clip_uint8(lrintf(b * max));
             if (in != out && out->linesize[3])
                 dsta[j] = srca[j];
         }
@@ -180,18 +225,26 @@ static int color_balance16_p(AVFilterContext *ctx, void *arg, int jobnr, int nb_
     uint16_t *dstr = (uint16_t *)out->data[2] + slice_start * out->linesize[2] / 2;
     uint16_t *dsta = (uint16_t *)out->data[3] + slice_start * out->linesize[3] / 2;
     const int depth = s->depth;
+    const float max = s->max;
     int i, j;
 
     for (i = slice_start; i < slice_end; i++) {
         for (j = 0; j < out->width; j++) {
-            const int r = srcr[j];
-            const int g = srcg[j];
-            const int b = srcb[j];
-            const int l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
-
-            dstr[j] = get_component(r, depth, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
-            dstg[j] = get_component(g, depth, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
-            dstb[j] = get_component(b, depth, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+            float r = srcr[j] / max;
+            float g = srcg[j] / max;
+            float b = srcb[j] / max;
+            const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
+
+            r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
+            g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
+            b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+
+            if (s->preserve_lightness)
+                preservel(&r, &g, &b, l);
+
+            dstr[j] = av_clip_uintp2_c(lrintf(r * max), depth);
+            dstg[j] = av_clip_uintp2_c(lrintf(g * max), depth);
+            dstb[j] = av_clip_uintp2_c(lrintf(b * max), depth);
             if (in != out && out->linesize[3])
                 dsta[j] = srca[j];
         }
@@ -223,7 +276,7 @@ static int color_balance8(AVFilterContext *ctx, void *arg, int jobnr, int nb_job
     const uint8_t goffset = s->rgba_map[G];
     const uint8_t boffset = s->rgba_map[B];
     const uint8_t aoffset = s->rgba_map[A];
-    const int depth = s->depth;
+    const float max = s->max;
     const int step = s->step;
     uint8_t *dstrow;
     int i, j;
@@ -234,14 +287,21 @@ static int color_balance8(AVFilterContext *ctx, void *arg, int jobnr, int nb_job
         uint8_t *dst = dstrow;
 
         for (j = 0; j < outlink->w * step; j += step) {
-            const int r = src[j + roffset];
-            const int g = src[j + goffset];
-            const int b = src[j + boffset];
-            const int l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
-
-            dst[j + roffset] = get_component(r, depth, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
-            dst[j + goffset] = get_component(g, depth, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
-            dst[j + boffset] = get_component(b, depth, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+            float r = src[j + roffset] / max;
+            float g = src[j + goffset] / max;
+            float b = src[j + boffset] / max;
+            const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
+
+            r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
+            g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
+            b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+
+            if (s->preserve_lightness)
+                preservel(&r, &g, &b, l);
+
+            dst[j + roffset] = av_clip_uint8(lrintf(r * max));
+            dst[j + goffset] = av_clip_uint8(lrintf(g * max));
+            dst[j + boffset] = av_clip_uint8(lrintf(b * max));
             if (in != out && step == 4)
                 dst[j + aoffset] = src[j + aoffset];
         }
@@ -269,6 +329,7 @@ static int color_balance16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo
     const uint8_t aoffset = s->rgba_map[A];
     const int step = s->step / 2;
     const int depth = s->depth;
+    const float max = s->max;
     uint16_t *dstrow;
     int i, j;
 
@@ -278,14 +339,21 @@ static int color_balance16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jo
         uint16_t *dst = dstrow;
 
         for (j = 0; j < outlink->w * step; j += step) {
-            const int r = src[j + roffset];
-            const int g = src[j + goffset];
-            const int b = src[j + boffset];
-            const int l = FFMAX3(r, g, b) + FFMIN3(r, g, b);
-
-            dst[j + roffset] = get_component(r, depth, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
-            dst[j + goffset] = get_component(g, depth, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
-            dst[j + boffset] = get_component(b, depth, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+            float r = src[j + roffset] / max;
+            float g = src[j + goffset] / max;
+            float b = src[j + boffset] / max;
+            const float l = (FFMAX3(r, g, b) + FFMIN3(r, g, b));
+
+            r = get_component(r, l, s->cyan_red.shadows, s->cyan_red.midtones, s->cyan_red.highlights);
+            g = get_component(g, l, s->magenta_green.shadows, s->magenta_green.midtones, s->magenta_green.highlights);
+            b = get_component(b, l, s->yellow_blue.shadows, s->yellow_blue.midtones, s->yellow_blue.highlights);
+
+            if (s->preserve_lightness)
+                preservel(&r, &g, &b, l);
+
+            dst[j + roffset] = av_clip_uintp2_c(lrintf(r * max), depth);
+            dst[j + goffset] = av_clip_uintp2_c(lrintf(g * max), depth);
+            dst[j + boffset] = av_clip_uintp2_c(lrintf(b * max), depth);
             if (in != out && step == 4)
                 dst[j + aoffset] = src[j + aoffset];
         }
@@ -303,16 +371,17 @@ static int config_output(AVFilterLink *outlink)
     ColorBalanceContext *s = ctx->priv;
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
     const int depth = desc->comp[0].depth;
-    const int max = 1 << depth;
+    const int max = (1 << depth) - 1;
     const int planar = av_pix_fmt_count_planes(outlink->format) > 1;
 
     s->depth = depth;
+    s->max = max;
 
-    if (max == 256 && planar) {
+    if (max == 255 && planar) {
         s->color_balance = color_balance8_p;
     } else if (planar) {
         s->color_balance = color_balance16_p;
-    } else if (max == 256) {
+    } else if (max == 255) {
         s->color_balance = color_balance8;
     } else {
         s->color_balance = color_balance16;
@@ -370,7 +439,7 @@ static const AVFilterPad colorbalance_outputs[] = {
     { NULL }
 };
 
-AVFilter ff_vf_colorbalance = {
+const AVFilter ff_vf_colorbalance = {
     .name          = "colorbalance",
     .description   = NULL_IF_CONFIG_SMALL("Adjust the color balance."),
     .priv_size     = sizeof(ColorBalanceContext),
@@ -379,4 +448,5 @@ AVFilter ff_vf_colorbalance = {
     .inputs        = colorbalance_inputs,
     .outputs       = colorbalance_outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
+    .process_command = ff_filter_process_command,
 };