Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5934027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:03:22+00:00 2026-05-22T15:03:22+00:00

I just started using the latest build of ffmpeg into which ffmpeg-mt has been

  • 0

I just started using the latest build of ffmpeg into which ffmpeg-mt has been merged.

However, since my application uses TBB (Intel Threading Building Blocks), the ffmpeg-mt imlementation with new thread creation and synchronization does not quite fit, as it could potentially block my TBB tasks executing the decode functions. Also it would trash the cache unnecessarily.

I was looking around in pthread.c which seems to implement the interface which ffmpeg uses to enable multithreading.

My question is whether it would be possible to create a tbb.c which implements the same functions but using tbb tasks instead of explicit threads?

I am not experienced with C, but my guess is that it would not be possible to easily compile tbb (which is C++) into ffmpeg. So maybe somehow overwriting the ffmpeg function pointers during run-time would be the way to go?

I would appreciate any suggestions or comments in regards to implementing TBB into ffmpeg threading api.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:03:23+00:00Added an answer on May 22, 2026 at 3:03 pm

    So I figured out how to do it by reading through the ffmpeg code.

    Basicly all you have to do is to include the code below and use tbb_avcodec_open/tbb_avcodec_close instead of ffmpegs’ avcodec_open/avcodec_close.

    This will use TBB tasks to execute decoding in parallel.

     // Author Robert Nagy
    
    #include "tbb_avcodec.h"
    
    #include <tbb/task.h>
    #include <tbb/atomic.h>
    
    extern "C" 
    {
        #define __STDC_CONSTANT_MACROS
        #define __STDC_LIMIT_MACROS
        #include <libavformat/avformat.h>
    }
    
    int task_execute(AVCodecContext* s, std::function<int(void* arg, int arg_size, int jobnr, int threadnr)>&& func, void* arg, int* ret, int count, int size)
    {   
        tbb::atomic<int> counter;
        counter = 0;
    
        // Execute s->thread_count number of tasks in parallel.
        tbb::parallel_for(0, s->thread_count, 1, [&](int threadnr) 
        {
            while(true)
            {
                int jobnr = counter++;
                if(jobnr >= count)
                    break;
    
                int r = func(arg, size, jobnr, threadnr);
                if (ret)
                    ret[jobnr] = r;
            }
        });
    
        return 0;
    }
    
    int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)
    {
        return task_execute(s, [&](void* arg, int arg_size, int jobnr, int threadnr) -> int
        {
            return func(s, reinterpret_cast<uint8_t*>(arg) + jobnr*size);
        }, arg, ret, count, size);
    }
    
    int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)
    {
        return task_execute(s, [&](void* arg, int arg_size, int jobnr, int threadnr) -> int
        {
            return func(s, arg, jobnr, threadnr);
        }, arg, ret, count, 0);
    }
    
    void thread_init(AVCodecContext* s)
    {
        static const size_t MAX_THREADS = 16; // See mpegvideo.h
        static int dummy_opaque;
    
        s->active_thread_type = FF_THREAD_SLICE;
        s->thread_opaque      = &dummy_opaque; 
        s->execute            = thread_execute;
        s->execute2           = thread_execute2;
        s->thread_count       = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible.
    }
    
    void thread_free(AVCodecContext* s)
    {
        s->thread_opaque = nullptr;
    }
    
    int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)
    {
        avctx->thread_count = 1;
        if((codec->capabilities & CODEC_CAP_SLICE_THREADS) && (avctx->thread_type & FF_THREAD_SLICE))
            thread_init(avctx);
    // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.
        return avcodec_open(avctx, codec); 
    }
    
    int tbb_avcodec_close(AVCodecContext* avctx)
    {
        thread_free(avctx);
        // ff_thread_free will not be executed since thread_opaque == nullptr.
        return avcodec_close(avctx); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using CCNet, and in the process of getting my build projects
I've just started using the MVP pattern in the large ASP.NET application that I'm
I've just started using Linq to SQL, and I'm wondering if anyone has any
I have been using JustCode(latest ver) for about 2 months now, and have started
I just started to dig into Boost::Spirit, latest version by now -- V2.4. The
I just downloaded the latest version of Netbeans - 6.9.1. I've been using 6.8
Have just started using Google Chrome , and noticed in parts of our site,
I just started using GNU Emacs as my text editor and I am concerned
I just started using the WPF WebBrowser that is included in Net 3.5 SP1.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.