Search code examples
c++clibavlibavcodec

Libav multi-threaded decoding


According to the documentation here, Libav provides the "infrastructure" for multithreaded decoding. But the docs are vague and confusing regarding how multithreaded decoding is implemented. Is it internally supported and just requires setting a flag in the struct, or does the user have to provide his own implementation with the provided functions? I searched a lot but could not find even one example of multithreaded video decoding with libav.


Solution

  • The link you have referred to looks like a description towards codec developers rather than to end-user of FFmpeg libraries using existing codecs.

    Multi-threaded support is indeed implemented by framework itself - it requires FFmpeg to be built with thread support (like --enable-pthreads or --enable-w32threads configure options), varies across specific codecs (e.g. one codec may support multiple threads while others don't) and implement different approaches (decoding multiple frames in parallel or multiple slices within a single frame).

    End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2()) and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE). Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.

    Some documents refer that AVCodecContext::thread_count default value set to 0 allows FFmpeg to automatically decide how many threads to use (which will be done based on a number of logical CPUs in system), but I've never tried this (always set this parameter manually). So probably it already does multi-threaded decoding on your system - check CPU load in task manager.

    What FFmpeg doesn't do is managing multiple threads for reading packets from a file, decoding different streams in different threads and other similar things which a video player normally does - this is normally implemented by application itself. Although I recall some features have been integrated to FFmpeg simplifying implementation of these routines (like a packets queue).