GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/vf_curves.c Lines: 200 362 55.2 %
Date: 2020-08-14 10:39:37 Branches: 100 251 39.8 %

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/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 CurvesContext {
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
    int depth;
74
75
    int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
76
} CurvesContext;
77
78
typedef struct ThreadData {
79
    AVFrame *in, *out;
80
} ThreadData;
81
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 },
108
    { NULL }
109
};
110
111
AVFILTER_DEFINE_CLASS(curves);
112
113
static const struct {
114
    const char *r;
115
    const char *g;
116
    const char *b;
117
    const char *master;
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",
123
    },
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",
128
    },
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" },
136
    [PRESET_VINTAGE] = {
137
        "0/0.11 0.42/0.51 1/0.95",
138
        "0/0 0.50/0.48 1/1",
139
        "0/0.22 0.49/0.44 1/0.8",
140
    }
141
};
142
143
9
static struct keypoint *make_point(double x, double y, struct keypoint *next)
144
{
145
9
    struct keypoint *point = av_mallocz(sizeof(*point));
146
147
9
    if (!point)
148
        return NULL;
149
9
    point->x = x;
150
9
    point->y = y;
151
9
    point->next = next;
152
9
    return point;
153
}
154
155
4
static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
156
                            int lut_size)
157
{
158
4
    char *p = (char *)s; // strtod won't alter the string
159
4
    struct keypoint *last = NULL;
160
4
    const int scale = lut_size - 1;
161
162
    /* construct a linked list based on the key points string */
163

13
    while (p && *p) {
164
9
        struct keypoint *point = make_point(0, 0, NULL);
165
9
        if (!point)
166
            return AVERROR(ENOMEM);
167

9
        point->x = av_strtod(p, &p); if (p && *p) p++;
168

9
        point->y = av_strtod(p, &p); if (p && *p) p++;
169


9
        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);
173
        }
174
9
        if (!*points)
175
3
            *points = point;
176
9
        if (last) {
177
6
            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);
183
            }
184
6
            last->next = point;
185
        }
186
9
        last = point;
187
    }
188
189

4
    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);
194
    }
195
196
4
    return 0;
197
}
198
199
4
static int get_nb_points(const struct keypoint *d)
200
{
201
4
    int n = 0;
202
13
    while (d) {
203
9
        n++;
204
9
        d = d->next;
205
    }
206
4
    return n;
207
}
208
209
/**
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
213
 */
214
215
#define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uintp2_c(v, nbits))
216
217
4
static inline int interpolate(void *log_ctx, uint16_t *y,
218
                              const struct keypoint *points, int nbits)
219
{
220
4
    int i, ret = 0;
221
4
    const struct keypoint *point = points;
222
4
    double xprev = 0;
223
4
    const int lut_size = 1<<nbits;
224
4
    const int scale = lut_size - 1;
225
226
    double (*matrix)[3];
227
    double *h, *r;
228
4
    const int n = get_nb_points(points); // number of splines
229
230
4
    if (n == 0) {
231
257
        for (i = 0; i < lut_size; i++)
232
256
            y[i] = i;
233
1
        return 0;
234
    }
235
236
3
    if (n == 1) {
237
        for (i = 0; i < lut_size; i++)
238
            y[i] = CLIP(point->y * scale);
239
        return 0;
240
    }
241
242
3
    matrix = av_calloc(n, sizeof(*matrix));
243
3
    h = av_malloc((n - 1) * sizeof(*h));
244
3
    r = av_calloc(n, sizeof(*r));
245
246

3
    if (!matrix || !h || !r) {
247
        ret = AVERROR(ENOMEM);
248
        goto end;
249
    }
250
251
    /* h(i) = x(i+1) - x(i) */
252
3
    i = -1;
253
12
    for (point = points; point; point = point->next) {
254
9
        if (i != -1)
255
6
            h[i] = point->x - xprev;
256
9
        xprev = point->x;
257
9
        i++;
258
    }
259
260
    /* right-side of the polynomials, will be modified to contains the solution */
261
3
    point = points;
262
6
    for (i = 1; i < n - 1; i++) {
263
3
        const double yp = point->y;
264
3
        const double yc = point->next->y;
265
3
        const double yn = point->next->next->y;
266
3
        r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
267
3
        point = point->next;
268
    }
269
270
#define BD 0 /* sub  diagonal (below main) */
271
#define MD 1 /* main diagonal (center) */
272
#define AD 2 /* sup  diagonal (above main) */
273
274
    /* left side of the polynomials into a tridiagonal matrix. */
275
3
    matrix[0][MD] = matrix[n - 1][MD] = 1;
276
6
    for (i = 1; i < n - 1; i++) {
277
3
        matrix[i][BD] = h[i-1];
278
3
        matrix[i][MD] = 2 * (h[i-1] + h[i]);
279
3
        matrix[i][AD] = h[i];
280
    }
281
282
    /* tridiagonal solving of the linear system */
283
9
    for (i = 1; i < n; i++) {
284
6
        const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
285
6
        const double k = den ? 1./den : 1.;
286
6
        matrix[i][AD] *= k;
287
6
        r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
288
    }
289
9
    for (i = n - 2; i >= 0; i--)
290
6
        r[i] = r[i] - matrix[i][AD] * r[i + 1];
291
292
3
    point = points;
293
294
    /* left padding */
295
3
    for (i = 0; i < (int)(point->x * scale); i++)
296
        y[i] = CLIP(point->y * scale);
297
298
    /* compute the graph with x=[x0..xN] */
299
3
    i = 0;
300
3
    av_assert0(point->next); // always at least 2 key points
301
9
    while (point->next) {
302
6
        const double yc = point->y;
303
6
        const double yn = point->next->y;
304
305
6
        const double a = yc;
306
6
        const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
307
6
        const double c = r[i] / 2.;
308
6
        const double d = (r[i+1] - r[i]) / (6.*h[i]);
309
310
        int x;
311
6
        const int x_start = point->x       * scale;
312
6
        const int x_end   = point->next->x * scale;
313
314


6
        av_assert0(x_start >= 0 && x_start < lut_size &&
315
                   x_end   >= 0 && x_end   < lut_size);
316
317
777
        for (x = x_start; x <= x_end; x++) {
318
771
            const double xx = (x - x_start) * 1./scale;
319
771
            const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
320
771
            y[x] = CLIP(yy * scale);
321
771
            av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
322
        }
323
324
6
        point = point->next;
325
6
        i++;
326
    }
327
328
    /* right padding */
329
6
    for (i = (int)(point->x * scale); i < lut_size; i++)
330
3
        y[i] = CLIP(point->y * scale);
331
332
3
end:
333
3
    av_free(matrix);
334
3
    av_free(h);
335
3
    av_free(r);
336
3
    return ret;
337
}
338
339
#define DECLARE_INTERPOLATE_FUNC(nbits)                                     \
340
static int interpolate##nbits(void *log_ctx, uint16_t *y,                   \
341
                              const struct keypoint *points)                \
342
{                                                                           \
343
    return interpolate(log_ctx, y, points, nbits);                          \
344
}
345
346
4
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)
352
353
static int parse_psfile(AVFilterContext *ctx, const char *fname)
354
{
355
    CurvesContext *curves = ctx->priv;
356
    uint8_t *buf;
357
    size_t size;
358
    int i, ret, av_unused(version), nb_curves;
359
    AVBPrint ptstr;
360
    static const int comp_ids[] = {3, 0, 1, 2};
361
362
    av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
363
364
    ret = av_file_map(fname, &buf, &size, 0, NULL);
365
    if (ret < 0)
366
        return ret;
367
368
#define READ16(dst) do {                \
369
    if (size < 2) {                     \
370
        ret = AVERROR_INVALIDDATA;      \
371
        goto end;                       \
372
    }                                   \
373
    dst = AV_RB16(buf);                 \
374
    buf  += 2;                          \
375
    size -= 2;                          \
376
} while (0)
377
378
    READ16(version);
379
    READ16(nb_curves);
380
    for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
381
        int nb_points, n;
382
        av_bprint_clear(&ptstr);
383
        READ16(nb_points);
384
        for (n = 0; n < nb_points; n++) {
385
            int y, x;
386
            READ16(y);
387
            READ16(x);
388
            av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
389
        }
390
        if (*ptstr.str) {
391
            char **pts = &curves->comp_points_str[comp_ids[i]];
392
            if (!*pts) {
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);
396
                if (!*pts) {
397
                    ret = AVERROR(ENOMEM);
398
                    goto end;
399
                }
400
            }
401
        }
402
    }
403
end:
404
    av_bprint_finalize(&ptstr, NULL);
405
    av_file_unmap(buf, size);
406
    return ret;
407
}
408
409
static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
410
                       struct keypoint *comp_points[NB_COMP + 1],
411
                       int lut_size)
412
{
413
    int i;
414
    AVBPrint buf;
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");
418
419
    av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
420
421
    if (!f) {
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));
425
        return ret;
426
    }
427
428
    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
429
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");
434
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]);
438
        if (comp_points[i])
439
            av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
440
                    colors[i]);
441
    }
442
    av_bprintf(&buf, "\n");
443
444
    for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
445
        int x;
446
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");
451
452
        /* plot user knots */
453
        if (comp_points[i]) {
454
            const struct keypoint *point = comp_points[i];
455
456
            while (point) {
457
                av_bprintf(&buf, "%f %f\n", point->x, point->y);
458
                point = point->next;
459
            }
460
            av_bprintf(&buf, "e\n");
461
        }
462
    }
463
464
    fwrite(buf.str, 1, buf.len, f);
465
    fclose(f);
466
    av_bprint_finalize(&buf, NULL);
467
    return 0;
468
}
469
470
1
static av_cold int curves_init(AVFilterContext *ctx)
471
{
472
    int i, ret;
473
1
    CurvesContext *curves = ctx->priv;
474
1
    char **pts = curves->comp_points_str;
475
1
    const char *allp = curves->comp_points_str_all;
476
477
    //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
478
    //    allp = curves_presets[curves->preset].all;
479
480
1
    if (allp) {
481
        for (i = 0; i < NB_COMP; i++) {
482
            if (!pts[i])
483
                pts[i] = av_strdup(allp);
484
            if (!pts[i])
485
                return AVERROR(ENOMEM);
486
        }
487
    }
488
489
1
    if (curves->psfile) {
490
        ret = parse_psfile(ctx, curves->psfile);
491
        if (ret < 0)
492
            return ret;
493
    }
494
495
1
    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);    \
499
        if (!pts[n])                                                \
500
            return AVERROR(ENOMEM);                                 \
501
    }                                                               \
502
} while (0)
503

1
        SET_COMP_IF_NOT_SET(0, r);
504

1
        SET_COMP_IF_NOT_SET(1, g);
505

1
        SET_COMP_IF_NOT_SET(2, b);
506

1
        SET_COMP_IF_NOT_SET(3, master);
507
    }
508
509
1
    return 0;
510
}
511
512
1
static int query_formats(AVFilterContext *ctx)
513
{
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,
523
        AV_PIX_FMT_GBRP9,
524
        AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
525
        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
526
        AV_PIX_FMT_GBRP14,
527
        AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
528
        AV_PIX_FMT_NONE
529
    };
530
1
    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
531
1
    if (!fmts_list)
532
        return AVERROR(ENOMEM);
533
1
    return ff_set_common_formats(ctx, fmts_list);
534
}
535
536
static int filter_slice_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
537
{
538
    int x, y;
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;
551
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]);
556
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];
563
            }
564
        }
565
    } else {
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];
568
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];
576
            }
577
            dst += out->linesize[0];
578
            src += in ->linesize[0];
579
        }
580
    }
581
    return 0;
582
}
583
584
45
static int filter_slice_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
585
{
586
    int x, y;
587
45
    const CurvesContext *curves = ctx->priv;
588
45
    const ThreadData *td = arg;
589
45
    const AVFrame *in  = td->in;
590
45
    const AVFrame *out = td->out;
591
45
    const int direct = out == in;
592
45
    const int step = curves->step;
593
45
    const uint8_t r = curves->rgba_map[R];
594
45
    const uint8_t g = curves->rgba_map[G];
595
45
    const uint8_t b = curves->rgba_map[B];
596
45
    const uint8_t a = curves->rgba_map[A];
597
45
    const int slice_start = (in->height *  jobnr   ) / nb_jobs;
598
45
    const int slice_end   = (in->height * (jobnr+1)) / nb_jobs;
599
600
45
    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]);
610
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)
616
                    dstap[x] = srcap[x];
617
            }
618
        }
619
    } else {
620
45
        uint8_t       *dstr = out->data[r] + slice_start * out->linesize[r];
621
45
        uint8_t       *dstg = out->data[g] + slice_start * out->linesize[g];
622
45
        uint8_t       *dstb = out->data[b] + slice_start * out->linesize[b];
623
45
        uint8_t       *dsta = out->data[a] + slice_start * out->linesize[a];
624
45
        const uint8_t *srcr =  in->data[r] + slice_start *  in->linesize[r];
625
45
        const uint8_t *srcg =  in->data[g] + slice_start *  in->linesize[g];
626
45
        const uint8_t *srcb =  in->data[b] + slice_start *  in->linesize[b];
627
45
        const uint8_t *srca =  in->data[a] + slice_start *  in->linesize[a];
628
629
2445
        for (y = slice_start; y < slice_end; y++) {
630
1538400
            for (x = 0; x < in->width; x++) {
631
1536000
                dstr[x] = curves->graph[R][srcr[x]];
632
1536000
                dstg[x] = curves->graph[G][srcg[x]];
633
1536000
                dstb[x] = curves->graph[B][srcb[x]];
634

1536000
                if (!direct && step == 4)
635
                    dsta[x] = srca[x];
636
            }
637
2400
            dstr += out->linesize[r];
638
2400
            dstg += out->linesize[g];
639
2400
            dstb += out->linesize[b];
640
2400
            dsta += out->linesize[a];
641
2400
            srcr += in ->linesize[r];
642
2400
            srcg += in ->linesize[g];
643
2400
            srcb += in ->linesize[b];
644
2400
            srca += in ->linesize[a];
645
        }
646
    }
647
45
    return 0;
648
}
649
650
1
static int config_input(AVFilterLink *inlink)
651
{
652
    int i, j, ret;
653
1
    AVFilterContext *ctx = inlink->dst;
654
1
    CurvesContext *curves = ctx->priv;
655
1
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
656
1
    char **pts = curves->comp_points_str;
657
1
    struct keypoint *comp_points[NB_COMP + 1] = {0};
658
659
1
    ff_fill_rgba_map(curves->rgba_map, inlink->format);
660
1
    curves->is_16bit = desc->comp[0].depth > 8;
661
1
    curves->depth = desc->comp[0].depth;
662
1
    curves->lut_size = 1 << curves->depth;
663
1
    curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
664
1
    curves->filter_slice = desc->flags & AV_PIX_FMT_FLAG_PLANAR ? filter_slice_planar : filter_slice_packed;
665
666
5
    for (i = 0; i < NB_COMP + 1; i++) {
667
4
        curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
668
4
        if (!curves->graph[i])
669
            return AVERROR(ENOMEM);
670
4
        ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
671
4
        if (ret < 0)
672
            return ret;
673

4
        switch (curves->depth) {
674
4
        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;
680
        }
681
4
        if (ret < 0)
682
            return ret;
683
    }
684
685
1
    if (pts[NB_COMP]) {
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]];
689
    }
690
691
1
    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);
695
            while (point) {
696
                av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
697
                point = point->next;
698
            }
699
        }
700
    }
701
702
1
    if (curves->plot_filename)
703
        dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
704
705
5
    for (i = 0; i < NB_COMP + 1; i++) {
706
4
        struct keypoint *point = comp_points[i];
707
13
        while (point) {
708
9
            struct keypoint *next = point->next;
709
9
            av_free(point);
710
9
            point = next;
711
        }
712
    }
713
714
1
    return 0;
715
}
716
717
5
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
718
{
719
5
    AVFilterContext *ctx = inlink->dst;
720
5
    CurvesContext *curves = ctx->priv;
721
5
    AVFilterLink *outlink = ctx->outputs[0];
722
    AVFrame *out;
723
    ThreadData td;
724
725
5
    if (av_frame_is_writable(in)) {
726
5
        out = in;
727
    } else {
728
        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
729
        if (!out) {
730
            av_frame_free(&in);
731
            return AVERROR(ENOMEM);
732
        }
733
        av_frame_copy_props(out, in);
734
    }
735
736
5
    td.in  = in;
737
5
    td.out = out;
738
5
    ctx->internal->execute(ctx, curves->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
739
740
5
    if (out != in)
741
        av_frame_free(&in);
742
743
5
    return ff_filter_frame(outlink, out);
744
}
745
746
1
static av_cold void curves_uninit(AVFilterContext *ctx)
747
{
748
    int i;
749
1
    CurvesContext *curves = ctx->priv;
750
751
5
    for (i = 0; i < NB_COMP + 1; i++)
752
4
        av_freep(&curves->graph[i]);
753
1
}
754
755
static const AVFilterPad curves_inputs[] = {
756
    {
757
        .name         = "default",
758
        .type         = AVMEDIA_TYPE_VIDEO,
759
        .filter_frame = filter_frame,
760
        .config_props = config_input,
761
    },
762
    { NULL }
763
};
764
765
static const AVFilterPad curves_outputs[] = {
766
    {
767
        .name = "default",
768
        .type = AVMEDIA_TYPE_VIDEO,
769
    },
770
    { NULL }
771
};
772
773
AVFilter ff_vf_curves = {
774
    .name          = "curves",
775
    .description   = NULL_IF_CONFIG_SMALL("Adjust components curves."),
776
    .priv_size     = sizeof(CurvesContext),
777
    .init          = curves_init,
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,
784
};