OSDN Git Service

Updated copyright year.
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
index a2d2c86..e3f539e 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2018 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
 #include "model_options.h"
 #include "model_preferences.h"
 #include "model_sysinfo.h"
+#include "model_clipInfo.h"
 #include "job_object.h"
-#include "binaries.h"
+#include "mediainfo.h"
 
 //Encoders
-#include "encoder_x264.h"
-#include "encoder_x265.h"
+#include "encoder_factory.h"
 
 //Source
-#include "source_avisynth.h"
-#include "source_vapoursynth.h"
+#include "source_factory.h"
+
+//MUtils
+#include <MUtils/OSSupport.h>
+#include <MUtils/Version.h>
 
 //Qt Framework
 #include <QDate>
@@ -106,21 +109,6 @@ while(0)
 } \
 while(0)
 
-/*
- * Input types
- */
-typedef enum
-{
-       INPUT_NATIVE = 0,
-       INPUT_AVISYN = 1,
-       INPUT_VAPOUR = 2
-};
-
-/*
- * Static vars
- */
-//static const char *VPS_TEST_FILE = "import vapoursynth as vs\ncore = vs.get_core()\nv = core.std.BlankClip()\nv.set_output()\n";
-
 ///////////////////////////////////////////////////////////////////////////////
 // Constructor & Destructor
 ///////////////////////////////////////////////////////////////////////////////
@@ -142,26 +130,22 @@ EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputF
        m_pause = false;
 
        //Create encoder object
-       switch(options->encType())
-       {
-       case OptionsModel::EncType_X264:
-               m_encoder = new X264Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
-               break;
-       case OptionsModel::EncType_X265:
-               m_encoder = new X265Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
-               break;
-       default:
-               throw "Unknown encoder type encountered!";
-       }
+       m_encoder = EncoderFactory::createEncoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
 
        //Create input handler object
-       switch(getInputType(QFileInfo(m_sourceFileName).suffix()))
+       switch(MediaInfo::analyze(m_sourceFileName))
        {
-       case INPUT_AVISYN:
-               m_pipedSource = new AvisynthSource   (m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
+       case MediaInfo::FILETYPE_AVISYNTH:
+               if(m_sysinfo->hasAvisynth())
+               {
+                       m_pipedSource = SourceFactory::createSource(SourceFactory::SourceType_AVS, m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
+               }
                break;
-       case INPUT_VAPOUR:
-               m_pipedSource = new VapoursynthSource(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
+       case MediaInfo::FILETYPE_VAPOURSYNTH:
+               if(m_sysinfo->hasVapourSynth())
+               {
+                       m_pipedSource = SourceFactory::createSource(SourceFactory::SourceType_VPS, m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
+               }
                break;
        }
 
@@ -172,9 +156,10 @@ EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputF
 
 EncodeThread::~EncodeThread(void)
 {
-       X264_DELETE(m_encoder);
-       X264_DELETE(m_jobObject);
-       X264_DELETE(m_options);
+       MUTILS_DELETE(m_encoder);
+       MUTILS_DELETE(m_jobObject);
+       MUTILS_DELETE(m_options);
+       MUTILS_DELETE(m_pipedSource);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -199,7 +184,7 @@ void EncodeThread::run(void)
        if(m_jobObject)
        {
                m_jobObject->terminateJob(42);
-               X264_DELETE(m_jobObject);
+               MUTILS_DELETE(m_jobObject);
        }
 }
 
@@ -215,6 +200,11 @@ void EncodeThread::checkedRun(void)
                        ExecutionStateHandler executionStateHandler;
                        encode();
                }
+               catch(const std::exception &e)
+               {
+                       log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what())));
+                       setStatus(JobStatus_Failed);
+               }
                catch(char *msg)
                {
                        log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
@@ -228,7 +218,7 @@ void EncodeThread::checkedRun(void)
        }
        catch(...)
        {
-               x264_fatal_exit(L"Unhandeled exception error in encode thread!");
+               MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!");
        }
 }
 
@@ -256,27 +246,29 @@ void EncodeThread::encode(void)
        // -----------------------------------------------------------------------------------
 
        //Print some basic info
-       log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
+       log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), MUtils::Version::app_build_date().toString(Qt::ISODate)));
        log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
-       log(tr("Source file: %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
-       log(tr("Output file: %1").arg(QDir::toNativeSeparators(m_outputFileName)));
+       log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
+       log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
        
        //Print system info
        log(tr("\n--- SYSTEMINFO ---\n"));
-       log(tr("Binary Path: %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
-       log(tr("Avisynth OK: %1").arg(m_sysinfo->hasAVSSupport() ? tr("Yes") : tr("No")));
-       log(tr("VapourSynth: %1").arg(m_sysinfo->hasVPSSupport() ? QDir::toNativeSeparators(m_sysinfo->getVPSPath()) : tr("N/A")));
+       log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
+       log(tr("Avisynth    : %1").arg(m_sysinfo->hasAvisynth() ? tr("Yes") : tr("No")));
+       log(tr("VapourSynth : %1").arg(m_sysinfo->hasVapourSynth() ? tr("Yes") : tr("No")));
 
        //Print encoder settings
        log(tr("\n--- SETTINGS ---\n"));
-       log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
-       log(tr("Preset:  %1").arg(m_options->preset()));
-       log(tr("Tuning:  %1").arg(m_options->tune()));
-       log(tr("Profile: %1").arg(m_options->profile()));
-       log(tr("Custom:  %1").arg(m_options->customEncParams().isEmpty() ? tr("(None)") : m_options->customEncParams()));
+       log(tr("Encoder : %1").arg(m_encoder->getName()));
+       log(tr("Source  : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
+       log(tr("RC Mode : %1").arg(m_encoder->getEncoderInfo().rcModeToString(m_options->rcMode())));
+       log(tr("Preset  : %1").arg(m_options->preset()));
+       log(tr("Tuning  : %1").arg(m_options->tune()));
+       log(tr("Profile : %1").arg(m_options->profile()));
+       log(tr("Custom  : %1").arg(m_options->customEncParams().isEmpty() ? tr("<None>") : m_options->customEncParams()));
        
        bool ok = false;
-       unsigned int frames = 0;
+       ClipInfo clipInfo;
        
        // -----------------------------------------------------------------------------------
        // Check Versions
@@ -284,34 +276,40 @@ void EncodeThread::encode(void)
        
        log(tr("\n--- CHECK VERSION ---\n"));
 
+       unsigned int encoderRevision = UINT_MAX, sourceRevision = UINT_MAX;
+       bool encoderModified = false, sourceModified = false;
+
+       log("Detect video encoder version:\n");
+
        //Check encoder version
-       bool encoderModified = false;
-       const unsigned int encoderRevision = m_encoder->checkVersion(encoderModified);
+       encoderRevision = m_encoder->checkVersion(encoderModified);
        CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
-       
-       //Print source versions
-       m_encoder->printVersion(encoderRevision, encoderModified);
 
        //Is encoder version suppoprted?
        CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
 
        if(m_pipedSource)
        {
+               log("\nDetect video source version:\n");
+
                //Is source type available?
                CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
 
                //Checking source version
-               bool sourceModified = false;
-               const unsigned int sourceRevision = m_pipedSource->checkVersion(sourceModified);
+               sourceRevision = m_pipedSource->checkVersion(sourceModified);
                CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
 
-               //Print source versions
-               m_pipedSource->printVersion(sourceRevision, sourceModified);
-
                //Is source version supported?
                CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
        }
 
+       //Print tool versions
+       log(QString("\n> %1").arg(m_encoder->printVersion(encoderRevision, encoderModified)));
+       if(m_pipedSource)
+       {
+               log(QString("> %1").arg(m_pipedSource->printVersion(sourceRevision, sourceModified)));
+       }
+
        // -----------------------------------------------------------------------------------
        // Detect Source Info
        // -----------------------------------------------------------------------------------
@@ -320,7 +318,7 @@ void EncodeThread::encode(void)
        if(m_pipedSource)
        {
                log(tr("\n--- GET SOURCE INFO ---\n"));
-               ok = m_pipedSource->checkSourceProperties(frames);
+               ok = m_pipedSource->checkSourceProperties(clipInfo);
                CHECK_STATUS(m_abort, ok);
        }
 
@@ -329,22 +327,22 @@ void EncodeThread::encode(void)
        // -----------------------------------------------------------------------------------
 
        //Run encoding passes
-       if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
+       if(m_encoder->getEncoderInfo().rcModeToType(m_options->rcMode()) == AbstractEncoderInfo::RC_TYPE_MULTIPASS)
        {
                const QString passLogFile = getPasslogFile(m_outputFileName);
                
                log(tr("\n--- ENCODING PASS #1 ---\n"));
-               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 1, passLogFile);
+               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo, 1, passLogFile);
                CHECK_STATUS(m_abort, ok);
 
                log(tr("\n--- ENCODING PASS #2 ---\n"));
-               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 2, passLogFile);
+               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo, 2, passLogFile);
                CHECK_STATUS(m_abort, ok);
        }
        else
        {
                log(tr("\n--- ENCODING VIDEO ---\n"));
-               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
+               ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo);
                CHECK_STATUS(m_abort, ok);
        }
 
@@ -365,7 +363,7 @@ void EncodeThread::encode(void)
 
 void EncodeThread::log(const QString &text)
 {
-       emit messageLogged(m_jobId, text);
+       emit messageLogged(m_jobId, QDateTime::currentMSecsSinceEpoch(), text);
 }
 
 void EncodeThread::setStatus(const JobStatus &newStatus)
@@ -407,18 +405,6 @@ void EncodeThread::setDetails(const QString &text)
        }
 }
 
-int EncodeThread::getInputType(const QString &fileExt)
-{
-       int type = INPUT_NATIVE;
-
-       if(fileExt.compare("avs",  Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
-       if(fileExt.compare("avsi", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
-       if(fileExt.compare("vpy",  Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
-       if(fileExt.compare("py",   Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
-
-       return type;
-}
-
 QString EncodeThread::getPasslogFile(const QString &outputFile)
 {
        QFileInfo info(outputFile);