FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_curves.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 198 486 40.7%
Functions: 9 17 52.9%
Branches: 100 372 26.9%

Line Branch Exec Source
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/mem.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/bprint.h"
24 #include "libavutil/eval.h"
25 #include "libavutil/file.h"
26 #include "libavutil/file_open.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "drawutils.h"
32 #include "internal.h"
33 #include "video.h"
34
35 #define R 0
36 #define G 1
37 #define B 2
38 #define A 3
39
40 struct keypoint {
41 double x, y;
42 struct keypoint *next;
43 };
44
45 #define NB_COMP 3
46
47 enum preset {
48 PRESET_NONE,
49 PRESET_COLOR_NEGATIVE,
50 PRESET_CROSS_PROCESS,
51 PRESET_DARKER,
52 PRESET_INCREASE_CONTRAST,
53 PRESET_LIGHTER,
54 PRESET_LINEAR_CONTRAST,
55 PRESET_MEDIUM_CONTRAST,
56 PRESET_NEGATIVE,
57 PRESET_STRONG_CONTRAST,
58 PRESET_VINTAGE,
59 NB_PRESETS,
60 };
61
62 enum interp {
63 INTERP_NATURAL,
64 INTERP_PCHIP,
65 NB_INTERPS,
66 };
67
68 typedef struct CurvesContext {
69 const AVClass *class;
70 int preset;
71 char *comp_points_str[NB_COMP + 1];
72 char *comp_points_str_all;
73 uint16_t *graph[NB_COMP + 1];
74 int lut_size;
75 char *psfile;
76 uint8_t rgba_map[4];
77 int step;
78 char *plot_filename;
79 int saved_plot;
80 int is_16bit;
81 int depth;
82 int parsed_psfile;
83 int interp;
84
85 int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
86 } CurvesContext;
87
88 typedef struct ThreadData {
89 AVFrame *in, *out;
90 } ThreadData;
91
92 #define OFFSET(x) offsetof(CurvesContext, x)
93 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
94 static const AVOption curves_options[] = {
95 { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, .unit = "preset_name" },
96 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, 0, 0, FLAGS, .unit = "preset_name" },
97 { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, 0, 0, FLAGS, .unit = "preset_name" },
98 { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, 0, 0, FLAGS, .unit = "preset_name" },
99 { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, 0, 0, FLAGS, .unit = "preset_name" },
100 { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, 0, 0, FLAGS, .unit = "preset_name" },
101 { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, 0, 0, FLAGS, .unit = "preset_name" },
102 { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, 0, 0, FLAGS, .unit = "preset_name" },
103 { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, 0, 0, FLAGS, .unit = "preset_name" },
104 { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, 0, 0, FLAGS, .unit = "preset_name" },
105 { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, 0, 0, FLAGS, .unit = "preset_name" },
106 { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, 0, 0, FLAGS, .unit = "preset_name" },
107 { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
108 { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
109 { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
110 { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
111 { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
112 { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
113 { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
114 { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
115 { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
116 { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
117 { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
118 { "interp", "specify the kind of interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=INTERP_NATURAL}, INTERP_NATURAL, NB_INTERPS-1, FLAGS, .unit = "interp_name" },
119 { "natural", "natural cubic spline", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_NATURAL}, 0, 0, FLAGS, .unit = "interp_name" },
120 { "pchip", "monotonically cubic interpolation", 0, AV_OPT_TYPE_CONST, {.i64=INTERP_PCHIP}, 0, 0, FLAGS, .unit = "interp_name" },
121 { NULL }
122 };
123
124 AVFILTER_DEFINE_CLASS(curves);
125
126 static const struct {
127 const char *r;
128 const char *g;
129 const char *b;
130 const char *master;
131 } curves_presets[] = {
132 [PRESET_COLOR_NEGATIVE] = {
133 "0.129/1 0.466/0.498 0.725/0",
134 "0.109/1 0.301/0.498 0.517/0",
135 "0.098/1 0.235/0.498 0.423/0",
136 },
137 [PRESET_CROSS_PROCESS] = {
138 "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
139 "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
140 "0/0 0.231/0.094 0.709/0.874 1/1",
141 },
142 [PRESET_DARKER] = { .master = "0/0 0.5/0.4 1/1" },
143 [PRESET_INCREASE_CONTRAST] = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
144 [PRESET_LIGHTER] = { .master = "0/0 0.4/0.5 1/1" },
145 [PRESET_LINEAR_CONTRAST] = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
146 [PRESET_MEDIUM_CONTRAST] = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
147 [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
148 [PRESET_STRONG_CONTRAST] = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
149 [PRESET_VINTAGE] = {
150 "0/0.11 0.42/0.51 1/0.95",
151 "0/0 0.50/0.48 1/1",
152 "0/0.22 0.49/0.44 1/0.8",
153 }
154 };
155
156 9 static struct keypoint *make_point(double x, double y, struct keypoint *next)
157 {
158 9 struct keypoint *point = av_mallocz(sizeof(*point));
159
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!point)
161 return NULL;
162 9 point->x = x;
163 9 point->y = y;
164 9 point->next = next;
165 9 return point;
166 }
167
168 4 static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
169 int lut_size)
170 {
171 4 char *p = (char *)s; // strtod won't alter the string
172 4 struct keypoint *last = NULL;
173 4 const int scale = lut_size - 1;
174
175 /* construct a linked list based on the key points string */
176
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 3 times.
13 while (p && *p) {
177 9 struct keypoint *point = make_point(0, 0, NULL);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!point)
179 return AVERROR(ENOMEM);
180
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
9 point->x = av_strtod(p, &p); if (p && *p) p++;
181
3/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3 times.
9 point->y = av_strtod(p, &p); if (p && *p) p++;
182
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 9 times.
9 if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
183 av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
184 "x and y must be in the [0;1] range.\n", point->x, point->y);
185 return AVERROR(EINVAL);
186 }
187
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (!*points)
188 3 *points = point;
189
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (last) {
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if ((int)(last->x * scale) >= (int)(point->x * scale)) {
191 av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
192 "and (%f;%f) are too close from each other or not "
193 "strictly increasing on the x-axis\n",
194 last->x, last->y, point->x, point->y);
195 return AVERROR(EINVAL);
196 }
197 6 last->next = point;
198 }
199 9 last = point;
200 }
201
202
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
4 if (*points && !(*points)->next) {
203 av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
204 "this is unlikely to behave as you expect. You probably want"
205 "at least 2 points.",
206 (*points)->x, (*points)->y);
207 }
208
209 4 return 0;
210 }
211
212 4 static int get_nb_points(const struct keypoint *d)
213 {
214 4 int n = 0;
215
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 4 times.
13 while (d) {
216 9 n++;
217 9 d = d->next;
218 }
219 4 return n;
220 }
221
222 /**
223 * Natural cubic spline interpolation
224 * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
225 * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
226 */
227
228 #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uintp2_c(v, nbits))
229
230 4 static inline int interpolate(void *log_ctx, uint16_t *y,
231 const struct keypoint *points, int nbits)
232 {
233 4 int i, ret = 0;
234 4 const struct keypoint *point = points;
235 4 double xprev = 0;
236 4 const int lut_size = 1<<nbits;
237 4 const int scale = lut_size - 1;
238
239 double (*matrix)[3];
240 double *h, *r;
241 4 const int n = get_nb_points(points); // number of splines
242
243
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (n == 0) {
244
2/2
✓ Branch 0 taken 256 times.
✓ Branch 1 taken 1 times.
257 for (i = 0; i < lut_size; i++)
245 256 y[i] = i;
246 1 return 0;
247 }
248
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (n == 1) {
250 for (i = 0; i < lut_size; i++)
251 y[i] = CLIP(point->y * scale);
252 return 0;
253 }
254
255 3 matrix = av_calloc(n, sizeof(*matrix));
256 3 h = av_malloc((n - 1) * sizeof(*h));
257 3 r = av_calloc(n, sizeof(*r));
258
259
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (!matrix || !h || !r) {
260 ret = AVERROR(ENOMEM);
261 goto end;
262 }
263
264 /* h(i) = x(i+1) - x(i) */
265 3 i = -1;
266
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
12 for (point = points; point; point = point->next) {
267
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (i != -1)
268 6 h[i] = point->x - xprev;
269 9 xprev = point->x;
270 9 i++;
271 }
272
273 /* right-side of the polynomials, will be modified to contains the solution */
274 3 point = points;
275
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (i = 1; i < n - 1; i++) {
276 3 const double yp = point->y;
277 3 const double yc = point->next->y;
278 3 const double yn = point->next->next->y;
279 3 r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
280 3 point = point->next;
281 }
282
283 #define BD 0 /* sub diagonal (below main) */
284 #define MD 1 /* main diagonal (center) */
285 #define AD 2 /* sup diagonal (above main) */
286
287 /* left side of the polynomials into a tridiagonal matrix. */
288 3 matrix[0][MD] = matrix[n - 1][MD] = 1;
289
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (i = 1; i < n - 1; i++) {
290 3 matrix[i][BD] = h[i-1];
291 3 matrix[i][MD] = 2 * (h[i-1] + h[i]);
292 3 matrix[i][AD] = h[i];
293 }
294
295 /* tridiagonal solving of the linear system */
296
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = 1; i < n; i++) {
297 6 const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
298
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const double k = den ? 1./den : 1.;
299 6 matrix[i][AD] *= k;
300 6 r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
301 }
302
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (i = n - 2; i >= 0; i--)
303 6 r[i] = r[i] - matrix[i][AD] * r[i + 1];
304
305 3 point = points;
306
307 /* left padding */
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 for (i = 0; i < (int)(point->x * scale); i++)
309 y[i] = CLIP(point->y * scale);
310
311 /* compute the graph with x=[x0..xN] */
312 3 i = 0;
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(point->next); // always at least 2 key points
314
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 while (point->next) {
315 6 const double yc = point->y;
316 6 const double yn = point->next->y;
317
318 6 const double a = yc;
319 6 const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
320 6 const double c = r[i] / 2.;
321 6 const double d = (r[i+1] - r[i]) / (6.*h[i]);
322
323 int x;
324 6 const int x_start = point->x * scale;
325 6 const int x_end = point->next->x * scale;
326
327
4/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
6 av_assert0(x_start >= 0 && x_start < lut_size &&
328 x_end >= 0 && x_end < lut_size);
329
330
2/2
✓ Branch 0 taken 771 times.
✓ Branch 1 taken 6 times.
777 for (x = x_start; x <= x_end; x++) {
331 771 const double xx = (x - x_start) * 1./scale;
332 771 const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
333
1/2
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
771 y[x] = CLIP(yy * scale);
334 771 av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
335 }
336
337 6 point = point->next;
338 6 i++;
339 }
340
341 /* right padding */
342
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 for (i = (int)(point->x * scale); i < lut_size; i++)
343
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 y[i] = CLIP(point->y * scale);
344
345 3 end:
346 3 av_free(matrix);
347 3 av_free(h);
348 3 av_free(r);
349 3 return ret;
350
351 }
352
353 #define SIGN(x) (x > 0.0 ? 1 : x < 0.0 ? -1 : 0)
354
355 /**
356 * Evalaute the derivative of an edge endpoint
357 *
358 * @param h0 input interval of the interval closest to the edge
359 * @param h1 input interval of the interval next to the closest
360 * @param m0 linear slope of the interval closest to the edge
361 * @param m1 linear slope of the intervalnext to the closest
362 * @return edge endpoint derivative
363 *
364 * Based on scipy.interpolate._edge_case()
365 * https://github.com/scipy/scipy/blob/2e5883ef7af4f5ed4a5b80a1759a45e43163bf3f/scipy/interpolate/_cubic.py#L239
366 * which is a python implementation of the special case endpoints, as suggested in
367 * Cleve Moler, Numerical Computing with MATLAB, Chap 3.6 (pchiptx.m)
368 */
369 static double pchip_edge_case(double h0, double h1, double m0, double m1)
370 {
371 int mask, mask2;
372 double d;
373
374 d = ((2 * h0 + h1) * m0 - h0 * m1) / (h0 + h1);
375
376 mask = SIGN(d) != SIGN(m0);
377 mask2 = (SIGN(m0) != SIGN(m1)) && (fabs(d) > 3. * fabs(m0));
378
379 if (mask) d = 0.0;
380 else if (mask2) d = 3.0 * m0;
381
382 return d;
383 }
384
385 /**
386 * Evalaute the piecewise polynomial derivatives at endpoints
387 *
388 * @param n input interval of the interval closest to the edge
389 * @param hk input intervals
390 * @param mk linear slopes over intervals
391 * @param dk endpoint derivatives (output)
392 * @return 0 success
393 *
394 * Based on scipy.interpolate._find_derivatives()
395 * https://github.com/scipy/scipy/blob/2e5883ef7af4f5ed4a5b80a1759a45e43163bf3f/scipy/interpolate/_cubic.py#L254
396 */
397
398 static int pchip_find_derivatives(const int n, const double *hk, const double *mk, double *dk)
399 {
400 int ret = 0;
401 const int m = n - 1;
402 int8_t *smk;
403
404 smk = av_malloc(n);
405 if (!smk) {
406 ret = AVERROR(ENOMEM);
407 goto end;
408 }
409
410 /* smk = sgn(mk) */
411 for (int i = 0; i < n; i++) smk[i] = SIGN(mk[i]);
412
413 /* check the strict monotonicity */
414 for (int i = 0; i < m; i++) {
415 int8_t condition = (smk[i + 1] != smk[i]) || (mk[i + 1] == 0) || (mk[i] == 0);
416 if (condition) {
417 dk[i + 1] = 0.0;
418 } else {
419 double w1 = 2 * hk[i + 1] + hk[i];
420 double w2 = hk[i + 1] + 2 * hk[i];
421 dk[i + 1] = (w1 + w2) / (w1 / mk[i] + w2 / mk[i + 1]);
422 }
423 }
424
425 dk[0] = pchip_edge_case(hk[0], hk[1], mk[0], mk[1]);
426 dk[n] = pchip_edge_case(hk[n - 1], hk[n - 2], mk[n - 1], mk[n - 2]);
427
428 end:
429 av_free(smk);
430
431 return ret;
432 }
433
434 /**
435 * Evalaute half of the cubic hermite interpolation expression, wrt one interval endpoint
436 *
437 * @param x normalized input value at the endpoint
438 * @param f output value at the endpoint
439 * @param d derivative at the endpoint: normalized to the interval, and properly sign adjusted
440 * @return half of the interpolated value
441 */
442 static inline double interp_cubic_hermite_half(const double x, const double f,
443 const double d)
444 {
445 double x2 = x * x, x3 = x2 * x;
446 return f * (3.0 * x2 - 2.0 * x3) + d * (x3 - x2);
447 }
448
449 /**
450 * Prepare the lookup table by piecewise monotonic cubic interpolation (PCHIP)
451 *
452 * @param log_ctx for logging
453 * @param y output lookup table (output)
454 * @param points user-defined control points/endpoints
455 * @param nbits bitdepth
456 * @return 0 success
457 *
458 * References:
459 * [1] F. N. Fritsch and J. Butland, A method for constructing local monotone piecewise
460 * cubic interpolants, SIAM J. Sci. Comput., 5(2), 300-304 (1984). DOI:10.1137/0905021.
461 * [2] scipy.interpolate: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.PchipInterpolator.html
462 */
463 static inline int interpolate_pchip(void *log_ctx, uint16_t *y,
464 const struct keypoint *points, int nbits)
465 {
466 const struct keypoint *point = points;
467 const int lut_size = 1<<nbits;
468 const int n = get_nb_points(points); // number of endpoints
469 double *xi, *fi, *di, *hi, *mi;
470 const int scale = lut_size - 1; // white value
471 uint16_t x; /* input index/value */
472 int ret = 0;
473
474 /* no change for n = 0 or 1 */
475 if (n == 0) {
476 /* no points, no change */
477 for (int i = 0; i < lut_size; i++) y[i] = i;
478 return 0;
479 }
480
481 if (n == 1) {
482 /* 1 point - 1 color everywhere */
483 const uint16_t yval = CLIP(point->y * scale);
484 for (int i = 0; i < lut_size; i++) y[i] = yval;
485 return 0;
486 }
487
488 xi = av_calloc(3*n + 2*(n-1), sizeof(double)); /* output values at interval endpoints */
489 if (!xi) {
490 ret = AVERROR(ENOMEM);
491 goto end;
492 }
493
494 fi = xi + n; /* output values at inteval endpoints */
495 di = fi + n; /* output slope wrt normalized input at interval endpoints */
496 hi = di + n; /* interval widths */
497 mi = hi + n - 1; /* linear slope over intervals */
498
499 /* scale endpoints and store them in a contiguous memory block */
500 for (int i = 0; i < n; i++) {
501 xi[i] = point->x * scale;
502 fi[i] = point->y * scale;
503 point = point->next;
504 }
505
506 /* h(i) = x(i+1) - x(i); mi(i) = (f(i+1)-f(i))/h(i) */
507 for (int i = 0; i < n - 1; i++) {
508 const double val = (xi[i+1]-xi[i]);
509 hi[i] = val;
510 mi[i] = (fi[i+1]-fi[i]) / val;
511 }
512
513 if (n == 2) {
514 /* edge case, use linear interpolation */
515 const double m = mi[0], b = fi[0] - xi[0]*m;
516 for (int i = 0; i < lut_size; i++) y[i] = CLIP(i*m + b);
517 goto end;
518 }
519
520 /* compute the derivatives at the endpoints*/
521 ret = pchip_find_derivatives(n-1, hi, mi, di);
522 if (ret)
523 goto end;
524
525 /* interpolate/extrapolate */
526 x = 0;
527 if (xi[0] > 0) {
528 /* below first endpoint, use the first endpoint value*/
529 const double xi0 = xi[0];
530 const double yi0 = fi[0];
531 const uint16_t yval = CLIP(yi0);
532 for (; x < xi0; x++) {
533 y[x] = yval;
534 av_log(log_ctx, AV_LOG_TRACE, "f(%f)=%f -> y[%d]=%d\n", xi0, yi0, x, y[x]);
535 }
536 av_log(log_ctx, AV_LOG_DEBUG, "Interval -1: [0, %d] -> %d\n", x - 1, yval);
537 }
538
539 /* for each interval */
540 for (int i = 0, x0 = x; i < n-1; i++, x0 = x) {
541 const double xi0 = xi[i]; /* start-of-interval input value */
542 const double xi1 = xi[i + 1]; /* end-of-interval input value */
543 const double h = hi[i]; /* interval width */
544 const double f0 = fi[i]; /* start-of-interval output value */
545 const double f1 = fi[i + 1]; /* end-of-interval output value */
546 const double d0 = di[i]; /* start-of-interval derivative */
547 const double d1 = di[i + 1]; /* end-of-interval derivative */
548
549 /* fill the lut over the interval */
550 for (; x < xi1; x++) { /* safe not to check j < lut_size */
551 const double xx = (x - xi0) / h; /* normalize input */
552 const double yy = interp_cubic_hermite_half(1 - xx, f0, -h * d0)
553 + interp_cubic_hermite_half(xx, f1, h * d1);
554 y[x] = CLIP(yy);
555 av_log(log_ctx, AV_LOG_TRACE, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
556 }
557
558 if (x > x0)
559 av_log(log_ctx, AV_LOG_DEBUG, "Interval %d: [%d, %d] -> [%d, %d]\n",
560 i, x0, x-1, y[x0], y[x-1]);
561 else
562 av_log(log_ctx, AV_LOG_DEBUG, "Interval %d: empty\n", i);
563 }
564
565 if (x && x < lut_size) {
566 /* above the last endpoint, use the last endpoint value*/
567 const double xi1 = xi[n - 1];
568 const double yi1 = fi[n - 1];
569 const uint16_t yval = CLIP(yi1);
570 av_log(log_ctx, AV_LOG_DEBUG, "Interval %d: [%d, %d] -> %d\n",
571 n-1, x, lut_size - 1, yval);
572 for (; x && x < lut_size; x++) { /* loop until int overflow */
573 y[x] = yval;
574 av_log(log_ctx, AV_LOG_TRACE, "f(%f)=%f -> y[%d]=%d\n", xi1, yi1, x, yval);
575 }
576 }
577
578 end:
579 av_free(xi);
580 return ret;
581 }
582
583
584 static int parse_psfile(AVFilterContext *ctx, const char *fname)
585 {
586 CurvesContext *curves = ctx->priv;
587 uint8_t *buf;
588 size_t size;
589 int i, ret, av_unused(version), nb_curves;
590 AVBPrint ptstr;
591 static const int comp_ids[] = {3, 0, 1, 2};
592
593 av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
594
595 ret = av_file_map(fname, &buf, &size, 0, NULL);
596 if (ret < 0)
597 return ret;
598
599 #define READ16(dst) do { \
600 if (size < 2) { \
601 ret = AVERROR_INVALIDDATA; \
602 goto end; \
603 } \
604 dst = AV_RB16(buf); \
605 buf += 2; \
606 size -= 2; \
607 } while (0)
608
609 READ16(version);
610 READ16(nb_curves);
611 for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
612 int nb_points, n;
613 av_bprint_clear(&ptstr);
614 READ16(nb_points);
615 for (n = 0; n < nb_points; n++) {
616 int y, x;
617 READ16(y);
618 READ16(x);
619 av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
620 }
621 if (*ptstr.str) {
622 char **pts = &curves->comp_points_str[comp_ids[i]];
623 if (!*pts) {
624 *pts = av_strdup(ptstr.str);
625 av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
626 i, comp_ids[i], nb_points, *pts);
627 if (!*pts) {
628 ret = AVERROR(ENOMEM);
629 goto end;
630 }
631 }
632 }
633 }
634 end:
635 av_bprint_finalize(&ptstr, NULL);
636 av_file_unmap(buf, size);
637 return ret;
638 }
639
640 static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
641 struct keypoint *comp_points[NB_COMP + 1],
642 int lut_size)
643 {
644 int i;
645 AVBPrint buf;
646 const double scale = 1. / (lut_size - 1);
647 static const char * const colors[] = { "red", "green", "blue", "#404040", };
648 FILE *f = avpriv_fopen_utf8(fname, "w");
649
650 av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
651
652 if (!f) {
653 int ret = AVERROR(errno);
654 av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
655 fname, av_err2str(ret));
656 return ret;
657 }
658
659 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
660
661 av_bprintf(&buf, "set xtics 0.1\n");
662 av_bprintf(&buf, "set ytics 0.1\n");
663 av_bprintf(&buf, "set size square\n");
664 av_bprintf(&buf, "set grid\n");
665
666 for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
667 av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
668 i ? ", " : "plot ", colors[i]);
669 if (comp_points[i])
670 av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
671 colors[i]);
672 }
673 av_bprintf(&buf, "\n");
674
675 for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
676 int x;
677
678 /* plot generated values */
679 for (x = 0; x < lut_size; x++)
680 av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
681 av_bprintf(&buf, "e\n");
682
683 /* plot user knots */
684 if (comp_points[i]) {
685 const struct keypoint *point = comp_points[i];
686
687 while (point) {
688 av_bprintf(&buf, "%f %f\n", point->x, point->y);
689 point = point->next;
690 }
691 av_bprintf(&buf, "e\n");
692 }
693 }
694
695 fwrite(buf.str, 1, buf.len, f);
696 fclose(f);
697 av_bprint_finalize(&buf, NULL);
698 return 0;
699 }
700
701 2 static av_cold int curves_init(AVFilterContext *ctx)
702 {
703 int i, ret;
704 2 CurvesContext *curves = ctx->priv;
705 2 char **pts = curves->comp_points_str;
706 2 const char *allp = curves->comp_points_str_all;
707
708 //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
709 // allp = curves_presets[curves->preset].all;
710
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (allp) {
712 for (i = 0; i < NB_COMP; i++) {
713 if (!pts[i])
714 pts[i] = av_strdup(allp);
715 if (!pts[i])
716 return AVERROR(ENOMEM);
717 }
718 }
719
720
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (curves->psfile && !curves->parsed_psfile) {
721 ret = parse_psfile(ctx, curves->psfile);
722 if (ret < 0)
723 return ret;
724 curves->parsed_psfile = 1;
725 }
726
727
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (curves->preset != PRESET_NONE) {
728 #define SET_COMP_IF_NOT_SET(n, name) do { \
729 if (!pts[n] && curves_presets[curves->preset].name) { \
730 pts[n] = av_strdup(curves_presets[curves->preset].name); \
731 if (!pts[n]) \
732 return AVERROR(ENOMEM); \
733 } \
734 } while (0)
735
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
2 SET_COMP_IF_NOT_SET(0, r);
736
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
2 SET_COMP_IF_NOT_SET(1, g);
737
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
2 SET_COMP_IF_NOT_SET(2, b);
738
2/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2 SET_COMP_IF_NOT_SET(3, master);
739 2 curves->preset = PRESET_NONE;
740 }
741
742 2 return 0;
743 }
744
745 static int filter_slice_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
746 {
747 int x, y;
748 const CurvesContext *curves = ctx->priv;
749 const ThreadData *td = arg;
750 const AVFrame *in = td->in;
751 const AVFrame *out = td->out;
752 const int direct = out == in;
753 const int step = curves->step;
754 const uint8_t r = curves->rgba_map[R];
755 const uint8_t g = curves->rgba_map[G];
756 const uint8_t b = curves->rgba_map[B];
757 const uint8_t a = curves->rgba_map[A];
758 const int slice_start = (in->height * jobnr ) / nb_jobs;
759 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
760
761 if (curves->is_16bit) {
762 for (y = slice_start; y < slice_end; y++) {
763 uint16_t *dstp = ( uint16_t *)(out->data[0] + y * out->linesize[0]);
764 const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y * in->linesize[0]);
765
766 for (x = 0; x < in->width * step; x += step) {
767 dstp[x + r] = curves->graph[R][srcp[x + r]];
768 dstp[x + g] = curves->graph[G][srcp[x + g]];
769 dstp[x + b] = curves->graph[B][srcp[x + b]];
770 if (!direct && step == 4)
771 dstp[x + a] = srcp[x + a];
772 }
773 }
774 } else {
775 uint8_t *dst = out->data[0] + slice_start * out->linesize[0];
776 const uint8_t *src = in->data[0] + slice_start * in->linesize[0];
777
778 for (y = slice_start; y < slice_end; y++) {
779 for (x = 0; x < in->width * step; x += step) {
780 dst[x + r] = curves->graph[R][src[x + r]];
781 dst[x + g] = curves->graph[G][src[x + g]];
782 dst[x + b] = curves->graph[B][src[x + b]];
783 if (!direct && step == 4)
784 dst[x + a] = src[x + a];
785 }
786 dst += out->linesize[0];
787 src += in ->linesize[0];
788 }
789 }
790 return 0;
791 }
792
793 45 static int filter_slice_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
794 {
795 int x, y;
796 45 const CurvesContext *curves = ctx->priv;
797 45 const ThreadData *td = arg;
798 45 const AVFrame *in = td->in;
799 45 const AVFrame *out = td->out;
800 45 const int direct = out == in;
801 45 const int step = curves->step;
802 45 const uint8_t r = curves->rgba_map[R];
803 45 const uint8_t g = curves->rgba_map[G];
804 45 const uint8_t b = curves->rgba_map[B];
805 45 const uint8_t a = curves->rgba_map[A];
806 45 const int slice_start = (in->height * jobnr ) / nb_jobs;
807 45 const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
808
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (curves->is_16bit) {
810 for (y = slice_start; y < slice_end; y++) {
811 uint16_t *dstrp = ( uint16_t *)(out->data[r] + y * out->linesize[r]);
812 uint16_t *dstgp = ( uint16_t *)(out->data[g] + y * out->linesize[g]);
813 uint16_t *dstbp = ( uint16_t *)(out->data[b] + y * out->linesize[b]);
814 uint16_t *dstap = ( uint16_t *)(out->data[a] + y * out->linesize[a]);
815 const uint16_t *srcrp = (const uint16_t *)(in ->data[r] + y * in->linesize[r]);
816 const uint16_t *srcgp = (const uint16_t *)(in ->data[g] + y * in->linesize[g]);
817 const uint16_t *srcbp = (const uint16_t *)(in ->data[b] + y * in->linesize[b]);
818 const uint16_t *srcap = (const uint16_t *)(in ->data[a] + y * in->linesize[a]);
819
820 for (x = 0; x < in->width; x++) {
821 dstrp[x] = curves->graph[R][srcrp[x]];
822 dstgp[x] = curves->graph[G][srcgp[x]];
823 dstbp[x] = curves->graph[B][srcbp[x]];
824 if (!direct && step == 4)
825 dstap[x] = srcap[x];
826 }
827 }
828 } else {
829 45 uint8_t *dstr = out->data[r] + slice_start * out->linesize[r];
830 45 uint8_t *dstg = out->data[g] + slice_start * out->linesize[g];
831 45 uint8_t *dstb = out->data[b] + slice_start * out->linesize[b];
832 45 uint8_t *dsta = out->data[a] + slice_start * out->linesize[a];
833 45 const uint8_t *srcr = in->data[r] + slice_start * in->linesize[r];
834 45 const uint8_t *srcg = in->data[g] + slice_start * in->linesize[g];
835 45 const uint8_t *srcb = in->data[b] + slice_start * in->linesize[b];
836 45 const uint8_t *srca = in->data[a] + slice_start * in->linesize[a];
837
838
2/2
✓ Branch 0 taken 2400 times.
✓ Branch 1 taken 45 times.
2445 for (y = slice_start; y < slice_end; y++) {
839
2/2
✓ Branch 0 taken 1536000 times.
✓ Branch 1 taken 2400 times.
1538400 for (x = 0; x < in->width; x++) {
840 1536000 dstr[x] = curves->graph[R][srcr[x]];
841 1536000 dstg[x] = curves->graph[G][srcg[x]];
842 1536000 dstb[x] = curves->graph[B][srcb[x]];
843
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1536000 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1536000 if (!direct && step == 4)
844 dsta[x] = srca[x];
845 }
846 2400 dstr += out->linesize[r];
847 2400 dstg += out->linesize[g];
848 2400 dstb += out->linesize[b];
849 2400 dsta += out->linesize[a];
850 2400 srcr += in ->linesize[r];
851 2400 srcg += in ->linesize[g];
852 2400 srcb += in ->linesize[b];
853 2400 srca += in ->linesize[a];
854 }
855 }
856 45 return 0;
857 }
858
859 1 static int config_input(AVFilterLink *inlink)
860 {
861 int i, j, ret;
862 1 AVFilterContext *ctx = inlink->dst;
863 1 CurvesContext *curves = ctx->priv;
864 1 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
865 1 char **pts = curves->comp_points_str;
866 1 struct keypoint *comp_points[NB_COMP + 1] = {0};
867
868 1 ff_fill_rgba_map(curves->rgba_map, inlink->format);
869 1 curves->is_16bit = desc->comp[0].depth > 8;
870 1 curves->depth = desc->comp[0].depth;
871 1 curves->lut_size = 1 << curves->depth;
872 1 curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
873
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 curves->filter_slice = desc->flags & AV_PIX_FMT_FLAG_PLANAR ? filter_slice_planar : filter_slice_packed;
874
875
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; i < NB_COMP + 1; i++) {
876
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!curves->graph[i])
877 4 curves->graph[i] = av_calloc(curves->lut_size, sizeof(*curves->graph[0]));
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!curves->graph[i])
879 return AVERROR(ENOMEM);
880 4 ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
881
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
882 return ret;
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (curves->interp == INTERP_PCHIP)
884 ret = interpolate_pchip(ctx, curves->graph[i], comp_points[i], curves->depth);
885 else
886 4 ret = interpolate(ctx, curves->graph[i], comp_points[i], curves->depth);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
888 return ret;
889 }
890
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (pts[NB_COMP]) {
892 for (i = 0; i < NB_COMP; i++)
893 for (j = 0; j < curves->lut_size; j++)
894 curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
895 }
896
897
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_log_get_level() >= AV_LOG_VERBOSE) {
898 for (i = 0; i < NB_COMP; i++) {
899 const struct keypoint *point = comp_points[i];
900 av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
901 while (point) {
902 av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
903 point = point->next;
904 }
905 }
906 }
907
908
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (curves->plot_filename && !curves->saved_plot) {
909 dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
910 curves->saved_plot = 1;
911 }
912
913
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; i < NB_COMP + 1; i++) {
914 4 struct keypoint *point = comp_points[i];
915
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 4 times.
13 while (point) {
916 9 struct keypoint *next = point->next;
917 9 av_free(point);
918 9 point = next;
919 }
920 }
921
922 1 return 0;
923 }
924
925 5 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
926 {
927 5 AVFilterContext *ctx = inlink->dst;
928 5 CurvesContext *curves = ctx->priv;
929 5 AVFilterLink *outlink = ctx->outputs[0];
930 AVFrame *out;
931 ThreadData td;
932
933
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (av_frame_is_writable(in)) {
934 5 out = in;
935 } else {
936 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
937 if (!out) {
938 av_frame_free(&in);
939 return AVERROR(ENOMEM);
940 }
941 av_frame_copy_props(out, in);
942 }
943
944 5 td.in = in;
945 5 td.out = out;
946 5 ff_filter_execute(ctx, curves->filter_slice, &td, NULL,
947
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
948
949
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (out != in)
950 av_frame_free(&in);
951
952 5 return ff_filter_frame(outlink, out);
953 }
954
955 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
956 char *res, int res_len, int flags)
957 {
958 CurvesContext *curves = ctx->priv;
959 int ret;
960
961 if (!strcmp(cmd, "plot")) {
962 curves->saved_plot = 0;
963 } else if (!strcmp(cmd, "all") || !strcmp(cmd, "preset") || !strcmp(cmd, "psfile") || !strcmp(cmd, "interp")) {
964 if (!strcmp(cmd, "psfile"))
965 curves->parsed_psfile = 0;
966 av_freep(&curves->comp_points_str_all);
967 av_freep(&curves->comp_points_str[0]);
968 av_freep(&curves->comp_points_str[1]);
969 av_freep(&curves->comp_points_str[2]);
970 av_freep(&curves->comp_points_str[NB_COMP]);
971 } else if (!strcmp(cmd, "red") || !strcmp(cmd, "r")) {
972 av_freep(&curves->comp_points_str[0]);
973 } else if (!strcmp(cmd, "green") || !strcmp(cmd, "g")) {
974 av_freep(&curves->comp_points_str[1]);
975 } else if (!strcmp(cmd, "blue") || !strcmp(cmd, "b")) {
976 av_freep(&curves->comp_points_str[2]);
977 } else if (!strcmp(cmd, "master") || !strcmp(cmd, "m")) {
978 av_freep(&curves->comp_points_str[NB_COMP]);
979 }
980
981 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
982 if (ret < 0)
983 return ret;
984
985 ret = curves_init(ctx);
986 if (ret < 0)
987 return ret;
988 return config_input(ctx->inputs[0]);
989 }
990
991 2 static av_cold void curves_uninit(AVFilterContext *ctx)
992 {
993 int i;
994 2 CurvesContext *curves = ctx->priv;
995
996
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (i = 0; i < NB_COMP + 1; i++)
997 8 av_freep(&curves->graph[i]);
998 2 }
999
1000 static const AVFilterPad curves_inputs[] = {
1001 {
1002 .name = "default",
1003 .type = AVMEDIA_TYPE_VIDEO,
1004 .filter_frame = filter_frame,
1005 .config_props = config_input,
1006 },
1007 };
1008
1009 const AVFilter ff_vf_curves = {
1010 .name = "curves",
1011 .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
1012 .priv_size = sizeof(CurvesContext),
1013 .init = curves_init,
1014 .uninit = curves_uninit,
1015 FILTER_INPUTS(curves_inputs),
1016 FILTER_OUTPUTS(ff_video_default_filterpad),
1017 FILTER_PIXFMTS(AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
1018 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
1019 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
1020 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
1021 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
1022 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
1023 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
1024 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
1025 AV_PIX_FMT_GBRP9,
1026 AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
1027 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
1028 AV_PIX_FMT_GBRP14,
1029 AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16),
1030 .priv_class = &curves_class,
1031 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
1032 .process_command = process_command,
1033 };
1034