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.

829 lines
29KB

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