I am testing the encoding performance (MPEG-4 and MJPEG) of a smart camera. I have written an application in OpenCV/FFMPEG for performing encoding, where the application captures images from the camera and encodes them to a desired encoding format. In the benchmarks, I came to know that MJPEG encoding is taking much longer than MPEG-4 encoding. I expected it to be other way around. Encoding a single frame to MPEG-4 takes around 31ms, whereas encoding to MJPEG takes around 80ms. Does MJPEG really takes such a long time in comparison to MPEG-4?
2 Answers
The two compression algorithms are completely different in the way they function, and MPEG4 can use GPU acceleration as well.
MPEG4 is a true video codec, whereas MJPEG simply compresses each frame into a separate JPEG image. The difference is that MPEG4 uses a variety of techniques (motion vector compensation, I/B frames, etc...) to enhance both quality and compression ratio.
Why one works faster then the other case really depends on how the encoder was implemented, and on how your particular hardware performs when encoding. Some encoders can use special CPU instructions (SSE/SSE2, MMX, etc...), or use GPU acceleration (I know you probably don't, but I'm just mentioning it). JPEG compression is largely CPU speed dependent, and doesn't really use any instruction set enhancements (for compatibility/stability, and the fact that it doesn't really help - see the changelog entry for November 16, 2006).
Lastly, unless you have a single-frame video, MPEG4 never really encodes a "single frame" at a time. It always performs frame look ahead/behind to determine better ways to compress the current frame (MJPEG does only do a single frame at a time). As such, it is largely dependent on the data before/after the frame, not only on the current one. This occurs in single-pass encoding as well (that's why it uses the motion vectors in the first place, after all).
- 34,847
MPEG-4 encoding is much more cpu-intensive than MJPEG. Your camera likely has MPEG4 encoding hardware built-in, which is why it can process MP£G4 data mushc quicker.