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.

272 lines
7.6KB

  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->param[0].contrast = eq->contrast;
  97. eq->param[0].lut_clean = 0;
  98. check_values(&eq->param[0], eq);
  99. }
  100. static void set_brightness(EQContext *eq)
  101. {
  102. eq->param[0].brightness = eq->brightness;
  103. eq->param[0].lut_clean = 0;
  104. check_values(&eq->param[0], eq);
  105. }
  106. static void set_gamma(EQContext *eq)
  107. {
  108. int i;
  109. eq->param[0].gamma = eq->gamma * eq->gamma_g;
  110. eq->param[1].gamma = sqrt(eq->gamma_b / eq->gamma_g);
  111. eq->param[2].gamma = sqrt(eq->gamma_r / eq->gamma_g);
  112. for (i = 0; i < 3; i++) {
  113. eq->param[i].gamma_weight = eq->gamma_weight;
  114. eq->param[i].lut_clean = 0;
  115. check_values(&eq->param[i], eq);
  116. }
  117. }
  118. static void set_saturation(EQContext *eq)
  119. {
  120. int i;
  121. for (i = 1; i < 3; i++) {
  122. eq->param[i].contrast = eq->saturation;
  123. eq->param[i].lut_clean = 0;
  124. check_values(&eq->param[i], eq);
  125. }
  126. }
  127. static int initialize(AVFilterContext *ctx)
  128. {
  129. EQContext *eq = ctx->priv;
  130. eq->process = process_c;
  131. if (ARCH_X86)
  132. ff_eq_init_x86(eq);
  133. set_gamma(eq);
  134. set_contrast(eq);
  135. set_brightness(eq);
  136. set_saturation(eq);
  137. return 0;
  138. }
  139. static int query_formats(AVFilterContext *ctx)
  140. {
  141. static const enum AVPixelFormat pixel_fmts_eq[] = {
  142. AV_PIX_FMT_GRAY8,
  143. AV_PIX_FMT_YUV410P,
  144. AV_PIX_FMT_YUV411P,
  145. AV_PIX_FMT_YUV420P,
  146. AV_PIX_FMT_YUV422P,
  147. AV_PIX_FMT_YUV444P,
  148. AV_PIX_FMT_NONE
  149. };
  150. ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
  151. return 0;
  152. }
  153. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  154. {
  155. AVFilterContext *ctx = inlink->dst;
  156. AVFilterLink *outlink = inlink->dst->outputs[0];
  157. EQContext *eq = ctx->priv;
  158. AVFrame *out;
  159. const AVPixFmtDescriptor *desc;
  160. int i;
  161. out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
  162. if (!out)
  163. return AVERROR(ENOMEM);
  164. av_frame_copy_props(out, in);
  165. desc = av_pix_fmt_desc_get(inlink->format);
  166. for (i = 0; i < desc->nb_components; i++) {
  167. int w = inlink->w;
  168. int h = inlink->h;
  169. if (i == 1 || i == 2) {
  170. w = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
  171. h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  172. }
  173. if (eq->param[i].adjust)
  174. eq->param[i].adjust(&eq->param[i], out->data[i], out->linesize[i],
  175. in->data[i], in->linesize[i], w, h);
  176. else
  177. av_image_copy_plane(out->data[i], out->linesize[i],
  178. in->data[i], in->linesize[i], w, h);
  179. }
  180. av_frame_free(&in);
  181. return ff_filter_frame(outlink, out);
  182. }
  183. static const AVFilterPad eq_inputs[] = {
  184. {
  185. .name = "default",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .filter_frame = filter_frame,
  188. },
  189. { NULL }
  190. };
  191. static const AVFilterPad eq_outputs[] = {
  192. {
  193. .name = "default",
  194. .type = AVMEDIA_TYPE_VIDEO,
  195. },
  196. { NULL }
  197. };
  198. #define OFFSET(x) offsetof(EQContext, x)
  199. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  200. static const AVOption eq_options[] = {
  201. { "contrast", "set the contrast adjustment, negative values give a negative image",
  202. OFFSET(contrast), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, -2.0, 2.0, FLAGS },
  203. { "brightness", "set the brightness adjustment",
  204. OFFSET(brightness), AV_OPT_TYPE_DOUBLE, {.dbl = 0.0}, -1.0, 1.0, FLAGS },
  205. { "saturation", "set the saturation adjustment",
  206. OFFSET(saturation), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.0, 3.0, FLAGS },
  207. { "gamma", "set the initial gamma value",
  208. OFFSET(gamma), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
  209. { "gamma_r", "gamma value for red",
  210. OFFSET(gamma_r), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
  211. { "gamma_g", "gamma value for green",
  212. OFFSET(gamma_g), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
  213. { "gamma_b", "gamma value for blue",
  214. OFFSET(gamma_b), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.1, 10.0, FLAGS },
  215. { "gamma_weight", "set the gamma weight which reduces the effect of gamma on bright areas",
  216. OFFSET(gamma_weight), AV_OPT_TYPE_DOUBLE, {.dbl = 1.0}, 0.0, 1.0, FLAGS },
  217. { NULL }
  218. };
  219. AVFILTER_DEFINE_CLASS(eq);
  220. AVFilter ff_vf_eq = {
  221. .name = "eq",
  222. .description = NULL_IF_CONFIG_SMALL("Adjust brightness, contrast, gamma, and saturation."),
  223. .priv_size = sizeof(EQContext),
  224. .priv_class = &eq_class,
  225. .inputs = eq_inputs,
  226. .outputs = eq_outputs,
  227. .query_formats = query_formats,
  228. .init = initialize,
  229. };