View Single Post
Old 25th November 2011, 04:24   #2  |  Link
vampiredom
Registered User
 
Join Date: Aug 2008
Posts: 233
Converting non-square pixels to square pixels

To convert a non-square pixel image into square pixels without cropping, multiply its width by the PAR. The height does not change. So…
704x480 @ 10/11 PAR would be 640x480 because:
704 * (10/11) = 640

Likewise,
720x576 @ 118/81 PAR would be 1049x576 because:
720 * (118/81) = 1048.8888888888888888888888888889

Just because a particular PAR conversion is "correct", however, it does not always mean it is the best choice.
Let's have a look at the example above, which gave use 1048.8888888888888888888888888889x576 for a widescreen PAL DVD. Setting a decimal width is impossible - and setting an odd width such as 1049 will cause problems in YUY2 and YV12 colorspaces. For YUY2, it must be evenly divisible by 2, while YV12 requires mod4. Our example of 1048 will work fine for both. So, in the real world:
720 * (118/81) = 1048

In cases where a video is 720 pixels wide (like all DV and DVD formats), the actual 4:3 or 16:9 area is only 704 pixels wide. This how it is possible, for example, for 720x480 and 704x480 to have the same height and pixel aspect ratio. When converting to square pixels from such sources, it is usually best to crop 8 pixels off of each side and start with a 704x480 box.

(Note: Some older professional NTSC formats may use 720x486. In which case, crop 4 pixels from the top and 2 from the bottom.)

In AviSynth terms, that's:

Code:
# assume a 720x576 source (PAL)
Crop(8,0,-8,0)

# assume a 720x480 source (NTSC)
Crop(8,0,-8,0)

# assume a 720x486 source (NTSC, like Matrox I-frame AVI files)
Crop(8,4,-8,-2)
So, let's do our PAL widescreen conversion again using the cropped numbers
704x576 @ 118/81 PAR would be 1024x576 because:
704 * (118/81) = 1025.5802469135802469135802469136 = ~1024

So, why not 1025 or 1026? If you are doing RGB formats only (such as making still images) this may be an OK choice.
However, with certain digital video formats, it is advantageous or requried for dimensions to be mod16.
1024 is also a standard computer monitor width, so it is a good choice for several reasons (and nobody will ever tell the difference).

In these examples, we will assume the target it YV12 and use dimensions that are mod4 (evenly divisible by 4) for output.

Code:
# We are assuming our input it NTSC 16:9 DVD in this example
# Note the decimal point values so that AviSynth returns a float instead of an integer
PAR = 40.0 / 33.0

# determine new width, make it mod4-safe for YV12
NewWidth = Round(width() * PAR) / 4 * 4
Spline36Resize(NewWidth, height())

Last edited by vampiredom; 25th November 2011 at 05:54.
vampiredom is offline   Reply With Quote