]> git.sesse.net Git - casparcg/blob - core/producer/ffmpeg/ffmpeg_producer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / ffmpeg / ffmpeg_producer.cpp
1 #include "../../stdafx.h"\r
2 \r
3 #include "ffmpeg_producer.h"\r
4 \r
5 #if defined(_MSC_VER)\r
6 #pragma warning (push)\r
7 #pragma warning (disable : 4244)\r
8 #endif\r
9 \r
10 extern "C" \r
11 {\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
18 }\r
19 \r
20 #if defined(_MSC_VER)\r
21 #pragma warning (pop)\r
22 #endif\r
23 \r
24 #include "input.h"\r
25 \r
26 #include "audio/audio_decoder.h"\r
27 #include "video/video_decoder.h"\r
28 #include "video/video_transformer.h"\r
29 \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
35 \r
36 #include <tbb/mutex.h>\r
37 #include <tbb/parallel_invoke.h>\r
38 #include <tbb/task_group.h>\r
39 \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
44 \r
45 using namespace boost::assign;\r
46 \r
47 namespace caspar { namespace core { namespace ffmpeg{\r
48         \r
49 struct ffmpeg_producer_impl\r
50 {\r
51 public:\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
53         {\r
54                 if(!boost::filesystem::exists(filename))\r
55                         BOOST_THROW_EXCEPTION(file_not_found() <<  boost::errinfo_file_name(narrow(filename)));\r
56                 \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
59                 \r
60                 static boost::once_flag avcodec_init_flag = BOOST_ONCE_INIT;\r
61                 boost::call_once(avcodec_init, avcodec_init_flag);      \r
62                                 \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
70 \r
71                 auto seek = std::find(params.begin(), params.end(), L"SEEK");\r
72                 if(seek != params.end() && ++seek != params.end())\r
73                 {\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
76                 }\r
77         }\r
78                 \r
79         void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
80         {\r
81                 format_desc_ = frame_processor->get_video_format_desc();\r
82                 video_transformer_->initialize(frame_processor);\r
83         }\r
84                 \r
85         safe_ptr<draw_frame> receive()\r
86         {\r
87                 while(ouput_channel_.empty() && !input_->is_eof())\r
88                 {       \r
89                         auto video_packet = input_->get_video_packet();         \r
90                         auto audio_packet = input_->get_audio_packet();         \r
91                         tbb::parallel_invoke(\r
92                         [&]\r
93                         { // Video Decoding and Scaling\r
94                                 if(!video_packet.empty())\r
95                                 {\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
99                                 }\r
100                         }, \r
101                         [&] \r
102                         { // Audio Decoding\r
103                                 if(!audio_packet.empty())\r
104                                 {\r
105                                         auto chunks = audio_decoder_->execute(audio_packet);\r
106                                         audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
107                                 }\r
108                         });\r
109 \r
110                         while(!video_frame_channel_.empty() && (!audio_chunk_channel_.empty() || !has_audio_))\r
111                         {\r
112                                 std::vector<short> audio_data;\r
113                                 if(has_audio_) \r
114                                 {\r
115                                         audio_data = std::move(audio_chunk_channel_.front());\r
116                                         audio_chunk_channel_.pop_front();\r
117                                 }\r
118                                                         \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
123                 \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
127                                 \r
128                                 ouput_channel_.push(std::move(transform));\r
129                         }                               \r
130 \r
131                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())\r
132                         {\r
133                                 if(underrun_count_++ == 0)\r
134                                         CASPAR_LOG(warning) << "### File read underflow has STARTED.";\r
135 \r
136                                 return last_frame_;\r
137                         }\r
138                         else if(underrun_count_ > 0)\r
139                         {\r
140                                 CASPAR_LOG(trace) << "### File Read Underrun has ENDED with " << underrun_count_ << " ticks.";\r
141                                 underrun_count_ = 0;\r
142                         }\r
143                 }\r
144 \r
145                 auto result = last_frame_;\r
146                 if(!ouput_channel_.empty())\r
147                 {\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
152                 }\r
153                 else if(input_->is_eof())\r
154                         return draw_frame::eof();\r
155 \r
156                 return result;\r
157         }\r
158 \r
159         std::wstring print() const\r
160         {\r
161                 return L"ffmpeg_producer. filename " + filename_;\r
162         }\r
163                         \r
164         bool has_audio_;\r
165 \r
166         input_uptr                                                              input_;         \r
167 \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
171         \r
172         audio_decoder_ptr                                               audio_decoder_;\r
173         std::deque<std::vector<short>>                  audio_chunk_channel_;\r
174 \r
175         std::queue<safe_ptr<transform_frame>>   ouput_channel_;\r
176         \r
177         std::wstring                                                    filename_;\r
178 \r
179         long                                                                    underrun_count_;\r
180 \r
181         safe_ptr<transform_frame>                               last_frame_;\r
182 \r
183         video_format_desc                                               format_desc_;\r
184 };\r
185 \r
186 class ffmpeg_producer : public frame_producer\r
187 {\r
188 public:\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
194 private:\r
195         std::shared_ptr<ffmpeg_producer_impl> impl_;\r
196 };\r
197 \r
198 safe_ptr<frame_producer> create_ffmpeg_producer(const  std::vector<std::wstring>& params)\r
199 {       \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
202         \r
203         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
204                 {                                       \r
205                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
206                 });\r
207 \r
208         if(ext == extensions.end())\r
209                 return frame_producer::empty();\r
210 \r
211         return make_safe<ffmpeg_producer>(filename + L"." + *ext, params);\r
212 }\r
213 \r
214 }}}