X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fproducer%2Fdecklink_producer.cpp;h=a6ead70661933b9bf96cc44f518ccd0ff0bc96da;hb=e0e1a2b1fdcab66e8da72e3eaa2db5564f8f9d72;hp=c099d878892c5cefc967cb0c333a43e454a0d031;hpb=5d522507a806b7d9849d6419357334f2096e57a6;p=casparcg diff --git a/modules/decklink/producer/decklink_producer.cpp b/modules/decklink/producer/decklink_producer.cpp index c099d8788..a6ead7066 100644 --- a/modules/decklink/producer/decklink_producer.cpp +++ b/modules/decklink/producer/decklink_producer.cpp @@ -43,7 +43,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -58,7 +60,7 @@ #pragma warning (push) #pragma warning (disable : 4244) #endif -extern "C" +extern "C" { #define __STDC_CONSTANT_MACROS #define __STDC_LIMIT_MACROS @@ -73,7 +75,6 @@ extern "C" #include namespace caspar { namespace decklink { - core::audio_channel_layout get_adjusted_channel_layout(core::audio_channel_layout layout) { if (layout.num_channels <= 2) @@ -91,9 +92,9 @@ std::wstring to_string(const T& cadence) { return boost::join(cadence | boost::adaptors::transformed([](size_t i) { return boost::lexical_cast(i); }), L", "); } - + class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback -{ +{ const int device_index_; core::monitor::subject monitor_subject_; spl::shared_ptr graph_; @@ -102,29 +103,38 @@ class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback com_ptr decklink_ = get_device(device_index_); com_iface_ptr input_ = iface_cast(decklink_); com_iface_ptr attributes_ = iface_cast(decklink_); - + const std::wstring model_name_ = get_model_name(decklink_); const std::wstring filter_; - + core::video_format_desc in_format_desc_; core::video_format_desc out_format_desc_; - std::vector audio_cadence_ = out_format_desc_.audio_cadence; + std::vector audio_cadence_ = in_format_desc_.audio_cadence; boost::circular_buffer sync_buffer_ { audio_cadence_.size() }; spl::shared_ptr frame_factory_; core::audio_channel_layout channel_layout_; - ffmpeg::frame_muxer muxer_ { in_format_desc_.fps, frame_factory_, out_format_desc_, channel_layout_, filter_ }; - + ffmpeg::frame_muxer muxer_ { + in_format_desc_.framerate, + { ffmpeg::create_input_pad(in_format_desc_, channel_layout_.num_channels) }, + frame_factory_, + out_format_desc_, + channel_layout_, + filter_, + ffmpeg::filter::is_deinterlacing(filter_) + }; + core::constraints constraints_ { in_format_desc_.width, in_format_desc_.height }; tbb::concurrent_bounded_queue frame_buffer_; + core::draw_frame last_frame_ = core::draw_frame::empty(); std::exception_ptr exception_; public: decklink_producer( - const core::video_format_desc& in_format_desc, - int device_index, - const spl::shared_ptr& frame_factory, + const core::video_format_desc& in_format_desc, + int device_index, + const spl::shared_ptr& frame_factory, const core::video_format_desc& out_format_desc, const core::audio_channel_layout& channel_layout, const std::wstring& filter) @@ -135,8 +145,8 @@ public: , frame_factory_(frame_factory) , channel_layout_(get_adjusted_channel_layout(channel_layout)) { - frame_buffer_.set_capacity(2); - + frame_buffer_.set_capacity(4); + 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)); @@ -144,12 +154,13 @@ public: graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f)); graph_->set_text(print()); diagnostics::register_graph(graph_); - - auto display_mode = get_display_mode(input_, in_format_desc.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault); - + + bool will_attempt_dma; + auto display_mode = get_display_mode(input_, in_format_desc.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault, will_attempt_dma); + // NOTE: bmdFormat8BitARGB is currently not supported by any decklink card. (2011-05-08) - if(FAILED(input_->EnableVideoInput(display_mode, bmdFormat8BitYUV, 0))) - CASPAR_THROW_EXCEPTION(caspar_exception() + if(FAILED(input_->EnableVideoInput(display_mode, bmdFormat8BitYUV, 0))) + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Could not enable video input.") << boost::errinfo_api_function("EnableVideoInput")); @@ -157,23 +168,29 @@ public: CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Could not enable audio input.") << boost::errinfo_api_function("EnableAudioInput")); - + if (FAILED(input_->SetCallback(this)) != S_OK) - CASPAR_THROW_EXCEPTION(caspar_exception() + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set input callback.") << boost::errinfo_api_function("SetCallback")); - + if(FAILED(input_->StartStreams())) - CASPAR_THROW_EXCEPTION(caspar_exception() + CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to start input stream.") << boost::errinfo_api_function("StartStreams")); + // Wait for first frame until returning or give up after 2 seconds. + caspar::timer timeout_timer; + + while (frame_buffer_.size() < 1 && timeout_timer.elapsed() < 2.0) + boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); + CASPAR_LOG(info) << print() << L" Initialized"; } ~decklink_producer() { - if(input_ != nullptr) + if(input_ != nullptr) { input_->StopStreams(); input_->DisableVideoInput(); @@ -188,14 +205,15 @@ public: virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID*) {return E_NOINTERFACE;} virtual ULONG STDMETHODCALLTYPE AddRef () {return 1;} virtual ULONG STDMETHODCALLTYPE Release () {return 1;} - + virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents /*notificationEvents*/, IDeckLinkDisplayMode* newDisplayMode, BMDDetectedVideoInputFormatFlags /*detectedSignalFlags*/) { return S_OK; } virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio) - { + { + ensure_gpf_handler_installed_for_thread("decklink-VideoInputFrameArrived"); if(!video) return S_OK; @@ -205,23 +223,24 @@ public: tick_timer_.restart(); caspar::timer frame_timer; - + // Video void* video_bytes = nullptr; if(FAILED(video->GetBytes(&video_bytes)) || !video_bytes) return S_OK; - + auto video_frame = ffmpeg::create_frame(); - + video_frame->data[0] = reinterpret_cast(video_bytes); - video_frame->linesize[0] = video->GetRowBytes(); - video_frame->format = PIX_FMT_UYVY422; + video_frame->linesize[0] = video->GetRowBytes(); + video_frame->format = AVPixelFormat::AV_PIX_FMT_UYVY422; video_frame->width = video->GetWidth(); video_frame->height = video->GetHeight(); video_frame->interlaced_frame = in_format_desc_.field_mode != core::field_mode::progressive; video_frame->top_field_first = in_format_desc_.field_mode == core::field_mode::upper ? 1 : 0; - + video_frame->key_frame = 1; + monitor_subject_ << core::monitor::message("/file/name") % model_name_ << core::monitor::message("/file/path") % device_index_ @@ -235,67 +254,57 @@ public: // Audio - auto audio_frame = ffmpeg::create_frame(); - audio_frame->format = AV_SAMPLE_FMT_S32; - core::mutable_audio_buffer audio_buf; + std::shared_ptr audio_buffer; + void* audio_bytes = nullptr; - if (audio) + // It is assumed that audio is always equal or ahead of video. + if (audio && SUCCEEDED(audio->GetBytes(&audio_bytes)) && audio_bytes) { - void* audio_bytes = nullptr; - if (FAILED(audio->GetBytes(&audio_bytes)) || !audio_bytes) - return S_OK; - + auto sample_frame_count = audio->GetSampleFrameCount(); + auto audio_data = reinterpret_cast(audio_bytes); - audio_frame->data[0] = reinterpret_cast(audio_bytes); - audio_frame->linesize[0] = audio->GetSampleFrameCount() * channel_layout_.num_channels * sizeof(int32_t); - audio_frame->nb_samples = audio->GetSampleFrameCount(); + audio_buffer = std::make_shared( + audio_data, + audio_data + sample_frame_count * channel_layout_.num_channels); } else - { - audio_buf.resize(audio_cadence_.front() * channel_layout_.num_channels, 0); - audio_frame->data[0] = reinterpret_cast(audio_buf.data()); - audio_frame->linesize[0] = audio_cadence_.front() * channel_layout_.num_channels * sizeof(int32_t); - audio_frame->nb_samples = audio_cadence_.front(); - } - + audio_buffer = std::make_shared(audio_cadence_.front() * channel_layout_.num_channels, 0); + // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601) // This cadence fills the audio mixer most optimally. - sync_buffer_.push_back(audio_frame->nb_samples); + sync_buffer_.push_back(audio_buffer->size() / channel_layout_.num_channels); if(!boost::range::equal(sync_buffer_, audio_cadence_)) { CASPAR_LOG(trace) << print() << L" Syncing audio. Expected cadence: " << to_string(audio_cadence_) << L" Got cadence: " << to_string(sync_buffer_); return S_OK; } boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1); - + // PUSH - muxer_.push_video(video_frame); - muxer_.push_audio(audio_frame); - + muxer_.push({ audio_buffer }); + muxer_.push(static_cast>(video_frame)); + // POLL - auto frame = core::draw_frame::late(); - if(!muxer_.empty()) + for (auto frame = muxer_.poll(); frame != core::draw_frame::empty(); frame = muxer_.poll()) { - frame = std::move(muxer_.front()); - muxer_.pop(); - - if(!frame_buffer_.try_push(frame)) + if (!frame_buffer_.try_push(frame)) { auto dummy = core::draw_frame::empty(); frame_buffer_.try_pop(dummy); + frame_buffer_.try_push(frame); - + graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame"); } } - - graph_->set_value("frame-time", frame_timer.elapsed()*out_format_desc_.fps*0.5); + + graph_->set_value("frame-time", frame_timer.elapsed()*out_format_desc_.fps*0.5); monitor_subject_ << core::monitor::message("/profiler/time") % frame_timer.elapsed() % out_format_desc_.fps; - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); monitor_subject_ << core::monitor::message("/buffer") % frame_buffer_.size() % frame_buffer_.capacity(); } catch(...) @@ -306,32 +315,42 @@ public: return S_OK; } - + core::draw_frame get_frame() { if(exception_ != nullptr) std::rethrow_exception(exception_); - - core::draw_frame frame = core::draw_frame::late(); - if(!frame_buffer_.try_pop(frame)) + + core::draw_frame frame = last_frame_; + + if (!frame_buffer_.try_pop(frame)) graph_->set_tag(diagnostics::tag_severity::WARNING, "late-frame"); - graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); + else + last_frame_ = frame; + + graph_->set_value("output-buffer", static_cast(frame_buffer_.size()) / static_cast(frame_buffer_.capacity())); + return frame; } - + std::wstring print() const { return model_name_ + L" [" + boost::lexical_cast(device_index_) + L"|" + in_format_desc_.name + L"]"; } + boost::rational get_out_framerate() const + { + return muxer_.out_framerate(); + } + core::monitor::subject& monitor_output() { return monitor_subject_; } }; - + class decklink_producer_proxy : public core::frame_producer_base -{ +{ std::unique_ptr producer_; const uint32_t length_; executor executor_; @@ -347,15 +366,17 @@ public: : executor_(L"decklink_producer[" + boost::lexical_cast(device_index) + L"]") , length_(length) { + auto ctx = core::diagnostics::call_context::for_thread(); executor_.invoke([=] { + core::diagnostics::call_context::for_thread() = ctx; com_initialize(); producer_.reset(new decklink_producer(in_format_desc, device_index, frame_factory, out_format_desc, channel_layout, filter_str)); }); } ~decklink_producer_proxy() - { + { executor_.invoke([=] { producer_.reset(); @@ -367,11 +388,11 @@ public: { return producer_->monitor_output(); } - + // frame_producer - + core::draw_frame receive_impl() override - { + { return producer_->get_frame(); } @@ -379,17 +400,17 @@ public: { return producer_->pixel_constraints(); } - + uint32_t nb_frames() const override { return length_; } - + std::wstring print() const override { return producer_->print(); } - + std::wstring name() const override { return L"decklink"; @@ -401,6 +422,11 @@ public: info.add(L"type", L"decklink"); return info; } + + boost::rational get_out_framerate() const + { + return producer_->get_out_framerate(); + } }; void describe_producer(core::help_sink& sink, const core::help_repository& repo) @@ -429,11 +455,11 @@ spl::shared_ptr create_producer(const core::frame_producer auto device_index = get_param(L"DEVICE", params, -1); if(device_index == -1) device_index = boost::lexical_cast(params.at(1)); - - auto filter_str = get_param(L"FILTER", params); - auto length = get_param(L"LENGTH", params, std::numeric_limits::max()); + + auto filter_str = get_param(L"FILTER", params); + auto length = get_param(L"LENGTH", params, std::numeric_limits::max()); auto in_format_desc = core::video_format_desc(get_param(L"FORMAT", params, L"INVALID")); - + if(in_format_desc.format == core::video_format::invalid) in_format_desc = dependencies.format_desc; @@ -449,15 +475,28 @@ spl::shared_ptr create_producer(const core::frame_producer channel_layout = *found_layout; } - - return create_destroy_proxy(spl::make_shared( + + boost::ireplace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1"); + boost::ireplace_all(filter_str, L"DEINTERLACE_LQ", L"SEPARATEFIELDS"); + boost::ireplace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1"); + + auto producer = spl::make_shared( in_format_desc, dependencies.frame_factory, dependencies.format_desc, channel_layout, device_index, filter_str, - length)); -} + length); + + auto get_source_framerate = [=] { return producer->get_out_framerate(); }; + auto target_framerate = dependencies.format_desc.framerate; + return core::create_destroy_proxy(core::create_framerate_producer( + producer, + get_source_framerate, + target_framerate, + dependencies.format_desc.field_mode, + dependencies.format_desc.audio_cadence)); +} }}