]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0.0.2: Renamed draw_frame to basic_frame.
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
1 #include "../stdafx.h"\r
2 \r
3 #include "ffmpeg_producer.h"\r
4 \r
5 #include "input.h"\r
6 #include "audio/audio_decoder.h"\r
7 #include "video/video_decoder.h"\r
8 \r
9 #include <core/video_format.h>\r
10 #include <common/utility/timer.h>\r
11 #include <common/diagnostics/graph.h>\r
12 #include <mixer/frame/basic_frame.h>\r
13 #include <mixer/audio/audio_transform.h>\r
14 \r
15 #include <common/env.h>\r
16 #include <common/utility/timer.h>\r
17 \r
18 #include <tbb/parallel_invoke.h>\r
19 \r
20 #include <deque>\r
21 #include <functional>\r
22 \r
23 namespace caspar {\r
24         \r
25 struct ffmpeg_producer : public core::frame_producer\r
26 {\r
27         const std::wstring                                      filename_;\r
28         const bool                                                      loop_;\r
29         printer                                                         parent_printer_;\r
30         \r
31         std::shared_ptr<diagnostics::graph>     graph_;\r
32         timer                                                           perf_timer_;\r
33                 \r
34         std::unique_ptr<audio_decoder>          audio_decoder_;\r
35         std::unique_ptr<video_decoder>          video_decoder_;\r
36 \r
37         std::deque<safe_ptr<core::write_frame>> video_frame_channel_;   \r
38         std::deque<std::vector<short>>                  audio_chunk_channel_;\r
39 \r
40         std::queue<safe_ptr<core::basic_frame>> ouput_channel_;\r
41                 \r
42         safe_ptr<core::basic_frame>                             last_frame_;\r
43         std::shared_ptr<core::frame_factory>    frame_factory_;\r
44 \r
45         std::unique_ptr<input>                          input_; \r
46 public:\r
47         explicit ffmpeg_producer(const std::wstring& filename, bool loop) \r
48                 : filename_(filename)\r
49                 , loop_(loop) \r
50                 , last_frame_(core::basic_frame(core::basic_frame::empty()))\r
51                 \r
52         {\r
53                 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this)); \r
54                 graph_->add_guide("frame-time", 0.5);\r
55                 graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
56         }\r
57         \r
58         virtual void initialize(const safe_ptr<core::frame_factory>& frame_factory)\r
59         {\r
60                 frame_factory_ = frame_factory;\r
61                 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, std::bind(&ffmpeg_producer::print, this)));\r
62                 video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);\r
63                 audio_decoder_.reset(input_->get_audio_codec_context().get() ? new audio_decoder(input_->get_audio_codec_context().get(), frame_factory->get_video_format_desc().fps) : nullptr);\r
64         }\r
65                 \r
66         virtual void set_parent_printer(const printer& parent_printer) \r
67         {\r
68                 parent_printer_ = parent_printer;\r
69         }\r
70 \r
71         virtual safe_ptr<core::basic_frame> receive()\r
72         {\r
73                 perf_timer_.reset();\r
74 \r
75                 while(ouput_channel_.empty() && !input_->is_eof())\r
76                 {       \r
77                         aligned_buffer video_packet;\r
78                         if(video_frame_channel_.size() < 3 && video_decoder_)   \r
79                                 video_packet = input_->get_video_packet();              \r
80                         \r
81                         aligned_buffer audio_packet;\r
82                         if(audio_chunk_channel_.size() < 3 && audio_decoder_)   \r
83                                 audio_packet = input_->get_audio_packet();              \r
84 \r
85                         tbb::parallel_invoke(\r
86                         [&]\r
87                         { // Video Decoding and Scaling\r
88                                 if(!video_packet.empty() && video_decoder_)\r
89                                 {\r
90                                         try\r
91                                         {\r
92                                                 auto frame = video_decoder_->execute(video_packet);\r
93                                                 frame->tag(reinterpret_cast<int>(this));\r
94                                                 video_frame_channel_.push_back(std::move(frame));\r
95                                         }\r
96                                         catch(...)\r
97                                         {\r
98                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
99                                                 video_decoder_.reset();\r
100                                                 CASPAR_LOG(warning) << print() << " removed video-stream.";\r
101                                         }\r
102                                 }\r
103                         }, \r
104                         [&] \r
105                         { // Audio Decoding\r
106                                 if(!audio_packet.empty() && audio_decoder_)\r
107                                 {\r
108                                         try\r
109                                         {\r
110                                                 auto chunks = audio_decoder_->execute(audio_packet);\r
111                                                 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
112                                         }\r
113                                         catch(...)\r
114                                         {\r
115                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
116                                                 audio_decoder_.reset();\r
117                                                 CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
118                                         }\r
119                                 }\r
120                         });\r
121 \r
122                         while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
123                         {\r
124                                 std::shared_ptr<core::write_frame> frame;\r
125 \r
126                                 if(video_decoder_)\r
127                                 {\r
128                                         frame = video_frame_channel_.front();\r
129                                         video_frame_channel_.pop_front();\r
130                                 }\r
131 \r
132                                 if(audio_decoder_) \r
133                                 {\r
134                                         if(!frame)\r
135                                         {\r
136                                                 frame = frame_factory_->create_frame(1, 1);\r
137                                                 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
138                                         }\r
139                                         \r
140                                         frame->audio_data() = std::move(audio_chunk_channel_.front());\r
141                                         audio_chunk_channel_.pop_front();\r
142                                 }\r
143                                                         \r
144                                 ouput_channel_.push(safe_ptr<core::write_frame>(frame));                                \r
145                         }                               \r
146 \r
147                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
148                                 return last_frame_;                     \r
149                 }\r
150                 \r
151                 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));\r
152 \r
153                 auto result = last_frame_;\r
154                 if(!ouput_channel_.empty())\r
155                 {\r
156                         result = std::move(ouput_channel_.front());\r
157                         last_frame_ = core::basic_frame(result);\r
158                         last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio\r
159                         ouput_channel_.pop();\r
160                 }\r
161                 else if(input_->is_eof())\r
162                         return core::basic_frame::eof();\r
163 \r
164                 return result;\r
165         }\r
166 \r
167         virtual std::wstring print() const\r
168         {\r
169                 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
170         }\r
171 };\r
172 \r
173 safe_ptr<core::frame_producer> create_ffmpeg_producer(const std::vector<std::wstring>& params)\r
174 {                       \r
175         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
176                 (L"mpg")(L"mpeg")(L"avi")(L"mov")(L"qt")(L"webm")(L"dv")(L"mp4")(L"f4v")(L"flv")(L"mkv")(L"mka")(L"wmw")(L"wma")(L"ogg")(L"divx")(L"wav")(L"mp3");\r
177         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
178         \r
179         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
180                 {                                       \r
181                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
182                 });\r
183 \r
184         if(ext == extensions.end())\r
185                 return core::frame_producer::empty();\r
186 \r
187         std::wstring path = filename + L"." + *ext;\r
188         bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();\r
189         \r
190         return make_safe<ffmpeg_producer>(path, loop);\r
191 }\r
192 \r
193 }