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