DGDecode frameserving

DGDecode is today's fastest way to frameserve MPEG-1/2 video to any video encoder via AviSynth. DGDecode and AviSynth 2.x allow the video to stay in the video's native colorspace, so you won't incurr quality loss and speed loss due to unnecessary conversions (unlike VFAPI, which converts everything to RGB and the encoder coverts it back to YV12).

Since we're going to use AviSynth in this tutorial, and not only as a simple frameserving mechanism but to crop and resize as well I strongly suggest that you visit the Avisynth homepage and check out the Avisynth tutorial and the Avisynth scripting reference. While you can get along without having read these it's as with any advanced program: You can only properly master it by having RTFM!

You need the following software in this guide:

AviSynth
DGDecode

Now let's get started

Step 1: Create a DGIndex project

This is a frequently used guide so I placed it in a separate document. Press the back button in your browser to get back here when you're done.

Step 2: Avisynth script creation

AviSynth can use the DGDecode plugin to open DGIndex projects. You can load AviSynth plugins using the LoadPlugin command. Alternatively, AviSynth will automatically load every plugin that has been copied to its plugin folder (normally it's a subdirectory called plugins in the AviSynth installation folder - c:\program files\avisynth\)

The following is a sample scrip which simply opens the d2v file.

LoadPlugin("c:\temp\dgdecode\dgdecode.dll")
mpeg2source("D:\matrix\matrix.d2v")

If you load this script into any encoding program that can read avs files you'll get the output of DGIndex, that is the decoded MPEG-2 data without any resizing applied (so the aspect ratio will be off).

The next step is cropping. You can skip it for DVD creation if your source is a DVD. Now the values you wrote down when working in DGIndex become useful again. Cropping works by the use of the Crop function in Avisynth. It takes 4 arguments: Left, Top, Horizontal output size and vertical output size. Here's an example which crops the black bars from a 16:9 1:2.35 NTSC stream:

Crop(0,60,720,360)

This will crop the top 60 line of the video, plus the bottom 60 lines (remember that the video stream originally was 720x480, so if you crop the top 60 lines away and take the next 360 lines that cuts off the bottom 60 as well - 60 (black) + 360 (video) + 60 (black) = 480) If you add this line to the script above you'll now get a 720x360 video stream which has no black bars, just the video data. In recent AviSynth versions, an alternative way to crop was added: if the third and fourth parameter are negative, they serve as crop values as well. So, the above crop line is equal to this:

Crop(0, 60, 0, -60)

In the following, I will not go into interlaced or telecined sources. If you're dealing with such a source, have a look at the Decomb guide, and if you don't know what interlaced or telecined means, have a look at the video basics.

DivX resizing

Now we come to the last operation: resizing. AviSynth, Bilinear, Bicubic and Lanczos resize. Lanczos yields the best quality, Bilinear is the fastest but yields the lowest quality. Resizing takes two parameters: the horizontal and vertical resolution the video should be resized to

BilinearResize(640,272)

For Bicubic Resize the line would be: BicubicResize(640,272), and LanczosResize(640,272) for lanczos resizing.

So a full script that would load the d2v project, crop and resize it to 640x272 would be:

LoadPlugin("C:\temp\dgdecode\dgdecode.dll")
mpeg2source("d:\matrix\matrix-unresized.d2v")
Crop(0,60,720,360)
BilinearResize(640,272)

SVCD resizing

To resize a DVD to SVCD resolution, no cropping is necessary. But once again we have to do the letterboxing on our own. As people who have used VirtualDub for SVCD frameserving already know what we have to do is to resize to 480x362, then add some black bars. To add black borders we use AddBorders which takes the following 4 arguments: left, top, right and bottom. In Avisynth language that would become the following:

BicubicResize(480,360)
AddBorders(0,60,0,60)

So the script would become

LoadPlugin("C:\temp\dgdecode\dgdecode.dll")
mpeg2source("d:\matrix\matrix-unresized.d2v")
BicubicResize(480,360)
AddBorders(0,60,0,60)

Please note that for PAL the script would be

LoadPlugin("C:\temp\dgdecode\dgdecode.dll")
mpeg2source("d:\matrix\matrix-unresized.d2v")
BicubicResize(480,432)
AddBorders(0,72,0,72)

Audio:

If you have decoded audio in DGIndex you might want to add the decoded audio to your AviSynth script. In order to do this you make use of the wavsource function which takes the path and name of the WAV file as argument. If your WAV file contains 6 channels, keep in mind that whichever software you're loading the AviSynth script into will have to support such audio (most do not, which means you have to downmix the audio by activating Dolby Digital Downmix in DGIndex). And now we get a little more into programming. I don't know if this is completely accurate but it appears to me as if mpeg2source would return the mpeg2 stream and all the following operations would be applied to this returned stream. If you'd add wavsource("d:\matrix\audio.wav") after mpeg2source you'd end up with some errors, as now Avisynth would try to apply the cropping to you wave source, which of course won't work. What you have to do instead is to define temporary variables for both video and audio. Here's an example:

video=mpeg2source("d:\matrix\matrix-unresized.d2v")
audio=wavsource("d:\matrix\audio.wav")
audiodub(video,audio)

where the last line adds video and audio stream to one stream and returns it. Now you can place your crop & resize lines.

LoadPlugin("C:\temp\dgdecode\dgdecode.dll")
video=mpeg2source("d:\matrix\matrix-unresized.d2v")
audio=wavsource("d:\matrix\audio.wav")
audiodub(video,audio)
Crop(0,60,720,360)
BilinearResize(640,272)

Is the final result for a DivX Avisynth script.

You can now load your avs file into the encoding program of your choice. Note that to prevent confusion I won't link to all the possible guides where you could continue now but if you've made it so far I'm sure you'll manage to find the right place to continue.

A last word for the DivX crowd: In VirtualDub you have to set video to Fast Recompress to profit from the speed increase. This means that you can't use additional filters :/

This document was last updated on January 4, 2009