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 "../../video_format.h"
\r
10 #include "../../processor/draw_frame.h"
\r
12 #include <common/env.h>
\r
14 #include <tbb/parallel_invoke.h>
\r
16 #include <boost/regex.hpp>
\r
20 using namespace boost::assign;
\r
22 namespace caspar { namespace core { namespace ffmpeg{
\r
24 struct ffmpeg_producer : public frame_producer
\r
27 std::unique_ptr<audio_decoder> audio_decoder_;
\r
28 video_decoder video_decoder_;
\r
30 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
31 std::deque<std::vector<short>> audio_chunk_channel_;
\r
33 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
35 const std::wstring filename_;
\r
37 safe_ptr<draw_frame> last_frame_;
\r
39 video_format_desc format_desc_;
\r
42 explicit ffmpeg_producer(const std::wstring& filename, bool loop, double start_time = 0, double end_time = -1.0)
\r
43 : filename_(filename)
\r
44 , last_frame_(draw_frame(draw_frame::empty()))
\r
45 , input_(filename, loop, start_time, end_time)
\r
46 , video_decoder_(input_.get_video_codec_context().get())
\r
47 , audio_decoder_(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), input_.fps()) : nullptr){}
\r
49 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
51 format_desc_ = frame_processor->get_video_format_desc();
\r
52 video_decoder_.initialize(frame_processor);
\r
55 virtual safe_ptr<draw_frame> receive()
\r
57 while(ouput_channel_.empty() && !input_.is_eof())
\r
59 aligned_buffer video_packet;
\r
60 if(video_frame_channel_.size() < 3)
\r
61 video_packet = input_.get_video_packet();
\r
63 aligned_buffer audio_packet;
\r
64 if(audio_chunk_channel_.size() < 3)
\r
65 audio_packet = input_.get_audio_packet();
\r
67 tbb::parallel_invoke(
\r
69 { // Video Decoding and Scaling
\r
70 if(!video_packet.empty())
\r
72 auto frame = video_decoder_.execute(video_packet);
\r
73 video_frame_channel_.push_back(std::move(frame));
\r
78 if(!audio_packet.empty() && audio_decoder_)
\r
82 auto chunks = audio_decoder_->execute(audio_packet);
\r
83 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
87 CASPAR_LOG_CURRENT_EXCEPTION();
\r
88 audio_decoder_.reset();
\r
93 while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
97 video_frame_channel_.front()->audio_data() = std::move(audio_chunk_channel_.front());
\r
98 audio_chunk_channel_.pop_front();
\r
101 ouput_channel_.push(video_frame_channel_.front());
\r
102 video_frame_channel_.pop_front();
\r
105 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
106 return last_frame_;
\r
109 auto result = last_frame_;
\r
110 if(!ouput_channel_.empty())
\r
112 result = std::move(ouput_channel_.front());
\r
113 last_frame_ = draw_frame(result);
\r
114 last_frame_->audio_volume(0.0); // last_frame should not have audio
\r
115 ouput_channel_.pop();
\r
117 else if(input_.is_eof())
\r
118 return draw_frame::eof();
\r
123 virtual std::wstring print() const
\r
125 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
129 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
131 static const std::vector<std::wstring> extensions = list_of(L"mpg")(L"avi")(L"mov")(L"dv")(L"wav")(L"mp3")(L"mp4")(L"f4v")(L"flv");
\r
132 std::wstring filename = env::media_folder() + L"\\" + params[0];
\r
134 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
136 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
139 if(ext == extensions.end())
\r
140 return frame_producer::empty();
\r
142 std::wstring path = filename + L"." + *ext;
\r
143 bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();
\r
145 static boost::wregex frame_range_expr(L".*\\[(?<START>\\d+\\.?\\d*)(-?(?<END>\\d+\\.?\\d*))?\\].*");
\r
147 double start_time = 0.0;
\r
148 double end_time = -1.0;
\r
150 boost::wsmatch what;
\r
151 if(std::find_if(params.begin(), params.end(), [&](const std::wstring& str){ return boost::regex_match(str, what, frame_range_expr);}) != params.end())
\r
153 try{start_time = boost::lexical_cast<double>(what["START"].str());}
\r
154 catch(boost::bad_lexical_cast&){}
\r
156 try{end_time = boost::lexical_cast<double>(what["END"].str());}
\r
157 catch(boost::bad_lexical_cast&){}
\r
160 return make_safe<ffmpeg_producer>(path, loop, start_time, end_time);
\r