src/platform/qt/VideoView.cpp (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "VideoView.h"
7
8#ifdef USE_FFMPEG
9
10#include "GBAApp.h"
11#include "LogController.h"
12
13#include <QMap>
14
15using namespace QGBA;
16
17QMap<QString, QString> VideoView::s_acodecMap;
18QMap<QString, QString> VideoView::s_vcodecMap;
19QMap<QString, QString> VideoView::s_containerMap;
20
21bool VideoView::Preset::compatible(const Preset& other) const {
22 if (!other.container.isNull() && !container.isNull() && other.container != container) {
23 return false;
24 }
25 if (!other.acodec.isNull() && !acodec.isNull() && other.acodec != acodec) {
26 return false;
27 }
28 if (!other.vcodec.isNull() && !vcodec.isNull() && other.vcodec != vcodec) {
29 return false;
30 }
31 if (other.abr && abr && other.abr != abr) {
32 return false;
33 }
34 if (other.vbr && vbr && other.vbr != vbr) {
35 return false;
36 }
37 if (other.dims.width() && dims.width() && other.dims.width() != dims.width()) {
38 return false;
39 }
40 if (other.dims.height() && dims.height() && other.dims.height() != dims.height()) {
41 return false;
42 }
43 return true;
44}
45
46VideoView::VideoView(QWidget* parent)
47 : QWidget(parent)
48{
49 m_ui.setupUi(this);
50
51 if (s_acodecMap.empty()) {
52 s_acodecMap["mp3"] = "libmp3lame";
53 s_acodecMap["opus"] = "libopus";
54 s_acodecMap["vorbis"] = "libvorbis";
55 s_acodecMap["uncompressed"] = "pcm_s16le";
56 }
57 if (s_vcodecMap.empty()) {
58 s_vcodecMap["dirac"] = "libschroedinger";
59 s_vcodecMap["h264"] = "libx264";
60 s_vcodecMap["h264 nvenc"] = "h264_nvenc";
61 s_vcodecMap["hevc"] = "libx265";
62 s_vcodecMap["hevc nvenc"] = "hevc_nvenc";
63 s_vcodecMap["theora"] = "libtheora";
64 s_vcodecMap["vp8"] = "libvpx";
65 s_vcodecMap["vp9"] = "libvpx-vp9";
66 s_vcodecMap["xvid"] = "libxvid";
67 }
68 if (s_containerMap.empty()) {
69 s_containerMap["mkv"] = "matroska";
70 }
71
72 connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &VideoView::close);
73 connect(m_ui.start, &QAbstractButton::clicked, this, &VideoView::startRecording);
74 connect(m_ui.stop, &QAbstractButton::clicked, this, &VideoView::stopRecording);
75
76 connect(m_ui.selectFile, &QAbstractButton::clicked, this, &VideoView::selectFile);
77 connect(m_ui.filename, &QLineEdit::textChanged, this, &VideoView::setFilename);
78
79 connect(m_ui.audio, SIGNAL(activated(const QString&)), this, SLOT(setAudioCodec(const QString&)));
80 connect(m_ui.video, SIGNAL(activated(const QString&)), this, SLOT(setVideoCodec(const QString&)));
81 connect(m_ui.container, SIGNAL(activated(const QString&)), this, SLOT(setContainer(const QString&)));
82 connect(m_ui.audio, SIGNAL(editTextChanged(const QString&)), this, SLOT(setAudioCodec(const QString&)));
83 connect(m_ui.video, SIGNAL(editTextChanged(const QString&)), this, SLOT(setVideoCodec(const QString&)));
84 connect(m_ui.container, SIGNAL(editTextChanged(const QString&)), this, SLOT(setContainer(const QString&)));
85
86 connect(m_ui.abr, SIGNAL(valueChanged(int)), this, SLOT(setAudioBitrate(int)));
87 connect(m_ui.vbr, SIGNAL(valueChanged(int)), this, SLOT(setVideoBitrate(int)));
88
89 connect(m_ui.width, SIGNAL(valueChanged(int)), this, SLOT(setWidth(int)));
90 connect(m_ui.height, SIGNAL(valueChanged(int)), this, SLOT(setHeight(int)));
91
92 connect(m_ui.wratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectWidth(int)));
93 connect(m_ui.hratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectHeight(int)));
94
95 connect(m_ui.showAdvanced, &QAbstractButton::clicked, this, &VideoView::showAdvanced);
96
97 FFmpegEncoderInit(&m_encoder);
98
99 updatePresets();
100
101 setPreset({
102 .container = "MKV",
103 .vcodec = "h.264",
104 .acodec = "FLAC",
105 .vbr = -1,
106 .abr = 0,
107 .dims = QSize(),
108 });
109 showAdvanced(false);
110}
111
112void VideoView::updatePresets() {
113 m_presets.clear();
114
115 addPreset(m_ui.preset4K, {
116 .container = QString(),
117 .vcodec = QString(),
118 .acodec = QString(),
119 .vbr = 0,
120 .abr = 0,
121 .dims = maintainAspect(QSize(3840, 2160))
122 });
123
124 addPreset(m_ui.preset1080, {
125 .container = QString(),
126 .vcodec = QString(),
127 .acodec = QString(),
128 .vbr = 0,
129 .abr = 0,
130 .dims = maintainAspect(QSize(1920, 1080))
131 });
132
133 addPreset(m_ui.preset720, {
134 .container = QString(),
135 .vcodec = QString(),
136 .acodec = QString(),
137 .vbr = 0,
138 .abr = 0,
139 .dims = maintainAspect(QSize(1280, 720))
140 });
141
142 addPreset(m_ui.preset480, {
143 .container = QString(),
144 .vcodec = QString(),
145 .acodec = QString(),
146 .vbr = 0,
147 .abr = 0,
148 .dims = maintainAspect(QSize(720, 480))
149 });
150
151 if (m_nativeWidth && m_nativeHeight) {
152 addPreset(m_ui.presetNative, {
153 .container = QString(),
154 .vcodec = QString(),
155 .acodec = QString(),
156 .vbr = 0,
157 .abr = 0,
158 .dims = QSize(m_nativeWidth, m_nativeHeight)
159 });
160 m_ui.presetNative->setEnabled(true);
161 }
162
163 addPreset(m_ui.presetHQ, {
164 .container = "MP4",
165 .vcodec = "h.264",
166 .acodec = "AAC",
167 .vbr = 8000,
168 .abr = 384,
169 .dims = maintainAspect(QSize(1920, 1080))
170 });
171
172 addPreset(m_ui.presetYoutube, {
173 .container = "MP4",
174 .vcodec = "h.264",
175 .acodec = "AAC",
176 .vbr = 5000,
177 .abr = 256,
178 .dims = maintainAspect(QSize(1280, 720))
179 });
180
181 addPreset(m_ui.presetWebM, {
182 .container = "WebM",
183 .vcodec = "VP9",
184 .acodec = "Opus",
185 .vbr = 800,
186 .abr = 128
187 });
188
189 addPreset(m_ui.presetMP4, {
190 .container = "MP4",
191 .vcodec = "h.264",
192 .acodec = "AAC",
193 .vbr = 800,
194 .abr = 128
195 });
196
197 if (m_nativeWidth && m_nativeHeight) {
198 addPreset(m_ui.presetLossless, {
199 .container = "MKV",
200 .vcodec = "h.264",
201 .acodec = "FLAC",
202 .vbr = -1,
203 .abr = 0,
204 .dims = QSize(m_nativeWidth, m_nativeHeight)
205 });
206 }
207}
208
209VideoView::~VideoView() {
210 stopRecording();
211 free(m_audioCodecCstr);
212 free(m_videoCodecCstr);
213 free(m_containerCstr);
214}
215
216void VideoView::setController(std::shared_ptr<CoreController> controller) {
217 CoreController* controllerPtr = controller.get();
218 connect(controllerPtr, &CoreController::frameAvailable, this, [this, controllerPtr]() {
219 setNativeResolution(controllerPtr->screenDimensions());
220 });
221 connect(controllerPtr, &CoreController::stopping, this, &VideoView::stopRecording);
222 connect(this, &VideoView::recordingStarted, controllerPtr, &CoreController::setAVStream);
223 connect(this, &VideoView::recordingStopped, controllerPtr, &CoreController::clearAVStream, Qt::DirectConnection);
224
225 setNativeResolution(controllerPtr->screenDimensions());
226}
227
228void VideoView::startRecording() {
229 if (!validateSettings()) {
230 return;
231 }
232 if (!FFmpegEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
233 LOG(QT, ERROR) << tr("Failed to open output video file: %1").arg(m_filename);
234 return;
235 }
236 m_ui.start->setEnabled(false);
237 m_ui.stop->setEnabled(true);
238 emit recordingStarted(&m_encoder.d);
239}
240
241void VideoView::stopRecording() {
242 emit recordingStopped();
243 FFmpegEncoderClose(&m_encoder);
244 m_ui.stop->setEnabled(false);
245 validateSettings();
246}
247
248void VideoView::setNativeResolution(const QSize& dims) {
249 if (dims.width() == m_nativeWidth && dims.height() == m_nativeHeight) {
250 return;
251 }
252 m_nativeWidth = dims.width();
253 m_nativeHeight = dims.height();
254 m_ui.presetNative->setText(tr("Native (%0x%1)").arg(m_nativeWidth).arg(m_nativeHeight));
255 QSize newSize = maintainAspect(QSize(m_width, m_height));
256 m_width = newSize.width();
257 m_height = newSize.height();
258 updateAspectRatio(m_nativeWidth, m_nativeHeight, false);
259 updatePresets();
260 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
261 if (iterator.key()->isChecked()) {
262 setPreset(*iterator);
263 break;
264 }
265 }
266}
267
268void VideoView::selectFile() {
269 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"));
270 if (!filename.isEmpty()) {
271 m_ui.filename->setText(filename);
272 }
273}
274
275void VideoView::setFilename(const QString& fname) {
276 m_filename = fname;
277 validateSettings();
278}
279
280void VideoView::setAudioCodec(const QString& codec, bool manual) {
281 free(m_audioCodecCstr);
282 m_audioCodec = sanitizeCodec(codec, s_acodecMap);
283 if (m_audioCodec == "none") {
284 m_audioCodecCstr = nullptr;
285 } else {
286 m_audioCodecCstr = strdup(m_audioCodec.toUtf8().constData());
287 }
288 if (!FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr)) {
289 free(m_audioCodecCstr);
290 m_audioCodecCstr = nullptr;
291 m_audioCodec = QString();
292 }
293 validateSettings();
294 if (manual) {
295 uncheckIncompatible();
296 }
297}
298
299void VideoView::setVideoCodec(const QString& codec, bool manual) {
300 free(m_videoCodecCstr);
301 m_videoCodec = sanitizeCodec(codec, s_vcodecMap);
302 if (m_videoCodec == "none") {
303 m_videoCodecCstr = nullptr;
304 } else {
305 m_videoCodecCstr = strdup(m_videoCodec.toUtf8().constData());
306 }
307 if (!FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr, 0)) {
308 free(m_videoCodecCstr);
309 m_videoCodecCstr = nullptr;
310 m_videoCodec = QString();
311 }
312 validateSettings();
313 if (manual) {
314 uncheckIncompatible();
315 }
316}
317
318void VideoView::setContainer(const QString& container, bool manual) {
319 free(m_containerCstr);
320 m_container = sanitizeCodec(container, s_containerMap);
321 m_containerCstr = strdup(m_container.toUtf8().constData());
322 if (!FFmpegEncoderSetContainer(&m_encoder, m_containerCstr)) {
323 free(m_containerCstr);
324 m_containerCstr = nullptr;
325 m_container = QString();
326 }
327 validateSettings();
328 if (manual) {
329 uncheckIncompatible();
330 }
331}
332
333void VideoView::setAudioBitrate(int br, bool manual) {
334 m_abr = br * 1000;
335 FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr);
336 validateSettings();
337 if (manual) {
338 uncheckIncompatible();
339 }
340}
341
342void VideoView::setVideoBitrate(int br, bool manual) {
343 m_vbr = br >= 0 ? br * 1000 : 0;
344 FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr, 0);
345 validateSettings();
346 if (manual) {
347 uncheckIncompatible();
348 }
349}
350
351void VideoView::setWidth(int width, bool manual) {
352 m_width = width;
353 updateAspectRatio(width, 0, false);
354 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
355 if (manual) {
356 uncheckIncompatible();
357 }
358}
359
360void VideoView::setHeight(int height, bool manual) {
361 m_height = height;
362 updateAspectRatio(0, height, false);
363 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
364 if (manual) {
365 uncheckIncompatible();
366 }
367}
368
369void VideoView::setAspectWidth(int, bool manual) {
370 updateAspectRatio(0, m_height, true);
371 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
372 if (manual) {
373 uncheckIncompatible();
374 }
375}
376
377void VideoView::setAspectHeight(int, bool manual) {
378 updateAspectRatio(m_width, 0, true);
379 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
380 if (manual) {
381 uncheckIncompatible();
382 }
383}
384
385void VideoView::showAdvanced(bool show) {
386 m_ui.advancedBox->setVisible(show);
387}
388
389bool VideoView::validateSettings() {
390 bool valid = !m_filename.isNull() && !FFmpegEncoderIsOpen(&m_encoder);
391 if (m_audioCodec.isNull()) {
392 valid = false;
393 m_ui.audio->setStyleSheet("QComboBox { color: red; }");
394 } else {
395 m_ui.audio->setStyleSheet("");
396 }
397
398 if (m_videoCodec.isNull()) {
399 valid = false;
400 m_ui.video->setStyleSheet("QComboBox { color: red; }");
401 } else {
402 m_ui.video->setStyleSheet("");
403 }
404
405 if (m_container.isNull()) {
406 valid = false;
407 m_ui.container->setStyleSheet("QComboBox { color: red; }");
408 } else {
409 m_ui.container->setStyleSheet("");
410 }
411
412 // This |valid| check is necessary as if one of the cstrs
413 // is null, the encoder likely has a dangling pointer
414 if (valid && !FFmpegEncoderVerifyContainer(&m_encoder)) {
415 valid = false;
416 }
417
418 m_ui.start->setEnabled(valid);
419
420 return valid;
421}
422
423void VideoView::updateAspectRatio(int width, int height, bool force) {
424 if (m_ui.lockRatio->isChecked() || force) {
425 if (width) {
426 height = m_ui.hratio->value() * width / m_ui.wratio->value();
427 } else if (height) {
428 width = m_ui.wratio->value() * height / m_ui.hratio->value();
429 }
430
431 m_width = width;
432 m_height = height;
433 safelySet(m_ui.width, m_width);
434 safelySet(m_ui.height, m_height);
435 } else {
436 int w = m_width;
437 int h = m_height;
438 // Get greatest common divisor
439 while (w != 0) {
440 int temp = h % w;
441 h = w;
442 w = temp;
443 }
444 int gcd = h;
445 w = m_width / gcd;
446 h = m_height / gcd;
447 safelySet(m_ui.wratio, w);
448 safelySet(m_ui.hratio, h);
449 }
450}
451
452void VideoView::uncheckIncompatible() {
453 Preset current = {
454 .container = m_container,
455 .vcodec = m_videoCodec,
456 .acodec = m_audioCodec,
457 .vbr = m_vbr / 1000,
458 .abr = m_abr / 1000,
459 .dims = QSize(m_width, m_height)
460 };
461
462 m_ui.presets->setExclusive(false);
463 m_ui.resolutions->setExclusive(false);
464 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
465 Preset next = *iterator;
466 next.container = sanitizeCodec(next.container, s_containerMap);
467 next.acodec = sanitizeCodec(next.acodec, s_acodecMap);
468 next.vcodec = sanitizeCodec(next.vcodec, s_vcodecMap);
469 if (!current.compatible(next)) {
470 safelyCheck(iterator.key(), false);
471 }
472 }
473 m_ui.presets->setExclusive(true);
474 m_ui.resolutions->setExclusive(true);
475
476 if (current.compatible(m_presets[m_ui.presetNative])) {
477 safelyCheck(m_ui.presetNative);
478 }
479 if (current.compatible(m_presets[m_ui.preset480])) {
480 safelyCheck(m_ui.preset480);
481 }
482 if (current.compatible(m_presets[m_ui.preset720])) {
483 safelyCheck(m_ui.preset720);
484 }
485 if (current.compatible(m_presets[m_ui.preset1080])) {
486 safelyCheck(m_ui.preset1080);
487 }
488}
489
490QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
491 QString sanitized = codec.toLower();
492 sanitized = sanitized.remove(QChar('.'));
493 sanitized = sanitized.remove(QChar('('));
494 sanitized = sanitized.remove(QChar(')'));
495 if (mapping.contains(sanitized)) {
496 sanitized = mapping[sanitized];
497 }
498 return sanitized;
499}
500
501void VideoView::safelyCheck(QAbstractButton* button, bool set) {
502 bool signalsBlocked = button->blockSignals(true);
503 bool autoExclusive = button->autoExclusive();
504 button->setAutoExclusive(false);
505 button->setChecked(set);
506 button->setAutoExclusive(autoExclusive);
507 button->blockSignals(signalsBlocked);
508}
509
510void VideoView::safelySet(QSpinBox* box, int value) {
511 bool signalsBlocked = box->blockSignals(true);
512 box->setValue(value);
513 box->blockSignals(signalsBlocked);
514}
515
516void VideoView::safelySet(QComboBox* box, const QString& value) {
517 bool signalsBlocked = box->blockSignals(true);
518 box->lineEdit()->setText(value);
519 box->blockSignals(signalsBlocked);
520}
521
522void VideoView::addPreset(QAbstractButton* button, const Preset& preset) {
523 m_presets[button] = preset;
524 button->disconnect();
525 connect(button, &QAbstractButton::pressed, [this, preset]() {
526 setPreset(preset);
527 });
528}
529
530void VideoView::setPreset(const Preset& preset) {
531 if (!preset.container.isNull()) {
532 setContainer(preset.container, false);
533 safelySet(m_ui.container, preset.container);
534 }
535 if (!preset.acodec.isNull()) {
536 setAudioCodec(preset.acodec, false);
537 safelySet(m_ui.audio, preset.acodec);
538 }
539 if (!preset.vcodec.isNull()) {
540 setVideoCodec(preset.vcodec, false);
541 safelySet(m_ui.video, preset.vcodec);
542 }
543 if (preset.abr) {
544 setAudioBitrate(preset.abr, false);
545 safelySet(m_ui.abr, preset.abr);
546 }
547 if (preset.vbr) {
548 setVideoBitrate(preset.vbr, false);
549 safelySet(m_ui.vbr, preset.vbr);
550 }
551 if (preset.dims.width() > 0) {
552 setWidth(preset.dims.width(), false);
553 safelySet(m_ui.width, preset.dims.width());
554 }
555 if (preset.dims.height() > 0) {
556 setHeight(preset.dims.height(), false);
557 safelySet(m_ui.height, preset.dims.height());
558 }
559
560 uncheckIncompatible();
561 validateSettings();
562}
563
564QSize VideoView::maintainAspect(const QSize& size) {
565 QSize ds = size;
566 if (ds.width() * m_nativeHeight > ds.height() * m_nativeWidth) {
567 ds.setWidth(ds.height() * m_nativeWidth / m_nativeHeight);
568 } else if (ds.width() * m_nativeHeight < ds.height() * m_nativeWidth) {
569 ds.setHeight(ds.width() * m_nativeHeight / m_nativeWidth);
570 }
571 return ds;
572}
573
574#endif