X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fproducer%2Fdecklink_producer.cpp;h=902d78e210e30e1f1dd9681045747a47b7f14a0b;hb=ac7b3acb915f90de6b224e54a2240023fc221e5a;hp=6fd45ae29f72c3844a5d320c3b5dbf99dac95195;hpb=d100d17e81ed61c7638b681989cfdab5276430db;p=casparcg diff --git a/modules/decklink/producer/decklink_producer.cpp b/modules/decklink/producer/decklink_producer.cpp index 6fd45ae29..902d78e21 100644 --- a/modules/decklink/producer/decklink_producer.cpp +++ b/modules/decklink/producer/decklink_producer.cpp @@ -25,21 +25,40 @@ #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 +#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) @@ -52,45 +71,48 @@ #include -namespace caspar { - -class decklink_producer : public IDeckLinkInputCallback +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::shared_ptr graph_; - boost::timer perf_timer_; + CComPtr decklink_; + CComQIPtr input_; - std::vector audio_data_; + const std::wstring model_name_; + const core::video_format_desc format_desc_; + const size_t device_index_; - std::shared_ptr frame_factory_; + std::shared_ptr graph_; + boost::timer tick_timer_; + boost::timer frame_timer_; + + safe_ptr frame_factory_; - tbb::concurrent_bounded_queue> frame_buffer_; - safe_ptr tail_; + tbb::concurrent_bounded_queue> frame_buffer_; - std::exception_ptr exception_; + std::exception_ptr exception_; + ffmpeg::filter filter_; + + ffmpeg::frame_muxer muxer_; public: - decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const std::shared_ptr& frame_factory) + 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) , 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(2); 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)); @@ -98,18 +120,26 @@ public: // 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.")); - - if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) - BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio input.")); + 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_producer() @@ -132,58 +162,54 @@ public: virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio) { + if(!video) + return S_OK; + try { - graph_->update_value("tick-time", perf_timer_.elapsed()*format_desc_.fps*0.5); - perf_timer_.restart(); - - core::pixel_format_desc desc; - desc.pix_fmt = core::pixel_format::ycbcr; - desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width, format_desc_.height, 1)); - desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width/2, format_desc_.height, 1)); - desc.planes.push_back(core::pixel_format_desc::plane(format_desc_.width/2, format_desc_.height, 1)); - auto frame = frame_factory_->create_frame(this, desc); - - void* bytes = nullptr; - video->GetBytes(&bytes); - unsigned char* data = reinterpret_cast(bytes); - const size_t frame_size = (format_desc_.width * 16 / 8) * format_desc_.height; - - // 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) - { - 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]; - } - }); + graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5); + tick_timer_.restart(); + + frame_timer_.restart(); + 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))) { - const size_t audio_samples = static_cast(48000.0 / format_desc_.fps); - const size_t audio_nchannels = 2; - 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() > audio_samples*audio_nchannels) - { - frame->audio_data() = std::vector(audio_data_.begin(), audio_data_.begin() + audio_samples*audio_nchannels); - audio_data_.erase(audio_data_.begin(), audio_data_.begin() + audio_samples*audio_nchannels); - } + 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(); + + while(!muxer_.empty()) + { + if(!frame_buffer_.try_push(muxer_.pop())) + graph_->add_tag("dropped-frame"); } - if(!frame_buffer_.try_push(frame)) - graph_->add_tag("dropped-frame"); + graph_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5); + graph_->set_value("output-buffer", static_cast(frame_buffer_.size())/static_cast(frame_buffer_.capacity())); } catch(...) @@ -200,10 +226,11 @@ public: if(exception_ != nullptr) std::rethrow_exception(exception_); - if(!frame_buffer_.try_pop(tail_)) + 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 tail_; + return frame; } std::wstring print() const @@ -214,18 +241,42 @@ public: class decklink_producer_proxy : public core::frame_producer { - com_context context_; + safe_ptr last_frame_; + com_context context_; + const int64_t length_; public: - explicit decklink_producer_proxy(const safe_ptr& frame_factory, const core::video_format_desc& format_desc, size_t device_index) + 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);}); + context_.reset([&]{return new decklink_producer(format_desc, device_index, frame_factory, filter_str);}); + } + + ~decklink_producer_proxy() + { + auto str = print(); + context_.reset(); + CASPAR_LOG(info) << str << L" Successfully Uninitialized."; } - virtual safe_ptr receive() + virtual safe_ptr receive(int) { - return context_->get_frame(); + auto frame = context_->get_frame(); + if(frame != core::basic_frame::late()) + last_frame_ = frame; + return frame; + } + + virtual safe_ptr last_frame() const + { + return disable_audio(last_frame_); + } + + virtual int64_t nb_frames() const + { + return length_; } std::wstring print() const @@ -234,24 +285,24 @@ public: } }; -safe_ptr create_decklink_producer(const safe_ptr& frame_factory, 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) - device_index = lexical_cast_or_default(params[1], 1); + 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) - { - auto desc = core::video_format_desc::get(params[2]); - if(desc.format != core::video_format::invalid) - format_desc = desc; - } + auto format_desc = core::video_format_desc::get(core::get_param(L"FORMAT", params, L"INVALID")); - return make_safe(frame_factory, 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