all repos — mgba @ 7619d8aa67e1ee25d2cc0cf5f1a22ef7e6e98a3e

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