all repos — mgba @ 8f1148498e12197745f62e477d9b8e07382cc72e

mGBA Game Boy Advance Emulator

src/platform/qt/GIFView.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 "GIFView.h"
  7
  8#ifdef USE_FFMPEG
  9
 10#include "CoreController.h"
 11#include "GBAApp.h"
 12#include "LogController.h"
 13
 14#include <QMap>
 15
 16using namespace QGBA;
 17
 18GIFView::GIFView(QWidget* parent)
 19	: QWidget(parent)
 20{
 21	m_ui.setupUi(this);
 22
 23	connect(m_ui.start, &QAbstractButton::clicked, this, &GIFView::startRecording);
 24	connect(m_ui.stop, &QAbstractButton::clicked, this, &GIFView::stopRecording);
 25
 26	connect(m_ui.selectFile, &QAbstractButton::clicked, this, &GIFView::selectFile);
 27	connect(m_ui.filename, &QLineEdit::textChanged, this, &GIFView::setFilename);
 28	connect(m_ui.fmtGif, &QAbstractButton::clicked, this, &GIFView::changeExtension);
 29	connect(m_ui.fmtApng, &QAbstractButton::clicked, this, &GIFView::changeExtension);
 30
 31	FFmpegEncoderInit(&m_encoder);
 32	FFmpegEncoderSetAudio(&m_encoder, nullptr, 0);
 33}
 34
 35GIFView::~GIFView() {
 36	stopRecording();
 37}
 38
 39void GIFView::setController(std::shared_ptr<CoreController> controller) {
 40	connect(controller.get(), &CoreController::stopping, this, &GIFView::stopRecording);
 41	connect(this, &GIFView::recordingStarted, controller.get(), &CoreController::setAVStream);
 42	connect(this, &GIFView::recordingStopped, controller.get(), &CoreController::clearAVStream, Qt::DirectConnection);
 43	QSize size(controller->screenDimensions());
 44	FFmpegEncoderSetDimensions(&m_encoder, size.width(), size.height());
 45}
 46
 47void GIFView::startRecording() {
 48	if (m_ui.fmtApng->isChecked()) {
 49		FFmpegEncoderSetContainer(&m_encoder, "apng");
 50		FFmpegEncoderSetVideo(&m_encoder, "apng", 0, m_ui.frameskip->value());
 51	} else {
 52		FFmpegEncoderSetContainer(&m_encoder, "gif");
 53		FFmpegEncoderSetVideo(&m_encoder, "gif", 0, m_ui.frameskip->value());
 54	}
 55	FFmpegEncoderSetLooping(&m_encoder, m_ui.loop->isChecked());
 56	if (!FFmpegEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
 57		LOG(QT, ERROR) << tr("Failed to open output GIF or APNG file: %1").arg(m_filename);
 58		return;
 59	}
 60	m_ui.start->setEnabled(false);
 61	m_ui.stop->setEnabled(true);
 62	m_ui.frameskip->setEnabled(false);
 63	m_ui.loop->setEnabled(false);
 64	m_ui.fmtApng->setEnabled(false);
 65	m_ui.fmtGif->setEnabled(false);
 66	emit recordingStarted(&m_encoder.d);
 67}
 68
 69void GIFView::stopRecording() {
 70	emit recordingStopped();
 71	FFmpegEncoderClose(&m_encoder);
 72	m_ui.stop->setEnabled(false);
 73	m_ui.start->setEnabled(!m_filename.isEmpty());
 74	m_ui.frameskip->setEnabled(true);
 75	m_ui.loop->setEnabled(true);
 76	m_ui.fmtApng->setEnabled(true);
 77	m_ui.fmtGif->setEnabled(true);
 78}
 79
 80void GIFView::selectFile() {
 81	QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif);;Animated Portable Network Graphics (*.png *.apng)"));
 82	m_ui.filename->setText(filename);
 83}
 84
 85void GIFView::setFilename(const QString& filename) {
 86	m_filename = filename;
 87	if (!FFmpegEncoderIsOpen(&m_encoder)) {
 88		m_ui.start->setEnabled(!filename.isEmpty());
 89		if (filename.endsWith(".gif")) {
 90			m_ui.fmtGif->setChecked(Qt::Checked);
 91		} else if (filename.endsWith(".png") || filename.endsWith(".apng")) {
 92			m_ui.fmtApng->setChecked(Qt::Checked);
 93		}
 94	}
 95}
 96
 97void GIFView::changeExtension() {
 98	if (m_filename.isEmpty()) {
 99		return;
100	}
101	QString filename = m_filename;
102	int index = m_filename.lastIndexOf(".");
103	if (index >= 0) {
104		filename.truncate(index);
105	}
106	if (m_ui.fmtGif->isChecked()) {
107		filename += ".gif";
108	} else if (m_ui.fmtApng->isChecked()) {
109		filename += ".png";
110	}
111	m_ui.filename->setText(filename);
112}
113
114#endif