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.

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