1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Encoder_Opus.h"
25 #include "Model_Settings.h"
31 ///////////////////////////////////////////////////////////////////////////////
33 ///////////////////////////////////////////////////////////////////////////////
35 class OpusEncoderInfo
: public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode
) const
41 case SettingsModel::VBRMode
:
42 case SettingsModel::ABRMode
:
43 case SettingsModel::CBRMode
:
47 throw "Bad RC mode specified!";
51 virtual int valueCount(int mode
) const
55 case SettingsModel::VBRMode
:
56 case SettingsModel::ABRMode
:
57 case SettingsModel::CBRMode
:
61 throw "Bad RC mode specified!";
65 virtual int valueAt(int mode
, int index
) const
69 case SettingsModel::VBRMode
:
70 case SettingsModel::ABRMode
:
71 case SettingsModel::CBRMode
:
72 return qBound(8, (index
+ 1) * 8, 256);
75 throw "Bad RC mode specified!";
79 virtual int valueType(int mode
) const
83 case SettingsModel::VBRMode
:
84 case SettingsModel::ABRMode
:
85 return TYPE_APPROX_BITRATE
;
87 case SettingsModel::CBRMode
:
91 throw "Bad RC mode specified!";
95 virtual const char *description(void) const
97 static const char* s_description
= "Opus-Tools OpusEnc (libopus)";
101 static const g_opusEncoderInfo
;
103 ///////////////////////////////////////////////////////////////////////////////
104 // Encoder implementation
105 ///////////////////////////////////////////////////////////////////////////////
107 OpusEncoder::OpusEncoder(void)
109 m_binary(lamexp_lookup_tool("opusenc.exe"))
111 if(m_binary
.isEmpty())
113 throw "Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!";
116 m_configOptimizeFor
= 0;
117 m_configEncodeComplexity
= 10;
118 m_configFrameSize
= 3;
121 OpusEncoder::~OpusEncoder(void)
125 bool OpusEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
127 const unsigned int fileDuration
= metaInfo
.fileDuration();
132 switch(m_configRCMode
)
134 case SettingsModel::VBRMode
:
137 case SettingsModel::ABRMode
:
140 case SettingsModel::CBRMode
:
141 args
<< "--hard-cbr";
144 throw "Bad rate-control mode!";
148 args
<< "--comp" << QString::number(m_configEncodeComplexity
);
150 switch(m_configFrameSize
)
153 args
<< "--framesize" << "2.5";
156 args
<< "--framesize" << "5";
159 args
<< "--framesize" << "10";
162 args
<< "--framesize" << "20";
165 args
<< "--framesize" << "40";
168 args
<< "--framesize" << "60";
172 args
<< QString("--bitrate") << QString::number(qBound(8, (m_configBitrate
+ 1) * 8, 256));
174 if(!metaInfo
.fileName().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.fileName());
175 if(!metaInfo
.fileArtist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.fileArtist());
176 if(!metaInfo
.fileAlbum().isEmpty()) args
<< "--album" << cleanTag(metaInfo
.fileAlbum());
177 if(!metaInfo
.fileGenre().isEmpty()) args
<< "--genre" << cleanTag(metaInfo
.fileGenre());
178 if(metaInfo
.fileYear()) args
<< "--date" << QString::number(metaInfo
.fileYear());
179 if(metaInfo
.filePosition()) args
<< "--comment" << QString("tracknumber=%1").arg(QString::number(metaInfo
.filePosition()));
180 if(!metaInfo
.fileComment().isEmpty()) args
<< "--comment" << QString("comment=%1").arg(cleanTag(metaInfo
.fileComment()));
182 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
184 args
<< QDir::toNativeSeparators(sourceFile
);
185 args
<< QDir::toNativeSeparators(outputFile
);
187 if(!startProcess(process
, m_binary
, args
))
192 bool bTimeout
= false;
193 bool bAborted
= false;
194 int prevProgress
= -1;
196 QRegExp
regExp("\\((\\d+)%\\)");
198 while(process
.state() != QProcess::NotRunning
)
204 emit
messageLogged("\nABORTED BY USER !!!");
207 process
.waitForReadyRead(m_processTimeoutInterval
);
208 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
211 qWarning("Opus process timed out <-- killing!");
212 emit
messageLogged("\nPROCESS TIMEOUT !!!");
216 while(process
.bytesAvailable() > 0)
218 QByteArray line
= process
.readLine();
219 QString text
= QString::fromUtf8(line
.constData()).simplified();
220 if(regExp
.lastIndexIn(text
) >= 0)
223 int progress
= regExp
.cap(1).toInt(&ok
);
224 if(ok
&& (progress
> prevProgress
))
226 emit
statusUpdated(progress
);
227 prevProgress
= qMin(progress
+ 2, 99);
230 else if(!text
.isEmpty())
232 emit
messageLogged(text
);
237 process
.waitForFinished();
238 if(process
.state() != QProcess::NotRunning
)
241 process
.waitForFinished(-1);
244 emit
statusUpdated(100);
245 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
247 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
255 void OpusEncoder::setOptimizeFor(int optimizeFor
)
257 m_configOptimizeFor
= qBound(0, optimizeFor
, 2);
260 void OpusEncoder::setEncodeComplexity(int complexity
)
262 m_configEncodeComplexity
= qBound(0, complexity
, 10);
265 void OpusEncoder::setFrameSize(int frameSize
)
267 m_configFrameSize
= qBound(0, frameSize
, 5);
270 QString
OpusEncoder::extension(void)
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)
288 const unsigned int *OpusEncoder::supportedChannelCount(void)
293 const unsigned int *OpusEncoder::supportedBitdepths(void)
295 static const unsigned int supportedBPS
[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32
, NULL
};
299 const bool OpusEncoder::needsTimingInfo(void)
304 const AbstractEncoderInfo
*OpusEncoder::getEncoderInfo(void)
306 return &g_opusEncoderInfo
;