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.

240 lines
7.8KB

  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[3]; ///< expressions for each plane
  35. char *expr_str[3]; ///< expression strings for each plane
  36. int framenum; ///< frame counter
  37. AVFilterBufferRef *picref; ///< current input buffer
  38. int hsub, vsub; ///< chroma subsampling
  39. } GEQContext;
  40. #define OFFSET(x) offsetof(GEQContext, x)
  41. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  42. static const AVOption geq_options[] = {
  43. { "lum_expr", "set luminance expression", OFFSET(expr_str), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  44. { "cb_expr", "set chroma blue expression", OFFSET(expr_str) + sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  45. { "cr_expr", "set chroma red expression", OFFSET(expr_str) + 2*sizeof(char*), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  46. {NULL},
  47. };
  48. AVFILTER_DEFINE_CLASS(geq);
  49. static inline double getpix(void *priv, double x, double y, int plane)
  50. {
  51. int xi, yi;
  52. GEQContext *geq = priv;
  53. AVFilterBufferRef *picref = geq->picref;
  54. const uint8_t *src = picref->data[plane];
  55. const int linesize = picref->linesize[plane];
  56. const int w = picref->video->w >> (plane ? geq->hsub : 0);
  57. const int h = picref->video->h >> (plane ? geq->vsub : 0);
  58. xi = x = av_clipf(x, 0, w - 2);
  59. yi = y = av_clipf(y, 0, h - 2);
  60. x -= xi;
  61. y -= yi;
  62. return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
  63. + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
  64. }
  65. //TODO: cubic interpolate
  66. //TODO: keep the last few frames
  67. static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
  68. static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
  69. static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
  70. static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
  71. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
  72. static av_cold int geq_init(AVFilterContext *ctx, const char *args)
  73. {
  74. GEQContext *geq = ctx->priv;
  75. int plane, ret = 0;
  76. static const char *shorthand[] = { "lum_expr", "cb_expr", "cr_expr", NULL };
  77. geq->class = &geq_class;
  78. av_opt_set_defaults(geq);
  79. if ((ret = av_opt_set_from_string(geq, args, shorthand, "=", ":")) < 0)
  80. return ret;
  81. if (!geq->expr_str[0]) {
  82. av_log(ctx, AV_LOG_ERROR, "Luminance expression is mandatory\n");
  83. ret = AVERROR(EINVAL);
  84. goto end;
  85. }
  86. if (!geq->expr_str[1] && !geq->expr_str[2]) {
  87. /* No chroma at all: fallback on luma */
  88. geq->expr_str[1] = av_strdup(geq->expr_str[0]);
  89. geq->expr_str[2] = av_strdup(geq->expr_str[0]);
  90. } else {
  91. /* One chroma unspecified, fallback on the other */
  92. if (!geq->expr_str[1]) geq->expr_str[1] = av_strdup(geq->expr_str[2]);
  93. if (!geq->expr_str[2]) geq->expr_str[2] = av_strdup(geq->expr_str[1]);
  94. }
  95. if (!geq->expr_str[1] || !geq->expr_str[2]) {
  96. ret = AVERROR(ENOMEM);
  97. goto end;
  98. }
  99. for (plane = 0; plane < 3; plane++) {
  100. static double (*p[])(void *, double, double) = { lum, cb, cr };
  101. static const char *const func2_names[] = { "lum", "cb", "cr", "p", NULL };
  102. double (*func2[])(void *, double, double) = { lum, cb, cr, p[plane], NULL };
  103. ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane], var_names,
  104. NULL, NULL, func2_names, func2, 0, ctx);
  105. if (ret < 0)
  106. break;
  107. }
  108. end:
  109. return ret;
  110. }
  111. static int geq_query_formats(AVFilterContext *ctx)
  112. {
  113. static const enum PixelFormat pix_fmts[] = {
  114. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  115. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  116. AV_PIX_FMT_YUVA420P,
  117. AV_PIX_FMT_NONE
  118. };
  119. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  120. return 0;
  121. }
  122. static int geq_config_props(AVFilterLink *inlink)
  123. {
  124. GEQContext *geq = inlink->dst->priv;
  125. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  126. geq->hsub = desc->log2_chroma_w;
  127. geq->vsub = desc->log2_chroma_h;
  128. return 0;
  129. }
  130. static int geq_filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
  131. {
  132. int plane;
  133. GEQContext *geq = inlink->dst->priv;
  134. AVFilterLink *outlink = inlink->dst->outputs[0];
  135. AVFilterBufferRef *out;
  136. double values[VAR_VARS_NB] = {
  137. [VAR_N] = geq->framenum++,
  138. [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
  139. };
  140. geq->picref = in;
  141. out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
  142. if (!out) {
  143. avfilter_unref_bufferp(&in);
  144. return AVERROR(ENOMEM);
  145. }
  146. avfilter_copy_buffer_ref_props(out, in);
  147. for (plane = 0; plane < 3; plane++) {
  148. int x, y;
  149. uint8_t *dst = out->data[plane];
  150. const int linesize = out->linesize[plane];
  151. const int w = inlink->w >> (plane ? geq->hsub : 0);
  152. const int h = inlink->h >> (plane ? geq->vsub : 0);
  153. values[VAR_W] = w;
  154. values[VAR_H] = h;
  155. values[VAR_SW] = w / (double)inlink->w;
  156. values[VAR_SH] = h / (double)inlink->h;
  157. for (y = 0; y < h; y++) {
  158. values[VAR_Y] = y;
  159. for (x = 0; x < w; x++) {
  160. values[VAR_X] = x;
  161. dst[x] = av_expr_eval(geq->e[plane], values, geq);
  162. }
  163. dst += linesize;
  164. }
  165. }
  166. avfilter_unref_bufferp(&geq->picref);
  167. return ff_filter_frame(outlink, out);
  168. }
  169. static av_cold void geq_uninit(AVFilterContext *ctx)
  170. {
  171. int i;
  172. GEQContext *geq = ctx->priv;
  173. for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
  174. av_expr_free(geq->e[i]);
  175. av_opt_free(geq);
  176. }
  177. static const AVFilterPad geq_inputs[] = {
  178. {
  179. .name = "default",
  180. .type = AVMEDIA_TYPE_VIDEO,
  181. .config_props = geq_config_props,
  182. .filter_frame = geq_filter_frame,
  183. .min_perms = AV_PERM_READ,
  184. },
  185. { NULL }
  186. };
  187. static const AVFilterPad geq_outputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. },
  192. { NULL }
  193. };
  194. AVFilter avfilter_vf_geq = {
  195. .name = "geq",
  196. .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
  197. .priv_size = sizeof(GEQContext),
  198. .init = geq_init,
  199. .uninit = geq_uninit,
  200. .query_formats = geq_query_formats,
  201. .inputs = geq_inputs,
  202. .outputs = geq_outputs,
  203. .priv_class = &geq_class,
  204. };