Wednesday, December 06, 2006

Video Encoding Redux.

Back in January I posted a quality-to-size specification for compressing widescreen video, to be played back on a large standard-def TV. This initial spec was based on my attempts to nail down a good quality DVD rip which I could pipe out of my iMac to a TV. And it did work pretty well: a 44-minute TV show would compress to about 215MB using this scheme, which is better than you'll find on the torrent networks (they generally put up 350MB files compressed using the XVID codec1), but doesn't look quite as good.

To recap, the original spec was this:

Encoder: Apple H.264 (main profile), single-pass.
Video: 512x288, 600 kb/s.
Audio: AAC-LC, Stereo, 44.1kHz, 96 kb/s

There were some problems with this spec, though, and things have progressed enough on the video front this year that I want to update it. The most notable problem with this initial recommendation has been that the resulting files won't play back on a 5G (video) iPod. That may not matter to a lot of people, but as I watch a lot of video on the iPod2 it matters to me very much, and re-encoding is a pain. Ideally I'd like a file that looks good on a TV and plays back on an iPod. Also a problem has been the quality-to-size tradeoff; a lot of the ripping I've done has been of X-Files episodes, and their general darkness are a worst-case scenario for video codecs, producing blocky, pretty crappy results. I now think the original spec went too far in its quest for small file sizes, and should be corrected.

The New Spec
Fortunately, Apple updated the 5G iPod's firmware back in October, expanding support for H.264-encoded video. This coincided with the introduction of movies in the iTunes Store, and was no doubt done in order to quell complaints of low-quality video for sale. What followed was pandemonium on the Internets, as people had to figure out what actually changed in the playback support. Things eventually settled down, but even now a lot of tools haven't been updated and don't properly produce video to the new standard.

But after a lot of fiddling I think I have it: a spec that unites DVD quality and iPod compatibility.

Encoder: x264 H.264 (baseline low-complexity profile), two-pass.
Video: 640x352, 1200 kb/s.
Audio: AAC-LC, Stereo, 44.1kHz, 96 kb/s

First of all, yes, this new spec will actually run on a 5G (and 5.5G) iPod. The 1.2 firmware upped the video playback abilities for H.264, from the old:
320x240 @ 768kb/s baseline profile
To the new:
640x480 @ 1500kb/s baseline low-complexity profile
It's a mouthful, but the point is that the iPod has been updated to play more-or-less TV-quality video. It's an impressive upgrade, all the more so for being free. Any iPod capable of playing video can, as of this writing, play this new standard.

Although being Apple, there's some bad to go with the good. The bad is that the encoding had to decrease in complexity in order to support the higher resolution and bitrate. This is what's caused so many headaches for developers. The "baseline low-complexity profile" doesn't really exist: it's just a feature degradation of the previous encoding algorithm3, most likely required because the iPod's video processor can't otherwise keep up. Before October, there weren't really any implementations of this "low-complexity" profile, and even now a number of popular encoding apps (notably, Handbrake) haven't yet been updated. So be advised: an iPod will only play H.264-encoded video at 640x480 if the implementation was updated specifically to support low-complexity.

Knowing that, how might one rip a DVD to this encoding spec? That is an excellent question, and unfortunately does not yet have an insanely great answer. Right now your best bet on a Mac is to use either this beta and hope for the best, or this followed by this4. Yeah, it's a pain right now.

File sizes for this spec are considerably bigger than the original spec (should be around 387MB for a 44-minute show, as opposed to the original 215), but I think the improvement is justified. It looks very good on a TV, and not bad on a monitor.


The HD Spec
I have had the good fortune lately to noodle around with a consumer-level HD camcorder, the Sony HDR-HC3. As is the case with most of its competitors, it records onto normal DV tapes, and imports video via Firewire at 1080i5. It's an impressive camera.

This provided an opportunity to try and spec out good compression settings for HD video - something that doesn't really exist right now. Most HD content emerging on the web creates pretty godawful file sizes (163MB for 4 minutes? 136MB for 3 minutes?), so it'd be nice to do better. I'd prefer to come up with something that comes in at or under 1500MB for a 44-minute show, and maintains 720p resolution at a noticeably improved level of quality.

Here's what I came up with:

Encoder: x264 H.264, two-pass.
Video: 1280x720, 5000 kb/s.
Audio: AAC-LC, Stereo, 44.1kHz, 96 kb/s

This approach makes use of the Baseline profile H.2646, and produces pretty nice video. This spec would produce about a 1500MB file for a 44-minute show, which isn't all that hot, but nor is it terrible. It maintains 1280x720 resolution and plays back more or less smoothly on my machine. (Pro Tip: If your PowerPC Mac stutters during strenuous Quicktime playback, try setting System Preferences -> Energy Saver -> Options -> Processor Performance to High. Seems to work for me; I think it forces the processor to max clock speed.)

With Thine Own Eyes
And to prove it, here are test videos. This montage has no sound, and was shot using the HDR-HC3. Quicktime 7 required.

Uncompressed HD footage.

HD spec.

New SD spec.

Original SD spec.







1XviD is a GPLed codec, so it's much more widely supported than Quicktime H.264, but it doesn't generate as high-quality a picture for a given file size.

2It works pretty well on the treadmill.

3Apparently, "low-complexity" means "one reference frame," with some additional restrictions on number of macroblocks and overall bitrate.

4Specifically, set the target format Quick Preset to "iPod H.264 w640", set video bitrate to 1500 under the Video tab, and set "2-pass encoding" under the Options tab.

5Of course, being video, that isn't the whole story. Although the imported file has metadata indicating it's 1080i (1920x1080 interlaced), the actual pixel data is only 1440x1080. The wonders of technology.

6Actually, I did some testing and it looks like, despite the internet's protestations, Quicktime 7.1.3 at least attempts to support b-frames. The actual profile may be Main, but I'm not totally sure. The x264 docs aren't exactly readable.