all repos — mgba @ ec626d723fbb4716a697ad3cac0b0b29943dba00

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