Browse code

avfilter: add selectivecolor filter

Clément Bœsch authored on 2015/06/08 03:21:28
Showing 6 changed files
... ...
@@ -17,6 +17,7 @@ version <next>:
17 17
 - chromaprint fingerprinting muxer
18 18
 - ffplay dynamic volume control
19 19
 - displace filter
20
+- selectivecolor filter
20 21
 
21 22
 
22 23
 version 2.8:
... ...
@@ -13037,6 +13037,73 @@ select=n=2:e='mod(n, 2)+1' [odd][even]; [odd] pad=h=2*ih [tmp]; [tmp][even] over
13037 13037
 @end example
13038 13038
 @end itemize
13039 13039
 
13040
+@section selectivecolor
13041
+
13042
+Adjust cyan, magenta, yellow and black (CMYK) to certain ranges of colors (such
13043
+as "reds", "yellows", "greens", "cyans", ...). The adjustment range is defined
13044
+by the "purity" of the color (that is, how saturated it already is).
13045
+
13046
+This filter is similar to the Adobe Photoshop Selective Color tool.
13047
+
13048
+The filter accepts the following options:
13049
+
13050
+@table @option
13051
+@item correction_method
13052
+Select color correction method.
13053
+
13054
+Available values are:
13055
+@table @samp
13056
+@item absolute
13057
+Specified adjustments are applied "as-is" (added/subtracted to original pixel
13058
+component value).
13059
+@item relative
13060
+Specified adjustments are relative to the original component value.
13061
+@end table
13062
+Default is @code{absolute}.
13063
+@item reds
13064
+Adjustments for red pixels (pixels where the red component is the maximum)
13065
+@item yellows
13066
+Adjustments for yellow pixels (pixels where the blue component is the minimum)
13067
+@item greens
13068
+Adjustments for green pixels (pixels where the green component is the maximum)
13069
+@item cyans
13070
+Adjustments for cyan pixels (pixels where the red component is the minimum)
13071
+@item blues
13072
+Adjustments for blue pixels (pixels where the blue component is the maximum)
13073
+@item magentas
13074
+Adjustments for magenta pixels (pixels where the green component is the minimum)
13075
+@item whites
13076
+Adjustments for white pixels (pixels where all components are greater than 128)
13077
+@item neutrals
13078
+Adjustments for all pixels except pure black and pure white
13079
+@item blacks
13080
+Adjustments for black pixels (pixels where all components are lesser than 128)
13081
+@item psfile
13082
+Specify a Photoshop selective color file (@code{.asv}) to import the settings from.
13083
+@end table
13084
+
13085
+All the adjustment settings (@option{reds}, @option{yellows}, ...) accept up to
13086
+4 space separated floating point adjustment values in the [-1,1] range,
13087
+respectively to adjust the amount of cyan, magenta, yellow and black for the
13088
+pixels of its range.
13089
+
13090
+@subsection Examples
13091
+
13092
+@itemize
13093
+@item
13094
+Increase cyan by 55% and reduce yellow by 33% in every green areas, and
13095
+increase magenta by 27% in blue areas:
13096
+@example
13097
+selectivecolor=greens=.5 0 -.33 0:blues=0 .27
13098
+@end example
13099
+
13100
+@item
13101
+Use a Photoshop selective color preset:
13102
+@example
13103
+selectivecolor=psfile=MySelectiveColorPresets/Misty.asv
13104
+@end example
13105
+@end itemize
13106
+
13040 13107
 @section sendcmd, asendcmd
13041 13108
 
13042 13109
 Send commands to filters in the filtergraph.
... ...
@@ -206,6 +206,7 @@ OBJS-$(CONFIG_SAB_FILTER)                    += vf_sab.o
206 206
 OBJS-$(CONFIG_SCALE_FILTER)                  += vf_scale.o
207 207
 OBJS-$(CONFIG_SCALE2REF_FILTER)              += vf_scale.o
208 208
 OBJS-$(CONFIG_SELECT_FILTER)                 += f_select.o
209
+OBJS-$(CONFIG_SELECTIVECOLOR_FILTER)         += vf_selectivecolor.o
209 210
 OBJS-$(CONFIG_SENDCMD_FILTER)                += f_sendcmd.o
210 211
 OBJS-$(CONFIG_SETDAR_FILTER)                 += vf_aspect.o
211 212
 OBJS-$(CONFIG_SETFIELD_FILTER)               += vf_setfield.o
... ...
@@ -226,6 +226,7 @@ void avfilter_register_all(void)
226 226
     REGISTER_FILTER(SCALE,          scale,          vf);
227 227
     REGISTER_FILTER(SCALE2REF,      scale2ref,      vf);
228 228
     REGISTER_FILTER(SELECT,         select,         vf);
229
+    REGISTER_FILTER(SELECTIVECOLOR, selectivecolor, vf);
229 230
     REGISTER_FILTER(SENDCMD,        sendcmd,        vf);
230 231
     REGISTER_FILTER(SEPARATEFIELDS, separatefields, vf);
231 232
     REGISTER_FILTER(SETDAR,         setdar,         vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  10
33
+#define LIBAVFILTER_VERSION_MINOR  11
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,458 @@
0
+/*
1
+ * Copyright (c) 2015 Clément Bœsch <u pkh me>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @todo
22
+ * - use integers so it can be made bitexact and a FATE test can be added
23
+ * - >8 bit support
24
+ */
25
+
26
+#include "libavutil/avassert.h"
27
+#include "libavutil/file.h"
28
+#include "libavutil/intreadwrite.h"
29
+#include "libavutil/opt.h"
30
+#include "avfilter.h"
31
+#include "formats.h"
32
+#include "internal.h"
33
+#include "video.h"
34
+
35
+enum color_range {
36
+    // WARNING: do NOT reorder (see parse_psfile())
37
+    RANGE_REDS,
38
+    RANGE_YELLOWS,
39
+    RANGE_GREENS,
40
+    RANGE_CYANS,
41
+    RANGE_BLUES,
42
+    RANGE_MAGENTAS,
43
+    RANGE_WHITES,
44
+    RANGE_NEUTRALS,
45
+    RANGE_BLACKS,
46
+    NB_RANGES
47
+};
48
+
49
+enum correction_method {
50
+    CORRECTION_METHOD_ABSOLUTE,
51
+    CORRECTION_METHOD_RELATIVE,
52
+    NB_CORRECTION_METHODS,
53
+};
54
+
55
+static const char *color_names[NB_RANGES] = {
56
+    "red", "yellow", "green", "cyan", "blue", "magenta", "white", "neutral", "black"
57
+};
58
+
59
+typedef int (*get_adjust_range_func)(int r, int g, int b, int min_val, int max_val);
60
+
61
+struct process_range {
62
+    int range_id;
63
+    uint32_t mask;
64
+    get_adjust_range_func get_adjust_range;
65
+};
66
+
67
+typedef struct ThreadData {
68
+    AVFrame *in, *out;
69
+} ThreadData;
70
+
71
+typedef struct {
72
+    const AVClass *class;
73
+    int correction_method;
74
+    char *opt_cmyk_adjust[NB_RANGES];
75
+    float cmyk_adjust[NB_RANGES][4];
76
+    struct process_range process_ranges[NB_RANGES]; // color ranges to process
77
+    int nb_process_ranges;
78
+    char *psfile;
79
+} SelectiveColorContext;
80
+
81
+#define OFFSET(x) offsetof(SelectiveColorContext, x)
82
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
83
+#define RANGE_OPTION(color_name, range) \
84
+    { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
85
+
86
+static const AVOption selectivecolor_options[] = {
87
+    { "correction_method", "select correction method", OFFSET(correction_method), AV_OPT_TYPE_INT, {.i64 = CORRECTION_METHOD_ABSOLUTE}, 0, NB_CORRECTION_METHODS, FLAGS, "correction_method" },
88
+        { "absolute", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_ABSOLUTE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
89
+        { "relative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_RELATIVE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
90
+    RANGE_OPTION("red",     RANGE_REDS),
91
+    RANGE_OPTION("yellow",  RANGE_YELLOWS),
92
+    RANGE_OPTION("green",   RANGE_GREENS),
93
+    RANGE_OPTION("cyan",    RANGE_CYANS),
94
+    RANGE_OPTION("blue",    RANGE_BLUES),
95
+    RANGE_OPTION("magenta", RANGE_MAGENTAS),
96
+    RANGE_OPTION("white",   RANGE_WHITES),
97
+    RANGE_OPTION("neutral", RANGE_NEUTRALS),
98
+    RANGE_OPTION("black",   RANGE_BLACKS),
99
+    { "psfile", "set Photoshop selectivecolor file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
100
+    { NULL }
101
+};
102
+
103
+AVFILTER_DEFINE_CLASS(selectivecolor);
104
+
105
+static inline int get_mid_val(int r, int g, int b)
106
+{
107
+    if ((r < g && r > b) || (r < b && r > g)) return r;
108
+    if ((g < r && g > b) || (g < b && g > r)) return g;
109
+    if ((b < r && b > g) || (b < g && b > r)) return b;
110
+    return -1;
111
+}
112
+
113
+static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
114
+{
115
+    // max - mid
116
+    const int mid_val = get_mid_val(r, g, b);
117
+    if (mid_val == -1) {
118
+        // XXX: can be simplified
119
+        if ((r != min_val && g == min_val && b == min_val) ||
120
+            (r == min_val && g != min_val && b == min_val) ||
121
+            (r == min_val && g == min_val && b != min_val))
122
+            return max_val - min_val;
123
+        return 0;
124
+    }
125
+    return max_val - mid_val;
126
+}
127
+
128
+static int get_cmy_adjust_range(int r, int g, int b, int min_val, int max_val)
129
+{
130
+    // mid - min
131
+    const int mid_val = get_mid_val(r, g, b);
132
+    if (mid_val == -1) {
133
+        // XXX: refactor with rgb
134
+        if ((r != max_val && g == max_val && b == max_val) ||
135
+            (r == max_val && g != max_val && b == max_val) ||
136
+            (r == max_val && g == max_val && b != max_val))
137
+            return max_val - min_val;
138
+        return 0;
139
+    }
140
+    return mid_val - min_val;
141
+}
142
+
143
+static int get_neutrals_adjust_range(int r, int g, int b, int min_val, int max_val)
144
+{
145
+    // 1 - (|max-0.5| + |min-0.5|)
146
+    return (255*2 - (abs((max_val<<1) - 255) + abs((min_val<<1) - 255)) + 1) >> 1;
147
+}
148
+
149
+static int get_whites_adjust_range(int r, int g, int b, int min_val, int max_val)
150
+{
151
+    // (min - 0.5) * 2
152
+    return (min_val<<1) - 255;
153
+}
154
+
155
+static int get_blacks_adjust_range(int r, int g, int b, int min_val, int max_val)
156
+{
157
+    // (0.5 - max) * 2
158
+    return 255 - (max_val<<1);
159
+}
160
+
161
+static int register_range(SelectiveColorContext *s, int range_id)
162
+{
163
+    const float *cmyk = s->cmyk_adjust[range_id];
164
+
165
+    /* If the color range has user settings, register the color range
166
+     * as "to be processed" */
167
+    if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
168
+        struct process_range *pr = &s->process_ranges[s->nb_process_ranges++];
169
+
170
+        if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
171
+            cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
172
+            cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
173
+            cmyk[3] < -1.0 || cmyk[3] > 1.0) {
174
+            av_log(s, AV_LOG_ERROR, "Invalid %s adjustments (%g %g %g %g). "
175
+                   "Settings must be set in [-1;1] range\n",
176
+                   color_names[range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
177
+            return AVERROR(EINVAL);
178
+        }
179
+
180
+        pr->range_id = range_id;
181
+        pr->mask = 1 << range_id;
182
+        if      (pr->mask & (1<<RANGE_REDS  | 1<<RANGE_GREENS   | 1<<RANGE_BLUES))   pr->get_adjust_range = get_rgb_adjust_range;
183
+        else if (pr->mask & (1<<RANGE_CYANS | 1<<RANGE_MAGENTAS | 1<<RANGE_YELLOWS)) pr->get_adjust_range = get_cmy_adjust_range;
184
+        else if (pr->mask & 1<<RANGE_WHITES)                                         pr->get_adjust_range = get_whites_adjust_range;
185
+        else if (pr->mask & 1<<RANGE_NEUTRALS)                                       pr->get_adjust_range = get_neutrals_adjust_range;
186
+        else if (pr->mask & 1<<RANGE_BLACKS)                                         pr->get_adjust_range = get_blacks_adjust_range;
187
+        else
188
+            av_assert0(0);
189
+    }
190
+    return 0;
191
+}
192
+
193
+static int parse_psfile(AVFilterContext *ctx, const char *fname)
194
+{
195
+    int16_t val;
196
+    int ret, i, version;
197
+    uint8_t *buf;
198
+    size_t size;
199
+    SelectiveColorContext *s = ctx->priv;
200
+
201
+    ret = av_file_map(fname, &buf, &size, 0, NULL);
202
+    if (ret < 0)
203
+        return ret;
204
+
205
+#define READ16(dst) do {                \
206
+    if (size < 2) {                     \
207
+        ret = AVERROR_INVALIDDATA;      \
208
+        goto end;                       \
209
+    }                                   \
210
+    dst = AV_RB16(buf);                 \
211
+    buf  += 2;                          \
212
+    size -= 2;                          \
213
+} while (0)
214
+
215
+    READ16(version);
216
+    if (version != 1)
217
+        av_log(s, AV_LOG_WARNING, "Unsupported selective color file version %d, "
218
+               "the settings might not be loaded properly\n", version);
219
+
220
+    READ16(s->correction_method);
221
+
222
+    // 1st CMYK entry is reserved/unused
223
+    for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); i++) {
224
+        READ16(val);
225
+        if (val)
226
+            av_log(s, AV_LOG_WARNING, "%c value of first CMYK entry is not 0 "
227
+                   "but %d\n", "CMYK"[i], val);
228
+    }
229
+
230
+    for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust); i++) {
231
+        int k;
232
+        for (k = 0; k < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); k++) {
233
+            READ16(val);
234
+            s->cmyk_adjust[i][k] = val / 100.;
235
+        }
236
+        ret = register_range(s, i);
237
+        if (ret < 0)
238
+            goto end;
239
+    }
240
+
241
+end:
242
+    av_file_unmap(buf, size);
243
+    return ret;
244
+}
245
+
246
+static av_cold int init(AVFilterContext *ctx)
247
+{
248
+    int i, ret;
249
+    SelectiveColorContext *s = ctx->priv;
250
+
251
+    /* If the following conditions are not met, it will cause trouble while
252
+     * parsing the PS file */
253
+    av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust) == 10 - 1);
254
+    av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust[0]) == 4);
255
+
256
+    if (s->psfile) {
257
+        ret = parse_psfile(ctx, s->psfile);
258
+        if (ret < 0)
259
+            return ret;
260
+    } else {
261
+        for (i = 0; i < FF_ARRAY_ELEMS(s->opt_cmyk_adjust); i++) {
262
+            const char *opt_cmyk_adjust = s->opt_cmyk_adjust[i];
263
+
264
+            if (opt_cmyk_adjust) {
265
+                float *cmyk = s->cmyk_adjust[i];
266
+
267
+                sscanf(s->opt_cmyk_adjust[i], "%f %f %f %f", cmyk, cmyk+1, cmyk+2, cmyk+3);
268
+                ret = register_range(s, i);
269
+                if (ret < 0)
270
+                    return ret;
271
+            }
272
+        }
273
+    }
274
+
275
+    av_log(s, AV_LOG_VERBOSE, "Adjustments:%s\n", s->nb_process_ranges ? "" : " none");
276
+    for (i = 0; i < s->nb_process_ranges; i++) {
277
+        const struct process_range *pr = &s->process_ranges[i];
278
+        const float *cmyk = s->cmyk_adjust[pr->range_id];
279
+
280
+        av_log(s, AV_LOG_VERBOSE, "%8ss: C=%6g M=%6g Y=%6g K=%6g\n",
281
+               color_names[pr->range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
282
+    }
283
+
284
+    return 0;
285
+}
286
+
287
+static int query_formats(AVFilterContext *ctx)
288
+{
289
+    static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB32, AV_PIX_FMT_0RGB32, AV_PIX_FMT_NONE};
290
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
291
+    if (!fmts_list)
292
+        return AVERROR(ENOMEM);
293
+    return ff_set_common_formats(ctx, fmts_list);
294
+}
295
+
296
+static inline int comp_adjust(int adjust_range, float value, float adjust, float k, int correction_method)
297
+{
298
+    const float min = -value;
299
+    const float max = 1. - value;
300
+    float res = (-1. - adjust) * k - adjust;
301
+    if (correction_method == CORRECTION_METHOD_RELATIVE)
302
+        res *= max;
303
+    return lrint(av_clipf(res, min, max) * adjust_range);
304
+}
305
+
306
+static inline int selective_color(AVFilterContext *ctx, ThreadData *td,
307
+                                  int jobnr, int nb_jobs, int direct, int correction_method)
308
+{
309
+    int i, x, y;
310
+    const AVFrame *in = td->in;
311
+    AVFrame *out = td->out;
312
+    const SelectiveColorContext *s = ctx->priv;
313
+    const int height = in->height;
314
+    const int width  = in->width;
315
+    const int slice_start = (height *  jobnr   ) / nb_jobs;
316
+    const int slice_end   = (height * (jobnr+1)) / nb_jobs;
317
+    const int dst_linesize = out->linesize[0];
318
+    const int src_linesize =  in->linesize[0];
319
+    uint8_t       *dst = out->data[0] + slice_start * dst_linesize;
320
+    const uint8_t *src =  in->data[0] + slice_start * src_linesize;
321
+
322
+    for (y = slice_start; y < slice_end; y++) {
323
+        const uint32_t *src32 = (const uint32_t *)src;
324
+        uint32_t       *dst32 = (uint32_t *)dst;
325
+
326
+        for (x = 0; x < width; x++) {
327
+            const uint32_t color = *src32++;
328
+            const int r = color >> 16 & 0xff;
329
+            const int g = color >>  8 & 0xff;
330
+            const int b = color       & 0xff;
331
+            const int min_color = FFMIN3(r, g, b);
332
+            const int max_color = FFMAX3(r, g, b);
333
+            const uint32_t range_flag = (r == max_color) << RANGE_REDS
334
+                                      | (r == min_color) << RANGE_CYANS
335
+                                      | (g == max_color) << RANGE_GREENS
336
+                                      | (g == min_color) << RANGE_MAGENTAS
337
+                                      | (b == max_color) << RANGE_BLUES
338
+                                      | (b == min_color) << RANGE_YELLOWS
339
+                                      | (r > 128 && g > 128 && b > 128) << RANGE_WHITES
340
+                                      | (color && (color & 0xffffff) != 0xffffff) << RANGE_NEUTRALS
341
+                                      | (r < 128 && g < 128 && b < 128) << RANGE_BLACKS;
342
+
343
+            const float rnorm = r / 255.;
344
+            const float gnorm = g / 255.;
345
+            const float bnorm = b / 255.;
346
+            int adjust_r = 0, adjust_g = 0, adjust_b = 0;
347
+
348
+            for (i = 0; i < s->nb_process_ranges; i++) {
349
+                const struct process_range *pr = &s->process_ranges[i];
350
+
351
+                if (range_flag & pr->mask) {
352
+                    const int adjust_range = pr->get_adjust_range(r, g, b, min_color, max_color);
353
+
354
+                    if (adjust_range > 0) {
355
+                        const float *cmyk_adjust = s->cmyk_adjust[pr->range_id];
356
+                        const float adj_c = cmyk_adjust[0];
357
+                        const float adj_m = cmyk_adjust[1];
358
+                        const float adj_y = cmyk_adjust[2];
359
+                        const float k = cmyk_adjust[3];
360
+
361
+                        adjust_r += comp_adjust(adjust_range, rnorm, adj_c, k, correction_method);
362
+                        adjust_g += comp_adjust(adjust_range, gnorm, adj_m, k, correction_method);
363
+                        adjust_b += comp_adjust(adjust_range, bnorm, adj_y, k, correction_method);
364
+                    }
365
+                }
366
+            }
367
+
368
+            if (!direct || adjust_r || adjust_g || adjust_b)
369
+                *dst32 = (color & 0xff000000)
370
+                       | av_clip_uint8(r + adjust_r) << 16
371
+                       | av_clip_uint8(g + adjust_g) <<  8
372
+                       | av_clip_uint8(b + adjust_b);
373
+            dst32++;
374
+        }
375
+        src += src_linesize;
376
+        dst += dst_linesize;
377
+    }
378
+    return 0;
379
+}
380
+
381
+#define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method)                           \
382
+static int selective_color_##name(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)  \
383
+{                                                                                           \
384
+    return selective_color(ctx, arg, jobnr, nb_jobs, direct, correction_method);            \
385
+}
386
+
387
+DEF_SELECTIVE_COLOR_FUNC(indirect_absolute, 0, CORRECTION_METHOD_ABSOLUTE)
388
+DEF_SELECTIVE_COLOR_FUNC(indirect_relative, 0, CORRECTION_METHOD_RELATIVE)
389
+DEF_SELECTIVE_COLOR_FUNC(  direct_absolute, 1, CORRECTION_METHOD_ABSOLUTE)
390
+DEF_SELECTIVE_COLOR_FUNC(  direct_relative, 1, CORRECTION_METHOD_RELATIVE)
391
+
392
+typedef int (*selective_color_func_type)(AVFilterContext *ctx, void *td, int jobnr, int nb_jobs);
393
+
394
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
395
+{
396
+    AVFilterContext *ctx = inlink->dst;
397
+    AVFilterLink *outlink = ctx->outputs[0];
398
+    int direct;
399
+    AVFrame *out;
400
+    ThreadData td;
401
+    const SelectiveColorContext *s = ctx->priv;
402
+    static const selective_color_func_type funcs[2][2] = {
403
+        {selective_color_indirect_absolute, selective_color_indirect_relative},
404
+        {selective_color_direct_absolute,   selective_color_direct_relative},
405
+    };
406
+
407
+    if (av_frame_is_writable(in)) {
408
+        direct = 1;
409
+        out = in;
410
+    } else {
411
+        direct = 0;
412
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
413
+        if (!out) {
414
+            av_frame_free(&in);
415
+            return AVERROR(ENOMEM);
416
+        }
417
+        av_frame_copy_props(out, in);
418
+    }
419
+
420
+    td.in = in;
421
+    td.out = out;
422
+    ctx->internal->execute(ctx, funcs[direct][s->correction_method], &td, NULL,
423
+                           FFMIN(inlink->h, ctx->graph->nb_threads));
424
+
425
+    if (!direct)
426
+        av_frame_free(&in);
427
+    return ff_filter_frame(outlink, out);
428
+}
429
+
430
+static const AVFilterPad selectivecolor_inputs[] = {
431
+    {
432
+        .name         = "default",
433
+        .type         = AVMEDIA_TYPE_VIDEO,
434
+        .filter_frame = filter_frame,
435
+    },
436
+    { NULL }
437
+};
438
+
439
+static const AVFilterPad selectivecolor_outputs[] = {
440
+    {
441
+        .name = "default",
442
+        .type = AVMEDIA_TYPE_VIDEO,
443
+    },
444
+    { NULL }
445
+};
446
+
447
+AVFilter ff_vf_selectivecolor = {
448
+    .name          = "selectivecolor",
449
+    .description   = NULL_IF_CONFIG_SMALL("Apply CMYK adjustments to specific color ranges."),
450
+    .priv_size     = sizeof(SelectiveColorContext),
451
+    .init          = init,
452
+    .query_formats = query_formats,
453
+    .inputs        = selectivecolor_inputs,
454
+    .outputs       = selectivecolor_outputs,
455
+    .priv_class    = &selectivecolor_class,
456
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
457
+};