2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../../StdAfx.h"
24 #include "parallel_yadif.h"
26 #include <common/log.h>
29 #pragma warning (push)
30 #pragma warning (disable : 4244)
34 #include <libavfilter/avfilter.h>
40 #include <tbb/parallel_for.h>
41 #include <tbb/concurrent_queue.h>
43 #include <boost/thread/once.hpp>
50 AVFilterBufferRef *cur;
51 AVFilterBufferRef *next;
52 AVFilterBufferRef *prev;
53 AVFilterBufferRef *out;
54 void (*filter_line)(uint8_t *dst,
55 uint8_t *prev, uint8_t *cur, uint8_t *next,
56 int w, int prefs, int mrefs, int parity, int mode);
57 //const AVPixFmtDescriptor *csp;
60 struct parallel_yadif_context
76 std::vector<arg> args;
79 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;
81 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)
83 parallel_yadif_context::arg arg = {dst, prev, cur, next, w, prefs, mrefs, parity, mode};
84 ctx.args.push_back(arg);
86 if(ctx.args.size() == ctx.size)
88 tbb::parallel_for(tbb::blocked_range<size_t>(0, ctx.args.size()), [=](const tbb::blocked_range<size_t>& r)
90 for(auto n = r.begin(); n != r.end(); ++n)
91 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);
97 namespace caspar { namespace ffmpeg {
99 tbb::concurrent_bounded_queue<decltype(org_yadif_filter_line)> parallel_line_func_pool;
100 std::array<parallel_yadif_context, 18> ctxs;
102 #define RENAME(a) f ## a
105 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) \
107 parallel_yadif_filter_line(ctxs[x], dst, prev, cur, next, w, prefs, mrefs, parity, mode);\
110 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);
112 void (*fs[])(uint8_t *dst, uint8_t *prev, uint8_t *cur, uint8_t *next, int w, int prefs, int mrefs, int parity, int mode) =
113 {f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15, f16, f17};
118 for(int n = 0; n < sizeof(fs)/sizeof(fs[0]); ++n)
119 parallel_line_func_pool.push(fs[n]);
122 void return_parallel_yadif(void* func)
125 parallel_line_func_pool.push(reinterpret_cast<decltype(fs[0])>(func));
128 std::shared_ptr<void> make_parallel_yadif(AVFilterContext* ctx)
130 static boost::once_flag flag = BOOST_ONCE_INIT;
131 boost::call_once(&init_pool, flag);
133 YADIFContext* yadif = (YADIFContext*)ctx->priv;
134 org_yadif_filter_line = yadif->filter_line; // Data race is not a problem.
136 decltype(org_yadif_filter_line) func = nullptr;
137 if(!parallel_line_func_pool.try_pop(func))
138 CASPAR_LOG(warning) << "Not enough scalable-yadif context instances. Running non-scalable";
142 while(index < sizeof(fs)/sizeof(fs[0]) && fs[index] != func)
145 ctxs[index].size = 0;
146 for (int y = 0; y < ctx->inputs[0]->h; y++)
148 if ((y ^ yadif->parity) & 1)
152 yadif->filter_line = func;
155 return std::shared_ptr<void>(func, return_parallel_yadif);