You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
12KB

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