From 6e63650c1f24bbd07a84e730534b8f01bed1073f Mon Sep 17 00:00:00 2001 From: ronag Date: Mon, 12 Mar 2012 20:49:29 +0000 Subject: [PATCH] 2.1.0: -ffmpeg_consumer: Use tbb_avcodec. git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches/2.1.0@2616 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d --- modules/ffmpeg/consumer/ffmpeg_consumer.cpp | 11 +++---- modules/ffmpeg/producer/tbb_avcodec.cpp | 35 ++++++++++----------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/modules/ffmpeg/consumer/ffmpeg_consumer.cpp b/modules/ffmpeg/consumer/ffmpeg_consumer.cpp index 276ddebe1..12ff96589 100644 --- a/modules/ffmpeg/consumer/ffmpeg_consumer.cpp +++ b/modules/ffmpeg/consumer/ffmpeg_consumer.cpp @@ -26,6 +26,7 @@ #include "ffmpeg_consumer.h" #include "../producer/audio/audio_resampler.h" +#include "../producer/tbb_avcodec.h" #include #include @@ -407,16 +408,12 @@ public: if(output_format_.format->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; - c->thread_count = boost::thread::hardware_concurrency(); - if(avcodec_open(c, encoder) < 0) - { - c->thread_count = 1; - THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]"); - } + //c->thread_count = boost::thread::hardware_concurrency(); + THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]"); return std::shared_ptr(st, [](AVStream* st) { - LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]"); + LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]"); av_freep(&st->codec); av_freep(&st); }); diff --git a/modules/ffmpeg/producer/tbb_avcodec.cpp b/modules/ffmpeg/producer/tbb_avcodec.cpp index e53ab34b0..91d6071d1 100644 --- a/modules/ffmpeg/producer/tbb_avcodec.cpp +++ b/modules/ffmpeg/producer/tbb_avcodec.cpp @@ -47,6 +47,8 @@ extern "C" namespace caspar { +static const int MAX_THREADS = 16; // See mpegvideo.h + int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size) { tbb::parallel_for(0, count, 1, [&](int i) @@ -60,30 +62,27 @@ int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2 } int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count) -{ - tbb::atomic counter; - counter = 0; - - CASPAR_VERIFY(tbb::tbb_thread::hardware_concurrency() <= 16); - // Note: this will probably only work when tbb::task_scheduler_init::num_threads() < 16. - tbb::parallel_for(tbb::blocked_range(0, count, 2), [&](const tbb::blocked_range &r) - { - int threadnr = counter++; - for(int jobnr = r.begin(); jobnr != r.end(); ++jobnr) - { - int r = func(s, arg, jobnr, threadnr); - if (ret) - ret[jobnr] = r; - } - --counter; +{ + std::array, 16> jobs; + + for(int n = 0; n < count; ++n) + jobs[(n*MAX_THREADS) / count].push_back(n); + + tbb::parallel_for(0, MAX_THREADS, [&](int n) + { + BOOST_FOREACH(auto k, jobs[n]) + { + int r = func(s, arg, k, n); + if(ret) + ret[k]= r; + } }); - return 0; + return 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; -- 2.39.5