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