LCOV - code coverage report
Current view: top level - libavfilter - vf_phase.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 91 135 67.4 %
Date: 2017-12-16 13:57:32 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2004 Ville Saari
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or
       7             :  * modify it under the terms of the GNU General Public
       8             :  * License as published by the Free Software Foundation; either
       9             :  * version 2 of the License, or (at your option) any later version.
      10             :  *
      11             :  * FFmpeg is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License along
      17             :  * with FFmpeg; if not, write to the Free Software
      18             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      19             :  */
      20             : 
      21             : #include "libavutil/avassert.h"
      22             : #include "libavutil/imgutils.h"
      23             : #include "libavutil/pixdesc.h"
      24             : #include "libavutil/opt.h"
      25             : #include "avfilter.h"
      26             : #include "formats.h"
      27             : #include "internal.h"
      28             : #include "video.h"
      29             : 
      30             : enum PhaseMode {
      31             :     PROGRESSIVE,
      32             :     TOP_FIRST,
      33             :     BOTTOM_FIRST,
      34             :     TOP_FIRST_ANALYZE,
      35             :     BOTTOM_FIRST_ANALYZE,
      36             :     ANALYZE,
      37             :     FULL_ANALYZE,
      38             :     AUTO,
      39             :     AUTO_ANALYZE
      40             : };
      41             : 
      42             : typedef struct PhaseContext {
      43             :     const AVClass *class;
      44             :     int mode;                   ///<PhaseMode
      45             :     AVFrame *frame; /* previous frame */
      46             :     int nb_planes;
      47             :     int planeheight[4];
      48             :     int linesize[4];
      49             : } PhaseContext;
      50             : 
      51             : #define OFFSET(x) offsetof(PhaseContext, x)
      52             : #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
      53             : #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
      54             : 
      55             : static const AVOption phase_options[] = {
      56             :     { "mode", "set phase mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=AUTO_ANALYZE}, PROGRESSIVE, AUTO_ANALYZE, FLAGS, "mode" },
      57             :     CONST("p", "progressive",          PROGRESSIVE,          "mode"),
      58             :     CONST("t", "top first",            TOP_FIRST,            "mode"),
      59             :     CONST("b", "bottom first",         BOTTOM_FIRST,         "mode"),
      60             :     CONST("T", "top first analyze",    TOP_FIRST_ANALYZE,    "mode"),
      61             :     CONST("B", "bottom first analyze", BOTTOM_FIRST_ANALYZE, "mode"),
      62             :     CONST("u", "analyze",              ANALYZE,              "mode"),
      63             :     CONST("U", "full analyze",         FULL_ANALYZE,         "mode"),
      64             :     CONST("a", "auto",                 AUTO,                 "mode"),
      65             :     CONST("A", "auto analyze",         AUTO_ANALYZE,         "mode"),
      66             :     { NULL }
      67             : };
      68             : 
      69             : AVFILTER_DEFINE_CLASS(phase);
      70             : 
      71           1 : static int query_formats(AVFilterContext *ctx)
      72             : {
      73             :     static const enum AVPixelFormat pix_fmts[] = {
      74             :         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P,
      75             :         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ422P,AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
      76             :         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
      77             :         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
      78             :     };
      79             : 
      80           1 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
      81           1 :     if (!fmts_list)
      82           0 :         return AVERROR(ENOMEM);
      83           1 :     return ff_set_common_formats(ctx, fmts_list);
      84             : }
      85             : 
      86           1 : static int config_input(AVFilterLink *inlink)
      87             : {
      88           1 :     PhaseContext *s = inlink->dst->priv;
      89           1 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
      90             :     int ret;
      91             : 
      92           1 :     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
      93           0 :         return ret;
      94             : 
      95           1 :     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
      96           1 :     s->planeheight[0] = s->planeheight[3] = inlink->h;
      97             : 
      98           1 :     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
      99             : 
     100           1 :     return 0;
     101             : }
     102             : 
     103             : /*
     104             :  * This macro interpolates the value of both fields at a point halfway
     105             :  * between lines and takes the squared difference. In field resolution
     106             :  * the point is a quarter pixel below a line in one field and a quarter
     107             :  * pixel above a line in other.
     108             :  *
     109             :  * (The result is actually multiplied by 25)
     110             :  */
     111             : #define DIFF(a, as, b, bs) ((t) = ((*(a) - (b)[bs]) << 2) + (a)[(as) << 1] - (b)[-(bs)], (t) * (t))
     112             : 
     113             : /*
     114             :  * Find which field combination has the smallest average squared difference
     115             :  * between the fields.
     116             :  */
     117          49 : static enum PhaseMode analyze_plane(void *ctx, enum PhaseMode mode, AVFrame *old, AVFrame *new)
     118             : {
     119             :     double bdiff, tdiff, pdiff;
     120             : 
     121          49 :     if (mode == AUTO) {
     122           0 :         mode = new->interlaced_frame ? new->top_field_first ?
     123           0 :                TOP_FIRST : BOTTOM_FIRST : PROGRESSIVE;
     124          49 :     } else if (mode == AUTO_ANALYZE) {
     125          98 :         mode = new->interlaced_frame ? new->top_field_first ?
     126          49 :                TOP_FIRST_ANALYZE : BOTTOM_FIRST_ANALYZE : FULL_ANALYZE;
     127             :     }
     128             : 
     129          49 :     if (mode <= BOTTOM_FIRST) {
     130           0 :         bdiff = pdiff = tdiff = 65536.0;
     131             :     } else {
     132          49 :         const int ns = new->linesize[0];
     133          49 :         const int os = old->linesize[0];
     134          49 :         const uint8_t *nptr = new->data[0];
     135          49 :         const uint8_t *optr = old->data[0];
     136          49 :         const int h = new->height;
     137          49 :         const int w = new->width;
     138             :         int bdif, tdif, pdif;
     139             :         double scale;
     140             : 
     141          49 :         int top = 0, t;
     142          49 :         const uint8_t *rend, *end = nptr + (h - 2) * ns;
     143             : 
     144          49 :         bdiff = pdiff = tdiff = 0.0;
     145             : 
     146          49 :         nptr += ns;
     147          49 :         optr += os;
     148       14063 :         while (nptr < end) {
     149       13965 :             pdif = tdif = bdif = 0;
     150             : 
     151       13965 :             switch (mode) {
     152           0 :             case TOP_FIRST_ANALYZE:
     153           0 :                 if (top) {
     154           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     155           0 :                         pdif += DIFF(nptr, ns, nptr, ns);
     156           0 :                         tdif += DIFF(nptr, ns, optr, os);
     157             :                     }
     158             :                 } else {
     159           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     160           0 :                         pdif += DIFF(nptr, ns, nptr, ns);
     161           0 :                         tdif += DIFF(optr, os, nptr, ns);
     162             :                     }
     163             :                 }
     164           0 :                 break;
     165           0 :             case BOTTOM_FIRST_ANALYZE:
     166           0 :                 if (top) {
     167           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     168           0 :                         pdif += DIFF(nptr, ns, nptr, ns);
     169           0 :                         bdif += DIFF(optr, os, nptr, ns);
     170             :                     }
     171             :                 } else {
     172           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     173           0 :                         pdif += DIFF(nptr, ns, nptr, ns);
     174           0 :                         bdif += DIFF(nptr, ns, optr, os);
     175             :                     }
     176             :                 }
     177           0 :                 break;
     178           0 :             case ANALYZE:
     179           0 :                 if (top) {
     180           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     181           0 :                         tdif += DIFF(nptr, ns, optr, os);
     182           0 :                         bdif += DIFF(optr, os, nptr, ns);
     183             :                     }
     184             :                 } else {
     185           0 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     186           0 :                         bdif += DIFF(nptr, ns, optr, os);
     187           0 :                         tdif += DIFF(optr, os, nptr, ns);
     188             :                     }
     189             :                 }
     190           0 :                 break;
     191       13965 :             case FULL_ANALYZE:
     192       13965 :                 if (top) {
     193     2456174 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     194     2449216 :                         pdif += DIFF(nptr, ns, nptr, ns);
     195     2449216 :                         tdif += DIFF(nptr, ns, optr, os);
     196     2449216 :                         bdif += DIFF(optr, os, nptr, ns);
     197             :                     }
     198             :                 } else {
     199     2473471 :                     for (rend = nptr + w; nptr < rend; nptr++, optr++) {
     200     2466464 :                         pdif += DIFF(nptr, ns, nptr, ns);
     201     2466464 :                         bdif += DIFF(nptr, ns, optr, os);
     202     2466464 :                         tdif += DIFF(optr, os, nptr, ns);
     203             :                     }
     204             :                 }
     205       13965 :                 break;
     206           0 :             default:
     207           0 :                 av_assert0(0);
     208             :             }
     209             : 
     210       13965 :             pdiff += (double)pdif;
     211       13965 :             tdiff += (double)tdif;
     212       13965 :             bdiff += (double)bdif;
     213       13965 :             nptr += ns - w;
     214       13965 :             optr += os - w;
     215       13965 :             top ^= 1;
     216             :         }
     217             : 
     218          49 :         scale = 1.0 / (w * (h - 3)) / 25.0;
     219          49 :         pdiff *= scale;
     220          49 :         tdiff *= scale;
     221          49 :         bdiff *= scale;
     222             : 
     223          49 :         if (mode == TOP_FIRST_ANALYZE) {
     224           0 :             bdiff = 65536.0;
     225          49 :         } else if (mode == BOTTOM_FIRST_ANALYZE) {
     226           0 :             tdiff = 65536.0;
     227          49 :         } else if (mode == ANALYZE) {
     228           0 :             pdiff = 65536.0;
     229             :         }
     230             : 
     231          49 :         if (bdiff < pdiff && bdiff < tdiff) {
     232           0 :             mode = BOTTOM_FIRST;
     233          49 :         } else if (tdiff < pdiff && tdiff < bdiff) {
     234           0 :             mode = TOP_FIRST;
     235             :         } else {
     236          49 :             mode = PROGRESSIVE;
     237             :         }
     238             :     }
     239             : 
     240          98 :     av_log(ctx, AV_LOG_DEBUG, "mode=%c tdiff=%f bdiff=%f pdiff=%f\n",
     241          49 :            mode == BOTTOM_FIRST ? 'b' : mode == TOP_FIRST ? 't' : 'p',
     242             :            tdiff, bdiff, pdiff);
     243          49 :     return mode;
     244             : }
     245             : 
     246          50 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     247             : {
     248          50 :     AVFilterContext *ctx = inlink->dst;
     249          50 :     AVFilterLink *outlink = ctx->outputs[0];
     250          50 :     PhaseContext *s = ctx->priv;
     251             :     enum PhaseMode mode;
     252             :     int plane, top, y;
     253             :     AVFrame *out;
     254             : 
     255          50 :     if (ctx->is_disabled) {
     256           0 :         av_frame_free(&s->frame);
     257             :         /* we keep a reference to the previous frame so the filter can start
     258             :          * being useful as soon as it's not disabled, avoiding the 1-frame
     259             :          * delay. */
     260           0 :         s->frame = av_frame_clone(in);
     261           0 :         return ff_filter_frame(outlink, in);
     262             :     }
     263             : 
     264          50 :     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     265          50 :     if (!out) {
     266           0 :         av_frame_free(&in);
     267           0 :         return AVERROR(ENOMEM);
     268             :     }
     269          50 :     av_frame_copy_props(out, in);
     270             : 
     271          50 :     if (!s->frame) {
     272           1 :         s->frame = in;
     273           1 :         mode = PROGRESSIVE;
     274             :     } else {
     275          49 :         mode = analyze_plane(ctx, s->mode, s->frame, in);
     276             :     }
     277             : 
     278         200 :     for (plane = 0; plane < s->nb_planes; plane++) {
     279         150 :         const uint8_t *buf = s->frame->data[plane];
     280         150 :         const uint8_t *from = in->data[plane];
     281         150 :         uint8_t *to = out->data[plane];
     282             : 
     283       28950 :         for (y = 0, top = 1; y < s->planeheight[plane]; y++, top ^= 1) {
     284       28800 :             memcpy(to, mode == (top ? BOTTOM_FIRST : TOP_FIRST) ? buf : from, s->linesize[plane]);
     285             : 
     286       28800 :             buf += s->frame->linesize[plane];
     287       28800 :             from += in->linesize[plane];
     288       28800 :             to += out->linesize[plane];
     289             :         }
     290             :     }
     291             : 
     292          50 :     if (in != s->frame)
     293          49 :         av_frame_free(&s->frame);
     294          50 :     s->frame = in;
     295          50 :     return ff_filter_frame(outlink, out);
     296             : }
     297             : 
     298           1 : static av_cold void uninit(AVFilterContext *ctx)
     299             : {
     300           1 :     PhaseContext *s = ctx->priv;
     301             : 
     302           1 :     av_frame_free(&s->frame);
     303           1 : }
     304             : 
     305             : static const AVFilterPad phase_inputs[] = {
     306             :     {
     307             :         .name         = "default",
     308             :         .type         = AVMEDIA_TYPE_VIDEO,
     309             :         .filter_frame = filter_frame,
     310             :         .config_props = config_input,
     311             :     },
     312             :     { NULL }
     313             : };
     314             : 
     315             : static const AVFilterPad phase_outputs[] = {
     316             :     {
     317             :         .name = "default",
     318             :         .type = AVMEDIA_TYPE_VIDEO,
     319             :     },
     320             :     { NULL }
     321             : };
     322             : 
     323             : AVFilter ff_vf_phase = {
     324             :     .name          = "phase",
     325             :     .description   = NULL_IF_CONFIG_SMALL("Phase shift fields."),
     326             :     .priv_size     = sizeof(PhaseContext),
     327             :     .priv_class    = &phase_class,
     328             :     .uninit        = uninit,
     329             :     .query_formats = query_formats,
     330             :     .inputs        = phase_inputs,
     331             :     .outputs       = phase_outputs,
     332             :     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
     333             : };

Generated by: LCOV version 1.13