Updated MediaInfo binary to a current SVN/Trunk version with Opus support. Also remov...
[LameXP.git] / src / Encoder_AAC_QAAC.cpp
blob4147d87f4373c39365c948d01e96ff0f75a5755d
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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 QAACEncoder::QAACEncoder(void)
34 m_binary_enc(lamexp_lookup_tool("qaac.exe")),
35 m_binary_dll(lamexp_lookup_tool("libsoxrate.dll"))
37 if(m_binary_enc.isEmpty() || m_binary_dll.isEmpty())
39 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
42 m_configProfile = 0;
45 QAACEncoder::~QAACEncoder(void)
49 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
51 QProcess process;
52 QStringList args;
54 process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
56 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
57 env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), lamexp_temp_folder2())));
58 process.setProcessEnvironment(env);
60 if(m_configRCMode != SettingsModel::VBRMode)
62 switch(m_configProfile)
64 case 2:
65 case 3:
66 args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
67 break;
71 switch(m_configRCMode)
73 case SettingsModel::CBRMode:
74 args << "--cbr" << QString::number(qBound(32, m_configBitrate * 8, 500));
75 break;
76 case SettingsModel::ABRMode:
77 args << "--abr" << QString::number(qBound(32, m_configBitrate * 8, 500));
78 break;
79 case SettingsModel::VBRMode:
80 args << "--tvbr" << QString::number(qBound(0, qRound((static_cast<double>(m_configBitrate * 5) / 100.0) * 127.0), 127));
81 break;
82 default:
83 throw "Bad rate-control mode!";
84 break;
87 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
89 if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
90 if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
91 if(!metaInfo.fileAlbum().isEmpty()) args << "--album" << metaInfo.fileAlbum();
92 if(!metaInfo.fileGenre().isEmpty()) args << "--genre" << metaInfo.fileGenre();
93 if(!metaInfo.fileComment().isEmpty()) args << "--comment" << metaInfo.fileComment();
94 if(metaInfo.fileYear()) args << "--date" << QString::number(metaInfo.fileYear());
95 if(metaInfo.filePosition()) args << "--track" << QString::number(metaInfo.filePosition());
96 if(!metaInfo.fileCover().isEmpty()) args << "--artwork" << metaInfo.fileCover();
98 args << "-d" << ".";
99 args << "-o" << QDir::toNativeSeparators(outputFile);
100 args << QDir::toNativeSeparators(sourceFile);
102 if(!startProcess(process, m_binary_enc, args))
104 return false;
107 bool bTimeout = false;
108 bool bAborted = false;
109 int prevProgress = -1;
111 QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
113 while(process.state() != QProcess::NotRunning)
115 if(*abortFlag)
117 process.kill();
118 bAborted = true;
119 emit messageLogged("\nABORTED BY USER !!!");
120 break;
122 process.waitForReadyRead(m_processTimeoutInterval);
123 if(!process.bytesAvailable() && process.state() == QProcess::Running)
125 process.kill();
126 qWarning("QAAC process timed out <-- killing!");
127 emit messageLogged("\nPROCESS TIMEOUT !!!");
128 bTimeout = true;
129 break;
131 while(process.bytesAvailable() > 0)
133 QByteArray line = process.readLine();
134 QString text = QString::fromUtf8(line.constData()).simplified();
135 if(regExp.lastIndexIn(text) >= 0)
137 bool ok = false;
138 int progress = regExp.cap(1).toInt(&ok);
139 if(ok && (progress > prevProgress))
141 emit statusUpdated(progress);
142 prevProgress = qMin(progress + 2, 99);
145 else if(!text.isEmpty())
147 emit messageLogged(text);
152 process.waitForFinished();
153 if(process.state() != QProcess::NotRunning)
155 process.kill();
156 process.waitForFinished(-1);
159 emit statusUpdated(100);
160 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
162 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
164 return false;
167 return true;
170 QString QAACEncoder::extension(void)
172 return "mp4";
175 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
177 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
179 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
181 return true;
185 return false;
189 void QAACEncoder::setProfile(int profile)
191 m_configProfile = profile;