Why does encoding a video using the MJPEG codec take longer than MPEG-4?

6,689

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:
6,689

Related videos on Youtube

umair
Author by

umair

Updated on September 18, 2022

Comments

  • umair
    umair almost 2 years

    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?

    • slhck
      slhck almost 13 years
      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?)
    • umair
      umair almost 13 years
      yes. It's libavcodec. I use OpenCV for encoding which, in turn, uses FFMPEG/libavcodec.
  • umair
    umair almost 13 years
    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).
  • slhck
    slhck almost 13 years
    In this case, yes, because the predictive encoding of a static scene is highly efficient. This is where MPEG-4 beats MJPEG.
  • Breakthrough
    Breakthrough almost 13 years
    @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.
  • slhck
    slhck almost 13 years
    @Breakthrough Given the right MB search algorithm, yup. I guess though that in this case, the implementation of the MJPEG encoder is rather weak.