[FFmpeg-devel] [RFC] Bug in colorspace.c

Hendrik Leppkes h.leppkes at gmail.com
Wed Jun 29 10:50:29 EEST 2022


On Wed, Jun 29, 2022 at 6:34 AM Hendrik Leppkes <h.leppkes at gmail.com> wrote:
>
> On Tue, Jun 28, 2022 at 11:38 PM Soft Works <softworkz at hotmail.com> wrote:
> >
> > Hi,
> >
> > in colorspace.c, there are two functions with contradicting behavior
> > regarding reading/writing the max_luminance value from/to
> > AVMasteringDisplayMetadata. The code seems to be unchanged since
> > 3 years:
> >
> >
> > 1. ff_determine_signal_peak()
> >
> >     sd = av_frame_get_side_data(in, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> >     if (!peak && sd) {
> >         AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
> >         if (metadata->has_luminance)
> >             peak = av_q2d(metadata->max_luminance) / REFERENCE_WHITE;
> >     }
> >
> >
> > 2. ff_update_hdr_metadata()
> >
> >     sd = av_frame_get_side_data(in, AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
> >     if (sd) {
> >         AVMasteringDisplayMetadata *metadata = (AVMasteringDisplayMetadata *)sd->data;
> >         if (metadata->has_luminance)
> >             metadata->max_luminance = av_d2q(peak * REFERENCE_WHITE, 10000);
> >     }
> >
> >
> > The latter function writes the value as an AVRational with a denominator of
> > 10000, but the former function doesn't multiply the value by 10000.
> >
>
> These two calls round-trip just fine. The 10000 is not a forced
> denominator, divisor, or multiplier, it is the maximum denominator it
> is allowed to use to convert the floating point value to a rational.
> av_d2q will ensure the value is properly scaled.
>

To elaborate some more, AVRational is just a number, it has no
inherent "base" or "range". An AVRational of 1000/1 or 10000000/10000
represents the same value - 1000, the extra zeros have no meaning and
none should be attributed to it. Its just that, a rational value
represented as math intended - a fraction.
If any code that processes it needs the value in a particular scale,
for example the 10000 denominator scale HEVC SEI uses, then it needs
to make sure to convert it into that particular scale, as the incoming
AVRational makes absolutely no guarantees in that regard.

All code currently using eg. max_luminance in ffmpeg seems to do it
right. The AVRational is never handled directly, but typically read
through av_q2d (to convert it into a real number), or rescaled
manually to the base a format expects.
So whatever odd values you see somewhere else, its not from FFmpeg.

- Hendrik


More information about the ffmpeg-devel mailing list