OSDN Git Service

Changed detection of QAAC for the new fully-static build.
[lamexp/LameXP.git] / src / Encoder_AAC_QAAC.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 QAACEncoder::QAACEncoder(void)
33 :
34         m_binary_enc(lamexp_lookup_tool("qaac.exe"))
35 {
36         if(m_binary_enc.isEmpty())
37         {
38                 throw "Error initializing QAAC. Tool 'qaac.exe' is not registred!";
39         }
40
41         m_configProfile = 0;
42 }
43
44 QAACEncoder::~QAACEncoder(void)
45 {
46 }
47
48 bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
49 {
50         QProcess process;
51         QStringList args;
52
53         process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());
54
55         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
56         env.insert("PATH", QString("%1;%2").arg(QDir::toNativeSeparators(QDir(QCoreApplication::applicationDirPath()).canonicalPath()), QDir::toNativeSeparators(lamexp_temp_folder2())));
57         env.insert("TEMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
58         env.insert("TMP", QDir::toNativeSeparators(lamexp_temp_folder2()));
59         process.setProcessEnvironment(env);
60
61         if(m_configRCMode != SettingsModel::VBRMode)
62         {
63                 switch(m_configProfile)
64                 {
65                 case 2:
66                 case 3:
67                         args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
68                         break;
69                 }
70         }
71
72         switch(m_configRCMode)
73         {
74         case SettingsModel::CBRMode:
75                 args << "--cbr" << QString::number(qBound(32, m_configBitrate * 8, 500));
76                 break;
77         case SettingsModel::ABRMode:
78                 args << "--abr" << QString::number(qBound(32, m_configBitrate * 8, 500));
79                 break;
80         case SettingsModel::VBRMode:
81                 args << "--tvbr" << QString::number(qBound(0, qRound((static_cast<double>(m_configBitrate * 5) / 100.0) * 127.0), 127));
82                 break;
83         default:
84                 throw "Bad rate-control mode!";
85                 break;
86         }
87
88         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
89
90         if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
91         if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
92         if(!metaInfo.fileAlbum().isEmpty()) args << "--album" << metaInfo.fileAlbum();
93         if(!metaInfo.fileGenre().isEmpty()) args << "--genre" << metaInfo.fileGenre();
94         if(!metaInfo.fileComment().isEmpty()) args << "--comment" << metaInfo.fileComment();
95         if(metaInfo.fileYear()) args << "--date" << QString::number(metaInfo.fileYear());
96         if(metaInfo.filePosition()) args << "--track" << QString::number(metaInfo.filePosition());
97         if(!metaInfo.fileCover().isEmpty()) args << "--artwork" << metaInfo.fileCover();
98
99         args << "-d" << ".";
100         args << "-o" << QDir::toNativeSeparators(outputFile);
101         args << QDir::toNativeSeparators(sourceFile);
102
103         if(!startProcess(process, m_binary_enc, args))
104         {
105                 return false;
106         }
107
108         bool bTimeout = false;
109         bool bAborted = false;
110         int prevProgress = -1;
111
112         QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
113
114         while(process.state() != QProcess::NotRunning)
115         {
116                 if(*abortFlag)
117                 {
118                         process.kill();
119                         bAborted = true;
120                         emit messageLogged("\nABORTED BY USER !!!");
121                         break;
122                 }
123                 process.waitForReadyRead(m_processTimeoutInterval);
124                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
125                 {
126                         process.kill();
127                         qWarning("QAAC process timed out <-- killing!");
128                         emit messageLogged("\nPROCESS TIMEOUT !!!");
129                         bTimeout = true;
130                         break;
131                 }
132                 while(process.bytesAvailable() > 0)
133                 {
134                         QByteArray line = process.readLine();
135                         QString text = QString::fromUtf8(line.constData()).simplified();
136                         if(regExp.lastIndexIn(text) >= 0)
137                         {
138                                 bool ok = false;
139                                 int progress = regExp.cap(1).toInt(&ok);
140                                 if(ok && (progress > prevProgress))
141                                 {
142                                         emit statusUpdated(progress);
143                                         prevProgress = qMin(progress + 2, 99);
144                                 }
145                         }
146                         else if(!text.isEmpty())
147                         {
148                                 emit messageLogged(text);
149                         }
150                 }
151         }
152
153         process.waitForFinished();
154         if(process.state() != QProcess::NotRunning)
155         {
156                 process.kill();
157                 process.waitForFinished(-1);
158         }
159         
160         emit statusUpdated(100);
161         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
162
163         if(bTimeout || bAborted || process.exitStatus() != QProcess::NormalExit)
164         {
165                 return false;
166         }
167
168         return true;
169 }
170
171 QString QAACEncoder::extension(void)
172 {
173         return "mp4";
174 }
175
176 bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
177 {
178         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
179         {
180                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
181                 {
182                         return true;
183                 }
184         }
185
186         return false;
187 }
188
189
190 void QAACEncoder::setProfile(int profile)
191 {
192         m_configProfile = profile;
193 }