1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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_FileAnalyzer_Task.h"
27 #include "LockedFile.h"
28 #include "Model_AudioFile.h"
29 #include "MimeTypes.h"
32 #include <MUtils/Global.h>
33 #include <MUtils/OSSupport.h>
34 #include <MUtils/Exception.h>
44 #include <QReadLocker>
45 #include <QWriteLocker>
47 #include <QXmlSimpleReader>
48 #include <QXmlInputSource>
56 #define IS_KEY(KEY) (key.compare(KEY, Qt::CaseInsensitive) == 0)
57 #define IS_SEC(SEC) (key.startsWith((SEC "_"), Qt::CaseInsensitive))
58 #define FIRST_TOK(STR) (STR.split(" ", QString::SkipEmptyParts).first())
60 #define STR_EQ(A,B) ((A).compare((B), Qt::CaseInsensitive) == 0)
62 ////////////////////////////////////////////////////////////
63 // XML Content Handler
64 ////////////////////////////////////////////////////////////
66 class AnalyzeTask_XmlHandler
: public QXmlDefaultHandler
69 AnalyzeTask_XmlHandler(AudioFileModel
&audioFile
) :
70 m_audioFile(audioFile
), m_trackType(trackType_non
), m_trackIdx(0), m_properties(initializeProperties()) {}
73 virtual bool startElement(const QString
&namespaceURI
, const QString
&localName
, const QString
&qName
, const QXmlAttributes
&atts
);
74 virtual bool endElement(const QString
&namespaceURI
, const QString
&localName
, const QString
&qName
);
75 virtual bool characters(const QString
& ch
);
88 propertyId_gen_format
,
89 propertyId_gen_format_profile
,
90 propertyId_gen_duration
,
91 propertyId_aud_format
,
92 propertyId_aud_format_version
,
93 propertyId_aud_format_profile
,
94 propertyId_aud_channel_s_
,
95 propertyId_aud_samplingrate
99 const QMap
<QPair
<trackType_t
, QString
>, propertyId_t
> &m_properties
;
101 QStack
<QString
> m_stack
;
102 AudioFileModel
&m_audioFile
;
103 trackType_t m_trackType
;
105 QPair
<trackType_t
, QString
> m_currentProperty
;
107 static QReadWriteLock s_propertiesMutex
;
108 static QScopedPointer
<const QMap
<QPair
<trackType_t
, QString
>, propertyId_t
>> s_propertiesMap
;
110 bool updatePropertry(const QPair
<trackType_t
, QString
> &id
, const QString
&value
);
112 static const QMap
<QPair
<trackType_t
, QString
>, propertyId_t
> &initializeProperties();
113 static bool parseUnsigned(const QString
&str
, quint32
&value
);
114 static quint32
decodeTime(quint32
&value
);
117 ////////////////////////////////////////////////////////////
119 ////////////////////////////////////////////////////////////
121 AnalyzeTask::AnalyzeTask(const int taskId
, const QString
&inputFile
, QAtomicInt
&abortFlag
)
124 m_inputFile(inputFile
),
125 m_mediaInfoBin(lamexp_tools_lookup("mediainfo.exe")),
126 m_avs2wavBin(lamexp_tools_lookup("avs2wav.exe")),
127 m_abortFlag(abortFlag
)
129 if(m_mediaInfoBin
.isEmpty() || m_avs2wavBin
.isEmpty())
131 qFatal("Invalid path to MediaInfo binary. Tool not initialized properly.");
135 AnalyzeTask::~AnalyzeTask(void)
137 emit
taskCompleted(m_taskId
);
140 ////////////////////////////////////////////////////////////
142 ////////////////////////////////////////////////////////////
144 void AnalyzeTask::run()
150 catch(const std::exception
&error
)
152 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error:\n%s\n", error
.what());
153 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
157 MUTILS_PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown exception error!\n");
158 MUtils::OS::fatal_exit(L
"Unhandeled C++ exception error, application will exit!");
162 void AnalyzeTask::run_ex(void)
164 int fileType
= fileTypeNormal
;
165 QString currentFile
= QDir::fromNativeSeparators(m_inputFile
);
166 qDebug("Analyzing: %s", MUTILS_UTF8(currentFile
));
168 AudioFileModel
fileInfo(currentFile
);
169 analyzeFile(currentFile
, fileInfo
, &fileType
);
171 if(MUTILS_BOOLIFY(m_abortFlag
))
173 qWarning("Operation cancelled by user!");
180 qWarning("Cannot access file for reading, skipping!");
183 qWarning("Dummy CDDA file detected, skipping!");
186 if(fileInfo
.metaInfo().title().isEmpty() || fileInfo
.techInfo().containerType().isEmpty() || fileInfo
.techInfo().audioType().isEmpty())
188 fileType
= fileTypeUnknown
;
189 if(!QFileInfo(currentFile
).suffix().compare("cue", Qt::CaseInsensitive
))
191 qWarning("Cue Sheet file detected, skipping!");
192 fileType
= fileTypeCueSheet
;
194 else if(!QFileInfo(currentFile
).suffix().compare("avs", Qt::CaseInsensitive
))
196 qDebug("Found a potential Avisynth script, investigating...");
197 if(analyzeAvisynthFile(currentFile
, fileInfo
))
199 fileType
= fileTypeNormal
;
203 qDebug("Rejected Avisynth file: %s", MUTILS_UTF8(fileInfo
.filePath()));
208 qDebug("Rejected file of unknown type: %s", MUTILS_UTF8(fileInfo
.filePath()));
215 emit
fileAnalyzed(m_taskId
, fileType
, fileInfo
);
218 ////////////////////////////////////////////////////////////
220 ////////////////////////////////////////////////////////////
222 const AudioFileModel
& AnalyzeTask::analyzeFile(const QString
&filePath
, AudioFileModel
&audioFile
, int *const type
)
224 *type
= fileTypeNormal
;
225 QFile
readTest(filePath
);
227 if (!readTest
.open(QIODevice::ReadOnly
))
229 *type
= fileTypeDenied
;
233 if (checkFile_CDDA(readTest
))
235 *type
= fileTypeCDDA
;
240 return analyzeMediaFile(filePath
, audioFile
);
243 const AudioFileModel
& AnalyzeTask::analyzeMediaFile(const QString
&filePath
, AudioFileModel
&audioFile
)
245 //bool skipNext = false;
246 QPair
<quint32
, quint32
> id_val(UINT_MAX
, UINT_MAX
);
247 quint32 coverType
= UINT_MAX
;
248 QByteArray coverData
;
251 params
<< QString("--Language=raw");
252 params
<< QString("-f");
253 params
<< QString("--Output=XML");
254 params
<< QDir::toNativeSeparators(filePath
);
257 MUtils::init_process(process
, QFileInfo(m_mediaInfoBin
).absolutePath());
258 process
.start(m_mediaInfoBin
, params
);
263 if(!process
.waitForStarted())
265 qWarning("MediaInfo process failed to create!");
266 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
268 process
.waitForFinished(-1);
272 while(process
.state() != QProcess::NotRunning
)
274 if(MUTILS_BOOLIFY(m_abortFlag
))
277 qWarning("Process was aborted on user request!");
281 if(!process
.waitForReadyRead())
283 if(process
.state() == QProcess::Running
)
285 qWarning("MediaInfo time out. Killing the process now!");
287 process
.waitForFinished(-1);
294 const QByteArray dataNext
= process
.readAll();
295 if (dataNext
.isEmpty()) {
296 break; /*no more input data*/
302 process
.waitForFinished();
303 if (process
.state() != QProcess::NotRunning
)
306 process
.waitForFinished(-1);
309 while (!process
.atEnd())
311 const QByteArray dataNext
= process
.readAll();
312 if (dataNext
.isEmpty()) {
313 break; /*no more input data*/
318 //qDebug("!!!--START-->>>\n%s\n<<<--END--!!!", data.constData());
319 return parseMediaInfo(data
, audioFile
);
321 /* if(audioFile.metaInfo().title().isEmpty())
323 QString baseName = QFileInfo(filePath).fileName();
324 int index = baseName.lastIndexOf(".");
328 baseName = baseName.left(index);
331 baseName = baseName.replace("_", " ").simplified();
332 index = baseName.lastIndexOf(" - ");
336 baseName = baseName.mid(index + 3).trimmed();
339 audioFile.metaInfo().setTitle(baseName);
342 if((coverType != UINT_MAX) && (!coverData.isEmpty()))
344 retrieveCover(audioFile, coverType, coverData);
347 if((audioFile.techInfo().audioType().compare("PCM", Qt::CaseInsensitive) == 0) && (audioFile.techInfo().audioProfile().compare("Float", Qt::CaseInsensitive) == 0))
349 if(audioFile.techInfo().audioBitdepth() == 32) audioFile.techInfo().setAudioBitdepth(AudioFileModel::BITDEPTH_IEEE_FLOAT32);
355 const AudioFileModel
& AnalyzeTask::parseMediaInfo(const QByteArray
&data
, AudioFileModel
&audioFile
)
357 QXmlInputSource xmlSource
;
358 xmlSource
.setData(data
);
360 QScopedPointer
<QXmlDefaultHandler
> xmlHandler(new AnalyzeTask_XmlHandler(audioFile
));
362 QXmlSimpleReader xmlReader
;
363 xmlReader
.setContentHandler(xmlHandler
.data());
364 xmlReader
.setErrorHandler(xmlHandler
.data());
366 if (xmlReader
.parse(xmlSource
))
368 while (xmlReader
.parseContinue()) {/*continue*/}
371 if(audioFile
.metaInfo().title().isEmpty())
373 QString baseName
= QFileInfo(audioFile
.filePath()).fileName();
375 if((index
= baseName
.lastIndexOf("."))>= 0)
377 baseName
= baseName
.left(index
);
379 baseName
= baseName
.replace("_", " ").simplified();
380 if((index
= baseName
.lastIndexOf(" - ")) >= 0)
382 baseName
= baseName
.mid(index
+ 3).trimmed();
384 audioFile
.metaInfo().setTitle(baseName
);
387 if ((audioFile
.techInfo().audioType().compare("PCM", Qt::CaseInsensitive
) == 0) && (audioFile
.techInfo().audioProfile().compare("Float", Qt::CaseInsensitive
) == 0))
389 if (audioFile
.techInfo().audioBitdepth() == 32) audioFile
.techInfo().setAudioBitdepth(AudioFileModel::BITDEPTH_IEEE_FLOAT32
);
395 bool AnalyzeTask::checkFile_CDDA(QFile
&file
)
398 QByteArray data
= file
.read(128);
400 int i
= data
.indexOf("RIFF");
401 int j
= data
.indexOf("CDDA");
402 int k
= data
.indexOf("fmt ");
404 return ((i
>= 0) && (j
>= 0) && (k
>= 0) && (k
> j
) && (j
> i
));
407 void AnalyzeTask::retrieveCover(AudioFileModel
&audioFile
, const quint32 coverType
, const QByteArray
&coverData
)
409 qDebug("Retrieving cover! (MIME_TYPES_MAX=%u)", MIME_TYPES_MAX
);
411 static const QString ext
= QString::fromLatin1(MIME_TYPES
[qBound(0U, coverType
, MIME_TYPES_MAX
)].ext
[0]);
412 if(!(QImage::fromData(coverData
, ext
.toUpper().toLatin1().constData()).isNull()))
414 QFile
coverFile(QString("%1/%2.%3").arg(MUtils::temp_folder(), MUtils::next_rand_str(), ext
));
415 if(coverFile
.open(QIODevice::WriteOnly
))
417 coverFile
.write(coverData
);
419 audioFile
.metaInfo().setCover(coverFile
.fileName(), true);
424 qWarning("Image data seems to be invalid :-(");
428 bool AnalyzeTask::analyzeAvisynthFile(const QString
&filePath
, AudioFileModel
&info
)
431 MUtils::init_process(process
, QFileInfo(m_avs2wavBin
).absolutePath());
433 process
.start(m_avs2wavBin
, QStringList() << QDir::toNativeSeparators(filePath
) << "?");
435 if(!process
.waitForStarted())
437 qWarning("AVS2WAV process failed to create!");
438 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
440 process
.waitForFinished(-1);
444 bool bInfoHeaderFound
= false;
446 while(process
.state() != QProcess::NotRunning
)
448 if(MUTILS_BOOLIFY(m_abortFlag
))
451 qWarning("Process was aborted on user request!");
455 if(!process
.waitForReadyRead())
457 if(process
.state() == QProcess::Running
)
459 qWarning("AVS2WAV time out. Killing process and skipping file!");
461 process
.waitForFinished(-1);
468 while(process
.canReadLine())
470 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
473 int index
= line
.indexOf(':');
476 QString key
= line
.left(index
).trimmed();
477 QString val
= line
.mid(index
+1).trimmed();
479 if(bInfoHeaderFound
&& !key
.isEmpty() && !val
.isEmpty())
481 if(key
.compare("TotalSeconds", Qt::CaseInsensitive
) == 0)
484 unsigned int duration
= val
.toUInt(&ok
);
485 if(ok
) info
.techInfo().setDuration(duration
);
487 if(key
.compare("SamplesPerSec", Qt::CaseInsensitive
) == 0)
490 unsigned int samplerate
= val
.toUInt(&ok
);
491 if(ok
) info
.techInfo().setAudioSamplerate (samplerate
);
493 if(key
.compare("Channels", Qt::CaseInsensitive
) == 0)
496 unsigned int channels
= val
.toUInt(&ok
);
497 if(ok
) info
.techInfo().setAudioChannels(channels
);
499 if(key
.compare("BitsPerSample", Qt::CaseInsensitive
) == 0)
502 unsigned int bitdepth
= val
.toUInt(&ok
);
503 if(ok
) info
.techInfo().setAudioBitdepth(bitdepth
);
509 if(line
.contains("[Audio Info]", Qt::CaseInsensitive
))
511 info
.techInfo().setAudioType("Avisynth");
512 info
.techInfo().setContainerType("Avisynth");
513 bInfoHeaderFound
= true;
520 process
.waitForFinished();
521 if(process
.state() != QProcess::NotRunning
)
524 process
.waitForFinished(-1);
528 switch(process
.exitCode())
531 qDebug("Avisynth script was analyzed successfully.");
535 qWarning("It appears that Avisynth is not installed on the system!");
539 qWarning("Failed to open the Avisynth script, bad AVS file?");
545 unsigned int AnalyzeTask::parseYear(const QString
&str
)
547 if(str
.startsWith("UTC", Qt::CaseInsensitive
))
549 QDate date
= QDate::fromString(str
.mid(3).trimmed().left(10), "yyyy-MM-dd");
562 int year
= str
.toInt(&ok
);
574 // ---------------------------------------------------------
575 // XML Content Handler Implementation
576 // ---------------------------------------------------------
578 #define DEFINE_PROPTERY_MAPPING(TYPE, NAME) do \
580 builder->insert(qMakePair(trackType_##TYPE, QString::fromLatin1(#NAME)), propertyId_##TYPE##_##NAME); \
584 #define SET_OPTIONAL(TYPE, IF_CMD, THEN_CMD) do \
587 if((IF_CMD)) { THEN_CMD; } \
591 QReadWriteLock
AnalyzeTask_XmlHandler::s_propertiesMutex
;
592 QScopedPointer
<const QMap
<QPair
<AnalyzeTask_XmlHandler::trackType_t
, QString
>, AnalyzeTask_XmlHandler::propertyId_t
>> AnalyzeTask_XmlHandler::s_propertiesMap
;
594 const QMap
<QPair
<AnalyzeTask_XmlHandler::trackType_t
, QString
>, AnalyzeTask_XmlHandler::propertyId_t
> &AnalyzeTask_XmlHandler::initializeProperties(void)
596 QReadLocker
rdLocker(&s_propertiesMutex
);
597 if (!s_propertiesMap
.isNull())
599 return *s_propertiesMap
.data();
603 QWriteLocker
wrLocker(&s_propertiesMutex
);
605 if (s_propertiesMap
.isNull())
607 qWarning("!!! --- SETTING UP MAP --- !!!");
608 QMap
<QPair
<trackType_t
, QString
>, propertyId_t
> *const builder
= new QMap
<QPair
<trackType_t
, QString
>, propertyId_t
>();
609 DEFINE_PROPTERY_MAPPING(gen
, format
);
610 DEFINE_PROPTERY_MAPPING(gen
, format_profile
);
611 DEFINE_PROPTERY_MAPPING(gen
, duration
);
612 DEFINE_PROPTERY_MAPPING(aud
, format
);
613 DEFINE_PROPTERY_MAPPING(aud
, format_version
);
614 DEFINE_PROPTERY_MAPPING(aud
, format_profile
);
615 DEFINE_PROPTERY_MAPPING(aud
, channel_s_
);
616 DEFINE_PROPTERY_MAPPING(aud
, samplingrate
);
617 s_propertiesMap
.reset(builder
);
620 return *s_propertiesMap
.data();
623 bool AnalyzeTask_XmlHandler::startElement(const QString
&namespaceURI
, const QString
&localName
, const QString
&qName
, const QXmlAttributes
&atts
)
626 switch (m_stack
.size())
629 if (!STR_EQ(qName
, "mediaInfo"))
631 qWarning("Invalid XML structure was detected! (1)");
636 if (!STR_EQ(qName
, "file"))
638 qWarning("Invalid XML structure was detected! (2)");
643 if (!STR_EQ(qName
, "track"))
645 qWarning("Invalid XML structure was detected! (3)");
650 const QString value
= atts
.value("type").trimmed();
651 if (STR_EQ(value
, "general"))
653 m_trackType
= trackType_gen
;
655 else if (STR_EQ(value
, "audio"))
659 qWarning("Skipping non-primary audio track!");
660 m_trackType
= trackType_non
;
664 m_trackType
= trackType_aud
;
669 qWarning("Skipping a non-audio track!");
670 m_trackType
= trackType_non
;
678 m_currentProperty
= qMakePair(trackType_gen
, qName
.simplified().toLower());
681 m_currentProperty
= qMakePair(trackType_aud
, qName
.simplified().toLower());
684 m_currentProperty
= qMakePair(trackType_non
, qName
.simplified().toLower());
692 bool AnalyzeTask_XmlHandler::endElement(const QString
&namespaceURI
, const QString
&localName
, const QString
&qName
)
698 bool AnalyzeTask_XmlHandler::characters(const QString
& ch
)
700 if ((m_stack
.size() == 4) && m_currentProperty
.first
)
702 const QString value
= ch
.simplified();
703 if (!value
.isEmpty())
705 qDebug("Property: %u::%s --> \"%s\"", m_currentProperty
.first
, MUTILS_UTF8(m_currentProperty
.second
), MUTILS_UTF8(value
));
706 if (!updatePropertry(m_currentProperty
, value
))
708 qWarning("Ignored property: %u::%s!", m_currentProperty
.first
, MUTILS_UTF8(m_currentProperty
.second
));
715 bool AnalyzeTask_XmlHandler::updatePropertry(const QPair
<trackType_t
, QString
> &id
, const QString
&value
)
717 switch (m_properties
.value(id
, propertyId_t(-1)))
719 case propertyId_gen_format
: m_audioFile
.techInfo().setContainerType(value
); return true;
720 case propertyId_gen_format_profile
: m_audioFile
.techInfo().setContainerProfile(value
); return true;
721 case propertyId_gen_duration
: SET_OPTIONAL(quint32
, parseUnsigned(value
, _tmp
), m_audioFile
.techInfo().setDuration(decodeTime(_tmp
))); return true;
722 case propertyId_aud_format
: m_audioFile
.techInfo().setAudioType(value
); return true;
723 case propertyId_aud_format_version
: m_audioFile
.techInfo().setAudioVersion(value
); return true;
724 case propertyId_aud_format_profile
: m_audioFile
.techInfo().setAudioProfile(value
); return true;
725 case propertyId_aud_channel_s_
: SET_OPTIONAL(quint32
, parseUnsigned(value
, _tmp
), m_audioFile
.techInfo().setAudioChannels(_tmp
)); return true;
726 case propertyId_aud_samplingrate
: SET_OPTIONAL(quint32
, parseUnsigned(value
, _tmp
), m_audioFile
.techInfo().setAudioSamplerate(_tmp
)); return true;
727 default: return false;
731 bool AnalyzeTask_XmlHandler::parseUnsigned(const QString
&str
, quint32
&value
)
734 value
= str
.toUInt(&okay
);
738 quint32
AnalyzeTask_XmlHandler::decodeTime(quint32
&value
)
740 return (value
+ 500U) / 1000U;
743 ////////////////////////////////////////////////////////////
745 ////////////////////////////////////////////////////////////
749 ////////////////////////////////////////////////////////////
751 ////////////////////////////////////////////////////////////