[FFmpeg-devel] rectification filter

Daniel Oberhoff danieloberhoff at gmail.com
Sun Aug 3 18:43:18 CEST 2014


Am 03.08.2014 um 18:38 schrieb Paul B Mahol <onemda at gmail.com>:

> On Sun, Aug 3, 2014 at 5:39 PM, Daniel Oberhoff <danieloberhoff at gmail.com>
> wrote:
> 
>> 
>> Am 03.08.2014 um 03:15 schrieb Daniel Oberhoff <
>> danieloberhoff at googlemail.com>:
>> 
>>> Am 03.08.2014 um 00:12 schrieb Clément Bœsch <u at pkh.me>:
>>> 
>>>> On Fri, Aug 01, 2014 at 12:56:42PM +0200, Daniel Oberhoff wrote:
>>>>> 
>>>>> Am 01.08.2014 um 12:22 schrieb Clément Bœsch <u at pkh.me>:
>>>>> 
>>>>>> On Fri, Aug 01, 2014 at 12:13:22PM +0200, Daniel Oberhoff wrote:
>>>>>>> 
>>>>>>> Am 29.07.2014 um 09:54 schrieb Carl Eugen Hoyos <cehoyos at ag.or.at>:
>>>>>>> 
>>>>>>>> Daniel Oberhoff <danieloberhoff <at> gmail.com> writes:
>>>>>>>> 
>>>>>>>>> OBJS-$(CONFIG_ZOOMPAN_FILTER)                += vf_zoompan.o
>>>>>>>>> +OBJS-$(CONFIG_RECTIFICATION_FILTER)          += vf_rectification.o
>>>>>>>> 
>>>>>>>>>  REGISTER_FILTER(ZOOMPAN,        zoompan,        vf);
>>>>>>>>> +    REGISTER_FILTER(RECTIFICATION,  rectification,  vf);
>>>>>>>> 
>>>>>>>> Keep the alphabetic ordering please.
>>>>>>>> 
>>>>>>> 
>>>>>>> Ok
>>>>>>> 
>>>>>>>>> +        AV_PIX_FMT_YUV410P,
>>>>>>>>> +        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
>>>>>>>>> +        AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
>>>>>>>>> +        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
>>>>>>>>> +        AV_PIX_FMT_NONE
>>>>>>>> 
>>>>>>>> I have no idea what this filter does and since the
>>>>>>>> most important format (yuv420p) is supported, this
>>>>>>>> doesn't really matter but yuv410p and yuva444p are
>>>>>>>> quite exotic formats, I would at least have
>>>>>>>> expected yuv422p, yuva420p and yuv444p in the list.
>>>>>>>> 
>>>>>>>> Did you test all above formats (at least the left row)?
>>>>>>> 
>>>>>>> Added the one from your list that was missing. Can you suggest an
>> easy way of testing all these formats?
>>>>>>> 
>>>>>> 
>>>>>> 
>>>>>> add a format filter before yours in the filtergraph (-vf
>>>>>> format=yuv410p,rectification …)
>>>>> 
>>>>> Ok, borders can be a bitt different, but otherwise all work now,
>> except vuy444p, which ffmpeg reports as invalid. btw, how can I get the
>> components of a given color in the colorspace?
>>>>> 
>>>>>> If you want to add a FATE test (which would be really awesome), you
>> can
>>>>>> grep for "video_filter" in tests/fate/filter-video.mak (to test all
>> pixel
>>>>>> formats the filter supports), or just make a standard test with
>> framecrc.
>>>>>> 
>>>>>> [...]
>>>>> 
>>>>> I did that by adding this:
>>>>> 
>>>>> FATE_FILTER_PIXFMTS-$(CONFIG_RECTIFICATION_FILTER) +=
>> fate-filter-pixfmts-rectification
>>>>> fate-filter-pixfmts-rectification: CMD = pixfmts "0.6:0.4:0.65:0.4"
>>>>> 
>>>>> then I ran
>>>>> 
>>>>> make fate-filter-pixfmts-rectification GEN=1
>>>>> 
>>>>> which made a file
>>>>> 
>>>>> test/data/fate/filter-pixfmts-rectification.rep
>>>>> 
>>>> 
>>>> This is the result of a normal run to be compared by with the reference
>>>> which should have also been generated with the help of GEN=1 for your
>>>> first run. The file(s) generated by GEN=1 need to be tracked.
>>>> 
>>>>> do I check this in or not?
>>>>> 
>>>> 
>>>> Of course you are supposed to check if the results make sense.
>>>> 
>>>> Use make fate-filter-pixfmts-rectifications V=1 to see what's going on.
>>>> 
>>>>> running
>>>>> 
>>>>> make fate-filter-pixfmts-rectification GEN=1
>>>>> 
>>>>> runs fine…
>>>>> 
>>>> 
>>>> Of course, it doesn't do the comparison, it generates the reference.
>>>> 
>>>>> Best
>>>>> 
>>>>> Daniel
>>>> 
>>>> --
>>>> Clément B.
>>>> _______________________________________________
>>>> ffmpeg-devel mailing list
>>>> ffmpeg-devel at ffmpeg.org
>>>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>>> 
>>> Alright, will get that cleaned up. Since I seem to have all consent I
>> need to also change the license I will see that I get a new patch ready
>> soon.
>>> 
>>> Best and thanks for feedback!
>>> 
>>> Daniel
>> 
>> Hello all,
>> 
>> I updated the patch trying to incorporate all review feedback. I also got
>> consent from the original author to put this filter under LGPL, and thus
>> have it compiled in by default. I also conversed with Cyrille from Krita
>> and he doesn’t see any more copyright issues, as at that point it boils
>> down to the use of a well known algorithm, in a straight-forward
>> implementation.
>> 
>> Compared to the previous patch I renamed the filter to lenscorrection,
>> since that is the name in the frei0r suite, and it will make using this
>> instead of the frei0r one as easy as stating
>> 
>> lenscorrection=...
>> 
>> instead of
>> 
>> frei0r=lenscorrection:...
>> 
>> Last but not least I adapted the format list, inspected results on all
>> formats, and supplied a fate test. Release notes and docs where also
>> adapted. Please check if this is ok to push now.
>> 
>> From dc552ae06a41725988250896327af2cceee1b812 Mon Sep 17 00:00:00 2001
>> From: Daniel Oberhoff <daniel at danieloberhoff.de>
>> Date: Mon, 28 Jul 2014 23:58:12 +0200
>> Subject: [PATCH] ported lenscorrection filter from frei0r
>> 
>> ---
>> Changelog                                    |   2 +-
>> doc/filters.texi                             |  36 +++++
>> libavfilter/Makefile                         |   1 +
>> libavfilter/allfilters.c                     |   1 +
>> libavfilter/version.h                        |   4 +-
>> libavfilter/vf_lenscorrection.c              | 208
>> +++++++++++++++++++++++++++
>> tests/fate/filter-video.mak                  |   3 +
>> tests/ref/fate/filter-pixfmts-lenscorrection |   8 ++
>> 8 files changed, 260 insertions(+), 3 deletions(-)
>> create mode 100644 libavfilter/vf_lenscorrection.c
>> create mode 100644 tests/ref/fate/filter-pixfmts-lenscorrection
>> 
>> 
>> 
>> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
>> index 0f54381..f74defa 100644
>> --- a/libavfilter/Makefile
>> +++ b/libavfilter/Makefile
>> @@ -162,6 +162,7 @@ OBJS-$(CONFIG_PIXDESCTEST_FILTER)            +=
>> vf_pixdesctest.o
>> OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
>> OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o
>> framesync.o
>> OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
>> +OBJS-$(CONFIG_LENSCORRECTION_FILTER)         += vf_lenscorrection.o
>> 
> 
> not in alphabetical order
> 
> E, {.dbl=0.5}, 0, 1, .flags=FLAGS },
>> +    { "k1",     "set quadratic distortion factor",
>> offsetof(LenscorrectionCtx, k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1,
>> .flags=FLAGS },
>> +    { "k2",     "set double quadratic distortion factor",
>> offsetof(LenscorrectionCtx, k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1,
>> .flags=FLAGS },
>> +    { NULL }
>> +};
>> +
>> +AVFILTER_DEFINE_CLASS(Lenscorrection);
>> +
>> +static av_cold int init(AVFilterContext *ctx)
>> +{
>> +    return 0;
>> +}
>> 
> 
> please remove this one, there is no point in having code that does nothing.
> 
>> +
>> +static av_cold void uninit(AVFilterContext *ctx)
>> +{
>> +}
>> +
>> 
> 
> ditto
> 

Done thanks!

> 
> 
>> +typedef struct ThreadData {
>> +    AVFrame *in, *out;
>> +    float w, h;
>> +    int plane;
>> +    float xcenter, ycenter;
>> +    float k1, k2;
>> +} ThreadData;
>> +
>> +static int filter_slice(AVFilterContext *ctx, void *arg, int job, int
>> nb_jobs)
>> +{
>> +    ThreadData *td = (ThreadData*)arg;
>> +    AVFrame *in = td->in;
>> +    AVFrame *out = td->out;
>> +
>> +    const float w = td->w, h = td->h;
>> +    const float xcenter = td->xcenter;
>> +    const float ycenter = td->ycenter;
>> +    const float r2inv = 4.0 / (w * w + h * h);
>> +    const float k1 = td->k1 - 0.5;
>> +    const float k2 = td->k2 - 0.5;
>> +    const int start = (h *  job   ) / nb_jobs;
>> +    const int end   = (h * (job+1)) / nb_jobs;
>> +    const int plane = td->plane;
>> +    const int inlinesize = in->linesize[plane];
>> +    const int outlinesize = out->linesize[plane];
>> +    const uint8_t *indata = in->data[plane];
>> +    uint8_t *outrow = out->data[plane] + start * outlinesize;
>> +    int i;
>> +    for (i = start; i < end; i++, outrow += outlinesize) {
>> +        const float off_y = i - ycenter;
>> +        const float off_y2 = off_y * off_y;
>> +        uint8_t *out = outrow;
>> +        int j;
>> +        for (j = 0; j < w; j++) {
>> +            const float off_x = j - xcenter;
>> +            const float r2 = (off_x * off_x + off_y2) * r2inv;
>> +            const float radius_mult = 1.0f + r2 * k1 + r2 * r2 * k2;
>> +            const int x = xcenter + radius_mult * off_x + 0.5f;
>> +            const int y = ycenter + radius_mult * off_y + 0.5f;
>> +            const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
>> +            *out++ =  isvalid ? indata[y * inlinesize + x] : 0;
>> +        }
>> +    }
>> +    return 0;
>> +}
>> +
>> +static int query_formats(AVFilterContext *ctx)
>> +{
>> +    static enum PixelFormat pix_fmts[] = {
>> +        AV_PIX_FMT_YUV410P,
>> +        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
>> +        AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
>> +        AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
>> +        AV_PIX_FMT_YUV422P,
>> +        AV_PIX_FMT_NONE
>> +    };
>> +
>> +    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
>> +    return 0;
>> +}
>> +
>> +static int config_props(AVFilterLink *outlink)
>> +{
>> +    AVFilterContext*  ctx = outlink->src;
>> +    LenscorrectionCtx* rect = ctx->priv;
>> +    AVFilterLink *inlink = ctx->inputs[0];
>> +    const AVPixFmtDescriptor *pixdesc =
>> av_pix_fmt_desc_get(inlink->format);
>> +    rect->hsub = pixdesc->log2_chroma_w;
>> +    rect->vsub = pixdesc->log2_chroma_h;
>> +    outlink->w = rect->width = inlink->w;
>> +    outlink->h = rect->height = inlink->h;
>> +    rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
>> +    return 0;
>> +}
>> +
>> +static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>> +{
>> +    AVFilterContext *ctx = inlink->dst;
>> +    AVFilterLink *outlink = ctx->outputs[0];
>> +    LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
>> +    AVFrame* out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
>> +    int plane;
>> +
>> +    if (!out) {
>> +        av_frame_free(&in);
>> +        return AVERROR(ENOMEM);
>> +    }
>> +
>> +    av_frame_copy_props(out, in);
>> +
>> +    for (plane = 0; plane < rect->nb_planes; ++plane) {
>> +        int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
>> +        int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
>> +        float hdiv = 1 << hsub;
>> +        float vdiv = 1 << vsub;
>> +        float w = rect->width / hdiv;
>> +        float h = rect->height / vdiv;
>> +        ThreadData td = { .in = in,   .out  = out,
>> +                          .w  = w,
>> +                          .h  = h,
>> +                          .xcenter = rect->cx * w,
>> +                          .ycenter = rect->cy * h,
>> +                          .k1 = rect->k1,
>> +                          .k2 = rect->k2,
>> +                          .plane = plane};
>> +        ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h,
>> ctx->graph->nb_threads));
>> +    }
>> +
>> +    av_frame_free(&in);
>> +    return ff_filter_frame(outlink, out);
>> +}
>> +
>> +static const AVFilterPad Lenscorrection_inputs[] = {
>> +    {
>> +        .name         = "default",
>> +        .type         = AVMEDIA_TYPE_VIDEO,
>> +        .filter_frame = filter_frame,
>> +    },
>> +    { NULL }
>> +};
>> +
>> +static const AVFilterPad Lenscorrection_outputs[] = {
>> +    {
>> +        .name         = "default",
>> +        .type         = AVMEDIA_TYPE_VIDEO,
>> +        .config_props = config_props,
>> +    },
>> +    { NULL }
>> +};
>> +
>> +AVFilter ff_vf_lenscorrection = {
>> +    .name          = "lenscorrection",
>> +    .description   = NULL_IF_CONFIG_SMALL("Rectify the image by
>> correcting for lens distortion."),
>> +    .priv_size     = sizeof(LenscorrectionCtx),
>> +    .init          = init,
>> +    .uninit        = uninit,
>> 
> 
> Please remove those 2 lines.
> 
> 
>> +    .query_formats = query_formats,
>> +    .inputs        = Lenscorrection_inputs,
>> +    .outputs       = Lenscorrection_outputs,
>> +    .priv_class    = &Lenscorrection_class,
>> +    .flags         = AVFILTER_FLAG_SLICE_THREADS,
>> +};
>> +
>> \ No newline at end of file
>> diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
>> index d87129b..cc5a71b 100644
>> --- a/tests/fate/filter-video.mak
>> +++ b/tests/fate/filter-video.mak
>> @@ -331,6 +331,9 @@ fate-filter-pixfmts-il:    CMD = pixfmts
>> "luma_mode=d:chroma_mode=d:alpha_mode=d
>> FATE_FILTER_PIXFMTS-$(CONFIG_KERNDEINT_FILTER) +=
>> fate-filter-pixfmts-kerndeint
>> fate-filter-pixfmts-kerndeint: CMD = pixfmts ""
>> "tinterlace=interleave_top,"
>> 
>> +FATE_FILTER_PIXFMTS-$(CONFIG_LENSCORRECTION_FILTER) +=
>> fate-filter-pixfmts-lenscorrection
>> +fate-filter-pixfmts-lenscorrection: CMD = pixfmts "0.6:0.4:0.65:0.4"
>> +
>> FATE_FILTER_PIXFMTS-$(CONFIG_LUT_FILTER) += fate-filter-pixfmts-lut
>> fate-filter-pixfmts-lut: CMD = pixfmts
>> "c0=2*val:c1=2*val:c2=val/2:c3=negval+40"
>> 
>> diff --git a/tests/ref/fate/filter-pixfmts-lenscorrection
>> b/tests/ref/fate/filter-pixfmts-lenscorrection
>> new file mode 100644
>> index 0000000..18af6fb
>> --- /dev/null
>> +++ b/tests/ref/fate/filter-pixfmts-lenscorrection
>> @@ -0,0 +1,8 @@
>> +yuv410p             e7d59dbdb1afab7e2a8f770d563e28c4
>> +yuv420p             bc45b4762d5271410ff825317c85af64
>> +yuv422p             5cce0c299322634d65e6b32c976e2c12
>> +yuv444p             03de9a93ab3045a523b234ea93f21c91
>> +yuva420p            d1fa6735c4e7fbbf3a501cec1f0b4ac1
>> +yuva444p            713ddf5861d3df11c70a242a13c5e92e
>> +yuvj420p            1d5cccaf4ef568ae9fa36f9a28e71c34
>> +yuvj444p            aef1db29848e3b1dcaf4309255c38cbd
>> --
>> 1.8.2
>> 
>> _______________________________________________
>> ffmpeg-devel mailing list
>> ffmpeg-devel at ffmpeg.org
>> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel



More information about the ffmpeg-devel mailing list