OSDN Git Service

Fixed a possible use-after-free bug in initialization code.
[lamexp/LameXP.git] / src / Encoder_AC3.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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_AC3.h"
24
25 #include "Global.h"
26 #include "Model_Settings.h"
27
28 #include <QProcess>
29 #include <QDir>
30
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};
32
33 ///////////////////////////////////////////////////////////////////////////////
34 // Encoder Info
35 ///////////////////////////////////////////////////////////////////////////////
36
37 class AC3EncoderInfo : public AbstractEncoderInfo
38 {
39         virtual bool isModeSupported(int mode) const
40         {
41                 switch(mode)
42                 {
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!");
52                 }
53         }
54
55         virtual int valueCount(int mode) const
56         {
57                 switch(mode)
58                 {
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!");
70                 }
71         }
72
73         virtual int valueAt(int mode, int index) const
74         {
75                 switch(mode)
76                 {
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!");
86                 }
87         }
88
89         virtual int valueType(int mode) const
90         {
91                 switch(mode)
92                 {
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!");
102                 }
103         }
104
105         virtual const char *description(void) const
106         {
107                 static const char* s_description = "Aften: A/52 Audio Encoder";
108                 return s_description;
109         }
110
111         virtual const char *extension(void) const
112         {
113                 static const char* s_extension = "ac3";
114                 return s_extension;
115         }
116 }
117 static const g_aftenEncoderInfo;
118
119 ///////////////////////////////////////////////////////////////////////////////
120 // Encoder implementation
121 ///////////////////////////////////////////////////////////////////////////////
122
123 AC3Encoder::AC3Encoder(void)
124 :
125         m_binary(lamexp_tools_lookup("aften.exe"))
126 {
127         if(m_binary.isEmpty())
128         {
129                 MUTILS_THROW("Error initializing FLAC encoder. Tool 'aften.exe' is not registred!");
130         }
131
132         m_configAudioCodingMode = 0;
133         m_configDynamicRangeCompression = 5;
134         m_configExponentSearchSize = 8;
135         m_configFastBitAllocation = false;
136 }
137
138 AC3Encoder::~AC3Encoder(void)
139 {
140 }
141
142 bool AC3Encoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
143 {
144         QProcess process;
145         QStringList args;
146
147         switch(m_configRCMode)
148         {
149         case SettingsModel::VBRMode:
150                 args << "-q" << QString::number(qBound(0, m_configBitrate * 16, 1023));
151                 break;
152         case SettingsModel::CBRMode:
153                 args << "-b" << QString::number(g_ac3BitratesLUT[qBound(0, m_configBitrate, 18)]);
154                 break;
155         default:
156                 MUTILS_THROW("Bad rate-control mode!");
157                 break;
158         }
159
160         if(m_configAudioCodingMode >= 1)
161         {
162                 args << "-acmod" << QString::number(m_configAudioCodingMode - 1);
163         }
164         if(m_configDynamicRangeCompression != 5)
165         {
166                 args << "-dynrng" << QString::number(m_configDynamicRangeCompression);
167         }
168         if(m_configExponentSearchSize != 8)
169         {
170                 args << "-exps" << QString::number(m_configExponentSearchSize);
171         }
172         if(m_configFastBitAllocation)
173         {
174                 args << "-fba" << QString::number(1);
175         }
176
177         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
178
179         args << QDir::toNativeSeparators(sourceFile);
180         args << QDir::toNativeSeparators(outputFile);
181
182         if(!startProcess(process, m_binary, args))
183         {
184                 return false;
185         }
186
187         bool bTimeout = false;
188         bool bAborted = false;
189         int prevProgress = -1;
190
191         QRegExp regExp("progress:(\\s+)(\\d+)%(\\s+)\\|");
192
193         while(process.state() != QProcess::NotRunning)
194         {
195                 if(*abortFlag)
196                 {
197                         process.kill();
198                         bAborted = true;
199                         emit messageLogged("\nABORTED BY USER !!!");
200                         break;
201                 }
202                 process.waitForReadyRead(m_processTimeoutInterval);
203                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
204                 {
205                         process.kill();
206                         qWarning("Aften process timed out <-- killing!");
207                         emit messageLogged("\nPROCESS TIMEOUT !!!");
208                         bTimeout = true;
209                         break;
210                 }
211                 while(process.bytesAvailable() > 0)
212                 {
213                         QByteArray line = process.readLine();
214                         QString text = QString::fromUtf8(line.constData()).simplified();
215                         if(regExp.lastIndexIn(text) >= 0)
216                         {
217                                 bool ok = false;
218                                 int progress = regExp.cap(2).toInt(&ok);
219                                 if(ok && (progress > prevProgress))
220                                 {
221                                         emit statusUpdated(progress);
222                                         prevProgress = qMin(progress + 2, 99);
223                                 }
224                         }
225                         else if(!text.isEmpty())
226                         {
227                                 emit messageLogged(text);
228                         }
229                 }
230         }
231
232         process.waitForFinished();
233         if(process.state() != QProcess::NotRunning)
234         {
235                 process.kill();
236                 process.waitForFinished(-1);
237         }
238         
239         emit statusUpdated(100);
240         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
241
242         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
243         {
244                 return false;
245         }
246         
247         return true;
248 }
249
250 void AC3Encoder::setAudioCodingMode(int value)
251 {
252         m_configAudioCodingMode = qMin(8, qMax(0, value));
253 }
254
255 void AC3Encoder::setDynamicRangeCompression(int value)
256 {
257         m_configDynamicRangeCompression = qMin(5, qMax(0, value));
258 }
259
260 void AC3Encoder::setExponentSearchSize(int value)
261 {
262         m_configExponentSearchSize = qMin(32, qMax(1, value));
263 }
264
265 void AC3Encoder::setFastBitAllocation(bool value)
266 {
267         m_configFastBitAllocation = value;
268 }
269
270 const unsigned int *AC3Encoder::supportedChannelCount(void)
271 {
272         static const unsigned int supportedChannels[] = {1, 2, 3, 4, 5, 6, NULL};
273         return supportedChannels;
274 }
275
276 const unsigned int *AC3Encoder::supportedSamplerates(void)
277 {
278         static const unsigned int supportedRates[] = {48000, 44100, 32000, NULL};
279         return supportedRates;
280 }
281
282 bool AC3Encoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
283 {
284         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
285         {
286                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
287                 {
288                         return true;
289                 }
290         }
291
292         return false;
293 }
294
295 const AbstractEncoderInfo *AC3Encoder::getEncoderInfo(void)
296 {
297         return &g_aftenEncoderInfo;
298 }