1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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 #define IS_VALID(X) (((X) != 0U) && ((X) != UINT_MAX))
38 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
42 class AACEncoderInfo
: public AbstractEncoderInfo
44 virtual bool isModeSupported(int mode
) const
48 case SettingsModel::VBRMode
:
49 case SettingsModel::ABRMode
:
50 case SettingsModel::CBRMode
:
54 MUTILS_THROW("Bad RC mode specified!");
58 virtual int valueCount(int mode
) const
62 case SettingsModel::VBRMode
:
65 case SettingsModel::ABRMode
:
66 case SettingsModel::CBRMode
:
70 MUTILS_THROW("Bad RC mode specified!");
74 virtual int valueAt(int mode
, int index
) const
78 case SettingsModel::VBRMode
:
79 return qBound(0, index
* 5, 100);
81 case SettingsModel::ABRMode
:
82 case SettingsModel::CBRMode
:
83 return qBound(8, index2bitrate(index
), 400);
86 MUTILS_THROW("Bad RC mode specified!");
90 virtual int valueType(int mode
) const
94 case SettingsModel::VBRMode
:
95 return TYPE_QUALITY_LEVEL_FLT
;
97 case SettingsModel::ABRMode
:
98 return TYPE_APPROX_BITRATE
;
100 case SettingsModel::CBRMode
:
104 MUTILS_THROW("Bad RC mode specified!");
108 virtual const char *description(void) const
110 static const char* s_description
= "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
111 return s_description
;
114 virtual const char *extension(void) const
116 static const char* s_extension
= "mp4";
120 virtual bool isResamplingSupported(void) const
125 static const g_aacEncoderInfo
;
127 ///////////////////////////////////////////////////////////////////////////////
128 // Encoder implementation
129 ///////////////////////////////////////////////////////////////////////////////
131 AACEncoder::AACEncoder(void)
133 m_binary_enc(lamexp_tools_lookup(L1S("neroAacEnc.exe"))),
134 m_binary_tag(lamexp_tools_lookup(L1S("neroAacTag.exe")))
136 if(m_binary_enc
.isEmpty() || m_binary_tag
.isEmpty())
138 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
142 m_configEnable2Pass
= true;
145 AACEncoder::~AACEncoder(void)
149 bool AACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const unsigned int channels
, const QString
&outputFile
, QAtomicInt
&abortFlag
)
153 const QString baseName
= QFileInfo(outputFile
).fileName();
155 switch(m_configRCMode
)
157 case SettingsModel::VBRMode
:
158 args
<< L1S("-q") << QString().sprintf("%.2f", double(qBound(0, m_configBitrate
* 5, 100)) / 100.0);
160 case SettingsModel::ABRMode
:
161 args
<< L1S("-br") << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
163 case SettingsModel::CBRMode
:
164 args
<< L1S("-cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate
), 400) * 1000);
167 MUTILS_THROW("Bad rate-control mode!");
171 if(m_configEnable2Pass
&& (m_configRCMode
== SettingsModel::ABRMode
))
173 args
<< L1S("-2pass");
176 int selectedProfile
= m_configProfile
;
177 if ((selectedProfile
== 3) && IS_VALID(channels
) && (channels
!= 2))
179 emit
messageLogged("WARNING: Cannot use HE-AAC v2 (SBR+PS) with Mono input --> reverting to HE-AAC (SBR)");
183 switch(selectedProfile
)
186 //Do *not* overwrite profile -> let the encoder decide!
189 args
<< L1S("-lc"); //Forces use of LC AAC profile
192 args
<< L1S("-he"); //Forces use of HE AAC profile
195 args
<< L1S("-hev2"); //Forces use of HEv2 AAC profile
198 MUTILS_THROW("Bad AAC Profile specified!");
201 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
203 args
<< L1S("-if") << QDir::toNativeSeparators(sourceFile
);
204 args
<< L1S("-of") << QDir::toNativeSeparators(outputFile
);
206 if(!startProcess(process
, m_binary_enc
, args
))
211 bool bTimeout
= false;
212 bool bAborted
= false;
213 int prevProgress
= -1;
216 QRegExp
regExp(L1S("Processed\\s+(\\d+)\\s+seconds"));
217 QRegExp
regExp_pass1(L1S("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"));
218 QRegExp
regExp_pass2(L1S("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"));
220 while(process
.state() != QProcess::NotRunning
)
222 if(checkFlag(abortFlag
))
226 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
229 process
.waitForReadyRead(m_processTimeoutInterval
);
230 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
233 qWarning("NeroAacEnc process timed out <-- killing!");
234 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
238 while(process
.bytesAvailable() > 0)
240 QByteArray line
= process
.readLine();
241 QString text
= QString::fromUtf8(line
.constData()).simplified();
242 if(regExp_pass1
.lastIndexIn(text
) >= 0)
245 int progress
= regExp_pass1
.cap(1).toInt(&ok
);
246 if(ok
&& (duration
> 0))
248 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0);
249 if(newProgress
> prevProgress
)
251 emit
statusUpdated(newProgress
);
252 prevProgress
= qMin(newProgress
+ 2, 99);
256 else if(regExp_pass2
.lastIndexIn(text
) >= 0)
259 int progress
= regExp_pass2
.cap(1).toInt(&ok
);
260 if(ok
&& (duration
> 0))
262 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 50.0) + 50;
263 if(newProgress
> prevProgress
)
265 emit
statusUpdated(newProgress
);
266 prevProgress
= qMin(newProgress
+ 2, 99);
270 else if(regExp
.lastIndexIn(text
) >= 0)
273 int progress
= regExp
.cap(1).toInt(&ok
);
274 if(ok
&& (duration
> 0))
276 int newProgress
= qRound((static_cast<double>(progress
) / static_cast<double>(duration
)) * 100.0);
277 if(newProgress
> prevProgress
)
279 emit
statusUpdated(newProgress
);
280 prevProgress
= qMin(newProgress
+ 2, 99);
284 else if(!text
.isEmpty())
286 emit
messageLogged(text
);
291 process
.waitForFinished();
292 if(process
.state() != QProcess::NotRunning
)
295 process
.waitForFinished(-1);
298 emit
statusUpdated(100);
299 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
301 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
306 if(metaInfo
.empty(false))
311 emit
messageLogged(L1S("\n-------------------------------\n"));
314 args
<< QDir::toNativeSeparators(outputFile
);
316 if(!metaInfo
.title().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.title()));
317 if(!metaInfo
.artist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.artist()));
318 if(!metaInfo
.album().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.album()));
319 if(!metaInfo
.genre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.genre()));
320 if(!metaInfo
.comment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.comment()));
321 if(metaInfo
.year()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.year()));
322 if(metaInfo
.position()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.position()));
323 if(!metaInfo
.cover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.cover());
325 if(!startProcess(process
, m_binary_tag
, args
))
332 while(process
.state() != QProcess::NotRunning
)
334 if(checkFlag(abortFlag
))
338 emit
messageLogged(L1S("\nABORTED BY USER !!!"));
341 process
.waitForReadyRead(m_processTimeoutInterval
);
342 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
345 qWarning("NeroAacTag process timed out <-- killing!");
346 emit
messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
350 while(process
.bytesAvailable() > 0)
352 QByteArray line
= process
.readLine();
353 QString text
= QString::fromUtf8(line
.constData()).simplified();
356 emit
messageLogged(text
);
361 process
.waitForFinished();
362 if(process
.state() != QProcess::NotRunning
)
365 process
.waitForFinished(-1);
368 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
370 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
378 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
380 if(containerType
.compare(L1S("Wave"), Qt::CaseInsensitive
) == 0)
382 if(formatType
.compare(L1S("PCM"), Qt::CaseInsensitive
) == 0)
391 void AACEncoder::setProfile(int profile
)
393 m_configProfile
= qBound(0, profile
, 3);
396 void AACEncoder::setEnable2Pass(bool enabled
)
398 m_configEnable2Pass
= enabled
;
401 const bool AACEncoder::needsTimingInfo(void)
406 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
408 return &g_aacEncoderInfo
;