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.

272 lines
9.7KB

  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+3]; ///< expression strings for each plane
  36. AVFrame *picref; ///< current input buffer
  37. int hsub, vsub; ///< chroma subsampling
  38. int planes; ///< number of planes
  39. int is_rgb;
  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. { "r", "set red expression", OFFSET(expr_str[6]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  49. { "g", "set green expression", OFFSET(expr_str[4]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  50. { "b", "set blue expression", OFFSET(expr_str[5]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  51. {NULL},
  52. };
  53. AVFILTER_DEFINE_CLASS(geq);
  54. static inline double getpix(void *priv, double x, double y, int plane)
  55. {
  56. int xi, yi;
  57. GEQContext *geq = priv;
  58. AVFrame *picref = geq->picref;
  59. const uint8_t *src = picref->data[plane];
  60. const int linesize = picref->linesize[plane];
  61. const int w = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(picref->width, geq->hsub) : picref->width;
  62. const int h = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
  63. if (!src)
  64. return 0;
  65. xi = x = av_clipf(x, 0, w - 2);
  66. yi = y = av_clipf(y, 0, h - 2);
  67. x -= xi;
  68. y -= yi;
  69. return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
  70. + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
  71. }
  72. //TODO: cubic interpolate
  73. //TODO: keep the last few frames
  74. static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
  75. static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
  76. static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
  77. static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
  78. static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
  79. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
  80. static av_cold int geq_init(AVFilterContext *ctx)
  81. {
  82. GEQContext *geq = ctx->priv;
  83. int plane, ret = 0;
  84. if (!geq->expr_str[0] && !geq->expr_str[4] && !geq->expr_str[5] && !geq->expr_str[6]) {
  85. av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
  86. ret = AVERROR(EINVAL);
  87. goto end;
  88. }
  89. geq->is_rgb = !geq->expr_str[0];
  90. if ((geq->expr_str[0] || geq->expr_str[1] || geq->expr_str[2]) && (geq->expr_str[4] || geq->expr_str[5] || geq->expr_str[6])) {
  91. av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
  92. ret = AVERROR(EINVAL);
  93. goto end;
  94. }
  95. if (!geq->expr_str[1] && !geq->expr_str[2]) {
  96. /* No chroma at all: fallback on luma */
  97. geq->expr_str[1] = av_strdup(geq->expr_str[0]);
  98. geq->expr_str[2] = av_strdup(geq->expr_str[0]);
  99. } else {
  100. /* One chroma unspecified, fallback on the other */
  101. if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
  102. if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
  103. }
  104. if (!geq->expr_str[3])
  105. geq->expr_str[3] = av_strdup("255");
  106. if (!geq->expr_str[4])
  107. geq->expr_str[4] = av_strdup("g(X,Y)");
  108. if (!geq->expr_str[5])
  109. geq->expr_str[5] = av_strdup("b(X,Y)");
  110. if (!geq->expr_str[6])
  111. geq->expr_str[6] = av_strdup("r(X,Y)");
  112. if (geq->is_rgb ?
  113. (!geq->expr_str[4] || !geq->expr_str[5] || !geq->expr_str[6])
  114. :
  115. (!geq->expr_str[1] || !geq->expr_str[2] || !geq->expr_str[3])) {
  116. ret = AVERROR(ENOMEM);
  117. goto end;
  118. }
  119. for (plane = 0; plane < 4; plane++) {
  120. static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
  121. static const char *const func2_yuv_names[] = { "lum", "cb", "cr", "alpha", "p", NULL };
  122. static const char *const func2_rgb_names[] = { "g", "b", "r", "alpha", "p", NULL };
  123. const char *const *func2_names = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
  124. double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
  125. ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
  126. NULL, NULL, func2_names, func2, 0, ctx);
  127. if (ret < 0)
  128. break;
  129. }
  130. end:
  131. return ret;
  132. }
  133. static int geq_query_formats(AVFilterContext *ctx)
  134. {
  135. GEQContext *geq = ctx->priv;
  136. static const enum PixelFormat yuv_pix_fmts[] = {
  137. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  138. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  139. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  140. AV_PIX_FMT_GRAY8,
  141. AV_PIX_FMT_NONE
  142. };
  143. static const enum PixelFormat rgb_pix_fmts[] = {
  144. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  145. AV_PIX_FMT_NONE
  146. };
  147. if (geq->is_rgb) {
  148. ff_set_common_formats(ctx, ff_make_format_list(rgb_pix_fmts));
  149. } else
  150. ff_set_common_formats(ctx, ff_make_format_list(yuv_pix_fmts));
  151. return 0;
  152. }
  153. static int geq_config_props(AVFilterLink *inlink)
  154. {
  155. GEQContext *geq = inlink->dst->priv;
  156. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  157. geq->hsub = desc->log2_chroma_w;
  158. geq->vsub = desc->log2_chroma_h;
  159. geq->planes = desc->nb_components;
  160. return 0;
  161. }
  162. static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
  163. {
  164. int plane;
  165. GEQContext *geq = inlink->dst->priv;
  166. AVFilterLink *outlink = inlink->dst->outputs[0];
  167. AVFrame *out;
  168. double values[VAR_VARS_NB] = {
  169. [VAR_N] = inlink->frame_count,
  170. [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
  171. };
  172. geq->picref = in;
  173. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  174. if (!out) {
  175. av_frame_free(&in);
  176. return AVERROR(ENOMEM);
  177. }
  178. av_frame_copy_props(out, in);
  179. for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
  180. int x, y;
  181. uint8_t *dst = out->data[plane];
  182. const int linesize = out->linesize[plane];
  183. const int w = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
  184. const int h = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
  185. values[VAR_W] = w;
  186. values[VAR_H] = h;
  187. values[VAR_SW] = w / (double)inlink->w;
  188. values[VAR_SH] = h / (double)inlink->h;
  189. for (y = 0; y < h; y++) {
  190. values[VAR_Y] = y;
  191. for (x = 0; x < w; x++) {
  192. values[VAR_X] = x;
  193. dst[x] = av_expr_eval(geq->e[plane], values, geq);
  194. }
  195. dst += linesize;
  196. }
  197. }
  198. av_frame_free(&geq->picref);
  199. return ff_filter_frame(outlink, out);
  200. }
  201. static av_cold void geq_uninit(AVFilterContext *ctx)
  202. {
  203. int i;
  204. GEQContext *geq = ctx->priv;
  205. for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
  206. av_expr_free(geq->e[i]);
  207. }
  208. static const AVFilterPad geq_inputs[] = {
  209. {
  210. .name = "default",
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .config_props = geq_config_props,
  213. .filter_frame = geq_filter_frame,
  214. },
  215. { NULL }
  216. };
  217. static const AVFilterPad geq_outputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. },
  222. { NULL }
  223. };
  224. AVFilter avfilter_vf_geq = {
  225. .name = "geq",
  226. .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
  227. .priv_size = sizeof(GEQContext),
  228. .init = geq_init,
  229. .uninit = geq_uninit,
  230. .query_formats = geq_query_formats,
  231. .inputs = geq_inputs,
  232. .outputs = geq_outputs,
  233. .priv_class = &geq_class,
  234. };