OSDN Git Service

Updated VS2012 project 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_std(lamexp_lookup_tool("opusenc_std.exe")),
34         m_binary_ea7(lamexp_lookup_tool("opusenc_ea7.exe"))
35 {
36         if(m_binary_std.isEmpty() || m_binary_ea7.isEmpty())
37         {
38                 throw "Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!";
39         }
40
41         m_configOptimizeFor = 0;
42         m_configEncodeComplexity = 10;
43         m_configFrameSize = 3;
44         m_configExpAnalysisOn = true;
45 }
46
47 OpusEncoder::~OpusEncoder(void)
48 {
49 }
50
51 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
52 {
53         const unsigned int fileDuration = metaInfo.fileDuration();
54         
55         QProcess process;
56         QStringList args;
57
58         switch(m_configRCMode)
59         {
60         case SettingsModel::VBRMode:
61                 args << "--vbr";
62                 break;
63         case SettingsModel::ABRMode:
64                 args << "--cvbr";
65                 break;
66         case SettingsModel::CBRMode:
67                 args << "--hard-cbr";
68                 break;
69         default:
70                 throw "Bad rate-control mode!";
71                 break;
72         }
73
74         //switch(m_configOptimizeFor)
75         //{
76         //case 0:
77         //      args << "--music";
78         //      break;
79         //case 1:
80         //      args << "--speech";
81         //      break;
82         //}
83
84         args << "--comp" << QString::number(m_configEncodeComplexity);
85
86         switch(m_configFrameSize)
87         {
88         case 0:
89                 args << "--framesize" << "2.5";
90                 break;
91         case 1:
92                 args << "--framesize" << "5";
93                 break;
94         case 2:
95                 args << "--framesize" << "10";
96                 break;
97         case 3:
98                 args << "--framesize" << "20";
99                 break;
100         case 4:
101                 args << "--framesize" << "40";
102                 break;
103         case 5:
104                 args << "--framesize" << "60";
105                 break;
106         }
107
108         args << QString("--bitrate") << QString::number(qMax(0, qMin(500, m_configBitrate * 8)));
109
110         if(!metaInfo.fileName().isEmpty()) args << "--title" << metaInfo.fileName();
111         if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << metaInfo.fileArtist();
112         if(!metaInfo.fileAlbum().isEmpty()) args << "--comment" << QString("album=%1").arg(metaInfo.fileAlbum());
113         if(!metaInfo.fileGenre().isEmpty()) args << "--comment" << QString("genre=%1").arg(metaInfo.fileGenre());
114         if(!metaInfo.fileComment().isEmpty()) args << "--comment" << QString("comment=%1").arg(metaInfo.fileComment());
115         if(metaInfo.fileYear()) args << "--comment" << QString("date=%1").arg(QString::number(metaInfo.fileYear()));
116         if(metaInfo.filePosition()) args << "--comment" << QString("track=%1").arg(QString::number(metaInfo.filePosition()));
117         
118         if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
119
120         args << QDir::toNativeSeparators(sourceFile);
121         args << QDir::toNativeSeparators(outputFile);
122
123         if(!startProcess(process, m_configExpAnalysisOn ? m_binary_ea7 : m_binary_std, args))
124         {
125                 return false;
126         }
127
128         bool bTimeout = false;
129         bool bAborted = false;
130         int prevProgress = -1;
131
132         QRegExp regExp("\\((\\d+)%\\)");
133
134         while(process.state() != QProcess::NotRunning)
135         {
136                 if(*abortFlag)
137                 {
138                         process.kill();
139                         bAborted = true;
140                         emit messageLogged("\nABORTED BY USER !!!");
141                         break;
142                 }
143                 process.waitForReadyRead(m_processTimeoutInterval);
144                 if(!process.bytesAvailable() && process.state() == QProcess::Running)
145                 {
146                         process.kill();
147                         qWarning("Opus process timed out <-- killing!");
148                         emit messageLogged("\nPROCESS TIMEOUT !!!");
149                         bTimeout = true;
150                         break;
151                 }
152                 while(process.bytesAvailable() > 0)
153                 {
154                         QByteArray line = process.readLine();
155                         QString text = QString::fromUtf8(line.constData()).simplified();
156                         if(regExp.lastIndexIn(text) >= 0)
157                         {
158                                 bool ok = false;
159                                 int progress = regExp.cap(1).toInt(&ok);
160                                 if(ok && (progress > prevProgress))
161                                 {
162                                         emit statusUpdated(progress);
163                                         prevProgress = qMin(progress + 2, 99);
164                                 }
165                         }
166                         else if(!text.isEmpty())
167                         {
168                                 emit messageLogged(text);
169                         }
170                 }
171         }
172
173         process.waitForFinished();
174         if(process.state() != QProcess::NotRunning)
175         {
176                 process.kill();
177                 process.waitForFinished(-1);
178         }
179         
180         emit statusUpdated(100);
181         emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
182
183         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
184         {
185                 return false;
186         }
187         
188         return true;
189 }
190
191 void OpusEncoder::setOptimizeFor(int optimizeFor)
192 {
193         m_configOptimizeFor = qBound(0, optimizeFor, 2);
194 }
195
196 void OpusEncoder::setEncodeComplexity(int complexity)
197 {
198         m_configEncodeComplexity = qBound(0, complexity, 10);
199 }
200
201 void OpusEncoder::setFrameSize(int frameSize)
202 {
203         m_configFrameSize = qBound(0, frameSize, 5);
204 }
205
206 void OpusEncoder::setExpAnalysisOn(bool expAnalysisOn)
207 {
208         m_configExpAnalysisOn = expAnalysisOn;
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         static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
237         return supportedBPS;
238 }
239
240 const bool OpusEncoder::needsTimingInfo(void)
241 {
242         return true;
243 }