OSDN Git Service

Updated Simplified Chinese translation, thanks to Hongchuan Zhuang <kidneybean@sohu...
[lamexp/LameXP.git] / src / Encoder_AAC_FDK.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2023 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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_FDK.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 ///////////////////////////////////////////////////////////////////////////////
46 // Encoder Info
47 ///////////////////////////////////////////////////////////////////////////////
48
49 class FDKAACEncoderInfo : public AbstractEncoderInfo
50 {
51         virtual bool isModeSupported(int mode) const
52         {
53                 switch(mode)
54                 {
55                 case SettingsModel::VBRMode:
56                 case SettingsModel::CBRMode:
57                         return true;
58                         break;
59                 case SettingsModel::ABRMode:
60                         return false;
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 5;
73                         break;
74                 case SettingsModel::CBRMode:
75                         return 52;
76                         break;
77                 default:
78                         MUTILS_THROW("Bad RC mode specified!");
79                 }
80         }
81
82         virtual int valueAt(int mode, int index) const
83         {
84                 switch(mode)
85                 {
86                 case SettingsModel::VBRMode:
87                         return qBound(1, index + 1 , 5);
88                         break;
89                 case SettingsModel::CBRMode:
90                         return qBound(8, index2bitrate(index), 576);
91                         break;
92                 default:
93                         MUTILS_THROW("Bad RC mode specified!");
94                 }
95         }
96
97         virtual int valueType(int mode) const
98         {
99                 switch(mode)
100                 {
101                 case SettingsModel::VBRMode:
102                         return TYPE_QUALITY_LEVEL_INT;
103                         break;
104                 case SettingsModel::CBRMode:
105                         return TYPE_BITRATE;
106                         break;
107                 default:
108                         MUTILS_THROW("Bad RC mode specified!");
109                 }
110         }
111
112         virtual const char *description(void) const
113         {
114                 static const char* s_description = "fdkaac (libfdk-aac encoder)";
115                 return s_description;
116         }
117
118         virtual const char *extension(void) const
119         {
120                 static const char* s_extension = "mp4";
121                 return s_extension;
122         }
123
124         virtual bool isResamplingSupported(void) const
125         {
126                 return false;
127         }
128 }
129 static const g_fdkAacEncoderInfo;
130
131 ///////////////////////////////////////////////////////////////////////////////
132 // Encoder implementation
133 ///////////////////////////////////////////////////////////////////////////////
134
135 FDKAACEncoder::FDKAACEncoder(void)
136 :
137         m_binary(lamexp_tools_lookup(L1S("fdkaac.exe")))
138 {
139         if(m_binary.isEmpty())
140         {
141                 MUTILS_THROW("Error initializing FDKAAC. Tool 'fdkaac.exe' is not registred!");
142         }
143
144         m_configProfile = 0;
145 }
146
147 FDKAACEncoder::~FDKAACEncoder(void)
148 {
149 }
150
151 bool FDKAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int /*duration*/, const unsigned int /*channels*/, const QString &outputFile, QAtomicInt &abortFlag)
152 {
153         QProcess process;
154         QStringList args;
155
156         QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
157         env.insert(L1S("PATH"), QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
158         process.setProcessEnvironment(env);
159
160         switch(m_configProfile)
161         {
162         case 1:
163                 args << L1S("-p") << QString::number(2);
164                 break;
165         case 2:
166                 args << L1S("-p") << QString::number(5);
167                 break;
168         case 3:
169                 args << L1S("-p") << QString::number(29);
170                 break;
171         }
172
173         switch(m_configRCMode)
174         {
175         case SettingsModel::CBRMode:
176                 args << L1S("-b") << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
177                 break;
178         case SettingsModel::VBRMode:
179                 args << L1S("-m") << QString::number(qBound(1, m_configBitrate + 1 , 5));
180                 break;
181         default:
182                 MUTILS_THROW("Bad rate-control mode!");
183                 break;
184         }
185
186         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
187
188         if(!metaInfo.title().isEmpty())   args << L1S("--title")   << cleanTag(metaInfo.title());
189         if(!metaInfo.artist().isEmpty())  args << L1S("--artist")  << cleanTag(metaInfo.artist());
190         if(!metaInfo.album().isEmpty())   args << L1S("--album")   << cleanTag(metaInfo.album());
191         if(!metaInfo.genre().isEmpty())   args << L1S("--genre")   << cleanTag(metaInfo.genre());
192         if(!metaInfo.comment().isEmpty()) args << L1S("--comment") << cleanTag( metaInfo.comment());
193         if(metaInfo.year())               args << L1S("--date")    << QString::number(metaInfo.year());
194         if(metaInfo.position())           args << L1S("--track")   << QString::number(metaInfo.position());
195
196         args << L1S("-o") << QDir::toNativeSeparators(outputFile);
197         args << QDir::toNativeSeparators(sourceFile);
198
199         if(!startProcess(process, m_binary, args, QFileInfo(outputFile).canonicalPath()))
200         {
201                 return false;
202         }
203
204         int prevProgress = -1;
205         QRegExp regExp(L1S("\\[(\\d+)%\\]\\s+(\\d+):(\\d+)"));
206
207         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
208         {
209                 if (regExp.lastIndexIn(text) >= 0)
210                 {
211                         qint32 newProgress;
212                         if (MUtils::regexp_parse_int32(regExp, newProgress))
213                         {
214                                 if (newProgress > prevProgress)
215                                 {
216                                         emit statusUpdated(newProgress);
217                                         prevProgress = NEXT_PROGRESS(newProgress);
218                                 }
219                         }
220                         return true;
221                 }
222                 return false;
223         });
224
225         return (result == RESULT_SUCCESS);
226 }
227
228 bool FDKAACEncoder::isFormatSupported(const QString &containerType, const QString& /*containerProfile*/, const QString &formatType, const QString& /*formatProfile*/, const QString& /*formatVersion*/)
229 {
230         if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
231         {
232                 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
233                 {
234                         return true;
235                 }
236         }
237
238         return false;
239 }
240
241 void FDKAACEncoder::setProfile(int profile)
242 {
243         m_configProfile = profile;
244 }
245
246 const AbstractEncoderInfo *FDKAACEncoder::getEncoderInfo(void)
247 {
248         return &g_fdkAacEncoderInfo;
249 }