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.

452 lines
14KB

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