Adapted for the latest MUtils library changes.
[LameXP.git] / src / Encoder_AAC_FHG.cpp
blobd2e4720846e538d0b31fd1d121df2193c259168d
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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_FHG.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <math.h>
29 #include <QProcess>
30 #include <QDir>
32 static int index2bitrate(const int index)
34 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
37 ///////////////////////////////////////////////////////////////////////////////
38 // Encoder Info
39 ///////////////////////////////////////////////////////////////////////////////
41 class FHGAACEncoderInfo : public AbstractEncoderInfo
43 virtual bool isModeSupported(int mode) const
45 switch(mode)
47 case SettingsModel::VBRMode:
48 case SettingsModel::CBRMode:
49 return true;
50 break;
51 case SettingsModel::ABRMode:
52 return false;
53 break;
54 default:
55 MUTILS_THROW("Bad RC mode specified!");
59 virtual int valueCount(int mode) const
61 switch(mode)
63 case SettingsModel::VBRMode:
64 return 6;
65 break;
66 case SettingsModel::ABRMode:
67 case SettingsModel::CBRMode:
68 return 52;
69 break;
70 default:
71 MUTILS_THROW("Bad RC mode specified!");
75 virtual int valueAt(int mode, int index) const
77 switch(mode)
79 case SettingsModel::VBRMode:
80 return qBound(1, index + 1, 6);
81 break;
82 case SettingsModel::ABRMode:
83 case SettingsModel::CBRMode:
84 return qBound(8, index2bitrate(index), 576);
85 break;
86 default:
87 MUTILS_THROW("Bad RC mode specified!");
91 virtual int valueType(int mode) const
93 switch(mode)
95 case SettingsModel::VBRMode:
96 return TYPE_QUALITY_LEVEL_INT;
97 break;
98 case SettingsModel::ABRMode:
99 return TYPE_APPROX_BITRATE;
100 break;
101 case SettingsModel::CBRMode:
102 return TYPE_BITRATE;
103 break;
104 default:
105 MUTILS_THROW("Bad RC mode specified!");
109 virtual const char *description(void) const
111 static const char* s_description = "fhgaacenc/Winamp (\x0C2\x0A9 Nullsoft)";
112 return s_description;
115 virtual const char *extension(void) const
117 static const char* s_extension = "mp4";
118 return s_extension;
121 virtual bool isResamplingSupported(void) const
123 return false;
126 static const g_fhgAacEncoderInfo;
128 ///////////////////////////////////////////////////////////////////////////////
129 // Encoder implementation
130 ///////////////////////////////////////////////////////////////////////////////
132 FHGAACEncoder::FHGAACEncoder(void)
134 m_binary_enc(lamexp_tools_lookup(L1S("fhgaacenc.exe"))),
135 m_binary_dll(lamexp_tools_lookup(L1S("enc_fhgaac.dll")))
137 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
139 MUTILS_THROW("Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!");
142 m_configProfile = 0;
145 FHGAACEncoder::~FHGAACEncoder(void)
149 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, volatile bool *abortFlag)
151 QProcess process;
152 QStringList args;
154 int maxBitrate = 576;
156 if(m_configRCMode == SettingsModel::CBRMode)
158 switch(m_configProfile)
160 case 1:
161 args << L1S("--profile") << L1S("lc"); //Forces use of LC AAC profile
162 break;
163 case 2:
164 maxBitrate = 128;
165 args << L1S("--profile") << L1S("he"); //Forces use of HE AAC profile
166 break;
167 case 3:
168 maxBitrate = 56;
169 args << L1S("--profile") << L1S("hev2"); //Forces use of HEv2 AAC profile
170 break;
174 switch(m_configRCMode)
176 case SettingsModel::CBRMode:
177 args << L1S("--cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), maxBitrate));
178 break;
179 case SettingsModel::VBRMode:
180 args << L1S("--vbr") << QString::number(qBound(1, m_configBitrate + 1, 6));
181 break;
182 default:
183 MUTILS_THROW("Bad rate-control mode!");
184 break;
187 //args << "--dll" << m_binary_dll;
189 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
191 args << QDir::toNativeSeparators(sourceFile);
192 args << QDir::toNativeSeparators(outputFile);
194 if(!startProcess(process, m_binary_enc, args))
196 return false;
199 bool bTimeout = false;
200 bool bAborted = false;
201 int prevProgress = -1;
203 QRegExp regExp(L1S("Progress:\\s*(\\d+)%"));
205 while(process.state() != QProcess::NotRunning)
207 if(*abortFlag)
209 process.kill();
210 bAborted = true;
211 emit messageLogged(L1S("\nABORTED BY USER !!!"));
212 break;
214 process.waitForReadyRead(m_processTimeoutInterval);
215 if(!process.bytesAvailable() && process.state() == QProcess::Running)
217 process.kill();
218 qWarning("FhgAacEnc process timed out <-- killing!");
219 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
220 bTimeout = true;
221 break;
223 while(process.bytesAvailable() > 0)
225 QByteArray line = process.readLine();
226 QString text = QString::fromUtf8(line.constData()).simplified();
227 if(regExp.lastIndexIn(text) >= 0)
229 bool ok = false;
230 int progress = regExp.cap(1).toInt(&ok);
231 if(ok && (progress > prevProgress))
233 emit statusUpdated(progress);
234 prevProgress = qMin(progress + 2, 99);
237 else if(!text.isEmpty())
239 emit messageLogged(text);
244 process.waitForFinished();
245 if(process.state() != QProcess::NotRunning)
247 process.kill();
248 process.waitForFinished(-1);
251 emit statusUpdated(100);
252 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
254 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
256 return false;
259 return true;
262 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
264 if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
266 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
268 return true;
272 return false;
275 const unsigned int *FHGAACEncoder::supportedChannelCount(void)
277 static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
278 return supportedChannels;
281 const unsigned int *FHGAACEncoder::supportedSamplerates(void)
283 static const unsigned int supportedRates[] = {192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 6000, NULL};
284 return supportedRates;
287 const unsigned int *FHGAACEncoder::supportedBitdepths(void)
289 static const unsigned int supportedBPS[] = {16, 24, NULL};
290 return supportedBPS;
293 void FHGAACEncoder::setProfile(int profile)
295 m_configProfile = profile;
298 const AbstractEncoderInfo *FHGAACEncoder::getEncoderInfo(void)
300 return &g_fhgAacEncoderInfo;