Two-pass encoding and complexity estimation
An encoder working in one pass has to decide each frame's quantizer before it has seen the rest of the video, beyond a limited lookahead. If the whole file is available, it can encode twice: a first pass to measure how hard every frame is, and a second that spends a fixed budget with that knowledge. The second pass plans from estimates, not certainties, so it still corrects as it goes.
What the first pass leaves behind
x264's first pass writes a line of statistics for every frame: its type, the quantizer it used, and the bits it spent on residual, on motion and on everything else. From those x264 can estimate how a frame's size would change at another quantizer. Its complexity measure is the bits predicted at a quantizer scale of 1, without the bits for everything else, adjusted for the frame's duration. It smooths that over neighboring frames, and then looks for a single rate factor: the one number that, applied to every frame's smoothed complexity, makes the predicted total equal the target, subject to its other limits. That is the same formula constant rate factor uses, with the constant found by search and not given. While it encodes, the second pass compares the bits spent with the plan and corrects when they diverge.
Same target, one pass and two
The sequence is the one from the earlier pages of this unit: quiet, busy, then in between. Compare the earlier one-pass controller, x264's average bitrate feedback, with an ideal planner that is given the whole sequence's complexity, shown as Two passes. Both aim at the bitrate you set. The planner predicts sizes with the same formula that generates them, so it can hit the target almost exactly; real first-pass statistics only estimate second-pass sizes. As on the earlier pages, macroblock-tree is off and there are no frame types. The plots show the quantization parameter (QP) and size of each frame.
| One pass | Two passes | |
|---|---|---|
| Average, kbit/s | ||
| QP, quiet section | ||
| QP, busy section | ||
| QP, medium section |
--qcomp 0.6. x264 also multiplies each weight by a running factor from the share of intra-coded blocks, which ends the smoothing at a frame that is all intra; the demo leaves that out and smooths across every boundary. Not modeled: frame types, the further smoothing of quantizers, and the buffer limit. QP spread is the highest QP of any frame minus the lowest.At a target of 700 kbit/s:
- The planner landed on the target; one pass ended 5.2% over. 700 against 736 kbit/s after ten seconds of this invented sequence. The planner's exact hit is built into the model, and neither figure is a typical error for x264. Feedback would keep pulling the one-pass encode back over a longer video.
- One pass overspent on the quiet opening. It averaged QP 19.5 there and two passes 23.2. Having seen nothing harder, it took the quiet frames for the whole video.
- The planner's two-sided smoothing began changing QP before a change in complexity. Its QP begins to rise before the busy section starts. This example smooths across every boundary; x264's smoothing also uses intra-block statistics, so a real cut can behave differently. Its spread across the whole sequence was 4.9 QP against one pass's 11.4, which describes the quantizers chosen, not how even the result looks.
- Two passes did not make the busy section finer than the rest. It still gave it the highest QP, 27.5 against 23.2. The plan follows the same rule as constant rate factor, and that rule quantizes complex frames more coarsely.
The settings in x264
--pass 1 writes the statistics file and --pass 2 reads it. The second pass needs --bitrate.
The file, the smoothing options and constant rate factor
--statsnames the statistics file.--cplxblur(default 20) sets the extent of the complexity smoothing, and--qblur(default 0.5) smooths the resulting quantizer scales among frames of the same type.- x264 offers no second pass for constant rate factor: that mode needs no total to aim at.
Trade-offs
- Two passes cost an extra pass, and its cost depends on its settings. Unless
--slow-firstpassis given, x264 runs the first pass with faster settings: one reference frame, the diamond search, no 8×8 transform, no trellis and a decision level of at most 2, among others. Neither the time saved nor the effect on the second pass was measured here. The statistics belong to the input they were made from. - It needs the whole input. Live video cannot wait. A lookahead gives limited information about what is coming and a buffer model constrains a local budget; together they still lack the whole-file knowledge of first-pass statistics.
- It controls size and allocates quality within that size. When a particular average bitrate or size matters, the statistics help plan the budget across the sequence. When size can vary, constant rate factor is often the simpler one-pass choice. The two share the rate factor formula; they need not produce the same allocation or quality.
Sources
Read 2026-11-15, at the commit linked.
- x264 at commit c24e06c (GitHub mirror): in
encoder/ratecontrol.c, the statistics line written for each frame, complexity as bits at a quantizer scale of 1, the smoothing reach and weights, the factor from the share of intra-coded blocks, the search for the rate factor, the smoothing of quantizers within one frame type, the warning when the plan does not reach the target, the correction against the plan while encoding, the size predicted at another quantizer, the refusal of two passes without a bitrate, and for one pass the average bitrate path and its feedback term; incommon/base.c,--cplxblurand--qblurdefaults,--pass,--statsand the faster settings for a first pass; the options in the command-line list and--slow-firstpass. - Not checked: the rest of the second pass's correction beyond the lines cited; the second pass under a buffer limit; how much time the fast first pass saves or what it costs the second pass; multi-pass encoding in x265, libaom and SVT-AV1; any measurement of quality.