vf_unsharp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 FFmpeg.
  7. *
  8. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 FFmpeg 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/imgutils.h"
  43. #include "libavutil/mem.h"
  44. #include "libavutil/opt.h"
  45. #include "libavutil/pixdesc.h"
  46. #include "unsharp.h"
  47. typedef struct TheadData {
  48. UnsharpFilterParam *fp;
  49. uint8_t *dst;
  50. const uint8_t *src;
  51. int dst_stride;
  52. int src_stride;
  53. int width;
  54. int height;
  55. } ThreadData;
  56. static int unsharp_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  57. {
  58. ThreadData *td = arg;
  59. UnsharpFilterParam *fp = td->fp;
  60. uint32_t **sc = fp->sc;
  61. uint32_t *sr = fp->sr;
  62. const uint8_t *src2 = NULL; //silence a warning
  63. const int amount = fp->amount;
  64. const int steps_x = fp->steps_x;
  65. const int steps_y = fp->steps_y;
  66. const int scalebits = fp->scalebits;
  67. const int32_t halfscale = fp->halfscale;
  68. uint8_t *dst = td->dst;
  69. const uint8_t *src = td->src;
  70. const int dst_stride = td->dst_stride;
  71. const int src_stride = td->src_stride;
  72. const int width = td->width;
  73. const int height = td->height;
  74. const int sc_offset = jobnr * 2 * steps_y;
  75. const int sr_offset = jobnr * (MAX_MATRIX_SIZE - 1);
  76. const int slice_start = (height * jobnr) / nb_jobs;
  77. const int slice_end = (height * (jobnr+1)) / nb_jobs;
  78. int32_t res;
  79. int x, y, z;
  80. uint32_t tmp1, tmp2;
  81. if (!amount) {
  82. av_image_copy_plane(dst + slice_start * dst_stride, dst_stride,
  83. src + slice_start * src_stride, src_stride,
  84. width, slice_end - slice_start);
  85. return 0;
  86. }
  87. for (y = 0; y < 2 * steps_y; y++)
  88. memset(sc[sc_offset + y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x));
  89. // if this is not the first tile, we start from (slice_start - steps_y),
  90. // so we can get smooth result at slice boundary
  91. if (slice_start > steps_y) {
  92. src += (slice_start - steps_y) * src_stride;
  93. dst += (slice_start - steps_y) * dst_stride;
  94. }
  95. for (y = -steps_y + slice_start; y < steps_y + slice_end; y++) {
  96. if (y < height)
  97. src2 = src;
  98. memset(sr + sr_offset, 0, sizeof(sr[0]) * (2 * steps_x - 1));
  99. for (x = -steps_x; x < width + steps_x; x++) {
  100. tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
  101. for (z = 0; z < steps_x * 2; z += 2) {
  102. tmp2 = sr[sr_offset + z + 0] + tmp1; sr[sr_offset + z + 0] = tmp1;
  103. tmp1 = sr[sr_offset + z + 1] + tmp2; sr[sr_offset + z + 1] = tmp2;
  104. }
  105. for (z = 0; z < steps_y * 2; z += 2) {
  106. tmp2 = sc[sc_offset + z + 0][x + steps_x] + tmp1; sc[sc_offset + z + 0][x + steps_x] = tmp1;
  107. tmp1 = sc[sc_offset + z + 1][x + steps_x] + tmp2; sc[sc_offset + z + 1][x + steps_x] = tmp2;
  108. }
  109. if (x >= steps_x && y >= (steps_y + slice_start)) {
  110. const uint8_t *srx = src - steps_y * src_stride + x - steps_x;
  111. uint8_t *dsx = dst - steps_y * dst_stride + x - steps_x;
  112. res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16);
  113. *dsx = av_clip_uint8(res);
  114. }
  115. }
  116. if (y >= 0) {
  117. dst += dst_stride;
  118. src += src_stride;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
  124. {
  125. AVFilterLink *inlink = ctx->inputs[0];
  126. UnsharpContext *s = ctx->priv;
  127. int i, plane_w[3], plane_h[3];
  128. UnsharpFilterParam *fp[3];
  129. ThreadData td;
  130. plane_w[0] = inlink->w;
  131. plane_w[1] = plane_w[2] = AV_CEIL_RSHIFT(inlink->w, s->hsub);
  132. plane_h[0] = inlink->h;
  133. plane_h[1] = plane_h[2] = AV_CEIL_RSHIFT(inlink->h, s->vsub);
  134. fp[0] = &s->luma;
  135. fp[1] = fp[2] = &s->chroma;
  136. for (i = 0; i < 3; i++) {
  137. td.fp = fp[i];
  138. td.dst = out->data[i];
  139. td.src = in->data[i];
  140. td.width = plane_w[i];
  141. td.height = plane_h[i];
  142. td.dst_stride = out->linesize[i];
  143. td.src_stride = in->linesize[i];
  144. ctx->internal->execute(ctx, unsharp_slice, &td, NULL, FFMIN(plane_h[i], s->nb_threads));
  145. }
  146. return 0;
  147. }
  148. static void set_filter_param(UnsharpFilterParam *fp, int msize_x, int msize_y, float amount)
  149. {
  150. fp->msize_x = msize_x;
  151. fp->msize_y = msize_y;
  152. fp->amount = amount * 65536.0;
  153. fp->steps_x = msize_x / 2;
  154. fp->steps_y = msize_y / 2;
  155. fp->scalebits = (fp->steps_x + fp->steps_y) * 2;
  156. fp->halfscale = 1 << (fp->scalebits - 1);
  157. }
  158. static av_cold int init(AVFilterContext *ctx)
  159. {
  160. UnsharpContext *s = ctx->priv;
  161. set_filter_param(&s->luma, s->lmsize_x, s->lmsize_y, s->lamount);
  162. set_filter_param(&s->chroma, s->cmsize_x, s->cmsize_y, s->camount);
  163. if (s->luma.scalebits >= 26 || s->chroma.scalebits >= 26) {
  164. av_log(ctx, AV_LOG_ERROR, "luma or chroma matrix size too big\n");
  165. return AVERROR(EINVAL);
  166. }
  167. s->apply_unsharp = apply_unsharp_c;
  168. return 0;
  169. }
  170. static int query_formats(AVFilterContext *ctx)
  171. {
  172. static const enum AVPixelFormat pix_fmts[] = {
  173. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
  174. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
  175. AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
  176. };
  177. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  178. if (!fmts_list)
  179. return AVERROR(ENOMEM);
  180. return ff_set_common_formats(ctx, fmts_list);
  181. }
  182. static int init_filter_param(AVFilterContext *ctx, UnsharpFilterParam *fp, const char *effect_type, int width)
  183. {
  184. int z;
  185. UnsharpContext *s = ctx->priv;
  186. const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
  187. if (!(fp->msize_x & fp->msize_y & 1)) {
  188. av_log(ctx, AV_LOG_ERROR,
  189. "Invalid even size for %s matrix size %dx%d\n",
  190. effect_type, fp->msize_x, fp->msize_y);
  191. return AVERROR(EINVAL);
  192. }
  193. av_log(ctx, AV_LOG_VERBOSE, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n",
  194. effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0);
  195. fp->sr = av_malloc_array((MAX_MATRIX_SIZE - 1) * s->nb_threads, sizeof(uint32_t));
  196. fp->sc = av_mallocz_array(2 * fp->steps_y * s->nb_threads, sizeof(uint32_t *));
  197. if (!fp->sr || !fp->sc)
  198. return AVERROR(ENOMEM);
  199. for (z = 0; z < 2 * fp->steps_y * s->nb_threads; z++)
  200. if (!(fp->sc[z] = av_malloc_array(width + 2 * fp->steps_x,
  201. sizeof(*(fp->sc[z])))))
  202. return AVERROR(ENOMEM);
  203. return 0;
  204. }
  205. static int config_props(AVFilterLink *link)
  206. {
  207. UnsharpContext *s = link->dst->priv;
  208. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
  209. int ret;
  210. s->hsub = desc->log2_chroma_w;
  211. s->vsub = desc->log2_chroma_h;
  212. // ensure (height / nb_threads) > 4 * steps_y,
  213. // so that we don't have too much overlap between two threads
  214. s->nb_threads = FFMIN(ff_filter_get_nb_threads(link->dst),
  215. link->h / (4 * s->luma.steps_y));
  216. ret = init_filter_param(link->dst, &s->luma, "luma", link->w);
  217. if (ret < 0)
  218. return ret;
  219. ret = init_filter_param(link->dst, &s->chroma, "chroma", AV_CEIL_RSHIFT(link->w, s->hsub));
  220. if (ret < 0)
  221. return ret;
  222. return 0;
  223. }
  224. static void free_filter_param(UnsharpFilterParam *fp, int nb_threads)
  225. {
  226. int z;
  227. if (fp->sc) {
  228. for (z = 0; z < 2 * fp->steps_y * nb_threads; z++)
  229. av_freep(&fp->sc[z]);
  230. av_freep(&fp->sc);
  231. }
  232. av_freep(&fp->sr);
  233. }
  234. static av_cold void uninit(AVFilterContext *ctx)
  235. {
  236. UnsharpContext *s = ctx->priv;
  237. free_filter_param(&s->luma, s->nb_threads);
  238. free_filter_param(&s->chroma, s->nb_threads);
  239. }
  240. static int filter_frame(AVFilterLink *link, AVFrame *in)
  241. {
  242. UnsharpContext *s = link->dst->priv;
  243. AVFilterLink *outlink = link->dst->outputs[0];
  244. AVFrame *out;
  245. int ret = 0;
  246. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  247. if (!out) {
  248. av_frame_free(&in);
  249. return AVERROR(ENOMEM);
  250. }
  251. av_frame_copy_props(out, in);
  252. ret = s->apply_unsharp(link->dst, in, out);
  253. av_frame_free(&in);
  254. if (ret < 0) {
  255. av_frame_free(&out);
  256. return ret;
  257. }
  258. return ff_filter_frame(outlink, out);
  259. }
  260. #define OFFSET(x) offsetof(UnsharpContext, x)
  261. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  262. #define MIN_SIZE 3
  263. #define MAX_SIZE 23
  264. static const AVOption unsharp_options[] = {
  265. { "luma_msize_x", "set luma matrix horizontal size", OFFSET(lmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  266. { "lx", "set luma matrix horizontal size", OFFSET(lmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  267. { "luma_msize_y", "set luma matrix vertical size", OFFSET(lmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  268. { "ly", "set luma matrix vertical size", OFFSET(lmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  269. { "luma_amount", "set luma effect strength", OFFSET(lamount), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, -2, 5, FLAGS },
  270. { "la", "set luma effect strength", OFFSET(lamount), AV_OPT_TYPE_FLOAT, { .dbl = 1 }, -2, 5, FLAGS },
  271. { "chroma_msize_x", "set chroma matrix horizontal size", OFFSET(cmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  272. { "cx", "set chroma matrix horizontal size", OFFSET(cmsize_x), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  273. { "chroma_msize_y", "set chroma matrix vertical size", OFFSET(cmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  274. { "cy", "set chroma matrix vertical size", OFFSET(cmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS },
  275. { "chroma_amount", "set chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS },
  276. { "ca", "set chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS },
  277. { "opencl", "ignored", OFFSET(opencl), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, FLAGS },
  278. { NULL }
  279. };
  280. AVFILTER_DEFINE_CLASS(unsharp);
  281. static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
  282. {
  283. .name = "default",
  284. .type = AVMEDIA_TYPE_VIDEO,
  285. .filter_frame = filter_frame,
  286. .config_props = config_props,
  287. },
  288. { NULL }
  289. };
  290. static const AVFilterPad avfilter_vf_unsharp_outputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. },
  295. { NULL }
  296. };
  297. AVFilter ff_vf_unsharp = {
  298. .name = "unsharp",
  299. .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
  300. .priv_size = sizeof(UnsharpContext),
  301. .priv_class = &unsharp_class,
  302. .init = init,
  303. .uninit = uninit,
  304. .query_formats = query_formats,
  305. .inputs = avfilter_vf_unsharp_inputs,
  306. .outputs = avfilter_vf_unsharp_outputs,
  307. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  308. };