2 * Copyright (c) 2011 Sveriges Television AB <info@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"
24 #include "ffmpeg_producer.h"
26 #include "../ffmpeg_error.h"
28 #include "muxer/frame_muxer.h"
29 #include "input/input.h"
30 #include "util/util.h"
31 #include "audio/audio_decoder.h"
32 #include "video/video_decoder.h"
34 #include <common/env.h>
35 #include <common/log.h>
36 #include <common/param.h>
37 #include <common/diagnostics/graph.h>
38 #include <common/future.h>
40 #include <core/video_format.h>
41 #include <core/producer/frame_producer.h>
42 #include <core/frame/frame_factory.h>
43 #include <core/frame/draw_frame.h>
44 #include <core/frame/frame_transform.h>
45 #include <core/monitor/monitor.h>
47 #include <boost/algorithm/string.hpp>
48 #include <common/assert.h>
49 #include <boost/assign.hpp>
50 #include <boost/timer.hpp>
51 #include <boost/foreach.hpp>
52 #include <boost/filesystem.hpp>
53 #include <boost/range/algorithm/find_if.hpp>
54 #include <boost/range/algorithm/find.hpp>
55 #include <boost/property_tree/ptree.hpp>
56 #include <boost/regex.hpp>
57 #include <boost/thread/future.hpp>
59 #include <tbb/parallel_invoke.h>
65 namespace caspar { namespace ffmpeg {
67 struct ffmpeg_producer : public core::frame_producer_base
69 monitor::basic_subject event_subject_;
70 const std::wstring filename_;
72 const spl::shared_ptr<diagnostics::graph> graph_;
74 const spl::shared_ptr<core::frame_factory> frame_factory_;
75 const core::video_format_desc format_desc_;
80 const uint32_t start_;
82 std::unique_ptr<video_decoder> video_decoder_;
83 std::unique_ptr<audio_decoder> audio_decoder_;
85 core::constraints constraints_;
87 core::draw_frame last_frame_;
89 boost::optional<uint32_t> seek_target_;
92 explicit ffmpeg_producer(const spl::shared_ptr<core::frame_factory>& frame_factory,
93 const core::video_format_desc& format_desc,
94 const std::wstring& filename,
95 const std::wstring& filter,
100 , frame_factory_(frame_factory)
101 , format_desc_(format_desc)
102 , input_(graph_, filename_, loop, start, length)
103 , fps_(read_fps(input_.context(), format_desc_.fps))
104 , muxer_(fps_, frame_factory, format_desc_, filter)
106 , last_frame_(core::draw_frame::empty())
108 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
109 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
110 diagnostics::register_graph(graph_);
115 video_decoder_.reset(new video_decoder(input_));
116 video_decoder_->subscribe(event_subject_);
117 constraints_.width.set(video_decoder_->width());
118 constraints_.height.set(video_decoder_->height());
120 CASPAR_LOG(info) << print() << L" " << video_decoder_->print();
122 catch(averror_stream_not_found&)
124 //CASPAR_LOG(warning) << print() << " No video-stream found. Running without video.";
128 CASPAR_LOG_CURRENT_EXCEPTION();
129 CASPAR_LOG(warning) << print() << "Failed to open video-stream. Running without video.";
134 audio_decoder_ .reset(new audio_decoder(input_, format_desc_));
135 audio_decoder_->subscribe(event_subject_);
137 CASPAR_LOG(info) << print() << L" " << audio_decoder_->print();
139 catch(averror_stream_not_found&)
141 //CASPAR_LOG(warning) << print() << " No audio-stream found. Running without audio.";
145 CASPAR_LOG_CURRENT_EXCEPTION();
146 CASPAR_LOG(warning) << print() << " Failed to open audio-stream. Running without audio.";
151 CASPAR_LOG(info) << print() << L" Initialized";
156 core::draw_frame receive_impl() override
158 auto frame = core::draw_frame::late();
160 boost::timer frame_timer;
168 last_frame_ = frame = std::move(muxer_.front());
172 graph_->set_tag("underflow");
174 graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);
175 event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc_.fps);
177 event_subject_ << monitor::event("file/time") % monitor::duration(file_frame_number()/fps_)
178 % monitor::duration(file_nb_frames()/fps_)
179 << monitor::event("file/frame") % static_cast<int32_t>(file_frame_number())
180 % static_cast<int32_t>(file_nb_frames())
181 << monitor::event("file/fps") % fps_
182 << monitor::event("file/path") % filename_
183 << monitor::event("loop") % input_.loop();
188 core::draw_frame last_frame() override
191 return core::draw_frame::still(last_frame_);
194 core::constraints& pixel_constraints() override
199 uint32_t nb_frames() const override
202 return std::numeric_limits<uint32_t>::max();
204 uint32_t nb_frames = file_nb_frames();
206 nb_frames = std::min(input_.length(), nb_frames);
207 nb_frames = muxer_.calc_nb_frames(nb_frames);
209 return nb_frames > start_ ? nb_frames - start_ : 0;
212 uint32_t file_nb_frames() const
214 uint32_t file_nb_frames = 0;
215 file_nb_frames = std::max(file_nb_frames, video_decoder_ ? video_decoder_->nb_frames() : 0);
216 file_nb_frames = std::max(file_nb_frames, audio_decoder_ ? audio_decoder_->nb_frames() : 0);
217 return file_nb_frames;
220 uint32_t file_frame_number() const
222 return video_decoder_ ? video_decoder_->file_frame_number() : 0;
225 boost::unique_future<std::wstring> call(const std::vector<std::wstring>& params) override
227 static const boost::wregex loop_exp(L"LOOP\\s*(?<VALUE>\\d?)?", boost::regex::icase);
228 static const boost::wregex seek_exp(L"SEEK\\s+(?<VALUE>\\d+)", boost::regex::icase);
229 static const boost::wregex length_exp(L"LENGTH\\s+(?<VALUE>\\d+)?", boost::regex::icase);
230 static const boost::wregex start_exp(L"START\\s+(?<VALUE>\\d+)?", boost::regex::icase);
232 auto param = boost::algorithm::join(params, L" ");
237 if(boost::regex_match(param, what, loop_exp))
239 auto value = what["VALUE"].str();
241 input_.loop(boost::lexical_cast<bool>(value));
242 result = boost::lexical_cast<std::wstring>(loop());
244 else if(boost::regex_match(param, what, seek_exp))
246 auto value = what["VALUE"].str();
247 seek(boost::lexical_cast<uint32_t>(value));
249 else if(boost::regex_match(param, what, length_exp))
251 auto value = what["VALUE"].str();
253 length(boost::lexical_cast<uint32_t>(value));
254 result = boost::lexical_cast<std::wstring>(length());
256 else if(boost::regex_match(param, what, start_exp))
258 auto value = what["VALUE"].str();
260 start(boost::lexical_cast<uint32_t>(value));
261 result = boost::lexical_cast<std::wstring>(start());
264 CASPAR_THROW_EXCEPTION(invalid_argument());
266 return async(launch::deferred, [=]{return result;});
269 std::wstring print() const override
271 return L"ffmpeg[" + boost::filesystem::path(filename_).filename().wstring() + L"|"
272 + print_mode() + L"|"
273 + boost::lexical_cast<std::wstring>(file_frame_number()) + L"/" + boost::lexical_cast<std::wstring>(file_nb_frames()) + L"]";
276 std::wstring name() const override
281 boost::property_tree::wptree info() const override
283 boost::property_tree::wptree info;
284 info.add(L"type", L"ffmpeg");
285 info.add(L"filename", filename_);
286 info.add(L"width", video_decoder_ ? video_decoder_->width() : 0);
287 info.add(L"height", video_decoder_ ? video_decoder_->height() : 0);
288 info.add(L"progressive", video_decoder_ ? video_decoder_->is_progressive() : 0);
289 info.add(L"fps", fps_);
290 info.add(L"loop", input_.loop());
291 info.add(L"frame-number", frame_number());
292 auto nb_frames2 = nb_frames();
293 info.add(L"nb-frames", nb_frames2 == std::numeric_limits<int64_t>::max() ? -1 : nb_frames2);
294 info.add(L"file-frame-number", file_frame_number());
295 info.add(L"file-nb-frames", file_nb_frames());
299 void subscribe(const monitor::observable::observer_ptr& o) override
301 event_subject_.subscribe(o);
304 void unsubscribe(const monitor::observable::observer_ptr& o) override
306 event_subject_.unsubscribe(o);
313 for(int n = 0; n < 8 && (last_frame_ == core::draw_frame::empty() || (seek_target_ && file_frame_number() != *seek_target_+2)); ++n)
318 last_frame_ = muxer_.front();
319 seek_target_.reset();
324 void loop(bool value)
331 return input_.loop();
334 void length(uint32_t value)
336 input_.length(value);
341 return input_.length();
344 void start(uint32_t value)
351 return input_.start();
354 void seek(uint32_t target)
356 seek_target_ = std::min(target, file_nb_frames());
358 input_.seek(*seek_target_);
362 std::wstring print_mode() const
364 return ffmpeg::print_mode(video_decoder_ ? video_decoder_->width() : 0,
365 video_decoder_ ? video_decoder_->height() : 0,
367 video_decoder_ ? !video_decoder_->is_progressive() : false);
370 void decode_next_frame()
372 for(int n = 0; n < 8 && muxer_.empty(); ++n)
374 if(!muxer_.video_ready())
375 muxer_.push_video(video_decoder_ ? (*video_decoder_)() : create_frame());
376 if(!muxer_.audio_ready())
377 muxer_.push_audio(audio_decoder_ ? (*audio_decoder_)() : create_frame());
379 graph_->set_text(print());
383 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
385 auto filename = probe_stem(env::media_folder() + L"\\" + params.at(0));
388 return core::frame_producer::empty();
390 auto loop = boost::range::find(params, L"LOOP") != params.end();
391 auto start = get_param(L"START", params, get_param(L"SEEK", params, static_cast<uint32_t>(0)));
392 auto length = get_param(L"LENGTH", params, std::numeric_limits<uint32_t>::max());
393 auto filter_str = get_param(L"FILTER", params, L"");
395 return create_destroy_proxy(spl::make_shared_ptr(std::make_shared<ffmpeg_producer>(frame_factory, format_desc, filename, filter_str, loop, start, length)));