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
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
140 const QString baseName
= QFileInfo(outputFile
).fileName();
142 switch(m_configRCMode
)
144 case SettingsModel::VBRMode
:
145 args
<< "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate
* 5, 100)) / 100.0);
147 case SettingsModel::ABRMode
:
148 args
<< "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
150 case SettingsModel::CBRMode
:
151 args
<< "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
154 throw "Bad rate-control mode!";
158 if(m_configEnable2Pass
&& (m_configRCMode
== SettingsModel::ABRMode
))
163 switch(m_configProfile
)
166 args
<< "-lc"; //Forces use of LC AAC profile
169 args
<< "-he"; //Forces use of HE AAC profile
172 args
<< "-hev2"; //Forces use of HEv2 AAC profile
176 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
178 args
<< "-if" << QDir::toNativeSeparators(sourceFile
);
179 args
<< "-of" << QDir::toNativeSeparators(outputFile
);
181 if(!startProcess(process
, m_binary_enc
, args
))
186 bool bTimeout
= false;
187 bool bAborted
= false;
188 int prevProgress
= -1;
191 QRegExp
regExp("Processed\\s+(\\d+)\\s+seconds");
192 QRegExp
regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
193 QRegExp
regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
195 while(process
.state() != QProcess::NotRunning
)
201 emit
messageLogged("\nABORTED BY USER !!!");
204 process
.waitForReadyRead(m_processTimeoutInterval
);
205 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
208 qWarning("NeroAacEnc process timed out <-- killing!");
209 emit
messageLogged("\nPROCESS TIMEOUT !!!");
213 while(process
.bytesAvailable() > 0)
215 QByteArray line
= process
.readLine();
216 QString text
= QString::fromUtf8(line
.constData()).simplified();
217 if(regExp_pass1
.lastIndexIn(text
) >= 0)
220 int progress
= regExp_pass1
.cap(1).toInt(&ok
);
221 if(ok
&& (duration
> 0))
223 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0);
224 if(newProgress
> prevProgress
)
226 emit
statusUpdated(newProgress
);
227 prevProgress
= qMin(newProgress
+ 2, 99);
231 else if(regExp_pass2
.lastIndexIn(text
) >= 0)
234 int progress
= regExp_pass2
.cap(1).toInt(&ok
);
235 if(ok
&& (duration
> 0))
237 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0) + 50;
238 if(newProgress
> prevProgress
)
240 emit
statusUpdated(newProgress
);
241 prevProgress
= qMin(newProgress
+ 2, 99);
245 else if(regExp
.lastIndexIn(text
) >= 0)
248 int progress
= regExp
.cap(1).toInt(&ok
);
249 if(ok
&& (duration
> 0))
251 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 100.0);
252 if(newProgress
> prevProgress
)
254 emit
statusUpdated(newProgress
);
255 prevProgress
= qMin(newProgress
+ 2, 99);
259 else if(!text
.isEmpty())
261 emit
messageLogged(text
);
266 process
.waitForFinished();
267 if(process
.state() != QProcess::NotRunning
)
270 process
.waitForFinished(-1);
273 emit
statusUpdated(100);
274 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
276 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
281 emit
messageLogged("\n-------------------------------\n");
284 args
<< QDir::toNativeSeparators(outputFile
);
286 if(!metaInfo
.title().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.title()));
287 if(!metaInfo
.artist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.artist()));
288 if(!metaInfo
.album().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.album()));
289 if(!metaInfo
.genre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.genre()));
290 if(!metaInfo
.comment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.comment()));
291 if(metaInfo
.year()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.year()));
292 if(metaInfo
.position()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.position()));
293 if(!metaInfo
.cover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.cover());
295 if(!startProcess(process
, m_binary_tag
, args
))
302 while(process
.state() != QProcess::NotRunning
)
308 emit
messageLogged("\nABORTED BY USER !!!");
311 process
.waitForReadyRead(m_processTimeoutInterval
);
312 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
315 qWarning("NeroAacTag process timed out <-- killing!");
316 emit
messageLogged("\nPROCESS TIMEOUT !!!");
320 while(process
.bytesAvailable() > 0)
322 QByteArray line
= process
.readLine();
323 QString text
= QString::fromUtf8(line
.constData()).simplified();
326 emit
messageLogged(text
);
331 process
.waitForFinished();
332 if(process
.state() != QProcess::NotRunning
)
335 process
.waitForFinished(-1);
338 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
340 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
348 QString
AACEncoder::extension(void)
353 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
355 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
357 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
366 void AACEncoder::setProfile(int profile
)
368 m_configProfile
= profile
;
371 void AACEncoder::setEnable2Pass(bool enabled
)
373 m_configEnable2Pass
= enabled
;
376 const bool AACEncoder::needsTimingInfo(void)
381 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
383 return &g_aacEncoderInfo
;