[FFmpeg-user] Using custom thread pool via setting execute and execute2

Olzhas Zhumabek anonymous.from.applecity at gmail.com
Wed Oct 13 14:40:10 EEST 2021


Background of my problem:
I've got an app that has multiple video streams. Since thread pool is
created per context, I've got way too many threads that interfere with a
performance critical part of my app. The solution for this I believe is to
ask ffmpeg to use my threads that I pinned as a thread pool by setting the
execute and execute2 function pointer variables in AVCodecContext.

Current problem:
Here is the example of my AVCodecContext creation stripped of error
checking (further uses of AVCodecContext do work and behave as expected,
just the specified handlers are not called):

AVCodec* codec = /*properly init, tested that works*/;
AVCodecContext* context = avcodec_alloc_context3(codec_);
context_->execute = &thread_execute;
context_->execute2 = &thread_execute2;
context_->thread_count = 0;
context_->thread_type = FF_THREAD_SLICE;
context_->active_thread_type = FF_THREAD_SLICE;

avcodec_open2(context, codec, NULL);

/* some usage which works afterwards*/

The problem is that the functions thread_execute and thread_execute2 are
not called. The codec library used is libx264 (checked that it is the one

Could you please advise on where I should search for errors in my code (why
the execute handler function is not called)?

Here are the functions I specified:

int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void
*arg2), void* arg, int* ret, int count, int size)
    // Do it all serially for now
    COMMON_LOG_SS(Info, "thread_execute IS CALLED !!!!!!!");

    for (int k = 0; k < count; ++k)
        ret[k] = func(s, arg);

    return 0;

int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2,
void* arg2, int, int), void* arg, int* ret, int count)
    // Do it all serially for now
    COMMON_LOG_SS(Info, "thread_execute2 IS CALLED !!!!!!!");

    for (int k = 0; k < count; ++k)
        ret[k] = func(s, arg, k, count);

    return 0;

I have tried setting debug points into the functions I created, but they do
not hit (I used the debug build of my app). I have also verified via
debugger that the handler are not changed after calling avcodec_open2.


More information about the ffmpeg-user mailing list