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