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