vf_fftfilt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2015 Arwa Arif <arwaarif1994@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation; either version 2.1 of the License,
  9. * or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * FFT domain filtering.
  23. */
  24. #include "libavfilter/internal.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/opt.h"
  28. #include "libavutil/pixdesc.h"
  29. #include "libavcodec/avfft.h"
  30. #include "libavutil/eval.h"
  31. #define MAX_PLANES 4
  32. typedef struct {
  33. const AVClass *class;
  34. RDFTContext *rdft;
  35. int rdft_hbits[MAX_PLANES];
  36. int rdft_vbits[MAX_PLANES];
  37. size_t rdft_hlen[MAX_PLANES];
  38. size_t rdft_vlen[MAX_PLANES];
  39. FFTSample *rdft_hdata[MAX_PLANES];
  40. FFTSample *rdft_vdata[MAX_PLANES];
  41. int dc[MAX_PLANES];
  42. char *weight_str[MAX_PLANES];
  43. AVExpr *weight_expr[MAX_PLANES];
  44. double *weight[MAX_PLANES];
  45. } FFTFILTContext;
  46. static const char *const var_names[] = { "X", "Y", "W", "H", NULL };
  47. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_VARS_NB };
  48. enum { Y = 0, U, V };
  49. #define OFFSET(x) offsetof(FFTFILTContext, x)
  50. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  51. static const AVOption fftfilt_options[] = {
  52. { "dc_Y", "adjust gain in Y plane", OFFSET(dc[Y]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  53. { "dc_U", "adjust gain in U plane", OFFSET(dc[U]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  54. { "dc_V", "adjust gain in V plane", OFFSET(dc[V]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  55. { "weight_Y", "set luminance expression in Y plane", OFFSET(weight_str[Y]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  56. { "weight_U", "set chrominance expression in U plane", OFFSET(weight_str[U]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  57. { "weight_V", "set chrominance expression in V plane", OFFSET(weight_str[V]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  58. {NULL},
  59. };
  60. AVFILTER_DEFINE_CLASS(fftfilt);
  61. static inline double lum(void *priv, double x, double y, int plane)
  62. {
  63. FFTFILTContext *s = priv;
  64. return s->rdft_vdata[plane][(int)x * s->rdft_vlen[plane] + (int)y];
  65. }
  66. static double weight_Y(void *priv, double x, double y) { return lum(priv, x, y, Y); }
  67. static double weight_U(void *priv, double x, double y) { return lum(priv, x, y, U); }
  68. static double weight_V(void *priv, double x, double y) { return lum(priv, x, y, V); }
  69. static void copy_rev (FFTSample *dest, int w, int w2)
  70. {
  71. int i;
  72. for (i = w; i < w + (w2-w)/2; i++)
  73. dest[i] = dest[2*w - i - 1];
  74. for (; i < w2; i++)
  75. dest[i] = dest[w2 - i];
  76. }
  77. /*Horizontal pass - RDFT*/
  78. static void rdft_horizontal(FFTFILTContext *s, AVFrame *in, int w, int h, int plane)
  79. {
  80. int i, j;
  81. s->rdft = av_rdft_init(s->rdft_hbits[plane], DFT_R2C);
  82. for (i = 0; i < h; i++) {
  83. for (j = 0; j < w; j++)
  84. s->rdft_hdata[plane][i * s->rdft_hlen[plane] + j] = *(in->data[plane] + in->linesize[plane] * i + j);
  85. copy_rev(s->rdft_hdata[plane] + i * s->rdft_hlen[plane], w, s->rdft_hlen[plane]);
  86. }
  87. for (i = 0; i < h; i++)
  88. av_rdft_calc(s->rdft, s->rdft_hdata[plane] + i * s->rdft_hlen[plane]);
  89. av_rdft_end(s->rdft);
  90. }
  91. /*Vertical pass - RDFT*/
  92. static void rdft_vertical(FFTFILTContext *s, int h, int plane)
  93. {
  94. int i, j;
  95. s->rdft = av_rdft_init(s->rdft_vbits[plane], DFT_R2C);
  96. for (i = 0; i < s->rdft_hlen[plane]; i++) {
  97. for (j = 0; j < h; j++)
  98. s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j] =
  99. s->rdft_hdata[plane][j * s->rdft_hlen[plane] + i];
  100. copy_rev(s->rdft_vdata[plane] + i * s->rdft_vlen[plane], h, s->rdft_vlen[plane]);
  101. }
  102. for (i = 0; i < s->rdft_hlen[plane]; i++)
  103. av_rdft_calc(s->rdft, s->rdft_vdata[plane] + i * s->rdft_vlen[plane]);
  104. av_rdft_end(s->rdft);
  105. }
  106. /*Vertical pass - IRDFT*/
  107. static void irdft_vertical(FFTFILTContext *s, int h, int plane)
  108. {
  109. int i, j;
  110. s->rdft = av_rdft_init(s->rdft_vbits[plane], IDFT_C2R);
  111. for (i = 0; i < s->rdft_hlen[plane]; i++)
  112. av_rdft_calc(s->rdft, s->rdft_vdata[plane] + i * s->rdft_vlen[plane]);
  113. for (i = 0; i < s->rdft_hlen[plane]; i++)
  114. for (j = 0; j < h; j++)
  115. s->rdft_hdata[plane][j * s->rdft_hlen[plane] + i] =
  116. s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j];
  117. av_rdft_end(s->rdft);
  118. }
  119. /*Horizontal pass - IRDFT*/
  120. static void irdft_horizontal(FFTFILTContext *s, AVFrame *out, int w, int h, int plane)
  121. {
  122. int i, j;
  123. s->rdft = av_rdft_init(s->rdft_hbits[plane], IDFT_C2R);
  124. for (i = 0; i < h; i++)
  125. av_rdft_calc(s->rdft, s->rdft_hdata[plane] + i * s->rdft_hlen[plane]);
  126. for (i = 0; i < h; i++)
  127. for (j = 0; j < w; j++)
  128. *(out->data[plane] + out->linesize[plane] * i + j) = av_clip(s->rdft_hdata[plane][i
  129. *s->rdft_hlen[plane] + j] * 4 /
  130. (s->rdft_hlen[plane] *
  131. s->rdft_vlen[plane]), 0, 255);
  132. av_rdft_end(s->rdft);
  133. }
  134. static av_cold int initialize(AVFilterContext *ctx)
  135. {
  136. FFTFILTContext *s = ctx->priv;
  137. int ret = 0, plane;
  138. if (!s->dc[U] && !s->dc[V]) {
  139. s->dc[U] = s->dc[Y];
  140. s->dc[V] = s->dc[Y];
  141. } else {
  142. if (!s->dc[U]) s->dc[U] = s->dc[V];
  143. if (!s->dc[V]) s->dc[V] = s->dc[U];
  144. }
  145. if (!s->weight_str[U] && !s->weight_str[V]) {
  146. s->weight_str[U] = av_strdup(s->weight_str[Y]);
  147. s->weight_str[V] = av_strdup(s->weight_str[Y]);
  148. } else {
  149. if (!s->weight_str[U]) s->weight_str[U] = av_strdup(s->weight_str[V]);
  150. if (!s->weight_str[V]) s->weight_str[V] = av_strdup(s->weight_str[U]);
  151. }
  152. for (plane = 0; plane < 3; plane++) {
  153. static double (*p[])(void *, double, double) = { weight_Y, weight_U, weight_V };
  154. const char *const func2_names[] = {"weight_Y", "weight_U", "weight_V", NULL };
  155. double (*func2[])(void *, double, double) = { weight_Y, weight_U, weight_V, p[plane], NULL };
  156. ret = av_expr_parse(&s->weight_expr[plane], s->weight_str[plane], var_names,
  157. NULL, NULL, func2_names, func2, 0, ctx);
  158. if (ret < 0)
  159. break;
  160. }
  161. return ret;
  162. }
  163. static int config_props(AVFilterLink *inlink)
  164. {
  165. FFTFILTContext *s = inlink->dst->priv;
  166. const AVPixFmtDescriptor *desc;
  167. int rdft_hbits, rdft_vbits, i, j, plane;
  168. double values[VAR_VARS_NB];
  169. desc = av_pix_fmt_desc_get(inlink->format);
  170. for (i = 0; i < desc->nb_components; i++) {
  171. int w = inlink->w;
  172. int h = inlink->h;
  173. /* RDFT - Array initialization for Horizontal pass*/
  174. for (rdft_hbits = 1; 1 << rdft_hbits < w*10/9; rdft_hbits++);
  175. s->rdft_hbits[i] = rdft_hbits;
  176. s->rdft_hlen[i] = 1 << rdft_hbits;
  177. if (!(s->rdft_hdata[i] = av_malloc_array(h, s->rdft_hlen[i] * sizeof(FFTSample))))
  178. return AVERROR(ENOMEM);
  179. /* RDFT - Array initialization for Vertical pass*/
  180. for (rdft_vbits = 1; 1 << rdft_vbits < h*10/9; rdft_vbits++);
  181. s->rdft_vbits[i] = rdft_vbits;
  182. s->rdft_vlen[i] = 1 << rdft_vbits;
  183. if (!(s->rdft_vdata[i] = av_malloc_array(s->rdft_hlen[i], s->rdft_vlen[i] * sizeof(FFTSample))))
  184. return AVERROR(ENOMEM);
  185. }
  186. /*Luminance value - Array initialization*/
  187. values[VAR_W] = inlink->w;
  188. values[VAR_H] = inlink->h;
  189. for (plane = 0; plane < 3; plane++)
  190. {
  191. if(!(s->weight[plane] = av_malloc_array(s->rdft_hlen[plane], s->rdft_vlen[plane] * sizeof(double))))
  192. return AVERROR(ENOMEM);
  193. for (i = 0; i < s->rdft_hlen[plane]; i++)
  194. {
  195. values[VAR_X] = i;
  196. for (j = 0; j < s->rdft_vlen[plane]; j++)
  197. {
  198. values[VAR_Y] = j;
  199. s->weight[plane][i * s->rdft_vlen[plane] + j] =
  200. av_expr_eval(s->weight_expr[plane], values, s);
  201. }
  202. }
  203. }
  204. return 0;
  205. }
  206. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  207. {
  208. AVFilterContext *ctx = inlink->dst;
  209. AVFilterLink *outlink = inlink->dst->outputs[0];
  210. const AVPixFmtDescriptor *desc;
  211. FFTFILTContext *s = ctx->priv;
  212. AVFrame *out;
  213. int i, j, plane;
  214. out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
  215. if (!out)
  216. return AVERROR(ENOMEM);
  217. av_frame_copy_props(out, in);
  218. desc = av_pix_fmt_desc_get(inlink->format);
  219. for (plane = 0; plane < desc->nb_components; plane++) {
  220. int w = inlink->w;
  221. int h = inlink->h;
  222. if (plane == 1 || plane == 2) {
  223. w = AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
  224. h = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  225. }
  226. rdft_horizontal(s, in, w, h, plane);
  227. rdft_vertical(s, h, plane);
  228. /*Change user defined parameters*/
  229. for (i = 0; i < s->rdft_hlen[plane]; i++)
  230. for (j = 0; j < s->rdft_vlen[plane]; j++)
  231. s->rdft_vdata[plane][i * s->rdft_vlen[plane] + j] *=
  232. s->weight[plane][i * s->rdft_vlen[plane] + j];
  233. s->rdft_vdata[plane][0] += s->rdft_hlen[plane] * s->rdft_vlen[plane] * s->dc[plane];
  234. irdft_vertical(s, h, plane);
  235. irdft_horizontal(s, out, w, h, plane);
  236. }
  237. av_frame_free(&in);
  238. return ff_filter_frame(outlink, out);
  239. }
  240. static av_cold void uninit(AVFilterContext *ctx)
  241. {
  242. FFTFILTContext *s = ctx->priv;
  243. int i;
  244. for (i = 0; i < MAX_PLANES; i++) {
  245. av_free(s->rdft_hdata[i]);
  246. av_free(s->rdft_vdata[i]);
  247. av_expr_free(s->weight_expr[i]);
  248. av_free(s->weight[i]);
  249. }
  250. }
  251. static int query_formats(AVFilterContext *ctx)
  252. {
  253. static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
  254. AV_PIX_FMT_GRAY8,
  255. AV_PIX_FMT_YUV444P,
  256. AV_PIX_FMT_NONE
  257. };
  258. AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
  259. if (!fmts_list)
  260. return AVERROR(ENOMEM);
  261. return ff_set_common_formats(ctx, fmts_list);
  262. }
  263. static const AVFilterPad fftfilt_inputs[] = {
  264. {
  265. .name = "default",
  266. .type = AVMEDIA_TYPE_VIDEO,
  267. .config_props = config_props,
  268. .filter_frame = filter_frame,
  269. },
  270. { NULL }
  271. };
  272. static const AVFilterPad fftfilt_outputs[] = {
  273. {
  274. .name = "default",
  275. .type = AVMEDIA_TYPE_VIDEO,
  276. },
  277. { NULL }
  278. };
  279. AVFilter ff_vf_fftfilt = {
  280. .name = "fftfilt",
  281. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to pixels in frequency domain."),
  282. .priv_size = sizeof(FFTFILTContext),
  283. .priv_class = &fftfilt_class,
  284. .inputs = fftfilt_inputs,
  285. .outputs = fftfilt_outputs,
  286. .query_formats = query_formats,
  287. .init = initialize,
  288. .uninit = uninit,
  289. };