VIDEO ASPECTS AND HOW TO ZOOM
=============================

What is this file?
------------------

This file is a document that describes in a theoretical or mathematical way
how videotrans zooms and scales video images so that they may be used on
standalone DVD players and generate full-screen images. The practical use of
this information has been integrated into the "zoomcalc.c" program, which
uses the math in this document to do the calculations that videotrans needs.
This file is only present in the source directory, it will not be installed
anywhere.


Assumptions
-----------

- The source image has dimensions SX by SY

- The source image has aspect ratio SA, which can be expressed as a
  fraction SAX/SAY. The default source aspect ratio, if none is given, is
  SX/SY, which means that the source pixels are perfectly square.

- The destination image has maximum dimensions DX by DY.

- The destination image has aspect DA, which can be expressed as a fraction
  DAX/DAY.


Goal
----

What we want to do is to determine new dimensions for a zoomed image that
preserves the source aspect ratio and fits into the destination image as
well as possible.


Definitions
-----------

There are two types of zooming. One is called letterboxing, which means
that the source image is preserved entirely, and if the aspect ratios of
the source image and the destination image are different, that black borders
are created on either the top and the bottom or on the left and the right
side of the destination image to make it exactly DX by DY in size. The
other zooming method is called panscan, which means that part of the source
image will be cropped to make it fit into the destination image without
creating any black borders. Either some part of the top and bottom of the
source image will be cropped or some part of the left and right of the
source image will be cropped.


Pixel sizes
-----------

Because the source image has dimensions SX by SY and has aspect ratio
SAX/SAY, it is possible that pixels are not perfectly square. The aspect
ratio of one pixel can be expressed as follow:

     SPX   SY * SAX
SP = --- = --------
     SPY   SX * SAY

That is to say, if the height of a pixel is defined as 1, then the width of
that pixel will be equal to SP.

For example, if we want to know what a pixel looks like in the PAL DVD
720x576 mode (in the aspect ratio 16:9), we would work out:

   SP = (576 * 16) / (720 * 9) = 1.422222

That means that a pixel in this DVD mode is 1.422222 times as wide as it is
tall.

Of course, the same holds for the destination image:

     DPX   DY * DAX
DP = --- = --------
     DPY   DX * DAY



ZOOMING AND CROPPING
--------------------

Because the pixel sizes of the source and the destination are potentially
different, we need to know how to zoom and/or crop the source image to get
to the destination image. We can do this by dividing the aspect ratios by
eachother. Depending on whether we're letterboxing or panscanning, we need
to create black border or chop off part of the source image, respectively.

The way in which we calculate what we want is as follows:

If we are letterboxing (creating black borders):

    CX = SX
    CY = SY

    If (SAX / SAY) < (DAX / DAY)  (the picture would be too narrow):

        ZX = ((DAY * SAX) / (DAX * SAY)) * DX
        ZY = DY

    Otherwise (the picture would be too wide):

        ZX = DX
        ZY = ((DAX * SAY) / (DAY * SAX)) * DY

Otherwise (we are panscanning, thus cropping the image):

    ZX = DX
    ZY = DY

    If (SAX / SAY) < (DAX / DAY)  (the picture would be too tall):

        CX = SX
        CY = ((SAX * DAY) / (DAX * SAY)) * SY

    Otherwise (the picture would be too wide):

        CX = ((DAX * SAY) / (DAY * SAX)) * SX
        CY = SY


Examples
--------

Say you have an AVI file with a video in it of which the dimensions are
576x432 and aspect ratio 4/3. You want to transcode the AVI file onto a DVD
in 720x576 PAL DVD mode in 16/9 aspect ratio. What should the parameters to
the encoder be if we want to letterbox the video (so that the entire image
is displayed)?

We calculate whether (SAX / SAY) < (DAX / DAY)

    (4 / 3) < (16 / 9)

This is true, so we do:

        CX = SX = 576
        CY = SY = 432
        ZX = ((9 * 4) / (16 * 3)) * 720 = 540
        ZY = DY = 576

And thus, the scaler should not crop the image and zoom the image to
540x576 and create black borders on both sides to expand the total image to
720x576.


Another example, with a source video of 480x576 in 4:3 mode. We want to
encode this movie in 720x576 PAL DVD in 4:3 mode. What should the scaler
do?

We calculate whether (SAX / SAY) < (DAX / DAY)

    (4 / 3) < (4 / 3)

This is not true, but the picture would fit perfectly. We can choose either
of the two formulae. We choose the first one, but the second one will yield
exactly the same results.

        CX = SX = 480
        CY = SY = 576
        ZX = ((3 * 4) / (4 * 3)) * 720 = 720
        ZY = DY = 576

And thus, the scaler should not crop the image and zoom the image to
720x576, which just happens to be exactly the desired destination size.


Another example, with a source video of 640/272 with square pixels. We want
to encode this movie in 720x576 PAL DVD mode in 16:9 mode, using the
panscan method to chop off the superfluous sides. What should the scaler
do?

We calculate whether (SAX / SAY) < (DAX / DAY)

    (640 / 272) < (16 / 9)

This is not true, so the picture would be too wide if we would stretch it so
far that there would be no borders on any side. Because we want to use the
panscan method to get rid of the superfluous sides, we calculate:

        CX = ((DAX * SAY) / (DAY * SAX)) * SX =
             ((16 * 272) / (9 * 640)) * 640 = 484
        CY = SY = 272
        ZX = DX = 720
        ZY = DY = 576

And thus, the scaler should crop the image to 484x272 and then zoom the
image to 720x576, which just happens to be exactly the desired destination
size.

