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_AAC.h"
25 #include "Model_Settings.h"
30 static int index2bitrate(const int index
)
32 return (index
< 32) ? ((index
+ 1) * 8) : ((index
- 15) * 16);
35 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
39 class AACEncoderInfo
: public AbstractEncoderInfo
41 virtual bool isModeSupported(int mode
) const
45 case SettingsModel::VBRMode
:
46 case SettingsModel::ABRMode
:
47 case SettingsModel::CBRMode
:
51 throw "Bad RC mode specified!";
55 virtual int valueCount(int mode
) const
59 case SettingsModel::VBRMode
:
62 case SettingsModel::ABRMode
:
63 case SettingsModel::CBRMode
:
67 throw "Bad RC mode specified!";
71 virtual int valueAt(int mode
, int index
) const
75 case SettingsModel::VBRMode
:
76 return qBound(0, index
* 5, 100);
78 case SettingsModel::ABRMode
:
79 case SettingsModel::CBRMode
:
80 return qBound(8, index2bitrate(index
), 400);
83 throw "Bad RC mode specified!";
87 virtual int valueType(int mode
) const
91 case SettingsModel::VBRMode
:
92 return TYPE_QUALITY_LEVEL_FLT
;
94 case SettingsModel::ABRMode
:
95 return TYPE_APPROX_BITRATE
;
97 case SettingsModel::CBRMode
:
101 throw "Bad RC mode specified!";
105 virtual const char *description(void) const
107 static const char* s_description
= "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
108 return s_description
;
111 static const g_aacEncoderInfo
;
113 ///////////////////////////////////////////////////////////////////////////////
114 // Encoder implementation
115 ///////////////////////////////////////////////////////////////////////////////
117 AACEncoder::AACEncoder(void)
119 m_binary_enc(lamexp_lookup_tool("neroAacEnc.exe")),
120 m_binary_tag(lamexp_lookup_tool("neroAacTag.exe")),
121 m_binary_sox(lamexp_lookup_tool("sox.exe"))
123 if(m_binary_enc
.isEmpty() || m_binary_tag
.isEmpty() || m_binary_sox
.isEmpty())
125 throw "Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!";
129 m_configEnable2Pass
= true;
132 AACEncoder::~AACEncoder(void)
136 bool AACEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
138 const unsigned int fileDuration
= metaInfo
.fileDuration();
142 const QString baseName
= QFileInfo(outputFile
).fileName();
144 switch(m_configRCMode
)
146 case SettingsModel::VBRMode
:
147 args
<< "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate
* 5, 100)) / 100.0);
149 case SettingsModel::ABRMode
:
150 args
<< "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
152 case SettingsModel::CBRMode
:
153 args
<< "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
156 throw "Bad rate-control mode!";
160 if(m_configEnable2Pass
&& (m_configRCMode
== SettingsModel::ABRMode
))
165 switch(m_configProfile
)
168 args
<< "-lc"; //Forces use of LC AAC profile
171 args
<< "-he"; //Forces use of HE AAC profile
174 args
<< "-hev2"; //Forces use of HEv2 AAC profile
178 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
180 args
<< "-if" << QDir::toNativeSeparators(sourceFile
);
181 args
<< "-of" << QDir::toNativeSeparators(outputFile
);
183 if(!startProcess(process
, m_binary_enc
, args
))
188 bool bTimeout
= false;
189 bool bAborted
= false;
190 int prevProgress
= -1;
193 QRegExp
regExp("Processed\\s+(\\d+)\\s+seconds");
194 QRegExp
regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
195 QRegExp
regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
197 while(process
.state() != QProcess::NotRunning
)
203 emit
messageLogged("\nABORTED BY USER !!!");
206 process
.waitForReadyRead(m_processTimeoutInterval
);
207 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
210 qWarning("NeroAacEnc process timed out <-- killing!");
211 emit
messageLogged("\nPROCESS TIMEOUT !!!");
215 while(process
.bytesAvailable() > 0)
217 QByteArray line
= process
.readLine();
218 QString text
= QString::fromUtf8(line
.constData()).simplified();
219 if(regExp_pass1
.lastIndexIn(text
) >= 0)
222 int progress
= regExp_pass1
.cap(1).toInt(&ok
);
223 if(ok
&& (fileDuration
> 0))
225 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(fileDuration
)) * 50.0);
226 if(newProgress
> prevProgress
)
228 emit
statusUpdated(newProgress
);
229 prevProgress
= qMin(newProgress
+ 2, 99);
233 else if(regExp_pass2
.lastIndexIn(text
) >= 0)
236 int progress
= regExp_pass2
.cap(1).toInt(&ok
);
237 if(ok
&& (fileDuration
> 0))
239 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(fileDuration
)) * 50.0) + 50;
240 if(newProgress
> prevProgress
)
242 emit
statusUpdated(newProgress
);
243 prevProgress
= qMin(newProgress
+ 2, 99);
247 else if(regExp
.lastIndexIn(text
) >= 0)
250 int progress
= regExp
.cap(1).toInt(&ok
);
251 if(ok
&& (fileDuration
> 0))
253 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(fileDuration
)) * 100.0);
254 if(newProgress
> prevProgress
)
256 emit
statusUpdated(newProgress
);
257 prevProgress
= qMin(newProgress
+ 2, 99);
261 else if(!text
.isEmpty())
263 emit
messageLogged(text
);
268 process
.waitForFinished();
269 if(process
.state() != QProcess::NotRunning
)
272 process
.waitForFinished(-1);
275 emit
statusUpdated(100);
276 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
278 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
283 emit
messageLogged("\n-------------------------------\n");
286 args
<< QDir::toNativeSeparators(outputFile
);
288 if(!metaInfo
.fileName().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.fileName()));
289 if(!metaInfo
.fileArtist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.fileArtist()));
290 if(!metaInfo
.fileAlbum().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.fileAlbum()));
291 if(!metaInfo
.fileGenre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.fileGenre()));
292 if(!metaInfo
.fileComment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.fileComment()));
293 if(metaInfo
.fileYear()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.fileYear()));
294 if(metaInfo
.filePosition()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.filePosition()));
295 if(!metaInfo
.fileCover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.fileCover());
297 if(!startProcess(process
, m_binary_tag
, args
))
304 while(process
.state() != QProcess::NotRunning
)
310 emit
messageLogged("\nABORTED BY USER !!!");
313 process
.waitForReadyRead(m_processTimeoutInterval
);
314 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
317 qWarning("NeroAacTag process timed out <-- killing!");
318 emit
messageLogged("\nPROCESS TIMEOUT !!!");
322 while(process
.bytesAvailable() > 0)
324 QByteArray line
= process
.readLine();
325 QString text
= QString::fromUtf8(line
.constData()).simplified();
328 emit
messageLogged(text
);
333 process
.waitForFinished();
334 if(process
.state() != QProcess::NotRunning
)
337 process
.waitForFinished(-1);
340 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
342 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
350 QString
AACEncoder::extension(void)
355 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
357 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
359 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
368 void AACEncoder::setProfile(int profile
)
370 m_configProfile
= profile
;
373 void AACEncoder::setEnable2Pass(bool enabled
)
375 m_configEnable2Pass
= enabled
;
378 const bool AACEncoder::needsTimingInfo(void)
383 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
385 return &g_aacEncoderInfo
;