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
16 using namespace boost::assign;
\r
18 namespace caspar { namespace core { namespace ffmpeg{
\r
20 struct ffmpeg_producer : public frame_producer
\r
23 audio_decoder audio_decoder_;
\r
24 video_decoder video_decoder_;
\r
26 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
27 std::deque<std::vector<short>> audio_chunk_channel_;
\r
29 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
31 const std::wstring filename_;
\r
33 safe_ptr<draw_frame> last_frame_;
\r
35 video_format_desc format_desc_;
\r
38 explicit ffmpeg_producer(const std::wstring& filename, const std::vector<std::wstring>& params)
\r
39 : filename_(filename)
\r
40 , last_frame_(draw_frame(draw_frame::empty()))
\r
42 , video_decoder_(input_.get_video_codec_context().get())
\r
43 , audio_decoder_(input_.get_audio_codec_context().get(), input_.fps())
\r
45 input_.set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());
\r
47 auto seek = std::find(params.begin(), params.end(), L"SEEK");
\r
48 if(seek != params.end() && ++seek != params.end())
\r
50 if(!input_.seek(boost::lexical_cast<unsigned long long>(*seek)))
\r
51 CASPAR_LOG(warning) << "Failed to seek file: " << filename_ << "to frame" << *seek;
\r
55 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
57 format_desc_ = frame_processor->get_video_format_desc();
\r
58 video_decoder_.initialize(frame_processor);
\r
61 virtual safe_ptr<draw_frame> receive()
\r
63 while(ouput_channel_.empty() && !input_.is_eof())
\r
65 aligned_buffer video_packet;
\r
66 if(video_frame_channel_.size() < 3)
\r
67 video_packet = input_.get_video_packet();
\r
69 aligned_buffer audio_packet;
\r
70 if(audio_chunk_channel_.size() < 3)
\r
71 audio_packet = input_.get_audio_packet();
\r
73 tbb::parallel_invoke(
\r
75 { // Video Decoding and Scaling
\r
76 if(!video_packet.empty())
\r
78 auto frame = video_decoder_.execute(video_packet);
\r
79 video_frame_channel_.push_back(std::move(frame));
\r
84 if(!audio_packet.empty())
\r
86 auto chunks = audio_decoder_.execute(audio_packet);
\r
87 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
91 while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || input_.get_audio_codec_context() == nullptr))
\r
93 if(input_.get_audio_codec_context() != nullptr)
\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 virtual std::wstring print() const
\r
123 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
127 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
129 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
130 std::wstring filename = params[0];
\r
132 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
134 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
137 if(ext == extensions.end())
\r
138 return frame_producer::empty();
\r
140 return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);
\r