]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_curves.c
lavfi/curves: remove pointless logging since the addition of plot option
[ffmpeg] / libavfilter / vf_curves.c
1 /*
2  * Copyright (c) 2013 Clément Bœsch
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/opt.h"
22 #include "libavutil/bprint.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/file.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "drawutils.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33
34 #define R 0
35 #define G 1
36 #define B 2
37 #define A 3
38
39 struct keypoint {
40     double x, y;
41     struct keypoint *next;
42 };
43
44 #define NB_COMP 3
45
46 enum preset {
47     PRESET_NONE,
48     PRESET_COLOR_NEGATIVE,
49     PRESET_CROSS_PROCESS,
50     PRESET_DARKER,
51     PRESET_INCREASE_CONTRAST,
52     PRESET_LIGHTER,
53     PRESET_LINEAR_CONTRAST,
54     PRESET_MEDIUM_CONTRAST,
55     PRESET_NEGATIVE,
56     PRESET_STRONG_CONTRAST,
57     PRESET_VINTAGE,
58     NB_PRESETS,
59 };
60
61 typedef struct {
62     const AVClass *class;
63     int preset;
64     char *comp_points_str[NB_COMP + 1];
65     char *comp_points_str_all;
66     uint8_t graph[NB_COMP + 1][256];
67     char *psfile;
68     uint8_t rgba_map[4];
69     int step;
70     char *plot_filename;
71 } CurvesContext;
72
73 typedef struct ThreadData {
74     AVFrame *in, *out;
75 } ThreadData;
76
77 #define OFFSET(x) offsetof(CurvesContext, x)
78 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
79 static const AVOption curves_options[] = {
80     { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
81         { "none",               NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE},                 INT_MIN, INT_MAX, FLAGS, "preset_name" },
82         { "color_negative",     NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE},       INT_MIN, INT_MAX, FLAGS, "preset_name" },
83         { "cross_process",      NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS},        INT_MIN, INT_MAX, FLAGS, "preset_name" },
84         { "darker",             NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER},               INT_MIN, INT_MAX, FLAGS, "preset_name" },
85         { "increase_contrast",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST},    INT_MIN, INT_MAX, FLAGS, "preset_name" },
86         { "lighter",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER},              INT_MIN, INT_MAX, FLAGS, "preset_name" },
87         { "linear_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
88         { "medium_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
89         { "negative",           NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE},             INT_MIN, INT_MAX, FLAGS, "preset_name" },
90         { "strong_contrast",    NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST},      INT_MIN, INT_MAX, FLAGS, "preset_name" },
91         { "vintage",            NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE},              INT_MIN, INT_MAX, FLAGS, "preset_name" },
92     { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
93     { "m",     "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
94     { "red",   "set red points coordinates",   OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
95     { "r",     "set red points coordinates",   OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
96     { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
97     { "g",     "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
98     { "blue",  "set blue points coordinates",  OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
99     { "b",     "set blue points coordinates",  OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
100     { "all",   "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
101     { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
102     { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
103     { NULL }
104 };
105
106 AVFILTER_DEFINE_CLASS(curves);
107
108 static const struct {
109     const char *r;
110     const char *g;
111     const char *b;
112     const char *master;
113 } curves_presets[] = {
114     [PRESET_COLOR_NEGATIVE] = {
115         "0.129/1 0.466/0.498 0.725/0",
116         "0.109/1 0.301/0.498 0.517/0",
117         "0.098/1 0.235/0.498 0.423/0",
118     },
119     [PRESET_CROSS_PROCESS] = {
120         "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
121         "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
122         "0/0 0.231/0.094 0.709/0.874 1/1",
123     },
124     [PRESET_DARKER]             = { .master = "0/0 0.5/0.4 1/1" },
125     [PRESET_INCREASE_CONTRAST]  = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
126     [PRESET_LIGHTER]            = { .master = "0/0 0.4/0.5 1/1" },
127     [PRESET_LINEAR_CONTRAST]    = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
128     [PRESET_MEDIUM_CONTRAST]    = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
129     [PRESET_NEGATIVE]           = { .master = "0/1 1/0" },
130     [PRESET_STRONG_CONTRAST]    = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
131     [PRESET_VINTAGE] = {
132         "0/0.11 0.42/0.51 1/0.95",
133         "0/0 0.50/0.48 1/1",
134         "0/0.22 0.49/0.44 1/0.8",
135     }
136 };
137
138 static struct keypoint *make_point(double x, double y, struct keypoint *next)
139 {
140     struct keypoint *point = av_mallocz(sizeof(*point));
141
142     if (!point)
143         return NULL;
144     point->x = x;
145     point->y = y;
146     point->next = next;
147     return point;
148 }
149
150 static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
151 {
152     char *p = (char *)s; // strtod won't alter the string
153     struct keypoint *last = NULL;
154
155     /* construct a linked list based on the key points string */
156     while (p && *p) {
157         struct keypoint *point = make_point(0, 0, NULL);
158         if (!point)
159             return AVERROR(ENOMEM);
160         point->x = av_strtod(p, &p); if (p && *p) p++;
161         point->y = av_strtod(p, &p); if (p && *p) p++;
162         if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
163             av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
164                    "x and y must be in the [0;1] range.\n", point->x, point->y);
165             return AVERROR(EINVAL);
166         }
167         if (!*points)
168             *points = point;
169         if (last) {
170             if ((int)(last->x * 255) >= (int)(point->x * 255)) {
171                 av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
172                        "and (%f;%f) are too close from each other or not "
173                        "strictly increasing on the x-axis\n",
174                        last->x, last->y, point->x, point->y);
175                 return AVERROR(EINVAL);
176             }
177             last->next = point;
178         }
179         last = point;
180     }
181
182     if (*points && !(*points)->next) {
183         av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
184                "this is unlikely to behave as you expect. You probably want"
185                "at least 2 points.",
186                (*points)->x, (*points)->y);
187     }
188
189     return 0;
190 }
191
192 static int get_nb_points(const struct keypoint *d)
193 {
194     int n = 0;
195     while (d) {
196         n++;
197         d = d->next;
198     }
199     return n;
200 }
201
202 /**
203  * Natural cubic spline interpolation
204  * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
205  * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
206  */
207 static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
208 {
209     int i, ret = 0;
210     const struct keypoint *point = points;
211     double xprev = 0;
212
213     double (*matrix)[3];
214     double *h, *r;
215     int n = get_nb_points(points); // number of splines
216
217     if (n == 0) {
218         for (i = 0; i < 256; i++)
219             y[i] = i;
220         return 0;
221     }
222
223     if (n == 1) {
224         for (i = 0; i < 256; i++)
225             y[i] = av_clip_uint8(point->y * 255);
226         return 0;
227     }
228
229     matrix = av_calloc(n, sizeof(*matrix));
230     h = av_malloc((n - 1) * sizeof(*h));
231     r = av_calloc(n, sizeof(*r));
232
233     if (!matrix || !h || !r) {
234         ret = AVERROR(ENOMEM);
235         goto end;
236     }
237
238     /* h(i) = x(i+1) - x(i) */
239     i = -1;
240     for (point = points; point; point = point->next) {
241         if (i != -1)
242             h[i] = point->x - xprev;
243         xprev = point->x;
244         i++;
245     }
246
247     /* right-side of the polynomials, will be modified to contains the solution */
248     point = points;
249     for (i = 1; i < n - 1; i++) {
250         double yp = point->y,
251                yc = point->next->y,
252                yn = point->next->next->y;
253         r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
254         point = point->next;
255     }
256
257 #define BD 0 /* sub  diagonal (below main) */
258 #define MD 1 /* main diagonal (center) */
259 #define AD 2 /* sup  diagonal (above main) */
260
261     /* left side of the polynomials into a tridiagonal matrix. */
262     matrix[0][MD] = matrix[n - 1][MD] = 1;
263     for (i = 1; i < n - 1; i++) {
264         matrix[i][BD] = h[i-1];
265         matrix[i][MD] = 2 * (h[i-1] + h[i]);
266         matrix[i][AD] = h[i];
267     }
268
269     /* tridiagonal solving of the linear system */
270     for (i = 1; i < n; i++) {
271         double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
272         double k = den ? 1./den : 1.;
273         matrix[i][AD] *= k;
274         r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
275     }
276     for (i = n - 2; i >= 0; i--)
277         r[i] = r[i] - matrix[i][AD] * r[i + 1];
278
279     point = points;
280
281     /* left padding */
282     for (i = 0; i < (int)(point->x * 255); i++)
283         y[i] = av_clip_uint8(point->y * 255);
284
285     /* compute the graph with x=[x0..xN] */
286     i = 0;
287     av_assert0(point->next); // always at least 2 key points
288     while (point->next) {
289         double yc = point->y;
290         double yn = point->next->y;
291
292         double a = yc;
293         double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
294         double c = r[i] / 2.;
295         double d = (r[i+1] - r[i]) / (6.*h[i]);
296
297         int x;
298         int x_start = point->x       * 255;
299         int x_end   = point->next->x * 255;
300
301         av_assert0(x_start >= 0 && x_start <= 255 &&
302                    x_end   >= 0 && x_end   <= 255);
303
304         for (x = x_start; x <= x_end; x++) {
305             double xx = (x - x_start) * 1/255.;
306             double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
307             y[x] = av_clip_uint8(yy * 255);
308             av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
309         }
310
311         point = point->next;
312         i++;
313     }
314
315     /* right padding */
316     for (i = (int)(point->x * 255); i <= 255; i++)
317         y[i] = av_clip_uint8(point->y * 255);
318
319 end:
320     av_free(matrix);
321     av_free(h);
322     av_free(r);
323     return ret;
324 }
325
326 static int parse_psfile(AVFilterContext *ctx, const char *fname)
327 {
328     CurvesContext *curves = ctx->priv;
329     uint8_t *buf;
330     size_t size;
331     int i, ret, av_unused(version), nb_curves;
332     AVBPrint ptstr;
333     static const int comp_ids[] = {3, 0, 1, 2};
334
335     av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
336
337     ret = av_file_map(fname, &buf, &size, 0, NULL);
338     if (ret < 0)
339         return ret;
340
341 #define READ16(dst) do {                \
342     if (size < 2) {                     \
343         ret = AVERROR_INVALIDDATA;      \
344         goto end;                       \
345     }                                   \
346     dst = AV_RB16(buf);                 \
347     buf  += 2;                          \
348     size -= 2;                          \
349 } while (0)
350
351     READ16(version);
352     READ16(nb_curves);
353     for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
354         int nb_points, n;
355         av_bprint_clear(&ptstr);
356         READ16(nb_points);
357         for (n = 0; n < nb_points; n++) {
358             int y, x;
359             READ16(y);
360             READ16(x);
361             av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
362         }
363         if (*ptstr.str) {
364             char **pts = &curves->comp_points_str[comp_ids[i]];
365             if (!*pts) {
366                 *pts = av_strdup(ptstr.str);
367                 av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
368                        i, comp_ids[i], nb_points, *pts);
369                 if (!*pts) {
370                     ret = AVERROR(ENOMEM);
371                     goto end;
372                 }
373             }
374         }
375     }
376 end:
377     av_bprint_finalize(&ptstr, NULL);
378     av_file_unmap(buf, size);
379     return ret;
380 }
381
382 static int dump_curves(const char *fname, uint8_t graph[NB_COMP + 1][256],
383                        struct keypoint *comp_points[NB_COMP + 1])
384 {
385     int i;
386     AVBPrint buf;
387     static const char * const colors[] = { "red", "green", "blue", "#404040", };
388     FILE *f = av_fopen_utf8(fname, "w");
389
390     av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
391
392     if (!f) {
393         int ret = AVERROR(errno);
394         av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
395                fname, av_err2str(ret));
396         return ret;
397     }
398
399     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
400
401     av_bprintf(&buf, "set xtics 0.1\n");
402     av_bprintf(&buf, "set ytics 0.1\n");
403     av_bprintf(&buf, "set size square\n");
404     av_bprintf(&buf, "set grid\n");
405
406     for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
407         av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
408                    i ? ", " : "plot ", colors[i]);
409         if (comp_points[i])
410             av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
411                     colors[i]);
412     }
413     av_bprintf(&buf, "\n");
414
415     for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
416         int x;
417
418         /* plot generated values */
419         for (x = 0; x < 256; x++)
420             av_bprintf(&buf, "%f %f\n", x/255., graph[i][x]/255.);
421         av_bprintf(&buf, "e\n");
422
423         /* plot user knots */
424         if (comp_points[i]) {
425             const struct keypoint *point = comp_points[i];
426
427             while (point) {
428                 av_bprintf(&buf, "%f %f\n", point->x, point->y);
429                 point = point->next;
430             }
431             av_bprintf(&buf, "e\n");
432         }
433     }
434
435     fwrite(buf.str, 1, buf.len, f);
436     fclose(f);
437     av_bprint_finalize(&buf, NULL);
438     return 0;
439 }
440
441 static av_cold int init(AVFilterContext *ctx)
442 {
443     int i, j, ret;
444     CurvesContext *curves = ctx->priv;
445     struct keypoint *comp_points[NB_COMP + 1] = {0};
446     char **pts = curves->comp_points_str;
447     const char *allp = curves->comp_points_str_all;
448
449     //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
450     //    allp = curves_presets[curves->preset].all;
451
452     if (allp) {
453         for (i = 0; i < NB_COMP; i++) {
454             if (!pts[i])
455                 pts[i] = av_strdup(allp);
456             if (!pts[i])
457                 return AVERROR(ENOMEM);
458         }
459     }
460
461     if (curves->psfile) {
462         ret = parse_psfile(ctx, curves->psfile);
463         if (ret < 0)
464             return ret;
465     }
466
467     if (curves->preset != PRESET_NONE) {
468 #define SET_COMP_IF_NOT_SET(n, name) do {                           \
469     if (!pts[n] && curves_presets[curves->preset].name) {           \
470         pts[n] = av_strdup(curves_presets[curves->preset].name);    \
471         if (!pts[n])                                                \
472             return AVERROR(ENOMEM);                                 \
473     }                                                               \
474 } while (0)
475         SET_COMP_IF_NOT_SET(0, r);
476         SET_COMP_IF_NOT_SET(1, g);
477         SET_COMP_IF_NOT_SET(2, b);
478         SET_COMP_IF_NOT_SET(3, master);
479     }
480
481     for (i = 0; i < NB_COMP + 1; i++) {
482         ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
483         if (ret < 0)
484             return ret;
485         ret = interpolate(ctx, curves->graph[i], comp_points[i]);
486         if (ret < 0)
487             return ret;
488     }
489
490     if (pts[NB_COMP]) {
491         for (i = 0; i < NB_COMP; i++)
492             for (j = 0; j < 256; j++)
493                 curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
494     }
495
496     if (av_log_get_level() >= AV_LOG_VERBOSE) {
497         for (i = 0; i < NB_COMP; i++) {
498             struct keypoint *point = comp_points[i];
499             av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
500             while (point) {
501                 av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
502                 point = point->next;
503             }
504         }
505     }
506
507     if (curves->plot_filename)
508         dump_curves(curves->plot_filename, curves->graph, comp_points);
509
510     for (i = 0; i < NB_COMP + 1; i++) {
511         struct keypoint *point = comp_points[i];
512         while (point) {
513             struct keypoint *next = point->next;
514             av_free(point);
515             point = next;
516         }
517     }
518
519     return 0;
520 }
521
522 static int query_formats(AVFilterContext *ctx)
523 {
524     static const enum AVPixelFormat pix_fmts[] = {
525         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
526         AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
527         AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
528         AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
529         AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
530         AV_PIX_FMT_NONE
531     };
532     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
533     if (!fmts_list)
534         return AVERROR(ENOMEM);
535     return ff_set_common_formats(ctx, fmts_list);
536 }
537
538 static int config_input(AVFilterLink *inlink)
539 {
540     CurvesContext *curves = inlink->dst->priv;
541     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
542
543     ff_fill_rgba_map(curves->rgba_map, inlink->format);
544     curves->step = av_get_padded_bits_per_pixel(desc) >> 3;
545
546     return 0;
547 }
548
549 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
550 {
551     int x, y;
552     const CurvesContext *curves = ctx->priv;
553     const ThreadData *td = arg;
554     const AVFrame *in  = td->in;
555     const AVFrame *out = td->out;
556     const int direct = out == in;
557     const int step = curves->step;
558     const uint8_t r = curves->rgba_map[R];
559     const uint8_t g = curves->rgba_map[G];
560     const uint8_t b = curves->rgba_map[B];
561     const uint8_t a = curves->rgba_map[A];
562     const int slice_start = (in->height *  jobnr   ) / nb_jobs;
563     const int slice_end   = (in->height * (jobnr+1)) / nb_jobs;
564     uint8_t       *dst = out->data[0] + slice_start * out->linesize[0];
565     const uint8_t *src =  in->data[0] + slice_start *  in->linesize[0];
566
567     for (y = slice_start; y < slice_end; y++) {
568         for (x = 0; x < in->width * step; x += step) {
569             dst[x + r] = curves->graph[R][src[x + r]];
570             dst[x + g] = curves->graph[G][src[x + g]];
571             dst[x + b] = curves->graph[B][src[x + b]];
572             if (!direct && step == 4)
573                 dst[x + a] = src[x + a];
574         }
575         dst += out->linesize[0];
576         src += in ->linesize[0];
577     }
578     return 0;
579 }
580
581 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
582 {
583     AVFilterContext *ctx = inlink->dst;
584     AVFilterLink *outlink = ctx->outputs[0];
585     AVFrame *out;
586     ThreadData td;
587
588     if (av_frame_is_writable(in)) {
589         out = in;
590     } else {
591         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
592         if (!out) {
593             av_frame_free(&in);
594             return AVERROR(ENOMEM);
595         }
596         av_frame_copy_props(out, in);
597     }
598
599     td.in  = in;
600     td.out = out;
601     ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
602
603     if (out != in)
604         av_frame_free(&in);
605
606     return ff_filter_frame(outlink, out);
607 }
608
609 static const AVFilterPad curves_inputs[] = {
610     {
611         .name         = "default",
612         .type         = AVMEDIA_TYPE_VIDEO,
613         .filter_frame = filter_frame,
614         .config_props = config_input,
615     },
616     { NULL }
617 };
618
619 static const AVFilterPad curves_outputs[] = {
620     {
621         .name = "default",
622         .type = AVMEDIA_TYPE_VIDEO,
623     },
624     { NULL }
625 };
626
627 AVFilter ff_vf_curves = {
628     .name          = "curves",
629     .description   = NULL_IF_CONFIG_SMALL("Adjust components curves."),
630     .priv_size     = sizeof(CurvesContext),
631     .init          = init,
632     .query_formats = query_formats,
633     .inputs        = curves_inputs,
634     .outputs       = curves_outputs,
635     .priv_class    = &curves_class,
636     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
637 };