4 #include <bmusb/bmusb.h>
6 #include <movit/effect_chain.h>
7 #include <movit/image_format.h>
8 #include <movit/resource_pool.h>
12 #include <QActionGroup>
13 #include <QInputDialog>
26 #include <QActionGroup>
28 #include <QInputDialog>
34 #include "audio_mixer.h"
35 #include "shared/context.h"
36 #include "context_menus.h"
37 #include "mainwindow.h"
39 #include "shared/ref_counted_gl_sync.h"
40 #include "shared/shared_defs.h"
41 #include "ycbcr_interpretation.h"
46 #include <movit/util.h>
49 using namespace movit;
51 using namespace std::placeholders;
55 double srgb_to_linear(double x)
60 return pow((x + 0.055) / 1.055, 2.4);
66 GLWidget::GLWidget(QWidget *parent)
67 : QGLWidget(parent, global_share_widget)
75 void GLWidget::shutdown()
77 if (resource_pool != nullptr) {
79 resource_pool->clean_context();
81 global_mixer->remove_frame_ready_callback(output, this);
84 void GLWidget::grab_white_balance(unsigned channel, unsigned x, unsigned y)
86 // Set the white balance to neutral for the grab. It's probably going to
87 // flicker a bit, but hopefully this display is not live anyway.
88 global_mixer->set_wb(output, 1.0, 1.0, 1.0);
89 global_mixer->wait_for_next_frame();
91 // Mark that the next paintGL() should grab the given pixel.
94 grab_output = Mixer::Output(Mixer::OUTPUT_INPUT0 + channel);
100 void GLWidget::initializeGL()
102 static once_flag flag;
103 call_once(flag, [this]{
104 global_mixer = new Mixer(QGLFormat::toSurfaceFormat(format()));
105 global_audio_mixer = global_mixer->get_audio_mixer();
106 global_mainwindow->mixer_created(global_mixer);
107 global_mixer->start();
109 global_mixer->add_frame_ready_callback(output, this, [this]{
110 QMetaObject::invokeMethod(this, "update", Qt::AutoConnection);
112 global_mixer->set_name_updated_callback(output, [this](const string &name){
113 emit name_updated(output, name);
115 if (output == Mixer::OUTPUT_LIVE) {
116 global_mixer->set_transition_names_updated_callback(output, [this](const vector<string> &names){
117 emit transition_names_updated(names);
120 if (output >= Mixer::OUTPUT_INPUT0) {
121 global_mixer->set_color_updated_callback(output, [this](const string &color){
122 emit color_updated(output, color);
125 setContextMenuPolicy(Qt::CustomContextMenu);
126 connect(this, &QWidget::customContextMenuRequested, bind(&GLWidget::show_context_menu, this, _1));
129 glDisable(GL_DEPTH_TEST);
130 glDepthMask(GL_FALSE);
133 void GLWidget::resizeGL(int width, int height)
135 current_width = width;
136 current_height = height;
137 glViewport(0, 0, width, height);
140 void GLWidget::paintGL()
142 Mixer::DisplayFrame frame;
143 if (!global_mixer->get_display_frame(output, &frame)) {
144 glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
146 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152 glWaitSync(frame.ready_fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
156 glDisable(GL_FRAMEBUFFER_SRGB);
158 frame.chain->render_to_fbo(0, current_width, current_height);
161 if (resource_pool == nullptr) {
162 resource_pool = frame.chain->get_resource_pool();
164 assert(resource_pool == frame.chain->get_resource_pool());
168 GLfloat reference_color[4];
169 glReadPixels(grab_x, current_height - grab_y - 1, 1, 1, GL_BGRA, GL_FLOAT, reference_color);
171 double r = srgb_to_linear(reference_color[2]);
172 double g = srgb_to_linear(reference_color[1]);
173 double b = srgb_to_linear(reference_color[0]);
174 global_mixer->set_wb(grab_output, r, g, b);
179 void GLWidget::mousePressEvent(QMouseEvent *event)
184 void GLWidget::show_context_menu(const QPoint &pos)
186 if (output == Mixer::OUTPUT_LIVE) {
187 show_live_context_menu(pos);
189 if (output >= Mixer::OUTPUT_INPUT0) {
190 int signal_num = global_mixer->map_channel_to_signal(output);
191 if (signal_num != -1) {
192 show_preview_context_menu(signal_num, pos);
197 void GLWidget::show_live_context_menu(const QPoint &pos)
199 QPoint global_pos = mapToGlobal(pos);
203 // Add a submenu for selecting output card, with an action for each card.
205 fill_hdmi_sdi_output_device_menu(&card_submenu);
206 card_submenu.setTitle("HDMI/SDI output device");
207 menu.addMenu(&card_submenu);
209 // Add a submenu for choosing the output resolution. Since this is
210 // card-dependent, it is disabled if we haven't chosen a card
211 // (but it's still there so that the user will know it exists).
212 QMenu resolution_submenu;
213 fill_hdmi_sdi_output_resolution_menu(&resolution_submenu);
214 resolution_submenu.setTitle("HDMI/SDI output resolution");
215 menu.addMenu(&resolution_submenu);
217 // Show the menu; if there's an action selected, it will deal with it itself.
218 menu.exec(global_pos);
221 void GLWidget::show_preview_context_menu(unsigned signal_num, const QPoint &pos)
223 QPoint global_pos = mapToGlobal(pos);
227 // Add a submenu for selecting input card, with an action for each card.
229 QActionGroup card_group(&card_submenu);
231 QMenu interpretation_submenu;
232 QActionGroup interpretation_group(&interpretation_submenu);
234 QMenu video_input_submenu;
235 QActionGroup video_input_group(&video_input_submenu);
237 QMenu audio_input_submenu;
238 QActionGroup audio_input_group(&audio_input_submenu);
241 QActionGroup mode_group(&mode_submenu);
243 unsigned current_card = global_mixer->map_signal_to_card(signal_num);
244 bool is_ffmpeg = global_mixer->card_is_ffmpeg(current_card);
246 if (!is_ffmpeg) { // FFmpeg inputs are not connected to any card; they're locked to a given input and have a given Y'CbCr interpretatio and have a given Y'CbCr interpretation.
247 for (unsigned card_index = 0; card_index < MAX_VIDEO_CARDS; ++card_index) {
248 if (!global_mixer->card_is_active(card_index)) continue;
249 if (global_mixer->card_is_cef(card_index) || global_mixer->card_is_ffmpeg(card_index)) continue;
250 QString description(QString::fromStdString(global_mixer->get_card_description(card_index)));
251 QAction *action = new QAction(description, &card_group);
252 action->setCheckable(true);
253 if (current_card == card_index) {
254 action->setChecked(true);
256 action->setData(QList<QVariant>{"card", card_index});
257 card_submenu.addAction(action);
260 card_submenu.setTitle("Input source");
261 menu.addMenu(&card_submenu);
263 // Note that this setting depends on which card is active.
265 YCbCrInterpretation current_interpretation = global_mixer->get_input_ycbcr_interpretation(current_card);
267 QAction *action = new QAction("Auto", &interpretation_group);
268 action->setCheckable(true);
269 if (current_interpretation.ycbcr_coefficients_auto) {
270 action->setChecked(true);
272 action->setData(QList<QVariant>{"interpretation", true, YCBCR_REC_709, false});
273 interpretation_submenu.addAction(action);
275 for (YCbCrLumaCoefficients ycbcr_coefficients : { YCBCR_REC_709, YCBCR_REC_601 }) {
276 for (bool full_range : { false, true }) {
277 std::string description;
278 if (ycbcr_coefficients == YCBCR_REC_709) {
279 description = "Rec. 709 (HD)";
281 description = "Rec. 601 (SD)";
284 description += ", full range (nonstandard)";
286 QAction *action = new QAction(QString::fromStdString(description), &interpretation_group);
287 action->setCheckable(true);
288 if (!current_interpretation.ycbcr_coefficients_auto &&
289 ycbcr_coefficients == current_interpretation.ycbcr_coefficients &&
290 full_range == current_interpretation.full_range) {
291 action->setChecked(true);
293 action->setData(QList<QVariant>{"interpretation", false, ycbcr_coefficients, full_range});
294 interpretation_submenu.addAction(action);
298 interpretation_submenu.setTitle("Input interpretation");
299 menu.addMenu(&interpretation_submenu);
302 // --- The choices in the next few options depend a lot on which card is active ---
304 bool has_auto_mode = false;
305 QAction *change_url_action = nullptr;
307 // Add a menu to change the source URL if we're an FFmpeg card.
308 // (The theme can still override.)
309 change_url_action = new QAction("Change source filename/URL…", &menu);
310 menu.addAction(change_url_action);
312 // Add a submenu for selecting video input, with an action for each input.
313 std::map<uint32_t, string> video_inputs = global_mixer->get_available_video_inputs(current_card);
314 uint32_t current_video_input = global_mixer->get_current_video_input(current_card);
315 for (const auto &mode : video_inputs) {
316 QString description(QString::fromStdString(mode.second));
317 QAction *action = new QAction(description, &video_input_group);
318 action->setCheckable(true);
319 if (mode.first == current_video_input) {
320 action->setChecked(true);
322 action->setData(QList<QVariant>{"video_input", mode.first});
323 video_input_submenu.addAction(action);
326 video_input_submenu.setTitle("Video input");
327 menu.addMenu(&video_input_submenu);
329 // The same for audio input.
330 std::map<uint32_t, string> audio_inputs = global_mixer->get_available_audio_inputs(current_card);
331 uint32_t current_audio_input = global_mixer->get_current_audio_input(current_card);
332 for (const auto &mode : audio_inputs) {
333 QString description(QString::fromStdString(mode.second));
334 QAction *action = new QAction(description, &audio_input_group);
335 action->setCheckable(true);
336 if (mode.first == current_audio_input) {
337 action->setChecked(true);
339 action->setData(QList<QVariant>{"audio_input", mode.first});
340 audio_input_submenu.addAction(action);
343 audio_input_submenu.setTitle("Audio input");
344 menu.addMenu(&audio_input_submenu);
346 // The same for resolution.
347 std::map<uint32_t, bmusb::VideoMode> video_modes = global_mixer->get_available_video_modes(current_card);
348 uint32_t current_video_mode = global_mixer->get_current_video_mode(current_card);
349 for (const auto &mode : video_modes) {
350 QString description(QString::fromStdString(mode.second.name));
351 QAction *action = new QAction(description, &mode_group);
352 action->setCheckable(true);
353 if (mode.first == current_video_mode) {
354 action->setChecked(true);
356 action->setData(QList<QVariant>{"video_mode", mode.first});
357 mode_submenu.addAction(action);
359 // TODO: Relying on the 0 value here (from bmusb.h) is ugly, it should be a named constant.
360 if (mode.first == 0) {
361 has_auto_mode = true;
365 // Add a “scan” menu if there's no “auto” mode.
366 if (!has_auto_mode) {
367 QAction *action = new QAction("Scan", &mode_group);
368 action->setData(QList<QVariant>{"video_mode", 0});
369 mode_submenu.addSeparator();
370 mode_submenu.addAction(action);
373 mode_submenu.setTitle("Input mode");
374 menu.addMenu(&mode_submenu);
377 // --- End of card-dependent choices ---
379 // Add an audio source selector.
380 QAction *audio_source_action = nullptr;
381 if (global_audio_mixer->get_mapping_mode() == AudioMixer::MappingMode::SIMPLE) {
382 audio_source_action = new QAction("Use as audio source", &menu);
383 audio_source_action->setCheckable(true);
384 if (global_audio_mixer->get_simple_input() == current_card) {
385 audio_source_action->setChecked(true);
386 audio_source_action->setEnabled(false);
388 menu.addAction(audio_source_action);
391 // And a master clock selector.
392 QAction *master_clock_action = new QAction("Use as master clock", &menu);
393 master_clock_action->setCheckable(true);
394 if (global_mixer->get_output_card_index() != -1 && global_mixer->get_output_card_is_master()) {
395 master_clock_action->setChecked(false);
396 master_clock_action->setEnabled(false);
397 } else if (global_mixer->get_master_clock() == signal_num) {
398 master_clock_action->setChecked(true);
399 master_clock_action->setEnabled(false);
401 menu.addAction(master_clock_action);
403 // Show the menu and look at the result.
404 QAction *selected_item = menu.exec(global_pos);
405 if (audio_source_action != nullptr && selected_item == audio_source_action) {
406 global_audio_mixer->set_simple_input(current_card);
407 } else if (change_url_action != nullptr && selected_item == change_url_action) {
408 // NOTE: We can't use “this” as parent, since the dialog would inherit our style sheet.
410 const string url = global_mixer->get_ffmpeg_filename(current_card);
411 QString new_url = QInputDialog::getText(window(), tr("Change URL"),
412 tr("Enter new filename/URL for the given video input:"), QLineEdit::Normal,
413 QString::fromStdString(url), &ok);
414 // FIXME prefill the input
416 global_mixer->set_ffmpeg_filename(current_card, new_url.toStdString());
418 } else if (selected_item == master_clock_action) {
419 global_mixer->set_master_clock(signal_num);
420 } else if (selected_item != nullptr) {
421 QList<QVariant> selected = selected_item->data().toList();
422 if (selected[0].toString() == "video_mode") {
423 uint32_t mode = selected[1].toUInt(nullptr);
424 if (mode == 0 && !has_auto_mode) {
425 global_mixer->start_mode_scanning(current_card);
427 global_mixer->set_video_mode(current_card, mode);
429 } else if (selected[0].toString() == "video_input") {
430 uint32_t input = selected[1].toUInt(nullptr);
431 global_mixer->set_video_input(current_card, input);
432 } else if (selected[0].toString() == "audio_input") {
433 uint32_t input = selected[1].toUInt(nullptr);
434 global_mixer->set_audio_input(current_card, input);
435 } else if (selected[0].toString() == "card") {
436 unsigned card_index = selected[1].toUInt(nullptr);
437 global_mixer->set_signal_mapping(signal_num, card_index);
438 } else if (selected[0].toString() == "interpretation") {
439 YCbCrInterpretation interpretation;
440 interpretation.ycbcr_coefficients_auto = selected[1].toBool();
441 interpretation.ycbcr_coefficients = YCbCrLumaCoefficients(selected[2].toUInt(nullptr));
442 interpretation.full_range = selected[3].toBool();
443 global_mixer->set_input_ycbcr_interpretation(current_card, interpretation);