OSDN Git Service

Improved Russian & Spanish translations for the installer
[lamexp/LameXP.git] / src / Encoder_AAC.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.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
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 AACEncoderInfo : public AbstractEncoderInfo
41 {
42         virtual bool isModeSupported(int mode) const
43         {
44                 switch(mode)
45                 {
46                 case SettingsModel::VBRMode:
47                 case SettingsModel::ABRMode:
48                 case SettingsModel::CBRMode:
49                         return true;
50                         break;
51                 default:
52                         MUTILS_THROW("Bad RC mode specified!");
53                 }
54         }
55
56         virtual int valueCount(int mode) const
57         {
58                 switch(mode)
59                 {
60                 case SettingsModel::VBRMode:
61                         return 21;
62                         break;
63                 case SettingsModel::ABRMode:
64                 case SettingsModel::CBRMode:
65                         return 41;
66                         break;
67                 default:
68                         MUTILS_THROW("Bad RC mode specified!");
69                 }
70         }
71
72         virtual int valueAt(int mode, int index) const
73         {
74                 switch(mode)
75                 {
76                 case SettingsModel::VBRMode:
77                         return qBound(0, index * 5, 100);
78                         break;
79                 case SettingsModel::ABRMode:
80                 case SettingsModel::CBRMode:
81                         return qBound(8, index2bitrate(index), 400);
82                         break;
83                 default:
84                         MUTILS_THROW("Bad RC mode specified!");
85                 }
86         }
87
88         virtual int valueType(int mode) const
89         {
90                 switch(mode)
91                 {
92                 case SettingsModel::VBRMode:
93                         return TYPE_QUALITY_LEVEL_FLT;
94                         break;
95                 case SettingsModel::ABRMode:
96                         return TYPE_APPROX_BITRATE;
97                         break;
98                 case SettingsModel::CBRMode:
99                         return TYPE_BITRATE;
100                         break;
101                 default:
102                         MUTILS_THROW("Bad RC mode specified!");
103                 }
104         }
105
106         virtual const char *description(void) const
107         {
108                 static const char* s_description = "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
109                 return s_description;
110         }
111
112         virtual const char *extension(void) const
113         {
114                 static const char* s_extension = "mp4";
115                 return s_extension;
116         }
117 }
118 static const g_aacEncoderInfo;
119
120 ///////////////////////////////////////////////////////////////////////////////
121 // Encoder implementation
122 ///////////////////////////////////////////////////////////////////////////////
123
124 AACEncoder::AACEncoder(void)
125 :
126         m_binary_enc(lamexp_tools_lookup("neroAacEnc.exe")),
127         m_binary_tag(lamexp_tools_lookup("neroAacTag.exe")),
128         m_binary_sox(lamexp_tools_lookup("sox.exe"))
129 {
130         if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty() || m_binary_sox.isEmpty())
131         {
132                 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
133         }
134
135         m_configProfile = 0;
136         m_configEnable2Pass = true;
137 }
138
139 AACEncoder::~AACEncoder(void)
140 {
141 }
142
143 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
144 {
145         QProcess process;
146         QStringList args;
147         const QString baseName = QFileInfo(outputFile).fileName();
148
149         switch(m_configRCMode)
150         {
151         case SettingsModel::VBRMode:
152                 args << "-q" << QString().sprintf("%.2f", double(qBound(0, m_configBitrate * 5, 100)) / 100.0);
153                 break;
154         case SettingsModel::ABRMode:
155                 args << "-br" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
156                 break;
157         case SettingsModel::CBRMode:
158                 args << "-cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
159                 break;
160         default:
161                 MUTILS_THROW("Bad rate-control mode!");
162                 break;
163         }
164
165         if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
166         {
167                 args << "-2pass";
168         }
169         
170         switch(m_configProfile)
171         {
172         case 1:
173                 args << "-lc"; //Forces use of LC AAC profile
174                 break;
175         case 2:
176                 args << "-he"; //Forces use of HE AAC profile
177                 break;
178         case 3:
179                 args << "-hev2"; //Forces use of HEv2 AAC profile
180                 break;
181         }
182
183         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
184
185         args << "-if" << QDir::toNativeSeparators(sourceFile);
186         args << "-of" << QDir::toNativeSeparators(outputFile);
187
188         if(!startProcess(process, m_binary_enc, args))
189         {
190                 return false;
191         }
192
193         bool bTimeout = false;
194         bool bAborted = false;
195         int prevProgress = -1;
196
197
198         QRegExp regExp("Processed\\s+(\\d+)\\s+seconds");
199         QRegExp regExp_pass1("First\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
200         QRegExp regExp_pass2("Second\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds");
201
202         while(process.state() != QProcess::NotRunning)
203         {
204                 if(*abortFlag)
205                 {
206                         process.kill();
207                         bAborted = true;
208                         emit messageLogged("\nABORTED BY USER !!!");
209                         break;
210                 }
211                 process.waitForReadyRead(m_processTimeoutInterval);
212                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
213                 {
214                         process.kill();
215                         qWarning("NeroAacEnc process timed out <-- killing!");
216                         emit messageLogged("\nPROCESS TIMEOUT !!!");
217                         bTimeout = true;
218                         break;
219                 }
220                 while(process.bytesAvailable() > 0)
221                 {
222                         QByteArray line = process.readLine();
223                         QString text = QString::fromUtf8(line.constData()).simplified();
224                         if(regExp_pass1.lastIndexIn(text) >= 0)
225                         {
226                                 bool ok = false;
227                                 int progress = regExp_pass1.cap(1).toInt(&ok);
228                                 if(ok && (duration > 0))
229                                 {
230                                         int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0);
231                                         if(newProgress > prevProgress)
232                                         {
233                                                 emit statusUpdated(newProgress);
234                                                 prevProgress = qMin(newProgress + 2, 99);
235                                         }
236                                 }
237                         }
238                         else if(regExp_pass2.lastIndexIn(text) >= 0)
239                         {
240                                 bool ok = false;
241                                 int progress = regExp_pass2.cap(1).toInt(&ok);
242                                 if(ok && (duration > 0))
243                                 {
244                                         int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 50.0) + 50;
245                                         if(newProgress > prevProgress)
246                                         {
247                                                 emit statusUpdated(newProgress);
248                                                 prevProgress = qMin(newProgress + 2, 99);
249                                         }
250                                 }
251                         }
252                         else if(regExp.lastIndexIn(text) >= 0)
253                         {
254                                 bool ok = false;
255                                 int progress = regExp.cap(1).toInt(&ok);
256                                 if(ok && (duration > 0))
257                                 {
258                                         int newProgress = qRound((static_cast<double>(progress) / static_cast<double>(duration)) * 100.0);
259                                         if(newProgress > prevProgress)
260                                         {
261                                                 emit statusUpdated(newProgress);
262                                                 prevProgress = qMin(newProgress + 2, 99);
263                                         }
264                                 }
265                         }
266                         else if(!text.isEmpty())
267                         {
268                                 emit messageLogged(text);
269                         }
270                 }
271         }
272
273         process.waitForFinished();
274         if(process.state() != QProcess::NotRunning)
275         {
276                 process.kill();
277                 process.waitForFinished(-1);
278         }
279         
280         emit statusUpdated(100);
281         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
282
283         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
284         {
285                 return false;
286         }
287
288         if(metaInfo.empty(false))
289         {
290                 return true;
291         }
292
293         emit messageLogged("\n-------------------------------\n");
294         
295         args.clear();
296         args << QDir::toNativeSeparators(outputFile);
297
298         if(!metaInfo.title().isEmpty())   args << QString("-meta:title=%1").arg(cleanTag(metaInfo.title()));
299         if(!metaInfo.artist().isEmpty())  args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.artist()));
300         if(!metaInfo.album().isEmpty())   args << QString("-meta:album=%1").arg(cleanTag(metaInfo.album()));
301         if(!metaInfo.genre().isEmpty())   args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.genre()));
302         if(!metaInfo.comment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.comment()));
303         if(metaInfo.year())               args << QString("-meta:year=%1").arg(QString::number(metaInfo.year()));
304         if(metaInfo.position())           args << QString("-meta:track=%1").arg(QString::number(metaInfo.position()));
305         if(!metaInfo.cover().isEmpty())   args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
306         
307         if(!startProcess(process, m_binary_tag, args))
308         {
309                 return false;
310         }
311
312         bTimeout = false;
313
314         while(process.state() != QProcess::NotRunning)
315         {
316                 if(*abortFlag)
317                 {
318                         process.kill();
319                         bAborted = true;
320                         emit messageLogged("\nABORTED BY USER !!!");
321                         break;
322                 }
323                 process.waitForReadyRead(m_processTimeoutInterval);
324                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
325                 {
326                         process.kill();
327                         qWarning("NeroAacTag process timed out <-- killing!");
328                         emit messageLogged("\nPROCESS TIMEOUT !!!");
329                         bTimeout = true;
330                         break;
331                 }
332                 while(process.bytesAvailable() > 0)
333                 {
334                         QByteArray line = process.readLine();
335                         QString text = QString::fromUtf8(line.constData()).simplified();
336                         if(!text.isEmpty())
337                         {
338                                 emit messageLogged(text);
339                         }
340                 }
341         }
342
343         process.waitForFinished();
344         if(process.state() != QProcess::NotRunning)
345         {
346                 process.kill();
347                 process.waitForFinished(-1);
348         }
349                 
350         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
351
352         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
353         {
354                 return false;
355         }
356
357         return true;
358 }
359
360 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
361 {
362         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
363         {
364                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
365                 {
366                         return true;
367                 }
368         }
369
370         return false;
371 }
372
373 void AACEncoder::setProfile(int profile)
374 {
375         m_configProfile = profile;
376 }
377
378 void AACEncoder::setEnable2Pass(bool enabled)
379 {
380         m_configEnable2Pass = enabled;
381 }
382
383 const bool AACEncoder::needsTimingInfo(void)
384 {
385         return true;
386 }
387
388 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
389 {
390         return &g_aacEncoderInfo;
391 }