AOMediaCodec / libavif

libavif - Library for encoding and decoding .avif files
Other
1.53k stars 195 forks source link

avifImageRGBToYUV should set color space to default values #2099

Open rzumer opened 5 months ago

rzumer commented 5 months ago

When converting RGB to YCbCr with libavif, the default color properties are known and mentioned here (sRGB with BT.601 coefficients), but those properties are not set in the AVIF image structure, so unless the user explicitly sets the color space, decoders tend to assume BT.709 and distort the colors. In my opinion libavif should set those fields since it has to make assumptions when converting from RGB anyway.

vrabaud commented 5 months ago

Hi, according to the spec (https://github.com/AOMediaCodec/libavif/blob/98ce107ee731a80894e389b26f972e48e82a259d/src/colr.c#L150), leaving it as undefined is fine as it should be interpreted as BT.601. Which decoders default to BT.709 ? They seem to not respect the spec.

rzumer commented 5 months ago

FFmpeg libraries keep the color space parameters as unspecified instead of overriding the values to the specified defaults which I think is reasonable if conservative. Maybe it has special-case handling for color-space conversion operations from AVIF internally, but if FFmpeg is used as the decoder and another library performs the color space conversion then having special default color spaces based on the format is impractical (when input is not constrained).

In my case I only ingest files converted from RGB with libavif, so the omission feels like a trap. The encode example isn't explicit about setting the correct color space parameters when converting from RGB, and the defaults are buried in a wiki page (or spec I guess). If there are specific situations where you would not want to set color space parameters when performing the conversion through libavif then I can understand the omission and would just advise setting the defaults in the RGB path of examples/avif_example_encode.c, but if not it just seems like good hygiene to be explicit about the color space when libavif performs the conversion.