2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../StdAfx.h"
\r
24 #include "parallel_yadif.h"
\r
26 #include <common/log/log.h>
\r
28 #if defined(_MSC_VER)
\r
29 #pragma warning (push)
\r
30 #pragma warning (disable : 4244)
\r
34 #include <libavfilter/avfilter.h>
\r
36 #if defined(_MSC_VER)
\r
37 #pragma warning (pop)
\r
40 #include <tbb/parallel_for.h>
\r
41 #include <tbb/concurrent_queue.h>
\r
43 #include <boost/thread/once.hpp>
\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
60 struct parallel_yadif_context
\r
79 parallel_yadif_context() : index(0){}
\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
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
86 parallel_yadif_context::arg arg = {dst, prev, cur, next, w, prefs, mrefs, parity, mode};
\r
87 ctx.args[ctx.index++] = arg;
\r
89 if(ctx.index == ctx.last_index)
\r
91 tbb::parallel_for(tbb::blocked_range<size_t>(0, ctx.index), [=](const tbb::blocked_range<size_t>& r)
\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
100 namespace caspar { namespace ffmpeg {
\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
105 #define RENAME(a) f ## a
\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
110 parallel_yadif_filter_line(ctxs[x], dst, prev, cur, next, w, prefs, mrefs, parity, mode);\
\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
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
121 for(int n = 0; n < sizeof(fs)/sizeof(fs[0]); ++n)
\r
122 parallel_line_func_pool.push(fs[n]);
\r
125 void return_parallel_yadif(void* func)
\r
127 if(func != nullptr)
\r
128 parallel_line_func_pool.push(reinterpret_cast<decltype(fs[0])>(func));
\r
131 std::shared_ptr<void> make_parallel_yadif(AVFilterContext* ctx)
\r
133 static boost::once_flag flag = BOOST_ONCE_INIT;
\r
134 boost::call_once(&init_pool, flag);
\r
136 YADIFContext* yadif = (YADIFContext*)ctx->priv;
\r
137 org_yadif_filter_line = yadif->filter_line; // Data race is not a problem.
\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
145 while(index < sizeof(fs)/sizeof(fs[0]) && fs[index] != func)
\r
148 ctxs[index].last_index = 0;
\r
149 for (int y = 0; y < ctx->inputs[0]->h; y++)
\r
151 if ((y ^ yadif->parity) & 1)
\r
152 ++ctxs[index].last_index;
\r
155 yadif->filter_line = func;
\r
158 return std::shared_ptr<void>(func, return_parallel_yadif);
\r