1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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, 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_AAC.h"
26 #include "Model_Settings.h"
31 static int index2bitrate(const int index
)
33 return (index
< 32) ? ((index
+ 1) * 8) : ((index
- 15) * 16);
36 ///////////////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////////
40 class AACEncoderInfo
: public AbstractEncoderInfo
42 virtual bool isModeSupported(int mode
) const
46 case SettingsModel::VBRMode
:
47 case SettingsModel::ABRMode
:
48 case SettingsModel::CBRMode
:
52 MUTILS_THROW("Bad RC mode specified!");
56 virtual int valueCount(int mode
) const
60 case SettingsModel::VBRMode
:
63 case SettingsModel::ABRMode
:
64 case SettingsModel::CBRMode
:
68 MUTILS_THROW("Bad RC mode specified!");
72 virtual int valueAt(int mode
, int index
) const
76 case SettingsModel::VBRMode
:
77 return qBound(0, index
* 5, 100);
79 case SettingsModel::ABRMode
:
80 case SettingsModel::CBRMode
:
81 return qBound(8, index2bitrate(index
), 400);
84 MUTILS_THROW("Bad RC mode specified!");
88 virtual int valueType(int mode
) const
92 case SettingsModel::VBRMode
:
93 return TYPE_QUALITY_LEVEL_FLT
;
95 case SettingsModel::ABRMode
:
96 return TYPE_APPROX_BITRATE
;
98 case SettingsModel::CBRMode
:
102 MUTILS_THROW("Bad RC mode specified!");
106 virtual const char *description(void) const
108 static const char* s_description
= "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
109 return s_description
;
112 virtual const char *extension(void) const
114 static const char* s_extension
= "mp4";
118 virtual bool isResamplingSupported(void) const
123 static const g_aacEncoderInfo
;
125 ///////////////////////////////////////////////////////////////////////////////
126 // Encoder implementation
127 ///////////////////////////////////////////////////////////////////////////////
129 AACEncoder::AACEncoder(void)
131 m_binary_enc(lamexp_tools_lookup("neroAacEnc.exe")),
132 m_binary_tag(lamexp_tools_lookup("neroAacTag.exe")),
133 m_binary_sox(lamexp_tools_lookup("sox.exe"))
135 if(m_binary_enc
.isEmpty() || m_binary_tag
.isEmpty() || m_binary_sox
.isEmpty())
137 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
141 m_configEnable2Pass
= true;
144 AACEncoder::~AACEncoder(void)
148 bool AACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
152 const QString baseName
= QFileInfo(outputFile
).fileName();
154 switch(m_configRCMode
)
156 case SettingsModel::VBRMode
:
157 args
<< "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate
* 5, 100)) / 100.0);
159 case SettingsModel::ABRMode
:
160 args
<< "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
162 case SettingsModel::CBRMode
:
163 args
<< "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
166 MUTILS_THROW("Bad rate-control mode!");
170 if(m_configEnable2Pass
&& (m_configRCMode
== SettingsModel::ABRMode
))
175 switch(m_configProfile
)
178 args
<< "-lc"; //Forces use of LC AAC profile
181 args
<< "-he"; //Forces use of HE AAC profile
184 args
<< "-hev2"; //Forces use of HEv2 AAC profile
188 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
190 args
<< "-if" << QDir::toNativeSeparators(sourceFile
);
191 args
<< "-of" << QDir::toNativeSeparators(outputFile
);
193 if(!startProcess(process
, m_binary_enc
, args
))
198 bool bTimeout
= false;
199 bool bAborted
= false;
200 int prevProgress
= -1;
203 QRegExp
regExp("Processed\\s+(\\d+)\\s+seconds");
204 QRegExp
regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
205 QRegExp
regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
207 while(process
.state() != QProcess::NotRunning
)
213 emit
messageLogged("\nABORTED BY USER !!!");
216 process
.waitForReadyRead(m_processTimeoutInterval
);
217 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
220 qWarning("NeroAacEnc process timed out <-- killing!");
221 emit
messageLogged("\nPROCESS TIMEOUT !!!");
225 while(process
.bytesAvailable() > 0)
227 QByteArray line
= process
.readLine();
228 QString text
= QString::fromUtf8(line
.constData()).simplified();
229 if(regExp_pass1
.lastIndexIn(text
) >= 0)
232 int progress
= regExp_pass1
.cap(1).toInt(&ok
);
233 if(ok
&& (duration
> 0))
235 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0);
236 if(newProgress
> prevProgress
)
238 emit
statusUpdated(newProgress
);
239 prevProgress
= qMin(newProgress
+ 2, 99);
243 else if(regExp_pass2
.lastIndexIn(text
) >= 0)
246 int progress
= regExp_pass2
.cap(1).toInt(&ok
);
247 if(ok
&& (duration
> 0))
249 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0) + 50;
250 if(newProgress
> prevProgress
)
252 emit
statusUpdated(newProgress
);
253 prevProgress
= qMin(newProgress
+ 2, 99);
257 else if(regExp
.lastIndexIn(text
) >= 0)
260 int progress
= regExp
.cap(1).toInt(&ok
);
261 if(ok
&& (duration
> 0))
263 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 100.0);
264 if(newProgress
> prevProgress
)
266 emit
statusUpdated(newProgress
);
267 prevProgress
= qMin(newProgress
+ 2, 99);
271 else if(!text
.isEmpty())
273 emit
messageLogged(text
);
278 process
.waitForFinished();
279 if(process
.state() != QProcess::NotRunning
)
282 process
.waitForFinished(-1);
285 emit
statusUpdated(100);
286 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
288 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
293 if(metaInfo
.empty(false))
298 emit
messageLogged("\n-------------------------------\n");
301 args
<< QDir::toNativeSeparators(outputFile
);
303 if(!metaInfo
.title().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.title()));
304 if(!metaInfo
.artist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.artist()));
305 if(!metaInfo
.album().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.album()));
306 if(!metaInfo
.genre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.genre()));
307 if(!metaInfo
.comment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.comment()));
308 if(metaInfo
.year()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.year()));
309 if(metaInfo
.position()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.position()));
310 if(!metaInfo
.cover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.cover());
312 if(!startProcess(process
, m_binary_tag
, args
))
319 while(process
.state() != QProcess::NotRunning
)
325 emit
messageLogged("\nABORTED BY USER !!!");
328 process
.waitForReadyRead(m_processTimeoutInterval
);
329 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
332 qWarning("NeroAacTag process timed out <-- killing!");
333 emit
messageLogged("\nPROCESS TIMEOUT !!!");
337 while(process
.bytesAvailable() > 0)
339 QByteArray line
= process
.readLine();
340 QString text
= QString::fromUtf8(line
.constData()).simplified();
343 emit
messageLogged(text
);
348 process
.waitForFinished();
349 if(process
.state() != QProcess::NotRunning
)
352 process
.waitForFinished(-1);
355 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
357 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
365 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
367 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
369 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
378 void AACEncoder::setProfile(int profile
)
380 m_configProfile
= profile
;
383 void AACEncoder::setEnable2Pass(bool enabled
)
385 m_configEnable2Pass
= enabled
;
388 const bool AACEncoder::needsTimingInfo(void)
393 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
395 return &g_aacEncoderInfo
;