Major redesign of the AudioFileModel class: Split data into separate AudioFileModel_M...
[LameXP.git] / src / Encoder_Opus.cpp
blob756f9aab199c29dd990b49fdf227d475ab620c87
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Encoder_Opus.h"
24 #include "Global.h"
25 #include "Model_Settings.h"
27 #include <QProcess>
28 #include <QDir>
29 #include <QUUid>
31 ///////////////////////////////////////////////////////////////////////////////
32 // Encoder Info
33 ///////////////////////////////////////////////////////////////////////////////
35 class OpusEncoderInfo : public AbstractEncoderInfo
37 virtual bool isModeSupported(int mode) const
39 switch(mode)
41 case SettingsModel::VBRMode:
42 case SettingsModel::ABRMode:
43 case SettingsModel::CBRMode:
44 return true;
45 break;
46 default:
47 throw "Bad RC mode specified!";
51 virtual int valueCount(int mode) const
53 switch(mode)
55 case SettingsModel::VBRMode:
56 case SettingsModel::ABRMode:
57 case SettingsModel::CBRMode:
58 return 32;
59 break;
60 default:
61 throw "Bad RC mode specified!";
65 virtual int valueAt(int mode, int index) const
67 switch(mode)
69 case SettingsModel::VBRMode:
70 case SettingsModel::ABRMode:
71 case SettingsModel::CBRMode:
72 return qBound(8, (index + 1) * 8, 256);
73 break;
74 default:
75 throw "Bad RC mode specified!";
79 virtual int valueType(int mode) const
81 switch(mode)
83 case SettingsModel::VBRMode:
84 case SettingsModel::ABRMode:
85 return TYPE_APPROX_BITRATE;
86 break;
87 case SettingsModel::CBRMode:
88 return TYPE_BITRATE;
89 break;
90 default:
91 throw "Bad RC mode specified!";
95 virtual const char *description(void) const
97 static const char* s_description = "Opus-Tools OpusEnc (libopus)";
98 return s_description;
101 static const g_opusEncoderInfo;
103 ///////////////////////////////////////////////////////////////////////////////
104 // Encoder implementation
105 ///////////////////////////////////////////////////////////////////////////////
107 OpusEncoder::OpusEncoder(void)
109 m_binary(lamexp_lookup_tool("opusenc.exe"))
111 if(m_binary.isEmpty())
113 throw "Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!";
116 m_configOptimizeFor = 0;
117 m_configEncodeComplexity = 10;
118 m_configFrameSize = 3;
121 OpusEncoder::~OpusEncoder(void)
125 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel &metaInfo, const QString &outputFile, volatile bool *abortFlag)
127 const unsigned int fileDuration = metaInfo.fileDuration();
129 QProcess process;
130 QStringList args;
132 switch(m_configRCMode)
134 case SettingsModel::VBRMode:
135 args << "--vbr";
136 break;
137 case SettingsModel::ABRMode:
138 args << "--cvbr";
139 break;
140 case SettingsModel::CBRMode:
141 args << "--hard-cbr";
142 break;
143 default:
144 throw "Bad rate-control mode!";
145 break;
148 args << "--comp" << QString::number(m_configEncodeComplexity);
150 switch(m_configFrameSize)
152 case 0:
153 args << "--framesize" << "2.5";
154 break;
155 case 1:
156 args << "--framesize" << "5";
157 break;
158 case 2:
159 args << "--framesize" << "10";
160 break;
161 case 3:
162 args << "--framesize" << "20";
163 break;
164 case 4:
165 args << "--framesize" << "40";
166 break;
167 case 5:
168 args << "--framesize" << "60";
169 break;
172 args << QString("--bitrate") << QString::number(qBound(8, (m_configBitrate + 1) * 8, 256));
174 if(!metaInfo.fileName().isEmpty()) args << "--title" << cleanTag(metaInfo.fileName());
175 if(!metaInfo.fileArtist().isEmpty()) args << "--artist" << cleanTag(metaInfo.fileArtist());
176 if(!metaInfo.fileAlbum().isEmpty()) args << "--album" << cleanTag(metaInfo.fileAlbum());
177 if(!metaInfo.fileGenre().isEmpty()) args << "--genre" << cleanTag(metaInfo.fileGenre());
178 if(metaInfo.fileYear()) args << "--date" << QString::number(metaInfo.fileYear());
179 if(metaInfo.filePosition()) args << "--comment" << QString("tracknumber=%1").arg(QString::number(metaInfo.filePosition()));
180 if(!metaInfo.fileComment().isEmpty()) args << "--comment" << QString("comment=%1").arg(cleanTag(metaInfo.fileComment()));
182 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
184 args << QDir::toNativeSeparators(sourceFile);
185 args << QDir::toNativeSeparators(outputFile);
187 if(!startProcess(process, m_binary, args))
189 return false;
192 bool bTimeout = false;
193 bool bAborted = false;
194 int prevProgress = -1;
196 QRegExp regExp("\\((\\d+)%\\)");
198 while(process.state() != QProcess::NotRunning)
200 if(*abortFlag)
202 process.kill();
203 bAborted = true;
204 emit messageLogged("\nABORTED BY USER !!!");
205 break;
207 process.waitForReadyRead(m_processTimeoutInterval);
208 if(!process.bytesAvailable() && process.state() == QProcess::Running)
210 process.kill();
211 qWarning("Opus process timed out <-- killing!");
212 emit messageLogged("\nPROCESS TIMEOUT !!!");
213 bTimeout = true;
214 break;
216 while(process.bytesAvailable() > 0)
218 QByteArray line = process.readLine();
219 QString text = QString::fromUtf8(line.constData()).simplified();
220 if(regExp.lastIndexIn(text) >= 0)
222 bool ok = false;
223 int progress = regExp.cap(1).toInt(&ok);
224 if(ok && (progress > prevProgress))
226 emit statusUpdated(progress);
227 prevProgress = qMin(progress + 2, 99);
230 else if(!text.isEmpty())
232 emit messageLogged(text);
237 process.waitForFinished();
238 if(process.state() != QProcess::NotRunning)
240 process.kill();
241 process.waitForFinished(-1);
244 emit statusUpdated(100);
245 emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
247 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
249 return false;
252 return true;
255 void OpusEncoder::setOptimizeFor(int optimizeFor)
257 m_configOptimizeFor = qBound(0, optimizeFor, 2);
260 void OpusEncoder::setEncodeComplexity(int complexity)
262 m_configEncodeComplexity = qBound(0, complexity, 10);
265 void OpusEncoder::setFrameSize(int frameSize)
267 m_configFrameSize = qBound(0, frameSize, 5);
270 QString OpusEncoder::extension(void)
272 return "opus";
275 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
277 if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
279 if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
281 return true;
285 return false;
288 const unsigned int *OpusEncoder::supportedChannelCount(void)
290 return NULL;
293 const unsigned int *OpusEncoder::supportedBitdepths(void)
295 static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
296 return supportedBPS;
299 const bool OpusEncoder::needsTimingInfo(void)
301 return true;
304 const AbstractEncoderInfo *OpusEncoder::getEncoderInfo(void)
306 return &g_opusEncoderInfo;