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.

463 lines
15KB

  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/eval.h"
  22. #include "libavutil/avassert.h"
  23. #include "avfilter.h"
  24. #include "formats.h"
  25. #include "internal.h"
  26. #include "video.h"
  27. struct keypoint {
  28. double x, y;
  29. struct keypoint *next;
  30. };
  31. #define NB_COMP 3
  32. enum preset {
  33. PRESET_NONE,
  34. PRESET_COLOR_NEGATIVE,
  35. PRESET_CROSS_PROCESS,
  36. PRESET_DARKER,
  37. PRESET_INCREASE_CONTRAST,
  38. PRESET_LIGHTER,
  39. PRESET_LINEAR_CONTRAST,
  40. PRESET_MEDIUM_CONTRAST,
  41. PRESET_NEGATIVE,
  42. PRESET_STRONG_CONTRAST,
  43. PRESET_VINTAGE,
  44. NB_PRESETS,
  45. };
  46. typedef struct {
  47. const AVClass *class;
  48. enum preset preset;
  49. char *comp_points_str[NB_COMP];
  50. char *comp_points_str_all;
  51. uint8_t graph[NB_COMP][256];
  52. } CurvesContext;
  53. #define OFFSET(x) offsetof(CurvesContext, x)
  54. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  55. static const AVOption curves_options[] = {
  56. { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  57. { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  58. { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  59. { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  60. { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  61. { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  62. { "all", "set points coordinates for all components", OFFSET(comp_points_str_all),AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  63. { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
  64. { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  65. { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  66. { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  67. { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  68. { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  69. { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  70. { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  71. { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  72. { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  73. { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
  74. { NULL }
  75. };
  76. AVFILTER_DEFINE_CLASS(curves);
  77. static const struct {
  78. const char *r;
  79. const char *g;
  80. const char *b;
  81. } curves_presets[] = {
  82. [PRESET_COLOR_NEGATIVE] = {
  83. "0/1 0.129/1 0.466/0.498 0.725/0 1/0",
  84. "0/1 0.109/1 0.301/0.498 0.517/0 1/0",
  85. "0/1 0.098/1 0.235/0.498 0.423/0 1/0",
  86. },
  87. [PRESET_CROSS_PROCESS] = {
  88. "0.25/0.156 0.501/0.501 0.686/0.745",
  89. "0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
  90. "0.231/0.094 0.709/0.874",
  91. },
  92. [PRESET_DARKER] = {
  93. "0.5/0.4", "0.5/0.4", "0.5/0.4",
  94. },
  95. [PRESET_INCREASE_CONTRAST] = {
  96. "0.149/0.066 0.831/0.905 0.905/0.98",
  97. "0.149/0.066 0.831/0.905 0.905/0.98",
  98. "0.149/0.066 0.831/0.905 0.905/0.98",
  99. },
  100. [PRESET_LIGHTER] = {
  101. "0.4/0.5", "0.4/0.5", "0.4/0.5",
  102. },
  103. [PRESET_LINEAR_CONTRAST] = {
  104. "0.305/0.286 0.694/0.713",
  105. "0.305/0.286 0.694/0.713",
  106. "0.305/0.286 0.694/0.713",
  107. },
  108. [PRESET_MEDIUM_CONTRAST] = {
  109. "0.286/0.219 0.639/0.643",
  110. "0.286/0.219 0.639/0.643",
  111. "0.286/0.219 0.639/0.643",
  112. },
  113. [PRESET_NEGATIVE] = {
  114. "0/1 1/0", "0/1 1/0", "0/1 1/0",
  115. },
  116. [PRESET_STRONG_CONTRAST] = {
  117. "0.301/0.196 0.592/0.6 0.686/0.737",
  118. "0.301/0.196 0.592/0.6 0.686/0.737",
  119. "0.301/0.196 0.592/0.6 0.686/0.737",
  120. },
  121. [PRESET_VINTAGE] = {
  122. "0/0.11 0.42/0.51 1/0.95",
  123. "0.50/0.48",
  124. "0/0.22 0.49/0.44 1/0.8",
  125. }
  126. };
  127. static struct keypoint *make_point(double x, double y, struct keypoint *next)
  128. {
  129. struct keypoint *point = av_mallocz(sizeof(*point));
  130. if (!point)
  131. return NULL;
  132. point->x = x;
  133. point->y = y;
  134. point->next = next;
  135. return point;
  136. }
  137. static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
  138. {
  139. char *p = (char *)s; // strtod won't alter the string
  140. struct keypoint *last = NULL;
  141. /* construct a linked list based on the key points string */
  142. while (p && *p) {
  143. struct keypoint *point = make_point(0, 0, NULL);
  144. if (!point)
  145. return AVERROR(ENOMEM);
  146. point->x = av_strtod(p, &p); if (p && *p) p++;
  147. point->y = av_strtod(p, &p); if (p && *p) p++;
  148. if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
  149. av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
  150. "x and y must be in the [0;1] range.\n", point->x, point->y);
  151. return AVERROR(EINVAL);
  152. }
  153. if (!*points)
  154. *points = point;
  155. if (last) {
  156. if ((int)(last->x * 255) >= (int)(point->x * 255)) {
  157. av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
  158. "and (%f;%f) are too close from each other or not "
  159. "strictly increasing on the x-axis\n",
  160. last->x, last->y, point->x, point->y);
  161. return AVERROR(EINVAL);
  162. }
  163. last->next = point;
  164. }
  165. last = point;
  166. }
  167. /* auto insert first key point if missing at x=0 */
  168. if (!*points) {
  169. last = make_point(0, 0, NULL);
  170. if (!last)
  171. return AVERROR(ENOMEM);
  172. last->x = last->y = 0;
  173. *points = last;
  174. } else if ((*points)->x != 0.) {
  175. struct keypoint *newfirst = make_point(0, 0, *points);
  176. if (!newfirst)
  177. return AVERROR(ENOMEM);
  178. *points = newfirst;
  179. }
  180. av_assert0(last);
  181. /* auto insert last key point if missing at x=1 */
  182. if (last->x != 1.) {
  183. struct keypoint *point = make_point(1, 1, NULL);
  184. if (!point)
  185. return AVERROR(ENOMEM);
  186. last->next = point;
  187. }
  188. return 0;
  189. }
  190. static int get_nb_points(const struct keypoint *d)
  191. {
  192. int n = 0;
  193. while (d) {
  194. n++;
  195. d = d->next;
  196. }
  197. return n;
  198. }
  199. /**
  200. * Natural cubic spline interpolation
  201. * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  202. * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  203. */
  204. static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
  205. {
  206. int i, ret = 0;
  207. const struct keypoint *point;
  208. double xprev = 0;
  209. int n = get_nb_points(points); // number of splines
  210. double (*matrix)[3] = av_calloc(n, sizeof(*matrix));
  211. double *h = av_malloc((n - 1) * sizeof(*h));
  212. double *r = av_calloc(n, sizeof(*r));
  213. if (!matrix || !h || !r) {
  214. ret = AVERROR(ENOMEM);
  215. goto end;
  216. }
  217. /* h(i) = x(i+1) - x(i) */
  218. i = -1;
  219. for (point = points; point; point = point->next) {
  220. if (i != -1)
  221. h[i] = point->x - xprev;
  222. xprev = point->x;
  223. i++;
  224. }
  225. /* right-side of the polynomials, will be modified to contains the solution */
  226. point = points;
  227. for (i = 1; i < n - 1; i++) {
  228. double yp = point->y,
  229. yc = point->next->y,
  230. yn = point->next->next->y;
  231. r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
  232. point = point->next;
  233. }
  234. #define B 0 /* sub diagonal (below main) */
  235. #define M 1 /* main diagonal (center) */
  236. #define A 2 /* sup diagonal (above main) */
  237. /* left side of the polynomials into a tridiagonal matrix. */
  238. matrix[0][M] = matrix[n - 1][M] = 1;
  239. for (i = 1; i < n - 1; i++) {
  240. matrix[i][B] = h[i-1];
  241. matrix[i][M] = 2 * (h[i-1] + h[i]);
  242. matrix[i][A] = h[i];
  243. }
  244. /* tridiagonal solving of the linear system */
  245. for (i = 1; i < n; i++) {
  246. double den = matrix[i][M] - matrix[i][B] * matrix[i-1][A];
  247. double k = den ? 1./den : 1.;
  248. matrix[i][A] *= k;
  249. r[i] = (r[i] - matrix[i][B] * r[i - 1]) * k;
  250. }
  251. for (i = n - 2; i >= 0; i--)
  252. r[i] = r[i] - matrix[i][A] * r[i + 1];
  253. /* compute the graph with x=[0..255] */
  254. i = 0;
  255. point = points;
  256. av_assert0(point->next); // always at least 2 key points
  257. while (point->next) {
  258. double yc = point->y;
  259. double yn = point->next->y;
  260. double a = yc;
  261. double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
  262. double c = r[i] / 2.;
  263. double d = (r[i+1] - r[i]) / (6.*h[i]);
  264. int x;
  265. int x_start = point->x * 255;
  266. int x_end = point->next->x * 255;
  267. av_assert0(x_start >= 0 && x_start <= 255 &&
  268. x_end >= 0 && x_end <= 255);
  269. for (x = x_start; x <= x_end; x++) {
  270. double xx = (x - x_start) * 1/255.;
  271. double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
  272. y[x] = av_clipf(yy, 0, 1) * 255;
  273. av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
  274. }
  275. point = point->next;
  276. i++;
  277. }
  278. end:
  279. av_free(matrix);
  280. av_free(h);
  281. av_free(r);
  282. return ret;
  283. }
  284. static av_cold int init(AVFilterContext *ctx, const char *args)
  285. {
  286. int i, j, ret;
  287. CurvesContext *curves = ctx->priv;
  288. struct keypoint *comp_points[NB_COMP] = {0};
  289. char **pts = curves->comp_points_str;
  290. if (curves->comp_points_str_all) {
  291. for (i = 0; i < NB_COMP; i++) {
  292. if (!pts[i])
  293. pts[i] = av_strdup(curves->comp_points_str_all);
  294. if (!pts[i])
  295. return AVERROR(ENOMEM);
  296. }
  297. }
  298. if (curves->preset != PRESET_NONE) {
  299. if (pts[0] || pts[1] || pts[2]) {
  300. av_log(ctx, AV_LOG_ERROR, "It is not possible to mix a preset "
  301. "with explicit points placements\n");
  302. return AVERROR(EINVAL);
  303. }
  304. pts[0] = av_strdup(curves_presets[curves->preset].r);
  305. pts[1] = av_strdup(curves_presets[curves->preset].g);
  306. pts[2] = av_strdup(curves_presets[curves->preset].b);
  307. if (!pts[0] || !pts[1] || !pts[2])
  308. return AVERROR(ENOMEM);
  309. }
  310. for (i = 0; i < NB_COMP; i++) {
  311. ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
  312. if (ret < 0)
  313. return ret;
  314. ret = interpolate(ctx, curves->graph[i], comp_points[i]);
  315. if (ret < 0)
  316. return ret;
  317. }
  318. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  319. for (i = 0; i < NB_COMP; i++) {
  320. struct keypoint *point = comp_points[i];
  321. av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
  322. while (point) {
  323. av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
  324. point = point->next;
  325. }
  326. av_log(ctx, AV_LOG_VERBOSE, "\n");
  327. av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
  328. for (j = 0; j < 256; j++)
  329. av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i][j]);
  330. av_log(ctx, AV_LOG_VERBOSE, "\n");
  331. }
  332. }
  333. for (i = 0; i < NB_COMP; i++) {
  334. struct keypoint *point = comp_points[i];
  335. while (point) {
  336. struct keypoint *next = point->next;
  337. av_free(point);
  338. point = next;
  339. }
  340. }
  341. return 0;
  342. }
  343. static int query_formats(AVFilterContext *ctx)
  344. {
  345. static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
  346. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  347. return 0;
  348. }
  349. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  350. {
  351. int x, y, i, direct = 0;
  352. AVFilterContext *ctx = inlink->dst;
  353. CurvesContext *curves = ctx->priv;
  354. AVFilterLink *outlink = inlink->dst->outputs[0];
  355. AVFrame *out;
  356. uint8_t *dst;
  357. const uint8_t *src;
  358. if (av_frame_is_writable(in)) {
  359. direct = 1;
  360. out = in;
  361. } else {
  362. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  363. if (!out) {
  364. av_frame_free(&in);
  365. return AVERROR(ENOMEM);
  366. }
  367. av_frame_copy_props(out, in);
  368. }
  369. dst = out->data[0];
  370. src = in ->data[0];
  371. for (y = 0; y < inlink->h; y++) {
  372. uint8_t *dstp = dst;
  373. const uint8_t *srcp = src;
  374. for (x = 0; x < inlink->w; x++)
  375. for (i = 0; i < NB_COMP; i++, dstp++, srcp++)
  376. *dstp = curves->graph[i][*srcp];
  377. dst += out->linesize[0];
  378. src += in ->linesize[0];
  379. }
  380. if (!direct)
  381. av_frame_free(&in);
  382. return ff_filter_frame(outlink, out);
  383. }
  384. static const AVFilterPad curves_inputs[] = {
  385. {
  386. .name = "default",
  387. .type = AVMEDIA_TYPE_VIDEO,
  388. .filter_frame = filter_frame,
  389. },
  390. { NULL }
  391. };
  392. static const AVFilterPad curves_outputs[] = {
  393. {
  394. .name = "default",
  395. .type = AVMEDIA_TYPE_VIDEO,
  396. },
  397. { NULL }
  398. };
  399. static const char *const shorthand[] = { "preset", NULL };
  400. AVFilter avfilter_vf_curves = {
  401. .name = "curves",
  402. .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
  403. .priv_size = sizeof(CurvesContext),
  404. .init = init,
  405. .query_formats = query_formats,
  406. .inputs = curves_inputs,
  407. .outputs = curves_outputs,
  408. .priv_class = &curves_class,
  409. .shorthand = shorthand,
  410. };