Tuesday, June 30, 2009

The problem is not MJPEG

You hear it over and over again: Why o why doesn't this particular camera compress to h.264? It would look so much better. Or you hear that MJPEG is a very inefficient, ancient codec. H.264 gives so much better quality.

Well thats true if you compare MJPEG and h.264 video with the same bit rate. H.264 will look better. But thats the only advantage of h.264. The codec keeps file sizes small and video looks very good at low bitrates. This is what makes h.264 the web delivery codec of choice.

Both codecs MJPEG and h.264 apply a Discrete Cosine Transformation/Quantization/Varable Length Encoding which is better known as JPEG compression as the first step. MJPEG stops here, each frame remains a JPEG compressed image but h.264 goes a whole lot further. It stores very few I frames i.e. full JPEG compressed images. What gets stored in the remaining frames is just changes from frame to frame plus there is a lot of prediction and interpolation over a wide frame span going on. While you may not notice interpolating artefacts when you watch a h.264 compressed movie you can't use every frame of it a as a still photo.

With MJPEG you can. The only blur visible will come from motion blur. MJPEG "wastes" storage space but it is closer to the original than any other lossy codec. Given a very high bit rate and good source material MJPEG will be visually lossless.

H.264 will also look visually lossless and it needs a lower data rate to achieve this than MJPEG, but my point is that both codecs MJPEG and h.264 can achieve the same goal: a visually lossless movie as close to the original as possible. If- and that's crucical - if the source is flawless.

Unfortunately the source material is the problem with the current large sensor cameras that shoot video. All are too slow to downsize a 12 Megapixel image properly. Developers choose to skip entire pixel rows from the sensor output and all this shivering and flickering in fine detail regions appears. This is annoying and it degrades the otherwise high quality impression.

No matter if it's the Nikon D90/D5000, the Panasonic GH1, the Pentax K-7 or the Olympus E-P1 the source is bad and that's the reason why the MJPEG compressed clips look so bad as soon as you film a street scene with buildings and edges. Even the Canon 5D mark2 with it's very high bit rate h.264 codec suffers from row skipping and can't deliver the best possible quality because of this.

To sum this up: MJPEG is not bad. It can look just as good as h.264. In order to achieve this it produces larger files. On the other hand editing native h.264 needs lots of processing power. Because of the way individual frames have to be generated it isn't recommended to edit native h.264 anyway. You should bring it into more edit friendly format. If you transfer h.264 to an intermediate codec like e.g. ProRes you'll get huge files plus you have the h.264 original plus this conversion needs time.