1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Portable macros, types and inlined assembly - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
|
* 2002 Peter Ross <pross@xvid.org> |
8 |
|
* 2002 Edouard Gomez <ed.gomez@free.fr> |
9 |
|
* |
10 |
|
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
11 |
|
* |
12 |
|
* XviD is free software; you can redistribute it and/or modify it |
13 |
|
* under the terms of the GNU General Public License as published by |
14 |
|
* the Free Software Foundation; either version 2 of the License, or |
15 |
|
* (at your option) any later version. |
16 |
|
* |
17 |
|
* This program is distributed in the hope that it will be useful, |
18 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 |
|
* GNU General Public License for more details. |
21 |
|
* |
22 |
|
* You should have received a copy of the GNU General Public License |
23 |
|
* along with this program; if not, write to the Free Software |
24 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
25 |
|
* |
26 |
|
* Under section 8 of the GNU General Public License, the copyright |
27 |
|
* holders of XVID explicitly forbid distribution in the following |
28 |
|
* countries: |
29 |
|
* |
30 |
|
* - Japan |
31 |
|
* - United States of America |
32 |
|
* |
33 |
|
* Linking XviD statically or dynamically with other modules is making a |
34 |
|
* combined work based on XviD. Thus, the terms and conditions of the |
35 |
|
* GNU General Public License cover the whole combination. |
36 |
|
* |
37 |
|
* As a special exception, the copyright holders of XviD give you |
38 |
|
* permission to link XviD with independent modules that communicate with |
39 |
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
40 |
|
* license terms of these independent modules, and to copy and distribute |
41 |
|
* the resulting combined work under terms of your choice, provided that |
42 |
|
* every copy of the combined work is accompanied by a complete copy of |
43 |
|
* the source code of XviD (the version of XviD used to produce the |
44 |
|
* combined work), being distributed under the terms of the GNU General |
45 |
|
* Public License plus this exception. An independent module is a module |
46 |
|
* which is not derived from or based on XviD. |
47 |
|
* |
48 |
|
* Note that people who make modified versions of XviD are not obligated |
49 |
|
* to grant this special exception for their modified versions; it is |
50 |
|
* their choice whether to do so. The GNU General Public License gives |
51 |
|
* permission to release a modified version without this exception; this |
52 |
|
* exception also makes it possible to release a modified version which |
53 |
|
* carries forward this exception. |
54 |
|
* |
55 |
|
* $Id: portab.h,v 1.41 2003-02-09 19:32:52 edgomez Exp $ |
56 |
|
* |
57 |
|
****************************************************************************/ |
58 |
|
|
59 |
#ifndef _PORTAB_H_ |
#ifndef _PORTAB_H_ |
60 |
#define _PORTAB_H_ |
#define _PORTAB_H_ |
61 |
|
|
62 |
#if defined(WIN32) |
/***************************************************************************** |
63 |
|
* Common things |
64 |
|
****************************************************************************/ |
65 |
|
|
66 |
|
/* Debug level masks */ |
67 |
|
#define DPRINTF_ERROR 0x00000001 |
68 |
|
#define DPRINTF_STARTCODE 0x00000002 |
69 |
|
#define DPRINTF_HEADER 0x00000004 |
70 |
|
#define DPRINTF_TIMECODE 0x00000008 |
71 |
|
#define DPRINTF_MB 0x00000010 |
72 |
|
#define DPRINTF_COEFF 0x00000020 |
73 |
|
#define DPRINTF_MV 0x00000040 |
74 |
|
#define DPRINTF_DEBUG 0x80000000 |
75 |
|
|
76 |
|
/* debug level for this library */ |
77 |
|
#define DPRINTF_LEVEL 0 |
78 |
|
|
79 |
|
/* Buffer size for non C99 compliant compilers (msvc) */ |
80 |
|
#define DPRINTF_BUF_SZ 1024 |
81 |
|
|
82 |
|
/***************************************************************************** |
83 |
|
* Types used in XviD sources |
84 |
|
****************************************************************************/ |
85 |
|
|
86 |
|
/*---------------------------------------------------------------------------- |
87 |
|
| For MSVC |
88 |
|
*---------------------------------------------------------------------------*/ |
89 |
|
|
90 |
#include <windows.h> |
#if defined(_MSC_VER) |
91 |
|
# define int8_t char |
92 |
|
# define uint8_t unsigned char |
93 |
|
# define int16_t short |
94 |
|
# define uint16_t unsigned short |
95 |
|
# define int32_t int |
96 |
|
# define uint32_t unsigned int |
97 |
|
# define int64_t __int64 |
98 |
|
# define uint64_t unsigned __int64 |
99 |
|
|
100 |
#ifdef _DEBUG |
/*---------------------------------------------------------------------------- |
101 |
#define DEBUG(S) OutputDebugString((S)); |
| For GNU C compatible compilers |
102 |
#define DEBUG1(S,I) { char tmp[100]; wsprintf(tmp, "%s %i\n", (S), (I)); OutputDebugString(tmp); } |
*---------------------------------------------------------------------------*/ |
|
#define DEBUG2(X,A,B) { char tmp[100]; wsprintf(tmp, "%s %i %i\n", (X), (A), (B)); OutputDebugString(tmp); } |
|
|
#define DEBUG3(X,A,B,C){ char tmp[1000]; wsprintf(tmp,"%s %i %i %i",(X),(A), (B), (C)); OutputDebugString(tmp); } |
|
|
#define DEBUG8(X,A,B,C,D,E,F,G,H){ char tmp[1000]; wsprintf(tmp,"%s %i %i %i %i %i %i %i %i",(X),(A),(B),(C),(D),(E),(F),(G),(H)); OutputDebugString(tmp); } |
|
|
#else |
|
|
#define DEBUG(S) |
|
|
#define DEBUG1(S,I) |
|
|
#define DEBUG2(X,A,B) |
|
|
#define DEBUG3(X,A,B,C) |
|
|
#define DEBUG8(X,A,B,C,D,E,F,G,H) |
|
|
#endif |
|
103 |
|
|
104 |
|
#elif defined(__GNUC__) || defined(__ICC) |
105 |
|
|
106 |
#define int8_t char |
#define int8_t char |
107 |
#define uint8_t unsigned char |
#define uint8_t unsigned char |
109 |
#define uint16_t unsigned short |
#define uint16_t unsigned short |
110 |
#define int32_t int |
#define int32_t int |
111 |
#define uint32_t unsigned int |
#define uint32_t unsigned int |
112 |
#define int64_t __int64 |
# define int64_t long long |
113 |
#define uint64_t unsigned __int64 |
# define uint64_t unsigned long long |
114 |
|
|
115 |
#define EMMS() __asm {emms} |
/*---------------------------------------------------------------------------- |
116 |
|
| Ok, we don't know how to define these types... error |
117 |
|
*---------------------------------------------------------------------------*/ |
118 |
|
|
119 |
|
#else |
120 |
|
# error Do not know how to define (u)int(size)_t types. |
121 |
|
#endif |
122 |
|
|
123 |
|
/***************************************************************************** |
124 |
|
* Some things that are only architecture dependant |
125 |
|
****************************************************************************/ |
126 |
|
|
127 |
|
#if defined(ARCH_IS_32BIT) |
128 |
#define CACHE_LINE 16 |
#define CACHE_LINE 16 |
129 |
|
# define ptr_t uint32_t |
130 |
|
#elif defined(ARCH_IS_64BIT) |
131 |
|
# define CACHE_LINE 32 |
132 |
|
# define ptr_t uint64_t |
133 |
|
#else |
134 |
|
# error You are trying to compile XviD without defining address bus size. |
135 |
|
#endif |
136 |
|
|
137 |
|
/***************************************************************************** |
138 |
|
* Things that must be sorted by compiler and then by architecture |
139 |
|
****************************************************************************/ |
140 |
|
|
141 |
|
/***************************************************************************** |
142 |
|
* MSVC compiler specific macros, functions |
143 |
|
****************************************************************************/ |
144 |
|
|
145 |
|
#if defined(_MSC_VER) |
146 |
|
|
147 |
|
/*---------------------------------------------------------------------------- |
148 |
|
| Common msvc stuff |
149 |
|
*---------------------------------------------------------------------------*/ |
150 |
|
|
151 |
|
#include <windows.h> |
152 |
|
#include <stdio.h> |
153 |
|
|
154 |
|
/* |
155 |
|
* This function must be declared/defined all the time because MSVC does |
156 |
|
* not support C99 variable arguments macros. |
157 |
|
* |
158 |
|
* Btw, if the MS compiler does its job well, it should remove the nop |
159 |
|
* DPRINTF function when not compiling in _DEBUG mode |
160 |
|
*/ |
161 |
|
# ifdef _DEBUG |
162 |
|
static __inline void DPRINTF(int level, char *fmt, ...) |
163 |
|
{ |
164 |
|
if (DPRINTF_LEVEL & level) { |
165 |
|
va_list args; |
166 |
|
char buf[DPRINTF_BUF_SZ]; |
167 |
|
va_start(args, fmt); |
168 |
|
vsprintf(buf, fmt, args); |
169 |
|
OutputDebugString(buf); |
170 |
|
fprintf(stderr, "%s\n", buf); |
171 |
|
} |
172 |
|
} |
173 |
|
# else |
174 |
|
static __inline void DPRINTF(int level, char *fmt, ...) |
175 |
|
{ |
176 |
|
} |
177 |
|
# endif |
178 |
|
|
179 |
#if _MSC_VER <= 1200 |
#if _MSC_VER <= 1200 |
180 |
#define CACHE_ALIGN |
# define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
181 |
|
type name##_storage[(sizex)*(sizey)+(alignment)-1]; \ |
182 |
|
type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1)) |
183 |
#else |
#else |
184 |
#define CACHE_ALIGN __declspec(align(CACHE_LINE)) |
# define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
185 |
|
__declspec(align(alignment)) type name[(sizex)*(sizey)] |
186 |
#endif |
#endif |
187 |
|
|
188 |
// needed for bitstream.h |
|
189 |
|
/*---------------------------------------------------------------------------- |
190 |
|
| msvc x86 specific macros/functions |
191 |
|
*---------------------------------------------------------------------------*/ |
192 |
|
# if defined(ARCH_IS_IA32) |
193 |
#define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax |
#define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax |
194 |
|
# define EMMS() __asm {emms} |
195 |
|
|
196 |
// needed for timer.c |
# ifdef _PROFILING_ |
197 |
static __inline int64_t read_counter() { |
static __inline int64_t read_counter(void) |
198 |
|
{ |
199 |
int64_t ts; |
int64_t ts; |
200 |
uint32_t ts1, ts2; |
uint32_t ts1, ts2; |
|
|
|
201 |
__asm { |
__asm { |
202 |
rdtsc |
rdtsc |
203 |
mov ts1, eax |
mov ts1, eax |
204 |
mov ts2, edx |
mov ts2, edx |
205 |
} |
} |
|
|
|
206 |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
|
|
|
207 |
return ts; |
return ts; |
208 |
} |
} |
209 |
|
# endif |
210 |
|
|
211 |
#elif defined(LINUX) || defined(DJGPP) |
/*---------------------------------------------------------------------------- |
212 |
|
| msvc GENERIC (plain C only) - Probably alpha or some embedded device |
213 |
|
*---------------------------------------------------------------------------*/ |
214 |
|
# elif defined(ARCH_IS_GENERIC) |
215 |
|
# define BSWAP(a) \ |
216 |
|
((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \ |
217 |
|
(((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff)) |
218 |
|
# define EMMS() |
219 |
|
|
220 |
#ifdef _DEBUG |
# ifdef _PROFILING_ |
221 |
|
# include <time.h> |
222 |
|
static __inline int64_t read_counter(void) |
223 |
|
{ |
224 |
|
return (int64_t)clock(); |
225 |
|
} |
226 |
|
# endif |
227 |
|
|
228 |
#include <stdio.h> |
/*---------------------------------------------------------------------------- |
229 |
#define DEBUG_WHERE stdout |
| msvc Not given architecture - This is probably an user who tries to build |
230 |
#define DEBUG(S) fprintf(DEBUG_WHERE, "%s\n", (S)); |
| XviD the wrong way. |
231 |
#define DEBUG1(S,I) fprintf(DEBUG_WHERE, "%s %i\n", (S), (I)) |
*---------------------------------------------------------------------------*/ |
232 |
#define DEBUG2(S,A,B) fprintf(DEBUG_WHERE, "%s%i=%i\n", (S), (A), (B)) |
# else |
233 |
#define DEBUG3(S,A,B,C) fprintf(DEBUG_WHERE, "%s %i %x %x\n", (S), (A), (B), (C)) |
# error You are trying to compile XviD without defining the architecture type. |
|
#define DEBUG8(S,A,B,C,D,E,F,G,H) |
|
|
#else |
|
|
#define DEBUG(S) |
|
|
#define DEBUG1(S,I) |
|
|
#define DEBUG2(X,A,B) |
|
|
#define DEBUG3(X,A,B,C) |
|
|
#define DEBUG8(X,A,B,C,D,E,F,G,H) |
|
234 |
#endif |
#endif |
235 |
|
|
|
#define CACHE_LINE 16 |
|
236 |
|
|
|
#if defined(LINUX) |
|
237 |
|
|
|
#include <stdint.h> |
|
238 |
|
|
239 |
#define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
/***************************************************************************** |
240 |
type name##_storage[(sizex)*(sizey)+(alignment)-1]; \ |
* GNU CC compiler stuff |
241 |
typedef type name##_sub[sizey]; \ |
****************************************************************************/ |
242 |
name##_sub * name = (void *) (((int32_t) name##_storage+(alignment)) & ~((int32_t)(alignment)-1)) |
|
243 |
|
#elif defined(__GNUC__) || defined(__ICC) /* Compiler test */ |
244 |
|
|
245 |
|
/*---------------------------------------------------------------------------- |
246 |
|
| Common gcc stuff |
247 |
|
*---------------------------------------------------------------------------*/ |
248 |
|
|
249 |
|
/* |
250 |
|
* As gcc is (mostly) C99 compliant, we define DPRINTF only if it's realy needed |
251 |
|
* and it's a macro calling fprintf directly |
252 |
|
*/ |
253 |
|
# ifdef _DEBUG |
254 |
|
|
255 |
#else |
/* Needed for all debuf fprintf calls */ |
256 |
|
# include <stdio.h> |
257 |
|
# include <stdarg.h> |
258 |
|
|
259 |
#define CACHE_ALIGN __attribute__ ((__aligned__(CACHE_LINE))) |
static __inline void DPRINTF(int level, char *format, ...) |
260 |
#define int8_t char |
{ |
261 |
#define uint8_t unsigned char |
va_list args; |
262 |
#define int16_t short |
va_start(args, format); |
263 |
#define uint16_t unsigned short |
if(DPRINTF_LEVEL & level) { |
264 |
#define int32_t int |
vfprintf(stderr, format, args); |
265 |
#define uint32_t unsigned int |
fprintf(stderr, "\n"); |
266 |
#define int64_t long long |
} |
267 |
#define uint64_t unsigned long long |
} |
268 |
|
|
269 |
#endif |
# else /* _DEBUG */ |
270 |
|
static __inline void DPRINTF(int level, char *format, ...) {} |
271 |
|
# endif /* _DEBUG */ |
272 |
|
|
273 |
|
|
274 |
// needed for bitstream.h |
# define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
275 |
#ifdef ARCH_PPC |
type name##_storage[(sizex)*(sizey)+(alignment)-1]; \ |
276 |
#define BSWAP(a) __asm__ ( "lwbrx %0,0,%1; eieio" : "=r" (a) : \ |
type * name = (type *) (((ptr_t) name##_storage+(alignment - 1)) & ~((ptr_t)(alignment)-1)) |
|
"r" (&(a)), "m" (a)); |
|
|
#define EMMS() |
|
|
#else |
|
|
#define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) ) |
|
|
#define EMMS() __asm__("emms\n\t") |
|
|
#endif |
|
277 |
|
|
278 |
// needed for timer.c |
/*---------------------------------------------------------------------------- |
279 |
static __inline int64_t read_counter() { |
| gcc IA32 specific macros/functions |
280 |
|
*---------------------------------------------------------------------------*/ |
281 |
|
# if defined(ARCH_IS_IA32) |
282 |
|
# define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) ); |
283 |
|
# define EMMS() __asm__ ("emms\n\t"); |
284 |
|
|
285 |
|
# ifdef _PROFILING_ |
286 |
|
static __inline int64_t read_counter(void) |
287 |
|
{ |
288 |
int64_t ts; |
int64_t ts; |
289 |
uint32_t ts1, ts2; |
uint32_t ts1, ts2; |
|
|
|
290 |
__asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2)); |
__asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2)); |
|
|
|
291 |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
|
|
|
292 |
return ts; |
return ts; |
293 |
} |
} |
294 |
|
# endif |
295 |
|
|
296 |
#else // OTHER OS |
/*---------------------------------------------------------------------------- |
297 |
|
| gcc PPC and PPC Altivec specific macros/functions |
298 |
|
*---------------------------------------------------------------------------*/ |
299 |
|
# elif defined(ARCH_IS_PPC) |
300 |
|
# define BSWAP(a) __asm__ __volatile__ \ |
301 |
|
( "lwbrx %0,0,%1; eieio" : "=r" (a) : "r" (&(a)), "m" (a)); |
302 |
|
# define EMMS() |
303 |
|
|
304 |
|
# ifdef _PROFILING_ |
305 |
|
static __inline unsigned long get_tbl(void) |
306 |
|
{ |
307 |
|
unsigned long tbl; |
308 |
|
asm volatile ("mftb %0":"=r" (tbl)); |
309 |
|
return tbl; |
310 |
|
} |
311 |
|
|
312 |
#define DEBUG(S) |
static __inline unsigned long get_tbu(void) |
313 |
#define DEBUG1(S,I) |
{ |
314 |
#define DEBUG2(X,A,B) |
unsigned long tbl; |
315 |
#define DEBUG3(X,A,B,C) |
asm volatile ("mftbu %0":"=r" (tbl)); |
316 |
#define DEBUG8(X,A,B,C,D,E,F,G,H) |
return tbl; |
317 |
|
} |
318 |
|
|
319 |
#include <inttypes.h> |
static __inline int64_t read_counter(void) |
320 |
|
{ |
321 |
|
unsigned long tb, tu; |
322 |
|
do { |
323 |
|
tu = get_tbu(); |
324 |
|
tb = get_tbl(); |
325 |
|
}while (tb != get_tbl()); |
326 |
|
return (((int64_t) tu) << 32) | (int64_t) tb; |
327 |
|
} |
328 |
|
# endif |
329 |
|
|
330 |
|
/*---------------------------------------------------------------------------- |
331 |
|
| gcc IA64 specific macros/functions |
332 |
|
*---------------------------------------------------------------------------*/ |
333 |
|
# elif defined(ARCH_IS_IA64) |
334 |
|
# define BSWAP(a) __asm__ __volatile__ \ |
335 |
|
("mux1 %1 = %0, @rev" ";;" \ |
336 |
|
"shr.u %1 = %1, 32" : "=r" (a) : "r" (a)); |
337 |
#define EMMS() |
#define EMMS() |
338 |
|
|
339 |
// needed for bitstream.h |
# ifdef _PROFILING_ |
340 |
|
static __inline int64_t read_counter(void) |
341 |
|
{ |
342 |
|
unsigned long result; |
343 |
|
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
344 |
|
return result; |
345 |
|
} |
346 |
|
# endif |
347 |
|
|
348 |
|
/*---------------------------------------------------------------------------- |
349 |
|
| gcc GENERIC (plain C only) specific macros/functions |
350 |
|
*---------------------------------------------------------------------------*/ |
351 |
|
# elif defined(ARCH_IS_GENERIC) |
352 |
#define BSWAP(a) \ |
#define BSWAP(a) \ |
353 |
((a) = ( ((a)&0xff)<<24) | (((a)&0xff00)<<8) | (((a)>>8)&0xff00) | (((a)>>24)&0xff)) |
((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \ |
354 |
|
(((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff)) |
355 |
|
# define EMMS() |
356 |
|
|
357 |
// rdtsc command most likely not supported, |
# ifdef _PROFILING_ |
358 |
// so just dummy code here |
# include <time.h> |
359 |
static __inline int64_t read_counter() { |
static __inline int64_t read_counter(void) |
360 |
return 0; |
{ |
361 |
|
return (int64_t)clock(); |
362 |
} |
} |
363 |
|
# endif |
364 |
|
|
365 |
#define CACHE_LINE 16 |
/*---------------------------------------------------------------------------- |
366 |
#define CACHE_ALIGN |
| gcc Not given architecture - This is probably an user who tries to build |
367 |
|
| XviD the wrong way. |
368 |
|
*---------------------------------------------------------------------------*/ |
369 |
|
# else |
370 |
|
# error You are trying to compile XviD without defining the architecture type. |
371 |
#endif |
#endif |
372 |
|
|
373 |
#endif // _PORTAB_H_ |
/***************************************************************************** |
374 |
|
* Unknown compiler |
375 |
|
****************************************************************************/ |
376 |
|
#else /* Compiler test */ |
377 |
|
|
378 |
|
# error Compiler not supported |
379 |
|
|
380 |
|
#endif /* Compiler test */ |
381 |
|
|
382 |
|
|
383 |
|
#endif |