Log in

View Full Version : Can I speed up this script with better syntax?


NerdWithNoLife
19th November 2008, 17:11
This already involves IVTC of 1080i, so I can't afford to slow it down any more.
a=MPEG2Source("video.d2v").TFM().BicubicResize(720,480).Crop(8,0,-8,0)

x=a.Trim(1,90).TDecimate()+\
a.Trim(91,610).SelectEvery(5,0,1,2,3)+\
a.Trim(611,16192).TDecimate()+\
a.Trim(16193,16877).SelectEvery(5,0,1,2,3)+\
a.Trim(16878,21832).TDecimate()+\
a.Trim(21833,22207).SelectEvery(5,0,1,2,3)+\
a.Trim(22208,29005).TDecimate()+\
a.Trim(29006,30605).SelectEvery(5,0,1,2,3)+\
a.Trim(30606,37572).TDecimate()+\
a.Trim(37573,38522).SelectEvery(5,0,1,2,3)+\
a.Trim(38523,39617).SelectEvery(5,0,1,2,3)+\
a.Trim(39618,51476).Tdecimate()+\
a.Trim(51477,52161).SelectEvery(5,0,1,2,3)+\
a.Trim(52162,53891).tdecimate()+\
a.Trim(53892,55571).SelectEvery(5,0,1,2,3)+\
a.Trim(55572,62391).tdecimate()+\
a.Trim(62392,63086).SelectEvery(5,0,1,2,3)+\
a.Trim(63087,65386).tdecimate()+\
a.Trim(65387,65751).SelectEvery(5,0,1,2,3)+\
a.Trim(65752,74868).tdecimate()+\
a.Trim(74869,77033).SelectEvery(5,0,1,2,3)

fix1=x.trim(0,4435)+x.Trim(4435,-1)+X.Trim(4436,0)
fix2=fix1.Trim(0,30685)+fix1.Trim(30685,-1)+fix1.Trim(30686,0)
fix3=fix2.Trim(0,59804)+fix2.Trim(59804,-1)+fix2.Trim(59805,0)

fix3
With that script, instead of the usual 9fps from IVTC followed by resizing, this semi-manual version crawls at 3fps. Is it my syntax that slowed it down?

kemuri-_9
19th November 2008, 21:17
It's probably due to the number of calls you do to Tdecimate and SelectEvery.

the most realistic way of increasing efficiency would be to use a Tdecimate override file for the SelectEvery sections,
so you only need one Tdecimate call to do all the decimation.

this would remove the need for all the trims in the decimation portion,
as well as the multiple selectevery and tdecimate calls there too.

NerdWithNoLife
19th November 2008, 21:50
That will be one long override file then! I did that once; it involved making an Excel sheet with thousands of lines, pluses and minuses, then converting it to a text file. Hmm... which is worse - the computer thinking for 3 more hours or me doing work for an extra half hour? I guess I could look into doing one call of TDecimate and overlaying the manual portions...

IanB
19th November 2008, 22:01
You could try adding a little out of order protection to MPEG2Source, but it is usually extremely well behaved in terms of seeking the input. Other input source filters are not as forgiving.

Generally do not instantiate many copies of the same filter, in this case SelectEvery is a zero cost filter and TDecimate is a well written filter, so it won't make much (if any) difference here. It is much better to create all the flavours of output once each then just slice and dice the resultant clips.

To duplicate or remove single frames there are the DuplicateFrame and DeleteFrame filters. Be aware they do not adjust any associated audio track, like the triple trim code you originally had. All 3 filters are zero cost so it comes down to a style choice.MPEG2Source("video.d2v")
ChangeFPS(Last, True) # Try a little out of order protection

TFM()
BicubicResize(720,480)
Crop(8,0,-8,0)

b = TDecimate()
c = SelectEvery(5,0,1,2,3)

b.Trim( 1, 90) + \
c.Trim( 91, 610) + \
b.Trim( 611, 16192) + \
c.Trim(16193, 16877) + \
b.Trim(16878, 21832) + \
c.Trim(21833, 22207) + \
b.Trim(22208, 29005) + \
c.Trim(29006, 30605) + \
b.Trim(30606, 37572) + \
c.Trim(37573, 38522) + \
c.Trim(38523, 39617) + \
b.Trim(39618, 51476) + \
c.Trim(51477, 52161) + \
b.Trim(52162, 53891) + \
c.Trim(53892, 55571) + \
b.Trim(55572, 62391) + \
c.Trim(62392, 63086) + \
b.Trim(63087, 65386) + \
c.Trim(65387, 65751) + \
b.Trim(65752, 74868) + \
c.Trim(74869, 77033)

DuplicateFrame( 4435)
DuplicateFrame(30685)
DuplicateFrame(59804)
If your input video has frames larger than your output size, you could reduce the width before using TFM() to partially reduce the frame size it has to work with. You must not adjust the height until after all the interlaced processing has been completed, i.e. TFM()

Also you could crop the input source during resizing.MPEG2Source("video.d2v")
ChangeFPS(Last, True) # Try a little out of order protection

BicubicResize(704, Height(), 21, 0, -21, 0)

TFM()

BicubicResize(Width(), 480)

...Conversely if the input video has frames smaller than your output size, you could defer the resize until the end of the script to minimise the frame size TDecimate() has to work with.

Gavino
19th November 2008, 22:41
b = TDecimate()
c = SelectEvery(5,0,1,2,3)

b.Trim( 1, 90) + \
c.Trim( 91, 610) + \
...

Won't that give different results?

Trim(i, j).SelectEvery(5, ...) is not the same as SelectEvery(5, ...).Trim(i, j) since the frame numbers change.

IanB
20th November 2008, 00:31
@Gavino,

Yeah, if it's a problem, do this. The original looked pretty arbitrary so I did not want to cloud the main point I was making.c = SelectEvery(5,0,1,2,3)
d = SelectEvery(5,0,1,2,4)
e = SelectEvery(5,0,1,3,4)
f = SelectEvery(5,0,2,3,4)
g = SelectEvery(5,1,2,3,4)

kemuri-_9
20th November 2008, 00:37
Won't that give different results?

Trim(i, j).SelectEvery(5, ...) is not the same as SelectEvery(5, ...).Trim(i, j) since the frame numbers change.
that will give different results

@NerdWithNoLife
Tdecimate accepts decimate patterns and they can be applied on ranges,
Ex.
91,610 ++++-
16193,16877 ++++-

these override lines are equivalent to SelectEvery(5,0,1,2,3) to be applied on those frame ranges.
there's no need to do it one frame per line.

Gavino
20th November 2008, 01:06
Yeah, if it's a problem, do this. The original looked pretty arbitrary so I did not want to cloud the main point I was making.c = SelectEvery(5,0,1,2,3)
d = SelectEvery(5,0,1,2,4)
...

That still doesn't complete the work as you would have to recalculate all the Trim frame numbers to allow for c, d, etc having 20% less frames than the original.

I understand the main point - my point was that this strategy doesn't work so simply when the filter in question changes frame numbers, as it does here.

NerdWithNoLife
20th November 2008, 02:32
@NerdWithNoLife
Tdecimate accepts decimate patterns and they can be applied on ranges,
Ex.
91,610 ++++-
16193,16877 ++++-

these override lines are equivalent to SelectEvery(5,0,1,2,3) to be applied on those frame ranges.
there's no need to do it one frame per line.
Well don't I feel stupid. Actually, I'm glad I do because now it's plugging along!:thanks:

NerdWithNoLife
20th November 2008, 05:21
Just for fun, I did get my crazy method working better with IanB's suggestions. If for some odd reason someone wanted to mix manual IVTC with TDecimate, here's how you could:
MPEG2Source("video.d2v")
BicubicResize(720, Height() )
TFM()
BicubicResize(Width(), 480)
UnD = last

Function a (clip c) { c.TDecimate() }
Function b (clip c) { c.SelectEvery(5,0,1,2,3) }

UnD.Trim( 1, 90) .a + \
UnD.Trim( 91, 610) .b + \
UnD.Trim( 611,16192) .a + \
UnD.Trim(16193,16877) .b + \
UnD.Trim(16878,21832) .a + \
UnD.Trim(21833,22207) .b + \
UnD.Trim(22208,29005) .a + \
UnD.Trim(29006,30605) .b + \
UnD.Trim(30606,37572) .a + \
UnD.Trim(37573,38522) .b + \
UnD.Trim(38523,39617) .b + \
UnD.Trim(39618,51476) .a + \
UnD.Trim(51477,52161) .b + \
UnD.Trim(52162,53891) .a + \
UnD.Trim(53892,55571) .b + \
UnD.Trim(55572,62391) .a + \
UnD.Trim(62392,63086) .b + \
UnD.Trim(63087,65386) .a + \
UnD.Trim(65387,65751) .b + \
UnD.Trim(65752,74868) .a + \
UnD.Trim(74869,77033) .b
The horizontal resize before IVTC got it up to 13fps instead of 9. So I'll definitely be doing that in the future, along with the easy way (a properly written override file)!

Gavino
20th November 2008, 10:52
Function a (clip c) { c.TDecimate() }
Function b (clip c) { c.SelectEvery(5,0,1,2,3) }

UnD.Trim( 1, 90) .a + \
UnD.Trim( 91, 610) .b + \
UnD.Trim( 611,16192) .a + \
UnD.Trim(16193,16877) .b + \
...etc


Note that this part of the code, although more readable, is effectively the same as the original. There is still a separate instance of TDecimate or SelectEvery for each slice, so there is no run-time difference.

NerdWithNoLife
20th November 2008, 14:25
Note that this part of the code, although more readable, is effectively the same as the original. There is still a separate instance of TDecimate or SelectEvery for each slice, so there is no run-time difference.
When I ran it there was. It got to 12 or 13 fps, while the other one was around 3.

Gavino
20th November 2008, 15:04
I was talking only about the part I quoted ("Note that this part of the code ...").

The speedup comes from the other bits you changed, principally doing the horizontal resizing before the IVTC.