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.

300 lines
9.9KB

  1. /*
  2. * Copyright (c) 2019 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, 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. * Redistribution and use in source and binary forms, with or without modification,
  20. * are permitted provided that the following conditions are met:
  21. */
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/imgutils.h"
  24. #include "libavutil/opt.h"
  25. #include "libavutil/pixdesc.h"
  26. #include "avfilter.h"
  27. #include "formats.h"
  28. #include "internal.h"
  29. #include "median.h"
  30. #include "video.h"
  31. #define DEPTH 8
  32. #include "median_template.c"
  33. #undef DEPTH
  34. #define DEPTH 9
  35. #include "median_template.c"
  36. #undef DEPTH
  37. #define DEPTH 10
  38. #include "median_template.c"
  39. #undef DEPTH
  40. #define DEPTH 12
  41. #include "median_template.c"
  42. #undef DEPTH
  43. #define DEPTH 14
  44. #include "median_template.c"
  45. #undef DEPTH
  46. #define DEPTH 16
  47. #include "median_template.c"
  48. #define OFFSET(x) offsetof(MedianContext, x)
  49. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  50. static const AVOption median_options[] = {
  51. { "radius", "set median radius", OFFSET(radius), AV_OPT_TYPE_INT, {.i64=1}, 1, 127, FLAGS },
  52. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  53. { "radiusV", "set median vertical radius", OFFSET(radiusV), AV_OPT_TYPE_INT, {.i64=0},0, 127, FLAGS },
  54. { "percentile", "set median percentile", OFFSET(percentile), AV_OPT_TYPE_FLOAT, {.dbl=.5}, 0., 1., FLAGS },
  55. { NULL }
  56. };
  57. AVFILTER_DEFINE_CLASS(median);
  58. static void hadd(htype *dst, const htype *src, int bins)
  59. {
  60. for (int i = 0; i < bins; i++)
  61. dst[i] += src[i];
  62. }
  63. static void hsub(htype *dst, const htype *src, int bins)
  64. {
  65. for (int i = 0; i < bins; i++)
  66. dst[i] -= src[i];
  67. }
  68. static void hmuladd(htype *dst, const htype *src, int f, int bins)
  69. {
  70. for (int i = 0; i < bins; i++)
  71. dst[i] += f * src[i];
  72. }
  73. static int query_formats(AVFilterContext *ctx)
  74. {
  75. static const enum AVPixelFormat pix_fmts[] = {
  76. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  77. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  78. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  79. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  80. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  81. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  82. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_GBRP9,
  83. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  84. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  85. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  86. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  87. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  88. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  89. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  90. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  91. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  92. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  93. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  94. AV_PIX_FMT_NONE
  95. };
  96. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  97. }
  98. static void check_params(MedianContext *s, AVFilterLink *inlink)
  99. {
  100. for (int i = 0; i < s->nb_planes; i++) {
  101. if (!(s->planes & (1 << i)))
  102. continue;
  103. if (s->planewidth[i] < s->radius * 2 + 1) {
  104. av_log(inlink->dst, AV_LOG_WARNING, "The %d plane width %d must be not less than %d, clipping radius.\n", i, s->planewidth[i], s->radius * 2 + 1);
  105. s->radius = (s->planewidth[i] - 1) / 2;
  106. }
  107. if (s->planeheight[i] < s->radiusV * 2 + 1) {
  108. av_log(inlink->dst, AV_LOG_WARNING, "The %d plane height %d must be not less than %d, clipping radiusV.\n", i, s->planeheight[i], s->radiusV * 2 + 1);
  109. s->radiusV = (s->planeheight[i] - 1) / 2;
  110. }
  111. }
  112. s->t = (2 * s->radius * s->radiusV + s->radiusV + s->radius) * 2.f * s->percentile;
  113. }
  114. static int config_input(AVFilterLink *inlink)
  115. {
  116. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  117. MedianContext *s = inlink->dst->priv;
  118. s->depth = desc->comp[0].depth;
  119. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  120. s->planewidth[0] = s->planewidth[3] = inlink->w;
  121. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  122. s->planeheight[0] = s->planeheight[3] = inlink->h;
  123. s->radiusV = !s->radiusV ? s->radius : s->radiusV;
  124. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  125. check_params(s, inlink);
  126. s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
  127. s->bins = 1 << ((s->depth + 1) / 2);
  128. s->fine_size = s->bins * s->bins * inlink->w;
  129. s->coarse_size = s->bins * inlink->w;
  130. s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
  131. s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
  132. if (!s->coarse || !s->fine)
  133. return AVERROR(ENOMEM);
  134. for (int i = 0; i < s->nb_threads; i++) {
  135. s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
  136. s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
  137. if (!s->coarse[i] || !s->fine[i])
  138. return AVERROR(ENOMEM);
  139. }
  140. s->hadd = hadd;
  141. s->hsub = hsub;
  142. s->hmuladd = hmuladd;
  143. switch (s->depth) {
  144. case 8: s->filter_plane = filter_plane_8; break;
  145. case 9: s->filter_plane = filter_plane_9; break;
  146. case 10: s->filter_plane = filter_plane_10; break;
  147. case 12: s->filter_plane = filter_plane_12; break;
  148. case 14: s->filter_plane = filter_plane_14; break;
  149. case 16: s->filter_plane = filter_plane_16; break;
  150. }
  151. return 0;
  152. }
  153. typedef struct ThreadData {
  154. AVFrame *in, *out;
  155. } ThreadData;
  156. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  157. {
  158. MedianContext *s = ctx->priv;
  159. ThreadData *td = arg;
  160. AVFrame *in = td->in;
  161. AVFrame *out = td->out;
  162. for (int plane = 0; plane < s->nb_planes; plane++) {
  163. const int h = s->planeheight[plane];
  164. const int w = s->planewidth[plane];
  165. const int slice_h_start = (h * jobnr) / nb_jobs;
  166. const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
  167. if (!(s->planes & (1 << plane))) {
  168. av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
  169. out->linesize[plane],
  170. in->data[plane] + slice_h_start * in->linesize[plane],
  171. in->linesize[plane],
  172. w * ((s->depth + 7) / 8),
  173. slice_h_end - slice_h_start);
  174. continue;
  175. }
  176. s->filter_plane(ctx, in->data[plane],
  177. in->linesize[plane],
  178. out->data[plane] + slice_h_start * out->linesize[plane],
  179. out->linesize[plane], w, h,
  180. slice_h_start, slice_h_end, jobnr);
  181. }
  182. return 0;
  183. }
  184. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  185. {
  186. AVFilterContext *ctx = inlink->dst;
  187. MedianContext *s = ctx->priv;
  188. AVFilterLink *outlink = ctx->outputs[0];
  189. ThreadData td;
  190. AVFrame *out;
  191. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  192. if (!out) {
  193. av_frame_free(&in);
  194. return AVERROR(ENOMEM);
  195. }
  196. av_frame_copy_props(out, in);
  197. td.in = in; td.out = out;
  198. ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
  199. av_frame_free(&in);
  200. return ff_filter_frame(outlink, out);
  201. }
  202. static av_cold void uninit(AVFilterContext *ctx)
  203. {
  204. MedianContext *s = ctx->priv;
  205. for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
  206. av_freep(&s->coarse[i]);
  207. av_freep(&s->fine[i]);
  208. }
  209. av_freep(&s->coarse);
  210. av_freep(&s->fine);
  211. }
  212. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  213. char *res, int res_len, int flags)
  214. {
  215. MedianContext *s = ctx->priv;
  216. int ret;
  217. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  218. if (ret < 0)
  219. return ret;
  220. if (!s->radiusV)
  221. s->radiusV = s->radius;
  222. check_params(s, ctx->inputs[0]);
  223. return 0;
  224. }
  225. static const AVFilterPad median_inputs[] = {
  226. {
  227. .name = "default",
  228. .type = AVMEDIA_TYPE_VIDEO,
  229. .config_props = config_input,
  230. .filter_frame = filter_frame,
  231. },
  232. { NULL }
  233. };
  234. static const AVFilterPad median_outputs[] = {
  235. {
  236. .name = "default",
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. },
  239. { NULL }
  240. };
  241. AVFilter ff_vf_median = {
  242. .name = "median",
  243. .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
  244. .priv_size = sizeof(MedianContext),
  245. .priv_class = &median_class,
  246. .uninit = uninit,
  247. .query_formats = query_formats,
  248. .inputs = median_inputs,
  249. .outputs = median_outputs,
  250. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  251. .process_command = process_command,
  252. };