How to Enable Macroblock-Tree Rate Control in FFmpeg
Macroblock-tree (MB-tree) rate control is an advanced encoding
feature in the libx264 library that optimizes video quality by tracking
how information propagates across frames over time. This article
explains how to explicitly enable or disable the -mbtree
option in FFmpeg using encoder-specific parameters, details how the
technology works, and highlights the best scenarios for its use.
Enabling MB-tree in FFmpeg
By default, the libx264 encoder in FFmpeg has macroblock-tree rate
control enabled. However, if you need to explicitly define it in your
command line or ensure it is active when using custom profiles, you must
pass the parameter through the -x264-params or
-x264opts private encoder flags.
To explicitly enable MB-tree, use the following FFmpeg command structure:
ffmpeg -i input.mp4 -c:v libx264 -x264-params mbtree=1 output.mp4Alternatively, you can use the older -x264opts
syntax:
ffmpeg -i input.mp4 -c:v libx264 -x264opts mbtree=1 output.mp4Disabling MB-tree in FFmpeg
There are specific cases where MB-tree can introduce compression
artifacts, such as during dark scenes or heavy film grain. To disable
MB-tree, set the parameter to 0 or use
no-mbtree:
ffmpeg -i input.mp4 -c:v libx264 -x264-params mbtree=0 output.mp4How Macroblock-Tree Rate Control Works
MB-tree rate control works by analyzing the temporal dependencies between frames in a video sequence. It tracks how individual macroblocks (blocks of pixels) move from frame to frame.
- High-dependency blocks: If a macroblock remains on screen for a long time (such as a static background), MB-tree allocates more bitrate to it. Improving the quality of this “source” block ensures that all subsequent frames referencing it also look better.
- Low-dependency blocks: If a macroblock changes quickly or disappears (such as fast-moving objects or camera cuts), MB-tree reduces its bitrate allocation, saving data for more critical parts of the video.
This dynamic allocation significantly improves overall perceived video quality, especially at lower bitrates.
When to Keep MB-tree Enabled
You should keep macroblock-tree rate control enabled for: * Standard video encodes: Movies, TV shows, and screen recordings benefit greatly from MB-tree’s bitrate efficiency. * Low-bitrate streaming: It maximizes visual fidelity when bandwidth is constrained. * High-latency encodes: Multi-pass encoding workflows where compression efficiency is prioritized over encoding speed.
When to Disable MB-tree
You should disable MB-tree (by setting mbtree=0) for: *
Film grain preservation: When using
-tune grain, FFmpeg automatically disables MB-tree because
the algorithm tends to smooth out fine, moving grain textures. *
Lossless or near-lossless encoding: If you are encoding
at very low Constant Rate Factor (CRF) values (e.g., CRF < 16),
MB-tree’s bit distribution offers diminishing returns. *
Real-time, ultra-low-latency streaming: MB-tree
requires looking ahead at future frames, which adds processing
latency.