]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/parallel_yadif.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / ffmpeg / producer / filter / parallel_yadif.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../../StdAfx.h"
23
24 #include "parallel_yadif.h"
25
26 #include <common/log.h>
27
28 #if defined(_MSC_VER)
29 #pragma warning (push)
30 #pragma warning (disable : 4244)
31 #endif
32 extern "C" 
33 {
34         #include <libavfilter/avfilter.h>
35 }
36 #if defined(_MSC_VER)
37 #pragma warning (pop)
38 #endif
39
40 #include <tbb/parallel_for.h>
41 #include <tbb/concurrent_queue.h>
42
43 #include <boost/thread/once.hpp>
44
45 typedef struct {
46     int mode;
47     int parity;
48     int frame_pending;
49     int auto_enable;
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;
58 } YADIFContext;
59
60 struct parallel_yadif_context
61 {
62         struct arg
63         {
64                 uint8_t *dst;
65                 uint8_t *prev;
66                 uint8_t *cur; 
67                 uint8_t *next; 
68                 int w; 
69                 int prefs; 
70                 int mrefs;
71                 int parity;
72                 int mode;
73         };
74
75         int                              size;
76         std::vector<arg> args;
77 };
78
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;
80
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)
82 {
83         parallel_yadif_context::arg arg = {dst, prev, cur, next, w, prefs, mrefs, parity, mode};
84         ctx.args.push_back(arg);
85         
86         if(ctx.args.size() == ctx.size)
87         {               
88                 tbb::parallel_for(tbb::blocked_range<size_t>(0, ctx.args.size()), [=](const tbb::blocked_range<size_t>& r)
89                 {
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);
92                 });
93                 ctx.args.clear();
94         }
95 }
96
97 namespace caspar { namespace ffmpeg {
98         
99 tbb::concurrent_bounded_queue<decltype(org_yadif_filter_line)> parallel_line_func_pool;
100 std::array<parallel_yadif_context, 18> ctxs;
101
102 #define RENAME(a) f ## a
103
104 #define ff(x) \
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) \
106 {\
107         parallel_yadif_filter_line(ctxs[x], dst, prev, cur, next, w, prefs, mrefs, parity, mode);\
108 }
109
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);
111
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};
114
115
116 void init_pool()
117 {
118         for(int n = 0; n < sizeof(fs)/sizeof(fs[0]); ++n)
119                 parallel_line_func_pool.push(fs[n]);
120 }
121
122 void return_parallel_yadif(void* func)
123 {
124         if(func != nullptr)
125                 parallel_line_func_pool.push(reinterpret_cast<decltype(fs[0])>(func));
126 }
127
128 std::shared_ptr<void> make_parallel_yadif(AVFilterContext* ctx)
129 {
130         static boost::once_flag flag = BOOST_ONCE_INIT;
131         boost::call_once(&init_pool, flag);
132
133         YADIFContext* yadif = (YADIFContext*)ctx->priv;
134         org_yadif_filter_line = yadif->filter_line; // Data race is not a problem.
135         
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";
139         else
140         {
141                 int index = 0;
142                 while(index < sizeof(fs)/sizeof(fs[0]) && fs[index] != func)
143                         ++index;
144
145                 ctxs[index].size = 0;
146                 for (int y = 0; y < ctx->inputs[0]->h; y++)
147                 {
148             if ((y ^ yadif->parity) & 1)
149                                 ++ctxs[index].size;
150                 }
151
152                 yadif->filter_line = func;
153         }
154
155         return std::shared_ptr<void>(func, return_parallel_yadif);
156 }
157
158 }}