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