]> 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 849af4f19f7748639b58bd6f21c6d99fef15001a..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
@@ -24,7 +24,9 @@
 #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
@@ -39,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
@@ -58,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
@@ -83,9 +87,9 @@ 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, bool thumbnail_mode\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
@@ -234,6 +238,98 @@ struct input::implementation : boost::noncopyable
                        }\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
@@ -254,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
@@ -278,8 +381,8 @@ 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, bool thumbnail_mode\r
-       : impl_(new implementation(graph, filename, loop, start, length, thumbnail_mode)){}\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