]> git.sesse.net Git - x264/blob - common/osdep.h
GSOC merge part 7: ARM NEON deblock assembly functions (partial)
[x264] / common / osdep.h
1 /*****************************************************************************
2  * osdep.h: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2007-2008 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 #ifndef X264_OSDEP_H
25 #define X264_OSDEP_H
26
27 #define _LARGEFILE_SOURCE 1
28 #define _FILE_OFFSET_BITS 64
29 #include <stdio.h>
30
31 #ifdef HAVE_STDINT_H
32 #include <stdint.h>
33 #else
34 #include <inttypes.h>
35 #endif
36
37 #ifdef _WIN32
38 #include <io.h>    // _setmode()
39 #include <fcntl.h> // _O_BINARY
40 #endif
41
42 #ifdef _MSC_VER
43 #define inline __inline
44 #define strcasecmp stricmp
45 #define strncasecmp strnicmp
46 #define snprintf _snprintf
47 #define fseek _fseeki64
48 #define ftell _ftelli64
49 #define isfinite _finite
50 #define strtok_r strtok_s
51 #define _CRT_SECURE_NO_DEPRECATE
52 #define X264_VERSION "" // no configure script for msvc
53 #endif
54
55 #if (defined(SYS_OPENBSD) && !defined(isfinite)) || defined(SYS_SunOS)
56 #define isfinite finite
57 #endif
58 #ifdef _WIN32
59 #define rename(src,dst) (unlink(dst), rename(src,dst)) // POSIX says that rename() removes the destination, but win32 doesn't.
60 #ifndef strtok_r
61 #define strtok_r(str,delim,save) strtok(str,delim)
62 #endif
63 #endif
64
65 #ifdef _MSC_VER
66 #define DECLARE_ALIGNED( var, n ) __declspec(align(n)) var
67 #else
68 #define DECLARE_ALIGNED( var, n ) var __attribute__((aligned(n)))
69 #endif
70 #define ALIGNED_16( var ) DECLARE_ALIGNED( var, 16 )
71 #define ALIGNED_8( var )  DECLARE_ALIGNED( var, 8 )
72 #define ALIGNED_4( var )  DECLARE_ALIGNED( var, 4 )
73
74 // current arm compilers only maintain 8-byte stack alignment
75 // and cannot align stack variables to more than 8-bytes
76 #ifdef ARCH_ARM
77 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
78     ALIGNED_8( uint8_t name##_8 [sizeof(type sub1 __VA_ARGS__) + 8] );\
79     type (*name) __VA_ARGS__ = (void*)(name##_8 + ((intptr_t)name##_8 & 8))
80 #else
81 #define ALIGNED_ARRAY_16( type, name, sub1, ... )\
82     ALIGNED_16( type name sub1 __VA_ARGS__ )
83 #endif
84
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 x264_constant_p(x) __builtin_constant_p(x)
90 #else
91 #define UNUSED
92 #define ALWAYS_INLINE inline
93 #define NOINLINE
94 #define x264_constant_p(x) 0
95 #endif
96
97 /* threads */
98 #if defined(SYS_BEOS)
99 #include <kernel/OS.h>
100 #define x264_pthread_t               thread_id
101 static inline int x264_pthread_create( x264_pthread_t *t, void *a, void *(*f)(void *), void *d )
102 {
103      *t = spawn_thread( f, "", 10, d );
104      if( *t < B_NO_ERROR )
105          return -1;
106      resume_thread( *t );
107      return 0;
108 }
109 #define x264_pthread_join(t,s)       { long tmp; \
110                                        wait_for_thread(t,(s)?(long*)(*(s)):&tmp); }
111 #ifndef usleep
112 #define usleep(t)                    snooze(t)
113 #endif
114 #define HAVE_PTHREAD 1
115
116 #elif defined(HAVE_PTHREAD)
117 #include <pthread.h>
118 #define USE_REAL_PTHREAD
119
120 #else
121 #define x264_pthread_t               int
122 #define x264_pthread_create(t,u,f,d) 0
123 #define x264_pthread_join(t,s)
124 #endif //SYS_*
125
126 #ifdef USE_REAL_PTHREAD
127 #define x264_pthread_t               pthread_t
128 #define x264_pthread_create          pthread_create
129 #define x264_pthread_join            pthread_join
130 #define x264_pthread_mutex_t         pthread_mutex_t
131 #define x264_pthread_mutex_init      pthread_mutex_init
132 #define x264_pthread_mutex_destroy   pthread_mutex_destroy
133 #define x264_pthread_mutex_lock      pthread_mutex_lock
134 #define x264_pthread_mutex_unlock    pthread_mutex_unlock
135 #define x264_pthread_cond_t          pthread_cond_t
136 #define x264_pthread_cond_init       pthread_cond_init
137 #define x264_pthread_cond_destroy    pthread_cond_destroy
138 #define x264_pthread_cond_broadcast  pthread_cond_broadcast
139 #define x264_pthread_cond_wait       pthread_cond_wait
140 #else
141 #define x264_pthread_mutex_t         int
142 #define x264_pthread_mutex_init(m,f) 0
143 #define x264_pthread_mutex_destroy(m)
144 #define x264_pthread_mutex_lock(m)
145 #define x264_pthread_mutex_unlock(m)
146 #define x264_pthread_cond_t          int
147 #define x264_pthread_cond_init(c,f)  0
148 #define x264_pthread_cond_destroy(c)
149 #define x264_pthread_cond_broadcast(c)
150 #define x264_pthread_cond_wait(c,m)
151 #endif
152
153 #define WORD_SIZE sizeof(void*)
154
155 #if !defined(_WIN64) && !defined(__LP64__)
156 #if defined(_MSC_VER) || defined(__INTEL_COMPILER)
157 #define BROKEN_STACK_ALIGNMENT /* define it if stack is not mod16 */
158 #endif
159 #endif
160
161 #ifdef WORDS_BIGENDIAN
162 #define endian_fix(x) (x)
163 #define endian_fix32(x) (x)
164 #define endian_fix16(x) (x)
165 #else
166 #if defined(__GNUC__) && defined(HAVE_MMX)
167 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
168 {
169     asm("bswap %0":"+r"(x));
170     return x;
171 }
172 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
173 {
174     asm("bswap %0":"+r"(x));
175     return x;
176 }
177 #elif defined(__GNUC__) && defined(HAVE_ARMV6)
178 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
179 {
180     asm("rev %0, %0":"+r"(x));
181     return x;
182 }
183 #define endian_fix32 endian_fix
184 #else
185 static ALWAYS_INLINE uint32_t endian_fix32( uint32_t x )
186 {
187     return (x<<24) + ((x<<8)&0xff0000) + ((x>>8)&0xff00) + (x>>24);
188 }
189 static ALWAYS_INLINE intptr_t endian_fix( intptr_t x )
190 {
191     if( WORD_SIZE == 8 )
192         return endian_fix32(x>>32) + ((uint64_t)endian_fix32(x)<<32);
193     else
194         return endian_fix32(x);
195 }
196 #endif
197 static ALWAYS_INLINE uint16_t endian_fix16( uint16_t x )
198 {
199     return (x<<8)|(x>>8);
200 }
201 #endif
202
203 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 3)
204 #define x264_clz(x) __builtin_clz(x)
205 #else
206 static int ALWAYS_INLINE x264_clz( uint32_t x )
207 {
208     static uint8_t lut[16] = {4,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0};
209     int y, z = ((x - 0x10000) >> 27) & 16;
210     x >>= z^16;
211     z += y = ((x - 0x100) >> 28) & 8;
212     x >>= y^8;
213     z += y = ((x - 0x10) >> 29) & 4;
214     x >>= y^4;
215     return z + lut[x];
216 }
217 #endif
218
219 #endif /* X264_OSDEP_H */