2 * Copyright 2013 Sveriges Television AB http://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"
26 #include "../../ffmpeg_error.h"
28 #include <common/assert.h>
29 #include <common/except.h>
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>
44 #pragma warning (push)
45 #pragma warning (disable : 4244)
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>
62 namespace caspar { namespace ffmpeg {
64 struct filter::implementation
66 std::string filtergraph_;
68 std::shared_ptr<AVFilterGraph> video_graph_;
69 AVFilterContext* video_graph_in_;
70 AVFilterContext* video_graph_out_;
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))
83 if(out_pix_fmts.empty())
99 out_pix_fmts.push_back(AV_PIX_FMT_NONE);
102 avfilter_graph_alloc(),
105 avfilter_graph_free(&p);
108 video_graph_->nb_threads = boost::thread::hardware_concurrency();
109 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
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
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();
118 AVFilterContext* filt_vsrc = nullptr;
119 FF(avfilter_graph_create_filter(
121 avfilter_get_by_name("buffer"),
123 vsrc_options.c_str(),
125 video_graph_.get()));
127 AVFilterContext* filt_vsink = nullptr;
128 FF(avfilter_graph_create_filter(
130 avfilter_get_by_name("buffersink"),
134 video_graph_.get()));
136 #pragma warning (push)
137 #pragma warning (disable : 4245)
139 FF(av_opt_set_int_list(
144 AV_OPT_SEARCH_CHILDREN));
146 #pragma warning (pop)
148 configure_filtergraph(
154 video_graph_in_ = filt_vsrc;
155 video_graph_out_ = filt_vsink;
158 << u16(std::string("\n")
159 + avfilter_graph_dump(
164 void configure_filtergraph(
165 AVFilterGraph& graph,
166 const std::string& filtergraph,
167 AVFilterContext& source_ctx,
168 AVFilterContext& sink_ctx)
170 AVFilterInOut* outputs = nullptr;
171 AVFilterInOut* inputs = nullptr;
175 if(!filtergraph.empty())
177 outputs = avfilter_inout_alloc();
178 inputs = avfilter_inout_alloc();
180 CASPAR_VERIFY(outputs && inputs);
182 outputs->name = av_strdup("in");
183 outputs->filter_ctx = &source_ctx;
184 outputs->pad_idx = 0;
185 outputs->next = nullptr;
187 inputs->name = av_strdup("out");
188 inputs->filter_ctx = &sink_ctx;
190 inputs->next = nullptr;
192 FF(avfilter_graph_parse(
208 FF(avfilter_graph_config(
214 avfilter_inout_free(&outputs);
215 avfilter_inout_free(&inputs);
220 void push(const std::shared_ptr<AVFrame>& src_av_frame)
222 FF(av_buffersrc_add_frame(
224 src_av_frame.get()));
227 std::shared_ptr<AVFrame> poll()
229 std::shared_ptr<AVFrame> filt_frame(
236 const auto ret = av_buffersink_get_frame(
240 if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
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(
263 in_sample_aspect_ratio,
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()
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));