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"
27 #include "../../ffmpeg.h"
29 #include <common/assert.h>
30 #include <common/except.h>
32 #include <boost/algorithm/string.hpp>
33 #include <boost/thread.hpp>
34 #include <boost/format.hpp>
35 #include <boost/rational.hpp>
42 #pragma warning (push)
43 #pragma warning (disable : 4244)
47 #include <libavutil/avutil.h>
48 #include <libavutil/imgutils.h>
49 #include <libavutil/opt.h>
50 #include <libavfilter/avfilter.h>
51 #include <libavfilter/avcodec.h>
52 #include <libavfilter/buffersink.h>
53 #include <libavfilter/buffersrc.h>
59 namespace caspar { namespace ffmpeg {
61 struct filter::implementation
63 std::string filtergraph_;
65 std::shared_ptr<AVFilterGraph> video_graph_;
66 AVFilterContext* video_graph_in_;
67 AVFilterContext* video_graph_out_;
72 boost::rational<int> in_time_base,
73 boost::rational<int> in_frame_rate,
74 boost::rational<int> in_sample_aspect_ratio,
75 AVPixelFormat in_pix_fmt,
76 std::vector<AVPixelFormat> out_pix_fmts,
77 const std::string& filtergraph)
78 : filtergraph_(boost::to_lower_copy(filtergraph))
80 if(out_pix_fmts.empty())
96 out_pix_fmts.push_back(AV_PIX_FMT_NONE);
99 avfilter_graph_alloc(),
102 avfilter_graph_free(&p);
105 video_graph_->nb_threads = 0;
106 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
108 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%")
109 % in_width % in_height
111 % in_time_base.numerator() % in_time_base.denominator()
112 % in_sample_aspect_ratio.numerator() % in_sample_aspect_ratio.denominator()
113 % in_frame_rate.numerator() % in_frame_rate.denominator()).str();
115 AVFilterContext* filt_vsrc = nullptr;
116 FF(avfilter_graph_create_filter(
118 avfilter_get_by_name("buffer"),
120 vsrc_options.c_str(),
122 video_graph_.get()));
124 AVFilterContext* filt_vsink = nullptr;
125 FF(avfilter_graph_create_filter(
127 avfilter_get_by_name("buffersink"),
131 video_graph_.get()));
133 #pragma warning (push)
134 #pragma warning (disable : 4245)
136 FF(av_opt_set_int_list(
141 AV_OPT_SEARCH_CHILDREN));
143 #pragma warning (pop)
145 configure_filtergraph(
151 video_graph_in_ = filt_vsrc;
152 video_graph_out_ = filt_vsink;
154 if (!is_logging_disabled_for_thread())
156 << u16(std::string("\n")
157 + avfilter_graph_dump(
162 void configure_filtergraph(
163 AVFilterGraph& graph,
164 const std::string& filtergraph,
165 AVFilterContext& source_ctx,
166 AVFilterContext& sink_ctx)
168 AVFilterInOut* outputs = nullptr;
169 AVFilterInOut* inputs = nullptr;
173 if(!filtergraph.empty())
175 outputs = avfilter_inout_alloc();
176 inputs = avfilter_inout_alloc();
178 CASPAR_VERIFY(outputs && inputs);
180 outputs->name = av_strdup("in");
181 outputs->filter_ctx = &source_ctx;
182 outputs->pad_idx = 0;
183 outputs->next = nullptr;
185 inputs->name = av_strdup("out");
186 inputs->filter_ctx = &sink_ctx;
188 inputs->next = nullptr;
190 FF(avfilter_graph_parse(
206 FF(avfilter_graph_config(
212 //avfilter_inout_free(&outputs);
213 //avfilter_inout_free(&inputs);
218 void push(const std::shared_ptr<AVFrame>& src_av_frame)
220 FF(av_buffersrc_add_frame(
222 src_av_frame.get()));
225 std::shared_ptr<AVFrame> poll()
227 std::shared_ptr<AVFrame> filt_frame(
234 const auto ret = av_buffersink_get_frame(
238 if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
250 boost::rational<int> in_time_base,
251 boost::rational<int> in_frame_rate,
252 boost::rational<int> in_sample_aspect_ratio,
253 AVPixelFormat in_pix_fmt,
254 std::vector<AVPixelFormat> out_pix_fmts,
255 const std::string& filtergraph)
256 : impl_(new implementation(
261 in_sample_aspect_ratio,
265 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}
266 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}
267 void filter::push(const std::shared_ptr<AVFrame>& frame){impl_->push(frame);}
268 std::shared_ptr<AVFrame> filter::poll(){return impl_->poll();}
269 std::wstring filter::filter_str() const{return u16(impl_->filtergraph_);}
270 std::vector<spl::shared_ptr<AVFrame>> filter::poll_all()
272 std::vector<spl::shared_ptr<AVFrame>> frames;
273 for(auto frame = poll(); frame; frame = poll())
274 frames.push_back(spl::make_shared_ptr(frame));