Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
I would have expected it to be the other way 'round as well. Maybe the MJPEG encoder is not as optimized as the MPEG-4 encoder (are we talking libavcodec?) – slhck Aug 12 '11 at 10:24
yes. It's libavcodec. I use OpenCV for encoding which, in turn, uses FFMPEG/libavcodec. – umair Aug 12 '11 at 11:00

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).

share|improve this answer
At present, the camera is covering a completely static scene. Is it possible that the static scene increases the performance of MPEG-4 (as it works on motion vectors) and there is not much overhead involved in MPEG-4 encoding in this case? Could it be the reason why it is performing much better than MJPEG in this case? As I said earlier, every time I capture a frame from the camera, I write it down to a file using OpenCV/FFMPEG (in MPEG-4 and MJPEG format respectivley). – umair Aug 12 '11 at 11:04
In this case, yes, because the predictive encoding of a static scene is highly efficient. This is where MPEG-4 beats MJPEG. – slhck Aug 12 '11 at 11:21
@slhck it is only efficient with respect to the compression ratio, it does not encode faster. That being said, if it is a static scene, you can reduce the number of I/B frames and decrease the macroblock calculations to increase your encoding speed without affecting quality too much. – Breakthrough Aug 12 '11 at 13:06
@Breakthrough Given the right MB search algorithm, yup. I guess though that in this case, the implementation of the MJPEG encoder is rather weak. – slhck Aug 12 '11 at 13:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .