1 /*****************************************************************************
2 * osdep.h: h264 encoder
3 *****************************************************************************
4 * Copyright (C) 2007-2008 x264 project
6 * Authors: Loren Merritt <lorenm@u.washington.edu>
7 * Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 *****************************************************************************/
27 #define _LARGEFILE_SOURCE 1
28 #define _FILE_OFFSET_BITS 64
38 #include <io.h> // _setmode()
39 #include <fcntl.h> // _O_BINARY
42 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
43 #define isfinite finite
46 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
48 #define strtok_r(str,delim,save) strtok(str,delim)
52 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
53 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
54 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
55 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
57 // current arm compilers only maintain 8-byte stack alignment
58 // and cannot align stack variables to more than 8-bytes
60 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
61 ALIGNED_8( uint8_t name##_8 [sizeof(type sub1 __VA_ARGS__) + 8] );\
62 type (*name) __VA_ARGS__ = (void*)(name##_8 + ((intptr_t)name##_8 & 8))
64 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
65 ALIGNED_16( type name sub1 __VA_ARGS__ )
68 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
69 #define UNUSED __attribute__((unused))
70 #define ALWAYS_INLINE __attribute__((always_inline)) inline
71 #define NOINLINE __attribute__((noinline))
72 #define x264_constant_p(x) __builtin_constant_p(x)
75 #define ALWAYS_INLINE inline
77 #define x264_constant_p(x) 0
82 #include <kernel/OS.h>
83 #define x264_pthread_t thread_id
84 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
86 *t = spawn_thread( f, "", 10, d );
92 #define x264_pthread_join(t,s) { long tmp; \
93 wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
95 #define usleep(t) snooze(t)
97 #define HAVE_PTHREAD 1
99 #elif defined(HAVE_PTHREAD)
101 #define USE_REAL_PTHREAD
104 #define x264_pthread_t int
105 #define x264_pthread_create(t,u,f,d) 0
106 #define x264_pthread_join(t,s)
109 #ifdef USE_REAL_PTHREAD
110 #define x264_pthread_t pthread_t
111 #define x264_pthread_create pthread_create
112 #define x264_pthread_join pthread_join
113 #define x264_pthread_mutex_t pthread_mutex_t
114 #define x264_pthread_mutex_init pthread_mutex_init
115 #define x264_pthread_mutex_destroy pthread_mutex_destroy
116 #define x264_pthread_mutex_lock pthread_mutex_lock
117 #define x264_pthread_mutex_unlock pthread_mutex_unlock
118 #define x264_pthread_cond_t pthread_cond_t
119 #define x264_pthread_cond_init pthread_cond_init
120 #define x264_pthread_cond_destroy pthread_cond_destroy
121 #define x264_pthread_cond_broadcast pthread_cond_broadcast
122 #define x264_pthread_cond_wait pthread_cond_wait
123 #define x264_pthread_attr_t pthread_attr_t
124 #define x264_pthread_attr_init pthread_attr_init
125 #define x264_pthread_attr_destroy pthread_attr_destroy
126 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
128 #define x264_pthread_mutex_t int
129 #define x264_pthread_mutex_init(m,f) 0
130 #define x264_pthread_mutex_destroy(m)
131 #define x264_pthread_mutex_lock(m)
132 #define x264_pthread_mutex_unlock(m)
133 #define x264_pthread_cond_t int
134 #define x264_pthread_cond_init(c,f) 0
135 #define x264_pthread_cond_destroy(c)
136 #define x264_pthread_cond_broadcast(c)
137 #define x264_pthread_cond_wait(c,m)
138 #define x264_pthread_attr_t int
139 #define x264_pthread_attr_init(a) 0
140 #define x264_pthread_attr_destroy(a)
141 #define X264_PTHREAD_MUTEX_INITIALIZER 0
144 #define WORD_SIZE sizeof(void*)
146 #if !defined(_WIN64) && !defined(__LP64__)
147 #if defined(__INTEL_COMPILER)
148 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
152 #ifdef WORDS_BIGENDIAN
153 #define endian_fix(x) (x)
154 #define endian_fix32(x) (x)
155 #define endian_fix16(x) (x)
157 #if defined(__GNUC__) && defined(HAVE_MMX)
158 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
160 asm("bswap %0":"+r"(x));
163 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
165 asm("bswap %0":"+r"(x));
168 #elif defined(__GNUC__) && defined(HAVE_ARMV6)
169 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
171 asm("rev %0, %0":"+r"(x));
174 #define endian_fix32 endian_fix
176 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
178 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
180 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
183 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
185 return endian_fix32(x);
188 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
190 return (x<<8)|(x>>8);
194 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
195 #define x264_clz(x) __builtin_clz(x)
197 static int ALWAYS_INLINE x264_clz( uint32_t x )
199 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
200 int y, z = ((x - 0x10000) >> 27) & 16;
202 z += y = ((x - 0x100) >> 28) & 8;
204 z += y = ((x - 0x10) >> 29) & 4;
210 #ifdef USE_REAL_PTHREAD
212 #define x264_lower_thread_priority(p)\
214 x264_pthread_t handle = pthread_self();\
215 struct sched_param sp;\
216 int policy = SCHED_OTHER;\
217 pthread_getschedparam( handle, &policy, &sp );\
218 sp.sched_priority -= p;\
219 pthread_setschedparam( handle, policy, &sp );\
223 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
224 #endif /* USE_REAL_PTHREAD */
226 #define x264_lower_thread_priority(p)
229 static inline uint8_t x264_is_regular_file( FILE *filehandle )
231 struct stat file_stat;
232 if( fstat( fileno( filehandle ), &file_stat ) )
234 return S_ISREG( file_stat.st_mode );
237 #endif /* X264_OSDEP_H */