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.

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