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