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->param[0].contrast = eq->contrast;
115  eq->param[0].lut_clean = 0;
116  check_values(&eq->param[0], eq);
117 }
118 
120 {
121  eq->param[0].brightness = eq->brightness;
122  eq->param[0].lut_clean = 0;
123  check_values(&eq->param[0], eq);
124 }
125 
126 static void set_gamma(EQContext *eq)
127 {
128  int i;
129  eq->param[0].gamma = eq->gamma * eq->gamma_g;
130  eq->param[1].gamma = sqrt(eq->gamma_b / eq->gamma_g);
131  eq->param[2].gamma = sqrt(eq->gamma_r / eq->gamma_g);
132 
133  for (i = 0; i < 3; i++) {
134  eq->param[i].gamma_weight = eq->gamma_weight;
135  eq->param[i].lut_clean = 0;
136  check_values(&eq->param[i], eq);
137  }
138 }
139 
141 {
142  int i;
143  for (i = 1; i < 3; i++) {
144  eq->param[i].contrast = eq->saturation;
145  eq->param[i].lut_clean = 0;
146  check_values(&eq->param[i], eq);
147  }
148 }
149 
150 static int initialize(AVFilterContext *ctx)
151 {
152  EQContext *eq = ctx->priv;
153 
154  eq->process = process_c;
155 
156  if (ARCH_X86)
157  ff_eq_init_x86(eq);
158 
159  set_gamma(eq);
160  set_contrast(eq);
161  set_brightness(eq);
162  set_saturation(eq);
163 
164  return 0;
165 }
166 
168 {
169  static const enum AVPixelFormat pixel_fmts_eq[] = {
177  };
178 
179  ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
180 
181  return 0;
182 }
183 
184 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
185 {
186  AVFilterContext *ctx = inlink->dst;
187  AVFilterLink *outlink = inlink->dst->outputs[0];
188  EQContext *eq = ctx->priv;
189  AVFrame *out;
190  const AVPixFmtDescriptor *desc;
191  int i;
192 
193  out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
194  if (!out)
195  return AVERROR(ENOMEM);
196 
197  av_frame_copy_props(out, in);
198  desc = av_pix_fmt_desc_get(inlink->format);
199 
200  for (i = 0; i < desc->nb_components; i++) {
201  int w = inlink->w;
202  int h = inlink->h;
203 
204  if (i == 1 || i == 2) {
205  w = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
206  h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
207  }
208 
209  if (eq->param[i].adjust)
210  eq->param[i].adjust(&eq->param[i], out->data[i], out->linesize[i],
211  in->data[i], in->linesize[i], w, h);
212  else
213  av_image_copy_plane(out->data[i], out->linesize[i],
214  in->data[i], in->linesize[i], w, h);
215  }
216 
217  av_frame_free(&in);
218  return ff_filter_frame(outlink, out);
219 }
220 static const AVFilterPad eq_inputs[] = {
221  {
222  .name = "default",
223  .type = AVMEDIA_TYPE_VIDEO,
224  .filter_frame = filter_frame,
225  },
226  { NULL }
227 };
228 
229 static const AVFilterPad eq_outputs[] = {
230  {
231  .name = "default",
232  .type = AVMEDIA_TYPE_VIDEO,
233  },
234  { NULL }
235 };
236 
237 #define OFFSET(x) offsetof(EQContext, x)
238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
239 
240 static const AVOption eq_options[] = {
241  { "contrast", "set the contrast adjustment, negative values give a negative image",
242  OFFSET(contrast), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, -2.0, 2.0, FLAGS },
243  { "brightness", "set the brightness adjustment",
244  OFFSET(brightness), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, -1.0, 1.0, FLAGS },
245  { "saturation", "set the saturation adjustment",
246  OFFSET(saturation), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.0, 3.0, FLAGS },
247  { "gamma", "set the initial gamma value",
248  OFFSET(gamma), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
249  { "gamma_r", "gamma value for red",
250  OFFSET(gamma_r), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
251  { "gamma_g", "gamma value for green",
252  OFFSET(gamma_g), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
253  { "gamma_b", "gamma value for blue",
254  OFFSET(gamma_b), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
255  { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
256  OFFSET(gamma_weight), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.0, 1.0, FLAGS },
257  { NULL }
258 };
259 
261 
263  .name = "eq",
264  .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
265  .priv_size = sizeof(EQContext),
266  .priv_class = &eq_class,
267  .inputs = eq_inputs,
268  .outputs = eq_outputs,
270  .init = initialize,
271 };