Added Japanese translation, thanks to Maboroshin <pc.genkaku.in>.
[LameXP.git] / src / Encoder_AAC.cpp
blob50e4aab8db52114f50d65dd29a9f0830ba3ad958
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2019 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
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.
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.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Encoder_AAC.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
31 static int index2bitrate(const int index)
33 return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
36 #define IS_VALID(X) (((X) != 0U) && ((X) != UINT_MAX))
38 ///////////////////////////////////////////////////////////////////////////////
39 // Encoder Info
40 ///////////////////////////////////////////////////////////////////////////////
42 class AACEncoderInfo : public AbstractEncoderInfo
44 virtual bool isModeSupported(int mode) const
46 switch(mode)
48 case SettingsModel::VBRMode:
49 case SettingsModel::ABRMode:
50 case SettingsModel::CBRMode:
51 return true;
52 break;
53 default:
54 MUTILS_THROW("Bad RC mode specified!");
58 virtual int valueCount(int mode) const
60 switch(mode)
62 case SettingsModel::VBRMode:
63 return 21;
64 break;
65 case SettingsModel::ABRMode:
66 case SettingsModel::CBRMode:
67 return 41;
68 break;
69 default:
70 MUTILS_THROW("Bad RC mode specified!");
74 virtual int valueAt(int mode, int index) const
76 switch(mode)
78 case SettingsModel::VBRMode:
79 return qBound(0, index * 5, 100);
80 break;
81 case SettingsModel::ABRMode:
82 case SettingsModel::CBRMode:
83 return qBound(8, index2bitrate(index), 400);
84 break;
85 default:
86 MUTILS_THROW("Bad RC mode specified!");
90 virtual int valueType(int mode) const
92 switch(mode)
94 case SettingsModel::VBRMode:
95 return TYPE_QUALITY_LEVEL_FLT;
96 break;
97 case SettingsModel::ABRMode:
98 return TYPE_APPROX_BITRATE;
99 break;
100 case SettingsModel::CBRMode:
101 return TYPE_BITRATE;
102 break;
103 default:
104 MUTILS_THROW("Bad RC mode specified!");
108 virtual const char *description(void) const
110 static const char* s_description = "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
111 return s_description;
114 virtual const char *extension(void) const
116 static const char* s_extension = "mp4";
117 return s_extension;
120 virtual bool isResamplingSupported(void) const
122 return false;
125 static const g_aacEncoderInfo;
127 ///////////////////////////////////////////////////////////////////////////////
128 // Encoder implementation
129 ///////////////////////////////////////////////////////////////////////////////
131 AACEncoder::AACEncoder(void)
133 m_binary_enc(lamexp_tools_lookup(L1S("neroAacEnc.exe"))),
134 m_binary_tag(lamexp_tools_lookup(L1S("neroAacTag.exe")))
136 if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
138 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
141 m_configProfile = 0;
142 m_configEnable2Pass = true;
145 AACEncoder::~AACEncoder(void)
149 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
151 QProcess process;
152 QStringList args;
153 const QString baseName = QFileInfo(outputFile).fileName();
155 switch(m_configRCMode)
157 case SettingsModel::VBRMode:
158 args << L1S("-q") << QString().sprintf("%.2f", double(qBound(0, m_configBitrate * 5, 100)) / 100.0);
159 break;
160 case SettingsModel::ABRMode:
161 args << L1S("-br") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
162 break;
163 case SettingsModel::CBRMode:
164 args << L1S("-cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), 400) * 1000);
165 break;
166 default:
167 MUTILS_THROW("Bad rate-control mode!");
168 break;
171 if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
173 args << L1S("-2pass");
176 int selectedProfile = m_configProfile;
177 if ((selectedProfile == 3) && IS_VALID(channels) && (channels != 2))
179 emit messageLogged("WARNING: Cannot use HE-AAC v2 (SBR+PS) with Mono input --> reverting to HE-AAC (SBR)");
180 selectedProfile = 2;
183 switch(selectedProfile)
185 case 0:
186 //Do *not* overwrite profile -> let the encoder decide!
187 break;
188 case 1:
189 args << L1S("-lc"); //Forces use of LC AAC profile
190 break;
191 case 2:
192 args << L1S("-he"); //Forces use of HE AAC profile
193 break;
194 case 3:
195 args << L1S("-hev2"); //Forces use of HEv2 AAC profile
196 break;
197 default:
198 MUTILS_THROW("Bad AAC Profile specified!");
201 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
203 args << L1S("-if") << QDir::toNativeSeparators(sourceFile);
204 args << L1S("-of") << QDir::toNativeSeparators(outputFile);
206 if(!startProcess(process, m_binary_enc, args))
208 return false;
211 int prevProgress = -1;
212 QRegExp regExp_sp(L1S("\\bprocessed\\s+(\\d+)\\s+seconds"), Qt::CaseInsensitive);
213 QRegExp regExp_mp(L1S("\\b(\\w+)\\s+pass:\\s+processed\\s+(\\d+)\\s+seconds"), Qt::CaseInsensitive);
215 const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &duration, &regExp_sp, &regExp_mp](const QString &text)
217 if (regExp_mp.lastIndexIn(text) >= 0)
219 int timeElapsed;
220 if ((duration > 0) && MUtils::regexp_parse_int32(regExp_mp, timeElapsed, 2))
222 const bool second_pass = (regExp_mp.cap(1).compare(L1S("second"), Qt::CaseInsensitive) == 0);
223 const int newProgress = qRound((second_pass ? 50.0 : 0.0) + ((static_cast<double>(timeElapsed) / static_cast<double>(duration)) * 50.0));
224 if (newProgress > prevProgress)
226 emit statusUpdated(newProgress);
227 prevProgress = NEXT_PROGRESS(newProgress);
230 return true;
232 if (regExp_sp.lastIndexIn(text) >= 0)
234 int timeElapsed;
235 if ((duration > 0) && MUtils::regexp_parse_int32(regExp_sp, timeElapsed))
237 const int newProgress = qRound((static_cast<double>(timeElapsed) / static_cast<double>(duration)) * 100.0);
238 if (newProgress > prevProgress)
240 emit statusUpdated(newProgress);
241 prevProgress = NEXT_PROGRESS(newProgress);
244 return true;
246 return false;
249 if(result != RESULT_SUCCESS)
251 return false;
254 if(metaInfo.empty(false))
256 return true;
259 emit messageLogged(L1S("\n-------------------------------\n"));
261 args.clear();
262 args << QDir::toNativeSeparators(outputFile);
264 if(!metaInfo.title().isEmpty()) args << QString("-meta:title=%1").arg(cleanTag(metaInfo.title()));
265 if(!metaInfo.artist().isEmpty()) args << QString("-meta:artist=%1").arg(cleanTag(metaInfo.artist()));
266 if(!metaInfo.album().isEmpty()) args << QString("-meta:album=%1").arg(cleanTag(metaInfo.album()));
267 if(!metaInfo.genre().isEmpty()) args << QString("-meta:genre=%1").arg(cleanTag(metaInfo.genre()));
268 if(!metaInfo.comment().isEmpty()) args << QString("-meta:comment=%1").arg(cleanTag(metaInfo.comment()));
269 if(metaInfo.year()) args << QString("-meta:year=%1").arg(QString::number(metaInfo.year()));
270 if(metaInfo.position()) args << QString("-meta:track=%1").arg(QString::number(metaInfo.position()));
271 if(!metaInfo.cover().isEmpty()) args << QString("-add-cover:%1:%2").arg("front", metaInfo.cover());
273 if(!startProcess(process, m_binary_tag, args))
275 return false;
278 return (awaitProcess(process, abortFlag) == RESULT_SUCCESS);
281 bool AACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
283 if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
285 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
287 return true;
291 return false;
294 const unsigned int *AACEncoder::supportedSamplerates(void)
296 static const unsigned int supportedRates[] = { 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
297 return supportedRates;
300 void AACEncoder::setProfile(int profile)
302 m_configProfile = qBound(0, profile, 3);
305 void AACEncoder::setEnable2Pass(bool enabled)
307 m_configEnable2Pass = enabled;
310 const bool AACEncoder::needsTimingInfo(void)
312 return true;
315 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
317 return &g_aacEncoderInfo;