LCOV - code coverage report
Current view: top level - libavfilter - vf_fieldmatch.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 408 538 75.8 %
Date: 2018-05-20 11:54:08 Functions: 20 20 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2012 Fredrik Mellbin
       3             :  * Copyright (c) 2013 Clément Bœsch
       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             :  * Fieldmatching filter, ported from VFM filter (VapourSynth) by Clément.
      25             :  * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
      26             :  * light clone of the TIVTC/TFM (AviSynth) filter written by Kevin Stone
      27             :  * (tritical), the original author.
      28             :  *
      29             :  * @see http://bengal.missouri.edu/~kes25c/
      30             :  * @see http://www.vapoursynth.com/about/
      31             :  */
      32             : 
      33             : #include <inttypes.h>
      34             : 
      35             : #include "libavutil/avassert.h"
      36             : #include "libavutil/imgutils.h"
      37             : #include "libavutil/opt.h"
      38             : #include "libavutil/timestamp.h"
      39             : #include "avfilter.h"
      40             : #include "filters.h"
      41             : #include "internal.h"
      42             : 
      43             : #define INPUT_MAIN     0
      44             : #define INPUT_CLEANSRC 1
      45             : 
      46             : enum fieldmatch_parity {
      47             :     FM_PARITY_AUTO   = -1,
      48             :     FM_PARITY_BOTTOM =  0,
      49             :     FM_PARITY_TOP    =  1,
      50             : };
      51             : 
      52             : enum matching_mode {
      53             :     MODE_PC,
      54             :     MODE_PC_N,
      55             :     MODE_PC_U,
      56             :     MODE_PC_N_UB,
      57             :     MODE_PCN,
      58             :     MODE_PCN_UB,
      59             :     NB_MODE
      60             : };
      61             : 
      62             : enum comb_matching_mode {
      63             :     COMBMATCH_NONE,
      64             :     COMBMATCH_SC,
      65             :     COMBMATCH_FULL,
      66             :     NB_COMBMATCH
      67             : };
      68             : 
      69             : enum comb_dbg {
      70             :     COMBDBG_NONE,
      71             :     COMBDBG_PCN,
      72             :     COMBDBG_PCNUB,
      73             :     NB_COMBDBG
      74             : };
      75             : 
      76             : typedef struct FieldMatchContext {
      77             :     const AVClass *class;
      78             : 
      79             :     AVFrame *prv,  *src,  *nxt;     ///< main sliding window of 3 frames
      80             :     AVFrame *prv2, *src2, *nxt2;    ///< sliding window of the optional second stream
      81             :     int got_frame[2];               ///< frame request flag for each input stream
      82             :     int hsub, vsub;                 ///< chroma subsampling values
      83             :     int bpc;                        ///< bytes per component
      84             :     uint32_t eof;                   ///< bitmask for end of stream
      85             :     int64_t lastscdiff;
      86             :     int64_t lastn;
      87             : 
      88             :     /* options */
      89             :     int order;
      90             :     int ppsrc;
      91             :     int mode;                       ///< matching_mode
      92             :     int field;
      93             :     int mchroma;
      94             :     int y0, y1;
      95             :     int64_t scthresh;
      96             :     double scthresh_flt;
      97             :     int combmatch;                  ///< comb_matching_mode
      98             :     int combdbg;
      99             :     int cthresh;
     100             :     int chroma;
     101             :     int blockx, blocky;
     102             :     int combpel;
     103             : 
     104             :     /* misc buffers */
     105             :     uint8_t *map_data[4];
     106             :     int map_linesize[4];
     107             :     uint8_t *cmask_data[4];
     108             :     int cmask_linesize[4];
     109             :     int *c_array;
     110             :     int tpitchy, tpitchuv;
     111             :     uint8_t *tbuffer;
     112             : } FieldMatchContext;
     113             : 
     114             : #define OFFSET(x) offsetof(FieldMatchContext, x)
     115             : #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
     116             : 
     117             : static const AVOption fieldmatch_options[] = {
     118             :     { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" },
     119             :         { "auto", "auto detect parity",        0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "order" },
     120             :         { "bff",  "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "order" },
     121             :         { "tff",  "assume top field first",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "order" },
     122             :     { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC_N}, MODE_PC, NB_MODE-1, FLAGS, "mode" },
     123             :         { "pc",      "2-way match (p/c)",                                                                    0, AV_OPT_TYPE_CONST, {.i64=MODE_PC},      INT_MIN, INT_MAX, FLAGS, "mode" },
     124             :         { "pc_n",    "2-way match + 3rd match on combed (p/c + u)",                                          0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N},    INT_MIN, INT_MAX, FLAGS, "mode" },
     125             :         { "pc_u",    "2-way match + 3rd match (same order) on combed (p/c + u)",                             0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U},    INT_MIN, INT_MAX, FLAGS, "mode" },
     126             :         { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)",  0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, "mode" },
     127             :         { "pcn",     "3-way match (p/c/n)",                                                                  0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN},     INT_MIN, INT_MAX, FLAGS, "mode" },
     128             :         { "pcn_ub",  "3-way match + 4th/5th matches on combed (p/c/n + u/b)",                                0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB},  INT_MIN, INT_MAX, FLAGS, "mode" },
     129             :     { "ppsrc", "mark main input as a pre-processed input and activate clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
     130             :     { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" },
     131             :         { "auto",   "automatic (same value as 'order')",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "field" },
     132             :         { "bottom", "bottom field",                         0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "field" },
     133             :         { "top",    "top field",                            0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "field" },
     134             :     { "mchroma", "set whether or not chroma is included during the match comparisons", OFFSET(mchroma), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1,  FLAGS },
     135             :     { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
     136             :     { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y1), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
     137             :     { "scthresh", "set scene change detection threshold", OFFSET(scthresh_flt), AV_OPT_TYPE_DOUBLE, {.dbl=12}, 0, 100, FLAGS },
     138             :     { "combmatch", "set combmatching mode", OFFSET(combmatch), AV_OPT_TYPE_INT, {.i64=COMBMATCH_SC}, COMBMATCH_NONE, NB_COMBMATCH-1, FLAGS, "combmatching" },
     139             :         { "none", "disable combmatching",                     0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
     140             :         { "sc",   "enable combmatching only on scene change", 0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_SC},   INT_MIN, INT_MAX, FLAGS, "combmatching" },
     141             :         { "full", "enable combmatching all the time",         0, AV_OPT_TYPE_CONST, {.i64=COMBMATCH_FULL}, INT_MIN, INT_MAX, FLAGS, "combmatching" },
     142             :     { "combdbg",   "enable comb debug", OFFSET(combdbg), AV_OPT_TYPE_INT, {.i64=COMBDBG_NONE}, COMBDBG_NONE, NB_COMBDBG-1, FLAGS, "dbglvl" },
     143             :         { "none",  "no forced calculation", 0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_NONE},  INT_MIN, INT_MAX, FLAGS, "dbglvl" },
     144             :         { "pcn",   "calculate p/c/n",       0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCN},   INT_MIN, INT_MAX, FLAGS, "dbglvl" },
     145             :         { "pcnub", "calculate p/c/n/u/b",   0, AV_OPT_TYPE_CONST, {.i64=COMBDBG_PCNUB}, INT_MIN, INT_MAX, FLAGS, "dbglvl" },
     146             :     { "cthresh", "set the area combing threshold used for combed frame detection",       OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64= 9}, -1, 0xff, FLAGS },
     147             :     { "chroma",  "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma),  AV_OPT_TYPE_BOOL,{.i64= 0},  0,    1, FLAGS },
     148             :     { "blockx",  "set the x-axis size of the window used during combed frame detection", OFFSET(blockx),  AV_OPT_TYPE_INT, {.i64=16},  4, 1<<9, FLAGS },
     149             :     { "blocky",  "set the y-axis size of the window used during combed frame detection", OFFSET(blocky),  AV_OPT_TYPE_INT, {.i64=16},  4, 1<<9, FLAGS },
     150             :     { "combpel", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(combpel), AV_OPT_TYPE_INT, {.i64=80}, 0, INT_MAX, FLAGS },
     151             :     { NULL }
     152             : };
     153             : 
     154             : AVFILTER_DEFINE_CLASS(fieldmatch);
     155             : 
     156        1579 : static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
     157             : {
     158        1579 :     return plane ? AV_CEIL_RSHIFT(f->width, fm->hsub) : f->width;
     159             : }
     160             : 
     161        1579 : static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
     162             : {
     163        1579 :     return plane ? AV_CEIL_RSHIFT(f->height, fm->vsub) : f->height;
     164             : }
     165             : 
     166         140 : static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
     167             : {
     168             :     int x, y;
     169         140 :     const uint8_t *srcp1 = f1->data[0];
     170         140 :     const uint8_t *srcp2 = f2->data[0];
     171         140 :     const int src1_linesize = f1->linesize[0];
     172         140 :     const int src2_linesize = f2->linesize[0];
     173         140 :     const int width  = f1->width;
     174         140 :     const int height = f1->height;
     175         140 :     int64_t acc = 0;
     176             : 
     177       40460 :     for (y = 0; y < height; y++) {
     178    14232960 :         for (x = 0; x < width; x++)
     179    14192640 :             acc += abs(srcp1[x] - srcp2[x]);
     180       40320 :         srcp1 += src1_linesize;
     181       40320 :         srcp2 += src2_linesize;
     182             :     }
     183         140 :     return acc;
     184             : }
     185             : 
     186         625 : static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
     187             : {
     188             :     int y;
     189             : 
     190      161185 :     for (y = 0; y < h; y++) {
     191      160560 :         memset(data, v, w);
     192      160560 :         data += linesize;
     193             :     }
     194         625 : }
     195             : 
     196         220 : static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
     197             : {
     198         220 :     int x, y, plane, max_v = 0;
     199         220 :     const int cthresh = fm->cthresh;
     200         220 :     const int cthresh6 = cthresh * 6;
     201             : 
     202         440 :     for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
     203         220 :         const uint8_t *srcp = src->data[plane];
     204         220 :         const int src_linesize = src->linesize[plane];
     205         220 :         const int width  = get_width (fm, src, plane);
     206         220 :         const int height = get_height(fm, src, plane);
     207         220 :         uint8_t *cmkp = fm->cmask_data[plane];
     208         220 :         const int cmk_linesize = fm->cmask_linesize[plane];
     209             : 
     210         220 :         if (cthresh < 0) {
     211           0 :             fill_buf(cmkp, width, height, cmk_linesize, 0xff);
     212           0 :             continue;
     213             :         }
     214         220 :         fill_buf(cmkp, width, height, cmk_linesize, 0);
     215             : 
     216             :         /* [1 -3 4 -3 1] vertical filter */
     217             : #define FILTER(xm2, xm1, xp1, xp2) \
     218             :         abs(  4 * srcp[x] \
     219             :              -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
     220             :              +    (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
     221             : 
     222             :         /* first line */
     223       77660 :         for (x = 0; x < width; x++) {
     224       77440 :             const int s1 = abs(srcp[x] - srcp[x + src_linesize]);
     225       77440 :             if (s1 > cthresh && FILTER(2, 1, 1, 2))
     226       40180 :                 cmkp[x] = 0xff;
     227             :         }
     228         220 :         srcp += src_linesize;
     229         220 :         cmkp += cmk_linesize;
     230             : 
     231             :         /* second line */
     232       77660 :         for (x = 0; x < width; x++) {
     233       77440 :             const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
     234       77440 :             const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
     235       77440 :             if (s1 > cthresh && s2 > cthresh && FILTER(2, -1, 1, 2))
     236       37099 :                 cmkp[x] = 0xff;
     237             :         }
     238         220 :         srcp += src_linesize;
     239         220 :         cmkp += cmk_linesize;
     240             : 
     241             :         /* all lines minus first two and last two */
     242       62700 :         for (y = 2; y < height-2; y++) {
     243    22055440 :             for (x = 0; x < width; x++) {
     244    21992960 :                 const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
     245    21992960 :                 const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
     246    21992960 :                 if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, 2))
     247     9768879 :                     cmkp[x] = 0xff;
     248             :             }
     249       62480 :             srcp += src_linesize;
     250       62480 :             cmkp += cmk_linesize;
     251             :         }
     252             : 
     253             :         /* before-last line */
     254       77660 :         for (x = 0; x < width; x++) {
     255       77440 :             const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
     256       77440 :             const int s2 = abs(srcp[x] - srcp[x + src_linesize]);
     257       77440 :             if (s1 > cthresh && s2 > cthresh && FILTER(-2, -1, 1, -2))
     258       30781 :                 cmkp[x] = 0xff;
     259             :         }
     260         220 :         srcp += src_linesize;
     261         220 :         cmkp += cmk_linesize;
     262             : 
     263             :         /* last line */
     264       77660 :         for (x = 0; x < width; x++) {
     265       77440 :             const int s1 = abs(srcp[x] - srcp[x - src_linesize]);
     266       77440 :             if (s1 > cthresh && FILTER(-2, -1, -1, -2))
     267       35380 :                 cmkp[x] = 0xff;
     268             :         }
     269             :     }
     270             : 
     271         220 :     if (fm->chroma) {
     272           0 :         uint8_t *cmkp  = fm->cmask_data[0];
     273           0 :         uint8_t *cmkpU = fm->cmask_data[1];
     274           0 :         uint8_t *cmkpV = fm->cmask_data[2];
     275           0 :         const int width  = AV_CEIL_RSHIFT(src->width,  fm->hsub);
     276           0 :         const int height = AV_CEIL_RSHIFT(src->height, fm->vsub);
     277           0 :         const int cmk_linesize   = fm->cmask_linesize[0] << 1;
     278           0 :         const int cmk_linesizeUV = fm->cmask_linesize[2];
     279           0 :         uint8_t *cmkpp  = cmkp - (cmk_linesize>>1);
     280           0 :         uint8_t *cmkpn  = cmkp + (cmk_linesize>>1);
     281           0 :         uint8_t *cmkpnn = cmkp +  cmk_linesize;
     282           0 :         for (y = 1; y < height - 1; y++) {
     283           0 :             cmkpp  += cmk_linesize;
     284           0 :             cmkp   += cmk_linesize;
     285           0 :             cmkpn  += cmk_linesize;
     286           0 :             cmkpnn += cmk_linesize;
     287           0 :             cmkpV  += cmk_linesizeUV;
     288           0 :             cmkpU  += cmk_linesizeUV;
     289           0 :             for (x = 1; x < width - 1; x++) {
     290             : #define HAS_FF_AROUND(p, lz) (p[(x)-1 - (lz)] == 0xff || p[(x) - (lz)] == 0xff || p[(x)+1 - (lz)] == 0xff || \
     291             :                               p[(x)-1       ] == 0xff ||                          p[(x)+1       ] == 0xff || \
     292             :                               p[(x)-1 + (lz)] == 0xff || p[(x) + (lz)] == 0xff || p[(x)+1 + (lz)] == 0xff)
     293           0 :                 if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
     294           0 :                     (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
     295           0 :                     ((uint16_t*)cmkp)[x]  = 0xffff;
     296           0 :                     ((uint16_t*)cmkpn)[x] = 0xffff;
     297           0 :                     if (y&1) ((uint16_t*)cmkpp)[x]  = 0xffff;
     298           0 :                     else     ((uint16_t*)cmkpnn)[x] = 0xffff;
     299             :                 }
     300             :             }
     301             :         }
     302             :     }
     303             : 
     304             :     {
     305         220 :         const int blockx = fm->blockx;
     306         220 :         const int blocky = fm->blocky;
     307         220 :         const int xhalf = blockx/2;
     308         220 :         const int yhalf = blocky/2;
     309         220 :         const int cmk_linesize = fm->cmask_linesize[0];
     310         220 :         const uint8_t *cmkp    = fm->cmask_data[0] + cmk_linesize;
     311         220 :         const int width  = src->width;
     312         220 :         const int height = src->height;
     313         220 :         const int xblocks = ((width+xhalf)/blockx) + 1;
     314         220 :         const int xblocks4 = xblocks<<2;
     315         220 :         const int yblocks = ((height+yhalf)/blocky) + 1;
     316         220 :         int *c_array = fm->c_array;
     317         220 :         const int arraysize = (xblocks*yblocks)<<2;
     318         220 :         int      heighta = (height/(blocky/2))*(blocky/2);
     319         220 :         const int widtha = (width /(blockx/2))*(blockx/2);
     320         220 :         if (heighta == height)
     321         220 :             heighta = height - yhalf;
     322         220 :         memset(c_array, 0, arraysize * sizeof(*c_array));
     323             : 
     324             : #define C_ARRAY_ADD(v) do {                         \
     325             :     const int box1 = (x / blockx) * 4;              \
     326             :     const int box2 = ((x + xhalf) / blockx) * 4;    \
     327             :     c_array[temp1 + box1    ] += v;                 \
     328             :     c_array[temp1 + box2 + 1] += v;                 \
     329             :     c_array[temp2 + box1 + 2] += v;                 \
     330             :     c_array[temp2 + box2 + 3] += v;                 \
     331             : } while (0)
     332             : 
     333             : #define VERTICAL_HALF(y_start, y_end) do {                                  \
     334             :     for (y = y_start; y < y_end; y++) {                                     \
     335             :         const int temp1 = (y / blocky) * xblocks4;                          \
     336             :         const int temp2 = ((y + yhalf) / blocky) * xblocks4;                \
     337             :         for (x = 0; x < width; x++)                                         \
     338             :             if (cmkp[x - cmk_linesize] == 0xff &&                           \
     339             :                 cmkp[x               ] == 0xff &&                           \
     340             :                 cmkp[x + cmk_linesize] == 0xff)                             \
     341             :                 C_ARRAY_ADD(1);                                             \
     342             :         cmkp += cmk_linesize;                                               \
     343             :     }                                                                       \
     344             : } while (0)
     345             : 
     346         220 :         VERTICAL_HALF(1, yhalf);
     347             : 
     348        7700 :         for (y = yhalf; y < heighta; y += yhalf) {
     349        7480 :             const int temp1 = (y / blocky) * xblocks4;
     350        7480 :             const int temp2 = ((y + yhalf) / blocky) * xblocks4;
     351             : 
     352      336600 :             for (x = 0; x < widtha; x += xhalf) {
     353      329120 :                 const uint8_t *cmkp_tmp = cmkp + x;
     354      329120 :                 int u, v, sum = 0;
     355     2962080 :                 for (u = 0; u < yhalf; u++) {
     356    23696640 :                     for (v = 0; v < xhalf; v++)
     357    30429488 :                         if (cmkp_tmp[v - cmk_linesize] == 0xff &&
     358    17819628 :                             cmkp_tmp[v               ] == 0xff &&
     359     8453820 :                             cmkp_tmp[v + cmk_linesize] == 0xff)
     360     7819534 :                             sum++;
     361     2632960 :                     cmkp_tmp += cmk_linesize;
     362             :                 }
     363      329120 :                 if (sum)
     364      200837 :                     C_ARRAY_ADD(sum);
     365             :             }
     366             : 
     367        7480 :             for (x = widtha; x < width; x++) {
     368           0 :                 const uint8_t *cmkp_tmp = cmkp + x;
     369           0 :                 int u, sum = 0;
     370           0 :                 for (u = 0; u < yhalf; u++) {
     371           0 :                     if (cmkp_tmp[-cmk_linesize] == 0xff &&
     372           0 :                         cmkp_tmp[            0] == 0xff &&
     373           0 :                         cmkp_tmp[ cmk_linesize] == 0xff)
     374           0 :                         sum++;
     375           0 :                     cmkp_tmp += cmk_linesize;
     376             :                 }
     377           0 :                 if (sum)
     378           0 :                     C_ARRAY_ADD(sum);
     379             :             }
     380             : 
     381        7480 :             cmkp += cmk_linesize * yhalf;
     382             :         }
     383             : 
     384         220 :         VERTICAL_HALF(heighta, height - 1);
     385             : 
     386      384780 :         for (x = 0; x < arraysize; x++)
     387      384560 :             if (c_array[x] > max_v)
     388         971 :                 max_v = c_array[x];
     389             :     }
     390         220 :     return max_v;
     391             : }
     392             : 
     393             : // the secret is that tbuffer is an interlaced, offset subset of all the lines
     394         405 : static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
     395             :                                 const uint8_t *nxtp, int nxt_linesize,
     396             :                                 uint8_t *tbuffer,    int tbuf_linesize,
     397             :                                 int width, int height)
     398             : {
     399             :     int y, x;
     400             : 
     401         405 :     prvp -= prv_linesize;
     402         405 :     nxtp -= nxt_linesize;
     403       49005 :     for (y = 0; y < height; y++) {
     404    12536856 :         for (x = 0; x < width; x++)
     405    12488256 :             tbuffer[x] = FFABS(prvp[x] - nxtp[x]);
     406       48600 :         prvp += prv_linesize;
     407       48600 :         nxtp += nxt_linesize;
     408       48600 :         tbuffer += tbuf_linesize;
     409             :     }
     410         405 : }
     411             : 
     412             : /**
     413             :  * Build a map over which pixels differ a lot/a little
     414             :  */
     415         405 : static void build_diff_map(FieldMatchContext *fm,
     416             :                            const uint8_t *prvp, int prv_linesize,
     417             :                            const uint8_t *nxtp, int nxt_linesize,
     418             :                            uint8_t *dstp, int dst_linesize, int height,
     419             :                            int width, int plane)
     420             : {
     421             :     int x, y, u, diff, count;
     422         405 :     int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
     423         405 :     const uint8_t *dp = fm->tbuffer + tpitch;
     424             : 
     425         405 :     build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
     426             :                         fm->tbuffer, tpitch, width, height>>1);
     427             : 
     428       48195 :     for (y = 2; y < height - 2; y += 2) {
     429    12250386 :         for (x = 1; x < width - 1; x++) {
     430    12202596 :             diff = dp[x];
     431    12202596 :             if (diff > 3) {
     432    17981804 :                 for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
     433     9251794 :                     count += dp[u-tpitch] > 3;
     434     9251794 :                     count += dp[u       ] > 3;
     435     9251794 :                     count += dp[u+tpitch] > 3;
     436             :                 }
     437     8730010 :                 if (count > 1) {
     438     8722083 :                     dstp[x] = 1;
     439     8722083 :                     if (diff > 19) {
     440     5460375 :                         int upper = 0, lower = 0;
     441    18209862 :                         for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
     442    12749487 :                             if (dp[u-tpitch] > 19) { count++; upper = 1; }
     443    12749487 :                             if (dp[u       ] > 19)   count++;
     444    12749487 :                             if (dp[u+tpitch] > 19) { count++; lower = 1; }
     445             :                         }
     446     5460375 :                         if (count > 3) {
     447     5022758 :                             if (upper && lower) {
     448     4554450 :                                 dstp[x] |= 1<<1;
     449             :                             } else {
     450      468308 :                                 int upper2 = 0, lower2 = 0;
     451     4659994 :                                 for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
     452     4191686 :                                     if (y != 2 &&        dp[u-2*tpitch] > 19) upper2 = 1;
     453     4191686 :                                     if (                 dp[u-  tpitch] > 19) upper  = 1;
     454     4191686 :                                     if (                 dp[u+  tpitch] > 19) lower  = 1;
     455     4191686 :                                     if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
     456             :                                 }
     457      468308 :                                 if ((upper && (lower || upper2)) ||
     458       57479 :                                     (lower && (upper || lower2)))
     459      455472 :                                     dstp[x] |= 1<<1;
     460       12836 :                                 else if (count > 5)
     461        9145 :                                     dstp[x] |= 1<<2;
     462             :                             }
     463             :                         }
     464             :                     }
     465             :                 }
     466             :             }
     467             :         }
     468       47790 :         dp += tpitch;
     469       47790 :         dstp += dst_linesize;
     470             :     }
     471         405 : }
     472             : 
     473             : enum { mP, mC, mN, mB, mU };
     474             : 
     475         810 : static int get_field_base(int match, int field)
     476             : {
     477         810 :     return match < 3 ? 2 - field : 1 + field;
     478             : }
     479             : 
     480         810 : static AVFrame *select_frame(FieldMatchContext *fm, int match)
     481             : {
     482         810 :     if      (match == mP || match == mB) return fm->prv;
     483         405 :     else if (match == mN || match == mU) return fm->nxt;
     484         405 :     else  /* match == mC */              return fm->src;
     485             : }
     486             : 
     487         135 : static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
     488             : {
     489             :     int plane, ret;
     490         135 :     uint64_t accumPc = 0, accumPm = 0, accumPml = 0;
     491         135 :     uint64_t accumNc = 0, accumNm = 0, accumNml = 0;
     492             :     int norm1, norm2, mtn1, mtn2;
     493             :     float c1, c2, mr;
     494         135 :     const AVFrame *src = fm->src;
     495             : 
     496         540 :     for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
     497             :         int x, y, temp1, temp2, fbase;
     498             :         const AVFrame *prev, *next;
     499         405 :         uint8_t *mapp    = fm->map_data[plane];
     500         405 :         int map_linesize = fm->map_linesize[plane];
     501         405 :         const uint8_t *srcp = src->data[plane];
     502         405 :         const int src_linesize  = src->linesize[plane];
     503         405 :         const int srcf_linesize = src_linesize << 1;
     504             :         int prv_linesize,  nxt_linesize;
     505             :         int prvf_linesize, nxtf_linesize;
     506         405 :         const int width  = get_width (fm, src, plane);
     507         405 :         const int height = get_height(fm, src, plane);
     508         405 :         const int y0a = fm->y0 >> (plane ? fm->vsub : 0);
     509         405 :         const int y1a = fm->y1 >> (plane ? fm->vsub : 0);
     510         405 :         const int startx = (plane == 0 ? 8 : 8 >> fm->hsub);
     511         405 :         const int stopx  = width - startx;
     512             :         const uint8_t *srcpf, *srcf, *srcnf;
     513             :         const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
     514             : 
     515         405 :         fill_buf(mapp, width, height, map_linesize, 0);
     516             : 
     517             :         /* match1 */
     518         405 :         fbase = get_field_base(match1, field);
     519         405 :         srcf  = srcp + (fbase + 1) * src_linesize;
     520         405 :         srcpf = srcf - srcf_linesize;
     521         405 :         srcnf = srcf + srcf_linesize;
     522         405 :         mapp  = mapp + fbase * map_linesize;
     523         405 :         prev = select_frame(fm, match1);
     524         405 :         prv_linesize  = prev->linesize[plane];
     525         405 :         prvf_linesize = prv_linesize << 1;
     526         405 :         prvpf = prev->data[plane] + fbase * prv_linesize;   // previous frame, previous field
     527         405 :         prvnf = prvpf + prvf_linesize;                      // previous frame, next     field
     528             : 
     529             :         /* match2 */
     530         405 :         fbase = get_field_base(match2, field);
     531         405 :         next = select_frame(fm, match2);
     532         405 :         nxt_linesize  = next->linesize[plane];
     533         405 :         nxtf_linesize = nxt_linesize << 1;
     534         405 :         nxtpf = next->data[plane] + fbase * nxt_linesize;   // next frame, previous field
     535         405 :         nxtnf = nxtpf + nxtf_linesize;                      // next frame, next     field
     536             : 
     537         405 :         map_linesize <<= 1;
     538         405 :         if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
     539           0 :             build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
     540             :                            mapp, map_linesize, height, width, plane);
     541             :         else
     542         405 :             build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
     543             :                            mapp + map_linesize, map_linesize, height, width, plane);
     544             : 
     545       48195 :         for (y = 2; y < height - 2; y += 2) {
     546       47790 :             if (y0a == y1a || y < y0a || y > y1a) {
     547    11786958 :                 for (x = startx; x < stopx; x++) {
     548    11739168 :                     if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
     549     9127496 :                         temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x]; // [1 4 1]
     550             : 
     551     9127496 :                         temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
     552     9127496 :                         if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
     553     3925691 :                             accumPc += temp2;
     554     9127496 :                         if (temp2 > 42) {
     555     3357735 :                             if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
     556     2693152 :                                 accumPm += temp2;
     557     3357735 :                             if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
     558        1527 :                                 accumPml += temp2;
     559             :                         }
     560             : 
     561     9127496 :                         temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
     562     9127496 :                         if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
     563     6710599 :                             accumNc += temp2;
     564     9127496 :                         if (temp2 > 42) {
     565     5902128 :                             if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
     566     3659151 :                                 accumNm += temp2;
     567     5902128 :                             if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
     568       10627 :                                 accumNml += temp2;
     569             :                         }
     570             :                     }
     571             :                 }
     572             :             }
     573       47790 :             prvpf += prvf_linesize;
     574       47790 :             prvnf += prvf_linesize;
     575       47790 :             srcpf += srcf_linesize;
     576       47790 :             srcf  += srcf_linesize;
     577       47790 :             srcnf += srcf_linesize;
     578       47790 :             nxtpf += nxtf_linesize;
     579       47790 :             nxtnf += nxtf_linesize;
     580       47790 :             mapp  += map_linesize;
     581             :         }
     582             :     }
     583             : 
     584         135 :     if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
     585           0 :         FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
     586           0 :         accumPm = accumPml;
     587           0 :         accumNm = accumNml;
     588             :     }
     589             : 
     590         135 :     norm1 = (int)((accumPc / 6.0f) + 0.5f);
     591         135 :     norm2 = (int)((accumNc / 6.0f) + 0.5f);
     592         135 :     mtn1  = (int)((accumPm / 6.0f) + 0.5f);
     593         135 :     mtn2  = (int)((accumNm / 6.0f) + 0.5f);
     594         135 :     c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
     595         135 :     c2 = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
     596         135 :     mr = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMAX(norm1,norm2),1));
     597         135 :     if (((mtn1 >=  500 || mtn2 >=  500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
     598          15 :         ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
     599          15 :         ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
     600          15 :         ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
     601         119 :         ret = mtn1 > mtn2 ? match2 : match1;
     602          16 :     else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
     603           0 :         ret = mtn1 > mtn2 ? match2 : match1;
     604             :     else
     605          16 :         ret = norm1 > norm2 ? match2 : match1;
     606         135 :     return ret;
     607             : }
     608             : 
     609         318 : static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
     610             :                         const AVFrame *src, int field)
     611             : {
     612             :     int plane;
     613        1272 :     for (plane = 0; plane < 4 && src->data[plane] && src->linesize[plane]; plane++) {
     614         954 :         const int plane_h = get_height(fm, src, plane);
     615         954 :         const int nb_copy_fields = (plane_h >> 1) + (field ? 0 : (plane_h & 1));
     616        2862 :         av_image_copy_plane(dst->data[plane] + field*dst->linesize[plane], dst->linesize[plane] << 1,
     617        1908 :                             src->data[plane] + field*src->linesize[plane], src->linesize[plane] << 1,
     618         954 :                             get_width(fm, src, plane) * fm->bpc, nb_copy_fields);
     619             :     }
     620         318 : }
     621             : 
     622         159 : static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
     623             :                                    const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
     624             : {
     625             :     AVFrame *dst;
     626         159 :     FieldMatchContext *fm = ctx->priv;
     627             : 
     628         159 :     if (match == mC) {
     629           0 :         dst = av_frame_clone(src);
     630             :     } else {
     631         159 :         AVFilterLink *outlink = ctx->outputs[0];
     632             : 
     633         159 :         dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
     634         159 :         if (!dst)
     635           0 :             return NULL;
     636         159 :         av_frame_copy_props(dst, src);
     637             : 
     638         159 :         switch (match) {
     639          49 :         case mP: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv,   field); break;
     640         110 :         case mN: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt,   field); break;
     641           0 :         case mB: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, prv, 1-field); break;
     642           0 :         case mU: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, nxt, 1-field); break;
     643           0 :         default: av_assert0(0);
     644             :         }
     645             :     }
     646         159 :     return dst;
     647             : }
     648             : 
     649         110 : static int checkmm(AVFilterContext *ctx, int *combs, int m1, int m2,
     650             :                    AVFrame **gen_frames, int field)
     651             : {
     652         110 :     const FieldMatchContext *fm = ctx->priv;
     653             : 
     654             : #define LOAD_COMB(mid) do {                                                     \
     655             :     if (combs[mid] < 0) {                                                       \
     656             :         if (!gen_frames[mid])                                                   \
     657             :             gen_frames[mid] = create_weave_frame(ctx, mid, field,               \
     658             :                                                  fm->prv, fm->src, fm->nxt);    \
     659             :         combs[mid] = calc_combed_score(fm, gen_frames[mid]);                    \
     660             :     }                                                                           \
     661             : } while (0)
     662             : 
     663         110 :     LOAD_COMB(m1);
     664         110 :     LOAD_COMB(m2);
     665             : 
     666         110 :     if ((combs[m2] * 3 < combs[m1] || (combs[m2] * 2 < combs[m1] && combs[m1] > fm->combpel)) &&
     667           0 :         abs(combs[m2] - combs[m1]) >= 30 && combs[m2] < fm->combpel)
     668           0 :         return m2;
     669             :     else
     670         110 :         return m1;
     671             : }
     672             : 
     673             : static const int fxo0m[] = { mP, mC, mN, mB, mU };
     674             : static const int fxo1m[] = { mN, mC, mP, mU, mB };
     675             : 
     676         140 : static int filter_frame(AVFilterLink *inlink, AVFrame *in)
     677             : {
     678         140 :     AVFilterContext *ctx  = inlink->dst;
     679         140 :     AVFilterLink *outlink = ctx->outputs[0];
     680         140 :     FieldMatchContext *fm = ctx->priv;
     681         140 :     int combs[] = { -1, -1, -1, -1, -1 };
     682         140 :     int order, field, i, match, sc = 0;
     683             :     const int *fxo;
     684         140 :     AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
     685             :     AVFrame *dst;
     686             : 
     687             :     /* update frames queue(s) */
     688             : #define SLIDING_FRAME_WINDOW(prv, src, nxt) do {                \
     689             :         if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
     690             :             av_frame_free(&prv);                                \
     691             :         prv = src;                                              \
     692             :         src = nxt;                                              \
     693             :         if (in)                                                 \
     694             :             nxt = in;                                           \
     695             :         if (!prv)                                               \
     696             :             prv = src;                                          \
     697             :         if (!prv) /* received only one frame at that point */   \
     698             :             return 0;                                           \
     699             :         av_assert0(prv && src && nxt);                          \
     700             : } while (0)
     701         140 :     if (FF_INLINK_IDX(inlink) == INPUT_MAIN) {
     702         140 :         av_assert0(fm->got_frame[INPUT_MAIN] == 0);
     703         140 :         SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
     704         135 :         fm->got_frame[INPUT_MAIN] = 1;
     705             :     } else {
     706           0 :         av_assert0(fm->got_frame[INPUT_CLEANSRC] == 0);
     707           0 :         SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
     708           0 :         fm->got_frame[INPUT_CLEANSRC] = 1;
     709             :     }
     710         135 :     if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
     711           0 :         return 0;
     712         135 :     fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0;
     713         135 :     in = fm->src;
     714             : 
     715             :     /* parity */
     716         135 :     order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1);
     717         135 :     field = fm->field != FM_PARITY_AUTO ? fm->field : order;
     718         135 :     av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
     719         135 :     fxo = field ^ order ? fxo1m : fxo0m;
     720             : 
     721             :     /* debug mode: we generate all the fields combinations and their associated
     722             :      * combed score. XXX: inject as frame metadata? */
     723         135 :     if (fm->combdbg) {
     724           0 :         for (i = 0; i < FF_ARRAY_ELEMS(combs); i++) {
     725           0 :             if (i > mN && fm->combdbg == COMBDBG_PCN)
     726           0 :                 break;
     727           0 :             gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt);
     728           0 :             if (!gen_frames[i])
     729           0 :                 return AVERROR(ENOMEM);
     730           0 :             combs[i] = calc_combed_score(fm, gen_frames[i]);
     731             :         }
     732           0 :         av_log(ctx, AV_LOG_INFO, "COMBS: %3d %3d %3d %3d %3d\n",
     733             :                combs[0], combs[1], combs[2], combs[3], combs[4]);
     734             :     } else {
     735         135 :         gen_frames[mC] = av_frame_clone(fm->src);
     736         135 :         if (!gen_frames[mC])
     737           0 :             return AVERROR(ENOMEM);
     738             :     }
     739             : 
     740             :     /* p/c selection and optional 3-way p/c/n matches */
     741         135 :     match = compare_fields(fm, fxo[mC], fxo[mP], field);
     742         135 :     if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
     743           0 :         match = compare_fields(fm, match, fxo[mN], field);
     744             : 
     745             :     /* scene change check */
     746         135 :     if (fm->combmatch == COMBMATCH_SC) {
     747         135 :         if (fm->lastn == outlink->frame_count_in - 1) {
     748          65 :             if (fm->lastscdiff > fm->scthresh)
     749          45 :                 sc = 1;
     750          70 :         } else if (luma_abs_diff(fm->prv, fm->src) > fm->scthresh) {
     751          20 :             sc = 1;
     752             :         }
     753             : 
     754         135 :         if (!sc) {
     755          70 :             fm->lastn = outlink->frame_count_in;
     756          70 :             fm->lastscdiff = luma_abs_diff(fm->src, fm->nxt);
     757          70 :             sc = fm->lastscdiff > fm->scthresh;
     758             :         }
     759             :     }
     760             : 
     761         135 :     if (fm->combmatch == COMBMATCH_FULL || (fm->combmatch == COMBMATCH_SC && sc)) {
     762         110 :         switch (fm->mode) {
     763             :         /* 2-way p/c matches */
     764           0 :         case MODE_PC:
     765           0 :             match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
     766           0 :             break;
     767         110 :         case MODE_PC_N:
     768         110 :             match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
     769         110 :             break;
     770           0 :         case MODE_PC_U:
     771           0 :             match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
     772           0 :             break;
     773           0 :         case MODE_PC_N_UB:
     774           0 :             match = checkmm(ctx, combs, match, fxo[mN], gen_frames, field);
     775           0 :             match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
     776           0 :             match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
     777           0 :             break;
     778             :         /* 3-way p/c/n matches */
     779           0 :         case MODE_PCN:
     780           0 :             match = checkmm(ctx, combs, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
     781           0 :             break;
     782           0 :         case MODE_PCN_UB:
     783           0 :             match = checkmm(ctx, combs, match, fxo[mU], gen_frames, field);
     784           0 :             match = checkmm(ctx, combs, match, fxo[mB], gen_frames, field);
     785           0 :             break;
     786           0 :         default:
     787           0 :             av_assert0(0);
     788             :         }
     789             :     }
     790             : 
     791             :     /* get output frame and drop the others */
     792         135 :     if (fm->ppsrc) {
     793             :         /* field matching was based on a filtered/post-processed input, we now
     794             :          * pick the untouched fields from the clean source */
     795           0 :         dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2);
     796             :     } else {
     797         135 :         if (!gen_frames[match]) { // XXX: is that possible?
     798           0 :             dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt);
     799             :         } else {
     800         135 :             dst = gen_frames[match];
     801         135 :             gen_frames[match] = NULL;
     802             :         }
     803             :     }
     804         135 :     if (!dst)
     805           0 :         return AVERROR(ENOMEM);
     806         810 :     for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
     807         675 :         av_frame_free(&gen_frames[i]);
     808             : 
     809             :     /* mark the frame we are unable to match properly as interlaced so a proper
     810             :      * de-interlacer can take the relay */
     811         135 :     dst->interlaced_frame = combs[match] >= fm->combpel;
     812         135 :     if (dst->interlaced_frame) {
     813         220 :         av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
     814         110 :                outlink->frame_count_in, av_ts2timestr(in->pts, &inlink->time_base));
     815         110 :         dst->top_field_first = field;
     816             :     }
     817             : 
     818         135 :     av_log(ctx, AV_LOG_DEBUG, "SC:%d | COMBS: %3d %3d %3d %3d %3d (combpel=%d)"
     819             :            " match=%d combed=%s\n", sc, combs[0], combs[1], combs[2], combs[3], combs[4],
     820         135 :            fm->combpel, match, dst->interlaced_frame ? "YES" : "NO");
     821             : 
     822         135 :     return ff_filter_frame(outlink, dst);
     823             : }
     824             : 
     825         240 : static int activate(AVFilterContext *ctx)
     826             : {
     827         240 :     FieldMatchContext *fm = ctx->priv;
     828         240 :     AVFrame *frame = NULL;
     829         240 :     int ret = 0, status;
     830             :     int64_t pts;
     831             : 
     832         480 :     if ((fm->got_frame[INPUT_MAIN] == 0) &&
     833         240 :         (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_MAIN], &frame)) > 0) {
     834         130 :         ret = filter_frame(ctx->inputs[INPUT_MAIN], frame);
     835         130 :         if (ret < 0)
     836           0 :             return ret;
     837             :     }
     838         240 :     if (ret < 0)
     839           0 :         return ret;
     840         240 :     if (fm->ppsrc &&
     841           0 :         (fm->got_frame[INPUT_CLEANSRC] == 0) &&
     842           0 :         (ret = ff_inlink_consume_frame(ctx->inputs[INPUT_CLEANSRC], &frame)) > 0) {
     843           0 :         ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], frame);
     844           0 :         if (ret < 0)
     845           0 :             return ret;
     846             :     }
     847         240 :     if (ret < 0) {
     848           0 :         return ret;
     849         240 :     } else if (ff_inlink_acknowledge_status(ctx->inputs[INPUT_MAIN], &status, &pts)) {
     850          10 :         if (status == AVERROR_EOF) { // flushing
     851          10 :             fm->eof |= 1 << INPUT_MAIN;
     852          10 :             ret = filter_frame(ctx->inputs[INPUT_MAIN], NULL);
     853             :         }
     854          10 :         ff_outlink_set_status(ctx->outputs[0], status, pts);
     855          10 :         return ret;
     856         230 :     } else if (fm->ppsrc && ff_inlink_acknowledge_status(ctx->inputs[INPUT_CLEANSRC], &status, &pts)) {
     857           0 :         if (status == AVERROR_EOF) { // flushing
     858           0 :             fm->eof |= 1 << INPUT_CLEANSRC;
     859           0 :             ret = filter_frame(ctx->inputs[INPUT_CLEANSRC], NULL);
     860             :         }
     861           0 :         ff_outlink_set_status(ctx->outputs[0], status, pts);
     862           0 :         return ret;
     863             :     } else {
     864         230 :         if (ff_outlink_frame_wanted(ctx->outputs[0])) {
     865         100 :             if (fm->got_frame[INPUT_MAIN] == 0)
     866         100 :                 ff_inlink_request_frame(ctx->inputs[INPUT_MAIN]);
     867         100 :             if (fm->ppsrc && (fm->got_frame[INPUT_CLEANSRC] == 0))
     868           0 :                 ff_inlink_request_frame(ctx->inputs[INPUT_CLEANSRC]);
     869             :         }
     870         230 :         return 0;
     871             :     }
     872             : }
     873             : 
     874           6 : static int query_formats(AVFilterContext *ctx)
     875             : {
     876           6 :     FieldMatchContext *fm = ctx->priv;
     877             : 
     878             :     static const enum AVPixelFormat pix_fmts[] = {
     879             :         AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
     880             :         AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,
     881             :         AV_PIX_FMT_NONE
     882             :     };
     883             :     static const enum AVPixelFormat unproc_pix_fmts[] = {
     884             :         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
     885             :         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
     886             :         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
     887             :         AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
     888             :         AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
     889             :         AV_PIX_FMT_YUVJ411P,
     890             :         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
     891             :         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
     892             :         AV_PIX_FMT_YUV440P10,
     893             :         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
     894             :         AV_PIX_FMT_YUV440P12,
     895             :         AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
     896             :         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
     897             :         AV_PIX_FMT_NONE
     898             :     };
     899             :     int ret;
     900             : 
     901           6 :     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
     902           6 :     if (!fmts_list)
     903           0 :         return AVERROR(ENOMEM);
     904           6 :     if (!fm->ppsrc) {
     905           6 :         return ff_set_common_formats(ctx, fmts_list);
     906             :     }
     907             : 
     908           0 :     if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_MAIN]->out_formats)) < 0)
     909           0 :         return ret;
     910           0 :     fmts_list = ff_make_format_list(unproc_pix_fmts);
     911           0 :     if (!fmts_list)
     912           0 :         return AVERROR(ENOMEM);
     913           0 :     if ((ret = ff_formats_ref(fmts_list, &ctx->outputs[0]->in_formats)) < 0)
     914           0 :         return ret;
     915           0 :     if ((ret = ff_formats_ref(fmts_list, &ctx->inputs[INPUT_CLEANSRC]->out_formats)) < 0)
     916           0 :         return ret;
     917           0 :     return 0;
     918             : }
     919             : 
     920           5 : static int config_input(AVFilterLink *inlink)
     921             : {
     922             :     int ret;
     923           5 :     AVFilterContext *ctx = inlink->dst;
     924           5 :     FieldMatchContext *fm = ctx->priv;
     925           5 :     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
     926           5 :     const int w = inlink->w;
     927           5 :     const int h = inlink->h;
     928             : 
     929           5 :     fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh_flt) / 100.0);
     930             : 
     931          10 :     if ((ret = av_image_alloc(fm->map_data,   fm->map_linesize,   w, h, inlink->format, 32)) < 0 ||
     932           5 :         (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
     933           0 :         return ret;
     934             : 
     935           5 :     fm->hsub = pix_desc->log2_chroma_w;
     936           5 :     fm->vsub = pix_desc->log2_chroma_h;
     937             : 
     938           5 :     fm->tpitchy  = FFALIGN(w,      16);
     939           5 :     fm->tpitchuv = FFALIGN(w >> 1, 16);
     940             : 
     941           5 :     fm->tbuffer = av_malloc(h/2 * fm->tpitchy);
     942          10 :     fm->c_array = av_malloc((((w + fm->blockx/2)/fm->blockx)+1) *
     943           5 :                             (((h + fm->blocky/2)/fm->blocky)+1) *
     944             :                             4 * sizeof(*fm->c_array));
     945           5 :     if (!fm->tbuffer || !fm->c_array)
     946           0 :         return AVERROR(ENOMEM);
     947             : 
     948           5 :     return 0;
     949             : }
     950             : 
     951           6 : static av_cold int fieldmatch_init(AVFilterContext *ctx)
     952             : {
     953           6 :     const FieldMatchContext *fm = ctx->priv;
     954          12 :     AVFilterPad pad = {
     955           6 :         .name         = av_strdup("main"),
     956             :         .type         = AVMEDIA_TYPE_VIDEO,
     957             :         .config_props = config_input,
     958             :     };
     959             :     int ret;
     960             : 
     961           6 :     if (!pad.name)
     962           0 :         return AVERROR(ENOMEM);
     963           6 :     if ((ret = ff_insert_inpad(ctx, INPUT_MAIN, &pad)) < 0) {
     964           0 :         av_freep(&pad.name);
     965           0 :         return ret;
     966             :     }
     967             : 
     968           6 :     if (fm->ppsrc) {
     969           0 :         pad.name = av_strdup("clean_src");
     970           0 :         pad.config_props = NULL;
     971           0 :         if (!pad.name)
     972           0 :             return AVERROR(ENOMEM);
     973           0 :         if ((ret = ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad)) < 0) {
     974           0 :             av_freep(&pad.name);
     975           0 :             return ret;
     976             :         }
     977             :     }
     978             : 
     979          12 :     if ((fm->blockx & (fm->blockx - 1)) ||
     980           6 :         (fm->blocky & (fm->blocky - 1))) {
     981           0 :         av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
     982           0 :         return AVERROR(EINVAL);
     983             :     }
     984             : 
     985           6 :     if (fm->combpel > fm->blockx * fm->blocky) {
     986           0 :         av_log(ctx, AV_LOG_ERROR, "Combed pixel should not be larger than blockx x blocky\n");
     987           0 :         return AVERROR(EINVAL);
     988             :     }
     989             : 
     990           6 :     return 0;
     991             : }
     992             : 
     993           6 : static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
     994             : {
     995             :     int i;
     996           6 :     FieldMatchContext *fm = ctx->priv;
     997             : 
     998           6 :     if (fm->prv != fm->src)
     999           0 :         av_frame_free(&fm->prv);
    1000           6 :     if (fm->nxt != fm->src)
    1001           0 :         av_frame_free(&fm->nxt);
    1002           6 :     if (fm->prv2 != fm->src2)
    1003           0 :         av_frame_free(&fm->prv2);
    1004           6 :     if (fm->nxt2 != fm->src2)
    1005           0 :         av_frame_free(&fm->nxt2);
    1006           6 :     av_frame_free(&fm->src);
    1007           6 :     av_frame_free(&fm->src2);
    1008           6 :     av_freep(&fm->map_data[0]);
    1009           6 :     av_freep(&fm->cmask_data[0]);
    1010           6 :     av_freep(&fm->tbuffer);
    1011           6 :     av_freep(&fm->c_array);
    1012          12 :     for (i = 0; i < ctx->nb_inputs; i++)
    1013           6 :         av_freep(&ctx->input_pads[i].name);
    1014           6 : }
    1015             : 
    1016           5 : static int config_output(AVFilterLink *outlink)
    1017             : {
    1018           5 :     AVFilterContext *ctx  = outlink->src;
    1019           5 :     FieldMatchContext *fm = ctx->priv;
    1020           5 :     const AVFilterLink *inlink =
    1021           5 :         ctx->inputs[fm->ppsrc ? INPUT_CLEANSRC : INPUT_MAIN];
    1022           5 :     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
    1023             : 
    1024           5 :     fm->bpc = (desc->comp[0].depth + 7) / 8;
    1025           5 :     outlink->time_base = inlink->time_base;
    1026           5 :     outlink->sample_aspect_ratio = inlink->sample_aspect_ratio;
    1027           5 :     outlink->frame_rate = inlink->frame_rate;
    1028           5 :     outlink->w = inlink->w;
    1029           5 :     outlink->h = inlink->h;
    1030           5 :     return 0;
    1031             : }
    1032             : 
    1033             : static const AVFilterPad fieldmatch_outputs[] = {
    1034             :     {
    1035             :         .name          = "default",
    1036             :         .type          = AVMEDIA_TYPE_VIDEO,
    1037             :         .config_props  = config_output,
    1038             :     },
    1039             :     { NULL }
    1040             : };
    1041             : 
    1042             : AVFilter ff_vf_fieldmatch = {
    1043             :     .name           = "fieldmatch",
    1044             :     .description    = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine."),
    1045             :     .query_formats  = query_formats,
    1046             :     .priv_size      = sizeof(FieldMatchContext),
    1047             :     .init           = fieldmatch_init,
    1048             :     .activate       = activate,
    1049             :     .uninit         = fieldmatch_uninit,
    1050             :     .inputs         = NULL,
    1051             :     .outputs        = fieldmatch_outputs,
    1052             :     .priv_class     = &fieldmatch_class,
    1053             :     .flags          = AVFILTER_FLAG_DYNAMIC_INPUTS,
    1054             : };

Generated by: LCOV version 1.13