1 #include "../../stdafx.h"
\r
3 #include "ffmpeg_producer.h"
\r
5 #if defined(_MSC_VER)
\r
6 #pragma warning (push)
\r
7 #pragma warning (disable : 4244)
\r
12 #define __STDC_CONSTANT_MACROS
\r
13 #define __STDC_LIMIT_MACROS
\r
14 #include <libavcodec/avcodec.h>
\r
15 #include <libavformat/avformat.h>
\r
16 #include <libavutil/avutil.h>
\r
17 #include <libswscale/swscale.h>
\r
20 #if defined(_MSC_VER)
\r
21 #pragma warning (pop)
\r
26 #include "audio/audio_decoder.h"
\r
27 #include "video/video_decoder.h"
\r
28 #include "video/video_decoder.h"
\r
30 #include "../../format/video_format.h"
\r
31 #include "../../processor/draw_frame.h"
\r
32 #include "../../processor/draw_frame.h"
\r
34 #include <tbb/mutex.h>
\r
35 #include <tbb/parallel_invoke.h>
\r
36 #include <tbb/task_group.h>
\r
38 #include <boost/algorithm/string/case_conv.hpp>
\r
39 #include <boost/lexical_cast.hpp>
\r
40 #include <boost/thread.hpp>
\r
41 #include <boost/thread/once.hpp>
\r
45 using namespace boost::assign;
\r
47 namespace caspar { namespace core { namespace ffmpeg{
\r
49 struct ffmpeg_producer_impl
\r
52 ffmpeg_producer_impl(const std::wstring& filename, const std::vector<std::wstring>& params) : filename_(filename), last_frame_(draw_frame(draw_frame::empty())),
\r
53 input_(filename), video_decoder_(input_.get_video_codec_context().get()), audio_decoder_(input_.get_audio_codec_context().get())
\r
55 input_.set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());
\r
57 auto seek = std::find(params.begin(), params.end(), L"SEEK");
\r
58 if(seek != params.end() && ++seek != params.end())
\r
60 if(!input_.seek(boost::lexical_cast<unsigned long long>(*seek)))
\r
61 CASPAR_LOG(warning) << "Failed to seek file: " << filename_ << "to frame" << *seek;
\r
65 void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
67 format_desc_ = frame_processor->get_video_format_desc();
\r
68 video_decoder_.initialize(frame_processor);
\r
71 safe_ptr<draw_frame> receive()
\r
73 while(ouput_channel_.empty() && !input_.is_eof())
\r
75 aligned_buffer video_packet;
\r
76 if(video_frame_channel_.size() < 3)
\r
77 video_packet = input_.get_video_packet();
\r
79 aligned_buffer audio_packet;
\r
80 if(audio_chunk_channel_.size() < 3)
\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())
\r
88 auto frame = video_decoder_.execute(video_packet);
\r
89 video_frame_channel_.push_back(std::move(frame));
\r
94 if(!audio_packet.empty())
\r
96 auto chunks = audio_decoder_.execute(audio_packet);
\r
97 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
101 while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || input_.get_audio_codec_context() == nullptr))
\r
103 std::vector<short> audio_data;
\r
104 if(input_.get_audio_codec_context() != nullptr)
\r
106 audio_data = std::move(audio_chunk_channel_.front());
\r
107 audio_chunk_channel_.pop_front();
\r
110 auto write = std::move(video_frame_channel_.front());
\r
111 write->audio_data() = std::move(audio_data);
\r
112 auto transform = draw_frame(write);
\r
113 video_frame_channel_.pop_front();
\r
115 // TODO: Make generic for all formats and modes.
\r
116 if(input_.get_video_codec_context()->codec_id == CODEC_ID_DVVIDEO) // Move up one field
\r
117 transform.translate(0.0f, 1.0/static_cast<double>(format_desc_.height));
\r
119 ouput_channel_.push(std::move(transform));
\r
122 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
123 return last_frame_;
\r
126 auto result = last_frame_;
\r
127 if(!ouput_channel_.empty())
\r
129 result = std::move(ouput_channel_.front());
\r
130 last_frame_ = draw_frame(result);
\r
131 last_frame_->audio_volume(0.0); // last_frame should not have audio
\r
132 ouput_channel_.pop();
\r
134 else if(input_.is_eof())
\r
135 return draw_frame::eof();
\r
140 std::wstring print() const
\r
142 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";
\r
146 audio_decoder audio_decoder_;
\r
147 video_decoder video_decoder_;
\r
149 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
150 std::deque<std::vector<short>> audio_chunk_channel_;
\r
152 std::queue<safe_ptr<draw_frame>> ouput_channel_;
\r
154 const std::wstring filename_;
\r
156 safe_ptr<draw_frame> last_frame_;
\r
158 video_format_desc format_desc_;
\r
161 class ffmpeg_producer : public frame_producer
\r
164 ffmpeg_producer(const std::wstring& filename, const std::vector<std::wstring>& params) : impl_(new ffmpeg_producer_impl(filename, params)){}
\r
165 ffmpeg_producer(ffmpeg_producer&& other) : impl_(std::move(other.impl_)){}
\r
166 virtual safe_ptr<draw_frame> receive(){return impl_->receive();}
\r
167 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor){impl_->initialize(frame_processor);}
\r
168 virtual std::wstring print() const{return impl_->print();}
\r
170 std::shared_ptr<ffmpeg_producer_impl> impl_;
\r
173 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
175 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
176 std::wstring filename = params[0];
\r
178 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
180 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
183 if(ext == extensions.end())
\r
184 return frame_producer::empty();
\r
186 static boost::once_flag av_register_all_flag = BOOST_ONCE_INIT;
\r
187 boost::call_once(av_register_all, av_register_all_flag);
\r
189 static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;
\r
190 boost::call_once(avcodec_init, avcodec_init_flag);
\r
192 return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);
\r