osdep.h 15.8 KB
Newer Older
1
/*****************************************************************************
Fiona Glaser's avatar
Fiona Glaser committed
2
 * osdep.h: platform-specific code
3
 *****************************************************************************
Henrik Gramner's avatar
Henrik Gramner committed
4
 * Copyright (C) 2007-2019 x264 project
5 6 7
 *
 * Authors: Loren Merritt <lorenm@u.washington.edu>
 *          Laurent Aimar <fenrir@via.ecp.fr>
Henrik Gramner's avatar
Henrik Gramner committed
8
 *          Henrik Gramner <henrik@gramner.com>
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
Fiona Glaser's avatar
Fiona Glaser committed
23 24 25
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
26 27
 *****************************************************************************/

28 29
#ifndef X264_OSDEP_H
#define X264_OSDEP_H
30

31 32 33
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
34
#include <sys/stat.h>
35
#include <inttypes.h>
Henrik Gramner's avatar
Henrik Gramner committed
36
#include <stdarg.h>
37

Diogo Franco's avatar
Diogo Franco committed
38 39
#include "config.h"

40 41 42 43 44 45
#ifdef __INTEL_COMPILER
#include <mathimf.h>
#else
#include <math.h>
#endif

46 47 48 49 50
#ifdef _WIN32
#include <windows.h>
#include <io.h>
#endif

51 52
#include "x264.h"

Steven Walters's avatar
Steven Walters committed
53
#if !HAVE_LOG2F
54 55
#define log2f(x) (logf(x)/0.693147180559945f)
#define log2(x) (log(x)/0.693147180559945)
56 57
#endif

Steven Walters's avatar
Steven Walters committed
58
#ifdef _MSC_VER
Steven Walters's avatar
Steven Walters committed
59 60 61 62 63
#define inline __inline
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#define strtok_r strtok_s
#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
64 65
#else
#include <strings.h>
Steven Walters's avatar
Steven Walters committed
66 67
#endif

Henrik Gramner's avatar
Henrik Gramner committed
68 69 70 71
#if !defined(va_copy) && defined(__INTEL_COMPILER)
#define va_copy(dst, src) ((dst) = (src))
#endif

Sean McGovern's avatar
Sean McGovern committed
72
#if !defined(isfinite) && (SYS_OPENBSD || SYS_SunOS)
73 74
#define isfinite finite
#endif
Henrik Gramner's avatar
Henrik Gramner committed
75

76
#if !HAVE_STRTOK_R && !defined(strtok_r)
Loren Merritt's avatar
Loren Merritt committed
77 78
#define strtok_r(str,delim,save) strtok(str,delim)
#endif
Henrik Gramner's avatar
Henrik Gramner committed
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#if defined(_MSC_VER) && _MSC_VER < 1900
/* MSVC pre-VS2015 has broken snprintf/vsnprintf implementations which are incompatible with C99. */
static inline int x264_vsnprintf( char *s, size_t n, const char *fmt, va_list arg )
{
    int length = -1;

    if( n )
    {
        va_list arg2;
        va_copy( arg2, arg );
        length = _vsnprintf( s, n, fmt, arg2 );
        va_end( arg2 );

        /* _(v)snprintf adds a null-terminator only if the length is less than the buffer size. */
        if( length < 0 || length >= n )
            s[n-1] = '\0';
    }

    /* _(v)snprintf returns a negative number if the length is greater than the buffer size. */
    if( length < 0 )
        return _vscprintf( fmt, arg );

    return length;
}

static inline int x264_snprintf( char *s, size_t n, const char *fmt, ... )
{
    va_list arg;
    va_start( arg, fmt );
    int length = x264_vsnprintf( s, n, fmt, arg );
    va_end( arg );
    return length;
}

#define snprintf  x264_snprintf
#define vsnprintf x264_vsnprintf
#endif

118
#ifdef _WIN32
Henrik Gramner's avatar
Henrik Gramner committed
119 120
#define utf8_to_utf16( utf8, utf16 )\
    MultiByteToWideChar( CP_UTF8, MB_ERR_INVALID_CHARS, utf8, -1, utf16, sizeof(utf16)/sizeof(wchar_t) )
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

/* Functions for dealing with Unicode on Windows. */
static inline FILE *x264_fopen( const char *filename, const char *mode )
{
    wchar_t filename_utf16[MAX_PATH];
    wchar_t mode_utf16[16];
    if( utf8_to_utf16( filename, filename_utf16 ) && utf8_to_utf16( mode, mode_utf16 ) )
        return _wfopen( filename_utf16, mode_utf16 );
    return NULL;
}

static inline int x264_rename( const char *oldname, const char *newname )
{
    wchar_t oldname_utf16[MAX_PATH];
    wchar_t newname_utf16[MAX_PATH];
    if( utf8_to_utf16( oldname, oldname_utf16 ) && utf8_to_utf16( newname, newname_utf16 ) )
    {
        /* POSIX says that rename() removes the destination, but Win32 doesn't. */
        _wunlink( newname_utf16 );
        return _wrename( oldname_utf16, newname_utf16 );
    }
    return -1;
}

Henrik Gramner's avatar
Henrik Gramner committed
145 146
#define x264_struct_stat struct _stati64
#define x264_fstat _fstati64
147 148 149 150 151 152 153 154

static inline int x264_stat( const char *path, x264_struct_stat *buf )
{
    wchar_t path_utf16[MAX_PATH];
    if( utf8_to_utf16( path, path_utf16 ) )
        return _wstati64( path_utf16, buf );
    return -1;
}
Henrik Gramner's avatar
Henrik Gramner committed
155 156 157 158 159 160
#else
#define x264_fopen       fopen
#define x264_rename      rename
#define x264_struct_stat struct stat
#define x264_fstat       fstat
#define x264_stat        stat
Henrik Gramner's avatar
Henrik Gramner committed
161 162
#endif

163
/* mdate: return the current date in microsecond */
164
X264_API int64_t x264_mdate( void );
165

Henrik Gramner's avatar
Henrik Gramner committed
166
#if defined(_WIN32) && !HAVE_WINRT
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static inline int x264_vfprintf( FILE *stream, const char *format, va_list arg )
{
    HANDLE console = NULL;
    DWORD mode;

    if( stream == stdout )
        console = GetStdHandle( STD_OUTPUT_HANDLE );
    else if( stream == stderr )
        console = GetStdHandle( STD_ERROR_HANDLE );

    /* Only attempt to convert to UTF-16 when writing to a non-redirected console screen buffer. */
    if( GetConsoleMode( console, &mode ) )
    {
        char buf[4096];
        wchar_t buf_utf16[4096];
        va_list arg2;

        va_copy( arg2, arg );
        int length = vsnprintf( buf, sizeof(buf), format, arg2 );
        va_end( arg2 );

        if( length > 0 && length < sizeof(buf) )
        {
            /* WriteConsoleW is the most reliable way to output Unicode to a console. */
            int length_utf16 = MultiByteToWideChar( CP_UTF8, 0, buf, length, buf_utf16, sizeof(buf_utf16)/sizeof(wchar_t) );
            DWORD written;
            WriteConsoleW( console, buf_utf16, length_utf16, &written, NULL );
            return length;
        }
    }
    return vfprintf( stream, format, arg );
}

static inline int x264_is_pipe( const char *path )
{
    wchar_t path_utf16[MAX_PATH];
    if( utf8_to_utf16( path, path_utf16 ) )
        return WaitNamedPipeW( path_utf16, 0 );
    return 0;
}
Henrik Gramner's avatar
Henrik Gramner committed
207 208 209
#else
#define x264_vfprintf vfprintf
#define x264_is_pipe(x) 0
210 211
#endif

212 213 214
#define x264_glue3_expand(x,y,z) x##_##y##_##z
#define x264_glue3(x,y,z) x264_glue3_expand(x,y,z)

Steven Walters's avatar
Steven Walters committed
215
#ifdef _MSC_VER
Steven Walters's avatar
Steven Walters committed
216 217
#define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
#else
Loren Merritt's avatar
Loren Merritt committed
218
#define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
Steven Walters's avatar
Steven Walters committed
219
#endif
220

221
#define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
222 223
#define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
#define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
224

225 226 227 228 229 230
// ARM compiliers don't reliably align stack variables
// - EABI requires only 8 byte stack alignment to be maintained
// - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
// - armcc can't either, but is nice enough to actually tell you so
// - Apple gcc only maintains 4 byte alignment
// - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
231 232 233 234 235

#define ALIGNED_ARRAY_EMU( mask, type, name, sub1, ... )\
    uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + mask]; \
    type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+mask) & ~mask)

Steven Walters's avatar
Steven Walters committed
236
#if ARCH_ARM && SYS_MACOSX
237
#define ALIGNED_ARRAY_8( ... ) EXPAND( ALIGNED_ARRAY_EMU( 7, __VA_ARGS__ ) )
238
#else
239
#define ALIGNED_ARRAY_8( type, name, sub1, ... ) ALIGNED_8( type name sub1 __VA_ARGS__ )
240 241
#endif

Steven Walters's avatar
Steven Walters committed
242
#if ARCH_ARM
243
#define ALIGNED_ARRAY_16( ... ) EXPAND( ALIGNED_ARRAY_EMU( 15, __VA_ARGS__ ) )
244
#else
245
#define ALIGNED_ARRAY_16( type, name, sub1, ... ) ALIGNED_16( type name sub1 __VA_ARGS__ )
246
#endif
247

248 249
#define EXPAND(x) x

250
#if ARCH_X86 || ARCH_X86_64
Henrik Gramner's avatar
Henrik Gramner committed
251
#define NATIVE_ALIGN 64
252
#define ALIGNED_32( var ) DECLARE_ALIGNED( var, 32 )
Henrik Gramner's avatar
Henrik Gramner committed
253
#define ALIGNED_64( var ) DECLARE_ALIGNED( var, 64 )
254
#if STACK_ALIGNMENT >= 32
255
#define ALIGNED_ARRAY_32( type, name, sub1, ... ) ALIGNED_32( type name sub1 __VA_ARGS__ )
256
#else
257
#define ALIGNED_ARRAY_32( ... ) EXPAND( ALIGNED_ARRAY_EMU( 31, __VA_ARGS__ ) )
258
#endif
Henrik Gramner's avatar
Henrik Gramner committed
259 260 261
#if STACK_ALIGNMENT >= 64
#define ALIGNED_ARRAY_64( type, name, sub1, ... ) ALIGNED_64( type name sub1 __VA_ARGS__ )
#else
262
#define ALIGNED_ARRAY_64( ... ) EXPAND( ALIGNED_ARRAY_EMU( 63, __VA_ARGS__ ) )
Henrik Gramner's avatar
Henrik Gramner committed
263
#endif
264 265
#else
#define NATIVE_ALIGN 16
266
#define ALIGNED_32 ALIGNED_16
Henrik Gramner's avatar
Henrik Gramner committed
267
#define ALIGNED_64 ALIGNED_16
268 269
#define ALIGNED_ARRAY_32 ALIGNED_ARRAY_16
#define ALIGNED_ARRAY_64 ALIGNED_ARRAY_16
270 271
#endif

272 273 274 275 276 277
#if STACK_ALIGNMENT > 16 || (ARCH_X86 && STACK_ALIGNMENT > 4)
#define REALIGN_STACK __attribute__((force_align_arg_pointer))
#else
#define REALIGN_STACK
#endif

278 279
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
#define UNUSED __attribute__((unused))
280
#define ALWAYS_INLINE __attribute__((always_inline)) inline
281
#define NOINLINE __attribute__((noinline))
282
#define MAY_ALIAS __attribute__((may_alias))
283
#define x264_constant_p(x) __builtin_constant_p(x)
Fiona Glaser's avatar
Fiona Glaser committed
284
#define x264_nonconstant_p(x) (!__builtin_constant_p(x))
285
#else
Steven Walters's avatar
Steven Walters committed
286
#ifdef _MSC_VER
Steven Walters's avatar
Steven Walters committed
287 288 289
#define ALWAYS_INLINE __forceinline
#define NOINLINE __declspec(noinline)
#else
290
#define ALWAYS_INLINE inline
291
#define NOINLINE
Steven Walters's avatar
Steven Walters committed
292 293
#endif
#define UNUSED
294
#define MAY_ALIAS
295
#define x264_constant_p(x) 0
Fiona Glaser's avatar
Fiona Glaser committed
296
#define x264_nonconstant_p(x) 0
297 298 299
#endif

/* threads */
300
#if HAVE_BEOSTHREAD
301
#include <kernel/OS.h>
302
#define x264_pthread_t               thread_id
303 304 305 306 307 308 309 310
static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
{
     *t = spawn_thread( f, "", 10, d );
     if( *t < B_NO_ERROR )
         return -1;
     resume_thread( *t );
     return 0;
}
311
#define x264_pthread_join(t,s)       { long tmp; \
312
                                       wait_for_thread(t,(s)?(long*)(s):&tmp); }
313

314
#elif HAVE_POSIXTHREAD
315
#include <pthread.h>
316 317 318 319 320 321 322 323 324 325 326 327 328
#define x264_pthread_t               pthread_t
#define x264_pthread_create          pthread_create
#define x264_pthread_join            pthread_join
#define x264_pthread_mutex_t         pthread_mutex_t
#define x264_pthread_mutex_init      pthread_mutex_init
#define x264_pthread_mutex_destroy   pthread_mutex_destroy
#define x264_pthread_mutex_lock      pthread_mutex_lock
#define x264_pthread_mutex_unlock    pthread_mutex_unlock
#define x264_pthread_cond_t          pthread_cond_t
#define x264_pthread_cond_init       pthread_cond_init
#define x264_pthread_cond_destroy    pthread_cond_destroy
#define x264_pthread_cond_broadcast  pthread_cond_broadcast
#define x264_pthread_cond_wait       pthread_cond_wait
Steven Walters's avatar
Steven Walters committed
329 330 331
#define x264_pthread_attr_t          pthread_attr_t
#define x264_pthread_attr_init       pthread_attr_init
#define x264_pthread_attr_destroy    pthread_attr_destroy
332
#define x264_pthread_num_processors_np pthread_num_processors_np
333
#define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
334 335 336 337

#elif HAVE_WIN32THREAD
#include "win32thread.h"

338
#else
339 340 341 342 343 344
#define x264_pthread_t               int
#define x264_pthread_create(t,u,f,d) 0
#define x264_pthread_join(t,s)
#endif //HAVE_*THREAD

#if !HAVE_POSIXTHREAD && !HAVE_WIN32THREAD
345
#define x264_pthread_mutex_t         int
346
#define x264_pthread_mutex_init(m,f) 0
347 348 349 350
#define x264_pthread_mutex_destroy(m)
#define x264_pthread_mutex_lock(m)
#define x264_pthread_mutex_unlock(m)
#define x264_pthread_cond_t          int
351
#define x264_pthread_cond_init(c,f)  0
352 353
#define x264_pthread_cond_destroy(c)
#define x264_pthread_cond_broadcast(c)
Gabriel Bouvigne's avatar
Gabriel Bouvigne committed
354
#define x264_pthread_cond_wait(c,m)
Steven Walters's avatar
Steven Walters committed
355 356 357
#define x264_pthread_attr_t          int
#define x264_pthread_attr_init(a)    0
#define x264_pthread_attr_destroy(a)
358
#define X264_PTHREAD_MUTEX_INITIALIZER 0
359 360
#endif

361
#if HAVE_WIN32THREAD || PTW32_STATIC_LIB
362
X264_API int x264_threading_init( void );
363 364 365 366
#else
#define x264_threading_init() 0
#endif

Fiona Glaser's avatar
Fiona Glaser committed
367 368 369
static ALWAYS_INLINE int x264_pthread_fetch_and_add( int *val, int add, x264_pthread_mutex_t *mutex )
{
#if HAVE_THREAD
370
#if defined(__GNUC__) && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ > 0) && (ARCH_X86 || ARCH_X86_64)
Fiona Glaser's avatar
Fiona Glaser committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    return __sync_fetch_and_add( val, add );
#else
    x264_pthread_mutex_lock( mutex );
    int res = *val;
    *val += add;
    x264_pthread_mutex_unlock( mutex );
    return res;
#endif
#else
    int res = *val;
    *val += add;
    return res;
#endif
}

Fiona Glaser's avatar
Fiona Glaser committed
386
#define WORD_SIZE sizeof(void*)
387

388 389
#define asm __asm__

Steven Walters's avatar
Steven Walters committed
390
#if WORDS_BIGENDIAN
Fiona Glaser's avatar
Fiona Glaser committed
391
#define endian_fix(x) (x)
Kieran Kunhya's avatar
Kieran Kunhya committed
392
#define endian_fix64(x) (x)
393
#define endian_fix32(x) (x)
Fiona Glaser's avatar
Fiona Glaser committed
394 395
#define endian_fix16(x) (x)
#else
Steven Walters's avatar
Steven Walters committed
396
#if HAVE_X86_INLINE_ASM && HAVE_MMX
Fiona Glaser's avatar
Fiona Glaser committed
397 398 399 400 401
static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
{
    asm("bswap %0":"+r"(x));
    return x;
}
Steven Walters's avatar
Steven Walters committed
402
#elif defined(__GNUC__) && HAVE_ARMV6
Kieran Kunhya's avatar
Kieran Kunhya committed
403
static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
404 405 406 407
{
    asm("rev %0, %0":"+r"(x));
    return x;
}
Fiona Glaser's avatar
Fiona Glaser committed
408 409 410 411 412
#else
static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
{
    return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
}
Kieran Kunhya's avatar
Kieran Kunhya committed
413
#endif
Steven Walters's avatar
Steven Walters committed
414
#if HAVE_X86_INLINE_ASM && ARCH_X86_64
Kieran Kunhya's avatar
Kieran Kunhya committed
415
static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
Fiona Glaser's avatar
Fiona Glaser committed
416
{
Kieran Kunhya's avatar
Kieran Kunhya committed
417 418 419 420 421 422 423
    asm("bswap %0":"+r"(x));
    return x;
}
#else
static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
{
    return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
Fiona Glaser's avatar
Fiona Glaser committed
424 425
}
#endif
Kieran Kunhya's avatar
Kieran Kunhya committed
426 427 428 429
static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
{
    return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
}
Fiona Glaser's avatar
Fiona Glaser committed
430 431 432 433 434
static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
{
    return (x<<8)|(x>>8);
}
#endif
Fiona Glaser's avatar
Fiona Glaser committed
435

436
/* For values with 4 bits or less. */
437
static ALWAYS_INLINE int x264_ctz_4bit( uint32_t x )
438 439 440 441 442
{
    static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
    return lut[x];
}

443
#if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
444
#define x264_clz(x) __builtin_clz(x)
445
#define x264_ctz(x) __builtin_ctz(x)
446
#else
447
static ALWAYS_INLINE int x264_clz( uint32_t x )
448 449
{
    static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
Loren Merritt's avatar
Loren Merritt committed
450
    int y, z = (((x >> 16) - 1) >> 27) & 16;
451 452 453 454 455 456 457
    x >>= z^16;
    z += y = ((x - 0x100) >> 28) & 8;
    x >>= y^8;
    z += y = ((x - 0x10) >> 29) & 4;
    x >>= y^4;
    return z + lut[x];
}
458

459
static ALWAYS_INLINE int x264_ctz( uint32_t x )
460 461 462 463 464 465 466 467 468 469
{
    static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
    int y, z = (((x & 0xffff) - 1) >> 27) & 16;
    x >>= z;
    z += y = (((x & 0xff) - 1) >> 28) & 8;
    x >>= y;
    z += y = (((x & 0xf) - 1) >> 29) & 4;
    x >>= y;
    return z + lut[x&0xf];
}
470 471
#endif

Steven Walters's avatar
Steven Walters committed
472
#if HAVE_X86_INLINE_ASM && HAVE_MMX
Fiona Glaser's avatar
Fiona Glaser committed
473 474 475 476 477 478 479 480 481
/* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
 * using complex address modes properly unless we use inline asm. */
static ALWAYS_INLINE void x264_prefetch( void *p )
{
    asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
}
/* We require that prefetch not fault on invalid reads, so we only enable it on
 * known architectures. */
#elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
Steven Walters's avatar
Steven Walters committed
482
      (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
Fiona Glaser's avatar
Fiona Glaser committed
483 484 485 486 487
#define x264_prefetch(x) __builtin_prefetch(x)
#else
#define x264_prefetch(x)
#endif

488
#if HAVE_POSIXTHREAD
Steven Walters's avatar
Steven Walters committed
489
#if SYS_WINDOWS
490 491 492 493 494 495 496 497 498
#define x264_lower_thread_priority(p)\
{\
    x264_pthread_t handle = pthread_self();\
    struct sched_param sp;\
    int policy = SCHED_OTHER;\
    pthread_getschedparam( handle, &policy, &sp );\
    sp.sched_priority -= p;\
    pthread_setschedparam( handle, policy, &sp );\
}
Jerome Duval's avatar
Jerome Duval committed
499 500 501 502
#elif SYS_HAIKU
#include <OS.h>
#define x264_lower_thread_priority(p)\
    { UNUSED status_t nice_ret = set_thread_priority( find_thread( NULL ), B_LOW_PRIORITY ); }
503
#else
Steven Walters's avatar
Steven Walters committed
504 505
#include <unistd.h>
#define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
Steven Walters's avatar
Steven Walters committed
506
#endif /* SYS_WINDOWS */
507 508
#elif HAVE_WIN32THREAD
#define x264_lower_thread_priority(p) SetThreadPriority( GetCurrentThread(), X264_MAX( -2, -p ) )
Steven Walters's avatar
Steven Walters committed
509 510 511 512
#else
#define x264_lower_thread_priority(p)
#endif

513
static inline int x264_is_regular_file( FILE *filehandle )
514
{
Henrik Gramner's avatar
Henrik Gramner committed
515 516
    x264_struct_stat file_stat;
    if( x264_fstat( fileno( filehandle ), &file_stat ) )
517
        return 1;
518 519 520
    return S_ISREG( file_stat.st_mode );
}

521
static inline int x264_is_regular_file_path( const char *filename )
522
{
Henrik Gramner's avatar
Henrik Gramner committed
523 524
    x264_struct_stat file_stat;
    if( x264_stat( filename, &file_stat ) )
525
        return !x264_is_pipe( filename );
526 527 528
    return S_ISREG( file_stat.st_mode );
}

529
#endif /* X264_OSDEP_H */