\r
#include "decklink_producer.h"\r
\r
-#include "../interop/DeckLinkAPI_h.h" // TODO: Change this\r
-#include "../util/util.h" // TODO: Change this\r
+#include "../interop/DeckLinkAPI_h.h"\r
+#include "../util/util.h"\r
\r
+#include "../../ffmpeg/producer/filter/filter.h"\r
+#include "../../ffmpeg/producer/util.h"\r
+#include "../../ffmpeg/producer/frame_muxer.h"\r
+\r
+#include <common/log/log.h>\r
#include <common/diagnostics/graph.h>\r
-#include <common/concurrency/executor.h>\r
+#include <common/concurrency/com_context.h>\r
#include <common/exception/exceptions.h>\r
-#include <common/utility/timer.h>\r
+#include <common/memory/memclr.h>\r
\r
+#include <core/mixer/write_frame.h>\r
+#include <core/producer/frame/frame_transform.h>\r
#include <core/producer/frame/frame_factory.h>\r
-#include <core/producer/frame/write_frame.h>\r
\r
-#include <tbb/concurrent_queue.h>\r
-#include <tbb/atomic.h>\r
+#include <agents.h>\r
+#include <agents_extras.h>\r
+#include <ppl.h>\r
\r
#include <boost/algorithm/string.hpp>\r
+#include <boost/foreach.hpp>\r
+#include <boost/timer.hpp>\r
+\r
+#if defined(_MSC_VER)\r
+#pragma warning (push)\r
+#pragma warning (disable : 4244)\r
+#endif\r
+extern "C" \r
+{\r
+ #define __STDC_CONSTANT_MACROS\r
+ #define __STDC_LIMIT_MACROS\r
+ #include <libavcodec/avcodec.h>\r
+}\r
+#if defined(_MSC_VER)\r
+#pragma warning (pop)\r
+#endif\r
\r
#pragma warning(push)\r
#pragma warning(disable : 4996)\r
\r
#include <functional>\r
\r
-namespace caspar { \r
-\r
-class decklink_input : public IDeckLinkInputCallback\r
-{\r
- struct co_init\r
- {\r
- co_init(){CoInitialize(nullptr);}\r
- ~co_init(){CoUninitialize();}\r
- } co_;\r
-\r
- const core::video_format_desc format_desc_;\r
- std::wstring model_name_;\r
- const size_t device_index_;\r
-\r
- std::shared_ptr<diagnostics::graph> graph_;\r
- timer perf_timer_;\r
-\r
- CComPtr<IDeckLink> decklink_;\r
- CComQIPtr<IDeckLinkInput> input_;\r
- IDeckLinkDisplayMode* d_mode_;\r
+namespace caspar { namespace decklink {\r
+ \r
+typedef std::pair<CComPtr<IDeckLinkVideoInputFrame>, CComPtr<IDeckLinkAudioInputPacket>> frame_packet;\r
\r
- std::vector<short> audio_data_;\r
+class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback\r
+{ \r
+ Concurrency::ITarget<frame_packet>& target_;\r
\r
- std::shared_ptr<core::frame_factory> frame_factory_;\r
+ CComPtr<IDeckLink> decklink_;\r
+ CComQIPtr<IDeckLinkInput> input_;\r
+ \r
+ const std::wstring model_name_;\r
+ const core::video_format_desc format_desc_;\r
+ const size_t device_index_;\r
\r
- tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
- safe_ptr<core::basic_frame> tail_;\r
+ safe_ptr<diagnostics::graph> graph_;\r
+ boost::timer tick_timer_;\r
+ boost::timer frame_timer_;\r
\r
public:\r
- decklink_input(const core::video_format_desc& format_desc, size_t device_index, const std::shared_ptr<core::frame_factory>& frame_factory)\r
- : format_desc_(format_desc)\r
+ decklink_producer(Concurrency::ITarget<frame_packet>& target, const core::video_format_desc& format_desc, size_t device_index)\r
+ : target_(target)\r
+ , decklink_(get_device(device_index))\r
+ , input_(decklink_)\r
+ , model_name_(get_model_name(decklink_))\r
+ , format_desc_(format_desc)\r
, device_index_(device_index)\r
- , model_name_(L"decklink")\r
- , frame_factory_(frame_factory)\r
- , tail_(core::basic_frame::empty())\r
- {\r
- frame_buffer_.set_capacity(4);\r
- \r
- CComPtr<IDeckLinkIterator> pDecklinkIterator;\r
- if(FAILED(pDecklinkIterator.CoCreateInstance(CLSID_CDeckLinkIterator)))\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " No Decklink drivers installed."));\r
-\r
- size_t n = 0;\r
- while(n < device_index_ && pDecklinkIterator->Next(&decklink_) == S_OK){++n;} \r
-\r
- if(n != device_index_ || !decklink_)\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Decklink card not found.") << arg_name_info("device_index") << arg_value_info(boost::lexical_cast<std::string>(device_index_)));\r
-\r
- input_ = decklink_;\r
-\r
- BSTR pModelName;\r
- decklink_->GetModelName(&pModelName);\r
- model_name_ = std::wstring(pModelName);\r
-\r
- graph_ = diagnostics::create_graph(boost::bind(&decklink_input::print, this));\r
+ , graph_ (diagnostics::create_graph("", false))\r
+ { \r
graph_->add_guide("tick-time", 0.5);\r
- graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
+ graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); \r
+ graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));\r
+ graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
+ graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
+ graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
+ graph_->update_text(narrow(print()));\r
+\r
+ auto display_mode = get_display_mode(input_, format_desc_.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault);\r
\r
- unsigned long decklinkVideoFormat = GetDecklinkVideoFormat(format_desc.format);\r
- if(decklinkVideoFormat == ULONG_MAX) \r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));\r
-\r
- d_mode_ = get_display_mode((BMDDisplayMode)decklinkVideoFormat);\r
- if(d_mode_ == nullptr) \r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));\r
-\r
- BMDDisplayModeSupport displayModeSupport;\r
- if(FAILED(input_->DoesSupportVideoMode((BMDDisplayMode)decklinkVideoFormat, bmdFormat8BitYUV, bmdVideoOutputFlagDefault, &displayModeSupport, nullptr)))\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));\r
-\r
- // NOTE: bmdFormat8BitARGB does not seem to work with Decklink HD Extreme 3D\r
- if(FAILED(input_->EnableVideoInput((BMDDisplayMode)decklinkVideoFormat, bmdFormat8BitYUV, 0))) \r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video input."));\r
-\r
- if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2))) \r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio input."));\r
+ // NOTE: bmdFormat8BitARGB is currently not supported by any decklink card. (2011-05-08)\r
+ if(FAILED(input_->EnableVideoInput(display_mode, bmdFormat8BitYUV, 0))) \r
+ BOOST_THROW_EXCEPTION(caspar_exception() \r
+ << msg_info(narrow(print()) + " Could not enable video input.")\r
+ << boost::errinfo_api_function("EnableVideoInput"));\r
+\r
+ if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, format_desc_.audio_channels))) \r
+ BOOST_THROW_EXCEPTION(caspar_exception() \r
+ << msg_info(narrow(print()) + " Could not enable audio input.")\r
+ << boost::errinfo_api_function("EnableAudioInput"));\r
\r
if (FAILED(input_->SetCallback(this)) != S_OK)\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set input callback."));\r
+ BOOST_THROW_EXCEPTION(caspar_exception() \r
+ << msg_info(narrow(print()) + " Failed to set input callback.")\r
+ << boost::errinfo_api_function("SetCallback"));\r
\r
if(FAILED(input_->StartStreams()))\r
- BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start input stream."));\r
+ BOOST_THROW_EXCEPTION(caspar_exception() \r
+ << msg_info(narrow(print()) + " Failed to start input stream.")\r
+ << boost::errinfo_api_function("StartStreams"));\r
+\r
+ CASPAR_LOG(info) << print() << L" Successfully Initialized.";\r
\r
- CASPAR_LOG(info) << print() << " successfully initialized decklink for " << format_desc_.name;\r
+ graph_->start();\r
}\r
\r
- ~decklink_input()\r
+ ~decklink_producer()\r
{\r
+ Concurrency::scoped_oversubcription_token oversubscribe;\r
if(input_ != nullptr) \r
{\r
input_->StopStreams();\r
\r
virtual HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(BMDVideoInputFormatChangedEvents /*notificationEvents*/, IDeckLinkDisplayMode* newDisplayMode, BMDDetectedVideoInputFormatFlags /*detectedSignalFlags*/)\r
{\r
- d_mode_ = newDisplayMode;\r
return S_OK;\r
}\r
\r
- // TODO: Enable audio input\r
virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio)\r
{ \r
- graph_->update_value("tick-time", static_cast<float>(perf_timer_.elapsed()/format_desc_.interval*0.5));\r
- perf_timer_.reset();\r
+ if(!Concurrency::asend(target_, frame_packet(CComPtr<IDeckLinkVideoInputFrame>(video), CComPtr<IDeckLinkAudioInputPacket>(audio))))\r
+ graph_->add_tag("dropped-frame");\r
+ return S_OK;\r
+ }\r
+ \r
+ std::wstring print() const\r
+ {\r
+ return model_name_ + L" [" + boost::lexical_cast<std::wstring>(device_index_) + L"]";\r
+ }\r
+};\r
+ \r
+class decklink_producer_proxy : public Concurrency::agent, public core::frame_producer\r
+{ \r
+ Concurrency::bounded_buffer<safe_ptr<AVFrame>> video_frames_;\r
+ Concurrency::bounded_buffer<safe_ptr<core::audio_buffer>> audio_buffers_;\r
+ Concurrency::bounded_buffer<safe_ptr<core::basic_frame>> muxed_frames_;\r
\r
- if(!video)\r
- return S_OK; \r
- \r
- void* bytes = nullptr;\r
- if(FAILED(video->GetBytes(&bytes)))\r
- return S_OK;\r
-\r
- core::pixel_format_desc desc;\r
- desc.pix_fmt = core::pixel_format::ycbcr;\r
- desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth(), d_mode_->GetHeight(), 1));\r
- desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth()/2, d_mode_->GetHeight(), 1));\r
- desc.planes.push_back(core::pixel_format_desc::plane(d_mode_->GetWidth()/2, d_mode_->GetHeight(), 1)); \r
- auto frame = frame_factory_->create_frame(desc);\r
-\r
- unsigned char* data = reinterpret_cast<unsigned char*>(bytes);\r
- int frame_size = (d_mode_->GetWidth() * 16 / 8) * d_mode_->GetHeight();\r
-\r
- // Convert to planar YUV422\r
- unsigned char* y = frame->image_data(0).begin();\r
- unsigned char* cb = frame->image_data(1).begin();\r
- unsigned char* cr = frame->image_data(2).begin();\r
+ const core::video_format_desc format_desc_;\r
+ const size_t device_index_;\r
+\r
+ safe_ptr<core::basic_frame> last_frame_;\r
+ const int64_t length_;\r
+\r
+ ffmpeg::filter filter_;\r
\r
- tbb::parallel_for(tbb::blocked_range<size_t>(0, frame_size/4), [&](const tbb::blocked_range<size_t>& r)\r
- {\r
- for(auto n = r.begin(); n != r.end(); ++n)\r
- {\r
- cb[n] = data[n*4+0];\r
- y [n*2+0] = data[n*4+1];\r
- cr[n] = data[n*4+2];\r
- y [n*2+1] = data[n*4+3];\r
- }\r
- });\r
+ ffmpeg::frame_muxer2 muxer_;\r
\r
- if(audio && SUCCEEDED(audio->GetBytes(&bytes)))\r
- {\r
- auto sample_frame_count = audio->GetSampleFrameCount();\r
- auto audio_data = reinterpret_cast<short*>(bytes);\r
- audio_data_.insert(audio_data_.end(), audio_data, audio_data + sample_frame_count*2);\r
+ mutable Concurrency::single_assignment<std::wstring> print_;\r
\r
- if(audio_data_.size() > 3840)\r
- {\r
- frame->audio_data() = std::vector<short>(audio_data_.begin(), audio_data_.begin() + 3840);\r
- audio_data_.erase(audio_data_.begin(), audio_data_.begin() + 3840);\r
- frame_buffer_.try_push(frame);\r
- }\r
- }\r
- else\r
- frame_buffer_.try_push(frame);\r
+ safe_ptr<Concurrency::semaphore> semaphore_;\r
\r
- return S_OK;\r
+ volatile bool is_running_;\r
+public:\r
+\r
+ explicit decklink_producer_proxy(const safe_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str, int64_t length)\r
+ : video_frames_(1)\r
+ , audio_buffers_(1)\r
+ , muxed_frames_(1)\r
+ , format_desc_(format_desc)\r
+ , device_index_(device_index)\r
+ , last_frame_(core::basic_frame::empty())\r
+ , length_(length)\r
+ , filter_(filter_str)\r
+ , muxer_(&video_frames_, &audio_buffers_, muxed_frames_, ffmpeg::double_rate(filter_str) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory)\r
+ , is_running_(true)\r
+ , semaphore_(make_safe<Concurrency::semaphore>(3))\r
+ {\r
+ agent::start();\r
}\r
\r
- IDeckLinkDisplayMode* get_display_mode(BMDDisplayMode mode)\r
- { \r
- CComPtr<IDeckLinkDisplayModeIterator> iterator;\r
- IDeckLinkDisplayMode* d_mode;\r
- \r
- if (input_->GetDisplayModeIterator(&iterator) != S_OK)\r
- return nullptr;\r
+ ~decklink_producer_proxy()\r
+ {\r
+ is_running_ = false;\r
+ agent::wait(this);\r
+ }\r
+ \r
+ virtual safe_ptr<core::basic_frame> receive(int)\r
+ {\r
+ auto frame = core::basic_frame::late();\r
\r
- while (iterator->Next(&d_mode) == S_OK)\r
+ try\r
{\r
- if(d_mode->GetDisplayMode() == mode)\r
- return d_mode;\r
+ last_frame_ = frame = Concurrency::receive(muxed_frames_);\r
}\r
- return nullptr;\r
+ catch(Concurrency::operation_timed_out&)\r
+ { \r
+ //graph_->add_tag("underflow"); \r
+ }\r
+\r
+ return frame;\r
}\r
\r
- safe_ptr<core::basic_frame> get_frame()\r
+ virtual safe_ptr<core::basic_frame> last_frame() const\r
{\r
- frame_buffer_.try_pop(tail_);\r
- return tail_;\r
+ return disable_audio(last_frame_);\r
}\r
\r
- std::wstring print() const\r
+ virtual int64_t nb_frames() const \r
{\r
- return L" [" + model_name_ + L"device:" + boost::lexical_cast<std::wstring>(device_index_) + L"]";\r
+ return length_;\r
}\r
-};\r
\r
-class decklink_producer : public core::frame_producer\r
-{ \r
- const size_t device_index_;\r
+ std::wstring print() const\r
+ {\r
+ return print_.value();\r
+ }\r
\r
- std::unique_ptr<decklink_input> input_;\r
- \r
- const core::video_format_desc format_desc_;\r
- \r
- executor executor_;\r
-public:\r
+ virtual void run()\r
+ {\r
+ try\r
+ {\r
+ struct co_init\r
+ {\r
+ co_init() {CoInitialize(NULL);}\r
+ ~co_init() {CoUninitialize();}\r
+ } init;\r
+ \r
+ Concurrency::bounded_buffer<frame_packet> input_buffer(2);\r
\r
- explicit decklink_producer(const core::video_format_desc& format_desc, size_t device_index)\r
- : format_desc_(format_desc) \r
- , device_index_(device_index)\r
- , executor_(L"decklink_producer"){}\r
+ std::unique_ptr<decklink_producer> producer;\r
+ { \r
+ Concurrency::scoped_oversubcription_token oversubscribe;\r
+ producer.reset(new decklink_producer(input_buffer, format_desc_, device_index_));\r
+ }\r
\r
- ~decklink_producer()\r
- { \r
- executor_.invoke([this]\r
- {\r
- input_ = nullptr;\r
- });\r
- }\r
+ Concurrency::send(print_, producer->print());\r
\r
- virtual void set_frame_factory(const safe_ptr<core::frame_factory>& frame_factory)\r
- {\r
- executor_.start();\r
- executor_.invoke([=]\r
+ while(is_running_)\r
+ {\r
+ auto packet = Concurrency::receive(input_buffer);\r
+ auto video = packet.first;\r
+ auto audio = packet.second;\r
+ \r
+ void* bytes = nullptr;\r
+ if(FAILED(video->GetBytes(&bytes)) || !bytes)\r
+ continue;\r
+ \r
+ safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free); \r
+ avcodec_get_frame_defaults(av_frame.get());\r
+ \r
+ av_frame->data[0] = reinterpret_cast<uint8_t*>(bytes);\r
+ av_frame->linesize[0] = video->GetRowBytes(); \r
+ av_frame->format = PIX_FMT_UYVY422;\r
+ av_frame->width = video->GetWidth();\r
+ av_frame->height = video->GetHeight();\r
+ av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;\r
+ av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;\r
+ \r
+ filter_.push(av_frame);\r
+\r
+ Concurrency::parallel_invoke(\r
+ [&]\r
+ {\r
+ while(true)\r
+ {\r
+ auto frame = filter_.poll();\r
+ if(!frame)\r
+ break;\r
+ Concurrency::send(video_frames_, make_safe_ptr(frame));\r
+ }\r
+ },\r
+ [&]\r
+ { \r
+ // It is assumed that audio is always equal or ahead of video.\r
+ if(audio && SUCCEEDED(audio->GetBytes(&bytes)))\r
+ {\r
+ auto sample_frame_count = audio->GetSampleFrameCount();\r
+ auto audio_data = reinterpret_cast<int32_t*>(bytes);\r
+ Concurrency::send(audio_buffers_, make_safe<core::audio_buffer>(audio_data, audio_data + sample_frame_count*format_desc_.audio_channels));\r
+ }\r
+ else\r
+ Concurrency::send(audio_buffers_, ffmpeg::empty_audio()); \r
+ });\r
+ }\r
+\r
+ }\r
+ catch(...)\r
{\r
- input_.reset(new decklink_input(format_desc_, device_index_, frame_factory));\r
- });\r
- }\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ }\r
\r
- virtual safe_ptr<core::basic_frame> receive()\r
- {\r
- return input_->get_frame();\r
- }\r
- \r
- std::wstring print() const\r
- {\r
- return input_ ? input_->print() : L"decklink_producer";\r
+ CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
+\r
+ done();\r
}\r
};\r
\r
-safe_ptr<core::frame_producer> create_decklink_producer(const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
{\r
if(params.empty() || !boost::iequals(params[0], "decklink"))\r
return core::frame_producer::empty();\r
\r
- size_t device_index = 1;\r
- if(params.size() > 1)\r
- {\r
- try{device_index = boost::lexical_cast<int>(params[1]);}\r
- catch(boost::bad_lexical_cast&){}\r
- }\r
+ auto device_index = core::get_param(L"DEVICE", params, 1);\r
+ auto filter_str = core::get_param<std::wstring>(L"FILTER", params, L""); \r
+ auto length = core::get_param(L"LENGTH", params, std::numeric_limits<int64_t>::max()); \r
+ \r
+ boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1");\r
+ boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1");\r
\r
- core::video_format_desc format_desc = core::video_format_desc::get(L"PAL");\r
- if(params.size() > 2)\r
- {\r
- format_desc = core::video_format_desc::get(params[2]);\r
- format_desc = format_desc.format != core::video_format::invalid ? format_desc : core::video_format_desc::get(L"PAL");\r
- }\r
+ auto format_desc = core::video_format_desc::get(core::get_param<std::wstring>(L"FORMAT", params, L"INVALID"));\r
\r
- return make_safe<decklink_producer>(format_desc, device_index);\r
+ if(format_desc.format == core::video_format::invalid)\r
+ format_desc = frame_factory->get_video_format_desc();\r
+ \r
+ return make_safe<decklink_producer_proxy>(frame_factory, format_desc, device_index, filter_str, length);\r
}\r
\r
-}
\ No newline at end of file
+}}
\ No newline at end of file