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.

438 lines
13KB

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