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.

386 lines
13KB

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