2 * Copyright (c) 2013 Clément Bœsch
4 * This file is part of FFmpeg.
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.
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.
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
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"
29 #include "drawutils.h"
41 struct keypoint *next;
48 PRESET_COLOR_NEGATIVE,
51 PRESET_INCREASE_CONTRAST,
53 PRESET_LINEAR_CONTRAST,
54 PRESET_MEDIUM_CONTRAST,
56 PRESET_STRONG_CONTRAST,
61 typedef struct CurvesContext {
64 char *comp_points_str[NB_COMP + 1];
65 char *comp_points_str_all;
66 uint16_t *graph[NB_COMP + 1];
75 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
78 typedef struct ThreadData {
82 #define OFFSET(x) offsetof(CurvesContext, x)
83 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
84 static const AVOption curves_options[] = {
85 { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
86 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
87 { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
88 { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
89 { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
90 { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
91 { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
92 { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
93 { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
94 { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
95 { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
96 { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
97 { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
98 { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
99 { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
100 { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
101 { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
102 { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
103 { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
104 { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
105 { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
106 { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
107 { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
111 AVFILTER_DEFINE_CLASS(curves);
113 static const struct {
118 } curves_presets[] = {
119 [PRESET_COLOR_NEGATIVE] = {
120 "0.129/1 0.466/0.498 0.725/0",
121 "0.109/1 0.301/0.498 0.517/0",
122 "0.098/1 0.235/0.498 0.423/0",
124 [PRESET_CROSS_PROCESS] = {
125 "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
126 "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
127 "0/0 0.231/0.094 0.709/0.874 1/1",
129 [PRESET_DARKER] = { .master = "0/0 0.5/0.4 1/1" },
130 [PRESET_INCREASE_CONTRAST] = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
131 [PRESET_LIGHTER] = { .master = "0/0 0.4/0.5 1/1" },
132 [PRESET_LINEAR_CONTRAST] = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
133 [PRESET_MEDIUM_CONTRAST] = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
134 [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
135 [PRESET_STRONG_CONTRAST] = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
137 "0/0.11 0.42/0.51 1/0.95",
139 "0/0.22 0.49/0.44 1/0.8",
143 static struct keypoint *make_point(double x, double y, struct keypoint *next)
145 struct keypoint *point = av_mallocz(sizeof(*point));
155 static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
158 char *p = (char *)s; // strtod won't alter the string
159 struct keypoint *last = NULL;
160 const int scale = lut_size - 1;
162 /* construct a linked list based on the key points string */
164 struct keypoint *point = make_point(0, 0, NULL);
166 return AVERROR(ENOMEM);
167 point->x = av_strtod(p, &p); if (p && *p) p++;
168 point->y = av_strtod(p, &p); if (p && *p) p++;
169 if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
170 av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
171 "x and y must be in the [0;1] range.\n", point->x, point->y);
172 return AVERROR(EINVAL);
177 if ((int)(last->x * scale) >= (int)(point->x * scale)) {
178 av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
179 "and (%f;%f) are too close from each other or not "
180 "strictly increasing on the x-axis\n",
181 last->x, last->y, point->x, point->y);
182 return AVERROR(EINVAL);
189 if (*points && !(*points)->next) {
190 av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
191 "this is unlikely to behave as you expect. You probably want"
192 "at least 2 points.",
193 (*points)->x, (*points)->y);
199 static int get_nb_points(const struct keypoint *d)
210 * Natural cubic spline interpolation
211 * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
212 * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
215 #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uintp2_c(v, nbits))
217 static inline int interpolate(void *log_ctx, uint16_t *y,
218 const struct keypoint *points, int nbits)
221 const struct keypoint *point = points;
223 const int lut_size = 1<<nbits;
224 const int scale = lut_size - 1;
228 const int n = get_nb_points(points); // number of splines
231 for (i = 0; i < lut_size; i++)
237 for (i = 0; i < lut_size; i++)
238 y[i] = CLIP(point->y * scale);
242 matrix = av_calloc(n, sizeof(*matrix));
243 h = av_malloc((n - 1) * sizeof(*h));
244 r = av_calloc(n, sizeof(*r));
246 if (!matrix || !h || !r) {
247 ret = AVERROR(ENOMEM);
251 /* h(i) = x(i+1) - x(i) */
253 for (point = points; point; point = point->next) {
255 h[i] = point->x - xprev;
260 /* right-side of the polynomials, will be modified to contains the solution */
262 for (i = 1; i < n - 1; i++) {
263 const double yp = point->y;
264 const double yc = point->next->y;
265 const double yn = point->next->next->y;
266 r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
270 #define BD 0 /* sub diagonal (below main) */
271 #define MD 1 /* main diagonal (center) */
272 #define AD 2 /* sup diagonal (above main) */
274 /* left side of the polynomials into a tridiagonal matrix. */
275 matrix[0][MD] = matrix[n - 1][MD] = 1;
276 for (i = 1; i < n - 1; i++) {
277 matrix[i][BD] = h[i-1];
278 matrix[i][MD] = 2 * (h[i-1] + h[i]);
279 matrix[i][AD] = h[i];
282 /* tridiagonal solving of the linear system */
283 for (i = 1; i < n; i++) {
284 const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
285 const double k = den ? 1./den : 1.;
287 r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
289 for (i = n - 2; i >= 0; i--)
290 r[i] = r[i] - matrix[i][AD] * r[i + 1];
295 for (i = 0; i < (int)(point->x * scale); i++)
296 y[i] = CLIP(point->y * scale);
298 /* compute the graph with x=[x0..xN] */
300 av_assert0(point->next); // always at least 2 key points
301 while (point->next) {
302 const double yc = point->y;
303 const double yn = point->next->y;
306 const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
307 const double c = r[i] / 2.;
308 const double d = (r[i+1] - r[i]) / (6.*h[i]);
311 const int x_start = point->x * scale;
312 const int x_end = point->next->x * scale;
314 av_assert0(x_start >= 0 && x_start < lut_size &&
315 x_end >= 0 && x_end < lut_size);
317 for (x = x_start; x <= x_end; x++) {
318 const double xx = (x - x_start) * 1./scale;
319 const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
320 y[x] = CLIP(yy * scale);
321 av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
329 for (i = (int)(point->x * scale); i < lut_size; i++)
330 y[i] = CLIP(point->y * scale);
339 #define DECLARE_INTERPOLATE_FUNC(nbits) \
340 static int interpolate##nbits(void *log_ctx, uint16_t *y, \
341 const struct keypoint *points) \
343 return interpolate(log_ctx, y, points, nbits); \
346 DECLARE_INTERPOLATE_FUNC(8)
347 DECLARE_INTERPOLATE_FUNC(9)
348 DECLARE_INTERPOLATE_FUNC(10)
349 DECLARE_INTERPOLATE_FUNC(12)
350 DECLARE_INTERPOLATE_FUNC(14)
351 DECLARE_INTERPOLATE_FUNC(16)
353 static int parse_psfile(AVFilterContext *ctx, const char *fname)
355 CurvesContext *curves = ctx->priv;
358 int i, ret, av_unused(version), nb_curves;
360 static const int comp_ids[] = {3, 0, 1, 2};
362 av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
364 ret = av_file_map(fname, &buf, &size, 0, NULL);
368 #define READ16(dst) do { \
370 ret = AVERROR_INVALIDDATA; \
373 dst = AV_RB16(buf); \
380 for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
382 av_bprint_clear(&ptstr);
384 for (n = 0; n < nb_points; n++) {
388 av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
391 char **pts = &curves->comp_points_str[comp_ids[i]];
393 *pts = av_strdup(ptstr.str);
394 av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
395 i, comp_ids[i], nb_points, *pts);
397 ret = AVERROR(ENOMEM);
404 av_bprint_finalize(&ptstr, NULL);
405 av_file_unmap(buf, size);
409 static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
410 struct keypoint *comp_points[NB_COMP + 1],
415 const double scale = 1. / (lut_size - 1);
416 static const char * const colors[] = { "red", "green", "blue", "#404040", };
417 FILE *f = av_fopen_utf8(fname, "w");
419 av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
422 int ret = AVERROR(errno);
423 av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
424 fname, av_err2str(ret));
428 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
430 av_bprintf(&buf, "set xtics 0.1\n");
431 av_bprintf(&buf, "set ytics 0.1\n");
432 av_bprintf(&buf, "set size square\n");
433 av_bprintf(&buf, "set grid\n");
435 for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
436 av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
437 i ? ", " : "plot ", colors[i]);
439 av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
442 av_bprintf(&buf, "\n");
444 for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
447 /* plot generated values */
448 for (x = 0; x < lut_size; x++)
449 av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
450 av_bprintf(&buf, "e\n");
452 /* plot user knots */
453 if (comp_points[i]) {
454 const struct keypoint *point = comp_points[i];
457 av_bprintf(&buf, "%f %f\n", point->x, point->y);
460 av_bprintf(&buf, "e\n");
464 fwrite(buf.str, 1, buf.len, f);
466 av_bprint_finalize(&buf, NULL);
470 static av_cold int curves_init(AVFilterContext *ctx)
473 CurvesContext *curves = ctx->priv;
474 char **pts = curves->comp_points_str;
475 const char *allp = curves->comp_points_str_all;
477 //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
478 // allp = curves_presets[curves->preset].all;
481 for (i = 0; i < NB_COMP; i++) {
483 pts[i] = av_strdup(allp);
485 return AVERROR(ENOMEM);
489 if (curves->psfile) {
490 ret = parse_psfile(ctx, curves->psfile);
495 if (curves->preset != PRESET_NONE) {
496 #define SET_COMP_IF_NOT_SET(n, name) do { \
497 if (!pts[n] && curves_presets[curves->preset].name) { \
498 pts[n] = av_strdup(curves_presets[curves->preset].name); \
500 return AVERROR(ENOMEM); \
503 SET_COMP_IF_NOT_SET(0, r);
504 SET_COMP_IF_NOT_SET(1, g);
505 SET_COMP_IF_NOT_SET(2, b);
506 SET_COMP_IF_NOT_SET(3, master);
512 static int query_formats(AVFilterContext *ctx)
514 static const enum AVPixelFormat pix_fmts[] = {
515 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
516 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
517 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
518 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
519 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
520 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
521 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
522 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
524 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
525 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
527 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
530 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
532 return AVERROR(ENOMEM);
533 return ff_set_common_formats(ctx, fmts_list);
536 static int filter_slice_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
539 const CurvesContext *curves = ctx->priv;
540 const ThreadData *td = arg;
541 const AVFrame *in = td->in;
542 const AVFrame *out = td->out;
543 const int direct = out == in;
544 const int step = curves->step;
545 const uint8_t r = curves->rgba_map[R];
546 const uint8_t g = curves->rgba_map[G];
547 const uint8_t b = curves->rgba_map[B];
548 const uint8_t a = curves->rgba_map[A];
549 const int slice_start = (in->height * jobnr ) / nb_jobs;
550 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
552 if (curves->is_16bit) {
553 for (y = slice_start; y < slice_end; y++) {
554 uint16_t *dstp = ( uint16_t *)(out->data[0] + y * out->linesize[0]);
555 const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y * in->linesize[0]);
557 for (x = 0; x < in->width * step; x += step) {
558 dstp[x + r] = curves->graph[R][srcp[x + r]];
559 dstp[x + g] = curves->graph[G][srcp[x + g]];
560 dstp[x + b] = curves->graph[B][srcp[x + b]];
561 if (!direct && step == 4)
562 dstp[x + a] = srcp[x + a];
566 uint8_t *dst = out->data[0] + slice_start * out->linesize[0];
567 const uint8_t *src = in->data[0] + slice_start * in->linesize[0];
569 for (y = slice_start; y < slice_end; y++) {
570 for (x = 0; x < in->width * step; x += step) {
571 dst[x + r] = curves->graph[R][src[x + r]];
572 dst[x + g] = curves->graph[G][src[x + g]];
573 dst[x + b] = curves->graph[B][src[x + b]];
574 if (!direct && step == 4)
575 dst[x + a] = src[x + a];
577 dst += out->linesize[0];
578 src += in ->linesize[0];
584 static int filter_slice_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
587 const CurvesContext *curves = ctx->priv;
588 const ThreadData *td = arg;
589 const AVFrame *in = td->in;
590 const AVFrame *out = td->out;
591 const int direct = out == in;
592 const int step = curves->step;
593 const uint8_t r = curves->rgba_map[R];
594 const uint8_t g = curves->rgba_map[G];
595 const uint8_t b = curves->rgba_map[B];
596 const uint8_t a = curves->rgba_map[A];
597 const int slice_start = (in->height * jobnr ) / nb_jobs;
598 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
600 if (curves->is_16bit) {
601 for (y = slice_start; y < slice_end; y++) {
602 uint16_t *dstrp = ( uint16_t *)(out->data[r] + y * out->linesize[r]);
603 uint16_t *dstgp = ( uint16_t *)(out->data[g] + y * out->linesize[g]);
604 uint16_t *dstbp = ( uint16_t *)(out->data[b] + y * out->linesize[b]);
605 uint16_t *dstap = ( uint16_t *)(out->data[a] + y * out->linesize[a]);
606 const uint16_t *srcrp = (const uint16_t *)(in ->data[r] + y * in->linesize[r]);
607 const uint16_t *srcgp = (const uint16_t *)(in ->data[g] + y * in->linesize[g]);
608 const uint16_t *srcbp = (const uint16_t *)(in ->data[b] + y * in->linesize[b]);
609 const uint16_t *srcap = (const uint16_t *)(in ->data[a] + y * in->linesize[a]);
611 for (x = 0; x < in->width; x++) {
612 dstrp[x] = curves->graph[R][srcrp[x]];
613 dstgp[x] = curves->graph[G][srcgp[x]];
614 dstbp[x] = curves->graph[B][srcbp[x]];
615 if (!direct && step == 4)
620 uint8_t *dstr = out->data[r] + slice_start * out->linesize[r];
621 uint8_t *dstg = out->data[g] + slice_start * out->linesize[g];
622 uint8_t *dstb = out->data[b] + slice_start * out->linesize[b];
623 uint8_t *dsta = out->data[a] + slice_start * out->linesize[a];
624 const uint8_t *srcr = in->data[r] + slice_start * in->linesize[r];
625 const uint8_t *srcg = in->data[g] + slice_start * in->linesize[g];
626 const uint8_t *srcb = in->data[b] + slice_start * in->linesize[b];
627 const uint8_t *srca = in->data[a] + slice_start * in->linesize[a];
629 for (y = slice_start; y < slice_end; y++) {
630 for (x = 0; x < in->width; x++) {
631 dstr[x] = curves->graph[R][srcr[x]];
632 dstg[x] = curves->graph[G][srcg[x]];
633 dstb[x] = curves->graph[B][srcb[x]];
634 if (!direct && step == 4)
637 dstr += out->linesize[r];
638 dstg += out->linesize[g];
639 dstb += out->linesize[b];
640 dsta += out->linesize[a];
641 srcr += in ->linesize[r];
642 srcg += in ->linesize[g];
643 srcb += in ->linesize[b];
644 srca += in ->linesize[a];
650 static int config_input(AVFilterLink *inlink)
653 AVFilterContext *ctx = inlink->dst;
654 CurvesContext *curves = ctx->priv;
655 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
656 char **pts = curves->comp_points_str;
657 struct keypoint *comp_points[NB_COMP + 1] = {0};
659 ff_fill_rgba_map(curves->rgba_map, inlink->format);
660 curves->is_16bit = desc->comp[0].depth > 8;
661 curves->depth = desc->comp[0].depth;
662 curves->lut_size = 1 << curves->depth;
663 curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
664 curves->filter_slice = desc->flags & AV_PIX_FMT_FLAG_PLANAR ? filter_slice_planar : filter_slice_packed;
666 for (i = 0; i < NB_COMP + 1; i++) {
667 curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
668 if (!curves->graph[i])
669 return AVERROR(ENOMEM);
670 ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
673 switch (curves->depth) {
674 case 8: ret = interpolate8 (ctx, curves->graph[i], comp_points[i]); break;
675 case 9: ret = interpolate9 (ctx, curves->graph[i], comp_points[i]); break;
676 case 10: ret = interpolate10(ctx, curves->graph[i], comp_points[i]); break;
677 case 12: ret = interpolate12(ctx, curves->graph[i], comp_points[i]); break;
678 case 14: ret = interpolate14(ctx, curves->graph[i], comp_points[i]); break;
679 case 16: ret = interpolate16(ctx, curves->graph[i], comp_points[i]); break;
686 for (i = 0; i < NB_COMP; i++)
687 for (j = 0; j < curves->lut_size; j++)
688 curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
691 if (av_log_get_level() >= AV_LOG_VERBOSE) {
692 for (i = 0; i < NB_COMP; i++) {
693 const struct keypoint *point = comp_points[i];
694 av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
696 av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
702 if (curves->plot_filename)
703 dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
705 for (i = 0; i < NB_COMP + 1; i++) {
706 struct keypoint *point = comp_points[i];
708 struct keypoint *next = point->next;
717 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
719 AVFilterContext *ctx = inlink->dst;
720 CurvesContext *curves = ctx->priv;
721 AVFilterLink *outlink = ctx->outputs[0];
725 if (av_frame_is_writable(in)) {
728 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
731 return AVERROR(ENOMEM);
733 av_frame_copy_props(out, in);
738 ctx->internal->execute(ctx, curves->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
743 return ff_filter_frame(outlink, out);
746 static av_cold void curves_uninit(AVFilterContext *ctx)
749 CurvesContext *curves = ctx->priv;
751 for (i = 0; i < NB_COMP + 1; i++)
752 av_freep(&curves->graph[i]);
755 static const AVFilterPad curves_inputs[] = {
758 .type = AVMEDIA_TYPE_VIDEO,
759 .filter_frame = filter_frame,
760 .config_props = config_input,
765 static const AVFilterPad curves_outputs[] = {
768 .type = AVMEDIA_TYPE_VIDEO,
773 AVFilter ff_vf_curves = {
775 .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
776 .priv_size = sizeof(CurvesContext),
778 .uninit = curves_uninit,
779 .query_formats = query_formats,
780 .inputs = curves_inputs,
781 .outputs = curves_outputs,
782 .priv_class = &curves_class,
783 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,