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