#
# InterpolateFrames2() function...
#
# FrameNumber is number of the 1-st frame in Source that needs replacing. 
# FrameCount  is total number of frames to replace.
# InterpolateFrames2(101, 5) would replace 101, 102, 103, 104, 105,
# by using MRecalculate() and MFlowInter() interpolation.
#
function InterpolateFrames2(clip clp, int FrameNumber, int FrameCount)
{
  # Constants
  # MSuper()
  #
  hpad          = 8           # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  vpad          = 8           # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  pel           = 4           # default=2
  sharp         = 2           # default=2 !!!
  rfilter       = 4           # default=2
  chroma        = true        # default=true
  isse          = true        # default=true
  planar        = false       # default=false
  levels        = 0           # default=0

  # Constants
  # MAnalyse()
  #
  blkh          = 16          # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  blkv          = 16          # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  overlap       = 4           # Maybe 2, 4,  8, 16,  !!! maybe blkh/2 or litle  !!! Change this constant for best tuning !!!
  overlapV      = 4           # Maybe 2, 4,  8, 16,  !!! maybe blkv/2 or litle  !!! Change this constant for best tuning !!!
  search        = 3           # default=4  !!! DO NOT CHANGE =3
  searchparam   = 16          # default=2  !!! DO NOT CHANGE =16
  dct           = 5           # default=0
  plevel        = 2           # default=0
  badrange      = (-24)       # default=24
  badsad        = 10000       # default=10000
  divide        = 0           # default=0
  sadx264       = 0           # default=0
  truemotion    = true        # default=true

  lambda        = (truemotion == false) ? 0   : (1000 * blkh * blkv / 64)   # default=0   truemotion=false, default=1000*blksize*blksizeV/64 truemotion=true
  lsad          = (truemotion == false) ? 400 : 1200                        # default=400 truemotion=false, default=1200 truemotion=true
  pnew          = (truemotion == false) ? 0   : 50                          # default=0   truemotion=false, default=50   truemotion=true
  pzero         = pnew                                                      # default pzero=pnew

  # Constants
  # MRecalculate()
  #
  blkhR         = 8           # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  blkvR         = 8           # Maybe 4, 8, 16, 32,  !!! Change this constant for best tuning !!!
  overlapR      = 4           # Maybe 2, 4,  8, 16,  !!! maybe blkhR/2 or litle  !!! Change this constant for best tuning !!!
  overlapVR     = 4           # Maybe 2, 4,  8, 16,  !!! maybe blkvR/2 or litle  !!! Change this constant for best tuning !!!
  searchR       = 3           # default=4
  searchparamR  = 1           # default=2
  thSAD         = 400         # default=200  !!! DO NOT CHANGE =400

  lambdaR       = (truemotion == false) ? 0 : (1000 * blkhR * blkvR / 64)   # default=0 truemotion=false, default=1000*blksize*blksizeV/64 truemotion=true

  # Constants
  # MFlowInter()
  #
  thSCD1        = 800         # default=400  !!! DO NOT CHANGE =800
  thSCD2        = 130         # default=130
  iml           = 70          # default=100
  blend         = true        # default=true


  # Here is code of function...
  #
  # Restore bad frames with interpolation with MFlowInter
  # Prepare functions
  #
  super = MSuper(clp,                 \
                 hpad    = hpad,      \
                 vpad    = vpad,      \
                 pel     = pel,       \
                 chroma  = chroma,    \
                 sharp   = sharp,     \
                 rfilter = rfilter,   \
                 isse    = isse,      \
                 planar  = planar,    \
                 levels  = levels)

  backward1 = MAnalyse(super,                              \
                       delta       = (FrameCount + 1),     \
                       isb         = true,                 \
                       blksize     = blkh,                 \
                       blksizeV    = blkv,                 \
                       overlap     = overlap,              \
                       overlapV    = overlapV,             \
                       search      = search,               \
                       searchparam = searchparam,          \
                       pelsearch   = pel,                  \
                       lambda      = lambda,               \
                       chroma      = chroma,               \
                       truemotion  = truemotion,           \ 
                       lsad        = lsad,                 \
                       plevel      = plevel,               \
                       pnew        = pnew,                 \
                       pzero       = pzero,                \
                       dct         = dct,                  \
                       divide      = divide,               \
                       sadx264     = sadx264,              \
                       badSAD      = badsad,               \
                       badrange    = badrange,             \
                       isse        = isse,                 \
                       levels      = levels,               \
                       global      = true,                 \
                       pglobal     = 0,                    \
                       meander     = true,                 \
                       temporal    = false,                \
                       trymany     = false)

  forward1 = MAnalyse(super,                               \
                      delta       = (FrameCount + 1),      \
                      isb         = false,                 \
                      blksize     = blkh,                  \
                      blksizeV    = blkv,                  \
                      overlap     = overlap,               \
                      overlapV    = overlapV,              \
                      search      = search,                \
                      searchparam = searchparam,           \
                      pelsearch   = pel,                   \
                      lambda      = lambda,                \
                      chroma      = chroma,                \
                      truemotion  = truemotion,            \ 
                      lsad        = lsad,                  \
                      plevel      = plevel,                \
                      pnew        = pnew,                  \
                      pzero       = pzero,                 \
                      dct         = dct,                   \
                      divide      = divide,                \
                      sadx264     = sadx264,               \
                      badSAD      = badsad,                \
                      badrange    = badrange,              \
                      isse        = isse,                  \
                      levels      = levels,                \
                      global      = true,                  \
                      pglobal     = 0,                     \
                      meander     = true,                  \
                      temporal    = false,                 \
                      trymany     = false)

  backward2 = MRecalculate(super,                           \
                           backward1,                       \
                           blksize     = blkhR,             \
                           blksizeV    = blkvR,             \
                           overlap     = overlapR,          \
                           overlapV    = overlapVR,         \
                           search      = searchR,           \
                           searchparam = searchparamR,      \
                           lambda      = lambdaR,           \
                           dct         = dct,               \
                           truemotion  = truemotion,        \
                           thSAD       = thSAD,             \
                           isse        = isse,              \
                           chroma      = chroma,            \
                           pnew        = pnew,              \
                           divide      = divide,            \
                           sadx264     = sadx264,           \
                           smooth      = 1)
  
  forward2  = MRecalculate(super,                           \
                           forward1,                        \
                           blksize     = blkhR,             \
                           blksizeV    = blkvR,             \
                           overlap     = overlapR,          \
                           overlapV    = overlapVR,         \
                           search      = searchR,           \
                           searchparam = searchparamR,      \
                           lambda      = lambdaR,           \
                           dct         = dct,               \
                           truemotion  = truemotion,        \
                           thSAD       = thSAD,             \
                           isse        = isse,              \
                           chroma      = chroma,            \
                           pnew        = pnew,              \
                           divide      = divide,            \
                           sadx264     = sadx264,           \
                           smooth      = 1)

  GScript("""
  if (FrameCount == 1) {

    inter2 = MFlowInter(clp,                 \
                        super,               \
                        backward2,           \
                        forward2,            \
                        time   = 50,         \
                        mL     = iml,        \
                        blend  = blend,      \
                        thSCD1 = thSCD1,     \
                        thSCD2 = thSCD2,     \
                        isse   = isse,       \
                        planar = planar)

    inter = inter2.Trim(FrameNumber, -1)
  }        # End of If-Then
  
  else {
    # Fill an Inter variable with the MFlowInter() clips with different times
    # Times are calculated in each step
    for (i=0, FrameCount-1) {
      itime = (100.0 / Float(FrameCount + 1)) * Float(i + 1)

      inter2 = MFlowInter(clp,                 \
                          super,               \
                          backward2,           \
                          forward2,            \
                          time   = itime,      \
                          mL     = iml,        \
                          blend  = blend,      \
                          thSCD1 = thSCD1,     \
                          thSCD2 = thSCD2,     \
                          isse   = isse,       \
                          planar = planar)

      if (i == 0) {
        inter = inter2.Trim(FrameNumber, -1)
      }    # End of If-Then
      else {
        inter = inter ++ inter2.Trim(FrameNumber, -1)
      }    # End of If-Else
    }      # End of For ()
  }        # End of If-Else
  """)     # End of GScript ()

  return (clp.trim(0, FrameNumber - 1) ++ inter ++ clp.trim(FrameNumber + FrameCount, 0))
}