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

Olzhas Zhumabek anonymous.from.applecity at gmail.com
Wed Oct 13 19:36:43 EEST 2021


Background of my problem:
I've got an app that has multiple video streams. I'm not sure which part of
my app is creating too many threads, but the performance critical section
of my app is getting context switched a lot. I believe there is a thread
pool per av codec context, so I thought I could start there. 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
    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
    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). Print statements were not
executed either. I have also verified via debugger that the handlers are
not changed after calling avcodec_open2.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20211013/fccaec36/attachment.htm>

More information about the Libav-user mailing list