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 "../../format/video_format.h"
\r
10 #include "../../processor/draw_frame.h"
\r
12 #include <tbb/parallel_invoke.h>
\r
14 #include <boost/optional.hpp>
\r
18 using namespace boost::assign;
\r
20 namespace caspar { namespace core { namespace ffmpeg{
\r
22 struct ffmpeg_producer : public frame_producer
\r
25 std::unique_ptr<audio_decoder> audio_decoder_;
\r
26 video_decoder video_decoder_;
\r
28 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
29 std::deque<std::vector<short>> audio_chunk_channel_;
\r
31 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
33 const std::wstring filename_;
\r
35 safe_ptr<draw_frame> last_frame_;
\r
37 video_format_desc format_desc_;
\r
40 explicit ffmpeg_producer(const std::wstring& filename)
\r
41 : filename_(filename)
\r
42 , last_frame_(draw_frame(draw_frame::empty()))
\r
44 , video_decoder_(input_.get_video_codec_context().get())
\r
45 , audio_decoder_(input_.get_audio_codec_context().get() ? new audio_decoder(input_.get_audio_codec_context().get(), input_.fps()) : nullptr){}
\r
47 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
49 format_desc_ = frame_processor->get_video_format_desc();
\r
50 video_decoder_.initialize(frame_processor);
\r
53 virtual safe_ptr<draw_frame> receive()
\r
55 while(ouput_channel_.empty() && !input_.is_eof())
\r
57 aligned_buffer video_packet;
\r
58 if(video_frame_channel_.size() < 3)
\r
59 video_packet = input_.get_video_packet();
\r
61 aligned_buffer audio_packet;
\r
62 if(audio_chunk_channel_.size() < 3)
\r
63 audio_packet = input_.get_audio_packet();
\r
65 tbb::parallel_invoke(
\r
67 { // Video Decoding and Scaling
\r
68 if(!video_packet.empty())
\r
70 auto frame = video_decoder_.execute(video_packet);
\r
71 video_frame_channel_.push_back(std::move(frame));
\r
76 if(!audio_packet.empty() && audio_decoder_)
\r
80 auto chunks = audio_decoder_->execute(audio_packet);
\r
81 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
85 CASPAR_LOG_CURRENT_EXCEPTION();
\r
86 audio_decoder_.reset();
\r
91 while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !audio_decoder_))
\r
95 video_frame_channel_.front()->audio_data() = std::move(audio_chunk_channel_.front());
\r
96 audio_chunk_channel_.pop_front();
\r
99 ouput_channel_.push(video_frame_channel_.front());
\r
100 video_frame_channel_.pop_front();
\r
103 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
104 return last_frame_;
\r
107 auto result = last_frame_;
\r
108 if(!ouput_channel_.empty())
\r
110 result = std::move(ouput_channel_.front());
\r
111 last_frame_ = draw_frame(result);
\r
112 last_frame_->audio_volume(0.0); // last_frame should not have audio
\r
113 ouput_channel_.pop();
\r
115 else if(input_.is_eof())
\r
116 return draw_frame::eof();
\r
121 void set_loop(bool value)
\r
123 input_.set_loop(value);
\r
126 void seek(unsigned long long value)
\r
128 if(!input_.seek(value))
\r
129 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Failed to seek file: )") << arg_value_info(narrow(filename_)));
\r
132 virtual std::wstring print() const
\r
134 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
138 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
140 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
141 std::wstring filename = params[0];
\r
143 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
145 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
148 if(ext == extensions.end())
\r
149 return frame_producer::empty();
\r
151 auto producer = make_safe<ffmpeg_producer>(filename + L"." + *ext);
\r
153 producer->set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());
\r
155 auto seek = std::find(params.begin(), params.end(), L"SEEK");
\r
156 if(seek != params.end() && ++seek != params.end())
\r
157 producer->seek(boost::lexical_cast<unsigned long long>(*seek));
\r