Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread locking around avcodec_open/close

I have a c++-cli code that is capturing videos from a folder in opencv using capture and then retrieving frames using cvquery frame. I then process frames and once all frames are processed i release the capture. It works fine but when I try to multithread it gives me a warning and cannot capture some of the videos in the folder with warning " insufficient thread locking around avcodec_open/close()".

//for each video in folder do
{
    capture=cvCreateFileCapture(filename);

    while(1)
    {
        img=cvqueryframe(capture) 

        if !img break;
        ///process img
    }

    cvreleasecapture(&capture);
}

Is there a way to fix the problem for multithreading? I was thinking of using

while(!capture) 
    capture=cvCreateFileCapture(filename);

but there should a more efficient way, maybe using locking Monitor::Enter(obj) or lock(obj)?

like image 251
fmvpsenior Avatar asked Aug 17 '12 19:08

fmvpsenior


1 Answers

The open and close functions in avcodec are not thread safe. To prevent issues in multi-threaded apps they have a simple mechanism that detects when two threads are inside these functions at the same time and when that happens they write the "insufficient thread locking" message.

The way to prevent this message is to lock the calls to cvCreateFileCapture and cvreleasecapture (which in turn call avcodec_open and avcodec_close) to ensure that these calls are always serialized. For example, if you had a mutex class called Mutex you could do something like this:

extern Mutex m; // application-wide mutex

//for each video in folder do
{
    m.lock();
    capture=cvCreateFileCapture(filename);
    m.unlock();

    while(1)
    {
        img=cvqueryframe(capture) 

        if !img break;
        ///process img
    }

    m.lock();
    cvreleasecapture(&capture);
    m.unlock();
}

There are many Mutex implementations. On Linux or OS X you can use pthread mutexes. On Windows you can use Win32 mutexes.

like image 74
Miguel Avatar answered Sep 22 '22 21:09

Miguel