all repos — mgba @ 1a0e44c014ad34bea30d237d27015d82d02cda4b

mGBA Game Boy Advance Emulator

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