Encoding DivX-compatible AMVs with XviD

What is XviD?

XviD is an mpeg-4 codec which is developed for educational use by programmers in their free time. The reason you are reading an XviD guide here instead of a DivX5 guide or a RealPlayer 9 guide etc is as follows:

1) XviD is more customisable, which allows us to choose options better suited for amvs - this is not entirely possible with DivX5.
2) Although RealPlayer 9 can give good results, it is a very restrictive format that doesn't allow great compatibility.
3) Some of DivX5's features might well reduce file sizes but equally they are quite ugly and occasionally broken.
4) XviD doesn't have any spyware of any kind; DivX 5 (if you want all the options) is.

The other good thing about XviD is that although you have to download a new codec to encode your videos, people who have DivX 4 or DivX 5 do not need to download a new codec to see them, provided you use the options I tell you to :) Also, as it is mpeg4 compliant it can be later muxed into a true standards compatible MPEG4 file. XviD files also run on Mac OS9 if prepared by the divx validator at divx.com

If you want to encode XviD avis with extra options, other people will need either the XviD codec or FFDShow installed to watch them.


Getting Started

First thing you need to do is to download the latest XviD Build from here:

Install it.

Open up virtualdub, load up your avisynth file that you have prepared with all its nice filters and go to the Video menu. Choose "Compression..."

Scroll to the bottom and choose XviD and click "configuration".

XviD has a lot of options but I really only recommend following these settings for quality outputs. We are going to do a 2-pass encode. This means that we are actually going to encode our video twice - once at maximum quality and again with the best compression. The advantage of 2 pass is that the encoder can find out exactly how well it needs to encode every scene so it can make sure certain bits don't end up looking rubbish when you make it smaller.

Setting Up the First Pass

Choose "2-pass 1st Pass" from the pulldown menu. Click "Advanced Options".

The first thing you see is the Global tab. In here we have the basic options which you should set up as follows:

Motion Search Precision - 6 (Ultra high)
Quantisation Type - H.263 [Note that some guides say that MPEG is better but this is not true for anime. Also, MPEG can't be decoded properly by the Divx filters]
FourCC Used - DIVX
Maximum I-frame interval - 300
Minimum I-frame interval - 1

Do not enable lumi-masking or interlacing. Only enable greyscale if you really are doing a true greyscale video.

Now click the Two-Pass tab and make sure "Discard first pass" is deselected as we will need the the first pass encode later.

Now we are ready to encode. Click ok and go back to the main virtualdub screen. Choose Video -> Fast Recompress and choose Audio -> No Audio. We don't need audio for the first pass and it's easier to work out the size of the first pass if we don't have to worry about audio.

OK, at this point the guide will split into two - there is a super advanced encoding version or just the normal one. If you want the super advanced one click here otherwise keep reading.

Click File -> "Save as avi..." It will give a warning about fast recompress - click yes.

BUT WE ARE NOT FINISHED - READ ON!!

This will create an encode of your amv at what is known as Constant Quantiser 2. In simple terms this is the best looking you can get without having stupidly big files (quantiser 1 only looks marginally better but is much bigger).

It will probably take about 20 minutes on an average PC, depending on the amount of filtering you are doing. When it's finished, don't close virtualdub yet as we'll use it again in a second.

OK, have a look at the file you made - it should be pretty nice. If it's not, then you need to work harder at that filtering :) OK, how big is it? If you think that it's small enough to distribute then you are finished! Go to the audio guide and see how to attach your sound.

However, it is likely that you might want to make this smaller. You can usually make the final encoded video stream to be about 70% of your first pass size and still look very good indeed.

Second Pass

How big is your file? Right-click your file, choose 'properties' have a look and see how many bytes it is. Copy that number and get a calculator (or open up calc in windows) and do this calculation:

(your file size in bytes * 0.7) / 1024

[your file size in bytes times 0.7 divided by 1024]

This is how big the video part of your amv should be and be almost always certain to look very good.


If for whatever reason that is too big (or even too small) for you then you will need to find out the best size for you - you can work that out like this:

Go to your Start Menu > Programs > XviD> MiniCalc (Bitrate Calculator). In 'film length' put in the length of you amv. Unfortunately this bitrate calc doesn't allow you to put in seconds, they have to be a fraction of a minute so...

4 minute 30 seconds is 0.045
3 minutes 23 seconds is 3/100 + 23/6000 =0.0338

Then choose your fps

Then select the bitrate you will encode your audio and in the Final Desired Size section, delete the CD size and put in the number of megabytes you want your file to be.

Choose CBR MP3 and then copy the number it makes in the XviD Video Size box.

Ok, so you've calculated the new video size, remember it and go back to virtualdub. Choose Video -> Compression -> Configuration.

This time select "2 Pass - 2nd Pass Int" and enter the number you just made into the Desired Filesize box.

Now choose "Advanced"

Everything on the 'Global' Tab is fine as it was before. In the 'Quantisation' tab make sure the options say:

Min I-frame Quantizer - 2
Max I-frame Quantiser - 31
Min P-frame quantizer - 2
Max P-frame quanizer - 31

Now, click "two pass" - all of the defaults here are fine, just make sure 'Dummy 2nd Pass' is NOT selected :)

Now click 'Alt. Curve' and select "use curve aggression system" - the settings you should use which will give the best results most of the time are:

Curve Aggression - Medium
High Distance from average % - 500
Low Distance from average % - 90

Enable Automatic minimum relative quality - Yes
Strength % 30 [Note: A strength of 0 is also known to give very good results, for a more linear approach]
Enable automatic bonus bias calculation - Yes


Max bitrate 10000

Now, this is very high but it is unlikely that you are going to have many frames that are at such a high bitrate. However, if you have a section of your amv that has lots and lots of complicated movement where almost all the pixels in the image is changing every second then you might be giving it too many bits when you don't need to. If you think this is happening in your amv then this section of the video will always max out the bitrate. You can use this number to make sure it isn't too high - try reducing it to 7000 or even 5000. Otherwise, the default of 10000 is fine.

Max overflow improvement - 60
Max overflow degradation - 60

OK, that should be everything. Now go back to the main virtualdub screen. If you want to have audio encoding here then reselect AVIaudio and choose the appropriate options. If you are going to mux the audio later then leave it as No Audio. (see the audio guide)

Wait... before you start the 2nd pass... you are still using your original input file for encoding, right? It's a common misconception that we have to use re-encode the file created by the 1st pass. We don't - and in fact that would be very bad. You should still be using your original file as the source for the 2nd pass encode just like in the 1st pass, ok?

Now choose "Save as Avi..." and you will get your distribution XviD footage.

If you don't have any audio done yet that's fine, just follow this guide.


Advanced Version:

N.B. This is an experimental method that in 'theory' can give you really excellent results based on your amv but may not work in every single situation. The normal method is very good so use the normal method unless you are prepared for a little bit of geekiness or you are going to compress a lot and want to try and get the very best out of the codec.

OK, You are just about to save your first pass. Before you do that go and download the following software pack:

DebugView,PerfectXviD and Debug View Analyser

Extract the zip file to some easy to remember directory.

Now, open Dbgview.exe (DebugView) and leave it open and go back to Virtualdub and start your first pass by choosing "Save as Avi...".

You'll notice that there will be information about every frame being encoded in your avi appearing in DebugView - but we wont need all that until much later so ignore it for now.

OK, when it's finished encoding, have a look at the file you made - it should be pretty nice. If it's not, then you need to work harder at that filtering :) OK, how big is it? If you think that it's small enough to distribute then you are finished! Go to the audio guide and see how to attach your sound.

However, it is likely that you might want to make this smaller. Open up PerfectXviD and click "Open XviD Stats File" - your stats file can be found in the root directory of whatever hard drive you made your encode. It's called video.stats.

When you open it, select Interval Length 10 and you'll get something like this:


PerfectXviD ver 0.5 by TheUnforgiven
unforgiven@soon.com

Total frames = 2962
eff. bits/(pixel*frame)= 0.650
2839 Delta-frame and 123 Key-frame

Key frames are excluded from the following stats :

Average frame size = 11,798 byte
Max frame size is +464% over the average = 66597 byte
Min frame size is -99% under the average = 84 byte

+470%
[ 3 frame 0.11%

+460%
[ frame 0.00%
+450%
[ frame 0.00%
+440%
[ frame 0.00%
+430%
[ frame 0.00%
+420%
[ frame 0.00%
+410%
[ frame 0.00%
+400%
[ frame 0.00%
+390%
[ frame 0.00%
+380%
[ 1 frame 0.04%

+370%
[ 1 frame 0.04%
+360%
[ 8 frame 0.28%
+350%
[ 12 frame 0.42%
+340%
[ 9 frame 0.32%
+330%
[ 13 frame 0.46%
+320%
[ 11 frame 0.39%
+310%
[ 9 frame 0.32%
+300%
[ 13 frame 0.46%
+290%
[ 11 frame 0.39%
+280%
[ 10 frame 0.35%
+270%
[ 5 frame 0.18%
+260%
[ 8 frame 0.28%
+250%
[ 9 frame 0.32%
+240%
[ 12 frame 0.42%
+230%
[ 13 frame 0.46%
+220%
[ 17 frame 0.60%
+210%
[ 21 frame 0.74%
+200%
[ 13 frame 0.46%
+190%
[ 9 frame 0.32%
+180%
[ 9 frame 0.32%
+170%
[ 11 frame 0.39%
+160%
[ 26 frame 0.92%
+150%
[ 30 frame 1.06%
+140%
[ 28 frame 0.99%
+130%
[ 35 frame 1.23%
+120%
[ 30 frame 1.06%
+110%
[ 41 frame 1.44%
+100%
[ 40 frame 1.41%
+90%
[ 59 frame 2.08%
+80%
[ 62 frame 2.18%
+70%
[ 59 frame 2.08%
+60%
[ 41 frame 1.44%
+50%
[ 38 frame 1.34%
+40%
[ 46 frame 1.62%
+30%
[ 45 frame 1.59%
+20%
[ 92 frame 3.24%
+10%
[ 98 frame 3.45%
--- Average frame size --- { 8 frame 0.28%
[ 151 frame 5.32%
-10%
[ 127 frame 4.47%
-20%
[ 136 frame 4.79%
-30%
[ 130 frame 4.58%
-40%
[ 116 frame 4.09%
-50%
[ 125 frame 4.40%
-60%
[ 134 frame 4.72%
-70%
[ 278 frame 9.79%
-80%
[ 420 frame 14.79%
-90%
[ 216 frame 7.61%
-100%

OK, what does this tell us? The important results are the ones in bold. Make a note of them as we will need them later.

Go back into Virtualdub, choose Video -> Compression -> Configuration

Select "2 Pass - 2nd Pass Int"

Now, there is a box where you have to enter in a desired filesize. For a good quality encode you need the filesize to be about 70% of your 1st pass encode.

How big is your file? Right-click your file, choose properties and see how many bytes it is. Copy that number and get a calculator (or open up calc in windows) and do this calculation:

(your file size in bytes * 0.7) / 1024

[your file size in bytes multiplied by 0.7 divided by 1024]

This is how big the video part of your amv should be.


If for whatever reason that size is too big for you then you need to work out a different file size based on your limitations like this:

This advanced method is surprisingly good for high compression, or so I've found, but like everything it is helped by clean sources.

Go to your Start Menu > Programs > XviD> MiniCalc (Bitrate Calculator). In 'film length' put in the length of you amv. Unfortunately this bitrate calc doesn't allow you to put in seconds, they have to be a fraction of a minute so...

4 minute 30 seconds is 0.045
3 minutes 23 seconds is 3/100 + 23/6000 =0.0338

Then choose your fps

Then select the bitrate you will encode your audio and in the Final Desired Size section, delete the CD size and put in the number of megabytes you want your file to be.

Choose CBR MP3 and then copy the number it makes in the XviD Video Size box.

OK, enter the number you calculated in the Desired Filesize box.

Everything on the Global Tab is fine as it was before. In the Quantisation tab make sure the options say:

Min I-frame Quantizer - 2
Max I-frame Quantiser - 31
Min P-frame quantizer - 2
Max P-frame quanizer - 31

Now, click "two pass" - all of the defaults here are fine, just make sure Dummy 2nd Pass is NOT selected :)

Now click Alt. Curve and select "use curve aggression system" - this is where we will need our PerfectXviD data. In my test I had:

Max frame size is +464% over the average = 66597 byte
Min frame size is -99% under the average = 84 byte

Now look at the Alt. Curve Compression Options. Firstly we have aggression: for stuff with loads of action low aggression will work better, otherwise use medium aggression (medium should also be used if you have a crappy source to start with).

I don't recommend High aggression unless you have very very fast scenes followed instantly by very very slow scenes. Even then, you will sometimes get better results with Medium.

High % and Low % - these values should be changed depending on the PerfectXviD Stats as follows:

High % - This should be the value of most of your maximum frames. If you see my list above, I have 3 frames at +470 but there's a really big gap between those frames and the next ones, which are at +380. I can probably safely reduce the size of those 3 frames pretty easily, so I'm going to choose 380 as my High %. If there wasn't a gap like this, I would be just setting it to the value it said the Max Frame Size is.

Low % - This will depend a little on the type of amv you have. Have a look at the min frame size. In my encode a large amount of frames occur in the -0 to -100 range. I think a good rule of thumb here is to start at the min frame size (in my case about -100%) and then reduce by 10 > 30% depending on how light your amv is (reduce if it is lighter). If it has lots of darks scenes a value around 100 will always be very good. In my example, I will enter the value 90 into the low % as it does have a few dark scenes but is mainly light.

The remaining options should all stay at their defaults as follows:

Enable Automatic minimum relative quality - Yes
Strength % 30
Enable automatic bonus bias calculation - Yes

Max bitrate 10000

Now, this is very high but it is unlikely that you are going to have many frames that are at such a high bitrate. However, if you have a section of your amv that has lots and lots of complicated movement where almost all the pixels in the image is changing every second then you might be giving it too many bits when you don't need to. If you think this is happening in your amv then this section of the video will always max out the bitrate. You can use this number to make sure it isn't too high - try reducing it to 7000 or even 5000. Otherwise, the default of 10000 is fine.

Max overflow improvement - 60
Max overflow degradation - 60

OK, once you've done that, OK out of everything. Wait... before you start the 2nd pass... you are still using your original source file for encoding, right? It's a common misconception that we have to use re-encode the file created by the 1st pass. We don't - and in fact that would be very bad. You should still be using your original file as the source for the 2nd pass encode just like in the 1st pass, ok?

Good, now choose "Save as avi..." to make your 2nd pass, making sure you keep DebugView open.

When it's done, have a look at it - how does it look? If it looks good to you then we can keep it but there is a way of finding out if it's as good as it can be.

Go to DebugView and choose "save as..." which will save the details of the debug view. Save it as dbg.log in the folder where you put the XviD Debug Analyser (xda.exe). Once saved, double-click on xda.exe and it will hopefully create two files: Analyze.txt and Graph.txt. Open up Analyze.txt and it will look like this:

DebugView analyzer for XviD codec v0.12 by MoonWalker
e-mail : s_ilias@gmx.net


Quantizers Analisis
---------------------

Quantizers Used For Movie :
------------------------------
Quant 2 Used : 842 Times, Percentage Used : 28.43%
Quant 3 Used : 1860 Times, Percentage Used : 62.80%
Quant 4 Used : 253 Times, Percentage Used : 8.54%
Quant 5 Used : 7 Times, Percentage Used : 0.24%

Average Quantizer Used for Movie : 2.806

No credits encoding!!

H.263 Quantization Type Used 2962 timed, Percentage Used : 100.00%

Quantizers prevented from rising too steeply 13 times


Intra-Frame (Key-Frame) Quantizers
------------------------------------

Movie
-------
Quant 2 Used : 68 Times, Percentage Used : 55.74%
Quant 3 Used : 54 Times, Percentage Used : 44.26%

Credits
---------

Number Of Consecutive I-Frames : 29

Inter-Frame (P-Frame) Quantizers
------------------------------------

Movie
-------
Quant 2 Used : 774 Times, Percentage Used : 27.25%
Quant 3 Used : 1806 Times, Percentage Used : 63.59%
Quant 4 Used : 253 Times, Percentage Used : 8.91%
Quant 5 Used : 7 Times, Percentage Used : 0.25%

Credits
---------

 

Frame Analisis
----------------

Number Of Intra-Frames (Key-Frames) : 122
Number Of Inter-Frames (P-Frames) : 2840
Total Number Of Frames : 2961

4.12% of the Movie is Intra-Frames (Key-Frames)
95.91% of the Movie is Inter-Frames (P-Frames)

 

Size Analysis
----------------

1-Pass Size : 36215692 Bytes or 35366 KBytes
Scaled Size : 25881601 Bytes or 25275 KBytes
Actual Size : 25887896 Bytes or 25281 KBytes

Usefull Statistics
------------------
Compressibility : 71.48%
Relative Quality of XviD avi : 71.28%
Absolute Quality of XviD avi : 97.58%

OK, so what does this all mean? Well the important bits are:

1) Average Quantizer Used for Movie : 2.806

This is very good. If you remember, the best settings were at quantiser 2 so this isn't far from that. If you find yourself using lots of very high quantisers (8 and upwards) then you either have a very noisy source or your filesize is too small as this will tell you:

2) Compressibility : 71.48%

This is pretty much what we are aiming for. Actually, this tells me that I could increase my resolution a little if I wanted to - any compressibility between 60 and 80% is very good. You can increase compressibility with better filtering. If your source is very poor then you might find that the compressibility here is something like 30-40% which is poor. If you get a compressibility that low then you either need to lower your resolution or increase the filesize.

Now, if you are happy with your results then go to the audio guide and see how to encode and attach your audio.