OSDN Git Service

Bump release year.
[lamexp/LameXP.git] / src / Encoder_DCA.cpp
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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Encoder_DCA.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QProcess>
29 #include <QDir>
30 #include <limits.h>
31
32 static int index2bitrate(const int index)
33 {
34         return (index < 8) ? ((index + 1) * 32) : ((index < 12) ? ((index - 3) * 64) : ((index < 24) ? (index - 7) * 128 : (index - 15) * 256));
35 }
36
37 ///////////////////////////////////////////////////////////////////////////////
38 // Encoder Info
39 ///////////////////////////////////////////////////////////////////////////////
40
41 class DCAEncoderInfo : public AbstractEncoderInfo
42 {
43         virtual bool isModeSupported(int mode) const
44         {
45                 switch(mode)
46                 {
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!");
56                 }
57         }
58
59         virtual int valueCount(int mode) const
60         {
61                 switch(mode)
62                 {
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!");
72                 }
73         }
74
75         virtual int valueAt(int mode, int index) const
76         {
77                 switch(mode)
78                 {
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!");
88                 }
89         }
90
91         virtual int valueType(int mode) const
92         {
93                 switch(mode)
94                 {
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!");
104                 }
105         }
106
107         virtual const char *description(void) const
108         {
109                 static const char* s_description = "dcaenc-2 by Alexander E. Patrakov";
110                 return s_description;
111         }
112
113         virtual const char *extension(void) const
114         {
115                 static const char* s_extension = "dts";
116                 return s_extension;
117         }
118
119         virtual bool isResamplingSupported(void) const
120         {
121                 return false;
122         }
123 }
124 static const g_dcaEncoderInfo;
125
126 ///////////////////////////////////////////////////////////////////////////////
127 // Encoder implementation
128 ///////////////////////////////////////////////////////////////////////////////
129
130 DCAEncoder::DCAEncoder(void)
131 :
132         m_binary(lamexp_tools_lookup(L1S("dcaenc.exe")))
133 {
134         if(m_binary.isEmpty())
135         {
136                 MUTILS_THROW("Error initializing DCA encoder. Tool 'dcaenc.exe' is not registred!");
137         }
138 }
139
140 DCAEncoder::~DCAEncoder(void)
141 {
142 }
143
144 bool DCAEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, volatile bool *abortFlag)
145 {
146         QProcess process;
147         QStringList args;
148
149         args << L1S("-i") << QDir::toNativeSeparators(sourceFile);
150         args << L1S("-o") << QDir::toNativeSeparators(outputFile);
151         args << L1S("-b") << QString::number(qBound(32, index2bitrate(m_configBitrate), 4096));
152
153         if(!startProcess(process, m_binary, args))
154         {
155                 return false;
156         }
157
158         bool bTimeout = false;
159         bool bAborted = false;
160         int prevProgress = -1;
161
162         QRegExp regExp(L1S("\\[(\\d+)\\.(\\d+)%\\]"));
163
164         while(process.state() != QProcess::NotRunning)
165         {
166                 if(*abortFlag)
167                 {
168                         process.kill();
169                         bAborted = true;
170                         emit messageLogged(L1S("\nABORTED BY USER !!!"));
171                         break;
172                 }
173                 process.waitForReadyRead(m_processTimeoutInterval);
174                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
175                 {
176                         process.kill();
177                         qWarning("DCAENC process timed out <-- killing!");
178                         emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
179                         bTimeout = true;
180                         break;
181                 }
182                 while(process.bytesAvailable() > 0)
183                 {
184                         QByteArray line = process.readLine();
185                         QString text = QString::fromUtf8(line.constData()).simplified();
186                         if(regExp.lastIndexIn(text) >= 0)
187                         {
188                                 bool ok = false;
189                                 int progress = regExp.cap(1).toInt(&ok);
190                                 if(ok && (progress > prevProgress))
191                                 {
192                                         emit statusUpdated(progress);
193                                         prevProgress = qMin(progress + 2, 99);
194                                 }
195                         }
196                         else if(!text.isEmpty())
197                         {
198                                 emit messageLogged(text);
199                         }
200                 }
201         }
202
203         process.waitForFinished();
204         if(process.state() != QProcess::NotRunning)
205         {
206                 process.kill();
207                 process.waitForFinished(-1);
208         }
209         
210         emit statusUpdated(100);
211         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
212
213         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
214         {
215                 return false;
216         }
217         
218         return true;
219 }
220
221 bool DCAEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
222 {
223         if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
224         {
225                 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
226                 {
227                         return true;
228                 }
229         }
230
231         return false;
232 }
233
234 const unsigned int *DCAEncoder::supportedChannelCount(void)
235 {
236         static const unsigned int supportedChannels[] = {1, 2, 4, 5, 6, NULL};
237         return supportedChannels;
238 }
239
240 const unsigned int *DCAEncoder::supportedSamplerates(void)
241 {
242         static const unsigned int supportedRates[] = {48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, NULL};
243         return supportedRates;
244 }
245
246 const unsigned int *DCAEncoder::supportedBitdepths(void)
247 {
248         static const unsigned int supportedBPS[] = {16, 32, NULL};
249         return supportedBPS;
250 }
251
252 const AbstractEncoderInfo *DCAEncoder::getEncoderInfo(void)
253 {
254         return &g_dcaEncoderInfo;
255 }