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"
28 #include "../util/util.h"
30 #include <common/assert.h>
31 #include <common/except.h>
33 #include <boost/algorithm/string.hpp>
34 #include <boost/thread.hpp>
35 #include <boost/format.hpp>
36 #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/buffersink.h>
54 #include <libavfilter/buffersrc.h>
60 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_;
69 std::queue<std::shared_ptr<AVFrame>> fast_path_;
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,
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);
110 video_graph_->nb_threads = 0;
111 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
115 video_graph_->nb_threads = 1;
118 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%")
119 % in_width % in_height
121 % in_time_base.numerator() % in_time_base.denominator()
122 % in_sample_aspect_ratio.numerator() % in_sample_aspect_ratio.denominator()
123 % in_frame_rate.numerator() % in_frame_rate.denominator()).str();
125 AVFilterContext* filt_vsrc = nullptr;
126 FF(avfilter_graph_create_filter(
128 avfilter_get_by_name("buffer"),
130 vsrc_options.c_str(),
132 video_graph_.get()));
134 AVFilterContext* filt_vsink = nullptr;
135 FF(avfilter_graph_create_filter(
137 avfilter_get_by_name("buffersink"),
141 video_graph_.get()));
143 #pragma warning (push)
144 #pragma warning (disable : 4245)
146 FF(av_opt_set_int_list(
151 AV_OPT_SEARCH_CHILDREN));
153 #pragma warning (pop)
155 configure_filtergraph(
161 video_graph_in_ = filt_vsrc;
162 video_graph_out_ = filt_vsink;
164 if (is_logging_quiet_for_thread())
166 << u16(std::string("\n")
167 + avfilter_graph_dump(
172 << u16(std::string("\n")
173 + avfilter_graph_dump(
178 void configure_filtergraph(
179 AVFilterGraph& graph,
180 const std::string& filtergraph,
181 AVFilterContext& source_ctx,
182 AVFilterContext& sink_ctx)
184 if (!filtergraph.empty())
186 auto outputs = avfilter_inout_alloc();
187 auto inputs = avfilter_inout_alloc();
189 CASPAR_VERIFY(outputs && inputs);
191 outputs->name = av_strdup("in");
192 outputs->filter_ctx = &source_ctx;
193 outputs->pad_idx = 0;
194 outputs->next = nullptr;
196 inputs->name = av_strdup("out");
197 inputs->filter_ctx = &sink_ctx;
199 inputs->next = nullptr;
201 FF(avfilter_graph_parse(
217 FF(avfilter_graph_config(&graph, nullptr));
220 bool fast_path() const
222 return filtergraph_.empty();
225 void push(const std::shared_ptr<AVFrame>& src_av_frame)
228 fast_path_.push(src_av_frame);
230 FF(av_buffersrc_add_frame(
232 src_av_frame.get()));
235 std::shared_ptr<AVFrame> poll()
239 if (fast_path_.empty())
242 auto result = fast_path_.front();
247 auto filt_frame = create_frame();
249 const auto ret = av_buffersink_get_frame(
253 if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
265 boost::rational<int> in_time_base,
266 boost::rational<int> in_frame_rate,
267 boost::rational<int> in_sample_aspect_ratio,
268 AVPixelFormat in_pix_fmt,
269 std::vector<AVPixelFormat> out_pix_fmts,
270 const std::string& filtergraph,
272 : impl_(new implementation(
277 in_sample_aspect_ratio,
282 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}
283 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}
284 void filter::push(const std::shared_ptr<AVFrame>& frame){impl_->push(frame);}
285 std::shared_ptr<AVFrame> filter::poll(){return impl_->poll();}
286 std::wstring filter::filter_str() const{return u16(impl_->filtergraph_);}
287 std::vector<spl::shared_ptr<AVFrame>> filter::poll_all()
289 std::vector<spl::shared_ptr<AVFrame>> frames;
290 for(auto frame = poll(); frame; frame = poll())
291 frames.push_back(spl::make_shared_ptr(frame));