Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 20th November 2022, 16:45   #201  |  Link
Alexkral
Registered User
 
Join Date: Oct 2018
Posts: 319
May I ask why the default pct_hdr_skin for PQMode in ConvertXYZ_BT2446_C_HDRtoSDR is set to 0.44? I haven't seen anything in ITU-R BT.2408 regarding this, only about HLG.
__________________
AviSynth AiUpscale
Alexkral is offline   Reply With Quote
Old 20th November 2022, 19:03   #202  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
I don't remember exactly... But if indeed there is nothing in BT.2408, i probably "estimate" it using informations from several BT, and i probably choose this value because "it seems not too bad".
There is probably some curve or anything which lead me to this choice.
__________________
My github.
jpsdr is offline   Reply With Quote
Old 20th November 2022, 20:45   #203  |  Link
Alexkral
Registered User
 
Join Date: Oct 2018
Posts: 319
Ah ok, I think I know why you did it. 44% PQ equals 50 nits, same as 50% HLG. It doesn't seem too bad indeed.
__________________
AviSynth AiUpscale
Alexkral is offline   Reply With Quote
Old 20th November 2022, 22:49   #204  |  Link
Alexkral
Registered User
 
Join Date: Oct 2018
Posts: 319
I found something funny looking at your code, not that it's in the least important, but I think I should let you know. You use 2.404 as the power in the BT.1886 EOTF, but the last 4 is actually a footnote . It's not that it's important as I say, but when I found the reason it made me laugh.

Also, I'm not too sure that the inverse OETF should be used, ITU repeats all over the place something that seems to imply this opinion, but I can understand why you do it.
__________________
AviSynth AiUpscale
Alexkral is offline   Reply With Quote
Old 21st November 2022, 01:43   #205  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
Thanks for the 2.404, i'll change this later. My guess is that at the time i read it, i didn't see the footnotes and thought it was misprint...
__________________
My github.
jpsdr is offline   Reply With Quote
Old 21st November 2022, 19:30   #206  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
New version, see first post.
__________________
My github.
jpsdr is offline   Reply With Quote
Old 26th February 2023, 12:36   #207  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
New version, see first post.
__________________
My github.
jpsdr is offline   Reply With Quote
Old 4th June 2023, 21:15   #208  |  Link
LouieChuckyMerry
Registered User
 
LouieChuckyMerry's Avatar
 
Join Date: Feb 2014
Posts: 355
Color Conversion: How To Tonemap BT.2020 To BT.709

First, many thanks jpsdr for all your effort .

I was gifted a UHD Blu-ray but don't have a 4K TV, so I figured I'd shrivel it to 1080p using AviSynth after color converting and resizing losslessly with VirtualDub2. Unfortunately, after hours of effort, I can't figure out how to tonemap BT.2020 to BT.709. I'm here because it seems to me that that's what your HDRTools plugin is for but using

Code:
ConvertYUVToXYZ(Color=1)
ConvertXYZToYUV(pColor=1)
as in your example runs fine but appears to do nothing. Please, what am I missing?

Ahhh, a video sample.

Last edited by LouieChuckyMerry; 4th June 2023 at 21:18. Reason: Link To Test Clip
LouieChuckyMerry is offline   Reply With Quote
Old 5th June 2023, 12:03   #209  |  Link
FranceBB
Broadcast Encoder
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Royal Borough of Kensington & Chelsea, UK
Posts: 2,883
Quote:
Originally Posted by LouieChuckyMerry View Post
Unfortunately, after hours of effort, I can't figure out how to tonemap BT.2020 to BT.709.
Try this to go from BT2020 HDR PQ to BT709 with HDRTools:

Code:
ConvertBits(16)

ConvertYUVtoXYZ(Color=0, OutputMode=1, HDRMode=0, fullrange=false)

ConvertXYZ_Reinhard_HDRtoSDR(exposure_X=2.5, contrast_X=0.9)

ConvertXYZtoYUV(pColor=0)

Converttoyuv420()
If the conversion is fine but looks a bit off, change Reinhard's parameters accordingly 'till you're ok with the end result.
FranceBB is offline   Reply With Quote
Old 6th June 2023, 00:48   #210  |  Link
LouieChuckyMerry
Registered User
 
LouieChuckyMerry's Avatar
 
Join Date: Feb 2014
Posts: 355
Thanks for your reply, FranceBB. Your suggested script runs fine but doesn't appear to change the video in any way; i.e., the output looks the same as the input, dull and muted (the same-looking result as the script I posted above).
LouieChuckyMerry is offline   Reply With Quote
Old 6th June 2023, 07:05   #211  |  Link
Boulder
Pig on the wing
 
Boulder's Avatar
 
Join Date: Mar 2002
Location: Finland
Posts: 5,718
If you have an nVidia GPU available, you can use DGHDRtoSDR to do the grading in one simple step. DGIndexNV can be used to tune the parameters per source.
__________________
And if the band you're in starts playing different tunes
I'll see you on the dark side of the Moon...
Boulder is offline   Reply With Quote
Old 6th June 2023, 11:58   #212  |  Link
FranceBB
Broadcast Encoder
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Royal Borough of Kensington & Chelsea, UK
Posts: 2,883
Quote:
Originally Posted by LouieChuckyMerry View Post
Thanks for your reply, FranceBB. [...] the output looks dull and muted
Well, I wouldn't call it "Dull and Muted", but ok xD

Anyway, here's a comparison:




This is done with the following script:

Code:
FFVideoSource("D:\TestClip2[2160p].mkv")
SinPowerResize(848, 480)
original=last.Text("BT2020 HDR PQ 1043 nits - Original")

ConvertBits(16)
hbd=last
ConvertYUVtoXYZ(Color=0, OutputMode=1, HDRMode=0, fullrange=false)
ConvertXYZ_Reinhard_HDRtoSDR(exposure_X=2.5, contrast_X=0.9)
ConvertXYZtoYUV(pColor=0)
Converttoyuv420()
ConvertBits(bits=10, dither=1)
Text("BT709 SDR 100 nits - Reinhard tonemapping")
tonemapped=last


ConvertToPlanarRGB(hbd, matrix="Rec2020", interlaced=false)
Cube("C:\Programmi\AviSynth+\LUTs\PQ_to_BT709_v2.cube", fullrange=true)
Converttoyuv420()
ConvertBits(bits=10, dither=1)
Text("BT709 SDR 100 nits - FranceBB LUT")
lutted=last


StackVertical(original, tonemapped, lutted)

If you like the last one, then I strongly suggest you to check this out: https://forum.doom9.org/showthread.php?t=176091
FranceBB is offline   Reply With Quote
Old 7th June 2023, 01:58   #213  |  Link
LouieChuckyMerry
Registered User
 
LouieChuckyMerry's Avatar
 
Join Date: Feb 2014
Posts: 355
Boulder: Thanks for your reply. I do have an nVidia GPU and a license for DGIndexNV, so I'll attack your suggestion ASAP.
_____________

FranceBB: Thanks again for your help. Your latest suggestion looks good but I'll need a goodly stretch of free time to wrap my head around it.
_____________

I should have time this weekend to work on the above, possibly (but doubtfully) earlier. I really appreciate the help .
_____________

Edit: FranceBB: I took a quick look at your last link and, wow, that's really cool! Of course, I understand it intellectually but not really any of the details .

Last edited by LouieChuckyMerry; 7th June 2023 at 18:46. Reason: That's Cool
LouieChuckyMerry is offline   Reply With Quote
Old 20th November 2023, 21:52   #214  |  Link
jpsdr
Registered User
 
Join Date: Oct 2002
Location: France
Posts: 2,309
New version, see first post, but nothing big...
__________________
My github.
jpsdr is offline   Reply With Quote
Old 18th March 2024, 09:35   #215  |  Link
rgr
Registered User
 
Join Date: Jun 2022
Posts: 30
Quote:
Originally Posted by FranceBB View Post
ConvertToPlanarRGB(hbd, matrix="Rec2020", interlaced=false)
Cube("C:\Programmi\AviSynth+\LUTs\PQ_to_BT709_v2.cube", fullrange=true)
Converttoyuv420()
ConvertBits(bits=10, dither=1)
Text("BT709 SDR 100 nits - FranceBB LUT")
lutted=last


StackVertical(original, tonemapped, lutted)
[/CODE]


If you like the last one, then I strongly suggest you to check this out: https://forum.doom9.org/showthread.php?t=176091
The latter worked great when playing the script through MPC-HC, but after conversion through ffmpeg it's still HDR -- there must be something missing in the script somewhere.

Code:
Video
ID                             : 1
Format                         : AVC
Format/Info                    : Advanced Video Codec
Format profile                 : High@L4
Format settings                : CABAC / 4 Ref Frames
Format settings, CABAC         : Yes
Format settings, Reference fra : 4 frames
Codec ID                       : V_MPEG4/ISO/AVC
Duration                       : 3 h 0 min
Width                          : 1 920 pixels
Height                         : 1 080 pixels
Display aspect ratio           : 16:9
Frame rate mode                : Constant
Frame rate                     : 23.976 (24000/1001) FPS
Color space                    : YUV
Chroma subsampling             : 4:2:0
Bit depth                      : 8 bits
Scan type                      : Progressive
Writing library                : x264 core 164 r3179 12426f5
Default                        : No
Forced                         : No
Color range                    : Limited
Color primaries                : BT.2020
Transfer characteristics       : PQ
Matrix coefficients            : BT.601

ffmpeg sets such data in the output:
Stream #0:0: Video: h264 (H264 / 0x34363248), yuv420p(tv, smpte170m/bt2020/smpte2084, progressive), 1920x1080, q=2-31, 23.98 fps, 1k tbn

Last edited by rgr; 18th March 2024 at 09:43.
rgr is offline   Reply With Quote
Old 18th March 2024, 10:35   #216  |  Link
FranceBB
Broadcast Encoder
 
FranceBB's Avatar
 
Join Date: Nov 2013
Location: Royal Borough of Kensington & Chelsea, UK
Posts: 2,883
Welcome to the world of frame properties, rgr!
No, jokes aside, it's not actually HDR but the reason why ffmpeg is reading it as HDR is that the frame properties are not updated.
When I wrote the script initially, we were in simpler times, when Avisynth only had the good old clip properties.

Anyway, to signal the right properties to ffmpeg you need to set them manually, so your script would become:


- For HDR Tools

Quote:
#Indexing PQ source
video=LWLibavVideoSource("D:\TestClip.mxf")
audio=LWLibavVideoSource("D:\TestClip.mxf")
AudioDub(video, audio)

#Screw frame properties
propclearall()

#Bring everything to 16bit planar
ConvertBits(16)

#From YUV to XYZ with 16bit precision
ConvertYUVtoXYZ(Color=0, OutputMode=1, HDRMode=0, fullrange=false)

#BT2020 PQ to BT709 SDR tonemapping in XYZ with 16bit precision
ConvertXYZ_Reinhard_HDRtoSDR(exposure_X=2.5, contrast_X=0.9)

#From XYZ to YUV with 16bit precision
ConvertXYZtoYUV(pColor=0)

#Conversion to YUV 4:2:0 with 16bit precision
ConverttoYUV420(matrix="Rec709", interlaced=false)

#Dithering down to 8bit with the Floyd Steinberg Error Diffusion
ConvertBits(bits=8, dither=1)

#Limiter TV Range 0.0 - 0.7V
Limiter(min_luma=16, max_luma=235, min_chroma=16, max_chroma=240)

#Setting frame properties to make FFMpeg happy
propSet("_FieldBased", 0) #progressive
propSet("_ColorRange", 1) #Limited TV Range
propSet("_Matrix", 1) #BT709
propSet("_Transfer", 1) #BT709
propSet("_Primaries", 1) #BT709


- For LUTs


Quote:
#Indexing PQ source
video=LWLibavVideoSource("D:\TestClip.mxf")
audio=LWLibavVideoSource("D:\TestClip.mxf")
AudioDub(video, audio)

#Screw frame properties
propclearall()

#Bring everything to 16bit planar
ConvertBits(16)

#From YUV to RGB with 16bit precision
ConvertToPlanarRGB(matrix="Rec2020", interlaced=false)

#From BT2020 PQ to BT709 SDR with 16bit precision and tetrahedral interpolation
Cube("C:\Program Files (x86)\AviSynth+\LUTs\PQ_to_BT709_v2.cube", fullrange=1, interp=1)

#Conversion to YUV 4:2:0 with 16bit precision
ConverttoYUV420(matrix="Rec709", interlaced=false)

#Dithering down to 8bit with the Floyd Steinberg Error Diffusion
ConvertBits(bits=8, dither=1)

#Limiter TV Range 0.0 - 0.7V
Limiter(min_luma=16, max_luma=235, min_chroma=16, max_chroma=240)

#Setting frame properties to make FFMpeg happy
propSet("_FieldBased", 0) #progressive
propSet("_ColorRange", 1) #Limited TV Range
propSet("_Matrix", 1) #BT709
propSet("_Transfer", 1) #BT709
propSet("_Primaries", 1) #BT709


Needless to say, I hate frame properties.
Frame properties have their own utility in the sense that we can now tell other things outside of Avisynth (like the filter_builder in FFAStrans, FFMpeg, MPV etc) what we're outputting from Avisynth, but the problem is that not every function sets them correctly, so you almost always end up with the wrong ones. An easy workaround is to get rid of them all, always, at the very beginning, right after indexing, write the script as you're used to back when Avisynth didn't have them, then populate the right ones manually at the very end.
This is how I've done it for all the supply chains at work anyway, while for my personal stuff at home I use x264/x265 directly which don't really give a crap about frame properties so I'm safe from this.

Last edited by FranceBB; 18th March 2024 at 10:40.
FranceBB is offline   Reply With Quote
Old 18th March 2024, 15:26   #217  |  Link
rgr
Registered User
 
Join Date: Jun 2022
Posts: 30
Yes, I just found out.
If it already exists, it's a pity it doesn't have a pixel aspect ratio.

I also found in my notes:
"Color primaries" had an effect only on Quick Time...
"Transfer characteristics" did not have an effect on any player.
"Matrix coefficients" was the one that mattered in proper BT.601/BT.709 reproduction...

rgr is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 15:39.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.