[FFmpeg-devel] [PATCH] avfilter: add filmgrain filter

James Almer jamrial at gmail.com
Thu Aug 25 23:19:14 EEST 2016


On 8/25/2016 4:44 PM, Paul B Mahol wrote:
> Hi,
> 
> patch attached.
> 
> 
> 0001-avfilter-add-filmgrain-filter.patch
> 
> 
> From baf7613d08f9de31ff196dfa399b546e466478e8 Mon Sep 17 00:00:00 2001
> From: Paul B Mahol <onemda at gmail.com>
> Date: Thu, 25 Aug 2016 20:53:35 +0200
> Subject: [PATCH] avfilter: add filmgrain filter
> 
> ---
>  libavfilter/Makefile       |   1 +
>  libavfilter/allfilters.c   |   1 +
>  libavfilter/vf_filmgrain.c | 905 +++++++++++++++++++++++++++++++++++++++++++++

Don't forget about doc/filters.texi, Changelog entry and version bump.

>  3 files changed, 907 insertions(+)
>  create mode 100644 libavfilter/vf_filmgrain.c
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 4ec7d8a..31183d8 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -174,6 +174,7 @@ OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
>  OBJS-$(CONFIG_FIELDHINT_FILTER)              += vf_fieldhint.o
>  OBJS-$(CONFIG_FIELDMATCH_FILTER)             += vf_fieldmatch.o
>  OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
> +OBJS-$(CONFIG_FILMGRAIN_FILTER)              += vf_filmgrain.o
>  OBJS-$(CONFIG_FIND_RECT_FILTER)              += vf_find_rect.o lavfutils.o
>  OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
>  OBJS-$(CONFIG_FPS_FILTER)                    += vf_fps.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 1ca2679..16fbc4a 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -191,6 +191,7 @@ void avfilter_register_all(void)
>      REGISTER_FILTER(FIELDHINT,      fieldhint,      vf);
>      REGISTER_FILTER(FIELDMATCH,     fieldmatch,     vf);
>      REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
> +    REGISTER_FILTER(FILMGRAIN,      filmgrain,      vf);
>      REGISTER_FILTER(FIND_RECT,      find_rect,      vf);
>      REGISTER_FILTER(FORMAT,         format,         vf);
>      REGISTER_FILTER(FPS,            fps,            vf);
> diff --git a/libavfilter/vf_filmgrain.c b/libavfilter/vf_filmgrain.c
> new file mode 100644
> index 0000000..84a1fa7
> --- /dev/null
> +++ b/libavfilter/vf_filmgrain.c
> @@ -0,0 +1,905 @@
> +/*
> + * 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 <float.h>
> +
> +#include "libavutil/opt.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/parseutils.h"
> +#include "libavutil/pixdesc.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "internal.h"
> +#include "video.h"
> +
> +struct osn_context {
> +    int16_t *perm;
> +    int16_t *permGradIndex3D;
> +};
> +
> +typedef struct FilmGrainContext {
> +    const AVClass *class;
> +
> +    int nb_planes;
> +    int bytewidth[4];
> +    int height[4];
> +
> +    double size;
> +    double speed;
> +    double strength;
> +    int planes;
> +    int64_t seed[4];
> +
> +    struct osn_context *osn[4];
> +} FilmGrainContext;
> +
> +#define OFFSET(x) offsetof(FilmGrainContext, x)
> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption filmgrain_options[] = {
> +    { "size",     "set pattern size",   OFFSET(size),     AV_OPT_TYPE_DOUBLE, {.dbl=.4},  0.1, 1,  FLAGS },
> +    { "speed",    "set change speed",   OFFSET(speed),    AV_OPT_TYPE_DOUBLE, {.dbl= 1},  0.1, 10, FLAGS },
> +    { "strength", "set noise strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=.02}, 0,   1,  FLAGS },
> +    { "planes",   "set planes",         OFFSET(planes),   AV_OPT_TYPE_INT,    {.i64=1},   0, 0xF,  FLAGS },
> +    { "seed0",    "set noise seed #0",  OFFSET(seed[0]),  AV_OPT_TYPE_INT64,  {.i64=42},  INT64_MIN, INT64_MAX,  FLAGS },
> +    { "seed1",    "set noise seed #1",  OFFSET(seed[1]),  AV_OPT_TYPE_INT64,  {.i64=24},  INT64_MIN, INT64_MAX,  FLAGS },
> +    { "seed2",    "set noise seed #2",  OFFSET(seed[2]),  AV_OPT_TYPE_INT64,  {.i64=17},  INT64_MIN, INT64_MAX,  FLAGS },
> +    { "seed3",    "set noise seed #3",  OFFSET(seed[3]),  AV_OPT_TYPE_INT64,  {.i64=11},  INT64_MIN, INT64_MAX,  FLAGS },

Why not use av_get_random_seed when not specified by the user instead?

> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(filmgrain);
> +
> +static int query_formats(AVFilterContext *ctx)
> +{
> +    static const enum AVPixelFormat pixel_fmts[] = {
> +        AV_PIX_FMT_GRAY8,
> +        AV_PIX_FMT_GRAY16,
> +        AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
> +        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
> +        AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
> +        AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
> +        AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
> +        AV_PIX_FMT_YUVJ411P,
> +        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
> +        AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
> +        AV_PIX_FMT_YUV440P10,
> +        AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
> +        AV_PIX_FMT_YUV440P12,
> +        AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
> +        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
> +        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
> +        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
> +        AV_PIX_FMT_NONE
> +    };
> +
> +    AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
> +    if (!formats)
> +        return AVERROR(ENOMEM);
> +    return ff_set_common_formats(ctx, formats);
> +}
> +
> +static int config_input(AVFilterLink *inlink)
> +{
> +    FilmGrainContext *n = inlink->dst->priv;
> +    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
> +    int ret;
> +
> +    n->nb_planes = av_pix_fmt_count_planes(inlink->format);
> +
> +    if ((ret = av_image_fill_linesizes(n->bytewidth, inlink->format, inlink->w)) < 0)
> +        return ret;
> +
> +    n->height[1] = n->height[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
> +    n->height[0] = n->height[3] = inlink->h;
> +
> +    return 0;
> +}
> +
> +#define NORM_CONSTANT_3D (103.0)
> +
> +/*
> + * Gradients for 3D. They approximate the directions to the
> + * vertices of a rhombicuboctahedron from the center, skewed so
> + * that the triangular and square facets can be inscribed inside
> + * circles of the same radius.
> + */
> +static const int8_t gradients3D[] = {
> +    -11,  4,  4,     -4,  11,  4,    -4,  4,  11,
> +     11,  4,  4,      4,  11,  4,     4,  4,  11,
> +    -11, -4,  4,     -4, -11,  4,    -4, -4,  11,
> +     11, -4,  4,      4, -11,  4,     4, -4,  11,
> +    -11,  4, -4,     -4,  11, -4,    -4,  4, -11,
> +     11,  4, -4,      4,  11, -4,     4,  4, -11,
> +    -11, -4, -4,     -4, -11, -4,    -4, -4, -11,
> +     11, -4, -4,      4, -11, -4,     4, -4, -11,
> +};
> +
> +static double extrapolate3(struct osn_context *ctx, int xsb, int ysb, int zsb, double dx, double dy, double dz)
> +{
> +    int16_t *perm = ctx->perm;
> +    int16_t *permGradIndex3D = ctx->permGradIndex3D;
> +    int index = permGradIndex3D[(perm[(perm[xsb & 0xFF] + ysb) & 0xFF] + zsb) & 0xFF];
> +
> +    return gradients3D[index] * dx
> +        + gradients3D[index + 1] * dy
> +        + gradients3D[index + 2] * dz;
> +}
> +
> +static inline int fastFloor(double x)
> +{
> +    int xi = (int) x;
> +    return x < xi ? xi - 1 : xi;
> +}
> +
> +static int allocate_perm(struct osn_context *ctx, int nperm, int ngrad)
> +{
> +    if (ctx->perm)
> +        free(ctx->perm);

av_free.

> +    if (ctx->permGradIndex3D)
> +        free(ctx->permGradIndex3D);
> +    ctx->perm = (int16_t *) malloc(sizeof(*ctx->perm) * nperm);

av_malloc or av_malloc_array.

There are tons of other cases for both functions below.

> +    if (!ctx->perm)
> +        return -ENOMEM;
> +    ctx->permGradIndex3D = (int16_t *) malloc(sizeof(*ctx->permGradIndex3D) * ngrad);
> +    if (!ctx->permGradIndex3D) {
> +        free(ctx->perm);
> +        return -ENOMEM;
> +    }
> +    return 0;
> +}
> +
> +#define ARRAYSIZE(x) (sizeof((x)) / sizeof((x)[0]))

FF_ARRAY_ELEMS



More information about the ffmpeg-devel mailing list