Two pass encoding:
Above,
it was suggested to always use two pass encoding, but there are still
reasons for not using it. For instance, if you are capturing live TV
and encoding in realtime, you are forced to use single-pass. Also, one
pass is obviously faster than two passes; if you use the exact same set
of options on both passes, two pass encoding is almost twice as slow.
Still,
there are very good reasons for using two pass encoding. For one thing,
single pass ratecontrol is not psychic, and it often makes unreasonable
choices because it cannot see the big picture. For example, suppose you
have a two minute long video consisting of two distinct halves. The
first half is a very high-motion scene lasting 60 seconds which, in
isolation, requires about 2500kbps in order to look decent. Immediately
following it is a much less demanding 60-second scene that looks good
at 300kbps. Suppose you ask for 1400kbps on the theory that this is
enough to accomodate both scenes. Single pass ratecontrol will make a
couple of “mistakes“ in such a case. First of all, it will target
1400kbps in both segments. The first segment may end up heavily
overquantized, causing it to look unacceptably and unreasonably blocky.
The second segment will be heavily underquantized; it may look perfect,
but the bitrate cost of that perfection will be completely
unreasonable. What is even harder to avoid is the problem at the
transition between the two scenes. The first seconds of the low motion
half will be hugely over-quantized, because the ratecontrol is still
expecting the kind of bitrate requirements it met in the first half of
the video. This “error period“ of heavily over-quantized low motion
will look jarringly bad, and will actually use less than the 300kbps it
would have taken to make it look decent. There are ways to mitigate the
pitfalls of single-pass encoding, but they may tend to increase bitrate
misprediction.
Multipass ratecontrol can offer huge advantages
over a single pass. Using the statistics gathered from the first pass
encode, the encoder can estimate, with reasonable accuracy, the “cost“
(in bits) of encoding any given frame, at any given quantizer. This
allows for a much more rational, better planned allocation of bits
between the expensive (high-motion) and cheap (low-motion) scenes. See
qcomp below for some ideas on how to tweak this allocation to your
liking.
Moreover, two passes need not take twice as long as one
pass. You can tweak the options in the first pass for higher speed and
lower quality. If you choose your options well, you can get a very fast
first pass. The resulting quality in the second pass will be slightly
lower because size prediction is less accurate, but the quality
difference is normally much too small to be visible. Try, for example,
adding subq=1:frameref=1 to the first pass x264encopts. Then, on the
second pass, use slower, higher-quality options:
subq=6:frameref=15:partitions=all:me=umh