all repos — mgba @ 8137a432339f6f734c4a6674d8b2c4c41fde5ff7

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