Explicitly pass "--discard-comments" to OggEnc2 + some code refactoring.
[LameXP.git] / src / Encoder_AAC.cpp
blob730b802137bfe30f6b1cd155ea5d2620805d78c8
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.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 ///////////////////////////////////////////////////////////////////////////////
37 // Encoder Info
38 ///////////////////////////////////////////////////////////////////////////////
40 class AACEncoderInfo : public AbstractEncoderInfo
42 virtual bool isModeSupported(int mode) const
44 switch(mode)
46 case SettingsModel::VBRMode:
47 case SettingsModel::ABRMode:
48 case SettingsModel::CBRMode:
49 return true;
50 break;
51 default:
52 MUTILS_THROW("Bad RC mode specified!");
56 virtual int valueCount(int mode) const
58 switch(mode)
60 case SettingsModel::VBRMode:
61 return 21;
62 break;
63 case SettingsModel::ABRMode:
64 case SettingsModel::CBRMode:
65 return 41;
66 break;
67 default:
68 MUTILS_THROW("Bad RC mode specified!");
72 virtual int valueAt(int mode, int index) const
74 switch(mode)
76 case SettingsModel::VBRMode:
77 return qBound(0, index * 5, 100);
78 break;
79 case SettingsModel::ABRMode:
80 case SettingsModel::CBRMode:
81 return qBound(8, index2bitrate(index), 400);
82 break;
83 default:
84 MUTILS_THROW("Bad RC mode specified!");
88 virtual int valueType(int mode) const
90 switch(mode)
92 case SettingsModel::VBRMode:
93 return TYPE_QUALITY_LEVEL_FLT;
94 break;
95 case SettingsModel::ABRMode:
96 return TYPE_APPROX_BITRATE;
97 break;
98 case SettingsModel::CBRMode:
99 return TYPE_BITRATE;
100 break;
101 default:
102 MUTILS_THROW("Bad RC mode specified!");
106 virtual const char *description(void) const
108 static const char* s_description = "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
109 return s_description;
112 virtual const char *extension(void) const
114 static const char* s_extension = "mp4";
115 return s_extension;
118 virtual bool isResamplingSupported(void) const
120 return false;
123 static const g_aacEncoderInfo;
125 ///////////////////////////////////////////////////////////////////////////////
126 // Encoder implementation
127 ///////////////////////////////////////////////////////////////////////////////
129 AACEncoder::AACEncoder(void)
131 m_binary_enc(lamexp_tools_lookup(L1S("neroAacEnc.exe"))),
132 m_binary_tag(lamexp_tools_lookup(L1S("neroAacTag.exe")))
134 if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
136 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
139 m_configProfile = 0;
140 m_configEnable2Pass = true;
143 AACEncoder::~AACEncoder(void)
147 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
149 QProcess process;
150 QStringList args;
151 const QString baseName = QFileInfo(outputFile).fileName();
153 switch(m_configRCMode)
155 case SettingsModel::VBRMode:
156 args << L1S("-q") << QString().sprintf("%.2f", double(qBound(0, m_configBitrate * 5, 100)) / 100.0);
157 break;
158 case SettingsModel::ABRMode:
159 args << L1S("-br") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
160 break;
161 case SettingsModel::CBRMode:
162 args << L1S("-cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
163 break;
164 default:
165 MUTILS_THROW("Bad rate-control mode!");
166 break;
169 if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
171 args << L1S("-2pass");
174 switch(m_configProfile)
176 case 1:
177 args << L1S("-lc"); //Forces use of LC AAC profile
178 break;
179 case 2:
180 args << L1S("-he"); //Forces use of HE AAC profile
181 break;
182 case 3:
183 args << L1S("-hev2"); //Forces use of HEv2 AAC profile
184 break;
187 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
189 args << L1S("-if") << QDir::toNativeSeparators(sourceFile);
190 args << L1S("-of") << QDir::toNativeSeparators(outputFile);
192 if(!startProcess(process, m_binary_enc, args))
194 return false;
197 bool bTimeout = false;
198 bool bAborted = false;
199 int prevProgress = -1;
202 QRegExp regExp(L1S("Processed\\s+(\\d+)\\s+seconds"));
203 QRegExp regExp_pass1(L1S("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"));
204 QRegExp regExp_pass2(L1S("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"));
206 while(process.state() != QProcess::NotRunning)
208 if(*abortFlag)
210 process.kill();
211 bAborted = true;
212 emit messageLogged(L1S("\nABORTED BY USER !!!"));
213 break;
215 process.waitForReadyRead(m_processTimeoutInterval);
216 if(!process.bytesAvailable() && process.state() == QProcess::Running)
218 process.kill();
219 qWarning("NeroAacEnc process timed out <-- killing!");
220 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
221 bTimeout = true;
222 break;
224 while(process.bytesAvailable() > 0)
226 QByteArray line = process.readLine();
227 QString text = QString::fromUtf8(line.constData()).simplified();
228 if(regExp_pass1.lastIndexIn(text) >= 0)
230 bool ok = false;
231 int progress = regExp_pass1.cap(1).toInt(&ok);
232 if(ok && (duration > 0))
234 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0);
235 if(newProgress > prevProgress)
237 emit statusUpdated(newProgress);
238 prevProgress = qMin(newProgress + 2, 99);
242 else if(regExp_pass2.lastIndexIn(text) >= 0)
244 bool ok = false;
245 int progress = regExp_pass2.cap(1).toInt(&ok);
246 if(ok && (duration > 0))
248 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0) + 50;
249 if(newProgress > prevProgress)
251 emit statusUpdated(newProgress);
252 prevProgress = qMin(newProgress + 2, 99);
256 else if(regExp.lastIndexIn(text) >= 0)
258 bool ok = false;
259 int progress = regExp.cap(1).toInt(&ok);
260 if(ok && (duration > 0))
262 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 100.0);
263 if(newProgress > prevProgress)
265 emit statusUpdated(newProgress);
266 prevProgress = qMin(newProgress + 2, 99);
270 else if(!text.isEmpty())
272 emit messageLogged(text);
277 process.waitForFinished();
278 if(process.state() != QProcess::NotRunning)
280 process.kill();
281 process.waitForFinished(-1);
284 emit statusUpdated(100);
285 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
287 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
289 return false;
292 if(metaInfo.empty(false))
294 return true;
297 emit messageLogged(L1S("\n-------------------------------\n"));
299 args.clear();
300 args << QDir::toNativeSeparators(outputFile);
302 if(!metaInfo.title().isEmpty()) args << QString("-meta:title=%1").arg(cleanTag(metaInfo.title()));
303 if(!metaInfo.artist().isEmpty()) args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.artist()));
304 if(!metaInfo.album().isEmpty()) args << QString("-meta:album=%1").arg(cleanTag(metaInfo.album()));
305 if(!metaInfo.genre().isEmpty()) args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.genre()));
306 if(!metaInfo.comment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.comment()));
307 if(metaInfo.year()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.year()));
308 if(metaInfo.position()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.position()));
309 if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
311 if(!startProcess(process, m_binary_tag, args))
313 return false;
316 bTimeout = false;
318 while(process.state() != QProcess::NotRunning)
320 if(*abortFlag)
322 process.kill();
323 bAborted = true;
324 emit messageLogged(L1S("\nABORTED BY USER !!!"));
325 break;
327 process.waitForReadyRead(m_processTimeoutInterval);
328 if(!process.bytesAvailable() && process.state() == QProcess::Running)
330 process.kill();
331 qWarning("NeroAacTag process timed out <-- killing!");
332 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
333 bTimeout = true;
334 break;
336 while(process.bytesAvailable() > 0)
338 QByteArray line = process.readLine();
339 QString text = QString::fromUtf8(line.constData()).simplified();
340 if(!text.isEmpty())
342 emit messageLogged(text);
347 process.waitForFinished();
348 if(process.state() != QProcess::NotRunning)
350 process.kill();
351 process.waitForFinished(-1);
354 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
356 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
358 return false;
361 return true;
364 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
366 if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
368 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
370 return true;
374 return false;
377 void AACEncoder::setProfile(int profile)
379 m_configProfile = profile;
382 void AACEncoder::setEnable2Pass(bool enabled)
384 m_configEnable2Pass = enabled;
387 const bool AACEncoder::needsTimingInfo(void)
389 return true;
392 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
394 return &g_aacEncoderInfo;