Updated Web Updater binary. Now takes an additional "Checksum" argument.
[LameXP.git] / src / Encoder_Opus.cpp
blob3798393e677d08a854423429d776892d59f58565
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Encoder_Opus.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
30 #include <QUUid>
32 ///////////////////////////////////////////////////////////////////////////////
33 // Encoder Info
34 ///////////////////////////////////////////////////////////////////////////////
36 class OpusEncoderInfo : public AbstractEncoderInfo
38 virtual bool isModeSupported(int mode) const
40 switch(mode)
42 case SettingsModel::VBRMode:
43 case SettingsModel::ABRMode:
44 case SettingsModel::CBRMode:
45 return true;
46 break;
47 default:
48 MUTILS_THROW("Bad RC mode specified!");
52 virtual int valueCount(int mode) const
54 switch(mode)
56 case SettingsModel::VBRMode:
57 case SettingsModel::ABRMode:
58 case SettingsModel::CBRMode:
59 return 32;
60 break;
61 default:
62 MUTILS_THROW("Bad RC mode specified!");
66 virtual int valueAt(int mode, int index) const
68 switch(mode)
70 case SettingsModel::VBRMode:
71 case SettingsModel::ABRMode:
72 case SettingsModel::CBRMode:
73 return qBound(8, (index + 1) * 8, 256);
74 break;
75 default:
76 MUTILS_THROW("Bad RC mode specified!");
80 virtual int valueType(int mode) const
82 switch(mode)
84 case SettingsModel::VBRMode:
85 case SettingsModel::ABRMode:
86 return TYPE_APPROX_BITRATE;
87 break;
88 case SettingsModel::CBRMode:
89 return TYPE_BITRATE;
90 break;
91 default:
92 MUTILS_THROW("Bad RC mode specified!");
96 virtual const char *description(void) const
98 static const char* s_description = "Opus-Tools OpusEnc (libopus)";
99 return s_description;
102 virtual const char *extension(void) const
104 static const char* s_extension = "opus";
105 return s_extension;
108 static const g_opusEncoderInfo;
110 ///////////////////////////////////////////////////////////////////////////////
111 // Encoder implementation
112 ///////////////////////////////////////////////////////////////////////////////
114 OpusEncoder::OpusEncoder(void)
116 m_binary(lamexp_tools_lookup("opusenc.exe"))
118 if(m_binary.isEmpty())
120 MUTILS_THROW("Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!");
123 m_configOptimizeFor = 0;
124 m_configEncodeComplexity = 10;
125 m_configFrameSize = 3;
128 OpusEncoder::~OpusEncoder(void)
132 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
134 QProcess process;
135 QStringList args;
137 switch(m_configRCMode)
139 case SettingsModel::VBRMode:
140 args << "--vbr";
141 break;
142 case SettingsModel::ABRMode:
143 args << "--cvbr";
144 break;
145 case SettingsModel::CBRMode:
146 args << "--hard-cbr";
147 break;
148 default:
149 MUTILS_THROW("Bad rate-control mode!");
150 break;
153 args << "--comp" << QString::number(m_configEncodeComplexity);
155 switch(m_configFrameSize)
157 case 0:
158 args << "--framesize" << "2.5";
159 break;
160 case 1:
161 args << "--framesize" << "5";
162 break;
163 case 2:
164 args << "--framesize" << "10";
165 break;
166 case 3:
167 args << "--framesize" << "20";
168 break;
169 case 4:
170 args << "--framesize" << "40";
171 break;
172 case 5:
173 args << "--framesize" << "60";
174 break;
177 args << QString("--bitrate") << QString::number(qBound(8, (m_configBitrate + 1) * 8, 256));
179 if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
180 if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
181 if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
182 if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
183 if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
184 if(metaInfo.position()) args << "--comment" << QString("tracknumber=%1").arg(QString::number(metaInfo.position()));
185 if(!metaInfo.comment().isEmpty()) args << "--comment" << QString("comment=%1").arg(cleanTag(metaInfo.comment()));
187 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
189 args << QDir::toNativeSeparators(sourceFile);
190 args << QDir::toNativeSeparators(outputFile);
192 if(!startProcess(process, m_binary, args))
194 return false;
197 bool bTimeout = false;
198 bool bAborted = false;
199 int prevProgress = -1;
201 QRegExp regExp("\\((\\d+)%\\)");
203 while(process.state() != QProcess::NotRunning)
205 if(*abortFlag)
207 process.kill();
208 bAborted = true;
209 emit messageLogged("\nABORTED BY USER !!!");
210 break;
212 process.waitForReadyRead(m_processTimeoutInterval);
213 if(!process.bytesAvailable() && process.state() == QProcess::Running)
215 process.kill();
216 qWarning("Opus process timed out <-- killing!");
217 emit messageLogged("\nPROCESS TIMEOUT !!!");
218 bTimeout = true;
219 break;
221 while(process.bytesAvailable() > 0)
223 QByteArray line = process.readLine();
224 QString text = QString::fromUtf8(line.constData()).simplified();
225 if(regExp.lastIndexIn(text) >= 0)
227 bool ok = false;
228 int progress = regExp.cap(1).toInt(&ok);
229 if(ok && (progress > prevProgress))
231 emit statusUpdated(progress);
232 prevProgress = qMin(progress + 2, 99);
235 else if(!text.isEmpty())
237 emit messageLogged(text);
242 process.waitForFinished();
243 if(process.state() != QProcess::NotRunning)
245 process.kill();
246 process.waitForFinished(-1);
249 emit statusUpdated(100);
250 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
252 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
254 return false;
257 return true;
260 void OpusEncoder::setOptimizeFor(int optimizeFor)
262 m_configOptimizeFor = qBound(0, optimizeFor, 2);
265 void OpusEncoder::setEncodeComplexity(int complexity)
267 m_configEncodeComplexity = qBound(0, complexity, 10);
270 void OpusEncoder::setFrameSize(int frameSize)
272 m_configFrameSize = qBound(0, frameSize, 5);
275 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
277 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
279 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
281 return true;
285 return false;
288 const unsigned int *OpusEncoder::supportedChannelCount(void)
290 return NULL;
293 const unsigned int *OpusEncoder::supportedBitdepths(void)
295 static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
296 return supportedBPS;
299 const bool OpusEncoder::needsTimingInfo(void)
301 return true;
304 const AbstractEncoderInfo *OpusEncoder::getEncoderInfo(void)
306 return &g_opusEncoderInfo;