[FFmpeg-user] The internal ffmpeg rgb=>yuv pipeline and conversion matrices

Thomas Worth dev at rarevision.com
Mon May 9 13:33:34 CEST 2016


On Fri, May 6, 2016 at 7:04 AM, Peter Rabbitson <rabbit+list at rabbit.us>
wrote:

> I am looking to produce high quality 720p H264 streams derived from RGB
> source material. Both the source and 99.999% of the playback environments
> are 24bit RGB color platforms (pc/mobile/etc displaying a youtube/vimeo/etc
> window in a browser)
>
>
Yeah. But for all intents and purposes, you'll need to distribute some type
standard, subsampled, YCbCr (uh, I mean eh, "YUV") video because otherwise
most players won't know what the hell to do with the wacky, out-of-spec
video.

Here's what you need to do:

The encoding matrix is only 1/3 of the equation. To go from RGB to a
proper, YCbCr 4:2:2 or 4:2:0 source (which every player expects), you also
need to:

+ Subsample the chroma channels to 1/2 res (4:2:2) or 1/4 res (4:2:0).
+ Scale your luma/color ranges to "broadcast" or "studio" levels (as
opposed to "full"). That means instead of 8 bit pixel values from 0-255,
they need to be 16-235 and 16-240, respectively.

Here's your cheat sheet if you want perfect decoding of YCbCr to RGB:

1. Encode RGB to YCbCr using the Poynton-approved RGB->709 matrix
coefficients (don't mess with 601, trust me, unless you think apples should
look like oranges)
2. Subsample the chroma channels to either 4:2:2 or 4:2:0
3. Scale (don't clip) luminance to 16-235
4. Scale (don't clip) chroma to 16-240

If you don't do all those things, the video you see in some off-the-shelf
player won't look like the RGB source. Why? Because the player is doing the
reverse of steps 1-4 as it assumes the video is encoded this way. And you
usually can't modify this behavior in the player because it's the "right"
way to do it. If you left out any of the steps, you'd have a non-standard
video stream and perfect results can't be guaranteed.

As words of inspiration, despite the trouble you can absolutely get
RGB->YUV->RGB conversions that are 99.999% perfect. Not 100.000%, because
this is a lossy process. But if you leave out one step, or use the wrong
coefficients for the matrices, it won't work.


More information about the ffmpeg-user mailing list