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.

308 lines
9.6KB

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