OSDN Git Service

85de4aa83e903195580819f1cf7626d7449cfd64
[lamexp/LameXP.git] / src / Encoder_AAC_QAAC.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_QAAC.h"
24
25 //Internal
26 #include "Global.h"
27 #include "Model_Settings.h"
28
29 //MUtils
30 #include <MUtils/Global.h>
31
32 //StdLib
33 #include <math.h>
34
35 //Qt
36 #include <QProcess>
37 #include <QDir>
38 #include <QCoreApplication>
39
40 static int index2bitrate(const int index)
41 {
42         return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
43 }
44
45 static const int g_qaacVBRQualityLUT[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX};
46
47 ///////////////////////////////////////////////////////////////////////////////
48 // Encoder Info
49 ///////////////////////////////////////////////////////////////////////////////
50
51 class QAACEncoderInfo : public AbstractEncoderInfo
52 {
53         virtual bool isModeSupported(int mode) const
54         {
55                 switch(mode)
56                 {
57                 case SettingsModel::VBRMode:
58                 case SettingsModel::CBRMode:
59                 case SettingsModel::ABRMode:
60                         return true;
61                         break;
62                 default:
63                         MUTILS_THROW("Bad RC mode specified!");
64                 }
65         }
66
67         virtual int valueCount(int mode) const
68         {
69                 switch(mode)
70                 {
71                 case SettingsModel::VBRMode:
72                         return 15;
73                         break;
74                 case SettingsModel::ABRMode:
75                 case SettingsModel::CBRMode:
76                         return 52;
77                         break;
78                 default:
79                         MUTILS_THROW("Bad RC mode specified!");
80                 }
81         }
82
83         virtual int valueAt(int mode, int index) const
84         {
85                 switch(mode)
86                 {
87                 case SettingsModel::VBRMode:
88                         return g_qaacVBRQualityLUT[qBound(0, index , 14)];
89                         break;
90                 case SettingsModel::ABRMode:
91                 case SettingsModel::CBRMode:
92                         return qBound(8, index2bitrate(index), 576);
93                         break;
94                 default:
95                         MUTILS_THROW("Bad RC mode specified!");
96                 }
97         }
98
99         virtual int valueType(int mode) const
100         {
101                 switch(mode)
102                 {
103                 case SettingsModel::VBRMode:
104                         return TYPE_QUALITY_LEVEL_INT;
105                         break;
106                 case SettingsModel::ABRMode:
107                         return TYPE_APPROX_BITRATE;
108                         break;
109                 case SettingsModel::CBRMode:
110                         return TYPE_BITRATE;
111                         break;
112                 default:
113                         MUTILS_THROW("Bad RC mode specified!");
114                 }
115         }
116
117         virtual const char *description(void) const
118         {
119                 static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Apple Inc.)";
120                 return s_description;
121         }
122 }
123 static const g_qaacEncoderInfo;
124
125 ///////////////////////////////////////////////////////////////////////////////
126 // Encoder implementation
127 ///////////////////////////////////////////////////////////////////////////////
128
129 QAACEncoder::QAACEncoder(void)
130 :
131         m_binary_qaac(lamexp_tools_lookup("qaac.exe")),
132         m_binary_soxr(lamexp_tools_lookup("libsoxr.dll")),
133         m_binary_soxc(lamexp_tools_lookup("libsoxconvolver.dll"))
134 {
135         if(m_binary_qaac.isEmpty() || m_binary_soxr.isEmpty() || m_binary_soxc.isEmpty())
136         {
137                 MUTILS_THROW("Error initializing QAAC. Tool 'qaac.exe' is not registred!");
138         }
139
140         m_configProfile = 0;
141 }
142
143 QAACEncoder::~QAACEncoder(void)
144 {
145 }
146
147 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
148 {
149         QProcess process;
150         QStringList args;
151
152         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
153
154         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
155         env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
156         process.setProcessEnvironment(env);
157
158         if(m_configRCMode != SettingsModel::VBRMode)
159         {
160                 switch(m_configProfile)
161                 {
162                 case 2:
163                 case 3:
164                         args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
165                         break;
166                 }
167         }
168
169         switch(m_configRCMode)
170         {
171         case SettingsModel::CBRMode:
172                 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
173                 break;
174         case SettingsModel::ABRMode:
175                 args << "--abr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
176                 break;
177         case SettingsModel::VBRMode:
178                 args << "--tvbr" << QString::number(g_qaacVBRQualityLUT[qBound(0, m_configBitrate , 14)]);
179                 break;
180         default:
181                 MUTILS_THROW("Bad rate-control mode!");
182                 break;
183         }
184
185         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
186
187         if(!metaInfo.title().isEmpty())   args << "--title"   << cleanTag(metaInfo.title());
188         if(!metaInfo.artist().isEmpty())  args << "--artist"  << cleanTag(metaInfo.artist());
189         if(!metaInfo.album().isEmpty())   args << "--album"   << cleanTag(metaInfo.album());
190         if(!metaInfo.genre().isEmpty())   args << "--genre"   << cleanTag(metaInfo.genre());
191         if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
192         if(metaInfo.year())               args << "--date"    << QString::number(metaInfo.year());
193         if(metaInfo.position())           args << "--track"   << QString::number(metaInfo.position());
194         if(!metaInfo.cover().isEmpty())   args << "--artwork" << metaInfo.cover();
195
196         args << "-d" << ".";
197         args << "-o" << QDir::toNativeSeparators(outputFile);
198         args << QDir::toNativeSeparators(sourceFile);
199
200         if(!startProcess(process, m_binary_qaac, args))
201         {
202                 return false;
203         }
204
205         bool bTimeout = false;
206         bool bAborted = false;
207         int prevProgress = -1;
208
209         QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
210
211         while(process.state() != QProcess::NotRunning)
212         {
213                 if(*abortFlag)
214                 {
215                         process.kill();
216                         bAborted = true;
217                         emit messageLogged("\nABORTED BY USER !!!");
218                         break;
219                 }
220                 process.waitForReadyRead(m_processTimeoutInterval);
221                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
222                 {
223                         process.kill();
224                         qWarning("QAAC process timed out <-- killing!");
225                         emit messageLogged("\nPROCESS TIMEOUT !!!");
226                         bTimeout = true;
227                         break;
228                 }
229                 while(process.bytesAvailable() > 0)
230                 {
231                         QByteArray line = process.readLine();
232                         QString text = QString::fromUtf8(line.constData()).simplified();
233                         if(regExp.lastIndexIn(text) >= 0)
234                         {
235                                 bool ok = false;
236                                 int progress = regExp.cap(1).toInt(&ok);
237                                 if(ok && (progress > prevProgress))
238                                 {
239                                         emit statusUpdated(progress);
240                                         prevProgress = qMin(progress + 2, 99);
241                                 }
242                         }
243                         else if(!text.isEmpty())
244                         {
245                                 emit messageLogged(text);
246                         }
247                 }
248         }
249
250         process.waitForFinished();
251         if(process.state() != QProcess::NotRunning)
252         {
253                 process.kill();
254                 process.waitForFinished(-1);
255         }
256         
257         emit statusUpdated(100);
258         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
259
260         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
261         {
262                 return false;
263         }
264
265         return true;
266 }
267
268 QString QAACEncoder::extension(void)
269 {
270         return "mp4";
271 }
272
273 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
274 {
275         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
276         {
277                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
278                 {
279                         return true;
280                 }
281         }
282
283         return false;
284 }
285
286 void QAACEncoder::setProfile(int profile)
287 {
288         m_configProfile = profile;
289 }
290
291 const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
292 {
293         return &g_qaacEncoderInfo;
294 }