1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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_AC3.h"
26 #include "Model_Settings.h"
31 static const int g_ac3BitratesLUT
[20] = {32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 448, 512, 576, 640, -1};
33 ///////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
37 class AC3EncoderInfo
: public AbstractEncoderInfo
39 virtual bool isModeSupported(int mode
) const
43 case SettingsModel::VBRMode
:
44 case SettingsModel::CBRMode
:
47 case SettingsModel::ABRMode
:
51 THROW("Bad RC mode specified!");
55 virtual int valueCount(int mode
) const
59 case SettingsModel::VBRMode
:
62 case SettingsModel::ABRMode
:
65 case SettingsModel::CBRMode
:
69 THROW("Bad RC mode specified!");
73 virtual int valueAt(int mode
, int index
) const
77 case SettingsModel::VBRMode
:
78 return qBound(0, index
* 16, 1023);
80 case SettingsModel::ABRMode
:
81 case SettingsModel::CBRMode
:
82 return g_ac3BitratesLUT
[qBound(0, index
, 18)];
85 THROW("Bad RC mode specified!");
89 virtual int valueType(int mode
) const
93 case SettingsModel::VBRMode
:
94 return TYPE_QUALITY_LEVEL_INT
;
96 case SettingsModel::ABRMode
:
97 case SettingsModel::CBRMode
:
101 THROW("Bad RC mode specified!");
105 virtual const char *description(void) const
107 static const char* s_description
= "Aften: A/52 Audio Encoder";
108 return s_description
;
111 static const g_aftenEncoderInfo
;
113 ///////////////////////////////////////////////////////////////////////////////
114 // Encoder implementation
115 ///////////////////////////////////////////////////////////////////////////////
117 AC3Encoder::AC3Encoder(void)
119 m_binary(lamexp_lookup_tool("aften.exe"))
121 if(m_binary
.isEmpty())
123 THROW("Error initializing FLAC encoder. Tool 'aften.exe' is not registred!");
126 m_configAudioCodingMode
= 0;
127 m_configDynamicRangeCompression
= 5;
128 m_configExponentSearchSize
= 8;
129 m_configFastBitAllocation
= false;
132 AC3Encoder::~AC3Encoder(void)
136 bool AC3Encoder::encode(const QString
&sourceFile
, const AudioFileModel_MetaInfo
&metaInfo
, const unsigned int duration
, const QString
&outputFile
, volatile bool *abortFlag
)
141 switch(m_configRCMode
)
143 case SettingsModel::VBRMode
:
144 args
<< "-q" << QString::number(qBound(0, m_configBitrate
* 16, 1023));
146 case SettingsModel::CBRMode
:
147 args
<< "-b" << QString::number(g_ac3BitratesLUT
[qBound(0, m_configBitrate
, 18)]);
150 THROW("Bad rate-control mode!");
154 if(m_configAudioCodingMode
>= 1)
156 args
<< "-acmod" << QString::number(m_configAudioCodingMode
- 1);
158 if(m_configDynamicRangeCompression
!= 5)
160 args
<< "-dynrng" << QString::number(m_configDynamicRangeCompression
);
162 if(m_configExponentSearchSize
!= 8)
164 args
<< "-exps" << QString::number(m_configExponentSearchSize
);
166 if(m_configFastBitAllocation
)
168 args
<< "-fba" << QString::number(1);
171 if(!m_configCustomParams
.isEmpty()) args
<< m_configCustomParams
.split(" ", QString::SkipEmptyParts
);
173 args
<< QDir::toNativeSeparators(sourceFile
);
174 args
<< QDir::toNativeSeparators(outputFile
);
176 if(!startProcess(process
, m_binary
, args
))
181 bool bTimeout
= false;
182 bool bAborted
= false;
183 int prevProgress
= -1;
185 QRegExp
regExp("progress:(\\s+)(\\d+)%(\\s+)\\|");
187 while(process
.state() != QProcess::NotRunning
)
193 emit
messageLogged("\nABORTED BY USER !!!");
196 process
.waitForReadyRead(m_processTimeoutInterval
);
197 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
200 qWarning("Aften process timed out <-- killing!");
201 emit
messageLogged("\nPROCESS TIMEOUT !!!");
205 while(process
.bytesAvailable() > 0)
207 QByteArray line
= process
.readLine();
208 QString text
= QString::fromUtf8(line
.constData()).simplified();
209 if(regExp
.lastIndexIn(text
) >= 0)
212 int progress
= regExp
.cap(2).toInt(&ok
);
213 if(ok
&& (progress
> prevProgress
))
215 emit
statusUpdated(progress
);
216 prevProgress
= qMin(progress
+ 2, 99);
219 else if(!text
.isEmpty())
221 emit
messageLogged(text
);
226 process
.waitForFinished();
227 if(process
.state() != QProcess::NotRunning
)
230 process
.waitForFinished(-1);
233 emit
statusUpdated(100);
234 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
236 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
244 void AC3Encoder::setAudioCodingMode(int value
)
246 m_configAudioCodingMode
= qMin(8, qMax(0, value
));
249 void AC3Encoder::setDynamicRangeCompression(int value
)
251 m_configDynamicRangeCompression
= qMin(5, qMax(0, value
));
254 void AC3Encoder::setExponentSearchSize(int value
)
256 m_configExponentSearchSize
= qMin(32, qMax(1, value
));
259 void AC3Encoder::setFastBitAllocation(bool value
)
261 m_configFastBitAllocation
= value
;
264 QString
AC3Encoder::extension(void)
269 const unsigned int *AC3Encoder::supportedChannelCount(void)
271 static const unsigned int supportedChannels
[] = {1, 2, 3, 4, 5, 6, NULL
};
272 return supportedChannels
;
275 const unsigned int *AC3Encoder::supportedSamplerates(void)
277 static const unsigned int supportedRates
[] = {48000, 44100, 32000, NULL
};
278 return supportedRates
;
281 bool AC3Encoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
283 if(containerType
.compare("Wave", Qt::CaseInsensitive
) == 0)
285 if(formatType
.compare("PCM", Qt::CaseInsensitive
) == 0)
294 const AbstractEncoderInfo
*AC3Encoder::getEncoderInfo(void)
296 return &g_aftenEncoderInfo
;