Major redesign of the AudioFileModel class: Split data into separate AudioFileModel_M...
[LameXP.git] / src / Encoder_AAC_QAAC.cpp
blobd0df476ceb71c482babb1f4e4eefc0e4159de0d0
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Encoder_AAC_QAAC.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <math.h>
28 #include <QProcess>
29 #include <QDir>
30 #include <QCoreApplication>
32 static int index2bitrate(const int index)
34 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
37 static const int g_qaacVBRQualityLUT[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX};
39 ///////////////////////////////////////////////////////////////////////////////
40 // Encoder Info
41 ///////////////////////////////////////////////////////////////////////////////
43 class QAACEncoderInfo : public AbstractEncoderInfo
45 virtual bool isModeSupported(int mode) const
47 switch(mode)
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!";
59 virtual int valueCount(int mode) const
61 switch(mode)
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!";
75 virtual int valueAt(int mode, int index) const
77 switch(mode)
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!";
91 virtual int valueType(int mode) const
93 switch(mode)
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!";
109 virtual const char *description(void) const
111 static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Appel)";
112 return s_description;
115 static const g_qaacEncoderInfo;
117 ///////////////////////////////////////////////////////////////////////////////
118 // Encoder implementation
119 ///////////////////////////////////////////////////////////////////////////////
121 QAACEncoder::QAACEncoder(void)
123 m_binary_enc(lamexp_lookup_tool("qaac.exe")),
124 m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
126 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
128 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
131 m_configProfile = 0;
134 QAACEncoder::~QAACEncoder(void)
138 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
140 QProcess process;
141 QStringList args;
143 process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
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);
149 if(m_configRCMode != SettingsModel::VBRMode)
151 switch(m_configProfile)
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;
160 switch(m_configRCMode)
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;
176 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
178 if(!metaInfo.fileName().isEmpty()) args << "--title" << cleanTag(metaInfo.fileName());
179 if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << cleanTag(metaInfo.fileArtist());
180 if(!metaInfo.fileAlbum().isEmpty()) args << "--album" << cleanTag(metaInfo.fileAlbum());
181 if(!metaInfo.fileGenre().isEmpty()) args << "--genre" << cleanTag(metaInfo.fileGenre());
182 if(!metaInfo.fileComment().isEmpty()) args << "--comment" << cleanTag( metaInfo.fileComment());
183 if(metaInfo.fileYear()) args << "--date" << QString::number(metaInfo.fileYear());
184 if(metaInfo.filePosition()) args << "--track" << QString::number(metaInfo.filePosition());
185 if(!metaInfo.fileCover().isEmpty()) args << "--artwork" << metaInfo.fileCover();
187 args << "-d" << ".";
188 args << "-o" << QDir::toNativeSeparators(outputFile);
189 args << QDir::toNativeSeparators(sourceFile);
191 if(!startProcess(process, m_binary_enc, args))
193 return false;
196 bool bTimeout = false;
197 bool bAborted = false;
198 int prevProgress = -1;
200 QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
202 while(process.state() != QProcess::NotRunning)
204 if(*abortFlag)
206 process.kill();
207 bAborted = true;
208 emit messageLogged("\nABORTED BY USER !!!");
209 break;
211 process.waitForReadyRead(m_processTimeoutInterval);
212 if(!process.bytesAvailable() && process.state() == QProcess::Running)
214 process.kill();
215 qWarning("QAAC process timed out <-- killing!");
216 emit messageLogged("\nPROCESS TIMEOUT !!!");
217 bTimeout = true;
218 break;
220 while(process.bytesAvailable() > 0)
222 QByteArray line = process.readLine();
223 QString text = QString::fromUtf8(line.constData()).simplified();
224 if(regExp.lastIndexIn(text) >= 0)
226 bool ok = false;
227 int progress = regExp.cap(1).toInt(&ok);
228 if(ok && (progress > prevProgress))
230 emit statusUpdated(progress);
231 prevProgress = qMin(progress + 2, 99);
234 else if(!text.isEmpty())
236 emit messageLogged(text);
241 process.waitForFinished();
242 if(process.state() != QProcess::NotRunning)
244 process.kill();
245 process.waitForFinished(-1);
248 emit statusUpdated(100);
249 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
251 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
253 return false;
256 return true;
259 QString QAACEncoder::extension(void)
261 return "mp4";
264 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
266 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
268 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
270 return true;
274 return false;
277 void QAACEncoder::setProfile(int profile)
279 m_configProfile = profile;
282 const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
284 return &g_qaacEncoderInfo;