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
32 * - Add support to process_command
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"
42 static void create_lut(EQParameters *param)
45 double g = 1.0 / param->gamma;
46 double lw = 1.0 - param->gamma_weight;
48 for (i = 0; i < 256; i++) {
50 v = param->contrast * (v - 0.5) + 0.5 + param->brightness;
55 v = v * lw + pow(v, g) * param->gamma_weight;
60 param->lut[i] = 256.0 * v;
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)
72 if (!param->lut_clean)
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]];
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)
87 int contrast = (int) (param->contrast * 256 * 16);
88 int brightness = ((int) (100.0 * param->brightness + 100.0) * 511) / 200 - 128 - contrast / 32;
90 for (y = 0; y < h; y++) {
91 for (x = 0; x < w; x++) {
92 pel = ((src[y * src_stride + x] * contrast) >> 12) + brightness;
97 dst[y * dst_stride + x] = pel;
102 static void check_values(EQParameters *param, EQContext *eq)
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;
109 param->adjust = apply_lut;
112 static void set_contrast(EQContext *eq)
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);
120 static void set_brightness(EQContext *eq)
122 eq->var_values[VAR_BRIGHTNESS] = av_clipf(av_expr_eval(eq->brightness_pexpr, eq->var_values, eq), -1.0, 1.0);
123 eq->param[0].brightness = eq->var_values[VAR_BRIGHTNESS];
124 eq->param[0].lut_clean = 0;
125 check_values(&eq->param[0], eq);
128 static void set_gamma(EQContext *eq)
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);
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]);
142 for (i = 0; i < 3; i++) {
143 eq->param[i].gamma_weight = eq->var_values[VAR_GAMMA_WEIGHT];
144 eq->param[i].lut_clean = 0;
145 check_values(&eq->param[i], eq);
149 static void set_saturation(EQContext *eq)
153 eq->var_values[VAR_SATURATION] = av_clipf(av_expr_eval(eq->saturation_pexpr, eq->var_values, eq), 0.0, 3.0);
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);
162 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
169 ret = av_expr_parse(pexpr, expr, var_names,
170 NULL, NULL, NULL, NULL, 0, log_ctx);
172 av_log(log_ctx, AV_LOG_ERROR,
173 "Error when evaluating the expression '%s' for %s\n",
183 static int initialize(AVFilterContext *ctx)
185 EQContext *eq = ctx->priv;
188 eq->process = process_c;
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 )
211 static void uninit(AVFilterContext *ctx)
213 EQContext *eq = ctx->priv;
215 av_expr_free(eq->contrast_pexpr); eq->contrast_pexpr = NULL;
216 av_expr_free(eq->brightness_pexpr); eq->brightness_pexpr = NULL;
217 av_expr_free(eq->saturation_pexpr); eq->saturation_pexpr = NULL;
218 av_expr_free(eq->gamma_pexpr); eq->gamma_pexpr = NULL;
219 av_expr_free(eq->gamma_weight_pexpr); eq->gamma_weight_pexpr = NULL;
220 av_expr_free(eq->gamma_r_pexpr); eq->gamma_r_pexpr = NULL;
221 av_expr_free(eq->gamma_g_pexpr); eq->gamma_g_pexpr = NULL;
222 av_expr_free(eq->gamma_b_pexpr); eq->gamma_b_pexpr = NULL;
225 static int query_formats(AVFilterContext *ctx)
227 static const enum AVPixelFormat pixel_fmts_eq[] = {
237 ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
242 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
244 AVFilterContext *ctx = inlink->dst;
245 AVFilterLink *outlink = inlink->dst->outputs[0];
246 EQContext *eq = ctx->priv;
248 const AVPixFmtDescriptor *desc;
251 out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
253 return AVERROR(ENOMEM);
255 av_frame_copy_props(out, in);
256 desc = av_pix_fmt_desc_get(inlink->format);
258 for (i = 0; i < desc->nb_components; i++) {
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);
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);
271 av_image_copy_plane(out->data[i], out->linesize[i],
272 in->data[i], in->linesize[i], w, h);
276 return ff_filter_frame(outlink, out);
279 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
280 char *res, int res_len, int flags)
282 EQContext *eq = ctx->priv;
285 if (!strcmp(cmd, "contrast")) {
286 ret = set_expr(&eq->contrast_pexpr, args, cmd, ctx);
290 else if (!strcmp(cmd, "brightness")) {
291 ret = set_expr(&eq->brightness_pexpr, args, cmd, ctx);
295 else if (!strcmp(cmd, "saturation")) {
296 ret = set_expr(&eq->saturation_pexpr, args, cmd, ctx);
300 else if (!strcmp(cmd, "gamma")) {
301 ret = set_expr(&eq->gamma_pexpr, args, cmd, ctx);
305 else if (!strcmp(cmd, "gamma_r")) {
306 ret = set_expr(&eq->gamma_r_pexpr, args, cmd, ctx);
310 else if (!strcmp(cmd, "gamma_g")) {
311 ret = set_expr(&eq->gamma_g_pexpr, args, cmd, ctx);
315 else if (!strcmp(cmd, "gamma_b")) {
316 ret = set_expr(&eq->gamma_b_pexpr, args, cmd, ctx);
320 else if (!strcmp(cmd, "gamma_weight")) {
321 ret = set_expr(&eq->gamma_weight_pexpr, args, cmd, ctx);
326 return AVERROR(ENOSYS);
329 static const AVFilterPad eq_inputs[] = {
332 .type = AVMEDIA_TYPE_VIDEO,
333 .filter_frame = filter_frame,
338 static const AVFilterPad eq_outputs[] = {
341 .type = AVMEDIA_TYPE_VIDEO,
346 #define OFFSET(x) offsetof(EQContext, x)
347 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
369 AVFILTER_DEFINE_CLASS(eq);
371 AVFilter ff_vf_eq = {
373 .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
374 .priv_size = sizeof(EQContext),
375 .priv_class = &eq_class,
377 .outputs = eq_outputs,
378 .process_command = process_command,
379 .query_formats = query_formats,