Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread Safety of LibAv/FFMpeg?

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.

like image 402
user71512 Avatar asked Dec 15 '12 03:12

user71512


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 is thread-safe in C sharp?

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.

What is multithreading How do you do it and what is thread safety?

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.


1 Answers

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;

}

like image 136
pogorskiy Avatar answered Sep 18 '22 12:09

pogorskiy