Updated license info.
[LameXP.git] / src / Decoder_Opus.cpp
blobe613a6002d844c434b708e085c2bf8db2d542111
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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 "Decoder_Opus.h"
25 #include "Global.h"
27 #include <QDir>
28 #include <QProcess>
29 #include <QRegExp>
30 #include <QUuid>
32 bool OpusDecoder::m_disableResampling = false;
34 OpusDecoder::OpusDecoder(void)
36 m_binary(lamexp_lookup_tool("opusdec.exe"))
38 if(m_binary.isEmpty())
40 THROW("Error initializing Opus decoder. Tool 'opusdec.exe' is not registred!");
44 OpusDecoder::~OpusDecoder(void)
48 bool OpusDecoder::decode(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
50 QProcess process;
51 QStringList args;
53 if(m_disableResampling)
55 args << "--no-resample";
58 args << QDir::toNativeSeparators(sourceFile);
59 args << QDir::toNativeSeparators(outputFile);
61 if(!startProcess(process, m_binary, args))
63 return false;
66 bool bTimeout = false;
67 bool bAborted = false;
68 int prevProgress = -1;
70 QRegExp regExp("\\((\\d+)%\\)");
72 while(process.state() != QProcess::NotRunning)
74 if(*abortFlag)
76 process.kill();
77 bAborted = true;
78 emit messageLogged("\nABORTED BY USER !!!");
79 break;
81 process.waitForReadyRead(m_processTimeoutInterval);
82 if(!process.bytesAvailable() && process.state() == QProcess::Running)
84 process.kill();
85 qWarning("opusdec process timed out <-- killing!");
86 emit messageLogged("\nPROCESS TIMEOUT !!!");
87 bTimeout = true;
88 break;
90 while(process.bytesAvailable() > 0)
92 QByteArray line = process.readLine();
93 QString text = QString::fromUtf8(line.constData()).simplified();
94 if(regExp.lastIndexIn(text) >= 0)
96 bool ok = false;
97 int progress = regExp.cap(1).toInt(&ok);
98 if(ok && (progress > prevProgress))
100 emit statusUpdated(progress);
101 prevProgress = qMin(progress + 2, 99);
104 else if(!text.isEmpty())
106 emit messageLogged(text);
111 process.waitForFinished();
112 if(process.state() != QProcess::NotRunning)
114 process.kill();
115 process.waitForFinished(-1);
118 emit statusUpdated(100);
119 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
121 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
123 return false;
126 return true;
129 bool OpusDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
131 if(containerType.compare("OGG", Qt::CaseInsensitive) == 0)
133 if(formatType.compare("Opus", Qt::CaseInsensitive) == 0)
136 return true;
141 return false;
144 QStringList OpusDecoder::supportedTypes(void)
146 return QStringList() << "Opus Audio Codec (*.opus *.ogg)";