]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/filter/filter.cpp
Refactored to use non-static data member initializers where it makes sense. Mixes...
[casparcg] / modules / ffmpeg / producer / filter / filter.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://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 "filter.h"
25
26 #include "../../ffmpeg_error.h"
27
28 #include <common/assert.h>
29 #include <common/except.h>
30
31 #include <boost/range/iterator_range.hpp>
32 #include <boost/range/adaptors.hpp>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/format.hpp>
36 #include <boost/rational.hpp>
37
38 #include <cstdio>
39 #include <sstream>
40 #include <string>
41
42 #if defined(_MSC_VER)
43 #pragma warning (push)
44 #pragma warning (disable : 4244)
45 #endif
46 extern "C" 
47 {
48         #include <libavutil/avutil.h>
49         #include <libavutil/imgutils.h>
50         #include <libavutil/opt.h>
51         #include <libavfilter/avfilter.h>
52         #include <libavfilter/avcodec.h>
53         #include <libavfilter/avfilter.h>
54         #include <libavfilter/buffersink.h>
55         #include <libavfilter/buffersrc.h>
56 }
57 #if defined(_MSC_VER)
58 #pragma warning (pop)
59 #endif
60
61 namespace caspar { namespace ffmpeg {
62         
63 struct filter::implementation
64 {
65         std::string                                             filtergraph_;
66
67         std::shared_ptr<AVFilterGraph>  video_graph_;   
68     AVFilterContext*                            video_graph_in_;  
69     AVFilterContext*                            video_graph_out_; 
70                 
71         implementation(
72                         int in_width,
73                         int in_height,
74                         boost::rational<int> in_time_base,
75                         boost::rational<int> in_frame_rate,
76                         boost::rational<int> in_sample_aspect_ratio,
77                         AVPixelFormat in_pix_fmt,
78                         std::vector<AVPixelFormat> out_pix_fmts,
79                         const std::string& filtergraph) 
80                 : filtergraph_(boost::to_lower_copy(filtergraph))
81         {
82                 if(out_pix_fmts.empty())
83                 {
84                         out_pix_fmts = {
85                                 AV_PIX_FMT_YUVA420P,
86                                 AV_PIX_FMT_YUV444P,
87                                 AV_PIX_FMT_YUV422P,
88                                 AV_PIX_FMT_YUV420P,
89                                 AV_PIX_FMT_YUV411P,
90                                 AV_PIX_FMT_BGRA,
91                                 AV_PIX_FMT_ARGB,
92                                 AV_PIX_FMT_RGBA,
93                                 AV_PIX_FMT_ABGR,
94                                 AV_PIX_FMT_GRAY8
95                         };
96                 }
97
98                 out_pix_fmts.push_back(AV_PIX_FMT_NONE);
99
100                 video_graph_.reset(
101                         avfilter_graph_alloc(), 
102                         [](AVFilterGraph* p)
103                         {
104                                 avfilter_graph_free(&p);
105                         });
106                 
107                 video_graph_->nb_threads  = boost::thread::hardware_concurrency();
108                 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
109                                 
110                 const auto vsrc_options = (boost::format("video_size=%1%x%2%:pix_fmt=%3%:time_base=%4%/%5%:pixel_aspect=%6%/%7%:frame_rate=%8%/%9%")
111                         % in_width % in_height
112                         % in_pix_fmt
113                         % in_time_base.numerator() % in_time_base.denominator()
114                         % in_sample_aspect_ratio.numerator() % in_sample_aspect_ratio.denominator()
115                         % in_frame_rate.numerator() % in_frame_rate.denominator()).str();
116                                         
117                 AVFilterContext* filt_vsrc = nullptr;                   
118                 FF(avfilter_graph_create_filter(
119                         &filt_vsrc,
120                         avfilter_get_by_name("buffer"), 
121                         "filter_buffer",
122                         vsrc_options.c_str(), 
123                         nullptr, 
124                         video_graph_.get()));
125                                 
126                 AVFilterContext* filt_vsink = nullptr;
127                 FF(avfilter_graph_create_filter(
128                         &filt_vsink,
129                         avfilter_get_by_name("buffersink"), 
130                         "filter_buffersink",
131                         nullptr, 
132                         nullptr, 
133                         video_graph_.get()));
134                 
135 #pragma warning (push)
136 #pragma warning (disable : 4245)
137
138                 FF(av_opt_set_int_list(
139                         filt_vsink, 
140                         "pix_fmts", 
141                         out_pix_fmts.data(), 
142                         -1,
143                         AV_OPT_SEARCH_CHILDREN));
144
145 #pragma warning (pop)
146                         
147                 configure_filtergraph(
148                         *video_graph_, 
149                         filtergraph_,
150                         *filt_vsrc,
151                         *filt_vsink);
152
153                 video_graph_in_  = filt_vsrc;
154                 video_graph_out_ = filt_vsink;
155                 
156                 CASPAR_LOG(info)
157                         <<      u16(std::string("\n") 
158                                 + avfilter_graph_dump(
159                                                 video_graph_.get(), 
160                                                 nullptr));
161         }
162         
163         void configure_filtergraph(
164                 AVFilterGraph& graph, 
165                 const std::string& filtergraph, 
166                 AVFilterContext& source_ctx, 
167                 AVFilterContext& sink_ctx)
168         {
169                 AVFilterInOut* outputs = nullptr;
170                 AVFilterInOut* inputs = nullptr;
171
172                 try
173                 {
174                         if(!filtergraph.empty()) 
175                         {
176                                 outputs = avfilter_inout_alloc();
177                                 inputs  = avfilter_inout_alloc();
178
179                                 CASPAR_VERIFY(outputs && inputs);
180
181                                 outputs->name       = av_strdup("in");
182                                 outputs->filter_ctx = &source_ctx;
183                                 outputs->pad_idx    = 0;
184                                 outputs->next       = nullptr;
185
186                                 inputs->name        = av_strdup("out");
187                                 inputs->filter_ctx  = &sink_ctx;
188                                 inputs->pad_idx     = 0;
189                                 inputs->next        = nullptr;
190
191                                 FF(avfilter_graph_parse(
192                                         &graph, 
193                                         filtergraph.c_str(), 
194                                         &inputs, 
195                                         &outputs, 
196                                         nullptr));
197                         } 
198                         else 
199                         {
200                                 FF(avfilter_link(
201                                         &source_ctx, 
202                                         0, 
203                                         &sink_ctx, 
204                                         0));
205                         }
206
207                         FF(avfilter_graph_config(
208                                 &graph, 
209                                 nullptr));
210                 }
211                 catch(...)
212                 {
213                         avfilter_inout_free(&outputs);
214                         avfilter_inout_free(&inputs);
215                         throw;
216                 }
217         }
218
219         void push(const std::shared_ptr<AVFrame>& src_av_frame)
220         {               
221                 FF(av_buffersrc_add_frame(
222                         video_graph_in_, 
223                         src_av_frame.get()));
224         }
225
226         std::shared_ptr<AVFrame> poll()
227         {
228                 std::shared_ptr<AVFrame> filt_frame(
229                         av_frame_alloc(), 
230                         [](AVFrame* p)
231                         {
232                                 av_frame_free(&p);
233                         });
234                 
235                 const auto ret = av_buffersink_get_frame(
236                         video_graph_out_, 
237                         filt_frame.get());
238                                 
239                 if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
240                         return nullptr;
241                                         
242                 FF_RET(ret, "poll");
243
244                 return filt_frame;
245         }
246 };
247
248 filter::filter(
249                 int in_width,
250                 int in_height,
251                 boost::rational<int> in_time_base,
252                 boost::rational<int> in_frame_rate,
253                 boost::rational<int> in_sample_aspect_ratio,
254                 AVPixelFormat in_pix_fmt,
255                 std::vector<AVPixelFormat> out_pix_fmts,
256                 const std::string& filtergraph) 
257                 : impl_(new implementation(
258                         in_width,
259                         in_height,
260                         in_time_base,
261                         in_frame_rate,
262                         in_sample_aspect_ratio,
263                         in_pix_fmt,
264                         out_pix_fmts,
265                         filtergraph)){}
266 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}
267 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}
268 void filter::push(const std::shared_ptr<AVFrame>& frame){impl_->push(frame);}
269 std::shared_ptr<AVFrame> filter::poll(){return impl_->poll();}
270 std::wstring filter::filter_str() const{return u16(impl_->filtergraph_);}
271 std::vector<spl::shared_ptr<AVFrame>> filter::poll_all()
272 {       
273         std::vector<spl::shared_ptr<AVFrame>> frames;
274         for(auto frame = poll(); frame; frame = poll())
275                 frames.push_back(spl::make_shared_ptr(frame));
276         return frames;
277 }
278
279 }}