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->param[0].contrast = eq->contrast;
115 eq->param[0].lut_clean = 0;
116 check_values(&eq->param[0], eq);
119 static void set_brightness(EQContext *eq)
121 eq->param[0].brightness = eq->brightness;
122 eq->param[0].lut_clean = 0;
123 check_values(&eq->param[0], eq);
126 static void set_gamma(EQContext *eq)
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);
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);
140 static void set_saturation(EQContext *eq)
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);
150 static int initialize(AVFilterContext *ctx)
152 EQContext *eq = ctx->priv;
154 eq->process = process_c;
167 static int query_formats(AVFilterContext *ctx)
169 static const enum AVPixelFormat pixel_fmts_eq[] = {
179 ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
184 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
186 AVFilterContext *ctx = inlink->dst;
187 AVFilterLink *outlink = inlink->dst->outputs[0];
188 EQContext *eq = ctx->priv;
190 const AVPixFmtDescriptor *desc;
193 out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
195 return AVERROR(ENOMEM);
197 av_frame_copy_props(out, in);
198 desc = av_pix_fmt_desc_get(inlink->format);
200 for (i = 0; i < desc->nb_components; i++) {
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);
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);
213 av_image_copy_plane(out->data[i], out->linesize[i],
214 in->data[i], in->linesize[i], w, h);
218 return ff_filter_frame(outlink, out);
220 static const AVFilterPad eq_inputs[] = {
223 .type = AVMEDIA_TYPE_VIDEO,
224 .filter_frame = filter_frame,
229 static const AVFilterPad eq_outputs[] = {
232 .type = AVMEDIA_TYPE_VIDEO,
237 #define OFFSET(x) offsetof(EQContext, x)
238 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
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 },
260 AVFILTER_DEFINE_CLASS(eq);
262 AVFilter ff_vf_eq = {
264 .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
265 .priv_size = sizeof(EQContext),
266 .priv_class = &eq_class,
268 .outputs = eq_outputs,
269 .query_formats = query_formats,