]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/consumer/ffmpeg_consumer.cpp
3601399: Added support for creating two separate files -- one for fill and one for...
[casparcg] / modules / ffmpeg / consumer / ffmpeg_consumer.cpp
index 37d290a66abd6a7bac67ac6c51930e6389d63021..7fd57edf41188080debc34a4c6487b20072009c6 100644 (file)
 \r
 #include "ffmpeg_consumer.h"\r
 \r
+#include "../producer/audio/audio_resampler.h"\r
+\r
 #include <core/mixer/read_frame.h>\r
 #include <core/mixer/audio/audio_util.h>\r
 #include <core/consumer/frame_consumer.h>\r
 #include <core/video_format.h>\r
 \r
 #include <common/concurrency/executor.h>\r
+#include <common/concurrency/future_util.h>\r
 #include <common/diagnostics/graph.h>\r
 #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
 #include <tbb/cache_aligned_allocator.h>\r
 #include <tbb/parallel_invoke.h>\r
 \r
+#include <boost/range/algorithm.hpp>\r
+#include <boost/range/algorithm_ext.hpp>\r
+#include <boost/lexical_cast.hpp>\r
+\r
 #include <string>\r
 \r
 #if defined(_MSC_VER)\r
@@ -57,12 +65,46 @@ extern "C"
        #include <libswscale/swscale.h>\r
        #include <libavutil/opt.h>\r
        #include <libavutil/pixdesc.h>\r
+       #include <libavutil/parseutils.h>\r
 }\r
 #if defined(_MSC_VER)\r
 #pragma warning (pop)\r
 #endif\r
 \r
 namespace caspar { namespace ffmpeg {\r
+       \r
+int av_opt_set(void *obj, const char *name, const char *val, int search_flags)\r
+{\r
+       AVClass* av_class = *(AVClass**)obj;\r
+\r
+       if((strcmp(name, "pix_fmt") == 0 || strcmp(name, "pixel_format") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)\r
+       {\r
+               AVCodecContext* c = (AVCodecContext*)obj;               \r
+               auto pix_fmt = av_get_pix_fmt(val);\r
+               if(pix_fmt == PIX_FMT_NONE)\r
+                       return -1;              \r
+               c->pix_fmt = pix_fmt;\r
+               return 0;\r
+       }\r
+       if((strcmp(name, "r") == 0 || strcmp(name, "frame_rate") == 0) && strcmp(av_class->class_name, "AVCodecContext") == 0)\r
+       {\r
+               AVCodecContext* c = (AVCodecContext*)obj;       \r
+\r
+               if(c->codec_type != AVMEDIA_TYPE_VIDEO)\r
+                       return -1;\r
+\r
+               AVRational rate;\r
+               int ret = av_parse_video_rate(&rate, val);\r
+               if(ret < 0)\r
+                       return ret;\r
+\r
+               c->time_base.num = rate.den;\r
+               c->time_base.den = rate.num;\r
+               return 0;\r
+       }\r
+\r
+       return ::av_opt_set(obj, name, val, search_flags);\r
+}\r
 \r
 struct option\r
 {\r
@@ -76,80 +118,104 @@ struct option
        }\r
 };\r
        \r
-void set_format(AVOutputFormat*& format, const std::string& value)\r
+struct output_format\r
 {\r
-       format = av_guess_format(value.c_str(), nullptr, nullptr);\r
-\r
-       if(format == nullptr)\r
-               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));        \r
-}\r
-\r
-bool parse_format(AVOutputFormat*& format, std::vector<option>& options)\r
-{      \r
-       auto format_it = std::find_if(options.begin(), options.end(), [](const option& o)\r
+       AVOutputFormat* format;\r
+       int                             width;\r
+       int                             height;\r
+       CodecID                 vcodec;\r
+       CodecID                 acodec;\r
+\r
+       output_format(const core::video_format_desc& format_desc, const std::string& filename, std::vector<option>& options)\r
+               : format(av_guess_format(nullptr, filename.c_str(), nullptr))\r
+               , width(format_desc.width)\r
+               , height(format_desc.height)\r
+               , vcodec(CODEC_ID_NONE)\r
+               , acodec(CODEC_ID_NONE)\r
        {\r
-               return o.name == "f" || o.name == "format";\r
-       });\r
-\r
-       if(format_it == options.end())\r
-               return false;\r
-\r
-       set_format(format, format_it->value);\r
-\r
-       options.erase(format_it);\r
-\r
-       return true;\r
-}\r
-\r
-void set_vcodec(CodecID& vcodec, const std::string& value)\r
-{      \r
-       vcodec = avcodec_find_encoder_by_name(value.c_str())->id;\r
-       if(vcodec == CODEC_ID_NONE)\r
-               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));\r
-}\r
+               boost::range::remove_erase_if(options, [&](const option& o)\r
+               {\r
+                       return set_opt(o.name, o.value);\r
+               });\r
+               \r
+               if(vcodec == CODEC_ID_NONE)\r
+                       vcodec = format->video_codec;\r
 \r
-bool parse_vcodec(CodecID& vcodec, std::vector<option>& options)\r
-{\r
-       auto vcodec_it = std::find_if(options.begin(), options.end(), [](const option& o)\r
+               if(acodec == CODEC_ID_NONE)\r
+                       acodec = format->audio_codec;\r
+               \r
+               if(vcodec == CODEC_ID_NONE)\r
+                       vcodec = CODEC_ID_H264;\r
+               \r
+               if(acodec == CODEC_ID_NONE)\r
+                       acodec = CODEC_ID_PCM_S16LE;\r
+       }\r
+       \r
+       bool set_opt(const std::string& name, const std::string& value)\r
        {\r
-               return o.name == "vcodec";\r
-       });\r
+               //if(name == "target")\r
+               //{ \r
+               //      enum { PAL, NTSC, FILM, UNKNOWN } norm = UNKNOWN;\r
+               //      \r
+               //      if(name.find("pal-") != std::string::npos)\r
+               //              norm = PAL;\r
+               //      else if(name.find("ntsc-") != std::string::npos)\r
+               //              norm = NTSC;\r
+\r
+               //      if(norm == UNKNOWN)\r
+               //              BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("target"));\r
+               //      \r
+               //      if (name.find("-dv") != std::string::npos) \r
+               //      {\r
+               //              set_opt("f", "dv");\r
+               //              set_opt("s", norm == PAL ? "720x576" : "720x480");\r
+               //              //set_opt("pix_fmt", name.find("-dv50") != std::string::npos ? "yuv422p" : norm == PAL ? "yuv420p" : "yuv411p");\r
+               //              //set_opt("ar", "48000");\r
+               //              //set_opt("ac", "2");\r
+               //      } \r
+               //}\r
+               if(name == "f")\r
+               {\r
+                       format = av_guess_format(value.c_str(), nullptr, nullptr);\r
 \r
-       if(vcodec_it == options.end())\r
-               return false;\r
-       \r
-       set_vcodec(vcodec, vcodec_it->value);\r
+                       if(format == nullptr)\r
+                               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("f"));\r
 \r
-       options.erase(vcodec_it);\r
+                       return true;\r
+               }\r
+               else if(name == "vcodec")\r
+               {\r
+                       auto c = avcodec_find_encoder_by_name(value.c_str());\r
+                       if(c == nullptr)\r
+                               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("vcodec"));\r
 \r
-       return true;\r
-}\r
+                       vcodec = avcodec_find_encoder_by_name(value.c_str())->id;\r
+                       return true;\r
 \r
-void set_pix_fmt(AVCodecContext* vcodec, const std::string& value)\r
-{\r
-       auto pix_fmt = av_get_pix_fmt(value.c_str());\r
-       if(pix_fmt == PIX_FMT_NONE)\r
-               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("pix_fmt"));\r
+               }\r
+               else if(name == "acodec")\r
+               {\r
+                       auto c = avcodec_find_encoder_by_name(value.c_str());\r
+                       if(c == nullptr)\r
+                               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("acodec"));\r
 \r
-       vcodec->pix_fmt = pix_fmt;\r
-}\r
+                       acodec = avcodec_find_encoder_by_name(value.c_str())->id;\r
 \r
-bool parse_pix_fmt(AVCodecContext* vcodec, std::vector<option>& options)\r
-{\r
-       auto pix_fmt_it = std::find_if(options.begin(), options.end(), [](const option& o)\r
-       {\r
-               return o.name == "pix_fmt" || o.name == "pixel_format";\r
-       });\r
-       \r
-       if(pix_fmt_it == options.end())\r
-               return false;\r
+                       return true;\r
+               }\r
+               else if(name == "s")\r
+               {\r
+                       if(av_parse_video_size(&width, &height, value.c_str()) < 0)\r
+                               BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("s"));\r
+                       \r
+                       return true;\r
+               }\r
 \r
-       set_pix_fmt(vcodec, pix_fmt_it->value);\r
-       \r
-       options.erase(pix_fmt_it);\r
+               return false;\r
+       }\r
+};\r
 \r
-       return true;\r
-}\r
+typedef std::vector<uint8_t, tbb::cache_aligned_allocator<uint8_t>>    byte_vector;\r
 \r
 struct ffmpeg_consumer : boost::noncopyable\r
 {              \r
@@ -159,61 +225,61 @@ struct ffmpeg_consumer : boost::noncopyable
        const core::video_format_desc                   format_desc_;\r
        \r
        const safe_ptr<diagnostics::graph>              graph_;\r
-       boost::timer                                                    frame_timer_;\r
-       boost::timer                                                    write_timer_;\r
 \r
-       executor                                                                executor_;\r
-       executor                                                                file_write_executor_;\r
-\r
-       // Audio\r
-       std::shared_ptr<AVStream>                               audio_st_;\r
+       executor                                                                encode_executor_;\r
        \r
-       // Video\r
+       std::shared_ptr<AVStream>                               audio_st_;\r
        std::shared_ptr<AVStream>                               video_st_;\r
-\r
-       std::vector<uint8_t>                                    video_outbuf_;\r
-       std::vector<uint8_t>                                    picture_buf_;\r
+       \r
+       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
 \r
-       int64_t                                                                 frame_number_;\r
+       int64_t                                                                 in_frame_number_;\r
+       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
                , 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
+               , encode_executor_(print())\r
+               , 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
 \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
+               graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
                graph_->set_text(print());\r
                diagnostics::register_graph(graph_);\r
 \r
-               executor_.set_capacity(8);\r
-               file_write_executor_.set_capacity(8);\r
+               encode_executor_.set_capacity(8);\r
 \r
-               oc_->oformat = av_guess_format(nullptr, filename_.c_str(), nullptr);\r
-               if (!oc_->oformat)\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not find suitable output format."));\r
-               \r
-               parse_format(oc_->oformat, options);\r
-\r
-               auto vcodec = oc_->oformat->video_codec;\r
-               parse_vcodec(vcodec, options);\r
-               \r
+               oc_->oformat = output_format_.format;\r
+                               \r
                THROW_ON_ERROR2(av_set_parameters(oc_.get(), nullptr), "[ffmpeg_consumer]");\r
 \r
                strcpy_s(oc_->filename, filename_.c_str());\r
                \r
-               //  Add the audio and video streams using the default format codecs     and initialize the codecs .\r
-               video_st_ = add_video_stream(vcodec, options);\r
-               audio_st_ = add_audio_stream();\r
+               //  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
+\r
+               if (!key_only)\r
+                       audio_st_ = add_audio_stream(options);\r
                                \r
                dump_format(oc_.get(), 0, filename_.c_str(), 1);\r
                 \r
@@ -223,20 +289,27 @@ public:
                                \r
                THROW_ON_ERROR2(av_write_header(oc_.get()), "[ffmpeg_consumer]");\r
 \r
+               if(options.size() > 0)\r
+               {\r
+                       BOOST_FOREACH(auto& option, options)\r
+                               CASPAR_LOG(warning) << L"Invalid option: -" << widen(option.name) << L" " << widen(option.value);\r
+               }\r
+\r
                CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
        }\r
 \r
        ~ffmpeg_consumer()\r
        {    \r
-               executor_.stop();\r
-               executor_.join();\r
+               encode_executor_.stop_execute_rest();\r
+               encode_executor_.join();\r
 \r
-               file_write_executor_.stop();\r
-               file_write_executor_.join();\r
+               // Flush\r
+               LOG_ON_ERROR2(av_interleaved_write_frame(oc_.get(), nullptr), "[ffmpeg_consumer]");\r
                \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
@@ -250,61 +323,62 @@ public:
                return L"ffmpeg[" + widen(filename_) + L"]";\r
        }\r
 \r
-       std::shared_ptr<AVStream> add_video_stream(CodecID vcodec, std::vector<option>& options)\r
+       std::shared_ptr<AVStream> add_video_stream(std::vector<option>& options)\r
        { \r
+               if(output_format_.vcodec == CODEC_ID_NONE)\r
+                       return nullptr;\r
+\r
                auto st = av_new_stream(oc_.get(), 0);\r
                if (!st)                \r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Could not allocate video-stream") << boost::errinfo_api_function("av_new_stream"));               \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(vcodec);\r
+               auto encoder = avcodec_find_encoder(output_format_.vcodec);\r
                if (!encoder)\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Codec not found."));\r
 \r
                auto c = st->codec;\r
 \r
                avcodec_get_context_defaults3(c, encoder);\r
                                \r
-               c->codec_id                     = vcodec;\r
+               c->codec_id                     = output_format_.vcodec;\r
                c->codec_type           = AVMEDIA_TYPE_VIDEO;\r
-               c->width                        = format_desc_.width;\r
-               c->height                       = format_desc_.height;\r
+               c->width                        = output_format_.width;\r
+               c->height                       = output_format_.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
+               if(c->pix_fmt == PIX_FMT_NONE)\r
+                       c->pix_fmt = PIX_FMT_YUV420P;\r
 \r
                if(c->codec_id == CODEC_ID_PRORES)\r
                {                       \r
-                       c->bit_rate     = format_desc_.width < 1280 ? 63*1000000 : 220*1000000;\r
+                       c->bit_rate     = c->width < 1280 ? 63*1000000 : 220*1000000;\r
                        c->pix_fmt      = PIX_FMT_YUV422P10;\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
+                       if(c->width < 1280 || c->height < 720)\r
+                               BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Unsupported video dimensions."));\r
 \r
                        c->bit_rate     = 220*1000000;\r
                        c->pix_fmt      = PIX_FMT_YUV422P;\r
                }\r
                else if(c->codec_id == CODEC_ID_DVVIDEO)\r
                {\r
-                       c->bit_rate     = format_desc_.width < 1280 ? 50*1000000 : 100*1000000;\r
-                       c->width = format_desc_.height == 1280 ? 960  : c->width;\r
+                       c->width = c->height == 1280 ? 960  : c->width;\r
                        \r
-                       //if(format_desc_.format == core::video_format::ntsc)\r
-                       //      c->pix_fmt = PIX_FMT_YUV411P;\r
-                       //else if(format_desc_.format == core::video_format::pal)\r
-                       //      c->pix_fmt = PIX_FMT_YUV420P;\r
-\r
-                       c->pix_fmt = PIX_FMT_YUV411P;\r
-\r
-                       if(c->bit_rate >= 50*1000000) // dv50\r
+                       if(format_desc_.format == core::video_format::ntsc)\r
+                               c->pix_fmt = PIX_FMT_YUV411P;\r
+                       else if(format_desc_.format == core::video_format::pal)\r
+                               c->pix_fmt = PIX_FMT_YUV420P;\r
+                       else // dv50\r
                                c->pix_fmt = PIX_FMT_YUV422P;\r
                        \r
                        if(format_desc_.duration == 1001)                       \r
-                               c->width = format_desc_.height == 1080 ? 1280 : c->width;                       \r
+                               c->width = c->height == 1080 ? 1280 : c->width;                 \r
                        else\r
-                               c->width = format_desc_.height == 1080 ? 1440 : c->width;                       \r
+                               c->width = c->height == 1080 ? 1440 : c->width;                 \r
                }\r
                else if(c->codec_id == CODEC_ID_H264)\r
                {                          \r
@@ -320,23 +394,24 @@ public:
                {\r
                        c->pix_fmt = PIX_FMT_ARGB;\r
                }\r
-               else\r
-               {\r
-                       BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported output parameters."));\r
-               }\r
-\r
-               parse_pix_fmt(c, options);\r
-               \r
+                               \r
                c->max_b_frames = 0; // b-frames not supported.\r
-\r
-               BOOST_FOREACH(auto& option, options)\r
-                       THROW_ON_ERROR2(av_opt_set(c, option.name.c_str(), option.value.c_str(), AV_OPT_SEARCH_CHILDREN), "[ffmpeg_consumer]");\r
                                \r
-               if(oc_->oformat->flags & AVFMT_GLOBALHEADER)\r
+               boost::range::remove_erase_if(options, [&](const option& o)\r
+               {\r
+                       return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1 ||\r
+                                  ffmpeg::av_opt_set(c->priv_data, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;\r
+               });\r
+                               \r
+               if(output_format_.format->flags & AVFMT_GLOBALHEADER)\r
                        c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
                \r
                c->thread_count = boost::thread::hardware_concurrency();\r
-               THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
+               if(avcodec_open(c, encoder) < 0)\r
+               {\r
+                       c->thread_count = 1;\r
+                       THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
+               }\r
 \r
                return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
                {\r
@@ -345,27 +420,42 @@ public:
                        av_freep(&st);\r
                });\r
        }\r
-       \r
-       std::shared_ptr<AVStream> add_audio_stream()\r
+               \r
+       std::shared_ptr<AVStream> add_audio_stream(std::vector<option>& options)\r
        {\r
+               if(output_format_.acodec == CODEC_ID_NONE)\r
+                       return nullptr;\r
+\r
                auto st = av_new_stream(oc_.get(), 1);\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
-               st->codec->sample_rate          = 48000;\r
-               st->codec->channels                     = 2;\r
-               st->codec->sample_fmt           = SAMPLE_FMT_S16;\r
                \r
-               if(oc_->oformat->flags & AVFMT_GLOBALHEADER)\r
-                       st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
-               \r
-               auto codec = avcodec_find_encoder(st->codec->codec_id);\r
-               if (!codec)\r
+               auto encoder = avcodec_find_encoder(output_format_.acodec);\r
+               if (!encoder)\r
                        BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("codec not found"));\r
+               \r
+               auto c = st->codec;\r
+\r
+               avcodec_get_context_defaults3(c, encoder);\r
+\r
+               c->codec_id                     = output_format_.acodec;\r
+               c->codec_type           = AVMEDIA_TYPE_AUDIO;\r
+               c->sample_rate          = 48000;\r
+               c->channels                     = 2;\r
+               c->sample_fmt           = SAMPLE_FMT_S16;\r
+\r
+               if(output_format_.vcodec == CODEC_ID_FLV1)              \r
+                       c->sample_rate  = 44100;                \r
 \r
-               THROW_ON_ERROR2(avcodec_open(st->codec, codec), "[ffmpeg_consumer]");\r
+               if(output_format_.format->flags & AVFMT_GLOBALHEADER)\r
+                       c->flags |= CODEC_FLAG_GLOBAL_HEADER;\r
+                               \r
+               boost::range::remove_erase_if(options, [&](const option& o)\r
+               {\r
+                       return ffmpeg::av_opt_set(c, o.name.c_str(), o.value.c_str(), AV_OPT_SEARCH_CHILDREN) > -1;\r
+               });\r
+\r
+               THROW_ON_ERROR2(avcodec_open(c, encoder), "[ffmpeg_consumer]");\r
 \r
                return std::shared_ptr<AVStream>(st, [](AVStream* st)\r
                {\r
@@ -375,7 +465,7 @@ public:
                });\r
        }\r
 \r
-       std::shared_ptr<AVFrame> convert_video_frame(const safe_ptr<core::read_frame>& frame, AVCodecContext* c)\r
+       std::shared_ptr<AVFrame> convert_video(core::read_frame& frame, AVCodecContext* c)\r
        {\r
                if(!sws_) \r
                {\r
@@ -384,129 +474,223 @@ public:
                                BOOST_THROW_EXCEPTION(caspar_exception() << msg_info("Cannot initialize the conversion context"));\r
                }\r
 \r
-               std::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);\r
-               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
-               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
+               std::shared_ptr<AVFrame> in_frame(avcodec_alloc_frame(), av_free);\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
 \r
-               sws_scale(sws_.get(), av_frame->data, av_frame->linesize, 0, c->height, local_av_frame->data, local_av_frame->linesize);\r
+               sws_scale(sws_.get(), in_frame->data, in_frame->linesize, 0, format_desc_.height, out_frame->data, out_frame->linesize);\r
 \r
-               return local_av_frame;\r
+               return out_frame;\r
        }\r
   \r
-       std::shared_ptr<AVPacket> encode_video_frame(const safe_ptr<core::read_frame>& frame)\r
+       void encode_video_frame(core::read_frame& frame)\r
        { \r
                auto c = video_st_->codec;\r
+               \r
+               auto in_time  = static_cast<double>(in_frame_number_) / format_desc_.fps;\r
+               auto out_time = static_cast<double>(out_frame_number_) / (static_cast<double>(c->time_base.den) / static_cast<double>(c->time_base.num));\r
+               \r
+               in_frame_number_++;\r
+\r
+               if(out_time - in_time > 0.01)\r
+                       return;\r
  \r
-               auto av_frame = convert_video_frame(frame, c);\r
+               auto av_frame = convert_video(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
+               av_frame->pts                           = out_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
+               if(out_size == 0)\r
+                       return;\r
+                               \r
+               safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
                {\r
-                       safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
-                       {\r
-                               av_free_packet(p);\r
-                               delete p;\r
-                       });\r
-                       av_init_packet(pkt.get());\r
+                       av_free_packet(p);\r
+                       delete p;\r
+               });\r
+               av_init_packet(pkt.get());\r
  \r
-                       if (c->coded_frame->pts != AV_NOPTS_VALUE)\r
-                               pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);\r
+               if (c->coded_frame->pts != AV_NOPTS_VALUE)\r
+                       pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, video_st_->time_base);\r
 \r
-                       if(c->coded_frame->key_frame)\r
-                               pkt->flags |= AV_PKT_FLAG_KEY;\r
+               if(c->coded_frame->key_frame)\r
+                       pkt->flags |= AV_PKT_FLAG_KEY;\r
 \r
-                       pkt->stream_index       = video_st_->index;\r
-                       pkt->data                       = video_outbuf_.data();\r
-                       pkt->size                       = out_size;\r
\r
-                       av_dup_packet(pkt.get());\r
-                       return pkt;\r
-               }       \r
-               return nullptr;\r
+               pkt->stream_index       = video_st_->index;\r
+               pkt->data                       = video_outbuf_.data();\r
+               pkt->size                       = out_size;\r
+                       \r
+               av_interleaved_write_frame(oc_.get(), pkt.get());               \r
        }\r
                \r
-       std::shared_ptr<AVPacket> encode_audio_frame(const safe_ptr<core::read_frame>& frame)\r
+       byte_vector convert_audio(core::read_frame& frame, AVCodecContext* c)\r
+       {\r
+               if(!swr_)               \r
+                       swr_.reset(new audio_resampler(c->channels, format_desc_.audio_channels, \r
+                                                                                  c->sample_rate, format_desc_.audio_sample_rate,\r
+                                                                                  c->sample_fmt, AV_SAMPLE_FMT_S32));\r
+               \r
+\r
+               auto audio_data = frame.audio_data();\r
+\r
+               std::vector<int8_t,  tbb::cache_aligned_allocator<int8_t>> audio_resample_buffer;\r
+               std::copy(reinterpret_cast<const uint8_t*>(audio_data.begin()), \r
+                                 reinterpret_cast<const uint8_t*>(audio_data.begin()) + audio_data.size()*4, \r
+                                 std::back_inserter(audio_resample_buffer));\r
+               \r
+               audio_resample_buffer = swr_->resample(std::move(audio_resample_buffer));\r
+               \r
+               return byte_vector(audio_resample_buffer.begin(), audio_resample_buffer.end());\r
+       }\r
+\r
+       void encode_audio_frame(core::read_frame& frame)\r
        {                       \r
                auto c = audio_st_->codec;\r
 \r
-               auto audio_data = core::audio_32_to_16(frame->audio_data());\r
+               boost::range::push_back(audio_buf_, convert_audio(frame, c));\r
                \r
-               safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
+               std::size_t frame_size = c->frame_size;\r
+               auto input_audio_size = frame_size * av_get_bytes_per_sample(c->sample_fmt) * c->channels;\r
+               \r
+               while(audio_buf_.size() >= input_audio_size)\r
                {\r
-                       av_free_packet(p);\r
-                       delete p;\r
-               });\r
-               av_init_packet(pkt.get());\r
+                       safe_ptr<AVPacket> pkt(new AVPacket, [](AVPacket* p)\r
+                       {\r
+                               av_free_packet(p);\r
+                               delete p;\r
+                       });\r
+                       av_init_packet(pkt.get());\r
+\r
+                       if(frame_size > 1)\r
+                       {                                                               \r
+                               pkt->size = avcodec_encode_audio(c, audio_outbuf_.data(), audio_outbuf_.size(), reinterpret_cast<short*>(audio_buf_.data()));\r
+                               audio_buf_.erase(audio_buf_.begin(), audio_buf_.begin() + input_audio_size);\r
+                       }\r
+                       else\r
+                       {\r
+                               audio_outbuf_ = std::move(audio_buf_);          \r
+                               audio_buf_.clear();\r
+                               pkt->size = audio_outbuf_.size();\r
+                               pkt->data = audio_outbuf_.data();\r
+                       }\r
                \r
-               if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)\r
-                       pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);\r
+                       if(pkt->size == 0)\r
+                               return;\r
 \r
-               pkt->flags               |= AV_PKT_FLAG_KEY;\r
-               pkt->stream_index = audio_st_->index;\r
-               pkt->size                = audio_data.size()*2;\r
-               pkt->data                = reinterpret_cast<uint8_t*>(audio_data.data());\r
+                       if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)\r
+                               pkt->pts = av_rescale_q(c->coded_frame->pts, c->time_base, audio_st_->time_base);\r
+\r
+                       pkt->flags               |= AV_PKT_FLAG_KEY;\r
+                       pkt->stream_index = audio_st_->index;\r
+                       pkt->data                 = reinterpret_cast<uint8_t*>(audio_outbuf_.data());\r
                \r
-               av_dup_packet(pkt.get());\r
-               return pkt;\r
+                       av_interleaved_write_frame(oc_.get(), pkt.get());\r
+               }\r
        }\r
                 \r
        void send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               executor_.begin_invoke([=]\r
+               encode_executor_.begin_invoke([=]\r
                {               \r
-                       frame_timer_.restart();\r
+                       boost::timer frame_timer;\r
 \r
-                       auto video = encode_video_frame(frame);\r
-                       auto audio = encode_audio_frame(frame);\r
+                       encode_video_frame(*frame);\r
 \r
-                       graph_->set_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
+                       if (!key_only_)\r
+                               encode_audio_frame(*frame);\r
 \r
-                               graph_->set_value("write-time", write_timer_.elapsed()*format_desc_.fps*0.5);\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
+       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
+       }\r
 };\r
 \r
 struct ffmpeg_consumer_proxy : public core::frame_consumer\r
 {\r
        const std::wstring                              filename_;\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 bool send(const safe_ptr<core::read_frame>& frame) override\r
+       virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
        {\r
-               consumer_->send(frame);\r
-               return true;\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
        virtual std::wstring print() const override\r
@@ -519,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
@@ -542,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
@@ -563,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