Major redesign of the AudioFileModel class: Split data into separate AudioFileModel_M...
[LameXP.git] / src / Registry_Encoder.cpp
blob7788ad63dcf0d41e00554be8f8966a1fef8fc13a
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Registry_Encoder.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
26 #include "Encoder_AAC.h"
27 #include "Encoder_AAC_FHG.h"
28 #include "Encoder_AAC_QAAC.h"
29 #include "Encoder_AC3.h"
30 #include "Encoder_DCA.h"
31 #include "Encoder_FLAC.h"
32 #include "Encoder_MP3.h"
33 #include "Encoder_Vorbis.h"
34 #include "Encoder_Opus.h"
35 #include "Encoder_Wave.h"
37 #define IS_VBR(RC_MODE) ((RC_MODE) == SettingsModel::VBRMode)
38 #define IS_ABR(RC_MODE) ((RC_MODE) == SettingsModel::ABRMode)
39 #define IS_CBR(RC_MODE) ((RC_MODE) == SettingsModel::CBRMode)
41 ////////////////////////////////////////////////////////////
42 // Create encoder instance
43 ////////////////////////////////////////////////////////////
45 AbstractEncoder *EncoderRegistry::createInstance(const int encoderId, const SettingsModel *settings, bool *nativeResampling)
47 int rcMode = -1;
48 AbstractEncoder *encoder = NULL;
49 *nativeResampling = false;
51 //Create new encoder instance and apply encoder-specific settings
52 switch(encoderId)
54 /*-------- MP3Encoder /*--------*/
55 case SettingsModel::MP3Encoder:
57 MP3Encoder *mp3Encoder = new MP3Encoder();
58 mp3Encoder->setAlgoQuality(settings->lameAlgoQuality());
59 if(settings->bitrateManagementEnabled())
61 mp3Encoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
63 if(settings->samplingRate() > 0)
65 mp3Encoder->setSamplingRate(SettingsModel::samplingRates[settings->samplingRate()]);
66 *nativeResampling = true;
68 mp3Encoder->setChannelMode(settings->lameChannelMode());
69 encoder = mp3Encoder;
71 break;
72 /*-------- VorbisEncoder /*--------*/
73 case SettingsModel::VorbisEncoder:
75 VorbisEncoder *vorbisEncoder = new VorbisEncoder();
76 if(settings->bitrateManagementEnabled())
78 vorbisEncoder->setBitrateLimits(settings->bitrateManagementMinRate(), settings->bitrateManagementMaxRate());
80 if(settings->samplingRate() > 0)
82 vorbisEncoder->setSamplingRate(SettingsModel::samplingRates[settings->samplingRate()]);
83 *nativeResampling = true;
85 encoder = vorbisEncoder;
87 break;
88 /*-------- AACEncoder /*--------*/
89 case SettingsModel::AACEncoder:
91 switch(getAacEncoder())
93 case SettingsModel::AAC_ENCODER_QAAC:
95 QAACEncoder *aacEncoder = new QAACEncoder();
96 aacEncoder->setProfile(settings->aacEncProfile());
97 encoder = aacEncoder;
99 break;
100 case SettingsModel::AAC_ENCODER_FHG:
102 FHGAACEncoder *aacEncoder = new FHGAACEncoder();
103 aacEncoder->setProfile(settings->aacEncProfile());
104 encoder = aacEncoder;
106 break;
107 case SettingsModel::AAC_ENCODER_NERO:
109 AACEncoder *aacEncoder = new AACEncoder();
110 aacEncoder->setEnable2Pass(settings->neroAACEnable2Pass());
111 aacEncoder->setProfile(settings->aacEncProfile());
112 encoder = aacEncoder;
114 break;
115 default:
116 throw "makeEncoder(): Unknown AAC encoder specified!";
117 break;
120 break;
121 /*-------- AC3Encoder /*--------*/
122 case SettingsModel::AC3Encoder:
124 AC3Encoder *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 *flacEncoder = new FLACEncoder();
136 encoder = flacEncoder;
138 break;
139 /*-------- OpusEncoder --------*/
140 case SettingsModel::OpusEncoder:
142 OpusEncoder *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 *dcaEncoder = new DCAEncoder();
153 encoder = dcaEncoder;
155 break;
156 /*-------- PCMEncoder --------*/
157 case SettingsModel::PCMEncoder:
159 WaveEncoder *waveEncoder = new WaveEncoder();
160 encoder = waveEncoder;
162 break;
163 /*-------- default --------*/
164 default:
165 throw "Unsupported encoder!";
168 //Sanity checking
169 if(!encoder)
171 throw "No encoder instance has been assigend!";
174 //Apply common settings
175 encoder->setRCMode(rcMode = loadEncoderMode(settings, encoderId));
176 encoder->setCustomParams(loadEncoderCustomParams(settings, encoderId));
177 encoder->setBitrate(loadEncoderValue(settings, encoderId, rcMode));
179 return encoder;
182 ////////////////////////////////////////////////////////////
183 // Get encoder info
184 ////////////////////////////////////////////////////////////
186 const AbstractEncoderInfo *EncoderRegistry::getEncoderInfo(const int encoderId)
188 const AbstractEncoderInfo *info = NULL;
190 switch(encoderId)
192 case SettingsModel::MP3Encoder: info = MP3Encoder::getEncoderInfo(); break;
193 case SettingsModel::VorbisEncoder: info = VorbisEncoder::getEncoderInfo(); break;
194 case SettingsModel::AC3Encoder: info = AC3Encoder::getEncoderInfo(); break;
195 case SettingsModel::FLACEncoder: info = FLACEncoder::getEncoderInfo(); break;
196 case SettingsModel::OpusEncoder: info = OpusEncoder::getEncoderInfo(); break;
197 case SettingsModel::DCAEncoder: info = DCAEncoder::getEncoderInfo(); break;
198 case SettingsModel::PCMEncoder: info = WaveEncoder::getEncoderInfo(); break;
199 case SettingsModel::AACEncoder:
200 switch(getAacEncoder())
202 case SettingsModel::AAC_ENCODER_QAAC: info = QAACEncoder::getEncoderInfo(); break;
203 case SettingsModel::AAC_ENCODER_FHG: info = FHGAACEncoder::getEncoderInfo(); break;
204 case SettingsModel::AAC_ENCODER_NERO: info = AACEncoder::getEncoderInfo(); break;
205 default: throw "Unknown AAC encoder specified!";
207 break;
208 default: throw "Unsupported encoder!";
211 //Sanity checking
212 if(!info)
214 throw "No encoder instance has been assigend!";
217 return info;
220 ////////////////////////////////////////////////////////////
221 // Load/store encoder RC mode
222 ////////////////////////////////////////////////////////////
224 #define STORE_MODE(ENCODER_ID, RC_MODE) do \
226 settings->compressionRCMode##ENCODER_ID(RC_MODE); \
228 while(0)
230 #define LOAD_MODE(RC_MODE, ENCODER_ID) do \
232 (RC_MODE) = settings->compressionRCMode##ENCODER_ID(); \
234 while(0)
236 void EncoderRegistry::saveEncoderMode(SettingsModel *settings, const int encoderId, const int rcMode)
238 //Sanity checking
239 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
241 throw "Unknown rate-control mode!";
244 //Store the encoder bitrate/quality value
245 switch(encoderId)
247 case SettingsModel::MP3Encoder: STORE_MODE(LAME, rcMode); break;
248 case SettingsModel::VorbisEncoder: STORE_MODE(OggEnc, rcMode); break;
249 case SettingsModel::AACEncoder: STORE_MODE(AacEnc, rcMode); break;
250 case SettingsModel::AC3Encoder: STORE_MODE(Aften, rcMode); break;
251 case SettingsModel::FLACEncoder: STORE_MODE(FLAC, rcMode); break;
252 case SettingsModel::OpusEncoder: STORE_MODE(OpusEnc, rcMode); break;
253 case SettingsModel::DCAEncoder: STORE_MODE(DcaEnc, rcMode); break;
254 case SettingsModel::PCMEncoder: STORE_MODE(Wave, rcMode); break;
255 default: throw "Unsupported encoder!";
259 int EncoderRegistry::loadEncoderMode(const SettingsModel *settings, const int encoderId)
261 int rcMode = -1;
263 //Store the encoder bitrate/quality value
264 switch(encoderId)
266 case SettingsModel::MP3Encoder: LOAD_MODE(rcMode, LAME); break;
267 case SettingsModel::VorbisEncoder: LOAD_MODE(rcMode, OggEnc); break;
268 case SettingsModel::AACEncoder: LOAD_MODE(rcMode, AacEnc); break;
269 case SettingsModel::AC3Encoder: LOAD_MODE(rcMode, Aften); break;
270 case SettingsModel::FLACEncoder: LOAD_MODE(rcMode, FLAC); break;
271 case SettingsModel::OpusEncoder: LOAD_MODE(rcMode, OpusEnc); break;
272 case SettingsModel::DCAEncoder: LOAD_MODE(rcMode, DcaEnc); break;
273 case SettingsModel::PCMEncoder: LOAD_MODE(rcMode, Wave); break;
274 default: throw "Unsupported encoder!";
277 return rcMode;
280 ////////////////////////////////////////////////////////////
281 // Load/store encoder bitrate/quality value
282 ////////////////////////////////////////////////////////////
284 #define STORE_VALUE(ENCODER_ID, RC_MODE, VALUE) do \
286 if(IS_VBR(RC_MODE)) settings->compressionVbrQuality##ENCODER_ID(VALUE); \
287 if(IS_ABR(RC_MODE)) settings->compressionAbrBitrate##ENCODER_ID(VALUE); \
288 if(IS_CBR(RC_MODE)) settings->compressionCbrBitrate##ENCODER_ID(VALUE); \
290 while(0)
292 #define LOAD_VALUE(VALUE, ENCODER_ID, RC_MODE) do \
294 if(IS_VBR(RC_MODE)) (VALUE) = settings->compressionVbrQuality##ENCODER_ID(); \
295 if(IS_ABR(RC_MODE)) (VALUE) = settings->compressionAbrBitrate##ENCODER_ID(); \
296 if(IS_CBR(RC_MODE)) (VALUE) = settings->compressionCbrBitrate##ENCODER_ID(); \
298 while(0)
300 void EncoderRegistry::saveEncoderValue(SettingsModel *settings, const int encoderId, const int rcMode, const int value)
302 //Sanity checking
303 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
305 throw "Unknown rate-control mode!";
308 //Store the encoder bitrate/quality value
309 switch(encoderId)
311 case SettingsModel::MP3Encoder: STORE_VALUE(LAME, rcMode, value); break;
312 case SettingsModel::VorbisEncoder: STORE_VALUE(OggEnc, rcMode, value); break;
313 case SettingsModel::AACEncoder: STORE_VALUE(AacEnc, rcMode, value); break;
314 case SettingsModel::AC3Encoder: STORE_VALUE(Aften, rcMode, value); break;
315 case SettingsModel::FLACEncoder: STORE_VALUE(FLAC, rcMode, value); break;
316 case SettingsModel::OpusEncoder: STORE_VALUE(OpusEnc, rcMode, value); break;
317 case SettingsModel::DCAEncoder: STORE_VALUE(DcaEnc, rcMode, value); break;
318 case SettingsModel::PCMEncoder: STORE_VALUE(Wave, rcMode, value); break;
319 default: throw "Unsupported encoder!";
323 int EncoderRegistry::loadEncoderValue(const SettingsModel *settings, const int encoderId, const int rcMode)
325 int value = INT_MAX;
327 //Sanity checking
328 if((rcMode < SettingsModel::VBRMode) || (rcMode > SettingsModel::CBRMode))
330 throw "Unknown rate-control mode!";
333 //Load the encoder bitrate/quality value
334 switch(encoderId)
336 case SettingsModel::MP3Encoder: LOAD_VALUE(value, LAME, rcMode); break;
337 case SettingsModel::VorbisEncoder: LOAD_VALUE(value, OggEnc, rcMode); break;
338 case SettingsModel::AACEncoder: LOAD_VALUE(value, AacEnc, rcMode); break;
339 case SettingsModel::AC3Encoder: LOAD_VALUE(value, Aften, rcMode); break;
340 case SettingsModel::FLACEncoder: LOAD_VALUE(value, FLAC, rcMode); break;
341 case SettingsModel::OpusEncoder: LOAD_VALUE(value, OpusEnc, rcMode); break;
342 case SettingsModel::DCAEncoder: LOAD_VALUE(value, DcaEnc, rcMode); break;
343 case SettingsModel::PCMEncoder: LOAD_VALUE(value, Wave, rcMode); break;
344 default: throw "Unsupported encoder!";
347 return value;
350 ////////////////////////////////////////////////////////////
351 // Load/store encoder custom parameters
352 ////////////////////////////////////////////////////////////
354 #define STORE_PARAMS(ENCODER_ID, PARAMS) do \
356 settings->customParameters##ENCODER_ID(PARAMS); \
358 while(0)
360 #define LOAD_PARAMS(PARAMS, ENCODER_ID) do \
362 (PARAMS) = settings->customParameters##ENCODER_ID(); \
364 while(0)
366 void EncoderRegistry::saveEncoderCustomParams(SettingsModel *settings, const int encoderId, const QString params)
368 //Store the encoder bitrate/quality value
369 switch(encoderId)
371 case SettingsModel::MP3Encoder: STORE_PARAMS(LAME, params.trimmed()); break;
372 case SettingsModel::VorbisEncoder: STORE_PARAMS(OggEnc, params.trimmed()); break;
373 case SettingsModel::AACEncoder: STORE_PARAMS(AacEnc, params.trimmed()); break;
374 case SettingsModel::AC3Encoder: STORE_PARAMS(Aften, params.trimmed()); break;
375 case SettingsModel::FLACEncoder: STORE_PARAMS(FLAC, params.trimmed()); break;
376 case SettingsModel::OpusEncoder: STORE_PARAMS(OpusEnc, params.trimmed()); break;
377 case SettingsModel::DCAEncoder: STORE_PARAMS(DcaEnc, params.trimmed()); break;
378 case SettingsModel::PCMEncoder: STORE_PARAMS(Wave, params.trimmed()); break;
379 default: throw "Unsupported encoder!";
383 QString EncoderRegistry::loadEncoderCustomParams(const SettingsModel *settings, const int encoderId)
385 QString params;
387 //Load the encoder bitrate/quality value
388 switch(encoderId)
390 case SettingsModel::MP3Encoder: LOAD_PARAMS(params, LAME); break;
391 case SettingsModel::VorbisEncoder: LOAD_PARAMS(params, OggEnc); break;
392 case SettingsModel::AACEncoder: LOAD_PARAMS(params, AacEnc); break;
393 case SettingsModel::AC3Encoder: LOAD_PARAMS(params, Aften); break;
394 case SettingsModel::FLACEncoder: LOAD_PARAMS(params, FLAC); break;
395 case SettingsModel::OpusEncoder: LOAD_PARAMS(params, OpusEnc); break;
396 case SettingsModel::DCAEncoder: LOAD_PARAMS(params, DcaEnc); break;
397 case SettingsModel::PCMEncoder: LOAD_PARAMS(params, Wave); break;
398 default: throw "Unsupported encoder!";
401 return params;
404 ////////////////////////////////////////////////////////////
405 // Reset encoder settings
406 ////////////////////////////////////////////////////////////
408 #define RESET_SETTING(OBJ,NAME) do \
410 (OBJ)->NAME((OBJ)->NAME##Default()); \
412 while(0)
414 void EncoderRegistry::resetAllEncoders(SettingsModel *settings)
416 RESET_SETTING(settings, compressionAbrBitrateAacEnc);
417 RESET_SETTING(settings, compressionAbrBitrateAften);
418 RESET_SETTING(settings, compressionAbrBitrateDcaEnc);
419 RESET_SETTING(settings, compressionAbrBitrateFLAC);
420 RESET_SETTING(settings, compressionAbrBitrateLAME);
421 RESET_SETTING(settings, compressionAbrBitrateOggEnc);
422 RESET_SETTING(settings, compressionAbrBitrateOpusEnc);
423 RESET_SETTING(settings, compressionAbrBitrateWave);
425 RESET_SETTING(settings, compressionCbrBitrateAacEnc);
426 RESET_SETTING(settings, compressionCbrBitrateAften);
427 RESET_SETTING(settings, compressionCbrBitrateDcaEnc);
428 RESET_SETTING(settings, compressionCbrBitrateFLAC);
429 RESET_SETTING(settings, compressionCbrBitrateLAME);
430 RESET_SETTING(settings, compressionCbrBitrateOggEnc);
431 RESET_SETTING(settings, compressionCbrBitrateOpusEnc);
432 RESET_SETTING(settings, compressionCbrBitrateWave);
434 RESET_SETTING(settings, compressionRCModeAacEnc);
435 RESET_SETTING(settings, compressionRCModeAften);
436 RESET_SETTING(settings, compressionRCModeDcaEnc);
437 RESET_SETTING(settings, compressionRCModeFLAC);
438 RESET_SETTING(settings, compressionRCModeLAME);
439 RESET_SETTING(settings, compressionRCModeOggEnc);
440 RESET_SETTING(settings, compressionRCModeOpusEnc);
441 RESET_SETTING(settings, compressionRCModeWave);
443 RESET_SETTING(settings, compressionVbrQualityAacEnc);
444 RESET_SETTING(settings, compressionVbrQualityAften);
445 RESET_SETTING(settings, compressionVbrQualityDcaEnc);
446 RESET_SETTING(settings, compressionVbrQualityFLAC);
447 RESET_SETTING(settings, compressionVbrQualityLAME);
448 RESET_SETTING(settings, compressionVbrQualityOggEnc);
449 RESET_SETTING(settings, compressionVbrQualityOpusEnc);
450 RESET_SETTING(settings, compressionVbrQualityWave);
453 ////////////////////////////////////////////////////////////
454 // Static Functions
455 ////////////////////////////////////////////////////////////
457 int EncoderRegistry::getAacEncoder(void)
459 if(lamexp_check_tool("qaac.exe") && lamexp_check_tool("libsoxrate.dll"))
461 return SettingsModel::AAC_ENCODER_QAAC;
463 else if(lamexp_check_tool("fhgaacenc.exe") && lamexp_check_tool("enc_fhgaac.dll") && lamexp_check_tool("nsutil.dll") && lamexp_check_tool("libmp4v2.dll"))
465 return SettingsModel::AAC_ENCODER_FHG;
467 else if(lamexp_check_tool("neroAacEnc.exe") && lamexp_check_tool("neroAacDec.exe") && lamexp_check_tool("neroAacTag.exe"))
469 return SettingsModel::AAC_ENCODER_NERO;
471 else
473 return SettingsModel::AAC_ENCODER_NONE;