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 6196619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:40:35+00:00 2026-05-24T03:40:35+00:00

I’m trying to create a small ffmpeg hack which enables parallel executin of the

  • 0

I’m trying to create a small ffmpeg “hack” which enables parallel executin of the yadif filter.

I think I have found a solution, however there can only be one concurrent instance of it. This is because the “scalable_yadif_context” is local to the function “scalable_yadif_filter_line1” which replaces the original yadif “filter_line” function. I could make the “scalable_yadif_context” thread local, however since this function is called often it would have a quite high overhead.

Any ideas as how to solve this issue?

// We need the context description in order to access the original filter_line function. Just redefine it here and hope that it is not changed inside of libavfilter.
typedef struct {
    int mode;
    int parity;
    int frame_pending;
    int auto_enable;
    AVFilterBufferRef *cur;
    AVFilterBufferRef *next;
    AVFilterBufferRef *prev;
    AVFilterBufferRef *out;
    void (*filter_line)(uint8_t *dst,
                        uint8_t *prev, uint8_t *cur, uint8_t *next,
                        int w, int prefs, int mrefs, int parity, int mode);
    const AVPixFmtDescriptor *csp;
} YADIFContext;

struct scalable_yadif_context
{
    std::vector<std::function<void()>> calls;
    int end_prefs;

    scalable_yadif_context() : end_prefs(std::numeric_limits<int>::max()){}
};

void (*org_yadif_filter_line)(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) = 0;

void scalable_yadif_filter_line(scalable_yadif_context& ctx, uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode)
{
    if(ctx.end_prefs == std::numeric_limits<int>::max())
        ctx.end_prefs = -prefs;  // Last call to filter_line will have negative pref

    ctx.calls.push_back([=]
    {
        org_yadif_filter_line(dst, prev, cur, next, w, prefs, mrefs, parity, mode);
    });    

    if(prefs == ctx.end_prefs)
    {       
        tbb::parallel_for(tbb::blocked_range<size_t>(0, ctx.calls.size()), [=](const tbb::blocked_range<size_t>& r)
        {
            for(auto n = r.begin(); n != r.end(); ++n)
                ctx.calls[n]();
        });
        ctx.calls.clear();
        ctx.end_prefs = std::numeric_limits<int>::max();
    }
}

void scalable_yadif_filter_line1(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode)
{
     // local to the current function, making this thread local would incur heavy overhead.
    static scalable_yadif_context ctx;
    scalable_yadif_filter_line(ctx, dst, prev, cur, next, w, prefs, mrefs, parity, mode);
}

void make_scalable_yadif(AVFilterContext* ctx)
{
    YADIFContext* yadif = (YADIFContext*)ctx->priv;

    // Data race should not be problem since we are always writing the same value
    org_yadif_filter_line = yadif->filter_line;

    // hmm, will only work for one concurrent instance... 
    // I need a unique "scalable_yadif_filter_line1" for each call...
    yadif->filter_line = scalable_yadif_filter_line1; 
}

I’ve created an extremely ugly solution that works for up to 18 concurrent instances.

#define RENAME(a) f ## a

#define ff(x) \
void RENAME(x)(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) \
{\
    static scalable_yadif_context ctx;\
    scalable_yadif_filter_line(ctx, dst, prev, cur, next, w, prefs, mrefs, parity, mode);\
}

ff(0); ff(1); ff(2); ff(3); ff(4); ff(5); ff(6); ff(7); ff(8); ff(9); ff(10); ff(11); ff(12); ff(13); ff(14); ff(15); ff(16); ff(17);

void (*fs[])(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) = 

{f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17};

namespace caspar {

void init()
{
    for(int n = 0; n < 18; ++n)
        tags.push(n);
}

int make_scalable_yadif(AVFilterContext* ctx)
{
    static boost::once_flag flag = BOOST_ONCE_INIT;
    boost::call_once(&init, flag);

    YADIFContext* yadif = (YADIFContext*)ctx->priv;
    org_yadif_filter_line = yadif->filter_line;

    int tag;
    if(!tags.try_pop(tag))
    {
        LOG(warning) << "Not enough scalable-yadif instances. Running non-scalable";
        return -1;
    }

    yadif->filter_line = fs[tag];
    return tag;
}

void release_scalable_yadif(int tag)
{
    if(tag != -1)
        tags.push(tag);
}
  • 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-24T03:40:36+00:00Added an answer on May 24, 2026 at 3:40 am

    Why not just pass a per-thread buffer to the scalable_yadif_filter_line1 function? It might require a bit of reorganization, but it’s far better than using statics or thread-locals (after all, what happens to your thread local buffer if the thread goes on to do something else?)

    If you cannot pass a buffer to the function (due to a fixed ffmpeg API), TLS is probably your only choice. Overhead isn’t as bad as you might think, but it’s still not that good. I’d highly recommend looking into modifying ffmpeg to add a context parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.