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