Build fix.
[LameXP.git] / src / Encoder_AC3.cpp
blobb9348a44cce053c2a58652d8aba18a1cf258d9c4
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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 "Encoder_AC3.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
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 ///////////////////////////////////////////////////////////////////////////////
34 // Encoder Info
35 ///////////////////////////////////////////////////////////////////////////////
37 class AC3EncoderInfo : public AbstractEncoderInfo
39 virtual bool isModeSupported(int mode) const
41 switch(mode)
43 case SettingsModel::VBRMode:
44 case SettingsModel::CBRMode:
45 return true;
46 break;
47 case SettingsModel::ABRMode:
48 return false;
49 break;
50 default:
51 MUTILS_THROW("Bad RC mode specified!");
55 virtual int valueCount(int mode) const
57 switch(mode)
59 case SettingsModel::VBRMode:
60 return 65;
61 break;
62 case SettingsModel::ABRMode:
63 return 0;
64 break;
65 case SettingsModel::CBRMode:
66 return 19;
67 break;
68 default:
69 MUTILS_THROW("Bad RC mode specified!");
73 virtual int valueAt(int mode, int index) const
75 switch(mode)
77 case SettingsModel::VBRMode:
78 return qBound(0, index * 16, 1023);
79 break;
80 case SettingsModel::ABRMode:
81 case SettingsModel::CBRMode:
82 return g_ac3BitratesLUT[qBound(0, index, 18)];
83 break;
84 default:
85 MUTILS_THROW("Bad RC mode specified!");
89 virtual int valueType(int mode) const
91 switch(mode)
93 case SettingsModel::VBRMode:
94 return TYPE_QUALITY_LEVEL_INT;
95 break;
96 case SettingsModel::ABRMode:
97 case SettingsModel::CBRMode:
98 return TYPE_BITRATE;
99 break;
100 default:
101 MUTILS_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 virtual const char *extension(void) const
113 static const char* s_extension = "ac3";
114 return s_extension;
117 virtual bool isResamplingSupported(void) const
119 return false;
122 static const g_aftenEncoderInfo;
124 ///////////////////////////////////////////////////////////////////////////////
125 // Encoder implementation
126 ///////////////////////////////////////////////////////////////////////////////
128 AC3Encoder::AC3Encoder(void)
130 m_binary(lamexp_tools_lookup(L1S("aften.exe")))
132 if(m_binary.isEmpty())
134 MUTILS_THROW("Error initializing FLAC encoder. Tool 'aften.exe' is not registred!");
137 m_configAudioCodingMode = 0;
138 m_configDynamicRangeCompression = 5;
139 m_configExponentSearchSize = 8;
140 m_configFastBitAllocation = false;
143 AC3Encoder::~AC3Encoder(void)
147 bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
149 QProcess process;
150 QStringList args;
152 switch(m_configRCMode)
154 case SettingsModel::VBRMode:
155 args << L1S("-q") << QString::number(qBound(0, m_configBitrate * 16, 1023));
156 break;
157 case SettingsModel::CBRMode:
158 args << L1S("-b") << QString::number(g_ac3BitratesLUT[qBound(0, m_configBitrate, 18)]);
159 break;
160 default:
161 MUTILS_THROW("Bad rate-control mode!");
162 break;
165 if(m_configAudioCodingMode >= 1)
167 args << L1S("-acmod") << QString::number(m_configAudioCodingMode - 1);
169 if(m_configDynamicRangeCompression != 5)
171 args << L1S("-dynrng") << QString::number(m_configDynamicRangeCompression);
173 if(m_configExponentSearchSize != 8)
175 args << L1S("-exps") << QString::number(m_configExponentSearchSize);
177 if(m_configFastBitAllocation)
179 args << L1S("-fba") << QString::number(1);
182 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
184 args << QDir::toNativeSeparators(sourceFile);
185 args << QDir::toNativeSeparators(outputFile);
187 if(!startProcess(process, m_binary, args))
189 return false;
192 bool bTimeout = false;
193 bool bAborted = false;
194 int prevProgress = -1;
196 QRegExp regExp(L1S("progress:(\\s+)(\\d+)%(\\s+)\\|"));
198 while(process.state() != QProcess::NotRunning)
200 if (checkFlag(abortFlag))
202 process.kill();
203 bAborted = true;
204 emit messageLogged(L1S("\nABORTED BY USER !!!"));
205 break;
207 process.waitForReadyRead(m_processTimeoutInterval);
208 if(!process.bytesAvailable() && process.state() == QProcess::Running)
210 process.kill();
211 qWarning("Aften process timed out <-- killing!");
212 emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
213 bTimeout = true;
214 break;
216 while(process.bytesAvailable() > 0)
218 QByteArray line = process.readLine();
219 QString text = QString::fromUtf8(line.constData()).simplified();
220 if(regExp.lastIndexIn(text) >= 0)
222 bool ok = false;
223 int progress = regExp.cap(2).toInt(&ok);
224 if(ok && (progress > prevProgress))
226 emit statusUpdated(progress);
227 prevProgress = qMin(progress + 2, 99);
230 else if(!text.isEmpty())
232 emit messageLogged(text);
237 process.waitForFinished();
238 if(process.state() != QProcess::NotRunning)
240 process.kill();
241 process.waitForFinished(-1);
244 emit statusUpdated(100);
245 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
247 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
249 return false;
252 return true;
255 void AC3Encoder::setAudioCodingMode(int value)
257 m_configAudioCodingMode = qMin(8, qMax(0, value));
260 void AC3Encoder::setDynamicRangeCompression(int value)
262 m_configDynamicRangeCompression = qMin(5, qMax(0, value));
265 void AC3Encoder::setExponentSearchSize(int value)
267 m_configExponentSearchSize = qMin(32, qMax(1, value));
270 void AC3Encoder::setFastBitAllocation(bool value)
272 m_configFastBitAllocation = value;
275 const unsigned int *AC3Encoder::supportedChannelCount(void)
277 static const unsigned int supportedChannels[] = {1, 2, 3, 4, 5, 6, NULL};
278 return supportedChannels;
281 const unsigned int *AC3Encoder::supportedSamplerates(void)
283 static const unsigned int supportedRates[] = {48000, 44100, 32000, NULL};
284 return supportedRates;
287 bool AC3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
289 if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
291 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
293 return true;
297 return false;
300 const AbstractEncoderInfo *AC3Encoder::getEncoderInfo(void)
302 return &g_aftenEncoderInfo;