Understanding Video Color Spaces, Gamma Correction, and Transcoding with FFmpeg
Video processing involves converting linear sensor data through gamma correction and multiple color‑space transformations—such as RGB, YUV, and XYZ—using standards like BT.601/709/2020, with FFmpeg’s colorspace filter and ffprobe to manage transfer functions, primaries, and ranges during transcoding to preserve accurate colors across devices.
In video processing, multiple color spaces such as non‑linear RGB, linear RGB, YUV, and XYZ are used. Different stages of a camera pipeline—optics, image sensor (CCD/CMOS), charge accumulation, ADC conversion, and ISP processing—require specific color attributes (color_space, color_transfer, color_primaries) to describe the data.
The image sensor converts incoming photons to electrons, accumulates charge during exposure, and then outputs a voltage that the ADC digitizes into integer values (e.g., 8‑bit range 0‑255). This digitized signal is linear with respect to light intensity.
Linear data from raw files appears dark and low‑contrast. Human vision perceives brightness logarithmically, so a non‑linear gamma curve is applied to map linear values to a perceptually uniform space. The ISP performs gamma correction, converting linear RGB to non‑linear RGB suitable for display.
Because storage is expensive, cameras often convert linear RGB to YUV and apply chroma subsampling (e.g., YUV420) before compression. Common conversion standards include BT.601 (SD), BT.709 (HD), and BT.2020 (UHD). These standards define both the YUV↔RGB matrix and the gamma curve used for linear↔non‑linear RGB conversion.
FFmpeg implements these conversions via the colorspace filter, which uses the AVFrame fields colorspace , color_trc , and color_primaries to store the YUV/RGB matrix, the transfer function, and the RGB↔XYZ matrix respectively. The range option (limited, full, tv, pc, jpeg) describes the valid value range for each component.
Example command to inspect a video file:
$ ffprobe -select_streams v:0 -show_entries stream=color_space,color_transfer,color_primaries test.mp4
[STREAM]
color_space=bt2020nc
color_transfer=arib-std-b67
color_primaries=bt2020
[/STREAM]The arib-std-b67 transfer corresponds to the HLG (Hybrid Log‑Gamma) curve.
During transcoding, source video may need color‑space conversion (e.g., BT.601 → BT.709) and gamma adjustment to match the target display’s gamut. Color management ensures that RGB values are transformed through a device‑independent linear space (XYZ) before being mapped to the destination RGB gamut.
Display devices use RGB but each device may support a different gamut (sRGB, Rec.2020, etc.). When the source and display gamuts differ, an additional color‑management step is required to avoid color distortion.
For human‑centric editing, the HSL model (Hue, Saturation, Lightness) is often preferred because it aligns with visual perception. FFmpeg’s signalstats filter provides raw tone, saturation, and luminance data, but its calculations differ from standard HSL. A custom vf_hsl filter can be used to obtain true HSL values.
Overall, the workflow from video capture to consumption involves multiple color‑space conversions, gamma corrections, and optional transcoding steps to ensure consistent visual quality across different devices and delivery formats.
Baidu Geek Talk
Follow us to discover more Baidu tech insights.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.