FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_curves.c
Go to the documentation of this file.
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 
21 #include "libavutil/opt.h"
22 #include "libavutil/bprint.h"
23 #include "libavutil/eval.h"
24 #include "libavutil/file.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "drawutils.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 
34 #define R 0
35 #define G 1
36 #define B 2
37 #define A 3
38 
39 struct keypoint {
40  double x, y;
41  struct keypoint *next;
42 };
43 
44 #define NB_COMP 3
45 
46 enum preset {
59 };
60 
61 typedef struct {
62  const AVClass *class;
63  int preset;
64  char *comp_points_str[NB_COMP + 1];
66  uint16_t *graph[NB_COMP + 1];
67  int lut_size;
68  char *psfile;
69  uint8_t rgba_map[4];
70  int step;
72  int is_16bit;
74 
75 typedef struct ThreadData {
76  AVFrame *in, *out;
77 } ThreadData;
78 
79 #define OFFSET(x) offsetof(CurvesContext, x)
80 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
81 static const AVOption curves_options[] = {
82  { "preset", "select a color curves preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=PRESET_NONE}, PRESET_NONE, NB_PRESETS-1, FLAGS, "preset_name" },
83  { "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NONE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
84  { "color_negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_COLOR_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
85  { "cross_process", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_CROSS_PROCESS}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
86  { "darker", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_DARKER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
87  { "increase_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_INCREASE_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
88  { "lighter", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LIGHTER}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
89  { "linear_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_LINEAR_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
90  { "medium_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_MEDIUM_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
91  { "negative", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_NEGATIVE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
92  { "strong_contrast", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_STRONG_CONTRAST}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
93  { "vintage", NULL, 0, AV_OPT_TYPE_CONST, {.i64=PRESET_VINTAGE}, INT_MIN, INT_MAX, FLAGS, "preset_name" },
94  { "master","set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
95  { "m", "set master points coordinates",OFFSET(comp_points_str[NB_COMP]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
96  { "red", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
97  { "r", "set red points coordinates", OFFSET(comp_points_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
98  { "green", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
99  { "g", "set green points coordinates", OFFSET(comp_points_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
100  { "blue", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
101  { "b", "set blue points coordinates", OFFSET(comp_points_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
102  { "all", "set points coordinates for all components", OFFSET(comp_points_str_all), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
103  { "psfile", "set Photoshop curves file name", OFFSET(psfile), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
104  { "plot", "save Gnuplot script of the curves in specified file", OFFSET(plot_filename), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
105  { NULL }
106 };
107 
108 AVFILTER_DEFINE_CLASS(curves);
109 
110 static const struct {
111  const char *r;
112  const char *g;
113  const char *b;
114  const char *master;
115 } curves_presets[] = {
117  "0.129/1 0.466/0.498 0.725/0",
118  "0.109/1 0.301/0.498 0.517/0",
119  "0.098/1 0.235/0.498 0.423/0",
120  },
122  "0/0 0.25/0.156 0.501/0.501 0.686/0.745 1/1",
123  "0/0 0.25/0.188 0.38/0.501 0.745/0.815 1/0.815",
124  "0/0 0.231/0.094 0.709/0.874 1/1",
125  },
126  [PRESET_DARKER] = { .master = "0/0 0.5/0.4 1/1" },
127  [PRESET_INCREASE_CONTRAST] = { .master = "0/0 0.149/0.066 0.831/0.905 0.905/0.98 1/1" },
128  [PRESET_LIGHTER] = { .master = "0/0 0.4/0.5 1/1" },
129  [PRESET_LINEAR_CONTRAST] = { .master = "0/0 0.305/0.286 0.694/0.713 1/1" },
130  [PRESET_MEDIUM_CONTRAST] = { .master = "0/0 0.286/0.219 0.639/0.643 1/1" },
131  [PRESET_NEGATIVE] = { .master = "0/1 1/0" },
132  [PRESET_STRONG_CONTRAST] = { .master = "0/0 0.301/0.196 0.592/0.6 0.686/0.737 1/1" },
133  [PRESET_VINTAGE] = {
134  "0/0.11 0.42/0.51 1/0.95",
135  "0/0 0.50/0.48 1/1",
136  "0/0.22 0.49/0.44 1/0.8",
137  }
138 };
139 
140 static struct keypoint *make_point(double x, double y, struct keypoint *next)
141 {
142  struct keypoint *point = av_mallocz(sizeof(*point));
143 
144  if (!point)
145  return NULL;
146  point->x = x;
147  point->y = y;
148  point->next = next;
149  return point;
150 }
151 
152 static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s,
153  int lut_size)
154 {
155  char *p = (char *)s; // strtod won't alter the string
156  struct keypoint *last = NULL;
157  const int scale = lut_size - 1;
158 
159  /* construct a linked list based on the key points string */
160  while (p && *p) {
161  struct keypoint *point = make_point(0, 0, NULL);
162  if (!point)
163  return AVERROR(ENOMEM);
164  point->x = av_strtod(p, &p); if (p && *p) p++;
165  point->y = av_strtod(p, &p); if (p && *p) p++;
166  if (point->x < 0 || point->x > 1 || point->y < 0 || point->y > 1) {
167  av_log(ctx, AV_LOG_ERROR, "Invalid key point coordinates (%f;%f), "
168  "x and y must be in the [0;1] range.\n", point->x, point->y);
169  return AVERROR(EINVAL);
170  }
171  if (!*points)
172  *points = point;
173  if (last) {
174  if ((int)(last->x * scale) >= (int)(point->x * scale)) {
175  av_log(ctx, AV_LOG_ERROR, "Key point coordinates (%f;%f) "
176  "and (%f;%f) are too close from each other or not "
177  "strictly increasing on the x-axis\n",
178  last->x, last->y, point->x, point->y);
179  return AVERROR(EINVAL);
180  }
181  last->next = point;
182  }
183  last = point;
184  }
185 
186  if (*points && !(*points)->next) {
187  av_log(ctx, AV_LOG_WARNING, "Only one point (at (%f;%f)) is defined, "
188  "this is unlikely to behave as you expect. You probably want"
189  "at least 2 points.",
190  (*points)->x, (*points)->y);
191  }
192 
193  return 0;
194 }
195 
196 static int get_nb_points(const struct keypoint *d)
197 {
198  int n = 0;
199  while (d) {
200  n++;
201  d = d->next;
202  }
203  return n;
204 }
205 
206 /**
207  * Natural cubic spline interpolation
208  * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
209  * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
210  */
211 
212 #define CLIP(v) (nbits == 8 ? av_clip_uint8(v) : av_clip_uint16(v))
213 
214 static inline int interpolate(void *log_ctx, uint16_t *y,
215  const struct keypoint *points, int nbits)
216 {
217  int i, ret = 0;
218  const struct keypoint *point = points;
219  double xprev = 0;
220  const int lut_size = 1<<nbits;
221  const int scale = lut_size - 1;
222 
223  double (*matrix)[3];
224  double *h, *r;
225  const int n = get_nb_points(points); // number of splines
226 
227  if (n == 0) {
228  for (i = 0; i < lut_size; i++)
229  y[i] = i;
230  return 0;
231  }
232 
233  if (n == 1) {
234  for (i = 0; i < lut_size; i++)
235  y[i] = CLIP(point->y * scale);
236  return 0;
237  }
238 
239  matrix = av_calloc(n, sizeof(*matrix));
240  h = av_malloc((n - 1) * sizeof(*h));
241  r = av_calloc(n, sizeof(*r));
242 
243  if (!matrix || !h || !r) {
244  ret = AVERROR(ENOMEM);
245  goto end;
246  }
247 
248  /* h(i) = x(i+1) - x(i) */
249  i = -1;
250  for (point = points; point; point = point->next) {
251  if (i != -1)
252  h[i] = point->x - xprev;
253  xprev = point->x;
254  i++;
255  }
256 
257  /* right-side of the polynomials, will be modified to contains the solution */
258  point = points;
259  for (i = 1; i < n - 1; i++) {
260  const double yp = point->y;
261  const double yc = point->next->y;
262  const double yn = point->next->next->y;
263  r[i] = 6 * ((yn-yc)/h[i] - (yc-yp)/h[i-1]);
264  point = point->next;
265  }
266 
267 #define BD 0 /* sub diagonal (below main) */
268 #define MD 1 /* main diagonal (center) */
269 #define AD 2 /* sup diagonal (above main) */
270 
271  /* left side of the polynomials into a tridiagonal matrix. */
272  matrix[0][MD] = matrix[n - 1][MD] = 1;
273  for (i = 1; i < n - 1; i++) {
274  matrix[i][BD] = h[i-1];
275  matrix[i][MD] = 2 * (h[i-1] + h[i]);
276  matrix[i][AD] = h[i];
277  }
278 
279  /* tridiagonal solving of the linear system */
280  for (i = 1; i < n; i++) {
281  const double den = matrix[i][MD] - matrix[i][BD] * matrix[i-1][AD];
282  const double k = den ? 1./den : 1.;
283  matrix[i][AD] *= k;
284  r[i] = (r[i] - matrix[i][BD] * r[i - 1]) * k;
285  }
286  for (i = n - 2; i >= 0; i--)
287  r[i] = r[i] - matrix[i][AD] * r[i + 1];
288 
289  point = points;
290 
291  /* left padding */
292  for (i = 0; i < (int)(point->x * scale); i++)
293  y[i] = CLIP(point->y * scale);
294 
295  /* compute the graph with x=[x0..xN] */
296  i = 0;
297  av_assert0(point->next); // always at least 2 key points
298  while (point->next) {
299  const double yc = point->y;
300  const double yn = point->next->y;
301 
302  const double a = yc;
303  const double b = (yn-yc)/h[i] - h[i]*r[i]/2. - h[i]*(r[i+1]-r[i])/6.;
304  const double c = r[i] / 2.;
305  const double d = (r[i+1] - r[i]) / (6.*h[i]);
306 
307  int x;
308  const int x_start = point->x * scale;
309  const int x_end = point->next->x * scale;
310 
311  av_assert0(x_start >= 0 && x_start < lut_size &&
312  x_end >= 0 && x_end < lut_size);
313 
314  for (x = x_start; x <= x_end; x++) {
315  const double xx = (x - x_start) * 1./scale;
316  const double yy = a + b*xx + c*xx*xx + d*xx*xx*xx;
317  y[x] = CLIP(yy * scale);
318  av_log(log_ctx, AV_LOG_DEBUG, "f(%f)=%f -> y[%d]=%d\n", xx, yy, x, y[x]);
319  }
320 
321  point = point->next;
322  i++;
323  }
324 
325  /* right padding */
326  for (i = (int)(point->x * scale); i < lut_size; i++)
327  y[i] = CLIP(point->y * scale);
328 
329 end:
330  av_free(matrix);
331  av_free(h);
332  av_free(r);
333  return ret;
334 }
335 
336 #define DECLARE_INTERPOLATE_FUNC(nbits) \
337 static int interpolate##nbits(void *log_ctx, uint16_t *y, \
338  const struct keypoint *points) \
339 { \
340  return interpolate(log_ctx, y, points, nbits); \
341 }
342 
345 
346 static int parse_psfile(AVFilterContext *ctx, const char *fname)
347 {
348  CurvesContext *curves = ctx->priv;
349  uint8_t *buf;
350  size_t size;
351  int i, ret, av_unused(version), nb_curves;
352  AVBPrint ptstr;
353  static const int comp_ids[] = {3, 0, 1, 2};
354 
356 
357  ret = av_file_map(fname, &buf, &size, 0, NULL);
358  if (ret < 0)
359  return ret;
360 
361 #define READ16(dst) do { \
362  if (size < 2) { \
363  ret = AVERROR_INVALIDDATA; \
364  goto end; \
365  } \
366  dst = AV_RB16(buf); \
367  buf += 2; \
368  size -= 2; \
369 } while (0)
370 
371  READ16(version);
372  READ16(nb_curves);
373  for (i = 0; i < FFMIN(nb_curves, FF_ARRAY_ELEMS(comp_ids)); i++) {
374  int nb_points, n;
375  av_bprint_clear(&ptstr);
376  READ16(nb_points);
377  for (n = 0; n < nb_points; n++) {
378  int y, x;
379  READ16(y);
380  READ16(x);
381  av_bprintf(&ptstr, "%f/%f ", x / 255., y / 255.);
382  }
383  if (*ptstr.str) {
384  char **pts = &curves->comp_points_str[comp_ids[i]];
385  if (!*pts) {
386  *pts = av_strdup(ptstr.str);
387  av_log(ctx, AV_LOG_DEBUG, "curves %d (intid=%d) [%d points]: [%s]\n",
388  i, comp_ids[i], nb_points, *pts);
389  if (!*pts) {
390  ret = AVERROR(ENOMEM);
391  goto end;
392  }
393  }
394  }
395  }
396 end:
397  av_bprint_finalize(&ptstr, NULL);
398  av_file_unmap(buf, size);
399  return ret;
400 }
401 
402 static int dump_curves(const char *fname, uint16_t *graph[NB_COMP + 1],
403  struct keypoint *comp_points[NB_COMP + 1],
404  int lut_size)
405 {
406  int i;
407  AVBPrint buf;
408  const double scale = 1. / (lut_size - 1);
409  static const char * const colors[] = { "red", "green", "blue", "#404040", };
410  FILE *f = av_fopen_utf8(fname, "w");
411 
412  av_assert0(FF_ARRAY_ELEMS(colors) == NB_COMP + 1);
413 
414  if (!f) {
415  int ret = AVERROR(errno);
416  av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s' for writing: %s\n",
417  fname, av_err2str(ret));
418  return ret;
419  }
420 
422 
423  av_bprintf(&buf, "set xtics 0.1\n");
424  av_bprintf(&buf, "set ytics 0.1\n");
425  av_bprintf(&buf, "set size square\n");
426  av_bprintf(&buf, "set grid\n");
427 
428  for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
429  av_bprintf(&buf, "%s'-' using 1:2 with lines lc '%s' title ''",
430  i ? ", " : "plot ", colors[i]);
431  if (comp_points[i])
432  av_bprintf(&buf, ", '-' using 1:2 with points pointtype 3 lc '%s' title ''",
433  colors[i]);
434  }
435  av_bprintf(&buf, "\n");
436 
437  for (i = 0; i < FF_ARRAY_ELEMS(colors); i++) {
438  int x;
439 
440  /* plot generated values */
441  for (x = 0; x < lut_size; x++)
442  av_bprintf(&buf, "%f %f\n", x * scale, graph[i][x] * scale);
443  av_bprintf(&buf, "e\n");
444 
445  /* plot user knots */
446  if (comp_points[i]) {
447  const struct keypoint *point = comp_points[i];
448 
449  while (point) {
450  av_bprintf(&buf, "%f %f\n", point->x, point->y);
451  point = point->next;
452  }
453  av_bprintf(&buf, "e\n");
454  }
455  }
456 
457  fwrite(buf.str, 1, buf.len, f);
458  fclose(f);
459  av_bprint_finalize(&buf, NULL);
460  return 0;
461 }
462 
464 {
465  int i, ret;
466  CurvesContext *curves = ctx->priv;
467  char **pts = curves->comp_points_str;
468  const char *allp = curves->comp_points_str_all;
469 
470  //if (!allp && curves->preset != PRESET_NONE && curves_presets[curves->preset].all)
471  // allp = curves_presets[curves->preset].all;
472 
473  if (allp) {
474  for (i = 0; i < NB_COMP; i++) {
475  if (!pts[i])
476  pts[i] = av_strdup(allp);
477  if (!pts[i])
478  return AVERROR(ENOMEM);
479  }
480  }
481 
482  if (curves->psfile) {
483  ret = parse_psfile(ctx, curves->psfile);
484  if (ret < 0)
485  return ret;
486  }
487 
488  if (curves->preset != PRESET_NONE) {
489 #define SET_COMP_IF_NOT_SET(n, name) do { \
490  if (!pts[n] && curves_presets[curves->preset].name) { \
491  pts[n] = av_strdup(curves_presets[curves->preset].name); \
492  if (!pts[n]) \
493  return AVERROR(ENOMEM); \
494  } \
495 } while (0)
500  }
501 
502  return 0;
503 }
504 
506 {
507  static const enum AVPixelFormat pix_fmts[] = {
516  };
517  AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
518  if (!fmts_list)
519  return AVERROR(ENOMEM);
520  return ff_set_common_formats(ctx, fmts_list);
521 }
522 
523 static int config_input(AVFilterLink *inlink)
524 {
525  int i, j, ret;
526  AVFilterContext *ctx = inlink->dst;
527  CurvesContext *curves = ctx->priv;
529  char **pts = curves->comp_points_str;
530  struct keypoint *comp_points[NB_COMP + 1] = {0};
531 
532  ff_fill_rgba_map(curves->rgba_map, inlink->format);
533  curves->is_16bit = desc->comp[0].depth > 8;
534  curves->lut_size = curves->is_16bit ? 1<<16 : 1<<8;
535  curves->step = av_get_padded_bits_per_pixel(desc) >> (3 + curves->is_16bit);
536 
537  for (i = 0; i < NB_COMP + 1; i++) {
538  curves->graph[i] = av_mallocz_array(curves->lut_size, sizeof(*curves->graph[0]));
539  if (!curves->graph[i])
540  return AVERROR(ENOMEM);
541  ret = parse_points_str(ctx, comp_points + i, curves->comp_points_str[i], curves->lut_size);
542  if (ret < 0)
543  return ret;
544  if (curves->is_16bit) ret = interpolate16(ctx, curves->graph[i], comp_points[i]);
545  else ret = interpolate8(ctx, curves->graph[i], comp_points[i]);
546  if (ret < 0)
547  return ret;
548  }
549 
550  if (pts[NB_COMP]) {
551  for (i = 0; i < NB_COMP; i++)
552  for (j = 0; j < curves->lut_size; j++)
553  curves->graph[i][j] = curves->graph[NB_COMP][curves->graph[i][j]];
554  }
555 
556  if (av_log_get_level() >= AV_LOG_VERBOSE) {
557  for (i = 0; i < NB_COMP; i++) {
558  const struct keypoint *point = comp_points[i];
559  av_log(ctx, AV_LOG_VERBOSE, "#%d points:", i);
560  while (point) {
561  av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", point->x, point->y);
562  point = point->next;
563  }
564  }
565  }
566 
567  if (curves->plot_filename)
568  dump_curves(curves->plot_filename, curves->graph, comp_points, curves->lut_size);
569 
570  for (i = 0; i < NB_COMP + 1; i++) {
571  struct keypoint *point = comp_points[i];
572  while (point) {
573  struct keypoint *next = point->next;
574  av_free(point);
575  point = next;
576  }
577  }
578 
579  return 0;
580 }
581 
582 static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
583 {
584  int x, y;
585  const CurvesContext *curves = ctx->priv;
586  const ThreadData *td = arg;
587  const AVFrame *in = td->in;
588  const AVFrame *out = td->out;
589  const int direct = out == in;
590  const int step = curves->step;
591  const uint8_t r = curves->rgba_map[R];
592  const uint8_t g = curves->rgba_map[G];
593  const uint8_t b = curves->rgba_map[B];
594  const uint8_t a = curves->rgba_map[A];
595  const int slice_start = (in->height * jobnr ) / nb_jobs;
596  const int slice_end = (in->height * (jobnr+1)) / nb_jobs;
597 
598  if (curves->is_16bit) {
599  for (y = slice_start; y < slice_end; y++) {
600  uint16_t *dstp = ( uint16_t *)(out->data[0] + y * out->linesize[0]);
601  const uint16_t *srcp = (const uint16_t *)(in ->data[0] + y * in->linesize[0]);
602 
603  for (x = 0; x < in->width * step; x += step) {
604  dstp[x + r] = curves->graph[R][srcp[x + r]];
605  dstp[x + g] = curves->graph[G][srcp[x + g]];
606  dstp[x + b] = curves->graph[B][srcp[x + b]];
607  if (!direct && step == 4)
608  dstp[x + a] = srcp[x + a];
609  }
610  }
611  } else {
612  uint8_t *dst = out->data[0] + slice_start * out->linesize[0];
613  const uint8_t *src = in->data[0] + slice_start * in->linesize[0];
614 
615  for (y = slice_start; y < slice_end; y++) {
616  for (x = 0; x < in->width * step; x += step) {
617  dst[x + r] = curves->graph[R][src[x + r]];
618  dst[x + g] = curves->graph[G][src[x + g]];
619  dst[x + b] = curves->graph[B][src[x + b]];
620  if (!direct && step == 4)
621  dst[x + a] = src[x + a];
622  }
623  dst += out->linesize[0];
624  src += in ->linesize[0];
625  }
626  }
627  return 0;
628 }
629 
630 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
631 {
632  AVFilterContext *ctx = inlink->dst;
633  AVFilterLink *outlink = ctx->outputs[0];
634  AVFrame *out;
635  ThreadData td;
636 
637  if (av_frame_is_writable(in)) {
638  out = in;
639  } else {
640  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
641  if (!out) {
642  av_frame_free(&in);
643  return AVERROR(ENOMEM);
644  }
645  av_frame_copy_props(out, in);
646  }
647 
648  td.in = in;
649  td.out = out;
650  ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
651 
652  if (out != in)
653  av_frame_free(&in);
654 
655  return ff_filter_frame(outlink, out);
656 }
657 
659 {
660  int i;
661  CurvesContext *curves = ctx->priv;
662 
663  for (i = 0; i < NB_COMP + 1; i++)
664  av_freep(&curves->graph[i]);
665 }
666 
667 static const AVFilterPad curves_inputs[] = {
668  {
669  .name = "default",
670  .type = AVMEDIA_TYPE_VIDEO,
671  .filter_frame = filter_frame,
672  .config_props = config_input,
673  },
674  { NULL }
675 };
676 
677 static const AVFilterPad curves_outputs[] = {
678  {
679  .name = "default",
680  .type = AVMEDIA_TYPE_VIDEO,
681  },
682  { NULL }
683 };
684 
686  .name = "curves",
687  .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
688  .priv_size = sizeof(CurvesContext),
689  .init = curves_init,
692  .inputs = curves_inputs,
693  .outputs = curves_outputs,
694  .priv_class = &curves_class,
696 };
#define MD
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVFrame * out
Definition: af_sofalizer.c:585
void av_bprintf(AVBPrint *buf, const char *fmt,...)
Definition: bprint.c:94
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2332
This structure describes decoded (raw) audio or video data.
Definition: frame.h:190
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
struct keypoint * next
Definition: vf_curves.c:41
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
const char * g
Definition: vf_curves.c:112
const char * desc
Definition: nvenc.c:60
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:341
FILE * av_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:154
#define CLIP(v)
Natural cubic spline interpolation Finding curves using Cubic Splines notes by Steven Rauch and John ...
Definition: vf_curves.c:212
static struct keypoint * make_point(double x, double y, struct keypoint *next)
Definition: vf_curves.c:140
const char * b
Definition: vf_curves.c:113
#define AV_PIX_FMT_BGRA64
Definition: pixfmt.h:346
#define AD
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:253
const char * master
Definition: vf_curves.c:114
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
#define src
Definition: vp8dsp.c:254
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
#define G
Definition: vf_curves.c:35
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:260
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
char * psfile
Definition: vf_curves.c:68
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:125
BYTE int const BYTE * srcp
Definition: avisynth_c.h:813
const char * name
Pad name.
Definition: internal.h:59
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
char * plot_filename
Definition: vf_curves.c:71
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1189
AVFrame * in
Definition: af_sofalizer.c:585
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static const AVOption curves_options[]
Definition: vf_curves.c:81
static av_cold int uninit(AVCodecContext *avctx)
Definition: crystalhd.c:287
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:252
AVOptions.
#define NB_COMP
Definition: vf_curves.c:44
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_curves.c:630
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
Definition: pixfmt.h:95
Misc file utilities.
static av_cold int curves_init(AVFilterContext *ctx)
Definition: vf_curves.c:463
static int flags
Definition: log.c:57
#define AV_PIX_FMT_BGR48
Definition: pixfmt.h:342
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
static int parse_points_str(AVFilterContext *ctx, struct keypoint **points, const char *s, int lut_size)
Definition: vf_curves.c:152
ptrdiff_t size
Definition: opengl_enc.c:101
#define B
Definition: vf_curves.c:36
#define av_log(a,...)
char * comp_points_str_all
Definition: vf_curves.c:65
A filter pad used for either input or output.
Definition: internal.h:53
#define FLAGS
Definition: vf_curves.c:80
#define SET_COMP_IF_NOT_SET(n, name)
int width
width and height of the video frame
Definition: frame.h:242
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition: file.c:129
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
#define td
Definition: regdef.h:70
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
Definition: file.c:49
#define AV_BPRINT_SIZE_UNLIMITED
BYTE * dstp
Definition: avisynth_c.h:813
#define AVERROR(e)
Definition: error.h:43
#define DECLARE_INTERPOLATE_FUNC(nbits)
Definition: vf_curves.c:336
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:158
#define READ16(dst)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void av_bprint_init(AVBPrint *buf, unsigned size_init, unsigned size_max)
Definition: bprint.c:69
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
const char * r
Definition: vf_curves.c:111
void * priv
private data for use by the filter
Definition: avfilter.h:322
int av_get_padded_bits_per_pixel(const AVPixFmtDescriptor *pixdesc)
Return the number of bits per pixel for the pixel format described by pixdesc, including any padding ...
Definition: pixdesc.c:2297
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * arg
Definition: jacosubdec.c:66
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:337
simple assert() macros that are a bit more flexible than ISO C assert().
int av_log_get_level(void)
Get the current log level.
Definition: log.c:386
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:226
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:93
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
static int config_input(AVFilterLink *inlink)
Definition: vf_curves.c:523
double x
Definition: vf_curves.c:40
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:786
#define R
Definition: vf_curves.c:34
char * comp_points_str[NB_COMP+1]
Definition: vf_curves.c:64
#define FFMIN(a, b)
Definition: common.h:96
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static const struct @174 curves_presets[]
int n
Definition: avisynth_c.h:684
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:65
AVFilter ff_vf_curves
Definition: vf_curves.c:685
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:386
#define FF_ARRAY_ELEMS(a)
int ff_fill_rgba_map(uint8_t *rgba_map, enum AVPixelFormat pix_fmt)
Definition: drawutils.c:35
double av_strtod(const char *numstr, char **tail)
Parse the string in numstr and return its value as a double.
Definition: eval.c:98
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:376
#define AV_BPRINT_SIZE_AUTOMATIC
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:267
static int parse_psfile(AVFilterContext *ctx, const char *fname)
Definition: vf_curves.c:346
misc drawing utilities
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:531
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:221
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define BD
double y
Definition: vf_curves.c:40
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:690
static int dump_curves(const char *fname, uint16_t *graph[NB_COMP+1], struct keypoint *comp_points[NB_COMP+1], int lut_size)
Definition: vf_curves.c:402
static int get_nb_points(const struct keypoint *d)
Definition: vf_curves.c:196
#define OFFSET(x)
Definition: vf_curves.c:79
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:254
const char * name
Filter name.
Definition: avfilter.h:148
static const AVFilterPad curves_inputs[]
Definition: vf_curves.c:667
#define A
Definition: vf_curves.c:37
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:319
static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_curves.c:582
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
static int64_t pts
Global timestamp for the audio frames.
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:347
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:204
void av_bprint_clear(AVBPrint *buf)
Reset the string to "" but keep internal allocated data.
Definition: bprint.c:227
AVFILTER_DEFINE_CLASS(curves)
preset
Definition: vf_curves.c:46
int
uint16_t * graph[NB_COMP+1]
Definition: vf_curves.c:66
avfilter_execute_func * execute
Definition: internal.h:153
static int slice_end(AVCodecContext *avctx, AVFrame *pict)
Handle slice ends.
Definition: mpeg12dec.c:2050
#define av_free(p)
uint8_t rgba_map[4]
Definition: vf_curves.c:69
static AVCodec * c
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:307
int height
Definition: frame.h:242
FILE * out
Definition: movenc.c:54
#define av_freep(p)
static int query_formats(AVFilterContext *ctx)
Definition: vf_curves.c:505
internal API functions
static int interpolate(void *log_ctx, uint16_t *y, const struct keypoint *points, int nbits)
Definition: vf_curves.c:214
int depth
Number of bits in the component.
Definition: pixdesc.h:58
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:251
static av_cold void curves_uninit(AVFilterContext *ctx)
Definition: vf_curves.c:658
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:591
#define av_unused
Definition: attributes.h:126
static const AVFilterPad curves_outputs[]
Definition: vf_curves.c:677
simple arithmetic expression evaluator