1 /*****************************************************************************
2 * osdep.c: platform-specific code
3 *****************************************************************************
4 * Copyright (C) 2003-2013 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
7 * Laurent Aimar <fenrir@via.ecp.fr>
8 * Henrik Gramner <henrik@gramner.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 * This program is also available under a commercial proprietary license.
25 * For more information, contact us at licensing@x264.com.
26 *****************************************************************************/
36 #include <sys/types.h>
37 #include <sys/timeb.h>
44 /* this is a global in pthread-win32 to indicate if it has been initialized or not */
45 extern int ptw32_processInitialized;
48 int64_t x264_mdate( void )
53 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
55 struct timeval tv_date;
56 gettimeofday( &tv_date, NULL );
57 return (int64_t)tv_date.tv_sec * 1000000 + (int64_t)tv_date.tv_usec;
61 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
62 /* state of the threading library being initialized */
63 static volatile LONG x264_threading_is_init = 0;
65 static void x264_threading_destroy( void )
68 pthread_win32_thread_detach_np();
69 pthread_win32_process_detach_np();
71 x264_win32_threading_destroy();
75 int x264_threading_init( void )
77 /* if already init, then do nothing */
78 if( InterlockedCompareExchange( &x264_threading_is_init, 1, 0 ) )
81 /* if static pthread-win32 is already initialized, then do nothing */
82 if( ptw32_processInitialized )
84 if( !pthread_win32_process_attach_np() )
87 if( x264_win32_threading_init() )
90 /* register cleanup to run at process termination */
91 atexit( x264_threading_destroy );
98 #ifdef __INTEL_COMPILER
99 /* Agner's patch to Intel's CPU dispatcher from pages 131-132 of
100 * http://agner.org/optimize/optimizing_cpp.pdf (2011-01-30)
101 * adapted to x264's cpu schema. */
103 // Global variable indicating cpu
104 int __intel_cpu_indicator = 0;
105 // CPU dispatcher function
106 void x264_intel_cpu_indicator_init( void )
108 unsigned int cpu = x264_cpu_detect();
109 if( cpu&X264_CPU_AVX )
110 __intel_cpu_indicator = 0x20000;
111 else if( cpu&X264_CPU_SSE42 )
112 __intel_cpu_indicator = 0x8000;
113 else if( cpu&X264_CPU_SSE4 )
114 __intel_cpu_indicator = 0x2000;
115 else if( cpu&X264_CPU_SSSE3 )
116 __intel_cpu_indicator = 0x1000;
117 else if( cpu&X264_CPU_SSE3 )
118 __intel_cpu_indicator = 0x800;
119 else if( cpu&X264_CPU_SSE2 && !(cpu&X264_CPU_SSE2_IS_SLOW) )
120 __intel_cpu_indicator = 0x200;
121 else if( cpu&X264_CPU_SSE )
122 __intel_cpu_indicator = 0x80;
123 else if( cpu&X264_CPU_MMX2 )
124 __intel_cpu_indicator = 8;
126 __intel_cpu_indicator = 1;
129 /* __intel_cpu_indicator_init appears to have a non-standard calling convention that
130 * assumes certain registers aren't preserved, so we'll route it through a function
131 * that backs up all the registers. */
132 void __intel_cpu_indicator_init( void )
134 x264_safe_intel_cpu_indicator_init();
137 void x264_intel_cpu_indicator_init( void )
143 /* Functions for dealing with Unicode on Windows. */
144 FILE *x264_fopen( const char *filename, const char *mode )
146 wchar_t filename_utf16[MAX_PATH];
147 wchar_t mode_utf16[16];
148 if( utf8_to_utf16( filename, filename_utf16 ) && utf8_to_utf16( mode, mode_utf16 ) )
149 return _wfopen( filename_utf16, mode_utf16 );
153 int x264_rename( const char *oldname, const char *newname )
155 wchar_t oldname_utf16[MAX_PATH];
156 wchar_t newname_utf16[MAX_PATH];
157 if( utf8_to_utf16( oldname, oldname_utf16 ) && utf8_to_utf16( newname, newname_utf16 ) )
159 /* POSIX says that rename() removes the destination, but Win32 doesn't. */
160 _wunlink( newname_utf16 );
161 return _wrename( oldname_utf16, newname_utf16 );
166 int x264_stat( const char *path, x264_struct_stat *buf )
168 wchar_t path_utf16[MAX_PATH];
169 if( utf8_to_utf16( path, path_utf16 ) )
170 return _wstati64( path_utf16, buf );
174 int x264_vfprintf( FILE *stream, const char *format, va_list arg )
176 HANDLE console = NULL;
179 if( stream == stdout )
180 console = GetStdHandle( STD_OUTPUT_HANDLE );
181 else if( stream == stderr )
182 console = GetStdHandle( STD_ERROR_HANDLE );
184 /* Only attempt to convert to UTF-16 when writing to a non-redirected console screen buffer. */
185 if( GetConsoleMode( console, &mode ) )
188 wchar_t buf_utf16[4096];
190 int length = vsnprintf( buf, sizeof(buf), format, arg );
191 if( length > 0 && length < sizeof(buf) )
193 /* WriteConsoleW is the most reliable way to output Unicode to a console. */
194 int length_utf16 = MultiByteToWideChar( CP_UTF8, 0, buf, length, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t) );
196 WriteConsoleW( console, buf_utf16, length_utf16, &written, NULL );
200 return vfprintf( stream, format, arg );