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_transformer.h"
\r
30 #include "../../format/video_format.h"
\r
31 #include "../../processor/transform_frame.h"
\r
32 #include "../../processor/draw_frame.h"
\r
33 #include "../../../common/utility/scope_exit.h"
\r
34 #include "../../server.h"
\r
36 #include <tbb/mutex.h>
\r
37 #include <tbb/parallel_invoke.h>
\r
38 #include <tbb/task_group.h>
\r
40 #include <boost/algorithm/string/case_conv.hpp>
\r
41 #include <boost/lexical_cast.hpp>
\r
42 #include <boost/thread.hpp>
\r
43 #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), underrun_count_(0), last_frame_(transform_frame(draw_frame::empty()))
\r
54 if(!boost::filesystem::exists(filename))
\r
55 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));
\r
57 static boost::once_flag av_register_all_flag = BOOST_ONCE_INIT;
\r
58 boost::call_once(av_register_all, av_register_all_flag);
\r
60 static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;
\r
61 boost::call_once(avcodec_init, avcodec_init_flag);
\r
63 input_.reset(new input());
\r
64 input_->set_loop(std::find(params.begin(), params.end(), L"LOOP") != params.end());
\r
65 input_->load(narrow(filename_));
\r
66 video_decoder_.reset(new video_decoder(input_->get_video_codec_context().get()));
\r
67 video_transformer_.reset(new video_transformer(input_->get_video_codec_context().get()));
\r
68 audio_decoder_.reset(new audio_decoder(input_->get_audio_codec_context().get()));
\r
69 has_audio_ = input_->get_audio_codec_context() != nullptr;
\r
71 auto seek = std::find(params.begin(), params.end(), L"SEEK");
\r
72 if(seek != params.end() && ++seek != params.end())
\r
74 if(!input_->seek(boost::lexical_cast<unsigned long long>(*seek)))
\r
75 CASPAR_LOG(warning) << "Failed to seek file: " << filename_ << "to frame" << *seek;
\r
79 void initialize(const safe_ptr<frame_processor_device>& frame_processor)
\r
81 format_desc_ = frame_processor->get_video_format_desc();
\r
82 video_transformer_->initialize(frame_processor);
\r
85 safe_ptr<draw_frame> receive()
\r
87 while(ouput_channel_.empty() && !input_->is_eof())
\r
89 auto video_packet = input_->get_video_packet();
\r
90 auto audio_packet = input_->get_audio_packet();
\r
91 tbb::parallel_invoke(
\r
93 { // Video Decoding and Scaling
\r
94 if(!video_packet.empty())
\r
96 auto decoded_frame = video_decoder_->execute(video_packet);
\r
97 auto frame = video_transformer_->execute(decoded_frame);
\r
98 video_frame_channel_.push_back(std::move(frame));
\r
102 { // Audio Decoding
\r
103 if(!audio_packet.empty())
\r
105 auto chunks = audio_decoder_->execute(audio_packet);
\r
106 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());
\r
110 while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !has_audio_))
\r
112 std::vector<short> audio_data;
\r
115 audio_data = std::move(audio_chunk_channel_.front());
\r
116 audio_chunk_channel_.pop_front();
\r
119 auto write = std::move(video_frame_channel_.front());
\r
120 write->audio_data() = std::move(audio_data);
\r
121 auto transform = transform_frame(write);
\r
122 video_frame_channel_.pop_front();
\r
124 // TODO: Make generic for all formats and modes.
\r
125 if(input_->get_video_codec_context()->codec_id == CODEC_ID_DVVIDEO) // Move up one field
\r
126 transform.translate(0.0f, 1.0/static_cast<double>(format_desc_.height));
\r
128 ouput_channel_.push(std::move(transform));
\r
131 if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())
\r
133 if(underrun_count_++ == 0)
\r
134 CASPAR_LOG(warning) << "### File read underflow has STARTED.";
\r
136 return last_frame_;
\r
138 else if(underrun_count_ > 0)
\r
140 CASPAR_LOG(trace) << "### File Read Underrun has ENDED with " << underrun_count_ << " ticks.";
\r
141 underrun_count_ = 0;
\r
145 auto result = last_frame_;
\r
146 if(!ouput_channel_.empty())
\r
148 result = std::move(ouput_channel_.front());
\r
149 last_frame_ = transform_frame(result);
\r
150 last_frame_->audio_volume(0.0); // last_frame should not have audio
\r
151 ouput_channel_.pop();
\r
153 else if(input_->is_eof())
\r
154 return draw_frame::eof();
\r
159 std::wstring print() const
\r
161 return L"ffmpeg_producer. filename " + filename_;
\r
166 input_uptr input_;
\r
168 video_decoder_uptr video_decoder_;
\r
169 video_transformer_uptr video_transformer_;
\r
170 std::deque<safe_ptr<write_frame>> video_frame_channel_;
\r
172 audio_decoder_ptr audio_decoder_;
\r
173 std::deque<std::vector<short>> audio_chunk_channel_;
\r
175 std::queue<safe_ptr<transform_frame>> ouput_channel_;
\r
177 std::wstring filename_;
\r
179 long underrun_count_;
\r
181 safe_ptr<transform_frame> last_frame_;
\r
183 video_format_desc format_desc_;
\r
186 class ffmpeg_producer : public frame_producer
\r
189 ffmpeg_producer(const std::wstring& filename, const std::vector<std::wstring>& params) : impl_(new ffmpeg_producer_impl(filename, params)){}
\r
190 ffmpeg_producer(ffmpeg_producer&& other) : impl_(std::move(other.impl_)){}
\r
191 virtual safe_ptr<draw_frame> receive(){return impl_->receive();}
\r
192 virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor){impl_->initialize(frame_processor);}
\r
193 virtual std::wstring print() const{return impl_->print();}
\r
195 std::shared_ptr<ffmpeg_producer_impl> impl_;
\r
198 safe_ptr<frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)
\r
200 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
201 std::wstring filename = server::media_folder() + L"\\" + params[0];
\r
203 auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
\r
205 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));
\r
208 if(ext == extensions.end())
\r
209 return frame_producer::empty();
\r
211 return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);
\r