]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0: ffmpeg_producer: Re-added and fixed filter capabilities.
[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/assert.h>\r
29 #include <common/utility/timer.h>\r
30 #include <common/diagnostics/graph.h>\r
31 \r
32 #include <core/producer/frame/basic_frame.h>\r
33 #include <core/mixer/write_frame.h>\r
34 #include <core/producer/frame/audio_transform.h>\r
35 #include <core/video_format.h>\r
36 \r
37 #include <common/env.h>\r
38 \r
39 #include <tbb/parallel_invoke.h>\r
40 \r
41 #include <boost/timer.hpp>\r
42 #include <boost/range/algorithm.hpp>\r
43 #include <boost/range/algorithm_ext.hpp>\r
44 \r
45 #include <deque>\r
46 \r
47 namespace caspar {\r
48         \r
49 struct ffmpeg_producer : public core::frame_producer\r
50 {\r
51         const std::wstring                                              filename_;\r
52         \r
53         const safe_ptr<diagnostics::graph>              graph_;\r
54         boost::timer                                                    frame_timer_;\r
55                                         \r
56         const safe_ptr<core::frame_factory>             frame_factory_;\r
57 \r
58         input                                                                   input_; \r
59         std::unique_ptr<video_decoder>                  video_decoder_;\r
60         std::unique_ptr<audio_decoder>                  audio_decoder_;\r
61 \r
62         std::deque<std::pair<int, std::vector<int16_t>>> audio_chunks_;\r
63         std::deque<std::pair<int, safe_ptr<core::write_frame>>> video_frames_;\r
64 public:\r
65         explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, const std::wstring& filter_str, bool loop, int start, int length) \r
66                 : filename_(filename)\r
67                 , graph_(diagnostics::create_graph(narrow(print())))\r
68                 , frame_factory_(frame_factory)         \r
69                 , input_(safe_ptr<diagnostics::graph>(graph_), filename_, loop, start, length)\r
70         {\r
71                 graph_->add_guide("frame-time", 0.5);\r
72                 graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
73                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));           \r
74                 \r
75                 double frame_time = 1.0f/input_.fps();\r
76                 double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
77                 if(abs(frame_time - format_frame_time) > 0.0001 && abs(frame_time - format_frame_time/2) > 0.0001)\r
78                         CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;\r
79                 \r
80                 video_decoder_.reset(input_.get_video_codec_context() ? \r
81                         new video_decoder(input_, frame_factory, narrow(filter_str)) : nullptr);\r
82                         \r
83                 audio_decoder_.reset(input_.get_audio_codec_context() ? \r
84                         new audio_decoder(input_, frame_factory->get_video_format_desc()) : nullptr);           \r
85                                         \r
86                 // Fill buffers.\r
87                 for(size_t n = 0; n < 2; ++n)\r
88                         decode_packets();\r
89         }\r
90 \r
91         virtual safe_ptr<core::basic_frame> receive()\r
92         {\r
93                 frame_timer_.restart();\r
94 \r
95                 auto result = decode_frame();\r
96                                 \r
97                 graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5));\r
98                                         \r
99                 return result;\r
100         }\r
101         \r
102         virtual std::wstring print() const\r
103         {\r
104                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
105         }\r
106 \r
107         void decode_packets()\r
108         {\r
109                 tbb::parallel_invoke\r
110                 (\r
111                         [&]\r
112                         {\r
113                                 if(video_decoder_ && video_frames_.size() < 3)\r
114                                         boost::range::push_back(video_frames_, video_decoder_->receive());              \r
115                         }, \r
116                         [&]\r
117                         {\r
118                                 if(audio_decoder_ && audio_chunks_.size() < 3)\r
119                                         boost::range::push_back(audio_chunks_, audio_decoder_->receive());                              \r
120                         }\r
121                 );\r
122                 \r
123                 // If video is on first frame, sync with audio\r
124                 if(audio_decoder_ && video_decoder_ && !video_frames_.empty() && !audio_chunks_.empty() &&\r
125                    video_frames_.front().first == 0 && audio_chunks_.front().first != 0)\r
126                 {\r
127                         audio_decoder_->restart(); // Notify decoder to wait for eof which was sent with video eof.\r
128                         audio_chunks_ = audio_decoder_->receive();              \r
129                 }\r
130                 \r
131                 CASPAR_ASSERT(!(video_decoder_ && audio_decoder_ && !video_frames_.empty() && !audio_chunks_.empty()) ||\r
132                                       video_frames_.front().first == audio_chunks_.front().first);\r
133         }\r
134 \r
135         safe_ptr<core::basic_frame> get_video_frame(std::vector<int16_t>&& audio_chunk)\r
136         {\r
137                 auto frame = std::move(video_frames_.front().second);   \r
138                 auto frame_number = video_frames_.front().first;\r
139                 video_frames_.pop_front();\r
140                                 \r
141                 frame->audio_data() = std::move(audio_chunk);\r
142                 if(frame->audio_data().empty())\r
143                         frame->get_audio_transform().set_has_audio(false);      \r
144 \r
145                 if(!video_frames_.empty()) // interlace if we have double frames\r
146                 {\r
147                         if(video_frames_.front().first == frame_number)\r
148                         {\r
149                                 auto frame2 = std::move(video_frames_.front().second);  \r
150                                 video_frames_.pop_front();\r
151 \r
152                                 return core::basic_frame::interlace(frame, frame2, frame_factory_->get_video_format_desc().mode);\r
153                         }\r
154                 }\r
155 \r
156                 return frame;\r
157         }\r
158 \r
159         safe_ptr<core::basic_frame> decode_frame()\r
160         {\r
161                 decode_packets();\r
162 \r
163                 if(video_decoder_ && audio_decoder_ && !video_frames_.empty() && !audio_chunks_.empty())\r
164                 {\r
165                         auto audio_chunk = std::move(audio_chunks_.front().second);\r
166                         audio_chunks_.pop_front();\r
167                                                 \r
168                         return get_video_frame(std::move(audio_chunk));\r
169                 }\r
170                 else if(video_decoder_ && !audio_decoder_ && !video_frames_.empty())\r
171                 {                                               \r
172                         return get_video_frame(std::vector<int16_t>());\r
173                 }\r
174                 else if(audio_decoder_ && !video_decoder_ && !audio_chunks_.empty())\r
175                 {\r
176                         auto frame = frame_factory_->create_frame(this, 1, 1);\r
177                         std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
178                                 \r
179                         frame->audio_data() = std::move(audio_chunks_.front().second);\r
180                         audio_chunks_.pop_front();\r
181 \r
182                         return frame;\r
183                 }\r
184                 else if(!input_.is_running() || (!video_decoder_ && !audio_decoder_))\r
185                 {\r
186                         return core::basic_frame::eof();\r
187                 }\r
188                 else\r
189                 {\r
190                         graph_->add_tag("underflow");\r
191                         return core::basic_frame::late();\r
192                 }\r
193         }\r
194 };\r
195 \r
196 safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
197 {               \r
198         static const std::vector<std::wstring> extensions = boost::assign::list_of\r
199                 (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"wmv")(L"wma")(L"ogg")(L"divx")(L"xvid")(L"wav")(L"mp3")(L"m2v");\r
200         std::wstring filename = env::media_folder() + L"\\" + params[0];\r
201         \r
202         auto ext = boost::find_if(extensions, [&](const std::wstring& ex)\r
203         {                                       \r
204                 return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename + L"." + ex));\r
205         });\r
206 \r
207         if(ext == extensions.end())\r
208                 return core::frame_producer::empty();\r
209 \r
210         std::wstring path = filename + L"." + *ext;\r
211         bool loop = boost::find(params, L"LOOP") != params.end();\r
212 \r
213         int start = -1;\r
214         int length = -1;\r
215         \r
216         auto seek_it = std::find(params.begin(), params.end(), L"SEEK");\r
217         if(seek_it != params.end())\r
218         {\r
219                 if(++seek_it != params.end())\r
220                         start = boost::lexical_cast<int>(*seek_it);\r
221         }\r
222         \r
223         std::wstring filter_str = L"";\r
224 \r
225         auto filter_it = std::find(params.begin(), params.end(), L"FILTER");\r
226         if(filter_it != params.end())\r
227         {\r
228                 if(++filter_it != params.end())\r
229                         filter_str = *filter_it;\r
230         }\r
231 \r
232         return make_safe<ffmpeg_producer>(frame_factory, path, filter_str, loop, start, length);\r
233 }\r
234 \r
235 }