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.

364 lines
11KB

  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 *comp_points_str[NB_COMP];
  35. uint8_t graph[NB_COMP][256];
  36. } CurvesContext;
  37. #define OFFSET(x) offsetof(CurvesContext, x)
  38. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  39. static const AVOption curves_options[] = {
  40. { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  41. { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  42. { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  43. { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  44. { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  45. { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
  46. { NULL }
  47. };
  48. AVFILTER_DEFINE_CLASS(curves);
  49. static struct keypoint *make_point(double x, double y, struct keypoint *next)
  50. {
  51. struct keypoint *point = av_mallocz(sizeof(*point));
  52. if (!point)
  53. return NULL;
  54. point->x = x;
  55. point->y = y;
  56. point->next = next;
  57. return point;
  58. }
  59. static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s)
  60. {
  61. char *p = (char *)s; // strtod won't alter the string
  62. struct keypoint *last = NULL;
  63. /* construct a linked list based on the key points string */
  64. while (p && *p) {
  65. struct keypoint *point = make_point(0, 0, NULL);
  66. if (!point)
  67. return AVERROR(ENOMEM);
  68. point->x = av_strtod(p, &p); if (p && *p) p++;
  69. point->y = av_strtod(p, &p); if (p && *p) p++;
  70. if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
  71. av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
  72. "x and y must be in the [0;1] range.\n", point->x, point->y);
  73. return AVERROR(EINVAL);
  74. }
  75. if (!*points)
  76. *points = point;
  77. if (last) {
  78. if ((int)(last->x * 255) >= (int)(point->x * 255)) {
  79. av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
  80. "and (%f;%f) are too close from each other or not "
  81. "strictly increasing on the x-axis\n",
  82. last->x, last->y, point->x, point->y);
  83. return AVERROR(EINVAL);
  84. }
  85. last->next = point;
  86. }
  87. last = point;
  88. }
  89. /* auto insert first key point if missing at x=0 */
  90. if (!*points) {
  91. last = make_point(0, 0, NULL);
  92. if (!last)
  93. return AVERROR(ENOMEM);
  94. last->x = last->y = 0;
  95. *points = last;
  96. } else if ((*points)->x != 0.) {
  97. struct keypoint *newfirst = make_point(0, 0, *points);
  98. if (!newfirst)
  99. return AVERROR(ENOMEM);
  100. *points = newfirst;
  101. }
  102. av_assert0(last);
  103. /* auto insert last key point if missing at x=1 */
  104. if (last->x != 1.) {
  105. struct keypoint *point = make_point(1, 1, NULL);
  106. if (!point)
  107. return AVERROR(ENOMEM);
  108. last->next = point;
  109. }
  110. return 0;
  111. }
  112. static int get_nb_points(const struct keypoint *d)
  113. {
  114. int n = 0;
  115. while (d) {
  116. n++;
  117. d = d->next;
  118. }
  119. return n;
  120. }
  121. /**
  122. * Natural cubic spline interpolation
  123. * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
  124. * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
  125. */
  126. static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keypoint *points)
  127. {
  128. int i, ret = 0;
  129. const struct keypoint *point;
  130. double xprev = 0;
  131. int n = get_nb_points(points); // number of splines
  132. double (*matrix)[3] = av_calloc(n, sizeof(*matrix));
  133. double *h = av_malloc((n - 1) * sizeof(*h));
  134. double *r = av_calloc(n, sizeof(*r));
  135. if (!matrix || !h || !r) {
  136. ret = AVERROR(ENOMEM);
  137. goto end;
  138. }
  139. /* h(i) = x(i+1) - x(i) */
  140. i = -1;
  141. for (point = points; point; point = point->next) {
  142. if (i != -1)
  143. h[i] = point->x - xprev;
  144. xprev = point->x;
  145. i++;
  146. }
  147. /* right-side of the polynomials, will be modified to contains the solution */
  148. point = points;
  149. for (i = 1; i < n - 1; i++) {
  150. double yp = point->y,
  151. yc = point->next->y,
  152. yn = point->next->next->y;
  153. r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
  154. point = point->next;
  155. }
  156. #define B 0 /* sub diagonal (below main) */
  157. #define M 1 /* main diagonal (center) */
  158. #define A 2 /* sup diagonal (above main) */
  159. /* left side of the polynomials into a tridiagonal matrix. */
  160. matrix[0][M] = matrix[n - 1][M] = 1;
  161. for (i = 1; i < n - 1; i++) {
  162. matrix[i][B] = h[i-1];
  163. matrix[i][M] = 2 * (h[i-1] + h[i]);
  164. matrix[i][A] = h[i];
  165. }
  166. /* tridiagonal solving of the linear system */
  167. for (i = 1; i < n; i++) {
  168. double den = matrix[i][M] - matrix[i][B] * matrix[i-1][A];
  169. double k = den ? 1./den : 1.;
  170. matrix[i][A] *= k;
  171. r[i] = (r[i] - matrix[i][B] * r[i - 1]) * k;
  172. }
  173. for (i = n - 2; i >= 0; i--)
  174. r[i] = r[i] - matrix[i][A] * r[i + 1];
  175. /* compute the graph with x=[0..255] */
  176. i = 0;
  177. point = points;
  178. av_assert0(point->next); // always at least 2 key points
  179. while (point->next) {
  180. double yc = point->y;
  181. double yn = point->next->y;
  182. double a = yc;
  183. double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
  184. double c = r[i] / 2.;
  185. double d = (r[i+1] - r[i]) / (6.*h[i]);
  186. int x;
  187. int x_start = point->x * 255;
  188. int x_end = point->next->x * 255;
  189. av_assert0(x_start >= 0 && x_start <= 255 &&
  190. x_end >= 0 && x_end <= 255);
  191. for (x = x_start; x <= x_end; x++) {
  192. double xx = (x - x_start) * 1/255.;
  193. double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
  194. y[x] = av_clipf(yy, 0, 1) * 255;
  195. av_log(ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
  196. }
  197. point = point->next;
  198. i++;
  199. }
  200. end:
  201. av_free(matrix);
  202. av_free(h);
  203. av_free(r);
  204. return ret;
  205. }
  206. static av_cold int init(AVFilterContext *ctx, const char *args)
  207. {
  208. int i, j, ret;
  209. CurvesContext *curves = ctx->priv;
  210. struct keypoint *comp_points[NB_COMP] = {0};
  211. curves->class = &curves_class;
  212. av_opt_set_defaults(curves);
  213. if ((ret = av_set_options_string(curves, args, "=", ":")) < 0)
  214. return ret;
  215. for (i = 0; i < NB_COMP; i++) {
  216. ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i]);
  217. if (ret < 0)
  218. return ret;
  219. ret = interpolate(ctx, curves->graph[i], comp_points[i]);
  220. if (ret < 0)
  221. return ret;
  222. }
  223. if (av_log_get_level() >= AV_LOG_VERBOSE) {
  224. for (i = 0; i < NB_COMP; i++) {
  225. struct keypoint *point = comp_points[i];
  226. av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
  227. while (point) {
  228. av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
  229. point = point->next;
  230. }
  231. av_log(ctx, AV_LOG_VERBOSE, "\n");
  232. av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
  233. for (j = 0; j < 256; j++)
  234. av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i][j]);
  235. av_log(ctx, AV_LOG_VERBOSE, "\n");
  236. }
  237. }
  238. for (i = 0; i < NB_COMP; i++) {
  239. struct keypoint *point = comp_points[i];
  240. while (point) {
  241. struct keypoint *next = point->next;
  242. av_free(point);
  243. point = next;
  244. }
  245. }
  246. av_opt_free(curves);
  247. return 0;
  248. }
  249. static int query_formats(AVFilterContext *ctx)
  250. {
  251. static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
  252. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  253. return 0;
  254. }
  255. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  256. {
  257. int x, y, i, direct = 0;
  258. AVFilterContext *ctx = inlink->dst;
  259. CurvesContext *curves = ctx->priv;
  260. AVFilterLink *outlink = inlink->dst->outputs[0];
  261. AVFrame *out;
  262. uint8_t *dst;
  263. const uint8_t *src;
  264. if (av_frame_is_writable(in)) {
  265. direct = 1;
  266. out = in;
  267. } else {
  268. out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  269. if (!out) {
  270. av_frame_free(&in);
  271. return AVERROR(ENOMEM);
  272. }
  273. av_frame_copy_props(out, in);
  274. }
  275. dst = out->data[0];
  276. src = in ->data[0];
  277. for (y = 0; y < inlink->h; y++) {
  278. uint8_t *dstp = dst;
  279. const uint8_t *srcp = src;
  280. for (x = 0; x < inlink->w; x++)
  281. for (i = 0; i < NB_COMP; i++, dstp++, srcp++)
  282. *dstp = curves->graph[i][*srcp];
  283. dst += out->linesize[0];
  284. src += in ->linesize[0];
  285. }
  286. if (!direct)
  287. av_frame_free(&in);
  288. return ff_filter_frame(outlink, out);
  289. }
  290. static const AVFilterPad curves_inputs[] = {
  291. {
  292. .name = "default",
  293. .type = AVMEDIA_TYPE_VIDEO,
  294. .filter_frame = filter_frame,
  295. },
  296. { NULL }
  297. };
  298. static const AVFilterPad curves_outputs[] = {
  299. {
  300. .name = "default",
  301. .type = AVMEDIA_TYPE_VIDEO,
  302. },
  303. { NULL }
  304. };
  305. AVFilter avfilter_vf_curves = {
  306. .name = "curves",
  307. .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
  308. .priv_size = sizeof(CurvesContext),
  309. .init = init,
  310. .query_formats = query_formats,
  311. .inputs = curves_inputs,
  312. .outputs = curves_outputs,
  313. .priv_class = &curves_class,
  314. };