all repos — mgba @ 5f46f126c172d44256b5bd5602f2ece73520c525

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