2011-11-21 01:22:41 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2017-03-12 12:12:49 +01:00
|
|
|
// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
|
2011-11-21 01:22:41 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2011-11-21 01:22:41 +01:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Encoder_AAC_QAAC.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
2011-11-21 01:22:41 +01:00
|
|
|
#include "Global.h"
|
|
|
|
#include "Model_Settings.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
|
|
|
|
|
|
|
//StdLib
|
2011-11-21 01:22:41 +01:00
|
|
|
#include <math.h>
|
2014-11-25 02:14:42 +01:00
|
|
|
|
|
|
|
//Qt
|
2011-11-21 01:22:41 +01:00
|
|
|
#include <QProcess>
|
|
|
|
#include <QDir>
|
2011-11-21 22:28:14 +01:00
|
|
|
#include <QCoreApplication>
|
2011-11-21 01:22:41 +01:00
|
|
|
|
2013-10-07 02:28:01 +02:00
|
|
|
static int index2bitrate(const int index)
|
|
|
|
{
|
|
|
|
return (index < 32) ? ((index + 1) * 8) : ((index - 15) * 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int g_qaacVBRQualityLUT[16] = {0 ,9, 18, 27, 36, 45, 54, 63, 73, 82, 91, 100, 109, 118, 127, INT_MAX};
|
|
|
|
|
2016-01-30 19:00:37 +01:00
|
|
|
static const int RESAMPLING_QUALITY = 127;
|
|
|
|
|
2013-10-07 02:28:01 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Encoder Info
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class QAACEncoderInfo : public AbstractEncoderInfo
|
|
|
|
{
|
|
|
|
virtual bool isModeSupported(int mode) const
|
|
|
|
{
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case SettingsModel::VBRMode:
|
|
|
|
case SettingsModel::CBRMode:
|
|
|
|
case SettingsModel::ABRMode:
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Bad RC mode specified!");
|
2013-10-07 02:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int valueCount(int mode) const
|
|
|
|
{
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case SettingsModel::VBRMode:
|
|
|
|
return 15;
|
|
|
|
break;
|
|
|
|
case SettingsModel::ABRMode:
|
|
|
|
case SettingsModel::CBRMode:
|
|
|
|
return 52;
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Bad RC mode specified!");
|
2013-10-07 02:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int valueAt(int mode, int index) const
|
|
|
|
{
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case SettingsModel::VBRMode:
|
|
|
|
return g_qaacVBRQualityLUT[qBound(0, index , 14)];
|
|
|
|
break;
|
|
|
|
case SettingsModel::ABRMode:
|
|
|
|
case SettingsModel::CBRMode:
|
|
|
|
return qBound(8, index2bitrate(index), 576);
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Bad RC mode specified!");
|
2013-10-07 02:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int valueType(int mode) const
|
|
|
|
{
|
|
|
|
switch(mode)
|
|
|
|
{
|
|
|
|
case SettingsModel::VBRMode:
|
|
|
|
return TYPE_QUALITY_LEVEL_INT;
|
|
|
|
break;
|
|
|
|
case SettingsModel::ABRMode:
|
|
|
|
return TYPE_APPROX_BITRATE;
|
|
|
|
break;
|
|
|
|
case SettingsModel::CBRMode:
|
|
|
|
return TYPE_BITRATE;
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Bad RC mode specified!");
|
2013-10-07 02:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *description(void) const
|
|
|
|
{
|
2014-01-27 15:35:35 +01:00
|
|
|
static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Apple Inc.)";
|
2013-10-07 02:28:01 +02:00
|
|
|
return s_description;
|
|
|
|
}
|
2015-05-10 16:34:07 +02:00
|
|
|
|
|
|
|
virtual const char *extension(void) const
|
|
|
|
{
|
|
|
|
static const char* s_extension = "mp4";
|
|
|
|
return s_extension;
|
|
|
|
}
|
2016-01-30 17:51:10 +01:00
|
|
|
|
|
|
|
virtual bool isResamplingSupported(void) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2013-10-07 02:28:01 +02:00
|
|
|
}
|
|
|
|
static const g_qaacEncoderInfo;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Encoder implementation
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-11-21 01:22:41 +01:00
|
|
|
QAACEncoder::QAACEncoder(void)
|
|
|
|
:
|
2016-04-16 13:26:21 +02:00
|
|
|
m_binary_qaac32(lamexp_tools_lookup(L1S("qaac.exe"))),
|
|
|
|
m_binary_qaac64(lamexp_tools_lookup(L1S("qaac64.exe")))
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
2015-05-12 00:29:17 +02:00
|
|
|
if(m_binary_qaac32.isEmpty() && m_binary_qaac64.isEmpty())
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Error initializing QAAC. Tool 'qaac.exe' is not registred!");
|
2011-11-21 01:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
m_configProfile = 0;
|
2016-02-01 20:20:14 +01:00
|
|
|
m_algorithmQuality = 2;
|
2011-11-21 01:22:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QAACEncoder::~QAACEncoder(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-18 21:05:28 +02:00
|
|
|
bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const unsigned int channels, const QString &outputFile, QAtomicInt &abortFlag)
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
2015-05-12 00:29:17 +02:00
|
|
|
const QString qaac_bin = m_binary_qaac64.isEmpty() ? m_binary_qaac32 : m_binary_qaac64;
|
|
|
|
|
2011-11-21 01:22:41 +01:00
|
|
|
QProcess process;
|
|
|
|
QStringList args;
|
|
|
|
|
2011-11-21 22:28:14 +01:00
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
2016-04-16 13:26:21 +02:00
|
|
|
env.insert(L1S("PATH"), QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), MUtils::temp_folder())));
|
2011-11-21 22:28:14 +01:00
|
|
|
process.setProcessEnvironment(env);
|
|
|
|
|
2011-11-21 01:22:41 +01:00
|
|
|
if(m_configRCMode != SettingsModel::VBRMode)
|
|
|
|
{
|
|
|
|
switch(m_configProfile)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
case 3:
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--he"); //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(m_configRCMode)
|
|
|
|
{
|
|
|
|
case SettingsModel::CBRMode:
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--cbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
case SettingsModel::ABRMode:
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--cvbr") << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
case SettingsModel::VBRMode:
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--tvbr") << QString::number(g_qaacVBRQualityLUT[qBound(0, m_configBitrate , 14)]);
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-30 21:32:23 +01:00
|
|
|
MUTILS_THROW("Bad rate-control mode!");
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--quality") << QString::number(qBound(0, m_algorithmQuality, 2));
|
2016-01-30 17:51:10 +01:00
|
|
|
if (m_configSamplingRate > 0)
|
|
|
|
{
|
2016-01-30 19:00:37 +01:00
|
|
|
args << QString("--native-resampler=bats,%0").arg(QString::number(RESAMPLING_QUALITY));
|
2016-04-16 13:26:21 +02:00
|
|
|
args << L1S("--rate") << QString::number(m_configSamplingRate);
|
2016-01-30 17:51:10 +01:00
|
|
|
}
|
|
|
|
|
2011-11-21 01:22:41 +01:00
|
|
|
if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
|
|
|
|
|
2016-04-16 13:26:21 +02:00
|
|
|
if(!metaInfo.title().isEmpty()) args << L1S("--title") << cleanTag(metaInfo.title());
|
|
|
|
if(!metaInfo.artist().isEmpty()) args << L1S("--artist") << cleanTag(metaInfo.artist());
|
|
|
|
if(!metaInfo.album().isEmpty()) args << L1S("--album") << cleanTag(metaInfo.album());
|
|
|
|
if(!metaInfo.genre().isEmpty()) args << L1S("--genre") << cleanTag(metaInfo.genre());
|
|
|
|
if(!metaInfo.comment().isEmpty()) args << L1S("--comment") << cleanTag( metaInfo.comment());
|
|
|
|
if(metaInfo.year()) args << L1S("--date") << QString::number(metaInfo.year());
|
|
|
|
if(metaInfo.position()) args << L1S("--track") << QString::number(metaInfo.position());
|
|
|
|
if(!metaInfo.cover().isEmpty()) args << L1S("--artwork") << metaInfo.cover();
|
|
|
|
|
|
|
|
args << L1S("-d") << L1S(".");
|
|
|
|
args << L1S("-o") << QDir::toNativeSeparators(outputFile);
|
2011-11-21 01:22:41 +01:00
|
|
|
args << QDir::toNativeSeparators(sourceFile);
|
|
|
|
|
2016-01-03 15:54:36 +01:00
|
|
|
if(!startProcess(process, qaac_bin, args, QFileInfo(outputFile).canonicalPath()))
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bTimeout = false;
|
|
|
|
bool bAborted = false;
|
|
|
|
int prevProgress = -1;
|
|
|
|
|
2016-04-16 13:26:21 +02:00
|
|
|
QRegExp regExp(L1S("\\[(\\d+)\\.(\\d)%\\]"));
|
2011-11-21 01:22:41 +01:00
|
|
|
|
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
2017-04-18 21:05:28 +02:00
|
|
|
if (checkFlag(abortFlag))
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
bAborted = true;
|
2016-04-16 13:26:21 +02:00
|
|
|
emit messageLogged(L1S("\nABORTED BY USER !!!"));
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
process.waitForReadyRead(m_processTimeoutInterval);
|
|
|
|
if(!process.bytesAvailable() && process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("QAAC process timed out <-- killing!");
|
2016-04-16 13:26:21 +02:00
|
|
|
emit messageLogged(L1S("\nPROCESS TIMEOUT !!!"));
|
2011-11-21 01:22:41 +01:00
|
|
|
bTimeout = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while(process.bytesAvailable() > 0)
|
|
|
|
{
|
|
|
|
QByteArray line = process.readLine();
|
|
|
|
QString text = QString::fromUtf8(line.constData()).simplified();
|
|
|
|
if(regExp.lastIndexIn(text) >= 0)
|
|
|
|
{
|
|
|
|
bool ok = false;
|
|
|
|
int progress = regExp.cap(1).toInt(&ok);
|
|
|
|
if(ok && (progress > prevProgress))
|
|
|
|
{
|
|
|
|
emit statusUpdated(progress);
|
|
|
|
prevProgress = qMin(progress + 2, 99);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!text.isEmpty())
|
|
|
|
{
|
|
|
|
emit messageLogged(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
process.waitForFinished();
|
|
|
|
if(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
process.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit statusUpdated(100);
|
|
|
|
emit messageLogged(QString().sprintf("\nExited with code: 0x%04X", process.exitCode()));
|
|
|
|
|
2011-12-22 00:06:34 +01:00
|
|
|
if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QAACEncoder::isFormatSupported(const QString &containerType, const QString &containerProfile, const QString &formatType, const QString &formatProfile, const QString &formatVersion)
|
|
|
|
{
|
2016-04-16 13:26:21 +02:00
|
|
|
if(containerType.compare(L1S("Wave"), Qt::CaseInsensitive) == 0)
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
2016-04-16 13:26:21 +02:00
|
|
|
if(formatType.compare(L1S("PCM"), Qt::CaseInsensitive) == 0)
|
2011-11-21 01:22:41 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QAACEncoder::setProfile(int profile)
|
|
|
|
{
|
|
|
|
m_configProfile = profile;
|
|
|
|
}
|
2013-10-07 02:28:01 +02:00
|
|
|
|
2016-02-01 20:20:14 +01:00
|
|
|
void QAACEncoder::setAlgoQuality(int value)
|
|
|
|
{
|
|
|
|
m_algorithmQuality = qBound(0, value, 2);
|
|
|
|
}
|
|
|
|
|
2013-10-07 02:28:01 +02:00
|
|
|
const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
|
|
|
|
{
|
|
|
|
return &g_qaacEncoderInfo;
|
|
|
|
}
|