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_QAAC.h"
25 #include "Model_Settings.h"
30 #include <QCoreApplication>
32 static int index2bitrate(const int index
)
34 return (index
< 32) ? ((index
+ 1) * 8) : ((index
- 15) * 16);
37 static const int g_qaacVBRQualityLUT
[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX
};
39 ///////////////////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////////
43 class QAACEncoderInfo
: public AbstractEncoderInfo
45 virtual bool isModeSupported(int mode
) const
49 case SettingsModel::VBRMode
:
50 case SettingsModel::CBRMode
:
51 case SettingsModel::ABRMode
:
55 throw "Bad RC mode specified!";
59 virtual int valueCount(int mode
) const
63 case SettingsModel::VBRMode
:
66 case SettingsModel::ABRMode
:
67 case SettingsModel::CBRMode
:
71 throw "Bad RC mode specified!";
75 virtual int valueAt(int mode
, int index
) const
79 case SettingsModel::VBRMode
:
80 return g_qaacVBRQualityLUT
[qBound(0, index
, 14)];
82 case SettingsModel::ABRMode
:
83 case SettingsModel::CBRMode
:
84 return qBound(8, index2bitrate(index
), 576);
87 throw "Bad RC mode specified!";
91 virtual int valueType(int mode
) const
95 case SettingsModel::VBRMode
:
96 return TYPE_QUALITY_LEVEL_INT
;
98 case SettingsModel::ABRMode
:
99 return TYPE_APPROX_BITRATE
;
101 case SettingsModel::CBRMode
:
105 throw "Bad RC mode specified!";
109 virtual const char *description(void) const
111 static const char* s_description
= "QAAC/QuickTime (\x0C2\x0A9 Appel)";
112 return s_description
;
115 static const g_qaacEncoderInfo
;
117 ///////////////////////////////////////////////////////////////////////////////
118 // Encoder implementation
119 ///////////////////////////////////////////////////////////////////////////////
121 QAACEncoder::QAACEncoder(void)
123 m_binary_enc(lamexp_lookup_tool("qaac.exe")),
124 m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
126 if(m_binary_enc
.isEmpty() || m_binary_dll
.isEmpty())
128 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
134 QAACEncoder::~QAACEncoder(void)
138 bool QAACEncoder::encode(const QString
&sourceFile
, const AudioFileModel
&metaInfo
, const QString
&outputFile
, volatile bool *abortFlag
)
143 process
.setWorkingDirectory(QFileInfo(outputFile
).canonicalPath());
145 QProcessEnvironment env
= QProcessEnvironment::systemEnvironment();
146 env
.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), lamexp_temp_folder2())));
147 process
.setProcessEnvironment(env
);
149 if(m_configRCMode
!= SettingsModel::VBRMode
)
151 switch(m_configProfile
)
155 args
<< "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
160 switch(m_configRCMode
)
162 case SettingsModel::CBRMode
:
163 args
<< "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 576));
165 case SettingsModel::ABRMode
:
166 args
<< "--abr" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 576));
168 case SettingsModel::VBRMode
:
169 args
<< "--tvbr" << QString::number(g_qaacVBRQualityLUT
[qBound(0, m_configBitrate
, 14)]);
172 throw "Bad rate-control mode!";
176 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
178 if(!metaInfo
.fileName().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.fileName());
179 if(!metaInfo
.fileArtist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.fileArtist());
180 if(!metaInfo
.fileAlbum().isEmpty()) args
<< "--album" << cleanTag(metaInfo
.fileAlbum());
181 if(!metaInfo
.fileGenre().isEmpty()) args
<< "--genre" << cleanTag(metaInfo
.fileGenre());
182 if(!metaInfo
.fileComment().isEmpty()) args
<< "--comment" << cleanTag( metaInfo
.fileComment());
183 if(metaInfo
.fileYear()) args
<< "--date" << QString::number(metaInfo
.fileYear());
184 if(metaInfo
.filePosition()) args
<< "--track" << QString::number(metaInfo
.filePosition());
185 if(!metaInfo
.fileCover().isEmpty()) args
<< "--artwork" << metaInfo
.fileCover();
188 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
189 args
<< QDir::toNativeSeparators(sourceFile
);
191 if(!startProcess(process
, m_binary_enc
, args
))
196 bool bTimeout
= false;
197 bool bAborted
= false;
198 int prevProgress
= -1;
200 QRegExp
regExp("\\[(\\d+)\\.(\\d)%\\]");
202 while(process
.state() != QProcess::NotRunning
)
208 emit
messageLogged("\nABORTED BY USER !!!");
211 process
.waitForReadyRead(m_processTimeoutInterval
);
212 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
215 qWarning("QAAC process timed out <-- killing!");
216 emit
messageLogged("\nPROCESS TIMEOUT !!!");
220 while(process
.bytesAvailable() > 0)
222 QByteArray line
= process
.readLine();
223 QString text
= QString::fromUtf8(line
.constData()).simplified();
224 if(regExp
.lastIndexIn(text
) >= 0)
227 int progress
= regExp
.cap(1).toInt(&ok
);
228 if(ok
&& (progress
> prevProgress
))
230 emit
statusUpdated(progress
);
231 prevProgress
= qMin(progress
+ 2, 99);
234 else if(!text
.isEmpty())
236 emit
messageLogged(text
);
241 process
.waitForFinished();
242 if(process
.state() != QProcess::NotRunning
)
245 process
.waitForFinished(-1);
248 emit
statusUpdated(100);
249 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
251 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
259 QString
QAACEncoder::extension(void)
264 bool QAACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
266 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
268 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
277 void QAACEncoder::setProfile(int profile
)
279 m_configProfile
= profile
;
282 const AbstractEncoderInfo
*QAACEncoder::getEncoderInfo(void)
284 return &g_qaacEncoderInfo
;