OSDN Git Service

Initial commit.
[x264-launcher/x264-launcher.git] / src / global.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "global.h"
23 #include "version.h"
24
25 //Qt includes
26 #include <QApplication>
27 #include <QMessageBox>
28 #include <QDir>
29 #include <QUuid>
30 #include <QMap>
31 #include <QDate>
32 #include <QIcon>
33 #include <QPlastiqueStyle>
34 #include <QImageReader>
35 #include <QSharedMemory>
36 #include <QSysInfo>
37 #include <QStringList>
38 #include <QSystemSemaphore>
39 #include <QMutex>
40 #include <QTextCodec>
41 #include <QLibrary>
42 #include <QRegExp>
43 #include <QResource>
44 #include <QTranslator>
45 #include <QEventLoop>
46 #include <QTimer>
47 #include <QLibraryInfo>
48 #include <QEvent>
49
50 //CRT includes
51 #include <fstream>
52 #include <io.h>
53 #include <fcntl.h>
54 #include <intrin.h>
55
56 //Debug only includes
57 #if X264_DEBUG
58 #include <Psapi.h>
59 #endif
60
61 //Global vars
62 static bool g_x264_console_attached = false;
63 static QMutex g_x264_message_mutex;
64 static const DWORD g_main_thread_id = GetCurrentThreadId();
65 static FILE *g_x264_log_file = NULL;
66 static QDate g_x264_version_date;
67
68 //Const
69 static const char *g_x264_months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
70 static const char *g_x264_imageformats[] = {"png", "jpg", "gif", "ico", "svg", NULL};
71
72 //Build version
73 static const struct
74 {
75         unsigned int ver_major;
76         unsigned int ver_minor;
77         const char* ver_date;
78         const char* ver_time;
79 }
80 g_x264_version =
81 {
82         VER_X264_MAJOR,
83         VER_X264_MINOR,
84         __DATE__,
85         __TIME__
86 };
87
88 //Compiler detection
89 //The following code was borrowed from MPC-HC project: http://mpc-hc.sf.net/
90 #if defined(__INTEL_COMPILER)
91         #if (__INTEL_COMPILER >= 1200)
92                 static const char *g_x264_version_compiler = "ICL 12.x";
93         #elif (__INTEL_COMPILER >= 1100)
94                 static const char *g_x264_version_compiler = = "ICL 11.x";
95         #elif (__INTEL_COMPILER >= 1000)
96                 static const char *g_x264_version_compiler = = "ICL 10.x";
97         #else
98                 #error Compiler is not supported!
99         #endif
100 #elif defined(_MSC_VER)
101         #if (_MSC_VER == 1600)
102                 #if (_MSC_FULL_VER >= 160040219)
103                         static const char *g_x264_version_compiler = "MSVC 2010-SP1";
104                 #else
105                         static const char *g_x264_version_compiler = "MSVC 2010";
106                 #endif
107         #elif (_MSC_VER == 1500)
108                 #if (_MSC_FULL_VER >= 150030729)
109                         static const char *g_x264_version_compiler = "MSVC 2008-SP1";
110                 #else
111                         static const char *g_x264_version_compiler = "MSVC 2008";
112                 #endif
113         #else
114                 #error Compiler is not supported!
115         #endif
116
117         // Note: /arch:SSE and /arch:SSE2 are only available for the x86 platform
118         #if !defined(_M_X64) && defined(_M_IX86_FP)
119                 #if (_M_IX86_FP == 1)
120                         x264_COMPILER_WARNING("SSE instruction set is enabled!")
121                 #elif (_M_IX86_FP == 2)
122                         x264_COMPILER_WARNING("SSE2 instruction set is enabled!")
123                 #endif
124         #endif
125 #else
126         #error Compiler is not supported!
127 #endif
128
129 //Architecture detection
130 #if defined(_M_X64)
131         static const char *g_x264_version_arch = "x64";
132 #elif defined(_M_IX86)
133         static const char *g_x264_version_arch = "x86";
134 #else
135         #error Architecture is not supported!
136 #endif
137
138 /*
139  * Global exception handler
140  */
141 LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo)
142 {
143         if(GetCurrentThreadId() != g_main_thread_id)
144         {
145                 HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
146                 if(mainThread) TerminateThread(mainThread, ULONG_MAX);
147         }
148
149         FatalAppExit(0, L"Unhandeled exception handler invoked, application will exit!");
150         TerminateProcess(GetCurrentProcess(), -1);
151         return LONG_MAX;
152 }
153
154 /*
155  * Invalid parameters handler
156  */
157 void x264_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t)
158 {
159         if(GetCurrentThreadId() != g_main_thread_id)
160         {
161                 HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
162                 if(mainThread) TerminateThread(mainThread, ULONG_MAX);
163         }
164
165         FatalAppExit(0, L"Invalid parameter handler invoked, application will exit!");
166         TerminateProcess(GetCurrentProcess(), -1);
167 }
168
169 /*
170  * Change console text color
171  */
172 static void x264_console_color(FILE* file, WORD attributes)
173 {
174         const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
175         if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
176         {
177                 SetConsoleTextAttribute(hConsole, attributes);
178         }
179 }
180
181 /*
182  * Qt message handler
183  */
184 void x264_message_handler(QtMsgType type, const char *msg)
185 {
186         static const char *GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
187         
188         QMutexLocker lock(&g_x264_message_mutex);
189
190         if(g_x264_log_file)
191         {
192                 static char prefix[] = "DWCF";
193                 int index = qBound(0, static_cast<int>(type), 3);
194                 unsigned int timestamp = static_cast<unsigned int>(_time64(NULL) % 3600I64);
195                 QString str = QString::fromUtf8(msg).trimmed().replace('\n', '\t');
196                 fprintf(g_x264_log_file, "[%c][%04u] %s\r\n", prefix[index], timestamp, str.toUtf8().constData());
197                 fflush(g_x264_log_file);
198         }
199
200         if(g_x264_console_attached)
201         {
202                 UINT oldOutputCP = GetConsoleOutputCP();
203                 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
204
205                 switch(type)
206                 {
207                 case QtCriticalMsg:
208                 case QtFatalMsg:
209                         fflush(stdout);
210                         fflush(stderr);
211                         x264_console_color(stderr, FOREGROUND_RED | FOREGROUND_INTENSITY);
212                         fprintf(stderr, GURU_MEDITATION);
213                         fprintf(stderr, "%s\n", msg);
214                         fflush(stderr);
215                         break;
216                 case QtWarningMsg:
217                         x264_console_color(stderr, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
218                         fprintf(stderr, "%s\n", msg);
219                         fflush(stderr);
220                         break;
221                 default:
222                         x264_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
223                         fprintf(stderr, "%s\n", msg);
224                         fflush(stderr);
225                         break;
226                 }
227         
228                 x264_console_color(stderr, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
229                 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(oldOutputCP);
230         }
231         else
232         {
233                 QString temp("[LameXP][%1] %2");
234                 
235                 switch(type)
236                 {
237                 case QtCriticalMsg:
238                 case QtFatalMsg:
239                         temp = temp.arg("C", QString::fromUtf8(msg));
240                         break;
241                 case QtWarningMsg:
242                         temp = temp.arg("W", QString::fromUtf8(msg));
243                         break;
244                 default:
245                         temp = temp.arg("I", QString::fromUtf8(msg));
246                         break;
247                 }
248
249                 temp.replace("\n", "\t").append("\n");
250                 OutputDebugStringA(temp.toLatin1().constData());
251         }
252
253         if(type == QtCriticalMsg || type == QtFatalMsg)
254         {
255                 lock.unlock();
256                 MessageBoxW(NULL, QWCHAR(QString::fromUtf8(msg)), L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
257                 FatalAppExit(0, L"The application has encountered a critical error and will exit now!");
258                 TerminateProcess(GetCurrentProcess(), -1);
259         }
260 }
261
262 /*
263  * Initialize the console
264  */
265 void x264_init_console(int argc, char* argv[])
266 {
267         bool enableConsole = true; //x264_version_demo();
268
269         if(_environ)
270         {
271                 wchar_t *logfile = NULL;
272                 size_t logfile_len = 0;
273                 if(!_wdupenv_s(&logfile, &logfile_len, L"X264_LAUNCHER_LOGFILE"))
274                 {
275                         if(logfile && (logfile_len > 0))
276                         {
277                                 FILE *temp = NULL;
278                                 if(!_wfopen_s(&temp, logfile, L"wb"))
279                                 {
280                                         fprintf(temp, "%c%c%c", 0xEF, 0xBB, 0xBF);
281                                         g_x264_log_file = temp;
282                                 }
283                                 free(logfile);
284                         }
285                 }
286         }
287
288         if(!X264_DEBUG)
289         {
290                 for(int i = 0; i < argc; i++)
291                 {
292                         if(!_stricmp(argv[i], "--console"))
293                         {
294                                 enableConsole = true;
295                         }
296                         else if(!_stricmp(argv[i], "--no-console"))
297                         {
298                                 enableConsole = false;
299                         }
300                 }
301         }
302
303         if(enableConsole)
304         {
305                 if(!g_x264_console_attached)
306                 {
307                         if(AllocConsole() != FALSE)
308                         {
309                                 SetConsoleCtrlHandler(NULL, TRUE);
310                                 SetConsoleTitle(L"Simple x264 Launcher | Debug Console");
311                                 SetConsoleOutputCP(CP_UTF8);
312                                 g_x264_console_attached = true;
313                         }
314                 }
315                 
316                 if(g_x264_console_attached)
317                 {
318                         //-------------------------------------------------------------------
319                         //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
320                         //-------------------------------------------------------------------
321                         const int flags = _O_WRONLY | _O_U8TEXT;
322                         int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
323                         int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE), flags);
324                         FILE *hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
325                         FILE *hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
326                         if(hfStdOut) { *stdout = *hfStdOut; std::cout.rdbuf(new std::filebuf(hfStdOut)); }
327                         if(hfStdErr) { *stderr = *hfStdErr; std::cerr.rdbuf(new std::filebuf(hfStdErr)); }
328                 }
329
330                 HWND hwndConsole = GetConsoleWindow();
331
332                 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
333                 {
334                         HMENU hMenu = GetSystemMenu(hwndConsole, 0);
335                         EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
336                         RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
337
338                         SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
339                         SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
340                         SetWindowPos(hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
341                 }
342         }
343 }
344
345 /*
346  * Version info
347  */
348 unsigned int x264_version_major(void)
349 {
350         return g_x264_version.ver_major;
351 }
352
353 unsigned int x264_version_minor(void)
354 {
355         return g_x264_version.ver_minor;
356 }
357
358 const char *x264_version_compiler(void)
359 {
360         return g_x264_version_compiler;
361 }
362
363 const char *x264_version_arch(void)
364 {
365         return g_x264_version_arch;
366 }
367
368 /*
369  * Get build date date
370  */
371 const QDate &x264_version_date(void)
372 {
373         if(!g_x264_version_date.isValid())
374         {
375                 int date[3] = {0, 0, 0}; char temp[12] = {'\0'};
376                 strncpy_s(temp, 12, g_x264_version.ver_date, _TRUNCATE);
377
378                 if(strlen(temp) == 11)
379                 {
380                         temp[3] = temp[6] = '\0';
381                         date[2] = atoi(&temp[4]);
382                         date[0] = atoi(&temp[7]);
383                         
384                         for(int j = 0; j < 12; j++)
385                         {
386                                 if(!_strcmpi(&temp[0], g_x264_months[j]))
387                                 {
388                                         date[1] = j+1;
389                                         break;
390                                 }
391                         }
392
393                         g_x264_version_date = QDate(date[0], date[1], date[2]);
394                 }
395
396                 if(!g_x264_version_date.isValid())
397                 {
398                         qFatal("Internal error: Date format could not be recognized!");
399                 }
400         }
401
402         return g_x264_version_date;
403 }
404
405 const char *x264_version_time(void)
406 {
407         return g_x264_version.ver_time;
408 }
409
410 /*
411  * Detect CPU features
412  */
413 x264_cpu_t x264_detect_cpu_features(int argc, char **argv)
414 {
415         typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process);
416         typedef VOID (WINAPI *GetNativeSystemInfoFun)(__out LPSYSTEM_INFO lpSystemInfo);
417         
418         static IsWow64ProcessFun IsWow64ProcessPtr = NULL;
419         static GetNativeSystemInfoFun GetNativeSystemInfoPtr = NULL;
420
421         x264_cpu_t features;
422         SYSTEM_INFO systemInfo;
423         int CPUInfo[4] = {-1};
424         char CPUIdentificationString[0x40];
425         char CPUBrandString[0x40];
426
427         memset(&features, 0, sizeof(x264_cpu_t));
428         memset(&systemInfo, 0, sizeof(SYSTEM_INFO));
429         memset(CPUIdentificationString, 0, sizeof(CPUIdentificationString));
430         memset(CPUBrandString, 0, sizeof(CPUBrandString));
431         
432         __cpuid(CPUInfo, 0);
433         memcpy(CPUIdentificationString, &CPUInfo[1], sizeof(int));
434         memcpy(CPUIdentificationString + 4, &CPUInfo[3], sizeof(int));
435         memcpy(CPUIdentificationString + 8, &CPUInfo[2], sizeof(int));
436         features.intel = (_stricmp(CPUIdentificationString, "GenuineIntel") == 0);
437         strncpy_s(features.vendor, 0x40, CPUIdentificationString, _TRUNCATE);
438
439         if(CPUInfo[0] >= 1)
440         {
441                 __cpuid(CPUInfo, 1);
442                 features.mmx = (CPUInfo[3] & 0x800000) || false;
443                 features.sse = (CPUInfo[3] & 0x2000000) || false;
444                 features.sse2 = (CPUInfo[3] & 0x4000000) || false;
445                 features.ssse3 = (CPUInfo[2] & 0x200) || false;
446                 features.sse3 = (CPUInfo[2] & 0x1) || false;
447                 features.ssse3 = (CPUInfo[2] & 0x200) || false;
448                 features.stepping = CPUInfo[0] & 0xf;
449                 features.model = ((CPUInfo[0] >> 4) & 0xf) + (((CPUInfo[0] >> 16) & 0xf) << 4);
450                 features.family = ((CPUInfo[0] >> 8) & 0xf) + ((CPUInfo[0] >> 20) & 0xff);
451         }
452
453         __cpuid(CPUInfo, 0x80000000);
454         int nExIds = qMax<int>(qMin<int>(CPUInfo[0], 0x80000004), 0x80000000);
455
456         for(int i = 0x80000002; i <= nExIds; ++i)
457         {
458                 __cpuid(CPUInfo, i);
459                 switch(i)
460                 {
461                 case 0x80000002:
462                         memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
463                         break;
464                 case 0x80000003:
465                         memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
466                         break;
467                 case 0x80000004:
468                         memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
469                         break;
470                 }
471         }
472
473         strncpy_s(features.brand, 0x40, CPUBrandString, _TRUNCATE);
474
475         if(strlen(features.brand) < 1) strncpy_s(features.brand, 0x40, "Unknown", _TRUNCATE);
476         if(strlen(features.vendor) < 1) strncpy_s(features.vendor, 0x40, "Unknown", _TRUNCATE);
477
478 #if !defined(_M_X64 ) && !defined(_M_IA64)
479         if(!IsWow64ProcessPtr || !GetNativeSystemInfoPtr)
480         {
481                 QLibrary Kernel32Lib("kernel32.dll");
482                 IsWow64ProcessPtr = (IsWow64ProcessFun) Kernel32Lib.resolve("IsWow64Process");
483                 GetNativeSystemInfoPtr = (GetNativeSystemInfoFun) Kernel32Lib.resolve("GetNativeSystemInfo");
484         }
485         if(IsWow64ProcessPtr)
486         {
487                 BOOL x64 = FALSE;
488                 if(IsWow64ProcessPtr(GetCurrentProcess(), &x64))
489                 {
490                         features.x64 = x64;
491                 }
492         }
493         if(GetNativeSystemInfoPtr)
494         {
495                 GetNativeSystemInfoPtr(&systemInfo);
496         }
497         else
498         {
499                 GetSystemInfo(&systemInfo);
500         }
501         features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
502 #else
503         GetNativeSystemInfo(&systemInfo);
504         features.count = systemInfo.dwNumberOfProcessors;
505         features.x64 = true;
506 #endif
507
508         if((argv != NULL) && (argc > 0))
509         {
510                 bool flag = false;
511                 for(int i = 0; i < argc; i++)
512                 {
513                         if(!_stricmp("--force-cpu-no-64bit", argv[i])) { flag = true; features.x64 = false; }
514                         if(!_stricmp("--force-cpu-no-sse", argv[i])) { flag = true; features.sse = features.sse2 = features.sse3 = features.ssse3 = false; }
515                         if(!_stricmp("--force-cpu-no-intel", argv[i])) { flag = true; features.intel = false; }
516                 }
517                 if(flag) qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
518         }
519
520         return features;
521 }
522
523 /*
524  * Get the native operating system version
525  */
526 DWORD x264_get_os_version(void)
527 {
528         OSVERSIONINFO osVerInfo;
529         memset(&osVerInfo, 0, sizeof(OSVERSIONINFO));
530         osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
531         DWORD version = 0;
532         
533         if(GetVersionEx(&osVerInfo) == TRUE)
534         {
535                 if(osVerInfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
536                 {
537                         throw "Ouuups: Not running under Windows NT. This is not supposed to happen!";
538                 }
539                 version = (DWORD)((osVerInfo.dwMajorVersion << 16) | (osVerInfo.dwMinorVersion & 0xffff));
540         }
541
542         return version;
543 }
544
545 /*
546  * Check for compatibility mode
547  */
548 static bool x264_check_compatibility_mode(const char *exportName, const char *executableName)
549 {
550         QLibrary kernel32("kernel32.dll");
551
552         if(exportName != NULL)
553         {
554                 if(kernel32.resolve(exportName) != NULL)
555                 {
556                         qWarning("Function '%s' exported from 'kernel32.dll' -> Windows compatibility mode!", exportName);
557                         qFatal("%s", QApplication::tr("Executable '%1' doesn't support Windows compatibility mode.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
558                         return false;
559                 }
560         }
561
562         return true;
563 }
564
565 /*
566  * Check for process elevation
567  */
568 static bool x264_check_elevation(void)
569 {
570         typedef enum { x264_token_elevationType_class = 18, x264_token_elevation_class = 20 } X264_TOKEN_INFORMATION_CLASS;
571         typedef enum { x264_elevationType_default = 1, x264_elevationType_full, x264_elevationType_limited } X264_TOKEN_ELEVATION_TYPE;
572
573         HANDLE hToken = NULL;
574         bool bIsProcessElevated = false;
575         
576         if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken))
577         {
578                 X264_TOKEN_ELEVATION_TYPE tokenElevationType;
579                 DWORD returnLength;
580                 if(GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS) x264_token_elevationType_class, &tokenElevationType, sizeof(X264_TOKEN_ELEVATION_TYPE), &returnLength))
581                 {
582                         if(returnLength == sizeof(X264_TOKEN_ELEVATION_TYPE))
583                         {
584                                 switch(tokenElevationType)
585                                 {
586                                 case x264_elevationType_default:
587                                         qDebug("Process token elevation type: Default -> UAC is disabled.\n");
588                                         break;
589                                 case x264_elevationType_full:
590                                         qWarning("Process token elevation type: Full -> potential security risk!\n");
591                                         bIsProcessElevated = true;
592                                         break;
593                                 case x264_elevationType_limited:
594                                         qDebug("Process token elevation type: Limited -> not elevated.\n");
595                                         break;
596                                 }
597                         }
598                 }
599                 CloseHandle(hToken);
600         }
601         else
602         {
603                 qWarning("Failed to open process token!");
604         }
605
606         return !bIsProcessElevated;
607 }
608
609 /*
610  * Initialize Qt framework
611  */
612 bool x264_init_qt(int argc, char* argv[])
613 {
614         static bool qt_initialized = false;
615         bool isWine = false;
616         typedef BOOL (WINAPI *SetDllDirectoryProc)(WCHAR *lpPathName);
617
618         //Don't initialized again, if done already
619         if(qt_initialized)
620         {
621                 return true;
622         }
623         
624         //Secure DLL loading
625         QLibrary kernel32("kernel32.dll");
626         if(kernel32.load())
627         {
628                 SetDllDirectoryProc pSetDllDirectory = (SetDllDirectoryProc) kernel32.resolve("SetDllDirectoryW");
629                 if(pSetDllDirectory != NULL) pSetDllDirectory(L"");
630                 kernel32.unload();
631         }
632
633         //Extract executable name from argv[] array
634         char *executableName = argv[0];
635         while(char *temp = strpbrk(executableName, "\\/:?"))
636         {
637                 executableName = temp + 1;
638         }
639
640         //Check Qt version
641         qDebug("Using Qt v%s [%s], %s, %s", qVersion(), QLibraryInfo::buildDate().toString(Qt::ISODate).toLatin1().constData(), (qSharedBuild() ? "DLL" : "Static"), QLibraryInfo::buildKey().toLatin1().constData());
642         qDebug("Compiled with Qt v%s [%s], %s\n", QT_VERSION_STR, QT_PACKAGEDATE_STR, QT_BUILD_KEY);
643         if(_stricmp(qVersion(), QT_VERSION_STR))
644         {
645                 qFatal("%s", QApplication::tr("Executable '%1' requires Qt v%2, but found Qt v%3.").arg(QString::fromLatin1(executableName), QString::fromLatin1(QT_VERSION_STR), QString::fromLatin1(qVersion())).toLatin1().constData());
646                 return false;
647         }
648         if(QLibraryInfo::buildKey().compare(QString::fromLatin1(QT_BUILD_KEY), Qt::CaseInsensitive))
649         {
650                 qFatal("%s", QApplication::tr("Executable '%1' was built for Qt '%2', but found Qt '%3'.").arg(QString::fromLatin1(executableName), QString::fromLatin1(QT_BUILD_KEY), QLibraryInfo::buildKey()).toLatin1().constData());
651                 return false;
652         }
653
654         //Check the Windows version
655         switch(QSysInfo::windowsVersion() & QSysInfo::WV_NT_based)
656         {
657         case 0:
658         case QSysInfo::WV_NT:
659                 qFatal("%s", QApplication::tr("Executable '%1' requires Windows 2000 or later.").arg(QString::fromLatin1(executableName)).toLatin1().constData());
660                 break;
661         case QSysInfo::WV_2000:
662                 qDebug("Running on Windows 2000 (not officially supported!).\n");
663                 x264_check_compatibility_mode("GetNativeSystemInfo", executableName);
664                 break;
665         case QSysInfo::WV_XP:
666                 qDebug("Running on Windows XP.\n");
667                 x264_check_compatibility_mode("GetLargePageMinimum", executableName);
668                 break;
669         case QSysInfo::WV_2003:
670                 qDebug("Running on Windows Server 2003 or Windows XP x64-Edition.\n");
671                 x264_check_compatibility_mode("GetLocaleInfoEx", executableName);
672                 break;
673         case QSysInfo::WV_VISTA:
674                 qDebug("Running on Windows Vista or Windows Server 2008.\n");
675                 x264_check_compatibility_mode("CreateRemoteThreadEx", executableName);
676                 break;
677         case QSysInfo::WV_WINDOWS7:
678                 qDebug("Running on Windows 7 or Windows Server 2008 R2.\n");
679                 x264_check_compatibility_mode(NULL, executableName);
680                 break;
681         default:
682                 {
683                         DWORD osVersionNo = x264_get_os_version();
684                         qWarning("Running on an unknown/untested WinNT-based OS (v%u.%u).\n", HIWORD(osVersionNo), LOWORD(osVersionNo));
685                 }
686                 break;
687         }
688
689         //Check for Wine
690         QLibrary ntdll("ntdll.dll");
691         if(ntdll.load())
692         {
693                 if(ntdll.resolve("wine_nt_to_unix_file_name") != NULL) isWine = true;
694                 if(ntdll.resolve("wine_get_version") != NULL) isWine = true;
695                 if(isWine) qWarning("It appears we are running under Wine, unexpected things might happen!\n");
696                 ntdll.unload();
697         }
698
699         //Create Qt application instance and setup version info
700         QApplication *application = new QApplication(argc, argv);
701         application->setApplicationName("LameXP - Audio Encoder Front-End");
702         application->setApplicationVersion(QString().sprintf("%d.%02d", x264_version_major(), x264_version_minor())); 
703         application->setOrganizationName("LoRd_MuldeR");
704         application->setOrganizationDomain("mulder.at.gg");
705         application->setWindowIcon(QIcon(":/icons/movie.ico"));
706         
707         //application->setEventFilter(x264_event_filter);
708
709         //Set text Codec for locale
710         QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
711
712         //Load plugins from application directory
713         QCoreApplication::setLibraryPaths(QStringList() << QApplication::applicationDirPath());
714         qDebug("Library Path:\n%s\n", QApplication::libraryPaths().first().toUtf8().constData());
715
716         //Check for supported image formats
717         QList<QByteArray> supportedFormats = QImageReader::supportedImageFormats();
718         for(int i = 0; g_x264_imageformats[i]; i++)
719         {
720                 if(!supportedFormats.contains(g_x264_imageformats[i]))
721                 {
722                         qFatal("Qt initialization error: QImageIOHandler for '%s' missing!", g_x264_imageformats[i]);
723                         return false;
724                 }
725         }
726
727         //Add default translations
728         // g_x264_translation.files.insert(x264_DEFAULT_LANGID, "");
729         // g_x264_translation.names.insert(x264_DEFAULT_LANGID, "English");
730
731         //Check for process elevation
732         if(!x264_check_elevation())
733         {
734                 if(QMessageBox::warning(NULL, "LameXP", "<nobr>LameXP was started with elevated rights. This is a potential security risk!</nobr>", "Quit Program (Recommended)", "Ignore") == 0)
735                 {
736                         return false;
737                 }
738         }
739
740         //Update console icon, if a console is attached
741         if(g_x264_console_attached && !isWine)
742         {
743                 typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
744                 QLibrary kernel32("kernel32.dll");
745                 if(kernel32.load())
746                 {
747                         SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon");
748                         if(SetConsoleIconPtr != NULL) SetConsoleIconPtr(QIcon(":/icons/movie.ico").pixmap(16, 16).toWinHICON());
749                         kernel32.unload();
750                 }
751         }
752
753         //Done
754         qt_initialized = true;
755         return true;
756 }
757
758 /*
759  * Check for debugger (detect routine)
760  */
761 static bool x264_check_for_debugger(void)
762 {
763         __try 
764         {
765                 DebugBreak();
766         }
767         __except(GetExceptionCode() == EXCEPTION_BREAKPOINT ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
768         {
769                 return false;
770         }
771         return true;
772 }
773
774 /*
775  * Check for debugger (thread proc)
776  */
777 static void WINAPI x264_debug_thread_proc(__in LPVOID lpParameter)
778 {
779         while(!(IsDebuggerPresent() || x264_check_for_debugger()))
780         {
781                 Sleep(333);
782         }
783         TerminateProcess(GetCurrentProcess(), -1);
784 }
785
786 /*
787  * Check for debugger (startup routine)
788  */
789 static HANDLE x264_debug_thread_init(void)
790 {
791         if(IsDebuggerPresent() || x264_check_for_debugger())
792         {
793                 FatalAppExit(0, L"Not a debug build. Please unload debugger and try again!");
794                 TerminateProcess(GetCurrentProcess(), -1);
795         }
796
797         return CreateThread(NULL, NULL, reinterpret_cast<LPTHREAD_START_ROUTINE>(&x264_debug_thread_proc), NULL, NULL, NULL);
798 }
799
800 /*
801  * Initialize debug thread
802  */
803 static const HANDLE g_debug_thread = X264_DEBUG ? NULL : x264_debug_thread_init();
804
805 /*
806  * Get number private bytes [debug only]
807  */
808 SIZE_T x264_dbg_private_bytes(void)
809 {
810 #if X264_DEBUG
811         PROCESS_MEMORY_COUNTERS_EX memoryCounters;
812         memoryCounters.cb = sizeof(PROCESS_MEMORY_COUNTERS_EX);
813         GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS) &memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX));
814         return memoryCounters.PrivateUsage;
815 #else
816         throw "Cannot call this function in a non-debug build!";
817 #endif //X264_DEBUG
818 }
819
820 /*
821  * Finalization function
822  */
823 void x264_finalization(void)
824 {
825         /* NOP */
826 }