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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions siti_tools/siti.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

frame_data = np.minimum(frame_data, 1.0)

a = 0.17883277
b = 0.02372241
c = 1.00429347
gamma = 1.2
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).


frame_data = (frame_data <= 0.5) * (np.power(frame_data, 2.0) / 3.0) + (
frame_data > 0.5
) * (np.exp((frame_data - c) / a) - b)

frame_data = (l_max - l_min) * np.power(frame_data, gamma - 1.0) + l_min
# NOTE: The following is only valid when the function is applied to the Y channel.
# 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.


return frame_data

Expand Down
18 changes: 9 additions & 9 deletions test/ground_truth/fall-hlg.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"si": [
11.60486742434359,
11.348906881585735,
11.550420779275415,
11.302442575496784,
11.53252417160579
56.859746855102614,
55.967538925996365,
56.31768629091642,
55.72770870085969,
56.65520632442709
],
"ti": [
1.8983682229673808,
2.222959198823672,
2.006899224083598,
1.8976135425959544
9.282396642166805,
10.913202661020888,
9.818995292103207,
9.34710188339459
],
"settings": {
"calculation_domain": "pq",
Expand Down
18 changes: 9 additions & 9 deletions test/ground_truth/lights-hlg.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"si": [
8.498430920800487,
8.321574730146757,
8.408620911280588,
8.276614669711085,
8.502094759638036
38.29655050751715,
37.94350168275566,
38.24326954168339,
37.92370519326262,
38.491130718383644
],
"ti": [
0.885369996432793,
0.8380038034866518,
0.840396082605608,
0.8602735745148169
3.6567758609168197,
3.5211110820450493,
3.5537786270630987,
3.599255913659733
],
"settings": {
"calculation_domain": "pq",
Expand Down
18 changes: 9 additions & 9 deletions test/ground_truth/sunset-hlg.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"si": [
0.7738618954474061,
0.7693658477258258,
0.8061181693471522,
0.8313873320178494,
0.8783931251175974
3.9986325451625366,
3.945613616354972,
4.138035019599985,
4.246972345956456,
4.48982814719558
],
"ti": [
1.0383126165513015,
1.0963192748625727,
1.1724608964066017,
1.2326597542789979
5.477032037410652,
5.759358031721984,
6.133345611005933,
6.424600294368263
],
"settings": {
"calculation_domain": "pq",
Expand Down
Loading