Updated Ukrainian translation.
[LameXP.git] / src / Encoder_Opus.cpp
blobb45dd33d15c03266b076ae75db58410a473fce72
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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 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 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 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 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 static const g_opusEncoderInfo;
104 ///////////////////////////////////////////////////////////////////////////////
105 // Encoder implementation
106 ///////////////////////////////////////////////////////////////////////////////
108 OpusEncoder::OpusEncoder(void)
110 m_binary(lamexp_lookup_tool("opusenc.exe"))
112 if(m_binary.isEmpty())
114 THROW("Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!");
117 m_configOptimizeFor = 0;
118 m_configEncodeComplexity = 10;
119 m_configFrameSize = 3;
122 OpusEncoder::~OpusEncoder(void)
126 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
128 QProcess process;
129 QStringList args;
131 switch(m_configRCMode)
133 case SettingsModel::VBRMode:
134 args << "--vbr";
135 break;
136 case SettingsModel::ABRMode:
137 args << "--cvbr";
138 break;
139 case SettingsModel::CBRMode:
140 args << "--hard-cbr";
141 break;
142 default:
143 THROW("Bad rate-control mode!");
144 break;
147 args << "--comp" << QString::number(m_configEncodeComplexity);
149 switch(m_configFrameSize)
151 case 0:
152 args << "--framesize" << "2.5";
153 break;
154 case 1:
155 args << "--framesize" << "5";
156 break;
157 case 2:
158 args << "--framesize" << "10";
159 break;
160 case 3:
161 args << "--framesize" << "20";
162 break;
163 case 4:
164 args << "--framesize" << "40";
165 break;
166 case 5:
167 args << "--framesize" << "60";
168 break;
171 args << QString("--bitrate") << QString::number(qBound(8, (m_configBitrate + 1) * 8, 256));
173 if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
174 if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
175 if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
176 if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
177 if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
178 if(metaInfo.position()) args << "--comment" << QString("tracknumber=%1").arg(QString::number(metaInfo.position()));
179 if(!metaInfo.comment().isEmpty()) args << "--comment" << QString("comment=%1").arg(cleanTag(metaInfo.comment()));
181 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
183 args << QDir::toNativeSeparators(sourceFile);
184 args << QDir::toNativeSeparators(outputFile);
186 if(!startProcess(process, m_binary, args))
188 return false;
191 bool bTimeout = false;
192 bool bAborted = false;
193 int prevProgress = -1;
195 QRegExp regExp("\\((\\d+)%\\)");
197 while(process.state() != QProcess::NotRunning)
199 if(*abortFlag)
201 process.kill();
202 bAborted = true;
203 emit messageLogged("\nABORTED BY USER !!!");
204 break;
206 process.waitForReadyRead(m_processTimeoutInterval);
207 if(!process.bytesAvailable() && process.state() == QProcess::Running)
209 process.kill();
210 qWarning("Opus process timed out <-- killing!");
211 emit messageLogged("\nPROCESS TIMEOUT !!!");
212 bTimeout = true;
213 break;
215 while(process.bytesAvailable() > 0)
217 QByteArray line = process.readLine();
218 QString text = QString::fromUtf8(line.constData()).simplified();
219 if(regExp.lastIndexIn(text) >= 0)
221 bool ok = false;
222 int progress = regExp.cap(1).toInt(&ok);
223 if(ok && (progress > prevProgress))
225 emit statusUpdated(progress);
226 prevProgress = qMin(progress + 2, 99);
229 else if(!text.isEmpty())
231 emit messageLogged(text);
236 process.waitForFinished();
237 if(process.state() != QProcess::NotRunning)
239 process.kill();
240 process.waitForFinished(-1);
243 emit statusUpdated(100);
244 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
246 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
248 return false;
251 return true;
254 void OpusEncoder::setOptimizeFor(int optimizeFor)
256 m_configOptimizeFor = qBound(0, optimizeFor, 2);
259 void OpusEncoder::setEncodeComplexity(int complexity)
261 m_configEncodeComplexity = qBound(0, complexity, 10);
264 void OpusEncoder::setFrameSize(int frameSize)
266 m_configFrameSize = qBound(0, frameSize, 5);
269 QString OpusEncoder::extension(void)
271 return "opus";
274 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
276 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
278 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
280 return true;
284 return false;
287 const unsigned int *OpusEncoder::supportedChannelCount(void)
289 return NULL;
292 const unsigned int *OpusEncoder::supportedBitdepths(void)
294 static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
295 return supportedBPS;
298 const bool OpusEncoder::needsTimingInfo(void)
300 return true;
303 const AbstractEncoderInfo *OpusEncoder::getEncoderInfo(void)
305 return &g_opusEncoderInfo;