OSDN Git Service

e8a4d649e10ea4375d1cb3c1170267d9fa103ed3
[lamexp/LameXP.git] / src / Encoder_AAC_QAAC.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_QAAC.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
30 #include <QCoreApplication>
31
32 static int index2bitrate(const int index)
33 {
34         return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
35 }
36
37 static const int g_qaacVBRQualityLUT[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX};
38
39 ///////////////////////////////////////////////////////////////////////////////
40 // Encoder Info
41 ///////////////////////////////////////////////////////////////////////////////
42
43 class QAACEncoderInfo : public AbstractEncoderInfo
44 {
45         virtual bool isModeSupported(int mode) const
46         {
47                 switch(mode)
48                 {
49                 case SettingsModel::VBRMode:
50                 case SettingsModel::CBRMode:
51                 case SettingsModel::ABRMode:
52                         return true;
53                         break;
54                 default:
55                         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 15;
65                         break;
66                 case SettingsModel::ABRMode:
67                 case SettingsModel::CBRMode:
68                         return 52;
69                         break;
70                 default:
71                         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 g_qaacVBRQualityLUT[qBound(0, index , 14)];
81                         break;
82                 case SettingsModel::ABRMode:
83                 case SettingsModel::CBRMode:
84                         return qBound(8, index2bitrate(index), 576);
85                         break;
86                 default:
87                         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                         throw "Bad RC mode specified!";
106                 }
107         }
108
109         virtual const char *description(void) const
110         {
111                 static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Appel)";
112                 return s_description;
113         }
114 }
115 static const g_qaacEncoderInfo;
116
117 ///////////////////////////////////////////////////////////////////////////////
118 // Encoder implementation
119 ///////////////////////////////////////////////////////////////////////////////
120
121 QAACEncoder::QAACEncoder(void)
122 :
123         m_binary_enc(lamexp_lookup_tool("qaac.exe")),
124         m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
125 {
126         if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
127         {
128                 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
129         }
130
131         m_configProfile = 0;
132 }
133
134 QAACEncoder::~QAACEncoder(void)
135 {
136 }
137
138 bool QAACEncoder::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         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
144
145         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
146         env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), lamexp_temp_folder2())));
147         process.setProcessEnvironment(env);
148
149         if(m_configRCMode != SettingsModel::VBRMode)
150         {
151                 switch(m_configProfile)
152                 {
153                 case 2:
154                 case 3:
155                         args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
156                         break;
157                 }
158         }
159
160         switch(m_configRCMode)
161         {
162         case SettingsModel::CBRMode:
163                 args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
164                 break;
165         case SettingsModel::ABRMode:
166                 args << "--abr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
167                 break;
168         case SettingsModel::VBRMode:
169                 args << "--tvbr" << QString::number(g_qaacVBRQualityLUT[qBound(0, m_configBitrate , 14)]);
170                 break;
171         default:
172                 throw "Bad rate-control mode!";
173                 break;
174         }
175
176         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
177
178         if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
179         if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
180         if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
181         if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
182         if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
183         if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
184         if(metaInfo.position()) args << "--track" << QString::number(metaInfo.position());
185         if(!metaInfo.cover().isEmpty()) args << "--artwork" << metaInfo.cover();
186
187         args << "-d" << ".";
188         args << "-o" << QDir::toNativeSeparators(outputFile);
189         args << QDir::toNativeSeparators(sourceFile);
190
191         if(!startProcess(process, m_binary_enc, args))
192         {
193                 return false;
194         }
195
196         bool bTimeout = false;
197         bool bAborted = false;
198         int prevProgress = -1;
199
200         QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
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("QAAC 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.lastIndexIn(text) >= 0)
225                         {
226                                 bool ok = false;
227                                 int progress = regExp.cap(1).toInt(&ok);
228                                 if(ok && (progress > prevProgress))
229                                 {
230                                         emit statusUpdated(progress);
231                                         prevProgress = qMin(progress + 2, 99);
232                                 }
233                         }
234                         else if(!text.isEmpty())
235                         {
236                                 emit messageLogged(text);
237                         }
238                 }
239         }
240
241         process.waitForFinished();
242         if(process.state() != QProcess::NotRunning)
243         {
244                 process.kill();
245                 process.waitForFinished(-1);
246         }
247         
248         emit statusUpdated(100);
249         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
250
251         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
252         {
253                 return false;
254         }
255
256         return true;
257 }
258
259 QString QAACEncoder::extension(void)
260 {
261         return "mp4";
262 }
263
264 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
265 {
266         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
267         {
268                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
269                 {
270                         return true;
271                 }
272         }
273
274         return false;
275 }
276
277 void QAACEncoder::setProfile(int profile)
278 {
279         m_configProfile = profile;
280 }
281
282 const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
283 {
284         return &g_qaacEncoderInfo;
285 }