Updated Changelog.
[LameXP.git] / src / Encoder_Opus.cpp
blobfd984a0b3e32e25528f102a5fa3c7f32c75322d5
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2018 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 "Encoder_Opus.h"
25 //MUtils
26 #include <MUtils/Global.h>
28 //Internal
29 #include "Global.h"
30 #include "Model_Settings.h"
31 #include "MimeTypes.h"
33 //Qt
34 #include <QProcess>
35 #include <QDir>
36 #include <QUUid>
38 ///////////////////////////////////////////////////////////////////////////////
39 // Encoder Info
40 ///////////////////////////////////////////////////////////////////////////////
42 class OpusEncoderInfo : public AbstractEncoderInfo
44 virtual bool isModeSupported(int mode) const
46 switch(mode)
48 case SettingsModel::VBRMode:
49 case SettingsModel::ABRMode:
50 case SettingsModel::CBRMode:
51 return true;
52 break;
53 default:
54 MUTILS_THROW("Bad RC mode specified!");
58 virtual int valueCount(int mode) const
60 switch(mode)
62 case SettingsModel::VBRMode:
63 case SettingsModel::ABRMode:
64 case SettingsModel::CBRMode:
65 return 32;
66 break;
67 default:
68 MUTILS_THROW("Bad RC mode specified!");
72 virtual int valueAt(int mode, int index) const
74 switch(mode)
76 case SettingsModel::VBRMode:
77 case SettingsModel::ABRMode:
78 case SettingsModel::CBRMode:
79 return qBound(8, (index + 1) * 8, 256);
80 break;
81 default:
82 MUTILS_THROW("Bad RC mode specified!");
86 virtual int valueType(int mode) const
88 switch(mode)
90 case SettingsModel::VBRMode:
91 case SettingsModel::ABRMode:
92 return TYPE_APPROX_BITRATE;
93 break;
94 case SettingsModel::CBRMode:
95 return TYPE_BITRATE;
96 break;
97 default:
98 MUTILS_THROW("Bad RC mode specified!");
102 virtual const char *description(void) const
104 static const char* s_description = "Opus-Tools OpusEnc (libopus)";
105 return s_description;
108 virtual const char *extension(void) const
110 static const char* s_extension = "opus";
111 return s_extension;
114 virtual bool isResamplingSupported(void) const
116 return false;
119 static const g_opusEncoderInfo;
121 ///////////////////////////////////////////////////////////////////////////////
122 // Encoder implementation
123 ///////////////////////////////////////////////////////////////////////////////
125 OpusEncoder::OpusEncoder(void)
127 m_binary(lamexp_tools_lookup(L1S("opusenc.exe")))
129 if(m_binary.isEmpty())
131 MUTILS_THROW("Error initializing Opus encoder. Tool 'opusenc.exe' is not registred!");
134 m_configOptimizeFor = 0;
135 m_configEncodeComplexity = 10;
136 m_configFrameSize = 3;
139 OpusEncoder::~OpusEncoder(void)
143 bool OpusEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
145 QProcess process;
146 QStringList args;
148 switch(m_configRCMode)
150 case SettingsModel::VBRMode:
151 args << L1S("--vbr");
152 break;
153 case SettingsModel::ABRMode:
154 args << L1S("--cvbr");
155 break;
156 case SettingsModel::CBRMode:
157 args << L1S("--hard-cbr");
158 break;
159 default:
160 MUTILS_THROW("Bad rate-control mode!");
161 break;
164 args << "--comp" << QString::number(m_configEncodeComplexity);
166 switch(m_configFrameSize)
168 case 0:
169 args << L1S("--framesize") << L1S("2.5");
170 break;
171 case 1:
172 args << L1S("--framesize") << L1S("5");
173 break;
174 case 2:
175 args << L1S("--framesize") << L1S("10");
176 break;
177 case 3:
178 args << L1S("--framesize") << L1S("20");
179 break;
180 case 4:
181 args << L1S("--framesize") << L1S("40");
182 break;
183 case 5:
184 args << L1S("--framesize") << L1S("60");
185 break;
188 args << L1S("--bitrate") << QString::number(qBound(8, (m_configBitrate + 1) * 8, 256));
190 if(!metaInfo.title().isEmpty()) args << L1S("--title") << cleanTag(metaInfo.title());
191 if(!metaInfo.artist().isEmpty()) args << L1S("--artist") << cleanTag(metaInfo.artist());
192 if(!metaInfo.album().isEmpty()) args << L1S("--album") << cleanTag(metaInfo.album());
193 if(!metaInfo.genre().isEmpty()) args << L1S("--genre") << cleanTag(metaInfo.genre());
194 if(metaInfo.year()) args << L1S("--date") << QString::number(metaInfo.year());
195 if(metaInfo.position()) args << L1S("--comment") << QString("tracknumber=%1").arg(QString::number(metaInfo.position()));
196 if(!metaInfo.comment().isEmpty()) args << L1S("--comment") << QString("comment=%1").arg(cleanTag(metaInfo.comment()));
197 if(!metaInfo.cover().isEmpty()) args << L1S("--picture") << makeCoverParam(metaInfo.cover());
199 if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
201 args << QDir::toNativeSeparators(sourceFile);
202 args << QDir::toNativeSeparators(outputFile);
204 if(!startProcess(process, m_binary, args))
206 return false;
209 int prevProgress = -1;
210 QRegExp regExp(L1S("\\[.\\]\\s+(\\d+)%"));
212 const result_t result = awaitProcess(process, abortFlag, [this, &prevProgress, &regExp](const QString &text)
214 if (regExp.lastIndexIn(text) >= 0)
216 qint32 newProgress;
217 if (MUtils::regexp_parse_int32(regExp, newProgress))
219 if (newProgress > prevProgress)
221 emit statusUpdated(newProgress);
222 prevProgress = NEXT_PROGRESS(newProgress);
225 return true;
227 return false;
230 return (result == RESULT_SUCCESS);
233 QString OpusEncoder::detectMimeType(const QString &coverFile)
235 const QString suffix = QFileInfo(coverFile).suffix();
236 for (size_t i = 0; MIME_TYPES[i].type; i++)
238 for (size_t k = 0; MIME_TYPES[i].ext[k]; k++)
240 if (suffix.compare(QString::fromLatin1(MIME_TYPES[i].ext[k]), Qt::CaseInsensitive) == 0)
242 return QString::fromLatin1(MIME_TYPES[i].type);
247 qWarning("Unknown MIME type for extension '%s' -> using default!", MUTILS_UTF8(coverFile));
248 return QString::fromLatin1(MIME_TYPES[0].type);
251 QString OpusEncoder::makeCoverParam(const QString &coverFile)
253 return QString("3|%1|||%2").arg(detectMimeType(coverFile), QDir::toNativeSeparators(coverFile));
256 void OpusEncoder::setOptimizeFor(int optimizeFor)
258 m_configOptimizeFor = qBound(0, optimizeFor, 2);
261 void OpusEncoder::setEncodeComplexity(int complexity)
263 m_configEncodeComplexity = qBound(0, complexity, 10);
266 void OpusEncoder::setFrameSize(int frameSize)
268 m_configFrameSize = qBound(0, frameSize, 5);
271 bool OpusEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
273 if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
275 if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
277 return true;
281 return false;
284 const unsigned int *OpusEncoder::supportedChannelCount(void)
286 return NULL;
289 const unsigned int *OpusEncoder::supportedBitdepths(void)
291 static const unsigned int supportedBPS[] = {8, 16, 24, AudioFileModel::BITDEPTH_IEEE_FLOAT32, NULL};
292 return supportedBPS;
295 const bool OpusEncoder::needsTimingInfo(void)
297 return true;
300 const AbstractEncoderInfo *OpusEncoder::getEncoderInfo(void)
302 return &g_opusEncoderInfo;