2 * Original MPlayer filters by Richard Felker, Hampa Hug, Daniel Moreno,
3 * and Michael Niedermeyer.
5 * Copyright (c) 2014 James Darnley <james.darnley@gmail.com>
6 * Copyright (c) 2015 Arwa Arif <arwaarif1994@gmail.com>
8 * This file is part of FFmpeg.
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.
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.
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.
27 * very simple video equalizer
30 #include "libavfilter/internal.h"
31 #include "libavutil/common.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
37 static void create_lut(EQParameters *param)
40 double g = 1.0 / param->gamma;
41 double lw = 1.0 - param->gamma_weight;
43 for (i = 0; i < 256; i++) {
45 v = param->contrast * (v - 0.5) + 0.5 + param->brightness;
50 v = v * lw + pow(v, g) * param->gamma_weight;
55 param->lut[i] = 256.0 * v;
62 static void apply_lut(EQParameters *param, uint8_t *dst, int dst_stride,
63 const uint8_t *src, int src_stride, int w, int h)
67 if (!param->lut_clean)
70 for (y = 0; y < h; y++) {
71 for (x = 0; x < w; x++) {
72 dst[y * dst_stride + x] = param->lut[src[y * src_stride + x]];
77 static void process_c(EQParameters *param, uint8_t *dst, int dst_stride,
78 const uint8_t *src, int src_stride, int w, int h)
82 int contrast = (int) (param->contrast * 256 * 16);
83 int brightness = ((int) (100.0 * param->brightness + 100.0) * 511) / 200 - 128 - contrast / 32;
85 for (y = 0; y < h; y++) {
86 for (x = 0; x < w; x++) {
87 pel = ((src[y * src_stride + x] * contrast) >> 12) + brightness;
92 dst[y * dst_stride + x] = pel;
97 static void check_values(EQParameters *param, EQContext *eq)
99 if (param->contrast == 1.0 && param->brightness == 0.0 && param->gamma == 1.0)
100 param->adjust = NULL;
101 else if (param->gamma == 1.0 && fabs(param->contrast) < 7.9)
102 param->adjust = eq->process;
104 param->adjust = apply_lut;
107 static void set_contrast(EQContext *eq)
109 eq->contrast = av_clipf(av_expr_eval(eq->contrast_pexpr, eq->var_values, eq), -1000.0, 1000.0);
110 eq->param[0].contrast = eq->contrast;
111 eq->param[0].lut_clean = 0;
112 check_values(&eq->param[0], eq);
115 static void set_brightness(EQContext *eq)
117 eq->brightness = av_clipf(av_expr_eval(eq->brightness_pexpr, eq->var_values, eq), -1.0, 1.0);
118 eq->param[0].brightness = eq->brightness;
119 eq->param[0].lut_clean = 0;
120 check_values(&eq->param[0], eq);
123 static void set_gamma(EQContext *eq)
127 eq->gamma = av_clipf(av_expr_eval(eq->gamma_pexpr, eq->var_values, eq), 0.1, 10.0);
128 eq->gamma_r = av_clipf(av_expr_eval(eq->gamma_r_pexpr, eq->var_values, eq), 0.1, 10.0);
129 eq->gamma_g = av_clipf(av_expr_eval(eq->gamma_g_pexpr, eq->var_values, eq), 0.1, 10.0);
130 eq->gamma_b = av_clipf(av_expr_eval(eq->gamma_b_pexpr, eq->var_values, eq), 0.1, 10.0);
131 eq->gamma_weight = av_clipf(av_expr_eval(eq->gamma_weight_pexpr, eq->var_values, eq), 0.0, 1.0);
133 eq->param[0].gamma = eq->gamma * eq->gamma_g;
134 eq->param[1].gamma = sqrt(eq->gamma_b / eq->gamma_g);
135 eq->param[2].gamma = sqrt(eq->gamma_r / eq->gamma_g);
137 for (i = 0; i < 3; i++) {
138 eq->param[i].gamma_weight = eq->gamma_weight;
139 eq->param[i].lut_clean = 0;
140 check_values(&eq->param[i], eq);
144 static void set_saturation(EQContext *eq)
148 eq->saturation = av_clipf(av_expr_eval(eq->saturation_pexpr, eq->var_values, eq), 0.0, 3.0);
150 for (i = 1; i < 3; i++) {
151 eq->param[i].contrast = eq->saturation;
152 eq->param[i].lut_clean = 0;
153 check_values(&eq->param[i], eq);
157 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
164 ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
166 av_log(log_ctx, AV_LOG_ERROR,
167 "Error when parsing the expression '%s' for %s\n",
177 void ff_eq_init(EQContext *eq)
179 eq->process = process_c;
184 static int initialize(AVFilterContext *ctx)
186 EQContext *eq = ctx->priv;
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 )
200 if (eq->eval_mode == EVAL_MODE_INIT) {
210 static av_cold void uninit(AVFilterContext *ctx)
212 EQContext *eq = ctx->priv;
214 av_expr_free(eq->contrast_pexpr); eq->contrast_pexpr = NULL;
215 av_expr_free(eq->brightness_pexpr); eq->brightness_pexpr = NULL;
216 av_expr_free(eq->saturation_pexpr); eq->saturation_pexpr = NULL;
217 av_expr_free(eq->gamma_pexpr); eq->gamma_pexpr = NULL;
218 av_expr_free(eq->gamma_weight_pexpr); eq->gamma_weight_pexpr = NULL;
219 av_expr_free(eq->gamma_r_pexpr); eq->gamma_r_pexpr = NULL;
220 av_expr_free(eq->gamma_g_pexpr); eq->gamma_g_pexpr = NULL;
221 av_expr_free(eq->gamma_b_pexpr); eq->gamma_b_pexpr = NULL;
224 static int config_props(AVFilterLink *inlink)
226 EQContext *eq = inlink->dst->priv;
228 eq->var_values[VAR_N] = 0;
229 eq->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
230 NAN : av_q2d(inlink->frame_rate);
235 static int query_formats(AVFilterContext *ctx)
237 static const enum AVPixelFormat pixel_fmts_eq[] = {
246 AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_eq);
248 return AVERROR(ENOMEM);
249 return ff_set_common_formats(ctx, fmts_list);
252 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
254 AVFilterContext *ctx = inlink->dst;
255 AVFilterLink *outlink = inlink->dst->outputs[0];
256 EQContext *eq = ctx->priv;
258 int64_t pos = in->pkt_pos;
259 const AVPixFmtDescriptor *desc;
262 out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
265 return AVERROR(ENOMEM);
268 av_frame_copy_props(out, in);
269 desc = av_pix_fmt_desc_get(inlink->format);
271 eq->var_values[VAR_N] = inlink->frame_count_out;
272 eq->var_values[VAR_POS] = pos == -1 ? NAN : pos;
273 eq->var_values[VAR_T] = TS2T(in->pts, inlink->time_base);
275 if (eq->eval_mode == EVAL_MODE_FRAME) {
282 for (i = 0; i < desc->nb_components; i++) {
286 if (i == 1 || i == 2) {
287 w = AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
288 h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
291 if (eq->param[i].adjust)
292 eq->param[i].adjust(&eq->param[i], out->data[i], out->linesize[i],
293 in->data[i], in->linesize[i], w, h);
295 av_image_copy_plane(out->data[i], out->linesize[i],
296 in->data[i], in->linesize[i], w, h);
300 return ff_filter_frame(outlink, out);
303 static inline int set_param(AVExpr **pexpr, const char *args, const char *cmd,
304 void (*set_fn)(EQContext *eq), AVFilterContext *ctx)
306 EQContext *eq = ctx->priv;
308 if ((ret = set_expr(pexpr, args, cmd, ctx)) < 0)
310 if (eq->eval_mode == EVAL_MODE_INIT)
315 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
316 char *res, int res_len, int flags)
318 EQContext *eq = ctx->priv;
320 #define SET_PARAM(param_name, set_fn_name) \
321 if (!strcmp(cmd, #param_name)) return set_param(&eq->param_name##_pexpr, args, cmd, set_##set_fn_name, ctx);
323 SET_PARAM(contrast, contrast)
324 else SET_PARAM(brightness, brightness)
325 else SET_PARAM(saturation, saturation)
326 else SET_PARAM(gamma, gamma)
327 else SET_PARAM(gamma_r, gamma)
328 else SET_PARAM(gamma_g, gamma)
329 else SET_PARAM(gamma_b, gamma)
330 else SET_PARAM(gamma_weight, gamma)
331 else return AVERROR(ENOSYS);
334 static const AVFilterPad eq_inputs[] = {
337 .type = AVMEDIA_TYPE_VIDEO,
338 .filter_frame = filter_frame,
339 .config_props = config_props,
344 static const AVFilterPad eq_outputs[] = {
347 .type = AVMEDIA_TYPE_VIDEO,
352 #define OFFSET(x) offsetof(EQContext, x)
353 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
354 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
355 static const AVOption eq_options[] = {
356 { "contrast", "set the contrast adjustment, negative values give a negative image",
357 OFFSET(contrast_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
358 { "brightness", "set the brightness adjustment",
359 OFFSET(brightness_expr), AV_OPT_TYPE_STRING, {.str = "0.0"}, 0, 0, TFLAGS },
360 { "saturation", "set the saturation adjustment",
361 OFFSET(saturation_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
362 { "gamma", "set the initial gamma value",
363 OFFSET(gamma_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
364 { "gamma_r", "gamma value for red",
365 OFFSET(gamma_r_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
366 { "gamma_g", "gamma value for green",
367 OFFSET(gamma_g_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
368 { "gamma_b", "gamma value for blue",
369 OFFSET(gamma_b_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
370 { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
371 OFFSET(gamma_weight_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
372 { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
373 { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
374 { "frame", "eval expressions per-frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
378 AVFILTER_DEFINE_CLASS(eq);
380 AVFilter ff_vf_eq = {
382 .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
383 .priv_size = sizeof(EQContext),
384 .priv_class = &eq_class,
386 .outputs = eq_outputs,
387 .process_command = process_command,
388 .query_formats = query_formats,
391 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,