]> git.sesse.net Git - casparcg/commitdiff
3601399: Added support for creating two separate files -- one for fill and one for...
authorhellgore <hellgore@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Fri, 18 Jan 2013 18:17:13 +0000 (18:17 +0000)
committerhellgore <hellgore@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Fri, 18 Jan 2013 18:17:13 +0000 (18:17 +0000)
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/trunk@3693 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d

modules/ffmpeg/consumer/ffmpeg_consumer.cpp
shell/casparcg.config

index db563f7ca63bf8970e86fad4dc94cb71fdd4c755..7fd57edf41188080debc34a4c6487b20072009c6 100644 (file)
@@ -38,6 +38,7 @@
 #include <common/env.h>\r
 #include <common/utility/string.h>\r
 #include <common/utility/param.h>\r
+#include <common/memory/memshfl.h>\r
 \r
 #include <boost/algorithm/string.hpp>\r
 #include <boost/timer.hpp>\r
@@ -233,6 +234,7 @@ struct ffmpeg_consumer : boost::noncopyable
        byte_vector                                                             audio_outbuf_;\r
        byte_vector                                                             audio_buf_;\r
        byte_vector                                                             video_outbuf_;\r
+       byte_vector                                                             key_picture_buf_;\r
        byte_vector                                                             picture_buf_;\r
        std::shared_ptr<audio_resampler>                swr_;\r
        std::shared_ptr<SwsContext>                             sws_;\r
@@ -241,9 +243,10 @@ struct ffmpeg_consumer : boost::noncopyable
        int64_t                                                                 out_frame_number_;\r
 \r
        output_format                                                   output_format_;\r
+       bool                                                                    key_only_;\r
        \r
 public:\r
-       ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options)\r
+       ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, std::vector<option> options, bool key_only)\r
                : filename_(filename)\r
                , video_outbuf_(1920*1080*8)\r
                , audio_outbuf_(10000)\r
@@ -253,6 +256,7 @@ public:
                , in_frame_number_(0)\r
                , out_frame_number_(0)\r
                , output_format_(format_desc, filename, options)\r
+               , key_only_(key_only)\r
        {\r
                // TODO: Ask stakeholders about case where file already exists.\r
                boost::filesystem2::remove(boost::filesystem2::wpath(env::media_folder() + widen(filename))); // Delete the file if it exists\r
@@ -273,7 +277,9 @@ public:
                //  Add the audio and video streams using the default format codecs     and initialize the codecs.\r
                auto options2 = options;\r
                video_st_ = add_video_stream(options2);\r
-               audio_st_ = add_audio_stream(options);\r
+\r
+               if (!key_only)\r
+                       audio_st_ = add_audio_stream(options);\r
                                \r
                dump_format(oc_.get(), 0, filename_.c_str(), 1);\r
                 \r
@@ -302,7 +308,8 @@ public:
                \r
                LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");\r
                \r
-               audio_st_.reset();\r
+               if (!key_only_)\r
+                       audio_st_.reset();\r
                video_st_.reset();\r
                          \r
                if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
@@ -468,8 +475,21 @@ public:
                }\r
 \r
                std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);\r
-               avpicture_fill(reinterpret_cast<AVPicture*>(in_frame.get()), const_cast<uint8_t*>(frame.image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);\r
-                               \r
+               auto in_picture = reinterpret_cast<AVPicture*>(in_frame.get());\r
+\r
+               if (key_only_)\r
+               {\r
+                       key_picture_buf_.resize(frame.image_data().size());\r
+                       in_picture->linesize[0] = format_desc_.width * 4;\r
+                       in_picture->data[0] = key_picture_buf_.data();\r
+\r
+                       fast_memshfl(in_picture->data[0], frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
+               }\r
+               else\r
+               {\r
+                       avpicture_fill(in_picture, const_cast<uint8_t*>(frame.image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);\r
+               }\r
+\r
                std::shared_ptr<AVFrame> out_frame(avcodec_alloc_frame(), av_free);\r
                picture_buf_.resize(avpicture_get_size(c->pix_fmt, c->width, c->height));\r
                avpicture_fill(reinterpret_cast<AVPicture*>(out_frame.get()), picture_buf_.data(), c->pix_fmt, c->width, c->height);\r
@@ -587,18 +607,27 @@ public:
                 \r
        void send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               bool queued = encode_executor_.try_begin_invoke([=]\r
+               encode_executor_.begin_invoke([=]\r
                {               \r
                        boost::timer frame_timer;\r
 \r
                        encode_video_frame(*frame);\r
-                       encode_audio_frame(*frame);\r
+\r
+                       if (!key_only_)\r
+                               encode_audio_frame(*frame);\r
 \r
                        graph_->set_value("frame-time", frame_timer.elapsed()*format_desc_.fps*0.5);                    \r
                });\r
+       }\r
+\r
+       bool ready_for_frame()\r
+       {\r
+               return encode_executor_.size() < encode_executor_.capacity();\r
+       }\r
 \r
-               if (!queued)\r
-                       graph_->set_tag("dropped-frame");\r
+       void mark_dropped()\r
+       {\r
+               graph_->set_tag("dropped-frame");\r
 \r
                // TODO: adjust PTS accordingly to make dropped frames contribute\r
                //       to the total playing time\r
@@ -608,27 +637,59 @@ public:
 struct ffmpeg_consumer_proxy : public core::frame_consumer\r
 {\r
        const std::wstring                              filename_;\r
-       const std::vector<option>                       options_;\r
+       const std::vector<option>               options_;\r
+       const bool                                              separate_key_;\r
 \r
        std::unique_ptr<ffmpeg_consumer> consumer_;\r
+       std::unique_ptr<ffmpeg_consumer> key_only_consumer_;\r
 \r
 public:\r
 \r
-       ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options)\r
+       ffmpeg_consumer_proxy(const std::wstring& filename, const std::vector<option>& options, bool separate_key_)\r
                : filename_(filename)\r
                , options_(options)\r
+               , separate_key_(separate_key_)\r
        {\r
        }\r
        \r
        virtual void initialize(const core::video_format_desc& format_desc, int)\r
        {\r
                consumer_.reset();\r
-               consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, options_));\r
+               key_only_consumer_.reset();\r
+               consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, options_, false));\r
+\r
+               if (separate_key_)\r
+               {\r
+                       boost::filesystem::wpath fill_file(filename_);\r
+                       auto without_extension = fill_file.stem();\r
+                       auto key_file = env::media_folder() + without_extension + L"_A" + fill_file.extension();\r
+                       \r
+                       key_only_consumer_.reset(new ffmpeg_consumer(narrow(key_file), format_desc, options_, true));\r
+               }\r
        }\r
        \r
        virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
        {\r
-               consumer_->send(frame);\r
+               bool ready_for_frame = consumer_->ready_for_frame();\r
+\r
+               if (ready_for_frame && separate_key_)\r
+                       ready_for_frame = ready_for_frame && key_only_consumer_->ready_for_frame();\r
+\r
+               if (ready_for_frame)\r
+               {\r
+                       consumer_->send(frame);\r
+\r
+                       if (separate_key_)\r
+                               key_only_consumer_->send(frame);\r
+               }\r
+               else\r
+               {\r
+                       consumer_->mark_dropped();\r
+\r
+                       if (separate_key_)\r
+                               key_only_consumer_->mark_dropped();\r
+               }\r
+\r
                return caspar::wrap_as_future(true);\r
        }\r
        \r
@@ -642,6 +703,7 @@ public:
                boost::property_tree::wptree info;\r
                info.add(L"type", L"ffmpeg-consumer");\r
                info.add(L"filename", filename_);\r
+               info.add(L"separate_key", separate_key_);\r
                return info;\r
        }\r
                \r
@@ -665,14 +727,24 @@ safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>&
 {\r
        if(params.size() < 1 || params[0] != L"FILE")\r
                return core::frame_consumer::empty();\r
+\r
+       std::vector<std::wstring> params2 = params;\r
        \r
-       auto filename   = (params.size() > 1 ? params[1] : L"");\r
+       auto filename   = (params2.size() > 1 ? params2[1] : L"");\r
+       auto separate_key_it = std::find(params2.begin(), params2.end(), L"SEPARATE_KEY");\r
+       bool separate_key = false;\r
+\r
+       if (separate_key_it != params2.end())\r
+       {\r
+               separate_key = true;\r
+               params2.erase(separate_key_it);\r
+       }\r
                        \r
        std::vector<option> options;\r
        \r
-       if(params.size() >= 3)\r
+       if(params2.size() >= 3)\r
        {\r
-               for(auto opt_it = params.begin()+2; opt_it != params.end();)\r
+               for(auto opt_it = params2.begin()+2; opt_it != params2.end();)\r
                {\r
                        auto name  = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++))).substr(1);\r
                        auto value = narrow(boost::trim_copy(boost::to_lower_copy(*opt_it++)));\r
@@ -686,18 +758,19 @@ safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>&
                }\r
        }\r
                \r
-       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);\r
+       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);\r
 }\r
 \r
 safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree)\r
 {\r
-       auto filename   = ptree.get<std::wstring>(L"path");\r
-       auto codec              = ptree.get(L"vcodec", L"libx264");\r
+       auto filename           = ptree.get<std::wstring>(L"path");\r
+       auto codec                      = ptree.get(L"vcodec", L"libx264");\r
+       auto separate_key       = ptree.get(L"separate-key", false);\r
 \r
        std::vector<option> options;\r
        options.push_back(option("vcodec", narrow(codec)));\r
        \r
-       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options);\r
+       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, options, separate_key);\r
 }\r
 \r
 }}\r
index aa73ab5bce7b3dc09a32fa7cb4ea815f0a9b589d..5f28b633adb31cb87209fb5636a6a9b813e33a49 100644 (file)
@@ -73,6 +73,7 @@
             <file>\r
                 <path></path>\r
                 <vcodec>libx264 [libx264|qtrle]</vcodec>\r
+                <separate-key>false [true|false]</separate-key>\r
             </file>\r
         </consumers>\r
     </channel>\r