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.

553 lines
18KB

  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 {
  56. const AVClass *class;
  57. enum preset preset;
  58. char *comp_points_str[NB_COMP + 1];
  59. char *comp_points_str_all;
  60. uint8_t graph[NB_COMP + 1][256];
  61. char *psfile;
  62. uint8_t rgba_map[4];
  63. int step;
  64. } CurvesContext;
  65. #define OFFSET(x) offsetof(CurvesContext, x)
  66. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  67. static const AVOption curves_options[] = {
  68. { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
  69. { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  70. { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  71. { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  72. { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  73. { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  74. { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  75. { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  76. { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  77. { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  78. { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  79. { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  80. { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  81. { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  82. { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  83. { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  84. { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  85. { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  86. { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  87. { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  88. { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  89. { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  90. { NULL }
  91. };
  92. AVFILTER_DEFINE_CLASS(curves);
  93. static const struct {
  94. const char *r;
  95. const char *g;
  96. const char *b;
  97. const char *master;
  98. } curves_presets[] = {
  99. [PRESET_COLOR_NEGATIVE] = {
  100. "0/1 0.129/1 0.466/0.498 0.725/0 1/0",
  101. "0/1 0.109/1 0.301/0.498 0.517/0 1/0",
  102. "0/1 0.098/1 0.235/0.498 0.423/0 1/0",
  103. },
  104. [PRESET_CROSS_PROCESS] = {
  105. "0.25/0.156 0.501/0.501 0.686/0.745",
  106. "0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
  107. "0.231/0.094 0.709/0.874",
  108. },
  109. [PRESET_DARKER] = { .master = "0.5/0.4" },
  110. [PRESET_INCREASE_CONTRAST] = { .master = "0.149/0.066 0.831/0.905 0.905/0.98" },
  111. [PRESET_LIGHTER] = { .master = "0.4/0.5" },
  112. [PRESET_LINEAR_CONTRAST] = { .master = "0.305/0.286 0.694/0.713" },
  113. [PRESET_MEDIUM_CONTRAST] = { .master = "0.286/0.219 0.639/0.643" },
  114. [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
  115. [PRESET_STRONG_CONTRAST] = { .master = "0.301/0.196 0.592/0.6 0.686/0.737" },
  116. [PRESET_VINTAGE] = {
  117. "0/0.11 0.42/0.51 1/0.95",
  118. "0.50/0.48",
  119. "0/0.22 0.49/0.44 1/0.8",
  120. }
  121. };
  122. static struct keypoint *make_point(double x, double y, struct keypoint *next)
  123. {
  124. struct keypoint *point = av_mallocz(sizeof(*point));
  125. if (!point)
  126. return NULL;
  127. point->x = x;
  128. point->y = y;
  129. point->next = next;
  130. return point;
  131. }
  132. static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
  133. {
  134. char *p = (char *)s; // strtod won't alter the string
  135. struct keypoint *last = NULL;
  136. /* construct a linked list based on the key points string */
  137. while (p && *p) {
  138. struct keypoint *point = make_point(0, 0, NULL);
  139. if (!point)
  140. return AVERROR(ENOMEM);
  141. point->x = av_strtod(p, &p); if (p && *p) p++;
  142. point->y = av_strtod(p, &p); if (p && *p) p++;
  143. if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
  144. av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
  145. "x and y must be in the [0;1] range.\n", point->x, point->y);
  146. return AVERROR(EINVAL);
  147. }
  148. if (!*points)
  149. *points = point;
  150. if (last) {
  151. if ((int)(last->x * 255) >= (int)(point->x * 255)) {
  152. av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
  153. "and (%f;%f) are too close from each other or not "
  154. "strictly increasing on the x-axis\n",
  155. last->x, last->y, point->x, point->y);
  156. return AVERROR(EINVAL);
  157. }
  158. last->next = point;
  159. }
  160. last = point;
  161. }
  162. /* auto insert first key point if missing at x=0 */
  163. if (!*points) {
  164. last = make_point(0, 0, NULL);
  165. if (!last)
  166. return AVERROR(ENOMEM);
  167. last->x = last->y = 0;
  168. *points = last;
  169. } else if ((*points)->x != 0.) {
  170. struct keypoint *newfirst = make_point(0, 0, *points);
  171. if (!newfirst)
  172. return AVERROR(ENOMEM);
  173. *points = newfirst;
  174. }
  175. av_assert0(last);
  176. /* auto insert last key point if missing at x=1 */
  177. if (last->x != 1.) {
  178. struct keypoint *point = make_point(1, 1, NULL);
  179. if (!point)
  180. return AVERROR(ENOMEM);
  181. last->next = point;
  182. }
  183. return 0;
  184. }
  185. static int get_nb_points(const struct keypoint *d)
  186. {
  187. int n = 0;
  188. while (d) {
  189. n++;
  190. d = d->next;
  191. }
  192. return n;
  193. }
  194. /**
  195. * Natural cubic spline interpolation
  196. * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  197. * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  198. */
  199. static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
  200. {
  201. int i, ret = 0;
  202. const struct keypoint *point;
  203. double xprev = 0;
  204. int n = get_nb_points(points); // number of splines
  205. double (*matrix)[3] = av_calloc(n, sizeof(*matrix));
  206. double *h = av_malloc((n - 1) * sizeof(*h));
  207. double *r = av_calloc(n, sizeof(*r));
  208. if (!matrix || !h || !r) {
  209. ret = AVERROR(ENOMEM);
  210. goto end;
  211. }
  212. /* h(i) = x(i+1) - x(i) */
  213. i = -1;
  214. for (point = points; point; point = point->next) {
  215. if (i != -1)
  216. h[i] = point->x - xprev;
  217. xprev = point->x;
  218. i++;
  219. }
  220. /* right-side of the polynomials, will be modified to contains the solution */
  221. point = points;
  222. for (i = 1; i < n - 1; i++) {
  223. double yp = point->y,
  224. yc = point->next->y,
  225. yn = point->next->next->y;
  226. r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
  227. point = point->next;
  228. }
  229. #define BD 0 /* sub diagonal (below main) */
  230. #define MD 1 /* main diagonal (center) */
  231. #define AD 2 /* sup diagonal (above main) */
  232. /* left side of the polynomials into a tridiagonal matrix. */
  233. matrix[0][MD] = matrix[n - 1][MD] = 1;
  234. for (i = 1; i < n - 1; i++) {
  235. matrix[i][BD] = h[i-1];
  236. matrix[i][MD] = 2 * (h[i-1] + h[i]);
  237. matrix[i][AD] = h[i];
  238. }
  239. /* tridiagonal solving of the linear system */
  240. for (i = 1; i < n; i++) {
  241. double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
  242. double k = den ? 1./den : 1.;
  243. matrix[i][AD] *= k;
  244. r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
  245. }
  246. for (i = n - 2; i >= 0; i--)
  247. r[i] = r[i] - matrix[i][AD] * r[i + 1];
  248. /* compute the graph with x=[0..255] */
  249. i = 0;
  250. point = points;
  251. av_assert0(point->next); // always at least 2 key points
  252. while (point->next) {
  253. double yc = point->y;
  254. double yn = point->next->y;
  255. double a = yc;
  256. double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
  257. double c = r[i] / 2.;
  258. double d = (r[i+1] - r[i]) / (6.*h[i]);
  259. int x;
  260. int x_start = point->x * 255;
  261. int x_end = point->next->x * 255;
  262. av_assert0(x_start >= 0 && x_start <= 255 &&
  263. x_end >= 0 && x_end <= 255);
  264. for (x = x_start; x <= x_end; x++) {
  265. double xx = (x - x_start) * 1/255.;
  266. double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
  267. y[x] = av_clipf(yy, 0, 1) * 255;
  268. av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
  269. }
  270. point = point->next;
  271. i++;
  272. }
  273. end:
  274. av_free(matrix);
  275. av_free(h);
  276. av_free(r);
  277. return ret;
  278. }
  279. static int parse_psfile(AVFilterContext *ctx, const char *fname)
  280. {
  281. CurvesContext *curves = ctx->priv;
  282. uint8_t *buf;
  283. size_t size;
  284. int i, ret, av_unused(version), nb_curves;
  285. AVBPrint ptstr;
  286. static const int comp_ids[] = {3, 0, 1, 2};
  287. av_bprint_init(&ptstr, 0, AV_BPRINT_SIZE_AUTOMATIC);
  288. ret = av_file_map(fname, &buf, &size, 0, NULL);
  289. if (ret < 0)
  290. return ret;
  291. #define READ16(dst) do { \
  292. if (size < 2) \
  293. return AVERROR_INVALIDDATA; \
  294. dst = AV_RB16(buf); \
  295. buf += 2; \
  296. size -= 2; \
  297. } while (0)
  298. READ16(version);
  299. READ16(nb_curves);
  300. for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
  301. int nb_points, n;
  302. av_bprint_clear(&ptstr);
  303. READ16(nb_points);
  304. for (n = 0; n < nb_points; n++) {
  305. int y, x;
  306. READ16(y);
  307. READ16(x);
  308. av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
  309. }
  310. if (*ptstr.str) {
  311. char **pts = &curves->comp_points_str[comp_ids[i]];
  312. if (!*pts) {
  313. *pts = av_strdup(ptstr.str);
  314. av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
  315. i, comp_ids[i], nb_points, *pts);
  316. if (!*pts) {
  317. ret = AVERROR(ENOMEM);
  318. goto end;
  319. }
  320. }
  321. }
  322. }
  323. end:
  324. av_bprint_finalize(&ptstr, NULL);
  325. av_file_unmap(buf, size);
  326. return ret;
  327. }
  328. static av_cold int init(AVFilterContext *ctx)
  329. {
  330. int i, j, ret;
  331. CurvesContext *curves = ctx->priv;
  332. struct keypoint *comp_points[NB_COMP + 1] = {0};
  333. char **pts = curves->comp_points_str;
  334. const char *allp = curves->comp_points_str_all;
  335. //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
  336. // allp = curves_presets[curves->preset].all;
  337. if (allp) {
  338. for (i = 0; i < NB_COMP; i++) {
  339. if (!pts[i])
  340. pts[i] = av_strdup(allp);
  341. if (!pts[i])
  342. return AVERROR(ENOMEM);
  343. }
  344. }
  345. if (curves->psfile) {
  346. ret = parse_psfile(ctx, curves->psfile);
  347. if (ret < 0)
  348. return ret;
  349. }
  350. if (curves->preset != PRESET_NONE) {
  351. #define SET_COMP_IF_NOT_SET(n, name) do { \
  352. if (!pts[n] && curves_presets[curves->preset].name) { \
  353. pts[n] = av_strdup(curves_presets[curves->preset].name); \
  354. if (!pts[n]) \
  355. return AVERROR(ENOMEM); \
  356. } \
  357. } while (0)
  358. SET_COMP_IF_NOT_SET(0, r);
  359. SET_COMP_IF_NOT_SET(1, g);
  360. SET_COMP_IF_NOT_SET(2, b);
  361. SET_COMP_IF_NOT_SET(3, master);
  362. }
  363. for (i = 0; i < NB_COMP + 1; i++) {
  364. ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
  365. if (ret < 0)
  366. return ret;
  367. ret = interpolate(ctx, curves->graph[i], comp_points[i]);
  368. if (ret < 0)
  369. return ret;
  370. }
  371. if (pts[NB_COMP]) {
  372. for (i = 0; i < NB_COMP; i++)
  373. for (j = 0; j < 256; j++)
  374. curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
  375. }
  376. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  377. for (i = 0; i < NB_COMP; i++) {
  378. struct keypoint *point = comp_points[i];
  379. av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
  380. while (point) {
  381. av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
  382. point = point->next;
  383. }
  384. av_log(ctx, AV_LOG_VERBOSE, "\n");
  385. av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
  386. for (j = 0; j < 256; j++)
  387. av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i][j]);
  388. av_log(ctx, AV_LOG_VERBOSE, "\n");
  389. }
  390. }
  391. for (i = 0; i < NB_COMP + 1; i++) {
  392. struct keypoint *point = comp_points[i];
  393. while (point) {
  394. struct keypoint *next = point->next;
  395. av_free(point);
  396. point = next;
  397. }
  398. }
  399. return 0;
  400. }
  401. static int query_formats(AVFilterContext *ctx)
  402. {
  403. static const enum AVPixelFormat pix_fmts[] = {
  404. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  405. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  406. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  407. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  408. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  409. AV_PIX_FMT_NONE
  410. };
  411. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  412. return 0;
  413. }
  414. static int config_input(AVFilterLink *inlink)
  415. {
  416. CurvesContext *curves = inlink->dst->priv;
  417. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
  418. ff_fill_rgba_map(curves->rgba_map, inlink->format);
  419. curves->step = av_get_padded_bits_per_pixel(desc) >> 3;
  420. return 0;
  421. }
  422. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  423. {
  424. int x, y, direct = 0;
  425. AVFilterContext *ctx = inlink->dst;
  426. CurvesContext *curves = ctx->priv;
  427. AVFilterLink *outlink = ctx->outputs[0];
  428. AVFrame *out;
  429. uint8_t *dst;
  430. const uint8_t *src;
  431. const int step = curves->step;
  432. const uint8_t r = curves->rgba_map[R];
  433. const uint8_t g = curves->rgba_map[G];
  434. const uint8_t b = curves->rgba_map[B];
  435. const uint8_t a = curves->rgba_map[A];
  436. if (av_frame_is_writable(in)) {
  437. direct = 1;
  438. out = in;
  439. } else {
  440. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  441. if (!out) {
  442. av_frame_free(&in);
  443. return AVERROR(ENOMEM);
  444. }
  445. av_frame_copy_props(out, in);
  446. }
  447. dst = out->data[0];
  448. src = in ->data[0];
  449. for (y = 0; y < inlink->h; y++) {
  450. for (x = 0; x < inlink->w * step; x += step) {
  451. dst[x + r] = curves->graph[R][src[x + r]];
  452. dst[x + g] = curves->graph[G][src[x + g]];
  453. dst[x + b] = curves->graph[B][src[x + b]];
  454. if (!direct && step == 4)
  455. dst[x + a] = src[x + a];
  456. }
  457. dst += out->linesize[0];
  458. src += in ->linesize[0];
  459. }
  460. if (!direct)
  461. av_frame_free(&in);
  462. return ff_filter_frame(outlink, out);
  463. }
  464. static const AVFilterPad curves_inputs[] = {
  465. {
  466. .name = "default",
  467. .type = AVMEDIA_TYPE_VIDEO,
  468. .filter_frame = filter_frame,
  469. .config_props = config_input,
  470. },
  471. { NULL }
  472. };
  473. static const AVFilterPad curves_outputs[] = {
  474. {
  475. .name = "default",
  476. .type = AVMEDIA_TYPE_VIDEO,
  477. },
  478. { NULL }
  479. };
  480. AVFilter avfilter_vf_curves = {
  481. .name = "curves",
  482. .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
  483. .priv_size = sizeof(CurvesContext),
  484. .init = init,
  485. .query_formats = query_formats,
  486. .inputs = curves_inputs,
  487. .outputs = curves_outputs,
  488. .priv_class = &curves_class,
  489. .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  490. };