]> git.sesse.net Git - casparcg/blobdiff - modules/bluefish/consumer/bluefish_consumer.cpp
gardening and improving code to be more inline with CasparCG style
[casparcg] / modules / bluefish / consumer / bluefish_consumer.cpp
index 17d2f2db8160d85df2e8c79ff8963f6be1cbb592..74096f2789ead94e2aaee7ab95f09853f7d0018d 100644 (file)
 #include <array>
 
 namespace caspar { namespace bluefish { 
-                       
+               
+enum class hardware_downstream_keyer_mode
+{
+       disable = 0,
+       enable = 1,
+};
+
+enum class hardware_downstream_keyer_audio_source
+{
+       SDIVideoInput = 1,
+       VideoOutputChannel = 2
+};
+
+enum class bluefish_hardware_output_channel
+{
+       // ** current Bluefish hardware only support 4224  on channels A and C.
+       hardware_4224_channel_a,
+       hardware_4224_channel_c,
+       hardware_422_channel_a,
+       hardware_422_channel_b,
+       hardware_422_channel_c,
+       hardware_422_channel_d,
+       default_output_channel = hardware_422_channel_a
+};
+
+EBlueVideoChannel get_bluesdk_videochannel_from_streamid(bluefish_hardware_output_channel streamid)
+{
+       /*This function would return the corresponding EBlueVideoChannel from the device output channel*/
+       switch (streamid)
+       {
+               case bluefish_hardware_output_channel::hardware_422_channel_a:  return BLUE_VIDEO_OUTPUT_CHANNEL_A;
+               case bluefish_hardware_output_channel::hardware_4224_channel_a: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
+               case bluefish_hardware_output_channel::hardware_422_channel_b:  return BLUE_VIDEO_OUTPUT_CHANNEL_B;
+               case bluefish_hardware_output_channel::hardware_4224_channel_c: return BLUE_VIDEO_OUTPUT_CHANNEL_C;
+               case bluefish_hardware_output_channel::hardware_422_channel_c:  return BLUE_VIDEO_OUTPUT_CHANNEL_C;
+               case bluefish_hardware_output_channel::hardware_422_channel_d:  return BLUE_VIDEO_OUTPUT_CHANNEL_D;
+               default: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
+       }
+}
+
+bool get_videooutput_channel_routing_info_from_streamid(bluefish_hardware_output_channel streamid,
+       EEpochRoutingElements & channelSrcElement,
+       EEpochRoutingElements & sdioutputDstElement)
+{
+       switch (streamid)
+       {
+       case bluefish_hardware_output_channel::hardware_4224_channel_a: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
+               break;
+       case bluefish_hardware_output_channel::hardware_4224_channel_c: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
+               break;
+       case bluefish_hardware_output_channel::hardware_422_channel_a:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
+               break;
+       case bluefish_hardware_output_channel::hardware_422_channel_b:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHB;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_B;
+               break;
+       case bluefish_hardware_output_channel::hardware_422_channel_c:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
+               break;
+       case bluefish_hardware_output_channel::hardware_422_channel_d:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHD;
+               sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_D;
+               break;
+       default: return false;
+       }
+       return true;
+}
+
 struct bluefish_consumer : boost::noncopyable
 {
-       spl::shared_ptr<CBlueVelvet4>                                           blue_;
+       spl::shared_ptr<bvc_wrapper>                                                    blue_;
        const unsigned int                                                                      device_index_;
        const core::video_format_desc                                           format_desc_;
        const core::audio_channel_layout                                        channel_layout_;
@@ -84,6 +152,9 @@ struct bluefish_consumer : boost::noncopyable
        const bool                                                                                      key_only_;
                
        executor                                                                                        executor_;
+       hardware_downstream_keyer_mode                                          hardware_keyer_;
+       hardware_downstream_keyer_audio_source                          keyer_audio_source_;
+       bluefish_hardware_output_channel                                        device_output_channel_;
 public:
        bluefish_consumer(
                        const core::video_format_desc& format_desc,
@@ -92,18 +163,24 @@ public:
                        int device_index,
                        bool embedded_audio,
                        bool key_only,
-                       int channel_index)
+                       hardware_downstream_keyer_mode keyer,
+                       hardware_downstream_keyer_audio_source keyer_audio_source,
+                       int channel_index,
+                       bluefish_hardware_output_channel device_output_channel)
                : blue_(create_blue(device_index))
                , device_index_(device_index)
                , format_desc_(format_desc)
                , channel_layout_(out_channel_layout)
                , channel_remapper_(in_channel_layout, out_channel_layout)
                , channel_index_(channel_index)
-               , model_name_(get_card_desc(*blue_))
+               , model_name_(get_card_desc(*blue_, device_index))
                , vid_fmt_(get_video_mode(*blue_, format_desc))
                , embedded_audio_(embedded_audio)
+               , hardware_keyer_(keyer)
+               , keyer_audio_source_(keyer_audio_source)
                , key_only_(key_only)
                , executor_(print())
+               , device_output_channel_(device_output_channel)
        {
                executor_.set_capacity(1);
                presentation_delay_millis_ = 0;
@@ -113,42 +190,36 @@ public:
                graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
                graph_->set_text(print());
                diagnostics::register_graph(graph_);
-                       
-               //Setting output Video mode
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_MODE, vid_fmt_))) 
+
+               // Specify the video channel
+               setup_hardware_output_channel();
+
+               // Setting output Video mode
+               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_MODE, vid_fmt_)))
                        CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set videomode."));
 
-               //Select Update Mode for output
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_UPDATE_TYPE, UPD_FMT_FRAME))) 
+               // Select Update Mode for output
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_UPDATE_TYPE, UPD_FMT_FRAME)))
                        CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
        
                disable_video_output();
-
-               //Enable dual link output
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT, 1)))
-                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable dual link."));
-
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
-                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
+               setup_hardware_output_channel_routing();
                        
                //Select output memory format
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC))) 
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC)))
                        CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
                
                //Select image orientation
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
                        CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";       
 
                // Select data range
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_RGB_DATA_RANGE, CGR_RANGE))) 
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_RGB_DATA_RANGE, CGR_RANGE)))
                        CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";     
                
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_PREDEFINED_COLOR_MATRIX, vid_fmt_ == VID_FMT_PAL ? MATRIX_601_CGR : MATRIX_709_CGR)))
-                       CASPAR_LOG(warning) << print() << L" Failed to set colormatrix to " << (vid_fmt_ == VID_FMT_PAL ? L"601 CGR" : L"709 CGR") << L".";
-
-               if(!embedded_audio_)
+               if(!embedded_audio_ || (hardware_keyer_ == hardware_downstream_keyer_mode::enable && keyer_audio_source_ == hardware_downstream_keyer_audio_source::VideoOutputChannel) )
                {
-                       if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, 0))) 
+                       if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
                                CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");                       
                        CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
                }
@@ -166,23 +237,17 @@ public:
                        if (channel_layout_.num_channels > 12)
                                audio_value |= blue_emb_audio_group4_enable;
 
-                       if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, audio_value)))
+                       if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, audio_value)))
                                CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");                    
                        CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
                }
-               
-               if (blue_->has_output_key()) 
-               {
-                       int dummy = TRUE; int v4444 = FALSE; int invert = FALSE; int white = FALSE;
-                       blue_->set_output_key(dummy, v4444, invert, white);
-               }
 
-               if(blue_->GetHDCardType(device_index_) != CRD_HD_INVALID) 
-                       blue_->Set_DownConverterSignalType(vid_fmt_ == VID_FMT_PAL ? SD_SDI : HD_SDI);  
-       
-               if(BLUE_FAIL(set_card_property(blue_, VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE))) 
-                       CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine."); 
-               
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE)))
+                       CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine.");
+
+               if (is_epoch_card((*blue_)))
+                       setup_hardware_downstream_keyer(hardware_keyer_, keyer_audio_source_);
+
                enable_video_output();
                                                
                int n = 0;
@@ -196,7 +261,7 @@ public:
                        executor_.invoke([&]
                        {
                                disable_video_output();
-                               blue_->device_detach();         
+                               blue_->detach();                
                        });
                }
                catch(...)
@@ -204,17 +269,158 @@ public:
                        CASPAR_LOG_CURRENT_EXCEPTION();
                }
        }
-       
+
+       void setup_hardware_output_channel()
+       {
+               // This function would be used to setup the logic video channel in the bluefish hardware
+               EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
+               if (is_epoch_card((*blue_)))
+               {
+                       if (out_vid_channel != BLUE_VIDEOCHANNEL_INVALID)
+                       {
+                               if (BLUE_FAIL(blue_->set_card_property32(DEFAULT_VIDEO_OUTPUT_CHANNEL, out_vid_channel)))
+                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set video stream."));
+                       }
+               }
+       }
+
+       void setup_hardware_output_channel_routing()
+       {
+               //This function would be used to setup the dual link and any other routing that would be required .
+               if (is_epoch_card(*blue_))
+               {
+                       EBlueVideoChannel blueVideoOutputChannel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
+                       EEpochRoutingElements src_element = (EEpochRoutingElements)0;
+                       EEpochRoutingElements dst_element = (EEpochRoutingElements)0;
+                       get_videooutput_channel_routing_info_from_streamid(device_output_channel_, src_element, dst_element);
+                       bool duallink_4224_enabled = false;
+
+                       if (device_output_channel_ == bluefish_hardware_output_channel::hardware_4224_channel_a || device_output_channel_ == bluefish_hardware_output_channel::hardware_4224_channel_c)
+                               duallink_4224_enabled = true;
+
+                       // Enable/Disable dual link output
+                       if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT, duallink_4224_enabled)))
+                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable/disable dual link."));
+
+                       if (!duallink_4224_enabled)
+                       {
+                               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_Independent_422)))
+                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2."));
+
+                               ULONG routingValue = EPOCH_SET_ROUTING(src_element, dst_element, BLUE_CONNECTOR_PROP_SINGLE_LINK);
+                               if (BLUE_FAIL(blue_->set_card_property32(MR2_ROUTING, routingValue)))
+                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to MR 2 routing."));
+
+                               // If single link 422, but on second channel AND on Neutron we need to set Genlock to Aux.
+                               if (is_epoch_neutron_1i2o_card((*blue_)))               
+                               {
+                                       if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_B)
+                                       {
+                                               ULONG genLockSource = BlueGenlockAux;
+                                               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
+                                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
+                                       }
+                               }
+                               if (is_epoch_neutron_3o_card((*blue_)))
+                               {
+                                       if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
+                                       {
+                                               ULONG genLockSource = BlueGenlockAux;
+                                               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
+                                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
+                                       }
+                               }
+                       }
+                       else            // dual Link IS enabled, ie. 4224 Fill and Key
+                       {
+                               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
+                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
+
+                               if (is_epoch_neutron_1i2o_card((*blue_)))               // Neutron cards require setting the Genlock conector to Aux to enable them to do Dual-Link
+                               {
+                                       ULONG genLockSource = BlueGenlockAux;
+                                       if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
+                                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
+                               }
+                               else if (is_epoch_neutron_3o_card((*blue_)))
+                               {
+                                       if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
+                                       {
+                                               ULONG genLockSource = BlueGenlockAux;
+                                               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
+                                                       CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
+                                       }
+                               }
+                       }
+               }
+       }
+
+       void setup_hardware_downstream_keyer(hardware_downstream_keyer_mode keyer, hardware_downstream_keyer_audio_source audio_source)
+       {
+               unsigned int keyer_control_value = 0, card_feature_value = 0;
+               unsigned int card_connector_value = 0;
+               unsigned int nOutputStreams = 0;
+               unsigned int nInputStreams = 0;
+               unsigned int nInputSDIConnector = 0;
+               unsigned int nOutputSDIConnector = 0;
+               if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_STREAM_INFO, card_feature_value)))
+               {
+                       nOutputStreams = CARD_FEATURE_GET_SDI_OUTPUT_STREAM_COUNT(card_feature_value);
+                       nInputStreams = CARD_FEATURE_GET_SDI_INPUT_STREAM_COUNT(card_feature_value);
+               }
+               if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_CONNECTOR_INFO, card_connector_value)))
+               {
+                       nOutputSDIConnector = CARD_FEATURE_GET_SDI_OUTPUT_CONNECTOR_COUNT(card_connector_value);
+                       nInputSDIConnector = CARD_FEATURE_GET_SDI_INPUT_CONNECTOR_COUNT(card_connector_value);
+               }
+               if (nInputSDIConnector == 0 || nInputStreams == 0)
+                       return;
+
+               if (keyer == hardware_downstream_keyer_mode::disable)
+               {
+                       keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLED(keyer_control_value);
+                       keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
+               }
+               else if (keyer == hardware_downstream_keyer_mode::enable)
+               {
+                       unsigned int invalidVideoModeFlag = 0;
+                       unsigned int inputVideoSignal = 0;
+                       if (BLUE_FAIL(blue_->get_card_property32(INVALID_VIDEO_MODE_FLAG, invalidVideoModeFlag)))
+                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get invalid video mode flag"));
+
+                       keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLED(keyer_control_value);
+                       if (BLUE_FAIL(blue_->get_card_property32(VIDEO_INPUT_SIGNAL_VIDEO_MODE, inputVideoSignal)))
+                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get video input signal mode"));
+
+                       if (inputVideoSignal >= invalidVideoModeFlag)
+                               keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLE_OVER_BLACK(keyer_control_value);
+                       else
+                               keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
+               
+                       // lock to input
+                       if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, BlueSDI_A_BNC)))
+                               CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set the genlock to the input for the HW keyer"));
+               }
+
+               if (audio_source == hardware_downstream_keyer_audio_source::SDIVideoInput && (keyer == hardware_downstream_keyer_mode::enable))
+                       keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_INPUT_ANCILLARY(keyer_control_value);
+               else if (audio_source == hardware_downstream_keyer_audio_source::VideoOutputChannel)
+                       keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_OUTPUT_ANCILLARY(keyer_control_value);
+
+               if (BLUE_FAIL(blue_->set_card_property32(VIDEO_ONBOARD_KEYER, keyer_control_value)))
+                       CASPAR_LOG(error) << print() << TEXT(" Failed to set keyer control.");
+       }
+
        void enable_video_output()
        {
-               if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 0)))
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 0)))
                        CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");       
        }
 
        void disable_video_output()
        {
                blue_->video_playback_stop(0,0);
-               if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 1)))
+               if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 1)))
                        CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");                
        }
        
@@ -243,7 +449,7 @@ public:
 
                sync_timer_.restart();
                unsigned long n_field = 0;
-               blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field);
+               blue_->wait_video_output_sync(UPD_FMT_FRAME, n_field);
                graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5);
                
                frame_timer_.restart();         
@@ -277,25 +483,25 @@ public:
                                                static_cast<int>(frame.audio_data().size()/channel_layout_.num_channels), 
                                                static_cast<int>(channel_layout_.num_channels));
                                                                
-                       blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()), 
-                                                                                       static_cast<unsigned long>(reserved_frames_.front()->image_size()), 
-                                                                                       nullptr
-                                                                                       BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
+                       blue_->system_buffer_write(const_cast<uint8_t*>(reserved_frames_.front()->image_data()), 
+                                                                                       static_cast<unsigned long>(reserved_frames_.front()->image_size()),  
+                                                                                       BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE)
+                                                                                       0);
 
-                       blue_->system_buffer_write_async(reserved_frames_.front()->hanc_data(),
-                                                                                       static_cast<unsigned long>(reserved_frames_.front()->hanc_size()), 
-                                                                                       nullptr,                 
-                                                                                       BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC));
+                       blue_->system_buffer_write(reserved_frames_.front()->hanc_data(),
+                                                                                       static_cast<unsigned long>(reserved_frames_.front()->hanc_size()),
+                                                                                       BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC),
+                                                                                       0);
 
                        if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id()))))
                                CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
                }
                else
                {
-                       blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
+                       blue_->system_buffer_write(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
                                                                                        static_cast<unsigned long>(reserved_frames_.front()->image_size()), 
-                                                                                       nullptr,                 
-                                                                                       BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
+                                                                                       BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE),
+                                                                                       0);
                        
                        if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id()))))
                                CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
@@ -330,10 +536,9 @@ public:
                hanc_stream_info.hanc_data_ptr    = hanc_data;
                hanc_stream_info.video_mode               = vid_fmt_;           
                
-               if (!is_epoch_card(*blue_))
-                       encode_hanc_frame(&hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);  
-               else
-                       encode_hanc_frame_ex(blue_->has_video_cardtype(), &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
+               int cardType = CRD_INVALID;
+               blue_->query_card_type(cardType, device_index_);
+               blue_->encode_hanc_frame(cardType, &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
        }
        
        std::wstring print() const
@@ -350,25 +555,38 @@ public:
 
 struct bluefish_consumer_proxy : public core::frame_consumer
 {
-       core::monitor::subject                          monitor_subject_;
+       core::monitor::subject                                  monitor_subject_;
 
-       std::unique_ptr<bluefish_consumer>      consumer_;
-       const int                                                       device_index_;
-       const bool                                                      embedded_audio_;
-       const bool                                                      key_only_;
+       std::unique_ptr<bluefish_consumer>              consumer_;
+       const int                                                               device_index_;
+       const bool                                                              embedded_audio_;
+       const bool                                                              key_only_;
 
-       std::vector<int>                                        audio_cadence_;
-       core::video_format_desc                         format_desc_;
-       core::audio_channel_layout                      in_channel_layout_              = core::audio_channel_layout::invalid();
-       core::audio_channel_layout                      out_channel_layout_;
+       std::vector<int>                                                audio_cadence_;
+       core::video_format_desc                                 format_desc_;
+       core::audio_channel_layout                              in_channel_layout_              = core::audio_channel_layout::invalid();
+       core::audio_channel_layout                              out_channel_layout_;
+       hardware_downstream_keyer_mode                  hardware_keyer_;
+       hardware_downstream_keyer_audio_source  hardware_keyer_audio_source_;
+       bluefish_hardware_output_channel                hardware_output_channel_;
 
 public:
 
-       bluefish_consumer_proxy(int device_index, bool embedded_audio, bool key_only, const core::audio_channel_layout& out_channel_layout)
+       bluefish_consumer_proxy(int device_index, 
+                                                       bool embedded_audio, 
+                                                       bool key_only, 
+                                                       hardware_downstream_keyer_mode keyer,
+                                                       hardware_downstream_keyer_audio_source keyer_audio_source,
+                                                       const core::audio_channel_layout& out_channel_layout,
+                                                       bluefish_hardware_output_channel hardware_output_channel)
+
                : device_index_(device_index)
                , embedded_audio_(embedded_audio)
                , key_only_(key_only)
+               , hardware_keyer_(keyer)
+               , hardware_keyer_audio_source_(keyer_audio_source)
                , out_channel_layout_(out_channel_layout)
+               , hardware_output_channel_(hardware_output_channel)
        {
        }
        
@@ -384,7 +602,16 @@ public:
                        out_channel_layout_ = in_channel_layout_;
 
                consumer_.reset();
-               consumer_.reset(new bluefish_consumer(format_desc, in_channel_layout_, out_channel_layout_, device_index_, embedded_audio_, key_only_, channel_index));
+               consumer_.reset(new bluefish_consumer(  format_desc, 
+                                                                                               in_channel_layout_, 
+                                                                                               out_channel_layout_, 
+                                                                                               device_index_, 
+                                                                                               embedded_audio_, 
+                                                                                               key_only_, 
+                                                                                               hardware_keyer_,
+                                                                                               hardware_keyer_audio_source_, 
+                                                                                               channel_index,
+                                                                                               hardware_output_channel_));
        }
        
        std::future<bool> send(core::const_frame frame) override
@@ -451,16 +678,34 @@ void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
                ->text(L"channel. This is useful when you have two SDI video cards, and neither has native support ")
                ->text(L"for separate fill/key output");
        sink.para()->text(L"Specify ")->code(L"channel_layout")->text(L" to output a different audio channel layout than the channel uses.");
+       sink.para()->text(L"Specify ")->code(L"BF_4224_CHANNELA")->text(L" to use channel A on the card to  be configured to output "
+               L"4:2:2:4 with fill and key HD-SDI output's A and B on the card.\n"
+               L"This is the default option\n");
+       sink.para()->text(L"Specify ")->code(L"BF_4224_CHANNELC")->text(L" to use channel C on the card to  be configured to output "
+               L" 4:2:2:4 with fill and key  HD-SDI output's C and D on the card\n"
+               L" Channel C 4:2:2:4 support is only available on 4 output variant of Supernova  and SupernovaS+ card\n");
+
+       sink.para()->text(L"Specify ")->code(L"ENABLE_KEYER")->text(L" to enable use of hardware keyer on the bluefish board.\n"
+               L"\t\t\tUsing this option hardware keyer would key using \n "
+               L"\t\t\tthe video input on the SDI as background layer \n"
+               L"\t\t\tand graphics Generated by CasparCG as foreground layer");
+       sink.para()->text(L"Specify ")->code(L"DISABLE_KEYER")->text(L" this option to disable hardware keyer on the bluefish board");
+       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");
+       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");
+
        sink.para()->text(L"Examples:");
        sink.example(L">> ADD 1 BLUEFISH", L"uses the default device_index of 1.");
        sink.example(L">> ADD 1 BLUEFISH 2", L"for device_index 2.");
        sink.example(
                L">> ADD 1 BLUEFISH 1 EMBEDDED_AUDIO\n"
                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.");
+
 }
 
-spl::shared_ptr<core::frame_consumer> create_consumer(
-               const std::vector<std::wstring>& params, core::interaction_sink*)
+
+spl::shared_ptr<core::frame_consumer> create_consumer( const std::vector<std::wstring>& params,
+                                                                                                               core::interaction_sink*,
+                                                                                                               std::vector<spl::shared_ptr<core::video_channel>> channels)
 {
        if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
                return core::frame_consumer::empty();
@@ -470,6 +715,7 @@ spl::shared_ptr<core::frame_consumer> create_consumer(
        const auto embedded_audio       = contains_param(       L"EMBEDDED_AUDIO",      params);
        const auto key_only                     = contains_param(       L"KEY_ONLY",            params);
        const auto channel_layout       = get_param(            L"CHANNEL_LAYOUT",      params);
+       const auto device_stream        = contains_param(       L"SDI-STREAM",          params);
 
        auto layout = core::audio_channel_layout::invalid();
 
@@ -482,22 +728,56 @@ spl::shared_ptr<core::frame_consumer> create_consumer(
 
                layout = *found_layout;
        }
-
-       return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, layout);
+       bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
+
+       if (contains_param(L"BLUE_4224_CHANNELA", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
+       else if (contains_param(L"BLUE_4224_CHANNELC", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_c;
+       else if (contains_param(L"BLUE_422_CHANNELA", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_a;
+       else if (contains_param(L"BLUE_422_CHANNELB", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_b;
+       else if (contains_param(L"BLUE_422_CHANNELC", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_c;
+       else if (contains_param(L"BLUE_422_CHANNELD", params))
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_d;
+
+       hardware_downstream_keyer_mode keyer = hardware_downstream_keyer_mode::disable;
+
+       if (contains_param(L"KEYER_DISABLED", params))
+               keyer = hardware_downstream_keyer_mode::disable;
+       else if (contains_param(L"KEYER_ENABLED", params))
+               keyer = hardware_downstream_keyer_mode::enable;
+
+       hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
+       if (contains_param(L"KEYER_AUDIO_SOURCE_SDIINPUT", params))
+               keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
+       else
+       if (contains_param(L"KEYER_AUDIO_SOURCE_OUTPUT_CHANNEL", params))
+               keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
+
+       return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer, keyer_audio_source, layout, device_output_channel);
 }
 
 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
-               const boost::property_tree::wptree& ptree, core::interaction_sink*)
+                                                                                       const boost::property_tree::wptree& ptree, core::interaction_sink*,
+                                                                                       std::vector<spl::shared_ptr<core::video_channel>> channels)
 {      
        const auto device_index         = ptree.get(                                            L"device",                      1);
        const auto embedded_audio       = ptree.get(                                            L"embedded-audio",      false);
        const auto key_only                     = ptree.get(                                            L"key-only",            false);
        const auto channel_layout       = ptree.get_optional<std::wstring>(     L"channel-layout");
+       const auto hardware_keyer_value = ptree.get(L"hardware-keyer", L"disabled");
+       const auto keyer_audio_source_value = ptree.get(L"hardware-keyer-audio-source", L"videooutputchannel");
+       const auto device_stream = ptree.get(L"sdi-stream", L"blue_4224_channela");
 
        auto layout = core::audio_channel_layout::invalid();
 
        if (channel_layout)
        {
+               CASPAR_SCOPED_CONTEXT_MSG("/channel-layout")
+
                auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(*channel_layout);
 
                if (!found_layout)
@@ -506,7 +786,38 @@ spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
                layout = *found_layout;
        }
 
-       return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, layout);
+       bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
+       if (device_stream == L"blue_4224_channela")
+               device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
+       else if (device_stream == L"blue_4224_channelc")
+               device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_c;
+       else if (device_stream == L"blue_422_channela")
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_a;
+       else if (device_stream == L"blue_422_channelb")
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_b;
+       else if (device_stream == L"blue_422_channelc")
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_c;
+       else if (device_stream == L"blue_422_channeld")
+               device_output_channel = bluefish_hardware_output_channel::hardware_422_channel_d;
+
+       hardware_downstream_keyer_mode keyer_mode = hardware_downstream_keyer_mode::disable;
+       if (hardware_keyer_value == L"disabled")
+       {
+               keyer_mode = hardware_downstream_keyer_mode::disable;
+       }
+       else if (hardware_keyer_value == L"enabled")
+       {
+               keyer_mode = hardware_downstream_keyer_mode::enable;
+       }
+
+       hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
+       if (keyer_audio_source_value == L"videooutputchannel")
+               keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
+       else
+               if (keyer_audio_source_value == L"sdivideoinput")
+                       keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
+
+       return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer_mode, keyer_audio_source, layout, device_output_channel);
 }
 
 }}
\ No newline at end of file