FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_eq.c
Go to the documentation of this file.
1 /*
2  * Original MPlayer filters by Richard Felker, Hampa Hug, Daniel Moreno,
3  * and Michael Niedermeyer.
4  *
5  * Copyright (c) 2014 James Darnley <james.darnley@gmail.com>
6  * Copyright (c) 2015 Arwa Arif <arwaarif1994@gmail.com>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 /**
26  * @file
27  * very simple video equalizer
28  */
29 
30 /**
31  * TODO:
32  * - Add support to process_command
33  */
34 
35 #include "libavfilter/internal.h"
36 #include "libavutil/common.h"
37 #include "libavutil/imgutils.h"
38 #include "libavutil/opt.h"
39 #include "libavutil/pixdesc.h"
40 #include "vf_eq.h"
41 
42 static void create_lut(EQParameters *param)
43 {
44  int i;
45  double g = 1.0 / param->gamma;
46  double lw = 1.0 - param->gamma_weight;
47 
48  for (i = 0; i < 256; i++) {
49  double v = i / 255.0;
50  v = param->contrast * (v - 0.5) + 0.5 + param->brightness;
51 
52  if (v <= 0.0) {
53  param->lut[i] = 0;
54  } else {
55  v = v * lw + pow(v, g) * param->gamma_weight;
56 
57  if (v >= 1.0)
58  param->lut[i] = 255;
59  else
60  param->lut[i] = 256.0 * v;
61  }
62  }
63 
64  param->lut_clean = 1;
65 }
66 
67 static void apply_lut(EQParameters *param, uint8_t *dst, int dst_stride,
68  const uint8_t *src, int src_stride, int w, int h)
69 {
70  int x, y;
71 
72  if (!param->lut_clean)
73  create_lut(param);
74 
75  for (y = 0; y < h; y++) {
76  for (x = 0; x < w; x++) {
77  dst[y * dst_stride + x] = param->lut[src[y * src_stride + x]];
78  }
79  }
80 }
81 
82 static void process_c(EQParameters *param, uint8_t *dst, int dst_stride,
83  const uint8_t *src, int src_stride, int w, int h)
84 {
85  int x, y, pel;
86 
87  int contrast = (int) (param->contrast * 256 * 16);
88  int brightness = ((int) (100.0 * param->brightness + 100.0) * 511) / 200 - 128 - contrast / 32;
89 
90  for (y = 0; y < h; y++) {
91  for (x = 0; x < w; x++) {
92  pel = ((src[y * src_stride + x] * contrast) >> 12) + brightness;
93 
94  if (pel & ~255)
95  pel = (-pel) >> 31;
96 
97  dst[y * dst_stride + x] = pel;
98  }
99  }
100 }
101 
102 static void check_values(EQParameters *param, EQContext *eq)
103 {
104  if (param->contrast == 1.0 && param->brightness == 0.0 && param->gamma == 1.0)
105  param->adjust = NULL;
106  else if (param->gamma == 1.0)
107  param->adjust = eq->process;
108  else
109  param->adjust = apply_lut;
110 }
111 
112 static void set_contrast(EQContext *eq)
113 {
114  eq->var_values[VAR_CONTRAST] = av_clipf(av_expr_eval(eq->contrast_pexpr, eq->var_values, eq),-2.0, 2.0);
115  eq->param[0].contrast = eq->var_values[VAR_CONTRAST];
116  eq->param[0].lut_clean = 0;
117  check_values(&eq->param[0], eq);
118 }
119 
121 {
122  eq->var_values[VAR_BRIGHTNESS] = av_clipf(av_expr_eval(eq->brightness_pexpr, eq->var_values, eq), -1.0, 1.0);
124  eq->param[0].lut_clean = 0;
125  check_values(&eq->param[0], eq);
126 }
127 
128 static void set_gamma(EQContext *eq)
129 {
130  int i;
131 
132  eq->var_values[VAR_GAMMA] = av_clipf(av_expr_eval(eq->gamma_pexpr, eq->var_values, eq), 0.1, 10.0);
133  eq->var_values[VAR_GAMMA_R] = av_clipf(av_expr_eval(eq->gamma_r_pexpr, eq->var_values, eq), 0.1, 10.0);
134  eq->var_values[VAR_GAMMA_G] = av_clipf(av_expr_eval(eq->gamma_g_pexpr, eq->var_values, eq), 0.1, 10.0);
135  eq->var_values[VAR_GAMMA_B] = av_clipf(av_expr_eval(eq->gamma_b_pexpr, eq->var_values, eq), 0.1, 10.0);
136  eq->var_values[VAR_GAMMA_WEIGHT] = av_clipf(av_expr_eval(eq->gamma_weight_pexpr, eq->var_values, eq), 0.0, 1.0);
137 
138  eq->param[0].gamma = eq->var_values[VAR_GAMMA] * eq->var_values[VAR_GAMMA_G];
139  eq->param[1].gamma = sqrt(eq->var_values[VAR_GAMMA_B] / eq->var_values[VAR_GAMMA_G]);
140  eq->param[2].gamma = sqrt(eq->var_values[VAR_GAMMA_R] / eq->var_values[VAR_GAMMA_G]);
141 
142  for (i = 0; i < 3; i++) {
144  eq->param[i].lut_clean = 0;
145  check_values(&eq->param[i], eq);
146  }
147 }
148 
150 {
151  int i;
152 
153  eq->var_values[VAR_SATURATION] = av_clipf(av_expr_eval(eq->saturation_pexpr, eq->var_values, eq), 0.0, 3.0);
154 
155  for (i = 1; i < 3; i++) {
156  eq->param[i].contrast = eq->var_values[VAR_SATURATION];
157  eq->param[i].lut_clean = 0;
158  check_values(&eq->param[i], eq);
159  }
160 }
161 
162 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
163 {
164  int ret;
165  AVExpr *old = NULL;
166 
167  if (*pexpr)
168  old = *pexpr;
169  ret = av_expr_parse(pexpr, expr, var_names,
170  NULL, NULL, NULL, NULL, 0, log_ctx);
171  if (ret < 0) {
172  av_log(log_ctx, AV_LOG_ERROR,
173  "Error when evaluating the expression '%s' for %s\n",
174  expr, option);
175  *pexpr = old;
176  return ret;
177  }
178 
179  av_expr_free(old);
180  return 0;
181 }
182 
183 static int initialize(AVFilterContext *ctx)
184 {
185  EQContext *eq = ctx->priv;
186  int ret;
187 
188  eq->process = process_c;
189 
190  if ((ret = set_expr(&eq->contrast_pexpr, eq->contrast_expr, "contrast", ctx)) < 0 ||
191  (ret = set_expr(&eq->brightness_pexpr, eq->brightness_expr, "brightness", ctx)) < 0 ||
192  (ret = set_expr(&eq->saturation_pexpr, eq->saturation_expr, "saturation", ctx)) < 0 ||
193  (ret = set_expr(&eq->gamma_pexpr, eq->gamma_expr, "gamma", ctx)) < 0 ||
194  (ret = set_expr(&eq->gamma_r_pexpr, eq->gamma_r_expr, "gamma_r", ctx)) < 0 ||
195  (ret = set_expr(&eq->gamma_g_pexpr, eq->gamma_g_expr, "gamma_g", ctx)) < 0 ||
196  (ret = set_expr(&eq->gamma_b_pexpr, eq->gamma_b_expr, "gamma_b", ctx)) < 0 ||
197  (ret = set_expr(&eq->gamma_weight_pexpr, eq->gamma_weight_expr, "gamma_weight", ctx)) < 0 )
198  return ret;
199 
200  if (ARCH_X86)
201  ff_eq_init_x86(eq);
202 
203  set_gamma(eq);
204  set_contrast(eq);
205  set_brightness(eq);
206  set_saturation(eq);
207 
208  return 0;
209 }
210 
211 static void uninit(AVFilterContext *ctx)
212 {
213  EQContext *eq = ctx->priv;
214 
223 }
224 
226 {
227  static const enum AVPixelFormat pixel_fmts_eq[] = {
235  };
236 
237  ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
238 
239  return 0;
240 }
241 
242 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
243 {
244  AVFilterContext *ctx = inlink->dst;
245  AVFilterLink *outlink = inlink->dst->outputs[0];
246  EQContext *eq = ctx->priv;
247  AVFrame *out;
248  const AVPixFmtDescriptor *desc;
249  int i;
250 
251  out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
252  if (!out)
253  return AVERROR(ENOMEM);
254 
255  av_frame_copy_props(out, in);
256  desc = av_pix_fmt_desc_get(inlink->format);
257 
258  for (i = 0; i < desc->nb_components; i++) {
259  int w = inlink->w;
260  int h = inlink->h;
261 
262  if (i == 1 || i == 2) {
263  w = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
264  h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
265  }
266 
267  if (eq->param[i].adjust)
268  eq->param[i].adjust(&eq->param[i], out->data[i], out->linesize[i],
269  in->data[i], in->linesize[i], w, h);
270  else
271  av_image_copy_plane(out->data[i], out->linesize[i],
272  in->data[i], in->linesize[i], w, h);
273  }
274 
275  av_frame_free(&in);
276  return ff_filter_frame(outlink, out);
277 }
278 
279 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
280  char *res, int res_len, int flags)
281 {
282  EQContext *eq = ctx->priv;
283  int ret;
284 
285  if (!strcmp(cmd, "contrast")) {
286  ret = set_expr(&eq->contrast_pexpr, args, cmd, ctx);
287  set_contrast(eq);
288  return ret;
289  }
290  else if (!strcmp(cmd, "brightness")) {
291  ret = set_expr(&eq->brightness_pexpr, args, cmd, ctx);
292  set_brightness(eq);
293  return ret;
294  }
295  else if (!strcmp(cmd, "saturation")) {
296  ret = set_expr(&eq->saturation_pexpr, args, cmd, ctx);
297  set_saturation(eq);
298  return ret;
299  }
300  else if (!strcmp(cmd, "gamma")) {
301  ret = set_expr(&eq->gamma_pexpr, args, cmd, ctx);
302  set_gamma(eq);
303  return ret;
304  }
305  else if (!strcmp(cmd, "gamma_r")) {
306  ret = set_expr(&eq->gamma_r_pexpr, args, cmd, ctx);
307  set_gamma(eq);
308  return ret;
309  }
310  else if (!strcmp(cmd, "gamma_g")) {
311  ret = set_expr(&eq->gamma_g_pexpr, args, cmd, ctx);
312  set_gamma(eq);
313  return ret;
314  }
315  else if (!strcmp(cmd, "gamma_b")) {
316  ret = set_expr(&eq->gamma_b_pexpr, args, cmd, ctx);
317  set_gamma(eq);
318  return ret;
319  }
320  else if (!strcmp(cmd, "gamma_weight")) {
321  ret = set_expr(&eq->gamma_weight_pexpr, args, cmd, ctx);
322  set_gamma(eq);
323  return ret;
324  }
325  else
326  return AVERROR(ENOSYS);
327 }
328 
329 static const AVFilterPad eq_inputs[] = {
330  {
331  .name = "default",
332  .type = AVMEDIA_TYPE_VIDEO,
333  .filter_frame = filter_frame,
334  },
335  { NULL }
336 };
337 
338 static const AVFilterPad eq_outputs[] = {
339  {
340  .name = "default",
341  .type = AVMEDIA_TYPE_VIDEO,
342  },
343  { NULL }
344 };
345 
346 #define OFFSET(x) offsetof(EQContext, x)
347 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
348 
349 static const AVOption eq_options[] = {
350  { "contrast", "set the contrast adjustment, negative values give a negative image",
351  OFFSET(contrast_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
352  { "brightness", "set the brightness adjustment",
353  OFFSET(brightness_expr), AV_OPT_TYPE_STRING, {.str = "0.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
354  { "saturation", "set the saturation adjustment",
355  OFFSET(saturation_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
356  { "gamma", "set the initial gamma value",
357  OFFSET(gamma_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
358  { "gamma_r", "gamma value for red",
359  OFFSET(gamma_r_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
360  { "gamma_g", "gamma value for green",
361  OFFSET(gamma_g_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
362  { "gamma_b", "gamma value for blue",
363  OFFSET(gamma_b_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
364  { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
365  OFFSET(gamma_weight_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, CHAR_MIN, CHAR_MAX, FLAGS },
366  { NULL }
367 };
368 
370 
372  .name = "eq",
373  .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
374  .priv_size = sizeof(EQContext),
375  .priv_class = &eq_class,
376  .inputs = eq_inputs,
377  .outputs = eq_outputs,
380  .init = initialize,
381  .uninit = uninit,
382 };