]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/tbb_avcodec.cpp
2.1.0: Don't allow experimental codecs!
[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/assert.h>\r
27 #include <common/except.h>\r
28 #include <common/log.h>\r
29 #include <common/env.h>\r
30 \r
31 #include <tbb/atomic.h>\r
32 #include <tbb/parallel_for.h>\r
33 #include <tbb/tbb_thread.h>\r
34 \r
35 #include <boost/foreach.hpp>\r
36 \r
37 #if defined(_MSC_VER)\r
38 #pragma warning (push)\r
39 #pragma warning (disable : 4244)\r
40 #endif\r
41 extern "C" \r
42 {\r
43         #define __STDC_CONSTANT_MACROS\r
44         #define __STDC_LIMIT_MACROS\r
45         #include <libavformat/avformat.h>\r
46 }\r
47 #if defined(_MSC_VER)\r
48 #pragma warning (pop)\r
49 #endif\r
50 \r
51 namespace caspar {\r
52                 \r
53 static const int MAX_THREADS = 16; // See mpegvideo.h\r
54 \r
55 int thread_execute(AVCodecContext* s, int (*func)(AVCodecContext *c2, void *arg2), void* arg, int* ret, int count, int size)\r
56 {\r
57         tbb::parallel_for(0, count, 1, [&](int i)\r
58         {\r
59         int r = func(s, (char*)arg + i*size);\r
60         if(ret) \r
61                         ret[i] = r;\r
62     });\r
63 \r
64         return 0;\r
65 }\r
66 \r
67 int thread_execute2(AVCodecContext* s, int (*func)(AVCodecContext* c2, void* arg2, int, int), void* arg, int* ret, int count)\r
68 {          \r
69         // TODO: Micro-optimize...\r
70 \r
71         std::array<std::vector<int>, 16> jobs;\r
72         \r
73         for(int n = 0; n < count; ++n)  \r
74                 jobs[(n*MAX_THREADS) / count].push_back(n);     \r
75         \r
76         tbb::parallel_for(0, MAX_THREADS, [&](int n)    \r
77     {   \r
78                 BOOST_FOREACH(auto k, jobs[n])\r
79                 {\r
80                         int r = func(s, arg, k, n);\r
81                         if(ret) \r
82                                 ret[k]= r;\r
83                 }\r
84     });   \r
85 \r
86         return 0; \r
87 }\r
88 \r
89 void thread_init(AVCodecContext* s)\r
90 {\r
91         static int dummy_opaque;\r
92 \r
93     s->active_thread_type = FF_THREAD_SLICE;\r
94         s->thread_opaque          = &dummy_opaque; \r
95     s->execute                    = thread_execute;\r
96     s->execute2                   = thread_execute2;\r
97     s->thread_count               = MAX_THREADS; // We are using a task-scheduler, so use as many "threads/tasks" as possible. \r
98 }\r
99 \r
100 void thread_free(AVCodecContext* s)\r
101 {\r
102         if(!s->thread_opaque)\r
103                 return;\r
104 \r
105         s->thread_opaque = nullptr;\r
106 }\r
107 \r
108 int tbb_avcodec_open(AVCodecContext* avctx, AVCodec* codec)\r
109 {\r
110         if(codec->capabilities & CODEC_CAP_EXPERIMENTAL)\r
111                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Experimental codecs are not supported."));\r
112 \r
113         avctx->thread_count = 1;\r
114 \r
115         if(codec->capabilities & CODEC_CAP_SLICE_THREADS)       \r
116                 thread_init(avctx);\r
117         \r
118         // ff_thread_init will not be executed since thread_opaque != nullptr || thread_count == 1.\r
119         return avcodec_open(avctx, codec); \r
120 }\r
121 \r
122 int tbb_avcodec_close(AVCodecContext* avctx)\r
123 {\r
124         thread_free(avctx);\r
125         // ff_thread_free will not be executed since thread_opaque == nullptr.\r
126         return avcodec_close(avctx); \r
127 }\r
128 \r
129 }