1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/*
* Copyright (c) 2018 Paul B Mahol
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "avfilter.h"
#include "drawutils.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
#define R 0
#define G 1
#define B 2
#define A 3
typedef struct VibranceContext {
const AVClass *class;
float intensity;
float balance[3];
float lcoeffs[3];
int alternate;
int step;
int depth;
uint8_t rgba_map[4];
int (*do_slice)(AVFilterContext *s, void *arg,
int jobnr, int nb_jobs);
} VibranceContext;
static inline float lerpf(float v0, float v1, float f)
{
return v0 + (v1 - v0) * f;
}
typedef struct ThreadData {
AVFrame *out, *in;
} ThreadData;
static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
{
VibranceContext *s = avctx->priv;
ThreadData *td = arg;
AVFrame *frame = td->out;
AVFrame *in = td->in;
const int width = frame->width;
const int height = frame->height;
const float scale = 1.f / 255.f;
const float gc = s->lcoeffs[0];
const float bc = s->lcoeffs[1];
const float rc = s->lcoeffs[2];
const float intensity = s->intensity;
const float alternate = s->alternate ? 1.f : -1.f;
const float gintensity = intensity * s->balance[0];
const float bintensity = intensity * s->balance[1];
const float rintensity = intensity * s->balance[2];
const float sgintensity = alternate * FFSIGN(gintensity);
const float sbintensity = alternate * FFSIGN(bintensity);
const float srintensity = alternate * FFSIGN(rintensity);
const int slice_start = (height * jobnr) / nb_jobs;
const int slice_end = (height * (jobnr + 1)) / nb_jobs;
const int glinesize = frame->linesize[0];
const int blinesize = frame->linesize[1];
const int rlinesize = frame->linesize[2];
const int alinesize = frame->linesize[3];
const int gslinesize = in->linesize[0];
const int bslinesize = in->linesize[1];
const int rslinesize = in->linesize[2];
const int aslinesize = in->linesize[3];
const uint8_t *gsrc = in->data[0] + slice_start * glinesize;
const uint8_t *bsrc = in->data[1] + slice_start * blinesize;
const uint8_t *rsrc = in->data[2] + slice_start * rlinesize;
uint8_t *gptr = frame->data[0] + slice_start * glinesize;
uint8_t *bptr = frame->data[1] + slice_start * blinesize;
uint8_t *rptr = frame->data[2] + slice_start * rlinesize;
const uint8_t *asrc = in->data[3];
uint8_t *aptr = frame->data[3];
for (int y = slice_start; y < slice_end; y++) {
for (int x = 0; x < width; x++) {
float g = gsrc[x] * scale;
float b = bsrc[x] * scale;
float r = rsrc[x] * scale;
float max_color = FFMAX3(r, g, b);
float min_color = FFMIN3(r, g, b);
float color_saturation = max_color - min_color;
float luma = g * gc + r * rc + b * bc;
const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
g = lerpf(luma, g, cg);
b = lerpf(luma, b, cb);
r = lerpf(luma, r, cr);
gptr[x] = av_clip_uint8(g * 255.f);
bptr[x] = av_clip_uint8(b * 255.f);
rptr[x] = av_clip_uint8(r * 255.f);
}
if (aptr && alinesize && frame != in)
memcpy(aptr + alinesize * y, asrc + aslinesize * y, width);
gsrc += gslinesize;
bsrc += bslinesize;
rsrc += rslinesize;
gptr += glinesize;
bptr += blinesize;
rptr += rlinesize;
}
return 0;
}
static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
{
VibranceContext *s = avctx->priv;
ThreadData *td = arg;
AVFrame *frame = td->out;
AVFrame *in = td->in;
const int depth = s->depth;
const float max = (1 << depth) - 1;
const float scale = 1.f / max;
const float gc = s->lcoeffs[0];
const float bc = s->lcoeffs[1];
const float rc = s->lcoeffs[2];
const int width = frame->width;
const int height = frame->height;
const float intensity = s->intensity;
const float alternate = s->alternate ? 1.f : -1.f;
const float gintensity = intensity * s->balance[0];
const float bintensity = intensity * s->balance[1];
const float rintensity = intensity * s->balance[2];
const float sgintensity = alternate * FFSIGN(gintensity);
const float sbintensity = alternate * FFSIGN(bintensity);
const float srintensity = alternate * FFSIGN(rintensity);
const int slice_start = (height * jobnr) / nb_jobs;
const int slice_end = (height * (jobnr + 1)) / nb_jobs;
const int gslinesize = in->linesize[0] / 2;
const int bslinesize = in->linesize[1] / 2;
const int rslinesize = in->linesize[2] / 2;
const int aslinesize = in->linesize[3] / 2;
const int glinesize = frame->linesize[0] / 2;
const int blinesize = frame->linesize[1] / 2;
const int rlinesize = frame->linesize[2] / 2;
const int alinesize = frame->linesize[3] / 2;
const uint16_t *gsrc = (const uint16_t *)in->data[0] + slice_start * gslinesize;
const uint16_t *bsrc = (const uint16_t *)in->data[1] + slice_start * bslinesize;
const uint16_t *rsrc = (const uint16_t *)in->data[2] + slice_start * rslinesize;
uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize;
uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize;
uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize;
const uint16_t *asrc = (const uint16_t *)in->data[3];
uint16_t *aptr = (uint16_t *)frame->data[3];
for (int y = slice_start; y < slice_end; y++) {
for (int x = 0; x < width; x++) {
float g = gsrc[x] * scale;
float b = bsrc[x] * scale;
float r = rsrc[x] * scale;
float max_color = FFMAX3(r, g, b);
float min_color = FFMIN3(r, g, b);
float color_saturation = max_color - min_color;
float luma = g * gc + r * rc + b * bc;
const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
g = lerpf(luma, g, cg);
b = lerpf(luma, b, cb);
r = lerpf(luma, r, cr);
gptr[x] = av_clip_uintp2_c(g * max, depth);
bptr[x] = av_clip_uintp2_c(b * max, depth);
rptr[x] = av_clip_uintp2_c(r * max, depth);
}
if (aptr && alinesize && frame != in)
memcpy(aptr + alinesize * y, asrc + aslinesize * y, width * 2);
gsrc += gslinesize;
bsrc += bslinesize;
rsrc += rslinesize;
gptr += glinesize;
bptr += blinesize;
rptr += rlinesize;
}
return 0;
}
static int vibrance_slice8p(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
{
VibranceContext *s = avctx->priv;
ThreadData *td = arg;
AVFrame *frame = td->out;
AVFrame *in = td->in;
const int step = s->step;
const int width = frame->width;
const int height = frame->height;
const float scale = 1.f / 255.f;
const float gc = s->lcoeffs[0];
const float bc = s->lcoeffs[1];
const float rc = s->lcoeffs[2];
const uint8_t roffset = s->rgba_map[R];
const uint8_t goffset = s->rgba_map[G];
const uint8_t boffset = s->rgba_map[B];
const uint8_t aoffset = s->rgba_map[A];
const float intensity = s->intensity;
const float alternate = s->alternate ? 1.f : -1.f;
const float gintensity = intensity * s->balance[0];
const float bintensity = intensity * s->balance[1];
const float rintensity = intensity * s->balance[2];
const float sgintensity = alternate * FFSIGN(gintensity);
const float sbintensity = alternate * FFSIGN(bintensity);
const float srintensity = alternate * FFSIGN(rintensity);
const int slice_start = (height * jobnr) / nb_jobs;
const int slice_end = (height * (jobnr + 1)) / nb_jobs;
const int linesize = frame->linesize[0];
const int slinesize = in->linesize[0];
const uint8_t *src = in->data[0] + slice_start * slinesize;
uint8_t *ptr = frame->data[0] + slice_start * linesize;
for (int y = slice_start; y < slice_end; y++) {
for (int x = 0; x < width; x++) {
float g = src[x * step + goffset] * scale;
float b = src[x * step + boffset] * scale;
float r = src[x * step + roffset] * scale;
float max_color = FFMAX3(r, g, b);
float min_color = FFMIN3(r, g, b);
float color_saturation = max_color - min_color;
float luma = g * gc + r * rc + b * bc;
const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
g = lerpf(luma, g, cg);
b = lerpf(luma, b, cb);
r = lerpf(luma, r, cr);
ptr[x * step + goffset] = av_clip_uint8(g * 255.f);
ptr[x * step + boffset] = av_clip_uint8(b * 255.f);
ptr[x * step + roffset] = av_clip_uint8(r * 255.f);
if (frame != in)
ptr[x * step + aoffset] = src[x * step + aoffset];
}
ptr += linesize;
src += slinesize;
}
return 0;
}
static int vibrance_slice16p(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
{
VibranceContext *s = avctx->priv;
ThreadData *td = arg;
AVFrame *frame = td->out;
AVFrame *in = td->in;
const int step = s->step;
const int depth = s->depth;
const float max = (1 << depth) - 1;
const float scale = 1.f / max;
const float gc = s->lcoeffs[0];
const float bc = s->lcoeffs[1];
const float rc = s->lcoeffs[2];
const uint8_t roffset = s->rgba_map[R];
const uint8_t goffset = s->rgba_map[G];
const uint8_t boffset = s->rgba_map[B];
const uint8_t aoffset = s->rgba_map[A];
const int width = frame->width;
const int height = frame->height;
const float intensity = s->intensity;
const float alternate = s->alternate ? 1.f : -1.f;
const float gintensity = intensity * s->balance[0];
const float bintensity = intensity * s->balance[1];
const float rintensity = intensity * s->balance[2];
const float sgintensity = alternate * FFSIGN(gintensity);
const float sbintensity = alternate * FFSIGN(bintensity);
const float srintensity = alternate * FFSIGN(rintensity);
const int slice_start = (height * jobnr) / nb_jobs;
const int slice_end = (height * (jobnr + 1)) / nb_jobs;
const int linesize = frame->linesize[0] / 2;
const int slinesize = in->linesize[0] / 2;
const uint16_t *src = (const uint16_t *)in->data[0] + slice_start * slinesize;
uint16_t *ptr = (uint16_t *)frame->data[0] + slice_start * linesize;
for (int y = slice_start; y < slice_end; y++) {
for (int x = 0; x < width; x++) {
float g = src[x * step + goffset] * scale;
float b = src[x * step + boffset] * scale;
float r = src[x * step + roffset] * scale;
float max_color = FFMAX3(r, g, b);
float min_color = FFMIN3(r, g, b);
float color_saturation = max_color - min_color;
float luma = g * gc + r * rc + b * bc;
const float cg = 1.f + gintensity * (1.f - sgintensity * color_saturation);
const float cb = 1.f + bintensity * (1.f - sbintensity * color_saturation);
const float cr = 1.f + rintensity * (1.f - srintensity * color_saturation);
g = lerpf(luma, g, cg);
b = lerpf(luma, b, cb);
r = lerpf(luma, r, cr);
ptr[x * step + goffset] = av_clip_uintp2_c(g * max, depth);
ptr[x * step + boffset] = av_clip_uintp2_c(b * max, depth);
ptr[x * step + roffset] = av_clip_uintp2_c(r * max, depth);
if (frame != in)
ptr[x * step + aoffset] = src[x * step + aoffset];
}
ptr += linesize;
src += slinesize;
}
return 0;
}
static int filter_frame(AVFilterLink *link, AVFrame *in)
{
AVFilterContext *avctx = link->dst;
AVFilterLink *outlink = avctx->outputs[0];
VibranceContext *s = avctx->priv;
ThreadData td;
AVFrame *out;
int res;
if (av_frame_is_writable(in)) {
out = in;
} else {
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
if (!out) {
av_frame_free(&in);
return AVERROR(ENOMEM);
}
av_frame_copy_props(out, in);
}
td.out = out;
td.in = in;
if (res = ff_filter_execute(avctx, s->do_slice, &td, NULL,
FFMIN(out->height, ff_filter_get_nb_threads(avctx))))
return res;
if (out != in)
av_frame_free(&in);
return ff_filter_frame(outlink, out);
}
static const enum AVPixelFormat pixel_fmts[] = {
AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12,
AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
AV_PIX_FMT_NONE
};
static av_cold int config_input(AVFilterLink *inlink)
{
AVFilterContext *avctx = inlink->dst;
VibranceContext *s = avctx->priv;
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
int planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR;
s->step = desc->nb_components;
if (inlink->format == AV_PIX_FMT_RGB0 ||
inlink->format == AV_PIX_FMT_0RGB ||
inlink->format == AV_PIX_FMT_BGR0 ||
inlink->format == AV_PIX_FMT_0BGR)
s->step = 4;
s->depth = desc->comp[0].depth;
s->do_slice = s->depth <= 8 ? vibrance_slice8 : vibrance_slice16;
if (!planar)
s->do_slice = s->depth <= 8 ? vibrance_slice8p : vibrance_slice16p;
ff_fill_rgba_map(s->rgba_map, inlink->format);
return 0;
}
static const AVFilterPad vibrance_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = filter_frame,
.config_props = config_input,
},
};
static const AVFilterPad vibrance_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
};
#define OFFSET(x) offsetof(VibranceContext, x)
#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
static const AVOption vibrance_options[] = {
{ "intensity", "set the intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0}, -2, 2, VF },
{ "rbal", "set the red balance value", OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
{ "gbal", "set the green balance value", OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
{ "bbal", "set the blue balance value", OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
{ "rlum", "set the red luma coefficient", OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0, 1, VF },
{ "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0, 1, VF },
{ "blum", "set the blue luma coefficient", OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0, 1, VF },
{ "alternate", "use alternate colors", OFFSET(alternate), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
{ NULL }
};
AVFILTER_DEFINE_CLASS(vibrance);
const AVFilter ff_vf_vibrance = {
.name = "vibrance",
.description = NULL_IF_CONFIG_SMALL("Boost or alter saturation."),
.priv_size = sizeof(VibranceContext),
.priv_class = &vibrance_class,
FILTER_INPUTS(vibrance_inputs),
FILTER_OUTPUTS(vibrance_outputs),
FILTER_PIXFMTS_ARRAY(pixel_fmts),
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
.process_command = ff_filter_process_command,
};
|