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.

307 lines
9.5KB

  1. /*
  2. * Copyright (c) 2020 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. */
  20. #include "libavutil/imgutils.h"
  21. #include "libavutil/opt.h"
  22. #include "libavutil/pixdesc.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. typedef struct DBlurContext {
  28. const AVClass *class;
  29. float angle;
  30. float radius;
  31. int planes;
  32. float b0, b1, q, c, R3;
  33. int depth;
  34. int planewidth[4];
  35. int planeheight[4];
  36. float *buffer;
  37. int nb_planes;
  38. } DBlurContext;
  39. #define OFFSET(x) offsetof(DBlurContext, x)
  40. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  41. static const AVOption dblur_options[] = {
  42. { "angle", "set angle", OFFSET(angle), AV_OPT_TYPE_FLOAT, {.dbl=45}, 0.0, 360, FLAGS },
  43. { "radius", "set radius", OFFSET(radius), AV_OPT_TYPE_FLOAT, {.dbl=5}, 1, 8192, FLAGS },
  44. { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
  45. { NULL }
  46. };
  47. AVFILTER_DEFINE_CLASS(dblur);
  48. #define f(n, m) (dst[(n) * width + (m)])
  49. static int filter_horizontally(AVFilterContext *ctx, int width, int height)
  50. {
  51. DBlurContext *s = ctx->priv;
  52. const float b0 = s->b0;
  53. const float b1 = s->b1;
  54. const float q = s->q;
  55. const float c = s->c;
  56. float *dst = s->buffer;
  57. float g;
  58. if (s->R3 > 0) {
  59. for (int y = 1; y < height - 1; y++) {
  60. g = q * f(0, 0) + c * f(0, 0);
  61. for (int x = 0; x < width; x++) {
  62. f(y, x) = b0 * f(y, x) + b1 * f(y - 1, x) + g;
  63. g = q * f(y, x) + c * f(y - 1, x);
  64. }
  65. }
  66. for (int y = height - 2; y >= 0; y--) {
  67. g = q * f(y, width - 1) + c * f(y, width - 1);
  68. for (int x = width - 1; x >= 0; x--) {
  69. f(y, x) = b0 * f(y, x) + b1 * f(y + 1, x) + g;
  70. g = q * f(y, x) + c * f(y + 1, x);
  71. }
  72. }
  73. } else {
  74. for (int y = 1; y < height - 1; y++) {
  75. g = q * f(0, width - 1) + c * f(0, width - 1);
  76. for (int x = width - 1; x >= 0; x--) {
  77. f(y, x) = b0 * f(y, x) + b1 * f(y - 1, x) + g;
  78. g = q * f(y, x) + c * f(y - 1, x);
  79. }
  80. }
  81. for (int y = height - 2; y >= 0; y--) {
  82. g = q * f(y, 0) + c * f(y, 0);
  83. for (int x = 0; x < width; x++) {
  84. f(y, x) = b0 * f(y, x) + b1 * f(y + 1, x) + g;
  85. g = q * f(y, x) + c * f(y + 1, x);
  86. }
  87. }
  88. }
  89. return 0;
  90. }
  91. static void diriir2d(AVFilterContext *ctx, int plane)
  92. {
  93. DBlurContext *s = ctx->priv;
  94. const int width = s->planewidth[plane];
  95. const int height = s->planeheight[plane];
  96. filter_horizontally(ctx, width, height);
  97. }
  98. static int query_formats(AVFilterContext *ctx)
  99. {
  100. static const enum AVPixelFormat pix_fmts[] = {
  101. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
  102. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
  103. AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
  104. AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
  105. AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
  106. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
  107. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
  108. AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
  109. AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
  110. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
  111. AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
  112. AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
  113. AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
  114. AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
  115. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
  116. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
  117. AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
  118. AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
  119. AV_PIX_FMT_NONE
  120. };
  121. return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  122. }
  123. static int config_input(AVFilterLink *inlink)
  124. {
  125. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  126. DBlurContext *s = inlink->dst->priv;
  127. s->depth = desc->comp[0].depth;
  128. s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
  129. s->planewidth[0] = s->planewidth[3] = inlink->w;
  130. s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
  131. s->planeheight[0] = s->planeheight[3] = inlink->h;
  132. s->nb_planes = av_pix_fmt_count_planes(inlink->format);
  133. s->buffer = av_malloc_array(FFALIGN(inlink->w, 16), FFALIGN(inlink->h, 16) * sizeof(*s->buffer));
  134. if (!s->buffer)
  135. return AVERROR(ENOMEM);
  136. return 0;
  137. }
  138. static void set_params(DBlurContext *s, float angle, float r)
  139. {
  140. float mu, nu, R1, R2, w1, w2;
  141. float a0, a1, a2, a3;
  142. angle = angle * M_PI / 180.f;
  143. mu = cosf(angle);
  144. nu = sinf(angle);
  145. R1 = (mu * r) * (mu * r);
  146. R2 = (nu * r) * (nu * r);
  147. s->R3 = mu * nu * r * r;
  148. w1 = sqrtf(0.25f + R1);
  149. w2 = sqrtf(0.25f + R2);
  150. a0 = (w1 + 0.5f) * (w2 + 0.5f) - fabsf(s->R3);
  151. a1 = 0.5f + w2 - a0;
  152. a2 = 0.5f + w1 - a0;
  153. a3 = a0 - w1 - w2;
  154. s->b0 = 1.f / a0;
  155. s->b1 = -a2 / a0;
  156. s->q = -a1 / a0;
  157. s->c = -a3 / a0;
  158. }
  159. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  160. {
  161. AVFilterContext *ctx = inlink->dst;
  162. DBlurContext *s = ctx->priv;
  163. AVFilterLink *outlink = ctx->outputs[0];
  164. AVFrame *out;
  165. int plane;
  166. set_params(s, s->angle, s->radius);
  167. if (av_frame_is_writable(in)) {
  168. out = in;
  169. } else {
  170. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  171. if (!out) {
  172. av_frame_free(&in);
  173. return AVERROR(ENOMEM);
  174. }
  175. av_frame_copy_props(out, in);
  176. }
  177. for (plane = 0; plane < s->nb_planes; plane++) {
  178. const int height = s->planeheight[plane];
  179. const int width = s->planewidth[plane];
  180. float *bptr = s->buffer;
  181. const uint8_t *src = in->data[plane];
  182. const uint16_t *src16 = (const uint16_t *)in->data[plane];
  183. uint8_t *dst = out->data[plane];
  184. uint16_t *dst16 = (uint16_t *)out->data[plane];
  185. int y, x;
  186. if (!(s->planes & (1 << plane))) {
  187. if (out != in)
  188. av_image_copy_plane(out->data[plane], out->linesize[plane],
  189. in->data[plane], in->linesize[plane],
  190. width * ((s->depth + 7) / 8), height);
  191. continue;
  192. }
  193. if (s->depth == 8) {
  194. for (y = 0; y < height; y++) {
  195. for (x = 0; x < width; x++) {
  196. bptr[x] = src[x];
  197. }
  198. bptr += width;
  199. src += in->linesize[plane];
  200. }
  201. } else {
  202. for (y = 0; y < height; y++) {
  203. for (x = 0; x < width; x++) {
  204. bptr[x] = src16[x];
  205. }
  206. bptr += width;
  207. src16 += in->linesize[plane] / 2;
  208. }
  209. }
  210. diriir2d(ctx, plane);
  211. bptr = s->buffer;
  212. if (s->depth == 8) {
  213. for (y = 0; y < height; y++) {
  214. for (x = 0; x < width; x++) {
  215. dst[x] = bptr[x];
  216. }
  217. bptr += width;
  218. dst += out->linesize[plane];
  219. }
  220. } else {
  221. for (y = 0; y < height; y++) {
  222. for (x = 0; x < width; x++) {
  223. dst16[x] = bptr[x];
  224. }
  225. bptr += width;
  226. dst16 += out->linesize[plane] / 2;
  227. }
  228. }
  229. }
  230. if (out != in)
  231. av_frame_free(&in);
  232. return ff_filter_frame(outlink, out);
  233. }
  234. static av_cold void uninit(AVFilterContext *ctx)
  235. {
  236. DBlurContext *s = ctx->priv;
  237. av_freep(&s->buffer);
  238. }
  239. static const AVFilterPad dblur_inputs[] = {
  240. {
  241. .name = "default",
  242. .type = AVMEDIA_TYPE_VIDEO,
  243. .config_props = config_input,
  244. .filter_frame = filter_frame,
  245. },
  246. { NULL }
  247. };
  248. static const AVFilterPad dblur_outputs[] = {
  249. {
  250. .name = "default",
  251. .type = AVMEDIA_TYPE_VIDEO,
  252. },
  253. { NULL }
  254. };
  255. AVFilter ff_vf_dblur = {
  256. .name = "dblur",
  257. .description = NULL_IF_CONFIG_SMALL("Apply Directional Blur filter."),
  258. .priv_size = sizeof(DBlurContext),
  259. .priv_class = &dblur_class,
  260. .uninit = uninit,
  261. .query_formats = query_formats,
  262. .inputs = dblur_inputs,
  263. .outputs = dblur_outputs,
  264. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  265. .process_command = ff_filter_process_command,
  266. };