Friday, 26 April 2013

Multicore MJPG

Over the years, I've spent quite a lot of time developing and tweaking my own highly optimised JPG compressor.  It's fast.  I've made heavy use of the Intel Performance Primitives (IPP) to leverage SSE instructions and get impressive performance boost over other third-party libraries I know of.

However, my implementation was limited to a single thread per image, which limited performance when encoding video.  Yes, I know there are ways to parallise the JPG algorithm on a single frame using restart intervals, but that was going to be complex to implement in my existing code.  

For video encoding, I could see a simpler way forward; encode sequential frames concurrently on separate threads.

My new VXJPG library is able to compress each image on its own thread.  So sequential frames are encoded concurrently.  This means that when processing sequences of images, the overall performance is vastly improved.  In fact, on a quad core processor the library pretty much achieves a 4x speedup over the single core version.

Multi-Threaded Compression

The problem with running multiple threads, each encoding a separate frame on a seperate core, is ensuring that the frames remain in sequence when added to the output video file.  Without proper synchronisation, it is entirely possible for the frames to end up out-of-order.

Each thread in a thread pool is launched when the library is initialised, then enters a loop waiting for new data to process.  When a new frame arrives, the next thread in sequence is selected from the pool. The scheduler waits until a 'Ready' event is fired by the thread, to signal that the thread is waiting for new data.  After Ready is signaled, the thread is passed the data and a 'DoProcess' event fired by the scheduler, which allows the thread to proceed onto compressing the frame.  


Figure 1. A single encoding thread waits until a Ready event before processing.  It must then wait for the previous thread to complete before adding its compressed frame to the file.  It can then signal the next thread before returning to wait for more data.
At this point, the scheduler is done and ready to receive a new frame, whilst the thread performs its time-consuming compression task.
 
Inside the thread, after compression completes, each thread must wait on a 'Frame Completed' event from the previous thread in the pool before it is permitted to write to the file.  This ensures that even when all threads are processing concurrently, the frame sequence remains correct.  



Finally, when the frame is written, the thread can signal 'Ready' for when the scheduler next visits.  When the data stops arriving, the threads naturally finish writing frames in sequence and simply all enter the wait state.  

Termination is achieved by signalling a terminate flag in each thread prior to firing the ready event.


Benchmark Compression

I tested the compressor using both single and multi-threaded modes using high def 1920x1080 colour bitmaps, two input frames, repeated 250 times.  

That's 1920x1080 RGB x 500 frames = 3.1GB input data to chew through.  I'm using an Intel Core2 Quad Q8400@2.66GHz to do this, with 4GB DDR3 1066MHz RAM on Win7 32bit.
 
Single Threaded:  
Total time 11.66 seconds
Avg time per frame = 23.2ms
Avg data consumption = 268 MB/sec

Multi Threaded:  
Total time 3.23 seconds
Avg time per frame = 6.5ms
Avg data consumption = 963 MB/sec

I am aware that the input images will be cached, so this is probably a higher performance than I would get with a camera, but its still impressive.  Recording multiple live HD color streams to MJPG is no problem at all even on a modest embedded PC.  MJPG does fill up drives pretty quickly, but is far better than RAW, which requires RAID and SSDs to achieve. 

At present, I'm using vxMJPG for my (sorry, shameless plug) Gecko GigE recorder with Stemmer Imaging cameras for high speed video recording projects.  The output (*.mjpg) files are my own thin custom container format but will play and can be transcoded in VLC.  Unfortunately, they don't play in Windows media player.  That's the price of speed.  To help portability, I also have my own MJPG player for the files which has some features like slow-mo, frame step, save frame etc to help.

My next project is to wrap this up in a DirectShow encoder, so that I can also make AVI files. However, that might have to be single threaded.  I cannot currently see a way of making the encoder in a DS filter graph safely asynchronous in the manner I am employing.  Fortunately, even single threaded, my version should be vastly faster then the codecs I am currently using - and I shall report those results when I have them.
 
Vision Experts

35 comments:

  1. I actually enjoyed reading through this posting.Many thanks.







    Image Processing Company

    ReplyDelete
  2. Intinya tim https://ultiqq.com yg draw tak ikut dalam perhitungan.

    ReplyDelete
  3. Maka kepada kejuaraan Agen poker terpercaya ifk varnamo odds won 1/2 ialah { (1.96 – 1) : 1/2 } +1 = 1.48.

    ReplyDelete
  4. Kepada rahasia https://mdominoqq.biz perhitungannya { 2.01 x 1.89 x 1.96 } : 2 = 3.72 x aset( = produk yg di dapat.

    ReplyDelete
  5. Menang seluruh https://murah99.net maupun berlutut sebanyak itu konsisten bakal di taksir ya.

    ReplyDelete
  6. Hal hal yang harus Poker di pahami sebelum bermain meliputi strategi dan cara bermain poker online.

    ReplyDelete
  7. (contoh: j-k-q-8-9) angka tertinggi Bandarq adalah ‘k’ one pair merupakan kombinasi kartu 1 pasang saja.

    ReplyDelete
  8. Karena resikonya https://angka99.net cukup besar jika anda melakukan kesalahan.

    ReplyDelete
  9. Di web judi https://atomqq.com sepak bola tidak sedikit elemen yg dapat kamu mainkan.

    ReplyDelete
  10. Ada beberapa https://sahqq.com cara main yg teramat wajib.

    ReplyDelete
  11. Tentu ada Pelangikita sebahagian orang yg aneh tahu penyakit aspek ini.

    ReplyDelete
  12. Tapi metode yg seperti ini tak http://pelangi99.cc bisa di pakai dengan cara tengah terusan lantaran sanggup menciptakan kamu menderita kekalahan.

    ReplyDelete
  13. Permainan poker http://sahabat99.cc mempunyai 10 fusi pokok card yg sanggup kamu temukan guna meja permainan.

    ReplyDelete
  14. Arahan, taktik, dan http://pasarqq.co resep main-main poker online server pkv.

    ReplyDelete
  15. Pada menguliahi http://ratu99.me saingan kamu laksanakan fold guna putaran tersebut.

    ReplyDelete
  16. Seandainya telah menggerapai http://raja99.cc untuk akhir permainan kamu sanggup serentak melaksanakan all-in bila kamu percaya dgn card anda.

    ReplyDelete
  17. Nah, disini hamba http://bandarjud1qq.com dapat meneruskan kamu tata cara bagaimana.

    ReplyDelete
  18. Sampel( q-q-9-8-a) two pair Rajawali qq yakni fusi card yg mempunyai 2 pasang kartu.

    ReplyDelete
  19. Terus tidak sedikit http://memberqq.me tengah metode main yg mampu di kuasai.

    ReplyDelete
  20. Sampel( a♣-6♣-8♣-4♣-5♣). full house http://indo99.cc card peleburan ini merupakangabungan bersumber one pair dan pula three of kind.

    ReplyDelete
  21. Tambahan pula https://asikqiu.co kamu seseorang pemula di jurusan poker online.

    ReplyDelete
  22. Sebelum main-main http://qqahli.net poker online tentunya kamu mesti tahu lebihlebih dahulu.

    ReplyDelete
  23. Sample( k-k-k-k-4) straight http://ikanqqiu.net flush yakni adukan bersumber penyatuan straight dan flush.

    ReplyDelete
  24. Soalnya terhadap http://janjiqiu.com rata-rata jikalau mereka yg telah memperoleh card yg bagus.

    ReplyDelete
  25. Sampai-sampai http://jadiqq.me kamu satu orang pemula di bagian poker online.

    ReplyDelete
  26. Tambahan pula kamu terus Murah qq seseorang pemula yg main judi poker online duit asli.

    ReplyDelete
  27. Tapi proses yg seperti ini tak http://campionqq.me bisa di pakai dengan cara lagi terusan lantaran mampu menciptakan kamu terserang kekalahan.

    ReplyDelete
  28. Tidak sedikit berasal pemain http://masterdominoqq.cc poker yg tidak jarang melalaikan ke-2 elemen ini disaat udah mulai sejak memperoleh card yg bagus.

    ReplyDelete
  29. Berikut lah formula Situs mdomino99 memperkirakan mix parlay, mudah-mudahan berguna guna kalian yah.

    ReplyDelete
  30. Atlanta Title Pawn is licensed in title lending in the state Atlanta title pawn of Georgia and specializes in providing auto title loans (cars, vans, trucks, RVs, motorcycles, boats, jet-skis, etc) to residents of the Atlanta metro area.

    ReplyDelete
  31. We work with professional click here to read researchers with vast experience in their different fields to bring you credible information you can rely on to better your life.

    ReplyDelete


  32. You can't believe what i just got,… A loan of $ 60,000. I have been looking for a loan for the past 2years until i was referred to a legitimate lender. Though it was not that easy to approve my loan, as you know nothing good comes easy. But I got my loan within 4 hours i got my loan, and before i knew it, the loan was transferred to me. please friends, don't let any body deceive you and scam you for this is real. Contact them via Email: gaincreditloan1@gmail.com OR You can also whatsApp them at: at +1-(901)676-0641 (WhatsApp Only)

    ReplyDelete
  33. REKOMENDASI situs judi qq poker domino99 online yang terpercaya. mungkin anda bingung dengan banyaknya agen situs judi online yang ada di indonesia. tidak semua agen situs judi online bisa anda percaya. jadi pastikan anda tidak salah memilih situs tempat anda bermain. link di bawah ini merupakan agen situs judi online yang sudah terbukti kwalitasnya. saya sangat merekomendasikan anda untuk bermain di salah satu agen yang ada di link tersebut.


    situs99dominoqq Daftar Agen situs PKV games terbaik dan terpercaya tahun 2020. tingkat kemenangan mencapai 100%. peluang menang sangat besar, daftar dan gabung dengan agen yang sudah terbukti dengan klik link dibawah ini :


    situs99dominoqq << silahkan di klik <<<

    ReplyDelete