Fixed a typo, thanks to Rub3n CT for reporting.
[LameXP.git] / src / Encoder_AAC_FHG.cpp
blobaf0b5bc936ea8fdea170edd74837e6e38cdd654f
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_FHG.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
31 #define max(a,b) (((a) > (b)) ? (a) : (b))
32 #define min(a,b) (((a) < (b)) ? (a) : (b))
34 inline int round(double x) { return static_cast<int>(floor((x < 0.0) ? x - 0.5 : x + 0.5)); }
36 FHGAACEncoder::FHGAACEncoder(void)
38 m_binary_enc(lamexp_lookup_tool("fhgaacenc.exe")),
39 m_binary_dll(lamexp_lookup_tool("enc_fhgaac.dll"))
41 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
43 throw "Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!";
46 m_configProfile = 0;
49 FHGAACEncoder::~FHGAACEncoder(void)
53 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
55 QProcess process;
56 QStringList args;
58 int maxBitrate = 500;
60 if(m_configRCMode == SettingsModel::CBRMode)
62 switch(m_configProfile)
64 case 1:
65 args << "--profile" << "lc"; //Forces use of LC AAC profile
66 break;
67 case 2:
68 maxBitrate = 128;
69 args << "--profile" << "he"; //Forces use of HE AAC profile
70 break;
71 case 3:
72 maxBitrate = 56;
73 args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
74 break;
78 switch(m_configRCMode)
80 case SettingsModel::CBRMode:
81 args << "--cbr" << QString::number(max(32, min(maxBitrate, (m_configBitrate * 8))));
82 break;
83 case SettingsModel::VBRMode:
84 args << "--vbr" << QString::number(round(static_cast<double>(m_configBitrate) / 5.0) + 1);
85 break;
86 default:
87 throw "Bad rate-control mode!";
88 break;
91 args << "--dll" << m_binary_dll;
93 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
95 args << QDir::toNativeSeparators(sourceFile);
96 args << QDir::toNativeSeparators(outputFile);
98 if(!startProcess(process, m_binary_enc, args))
100 return false;
103 bool bTimeout = false;
104 bool bAborted = false;
106 QRegExp regExp("Progress:\\s*(\\d+)%");
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("FhgAacEnc 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.lastIndexIn(text) >= 0)
132 bool ok = false;
133 int progress = regExp.cap(1).toInt(&ok);
134 if(ok) emit statusUpdated(progress);
136 else if(!text.isEmpty())
138 emit messageLogged(text);
143 process.waitForFinished();
144 if(process.state() != QProcess::NotRunning)
146 process.kill();
147 process.waitForFinished(-1);
150 emit statusUpdated(100);
151 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
153 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
155 return false;
158 return true;
161 QString FHGAACEncoder::extension(void)
163 return "mp4";
166 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
168 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
170 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
172 return true;
176 return false;
180 void FHGAACEncoder::setProfile(int profile)
182 m_configProfile = profile;