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.

345 lines
12KB

  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. typedef struct {
  32. const AVClass *class;
  33. RDFTContext *rdft;
  34. int rdft_hbits[3];
  35. int rdft_vbits[3];
  36. size_t rdft_hlen[3];
  37. size_t rdft_vlen[3];
  38. FFTSample *rdft_hdata[3];
  39. FFTSample *rdft_vdata[3];
  40. int dc[3];
  41. char *weight_str[3];
  42. AVExpr *weight_expr[3];
  43. double *weight[3];
  44. } FFTFILTContext;
  45. static const char *const var_names[] = { "X", "Y", "W", "H", NULL };
  46. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_VARS_NB };
  47. enum { Y = 0, U, V };
  48. #define OFFSET(x) offsetof(FFTFILTContext, x)
  49. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  50. static const AVOption fftfilt_options[] = {
  51. { "dc_Y", "adjust gain in Y plane", OFFSET(dc[Y]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  52. { "dc_U", "adjust gain in U plane", OFFSET(dc[U]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  53. { "dc_V", "adjust gain in V plane", OFFSET(dc[V]), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1000, FLAGS },
  54. { "weight_Y", "set luminance expression in Y plane", OFFSET(weight_str[Y]), AV_OPT_TYPE_STRING, {.str = "1"}, CHAR_MIN, CHAR_MAX, FLAGS },
  55. { "weight_U", "set chrominance expression in U plane", OFFSET(weight_str[U]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  56. { "weight_V", "set chrominance expression in V plane", OFFSET(weight_str[V]), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  57. {NULL},
  58. };
  59. AVFILTER_DEFINE_CLASS(fftfilt);
  60. static inline double lum(void *priv, double x, double y, int plane)
  61. {
  62. FFTFILTContext *fftfilt = priv;
  63. return fftfilt->rdft_vdata[plane][(int)x * fftfilt->rdft_vlen[plane] + (int)y];
  64. }
  65. static double weight_Y(void *priv, double x, double y) { return lum(priv, x, y, Y); }
  66. static double weight_U(void *priv, double x, double y) { return lum(priv, x, y, U); }
  67. static double weight_V(void *priv, double x, double y) { return lum(priv, x, y, V); }
  68. static void copy_rev (FFTSample *dest, int w, int w2)
  69. {
  70. int i;
  71. for (i = w; i < w + (w2-w)/2; i++)
  72. dest[i] = dest[2*w - i - 1];
  73. for (; i < w2; i++)
  74. dest[i] = dest[w2 - i];
  75. }
  76. /*Horizontal pass - RDFT*/
  77. static void rdft_horizontal(FFTFILTContext *fftfilt, AVFrame *in, int w, int h, int plane)
  78. {
  79. int i, j;
  80. fftfilt->rdft = av_rdft_init(fftfilt->rdft_hbits[plane], DFT_R2C);
  81. for (i = 0; i < h; i++) {
  82. for (j = 0; j < w; j++)
  83. fftfilt->rdft_hdata[plane][i * fftfilt->rdft_hlen[plane] + j] = *(in->data[plane] + in->linesize[plane] * i + j);
  84. copy_rev(fftfilt->rdft_hdata[plane] + i * fftfilt->rdft_hlen[plane], w, fftfilt->rdft_hlen[plane]);
  85. }
  86. for (i = 0; i < h; i++)
  87. av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata[plane] + i * fftfilt->rdft_hlen[plane]);
  88. av_rdft_end(fftfilt->rdft);
  89. }
  90. /*Vertical pass - RDFT*/
  91. static void rdft_vertical(FFTFILTContext *fftfilt, int h, int plane)
  92. {
  93. int i, j;
  94. fftfilt->rdft = av_rdft_init(fftfilt->rdft_vbits[plane], DFT_R2C);
  95. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++) {
  96. for (j = 0; j < h; j++)
  97. fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j] =
  98. fftfilt->rdft_hdata[plane][j * fftfilt->rdft_hlen[plane] + i];
  99. copy_rev(fftfilt->rdft_vdata[plane] + i * fftfilt->rdft_vlen[plane], h, fftfilt->rdft_vlen[plane]);
  100. }
  101. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
  102. av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata[plane] + i * fftfilt->rdft_vlen[plane]);
  103. av_rdft_end(fftfilt->rdft);
  104. }
  105. /*Vertical pass - IRDFT*/
  106. static void irdft_vertical(FFTFILTContext *fftfilt, int h, int plane)
  107. {
  108. int i, j;
  109. fftfilt->rdft = av_rdft_init(fftfilt->rdft_vbits[plane], IDFT_C2R);
  110. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
  111. av_rdft_calc(fftfilt->rdft, fftfilt->rdft_vdata[plane] + i * fftfilt->rdft_vlen[plane]);
  112. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
  113. for (j = 0; j < h; j++)
  114. fftfilt->rdft_hdata[plane][j * fftfilt->rdft_hlen[plane] + i] =
  115. fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j];
  116. av_rdft_end(fftfilt->rdft);
  117. }
  118. /*Horizontal pass - IRDFT*/
  119. static void irdft_horizontal(FFTFILTContext *fftfilt, AVFrame *out, int w, int h, int plane)
  120. {
  121. int i, j;
  122. fftfilt->rdft = av_rdft_init(fftfilt->rdft_hbits[plane], IDFT_C2R);
  123. for (i = 0; i < h; i++)
  124. av_rdft_calc(fftfilt->rdft, fftfilt->rdft_hdata[plane] + i * fftfilt->rdft_hlen[plane]);
  125. for (i = 0; i < h; i++)
  126. for (j = 0; j < w; j++)
  127. *(out->data[plane] + out->linesize[plane] * i + j) = av_clip(fftfilt->rdft_hdata[plane][i
  128. *fftfilt->rdft_hlen[plane] + j] * 4 /
  129. (fftfilt->rdft_hlen[plane] *
  130. fftfilt->rdft_vlen[plane]), 0, 255);
  131. av_rdft_end(fftfilt->rdft);
  132. }
  133. static av_cold int initialize(AVFilterContext *ctx)
  134. {
  135. FFTFILTContext *fftfilt = ctx->priv;
  136. int ret = 0, plane;
  137. if (!fftfilt->dc[U] && !fftfilt->dc[V]) {
  138. fftfilt->dc[U] = fftfilt->dc[Y];
  139. fftfilt->dc[V] = fftfilt->dc[Y];
  140. } else {
  141. if (!fftfilt->dc[U]) fftfilt->dc[U] = fftfilt->dc[V];
  142. if (!fftfilt->dc[V]) fftfilt->dc[V] = fftfilt->dc[U];
  143. }
  144. if (!fftfilt->weight_str[U] && !fftfilt->weight_str[V]) {
  145. fftfilt->weight_str[U] = av_strdup(fftfilt->weight_str[Y]);
  146. fftfilt->weight_str[V] = av_strdup(fftfilt->weight_str[Y]);
  147. } else {
  148. if (!fftfilt->weight_str[U]) fftfilt->weight_str[U] = av_strdup(fftfilt->weight_str[V]);
  149. if (!fftfilt->weight_str[V]) fftfilt->weight_str[V] = av_strdup(fftfilt->weight_str[U]);
  150. }
  151. for (plane = 0; plane < 3; plane++) {
  152. static double (*p[])(void *, double, double) = { weight_Y, weight_U, weight_V };
  153. const char *const func2_names[] = {"weight_Y", "weight_U", "weight_V", NULL };
  154. double (*func2[])(void *, double, double) = { weight_Y, weight_U, weight_V, p[plane], NULL };
  155. ret = av_expr_parse(&fftfilt->weight_expr[plane], fftfilt->weight_str[plane], var_names,
  156. NULL, NULL, func2_names, func2, 0, ctx);
  157. if (ret < 0)
  158. break;
  159. }
  160. return ret;
  161. }
  162. static int config_props(AVFilterLink *inlink)
  163. {
  164. FFTFILTContext *fftfilt = inlink->dst->priv;
  165. const AVPixFmtDescriptor *desc;
  166. int rdft_hbits, rdft_vbits, i, j, plane;
  167. double values[VAR_VARS_NB];
  168. desc = av_pix_fmt_desc_get(inlink->format);
  169. for (i = 0; i < desc->nb_components; i++) {
  170. int w = inlink->w;
  171. int h = inlink->h;
  172. /* RDFT - Array initialization for Horizontal pass*/
  173. for (rdft_hbits = 1; 1 << rdft_hbits < w*10/9; rdft_hbits++);
  174. fftfilt->rdft_hbits[i] = rdft_hbits;
  175. fftfilt->rdft_hlen[i] = 1 << rdft_hbits;
  176. if (!(fftfilt->rdft_hdata[i] = av_malloc_array(h, fftfilt->rdft_hlen[i] * sizeof(FFTSample))))
  177. return AVERROR(ENOMEM);
  178. /* RDFT - Array initialization for Vertical pass*/
  179. for (rdft_vbits = 1; 1 << rdft_vbits < h*10/9; rdft_vbits++);
  180. fftfilt->rdft_vbits[i] = rdft_vbits;
  181. fftfilt->rdft_vlen[i] = 1 << rdft_vbits;
  182. if (!(fftfilt->rdft_vdata[i] = av_malloc_array(fftfilt->rdft_hlen[i], fftfilt->rdft_vlen[i] * sizeof(FFTSample))))
  183. return AVERROR(ENOMEM);
  184. }
  185. /*Luminance value - Array initialization*/
  186. values[VAR_W] = inlink->w;
  187. values[VAR_H] = inlink->h;
  188. for (plane = 0; plane < 3; plane++)
  189. {
  190. if(!(fftfilt->weight[plane] = av_malloc_array(fftfilt->rdft_hlen[plane], fftfilt->rdft_vlen[plane] * sizeof(double))))
  191. return AVERROR(ENOMEM);
  192. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
  193. {
  194. values[VAR_X] = i;
  195. for (j = 0; j < fftfilt->rdft_vlen[plane]; j++)
  196. {
  197. values[VAR_Y] = j;
  198. fftfilt->weight[plane][i * fftfilt->rdft_vlen[plane] + j] =
  199. av_expr_eval(fftfilt->weight_expr[plane], values, fftfilt);
  200. }
  201. }
  202. }
  203. return 0;
  204. }
  205. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  206. {
  207. AVFilterContext *ctx = inlink->dst;
  208. AVFilterLink *outlink = inlink->dst->outputs[0];
  209. const AVPixFmtDescriptor *desc;
  210. FFTFILTContext *fftfilt = ctx->priv;
  211. AVFrame *out;
  212. int i, j, plane;
  213. out = ff_get_video_buffer(outlink, inlink->w, inlink->h);
  214. if (!out)
  215. return AVERROR(ENOMEM);
  216. av_frame_copy_props(out, in);
  217. desc = av_pix_fmt_desc_get(inlink->format);
  218. for (plane = 0; plane < desc->nb_components; plane++) {
  219. int w = inlink->w;
  220. int h = inlink->h;
  221. if (plane == 1 || plane == 2) {
  222. w = FF_CEIL_RSHIFT(w, desc->log2_chroma_w);
  223. h = FF_CEIL_RSHIFT(h, desc->log2_chroma_h);
  224. }
  225. rdft_horizontal(fftfilt, in, w, h, plane);
  226. rdft_vertical(fftfilt, h, plane);
  227. /*Change user defined parameters*/
  228. for (i = 0; i < fftfilt->rdft_hlen[plane]; i++)
  229. for (j = 0; j < fftfilt->rdft_vlen[plane]; j++)
  230. fftfilt->rdft_vdata[plane][i * fftfilt->rdft_vlen[plane] + j] *=
  231. fftfilt->weight[plane][i * fftfilt->rdft_vlen[plane] + j];
  232. fftfilt->rdft_vdata[plane][0] += fftfilt->rdft_hlen[plane] * fftfilt->rdft_vlen[plane] * fftfilt->dc[plane];
  233. irdft_vertical(fftfilt, h, plane);
  234. irdft_horizontal(fftfilt, out, w, h, plane);
  235. }
  236. av_frame_free(&in);
  237. return ff_filter_frame(outlink, out);
  238. }
  239. static av_cold void uninit(AVFilterContext *ctx)
  240. {
  241. FFTFILTContext *fftfilt = ctx->priv;
  242. int i;
  243. for (i = 0; i < 3; i++) {
  244. av_free(fftfilt->rdft_hdata[i]);
  245. av_free(fftfilt->rdft_vdata[i]);
  246. av_expr_free(fftfilt->weight_expr[i]);
  247. av_free(fftfilt->weight[i]);
  248. }
  249. }
  250. static int query_formats(AVFilterContext *ctx)
  251. {
  252. static const enum AVPixelFormat pixel_fmts_fftfilt[] = {
  253. AV_PIX_FMT_GRAY8,
  254. AV_PIX_FMT_YUV444P,
  255. AV_PIX_FMT_NONE
  256. };
  257. AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_fftfilt);
  258. if (!fmts_list)
  259. return AVERROR(ENOMEM);
  260. return ff_set_common_formats(ctx, fmts_list);
  261. }
  262. static const AVFilterPad fftfilt_inputs[] = {
  263. {
  264. .name = "default",
  265. .type = AVMEDIA_TYPE_VIDEO,
  266. .config_props = config_props,
  267. .filter_frame = filter_frame,
  268. },
  269. { NULL }
  270. };
  271. static const AVFilterPad fftfilt_outputs[] = {
  272. {
  273. .name = "default",
  274. .type = AVMEDIA_TYPE_VIDEO,
  275. },
  276. { NULL }
  277. };
  278. AVFilter ff_vf_fftfilt = {
  279. .name = "fftfilt",
  280. .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain"),
  281. .priv_size = sizeof(FFTFILTContext),
  282. .priv_class = &fftfilt_class,
  283. .inputs = fftfilt_inputs,
  284. .outputs = fftfilt_outputs,
  285. .query_formats = query_formats,
  286. .init = initialize,
  287. .uninit = uninit,
  288. };