2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
24 #include "bluefish_consumer.h"
25 #include "../util/blue_velvet.h"
26 #include "../util/memory.h"
28 #include <core/video_format.h>
29 #include <core/frame/frame.h>
31 #include <common/executor.h>
32 #include <common/diagnostics/graph.h>
33 #include <common/array.h>
34 #include <common/memshfl.h>
35 #include <common/param.h>
37 #include <core/consumer/frame_consumer.h>
38 #include <core/mixer/audio/audio_util.h>
40 #include <tbb/concurrent_queue.h>
42 #include <common/assert.h>
43 #include <boost/lexical_cast.hpp>
44 #include <boost/timer.hpp>
45 #include <boost/range/algorithm.hpp>
46 #include <boost/property_tree/ptree.hpp>
47 #include <boost/algorithm/string.hpp>
54 namespace caspar { namespace bluefish {
56 struct bluefish_consumer : boost::noncopyable
58 spl::shared_ptr<CBlueVelvet4> blue_;
59 const unsigned int device_index_;
60 const core::video_format_desc format_desc_;
61 const int channel_index_;
63 const std::wstring model_name_;
65 spl::shared_ptr<diagnostics::graph> graph_;
66 boost::timer frame_timer_;
67 boost::timer tick_timer_;
68 boost::timer sync_timer_;
70 unsigned int vid_fmt_;
72 std::array<blue_dma_buffer_ptr, 4> reserved_frames_;
73 tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
75 const bool embedded_audio_;
80 bluefish_consumer(const core::video_format_desc& format_desc, int device_index, bool embedded_audio, bool key_only, int channel_index)
81 : blue_(create_blue(device_index))
82 , device_index_(device_index)
83 , format_desc_(format_desc)
84 , channel_index_(channel_index)
85 , model_name_(get_card_desc(*blue_))
86 , vid_fmt_(get_video_mode(*blue_, format_desc))
87 , embedded_audio_(embedded_audio)
91 executor_.set_capacity(1);
93 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
94 graph_->set_color("sync-time", diagnostics::color(1.0f, 0.0f, 0.0f));
95 graph_->set_color("frame-time", diagnostics::color(0.5f, 1.0f, 0.2f));
96 graph_->set_text(print());
97 diagnostics::register_graph(graph_);
99 //Setting output Video mode
100 if(BLUE_FAIL(set_card_property(blue_, VIDEO_MODE, vid_fmt_)))
101 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set videomode."));
103 //Select Update Mode for output
104 if(BLUE_FAIL(set_card_property(blue_, VIDEO_UPDATE_TYPE, UPD_FMT_FRAME)))
105 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set update type."));
107 disable_video_output();
109 //Enable dual link output
110 if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT, 1)))
111 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to enable dual link."));
113 if(BLUE_FAIL(set_card_property(blue_, VIDEO_DUAL_LINK_OUTPUT_SIGNAL_FORMAT_TYPE, Signal_FormatType_4224)))
114 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set dual link format type to 4:2:2:4."));
116 //Select output memory format
117 if(BLUE_FAIL(set_card_property(blue_, VIDEO_MEMORY_FORMAT, MEM_FMT_ARGB_PC)))
118 CASPAR_THROW_EXCEPTION(caspar_exception() << msg_info(print() + L" Failed to set memory format."));
120 //Select image orientation
121 if(BLUE_FAIL(set_card_property(blue_, VIDEO_IMAGE_ORIENTATION, ImageOrientation_Normal)))
122 CASPAR_LOG(warning) << print() << L" Failed to set image orientation to normal.";
125 if(BLUE_FAIL(set_card_property(blue_, VIDEO_RGB_DATA_RANGE, CGR_RANGE)))
126 CASPAR_LOG(warning) << print() << L" Failed to set RGB data range to CGR.";
128 if(BLUE_FAIL(set_card_property(blue_, VIDEO_PREDEFINED_COLOR_MATRIX, vid_fmt_ == VID_FMT_PAL ? MATRIX_601_CGR : MATRIX_709_CGR)))
129 CASPAR_LOG(warning) << print() << L" Failed to set colormatrix to " << (vid_fmt_ == VID_FMT_PAL ? L"601 CGR" : L"709 CGR") << L".";
133 if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, 0)))
134 CASPAR_LOG(warning) << TEXT("BLUECARD ERROR: Failed to disable embedded audio.");
135 CASPAR_LOG(info) << print() << TEXT(" Disabled embedded-audio.");
139 if(BLUE_FAIL(set_card_property(blue_, EMBEDEDDED_AUDIO_OUTPUT, blue_emb_audio_enable | blue_emb_audio_group1_enable)))
140 CASPAR_LOG(warning) << print() << TEXT(" Failed to enable embedded audio.");
141 CASPAR_LOG(info) << print() << TEXT(" Enabled embedded-audio.");
144 if (blue_->has_output_key())
146 int dummy = TRUE; int v4444 = FALSE; int invert = FALSE; int white = FALSE;
147 blue_->set_output_key(dummy, v4444, invert, white);
150 if(blue_->GetHDCardType(device_index_) != CRD_HD_INVALID)
151 blue_->Set_DownConverterSignalType(vid_fmt_ == VID_FMT_PAL ? SD_SDI : HD_SDI);
153 if(BLUE_FAIL(set_card_property(blue_, VIDEO_OUTPUT_ENGINE, VIDEO_ENGINE_FRAMESTORE)))
154 CASPAR_LOG(warning) << print() << TEXT(" Failed to set video engine.");
156 enable_video_output();
159 boost::range::generate(reserved_frames_, [&]{return std::make_shared<blue_dma_buffer>(static_cast<int>(format_desc_.size), n++);});
168 disable_video_output();
169 blue_->device_detach();
174 CASPAR_LOG_CURRENT_EXCEPTION();
178 void enable_video_output()
180 if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 0)))
181 CASPAR_LOG(error) << print() << TEXT(" Failed to disable video output.");
184 void disable_video_output()
186 blue_->video_playback_stop(0,0);
187 if(!BLUE_PASS(set_card_property(blue_, VIDEO_BLACKGENERATOR, 1)))
188 CASPAR_LOG(error)<< print() << TEXT(" Failed to disable video output.");
191 std::future<bool> send(core::const_frame& frame)
193 return executor_.begin_invoke([=]() -> bool
197 display_frame(frame);
198 graph_->set_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
199 tick_timer_.restart();
203 CASPAR_LOG_CURRENT_EXCEPTION();
210 void display_frame(core::const_frame frame)
214 sync_timer_.restart();
215 unsigned long n_field = 0;
216 blue_->wait_output_video_synch(UPD_FMT_FRAME, n_field);
217 graph_->set_value("sync-time", sync_timer_.elapsed()*format_desc_.fps*0.5);
219 frame_timer_.restart();
221 // Copy to local buffers
223 if(!frame.image_data().empty())
226 aligned_memshfl(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
228 A_memcpy(reserved_frames_.front()->image_data(), frame.image_data().begin(), frame.image_data().size());
231 A_memset(reserved_frames_.front()->image_data(), 0, reserved_frames_.front()->image_size());
238 auto frame_audio = core::audio_32_to_24(frame.audio_data());
239 encode_hanc(reinterpret_cast<BLUE_UINT32*>(reserved_frames_.front()->hanc_data()),
241 static_cast<int>(frame.audio_data().size()/format_desc_.audio_channels),
242 static_cast<int>(format_desc_.audio_channels));
244 blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
245 static_cast<unsigned long>(reserved_frames_.front()->image_size()),
247 BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
249 blue_->system_buffer_write_async(reserved_frames_.front()->hanc_data(),
250 static_cast<unsigned long>(reserved_frames_.front()->hanc_size()),
252 BlueImage_HANC_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_HANC));
254 if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image_HANC(reserved_frames_.front()->id()))))
255 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
259 blue_->system_buffer_write_async(const_cast<uint8_t*>(reserved_frames_.front()->image_data()),
260 static_cast<unsigned long>(reserved_frames_.front()->image_size()),
262 BlueImage_DMABuffer(reserved_frames_.front()->id(), BLUE_DATA_IMAGE));
264 if(BLUE_FAIL(blue_->render_buffer_update(BlueBuffer_Image(reserved_frames_.front()->id()))))
265 CASPAR_LOG(warning) << print() << TEXT(" render_buffer_update failed.");
268 boost::range::rotate(reserved_frames_, std::begin(reserved_frames_)+1);
270 graph_->set_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
273 void encode_hanc(BLUE_UINT32* hanc_data, void* audio_data, int audio_samples, int audio_nchannels)
275 const auto sample_type = AUDIO_CHANNEL_24BIT | AUDIO_CHANNEL_LITTLEENDIAN;
276 const auto emb_audio_flag = blue_emb_audio_enable | blue_emb_audio_group1_enable;
278 hanc_stream_info_struct hanc_stream_info;
279 memset(&hanc_stream_info, 0, sizeof(hanc_stream_info));
281 hanc_stream_info.AudioDBNArray[0] = -1;
282 hanc_stream_info.AudioDBNArray[1] = -1;
283 hanc_stream_info.AudioDBNArray[2] = -1;
284 hanc_stream_info.AudioDBNArray[3] = -1;
285 hanc_stream_info.hanc_data_ptr = hanc_data;
286 hanc_stream_info.video_mode = vid_fmt_;
288 if (!is_epoch_card(*blue_))
289 encode_hanc_frame(&hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
291 encode_hanc_frame_ex(blue_->has_video_cardtype(), &hanc_stream_info, audio_data, audio_nchannels, audio_samples, sample_type, emb_audio_flag);
294 std::wstring print() const
296 return model_name_ + L" [" + boost::lexical_cast<std::wstring>(channel_index_) + L"-" +
297 boost::lexical_cast<std::wstring>(device_index_) + L"|" + format_desc_.name + L"]";
301 struct bluefish_consumer_proxy : public core::frame_consumer
303 core::monitor::subject monitor_subject_;
305 std::unique_ptr<bluefish_consumer> consumer_;
306 const int device_index_;
307 const bool embedded_audio_;
308 const bool key_only_;
310 std::vector<int> audio_cadence_;
311 core::video_format_desc format_desc_;
315 bluefish_consumer_proxy(int device_index, bool embedded_audio, bool key_only)
316 : device_index_(device_index)
317 , embedded_audio_(embedded_audio)
318 , key_only_(key_only)
324 void initialize(const core::video_format_desc& format_desc, int channel_index) override
326 format_desc_ = format_desc;
327 audio_cadence_ = format_desc.audio_cadence;
330 consumer_.reset(new bluefish_consumer(format_desc, device_index_, embedded_audio_, key_only_, channel_index));
333 std::future<bool> send(core::const_frame frame) override
335 CASPAR_VERIFY(audio_cadence_.front() * format_desc_.audio_channels == static_cast<size_t>(frame.audio_data().size()));
336 boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);
337 return consumer_->send(frame);
340 std::wstring print() const override
342 return consumer_ ? consumer_->print() : L"[bluefish_consumer]";
345 std::wstring name() const override
350 boost::property_tree::wptree info() const override
352 boost::property_tree::wptree info;
353 info.add(L"type", L"bluefish");
354 info.add(L"key-only", key_only_);
355 info.add(L"device", device_index_);
356 info.add(L"embedded-audio", embedded_audio_);
360 int buffer_depth() const override
365 int index() const override
367 return 400 + device_index_;
370 core::monitor::subject& monitor_output()
372 return monitor_subject_;
376 spl::shared_ptr<core::frame_consumer> create_consumer(
377 const std::vector<std::wstring>& params, core::interaction_sink*)
379 if(params.size() < 1 || !boost::iequals(params.at(0), L"BLUEFISH"))
380 return core::frame_consumer::empty();
382 const auto device_index = params.size() > 1 ? boost::lexical_cast<int>(params.at(1)) : 1;
384 const auto embedded_audio = contains_param(L"EMBEDDED_AUDIO", params);
385 const auto key_only = contains_param(L"KEY_ONLY", params);
387 return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only);
390 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
391 const boost::property_tree::wptree& ptree, core::interaction_sink*)
393 const auto device_index = ptree.get(L"device", 1);
394 const auto embedded_audio = ptree.get(L"embedded-audio", false);
395 const auto key_only = ptree.get(L"key-only", false);
397 return spl::make_shared<bluefish_consumer_proxy>(device_index, embedded_audio, key_only);