]> git.sesse.net Git - casparcg/commitdiff
2.1.0: -ffmpeg_consumer: Use tbb_avcodec.
authorronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 12 Mar 2012 20:49:29 +0000 (20:49 +0000)
committerronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Mon, 12 Mar 2012 20:49:29 +0000 (20:49 +0000)
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
modules/ffmpeg/producer/tbb_avcodec.cpp

index 276ddebe176a0c68a35cd555a39f1722e4dacba6..12ff965892fdc78d6ff72818d852070032f24b42 100644 (file)
@@ -26,6 +26,7 @@
 #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
@@ -407,16 +408,12 @@ public:
                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
index e53ab34b04da2f84eb3f49c985c1cefc25feef5f..91d6071d1c1e08d52c4caaaa744c864bd53d8a19 100644 (file)
@@ -47,6 +47,8 @@ extern "C"
 \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
@@ -60,30 +62,27 @@ int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2
 }\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