all repos — mgba @ 5f59ad4c43b530f0754c883e61c2afa8fdd6c3fb

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