View Full Version : Converting to "Linear Light" and back
Stereodude
26th July 2019, 14:38
I want to try some processing in linear light.
How can I convert YV12 or other YUV video to "linear light" (no gamma correction) 16-bit RGB in Avisynth+?
Then, how do I reverse the operation and convert the 16-bit "linear light" RGB back to YUV video?
Stereodude
26th July 2019, 17:19
I found this Dithertools example:
# Y'Cb'Cr' input in 16 bits
Dither_convert_yuv_to_rgb (matrix="709", output="rgb48y", lsb_in=true)
Dither_y_gamma_to_linear (tv_range_in=false, tv_range_out=false, curve="709", sigmoid=true)
Dither_resize16nr (1280, 720, kernel="spline36")
Dither_y_linear_to_gamma (tv_range_in=false, tv_range_out=false, curve="709", sigmoid=true)
r = SelectEvery (3, 0)
g = SelectEvery (3, 1)
b = SelectEvery (3, 2)
Dither_convert_rgb_to_yuv (r, g, b, matrix="709", lsb=true)
# Y'Cb'Cr' output in 16 bits
The problem here is that rgb48y is not a normal color format supported by other plugins. I think it's basically the same thing as AVIsynth+'s PlanarRGB in 16 bits, but I don't know how to convert rgb48y <--> RGBP16.
real.finder
26th July 2019, 17:24
I port that to work with avs26/avs+ in ResizeX (https://forum.doom9.org/showthread.php?p=1837361&posted=1#post1837361) (still not use it in ResizeX functions though)
y_gamma_to_linear = Dither_y_gamma_to_linear
y_linear_to_gamma = Dither_y_linear_to_gamma
from what I read you should use it with 8 < bits (10 bits or more) even if it work with 8bit
StvG
26th July 2019, 18:20
I want to try some processing in linear light.
How can I convert YV12 or other YUV video to "linear light" (no gamma correction) 16-bit RGB in Avisynth+?
Then, how do I reverse the operation and convert the 16-bit "linear light" RGB back to YUV video?
avsresize?
z_ConvertFormat(pixel_type="RGBPS",colorspace_op="709:709=>rgb:linear",resample_filter_uv="spline36")
z_ConvertFormat(pixel_type="YV12",colorspace_op="rgb:linear=>709:709",resample_filter_uv="spline36",dither_type="error_diffusion")
Stereodude
26th July 2019, 20:23
I port that to work with avs26/avs+ in ResizeX (https://forum.doom9.org/showthread.php?p=1837361&posted=1#post1837361) (still not use it in ResizeX functions though)
y_gamma_to_linear = Dither_y_gamma_to_linear
y_linear_to_gamma = Dither_y_linear_to_gamma
from what I read you should use it with 8 < bits (10 bits or more) even if it work with 8bit
So are they available in a release somewhere?
avsresize?
z_ConvertFormat(pixel_type="RGBPS",colorspace_op="709:709=>rgb:linear",resample_filter_uv="spline36")
z_ConvertFormat(pixel_type="YV12",colorspace_op="rgb:linear=>709:709",resample_filter_uv="spline36",dither_type="error_diffusion")
I'll give this a shot.
real.finder
26th July 2019, 20:43
So are they available in a release somewhere?
they available but no other functions use them
I did update the ResizeX now, added sigmoid_inverse and sigmoid_direct
and make y_gamma_to_linear and y_linear_to_gamma deal with PlanarRGB smartly
edit: Example
convertbits(16)
ConvertToPlanarRGB
y_gamma_to_linear()
resizex(width/2,height/2)
y_linear_to_gamma()
ConvertToyuv420
Stereodude
26th July 2019, 22:33
I got the conversions working both ways with avsresize, but it looks like the processing idea that I wanted to try in linear light doesn't work quite the way I was expecting.
Oh well...
jpsdr
27th July 2019, 10:21
How can I convert YV12 or other YUV video to "linear light" (no gamma correction) 16-bit RGB in Avisynth+?
You can try my HDRTools here (https://forum.doom9.org/showthread.php?t=175488).
videoh
27th July 2019, 11:38
it looks like the processing idea that I wanted to try in linear light doesn't work Maybe if you told us your idea we could help you with that.
Stereodude
27th July 2019, 13:40
Maybe if you told us your idea we could help you with that.
I have some video footage from a live performance DVD that has incorrect black levels, specifically they're boosted up quite a bit. I've found that if I adjust the black levels down the footage seems to need a gamma adjustment to look right.
I had the idea that perhaps if I converted it to linear light, then adjusted the black levels, and then converted it back the gamma would self correct. I didn't realize how tiny the black level adjustment would need to be when the footage is in linear light which appears to make it impractical to work this way.
Maybe what I was hoping to do in linear light would work better in a log video mode. However, converting to log video doesn't appear to be reversible with avsresize. What I mean is just converting to a log mode (like 100:1) and then back from that mode results in a significant change to the original footage shifting the video levels significantly.
z_ConvertFormat(resample_filter="bicubic", filter_param_a=1/3, filter_param_b=1/3, pixel_type="RGBP16", colorspace_op="170m:601:170m:l=>rgb:log100:170m:l", dither_type="error_diffusion")
z_ConvertFormat(resample_filter="bicubic", filter_param_a=1/3, filter_param_b=1/3, pixel_type="YUV420P16", colorspace_op="rgb:log100:170m:l=>170m:601:170m:l", dither_type="error_diffusion")
ajp_anton
29th July 2019, 21:55
The point (or one of the points?) of the gamma correction is that each step is perceived to be about equal to the eye, so all of the 256 levels (8bit) are worth the same. Converting to linear light will greatly stretch the bright end of the scale and compress the dark, so that a lot of bits are wasted on bright areas (lots of levels unused) and information is lost in dark areas because too many levels are squeezed into too few boxes. That's why the conversion is done in 16 bits, to increase the number of "boxes" so that information isn't lost. You could of course convert that into regular 8bit RGB for testing purposes, to have more compatible filters available, but remember that heavy banding will appear in dark areas because the effective bitdepth there is reduced.
Dressy
7th May 2021, 07:21
This is very interesting. Thanks a lot for sharing. In general, I really like to read new information from different forums, articles, and sites because, in this way, I develop multilaterally and find things that help me in my daily life. For example, looking for something new to read, I found out about amazon night light (https://www.amazon.com/Night-Light-Plug-Smart-Sensor/dp/B07NF4P12L), and I was amazed because it works great. Why? Because it automatically adjusts itself according to the brightness of the rooms, so I don't have to turn my head to turn it off or on, which is really perfect.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.