The information whether an encoder supports "native" resampling is provided via Abstr...
[LameXP.git] / src / Encoder_AAC_QAAC.cpp
blob149740629525191d81ec8f6fa8b5f00aacab6729
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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"
25 //Internal
26 #include "Global.h"
27 #include "Model_Settings.h"
29 //MUtils
30 #include <MUtils/Global.h>
32 //StdLib
33 #include <math.h>
35 //Qt
36 #include <QProcess>
37 #include <QDir>
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 ///////////////////////////////////////////////////////////////////////////////
48 // Encoder Info
49 ///////////////////////////////////////////////////////////////////////////////
51 class QAACEncoderInfo : public AbstractEncoderInfo
53 virtual bool isModeSupported(int mode) const
55 switch(mode)
57 case SettingsModel::VBRMode:
58 case SettingsModel::CBRMode:
59 case SettingsModel::ABRMode:
60 return true;
61 break;
62 default:
63 MUTILS_THROW("Bad RC mode specified!");
67 virtual int valueCount(int mode) const
69 switch(mode)
71 case SettingsModel::VBRMode:
72 return 15;
73 break;
74 case SettingsModel::ABRMode:
75 case SettingsModel::CBRMode:
76 return 52;
77 break;
78 default:
79 MUTILS_THROW("Bad RC mode specified!");
83 virtual int valueAt(int mode, int index) const
85 switch(mode)
87 case SettingsModel::VBRMode:
88 return g_qaacVBRQualityLUT[qBound(0, index , 14)];
89 break;
90 case SettingsModel::ABRMode:
91 case SettingsModel::CBRMode:
92 return qBound(8, index2bitrate(index), 576);
93 break;
94 default:
95 MUTILS_THROW("Bad RC mode specified!");
99 virtual int valueType(int mode) const
101 switch(mode)
103 case SettingsModel::VBRMode:
104 return TYPE_QUALITY_LEVEL_INT;
105 break;
106 case SettingsModel::ABRMode:
107 return TYPE_APPROX_BITRATE;
108 break;
109 case SettingsModel::CBRMode:
110 return TYPE_BITRATE;
111 break;
112 default:
113 MUTILS_THROW("Bad RC mode specified!");
117 virtual const char *description(void) const
119 static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Apple Inc.)";
120 return s_description;
123 virtual const char *extension(void) const
125 static const char* s_extension = "mp4";
126 return s_extension;
129 virtual bool isResamplingSupported(void) const
131 return true;
134 static const g_qaacEncoderInfo;
136 ///////////////////////////////////////////////////////////////////////////////
137 // Encoder implementation
138 ///////////////////////////////////////////////////////////////////////////////
140 QAACEncoder::QAACEncoder(void)
142 m_binary_qaac32(lamexp_tools_lookup("qaac.exe")),
143 m_binary_qaac64(lamexp_tools_lookup("qaac64.exe"))
145 if(m_binary_qaac32.isEmpty() && m_binary_qaac64.isEmpty())
147 MUTILS_THROW("Error initializing QAAC. Tool 'qaac.exe' is not registred!");
150 m_configProfile = 0;
153 QAACEncoder::~QAACEncoder(void)
157 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
159 const QString qaac_bin = m_binary_qaac64.isEmpty() ? m_binary_qaac32 : m_binary_qaac64;
161 QProcess process;
162 QStringList args;
164 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
165 env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
166 process.setProcessEnvironment(env);
168 if(m_configRCMode != SettingsModel::VBRMode)
170 switch(m_configProfile)
172 case 2:
173 case 3:
174 args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
175 break;
179 switch(m_configRCMode)
181 case SettingsModel::CBRMode:
182 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
183 break;
184 case SettingsModel::ABRMode:
185 args << "--cvbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
186 break;
187 case SettingsModel::VBRMode:
188 args << "--tvbr" << QString::number(g_qaacVBRQualityLUT[qBound(0, m_configBitrate , 14)]);
189 break;
190 default:
191 MUTILS_THROW("Bad rate-control mode!");
192 break;
195 if (m_configSamplingRate > 0)
197 args << "-native-resampler" << "bats,127";
198 args << "--rate" << QString::number(m_configSamplingRate);
201 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
203 if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
204 if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
205 if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
206 if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
207 if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
208 if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
209 if(metaInfo.position()) args << "--track" << QString::number(metaInfo.position());
210 if(!metaInfo.cover().isEmpty()) args << "--artwork" << metaInfo.cover();
212 args << "-d" << ".";
213 args << "-o" << QDir::toNativeSeparators(outputFile);
214 args << QDir::toNativeSeparators(sourceFile);
216 if(!startProcess(process, qaac_bin, args, QFileInfo(outputFile).canonicalPath()))
218 return false;
221 bool bTimeout = false;
222 bool bAborted = false;
223 int prevProgress = -1;
225 QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
227 while(process.state() != QProcess::NotRunning)
229 if(*abortFlag)
231 process.kill();
232 bAborted = true;
233 emit messageLogged("\nABORTED BY USER !!!");
234 break;
236 process.waitForReadyRead(m_processTimeoutInterval);
237 if(!process.bytesAvailable() && process.state() == QProcess::Running)
239 process.kill();
240 qWarning("QAAC process timed out <-- killing!");
241 emit messageLogged("\nPROCESS TIMEOUT !!!");
242 bTimeout = true;
243 break;
245 while(process.bytesAvailable() > 0)
247 QByteArray line = process.readLine();
248 QString text = QString::fromUtf8(line.constData()).simplified();
249 if(regExp.lastIndexIn(text) >= 0)
251 bool ok = false;
252 int progress = regExp.cap(1).toInt(&ok);
253 if(ok && (progress > prevProgress))
255 emit statusUpdated(progress);
256 prevProgress = qMin(progress + 2, 99);
259 else if(!text.isEmpty())
261 emit messageLogged(text);
266 process.waitForFinished();
267 if(process.state() != QProcess::NotRunning)
269 process.kill();
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)
278 return false;
281 return true;
284 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
286 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
288 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
290 return true;
294 return false;
297 void QAACEncoder::setProfile(int profile)
299 m_configProfile = profile;
302 const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
304 return &g_qaacEncoderInfo;