]> git.sesse.net Git - casparcg/blob - modules/bluefish/consumer/bluefish_consumer.cpp
gardening and improving code to be more inline with CasparCG style
[casparcg] / modules / bluefish / consumer / bluefish_consumer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21  
22 #include "../StdAfx.h"
23
24 #include "bluefish_consumer.h"
25 #include "../util/blue_velvet.h"
26 #include "../util/memory.h"
27
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>
33
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>
39
40 #include <core/consumer/frame_consumer.h>
41 #include <core/mixer/audio/audio_util.h>
42
43 #include <tbb/concurrent_queue.h>
44 #include <tbb/atomic.h>
45
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>
52
53 #include <asmlib.h>
54
55 #include <memory>
56 #include <array>
57
58 namespace caspar { namespace bluefish { 
59                 
60 enum class hardware_downstream_keyer_mode
61 {
62         disable = 0,
63         enable = 1,
64 };
65
66 enum class hardware_downstream_keyer_audio_source
67 {
68         SDIVideoInput = 1,
69         VideoOutputChannel = 2
70 };
71
72 enum class bluefish_hardware_output_channel
73 {
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
82 };
83
84 EBlueVideoChannel get_bluesdk_videochannel_from_streamid(bluefish_hardware_output_channel streamid)
85 {
86         /*This function would return the corresponding EBlueVideoChannel from the device output channel*/
87         switch (streamid)
88         {
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;
96         }
97 }
98
99 bool get_videooutput_channel_routing_info_from_streamid(bluefish_hardware_output_channel streamid,
100         EEpochRoutingElements & channelSrcElement,
101         EEpochRoutingElements & sdioutputDstElement)
102 {
103         switch (streamid)
104         {
105         case bluefish_hardware_output_channel::hardware_4224_channel_a: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
106                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
107                 break;
108         case bluefish_hardware_output_channel::hardware_4224_channel_c: channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
109                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
110                 break;
111         case bluefish_hardware_output_channel::hardware_422_channel_a:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
112                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
113                 break;
114         case bluefish_hardware_output_channel::hardware_422_channel_b:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHB;
115                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_B;
116                 break;
117         case bluefish_hardware_output_channel::hardware_422_channel_c:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
118                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
119                 break;
120         case bluefish_hardware_output_channel::hardware_422_channel_d:  channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHD;
121                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_D;
122                 break;
123         default: return false;
124         }
125         return true;
126 }
127
128 struct bluefish_consumer : boost::noncopyable
129 {
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_;
136
137         const std::wstring                                                                      model_name_;
138
139         spl::shared_ptr<diagnostics::graph>                                     graph_;
140         boost::timer                                                                            frame_timer_;
141         boost::timer                                                                            tick_timer_;
142         boost::timer                                                                            sync_timer_;    
143                         
144         unsigned int                                                                            vid_fmt_;
145
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();
150
151         const bool                                                                                      embedded_audio_;
152         const bool                                                                                      key_only_;
153                 
154         executor                                                                                        executor_;
155         hardware_downstream_keyer_mode                                          hardware_keyer_;
156         hardware_downstream_keyer_audio_source                          keyer_audio_source_;
157         bluefish_hardware_output_channel                                        device_output_channel_;
158 public:
159         bluefish_consumer(
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,
163                         int device_index,
164                         bool embedded_audio,
165                         bool key_only,
166                         hardware_downstream_keyer_mode keyer,
167                         hardware_downstream_keyer_audio_source keyer_audio_source,
168                         int channel_index,
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)
182                 , executor_(print())
183                 , device_output_channel_(device_output_channel)
184         {
185                 executor_.set_capacity(1);
186                 presentation_delay_millis_ = 0;
187
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_);
193
194                 // Specify the video channel
195                 setup_hardware_output_channel();
196
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."));
200
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."));
204         
205                 disable_video_output();
206                 setup_hardware_output_channel_routing();
207                         
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."));
211                 
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.";       
215
216                 // Select data range
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.";     
219                 
220                 if(!embedded_audio_ || (hardware_keyer_ == hardware_downstream_keyer_mode::enable && keyer_audio_source_ == hardware_downstream_keyer_audio_source::VideoOutputChannel) )
221                 {
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.");
225                 }
226                 else
227                 {
228                         ULONG audio_value =
229                                 EMBEDDED_AUDIO_OUTPUT | blue_emb_audio_group1_enable;
230
231                         if (channel_layout_.num_channels > 4)
232                                 audio_value |= blue_emb_audio_group2_enable;
233
234                         if (channel_layout_.num_channels > 8)
235                                 audio_value |= blue_emb_audio_group3_enable;
236
237                         if (channel_layout_.num_channels > 12)
238                                 audio_value |= blue_emb_audio_group4_enable;
239
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.");
243                 }
244
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.");
247
248                 if (is_epoch_card((*blue_)))
249                         setup_hardware_downstream_keyer(hardware_keyer_, keyer_audio_source_);
250
251                 enable_video_output();
252                                                 
253                 int n = 0;
254                 boost::range::generate(reserved_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
255         }
256
257         ~bluefish_consumer()
258         {
259                 try
260                 {
261                         executor_.invoke([&]
262                         {
263                                 disable_video_output();
264                                 blue_->detach();                
265                         });
266                 }
267                 catch(...)
268                 {
269                         CASPAR_LOG_CURRENT_EXCEPTION();
270                 }
271         }
272
273         void setup_hardware_output_channel()
274         {
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_)))
278                 {
279                         if (out_vid_channel != BLUE_VIDEOCHANNEL_INVALID)
280                         {
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."));
283                         }
284                 }
285         }
286
287         void setup_hardware_output_channel_routing()
288         {
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_))
291                 {
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;
297
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;
300
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."));
304
305                         if (!duallink_4224_enabled)
306                         {
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."));
309
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."));
313
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_)))               
316                                 {
317                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_B)
318                                         {
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."));
322                                         }
323                                 }
324                                 if (is_epoch_neutron_3o_card((*blue_)))
325                                 {
326                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
327                                         {
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."));
331                                         }
332                                 }
333                         }
334                         else            // dual Link IS enabled, ie. 4224 Fill and Key
335                         {
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."));
338
339                                 if (is_epoch_neutron_1i2o_card((*blue_)))               // Neutron cards require setting the Genlock conector to Aux to enable them to do Dual-Link
340                                 {
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."));
344                                 }
345                                 else if (is_epoch_neutron_3o_card((*blue_)))
346                                 {
347                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
348                                         {
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."));
352                                         }
353                                 }
354                         }
355                 }
356         }
357
358         void setup_hardware_downstream_keyer(hardware_downstream_keyer_mode keyer, hardware_downstream_keyer_audio_source audio_source)
359         {
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)))
367                 {
368                         nOutputStreams = CARD_FEATURE_GET_SDI_OUTPUT_STREAM_COUNT(card_feature_value);
369                         nInputStreams = CARD_FEATURE_GET_SDI_INPUT_STREAM_COUNT(card_feature_value);
370                 }
371                 if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_CONNECTOR_INFO, card_connector_value)))
372                 {
373                         nOutputSDIConnector = CARD_FEATURE_GET_SDI_OUTPUT_CONNECTOR_COUNT(card_connector_value);
374                         nInputSDIConnector = CARD_FEATURE_GET_SDI_INPUT_CONNECTOR_COUNT(card_connector_value);
375                 }
376                 if (nInputSDIConnector == 0 || nInputStreams == 0)
377                         return;
378
379                 if (keyer == hardware_downstream_keyer_mode::disable)
380                 {
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);
383                 }
384                 else if (keyer == hardware_downstream_keyer_mode::enable)
385                 {
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"));
390
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"));
394
395                         if (inputVideoSignal >= invalidVideoModeFlag)
396                                 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLE_OVER_BLACK(keyer_control_value);
397                         else
398                                 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
399                 
400                         // lock to input
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"));
403                 }
404
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);
409
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.");
412         }
413
414         void enable_video_output()
415         {
416                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 0)))
417                         CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");       
418         }
419
420         void disable_video_output()
421         {
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.");                
425         }
426         
427         std::future<bool> send(core::const_frame& frame)
428         {                                       
429                 return executor_.begin_invoke([=]() -> bool
430                 {
431                         try
432                         {       
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();
436                         }
437                         catch(...)
438                         {
439                                 CASPAR_LOG_CURRENT_EXCEPTION();
440                         }
441
442                         return true;
443                 });
444         }
445
446         void display_frame(core::const_frame frame)
447         {
448                 // Sync
449
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);
454                 
455                 frame_timer_.restart();         
456
457                 if (previous_frame_ != core::const_frame::empty())
458                         presentation_delay_millis_ = previous_frame_.get_age_millis();
459
460                 previous_frame_ = frame;
461
462                 // Copy to local buffers
463                 
464                 if(!frame.image_data().empty())
465                 {
466                         if(key_only_)                                           
467                                 aligned_memshfl(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
468                         else
469                                 A_memcpy(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size());
470                 }
471                 else
472                         A_memset(reserved_frames_.front()->image_data(), 0, reserved_frames_.front()->image_size());
473                                                                 
474
475                 // Send and display
476
477                 if(embedded_audio_)
478                 {
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()), 
482                                                 frame_audio.data(), 
483                                                 static_cast<int>(frame.audio_data().size()/channel_layout_.num_channels), 
484                                                 static_cast<int>(channel_layout_.num_channels));
485                                                                 
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), 
489                                                                                         0);
490
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),
494                                                                                         0);
495
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.");
498                 }
499                 else
500                 {
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),
504                                                                                         0);
505                         
506                         if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id()))))
507                                 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
508                 }
509
510                 boost::range::rotate(reserved_frames_, std::begin(reserved_frames_)+1);
511                 
512                 graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
513         }
514
515         void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
516         {       
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;
519
520                 if (audio_nchannels > 4)
521                         emb_audio_flag |= blue_emb_audio_group2_enable;
522
523                 if (audio_nchannels > 8)
524                         emb_audio_flag |= blue_emb_audio_group3_enable;
525
526                 if (audio_nchannels > 12)
527                         emb_audio_flag |= blue_emb_audio_group4_enable;
528                 
529                 hanc_stream_info_struct hanc_stream_info;
530                 memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
531                 
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_;           
538                 
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);
542         }
543         
544         std::wstring print() const
545         {
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"]";
548         }
549
550         int64_t presentation_delay_millis() const
551         {
552                 return presentation_delay_millis_;
553         }
554 };
555
556 struct bluefish_consumer_proxy : public core::frame_consumer
557 {
558         core::monitor::subject                                  monitor_subject_;
559
560         std::unique_ptr<bluefish_consumer>              consumer_;
561         const int                                                               device_index_;
562         const bool                                                              embedded_audio_;
563         const bool                                                              key_only_;
564
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_;
572
573 public:
574
575         bluefish_consumer_proxy(int device_index, 
576                                                         bool embedded_audio, 
577                                                         bool key_only, 
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)
582
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)
590         {
591         }
592         
593         // frame_consumer
594         
595         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) override
596         {
597                 format_desc_            = format_desc;
598                 in_channel_layout_      = channel_layout;
599                 audio_cadence_          = format_desc.audio_cadence;
600
601                 if (out_channel_layout_ == core::audio_channel_layout::invalid())
602                         out_channel_layout_ = in_channel_layout_;
603
604                 consumer_.reset();
605                 consumer_.reset(new bluefish_consumer(  format_desc, 
606                                                                                                 in_channel_layout_, 
607                                                                                                 out_channel_layout_, 
608                                                                                                 device_index_, 
609                                                                                                 embedded_audio_, 
610                                                                                                 key_only_, 
611                                                                                                 hardware_keyer_,
612                                                                                                 hardware_keyer_audio_source_, 
613                                                                                                 channel_index,
614                                                                                                 hardware_output_channel_));
615         }
616         
617         std::future<bool> send(core::const_frame frame) override
618         {
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);
622         }
623                 
624         std::wstring print() const override
625         {
626                 return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
627         }
628
629         std::wstring name() const override
630         {
631                 return L"bluefish";
632         }
633
634         boost::property_tree::wptree info() const override
635         {
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());
642                 return info;
643         }
644
645         int buffer_depth() const override
646         {
647                 return 1;
648         }
649         
650         int index() const override
651         {
652                 return 400 + device_index_;
653         }
654
655         int64_t presentation_frame_age_millis() const override
656         {
657                 return consumer_ ? consumer_->presentation_delay_millis() : 0;
658         }
659
660         core::monitor::subject& monitor_output()
661         {
662                 return monitor_subject_;
663         }
664 };      
665
666
667 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
668 {
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]}");
671         sink.para()
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.");
676         sink.para()
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");
687
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");
695
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.");
699         sink.example(
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.");
702
703 }
704
705
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)
709 {
710         if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
711                 return core::frame_consumer::empty();
712
713         const auto device_index = params.size() > 1 ? boost::lexical_cast<int>(params.at(1)) : 1;
714
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);
719
720         auto layout = core::audio_channel_layout::invalid();
721
722         if (!channel_layout.empty())
723         {
724                 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout);
725
726                 if (!found_layout)
727                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + channel_layout + L" not found"));
728
729                 layout = *found_layout;
730         }
731         bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::hardware_4224_channel_a;
732
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;
745
746         hardware_downstream_keyer_mode keyer = hardware_downstream_keyer_mode::disable;
747
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;
752
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;
756         else
757         if (contains_param(L"KEYER_AUDIO_SOURCE_OUTPUT_CHANNEL", params))
758                 keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
759
760         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer, keyer_audio_source, layout, device_output_channel);
761 }
762
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)
766 {       
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");
774
775         auto layout = core::audio_channel_layout::invalid();
776
777         if (channel_layout)
778         {
779                 CASPAR_SCOPED_CONTEXT_MSG("/channel-layout")
780
781                 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(*channel_layout);
782
783                 if (!found_layout)
784                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + *channel_layout + L" not found"));
785
786                 layout = *found_layout;
787         }
788
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;
802
803         hardware_downstream_keyer_mode keyer_mode = hardware_downstream_keyer_mode::disable;
804         if (hardware_keyer_value == L"disabled")
805         {
806                 keyer_mode = hardware_downstream_keyer_mode::disable;
807         }
808         else if (hardware_keyer_value == L"enabled")
809         {
810                 keyer_mode = hardware_downstream_keyer_mode::enable;
811         }
812
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;
816         else
817                 if (keyer_audio_source_value == L"sdivideoinput")
818                         keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
819
820         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer_mode, keyer_audio_source, layout, device_output_channel);
821 }
822
823 }}