Bump version.
[LameXP.git] / src / Encoder_Opus.cpp
blobef9e863b058a19300cd5945f0d6e32db14f2b1d8
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()));
186 if(!metaInfo.cover().isEmpty()) args << "--picture" << QDir::toNativeSeparators(metaInfo.cover());
189 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
191 args << QDir::toNativeSeparators(sourceFile);
192 args << QDir::toNativeSeparators(outputFile);
194 if(!startProcess(process, m_binary, args))
196 return false;
199 bool bTimeout = false;
200 bool bAborted = false;
201 int prevProgress = -1;
203 QRegExp regExp("\\((\\d+)%\\)");
205 while(process.state() != QProcess::NotRunning)
207 if(*abortFlag)
209 process.kill();
210 bAborted = true;
211 emit messageLogged("\nABORTED BY USER !!!");
212 break;
214 process.waitForReadyRead(m_processTimeoutInterval);
215 if(!process.bytesAvailable() && process.state() == QProcess::Running)
217 process.kill();
218 qWarning("Opus process timed out <-- killing!");
219 emit messageLogged("\nPROCESS TIMEOUT !!!");
220 bTimeout = true;
221 break;
223 while(process.bytesAvailable() > 0)
225 QByteArray line = process.readLine();
226 QString text = QString::fromUtf8(line.constData()).simplified();
227 if(regExp.lastIndexIn(text) >= 0)
229 bool ok = false;
230 int progress = regExp.cap(1).toInt(&ok);
231 if(ok && (progress > prevProgress))
233 emit statusUpdated(progress);
234 prevProgress = qMin(progress + 2, 99);
237 else if(!text.isEmpty())
239 emit messageLogged(text);
244 process.waitForFinished();
245 if(process.state() != QProcess::NotRunning)
247 process.kill();
248 process.waitForFinished(-1);
251 emit statusUpdated(100);
252 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
254 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
256 return false;
259 return true;
262 void OpusEncoder::setOptimizeFor(int optimizeFor)
264 m_configOptimizeFor = qBound(0, optimizeFor, 2);
267 void OpusEncoder::setEncodeComplexity(int complexity)
269 m_configEncodeComplexity = qBound(0, complexity, 10);
272 void OpusEncoder::setFrameSize(int frameSize)
274 m_configFrameSize = qBound(0, frameSize, 5);
277 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
279 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
281 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
283 return true;
287 return false;
290 const unsigned int *OpusEncoder::supportedChannelCount(void)
292 return NULL;
295 const unsigned int *OpusEncoder::supportedBitdepths(void)
297 static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
298 return supportedBPS;
301 const bool OpusEncoder::needsTimingInfo(void)
303 return true;
306 const AbstractEncoderInfo *OpusEncoder::getEncoderInfo(void)
308 return &g_opusEncoderInfo;