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@wanadoo.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$ |
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 |
#include <windows.h> |
****************************************************************************/ |
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_RC 0x00000080 |
75 |
|
#define DPRINTF_DEBUG 0x80000000 |
76 |
|
|
77 |
|
/* debug level for this library */ |
78 |
#ifdef _DEBUG |
#ifdef _DEBUG |
79 |
#define DEBUG(S) OutputDebugString((S)); |
#define DPRINTF_LEVEL (DPRINTF_RC|0x0000000f) |
80 |
#define DEBUG1(S,I) { char tmp[100]; wsprintf(tmp, "%s %i\n", (S), (I)); OutputDebugString(tmp); } |
#else |
81 |
#define DEBUG2(X,A,B) { char tmp[100]; wsprintf(tmp, "%s %i %i\n", (X), (A), (B)); OutputDebugString(tmp); } |
#define DPRINTF_LEVEL DPRINTF_RC |
|
#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) |
|
82 |
#endif |
#endif |
83 |
|
|
84 |
|
/* Buffer size for non C99 compliant compilers (msvc) */ |
85 |
|
#define DPRINTF_BUF_SZ 1024 |
86 |
|
|
87 |
|
/***************************************************************************** |
88 |
|
* Types used in XviD sources |
89 |
|
****************************************************************************/ |
90 |
|
|
91 |
|
/*---------------------------------------------------------------------------- |
92 |
|
| Standard Unix include file (sorry, we put all unix into "linux" case) |
93 |
|
*---------------------------------------------------------------------------*/ |
94 |
|
|
95 |
|
#if defined(LINUX) || defined(BEOS) || defined(FREEBSD) |
96 |
|
|
97 |
|
/* All (u)int(size)_t types are defined here */ |
98 |
|
# include <inttypes.h> |
99 |
|
|
100 |
|
/*---------------------------------------------------------------------------- |
101 |
|
| msvc (lacks such a header file) |
102 |
|
*---------------------------------------------------------------------------*/ |
103 |
|
|
104 |
|
#elif defined(_MSC_VER) |
105 |
#define int8_t char |
#define int8_t char |
106 |
#define uint8_t unsigned char |
#define uint8_t unsigned char |
107 |
#define int16_t short |
#define int16_t short |
111 |
#define int64_t __int64 |
#define int64_t __int64 |
112 |
#define uint64_t unsigned __int64 |
#define uint64_t unsigned __int64 |
113 |
|
|
114 |
#define EMMS() __asm {emms} |
/*---------------------------------------------------------------------------- |
115 |
|
| Fallback when using gcc |
116 |
|
*---------------------------------------------------------------------------*/ |
117 |
|
|
118 |
|
#elif defined(__GNUC__) || defined(__ICC) |
119 |
|
|
120 |
|
# define int8_t char |
121 |
|
# define uint8_t unsigned char |
122 |
|
# define int16_t short |
123 |
|
# define uint16_t unsigned short |
124 |
|
# define int32_t int |
125 |
|
# define uint32_t unsigned int |
126 |
|
# define int64_t long long |
127 |
|
# define uint64_t unsigned long long |
128 |
|
|
129 |
|
/*---------------------------------------------------------------------------- |
130 |
|
| Ok, we don't know how to define these types... error |
131 |
|
*---------------------------------------------------------------------------*/ |
132 |
|
|
133 |
|
#else |
134 |
|
# error Do not know how to define (u)int(size)_t types |
135 |
|
#endif |
136 |
|
|
137 |
|
/***************************************************************************** |
138 |
|
* Some things that are only architecture dependant |
139 |
|
****************************************************************************/ |
140 |
|
|
141 |
|
#if defined(ARCH_X86) || defined(ARCH_PPC) || defined(ARCH_MIPS) || defined(ARCH_SPARC) |
142 |
|
# define CACHE_LINE 16 |
143 |
|
# define ptr_t uint32_t |
144 |
|
#elif defined(ARCH_IA64) |
145 |
|
# define CACHE_LINE 32 |
146 |
|
# define ptr_t uint64_t |
147 |
|
#else |
148 |
|
/* todo: fix cache_line 0 operation */ |
149 |
#define CACHE_LINE 16 |
#define CACHE_LINE 16 |
150 |
|
# define ptr_t uint32_t |
151 |
|
#endif |
152 |
|
|
153 |
|
/***************************************************************************** |
154 |
|
* Things that must be sorted by compiler and then by architecture |
155 |
|
****************************************************************************/ |
156 |
|
|
157 |
|
/***************************************************************************** |
158 |
|
* MSVC compiler specific macros, functions |
159 |
|
****************************************************************************/ |
160 |
|
|
161 |
|
#if defined(_MSC_VER) |
162 |
|
|
163 |
|
/*---------------------------------------------------------------------------- |
164 |
|
| Common msvc stuff |
165 |
|
*---------------------------------------------------------------------------*/ |
166 |
|
|
167 |
|
#include <windows.h> |
168 |
|
#include <stdio.h> |
169 |
|
|
170 |
|
/* |
171 |
|
* This function must be declared/defined all the time because MSVC does |
172 |
|
* not support C99 variable arguments macros |
173 |
|
*/ |
174 |
|
static __inline void DPRINTF(int level, char *fmt, ...) |
175 |
|
{ |
176 |
|
if (DPRINTF_LEVEL & level) { |
177 |
|
va_list args; |
178 |
|
char buf[DPRINTF_BUF_SZ]; |
179 |
|
va_start(args, fmt); |
180 |
|
vsprintf(buf, fmt, args); |
181 |
|
OutputDebugString(buf); |
182 |
|
fprintf(stderr, "%s\n", buf); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
#if _MSC_VER <= 1200 |
#if _MSC_VER <= 1200 |
187 |
#define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
#define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
192 |
__declspec(align(alignment)) type name[(sizex)*(sizey)] |
__declspec(align(alignment)) type name[(sizex)*(sizey)] |
193 |
#endif |
#endif |
194 |
|
|
195 |
// needed for bitstream.h |
|
196 |
|
/*---------------------------------------------------------------------------- |
197 |
|
| msvc x86 specific macros/functions |
198 |
|
*---------------------------------------------------------------------------*/ |
199 |
|
# if defined(ARCH_X86) |
200 |
#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 |
201 |
|
# define EMMS() __asm {emms} |
202 |
|
# define FEMMS() __asm {femms} |
203 |
|
|
204 |
// needed for timer.c |
static __inline int64_t read_counter(void) |
205 |
static __inline int64_t read_counter() { |
{ |
206 |
int64_t ts; |
int64_t ts; |
207 |
uint32_t ts1, ts2; |
uint32_t ts1, ts2; |
|
|
|
208 |
__asm { |
__asm { |
209 |
rdtsc |
rdtsc |
210 |
mov ts1, eax |
mov ts1, eax |
211 |
mov ts2, edx |
mov ts2, edx |
212 |
} |
} |
|
|
|
213 |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
|
|
|
214 |
return ts; |
return ts; |
215 |
} |
} |
216 |
|
|
217 |
#elif defined(LINUX) || defined(DJGPP) |
/*---------------------------------------------------------------------------- |
218 |
|
| msvc unknown architecture |
219 |
|
*---------------------------------------------------------------------------*/ |
220 |
|
# else |
221 |
|
/* ANSI C version of BSWAP */ |
222 |
|
#define BSWAP(x) \ |
223 |
|
x = ((((x) & 0xff000000) >> 24) | \ |
224 |
|
(((x) & 0x00ff0000) >> 8) | \ |
225 |
|
(((x) & 0x0000ff00) << 8) | \ |
226 |
|
(((x) & 0x000000ff) << 24)) |
227 |
|
|
228 |
|
#define EMMS() |
229 |
|
#define FEMMS() |
230 |
|
# endif |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
/***************************************************************************** |
236 |
|
* GNU CC compiler stuff |
237 |
|
****************************************************************************/ |
238 |
|
|
239 |
|
#elif defined(__GNUC__) || defined(__ICC) /* Compiler test */ |
240 |
|
|
241 |
|
/*---------------------------------------------------------------------------- |
242 |
|
| Common gcc stuff |
243 |
|
*---------------------------------------------------------------------------*/ |
244 |
|
|
245 |
|
/* |
246 |
|
* As gcc is (mostly) C99 compliant, we define DPRINTF only if it's realy needed |
247 |
|
* and it's a macro calling fprintf directly |
248 |
|
*/ |
249 |
#ifdef _DEBUG |
#ifdef _DEBUG |
250 |
|
|
251 |
|
/* Needed for all debuf fprintf calls */ |
252 |
#include <stdio.h> |
#include <stdio.h> |
|
#define DEBUG_WHERE stdout |
|
|
#define DEBUG(S) fprintf(DEBUG_WHERE, "%s\n", (S)); |
|
|
#define DEBUG1(S,I) fprintf(DEBUG_WHERE, "%s %i\n", (S), (I)) |
|
|
#define DEBUG2(S,A,B) fprintf(DEBUG_WHERE, "%s%i=%i\n", (S), (A), (B)) |
|
|
#define DEBUG3(S,A,B,C) fprintf(DEBUG_WHERE, "%s %i %x %x\n", (S), (A), (B), (C)) |
|
|
#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) |
|
|
#endif |
|
253 |
|
|
254 |
#define CACHE_LINE 16 |
# define DPRINTF(level, format, ...) \ |
255 |
|
do {\ |
256 |
|
if(DPRINTF_LEVEL & level)\ |
257 |
|
fprintf(stderr, format"\n", ##__VA_ARGS__);\ |
258 |
|
}while(0); |
259 |
|
|
260 |
|
# else /* _DEBUG */ |
261 |
|
# define DPRINTF(level, format, ...) |
262 |
|
# endif /* _DEBUG */ |
263 |
|
|
|
#if defined(LINUX) |
|
264 |
|
|
|
#include <stdint.h> |
|
265 |
|
|
266 |
#define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
#define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
267 |
type name##_storage[(sizex)*(sizey)+(alignment)-1]; \ |
type name##_storage[(sizex)*(sizey)+(alignment)-1]; \ |
268 |
type * name = (type *) (((int32_t) name##_storage+(alignment - 1)) & ~((int32_t)(alignment)-1)) |
type * name = (type *) (((ptr_t) name##_storage+(alignment - 1)) & ~((ptr_t)(alignment)-1)) |
|
|
|
|
#else |
|
269 |
|
|
270 |
#define #define #define DECLARE_ALIGNED_MATRIX(name,sizex,sizey,type,alignment) \ |
/*---------------------------------------------------------------------------- |
271 |
__attribute__ ((__aligned__(CACHE_LINE))) type name[(sizex)*(sizey)] |
| gcc x86 specific macros/functions |
272 |
|
*---------------------------------------------------------------------------*/ |
273 |
#define int8_t char |
# if defined(ARCH_X86) |
274 |
#define uint8_t unsigned char |
# define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) ); |
275 |
#define int16_t short |
# define EMMS() __asm__ ("emms\n\t"); |
276 |
#define uint16_t unsigned short |
# define FEMMS() __asm__ ("femms\n\t"); |
|
#define int32_t int |
|
|
#define uint32_t unsigned int |
|
|
#define int64_t long long |
|
|
#define uint64_t unsigned long long |
|
|
|
|
|
#endif |
|
277 |
|
|
278 |
|
static __inline int64_t read_counter(void) |
279 |
|
{ |
280 |
|
int64_t ts; |
281 |
|
uint32_t ts1, ts2; |
282 |
|
__asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2)); |
283 |
|
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
284 |
|
return ts; |
285 |
|
} |
286 |
|
|
287 |
// needed for bitstream.h |
/*---------------------------------------------------------------------------- |
288 |
#ifdef ARCH_PPC |
| gcc PPC and PPC Altivec specific macros/functions |
289 |
#define BSWAP(a) __asm__ __volatile__ ( "lwbrx %0,0,%1; eieio" : "=r" (a) : \ |
*---------------------------------------------------------------------------*/ |
290 |
"r" (&(a)), "m" (a)); |
# elif defined(ARCH_PPC) |
291 |
|
# define BSWAP(a) __asm__ __volatile__ \ |
292 |
|
( "lwbrx %0,0,%1; eieio" : "=r" (a) : "r" (&(a)), "m" (a)); |
293 |
#define EMMS() |
#define EMMS() |
294 |
|
# define FEMMS() |
295 |
|
|
296 |
static __inline unsigned long get_tbl(void) { |
static __inline unsigned long get_tbl(void) |
297 |
|
{ |
298 |
unsigned long tbl; |
unsigned long tbl; |
299 |
asm volatile("mftb %0" : "=r" (tbl)); |
asm volatile("mftb %0" : "=r" (tbl)); |
300 |
return tbl; |
return tbl; |
301 |
} |
} |
302 |
static __inline unsigned long get_tbu(void) { |
|
303 |
|
static __inline unsigned long get_tbu(void) |
304 |
|
{ |
305 |
unsigned long tbl; |
unsigned long tbl; |
306 |
asm volatile("mftbu %0" : "=r" (tbl)); |
asm volatile("mftbu %0" : "=r" (tbl)); |
307 |
return tbl; |
return tbl; |
308 |
} |
} |
309 |
static __inline int64_t read_counter() { |
|
310 |
|
static __inline int64_t read_counter(void) |
311 |
|
{ |
312 |
unsigned long tb, tu; |
unsigned long tb, tu; |
313 |
do { |
do { |
314 |
tu = get_tbu(); |
tu = get_tbu(); |
316 |
} while(tb != get_tbl()); |
} while(tb != get_tbl()); |
317 |
return (((int64_t)tu) << 32) | (int64_t)tb; |
return (((int64_t)tu) << 32) | (int64_t)tb; |
318 |
} |
} |
|
#else |
|
|
#define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) ) |
|
|
#define EMMS() __asm__("emms\n\t") |
|
|
|
|
319 |
|
|
320 |
// needed for timer.c |
/*---------------------------------------------------------------------------- |
321 |
static __inline int64_t read_counter() { |
| gcc IA64 specific macros/functions |
322 |
int64_t ts; |
*---------------------------------------------------------------------------*/ |
323 |
uint32_t ts1, ts2; |
# elif defined(ARCH_IA64) |
324 |
|
# define BSWAP(a) __asm__ __volatile__ \ |
325 |
|
("mux1 %1 = %0, @rev" ";;" \ |
326 |
|
"shr.u %1 = %1, 32" : "=r" (a) : "r" (a)); |
327 |
|
# define EMMS() |
328 |
|
# define FEMMS() |
329 |
|
|
330 |
__asm__ __volatile__("rdtsc\n\t":"=a"(ts1), "=d"(ts2)); |
static __inline int64_t read_counter(void) { |
331 |
|
unsigned long result; |
332 |
|
__asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory"); |
333 |
|
return result; |
334 |
|
} |
335 |
|
|
336 |
ts = ((uint64_t) ts2 << 32) | ((uint64_t) ts1); |
/*---------------------------------------------------------------------------- |
337 |
|
| gcc SPARC specific macros/functions |
338 |
|
*---------------------------------------------------------------------------*/ |
339 |
|
# elif defined(ARCH_SPARC) |
340 |
|
# define BSWAP(a) \ |
341 |
|
((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \ |
342 |
|
(((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff)) |
343 |
|
# define EMMS() |
344 |
|
# define FEMMS() |
345 |
|
|
346 |
return ts; |
static __inline int64_t read_counter(void) |
347 |
|
{ |
348 |
|
return 0; |
349 |
} |
} |
350 |
|
|
351 |
#endif |
/*---------------------------------------------------------------------------- |
352 |
|
| gcc MIPS specific macros/functions |
353 |
#else // OTHER OS |
*---------------------------------------------------------------------------*/ |
354 |
|
# elif defined(ARCH_MIPS) |
355 |
|
# define BSWAP(a) \ |
356 |
|
((a) = (((a) & 0xff) << 24) | (((a) & 0xff00) << 8) | \ |
357 |
|
(((a) >> 8) & 0xff00) | (((a) >> 24) & 0xff)) |
358 |
|
# define EMMS() |
359 |
|
# define FEMMS() |
360 |
|
|
361 |
#define DEBUG(S) |
static __inline int64_t read_counter(void) |
362 |
#define DEBUG1(S,I) |
{ |
363 |
#define DEBUG2(X,A,B) |
return 0; |
364 |
#define DEBUG3(X,A,B,C) |
} |
|
#define DEBUG8(X,A,B,C,D,E,F,G,H) |
|
365 |
|
|
366 |
#include <inttypes.h> |
/*---------------------------------------------------------------------------- |
367 |
|
| XviD + gcc unsupported Architecture |
368 |
|
*---------------------------------------------------------------------------*/ |
369 |
|
# else |
370 |
|
/* ANSI C version of BSWAP */ |
371 |
|
#define BSWAP(x) \ |
372 |
|
x = ((((x) & 0xff000000) >> 24) | \ |
373 |
|
(((x) & 0x00ff0000) >> 8) | \ |
374 |
|
(((x) & 0x0000ff00) << 8) | \ |
375 |
|
(((x) & 0x000000ff) << 24)) |
376 |
|
|
377 |
#define EMMS() |
#define EMMS() |
378 |
|
#define FEMMS() |
379 |
|
# endif /* Architecture checking */ |
380 |
|
|
381 |
// needed for bitstream.h |
/***************************************************************************** |
382 |
#define BSWAP(a) \ |
* Unknown compiler |
383 |
((a) = ( ((a)&0xff)<<24) | (((a)&0xff00)<<8) | (((a)>>8)&0xff00) | (((a)>>24)&0xff)) |
****************************************************************************/ |
384 |
|
#else /* Compiler test */ |
|
// rdtsc command most likely not supported, |
|
|
// so just dummy code here |
|
|
static __inline int64_t read_counter() { |
|
|
return 0; |
|
|
} |
|
385 |
|
|
386 |
#define CACHE_LINE 16 |
# error Compiler not supported |
|
#define CACHE_ALIGN |
|
387 |
|
|
388 |
#endif |
#endif /* Compiler test */ |
389 |
|
|
|
#endif // _PORTAB_H_ |
|
390 |
|
|
391 |
|
#endif |