]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/producer/ffmpeg_producer.cpp
e5b03432121fe83210e2220da6089f57b3ea888f
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../stdafx.h"\r
23 \r
24 #include "ffmpeg_producer.h"\r
25 \r
26 #include "../ffmpeg_error.h"\r
27 #include "../ffmpeg.h"\r
28 \r
29 #include "muxer/frame_muxer.h"\r
30 #include "input/input.h"\r
31 #include "util/util.h"\r
32 #include "audio/audio_decoder.h"\r
33 #include "video/video_decoder.h"\r
34 \r
35 #include <common/env.h>\r
36 #include <common/utility/assert.h>\r
37 #include <common/utility/param.h>\r
38 #include <common/diagnostics/graph.h>\r
39 \r
40 #include <core/video_format.h>\r
41 #include <core/producer/frame_producer.h>\r
42 #include <core/producer/frame/frame_factory.h>\r
43 #include <core/producer/frame/basic_frame.h>\r
44 #include <core/producer/frame/frame_transform.h>\r
45 \r
46 #include <boost/algorithm/string.hpp>\r
47 #include <boost/assign.hpp>\r
48 #include <boost/timer.hpp>\r
49 #include <boost/foreach.hpp>\r
50 #include <boost/filesystem.hpp>\r
51 #include <boost/range/algorithm/find_if.hpp>\r
52 #include <boost/range/algorithm/find.hpp>\r
53 #include <boost/regex.hpp>\r
54 \r
55 #include <tbb/parallel_invoke.h>\r
56 \r
57 #include <limits>\r
58 #include <memory>\r
59 #include <queue>\r
60 \r
61 namespace caspar { namespace ffmpeg {\r
62                                 \r
63 struct ffmpeg_producer : public core::frame_producer\r
64 {\r
65         const std::wstring                                                                                      filename_;\r
66         \r
67         const safe_ptr<diagnostics::graph>                                                      graph_;\r
68         boost::timer                                                                                            frame_timer_;\r
69                                         \r
70         const safe_ptr<core::frame_factory>                                                     frame_factory_;\r
71         const core::video_format_desc                                                           format_desc_;\r
72 \r
73         std::shared_ptr<void>                                                                           initial_logger_disabler_;\r
74 \r
75         input                                                                                                           input_; \r
76         std::unique_ptr<video_decoder>                                                          video_decoder_;\r
77         std::unique_ptr<audio_decoder>                                                          audio_decoder_; \r
78         std::unique_ptr<frame_muxer>                                                            muxer_;\r
79 \r
80         const double                                                                                            fps_;\r
81         const uint32_t                                                                                          start_;\r
82         const uint32_t                                                                                          length_;\r
83         const bool                                                                                                      thumbnail_mode_;\r
84 \r
85         safe_ptr<core::basic_frame>                                                                     last_frame_;\r
86         \r
87         std::queue<std::pair<safe_ptr<core::basic_frame>, size_t>>      frame_buffer_;\r
88 \r
89         int64_t                                                                                                         frame_number_;\r
90         uint32_t                                                                                                        file_frame_number_;\r
91         \r
92 public:\r
93         explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, const std::wstring& filter, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode)\r
94                 : filename_(filename)\r
95                 , frame_factory_(frame_factory)         \r
96                 , format_desc_(frame_factory->get_video_format_desc())\r
97                 , initial_logger_disabler_(temporary_disable_logging_for_thread(thumbnail_mode))\r
98                 , input_(graph_, filename_, loop, start, length, thumbnail_mode)\r
99                 , fps_(read_fps(*input_.context(), format_desc_.fps))\r
100                 , start_(start)\r
101                 , length_(length)\r
102                 , thumbnail_mode_(thumbnail_mode)\r
103                 , last_frame_(core::basic_frame::empty())\r
104                 , frame_number_(0)\r
105         {\r
106                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
107                 graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));   \r
108                 diagnostics::register_graph(graph_);\r
109         \r
110                 try\r
111                 {\r
112                         video_decoder_.reset(new video_decoder(input_.context()));\r
113                         if (!thumbnail_mode_)\r
114                                 CASPAR_LOG(info) << print() << L" " << video_decoder_->print();\r
115                 }\r
116                 catch(averror_stream_not_found&)\r
117                 {\r
118                         //CASPAR_LOG(warning) << print() << " No video-stream found. Running without video.";   \r
119                 }\r
120                 catch(...)\r
121                 {\r
122                         if (!thumbnail_mode_)\r
123                         {\r
124                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
125                                 CASPAR_LOG(warning) << print() << "Failed to open video-stream. Running without video.";        \r
126                         }\r
127                 }\r
128 \r
129                 if (!thumbnail_mode_)\r
130                 {\r
131                         try\r
132                         {\r
133                                 audio_decoder_.reset(new audio_decoder(input_.context(), frame_factory->get_video_format_desc()));\r
134                                 CASPAR_LOG(info) << print() << L" " << audio_decoder_->print();\r
135                         }\r
136                         catch(averror_stream_not_found&)\r
137                         {\r
138                                 //CASPAR_LOG(warning) << print() << " No audio-stream found. Running without audio.";   \r
139                         }\r
140                         catch(...)\r
141                         {\r
142                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
143                                 CASPAR_LOG(warning) << print() << " Failed to open audio-stream. Running without audio.";               \r
144                         }\r
145                 }\r
146 \r
147                 if(!video_decoder_ && !audio_decoder_)\r
148                         BOOST_THROW_EXCEPTION(averror_stream_not_found() << msg_info("No streams found"));\r
149 \r
150                 muxer_.reset(new frame_muxer(fps_, frame_factory, thumbnail_mode_, filter));\r
151         }\r
152 \r
153         // frame_producer\r
154         \r
155         virtual safe_ptr<core::basic_frame> receive(int hints) override\r
156         {\r
157                 return render_frame(hints).first;\r
158         }\r
159 \r
160         virtual safe_ptr<core::basic_frame> last_frame() const override\r
161         {\r
162                 return disable_audio(last_frame_);\r
163         }\r
164 \r
165         std::pair<safe_ptr<core::basic_frame>, uint32_t> render_frame(int hints)\r
166         {               \r
167                 frame_timer_.restart();\r
168                 auto disable_logging = temporary_disable_logging_for_thread(thumbnail_mode_);\r
169                                 \r
170                 for(int n = 0; n < 16 && frame_buffer_.size() < 2; ++n)\r
171                         try_decode_frame(hints);\r
172                 \r
173                 graph_->set_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
174                                 \r
175                 if(frame_buffer_.empty() && input_.eof())\r
176                         return std::make_pair(last_frame(), -1);\r
177 \r
178                 if(frame_buffer_.empty())\r
179                 {\r
180                         graph_->set_tag("underflow");   \r
181                         return std::make_pair(core::basic_frame::late(), -1);\r
182                 }\r
183                 \r
184                 auto frame = frame_buffer_.front(); \r
185                 frame_buffer_.pop();\r
186                 \r
187                 ++frame_number_;\r
188                 file_frame_number_ = frame.second;\r
189 \r
190                 graph_->set_text(print());\r
191 \r
192                 last_frame_ = frame.first;\r
193 \r
194                 return frame;\r
195         }\r
196 \r
197         safe_ptr<core::basic_frame> render_specific_frame(uint32_t file_position, int hints)\r
198         {\r
199                 // Some trial and error and undeterministic stuff here\r
200                 static const int NUM_RETRIES = 32;\r
201                 \r
202                 if (file_position > 0) // Assume frames are requested in sequential order,\r
203                                            // therefore no seeking should be necessary for the first frame.\r
204                 {\r
205                         input_.seek(file_position > 1 ? file_position - 2: file_position).get();\r
206                         boost::this_thread::sleep(boost::posix_time::milliseconds(40));\r
207                 }\r
208 \r
209                 for (int i = 0; i < NUM_RETRIES; ++i)\r
210                 {\r
211                         boost::this_thread::sleep(boost::posix_time::milliseconds(40));\r
212                 \r
213                         auto frame = render_frame(hints);\r
214 \r
215                         if (frame.second == std::numeric_limits<uint32_t>::max())\r
216                         {\r
217                                 // Retry\r
218                                 continue;\r
219                         }\r
220                         else if (frame.second == file_position + 1 || frame.second == file_position)\r
221                                 return frame.first;\r
222                         else if (frame.second > file_position + 1)\r
223                         {\r
224                                 CASPAR_LOG(trace) << print() << L" " << frame.second << L" received, wanted " << file_position + 1;\r
225                                 int64_t adjusted_seek = file_position - (frame.second - file_position + 1);\r
226 \r
227                                 if (adjusted_seek > 1 && file_position > 0)\r
228                                 {\r
229                                         CASPAR_LOG(trace) << print() << L" adjusting to " << adjusted_seek;\r
230                                         input_.seek(static_cast<uint32_t>(adjusted_seek) - 1).get();\r
231                                         boost::this_thread::sleep(boost::posix_time::milliseconds(40));\r
232                                 }\r
233                                 else\r
234                                         return frame.first;\r
235                         }\r
236                 }\r
237 \r
238                 CASPAR_LOG(trace) << print() << " Giving up finding frame at " << file_position;\r
239                 return core::basic_frame::empty();\r
240         }\r
241 \r
242         virtual safe_ptr<core::basic_frame> create_thumbnail_frame() override\r
243         {\r
244                 auto disable_logging = temporary_disable_logging_for_thread(thumbnail_mode_);\r
245                 auto total_frames = nb_frames();\r
246                 auto grid = env::properties().get(L"configuration.thumbnails.video-grid", 2);\r
247 \r
248                 if (grid < 1)\r
249                 {\r
250                         CASPAR_LOG(error) << L"configuration/thumbnails/video-grid cannot be less than 1";\r
251                         BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("configuration/thumbnails/video-grid cannot be less than 1"));\r
252                 }\r
253 \r
254                 if (grid == 1)\r
255                 {\r
256                         return render_specific_frame(total_frames / 2, 0/*DEINTERLACE_HINT*/);\r
257                 }\r
258 \r
259                 auto num_snapshots = grid * grid;\r
260 \r
261                 std::vector<safe_ptr<core::basic_frame>> frames;\r
262 \r
263                 for (int i = 0; i < num_snapshots; ++i)\r
264                 {\r
265                         int x = i % grid;\r
266                         int y = i / grid;\r
267                         int desired_frame;\r
268                         \r
269                         if (i == 0)\r
270                                 desired_frame = 0; // first\r
271                         else if (i == num_snapshots - 1)\r
272                                 desired_frame = total_frames - 1; // last\r
273                         else\r
274                                 // evenly distributed across the file.\r
275                                 desired_frame = total_frames * i / (num_snapshots - 1);\r
276 \r
277                         auto frame = render_specific_frame(desired_frame, 0/*DEINTERLACE_HINT*/);\r
278                         frame->get_frame_transform().fill_scale[0] = 1.0 / static_cast<double>(grid);\r
279                         frame->get_frame_transform().fill_scale[1] = 1.0 / static_cast<double>(grid);\r
280                         frame->get_frame_transform().fill_translation[0] = 1.0 / static_cast<double>(grid) * x;\r
281                         frame->get_frame_transform().fill_translation[1] = 1.0 / static_cast<double>(grid) * y;\r
282 \r
283                         frames.push_back(frame);\r
284                 }\r
285 \r
286                 return make_safe<core::basic_frame>(frames);\r
287         }\r
288 \r
289         virtual uint32_t nb_frames() const override\r
290         {\r
291                 if(input_.loop())\r
292                         return std::numeric_limits<uint32_t>::max();\r
293 \r
294                 uint32_t nb_frames = file_nb_frames();\r
295 \r
296                 nb_frames = std::min(length_, nb_frames);\r
297                 nb_frames = muxer_->calc_nb_frames(nb_frames);\r
298                 \r
299                 return nb_frames > start_ ? nb_frames - start_ : 0;\r
300         }\r
301 \r
302         uint32_t file_nb_frames() const\r
303         {\r
304                 uint32_t file_nb_frames = 0;\r
305                 file_nb_frames = std::max(file_nb_frames, video_decoder_ ? video_decoder_->nb_frames() : 0);\r
306                 file_nb_frames = std::max(file_nb_frames, audio_decoder_ ? audio_decoder_->nb_frames() : 0);\r
307                 return file_nb_frames;\r
308         }\r
309         \r
310         virtual boost::unique_future<std::wstring> call(const std::wstring& param) override\r
311         {\r
312                 boost::promise<std::wstring> promise;\r
313                 promise.set_value(do_call(param));\r
314                 return promise.get_future();\r
315         }\r
316                                 \r
317         virtual std::wstring print() const override\r
318         {\r
319                 return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"|" \r
320                                                   + print_mode() + L"|" \r
321                                                   + boost::lexical_cast<std::wstring>(file_frame_number_) + L"/" + boost::lexical_cast<std::wstring>(file_nb_frames()) + L"]";\r
322         }\r
323 \r
324         boost::property_tree::wptree info() const override\r
325         {\r
326                 boost::property_tree::wptree info;\r
327                 info.add(L"type",                               L"ffmpeg-producer");\r
328                 info.add(L"filename",                   filename_);\r
329                 info.add(L"width",                              video_decoder_ ? video_decoder_->width() : 0);\r
330                 info.add(L"height",                             video_decoder_ ? video_decoder_->height() : 0);\r
331                 info.add(L"progressive",                video_decoder_ ? video_decoder_->is_progressive() : false);\r
332                 info.add(L"fps",                                fps_);\r
333                 info.add(L"loop",                               input_.loop());\r
334                 info.add(L"frame-number",               frame_number_);\r
335                 auto nb_frames2 = nb_frames();\r
336                 info.add(L"nb-frames",                  nb_frames2 == std::numeric_limits<int64_t>::max() ? -1 : nb_frames2);\r
337                 info.add(L"file-frame-number",  file_frame_number_);\r
338                 info.add(L"file-nb-frames",             file_nb_frames());\r
339                 return info;\r
340         }\r
341 \r
342         // ffmpeg_producer\r
343 \r
344         std::wstring print_mode() const\r
345         {\r
346                 return video_decoder_ ? ffmpeg::print_mode(video_decoder_->width(), video_decoder_->height(), fps_, !video_decoder_->is_progressive()) : L"";\r
347         }\r
348                                         \r
349         std::wstring do_call(const std::wstring& param)\r
350         {\r
351                 static const boost::wregex loop_exp(L"LOOP\\s*(?<VALUE>\\d?)?", boost::regex::icase);\r
352                 static const boost::wregex seek_exp(L"SEEK\\s+(?<VALUE>\\d+)", boost::regex::icase);\r
353                 \r
354                 boost::wsmatch what;\r
355                 if(boost::regex_match(param, what, loop_exp))\r
356                 {\r
357                         if(!what["VALUE"].str().empty())\r
358                                 input_.loop(boost::lexical_cast<bool>(what["VALUE"].str()));\r
359                         return boost::lexical_cast<std::wstring>(input_.loop());\r
360                 }\r
361                 if(boost::regex_match(param, what, seek_exp))\r
362                 {\r
363                         input_.seek(boost::lexical_cast<uint32_t>(what["VALUE"].str()));\r
364                         return L"";\r
365                 }\r
366 \r
367                 BOOST_THROW_EXCEPTION(invalid_argument());\r
368         }\r
369 \r
370         void try_decode_frame(int hints)\r
371         {\r
372                 std::shared_ptr<AVPacket> pkt;\r
373 \r
374                 for(int n = 0; n < 32 && ((video_decoder_ && !video_decoder_->ready()) || (audio_decoder_ && !audio_decoder_->ready())) && input_.try_pop(pkt); ++n)\r
375                 {\r
376                         if(video_decoder_)\r
377                                 video_decoder_->push(pkt);\r
378                         if(audio_decoder_)\r
379                                 audio_decoder_->push(pkt);\r
380                 }\r
381                 \r
382                 std::shared_ptr<AVFrame>                        video;\r
383                 std::shared_ptr<core::audio_buffer> audio;\r
384 \r
385                 tbb::parallel_invoke(\r
386                 [&]\r
387                 {\r
388                         if(!muxer_->video_ready() && video_decoder_)    \r
389                                 video = video_decoder_->poll(); \r
390                 },\r
391                 [&]\r
392                 {               \r
393                         if(!muxer_->audio_ready() && audio_decoder_)            \r
394                                 audio = audio_decoder_->poll();         \r
395                 });\r
396                 \r
397                 muxer_->push(video, hints);\r
398                 muxer_->push(audio);\r
399 \r
400                 if(!audio_decoder_)\r
401                 {\r
402                         if(video == flush_video())\r
403                                 muxer_->push(flush_audio());\r
404                         else if(!muxer_->audio_ready())\r
405                                 muxer_->push(empty_audio());\r
406                 }\r
407 \r
408                 if(!video_decoder_)\r
409                 {\r
410                         if(audio == flush_audio())\r
411                                 muxer_->push(flush_video(), 0);\r
412                         else if(!muxer_->video_ready())\r
413                                 muxer_->push(empty_video(), 0);\r
414                 }\r
415                 \r
416                 size_t file_frame_number = 0;\r
417                 file_frame_number = std::max(file_frame_number, video_decoder_ ? video_decoder_->file_frame_number() : 0);\r
418                 //file_frame_number = std::max(file_frame_number, audio_decoder_ ? audio_decoder_->file_frame_number() : 0);\r
419 \r
420                 for(auto frame = muxer_->poll(); frame; frame = muxer_->poll())\r
421                         frame_buffer_.push(std::make_pair(make_safe_ptr(frame), file_frame_number));\r
422         }\r
423 };\r
424 \r
425 safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
426 {               \r
427         static const std::vector<std::wstring> invalid_exts = boost::assign::list_of(L".png")(L".tga")(L".bmp")(L".jpg")(L".jpeg")(L".gif")(L".tiff")(L".tif")(L".jp2")(L".jpx")(L".j2k")(L".j2c")(L".swf")(L".ct");\r
428         auto filename = probe_stem(env::media_folder() + L"\\" + params.at(0), invalid_exts);\r
429 \r
430         if(filename.empty())\r
431                 return core::frame_producer::empty();\r
432         \r
433         auto loop               = boost::range::find(params, L"LOOP") != params.end();\r
434         auto start              = get_param(L"SEEK", params, static_cast<uint32_t>(0));\r
435         auto length             = get_param(L"LENGTH", params, std::numeric_limits<uint32_t>::max());\r
436         auto filter_str = get_param(L"FILTER", params, L"");    \r
437                 \r
438         boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1");\r
439         boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1");\r
440         \r
441         return create_producer_destroy_proxy(make_safe<ffmpeg_producer>(frame_factory, filename, filter_str, loop, start, length, false));\r
442 }\r
443 \r
444 safe_ptr<core::frame_producer> create_thumbnail_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
445 {               \r
446         static const std::vector<std::wstring> invalid_exts = boost::assign::list_of\r
447                         (L".png")(L".tga")(L".bmp")(L".jpg")(L".jpeg")(L".gif")(L".tiff")(L".tif")(L".jp2")(L".jpx")(L".j2k")(L".j2c")(L".swf")(L".ct")\r
448                         (L".wav")(L".mp3"); // audio shall not have thumbnails\r
449         auto filename = probe_stem(env::media_folder() + L"\\" + params.at(0), invalid_exts);\r
450 \r
451         if(filename.empty())\r
452                 return core::frame_producer::empty();\r
453         \r
454         auto loop               = false;\r
455         auto start              = 0;\r
456         auto length             = std::numeric_limits<uint32_t>::max();\r
457         auto filter_str = L"";\r
458                 \r
459         return create_producer_destroy_proxy(make_safe<ffmpeg_producer>(frame_factory, filename, filter_str, loop, start, length, true));\r
460 }\r
461 \r
462 }}