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
33 std::shared_ptr<diagnostics::graph> graph_;
\r
36 std::unique_ptr<audio_decoder> audio_decoder_;
\r
37 std::unique_ptr<video_decoder> video_decoder_;
\r
39 std::deque<safe_ptr<core::write_frame>> video_frame_channel_;
\r
40 std::deque<std::vector<short>> audio_chunk_channel_;
\r
42 std::queue<safe_ptr<core::basic_frame>> ouput_channel_;
\r
44 safe_ptr<core::basic_frame> last_frame_;
\r
45 std::shared_ptr<core::frame_factory> frame_factory_;
\r
47 std::unique_ptr<input> input_;
\r
49 explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, bool loop)
\r
50 : filename_(filename)
\r
52 , last_frame_(core::basic_frame(core::basic_frame::empty()))
\r
53 , frame_factory_(frame_factory)
\r
55 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this));
\r
56 graph_->add_guide("frame-time", 0.5);
\r
57 graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
59 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_));
\r
60 video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);
\r
61 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
63 double frame_time = 1.0f/input_->fps();
\r
64 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;
\r
65 if(abs(frame_time - format_frame_time) > 0.0001)
\r
66 CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;
\r
69 virtual safe_ptr<core::basic_frame> receive()
\r
71 perf_timer_.reset();
\r
73 while(ouput_channel_.size() < 2 && !input_->is_eof())
\r
75 aligned_buffer video_packet;
\r
76 if(video_frame_channel_.size() < 3 && video_decoder_)
\r
77 video_packet = input_->get_video_packet();
\r
79 aligned_buffer audio_packet;
\r
80 if(audio_chunk_channel_.size() < 3 && audio_decoder_)
\r
81 audio_packet = input_->get_audio_packet();
\r
83 tbb::parallel_invoke(
\r
85 { // Video Decoding and Scaling
\r
86 if(!video_packet.empty() && video_decoder_)
\r
90 auto frame = video_decoder_->execute(this, video_packet);
\r
91 video_frame_channel_.push_back(std::move(frame));
\r
95 CASPAR_LOG_CURRENT_EXCEPTION();
\r
96 video_decoder_.reset();
\r
97 CASPAR_LOG(warning) << print() << " removed video-stream.";
\r
102 { // Audio Decoding
\r
103 if(!audio_packet.empty() && audio_decoder_)
\r
107 auto chunks = audio_decoder_->execute(audio_packet);
\r
108 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
112 CASPAR_LOG_CURRENT_EXCEPTION();
\r
113 audio_decoder_.reset();
\r
114 CASPAR_LOG(warning) << print() << " removed audio-stream.";
\r
119 while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
121 std::shared_ptr<core::write_frame> frame;
\r
125 frame = video_frame_channel_.front();
\r
126 video_frame_channel_.pop_front();
\r
129 if(audio_decoder_)
\r
131 if(!frame) // If there is no video create a dummy frame.
\r
133 frame = frame_factory_->create_frame(this, 1, 1);
\r
134 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);
\r
137 frame->audio_data() = std::move(audio_chunk_channel_.front());
\r
138 audio_chunk_channel_.pop_front();
\r
141 ouput_channel_.push(safe_ptr<core::write_frame>(frame));
\r
144 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
145 return last_frame_;
\r
148 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));
\r
150 auto result = last_frame_;
\r
151 if(!ouput_channel_.empty())
\r
152 result = get_frame(); // TODO: Support 50p
\r
153 else if(input_->is_eof())
\r
154 result = core::basic_frame::eof();
\r
159 core::basic_frame get_frame()
\r
161 CASPAR_ASSERT(!ouput_channel_.empty());
\r
162 auto result = std::move(ouput_channel_.front());
\r
163 last_frame_ = core::basic_frame(result);
\r
164 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio
\r
165 ouput_channel_.pop();
\r
169 virtual std::wstring print() const
\r
171 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
175 safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)
\r
177 static const std::vector<std::wstring> extensions = boost::assign::list_of
\r
178 (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
179 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
181 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
183 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
186 if(ext == extensions.end())
\r
187 return core::frame_producer::empty();
\r
189 std::wstring path = filename + L"." + *ext;
\r
190 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
192 return make_safe<ffmpeg_producer>(frame_factory, path, loop);
\r