Log in

View Full Version : automated NTSC -> PAL conversion for hybrid clips


scharfis_brain
20th December 2003, 12:16
Function
function converter(clip input, int x, int y, bool TFF, int "combth", bool "speedup", bool "show")
{
function kernelbob(clip a, int th)
{
f=a.
\kerneldeint(order=0, sharp=true, twoway=true, threshold=th)
e=a.separatefields.trim(1,0).weave.
\kerneldeint(order=1, sharp=true, twoway=true, threshold=th)
interleave(f,e)
}

function speeduphybrid(clip i, int width, int height, bool debug)
{
comb = telecide(i,order=1,post=0).decimate(5)
f = framerate(comb)

film = comb.lanczosresize(width,height).assumefps(25,true)
film = (debug == false) ? film : film.subtitle("film -> IVTC -> speedup")

video = i.kernelbob(5).lanczosresize(width,height).
\convertfps(2*f).assumefps(50,true).separatefields.selectevery(4,0,3).weave
video = (debug == false) ? video : video.subtitle("video -> blending -> speedup")

conditionalfilter(comb,video,film,"iscombed(combthr)","=","true")
}

function speedupfilm(clip i, int width, int height, bool debug)
{
film = i.assumefps(25,true).lanczosresize(width,height)
film = (debug == false) ? film : film.subtitle("film -> speedup")
return film
}

function converthybrid(clip i, int width, int height, bool debug)
{
comb = telecide(i,order=1,post=0).decimate(5)

film = comb.lanczosresize(width,height).
\changefps(50).separatefields.selectevery(4,0,3).weave
film = (debug == false) ? film : film.subtitle("film -> IVTC -> convert")

video = i.kernelbob(5).lanczosresize(width,height).
\convertfps(50).separatefields.selectevery(4,0,3).weave
video = (debug == false) ? video : video.subtitle("video -> blending")

conditionalfilter(convertfps(comb,25),video,film,"iscombed(combthr)","=","true")
}

function convertfilm(clip i, int width, int height, bool debug)
{
film = i.lanczosresize(width,height).
\changefps(50).separatefields.selectevery(4,0,3).weave
film = (debug == false) ? film : film.subtitle("film -> convert")
return film
}

combth = default(combth,20)
global combthr=combth
speed = default(speedup,true)
debug = default(show ,false)

input = (TFF == true) ? input.assumetff : input.assumebff

speedupout = (framerate(input) < 26) ?
\speedupfilm(input,x,y,debug) : speeduphybrid(input,x,y,debug)

convertout = (framerate(input) < 26) ?
\convertfilm(input,x,y,debug) : converthybrid(input,x,y,debug)

final = (speed == true) ? speedupout : convertout

final.resampleaudio(audiorate(input))
}



Usage

loadplugin("...\kerneldeint140.dll")
loadplugin("...\decomb510.dll")
import("...\converter.avs")

avisource("hybrid.avi")
converter(768,576,TFF=true,combth=20,speedup=true,show=false)


this function converts a hybrid NTSC-Video automated
to PAL using IVTC on Telecined Film and blending on
the other parts.

You can decide, wheater the video should be speeded up
by 25/23.976 to get progressive Film-parts.

If no speedup is whished, the Film-parts get interlaced,
too. But with better quality then with stupid blending.

If the input-clip is already 23.976 fps, this will be
detected and just a speedup or conversion from 23.976
to 25 will be made.

Parameters:
x - new width
y - new height

TFF - true for TFF-clips, false for BFF-clips

combth - (default = 20) defines the threshold weather the
.........input clip is interlaced or progressive (telecined)

speedup - (default = true)
show - (default = false) shows, which conversion is applied
.........to the video:

29.97 fps input-clip with speedup gives either
"film -> IVTC -> speedup" or "video -> blending -> speedup"

29.97 fps input-clip without speedup gives either
"film -> IVTC -> convert" or "video -> blending"

23.976 fps input-clip shows:
with speedup "film -> speedup"
without speedup "film -> convert"

The Audio stays always in sync with the Video.

Didée
21st December 2003, 12:29
Now, this is a nice one! Pity I've not one single NTSC source to test it on.

Remarks:

1) Is "IsCombed = 20" the one-and-only sweetspot, or should that value also be parametrized?

2) For the not-so-advanced users, it should be mentioned that when using speedup, the audio part also gets (dumbly) speed up by ~4% to stay in synch. This is okay since it is the only way to do it in AviSynth. A better solution, though, is to change audio speed with an appropriate external audio editor to preserve pitch.

Once more, good work, Scharfi!

- Didée

scharfis_brain
21st December 2003, 15:00
I've already tried to make something like that:
function speeduphybrid(clip i, int width, int height, int th, bool debug)
{i.iscombed(th)}


This returns "Don't know what th means"

thats really weird

Didée
21st December 2003, 16:28
Oh, yes.

Some time ago, this was driving me nuts, too.

Tryfunction speeduphybrid(clip i, int width, int height, int "th", bool debug)

Better?

- Didée

scharfis_brain
21st December 2003, 16:47
Grrrrrrrrrrrrrrrrrrrr

can't even get
x=avisource("hybrid.avi").assumetff

a=x.IsCombed()
x = ( a== true) ? x.subtitle("interlaced") : x.subtitle("progressive")
return x working.

It always claims Invalid arguments to function "IsCombed"

no matter weather I use
x.iscombed()
x.iscombed(20)
iscombed(x) or
iscombed(x,20)

but within my NTSC->PAL converter it is working...

I am totally confused. Can anybody clear this up?

Wilbert
21st December 2003, 17:17
You need to use it in the conditionalfilter invironment.

scharfis_brain
21st December 2003, 17:44
Okay. Found out that (Pseudocode)

function x(clip x, clip y, clip z, int th)
{ conditionalfilter(x,y,z,"iscombed(th)","=","true") }

didn't work


but

global th = yourvalue
function x(clip x, clip y, clip z)
{ conditionalfilter(x,y,z,"iscombed(th)","=","true") }

worked....

weird again.

btw. I've updated the NTSC->PAL conversion-function above with variable combingthreshold

Wilbert
21st December 2003, 17:57
Variables in the conditional invironment need to be global and initiated. Thus something like


function x(clip x, clip y, clip z, int th)
{
global th2 = th
conditionalfilter(x,y,z,"iscombed(th2)","=","true")
}

should also work ...

verabgd
5th January 2004, 21:17
@scharfis_brain

I have tried your “converter” but I got the error message in VirtualDubMob: Invalid arguments to function “converter”.

The script is:

++++++++++

loadplugin("...\mpeg2dec3.dll")
loadplugin("...\kerneldeint140.dll")
loadplugin("...\decomb510.dll")
import("...\converter.avs")

video1=MPEG2Source("...\Concert.d2v")
video=converter(video1,768,576,true,20,true,false)

return video
++++++++++++++++++

How to put right arguments? I suppose that the problem is argument “input”, type clip? The source clip must be AVI?

Thank you very much in advance.

Bogalvator
5th January 2004, 22:44
If you replace your last 3 lines of script with:

MPEG2Source("...\Concert.d2v")
ConvertToYUY2(interlaced=true) #so convertfps(...) will work (only used when blending video sections)
converter(768,576,true,20,true,false)

it should be OK. ConvertToYUY2(...) is not required if your source is 100% film.

verabgd
6th January 2004, 04:00
Thanks, but it doesn't work.

Actually it cannot work. If the function is defined as

function converter(clip input, int x, int y, bool TFF, int "combth", bool "speedup", bool "show")

with 7 arguments, function call must have 7 input values.

Function call:

converter(768,576,true,20,true,false)

has 6 input values. The input clip that should be converted is missing?

scharfis_brain
6th January 2004, 04:47
just use:


loadplugin("...\mpeg2dec3.dll")
loadplugin("...\kerneldeint140.dll")
loadplugin("...\decomb510.dll")
import("...\converter.avs")

MPEG2Source("...\Concert.d2v")
converttoyuy2(interlaced=true)
converter(768,576,true)


this should work, becase all other parameters are optional

verabgd
7th January 2004, 00:03
Yes, it works. Actually the quality of the output video is extremely good! Clean, sharp, with smooth movements … unbelievable for second generation NTSC hybrid tape from 1992! Thanks a lot.

scharfis_brain
7th January 2004, 00:54
Nice to hear, yuo had success!

I have one question:
sometimes my script fails and thus detecting FILM as Video and vice versa.

could you check your video for some of those parts and report whether the are causing jerks or not?

Please do the same with the cut-points, where Film is becoming Video & vice versa.

use show=true for examining this.


I ask this, because I am PAL-Man who doesn't have any hybrid NTSC.

This function is only build out of theory and synthetic generated clips....

Bogalvator
7th January 2004, 01:27
Could I suggest that you can replace convertfps(50) in the "function converthybrid" section with changefps(60).decimate(6)

This has the advantage of not having to change to YUY2 and also has no blending. However video may be slightly jerky. See which you prefer....
(The above is only applicable if you set speedup=false)


In my very swift test, I did find that the default value for combth of 20 did result in a large amount of video sections being detected as film - around 30% - which obviously results in a few jerky bits. Reducing this to 10 did help greatly. There were naturally still a few errors but never more than a few isolated frames.

scharfis_brain
7th January 2004, 01:36
just replace convertfps with changefps and you'll get your jerky result.

I dislike discharding 10 out of 60 frames to get 50 fps, because you'll get a really weird 10 Hz motion-stutter.

This is flatten out by convertfps

as you can see, I use changfps for converting 24fps to 25fps (no speedup), (inserting 2 fields per second -> two slight stutters per second) instead of using convertfps here, because this case isn't worth heavy blending.

Mug Funky
7th January 2004, 15:49
this script is superb! i've been looking for a pain free solution for hybrid clips, but just couldn't be bothered writing one for myself :)

thanks heaps. will try it out right away.

[edit]

nice. i'm curious as to how it handles 30p content? it seems to treat them as filmlike. that's okay, but it means i get the occasional jerky pan in some animes (chobits disc 2, R1)

[edit edit]

ha. looks like you posted while i was editing. yeah, i'd discovered that problem :):)

scharfis_brain
7th January 2004, 15:56
If you want to have even more superbness (does this word exist?), just replace kernelbob with TMCkernelbob or TMCbob...

btw. One thing is unsolved with this converter.avs:
30p Video will be detected as progressive. That means every 5. Frame is taken out, which results in heavy jerkieness on 30p sequences.
But maybe there is a decomb-parameter which can do some blending in such cases?!? I don't know much about tweaking filters, I am just toooo lazy for doing so...

btw. maybe, I am writing the vice versa script for PAL-hybrids to NTSC, hehe (this will be MUCH easier than NTSC -> PAL)

gomi
9th January 2004, 21:42
He!
Is there already a compiled version of this DLL-funktion (Hybrid NTSC > PAL) available to download, or do i have to compile it oneself?

I,ll encode Star Trek NG Season 2 (Hybrid NTSC) soon and i never had such material, so i need to think about a method to handle this.

thanks

Boulder
9th January 2004, 23:08
You don't need a dll, the function is there already. You'll need KernelDeint and Decomb 5 though.

You may want to copy-paste the whole function to Notepad and save it to your plugins folder with any name but with the extension avsi so it will be loaded whenever you call any of the included functions in your script.

gomi
10th January 2004, 04:44
he,

you mean *.AVS, right? ok, thank you, i'll try it...
i make a "ANSI.avs" within the "function converter" and load the file as a plugin. - please correct me if i'm wrong!

could you tell me wich parameters to use for a CCEDVD-encode?
i would leave it as it is, but i have to match the size of 4,7 and thats why i reencode it. anyway, i'll read about this topic and maybe i figure it out alone... :(

thank you!
by

Richard Berg
10th January 2004, 09:52
No, Boulder was correct. Only *.avsi will be automatically imported.

Boulder
10th January 2004, 09:53
EDIT: Richard was faster:D

No, it has to be *.AVSI!

Copy-paste this part:

function converter(clip input, int x, int y, bool TFF, int "combth", bool "speedup", bool "show")
{
function kernelbob(clip a, int th)
{
f=a.
\kerneldeint(order=0, sharp=true, twoway=true, threshold=th)
e=a.separatefields.trim(1,0).weave.
\kerneldeint(order=1, sharp=true, twoway=true, threshold=th)
interleave(f,e)
}

function speeduphybrid(clip i, int width, int height, bool debug)
{
comb = telecide(i,order=1,post=0).decimate(5)
f = framerate(comb)

film = comb.lanczosresize(width,height).assumefps(25,true)
film = (debug == false) ? film : film.subtitle("film -> IVTC -> speedup")

video = i.kernelbob(5).lanczosresize(width,height).
\convertfps(2*f).assumefps(50,true).separatefields.selectevery(4,0,3).weave
video = (debug == false) ? video : video.subtitle("video -> blending -> speedup")

conditionalfilter(comb,video,film,"iscombed(combthr)","=","true")
}

function speedupfilm(clip i, int width, int height, bool debug)
{
film = i.assumefps(25,true).lanczosresize(width,height)
film = (debug == false) ? film : film.subtitle("film -> speedup")
return film
}

function converthybrid(clip i, int width, int height, bool debug)
{
comb = telecide(i,order=1,post=0).decimate(5)

film = comb.lanczosresize(width,height).
\changefps(50).separatefields.selectevery(4,0,3).weave
film = (debug == false) ? film : film.subtitle("film -> IVTC -> convert")

video = i.kernelbob(5).lanczosresize(width,height).
\convertfps(50).separatefields.selectevery(4,0,3).weave
video = (debug == false) ? video : video.subtitle("video -> blending")

conditionalfilter(convertfps(comb,25),video,film,"iscombed(combthr)","=","true")
}

function convertfilm(clip i, int width, int height, bool debug)
{
film = i.lanczosresize(width,height).
\changefps(50).separatefields.selectevery(4,0,3).weave
film = (debug == false) ? film : film.subtitle("film -> convert")
return film
}

combth = default(combth,20)
global combthr=combth
speed = default(speedup,true)
debug = default(show ,false)

input = (TFF == true) ? input.assumetff : input.assumebff

speedupout = (framerate(input) < 26) ?
\speedupfilm(input,x,y,debug) : speeduphybrid(input,x,y,debug)

convertout = (framerate(input) < 26) ?
\convertfilm(input,x,y,debug) : converthybrid(input,x,y,debug)

final = (speed == true) ? speedupout : convertout

final.resampleaudio(audiorate(input))
}

Save it as any name you like and use the extension avsi. If you use Notepad, you probably have to select 'All Files' first so that the program won't add the extension .txt to the name when saving. Place the file in your AVS2.5 plugins folder (probably C:\Program Files\Avisynth 2.5\plugins).

You don't have to load the function in your script. AVS finds it automatically if it's in your plugins folder just like it finds all the filter dlls if you put them there.