Added indicators for current CPU usage, RAM usage and free disk space to the processi...
[LameXP.git] / src / Encoder_AAC.cpp
blobb4e29675da085131d8c3b00e99b4d1e779f122e2
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 #define max(a,b) (((a) > (b)) ? (a) : (b))
31 #define min(a,b) (((a) < (b)) ? (a) : (b))
33 AACEncoder::AACEncoder(void)
35 m_binary_enc(lamexp_lookup_tool("neroAacEnc.exe")),
36 m_binary_tag(lamexp_lookup_tool("neroAacTag.exe"))
38 if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
40 throw "Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!";
43 m_configProfile = 0;
44 m_configEnable2Pass = true;
47 AACEncoder::~AACEncoder(void)
51 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
53 QProcess process;
54 QStringList args;
55 const QString baseName = QFileInfo(outputFile).fileName();
57 switch(m_configRCMode)
59 case SettingsModel::VBRMode:
60 args << "-q" << QString().sprintf("%.2f", min(1.0, max(0.0, static_cast<double>(m_configBitrate * 5) / 100.0)));
61 break;
62 case SettingsModel::ABRMode:
63 args << "-br" << QString::number(max(32, min(500, (m_configBitrate * 8))) * 1000);
64 break;
65 case SettingsModel::CBRMode:
66 args << "-cbr" << QString::number(max(32, min(500, (m_configBitrate * 8))) * 1000);
67 break;
68 default:
69 throw "Bad rate-control mode!";
70 break;
73 if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
75 args << "-2pass";
78 switch(m_configProfile)
80 case 1:
81 args << "-lc"; //Forces use of LC AAC profile
82 break;
83 case 2:
84 args << "-he"; //Forces use of HE AAC profile
85 break;
86 case 3:
87 args << "-hev2"; //Forces use of HEv2 AAC profile
88 break;
91 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
93 args << "-if" << QDir::toNativeSeparators(sourceFile);
94 args << "-of" << QDir::toNativeSeparators(outputFile);
96 if(!startProcess(process, m_binary_enc, args))
98 return false;
101 bool bTimeout = false;
102 bool bAborted = false;
104 QRegExp regExp("Processed\\s+(\\d+)\\s+seconds");
105 QRegExp regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
106 QRegExp regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
108 while(process.state() != QProcess::NotRunning)
110 if(*abortFlag)
112 process.kill();
113 bAborted = true;
114 emit messageLogged("\nABORTED BY USER !!!");
115 break;
117 process.waitForReadyRead(m_processTimeoutInterval);
118 if(!process.bytesAvailable() && process.state() == QProcess::Running)
120 process.kill();
121 qWarning("NeroAacEnc process timed out <-- killing!");
122 emit messageLogged("\nPROCESS TIMEOUT !!!");
123 bTimeout = true;
124 break;
126 while(process.bytesAvailable() > 0)
128 QByteArray line = process.readLine();
129 QString text = QString::fromUtf8(line.constData()).simplified();
130 if(regExp_pass1.lastIndexIn(text) >= 0)
132 bool ok = false;
133 int progress = regExp_pass1.cap(1).toInt(&ok);
134 if(ok && metaInfo.fileDuration() > 0)
136 emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 50.0));
139 else if(regExp_pass2.lastIndexIn(text) >= 0)
141 bool ok = false;
142 int progress = regExp_pass2.cap(1).toInt(&ok);
143 if(ok && metaInfo.fileDuration() > 0)
145 emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 50.0) + 50);
148 else if(regExp.lastIndexIn(text) >= 0)
150 bool ok = false;
151 int progress = regExp.cap(1).toInt(&ok);
152 if(ok && metaInfo.fileDuration() > 0)
154 emit statusUpdated(static_cast<int>((static_cast<double>(progress) / static_cast<double>(metaInfo.fileDuration())) * 100.0));
157 else if(!text.isEmpty())
159 emit messageLogged(text);
164 process.waitForFinished();
165 if(process.state() != QProcess::NotRunning)
167 process.kill();
168 process.waitForFinished(-1);
171 emit statusUpdated(100);
172 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
174 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
176 return false;
179 emit messageLogged("\n-------------------------------\n");
181 args.clear();
182 args << QDir::toNativeSeparators(outputFile);
184 if(!metaInfo.fileName().isEmpty()) args << QString("-meta:title=%1").arg(metaInfo.fileName());
185 if(!metaInfo.fileArtist().isEmpty()) args << QString("-meta:artist=%1").arg(metaInfo.fileArtist());
186 if(!metaInfo.fileAlbum().isEmpty()) args << QString("-meta:album=%1").arg(metaInfo.fileAlbum());
187 if(!metaInfo.fileGenre().isEmpty()) args << QString("-meta:genre=%1").arg(metaInfo.fileGenre());
188 if(!metaInfo.fileComment().isEmpty()) args << QString("-meta:comment=%1").arg(metaInfo.fileComment());
189 if(metaInfo.fileYear()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.fileYear()));
190 if(metaInfo.filePosition()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.filePosition()));
191 if(!metaInfo.fileCover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.fileCover());
193 if(!startProcess(process, m_binary_tag, args))
195 return false;
198 bTimeout = false;
200 while(process.state() != QProcess::NotRunning)
202 if(*abortFlag)
204 process.kill();
205 bAborted = true;
206 emit messageLogged("\nABORTED BY USER !!!");
207 break;
209 process.waitForReadyRead(m_processTimeoutInterval);
210 if(!process.bytesAvailable() && process.state() == QProcess::Running)
212 process.kill();
213 qWarning("NeroAacTag process timed out <-- killing!");
214 emit messageLogged("\nPROCESS TIMEOUT !!!");
215 bTimeout = true;
216 break;
218 while(process.bytesAvailable() > 0)
220 QByteArray line = process.readLine();
221 QString text = QString::fromUtf8(line.constData()).simplified();
222 if(!text.isEmpty())
224 emit messageLogged(text);
229 process.waitForFinished();
230 if(process.state() != QProcess::NotRunning)
232 process.kill();
233 process.waitForFinished(-1);
236 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
238 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
240 return false;
243 return true;
246 QString AACEncoder::extension(void)
248 return "mp4";
251 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
253 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
255 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
257 return true;
261 return false;
265 void AACEncoder::setProfile(int profile)
267 m_configProfile = profile;
270 void AACEncoder::setEnable2Pass(bool enabled)
272 m_configEnable2Pass = enabled;