LCOV - code coverage report
Current view: top level - libavfilter - vf_lenscorrection.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 85 0.0 %
Date: 2017-12-14 08:27:08 Functions: 0 5 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2007 Richard Spindler (author of frei0r plugin from which this was derived)
       3             :  * Copyright (C) 2014 Daniel Oberhoff
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : /**
      23             :  * @file
      24             :  * Lenscorrection filter, algorithm from the frei0r plugin with the same name
      25             : */
      26             : #include <stdlib.h>
      27             : #include <math.h>
      28             : 
      29             : #include "libavutil/opt.h"
      30             : #include "libavutil/intreadwrite.h"
      31             : #include "libavutil/pixdesc.h"
      32             : 
      33             : #include "avfilter.h"
      34             : #include "internal.h"
      35             : #include "video.h"
      36             : 
      37             : typedef struct LenscorrectionCtx {
      38             :     const AVClass *av_class;
      39             :     unsigned int width;
      40             :     unsigned int height;
      41             :     int hsub, vsub;
      42             :     int nb_planes;
      43             :     double cx, cy, k1, k2;
      44             :     int32_t *correction[4];
      45             : } LenscorrectionCtx;
      46             : 
      47             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      48             : static const AVOption lenscorrection_options[] = {
      49             :     { "cx",     "set relative center x", offsetof(LenscorrectionCtx, cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
      50             :     { "cy",     "set relative center y", offsetof(LenscorrectionCtx, cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
      51             :     { "k1",     "set quadratic distortion factor", offsetof(LenscorrectionCtx, k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
      52             :     { "k2",     "set double quadratic distortion factor", offsetof(LenscorrectionCtx, k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
      53             :     { NULL }
      54             : };
      55             : 
      56             : AVFILTER_DEFINE_CLASS(lenscorrection);
      57             : 
      58             : typedef struct ThreadData {
      59             :     AVFrame *in, *out;
      60             :     int w, h;
      61             :     int plane;
      62             :     int xcenter, ycenter;
      63             :     int32_t *correction;
      64             : } ThreadData;
      65             : 
      66           0 : static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
      67             : {
      68           0 :     ThreadData *td = (ThreadData*)arg;
      69           0 :     AVFrame *in = td->in;
      70           0 :     AVFrame *out = td->out;
      71             : 
      72           0 :     const int w = td->w, h = td->h;
      73           0 :     const int xcenter = td->xcenter;
      74           0 :     const int ycenter = td->ycenter;
      75           0 :     const int start = (h *  job   ) / nb_jobs;
      76           0 :     const int end   = (h * (job+1)) / nb_jobs;
      77           0 :     const int plane = td->plane;
      78           0 :     const int inlinesize = in->linesize[plane];
      79           0 :     const int outlinesize = out->linesize[plane];
      80           0 :     const uint8_t *indata = in->data[plane];
      81           0 :     uint8_t *outrow = out->data[plane] + start * outlinesize;
      82             :     int i;
      83           0 :     for (i = start; i < end; i++, outrow += outlinesize) {
      84           0 :         const int off_y = i - ycenter;
      85           0 :         uint8_t *out = outrow;
      86             :         int j;
      87           0 :         for (j = 0; j < w; j++) {
      88           0 :             const int off_x = j - xcenter;
      89           0 :             const int64_t radius_mult = td->correction[j + i*w];
      90           0 :             const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
      91           0 :             const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
      92           0 :             const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
      93           0 :             *out++ =  isvalid ? indata[y * inlinesize + x] : 0;
      94             :         }
      95             :     }
      96           0 :     return 0;
      97             : }
      98             : 
      99           0 : static int query_formats(AVFilterContext *ctx)
     100             : {
     101             :     static const enum AVPixelFormat pix_fmts[] = {
     102             :         AV_PIX_FMT_YUV410P,
     103             :         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUVJ444P,
     104             :         AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUVJ420P,
     105             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
     106             :         AV_PIX_FMT_YUV422P,
     107             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
     108             :         AV_PIX_FMT_NONE
     109             :     };
     110           0 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     111           0 :     if (!fmts_list)
     112           0 :         return AVERROR(ENOMEM);
     113           0 :     return ff_set_common_formats(ctx, fmts_list);
     114             : }
     115             : 
     116           0 : static av_cold void uninit(AVFilterContext *ctx)
     117             : {
     118           0 :     LenscorrectionCtx *rect = ctx->priv;
     119             :     int i;
     120             : 
     121           0 :     for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
     122           0 :         av_freep(&rect->correction[i]);
     123             :     }
     124           0 : }
     125             : 
     126           0 : static int config_props(AVFilterLink *outlink)
     127             : {
     128           0 :     AVFilterContext *ctx = outlink->src;
     129           0 :     LenscorrectionCtx *rect = ctx->priv;
     130           0 :     AVFilterLink *inlink = ctx->inputs[0];
     131           0 :     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
     132           0 :     rect->hsub = pixdesc->log2_chroma_w;
     133           0 :     rect->vsub = pixdesc->log2_chroma_h;
     134           0 :     outlink->w = rect->width = inlink->w;
     135           0 :     outlink->h = rect->height = inlink->h;
     136           0 :     rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
     137           0 :     return 0;
     138             : }
     139             : 
     140           0 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     141             : {
     142           0 :     AVFilterContext *ctx = inlink->dst;
     143           0 :     AVFilterLink *outlink = ctx->outputs[0];
     144           0 :     LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
     145           0 :     AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     146             :     int plane;
     147             : 
     148           0 :     if (!out) {
     149           0 :         av_frame_free(&in);
     150           0 :         return AVERROR(ENOMEM);
     151             :     }
     152             : 
     153           0 :     av_frame_copy_props(out, in);
     154             : 
     155           0 :     for (plane = 0; plane < rect->nb_planes; ++plane) {
     156           0 :         int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
     157           0 :         int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
     158           0 :         int hdiv = 1 << hsub;
     159           0 :         int vdiv = 1 << vsub;
     160           0 :         int w = rect->width / hdiv;
     161           0 :         int h = rect->height / vdiv;
     162           0 :         int xcenter = rect->cx * w;
     163           0 :         int ycenter = rect->cy * h;
     164           0 :         int k1 = rect->k1 * (1<<24);
     165           0 :         int k2 = rect->k2 * (1<<24);
     166           0 :         ThreadData td = {
     167             :             .in = in,
     168             :             .out  = out,
     169             :             .w  = w,
     170             :             .h  = h,
     171             :             .xcenter = xcenter,
     172             :             .ycenter = ycenter,
     173             :             .plane = plane};
     174             : 
     175           0 :         if (!rect->correction[plane]) {
     176             :             int i,j;
     177           0 :             const int64_t r2inv = (4LL<<60) / (w * w + h * h);
     178             : 
     179           0 :             rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
     180           0 :             if (!rect->correction[plane])
     181           0 :                 return AVERROR(ENOMEM);
     182           0 :             for (j = 0; j < h; j++) {
     183           0 :                 const int off_y = j - ycenter;
     184           0 :                 const int off_y2 = off_y * off_y;
     185           0 :                 for (i = 0; i < w; i++) {
     186           0 :                     const int off_x = i - xcenter;
     187           0 :                     const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
     188           0 :                     const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
     189           0 :                     const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
     190           0 :                     rect->correction[plane][j * w + i] = radius_mult;
     191             :                 }
     192             :             }
     193             :         }
     194             : 
     195           0 :         td.correction = rect->correction[plane];
     196           0 :         ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ff_filter_get_nb_threads(ctx)));
     197             :     }
     198             : 
     199           0 :     av_frame_free(&in);
     200           0 :     return ff_filter_frame(outlink, out);
     201             : }
     202             : 
     203             : static const AVFilterPad lenscorrection_inputs[] = {
     204             :     {
     205             :         .name         = "default",
     206             :         .type         = AVMEDIA_TYPE_VIDEO,
     207             :         .filter_frame = filter_frame,
     208             :     },
     209             :     { NULL }
     210             : };
     211             : 
     212             : static const AVFilterPad lenscorrection_outputs[] = {
     213             :     {
     214             :         .name         = "default",
     215             :         .type         = AVMEDIA_TYPE_VIDEO,
     216             :         .config_props = config_props,
     217             :     },
     218             :     { NULL }
     219             : };
     220             : 
     221             : AVFilter ff_vf_lenscorrection = {
     222             :     .name          = "lenscorrection",
     223             :     .description   = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
     224             :     .priv_size     = sizeof(LenscorrectionCtx),
     225             :     .query_formats = query_formats,
     226             :     .inputs        = lenscorrection_inputs,
     227             :     .outputs       = lenscorrection_outputs,
     228             :     .priv_class    = &lenscorrection_class,
     229             :     .uninit        = uninit,
     230             :     .flags         = AVFILTER_FLAG_SLICE_THREADS,
     231             : };

Generated by: LCOV version 1.13