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.

519 lines
17KB

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