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 m_videoCodecCstr = strdup(m_videoCodec.toUtf8().constData());
279 if (!FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr)) {
280 free(m_videoCodecCstr);
281 m_videoCodecCstr = nullptr;
282 m_videoCodec = QString();
283 }
284 validateSettings();
285 if (manual) {
286 uncheckIncompatible();
287 }
288}
289
290void VideoView::setContainer(const QString& container, bool manual) {
291 free(m_containerCstr);
292 m_container = sanitizeCodec(container, s_containerMap);
293 m_containerCstr = strdup(m_container.toUtf8().constData());
294 if (!FFmpegEncoderSetContainer(&m_encoder, m_containerCstr)) {
295 free(m_containerCstr);
296 m_containerCstr = nullptr;
297 m_container = QString();
298 }
299 validateSettings();
300 if (manual) {
301 uncheckIncompatible();
302 }
303}
304
305void VideoView::setAudioBitrate(int br, bool manual) {
306 m_abr = br * 1000;
307 FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr);
308 validateSettings();
309 if (manual) {
310 uncheckIncompatible();
311 }
312}
313
314void VideoView::setVideoBitrate(int br, bool manual) {
315 m_vbr = br >= 0 ? br * 1000 : 0;
316 FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr);
317 validateSettings();
318 if (manual) {
319 uncheckIncompatible();
320 }
321}
322
323void VideoView::setWidth(int width, bool manual) {
324 m_width = width;
325 updateAspectRatio(width, 0, false);
326 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
327 if (manual) {
328 uncheckIncompatible();
329 }
330}
331
332void VideoView::setHeight(int height, bool manual) {
333 m_height = height;
334 updateAspectRatio(0, height, false);
335 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
336 if (manual) {
337 uncheckIncompatible();
338 }
339}
340
341void VideoView::setAspectWidth(int, bool manual) {
342 updateAspectRatio(0, m_height, true);
343 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
344 if (manual) {
345 uncheckIncompatible();
346 }
347}
348
349void VideoView::setAspectHeight(int, bool manual) {
350 updateAspectRatio(m_width, 0, true);
351 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
352 if (manual) {
353 uncheckIncompatible();
354 }
355}
356
357void VideoView::showAdvanced(bool show) {
358 m_ui.advancedBox->setVisible(show);
359}
360
361bool VideoView::validateSettings() {
362 bool valid = !m_filename.isNull() && !FFmpegEncoderIsOpen(&m_encoder);
363 if (m_audioCodec.isNull()) {
364 valid = false;
365 m_ui.audio->setStyleSheet("QComboBox { color: red; }");
366 } else {
367 m_ui.audio->setStyleSheet("");
368 }
369
370 if (m_videoCodec.isNull()) {
371 valid = false;
372 m_ui.video->setStyleSheet("QComboBox { color: red; }");
373 } else {
374 m_ui.video->setStyleSheet("");
375 }
376
377 if (m_container.isNull()) {
378 valid = false;
379 m_ui.container->setStyleSheet("QComboBox { color: red; }");
380 } else {
381 m_ui.container->setStyleSheet("");
382 }
383
384 // This |valid| check is necessary as if one of the cstrs
385 // is null, the encoder likely has a dangling pointer
386 if (valid && !FFmpegEncoderVerifyContainer(&m_encoder)) {
387 valid = false;
388 }
389
390 m_ui.start->setEnabled(valid);
391
392 return valid;
393}
394
395void VideoView::updateAspectRatio(int width, int height, bool force) {
396 if (m_ui.lockRatio->isChecked() || force) {
397 if (width) {
398 height = m_ui.hratio->value() * width / m_ui.wratio->value();
399 } else if (height) {
400 width = m_ui.wratio->value() * height / m_ui.hratio->value();
401 }
402
403 m_width = width;
404 m_height = height;
405 safelySet(m_ui.width, m_width);
406 safelySet(m_ui.height, m_height);
407 } else {
408 int w = m_width;
409 int h = m_height;
410 // Get greatest common divisor
411 while (w != 0) {
412 int temp = h % w;
413 h = w;
414 w = temp;
415 }
416 int gcd = h;
417 w = m_width / gcd;
418 h = m_height / gcd;
419 safelySet(m_ui.wratio, w);
420 safelySet(m_ui.hratio, h);
421 }
422}
423
424void VideoView::uncheckIncompatible() {
425 Preset current = {
426 .container = m_container,
427 .vcodec = m_videoCodec,
428 .acodec = m_audioCodec,
429 .vbr = m_vbr / 1000,
430 .abr = m_abr / 1000,
431 .dims = QSize(m_width, m_height)
432 };
433
434 m_ui.presets->setExclusive(false);
435 m_ui.resolutions->setExclusive(false);
436 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
437 Preset next = *iterator;
438 next.container = sanitizeCodec(next.container, s_containerMap);
439 next.acodec = sanitizeCodec(next.acodec, s_acodecMap);
440 next.vcodec = sanitizeCodec(next.vcodec, s_vcodecMap);
441 if (!current.compatible(next)) {
442 safelyCheck(iterator.key(), false);
443 }
444 }
445 m_ui.presets->setExclusive(true);
446 m_ui.resolutions->setExclusive(true);
447
448 if (current.compatible(m_presets[m_ui.presetNative])) {
449 safelyCheck(m_ui.presetNative);
450 }
451 if (current.compatible(m_presets[m_ui.preset480])) {
452 safelyCheck(m_ui.preset480);
453 }
454 if (current.compatible(m_presets[m_ui.preset720])) {
455 safelyCheck(m_ui.preset720);
456 }
457 if (current.compatible(m_presets[m_ui.preset1080])) {
458 safelyCheck(m_ui.preset1080);
459 }
460}
461
462QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
463 QString sanitized = codec.toLower();
464 sanitized = sanitized.remove(QChar('.'));
465 sanitized = sanitized.remove(QChar('('));
466 sanitized = sanitized.remove(QChar(')'));
467 if (mapping.contains(sanitized)) {
468 sanitized = mapping[sanitized];
469 }
470 return sanitized;
471}
472
473void VideoView::safelyCheck(QAbstractButton* button, bool set) {
474 bool signalsBlocked = button->blockSignals(true);
475 bool autoExclusive = button->autoExclusive();
476 button->setAutoExclusive(false);
477 button->setChecked(set);
478 button->setAutoExclusive(autoExclusive);
479 button->blockSignals(signalsBlocked);
480}
481
482void VideoView::safelySet(QSpinBox* box, int value) {
483 bool signalsBlocked = box->blockSignals(true);
484 box->setValue(value);
485 box->blockSignals(signalsBlocked);
486}
487
488void VideoView::safelySet(QComboBox* box, const QString& value) {
489 bool signalsBlocked = box->blockSignals(true);
490 box->lineEdit()->setText(value);
491 box->blockSignals(signalsBlocked);
492}
493
494void VideoView::addPreset(QAbstractButton* button, const Preset& preset) {
495 m_presets[button] = preset;
496 button->disconnect();
497 connect(button, &QAbstractButton::pressed, [this, preset]() {
498 setPreset(preset);
499 });
500}
501
502void VideoView::setPreset(const Preset& preset) {
503 if (!preset.container.isNull()) {
504 setContainer(preset.container, false);
505 safelySet(m_ui.container, preset.container);
506 }
507 if (!preset.acodec.isNull()) {
508 setAudioCodec(preset.acodec, false);
509 safelySet(m_ui.audio, preset.acodec);
510 }
511 if (!preset.vcodec.isNull()) {
512 setVideoCodec(preset.vcodec, false);
513 safelySet(m_ui.video, preset.vcodec);
514 }
515 if (preset.abr) {
516 setAudioBitrate(preset.abr, false);
517 safelySet(m_ui.abr, preset.abr);
518 }
519 if (preset.vbr) {
520 setVideoBitrate(preset.vbr, false);
521 safelySet(m_ui.vbr, preset.vbr);
522 }
523 if (preset.dims.width() > 0) {
524 setWidth(preset.dims.width(), false);
525 safelySet(m_ui.width, preset.dims.width());
526 }
527 if (preset.dims.height() > 0) {
528 setHeight(preset.dims.height(), false);
529 safelySet(m_ui.height, preset.dims.height());
530 }
531
532 uncheckIncompatible();
533 validateSettings();
534}
535
536QSize VideoView::maintainAspect(const QSize& size) {
537 QSize ds = size;
538 if (ds.width() * m_nativeHeight > ds.height() * m_nativeWidth) {
539 ds.setWidth(ds.height() * m_nativeWidth / m_nativeHeight);
540 } else if (ds.width() * m_nativeHeight < ds.height() * m_nativeWidth) {
541 ds.setHeight(ds.width() * m_nativeHeight / m_nativeWidth);
542 }
543 return ds;
544}
545
546#endif