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