X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fproducer%2Fdecklink_producer.cpp;h=902d78e210e30e1f1dd9681045747a47b7f14a0b;hb=ac7b3acb915f90de6b224e54a2240023fc221e5a;hp=0f3111688112981a4e96946a9693b9dea6cbe468;hpb=c2ce81cf2749e7adf26715227d80af1a325c7370;p=casparcg diff --git a/modules/decklink/producer/decklink_producer.cpp b/modules/decklink/producer/decklink_producer.cpp index 0f3111688..902d78e21 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) @@ -49,95 +71,78 @@ #include -namespace caspar { - -class decklink_input : public IDeckLinkInputCallback -{ - struct co_init - { - co_init(){CoInitialize(nullptr);} - ~co_init(){CoUninitialize();} - } co_; - - const object* parent_; - 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_; +namespace caspar { namespace decklink { + +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_; - 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_; + ffmpeg::filter filter_; + + ffmpeg::frame_muxer muxer_; public: - decklink_input(const object* parent, const core::video_format_desc& format_desc, size_t device_index, const std::shared_ptr& frame_factory) - : parent_(parent) + 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_(ffmpeg::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, format_desc_.audio_channels))) + 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,160 +157,152 @@ 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*format_desc_.audio_channels)); } - }); + else + muxer_.push(std::make_shared(frame_factory_->get_video_format_desc().audio_samples_per_frame, 0)); + + muxer_.commit(); - 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); - - 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 static_cast(parent_)->print() + 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_; - - 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_; + const int64_t length_; public: - explicit decklink_producer(const core::video_format_desc& format_desc, size_t device_index) - : format_desc_(format_desc) - , device_index_(device_index) - , executor_(L"decklink_producer"){} + 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, int64_t length) + : context_(L"decklink_producer[" + boost::lexical_cast(device_index) + L"]") + , last_frame_(core::basic_frame::empty()) + , length_(length) + { + context_.reset([&]{return new decklink_producer(format_desc, device_index, frame_factory, filter_str);}); + } - ~decklink_producer() - { - executor_.invoke([this] - { - input_ = nullptr; - }); + ~decklink_producer_proxy() + { + auto str = print(); + context_.reset(); + CASPAR_LOG(info) << str << L" Successfully Uninitialized."; + } + + virtual safe_ptr receive(int) + { + auto frame = context_->get_frame(); + if(frame != core::basic_frame::late()) + last_frame_ = frame; + return frame; } - virtual void set_frame_factory(const safe_ptr& frame_factory) + virtual safe_ptr last_frame() const { - executor_.start(); - executor_.invoke([=] - { - input_.reset(new decklink_input(this, format_desc_, device_index_, frame_factory)); - }); + return disable_audio(last_frame_); } - - virtual safe_ptr receive() + + virtual int64_t nb_frames() const { - return input_->get_frame(); + return length_; } std::wstring print() const { - return (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_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&){} - } + auto device_index = core::get_param(L"DEVICE", params, 1); + auto filter_str = core::get_param(L"FILTER", params, L""); + auto length = core::get_param(L"LENGTH", params, std::numeric_limits::max()); + + boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1"); + boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=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 format_desc = core::video_format_desc::get(core::get_param(L"FORMAT", params, L"INVALID")); - return make_safe(format_desc, device_index); + if(format_desc.format == core::video_format::invalid) + format_desc = frame_factory->get_video_format_desc(); + + return make_safe(frame_factory, format_desc, device_index, filter_str, length); } -} \ No newline at end of file +}} \ No newline at end of file