2011-11-21 01:22:41 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2016-01-03 15:54:36 +01:00
|
|
|
// Copyright (C) 2004-2016 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)
|
|
|
|
:
|
2015-05-12 00:29:17 +02:00
|
|
|
m_binary_qaac32(lamexp_tools_lookup("qaac.exe")),
|
|
|
|
m_binary_qaac64(lamexp_tools_lookup("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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QAACEncoder::~QAACEncoder(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *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();
|
2014-11-25 02:14:42 +01:00
|
|
|
env.insert("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:
|
|
|
|
args << "--he"; //Forces use of HE AAC profile (there is no explicit HEv2 switch for QAAC)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(m_configRCMode)
|
|
|
|
{
|
|
|
|
case SettingsModel::CBRMode:
|
2013-10-07 02:28:01 +02:00
|
|
|
args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
case SettingsModel::ABRMode:
|
2015-10-18 14:59:17 +02:00
|
|
|
args << "--cvbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
|
2011-11-21 01:22:41 +01:00
|
|
|
break;
|
|
|
|
case SettingsModel::VBRMode:
|
2013-10-07 02:28:01 +02:00
|
|
|
args << "--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-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-01-30 17:51:10 +01:00
|
|
|
args << "--rate" << QString::number(m_configSamplingRate);
|
|
|
|
}
|
|
|
|
|
2011-11-21 01:22:41 +01:00
|
|
|
if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);
|
|
|
|
|
2014-12-25 00:47:34 +01:00
|
|
|
if(!metaInfo.title().isEmpty()) args << "--title" << cleanTag(metaInfo.title());
|
|
|
|
if(!metaInfo.artist().isEmpty()) args << "--artist" << cleanTag(metaInfo.artist());
|
|
|
|
if(!metaInfo.album().isEmpty()) args << "--album" << cleanTag(metaInfo.album());
|
|
|
|
if(!metaInfo.genre().isEmpty()) args << "--genre" << cleanTag(metaInfo.genre());
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
|
2014-12-25 00:47:34 +01:00
|
|
|
if(metaInfo.year()) args << "--date" << QString::number(metaInfo.year());
|
|
|
|
if(metaInfo.position()) args << "--track" << QString::number(metaInfo.position());
|
|
|
|
if(!metaInfo.cover().isEmpty()) args << "--artwork" << metaInfo.cover();
|
2011-11-21 01:22:41 +01:00
|
|
|
|
2011-11-21 14:40:32 +01:00
|
|
|
args << "-d" << ".";
|
2011-11-21 01:22:41 +01:00
|
|
|
args << "-o" << QDir::toNativeSeparators(outputFile);
|
|
|
|
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;
|
|
|
|
|
|
|
|
QRegExp regExp("\\[(\\d+)\\.(\\d)%\\]");
|
|
|
|
|
|
|
|
while(process.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
if(*abortFlag)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
bAborted = true;
|
|
|
|
emit messageLogged("\nABORTED BY USER !!!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
process.waitForReadyRead(m_processTimeoutInterval);
|
|
|
|
if(!process.bytesAvailable() && process.state() == QProcess::Running)
|
|
|
|
{
|
|
|
|
process.kill();
|
|
|
|
qWarning("QAAC process timed out <-- killing!");
|
|
|
|
emit messageLogged("\nPROCESS TIMEOUT !!!");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(containerType.compare("Wave", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
if(formatType.compare("PCM", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QAACEncoder::setProfile(int profile)
|
|
|
|
{
|
|
|
|
m_configProfile = profile;
|
|
|
|
}
|
2013-10-07 02:28:01 +02:00
|
|
|
|
|
|
|
const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
|
|
|
|
{
|
|
|
|
return &g_qaacEncoderInfo;
|
|
|
|
}
|