OSDN Git Service

Replaced the Opus encoder/decoder binary wit custom binaries that support UTF-8 file...
[lamexp/LameXP.git] / src / Encoder_Opus.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Encoder_Opus.h"
23
24 #include "Global.h"
25 #include "Model_Settings.h"
26
27 #include <QProcess>
28 #include <QDir>
29 #include <QUUid>
30
31 OpusEncoder::OpusEncoder(void)
32 :
33         m_binary(lamexp_lookup_tool("opusenc.exe"))
34 {
35         if(m_binary.isEmpty())
36         {
37                 throw "Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!";
38         }
39
40         m_configOptimizeFor = 0;
41         m_configEncodeComplexity = 10;
42         m_configFrameSize = 3;
43 }
44
45 OpusEncoder::~OpusEncoder(void)
46 {
47 }
48
49 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
50 {
51         const unsigned int fileDuration = metaInfo.fileDuration();
52         
53         QProcess process;
54         QStringList args;
55
56         switch(m_configRCMode)
57         {
58         case SettingsModel::VBRMode:
59                 args << "--vbr";
60                 break;
61         case SettingsModel::ABRMode:
62                 args << "-cvbr";
63                 break;
64         case SettingsModel::CBRMode:
65                 args << "--hard-cbr";
66                 break;
67         default:
68                 throw "Bad rate-control mode!";
69                 break;
70         }
71
72         switch(m_configOptimizeFor)
73         {
74         case 0:
75                 args << "--music";
76                 break;
77         case 1:
78                 args << "--speech";
79                 break;
80         }
81
82         args << "--comp" << QString::number(m_configEncodeComplexity);
83
84         switch(m_configFrameSize)
85         {
86         case 0:
87                 args << "--framesize" << "2.5";
88                 break;
89         case 1:
90                 args << "--framesize" << "5";
91                 break;
92         case 2:
93                 args << "--framesize" << "10";
94                 break;
95         case 3:
96                 args << "--framesize" << "20";
97                 break;
98         case 4:
99                 args << "--framesize" << "40";
100                 break;
101         case 5:
102                 args << "--framesize" << "60";
103                 break;
104         }
105
106         args << QString("--bitrate") << QString::number(qMax(0, qMin(500, m_configBitrate * 8)));
107
108         if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
109         if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
110         if(!metaInfo.fileAlbum().isEmpty()) args << "--comment" << QString("album=%1").arg(metaInfo.fileAlbum());
111         if(!metaInfo.fileGenre().isEmpty()) args << "--comment" << QString("genre=%1").arg(metaInfo.fileGenre());
112         if(!metaInfo.fileComment().isEmpty()) args << "--comment" << QString("comment=%1").arg(metaInfo.fileComment());
113         if(metaInfo.fileYear()) args << "--comment" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
114         if(metaInfo.filePosition()) args << "--comment" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
115         
116         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
117
118         args << QDir::toNativeSeparators(sourceFile);
119         args << QDir::toNativeSeparators(outputFile);
120
121         if(!startProcess(process, m_binary, args))
122         {
123                 return false;
124         }
125
126         bool bTimeout = false;
127         bool bAborted = false;
128         int prevProgress = -1;
129
130         QRegExp regExp("\\[(-|\\\\|/|\\|)\\]\\s*(\\d+):(\\d+):(\\d+)");
131
132         while(process.state() != QProcess::NotRunning)
133         {
134                 if(*abortFlag)
135                 {
136                         process.kill();
137                         bAborted = true;
138                         emit messageLogged("\nABORTED BY USER !!!");
139                         break;
140                 }
141                 process.waitForReadyRead(m_processTimeoutInterval);
142                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
143                 {
144                         process.kill();
145                         qWarning("Opus process timed out <-- killing!");
146                         emit messageLogged("\nPROCESS TIMEOUT !!!");
147                         bTimeout = true;
148                         break;
149                 }
150                 while(process.bytesAvailable() > 0)
151                 {
152                         QByteArray line = process.readLine();
153                         QString text = QString::fromUtf8(line.constData()).simplified();
154                         if(regExp.lastIndexIn(text) >= 0)
155                         {
156                                 bool ok[3] = {false, false, false};
157                                 int h = regExp.cap(2).toInt(&ok[0]);
158                                 int m = regExp.cap(3).toInt(&ok[1]);
159                                 int s = regExp.cap(4).toInt(&ok[2]);
160                                 if(ok[0] && ok[1] && ok[2] && (fileDuration > 0))
161                                 {
162                                         int filePosition = (h * 3600) + (m * 60) + s;
163                                         int newProgress = qRound((static_cast<double>(filePosition) / static_cast<double>(fileDuration)) * 100.0);
164                                         if(newProgress > prevProgress)
165                                         {
166                                                 emit statusUpdated(newProgress);
167                                                 prevProgress = qMin(newProgress + 2, 99);
168                                         }
169                                 }
170                         }
171                         else if(!text.isEmpty())
172                         {
173                                 emit messageLogged(text);
174                         }
175                 }
176         }
177
178         process.waitForFinished();
179         if(process.state() != QProcess::NotRunning)
180         {
181                 process.kill();
182                 process.waitForFinished(-1);
183         }
184         
185         emit statusUpdated(100);
186         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
187
188         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
189         {
190                 return false;
191         }
192         
193         return true;
194 }
195
196 void OpusEncoder::setOptimizeFor(int optimizeFor)
197 {
198         m_configOptimizeFor = qBound(0, optimizeFor, 2);
199 }
200
201 void OpusEncoder::setEncodeComplexity(int complexity)
202 {
203         m_configEncodeComplexity = qBound(0, complexity, 10);
204 }
205
206 void OpusEncoder::setFrameSize(int frameSize)
207 {
208         m_configFrameSize = qBound(0, frameSize, 5);
209 }
210
211 QString OpusEncoder::extension(void)
212 {
213         return "opus";
214 }
215
216 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
217 {
218         if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
219         {
220                 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
221                 {
222                         return true;
223                 }
224         }
225
226         return false;
227 }
228
229 const unsigned int *OpusEncoder::supportedChannelCount(void)
230 {
231         return NULL;
232 }
233
234 const unsigned int *OpusEncoder::supportedBitdepths(void)
235 {
236         return NULL;
237 }
238
239 const bool OpusEncoder::needsTimingInfo(void)
240 {
241         return true;
242 }