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 <common/utility/timer.h>
\r
10 #include <common/diagnostics/graph.h>
\r
12 #include <core/producer/frame/basic_frame.h>
\r
13 #include <core/producer/frame/write_frame.h>
\r
14 #include <core/producer/frame/audio_transform.h>
\r
15 #include <core/video_format.h>
\r
17 #include <common/env.h>
\r
18 #include <common/utility/timer.h>
\r
19 #include <common/utility/assert.h>
\r
21 #include <tbb/parallel_invoke.h>
\r
24 #include <functional>
\r
28 struct ffmpeg_producer : public core::frame_producer
\r
30 const std::wstring filename_;
\r
32 printer parent_printer_;
\r
34 std::shared_ptr<diagnostics::graph> graph_;
\r
37 std::unique_ptr<audio_decoder> audio_decoder_;
\r
38 std::unique_ptr<video_decoder> video_decoder_;
\r
40 std::deque<safe_ptr<core::write_frame>> video_frame_channel_;
\r
41 std::deque<std::vector<short>> audio_chunk_channel_;
\r
43 std::queue<safe_ptr<core::basic_frame>> ouput_channel_;
\r
45 safe_ptr<core::basic_frame> last_frame_;
\r
46 std::shared_ptr<core::frame_factory> frame_factory_;
\r
48 std::unique_ptr<input> input_;
\r
50 explicit ffmpeg_producer(const std::wstring& filename, bool loop)
\r
51 : filename_(filename)
\r
53 , last_frame_(core::basic_frame(core::basic_frame::empty()))
\r
56 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this));
\r
57 graph_->add_guide("frame-time", 0.5);
\r
58 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
61 virtual void initialize(const safe_ptr<core::frame_factory>& frame_factory)
\r
63 frame_factory_ = frame_factory;
\r
64 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, std::bind(&ffmpeg_producer::print, this)));
\r
65 video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);
\r
66 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
68 double frame_time = 1.0f/input_->fps();
\r
69 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;
\r
70 if(abs(frame_time - format_frame_time) > 0.0001)
\r
71 CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;
\r
75 virtual void set_parent_printer(const printer& parent_printer)
\r
77 parent_printer_ = parent_printer;
\r
80 virtual safe_ptr<core::basic_frame> receive()
\r
82 perf_timer_.reset();
\r
84 while(ouput_channel_.size() < 2 && !input_->is_eof())
\r
86 aligned_buffer video_packet;
\r
87 if(video_frame_channel_.size() < 3 && video_decoder_)
\r
88 video_packet = input_->get_video_packet();
\r
90 aligned_buffer audio_packet;
\r
91 if(audio_chunk_channel_.size() < 3 && audio_decoder_)
\r
92 audio_packet = input_->get_audio_packet();
\r
94 tbb::parallel_invoke(
\r
96 { // Video Decoding and Scaling
\r
97 if(!video_packet.empty() && video_decoder_)
\r
101 auto frame = video_decoder_->execute(video_packet);
\r
102 frame->tag(reinterpret_cast<int>(this));
\r
103 video_frame_channel_.push_back(std::move(frame));
\r
107 CASPAR_LOG_CURRENT_EXCEPTION();
\r
108 video_decoder_.reset();
\r
109 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
114 { // Audio Decoding
\r
115 if(!audio_packet.empty() && audio_decoder_)
\r
119 auto chunks = audio_decoder_->execute(audio_packet);
\r
120 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
124 CASPAR_LOG_CURRENT_EXCEPTION();
\r
125 audio_decoder_.reset();
\r
126 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
131 while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
133 std::shared_ptr<core::write_frame> frame;
\r
137 frame = video_frame_channel_.front();
\r
138 video_frame_channel_.pop_front();
\r
141 if(audio_decoder_)
\r
145 frame = frame_factory_->create_frame(1, 1);
\r
146 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);
\r
149 frame->audio_data() = std::move(audio_chunk_channel_.front());
\r
150 audio_chunk_channel_.pop_front();
\r
153 ouput_channel_.push(safe_ptr<core::write_frame>(frame));
\r
156 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
157 return last_frame_;
\r
160 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));
\r
162 auto result = last_frame_;
\r
163 if(!ouput_channel_.empty())
\r
165 result = get_frame();
\r
167 bool interlace = abs(input_->fps()/2.0 - frame_factory_->get_video_format_desc().fps) < 0.001;
\r
168 if(interlace && !ouput_channel_.empty())
\r
169 result = core::basic_frame::interlace(result, get_frame(), frame_factory_->get_video_format_desc().mode);
\r
171 else if(input_->is_eof())
\r
172 result = core::basic_frame::eof();
\r
177 core::basic_frame get_frame()
\r
179 CASPAR_ASSERT(!ouput_channel_.empty());
\r
180 auto result = std::move(ouput_channel_.front());
\r
181 last_frame_ = core::basic_frame(result);
\r
182 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio
\r
183 ouput_channel_.pop();
\r
187 virtual std::wstring print() const
\r
189 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
193 safe_ptr<core::frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
195 static const std::vector<std::wstring> extensions = boost::assign::list_of
\r
196 (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
197 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
199 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
201 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
204 if(ext == extensions.end())
\r
205 return core::frame_producer::empty();
\r
207 std::wstring path = filename + L"." + *ext;
\r
208 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
210 return make_safe<ffmpeg_producer>(path, loop);
\r