]> git.sesse.net Git - casparcg/blobdiff - modules/ffmpeg/producer/muxer/frame_muxer.cpp
[ffmpeg] Ported 2.0.7 ffmpeg producer to 2.1.0 while still keeping the usage of the...
[casparcg] / modules / ffmpeg / producer / muxer / frame_muxer.cpp
index 463ced26b4a060818e34ddb602c7b591d3bc3aec..bf4850ddc8bbfdf98da2b1b228ba0375ba7e75a7 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\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
-*\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 "frame_muxer.h"\r
-\r
-#include "../filter/filter.h"\r
-#include "../util/util.h"\r
-\r
-#include <core/producer/frame_producer.h>\r
-#include <core/frame/draw_frame.h>\r
-#include <core/frame/frame_transform.h>\r
-#include <core/frame/pixel_format.h>\r
-#include <core/frame/frame_factory.h>\r
-#include <core/frame/frame.h>\r
-\r
-#include <common/env.h>\r
-#include <common/except.h>\r
-#include <common/log.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (push)\r
-#pragma warning (disable : 4244)\r
-#endif\r
-extern "C" \r
-{\r
-       #define __STDC_CONSTANT_MACROS\r
-       #define __STDC_LIMIT_MACROS\r
-       #include <libavcodec/avcodec.h>\r
-       #include <libavformat/avformat.h>\r
-}\r
-#if defined(_MSC_VER)\r
-#pragma warning (pop)\r
-#endif\r
-\r
-#include <common/assert.h>\r
-#include <boost/foreach.hpp>\r
-#include <boost/range/algorithm_ext/push_back.hpp>\r
-#include <boost/algorithm/string/predicate.hpp>\r
-\r
-#include <deque>\r
-#include <queue>\r
-#include <vector>\r
-\r
-using namespace caspar::core;\r
-\r
-namespace caspar { namespace ffmpeg {\r
-       \r
-struct frame_muxer::impl : boost::noncopyable\r
-{      \r
-       std::queue<core::mutable_frame>                                 video_stream_;\r
-       core::audio_buffer                                                              audio_stream_;\r
-       std::queue<draw_frame>                                                  frame_buffer_;\r
-       display_mode                                                                    display_mode_;\r
-       const double                                                                    in_fps_;\r
-       const video_format_desc                                                 format_desc_;\r
-       \r
-       std::vector<int>                                                                audio_cadence_;\r
-                       \r
-       spl::shared_ptr<core::frame_factory>                    frame_factory_;\r
-       \r
-       filter                                                                                  filter_;\r
-       const std::wstring                                                              filter_str_;\r
-       bool                                                                                    force_deinterlacing_;\r
-               \r
-       impl(double in_fps, const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filter_str)\r
-               : display_mode_(display_mode::invalid)\r
-               , in_fps_(in_fps)\r
-               , format_desc_(format_desc)\r
-               , audio_cadence_(format_desc_.audio_cadence)\r
-               , frame_factory_(frame_factory)\r
-               , filter_str_(filter_str)\r
-               , force_deinterlacing_(env::properties().get(L"configuration.force-deinterlace", true))\r
-       {               \r
-               // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601)\r
-               // This cadence fills the audio mixer most optimally.\r
-               boost::range::rotate(audio_cadence_, std::end(audio_cadence_)-1);\r
-       }\r
-       \r
-       void push_video(const std::shared_ptr<AVFrame>& video)\r
-       {               \r
-               if(!video)\r
-                       return;\r
-\r
-               if(!video->data[0])\r
-               {\r
-                       auto empty_frame = frame_factory_->create_frame(this, core::pixel_format_desc(core::pixel_format::invalid));\r
-                       video_stream_.push(std::move(empty_frame));\r
-                       display_mode_ = display_mode::simple;\r
-               }\r
-               else\r
-               {\r
-                       if(display_mode_ == display_mode::invalid)\r
-                               update_display_mode(video);\r
-                               \r
-                       filter_.push(video);\r
-                       BOOST_FOREACH(auto& av_frame, filter_.poll_all())                       \r
-                               video_stream_.push(make_frame(this, av_frame, format_desc_.fps, *frame_factory_));                      \r
-               }\r
-\r
-               merge();\r
-       }\r
-       \r
-       void push_audio(const std::shared_ptr<AVFrame>& audio)\r
-       {\r
-               if(!audio)\r
-                       return;\r
-\r
-               if(!audio->data[0])             \r
-               {\r
-                       boost::range::push_back(audio_stream_, core::audio_buffer(audio_cadence_.front(), 0));  \r
-               }\r
-               else\r
-               {\r
-                       auto ptr = reinterpret_cast<int32_t*>(audio->data[0]);\r
-                       audio_stream_.insert(audio_stream_.end(), ptr, ptr + audio->linesize[0]/sizeof(int32_t));\r
-               }\r
-\r
-               merge();\r
-       }\r
-       \r
-       bool video_ready() const\r
-       {               \r
-               switch(display_mode_)\r
-               {\r
-               case display_mode::deinterlace_bob_reinterlace:                                 \r
-               case display_mode::interlace:   \r
-               case display_mode::half:\r
-                       return video_stream_.size() >= 2;\r
-               default:                                                                                \r
-                       return video_stream_.size() >= 1;\r
-               }\r
-       }\r
-       \r
-       bool audio_ready() const\r
-       {\r
-               switch(display_mode_)\r
-               {\r
-               case display_mode::duplicate:                                   \r
-                       return audio_stream_.size() >= static_cast<size_t>(audio_cadence_[0] + audio_cadence_[1 % audio_cadence_.size()]);\r
-               default:                                                                                \r
-                       return audio_stream_.size() >= static_cast<size_t>(audio_cadence_.front());\r
-               }\r
-       }\r
-\r
-       bool empty() const\r
-       {\r
-               return frame_buffer_.empty();\r
-       }\r
-\r
-       core::draw_frame front() const\r
-       {\r
-               return frame_buffer_.front();\r
-       }\r
-\r
-       void pop()\r
-       {\r
-               frame_buffer_.pop();\r
-       }\r
-               \r
-       void merge()\r
-       {\r
-               while(video_ready() && audio_ready() && display_mode_ != display_mode::invalid)\r
-               {                               \r
-                       auto frame1                     = pop_video();\r
-                       frame1.audio_data()     = pop_audio();\r
-\r
-                       switch(display_mode_)\r
-                       {\r
-                       case display_mode::simple:                                              \r
-                       case display_mode::deinterlace_bob:                             \r
-                       case display_mode::deinterlace: \r
-                               {\r
-                                       frame_buffer_.push(core::draw_frame(std::move(frame1)));\r
-                                       break;\r
-                               }\r
-                       case display_mode::interlace:                                   \r
-                       case display_mode::deinterlace_bob_reinterlace: \r
-                               {                               \r
-                                       auto frame2 = pop_video();\r
-\r
-                                       frame_buffer_.push(core::draw_frame::interlace(\r
-                                               core::draw_frame(std::move(frame1)),\r
-                                               core::draw_frame(std::move(frame2)),\r
-                                               format_desc_.field_mode));      \r
-                                       break;\r
-                               }\r
-                       case display_mode::duplicate:   \r
-                               {\r
-                                       boost::range::push_back(frame1.audio_data(), pop_audio());\r
-\r
-                                       auto draw_frame = core::draw_frame(std::move(frame1));\r
-                                       frame_buffer_.push(draw_frame);\r
-                                       frame_buffer_.push(draw_frame);\r
-                                       break;\r
-                               }\r
-                       case display_mode::half:        \r
-                               {                               \r
-                                       pop_video(); // Throw away\r
-\r
-                                       frame_buffer_.push(core::draw_frame(std::move(frame1)));\r
-                                       break;\r
-                               }\r
-                       default:\r
-                               CASPAR_THROW_EXCEPTION(invalid_operation());\r
-                       }\r
-               }\r
-       }\r
-       \r
-       core::mutable_frame pop_video()\r
-       {\r
-               auto frame = std::move(video_stream_.front());\r
-               video_stream_.pop();            \r
-               return std::move(frame);\r
-       }\r
-\r
-       core::audio_buffer pop_audio()\r
-       {\r
-               if(audio_stream_.size() < audio_cadence_.front())\r
-                       CASPAR_THROW_EXCEPTION(out_of_range());\r
-\r
-               auto begin = audio_stream_.begin();\r
-               auto end   = begin + audio_cadence_.front();\r
-\r
-               core::audio_buffer samples(begin, end);\r
-               audio_stream_.erase(begin, end);\r
-               \r
-               boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
-\r
-               return samples;\r
-       }\r
-                               \r
-       void update_display_mode(const std::shared_ptr<AVFrame>& frame)\r
-       {\r
-               std::wstring filter_str = filter_str_;\r
-\r
-               display_mode_ = display_mode::simple;\r
-\r
-               auto mode = get_mode(*frame);\r
-               if(mode == core::field_mode::progressive && frame->height < 720 && in_fps_ < 50.0) // SD frames are interlaced. Probably incorrect meta-data. Fix it.\r
-                       mode = core::field_mode::upper;\r
-\r
-               auto fps  = in_fps_;\r
-\r
-               if(filter::is_deinterlacing(filter_str_))\r
-                       mode = core::field_mode::progressive;\r
-\r
-               if(filter::is_double_rate(filter_str_))\r
-                       fps *= 2;\r
-                       \r
-               display_mode_ = get_display_mode(mode, fps, format_desc_.field_mode, format_desc_.fps);\r
-                       \r
-               if((frame->height != 480 || format_desc_.height != 486) && // don't deinterlace for NTSC DV\r
-                               display_mode_ == display_mode::simple && mode != core::field_mode::progressive && format_desc_.field_mode != core::field_mode::progressive && \r
-                               frame->height != format_desc_.height)\r
-               {\r
-                       display_mode_ = display_mode::deinterlace_bob_reinterlace; // The frame will most likely be scaled, we need to deinterlace->reinterlace \r
-               }\r
-\r
-               // ALWAYS de-interlace, until we have GPU de-interlacing.\r
-               if(force_deinterlacing_ && frame->interlaced_frame && display_mode_ != display_mode::deinterlace_bob && display_mode_ != display_mode::deinterlace)\r
-                       display_mode_ = display_mode::deinterlace_bob_reinterlace;\r
-               \r
-               if(display_mode_ == display_mode::deinterlace)\r
-                       filter_str = append_filter(filter_str, L"YADIF=0:-1");\r
-               else if(display_mode_ == display_mode::deinterlace_bob || display_mode_ == display_mode::deinterlace_bob_reinterlace)\r
-                       filter_str = append_filter(filter_str, L"YADIF=1:-1");\r
-\r
-               if(display_mode_ == display_mode::invalid)\r
-               {\r
-                       CASPAR_LOG(warning) << L"[frame_muxer] Auto-transcode: Failed to detect display-mode.";\r
-                       display_mode_ = display_mode::simple;\r
-               }\r
-\r
-               if(frame->height == 480) // NTSC DV\r
-               {\r
-                       auto pad_str = L"PAD=" + boost::lexical_cast<std::wstring>(frame->width) + L":486:0:2:black";\r
-                       filter_str = append_filter(filter_str, pad_str);\r
-               }\r
-\r
-               filter_ = filter(filter_str);\r
-               CASPAR_LOG(info) << L"[frame_muxer] " << display_mode_ << L" " << print_mode(frame->width, frame->height, in_fps_, frame->interlaced_frame > 0);\r
-       }\r
-       \r
-       uint32_t calc_nb_frames(uint32_t nb_frames) const\r
-       {\r
-               uint64_t nb_frames2 = nb_frames;\r
-               \r
-               if(filter_.is_double_rate()) // Take into account transformations in filter.\r
-                       nb_frames2 *= 2;\r
-\r
-               switch(display_mode_) // Take into account transformation in run.\r
-               {\r
-               case display_mode::deinterlace_bob_reinterlace:\r
-               case display_mode::interlace:   \r
-               case display_mode::half:\r
-                       nb_frames2 /= 2;\r
-                       break;\r
-               case display_mode::duplicate:\r
-                       nb_frames2 *= 2;\r
-                       break;\r
-               }\r
-\r
-               return static_cast<uint32_t>(nb_frames2);\r
-       }\r
-\r
-       void clear()\r
-       {\r
-               while(!video_stream_.empty())\r
-                       video_stream_.pop();    \r
-\r
-               audio_stream_.clear();\r
-\r
-               while(!frame_buffer_.empty())\r
-                       frame_buffer_.pop();\r
-\r
-               filter_ = filter(filter_.filter_str());\r
-       }\r
-};\r
-\r
-frame_muxer::frame_muxer(double in_fps, const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::wstring& filter)\r
-       : impl_(new impl(in_fps, frame_factory, format_desc, filter)){}\r
-void frame_muxer::push_video(const std::shared_ptr<AVFrame>& frame){impl_->push_video(frame);}\r
-void frame_muxer::push_audio(const std::shared_ptr<AVFrame>& frame){impl_->push_audio(frame);}\r
-bool frame_muxer::empty() const{return impl_->empty();}\r
-core::draw_frame frame_muxer::front() const{return impl_->front();}\r
-void frame_muxer::pop(){return impl_->pop();}\r
-void frame_muxer::clear(){impl_->clear();}\r
-uint32_t frame_muxer::calc_nb_frames(uint32_t nb_frames) const {return impl_->calc_nb_frames(nb_frames);}\r
-bool frame_muxer::video_ready() const{return impl_->video_ready();}\r
-bool frame_muxer::audio_ready() const{return impl_->audio_ready();}\r
-\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../../StdAfx.h"
+
+#include "frame_muxer.h"
+
+#include "../filter/filter.h"
+#include "../util/util.h"
+#include "../../ffmpeg.h"
+
+#include <core/producer/frame_producer.h>
+#include <core/frame/draw_frame.h>
+#include <core/frame/frame_transform.h>
+#include <core/frame/pixel_format.h>
+#include <core/frame/frame_factory.h>
+#include <core/frame/frame.h>
+#include <core/frame/audio_channel_layout.h>
+
+#include <common/env.h>
+#include <common/except.h>
+#include <common/log.h>
+
+#if defined(_MSC_VER)
+#pragma warning (push)
+#pragma warning (disable : 4244)
+#endif
+extern "C"
+{
+       #define __STDC_CONSTANT_MACROS
+       #define __STDC_LIMIT_MACROS
+       #include <libavcodec/avcodec.h>
+       #include <libavformat/avformat.h>
+}
+#if defined(_MSC_VER)
+#pragma warning (pop)
+#endif
+
+#include <common/assert.h>
+#include <boost/range/algorithm_ext/push_back.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/optional.hpp>
+
+#include <deque>
+#include <queue>
+#include <vector>
+
+using namespace caspar::core;
+
+namespace caspar { namespace ffmpeg {
+struct av_frame_format
+{
+       int                                                                             pix_format;
+       std::array<int, AV_NUM_DATA_POINTERS>   line_sizes;
+       int                                                                             width;
+       int                                                                             height;
+
+       av_frame_format(const AVFrame& frame)
+               : pix_format(frame.format)
+               , width(frame.width)
+               , height(frame.height)
+       {
+               boost::copy(frame.linesize, line_sizes.begin());
+       }
+
+       bool operator==(const av_frame_format& other) const
+       {
+               return pix_format == other.pix_format
+                       && line_sizes == other.line_sizes
+                       && width == other.width
+                       && height == other.height;
+       }
+
+       bool operator!=(const av_frame_format& other) const
+       {
+               return !(*this == other);
+       }
+};
+
+struct frame_muxer::impl : boost::noncopyable
+{
+       std::queue<std::queue<core::mutable_frame>>             video_streams_;
+       std::queue<core::mutable_audio_buffer>                  audio_streams_;
+       std::queue<core::draw_frame>                                    frame_buffer_;
+       display_mode                                                                    display_mode_                   = display_mode::invalid;
+       const boost::rational<int>                                              in_framerate_;
+       const video_format_desc                                                 format_desc_;
+       const audio_channel_layout                                              audio_channel_layout_;
+
+       std::vector<int>                                                                audio_cadence_                  = format_desc_.audio_cadence;
+
+       spl::shared_ptr<core::frame_factory>                    frame_factory_;
+       boost::optional<av_frame_format>                                previously_filtered_frame_;
+
+       std::unique_ptr<filter>                                                 filter_;
+       const std::wstring                                                              filter_str_;
+       const bool                                                                              multithreaded_filter_;
+       bool                                                                                    force_deinterlacing_    = env::properties().get(L"configuration.force-deinterlace", false);
+
+       mutable boost::mutex                                                    out_framerate_mutex_;
+       boost::rational<int>                                                    out_framerate_;
+
+       impl(
+                       boost::rational<int> in_framerate,
+                       const spl::shared_ptr<core::frame_factory>& frame_factory,
+                       const core::video_format_desc& format_desc,
+                       const core::audio_channel_layout& channel_layout,
+                       const std::wstring& filter_str,
+                       bool multithreaded_filter)
+               : in_framerate_(in_framerate)
+               , format_desc_(format_desc)
+               , audio_channel_layout_(channel_layout)
+               , frame_factory_(frame_factory)
+               , filter_str_(filter_str)
+               , multithreaded_filter_(multithreaded_filter)
+       {
+               video_streams_.push(std::queue<core::mutable_frame>());
+               audio_streams_.push(core::mutable_audio_buffer());
+
+               set_out_framerate(in_framerate_);
+       }
+
+       void push(const std::shared_ptr<AVFrame>& video_frame)
+       {
+               if (!video_frame)
+                       return;
+
+               av_frame_format current_frame_format(*video_frame);
+
+               if (previously_filtered_frame_ && video_frame->data[0] && *previously_filtered_frame_ != current_frame_format)
+               {
+                       // Fixes bug where avfilter crashes server on some DV files (starts in YUV420p but changes to YUV411p after the first frame).
+                       if (ffmpeg::is_logging_quiet_for_thread())
+                               CASPAR_LOG(debug) << L"[frame_muxer] Frame format has changed. Resetting display mode.";
+                       else
+                               CASPAR_LOG(info) << L"[frame_muxer] Frame format has changed. Resetting display mode.";
+
+                       display_mode_ = display_mode::invalid;
+                       filter_.reset();
+                       previously_filtered_frame_ = boost::none;
+               }
+
+               if (video_frame == flush_video())
+               {
+                       video_streams_.push(std::queue<core::mutable_frame>());
+               }
+               else if (video_frame == empty_video())
+               {
+                       video_streams_.back().push(frame_factory_->create_frame(this, core::pixel_format::invalid, audio_channel_layout_));
+                       display_mode_ = display_mode::simple;
+               }
+               else
+               {
+                       if (!filter_ || display_mode_ == display_mode::invalid)
+                               update_display_mode(video_frame);
+
+                       if (filter_)
+                       {
+                               filter_->push(video_frame);
+                               previously_filtered_frame_ = current_frame_format;
+
+                               for (auto& av_frame : filter_->poll_all())
+                                       video_streams_.back().push(make_frame(this, av_frame, *frame_factory_, audio_channel_layout_));
+                       }
+               }
+
+               if (video_streams_.back().size() > 32)
+                       CASPAR_THROW_EXCEPTION(invalid_operation() << source_info("frame_muxer") << msg_info("video-stream overflow. This can be caused by incorrect frame-rate. Check clip meta-data."));
+       }
+
+       void push(const std::shared_ptr<core::mutable_audio_buffer>& audio)
+       {
+               if (!audio)
+                       return;
+
+               if (audio == flush_audio())
+               {
+                       audio_streams_.push(core::mutable_audio_buffer());
+               }
+               else if (audio == empty_audio())
+               {
+                       boost::range::push_back(audio_streams_.back(), core::mutable_audio_buffer(audio_cadence_.front() * audio_channel_layout_.num_channels, 0));
+               }
+               else
+               {
+                       boost::range::push_back(audio_streams_.back(), *audio);
+               }
+
+               if (audio_streams_.back().size() > 32 * audio_cadence_.front() * audio_channel_layout_.num_channels)
+                       BOOST_THROW_EXCEPTION(invalid_operation() << source_info("frame_muxer") << msg_info("audio-stream overflow. This can be caused by incorrect frame-rate. Check clip meta-data."));
+       }
+
+       bool video_ready() const
+       {
+               return video_streams_.size() > 1 || (video_streams_.size() >= audio_streams_.size() && video_ready2());
+       }
+
+       bool audio_ready() const
+       {
+               return audio_streams_.size() > 1 || (audio_streams_.size() >= video_streams_.size() && audio_ready2());
+       }
+
+       bool video_ready2() const
+       {
+               return video_streams_.front().size() >= 1;
+       }
+
+       bool audio_ready2() const
+       {
+               return audio_streams_.front().size() >= audio_cadence_.front() * audio_channel_layout_.num_channels;
+       }
+
+       core::draw_frame poll()
+       {
+               if (!frame_buffer_.empty())
+               {
+                       auto frame = frame_buffer_.front();
+                       frame_buffer_.pop();
+                       return frame;
+               }
+
+               if (video_streams_.size() > 1 && audio_streams_.size() > 1 && (!video_ready2() || !audio_ready2()))
+               {
+                       if (!video_streams_.front().empty() || !audio_streams_.front().empty())
+                               CASPAR_LOG(trace) << "Truncating: " << video_streams_.front().size() << L" video-frames, " << audio_streams_.front().size() << L" audio-samples.";
+
+                       video_streams_.pop();
+                       audio_streams_.pop();
+               }
+
+               if (!video_ready2() || !audio_ready2() || display_mode_ == display_mode::invalid)
+                       return core::draw_frame::empty();
+
+               auto frame                      = pop_video();
+               frame.audio_data()      = pop_audio();
+
+               frame_buffer_.push(core::draw_frame(std::move(frame)));
+
+               return frame_buffer_.empty() ? core::draw_frame::empty() : poll();
+       }
+
+       core::mutable_frame pop_video()
+       {
+               auto frame = std::move(video_streams_.front().front());
+               video_streams_.front().pop();
+               return frame;
+       }
+
+       core::mutable_audio_buffer pop_audio()
+       {
+               CASPAR_VERIFY(audio_streams_.front().size() >= audio_cadence_.front() * audio_channel_layout_.num_channels);
+
+               auto begin      = audio_streams_.front().begin();
+               auto end        = begin + (audio_cadence_.front() * audio_channel_layout_.num_channels);
+
+               core::mutable_audio_buffer samples(begin, end);
+               audio_streams_.front().erase(begin, end);
+
+               boost::range::rotate(audio_cadence_, std::begin(audio_cadence_) + 1);
+
+               return samples;
+       }
+
+       uint32_t calc_nb_frames(uint32_t nb_frames) const
+       {
+               uint64_t nb_frames2 = nb_frames;
+
+               if(filter_ && filter_->is_double_rate()) // Take into account transformations in filter.
+                       nb_frames2 *= 2;
+
+               return static_cast<uint32_t>(nb_frames2);
+       }
+
+       boost::rational<int> out_framerate() const
+       {
+               boost::lock_guard<boost::mutex> lock(out_framerate_mutex_);
+
+               return out_framerate_;
+       }
+private:
+       void update_display_mode(const std::shared_ptr<AVFrame>& frame)
+       {
+               std::wstring filter_str = filter_str_;
+
+               display_mode_ = display_mode::simple;
+
+               auto mode = get_mode(*frame);
+               if (mode == core::field_mode::progressive && frame->height < 720 && in_framerate_ < 50) // SD frames are interlaced. Probably incorrect meta-data. Fix it.
+                       mode = core::field_mode::upper;
+
+               if (filter::is_deinterlacing(filter_str_))
+               {
+                       display_mode_ = display_mode::simple;
+               }
+               else if (mode != core::field_mode::progressive)
+               {
+                       if (force_deinterlacing_)
+                       {
+                               display_mode_ = display_mode::deinterlace_bob;
+                       }
+                       else
+                       {
+                               bool output_also_interlaced = format_desc_.field_mode != core::field_mode::progressive;
+                               bool interlaced_output_compatible =
+                                               output_also_interlaced
+                                               && (
+                                                               (frame->height == 480 && format_desc_.height == 486) // don't deinterlace for NTSC DV
+                                                               || frame->height == format_desc_.height
+                                               )
+                                               && in_framerate_ == format_desc_.framerate;
+
+                               display_mode_ = interlaced_output_compatible ? display_mode::simple : display_mode::deinterlace_bob;
+                       }
+               }
+
+               if (display_mode_ == display_mode::deinterlace_bob)
+                       filter_str = append_filter(filter_str, L"YADIF=1:-1");
+
+               auto out_framerate = in_framerate_;
+
+               if (filter::is_double_rate(filter_str))
+                       out_framerate *= 2;
+
+               if (frame->height == 480) // NTSC DV
+               {
+                       auto pad_str = L"PAD=" + boost::lexical_cast<std::wstring>(frame->width) + L":486:0:2:black";
+                       filter_str = append_filter(filter_str, pad_str);
+               }
+
+               filter_.reset (new filter(
+                               frame->width,
+                               frame->height,
+                               1 / in_framerate_,
+                               in_framerate_,
+                               boost::rational<int>(frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den),
+                               static_cast<AVPixelFormat>(frame->format),
+                               std::vector<AVPixelFormat>(),
+                               u8(filter_str)));
+
+               set_out_framerate(out_framerate);
+
+               auto in_fps = static_cast<double>(in_framerate_.numerator()) / static_cast<double>(in_framerate_.denominator());
+
+               if (ffmpeg::is_logging_quiet_for_thread())
+                       CASPAR_LOG(debug) << L"[frame_muxer] " << display_mode_ << L" " << print_mode(frame->width, frame->height, in_fps, frame->interlaced_frame > 0);
+               else
+                       CASPAR_LOG(info) << L"[frame_muxer] " << display_mode_ << L" " << print_mode(frame->width, frame->height, in_fps, frame->interlaced_frame > 0);
+       }
+
+       void merge()
+       {
+               while (video_ready() && audio_ready() && display_mode_ != display_mode::invalid)
+               {
+                       auto frame1 = pop_video();
+                       frame1.audio_data() = pop_audio();
+
+                       frame_buffer_.push(core::draw_frame(std::move(frame1)));
+               }
+       }
+
+       void set_out_framerate(boost::rational<int> out_framerate)
+       {
+               boost::lock_guard<boost::mutex> lock(out_framerate_mutex_);
+
+               bool changed = out_framerate != out_framerate_;
+               out_framerate_ = std::move(out_framerate);
+
+               if (changed)
+                       update_audio_cadence();
+       }
+
+       void update_audio_cadence()
+       {
+               audio_cadence_ = find_audio_cadence(out_framerate_);
+
+               // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601)
+               // This cadence fills the audio mixer most optimally.
+               boost::range::rotate(audio_cadence_, std::end(audio_cadence_) - 1);
+       }
+};
+
+frame_muxer::frame_muxer(
+               boost::rational<int> in_framerate,
+               const spl::shared_ptr<core::frame_factory>& frame_factory,
+               const core::video_format_desc& format_desc,
+               const core::audio_channel_layout& channel_layout,
+               const std::wstring& filter,
+               bool multithreaded_filter)
+       : impl_(new impl(in_framerate, frame_factory, format_desc, channel_layout, filter, multithreaded_filter)){}
+void frame_muxer::push(const std::shared_ptr<AVFrame>& video){impl_->push(video);}
+void frame_muxer::push(const std::shared_ptr<core::mutable_audio_buffer>& audio){impl_->push(audio);}
+core::draw_frame frame_muxer::poll(){return impl_->poll();}
+uint32_t frame_muxer::calc_nb_frames(uint32_t nb_frames) const {return impl_->calc_nb_frames(nb_frames);}
+bool frame_muxer::video_ready() const{return impl_->video_ready();}
+bool frame_muxer::audio_ready() const{return impl_->audio_ready();}
+boost::rational<int> frame_muxer::out_framerate() const { return impl_->out_framerate(); }
+}}