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
41 #define log2f(x) (logf(x)/0.693147180559945f)
42 #define log2(x) (log(x)/0.693147180559945)
46 #include <io.h> // _setmode()
47 #include <fcntl.h> // _O_BINARY
50 #if (SYS_OPENBSD && !defined(isfinite)) || SYS_SunOS
51 #define isfinite finite
54 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
56 #define strtok_r(str,delim,save) strtok(str,delim)
60 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
61 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
62 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
63 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
65 // ARM compiliers don't reliably align stack variables
66 // - EABI requires only 8 byte stack alignment to be maintained
67 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
68 // - armcc can't either, but is nice enough to actually tell you so
69 // - Apple gcc only maintains 4 byte alignment
70 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
71 #if ARCH_ARM && SYS_MACOSX
72 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
73 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
74 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
76 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
77 ALIGNED_8( type name sub1 __VA_ARGS__ )
81 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
82 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
83 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
85 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
86 ALIGNED_16( type name sub1 __VA_ARGS__ )
89 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
90 #define UNUSED __attribute__((unused))
91 #define ALWAYS_INLINE __attribute__((always_inline)) inline
92 #define NOINLINE __attribute__((noinline))
93 #define MAY_ALIAS __attribute__((may_alias))
94 #define x264_constant_p(x) __builtin_constant_p(x)
95 #define x264_nonconstant_p(x) (!__builtin_constant_p(x))
98 #define ALWAYS_INLINE inline
101 #define x264_constant_p(x) 0
102 #define x264_nonconstant_p(x) 0
107 #include <kernel/OS.h>
108 #define x264_pthread_t thread_id
109 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
111 *t = spawn_thread( f, "", 10, d );
112 if( *t < B_NO_ERROR )
117 #define x264_pthread_join(t,s) { long tmp; \
118 wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
120 #define usleep(t) snooze(t)
122 #define HAVE_PTHREAD 1
126 #define USE_REAL_PTHREAD 1
129 #define x264_pthread_t int
130 #define x264_pthread_create(t,u,f,d) 0
131 #define x264_pthread_join(t,s)
135 #define x264_pthread_t pthread_t
136 #define x264_pthread_create pthread_create
137 #define x264_pthread_join pthread_join
138 #define x264_pthread_mutex_t pthread_mutex_t
139 #define x264_pthread_mutex_init pthread_mutex_init
140 #define x264_pthread_mutex_destroy pthread_mutex_destroy
141 #define x264_pthread_mutex_lock pthread_mutex_lock
142 #define x264_pthread_mutex_unlock pthread_mutex_unlock
143 #define x264_pthread_cond_t pthread_cond_t
144 #define x264_pthread_cond_init pthread_cond_init
145 #define x264_pthread_cond_destroy pthread_cond_destroy
146 #define x264_pthread_cond_broadcast pthread_cond_broadcast
147 #define x264_pthread_cond_wait pthread_cond_wait
148 #define x264_pthread_attr_t pthread_attr_t
149 #define x264_pthread_attr_init pthread_attr_init
150 #define x264_pthread_attr_destroy pthread_attr_destroy
151 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
153 #define x264_pthread_mutex_t int
154 #define x264_pthread_mutex_init(m,f) 0
155 #define x264_pthread_mutex_destroy(m)
156 #define x264_pthread_mutex_lock(m)
157 #define x264_pthread_mutex_unlock(m)
158 #define x264_pthread_cond_t int
159 #define x264_pthread_cond_init(c,f) 0
160 #define x264_pthread_cond_destroy(c)
161 #define x264_pthread_cond_broadcast(c)
162 #define x264_pthread_cond_wait(c,m)
163 #define x264_pthread_attr_t int
164 #define x264_pthread_attr_init(a) 0
165 #define x264_pthread_attr_destroy(a)
166 #define X264_PTHREAD_MUTEX_INITIALIZER 0
169 #define WORD_SIZE sizeof(void*)
173 #if !defined(_WIN64) && !defined(__LP64__)
174 #if defined(__INTEL_COMPILER)
175 #define BROKEN_STACK_ALIGNMENT 1 /* define it if stack is not mod16 */
180 #define endian_fix(x) (x)
181 #define endian_fix64(x) (x)
182 #define endian_fix32(x) (x)
183 #define endian_fix16(x) (x)
185 #if defined(__GNUC__) && HAVE_MMX
186 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
188 asm("bswap %0":"+r"(x));
191 #elif defined(__GNUC__) && HAVE_ARMV6
192 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
194 asm("rev %0, %0":"+r"(x));
198 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
200 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
203 #if defined(__GNUC__) && ARCH_X86_64
204 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
206 asm("bswap %0":"+r"(x));
210 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
212 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
215 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
217 return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
219 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
221 return (x<<8)|(x>>8);
225 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
226 #define x264_clz(x) __builtin_clz(x)
227 #define x264_ctz(x) __builtin_ctz(x)
229 static int ALWAYS_INLINE x264_clz( uint32_t x )
231 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
232 int y, z = (((x >> 16) - 1) >> 27) & 16;
234 z += y = ((x - 0x100) >> 28) & 8;
236 z += y = ((x - 0x10) >> 29) & 4;
241 static int ALWAYS_INLINE x264_ctz( uint32_t x )
243 static uint8_t lut[16] = {4,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0};
244 int y, z = (((x & 0xffff) - 1) >> 27) & 16;
246 z += y = (((x & 0xff) - 1) >> 28) & 8;
248 z += y = (((x & 0xf) - 1) >> 29) & 4;
250 return z + lut[x&0xf];
254 #if defined(__GNUC__) && HAVE_MMX
255 /* Don't use __builtin_prefetch; even as recent as 4.3.4, GCC seems incapable of
256 * using complex address modes properly unless we use inline asm. */
257 static ALWAYS_INLINE void x264_prefetch( void *p )
259 asm volatile( "prefetcht0 %0"::"m"(*(uint8_t*)p) );
261 /* We require that prefetch not fault on invalid reads, so we only enable it on
262 * known architectures. */
263 #elif defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 1) &&\
264 (ARCH_X86 || ARCH_X86_64 || ARCH_ARM || ARCH_PPC)
265 #define x264_prefetch(x) __builtin_prefetch(x)
267 #define x264_prefetch(x)
272 #define x264_lower_thread_priority(p)\
274 x264_pthread_t handle = pthread_self();\
275 struct sched_param sp;\
276 int policy = SCHED_OTHER;\
277 pthread_getschedparam( handle, &policy, &sp );\
278 sp.sched_priority -= p;\
279 pthread_setschedparam( handle, policy, &sp );\
283 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
284 #endif /* USE_REAL_PTHREAD */
286 #define x264_lower_thread_priority(p)
289 static inline uint8_t x264_is_regular_file( FILE *filehandle )
291 struct stat file_stat;
292 if( fstat( fileno( filehandle ), &file_stat ) )
294 return S_ISREG( file_stat.st_mode );
297 #endif /* X264_OSDEP_H */