]> git.sesse.net Git - casparcg/blob - modules/bluefish/consumer/bluefish_consumer.cpp
Merged INFO DELAY from 2.0
[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/help/help_repository.h>
31 #include <core/help/help_sink.h>
32
33 #include <common/executor.h>
34 #include <common/diagnostics/graph.h>
35 #include <common/array.h>
36 #include <common/memshfl.h>
37 #include <common/param.h>
38
39 #include <core/consumer/frame_consumer.h>
40 #include <core/mixer/audio/audio_util.h>
41
42 #include <tbb/concurrent_queue.h>
43 #include <tbb/atomic.h>
44
45 #include <common/assert.h>
46 #include <boost/lexical_cast.hpp>
47 #include <boost/timer.hpp>
48 #include <boost/range/algorithm.hpp>
49 #include <boost/property_tree/ptree.hpp>
50 #include <boost/algorithm/string.hpp>
51
52 #include <asmlib.h>
53
54 #include <memory>
55 #include <array>
56
57 namespace caspar { namespace bluefish { 
58                         
59 struct bluefish_consumer : boost::noncopyable
60 {
61         spl::shared_ptr<CBlueVelvet4>           blue_;
62         const unsigned int                                      device_index_;
63         const core::video_format_desc           format_desc_;
64         const int                                                       channel_index_;
65
66         const std::wstring                                      model_name_;
67
68         spl::shared_ptr<diagnostics::graph>     graph_;
69         boost::timer                                            frame_timer_;
70         boost::timer                                            tick_timer_;
71         boost::timer                                            sync_timer_;    
72                         
73         unsigned int                                            vid_fmt_;
74
75         std::array<blue_dma_buffer_ptr, 4>      reserved_frames_;       
76         tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
77         tbb::atomic<int64_t>                            presentation_delay_millis_;
78         core::const_frame                                       previous_frame_                         = core::const_frame::empty();
79
80         const bool                                                      embedded_audio_;
81         const bool                                                      key_only_;
82                 
83         executor                                                        executor_;
84 public:
85         bluefish_consumer(const core::video_format_desc& format_desc, int device_index, bool embedded_audio, bool key_only, int channel_index) 
86                 : blue_(create_blue(device_index))
87                 , device_index_(device_index)
88                 , format_desc_(format_desc) 
89                 , channel_index_(channel_index)
90                 , model_name_(get_card_desc(*blue_))
91                 , vid_fmt_(get_video_mode(*blue_, format_desc))
92                 , embedded_audio_(embedded_audio)
93                 , key_only_(key_only)
94                 , executor_(print())
95         {
96                 executor_.set_capacity(1);
97                 presentation_delay_millis_ = 0;
98
99                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   
100                 graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f));
101                 graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
102                 graph_->set_text(print());
103                 diagnostics::register_graph(graph_);
104                         
105                 //Setting output Video mode
106                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_MODE, vid_fmt_))) 
107                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set videomode."));
108
109                 //Select Update Mode for output
110                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_UPDATE_TYPE, UPD_FMT_FRAME))) 
111                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
112         
113                 disable_video_output();
114
115                 //Enable dual link output
116                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT, 1)))
117                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable dual link."));
118
119                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
120                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
121                         
122                 //Select output memory format
123                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC))) 
124                         CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
125                 
126                 //Select image orientation
127                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
128                         CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";       
129
130                 // Select data range
131                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_RGB_DATA_RANGE, CGR_RANGE))) 
132                         CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";     
133                 
134                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_PREDEFINED_COLOR_MATRIX, vid_fmt_ == VID_FMT_PAL ? MATRIX_601_CGR : MATRIX_709_CGR)))
135                         CASPAR_LOG(warning) << print() << L" Failed to set colormatrix to " << (vid_fmt_ == VID_FMT_PAL ? L"601 CGR" : L"709 CGR") << L".";
136
137                 if(!embedded_audio_)
138                 {
139                         if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, 0))) 
140                                 CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");                       
141                         CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
142                 }
143                 else
144                 {
145                         if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, blue_emb_audio_enable | blue_emb_audio_group1_enable))) 
146                                 CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");                    
147                         CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
148                 }
149                 
150                 if (blue_->has_output_key()) 
151                 {
152                         int dummy = TRUE; int v4444 = FALSE; int invert = FALSE; int white = FALSE;
153                         blue_->set_output_key(dummy, v4444, invert, white);
154                 }
155
156                 if(blue_->GetHDCardType(device_index_) != CRD_HD_INVALID) 
157                         blue_->Set_DownConverterSignalType(vid_fmt_ == VID_FMT_PAL ? SD_SDI : HD_SDI);  
158         
159                 if(BLUE_FAIL(set_card_property(blue_, VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE))) 
160                         CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine."); 
161                 
162                 enable_video_output();
163                                                 
164                 int n = 0;
165                 boost::range::generate(reserved_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
166         }
167
168         ~bluefish_consumer()
169         {
170                 try
171                 {
172                         executor_.invoke([&]
173                         {
174                                 disable_video_output();
175                                 blue_->device_detach();         
176                         });
177                 }
178                 catch(...)
179                 {
180                         CASPAR_LOG_CURRENT_EXCEPTION();
181                 }
182         }
183         
184         void enable_video_output()
185         {
186                 if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 0)))
187                         CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");       
188         }
189
190         void disable_video_output()
191         {
192                 blue_->video_playback_stop(0,0);
193                 if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 1)))
194                         CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");                
195         }
196         
197         std::future<bool> send(core::const_frame& frame)
198         {                                       
199                 return executor_.begin_invoke([=]() -> bool
200                 {
201                         try
202                         {       
203                                 display_frame(frame);                           
204                                 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
205                                 tick_timer_.restart();
206                         }
207                         catch(...)
208                         {
209                                 CASPAR_LOG_CURRENT_EXCEPTION();
210                         }
211
212                         return true;
213                 });
214         }
215
216         void display_frame(core::const_frame frame)
217         {
218                 // Sync
219
220                 sync_timer_.restart();
221                 unsigned long n_field = 0;
222                 blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field);
223                 graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5);
224                 
225                 frame_timer_.restart();         
226
227                 if (previous_frame_ != core::const_frame::empty())
228                         presentation_delay_millis_ = previous_frame_.get_age_millis();
229
230                 previous_frame_ = frame;
231
232                 // Copy to local buffers
233                 
234                 if(!frame.image_data().empty())
235                 {
236                         if(key_only_)                                           
237                                 aligned_memshfl(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
238                         else
239                                 A_memcpy(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size());
240                 }
241                 else
242                         A_memset(reserved_frames_.front()->image_data(), 0, reserved_frames_.front()->image_size());
243                                                                 
244
245                 // Send and display
246
247                 if(embedded_audio_)
248                 {               
249                         auto frame_audio = core::audio_32_to_24(frame.audio_data());                    
250                         encode_hanc(reinterpret_cast<BLUE_UINT32*>(reserved_frames_.front()->hanc_data()), 
251                                                 frame_audio.data(), 
252                                                 static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels), 
253                                                 static_cast<int>(format_desc_.audio_channels));
254                                                                 
255                         blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()), 
256                                                                                         static_cast<unsigned long>(reserved_frames_.front()->image_size()), 
257                                                                                         nullptr, 
258                                                                                         BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
259
260                         blue_->system_buffer_write_async(reserved_frames_.front()->hanc_data(),
261                                                                                         static_cast<unsigned long>(reserved_frames_.front()->hanc_size()), 
262                                                                                         nullptr,                 
263                                                                                         BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC));
264
265                         if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id()))))
266                                 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
267                 }
268                 else
269                 {
270                         blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
271                                                                                         static_cast<unsigned long>(reserved_frames_.front()->image_size()), 
272                                                                                         nullptr,                 
273                                                                                         BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
274                         
275                         if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id()))))
276                                 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
277                 }
278
279                 boost::range::rotate(reserved_frames_, std::begin(reserved_frames_)+1);
280                 
281                 graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
282         }
283
284         void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
285         {       
286                 const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN;
287                 const auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable;
288                 
289                 hanc_stream_info_struct hanc_stream_info;
290                 memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
291                 
292                 hanc_stream_info.AudioDBNArray[0] = -1;
293                 hanc_stream_info.AudioDBNArray[1] = -1;
294                 hanc_stream_info.AudioDBNArray[2] = -1;
295                 hanc_stream_info.AudioDBNArray[3] = -1;
296                 hanc_stream_info.hanc_data_ptr    = hanc_data;
297                 hanc_stream_info.video_mode               = vid_fmt_;           
298                 
299                 if (!is_epoch_card(*blue_))
300                         encode_hanc_frame(&hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);  
301                 else
302                         encode_hanc_frame_ex(blue_->has_video_cardtype(), &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
303         }
304         
305         std::wstring print() const
306         {
307                 return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" + 
308                         boost::lexical_cast<std::wstring>(device_index_) + L"|" +  format_desc_.name + L"]";
309         }
310
311         int64_t presentation_delay_millis() const
312         {
313                 return presentation_delay_millis_;
314         }
315 };
316
317 struct bluefish_consumer_proxy : public core::frame_consumer
318 {
319         core::monitor::subject                          monitor_subject_;
320
321         std::unique_ptr<bluefish_consumer>      consumer_;
322         const int                                                       device_index_;
323         const bool                                                      embedded_audio_;
324         const bool                                                      key_only_;
325
326         std::vector<int>                                        audio_cadence_;
327         core::video_format_desc                         format_desc_;
328
329 public:
330
331         bluefish_consumer_proxy(int device_index, bool embedded_audio, bool key_only)
332                 : device_index_(device_index)
333                 , embedded_audio_(embedded_audio)
334                 , key_only_(key_only)
335         {
336         }
337         
338         // frame_consumer
339         
340         void initialize(const core::video_format_desc& format_desc, int channel_index) override
341         {
342                 format_desc_ = format_desc;
343                 audio_cadence_ = format_desc.audio_cadence;
344
345                 consumer_.reset();
346                 consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_, key_only_, channel_index));
347         }
348         
349         std::future<bool> send(core::const_frame frame) override
350         {
351                 CASPAR_VERIFY(audio_cadence_.front() * format_desc_.audio_channels == static_cast<size_t>(frame.audio_data().size()));
352                 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
353                 return consumer_->send(frame);
354         }
355                 
356         std::wstring print() const override
357         {
358                 return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
359         }
360
361         std::wstring name() const override
362         {
363                 return L"bluefish";
364         }
365
366         boost::property_tree::wptree info() const override
367         {
368                 boost::property_tree::wptree info;
369                 info.add(L"type", L"bluefish");
370                 info.add(L"key-only", key_only_);
371                 info.add(L"device", device_index_);
372                 info.add(L"embedded-audio", embedded_audio_);
373                 info.add(L"presentation-frame-age", presentation_frame_age_millis());
374                 return info;
375         }
376
377         int buffer_depth() const override
378         {
379                 return 1;
380         }
381         
382         int index() const override
383         {
384                 return 400 + device_index_;
385         }
386
387         int64_t presentation_frame_age_millis() const override
388         {
389                 return consumer_ ? consumer_->presentation_delay_millis() : 0;
390         }
391
392         core::monitor::subject& monitor_output()
393         {
394                 return monitor_subject_;
395         }
396 };      
397
398
399 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
400 {
401         sink.short_description(L"Sends video on an SDI output using Bluefish video cards.");
402         sink.syntax(L"BLUEFISH {[device_index:int]|1} {[embedded_audio:EMBEDDED_AUDIO]} {[key_only:KEY_ONLY]}");
403         sink.para()
404                 ->text(L"Sends video on an SDI output using Bluefish video cards. Multiple video cards can be ")
405                 ->text(L"installed in the same machine and used at the same time, they will be addressed via ")
406                 ->text(L"different ")->code(L"device_index")->text(L" parameters.");
407         sink.para()->text(L"Specify ")->code(L"embedded_audio")->text(L" to embed audio into the SDI signal.");
408         sink.para()
409                 ->text(L"Specifying ")->code(L"key_only")->text(L" will extract only the alpha channel from the ")
410                 ->text(L"channel. This is useful when you have two SDI video cards, and neither has native support ")
411                 ->text(L"for separate fill/key output");
412         sink.para()->text(L"Examples:");
413         sink.example(L">> ADD 1 BLUEFISH", L"uses the default device_index of 1.");
414         sink.example(L">> ADD 1 BLUEFISH 2", L"for device_index 2.");
415         sink.example(
416                 L">> ADD 1 BLUEFISH 1 EMBEDDED_AUDIO\n"
417                 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.");
418 }
419
420 spl::shared_ptr<core::frame_consumer> create_consumer(
421                 const std::vector<std::wstring>& params, core::interaction_sink*)
422 {
423         if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
424                 return core::frame_consumer::empty();
425
426         const auto device_index = params.size() > 1 ? boost::lexical_cast<int>(params.at(1)) : 1;
427
428         const auto embedded_audio       = contains_param(L"EMBEDDED_AUDIO", params);
429         const auto key_only                     = contains_param(L"KEY_ONLY", params);
430
431         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only);
432 }
433
434 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
435                 const boost::property_tree::wptree& ptree, core::interaction_sink*)
436 {       
437         const auto device_index         = ptree.get(L"device",                  1);
438         const auto embedded_audio       = ptree.get(L"embedded-audio",  false);
439         const auto key_only                     = ptree.get(L"key-only",                false);
440
441         return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only);
442 }
443
444 }}