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.

250 lines
8.3KB

  1. /*
  2. * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (C) 2012 Clément Bœsch <ubitux@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. /**
  22. * @file
  23. * Generic equation change filter
  24. * Originally written by Michael Niedermayer for the MPlayer project, and
  25. * ported by Clément Bœsch for FFmpeg.
  26. */
  27. #include "libavutil/avstring.h"
  28. #include "libavutil/eval.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/pixdesc.h"
  31. #include "internal.h"
  32. typedef struct {
  33. const AVClass *class;
  34. AVExpr *e[4]; ///< expressions for each plane
  35. char *expr_str[4]; ///< expression strings for each plane
  36. int framenum; ///< frame counter
  37. AVFrame *picref; ///< current input buffer
  38. int hsub, vsub; ///< chroma subsampling
  39. int planes; ///< number of planes
  40. } GEQContext;
  41. #define OFFSET(x) offsetof(GEQContext, x)
  42. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  43. static const AVOption geq_options[] = {
  44. { "lum_expr", "set luminance expression", OFFSET(expr_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  45. { "cb_expr", "set chroma blue expression", OFFSET(expr_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  46. { "cr_expr", "set chroma red expression", OFFSET(expr_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  47. { "alpha_expr", "set alpha expression", OFFSET(expr_str[3]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  48. {NULL},
  49. };
  50. AVFILTER_DEFINE_CLASS(geq);
  51. static inline double getpix(void *priv, double x, double y, int plane)
  52. {
  53. int xi, yi;
  54. GEQContext *geq = priv;
  55. AVFrame *picref = geq->picref;
  56. const uint8_t *src = picref->data[plane];
  57. const int linesize = picref->linesize[plane];
  58. const int w = picref->width >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
  59. const int h = picref->height >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
  60. if (!src)
  61. return 0;
  62. xi = x = av_clipf(x, 0, w - 2);
  63. yi = y = av_clipf(y, 0, h - 2);
  64. x -= xi;
  65. y -= yi;
  66. return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
  67. + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
  68. }
  69. //TODO: cubic interpolate
  70. //TODO: keep the last few frames
  71. static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
  72. static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
  73. static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
  74. static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
  75. static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
  76. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
  77. static av_cold int geq_init(AVFilterContext *ctx, const char *args)
  78. {
  79. GEQContext *geq = ctx->priv;
  80. int plane, ret = 0;
  81. static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", "alpha_expr", NULL };
  82. geq->class = &geq_class;
  83. av_opt_set_defaults(geq);
  84. if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
  85. return ret;
  86. if (!geq->expr_str[0]) {
  87. av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
  88. ret = AVERROR(EINVAL);
  89. goto end;
  90. }
  91. if (!geq->expr_str[1] && !geq->expr_str[2]) {
  92. /* No chroma at all: fallback on luma */
  93. geq->expr_str[1] = av_strdup(geq->expr_str[0]);
  94. geq->expr_str[2] = av_strdup(geq->expr_str[0]);
  95. } else {
  96. /* One chroma unspecified, fallback on the other */
  97. if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
  98. if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
  99. }
  100. if (!geq->expr_str[3])
  101. geq->expr_str[3] = av_strdup("255");
  102. if (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3]) {
  103. ret = AVERROR(ENOMEM);
  104. goto end;
  105. }
  106. for (plane = 0; plane < 4; plane++) {
  107. static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
  108. static const char *const func2_names[] = { "lum", "cb", "cr", "alpha", "p", NULL };
  109. double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
  110. ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
  111. NULL, NULL, func2_names, func2, 0, ctx);
  112. if (ret < 0)
  113. break;
  114. }
  115. end:
  116. return ret;
  117. }
  118. static int geq_query_formats(AVFilterContext *ctx)
  119. {
  120. static const enum PixelFormat pix_fmts[] = {
  121. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  122. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  123. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  124. AV_PIX_FMT_GRAY8,
  125. AV_PIX_FMT_NONE
  126. };
  127. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  128. return 0;
  129. }
  130. static int geq_config_props(AVFilterLink *inlink)
  131. {
  132. GEQContext *geq = inlink->dst->priv;
  133. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  134. geq->hsub = desc->log2_chroma_w;
  135. geq->vsub = desc->log2_chroma_h;
  136. geq->planes = desc->nb_components;
  137. return 0;
  138. }
  139. static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
  140. {
  141. int plane;
  142. GEQContext *geq = inlink->dst->priv;
  143. AVFilterLink *outlink = inlink->dst->outputs[0];
  144. AVFrame *out;
  145. double values[VAR_VARS_NB] = {
  146. [VAR_N] = geq->framenum++,
  147. [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
  148. };
  149. geq->picref = in;
  150. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  151. if (!out) {
  152. av_frame_free(&in);
  153. return AVERROR(ENOMEM);
  154. }
  155. av_frame_copy_props(out, in);
  156. for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
  157. int x, y;
  158. uint8_t *dst = out->data[plane];
  159. const int linesize = out->linesize[plane];
  160. const int w = inlink->w >> ((plane == 1 || plane == 2) ? geq->hsub : 0);
  161. const int h = inlink->h >> ((plane == 1 || plane == 2) ? geq->vsub : 0);
  162. values[VAR_W] = w;
  163. values[VAR_H] = h;
  164. values[VAR_SW] = w / (double)inlink->w;
  165. values[VAR_SH] = h / (double)inlink->h;
  166. for (y = 0; y < h; y++) {
  167. values[VAR_Y] = y;
  168. for (x = 0; x < w; x++) {
  169. values[VAR_X] = x;
  170. dst[x] = av_expr_eval(geq->e[plane], values, geq);
  171. }
  172. dst += linesize;
  173. }
  174. }
  175. av_frame_free(&geq->picref);
  176. return ff_filter_frame(outlink, out);
  177. }
  178. static av_cold void geq_uninit(AVFilterContext *ctx)
  179. {
  180. int i;
  181. GEQContext *geq = ctx->priv;
  182. for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
  183. av_expr_free(geq->e[i]);
  184. av_opt_free(geq);
  185. }
  186. static const AVFilterPad geq_inputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .config_props = geq_config_props,
  191. .filter_frame = geq_filter_frame,
  192. },
  193. { NULL }
  194. };
  195. static const AVFilterPad geq_outputs[] = {
  196. {
  197. .name = "default",
  198. .type = AVMEDIA_TYPE_VIDEO,
  199. },
  200. { NULL }
  201. };
  202. AVFilter avfilter_vf_geq = {
  203. .name = "geq",
  204. .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
  205. .priv_size = sizeof(GEQContext),
  206. .init = geq_init,
  207. .uninit = geq_uninit,
  208. .query_formats = geq_query_formats,
  209. .inputs = geq_inputs,
  210. .outputs = geq_outputs,
  211. .priv_class = &geq_class,
  212. };