vf_geq.c 14 KB

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