PDA

View Full Version : MeGUI Suggest Resolution Algorithm Question


plasm
15th April 2007, 21:32
Hi, I'm trying to understand how the MeGUI "Suggest Resolution" algorithm works. I've searched and read a few threads here about resizing and aspect ratio, but none of them addressed this particular point.

I currently have a 16:9 anamorphic source that is 720x480 (no black bars, uses full frame, crop values are all 0). When I set horizontal resolution to 720 and check the Suggest Resolution box, it suggests 384. My quick calculation suggests that 720x400 would be closer to a 16:9 aspect ratio. I arrived at this by taking (9/16) * 720 = 405 and rounding to the nearest multiple of 16 to get 400.

Can someone please explain the algorithm behind the suggest resolution box? Why doesn't it use the algorithm I stated above? I looked at the output at both 720x384 and 720x400, and I think the 720x400 looks better.

Thanks!

EDIT: Okay, I noticed that setting the Input DAR to ITU 16:9 provides an aspect ratio of 1.823 in the custon DAR field. I guess my question is now where did this number 1.823 come from, and why is it not 16/9 = 1.778? Also, 720 / 1.823 = 395, which is closer to 400 than to 384, so my question still remains: why is 384 chosen by MeGUI? I noticed that GKnot choses 720x400 under the same parameters. Thanks!

saint-francis
15th April 2007, 22:11
I currently have a 16:9 source that is 720x480 (no black bars, uses full frame, crop values are all 0.


720x480 isn't 16:9 it's 4x3. Are you saying that this image is anamorphic?

plasm
15th April 2007, 22:15
720x480 isn't 16:9 it's 4x3. Are you saying that this image is anamorphic?

Well, 720x480 = (240*3 x 240*2) is really 3:2. But to answer your question: yes, the image source is meant to be interpreted as 16:9 anamorphic.

Doom9
16th April 2007, 12:36
Aren't you not quite done yet with that configuration? What exactly is it that you'd like to do? Is it anamorphic encoding? Non anamorphic encoding but without cutting off the black bars, or non anamorphic encoding with cutting off the black bars?
Also, if it's not anamorphic encoding, have you tried saving a script and opening this again just to see if it doesn't get you the results you want?

The avisynth script creator is meant to be used from top to bottom.. if you skip a few steps, you might not get the results you want (e.g. before you get busy with resizing, you really need to crop - if you don't crop then we expect that you do anamorphic encoding as in any other case the resolution of your output will be off).

plasm
16th April 2007, 17:32
Aren't you not quite done yet with that configuration? What exactly is it that you'd like to do? Is it anamorphic encoding? Non anamorphic encoding but without cutting off the black bars, or non anamorphic encoding with cutting off the black bars?
Also, if it's not anamorphic encoding, have you tried saving a script and opening this again just to see if it doesn't get you the results you want?

The avisynth script creator is meant to be used from top to bottom.. if you skip a few steps, you might not get the results you want (e.g. before you get busy with resizing, you really need to crop - if you don't crop then we expect that you do anamorphic encoding as in any other case the resolution of your output will be off).

The input is anamorphic. It's from a DVD source (so the MPEG-2 VOB input resolution is 720x480), and the image fills the entire view (no black bars, no cropping necessary). I can tell it's anamorphic because everything appears stretched vertically (too tall and skinny).

I filled in the AVS script creator from top to bottom. First, the ITU 16:9 selection is automatically chosen. The aspect ratio box to the right of that selection is filled in with the value 1.823. This is my first question: where on earth does the number 1.823 come from, and why isn't 16:9 = 16/9 = 1.778?

Next, the crop checkbox is left unchecked and I choose a horizontal resolution of 720. I check the Suggest Resolution box, which fills in a vertical resolution of 384. This is my second and main question: Using a horizontal resolution of 720 and an aspect ratio of 1.823, the vertical resolution should be 720 / 1.823 = 395, which when rounded to the nearest multiple of 16, yields 400. If you do the same calculation using an aspect ratio of 1.778, you get 720 / 1.778 = 405, which is also rounded to 400. Even GKnot suggests a resize of 720x400 when the aspect ratio is 1.823. So, why does MeGUI suggest 384?

Thanks again!

Doom9
16th April 2007, 18:26
So where on earth did you get a 1:1.778 movie from? I have a large DVD collection but not a single one differs from 1:1.85, 1:2.35 and 1:1.33.
Either way.. let's do it the way it's supposed to be done: you load your source, set DAR if it's not a D2V file (for those DAR is set automatically), press autocrop, check "suggest resolution", then press the "preview avs script" and if it looks okay, leave it be. I just can't get myself to dig into the code again unless something is wrong with it (which I have a really hard time believing).
Or perhaps somebody else wants to jump in here.. or you could look under the hood yourself seeing as it's open source and all. I'm sure if you'll follow all the code in that particular class in the end you'll agree that it all fits together nicely.
Here's the code by the way:
if (inputDAR.SelectedIndex == -1)
dar = 1; // assume square pixels if user specifies nothing
else
if (!Double.TryParse(customDAR.Text, out dar))
dar = 1.0;
int sarx, sary;
int scriptVerticalResolution = VideoUtil.suggestResolution(reader.Height, reader.Width, dar,
Cropping, (int)horizontalResolution.Value, signalAR, mainForm.Settings.AcceptableAspectErrorPercent, out sarx, out sary);
if (scriptVerticalResolution > verticalResolution.Maximum)
{ // Reduce horizontal resolution until a fit is found that doesn't require upsizing. This is really only needed for oddball DAR scenarios
int hres = (int)horizontalResolution.Value;
do
{
hres -= 16;
scriptVerticalResolution = VideoUtil.suggestResolution(reader.Height, reader.Width, dar, Cropping, hres, signalAR, mainForm.Settings.AcceptableAspectErrorPercent, out sarx, out sary);
}
while (scriptVerticalResolution > verticalResolution.Maximum && hres > 0);
eventsOn = false;
horizontalResolution.Value = hres;
eventsOn = true;
}
verticalResolution.Value = (decimal)scriptVerticalResolution;
if (signalAR)
{
this.sarX = sarx;
this.sarY = sary;
}
inputDar.SelectedIndex is the value you select from the input dropdown (0 would be 16:9)

And this is the suggestResolution method:
public static int suggestResolution(double readerHeight, double readerWidth, double customDAR, CropValues cropping, int horizontalResolution,
bool signalAR, int acceptableAspectError, out int sarX, out int sarY)
{
double fractionOfWidth = (readerWidth - (double)cropping.left - (double)cropping.right) / readerWidth;
double inputWidthOnHeight = (readerWidth - (double)cropping.left - (double)cropping.right) /
(readerHeight - (double)cropping.top - (double)cropping.bottom);
double sourceHorizontalResolution = readerHeight * customDAR * fractionOfWidth;
double sourceVerticalResolution = readerHeight - (double)cropping.top - (double)cropping.bottom;
double realAspectRatio = sourceHorizontalResolution / sourceVerticalResolution; // the real aspect ratio of the video
realAspectRatio = getAspectRatio(realAspectRatio, acceptableAspectError); // Constrains DAR to a set of limited possibilities
double resizedVerticalResolution = (double)horizontalResolution / realAspectRatio;

int scriptVerticalResolution = ((int)Math.Round(resizedVerticalResolution / 16.0)) * 16;

if (signalAR)
{
resizedVerticalResolution = (double)horizontalResolution / inputWidthOnHeight; // Scale vertical resolution appropriately
scriptVerticalResolution = ((int)Math.Round(resizedVerticalResolution / 16.0) * 16);

int parX = 0;
int parY = 0;
double distance = 999999;
for (int i = 1; i < 101; i++)
{
// We create a fraction with integers, and then convert back to a double, and see how big the rounding error is
double fractionApproximation = (double)Math.Round(realAspectRatio * ((double)i)) / (double)i;
double approximationDifference = Math.Abs(realAspectRatio - fractionApproximation);
if (approximationDifference < distance)
{
distance = approximationDifference;
parY = i;
parX = (int)Math.Round(realAspectRatio * ((double)parY));
}
}
//sarX = (int)Math.Round(realAspectRatio * resizedVerticalResolution);
//sarY = (int)Math.Round(horizontalResolution);
sarX = parX;
sarY = parY;

return scriptVerticalResolution;
}
else
{
sarX = 0;
sarY = 0;
return scriptVerticalResolution;
}
}
And getAspectRatio
private static double getAspectRatio(double calculatedAR, int acceptableAspectErrorPercent)
{
double[] availableAspectRatios = {1.0, 1.33333, 1.66666, 1.77778, 1.85, 2.35};
double[] distances = new double[availableAspectRatios.Length];
double minDist = 1000.0;
double realAspectRatio = 1.0;
foreach (double d in availableAspectRatios)
{
double dist = Math.Abs(d - calculatedAR);
if (dist < minDist)
{
minDist = dist;
realAspectRatio = d;
}
}
double aspectError = realAspectRatio / calculatedAR;
if (Math.Abs(aspectError - 1.0) * 100.0 < acceptableAspectErrorPercent)
return realAspectRatio;
else
return calculatedAR;
}

plasm
17th April 2007, 02:08
So where on earth did you get a 1:1.778 movie from? I have a large DVD collection but not a single one differs from 1:1.85, 1:2.35 and 1:1.33.

It's an HD television show (Battlestar Galactica) not a movie, so AFAIK (http://en.wikipedia.org/wiki/Display_Aspect_Ratio#16:9_standard), the final aspect ratio is supposed to be 1:1.78.

Either way.. let's do it the way it's supposed to be done: you load your source, set DAR if it's not a D2V file (for those DAR is set automatically), press autocrop, check "suggest resolution", then press the "preview avs script" and if it looks okay, leave it be. I just can't get myself to dig into the code again unless something is wrong with it (which I have a really hard time believing).

Well, the code looks fine to me too. I did figure out the vertical resolution of 384 vs 400 mystery by examining the code: the aspect ratio is being constrained to the set {1.0, 1.33333, 1.66666, 1.77778, 1.85, 2.35}, so in my case, the final AR of 1.823 is being interpreted as 1.85 by the function getAspectRatio. This is why 384 is chosen as the vertical resolution instead of 400. 720 / 1.85 = 389, which is rounded to 384.

All of this leads me to believe that my DVD was mastered incorrectly. If, in fact, anamorphic DVDs using a DAR of ITU 16:9 use 1.823 as the aspect ratio, then there should have been some vertical black bars on the left and right sides of the DVD's VOBs so that the result of the aspect ratio correction would yield an image with aspect ratio 1.778 instead of 1.823.

Does this sound reasonable? I think I'll just use a custom input DAR of 1.778 in this case so that I get something that looks good to me. Thanks!