• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "libavfilter/internal.h"
31 #include "libavutil/common.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "vf_eq.h"
36 
create_lut(EQParameters * param)37 static void create_lut(EQParameters *param)
38 {
39     int i;
40     double   g  = 1.0 / param->gamma;
41     double   lw = 1.0 - param->gamma_weight;
42 
43     for (i = 0; i < 256; i++) {
44         double v = i / 255.0;
45         v = param->contrast * (v - 0.5) + 0.5 + param->brightness;
46 
47         if (v <= 0.0) {
48             param->lut[i] = 0;
49         } else {
50             v = v * lw + pow(v, g) * param->gamma_weight;
51 
52             if (v >= 1.0)
53                 param->lut[i] = 255;
54             else
55                 param->lut[i] = 256.0 * v;
56         }
57     }
58 
59     param->lut_clean = 1;
60 }
61 
apply_lut(EQParameters * param,uint8_t * dst,int dst_stride,const uint8_t * src,int src_stride,int w,int h)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)
64 {
65     int x, y;
66 
67     if (!param->lut_clean)
68         create_lut(param);
69 
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]];
73         }
74     }
75 }
76 
check_values(EQParameters * param,EQContext * eq)77 static void check_values(EQParameters *param, EQContext *eq)
78 {
79     if (param->contrast == 1.0 && param->brightness == 0.0 && param->gamma == 1.0)
80         param->adjust = NULL;
81     else if (param->gamma == 1.0 && fabs(param->contrast) < 7.9)
82         param->adjust = eq->process;
83     else
84         param->adjust = apply_lut;
85 }
86 
set_contrast(EQContext * eq)87 static void set_contrast(EQContext *eq)
88 {
89     eq->contrast = av_clipf(av_expr_eval(eq->contrast_pexpr, eq->var_values, eq), -1000.0, 1000.0);
90     eq->param[0].contrast = eq->contrast;
91     eq->param[0].lut_clean = 0;
92     check_values(&eq->param[0], eq);
93 }
94 
set_brightness(EQContext * eq)95 static void set_brightness(EQContext *eq)
96 {
97     eq->brightness = av_clipf(av_expr_eval(eq->brightness_pexpr, eq->var_values, eq), -1.0, 1.0);
98     eq->param[0].brightness = eq->brightness;
99     eq->param[0].lut_clean = 0;
100     check_values(&eq->param[0], eq);
101 }
102 
set_gamma(EQContext * eq)103 static void set_gamma(EQContext *eq)
104 {
105     int i;
106 
107     eq->gamma        = av_clipf(av_expr_eval(eq->gamma_pexpr,        eq->var_values, eq), 0.1, 10.0);
108     eq->gamma_r      = av_clipf(av_expr_eval(eq->gamma_r_pexpr,      eq->var_values, eq), 0.1, 10.0);
109     eq->gamma_g      = av_clipf(av_expr_eval(eq->gamma_g_pexpr,      eq->var_values, eq), 0.1, 10.0);
110     eq->gamma_b      = av_clipf(av_expr_eval(eq->gamma_b_pexpr,      eq->var_values, eq), 0.1, 10.0);
111     eq->gamma_weight = av_clipf(av_expr_eval(eq->gamma_weight_pexpr, eq->var_values, eq), 0.0,  1.0);
112 
113     eq->param[0].gamma = eq->gamma * eq->gamma_g;
114     eq->param[1].gamma = sqrt(eq->gamma_b / eq->gamma_g);
115     eq->param[2].gamma = sqrt(eq->gamma_r / eq->gamma_g);
116 
117     for (i = 0; i < 3; i++) {
118         eq->param[i].gamma_weight = eq->gamma_weight;
119         eq->param[i].lut_clean = 0;
120         check_values(&eq->param[i], eq);
121     }
122 }
123 
set_saturation(EQContext * eq)124 static void set_saturation(EQContext *eq)
125 {
126     int i;
127 
128     eq->saturation = av_clipf(av_expr_eval(eq->saturation_pexpr, eq->var_values, eq), 0.0, 3.0);
129 
130     for (i = 1; i < 3; i++) {
131         eq->param[i].contrast = eq->saturation;
132         eq->param[i].lut_clean = 0;
133         check_values(&eq->param[i], eq);
134     }
135 }
136 
set_expr(AVExpr ** pexpr,const char * expr,const char * option,void * log_ctx)137 static int set_expr(AVExpr **pexpr, const char *expr, const char *option, void *log_ctx)
138 {
139     int ret;
140     AVExpr *old = NULL;
141 
142     if (*pexpr)
143         old = *pexpr;
144     ret = av_expr_parse(pexpr, expr, var_names, NULL, NULL, NULL, NULL, 0, log_ctx);
145     if (ret < 0) {
146         av_log(log_ctx, AV_LOG_ERROR,
147                "Error when parsing the expression '%s' for %s\n",
148                expr, option);
149         *pexpr = old;
150         return ret;
151     }
152 
153     av_expr_free(old);
154     return 0;
155 }
156 
initialize(AVFilterContext * ctx)157 static int initialize(AVFilterContext *ctx)
158 {
159     EQContext *eq = ctx->priv;
160     int ret;
161     ff_eq_init(eq);
162 
163     if ((ret = set_expr(&eq->contrast_pexpr,     eq->contrast_expr,     "contrast",     ctx)) < 0 ||
164         (ret = set_expr(&eq->brightness_pexpr,   eq->brightness_expr,   "brightness",   ctx)) < 0 ||
165         (ret = set_expr(&eq->saturation_pexpr,   eq->saturation_expr,   "saturation",   ctx)) < 0 ||
166         (ret = set_expr(&eq->gamma_pexpr,        eq->gamma_expr,        "gamma",        ctx)) < 0 ||
167         (ret = set_expr(&eq->gamma_r_pexpr,      eq->gamma_r_expr,      "gamma_r",      ctx)) < 0 ||
168         (ret = set_expr(&eq->gamma_g_pexpr,      eq->gamma_g_expr,      "gamma_g",      ctx)) < 0 ||
169         (ret = set_expr(&eq->gamma_b_pexpr,      eq->gamma_b_expr,      "gamma_b",      ctx)) < 0 ||
170         (ret = set_expr(&eq->gamma_weight_pexpr, eq->gamma_weight_expr, "gamma_weight", ctx)) < 0 )
171         return ret;
172 
173     if (eq->eval_mode == EVAL_MODE_INIT) {
174         set_gamma(eq);
175         set_contrast(eq);
176         set_brightness(eq);
177         set_saturation(eq);
178     }
179 
180     return 0;
181 }
182 
uninit(AVFilterContext * ctx)183 static av_cold void uninit(AVFilterContext *ctx)
184 {
185     EQContext *eq = ctx->priv;
186 
187     av_expr_free(eq->contrast_pexpr);     eq->contrast_pexpr     = NULL;
188     av_expr_free(eq->brightness_pexpr);   eq->brightness_pexpr   = NULL;
189     av_expr_free(eq->saturation_pexpr);   eq->saturation_pexpr   = NULL;
190     av_expr_free(eq->gamma_pexpr);        eq->gamma_pexpr        = NULL;
191     av_expr_free(eq->gamma_weight_pexpr); eq->gamma_weight_pexpr = NULL;
192     av_expr_free(eq->gamma_r_pexpr);      eq->gamma_r_pexpr      = NULL;
193     av_expr_free(eq->gamma_g_pexpr);      eq->gamma_g_pexpr      = NULL;
194     av_expr_free(eq->gamma_b_pexpr);      eq->gamma_b_pexpr      = NULL;
195 }
196 
config_props(AVFilterLink * inlink)197 static int config_props(AVFilterLink *inlink)
198 {
199     EQContext *eq = inlink->dst->priv;
200 
201     eq->var_values[VAR_N] = 0;
202     eq->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
203         NAN : av_q2d(inlink->frame_rate);
204 
205     return 0;
206 }
207 
208 static const enum AVPixelFormat pixel_fmts_eq[] = {
209     AV_PIX_FMT_GRAY8,
210     AV_PIX_FMT_YUV410P,
211     AV_PIX_FMT_YUV411P,
212     AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P,
213     AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P,
214     AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P,
215     AV_PIX_FMT_NONE
216 };
217 
filter_frame(AVFilterLink * inlink,AVFrame * in)218 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
219 {
220     AVFilterContext *ctx = inlink->dst;
221     AVFilterLink *outlink = inlink->dst->outputs[0];
222     EQContext *eq = ctx->priv;
223     AVFrame *out;
224     int64_t pos = in->pkt_pos;
225     const AVPixFmtDescriptor *desc;
226     int i;
227 
228     out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
229     if (!out) {
230         av_frame_free(&in);
231         return AVERROR(ENOMEM);
232     }
233 
234     av_frame_copy_props(out, in);
235     desc = av_pix_fmt_desc_get(inlink->format);
236 
237     eq->var_values[VAR_N]   = inlink->frame_count_out;
238     eq->var_values[VAR_POS] = pos == -1 ? NAN : pos;
239     eq->var_values[VAR_T]   = TS2T(in->pts, inlink->time_base);
240 
241     if (eq->eval_mode == EVAL_MODE_FRAME) {
242         set_gamma(eq);
243         set_contrast(eq);
244         set_brightness(eq);
245         set_saturation(eq);
246     }
247 
248     for (i = 0; i < desc->nb_components; i++) {
249         int w = inlink->w;
250         int h = inlink->h;
251 
252         if (i == 1 || i == 2) {
253             w = AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
254             h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
255         }
256 
257         if (i == 3 || !eq->param[i].adjust)
258             av_image_copy_plane(out->data[i], out->linesize[i],
259                                 in->data[i], in->linesize[i], w, h);
260 
261         else
262             eq->param[i].adjust(&eq->param[i], out->data[i], out->linesize[i],
263                                  in->data[i], in->linesize[i], w, h);
264     }
265 
266     av_frame_free(&in);
267     return ff_filter_frame(outlink, out);
268 }
269 
set_param(AVExpr ** pexpr,const char * args,const char * cmd,void (* set_fn)(EQContext * eq),AVFilterContext * ctx)270 static inline int set_param(AVExpr **pexpr, const char *args, const char *cmd,
271                             void (*set_fn)(EQContext *eq), AVFilterContext *ctx)
272 {
273     EQContext *eq = ctx->priv;
274     int ret;
275     if ((ret = set_expr(pexpr, args, cmd, ctx)) < 0)
276         return ret;
277     if (eq->eval_mode == EVAL_MODE_INIT)
278         set_fn(eq);
279     return 0;
280 }
281 
process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)282 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
283                            char *res, int res_len, int flags)
284 {
285     EQContext *eq = ctx->priv;
286 
287 #define SET_PARAM(param_name, set_fn_name)                              \
288     if (!strcmp(cmd, #param_name)) return set_param(&eq->param_name##_pexpr, args, cmd, set_##set_fn_name, ctx);
289 
290          SET_PARAM(contrast, contrast)
291     else SET_PARAM(brightness, brightness)
292     else SET_PARAM(saturation, saturation)
293     else SET_PARAM(gamma, gamma)
294     else SET_PARAM(gamma_r, gamma)
295     else SET_PARAM(gamma_g, gamma)
296     else SET_PARAM(gamma_b, gamma)
297     else SET_PARAM(gamma_weight, gamma)
298     else return AVERROR(ENOSYS);
299 }
300 
301 static const AVFilterPad eq_inputs[] = {
302     {
303         .name = "default",
304         .type = AVMEDIA_TYPE_VIDEO,
305         .filter_frame = filter_frame,
306         .config_props = config_props,
307     },
308 };
309 
310 static const AVFilterPad eq_outputs[] = {
311     {
312         .name = "default",
313         .type = AVMEDIA_TYPE_VIDEO,
314     },
315 };
316 
317 #define OFFSET(x) offsetof(EQContext, x)
318 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
319 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
320 static const AVOption eq_options[] = {
321     { "contrast",     "set the contrast adjustment, negative values give a negative image",
322         OFFSET(contrast_expr),     AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
323     { "brightness",   "set the brightness adjustment",
324         OFFSET(brightness_expr),   AV_OPT_TYPE_STRING, {.str = "0.0"}, 0, 0, TFLAGS },
325     { "saturation",   "set the saturation adjustment",
326         OFFSET(saturation_expr),   AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
327     { "gamma",        "set the initial gamma value",
328         OFFSET(gamma_expr),        AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
329     { "gamma_r",      "gamma value for red",
330         OFFSET(gamma_r_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
331     { "gamma_g",      "gamma value for green",
332         OFFSET(gamma_g_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
333     { "gamma_b",      "gamma value for blue",
334         OFFSET(gamma_b_expr),      AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
335     { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
336         OFFSET(gamma_weight_expr), AV_OPT_TYPE_STRING, {.str = "1.0"}, 0, 0, TFLAGS },
337     { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
338          { "init",  "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT},  .flags = FLAGS, .unit = "eval" },
339          { "frame", "eval expressions per-frame",                  0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
340     { NULL }
341 };
342 
343 AVFILTER_DEFINE_CLASS(eq);
344 
345 const AVFilter ff_vf_eq = {
346     .name            = "eq",
347     .description     = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
348     .priv_size       = sizeof(EQContext),
349     .priv_class      = &eq_class,
350     FILTER_INPUTS(eq_inputs),
351     FILTER_OUTPUTS(eq_outputs),
352     FILTER_PIXFMTS_ARRAY(pixel_fmts_eq),
353     .process_command = process_command,
354     .init            = initialize,
355     .uninit          = uninit,
356     .flags           = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
357 };
358