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
15#ifdef M_CORE_GB
16#include "gb/video.h"
17#endif
18
19using namespace QGBA;
20
21QMap<QString, QString> VideoView::s_acodecMap;
22QMap<QString, QString> VideoView::s_vcodecMap;
23QMap<QString, QString> VideoView::s_containerMap;
24
25bool VideoView::Preset::compatible(const Preset& other) const {
26 if (!other.container.isNull() && !container.isNull() && other.container != container) {
27 return false;
28 }
29 if (!other.acodec.isNull() && !acodec.isNull() && other.acodec != acodec) {
30 return false;
31 }
32 if (!other.vcodec.isNull() && !vcodec.isNull() && other.vcodec != vcodec) {
33 return false;
34 }
35 if (other.abr && abr && other.abr != abr) {
36 return false;
37 }
38 if (other.vbr && vbr && other.vbr != vbr) {
39 return false;
40 }
41 if (other.dims.width() && dims.width() && other.dims.width() != dims.width()) {
42 return false;
43 }
44 if (other.dims.height() && dims.height() && other.dims.height() != dims.height()) {
45 return false;
46 }
47 return true;
48}
49
50VideoView::VideoView(QWidget* parent)
51 : QWidget(parent)
52 , m_audioCodecCstr(nullptr)
53 , m_videoCodecCstr(nullptr)
54 , m_containerCstr(nullptr)
55 , m_nativeWidth(0)
56 , m_nativeHeight(0)
57 , m_width(1)
58 , m_height(1)
59{
60 m_ui.setupUi(this);
61
62 if (s_acodecMap.empty()) {
63 s_acodecMap["mp3"] = "libmp3lame";
64 s_acodecMap["opus"] = "libopus";
65 s_acodecMap["uncompressed"] = "pcm_s16le";
66 }
67 if (s_vcodecMap.empty()) {
68 s_vcodecMap["dirac"] = "libschroedinger";
69 s_vcodecMap["h264"] = "libx264";
70 s_vcodecMap["hevc"] = "libx265";
71 s_vcodecMap["theora"] = "libtheora";
72 s_vcodecMap["vp8"] = "libvpx";
73 s_vcodecMap["vp9"] = "libvpx-vp9";
74 s_vcodecMap["xvid"] = "libxvid";
75 }
76 if (s_containerMap.empty()) {
77 s_containerMap["mkv"] = "matroska";
78 }
79
80 connect(m_ui.buttonBox, SIGNAL(rejected()), this, SLOT(close()));
81 connect(m_ui.start, SIGNAL(clicked()), this, SLOT(startRecording()));
82 connect(m_ui.stop, SIGNAL(clicked()), this, SLOT(stopRecording()));
83
84 connect(m_ui.selectFile, SIGNAL(clicked()), this, SLOT(selectFile()));
85 connect(m_ui.filename, SIGNAL(textChanged(const QString&)), this, SLOT(setFilename(const QString&)));
86
87 connect(m_ui.audio, SIGNAL(activated(const QString&)), this, SLOT(setAudioCodec(const QString&)));
88 connect(m_ui.video, SIGNAL(activated(const QString&)), this, SLOT(setVideoCodec(const QString&)));
89 connect(m_ui.container, SIGNAL(activated(const QString&)), this, SLOT(setContainer(const QString&)));
90 connect(m_ui.audio, SIGNAL(editTextChanged(const QString&)), this, SLOT(setAudioCodec(const QString&)));
91 connect(m_ui.video, SIGNAL(editTextChanged(const QString&)), this, SLOT(setVideoCodec(const QString&)));
92 connect(m_ui.container, SIGNAL(editTextChanged(const QString&)), this, SLOT(setContainer(const QString&)));
93
94 connect(m_ui.abr, SIGNAL(valueChanged(int)), this, SLOT(setAudioBitrate(int)));
95 connect(m_ui.vbr, SIGNAL(valueChanged(int)), this, SLOT(setVideoBitrate(int)));
96
97 connect(m_ui.width, SIGNAL(valueChanged(int)), this, SLOT(setWidth(int)));
98 connect(m_ui.height, SIGNAL(valueChanged(int)), this, SLOT(setHeight(int)));
99
100 connect(m_ui.wratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectWidth(int)));
101 connect(m_ui.hratio, SIGNAL(valueChanged(int)), this, SLOT(setAspectHeight(int)));
102
103 connect(m_ui.showAdvanced, SIGNAL(clicked(bool)), this, SLOT(showAdvanced(bool)));
104
105 FFmpegEncoderInit(&m_encoder);
106
107 updatePresets();
108
109 setPreset({
110 .container = "MKV",
111 .vcodec = "PNG",
112 .acodec = "FLAC",
113 .vbr = 0,
114 .abr = 0,
115 .dims = QSize(),
116 });
117 showAdvanced(false);
118}
119
120void VideoView::updatePresets() {
121 m_presets.clear();
122
123 addPreset(m_ui.preset1080, {
124 .container = QString(),
125 .vcodec = QString(),
126 .acodec = QString(),
127 .vbr = 0,
128 .abr = 0,
129 .dims = maintainAspect(QSize(1920, 1080))
130 });
131
132 addPreset(m_ui.preset720, {
133 .container = QString(),
134 .vcodec = QString(),
135 .acodec = QString(),
136 .vbr = 0,
137 .abr = 0,
138 .dims = maintainAspect(QSize(1280, 720))
139 });
140
141 addPreset(m_ui.preset480, {
142 .container = QString(),
143 .vcodec = QString(),
144 .acodec = QString(),
145 .vbr = 0,
146 .abr = 0,
147 .dims = maintainAspect(QSize(720, 480))
148 });
149
150 if (m_nativeWidth && m_nativeHeight) {
151 addPreset(m_ui.presetNative, {
152 .container = QString(),
153 .vcodec = QString(),
154 .acodec = QString(),
155 .vbr = 0,
156 .abr = 0,
157 .dims = QSize(m_nativeWidth, m_nativeHeight)
158 });
159 m_ui.presetNative->setEnabled(true);
160 }
161
162 addPreset(m_ui.presetHQ, {
163 .container = "MP4",
164 .vcodec = "h.264",
165 .acodec = "AAC",
166 .vbr = 8000,
167 .abr = 384,
168 .dims = maintainAspect(QSize(1920, 1080))
169 });
170
171 addPreset(m_ui.presetYoutube, {
172 .container = "MP4",
173 .vcodec = "h.264",
174 .acodec = "AAC",
175 .vbr = 5000,
176 .abr = 256,
177 .dims = maintainAspect(QSize(1280, 720))
178 });
179
180 addPreset(m_ui.presetWebM, {
181 .container = "WebM",
182 .vcodec = "VP8",
183 .acodec = "Vorbis",
184 .vbr = 800,
185 .abr = 128
186 });
187
188 if (m_nativeWidth && m_nativeHeight) {
189 addPreset(m_ui.presetLossless, {
190 .container = "MKV",
191 .vcodec = "PNG",
192 .acodec = "FLAC",
193 .vbr = 0,
194 .abr = 0,
195 .dims = QSize(m_nativeWidth, m_nativeHeight)
196 });
197 }
198}
199
200VideoView::~VideoView() {
201 stopRecording();
202 free(m_audioCodecCstr);
203 free(m_videoCodecCstr);
204 free(m_containerCstr);
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 * 1000;
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 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