]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/vf_curves.c
lavc/parser: export field order if not already set
[ffmpeg] / libavfilter / vf_curves.c
index 1c51c1baf7f43e091db4e5949beb84196ccbd1aa..69ec1084bb3e0933bda9ed3128897bb32fc10502 100644 (file)
@@ -63,10 +63,13 @@ typedef struct {
     int preset;
     char *comp_points_str[NB_COMP + 1];
     char *comp_points_str_all;
-    uint8_t graph[NB_COMP + 1][256];
+    uint16_t *graph[NB_COMP + 1];
+    int lut_size;
     char *psfile;
     uint8_t rgba_map[4];
     int step;
+    char *plot_filename;
+    int is_16bit;
 } CurvesContext;
 
 typedef struct ThreadData {
@@ -98,6 +101,7 @@ static const AVOption curves_options[] = {
     { "b",     "set blue points coordinates",  OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
     { "all",   "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
     { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
+    { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
     { NULL }
 };
 
@@ -110,25 +114,25 @@ static const struct {
     const char *master;
 } curves_presets[] = {
     [PRESET_COLOR_NEGATIVE] = {
-        "0/1 0.129/1 0.466/0.498 0.725/0 1/0",
-        "0/1 0.109/1 0.301/0.498 0.517/0 1/0",
-        "0/1 0.098/1 0.235/0.498 0.423/0 1/0",
+        "0.129/1 0.466/0.498 0.725/0",
+        "0.109/1 0.301/0.498 0.517/0",
+        "0.098/1 0.235/0.498 0.423/0",
     },
     [PRESET_CROSS_PROCESS] = {
-        "0.25/0.156 0.501/0.501 0.686/0.745",
-        "0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
-        "0.231/0.094 0.709/0.874",
+        "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
+        "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
+        "0/0 0.231/0.094 0.709/0.874 1/1",
     },
-    [PRESET_DARKER]             = { .master = "0.5/0.4" },
-    [PRESET_INCREASE_CONTRAST]  = { .master = "0.149/0.066 0.831/0.905 0.905/0.98" },
-    [PRESET_LIGHTER]            = { .master = "0.4/0.5" },
-    [PRESET_LINEAR_CONTRAST]    = { .master = "0.305/0.286 0.694/0.713" },
-    [PRESET_MEDIUM_CONTRAST]    = { .master = "0.286/0.219 0.639/0.643" },
+    [PRESET_DARKER]             = { .master = "0/0 0.5/0.4 1/1" },
+    [PRESET_INCREASE_CONTRAST]  = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
+    [PRESET_LIGHTER]            = { .master = "0/0 0.4/0.5 1/1" },
+    [PRESET_LINEAR_CONTRAST]    = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
+    [PRESET_MEDIUM_CONTRAST]    = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
     [PRESET_NEGATIVE]           = { .master = "0/1 1/0" },
-    [PRESET_STRONG_CONTRAST]    = { .master = "0.301/0.196 0.592/0.6 0.686/0.737" },
+    [PRESET_STRONG_CONTRAST]    = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
     [PRESET_VINTAGE] = {
         "0/0.11 0.42/0.51 1/0.95",
-        "0.50/0.48",
+        "0/0 0.50/0.48 1/1",
         "0/0.22 0.49/0.44 1/0.8",
     }
 };
@@ -145,10 +149,12 @@ static struct keypoint *make_point(double x, double y, struct keypoint *next)
     return point;
 }
 
-static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
+static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
+                            int lut_size)
 {
     char *p = (char *)s; // strtod won't alter the string
     struct keypoint *last = NULL;
+    const int scale = lut_size - 1;
 
     /* construct a linked list based on the key points string */
     while (p && *p) {
@@ -165,7 +171,7 @@ static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, cons
         if (!*points)
             *points = point;
         if (last) {
-            if ((int)(last->x * 255) >= (int)(point->x * 255)) {
+            if ((int)(last->x * scale) >= (int)(point->x * scale)) {
                 av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
                        "and (%f;%f) are too close from each other or not "
                        "strictly increasing on the x-axis\n",
@@ -177,28 +183,11 @@ static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, cons
         last = point;
     }
 
-    /* auto insert first key point if missing at x=0 */
-    if (!*points) {
-        last = make_point(0, 0, NULL);
-        if (!last)
-            return AVERROR(ENOMEM);
-        last->x = last->y = 0;
-        *points = last;
-    } else if ((*points)->x != 0.) {
-        struct keypoint *newfirst = make_point(0, 0, *points);
-        if (!newfirst)
-            return AVERROR(ENOMEM);
-        *points = newfirst;
-    }
-
-    av_assert0(last);
-
-    /* auto insert last key point if missing at x=1 */
-    if (last->x != 1.) {
-        struct keypoint *point = make_point(1, 1, NULL);
-        if (!point)
-            return AVERROR(ENOMEM);
-        last->next = point;
+    if (*points && !(*points)->next) {
+        av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
+               "this is unlikely to behave as you expect. You probably want"
+               "at least 2 points.",
+               (*points)->x, (*points)->y);
     }
 
     return 0;
@@ -219,17 +208,37 @@ static int get_nb_points(const struct keypoint *d)
  * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  */
-static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
+
+#define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uint16(v))
+
+static inline int interpolate(void *log_ctx, uint16_t *y,
+                              const struct keypoint *points, int nbits)
 {
     int i, ret = 0;
-    const struct keypoint *point;
+    const struct keypoint *point = points;
     double xprev = 0;
+    const int lut_size = 1<<nbits;
+    const int scale = lut_size - 1;
 
-    int n = get_nb_points(points); // number of splines
+    double (*matrix)[3];
+    double *h, *r;
+    const int n = get_nb_points(points); // number of splines
 
-    double (*matrix)[3] = av_calloc(n, sizeof(*matrix));
-    double *h = av_malloc((n - 1) * sizeof(*h));
-    double *r = av_calloc(n, sizeof(*r));
+    if (n == 0) {
+        for (i = 0; i < lut_size; i++)
+            y[i] = i;
+        return 0;
+    }
+
+    if (n == 1) {
+        for (i = 0; i < lut_size; i++)
+            y[i] = CLIP(point->y * scale);
+        return 0;
+    }
+
+    matrix = av_calloc(n, sizeof(*matrix));
+    h = av_malloc((n - 1) * sizeof(*h));
+    r = av_calloc(n, sizeof(*r));
 
     if (!matrix || !h || !r) {
         ret = AVERROR(ENOMEM);
@@ -248,9 +257,9 @@ static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *
     /* right-side of the polynomials, will be modified to contains the solution */
     point = points;
     for (i = 1; i < n - 1; i++) {
-        double yp = point->y,
-               yc = point->next->y,
-               yn = point->next->next->y;
+        const double yp = point->y;
+        const double yc = point->next->y;
+        const double yn = point->next->next->y;
         r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
         point = point->next;
     }
@@ -269,45 +278,54 @@ static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *
 
     /* tridiagonal solving of the linear system */
     for (i = 1; i < n; i++) {
-        double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
-        double k = den ? 1./den : 1.;
+        const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
+        const double k = den ? 1./den : 1.;
         matrix[i][AD] *= k;
         r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
     }
     for (i = n - 2; i >= 0; i--)
         r[i] = r[i] - matrix[i][AD] * r[i + 1];
 
-    /* compute the graph with x=[0..255] */
-    i = 0;
     point = points;
+
+    /* left padding */
+    for (i = 0; i < (int)(point->x * scale); i++)
+        y[i] = CLIP(point->y * scale);
+
+    /* compute the graph with x=[x0..xN] */
+    i = 0;
     av_assert0(point->next); // always at least 2 key points
     while (point->next) {
-        double yc = point->y;
-        double yn = point->next->y;
+        const double yc = point->y;
+        const double yn = point->next->y;
 
-        double a = yc;
-        double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
-        double c = r[i] / 2.;
-        double d = (r[i+1] - r[i]) / (6.*h[i]);
+        const double a = yc;
+        const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
+        const double c = r[i] / 2.;
+        const double d = (r[i+1] - r[i]) / (6.*h[i]);
 
         int x;
-        int x_start = point->x       * 255;
-        int x_end   = point->next->x * 255;
+        const int x_start = point->x       * scale;
+        const int x_end   = point->next->x * scale;
 
-        av_assert0(x_start >= 0 && x_start <= 255 &&
-                   x_end   >= 0 && x_end   <= 255);
+        av_assert0(x_start >= 0 && x_start < lut_size &&
+                   x_end   >= 0 && x_end   < lut_size);
 
         for (x = x_start; x <= x_end; x++) {
-            double xx = (x - x_start) * 1/255.;
-            double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
-            y[x] = av_clipf(yy, 0, 1) * 255;
-            av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
+            const double xx = (x - x_start) * 1./scale;
+            const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
+            y[x] = CLIP(yy * scale);
+            av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
         }
 
         point = point->next;
         i++;
     }
 
+    /* right padding */
+    for (i = (int)(point->x * scale); i < lut_size; i++)
+        y[i] = CLIP(point->y * scale);
+
 end:
     av_free(matrix);
     av_free(h);
@@ -315,6 +333,16 @@ end:
     return ret;
 }
 
+#define DECLARE_INTERPOLATE_FUNC(nbits)                                     \
+static int interpolate##nbits(void *log_ctx, uint16_t *y,                   \
+                              const struct keypoint *points)                \
+{                                                                           \
+    return interpolate(log_ctx, y, points, nbits);                          \
+}
+
+DECLARE_INTERPOLATE_FUNC(8)
+DECLARE_INTERPOLATE_FUNC(16)
+
 static int parse_psfile(AVFilterContext *ctx, const char *fname)
 {
     CurvesContext *curves = ctx->priv;
@@ -371,11 +399,71 @@ end:
     return ret;
 }
 
-static av_cold int init(AVFilterContext *ctx)
+static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
+                       struct keypoint *comp_points[NB_COMP + 1],
+                       int lut_size)
 {
-    int i, j, ret;
+    int i;
+    AVBPrint buf;
+    const double scale = 1. / (lut_size - 1);
+    static const char * const colors[] = { "red", "green", "blue", "#404040", };
+    FILE *f = av_fopen_utf8(fname, "w");
+
+    av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
+
+    if (!f) {
+        int ret = AVERROR(errno);
+        av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
+               fname, av_err2str(ret));
+        return ret;
+    }
+
+    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
+
+    av_bprintf(&buf, "set xtics 0.1\n");
+    av_bprintf(&buf, "set ytics 0.1\n");
+    av_bprintf(&buf, "set size square\n");
+    av_bprintf(&buf, "set grid\n");
+
+    for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
+        av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
+                   i ? ", " : "plot ", colors[i]);
+        if (comp_points[i])
+            av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
+                    colors[i]);
+    }
+    av_bprintf(&buf, "\n");
+
+    for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
+        int x;
+
+        /* plot generated values */
+        for (x = 0; x < lut_size; x++)
+            av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
+        av_bprintf(&buf, "e\n");
+
+        /* plot user knots */
+        if (comp_points[i]) {
+            const struct keypoint *point = comp_points[i];
+
+            while (point) {
+                av_bprintf(&buf, "%f %f\n", point->x, point->y);
+                point = point->next;
+            }
+            av_bprintf(&buf, "e\n");
+        }
+    }
+
+    fwrite(buf.str, 1, buf.len, f);
+    fclose(f);
+    av_bprint_finalize(&buf, NULL);
+    return 0;
+}
+
+static av_cold int curves_init(AVFilterContext *ctx)
+{
+    int i, ret;
     CurvesContext *curves = ctx->priv;
-    struct keypoint *comp_points[NB_COMP + 1] = {0};
     char **pts = curves->comp_points_str;
     const char *allp = curves->comp_points_str_all;
 
@@ -411,37 +499,74 @@ static av_cold int init(AVFilterContext *ctx)
         SET_COMP_IF_NOT_SET(3, master);
     }
 
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
+        AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
+        AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
+        AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
+        AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
+        AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
+        AV_PIX_FMT_NONE
+    };
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    int i, j, ret;
+    AVFilterContext *ctx = inlink->dst;
+    CurvesContext *curves = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    char **pts = curves->comp_points_str;
+    struct keypoint *comp_points[NB_COMP + 1] = {0};
+
+    ff_fill_rgba_map(curves->rgba_map, inlink->format);
+    curves->is_16bit = desc->comp[0].depth > 8;
+    curves->lut_size = curves->is_16bit ? 1<<16 : 1<<8;
+    curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
+
     for (i = 0; i < NB_COMP + 1; i++) {
-        ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
+        curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
+        if (!curves->graph[i])
+            return AVERROR(ENOMEM);
+        ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
         if (ret < 0)
             return ret;
-        ret = interpolate(ctx, curves->graph[i], comp_points[i]);
+        if (curves->is_16bit) ret = interpolate16(ctx, curves->graph[i], comp_points[i]);
+        else                  ret = interpolate8(ctx, curves->graph[i], comp_points[i]);
         if (ret < 0)
             return ret;
     }
 
     if (pts[NB_COMP]) {
         for (i = 0; i < NB_COMP; i++)
-            for (j = 0; j < 256; j++)
+            for (j = 0; j < curves->lut_size; j++)
                 curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
     }
 
     if (av_log_get_level() >= AV_LOG_VERBOSE) {
         for (i = 0; i < NB_COMP; i++) {
-            struct keypoint *point = comp_points[i];
+            const struct keypoint *point = comp_points[i];
             av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
             while (point) {
                 av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
                 point = point->next;
             }
-            av_log(ctx, AV_LOG_VERBOSE, "\n");
-            av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
-            for (j = 0; j < 256; j++)
-                av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i][j]);
-            av_log(ctx, AV_LOG_VERBOSE, "\n");
         }
     }
 
+    if (curves->plot_filename)
+        dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
+
     for (i = 0; i < NB_COMP + 1; i++) {
         struct keypoint *point = comp_points[i];
         while (point) {
@@ -454,33 +579,6 @@ static av_cold int init(AVFilterContext *ctx)
     return 0;
 }
 
-static int query_formats(AVFilterContext *ctx)
-{
-    static const enum AVPixelFormat pix_fmts[] = {
-        AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
-        AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
-        AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
-        AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
-        AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
-        AV_PIX_FMT_NONE
-    };
-    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
-    if (!fmts_list)
-        return AVERROR(ENOMEM);
-    return ff_set_common_formats(ctx, fmts_list);
-}
-
-static int config_input(AVFilterLink *inlink)
-{
-    CurvesContext *curves = inlink->dst->priv;
-    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
-
-    ff_fill_rgba_map(curves->rgba_map, inlink->format);
-    curves->step = av_get_padded_bits_per_pixel(desc) >> 3;
-
-    return 0;
-}
-
 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
 {
     int x, y;
@@ -496,19 +594,35 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     const uint8_t a = curves->rgba_map[A];
     const int slice_start = (in->height *  jobnr   ) / nb_jobs;
     const int slice_end   = (in->height * (jobnr+1)) / nb_jobs;
-    uint8_t       *dst = out->data[0] + slice_start * out->linesize[0];
-    const uint8_t *src =  in->data[0] + slice_start *  in->linesize[0];
-
-    for (y = slice_start; y < slice_end; y++) {
-        for (x = 0; x < in->width * step; x += step) {
-            dst[x + r] = curves->graph[R][src[x + r]];
-            dst[x + g] = curves->graph[G][src[x + g]];
-            dst[x + b] = curves->graph[B][src[x + b]];
-            if (!direct && step == 4)
-                dst[x + a] = src[x + a];
+
+    if (curves->is_16bit) {
+        for (y = slice_start; y < slice_end; y++) {
+            uint16_t       *dstp = (      uint16_t *)(out->data[0] + y * out->linesize[0]);
+            const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y *  in->linesize[0]);
+
+            for (x = 0; x < in->width * step; x += step) {
+                dstp[x + r] = curves->graph[R][srcp[x + r]];
+                dstp[x + g] = curves->graph[G][srcp[x + g]];
+                dstp[x + b] = curves->graph[B][srcp[x + b]];
+                if (!direct && step == 4)
+                    dstp[x + a] = srcp[x + a];
+            }
+        }
+    } else {
+        uint8_t       *dst = out->data[0] + slice_start * out->linesize[0];
+        const uint8_t *src =  in->data[0] + slice_start *  in->linesize[0];
+
+        for (y = slice_start; y < slice_end; y++) {
+            for (x = 0; x < in->width * step; x += step) {
+                dst[x + r] = curves->graph[R][src[x + r]];
+                dst[x + g] = curves->graph[G][src[x + g]];
+                dst[x + b] = curves->graph[B][src[x + b]];
+                if (!direct && step == 4)
+                    dst[x + a] = src[x + a];
+            }
+            dst += out->linesize[0];
+            src += in ->linesize[0];
         }
-        dst += out->linesize[0];
-        src += in ->linesize[0];
     }
     return 0;
 }
@@ -533,7 +647,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
 
     td.in  = in;
     td.out = out;
-    ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
+    ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
 
     if (out != in)
         av_frame_free(&in);
@@ -541,6 +655,15 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     return ff_filter_frame(outlink, out);
 }
 
+static av_cold void curves_uninit(AVFilterContext *ctx)
+{
+    int i;
+    CurvesContext *curves = ctx->priv;
+
+    for (i = 0; i < NB_COMP + 1; i++)
+        av_freep(&curves->graph[i]);
+}
+
 static const AVFilterPad curves_inputs[] = {
     {
         .name         = "default",
@@ -563,7 +686,8 @@ AVFilter ff_vf_curves = {
     .name          = "curves",
     .description   = NULL_IF_CONFIG_SMALL("Adjust components curves."),
     .priv_size     = sizeof(CurvesContext),
-    .init          = init,
+    .init          = curves_init,
+    .uninit        = curves_uninit,
     .query_formats = query_formats,
     .inputs        = curves_inputs,
     .outputs       = curves_outputs,