]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/tbb_avcodec.cpp
2.1.0: Some mayor refactoring.
[casparcg] / modules / ffmpeg / producer / tbb_avcodec.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "tbb_avcodec.h"\r
25 \r
26 #include <common/log.h>\r
27 #include <common/env.h>\r
28 \r
29 #include <tbb/atomic.h>\r
30 #include <tbb/parallel_for.h>\r
31 #include <tbb/tbb_thread.h>\r
32 \r
33 #if defined(_MSC_VER)\r
34 #pragma warning (push)\r
35 #pragma warning (disable : 4244)\r
36 #endif\r
37 extern "C" \r
38 {\r
39         #define __STDC_CONSTANT_MACROS\r
40         #define __STDC_LIMIT_MACROS\r
41         #include <libavformat/avformat.h>\r
42 }\r
43 #if defined(_MSC_VER)\r
44 #pragma warning (pop)\r
45 #endif\r
46 \r
47 namespace caspar {\r
48                 \r
49 int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)\r
50 {\r
51         tbb::parallel_for(0, count, 1, [&](int i)\r
52         {\r
53         int r = func(s, (char*)arg + i*size);\r
54         if(ret) \r
55                         ret[i] = r;\r
56     });\r
57 \r
58         return 0;\r
59 }\r
60 \r
61 int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)\r
62 {       \r
63         tbb::atomic<int> counter;   \r
64     counter = 0;   \r
65 \r
66         BOOST_VERIFY(tbb::tbb_thread::hardware_concurrency() < 16);\r
67         // Note: this will probably only work when tbb::task_scheduler_init::num_threads() < 16.\r
68     tbb::parallel_for(tbb::blocked_range<int>(0, count, 2), [&](const tbb::blocked_range<int> &r)    \r
69     {   \r
70         int threadnr = counter++;   \r
71         for(int jobnr = r.begin(); jobnr != r.end(); ++jobnr)\r
72         {   \r
73             int r = func(s, arg, jobnr, threadnr);   \r
74             if (ret)   \r
75                 ret[jobnr] = r;   \r
76         }\r
77         --counter;\r
78     });   \r
79 \r
80     return 0;  \r
81 }\r
82 \r
83 void thread_init(AVCodecContext* s)\r
84 {\r
85         static const size_t MAX_THREADS = 16; // See mpegvideo.h\r
86         static int dummy_opaque;\r
87 \r
88     s->active_thread_type = FF_THREAD_SLICE;\r
89         s->thread_opaque          = &dummy_opaque; \r
90     s->execute                    = thread_execute;\r
91     s->execute2                   = thread_execute2;\r
92     s->thread_count               = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible. \r
93 \r
94         CASPAR_LOG(info) << "Initialized ffmpeg tbb context.";\r
95 }\r
96 \r
97 void thread_free(AVCodecContext* s)\r
98 {\r
99         if(!s->thread_opaque)\r
100                 return;\r
101 \r
102         s->thread_opaque = nullptr;\r
103         \r
104         CASPAR_LOG(info) << "Released ffmpeg tbb context.";\r
105 }\r
106 \r
107 int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)\r
108 {\r
109         CodecID supported_codecs[] = {CODEC_ID_MPEG2VIDEO, CODEC_ID_PRORES, CODEC_ID_FFV1};\r
110 \r
111         avctx->thread_count = 1;\r
112         // Some codecs don't like to have multiple multithreaded decoding instances. Only enable for those we know work.\r
113         if(std::find(std::begin(supported_codecs), std::end(supported_codecs), codec->id) != std::end(supported_codecs) && \r
114           (codec->capabilities & CODEC_CAP_SLICE_THREADS) && \r
115           (avctx->thread_type & FF_THREAD_SLICE)) \r
116         {\r
117                 thread_init(avctx);\r
118         }       \r
119         // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.\r
120         return avcodec_open(avctx, codec); \r
121 }\r
122 \r
123 int tbb_avcodec_close(AVCodecContext* avctx)\r
124 {\r
125         thread_free(avctx);\r
126         // ff_thread_free will not be executed since thread_opaque == nullptr.\r
127         return avcodec_close(avctx); \r
128 }\r
129 \r
130 }