1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
24 #include "targetver.h"
27 #define ENABLE_X264_VERSION_INCLUDE
29 #undef ENABLE_X264_VERSION_INCLUDE
33 #define WIN32_LEAN_AND_MEAN
48 #include <QApplication>
49 #include <QMessageBox>
55 #include <QPlastiqueStyle>
56 #include <QImageReader>
57 #include <QSharedMemory>
59 #include <QStringList>
60 #include <QSystemSemaphore>
61 #include <QDesktopServices>
67 #include <QTranslator>
70 #include <QLibraryInfo>
72 #include <QReadLocker>
73 #include <QWriteLocker>
89 static bool g_x264_console_attached = false;
90 static QMutex g_x264_message_mutex;
91 static const DWORD g_main_thread_id = GetCurrentThreadId();
92 static FILE *g_x264_log_file = NULL;
93 static QDate g_x264_version_date;
96 static const char *g_x264_months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
97 static const char *g_x264_imageformats[] = {"png", "jpg", "gif", "ico", "svg", NULL};
102 unsigned int ver_major;
103 unsigned int ver_minor;
104 unsigned int ver_patch;
105 unsigned int ver_build;
106 const char* ver_date;
107 const char* ver_time;
108 unsigned int ver_x264_minimum_rev;
109 unsigned int ver_x264_current_api;
110 unsigned int ver_x264_avs2yuv_ver;
120 (VER_X264_MINIMUM_REV),
121 (VER_X264_CURRENT_API),
122 (VER_X264_AVS2YUV_VER)
137 x264_os_version_t version;
155 bool bPortableModeEnabled;
160 //Known Windows versions - maps marketing names to the actual Windows NT versions
161 const x264_os_version_t x264_winver_win2k = {5,0};
162 const x264_os_version_t x264_winver_winxp = {5,1};
163 const x264_os_version_t x264_winver_xpx64 = {5,2};
164 const x264_os_version_t x264_winver_vista = {6,0};
165 const x264_os_version_t x264_winver_win70 = {6,1};
166 const x264_os_version_t x264_winver_win80 = {6,2};
167 const x264_os_version_t x264_winver_win81 = {6,3};
170 static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
172 ///////////////////////////////////////////////////////////////////////////////
174 ///////////////////////////////////////////////////////////////////////////////
177 #define CLEAN_OUTPUT_STRING(STR) do \
179 const char CTRL_CHARS[3] = { '\r', '\n', '\t' }; \
180 for(size_t i = 0; i < 3; i++) \
182 while(char *pos = strchr((STR), CTRL_CHARS[i])) *pos = char(0x20); \
188 #define TRIM_LEFT(STR) do \
190 const char WHITE_SPACE[4] = { char(0x20), '\r', '\n', '\t' }; \
191 for(size_t i = 0; i < 4; i++) \
193 while(*(STR) == WHITE_SPACE[i]) (STR)++; \
198 #define X264_ZERO_MEMORY(X) SecureZeroMemory(&X, sizeof(X))
200 ///////////////////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////////////////
205 * Disclaimer: Parts of the following code were borrowed from MPC-HC project: http://mpc-hc.sf.net/
209 #if defined(__INTEL_COMPILER)
210 #if (__INTEL_COMPILER >= 1300)
211 static const char *g_x264_version_compiler = "ICL 13." X264_MAKE_STR(__INTEL_COMPILER_BUILD_DATE);
212 #elif (__INTEL_COMPILER >= 1200)
213 static const char *g_x264_version_compiler = "ICL 12." X264_MAKE_STR(__INTEL_COMPILER_BUILD_DATE);
214 #elif (__INTEL_COMPILER >= 1100)
215 static const char *g_x264_version_compiler = "ICL 11.x";
216 #elif (__INTEL_COMPILER >= 1000)
217 static const char *g_x264_version_compiler = "ICL 10.x";
219 #error Compiler is not supported!
221 #elif defined(_MSC_VER)
222 #if (_MSC_VER == 1800)
223 #if (_MSC_FULL_VER < 180021005)
224 static const char *g_x264_version_compiler = "MSVC 2013-Beta";
225 #elif (_MSC_FULL_VER == 180021005)
226 static const char *g_x264_version_compiler = "MSVC 2013";
228 #error Compiler version is not supported yet!
230 #elif (_MSC_VER == 1700)
231 #if (_MSC_FULL_VER < 170050727)
232 static const char *g_x264_version_compiler = "MSVC 2012-Beta";
233 #elif (_MSC_FULL_VER < 170051020)
234 static const char *g_x264_version_compiler = "MSVC 2012";
235 #elif (_MSC_FULL_VER < 170051106)
236 static const char *g_x264_version_compiler = "MSVC 2012.1-CTP";
237 #elif (_MSC_FULL_VER < 170060315)
238 static const char *g_x264_version_compiler = "MSVC 2012.1";
239 #elif (_MSC_FULL_VER < 170060610)
240 static const char *g_x264_version_compiler = "MSVC 2012.2";
241 #elif (_MSC_FULL_VER == 170060610)
242 static const char *g_x264_version_compiler = "MSVC 2012.3";
244 #error Compiler version is not supported yet!
246 #elif (_MSC_VER == 1600)
247 #if (_MSC_FULL_VER < 160040219)
248 static const char *g_x264_version_compiler = "MSVC 2010";
249 #elif (_MSC_FULL_VER == 160040219)
250 static const char *g_x264_version_compiler = "MSVC 2010-SP1";
252 #error Compiler version is not supported yet!
254 #elif (_MSC_VER == 1500)
255 #if (_MSC_FULL_VER >= 150030729)
256 static const char *g_x264_version_compiler = "MSVC 2008-SP1";
258 static const char *g_x264_version_compiler = "MSVC 2008";
261 #error Compiler is not supported!
264 // Note: /arch:SSE and /arch:SSE2 are only available for the x86 platform
265 #if !defined(_M_X64) && defined(_M_IX86_FP)
266 #if (_M_IX86_FP == 1)
267 X264_COMPILER_WARNING("SSE instruction set is enabled!")
268 #elif (_M_IX86_FP == 2)
269 X264_COMPILER_WARNING("SSE2 (or higher) instruction set is enabled!")
273 #error Compiler is not supported!
276 //Architecture detection
278 static const char *g_x264_version_arch = "x64";
279 #elif defined(_M_IX86)
280 static const char *g_x264_version_arch = "x86";
282 #error Architecture is not supported!
285 ///////////////////////////////////////////////////////////////////////////////
287 ///////////////////////////////////////////////////////////////////////////////
289 static __forceinline bool x264_check_for_debugger(void);
292 * Suspend calling thread for N milliseconds
294 inline void x264_sleep(const unsigned int delay)
300 * Global exception handler
302 LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo)
304 if(GetCurrentThreadId() != g_main_thread_id)
306 HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
307 if(mainThread) TerminateThread(mainThread, ULONG_MAX);
310 x264_fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
315 * Invalid parameters handler
317 void x264_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t)
319 if(GetCurrentThreadId() != g_main_thread_id)
321 HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
322 if(mainThread) TerminateThread(mainThread, ULONG_MAX);
325 x264_fatal_exit(L"Invalid parameter handler invoked, application will exit!");
329 * Change console text color
331 static void x264_console_color(FILE* file, WORD attributes)
333 const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
334 if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
336 SetConsoleTextAttribute(hConsole, attributes);
341 * Output logging message to console
343 static void x264_write_console(const int type, const char *msg)
347 if(_isatty(_fileno(stderr)))
349 UINT oldOutputCP = GetConsoleOutputCP();
350 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
356 x264_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY);
357 fprintf(stderr, GURU_MEDITATION);
358 fprintf(stderr, "%s\n", msg);
362 x264_console_color(stderr, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
363 fprintf(stderr, "%s\n", msg);
367 x264_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
368 fprintf(stderr, "%s\n", msg);
373 x264_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
374 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(oldOutputCP);
379 /*ignore any exception that might occur here!*/
384 * Output logging message to debugger
386 static void x264_write_dbg_out(const int type, const char *msg)
388 const char *FORMAT = "[sx264l][%c] %s\n";
393 const char* input = msg;
400 _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'C', input);
403 _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'W', input);
406 _snprintf_s(buffer, 512, _TRUNCATE, FORMAT, 'I', input);
410 char *temp = &buffer[0];
411 CLEAN_OUTPUT_STRING(temp);
412 OutputDebugStringA(temp);
416 /*ignore any exception that might occur here!*/
421 * Output logging message to logfile
423 static void x264_write_logfile(const int type, const char *msg)
425 const char *FORMAT = "[%c][%04u] %s\r\n";
432 strncpy_s(buffer, 512, msg, _TRUNCATE);
434 char *temp = &buffer[0];
436 CLEAN_OUTPUT_STRING(temp);
438 const unsigned int timestamp = static_cast<unsigned int>(_time64(NULL) % 3600I64);
444 fprintf(g_x264_log_file, FORMAT, 'C', timestamp, temp);
447 fprintf(g_x264_log_file, FORMAT, 'W', timestamp, temp);
450 fprintf(g_x264_log_file, FORMAT, 'I', timestamp, temp);
454 fflush(g_x264_log_file);
459 /*ignore any exception that might occur here!*/
466 void x264_message_handler(QtMsgType type, const char *msg)
468 if((!msg) || (!(msg[0])))
473 QMutexLocker lock(&g_x264_message_mutex);
477 x264_write_logfile(type, msg);
480 if(g_x264_console_attached)
482 x264_write_console(type, msg);
486 x264_write_dbg_out(type, msg);
489 if((type == QtCriticalMsg) || (type == QtFatalMsg))
492 x264_fatal_exit(L"The application has encountered a critical error and will exit now!", QWCHAR(QString::fromUtf8(msg)));
497 * Initialize the console
499 void x264_init_console(const QStringList &argv)
501 bool enableConsole = x264_is_prerelease() || (X264_DEBUG);
505 wchar_t *logfile = NULL;
506 size_t logfile_len = 0;
507 if(!_wdupenv_s(&logfile, &logfile_len, L"X264_LOGFILE"))
509 if(logfile && (logfile_len > 0))
512 if(!_wfopen_s(&temp, logfile, L"wb"))
514 fprintf(temp, "%c%c%c", char(0xEF), char(0xBB), char(0xBF));
515 g_x264_log_file = temp;
524 for(int i = 0; i < argv.count(); i++)
526 if(!argv.at(i).compare("--console", Qt::CaseInsensitive))
528 enableConsole = true;
530 else if(!argv.at(i).compare("--no-console", Qt::CaseInsensitive))
532 enableConsole = false;
539 if(!g_x264_console_attached)
541 if(AllocConsole() != FALSE)
543 SetConsoleCtrlHandler(NULL, TRUE);
544 SetConsoleTitle(L"Simple x264 Launcher | Debug Console");
545 SetConsoleOutputCP(CP_UTF8);
546 g_x264_console_attached = true;
550 if(g_x264_console_attached)
552 //-------------------------------------------------------------------
553 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
554 //-------------------------------------------------------------------
555 const int flags = _O_WRONLY | _O_U8TEXT;
556 int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
557 int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE), flags);
558 FILE *hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
559 FILE *hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
560 if(hfStdOut) { *stdout = *hfStdOut; std::cout.rdbuf(new std::filebuf(hfStdOut)); }
561 if(hfStdErr) { *stderr = *hfStdErr; std::cerr.rdbuf(new std::filebuf(hfStdErr)); }
564 HWND hwndConsole = GetConsoleWindow();
566 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
568 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
569 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
570 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
572 SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
573 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
574 SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
580 * Initialize the console
582 void x264_init_console(int argc, char* argv[])
584 bool enableConsole = x264_is_prerelease() || (X264_DEBUG);
588 wchar_t *logfile = NULL;
589 size_t logfile_len = 0;
590 if(!_wdupenv_s(&logfile, &logfile_len, L"X264_LAUNCHER_LOGFILE"))
592 if(logfile && (logfile_len > 0))
595 if(!_wfopen_s(&temp, logfile, L"wb"))
597 fprintf(temp, "%c%c%c", 0xEF, 0xBB, 0xBF);
598 g_x264_log_file = temp;
607 for(int i = 0; i < argc; i++)
609 if(!_stricmp(argv[i], "--console"))
611 enableConsole = true;
613 else if(!_stricmp(argv[i], "--no-console"))
615 enableConsole = false;
622 if(!g_x264_console_attached)
626 SetConsoleCtrlHandler(NULL, TRUE);
627 SetConsoleTitle(L"Simple x264 Launcher | Debug Console");
628 SetConsoleOutputCP(CP_UTF8);
629 g_x264_console_attached = true;
633 if(g_x264_console_attached)
635 //-------------------------------------------------------------------
636 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
637 //-------------------------------------------------------------------
638 const int flags = _O_WRONLY | _O_U8TEXT;
639 int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
640 int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE), flags);
641 FILE *hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
642 FILE *hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
643 if(hfStdOut) { *stdout = *hfStdOut; std::cout.rdbuf(new std::filebuf(hfStdOut)); }
644 if(hfStdErr) { *stderr = *hfStdErr; std::cerr.rdbuf(new std::filebuf(hfStdErr)); }
647 HWND hwndConsole = GetConsoleWindow();
649 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
651 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
652 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
653 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
655 SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
656 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
657 SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
665 unsigned int x264_version_major(void)
667 return g_x264_version.ver_major;
670 unsigned int x264_version_minor(void)
672 return (g_x264_version.ver_minor * 10) + (g_x264_version.ver_patch % 10);
675 unsigned int x264_version_build(void)
677 return g_x264_version.ver_build;
680 const char *x264_version_compiler(void)
682 return g_x264_version_compiler;
685 const char *x264_version_arch(void)
687 return g_x264_version_arch;
690 unsigned int x264_version_x264_minimum_rev(void)
692 return g_x264_version.ver_x264_minimum_rev;
695 unsigned int x264_version_x264_current_api(void)
697 return g_x264_version.ver_x264_current_api;
700 unsigned int x264_version_x264_avs2yuv_ver(void)
702 return g_x264_version.ver_x264_avs2yuv_ver;
708 const QStringList &x264_arguments(void)
710 QReadLocker readLock(&g_x264_argv.lock);
712 if(!g_x264_argv.list)
715 QWriteLocker writeLock(&g_x264_argv.lock);
717 g_x264_argv.list = new QStringList;
720 LPWSTR *szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
722 if(NULL != szArglist)
724 for(int i = 0; i < nArgs; i++)
726 (*g_x264_argv.list) << WCHAR2QSTR(szArglist[i]);
728 LocalFree(szArglist);
732 qWarning("CommandLineToArgvW() has failed !!!");
736 return (*g_x264_argv.list);
740 * Check for portable mode
742 bool x264_portable(void)
744 QReadLocker readLock(&g_x264_portable.lock);
746 if(g_x264_portable.bInitialized)
748 return g_x264_portable.bPortableModeEnabled;
752 QWriteLocker writeLock(&g_x264_portable.lock);
754 if(!g_x264_portable.bInitialized)
756 if(VER_X264_PORTABLE_EDITION)
758 qWarning("Simple x264 Launcher portable edition!\n");
759 g_x264_portable.bPortableModeEnabled = true;
763 QString baseName = QFileInfo(QApplication::applicationFilePath()).completeBaseName();
764 int idx1 = baseName.indexOf("x264", 0, Qt::CaseInsensitive);
765 int idx2 = baseName.lastIndexOf("portable", -1, Qt::CaseInsensitive);
766 g_x264_portable.bPortableModeEnabled = (idx1 >= 0) && (idx2 >= 0) && (idx1 < idx2);
768 g_x264_portable.bInitialized = true;
771 return g_x264_portable.bPortableModeEnabled;
775 * Get data path (i.e. path to store config files)
777 const QString &x264_data_path(void)
779 static QString pathCache;
781 if(pathCache.isNull())
785 pathCache = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
787 if(pathCache.isEmpty() || x264_portable())
789 pathCache = QApplication::applicationDirPath();
791 if(!QDir(pathCache).mkpath("."))
793 qWarning("Data directory could not be created:\n%s\n", pathCache.toUtf8().constData());
794 pathCache = QDir::currentPath();
802 * Get build date date
804 const QDate &x264_version_date(void)
806 if(!g_x264_version_date.isValid())
808 int date[3] = {0, 0, 0}; char temp[12] = {'\0'};
809 strncpy_s(temp, 12, g_x264_version.ver_date, _TRUNCATE);
811 if(strlen(temp) == 11)
813 temp[3] = temp[6] = '\0';
814 date[2] = atoi(&temp[4]);
815 date[0] = atoi(&temp[7]);
817 for(int j = 0; j < 12; j++)
819 if(!_strcmpi(&temp[0], g_x264_months[j]))
826 g_x264_version_date = QDate(date[0], date[1], date[2]);
829 if(!g_x264_version_date.isValid())
831 qFatal("Internal error: Date format could not be recognized!");
835 return g_x264_version_date;
838 const char *x264_version_time(void)
840 return g_x264_version.ver_time;
843 bool x264_is_prerelease(void)
845 return (VER_X264_PRE_RELEASE);
849 * CPUID prototype (actual function is in ASM code)
853 void x264_cpu_cpuid(unsigned int op, unsigned int *eax, unsigned int *ebx, unsigned int *ecx, unsigned int *edx);
857 * Detect CPU features
859 x264_cpu_t x264_detect_cpu_features(const QStringList &argv)
861 typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process);
864 SYSTEM_INFO systemInfo;
865 unsigned int CPUInfo[4];
866 char CPUIdentificationString[0x40];
867 char CPUBrandString[0x40];
869 memset(&features, 0, sizeof(x264_cpu_t));
870 memset(&systemInfo, 0, sizeof(SYSTEM_INFO));
871 memset(CPUIdentificationString, 0, sizeof(CPUIdentificationString));
872 memset(CPUBrandString, 0, sizeof(CPUBrandString));
874 x264_cpu_cpuid(0, &CPUInfo[0], &CPUInfo[1], &CPUInfo[2], &CPUInfo[3]);
875 memcpy(CPUIdentificationString, &CPUInfo[1], sizeof(int));
876 memcpy(CPUIdentificationString + 4, &CPUInfo[3], sizeof(int));
877 memcpy(CPUIdentificationString + 8, &CPUInfo[2], sizeof(int));
878 features.intel = (_stricmp(CPUIdentificationString, "GenuineIntel") == 0);
879 strncpy_s(features.vendor, 0x40, CPUIdentificationString, _TRUNCATE);
883 x264_cpu_cpuid(1, &CPUInfo[0], &CPUInfo[1], &CPUInfo[2], &CPUInfo[3]);
884 features.mmx = (CPUInfo[3] & 0x800000) || false;
885 features.sse = (CPUInfo[3] & 0x2000000) || false;
886 features.sse2 = (CPUInfo[3] & 0x4000000) || false;
887 features.ssse3 = (CPUInfo[2] & 0x200) || false;
888 features.sse3 = (CPUInfo[2] & 0x1) || false;
889 features.ssse3 = (CPUInfo[2] & 0x200) || false;
890 features.stepping = CPUInfo[0] & 0xf;
891 features.model = ((CPUInfo[0] >> 4) & 0xf) + (((CPUInfo[0] >> 16) & 0xf) << 4);
892 features.family = ((CPUInfo[0] >> 8) & 0xf) + ((CPUInfo[0] >> 20) & 0xff);
893 if(features.sse) features.mmx2 = true; //MMXEXT is a subset of SSE!
896 x264_cpu_cpuid(0x80000000, &CPUInfo[0], &CPUInfo[1], &CPUInfo[2], &CPUInfo[3]);
897 int nExIds = qMax<int>(qMin<int>(CPUInfo[0], 0x80000004), 0x80000000);
899 if((_stricmp(CPUIdentificationString, "AuthenticAMD") == 0) && (nExIds >= 0x80000001U))
901 x264_cpu_cpuid(0x80000001, &CPUInfo[0], &CPUInfo[1], &CPUInfo[2], &CPUInfo[3]);
902 features.mmx2 = features.mmx2 || (CPUInfo[3] & 0x00400000U);
905 for(int i = 0x80000002; i <= nExIds; ++i)
907 x264_cpu_cpuid(i, &CPUInfo[0], &CPUInfo[1], &CPUInfo[2], &CPUInfo[3]);
911 memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
914 memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
917 memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
922 strncpy_s(features.brand, 0x40, CPUBrandString, _TRUNCATE);
924 if(strlen(features.brand) < 1) strncpy_s(features.brand, 0x40, "Unknown", _TRUNCATE);
925 if(strlen(features.vendor) < 1) strncpy_s(features.vendor, 0x40, "Unknown", _TRUNCATE);
927 #if (!(defined(_M_X64) || defined(_M_IA64)))
928 QLibrary Kernel32Lib("kernel32.dll");
929 if(IsWow64ProcessFun IsWow64ProcessPtr = (IsWow64ProcessFun) Kernel32Lib.resolve("IsWow64Process"))
931 BOOL x64flag = FALSE;
932 if(IsWow64ProcessPtr(GetCurrentProcess(), &x64flag))
934 features.x64 = (x64flag == TRUE);
941 DWORD_PTR procAffinity, sysAffinity;
942 if(GetProcessAffinityMask(GetCurrentProcess(), &procAffinity, &sysAffinity))
944 for(DWORD_PTR mask = 1; mask; mask <<= 1)
946 features.count += ((sysAffinity & mask) ? (1) : (0));
949 if(features.count < 1)
951 GetNativeSystemInfo(&systemInfo);
952 features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
958 for(int i = 0; i < argv.count(); i++)
960 if(!argv[i].compare("--force-cpu-no-64bit", Qt::CaseInsensitive)) { flag = true; features.x64 = false; }
961 if(!argv[i].compare("--force-cpu-no-sse", Qt::CaseInsensitive)) { flag = true; features.sse = features.sse2 = features.sse3 = features.ssse3 = false; }
962 if(!argv[i].compare("--force-cpu-no-intel", Qt::CaseInsensitive)) { flag = true; features.intel = false; }
964 if(flag) qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
971 * Verify a specific Windows version
973 static bool x264_verify_os_version(const DWORD major, const DWORD minor)
975 OSVERSIONINFOEXW osvi;
976 DWORDLONG dwlConditionMask = 0;
978 //Initialize the OSVERSIONINFOEX structure
979 memset(&osvi, 0, sizeof(OSVERSIONINFOEXW));
980 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
981 osvi.dwMajorVersion = major;
982 osvi.dwMinorVersion = minor;
983 osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;
985 //Initialize the condition mask
986 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
987 VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
988 VER_SET_CONDITION(dwlConditionMask, VER_PLATFORMID, VER_EQUAL);
991 const BOOL ret = VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_PLATFORMID, dwlConditionMask);
996 if(GetLastError() != ERROR_OLD_WIN_VERSION)
998 qWarning("VerifyVersionInfo() system call has failed!");
1002 return (ret != FALSE);
1006 * Determine the *real* Windows version
1008 static bool x264_get_real_os_version(unsigned int *major, unsigned int *minor, bool *pbOverride)
1010 *major = *minor = 0;
1011 *pbOverride = false;
1013 //Initialize local variables
1014 OSVERSIONINFOEXW osvi;
1015 memset(&osvi, 0, sizeof(OSVERSIONINFOEXW));
1016 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEXW);
1018 //Try GetVersionEx() first
1019 if(GetVersionExW((LPOSVERSIONINFOW)&osvi) == FALSE)
1021 qWarning("GetVersionEx() has failed, cannot detect Windows version!");
1025 //Make sure we are running on NT
1026 if(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
1028 *major = osvi.dwMajorVersion;
1029 *minor = osvi.dwMinorVersion;
1033 qWarning("Not running on Windows NT, unsupported operating system!");
1037 //Determine the real *major* version first
1040 const DWORD nextMajor = (*major) + 1;
1041 if(x264_verify_os_version(nextMajor, 0))
1051 //Now also determine the real *minor* version
1054 const DWORD nextMinor = (*minor) + 1;
1055 if(x264_verify_os_version((*major), nextMinor))
1068 * Get the native operating system version
1070 const x264_os_version_t &x264_get_os_version(void)
1072 QReadLocker readLock(&g_x264_os_version.lock);
1074 //Already initialized?
1075 if(g_x264_os_version.bInitialized)
1077 return g_x264_os_version.version;
1081 QWriteLocker writeLock(&g_x264_os_version.lock);
1084 if(!g_x264_os_version.bInitialized)
1086 unsigned int major, minor; bool oflag;
1087 if(x264_get_real_os_version(&major, &minor, &oflag))
1089 g_x264_os_version.version.versionMajor = major;
1090 g_x264_os_version.version.versionMinor = minor;
1091 g_x264_os_version.version.overrideFlag = oflag;
1092 g_x264_os_version.bInitialized = true;
1096 qWarning("Failed to determin the operating system version!");
1100 return g_x264_os_version.version;
1104 * Check for compatibility mode
1106 static bool x264_check_compatibility_mode(const char *exportName, const char *executableName)
1108 QLibrary kernel32("kernel32.dll");
1110 if(exportName != NULL)
1112 if(kernel32.resolve(exportName) != NULL)
1114 qWarning("Function '%s' exported from 'kernel32.dll' -> Windows compatibility mode!", exportName);
1115 qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
1124 * Check if we are running under wine
1126 bool x264_detect_wine(void)
1128 QReadLocker readLock(&g_x264_wine.lock);
1130 //Already initialized?
1131 if(g_x264_wine.bInitialized)
1133 return g_x264_wine.bIsWine;
1137 QWriteLocker writeLock(&g_x264_wine.lock);
1139 if(!g_x264_wine.bInitialized)
1141 g_x264_wine.bIsWine = false;
1142 QLibrary ntdll("ntdll.dll");
1145 if(ntdll.resolve("wine_nt_to_unix_file_name") != NULL) g_x264_wine.bIsWine = true;
1146 if(ntdll.resolve("wine_get_version") != NULL) g_x264_wine.bIsWine = true;
1149 g_x264_wine.bInitialized = true;
1152 return g_x264_wine.bIsWine;
1158 static bool x264_event_filter(void *message, long *result)
1160 if((!(X264_DEBUG)) && x264_check_for_debugger())
1162 x264_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
1165 //switch(reinterpret_cast<MSG*>(message)->message)
1167 //case WM_QUERYENDSESSION:
1168 // qWarning("WM_QUERYENDSESSION message received!");
1169 // *result = x264_broadcast(x264_event_queryendsession, false) ? TRUE : FALSE;
1171 //case WM_ENDSESSION:
1172 // qWarning("WM_ENDSESSION message received!");
1173 // if(reinterpret_cast<MSG*>(message)->wParam == TRUE)
1175 // x264_broadcast(x264_event_endsession, false);
1176 // if(QApplication *app = reinterpret_cast<QApplication*>(QApplication::instance()))
1178 // app->closeAllWindows();
1181 // x264_finalization();
1187 // /*ignore this message and let Qt handle it*/
1195 * Check for process elevation
1197 static bool x264_check_elevation(void)
1199 typedef enum { x264_token_elevationType_class = 18, x264_token_elevation_class = 20 } X264_TOKEN_INFORMATION_CLASS;
1200 typedef enum { x264_elevationType_default = 1, x264_elevationType_full, x264_elevationType_limited } X264_TOKEN_ELEVATION_TYPE;
1202 HANDLE hToken = NULL;
1203 bool bIsProcessElevated = false;
1205 if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
1207 X264_TOKEN_ELEVATION_TYPE tokenElevationType;
1209 if(GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS) x264_token_elevationType_class, &tokenElevationType, sizeof(X264_TOKEN_ELEVATION_TYPE), &returnLength))
1211 if(returnLength == sizeof(X264_TOKEN_ELEVATION_TYPE))
1213 switch(tokenElevationType)
1215 case x264_elevationType_default:
1216 qDebug("Process token elevation type: Default -> UAC is disabled.\n");
1218 case x264_elevationType_full:
1219 qWarning("Process token elevation type: Full -> potential security risk!\n");
1220 bIsProcessElevated = true;
1222 case x264_elevationType_limited:
1223 qDebug("Process token elevation type: Limited -> not elevated.\n");
1228 CloseHandle(hToken);
1232 qWarning("Failed to open process token!");
1235 return !bIsProcessElevated;
1239 * Initialize Qt framework
1241 bool x264_init_qt(int argc, char* argv[])
1243 static bool qt_initialized = false;
1244 typedef BOOL (WINAPI *SetDllDirectoryProc)(WCHAR *lpPathName);
1245 const QStringList &arguments = x264_arguments();
1247 //Don't initialized again, if done already
1253 //Secure DLL loading
1254 QLibrary kernel32("kernel32.dll");
1257 SetDllDirectoryProc pSetDllDirectory = (SetDllDirectoryProc) kernel32.resolve("SetDllDirectoryW");
1258 if(pSetDllDirectory != NULL) pSetDllDirectory(L"");
1261 //Extract executable name from argv[] array
1262 QString executableName = QLatin1String("x264_launcher.exe");
1263 if(arguments.count() > 0)
1265 static const char *delimiters = "\\/:?";
1266 executableName = arguments[0].trimmed();
1267 for(int i = 0; delimiters[i]; i++)
1269 int temp = executableName.lastIndexOf(QChar(delimiters[i]));
1270 if(temp >= 0) executableName = executableName.mid(temp + 1);
1272 executableName = executableName.trimmed();
1273 if(executableName.isEmpty())
1275 executableName = QLatin1String("x264_launcher.exe");
1281 qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
1282 qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR, QT_PACKAGEDATE_STR, QT_BUILD_KEY);
1283 if(_stricmp(qVersion(), QT_VERSION_STR))
1285 qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(executableName, QString::fromLatin1(QT_VERSION_STR), QString::fromLatin1(qVersion())).toLatin1().constData());
1288 if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY), Qt::CaseInsensitive))
1290 qFatal("%s", QApplication::tr("Executable '%1' was built for Qt '%2', but found Qt '%3'.").arg(executableName, QString::fromLatin1(QT_BUILD_KEY), QLibraryInfo::buildKey()).toLatin1().constData());
1294 qDebug("Using Qt v%s [%s], %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"));
1295 qDebug("Compiled with Qt v%s [%s]\n", QT_VERSION_STR, QT_PACKAGEDATE_STR);
1298 //Check the Windows version
1299 const x264_os_version_t &osVersionNo = x264_get_os_version();
1300 if(osVersionNo < x264_winver_winxp)
1302 qFatal("%s", QApplication::tr("Executable '%1' requires Windows XP or later.").arg(executableName).toLatin1().constData());
1305 //Supported Windows version?
1306 if(osVersionNo == x264_winver_winxp)
1308 qDebug("Running on Windows XP or Windows XP Media Center Edition.\n"); //x264_check_compatibility_mode("GetLargePageMinimum", executableName);
1310 else if(osVersionNo == x264_winver_xpx64)
1312 qDebug("Running on Windows Server 2003, Windows Server 2003 R2 or Windows XP x64.\n"); //x264_check_compatibility_mode("GetLocaleInfoEx", executableName);
1314 else if(osVersionNo == x264_winver_vista)
1316 qDebug("Running on Windows Vista or Windows Server 2008.\n"); //x264_check_compatibility_mode("CreateRemoteThreadEx", executableName*/);
1318 else if(osVersionNo == x264_winver_win70)
1320 qDebug("Running on Windows 7 or Windows Server 2008 R2.\n"); //x264_check_compatibility_mode("CreateFile2", executableName);
1322 else if(osVersionNo == x264_winver_win80)
1324 qDebug("Running on Windows 8 or Windows Server 2012.\n"); //x264_check_compatibility_mode("FindPackagesByPackageFamily", executableName);
1326 else if(osVersionNo == x264_winver_win81)
1328 qDebug("Running on Windows 8.1 or Windows Server 2012 R2.\n"); //x264_check_compatibility_mode(NULL, executableName);
1332 const QString message = QString().sprintf("Running on an unknown WindowsNT-based system (v%u.%u).", osVersionNo.versionMajor, osVersionNo.versionMinor);
1333 qWarning("%s\n", QUTF8(message));
1334 MessageBoxW(NULL, QWCHAR(message), L"Simple x264 Launcher", MB_OK | MB_TOPMOST | MB_ICONWARNING);
1337 //Check for compat mode
1338 if(osVersionNo.overrideFlag && (osVersionNo <= x264_winver_win81))
1340 qWarning("Windows compatibility mode detected!");
1341 if(!arguments.contains("--ignore-compat-mode", Qt::CaseInsensitive))
1343 qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(executableName).toLatin1().constData());
1349 if(x264_detect_wine())
1351 qWarning("It appears we are running under Wine, unexpected things might happen!\n");
1354 //Set text Codec for locale
1355 QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
1357 //Create Qt application instance
1358 QApplication *application = new QApplication(argc, argv);
1360 //Load plugins from application directory
1361 QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
1362 qDebug("Library Path:\n%s\n", QUTF8(QApplication::libraryPaths().first()));
1364 //Create Qt application instance and setup version info
1365 application->setApplicationName("Simple x264 Launcher");
1366 application->setApplicationVersion(QString().sprintf("%d.%02d", x264_version_major(), x264_version_minor()));
1367 application->setOrganizationName("LoRd_MuldeR");
1368 application->setOrganizationDomain("mulder.at.gg");
1369 application->setWindowIcon(QIcon(":/icons/movie.ico"));
1370 application->setEventFilter(x264_event_filter);
1372 //Check for supported image formats
1373 QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
1374 for(int i = 0; g_x264_imageformats[i]; i++)
1376 if(!supportedFormats.contains(g_x264_imageformats[i]))
1378 qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_x264_imageformats[i]);
1383 //Add default translations
1385 QWriteLocker writeLockTranslations(&g_x264_translation.lock);
1386 if(!g_x264_translation.files) g_x264_translation.files = new QMap<QString, QString>();
1387 if(!g_x264_translation.names) g_x264_translation.names = new QMap<QString, QString>();
1388 g_x264_translation.files->insert(X264_DEFAULT_LANGID, "");
1389 g_x264_translation.names->insert(X264_DEFAULT_LANGID, "English");
1390 writeLockTranslations.unlock();
1393 //Check for process elevation
1394 if((!x264_check_elevation()) && (!x264_detect_wine()))
1396 QMessageBox messageBox(QMessageBox::Warning, "Simple x264 Launcher", "<nobr>Simple x264 Launcher was started with 'elevated' rights, altough it does not need these rights.<br>Running an applications with unnecessary rights is a potential security risk!</nobr>", QMessageBox::NoButton, NULL, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowStaysOnTopHint);
1397 messageBox.addButton("Quit Program (Recommended)", QMessageBox::NoRole);
1398 messageBox.addButton("Ignore", QMessageBox::NoRole);
1399 if(messageBox.exec() == 0)
1405 //Update console icon, if a console is attached
1406 #if QT_VERSION < QT_VERSION_CHECK(5,0,0)
1407 if(g_x264_console_attached && (!x264_detect_wine()))
1409 typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
1410 QLibrary kernel32("kernel32.dll");
1413 SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon");
1414 QPixmap pixmap = QIcon(":/icons/movie.ico").pixmap(16, 16);
1415 if((SetConsoleIconPtr != NULL) && (!pixmap.isNull())) SetConsoleIconPtr(pixmap.toWinHICON());
1422 qt_initialized = true;
1427 * Suspend or resume process
1429 bool x264_suspendProcess(const QProcess *proc, const bool suspend)
1431 if(Q_PID pid = proc->pid())
1435 return (SuspendThread(pid->hThread) != ((DWORD) -1));
1439 return (ResumeThread(pid->hThread) != ((DWORD) -1));
1449 * Convert path to short/ANSI path
1451 QString x264_path2ansi(const QString &longPath)
1453 QString shortPath = longPath;
1455 DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
1459 wchar_t *buffer = new wchar_t[buffSize];
1460 DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
1462 if((result > 0) && (result < buffSize))
1464 shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer), result);
1475 * Set the process priority class for current process
1477 bool x264_change_process_priority(const int priority)
1479 return x264_change_process_priority(GetCurrentProcess(), priority);
1483 * Set the process priority class for specified process
1485 bool x264_change_process_priority(const QProcess *proc, const int priority)
1487 if(Q_PID qPid = proc->pid())
1489 return x264_change_process_priority(qPid->hProcess, priority);
1498 * Set the process priority class for specified process
1500 bool x264_change_process_priority(void *hProcess, const int priority)
1504 switch(qBound(-2, priority, 2))
1507 ok = (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS) == TRUE);
1510 if(!(ok = (SetPriorityClass(hProcess, ABOVE_NORMAL_PRIORITY_CLASS) == TRUE)))
1512 ok = (SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS) == TRUE);
1516 ok = (SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS) == TRUE);
1519 if(!(ok = (SetPriorityClass(hProcess, BELOW_NORMAL_PRIORITY_CLASS) == TRUE)))
1521 ok = (SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS) == TRUE);
1525 ok = (SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS) == TRUE);
1533 * Play a sound (from resources)
1535 bool x264_play_sound(const unsigned short uiSoundIdx, const bool bAsync, const wchar_t *alias)
1539 return PlaySound(alias, GetModuleHandle(NULL), (SND_ALIAS | (bAsync ? SND_ASYNC : SND_SYNC))) == TRUE;
1543 return PlaySound(MAKEINTRESOURCE(uiSoundIdx), GetModuleHandle(NULL), (SND_RESOURCE | (bAsync ? SND_ASYNC : SND_SYNC))) == TRUE;
1548 * Current process ID
1550 unsigned int x264_process_id(void)
1552 return GetCurrentProcessId();
1556 * Make a window blink (to draw user's attention)
1558 void x264_blink_window(QWidget *poWindow, unsigned int count, unsigned int delay)
1560 static QMutex blinkMutex;
1562 const double maxOpac = 1.0;
1563 const double minOpac = 0.3;
1564 const double delOpac = 0.1;
1566 if(!blinkMutex.tryLock())
1568 qWarning("Blinking is already in progress, skipping!");
1574 const int steps = static_cast<int>(ceil(maxOpac - minOpac) / delOpac);
1575 const int sleep = static_cast<int>(floor(static_cast<double>(delay) / static_cast<double>(steps)));
1576 const double opacity = poWindow->windowOpacity();
1578 for(unsigned int i = 0; i < count; i++)
1580 for(double x = maxOpac; x >= minOpac; x -= delOpac)
1582 poWindow->setWindowOpacity(x);
1583 QApplication::processEvents();
1587 for(double x = minOpac; x <= maxOpac; x += delOpac)
1589 poWindow->setWindowOpacity(x);
1590 QApplication::processEvents();
1595 poWindow->setWindowOpacity(opacity);
1596 QApplication::processEvents();
1597 blinkMutex.unlock();
1601 blinkMutex.unlock();
1602 qWarning("Exception error while blinking!");
1607 * Bring the specifed window to the front
1609 bool x264_bring_to_front(const QWidget *win)
1611 const bool ret = (SetForegroundWindow(win->winId()) == TRUE);
1612 SwitchToThisWindow(win->winId(), TRUE);
1617 * Check if file is a valid Win32/Win64 executable
1619 bool x264_is_executable(const QString &path)
1621 bool bIsExecutable = false;
1623 if(GetBinaryType(QWCHAR(QDir::toNativeSeparators(path)), &binaryType))
1625 bIsExecutable = (binaryType == SCS_32BIT_BINARY || binaryType == SCS_64BIT_BINARY);
1627 return bIsExecutable;
1631 * Read value from registry
1633 QString x264_query_reg_string(const bool bUser, const QString &path, const QString &name)
1635 QString result; HKEY hKey = NULL;
1636 if(RegOpenKey((bUser ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE), QWCHAR(path), &hKey) == ERROR_SUCCESS)
1638 const size_t DATA_LEN = 2048; wchar_t data[DATA_LEN];
1639 DWORD type = REG_NONE, size = sizeof(wchar_t) * DATA_LEN;
1640 if(RegQueryValueEx(hKey, QWCHAR(name), NULL, &type, ((BYTE*)&data[0]), &size) == ERROR_SUCCESS)
1642 if((type == REG_SZ) || (type == REG_EXPAND_SZ))
1644 result = WCHAR2QSTR(&data[0]);
1653 * Display the window's close button
1655 bool x264_enable_close_button(const QWidget *win, const bool bEnable)
1659 if(HMENU hMenu = GetSystemMenu(win->winId(), FALSE))
1661 ok = (EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | (bEnable ? MF_ENABLED : MF_GRAYED)) == TRUE);
1667 bool x264_beep(int beepType)
1671 case x264_beep_info: return MessageBeep(MB_ICONASTERISK) == TRUE; break;
1672 case x264_beep_warning: return MessageBeep(MB_ICONEXCLAMATION) == TRUE; break;
1673 case x264_beep_error: return MessageBeep(MB_ICONHAND) == TRUE; break;
1674 default: return false;
1679 * Shutdown the computer
1681 bool x264_shutdown_computer(const QString &message, const unsigned long timeout, const bool forceShutdown)
1683 HANDLE hToken = NULL;
1685 if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
1687 TOKEN_PRIVILEGES privileges;
1688 memset(&privileges, 0, sizeof(TOKEN_PRIVILEGES));
1689 privileges.PrivilegeCount = 1;
1690 privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1692 if(LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &privileges.Privileges[0].Luid))
1694 if(AdjustTokenPrivileges(hToken, FALSE, &privileges, NULL, NULL, NULL))
1696 const DWORD reason = SHTDN_REASON_MAJOR_APPLICATION | SHTDN_REASON_FLAG_PLANNED;
1697 return InitiateSystemShutdownEx(NULL, const_cast<wchar_t*>(QWCHAR(message)), timeout, forceShutdown ? TRUE : FALSE, FALSE, reason);
1706 * Check for debugger (detect routine)
1708 static __forceinline bool x264_check_for_debugger(void)
1712 CloseHandle((HANDLE)((DWORD_PTR)-3));
1724 return IsDebuggerPresent();
1730 * Check for debugger (thread proc)
1732 static unsigned int __stdcall x264_debug_thread_proc(LPVOID lpParameter)
1734 SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST);
1737 if(x264_check_for_debugger())
1739 x264_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
1747 * Check for debugger (startup routine)
1749 static HANDLE x264_debug_thread_init()
1751 if(x264_check_for_debugger())
1753 x264_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
1755 const uintptr_t h = _beginthreadex(NULL, 0, x264_debug_thread_proc, NULL, 0, NULL);
1756 return (HANDLE)(h^0xdeadbeef);
1760 * Fatal application exit
1762 #pragma intrinsic(_InterlockedExchange)
1763 void x264_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
1765 static volatile long bFatalFlag = 0L;
1767 if(_InterlockedExchange(&bFatalFlag, 1L) == 0L)
1769 if(GetCurrentThreadId() != g_main_thread_id)
1771 HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
1772 if(mainThread) TerminateThread(mainThread, ULONG_MAX);
1777 MessageBoxW(NULL, errorBoxMessage, L"Simple x264 Launcher - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
1780 FatalAppExit(0, exitMessage);
1784 TerminateProcess(GetCurrentProcess(), -1);
1790 * Entry point checks
1792 static DWORD x264_entry_check(void);
1793 static DWORD g_x264_entry_check_result = x264_entry_check();
1794 static DWORD g_x264_entry_check_flag = 0x789E09B2;
1795 static DWORD x264_entry_check(void)
1797 volatile DWORD retVal = 0xA199B5AF;
1798 if(g_x264_entry_check_flag != 0x8761F64D)
1800 x264_fatal_exit(L"Application initialization has failed, take care!");
1806 * Application entry point (runs before static initializers)
1810 int WinMainCRTStartup(void);
1812 int x264_entry_point(void)
1814 if((!X264_DEBUG) && x264_check_for_debugger())
1816 x264_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
1818 if(g_x264_entry_check_flag != 0x789E09B2)
1820 x264_fatal_exit(L"Application initialization has failed, take care!");
1823 //Zero *before* constructors are called
1824 X264_ZERO_MEMORY(g_x264_argv);
1825 X264_ZERO_MEMORY(g_x264_os_version);
1826 X264_ZERO_MEMORY(g_x264_portable);
1828 //Make sure we will pass the check
1829 g_x264_entry_check_flag = ~g_x264_entry_check_flag;
1831 //Now initialize the C Runtime library!
1832 return WinMainCRTStartup();
1837 * Initialize debug thread
1839 static const HANDLE g_debug_thread = X264_DEBUG ? NULL : x264_debug_thread_init();
1842 * Get number private bytes [debug only]
1844 size_t x264_dbg_private_bytes(void)
1847 PROCESS_MEMORY_COUNTERS_EX memoryCounters;
1848 memoryCounters.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
1849 GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS) &memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX));
1850 return memoryCounters.PrivateUsage;
1852 throw "Cannot call this function in a non-debug build!";
1857 * Finalization function
1859 void x264_finalization(void)
1861 //Destroy Qt application object
1862 QApplication *application = dynamic_cast<QApplication*>(QApplication::instance());
1863 X264_DELETE(application);
1865 //Free STDOUT and STDERR buffers
1866 if(g_x264_console_attached)
1868 if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cout.rdbuf()))
1870 std::cout.rdbuf(NULL);
1873 if(std::filebuf *tmp = dynamic_cast<std::filebuf*>(std::cerr.rdbuf()))
1875 std::cerr.rdbuf(NULL);