1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
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"
27 #include "Model_Settings.h"
30 #include <MUtils/Global.h>
38 #include <QCoreApplication>
40 static int index2bitrate(const int index
)
42 return (index
< 32) ? ((index
+ 1) * 8) : ((index
- 15) * 16);
45 ///////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////
49 class FDKAACEncoderInfo
: public AbstractEncoderInfo
51 virtual bool isModeSupported(int mode
) const
55 case SettingsModel::VBRMode
:
56 case SettingsModel::CBRMode
:
59 case SettingsModel::ABRMode
:
63 MUTILS_THROW("Bad RC mode specified!");
67 virtual int valueCount(int mode
) const
71 case SettingsModel::VBRMode
:
74 case SettingsModel::CBRMode
:
78 MUTILS_THROW("Bad RC mode specified!");
82 virtual int valueAt(int mode
, int index
) const
86 case SettingsModel::VBRMode
:
87 return qBound(1, index
+ 1 , 5);
89 case SettingsModel::CBRMode
:
90 return qBound(8, index2bitrate(index
), 576);
93 MUTILS_THROW("Bad RC mode specified!");
97 virtual int valueType(int mode
) const
101 case SettingsModel::VBRMode
:
102 return TYPE_QUALITY_LEVEL_INT
;
104 case SettingsModel::CBRMode
:
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 virtual const char *extension(void) const
120 static const char* s_extension
= "mp4";
124 static const g_fdkAacEncoderInfo
;
126 ///////////////////////////////////////////////////////////////////////////////
127 // Encoder implementation
128 ///////////////////////////////////////////////////////////////////////////////
130 FDKAACEncoder::FDKAACEncoder(void)
132 m_binary(lamexp_tools_lookup("fdkaac.exe"))
134 if(m_binary
.isEmpty())
136 MUTILS_THROW("Error initializing FDKAAC. Tool 'fdkaac.exe' is not registred!");
142 FDKAACEncoder::~FDKAACEncoder(void)
146 bool FDKAACEncoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
151 process
.setWorkingDirectory(QFileInfo(outputFile
).canonicalPath());
153 QProcessEnvironment env
= QProcessEnvironment::systemEnvironment();
154 env
.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
155 process
.setProcessEnvironment(env
);
157 switch(m_configProfile
)
166 args
<< "-p" << "29";
170 switch(m_configRCMode
)
172 case SettingsModel::CBRMode
:
173 args
<< "-b" << QString::number(qBound(8, index2bitrate(m_configBitrate
), 576));
175 case SettingsModel::VBRMode
:
176 args
<< "-m" << QString::number(qBound(1, m_configBitrate
+ 1 , 5));
179 MUTILS_THROW("Bad rate-control mode!");
183 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
185 if(!metaInfo
.title().isEmpty()) args
<< "--title" << cleanTag(metaInfo
.title());
186 if(!metaInfo
.artist().isEmpty()) args
<< "--artist" << cleanTag(metaInfo
.artist());
187 if(!metaInfo
.album().isEmpty()) args
<< "--album" << cleanTag(metaInfo
.album());
188 if(!metaInfo
.genre().isEmpty()) args
<< "--genre" << cleanTag(metaInfo
.genre());
189 if(!metaInfo
.comment().isEmpty()) args
<< "--comment" << cleanTag( metaInfo
.comment());
190 if(metaInfo
.year()) args
<< "--date" << QString::number(metaInfo
.year());
191 if(metaInfo
.position()) args
<< "--track" << QString::number(metaInfo
.position());
193 args
<< "-o" << QDir::toNativeSeparators(outputFile
);
194 args
<< QDir::toNativeSeparators(sourceFile
);
196 if(!startProcess(process
, m_binary
, args
))
201 bool bTimeout
= false;
202 bool bAborted
= false;
203 int prevProgress
= -1;
205 QRegExp
regExp("\\[(\\d+)%\\]\\s+(\\d+):(\\d+)");
207 while(process
.state() != QProcess::NotRunning
)
213 emit
messageLogged("\nABORTED BY USER !!!");
216 process
.waitForReadyRead(m_processTimeoutInterval
);
217 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
220 qWarning("FDKAAC process timed out <-- killing!");
221 emit
messageLogged("\nPROCESS TIMEOUT !!!");
225 while(process
.bytesAvailable() > 0)
227 QByteArray line
= process
.readLine();
228 QString text
= QString::fromUtf8(line
.constData()).simplified();
229 if(regExp
.lastIndexIn(text
) >= 0)
232 int progress
= regExp
.cap(1).toInt(&ok
);
233 if(ok
&& (progress
> prevProgress
))
235 emit
statusUpdated(progress
);
236 prevProgress
= qMin(progress
+ 2, 99);
239 else if(!text
.isEmpty())
241 emit
messageLogged(text
);
246 process
.waitForFinished();
247 if(process
.state() != QProcess::NotRunning
)
250 process
.waitForFinished(-1);
253 emit
statusUpdated(100);
254 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
256 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
264 bool FDKAACEncoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
266 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
268 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
277 void FDKAACEncoder::setProfile(int profile
)
279 m_configProfile
= profile
;
282 const AbstractEncoderInfo
*FDKAACEncoder::getEncoderInfo(void)
284 return &g_fdkAacEncoderInfo
;