]> git.sesse.net Git - casparcg/blobdiff - modules/decklink/producer/decklink_producer.cpp
2.0. audio:
[casparcg] / modules / decklink / producer / decklink_producer.cpp
index c25d0c9bdf8e6fc120f543443f40ac7a9542c066..f5734796641bd2b7354fadf797abd3e590590c6b 100644 (file)
 \r
 #include "../../ffmpeg/producer/filter/filter.h"\r
 #include "../../ffmpeg/producer/util.h"\r
+#include "../../ffmpeg/producer/frame_muxer.h"\r
 \r
+#include <common/log/log.h>\r
 #include <common/diagnostics/graph.h>\r
 #include <common/concurrency/com_context.h>\r
 #include <common/exception/exceptions.h>\r
 #include <common/memory/memclr.h>\r
 \r
-#include <core/producer/frame/frame_factory.h>\r
 #include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/audio_transform.h>\r
+#include <core/producer/frame/frame_transform.h>\r
+#include <core/producer/frame/frame_factory.h>\r
 \r
 #include <tbb/concurrent_queue.h>\r
-#include <tbb/atomic.h>\r
 \r
 #include <boost/algorithm/string.hpp>\r
+#include <boost/foreach.hpp>\r
 #include <boost/timer.hpp>\r
 \r
 #if defined(_MSC_VER)\r
@@ -70,70 +72,8 @@ extern "C"
 #include <functional>\r
 \r
 namespace caspar { \r
-\r
-class frame_filter\r
-{\r
-       std::unique_ptr<filter>                                 filter_;\r
-       safe_ptr<core::frame_factory>                   frame_factory_;\r
-\r
-public:\r
-       frame_filter(const std::string& filter_str, const safe_ptr<core::frame_factory>& frame_factory) \r
-               : filter_(filter_str.empty() ? nullptr : new filter(filter_str))\r
-               , frame_factory_(frame_factory)\r
-       {\r
-       }\r
-\r
-       std::vector<safe_ptr<core::basic_frame>> execute(const safe_ptr<core::write_frame>& input_frame)\r
-       {               \r
-               std::vector<safe_ptr<core::basic_frame>> result;\r
-\r
-               if(!filter_)\r
-               {\r
-                       input_frame->commit();\r
-                       result.push_back(input_frame);\r
-               }\r
-               else\r
-               {\r
-                       auto desc = input_frame->get_pixel_format_desc();\r
-\r
-                       auto av_frame = as_av_frame(input_frame);\r
-                                       \r
-                       filter_->push(av_frame);        \r
-                       auto buffer = filter_->poll();  \r
-                                               \r
-                       if(buffer.size() == 2)\r
-                       {\r
-                               auto frame1 = make_write_frame(this, buffer[0], frame_factory_);\r
-                               auto frame2 = make_write_frame(this, buffer[1], frame_factory_);\r
-                               frame1->audio_data() = std::move(input_frame->audio_data());\r
-                               \r
-                               if(frame_factory_->get_video_format_desc().mode == core::video_mode::progressive)\r
-                               {\r
-                                       frame2->audio_data().insert(frame2->audio_data().begin(), frame1->audio_data().begin() + frame1->audio_data().size()/2, frame1->audio_data().end());\r
-                                       frame1->audio_data().erase(frame1->audio_data().begin() + frame1->audio_data().size()/2, frame1->audio_data().end());\r
-                                       result.push_back(frame1);\r
-                                       result.push_back(frame2);\r
-                               }\r
-                               else\r
-                               {\r
-                                       frame2->get_audio_transform().set_has_audio(false);\r
-                                       result.push_back(core::basic_frame::interlace(frame1, frame2, frame_factory_->get_video_format_desc().mode));\r
-                               }\r
-                       }\r
-                       else if(buffer.size() > 0)\r
-                       {\r
-                               auto frame1 = make_write_frame(this, buffer[0], frame_factory_);\r
-                               frame1->audio_data() = std::move(input_frame->audio_data());\r
-                               result.push_back(frame1);\r
-                       }\r
-\r
-               }\r
                \r
-               return result;\r
-       }\r
-};\r
-       \r
-class decklink_producer : public IDeckLinkInputCallback\r
+class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback\r
 {      \r
        CComPtr<IDeckLink>                                                                                      decklink_;\r
        CComQIPtr<IDeckLinkInput>                                                                       input_;\r
@@ -145,33 +85,32 @@ class decklink_producer : public IDeckLinkInputCallback
        std::shared_ptr<diagnostics::graph>                                                     graph_;\r
        boost::timer                                                                                            tick_timer_;\r
        boost::timer                                                                                            frame_timer_;\r
-       \r
-       std::vector<short>                                                                                      audio_data_;\r
-\r
+               \r
        safe_ptr<core::frame_factory>                                                           frame_factory_;\r
 \r
        tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>>      frame_buffer_;\r
-       safe_ptr<core::basic_frame>                                                                     tail_;\r
 \r
        std::exception_ptr                                                                                      exception_;\r
-       frame_filter                                                                                            filter_;\r
+       filter                                                                                                          filter_;\r
+               \r
+       frame_muxer                                                                                                     muxer_;\r
 \r
 public:\r
-       decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter_str)\r
+       decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)\r
                : decklink_(get_device(device_index))\r
                , input_(decklink_)\r
                , model_name_(get_model_name(decklink_))\r
                , format_desc_(format_desc)\r
                , device_index_(device_index)\r
                , frame_factory_(frame_factory)\r
-               , tail_(core::basic_frame::empty())\r
-               , filter_(narrow(filter_str), frame_factory_)\r
+               , filter_(filter)\r
+               , muxer_(double_rate(filter) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory)\r
        {\r
                frame_buffer_.set_capacity(2);\r
                \r
                graph_ = diagnostics::create_graph(boost::bind(&decklink_producer::print, this));\r
                graph_->add_guide("tick-time", 0.5);\r
-               graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
+               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
                graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));\r
                graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
                graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
@@ -185,7 +124,7 @@ public:
                                                                        << msg_info(narrow(print()) + " Could not enable video input.")\r
                                                                        << boost::errinfo_api_function("EnableVideoInput"));\r
 \r
-               if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) \r
+               if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2))) \r
                        BOOST_THROW_EXCEPTION(caspar_exception() \r
                                                                        << msg_info(narrow(print()) + " Could not enable audio input.")\r
                                                                        << boost::errinfo_api_function("EnableAudioInput"));\r
@@ -228,66 +167,44 @@ public:
 \r
                try\r
                {\r
-                       auto result = core::basic_frame::empty();\r
-\r
                        graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
                        tick_timer_.restart();\r
 \r
                        frame_timer_.restart();\r
-                                               \r
-                       core::pixel_format_desc desc;\r
-                       desc.pix_fmt = core::pixel_format::ycbcr;\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(video->GetWidth(),   video->GetHeight(), 1));\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(video->GetWidth()/2, video->GetHeight(), 1));\r
-                       desc.planes.push_back(core::pixel_format_desc::plane(video->GetWidth()/2, video->GetHeight(), 1));                      \r
-                       auto frame = frame_factory_->create_frame(this, desc);\r
-                                               \r
+\r
                        void* bytes = nullptr;\r
                        if(FAILED(video->GetBytes(&bytes)) || !bytes)\r
                                return S_OK;\r
-\r
-                       unsigned char* data = reinterpret_cast<unsigned char*>(bytes);\r
-                       const size_t frame_size = (format_desc_.width * 16 / 8) * format_desc_.height;\r
-\r
-                       // Convert to planar YUV422\r
-                       unsigned char* y  = frame->image_data(0).begin();\r
-                       unsigned char* cb = frame->image_data(1).begin();\r
-                       unsigned char* cr = frame->image_data(2).begin();\r
-               \r
-                       tbb::parallel_for(tbb::blocked_range<size_t>(0, frame_size/4), [&](const tbb::blocked_range<size_t>& r)\r
-                       {\r
-                               for(auto n = r.begin(); n != r.end(); ++n)\r
-                               {\r
-                                       cb[n]     = data[n*4+0];\r
-                                       y [n*2+0] = data[n*4+1];\r
-                                       cr[n]     = data[n*4+2];\r
-                                       y [n*2+1] = data[n*4+3];\r
-                               }\r
-                       });\r
-                       frame->set_type(format_desc_.mode);\r
                        \r
+                       safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
+                       avcodec_get_frame_defaults(av_frame.get());\r
+                                               \r
+                       av_frame->data[0]                       = reinterpret_cast<uint8_t*>(bytes);\r
+                       av_frame->linesize[0]           = video->GetRowBytes();                 \r
+                       av_frame->format                        = PIX_FMT_UYVY422;\r
+                       av_frame->width                         = video->GetWidth();\r
+                       av_frame->height                        = video->GetHeight();\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 ? 1 : 0;\r
+                                       \r
+                       BOOST_FOREACH(auto& av_frame2, filter_.execute(av_frame))\r
+                               muxer_.push(av_frame2);         \r
+                                                                       \r
                        // It is assumed that audio is always equal or ahead of video.\r
                        if(audio && SUCCEEDED(audio->GetBytes(&bytes)))\r
                        {\r
-                               const size_t audio_samples = static_cast<size_t>(48000.0 / format_desc_.fps);\r
-                               const size_t audio_nchannels = 2;\r
-\r
                                auto sample_frame_count = audio->GetSampleFrameCount();\r
-                               auto audio_data = reinterpret_cast<short*>(bytes);\r
-                               audio_data_.insert(audio_data_.end(), audio_data, audio_data + sample_frame_count*2);\r
-\r
-                               if(audio_data_.size() > audio_samples*audio_nchannels)\r
-                               {\r
-                                       frame->audio_data() = std::vector<short>(audio_data_.begin(), audio_data_.begin() +  audio_samples*audio_nchannels);\r
-                                       audio_data_.erase(audio_data_.begin(), audio_data_.begin() +  audio_samples*audio_nchannels);\r
-                               }\r
+                               auto audio_data = reinterpret_cast<int32_t*>(bytes);\r
+                               muxer_.push(std::make_shared<core::audio_buffer>(audio_data, audio_data + sample_frame_count*sizeof(int32_t)));\r
                        }\r
-               \r
-                       auto frames = filter_.execute(frame);           \r
-                       \r
-                       for(size_t n = 0; n < frames.size(); ++n)\r
+                       else\r
+                               muxer_.push(std::make_shared<core::audio_buffer>(frame_factory_->get_video_format_desc().audio_samples_per_frame, 0));\r
+                                       \r
+                       muxer_.commit();\r
+\r
+                       while(!muxer_.empty())\r
                        {\r
-                               if(!frame_buffer_.try_push(frames[n]))\r
+                               if(!frame_buffer_.try_push(muxer_.pop()))\r
                                        graph_->add_tag("dropped-frame");\r
                        }\r
 \r
@@ -309,10 +226,11 @@ public:
                if(exception_ != nullptr)\r
                        std::rethrow_exception(exception_);\r
 \r
-               if(!frame_buffer_.try_pop(tail_))\r
+               safe_ptr<core::basic_frame> frame = core::basic_frame::late();\r
+               if(!frame_buffer_.try_pop(frame))\r
                        graph_->add_tag("late-frame");\r
                graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));      \r
-               return tail_;\r
+               return frame;\r
        }\r
        \r
        std::wstring print() const\r
@@ -323,18 +241,28 @@ public:
        \r
 class decklink_producer_proxy : public core::frame_producer\r
 {              \r
+       safe_ptr<core::basic_frame>     last_frame_;\r
        com_context<decklink_producer> context_;\r
 public:\r
 \r
        explicit decklink_producer_proxy(const safe_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str = L"")\r
                : context_(L"decklink_producer[" + boost::lexical_cast<std::wstring>(device_index) + L"]")\r
+               , last_frame_(core::basic_frame::empty())\r
        {\r
                context_.reset([&]{return new decklink_producer(format_desc, device_index, frame_factory, filter_str);}); \r
        }\r
                                \r
-       virtual safe_ptr<core::basic_frame> receive()\r
+       virtual safe_ptr<core::basic_frame> receive(int)\r
+       {\r
+               auto frame = context_->get_frame();\r
+               if(frame != core::basic_frame::late())\r
+                       last_frame_ = frame;\r
+               return frame;\r
+       }\r
+\r
+       virtual safe_ptr<core::basic_frame> last_frame() const\r
        {\r
-               return context_->get_frame();\r
+               return disable_audio(last_frame_);\r
        }\r
        \r
        std::wstring print() const\r