]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/tbb_avcodec.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ffmpeg / tbb_avcodec.cpp
1 // Author Robert Nagy\r
2 \r
3 #include "stdafx.h"\r
4 \r
5 #include "tbb_avcodec.h"\r
6 \r
7 #include <common/log/log.h>\r
8 #include <common/env.h>\r
9 \r
10 #include <tbb/task.h>\r
11 #include <tbb/atomic.h>\r
12 #include <tbb/tbb_thread.h>\r
13 \r
14 #include <regex>\r
15 #include <boost/algorithm/string.hpp>\r
16 \r
17 extern "C" \r
18 {\r
19         #define __STDC_CONSTANT_MACROS\r
20         #define __STDC_LIMIT_MACROS\r
21         #include <libavformat/avformat.h>\r
22 }\r
23 \r
24 namespace caspar {\r
25                 \r
26 int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)\r
27 {\r
28         tbb::parallel_for(tbb::blocked_range<size_t>(0, count), [&](const tbb::blocked_range<size_t>& r)\r
29         {\r
30                 for(size_t n = r.begin(); n != r.end(); ++n)            \r
31                 {\r
32                         int r = func(s, reinterpret_cast<uint8_t*>(arg) + n*size);\r
33                         if(ret)\r
34                                 ret[n] = r;\r
35                 }\r
36         });\r
37 \r
38         return 0;\r
39 }\r
40 \r
41 int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)\r
42 {       \r
43         tbb::atomic<int> counter;   \r
44     counter = 0;   \r
45 \r
46         CASPAR_ASSERT(tbb::tbb_thread::hardware_concurrency() < 16);\r
47         // Note: this will probably only work when tbb::task_scheduler_init::num_threads() < 16.\r
48     tbb::parallel_for(tbb::blocked_range<int>(0, count, 2), [&](const tbb::blocked_range<int> &r)    \r
49     {   \r
50         int threadnr = counter++;   \r
51         for(int jobnr = r.begin(); jobnr != r.end(); ++jobnr)\r
52         {   \r
53             int r = func(s, arg, jobnr, threadnr);   \r
54             if (ret)   \r
55                 ret[jobnr] = r;   \r
56         }\r
57         --counter;\r
58     });   \r
59 \r
60     return 0;  \r
61 }\r
62 \r
63 void thread_init(AVCodecContext* s)\r
64 {\r
65         static const size_t MAX_THREADS = 16; // See mpegvideo.h\r
66         static int dummy_opaque;\r
67 \r
68     s->active_thread_type = FF_THREAD_SLICE;\r
69         s->thread_opaque          = &dummy_opaque; \r
70     s->execute                    = thread_execute;\r
71     s->execute2                   = thread_execute2;\r
72     s->thread_count               = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible. \r
73 \r
74         CASPAR_LOG(info) << "Initialized ffmpeg tbb context.";\r
75 }\r
76 \r
77 void thread_free(AVCodecContext* s)\r
78 {\r
79         if(!s->thread_opaque)\r
80                 return;\r
81 \r
82         s->thread_opaque = nullptr;\r
83         \r
84         CASPAR_LOG(info) << "Released ffmpeg tbb context.";\r
85 }\r
86 \r
87 int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)\r
88 {\r
89         avctx->thread_count = 1;\r
90         // Some codecs don't like to have multiple multithreaded decoding instances. Only enable for those we know work.\r
91         if((codec->id == CODEC_ID_MPEG2VIDEO) && \r
92           (codec->capabilities & CODEC_CAP_SLICE_THREADS) && \r
93           (avctx->thread_type & FF_THREAD_SLICE))\r
94         {\r
95                 thread_init(avctx);\r
96         }       \r
97         // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.\r
98         return avcodec_open(avctx, codec); \r
99 }\r
100 \r
101 int tbb_avcodec_close(AVCodecContext* avctx)\r
102 {\r
103         thread_free(avctx);\r
104         // ff_thread_free will not be executed since thread_opaque == nullptr.\r
105         return avcodec_close(avctx); \r
106 }\r
107 \r
108 }