]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0.0.2: Simplified conversion between shared_ptr and safe_ptr through a make_safe...
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "../stdafx.h"\r
21 \r
22 #include "ffmpeg_producer.h"\r
23 \r
24 #include "input.h"\r
25 #include "audio/audio_decoder.h"\r
26 #include "video/video_decoder.h"\r
27 \r
28 #include <common/utility/timer.h>\r
29 #include <common/diagnostics/graph.h>\r
30 \r
31 #include <core/producer/frame/basic_frame.h>\r
32 #include <core/producer/frame/write_frame.h>\r
33 #include <core/producer/frame/audio_transform.h>\r
34 #include <core/video_format.h>\r
35 \r
36 #include <common/env.h>\r
37 #include <common/utility/timer.h>\r
38 #include <common/utility/assert.h>\r
39 \r
40 #include <tbb/parallel_invoke.h>\r
41 \r
42 #include <deque>\r
43 #include <functional>\r
44 \r
45 namespace caspar {\r
46         \r
47 struct ffmpeg_producer : public core::frame_producer\r
48 {\r
49         const std::wstring                                      filename_;\r
50         const bool                                                      loop_;\r
51         \r
52         std::shared_ptr<diagnostics::graph>     graph_;\r
53         timer                                                           perf_timer_;\r
54                 \r
55         std::unique_ptr<audio_decoder>          audio_decoder_;\r
56         std::unique_ptr<video_decoder>          video_decoder_;\r
57 \r
58         std::deque<safe_ptr<core::write_frame>> video_frame_channel_;   \r
59         std::deque<std::vector<short>>                  audio_chunk_channel_;\r
60 \r
61         std::queue<safe_ptr<core::basic_frame>> ouput_channel_;\r
62                 \r
63         safe_ptr<core::basic_frame>                             last_frame_;\r
64         std::shared_ptr<core::frame_factory>    frame_factory_;\r
65 \r
66         std::unique_ptr<input>                          input_; \r
67 public:\r
68         explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, bool loop) \r
69                 : filename_(filename)\r
70                 , loop_(loop) \r
71                 , last_frame_(core::basic_frame(core::basic_frame::empty()))\r
72                 , frame_factory_(frame_factory)         \r
73         {\r
74                 graph_ = diagnostics::create_graph(boost::bind(&ffmpeg_producer::print, this)); \r
75                 graph_->add_guide("frame-time", 0.5);\r
76                 graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
77                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));           \r
78 \r
79                 input_.reset(new input(safe_ptr<diagnostics::graph>(graph_), filename_, loop_));\r
80                 video_decoder_.reset(input_->get_video_codec_context().get() ? new video_decoder(input_->get_video_codec_context().get(), frame_factory) : nullptr);\r
81                 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
82 \r
83                 double frame_time = 1.0f/input_->fps();\r
84                 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
85                 if(abs(frame_time - format_frame_time) > 0.0001)\r
86                         CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;\r
87         }\r
88                         \r
89         virtual safe_ptr<core::basic_frame> receive()\r
90         {\r
91                 perf_timer_.reset();\r
92 \r
93                 while(ouput_channel_.size() < 2 && !input_->is_eof())\r
94                 {       \r
95                         aligned_buffer video_packet;\r
96                         if(video_frame_channel_.size() < 3 && video_decoder_)   \r
97                                 video_packet = input_->get_video_packet();              \r
98                         \r
99                         aligned_buffer audio_packet;\r
100                         if(audio_chunk_channel_.size() < 3 && audio_decoder_)   \r
101                                 audio_packet = input_->get_audio_packet();      \r
102 \r
103                         if(video_packet.empty() && audio_packet.empty()) // Skip frame if lagging.                       \r
104                                 break;\r
105 \r
106                         tbb::parallel_invoke(\r
107                         [&]\r
108                         {\r
109                                 if(!video_packet.empty() && video_decoder_) // Video Decoding.\r
110                                 {\r
111                                         try\r
112                                         {\r
113                                                 auto frame = video_decoder_->execute(this, video_packet);\r
114                                                 video_frame_channel_.push_back(std::move(frame));\r
115                                         }\r
116                                         catch(...)\r
117                                         {\r
118                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
119                                                 video_decoder_.reset();\r
120                                                 CASPAR_LOG(warning) << print() << " removed video-stream.";\r
121                                         }\r
122                                 }\r
123                         }, \r
124                         [&] \r
125                         { \r
126                                 if(!audio_packet.empty() && audio_decoder_) // Audio Decoding.\r
127                                 {\r
128                                         try\r
129                                         {\r
130                                                 auto chunks = audio_decoder_->execute(audio_packet);\r
131                                                 audio_chunk_channel_.insert(audio_chunk_channel_.end(), chunks.begin(), chunks.end());\r
132                                         }\r
133                                         catch(...)\r
134                                         {\r
135                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
136                                                 audio_decoder_.reset();\r
137                                                 CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
138                                         }\r
139                                 }\r
140                         });\r
141 \r
142                         while((!video_frame_channel_.empty() || !video_decoder_) && (!audio_chunk_channel_.empty() || !audio_decoder_))\r
143                         {\r
144                                 std::shared_ptr<core::write_frame> frame;\r
145 \r
146                                 if(video_decoder_)\r
147                                 {\r
148                                         frame = video_frame_channel_.front();\r
149                                         video_frame_channel_.pop_front();\r
150                                 }\r
151 \r
152                                 if(audio_decoder_) \r
153                                 {\r
154                                         if(!frame) // If there is no video create a dummy frame.\r
155                                         {\r
156                                                 frame = frame_factory_->create_frame(this, 1, 1);\r
157                                                 std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
158                                         }\r
159                                         \r
160                                         frame->audio_data() = std::move(audio_chunk_channel_.front());\r
161                                         audio_chunk_channel_.pop_front();\r
162                                 }\r
163                                                         \r
164                                 ouput_channel_.push(make_safe(frame));                          \r
165                         }                               \r
166 \r
167                         if(ouput_channel_.empty() && video_packet.empty() && audio_packet.empty())                      \r
168                                 return last_frame_;                     \r
169                 }\r
170                 \r
171                 graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()/frame_factory_->get_video_format_desc().interval*0.5));\r
172 \r
173                 auto result = last_frame_;\r
174                 if(!ouput_channel_.empty())             \r
175                         result = get_frame(); // TODO: Support 50p              \r
176                 else if(!input_->is_running())\r
177                         result = core::basic_frame::eof();\r
178                 else\r
179                         graph_->add_tag("underflow");\r
180                 \r
181                 return result;\r
182         }\r
183 \r
184         core::basic_frame get_frame()\r
185         {\r
186                 CASPAR_ASSERT(!ouput_channel_.empty());\r
187                 auto result = std::move(ouput_channel_.front());\r
188                 last_frame_ = core::basic_frame(result);\r
189                 last_frame_->get_audio_transform().set_gain(0.0); // last_frame should not have audio\r
190                 ouput_channel_.pop();\r
191                 return result;\r
192         }\r
193 \r
194         virtual std::wstring print() const\r
195         {\r
196                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
197         }\r
198 };\r
199 \r
200 safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
201 {                       \r
202         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
203                 (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
204         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
205         \r
206         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool\r
207                 {                                       \r
208                         return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
209                 });\r
210 \r
211         if(ext == extensions.end())\r
212                 return core::frame_producer::empty();\r
213 \r
214         std::wstring path = filename + L"." + *ext;\r
215         bool loop = std::find(params.begin(), params.end(), L"LOOP") != params.end();\r
216         \r
217         return make_safe<ffmpeg_producer>(frame_factory, path, loop);\r
218 }\r
219 \r
220 }