Make sure the "outer" installer isn't renamed to 'LameXP.exe' or 'LameXP-Portable...
[LameXP.git] / src / Encoder_AAC_FHG.cpp
blobcb1b7a1780f07dc6c1c8132ed01e122a8087ad1d
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 FHGAACEncoder::FHGAACEncoder(void)
36 m_binary_enc(lamexp_lookup_tool("fhgaacenc.exe")),
37 m_binary_dll(lamexp_lookup_tool("enc_fhgaac.dll"))
39 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
41 throw "Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!";
44 m_configProfile = 0;
47 FHGAACEncoder::~FHGAACEncoder(void)
51 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
53 QProcess process;
54 QStringList args;
56 int maxBitrate = 500;
58 if(m_configRCMode == SettingsModel::CBRMode)
60 switch(m_configProfile)
62 case 1:
63 args << "--profile" << "lc"; //Forces use of LC AAC profile
64 break;
65 case 2:
66 maxBitrate = 128;
67 args << "--profile" << "he"; //Forces use of HE AAC profile
68 break;
69 case 3:
70 maxBitrate = 56;
71 args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
72 break;
76 switch(m_configRCMode)
78 case SettingsModel::CBRMode:
79 args << "--cbr" << QString::number(max(32, min(maxBitrate, (m_configBitrate * 8))));
80 break;
81 case SettingsModel::VBRMode:
82 args << "--vbr" << QString::number(qRound(static_cast<double>(m_configBitrate) / 5.0) + 1);
83 break;
84 default:
85 throw "Bad rate-control mode!";
86 break;
89 args << "--dll" << m_binary_dll;
91 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
93 args << QDir::toNativeSeparators(sourceFile);
94 args << QDir::toNativeSeparators(outputFile);
96 if(!startProcess(process, m_binary_enc, args))
98 return false;
101 bool bTimeout = false;
102 bool bAborted = false;
104 QRegExp regExp("Progress:\\s*(\\d+)%");
106 while(process.state() != QProcess::NotRunning)
108 if(*abortFlag)
110 process.kill();
111 bAborted = true;
112 emit messageLogged("\nABORTED BY USER !!!");
113 break;
115 process.waitForReadyRead(m_processTimeoutInterval);
116 if(!process.bytesAvailable() && process.state() == QProcess::Running)
118 process.kill();
119 qWarning("FhgAacEnc process timed out <-- killing!");
120 emit messageLogged("\nPROCESS TIMEOUT !!!");
121 bTimeout = true;
122 break;
124 while(process.bytesAvailable() > 0)
126 QByteArray line = process.readLine();
127 QString text = QString::fromUtf8(line.constData()).simplified();
128 if(regExp.lastIndexIn(text) >= 0)
130 bool ok = false;
131 int progress = regExp.cap(1).toInt(&ok);
132 if(ok) emit statusUpdated(progress);
134 else if(!text.isEmpty())
136 emit messageLogged(text);
141 process.waitForFinished();
142 if(process.state() != QProcess::NotRunning)
144 process.kill();
145 process.waitForFinished(-1);
148 emit statusUpdated(100);
149 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
151 if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
153 return false;
156 return true;
159 QString FHGAACEncoder::extension(void)
161 return "mp4";
164 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
166 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
168 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
170 return true;
174 return false;
178 void FHGAACEncoder::setProfile(int profile)
180 m_configProfile = profile;