2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "../stdafx.h"
\r
22 #include "ffmpeg_producer.h"
\r
25 #include "audio/audio_decoder.h"
\r
26 #include "video/video_decoder.h"
\r
28 #include <common/utility/timer.h>
\r
29 #include <common/diagnostics/graph.h>
\r
31 #include <core/producer/frame/basic_frame.h>
\r
32 #include <core/producer/frame/write_frame.h>
\r
33 #include <core/producer/frame/audio_transform.h>
\r
34 #include <core/video_format.h>
\r
36 #include <common/env.h>
\r
37 #include <common/utility/timer.h>
\r
38 #include <common/utility/assert.h>
\r
40 #include <tbb/parallel_invoke.h>
\r
42 #include <boost/timer.hpp>
\r
45 #include <functional>
\r
49 struct ffmpeg_producer : public core::frame_producer
\r
51 const std::wstring filename_;
\r
54 std::shared_ptr<diagnostics::graph> graph_;
\r
55 boost::timer perf_timer_;
\r
57 std::deque<safe_ptr<core::write_frame>> video_frame_buffer_;
\r
58 std::deque<std::vector<short>> audio_chunk_buffer_;
\r
60 std::queue<safe_ptr<core::basic_frame>> ouput_channel_;
\r
62 std::shared_ptr<core::frame_factory> frame_factory_;
\r
65 std::unique_ptr<video_decoder> video_decoder_;
\r
66 std::unique_ptr<audio_decoder> audio_decoder_;
\r
68 explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, bool loop, int start_frame, int end_frame)
\r
69 : filename_(filename)
\r
71 , graph_(diagnostics::create_graph(narrow(print())))
\r
72 , frame_factory_(frame_factory)
\r
73 , input_(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, start_frame, end_frame)
\r
75 graph_->add_guide("frame-time", 0.5);
\r
76 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
77 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));
\r
79 double frame_time = 1.0f/input_.fps();
\r
80 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;
\r
81 if(abs(frame_time - format_frame_time) > 0.0001)
\r
82 CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;
\r
86 video_decoder_.reset(input_.get_video_codec_context().get() ?
\r
87 new video_decoder(input_.get_video_codec_context().get(), frame_factory) : nullptr);
\r
91 CASPAR_LOG_CURRENT_EXCEPTION();
\r
92 video_decoder_.reset();
\r
93 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
98 audio_decoder_.reset(input_.get_audio_codec_context().get() ?
\r
99 new audio_decoder(input_.get_audio_codec_context().get(), frame_factory->get_video_format_desc()) : nullptr);
\r
103 CASPAR_LOG_CURRENT_EXCEPTION();
\r
104 audio_decoder_.reset();
\r
105 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
108 if(!video_decoder_ && !audio_decoder_)
\r
110 BOOST_THROW_EXCEPTION(
\r
111 caspar_exception() <<
\r
112 msg_info("Failed to initialize any decoder"));
\r
116 virtual safe_ptr<core::basic_frame> receive()
\r
118 perf_timer_.restart();
\r
120 for(size_t n = 0; ouput_channel_.size() < 2 && input_.has_packet() && n < 32; ++n) // 32 packets should be enough. Otherwise there probably was an error and we want to avoid infinite looping.
\r
122 tbb::parallel_invoke
\r
126 if(video_frame_buffer_.size() < 3)
\r
127 try_decode_video_packet(input_.get_video_packet());
\r
131 if(audio_chunk_buffer_.size() < 3)
\r
132 try_decode_audio_packet(input_.get_audio_packet());
\r
136 merge_audio_and_video();
\r
139 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5));
\r
141 return get_next_frame();
\r
144 virtual std::wstring print() const
\r
146 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
149 void try_decode_video_packet(const std::shared_ptr<aligned_buffer>& video_packet)
\r
151 if(video_decoder_) // Video Decoding.
\r
155 auto frames = video_decoder_->execute(this, video_packet);
\r
156 video_frame_buffer_.insert(video_frame_buffer_.end(), frames.begin(), frames.end());
\r
160 CASPAR_LOG_CURRENT_EXCEPTION();
\r
161 video_decoder_.reset();
\r
162 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
167 void try_decode_audio_packet(const std::shared_ptr<aligned_buffer>& audio_packet)
\r
169 if(audio_decoder_) // Audio Decoding.
\r
173 auto chunks = audio_decoder_->execute(audio_packet);
\r
174 audio_chunk_buffer_.insert(audio_chunk_buffer_.end(), chunks.begin(), chunks.end());
\r
178 CASPAR_LOG_CURRENT_EXCEPTION();
\r
179 audio_decoder_.reset();
\r
180 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
185 void merge_audio_and_video()
\r
187 std::shared_ptr<core::write_frame> frame;
\r
189 if(!video_frame_buffer_.empty() && !audio_chunk_buffer_.empty())
\r
191 frame = video_frame_buffer_.front();
\r
192 video_frame_buffer_.pop_front();
\r
194 frame->audio_data() = std::move(audio_chunk_buffer_.front());
\r
195 audio_chunk_buffer_.pop_front();
\r
197 else if(!video_frame_buffer_.empty() && !audio_decoder_)
\r
199 frame = video_frame_buffer_.front();
\r
200 video_frame_buffer_.pop_front();
\r
201 frame->get_audio_transform().set_has_audio(false);
\r
203 else if(!audio_chunk_buffer_.empty() && !video_decoder_)
\r
205 frame = frame_factory_->create_frame(this, 1, 1);
\r
206 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);
\r
208 frame->audio_data() = std::move(audio_chunk_buffer_.front());
\r
209 audio_chunk_buffer_.pop_front();
\r
213 ouput_channel_.push(make_safe(frame));
\r
216 safe_ptr<core::basic_frame> get_next_frame()
\r
219 return core::basic_frame::eof();
\r
221 if(ouput_channel_.empty())
\r
223 graph_->add_tag("underflow");
\r
224 return core::basic_frame::late();
\r
227 auto frame = std::move(ouput_channel_.front());
\r
228 ouput_channel_.pop();
\r
232 bool is_eof() const
\r
234 return ouput_channel_.empty() && ((!video_decoder_ && !audio_decoder_) || !input_.is_running());
\r
238 safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
240 static const std::vector<std::wstring> extensions = boost::assign::list_of
\r
241 (L"mpg")(L"mpeg")(L"avi")(L"mov")(L"qt")(L"webm")(L"dv")(L"mp4")(L"f4v")(L"flv")(L"mkv")(L"mka")(L"wmv")(L"wma")(L"ogg")(L"divx")(L"xvid")(L"wav")(L"mp3")(L"m2v");
\r
242 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
244 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
246 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
249 if(ext == extensions.end())
\r
250 return core::frame_producer::empty();
\r
252 std::wstring path = filename + L"." + *ext;
\r
253 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
255 static const boost::wregex expr(L"\\((?<START>\\d+)(,(?<END>\\d+)?)?\\)");//(,(?<END>\\d+))?\\]"); // boost::regex has no repeated captures?
\r
256 boost::wsmatch what;
\r
257 auto it = std::find_if(params.begin(), params.end(), [&](const std::wstring& str)
\r
259 return boost::regex_match(str, what, expr);
\r
265 if(it != params.end())
\r
267 start = lexical_cast_or_default(what["START"].str(), -1);
\r
268 if(what["END"].matched)
\r
269 end = lexical_cast_or_default(what["END"].str(), -1);
\r
272 return make_safe<ffmpeg_producer>(frame_factory, path, loop, start, end);
\r