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