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