vf_eq.c 13 KB

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