#include "ffmpeg_consumer.h"\r
\r
#include "../producer/audio/audio_resampler.h"\r
+#include "../producer/tbb_avcodec.h"\r
\r
#include <core/frame/frame.h>\r
#include <core/mixer/audio/audio_util.h>\r
if(output_format_.format->flags & AVFMT_GLOBALHEADER)\r
c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
\r
- c->thread_count = boost::thread::hardware_concurrency();\r
- if(avcodec_open(c, encoder) < 0)\r
- {\r
- c->thread_count = 1;\r
- THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
- }\r
+ //c->thread_count = boost::thread::hardware_concurrency();\r
+ THROW_ON_ERROR2(tbb_avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
\r
return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
{\r
- LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");\r
+ LOG_ON_ERROR2(tbb_avcodec_close(st->codec), "[ffmpeg_consumer]");\r
av_freep(&st->codec);\r
av_freep(&st);\r
});\r
\r
namespace caspar {\r
\r
+static const int MAX_THREADS = 16; // See mpegvideo.h\r
+\r
int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)\r
{\r
tbb::parallel_for(0, count, 1, [&](int i)\r
}\r
\r
int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)\r
-{ \r
- tbb::atomic<int> counter; \r
- counter = 0; \r
-\r
- CASPAR_VERIFY(tbb::tbb_thread::hardware_concurrency() <= 16);\r
- // Note: this will probably only work when tbb::task_scheduler_init::num_threads() < 16.\r
- tbb::parallel_for(tbb::blocked_range<int>(0, count, 2), [&](const tbb::blocked_range<int> &r) \r
- { \r
- int threadnr = counter++; \r
- for(int jobnr = r.begin(); jobnr != r.end(); ++jobnr)\r
- { \r
- int r = func(s, arg, jobnr, threadnr); \r
- if (ret) \r
- ret[jobnr] = r; \r
- }\r
- --counter;\r
+{ \r
+ std::array<std::vector<int>, 16> jobs;\r
+ \r
+ for(int n = 0; n < count; ++n) \r
+ jobs[(n*MAX_THREADS) / count].push_back(n); \r
+ \r
+ tbb::parallel_for(0, MAX_THREADS, [&](int n) \r
+ {
+ BOOST_FOREACH(auto k, jobs[n])
+ {
+ int r = func(s, arg, k, n);
+ if(ret)
+ ret[k]= r;
+ }\r
}); \r
\r
- return 0; \r
+ return 0; \r
}\r
\r
void thread_init(AVCodecContext* s)\r
{\r
- static const size_t MAX_THREADS = 16; // See mpegvideo.h\r
static int dummy_opaque;\r
\r
s->active_thread_type = FF_THREAD_SLICE;\r