1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 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 int prevProgress
= -1;
212 QRegExp
regExp_sp(L1S("\\bprocessed\\s+(\\d+)\\s+seconds"), Qt::CaseInsensitive
);
213 QRegExp
regExp_mp(L1S("\\b(\\w+)\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"), Qt::CaseInsensitive
);
215 const result_t result
= awaitProcess(process
, abortFlag
, [this, &prevProgress
, &duration
, ®Exp_sp
, ®Exp_mp
](const QString
&text
)
217 if (regExp_mp
.lastIndexIn(text
) >= 0)
220 if ((duration
> 0) && MUtils::regexp_parse_int32(regExp_mp
, timeElapsed
, 2))
222 const bool second_pass
= (regExp_mp
.cap(1).compare(L1S("second"), Qt::CaseInsensitive
) == 0);
223 const int newProgress
= qRound((second_pass
? 50.0 : 0.0) + ((static_cast<double>(timeElapsed
) / static_cast<double>(duration
)) * 50.0));
224 if (newProgress
> prevProgress
)
226 emit
statusUpdated(newProgress
);
227 prevProgress
= NEXT_PROGRESS(newProgress
);
232 if (regExp_sp
.lastIndexIn(text
) >= 0)
235 if ((duration
> 0) && MUtils::regexp_parse_int32(regExp_sp
, timeElapsed
))
237 const int newProgress
= qRound((static_cast<double>(timeElapsed
) / static_cast<double>(duration
)) * 100.0);
238 if (newProgress
> prevProgress
)
240 emit
statusUpdated(newProgress
);
241 prevProgress
= NEXT_PROGRESS(newProgress
);
249 if(result
!= RESULT_SUCCESS
)
254 if(metaInfo
.empty(false))
259 emit
messageLogged(L1S("\n-------------------------------\n"));
262 args
<< QDir::toNativeSeparators(outputFile
);
264 if(!metaInfo
.title().isEmpty()) args
<< QString("-meta:title=%1").arg(cleanTag(metaInfo
.title()));
265 if(!metaInfo
.artist().isEmpty()) args
<< QString("-meta:artist=%1").arg(cleanTag(metaInfo
.artist()));
266 if(!metaInfo
.album().isEmpty()) args
<< QString("-meta:album=%1").arg(cleanTag(metaInfo
.album()));
267 if(!metaInfo
.genre().isEmpty()) args
<< QString("-meta:genre=%1").arg(cleanTag(metaInfo
.genre()));
268 if(!metaInfo
.comment().isEmpty()) args
<< QString("-meta:comment=%1").arg(cleanTag(metaInfo
.comment()));
269 if(metaInfo
.year()) args
<< QString("-meta:year=%1").arg(QString::number(metaInfo
.year()));
270 if(metaInfo
.position()) args
<< QString("-meta:track=%1").arg(QString::number(metaInfo
.position()));
271 if(!metaInfo
.cover().isEmpty()) args
<< QString("-add-cover:%1:%2").arg("front", metaInfo
.cover());
273 if(!startProcess(process
, m_binary_tag
, args
))
278 return (awaitProcess(process
, abortFlag
) == RESULT_SUCCESS
);
281 bool AACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
283 if(containerType
.compare(L1S("Wave"), Qt::CaseInsensitive
) == 0)
285 if(formatType
.compare(L1S("PCM"), Qt::CaseInsensitive
) == 0)
294 const unsigned int *AACEncoder::supportedSamplerates(void)
296 static const unsigned int supportedRates
[] = { 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL
};
297 return supportedRates
;
300 void AACEncoder::setProfile(int profile
)
302 m_configProfile
= qBound(0, profile
, 3);
305 void AACEncoder::setEnable2Pass(bool enabled
)
307 m_configEnable2Pass
= enabled
;
310 const bool AACEncoder::needsTimingInfo(void)
315 const AbstractEncoderInfo
*AACEncoder::getEncoderInfo(void)
317 return &g_aacEncoderInfo
;