OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Decoder_Opus.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 "Decoder_Opus.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Exception.h>
30
31 //Qt
32 #include <QDir>
33 #include <QProcess>
34 #include <QRegExp>
35 #include <QUuid>
36
37 bool OpusDecoder::m_disableResampling = false;
38
39 OpusDecoder::OpusDecoder(void)
40 :
41         m_binary(lamexp_tools_lookup("opusdec.exe"))
42 {
43         if(m_binary.isEmpty())
44         {
45                 MUTILS_THROW("Error initializing Opus decoder. Tool 'opusdec.exe' is not registred!");
46         }
47 }
48
49 OpusDecoder::~OpusDecoder(void)
50 {
51 }
52
53 bool OpusDecoder::decode(const QString &sourceFile, const QString &outputFile, volatile bool *abortFlag)
54 {
55         QProcess process;
56         QStringList args;
57
58         if(m_disableResampling)
59         {
60                 args << "--no-resample";
61         }
62
63         args << QDir::toNativeSeparators(sourceFile);
64         args << QDir::toNativeSeparators(outputFile);
65
66         if(!startProcess(process, m_binary, args))
67         {
68                 return false;
69         }
70
71         bool bTimeout = false;
72         bool bAborted = false;
73         int prevProgress = -1;
74
75         QRegExp regExp("\\((\\d+)%\\)");
76
77         while(process.state() != QProcess::NotRunning)
78         {
79                 if(*abortFlag)
80                 {
81                         process.kill();
82                         bAborted = true;
83                         emit messageLogged("\nABORTED BY USER !!!");
84                         break;
85                 }
86                 process.waitForReadyRead(m_processTimeoutInterval);
87                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
88                 {
89                         process.kill();
90                         qWarning("opusdec process timed out <-- killing!");
91                         emit messageLogged("\nPROCESS TIMEOUT !!!");
92                         bTimeout = true;
93                         break;
94                 }
95                 while(process.bytesAvailable() > 0)
96                 {
97                         QByteArray line = process.readLine();
98                         QString text = QString::fromUtf8(line.constData()).simplified();
99                         if(regExp.lastIndexIn(text) >= 0)
100                         {
101                                 bool ok = false;
102                                 int progress = regExp.cap(1).toInt(&ok);
103                                 if(ok && (progress > prevProgress))
104                                 {
105                                         emit statusUpdated(progress);
106                                         prevProgress = qMin(progress + 2, 99);
107                                 }
108                         }
109                         else if(!text.isEmpty())
110                         {
111                                 emit messageLogged(text);
112                         }
113                 }
114         }
115
116         process.waitForFinished();
117         if(process.state() != QProcess::NotRunning)
118         {
119                 process.kill();
120                 process.waitForFinished(-1);
121         }
122         
123         emit statusUpdated(100);
124         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
125
126         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
127         {
128                 return false;
129         }
130         
131         return true;
132 }
133
134 bool OpusDecoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
135 {
136         if(containerType.compare("OGG", Qt::CaseInsensitive) == 0)
137         {
138                 if(formatType.compare("Opus", Qt::CaseInsensitive) == 0)
139                 {
140                         {
141                                 return true;
142                         }
143                 }
144         }
145
146         return false;
147 }
148
149 const AbstractDecoder::supportedType_t *OpusDecoder::supportedTypes(void)
150 {
151         static const char *exts[] =
152         {
153                 "opus", "ogg", NULL
154         };
155
156         static const supportedType_t s_supportedTypes[] =
157         {
158                 { "Opus Audio Codec", exts },
159                 { NULL, NULL }
160         };
161
162         return s_supportedTypes;
163 }