Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 27th July 2003, 09:04   #1  |  Link
stickboy
AviSynth Enthusiast
 
Join Date: Jul 2002
Location: California, U.S.
Posts: 1,267
Caveats of using Trim in functions

I was looking through some of the shared functions, and I saw a few cases where Trim isn't used very carefully. IMO, Trim's syntax--intended for editing convenience--is tricky to use robustly for general-purpose, user-defined functions.

For example, let's say we wanted to write our own version of ApplyRange. Here's the naive approach:
Code:
# ApplyRange2
#
#   A version of ApplyRange that works with Avisynth 2.0x and that allows
#   named arguments and filters that take no arguments.
#
# USAGE:
#   c.ApplyRange2(123, 456, "Tweak(sat=1.1)")
#
function ApplyRange2(clip c, int start, int end, string thunk)
{
    # NOTE: this version isn't very safe!

    seg1 = c.Trim(0, start - 1)
    seg2 = c.Trim(start, end) # filter will be applied to frames in the
                              # range [start, end]
    seg3 = c.Trim(end + 1, 0)

    seg2 = Eval("seg2." + thunk)
    return seg1 + seg2 + seg3
}
At first glance, there doesn't appear to be anything wrong. But wait! What happens if:
  • start == 0
  • start == 1
  • start == c.FrameCount() - 1
  • start == c.FrameCount()
  • start > c.FrameCount()
  • end < 0
  • end == 0
  • end == c.FrameCount() - 1
  • end == c.FrameCount()
  • end > c.FrameCount()
Not all of those cases necessarily lead to unwanted behavior, and some of them don't make sense in the context of ApplyRange. Nevertheless, they still are things you generally need to watch out for when using Trim. Ugh! Who wants to waste time thinking about all that?

(I'm not sure why, but c.Trim(c.FrameCount() + N, 0) returns the last frame of c instead of an error or instead of a 0-frame clip.)

Okay, so what can we do? We can simplify Trim a lot by:
  • using the interval [start, end) instead of [start, end]
  • disallowing end=0
  • disallowing out-of-bounds start/end values
  • allowing generation of null (0 frame) clips
Here are a couple versions of Trim that I use in my functions:

[Edit: code moved to <http://www.avisynth.org/stickboy/>]

Now we safely can define ApplyRange2 as:
Code:
function ApplyRange2(clip c, int start, int end, string thunk)
{
    seg1 = c.Trim2(0, start)
    seg2 = c.Trim2(start, end + 1) # filter will be applied to frames in the
                                   # range [start, end]
    seg3 = c.Trim2(end + 1)

    seg2 = Eval("seg2." + thunk)
    return seg1 + seg2 + seg3
}
Edit (2003-08-13):
Greatly relaxed the bounds-checking for Trim2; now allows start/end values that go past the end of the clip.

Edit: (2003-08-20):
Oops, I broke Trim2's support for negative values of end. Should be fixed now, hopefully without breaking anything else.

Last edited by stickboy; 27th November 2003 at 01:47.
stickboy is offline   Reply With Quote
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 00:16.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.