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.preset1080, {
116 .container = QString(),
117 .vcodec = QString(),
118 .acodec = QString(),
119 .vbr = 0,
120 .abr = 0,
121 .dims = maintainAspect(QSize(1920, 1080))
122 });
123
124 addPreset(m_ui.preset720, {
125 .container = QString(),
126 .vcodec = QString(),
127 .acodec = QString(),
128 .vbr = 0,
129 .abr = 0,
130 .dims = maintainAspect(QSize(1280, 720))
131 });
132
133 addPreset(m_ui.preset480, {
134 .container = QString(),
135 .vcodec = QString(),
136 .acodec = QString(),
137 .vbr = 0,
138 .abr = 0,
139 .dims = maintainAspect(QSize(720, 480))
140 });
141
142 if (m_nativeWidth && m_nativeHeight) {
143 addPreset(m_ui.presetNative, {
144 .container = QString(),
145 .vcodec = QString(),
146 .acodec = QString(),
147 .vbr = 0,
148 .abr = 0,
149 .dims = QSize(m_nativeWidth, m_nativeHeight)
150 });
151 m_ui.presetNative->setEnabled(true);
152 }
153
154 addPreset(m_ui.presetHQ, {
155 .container = "MP4",
156 .vcodec = "h.264",
157 .acodec = "AAC",
158 .vbr = 8000,
159 .abr = 384,
160 .dims = maintainAspect(QSize(1920, 1080))
161 });
162
163 addPreset(m_ui.presetYoutube, {
164 .container = "MP4",
165 .vcodec = "h.264",
166 .acodec = "AAC",
167 .vbr = 5000,
168 .abr = 256,
169 .dims = maintainAspect(QSize(1280, 720))
170 });
171
172 addPreset(m_ui.presetWebM, {
173 .container = "WebM",
174 .vcodec = "VP9",
175 .acodec = "Opus",
176 .vbr = 800,
177 .abr = 128
178 });
179
180 if (m_nativeWidth && m_nativeHeight) {
181 addPreset(m_ui.presetLossless, {
182 .container = "MKV",
183 .vcodec = "h.264",
184 .acodec = "FLAC",
185 .vbr = -1,
186 .abr = 0,
187 .dims = QSize(m_nativeWidth, m_nativeHeight)
188 });
189 }
190}
191
192VideoView::~VideoView() {
193 stopRecording();
194 free(m_audioCodecCstr);
195 free(m_videoCodecCstr);
196 free(m_containerCstr);
197}
198
199void VideoView::setController(std::shared_ptr<CoreController> controller) {
200 connect(controller.get(), &CoreController::stopping, this, &VideoView::stopRecording);
201 connect(this, &VideoView::recordingStarted, controller.get(), &CoreController::setAVStream);
202 connect(this, &VideoView::recordingStopped, controller.get(), &CoreController::clearAVStream, Qt::DirectConnection);
203
204 setNativeResolution(controller->screenDimensions());
205}
206
207void VideoView::startRecording() {
208 if (!validateSettings()) {
209 return;
210 }
211 if (!FFmpegEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
212 LOG(QT, ERROR) << tr("Failed to open output video file: %1").arg(m_filename);
213 return;
214 }
215 m_ui.start->setEnabled(false);
216 m_ui.stop->setEnabled(true);
217 emit recordingStarted(&m_encoder.d);
218}
219
220void VideoView::stopRecording() {
221 emit recordingStopped();
222 FFmpegEncoderClose(&m_encoder);
223 m_ui.stop->setEnabled(false);
224 validateSettings();
225}
226
227void VideoView::setNativeResolution(const QSize& dims) {
228 m_nativeWidth = dims.width();
229 m_nativeHeight = dims.height();
230 m_ui.presetNative->setText(tr("Native (%0x%1)").arg(m_nativeWidth).arg(m_nativeHeight));
231 QSize newSize = maintainAspect(QSize(m_width, m_height));
232 m_width = newSize.width();
233 m_height = newSize.height();
234 updateAspectRatio(m_nativeWidth, m_nativeHeight, false);
235 updatePresets();
236 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
237 if (iterator.key()->isChecked()) {
238 setPreset(*iterator);
239 break;
240 }
241 }
242}
243
244void VideoView::selectFile() {
245 QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"));
246 if (!filename.isEmpty()) {
247 m_ui.filename->setText(filename);
248 }
249}
250
251void VideoView::setFilename(const QString& fname) {
252 m_filename = fname;
253 validateSettings();
254}
255
256void VideoView::setAudioCodec(const QString& codec, bool manual) {
257 free(m_audioCodecCstr);
258 m_audioCodec = sanitizeCodec(codec, s_acodecMap);
259 if (m_audioCodec == "none") {
260 m_audioCodecCstr = nullptr;
261 } else {
262 m_audioCodecCstr = strdup(m_audioCodec.toUtf8().constData());
263 }
264 if (!FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr)) {
265 free(m_audioCodecCstr);
266 m_audioCodecCstr = nullptr;
267 m_audioCodec = QString();
268 }
269 validateSettings();
270 if (manual) {
271 uncheckIncompatible();
272 }
273}
274
275void VideoView::setVideoCodec(const QString& codec, bool manual) {
276 free(m_videoCodecCstr);
277 m_videoCodec = sanitizeCodec(codec, s_vcodecMap);
278 if (m_videoCodec == "none") {
279 m_videoCodecCstr = nullptr;
280 } else {
281 m_videoCodecCstr = strdup(m_videoCodec.toUtf8().constData());
282 }
283 if (!FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr)) {
284 free(m_videoCodecCstr);
285 m_videoCodecCstr = nullptr;
286 m_videoCodec = QString();
287 }
288 validateSettings();
289 if (manual) {
290 uncheckIncompatible();
291 }
292}
293
294void VideoView::setContainer(const QString& container, bool manual) {
295 free(m_containerCstr);
296 m_container = sanitizeCodec(container, s_containerMap);
297 m_containerCstr = strdup(m_container.toUtf8().constData());
298 if (!FFmpegEncoderSetContainer(&m_encoder, m_containerCstr)) {
299 free(m_containerCstr);
300 m_containerCstr = nullptr;
301 m_container = QString();
302 }
303 validateSettings();
304 if (manual) {
305 uncheckIncompatible();
306 }
307}
308
309void VideoView::setAudioBitrate(int br, bool manual) {
310 m_abr = br * 1000;
311 FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr);
312 validateSettings();
313 if (manual) {
314 uncheckIncompatible();
315 }
316}
317
318void VideoView::setVideoBitrate(int br, bool manual) {
319 m_vbr = br >= 0 ? br * 1000 : 0;
320 FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr);
321 validateSettings();
322 if (manual) {
323 uncheckIncompatible();
324 }
325}
326
327void VideoView::setWidth(int width, bool manual) {
328 m_width = width;
329 updateAspectRatio(width, 0, false);
330 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
331 if (manual) {
332 uncheckIncompatible();
333 }
334}
335
336void VideoView::setHeight(int height, bool manual) {
337 m_height = height;
338 updateAspectRatio(0, height, false);
339 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
340 if (manual) {
341 uncheckIncompatible();
342 }
343}
344
345void VideoView::setAspectWidth(int, bool manual) {
346 updateAspectRatio(0, m_height, true);
347 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
348 if (manual) {
349 uncheckIncompatible();
350 }
351}
352
353void VideoView::setAspectHeight(int, bool manual) {
354 updateAspectRatio(m_width, 0, true);
355 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
356 if (manual) {
357 uncheckIncompatible();
358 }
359}
360
361void VideoView::showAdvanced(bool show) {
362 m_ui.advancedBox->setVisible(show);
363}
364
365bool VideoView::validateSettings() {
366 bool valid = !m_filename.isNull() && !FFmpegEncoderIsOpen(&m_encoder);
367 if (m_audioCodec.isNull()) {
368 valid = false;
369 m_ui.audio->setStyleSheet("QComboBox { color: red; }");
370 } else {
371 m_ui.audio->setStyleSheet("");
372 }
373
374 if (m_videoCodec.isNull()) {
375 valid = false;
376 m_ui.video->setStyleSheet("QComboBox { color: red; }");
377 } else {
378 m_ui.video->setStyleSheet("");
379 }
380
381 if (m_container.isNull()) {
382 valid = false;
383 m_ui.container->setStyleSheet("QComboBox { color: red; }");
384 } else {
385 m_ui.container->setStyleSheet("");
386 }
387
388 // This |valid| check is necessary as if one of the cstrs
389 // is null, the encoder likely has a dangling pointer
390 if (valid && !FFmpegEncoderVerifyContainer(&m_encoder)) {
391 valid = false;
392 }
393
394 m_ui.start->setEnabled(valid);
395
396 return valid;
397}
398
399void VideoView::updateAspectRatio(int width, int height, bool force) {
400 if (m_ui.lockRatio->isChecked() || force) {
401 if (width) {
402 height = m_ui.hratio->value() * width / m_ui.wratio->value();
403 } else if (height) {
404 width = m_ui.wratio->value() * height / m_ui.hratio->value();
405 }
406
407 m_width = width;
408 m_height = height;
409 safelySet(m_ui.width, m_width);
410 safelySet(m_ui.height, m_height);
411 } else {
412 int w = m_width;
413 int h = m_height;
414 // Get greatest common divisor
415 while (w != 0) {
416 int temp = h % w;
417 h = w;
418 w = temp;
419 }
420 int gcd = h;
421 w = m_width / gcd;
422 h = m_height / gcd;
423 safelySet(m_ui.wratio, w);
424 safelySet(m_ui.hratio, h);
425 }
426}
427
428void VideoView::uncheckIncompatible() {
429 Preset current = {
430 .container = m_container,
431 .vcodec = m_videoCodec,
432 .acodec = m_audioCodec,
433 .vbr = m_vbr / 1000,
434 .abr = m_abr / 1000,
435 .dims = QSize(m_width, m_height)
436 };
437
438 m_ui.presets->setExclusive(false);
439 m_ui.resolutions->setExclusive(false);
440 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
441 Preset next = *iterator;
442 next.container = sanitizeCodec(next.container, s_containerMap);
443 next.acodec = sanitizeCodec(next.acodec, s_acodecMap);
444 next.vcodec = sanitizeCodec(next.vcodec, s_vcodecMap);
445 if (!current.compatible(next)) {
446 safelyCheck(iterator.key(), false);
447 }
448 }
449 m_ui.presets->setExclusive(true);
450 m_ui.resolutions->setExclusive(true);
451
452 if (current.compatible(m_presets[m_ui.presetNative])) {
453 safelyCheck(m_ui.presetNative);
454 }
455 if (current.compatible(m_presets[m_ui.preset480])) {
456 safelyCheck(m_ui.preset480);
457 }
458 if (current.compatible(m_presets[m_ui.preset720])) {
459 safelyCheck(m_ui.preset720);
460 }
461 if (current.compatible(m_presets[m_ui.preset1080])) {
462 safelyCheck(m_ui.preset1080);
463 }
464}
465
466QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
467 QString sanitized = codec.toLower();
468 sanitized = sanitized.remove(QChar('.'));
469 sanitized = sanitized.remove(QChar('('));
470 sanitized = sanitized.remove(QChar(')'));
471 if (mapping.contains(sanitized)) {
472 sanitized = mapping[sanitized];
473 }
474 return sanitized;
475}
476
477void VideoView::safelyCheck(QAbstractButton* button, bool set) {
478 bool signalsBlocked = button->blockSignals(true);
479 bool autoExclusive = button->autoExclusive();
480 button->setAutoExclusive(false);
481 button->setChecked(set);
482 button->setAutoExclusive(autoExclusive);
483 button->blockSignals(signalsBlocked);
484}
485
486void VideoView::safelySet(QSpinBox* box, int value) {
487 bool signalsBlocked = box->blockSignals(true);
488 box->setValue(value);
489 box->blockSignals(signalsBlocked);
490}
491
492void VideoView::safelySet(QComboBox* box, const QString& value) {
493 bool signalsBlocked = box->blockSignals(true);
494 box->lineEdit()->setText(value);
495 box->blockSignals(signalsBlocked);
496}
497
498void VideoView::addPreset(QAbstractButton* button, const Preset& preset) {
499 m_presets[button] = preset;
500 button->disconnect();
501 connect(button, &QAbstractButton::pressed, [this, preset]() {
502 setPreset(preset);
503 });
504}
505
506void VideoView::setPreset(const Preset& preset) {
507 if (!preset.container.isNull()) {
508 setContainer(preset.container, false);
509 safelySet(m_ui.container, preset.container);
510 }
511 if (!preset.acodec.isNull()) {
512 setAudioCodec(preset.acodec, false);
513 safelySet(m_ui.audio, preset.acodec);
514 }
515 if (!preset.vcodec.isNull()) {
516 setVideoCodec(preset.vcodec, false);
517 safelySet(m_ui.video, preset.vcodec);
518 }
519 if (preset.abr) {
520 setAudioBitrate(preset.abr, false);
521 safelySet(m_ui.abr, preset.abr);
522 }
523 if (preset.vbr) {
524 setVideoBitrate(preset.vbr, false);
525 safelySet(m_ui.vbr, preset.vbr);
526 }
527 if (preset.dims.width() > 0) {
528 setWidth(preset.dims.width(), false);
529 safelySet(m_ui.width, preset.dims.width());
530 }
531 if (preset.dims.height() > 0) {
532 setHeight(preset.dims.height(), false);
533 safelySet(m_ui.height, preset.dims.height());
534 }
535
536 uncheckIncompatible();
537 validateSettings();
538}
539
540QSize VideoView::maintainAspect(const QSize& size) {
541 QSize ds = size;
542 if (ds.width() * m_nativeHeight > ds.height() * m_nativeWidth) {
543 ds.setWidth(ds.height() * m_nativeWidth / m_nativeHeight);
544 } else if (ds.width() * m_nativeHeight < ds.height() * m_nativeWidth) {
545 ds.setHeight(ds.width() * m_nativeHeight / m_nativeWidth);
546 }
547 return ds;
548}
549
550#endif