Fun with code (since 2006)

Faster JPEG Encoding with Flash Player 10


05.22.09 Posted in Actionscript 3, ByteArray by

When Flash Player 10 was released, you heard about this new Vector class, if you are not using it already in your FP10 projects, well you definitely should. When talking with some guys at DotEmu, they confessed me that their whole emulation engine (ported from Java to AS3) which was able to emulate any kind of 8bit and 16bit console was not fast enough to be used in production in Flash Player 9.
When Flash Player 10 was released, they switched from Array to Vectors and realized that their engine was running much faster thanks to methods like BitmapData.setVector() and the Vector class.

After a few days I realised that the JPEG encoding class used in the corelib package was using a lot of Arrays and could benefit from the Vector class and I was pretty sure some tricky optimization could be done. That's what I did and modified its code to make it faster, I was happy to see that the new version was around 2.5 times faster (on my machine). Now more than 4 times.

Here is a little demo (encoding a 2880*2880 BitmapData) showing the difference between the old version and the new one, if you could post your results through the comments that would be interesting :

A Flash animation should have appeared here, but it seems that your browser has an older version of the Flash Player or it is not installed at all. Please, install the last release of the Flash Player now, then reload this page.

So what did I do ?

  • I used bitwise operators as much as possible.
  • I replaced all Arrays with fixed length Vectors.
  • I used pre-incrementation rather than post-incrementation (thanks Joa for this one ;)).
  • I casted to int almost all my Vector indices access.
  • Other minor stuff you always do to optimize your code :)

The encoding process could be even faster thanks to Pixel Bender for the RGB to YUV process, unfortunately as you may know loops are forbidden when Pixel Bender jam is running in the Flash Player. Pre-processed the RGB to YUV with a simple ColorMatrixFilter or Pixel Bender, this didn't bring performance improvements. I am sure this can still be optimized if ported to haXe for instance, if some of you are using an asynchronous version of the original version, you can integrate the optimization from this one.

Download it here.

Update : 05/24/09 : New version with tiny additions, which should make it run a little bit faster.
Update : 05/26/09 : New version with tiny additions, a little bit faster. (Thanks Joa and Kyle for your tips)
Update : 05/27/09 : New version with some unuseful code removed.
Update : 05/30/09 : New version with tiny additions, a little bit faster again. (Thanks tst)



212 Responses to “Faster JPEG Encoding with Flash Player 10”

  1. Sly1024 says:

    Awesom work!
    Core 2 Duo 2.4GHz:
    Chrome Flash Player 10

    orig: 6865
    optimized: 2414

  2. ukjoesaar@gmail.com says:

    I have 2Ghz Mac, 4gb ddr2 667Mhz,
    7573ms vs 2709ms. Safari 5.1, Flashplayer 11.1.102

  3. ahmet kucuk says:

    9099 to 3111
    I guess this swf uses GPU, i have nvidia 335m GT and my processor is just 1.3mhz but results re fastest one so it should be related to GPU.
    (everyone published cpu)

  4. Liviu says:

    corelib: 6677 ms
    faster: 1281 ms
    the only attempt

    over 5x times faster

  5. AnryLinkage says:

    Original 3402
    Optimized 676
    Core i7 950, 12G Ram.

  6. menfin says:

    Nice work Thibault, but FileReference().save causes systematic crash in IE9 with flash player 11, so you can’t save JPEG. Is this problem known ? (win 7 64 bits)

  7. saif says:

    This one is 4 times faster for sure, but I found another one which is like 7-8 times faster for this image. Didn’t test it for any other image, you can test and upgrade yours one I guess, Good luck.

    http://blog.inspirit.ru/?p=201

    use the normal one, Async JPGEncoder shows progress but a little slower I guess, but the best thing is flash never get hang for that stupid 15 second limitation.

  8. [...] in must be encoded as a JPG – you can use any of the available encoders such as CoreLib, Faster JPG Encoder, or the Alchemy [...]

     
  9. pa1bmsce says:

    AWESOME JOB :) YOU SAVED MY LIFE

  10. JC Gauthier says:

    Great stuff! I translated it to C# so I can use it in Silverlight. I noticed an issue with incomplete macroblocks (width or height not dividable by 8).
    I fixed it by repeating the last pixel until the block is filled. Now the borders look fine :)

  11. [...] go http://www.bytearray.org/?p=775 it is really good ! This entry was posted in FLEX. Bookmark the permalink. ← GWT and [...]

     

Leave a Reply

Open Sort Options

Sort comments by:
  • * Applied after refresh

ByteArray.org
Fun with code since 2006.