]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/input/input.cpp
Seeking fix from Robert Nagy. This needs testing!
[casparcg] / modules / ffmpeg / producer / input / input.cpp
index 0ab791584d193a569a2a649ab16da3e01e1f195c..8f62f748e6d1d98a4380d90a65d7c77d8008010e 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
 * This file is part of CasparCG (www.casparcg.com).\r
 *\r
 #include "input.h"\r
 \r
 #include "../util/util.h"\r
+#include "../util/flv.h"\r
 #include "../../ffmpeg_error.h"\r
+#include "../../ffmpeg_params.h"\r
+#include "../../ffmpeg.h"\r
 \r
 #include <core/video_format.h>\r
 \r
 #include <common/diagnostics/graph.h>\r
 #include <common/concurrency/executor.h>\r
+#include <common/concurrency/future_util.h>\r
 #include <common/exception/exceptions.h>\r
 #include <common/exception/win32_exception.h>\r
 \r
@@ -37,6 +41,7 @@
 #include <tbb/atomic.h>\r
 #include <tbb/recursive_mutex.h>\r
 \r
+#include <boost/rational.hpp>\r
 #include <boost/range/algorithm.hpp>\r
 #include <boost/thread/condition_variable.hpp>\r
 #include <boost/thread/mutex.hpp>\r
@@ -56,9 +61,10 @@ extern "C"
 #pragma warning (pop)\r
 #endif\r
 \r
-static const size_t MAX_BUFFER_COUNT = 100;\r
-static const size_t MIN_BUFFER_COUNT = 50;\r
-static const size_t MAX_BUFFER_SIZE  = 64 * 1000000;\r
+static const size_t MAX_BUFFER_COUNT    = 100;\r
+static const size_t MAX_BUFFER_COUNT_RT = 3;\r
+static const size_t MIN_BUFFER_COUNT    = 50;\r
+static const size_t MAX_BUFFER_SIZE     = 64 * 1000000;\r
 \r
 namespace caspar { namespace ffmpeg {\r
                \r
@@ -72,6 +78,7 @@ struct input::implementation : boost::noncopyable
        const std::wstring                                                                                      filename_;\r
        const uint32_t                                                                                          start_;         \r
        const uint32_t                                                                                          length_;\r
+       const bool                                                                                                      thumbnail_mode_;\r
        tbb::atomic<bool>                                                                                       loop_;\r
        uint32_t                                                                                                        frame_number_;\r
        \r
@@ -80,16 +87,23 @@ struct input::implementation : boost::noncopyable
                \r
        executor                                                                                                        executor_;\r
        \r
-       explicit implementation(const safe_ptr<diagnostics::graph> graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length\r
+       explicit implementation(const safe_ptr<diagnostics::graph> graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_producer_params& vid_params\r
                : graph_(graph)\r
-               , format_context_(open_input(filename))         \r
+               , format_context_(open_input(filename, resource_type, vid_params))              \r
                , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
                , filename_(filename)\r
                , start_(start)\r
                , length_(length)\r
+               , thumbnail_mode_(thumbnail_mode)\r
                , frame_number_(0)\r
                , executor_(print())\r
-       {               \r
+       {\r
+               if (thumbnail_mode_)\r
+                       executor_.invoke([]\r
+                       {\r
+                               disable_logging_for_thread();\r
+                       });\r
+\r
                loop_                   = loop;\r
                buffer_size_    = 0;\r
 \r
@@ -120,9 +134,22 @@ struct input::implementation : boost::noncopyable
                return result;\r
        }\r
 \r
-       void seek(uint32_t target)\r
+       std::ptrdiff_t get_max_buffer_count() const\r
+       {\r
+               return thumbnail_mode_ ? 1 : MAX_BUFFER_COUNT;\r
+       }\r
+\r
+       std::ptrdiff_t get_min_buffer_count() const\r
        {\r
-               executor_.begin_invoke([=]\r
+               return thumbnail_mode_ ? 0 : MIN_BUFFER_COUNT;\r
+       }\r
+\r
+       boost::unique_future<bool> seek(uint32_t target)\r
+       {\r
+               if (!executor_.is_running())\r
+                       return wrap_as_future(false);\r
+\r
+               return executor_.begin_invoke([=]() -> bool\r
                {\r
                        std::shared_ptr<AVPacket> packet;\r
                        while(buffer_.try_pop(packet) && packet)\r
@@ -131,6 +158,8 @@ struct input::implementation : boost::noncopyable
                        queued_seek(target);\r
 \r
                        tick();\r
+\r
+                       return true;\r
                }, high_priority);\r
        }\r
        \r
@@ -141,7 +170,7 @@ struct input::implementation : boost::noncopyable
        \r
        bool full() const\r
        {\r
-               return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > MAX_BUFFER_COUNT) && buffer_.size() > MIN_BUFFER_COUNT;\r
+               return (buffer_size_ > MAX_BUFFER_SIZE || buffer_.size() > get_max_buffer_count()) && buffer_.size() > get_min_buffer_count();\r
        }\r
 \r
        void tick()\r
@@ -203,15 +232,109 @@ struct input::implementation : boost::noncopyable
                        }\r
                        catch(...)\r
                        {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
+                               if (!thumbnail_mode_)\r
+                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
                                executor_.stop();\r
                        }\r
                });\r
        }       \r
+\r
+       safe_ptr<AVFormatContext> open_input(const std::wstring resource_name, FFMPEG_Resource resource_type, const ffmpeg_producer_params& vid_params)\r
+       {\r
+               AVFormatContext* weak_context = nullptr;\r
+\r
+               switch (resource_type) {\r
+                       case FFMPEG_FILE:\r
+                               THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, nullptr), resource_name);\r
+                               break;\r
+                       case FFMPEG_DEVICE: {\r
+                               AVDictionary* format_options = NULL;\r
+                               for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
+                               {\r
+                                       av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
+                               }\r
+                               AVInputFormat* input_format = av_find_input_format("dshow");\r
+                               THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), input_format, &format_options), resource_name);\r
+                               if (format_options != nullptr)\r
+                               {\r
+                                       std::string unsupported_tokens = "";\r
+                                       AVDictionaryEntry *t = NULL;\r
+                                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
+                                       {\r
+                                               if (!unsupported_tokens.empty())\r
+                                                       unsupported_tokens += ", ";\r
+                                               unsupported_tokens += t->key;\r
+                                       }\r
+                                       av_close_input_file(weak_context);\r
+                                       BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
+                               }\r
+                               av_dict_free(&format_options);\r
+                       } break;\r
+                       case FFMPEG_STREAM: {\r
+                               AVDictionary* format_options = NULL;\r
+                               for (auto it = vid_params.options.begin(); it != vid_params.options.end(); ++it)\r
+                               {\r
+                                       av_dict_set(&format_options, (*it).name.c_str(), (*it).value.c_str(), 0);\r
+                               }\r
+                               THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, &format_options), resource_name);\r
+                               if (format_options != nullptr)\r
+                               {\r
+                                       std::string unsupported_tokens = "";\r
+                                       AVDictionaryEntry *t = NULL;\r
+                                       while ((t = av_dict_get(format_options, "", t, AV_DICT_IGNORE_SUFFIX)) != nullptr)\r
+                                       {\r
+                                               if (!unsupported_tokens.empty())\r
+                                                       unsupported_tokens += ", ";\r
+                                               unsupported_tokens += t->key;\r
+                                       }\r
+                                       av_close_input_file(weak_context);\r
+                                       BOOST_THROW_EXCEPTION(ffmpeg_error() << msg_info(unsupported_tokens));\r
+                               }\r
+                               av_dict_free(&format_options);\r
+                       } break;\r
+               };\r
+               safe_ptr<AVFormatContext> context(weak_context, av_close_input_file);      \r
+               THROW_ON_ERROR2(avformat_find_stream_info(weak_context, nullptr), resource_name);\r
+               fix_meta_data(*context);\r
+               return context;\r
+       }\r
+\r
+  void fix_meta_data(AVFormatContext& context)\r
+  {\r
+    auto video_index = av_find_best_stream(&context, AVMEDIA_TYPE_VIDEO, -1, -1, 0, 0);\r
+\r
+    if(video_index > -1)\r
+    {\r
+     auto video_stream   = context.streams[video_index];\r
+      auto video_context  = context.streams[video_index]->codec;\r
+            \r
+      if(boost::filesystem2::path(context.filename).extension() == ".flv")\r
+      {\r
+        try\r
+        {\r
+          auto meta = read_flv_meta_info(context.filename);\r
+          double fps = boost::lexical_cast<double>(meta["framerate"]);\r
+          video_stream->nb_frames = static_cast<int64_t>(boost::lexical_cast<double>(meta["duration"])*fps);\r
+        }\r
+        catch(...){}\r
+      }\r
+      else\r
+      {\r
+        auto stream_time = video_stream->time_base;\r
+        auto duration   = video_stream->duration;\r
+        auto codec_time  = video_context->time_base;\r
+        auto ticks     = video_context->ticks_per_frame;\r
+\r
+        if(video_stream->nb_frames == 0)\r
+          video_stream->nb_frames = (duration*stream_time.num*codec_time.den)/(stream_time.den*codec_time.num*ticks);  \r
+      }\r
+    }\r
+  }\r
                        \r
        void queued_seek(const uint32_t target)\r
        {       \r
-               CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
+               if (!thumbnail_mode_)\r
+                       CASPAR_LOG(debug) << print() << " Seeking: " << target;\r
 \r
                int flags = AVSEEK_FLAG_FRAME;\r
                if(target == 0)\r
@@ -227,11 +350,18 @@ struct input::implementation : boost::noncopyable
                }\r
                \r
                auto stream = format_context_->streams[default_stream_index_];\r
-               auto codec  = stream->codec;\r
-               auto fixed_target = (target*stream->time_base.den*codec->time_base.num)/(stream->time_base.num*codec->time_base.den)*codec->ticks_per_frame;\r
                \r
-               THROW_ON_ERROR2(avformat_seek_file(format_context_.get(), default_stream_index_, std::numeric_limits<int64_t>::min(), fixed_target, std::numeric_limits<int64_t>::max(), 0), print());          \r
                \r
+               auto fps = read_fps(*format_context_, 0.0);\r
+                               \r
+               THROW_ON_ERROR2(avformat_seek_file(\r
+                       format_context_.get(), \r
+                       default_stream_index_, \r
+                       std::numeric_limits<int64_t>::min(),\r
+                       static_cast<int64_t>((target / fps * stream->time_base.den) / stream->time_base.num),\r
+                       std::numeric_limits<int64_t>::max(), \r
+                       0), print());\r
+\r
                auto flush_packet       = create_packet();\r
                flush_packet->data      = nullptr;\r
                flush_packet->size      = 0;\r
@@ -251,12 +381,12 @@ struct input::implementation : boost::noncopyable
        }\r
 };\r
 \r
-input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, bool loop, uint32_t start, uint32_t length\r
-       : impl_(new implementation(graph, filename, loop, start, length)){}\r
+input::input(const safe_ptr<diagnostics::graph>& graph, const std::wstring& filename, FFMPEG_Resource resource_type, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const ffmpeg_producer_params& vid_params\r
+       : impl_(new implementation(graph, filename, resource_type, loop, start, length, thumbnail_mode, vid_params)){}\r
 bool input::eof() const {return !impl_->executor_.is_running();}\r
 bool input::try_pop(std::shared_ptr<AVPacket>& packet){return impl_->try_pop(packet);}\r
 safe_ptr<AVFormatContext> input::context(){return impl_->format_context_;}\r
 void input::loop(bool value){impl_->loop_ = value;}\r
 bool input::loop() const{return impl_->loop_;}\r
-void input::seek(uint32_t target){impl_->seek(target);}\r
+boost::unique_future<bool> input::seek(uint32_t target){return impl_->seek(target);}\r
 }}\r