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.

697 lines
23KB

  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. } CurvesContext;
  68. typedef struct ThreadData {
  69. AVFrame *in, *out;
  70. } ThreadData;
  71. #define OFFSET(x) offsetof(CurvesContext, x)
  72. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  73. static const AVOption curves_options[] = {
  74. { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
  75. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  76. { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  77. { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  78. { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  79. { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  80. { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  81. { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  82. { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  83. { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  84. { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  85. { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  86. { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  87. { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  88. { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  89. { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  90. { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  91. { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  92. { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  93. { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  94. { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  95. { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  96. { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  97. { NULL }
  98. };
  99. AVFILTER_DEFINE_CLASS(curves);
  100. static const struct {
  101. const char *r;
  102. const char *g;
  103. const char *b;
  104. const char *master;
  105. } curves_presets[] = {
  106. [PRESET_COLOR_NEGATIVE] = {
  107. "0.129/1 0.466/0.498 0.725/0",
  108. "0.109/1 0.301/0.498 0.517/0",
  109. "0.098/1 0.235/0.498 0.423/0",
  110. },
  111. [PRESET_CROSS_PROCESS] = {
  112. "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
  113. "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
  114. "0/0 0.231/0.094 0.709/0.874 1/1",
  115. },
  116. [PRESET_DARKER] = { .master = "0/0 0.5/0.4 1/1" },
  117. [PRESET_INCREASE_CONTRAST] = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
  118. [PRESET_LIGHTER] = { .master = "0/0 0.4/0.5 1/1" },
  119. [PRESET_LINEAR_CONTRAST] = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
  120. [PRESET_MEDIUM_CONTRAST] = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
  121. [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
  122. [PRESET_STRONG_CONTRAST] = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
  123. [PRESET_VINTAGE] = {
  124. "0/0.11 0.42/0.51 1/0.95",
  125. "0/0 0.50/0.48 1/1",
  126. "0/0.22 0.49/0.44 1/0.8",
  127. }
  128. };
  129. static struct keypoint *make_point(double x, double y, struct keypoint *next)
  130. {
  131. struct keypoint *point = av_mallocz(sizeof(*point));
  132. if (!point)
  133. return NULL;
  134. point->x = x;
  135. point->y = y;
  136. point->next = next;
  137. return point;
  138. }
  139. static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
  140. int lut_size)
  141. {
  142. char *p = (char *)s; // strtod won't alter the string
  143. struct keypoint *last = NULL;
  144. const int scale = lut_size - 1;
  145. /* construct a linked list based on the key points string */
  146. while (p && *p) {
  147. struct keypoint *point = make_point(0, 0, NULL);
  148. if (!point)
  149. return AVERROR(ENOMEM);
  150. point->x = av_strtod(p, &p); if (p && *p) p++;
  151. point->y = av_strtod(p, &p); if (p && *p) p++;
  152. if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
  153. av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
  154. "x and y must be in the [0;1] range.\n", point->x, point->y);
  155. return AVERROR(EINVAL);
  156. }
  157. if (!*points)
  158. *points = point;
  159. if (last) {
  160. if ((int)(last->x * scale) >= (int)(point->x * scale)) {
  161. av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
  162. "and (%f;%f) are too close from each other or not "
  163. "strictly increasing on the x-axis\n",
  164. last->x, last->y, point->x, point->y);
  165. return AVERROR(EINVAL);
  166. }
  167. last->next = point;
  168. }
  169. last = point;
  170. }
  171. if (*points && !(*points)->next) {
  172. av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
  173. "this is unlikely to behave as you expect. You probably want"
  174. "at least 2 points.",
  175. (*points)->x, (*points)->y);
  176. }
  177. return 0;
  178. }
  179. static int get_nb_points(const struct keypoint *d)
  180. {
  181. int n = 0;
  182. while (d) {
  183. n++;
  184. d = d->next;
  185. }
  186. return n;
  187. }
  188. /**
  189. * Natural cubic spline interpolation
  190. * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  191. * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  192. */
  193. #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uint16(v))
  194. static inline int interpolate(void *log_ctx, uint16_t *y,
  195. const struct keypoint *points, int nbits)
  196. {
  197. int i, ret = 0;
  198. const struct keypoint *point = points;
  199. double xprev = 0;
  200. const int lut_size = 1<<nbits;
  201. const int scale = lut_size - 1;
  202. double (*matrix)[3];
  203. double *h, *r;
  204. const int n = get_nb_points(points); // number of splines
  205. if (n == 0) {
  206. for (i = 0; i < lut_size; i++)
  207. y[i] = i;
  208. return 0;
  209. }
  210. if (n == 1) {
  211. for (i = 0; i < lut_size; i++)
  212. y[i] = CLIP(point->y * scale);
  213. return 0;
  214. }
  215. matrix = av_calloc(n, sizeof(*matrix));
  216. h = av_malloc((n - 1) * sizeof(*h));
  217. r = av_calloc(n, sizeof(*r));
  218. if (!matrix || !h || !r) {
  219. ret = AVERROR(ENOMEM);
  220. goto end;
  221. }
  222. /* h(i) = x(i+1) - x(i) */
  223. i = -1;
  224. for (point = points; point; point = point->next) {
  225. if (i != -1)
  226. h[i] = point->x - xprev;
  227. xprev = point->x;
  228. i++;
  229. }
  230. /* right-side of the polynomials, will be modified to contains the solution */
  231. point = points;
  232. for (i = 1; i < n - 1; i++) {
  233. const double yp = point->y;
  234. const double yc = point->next->y;
  235. const double yn = point->next->next->y;
  236. r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
  237. point = point->next;
  238. }
  239. #define BD 0 /* sub diagonal (below main) */
  240. #define MD 1 /* main diagonal (center) */
  241. #define AD 2 /* sup diagonal (above main) */
  242. /* left side of the polynomials into a tridiagonal matrix. */
  243. matrix[0][MD] = matrix[n - 1][MD] = 1;
  244. for (i = 1; i < n - 1; i++) {
  245. matrix[i][BD] = h[i-1];
  246. matrix[i][MD] = 2 * (h[i-1] + h[i]);
  247. matrix[i][AD] = h[i];
  248. }
  249. /* tridiagonal solving of the linear system */
  250. for (i = 1; i < n; i++) {
  251. const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
  252. const double k = den ? 1./den : 1.;
  253. matrix[i][AD] *= k;
  254. r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
  255. }
  256. for (i = n - 2; i >= 0; i--)
  257. r[i] = r[i] - matrix[i][AD] * r[i + 1];
  258. point = points;
  259. /* left padding */
  260. for (i = 0; i < (int)(point->x * scale); i++)
  261. y[i] = CLIP(point->y * scale);
  262. /* compute the graph with x=[x0..xN] */
  263. i = 0;
  264. av_assert0(point->next); // always at least 2 key points
  265. while (point->next) {
  266. const double yc = point->y;
  267. const double yn = point->next->y;
  268. const double a = yc;
  269. const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
  270. const double c = r[i] / 2.;
  271. const double d = (r[i+1] - r[i]) / (6.*h[i]);
  272. int x;
  273. const int x_start = point->x * scale;
  274. const int x_end = point->next->x * scale;
  275. av_assert0(x_start >= 0 && x_start < lut_size &&
  276. x_end >= 0 && x_end < lut_size);
  277. for (x = x_start; x <= x_end; x++) {
  278. const double xx = (x - x_start) * 1./scale;
  279. const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
  280. y[x] = CLIP(yy * scale);
  281. av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
  282. }
  283. point = point->next;
  284. i++;
  285. }
  286. /* right padding */
  287. for (i = (int)(point->x * scale); i < lut_size; i++)
  288. y[i] = CLIP(point->y * scale);
  289. end:
  290. av_free(matrix);
  291. av_free(h);
  292. av_free(r);
  293. return ret;
  294. }
  295. #define DECLARE_INTERPOLATE_FUNC(nbits) \
  296. static int interpolate##nbits(void *log_ctx, uint16_t *y, \
  297. const struct keypoint *points) \
  298. { \
  299. return interpolate(log_ctx, y, points, nbits); \
  300. }
  301. DECLARE_INTERPOLATE_FUNC(8)
  302. DECLARE_INTERPOLATE_FUNC(16)
  303. static int parse_psfile(AVFilterContext *ctx, const char *fname)
  304. {
  305. CurvesContext *curves = ctx->priv;
  306. uint8_t *buf;
  307. size_t size;
  308. int i, ret, av_unused(version), nb_curves;
  309. AVBPrint ptstr;
  310. static const int comp_ids[] = {3, 0, 1, 2};
  311. av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
  312. ret = av_file_map(fname, &buf, &size, 0, NULL);
  313. if (ret < 0)
  314. return ret;
  315. #define READ16(dst) do { \
  316. if (size < 2) { \
  317. ret = AVERROR_INVALIDDATA; \
  318. goto end; \
  319. } \
  320. dst = AV_RB16(buf); \
  321. buf += 2; \
  322. size -= 2; \
  323. } while (0)
  324. READ16(version);
  325. READ16(nb_curves);
  326. for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
  327. int nb_points, n;
  328. av_bprint_clear(&ptstr);
  329. READ16(nb_points);
  330. for (n = 0; n < nb_points; n++) {
  331. int y, x;
  332. READ16(y);
  333. READ16(x);
  334. av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
  335. }
  336. if (*ptstr.str) {
  337. char **pts = &curves->comp_points_str[comp_ids[i]];
  338. if (!*pts) {
  339. *pts = av_strdup(ptstr.str);
  340. av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
  341. i, comp_ids[i], nb_points, *pts);
  342. if (!*pts) {
  343. ret = AVERROR(ENOMEM);
  344. goto end;
  345. }
  346. }
  347. }
  348. }
  349. end:
  350. av_bprint_finalize(&ptstr, NULL);
  351. av_file_unmap(buf, size);
  352. return ret;
  353. }
  354. static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
  355. struct keypoint *comp_points[NB_COMP + 1],
  356. int lut_size)
  357. {
  358. int i;
  359. AVBPrint buf;
  360. const double scale = 1. / (lut_size - 1);
  361. static const char * const colors[] = { "red", "green", "blue", "#404040", };
  362. FILE *f = av_fopen_utf8(fname, "w");
  363. av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
  364. if (!f) {
  365. int ret = AVERROR(errno);
  366. av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
  367. fname, av_err2str(ret));
  368. return ret;
  369. }
  370. av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
  371. av_bprintf(&buf, "set xtics 0.1\n");
  372. av_bprintf(&buf, "set ytics 0.1\n");
  373. av_bprintf(&buf, "set size square\n");
  374. av_bprintf(&buf, "set grid\n");
  375. for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
  376. av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
  377. i ? ", " : "plot ", colors[i]);
  378. if (comp_points[i])
  379. av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
  380. colors[i]);
  381. }
  382. av_bprintf(&buf, "\n");
  383. for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
  384. int x;
  385. /* plot generated values */
  386. for (x = 0; x < lut_size; x++)
  387. av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
  388. av_bprintf(&buf, "e\n");
  389. /* plot user knots */
  390. if (comp_points[i]) {
  391. const struct keypoint *point = comp_points[i];
  392. while (point) {
  393. av_bprintf(&buf, "%f %f\n", point->x, point->y);
  394. point = point->next;
  395. }
  396. av_bprintf(&buf, "e\n");
  397. }
  398. }
  399. fwrite(buf.str, 1, buf.len, f);
  400. fclose(f);
  401. av_bprint_finalize(&buf, NULL);
  402. return 0;
  403. }
  404. static av_cold int curves_init(AVFilterContext *ctx)
  405. {
  406. int i, ret;
  407. CurvesContext *curves = ctx->priv;
  408. char **pts = curves->comp_points_str;
  409. const char *allp = curves->comp_points_str_all;
  410. //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
  411. // allp = curves_presets[curves->preset].all;
  412. if (allp) {
  413. for (i = 0; i < NB_COMP; i++) {
  414. if (!pts[i])
  415. pts[i] = av_strdup(allp);
  416. if (!pts[i])
  417. return AVERROR(ENOMEM);
  418. }
  419. }
  420. if (curves->psfile) {
  421. ret = parse_psfile(ctx, curves->psfile);
  422. if (ret < 0)
  423. return ret;
  424. }
  425. if (curves->preset != PRESET_NONE) {
  426. #define SET_COMP_IF_NOT_SET(n, name) do { \
  427. if (!pts[n] && curves_presets[curves->preset].name) { \
  428. pts[n] = av_strdup(curves_presets[curves->preset].name); \
  429. if (!pts[n]) \
  430. return AVERROR(ENOMEM); \
  431. } \
  432. } while (0)
  433. SET_COMP_IF_NOT_SET(0, r);
  434. SET_COMP_IF_NOT_SET(1, g);
  435. SET_COMP_IF_NOT_SET(2, b);
  436. SET_COMP_IF_NOT_SET(3, master);
  437. }
  438. return 0;
  439. }
  440. static int query_formats(AVFilterContext *ctx)
  441. {
  442. static const enum AVPixelFormat pix_fmts[] = {
  443. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  444. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  445. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  446. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  447. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  448. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  449. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  450. AV_PIX_FMT_NONE
  451. };
  452. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  453. if (!fmts_list)
  454. return AVERROR(ENOMEM);
  455. return ff_set_common_formats(ctx, fmts_list);
  456. }
  457. static int config_input(AVFilterLink *inlink)
  458. {
  459. int i, j, ret;
  460. AVFilterContext *ctx = inlink->dst;
  461. CurvesContext *curves = ctx->priv;
  462. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  463. char **pts = curves->comp_points_str;
  464. struct keypoint *comp_points[NB_COMP + 1] = {0};
  465. ff_fill_rgba_map(curves->rgba_map, inlink->format);
  466. curves->is_16bit = desc->comp[0].depth > 8;
  467. curves->lut_size = curves->is_16bit ? 1<<16 : 1<<8;
  468. curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
  469. for (i = 0; i < NB_COMP + 1; i++) {
  470. curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
  471. if (!curves->graph[i])
  472. return AVERROR(ENOMEM);
  473. ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
  474. if (ret < 0)
  475. return ret;
  476. if (curves->is_16bit) ret = interpolate16(ctx, curves->graph[i], comp_points[i]);
  477. else ret = interpolate8(ctx, curves->graph[i], comp_points[i]);
  478. if (ret < 0)
  479. return ret;
  480. }
  481. if (pts[NB_COMP]) {
  482. for (i = 0; i < NB_COMP; i++)
  483. for (j = 0; j < curves->lut_size; j++)
  484. curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
  485. }
  486. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  487. for (i = 0; i < NB_COMP; i++) {
  488. const struct keypoint *point = comp_points[i];
  489. av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
  490. while (point) {
  491. av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
  492. point = point->next;
  493. }
  494. }
  495. }
  496. if (curves->plot_filename)
  497. dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
  498. for (i = 0; i < NB_COMP + 1; i++) {
  499. struct keypoint *point = comp_points[i];
  500. while (point) {
  501. struct keypoint *next = point->next;
  502. av_free(point);
  503. point = next;
  504. }
  505. }
  506. return 0;
  507. }
  508. static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
  509. {
  510. int x, y;
  511. const CurvesContext *curves = ctx->priv;
  512. const ThreadData *td = arg;
  513. const AVFrame *in = td->in;
  514. const AVFrame *out = td->out;
  515. const int direct = out == in;
  516. const int step = curves->step;
  517. const uint8_t r = curves->rgba_map[R];
  518. const uint8_t g = curves->rgba_map[G];
  519. const uint8_t b = curves->rgba_map[B];
  520. const uint8_t a = curves->rgba_map[A];
  521. const int slice_start = (in->height * jobnr ) / nb_jobs;
  522. const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
  523. if (curves->is_16bit) {
  524. for (y = slice_start; y < slice_end; y++) {
  525. uint16_t *dstp = ( uint16_t *)(out->data[0] + y * out->linesize[0]);
  526. const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y * in->linesize[0]);
  527. for (x = 0; x < in->width * step; x += step) {
  528. dstp[x + r] = curves->graph[R][srcp[x + r]];
  529. dstp[x + g] = curves->graph[G][srcp[x + g]];
  530. dstp[x + b] = curves->graph[B][srcp[x + b]];
  531. if (!direct && step == 4)
  532. dstp[x + a] = srcp[x + a];
  533. }
  534. }
  535. } else {
  536. uint8_t *dst = out->data[0] + slice_start * out->linesize[0];
  537. const uint8_t *src = in->data[0] + slice_start * in->linesize[0];
  538. for (y = slice_start; y < slice_end; y++) {
  539. for (x = 0; x < in->width * step; x += step) {
  540. dst[x + r] = curves->graph[R][src[x + r]];
  541. dst[x + g] = curves->graph[G][src[x + g]];
  542. dst[x + b] = curves->graph[B][src[x + b]];
  543. if (!direct && step == 4)
  544. dst[x + a] = src[x + a];
  545. }
  546. dst += out->linesize[0];
  547. src += in ->linesize[0];
  548. }
  549. }
  550. return 0;
  551. }
  552. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  553. {
  554. AVFilterContext *ctx = inlink->dst;
  555. AVFilterLink *outlink = ctx->outputs[0];
  556. AVFrame *out;
  557. ThreadData td;
  558. if (av_frame_is_writable(in)) {
  559. out = in;
  560. } else {
  561. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  562. if (!out) {
  563. av_frame_free(&in);
  564. return AVERROR(ENOMEM);
  565. }
  566. av_frame_copy_props(out, in);
  567. }
  568. td.in = in;
  569. td.out = out;
  570. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
  571. if (out != in)
  572. av_frame_free(&in);
  573. return ff_filter_frame(outlink, out);
  574. }
  575. static av_cold void curves_uninit(AVFilterContext *ctx)
  576. {
  577. int i;
  578. CurvesContext *curves = ctx->priv;
  579. for (i = 0; i < NB_COMP + 1; i++)
  580. av_freep(&curves->graph[i]);
  581. }
  582. static const AVFilterPad curves_inputs[] = {
  583. {
  584. .name = "default",
  585. .type = AVMEDIA_TYPE_VIDEO,
  586. .filter_frame = filter_frame,
  587. .config_props = config_input,
  588. },
  589. { NULL }
  590. };
  591. static const AVFilterPad curves_outputs[] = {
  592. {
  593. .name = "default",
  594. .type = AVMEDIA_TYPE_VIDEO,
  595. },
  596. { NULL }
  597. };
  598. AVFilter ff_vf_curves = {
  599. .name = "curves",
  600. .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
  601. .priv_size = sizeof(CurvesContext),
  602. .init = curves_init,
  603. .uninit = curves_uninit,
  604. .query_formats = query_formats,
  605. .inputs = curves_inputs,
  606. .outputs = curves_outputs,
  607. .priv_class = &curves_class,
  608. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
  609. };