LCOV - code coverage report
Current view: top level - libavfilter - vf_mcdeint.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 93 107 86.9 %
Date: 2017-12-16 01:21:47 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
       3             :  *
       4             :  * This file is part of FFmpeg.
       5             :  *
       6             :  * FFmpeg is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 2 of the License, or
       9             :  * (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 Foundation, Inc.,
      18             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      19             :  */
      20             : 
      21             : /**
      22             :  * @file
      23             :  * Motion Compensation Deinterlacer
      24             :  * Ported from MPlayer libmpcodecs/vf_mcdeint.c.
      25             :  *
      26             :  * Known Issues:
      27             :  *
      28             :  * The motion estimation is somewhat at the mercy of the input, if the
      29             :  * input frames are created purely based on spatial interpolation then
      30             :  * for example a thin black line or another random and not
      31             :  * interpolateable pattern will cause problems.
      32             :  * Note: completely ignoring the "unavailable" lines during motion
      33             :  * estimation did not look any better, so the most obvious solution
      34             :  * would be to improve tfields or penalize problematic motion vectors.
      35             :  *
      36             :  * If non iterative ME is used then snow currently ignores the OBMC
      37             :  * window and as a result sometimes creates artifacts.
      38             :  *
      39             :  * Only past frames are used, we should ideally use future frames too,
      40             :  * something like filtering the whole movie in forward and then
      41             :  * backward direction seems like an interesting idea but the current
      42             :  * filter framework is FAR from supporting such things.
      43             :  *
      44             :  * Combining the motion compensated image with the input image also is
      45             :  * not as trivial as it seems, simple blindly taking even lines from
      46             :  * one and odd ones from the other does not work at all as ME/MC
      47             :  * sometimes has nothing in the previous frames which matches the
      48             :  * current. The current algorithm has been found by trial and error
      49             :  * and almost certainly can be improved...
      50             :  */
      51             : 
      52             : #include "libavutil/opt.h"
      53             : #include "libavutil/pixdesc.h"
      54             : #include "libavcodec/avcodec.h"
      55             : #include "avfilter.h"
      56             : #include "formats.h"
      57             : #include "internal.h"
      58             : 
      59             : enum MCDeintMode {
      60             :     MODE_FAST = 0,
      61             :     MODE_MEDIUM,
      62             :     MODE_SLOW,
      63             :     MODE_EXTRA_SLOW,
      64             :     MODE_NB,
      65             : };
      66             : 
      67             : enum MCDeintParity {
      68             :     PARITY_TFF  =  0, ///< top field first
      69             :     PARITY_BFF  =  1, ///< bottom field first
      70             : };
      71             : 
      72             : typedef struct MCDeintContext {
      73             :     const AVClass *class;
      74             :     int mode;           ///< MCDeintMode
      75             :     int parity;         ///< MCDeintParity
      76             :     int qp;
      77             :     AVCodecContext *enc_ctx;
      78             : } MCDeintContext;
      79             : 
      80             : #define OFFSET(x) offsetof(MCDeintContext, x)
      81             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
      82             : #define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
      83             : 
      84             : static const AVOption mcdeint_options[] = {
      85             :     { "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_FAST}, 0, MODE_NB-1, FLAGS, .unit="mode" },
      86             :     CONST("fast",       NULL, MODE_FAST,       "mode"),
      87             :     CONST("medium",     NULL, MODE_MEDIUM,     "mode"),
      88             :     CONST("slow",       NULL, MODE_SLOW,       "mode"),
      89             :     CONST("extra_slow", NULL, MODE_EXTRA_SLOW, "mode"),
      90             : 
      91             :     { "parity", "set the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=PARITY_BFF}, -1, 1, FLAGS, "parity" },
      92             :     CONST("tff", "assume top field first",    PARITY_TFF, "parity"),
      93             :     CONST("bff", "assume bottom field first", PARITY_BFF, "parity"),
      94             : 
      95             :     { "qp", "set qp", OFFSET(qp), AV_OPT_TYPE_INT, {.i64=1}, INT_MIN, INT_MAX, FLAGS },
      96             :     { NULL }
      97             : };
      98             : 
      99             : AVFILTER_DEFINE_CLASS(mcdeint);
     100             : 
     101           2 : static int config_props(AVFilterLink *inlink)
     102             : {
     103           2 :     AVFilterContext *ctx = inlink->dst;
     104           2 :     MCDeintContext *mcdeint = ctx->priv;
     105             :     AVCodec *enc;
     106             :     AVCodecContext *enc_ctx;
     107           2 :     AVDictionary *opts = NULL;
     108             :     int ret;
     109             : 
     110           2 :     if (!(enc = avcodec_find_encoder(AV_CODEC_ID_SNOW))) {
     111           0 :         av_log(ctx, AV_LOG_ERROR, "Snow encoder is not enabled in libavcodec\n");
     112           0 :         return AVERROR(EINVAL);
     113             :     }
     114             : 
     115           2 :     mcdeint->enc_ctx = avcodec_alloc_context3(enc);
     116           2 :     if (!mcdeint->enc_ctx)
     117           0 :         return AVERROR(ENOMEM);
     118           2 :     enc_ctx = mcdeint->enc_ctx;
     119           2 :     enc_ctx->width  = inlink->w;
     120           2 :     enc_ctx->height = inlink->h;
     121           2 :     enc_ctx->time_base = (AVRational){1,25};  // meaningless
     122           2 :     enc_ctx->gop_size = INT_MAX;
     123           2 :     enc_ctx->max_b_frames = 0;
     124           2 :     enc_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
     125           2 :     enc_ctx->flags = AV_CODEC_FLAG_QSCALE | AV_CODEC_FLAG_LOW_DELAY;
     126           2 :     enc_ctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
     127           2 :     enc_ctx->global_quality = 1;
     128           2 :     enc_ctx->me_cmp = enc_ctx->me_sub_cmp = FF_CMP_SAD;
     129           2 :     enc_ctx->mb_cmp = FF_CMP_SSE;
     130           2 :     av_dict_set(&opts, "memc_only", "1", 0);
     131           2 :     av_dict_set(&opts, "no_bitstream", "1", 0);
     132             : 
     133           2 :     switch (mcdeint->mode) {
     134           0 :     case MODE_EXTRA_SLOW:
     135           0 :         enc_ctx->refs = 3;
     136           0 :     case MODE_SLOW:
     137           0 :         av_dict_set(&opts, "motion_est", "iter", 0);
     138           1 :     case MODE_MEDIUM:
     139           1 :         enc_ctx->flags |= AV_CODEC_FLAG_4MV;
     140           1 :         enc_ctx->dia_size = 2;
     141           2 :     case MODE_FAST:
     142           2 :         enc_ctx->flags |= AV_CODEC_FLAG_QPEL;
     143             :     }
     144             : 
     145           2 :     ret = avcodec_open2(enc_ctx, enc, &opts);
     146           2 :     av_dict_free(&opts);
     147           2 :     if (ret < 0)
     148           0 :         return ret;
     149             : 
     150           2 :     return 0;
     151             : }
     152             : 
     153           2 : static av_cold void uninit(AVFilterContext *ctx)
     154             : {
     155           2 :     MCDeintContext *mcdeint = ctx->priv;
     156             : 
     157           2 :     avcodec_free_context(&mcdeint->enc_ctx);
     158           2 : }
     159             : 
     160           2 : static int query_formats(AVFilterContext *ctx)
     161             : {
     162             :     static const enum AVPixelFormat pix_fmts[] = {
     163             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
     164             :     };
     165           2 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     166           2 :     if (!fmts_list)
     167           0 :         return AVERROR(ENOMEM);
     168           2 :     return ff_set_common_formats(ctx, fmts_list);
     169             : }
     170             : 
     171          60 : static int filter_frame(AVFilterLink *inlink, AVFrame *inpic)
     172             : {
     173          60 :     MCDeintContext *mcdeint = inlink->dst->priv;
     174          60 :     AVFilterLink *outlink = inlink->dst->outputs[0];
     175             :     AVFrame *outpic, *frame_dec;
     176          60 :     AVPacket pkt = {0};
     177          60 :     int x, y, i, ret, got_frame = 0;
     178             : 
     179          60 :     outpic = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     180          60 :     if (!outpic) {
     181           0 :         av_frame_free(&inpic);
     182           0 :         return AVERROR(ENOMEM);
     183             :     }
     184          60 :     av_frame_copy_props(outpic, inpic);
     185          60 :     inpic->quality = mcdeint->qp * FF_QP2LAMBDA;
     186             : 
     187          60 :     av_init_packet(&pkt);
     188             : 
     189          60 :     ret = avcodec_encode_video2(mcdeint->enc_ctx, &pkt, inpic, &got_frame);
     190          60 :     if (ret < 0)
     191           0 :         goto end;
     192             : 
     193          60 :     frame_dec = mcdeint->enc_ctx->coded_frame;
     194             : 
     195         240 :     for (i = 0; i < 3; i++) {
     196         180 :         int is_chroma = !!i;
     197         180 :         int w = AV_CEIL_RSHIFT(inlink->w, is_chroma);
     198         180 :         int h = AV_CEIL_RSHIFT(inlink->h, is_chroma);
     199         180 :         int fils = frame_dec->linesize[i];
     200         180 :         int srcs = inpic    ->linesize[i];
     201         180 :         int dsts = outpic   ->linesize[i];
     202             : 
     203       69300 :         for (y = 0; y < h; y++) {
     204       69120 :             if ((y ^ mcdeint->parity) & 1) {
     205    18696960 :                 for (x = 0; x < w; x++) {
     206    18662400 :                     uint8_t *filp = &frame_dec->data[i][x + y*fils];
     207    18662400 :                     uint8_t *srcp = &inpic    ->data[i][x + y*srcs];
     208    18662400 :                     uint8_t *dstp = &outpic   ->data[i][x + y*dsts];
     209             : 
     210    37238400 :                     if (y > 0 && y < h-1){
     211    18576000 :                         int is_edge = x < 3 || x > w-4;
     212    18576000 :                         int diff0 = filp[-fils] - srcp[-srcs];
     213    18576000 :                         int diff1 = filp[+fils] - srcp[+srcs];
     214    18576000 :                         int temp = filp[0];
     215             : 
     216             : #define DELTA(j) av_clip(j, -x, w-1-x)
     217             : 
     218             : #define GET_SCORE_EDGE(j)\
     219             :    FFABS(srcp[-srcs+DELTA(-1+(j))] - srcp[+srcs+DELTA(-1-(j))])+\
     220             :    FFABS(srcp[-srcs+DELTA(j)     ] - srcp[+srcs+DELTA(  -(j))])+\
     221             :    FFABS(srcp[-srcs+DELTA(1+(j)) ] - srcp[+srcs+DELTA( 1-(j))])
     222             : 
     223             : #define GET_SCORE(j)\
     224             :    FFABS(srcp[-srcs-1+(j)] - srcp[+srcs-1-(j)])+\
     225             :    FFABS(srcp[-srcs  +(j)] - srcp[+srcs  -(j)])+\
     226             :    FFABS(srcp[-srcs+1+(j)] - srcp[+srcs+1-(j)])
     227             : 
     228             : #define CHECK_EDGE(j)\
     229             :     {   int score = GET_SCORE_EDGE(j);\
     230             :         if (score < spatial_score){\
     231             :             spatial_score = score;\
     232             :             diff0 = filp[-fils+DELTA(j)]    - srcp[-srcs+DELTA(j)];\
     233             :             diff1 = filp[+fils+DELTA(-(j))] - srcp[+srcs+DELTA(-(j))];\
     234             : 
     235             : #define CHECK(j)\
     236             :     {   int score = GET_SCORE(j);\
     237             :         if (score < spatial_score){\
     238             :             spatial_score= score;\
     239             :             diff0 = filp[-fils+(j)] - srcp[-srcs+(j)];\
     240             :             diff1 = filp[+fils-(j)] - srcp[+srcs-(j)];\
     241             : 
     242    18576000 :                         if (is_edge) {
     243      206280 :                             int spatial_score = GET_SCORE_EDGE(0) - 1;
     244      206280 :                             CHECK_EDGE(-1) CHECK_EDGE(-2) }} }}
     245      206280 :                             CHECK_EDGE( 1) CHECK_EDGE( 2) }} }}
     246             :                         } else {
     247    18369720 :                             int spatial_score = GET_SCORE(0) - 1;
     248    18369720 :                             CHECK(-1) CHECK(-2) }} }}
     249    18369720 :                             CHECK( 1) CHECK( 2) }} }}
     250             :                         }
     251             : 
     252             : 
     253    18576000 :                         if (diff0 + diff1 > 0)
     254     7866906 :                             temp -= (diff0 + diff1 - FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
     255             :                         else
     256    10709094 :                             temp -= (diff0 + diff1 + FFABS(FFABS(diff0) - FFABS(diff1)) / 2) / 2;
     257    18576000 :                         *filp = *dstp = temp > 255U ? ~(temp>>31) : temp;
     258             :                     } else {
     259       86400 :                         *dstp = *filp;
     260             :                     }
     261             :                 }
     262             :             }
     263             :         }
     264             : 
     265       69300 :         for (y = 0; y < h; y++) {
     266       69120 :             if (!((y ^ mcdeint->parity) & 1)) {
     267    18696960 :                 for (x = 0; x < w; x++) {
     268    37324800 :                     frame_dec->data[i][x + y*fils] =
     269    37324800 :                     outpic   ->data[i][x + y*dsts] = inpic->data[i][x + y*srcs];
     270             :                 }
     271             :             }
     272             :         }
     273             :     }
     274          60 :     mcdeint->parity ^= 1;
     275             : 
     276          60 : end:
     277          60 :     av_packet_unref(&pkt);
     278          60 :     av_frame_free(&inpic);
     279          60 :     if (ret < 0) {
     280           0 :         av_frame_free(&outpic);
     281           0 :         return ret;
     282             :     }
     283          60 :     return ff_filter_frame(outlink, outpic);
     284             : }
     285             : 
     286             : static const AVFilterPad mcdeint_inputs[] = {
     287             :     {
     288             :         .name         = "default",
     289             :         .type         = AVMEDIA_TYPE_VIDEO,
     290             :         .filter_frame = filter_frame,
     291             :         .config_props = config_props,
     292             :     },
     293             :     { NULL }
     294             : };
     295             : 
     296             : static const AVFilterPad mcdeint_outputs[] = {
     297             :     {
     298             :         .name = "default",
     299             :         .type = AVMEDIA_TYPE_VIDEO,
     300             :     },
     301             :     { NULL }
     302             : };
     303             : 
     304             : AVFilter ff_vf_mcdeint = {
     305             :     .name          = "mcdeint",
     306             :     .description   = NULL_IF_CONFIG_SMALL("Apply motion compensating deinterlacing."),
     307             :     .priv_size     = sizeof(MCDeintContext),
     308             :     .uninit        = uninit,
     309             :     .query_formats = query_formats,
     310             :     .inputs        = mcdeint_inputs,
     311             :     .outputs       = mcdeint_outputs,
     312             :     .priv_class    = &mcdeint_class,
     313             : };

Generated by: LCOV version 1.13