PDA

View Full Version : Aug 20 Build and SemiAutoCropAndResize


DDogg
21st August 2003, 14:45
The Semi-Auto Script of gerti67's for DVD2SVCD will no longer work with the August 20 build.

This script:
mpeg2source("C:\PROGRA~1\DVD2SVCD\TEMP\preview.d2v")
DetectedTop=6 DetectedHeight=468
BlocksToCropHorizontal=2 BlocksToCropVertical=2 BlockOptimization=16
ResizeMethod=3 Bicubic_b_Value=0.0 Bicubic_c_Value=0.55
GammaCorrection=1.0 Hue=0 Saturation=1.0 Brightness=5.0 Contrast=1.0
DebugMode=0
DestinationWidth=480
DestinationHeight=360
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\SimpleResize.dll")
Import("C:\Program Files\AviSynth 2.5\plugins\autocrop\SemiAutoCropAndResize.avs")
Which imports the script below fails at the section titled "#### Output the clip
correctly cropped and resized and with borders added". If you Comment out the
resizing line in the script below that is specified in the script above,
the script will play.
#### SemiAutoCropAndResize Script - v1.05 - by gerti67

#### Parameter definitions and (useful) ranges

# -- Used internally by the script

# -- DestinationSystemHeight : {480, 576}
# -- DestinationSubSystemHeight : {240, 288}
# -- DestinationWidth : {352, 480, 720}
# -- DestinationHeight : {180, 216, 240, 288, 360, 432, 480, 576}
# -- HorizontalFactor : {0.500000, 0.666666, 1.000000}
# -- VerticalFactor : {0.375000, 0.500000, 0.750000, 1.000000}
# -- Correct704Factor : {0.977777, 1.000000}

# -- Editable by user via DVD2SVCD's "Frameserver" tab

# -- DetectedTop : {0 ..}
# -- DetectedHeight . {0 ..}
# -- BlocksToCropHorizontal : {0, 1, 2}
# -- BlocksToCropVertical : {0, 1, 2}
# -- BlockOptimization : {1, 2, 4, 8, 16}
# -- ResizeMethod : {1, 2, 3, 4}
# -- Bicubic_b_Value : {0.00 .. 1.00}
# -- Bicubic_c_Value : {0.00 .. 1.00}
# -- GammaCorrection : {0.50 .. 1.50}
# -- Hue : {-180 .. 180}
# -- Saturation : {0.0 .. 10.0}
# -- Brightness : {-255 .. 255}
# -- Contrast : {0.0 .. 10.0}
# -- DebugMode : {0, 1, 2}

#### Calculate some important base parameters

DestinationSystemHeight = (DestinationHeight % 5 > 0) ? 576 : 480
DestinationSubSystemHeight = (DestinationHeight / 360. < 1) ? DestinationSystemHeight / 2 : DestinationSystemHeight
HorizontalFactor = (DestinationWidth < 480) ? 0.5 : DestinationWidth / 720.
VerticalFactor = DestinationHeight / DestinationSystemHeight * 1.
Correct704Factor = (DestinationWidth == 352) ? 0.977777 : 1.

#### Correct DetectedHeight according to BlocksToCrop value if necessary

CropTop = (DetectedTop < BlocksToCropVertical * 8) ? BlocksToCropVertical * 8 : Round(DetectedTop / 2.) * 2

DetectedHeight = (DetectedTop < CropTop) ? DetectedHeight - CropTop + DetectedTop : DetectedHeight
DetectedHeight = ((CropTop + DetectedHeight) > (DestinationSystemHeight - BlocksToCropVertical * 8)) ? DetectedHeight - (CropTop + DetectedHeight - DestinationSystemHeight + BlocksToCropVertical * 8) : DetectedHeight

#### Calculate rest of the crop values and make sure they're YUV12 aware

CropLeft = Round((BlocksToCropHorizontal * 4.) / (HorizontalFactor * 1.)) * 2
CropWidth = Round((Width - 2 * CropLeft) / 2.) * 2
CropHeight = Round(DetectedHeight / 2.) * 2

#### Calculate destination width and height and make sure they're YUV12 aware

NewDestinationWidth = Round((DestinationWidth - BlocksToCropHorizontal * 16) / 4.) * 4
NewDestinationHeight = Round(CropHeight * VerticalFactor * Correct704Factor) - Round(CropHeight * VerticalFactor * Correct704Factor) % 2

#### If going for VCD then we have to do some backward calculations to get the desired minimal borders due to VCD resolution based on 704x480/576

VCDCorrection = ((DestinationSubSystemHeight - NewDestinationHeight) < (BlocksToCropVertical * 16)) ? 1 : 0
NewDestinationHeight = ((DestinationSubSystemHeight <= 288) && (VCDCorrection == 1)) ? DestinationSubSystemHeight - BlocksToCropVertical * 16 : NewDestinationHeight
CropHeight = ((DestinationSubSystemHeight <= 288) && (VCDCorrection == 1)) ? Round(NewDestinationHeight * 1.022727) * 2 : CropHeight
CropTop = ((DestinationSubSystemHeight <= 288) && (VCDCorrection == 1)) ? Round((DestinationSystemHeight - CropHeight) / 4.) * 2 : CropTop

#### Calculate borders to reach the system destination size

BorderLeft = Round((DestinationWidth - NewDestinationWidth) / 4.) * 2
BorderRight = DestinationWidth - NewDestinationWidth - BorderLeft

#### Optimize TopBorder to be aligned on desired multiples of BlockOptimization

BorderTop = Round((DestinationSubSystemHeight - NewDestinationHeight) / (2. * BlockOptimization)) * BlockOptimization
BorderBottom = DestinationSubSystemHeight - NewDestinationHeight - BorderTop

#### Output the clip correctly cropped and resized and with borders added

Crop(CropLeft, CropTop, CropWidth, CropHeight)

#######***************************************************
#######***************************************************
#######************* SCRIPT FAILS HERE *******************

(ResizeMethod == 1) ? BilinearResize(NewDestinationWidth, NewDestinationHeight) : NOP()
(ResizeMethod == 2) ? BicubicResize(NewDestinationWidth, NewDestinationHeight, Bicubic_b_Value, Bicubic_c_Value) : NOP()
(ResizeMethod == 3) ? LanczosResize(NewDestinationWidth, NewDestinationHeight) : NOP()
(ResizeMethod == 4) ? SimpleResize(NewDestinationWidth, NewDestinationHeight) : NOP()

######*****************************************************
#######****************************************************
#######****************************************************

((Hue <> 0) || (Saturation <> 1) || (Brightness <> 0) || (Contrast <> 1)) ? Tweak(Hue, Saturation, Brightness, Contrast) : NOP()
(GammaCorrection <> 1) ? Levels(0, GammaCorrection, 255, 0, 255) : NOP()

Last=AddBorders(BorderLeft, BorderTop, BorderRight, BorderBottom)

#### Output debug values

(DebugMode > 0) ? Subtitle("Crop", 20, 20) : NOP()
(DebugMode > 0) ? Subtitle(": (" + String(CropLeft) + "," + String(CropTop) + "," + String(CropWidth) + "," + String(CropHeight) + ")", 90, 20) : NOP()
(DebugMode > 0) ? Subtitle("Resize", 20, 40) : NOP()
(DebugMode > 0) ? Subtitle(": (" + String(NewDestinationWidth) + "," + String(NewDestinationHeight) + ")", 90, 40) : NOP()
(DebugMode > 0) ? Subtitle("Borders", 20, 60) : NOP()
(DebugMode > 0) ? Subtitle(": (" + String(BorderLeft) + "," + String(BorderTop) + "," + String(BorderRight) + "," + String(BorderBottom) + ")", 90, 60) : NOP()
(DebugMode > 1) ? Subtitle("Detected", 20, 100) : NOP()
(DebugMode > 1) ? Subtitle(": T " + String(DetectedTop) + " / H " + String(DetectedHeight), 90, 100) : NOP()
(DebugMode > 1) ? Subtitle("System", 20, 120) : NOP()
(DebugMode > 1) ? Subtitle(": H " + String(DestinationSystemHeight) + " / S " + String(DestinationSubSystemHeight), 90, 120) : NOP()
(DebugMode > 1) ? Subtitle("Factors", 20, 140) : NOP()
(DebugMode > 1) ? Subtitle(": H " + LeftStr(String(HorizontalFactor), 6) + " / V " + LeftStr(String(VerticalFactor), 6)+ " / C " + LeftStr(String(Correct704Factor), 6), 90, 140) : NOP()

#### Return a Clip or it will crash!
Return(last)

Reference Thread:http://forum.doom9.org/showthread.php?s=&threadid=55641

DDogg
26th August 2003, 16:03
Anybody know when Sh0dan will be back? I believe the report above shows a problem with the August 20 build. If I missed something obvious please let me know.

sh0dan
26th August 2003, 16:28
Most probably related to the "Corrected ParseMultiplication so it parses from left to right."-fix. I'll have a look at it, and return.

Edit: Seems like height is set to 0.

Edit 2: Bingo:

"VerticalFactor = DestinationHeight / (DestinationSystemHeight * 1.)"

or even safer:

"VerticalFactor = float(DestinationHeight) / float(DestinationSystemHeight)"

Both fixes the problem. I will however look into why AviSynth crashes instead of giving the error message.

DDogg
26th August 2003, 18:27
Thanks for looking in to it. Hmm, thinking out loud, this is sort of a can of worms. This script is in wide circulation, but one cannot expect for you to be able to deal with scripts written with workarounds for pre Aug 20 builds.

Hey, I guess my question is which is broken the script or the build? I suspect the answer is neither, just a situation that has to be dealt with.

Just for the record, replacing:
VerticalFactor = DestinationHeight / DestinationSystemHeight * 1.
with either:
VerticalFactor = DestinationHeight / (DestinationSystemHeight * 1.)
or
VerticalFactor = float(DestinationHeight) / float(DestinationSystemHeight)
still gave me a script error with the new build.

sh0dan
27th August 2003, 12:43
The script is broken - avisynth had a bug, which was corrected. It is related to the order multiplications and divisions are performed.

AviSynth now does it like it is supposed to do. discussion (http://forum.doom9.org/showthread.php?s=&threadid=59361) . Quite strange the script doesn't work - it works fine here. What's the error message?

DDogg
27th August 2003, 16:00
Yep, you are right. I must have got in a hurry when checking it before. It now works fine when the line in SemiAutoCropAndResize.avs:
VerticalFactor = DestinationHeight / DestinationSystemHeight * 1.
is changed to:
VerticalFactor = float(DestinationHeight) / float(DestinationSystemHeight)

Sh0dan, thanks a lot for taking the time to check this out. You are much appreciated. Also, OT, thanks for trying to deal with the Direct Show stuff. I don't imagine that has been a real thrill for you. It does not sound like a lot of fun. I hope your office walls don't have too many kicked holes in them :)