Happy new year 2016!
[LameXP.git] / src / Encoder_DCA.cpp
blob61a2097365e31a12823669aff1602e1f64ca4763
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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_DCA.h"
25 #include "Global.h"
26 #include "Model_Settings.h"
28 #include <QProcess>
29 #include <QDir>
30 #include <limits.h>
32 static int index2bitrate(const int index)
34 return (index < 8) ? ((index + 1) * 32) : ((index < 12) ? ((index - 3) * 64) : ((index < 24) ? (index - 7) * 128 : (index - 15) * 256));
37 ///////////////////////////////////////////////////////////////////////////////
38 // Encoder Info
39 ///////////////////////////////////////////////////////////////////////////////
41 class DCAEncoderInfo : public AbstractEncoderInfo
43 virtual bool isModeSupported(int mode) const
45 switch(mode)
47 case SettingsModel::VBRMode:
48 case SettingsModel::ABRMode:
49 return false;
50 break;
51 case SettingsModel::CBRMode:
52 return true;
53 break;
54 default:
55 MUTILS_THROW("Bad RC mode specified!");
59 virtual int valueCount(int mode) const
61 switch(mode)
63 case SettingsModel::VBRMode:
64 case SettingsModel::ABRMode:
65 return 0;
66 break;
67 case SettingsModel::CBRMode:
68 return 32;
69 break;
70 default:
71 MUTILS_THROW("Bad RC mode specified!");
75 virtual int valueAt(int mode, int index) const
77 switch(mode)
79 case SettingsModel::VBRMode:
80 case SettingsModel::ABRMode:
81 return 0;
82 break;
83 case SettingsModel::CBRMode:
84 return qBound(32, index2bitrate(index), 4096);
85 break;
86 default:
87 MUTILS_THROW("Bad RC mode specified!");
91 virtual int valueType(int mode) const
93 switch(mode)
95 case SettingsModel::VBRMode:
96 return TYPE_QUALITY_LEVEL_INT;
97 break;
98 case SettingsModel::ABRMode:
99 case SettingsModel::CBRMode:
100 return TYPE_BITRATE;
101 break;
102 default:
103 MUTILS_THROW("Bad RC mode specified!");
107 virtual const char *description(void) const
109 static const char* s_description = "dcaenc-2 by Alexander E. Patrakov";
110 return s_description;
113 virtual const char *extension(void) const
115 static const char* s_extension = "dts";
116 return s_extension;
119 static const g_dcaEncoderInfo;
121 ///////////////////////////////////////////////////////////////////////////////
122 // Encoder implementation
123 ///////////////////////////////////////////////////////////////////////////////
125 DCAEncoder::DCAEncoder(void)
127 m_binary(lamexp_tools_lookup("dcaenc.exe"))
129 if(m_binary.isEmpty())
131 MUTILS_THROW("Error initializing DCA encoder. Tool 'dcaenc.exe' is not registred!");
135 DCAEncoder::~DCAEncoder(void)
139 bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
141 QProcess process;
142 QStringList args;
144 args << "-i" << QDir::toNativeSeparators(sourceFile);
145 args << "-o" << QDir::toNativeSeparators(outputFile);
146 args << "-b" << QString::number(qBound(32, index2bitrate(m_configBitrate), 4096));
148 if(!startProcess(process, m_binary, args))
150 return false;
153 bool bTimeout = false;
154 bool bAborted = false;
155 int prevProgress = -1;
157 QRegExp regExp("\\[(\\d+)\\.(\\d+)%\\]");
159 while(process.state() != QProcess::NotRunning)
161 if(*abortFlag)
163 process.kill();
164 bAborted = true;
165 emit messageLogged("\nABORTED BY USER !!!");
166 break;
168 process.waitForReadyRead(m_processTimeoutInterval);
169 if(!process.bytesAvailable() && process.state() == QProcess::Running)
171 process.kill();
172 qWarning("DCAENC process timed out <-- killing!");
173 emit messageLogged("\nPROCESS TIMEOUT !!!");
174 bTimeout = true;
175 break;
177 while(process.bytesAvailable() > 0)
179 QByteArray line = process.readLine();
180 QString text = QString::fromUtf8(line.constData()).simplified();
181 if(regExp.lastIndexIn(text) >= 0)
183 bool ok = false;
184 int progress = regExp.cap(1).toInt(&ok);
185 if(ok && (progress > prevProgress))
187 emit statusUpdated(progress);
188 prevProgress = qMin(progress + 2, 99);
191 else if(!text.isEmpty())
193 emit messageLogged(text);
198 process.waitForFinished();
199 if(process.state() != QProcess::NotRunning)
201 process.kill();
202 process.waitForFinished(-1);
205 emit statusUpdated(100);
206 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
208 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
210 return false;
213 return true;
216 bool DCAEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
218 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
220 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
222 return true;
226 return false;
229 const unsigned int *DCAEncoder::supportedChannelCount(void)
231 static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
232 return supportedChannels;
235 const unsigned int *DCAEncoder::supportedSamplerates(void)
237 static const unsigned int supportedRates[] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
238 return supportedRates;
241 const unsigned int *DCAEncoder::supportedBitdepths(void)
243 static const unsigned int supportedBPS[] = {16, 32, NULL};
244 return supportedBPS;
247 const AbstractEncoderInfo *DCAEncoder::getEncoderInfo(void)
249 return &g_dcaEncoderInfo;