Wrapper

From Avisynth wiki
Jump to: navigation, search

Even if you know little to nothing about programming, you can write your own AviSynth functions to simplify your scripts.

Suppose that you wished that some of the parameters to your favorite filter were optional or that some of its parameters had different default values.

For example, consider the Levels filter:

   Levels(clip c,
          int input_low, float gamma, int input_high,
          int output_low, int output_high)

I rarely adjust output_low or output_high. It might be convenient if they were optional and had default values of 0 and 255, respectively. We can write a wrapper function for it:

   # MyLevels
   #
   #   A wrapper function to Levels with named, optional parameters.
   #
   # PARAMETERS:
   #   input_low   - (default: 0)
   #   gamma       - (default: 1.0)
   #   input_high  - (default: 255)
   #   output_low  - (default: 0)
   #   output_high - (default: 255)
   #
   # USAGE:
   #   MyLevels(0, 1.2, 255, 0, 255)
   #   MyLevels(0, 1.2, 255)
   #   MyLevels(gamma=1.2)
   #
   function MyLevels(clip c,
   \                 int "input_low", float "gamma", int "input_high",
   \                 int "output_low", int "output_high")
   {
       return c.Levels(Default(input_low, 0),
       \               Default(gamma, 1.0),
       \               Default(input_high, 255),
       \               Default(output_low, 0),
       \               Default(output_high, 255))
   }

As another example, suppose you would like your favorite filter to have presets like [Convolution3D]. Consider TemporalSoften:

   TemporalSoften(clip c, int radius,
                  int luma_threshold, int chroma_threshold,
                  int "scenechange", int "mode")

I can never remember what combination of values work well together, so I'd prefer to supply presets:

   # TemporalSoftenPreset
   #
   #   A wrapper function to TemporalSoften to specify presets.
   #
   # PARAMETERS:
   #   preset - the preset name:
   #              { "default", "sh0dan-soft",
   #                "sh0dan-medium", "sh0dan-heavy" }
   #            (default: "default")
   #
   # USAGE:
   #   TemporalSoftenPreset(preset="default")
   #
   function TemporalSoftenPreset(clip c, string "preset")
   {
       preset = Default(preset, "default")
       s = SelectByString(preset,
       \                  "default",       "c.TemporalSoften(4, 4, 8, 15, 2)",
       \                  "sh0dan-soft",   "c.TemporalSoften(2, 3, 3,  6, 2)",
       \                  "sh0dan-medium", "c.TemporalSoften(3, 5, 5, 10, 2)",
       \                  "sh0dan-heavy",  "c.TemporalSoften(4, 8, 8, 10, 2)",
       \                  else="")

       Assert(s != "", "TemporalSoftenPreset: invalid preset")

       return Eval(s)
   }

[NOTE: The above function uses my SelectByString AviSynth plugin.] Advanced

Presets are nice, but what if we wanted to use presets and normal arguments? Ideally we'd want to use the preset's default values and then to override some of those values with the normal parameters.

Combining principles from the two examples above, we have:

   # TemporalSoftenPreset2
   #
   #   A wrapper function to TemporalSoften to specify presets.
   #
   # PARAMETERS:
   #   radius
   #   luma_threshold
   #   chroma_threshold
   #   scenechange
   #   mode
   #   preset           - the preset name:
   #                        { "default", "sh0dan-soft",
   #                          "sh0dan-medium", "sh0dan-heavy" }
   #                      (default: "default")
   #
   # USAGE:
   #   TemporalSoftenPreset2(4, 4, 8, 15, 12)
   #   TemporalSoftenPreset2(preset="default")
   #   TemporalSoftenPreset2(preset="default", radius=2)
   #
   function TemporalSoftenPreset2(clip c, int "radius",
   \                              int "luma_threshold",
   \                              int "chroma_threshold",
   \                              int "scenechange", int "mode",
   \                              string "preset")
   {
       preset = Default(preset, "default")

       # We can get Eval to assign multiple variables at once
       # by putting linebreaks in our strings.  Evil!
       s = SelectByString(preset,
       \                  "default",       "r  =  4
                                            L  =  4
                                            ch =  8
                                            sc = 15
                                            m  =  2",
       \                  "sh0dan-soft",   "r  =  2
                                            L  =  3
                                            ch =  3
                                            sc =  6
                                            m  =  2",
       \                  "sh0dan-medium", "r  =  3
                                            L  =  5
                                            ch =  5
                                            sc = 10
                                            m  =  2",
       \                  "sh0dan-heavy",  "r  =  4
                                            L  =  8
                                            ch =  8
                                            sc = 10
                                            m  =  2",
       \                  else="")

       Assert(s != "", "TemporalSoftenPreset2: invalid preset")

       Eval(s)
           
       radius = Default(radius, r)
       luma_threshold = Default(luma_threshold, L)
       chroma_threshold = Default(chroma_threshold, ch)
       scenechange = Default(scenechange, sc)
       mode = Default(mode, m)

       return c.TemporalSoften(radius, luma_threshold, chroma_threshold, 
       \                       scenechange, mode)
   }

-- [stickboy]

Personal tools