X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fdecklink%2Fproducer%2Fdecklink_producer.cpp;h=176e79f56ee79dd6394a27c1b285581d480702d4;hb=refs%2Ftags%2F2.0.0.2;hp=06eca9c28f462bf51425f5dd52056ca27f1eb232;hpb=890bdc2d683e293ee744dc062a5ecb10c445e34d;p=casparcg diff --git a/modules/decklink/producer/decklink_producer.cpp b/modules/decklink/producer/decklink_producer.cpp index 06eca9c28..176e79f56 100644 --- a/modules/decklink/producer/decklink_producer.cpp +++ b/modules/decklink/producer/decklink_producer.cpp @@ -27,21 +27,22 @@ #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) @@ -70,84 +71,9 @@ extern "C" #include -namespace caspar { - -class frame_filter -{ - std::unique_ptr filter_; - safe_ptr frame_factory_; - std::deque> audio_buffer_; - tbb::task_group task_group_; - - std::vector> buffer_; - -public: - frame_filter(const std::string& filter_str, const safe_ptr& frame_factory) - : filter_(filter_str.empty() ? nullptr : new filter(filter_str)) - , frame_factory_(frame_factory) - { - } - - bool execute(const safe_ptr& input_frame, safe_ptr& output_frame) - { - if(!filter_) - { - input_frame->commit(); - output_frame = input_frame; - return true; - } - - auto desc = input_frame->get_pixel_format_desc(); - - auto av_frame = as_av_frame(input_frame); - - audio_buffer_.push_back(std::move(input_frame->audio_data())); - - task_group_.wait(); - - filter_->push(av_frame); +namespace caspar { namespace decklink { - bool result = try_pop(output_frame); - - task_group_.run([this] - { - buffer_ = filter_->poll(); - }); - - return result; - } - -private: - - bool try_pop(safe_ptr& output) - { - if(buffer_.empty()) - return false; - - auto audio_data = std::move(audio_buffer_.front()); - audio_buffer_.pop_back(); - - if(buffer_.size() == 2) - { - auto frame1 = make_write_frame(this, buffer_[0], frame_factory_); - auto frame2 = make_write_frame(this, buffer_[1], frame_factory_); - frame1->audio_data() = std::move(audio_data); - frame2->get_audio_transform().set_has_audio(false); - output = core::basic_frame::interlace(frame1, frame2, frame_factory_->get_video_format_desc().mode); - } - else if(buffer_.size() > 0) - { - auto frame1 = make_write_frame(this, buffer_[0], frame_factory_); - frame1->audio_data() = std::move(audio_data); - output = frame1; - } - buffer_.clear(); - - return true; - } -}; - -class decklink_producer : public IDeckLinkInputCallback +class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback { CComPtr decklink_; CComQIPtr input_; @@ -156,38 +82,40 @@ class decklink_producer : public IDeckLinkInputCallback const core::video_format_desc format_desc_; const size_t device_index_; - std::shared_ptr graph_; - boost::timer perf_timer_; - - std::vector audio_data_; - + safe_ptr graph_; + boost::timer tick_timer_; + boost::timer frame_timer_; + safe_ptr frame_factory_; tbb::concurrent_bounded_queue> frame_buffer_; - safe_ptr tail_; std::exception_ptr exception_; - frame_filter filter_; + ffmpeg::filter filter_; + + ffmpeg::frame_muxer muxer_; public: - decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const safe_ptr& frame_factory, const std::wstring& filter_str) + 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_(narrow(filter_str), frame_factory_) + , 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)); + graph_->set_text(print()); + diagnostics::register_graph(graph_); auto display_mode = get_display_mode(input_, format_desc_.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault); @@ -197,7 +125,7 @@ public: << msg_info(narrow(print()) + " Could not enable video input.") << boost::errinfo_api_function("EnableVideoInput")); - if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) + 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")); @@ -235,68 +163,53 @@ public: virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio) { - if(!video || video->GetWidth() != static_cast(format_desc_.width) || video->GetHeight() != static_cast(format_desc_.height)) + if(!video) return S_OK; try { - auto result = core::basic_frame::empty(); + graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5); + tick_timer_.restart(); + + frame_timer_.restart(); - 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; if(FAILED(video->GetBytes(&bytes)) || !bytes) return S_OK; - - 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]; - } - }); - frame->set_type(format_desc_.mode); + 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)); } - - filter_.execute(frame, result); - - if(!frame_buffer_.try_push(result)) - graph_->add_tag("dropped-frame"); + 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"); + } + + 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())); } @@ -314,10 +227,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 @@ -328,18 +242,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, const std::wstring& filter_str = L"") + 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_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 @@ -348,33 +286,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); - - 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 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()); - std::wstring filter_str = L""; + boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1"); + boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1"); - 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; - } + 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, filter_str); + 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