1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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 static const g_aacEncoderInfo
;
114 ///////////////////////////////////////////////////////////////////////////////
115 // Encoder implementation
116 ///////////////////////////////////////////////////////////////////////////////
118 AACEncoder::AACEncoder(void)
120 m_binary_enc(lamexp_tools_lookup("neroAacEnc.exe")),
121 m_binary_tag(lamexp_tools_lookup("neroAacTag.exe")),
122 m_binary_sox(lamexp_tools_lookup("sox.exe"))
124 if(m_binary_enc
.isEmpty() || m_binary_tag
.isEmpty() || m_binary_sox
.isEmpty())
126 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
130 m_configEnable2Pass
= true;
133 AACEncoder::~AACEncoder(void)
137 bool AACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
141 const QString baseName
= QFileInfo(outputFile
).fileName();
143 switch(m_configRCMode
)
145 case SettingsModel::VBRMode
:
146 args
<< "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate
* 5, 100)) / 100.0);
148 case SettingsModel::ABRMode
:
149 args
<< "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
151 case SettingsModel::CBRMode
:
152 args
<< "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
155 MUTILS_THROW("Bad rate-control mode!");
159 if(m_configEnable2Pass
&& (m_configRCMode
== SettingsModel::ABRMode
))
164 switch(m_configProfile
)
167 args
<< "-lc"; //Forces use of LC AAC profile
170 args
<< "-he"; //Forces use of HE AAC profile
173 args
<< "-hev2"; //Forces use of HEv2 AAC profile
177 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
179 args
<< "-if" << QDir::toNativeSeparators(sourceFile
);
180 args
<< "-of" << QDir::toNativeSeparators(outputFile
);
182 if(!startProcess(process
, m_binary_enc
, args
))
187 bool bTimeout
= false;
188 bool bAborted
= false;
189 int prevProgress
= -1;
192 QRegExp
regExp("Processed\\s+(\\d+)\\s+seconds");
193 QRegExp
regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
194 QRegExp
regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
196 while(process
.state() != QProcess::NotRunning
)
202 emit
messageLogged("\nABORTED BY USER !!!");
205 process
.waitForReadyRead(m_processTimeoutInterval
);
206 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
209 qWarning("NeroAacEnc process timed out <-- killing!");
210 emit
messageLogged("\nPROCESS TIMEOUT !!!");
214 while(process
.bytesAvailable() > 0)
216 QByteArray line
= process
.readLine();
217 QString text
= QString::fromUtf8(line
.constData()).simplified();
218 if(regExp_pass1
.lastIndexIn(text
) >= 0)
221 int progress
= regExp_pass1
.cap(1).toInt(&ok
);
222 if(ok
&& (duration
> 0))
224 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0);
225 if(newProgress
> prevProgress
)
227 emit
statusUpdated(newProgress
);
228 prevProgress
= qMin(newProgress
+ 2, 99);
232 else if(regExp_pass2
.lastIndexIn(text
) >= 0)
235 int progress
= regExp_pass2
.cap(1).toInt(&ok
);
236 if(ok
&& (duration
> 0))
238 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0) + 50;
239 if(newProgress
> prevProgress
)
241 emit
statusUpdated(newProgress
);
242 prevProgress
= qMin(newProgress
+ 2, 99);
246 else if(regExp
.lastIndexIn(text
) >= 0)
249 int progress
= regExp
.cap(1).toInt(&ok
);
250 if(ok
&& (duration
> 0))
252 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 100.0);
253 if(newProgress
> prevProgress
)
255 emit
statusUpdated(newProgress
);
256 prevProgress
= qMin(newProgress
+ 2, 99);
260 else if(!text
.isEmpty())
262 emit
messageLogged(text
);
267 process
.waitForFinished();
268 if(process
.state() != QProcess::NotRunning
)
271 process
.waitForFinished(-1);
274 emit
statusUpdated(100);
275 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
277 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
282 emit
messageLogged("\n-------------------------------\n");
285 args
<< QDir::toNativeSeparators(outputFile
);
287 if(!metaInfo
.title().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.title()));
288 if(!metaInfo
.artist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.artist()));
289 if(!metaInfo
.album().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.album()));
290 if(!metaInfo
.genre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.genre()));
291 if(!metaInfo
.comment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.comment()));
292 if(metaInfo
.year()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.year()));
293 if(metaInfo
.position()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.position()));
294 if(!metaInfo
.cover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.cover());
296 if(!startProcess(process
, m_binary_tag
, args
))
303 while(process
.state() != QProcess::NotRunning
)
309 emit
messageLogged("\nABORTED BY USER !!!");
312 process
.waitForReadyRead(m_processTimeoutInterval
);
313 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
316 qWarning("NeroAacTag process timed out <-- killing!");
317 emit
messageLogged("\nPROCESS TIMEOUT !!!");
321 while(process
.bytesAvailable() > 0)
323 QByteArray line
= process
.readLine();
324 QString text
= QString::fromUtf8(line
.constData()).simplified();
327 emit
messageLogged(text
);
332 process
.waitForFinished();
333 if(process
.state() != QProcess::NotRunning
)
336 process
.waitForFinished(-1);
339 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
341 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
349 QString
AACEncoder::extension(void)
354 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
356 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
358 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
367 void AACEncoder::setProfile(int profile
)
369 m_configProfile
= profile
;
372 void AACEncoder::setEnable2Pass(bool enabled
)
374 m_configEnable2Pass
= enabled
;
377 const bool AACEncoder::needsTimingInfo(void)
382 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
384 return &g_aacEncoderInfo
;