]> git.sesse.net Git - casparcg/blob - modules/bluefish/consumer/bluefish_consumer.cpp
Modify behaviour of 2nd link upon exit, retunring to a more normal configuration.
[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 #define BLUEFISH_HW_BUFFER_DEPTH 1
61 #define BLUEFISH_SOFTWARE_BUFFERS 4
62                 
63 enum class hardware_downstream_keyer_mode
64 {
65         disable = 0,
66         external = 1,
67         internal = 2,           // Bluefish dedicated HW keyer - only available on some models.
68 };
69
70 enum class hardware_downstream_keyer_audio_source
71 {
72         SDIVideoInput = 1,
73         VideoOutputChannel = 2
74 };
75
76 enum class bluefish_hardware_output_channel
77 {
78         channel_a,
79         channel_b,
80         channel_c,
81         channel_d,
82         default_output_channel = channel_a
83 };
84
85 EBlueVideoChannel get_bluesdk_videochannel_from_streamid(bluefish_hardware_output_channel streamid)
86 {
87         /*This function would return the corresponding EBlueVideoChannel from the device output channel*/
88         switch (streamid)
89         {
90                 case bluefish_hardware_output_channel::channel_a:       return BLUE_VIDEO_OUTPUT_CHANNEL_A;
91                 case bluefish_hardware_output_channel::channel_b:       return BLUE_VIDEO_OUTPUT_CHANNEL_B;
92                 case bluefish_hardware_output_channel::channel_c:       return BLUE_VIDEO_OUTPUT_CHANNEL_C;
93                 case bluefish_hardware_output_channel::channel_d:       return BLUE_VIDEO_OUTPUT_CHANNEL_D;
94                 default: return BLUE_VIDEO_OUTPUT_CHANNEL_A;
95         }
96 }
97
98 bool get_videooutput_channel_routing_info_from_streamid(bluefish_hardware_output_channel streamid,
99         EEpochRoutingElements & channelSrcElement,
100         EEpochRoutingElements & sdioutputDstElement)
101 {
102         switch (streamid)
103         {
104         case bluefish_hardware_output_channel::channel_a:       channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA;
105                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_A;
106                 break;
107         case bluefish_hardware_output_channel::channel_b:       channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHB;
108                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_B;
109                 break;
110         case bluefish_hardware_output_channel::channel_c:       channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHC;
111                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_C;
112                 break;
113         case bluefish_hardware_output_channel::channel_d:       channelSrcElement = EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHD;
114                 sdioutputDstElement = EPOCH_DEST_SDI_OUTPUT_D;
115                 break;
116         default: return false;
117         }
118         return true;
119 }
120
121 struct bluefish_consumer : boost::noncopyable
122 {
123         spl::shared_ptr<bvc_wrapper>                                                            blue_;
124         const unsigned int                                                                                      device_index_;
125         const core::video_format_desc                                                           format_desc_;
126         const core::audio_channel_layout                                                        channel_layout_;
127         core::audio_channel_remapper                                                            channel_remapper_;
128         const int                                                                                                       channel_index_;
129
130         const std::wstring                                                                                      model_name_;
131
132         spl::shared_ptr<diagnostics::graph>                                                     graph_;
133         boost::timer                                                                                            frame_timer_;
134         boost::timer                                                                                            tick_timer_;
135         boost::timer                                                                                            sync_timer_;    
136                         
137         unsigned int                                                                                            vid_fmt_;
138
139         std::array<blue_dma_buffer_ptr, BLUEFISH_SOFTWARE_BUFFERS>      all_frames_;    
140         tbb::concurrent_bounded_queue<blue_dma_buffer_ptr>                      reserved_frames_;
141         tbb::concurrent_bounded_queue<blue_dma_buffer_ptr>                      live_frames_;
142         std::shared_ptr<std::thread>                                                            dma_present_thread_;
143         tbb::atomic<bool>                                                                                       end_dma_thread_;
144
145         tbb::concurrent_bounded_queue<core::const_frame>                        frame_buffer_;
146         tbb::atomic<int64_t>                                                                            presentation_delay_millis_;
147         core::const_frame                                                                                       previous_frame_                         = core::const_frame::empty();
148
149         const bool                                                                                                      embedded_audio_;
150         const bool                                                                                                      key_only_;
151                 
152         executor                                                                                                        executor_;
153         hardware_downstream_keyer_mode                                                          hardware_keyer_;
154         hardware_downstream_keyer_audio_source                                          keyer_audio_source_;
155         bluefish_hardware_output_channel                                                        device_output_channel_;
156 public:
157         bluefish_consumer(
158                         const core::video_format_desc& format_desc,
159                         const core::audio_channel_layout& in_channel_layout,
160                         const core::audio_channel_layout& out_channel_layout,
161                         int device_index,
162                         bool embedded_audio,
163                         bool key_only,
164                         hardware_downstream_keyer_mode keyer,
165                         hardware_downstream_keyer_audio_source keyer_audio_source,
166                         int channel_index,
167                         bluefish_hardware_output_channel device_output_channel)
168                 : blue_(create_blue(device_index))
169                 , device_index_(device_index)
170                 , format_desc_(format_desc)
171                 , channel_layout_(out_channel_layout)
172                 , channel_remapper_(in_channel_layout, out_channel_layout)
173                 , channel_index_(channel_index)
174                 , model_name_(get_card_desc(*blue_, device_index))
175                 , vid_fmt_(get_video_mode(*blue_, format_desc))
176                 , embedded_audio_(embedded_audio)
177                 , hardware_keyer_(keyer)
178                 , keyer_audio_source_(keyer_audio_source)
179                 , key_only_(key_only)
180                 , executor_(print())
181                 , device_output_channel_(device_output_channel)
182         {
183                 executor_.set_capacity(1);
184                 presentation_delay_millis_ = 0;
185
186                 reserved_frames_.set_capacity(BLUEFISH_SOFTWARE_BUFFERS);
187                 live_frames_.set_capacity(BLUEFISH_SOFTWARE_BUFFERS);
188                 
189                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   
190                 graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f));
191                 graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
192                 graph_->set_text(print());
193                 diagnostics::register_graph(graph_);
194
195                 // Specify the video channel
196                 setup_hardware_output_channel();
197
198                 // Setting output Video mode
199                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_MODE, vid_fmt_)))
200                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set videomode."));
201
202                 // Select Update Mode for output
203                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_UPDATE_TYPE, UPD_FMT_FRAME)))
204                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
205         
206                 disable_video_output();
207                 setup_hardware_output_channel_routing();
208                         
209                 //Select output memory format
210                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC)))
211                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
212                 
213                 //Select image orientation
214                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
215                         CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";       
216
217                 // Select data range
218                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_RGB_DATA_RANGE, CGR_RANGE)))
219                         CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";     
220                 
221                 if(!embedded_audio_ || (hardware_keyer_ == hardware_downstream_keyer_mode::internal && keyer_audio_source_ == hardware_downstream_keyer_audio_source::SDIVideoInput) )
222                 {
223                         if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
224                                 CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");                       
225                         CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
226                 }
227                 else
228                 {
229                         ULONG audio_value =
230                                 EMBEDDED_AUDIO_OUTPUT | blue_emb_audio_group1_enable;
231
232                         if (channel_layout_.num_channels > 4)
233                                 audio_value |= blue_emb_audio_group2_enable;
234
235                         if (channel_layout_.num_channels > 8)
236                                 audio_value |= blue_emb_audio_group3_enable;
237
238                         if (channel_layout_.num_channels > 12)
239                                 audio_value |= blue_emb_audio_group4_enable;
240
241                         if(BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, audio_value)))
242                                 CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");                    
243                         CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
244                 }
245
246                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_PLAYBACK)))
247                         CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine.");
248
249                 if (is_epoch_card((*blue_)))
250                         setup_hardware_downstream_keyer(hardware_keyer_, keyer_audio_source_);
251
252                 enable_video_output();
253                                                 
254                 int n = 0;
255                 boost::range::generate(all_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
256         
257                 for (size_t i = 0; i < all_frames_.size(); i++)
258                         reserved_frames_.push(all_frames_[i]);
259         }
260
261         ~bluefish_consumer()
262         {
263                 try
264                 {
265                         executor_.invoke([&]
266                         {
267                                 end_dma_thread_ = true;
268                                 disable_video_output();
269                                 blue_->detach();        
270
271                                 if (dma_present_thread_)
272                                         dma_present_thread_->join();
273                         });
274                 }
275                 catch(...)
276                 {
277                         CASPAR_LOG_CURRENT_EXCEPTION();
278                 }
279         }
280
281         void setup_hardware_output_channel()
282         {
283                 // This function would be used to setup the logic video channel in the bluefish hardware
284                 EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
285                 if (is_epoch_card((*blue_)))
286                 {
287                         if (out_vid_channel != BLUE_VIDEOCHANNEL_INVALID)
288                         {
289                                 if (BLUE_FAIL(blue_->set_card_property32(DEFAULT_VIDEO_OUTPUT_CHANNEL, out_vid_channel)))
290                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set video stream."));
291
292                                 blue_->video_playback_stop(0, 0);
293                         }
294                 }
295         }
296
297         void setup_hardware_output_channel_routing()
298         {
299                 //This function would be used to setup the dual link and any other routing that would be required .
300                 if (is_epoch_card(*blue_))
301                 {
302                         EBlueVideoChannel blueVideoOutputChannel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
303                         EEpochRoutingElements src_element = (EEpochRoutingElements)0;
304                         EEpochRoutingElements dst_element = (EEpochRoutingElements)0;
305                         get_videooutput_channel_routing_info_from_streamid(device_output_channel_, src_element, dst_element);
306                         bool duallink_4224_enabled = false;
307
308                         if ((device_output_channel_ == bluefish_hardware_output_channel::channel_a || device_output_channel_ == bluefish_hardware_output_channel::channel_c) &&
309                                 (hardware_keyer_ == hardware_downstream_keyer_mode::external) || (hardware_keyer_ == hardware_downstream_keyer_mode::internal) )
310                         {
311                                 duallink_4224_enabled = true;
312                         }
313
314                         // Enable/Disable dual link output
315                         if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT, duallink_4224_enabled)))
316                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable/disable dual link."));
317
318                         if (!duallink_4224_enabled)
319                         {
320                                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_Independent_422)))
321                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2."));
322
323                                 ULONG routingValue = EPOCH_SET_ROUTING(src_element, dst_element, BLUE_CONNECTOR_PROP_SINGLE_LINK);
324                                 if (BLUE_FAIL(blue_->set_card_property32(MR2_ROUTING, routingValue)))
325                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to MR 2 routing."));
326
327                                 // If single link 422, but on second channel AND on Neutron we need to set Genlock to Aux.
328                                 if (is_epoch_neutron_1i2o_card((*blue_)))               
329                                 {
330                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_B)
331                                         {
332                                                 ULONG genLockSource = BlueGenlockAux;
333                                                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
334                                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
335                                         }
336                                 }
337                                 if (is_epoch_neutron_3o_card((*blue_)))
338                                 {
339                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
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                                 }
346                         }
347                         else            // dual Link IS enabled, ie. 4224 Fill and Key
348                         {
349                                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
350                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
351
352                                 if (is_epoch_neutron_1i2o_card((*blue_)))               // Neutron cards require setting the Genlock conector to Aux to enable them to do Dual-Link
353                                 {
354                                         ULONG genLockSource = BlueGenlockAux;
355                                         if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
356                                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
357                                 }
358                                 else if (is_epoch_neutron_3o_card((*blue_)))
359                                 {
360                                         if (blueVideoOutputChannel == BLUE_VIDEO_OUTPUT_CHANNEL_C)
361                                         {
362                                                 ULONG genLockSource = BlueGenlockAux;
363                                                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, genLockSource)))
364                                                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info("Failed to set GenLock to Aux Input."));
365                                         }
366                                 }
367                         }
368                 }
369         }
370
371         void setup_hardware_downstream_keyer(hardware_downstream_keyer_mode keyer, hardware_downstream_keyer_audio_source audio_source)
372         {
373                 unsigned int keyer_control_value = 0, card_feature_value = 0;
374                 unsigned int card_connector_value = 0;
375                 unsigned int nOutputStreams = 0;
376                 unsigned int nInputStreams = 0;
377                 unsigned int nInputSDIConnector = 0;
378                 unsigned int nOutputSDIConnector = 0;
379                 if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_STREAM_INFO, card_feature_value)))
380                 {
381                         nOutputStreams = CARD_FEATURE_GET_SDI_OUTPUT_STREAM_COUNT(card_feature_value);
382                         nInputStreams = CARD_FEATURE_GET_SDI_INPUT_STREAM_COUNT(card_feature_value);
383                 }
384                 if (BLUE_OK(blue_->get_card_property32(CARD_FEATURE_CONNECTOR_INFO, card_connector_value)))
385                 {
386                         nOutputSDIConnector = CARD_FEATURE_GET_SDI_OUTPUT_CONNECTOR_COUNT(card_connector_value);
387                         nInputSDIConnector = CARD_FEATURE_GET_SDI_INPUT_CONNECTOR_COUNT(card_connector_value);
388                 }
389                 if (nInputSDIConnector == 0 || nInputStreams == 0)
390                         return;
391
392                 if (keyer == hardware_downstream_keyer_mode::disable || keyer == hardware_downstream_keyer_mode::external)
393                 {
394                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLED(keyer_control_value);
395                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
396                 }
397                 else if (keyer == hardware_downstream_keyer_mode::internal)
398                 {
399                         unsigned int invalidVideoModeFlag = 0;
400                         unsigned int inputVideoSignal = 0;
401                         if (BLUE_FAIL(blue_->get_card_property32(INVALID_VIDEO_MODE_FLAG, invalidVideoModeFlag)))
402                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get invalid video mode flag"));
403
404                         // The bluefish HW keyer is NOT going to pre-multiply the RGB with the A. 
405                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DATA_IS_PREMULTIPLIED(keyer_control_value);
406
407                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLED(keyer_control_value);
408                         if (BLUE_FAIL(blue_->get_card_property32(VIDEO_INPUT_SIGNAL_VIDEO_MODE, inputVideoSignal)))
409                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to get video input signal mode"));
410
411                         if (inputVideoSignal >= invalidVideoModeFlag)
412                                 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_ENABLE_OVER_BLACK(keyer_control_value);
413                         else
414                                 keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_DISABLE_OVER_BLACK(keyer_control_value);
415                 
416                         // lock to input
417                         if (BLUE_FAIL(blue_->set_card_property32(VIDEO_GENLOCK_SIGNAL, BlueSDI_A_BNC)))
418                                 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(" Failed to set the genlock to the input for the HW keyer"));
419                 }
420
421                 if (audio_source == hardware_downstream_keyer_audio_source::SDIVideoInput && (keyer == hardware_downstream_keyer_mode::internal))
422                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_INPUT_ANCILLARY(keyer_control_value);
423                 else if (audio_source == hardware_downstream_keyer_audio_source::VideoOutputChannel)
424                         keyer_control_value = VIDEO_ONBOARD_KEYER_SET_STATUS_USE_OUTPUT_ANCILLARY(keyer_control_value);
425
426                 if (BLUE_FAIL(blue_->set_card_property32(VIDEO_ONBOARD_KEYER, keyer_control_value)))
427                         CASPAR_LOG(error) << print() << TEXT(" Failed to set keyer control.");
428         }
429
430         void enable_video_output()
431         {
432                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 0)))
433                         CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");       
434         }
435
436         void disable_video_output()
437         {
438                 blue_->video_playback_stop(0,0);
439                 blue_->set_card_property32(VIDEO_DUAL_LINK_OUTPUT, 0);
440                 ULONG routingValue = EPOCH_SET_ROUTING(EPOCH_SRC_OUTPUT_MEM_INTERFACE_CHA, EPOCH_DEST_SDI_OUTPUT_B, BLUE_CONNECTOR_PROP_SINGLE_LINK);
441                 blue_->set_card_property32(MR2_ROUTING, routingValue);
442
443                 if(BLUE_FAIL(blue_->set_card_property32(VIDEO_BLACKGENERATOR, 1)))
444                         CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");        
445                 if (BLUE_FAIL(blue_->set_card_property32(EMBEDEDDED_AUDIO_OUTPUT, 0)))
446                         CASPAR_LOG(error) << print() << TEXT(" Failed to disable audio output.");
447
448         }
449         
450         std::future<bool> send(core::const_frame& frame)
451         {                               
452                 return executor_.begin_invoke([=]() -> bool
453                 {
454                         try
455                         {       
456                                 display_frame(frame);                           
457                                 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
458                                 tick_timer_.restart();
459                         }
460                         catch(...)
461                         {
462                                 CASPAR_LOG_CURRENT_EXCEPTION();
463                         }
464
465                         return true;
466                 });
467         }
468
469         void dma_present_thread_actual()
470         {
471                 bvc_wrapper wait_b;
472                 wait_b.attach(device_index_);
473                 EBlueVideoChannel out_vid_channel = get_bluesdk_videochannel_from_streamid(device_output_channel_);
474                 wait_b.set_card_property32(DEFAULT_VIDEO_OUTPUT_CHANNEL, out_vid_channel);
475                 int frames_to_buffer = BLUEFISH_HW_BUFFER_DEPTH;
476                 unsigned long buffer_id = 0;
477                 unsigned long underrun = 0;
478
479                 while (!end_dma_thread_)
480                 {
481                         blue_dma_buffer_ptr buf = nullptr;
482                         if (live_frames_.try_pop(buf) && BLUE_OK(blue_->video_playback_allocate(buffer_id, underrun)))
483                         {
484                                 // Send and display
485                                 if (embedded_audio_)
486                                 {
487                                         // Do video first, then do hanc DMA...
488                                         blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
489                                                 static_cast<unsigned long>(buf->image_size()),
490                                                 BlueImage_HANC_DMABuffer(buffer_id, BLUE_DATA_IMAGE),
491                                                 0);
492
493                                         blue_->system_buffer_write(buf->hanc_data(),
494                                                 static_cast<unsigned long>(buf->hanc_size()),
495                                                 BlueImage_HANC_DMABuffer(buffer_id, BLUE_DATA_HANC),
496                                                 0);
497
498                                         if (BLUE_FAIL(blue_->video_playback_present(BlueBuffer_Image_HANC(buffer_id), 1, 0, 0)))
499                                         {
500                                                 CASPAR_LOG(warning) << print() << TEXT(" video_playback_present failed.");
501                                         }
502                                 }
503                                 else
504                                 {
505                                         blue_->system_buffer_write(const_cast<uint8_t*>(buf->image_data()),
506                                                 static_cast<unsigned long>(buf->image_size()),
507                                                 BlueImage_DMABuffer(buffer_id, BLUE_DATA_IMAGE),
508                                                 0);
509
510                                         if (BLUE_FAIL(blue_->video_playback_present(BlueBuffer_Image(buffer_id), 1, 0, 0)))
511                                                 CASPAR_LOG(warning) << print() << TEXT(" video_playback_present failed.");
512                                 }
513
514                                 reserved_frames_.push(buf);
515                         }
516                         else
517                         {
518                                 // do WFS       
519                                 unsigned long n_field = 0;
520                                 wait_b.wait_video_output_sync(UPD_FMT_FRAME, n_field);
521                         }
522
523                         if (frames_to_buffer > 0)
524                         {
525                                 frames_to_buffer--;
526                                 if (frames_to_buffer == 0)
527                                 {
528                                         if (BLUE_FAIL(blue_->video_playback_start(0, 0)))
529                                                 CASPAR_LOG(warning) << print() << TEXT("Error video playback start failed");
530                                 }
531                         }
532                 }
533                 wait_b.detach();
534         }
535
536         void display_frame(core::const_frame frame)
537         {
538                 frame_timer_.restart();         
539
540                 if (previous_frame_ != core::const_frame::empty())
541                         presentation_delay_millis_ = previous_frame_.get_age_millis();
542
543                 previous_frame_ = frame;
544                 blue_dma_buffer_ptr buf = nullptr;
545
546                 // Copy to local buffers
547                 if (reserved_frames_.try_pop(buf))
548                 {
549                         void* dest = buf->image_data();
550                         if (!frame.image_data().empty())
551                         {
552                                 if (key_only_)
553                                         aligned_memshfl(dest, frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
554                                 else
555                                         A_memcpy(dest, frame.image_data().begin(), frame.image_data().size());
556                         }
557                         else
558                                 A_memset(dest, 0, buf->image_size());
559
560                         frame_timer_.restart();
561
562                         // remap, encode and copy hanc data
563                         if (embedded_audio_)
564                         {
565                                 auto remapped_audio = channel_remapper_.mix_and_rearrange(frame.audio_data());
566                                 auto frame_audio = core::audio_32_to_24(remapped_audio);
567                                 encode_hanc(reinterpret_cast<BLUE_UINT32*>(buf->hanc_data()),
568                                         frame_audio.data(),
569                                         static_cast<int>(frame.audio_data().size() / channel_layout_.num_channels),
570                                         static_cast<int>(channel_layout_.num_channels));
571                         }
572                         live_frames_.push(buf);
573
574                         // start the thread if required.
575                         if (dma_present_thread_ == 0)
576                         {
577                                 end_dma_thread_ = false;
578                                 dma_present_thread_ = std::make_shared<std::thread>([this] {dma_present_thread_actual(); });
579 #if defined(_WIN32)
580                                 HANDLE handle = (HANDLE)dma_present_thread_->native_handle();
581                                 SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
582 #endif
583                         }
584                 }
585                 graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
586
587                 // Sync
588                 sync_timer_.restart();
589                 unsigned long n_field = 0;
590                 blue_->wait_video_output_sync(UPD_FMT_FRAME, n_field);
591                 graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5);
592         }
593
594         void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
595         {       
596                 const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN;
597                 auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable;
598
599                 if (audio_nchannels > 4)
600                         emb_audio_flag |= blue_emb_audio_group2_enable;
601
602                 if (audio_nchannels > 8)
603                         emb_audio_flag |= blue_emb_audio_group3_enable;
604
605                 if (audio_nchannels > 12)
606                         emb_audio_flag |= blue_emb_audio_group4_enable;
607                 
608                 hanc_stream_info_struct hanc_stream_info;
609                 memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
610                 
611                 hanc_stream_info.AudioDBNArray[0] = -1;
612                 hanc_stream_info.AudioDBNArray[1] = -1;
613                 hanc_stream_info.AudioDBNArray[2] = -1;
614                 hanc_stream_info.AudioDBNArray[3] = -1;
615                 hanc_stream_info.hanc_data_ptr    = hanc_data;
616                 hanc_stream_info.video_mode               = vid_fmt_;           
617                 
618                 int cardType = CRD_INVALID;
619                 blue_->query_card_type(cardType, device_index_);
620                 blue_->encode_hanc_frame(cardType, &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
621         }
622         
623         std::wstring print() const
624         {
625                 return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" + 
626                         boost::lexical_cast<std::wstring>(device_index_) + L"|" +  format_desc_.name + L"]";
627         }
628
629         int64_t presentation_delay_millis() const
630         {
631                 return presentation_delay_millis_;
632         }
633 };
634
635 struct bluefish_consumer_proxy : public core::frame_consumer
636 {
637         core::monitor::subject                                  monitor_subject_;
638
639         std::unique_ptr<bluefish_consumer>              consumer_;
640         const int                                                               device_index_;
641         const bool                                                              embedded_audio_;
642         const bool                                                              key_only_;
643
644         std::vector<int>                                                audio_cadence_;
645         core::video_format_desc                                 format_desc_;
646         core::audio_channel_layout                              in_channel_layout_              = core::audio_channel_layout::invalid();
647         core::audio_channel_layout                              out_channel_layout_;
648         hardware_downstream_keyer_mode                  hardware_keyer_;
649         hardware_downstream_keyer_audio_source  hardware_keyer_audio_source_;
650         bluefish_hardware_output_channel                hardware_output_channel_;
651
652 public:
653
654         bluefish_consumer_proxy(int device_index, 
655                                                         bool embedded_audio, 
656                                                         bool key_only, 
657                                                         hardware_downstream_keyer_mode keyer,
658                                                         hardware_downstream_keyer_audio_source keyer_audio_source,
659                                                         const core::audio_channel_layout& out_channel_layout,
660                                                         bluefish_hardware_output_channel hardware_output_channel)
661
662                 : device_index_(device_index)
663                 , embedded_audio_(embedded_audio)
664                 , key_only_(key_only)
665                 , hardware_keyer_(keyer)
666                 , hardware_keyer_audio_source_(keyer_audio_source)
667                 , out_channel_layout_(out_channel_layout)
668                 , hardware_output_channel_(hardware_output_channel)
669         {
670         }
671         
672         // frame_consumer
673         
674         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) override
675         {
676                 format_desc_            = format_desc;
677                 in_channel_layout_      = channel_layout;
678                 audio_cadence_          = format_desc.audio_cadence;
679
680                 if (out_channel_layout_ == core::audio_channel_layout::invalid())
681                         out_channel_layout_ = in_channel_layout_;
682
683                 consumer_.reset();
684                 consumer_.reset(new bluefish_consumer(  format_desc, 
685                                                                                                 in_channel_layout_, 
686                                                                                                 out_channel_layout_, 
687                                                                                                 device_index_, 
688                                                                                                 embedded_audio_, 
689                                                                                                 key_only_, 
690                                                                                                 hardware_keyer_,
691                                                                                                 hardware_keyer_audio_source_, 
692                                                                                                 channel_index,
693                                                                                                 hardware_output_channel_));
694         }
695         
696         std::future<bool> send(core::const_frame frame) override
697         {
698                 CASPAR_VERIFY(audio_cadence_.front() * in_channel_layout_.num_channels == static_cast<size_t>(frame.audio_data().size()));
699                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
700                 return consumer_->send(frame);
701         }
702                 
703         std::wstring print() const override
704         {
705                 return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
706         }
707
708         std::wstring name() const override
709         {
710                 return L"bluefish";
711         }
712
713         boost::property_tree::wptree info() const override
714         {
715                 boost::property_tree::wptree info;
716                 info.add(L"type", L"bluefish");
717                 info.add(L"key-only", key_only_);
718                 info.add(L"device", device_index_);
719                 info.add(L"embedded-audio", embedded_audio_);
720                 info.add(L"presentation-frame-age", presentation_frame_age_millis());
721                 return info;
722         }
723
724         int buffer_depth() const override
725         {
726                 return BLUEFISH_HW_BUFFER_DEPTH;
727         }
728         
729         int index() const override
730         {
731                 return 400 + device_index_;
732         }
733
734         int64_t presentation_frame_age_millis() const override
735         {
736                 return consumer_ ? consumer_->presentation_delay_millis() : 0;
737         }
738
739         core::monitor::subject& monitor_output()
740         {
741                 return monitor_subject_;
742         }
743 };      
744
745
746 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
747 {
748         sink.short_description(L"Sends video on an SDI output using Bluefish video cards.");
749         sink.syntax(L"BLUEFISH {[device_index:int]|1} {[sdi_device:int]|a} {[embedded_audio:EMBEDDED_AUDIO]} {[key_only:KEY_ONLY]} {CHANNEL_LAYOUT [channel_layout:string]} {[keyer:string|disabled]} ");
750         sink.para()
751                 ->text(L"Sends video on an SDI output using Bluefish video cards. Multiple devices can be ")
752                 ->text(L"installed in the same machine and used at the same time, they will be addressed via ")
753                 ->text(L"different ")->code(L"device_index")->text(L" parameters.");
754         sink.para()->text(L"Multiple output channels can be accessed via the ")->code(L"sdi_device")->text(L" parameter.");
755         sink.para()->text(L"Specify ")->code(L"embedded_audio")->text(L" to embed audio into the SDI signal.");
756         sink.para()
757                 ->text(L"Specifying ")->code(L"key_only")->text(L" will extract only the alpha channel from the ")
758                 ->text(L"channel. This is useful when you have two SDI video cards, and neither has native support ")
759                 ->text(L"for separate fill/key output");
760         sink.para()->text(L"Specify ")->code(L"channel_layout")->text(L" to output a different audio channel layout than the channel uses.");
761         sink.para()->text(L"Specify ")->code(L"keyer")->text(L" to control the output channel configuration and hardware keyer")
762                 ->text(L"disabled results in a single SDI stream of 422 output - This is the default")
763                 ->text(L"external results in a 4224 stream across 2 SDI connectors, ")
764                 ->text(L"internal results in a 422 output keyed over the incoming SDI input using the dedicated hardware keyer on the Bleufish hadrware");
765         sink.para()->text(L"Specify ")->code(L"internal-keyer-audio-source")->text(L" to control the source of the audio and ANC data when using the internal/hardware keyer");
766
767         sink.para()->text(L"Examples:");
768         sink.example(L">> ADD 1 BLUEFISH", L"uses the default device_index of 1.");
769         sink.example(L">> ADD 1 BLUEFISH 2", L"for device_index 2.");
770         sink.example(
771                 L">> ADD 1 BLUEFISH 1 EMBEDDED_AUDIO\n"
772
773                 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.");
774
775 }
776
777
778 spl::shared_ptr<core::frame_consumer> create_consumer(  const std::vector<std::wstring>& params,
779                                                                                                                 core::interaction_sink*,
780                                                                                                                 std::vector<spl::shared_ptr<core::video_channel>> channels)
781 {
782         if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
783                 return core::frame_consumer::empty();
784
785         const auto device_index                 = params.size() > 1 ? boost::lexical_cast<int>(params.at(1)) : 1;
786         const auto device_stream                = contains_param(       L"SDI-STREAM", params);
787         const auto embedded_audio               = contains_param(       L"EMBEDDED_AUDIO",      params);
788         const auto key_only                             = contains_param(       L"KEY_ONLY",            params);
789         const auto channel_layout               = get_param(            L"CHANNEL_LAYOUT",      params);
790         const auto keyer_option                 = contains_param(       L"KEYER",                       params);
791         const auto keyer_audio_option   = contains_param(       L"INTERNAL-KEYER-AUDIO-SOURCE", params);
792
793         auto layout = core::audio_channel_layout::invalid();
794
795         if (!channel_layout.empty())
796         {
797                 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(channel_layout);
798
799                 if (!found_layout)
800                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + channel_layout + L" not found"));
801
802                 layout = *found_layout;
803         }
804
805         bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::channel_a;
806         if (contains_param(L"A", params))
807                 device_output_channel = bluefish_hardware_output_channel::channel_a;
808         else if (contains_param(L"B", params))
809                 device_output_channel = bluefish_hardware_output_channel::channel_b;
810         else if (contains_param(L"C", params))
811                 device_output_channel = bluefish_hardware_output_channel::channel_c;
812         else if (contains_param(L"D", params))
813                 device_output_channel = bluefish_hardware_output_channel::channel_d;
814
815         hardware_downstream_keyer_mode keyer = hardware_downstream_keyer_mode::disable;
816         if (contains_param(L"DISABLED", params))
817                 keyer = hardware_downstream_keyer_mode::disable;
818         else if (contains_param(L"EXTERNAL", params))
819                 keyer = hardware_downstream_keyer_mode::external;
820         else if (contains_param(L"INTERNAL", params))
821                 keyer = hardware_downstream_keyer_mode::internal;
822
823         hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
824         if (contains_param(L"SDIVIDEOINPUT", params))
825                 keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
826         else
827         if (contains_param(L"VIDEOOUTPUTCHANNEL", params))
828                 keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
829
830         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer, keyer_audio_source, layout, device_output_channel);
831 }
832
833 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
834                                                                                         const boost::property_tree::wptree& ptree, core::interaction_sink*,
835                                                                                         std::vector<spl::shared_ptr<core::video_channel>> channels)
836 {       
837         const auto device_index         = ptree.get(                                            L"device",                      1);
838         const auto device_stream        = ptree.get(                                            L"sdi-stream", L"a");
839         const auto embedded_audio       = ptree.get(                                            L"embedded-audio",      false);
840         const auto key_only                     = ptree.get(                                            L"key-only",            false);
841         const auto channel_layout       = ptree.get_optional<std::wstring>(     L"channel-layout");
842         const auto hardware_keyer_value = ptree.get(                                    L"keyer", L"disabled");
843         const auto keyer_audio_source_value = ptree.get(                                L"internal-keyer-audio-source", L"videooutputchannel");
844         
845         auto layout = core::audio_channel_layout::invalid();
846
847         if (channel_layout)
848         {
849                 CASPAR_SCOPED_CONTEXT_MSG("/channel-layout")
850
851                 auto found_layout = core::audio_channel_layout_repository::get_default()->get_layout(*channel_layout);
852
853                 if (!found_layout)
854                         CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Channel layout " + *channel_layout + L" not found"));
855
856                 layout = *found_layout;
857         }
858
859         bluefish_hardware_output_channel device_output_channel = bluefish_hardware_output_channel::channel_a;
860         if (device_stream == L"a")
861                 device_output_channel = bluefish_hardware_output_channel::channel_a;
862         else if (device_stream == L"b")
863                 device_output_channel = bluefish_hardware_output_channel::channel_b;
864         else if (device_stream == L"c")
865                 device_output_channel = bluefish_hardware_output_channel::channel_c;
866         else if (device_stream == L"d")
867                 device_output_channel = bluefish_hardware_output_channel::channel_d;
868
869         hardware_downstream_keyer_mode keyer_mode = hardware_downstream_keyer_mode::disable;
870         if (hardware_keyer_value == L"disabled")
871                 keyer_mode = hardware_downstream_keyer_mode::disable;
872         else if (hardware_keyer_value == L"external")
873                 keyer_mode = hardware_downstream_keyer_mode::external;
874         else if (hardware_keyer_value == L"internal")
875                 keyer_mode = hardware_downstream_keyer_mode::internal;
876
877         hardware_downstream_keyer_audio_source keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
878         if (keyer_audio_source_value == L"videooutputchannel")
879                 keyer_audio_source = hardware_downstream_keyer_audio_source::VideoOutputChannel;
880         else
881                 if (keyer_audio_source_value == L"sdivideoinput")
882                         keyer_audio_source = hardware_downstream_keyer_audio_source::SDIVideoInput;
883
884         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only, keyer_mode, keyer_audio_source, layout, device_output_channel);
885 }
886
887 }}