1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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 "Decoder_Opus.h"
29 #include <MUtils/Exception.h>
37 bool OpusDecoder::m_disableResampling
= false;
39 OpusDecoder::OpusDecoder(void)
41 m_binary(lamexp_tools_lookup("opusdec.exe"))
43 if(m_binary
.isEmpty())
45 MUTILS_THROW("Error initializing Opus decoder. Tool 'opusdec.exe' is not registred!");
49 OpusDecoder::~OpusDecoder(void)
53 bool OpusDecoder::decode(const QString
&sourceFile
, const QString
&outputFile
, QAtomicInt
&abortFlag
)
58 if(m_disableResampling
)
60 args
<< "--no-resample";
63 args
<< QDir::toNativeSeparators(sourceFile
);
64 args
<< QDir::toNativeSeparators(outputFile
);
66 if(!startProcess(process
, m_binary
, args
))
71 bool bTimeout
= false;
72 bool bAborted
= false;
73 int prevProgress
= -1;
75 QRegExp
regExp("\\((\\d+)%\\)");
77 while(process
.state() != QProcess::NotRunning
)
79 if(checkFlag(abortFlag
))
83 emit
messageLogged("\nABORTED BY USER !!!");
86 process
.waitForReadyRead(m_processTimeoutInterval
);
87 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
90 qWarning("opusdec process timed out <-- killing!");
91 emit
messageLogged("\nPROCESS TIMEOUT !!!");
95 while(process
.bytesAvailable() > 0)
97 QByteArray line
= process
.readLine();
98 QString text
= QString::fromUtf8(line
.constData()).simplified();
99 if(regExp
.lastIndexIn(text
) >= 0)
102 int progress
= regExp
.cap(1).toInt(&ok
);
103 if(ok
&& (progress
> prevProgress
))
105 emit
statusUpdated(progress
);
106 prevProgress
= qMin(progress
+ 2, 99);
109 else if(!text
.isEmpty())
111 emit
messageLogged(text
);
116 process
.waitForFinished();
117 if(process
.state() != QProcess::NotRunning
)
120 process
.waitForFinished(-1);
123 emit
statusUpdated(100);
124 emit
messageLogged(QString().sprintf("\nExited with code: 0x%04X", process
.exitCode()));
126 if(bTimeout
|| bAborted
|| process
.exitCode() != EXIT_SUCCESS
)
134 bool OpusDecoder::isFormatSupported(const QString
&containerType
, const QString
&containerProfile
, const QString
&formatType
, const QString
&formatProfile
, const QString
&formatVersion
)
136 if(containerType
.compare("OGG", Qt::CaseInsensitive
) == 0)
138 if(formatType
.compare("Opus", Qt::CaseInsensitive
) == 0)
149 const AbstractDecoder::supportedType_t
*OpusDecoder::supportedTypes(void)
151 static const char *exts
[] =
156 static const supportedType_t s_supportedTypes
[] =
158 { "Opus Audio Codec", exts
},
162 return s_supportedTypes
;