Actually make RegExp-based file renaming work.
[LameXP.git] / src / Encoder_AAC_FDK.cpp
blobfff9dd8ce71f06b9704b5c492d0d975b2ae83b1a
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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_FDK.h"
25 //Internal
26 #include "Global.h"
27 #include "Model_Settings.h"
29 //MUtils
30 #include <MUtils/Global.h>
32 //StdLib
33 #include <math.h>
35 //Qt
36 #include <QProcess>
37 #include <QDir>
38 #include <QCoreApplication>
40 static int index2bitrate(const int index)
42 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
45 ///////////////////////////////////////////////////////////////////////////////
46 // Encoder Info
47 ///////////////////////////////////////////////////////////////////////////////
49 class FDKAACEncoderInfo : public AbstractEncoderInfo
51 virtual bool isModeSupported(int mode) const
53 switch(mode)
55 case SettingsModel::VBRMode:
56 case SettingsModel::CBRMode:
57 return true;
58 break;
59 case SettingsModel::ABRMode:
60 return false;
61 break;
62 default:
63 MUTILS_THROW("Bad RC mode specified!");
67 virtual int valueCount(int mode) const
69 switch(mode)
71 case SettingsModel::VBRMode:
72 return 4;
73 break;
74 case SettingsModel::CBRMode:
75 return 52;
76 break;
77 default:
78 MUTILS_THROW("Bad RC mode specified!");
82 virtual int valueAt(int mode, int index) const
84 switch(mode)
86 case SettingsModel::VBRMode:
87 return qBound(1, index + 1 , 5);
88 break;
89 case SettingsModel::CBRMode:
90 return qBound(8, index2bitrate(index), 576);
91 break;
92 default:
93 MUTILS_THROW("Bad RC mode specified!");
97 virtual int valueType(int mode) const
99 switch(mode)
101 case SettingsModel::VBRMode:
102 return TYPE_QUALITY_LEVEL_INT;
103 break;
104 case SettingsModel::CBRMode:
105 return TYPE_BITRATE;
106 break;
107 default:
108 MUTILS_THROW("Bad RC mode specified!");
112 virtual const char *description(void) const
114 static const char* s_description = "fdkaac (libfdk-aac encoder)";
115 return s_description;
118 static const g_fdkAacEncoderInfo;
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
124 FDKAACEncoder::FDKAACEncoder(void)
126 m_binary(lamexp_tools_lookup("fdkaac.exe"))
128 if(m_binary.isEmpty())
130 MUTILS_THROW("Error initializing FDKAAC. Tool 'fdkaac.exe' is not registred!");
133 m_configProfile = 0;
136 FDKAACEncoder::~FDKAACEncoder(void)
140 bool FDKAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
142 QProcess process;
143 QStringList args;
145 process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
147 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
148 env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
149 process.setProcessEnvironment(env);
151 switch(m_configProfile)
153 case 1:
154 args << "-p" << "2";
155 break;
156 case 2:
157 args << "-p" << "5";
158 break;
159 case 3:
160 args << "-p" << "29";
161 break;
164 switch(m_configRCMode)
166 case SettingsModel::CBRMode:
167 args << "-b" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
168 break;
169 case SettingsModel::VBRMode:
170 args << "-m" << QString::number(qBound(1, m_configBitrate + 1 , 5));
171 break;
172 default:
173 MUTILS_THROW("Bad rate-control mode!");
174 break;
177 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
179 if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
180 if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
181 if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
182 if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
183 if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
184 if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
185 if(metaInfo.position()) args << "--track" << QString::number(metaInfo.position());
187 args << "-o" << QDir::toNativeSeparators(outputFile);
188 args << QDir::toNativeSeparators(sourceFile);
190 if(!startProcess(process, m_binary, args))
192 return false;
195 bool bTimeout = false;
196 bool bAborted = false;
197 int prevProgress = -1;
199 QRegExp regExp("\\[(\\d+)%\\]\\s+(\\d+):(\\d+)");
201 while(process.state() != QProcess::NotRunning)
203 if(*abortFlag)
205 process.kill();
206 bAborted = true;
207 emit messageLogged("\nABORTED BY USER !!!");
208 break;
210 process.waitForReadyRead(m_processTimeoutInterval);
211 if(!process.bytesAvailable() && process.state() == QProcess::Running)
213 process.kill();
214 qWarning("FDKAAC process timed out <-- killing!");
215 emit messageLogged("\nPROCESS TIMEOUT !!!");
216 bTimeout = true;
217 break;
219 while(process.bytesAvailable() > 0)
221 QByteArray line = process.readLine();
222 QString text = QString::fromUtf8(line.constData()).simplified();
223 if(regExp.lastIndexIn(text) >= 0)
225 bool ok = false;
226 int progress = regExp.cap(1).toInt(&ok);
227 if(ok && (progress > prevProgress))
229 emit statusUpdated(progress);
230 prevProgress = qMin(progress + 2, 99);
233 else if(!text.isEmpty())
235 emit messageLogged(text);
240 process.waitForFinished();
241 if(process.state() != QProcess::NotRunning)
243 process.kill();
244 process.waitForFinished(-1);
247 emit statusUpdated(100);
248 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
250 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
252 return false;
255 return true;
258 QString FDKAACEncoder::extension(void)
260 return "mp4";
263 bool FDKAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
265 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
267 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
269 return true;
273 return false;
276 void FDKAACEncoder::setProfile(int profile)
278 m_configProfile = profile;
281 const AbstractEncoderInfo *FDKAACEncoder::getEncoderInfo(void)
283 return &g_fdkAacEncoderInfo;