vf_curves.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright (c) 2013 Clément Bœsch
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "libavutil/bprint.h"
  22. #include "libavutil/eval.h"
  23. #include "libavutil/file.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avfilter.h"
  28. #include "drawutils.h"
  29. #include "formats.h"
  30. #include "internal.h"
  31. #include "video.h"
  32. #define R 0
  33. #define G 1
  34. #define B 2
  35. #define A 3
  36. struct keypoint {
  37. double x, y;
  38. struct keypoint *next;
  39. };
  40. #define NB_COMP 3
  41. enum preset {
  42. PRESET_NONE,
  43. PRESET_COLOR_NEGATIVE,
  44. PRESET_CROSS_PROCESS,
  45. PRESET_DARKER,
  46. PRESET_INCREASE_CONTRAST,
  47. PRESET_LIGHTER,
  48. PRESET_LINEAR_CONTRAST,
  49. PRESET_MEDIUM_CONTRAST,
  50. PRESET_NEGATIVE,
  51. PRESET_STRONG_CONTRAST,
  52. PRESET_VINTAGE,
  53. NB_PRESETS,
  54. };
  55. typedef struct CurvesContext {
  56. const AVClass *class;
  57. int preset;
  58. char *comp_points_str[NB_COMP + 1];
  59. char *comp_points_str_all;
  60. uint16_t *graph[NB_COMP + 1];
  61. int lut_size;
  62. char *psfile;
  63. uint8_t rgba_map[4];
  64. int step;
  65. char *plot_filename;
  66. int is_16bit;
  67. int depth;
  68. int (*filter_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
  69. } CurvesContext;
  70. typedef struct ThreadData {
  71. AVFrame *in, *out;
  72. } ThreadData;
  73. #define OFFSET(x) offsetof(CurvesContext, x)
  74. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  75. static const AVOption curves_options[] = {
  76. { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
  77. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  78. { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  79. { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  80. { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  81. { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  82. { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  83. { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  84. { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  85. { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  86. { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  87. { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  88. { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  89. { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  90. { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  91. { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  92. { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  93. { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  94. { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  95. { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  96. { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  97. { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  98. { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  99. { NULL }
  100. };
  101. AVFILTER_DEFINE_CLASS(curves);
  102. static const struct {
  103. const char *r;
  104. const char *g;
  105. const char *b;
  106. const char *master;
  107. } curves_presets[] = {
  108. [PRESET_COLOR_NEGATIVE] = {
  109. "0.129/1 0.466/0.498 0.725/0",
  110. "0.109/1 0.301/0.498 0.517/0",
  111. "0.098/1 0.235/0.498 0.423/0",
  112. },
  113. [PRESET_CROSS_PROCESS] = {
  114. "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
  115. "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
  116. "0/0 0.231/0.094 0.709/0.874 1/1",
  117. },
  118. [PRESET_DARKER] = { .master = "0/0 0.5/0.4 1/1" },
  119. [PRESET_INCREASE_CONTRAST] = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
  120. [PRESET_LIGHTER] = { .master = "0/0 0.4/0.5 1/1" },
  121. [PRESET_LINEAR_CONTRAST] = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
  122. [PRESET_MEDIUM_CONTRAST] = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
  123. [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
  124. [PRESET_STRONG_CONTRAST] = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
  125. [PRESET_VINTAGE] = {
  126. "0/0.11 0.42/0.51 1/0.95",
  127. "0/0 0.50/0.48 1/1",
  128. "0/0.22 0.49/0.44 1/0.8",
  129. }
  130. };
  131. static struct keypoint *make_point(double x, double y, struct keypoint *next)
  132. {
  133. struct keypoint *point = av_mallocz(sizeof(*point));
  134. if (!point)
  135. return NULL;
  136. point->x = x;
  137. point->y = y;
  138. point->next = next;
  139. return point;
  140. }
  141. static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
  142. int lut_size)
  143. {
  144. char *p = (char *)s; // strtod won't alter the string
  145. struct keypoint *last = NULL;
  146. const int scale = lut_size - 1;
  147. /* construct a linked list based on the key points string */
  148. while (p && *p) {
  149. struct keypoint *point = make_point(0, 0, NULL);
  150. if (!point)
  151. return AVERROR(ENOMEM);
  152. point->x = av_strtod(p, &p); if (p && *p) p++;
  153. point->y = av_strtod(p, &p); if (p && *p) p++;
  154. if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
  155. av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
  156. "x and y must be in the [0;1] range.\n", point->x, point->y);
  157. return AVERROR(EINVAL);
  158. }
  159. if (!*points)
  160. *points = point;
  161. if (last) {
  162. if ((int)(last->x * scale) >= (int)(point->x * scale)) {
  163. av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
  164. "and (%f;%f) are too close from each other or not "
  165. "strictly increasing on the x-axis\n",
  166. last->x, last->y, point->x, point->y);
  167. return AVERROR(EINVAL);
  168. }
  169. last->next = point;
  170. }
  171. last = point;
  172. }
  173. if (*points && !(*points)->next) {
  174. av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
  175. "this is unlikely to behave as you expect. You probably want"
  176. "at least 2 points.",
  177. (*points)->x, (*points)->y);
  178. }
  179. return 0;
  180. }
  181. static int get_nb_points(const struct keypoint *d)
  182. {
  183. int n = 0;
  184. while (d) {
  185. n++;
  186. d = d->next;
  187. }
  188. return n;
  189. }
  190. /**
  191. * Natural cubic spline interpolation
  192. * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  193. * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  194. */
  195. #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uintp2_c(v, nbits))
  196. static inline int interpolate(void *log_ctx, uint16_t *y,
  197. const struct keypoint *points, int nbits)
  198. {
  199. int i, ret = 0;
  200. const struct keypoint *point = points;
  201. double xprev = 0;
  202. const int lut_size = 1<<nbits;
  203. const int scale = lut_size - 1;
  204. double (*matrix)[3];
  205. double *h, *r;
  206. const int n = get_nb_points(points); // number of splines
  207. if (n == 0) {
  208. for (i = 0; i < lut_size; i++)
  209. y[i] = i;
  210. return 0;
  211. }
  212. if (n == 1) {
  213. for (i = 0; i < lut_size; i++)
  214. y[i] = CLIP(point->y * scale);
  215. return 0;
  216. }
  217. matrix = av_calloc(n, sizeof(*matrix));
  218. h = av_malloc((n - 1) * sizeof(*h));
  219. r = av_calloc(n, sizeof(*r));
  220. if (!matrix || !h || !r) {
  221. ret = AVERROR(ENOMEM);
  222. goto end;
  223. }
  224. /* h(i) = x(i+1) - x(i) */
  225. i = -1;
  226. for (point = points; point; point = point->next) {
  227. if (i != -1)
  228. h[i] = point->x - xprev;
  229. xprev = point->x;
  230. i++;
  231. }
  232. /* right-side of the polynomials, will be modified to contains the solution */
  233. point = points;
  234. for (i = 1; i < n - 1; i++) {
  235. const double yp = point->y;
  236. const double yc = point->next->y;
  237. const double yn = point->next->next->y;
  238. r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
  239. point = point->next;
  240. }
  241. #define BD 0 /* sub diagonal (below main) */
  242. #define MD 1 /* main diagonal (center) */
  243. #define AD 2 /* sup diagonal (above main) */
  244. /* left side of the polynomials into a tridiagonal matrix. */
  245. matrix[0][MD] = matrix[n - 1][MD] = 1;
  246. for (i = 1; i < n - 1; i++) {
  247. matrix[i][BD] = h[i-1];
  248. matrix[i][MD] = 2 * (h[i-1] + h[i]);
  249. matrix[i][AD] = h[i];
  250. }
  251. /* tridiagonal solving of the linear system */
  252. for (i = 1; i < n; i++) {
  253. const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
  254. const double k = den ? 1./den : 1.;
  255. matrix[i][AD] *= k;
  256. r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
  257. }
  258. for (i = n - 2; i >= 0; i--)
  259. r[i] = r[i] - matrix[i][AD] * r[i + 1];
  260. point = points;
  261. /* left padding */
  262. for (i = 0; i < (int)(point->x * scale); i++)
  263. y[i] = CLIP(point->y * scale);
  264. /* compute the graph with x=[x0..xN] */
  265. i = 0;
  266. av_assert0(point->next); // always at least 2 key points
  267. while (point->next) {
  268. const double yc = point->y;
  269. const double yn = point->next->y;
  270. const double a = yc;
  271. const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
  272. const double c = r[i] / 2.;
  273. const double d = (r[i+1] - r[i]) / (6.*h[i]);
  274. int x;
  275. const int x_start = point->x * scale;
  276. const int x_end = point->next->x * scale;
  277. av_assert0(x_start >= 0 && x_start < lut_size &&
  278. x_end >= 0 && x_end < lut_size);
  279. for (x = x_start; x <= x_end; x++) {
  280. const double xx = (x - x_start) * 1./scale;
  281. const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
  282. y[x] = CLIP(yy * scale);
  283. av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
  284. }
  285. point = point->next;
  286. i++;
  287. }
  288. /* right padding */
  289. for (i = (int)(point->x * scale); i < lut_size; i++)
  290. y[i] = CLIP(point->y * scale);
  291. end:
  292. av_free(matrix);
  293. av_free(h);
  294. av_free(r);
  295. return ret;
  296. }
  297. #define DECLARE_INTERPOLATE_FUNC(nbits) \
  298. static int interpolate##nbits(void *log_ctx, uint16_t *y, \
  299. const struct keypoint *points) \
  300. { \
  301. return interpolate(log_ctx, y, points, nbits); \
  302. }
  303. DECLARE_INTERPOLATE_FUNC(8)
  304. DECLARE_INTERPOLATE_FUNC(9)
  305. DECLARE_INTERPOLATE_FUNC(10)
  306. DECLARE_INTERPOLATE_FUNC(12)
  307. DECLARE_INTERPOLATE_FUNC(14)
  308. DECLARE_INTERPOLATE_FUNC(16)
  309. static int parse_psfile(AVFilterContext *ctx, const char *fname)
  310. {
  311. CurvesContext *curves = ctx->priv;
  312. uint8_t *buf;
  313. size_t size;
  314. int i, ret, av_unused(version), nb_curves;
  315. AVBPrint ptstr;
  316. static const int comp_ids[] = {3, 0, 1, 2};
  317. av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
  318. ret = av_file_map(fname, &buf, &size, 0, NULL);
  319. if (ret < 0)
  320. return ret;
  321. #define READ16(dst) do { \
  322. if (size < 2) { \
  323. ret = AVERROR_INVALIDDATA; \
  324. goto end; \
  325. } \
  326. dst = AV_RB16(buf); \
  327. buf += 2; \
  328. size -= 2; \
  329. } while (0)
  330. READ16(version);
  331. READ16(nb_curves);
  332. for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
  333. int nb_points, n;
  334. av_bprint_clear(&ptstr);
  335. READ16(nb_points);
  336. for (n = 0; n < nb_points; n++) {
  337. int y, x;
  338. READ16(y);
  339. READ16(x);
  340. av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
  341. }
  342. if (*ptstr.str) {
  343. char **pts = &curves->comp_points_str[comp_ids[i]];
  344. if (!*pts) {
  345. *pts = av_strdup(ptstr.str);
  346. av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
  347. i, comp_ids[i], nb_points, *pts);
  348. if (!*pts) {
  349. ret = AVERROR(ENOMEM);
  350. goto end;
  351. }
  352. }
  353. }
  354. }
  355. end:
  356. av_bprint_finalize(&ptstr, NULL);
  357. av_file_unmap(buf, size);
  358. return ret;
  359. }
  360. static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
  361. struct keypoint *comp_points[NB_COMP + 1],
  362. int lut_size)
  363. {
  364. int i;
  365. AVBPrint buf;
  366. const double scale = 1. / (lut_size - 1);
  367. static const char * const colors[] = { "red", "green", "blue", "#404040", };
  368. FILE *f = av_fopen_utf8(fname, "w");
  369. av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
  370. if (!f) {
  371. int ret = AVERROR(errno);
  372. av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
  373. fname, av_err2str(ret));
  374. return ret;
  375. }
  376. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  377. av_bprintf(&buf, "set xtics 0.1\n");
  378. av_bprintf(&buf, "set ytics 0.1\n");
  379. av_bprintf(&buf, "set size square\n");
  380. av_bprintf(&buf, "set grid\n");
  381. for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
  382. av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
  383. i ? ", " : "plot ", colors[i]);
  384. if (comp_points[i])
  385. av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
  386. colors[i]);
  387. }
  388. av_bprintf(&buf, "\n");
  389. for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
  390. int x;
  391. /* plot generated values */
  392. for (x = 0; x < lut_size; x++)
  393. av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
  394. av_bprintf(&buf, "e\n");
  395. /* plot user knots */
  396. if (comp_points[i]) {
  397. const struct keypoint *point = comp_points[i];
  398. while (point) {
  399. av_bprintf(&buf, "%f %f\n", point->x, point->y);
  400. point = point->next;
  401. }
  402. av_bprintf(&buf, "e\n");
  403. }
  404. }
  405. fwrite(buf.str, 1, buf.len, f);
  406. fclose(f);
  407. av_bprint_finalize(&buf, NULL);
  408. return 0;
  409. }
  410. static av_cold int curves_init(AVFilterContext *ctx)
  411. {
  412. int i, ret;
  413. CurvesContext *curves = ctx->priv;
  414. char **pts = curves->comp_points_str;
  415. const char *allp = curves->comp_points_str_all;
  416. //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
  417. // allp = curves_presets[curves->preset].all;
  418. if (allp) {
  419. for (i = 0; i < NB_COMP; i++) {
  420. if (!pts[i])
  421. pts[i] = av_strdup(allp);
  422. if (!pts[i])
  423. return AVERROR(ENOMEM);
  424. }
  425. }
  426. if (curves->psfile) {
  427. ret = parse_psfile(ctx, curves->psfile);
  428. if (ret < 0)
  429. return ret;
  430. }
  431. if (curves->preset != PRESET_NONE) {
  432. #define SET_COMP_IF_NOT_SET(n, name) do { \
  433. if (!pts[n] && curves_presets[curves->preset].name) { \
  434. pts[n] = av_strdup(curves_presets[curves->preset].name); \
  435. if (!pts[n]) \
  436. return AVERROR(ENOMEM); \
  437. } \
  438. } while (0)
  439. SET_COMP_IF_NOT_SET(0, r);
  440. SET_COMP_IF_NOT_SET(1, g);
  441. SET_COMP_IF_NOT_SET(2, b);
  442. SET_COMP_IF_NOT_SET(3, master);
  443. }
  444. return 0;
  445. }
  446. static int query_formats(AVFilterContext *ctx)
  447. {
  448. static const enum AVPixelFormat pix_fmts[] = {
  449. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  450. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  451. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  452. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  453. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  454. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  455. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  456. AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
  457. AV_PIX_FMT_GBRP9,
  458. AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRAP10,
  459. AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRAP12,
  460. AV_PIX_FMT_GBRP14,
  461. AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
  462. AV_PIX_FMT_NONE
  463. };
  464. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  465. if (!fmts_list)
  466. return AVERROR(ENOMEM);
  467. return ff_set_common_formats(ctx, fmts_list);
  468. }
  469. static int filter_slice_packed(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  470. {
  471. int x, y;
  472. const CurvesContext *curves = ctx->priv;
  473. const ThreadData *td = arg;
  474. const AVFrame *in = td->in;
  475. const AVFrame *out = td->out;
  476. const int direct = out == in;
  477. const int step = curves->step;
  478. const uint8_t r = curves->rgba_map[R];
  479. const uint8_t g = curves->rgba_map[G];
  480. const uint8_t b = curves->rgba_map[B];
  481. const uint8_t a = curves->rgba_map[A];
  482. const int slice_start = (in->height * jobnr ) / nb_jobs;
  483. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  484. if (curves->is_16bit) {
  485. for (y = slice_start; y < slice_end; y++) {
  486. uint16_t *dstp = ( uint16_t *)(out->data[0] + y * out->linesize[0]);
  487. const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y * in->linesize[0]);
  488. for (x = 0; x < in->width * step; x += step) {
  489. dstp[x + r] = curves->graph[R][srcp[x + r]];
  490. dstp[x + g] = curves->graph[G][srcp[x + g]];
  491. dstp[x + b] = curves->graph[B][srcp[x + b]];
  492. if (!direct && step == 4)
  493. dstp[x + a] = srcp[x + a];
  494. }
  495. }
  496. } else {
  497. uint8_t *dst = out->data[0] + slice_start * out->linesize[0];
  498. const uint8_t *src = in->data[0] + slice_start * in->linesize[0];
  499. for (y = slice_start; y < slice_end; y++) {
  500. for (x = 0; x < in->width * step; x += step) {
  501. dst[x + r] = curves->graph[R][src[x + r]];
  502. dst[x + g] = curves->graph[G][src[x + g]];
  503. dst[x + b] = curves->graph[B][src[x + b]];
  504. if (!direct && step == 4)
  505. dst[x + a] = src[x + a];
  506. }
  507. dst += out->linesize[0];
  508. src += in ->linesize[0];
  509. }
  510. }
  511. return 0;
  512. }
  513. static int filter_slice_planar(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  514. {
  515. int x, y;
  516. const CurvesContext *curves = ctx->priv;
  517. const ThreadData *td = arg;
  518. const AVFrame *in = td->in;
  519. const AVFrame *out = td->out;
  520. const int direct = out == in;
  521. const int step = curves->step;
  522. const uint8_t r = curves->rgba_map[R];
  523. const uint8_t g = curves->rgba_map[G];
  524. const uint8_t b = curves->rgba_map[B];
  525. const uint8_t a = curves->rgba_map[A];
  526. const int slice_start = (in->height * jobnr ) / nb_jobs;
  527. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  528. if (curves->is_16bit) {
  529. for (y = slice_start; y < slice_end; y++) {
  530. uint16_t *dstrp = ( uint16_t *)(out->data[r] + y * out->linesize[r]);
  531. uint16_t *dstgp = ( uint16_t *)(out->data[g] + y * out->linesize[g]);
  532. uint16_t *dstbp = ( uint16_t *)(out->data[b] + y * out->linesize[b]);
  533. uint16_t *dstap = ( uint16_t *)(out->data[a] + y * out->linesize[a]);
  534. const uint16_t *srcrp = (const uint16_t *)(in ->data[r] + y * in->linesize[r]);
  535. const uint16_t *srcgp = (const uint16_t *)(in ->data[g] + y * in->linesize[g]);
  536. const uint16_t *srcbp = (const uint16_t *)(in ->data[b] + y * in->linesize[b]);
  537. const uint16_t *srcap = (const uint16_t *)(in ->data[a] + y * in->linesize[a]);
  538. for (x = 0; x < in->width; x++) {
  539. dstrp[x] = curves->graph[R][srcrp[x]];
  540. dstgp[x] = curves->graph[G][srcgp[x]];
  541. dstbp[x] = curves->graph[B][srcbp[x]];
  542. if (!direct && step == 4)
  543. dstap[x] = srcap[x];
  544. }
  545. }
  546. } else {
  547. uint8_t *dstr = out->data[r] + slice_start * out->linesize[r];
  548. uint8_t *dstg = out->data[g] + slice_start * out->linesize[g];
  549. uint8_t *dstb = out->data[b] + slice_start * out->linesize[b];
  550. uint8_t *dsta = out->data[a] + slice_start * out->linesize[a];
  551. const uint8_t *srcr = in->data[r] + slice_start * in->linesize[r];
  552. const uint8_t *srcg = in->data[g] + slice_start * in->linesize[g];
  553. const uint8_t *srcb = in->data[b] + slice_start * in->linesize[b];
  554. const uint8_t *srca = in->data[a] + slice_start * in->linesize[a];
  555. for (y = slice_start; y < slice_end; y++) {
  556. for (x = 0; x < in->width; x++) {
  557. dstr[x] = curves->graph[R][srcr[x]];
  558. dstg[x] = curves->graph[G][srcg[x]];
  559. dstb[x] = curves->graph[B][srcb[x]];
  560. if (!direct && step == 4)
  561. dsta[x] = srca[x];
  562. }
  563. dstr += out->linesize[r];
  564. dstg += out->linesize[g];
  565. dstb += out->linesize[b];
  566. dsta += out->linesize[a];
  567. srcr += in ->linesize[r];
  568. srcg += in ->linesize[g];
  569. srcb += in ->linesize[b];
  570. srca += in ->linesize[a];
  571. }
  572. }
  573. return 0;
  574. }
  575. static int config_input(AVFilterLink *inlink)
  576. {
  577. int i, j, ret;
  578. AVFilterContext *ctx = inlink->dst;
  579. CurvesContext *curves = ctx->priv;
  580. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  581. char **pts = curves->comp_points_str;
  582. struct keypoint *comp_points[NB_COMP + 1] = {0};
  583. ff_fill_rgba_map(curves->rgba_map, inlink->format);
  584. curves->is_16bit = desc->comp[0].depth > 8;
  585. curves->depth = desc->comp[0].depth;
  586. curves->lut_size = 1 << curves->depth;
  587. curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
  588. curves->filter_slice = desc->flags & AV_PIX_FMT_FLAG_PLANAR ? filter_slice_planar : filter_slice_packed;
  589. for (i = 0; i < NB_COMP + 1; i++) {
  590. curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
  591. if (!curves->graph[i])
  592. return AVERROR(ENOMEM);
  593. ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
  594. if (ret < 0)
  595. return ret;
  596. switch (curves->depth) {
  597. case 8: ret = interpolate8 (ctx, curves->graph[i], comp_points[i]); break;
  598. case 9: ret = interpolate9 (ctx, curves->graph[i], comp_points[i]); break;
  599. case 10: ret = interpolate10(ctx, curves->graph[i], comp_points[i]); break;
  600. case 12: ret = interpolate12(ctx, curves->graph[i], comp_points[i]); break;
  601. case 14: ret = interpolate14(ctx, curves->graph[i], comp_points[i]); break;
  602. case 16: ret = interpolate16(ctx, curves->graph[i], comp_points[i]); break;
  603. }
  604. if (ret < 0)
  605. return ret;
  606. }
  607. if (pts[NB_COMP]) {
  608. for (i = 0; i < NB_COMP; i++)
  609. for (j = 0; j < curves->lut_size; j++)
  610. curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
  611. }
  612. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  613. for (i = 0; i < NB_COMP; i++) {
  614. const struct keypoint *point = comp_points[i];
  615. av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
  616. while (point) {
  617. av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
  618. point = point->next;
  619. }
  620. }
  621. }
  622. if (curves->plot_filename)
  623. dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
  624. for (i = 0; i < NB_COMP + 1; i++) {
  625. struct keypoint *point = comp_points[i];
  626. while (point) {
  627. struct keypoint *next = point->next;
  628. av_free(point);
  629. point = next;
  630. }
  631. }
  632. return 0;
  633. }
  634. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  635. {
  636. AVFilterContext *ctx = inlink->dst;
  637. CurvesContext *curves = ctx->priv;
  638. AVFilterLink *outlink = ctx->outputs[0];
  639. AVFrame *out;
  640. ThreadData td;
  641. if (av_frame_is_writable(in)) {
  642. out = in;
  643. } else {
  644. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  645. if (!out) {
  646. av_frame_free(&in);
  647. return AVERROR(ENOMEM);
  648. }
  649. av_frame_copy_props(out, in);
  650. }
  651. td.in = in;
  652. td.out = out;
  653. ctx->internal->execute(ctx, curves->filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  654. if (out != in)
  655. av_frame_free(&in);
  656. return ff_filter_frame(outlink, out);
  657. }
  658. static av_cold void curves_uninit(AVFilterContext *ctx)
  659. {
  660. int i;
  661. CurvesContext *curves = ctx->priv;
  662. for (i = 0; i < NB_COMP + 1; i++)
  663. av_freep(&curves->graph[i]);
  664. }
  665. static const AVFilterPad curves_inputs[] = {
  666. {
  667. .name = "default",
  668. .type = AVMEDIA_TYPE_VIDEO,
  669. .filter_frame = filter_frame,
  670. .config_props = config_input,
  671. },
  672. { NULL }
  673. };
  674. static const AVFilterPad curves_outputs[] = {
  675. {
  676. .name = "default",
  677. .type = AVMEDIA_TYPE_VIDEO,
  678. },
  679. { NULL }
  680. };
  681. AVFilter ff_vf_curves = {
  682. .name = "curves",
  683. .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
  684. .priv_size = sizeof(CurvesContext),
  685. .init = curves_init,
  686. .uninit = curves_uninit,
  687. .query_formats = query_formats,
  688. .inputs = curves_inputs,
  689. .outputs = curves_outputs,
  690. .priv_class = &curves_class,
  691. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  692. };