all repos — mgba @ d49df216b84b25f6612200116fc44c8ab325c90a

mGBA Game Boy Advance Emulator

src/platform/qt/VideoView.cpp (view raw)

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