2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
24 #include "bluefish_consumer.h"
25 #include "../util/blue_velvet.h"
26 #include "../util/memory.h"
28 #include <core/video_format.h>
29 #include <core/frame/frame.h>
30 #include <core/frame/audio_channel_layout.h>
31 #include <core/help/help_repository.h>
32 #include <core/help/help_sink.h>
34 #include <common/executor.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/array.h>
37 #include <common/memshfl.h>
38 #include <common/param.h>
40 #include <core/consumer/frame_consumer.h>
41 #include <core/mixer/audio/audio_util.h>
43 #include <tbb/concurrent_queue.h>
44 #include <tbb/atomic.h>
46 #include <common/assert.h>
47 #include <boost/lexical_cast.hpp>
48 #include <boost/timer.hpp>
49 #include <boost/range/algorithm.hpp>
50 #include <boost/property_tree/ptree.hpp>
51 #include <boost/algorithm/string.hpp>
58 namespace caspar { namespace bluefish {
60 enum class hardware_downstream_keyer_mode
66 enum class hardware_downstream_keyer_audio_source
69 VideoOutputChannel = 2
72 enum class bluefish_hardware_output_channel
74 // ** current Bluefish hardware only support 4224 on channels A and C.
75 hardware_4224_channel_a,
76 hardware_4224_channel_c,
77 hardware_422_channel_a,
78 hardware_422_channel_b,
79 hardware_422_channel_c,
80 hardware_422_channel_d,
81 default_output_channel = hardware_422_channel_a
84 EBlueVideoChannel get_bluesdk_videochannel_from_streamid(bluefish_hardware_output_channel streamid)
86 /*This function would return the corresponding EBlueVideoChannel from the device output channel*/
89 case bluefish_hardware_output_channel::hardware_422_channel_a: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
90 case bluefish_hardware_output_channel::hardware_4224_channel_a: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
91 case bluefish_hardware_output_channel::hardware_422_channel_b: return BLUE_VIDEO_OUTPUT_CHANNEL_B;
92 case bluefish_hardware_output_channel::hardware_4224_channel_c: return BLUE_VIDEO_OUTPUT_CHANNEL_C;
93 case bluefish_hardware_output_channel::hardware_422_channel_c: return BLUE_VIDEO_OUTPUT_CHANNEL_C;
94 case bluefish_hardware_output_channel::hardware_422_channel_d: return BLUE_VIDEO_OUTPUT_CHANNEL_D;
95 default: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
99 bool get_videooutput_channel_routing_info_from_streamid(bluefish_hardware_output_channel streamid,
100 EEpochRoutingElements & channelSrcElement,
101 EEpochRoutingElements & sdioutputDstElement)
105 case bluefish_hardware_output_channel::hardware_4224_channel_a: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
106 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
108 case bluefish_hardware_output_channel::hardware_4224_channel_c: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
109 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
111 case bluefish_hardware_output_channel::hardware_422_channel_a: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
112 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
114 case bluefish_hardware_output_channel::hardware_422_channel_b: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHB;
115 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_B;
117 case bluefish_hardware_output_channel::hardware_422_channel_c: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
118 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
120 case bluefish_hardware_output_channel::hardware_422_channel_d: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHD;
121 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_D;
123 default: return false;
128 struct bluefish_consumer : boost::noncopyable
130 spl::shared_ptr<bvc_wrapper> blue_;
131 const unsigned int device_index_;
132 const core::video_format_desc format_desc_;
133 const core::audio_channel_layout channel_layout_;
134 core::audio_channel_remapper channel_remapper_;
135 const int channel_index_;
137 const std::wstring model_name_;
139 spl::shared_ptr<diagnostics::graph> graph_;
140 boost::timer frame_timer_;
141 boost::timer tick_timer_;
142 boost::timer sync_timer_;
144 unsigned int vid_fmt_;
146 std::array<blue_dma_buffer_ptr, 4> reserved_frames_;
147 tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
148 tbb::atomic<int64_t> presentation_delay_millis_;
149 core::const_frame previous_frame_ = core::const_frame::empty();
151 const bool embedded_audio_;
152 const bool key_only_;
155 hardware_downstream_keyer_mode hardware_keyer_;
156 hardware_downstream_keyer_audio_source keyer_audio_source_;
157 bluefish_hardware_output_channel device_output_channel_;
160 const core::video_format_desc& format_desc,
161 const core::audio_channel_layout& in_channel_layout,
162 const core::audio_channel_layout& out_channel_layout,
166 hardware_downstream_keyer_mode keyer,
167 hardware_downstream_keyer_audio_source keyer_audio_source,
169 bluefish_hardware_output_channel device_output_channel)
170 : blue_(create_blue(device_index))
171 , device_index_(device_index)
172 , format_desc_(format_desc)
173 , channel_layout_(out_channel_layout)
174 , channel_remapper_(in_channel_layout, out_channel_layout)
175 , channel_index_(channel_index)
176 , model_name_(get_card_desc(*blue_, device_index))
177 , vid_fmt_(get_video_mode(*blue_, format_desc))
178 , embedded_audio_(embedded_audio)
179 , hardware_keyer_(keyer)
180 , keyer_audio_source_(keyer_audio_source)
181 , key_only_(key_only)
183 , device_output_channel_(device_output_channel)
185 executor_.set_capacity(1);
186 presentation_delay_millis_ = 0;
188 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
189 graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f));
190 graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
191 graph_->set_text(print());
192 diagnostics::register_graph(graph_);
194 // Specify the video channel
195 setup_hardware_output_channel();
197 // Setting output Video mode
198 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_MODE, vid_fmt_)))
199 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set videomode."));
201 // Select Update Mode for output
202 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_UPDATE_TYPE, UPD_FMT_FRAME)))
203 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
205 disable_video_output();
206 setup_hardware_output_channel_routing();
208 //Select output memory format
209 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC)))
210 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
212 //Select image orientation
213 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
214 CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";
217 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_RGB_DATA_RANGE, CGR_RANGE)))
218 CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";
220 if(!embedded_audio_ || (hardware_keyer_ == hardware_downstream_keyer_mode::enable && keyer_audio_source_ == hardware_downstream_keyer_audio_source::VideoOutputChannel) )
222 if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
223 CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");
224 CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
229 EMBEDDED_AUDIO_OUTPUT | blue_emb_audio_group1_enable;
231 if (channel_layout_.num_channels > 4)
232 audio_value |= blue_emb_audio_group2_enable;
234 if (channel_layout_.num_channels > 8)
235 audio_value |= blue_emb_audio_group3_enable;
237 if (channel_layout_.num_channels > 12)
238 audio_value |= blue_emb_audio_group4_enable;
240 if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, audio_value)))
241 CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");
242 CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
245 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE)))
246 CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine.");
248 if (is_epoch_card((*blue_)))
249 setup_hardware_downstream_keyer(hardware_keyer_, keyer_audio_source_);
251 enable_video_output();
254 boost::range::generate(reserved_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
263 disable_video_output();
269 CASPAR_LOG_CURRENT_EXCEPTION();
273 void setup_hardware_output_channel()
275 // This function would be used to setup the logic video channel in the bluefish hardware
276 EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
277 if (is_epoch_card((*blue_)))
279 if (out_vid_channel != BLUE_VIDEOCHANNEL_INVALID)
281 if (BLUE_FAIL(blue_->set_card_property32(DEFAULT_VIDEO_OUTPUT_CHANNEL, out_vid_channel)))
282 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set video stream."));
287 void setup_hardware_output_channel_routing()
289 //This function would be used to setup the dual link and any other routing that would be required .
290 if (is_epoch_card(*blue_))
292 EBlueVideoChannel blueVideoOutputChannel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
293 EEpochRoutingElements src_element = (EEpochRoutingElements)0;
294 EEpochRoutingElements dst_element = (EEpochRoutingElements)0;
295 get_videooutput_channel_routing_info_from_streamid(device_output_channel_, src_element, dst_element);
296 bool duallink_4224_enabled = false;
298 if (device_output_channel_ == bluefish_hardware_output_channel::hardware_4224_channel_a || device_output_channel_ == bluefish_hardware_output_channel::hardware_4224_channel_c)
299 duallink_4224_enabled = true;
301 // Enable/Disable dual link output
302 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT, duallink_4224_enabled)))
303 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable/disable dual link."));
305 if (!duallink_4224_enabled)
307 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_Independent_422)))
308 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2."));
310 ULONG routingValue = EPOCH_SET_ROUTING(src_element, dst_element, BLUE_CONNECTOR_PROP_SINGLE_LINK);
311 if (BLUE_FAIL(blue_->set_card_property32(MR2_ROUTING, routingValue)))
312 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to MR 2 routing."));
314 // If single link 422, but on second channel AND on Neutron we need to set Genlock to Aux.
315 if (is_epoch_neutron_1i2o_card((*blue_)))
317 if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_B)
319 ULONG genLockSource = BlueGenlockAux;
320 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
321 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
324 if (is_epoch_neutron_3o_card((*blue_)))
326 if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
328 ULONG genLockSource = BlueGenlockAux;
329 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
330 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
334 else // dual Link IS enabled, ie. 4224 Fill and Key
336 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
337 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
339 if (is_epoch_neutron_1i2o_card((*blue_))) // Neutron cards require setting the Genlock conector to Aux to enable them to do Dual-Link
341 ULONG genLockSource = BlueGenlockAux;
342 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
343 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
345 else if (is_epoch_neutron_3o_card((*blue_)))
347 if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
349 ULONG genLockSource = BlueGenlockAux;
350 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
351 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
358 void setup_hardware_downstream_keyer(hardware_downstream_keyer_mode keyer, hardware_downstream_keyer_audio_source audio_source)
360 unsigned int keyer_control_value = 0, card_feature_value = 0;
361 unsigned int card_connector_value = 0;
362 unsigned int nOutputStreams = 0;
363 unsigned int nInputStreams = 0;
364 unsigned int nInputSDIConnector = 0;
365 unsigned int nOutputSDIConnector = 0;
366 if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_STREAM_INFO, card_feature_value)))
368 nOutputStreams = CARD_FEATURE_GET_SDI_OUTPUT_STREAM_COUNT(card_feature_value);
369 nInputStreams = CARD_FEATURE_GET_SDI_INPUT_STREAM_COUNT(card_feature_value);
371 if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_CONNECTOR_INFO, card_connector_value)))
373 nOutputSDIConnector = CARD_FEATURE_GET_SDI_OUTPUT_CONNECTOR_COUNT(card_connector_value);
374 nInputSDIConnector = CARD_FEATURE_GET_SDI_INPUT_CONNECTOR_COUNT(card_connector_value);
376 if (nInputSDIConnector == 0 || nInputStreams == 0)
379 if (keyer == hardware_downstream_keyer_mode::disable)
381 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLED(keyer_control_value);
382 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
384 else if (keyer == hardware_downstream_keyer_mode::enable)
386 unsigned int invalidVideoModeFlag = 0;
387 unsigned int inputVideoSignal = 0;
388 if (BLUE_FAIL(blue_->get_card_property32(INVALID_VIDEO_MODE_FLAG, invalidVideoModeFlag)))
389 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get invalid video mode flag"));
391 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLED(keyer_control_value);
392 if (BLUE_FAIL(blue_->get_card_property32(VIDEO_INPUT_SIGNAL_VIDEO_MODE, inputVideoSignal)))
393 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get video input signal mode"));
395 if (inputVideoSignal >= invalidVideoModeFlag)
396 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLE_OVER_BLACK(keyer_control_value);
398 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
401 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, BlueSDI_A_BNC)))
402 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set the genlock to the input for the HW keyer"));
405 if (audio_source == hardware_downstream_keyer_audio_source::SDIVideoInput && (keyer == hardware_downstream_keyer_mode::enable))
406 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_INPUT_ANCILLARY(keyer_control_value);
407 else if (audio_source == hardware_downstream_keyer_audio_source::VideoOutputChannel)
408 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_OUTPUT_ANCILLARY(keyer_control_value);
410 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_ONBOARD_KEYER, keyer_control_value)))
411 CASPAR_LOG(error) << print() << TEXT(" Failed to set keyer control.");
414 void enable_video_output()
416 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 0)))
417 CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");
420 void disable_video_output()
422 blue_->video_playback_stop(0,0);
423 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 1)))
424 CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");
427 std::future<bool> send(core::const_frame& frame)
429 return executor_.begin_invoke([=]() -> bool
433 display_frame(frame);
434 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
435 tick_timer_.restart();
439 CASPAR_LOG_CURRENT_EXCEPTION();
446 void display_frame(core::const_frame frame)
450 sync_timer_.restart();
451 unsigned long n_field = 0;
452 blue_->wait_video_output_sync(UPD_FMT_FRAME, n_field);
453 graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5);
455 frame_timer_.restart();
457 if (previous_frame_ != core::const_frame::empty())
458 presentation_delay_millis_ = previous_frame_.get_age_millis();
460 previous_frame_ = frame;
462 // Copy to local buffers
464 if(!frame.image_data().empty())
467 aligned_memshfl(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
469 A_memcpy(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size());
472 A_memset(reserved_frames_.front()->image_data(), 0, reserved_frames_.front()->image_size());
479 auto remapped_audio = channel_remapper_.mix_and_rearrange(frame.audio_data());
480 auto frame_audio = core::audio_32_to_24(remapped_audio);
481 encode_hanc(reinterpret_cast<BLUE_UINT32*>(reserved_frames_.front()->hanc_data()),
483 static_cast<int>(frame.audio_data().size()/channel_layout_.num_channels),
484 static_cast<int>(channel_layout_.num_channels));
486 blue_->system_buffer_write(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
487 static_cast<unsigned long>(reserved_frames_.front()->image_size()),
488 BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE),
491 blue_->system_buffer_write(reserved_frames_.front()->hanc_data(),
492 static_cast<unsigned long>(reserved_frames_.front()->hanc_size()),
493 BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC),
496 if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id()))))
497 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
501 blue_->system_buffer_write(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
502 static_cast<unsigned long>(reserved_frames_.front()->image_size()),
503 BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE),
506 if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id()))))
507 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
510 boost::range::rotate(reserved_frames_, std::begin(reserved_frames_)+1);
512 graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
515 void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
517 const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN;
518 auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable;
520 if (audio_nchannels > 4)
521 emb_audio_flag |= blue_emb_audio_group2_enable;
523 if (audio_nchannels > 8)
524 emb_audio_flag |= blue_emb_audio_group3_enable;
526 if (audio_nchannels > 12)
527 emb_audio_flag |= blue_emb_audio_group4_enable;
529 hanc_stream_info_struct hanc_stream_info;
530 memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
532 hanc_stream_info.AudioDBNArray[0] = -1;
533 hanc_stream_info.AudioDBNArray[1] = -1;
534 hanc_stream_info.AudioDBNArray[2] = -1;
535 hanc_stream_info.AudioDBNArray[3] = -1;
536 hanc_stream_info.hanc_data_ptr = hanc_data;
537 hanc_stream_info.video_mode = vid_fmt_;
539 int cardType = CRD_INVALID;
540 blue_->query_card_type(cardType, device_index_);
541 blue_->encode_hanc_frame(cardType, &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
544 std::wstring print() const
546 return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" +
547 boost::lexical_cast<std::wstring>(device_index_) + L"|" + format_desc_.name + L"]";
550 int64_t presentation_delay_millis() const
552 return presentation_delay_millis_;
556 struct bluefish_consumer_proxy : public core::frame_consumer
558 core::monitor::subject monitor_subject_;
560 std::unique_ptr<bluefish_consumer> consumer_;
561 const int device_index_;
562 const bool embedded_audio_;
563 const bool key_only_;
565 std::vector<int> audio_cadence_;
566 core::video_format_desc format_desc_;
567 core::audio_channel_layout in_channel_layout_ = core::audio_channel_layout::invalid();
568 core::audio_channel_layout out_channel_layout_;
569 hardware_downstream_keyer_mode hardware_keyer_;
570 hardware_downstream_keyer_audio_source hardware_keyer_audio_source_;
571 bluefish_hardware_output_channel hardware_output_channel_;
575 bluefish_consumer_proxy(int device_index,
578 hardware_downstream_keyer_mode keyer,
579 hardware_downstream_keyer_audio_source keyer_audio_source,
580 const core::audio_channel_layout& out_channel_layout,
581 bluefish_hardware_output_channel hardware_output_channel)
583 : device_index_(device_index)
584 , embedded_audio_(embedded_audio)
585 , key_only_(key_only)
586 , hardware_keyer_(keyer)
587 , hardware_keyer_audio_source_(keyer_audio_source)
588 , out_channel_layout_(out_channel_layout)
589 , hardware_output_channel_(hardware_output_channel)
595 void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) override
597 format_desc_ = format_desc;
598 in_channel_layout_ = channel_layout;
599 audio_cadence_ = format_desc.audio_cadence;
601 if (out_channel_layout_ == core::audio_channel_layout::invalid())
602 out_channel_layout_ = in_channel_layout_;
605 consumer_.reset(new bluefish_consumer( format_desc,
612 hardware_keyer_audio_source_,
614 hardware_output_channel_));
617 std::future<bool> send(core::const_frame frame) override
619 CASPAR_VERIFY(audio_cadence_.front() * in_channel_layout_.num_channels == static_cast<size_t>(frame.audio_data().size()));
620 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
621 return consumer_->send(frame);
624 std::wstring print() const override
626 return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
629 std::wstring name() const override
634 boost::property_tree::wptree info() const override
636 boost::property_tree::wptree info;
637 info.add(L"type", L"bluefish");
638 info.add(L"key-only", key_only_);
639 info.add(L"device", device_index_);
640 info.add(L"embedded-audio", embedded_audio_);
641 info.add(L"presentation-frame-age", presentation_frame_age_millis());
645 int buffer_depth() const override
650 int index() const override
652 return 400 + device_index_;
655 int64_t presentation_frame_age_millis() const override
657 return consumer_ ? consumer_->presentation_delay_millis() : 0;
660 core::monitor::subject& monitor_output()
662 return monitor_subject_;
667 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
669 sink.short_description(L"Sends video on an SDI output using Bluefish video cards.");
670 sink.syntax(L"BLUEFISH {[device_index:int]|1} {[embedded_audio:EMBEDDED_AUDIO]} {[key_only:KEY_ONLY]} {CHANNEL_LAYOUT [channel_layout:string]}");
672 ->text(L"Sends video on an SDI output using Bluefish video cards. Multiple video cards can be ")
673 ->text(L"installed in the same machine and used at the same time, they will be addressed via ")
674 ->text(L"different ")->code(L"device_index")->text(L" parameters.");
675 sink.para()->text(L"Specify ")->code(L"embedded_audio")->text(L" to embed audio into the SDI signal.");
677 ->text(L"Specifying ")->code(L"key_only")->text(L" will extract only the alpha channel from the ")
678 ->text(L"channel. This is useful when you have two SDI video cards, and neither has native support ")
679 ->text(L"for separate fill/key output");
680 sink.para()->text(L"Specify ")->code(L"channel_layout")->text(L" to output a different audio channel layout than the channel uses.");
681 sink.para()->text(L"Specify ")->code(L"BF_4224_CHANNELA")->text(L" to use channel A on the card to be configured to output "
682 L"4:2:2:4 with fill and key HD-SDI output's A and B on the card.\n"
683 L"This is the default option\n");
684 sink.para()->text(L"Specify ")->code(L"BF_4224_CHANNELC")->text(L" to use channel C on the card to be configured to output "
685 L" 4:2:2:4 with fill and key HD-SDI output's C and D on the card\n"
686 L" Channel C 4:2:2:4 support is only available on 4 output variant of Supernova and SupernovaS+ card\n");
688 sink.para()->text(L"Specify ")->code(L"ENABLE_KEYER")->text(L" to enable use of hardware keyer on the bluefish board.\n"
689 L"\t\t\tUsing this option hardware keyer would key using \n "
690 L"\t\t\tthe video input on the SDI as background layer \n"
691 L"\t\t\tand graphics Generated by CasparCG as foreground layer");
692 sink.para()->text(L"Specify ")->code(L"DISABLE_KEYER")->text(L" this option to disable hardware keyer on the bluefish board");
693 sink.para()->text(L"Specify ")->code(L"KEYER_AUDIO_SOURCE_SDIINPUT")->text(L" to enable SDI embedded audio to be transparently passed through the keyer to the SDI output\n");
694 sink.para()->text(L"Specify ")->code(L"KEYER_AUDIO_SOURCE_OUTPUT_CHANNEL")->text(L" to enable keyer to pass the audio provided by CasparCG to the SDI output\n");
696 sink.para()->text(L"Examples:");
697 sink.example(L">> ADD 1 BLUEFISH", L"uses the default device_index of 1.");
698 sink.example(L">> ADD 1 BLUEFISH 2", L"for device_index 2.");
700 L">> ADD 1 BLUEFISH 1 EMBEDDED_AUDIO\n"
701 L">> ADD 1 BLUEFISH 2 KEY_ONLY", L"uses device with index 1 as fill output with audio and device with index 2 as key output.");
706 spl::shared_ptr<core::frame_consumer> create_consumer( const std::vector<std::wstring>& params,
707 core::interaction_sink*,
708 std::vector<spl::shared_ptr<core::video_channel>> channels)
710 if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
711 return core::frame_consumer::empty();
713 const auto device_index = params.size() > 1 ? boost::lexical_cast<int>(params.at(1)) : 1;
715 const auto embedded_audio = contains_param( L"EMBEDDED_AUDIO", params);
716 const auto key_only = contains_param( L"KEY_ONLY", params);
717 const auto channel_layout = get_param( L"CHANNEL_LAYOUT", params);
718 const auto device_stream = contains_param( L"SDI-STREAM", params);
720 auto layout = core::audio_channel_layout::invalid();
722 if (!channel_layout.empty())
724 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout);
727 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + channel_layout + L" not found"));
729 layout = *found_layout;
731 bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
733 if (contains_param(L"BLUE_4224_CHANNELA", params))
734 device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
735 else if (contains_param(L"BLUE_4224_CHANNELC", params))
736 device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_c;
737 else if (contains_param(L"BLUE_422_CHANNELA", params))
738 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_a;
739 else if (contains_param(L"BLUE_422_CHANNELB", params))
740 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_b;
741 else if (contains_param(L"BLUE_422_CHANNELC", params))
742 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_c;
743 else if (contains_param(L"BLUE_422_CHANNELD", params))
744 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_d;
746 hardware_downstream_keyer_mode keyer = hardware_downstream_keyer_mode::disable;
748 if (contains_param(L"KEYER_DISABLED", params))
749 keyer = hardware_downstream_keyer_mode::disable;
750 else if (contains_param(L"KEYER_ENABLED", params))
751 keyer = hardware_downstream_keyer_mode::enable;
753 hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
754 if (contains_param(L"KEYER_AUDIO_SOURCE_SDIINPUT", params))
755 keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
757 if (contains_param(L"KEYER_AUDIO_SOURCE_OUTPUT_CHANNEL", params))
758 keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
760 return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer, keyer_audio_source, layout, device_output_channel);
763 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
764 const boost::property_tree::wptree& ptree, core::interaction_sink*,
765 std::vector<spl::shared_ptr<core::video_channel>> channels)
767 const auto device_index = ptree.get( L"device", 1);
768 const auto embedded_audio = ptree.get( L"embedded-audio", false);
769 const auto key_only = ptree.get( L"key-only", false);
770 const auto channel_layout = ptree.get_optional<std::wstring>( L"channel-layout");
771 const auto hardware_keyer_value = ptree.get(L"hardware-keyer", L"disabled");
772 const auto keyer_audio_source_value = ptree.get(L"hardware-keyer-audio-source", L"videooutputchannel");
773 const auto device_stream = ptree.get(L"sdi-stream", L"blue_4224_channela");
775 auto layout = core::audio_channel_layout::invalid();
779 CASPAR_SCOPED_CONTEXT_MSG("/channel-layout")
781 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(*channel_layout);
784 CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + *channel_layout + L" not found"));
786 layout = *found_layout;
789 bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
790 if (device_stream == L"blue_4224_channela")
791 device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
792 else if (device_stream == L"blue_4224_channelc")
793 device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_c;
794 else if (device_stream == L"blue_422_channela")
795 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_a;
796 else if (device_stream == L"blue_422_channelb")
797 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_b;
798 else if (device_stream == L"blue_422_channelc")
799 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_c;
800 else if (device_stream == L"blue_422_channeld")
801 device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_d;
803 hardware_downstream_keyer_mode keyer_mode = hardware_downstream_keyer_mode::disable;
804 if (hardware_keyer_value == L"disabled")
806 keyer_mode = hardware_downstream_keyer_mode::disable;
808 else if (hardware_keyer_value == L"enabled")
810 keyer_mode = hardware_downstream_keyer_mode::enable;
813 hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
814 if (keyer_audio_source_value == L"videooutputchannel")
815 keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
817 if (keyer_audio_source_value == L"sdivideoinput")
818 keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
820 return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer_mode, keyer_audio_source, layout, device_output_channel);