1 #include "../../stdafx.h"
\r
3 #include "ffmpeg_producer.h"
\r
6 #include "audio/audio_decoder.h"
\r
7 #include "video/video_decoder.h"
\r
9 #include <core/video_format.h>
\r
10 #include <common/utility/timer.h>
\r
11 #include <common/diagnostics/graph.h>
\r
12 #include <mixer/frame/draw_frame.h>
\r
13 #include <mixer/audio/audio_transform.h>
\r
15 #include <common/env.h>
\r
17 #include <tbb/parallel_invoke.h>
\r
21 namespace caspar { namespace core { namespace ffmpeg{
\r
23 struct ffmpeg_producer : public frame_producer
\r
25 safe_ptr<diagnostics::graph> graph_;
\r
29 std::unique_ptr<audio_decoder> audio_decoder_;
\r
30 std::unique_ptr<video_decoder> video_decoder_;
\r
32 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
33 std::deque<std::vector<short>> audio_chunk_channel_;
\r
35 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
37 const std::wstring filename_;
\r
39 safe_ptr<draw_frame> last_frame_;
\r
40 std::shared_ptr<frame_factory> frame_factory_;
\r
43 explicit ffmpeg_producer(const std::wstring& filename, bool loop)
\r
44 : graph_(diagnostics::create_graph("ffmpeg"))
\r
45 , filename_(filename)
\r
46 , last_frame_(draw_frame(draw_frame::empty()))
\r
47 , input_(graph_, filename, loop)
\r
49 graph_->add_guide("frame_time_target", 0.5, diagnostics::color(1.0f, 0.0f, 0.0f));
\r
50 graph_->set_color("frame_time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
55 CASPAR_LOG(info) << print() << " closing.";
\r
58 virtual void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
60 frame_factory_ = frame_factory;
\r
61 video_decoder_.reset(input_.get_video_codec_context().get() ? new video_decoder(input_.get_video_codec_context().get(), frame_factory) : nullptr);
\r
62 audio_decoder_.reset(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), frame_factory->get_video_format_desc().fps) : nullptr);
\r
65 virtual safe_ptr<draw_frame> receive()
\r
67 perf_timer_.reset();
\r
69 while(ouput_channel_.empty() && !input_.is_eof())
\r
71 aligned_buffer video_packet;
\r
72 if(video_frame_channel_.size() < 3 && video_decoder_)
\r
73 video_packet = input_.get_video_packet();
\r
75 aligned_buffer audio_packet;
\r
76 if(audio_chunk_channel_.size() < 3 && audio_decoder_)
\r
77 audio_packet = input_.get_audio_packet();
\r
79 tbb::parallel_invoke(
\r
81 { // Video Decoding and Scaling
\r
82 if(!video_packet.empty() && video_decoder_)
\r
86 auto frame = video_decoder_->execute(video_packet);
\r
87 video_frame_channel_.push_back(std::move(frame));
\r
91 CASPAR_LOG_CURRENT_EXCEPTION();
\r
92 video_decoder_.reset();
\r
93 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
99 if(!audio_packet.empty() && audio_decoder_)
\r
103 auto chunks = audio_decoder_->execute(audio_packet);
\r
104 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
108 CASPAR_LOG_CURRENT_EXCEPTION();
\r
109 audio_decoder_.reset();
\r
110 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
115 while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
117 std::shared_ptr<write_frame> frame;
\r
121 frame = video_frame_channel_.front();
\r
122 video_frame_channel_.pop_front();
\r
125 if(audio_decoder_)
\r
129 frame = frame_factory_->create_frame(1, 1);
\r
130 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);
\r
133 frame->audio_data() = std::move(audio_chunk_channel_.front());
\r
134 audio_chunk_channel_.pop_front();
\r
137 ouput_channel_.push(safe_ptr<write_frame>(frame));
\r
140 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
141 return last_frame_;
\r
144 graph_->update("frame_time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));
\r
146 auto result = last_frame_;
\r
147 if(!ouput_channel_.empty())
\r
149 result = std::move(ouput_channel_.front());
\r
150 last_frame_ = draw_frame(result);
\r
151 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio
\r
152 ouput_channel_.pop();
\r
154 else if(input_.is_eof())
\r
155 return draw_frame::eof();
\r
160 virtual std::wstring print() const
\r
162 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
166 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
168 static const std::vector<std::wstring> extensions = boost::assign::list_of
\r
169 (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"wmw")(L"wma")(L"ogg")(L"divx")(L"wav")(L"mp3");
\r
170 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
172 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
174 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
177 if(ext == extensions.end())
\r
178 return frame_producer::empty();
\r
180 std::wstring path = filename + L"." + *ext;
\r
181 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
183 return make_safe<ffmpeg_producer>(path, loop);
\r