Small improvement to UpdateDialog.
[LameXP.git] / src / Registry_Encoder.cpp
blobe0247e5802e9d5d5c16211afa01950b1f8348d93
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 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!
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 "Registry_Encoder.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
27 #include "Encoder_AAC.h"
28 #include "Encoder_AAC_FHG.h"
29 #include "Encoder_AAC_FDK.h"
30 #include "Encoder_AAC_QAAC.h"
31 #include "Encoder_AC3.h"
32 #include "Encoder_DCA.h"
33 #include "Encoder_FLAC.h"
34 #include "Encoder_MP3.h"
35 #include "Encoder_Vorbis.h"
36 #include "Encoder_Opus.h"
37 #include "Encoder_MAC.h"
38 #include "Encoder_Wave.h"
40 #define IS_VBR(RC_MODE) ((RC_MODE) == SettingsModel::VBRMode)
41 #define IS_ABR(RC_MODE) ((RC_MODE) == SettingsModel::ABRMode)
42 #define IS_CBR(RC_MODE) ((RC_MODE) == SettingsModel::CBRMode)
44 ////////////////////////////////////////////////////////////
45 // Create encoder instance
46 ////////////////////////////////////////////////////////////
48 AbstractEncoder *EncoderRegistry::createInstance(const int encoderId, const SettingsModel *settings)
50 int rcMode = -1;
51 AbstractEncoder *encoder = NULL;
53 //Create new encoder instance and apply encoder-specific settings
54 switch(encoderId)
56 /*-------- MP3Encoder /*--------*/
57 case SettingsModel::MP3Encoder:
59 MP3Encoder *const mp3Encoder = new MP3Encoder();
60 mp3Encoder->setAlgoQuality(settings->lameAlgoQuality());
61 if(settings->bitrateManagementEnabled())
63 mp3Encoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
65 mp3Encoder->setChannelMode(settings->lameChannelMode());
66 encoder = mp3Encoder;
68 break;
69 /*-------- VorbisEncoder /*--------*/
70 case SettingsModel::VorbisEncoder:
72 VorbisEncoder *const vorbisEncoder = new VorbisEncoder();
73 if(settings->bitrateManagementEnabled())
75 vorbisEncoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
77 encoder = vorbisEncoder;
79 break;
80 /*-------- AACEncoder /*--------*/
81 case SettingsModel::AACEncoder:
83 switch(getAacEncoder())
85 case SettingsModel::AAC_ENCODER_QAAC:
87 QAACEncoder *const aacEncoder = new QAACEncoder();
88 aacEncoder->setProfile(settings->aacEncProfile());
89 aacEncoder->setAlgoQuality(settings->lameAlgoQuality());
90 encoder = aacEncoder;
92 break;
93 case SettingsModel::AAC_ENCODER_FHG:
95 FHGAACEncoder *const aacEncoder = new FHGAACEncoder();
96 aacEncoder->setProfile(settings->aacEncProfile());
97 encoder = aacEncoder;
99 break;
100 case SettingsModel::AAC_ENCODER_FDK:
102 FDKAACEncoder *const aacEncoder = new FDKAACEncoder();
103 aacEncoder->setProfile(settings->aacEncProfile());
104 encoder = aacEncoder;
106 break;
107 case SettingsModel::AAC_ENCODER_NERO:
109 AACEncoder *const aacEncoder = new AACEncoder();
110 aacEncoder->setEnable2Pass(settings->neroAACEnable2Pass());
111 aacEncoder->setProfile(settings->aacEncProfile());
112 encoder = aacEncoder;
114 break;
115 default:
116 MUTILS_THROW("makeEncoder(): Unknown AAC encoder specified!");
117 break;
120 break;
121 /*-------- AC3Encoder /*--------*/
122 case SettingsModel::AC3Encoder:
124 AC3Encoder *const ac3Encoder = new AC3Encoder();
125 ac3Encoder->setAudioCodingMode(settings->aftenAudioCodingMode());
126 ac3Encoder->setDynamicRangeCompression(settings->aftenDynamicRangeCompression());
127 ac3Encoder->setExponentSearchSize(settings->aftenExponentSearchSize());
128 ac3Encoder->setFastBitAllocation(settings->aftenFastBitAllocation());
129 encoder = ac3Encoder;
131 break;
132 /*-------- FLACEncoder /*--------*/
133 case SettingsModel::FLACEncoder:
135 FLACEncoder *const flacEncoder = new FLACEncoder();
136 encoder = flacEncoder;
138 break;
139 /*-------- OpusEncoder --------*/
140 case SettingsModel::OpusEncoder:
142 OpusEncoder *const opusEncoder = new OpusEncoder();
143 opusEncoder->setOptimizeFor(settings->opusOptimizeFor());
144 opusEncoder->setEncodeComplexity(settings->opusComplexity());
145 opusEncoder->setFrameSize(settings->opusFramesize());
146 encoder = opusEncoder;
148 break;
149 /*-------- DCAEncoder --------*/
150 case SettingsModel::DCAEncoder:
152 DCAEncoder *const dcaEncoder = new DCAEncoder();
153 encoder = dcaEncoder;
155 break;
156 /*-------- MACEncoder --------*/
157 case SettingsModel::MACEncoder:
159 MACEncoder *const macEncoder = new MACEncoder();
160 encoder = macEncoder;
162 break;
163 /*-------- PCMEncoder --------*/
164 case SettingsModel::PCMEncoder:
166 WaveEncoder *const waveEncoder = new WaveEncoder();
167 encoder = waveEncoder;
169 break;
170 /*-------- default --------*/
171 default:
172 MUTILS_THROW("Unsupported encoder!");
175 //Sanity checking
176 if(!encoder)
178 MUTILS_THROW("No encoder instance has been assigend!");
181 //Apply common settings
182 encoder->setRCMode(rcMode = loadEncoderMode(settings, encoderId));
183 encoder->setCustomParams(loadEncoderCustomParams(settings, encoderId));
184 encoder->setBitrate(loadEncoderValue(settings, encoderId, rcMode));
186 return encoder;
189 ////////////////////////////////////////////////////////////
190 // Get encoder info
191 ////////////////////////////////////////////////////////////
193 const AbstractEncoderInfo *EncoderRegistry::getEncoderInfo(const int encoderId)
195 const AbstractEncoderInfo *info = NULL;
197 switch(encoderId)
199 case SettingsModel::MP3Encoder: info = MP3Encoder ::getEncoderInfo(); break;
200 case SettingsModel::VorbisEncoder: info = VorbisEncoder::getEncoderInfo(); break;
201 case SettingsModel::AC3Encoder: info = AC3Encoder ::getEncoderInfo(); break;
202 case SettingsModel::FLACEncoder: info = FLACEncoder ::getEncoderInfo(); break;
203 case SettingsModel::OpusEncoder: info = OpusEncoder ::getEncoderInfo(); break;
204 case SettingsModel::DCAEncoder: info = DCAEncoder ::getEncoderInfo(); break;
205 case SettingsModel::MACEncoder: info = MACEncoder ::getEncoderInfo(); break;
206 case SettingsModel::PCMEncoder: info = WaveEncoder ::getEncoderInfo(); break;
207 case SettingsModel::AACEncoder:
208 switch(getAacEncoder())
210 case SettingsModel::AAC_ENCODER_QAAC: info = QAACEncoder ::getEncoderInfo(); break;
211 case SettingsModel::AAC_ENCODER_FHG: info = FHGAACEncoder::getEncoderInfo(); break;
212 case SettingsModel::AAC_ENCODER_FDK: info = FDKAACEncoder::getEncoderInfo(); break;
213 case SettingsModel::AAC_ENCODER_NERO: info = AACEncoder ::getEncoderInfo(); break;
214 default: MUTILS_THROW("Unknown AAC encoder specified!");
216 break;
217 default: MUTILS_THROW("Unsupported encoder!");
220 //Sanity checking
221 if(!info)
223 MUTILS_THROW("No encoder instance has been assigend!");
226 return info;
229 ////////////////////////////////////////////////////////////
230 // Load/store encoder RC mode
231 ////////////////////////////////////////////////////////////
233 #define STORE_MODE(ENCODER_ID, RC_MODE) do \
235 settings->compressionRCMode##ENCODER_ID(RC_MODE); \
237 while(0)
239 #define LOAD_MODE(RC_MODE, ENCODER_ID) do \
241 (RC_MODE) = settings->compressionRCMode##ENCODER_ID(); \
243 while(0)
245 void EncoderRegistry::saveEncoderMode(SettingsModel *settings, const int encoderId, const int rcMode)
247 //Sanity checking
248 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
250 MUTILS_THROW("Unknown rate-control mode!");
253 //Store the encoder bitrate/quality value
254 switch(encoderId)
256 case SettingsModel::MP3Encoder: STORE_MODE(LAME, rcMode); break;
257 case SettingsModel::VorbisEncoder: STORE_MODE(OggEnc, rcMode); break;
258 case SettingsModel::AACEncoder: STORE_MODE(AacEnc, rcMode); break;
259 case SettingsModel::AC3Encoder: STORE_MODE(Aften, rcMode); break;
260 case SettingsModel::FLACEncoder: STORE_MODE(FLAC, rcMode); break;
261 case SettingsModel::OpusEncoder: STORE_MODE(OpusEnc, rcMode); break;
262 case SettingsModel::DCAEncoder: STORE_MODE(DcaEnc, rcMode); break;
263 case SettingsModel::MACEncoder: STORE_MODE(MacEnc, rcMode); break;
264 case SettingsModel::PCMEncoder: STORE_MODE(Wave, rcMode); break;
265 default: MUTILS_THROW("Unsupported encoder!");
269 int EncoderRegistry::loadEncoderMode(const SettingsModel *settings, const int encoderId)
271 int rcMode = -1;
273 //Store the encoder bitrate/quality value
274 switch(encoderId)
276 case SettingsModel::MP3Encoder: LOAD_MODE(rcMode, LAME); break;
277 case SettingsModel::VorbisEncoder: LOAD_MODE(rcMode, OggEnc); break;
278 case SettingsModel::AACEncoder: LOAD_MODE(rcMode, AacEnc); break;
279 case SettingsModel::AC3Encoder: LOAD_MODE(rcMode, Aften); break;
280 case SettingsModel::FLACEncoder: LOAD_MODE(rcMode, FLAC); break;
281 case SettingsModel::OpusEncoder: LOAD_MODE(rcMode, OpusEnc); break;
282 case SettingsModel::DCAEncoder: LOAD_MODE(rcMode, DcaEnc); break;
283 case SettingsModel::MACEncoder: LOAD_MODE(rcMode, MacEnc); break;
284 case SettingsModel::PCMEncoder: LOAD_MODE(rcMode, Wave); break;
285 default: MUTILS_THROW("Unsupported encoder!");
288 return rcMode;
291 ////////////////////////////////////////////////////////////
292 // Load/store encoder bitrate/quality value
293 ////////////////////////////////////////////////////////////
295 #define STORE_VALUE(ENCODER_ID, RC_MODE, VALUE) do \
297 if(IS_VBR(RC_MODE)) settings->compressionVbrQuality##ENCODER_ID(VALUE); \
298 if(IS_ABR(RC_MODE)) settings->compressionAbrBitrate##ENCODER_ID(VALUE); \
299 if(IS_CBR(RC_MODE)) settings->compressionCbrBitrate##ENCODER_ID(VALUE); \
301 while(0)
303 #define LOAD_VALUE(VALUE, ENCODER_ID, RC_MODE) do \
305 if(IS_VBR(RC_MODE)) (VALUE) = settings->compressionVbrQuality##ENCODER_ID(); \
306 if(IS_ABR(RC_MODE)) (VALUE) = settings->compressionAbrBitrate##ENCODER_ID(); \
307 if(IS_CBR(RC_MODE)) (VALUE) = settings->compressionCbrBitrate##ENCODER_ID(); \
309 while(0)
311 void EncoderRegistry::saveEncoderValue(SettingsModel *settings, const int encoderId, const int rcMode, const int value)
313 //Sanity checking
314 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
316 MUTILS_THROW("Unknown rate-control mode!");
319 //Store the encoder bitrate/quality value
320 switch(encoderId)
322 case SettingsModel::MP3Encoder: STORE_VALUE(LAME, rcMode, value); break;
323 case SettingsModel::VorbisEncoder: STORE_VALUE(OggEnc, rcMode, value); break;
324 case SettingsModel::AACEncoder: STORE_VALUE(AacEnc, rcMode, value); break;
325 case SettingsModel::AC3Encoder: STORE_VALUE(Aften, rcMode, value); break;
326 case SettingsModel::FLACEncoder: STORE_VALUE(FLAC, rcMode, value); break;
327 case SettingsModel::OpusEncoder: STORE_VALUE(OpusEnc, rcMode, value); break;
328 case SettingsModel::DCAEncoder: STORE_VALUE(DcaEnc, rcMode, value); break;
329 case SettingsModel::MACEncoder: STORE_VALUE(MacEnc, rcMode, value); break;
330 case SettingsModel::PCMEncoder: STORE_VALUE(Wave, rcMode, value); break;
331 default: MUTILS_THROW("Unsupported encoder!");
335 int EncoderRegistry::loadEncoderValue(const SettingsModel *settings, const int encoderId, const int rcMode)
337 int value = INT_MAX;
339 //Sanity checking
340 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
342 MUTILS_THROW("Unknown rate-control mode!");
345 //Load the encoder bitrate/quality value
346 switch(encoderId)
348 case SettingsModel::MP3Encoder: LOAD_VALUE(value, LAME, rcMode); break;
349 case SettingsModel::VorbisEncoder: LOAD_VALUE(value, OggEnc, rcMode); break;
350 case SettingsModel::AACEncoder: LOAD_VALUE(value, AacEnc, rcMode); break;
351 case SettingsModel::AC3Encoder: LOAD_VALUE(value, Aften, rcMode); break;
352 case SettingsModel::FLACEncoder: LOAD_VALUE(value, FLAC, rcMode); break;
353 case SettingsModel::OpusEncoder: LOAD_VALUE(value, OpusEnc, rcMode); break;
354 case SettingsModel::DCAEncoder: LOAD_VALUE(value, DcaEnc, rcMode); break;
355 case SettingsModel::MACEncoder: LOAD_VALUE(value, MacEnc, rcMode); break;
356 case SettingsModel::PCMEncoder: LOAD_VALUE(value, Wave, rcMode); break;
357 default: MUTILS_THROW("Unsupported encoder!");
360 return value;
363 ////////////////////////////////////////////////////////////
364 // Load/store encoder custom parameters
365 ////////////////////////////////////////////////////////////
367 #define STORE_PARAMS(ENCODER_ID, PARAMS) do \
369 settings->customParameters##ENCODER_ID(PARAMS); \
371 while(0)
373 #define LOAD_PARAMS(PARAMS, ENCODER_ID) do \
375 (PARAMS) = settings->customParameters##ENCODER_ID(); \
377 while(0)
379 void EncoderRegistry::saveEncoderCustomParams(SettingsModel *settings, const int encoderId, const QString &params)
381 //Store the encoder bitrate/quality value
382 switch(encoderId)
384 case SettingsModel::MP3Encoder: STORE_PARAMS(LAME, params.trimmed()); break;
385 case SettingsModel::VorbisEncoder: STORE_PARAMS(OggEnc, params.trimmed()); break;
386 case SettingsModel::AACEncoder: STORE_PARAMS(AacEnc, params.trimmed()); break;
387 case SettingsModel::AC3Encoder: STORE_PARAMS(Aften, params.trimmed()); break;
388 case SettingsModel::FLACEncoder: STORE_PARAMS(FLAC, params.trimmed()); break;
389 case SettingsModel::OpusEncoder: STORE_PARAMS(OpusEnc, params.trimmed()); break;
390 case SettingsModel::DCAEncoder: STORE_PARAMS(DcaEnc, params.trimmed()); break;
391 case SettingsModel::MACEncoder: STORE_PARAMS(MacEnc, params.trimmed()); break;
392 case SettingsModel::PCMEncoder: STORE_PARAMS(Wave, params.trimmed()); break;
393 default: MUTILS_THROW("Unsupported encoder!");
397 QString EncoderRegistry::loadEncoderCustomParams(const SettingsModel *settings, const int encoderId)
399 QString params;
401 //Load the encoder bitrate/quality value
402 switch(encoderId)
404 case SettingsModel::MP3Encoder: LOAD_PARAMS(params, LAME); break;
405 case SettingsModel::VorbisEncoder: LOAD_PARAMS(params, OggEnc); break;
406 case SettingsModel::AACEncoder: LOAD_PARAMS(params, AacEnc); break;
407 case SettingsModel::AC3Encoder: LOAD_PARAMS(params, Aften); break;
408 case SettingsModel::FLACEncoder: LOAD_PARAMS(params, FLAC); break;
409 case SettingsModel::OpusEncoder: LOAD_PARAMS(params, OpusEnc); break;
410 case SettingsModel::DCAEncoder: LOAD_PARAMS(params, DcaEnc); break;
411 case SettingsModel::MACEncoder: LOAD_PARAMS(params, MacEnc); break;
412 case SettingsModel::PCMEncoder: LOAD_PARAMS(params, Wave); break;
413 default: MUTILS_THROW("Unsupported encoder!");
416 return params;
419 ////////////////////////////////////////////////////////////
420 // Reset encoder settings
421 ////////////////////////////////////////////////////////////
423 #define RESET_SETTING(OBJ,NAME) do \
425 (OBJ)->NAME((OBJ)->NAME##Default()); \
427 while(0)
429 void EncoderRegistry::resetAllEncoders(SettingsModel *settings)
431 RESET_SETTING(settings, compressionAbrBitrateAacEnc);
432 RESET_SETTING(settings, compressionAbrBitrateAften);
433 RESET_SETTING(settings, compressionAbrBitrateDcaEnc);
434 RESET_SETTING(settings, compressionAbrBitrateFLAC);
435 RESET_SETTING(settings, compressionAbrBitrateLAME);
436 RESET_SETTING(settings, compressionAbrBitrateMacEnc);
437 RESET_SETTING(settings, compressionAbrBitrateOggEnc);
438 RESET_SETTING(settings, compressionAbrBitrateOpusEnc);
439 RESET_SETTING(settings, compressionAbrBitrateWave);
441 RESET_SETTING(settings, compressionCbrBitrateAacEnc);
442 RESET_SETTING(settings, compressionCbrBitrateAften);
443 RESET_SETTING(settings, compressionCbrBitrateDcaEnc);
444 RESET_SETTING(settings, compressionCbrBitrateFLAC);
445 RESET_SETTING(settings, compressionCbrBitrateLAME);
446 RESET_SETTING(settings, compressionCbrBitrateMacEnc);
447 RESET_SETTING(settings, compressionCbrBitrateOggEnc);
448 RESET_SETTING(settings, compressionCbrBitrateOpusEnc);
449 RESET_SETTING(settings, compressionCbrBitrateWave);
451 RESET_SETTING(settings, compressionRCModeAacEnc);
452 RESET_SETTING(settings, compressionRCModeAften);
453 RESET_SETTING(settings, compressionRCModeDcaEnc);
454 RESET_SETTING(settings, compressionRCModeFLAC);
455 RESET_SETTING(settings, compressionRCModeLAME);
456 RESET_SETTING(settings, compressionRCModeMacEnc);
457 RESET_SETTING(settings, compressionRCModeOggEnc);
458 RESET_SETTING(settings, compressionRCModeOpusEnc);
459 RESET_SETTING(settings, compressionRCModeWave);
461 RESET_SETTING(settings, compressionVbrQualityAacEnc);
462 RESET_SETTING(settings, compressionVbrQualityAften);
463 RESET_SETTING(settings, compressionVbrQualityDcaEnc);
464 RESET_SETTING(settings, compressionVbrQualityFLAC);
465 RESET_SETTING(settings, compressionVbrQualityLAME);
466 RESET_SETTING(settings, compressionVbrQualityMacEnc);
467 RESET_SETTING(settings, compressionVbrQualityOggEnc);
468 RESET_SETTING(settings, compressionVbrQualityOpusEnc);
469 RESET_SETTING(settings, compressionVbrQualityWave);
472 ////////////////////////////////////////////////////////////
473 // Get File Extensions
474 ////////////////////////////////////////////////////////////
476 QStringList EncoderRegistry::getOutputFileExtensions(void)
478 QStringList list;
479 for(int encoderId = SettingsModel::MP3Encoder; encoderId < SettingsModel::ENCODER_COUNT; encoderId++)
481 if((encoderId == SettingsModel::AACEncoder) && (getAacEncoder() == SettingsModel::AAC_ENCODER_NONE))
483 continue;
485 list << QString::fromLatin1(getEncoderInfo(encoderId)->extension());
487 return list;
490 ////////////////////////////////////////////////////////////
491 // Static Functions
492 ////////////////////////////////////////////////////////////
494 int EncoderRegistry::getAacEncoder(void)
496 if(lamexp_tools_check("qaac.exe") && lamexp_tools_check("libsoxr.dll") && lamexp_tools_check("libsoxconvolver.dll"))
498 return SettingsModel::AAC_ENCODER_QAAC;
500 else if(lamexp_tools_check("qaac64.exe") && lamexp_tools_check("libsoxr64.dll") && lamexp_tools_check("libsoxconvolver64.dll"))
502 return SettingsModel::AAC_ENCODER_QAAC;
504 else if(lamexp_tools_check("fdkaac.exe"))
506 return SettingsModel::AAC_ENCODER_FDK;
508 else if(lamexp_tools_check("fhgaacenc.exe") && lamexp_tools_check("enc_fhgaac.dll") && lamexp_tools_check("nsutil.dll") && lamexp_tools_check("libmp4v2.dll"))
510 return SettingsModel::AAC_ENCODER_FHG;
512 else if(lamexp_tools_check("neroAacEnc.exe") && lamexp_tools_check("neroAacDec.exe") && lamexp_tools_check("neroAacTag.exe"))
514 return SettingsModel::AAC_ENCODER_NERO;
516 else
518 return SettingsModel::AAC_ENCODER_NONE;