Properly detect Windows 8, now that Qt supports it officially.
[LameXP.git] / src / Encoder_AAC.cpp
blobe4554e7c40ba3d3ede68779d7cfd31e836be693e
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
9 //
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.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
30 static int index2bitrate(const int index)
32 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
35 ///////////////////////////////////////////////////////////////////////////////
36 // Encoder Info
37 ///////////////////////////////////////////////////////////////////////////////
39 class AACEncoderInfo : public AbstractEncoderInfo
41 virtual bool isModeSupported(int mode) const
43 switch(mode)
45 case SettingsModel::VBRMode:
46 case SettingsModel::ABRMode:
47 case SettingsModel::CBRMode:
48 return true;
49 break;
50 default:
51 throw "Bad RC mode specified!";
55 virtual int valueCount(int mode) const
57 switch(mode)
59 case SettingsModel::VBRMode:
60 return 21;
61 break;
62 case SettingsModel::ABRMode:
63 case SettingsModel::CBRMode:
64 return 41;
65 break;
66 default:
67 throw "Bad RC mode specified!";
71 virtual int valueAt(int mode, int index) const
73 switch(mode)
75 case SettingsModel::VBRMode:
76 return qBound(0, index * 5, 100);
77 break;
78 case SettingsModel::ABRMode:
79 case SettingsModel::CBRMode:
80 return qBound(8, index2bitrate(index), 400);
81 break;
82 default:
83 throw "Bad RC mode specified!";
87 virtual int valueType(int mode) const
89 switch(mode)
91 case SettingsModel::VBRMode:
92 return TYPE_QUALITY_LEVEL_FLT;
93 break;
94 case SettingsModel::ABRMode:
95 return TYPE_APPROX_BITRATE;
96 break;
97 case SettingsModel::CBRMode:
98 return TYPE_BITRATE;
99 break;
100 default:
101 throw "Bad RC mode specified!";
105 virtual const char *description(void) const
107 static const char* s_description = "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
108 return s_description;
111 static const g_aacEncoderInfo;
113 ///////////////////////////////////////////////////////////////////////////////
114 // Encoder implementation
115 ///////////////////////////////////////////////////////////////////////////////
117 AACEncoder::AACEncoder(void)
119 m_binary_enc(lamexp_lookup_tool("neroAacEnc.exe")),
120 m_binary_tag(lamexp_lookup_tool("neroAacTag.exe")),
121 m_binary_sox(lamexp_lookup_tool("sox.exe"))
123 if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty() || m_binary_sox.isEmpty())
125 throw "Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!";
128 m_configProfile = 0;
129 m_configEnable2Pass = true;
132 AACEncoder::~AACEncoder(void)
136 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
138 QProcess process;
139 QStringList args;
140 const QString baseName = QFileInfo(outputFile).fileName();
142 switch(m_configRCMode)
144 case SettingsModel::VBRMode:
145 args << "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate * 5, 100)) / 100.0);
146 break;
147 case SettingsModel::ABRMode:
148 args << "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
149 break;
150 case SettingsModel::CBRMode:
151 args << "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
152 break;
153 default:
154 throw "Bad rate-control mode!";
155 break;
158 if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
160 args << "-2pass";
163 switch(m_configProfile)
165 case 1:
166 args << "-lc"; //Forces use of LC AAC profile
167 break;
168 case 2:
169 args << "-he"; //Forces use of HE AAC profile
170 break;
171 case 3:
172 args << "-hev2"; //Forces use of HEv2 AAC profile
173 break;
176 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
178 args << "-if" << QDir::toNativeSeparators(sourceFile);
179 args << "-of" << QDir::toNativeSeparators(outputFile);
181 if(!startProcess(process, m_binary_enc, args))
183 return false;
186 bool bTimeout = false;
187 bool bAborted = false;
188 int prevProgress = -1;
191 QRegExp regExp("Processed\\s+(\\d+)\\s+seconds");
192 QRegExp regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
193 QRegExp regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
195 while(process.state() != QProcess::NotRunning)
197 if(*abortFlag)
199 process.kill();
200 bAborted = true;
201 emit messageLogged("\nABORTED BY USER !!!");
202 break;
204 process.waitForReadyRead(m_processTimeoutInterval);
205 if(!process.bytesAvailable() && process.state() == QProcess::Running)
207 process.kill();
208 qWarning("NeroAacEnc process timed out <-- killing!");
209 emit messageLogged("\nPROCESS TIMEOUT !!!");
210 bTimeout = true;
211 break;
213 while(process.bytesAvailable() > 0)
215 QByteArray line = process.readLine();
216 QString text = QString::fromUtf8(line.constData()).simplified();
217 if(regExp_pass1.lastIndexIn(text) >= 0)
219 bool ok = false;
220 int progress = regExp_pass1.cap(1).toInt(&ok);
221 if(ok && (duration > 0))
223 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0);
224 if(newProgress > prevProgress)
226 emit statusUpdated(newProgress);
227 prevProgress = qMin(newProgress + 2, 99);
231 else if(regExp_pass2.lastIndexIn(text) >= 0)
233 bool ok = false;
234 int progress = regExp_pass2.cap(1).toInt(&ok);
235 if(ok && (duration > 0))
237 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0) + 50;
238 if(newProgress > prevProgress)
240 emit statusUpdated(newProgress);
241 prevProgress = qMin(newProgress + 2, 99);
245 else if(regExp.lastIndexIn(text) >= 0)
247 bool ok = false;
248 int progress = regExp.cap(1).toInt(&ok);
249 if(ok && (duration > 0))
251 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 100.0);
252 if(newProgress > prevProgress)
254 emit statusUpdated(newProgress);
255 prevProgress = qMin(newProgress + 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 emit messageLogged("\n-------------------------------\n");
283 args.clear();
284 args << QDir::toNativeSeparators(outputFile);
286 if(!metaInfo.title().isEmpty()) args << QString("-meta:title=%1").arg(cleanTag(metaInfo.title()));
287 if(!metaInfo.artist().isEmpty()) args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.artist()));
288 if(!metaInfo.album().isEmpty()) args << QString("-meta:album=%1").arg(cleanTag(metaInfo.album()));
289 if(!metaInfo.genre().isEmpty()) args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.genre()));
290 if(!metaInfo.comment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.comment()));
291 if(metaInfo.year()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.year()));
292 if(metaInfo.position()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.position()));
293 if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
295 if(!startProcess(process, m_binary_tag, args))
297 return false;
300 bTimeout = false;
302 while(process.state() != QProcess::NotRunning)
304 if(*abortFlag)
306 process.kill();
307 bAborted = true;
308 emit messageLogged("\nABORTED BY USER !!!");
309 break;
311 process.waitForReadyRead(m_processTimeoutInterval);
312 if(!process.bytesAvailable() && process.state() == QProcess::Running)
314 process.kill();
315 qWarning("NeroAacTag process timed out <-- killing!");
316 emit messageLogged("\nPROCESS TIMEOUT !!!");
317 bTimeout = true;
318 break;
320 while(process.bytesAvailable() > 0)
322 QByteArray line = process.readLine();
323 QString text = QString::fromUtf8(line.constData()).simplified();
324 if(!text.isEmpty())
326 emit messageLogged(text);
331 process.waitForFinished();
332 if(process.state() != QProcess::NotRunning)
334 process.kill();
335 process.waitForFinished(-1);
338 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
340 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
342 return false;
345 return true;
348 QString AACEncoder::extension(void)
350 return "mp4";
353 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
355 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
357 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
359 return true;
363 return false;
366 void AACEncoder::setProfile(int profile)
368 m_configProfile = profile;
371 void AACEncoder::setEnable2Pass(bool enabled)
373 m_configEnable2Pass = enabled;
376 const bool AACEncoder::needsTimingInfo(void)
378 return true;
381 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
383 return &g_aacEncoderInfo;