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