View Full Version : Levels(0,1,128,0,255) - why does it work like that with U and V?
rgr
11th April 2025, 10:05
This is the original YUV range without modification (histogram="levels"):
https://gcdnb.pbrd.co/images/32tlGGELwpBx.png?o=1
I wanted to cut out only the left half (ranges 0-127 in each planes) and I did it like this:
levels(0,1,128,0,255,coring=false)
And the graph looks like this:
https://gcdnb.pbrd.co/images/ut5tEly9dk0n.png?o=1
And while the luma graph is as expected, U and V have only expanded proportionally to both sides.
These graphs don't represent values from 0 to 255? (like luma)
When I do:
levels(128,1,255,0,255,coring=false)
the U and V graphs look the same.
https://gcdnb.pbrd.co/images/T2nxIC6P0CZb.png?o=1
What don't I understand?
And why am I doing this -- a captured miniDV cassette has a Y range 16-255. I wanted to check if the chroma also exceeds 16-240 range.
rgr
11th April 2025, 10:16
At least I found out that chroma does exceed the 0-240 range.
https://gcdnb.pbrd.co/images/rgrmkFydSKE4.png?o=1
Selur
11th April 2025, 11:34
iirc. Levels in Avisynth is meant or 'brightness, contrast, and gamma', so it does not change the chroma.
(different story in Vapoursynth, which is why at the beginning folks were confused that is didn't work like Avisynth unless you only process the luma or rgb.)
hmm, on the wiki it states:
For adjusting brightness or contrast in YUV mode, it may be better (depending on the effect you are looking for) to use Tweak or ColorYUV, because Levels changes the chroma of the clip.
rgr
11th April 2025, 11:52
"because Levels changes the chroma of the clip"
And that's exactly what I was looking for. But this change of chrome is not known by what rules.
Emulgator
11th April 2025, 13:23
At least I found out that chroma does exceed the 0-240 range.
I would be interested to see the script that outputs such histogram.
With DV and HDV cam chipsets Y going 16..255 is encountered often, but U,V ?
Canopus ADVC-300: Analog Device's Video DAC's ADV7172, ADV7173 implemented modulation range for U,V values is 128±112
lollo2
11th April 2025, 16:54
iirc. Levels in Avisynth is meant or 'brightness, contrast, and gamma', so it does not change the chroma.
Levels in AviSynth affects the chroma. The simple functions LevelsLumaOnly does not: https://forum.doom9.org/showthread.php?p=536606#post536606
AFAIK Chroma manipulations with Levels is undetermined and not suggested. Use ColorYUV to act on U and V channels.
DTL
11th April 2025, 19:30
For YUV if you want to tweak U and V - try to separate planes of planar YUV and apply Levels() to each plane as Y-only (single plane) clip. And merge YUV back after.
Sharc
11th April 2025, 23:00
Avisynth's Levels() was designed to emulate VirtualDub's Levels filter working in RGB mode. The transform that calculated from the values given to Levels() is applied equally to each RGB channel. The result is that the U and V channels change along with the Y channel. It increases saturation when one increases the contrast.
If you want Y to be affected only use YLevels() which preserves color saturation better.
http://avisynth.nl/index.php/Ylevels
https://forum.doom9.org/showpost.php?p=1909552&postcount=41
Or use Levels() with chroma copyback from the original chroma.
Edit:
@rgr. The screenshot with histogram of post#2 doesn't make much sense to me. (How) did you manipulate the source?
rgr
14th April 2025, 14:09
I would be interested to see the script that outputs such histogram.
The whole script is just the above mentioned "levels(0,1,128,0,255,coring=false)".
With DV and HDV cam chipsets Y going 16..255 is encountered often, but U,V ?
I was also surprised, but that is clear from the histogeram.
rgr
14th April 2025, 14:16
ses saturation when one increases the contrast.
If you want Y to be affected only use YLevels() which preserves color saturation better.
http://avisynth.nl/index.php/Ylevels
https://forum.doom9.org/showpost.php?p=1909552&postcount=41
Or use Levels() with chroma copyback from the original chroma.
No, I also want to change the value of all planes.
Y - from 16-255 to 0-255 (or 16-235)
U,V - from 16-255 to 0-255 (or 16-235)
I expected a simple "Levels (16,1,255,0,255)" to do just that.
Edit:
@rgr. The screenshot with histogram of post#2 doesn't make much sense to me. (How) did you manipulate the source?
No, this is what the histogram looks like after applying "levels(0,1,128,0,255,coring=false)" -- theoretically converting the range 0-128 to 0-255.
StvG
14th April 2025, 14:48
And why am I doing this -- a captured miniDV cassette has a Y range 16-255. I wanted to check if the chroma also exceeds 16-240 range.
There is Limiter() function to check/display if planes have outside range pixels.
No, I also want to change the value of all planes.
Y - from 16-255 to 0-255 (or 16-235)
U,V - from 16-255 to 0-255 (or 16-235)
Separate planes, apply Ylevels for every plane, combine planes.
Sharc
14th April 2025, 15:36
And why am I doing this -- a captured miniDV cassette has a Y range 16-255. I wanted to check if the chroma also exceeds 16-240 range.
You don't have to tamper the levels for this test.
The standard histogram("levels") provides this information. See your very first screenshot. U and V are well within the limits (as usual). It is only Y which exceeds 235 and clips at 255.
or use
ColorYUV(analyze=true)
http://avisynth.nl/index.php/ColorYUV
Scroll down to the bottom of that page.
or use to see the Y,U,V waveforms and Vectorscope:
VideoScope(DrawMode="both",HistoTypeBottom="UV",HistoTypeSide="Y",FrameType="UV")
http://avisynth.nl/index.php/VideoScope
Edit:
And if you want to modify YUV data use
ColorYUV(...........)
http://avisynth.nl/index.php/ColorYUV
DTL
14th April 2025, 18:18
No, I also want to change the value of all planes.
Y - from 16-255 to 0-255 (or 16-235)
U,V - from 16-255 to 0-255 (or 16-235)
I expected a simple "Levels (16,1,255,0,255)" to do just that.
UVs are completely different from Y. They are zeroed at 128 code level and bi-polar. If you really execute U,V - from 16-255 to 0-255 (or 16-235) it mostly probably will shift coded zero level from 128 to lower and it will cause some constant colour tone and saturation shift.
So Levels for YUV in good case simply do not change anything with UVs (copy).
wonkey_monkey
14th April 2025, 20:13
They are zeroed at 127
Isn't it 128/0x80?
StainlessS
14th April 2025, 21:39
"Isn't it 128/0x80?"
+1.
wonkey_monkey
14th April 2025, 23:35
"Isn't it 128/0x80?"
+1.
No, it's definitely not 129 :cool:
DTL
15th April 2025, 05:02
Yes - Cb Cr are (colour difference)+128 in ITU BT.601 equations. So its zeroes are at 128 in 8 bit. I fixed that post. I typically make +-1 errors and it cause bugs in software too.
"it's definitely not 129"
But 128 is also 129th code value counted as integer from 0. The digital 8bit range of 0..255 (counted/mapped as 0..255 not 1..256) is centered at 255/2=127.5 . And ITU select to round it to 128. So UVs are also +0.5 offsetted from real 255/2 mid value of 8 bit.
StainlessS
15th April 2025, 14:40
No, it's definitely not 129 :cool:
I was agreeing with you, and not providing an alternative answer [and I think you know that :p ]
and, for 8 bit TV Y levels mid point is,
(16 + 235) / 2 = 125.5 # Rounded to 126.
and for 8 bit U/V,
(16 + 240) / 2 = 128 # FIXED
rgr
16th April 2025, 13:28
You don't have to tamper the levels for this test.
The standard histogram("levels") provides this information. See your very first screenshot. U and V are well within the limits (as usual). It is only Y which exceeds 235 and clips at 255.
Look post #2.
rgr
16th April 2025, 13:31
UVs are completely different from Y. They are zeroed at 128 code level and bi-polar. If you really execute U,V - from 16-255 to 0-255 (or 16-235) it mostly probably will shift coded zero level from 128 to lower and it will cause some constant colour tone and saturation shift.
So Levels for YUV in good case simply do not change anything with UVs (copy).
Yes, I noticed that it doesn't work. White (YUV 255,127,127) stops being white (UV -> ~120). The image becomes greenish overall.
Sharc
16th April 2025, 21:29
Look post #2.
This doesn't make any sense to me. Which codec produced such mess? Certainly not regular DV, unless the camera or capture device is broken, or some weird script has been applied.
rgr
16th April 2025, 21:52
This doesn't make any sense to me. Which codec produced such mess? Certainly not DV, unless the camera or capture device is broken, or some weird script has been applied.
This is a rip from a DV camcorder.
Sharc
16th April 2025, 22:22
Yes, I noticed that it doesn't work. White (YUV 255,127,127) stops being white (UV -> ~120). The image becomes greenish overall.
For presenting visible colors YUV data need to be transformed to RGB by using a set of equations, for example following ITU specs ITU-BT.601-7 or ITU-BT.709-6, or according to the matrix used by Avisynth, etc. So the viewable color (what you eventually see on the PC monitor or TV) depends on the matrix (=set of equations) which is applied to the YUV data.
White is RGB(255,255,255). It is represented by YUV(235,128,128) using Avisynths's standard matrix, or YUV(255,128,128) using the ITU- "studio" matrices.
In either case your YUV(255,~120,~120) produces out-of-gamut RGB which get clipped in one or several of the R,G,B components (depending on the matrix), means distorted colors - which you apparently observed as becoming "greenish" in your case.
For example, if you use the ITU 601 "studio" matrix the YUV(255,120,120) would be transformed to RGB(244,263,241), but the green (263) would usually be clipped at 255 (8bit), so finally RGB(244,255,241).
If you use the standard Avisynth 601 matrix (limited range YUV-> full range RGB) the YUV(255,120,120) would be transformed to RGB(266,288,262), but all 3 components would usually be clipped at 255 (8bit), so finally RGB(255,255,255) which is just plain WHITE, loosing any structure or picture details.
You may also want to take a look at "Gavino's classic" here:
https://forum.doom9.org/showthread.php?t=154731
Sharc
16th April 2025, 22:26
This is a rip from a DV camcorder.
How did you rip this?
rgr
21st April 2025, 11:06
How did you rip this?
Vegas Video Capture -- as always
Sharc
21st April 2025, 13:01
Vegas Video Capture -- as always
ok. I can't comment on Vegas captures as I don't use it and don't know what it does. But still post#2 looks strange to me - colors and histogram wise. Hard to believe that this is native DV - I might be wrong though ......
rgr
21st April 2025, 15:27
ok. I can't comment on Vegas captures as I don't use it and don't know what it does. But still post#2 looks strange to me - colors and histogram wise. Hard to believe that this is native DV - I might be wrong though ......
You can check it yourself :)
https://files.fm/u/qv9pcyxkv9
Sharc
21st April 2025, 16:17
You can check it yourself :)
https://files.fm/u/qv9pcyxkv9
Nothing unusual. Luma in the >235 range (unfortunately clipped) is rather typical for DV, but chroma is well within legal limits, however some YUV combos produce out-of-gamut RGB -> some clipped REDs. Not really worrying or exceptional, IMO.
Added:
https://mega.nz/file/rA01ALZB#kW_zU0biQQuAV5QAjk-8HkLr5xBfzEPJUPph9Es1PVg
The panel on the right marks in Cyan the clipped brights (losing all details there) and the clipped REDs of the 2 backlights of the car, using the standard Avisynth conversion matrix. A loss for the unrecoverable overexposed brights, a minor issue only for the 2 red backlights.
(The unnatural colors are due to the filming through the car's tinted windscreen).
rgr
22nd April 2025, 11:10
Check carefully.
https://gcdnb.pbrd.co/images/OL3bgcG35v5v.png?o=1
Sharc
22nd April 2025, 12:27
Yes, I have seen the excessive V peak data. Existing, but statistically pretty much irrelevant (histogram scale factor 1000!). Maybe a camera or capture card oddity, maybe a consequence of the clipping non-linearity, I don't know. The brights (Y) hard clipping is more damaging overall.
rgr
23rd April 2025, 17:58
Here we fight for every bit (which will be destroyed by compression anyway), and then Sharc comes and says that this beautiful red is not worthy of attention :)
I'll code it in xvycc and it will be there :)
Z2697
23rd April 2025, 18:23
I was agreeing with you, and not providing an alternative answer [and I think you know that :p ]
and, for 8 bit TV Y levels mid point is,
(16 + 235) / 2 = 125.5 # Rounded to 126.
and for 8 bit U/V,
(16 + 239) / 2 = 127.5 # Rounded to 128.
Limited range chroma is 16-240 so the mid / zero point is centered right at 128.
Sharc
23rd April 2025, 19:25
Here we fight for every bit (which will be destroyed by compression anyway), and then Sharc comes and says that this beautiful red is not worthy of attention :)
I'll code it in xvycc and it will be there :)
I gave you the explanation of the cause of color distortions in post#23.
You would have to properly legalize the YUV, which is more stringent than just keeping the Y in (16...235) and U and V in (16....240).
StainlessS
23rd April 2025, 19:55
Limited range chroma is 16-240 so the mid / zero point is centered right at 128.
Yep, I'll be 70 Y.O. in a few months, brain rot sets in, sorry. [dont know what/if I was thinkin', fixed in post]
Sharc
23rd April 2025, 20:43
Yep, I'll be 70 Y.O. in a few months, brain rot sets in, sorry. [dont know what/if I was thinkin', fixed in post]
Congrats, same bracket. Maybe similar decay here .....;)
johnmeyer
23rd April 2025, 20:51
Congrats, same bracket. Maybe similar decay here .....;)
Even older here, but my mind is sharp as a steel trap ... old and rusty. :)
wonkey_monkey
24th April 2025, 00:11
Oh good, that makes me a youngster.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.