all repos — mgba @ d8ad87504d300af1e5a61affb4ae340f58e29cc9

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.width && width && other.width != width) {
 38		return false;
 39	}
 40	if (other.height && height && other.height != 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{
 52	m_ui.setupUi(this);
 53
 54	if (s_acodecMap.empty()) {
 55		s_acodecMap["mp3"] = "libmp3lame";
 56		s_acodecMap["opus"] = "libopus";
 57		s_acodecMap["uncompressed"] = "pcm_s16le";
 58	}
 59	if (s_vcodecMap.empty()) {
 60		s_vcodecMap["dirac"] = "libschroedinger";
 61		s_vcodecMap["h264"] = "libx264";
 62		s_vcodecMap["hevc"] = "libx265";
 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, SIGNAL(rejected()), this, SLOT(close()));
 73	connect(m_ui.start, SIGNAL(clicked()), this, SLOT(startRecording()));
 74	connect(m_ui.stop, SIGNAL(clicked()), this, SLOT(stopRecording()));
 75
 76	connect(m_ui.selectFile, SIGNAL(clicked()), this, SLOT(selectFile()));
 77	connect(m_ui.filename, SIGNAL(textChanged(const QString&)), this, SLOT(setFilename(const QString&)));
 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, SIGNAL(clicked(bool)), this, SLOT(showAdvanced(bool)));
 96
 97	FFmpegEncoderInit(&m_encoder);
 98
 99	addPreset(m_ui.preset1080, (Preset) {
100		.container = QString(),
101		.vcodec = QString(),
102		.acodec = QString(),
103		.vbr = 0,
104		.abr = 0,
105		.width = 1620,
106		.height = 1080
107	});
108
109	addPreset(m_ui.preset720, (Preset) {
110		.container = QString(),
111		.vcodec = QString(),
112		.acodec = QString(),
113		.vbr = 0,
114		.abr = 0,
115		.width = 1080,
116		.height = 720
117	});
118
119	addPreset(m_ui.preset480, (Preset) {
120		.container = QString(),
121		.vcodec = QString(),
122		.acodec = QString(),
123		.vbr = 0,
124		.abr = 0,
125		.width = 720,
126		.height = 480
127	});
128
129	addPreset(m_ui.preset160, (Preset) {
130		.container = QString(),
131		.vcodec = QString(),
132		.acodec = QString(),
133		.vbr = 0,
134		.abr = 0,
135		.width = VIDEO_HORIZONTAL_PIXELS,
136		.height = VIDEO_VERTICAL_PIXELS
137	});
138
139	addPreset(m_ui.presetHQ, (Preset) {
140		.container = "MP4",
141		.vcodec = "h.264",
142		.acodec = "AAC",
143		.vbr = 8000,
144		.abr = 384,
145		.width = 1620,
146		.height = 1080
147	});
148
149	addPreset(m_ui.presetYoutube, (Preset) {
150		.container = "MP4",
151		.vcodec = "h.264",
152		.acodec = "AAC",
153		.vbr = 5000,
154		.abr = 256,
155		.width = 1080,
156		.height = 720
157	});
158
159	addPreset(m_ui.presetWebM, (Preset) {
160		.container = "WebM",
161		.vcodec = "VP8",
162		.acodec = "Vorbis",
163		.vbr = 800,
164		.abr = 128
165	});
166
167	addPreset(m_ui.presetLossless, (Preset) {
168		.container = "MKV",
169		.vcodec = "PNG",
170		.acodec = "FLAC",
171		.vbr = 0,
172		.abr = 0,
173		.width = VIDEO_HORIZONTAL_PIXELS,
174		.height = VIDEO_VERTICAL_PIXELS,
175	});
176
177	setPreset((Preset) {
178		.container = "MKV",
179		.vcodec = "PNG",
180		.acodec = "FLAC",
181		.vbr = 0,
182		.abr = 0,
183		.width = VIDEO_HORIZONTAL_PIXELS,
184		.height = VIDEO_VERTICAL_PIXELS,
185	});
186
187	showAdvanced(false);
188}
189
190VideoView::~VideoView() {
191	stopRecording();
192	free(m_audioCodecCstr);
193	free(m_videoCodecCstr);
194	free(m_containerCstr);
195}
196
197void VideoView::startRecording() {
198	if (!validateSettings()) {
199		return;
200	}
201	if (!FFmpegEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
202		LOG(QT, ERROR) << tr("Failed to open output video file: %1").arg(m_filename);
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 = GBAApp::app()->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.toUtf8().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.toUtf8().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.toUtf8().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