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
20 #include <functional>
\r
22 namespace caspar { namespace core { namespace ffmpeg{
\r
24 struct ffmpeg_producer : public frame_producer
\r
26 const std::wstring filename_;
\r
28 printer parent_printer_;
\r
30 std::shared_ptr<diagnostics::graph> graph_;
\r
33 std::unique_ptr<audio_decoder> audio_decoder_;
\r
34 std::unique_ptr<video_decoder> video_decoder_;
\r
36 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
37 std::deque<std::vector<short>> audio_chunk_channel_;
\r
39 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
41 safe_ptr<draw_frame> last_frame_;
\r
42 std::shared_ptr<frame_factory> frame_factory_;
\r
44 std::unique_ptr<input> input_;
\r
46 explicit ffmpeg_producer(const std::wstring& filename, bool loop)
\r
47 : filename_(filename)
\r
49 , last_frame_(draw_frame(draw_frame::empty()))
\r
52 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this));
\r
53 graph_->add_guide("frame-time", 0.5);
\r
54 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
57 virtual void initialize(const safe_ptr<frame_factory>& frame_factory)
\r
59 frame_factory_ = frame_factory;
\r
60 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, std::bind(&ffmpeg_producer::print, this)));
\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 void set_parent_printer(const printer& parent_printer)
\r
67 parent_printer_ = parent_printer;
\r
70 virtual safe_ptr<draw_frame> receive()
\r
72 perf_timer_.reset();
\r
74 while(ouput_channel_.empty() && !input_->is_eof())
\r
76 aligned_buffer video_packet;
\r
77 if(video_frame_channel_.size() < 3 && video_decoder_)
\r
78 video_packet = input_->get_video_packet();
\r
80 aligned_buffer audio_packet;
\r
81 if(audio_chunk_channel_.size() < 3 && audio_decoder_)
\r
82 audio_packet = input_->get_audio_packet();
\r
84 tbb::parallel_invoke(
\r
86 { // Video Decoding and Scaling
\r
87 if(!video_packet.empty() && video_decoder_)
\r
91 auto frame = video_decoder_->execute(video_packet);
\r
92 frame->tag(reinterpret_cast<int>(this));
\r
93 video_frame_channel_.push_back(std::move(frame));
\r
97 CASPAR_LOG_CURRENT_EXCEPTION();
\r
98 video_decoder_.reset();
\r
99 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
104 { // Audio Decoding
\r
105 if(!audio_packet.empty() && audio_decoder_)
\r
109 auto chunks = audio_decoder_->execute(audio_packet);
\r
110 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
114 CASPAR_LOG_CURRENT_EXCEPTION();
\r
115 audio_decoder_.reset();
\r
116 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
121 while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
123 std::shared_ptr<write_frame> frame;
\r
127 frame = video_frame_channel_.front();
\r
128 video_frame_channel_.pop_front();
\r
131 if(audio_decoder_)
\r
135 frame = frame_factory_->create_frame(1, 1);
\r
136 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);
\r
139 frame->audio_data() = std::move(audio_chunk_channel_.front());
\r
140 audio_chunk_channel_.pop_front();
\r
143 ouput_channel_.push(safe_ptr<write_frame>(frame));
\r
146 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
147 return last_frame_;
\r
150 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));
\r
152 auto result = last_frame_;
\r
153 if(!ouput_channel_.empty())
\r
155 result = std::move(ouput_channel_.front());
\r
156 last_frame_ = draw_frame(result);
\r
157 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio
\r
158 ouput_channel_.pop();
\r
160 else if(input_->is_eof())
\r
161 return draw_frame::eof();
\r
166 virtual std::wstring print() const
\r
168 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
172 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
174 static const std::vector<std::wstring> extensions = boost::assign::list_of
\r
175 (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
176 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
178 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
180 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
183 if(ext == extensions.end())
\r
184 return frame_producer::empty();
\r
186 std::wstring path = filename + L"." + *ext;
\r
187 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
189 return make_safe<ffmpeg_producer>(path, loop);
\r