all repos — mgba @ 6ae02d05536568c6139b201c72db41a126451235

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::selectFile() {
252	QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"));
253	if (!filename.isEmpty()) {
254		m_ui.filename->setText(filename);
255	}
256}
257
258void VideoView::setFilename(const QString& fname) {
259	m_filename = fname;
260	validateSettings();
261}
262
263void VideoView::setAudioCodec(const QString& codec, bool manual) {
264	free(m_audioCodecCstr);
265	m_audioCodec = sanitizeCodec(codec, s_acodecMap);
266	if (m_audioCodec == "none") {
267		m_audioCodecCstr = nullptr;
268	} else {
269		m_audioCodecCstr = strdup(m_audioCodec.toUtf8().constData());
270	}
271	if (!FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr)) {
272		free(m_audioCodecCstr);
273		m_audioCodecCstr = nullptr;
274		m_audioCodec = QString();
275	}
276	validateSettings();
277	if (manual) {
278		uncheckIncompatible();
279	}
280}
281
282void VideoView::setVideoCodec(const QString& codec, bool manual) {
283	free(m_videoCodecCstr);
284	m_videoCodec = sanitizeCodec(codec, s_vcodecMap);
285	if (m_videoCodec == "none") {
286		m_videoCodecCstr = nullptr;
287	} else {
288		m_videoCodecCstr = strdup(m_videoCodec.toUtf8().constData());
289	}
290	if (!FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr, 0)) {
291		free(m_videoCodecCstr);
292		m_videoCodecCstr = nullptr;
293		m_videoCodec = QString();
294	}
295	validateSettings();
296	if (manual) {
297		uncheckIncompatible();
298	}
299}
300
301void VideoView::setContainer(const QString& container, bool manual) {
302	free(m_containerCstr);
303	m_container = sanitizeCodec(container, s_containerMap);
304	m_containerCstr = strdup(m_container.toUtf8().constData());
305	if (!FFmpegEncoderSetContainer(&m_encoder, m_containerCstr)) {
306		free(m_containerCstr);
307		m_containerCstr = nullptr;
308		m_container = QString();
309	}
310	validateSettings();
311	if (manual) {
312		uncheckIncompatible();
313	}
314}
315
316void VideoView::setAudioBitrate(int br, bool manual) {
317	m_abr = br * 1000;
318	FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr);
319	validateSettings();
320	if (manual) {
321		uncheckIncompatible();
322	}
323}
324
325void VideoView::setVideoBitrate(int br, bool manual) {
326	m_vbr = br >= 0 ? br * 1000 : 0;
327	FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr, 0);
328	validateSettings();
329	if (manual) {
330		uncheckIncompatible();
331	}
332}
333
334void VideoView::setWidth(int width, bool manual) {
335	m_width = width;
336	updateAspectRatio(width, 0, false);
337	FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
338	if (manual) {
339		uncheckIncompatible();
340	}
341}
342
343void VideoView::setHeight(int height, bool manual) {
344	m_height = height;
345	updateAspectRatio(0, height, false);
346	FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
347	if (manual) {
348		uncheckIncompatible();
349	}
350}
351
352void VideoView::setAspectWidth(int, bool manual) {
353	updateAspectRatio(0, m_height, true);
354	FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
355	if (manual) {
356		uncheckIncompatible();
357	}
358}
359
360void VideoView::setAspectHeight(int, bool manual) {
361	updateAspectRatio(m_width, 0, true);
362	FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
363	if (manual) {
364		uncheckIncompatible();
365	}
366}
367
368void VideoView::showAdvanced(bool show) {
369	m_ui.advancedBox->setVisible(show);
370}
371
372bool VideoView::validateSettings() {
373	bool valid = !m_filename.isNull() && !FFmpegEncoderIsOpen(&m_encoder);
374	if (m_audioCodec.isNull()) {
375		valid = false;
376		m_ui.audio->setStyleSheet("QComboBox { color: red; }");
377	} else {
378		m_ui.audio->setStyleSheet("");
379	}
380
381	if (m_videoCodec.isNull()) {
382		valid = false;
383		m_ui.video->setStyleSheet("QComboBox { color: red; }");
384	} else {
385		m_ui.video->setStyleSheet("");
386	}
387
388	if (m_container.isNull()) {
389		valid = false;
390		m_ui.container->setStyleSheet("QComboBox { color: red; }");
391	} else {
392		m_ui.container->setStyleSheet("");
393	}
394
395	// This |valid| check is necessary as if one of the cstrs
396	// is null, the encoder likely has a dangling pointer
397	if (valid && !FFmpegEncoderVerifyContainer(&m_encoder)) {
398		valid = false;
399	}
400
401	m_ui.start->setEnabled(valid);
402
403	return valid;
404}
405
406void VideoView::updateAspectRatio(int width, int height, bool force) {
407	if (m_ui.lockRatio->isChecked() || force) {
408		if (width) {
409			height = m_ui.hratio->value() * width / m_ui.wratio->value();
410		} else if (height) {
411			width = m_ui.wratio->value() * height / m_ui.hratio->value();
412		}
413
414		m_width = width;
415		m_height = height;
416		safelySet(m_ui.width, m_width);
417		safelySet(m_ui.height, m_height);
418	} else {
419		int w = m_width;
420		int h = m_height;
421		// Get greatest common divisor
422		while (w != 0) {
423			int temp = h % w;
424			h = w;
425			w = temp;
426		}
427		int gcd = h;
428		w = m_width / gcd;
429		h = m_height / gcd;
430		safelySet(m_ui.wratio, w);
431		safelySet(m_ui.hratio, h);
432	}
433}
434
435void VideoView::uncheckIncompatible() {
436	Preset current = {
437		.container = m_container,
438		.vcodec = m_videoCodec,
439		.acodec = m_audioCodec,
440		.vbr = m_vbr / 1000,
441		.abr = m_abr / 1000,
442		.dims = QSize(m_width, m_height)
443	};
444
445	m_ui.presets->setExclusive(false);
446	m_ui.resolutions->setExclusive(false);
447	for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
448		Preset next = *iterator;
449		next.container = sanitizeCodec(next.container, s_containerMap);
450		next.acodec = sanitizeCodec(next.acodec, s_acodecMap);
451		next.vcodec = sanitizeCodec(next.vcodec, s_vcodecMap);
452		if (!current.compatible(next)) {
453			safelyCheck(iterator.key(), false);
454		}
455	}
456	m_ui.presets->setExclusive(true);
457	m_ui.resolutions->setExclusive(true);
458
459	if (current.compatible(m_presets[m_ui.presetNative])) {
460		safelyCheck(m_ui.presetNative);
461	}
462	if (current.compatible(m_presets[m_ui.preset480])) {
463		safelyCheck(m_ui.preset480);
464	}
465	if (current.compatible(m_presets[m_ui.preset720])) {
466		safelyCheck(m_ui.preset720);
467	}
468	if (current.compatible(m_presets[m_ui.preset1080])) {
469		safelyCheck(m_ui.preset1080);
470	}
471}
472
473QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
474	QString sanitized = codec.toLower();
475	sanitized = sanitized.remove(QChar('.'));
476	sanitized = sanitized.remove(QChar('('));
477	sanitized = sanitized.remove(QChar(')'));
478	if (mapping.contains(sanitized)) {
479		sanitized = mapping[sanitized];
480	}
481	return sanitized;
482}
483
484void VideoView::safelyCheck(QAbstractButton* button, bool set) {
485	bool signalsBlocked = button->blockSignals(true);
486	bool autoExclusive = button->autoExclusive();
487	button->setAutoExclusive(false);
488	button->setChecked(set);
489	button->setAutoExclusive(autoExclusive);
490	button->blockSignals(signalsBlocked);
491}
492
493void VideoView::safelySet(QSpinBox* box, int value) {
494	bool signalsBlocked = box->blockSignals(true);
495	box->setValue(value);
496	box->blockSignals(signalsBlocked);
497}
498
499void VideoView::safelySet(QComboBox* box, const QString& value) {
500	bool signalsBlocked = box->blockSignals(true);
501	box->lineEdit()->setText(value);
502	box->blockSignals(signalsBlocked);
503}
504
505void VideoView::addPreset(QAbstractButton* button, const Preset& preset) {
506	m_presets[button] = preset;
507	button->disconnect();
508	connect(button, &QAbstractButton::pressed, [this, preset]() {
509		setPreset(preset);
510	});
511}
512
513void VideoView::setPreset(const Preset& preset) {
514	if (!preset.container.isNull()) {
515		setContainer(preset.container, false);
516		safelySet(m_ui.container, preset.container);
517	}
518	if (!preset.acodec.isNull()) {
519		setAudioCodec(preset.acodec, false);
520		safelySet(m_ui.audio, preset.acodec);
521	}
522	if (!preset.vcodec.isNull()) {
523		setVideoCodec(preset.vcodec, false);
524		safelySet(m_ui.video, preset.vcodec);
525	}
526	if (preset.abr) {
527		setAudioBitrate(preset.abr, false);
528		safelySet(m_ui.abr, preset.abr);
529	}
530	if (preset.vbr) {
531		setVideoBitrate(preset.vbr, false);
532		safelySet(m_ui.vbr, preset.vbr);
533	}
534	if (preset.dims.width() > 0) {
535		setWidth(preset.dims.width(), false);
536		safelySet(m_ui.width, preset.dims.width());
537	}
538	if (preset.dims.height() > 0) {
539		setHeight(preset.dims.height(), false);
540		safelySet(m_ui.height, preset.dims.height());
541	}
542
543	uncheckIncompatible();
544	validateSettings();
545}
546
547QSize VideoView::maintainAspect(const QSize& size) {
548	QSize ds = size;
549	if (ds.width() * m_nativeHeight > ds.height() * m_nativeWidth) {
550		ds.setWidth(ds.height() * m_nativeWidth / m_nativeHeight);
551	} else if (ds.width() * m_nativeHeight < ds.height() * m_nativeWidth) {
552		ds.setHeight(ds.width() * m_nativeHeight / m_nativeWidth);
553	}
554	return ds;
555}
556
557#endif