OSDN Git Service

f8b03601fad62c1f1ebd05261a32b5a7e3ec52bb
[lamexp/LameXP.git] / src / Encoder_AAC_FHG.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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 static const g_fhgAacEncoderInfo;
116
117 ///////////////////////////////////////////////////////////////////////////////
118 // Encoder implementation
119 ///////////////////////////////////////////////////////////////////////////////
120
121 FHGAACEncoder::FHGAACEncoder(void)
122 :
123         m_binary_enc(lamexp_lookup_tool("fhgaacenc.exe")),
124         m_binary_dll(lamexp_lookup_tool("enc_fhgaac.dll"))
125 {
126         if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
127         {
128                 MUTILS_THROW("Error initializing FhgAacEnc. Tool 'fhgaacenc.exe' is not registred!");
129         }
130
131         m_configProfile = 0;
132 }
133
134 FHGAACEncoder::~FHGAACEncoder(void)
135 {
136 }
137
138 bool FHGAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
139 {
140         QProcess process;
141         QStringList args;
142         
143         int maxBitrate = 576;
144
145         if(m_configRCMode == SettingsModel::CBRMode)
146         {
147                 switch(m_configProfile)
148                 {
149                 case 1:
150                         args << "--profile" << "lc"; //Forces use of LC AAC profile
151                         break;
152                 case 2:
153                         maxBitrate = 128;
154                         args << "--profile" << "he"; //Forces use of HE AAC profile
155                         break;
156                 case 3:
157                         maxBitrate = 56;
158                         args << "--profile" << "hev2"; //Forces use of HEv2 AAC profile
159                         break;
160                 }
161         }
162
163         switch(m_configRCMode)
164         {
165         case SettingsModel::CBRMode:
166                 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), maxBitrate));
167                 break;
168         case SettingsModel::VBRMode:
169                 args << "--vbr" << QString::number(qBound(1, m_configBitrate + 1, 6));
170                 break;
171         default:
172                 MUTILS_THROW("Bad rate-control mode!");
173                 break;
174         }
175
176         args << "--dll" << m_binary_dll;
177
178         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
179
180         args << QDir::toNativeSeparators(sourceFile);
181         args << QDir::toNativeSeparators(outputFile);
182
183         if(!startProcess(process, m_binary_enc, args))
184         {
185                 return false;
186         }
187
188         bool bTimeout = false;
189         bool bAborted = false;
190         int prevProgress = -1;
191
192         QRegExp regExp("Progress:\\s*(\\d+)%");
193
194         while(process.state() != QProcess::NotRunning)
195         {
196                 if(*abortFlag)
197                 {
198                         process.kill();
199                         bAborted = true;
200                         emit messageLogged("\nABORTED BY USER !!!");
201                         break;
202                 }
203                 process.waitForReadyRead(m_processTimeoutInterval);
204                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
205                 {
206                         process.kill();
207                         qWarning("FhgAacEnc process timed out <-- killing!");
208                         emit messageLogged("\nPROCESS TIMEOUT !!!");
209                         bTimeout = true;
210                         break;
211                 }
212                 while(process.bytesAvailable() > 0)
213                 {
214                         QByteArray line = process.readLine();
215                         QString text = QString::fromUtf8(line.constData()).simplified();
216                         if(regExp.lastIndexIn(text) >= 0)
217                         {
218                                 bool ok = false;
219                                 int progress = regExp.cap(1).toInt(&ok);
220                                 if(ok && (progress > prevProgress))
221                                 {
222                                         emit statusUpdated(progress);
223                                         prevProgress = qMin(progress + 2, 99);
224                                 }
225                         }
226                         else if(!text.isEmpty())
227                         {
228                                 emit messageLogged(text);
229                         }
230                 }
231         }
232
233         process.waitForFinished();
234         if(process.state() != QProcess::NotRunning)
235         {
236                 process.kill();
237                 process.waitForFinished(-1);
238         }
239         
240         emit statusUpdated(100);
241         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
242
243         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
244         {
245                 return false;
246         }
247
248         return true;
249 }
250
251 QString FHGAACEncoder::extension(void)
252 {
253         return "mp4";
254 }
255
256 bool FHGAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
257 {
258         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
259         {
260                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
261                 {
262                         return true;
263                 }
264         }
265
266         return false;
267 }
268
269 const unsigned int *FHGAACEncoder::supportedChannelCount(void)
270 {
271         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
272         return supportedChannels;
273 }
274
275 const unsigned int *FHGAACEncoder::supportedSamplerates(void)
276 {
277         static const unsigned int supportedRates[] = {192000, 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 6000, NULL};
278         return supportedRates;
279 }
280
281 const unsigned int *FHGAACEncoder::supportedBitdepths(void)
282 {
283         static const unsigned int supportedBPS[] = {16, 24, NULL};
284         return supportedBPS;
285 }
286
287 void FHGAACEncoder::setProfile(int profile)
288 {
289         m_configProfile = profile;
290 }
291
292 const AbstractEncoderInfo *FHGAACEncoder::getEncoderInfo(void)
293 {
294         return &g_fhgAacEncoderInfo;
295 }