vf_vignette.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  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 <float.h> /* DBL_MAX */
  21. #include "libavutil/opt.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "avfilter.h"
  26. #include "formats.h"
  27. #include "internal.h"
  28. #include "video.h"
  29. static const char *const var_names[] = {
  30. "w", // stream width
  31. "h", // stream height
  32. "n", // frame count
  33. "pts", // presentation timestamp expressed in AV_TIME_BASE units
  34. "r", // frame rate
  35. "t", // timestamp expressed in seconds
  36. "tb", // timebase
  37. NULL
  38. };
  39. enum var_name {
  40. VAR_W,
  41. VAR_H,
  42. VAR_N,
  43. VAR_PTS,
  44. VAR_R,
  45. VAR_T,
  46. VAR_TB,
  47. VAR_NB
  48. };
  49. typedef struct {
  50. const AVClass *class;
  51. const AVPixFmtDescriptor *desc;
  52. int backward;
  53. enum EvalMode { EVAL_MODE_INIT, EVAL_MODE_FRAME, EVAL_MODE_NB } eval_mode;
  54. #define DEF_EXPR_FIELDS(name) AVExpr *name##_pexpr; char *name##_expr; double name
  55. DEF_EXPR_FIELDS(angle);
  56. DEF_EXPR_FIELDS(x0);
  57. DEF_EXPR_FIELDS(y0);
  58. double var_values[VAR_NB];
  59. float *fmap;
  60. int fmap_linesize;
  61. double dmax;
  62. float xscale, yscale;
  63. uint32_t dither;
  64. int do_dither;
  65. AVRational aspect;
  66. AVRational scale;
  67. } VignetteContext;
  68. #define OFFSET(x) offsetof(VignetteContext, x)
  69. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  70. static const AVOption vignette_options[] = {
  71. { "angle", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
  72. { "a", "set lens angle", OFFSET(angle_expr), AV_OPT_TYPE_STRING, {.str="PI/5"}, .flags = FLAGS },
  73. { "x0", "set circle center position on x-axis", OFFSET(x0_expr), AV_OPT_TYPE_STRING, {.str="w/2"}, .flags = FLAGS },
  74. { "y0", "set circle center position on y-axis", OFFSET(y0_expr), AV_OPT_TYPE_STRING, {.str="h/2"}, .flags = FLAGS },
  75. { "mode", "set forward/backward mode", OFFSET(backward), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS, "mode" },
  76. { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 0}, INT_MIN, INT_MAX, FLAGS, "mode"},
  77. { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, INT_MIN, INT_MAX, FLAGS, "mode"},
  78. { "eval", "specify when to evaluate expressions", OFFSET(eval_mode), AV_OPT_TYPE_INT, {.i64 = EVAL_MODE_INIT}, 0, EVAL_MODE_NB-1, FLAGS, "eval" },
  79. { "init", "eval expressions once during initialization", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_INIT}, .flags = FLAGS, .unit = "eval" },
  80. { "frame", "eval expressions for each frame", 0, AV_OPT_TYPE_CONST, {.i64=EVAL_MODE_FRAME}, .flags = FLAGS, .unit = "eval" },
  81. { "dither", "set dithering", OFFSET(do_dither), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
  82. { "aspect", "set aspect ratio", OFFSET(aspect), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, DBL_MAX, .flags = FLAGS },
  83. { NULL }
  84. };
  85. AVFILTER_DEFINE_CLASS(vignette);
  86. static av_cold int init(AVFilterContext *ctx)
  87. {
  88. VignetteContext *s = ctx->priv;
  89. #define PARSE_EXPR(name) do { \
  90. int ret = av_expr_parse(&s->name##_pexpr, s->name##_expr, var_names, \
  91. NULL, NULL, NULL, NULL, 0, ctx); \
  92. if (ret < 0) { \
  93. av_log(ctx, AV_LOG_ERROR, "Unable to parse expression for '" \
  94. AV_STRINGIFY(name) "'\n"); \
  95. return ret; \
  96. } \
  97. } while (0)
  98. PARSE_EXPR(angle);
  99. PARSE_EXPR(x0);
  100. PARSE_EXPR(y0);
  101. return 0;
  102. }
  103. static av_cold void uninit(AVFilterContext *ctx)
  104. {
  105. VignetteContext *s = ctx->priv;
  106. av_freep(&s->fmap);
  107. av_expr_free(s->angle_pexpr);
  108. av_expr_free(s->x0_pexpr);
  109. av_expr_free(s->y0_pexpr);
  110. }
  111. static int query_formats(AVFilterContext *ctx)
  112. {
  113. static const enum AVPixelFormat pix_fmts[] = {
  114. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P,
  115. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
  116. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  117. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  118. AV_PIX_FMT_GRAY8,
  119. AV_PIX_FMT_NONE
  120. };
  121. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  122. return 0;
  123. }
  124. static double get_natural_factor(const VignetteContext *s, int x, int y)
  125. {
  126. const int xx = (x - s->x0) * s->xscale;
  127. const int yy = (y - s->y0) * s->yscale;
  128. const double dnorm = hypot(xx, yy) / s->dmax;
  129. if (dnorm > 1) {
  130. return 0;
  131. } else {
  132. const double c = cos(s->angle * dnorm);
  133. return (c*c)*(c*c); // do not remove braces, it helps compilers
  134. }
  135. }
  136. #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
  137. #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts) * av_q2d(tb))
  138. static void update_context(VignetteContext *s, AVFilterLink *inlink, AVFrame *frame)
  139. {
  140. int x, y;
  141. float *dst = s->fmap;
  142. int dst_linesize = s->fmap_linesize;
  143. if (frame) {
  144. s->var_values[VAR_N] = inlink->frame_count;
  145. s->var_values[VAR_T] = TS2T(frame->pts, inlink->time_base);
  146. s->var_values[VAR_PTS] = TS2D(frame->pts);
  147. } else {
  148. s->var_values[VAR_N] = 0;
  149. s->var_values[VAR_T] = NAN;
  150. s->var_values[VAR_PTS] = NAN;
  151. }
  152. s->angle = av_clipf(av_expr_eval(s->angle_pexpr, s->var_values, NULL), 0, M_PI_2);
  153. s->x0 = av_expr_eval(s->x0_pexpr, s->var_values, NULL);
  154. s->y0 = av_expr_eval(s->y0_pexpr, s->var_values, NULL);
  155. if (s->backward) {
  156. for (y = 0; y < inlink->h; y++) {
  157. for (x = 0; x < inlink->w; x++)
  158. dst[x] = 1. / get_natural_factor(s, x, y);
  159. dst += dst_linesize;
  160. }
  161. } else {
  162. for (y = 0; y < inlink->h; y++) {
  163. for (x = 0; x < inlink->w; x++)
  164. dst[x] = get_natural_factor(s, x, y);
  165. dst += dst_linesize;
  166. }
  167. }
  168. }
  169. static inline double get_dither_value(VignetteContext *s)
  170. {
  171. double dv = 0;
  172. if (s->do_dither) {
  173. dv = s->dither / (double)(1LL<<32);
  174. s->dither = s->dither * 1664525 + 1013904223;
  175. }
  176. return dv;
  177. }
  178. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  179. {
  180. unsigned x, y, direct = 0;
  181. AVFilterContext *ctx = inlink->dst;
  182. VignetteContext *s = ctx->priv;
  183. AVFilterLink *outlink = ctx->outputs[0];
  184. AVFrame *out;
  185. if (av_frame_is_writable(in)) {
  186. direct = 1;
  187. out = in;
  188. } else {
  189. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  190. if (!out) {
  191. av_frame_free(&in);
  192. return AVERROR(ENOMEM);
  193. }
  194. av_frame_copy_props(out, in);
  195. }
  196. if (s->eval_mode == EVAL_MODE_FRAME)
  197. update_context(s, inlink, in);
  198. if (s->desc->flags & AV_PIX_FMT_FLAG_RGB) {
  199. uint8_t *dst = out->data[0];
  200. const uint8_t *src = in ->data[0];
  201. const float *fmap = s->fmap;
  202. const int dst_linesize = out->linesize[0];
  203. const int src_linesize = in ->linesize[0];
  204. const int fmap_linesize = s->fmap_linesize;
  205. for (y = 0; y < inlink->h; y++) {
  206. uint8_t *dstp = dst;
  207. const uint8_t *srcp = src;
  208. for (x = 0; x < inlink->w; x++, dstp += 3, srcp += 3) {
  209. const float f = fmap[x];
  210. dstp[0] = av_clip_uint8(srcp[0] * f + get_dither_value(s));
  211. dstp[1] = av_clip_uint8(srcp[1] * f + get_dither_value(s));
  212. dstp[2] = av_clip_uint8(srcp[2] * f + get_dither_value(s));
  213. }
  214. dst += dst_linesize;
  215. src += src_linesize;
  216. fmap += fmap_linesize;
  217. }
  218. } else {
  219. int plane;
  220. for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
  221. uint8_t *dst = out->data[plane];
  222. const uint8_t *src = in ->data[plane];
  223. const float *fmap = s->fmap;
  224. const int dst_linesize = out->linesize[plane];
  225. const int src_linesize = in ->linesize[plane];
  226. const int fmap_linesize = s->fmap_linesize;
  227. const int chroma = plane == 1 || plane == 2;
  228. const int hsub = chroma ? s->desc->log2_chroma_w : 0;
  229. const int vsub = chroma ? s->desc->log2_chroma_h : 0;
  230. const int w = FF_CEIL_RSHIFT(inlink->w, hsub);
  231. const int h = FF_CEIL_RSHIFT(inlink->h, vsub);
  232. for (y = 0; y < h; y++) {
  233. uint8_t *dstp = dst;
  234. const uint8_t *srcp = src;
  235. for (x = 0; x < w; x++) {
  236. const double dv = get_dither_value(s);
  237. if (chroma) *dstp++ = av_clip_uint8(fmap[x << hsub] * (*srcp++ - 127) + 127 + dv);
  238. else *dstp++ = av_clip_uint8(fmap[x ] * *srcp++ + dv);
  239. }
  240. dst += dst_linesize;
  241. src += src_linesize;
  242. fmap += fmap_linesize << vsub;
  243. }
  244. }
  245. }
  246. if (!direct)
  247. av_frame_free(&in);
  248. return ff_filter_frame(outlink, out);
  249. }
  250. static int config_props(AVFilterLink *inlink)
  251. {
  252. VignetteContext *s = inlink->dst->priv;
  253. AVRational sar = inlink->sample_aspect_ratio;
  254. s->desc = av_pix_fmt_desc_get(inlink->format);
  255. s->var_values[VAR_W] = inlink->w;
  256. s->var_values[VAR_H] = inlink->h;
  257. s->var_values[VAR_TB] = av_q2d(inlink->time_base);
  258. s->var_values[VAR_R] = inlink->frame_rate.num == 0 || inlink->frame_rate.den == 0 ?
  259. NAN : av_q2d(inlink->frame_rate);
  260. if (!sar.num || !sar.den)
  261. sar.num = sar.den = 1;
  262. if (sar.num > sar.den) {
  263. s->xscale = av_q2d(av_div_q(sar, s->aspect));
  264. s->yscale = 1;
  265. } else {
  266. s->yscale = av_q2d(av_div_q(s->aspect, sar));
  267. s->xscale = 1;
  268. }
  269. s->dmax = hypot(inlink->w / 2., inlink->h / 2.);
  270. av_log(s, AV_LOG_DEBUG, "xscale=%f yscale=%f dmax=%f\n",
  271. s->xscale, s->yscale, s->dmax);
  272. s->fmap_linesize = FFALIGN(inlink->w, 32);
  273. s->fmap = av_malloc_array(s->fmap_linesize, inlink->h * sizeof(*s->fmap));
  274. if (!s->fmap)
  275. return AVERROR(ENOMEM);
  276. if (s->eval_mode == EVAL_MODE_INIT)
  277. update_context(s, inlink, NULL);
  278. return 0;
  279. }
  280. static const AVFilterPad vignette_inputs[] = {
  281. {
  282. .name = "default",
  283. .type = AVMEDIA_TYPE_VIDEO,
  284. .filter_frame = filter_frame,
  285. .config_props = config_props,
  286. },
  287. { NULL }
  288. };
  289. static const AVFilterPad vignette_outputs[] = {
  290. {
  291. .name = "default",
  292. .type = AVMEDIA_TYPE_VIDEO,
  293. },
  294. { NULL }
  295. };
  296. AVFilter ff_vf_vignette = {
  297. .name = "vignette",
  298. .description = NULL_IF_CONFIG_SMALL("Make or reverse a vignette effect."),
  299. .priv_size = sizeof(VignetteContext),
  300. .init = init,
  301. .uninit = uninit,
  302. .query_formats = query_formats,
  303. .inputs = vignette_inputs,
  304. .outputs = vignette_outputs,
  305. .priv_class = &vignette_class,
  306. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  307. };