aboutsummaryrefslogtreecommitdiff
path: root/visualization/renderer/qmpvisrendercore.cpp
blob: a2733a707be4aa481e31d1ad8ac8c659568c6aeb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "qmpvisrendercore.hpp"
#include "qmppluginapistub.hpp"
#include "qmpsettingsro.hpp"
#include "qmpmidiplay.hpp"
#include "qmpcorepublic.hpp"

#include <cassert>
#include <dlfcn.h>

#include <QProcess>
#include <QCommandLineParser>
#include <QDebug>
#include <QThread>
qmpVisRenderCore *qmpVisRenderCore::inst=nullptr;

qmpVisRenderCore::qmpVisRenderCore(QCommandLineParser *_clp):QObject(nullptr),clp(_clp)
{
	inst=this;
	player=new CMidiPlayer();
	api=new qmpPluginAPIStub(this);
	msettings=new qmpSettingsRO();
	msettings->registerOptionEnumInt("MIDI","Text encoding","Midi/TextEncoding",{"Unicode","Big5","Big5-HKSCS","CP949","EUC-JP","EUC-KR","GB18030","KOI8-R","KOI8-U","Macintosh","Shift-JIS"},0);
}

bool qmpVisRenderCore::loadVisualizationLibrary()
{
	mp=dlopen("libvisualization.so",RTLD_LAZY);
	if(!mp)
	{
		fprintf(stderr,"failed to load the visualization module!\n");
		return false;
	}
	GetInterface_func getintf=reinterpret_cast<GetInterface_func>(dlsym(mp,"qmpPluginGetInterface"));
	SwitchMode_func switchmode=reinterpret_cast<SwitchMode_func>(dlsym(mp,"switchToRenderMode"));
	vf=nullptr;
	vp=getintf(api);
	switchmode(&qmpVisRenderCore::framefunc,!clp->isSet("show-window"));
	vp->init();
	return true;
}

void qmpVisRenderCore::unloadVisualizationLibrary()
{
	vp->deinit();
	dlclose(mp);
}

void qmpVisRenderCore::loadSettings()
{
	if(clp->isSet("config"))
		msettings->load(clp->value("config").toStdString().c_str());
	for(auto &o:clp->values("option"))
	{
		int sp=o.indexOf('=');
		if(!~sp)
			qDebug("invalid option pair: %s",o.toStdString().c_str());
		QString key=o.left(sp);
		QString value=o.mid(sp+1);
		msettings->setopt(key.toStdString(),value.toStdString());
	}
}

void qmpVisRenderCore::setMIDIFile(const char *url)
{
	player->playerLoadFile(url);
}

void qmpVisRenderCore::startRender()
{
	assert(vf);
	ffmpegproc=new QProcess();
	ffmpegproc->setProgram("ffmpeg");
	QStringList arguments;
	arguments.append(split_arguments(clp->value("ffmpeg-pre-args")));
	arguments
		<<"-f"<<"rawvideo"
		<<"-pixel_format"<<"rgba"
		<<"-video_size"<<QString("%1x%2").arg(msettings->getOptionInt("Visualization/wwidth")).arg(msettings->getOptionInt("Visualization/wheight"))
		<<"-framerate"<<QString::number(msettings->getOptionInt("Visualization/tfps"))
		<<"-i"<<"pipe:";
	arguments.append(split_arguments(clp->value("ffmpeg-args")));
	arguments<<clp->value("output-file");
	ffmpegproc->setArguments(arguments);
	QMetaObject::Connection frameconn=connect(this,&qmpVisRenderCore::frameRendered,this,
	[this,&frameconn](void* px,size_t sz,uint32_t c,uint32_t t)
	{
		if(sz)
		{
			if(!ffmpegproc->isOpen())return;
			ffmpegproc->write(static_cast<const char*>(px),static_cast<qint64>(sz));
			while(ffmpegproc->bytesToWrite()>1<<26)
				ffmpegproc->waitForBytesWritten();
		}
		fprintf(stderr,"Rendered tick %u of %u, %.2f%% done.\r",c,t,100.*c/t);
		if(c>t)
		{
			this->ffmpegproc->closeWriteChannel();
			disconnect(frameconn);
			qApp->exit(0);
		}
	},Qt::ConnectionType::BlockingQueuedConnection);
	connect(ffmpegproc,QOverload<int,QProcess::ExitStatus>::of(&QProcess::finished),
			[this,&frameconn](int x,QProcess::ExitStatus st){
				qDebug("%s",this->ffmpegproc->readAllStandardError().data());
				disconnect(frameconn);
				if(x||st==QProcess::ExitStatus::CrashExit)
					qApp->exit(1);
				else
					qApp->exit(0);
	});
	QMetaObject::invokeMethod(this,[this](){
		ffmpegproc->start();
		vf->show();
		startcb(nullptr,nullptr);
	},Qt::ConnectionType::QueuedConnection);
}

QStringList qmpVisRenderCore::split_arguments(QString a)
{
	QStringList ret;
	QString buf;
	bool escaped=false;
	for(int i=0;i<a.length();++i)
	{
		if(a[i]=='\\')
		{
			if(escaped)
			{
				buf+='\\';
				escaped=false;
			}
			else escaped=true;
		}
		else if(a[i]==' ')
		{
			if(escaped)buf+=' ';
			else
			{
				ret.append(buf);
				buf.clear();
			}
			escaped=false;
		}
		else
		{
			if(escaped)
			{
				buf+='\\';
				escaped=false;
			}
			buf+=a[i];
		}
	}
	if(buf.length())
		ret.append(buf);
	return ret;
}

void qmpVisRenderCore::framefunc(void *px, size_t sz,uint32_t curf,uint32_t totf)
{
	emit inst->frameRendered(px,sz,curf,totf);
}