Properly detect Windows 8, now that Qt supports it officially.
[LameXP.git] / src / Encoder_AAC_FHG.cpp
blobf5a16632e02e016d4e54a0d21f5b9414afaca868
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_FHG.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
31 static int index2bitrate(const int index)
33 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
36 ///////////////////////////////////////////////////////////////////////////////
37 // Encoder Info
38 ///////////////////////////////////////////////////////////////////////////////
40 class FHGAACEncoderInfo : public AbstractEncoderInfo
42 virtual bool isModeSupported(int mode) const
44 switch(mode)
46 case SettingsModel::VBRMode:
47 case SettingsModel::CBRMode:
48 return true;
49 break;
50 case SettingsModel::ABRMode:
51 return false;
52 break;
53 default:
54 throw "Bad RC mode specified!";
58 virtual int valueCount(int mode) const
60 switch(mode)
62 case SettingsModel::VBRMode:
63 return 6;
64 break;
65 case SettingsModel::ABRMode:
66 case SettingsModel::CBRMode:
67 return 52;
68 break;
69 default:
70 throw "Bad RC mode specified!";
74 virtual int valueAt(int mode, int index) const
76 switch(mode)
78 case SettingsModel::VBRMode:
79 return qBound(1, index + 1, 6);
80 break;
81 case SettingsModel::ABRMode:
82 case SettingsModel::CBRMode:
83 return qBound(8, index2bitrate(index), 576);
84 break;
85 default:
86 throw "Bad RC mode specified!";
90 virtual int valueType(int mode) const
92 switch(mode)
94 case SettingsModel::VBRMode:
95 return TYPE_QUALITY_LEVEL_INT;
96 break;
97 case SettingsModel::ABRMode:
98 return TYPE_APPROX_BITRATE;
99 break;
100 case SettingsModel::CBRMode:
101 return TYPE_BITRATE;
102 break;
103 default:
104 throw "Bad RC mode specified!";
108 virtual const char *description(void) const
110 static const char* s_description = "fhgaacenc/Winamp (\x0C2\x0A9 Nullsoft)";
111 return s_description;
114 static const g_fhgAacEncoderInfo;
116 ///////////////////////////////////////////////////////////////////////////////
117 // Encoder implementation
118 ///////////////////////////////////////////////////////////////////////////////
120 FHGAACEncoder::FHGAACEncoder(void)
122 m_binary_enc(lamexp_lookup_tool("fhgaacenc.exe")),
123 m_binary_dll(lamexp_lookup_tool("enc_fhgaac.dll"))
125 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
127 throw "Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!";
130 m_configProfile = 0;
133 FHGAACEncoder::~FHGAACEncoder(void)
137 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
139 QProcess process;
140 QStringList args;
142 int maxBitrate = 576;
144 if(m_configRCMode == SettingsModel::CBRMode)
146 switch(m_configProfile)
148 case 1:
149 args << "--profile" << "lc"; //Forces use of LC AAC profile
150 break;
151 case 2:
152 maxBitrate = 128;
153 args << "--profile" << "he"; //Forces use of HE AAC profile
154 break;
155 case 3:
156 maxBitrate = 56;
157 args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
158 break;
162 switch(m_configRCMode)
164 case SettingsModel::CBRMode:
165 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), maxBitrate));
166 break;
167 case SettingsModel::VBRMode:
168 args << "--vbr" << QString::number(qBound(1, m_configBitrate + 1, 6));
169 break;
170 default:
171 throw "Bad rate-control mode!";
172 break;
175 args << "--dll" << m_binary_dll;
177 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
179 args << QDir::toNativeSeparators(sourceFile);
180 args << QDir::toNativeSeparators(outputFile);
182 if(!startProcess(process, m_binary_enc, args))
184 return false;
187 bool bTimeout = false;
188 bool bAborted = false;
189 int prevProgress = -1;
191 QRegExp regExp("Progress:\\s*(\\d+)%");
193 while(process.state() != QProcess::NotRunning)
195 if(*abortFlag)
197 process.kill();
198 bAborted = true;
199 emit messageLogged("\nABORTED BY USER !!!");
200 break;
202 process.waitForReadyRead(m_processTimeoutInterval);
203 if(!process.bytesAvailable() && process.state() == QProcess::Running)
205 process.kill();
206 qWarning("FhgAacEnc process timed out <-- killing!");
207 emit messageLogged("\nPROCESS TIMEOUT !!!");
208 bTimeout = true;
209 break;
211 while(process.bytesAvailable() > 0)
213 QByteArray line = process.readLine();
214 QString text = QString::fromUtf8(line.constData()).simplified();
215 if(regExp.lastIndexIn(text) >= 0)
217 bool ok = false;
218 int progress = regExp.cap(1).toInt(&ok);
219 if(ok && (progress > prevProgress))
221 emit statusUpdated(progress);
222 prevProgress = qMin(progress + 2, 99);
225 else if(!text.isEmpty())
227 emit messageLogged(text);
232 process.waitForFinished();
233 if(process.state() != QProcess::NotRunning)
235 process.kill();
236 process.waitForFinished(-1);
239 emit statusUpdated(100);
240 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
242 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
244 return false;
247 return true;
250 QString FHGAACEncoder::extension(void)
252 return "mp4";
255 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
257 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
259 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
261 return true;
265 return false;
268 const unsigned int *FHGAACEncoder::supportedChannelCount(void)
270 static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
271 return supportedChannels;
274 const unsigned int *FHGAACEncoder::supportedSamplerates(void)
276 static const unsigned int supportedRates[] = {192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 6000, NULL};
277 return supportedRates;
280 const unsigned int *FHGAACEncoder::supportedBitdepths(void)
282 static const unsigned int supportedBPS[] = {16, 24, NULL};
283 return supportedBPS;
286 void FHGAACEncoder::setProfile(int profile)
288 m_configProfile = profile;
291 const AbstractEncoderInfo *FHGAACEncoder::getEncoderInfo(void)
293 return &g_fhgAacEncoderInfo;