all repos — mgba @ 5988b4ad2ed10c4193307816d0d07ee96eeb9ee8

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