Guide to Anamorphic Encoding in MediaFork

http://img46.imageshack.us/img46/9127/kobolanimationyn5.gif

I am in love with clee's new Anamorphic Pixel Aspect Ratio feature, which prigaux patched into MediaFork. Let me explain why I adore it and, to the best of my understanding, how it works. My hope is this will encourage other people to use it too...

  • When not to use Anamorphic PAR
  • Short visual explanation
  • Enabling Anamorphic PAR
  • What does "anamorphic" mean?
  • Anamorphic DVDs
  • MediaFork's default behavior
  • Anamorphic encoding methods
    • Crude
    • Clever
  • Other resources
  • Appendices
    • Anamorphic in MediaForkCLI
    • Hard letterboxing
    • Macroblocks
    • PAL

When not to use Anamorphic PAR

Right off the bat, to save people time, here are situations when you DON'T want to use anamorphic encoding:

  1. When you want to watch your movies in QuickTime? (support will be coming soon thanks to maurj's hard work)
  2. When you want to watch your movies on an iPod
  3. When you want to watch your movies on an iPhone
  4. When you want to watch your movies on an Apple TV

Seeing a pattern? Apple products tend not to respect anamorphic settings. The video will still play, have no fear of that, but it will appear distorted—vertically stretched.

If any of those usage scenarios apply to you, feel free to stop reading.

If those caveats do not faze you, read on.


Short visual explanation

These are scaled down 50% in size, in case you couldn't tell.

Here's the size of a movie stored on a DVD. On the disc, the film's native aspect ratio is distorted:

http://img300.imageshack.us/img300/8282/smallfireflysaruw8.png

To restore the aspect ratio, you can either squeeze the picture vertically or stretch the picture horizontally. One shrinks the image, and one expands it.

HandBrake shrinks it. Here's the size of a movie HandBrake outputs, and the size MediaFork outputs by default. Notice how the width stays the same as what's stored on the DVD, but the height is reduced:

http://img299.imageshack.us/img299/5953/smallfireflydefaulthf3.png

Here's the display size of a movie when you enable Anamorphic PAR in MediaFork. Note that it preserves the full height of the image stored on the DVD:

http://img299.imageshack.us/img299/3169/smallfireflyparli9.png


Enabling Anamorphic PAR

You enable anamorphic encoding by checking a box on the Settings window:

You can also turn it on by default in the Preferences:

http://img211.imageshack.us/img211/3730/parprefrd4.png

That's all well and good, but to quote Mayor LaGuardia?: "Say, children—what does it all mean?"


What does "anamorphic" mean?

First, like a high school English teacher, I'll break the word down.

Ana:

back, towards, against

morph:

body, shape, form

Put them together and what do you get?

"Back towards a form" or "Against a shape." It almost seems to imply that you're comparing against a shape to return to a form.

But a DVD already has a shape, right? Why does it need to compare its form to anything else?


Anamorphic DVDs

An image is stored on a DVD at 720*480 (NTSC) or 720*576 (for PAL). I'm in North America so I'll be using NTSC numbers in my examples. (See the PAL appendix for the differences.)

This is what the image stored on an anamorphic NTSC DVD looks like (for information on non-anamorphic DVDs, see the Hard Letterboxing Appendix):

http://img77.imageshack.us/img77/7066/bsgstorageeh1.png

Notice how it's distorted? That's the anamorphic part. DVDs are stored with a 1.5:1 aspect ratio.

Wait, aspect ratio? What?

Aspect ratio is the width, divided by the height. DVDs are stored with a 720*480 resolution, and 720 / 480 = 1.5. That means it's 1.5 times wider than it is tall.

But video isn't meant to be seen with a 1.5:1 aspect ratio. It's not wide enough for movies and widescreen TV, and too wide for standard TV. One common widescreen aspect ratio is 1.78:1, or 16:9. This is the native aspect ratio of widescreen TVs. Standard TVs use an aspect ratio of 4:3 (1.33:1).

DVD video has an "aspect ratio flag" which tells the DVD player how to distort the picture stored on the DVD to recreate the original film aspect ratio. This flag is either 4:3 or 16:9.

Of course, a lot of movies are wider than 16:9. One popular aspect ratio is 2.35:1, which is quite a bit wider. When this is stored on a DVD, it too is given the 16:9 aspect ratio flag. Of course, the content itself is wider. To cope with this, black bars (letterboxing) line the top and bottom of the picture. This can be a difficult part to grasp. At least, it was for me. So I'm going to repeat this with a few different wordings.

When anamorphic DVD content is stretched for viewing on a widescreen TV, it *always* stretches to 853*480. 480 is too "tall" for films wider than 16:9. To make it "shorter" the top and bottom are matted with black lines. So whether a movie as an aspect ratio of 1.78:1, 1.85:1, or 2.35:1, it is always going to displayed 853 pixels wide. The height just gets shorter and shorter to match the aspect ratio—480, 460, and 360, consecutively. (Those height and width values are only approximate. For details, see the Macroblock Appendix.)

The storage width is the width of the visible frame on the DVD (almost always 720) and the display width is 853 (storage width times 16:9). The height is the height of the visible frame on the DVD—the frame after cropping away black bars. This is going to be roughly equivalent to the display width divided by the film's aspect ratio.

When you play the DVD on a wide 16:9 television, it keeps the height and stretches the width (853*480). This is what it means when a DVD box proclaims "Enhanced for widescreen." When you play the DVD on a standard 4:3 television, it reduces the width to 640 (the maximum width for a standard TV) and squishes the height to 360 to match the aspect ratio.

Both ways recreate the film's aspect ratio. The first way multiplies the height (480) by a 16:9 aspect ratio, and uses that for the width. The second method takes the width (720), reduces it to the maximum width of a standard TV (640), divides it by the film's aspect ratio (1.78:1 aka 16:9), and uses that as the height.

To put this another way, "anamorphic" means the movie doesn't have a single, native shape that you watch. Instead, it shape-shifts. If you're watching it on a standard TV, it morphs to fit it. When you play it on a widescreen TV, it morphs to fit it, too.


MediaFork's default behavior

What HandBrake does, and what MediaFork does by default, is correct the aspect ratio by maintaining the width and squishing the height to match. The result looks like this:

http://img160.imageshack.us/img160/2872/bsgdefaultkj3.png

This is very similar to what happens when you play a DVD on a standard 4:3 television. The only difference is, MediaFork leaves the width at 720, instead of reducing it to 640. So it divides 720 by the aspect ratio, giving you output dimensions of 720*404. (If you're testing me on this and you get a width of 704 or a height of 400, please see the Macroblock Appendix.)

Of course, that means you're reducing the vertical line count from 480p to 404p... a significant reduction in picture quality.

There are two other ways to go about encoding the anamorphic video, in order to keep that from happening.


Anamorphic encoding methods

Crude anamorphic

The crude way (which is easily done from MediaForkCLI), is to maintain the height and "hard" stretch the frame width to 853 (for details, see the CLI Appendix). The problem with this is that you end up storing an image that's got a higher resolution than your DVD source—it takes up more space. Instead of a 720*480 frame or a 720*404 frame, you're writing a 853*480 frame to disk.

Clever anamorphic

The final method is the clever one. Why not do the same thing a DVD does? Store in one aspect and display in another. With this method, you can preserve the full frame on the DVD, without having to store it at its wider display resolution. Compared to storing 853*480 on disk, storing 720*480 reduces file size while maintaining exactly the same quality. As an added bonus, because MediaFork does not need to scale the video, encoding should be faster (clee, the feature's author, saw a 15% speed boost).

Now, how do we go about doing this?

Vide on a computer is stored in a container file, be it .mp4 or .mkv or .avi or .ogm or something else. Inside that container are tracks or streams. Usually, there will be one video track and one audio track.

It's possible to set a display aspect ratio in the container, but this is most robustly supported by Matroska (.mkv), a container we don't use.

So the other option is setting it in the video track. And this is exactly what clee has provided us with:

http://img115.imageshack.us/img115/5933/bsgparjy0.png

This technique is so clever, VLC doesn't even realize what's going on. It thinks it's displaying 720*480 when it's really showing 853*480.

In fact, it is displaying 720*480. Only, the video track is telling VLC: "Display this with wide pixels instead of the square ones you usually use." So instead of an image of square blocks, it becomes an image of wide rectangular blocks.

Because computers think of video in terms of square pixels, VLC has to figure out what arrangement of square pixels is needed to reproduce the image in its correct dimensions. It does this by multiplying the storage width (720) by a ratio: the Pixel Aspect Ratio, or PAR. By default, the PAR is 1:1. With that ratio, what you see is what you get—square pixels. The video is stored and displayed with the same dimensions. In order to recreate 16:9 pixels from 1:1 pixels, the ratio is 32/27 (16*2 / 9*3). For every 32 square pixels across, it uses 27 square pixels up and down. You already know this, another way: it produces the same results as multiplying the height (480) by 16/9. Multiply 720 by 32 and divide that by 27, and you end up with 853, the display width in square pixels.

This is anamorphic PAR, and it is very, very sexy.


Other Resources

http://c133.org/blog/tech/screeching_handbrake.html (the birth of the feature)

http://seemoredigital.net/03_Video_Only_Info/What_is_an_anamorphic_encode.html

http://www.3ivx.com/support/par.html

http://lipas.uwasa.fi/~f76998/video/conversion/

http://en.wikipedia.org/wiki/Anamorphic_widescreen

http://gregl.net/videophile/anamorphic.htm

http://www.dvdfile.com/news/special_report/production_a_z/anamorphic.htm


Appendices

CLI Appendix

Hard stretching to anamorphic in MediaForkCLI

It's really easy. You just have to specify the visible frame height. Say your command is:

./MediaForkCLI -i dvd -o movie.mp4

What you add for anamorphic encoding depends on the film's aspect ratio.

  • 1.78:1 means adding "-l 480" to the end of the command.
  • 1.85:1 means adding "-l 460"
  • 2.35:1 means adding "-l 360"

So for a 1.85:1 movie your complete command would be:

./MediaForkCLI -i dvd -o movie.mp4 -l 460

What this is going to do is give you output dimensions of 853*460. That means you are storing a movie with a frame size 33% larger than the DVD. So only use it to play around, or if you absolutely *must* be able to watch the movie in QuickTime?. It's a total waste of storage space.

Instead do...

Anamorphic PAR in MediaForkCLI

This is even easier. Just add a "-p" like so:

./MediaForkCLI -i dvd -o movie.mp4 -p

That will output a movie with dimensions of 720*480 (for a 1.78:1 NTSC film) but which VLC and MPlayer will display as 853*480. The same display as a hard stretched anamorphic DVD, without storing a 33% larger image.


Hard Letterboxing Appendix

Some widescreen DVDs, especially older ones, aren't anamorphic. These DVDs are specifically designed to play on standard 4:3 TVs. The "aspect ratio flag" for these discs is set to 4:3 even though the content's aspect ratio is wider.

http://img126.imageshack.us/img126/1217/buffy16x9jc0.png

When you play it on a standard square TV, everything's shiny. However, when you play it on a widescreen TV, you will have black bars not only on the top and bottom, but also on the sides. This gives you a far smaller picture. Those side-bars are often called "pillars." This technique is often used in broadcast television to deliver 16x9 content to standard definition sets.


Macroblock Appendix

MPEG video encoders (like the MPEG-2 used on DVDs or the MPEG-4 variants used by MediaFork) work by dividing the video frame into blocks. The entire frame becomes a grid of blocks, 16 pixels high and 16 pixels wide. These 16x16 blocks are called macroblocks. When you encode video, you have to use height and width values that are multiples of 16. When the height or width doesn't divide cleanly into 16 (that is to say, when there is a remainder), the video encoder has to make up extra "garbage" information for the edges of the frame. This increases the file size or decreases the video quality, depending on whether you're targeting a constant quality or a size/bitrate.

An unhappy side effect of this is that aspect ratios can only be approximate. 720*360 is the storage dimensions of a 2.35:1 movie after cropping. But because 360 is not divisible by 16, you have to bump up to 368 or down to 352. Suddenly the display aspect ratio is 2.32:1.

This is why sometimes, using MediaFork's default behavior, you will get an output width of 704 for a 1.78:1 film. For whatever reason, MediaFork has decided it needs a width that is slightly lower than 720. But in order to honor macroblocking requirements, widths have to be divisible by 16. 704 is next possible width lower than 720.

Similarly, even though the "perfect" height for a movie 720 pixels wide with a 1.78:1 aspect ratio would be 404, that number isn't cleanly divisible by 16, while 400 is.

For now, MediaFork ignores macroblocking requirements for anamorphic encoding. It makes perfectly preserving the visible frame with a precise aspect ratio its top priority.


PAL Appendix

PAL storage frame size is 720*576. That means the display width is always the height times 1.78 (16/9), or 1024. Both the storage and the display height are 1024 divided by the aspect ratio.

  • PAL output dimensions:
    • 1.78:1: 1024*576
    • 1.85:1: 1024*554
    • 2.35:1: 1024*436
  • PAL storage dimensions:
    • 1.78:1: 720*576
    • 1.85:1: 720*554
    • 2.35:1: 720*436