]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/ffmpeg_producer.cpp
2.0. ffmpeg_producer: Fixed arithmetic overflow for length.
[casparcg] / modules / ffmpeg / producer / ffmpeg_producer.cpp
index a078af740a13ba4665bdb3f0c4e0cb00ddd2f6d8..3b62cf40319dd8dcd5da9608260b8b039f39d56b 100644 (file)
 \r
 #include "ffmpeg_producer.h"\r
 \r
+#include "frame_muxer.h"\r
 #include "input.h"\r
+#include "util.h"\r
 #include "audio/audio_decoder.h"\r
 #include "video/video_decoder.h"\r
 \r
-#include <common/utility/timer.h>\r
+#include <common/env.h>\r
+#include <common/utility/assert.h>\r
 #include <common/diagnostics/graph.h>\r
 \r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/audio_transform.h>\r
 #include <core/video_format.h>\r
+#include <core/producer/frame_producer.h>\r
+#include <core/producer/frame/frame_factory.h>\r
+#include <core/producer/frame/basic_frame.h>\r
 \r
-#include <common/env.h>\r
-\r
-#include <tbb/parallel_invoke.h>\r
-\r
+#include <boost/assign.hpp>\r
 #include <boost/timer.hpp>\r
-#include <boost/range/algorithm.hpp>\r
-#include <boost/range/algorithm_ext.hpp>\r
+#include <boost/foreach.hpp>\r
+#include <boost/filesystem.hpp>\r
+#include <boost/range/algorithm/find_if.hpp>\r
+#include <boost/range/algorithm/find.hpp>\r
 \r
-#include <deque>\r
+#include <tbb/parallel_invoke.h>\r
 \r
 namespace caspar {\r
-       \r
+                               \r
 struct ffmpeg_producer : public core::frame_producer\r
 {\r
-       static const size_t                                             DECODED_PACKET_BUFFER_SIZE = 4;\r
-       static const size_t                                             MAX_PACKET_OFFSET = 64; // Avoid infinite looping.\r
-\r
-       const std::wstring                                              filename_;\r
-       const bool                                                              loop_;\r
+       const std::wstring                                                              filename_;\r
        \r
-       const safe_ptr<diagnostics::graph>              graph_;\r
-       boost::timer                                                    frame_timer_;\r
-               \r
-       std::deque<safe_ptr<core::write_frame>> video_frame_buffer_;    \r
-       std::deque<std::vector<short>>                  audio_chunk_buffer_;\r
-                       \r
-       const safe_ptr<core::frame_factory>             frame_factory_;\r
+       const safe_ptr<diagnostics::graph>                              graph_;\r
+       boost::timer                                                                    frame_timer_;\r
+       boost::timer                                                                    video_timer_;\r
+       boost::timer                                                                    audio_timer_;\r
+                                       \r
+       const safe_ptr<core::frame_factory>                             frame_factory_;\r
+       const core::video_format_desc                                   format_desc_;\r
+\r
+       input                                                                                   input_; \r
+       video_decoder                                                                   video_decoder_;\r
+       audio_decoder                                                                   audio_decoder_; \r
+       double                                                                                  fps_;\r
+       frame_muxer                                                                             muxer_;\r
+\r
+       int                                                                                             late_frames_;\r
+       const int                                                                               start_;\r
+       const bool                                                                              loop_;\r
+       const size_t                                                                    length_;\r
+\r
+       safe_ptr<core::basic_frame>                                             last_frame_;\r
 \r
-       input                                                                   input_; \r
-       std::unique_ptr<video_decoder>                  video_decoder_;\r
-       std::unique_ptr<audio_decoder>                  audio_decoder_;\r
+       const size_t                                                                    width_;\r
+       const size_t                                                                    height_;\r
+       bool                                                                                    is_progressive_;\r
+       \r
 public:\r
-       explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, bool loop, int start, int length) \r
+       explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, const std::wstring& filter, bool loop, int start, size_t length) \r
                : filename_(filename)\r
-               , loop_(loop) \r
-               , graph_(diagnostics::create_graph(narrow(print())))\r
+               , graph_(diagnostics::create_graph([this]{return print();}))\r
                , frame_factory_(frame_factory)         \r
-               , input_(safe_ptr<diagnostics::graph>(graph_), filename_, loop_, start)\r
+               , format_desc_(frame_factory->get_video_format_desc())\r
+               , input_(graph_, filename_, loop, start, length)\r
+               , video_decoder_(input_.context(), frame_factory, filter)\r
+               , audio_decoder_(input_.context(), frame_factory->get_video_format_desc())\r
+               , fps_(video_decoder_.fps())\r
+               , muxer_(fps_, frame_factory)\r
+               , late_frames_(0)\r
+               , start_(start)\r
+               , loop_(loop)\r
+               , length_(length)\r
+               , last_frame_(core::basic_frame::empty())\r
+               , width_(video_decoder_.width())\r
+               , height_(video_decoder_.height())\r
+               , is_progressive_(true)\r
        {\r
                graph_->add_guide("frame-time", 0.5);\r
-               graph_->set_color("frame-time",  diagnostics::color(1.0f, 0.0f, 0.0f));\r
-               graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));           \r
-               \r
-               double frame_time = 1.0f/input_.fps();\r
-               double format_frame_time = 1.0/frame_factory->get_video_format_desc().fps;\r
-               if(abs(frame_time - format_frame_time) > 0.0001 && abs(frame_time - format_frame_time/2) > 0.0001)\r
-                       CASPAR_LOG(warning) << print() << L" Invalid framerate detected. This may cause distorted audio during playback. frame-time: " << frame_time;\r
-\r
-               try\r
-               {                       \r
-                       video_decoder_.reset(input_.get_video_codec_context() ? \r
-                               new video_decoder(*input_.get_video_codec_context(), frame_factory) : nullptr);\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       CASPAR_LOG(warning) << print() << " failed to initialize video-decoder.";\r
-               }\r
+               graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
+               graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));   \r
                \r
-               try\r
-               {                       \r
-                       audio_decoder_.reset(input_.get_audio_codec_context() ? \r
-                               new audio_decoder(*input_.get_audio_codec_context(), frame_factory->get_video_format_desc()) : nullptr);\r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       CASPAR_LOG(warning) << print() << " failed to initialize audio-decoder.";\r
-               }               \r
-\r
-               if(!video_decoder_ && !audio_decoder_)\r
-               {\r
-                       BOOST_THROW_EXCEPTION(\r
-                               caspar_exception() <<\r
-                               source_info(narrow(print())) << \r
-                               msg_info("Failed to initialize any decoder"));\r
-               }\r
-                       \r
-               // Take some time to read the first packets\r
-               Sleep(40);\r
-\r
-               // Pre-roll since first frames can be heavy.\r
-               while(video_frame_buffer_.size() < DECODED_PACKET_BUFFER_SIZE && \r
-                         audio_chunk_buffer_.size() < DECODED_PACKET_BUFFER_SIZE && \r
-                         input_.has_packet())\r
-               {\r
-                       try_decode_packet();\r
-               }\r
+               for(int n = 0; n < 32 && muxer_.empty(); ++n)\r
+                       decode_frame(0);\r
        }\r
-\r
-       virtual safe_ptr<core::basic_frame> receive()\r
+                       \r
+       virtual safe_ptr<core::basic_frame> receive(int hints)\r
        {\r
+               auto frame = core::basic_frame::late();\r
+               \r
                frame_timer_.restart();\r
-\r
-               std::shared_ptr<core::basic_frame> frame;       \r
-               for(size_t n = 0; !frame && input_.has_packet() && n < MAX_PACKET_OFFSET ; ++n) \r
-                       frame = try_get_frame();\r
-                               \r
-               graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*frame_factory_->get_video_format_desc().fps*0.5));\r
-                                       \r
-               if(frame)\r
-                       return make_safe(frame);\r
                \r
-               if(!input_.is_running())\r
-                       return core::basic_frame::eof();\r
+               for(int n = 0; n < 64 && muxer_.empty(); ++n)\r
+                       decode_frame(hints);\r
+               \r
+               graph_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));\r
 \r
-               if(!video_decoder_ && !audio_decoder_)\r
-                       return core::basic_frame::eof();\r
-                       \r
-               graph_->add_tag("underflow");\r
-               return core::basic_frame::late();       \r
-       }\r
-       \r
-       virtual std::wstring print() const\r
-       {\r
-               return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"]";\r
-       }\r
-       \r
-       void try_decode_packet()\r
-       {\r
-               tbb::parallel_invoke\r
-               (\r
-                       [&]\r
-                       {\r
-                               if(video_frame_buffer_.size() < DECODED_PACKET_BUFFER_SIZE)\r
-                                       try_decode_video_packet(input_.get_video_packet());\r
-                       }, \r
-                       [&]\r
+               if(!muxer_.empty())\r
+                       frame = last_frame_ = muxer_.pop();     \r
+               else\r
+               {\r
+                       if(input_.eof())\r
+                               return core::basic_frame::eof();\r
+                       else\r
                        {\r
-                               if(audio_chunk_buffer_.size() < DECODED_PACKET_BUFFER_SIZE)\r
-                                       try_decode_audio_packet(input_.get_audio_packet());\r
+                               graph_->add_tag("underflow");   \r
+                               ++late_frames_;         \r
                        }\r
-               );      \r
+               }\r
+               \r
+               return frame;\r
        }\r
 \r
-       void try_decode_video_packet(std::shared_ptr<AVPacket>&& video_packet)\r
+       virtual safe_ptr<core::basic_frame> last_frame() const\r
        {\r
-               if(!video_decoder_)\r
-                       return;\r
+               return disable_audio(last_frame_);\r
+       }\r
 \r
-               try\r
+       void decode_frame(int hints)\r
+       {\r
+               for(int n = 0; n < 16 && ((!muxer_.video_ready() && !video_decoder_.ready()) || (!muxer_.audio_ready() && !audio_decoder_.ready())); ++n) \r
                {\r
-                       boost::range::push_back(video_frame_buffer_, video_decoder_->execute(std::move(video_packet)));\r
+                       std::shared_ptr<AVPacket> pkt;\r
+                       if(input_.try_pop(pkt))\r
+                       {\r
+                               video_decoder_.push(pkt);\r
+                               audio_decoder_.push(pkt);\r
+                       }\r
                }\r
-               catch(...)\r
+               \r
+               tbb::parallel_invoke(\r
+               [&]\r
                {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       video_decoder_.reset();\r
-                       CASPAR_LOG(warning) << print() << " removed video-stream.";\r
-               }\r
+                       if(muxer_.video_ready())\r
+                               return;\r
+\r
+                       auto video_frames = video_decoder_.poll();\r
+                       BOOST_FOREACH(auto& video, video_frames)        \r
+                       {\r
+                               is_progressive_ = video ? video->interlaced_frame == 0 : is_progressive_;\r
+                               muxer_.push(video, hints);      \r
+                       }\r
+               },\r
+               [&]\r
+               {\r
+                       if(muxer_.audio_ready())\r
+                               return;\r
+                                       \r
+                       auto audio_samples = audio_decoder_.poll();\r
+                       BOOST_FOREACH(auto& audio, audio_samples)\r
+                               muxer_.push(audio);                             \r
+               });\r
+\r
+               muxer_.commit();\r
        }\r
 \r
-       void try_decode_audio_packet(std::shared_ptr<AVPacket>&& audio_packet)\r
+       virtual int64_t nb_frames() const \r
        {\r
-               if(!audio_decoder_)\r
-                       return;\r
+               if(loop_)\r
+                       return std::numeric_limits<int64_t>::max();\r
 \r
-               try\r
-               {\r
-                       boost::range::push_back(audio_chunk_buffer_, audio_decoder_->execute(std::move(audio_packet)));\r
-               }\r
-               catch(...)\r
+               // This function estimates nb_frames until input has read all packets for one loop, at which point the count should be accurate.\r
+\r
+               int64_t nb_frames = input_.nb_frames();\r
+               if(input_.nb_loops() < 1) // input still hasn't counted all frames\r
                {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                       audio_decoder_.reset();\r
-                       CASPAR_LOG(warning) << print() << " removed audio-stream.";\r
+                       int64_t video_nb_frames = video_decoder_.nb_frames();\r
+                       int64_t audio_nb_frames = audio_decoder_.nb_frames();\r
+\r
+                       nb_frames = std::min(static_cast<int64_t>(length_), std::max(nb_frames, std::max(video_nb_frames, audio_nb_frames)));\r
                }\r
-       }\r
 \r
-       std::shared_ptr<core::basic_frame> try_get_frame()\r
-       {               \r
-               try_decode_packet();\r
+               nb_frames = muxer_.calc_nb_frames(nb_frames);\r
 \r
-               std::shared_ptr<core::write_frame> frame;       \r
+               // TODO: Might need to scale nb_frames av frame_muxer transformations.\r
 \r
-               if(!video_frame_buffer_.empty() && !audio_chunk_buffer_.empty())\r
-               {\r
-                       frame = video_frame_buffer_.front();                            \r
-                       video_frame_buffer_.pop_front();\r
-                               \r
-                       frame->audio_data() = std::move(audio_chunk_buffer_.front());\r
-                       audio_chunk_buffer_.pop_front();        \r
-               }\r
-               else if(!video_frame_buffer_.empty() && !audio_decoder_)\r
-               {\r
-                       frame = std::move(video_frame_buffer_.front());                         \r
-                       video_frame_buffer_.pop_front();\r
-                       frame->get_audio_transform().set_has_audio(false);      \r
-               }\r
-               else if(!audio_chunk_buffer_.empty() && !video_decoder_)\r
-               {\r
-                       frame = frame_factory_->create_frame(this, 1, 1);\r
-                       std::fill(frame->image_data().begin(), frame->image_data().end(), 0);\r
+               return nb_frames + late_frames_ - start_;\r
+       }\r
                                \r
-                       frame->audio_data() = std::move(audio_chunk_buffer_.front());\r
-                       audio_chunk_buffer_.pop_front();\r
-               }\r
-               \r
-               return frame;   \r
+       virtual std::wstring print() const\r
+       {\r
+               return L"ffmpeg[" + boost::filesystem::wpath(filename_).filename() + L"|" \r
+                                                 + boost::lexical_cast<std::wstring>(width_) + L"x" + boost::lexical_cast<std::wstring>(height_)\r
+                                                 + (is_progressive_ ? L"p" : L"i")  + boost::lexical_cast<std::wstring>(fps_)\r
+                                                 + L"]";\r
        }\r
 };\r
 \r
@@ -243,7 +214,7 @@ safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame
        \r
        auto ext = boost::find_if(extensions, [&](const std::wstring& ex)\r
        {                                       \r
-               return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename).replace_extension(ex));\r
+               return boost::filesystem::is_regular_file(boost::filesystem::wpath(filename + L"." + ex));\r
        });\r
 \r
        if(ext == extensions.end())\r
@@ -252,24 +223,32 @@ safe_ptr<core::frame_producer> create_ffmpeg_producer(const safe_ptr<core::frame
        std::wstring path = filename + L"." + *ext;\r
        bool loop = boost::find(params, L"LOOP") != params.end();\r
 \r
-       static const boost::wregex expr(L"\\((?<START>\\d+)(,(?<LENGTH>\\d+)?)?\\)");//(,(?<END>\\d+))?\\]"); // boost::regex has no repeated captures?\r
-       boost::wsmatch what;\r
-       auto it = std::find_if(params.begin(), params.end(), [&](const std::wstring& str)\r
+       size_t start = 0;\r
+       size_t length = std::numeric_limits<size_t>::max();\r
+       \r
+       auto seek_it = boost::find(params, L"SEEK");\r
+       if(seek_it != params.end())\r
        {\r
-               return boost::regex_match(str, what, expr);\r
-       });\r
-\r
-       int start = -1;\r
-       int length = -1;\r
+               if(++seek_it != params.end())\r
+                       start = boost::lexical_cast<size_t>(*seek_it);\r
+       }\r
+       \r
+       auto length_it = boost::find(params, L"LENGTH");\r
+       if(length_it != params.end())\r
+       {\r
+               if(++length_it != params.end())\r
+                       length = boost::lexical_cast<size_t>(*length_it);\r
+       }\r
 \r
-       if(it != params.end())\r
+       std::wstring filter = L"";\r
+       auto filter_it = boost::find(params, L"FILTER");\r
+       if(filter_it != params.end())\r
        {\r
-               start = lexical_cast_or_default(what["START"].str(), -1);\r
-               if(what["LENGTH"].matched)\r
-                       length = lexical_cast_or_default(what["LENGTH"].str(), -1);\r
+               if(++filter_it != params.end())\r
+                       filter = *filter_it;\r
        }\r
-       \r
-       return make_safe<ffmpeg_producer>(frame_factory, path, loop, start, length);\r
+\r
+       return make_safe<ffmpeg_producer>(frame_factory, path, filter, loop, start, length);\r
 }\r
 \r
 }
\ No newline at end of file