Xvid

From wiki.selur.net
Jump to navigation Jump to search

Xvid is the open-source encoder for MPEG-4 Part 2 video (Simple and Advanced Simple Profile), the format of the DivX/Xvid era. It is mainly of interest today for old stand-alone players and car or portable devices that play "DivX" files but no H.264. Hybrid drives the command line encoder xvid_encraw.

Base → Video: Xvid, then the Xvid tab

What it does

Xvid predicts each frame from the previous or next one in 16×16 macroblocks (motion estimation), transforms what is left with an 8×8 DCT and quantizes it. The quantizer runs from 1 (finest, biggest) to 31 (coarsest, smallest). The encoding mode decides how it is chosen:

  • specific filesize/bitrate (2-pass): a first pass measures the video, the second distributes a given bit rate or file size over it. The usual choice, because MPEG-4 ASP has no quality-based mode comparable to the CRF of newer encoders.
  • 2pass - 1st pass / 2pass - 2nd pass: run only one of the two passes, e.g. to repeat a second pass with an existing statistics file.
  • average bitrate (1-pass): one pass towards a bit rate; faster, with less even quality.
  • constant quantizer (1-pass): the same quantizer for the whole video; the size results from the content.

The remaining options trade encoding time against compression, or restrict the stream to what a certain class of players can decode.

Requirements

xvid_encraw, which is part of Hybrid. Xvid only encodes 8-bit YUV 4:2:0; Hybrid converts everything else before the encoder. The usual containers are AVI, MP4 and Matroska. B-frames, quarter pixel and global motion compensation belong to the Advanced Simple Profile and are not played by every hardware player.

Settings

The option column shows the xvid_encraw option a control ends up as. Several controls are locked depending on the encoding mode and the profile restriction; the tables name the conditions.

Base

Xvid Base tab
Xvid Base tab

General Settings

Control xvid_encraw option Range / values Hybrid default Meaning
Encoding mode -bitrate, -cq, -pass1, -pass2 specific filesize/bitrate (2-pass), specific filesize/bitrate (2pass - 1st pass), specific filesize/bitrate (2pass - 2nd pass), average bitrate (1-pass), constant quantizer (1-pass) specific filesize/bitrate (2-pass) Rate control mode, see above. Constant quantizer switches the profile restriction and adaptive quantization off and locks the Zones tab.
Quantization strength -cq 1 – 31 3 Quantizer of the constant quantizer mode; lower is better and bigger. Only shown in that mode.
Target mode – custom, 1/4 CD … 1 BD 50 custom Common media sizes as target for the file size. custom lets you enter a size or a bit rate yourself.
Target size (MiB) -bitrate 0.2 MiB and up 700 Aim at a file size: Hybrid computes the video bit rate from the size, the length and the size of audio and other streams. Only for the 2-pass modes; with a bit rate selected the field shows the resulting size.
Bitrate (kbit/s) -bitrate 16 – 245760 1500 (selected) Target bit rate of the 2-pass and the average bitrate modes. A profile restriction lowers the maximum.
Fast 1st pass -turbo (on), -full1pass (off) on/off on Runs the first pass with xvid_encraw's turbo presets, which is much faster and hardly changes the result of the second pass. Only available in the 2-pass mode and the 1st-pass mode.
Discard first pass -o NUL on/off on The first pass writes no video (-o NUL). Off, it writes its video, which Hybrid removes unless intermediate files are kept. Only available in the 2-pass mode and the 1st-pass mode.
Threads -threads 0 – 16 0 Number of encoding threads. With 0 Hybrid passes the number of logical CPU cores.

Restriction and Video Buffer Verifier (VBV)

Control xvid_encraw option Range / values Hybrid default Meaning
Restrict to -vbvsize, -vbvmax, -vbvpeak off; Simple Profile Level 0 – 3, Advanced Simple Profile Level 0 – 5, DivXNetworks Handheld, Portable NTSC, Portable PAL, Home Theater NTSC, Home Theater PAL, HDTV off (unrestricted) Restricts the encode to an MPEG-4 profile and level or to one of the DivX device profiles. Hybrid then sets the buffer values below and locks what the profile does not allow: no B-frames, no adaptive quantization and no interlacing in the Simple Profile; only the H.263 quantization type, no quarter pixel and no global motion compensation in the Simple and DivX profiles; at most one B-frame in the DivX profiles (two in HDTV, none in Handheld). Not available in constant quantizer mode.
Buffer size, Max bitrate, Bitrate peak -vbvsize, -vbvmax, -vbvpeak set by the profile 0 Size of the decoder buffer, the highest rate it is filled with and the highest rate over one second. The fields only show the values of the selected profile and cannot be edited; a peak is only set by the DivX profiles. Without a restriction none of the three options is written.

Frames

Xvid Frames tab
Xvid Frames tab

General Frame Settings

Control xvid_encraw option Range / values Hybrid default Meaning
Interlaced encoding -interlaced on/off off Encodes the video as interlaced, so that fields stay separate instead of being coded as one progressive picture. The field order comes from the source scan type (-interlaced 1 bottom field first, -interlaced 2 top field first). Not available in the Simple Profile and in the DivX Handheld and Portable profiles.
Closed group of pictures -noclosed_gop (when off) on/off on With a closed GOP no B-frame refers to a frame of the neighbouring GOP, which keeps every GOP decodable on its own. Leave it on unless every bit counts.
Frame drop ratio (%) -drop 0 – 100 0 Lets the encoder drop frames that hardly differ from the previous one. Above 0 this quickly costs smoothness; leave it at 0. Only used without B-frames.
Max GOP size -max_key_interval 1 – 10000 250 Longest distance between two key frames. Larger values compress better, smaller ones make seeking faster; ten times the frame rate is a common value. xvid_encraw's own default is 300.

B-Frame Settings

Control xvid_encraw option Range / values Hybrid default Meaning
Maximum number of consecutive b-frames -max_bframes 0 – 16 2 Upper limit of B-frames in a row; 0 switches B-frames off (Simple Profile compatible). More than 2 rarely helps and is not played by many devices. The profile restriction limits or locks the value.
B-frame ratio -bquant_ratio 0 – 1000 150 Quantizer of a B-frame as a percentage of the average quantizer of the surrounding P-frames; 150 means 1.5 times. Higher saves bits in the B-frames. Needs B-frames.
B-frame offset -bquant_offset -1000 – 1000 100 Fixed amount added to the B-frame quantizer on top of the ratio, in hundredths; 100 adds 1. Needs B-frames.
Packed bitstream -nopacked (when off) on/off off Packed bitstream: stores a B-frame together with the following frame in one chunk, a workaround for AVI and old DivX players. It upsets the timing in other containers, so leave it off unless a device needs it. Needs B-frames.

Quantization

Xvid Quantization tab
Xvid Quantization tab

Quantization Settings

Control xvid_encraw option Range / values Hybrid default Meaning
Type -qtype H.263, MPEG, Custom H.263 Quantization type. H.263 (0) quantizes all frequencies alike and gives a slightly softer picture that suits low bit rates. MPEG (1) uses a matrix that keeps more detail and suits high bit rates. Custom is the MPEG type with your own matrix. Simple and DivX profiles only allow H.263.
I-frame min/max -imin, -imax 1 – 31 each 1 / 31 Lowest and highest quantizer for key frames. Raising the minimum stops the encoder from wasting bits on very easy frames; lowering the maximum protects quality at the price of missing the target size. Not available in constant quantizer mode. xvid_encraw's own minimum is 2.
P-frame min/max -pmin, -pmax 1 – 31 each 1 / 31 The same for P-frames, which make up most of the stream.
B-frame min/max -bmin, -bmax 1 – 31 each 1 / 31 The same for B-frames. Needs B-frames.
Trellis quantization -notrellis (when off) on/off on Picks the quantized coefficients by weighing the error against the bits they cost instead of plain rounding. Better compression for a little time.
Adaptive quantization -masking none, lumi, variance none Varies the quantizer inside a frame (0, 1, 2): lumi quantizes very dark and very bright blocks coarser, variance decides by how busy a block is. Not available in the Simple Profile; reset to none in constant quantizer mode.
Custom Matrix -qmatrix file empty Matrix file for the type Custom; the button opens a file dialog. Only available with Type Custom.

Motion Estimation and Compensation

Control xvid_encraw option Range / values Hybrid default Meaning
Motion search precision -quality None, Very Low, Low, Medium, High, Very High, Ultra High Ultra High How thorough the motion search is (0 – 6). Lower levels are faster and compress worse; None switches the search off.
VHQ -vhqmode Off, Mode Decision, Limited Search, Medium Search, Wide Search Limited Search Rate-distortion optimized decisions (0 – 4): the encoder tries several ways to code a macroblock and keeps the cheapest; the search levels also refine the motion vectors that way. Each level is slower. xvid_encraw's own default is 1.
VHQ for b-frames -bvhq on/off on Uses the rate-distortion optimization for B-frames as well. Written only with VHQ on and B-frames in use.
Chroma motion estimation -nochromame (when off) on/off on Includes the color planes in the motion search instead of luma only; slightly better and slightly slower.
Quarter pixel motion compensation -qpel on/off off Motion vectors with quarter instead of half pixel precision. Helps with detailed, slow motion, but many hardware players do not support it. Not available in the Simple and DivX profiles.
3-point global motion compensation -gmc on/off off Describes pans, zooms and rotations as one motion of the whole frame. Saves bits with camera movement; poorly supported by hardware players. Not available in the Simple and DivX profiles.

Rate Control

Xvid Rate Control tab
Xvid Rate Control tab

The RateControl box is locked in constant quantizer mode. In the average bitrate mode it shows the three single-pass controls instead of the key-frame controls.

RateControl

Control xvid_encraw option Range / values Hybrid default Meaning
Key-frame boost (%) -kboost 0 – 200 10 Extra bits for key frames in the second pass. All following frames of the GOP are predicted from them, so a moderate boost pays off.
Key-frames closer than (frames), ... are reduced by (%) -kthresh, -kreduction 1 – 250, 0 – 250 1 / 20 Key frames that follow each other closer than the threshold get fewer bits, by the given percentage. Stops fast cuts from eating the budget.
Reaction delay factor -reaction 0 – 1000 16 Single pass only: how fast the rate control reacts to a deviation from the target. Low values hold the bit rate, high values hold the quality.
Averaging period -averaging 0 – 1000 100 Single pass only: number of frames over which the bit rate is averaged. Longer gives steadier quality and a looser bit rate.
Smoother -smoother 0 – 1000 100 Single pass only: size of the smoothing buffer that damps quantizer changes from frame to frame.
High bitrate scenes degradation (%) -chigh 0 – 100 0 Curve compression: takes this share of bits away from scenes that need more than the average.
Low bitrate scenes improvement (%) -clow 0 – 100 0 Curve compression: gives scenes that need fewer bits than the average this much more.
Statistics file -pass1, -pass2 file empty Statistics file of the 2-pass modes. Empty uses a temporary file that Hybrid removes after the second pass; set a name to keep it or to run the passes separately. Not available in the single-pass modes.

Overflow Treatment

The box is only available in the 2-pass mode and the 2nd-pass mode. The overflow is the difference between the bits planned and the bits actually used so far.

Control xvid_encraw option Range / values Hybrid default Meaning
Overflow control strength (%) -ostrength 0 – 1000 10 How hard the second pass corrects a deviation from the planned size. Higher hits the size more exactly and makes quality less even. xvid_encraw's own default is 5.
Max overflow improvement (%) -oimprove 0 – 1000 10 Upper limit for the extra bits a frame may get while the encode is below its plan. xvid_encraw's own default is 5.
Max overflow degradation (%) -odegrade 0 – 99 10 Upper limit for the bits a frame may lose while the encode is above its plan. xvid_encraw's own default is 5.

Zones

Xvid Zones tab
Xvid Zones tab

A zone gives a part of the video its own treatment, e.g. fewer bits for the credits. A zone starts at its start frame and ends where the next zone starts or at the end of the video. Enter the start frame, the type and the value, then add the zone to the list with the plus button; the arrow takes the selected zone back into the fields for editing, minus removes it. Hybrid passes the list as -zones; the tab is locked in constant quantizer mode.

Type xvid_encraw zone mode Meaning of the value
fixed quantizer q Quantizer for the zone (up to 31); lower is better and bigger.
bitrate modifier w Weight on the bit rate of the zone: 1.0 changes nothing, 0.5 halves it, 2.0 doubles it. What is saved is spent in the rest of the video, the target size stays.

The Configuration row below the tabs saves, loads and deletes named sets of all Xvid settings.

Example

2-pass with 1500 kbit/s, Fast 1st pass, 2 B-frames, H.263 quantization, VHQ Limited Search on a 10-bit YUV 4:2:0 source (640x352, 25 fps). First pass:

vspipe "encodingTempSynthSkript.vpy" - --outputindex 0 -c y4m --progress | ffmpeg -y -loglevel fatal -noautorotate -nostdin -threads 8 -f yuv4mpegpipe -i - -an -sn -pix_fmt yuv420p -fps_mode auto -r 25/1 -f rawvideo - | xvid_encraw -turbo -bitrate 1500 -quality 6 -qtype 0 -masking 0 -imin 1 -imax 31 -pmin 1 -pmax 31 -bmin 1 -bmax 31 -max_key_interval 250 -kboost 10 -kthresh 1 -kreduction 20 -max_bframes 2 -bquant_ratio 150 -bquant_offset 100 -vhqmode 2 -bvhq -chigh 0 -clow 0 -ostrength 10 -oimprove 10 -odegrade 10 -par 1:1 -nopacked -threads 32 -pass1 "test.stats" -progress 2 -frames -1 -w 640 -h 352 -csp i420 -framerate 25.000 -o NUL

Second pass (same input pipe):

xvid_encraw -bitrate 1500 -quality 6 -qtype 0 -masking 0 -imin 1 -imax 31 -pmin 1 -pmax 31 -bmin 1 -bmax 31 -max_key_interval 250 -kboost 10 -kthresh 1 -kreduction 20 -max_bframes 2 -bquant_ratio 150 -bquant_offset 100 -vhqmode 2 -bvhq -chigh 0 -clow 0 -ostrength 10 -oimprove 10 -odegrade 10 -par 1:1 -nopacked -threads 32 -pass2 "test.stats" -progress 2 -frames -1 -w 640 -h 352 -csp i420 -framerate 25.000 -o "video.m4v"

How Hybrid calls it

  • xvid_encraw reads raw video from a pipe. The filter script (Vapoursynth or AviSynth) is piped through ffmpeg, which turns it into raw 8-bit 4:2:0 video; frame size, color format and frame rate are passed with -w, -h, -csp i420 and -framerate.
  • The 10-bit source of the example is converted to 8 bit in the script, because Xvid encodes nothing else.
  • xvid_encraw writes a raw MPEG-4 stream (.m4v); Hybrid muxes it into the chosen container in a separate job.
  • Most options are always written, whether they equal xvid_encraw's defaults or not. Switches are only written when they deviate: -notrellis, -nochromame, -noclosed_gop, -nopacked, -qpel, -gmc, -interlaced.
  • In the 2-pass mode Hybrid creates one job per pass. The first pass writes no video (-o NUL) when Discard first pass is on; the statistics file is removed after the second pass. The 1st-pass mode writes -pass1, the 2nd-pass mode -pass2.
  • The key-frame options -kboost, -kthresh and -kreduction are left out in the single-pass modes, the B-frame quantizer options without B-frames.
  • The pixel aspect ratio is always written (-par) and comes from Crop/Resize. MPEG-4 stores each side in 8 bit, so Hybrid reduces ratios with a side above 255.
  • -interlaced is written with the field order of the source: 1 for bottom field first, 2 otherwise.

Background

Profiles and players

Stand-alone players state what they play as a profile: Simple Profile (no B-frames), Advanced Simple Profile (B-frames, quarter pixel, global motion compensation, MPEG quantization, interlacing) or one of the DivX profiles (Home Theater and the like), which allow B-frames but neither quarter pixel nor global motion compensation and limit frame size and bit rate. When encoding for such a device, use Restrict to and keep the frame size within what the profile allows; for playback on a computer no restriction is needed.

H.263 or MPEG quantization

A common rule: H.263 for strong compression (around 1 CD per film), MPEG or a custom matrix for high bit rates, where the extra detail is worth the bits. The two types cannot be mixed within one encode here.

Sources