Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ffmpeg: which functions are multithreading safe?

Tags:

ffmpeg

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?

like image 424
Albert Avatar asked Mar 12 '13 16:03

Albert


People also ask

Is FFmpeg thread 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.

What are thread safe functions?

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.

Which interface is thread safe?

So, a routine is MT-safe when it is thread safe and its execution does not negatively affect performance.


1 Answers

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.

like image 137
Albert Avatar answered Oct 27 '22 15:10

Albert