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.

328 lines
13KB

  1. /*
  2. * Copyright (C) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. * Copyright (C) 2012 Clément Bœsch <u pkh me>
  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/avassert.h"
  28. #include "libavutil/avstring.h"
  29. #include "libavutil/eval.h"
  30. #include "libavutil/opt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "internal.h"
  33. typedef struct GEQContext {
  34. const AVClass *class;
  35. AVExpr *e[4]; ///< expressions for each plane
  36. char *expr_str[4+3]; ///< expression strings for each plane
  37. AVFrame *picref; ///< current input buffer
  38. int hsub, vsub; ///< chroma subsampling
  39. int planes; ///< number of planes
  40. int is_rgb;
  41. int bps;
  42. } GEQContext;
  43. enum { Y = 0, U, V, A, G, B, R };
  44. #define OFFSET(x) offsetof(GEQContext, x)
  45. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  46. static const AVOption geq_options[] = {
  47. { "lum_expr", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  48. { "lum", "set luminance expression", OFFSET(expr_str[Y]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  49. { "cb_expr", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  50. { "cb", "set chroma blue expression", OFFSET(expr_str[U]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  51. { "cr_expr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  52. { "cr", "set chroma red expression", OFFSET(expr_str[V]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  53. { "alpha_expr", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  54. { "a", "set alpha expression", OFFSET(expr_str[A]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  55. { "red_expr", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  56. { "r", "set red expression", OFFSET(expr_str[R]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  57. { "green_expr", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  58. { "g", "set green expression", OFFSET(expr_str[G]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  59. { "blue_expr", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  60. { "b", "set blue expression", OFFSET(expr_str[B]), AV_OPT_TYPE_STRING, {.str=NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  61. {NULL},
  62. };
  63. AVFILTER_DEFINE_CLASS(geq);
  64. static inline double getpix(void *priv, double x, double y, int plane)
  65. {
  66. int xi, yi;
  67. GEQContext *geq = priv;
  68. AVFrame *picref = geq->picref;
  69. const uint8_t *src = picref->data[plane];
  70. int linesize = picref->linesize[plane];
  71. const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->width, geq->hsub) : picref->width;
  72. const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(picref->height, geq->vsub) : picref->height;
  73. if (!src)
  74. return 0;
  75. xi = x = av_clipf(x, 0, w - 2);
  76. yi = y = av_clipf(y, 0, h - 2);
  77. x -= xi;
  78. y -= yi;
  79. if (geq->bps > 8) {
  80. const uint16_t *src16 = (const uint16_t*)src;
  81. linesize /= 2;
  82. return (1-y)*((1-x)*src16[xi + yi * linesize] + x*src16[xi + 1 + yi * linesize])
  83. + y *((1-x)*src16[xi + (yi+1) * linesize] + x*src16[xi + 1 + (yi+1) * linesize]);
  84. } else {
  85. return (1-y)*((1-x)*src[xi + yi * linesize] + x*src[xi + 1 + yi * linesize])
  86. + y *((1-x)*src[xi + (yi+1) * linesize] + x*src[xi + 1 + (yi+1) * linesize]);
  87. }
  88. }
  89. //TODO: cubic interpolate
  90. //TODO: keep the last few frames
  91. static double lum(void *priv, double x, double y) { return getpix(priv, x, y, 0); }
  92. static double cb(void *priv, double x, double y) { return getpix(priv, x, y, 1); }
  93. static double cr(void *priv, double x, double y) { return getpix(priv, x, y, 2); }
  94. static double alpha(void *priv, double x, double y) { return getpix(priv, x, y, 3); }
  95. static const char *const var_names[] = { "X", "Y", "W", "H", "N", "SW", "SH", "T", NULL };
  96. enum { VAR_X, VAR_Y, VAR_W, VAR_H, VAR_N, VAR_SW, VAR_SH, VAR_T, VAR_VARS_NB };
  97. static av_cold int geq_init(AVFilterContext *ctx)
  98. {
  99. GEQContext *geq = ctx->priv;
  100. int plane, ret = 0;
  101. if (!geq->expr_str[Y] && !geq->expr_str[G] && !geq->expr_str[B] && !geq->expr_str[R]) {
  102. av_log(ctx, AV_LOG_ERROR, "A luminance or RGB expression is mandatory\n");
  103. ret = AVERROR(EINVAL);
  104. goto end;
  105. }
  106. geq->is_rgb = !geq->expr_str[Y];
  107. if ((geq->expr_str[Y] || geq->expr_str[U] || geq->expr_str[V]) && (geq->expr_str[G] || geq->expr_str[B] || geq->expr_str[R])) {
  108. av_log(ctx, AV_LOG_ERROR, "Either YCbCr or RGB but not both must be specified\n");
  109. ret = AVERROR(EINVAL);
  110. goto end;
  111. }
  112. if (!geq->expr_str[U] && !geq->expr_str[V]) {
  113. /* No chroma at all: fallback on luma */
  114. geq->expr_str[U] = av_strdup(geq->expr_str[Y]);
  115. geq->expr_str[V] = av_strdup(geq->expr_str[Y]);
  116. } else {
  117. /* One chroma unspecified, fallback on the other */
  118. if (!geq->expr_str[U]) geq->expr_str[U] = av_strdup(geq->expr_str[V]);
  119. if (!geq->expr_str[V]) geq->expr_str[V] = av_strdup(geq->expr_str[U]);
  120. }
  121. if (!geq->expr_str[A]) {
  122. char bps_string[8];
  123. snprintf(bps_string, sizeof(bps_string), "%d", (1<<geq->bps) - 1);
  124. geq->expr_str[A] = av_strdup(bps_string);
  125. }
  126. if (!geq->expr_str[G])
  127. geq->expr_str[G] = av_strdup("g(X,Y)");
  128. if (!geq->expr_str[B])
  129. geq->expr_str[B] = av_strdup("b(X,Y)");
  130. if (!geq->expr_str[R])
  131. geq->expr_str[R] = av_strdup("r(X,Y)");
  132. if (geq->is_rgb ?
  133. (!geq->expr_str[G] || !geq->expr_str[B] || !geq->expr_str[R])
  134. :
  135. (!geq->expr_str[U] || !geq->expr_str[V] || !geq->expr_str[A])) {
  136. ret = AVERROR(ENOMEM);
  137. goto end;
  138. }
  139. for (plane = 0; plane < 4; plane++) {
  140. static double (*p[])(void *, double, double) = { lum, cb, cr, alpha };
  141. static const char *const func2_yuv_names[] = { "lum", "cb", "cr", "alpha", "p", NULL };
  142. static const char *const func2_rgb_names[] = { "g", "b", "r", "alpha", "p", NULL };
  143. const char *const *func2_names = geq->is_rgb ? func2_rgb_names : func2_yuv_names;
  144. double (*func2[])(void *, double, double) = { lum, cb, cr, alpha, p[plane], NULL };
  145. ret = av_expr_parse(&geq->e[plane], geq->expr_str[plane < 3 && geq->is_rgb ? plane+4 : plane], var_names,
  146. NULL, NULL, func2_names, func2, 0, ctx);
  147. if (ret < 0)
  148. break;
  149. }
  150. end:
  151. return ret;
  152. }
  153. static int geq_query_formats(AVFilterContext *ctx)
  154. {
  155. GEQContext *geq = ctx->priv;
  156. static const enum AVPixelFormat yuv_pix_fmts[] = {
  157. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P,
  158. AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV440P,
  159. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
  160. AV_PIX_FMT_GRAY8,
  161. AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
  162. AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9,
  163. AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
  164. AV_PIX_FMT_YUV440P10,
  165. AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10,
  166. AV_PIX_FMT_GRAY10,
  167. AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
  168. AV_PIX_FMT_GRAY12,
  169. AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
  170. AV_PIX_FMT_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16,
  171. AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16,
  172. AV_PIX_FMT_GRAY16,
  173. AV_PIX_FMT_NONE
  174. };
  175. static const enum AVPixelFormat rgb_pix_fmts[] = {
  176. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  177. AV_PIX_FMT_GBRP9,
  178. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  179. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  180. AV_PIX_FMT_GBRP14,
  181. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  182. AV_PIX_FMT_NONE
  183. };
  184. AVFilterFormats *fmts_list;
  185. if (geq->is_rgb) {
  186. fmts_list = ff_make_format_list(rgb_pix_fmts);
  187. } else
  188. fmts_list = ff_make_format_list(yuv_pix_fmts);
  189. if (!fmts_list)
  190. return AVERROR(ENOMEM);
  191. return ff_set_common_formats(ctx, fmts_list);
  192. }
  193. static int geq_config_props(AVFilterLink *inlink)
  194. {
  195. GEQContext *geq = inlink->dst->priv;
  196. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  197. av_assert0(desc);
  198. geq->hsub = desc->log2_chroma_w;
  199. geq->vsub = desc->log2_chroma_h;
  200. geq->planes = desc->nb_components;
  201. geq->bps = desc->comp[0].depth;
  202. return 0;
  203. }
  204. static int geq_filter_frame(AVFilterLink *inlink, AVFrame *in)
  205. {
  206. int plane;
  207. GEQContext *geq = inlink->dst->priv;
  208. AVFilterLink *outlink = inlink->dst->outputs[0];
  209. AVFrame *out;
  210. double values[VAR_VARS_NB] = {
  211. [VAR_N] = inlink->frame_count_out,
  212. [VAR_T] = in->pts == AV_NOPTS_VALUE ? NAN : in->pts * av_q2d(inlink->time_base),
  213. };
  214. geq->picref = in;
  215. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  216. if (!out) {
  217. av_frame_free(&in);
  218. return AVERROR(ENOMEM);
  219. }
  220. av_frame_copy_props(out, in);
  221. for (plane = 0; plane < geq->planes && out->data[plane]; plane++) {
  222. int x, y;
  223. uint8_t *dst = out->data[plane];
  224. uint16_t *dst16 = (uint16_t*)out->data[plane];
  225. const int linesize = out->linesize[plane];
  226. const int w = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, geq->hsub) : inlink->w;
  227. const int h = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, geq->vsub) : inlink->h;
  228. values[VAR_W] = w;
  229. values[VAR_H] = h;
  230. values[VAR_SW] = w / (double)inlink->w;
  231. values[VAR_SH] = h / (double)inlink->h;
  232. for (y = 0; y < h; y++) {
  233. values[VAR_Y] = y;
  234. if (geq->bps > 8) {
  235. for (x = 0; x < w; x++) {
  236. values[VAR_X] = x;
  237. dst16[x] = av_expr_eval(geq->e[plane], values, geq);
  238. }
  239. dst16 += linesize / 2;
  240. } else {
  241. for (x = 0; x < w; x++) {
  242. values[VAR_X] = x;
  243. dst[x] = av_expr_eval(geq->e[plane], values, geq);
  244. }
  245. dst += linesize;
  246. }
  247. }
  248. }
  249. av_frame_free(&geq->picref);
  250. return ff_filter_frame(outlink, out);
  251. }
  252. static av_cold void geq_uninit(AVFilterContext *ctx)
  253. {
  254. int i;
  255. GEQContext *geq = ctx->priv;
  256. for (i = 0; i < FF_ARRAY_ELEMS(geq->e); i++)
  257. av_expr_free(geq->e[i]);
  258. }
  259. static const AVFilterPad geq_inputs[] = {
  260. {
  261. .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .config_props = geq_config_props,
  264. .filter_frame = geq_filter_frame,
  265. },
  266. { NULL }
  267. };
  268. static const AVFilterPad geq_outputs[] = {
  269. {
  270. .name = "default",
  271. .type = AVMEDIA_TYPE_VIDEO,
  272. },
  273. { NULL }
  274. };
  275. AVFilter ff_vf_geq = {
  276. .name = "geq",
  277. .description = NULL_IF_CONFIG_SMALL("Apply generic equation to each pixel."),
  278. .priv_size = sizeof(GEQContext),
  279. .init = geq_init,
  280. .uninit = geq_uninit,
  281. .query_formats = geq_query_formats,
  282. .inputs = geq_inputs,
  283. .outputs = geq_outputs,
  284. .priv_class = &geq_class,
  285. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  286. };