X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fproducer%2Fdecklink_producer.cpp;h=f5734796641bd2b7354fadf797abd3e590590c6b;hb=ffcb75eabba1d77eab8d83447136261462e3e088;hp=9e3cf4888b7b4df320e36fd4892fd02596c95379;hpb=a5d96f340299f70db6f026f700088dd67ee3b517;p=casparcg diff --git a/modules/decklink/producer/decklink_producer.cpp b/modules/decklink/producer/decklink_producer.cpp index 9e3cf4888..f57347966 100644 --- a/modules/decklink/producer/decklink_producer.cpp +++ b/modules/decklink/producer/decklink_producer.cpp @@ -22,20 +22,42 @@ #include "decklink_producer.h" -#include "../interop/DeckLinkAPI_h.h" // TODO: Change this -#include "../util/util.h" // TODO: Change this +#include "../interop/DeckLinkAPI_h.h" +#include "../util/util.h" +#include "../../ffmpeg/producer/filter/filter.h" +#include "../../ffmpeg/producer/util.h" +#include "../../ffmpeg/producer/frame_muxer.h" + +#include #include -#include +#include #include -#include +#include -#include +#include +#include +#include #include -#include #include +#include +#include + +#if defined(_MSC_VER) +#pragma warning (push) +#pragma warning (disable : 4244) +#endif +extern "C" +{ + #define __STDC_CONSTANT_MACROS + #define __STDC_LIMIT_MACROS + #include +} +#if defined(_MSC_VER) +#pragma warning (pop) +#endif #pragma warning(push) #pragma warning(disable : 4996) @@ -50,94 +72,77 @@ #include namespace caspar { + +class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback +{ + CComPtr decklink_; + CComQIPtr input_; + + const std::wstring model_name_; + const core::video_format_desc format_desc_; + const size_t device_index_; -class decklink_input : public IDeckLinkInputCallback -{ - struct co_init - { - co_init(){CoInitialize(nullptr);} - ~co_init(){CoUninitialize();} - } co_; - - printer parent_printer_; - const core::video_format_desc format_desc_; - std::wstring model_name_; - const size_t device_index_; - - std::shared_ptr graph_; - timer perf_timer_; - - CComPtr decklink_; - CComQIPtr input_; - IDeckLinkDisplayMode* d_mode_; - - std::vector audio_data_; + std::shared_ptr graph_; + boost::timer tick_timer_; + boost::timer frame_timer_; + + safe_ptr frame_factory_; - std::shared_ptr frame_factory_; + tbb::concurrent_bounded_queue> frame_buffer_; - tbb::concurrent_bounded_queue> frame_buffer_; - safe_ptr tail_; + std::exception_ptr exception_; + filter filter_; + + frame_muxer muxer_; public: - decklink_input(const core::video_format_desc& format_desc, size_t device_index, const std::shared_ptr& frame_factory, const printer& parent_printer) - : parent_printer_(parent_printer) + decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const safe_ptr& frame_factory, const std::wstring& filter) + : decklink_(get_device(device_index)) + , input_(decklink_) + , model_name_(get_model_name(decklink_)) , format_desc_(format_desc) , device_index_(device_index) - , model_name_(L"DECKLINK") , frame_factory_(frame_factory) - , tail_(core::basic_frame::empty()) + , filter_(filter) + , muxer_(double_rate(filter) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory) { - frame_buffer_.set_capacity(4); + frame_buffer_.set_capacity(2); - CComPtr pDecklinkIterator; - if(FAILED(pDecklinkIterator.CoCreateInstance(CLSID_CDeckLinkIterator))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " No Decklink drivers installed.")); - - size_t n = 0; - while(n < device_index_ && pDecklinkIterator->Next(&decklink_) == S_OK){++n;} - - if(n != device_index_ || !decklink_) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Decklink card not found.") << arg_name_info("device_index") << arg_value_info(boost::lexical_cast(device_index_))); - - input_ = decklink_; - - BSTR pModelName; - decklink_->GetModelName(&pModelName); - model_name_ = std::wstring(pModelName); - - graph_ = diagnostics::create_graph(boost::bind(&decklink_input::print, this)); + graph_ = diagnostics::create_graph(boost::bind(&decklink_producer::print, this)); graph_->add_guide("tick-time", 0.5); - graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f)); + graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); + graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f)); + graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f)); + graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f)); + graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); - unsigned long decklinkVideoFormat = GetDecklinkVideoFormat(format_desc.format); - if(decklinkVideoFormat == ULONG_MAX) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat.")); - - d_mode_ = get_display_mode((BMDDisplayMode)decklinkVideoFormat); - if(d_mode_ == nullptr) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat.")); - - BMDDisplayModeSupport displayModeSupport; - if(FAILED(input_->DoesSupportVideoMode((BMDDisplayMode)decklinkVideoFormat, bmdFormat8BitYUV, bmdVideoOutputFlagDefault, &displayModeSupport, nullptr))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat.")); - - // NOTE: bmdFormat8BitARGB does not seem to work with Decklink HD Extreme 3D - if(FAILED(input_->EnableVideoInput((BMDDisplayMode)decklinkVideoFormat, bmdFormat8BitYUV, 0))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video input.")); - - if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio input.")); + auto display_mode = get_display_mode(input_, format_desc_.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault); + + // NOTE: bmdFormat8BitARGB is currently not supported by any decklink card. (2011-05-08) + if(FAILED(input_->EnableVideoInput(display_mode, bmdFormat8BitYUV, 0))) + BOOST_THROW_EXCEPTION(caspar_exception() + << msg_info(narrow(print()) + " Could not enable video input.") + << boost::errinfo_api_function("EnableVideoInput")); + + if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, 2))) + BOOST_THROW_EXCEPTION(caspar_exception() + << msg_info(narrow(print()) + " Could not enable audio input.") + << boost::errinfo_api_function("EnableAudioInput")); if (FAILED(input_->SetCallback(this)) != S_OK) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set input callback.")); + BOOST_THROW_EXCEPTION(caspar_exception() + << msg_info(narrow(print()) + " Failed to set input callback.") + << boost::errinfo_api_function("SetCallback")); if(FAILED(input_->StartStreams())) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start input stream.")); + BOOST_THROW_EXCEPTION(caspar_exception() + << msg_info(narrow(print()) + " Failed to start input stream.") + << boost::errinfo_api_function("StartStreams")); - CASPAR_LOG(info) << print() << " successfully initialized decklink for " << format_desc_.name; + CASPAR_LOG(info) << print() << L" Successfully Initialized."; } - ~decklink_input() + ~decklink_producer() { if(input_ != nullptr) { @@ -152,163 +157,147 @@ public: virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents /*notificationEvents*/, IDeckLinkDisplayMode* newDisplayMode, BMDDetectedVideoInputFormatFlags /*detectedSignalFlags*/) { - d_mode_ = newDisplayMode; return S_OK; } - // TODO: Enable audio input virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio) { - graph_->update_value("tick-time", static_cast(perf_timer_.elapsed()/format_desc_.interval*0.5)); - perf_timer_.reset(); - if(!video) - return S_OK; - - void* bytes = nullptr; - if(FAILED(video->GetBytes(&bytes))) return S_OK; - core::pixel_format_desc desc; - desc.pix_fmt = core::pixel_format::ycbcr; - desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth(), d_mode_->GetHeight(), 1)); - desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth()/2, d_mode_->GetHeight(), 1)); - desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth()/2, d_mode_->GetHeight(), 1)); - auto frame = frame_factory_->create_frame(desc); + try + { + graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5); + tick_timer_.restart(); - unsigned char* data = reinterpret_cast(bytes); - int frame_size = (d_mode_->GetWidth() * 16 / 8) * d_mode_->GetHeight(); + frame_timer_.restart(); - // Convert to planar YUV422 - unsigned char* y = frame->image_data(0).begin(); - unsigned char* cb = frame->image_data(1).begin(); - unsigned char* cr = frame->image_data(2).begin(); - - tbb::parallel_for(tbb::blocked_range(0, frame_size/4), [&](const tbb::blocked_range& r) - { - for(auto n = r.begin(); n != r.end(); ++n) + void* bytes = nullptr; + if(FAILED(video->GetBytes(&bytes)) || !bytes) + return S_OK; + + safe_ptr av_frame(avcodec_alloc_frame(), av_free); + avcodec_get_frame_defaults(av_frame.get()); + + av_frame->data[0] = reinterpret_cast(bytes); + av_frame->linesize[0] = video->GetRowBytes(); + av_frame->format = PIX_FMT_UYVY422; + av_frame->width = video->GetWidth(); + av_frame->height = video->GetHeight(); + av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive; + av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0; + + BOOST_FOREACH(auto& av_frame2, filter_.execute(av_frame)) + muxer_.push(av_frame2); + + // It is assumed that audio is always equal or ahead of video. + if(audio && SUCCEEDED(audio->GetBytes(&bytes))) { - cb[n] = data[n*4+0]; - y [n*2+0] = data[n*4+1]; - cr[n] = data[n*4+2]; - y [n*2+1] = data[n*4+3]; + auto sample_frame_count = audio->GetSampleFrameCount(); + auto audio_data = reinterpret_cast(bytes); + muxer_.push(std::make_shared(audio_data, audio_data + sample_frame_count*sizeof(int32_t))); } - }); - - if(audio && SUCCEEDED(audio->GetBytes(&bytes))) - { - auto sample_frame_count = audio->GetSampleFrameCount(); - auto audio_data = reinterpret_cast(bytes); - audio_data_.insert(audio_data_.end(), audio_data, audio_data + sample_frame_count*2); + else + muxer_.push(std::make_shared(frame_factory_->get_video_format_desc().audio_samples_per_frame, 0)); + + muxer_.commit(); - if(audio_data_.size() > 3840) + while(!muxer_.empty()) { - frame->audio_data() = std::vector(audio_data_.begin(), audio_data_.begin() + 3840); - audio_data_.erase(audio_data_.begin(), audio_data_.begin() + 3840); - frame_buffer_.try_push(frame); + if(!frame_buffer_.try_push(muxer_.pop())) + graph_->add_tag("dropped-frame"); } - } - else - frame_buffer_.try_push(frame); - return S_OK; - } + graph_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5); - IDeckLinkDisplayMode* get_display_mode(BMDDisplayMode mode) - { - CComPtr iterator; - IDeckLinkDisplayMode* d_mode; - - if (input_->GetDisplayModeIterator(&iterator) != S_OK) - return nullptr; - - while (iterator->Next(&d_mode) == S_OK) + graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + } + catch(...) { - if(d_mode->GetDisplayMode() == mode) - return d_mode; + exception_ = std::current_exception(); + return E_FAIL; } - return nullptr; - } + return S_OK; + } + safe_ptr get_frame() { - frame_buffer_.try_pop(tail_); - return tail_; + if(exception_ != nullptr) + std::rethrow_exception(exception_); + + safe_ptr frame = core::basic_frame::late(); + if(!frame_buffer_.try_pop(frame)) + graph_->add_tag("late-frame"); + graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + return frame; } - + std::wstring print() const { - return (parent_printer_ ? parent_printer_() + L"/" : L"") + L" [" + model_name_ + L"device:" + boost::lexical_cast(device_index_) + L"]"; + return model_name_ + L" [" + boost::lexical_cast(device_index_) + L"]"; } }; -class decklink_producer : public core::frame_producer -{ - const size_t device_index_; - printer parent_printer_; - - std::unique_ptr input_; - - const core::video_format_desc format_desc_; - - executor executor_; +class decklink_producer_proxy : public core::frame_producer +{ + safe_ptr last_frame_; + com_context context_; public: - explicit decklink_producer(const core::video_format_desc& format_desc, size_t device_index) - : format_desc_(format_desc) - , device_index_(device_index) - , executor_(print()){} - - ~decklink_producer() - { - executor_.invoke([this] - { - input_ = nullptr; - }); + explicit decklink_producer_proxy(const safe_ptr& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str = L"") + : context_(L"decklink_producer[" + boost::lexical_cast(device_index) + L"]") + , last_frame_(core::basic_frame::empty()) + { + context_.reset([&]{return new decklink_producer(format_desc, device_index, frame_factory, filter_str);}); } - - virtual void initialize(const safe_ptr& frame_factory) + + virtual safe_ptr receive(int) { - executor_.start(); - executor_.invoke([=] - { - input_.reset(new decklink_input(format_desc_, device_index_, frame_factory, parent_printer_)); - }); + auto frame = context_->get_frame(); + if(frame != core::basic_frame::late()) + last_frame_ = frame; + return frame; } - virtual void set_parent_printer(const printer& parent_printer) { parent_printer_ = parent_printer;} - - virtual safe_ptr receive() + virtual safe_ptr last_frame() const { - return input_->get_frame(); + return disable_audio(last_frame_); } std::wstring print() const { - return (parent_printer_ ? parent_printer_() + L"/" : L"") + (input_ ? input_->print() : L"Unknown Decklink [input-" + boost::lexical_cast(device_index_) + L"]"); + return context_->print(); } }; -safe_ptr create_decklink_producer(const std::vector& params) +safe_ptr create_decklink_producer(const safe_ptr& frame_factory, const std::vector& params) { if(params.empty() || !boost::iequals(params[0], "decklink")) return core::frame_producer::empty(); size_t device_index = 1; if(params.size() > 1) - { - try{device_index = boost::lexical_cast(params[1]);} - catch(boost::bad_lexical_cast&){} - } + device_index = lexical_cast_or_default(params[1], 1); core::video_format_desc format_desc = core::video_format_desc::get(L"PAL"); if(params.size() > 2) { - format_desc = core::video_format_desc::get(params[2]); - format_desc = format_desc.format != core::video_format::invalid ? format_desc : core::video_format_desc::get(L"PAL"); + auto desc = core::video_format_desc::get(params[2]); + if(desc.format != core::video_format::invalid) + format_desc = desc; + } + + std::wstring filter_str = L""; + + auto filter_it = std::find(params.begin(), params.end(), L"FILTER"); + if(filter_it != params.end()) + { + if(++filter_it != params.end()) + filter_str = *filter_it; } - return make_safe(format_desc, device_index); + return make_safe(frame_factory, format_desc, device_index, filter_str); } } \ No newline at end of file