Build fix.
[LameXP.git] / src / Encoder_AAC.cpp
blob91919f57718d695c40d0b5cdbbca2fd718e6b6e6
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
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 ///////////////////////////////////////////////////////////////////////////////
39 // Encoder Info
40 ///////////////////////////////////////////////////////////////////////////////
42 class AACEncoderInfo : public AbstractEncoderInfo
44 virtual bool isModeSupported(int mode) const
46 switch(mode)
48 case SettingsModel::VBRMode:
49 case SettingsModel::ABRMode:
50 case SettingsModel::CBRMode:
51 return true;
52 break;
53 default:
54 MUTILS_THROW("Bad RC mode specified!");
58 virtual int valueCount(int mode) const
60 switch(mode)
62 case SettingsModel::VBRMode:
63 return 21;
64 break;
65 case SettingsModel::ABRMode:
66 case SettingsModel::CBRMode:
67 return 41;
68 break;
69 default:
70 MUTILS_THROW("Bad RC mode specified!");
74 virtual int valueAt(int mode, int index) const
76 switch(mode)
78 case SettingsModel::VBRMode:
79 return qBound(0, index * 5, 100);
80 break;
81 case SettingsModel::ABRMode:
82 case SettingsModel::CBRMode:
83 return qBound(8, index2bitrate(index), 400);
84 break;
85 default:
86 MUTILS_THROW("Bad RC mode specified!");
90 virtual int valueType(int mode) const
92 switch(mode)
94 case SettingsModel::VBRMode:
95 return TYPE_QUALITY_LEVEL_FLT;
96 break;
97 case SettingsModel::ABRMode:
98 return TYPE_APPROX_BITRATE;
99 break;
100 case SettingsModel::CBRMode:
101 return TYPE_BITRATE;
102 break;
103 default:
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";
117 return s_extension;
120 virtual bool isResamplingSupported(void) const
122 return false;
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!");
141 m_configProfile = 0;
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)
151 QProcess process;
152 QStringList args;
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);
159 break;
160 case SettingsModel::ABRMode:
161 args << L1S("-br") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
162 break;
163 case SettingsModel::CBRMode:
164 args << L1S("-cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
165 break;
166 default:
167 MUTILS_THROW("Bad rate-control mode!");
168 break;
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)");
180 selectedProfile = 2;
183 switch(selectedProfile)
185 case 0:
186 //Do *not* overwrite profile -> let the encoder decide!
187 break;
188 case 1:
189 args << L1S("-lc"); //Forces use of LC AAC profile
190 break;
191 case 2:
192 args << L1S("-he"); //Forces use of HE AAC profile
193 break;
194 case 3:
195 args << L1S("-hev2"); //Forces use of HEv2 AAC profile
196 break;
197 default:
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))
208 return false;
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))
224 process.kill();
225 bAborted = true;
226 emit messageLogged(L1S("\nABORTED BY USER !!!"));
227 break;
229 process.waitForReadyRead(m_processTimeoutInterval);
230 if(!process.bytesAvailable() && process.state() == QProcess::Running)
232 process.kill();
233 qWarning("NeroAacEnc process timed out <-- killing!");
234 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
235 bTimeout = true;
236 break;
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)
244 bool ok = false;
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)
258 bool ok = false;
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)
272 bool ok = false;
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)
294 process.kill();
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)
303 return false;
306 if(metaInfo.empty(false))
308 return true;
311 emit messageLogged(L1S("\n-------------------------------\n"));
313 args.clear();
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))
327 return false;
330 bTimeout = false;
332 while(process.state() != QProcess::NotRunning)
334 if(checkFlag(abortFlag))
336 process.kill();
337 bAborted = true;
338 emit messageLogged(L1S("\nABORTED BY USER !!!"));
339 break;
341 process.waitForReadyRead(m_processTimeoutInterval);
342 if(!process.bytesAvailable() && process.state() == QProcess::Running)
344 process.kill();
345 qWarning("NeroAacTag process timed out <-- killing!");
346 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
347 bTimeout = true;
348 break;
350 while(process.bytesAvailable() > 0)
352 QByteArray line = process.readLine();
353 QString text = QString::fromUtf8(line.constData()).simplified();
354 if(!text.isEmpty())
356 emit messageLogged(text);
361 process.waitForFinished();
362 if(process.state() != QProcess::NotRunning)
364 process.kill();
365 process.waitForFinished(-1);
368 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
370 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
372 return false;
375 return true;
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)
384 return true;
388 return false;
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)
403 return true;
406 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
408 return &g_aacEncoderInfo;