1 /*****************************************************************************
2  * osdep.h: platform-specific code
3  *****************************************************************************
4  * Copyright (C) 2007-2021 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *          Henrik Gramner <henrik@gramner.com>
9  *
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.
14  *
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.
19  *
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.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27 
28 #ifndef X264_OSDEP_H
29 #define X264_OSDEP_H
30 
31 #define _LARGEFILE_SOURCE 1
32 #define _FILE_OFFSET_BITS 64
33 #include <stdio.h>
34 #include <sys/stat.h>
35 #include <inttypes.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 
39 #include "config.h"
40 
41 #ifdef __INTEL_COMPILER
42 #include <mathimf.h>
43 #else
44 #include <math.h>
45 #endif
46 
47 #ifdef _WIN32
48 #include <windows.h>
49 #include <io.h>
50 #endif
51 
52 #include "x264.h"
53 
54 #if !HAVE_LOG2F
55 #define log2f(x) (logf(x)/0.693147180559945f)
56 #define log2(x) (log(x)/0.693147180559945)
57 #endif
58 
59 #ifdef _MSC_VER
60 #define inline __inline
61 #define strcasecmp _stricmp
62 #define strncasecmp _strnicmp
63 #define strtok_r strtok_s
64 #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
65 #else
66 #include <strings.h>
67 #endif
68 
69 #if !defined(va_copy) && defined(__INTEL_COMPILER)
70 #define va_copy(dst, src) ((dst) = (src))
71 #endif
72 
73 #if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
74 #define isfinite finite
75 #endif
76 
77 #if !HAVE_STRTOK_R && !defined(strtok_r)
78 #define strtok_r(str,delim,save) strtok(str,delim)
79 #endif
80 
81 #if defined(_MSC_VER) && _MSC_VER < 1900
82 /* MSVC pre-VS2015 has broken snprintf/vsnprintf implementations which are incompatible with C99. */
x264_vsnprintf(char * s,size_t n,const char * fmt,va_list arg)83 static inline int x264_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
84 {
85     int length = -1;
86 
87     if( n )
88     {
89         va_list arg2;
90         va_copy( arg2, arg );
91         length = _vsnprintf( s, n, fmt, arg2 );
92         va_end( arg2 );
93 
94         /* _(v)snprintf adds a null-terminator only if the length is less than the buffer size. */
95         if( length < 0 || length >= n )
96             s[n-1] = '\0';
97     }
98 
99     /* _(v)snprintf returns a negative number if the length is greater than the buffer size. */
100     if( length < 0 )
101         return _vscprintf( fmt, arg );
102 
103     return length;
104 }
105 
x264_snprintf(char * s,size_t n,const char * fmt,...)106 static inline int x264_snprintf( char *s, size_t n, const char *fmt, ... )
107 {
108     va_list arg;
109     va_start( arg, fmt );
110     int length = x264_vsnprintf( s, n, fmt, arg );
111     va_end( arg );
112     return length;
113 }
114 
115 #define snprintf  x264_snprintf
116 #define vsnprintf x264_vsnprintf
117 #endif
118 
119 #ifdef _WIN32
120 /* Functions for dealing with Unicode on Windows. */
x264_utf8_to_utf16(const char * utf8)121 static inline wchar_t *x264_utf8_to_utf16( const char *utf8 )
122 {
123     int len = MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, NULL, 0 );
124     if( len )
125     {
126         wchar_t *utf16 = malloc( len * sizeof( wchar_t ) );
127         if( utf16 )
128         {
129             if( MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, utf16, len ) )
130                 return utf16;
131             free( utf16 );
132         }
133     }
134     return NULL;
135 }
136 
x264_utf8_to_utf16_try_buf(const char * utf8,wchar_t * buf_utf16,int buf_len)137 static inline wchar_t *x264_utf8_to_utf16_try_buf( const char *utf8, wchar_t *buf_utf16, int buf_len ) {
138     if( MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, buf_utf16, buf_len ) )
139         return buf_utf16;
140     return x264_utf8_to_utf16( utf8 );
141 }
142 
143 #define x264_fopen( filename, mode ) x264_fopen_internal( filename, L##mode )
x264_fopen_internal(const char * filename,const wchar_t * mode_utf16)144 static inline FILE *x264_fopen_internal( const char *filename, const wchar_t *mode_utf16 )
145 {
146     FILE *f = NULL;
147     wchar_t filename_buf[MAX_PATH];
148     wchar_t *filename_utf16 = x264_utf8_to_utf16_try_buf( filename, filename_buf, MAX_PATH );
149     if( filename_utf16 )
150     {
151         f = _wfopen( filename_utf16, mode_utf16 );
152         if( filename_utf16 != filename_buf )
153             free( filename_utf16 );
154     }
155     return f;
156 }
157 
x264_rename(const char * oldname,const char * newname)158 static inline int x264_rename( const char *oldname, const char *newname )
159 {
160     int ret = -1;
161     wchar_t oldname_buf[MAX_PATH];
162     wchar_t *oldname_utf16 = x264_utf8_to_utf16_try_buf( oldname, oldname_buf, MAX_PATH );
163     if( oldname_utf16 )
164     {
165         wchar_t newname_buf[MAX_PATH];
166         wchar_t *newname_utf16 = x264_utf8_to_utf16_try_buf( newname, newname_buf, MAX_PATH );
167         if( newname_utf16 )
168         {
169             /* POSIX says that rename() removes the destination, but Win32 doesn't. */
170             _wunlink( newname_utf16 );
171             ret = _wrename( oldname_utf16, newname_utf16 );
172             if( newname_utf16 != newname_buf )
173                 free( newname_utf16 );
174         }
175         if( oldname_utf16 != oldname_buf )
176             free( oldname_utf16 );
177     }
178     return ret;
179 }
180 
181 #define x264_struct_stat struct _stati64
182 #define x264_fstat _fstati64
183 
x264_stat(const char * path,x264_struct_stat * buf)184 static inline int x264_stat( const char *path, x264_struct_stat *buf )
185 {
186     int ret = -1;
187     wchar_t path_buf[MAX_PATH];
188     wchar_t *path_utf16 = x264_utf8_to_utf16_try_buf( path, path_buf, MAX_PATH );
189     if( path_utf16 )
190     {
191         ret = _wstati64( path_utf16, buf );
192         if( path_utf16 != path_buf )
193             free( path_utf16 );
194     }
195     return ret;
196 }
197 #else
198 #define x264_fopen       fopen
199 #define x264_rename      rename
200 #define x264_struct_stat struct stat
201 #define x264_fstat       fstat
202 #define x264_stat        stat
203 #endif
204 
205 /* mdate: return the current date in microsecond */
206 X264_API int64_t x264_mdate( void );
207 
208 #if defined(_WIN32) && !HAVE_WINRT
x264_vfprintf(FILE * stream,const char * format,va_list arg)209 static inline int x264_vfprintf( FILE *stream, const char *format, va_list arg )
210 {
211     HANDLE console = NULL;
212     DWORD mode;
213 
214     if( stream == stdout )
215         console = GetStdHandle( STD_OUTPUT_HANDLE );
216     else if( stream == stderr )
217         console = GetStdHandle( STD_ERROR_HANDLE );
218 
219     /* Only attempt to convert to UTF-16 when writing to a non-redirected console screen buffer. */
220     if( GetConsoleMode( console, &mode ) )
221     {
222         char buf[4096];
223         wchar_t buf_utf16[4096];
224         va_list arg2;
225 
226         va_copy( arg2, arg );
227         int length = vsnprintf( buf, sizeof(buf), format, arg2 );
228         va_end( arg2 );
229 
230         if( length > 0 && (unsigned)length < sizeof(buf) )
231         {
232             /* WriteConsoleW is the most reliable way to output Unicode to a console. */
233             int length_utf16 = MultiByteToWideChar( CP_UTF8, 0, buf, length, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t) );
234             DWORD written;
235             WriteConsoleW( console, buf_utf16, length_utf16, &written, NULL );
236             return length;
237         }
238     }
239     return vfprintf( stream, format, arg );
240 }
241 
x264_is_regular_file_path(const char * path)242 static inline int x264_is_regular_file_path( const char *path )
243 {
244     int ret = -1;
245     wchar_t path_buf[MAX_PATH];
246     wchar_t *path_utf16 = x264_utf8_to_utf16_try_buf( path, path_buf, MAX_PATH );
247     if( path_utf16 )
248     {
249         x264_struct_stat buf;
250         if( _wstati64( path_utf16, &buf ) )
251             ret = !WaitNamedPipeW( path_utf16, 0 );
252         else
253             ret = S_ISREG( buf.st_mode );
254         if( path_utf16 != path_buf )
255             free( path_utf16 );
256     }
257     return ret;
258 }
259 #else
260 #define x264_vfprintf vfprintf
261 
x264_is_regular_file_path(const char * filename)262 static inline int x264_is_regular_file_path( const char *filename )
263 {
264     x264_struct_stat file_stat;
265     if( x264_stat( filename, &file_stat ) )
266         return 1;
267     return S_ISREG( file_stat.st_mode );
268 }
269 #endif
270 
x264_is_regular_file(FILE * filehandle)271 static inline int x264_is_regular_file( FILE *filehandle )
272 {
273     x264_struct_stat file_stat;
274     if( x264_fstat( fileno( filehandle ), &file_stat ) )
275         return 1;
276     return S_ISREG( file_stat.st_mode );
277 }
278 
279 #define x264_glue3_expand(x,y,z) x##_##y##_##z
280 #define x264_glue3(x,y,z) x264_glue3_expand(x,y,z)
281 
282 #ifdef _MSC_VER
283 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
284 #else
285 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
286 #endif
287 
288 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
289 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
290 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
291 
292 // ARM compiliers don't reliably align stack variables
293 // - EABI requires only 8 byte stack alignment to be maintained
294 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
295 // - armcc can't either, but is nice enough to actually tell you so
296 // - Apple gcc only maintains 4 byte alignment
297 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
298 
299 #define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
300     uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
301     type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)
302 
303 #if ARCH_ARM && SYS_MACOSX
304 #define ALIGNED_ARRAY_8( ... ) EXPAND( ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ ) )
305 #else
306 #define ALIGNED_ARRAY_8( type, name, sub1, ... ) ALIGNED_8( type name sub1 __VA_ARGS__ )
307 #endif
308 
309 #if ARCH_ARM
310 #define ALIGNED_ARRAY_16( ... ) EXPAND( ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ ) )
311 #else
312 #define ALIGNED_ARRAY_16( type, name, sub1, ... ) ALIGNED_16( type name sub1 __VA_ARGS__ )
313 #endif
314 
315 #define EXPAND(x) x
316 
317 #if ARCH_X86 || ARCH_X86_64
318 #define NATIVE_ALIGN 64
319 #define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
320 #define ALIGNED_64( var ) DECLARE_ALIGNED( var, 64 )
321 #if STACK_ALIGNMENT >= 32
322 #define ALIGNED_ARRAY_32( type, name, sub1, ... ) ALIGNED_32( type name sub1 __VA_ARGS__ )
323 #else
324 #define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
325 #endif
326 #if STACK_ALIGNMENT >= 64
327 #define ALIGNED_ARRAY_64( type, name, sub1, ... ) ALIGNED_64( type name sub1 __VA_ARGS__ )
328 #else
329 #define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
330 #endif
331 #else
332 #define NATIVE_ALIGN 16
333 #define ALIGNED_32 ALIGNED_16
334 #define ALIGNED_64 ALIGNED_16
335 #define ALIGNED_ARRAY_32 ALIGNED_ARRAY_16
336 #define ALIGNED_ARRAY_64 ALIGNED_ARRAY_16
337 #endif
338 
339 #if STACK_ALIGNMENT > 16 || (ARCH_X86 && STACK_ALIGNMENT > 4)
340 #define REALIGN_STACK __attribute__((force_align_arg_pointer))
341 #else
342 #define REALIGN_STACK
343 #endif
344 
345 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
346 #define UNUSED __attribute__((unused))
347 #define ALWAYS_INLINE __attribute__((always_inline)) inline
348 #define NOINLINE __attribute__((noinline))
349 #define MAY_ALIAS __attribute__((may_alias))
350 #define x264_constant_p(x) __builtin_constant_p(x)
351 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
352 #else
353 #ifdef _MSC_VER
354 #define ALWAYS_INLINE __forceinline
355 #define NOINLINE __declspec(noinline)
356 #else
357 #define ALWAYS_INLINE inline
358 #define NOINLINE
359 #endif
360 #define UNUSED
361 #define MAY_ALIAS
362 #define x264_constant_p(x) 0
363 #define x264_nonconstant_p(x) 0
364 #endif
365 
366 /* threads */
367 #if HAVE_BEOSTHREAD
368 #include <kernel/OS.h>
369 #define x264_pthread_t               thread_id
x264_pthread_create(x264_pthread_t * t,void * a,void * (* f)(void *),void * d)370 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
371 {
372      *t = spawn_thread( f, "", 10, d );
373      if( *t < B_NO_ERROR )
374          return -1;
375      resume_thread( *t );
376      return 0;
377 }
378 #define x264_pthread_join(t,s)       { long tmp; \
379                                        wait_for_thread(t,(s)?(long*)(s):&tmp); }
380 
381 #elif HAVE_POSIXTHREAD
382 #include <pthread.h>
383 #define x264_pthread_t               pthread_t
384 #define x264_pthread_create          pthread_create
385 #define x264_pthread_join            pthread_join
386 #define x264_pthread_mutex_t         pthread_mutex_t
387 #define x264_pthread_mutex_init      pthread_mutex_init
388 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
389 #define x264_pthread_mutex_lock      pthread_mutex_lock
390 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
391 #define x264_pthread_cond_t          pthread_cond_t
392 #define x264_pthread_cond_init       pthread_cond_init
393 #define x264_pthread_cond_destroy    pthread_cond_destroy
394 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
395 #define x264_pthread_cond_wait       pthread_cond_wait
396 #define x264_pthread_attr_t          pthread_attr_t
397 #define x264_pthread_attr_init       pthread_attr_init
398 #define x264_pthread_attr_destroy    pthread_attr_destroy
399 #define x264_pthread_num_processors_np pthread_num_processors_np
400 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
401 
402 #elif HAVE_WIN32THREAD
403 #include "win32thread.h"
404 
405 #else
406 #define x264_pthread_t               int
407 #define x264_pthread_create(t,u,f,d) 0
408 #define x264_pthread_join(t,s)
409 #endif //HAVE_*THREAD
410 
411 #if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
412 #define x264_pthread_mutex_t         int
413 #define x264_pthread_mutex_init(m,f) 0
414 #define x264_pthread_mutex_destroy(m)
415 #define x264_pthread_mutex_lock(m)
416 #define x264_pthread_mutex_unlock(m)
417 #define x264_pthread_cond_t          int
418 #define x264_pthread_cond_init(c,f)  0
419 #define x264_pthread_cond_destroy(c)
420 #define x264_pthread_cond_broadcast(c)
421 #define x264_pthread_cond_wait(c,m)
422 #define x264_pthread_attr_t          int
423 #define x264_pthread_attr_init(a)    0
424 #define x264_pthread_attr_destroy(a)
425 #define X264_PTHREAD_MUTEX_INITIALIZER 0
426 #endif
427 
428 #if HAVE_WIN32THREAD || PTW32_STATIC_LIB
429 X264_API int x264_threading_init( void );
430 #else
431 #define x264_threading_init() 0
432 #endif
433 
x264_pthread_fetch_and_add(int * val,int add,x264_pthread_mutex_t * mutex)434 static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
435 {
436 #if HAVE_THREAD
437 #if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && (ARCH_X86 || ARCH_X86_64)
438     return __sync_fetch_and_add( val, add );
439 #else
440     x264_pthread_mutex_lock( mutex );
441     int res = *val;
442     *val += add;
443     x264_pthread_mutex_unlock( mutex );
444     return res;
445 #endif
446 #else
447     int res = *val;
448     *val += add;
449     return res;
450 #endif
451 }
452 
453 #define WORD_SIZE sizeof(void*)
454 
455 #define asm __asm__
456 
457 #if WORDS_BIGENDIAN
458 #define endian_fix(x) (x)
459 #define endian_fix64(x) (x)
460 #define endian_fix32(x) (x)
461 #define endian_fix16(x) (x)
462 #else
463 #if HAVE_X86_INLINE_ASM && HAVE_MMX
endian_fix32(uint32_t x)464 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
465 {
466     asm("bswap %0":"+r"(x));
467     return x;
468 }
469 #elif defined(__GNUC__) && HAVE_ARMV6
endian_fix32(uint32_t x)470 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
471 {
472     asm("rev %0, %0":"+r"(x));
473     return x;
474 }
475 #else
endian_fix32(uint32_t x)476 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
477 {
478     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
479 }
480 #endif
481 #if HAVE_X86_INLINE_ASM && ARCH_X86_64
endian_fix64(uint64_t x)482 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
483 {
484     asm("bswap %0":"+r"(x));
485     return x;
486 }
487 #else
endian_fix64(uint64_t x)488 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
489 {
490     return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
491 }
492 #endif
endian_fix(uintptr_t x)493 static ALWAYS_INLINE uintptr_t endian_fix( uintptr_t x )
494 {
495     return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
496 }
endian_fix16(uint16_t x)497 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
498 {
499     return (x<<8)|(x>>8);
500 }
501 #endif
502 
503 /* For values with 4 bits or less. */
x264_ctz_4bit(uint32_t x)504 static ALWAYS_INLINE int x264_ctz_4bit( uint32_t x )
505 {
506     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
507     return lut[x];
508 }
509 
510 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
511 #define x264_clz(x) __builtin_clz(x)
512 #define x264_ctz(x) __builtin_ctz(x)
513 #else
x264_clz(uint32_t x)514 static ALWAYS_INLINE int x264_clz( uint32_t x )
515 {
516     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
517     int y, z = (((x >> 16) - 1) >> 27) & 16;
518     x >>= z^16;
519     z += y = ((x - 0x100) >> 28) & 8;
520     x >>= y^8;
521     z += y = ((x - 0x10) >> 29) & 4;
522     x >>= y^4;
523     return z + lut[x];
524 }
525 
x264_ctz(uint32_t x)526 static ALWAYS_INLINE int x264_ctz( uint32_t x )
527 {
528     static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
529     int y, z = (((x & 0xffff) - 1) >> 27) & 16;
530     x >>= z;
531     z += y = (((x & 0xff) - 1) >> 28) & 8;
532     x >>= y;
533     z += y = (((x & 0xf) - 1) >> 29) & 4;
534     x >>= y;
535     return z + lut[x&0xf];
536 }
537 #endif
538 
539 #if HAVE_X86_INLINE_ASM && HAVE_MMX
540 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
541  * using complex address modes properly unless we use inline asm. */
x264_prefetch(void * p)542 static ALWAYS_INLINE void x264_prefetch( void *p )
543 {
544     asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
545 }
546 /* We require that prefetch not fault on invalid reads, so we only enable it on
547  * known architectures. */
548 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
549       (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
550 #define x264_prefetch(x) __builtin_prefetch(x)
551 #else
552 #define x264_prefetch(x)
553 #endif
554 
555 #if HAVE_POSIXTHREAD
556 #if SYS_WINDOWS
557 #define x264_lower_thread_priority(p)\
558 {\
559     x264_pthread_t handle = pthread_self();\
560     struct sched_param sp;\
561     int policy = SCHED_OTHER;\
562     pthread_getschedparam( handle, &policy, &sp );\
563     sp.sched_priority -= p;\
564     pthread_setschedparam( handle, policy, &sp );\
565 }
566 #elif SYS_HAIKU
567 #include <OS.h>
568 #define x264_lower_thread_priority(p)\
569     { UNUSED status_t nice_ret = set_thread_priority( find_thread( NULL ), B_LOW_PRIORITY ); }
570 #else
571 #include <unistd.h>
572 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
573 #endif /* SYS_WINDOWS */
574 #elif HAVE_WIN32THREAD
575 #define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
576 #else
577 #define x264_lower_thread_priority(p)
578 #endif
579 
580 #endif /* X264_OSDEP_H */
581