Thursday, November 8, 2012

What's the Difference Between a Codec and a Container?

A codec is a method for encoding or decoding data--specifically, compressed data. Though the word codec has become somewhat generic, the term was originally a shortened form of compressor-decompressor. That's what codecs do: they take digital media data and either compress them (for transport and storage) or decompress them (for viewing or transcoding.)
Raw video and audio data consume vast amounts of storage space. Uncompressed 1080i high-definition video recorded at 60 frames per second eats up 410 gigabytes per hour of video. CD audio, which is pretty passé by modern standards, puts about 74 minutes of audio on a 680MB CD. However, eight channels of audio encoded at 24-bit resolution churns through 16 megabits per second, or a little over 7 gigabytes per hour of audio. Even with high-speed broadband, that's too much data. That's why video and audio needs to be compressed for storage.
Once the media data is compressed into suitable formats and reasonable sizes, it needs to be packaged, transported, and presented. That's the purpose of container formats--to be discrete "black boxes" for holding a variety of media formats. Good container formats can handle files compressed with a variety of different codecs.
Dozens of codecs exist, with specific use models; most of them you may never encounter as a typical consumer. This section focuses on codecs as compression/decompression schemes, not as specific software that may encode or transcode video. That discussion comes later.

H.264 / MPEG-4 AVC: This is the most common codec used in modern camcorders and digital cameras that capture to file-based devices (hard drives, memory cards, and so on). Again, note that this is the codec, not the container format, which is typically AVCHD (see the container section on the next page.)
MJPEG (Motion JPEG): This is an older format used by some digital cameras and older devices to capture video. It was developed by the same group (Joint Picture Experts Group) that developed the JPEG photography compression codec, hence the name.
DV and HDV: DV was developed by a consortium of consumer electronics companies that manufacture and sell camcorders. DV is a tape-based standard and is common on camcorders that use mini-DV tape cartridges. (Some versions of DV are used in professional tape-based gear as well, like DVCPRO and DVCAM.) DV itself is limited to standard definition, so one version, called HDV, was created to allow capture of high-definition video to mini-DV tape cartridges.
Note that DV and HDV describe both the codec used and the container format.

Disc-Based Delivery Formats

Let's talk about the old-fashioned DVD or slightly more newfangled Blu-ray Discs for a bit. Despite the increasing popularity of streaming video, the capability to deliver disc-based media is still needed. I've created Blu-ray and DVD discs to hand out to parents of high school athletes, or to send to relatives, for example. Practically everyone has a DVD player, and you don't need an Internet connection to share a DVD.
MPEG-2: Again, we have to distinguish the codec (MPEG-2 Part 2, also known as H.262) from the MPEG-2 container format. MPEG-2 is used exclusively as the compression standard for DVD video. MPEG-2 was also used in the early days of Blu-ray Disc creation, though most of the newer Blu-ray movies no longer use MPEG-2. MPEG-2 is also used to compress video for over-the-air HDTV broadcast.
H.264 / MPEG-4 AVC: This is essentially the same codec used by modern camcorders for capturing video. It's also used for delivering Web video. H.264 offers robust image quality at relatively low bit rates and high compression ratios. It's very scalable, so you can also have high-bit-rate H.264 video that looks fabulous. This is what's typically used for Blu-ray playback.
Microsoft VC-1: VC-1 is one of three codecs used to encode Blu-ray content. It's also used in Microsoft's Silverlight alternative to Adobe Flash. Blu-ray movies using VC-1 mostly use VC-1 Advanced Profile, also known as Windows Media Video 9 Advanced Profile or WVC1.

Streaming From the Web

Delivering video over the Web necessarily means compromises, mostly trading off image quality for lower bit rates. Broadband bit rates vary depending on the ISP and transport technology. Most of what applies to Web content delivery also applies to video stored on mobile, handheld devices.
MPEG-1: MPEG-1 is the old warhorse for delivering video over the Web. While YouTube, Netflix, and other relatively sophisticated streaming video providers have moved away from MPEG-1, a ton of MPEG-1 standard-definition video is still available on other sites. I've included it mostly for completeness; if you're planning on using high definition at all, you'll want to avoid MPEG-1.
WMV (Windows Media Video): Once again, we're talking about the codec, not the Windows Media Video container format. While not as common as MPEG-1, there's still a lot of WMV content available. Again, it's probably best to avoid using this.
H.264 / MPEG-4 AVC: H.264, at lower bit rates, delivers fairly high-quality video. H.264 will likely become the most common codec used: Adobe supports it in Flash, HTML5 canvas can use H.264, YouTube is steadily moving to H.264, and Apple fully supports it. While creating a video compressed in H.264 might not play on older devices, it's a sure bet going forward.

The Right Container: Flexible and Usable

I'm only going to touch on commonly used container formats you'll encounter currently. Each serves different purposes and audiences, and which you choose depends on what you want to accomplish. Since container formats also carry audio along with them, we'll touch on audio codecs.
When you actually render your video in your video editing package, you'll be creating a container file, which will include the compressed video and audio, as well as the information needed by the rendering device's data format, so the actual video and audio can be decoded by the target hardware. This is why knowing the final destination for your video is critical to choosing the best container.
Unlike codecs, the usage models of container formats tend to overlap heavily. These are the most common ones in use.

Containers for Archiving and Capturing

As mentioned with codecs, you need to pick the highest quality archive medium. Most consumers may simply want to store video in a way that's easy to stream to other PCs on the network--or even over the Internet--but you don't want it to look like a pixilated mess. The right container will help you strike the right balance between quality and streamability for your particular needs.
Advanced Systems Format: ASF is a Microsoft-based container format. You'll see various file extensions for ASF files, including .asf, .wma, and .wmv. Note that a file with a .wmv extension is probably compressed with Microsoft's WMV (Windows Media Video) codec, but the file itself is an ASF container file.
ASF files can, in theory, contain video and audio files compressed with any codec. In practice, it's playback that can become a problem, particularly with video compressed with H.264 codecs. If you're planning on remaining within the Microsoft universe, ASF is fine, but you might have problems with non-Microsoft media.
Audio Video Interleave AVI is an older Microsoft container format. It's still fairly common, but you probably won't want to use it with new projects.

QuickTime: QuickTime is Apple's own container format. QuickTime sometimes gets criticized because codec support (both audio and video) is limited to whatever Apple supports. This is true, but QuickTime supports a large array of codecs for audio and video. Apple is a strong proponent of H.264, so QuickTime files can contain H.264-encoded video.
MP4: People sometimes confuse MP4 files with QuickTime, but they're actually different standards. MP4 is another container format developed by the Motion Pictures Expert Group, and is more technically known as MPEG-4 Part 14. Video inside MP4 files are encoded with H.264, while audio is usually encoded with AAC, but other audio standards can also be used.
MPEG and BDAV MPEG-2 Transport Streams: These are the container formats used in DVDs and Blu-ray discs, respectively. The VOB (Video Objects) container file format is a subset of the MPEG transport stream, and is used specifically in DVD video creation. (VOBs can also hold other types of data, but that's beyond what we're discussing here.) MPEG-2 Transport Streams, as the name suggests, uses video compressed with MPEG-2 Part 2 encoders, but it's actually not limited to MPEG-2. MPEG-2 TS data can also be compressed with H.264 and VC-1, since those are also defined as part of the Blu-ray standard. Audio files can be Dolby Digital (AC3) files, Dolby Digital Plus, Dolby Lossless, DTS, DTS HD, and Linear PCM (uncompressed) multichannel audio data.
AVCHD: This is the standard container used by many camcorders. Video captured with these devices is compressed with the H.264 AVC codec. Audio is encoded as Dolby Digital (AC3) or uncompressed linear PCM.

Flash: Adobe's own container format is Flash, which supports a variety of codecs. More recent Flash video is encoded with H.264 video and AAC audio codecs, but don't expect all Flash sites to use only those codecs, particularly if the video was created and encoded in years past.
Others: A variety of other formats are widely used, particularly for delivering video over the Internet. These include Matroska (.mkv), OGG (and the related containers OGM and OGV), and DiVX.

How to Use Your Codecs and Containers

First, let's consider what amateur videographers who are creating and editing their own videos need to consider. One key thing you'll have to invest in is storage--lots of it.
My own video projects consist of mixed video and audio, plus photographs. I shoot video using digital HD camcorders that record AVCHD files, but I often get files delivered to me in QuickTime (.mov) and AVI formats. I have a few older files rendered in MJPEG, and someday I'll probably transcode those to a more modern format.
All these live on a combination of a 2TB RAID 1 array on my production PC, or on a 4TB home server attached to the network. So far, I haven't had to archive off any data, but if I do, I'll probably use external hard drives, rather than tape-based or optical-disc-based media.

Editing video in Adobe Premiere Elements 9.
I tend to keep videos on my home server in their original formats, and to use Adobe Premiere Pro CS5 to create videos that are delivered via disc formats; my most recent projects involved Blu-ray delivery.
If you're more interested in playing other people's videos instead of making your own, your needs will be different--if you plan on ripping your personal collection of DVDs so you can stream them over your home network, most container formats should do fine. However, you might want to use the format that's best suited for your delivery vehicle. For example, if you're using the freely available VLC Media Player and all the client hardware in your home consists of PCs or Macs, ripping to MP4 containers is a good choice--you get a good mix of compression versus storage requirements.
On the other hand, if you're storing video on a home server, but delivering it to a set-top box or a portable media player, you'll need to know what that particular device supports. Right now, most networked CE devices support a mixed bag of standards, and you need to know which formats will play. You're almost always safe encoding in MPEG-2 in those cases, but high-quality MPEG-2--particularly high-definition video--can consume a lot of storage space.

Looking Forward

Right now, so many codecs and containers are available that it's hard to ensure your video can be watched by everyone out there. The good news is that the future seems to lie with variations on the H.264 codec and MP4 containers. If you store your video in those formats today, transcoding to other widely supported formats (QuickTime, Flash, ASF) becomes pretty simple.
I've really only scratched the surface on codecs and containers. It's enough to get you started, but if you want to dive deeper, here are a few online resources.
Wikipedia: Wikipedia offers a good starting point if you want to dip your toes a bit deeper. Check out the entries on Comparison of Video Codecs article and Comparison of Container Formats. The descriptions are terse, but these entries have lots of links to deeper articles.
Master New Media's Video Encoding Guide: Robin Goode's article on codecs and container formats is a great jumping-off place, with links to other great articles, plus good basic information.
High Definition on Youtube: If you're looking to get the most out of uploading your HD videos to YouTube, this is a concise, useful place to start.

1 comment:

  1. This clears my lots of basic doubts on codecs and containers. Thanks again Rajeev