FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_huesaturation.c
Date: 2024-04-19 17:50:32
Exec Total Coverage
Lines: 0 162 0.0%
Functions: 0 22 0.0%
Branches: 0 100 0.0%

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