• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/opt.h"
20 #include "libavutil/imgutils.h"
21 #include "avfilter.h"
22 #include "drawutils.h"
23 #include "formats.h"
24 #include "internal.h"
25 #include "video.h"
26 
27 #define R 0
28 #define G 1
29 #define B 2
30 
31 #define REDS     0
32 #define YELLOWS  1
33 #define GREENS   2
34 #define CYANS    3
35 #define BLUES    4
36 #define MAGENTAS 5
37 
38 #define RED     (1 << REDS)
39 #define YELLOW  (1 << YELLOWS)
40 #define GREEN   (1 << GREENS)
41 #define CYAN    (1 << CYANS)
42 #define BLUE    (1 << BLUES)
43 #define MAGENTA (1 << MAGENTAS)
44 #define ALL      0x3F
45 
46 typedef struct HueSaturationContext {
47     const AVClass *class;
48 
49     float hue;
50     float saturation;
51     float intensity;
52     float strength;
53     float rlw, glw, blw;
54     int lightness;
55     int colors;
56 
57     int depth;
58     int planewidth[4];
59     int planeheight[4];
60 
61     float matrix[4][4];
62     int64_t imatrix[4][4];
63 
64     int bpp;
65     int step;
66     uint8_t rgba_map[4];
67 
68     int (*do_slice[2])(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
69 } HueSaturationContext;
70 
71 #define DENOM 0x10000
72 
get_triplet(int64_t m[4][4],int * r,int * g,int * b)73 static inline void get_triplet(int64_t m[4][4], int *r, int *g, int *b)
74 {
75     const int ir = *r, ig = *g, ib = *b;
76 
77     *r = (ir * m[0][0] + ig * m[1][0] + ib * m[2][0] /*+ m[3][0]*/) >> 16;
78     *g = (ir * m[0][1] + ig * m[1][1] + ib * m[2][1] /*+ m[3][1]*/) >> 16;
79     *b = (ir * m[0][2] + ig * m[1][2] + ib * m[2][2] /*+ m[3][2]*/) >> 16;
80 }
81 
82 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
83 
lerpi8(int v0,int v1,int f,int max)84 static inline int lerpi8(int v0, int v1, int f, int max)
85 {
86     return v0 + FAST_DIV255((v1 - v0) * f);
87 }
88 
lerpi16(int v0,int v1,int f,int max)89 static inline int lerpi16(int v0, int v1, int f, int max)
90 {
91     return v0 + (v1 - v0) * (int64_t)f / max;
92 }
93 
94 #define HUESATURATION(name, type, clip, xall)                        \
95 static int do_slice_##name##_##xall(AVFilterContext *ctx,            \
96                                           void *arg,                 \
97                                           int jobnr, int nb_jobs)    \
98 {                                                                    \
99     HueSaturationContext *s = ctx->priv;                             \
100     AVFrame *frame = arg;                                            \
101     const int imax = (1 << name) - 1;                                \
102     const float strength = s->strength;                              \
103     const int colors = s->colors;                                    \
104     const int step = s->step;                                        \
105     const int width = frame->width;                                  \
106     const int process_h = frame->height;                             \
107     const int slice_start = (process_h *  jobnr   ) / nb_jobs;       \
108     const int slice_end   = (process_h * (jobnr+1)) / nb_jobs;       \
109     const int linesize = frame->linesize[0] / sizeof(type);          \
110     type *row = (type *)frame->data[0] + linesize * slice_start;     \
111     const uint8_t offset_r = s->rgba_map[R];                         \
112     const uint8_t offset_g = s->rgba_map[G];                         \
113     const uint8_t offset_b = s->rgba_map[B];                         \
114     type *dst_r = row + offset_r;                                    \
115     type *dst_g = row + offset_g;                                    \
116     type *dst_b = row + offset_b;                                    \
117                                                                      \
118     for (int y = slice_start; y < slice_end; y++) {                  \
119         for (int x = 0; x < width * step; x += step) {               \
120             int ir, ig, ib, ro, go, bo;                              \
121                                                                      \
122             ir = ro = dst_r[x];                                      \
123             ig = go = dst_g[x];                                      \
124             ib = bo = dst_b[x];                                      \
125                                                                      \
126             if (xall) {                                              \
127                 get_triplet(s->imatrix, &ir, &ig, &ib);              \
128             } else {                                                 \
129                 const int min = FFMIN3(ir, ig, ib);                  \
130                 const int max = FFMAX3(ir, ig, ib);                  \
131                 const int flags = (ir == max) << REDS                \
132                                 | (ir == min) << CYANS               \
133                                 | (ig == max) << GREENS              \
134                                 | (ig == min) << MAGENTAS            \
135                                 | (ib == max) << BLUES               \
136                                 | (ib == min) << YELLOWS;            \
137                 if (colors & flags) {                                \
138                     int f = 0;                                       \
139                                                                      \
140                     if (colors & RED)                                \
141                         f = FFMAX(f, ir - FFMAX(ig, ib));            \
142                     if (colors & YELLOW)                             \
143                         f = FFMAX(f, FFMIN(ir, ig) - ib);            \
144                     if (colors & GREEN)                              \
145                         f = FFMAX(f, ig - FFMAX(ir, ib));            \
146                     if (colors & CYAN)                               \
147                         f = FFMAX(f, FFMIN(ig, ib) - ir);            \
148                     if (colors & BLUE)                               \
149                         f = FFMAX(f, ib - FFMAX(ir, ig));            \
150                     if (colors & MAGENTA)                            \
151                         f = FFMAX(f, FFMIN(ir, ib) - ig);            \
152                     f = FFMIN(f * strength, imax);                   \
153                     get_triplet(s->imatrix, &ir, &ig, &ib);          \
154                     ir = lerpi##name(ro, ir, f, imax);               \
155                     ig = lerpi##name(go, ig, f, imax);               \
156                     ib = lerpi##name(bo, ib, f, imax);               \
157                 }                                                    \
158             }                                                        \
159                                                                      \
160             dst_r[x] = clip(ir);                                     \
161             dst_g[x] = clip(ig);                                     \
162             dst_b[x] = clip(ib);                                     \
163         }                                                            \
164                                                                      \
165         dst_r += linesize;                                           \
166         dst_g += linesize;                                           \
167         dst_b += linesize;                                           \
168     }                                                                \
169                                                                      \
170     return 0;                                                        \
171 }
172 
173 HUESATURATION(8,  uint8_t,  av_clip_uint8, 0)
174 HUESATURATION(16, uint16_t, av_clip_uint16, 0)
175 
176 HUESATURATION(8,  uint8_t,  av_clip_uint8, 1)
177 HUESATURATION(16, uint16_t, av_clip_uint16, 1)
178 
identity_matrix(float matrix[4][4])179 static void identity_matrix(float matrix[4][4])
180 {
181     for (int y = 0; y < 4; y++)
182         for (int x = 0; x < 4; x++)
183             matrix[y][x] = y == x;
184 }
185 
matrix_multiply(float a[4][4],float b[4][4],float c[4][4])186 static void matrix_multiply(float a[4][4], float b[4][4], float c[4][4])
187 {
188     float temp[4][4];
189 
190     for (int y = 0; y < 4; y++) {
191         for (int x = 0; x < 4; x++) {
192             temp[y][x] = b[y][0] * a[0][x]
193                        + b[y][1] * a[1][x]
194                        + b[y][2] * a[2][x]
195                        + b[y][3] * a[3][x];
196         }
197     }
198 
199     for (int y = 0; y < 4; y++) {
200         for (int x = 0; x < 4; x++)
201             c[y][x] = temp[y][x];
202     }
203 }
204 
colorscale_matrix(float matrix[4][4],float r,float g,float b)205 static void colorscale_matrix(float matrix[4][4], float r, float g, float b)
206 {
207     float temp[4][4];
208 
209     temp[0][0] = r;   temp[0][1] = 0.f; temp[0][2] = 0.f; temp[0][3] = 0.f;
210     temp[1][0] = 0.f; temp[1][1] = g;   temp[1][2] = 0.f; temp[1][3] = 0.f;
211     temp[2][0] = 0.f; temp[2][1] = 0.f; temp[2][2] = b;   temp[2][3] = 0.f;
212     temp[3][0] = 0.f; temp[3][1] = 0.f; temp[3][2] = 0.f; temp[3][3] = 1.f;
213 
214     matrix_multiply(temp, matrix, matrix);
215 }
216 
saturation_matrix(float matrix[4][4],float saturation,float rlw,float glw,float blw)217 static void saturation_matrix(float matrix[4][4], float saturation,
218                               float rlw, float glw, float blw)
219 {
220     float s = 1.f - saturation;
221     float a = s * rlw + saturation;
222     float b = s * rlw;
223     float c = s * rlw;
224     float d = s * glw;
225     float e = s * glw + saturation;
226     float f = s * glw;
227     float g = s * blw;
228     float h = s * blw;
229     float i = s * blw + saturation;
230     float m[4][4];
231 
232     m[0][0] = a;   m[0][1] = b;   m[0][2] = c;   m[0][3] = 0.f;
233     m[1][0] = d;   m[1][1] = e;   m[1][2] = f;   m[1][3] = 0.f;
234     m[2][0] = g;   m[2][1] = h;   m[2][2] = i;   m[2][3] = 0.f;
235     m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f;
236 
237     matrix_multiply(m, matrix, matrix);
238 }
239 
matrix2imatrix(float matrix[4][4],int64_t imatrix[4][4])240 static void matrix2imatrix(float matrix[4][4], int64_t imatrix[4][4])
241 {
242     for (int y = 0; y < 4; y++)
243         for (int x = 0; x < 4; x++)
244             imatrix[y][x] = lrintf(matrix[y][x] * DENOM);
245 }
246 
x_rotate_matrix(float matrix[4][4],float rs,float rc)247 static void x_rotate_matrix(float matrix[4][4], float rs, float rc)
248 {
249     float m[4][4];
250 
251     m[0][0] = 1.f; m[0][1] = 0.f; m[0][2] = 0.f; m[0][3] = 0.f;
252     m[1][0] = 0.f; m[1][1] = rc;  m[1][2] = rs;  m[1][3] = 0.f;
253     m[2][0] = 0.f; m[2][1] = -rs; m[2][2] = rc;  m[2][3] = 0.f;
254     m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f;
255 
256     matrix_multiply(m, matrix, matrix);
257 }
258 
y_rotate_matrix(float matrix[4][4],float rs,float rc)259 static void y_rotate_matrix(float matrix[4][4], float rs, float rc)
260 {
261     float m[4][4];
262 
263     m[0][0] = rc;  m[0][1] = 0.f; m[0][2] = -rs; m[0][3] = 0.f;
264     m[1][0] = 0.f; m[1][1] = 1.f; m[1][2] = 0.f; m[1][3] = 0.f;
265     m[2][0] = rs;  m[2][1] = 0.f; m[2][2] = rc;  m[2][3] = 0.f;
266     m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f;
267 
268     matrix_multiply(m, matrix, matrix);
269 }
270 
z_rotate_matrix(float matrix[4][4],float rs,float rc)271 static void z_rotate_matrix(float matrix[4][4], float rs, float rc)
272 {
273     float m[4][4];
274 
275     m[0][0] = rc;  m[0][1] = rs;  m[0][2] = 0.f; m[0][3] = 0.f;
276     m[1][0] = -rs; m[1][1] = rc;  m[1][2] = 0.f; m[1][3] = 0.f;
277     m[2][0] = 0.f; m[2][1] = 0.f; m[2][2] = 1.f; m[2][3] = 0.f;
278     m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f;
279 
280     matrix_multiply(m, matrix, matrix);
281 }
282 
z_shear_matrix(float matrix[4][4],float dx,float dy)283 static void z_shear_matrix(float matrix[4][4], float dx, float dy)
284 {
285     float m[4][4];
286 
287     m[0][0] = 1.f; m[0][1] = 0.f; m[0][2] = dx;  m[0][3] = 0.f;
288     m[1][0] = 0.f; m[1][1] = 1.f; m[1][2] = dy;  m[1][3] = 0.f;
289     m[2][0] = 0.f; m[2][1] = 0.f; m[2][2] = 1.f; m[2][3] = 0.f;
290     m[3][0] = 0.f; m[3][1] = 0.f; m[3][2] = 0.f; m[3][3] = 1.f;
291 
292     matrix_multiply(m, matrix, matrix);
293 }
294 
transform_point(float matrix[4][4],float x,float y,float z,float * tx,float * ty,float * tz)295 static void transform_point(float matrix[4][4],
296                             float x, float y, float z,
297                             float *tx, float *ty, float *tz)
298 {
299     x = y;
300     *tx = x * matrix[0][0] + y * matrix[1][0] + z * matrix[2][0] + matrix[3][0];
301     *ty = x * matrix[0][1] + y * matrix[1][1] + z * matrix[2][1] + matrix[3][1];
302     *tz = x * matrix[0][2] + y * matrix[1][2] + z * matrix[2][2] + matrix[3][2];
303 }
304 
hue_rotate_matrix(float matrix[4][4],float rotation,float rlw,float glw,float blw)305 static void hue_rotate_matrix(float matrix[4][4], float rotation,
306                               float rlw, float glw, float blw)
307 {
308     float mag, lx, ly, lz;
309     float xrs, xrc;
310     float yrs, yrc;
311     float zrs, zrc;
312     float zsx, zsy;
313 
314     mag = M_SQRT2;
315     xrs = 1.f / mag;
316     xrc = 1.f / mag;
317     x_rotate_matrix(matrix, xrs, xrc);
318 
319     mag = sqrtf(3.f);
320     yrs = -1.f / mag;
321     yrc = M_SQRT2 / mag;
322     y_rotate_matrix(matrix, yrs, yrc);
323 
324     transform_point(matrix, rlw, glw, blw, &lx, &ly, &lz);
325     zsx = lx / lz;
326     zsy = ly / lz;
327     z_shear_matrix(matrix, zsx, zsy);
328 
329     zrs = sinf(rotation * M_PI / 180.f);
330     zrc = cosf(rotation * M_PI / 180.f);
331     z_rotate_matrix(matrix, zrs, zrc);
332 
333     z_shear_matrix(matrix, -zsx, -zsy);
334 
335     y_rotate_matrix(matrix, -yrs, yrc);
336     x_rotate_matrix(matrix, -xrs, xrc);
337 }
338 
shue_rotate_matrix(float m[4][4],float rotation)339 static void shue_rotate_matrix(float m[4][4], float rotation)
340 {
341     float xrs, xrc, yrs, yrc, zrs, zrc, mag;
342 
343     mag = M_SQRT2;
344     xrs = 1.f / mag;
345     xrc = 1.f / mag;
346     x_rotate_matrix(m, xrs, xrc);
347 
348     mag = sqrtf(3.f);
349     yrs = -1.f / mag;
350     yrc = M_SQRT2 / mag;
351     y_rotate_matrix(m, yrs, yrc);
352 
353     zrs = sinf(rotation * M_PI / 180.f);
354     zrc = cosf(rotation * M_PI / 180.f);
355     z_rotate_matrix(m, zrs, zrc);
356 
357     y_rotate_matrix(m, -yrs, yrc);
358     x_rotate_matrix(m, -xrs, xrc);
359 }
360 
init_matrix(HueSaturationContext * s)361 static void init_matrix(HueSaturationContext *s)
362 {
363     float i = 1.f + s->intensity;
364     float saturation = 1.f + s->saturation;
365     float hue = s->hue;
366 
367     identity_matrix(s->matrix);
368     colorscale_matrix(s->matrix, i, i, i);
369     saturation_matrix(s->matrix, saturation,
370                       s->rlw, s->glw, s->blw);
371 
372     if (s->lightness)
373         hue_rotate_matrix(s->matrix, hue,
374                           s->rlw, s->glw, s->blw);
375     else
376         shue_rotate_matrix(s->matrix, hue);
377 
378     matrix2imatrix(s->matrix, s->imatrix);
379 }
380 
filter_frame(AVFilterLink * inlink,AVFrame * frame)381 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
382 {
383     AVFilterContext *ctx = inlink->dst;
384     HueSaturationContext *s = ctx->priv;
385 
386     init_matrix(s);
387 
388     ff_filter_execute(ctx, s->do_slice[(s->strength >= 99.f) && (s->colors == ALL)], frame, NULL,
389                       FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
390 
391     return ff_filter_frame(ctx->outputs[0], frame);
392 }
393 
394 static const enum AVPixelFormat pixel_fmts[] = {
395     AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
396     AV_PIX_FMT_RGBA,  AV_PIX_FMT_BGRA,
397     AV_PIX_FMT_ABGR,  AV_PIX_FMT_ARGB,
398     AV_PIX_FMT_0BGR,  AV_PIX_FMT_0RGB,
399     AV_PIX_FMT_RGB0,  AV_PIX_FMT_BGR0,
400     AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
401     AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
402     AV_PIX_FMT_NONE
403 };
404 
config_input(AVFilterLink * inlink)405 static av_cold int config_input(AVFilterLink *inlink)
406 {
407     AVFilterContext *ctx = inlink->dst;
408     HueSaturationContext *s = ctx->priv;
409     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
410 
411     s->depth = desc->comp[0].depth;
412     s->bpp = s->depth >> 3;
413     s->step = av_get_padded_bits_per_pixel(desc) >> (3 + (s->bpp == 2));
414     ff_fill_rgba_map(s->rgba_map, inlink->format);
415 
416     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
417     s->planewidth[0] = s->planewidth[3] = inlink->w;
418     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
419     s->planeheight[0] = s->planeheight[3] = inlink->h;
420 
421     s->do_slice[0] = s->depth <= 8 ? do_slice_8_0 : do_slice_16_0;
422     s->do_slice[1] = s->depth <= 8 ? do_slice_8_1 : do_slice_16_1;
423 
424     return 0;
425 }
426 
427 static const AVFilterPad huesaturation_inputs[] = {
428     {
429         .name           = "default",
430         .type           = AVMEDIA_TYPE_VIDEO,
431         .flags          = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
432         .filter_frame   = filter_frame,
433         .config_props   = config_input,
434     },
435 };
436 
437 static const AVFilterPad huesaturation_outputs[] = {
438     {
439         .name = "default",
440         .type = AVMEDIA_TYPE_VIDEO,
441     },
442 };
443 
444 #define OFFSET(x) offsetof(HueSaturationContext, x)
445 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
446 
447 static const AVOption huesaturation_options[] = {
448     { "hue",        "set the hue shift",               OFFSET(hue),        AV_OPT_TYPE_FLOAT, {.dbl=0},-180, 180, VF },
449     { "saturation", "set the saturation shift",        OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl=0},  -1,   1, VF },
450     { "intensity",  "set the intensity shift",         OFFSET(intensity),  AV_OPT_TYPE_FLOAT, {.dbl=0},  -1,   1, VF },
451     { "colors",     "set colors range",                OFFSET(colors),     AV_OPT_TYPE_FLAGS, {.i64=ALL},     0,ALL,VF, "colors" },
452     {  "r",         "set reds",                        0,                  AV_OPT_TYPE_CONST, {.i64=RED},     0, 0, VF, "colors" },
453     {  "y",         "set yellows",                     0,                  AV_OPT_TYPE_CONST, {.i64=YELLOW},  0, 0, VF, "colors" },
454     {  "g",         "set greens",                      0,                  AV_OPT_TYPE_CONST, {.i64=GREEN},   0, 0, VF, "colors" },
455     {  "c",         "set cyans",                       0,                  AV_OPT_TYPE_CONST, {.i64=CYAN},    0, 0, VF, "colors" },
456     {  "b",         "set blues",                       0,                  AV_OPT_TYPE_CONST, {.i64=BLUE},    0, 0, VF, "colors" },
457     {  "m",         "set magentas",                    0,                  AV_OPT_TYPE_CONST, {.i64=MAGENTA}, 0, 0, VF, "colors" },
458     {  "a",         "set all colors",                  0,                  AV_OPT_TYPE_CONST, {.i64=ALL},     0, 0, VF, "colors" },
459     { "strength",   "set the filtering strength",      OFFSET(strength),   AV_OPT_TYPE_FLOAT, {.dbl=1},       0,100,VF },
460     { "rw",         "set the red weight",              OFFSET(rlw),        AV_OPT_TYPE_FLOAT, {.dbl=.333},    0, 1, VF },
461     { "gw",         "set the green weight",            OFFSET(glw),        AV_OPT_TYPE_FLOAT, {.dbl=.334},    0, 1, VF },
462     { "bw",         "set the blue weight",             OFFSET(blw),        AV_OPT_TYPE_FLOAT, {.dbl=.333},    0, 1, VF },
463     { "lightness",  "set the preserve lightness",      OFFSET(lightness),  AV_OPT_TYPE_BOOL,  {.i64=0},       0, 1, VF },
464     { NULL }
465 };
466 
467 AVFILTER_DEFINE_CLASS(huesaturation);
468 
469 const AVFilter ff_vf_huesaturation = {
470     .name            = "huesaturation",
471     .description     = NULL_IF_CONFIG_SMALL("Apply hue-saturation-intensity adjustments."),
472     .priv_size       = sizeof(HueSaturationContext),
473     .priv_class      = &huesaturation_class,
474     FILTER_INPUTS(huesaturation_inputs),
475     FILTER_OUTPUTS(huesaturation_outputs),
476     FILTER_PIXFMTS_ARRAY(pixel_fmts),
477     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
478     .process_command = ff_filter_process_command,
479 };
480