Is LibAV/FFMpeg thread safe? For example. Could i read from a file using AVFormatContext* in one thread and decode the read packet it in another with simple additions of mutexes or is the thread safetyness of the library a "don't know don't care" type deal? I know that libav has basic support for encoder threads but i'm trying more of a blackbox type approach where i break it up into multiple threads (Source -> Decoder -> Filter -> Encoder -> Sink) and trying to understand the complications of such.
Anyone with any experience with ffmpeg and threads and would like to chime in with any other info pertinent to this would also be greatly appreciated.
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.
Thread safety is the technique which manipulates shared data structure in a manner that guarantees the safe execution of a piece of code by the multiple threads at the same time. A code is called thread-safe. If it is run concurrently without break function.
When multiple threads are working on the same data, and the value of our data is changing, that scenario is not thread-safe and we will get inconsistent results. When a thread is already working on an object and preventing another thread on working on the same object, this process is called Thread-Safety.
You can register your own lock manager. The ffmpeg library will control thread safety.
Example:
::av_lockmgr_register(&my_lockmgr_cb);
//// ..........
int my_lockmgr_cb(void **mutex, enum AVLockOp op)
{
if (NULL == mutex)
return -1;
switch(op)
{
case AV_LOCK_CREATE:
{
*mutex = NULL;
boost::mutex * m = new boost::mutex();
*mutex = static_cast<void*>(m);
break;
}
case AV_LOCK_OBTAIN:
{
boost::mutex * m = static_cast<boost::mutex*>(*mutex);
m->lock();
break;
}
case AV_LOCK_RELEASE:
{
boost::mutex * m = static_cast<boost::mutex*>(*mutex);
m->unlock();
break;
}
case AV_LOCK_DESTROY:
{
boost::mutex * m = static_cast<boost::mutex*>(*mutex);
delete m;
break;
}
default:
break;
}
return 0;
}
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