[FFmpeg-devel] [PATCH] lavfi: port MP noise filter

Paul B Mahol onemda at gmail.com
Thu Feb 14 10:39:10 CET 2013


On 2/13/13, Paul B Mahol <onemda at gmail.com> wrote:
> On 2/12/13, Paul B Mahol <onemda at gmail.com> wrote:
>> On 2/12/13, Stefano Sabatini <stefasab at gmail.com> wrote:
>>> On date Sunday 2013-02-10 22:47:09 +0000, Paul B Mahol encoded:
>>>> Signed-off-by: Paul B Mahol <onemda at gmail.com>
>>>> ---
>>>>  configure                |   1 +
>>>>  doc/filters.texi         |  33 +++++
>>>>  libavfilter/Makefile     |   1 +
>>>>  libavfilter/allfilters.c |   1 +
>>>>  libavfilter/vf_noise.c   | 337
>>>> +++++++++++++++++++++++++++++++++++++++++++++++
>>>>  5 files changed, 373 insertions(+)
>>>>  create mode 100644 libavfilter/vf_noise.c
>>>
>>> General questions:
>>> is it bit-exact with mp=noise?
>>
>> Yes. That it is really irrelevant.
>>
>>>
>>> Any plan to port MMX optims? (This can/should be done in a second
>>> commit).
>>
>> I compared performance and native one was always faster.
>> So either inline assembly is not enabled or it does not really help much.
>>
>>>
>>> Also a FATE test would be nice.
>>>
>>>> diff --git a/configure b/configure
>>>> index 8b6113d..fda0e94 100755
>>>> --- a/configure
>>>> +++ b/configure
>>>> @@ -2012,6 +2012,7 @@ movie_filter_deps="avcodec avformat"
>>>>  mp_filter_deps="gpl avcodec swscale inline_asm"
>>>>  mptestsrc_filter_deps="gpl"
>>>>  negate_filter_deps="lut_filter"
>>>> +noise_filter_deps="gpl"
>>>>  resample_filter_deps="avresample"
>>>>  ocv_filter_deps="libopencv"
>>>>  pan_filter_deps="swresample"
>>>> diff --git a/doc/filters.texi b/doc/filters.texi
>>>> index 4613917..8ca8012 100644
>>>> --- a/doc/filters.texi
>>>> +++ b/doc/filters.texi
>>>> @@ -4684,6 +4684,39 @@ Default value is @code{none}.
>>>>  Swap luma/chroma/alpha fields. Exchange even & odd lines. Default
>>>> value
>>>> is @code{0}.
>>>>  @end table
>>>>
>>>> + at section noise
>>>> +Apply noise on video input frame.
>>>
>>> @section noise
>>>
>>> Add noise to video input frame.
>>
>> fixed
>>
>>>
>>>> +
>>>> +It accepts a list of options in the form of @var{key}=@var{value}
>>>> pairs
>>>
>>> Nit+: This filter accepts ...
>>
>> fixed
>>
>>>
>>>> +separated by ":". A description of the accepted options follows.
>>>
>>>> +
>>>> + at table @option
>>>> + at item c0_strength, c0s
>>>> + at item c1_strength, c1s
>>>> + at item c2_strength, c2s
>>>> + at item c3_strength, c3s
>>>> +Set noise strength for pixel component. Default value is @code{0}.
>>>> +Allowed range is [0, 100].
>>>> +
>>>> + at item c0_flags, c0f
>>>> + at item c1_flags, c1f
>>>> + at item c2_flags, c2f
>>>> + at item c3_flags, c3f
>>>> +Set pixel component flags. Available values for component flags are:
>>>> + at table @samp
>>>> + at item a
>>>> +averaged temporal noise (smoother)
>>>> + at item p
>>>> +mix random noise with a (semi)regular pattern
>>>> + at item q
>>>> +higher quality (slightly better looking, slightly slower)
>>>> + at item t
>>>> +temporal noise (noise pattern changes between frames)
>>>> + at item u
>>>> +uniform noise (gaussian otherwise)
>>>> + at end table
>>>> + at end table
>>>
>>> I think an option to set the same strength/flags to all components
>>> would be handy.
>>
>> added
>>
>>>
>>>> +
>>>>  @section thumbnail
>>>
>>> ^^ order
>>
>> fixed
>>
>>>
>>>>  Select the most representative frame in a given sequence of
>>>> consecutive
>>>> frames.
>>>>
>>>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>>>> index 9158188..906f3a4 100644
>>>> --- a/libavfilter/Makefile
>>>> +++ b/libavfilter/Makefile
>>>> @@ -134,6 +134,7 @@ OBJS-$(CONFIG_LUTYUV_FILTER)                 +=
>>>> vf_lut.o
>>>>  OBJS-$(CONFIG_MP_FILTER)                     += vf_mp.o
>>>>  OBJS-$(CONFIG_NEGATE_FILTER)                 += vf_lut.o
>>>>  OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
>>>> +OBJS-$(CONFIG_NOISE_FILTER)                  += vf_noise.o
>>>>  OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
>>>>  OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
>>>>  OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o
>>>> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
>>>> index ff55428..61f29ff 100644
>>>> --- a/libavfilter/allfilters.c
>>>> +++ b/libavfilter/allfilters.c
>>>> @@ -128,6 +128,7 @@ void avfilter_register_all(void)
>>>>      REGISTER_FILTER(MP,             mp,             vf);
>>>>      REGISTER_FILTER(NEGATE,         negate,         vf);
>>>>      REGISTER_FILTER(NOFORMAT,       noformat,       vf);
>>>> +    REGISTER_FILTER(NOISE,          noise,          vf);
>>>>      REGISTER_FILTER(NULL,           null,           vf);
>>>>      REGISTER_FILTER(OCV,            ocv,            vf);
>>>>      REGISTER_FILTER(OVERLAY,        overlay,        vf);
>>>> diff --git a/libavfilter/vf_noise.c b/libavfilter/vf_noise.c
>>>> new file mode 100644
>>>> index 0000000..73ac173
>>>> --- /dev/null
>>>> +++ b/libavfilter/vf_noise.c
>>>> @@ -0,0 +1,337 @@
>>>> +/*
>>>> + * Copyright (c) 2002 Michael Niedermayer <michaelni at gmx.at>
>>>> + * Copyright (c) 2013 Paul B Mahol
>>>> + *
>>>> + * This file is part of FFmpeg.
>>>> + *
>>>> + * FFmpeg is free software; you can redistribute it and/or
>>>> + * modify it under the terms of the GNU General Public
>>>> + * License as published by the Free Software Foundation; either
>>>> + * version 2 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 General Public License for more details.
>>>> + *
>>>> + * You should have received a copy of the GNU 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
>>>> + */
>>>> +
>>>> +/**
>>>> + * @file
>>>> + * noise generator
>>>> + */
>>>> +
>>>> +#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"
>>>> +
>>>> +#define MAX_NOISE 4096
>>>> +#define MAX_SHIFT 1024
>>>> +#define MAX_RES (MAX_NOISE-MAX_SHIFT)
>>>> +
>>>> +#define NOISE_UNIFORM  1
>>>> +#define NOISE_TEMPORAL 2
>>>> +#define NOISE_QUALITY  4
>>>> +#define NOISE_AVERAGED 8
>>>> +#define NOISE_PATTERN  16
>>>> +
>>>> +typedef struct {
>>>> +    const AVClass *class;
>>>> +    int nb_planes;
>>>> +    int linesize[4];
>>>> +    int height[4];
>>>> +    int strength[4];
>>>> +    int flags[4];
>>>> +    int shiftptr[4];
>>>> +    int8_t *noise[4];
>>>> +    int8_t *prev_shift[4][MAX_RES][3];
>>>> +    int rand_shift[MAX_RES];
>>>> +    int rand_shift_init;
>>>> +} NoiseContext;
>>>> +
>>>> +#define OFFSET(x) offsetof(NoiseContext, x)
>>>> +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
>>>> +
>>>> +#define NOISE_PARAMS(name, x)   \
>>>> +    {#name"_strength", "set component #"#x" strength",
>>>> OFFSET(strength[x]), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
>>>> +    {#name"s",         "set component #"#x" strength",
>>>> OFFSET(strength[x]), AV_OPT_TYPE_INT, {.i64=0}, 0, 100, FLAGS}, \
>>>> +    {#name"_flags", "set component #"#x" flags", OFFSET(flags[x]),
>>>> AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
>>>> +    {#name"f", "set component #"#x" flags", OFFSET(flags[x]),
>>>> AV_OPT_TYPE_FLAGS, {.i64=0}, 0, 31, FLAGS, #name"_flags"}, \
>>>> +    {"u", "uniform noise",  0, AV_OPT_TYPE_CONST,
>>>> {.i64=NOISE_UNIFORM},
>>>> 0, 0, FLAGS, #name"_flags"}, \
>>>> +    {"t", "temporal noise", 0, AV_OPT_TYPE_CONST,
>>>> {.i64=NOISE_TEMPORAL},
>>>> 0, 0, FLAGS, #name"_flags"}, \
>>>> +    {"a", "averaged noise", 0, AV_OPT_TYPE_CONST,
>>>> {.i64=NOISE_AVERAGED},
>>>> 0, 0, FLAGS, #name"_flags"}, \
>>>> +    {"q", "high quality",   0, AV_OPT_TYPE_CONST,
>>>> {.i64=NOISE_QUALITY},
>>>> 0, 0, FLAGS, #name"_flags"}, \
>>>> +    {"p", "(semi)regular pattern", 0, AV_OPT_TYPE_CONST,
>>>> {.i64=NOISE_PATTERN},  0, 0, FLAGS, #name"_flags"},
>>>> +
>>>> +static const AVOption noise_options[] = {
>>>> +    NOISE_PARAMS(c0, 0)
>>>> +    NOISE_PARAMS(c1, 1)
>>>> +    NOISE_PARAMS(c2, 2)
>>>> +    NOISE_PARAMS(c3, 3)
>>>> +    {NULL}
>>>> +};
>>>> +
>>>> +AVFILTER_DEFINE_CLASS(noise);
>>>> +
>>>> +static const int8_t patt[4] = { -1, 0, 1, 0 };
>>>> +
>>>
>>>> +#define RAND_N(range) ((int) ((double) range * rand() / (RAND_MAX +
>>>> 1.0)))
>>>
>>> Note: an option setting the seed may be useful. Also I wonder if we
>>> should favor av_lfg() or a simple LCG (unless this is for
>>> bit-exactness, in this case I'm fine with the current code and we can
>>> change it later).
>>
>> added seed.
>>
>>>
>>> [...]
>>>
>>>> +static int query_formats(AVFilterContext *ctx)
>>>> +{
>>>> +    AVFilterFormats *formats = NULL;
>>>> +    int fmt;
>>>> +
>>>
>>>> +    for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
>>>> +        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
>>>
>>>> +        if (desc->flags & PIX_FMT_PLANAR &&
>>>> !((desc->comp[0].depth_minus1
>>>> + 1) & 7))
>>>> +            ff_add_format(&formats, fmt);
>>>
>>> I'm against the use of PIX_FMT_PLANAR (indeed I'd like to
>>> deprecate/drop it soon or later). Also what's the second condition
>>> good for?
>>
>> It is for non 8/16-bit pixel formats. Feel free to extend it to them
>> also,
>> and also to packed formats.
>>
>>>
>>> [...]
>>>> +AVFilter avfilter_vf_noise = {
>>>> +    .name          = "noise",
>>>
>>>> +    .description   = NULL_IF_CONFIG_SMALL("Noise generator."),
>>>
>>> Generate/Add noise.
>>>
>>> Please let me have a second review when I'm awake.
>
> And?
>

As I see nothing obviously bad, will apply with all local changes.


More information about the ffmpeg-devel mailing list