1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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_QAAC.h"
27 #include "Model_Settings.h"
30 #include <MUtils/Global.h>
38 #include <QCoreApplication>
40 static int index2bitrate(const int index
)
42 return (index
< 32) ? ((index
+ 1) * 8) : ((index
- 15) * 16);
45 static const int g_qaacVBRQualityLUT
[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX
};
47 static const int RESAMPLING_QUALITY
= 127;
49 ///////////////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////////////////
53 class QAACEncoderInfo
: public AbstractEncoderInfo
55 virtual bool isModeSupported(int mode
) const
59 case SettingsModel::VBRMode
:
60 case SettingsModel::CBRMode
:
61 case SettingsModel::ABRMode
:
65 MUTILS_THROW("Bad RC mode specified!");
69 virtual int valueCount(int mode
) const
73 case SettingsModel::VBRMode
:
76 case SettingsModel::ABRMode
:
77 case SettingsModel::CBRMode
:
81 MUTILS_THROW("Bad RC mode specified!");
85 virtual int valueAt(int mode
, int index
) const
89 case SettingsModel::VBRMode
:
90 return g_qaacVBRQualityLUT
[qBound(0, index
, 14)];
92 case SettingsModel::ABRMode
:
93 case SettingsModel::CBRMode
:
94 return qBound(8, index2bitrate(index
), 576);
97 MUTILS_THROW("Bad RC mode specified!");
101 virtual int valueType(int mode
) const
105 case SettingsModel::VBRMode
:
106 return TYPE_QUALITY_LEVEL_INT
;
108 case SettingsModel::ABRMode
:
109 return TYPE_APPROX_BITRATE
;
111 case SettingsModel::CBRMode
:
115 MUTILS_THROW("Bad RC mode specified!");
119 virtual const char *description(void) const
121 static const char* s_description
= "QAAC/QuickTime (\x0C2\x0A9 Apple Inc.)";
122 return s_description
;
125 virtual const char *extension(void) const
127 static const char* s_extension
= "mp4";
131 virtual bool isResamplingSupported(void) const
136 static const g_qaacEncoderInfo
;
138 ///////////////////////////////////////////////////////////////////////////////
139 // Encoder implementation
140 ///////////////////////////////////////////////////////////////////////////////
142 QAACEncoder::QAACEncoder(void)
144 m_binary_qaac32(lamexp_tools_lookup("qaac.exe")),
145 m_binary_qaac64(lamexp_tools_lookup("qaac64.exe"))
147 if(m_binary_qaac32
.isEmpty() && m_binary_qaac64
.isEmpty())
149 MUTILS_THROW("Error initializing QAAC. Tool 'qaac.exe' is not registred!");
155 QAACEncoder::~QAACEncoder(void)
159 bool QAACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
161 const QString qaac_bin
= m_binary_qaac64
.isEmpty() ? m_binary_qaac32
: m_binary_qaac64
;
166 QProcessEnvironment env
= QProcessEnvironment::systemEnvironment();
167 env
.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
168 process
.setProcessEnvironment(env
);
170 if(m_configRCMode
!= SettingsModel::VBRMode
)
172 switch(m_configProfile
)
176 args
<< "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
181 switch(m_configRCMode
)
183 case SettingsModel::CBRMode
:
184 args
<< "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 576));
186 case SettingsModel::ABRMode
:
187 args
<< "--cvbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 576));
189 case SettingsModel::VBRMode
:
190 args
<< "--tvbr" << QString::number(g_qaacVBRQualityLUT
[qBound(0, m_configBitrate
, 14)]);
193 MUTILS_THROW("Bad rate-control mode!");
197 if (m_configSamplingRate
> 0)
199 args
<< QString("--native-resampler=bats,%0").arg(QString::number(RESAMPLING_QUALITY
));
200 args
<< "--rate" << QString::number(m_configSamplingRate
);
203 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
205 if(!metaInfo
.title().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.title());
206 if(!metaInfo
.artist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.artist());
207 if(!metaInfo
.album().isEmpty()) args
<< "--album" << cleanTag(metaInfo
.album());
208 if(!metaInfo
.genre().isEmpty()) args
<< "--genre" << cleanTag(metaInfo
.genre());
209 if(!metaInfo
.comment().isEmpty()) args
<< "--comment" << cleanTag( metaInfo
.comment());
210 if(metaInfo
.year()) args
<< "--date" << QString::number(metaInfo
.year());
211 if(metaInfo
.position()) args
<< "--track" << QString::number(metaInfo
.position());
212 if(!metaInfo
.cover().isEmpty()) args
<< "--artwork" << metaInfo
.cover();
215 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
216 args
<< QDir::toNativeSeparators(sourceFile
);
218 if(!startProcess(process
, qaac_bin
, args
, QFileInfo(outputFile
).canonicalPath()))
223 bool bTimeout
= false;
224 bool bAborted
= false;
225 int prevProgress
= -1;
227 QRegExp
regExp("\\[(\\d+)\\.(\\d)%\\]");
229 while(process
.state() != QProcess::NotRunning
)
235 emit
messageLogged("\nABORTED BY USER !!!");
238 process
.waitForReadyRead(m_processTimeoutInterval
);
239 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
242 qWarning("QAAC process timed out <-- killing!");
243 emit
messageLogged("\nPROCESS TIMEOUT !!!");
247 while(process
.bytesAvailable() > 0)
249 QByteArray line
= process
.readLine();
250 QString text
= QString::fromUtf8(line
.constData()).simplified();
251 if(regExp
.lastIndexIn(text
) >= 0)
254 int progress
= regExp
.cap(1).toInt(&ok
);
255 if(ok
&& (progress
> prevProgress
))
257 emit
statusUpdated(progress
);
258 prevProgress
= qMin(progress
+ 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
)
286 bool QAACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
288 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
290 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
299 void QAACEncoder::setProfile(int profile
)
301 m_configProfile
= profile
;
304 const AbstractEncoderInfo
*QAACEncoder::getEncoderInfo(void)
306 return &g_qaacEncoderInfo
;