Further translation
[LameXP.git] / src / Registry_Encoder.cpp
blobc8fbf57774b7f37f94884f7c06ca6ef8ab595602
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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 "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_QAAC.h"
30 #include "Encoder_AC3.h"
31 #include "Encoder_DCA.h"
32 #include "Encoder_FLAC.h"
33 #include "Encoder_MP3.h"
34 #include "Encoder_Vorbis.h"
35 #include "Encoder_Opus.h"
36 #include "Encoder_MAC.h"
37 #include "Encoder_Wave.h"
39 #define IS_VBR(RC_MODE) ((RC_MODE) == SettingsModel::VBRMode)
40 #define IS_ABR(RC_MODE) ((RC_MODE) == SettingsModel::ABRMode)
41 #define IS_CBR(RC_MODE) ((RC_MODE) == SettingsModel::CBRMode)
43 ////////////////////////////////////////////////////////////
44 // Create encoder instance
45 ////////////////////////////////////////////////////////////
47 AbstractEncoder *EncoderRegistry::createInstance(const int encoderId, const SettingsModel *settings, bool *nativeResampling)
49 int rcMode = -1;
50 AbstractEncoder *encoder = NULL;
51 *nativeResampling = false;
53 //Create new encoder instance and apply encoder-specific settings
54 switch(encoderId)
56 /*-------- MP3Encoder /*--------*/
57 case SettingsModel::MP3Encoder:
59 MP3Encoder *mp3Encoder = new MP3Encoder();
60 mp3Encoder->setAlgoQuality(settings->lameAlgoQuality());
61 if(settings->bitrateManagementEnabled())
63 mp3Encoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
65 if(settings->samplingRate() > 0)
67 mp3Encoder->setSamplingRate(SettingsModel::samplingRates[settings->samplingRate()]);
68 *nativeResampling = true;
70 mp3Encoder->setChannelMode(settings->lameChannelMode());
71 encoder = mp3Encoder;
73 break;
74 /*-------- VorbisEncoder /*--------*/
75 case SettingsModel::VorbisEncoder:
77 VorbisEncoder *vorbisEncoder = new VorbisEncoder();
78 if(settings->bitrateManagementEnabled())
80 vorbisEncoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
82 if(settings->samplingRate() > 0)
84 vorbisEncoder->setSamplingRate(SettingsModel::samplingRates[settings->samplingRate()]);
85 *nativeResampling = true;
87 encoder = vorbisEncoder;
89 break;
90 /*-------- AACEncoder /*--------*/
91 case SettingsModel::AACEncoder:
93 switch(getAacEncoder())
95 case SettingsModel::AAC_ENCODER_QAAC:
97 QAACEncoder *aacEncoder = new QAACEncoder();
98 aacEncoder->setProfile(settings->aacEncProfile());
99 encoder = aacEncoder;
101 break;
102 case SettingsModel::AAC_ENCODER_FHG:
104 FHGAACEncoder *aacEncoder = new FHGAACEncoder();
105 aacEncoder->setProfile(settings->aacEncProfile());
106 encoder = aacEncoder;
108 break;
109 case SettingsModel::AAC_ENCODER_NERO:
111 AACEncoder *aacEncoder = new AACEncoder();
112 aacEncoder->setEnable2Pass(settings->neroAACEnable2Pass());
113 aacEncoder->setProfile(settings->aacEncProfile());
114 encoder = aacEncoder;
116 break;
117 default:
118 MUTILS_THROW("makeEncoder(): Unknown AAC encoder specified!");
119 break;
122 break;
123 /*-------- AC3Encoder /*--------*/
124 case SettingsModel::AC3Encoder:
126 AC3Encoder *ac3Encoder = new AC3Encoder();
127 ac3Encoder->setAudioCodingMode(settings->aftenAudioCodingMode());
128 ac3Encoder->setDynamicRangeCompression(settings->aftenDynamicRangeCompression());
129 ac3Encoder->setExponentSearchSize(settings->aftenExponentSearchSize());
130 ac3Encoder->setFastBitAllocation(settings->aftenFastBitAllocation());
131 encoder = ac3Encoder;
133 break;
134 /*-------- FLACEncoder /*--------*/
135 case SettingsModel::FLACEncoder:
137 FLACEncoder *flacEncoder = new FLACEncoder();
138 encoder = flacEncoder;
140 break;
141 /*-------- OpusEncoder --------*/
142 case SettingsModel::OpusEncoder:
144 OpusEncoder *opusEncoder = new OpusEncoder();
145 opusEncoder->setOptimizeFor(settings->opusOptimizeFor());
146 opusEncoder->setEncodeComplexity(settings->opusComplexity());
147 opusEncoder->setFrameSize(settings->opusFramesize());
148 encoder = opusEncoder;
150 break;
151 /*-------- DCAEncoder --------*/
152 case SettingsModel::DCAEncoder:
154 DCAEncoder *dcaEncoder = new DCAEncoder();
155 encoder = dcaEncoder;
157 break;
158 /*-------- MACEncoder --------*/
159 case SettingsModel::MACEncoder:
161 MACEncoder *macEncoder = new MACEncoder();
162 encoder = macEncoder;
164 break;
165 /*-------- PCMEncoder --------*/
166 case SettingsModel::PCMEncoder:
168 WaveEncoder *waveEncoder = new WaveEncoder();
169 encoder = waveEncoder;
171 break;
172 /*-------- default --------*/
173 default:
174 MUTILS_THROW("Unsupported encoder!");
177 //Sanity checking
178 if(!encoder)
180 MUTILS_THROW("No encoder instance has been assigend!");
183 //Apply common settings
184 encoder->setRCMode(rcMode = loadEncoderMode(settings, encoderId));
185 encoder->setCustomParams(loadEncoderCustomParams(settings, encoderId));
186 encoder->setBitrate(loadEncoderValue(settings, encoderId, rcMode));
188 return encoder;
191 ////////////////////////////////////////////////////////////
192 // Get encoder info
193 ////////////////////////////////////////////////////////////
195 const AbstractEncoderInfo *EncoderRegistry::getEncoderInfo(const int encoderId)
197 const AbstractEncoderInfo *info = NULL;
199 switch(encoderId)
201 case SettingsModel::MP3Encoder: info = MP3Encoder::getEncoderInfo(); break;
202 case SettingsModel::VorbisEncoder: info = VorbisEncoder::getEncoderInfo(); break;
203 case SettingsModel::AC3Encoder: info = AC3Encoder::getEncoderInfo(); break;
204 case SettingsModel::FLACEncoder: info = FLACEncoder::getEncoderInfo(); break;
205 case SettingsModel::OpusEncoder: info = OpusEncoder::getEncoderInfo(); break;
206 case SettingsModel::DCAEncoder: info = DCAEncoder::getEncoderInfo(); break;
207 case SettingsModel::MACEncoder: info = MACEncoder::getEncoderInfo(); break;
208 case SettingsModel::PCMEncoder: info = WaveEncoder::getEncoderInfo(); break;
209 case SettingsModel::AACEncoder:
210 switch(getAacEncoder())
212 case SettingsModel::AAC_ENCODER_QAAC: info = QAACEncoder::getEncoderInfo(); break;
213 case SettingsModel::AAC_ENCODER_FHG: info = FHGAACEncoder::getEncoderInfo(); break;
214 case SettingsModel::AAC_ENCODER_NERO: info = AACEncoder::getEncoderInfo(); break;
215 default: MUTILS_THROW("Unknown AAC encoder specified!");
217 break;
218 default: MUTILS_THROW("Unsupported encoder!");
221 //Sanity checking
222 if(!info)
224 MUTILS_THROW("No encoder instance has been assigend!");
227 return info;
230 ////////////////////////////////////////////////////////////
231 // Load/store encoder RC mode
232 ////////////////////////////////////////////////////////////
234 #define STORE_MODE(ENCODER_ID, RC_MODE) do \
236 settings->compressionRCMode##ENCODER_ID(RC_MODE); \
238 while(0)
240 #define LOAD_MODE(RC_MODE, ENCODER_ID) do \
242 (RC_MODE) = settings->compressionRCMode##ENCODER_ID(); \
244 while(0)
246 void EncoderRegistry::saveEncoderMode(SettingsModel *settings, const int encoderId, const int rcMode)
248 //Sanity checking
249 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
251 MUTILS_THROW("Unknown rate-control mode!");
254 //Store the encoder bitrate/quality value
255 switch(encoderId)
257 case SettingsModel::MP3Encoder: STORE_MODE(LAME, rcMode); break;
258 case SettingsModel::VorbisEncoder: STORE_MODE(OggEnc, rcMode); break;
259 case SettingsModel::AACEncoder: STORE_MODE(AacEnc, rcMode); break;
260 case SettingsModel::AC3Encoder: STORE_MODE(Aften, rcMode); break;
261 case SettingsModel::FLACEncoder: STORE_MODE(FLAC, rcMode); break;
262 case SettingsModel::OpusEncoder: STORE_MODE(OpusEnc, rcMode); break;
263 case SettingsModel::DCAEncoder: STORE_MODE(DcaEnc, rcMode); break;
264 case SettingsModel::MACEncoder: STORE_MODE(MacEnc, rcMode); break;
265 case SettingsModel::PCMEncoder: STORE_MODE(Wave, rcMode); break;
266 default: MUTILS_THROW("Unsupported encoder!");
270 int EncoderRegistry::loadEncoderMode(const SettingsModel *settings, const int encoderId)
272 int rcMode = -1;
274 //Store the encoder bitrate/quality value
275 switch(encoderId)
277 case SettingsModel::MP3Encoder: LOAD_MODE(rcMode, LAME); break;
278 case SettingsModel::VorbisEncoder: LOAD_MODE(rcMode, OggEnc); break;
279 case SettingsModel::AACEncoder: LOAD_MODE(rcMode, AacEnc); break;
280 case SettingsModel::AC3Encoder: LOAD_MODE(rcMode, Aften); break;
281 case SettingsModel::FLACEncoder: LOAD_MODE(rcMode, FLAC); break;
282 case SettingsModel::OpusEncoder: LOAD_MODE(rcMode, OpusEnc); break;
283 case SettingsModel::DCAEncoder: LOAD_MODE(rcMode, DcaEnc); break;
284 case SettingsModel::MACEncoder: LOAD_MODE(rcMode, MacEnc); break;
285 case SettingsModel::PCMEncoder: LOAD_MODE(rcMode, Wave); break;
286 default: MUTILS_THROW("Unsupported encoder!");
289 return rcMode;
292 ////////////////////////////////////////////////////////////
293 // Load/store encoder bitrate/quality value
294 ////////////////////////////////////////////////////////////
296 #define STORE_VALUE(ENCODER_ID, RC_MODE, VALUE) do \
298 if(IS_VBR(RC_MODE)) settings->compressionVbrQuality##ENCODER_ID(VALUE); \
299 if(IS_ABR(RC_MODE)) settings->compressionAbrBitrate##ENCODER_ID(VALUE); \
300 if(IS_CBR(RC_MODE)) settings->compressionCbrBitrate##ENCODER_ID(VALUE); \
302 while(0)
304 #define LOAD_VALUE(VALUE, ENCODER_ID, RC_MODE) do \
306 if(IS_VBR(RC_MODE)) (VALUE) = settings->compressionVbrQuality##ENCODER_ID(); \
307 if(IS_ABR(RC_MODE)) (VALUE) = settings->compressionAbrBitrate##ENCODER_ID(); \
308 if(IS_CBR(RC_MODE)) (VALUE) = settings->compressionCbrBitrate##ENCODER_ID(); \
310 while(0)
312 void EncoderRegistry::saveEncoderValue(SettingsModel *settings, const int encoderId, const int rcMode, const int value)
314 //Sanity checking
315 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
317 MUTILS_THROW("Unknown rate-control mode!");
320 //Store the encoder bitrate/quality value
321 switch(encoderId)
323 case SettingsModel::MP3Encoder: STORE_VALUE(LAME, rcMode, value); break;
324 case SettingsModel::VorbisEncoder: STORE_VALUE(OggEnc, rcMode, value); break;
325 case SettingsModel::AACEncoder: STORE_VALUE(AacEnc, rcMode, value); break;
326 case SettingsModel::AC3Encoder: STORE_VALUE(Aften, rcMode, value); break;
327 case SettingsModel::FLACEncoder: STORE_VALUE(FLAC, rcMode, value); break;
328 case SettingsModel::OpusEncoder: STORE_VALUE(OpusEnc, rcMode, value); break;
329 case SettingsModel::DCAEncoder: STORE_VALUE(DcaEnc, rcMode, value); break;
330 case SettingsModel::MACEncoder: STORE_VALUE(MacEnc, rcMode, value); break;
331 case SettingsModel::PCMEncoder: STORE_VALUE(Wave, rcMode, value); break;
332 default: MUTILS_THROW("Unsupported encoder!");
336 int EncoderRegistry::loadEncoderValue(const SettingsModel *settings, const int encoderId, const int rcMode)
338 int value = INT_MAX;
340 //Sanity checking
341 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
343 MUTILS_THROW("Unknown rate-control mode!");
346 //Load the encoder bitrate/quality value
347 switch(encoderId)
349 case SettingsModel::MP3Encoder: LOAD_VALUE(value, LAME, rcMode); break;
350 case SettingsModel::VorbisEncoder: LOAD_VALUE(value, OggEnc, rcMode); break;
351 case SettingsModel::AACEncoder: LOAD_VALUE(value, AacEnc, rcMode); break;
352 case SettingsModel::AC3Encoder: LOAD_VALUE(value, Aften, rcMode); break;
353 case SettingsModel::FLACEncoder: LOAD_VALUE(value, FLAC, rcMode); break;
354 case SettingsModel::OpusEncoder: LOAD_VALUE(value, OpusEnc, rcMode); break;
355 case SettingsModel::DCAEncoder: LOAD_VALUE(value, DcaEnc, rcMode); break;
356 case SettingsModel::MACEncoder: LOAD_VALUE(value, MacEnc, rcMode); break;
357 case SettingsModel::PCMEncoder: LOAD_VALUE(value, Wave, rcMode); break;
358 default: MUTILS_THROW("Unsupported encoder!");
361 return value;
364 ////////////////////////////////////////////////////////////
365 // Load/store encoder custom parameters
366 ////////////////////////////////////////////////////////////
368 #define STORE_PARAMS(ENCODER_ID, PARAMS) do \
370 settings->customParameters##ENCODER_ID(PARAMS); \
372 while(0)
374 #define LOAD_PARAMS(PARAMS, ENCODER_ID) do \
376 (PARAMS) = settings->customParameters##ENCODER_ID(); \
378 while(0)
380 void EncoderRegistry::saveEncoderCustomParams(SettingsModel *settings, const int encoderId, const QString params)
382 //Store the encoder bitrate/quality value
383 switch(encoderId)
385 case SettingsModel::MP3Encoder: STORE_PARAMS(LAME, params.trimmed()); break;
386 case SettingsModel::VorbisEncoder: STORE_PARAMS(OggEnc, params.trimmed()); break;
387 case SettingsModel::AACEncoder: STORE_PARAMS(AacEnc, params.trimmed()); break;
388 case SettingsModel::AC3Encoder: STORE_PARAMS(Aften, params.trimmed()); break;
389 case SettingsModel::FLACEncoder: STORE_PARAMS(FLAC, params.trimmed()); break;
390 case SettingsModel::OpusEncoder: STORE_PARAMS(OpusEnc, params.trimmed()); break;
391 case SettingsModel::DCAEncoder: STORE_PARAMS(DcaEnc, params.trimmed()); break;
392 case SettingsModel::MACEncoder: STORE_PARAMS(MacEnc, params.trimmed()); break;
393 case SettingsModel::PCMEncoder: STORE_PARAMS(Wave, params.trimmed()); break;
394 default: MUTILS_THROW("Unsupported encoder!");
398 QString EncoderRegistry::loadEncoderCustomParams(const SettingsModel *settings, const int encoderId)
400 QString params;
402 //Load the encoder bitrate/quality value
403 switch(encoderId)
405 case SettingsModel::MP3Encoder: LOAD_PARAMS(params, LAME); break;
406 case SettingsModel::VorbisEncoder: LOAD_PARAMS(params, OggEnc); break;
407 case SettingsModel::AACEncoder: LOAD_PARAMS(params, AacEnc); break;
408 case SettingsModel::AC3Encoder: LOAD_PARAMS(params, Aften); break;
409 case SettingsModel::FLACEncoder: LOAD_PARAMS(params, FLAC); break;
410 case SettingsModel::OpusEncoder: LOAD_PARAMS(params, OpusEnc); break;
411 case SettingsModel::DCAEncoder: LOAD_PARAMS(params, DcaEnc); break;
412 case SettingsModel::MACEncoder: LOAD_PARAMS(params, MacEnc); break;
413 case SettingsModel::PCMEncoder: LOAD_PARAMS(params, Wave); break;
414 default: MUTILS_THROW("Unsupported encoder!");
417 return params;
420 ////////////////////////////////////////////////////////////
421 // Reset encoder settings
422 ////////////////////////////////////////////////////////////
424 #define RESET_SETTING(OBJ,NAME) do \
426 (OBJ)->NAME((OBJ)->NAME##Default()); \
428 while(0)
430 void EncoderRegistry::resetAllEncoders(SettingsModel *settings)
432 RESET_SETTING(settings, compressionAbrBitrateAacEnc);
433 RESET_SETTING(settings, compressionAbrBitrateAften);
434 RESET_SETTING(settings, compressionAbrBitrateDcaEnc);
435 RESET_SETTING(settings, compressionAbrBitrateFLAC);
436 RESET_SETTING(settings, compressionAbrBitrateLAME);
437 RESET_SETTING(settings, compressionAbrBitrateMacEnc);
438 RESET_SETTING(settings, compressionAbrBitrateOggEnc);
439 RESET_SETTING(settings, compressionAbrBitrateOpusEnc);
440 RESET_SETTING(settings, compressionAbrBitrateWave);
442 RESET_SETTING(settings, compressionCbrBitrateAacEnc);
443 RESET_SETTING(settings, compressionCbrBitrateAften);
444 RESET_SETTING(settings, compressionCbrBitrateDcaEnc);
445 RESET_SETTING(settings, compressionCbrBitrateFLAC);
446 RESET_SETTING(settings, compressionCbrBitrateLAME);
447 RESET_SETTING(settings, compressionCbrBitrateMacEnc);
448 RESET_SETTING(settings, compressionCbrBitrateOggEnc);
449 RESET_SETTING(settings, compressionCbrBitrateOpusEnc);
450 RESET_SETTING(settings, compressionCbrBitrateWave);
452 RESET_SETTING(settings, compressionRCModeAacEnc);
453 RESET_SETTING(settings, compressionRCModeAften);
454 RESET_SETTING(settings, compressionRCModeDcaEnc);
455 RESET_SETTING(settings, compressionRCModeFLAC);
456 RESET_SETTING(settings, compressionRCModeLAME);
457 RESET_SETTING(settings, compressionRCModeMacEnc);
458 RESET_SETTING(settings, compressionRCModeOggEnc);
459 RESET_SETTING(settings, compressionRCModeOpusEnc);
460 RESET_SETTING(settings, compressionRCModeWave);
462 RESET_SETTING(settings, compressionVbrQualityAacEnc);
463 RESET_SETTING(settings, compressionVbrQualityAften);
464 RESET_SETTING(settings, compressionVbrQualityDcaEnc);
465 RESET_SETTING(settings, compressionVbrQualityFLAC);
466 RESET_SETTING(settings, compressionVbrQualityLAME);
467 RESET_SETTING(settings, compressionVbrQualityMacEnc);
468 RESET_SETTING(settings, compressionVbrQualityOggEnc);
469 RESET_SETTING(settings, compressionVbrQualityOpusEnc);
470 RESET_SETTING(settings, compressionVbrQualityWave);
473 ////////////////////////////////////////////////////////////
474 // Static Functions
475 ////////////////////////////////////////////////////////////
477 int EncoderRegistry::getAacEncoder(void)
479 if(lamexp_tools_check("qaac.exe") && lamexp_tools_check("libsoxr.dll") && lamexp_tools_check("libsoxconvolver.dll"))
481 return SettingsModel::AAC_ENCODER_QAAC;
483 else if(lamexp_tools_check("fhgaacenc.exe") && lamexp_tools_check("enc_fhgaac.dll") && lamexp_tools_check("nsutil.dll") && lamexp_tools_check("libmp4v2.dll"))
485 return SettingsModel::AAC_ENCODER_FHG;
487 else if(lamexp_tools_check("neroAacEnc.exe") && lamexp_tools_check("neroAacDec.exe") && lamexp_tools_check("neroAacTag.exe"))
489 return SettingsModel::AAC_ENCODER_NERO;
491 else
493 return SettingsModel::AAC_ENCODER_NONE;