Dup

Abstract

author: Donald A. Graft
version: 2.20 beta 1
download: http://neuron2.net/mine.html
category: Misc Plugins
requirements:  YV12 & YUY2 Colorspace

Description

This plugin for Avisynth implements a robust duplicate frame detector. It was initially created as a test bed for a new frame differencing algorithm, but to make it a useful filter in its own right, copy and blend features have been implemented, whereby a string of frames that are all within threshold difference of the first frame of the string will be replaced with a string of frames all identical to the last frame of the source string. The last frame is used instead of the first because often the first frame after a scene change has more blocking artifacts, etc. If the blend option is enabled, the replacement frame will be generated from a blending of all the duplicates in the string (useful for noise reduction).

This is intended for use in clips that have a significant number of duplicate content frames, but which differ due to noise. Typically anime has many such duplicates. By replacing noisy duplicates with exact duplicates, a bitrate reduction can be achieved. It's a free lunch! ('Marc FD' first implemented this copying functionality.) Additionally, if the blend option is enabled, a significant noise reduction can be achieved for duplicated frames.

Some duplicate detectors are incapable of adapting to changes in the luminance levels of the various scenes in a clip, and they are badly affected by noise. They have delicate absolute thresholds that must be set by the user. This filter attempts to solve all of these problems.

Dup uses a small window that it scans over the frame, so it is capable of seeing very small changes. The effect of noise is reduced by means of automatic cancellation through a summation process. The frame difference is normalized to the range 0-100% to make setting the threshold more intuitive.

The filter accepts both YUY2 and YV12 input. It is not yet optimized for speed. Optimizations will be included in a future version.

This version of Dup requires Avisynth 2.5 and beyond. There is a legacy version of Dup for use with Avisynth 2.0x versions.


Dup Function Syntax

Dup uses named parameters. That means you do not have to worry about the ordering of parameters and can simply refer to them by name and put them in any order in the list of parameters. If you omit a parameter it takes its default value. For example, if you want to run Dup with a duplication declaration level of 5% and with debug enabled, you can simply say:

Dup(threshold=5,debug=true)

Any combination and order of named parameters is allowed. Remember, however, that you should always include empty parentheses if you are not specifying any parameters.


Changing Default Parameter Values

If you do not like the defaults as documented below, you can set your own standard defaults. To override the defaults, first create an Avisynth plugins directory and register it. You register it by putting the following lines in a text file called 'plugin.reg', changing the path as appropriate, and then right clicking on the file's icon and selecting Merge.
REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Avisynth]
"plugindir2_5"="d:\\avisynthplugins"

Next, create defaults files as required in the plugins directory. For example, to set the default blend=true for Dup(), make a file called Dup.def and put this line in it:

blend=true

You can list as many parameter assignments as you like, one per line. Those not specified assume the values given below. Of course, you can always override the defaults in your scripts when you invoke the functions. NOTE: The lines in the defaults file must not contain any spaces or tabs.


Following is the syntax for Dup (replace parameter_list with your comma-separated list of named parameters):

Dup(parameter_list)

threshold (0.0-100.0, default 3.0): This parameter defines the percentage change in the most different 32x32-pixel window that is enough to declare a frame a duplicate. The way to think about it is that if the threshold is (say) 7%, then if any 32x32 pixel area changes by 7% or more, it is not a duplicate, otherwise it is. Use the show option to display frame difference values to help in setting the threshold if you prefer something other than the default.

You should always set your threshold above the noise level of the clip. The amount above will depend on how many duplicates you want to generate, i.e., the desired bitrate reduction. To determine the noise level, examine the frames and look for those having duplicate content, albeit with noise differences. You may find that frames that are content duplicates show a metric of as high as a few percent (due to noise). Determine the highest metric that you get for the content duplicates. Then set your threshold above that level with as much margin as you want to achieve the desired bitrate reduction.

chroma (true/false, default true): Set chroma=false to exclude chroma from the frame differencing.

show (true/false, default false): When set to true, information is overlayed onto the upper left of the frame. When copy=false, the 32x32 pixel area that is most different from the same area in the next frame is delineated with a white box, and the difference metric is displayed. If the box has an X through it, the frame was declared a duplicate according to the configured difference percentage (threshold). When copy=true, the box is not displayed. The text information will tell you which frames are duplicated and with which source frame.

copy (true/false, default true): If this option is set to true, declared duplicates will be replaced with a single copy. If there is a string of duplicates, the last frame in the string is used for all copies. If blend=true, the copy frame will be generated by blending together all the duplicates in the string.

maxcopies (1-20, default 20): Determines the maximum number of copies that can be emitted due to duplicate declaration. However, if the input stream has more than the maximum number of duplicates in a string, then, of course, they will all be output. This limit just forces the current duplicate sequence to end; the comparisons restart with the nextframe frame.

blend (true/false, default false): If this option is set to true, the copy behavior will be modified such that instead of using the last frame in the string of duplicates, a frame will be created and used from a blend of all the duplicates in the string. This is useful for noise reduction. This option requires copy=true. Also, note that the blend frame is generated when the first frame in the string is requested. So random access into the middle of a string will not invoke blending. Play the clip straight through from a starting point for correct operation.

debug (true/false, default false): This parameter enables debug output to the DebugView utility. This utility can be downloaded from my web site as given below.

$Date: 2005/07/27 17:51:01 $