]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
2.0.2: - Updated get_param.
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
index 66bc664cfe16c5bcecacbdf99c1f60fdebc39afd..2a93beac856441b32c6a231e228d286750e58dd8 100644 (file)
@@ -1,21 +1,22 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This ffmpeg is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
 *\r
-*    CasparCG is free software: you can redistribute it and/or modify\r
-*    it under the terms of the GNU General Public License as published by\r
-*    the Free Software Foundation, either version 3 of the License, or\r
-*    (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
 *\r
-*    CasparCG is distributed in the hope that it will be useful,\r
-*    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-*    GNU General Public License for more details.\r
-\r
-*    You should have received a copy of the GNU General Public License\r
-*    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
 *\r
+* Author: Robert Nagy, ronag89@gmail.com\r
 */\r
  \r
 #include "../StdAfx.h"\r
 \r
 #include <common/concurrency/executor.h>\r
 #include <common/diagnostics/graph.h>\r
-#include <common/utility/string.h>\r
 #include <common/env.h>\r
+#include <common/utility/string.h>\r
+#include <common/utility/param.h>\r
 \r
-#include <boost/timer.hpp>\r
-#include <boost/thread/once.hpp>\r
-#include <boost/thread.hpp>\r
 #include <boost/algorithm/string.hpp>\r
+#include <boost/timer.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
 \r
 #include <tbb/cache_aligned_allocator.h>\r
 #include <tbb/parallel_invoke.h>\r
 \r
-#include <cstdio>\r
-\r
 #if defined(_MSC_VER)\r
 #pragma warning (push)\r
 #pragma warning (disable : 4244)\r
@@ -53,7 +52,7 @@ extern "C"
        #define __STDC_CONSTANT_MACROS\r
        #define __STDC_LIMIT_MACROS\r
        #include <libavformat/avformat.h>\r
-       #include <libswscale/swscale.h>
+       #include <libswscale/swscale.h>\r
        #include <libavutil/opt.h>\r
 }\r
 #if defined(_MSC_VER)\r
@@ -78,30 +77,29 @@ struct ffmpeg_consumer : boost::noncopyable
 \r
        // Audio\r
        std::shared_ptr<AVStream>                               audio_st_;\r
-       std::vector<uint8_t>                                    audio_outbuf_;\r
-\r
-       std::vector<int16_t>                                    audio_input_buffer_;\r
-\r
+       \r
        // Video\r
        std::shared_ptr<AVStream>                               video_st_;\r
-       std::vector<uint8_t>                                    video_outbuf_;\r
 \r
+       std::vector<uint8_t>                                    video_outbuf_;\r
        std::vector<uint8_t>                                    picture_buf_;\r
-       std::shared_ptr<SwsContext>                             img_convert_ctx_;\r
+       std::shared_ptr<SwsContext>                             sws_;\r
 \r
        int64_t                                                                 frame_number_;\r
        \r
 public:\r
-       ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, const std::string& codec, int bitrate)\r
-               : filename_(filename + ".mov")\r
+       ffmpeg_consumer(const std::string& filename, const core::video_format_desc& format_desc, const std::string& codec, const std::string& options)\r
+               : filename_(filename)\r
                , video_outbuf_(1920*1080*8)\r
-               , audio_outbuf_(48000)\r
                , oc_(avformat_alloc_context(), av_free)\r
                , format_desc_(format_desc)\r
                , executor_(print())\r
                , file_write_executor_(print() + L"/output")\r
                , frame_number_(0)\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
+\r
                graph_->add_guide("frame-time", 0.5);\r
                graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
                graph_->set_color("write-time", diagnostics::color(0.5f, 0.5f, 0.1f));\r
@@ -124,7 +122,7 @@ public:
                        BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info(codec));\r
 \r
                //  Add the audio and video streams using the default format codecs     and initialize the codecs .\r
-               video_st_ = add_video_stream(video_codec->id, bitrate);\r
+               video_st_ = add_video_stream(video_codec->id, options);\r
                audio_st_ = add_audio_stream();\r
                                \r
                dump_format(oc_.get(), 0, filename_.c_str(), 1);\r
@@ -140,35 +138,21 @@ public:
 \r
        ~ffmpeg_consumer()\r
        {    \r
-               file_write_executor_.stop();\r
-               file_write_executor_.join();\r
-\r
                executor_.stop();\r
                executor_.join();\r
+\r
+               file_write_executor_.stop();\r
+               file_write_executor_.join();\r
                \r
-               try\r
-               {\r
-                       THROW_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");\r
+               LOG_ON_ERROR2(av_write_trailer(oc_.get()), "[ffmpeg_consumer]");\r
                \r
-                       audio_st_.reset();\r
-                       video_st_.reset();\r
+               audio_st_.reset();\r
+               video_st_.reset();\r
                          \r
-                       for(size_t i = 0; i < oc_->nb_streams; i++) \r
-                       {\r
-                               av_freep(&oc_->streams[i]->codec);\r
-                               av_freep(&oc_->streams[i]);\r
-                       }\r
-\r
-                       if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
-                               THROW_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.\r
-\r
-                       CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
-               }\r
-               catch(...)\r
-               {\r
-                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-               }\r
+               if (!(oc_->oformat->flags & AVFMT_NOFILE)) \r
+                       LOG_ON_ERROR2(avio_close(oc_->pb), "[ffmpeg_consumer]"); // Close the output ffmpeg.\r
 \r
+               CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
        }\r
                        \r
        std::wstring print() const\r
@@ -176,15 +160,11 @@ public:
                return L"ffmpeg[" + widen(filename_) + L"]";\r
        }\r
 \r
-       std::shared_ptr<AVStream> add_video_stream(enum CodecID codec_id, int bitrate)\r
+       std::shared_ptr<AVStream> add_video_stream(enum CodecID codec_id, const std::string& options)\r
        { \r
                auto st = av_new_stream(oc_.get(), 0);\r
-               if (!st) \r
-               {\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() \r
-                               << msg_info("Could not alloc video-stream")                             \r
-                               << boost::errinfo_api_function("av_new_stream"));\r
-               }\r
+               if (!st)                \r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream") << boost::errinfo_api_function("av_new_stream"));               \r
 \r
                auto encoder = avcodec_find_encoder(codec_id);\r
                if (!encoder)\r
@@ -193,44 +173,63 @@ public:
                auto c = st->codec;\r
 \r
                avcodec_get_context_defaults3(c, encoder);\r
-\r
-               bitrate *= 1000000;\r
-               \r
+                               \r
                c->codec_id                     = codec_id;\r
-               c->bit_rate                     = bitrate > 0 ? bitrate : format_desc_.width < 1280 ? 63*1000000 : 220*1000000;\r
                c->codec_type           = AVMEDIA_TYPE_VIDEO;\r
                c->width                        = format_desc_.width;\r
                c->height                       = format_desc_.height;\r
                c->time_base.den        = format_desc_.time_scale;\r
                c->time_base.num        = format_desc_.duration;\r
                c->gop_size                     = 25;\r
+               c->flags                   |= format_desc_.field_mode == core::field_mode::progressive ? 0 : (CODEC_FLAG_INTERLACED_ME | CODEC_FLAG_INTERLACED_DCT);\r
 \r
                if(c->codec_id == CODEC_ID_PRORES)\r
                {                       \r
-                       c->bit_rate     = bitrate > 0 ? bitrate : format_desc_.width < 1280 ? 42*1000000 : 147*1000000;\r
+                       c->bit_rate     = format_desc_.width < 1280 ? 63*1000000 : 220*1000000;\r
                        c->pix_fmt      = PIX_FMT_YUV422P10;\r
+                       THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
                }\r
                else if(c->codec_id == CODEC_ID_DNXHD)\r
                {\r
                        if(format_desc_.width < 1280 || format_desc_.height < 720)\r
                                BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("unsupported dimension"));\r
 \r
-                       c->bit_rate     = bitrate > 0 ? bitrate : 220*1000000;\r
+                       c->bit_rate     = 220*1000000;\r
                        c->pix_fmt      = PIX_FMT_YUV422P;\r
+                       \r
+                       THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
                }\r
                else if(c->codec_id == CODEC_ID_DVVIDEO)\r
                {\r
-                       c->bit_rate     = bitrate > 0 ? bitrate : format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
+                       c->bit_rate     = format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
                        c->pix_fmt      = PIX_FMT_YUV422P;\r
+                       \r
+                       c->width = format_desc_.height == 1280 ? 960  : c->width;\r
+\r
+                       if(format_desc_.duration == 1001)                       \r
+                               c->width = format_desc_.height == 1080 ? 1280 : c->width;                       \r
+                       else\r
+                               c->width = format_desc_.height == 1080 ? 1440 : c->width;\r
+                       \r
+                       THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
+               }\r
+               else if(c->codec_id == CODEC_ID_H264)\r
+               {                          \r
+                       c->pix_fmt = PIX_FMT_YUV420P;    \r
+                       av_opt_set(c->priv_data, "preset", "ultrafast", 0);\r
+                       av_opt_set(c->priv_data, "tune",   "film",   0);\r
+                       av_opt_set(c->priv_data, "crf",    "5",     0);\r
+                       \r
+                       THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
                }\r
-               //else if(st->codec->codec_id == CODEC_ID_H264)\r
-               //{                        \r
-               //      c->pix_fmt = PIX_FMT_YUV422P;    
-               //      THROW_ON_ERROR2(av_opt_set(c->priv_data, "preset", "slow", 0), "[ffmpeg_consumer]");\r
-               //}\r
                else\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("unsupported codec"));\r
+               {\r
+                       THROW_ON_ERROR2(av_set_options_string(c->priv_data, options.c_str(), "=", ":"), "[ffmpeg_consumer]");\r
+                       CASPAR_LOG(warning) << " Potentially unsupported output parameters.";\r
+               }\r
                \r
+               c->max_b_frames = 0; // b-franes not supported.\r
+\r
                if(oc_->oformat->flags & AVFMT_GLOBALHEADER)\r
                        c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
                \r
@@ -239,19 +238,17 @@ public:
 \r
                return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
                {\r
-                       avcodec_close(st->codec);\r
+                       LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");\r
+                       av_freep(&st->codec);\r
+                       av_freep(&st);\r
                });\r
        }\r
        \r
        std::shared_ptr<AVStream> add_audio_stream()\r
        {\r
                auto st = av_new_stream(oc_.get(), 1);\r
-               if (!st) \r
-               {\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() \r
-                               << msg_info("Could not alloc audio-stream")                             \r
-                               << boost::errinfo_api_function("av_new_stream"));\r
-               }\r
+               if(!st)\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate audio-stream") << boost::errinfo_api_function("av_new_stream"));               \r
 \r
                st->codec->codec_id                     = CODEC_ID_PCM_S16LE;\r
                st->codec->codec_type           = AVMEDIA_TYPE_AUDIO;\r
@@ -270,18 +267,18 @@ public:
 \r
                return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
                {\r
-                       avcodec_close(st->codec);\r
+                       LOG_ON_ERROR2(avcodec_close(st->codec), "[ffmpeg_consumer]");;\r
+                       av_freep(&st->codec);\r
+                       av_freep(&st);\r
                });\r
        }\r
-  \r
-       std::shared_ptr<AVPacket> encode_video_frame(const safe_ptr<core::read_frame>& frame)\r
-       { \r
-               auto c = video_st_->codec;\r
\r
-               if(!img_convert_ctx_) \r
+\r
+       std::shared_ptr<AVFrame> convert_video_frame(const safe_ptr<core::read_frame>& frame, AVCodecContext* c)\r
+       {\r
+               if(!sws_) \r
                {\r
-                       img_convert_ctx_.reset(sws_getContext(format_desc_.width, format_desc_.height, PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr), sws_freeContext);\r
-                       if (img_convert_ctx_ == nullptr) \r
+                       sws_.reset(sws_getContext(format_desc_.width, format_desc_.height, PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr), sws_freeContext);\r
+                       if (sws_ == nullptr) \r
                                BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));\r
                }\r
 \r
@@ -289,16 +286,24 @@ public:
                avpicture_fill(reinterpret_cast<AVPicture*>(av_frame.get()), const_cast<uint8_t*>(frame->image_data().begin()), PIX_FMT_BGRA, format_desc_.width, format_desc_.height);\r
                                \r
                std::shared_ptr<AVFrame> local_av_frame(avcodec_alloc_frame(), av_free);\r
-               local_av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;\r
-               local_av_frame->top_field_first  = format_desc_.field_mode == core::field_mode::upper;\r
-               local_av_frame->pts                              = (frame_number_ * video_st_->time_base.den) * video_st_->time_base.num;\r
-\r
                picture_buf_.resize(avpicture_get_size(c->pix_fmt, format_desc_.width, format_desc_.height));\r
                avpicture_fill(reinterpret_cast<AVPicture*>(local_av_frame.get()), picture_buf_.data(), c->pix_fmt, format_desc_.width, format_desc_.height);\r
 \r
-               sws_scale(img_convert_ctx_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
-                               \r
-               int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), local_av_frame.get()), "[ffmpeg_consumer]");\r
+               sws_scale(sws_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
+\r
+               return local_av_frame;\r
+       }\r
+  \r
+       std::shared_ptr<AVPacket> encode_video_frame(const safe_ptr<core::read_frame>& frame)\r
+       { \r
+               auto c = video_st_->codec;\r
\r
+               auto av_frame = convert_video_frame(frame, c);\r
+               av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;\r
+               av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper;\r
+               av_frame->pts                           = frame_number_++;\r
+\r
+               int out_size = THROW_ON_ERROR2(avcodec_encode_video(c, video_outbuf_.data(), video_outbuf_.size(), av_frame.get()), "[ffmpeg_consumer]");\r
                if(out_size > 0)\r
                {\r
                        safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
@@ -354,18 +359,21 @@ public:
                executor_.begin_invoke([=]\r
                {               \r
                        frame_timer_.restart();\r
+\r
                        auto video = encode_video_frame(frame);\r
                        auto audio = encode_audio_frame(frame);\r
+\r
                        graph_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
                        \r
                        file_write_executor_.begin_invoke([=]\r
                        {\r
                                write_timer_.restart();\r
+\r
                                if(video)\r
                                        av_write_frame(oc_.get(), video.get());\r
                                if(audio)\r
                                        av_write_frame(oc_.get(), audio.get());\r
-                               ++frame_number_;\r
+\r
                                graph_->update_value("write-time", write_timer_.elapsed()*format_desc_.fps*0.5);\r
                        });\r
                });\r
@@ -376,25 +384,25 @@ struct ffmpeg_consumer_proxy : public core::frame_consumer
 {\r
        const std::wstring      filename_;\r
        const bool                      key_only_;\r
-       const std::string       codec_;\r
-       const int                       bitrate_;\r
+       const std::wstring      codec_;\r
+       const std::wstring      options_;\r
 \r
        std::unique_ptr<ffmpeg_consumer> consumer_;\r
 \r
 public:\r
 \r
-       ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::string codec, int bitrate)\r
+       ffmpeg_consumer_proxy(const std::wstring& filename, bool key_only, const std::wstring codec, const std::wstring& options)\r
                : filename_(filename)\r
                , key_only_(key_only)\r
                , codec_(boost::to_lower_copy(codec))\r
-               , bitrate_(bitrate)\r
+               , options_(options)\r
        {\r
        }\r
        \r
-       virtual void initialize(const core::video_format_desc& format_desc, int, int)\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, codec_, bitrate_));\r
+               consumer_.reset(new ffmpeg_consumer(narrow(filename_), format_desc, narrow(codec_), narrow(options_)));\r
        }\r
        \r
        virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
@@ -407,6 +415,17 @@ public:
        {\r
                return consumer_ ? consumer_->print() : L"[ffmpeg_consumer]";\r
        }\r
+\r
+       virtual boost::property_tree::wptree info() const override\r
+       {\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"ffmpeg-consumer");\r
+               info.add(L"key-only", key_only_);\r
+               info.add(L"filename", filename_);\r
+               info.add(L"codec", codec_);\r
+               info.add(L"options", options_);\r
+               return info;\r
+       }\r
                \r
        virtual bool has_synchronization_clock() const override\r
        {\r
@@ -417,41 +436,42 @@ public:
        {\r
                return 1;\r
        }\r
+\r
+       virtual int index() const override\r
+       {\r
+               return 200;\r
+       }\r
 };     \r
 \r
-safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
 {\r
-       if(params.size() < 2 || params[0] != L"FILE")\r
+       if(params.size() < 1 || params[0] != L"FILE")\r
                return core::frame_consumer::empty();\r
        \r
-       // TODO: Ask stakeholders about case where file already exists.\r
-       boost::filesystem::remove(boost::filesystem::wpath(env::media_folder() + params[1])); // Delete the file if it exists\r
-       bool key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();\r
-\r
-       std::string codec = "dnxhd";\r
-       auto codec_it = std::find(params.begin(), params.end(), L"CODEC");\r
-       if(codec_it != params.end() && codec_it++ != params.end())\r
-               codec = narrow(*codec_it);\r
+       auto filename   = (params.size() > 1 ? params[1] : L"");\r
+       bool key_only   = get_param(L"KEY_ONLY", params, false);\r
+       auto codec              = get_param(L"CODEC", params, L"libx264");\r
+       auto options    = get_param(L"OPTIONS", params);\r
+       \r
+       if(codec == L"H264")\r
+               codec = L"libx264";\r
 \r
-       if(codec == "H264" || codec == "h264")\r
-               codec = "libx264";\r
+       if(codec == L"DVCPRO")\r
+               codec = L"dvvideo";\r
 \r
-       int bitrate = 0;        \r
-       auto bitrate_it = std::find(params.begin(), params.end(), L"BITRATE");\r
-       if(bitrate_it != params.end() && bitrate_it++ != params.end())\r
-               bitrate = boost::lexical_cast<int>(*bitrate_it);\r
+       boost::to_lower(options);\r
 \r
-       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + params[1], key_only, codec, bitrate);\r
+       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + filename, key_only, codec, options);\r
 }\r
 \r
-safe_ptr<core::frame_consumer> create_ffmpeg_consumer(const boost::property_tree::ptree& ptree)\r
+safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::ptree& ptree)\r
 {\r
        std::string filename = ptree.get<std::string>("path");\r
        auto key_only            = ptree.get("key-only", false);\r
-       auto codec                       = ptree.get("codec", "dnxhd");\r
-       auto bitrate             = ptree.get("bitrate", 0);\r
+       auto codec                       = ptree.get("codec", "libx264");\r
+       auto options             = ptree.get("options", "");\r
        \r
-       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + widen(filename), key_only, codec, bitrate);\r
+       return make_safe<ffmpeg_consumer_proxy>(env::media_folder() + widen(filename), key_only, widen(codec), widen(options));\r
 }\r
 \r
 }}\r