Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 287 lines (251 sloc) 7.853 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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
// (at your option) any later version, but always including the *additional*
// restrictions defined in the "License.txt" file.
//
// 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"

#include "Global.h"
#include "Model_Settings.h"

#include <math.h>
#include <QProcess>
#include <QDir>
#include <QCoreApplication>

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};

///////////////////////////////////////////////////////////////////////////////
// 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:
THROW("Bad RC mode specified!");
}
}

virtual int valueCount(int mode) const
{
switch(mode)
{
case SettingsModel::VBRMode:
return 15;
break;
case SettingsModel::ABRMode:
case SettingsModel::CBRMode:
return 52;
break;
default:
THROW("Bad RC mode specified!");
}
}

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:
THROW("Bad RC mode specified!");
}
}

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:
THROW("Bad RC mode specified!");
}
}

virtual const char *description(void) const
{
static const char* s_description = "QAAC/QuickTime (\x0C2\x0A9 Apple Inc.)";
return s_description;
}
}
static const g_qaacEncoderInfo;

///////////////////////////////////////////////////////////////////////////////
// Encoder implementation
///////////////////////////////////////////////////////////////////////////////

QAACEncoder::QAACEncoder(void)
:
m_binary_qaac(lamexp_lookup_tool("qaac.exe")),
m_binary_soxr(lamexp_lookup_tool("libsoxr.dll")),
m_binary_soxc(lamexp_lookup_tool("libsoxconvolver.dll"))
{
if(m_binary_qaac.isEmpty() || m_binary_soxr.isEmpty() || m_binary_soxc.isEmpty())
{
THROW("Error initializing QAAC. Tool 'qaac.exe' is not registred!");
}

m_configProfile = 0;
}

QAACEncoder::~QAACEncoder(void)
{
}

bool QAACEncoder::encode(const QString &sourceFile, const AudioFileModel_MetaInfo &metaInfo, const unsigned int duration, const QString &outputFile, volatile bool *abortFlag)
{
QProcess process;
QStringList args;

process.setWorkingDirectory(QFileInfo(outputFile).canonicalPath());

QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH", QDir::toNativeSeparators(QString("%1;%1/QTfiles;%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), lamexp_temp_folder2())));
process.setProcessEnvironment(env);

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:
args << "--cbr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
break;
case SettingsModel::ABRMode:
args << "--abr" << QString::number(qBound(8, index2bitrate(m_configBitrate), 576));
break;
case SettingsModel::VBRMode:
args << "--tvbr" << QString::number(g_qaacVBRQualityLUT[qBound(0, m_configBitrate , 14)]);
break;
default:
THROW("Bad rate-control mode!");
break;
}

if(!m_configCustomParams.isEmpty()) args << m_configCustomParams.split(" ", QString::SkipEmptyParts);

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());
if(!metaInfo.comment().isEmpty()) args << "--comment" << cleanTag( metaInfo.comment());
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();

args << "-d" << ".";
args << "-o" << QDir::toNativeSeparators(outputFile);
args << QDir::toNativeSeparators(sourceFile);

if(!startProcess(process, m_binary_qaac, args))
{
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()));

if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
{
return false;
}

return true;
}

QString QAACEncoder::extension(void)
{
return "mp4";
}

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;
}

const AbstractEncoderInfo *QAACEncoder::getEncoderInfo(void)
{
return &g_qaacEncoderInfo;
}
Something went wrong with that request. Please try again.