OSDN Git Service

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