vf_unsharp.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Original copyright (c) 2002 Remi Guyomarch <rguyom@pobox.com>
  3. * Port copyright (c) 2010 Daniel G. Taylor <dan@programmer-art.org>
  4. * Relicensed to the LGPL with permission from Remi Guyomarch.
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * blur / sharpen filter, ported to Libav from MPlayer
  25. * libmpcodecs/unsharp.c.
  26. *
  27. * This code is based on:
  28. *
  29. * An Efficient algorithm for Gaussian blur using finite-state machines
  30. * Frederick M. Waltz and John W. V. Miller
  31. *
  32. * SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
  33. * Originally published Boston, Nov 98
  34. *
  35. * http://www.engin.umd.umich.edu/~jwvm/ece581/21_GBlur.pdf
  36. */
  37. #include "avfilter.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. #include "libavutil/common.h"
  42. #include "libavutil/mem.h"
  43. #include "libavutil/opt.h"
  44. #include "libavutil/pixdesc.h"
  45. #define MIN_SIZE 3
  46. #define MAX_SIZE 13
  47. typedef struct FilterParam {
  48. int msize_x; ///< matrix width
  49. int msize_y; ///< matrix height
  50. int amount; ///< effect amount
  51. int steps_x; ///< horizontal step count
  52. int steps_y; ///< vertical step count
  53. int scalebits; ///< bits to shift pixel
  54. int32_t halfscale; ///< amount to add to pixel
  55. uint32_t *sc[(MAX_SIZE * MAX_SIZE) - 1]; ///< finite state machine storage
  56. } FilterParam;
  57. typedef struct UnsharpContext {
  58. const AVClass *class;
  59. int lmsize_x, lmsize_y, cmsize_x, cmsize_y;
  60. float lamount, camount;
  61. FilterParam luma; ///< luma parameters (width, height, amount)
  62. FilterParam chroma; ///< chroma parameters (width, height, amount)
  63. int hsub, vsub;
  64. } UnsharpContext;
  65. static void apply_unsharp( uint8_t *dst, int dst_stride,
  66. const uint8_t *src, int src_stride,
  67. int width, int height, FilterParam *fp)
  68. {
  69. uint32_t **sc = fp->sc;
  70. uint32_t sr[(MAX_SIZE * MAX_SIZE) - 1], tmp1, tmp2;
  71. int32_t res;
  72. int x, y, z;
  73. const uint8_t *src2;
  74. if (!fp->amount) {
  75. if (dst_stride == src_stride)
  76. memcpy(dst, src, src_stride * height);
  77. else
  78. for (y = 0; y < height; y++, dst += dst_stride, src += src_stride)
  79. memcpy(dst, src, width);
  80. return;
  81. }
  82. for (y = 0; y < 2 * fp->steps_y; y++)
  83. memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * fp->steps_x));
  84. for (y = -fp->steps_y; y < height + fp->steps_y; y++) {
  85. if (y < height)
  86. src2 = src;
  87. memset(sr, 0, sizeof(sr[0]) * (2 * fp->steps_x - 1));
  88. for (x = -fp->steps_x; x < width + fp->steps_x; x++) {
  89. tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
  90. for (z = 0; z < fp->steps_x * 2; z += 2) {
  91. tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1;
  92. tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2;
  93. }
  94. for (z = 0; z < fp->steps_y * 2; z += 2) {
  95. tmp2 = sc[z + 0][x + fp->steps_x] + tmp1; sc[z + 0][x + fp->steps_x] = tmp1;
  96. tmp1 = sc[z + 1][x + fp->steps_x] + tmp2; sc[z + 1][x + fp->steps_x] = tmp2;
  97. }
  98. if (x >= fp->steps_x && y >= fp->steps_y) {
  99. const uint8_t *srx = src - fp->steps_y * src_stride + x - fp->steps_x;
  100. uint8_t *dsx = dst - fp->steps_y * dst_stride + x - fp->steps_x;
  101. res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + fp->halfscale) >> fp->scalebits)) * fp->amount) >> 16);
  102. *dsx = av_clip_uint8(res);
  103. }
  104. }
  105. if (y >= 0) {
  106. dst += dst_stride;
  107. src += src_stride;
  108. }
  109. }
  110. }
  111. static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float amount)
  112. {
  113. fp->msize_x = msize_x;
  114. fp->msize_y = msize_y;
  115. fp->amount = amount * 65536.0;
  116. fp->steps_x = msize_x / 2;
  117. fp->steps_y = msize_y / 2;
  118. fp->scalebits = (fp->steps_x + fp->steps_y) * 2;
  119. fp->halfscale = 1 << (fp->scalebits - 1);
  120. }
  121. static av_cold int init(AVFilterContext *ctx)
  122. {
  123. UnsharpContext *unsharp = ctx->priv;
  124. set_filter_param(&unsharp->luma, unsharp->lmsize_x, unsharp->lmsize_y, unsharp->lamount);
  125. set_filter_param(&unsharp->chroma, unsharp->cmsize_x, unsharp->cmsize_y, unsharp->camount);
  126. return 0;
  127. }
  128. static int query_formats(AVFilterContext *ctx)
  129. {
  130. enum AVPixelFormat pix_fmts[] = {
  131. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
  132. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  133. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  134. };
  135. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  136. return 0;
  137. }
  138. static void init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
  139. {
  140. int z;
  141. const char *effect;
  142. effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
  143. av_log(ctx, AV_LOG_VERBOSE, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n",
  144. effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0);
  145. for (z = 0; z < 2 * fp->steps_y; z++)
  146. fp->sc[z] = av_malloc(sizeof(*(fp->sc[z])) * (width + 2 * fp->steps_x));
  147. }
  148. static int config_props(AVFilterLink *link)
  149. {
  150. UnsharpContext *unsharp = link->dst->priv;
  151. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  152. unsharp->hsub = desc->log2_chroma_w;
  153. unsharp->vsub = desc->log2_chroma_h;
  154. init_filter_param(link->dst, &unsharp->luma, "luma", link->w);
  155. init_filter_param(link->dst, &unsharp->chroma, "chroma", AV_CEIL_RSHIFT(link->w, unsharp->hsub));
  156. return 0;
  157. }
  158. static void free_filter_param(FilterParam *fp)
  159. {
  160. int z;
  161. for (z = 0; z < 2 * fp->steps_y; z++)
  162. av_free(fp->sc[z]);
  163. }
  164. static av_cold void uninit(AVFilterContext *ctx)
  165. {
  166. UnsharpContext *unsharp = ctx->priv;
  167. free_filter_param(&unsharp->luma);
  168. free_filter_param(&unsharp->chroma);
  169. }
  170. static int filter_frame(AVFilterLink *link, AVFrame *in)
  171. {
  172. UnsharpContext *unsharp = link->dst->priv;
  173. AVFilterLink *outlink = link->dst->outputs[0];
  174. AVFrame *out;
  175. int cw = AV_CEIL_RSHIFT(link->w, unsharp->hsub);
  176. int ch = AV_CEIL_RSHIFT(link->h, unsharp->vsub);
  177. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  178. if (!out) {
  179. av_frame_free(&in);
  180. return AVERROR(ENOMEM);
  181. }
  182. av_frame_copy_props(out, in);
  183. apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
  184. apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma);
  185. apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma);
  186. av_frame_free(&in);
  187. return ff_filter_frame(outlink, out);
  188. }
  189. #define OFFSET(x) offsetof(UnsharpContext, x)
  190. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  191. static const AVOption options[] = {
  192. { "luma_msize_x", "luma matrix horizontal size", OFFSET(lmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  193. { "luma_msize_y", "luma matrix vertical size", OFFSET(lmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  194. { "luma_amount", "luma effect strength", OFFSET(lamount), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, -2, 5, FLAGS },
  195. { "chroma_msize_x", "chroma matrix horizontal size", OFFSET(cmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  196. { "chroma_msize_y", "chroma matrix vertical size", OFFSET(cmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  197. { "chroma_amount", "chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS },
  198. { NULL },
  199. };
  200. static const AVClass unsharp_class = {
  201. .class_name = "unsharp",
  202. .item_name = av_default_item_name,
  203. .option = options,
  204. .version = LIBAVUTIL_VERSION_INT,
  205. };
  206. static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .filter_frame = filter_frame,
  211. .config_props = config_props,
  212. },
  213. { NULL }
  214. };
  215. static const AVFilterPad avfilter_vf_unsharp_outputs[] = {
  216. {
  217. .name = "default",
  218. .type = AVMEDIA_TYPE_VIDEO,
  219. },
  220. { NULL }
  221. };
  222. AVFilter ff_vf_unsharp = {
  223. .name = "unsharp",
  224. .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
  225. .priv_size = sizeof(UnsharpContext),
  226. .priv_class = &unsharp_class,
  227. .init = init,
  228. .uninit = uninit,
  229. .query_formats = query_formats,
  230. .inputs = avfilter_vf_unsharp_inputs,
  231. .outputs = avfilter_vf_unsharp_outputs,
  232. };