[FFmpeg-devel] [PATCH] lavfi: add curves filter.
Nicolas George
nicolas.george at normalesup.org
Tue Mar 5 19:27:43 CET 2013
Le quintidi 15 ventôse, an CCXXI, Clement Boesch a écrit :
> TODO: bump minor
> ---
> pseudo-vintage/matrix effect: http://imgur.com/w8sQ4gw
> Obtained with:
> mp=eq2=1:1.2,
> hue=h=20,
> curves=r='0.4/0.2 0.9/1':g='0.75/0.8':b='0/0.1 1/0.9'
> ---
> doc/filters.texi | 64 +++++++++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/vf_curves.c | 345 +++++++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 411 insertions(+)
> create mode 100644 libavfilter/vf_curves.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index f4c1d03..e97e974 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -2187,6 +2187,70 @@ indicates never reset and return the largest area encountered during
> playback.
> @end table
>
> + at section curves
> +
> +Apply color adjustments using curves.
> +
> +This filter is similar to the Adobe Photoshop and GIMP curves tools. Each
> +component (red, green and blue) has its values defined by @var{N} key points
> +tied from each other using a smooth curve.
> +
> +By default, a component curve is defined by the two points @var{(0;0)} and
> + at var{(1;1)}. This creates a straight line where each original pixel value is
> +"adjusted" to its own value, which means no change to the image.
> +
> +The filter allows you to redefine these two points and add some more. A new
> +curve (using a natural cubic spline interpolation) will be define to pass
> +smoothly through all these new coordinates. The new defined points needs to be
> +strictly increasing over the x-axis, and their @var{x} and @var{y} values must
> +be in the @var{[0;1]} interval. If the computed curves happened to go outside
> +the vector spaces, the values will be clipped accordingly.
> +
> +If there is no key point defined in @code{x=0}, the filter will automatically
> +insert a @var{(0;0)} point. In the same way, if there is no key point defined
> +in @code{x=1}, the filter will automatically insert a @var{(1;1)} point.
> +
> +The filter accepts parameters as a list of @var{key}=@var{value} pairs,
> +separated by ":".
> +
> +A description of the accepted parameters follows.
> +
> + at table @option
> + at item r, red
> +Set the key points for the red components.
> + at item g, green
> +Set the key points for the green components.
> + at item b, blue
> +Set the key points for the red components.
> + at end table
> +
> +To avoid some filtergraph syntax conflicts, each key points list need to be
> +defined using the following syntax: @code{x0/y0 x1/y1 x2/y2 ...}.
> +
> + at subsection Examples
> + at itemize
> + at item
> +Increase slightly the middle level of blue:
> + at example
> +curves=b='0.5/0.58'
> + at end example
> +
> + at item
> +Pseudo-vintage effect:
> + at example
> +curves=r='0.4/0.2 0.9/1':g='0.75/0.8':b='0/0.1 1/0.9'
> + at end example
> +Here we obtain the following coordinates for each components:
> + at table @var
> + at item red
> + at code{(0;0) (0.4;0.2) (0.9;1) (1;1)}
> + at item green
> + at code{(0;0) (0.75;0.8) (1;1)}
> + at item blue
> + at code{(0;0.1) (1;0.9)}
> + at end table
> + at end itemize
> +
> @section decimate
>
> Drop frames that do not differ greatly from the previous frame in
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 04ed8b8..d6a1eb4 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -106,6 +106,7 @@ OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o
> OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o
> OBJS-$(CONFIG_CROP_FILTER) += vf_crop.o
> OBJS-$(CONFIG_CROPDETECT_FILTER) += vf_cropdetect.o
> +OBJS-$(CONFIG_CURVES_FILTER) += vf_curves.o
> OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o
> OBJS-$(CONFIG_DELOGO_FILTER) += vf_delogo.o
> OBJS-$(CONFIG_DESHAKE_FILTER) += vf_deshake.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index f05f8ae..14783a7 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -100,6 +100,7 @@ void avfilter_register_all(void)
> REGISTER_FILTER(COPY, copy, vf);
> REGISTER_FILTER(CROP, crop, vf);
> REGISTER_FILTER(CROPDETECT, cropdetect, vf);
> + REGISTER_FILTER(CURVES, curves, vf);
> REGISTER_FILTER(DECIMATE, decimate, vf);
> REGISTER_FILTER(DELOGO, delogo, vf);
> REGISTER_FILTER(DESHAKE, deshake, vf);
> diff --git a/libavfilter/vf_curves.c b/libavfilter/vf_curves.c
> new file mode 100644
> index 0000000..81c0fcb
> --- /dev/null
> +++ b/libavfilter/vf_curves.c
> @@ -0,0 +1,345 @@
> +/*
> + * Copyright (c) 2013 Clément Bœsch
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/eval.h"
> +#include "libavutil/avassert.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +struct keydot {
In the docs, you use the word "point", to my non-native ear it sounds more
correct in this case.
> + double x, y;
> + struct keydot *next;
> +};
> +
> +#define NB_COMP 3
> +
> +typedef struct {
> + const AVClass *class;
> + char *comp_dots_str[NB_COMP];
> + struct keydot *comp_dots[NB_COMP];
The key points are only used during the init: you can make it a local
variable.
> + uint8_t graph[NB_COMP*256];
uint8_t graph[NB_COMP][256]
> +} CurvesContext;
> +
> +#define OFFSET(x) offsetof(CurvesContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +static const AVOption curves_options[] = {
> + { "r", "set red dots coordinates", OFFSET(comp_dots_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { "red", "set red dots coordinates", OFFSET(comp_dots_str[0]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { "g", "set green dots coordinates", OFFSET(comp_dots_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { "green", "set green dots coordinates", OFFSET(comp_dots_str[1]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { "b", "set blue dots coordinates", OFFSET(comp_dots_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { "blue", "set blue dots coordinates", OFFSET(comp_dots_str[2]), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = FLAGS },
> + { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(curves);
> +
> +static struct keydot *new_dot(double x, double y, struct keydot *next)
> +{
> + struct keydot *dot = av_mallocz(sizeof(*dot));
> +
> + if (!dot)
> + return NULL;
> + dot->x = x;
> + dot->y = y;
> + dot->next = next;
> + return dot;
> +}
This is very inefficient, both in speed and in memory, and even more in
needs of error checks. Since there is a hard limit of 256 on the number of
control points, what about a simple array?
double dots[256][2];
unsigned nb_dots = 0;
> +
> +static int parse_dots_str(AVFilterContext *ctx, struct keydot **dots, const char *s)
> +{
> + char *p = (char *)s; // strtod won't alter the string
> + struct keydot *last = NULL;
When building linked lists, keeping a pointer to the next field of the list
tail makes the code a little simpler:
struct keydot **tail = dots;
***
*tail = dot;
tail = &dot->next;
> +
> + /* construct a linked list based on the key dots string */
> + while (p && *p) {
> + struct keydot *dot = new_dot(0, 0, NULL);
> + if (!dot)
> + return AVERROR(ENOMEM);
> + dot->x = av_strtod(p, &p); if (p && *p) p++;
> + dot->y = av_strtod(p, &p); if (p && *p) p++;
You are not checking the delimiter, which may be fine.
> + if (dot->x < 0 || dot->x > 1 || dot->y < 0 || dot->y > 1) {
> + av_log(ctx, AV_LOG_ERROR, "Invalid key dot coordinates (%f;%f), "
> + "x and y must be in the [0;1] range.\n", dot->x, dot->y);
> + return AVERROR(EINVAL);
> + }
> + if (!*dots)
> + *dots = dot;
> + if (last) {
> + if ((int)(last->x * 255) >= (int)(dot->x * 255)) {
If you keep a single variable "double last_x = -1", you can omit the
"if(last)" test.
> + av_log(ctx, AV_LOG_WARNING, "Key dot coordinates (%f;%f) "
> + "and (%f;%f) are too close from each other on the "
> + "x-axis\n", last->x, last->y, dot->x, dot->y);
> + return AVERROR(EINVAL);
> + }
> + last->next = dot;
> + }
> + last = dot;
> + }
> +
> + /* auto insert first key dot if missing at x=0 */
> + if (!*dots) {
> + last = new_dot(0, 0, NULL);
> + if (!last)
> + return AVERROR(ENOMEM);
> + last->x = last->y = 0;
> + *dots = last;
> + } else if ((*dots)->x != 0.) {
> + struct keydot *newfirst = new_dot(0, 0, *dots);
> + if (!newfirst)
> + return AVERROR(ENOMEM);
> + *dots = newfirst;
> + }
> +
> + av_assert0(last);
> +
> + /* auto insert last key dot if missing at x=1 */
> + if (last->x != 1.) {
> + struct keydot *dot = new_dot(1, 1, NULL);
> + if (!dot)
> + return AVERROR(ENOMEM);
> + last->next = dot;
> + }
This part can certainly be simplified, but it depends on the actual data
structure used.
Right now, I would suggest to insert the last point first.
> +
> + return 0;
> +}
> +
> +static int get_nb_dots(const struct keydot *d)
> +{
> + int n = 0;
> + while (d) {
> + n++;
> + d = d->next;
> + }
> + return n;
> +}
> +
> +/**
> + * Natural cubic spline interpolation
> + * Finding curves using Cubic Splines notes by Steven Rauch and John Stockie.
> + * @see http://people.math.sfu.ca/~stockie/teaching/macm316/notes/splines.pdf
> + */
> +static int interpolate(AVFilterContext *ctx, uint8_t *y, const struct keydot *dots)
I will look at the maths later.
<snip>
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> + int i, j, ret;
> + CurvesContext *curves = ctx->priv;
> +
> + curves->class = &curves_class;
> + av_opt_set_defaults(curves);
> +
> + if ((ret = av_set_options_string(curves, args, "=", ":")) < 0)
> + return ret;
> +
> + for (i = 0; i < NB_COMP; i++) {
> + ret = parse_dots_str(ctx, curves->comp_dots + i, curves->comp_dots_str[i]);
> + if (ret < 0)
> + return ret;
> + ret = interpolate(ctx, curves->graph + 256*i, curves->comp_dots[i]);
> + if (ret < 0)
> + return ret;
> + }
> +
> + if (av_log_get_level() >= AV_LOG_VERBOSE) {
> + for (i = 0; i < NB_COMP; i++) {
> + struct keydot *dot = curves->comp_dots[i];
> + av_log(ctx, AV_LOG_VERBOSE, "#%d dots:", i);
> + while (dot) {
> + av_log(ctx, AV_LOG_VERBOSE, " (%f;%f)", dot->x, dot->y);
> + dot = dot->next;
> + }
> + av_log(ctx, AV_LOG_VERBOSE, "\n");
> + av_log(ctx, AV_LOG_VERBOSE, "#%d values:", i);
> + for (j = 0; j < 256; j++)
> + av_log(ctx, AV_LOG_VERBOSE, " %02X", curves->graph[i*256 + j]);
> + av_log(ctx, AV_LOG_VERBOSE, "\n");
> + }
> + }
> +
> + return 0;
> +}
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> + static const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE};
> + ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
> + return 0;
> +}
> +
> +static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
> +{
> + int x, y, i;
> + AVFilterContext *ctx = inlink->dst;
> + CurvesContext *curves = ctx->priv;
> + AVFilterLink *outlink = inlink->dst->outputs[0];
> + uint8_t *dst = in->data[0];
> +
> + for (y = 0; y < inlink->h; y++) {
> + uint8_t *p = dst;
> + for (x = 0; x < inlink->w; x++) {
> + for (i = 0; i < 3; i++)
s/3/NB_COMP/
> + p[i] = curves->graph[p[i] + i*256];
> + p += i;
Why not "p++" in the inner loop?
> + }
> + dst += in->linesize[0];
> + }
> + return ff_filter_frame(outlink, in);
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + int i;
> + CurvesContext *curves = ctx->priv;
> +
> + for (i = 0; i < FF_ARRAY_ELEMS(curves->comp_dots_str); i++) {
> + struct keydot *dot = curves->comp_dots[i];
> + while (dot) {
> + struct keydot *next = dot->next;
> + av_free(dot);
> + dot = next;
> + }
> + }
> +
> + av_opt_free(curves);
> +}
> +
> +static const AVFilterPad curves_inputs[] = {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_VIDEO,
> + .filter_frame = filter_frame,
> + .min_perms = AV_PERM_WRITE,
> + },
> + { NULL }
> +};
> +
> +static const AVFilterPad curves_outputs[] = {
> + {
> + .name = "default",
> + .type = AVMEDIA_TYPE_VIDEO,
> + },
> + { NULL }
> +};
> +
> +AVFilter avfilter_vf_curves = {
> + .name = "curves",
> + .description = NULL_IF_CONFIG_SMALL("Adjust components curves."),
> + .priv_size = sizeof(CurvesContext),
> + .init = init,
> + .uninit = uninit,
> + .query_formats = query_formats,
> + .inputs = curves_inputs,
> + .outputs = curves_outputs,
> + .priv_class = &curves_class,
> +};
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130305/4ea75eb2/attachment.asc>
More information about the ffmpeg-devel
mailing list