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 #define log2f(x) (logf((x))/0.693147180559945f)
42 #include <io.h> // _setmode()
43 #include <fcntl.h> // _O_BINARY
46 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
47 #define isfinite finite
50 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
52 #define strtok_r(str,delim,save) strtok(str,delim)
56 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
57 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
58 #define ALIGNED_8( var ) DECLARE_ALIGNED( var, 8 )
59 #define ALIGNED_4( var ) DECLARE_ALIGNED( var, 4 )
61 // ARM compiliers don't reliably align stack variables
62 // - EABI requires only 8 byte stack alignment to be maintained
63 // - gcc can't align stack variables to more even if the stack were to be correctly aligned outside the function
64 // - armcc can't either, but is nice enough to actually tell you so
65 // - Apple gcc only maintains 4 byte alignment
66 // - llvm can align the stack, but only in svn and (unrelated) it exposes bugs in all released GNU binutils...
67 #if defined(ARCH_ARM) && defined(SYS_MACOSX)
68 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
69 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 7]; \
70 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+7) & ~7)
72 #define ALIGNED_ARRAY_8( type, name, sub1, ... )\
73 ALIGNED_8( type name sub1 __VA_ARGS__ )
77 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
78 uint8_t name##_u [sizeof(type sub1 __VA_ARGS__) + 15];\
79 type (*name) __VA_ARGS__ = (void*)((intptr_t)(name##_u+15) & ~15)
81 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
82 ALIGNED_16( type name sub1 __VA_ARGS__ )
85 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
86 #define UNUSED __attribute__((unused))
87 #define ALWAYS_INLINE __attribute__((always_inline)) inline
88 #define NOINLINE __attribute__((noinline))
89 #define MAY_ALIAS __attribute__((may_alias))
90 #define x264_constant_p(x) __builtin_constant_p(x)
93 #define ALWAYS_INLINE inline
96 #define x264_constant_p(x) 0
100 #if defined(SYS_BEOS)
101 #include <kernel/OS.h>
102 #define x264_pthread_t thread_id
103 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
105 *t = spawn_thread( f, "", 10, d );
106 if( *t < B_NO_ERROR )
111 #define x264_pthread_join(t,s) { long tmp; \
112 wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
114 #define usleep(t) snooze(t)
116 #define HAVE_PTHREAD 1
118 #elif defined(HAVE_PTHREAD)
120 #define USE_REAL_PTHREAD
123 #define x264_pthread_t int
124 #define x264_pthread_create(t,u,f,d) 0
125 #define x264_pthread_join(t,s)
128 #ifdef USE_REAL_PTHREAD
129 #define x264_pthread_t pthread_t
130 #define x264_pthread_create pthread_create
131 #define x264_pthread_join pthread_join
132 #define x264_pthread_mutex_t pthread_mutex_t
133 #define x264_pthread_mutex_init pthread_mutex_init
134 #define x264_pthread_mutex_destroy pthread_mutex_destroy
135 #define x264_pthread_mutex_lock pthread_mutex_lock
136 #define x264_pthread_mutex_unlock pthread_mutex_unlock
137 #define x264_pthread_cond_t pthread_cond_t
138 #define x264_pthread_cond_init pthread_cond_init
139 #define x264_pthread_cond_destroy pthread_cond_destroy
140 #define x264_pthread_cond_broadcast pthread_cond_broadcast
141 #define x264_pthread_cond_wait pthread_cond_wait
142 #define x264_pthread_attr_t pthread_attr_t
143 #define x264_pthread_attr_init pthread_attr_init
144 #define x264_pthread_attr_destroy pthread_attr_destroy
145 #define X264_PTHREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
147 #define x264_pthread_mutex_t int
148 #define x264_pthread_mutex_init(m,f) 0
149 #define x264_pthread_mutex_destroy(m)
150 #define x264_pthread_mutex_lock(m)
151 #define x264_pthread_mutex_unlock(m)
152 #define x264_pthread_cond_t int
153 #define x264_pthread_cond_init(c,f) 0
154 #define x264_pthread_cond_destroy(c)
155 #define x264_pthread_cond_broadcast(c)
156 #define x264_pthread_cond_wait(c,m)
157 #define x264_pthread_attr_t int
158 #define x264_pthread_attr_init(a) 0
159 #define x264_pthread_attr_destroy(a)
160 #define X264_PTHREAD_MUTEX_INITIALIZER 0
163 #define WORD_SIZE sizeof(void*)
165 #if !defined(_WIN64) && !defined(__LP64__)
166 #if defined(__INTEL_COMPILER)
167 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
171 #ifdef WORDS_BIGENDIAN
172 #define endian_fix(x) (x)
173 #define endian_fix64(x) (x)
174 #define endian_fix32(x) (x)
175 #define endian_fix16(x) (x)
177 #if defined(__GNUC__) && defined(HAVE_MMX)
178 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
180 asm("bswap %0":"+r"(x));
183 #elif defined(__GNUC__) && defined(HAVE_ARMV6)
184 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
186 asm("rev %0, %0":"+r"(x));
190 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
192 return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
195 #if defined(__GNUC__) && defined(ARCH_X86_64)
196 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
198 asm("bswap %0":"+r"(x));
202 static ALWAYS_INLINE uint64_t endian_fix64( uint64_t x )
204 return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
207 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
209 return WORD_SIZE == 8 ? endian_fix64(x) : endian_fix32(x);
211 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
213 return (x<<8)|(x>>8);
217 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
218 #define x264_clz(x) __builtin_clz(x)
220 static int ALWAYS_INLINE x264_clz( uint32_t x )
222 static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
223 int y, z = (((x >> 16) - 1) >> 27) & 16;
225 z += y = ((x - 0x100) >> 28) & 8;
227 z += y = ((x - 0x10) >> 29) & 4;
233 #ifdef USE_REAL_PTHREAD
235 #define x264_lower_thread_priority(p)\
237 x264_pthread_t handle = pthread_self();\
238 struct sched_param sp;\
239 int policy = SCHED_OTHER;\
240 pthread_getschedparam( handle, &policy, &sp );\
241 sp.sched_priority -= p;\
242 pthread_setschedparam( handle, policy, &sp );\
246 #define x264_lower_thread_priority(p) { UNUSED int nice_ret = nice(p); }
247 #endif /* USE_REAL_PTHREAD */
249 #define x264_lower_thread_priority(p)
252 static inline uint8_t x264_is_regular_file( FILE *filehandle )
254 struct stat file_stat;
255 if( fstat( fileno( filehandle ), &file_stat ) )
257 return S_ISREG( file_stat.st_mode );
260 #endif /* X264_OSDEP_H */