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.

274 lines
9.1KB

  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
  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_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  89. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  90. AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  91. AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  92. AV_PIX_FMT_NONE
  93. };
  94. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  95. }
  96. static int config_input(AVFilterLink *inlink)
  97. {
  98. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  99. MedianContext *s = inlink->dst->priv;
  100. s->depth = desc->comp[0].depth;
  101. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  102. s->planewidth[0] = s->planewidth[3] = inlink->w;
  103. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  104. s->planeheight[0] = s->planeheight[3] = inlink->h;
  105. s->radiusV = !s->radiusV ? s->radius : s->radiusV;
  106. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  107. s->t = 2 * s->radius * s->radiusV + 2 * s->radius;
  108. for (int i = 0; i < s->nb_planes; i++) {
  109. if (!(s->planes & (1 << i)))
  110. continue;
  111. if (s->planewidth[i] < s->radius * 2 + 1) {
  112. av_log(inlink->dst, AV_LOG_ERROR, "The %d plane width %d must be not less than %d\n", i, s->planewidth[i], s->radius * 2 + 1);
  113. return AVERROR(EINVAL);
  114. }
  115. if (s->planeheight[i] < s->radiusV * 2 + 1) {
  116. av_log(inlink->dst, AV_LOG_ERROR, "The %d plane height %d must be not less than %d\n", i, s->planeheight[i], s->radiusV * 2 + 1);
  117. return AVERROR(EINVAL);
  118. }
  119. }
  120. s->nb_threads = FFMAX(1, FFMIN(s->planeheight[1] / (s->radiusV + 1), ff_filter_get_nb_threads(inlink->dst)));
  121. s->bins = 1 << ((s->depth + 1) / 2);
  122. s->fine_size = s->bins * s->bins * inlink->w;
  123. s->coarse_size = s->bins * inlink->w;
  124. s->coarse = av_calloc(s->nb_threads, sizeof(*s->coarse));
  125. s->fine = av_calloc(s->nb_threads, sizeof(*s->fine));
  126. if (!s->coarse || !s->fine)
  127. return AVERROR(ENOMEM);
  128. for (int i = 0; i < s->nb_threads; i++) {
  129. s->coarse[i] = av_malloc_array(s->coarse_size, sizeof(**s->coarse));
  130. s->fine[i] = av_malloc_array(s->fine_size, sizeof(**s->fine));
  131. if (!s->coarse[i] || !s->fine[i])
  132. return AVERROR(ENOMEM);
  133. }
  134. s->hadd = hadd;
  135. s->hsub = hsub;
  136. s->hmuladd = hmuladd;
  137. switch (s->depth) {
  138. case 8: s->filter_plane = filter_plane_8; break;
  139. case 9: s->filter_plane = filter_plane_9; break;
  140. case 10: s->filter_plane = filter_plane_10; break;
  141. case 12: s->filter_plane = filter_plane_12; break;
  142. case 14: s->filter_plane = filter_plane_14; break;
  143. case 16: s->filter_plane = filter_plane_16; break;
  144. }
  145. return 0;
  146. }
  147. typedef struct ThreadData {
  148. AVFrame *in, *out;
  149. } ThreadData;
  150. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  151. {
  152. MedianContext *s = ctx->priv;
  153. ThreadData *td = arg;
  154. AVFrame *in = td->in;
  155. AVFrame *out = td->out;
  156. for (int plane = 0; plane < s->nb_planes; plane++) {
  157. const int h = s->planeheight[plane];
  158. const int w = s->planewidth[plane];
  159. const int slice_h_start = (h * jobnr) / nb_jobs;
  160. const int slice_h_end = (h * (jobnr+1)) / nb_jobs;
  161. if (!(s->planes & (1 << plane))) {
  162. av_image_copy_plane(out->data[plane] + slice_h_start * out->linesize[plane],
  163. out->linesize[plane],
  164. in->data[plane] + slice_h_start * in->linesize[plane],
  165. in->linesize[plane],
  166. w * ((s->depth + 7) / 8),
  167. slice_h_end - slice_h_start);
  168. continue;
  169. }
  170. s->filter_plane(ctx, in->data[plane],
  171. in->linesize[plane],
  172. out->data[plane] + slice_h_start * out->linesize[plane],
  173. out->linesize[plane], w, h,
  174. slice_h_start, slice_h_end, jobnr);
  175. }
  176. return 0;
  177. }
  178. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  179. {
  180. AVFilterContext *ctx = inlink->dst;
  181. MedianContext *s = ctx->priv;
  182. AVFilterLink *outlink = ctx->outputs[0];
  183. ThreadData td;
  184. AVFrame *out;
  185. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  186. if (!out) {
  187. av_frame_free(&in);
  188. return AVERROR(ENOMEM);
  189. }
  190. av_frame_copy_props(out, in);
  191. td.in = in; td.out = out;
  192. ctx->internal->execute(ctx, filter_slice, &td, NULL, s->nb_threads);
  193. av_frame_free(&in);
  194. return ff_filter_frame(outlink, out);
  195. }
  196. static av_cold void uninit(AVFilterContext *ctx)
  197. {
  198. MedianContext *s = ctx->priv;
  199. for (int i = 0; i < s->nb_threads && s->coarse && s->fine; i++) {
  200. av_freep(&s->coarse[i]);
  201. av_freep(&s->fine[i]);
  202. }
  203. av_freep(&s->coarse);
  204. av_freep(&s->fine);
  205. }
  206. static const AVFilterPad median_inputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .config_props = config_input,
  211. .filter_frame = filter_frame,
  212. },
  213. { NULL }
  214. };
  215. static const AVFilterPad median_outputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. },
  220. { NULL }
  221. };
  222. AVFilter ff_vf_median = {
  223. .name = "median",
  224. .description = NULL_IF_CONFIG_SMALL("Apply Median filter."),
  225. .priv_size = sizeof(MedianContext),
  226. .priv_class = &median_class,
  227. .uninit = uninit,
  228. .query_formats = query_formats,
  229. .inputs = median_inputs,
  230. .outputs = median_outputs,
  231. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  232. };