I have read (somewhere, don't remember and find it anymore right now - maybe the question would be obsolete for me otherwise anyway) that some FFmpeg functions are not safe to call from multiple threads at the same time.
FFmpeg itself is not multithreading safe in the sense that you shouldn't call av_read_frame
or avcodec_decode_audio4
on the same context from different threads at the same time - but that is mostly obvious.
But for example, it seems like avformat_find_stream_info
even on separate contexts is not safe to be called from different threads at the same time. So to make that safe, you would need a global mutex.
Is there a list of the functions which are not safe?
FFmpeg itself is not multithreading safe in the sense that you shouldn't call av_read_frame or avcodec_decode_audio4 on the same context from different threads at the same time - but that is mostly obvious.
A threadsafe function protects shared resources from concurrent access by locks. Thread safety concerns only the implementation of a function and does not affect its external interface. In C language, local variables are dynamically allocated on the stack.
So, a routine is MT-safe when it is thread safe and its execution does not negatively affect performance.
Some discussions I found: here or here
From what I have found so far, it seems like only avcodec_open
and avcodec_close
are not thread safe.
However, the correct solution seems to be to use av_lockmgr_register
to register a mutex handler in FFmpeg which is then called automatically by FFmpeg at the needed places. See here or here. Also Chrome does that, see here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With