Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the implementation of the HLG EOTF function #23

Merged
merged 2 commits into from
Jul 31, 2024

Conversation

krasuluk
Copy link
Collaborator

This PR fixes the implementation of HLG EOTF function. An important note is also added in case the function was to be used on channels other than Y.

@krasuluk krasuluk requested a review from slhck July 29, 2024 19:58
@@ -482,19 +482,24 @@ def eotf_hlg(
Returns:
frame_data: pixel values in the physical luminance up to 10,000 cd/m^2
"""
frame_data = np.maximum(frame_data, 0.0)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ITU-R Recommendation BT.2100, the values below 0 and above 1 are also possible and acceptable (see NOTE 5g).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fine, note 5h per latest version.

@krasuluk krasuluk force-pushed the hlg-eotf-fix-krasuluk branch from 799b2e0 to 8762be6 Compare July 29, 2024 20:02
if l_max <= 1000.0:
gamma = 1.2
else:
gamma = 1.2 + 0.42 * np.log10(l_max/1000.0)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See NOTE 5e in the Recommendation BT.2100

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it appears to be NOTE 5f though in the latest verson (https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2100-2-201807-I!!PDF-E.pdf).

# If frame_data was a color channel (R, G, or B), the correct implementation would be:
# (l_max - l_min) * np.power(Y, gamma - 1.0) * frame_data + l_min,
# where Y would be obtained from linearized R, G, B channels.
frame_data = (l_max - l_min) * np.power(frame_data, gamma) + l_min
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If frame_data = Y the equation in the NOTE can be written as:

(l_max - l_min) * np.power(frame_data, gamma - 1.0 + 1.0) + l_min

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part I don't see in BT.2100, but to be honest I have only looked very briefly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slhck You are right, it is not in the recommendation as it only defines the functions for the individual components. The "most correct" way would therefore be to convert the YUV input to RGB, apply the EOTF on individual components, and convert back to YUV. But I believe that is not really needed for our use case and this solution seems like a reasonable approximation. Would you agree?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yes, that makes sense. I was more confused about the subtraction gamma - 1.0 in the original code and where that came from.

@krasuluk
Copy link
Collaborator Author

@slhck the fix results in test failures, as expected. I assume those were obtained from the original implementation which I believe had a bug (brought to my attention by @cosmin). Can you double-check the proposed changes make sense? Once verified, we can fix the failing tests.

if l_max <= 1000.0:
gamma = 1.2
else:
gamma = 1.2 + 0.42 * np.log10(l_max/1000.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it appears to be NOTE 5f though in the latest verson (https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.2100-2-201807-I!!PDF-E.pdf).

@@ -482,19 +482,24 @@ def eotf_hlg(
Returns:
frame_data: pixel values in the physical luminance up to 10,000 cd/m^2
"""
frame_data = np.maximum(frame_data, 0.0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fine, note 5h per latest version.

@slhck
Copy link
Collaborator

slhck commented Jul 30, 2024

Feel free to merge and fix the tests after, or change the values before! Thanks to @cosmin and you for providing the fix!

@krasuluk krasuluk merged commit 1a15ffe into main Jul 31, 2024
2 checks passed
@slhck
Copy link
Collaborator

slhck commented Jul 31, 2024

Will do a release tomorrow, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants