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. |
|
|
#1 | Link |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
Preserving MPEG Brightness
[MODERATOR]: Continued from this thread.
@Didee and others, I have made some progress in understanding this issue! Let me first summarize my findings and then give the evidence: 1) dvd2avi/AviSynth is recreating the clip correctly. No variations in brightness due to AviSynth. 2) The mpeg2 decoder which is used in VirtualDub/VirtualDubMod (I will ask Avery which one this is) is messing up. It's only messing up if there is GREEN in the clip. In order to test this I made the following script Code:
#mixUVc.avs BlankClip(pixel_type="YV12", width=720, height=576, color=$00ff00) # green: Y=145, U=54, V=34 ConvertToYV12 #ColorYUV(analyze=true) Code:
#mixUVc2.avs
mpeg2source("F:\mixUVc2.d2v")
ColorYUV(analyze=true)
1) mixUVc2.avs looks exactly like the original script (ie mixUVc.avs) 2) mixUVc.m2v looks a bit darker than the original script (ie mixUVc.avs) I repeated the test for the red clip (mixUVd.avs) and the blue clip (mixUVe.avs), which didn't show any difference in brightness. All files can be found here http://www.geocities.com/wilbertdijkhof/mix.zip Last edited by sh0dan; 22nd August 2004 at 11:28. |
|
|
|
|
|
#2 | Link | |
|
Simply me
Join Date: Aug 2002
Location: Lancashire, England
Posts: 597
|
Quote:
regards Simon
__________________
http://www.geocities.com/siwalters_uk/fnews.html |
|
|
|
|
|
|
#3 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
I meant that VDub/VDubMod uses external mpeg decoder converters (maybe mpeg1 dunno):
Quote:
edit: Oh, I get what you mean. Of course VDub can't open mpeg2 files. I will ask the VDubMod folks about this. Last edited by Wilbert; 20th August 2004 at 00:29. |
|
|
|
|
|
|
#4 | Link |
|
Registered Jedi
Join Date: Jan 2003
Location: Georgia, U.S.A.
Posts: 734
|
AFAIK, VirtualDubMod borrows the MPEG-(1/2) code from my VirtualDub-MPEG2 mod, so you should've asked me first.
![]() I downloaded your mix.zip, and the first thing that caught my attention was the fact that QuEnc uses matrix_coefficients = 4 in the "sequence display extension" for mix2.mpg. This value specifies the FCC's coefficients for red, green, and blue (0.3, 0.59, 0.11), rather than ITU's default MPEG-2 coefficients (0.2125, 0.7154, 0.0721), or the more common MPEG-1 coefficients (0.299, 0.587, 0.114). It's important to note that the "sequence display extension" and "matrix coefficients" fields are optional. But if they are present, my MPEG-2 decoder will use them to convert to RGB. It is the responsibility of the encoder to specify the correct coefficients the decoder should use. And I guarantee that VirtualDub-MPEG2 presents the RGB image correctly, based on those coefficients. (By corollary, "matrix coefficients" has no meaning if the decoder is not converting to RGB.) Naturally there will be a discrepancy if Avisynth's "ConvertToYV12" command is not using the same coefficients that QuEnc has specified for the stream, and I think that's what you are seeing.
__________________
May the FOURCC be with you... |
|
|
|
|
|
#5 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
Quote:
|
|
|
|
|
|
|
#6 | Link | |
|
budala
Join Date: Oct 2003
Location: U.S.
Posts: 545
|
Quote:
|
|
|
|
|
|
|
#7 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
I updated the mix.zip with the good files.
Quote:
2) These are for the luma, YCbCr [0,255] -> RGB [0,255] right? Why are the coefficients "0.3, 0.59, 0.11" used? But, should the coefficients be used for a YCbCr [16,235] -> RGB [0,255] conversion (ie 0.257, 0.504, 0.098)? 3) I will try it with TMPGEnc and post my findings. Last edited by Wilbert; 21st August 2004 at 00:57. |
|
|
|
|
|
|
#8 | Link | ||
|
Registered Jedi
Join Date: Jan 2003
Location: Georgia, U.S.A.
Posts: 734
|
Quote:
Quote:
Y = (0.299 * (219 / 255) * R) + (0.587 * (219 / 255) * G) + (0.114 * (219 / 255) * B) + 16; Note that a scaling does take place, because an RGB value of 0,0,0 is converted to a luminance value of 16. Honestly I've never given much thought to that, because I was only concerned with the decoding part, not the encoding. To recover the original RGB data, I use the same three coefficients, plus some inverted math inspired by the MSSG reference. I can only describe it in code: crv = (255 / 224) * (1 - 0.299) * 2; cbu = (255 / 224) * (1 - 0.114) * 2; cgu = (255 / 224) * (0.114 / 0.587) * (1 - 0.114) * -2; cgv = (255 / 224) * (0.299 / 0.587) * (1 - 0.299) * -2; y2 = (Y * (255 / 219)) - 16; R = y2 + crv * (Cr - 128); G = y2 + cgu * (Cb - 128) + cgv * (Cr - 128); B = y2 + cbu * (Cb - 128);
__________________
May the FOURCC be with you... |
||
|
|
|
|
|
#9 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
Quote:
![]() I think that AviSynth is using the same coefficients. Ie, (0.299, 0.587, 0.114) for luma. ----- Let's calculate it, to see where it goes wrong. Take a look at mixUVc.m2v (in mix.zip). Presumable it has the same matrix coefficients. How can I check that myself? mixUVc.m2v is created (with QuEnc) with an avs script: R=B=0, G=256. Converted to YV12 in AviSynth, resulting in Y=145, U=54, V=34 (can be checked with coloryuv(analyze=true)). The (0.299, 0.587, 0.114) coefficients result in the same YUV values (ie Y=145, U=54, V=34). Ie Y = (0.299 * (219 / 255) * 0) + (0.587 * (219 / 255) * 256) + (0.114 * (219 / 255) * 0) + 16 = 145 (same for U and V) Now, the decoder side: 1) If I open the mixUVc.m2v is AviSynth and take a look at the YUV values, they are precisely Y=145, U=54, V=34. As it should. Converting to RGB (in avs itself), gives R=B=0, G=256, as it should. 2) What about VirtualDubMod? y2 = (145 * (255 / 219)) - 16 = 152.8356 I will assume this is a typo, because it should be y2 = (145 - 16)*(255 / 219) = 150.2054795 crv = 1.5960 cbu = 2.0172 cgu = -.3918 cgv = -.8130 R = y2 + crv * (V - 128) = .1789616 -> 0 G = y2 + cgu * (U - 128) + cgv * (V - 128) = 255.6148478 -> 256 B = y2 + cbu * (U - 128) = .9303010 -> 1 But this is not what I see if I open mixUVc.m2v in VDubMod. If I take a snapshot in VDudMod, open it in Color photo paint it gives R0 G216 B0 Could you please check the YUV->RGB conversion in VDubMod? edit: I did the same avs script with TMPGEnc: BlankClip(pixel_type="YV12", width=720, height=576, color=$00ff00) result: http://www.geocities.com/wilbertdijkhof/mixUVc_tmpg.mpg YUV values are Y=145, U=54, V=34. What are the matrix coefficients of this one? Cause, if I open it in VDubMod it does look like the original. Last edited by Wilbert; 21st August 2004 at 20:56. |
|
|
|
|
|
|
#11 | Link | ||||||||
|
Registered Jedi
Join Date: Jan 2003
Location: Georgia, U.S.A.
Posts: 734
|
Quote:
![]() Quote:
Quote:
Quote:
Quote:
Quote:
Quote:
crv = (255 / 224) * (1 - 0.2125) * 2 = 1.7929687 cbu = (255 / 224) * (1 - 0.0721) * 2 = 2.1126294 cgu = (255 / 224) * (0.0721 / 0.7154) * (1 - 0.0721) * -2 = -0.2129164 cgv = (255 / 224) * (0.2125 / 0.7154) * (1 - 0.2125) * -2 = -0.5325772 y2 = (145 - 16) * (255 / 219) = 150.20548 R = y2 + crv * (34 - 128) = 0 G = y2 + cgu * (54 - 128) + cgv * (34 - 128) = 216 B = y2 + cbu * (54 - 128) = 0 Quote:
__________________
May the FOURCC be with you... |
||||||||
|
|
|
|
|
#12 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
Let me guess: The following mpeg2 stream comes (=demuxed) from a dvd (from hell). I bet it uses the mpeg2 coefficients
http://www.geocities.com/wilbertdijkhof/fh.m2v So, summarized the problem is as follows (if the above is indeed true): 1) VirtualDubMod (and probably the majority of the software dvd players) does it right, because it uses the mpeg2 coefficients for playback. 2) dvd2avi, AviSynth, DivX/XviD/TMPGEnc (CCE?) does it "wrong", because: a) dvd2avi/AviSynth doesn't correct for this, doesn't pass the mpeg2 coefficients through. b) DivX/XviD resp. TMPGEnc (CCE?) uses the mpeg1 coefficients resp. FCC coefficients. So, the mpeg2/mpeg4 decoders uses the wrong coefficients. Quote:
How do you suggest the dvd2avi/AviSynth should solve this? Make an AviSynth plugin to correct for this, or (I think a better solution is to) bug the DivX/XviD/"programs which let you change the mpeg2 header info" folks to add a setting for choosing the right coefficients? |
|
|
|
|
|
|
#13 | Link |
|
AviSynth Developer
![]() Join Date: Nov 2001
Location: Dark Side of the Moon
Posts: 3,469
|
Interesting. I've googled around a bit and haven't so far found anything to use as reference for this. Do you have any links?
There are a bunch of possible solutions - even though I don't much like either of them. Either way - does DVD2AVI write the value into the D2V-file? My initial idea is a separate filter for conversion filter reading a default "in" color-coefficients from a variable that mpeg2dec3 could set on initialization, and default output to 601 coefficients. Could you list all known coefficients? |
|
|
|
|
|
#14 | Link | |||
|
Registered Jedi
Join Date: Jan 2003
Location: Georgia, U.S.A.
Posts: 734
|
Quote:
Quote:
Quote:
__________________
May the FOURCC be with you... |
|||
|
|
|
|
|
#15 | Link | |
|
Registered Jedi
Join Date: Jan 2003
Location: Georgia, U.S.A.
Posts: 734
|
Quote:
http://www.ece.cmu.edu/~ece796/docum...2_Video_IS.doc http://le-hacker.org/hacks/mpeg-drafts/is138182.pdf Section 6.3.6 describes "sequence display extension," and Table 6-9 of that section lists the matrix coefficients.
__________________
May the FOURCC be with you... |
|
|
|
|
|
|
#16 | Link | |
|
Moderator
![]() Join Date: Nov 2001
Location: Netherlands
Posts: 5,616
|
Quote:
default: mpeg2 coefficients -> default: mpeg1 coefficients. If Graft gets back we can ask him to modify his DVD2AVI (called DGIndex) and mpeg2dec3 (called DGDecode). Might take a while though, before he gets back
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|