2 * Copyright (c) 2018 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/opt.h"
22 #include "libavutil/imgutils.h"
28 typedef struct VibranceContext {
38 int (*do_slice)(AVFilterContext *s, void *arg,
39 int jobnr, int nb_jobs);
42 static inline float lerpf(float v0, float v1, float f)
44 return v0 + (v1 - v0) * f;
47 static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
49 VibranceContext *s = avctx->priv;
51 const int width = frame->width;
52 const int height = frame->height;
53 const float scale = 1.f / 255.f;
54 const float gc = s->lcoeffs[0];
55 const float bc = s->lcoeffs[1];
56 const float rc = s->lcoeffs[2];
57 const float intensity = s->intensity;
58 const float alternate = s->alternate ? 1.f : -1.f;
59 const float gintensity = intensity * s->balance[0];
60 const float bintensity = intensity * s->balance[1];
61 const float rintensity = intensity * s->balance[2];
62 const float sgintensity = alternate * FFSIGN(gintensity);
63 const float sbintensity = alternate * FFSIGN(bintensity);
64 const float srintensity = alternate * FFSIGN(rintensity);
65 const int slice_start = (height * jobnr) / nb_jobs;
66 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
67 const int glinesize = frame->linesize[0];
68 const int blinesize = frame->linesize[1];
69 const int rlinesize = frame->linesize[2];
70 uint8_t *gptr = frame->data[0] + slice_start * glinesize;
71 uint8_t *bptr = frame->data[1] + slice_start * blinesize;
72 uint8_t *rptr = frame->data[2] + slice_start * rlinesize;
74 for (int y = slice_start; y < slice_end; y++) {
75 for (int x = 0; x < width; x++) {
76 float g = gptr[x] * scale;
77 float b = bptr[x] * scale;
78 float r = rptr[x] * scale;
79 float max_color = FFMAX3(r, g, b);
80 float min_color = FFMIN3(r, g, b);
81 float color_saturation = max_color - min_color;
82 float luma = g * gc + r * rc + b * bc;
83 const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
84 const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
85 const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
87 g = lerpf(luma, g, cg);
88 b = lerpf(luma, b, cb);
89 r = lerpf(luma, r, cr);
91 gptr[x] = av_clip_uint8(g * 255.f);
92 bptr[x] = av_clip_uint8(b * 255.f);
93 rptr[x] = av_clip_uint8(r * 255.f);
104 static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
106 VibranceContext *s = avctx->priv;
107 AVFrame *frame = arg;
108 const int depth = s->depth;
109 const float max = (1 << depth) - 1;
110 const float scale = 1.f / max;
111 const float gc = s->lcoeffs[0];
112 const float bc = s->lcoeffs[1];
113 const float rc = s->lcoeffs[2];
114 const int width = frame->width;
115 const int height = frame->height;
116 const float intensity = s->intensity;
117 const float alternate = s->alternate ? 1.f : -1.f;
118 const float gintensity = intensity * s->balance[0];
119 const float bintensity = intensity * s->balance[1];
120 const float rintensity = intensity * s->balance[2];
121 const float sgintensity = alternate * FFSIGN(gintensity);
122 const float sbintensity = alternate * FFSIGN(bintensity);
123 const float srintensity = alternate * FFSIGN(rintensity);
124 const int slice_start = (height * jobnr) / nb_jobs;
125 const int slice_end = (height * (jobnr + 1)) / nb_jobs;
126 const int glinesize = frame->linesize[0] / 2;
127 const int blinesize = frame->linesize[1] / 2;
128 const int rlinesize = frame->linesize[2] / 2;
129 uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize;
130 uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize;
131 uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize;
133 for (int y = slice_start; y < slice_end; y++) {
134 for (int x = 0; x < width; x++) {
135 float g = gptr[x] * scale;
136 float b = bptr[x] * scale;
137 float r = rptr[x] * scale;
138 float max_color = FFMAX3(r, g, b);
139 float min_color = FFMIN3(r, g, b);
140 float color_saturation = max_color - min_color;
141 float luma = g * gc + r * rc + b * bc;
142 const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
143 const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
144 const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
146 g = lerpf(luma, g, cg);
147 b = lerpf(luma, b, cb);
148 r = lerpf(luma, r, cr);
150 gptr[x] = av_clip_uintp2_c(g * max, depth);
151 bptr[x] = av_clip_uintp2_c(b * max, depth);
152 rptr[x] = av_clip_uintp2_c(r * max, depth);
163 static int filter_frame(AVFilterLink *link, AVFrame *frame)
165 AVFilterContext *avctx = link->dst;
166 VibranceContext *s = avctx->priv;
169 if (res = avctx->internal->execute(avctx, s->do_slice, frame, NULL,
170 FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
173 return ff_filter_frame(avctx->outputs[0], frame);
176 static av_cold int query_formats(AVFilterContext *avctx)
178 static const enum AVPixelFormat pixel_fmts[] = {
179 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
180 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
181 AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
182 AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
186 AVFilterFormats *formats = NULL;
188 formats = ff_make_format_list(pixel_fmts);
190 return AVERROR(ENOMEM);
192 return ff_set_common_formats(avctx, formats);
195 static av_cold int config_input(AVFilterLink *inlink)
197 AVFilterContext *avctx = inlink->dst;
198 VibranceContext *s = avctx->priv;
199 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
201 s->depth = desc->comp[0].depth;
202 s->do_slice = s->depth <= 8 ? vibrance_slice8 : vibrance_slice16;
207 static const AVFilterPad vibrance_inputs[] = {
210 .type = AVMEDIA_TYPE_VIDEO,
212 .filter_frame = filter_frame,
213 .config_props = config_input,
218 static const AVFilterPad vibrance_outputs[] = {
221 .type = AVMEDIA_TYPE_VIDEO,
226 #define OFFSET(x) offsetof(VibranceContext, x)
227 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
229 static const AVOption vibrance_options[] = {
230 { "intensity", "set the intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0}, -2, 2, VF },
231 { "rbal", "set the red balance value", OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
232 { "gbal", "set the green balance value", OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
233 { "bbal", "set the blue balance value", OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
234 { "rlum", "set the red luma coefficient", OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0, 1, VF },
235 { "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0, 1, VF },
236 { "blum", "set the blue luma coefficient", OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0, 1, VF },
237 { "alternate", "use alternate colors", OFFSET(alternate), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
241 AVFILTER_DEFINE_CLASS(vibrance);
243 AVFilter ff_vf_vibrance = {
245 .description = NULL_IF_CONFIG_SMALL("Boost or alter saturation."),
246 .priv_size = sizeof(VibranceContext),
247 .priv_class = &vibrance_class,
248 .query_formats = query_formats,
249 .inputs = vibrance_inputs,
250 .outputs = vibrance_outputs,
251 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
252 .process_command = ff_filter_process_command,