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/avcodec.h>
54 #include <libavfilter/buffersink.h>
55 #include <libavfilter/buffersrc.h>
61 namespace caspar { namespace ffmpeg {
62 struct filter::implementation
64 std::string filtergraph_;
66 std::shared_ptr<AVFilterGraph> video_graph_;
67 AVFilterContext* video_graph_in_;
68 AVFilterContext* video_graph_out_;
70 std::queue<std::shared_ptr<AVFrame>> fast_path_;
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,
82 : filtergraph_(boost::to_lower_copy(filtergraph))
84 if(out_pix_fmts.empty())
100 out_pix_fmts.push_back(AV_PIX_FMT_NONE);
103 avfilter_graph_alloc(),
106 avfilter_graph_free(&p);
111 video_graph_->nb_threads = 0;
112 video_graph_->thread_type = AVFILTER_THREAD_SLICE;
116 video_graph_->nb_threads = 1;
119 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%")
120 % in_width % in_height
122 % in_time_base.numerator() % in_time_base.denominator()
123 % in_sample_aspect_ratio.numerator() % in_sample_aspect_ratio.denominator()
124 % in_frame_rate.numerator() % in_frame_rate.denominator()).str();
126 AVFilterContext* filt_vsrc = nullptr;
127 FF(avfilter_graph_create_filter(
129 avfilter_get_by_name("buffer"),
131 vsrc_options.c_str(),
133 video_graph_.get()));
135 AVFilterContext* filt_vsink = nullptr;
136 FF(avfilter_graph_create_filter(
138 avfilter_get_by_name("buffersink"),
142 video_graph_.get()));
144 #pragma warning (push)
145 #pragma warning (disable : 4245)
147 FF(av_opt_set_int_list(
152 AV_OPT_SEARCH_CHILDREN));
154 #pragma warning (pop)
156 configure_filtergraph(
162 video_graph_in_ = filt_vsrc;
163 video_graph_out_ = filt_vsink;
165 if (is_logging_quiet_for_thread())
167 << u16(std::string("\n")
168 + avfilter_graph_dump(
173 << u16(std::string("\n")
174 + avfilter_graph_dump(
179 void configure_filtergraph(
180 AVFilterGraph& graph,
181 const std::string& filtergraph,
182 AVFilterContext& source_ctx,
183 AVFilterContext& sink_ctx)
185 if (!filtergraph.empty())
187 auto outputs = avfilter_inout_alloc();
188 auto inputs = avfilter_inout_alloc();
190 CASPAR_VERIFY(outputs && inputs);
192 outputs->name = av_strdup("in");
193 outputs->filter_ctx = &source_ctx;
194 outputs->pad_idx = 0;
195 outputs->next = nullptr;
197 inputs->name = av_strdup("out");
198 inputs->filter_ctx = &sink_ctx;
200 inputs->next = nullptr;
202 FF(avfilter_graph_parse(
218 FF(avfilter_graph_config(&graph, nullptr));
221 bool fast_path() const
223 return filtergraph_.empty();
226 void push(const std::shared_ptr<AVFrame>& src_av_frame)
229 fast_path_.push(src_av_frame);
231 FF(av_buffersrc_add_frame(
233 src_av_frame.get()));
236 std::shared_ptr<AVFrame> poll()
240 if (fast_path_.empty())
243 auto result = fast_path_.front();
248 auto filt_frame = create_frame();
250 const auto ret = av_buffersink_get_frame(
254 if(ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
266 boost::rational<int> in_time_base,
267 boost::rational<int> in_frame_rate,
268 boost::rational<int> in_sample_aspect_ratio,
269 AVPixelFormat in_pix_fmt,
270 std::vector<AVPixelFormat> out_pix_fmts,
271 const std::string& filtergraph,
273 : impl_(new implementation(
278 in_sample_aspect_ratio,
283 filter::filter(filter&& other) : impl_(std::move(other.impl_)){}
284 filter& filter::operator=(filter&& other){impl_ = std::move(other.impl_); return *this;}
285 void filter::push(const std::shared_ptr<AVFrame>& frame){impl_->push(frame);}
286 std::shared_ptr<AVFrame> filter::poll(){return impl_->poll();}
287 std::wstring filter::filter_str() const{return u16(impl_->filtergraph_);}
288 std::vector<spl::shared_ptr<AVFrame>> filter::poll_all()
290 std::vector<spl::shared_ptr<AVFrame>> frames;
291 for(auto frame = poll(); frame; frame = poll())
292 frames.push_back(spl::make_shared_ptr(frame));