OSDN Git Service

Updated changelog.
[lamexp/LameXP.git] / src / Encoder_AAC.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.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QProcess>
29 #include <QDir>
30
31 static int index2bitrate(const int index)
32 {
33         return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
34 }
35
36 #define IS_VALID(X) (((X) != 0U) && ((X) != UINT_MAX))
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // Encoder Info
40 ///////////////////////////////////////////////////////////////////////////////
41
42 class AACEncoderInfo : public AbstractEncoderInfo
43 {
44         virtual bool isModeSupported(int mode) const
45         {
46                 switch(mode)
47                 {
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!");
55                 }
56         }
57
58         virtual int valueCount(int mode) const
59         {
60                 switch(mode)
61                 {
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!");
71                 }
72         }
73
74         virtual int valueAt(int mode, int index) const
75         {
76                 switch(mode)
77                 {
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!");
87                 }
88         }
89
90         virtual int valueType(int mode) const
91         {
92                 switch(mode)
93                 {
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!");
105                 }
106         }
107
108         virtual const char *description(void) const
109         {
110                 static const char* s_description = "Nero AAC Encoder (\x0C2\x0A9 Nero AG)";
111                 return s_description;
112         }
113
114         virtual const char *extension(void) const
115         {
116                 static const char* s_extension = "mp4";
117                 return s_extension;
118         }
119
120         virtual bool isResamplingSupported(void) const
121         {
122                 return false;
123         }
124 }
125 static const g_aacEncoderInfo;
126
127 ///////////////////////////////////////////////////////////////////////////////
128 // Encoder implementation
129 ///////////////////////////////////////////////////////////////////////////////
130
131 AACEncoder::AACEncoder(void)
132 :
133         m_binary_enc(lamexp_tools_lookup(L1S("neroAacEnc.exe"))),
134         m_binary_tag(lamexp_tools_lookup(L1S("neroAacTag.exe")))
135 {
136         if(m_binary_enc.isEmpty() || m_binary_tag.isEmpty())
137         {
138                 MUTILS_THROW("Error initializing AAC encoder. Tool 'neroAacEnc.exe' is not registred!");
139         }
140
141         m_configProfile = 0;
142         m_configEnable2Pass = true;
143 }
144
145 AACEncoder::~AACEncoder(void)
146 {
147 }
148
149 bool AACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
150 {
151         QProcess process;
152         QStringList args;
153         const QString baseName = QFileInfo(outputFile).fileName();
154
155         switch(m_configRCMode)
156         {
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;
169         }
170
171         if(m_configEnable2Pass && (m_configRCMode == SettingsModel::ABRMode))
172         {
173                 args << L1S("-2pass");
174         }
175         
176         int selectedProfile = m_configProfile;
177         if ((selectedProfile == 3) && IS_VALID(channels) && (channels != 2))
178         {
179                 emit messageLogged("WARNING: Cannot use HE-AAC v2 (SBR+PS) with Mono input --> reverting to HE-AAC (SBR)");
180                 selectedProfile = 2;
181         }
182
183         switch(selectedProfile)
184         {
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!");
199         }
200
201         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
202
203         args << L1S("-if") << QDir::toNativeSeparators(sourceFile);
204         args << L1S("-of") << QDir::toNativeSeparators(outputFile);
205
206         if(!startProcess(process, m_binary_enc, args))
207         {
208                 return false;
209         }
210
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);
214
215         const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &duration, &regExp_sp, &regExp_mp](const QString &text)
216         {
217                 if (regExp_mp.lastIndexIn(text) >= 0)
218                 {
219                         int timeElapsed;
220                         if ((duration > 0) && MUtils::regexp_parse_int32(regExp_mp, timeElapsed, 2))
221                         {
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)
225                                 {
226                                         emit statusUpdated(newProgress);
227                                         prevProgress = NEXT_PROGRESS(newProgress);
228                                 }
229                         }
230                         return true;
231                 }
232                 if (regExp_sp.lastIndexIn(text) >= 0)
233                 {
234                         int timeElapsed;
235                         if ((duration > 0) && MUtils::regexp_parse_int32(regExp_sp, timeElapsed))
236                         {
237                                 const int newProgress = qRound((static_cast<double>(timeElapsed) / static_cast<double>(duration)) * 100.0);
238                                 if (newProgress > prevProgress)
239                                 {
240                                         emit statusUpdated(newProgress);
241                                         prevProgress = NEXT_PROGRESS(newProgress);
242                                 }
243                         }
244                         return true;
245                 }
246                 return false;
247         });
248
249         if(result != RESULT_SUCCESS)
250         {
251                 return false;
252         }
253
254         if(metaInfo.empty(false))
255         {
256                 return true;
257         }
258
259         emit messageLogged(L1S("\n-------------------------------\n"));
260         
261         args.clear();
262         args << QDir::toNativeSeparators(outputFile);
263
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());
272         
273         if(!startProcess(process, m_binary_tag, args))
274         {
275                 return false;
276         }
277
278         return (awaitProcess(process, abortFlag) == RESULT_SUCCESS);
279 }
280
281 bool AACEncoder::isFormatSupported(const QString &containerType, const QString& /*containerProfile*/, const QString &formatType, const QString& /*formatProfile*/, const QString& /*formatVersion*/)
282 {
283         if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
284         {
285                 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
286                 {
287                         return true;
288                 }
289         }
290
291         return false;
292 }
293
294 const unsigned int *AACEncoder::supportedSamplerates(void)
295 {
296         static const unsigned int supportedRates[] = { 96000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
297         return supportedRates;
298 }
299
300 void AACEncoder::setProfile(int profile)
301 {
302         m_configProfile = qBound(0, profile, 3);
303 }
304
305 void AACEncoder::setEnable2Pass(bool enabled)
306 {
307         m_configEnable2Pass = enabled;
308 }
309
310 const bool AACEncoder::needsTimingInfo(void)
311 {
312         return true;
313 }
314
315 const AbstractEncoderInfo *AACEncoder::getEncoderInfo(void)
316 {
317         return &g_aacEncoderInfo;
318 }