vf_geq.c 15 KB

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