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