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.

299 lines
9.8KB

  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. { NULL }
  55. };
  56. AVFILTER_DEFINE_CLASS(median);
  57. static void hadd(htype *dst, const htype *src, int bins)
  58. {
  59. for (int i = 0; i < bins; i++)
  60. dst[i] += src[i];
  61. }
  62. static void hsub(htype *dst, const htype *src, int bins)
  63. {
  64. for (int i = 0; i < bins; i++)
  65. dst[i] -= src[i];
  66. }
  67. static void hmuladd(htype *dst, const htype *src, int f, int bins)
  68. {
  69. for (int i = 0; i < bins; i++)
  70. dst[i] += f * src[i];
  71. }
  72. static int query_formats(AVFilterContext *ctx)
  73. {
  74. static const enum AVPixelFormat pix_fmts[] = {
  75. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  76. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  77. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  78. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  79. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  80. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9,
  81. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, AV_PIX_FMT_GBRP9,
  82. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  83. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  84. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  85. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  86. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  87. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  88. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  89. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  90. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  91. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  92. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  93. AV_PIX_FMT_NONE
  94. };
  95. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  96. }
  97. static void check_params(MedianContext *s, AVFilterLink *inlink)
  98. {
  99. for (int i = 0; i < s->nb_planes; i++) {
  100. if (!(s->planes & (1 << i)))
  101. continue;
  102. if (s->planewidth[i] < s->radius * 2 + 1) {
  103. 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);
  104. s->radius = (s->planewidth[i] - 1) / 2;
  105. }
  106. if (s->planeheight[i] < s->radiusV * 2 + 1) {
  107. 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);
  108. s->radiusV = (s->planeheight[i] - 1) / 2;
  109. }
  110. }
  111. s->t = 2 * s->radius * s->radiusV + 2 * s->radius;
  112. }
  113. static int config_input(AVFilterLink *inlink)
  114. {
  115. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  116. MedianContext *s = inlink->dst->priv;
  117. s->depth = desc->comp[0].depth;
  118. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  119. s->planewidth[0] = s->planewidth[3] = inlink->w;
  120. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  121. s->planeheight[0] = s->planeheight[3] = inlink->h;
  122. s->radiusV = !s->radiusV ? s->radius : s->radiusV;
  123. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  124. check_params(s, inlink);
  125. s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
  126. s->bins = 1 << ((s->depth + 1) / 2);
  127. s->fine_size = s->bins * s->bins * inlink->w;
  128. s->coarse_size = s->bins * inlink->w;
  129. s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
  130. s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
  131. if (!s->coarse || !s->fine)
  132. return AVERROR(ENOMEM);
  133. for (int i = 0; i < s->nb_threads; i++) {
  134. s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
  135. s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
  136. if (!s->coarse[i] || !s->fine[i])
  137. return AVERROR(ENOMEM);
  138. }
  139. s->hadd = hadd;
  140. s->hsub = hsub;
  141. s->hmuladd = hmuladd;
  142. switch (s->depth) {
  143. case 8: s->filter_plane = filter_plane_8; break;
  144. case 9: s->filter_plane = filter_plane_9; break;
  145. case 10: s->filter_plane = filter_plane_10; break;
  146. case 12: s->filter_plane = filter_plane_12; break;
  147. case 14: s->filter_plane = filter_plane_14; break;
  148. case 16: s->filter_plane = filter_plane_16; break;
  149. }
  150. return 0;
  151. }
  152. typedef struct ThreadData {
  153. AVFrame *in, *out;
  154. } ThreadData;
  155. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  156. {
  157. MedianContext *s = ctx->priv;
  158. ThreadData *td = arg;
  159. AVFrame *in = td->in;
  160. AVFrame *out = td->out;
  161. for (int plane = 0; plane < s->nb_planes; plane++) {
  162. const int h = s->planeheight[plane];
  163. const int w = s->planewidth[plane];
  164. const int slice_h_start = (h * jobnr) / nb_jobs;
  165. const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
  166. if (!(s->planes & (1 << plane))) {
  167. av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
  168. out->linesize[plane],
  169. in->data[plane] + slice_h_start * in->linesize[plane],
  170. in->linesize[plane],
  171. w * ((s->depth + 7) / 8),
  172. slice_h_end - slice_h_start);
  173. continue;
  174. }
  175. s->filter_plane(ctx, in->data[plane],
  176. in->linesize[plane],
  177. out->data[plane] + slice_h_start * out->linesize[plane],
  178. out->linesize[plane], w, h,
  179. slice_h_start, slice_h_end, jobnr);
  180. }
  181. return 0;
  182. }
  183. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  184. {
  185. AVFilterContext *ctx = inlink->dst;
  186. MedianContext *s = ctx->priv;
  187. AVFilterLink *outlink = ctx->outputs[0];
  188. ThreadData td;
  189. AVFrame *out;
  190. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  191. if (!out) {
  192. av_frame_free(&in);
  193. return AVERROR(ENOMEM);
  194. }
  195. av_frame_copy_props(out, in);
  196. td.in = in; td.out = out;
  197. ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
  198. av_frame_free(&in);
  199. return ff_filter_frame(outlink, out);
  200. }
  201. static av_cold void uninit(AVFilterContext *ctx)
  202. {
  203. MedianContext *s = ctx->priv;
  204. for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
  205. av_freep(&s->coarse[i]);
  206. av_freep(&s->fine[i]);
  207. }
  208. av_freep(&s->coarse);
  209. av_freep(&s->fine);
  210. }
  211. static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  212. char *res, int res_len, int flags)
  213. {
  214. MedianContext *s = ctx->priv;
  215. int ret;
  216. ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
  217. if (ret < 0)
  218. return ret;
  219. if (!s->radiusV)
  220. s->radiusV = s->radius;
  221. check_params(s, ctx->inputs[0]);
  222. return 0;
  223. }
  224. static const AVFilterPad median_inputs[] = {
  225. {
  226. .name = "default",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .config_props = config_input,
  229. .filter_frame = filter_frame,
  230. },
  231. { NULL }
  232. };
  233. static const AVFilterPad median_outputs[] = {
  234. {
  235. .name = "default",
  236. .type = AVMEDIA_TYPE_VIDEO,
  237. },
  238. { NULL }
  239. };
  240. AVFilter ff_vf_median = {
  241. .name = "median",
  242. .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
  243. .priv_size = sizeof(MedianContext),
  244. .priv_class = &median_class,
  245. .uninit = uninit,
  246. .query_formats = query_formats,
  247. .inputs = median_inputs,
  248. .outputs = median_outputs,
  249. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  250. .process_command = process_command,
  251. };