FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_vibrance.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Paul B Mahol
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 #include "libavutil/opt.h"
22 #include "libavutil/imgutils.h"
23 #include "avfilter.h"
24 #include "formats.h"
25 #include "internal.h"
26 #include "video.h"
27 
28 typedef struct VibranceContext {
29  const AVClass *class;
30 
31  float intensity;
32  float balance[3];
33  float lcoeffs[3];
34 
35  int depth;
36 
38  int jobnr, int nb_jobs);
40 
41 static inline float lerpf(float v0, float v1, float f)
42 {
43  return v0 + (v1 - v0) * f;
44 }
45 
46 static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
47 {
48  VibranceContext *s = avctx->priv;
49  AVFrame *frame = arg;
50  const int width = frame->width;
51  const int height = frame->height;
52  const float gc = s->lcoeffs[0];
53  const float bc = s->lcoeffs[1];
54  const float rc = s->lcoeffs[2];
55  const float intensity = s->intensity;
56  const float gintensity = intensity * s->balance[0];
57  const float bintensity = intensity * s->balance[1];
58  const float rintensity = intensity * s->balance[2];
59  const int slice_start = (height * jobnr) / nb_jobs;
60  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
61  const int glinesize = frame->linesize[0];
62  const int blinesize = frame->linesize[1];
63  const int rlinesize = frame->linesize[2];
64  uint8_t *gptr = frame->data[0] + slice_start * glinesize;
65  uint8_t *bptr = frame->data[1] + slice_start * blinesize;
66  uint8_t *rptr = frame->data[2] + slice_start * rlinesize;
67 
68  for (int y = slice_start; y < slice_end; y++) {
69  for (int x = 0; x < width; x++) {
70  float g = gptr[x] / 255.f;
71  float b = bptr[x] / 255.f;
72  float r = rptr[x] / 255.f;
73  float max_color = FFMAX3(r, g, b);
74  float min_color = FFMIN3(r, g, b);
75  float color_saturation = max_color - min_color;
76  float luma = g * gc + r * rc + b * bc;
77  const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation);
78  const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation);
79  const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation);
80 
81  g = lerpf(luma, g, cg);
82  b = lerpf(luma, b, cb);
83  r = lerpf(luma, r, cr);
84 
85  gptr[x] = av_clip_uint8(g * 255.f);
86  bptr[x] = av_clip_uint8(b * 255.f);
87  rptr[x] = av_clip_uint8(r * 255.f);
88  }
89 
90  gptr += glinesize;
91  bptr += blinesize;
92  rptr += rlinesize;
93  }
94 
95  return 0;
96 }
97 
98 static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
99 {
100  VibranceContext *s = avctx->priv;
101  AVFrame *frame = arg;
102  const int depth = s->depth;
103  const float max = (1 << depth) - 1;
104  const float gc = s->lcoeffs[0];
105  const float bc = s->lcoeffs[1];
106  const float rc = s->lcoeffs[2];
107  const int width = frame->width;
108  const int height = frame->height;
109  const float intensity = s->intensity;
110  const float gintensity = intensity * s->balance[0];
111  const float bintensity = intensity * s->balance[1];
112  const float rintensity = intensity * s->balance[2];
113  const int slice_start = (height * jobnr) / nb_jobs;
114  const int slice_end = (height * (jobnr + 1)) / nb_jobs;
115  const int glinesize = frame->linesize[0] / 2;
116  const int blinesize = frame->linesize[1] / 2;
117  const int rlinesize = frame->linesize[2] / 2;
118  uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize;
119  uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize;
120  uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize;
121 
122  for (int y = slice_start; y < slice_end; y++) {
123  for (int x = 0; x < width; x++) {
124  float g = gptr[x] / max;
125  float b = bptr[x] / max;
126  float r = rptr[x] / max;
127  float max_color = FFMAX3(r, g, b);
128  float min_color = FFMIN3(r, g, b);
129  float color_saturation = max_color - min_color;
130  float luma = g * gc + r * rc + b * bc;
131  const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation);
132  const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation);
133  const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation);
134 
135  g = lerpf(luma, g, cg);
136  b = lerpf(luma, b, cb);
137  r = lerpf(luma, r, cr);
138 
139  gptr[x] = av_clip_uintp2_c(g * max, depth);
140  bptr[x] = av_clip_uintp2_c(b * max, depth);
141  rptr[x] = av_clip_uintp2_c(r * max, depth);
142  }
143 
144  gptr += glinesize;
145  bptr += blinesize;
146  rptr += rlinesize;
147  }
148 
149  return 0;
150 }
151 
153 {
154  AVFilterContext *avctx = link->dst;
155  VibranceContext *s = avctx->priv;
156  int res;
157 
158  if (res = avctx->internal->execute(avctx, s->do_slice, frame, NULL,
159  FFMIN(frame->height, ff_filter_get_nb_threads(avctx))))
160  return res;
161 
162  return ff_filter_frame(avctx->outputs[0], frame);
163 }
164 
166 {
167  static const enum AVPixelFormat pixel_fmts[] = {
173  };
174 
176 
177  formats = ff_make_format_list(pixel_fmts);
178  if (!formats)
179  return AVERROR(ENOMEM);
180 
181  return ff_set_common_formats(avctx, formats);
182 }
183 
184 static av_cold int config_input(AVFilterLink *inlink)
185 {
186  AVFilterContext *avctx = inlink->dst;
187  VibranceContext *s = avctx->priv;
189 
190  s->depth = desc->comp[0].depth;
192 
193  return 0;
194 }
195 
196 static const AVFilterPad vibrance_inputs[] = {
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_VIDEO,
200  .needs_writable = 1,
201  .filter_frame = filter_frame,
202  .config_props = config_input,
203  },
204  { NULL }
205 };
206 
207 static const AVFilterPad vibrance_outputs[] = {
208  {
209  .name = "default",
210  .type = AVMEDIA_TYPE_VIDEO,
211  },
212  { NULL }
213 };
214 
215 #define OFFSET(x) offsetof(VibranceContext, x)
216 #define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
217 
218 static const AVOption vibrance_options[] = {
219  { "intensity", "set the intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0}, -2, 2, VF },
220  { "rbal", "set the red balance value", OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
221  { "gbal", "set the green balance value", OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
222  { "bbal", "set the blue balance value", OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF },
223  { "rlum", "set the red luma coefficient", OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0, 1, VF },
224  { "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0, 1, VF },
225  { "blum", "set the blue luma coefficient", OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0, 1, VF },
226  { NULL }
227 };
228 
229 AVFILTER_DEFINE_CLASS(vibrance);
230 
232  .name = "vibrance",
233  .description = NULL_IF_CONFIG_SMALL("Boost or alter saturation."),
234  .priv_size = sizeof(VibranceContext),
235  .priv_class = &vibrance_class,
237  .inputs = vibrance_inputs,
238  .outputs = vibrance_outputs,
240 };
AVFILTER_DEFINE_CLASS(vibrance)
#define NULL
Definition: coverity.c:32
static av_cold int query_formats(AVFilterContext *avctx)
Definition: vf_vibrance.c:165
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2446
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
#define AV_PIX_FMT_GBRAP10
Definition: pixfmt.h:399
misc image utilities
Main libavfilter public API header.
const char * g
Definition: vf_curves.c:115
const char * desc
Definition: nvenc.c:65
planar GBR 4:4:4 24bpp
Definition: pixfmt.h:168
const char * b
Definition: vf_curves.c:116
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:395
GLfloat v0
Definition: opengl_enc.c:107
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:125
const char * name
Pad name.
Definition: internal.h:60
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:394
static AVFrame * frame
#define height
float lcoeffs[3]
Definition: vf_vibrance.c:33
#define FFMIN3(a, b, c)
Definition: common.h:97
static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_vibrance.c:46
#define VF
Definition: vf_vibrance.c:216
A filter pad used for either input or output.
Definition: internal.h:54
int width
Definition: frame.h:284
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:114
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
const char * arg
Definition: jacosubdec.c:66
#define AV_PIX_FMT_GBRAP12
Definition: pixfmt.h:400
static const AVOption vibrance_options[]
Definition: vf_vibrance.c:218
float balance[3]
Definition: vf_vibrance.c:32
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:401
int(* do_slice)(AVFilterContext *s, void *arg, int jobnr, int nb_jobs)
Definition: vf_vibrance.c:37
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:398
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
#define FFMIN(a, b)
Definition: common.h:96
static const AVFilterPad vibrance_inputs[]
Definition: vf_vibrance.c:196
#define width
#define FFSIGN(a)
Definition: common.h:73
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:397
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_vibrance.c:152
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
const char * name
Filter name.
Definition: avfilter.h:148
static const AVFilterPad vibrance_outputs[]
Definition: vf_vibrance.c:207
static av_cold int config_input(AVFilterLink *inlink)
Definition: vf_vibrance.c:184
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
static float lerpf(float v0, float v1, float f)
Definition: vf_vibrance.c:41
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:396
#define flags(name, subs,...)
Definition: cbs_av1.c:596
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:378
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
int
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_vibrance.c:98
avfilter_execute_func * execute
Definition: internal.h:155
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2029
AVFilter ff_vf_vibrance
Definition: vf_vibrance.c:231
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:338
int height
Definition: frame.h:284
formats
Definition: signature.h:48
internal API functions
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:113
int depth
Number of bits in the component.
Definition: pixdesc.h:58
#define OFFSET(x)
Definition: vf_vibrance.c:215
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
for(j=16;j >0;--j)
#define FFMAX3(a, b, c)
Definition: common.h:95
static av_always_inline av_const unsigned av_clip_uintp2_c(int a, int p)
Clip a signed integer to an unsigned power of two range.
Definition: common.h:229