OSDN Git Service

Improved Russian & Spanish translations for the installer
[lamexp/LameXP.git] / src / Encoder_AAC_FHG.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Encoder_AAC_FHG.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <math.h>
29 #include <QProcess>
30 #include <QDir>
31
32 static int index2bitrate(const int index)
33 {
34         return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38 // Encoder Info
39 ///////////////////////////////////////////////////////////////////////////////
40
41 class FHGAACEncoderInfo : public AbstractEncoderInfo
42 {
43         virtual bool isModeSupported(int mode) const
44         {
45                 switch(mode)
46                 {
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!");
56                 }
57         }
58
59         virtual int valueCount(int mode) const
60         {
61                 switch(mode)
62                 {
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!");
72                 }
73         }
74
75         virtual int valueAt(int mode, int index) const
76         {
77                 switch(mode)
78                 {
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!");
88                 }
89         }
90
91         virtual int valueType(int mode) const
92         {
93                 switch(mode)
94                 {
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!");
106                 }
107         }
108
109         virtual const char *description(void) const
110         {
111                 static const char* s_description = "fhgaacenc/Winamp (\x0C2\x0A9 Nullsoft)";
112                 return s_description;
113         }
114
115         virtual const char *extension(void) const
116         {
117                 static const char* s_extension = "mp4";
118                 return s_extension;
119         }
120 }
121 static const g_fhgAacEncoderInfo;
122
123 ///////////////////////////////////////////////////////////////////////////////
124 // Encoder implementation
125 ///////////////////////////////////////////////////////////////////////////////
126
127 FHGAACEncoder::FHGAACEncoder(void)
128 :
129         m_binary_enc(lamexp_tools_lookup("fhgaacenc.exe")),
130         m_binary_dll(lamexp_tools_lookup("enc_fhgaac.dll"))
131 {
132         if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
133         {
134                 MUTILS_THROW("Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!");
135         }
136
137         m_configProfile = 0;
138 }
139
140 FHGAACEncoder::~FHGAACEncoder(void)
141 {
142 }
143
144 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
145 {
146         QProcess process;
147         QStringList args;
148         
149         int maxBitrate = 576;
150
151         if(m_configRCMode == SettingsModel::CBRMode)
152         {
153                 switch(m_configProfile)
154                 {
155                 case 1:
156                         args << "--profile" << "lc"; //Forces use of LC AAC profile
157                         break;
158                 case 2:
159                         maxBitrate = 128;
160                         args << "--profile" << "he"; //Forces use of HE AAC profile
161                         break;
162                 case 3:
163                         maxBitrate = 56;
164                         args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
165                         break;
166                 }
167         }
168
169         switch(m_configRCMode)
170         {
171         case SettingsModel::CBRMode:
172                 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), maxBitrate));
173                 break;
174         case SettingsModel::VBRMode:
175                 args << "--vbr" << QString::number(qBound(1, m_configBitrate + 1, 6));
176                 break;
177         default:
178                 MUTILS_THROW("Bad rate-control mode!");
179                 break;
180         }
181
182         args << "--dll" << m_binary_dll;
183
184         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
185
186         args << QDir::toNativeSeparators(sourceFile);
187         args << QDir::toNativeSeparators(outputFile);
188
189         if(!startProcess(process, m_binary_enc, args))
190         {
191                 return false;
192         }
193
194         bool bTimeout = false;
195         bool bAborted = false;
196         int prevProgress = -1;
197
198         QRegExp regExp("Progress:\\s*(\\d+)%");
199
200         while(process.state() != QProcess::NotRunning)
201         {
202                 if(*abortFlag)
203                 {
204                         process.kill();
205                         bAborted = true;
206                         emit messageLogged("\nABORTED BY USER !!!");
207                         break;
208                 }
209                 process.waitForReadyRead(m_processTimeoutInterval);
210                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
211                 {
212                         process.kill();
213                         qWarning("FhgAacEnc process timed out <-- killing!");
214                         emit messageLogged("\nPROCESS TIMEOUT !!!");
215                         bTimeout = true;
216                         break;
217                 }
218                 while(process.bytesAvailable() > 0)
219                 {
220                         QByteArray line = process.readLine();
221                         QString text = QString::fromUtf8(line.constData()).simplified();
222                         if(regExp.lastIndexIn(text) >= 0)
223                         {
224                                 bool ok = false;
225                                 int progress = regExp.cap(1).toInt(&ok);
226                                 if(ok && (progress > prevProgress))
227                                 {
228                                         emit statusUpdated(progress);
229                                         prevProgress = qMin(progress + 2, 99);
230                                 }
231                         }
232                         else if(!text.isEmpty())
233                         {
234                                 emit messageLogged(text);
235                         }
236                 }
237         }
238
239         process.waitForFinished();
240         if(process.state() != QProcess::NotRunning)
241         {
242                 process.kill();
243                 process.waitForFinished(-1);
244         }
245         
246         emit statusUpdated(100);
247         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
248
249         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
250         {
251                 return false;
252         }
253
254         return true;
255 }
256
257 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
258 {
259         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
260         {
261                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
262                 {
263                         return true;
264                 }
265         }
266
267         return false;
268 }
269
270 const unsigned int *FHGAACEncoder::supportedChannelCount(void)
271 {
272         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
273         return supportedChannels;
274 }
275
276 const unsigned int *FHGAACEncoder::supportedSamplerates(void)
277 {
278         static const unsigned int supportedRates[] = {192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 6000, NULL};
279         return supportedRates;
280 }
281
282 const unsigned int *FHGAACEncoder::supportedBitdepths(void)
283 {
284         static const unsigned int supportedBPS[] = {16, 24, NULL};
285         return supportedBPS;
286 }
287
288 void FHGAACEncoder::setProfile(int profile)
289 {
290         m_configProfile = profile;
291 }
292
293 const AbstractEncoderInfo *FHGAACEncoder::getEncoderInfo(void)
294 {
295         return &g_fhgAacEncoderInfo;
296 }