]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_selectivecolor.c
Merge commit '0b1bd1b2057d41fd0ccba7317911c484a50f9207'
[ffmpeg] / libavfilter / vf_selectivecolor.c
1 /*
2  * Copyright (c) 2015-2016 Clément Bœsch <u pkh me>
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 /**
22  * @todo
23  * - use integers so it can be made bitexact and a FATE test can be added
24  */
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/file.h"
28 #include "libavutil/intreadwrite.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "drawutils.h"
33 #include "formats.h"
34 #include "internal.h"
35 #include "video.h"
36
37 #define R 0
38 #define G 1
39 #define B 2
40 #define A 3
41
42 enum color_range {
43     // WARNING: do NOT reorder (see parse_psfile())
44     RANGE_REDS,
45     RANGE_YELLOWS,
46     RANGE_GREENS,
47     RANGE_CYANS,
48     RANGE_BLUES,
49     RANGE_MAGENTAS,
50     RANGE_WHITES,
51     RANGE_NEUTRALS,
52     RANGE_BLACKS,
53     NB_RANGES
54 };
55
56 enum correction_method {
57     CORRECTION_METHOD_ABSOLUTE,
58     CORRECTION_METHOD_RELATIVE,
59     NB_CORRECTION_METHODS,
60 };
61
62 static const char *color_names[NB_RANGES] = {
63     "red", "yellow", "green", "cyan", "blue", "magenta", "white", "neutral", "black"
64 };
65
66 typedef int (*get_adjust_range_func)(int r, int g, int b, int min_val, int max_val);
67
68 struct process_range {
69     int range_id;
70     uint32_t mask;
71     get_adjust_range_func get_adjust_range;
72 };
73
74 typedef struct ThreadData {
75     AVFrame *in, *out;
76 } ThreadData;
77
78 typedef struct {
79     const AVClass *class;
80     int correction_method;
81     char *opt_cmyk_adjust[NB_RANGES];
82     float cmyk_adjust[NB_RANGES][4];
83     struct process_range process_ranges[NB_RANGES]; // color ranges to process
84     int nb_process_ranges;
85     char *psfile;
86     uint8_t rgba_map[4];
87     int is_16bit;
88     int step;
89 } SelectiveColorContext;
90
91 #define OFFSET(x) offsetof(SelectiveColorContext, x)
92 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
93 #define RANGE_OPTION(color_name, range) \
94     { color_name"s", "adjust "color_name" regions", OFFSET(opt_cmyk_adjust[range]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS }
95
96 static const AVOption selectivecolor_options[] = {
97     { "correction_method", "select correction method", OFFSET(correction_method), AV_OPT_TYPE_INT, {.i64 = CORRECTION_METHOD_ABSOLUTE}, 0, NB_CORRECTION_METHODS-1, FLAGS, "correction_method" },
98         { "absolute", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_ABSOLUTE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
99         { "relative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CORRECTION_METHOD_RELATIVE}, INT_MIN, INT_MAX, FLAGS, "correction_method" },
100     RANGE_OPTION("red",     RANGE_REDS),
101     RANGE_OPTION("yellow",  RANGE_YELLOWS),
102     RANGE_OPTION("green",   RANGE_GREENS),
103     RANGE_OPTION("cyan",    RANGE_CYANS),
104     RANGE_OPTION("blue",    RANGE_BLUES),
105     RANGE_OPTION("magenta", RANGE_MAGENTAS),
106     RANGE_OPTION("white",   RANGE_WHITES),
107     RANGE_OPTION("neutral", RANGE_NEUTRALS),
108     RANGE_OPTION("black",   RANGE_BLACKS),
109     { "psfile", "set Photoshop selectivecolor file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
110     { NULL }
111 };
112
113 AVFILTER_DEFINE_CLASS(selectivecolor);
114
115 static inline int get_mid_val(int r, int g, int b)
116 {
117     if ((r < g && r > b) || (r < b && r > g)) return r;
118     if ((g < r && g > b) || (g < b && g > r)) return g;
119     if ((b < r && b > g) || (b < g && b > r)) return b;
120     return -1;
121 }
122
123 static int get_rgb_adjust_range(int r, int g, int b, int min_val, int max_val)
124 {
125     // max - mid
126     const int mid_val = get_mid_val(r, g, b);
127     if (mid_val == -1) {
128         // XXX: can be simplified
129         if ((r != min_val && g == min_val && b == min_val) ||
130             (r == min_val && g != min_val && b == min_val) ||
131             (r == min_val && g == min_val && b != min_val))
132             return max_val - min_val;
133         return 0;
134     }
135     return max_val - mid_val;
136 }
137
138 static int get_cmy_adjust_range(int r, int g, int b, int min_val, int max_val)
139 {
140     // mid - min
141     const int mid_val = get_mid_val(r, g, b);
142     if (mid_val == -1) {
143         // XXX: refactor with rgb
144         if ((r != max_val && g == max_val && b == max_val) ||
145             (r == max_val && g != max_val && b == max_val) ||
146             (r == max_val && g == max_val && b != max_val))
147             return max_val - min_val;
148         return 0;
149     }
150     return mid_val - min_val;
151 }
152
153 #define DECLARE_ADJUST_RANGE_FUNCS(nbits)                                                   \
154 static int get_neutrals_adjust_range##nbits(int r, int g, int b, int min_val, int max_val)  \
155 {                                                                                           \
156     /* 1 - (|max-0.5| + |min-0.5|) */                                                       \
157     return (((1<<nbits)-1)*2 - (  abs((max_val<<1) - ((1<<nbits)-1))                        \
158                                 + abs((min_val<<1) - ((1<<nbits)-1))) + 1) >> 1;            \
159 }                                                                                           \
160                                                                                             \
161 static int get_whites_adjust_range##nbits(int r, int g, int b, int min_val, int max_val)    \
162 {                                                                                           \
163     /* (min - 0.5) * 2 */                                                                   \
164     return (min_val<<1) - ((1<<nbits)-1);                                                   \
165 }                                                                                           \
166                                                                                             \
167 static int get_blacks_adjust_range##nbits(int r, int g, int b, int min_val, int max_val)    \
168 {                                                                                           \
169     /* (0.5 - max) * 2 */                                                                   \
170     return ((1<<nbits)-1) - (max_val<<1);                                                   \
171 }                                                                                           \
172
173 DECLARE_ADJUST_RANGE_FUNCS(8)
174 DECLARE_ADJUST_RANGE_FUNCS(16)
175
176 static int register_range(SelectiveColorContext *s, int range_id)
177 {
178     const float *cmyk = s->cmyk_adjust[range_id];
179
180     /* If the color range has user settings, register the color range
181      * as "to be processed" */
182     if (cmyk[0] || cmyk[1] || cmyk[2] || cmyk[3]) {
183         struct process_range *pr = &s->process_ranges[s->nb_process_ranges++];
184
185         if (cmyk[0] < -1.0 || cmyk[0] > 1.0 ||
186             cmyk[1] < -1.0 || cmyk[1] > 1.0 ||
187             cmyk[2] < -1.0 || cmyk[2] > 1.0 ||
188             cmyk[3] < -1.0 || cmyk[3] > 1.0) {
189             av_log(s, AV_LOG_ERROR, "Invalid %s adjustments (%g %g %g %g). "
190                    "Settings must be set in [-1;1] range\n",
191                    color_names[range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
192             return AVERROR(EINVAL);
193         }
194
195         pr->range_id = range_id;
196         pr->mask = 1 << range_id;
197         if      (pr->mask & (1<<RANGE_REDS  | 1<<RANGE_GREENS   | 1<<RANGE_BLUES))   pr->get_adjust_range = get_rgb_adjust_range;
198         else if (pr->mask & (1<<RANGE_CYANS | 1<<RANGE_MAGENTAS | 1<<RANGE_YELLOWS)) pr->get_adjust_range = get_cmy_adjust_range;
199         else if (!s->is_16bit && (pr->mask & 1<<RANGE_WHITES))                       pr->get_adjust_range = get_whites_adjust_range8;
200         else if (!s->is_16bit && (pr->mask & 1<<RANGE_NEUTRALS))                     pr->get_adjust_range = get_neutrals_adjust_range8;
201         else if (!s->is_16bit && (pr->mask & 1<<RANGE_BLACKS))                       pr->get_adjust_range = get_blacks_adjust_range8;
202         else if ( s->is_16bit && (pr->mask & 1<<RANGE_WHITES))                       pr->get_adjust_range = get_whites_adjust_range16;
203         else if ( s->is_16bit && (pr->mask & 1<<RANGE_NEUTRALS))                     pr->get_adjust_range = get_neutrals_adjust_range16;
204         else if ( s->is_16bit && (pr->mask & 1<<RANGE_BLACKS))                       pr->get_adjust_range = get_blacks_adjust_range16;
205         else
206             av_assert0(0);
207     }
208     return 0;
209 }
210
211 static int parse_psfile(AVFilterContext *ctx, const char *fname)
212 {
213     int16_t val;
214     int ret, i, version;
215     uint8_t *buf;
216     size_t size;
217     SelectiveColorContext *s = ctx->priv;
218
219     ret = av_file_map(fname, &buf, &size, 0, NULL);
220     if (ret < 0)
221         return ret;
222
223 #define READ16(dst) do {                \
224     if (size < 2) {                     \
225         ret = AVERROR_INVALIDDATA;      \
226         goto end;                       \
227     }                                   \
228     dst = AV_RB16(buf);                 \
229     buf  += 2;                          \
230     size -= 2;                          \
231 } while (0)
232
233     READ16(version);
234     if (version != 1)
235         av_log(s, AV_LOG_WARNING, "Unsupported selective color file version %d, "
236                "the settings might not be loaded properly\n", version);
237
238     READ16(s->correction_method);
239
240     // 1st CMYK entry is reserved/unused
241     for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); i++) {
242         READ16(val);
243         if (val)
244             av_log(s, AV_LOG_WARNING, "%c value of first CMYK entry is not 0 "
245                    "but %d\n", "CMYK"[i], val);
246     }
247
248     for (i = 0; i < FF_ARRAY_ELEMS(s->cmyk_adjust); i++) {
249         int k;
250         for (k = 0; k < FF_ARRAY_ELEMS(s->cmyk_adjust[0]); k++) {
251             READ16(val);
252             s->cmyk_adjust[i][k] = val / 100.;
253         }
254         ret = register_range(s, i);
255         if (ret < 0)
256             goto end;
257     }
258
259 end:
260     av_file_unmap(buf, size);
261     return ret;
262 }
263
264 static int config_input(AVFilterLink *inlink)
265 {
266     int i, ret;
267     AVFilterContext *ctx = inlink->dst;
268     SelectiveColorContext *s = ctx->priv;
269     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
270
271     s->is_16bit = desc->comp[0].depth > 8;
272     s->step = av_get_padded_bits_per_pixel(desc) >> (3 + s->is_16bit);
273
274     ret = ff_fill_rgba_map(s->rgba_map, inlink->format);
275     if (ret < 0)
276         return ret;
277
278     /* If the following conditions are not met, it will cause trouble while
279      * parsing the PS file */
280     av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust) == 10 - 1);
281     av_assert0(FF_ARRAY_ELEMS(s->cmyk_adjust[0]) == 4);
282
283     if (s->psfile) {
284         ret = parse_psfile(ctx, s->psfile);
285         if (ret < 0)
286             return ret;
287     } else {
288         for (i = 0; i < FF_ARRAY_ELEMS(s->opt_cmyk_adjust); i++) {
289             const char *opt_cmyk_adjust = s->opt_cmyk_adjust[i];
290
291             if (opt_cmyk_adjust) {
292                 float *cmyk = s->cmyk_adjust[i];
293
294                 sscanf(s->opt_cmyk_adjust[i], "%f %f %f %f", cmyk, cmyk+1, cmyk+2, cmyk+3);
295                 ret = register_range(s, i);
296                 if (ret < 0)
297                     return ret;
298             }
299         }
300     }
301
302     av_log(s, AV_LOG_VERBOSE, "Adjustments:%s\n", s->nb_process_ranges ? "" : " none");
303     for (i = 0; i < s->nb_process_ranges; i++) {
304         const struct process_range *pr = &s->process_ranges[i];
305         const float *cmyk = s->cmyk_adjust[pr->range_id];
306
307         av_log(s, AV_LOG_VERBOSE, "%8ss: C=%6g M=%6g Y=%6g K=%6g\n",
308                color_names[pr->range_id], cmyk[0], cmyk[1], cmyk[2], cmyk[3]);
309     }
310
311     return 0;
312 }
313
314 static int query_formats(AVFilterContext *ctx)
315 {
316     static const enum AVPixelFormat pix_fmts[] = {
317         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
318         AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
319         AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
320         AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
321         AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
322         AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
323         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
324         AV_PIX_FMT_NONE
325     };
326     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
327     if (!fmts_list)
328         return AVERROR(ENOMEM);
329     return ff_set_common_formats(ctx, fmts_list);
330 }
331
332 static inline int comp_adjust(int adjust_range, float value, float adjust, float k, int correction_method)
333 {
334     const float min = -value;
335     const float max = 1. - value;
336     float res = (-1. - adjust) * k - adjust;
337     if (correction_method == CORRECTION_METHOD_RELATIVE)
338         res *= max;
339     return lrint(av_clipf(res, min, max) * adjust_range);
340 }
341
342 #define DECLARE_SELECTIVE_COLOR_FUNC(nbits)                                                             \
343 static inline int selective_color_##nbits(AVFilterContext *ctx, ThreadData *td,                         \
344                                           int jobnr, int nb_jobs, int direct, int correction_method)    \
345 {                                                                                                       \
346     int i, x, y;                                                                                        \
347     const AVFrame *in = td->in;                                                                         \
348     AVFrame *out = td->out;                                                                             \
349     const SelectiveColorContext *s = ctx->priv;                                                         \
350     const int height = in->height;                                                                      \
351     const int width  = in->width;                                                                       \
352     const int slice_start = (height *  jobnr   ) / nb_jobs;                                             \
353     const int slice_end   = (height * (jobnr+1)) / nb_jobs;                                             \
354     const int dst_linesize = out->linesize[0];                                                          \
355     const int src_linesize =  in->linesize[0];                                                          \
356     const uint8_t roffset = s->rgba_map[R];                                                             \
357     const uint8_t goffset = s->rgba_map[G];                                                             \
358     const uint8_t boffset = s->rgba_map[B];                                                             \
359     const uint8_t aoffset = s->rgba_map[A];                                                             \
360                                                                                                         \
361     for (y = slice_start; y < slice_end; y++) {                                                         \
362         uint##nbits##_t       *dst = (      uint##nbits##_t *)(out->data[0] + y * dst_linesize);        \
363         const uint##nbits##_t *src = (const uint##nbits##_t *)( in->data[0] + y * src_linesize);        \
364                                                                                                         \
365         for (x = 0; x < width * s->step; x += s->step) {                                                \
366             const int r = src[x + roffset];                                                             \
367             const int g = src[x + goffset];                                                             \
368             const int b = src[x + boffset];                                                             \
369             const int min_color = FFMIN3(r, g, b);                                                      \
370             const int max_color = FFMAX3(r, g, b);                                                      \
371             const int is_white   = (r > 1<<(nbits-1) && g > 1<<(nbits-1) && b > 1<<(nbits-1));          \
372             const int is_neutral = (r || g || b) &&                                                     \
373                                    r != (1<<nbits)-1 && g != (1<<nbits)-1 && b != (1<<nbits)-1;         \
374             const int is_black   = (r < 1<<(nbits-1) && g < 1<<(nbits-1) && b < 1<<(nbits-1));          \
375             const uint32_t range_flag = (r == max_color) << RANGE_REDS                                  \
376                                       | (r == min_color) << RANGE_CYANS                                 \
377                                       | (g == max_color) << RANGE_GREENS                                \
378                                       | (g == min_color) << RANGE_MAGENTAS                              \
379                                       | (b == max_color) << RANGE_BLUES                                 \
380                                       | (b == min_color) << RANGE_YELLOWS                               \
381                                       | is_white         << RANGE_WHITES                                \
382                                       | is_neutral       << RANGE_NEUTRALS                              \
383                                       | is_black         << RANGE_BLACKS;                               \
384                                                                                                         \
385             const float rnorm = r * (1.f / ((1<<nbits)-1));                                             \
386             const float gnorm = g * (1.f / ((1<<nbits)-1));                                             \
387             const float bnorm = b * (1.f / ((1<<nbits)-1));                                             \
388             int adjust_r = 0, adjust_g = 0, adjust_b = 0;                                               \
389                                                                                                         \
390             for (i = 0; i < s->nb_process_ranges; i++) {                                                \
391                 const struct process_range *pr = &s->process_ranges[i];                                 \
392                                                                                                         \
393                 if (range_flag & pr->mask) {                                                            \
394                     const int adjust_range = pr->get_adjust_range(r, g, b, min_color, max_color);       \
395                                                                                                         \
396                     if (adjust_range > 0) {                                                             \
397                         const float *cmyk_adjust = s->cmyk_adjust[pr->range_id];                        \
398                         const float adj_c = cmyk_adjust[0];                                             \
399                         const float adj_m = cmyk_adjust[1];                                             \
400                         const float adj_y = cmyk_adjust[2];                                             \
401                         const float k = cmyk_adjust[3];                                                 \
402                                                                                                         \
403                         adjust_r += comp_adjust(adjust_range, rnorm, adj_c, k, correction_method);      \
404                         adjust_g += comp_adjust(adjust_range, gnorm, adj_m, k, correction_method);      \
405                         adjust_b += comp_adjust(adjust_range, bnorm, adj_y, k, correction_method);      \
406                     }                                                                                   \
407                 }                                                                                       \
408             }                                                                                           \
409                                                                                                         \
410             if (!direct || adjust_r || adjust_g || adjust_b) {                                          \
411                 dst[x + roffset] = av_clip_uint##nbits(r + adjust_r);                                   \
412                 dst[x + goffset] = av_clip_uint##nbits(g + adjust_g);                                   \
413                 dst[x + boffset] = av_clip_uint##nbits(b + adjust_b);                                   \
414                 if (!direct && s->step == 4)                                                            \
415                     dst[x + aoffset] = src[x + aoffset];                                                \
416             }                                                                                           \
417         }                                                                                               \
418     }                                                                                                   \
419     return 0;                                                                                           \
420 }
421
422 #define DEF_SELECTIVE_COLOR_FUNC(name, direct, correction_method, nbits)                                \
423 static int selective_color_##name##_##nbits(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)    \
424 {                                                                                                       \
425     return selective_color_##nbits(ctx, arg, jobnr, nb_jobs, direct, correction_method);                \
426 }
427
428 #define DEF_SELECTIVE_COLOR_FUNCS(nbits)                                                                \
429 DECLARE_SELECTIVE_COLOR_FUNC(nbits)                                                                     \
430 DEF_SELECTIVE_COLOR_FUNC(indirect_absolute, 0, CORRECTION_METHOD_ABSOLUTE, nbits)                       \
431 DEF_SELECTIVE_COLOR_FUNC(indirect_relative, 0, CORRECTION_METHOD_RELATIVE, nbits)                       \
432 DEF_SELECTIVE_COLOR_FUNC(  direct_absolute, 1, CORRECTION_METHOD_ABSOLUTE, nbits)                       \
433 DEF_SELECTIVE_COLOR_FUNC(  direct_relative, 1, CORRECTION_METHOD_RELATIVE, nbits)
434
435 DEF_SELECTIVE_COLOR_FUNCS(8)
436 DEF_SELECTIVE_COLOR_FUNCS(16)
437
438 typedef int (*selective_color_func_type)(AVFilterContext *ctx, void *td, int jobnr, int nb_jobs);
439
440 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
441 {
442     AVFilterContext *ctx = inlink->dst;
443     AVFilterLink *outlink = ctx->outputs[0];
444     int direct;
445     AVFrame *out;
446     ThreadData td;
447     const SelectiveColorContext *s = ctx->priv;
448     static const selective_color_func_type funcs[2][2][2] = {
449         {
450             {selective_color_indirect_absolute_8, selective_color_indirect_relative_8},
451             {selective_color_direct_absolute_8,   selective_color_direct_relative_8},
452         },{
453             {selective_color_indirect_absolute_16, selective_color_indirect_relative_16},
454             {selective_color_direct_absolute_16,   selective_color_direct_relative_16},
455         }
456     };
457
458     if (av_frame_is_writable(in)) {
459         direct = 1;
460         out = in;
461     } else {
462         direct = 0;
463         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
464         if (!out) {
465             av_frame_free(&in);
466             return AVERROR(ENOMEM);
467         }
468         av_frame_copy_props(out, in);
469     }
470
471     td.in = in;
472     td.out = out;
473     ctx->internal->execute(ctx, funcs[s->is_16bit][direct][s->correction_method],
474                            &td, NULL, FFMIN(inlink->h, ff_filter_get_nb_threads(ctx)));
475
476     if (!direct)
477         av_frame_free(&in);
478     return ff_filter_frame(outlink, out);
479 }
480
481 static const AVFilterPad selectivecolor_inputs[] = {
482     {
483         .name         = "default",
484         .type         = AVMEDIA_TYPE_VIDEO,
485         .filter_frame = filter_frame,
486         .config_props = config_input,
487     },
488     { NULL }
489 };
490
491 static const AVFilterPad selectivecolor_outputs[] = {
492     {
493         .name = "default",
494         .type = AVMEDIA_TYPE_VIDEO,
495     },
496     { NULL }
497 };
498
499 AVFilter ff_vf_selectivecolor = {
500     .name          = "selectivecolor",
501     .description   = NULL_IF_CONFIG_SMALL("Apply CMYK adjustments to specific color ranges."),
502     .priv_size     = sizeof(SelectiveColorContext),
503     .query_formats = query_formats,
504     .inputs        = selectivecolor_inputs,
505     .outputs       = selectivecolor_outputs,
506     .priv_class    = &selectivecolor_class,
507     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
508 };