]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/parallel_yadif.cpp
Updated master to latest ffmpeg version.
[casparcg] / modules / ffmpeg / producer / filter / parallel_yadif.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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 "parallel_yadif.h"\r
25 \r
26 #include <common/log/log.h>\r
27 \r
28 #if defined(_MSC_VER)\r
29 #pragma warning (push)\r
30 #pragma warning (disable : 4244)\r
31 #endif\r
32 extern "C" \r
33 {\r
34         #include <libavfilter/avfilter.h>\r
35 }\r
36 #if defined(_MSC_VER)\r
37 #pragma warning (pop)\r
38 #endif\r
39 \r
40 #include <tbb/parallel_for.h>\r
41 #include <tbb/concurrent_queue.h>\r
42 \r
43 #include <boost/thread/once.hpp>\r
44 \r
45 typedef struct {\r
46     int mode;\r
47     int parity;\r
48     int frame_pending;\r
49     int auto_enable;\r
50     AVFilterBufferRef *cur;\r
51     AVFilterBufferRef *next;\r
52     AVFilterBufferRef *prev;\r
53     AVFilterBufferRef *out;\r
54     void (*filter_line)(uint8_t *dst,\r
55                         uint8_t *prev, uint8_t *cur, uint8_t *next,\r
56                         int w, int prefs, int mrefs, int parity, int mode);\r
57     //const AVPixFmtDescriptor *csp;\r
58 } YADIFContext;\r
59 \r
60 struct parallel_yadif_context\r
61 {\r
62         struct arg\r
63         {\r
64                 uint8_t *dst;\r
65                 uint8_t *prev;\r
66                 uint8_t *cur; \r
67                 uint8_t *next; \r
68                 int w; \r
69                 int prefs; \r
70                 int mrefs;\r
71                 int parity;\r
72                 int mode;\r
73         };\r
74 \r
75         arg     args[1080];\r
76         int     index;\r
77         int last_index;\r
78 \r
79         parallel_yadif_context() : index(0){}\r
80 };\r
81 \r
82 void (*org_yadif_filter_line)(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) = 0;\r
83 \r
84 void parallel_yadif_filter_line(parallel_yadif_context& ctx, uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode)\r
85 {\r
86         parallel_yadif_context::arg arg = {dst, prev, cur, next, w, prefs, mrefs, parity, mode};\r
87         ctx.args[ctx.index++] = arg;\r
88         \r
89         if(ctx.index == ctx.last_index)\r
90         {               \r
91                 tbb::parallel_for(tbb::blocked_range<size_t>(0, ctx.index), [=](const tbb::blocked_range<size_t>& r)\r
92                 {\r
93                         for(auto n = r.begin(); n != r.end(); ++n)\r
94                                 org_yadif_filter_line(ctx.args[n].dst, ctx.args[n].prev, ctx.args[n].cur, ctx.args[n].next, ctx.args[n].w, ctx.args[n].prefs, ctx.args[n].mrefs, ctx.args[n].parity, ctx.args[n].mode);\r
95                 });\r
96                 ctx.index = 0;\r
97         }\r
98 }\r
99 \r
100 namespace caspar { namespace ffmpeg {\r
101         \r
102 tbb::concurrent_bounded_queue<decltype(org_yadif_filter_line)> parallel_line_func_pool;\r
103 std::array<parallel_yadif_context, 18> ctxs;\r
104 \r
105 #define RENAME(a) f ## a\r
106 \r
107 #define ff(x) \\r
108 void RENAME(x)(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) \\r
109 {\\r
110         parallel_yadif_filter_line(ctxs[x], dst, prev, cur, next, w, prefs, mrefs, parity, mode);\\r
111 }\r
112 \r
113 ff(0); ff(1); ff(2); ff(3); ff(4); ff(5); ff(6); ff(7); ff(8); ff(9); ff(10); ff(11); ff(12); ff(13); ff(14); ff(15); ff(16); ff(17);\r
114 \r
115 void (*fs[])(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) = \r
116 {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17};\r
117 \r
118 \r
119 void init_pool()\r
120 {\r
121         for(int n = 0; n < sizeof(fs)/sizeof(fs[0]); ++n)\r
122                 parallel_line_func_pool.push(fs[n]);\r
123 }\r
124 \r
125 void return_parallel_yadif(void* func)\r
126 {\r
127         if(func != nullptr)\r
128                 parallel_line_func_pool.push(reinterpret_cast<decltype(fs[0])>(func));\r
129 }\r
130 \r
131 std::shared_ptr<void> make_parallel_yadif(AVFilterContext* ctx)\r
132 {\r
133         static boost::once_flag flag = BOOST_ONCE_INIT;\r
134         boost::call_once(&init_pool, flag);\r
135 \r
136         YADIFContext* yadif = (YADIFContext*)ctx->priv;\r
137         org_yadif_filter_line = yadif->filter_line; // Data race is not a problem.\r
138         \r
139         decltype(org_yadif_filter_line) func = nullptr;\r
140         if(!parallel_line_func_pool.try_pop(func))      \r
141                 CASPAR_LOG(warning) << "Not enough scalable-yadif context instances. Running non-scalable";\r
142         else\r
143         {\r
144                 int index = 0;\r
145                 while(index < sizeof(fs)/sizeof(fs[0]) && fs[index] != func)\r
146                         ++index;\r
147 \r
148                 ctxs[index].last_index = 0;\r
149                 for (int y = 0; y < ctx->inputs[0]->h; y++)\r
150                 {\r
151             if ((y ^ yadif->parity) & 1)\r
152                                 ++ctxs[index].last_index;\r
153                 }\r
154 \r
155                 yadif->filter_line = func;\r
156         }\r
157 \r
158         return std::shared_ptr<void>(func, return_parallel_yadif);\r
159 }\r
160 \r
161 }}