]> git.sesse.net Git - casparcg/commitdiff
ffmpeg: Add support for arbitrary ffmpeg options in ffmpeg_producer
authorcambell <cambell.prince@gmail.com>
Sat, 15 Jun 2013 03:31:44 +0000 (10:31 +0700)
committercambell <cambell.prince@gmail.com>
Sat, 15 Jun 2013 03:31:44 +0000 (10:31 +0700)
modules/ffmpeg/consumer/ffmpeg_consumer.cpp
modules/ffmpeg/ffmpeg.vcxproj
modules/ffmpeg/ffmpeg.vcxproj.filters
modules/ffmpeg/ffmpeg_params.h [moved from modules/ffmpeg/producer/ffmpeg_params.h with 51% similarity]
modules/ffmpeg/producer/ffmpeg_producer.cpp
modules/ffmpeg/producer/input/input.cpp
modules/ffmpeg/producer/input/input.h

index 75094136a73b9cbb5ddf0e216694aca153ae467d..efd49841f05fd4cfe2f010d14393b53821939ce1 100644 (file)
@@ -25,6 +25,7 @@
 \r
 #include "ffmpeg_consumer.h"\r
 \r
+#include "../ffmpeg_params.h"\r
 #include "../producer/audio/audio_resampler.h"\r
 \r
 #include <core/parameters/parameters.h>\r
@@ -106,18 +107,6 @@ int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
        return ::av_opt_set(obj, name, val, search_flags);\r
 }\r
 \r
-struct option\r
-{\r
-       std::string name;\r
-       std::string value;\r
-\r
-       option(std::string name, std::string value)\r
-               : name(std::move(name))\r
-               , value(std::move(value))\r
-       {\r
-       }\r
-};\r
-       \r
 struct output_format\r
 {\r
        AVOutputFormat* format;\r
index 7f7316646b169a3862bbeaf42155dfcb2ba1f5c3..63980cd0bf754c96cf1a929737ec5b3ff247a9a5 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>\r
+<?xml version="1.0" encoding="utf-8"?>\r
 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
   <ItemGroup Label="ProjectConfigurations">\r
     <ProjectConfiguration Include="Profile|Win32">\r
     <ClInclude Include="consumer\ffmpeg_consumer.h" />\r
     <ClInclude Include="ffmpeg.h" />\r
     <ClInclude Include="ffmpeg_error.h" />\r
+    <ClInclude Include="ffmpeg_params.h" />\r
     <ClInclude Include="producer\audio\audio_decoder.h" />\r
     <ClInclude Include="producer\audio\audio_resampler.h" />\r
-    <ClInclude Include="producer\ffmpeg_params.h" />\r
     <ClInclude Include="producer\ffmpeg_producer.h" />\r
     <ClInclude Include="producer\filter\filter.h" />\r
     <ClInclude Include="producer\filter\parallel_yadif.h" />\r
index c8203601184a993b4be56e54f2c4fe6db4fd934d..540b4acefcf057a4f02fa106c1431dd111f4850b 100644 (file)
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>\r
+<?xml version="1.0" encoding="utf-8"?>\r
 <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">\r
   <ItemGroup>\r
     <Filter Include="source">\r
@@ -91,6 +91,9 @@
     <ClInclude Include="ffmpeg.h">\r
       <Filter>source</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="ffmpeg_params.h">\r
+      <Filter>source</Filter>\r
+    </ClInclude>\r
     <ClInclude Include="producer\filter\filter.h">\r
       <Filter>source\producer\filter</Filter>\r
     </ClInclude>\r
     <ClInclude Include="producer\tbb_avcodec.h">\r
       <Filter>source\producer</Filter>\r
     </ClInclude>\r
-    <ClInclude Include="producer\ffmpeg_params.h">\r
-      <Filter>source\producer</Filter>\r
-    </ClInclude>\r
   </ItemGroup>\r
 </Project>
\ No newline at end of file
similarity index 51%
rename from modules/ffmpeg/producer/ffmpeg_params.h
rename to modules/ffmpeg/ffmpeg_params.h
index f7c148cd535b4fe044be8b9e0ac2005b9ada8b34..23c5faa14d336b906fbd4e7f7747e10166ef8429 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright 2013 Sveriges Television AB http://casparcg.com/
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
 *
 * This file is part of CasparCG (www.casparcg.com).
 *
 * You should have received a copy of the GNU General Public License
 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
 *
-* Author: Julian Waller
+* Author: Cambell Prince, cambell.prince@gmail.com
 */
+
 #pragma once
 
+#include <common/memory/safe_ptr.h>
+
+#include <stdint.h>
 #include <string>
+#include <vector>
 
 namespace caspar {
+       
 namespace ffmpeg {
 
 enum FFMPEG_Resource {
@@ -31,20 +37,40 @@ enum FFMPEG_Resource {
        FFMPEG_STREAM
 };
 
-struct ffmpeg_params
+struct option
 {
-       std::wstring  size_str;
-       std::wstring  pixel_format;
-       std::wstring  frame_rate;
+       std::string name;
+       std::string value;
+
+       option(std::string name, std::string value)
+               : name(std::move(name))
+               , value(std::move(value))
+       {
+       }
+};
+
+struct ffmpeg_producer_params
+{
+       bool                loop;
+       uint32_t            start;
+       uint32_t            length;
+       std::wstring        filter_str;
+
+       FFMPEG_Resource     resource_type;
+       std::wstring        resource_name;
 
+       std::vector<option> options;
 
-       ffmpeg_params() 
-               : size_str(L"")
-               , pixel_format(L"")
-               , frame_rate(L"")
+       ffmpeg_producer_params() 
+               : loop(false)
+               , start(0)
+               , length(std::numeric_limits<uint32_t>::max())
+               , filter_str(L"")
+               , resource_type(FFMPEG_FILE)
+               , resource_name(L"")
        {
        }
 
 };
 
-}}
\ No newline at end of file
+}}
index ec4c81b9c9bfee092170f05a391cd071ad5b50dd..3daea369ff859517ddb5e43b72424175e8ec609a 100644 (file)
@@ -23,9 +23,9 @@
 \r
 #include "ffmpeg_producer.h"\r
 \r
-#include "../ffmpeg_error.h"\r
 #include "../ffmpeg.h"\r
-#include "ffmpeg_params.h"\r
+#include "../ffmpeg_error.h"\r
+#include "../ffmpeg_params.h"\r
 \r
 #include "muxer/frame_muxer.h"\r
 #include "input/input.h"\r
@@ -121,7 +121,7 @@ struct ffmpeg_producer : public core::frame_producer
        uint32_t                                                                                                        file_frame_number_;\r
                \r
 public:\r
-       explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, FFMPEG_Resource resource_type, const std::wstring& filter, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const std::wstring& custom_channel_order, const ffmpeg_params& vid_params)\r
+       explicit ffmpeg_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, FFMPEG_Resource resource_type, const std::wstring& filter, bool loop, uint32_t start, uint32_t length, bool thumbnail_mode, const std::wstring& custom_channel_order, const ffmpeg_producer_params& vid_params)\r
                : filename_(filename)\r
                , path_relative_to_media_(get_relative_or_original(filename, env::media_folder()))\r
                , resource_type_(resource_type)\r
@@ -527,8 +527,8 @@ safe_ptr<core::frame_producer> create_producer(
                        filename = probe_stem(filename);\r
 \r
                //TODO fix these?\r
-               //ffmpeg_params->loop       = params.has(L"LOOP");\r
-               //ffmpeg_params->start     = params.get(L"SEEK", static_cast<uint32_t>(0));\r
+               //vid_params->loop       = params.has(L"LOOP");\r
+               //vid_params->start     = params.get(L"SEEK", static_cast<uint32_t>(0));\r
        }\r
 \r
        if(filename.empty())\r
@@ -542,11 +542,24 @@ safe_ptr<core::frame_producer> create_producer(
 \r
        boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1");\r
        boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1");\r
+       \r
+       ffmpeg_producer_params vid_params;\r
+       bool haveFFMPEGStartIndicator = false;\r
+       for (size_t i = 0; i < params.size() - 1; ++i)\r
+       {\r
+               if (!haveFFMPEGStartIndicator && params[i] == L"--")\r
+               {\r
+                       haveFFMPEGStartIndicator = true;\r
+                       continue;\r
+               }\r
+               if (haveFFMPEGStartIndicator)\r
+               {\r
+                       auto name = narrow(params.at_original(i)).substr(1);\r
+                       auto value = narrow(params.at_original(i + 1));\r
+                       vid_params.options.push_back(option(name, value));\r
+               }\r
+       }\r
 \r
-       ffmpeg_params vid_params;\r
-       vid_params.size_str = params.get(L"SIZE", L"");\r
-       vid_params.pixel_format = params.get(L"PIXFMT", L"");\r
-       vid_params.frame_rate = params.get(L"FRAMERATE", L"");\r
        \r
        return create_producer_destroy_proxy(make_safe<ffmpeg_producer>(frame_factory, filename, resource_type, filter_str, loop, start, length, false, custom_channel_order, vid_params));\r
 }\r
@@ -567,9 +580,8 @@ safe_ptr<core::frame_producer> create_thumbnail_producer(
        auto start              = 0;\r
        auto length             = std::numeric_limits<uint32_t>::max();\r
        auto filter_str = L"";\r
-               \r
-       ffmpeg_params vid_params;\r
 \r
+       ffmpeg_producer_params vid_params;\r
        return make_safe<ffmpeg_producer>(frame_factory, filename, FFMPEG_FILE, filter_str, loop, start, length, true, L"", vid_params);\r
 }\r
 \r
index b11e6565357e1a9a9ad18a54d8bc1304cc2b1a72..d8f5bf0df9480f42022b4783f4a36e79697e853c 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
@@ -59,9 +60,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 +86,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 +238,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
@@ -345,7 +373,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
index 05431b6a3d12a0d827e9a09b2c43d695e381b1bd..6d9319e2aa1013890da706b3b76b2be0c44ab218 100644 (file)
@@ -20,7 +20,7 @@
 */\r
 \r
 #pragma once\r
-#include "../ffmpeg_params.h"\r
+#include "../../ffmpeg_params.h"\r
 \r
 #include <common/memory/safe_ptr.h>\r
 \r
@@ -47,7 +47,7 @@ namespace ffmpeg {
 class input : boost::noncopyable\r
 {\r
 public:\r
-       explicit 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
+       explicit 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
 \r
        bool try_pop(std::shared_ptr<AVPacket>& packet);\r
        bool eof() const;\r