Major redesign of the AudioFileModel class: Split data into separate AudioFileModel_M...
[LameXP.git] / src / Encoder_AAC.cpp
blob56c430a8b58d29c7eff3450e0ff5d03e66b34c0c
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, const QString &outputFile, volatile bool *abortFlag)
138 const unsigned int fileDuration = metaInfo.fileDuration();
140 QProcess process;
141 QStringList args;
142 const QString baseName = QFileInfo(outputFile).fileName();
144 switch(m_configRCMode)
146 case SettingsModel::VBRMode:
147 args << "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate * 5, 100)) / 100.0);
148 break;
149 case SettingsModel::ABRMode:
150 args << "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
151 break;
152 case SettingsModel::CBRMode:
153 args << "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
154 break;
155 default:
156 throw "Bad rate-control mode!";
157 break;
160 if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
162 args << "-2pass";
165 switch(m_configProfile)
167 case 1:
168 args << "-lc"; //Forces use of LC AAC profile
169 break;
170 case 2:
171 args << "-he"; //Forces use of HE AAC profile
172 break;
173 case 3:
174 args << "-hev2"; //Forces use of HEv2 AAC profile
175 break;
178 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
180 args << "-if" << QDir::toNativeSeparators(sourceFile);
181 args << "-of" << QDir::toNativeSeparators(outputFile);
183 if(!startProcess(process, m_binary_enc, args))
185 return false;
188 bool bTimeout = false;
189 bool bAborted = false;
190 int prevProgress = -1;
193 QRegExp regExp("Processed\\s+(\\d+)\\s+seconds");
194 QRegExp regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
195 QRegExp regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
197 while(process.state() != QProcess::NotRunning)
199 if(*abortFlag)
201 process.kill();
202 bAborted = true;
203 emit messageLogged("\nABORTED BY USER !!!");
204 break;
206 process.waitForReadyRead(m_processTimeoutInterval);
207 if(!process.bytesAvailable() && process.state() == QProcess::Running)
209 process.kill();
210 qWarning("NeroAacEnc process timed out <-- killing!");
211 emit messageLogged("\nPROCESS TIMEOUT !!!");
212 bTimeout = true;
213 break;
215 while(process.bytesAvailable() > 0)
217 QByteArray line = process.readLine();
218 QString text = QString::fromUtf8(line.constData()).simplified();
219 if(regExp_pass1.lastIndexIn(text) >= 0)
221 bool ok = false;
222 int progress = regExp_pass1.cap(1).toInt(&ok);
223 if(ok && (fileDuration > 0))
225 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 50.0);
226 if(newProgress > prevProgress)
228 emit statusUpdated(newProgress);
229 prevProgress = qMin(newProgress + 2, 99);
233 else if(regExp_pass2.lastIndexIn(text) >= 0)
235 bool ok = false;
236 int progress = regExp_pass2.cap(1).toInt(&ok);
237 if(ok && (fileDuration > 0))
239 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 50.0) + 50;
240 if(newProgress > prevProgress)
242 emit statusUpdated(newProgress);
243 prevProgress = qMin(newProgress + 2, 99);
247 else if(regExp.lastIndexIn(text) >= 0)
249 bool ok = false;
250 int progress = regExp.cap(1).toInt(&ok);
251 if(ok && (fileDuration > 0))
253 int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(fileDuration)) * 100.0);
254 if(newProgress > prevProgress)
256 emit statusUpdated(newProgress);
257 prevProgress = qMin(newProgress + 2, 99);
261 else if(!text.isEmpty())
263 emit messageLogged(text);
268 process.waitForFinished();
269 if(process.state() != QProcess::NotRunning)
271 process.kill();
272 process.waitForFinished(-1);
275 emit statusUpdated(100);
276 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
278 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
280 return false;
283 emit messageLogged("\n-------------------------------\n");
285 args.clear();
286 args << QDir::toNativeSeparators(outputFile);
288 if(!metaInfo.fileName().isEmpty()) args << QString("-meta:title=%1").arg(cleanTag(metaInfo.fileName()));
289 if(!metaInfo.fileArtist().isEmpty()) args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.fileArtist()));
290 if(!metaInfo.fileAlbum().isEmpty()) args << QString("-meta:album=%1").arg(cleanTag(metaInfo.fileAlbum()));
291 if(!metaInfo.fileGenre().isEmpty()) args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.fileGenre()));
292 if(!metaInfo.fileComment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.fileComment()));
293 if(metaInfo.fileYear()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.fileYear()));
294 if(metaInfo.filePosition()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.filePosition()));
295 if(!metaInfo.fileCover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.fileCover());
297 if(!startProcess(process, m_binary_tag, args))
299 return false;
302 bTimeout = false;
304 while(process.state() != QProcess::NotRunning)
306 if(*abortFlag)
308 process.kill();
309 bAborted = true;
310 emit messageLogged("\nABORTED BY USER !!!");
311 break;
313 process.waitForReadyRead(m_processTimeoutInterval);
314 if(!process.bytesAvailable() && process.state() == QProcess::Running)
316 process.kill();
317 qWarning("NeroAacTag process timed out <-- killing!");
318 emit messageLogged("\nPROCESS TIMEOUT !!!");
319 bTimeout = true;
320 break;
322 while(process.bytesAvailable() > 0)
324 QByteArray line = process.readLine();
325 QString text = QString::fromUtf8(line.constData()).simplified();
326 if(!text.isEmpty())
328 emit messageLogged(text);
333 process.waitForFinished();
334 if(process.state() != QProcess::NotRunning)
336 process.kill();
337 process.waitForFinished(-1);
340 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
342 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
344 return false;
347 return true;
350 QString AACEncoder::extension(void)
352 return "mp4";
355 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
357 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
359 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
361 return true;
365 return false;
368 void AACEncoder::setProfile(int profile)
370 m_configProfile = profile;
373 void AACEncoder::setEnable2Pass(bool enabled)
375 m_configEnable2Pass = enabled;
378 const bool AACEncoder::needsTimingInfo(void)
380 return true;
383 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
385 return &g_aacEncoderInfo;