]> 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 b11e6565357e1a9a9ad18a54d8bc1304cc2b1a72..8f62f748e6d1d98a4380d90a65d7c77d8008010e 100644 (file)
@@ -26,6 +26,7 @@
 #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
@@ -40,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
@@ -59,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
@@ -84,7 +87,7 @@ struct input::implementation : boost::noncopyable
                \r
        executor                                                                                                        executor_;\r
        \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_params& vid_params) \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, resource_type, vid_params))              \r
                , default_stream_index_(av_find_default_stream_index(format_context_.get()))\r
@@ -236,39 +239,65 @@ struct input::implementation : boost::noncopyable
                });\r
        }       \r
 \r
-       safe_ptr<AVFormatContext> open_input(const std::wstring resource_name, FFMPEG_Resource resource_type, const ffmpeg_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
-         if(vid_params.size_str != L"")\r
-               av_dict_set(&format_options, "video_size", narrow(vid_params.size_str).c_str(), 0); // 640x360 for 16:9\r
-         if(vid_params.pixel_format != L"")\r
-               av_dict_set(&format_options, "pixel_format", narrow(vid_params.pixel_format).c_str(), 0); // yuyv422 for sony\r
-      av_dict_set(&format_options, "avioflags", "direct", 0);\r
-\r
-      // Don't set framerate here, it is typically set to a fixed rate anyway. Better to insert a fps filter to adjust the frame rate.\r
-         // Im going to try anyway for now\r
-      if(vid_params.frame_rate != L"")\r
-               av_dict_set(&format_options, "framerate", narrow(vid_params.frame_rate).c_str(), 0);\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
-      av_dict_free(&format_options);\r
-      } break;\r
-    case FFMPEG_STREAM:\r
-      THROW_ON_ERROR2(avformat_open_input(&weak_context, narrow(resource_name).c_str(), nullptr, nullptr), resource_name);\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
+       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
@@ -321,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
@@ -345,7 +381,7 @@ struct input::implementation : boost::noncopyable
        }\r
 };\r
 \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_params& vid_params) \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