all repos — mgba @ 9eb0c374b3459264d3180f59f033cf9490c0e1ea

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