1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
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 "Thread_Process.h"
27 #include "Model_AudioFile.h"
28 #include "Model_Progress.h"
29 #include "Encoder_Abstract.h"
30 #include "Decoder_Abstract.h"
31 #include "Filter_Abstract.h"
32 #include "Filter_Downmix.h"
33 #include "Filter_Resample.h"
34 #include "Tool_WaveProperties.h"
35 #include "Registry_Decoder.h"
36 #include "Model_Settings.h"
39 #include <MUtils/Global.h>
40 #include <MUtils/OSSupport.h>
41 #include <MUtils/Version.h>
48 #include <QMutexLocker>
50 #include <QThreadPool>
57 #define DIFF(X,Y) ((X > Y) ? (X-Y) : (Y-X))
58 #define IS_WAVE(X) ((X.containerType().compare("Wave", Qt::CaseInsensitive) == 0) && (X.audioType().compare("PCM", Qt::CaseInsensitive) == 0))
59 #define STRDEF(STR,DEF) ((!STR.isEmpty()) ? STR : DEF)
61 ////////////////////////////////////////////////////////////
63 ////////////////////////////////////////////////////////////
65 ProcessThread::ProcessThread(const AudioFileModel &audioFile, const QString &outputDirectory, const QString &tempDirectory, AbstractEncoder *encoder, const bool prependRelativeSourcePath)
67 m_audioFile(audioFile),
68 m_outputDirectory(outputDirectory),
69 m_tempDirectory(tempDirectory),
71 m_jobId(QUuid::createUuid()),
72 m_prependRelativeSourcePath(prependRelativeSourcePath),
73 m_renamePattern("<BaseName>"),
74 m_overwriteMode(OverwriteMode_KeepBoth),
77 m_propDetect(new WaveProperties())
79 connect(m_encoder, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
80 connect(m_encoder, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
82 connect(m_propDetect, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
83 connect(m_propDetect, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
85 m_currentStep = UnknownStep;
88 ProcessThread::~ProcessThread(void)
90 while(!m_tempFiles.isEmpty())
92 MUtils::remove_file(m_tempFiles.takeFirst());
95 while(!m_filters.isEmpty())
97 delete m_filters.takeFirst();
100 MUTILS_DELETE(m_encoder);
101 MUTILS_DELETE(m_propDetect);
103 emit processFinished();
106 ////////////////////////////////////////////////////////////
108 ////////////////////////////////////////////////////////////
110 bool ProcessThread::init(void)
112 if(m_initialized < 0)
116 //Initialize job status
117 qDebug("Process thread %s has started.", m_jobId.toString().toLatin1().constData());
118 emit processStateInitialized(m_jobId, QFileInfo(m_audioFile.filePath()).fileName(), tr("Starting..."), ProgressModel::JobRunning);
121 handleMessage(QString().sprintf("LameXP v%u.%02u (Build #%u), compiled on %s at %s", lamexp_version_major(), lamexp_version_minor(), lamexp_version_build(), MUTILS_UTF8(MUtils::Version::app_build_date().toString(Qt::ISODate)), MUTILS_UTF8(MUtils::Version::app_build_time().toString(Qt::ISODate))));
122 handleMessage("\n-------------------------------\n");
127 qWarning("[ProcessThread::init] Job %s already initialialized, skipping!", m_jobId.toString().toLatin1().constData());
131 bool ProcessThread::start(QThreadPool *pool)
133 //Make sure object was initialized correctly
134 if(m_initialized < 0)
136 MUTILS_THROW("Object not initialized yet!");
139 if(m_initialized < 1)
143 m_outFileName.clear();
144 bool bSuccess = false;
146 //Generate output file name
147 switch(generateOutFileName(m_outFileName))
150 //File name generated successfully :-)
155 //File name already exists -> skipping!
156 emit processStateChanged(m_jobId, tr("Skipped."), ProgressModel::JobSkipped);
157 emit processStateFinished(m_jobId, m_outFileName, -1);
160 //File name could not be generated
161 emit processStateChanged(m_jobId, tr("Not found!"), ProgressModel::JobFailed);
162 emit processStateFinished(m_jobId, m_outFileName, 0);
168 emit processFinished();
174 qWarning("[ProcessThread::start] Job %s already started, skipping!", m_jobId.toString().toLatin1().constData());
178 ////////////////////////////////////////////////////////////
179 // Thread Entry Point
180 ////////////////////////////////////////////////////////////
182 void ProcessThread::run()
188 catch(const std::exception &error)
190 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error.what());
191 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
195 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
196 MUtils::OS::fatal_exit(L"Unhandeled C++ exception error, application will exit!");
200 void ProcessThread::processFile()
203 bool bSuccess = true;
205 //Make sure object was initialized correctly
206 if(m_initialized < 1)
208 MUTILS_THROW("Object not initialized yet!");
211 QString sourceFile = m_audioFile.filePath();
216 const AudioFileModel_TechInfo &formatInfo = m_audioFile.techInfo();
217 if(!m_filters.isEmpty() || !m_encoder->isFormatSupported(formatInfo.containerType(), formatInfo.containerProfile(), formatInfo.audioType(), formatInfo.audioProfile(), formatInfo.audioVersion()))
219 m_currentStep = DecodingStep;
220 AbstractDecoder *decoder = DecoderRegistry::lookup(formatInfo.containerType(), formatInfo.containerProfile(), formatInfo.audioType(), formatInfo.audioProfile(), formatInfo.audioVersion());
224 QString tempFile = generateTempFileName();
226 connect(decoder, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
227 connect(decoder, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
229 bSuccess = decoder->decode(sourceFile, tempFile, &m_aborted);
230 MUTILS_DELETE(decoder);
234 sourceFile = tempFile;
235 m_audioFile.techInfo().setContainerType(QString::fromLatin1("Wave"));
236 m_audioFile.techInfo().setAudioType(QString::fromLatin1("PCM"));
238 if(QFileInfo(sourceFile).size() >= 4294967296i64)
240 handleMessage(tr("WARNING: Decoded file size exceeds 4 GB, problems might occur!\n"));
243 handleMessage("\n-------------------------------\n");
248 if(QFileInfo(m_outFileName).exists() && (QFileInfo(m_outFileName).size() < 512)) QFile::remove(m_outFileName);
249 handleMessage(QString("%1\n%2\n\n%3\t%4\n%5\t%6").arg(tr("The format of this file is NOT supported:"), m_audioFile.filePath(), tr("Container Format:"), m_audioFile.containerInfo(), tr("Audio Format:"), m_audioFile.audioCompressInfo()));
250 emit processStateChanged(m_jobId, tr("Unsupported!"), ProgressModel::JobFailed);
251 emit processStateFinished(m_jobId, m_outFileName, 0);
256 //------------------------------------
257 //Update audio properties after decode
258 //------------------------------------
259 if(bSuccess && !m_aborted && IS_WAVE(m_audioFile.techInfo()))
261 if(m_encoder->supportedSamplerates() || m_encoder->supportedBitdepths() || m_encoder->supportedChannelCount() || m_encoder->needsTimingInfo() || !m_filters.isEmpty())
263 m_currentStep = AnalyzeStep;
264 bSuccess = m_propDetect->detect(sourceFile, &m_audioFile.techInfo(), &m_aborted);
268 handleMessage("\n-------------------------------\n");
270 //Do we need to take care if Stereo downmix?
271 if(m_encoder->supportedChannelCount())
273 insertDownmixFilter();
276 //Do we need to take care of downsampling the input?
277 if(m_encoder->supportedSamplerates() || m_encoder->supportedBitdepths())
279 insertDownsampleFilter();
285 //-----------------------
286 //Apply all audio filters
287 //-----------------------
290 while(!m_filters.isEmpty() && !m_aborted)
292 QString tempFile = generateTempFileName();
293 AbstractFilter *poFilter = m_filters.takeFirst();
294 m_currentStep = FilteringStep;
296 connect(poFilter, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
297 connect(poFilter, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
299 if(poFilter->apply(sourceFile, tempFile, &m_audioFile.techInfo(), &m_aborted))
301 sourceFile = tempFile;
304 handleMessage("\n-------------------------------\n");
312 if(bSuccess && !m_aborted)
314 m_currentStep = EncodingStep;
315 bSuccess = m_encoder->encode(sourceFile, m_audioFile.metaInfo(), m_audioFile.techInfo().duration(), m_outFileName, &m_aborted);
319 if((!bSuccess) || m_aborted)
321 QFileInfo fileInfo(m_outFileName);
322 if(fileInfo.exists() && (fileInfo.size() < 512))
324 QFile::remove(m_outFileName);
328 //Make sure output file exists
329 if(bSuccess && (!m_aborted))
331 QFileInfo fileInfo(m_outFileName);
332 bSuccess = fileInfo.exists() && fileInfo.isFile() && (fileInfo.size() > 0);
335 MUtils::OS::sleep_ms(125);
338 emit processStateChanged(m_jobId, (m_aborted ? tr("Aborted!") : (bSuccess ? tr("Done.") : tr("Failed!"))), ((bSuccess && !m_aborted) ? ProgressModel::JobComplete : ProgressModel::JobFailed));
339 emit processStateFinished(m_jobId, m_outFileName, (bSuccess ? 1 : 0));
341 qDebug("Process thread is done.");
344 ////////////////////////////////////////////////////////////
346 ////////////////////////////////////////////////////////////
348 void ProcessThread::handleUpdate(int progress)
350 //qDebug("Progress: %d\n", progress);
352 switch(m_currentStep)
355 emit processStateChanged(m_jobId, QString("%1 (%2%)").arg(tr("Encoding"), QString::number(progress)), ProgressModel::JobRunning);
358 emit processStateChanged(m_jobId, QString("%1 (%2%)").arg(tr("Analyzing"), QString::number(progress)), ProgressModel::JobRunning);
361 emit processStateChanged(m_jobId, QString("%1 (%2%)").arg(tr("Filtering"), QString::number(progress)), ProgressModel::JobRunning);
364 emit processStateChanged(m_jobId, QString("%1 (%2%)").arg(tr("Decoding"), QString::number(progress)), ProgressModel::JobRunning);
369 void ProcessThread::handleMessage(const QString &line)
371 emit processMessageLogged(m_jobId, line);
374 ////////////////////////////////////////////////////////////
376 ////////////////////////////////////////////////////////////
378 int ProcessThread::generateOutFileName(QString &outFileName)
382 //Make sure the source file exists
383 QFileInfo sourceFile(m_audioFile.filePath());
384 if(!(sourceFile.exists() && sourceFile.isFile()))
386 handleMessage(QString("%1\n%2").arg(tr("The source audio file could not be found:"), sourceFile.absoluteFilePath()));
390 //Make sure the source file readable
391 QFile readTest(sourceFile.canonicalFilePath());
392 if(!readTest.open(QIODevice::ReadOnly))
394 handleMessage(QString("%1\n%2").arg(tr("The source audio file could not be opened for reading:"), QDir::toNativeSeparators(readTest.fileName())));
402 QString baseName = sourceFile.completeBaseName();
403 QDir targetDir(m_outputDirectory.isEmpty() ? sourceFile.canonicalPath() : m_outputDirectory);
405 //Prepend relative source file path?
406 if(m_prependRelativeSourcePath && !m_outputDirectory.isEmpty())
408 QDir rootDir = sourceFile.dir();
409 while(!rootDir.isRoot())
411 if(!rootDir.cdUp()) break;
413 targetDir.setPath(QString("%1/%2").arg(targetDir.absolutePath(), QFileInfo(rootDir.relativeFilePath(sourceFile.canonicalFilePath())).path()));
416 //Make sure output directory does exist
417 if(!targetDir.exists())
419 targetDir.mkpath(".");
420 if(!targetDir.exists())
422 handleMessage(QString("%1\n%2").arg(tr("The target output directory doesn't exist and could NOT be created:"), QDir::toNativeSeparators(targetDir.absolutePath())));
427 //Make sure that the output dir is writable
428 QFile writeTest(QString("%1/.%2").arg(targetDir.canonicalPath(), MUtils::rand_str()));
429 if(!writeTest.open(QIODevice::ReadWrite))
431 handleMessage(QString("%1\n%2").arg(tr("The target output directory is NOT writable:"), QDir::toNativeSeparators(targetDir.absolutePath())));
439 //Apply rename pattern
440 QString fileName = applyRenamePattern(baseName, m_audioFile.metaInfo());
442 //Generate full output path
443 outFileName = QString("%1/%2.%3").arg(targetDir.canonicalPath(), fileName, m_encoder->extension());
445 //Skip file, if target file exists (optional!)
446 if((m_overwriteMode == OverwriteMode_SkipExisting) && QFileInfo(outFileName).exists())
448 handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to skip this file:"), QDir::toNativeSeparators(outFileName)));
449 handleMessage(tr("If you don't want existing files to be skipped, please change the overwrite mode!"));
453 //Delete file, if target file exists (optional!)
454 if((m_overwriteMode == OverwriteMode_Overwrite) && QFileInfo(outFileName).exists() && QFileInfo(outFileName).isFile())
456 handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to delete existing file:"), QDir::toNativeSeparators(outFileName)));
457 if(sourceFile.canonicalFilePath().compare(QFileInfo(outFileName).absoluteFilePath(), Qt::CaseInsensitive) != 0)
459 for(int i = 0; i < 16; i++)
461 if(QFile::remove(outFileName))
465 MUtils::OS::sleep_ms(125);
468 if(QFileInfo(outFileName).exists())
470 handleMessage(QString("%1\n").arg(tr("Failed to delete existing target file, will save to another file name!")));
476 //Generate final name
477 while(QFileInfo(outFileName).exists() && (n < (INT_MAX/2)))
479 outFileName = QString("%1/%2 (%3).%4").arg(targetDir.canonicalPath(), fileName, QString::number(++n), m_encoder->extension());
483 QFile placeholder(outFileName);
484 if(placeholder.open(QIODevice::WriteOnly))
492 QString ProcessThread::applyRenamePattern(const QString &baseName, const AudioFileModel_MetaInfo &metaInfo)
494 QString fileName = m_renamePattern;
496 fileName.replace("<BaseName>", STRDEF(baseName, tr("Unknown File Name")), Qt::CaseInsensitive);
497 fileName.replace("<TrackNo>", QString().sprintf("%02d", metaInfo.position()), Qt::CaseInsensitive);
498 fileName.replace("<Title>", STRDEF(metaInfo.title(), tr("Unknown Title")) , Qt::CaseInsensitive);
499 fileName.replace("<Artist>", STRDEF(metaInfo.artist(), tr("Unknown Artist")), Qt::CaseInsensitive);
500 fileName.replace("<Album>", STRDEF(metaInfo.album(), tr("Unknown Album")), Qt::CaseInsensitive);
501 fileName.replace("<Year>", QString().sprintf("%04d", metaInfo.year()), Qt::CaseInsensitive);
502 fileName.replace("<Comment>", STRDEF(metaInfo.comment(), tr("Unknown Comment")), Qt::CaseInsensitive);
503 fileName = lamexp_clean_filename(fileName).simplified();
508 QString ProcessThread::generateTempFileName(void)
511 QString tempFileName;
513 for(int i = 0; i < 4096; i++)
515 tempFileName = QString("%1/%2.wav").arg(m_tempDirectory, MUtils::rand_str());
516 if(m_tempFiles.contains(tempFileName, Qt::CaseInsensitive) || QFileInfo(tempFileName).exists())
521 QFile file(tempFileName);
522 if(file.open(QFile::ReadWrite))
532 qWarning("Failed to generate unique temp file name!");
533 return QString("%1/~whoops.wav").arg(m_tempDirectory);
536 m_tempFiles << tempFileName;
540 void ProcessThread::insertDownsampleFilter(void)
542 int targetSampleRate = 0;
543 int targetBitDepth = 0;
545 /* Adjust sample rate */
546 if(m_encoder->supportedSamplerates() && m_audioFile.techInfo().audioSamplerate())
548 bool applyDownsampling = true;
550 //Check if downsampling filter is already in the chain
551 for(int i = 0; i < m_filters.count(); i++)
553 if(dynamic_cast<ResampleFilter*>(m_filters.at(i)))
555 qWarning("Encoder requires downsampling, but user has already set resamling filter!");
556 handleMessage("WARNING: Encoder may need resampling, but already using resample filter. Encoding *may* fail!\n");
557 applyDownsampling = false;
561 //Now determine the target sample rate, if required
562 if(applyDownsampling)
564 const unsigned int *supportedRates = m_encoder->supportedSamplerates();
565 const unsigned int inputRate = m_audioFile.techInfo().audioSamplerate();
566 unsigned int currentDiff = UINT_MAX, minimumDiff = UINT_MAX, bestRate = UINT_MAX;
568 //Find the most suitable supported sampling rate
569 for(int i = 0; supportedRates[i]; i++)
571 currentDiff = DIFF(inputRate, supportedRates[i]);
572 if((currentDiff < minimumDiff) || ((currentDiff == minimumDiff) && (bestRate < supportedRates[i])))
574 bestRate = supportedRates[i];
575 minimumDiff = currentDiff;
576 if(!(minimumDiff > 0)) break;
580 if(bestRate != inputRate)
582 targetSampleRate = (bestRate != UINT_MAX) ? bestRate : supportedRates[0];
587 /* Adjust bit depth (word size) */
588 if(m_encoder->supportedBitdepths() && m_audioFile.techInfo().audioBitdepth())
590 const unsigned int inputBPS = m_audioFile.techInfo().audioBitdepth();
591 const unsigned int *supportedBPS = m_encoder->supportedBitdepths();
593 bool bAdjustBitdepth = true;
595 //Is the input bit depth supported exactly? (including IEEE Float)
596 for(int i = 0; supportedBPS[i]; i++)
598 if(supportedBPS[i] == inputBPS) bAdjustBitdepth = false;
603 unsigned int currentDiff = UINT_MAX, minimumDiff = UINT_MAX, bestBPS = UINT_MAX;
604 const unsigned int originalBPS = (inputBPS == AudioFileModel::BITDEPTH_IEEE_FLOAT32) ? 32 : inputBPS;
606 //Find the most suitable supported bit depth
607 for(int i = 0; supportedBPS[i]; i++)
609 if(supportedBPS[i] == AudioFileModel::BITDEPTH_IEEE_FLOAT32) continue;
611 currentDiff = DIFF(originalBPS, supportedBPS[i]);
612 if((currentDiff < minimumDiff) || ((currentDiff == minimumDiff) && (bestBPS < supportedBPS[i])))
614 bestBPS = supportedBPS[i];
615 minimumDiff = currentDiff;
616 if(!(minimumDiff > 0)) break;
620 if(bestBPS != originalBPS)
622 targetBitDepth = (bestBPS != UINT_MAX) ? bestBPS : supportedBPS[0];
627 /* Insert the filter */
628 if(targetSampleRate || targetBitDepth)
630 m_filters.append(new ResampleFilter(targetSampleRate, targetBitDepth));
634 void ProcessThread::insertDownmixFilter(void)
636 bool applyDownmixing = true;
638 //Check if downmixing filter is already in the chain
639 for(int i = 0; i < m_filters.count(); i++)
641 if(dynamic_cast<DownmixFilter*>(m_filters.at(i)))
643 qWarning("Encoder requires Stereo downmix, but user has already forced downmix!");
644 handleMessage("WARNING: Encoder may need downmixning, but already using downmixning filter. Encoding *may* fail!\n");
645 applyDownmixing = false;
649 //Now add the downmixing filter, if needed
652 bool requiresDownmix = true;
653 const unsigned int *supportedChannels = m_encoder->supportedChannelCount();
654 unsigned int channels = m_audioFile.techInfo().audioChannels();
656 for(int i = 0; supportedChannels[i]; i++)
658 if(supportedChannels[i] == channels)
660 requiresDownmix = false;
667 m_filters.append(new DownmixFilter());
672 ////////////////////////////////////////////////////////////
674 ////////////////////////////////////////////////////////////
676 void ProcessThread::addFilter(AbstractFilter *filter)
678 m_filters.append(filter);
681 void ProcessThread::setRenamePattern(const QString &pattern)
683 QString newPattern = pattern.simplified();
684 if(!newPattern.isEmpty()) m_renamePattern = newPattern;
687 void ProcessThread::setOverwriteMode(const bool &bSkipExistingFile, const bool &bReplacesExisting)
689 if(bSkipExistingFile && bReplacesExisting)
691 qWarning("Inconsistent overwrite flags -> reverting to default!");
692 m_overwriteMode = OverwriteMode_KeepBoth;
696 m_overwriteMode = OverwriteMode_KeepBoth;
697 if(bSkipExistingFile) m_overwriteMode = OverwriteMode_SkipExisting;
698 if(bReplacesExisting) m_overwriteMode = OverwriteMode_Overwrite;
702 ////////////////////////////////////////////////////////////
704 ////////////////////////////////////////////////////////////