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["mp3"] = "libmp3lame";
49 s_acodecMap["opus"] = "libopus";
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 setAudioCodec(m_ui.audio->currentText());
171 setVideoCodec(m_ui.video->currentText());
172 setAudioBitrate(m_ui.abr->value());
173 setVideoBitrate(m_ui.vbr->value());
174 setContainer(m_ui.container->currentText());
175 setWidth(m_ui.width->value());
176 setHeight(m_ui.height->value());
177
178 showAdvanced(false);
179}
180
181VideoView::~VideoView() {
182 stopRecording();
183 free(m_audioCodecCstr);
184 free(m_videoCodecCstr);
185 free(m_containerCstr);
186}
187
188void VideoView::startRecording() {
189 if (!validateSettings()) {
190 return;
191 }
192 if (!FFmpegEncoderOpen(&m_encoder, m_filename.toLocal8Bit().constData())) {
193 return;
194 }
195 m_ui.start->setEnabled(false);
196 m_ui.stop->setEnabled(true);
197 emit recordingStarted(&m_encoder.d);
198}
199
200void VideoView::stopRecording() {
201 emit recordingStopped();
202 FFmpegEncoderClose(&m_encoder);
203 m_ui.stop->setEnabled(false);
204 validateSettings();
205}
206
207void VideoView::selectFile() {
208 QString filename = QFileDialog::getSaveFileName(this, tr("Select output file"));
209 if (!filename.isEmpty()) {
210 m_ui.filename->setText(filename);
211 }
212}
213
214void VideoView::setFilename(const QString& fname) {
215 m_filename = fname;
216 validateSettings();
217}
218
219void VideoView::setAudioCodec(const QString& codec, bool manual) {
220 free(m_audioCodecCstr);
221 m_audioCodec = sanitizeCodec(codec, s_acodecMap);
222 if (m_audioCodec == "none") {
223 m_audioCodecCstr = nullptr;
224 } else {
225 m_audioCodecCstr = strdup(m_audioCodec.toLocal8Bit().constData());
226 }
227 if (!FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr)) {
228 free(m_audioCodecCstr);
229 m_audioCodecCstr = nullptr;
230 m_audioCodec = QString();
231 }
232 validateSettings();
233 if (manual) {
234 uncheckIncompatible();
235 }
236}
237
238void VideoView::setVideoCodec(const QString& codec, bool manual) {
239 free(m_videoCodecCstr);
240 m_videoCodec = sanitizeCodec(codec, s_vcodecMap);
241 m_videoCodecCstr = strdup(m_videoCodec.toLocal8Bit().constData());
242 if (!FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr)) {
243 free(m_videoCodecCstr);
244 m_videoCodecCstr = nullptr;
245 m_videoCodec = QString();
246 }
247 validateSettings();
248 if (manual) {
249 uncheckIncompatible();
250 }
251}
252
253void VideoView::setContainer(const QString& container, bool manual) {
254 free(m_containerCstr);
255 m_container = sanitizeCodec(container, s_containerMap);
256 m_containerCstr = strdup(m_container.toLocal8Bit().constData());
257 if (!FFmpegEncoderSetContainer(&m_encoder, m_containerCstr)) {
258 free(m_containerCstr);
259 m_containerCstr = nullptr;
260 m_container = QString();
261 }
262 validateSettings();
263 if (manual) {
264 uncheckIncompatible();
265 }
266}
267
268void VideoView::setAudioBitrate(int br, bool manual) {
269 m_abr = br * 1000;
270 FFmpegEncoderSetAudio(&m_encoder, m_audioCodecCstr, m_abr);
271 validateSettings();
272 if (manual) {
273 uncheckIncompatible();
274 }
275}
276
277void VideoView::setVideoBitrate(int br, bool manual) {
278 m_vbr = br * 1000;
279 FFmpegEncoderSetVideo(&m_encoder, m_videoCodecCstr, m_vbr);
280 validateSettings();
281 if (manual) {
282 uncheckIncompatible();
283 }
284}
285
286void VideoView::setWidth(int width, bool manual) {
287 m_width = width;
288 updateAspectRatio(width, 0);
289 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
290 if (manual) {
291 uncheckIncompatible();
292 }
293}
294
295void VideoView::setHeight(int height, bool manual) {
296 m_height = height;
297 updateAspectRatio(0, height);
298 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
299 if (manual) {
300 uncheckIncompatible();
301 }
302}
303
304void VideoView::setAspectWidth(int, bool manual) {
305 updateAspectRatio(0, m_height, true);
306 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
307 if (manual) {
308 uncheckIncompatible();
309 }
310}
311
312void VideoView::setAspectHeight(int, bool manual) {
313 updateAspectRatio(m_width, 0, true);
314 FFmpegEncoderSetDimensions(&m_encoder, m_width, m_height);
315 if (manual) {
316 uncheckIncompatible();
317 }
318}
319
320void VideoView::showAdvanced(bool show) {
321 m_ui.advancedBox->setVisible(show);
322}
323
324bool VideoView::validateSettings() {
325 bool valid = !m_filename.isNull() && !FFmpegEncoderIsOpen(&m_encoder);
326 if (m_audioCodec.isNull()) {
327 valid = false;
328 m_ui.audio->setStyleSheet("QComboBox { color: red; }");
329 } else {
330 m_ui.audio->setStyleSheet("");
331 }
332
333 if (m_videoCodec.isNull()) {
334 valid = false;
335 m_ui.video->setStyleSheet("QComboBox { color: red; }");
336 } else {
337 m_ui.video->setStyleSheet("");
338 }
339
340 if (m_container.isNull()) {
341 valid = false;
342 m_ui.container->setStyleSheet("QComboBox { color: red; }");
343 } else {
344 m_ui.container->setStyleSheet("");
345 }
346
347 // This |valid| check is necessary as if one of the cstrs
348 // is null, the encoder likely has a dangling pointer
349 if (valid && !FFmpegEncoderVerifyContainer(&m_encoder)) {
350 valid = false;
351 }
352
353 m_ui.start->setEnabled(valid);
354
355 return valid;
356}
357
358void VideoView::updateAspectRatio(int width, int height, bool force) {
359 if (m_ui.lockRatio->isChecked() || force) {
360 if (width) {
361 height = m_ui.hratio->value() * width / m_ui.wratio->value();
362 } else if (height) {
363 width = m_ui.wratio->value() * height / m_ui.hratio->value();
364 }
365
366 m_width = width;
367 m_height = height;
368 safelySet(m_ui.width, m_width);
369 safelySet(m_ui.height, m_height);
370 } else {
371 int w = m_width;
372 int h = m_height;
373 // Get greatest common divisor
374 while (w != 0) {
375 int temp = h % w;
376 h = w;
377 w = temp;
378 }
379 int gcd = h;
380 w = m_width / gcd;
381 h = m_height / gcd;
382 safelySet(m_ui.wratio, w);
383 safelySet(m_ui.hratio, h);
384 }
385}
386
387void VideoView::uncheckIncompatible() {
388 Preset current = {
389 .container = m_container,
390 .vcodec = m_videoCodec,
391 .acodec = m_audioCodec,
392 .vbr = m_vbr / 1000,
393 .abr = m_abr / 1000,
394 .width = m_width,
395 .height = m_height
396 };
397
398 m_ui.presets->setExclusive(false);
399 m_ui.resolutions->setExclusive(false);
400 for (auto iterator = m_presets.constBegin(); iterator != m_presets.constEnd(); ++iterator) {
401 Preset next = *iterator;
402 next.container = sanitizeCodec(next.container, s_containerMap);
403 next.acodec = sanitizeCodec(next.acodec, s_acodecMap);
404 next.vcodec = sanitizeCodec(next.vcodec, s_vcodecMap);
405 if (!current.compatible(next)) {
406 safelyCheck(iterator.key(), false);
407 }
408 }
409 m_ui.presets->setExclusive(true);
410 m_ui.resolutions->setExclusive(true);
411
412 if (current.compatible(m_presets[m_ui.preset160])) {
413 safelyCheck(m_ui.preset160);
414 }
415 if (current.compatible(m_presets[m_ui.preset480])) {
416 safelyCheck(m_ui.preset480);
417 }
418 if (current.compatible(m_presets[m_ui.preset720])) {
419 safelyCheck(m_ui.preset720);
420 }
421 if (current.compatible(m_presets[m_ui.preset1080])) {
422 safelyCheck(m_ui.preset1080);
423 }
424}
425
426QString VideoView::sanitizeCodec(const QString& codec, const QMap<QString, QString>& mapping) {
427 QString sanitized = codec.toLower();
428 sanitized = sanitized.remove(QChar('.'));
429 if (mapping.contains(sanitized)) {
430 sanitized = mapping[sanitized];
431 }
432 return sanitized;
433}
434
435void VideoView::safelyCheck(QAbstractButton* button, bool set) {
436 bool signalsBlocked = button->blockSignals(true);
437 bool autoExclusive = button->autoExclusive();
438 button->setAutoExclusive(false);
439 button->setChecked(set);
440 button->setAutoExclusive(autoExclusive);
441 button->blockSignals(signalsBlocked);
442}
443
444void VideoView::safelySet(QSpinBox* box, int value) {
445 bool signalsBlocked = box->blockSignals(true);
446 box->setValue(value);
447 box->blockSignals(signalsBlocked);
448}
449
450void VideoView::safelySet(QComboBox* box, const QString& value) {
451 bool signalsBlocked = box->blockSignals(true);
452 box->lineEdit()->setText(value);
453 box->blockSignals(signalsBlocked);
454}
455
456void VideoView::addPreset(QAbstractButton* button, const Preset& preset) {
457 m_presets[button] = preset;
458 connect(button, &QAbstractButton::pressed, [this, preset]() {
459 setPreset(preset);
460 });
461}
462
463void VideoView::setPreset(const Preset& preset) {
464 if (!preset.container.isNull()) {
465 setContainer(preset.container, false);
466 safelySet(m_ui.container, preset.container);
467 }
468 if (!preset.acodec.isNull()) {
469 setAudioCodec(preset.acodec, false);
470 safelySet(m_ui.audio, preset.acodec);
471 }
472 if (!preset.vcodec.isNull()) {
473 setVideoCodec(preset.vcodec, false);
474 safelySet(m_ui.video, preset.vcodec);
475 }
476 if (preset.abr) {
477 setAudioBitrate(preset.abr, false);
478 safelySet(m_ui.abr, preset.abr);
479 }
480 if (preset.vbr) {
481 setVideoBitrate(preset.vbr, false);
482 safelySet(m_ui.vbr, preset.vbr);
483 }
484 if (preset.width) {
485 setWidth(preset.width, false);
486 safelySet(m_ui.width, preset.width);
487 }
488 if (preset.height) {
489 setHeight(preset.height, false);
490 safelySet(m_ui.height, preset.height);
491 }
492
493 uncheckIncompatible();
494 validateSettings();
495}
496
497#endif