[?1034h# HG changeset patch
# User Petri Hintukainen <phintuka@users.sourceforge.net>
# Date 1183507798 -10800
# Node ID 79a96eefa515c2a31436b95f5df0f0bd5a183247
# Parent  268ad8e747c8112e8b6860b73bc1425a3b0e9dd4
Update idct + mc from libmpeg2 0.4.1
- only "Make the IDCT input be preshifted by 4" part of libmpeg2 svn changeset 944 is missing

diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/Makefile.am
--- a/src/libmpeg2/Makefile.am	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/Makefile.am	Wed Jul 04 03:09:58 2007 +0300
@@ -8,13 +8,13 @@ xineplug_decode_mpeg2_la_SOURCES = \
 	header.c \
 	idct.c \
 	idct_altivec.c \
-	idct_mlib.c \
 	idct_mmx.c \
+	idct_alpha.c \
 	motion_comp.c \
 	motion_comp_altivec.c \
 	motion_comp_mmx.c \
-	motion_comp_mlib.c \
 	motion_comp_vis.c \
+	motion_comp_alpha.c \
 	slice.c \
 	slice_xvmc.c \
 	slice_xvmc_vld.c \
@@ -26,5 +26,5 @@ xineplug_decode_mpeg2_la_CFLAGS = $(VISI
 xineplug_decode_mpeg2_la_CFLAGS = $(VISIBILITY_FLAG) $(LIBMPEG2_CFLAGS)
 xineplug_decode_mpeg2_la_LDFLAGS = $(xineplug_ldflags)
 
-noinst_HEADERS = vlc.h mpeg2.h xvmc.h xvmc_vld.h mpeg2_internal.h idct_mlib.h vis.h \
-	libmpeg2_accel.h
+noinst_HEADERS = vlc.h mpeg2.h xvmc.h xvmc_vld.h mpeg2_internal.h vis.h \
+	libmpeg2_accel.h alpha_asm.h
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/alpha_asm.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmpeg2/alpha_asm.h	Wed Jul 04 03:09:58 2007 +0300
@@ -0,0 +1,181 @@
+/*
+ * Alpha assembly macros
+ * Copyright (c) 2002-2003 Falk Hueffner <falk@debian.org>
+ *
+ * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
+ * See http://libmpeg2.sourceforge.net/ for updates.
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307	 USA
+ */
+
+#ifndef ALPHA_ASM_H
+#define ALPHA_ASM_H
+
+#include <inttypes.h>
+
+#if defined __GNUC__
+# define GNUC_PREREQ(maj, min) \
+        ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
+#else
+# define GNUC_PREREQ(maj, min) 0
+#endif
+
+#define AMASK_BWX (1 << 0)
+#define AMASK_FIX (1 << 1)
+#define AMASK_CIX (1 << 2)
+#define AMASK_MVI (1 << 8)
+
+#ifdef __alpha_bwx__
+# define HAVE_BWX() 1
+#else
+# define HAVE_BWX() (amask(AMASK_BWX) == 0)
+#endif
+#ifdef __alpha_fix__
+# define HAVE_FIX() 1
+#else
+# define HAVE_FIX() (amask(AMASK_FIX) == 0)
+#endif
+#ifdef __alpha_max__
+# define HAVE_MVI() 1
+#else
+# define HAVE_MVI() (amask(AMASK_MVI) == 0)
+#endif
+#ifdef __alpha_cix__
+# define HAVE_CIX() 1
+#else
+# define HAVE_CIX() (amask(AMASK_CIX) == 0)
+#endif
+
+inline static uint64_t BYTE_VEC(uint64_t x)
+{
+    x |= x <<  8;
+    x |= x << 16;
+    x |= x << 32;
+    return x;
+}
+inline static uint64_t WORD_VEC(uint64_t x)
+{
+    x |= x << 16;
+    x |= x << 32;
+    return x;
+}
+
+#define ldq(p) (*(const uint64_t *) (p))
+#define ldl(p) (*(const int32_t *) (p))
+#define stl(l, p) do { *(uint32_t *) (p) = (l); } while (0)
+#define stq(l, p) do { *(uint64_t *) (p) = (l); } while (0)
+#define sextw(x) ((int16_t) (x))
+
+#ifdef __GNUC__
+struct unaligned_long { uint64_t l; } __attribute__((packed));
+#define ldq_u(p)     (*(const uint64_t *) (((uint64_t) (p)) & ~7ul))
+#define uldq(a)	     (((const struct unaligned_long *) (a))->l)
+
+#if GNUC_PREREQ(3,3)
+#define prefetch(p)     __builtin_prefetch((p), 0, 1)
+#define prefetch_en(p)  __builtin_prefetch((p), 0, 0)
+#define prefetch_m(p)   __builtin_prefetch((p), 1, 1)
+#define prefetch_men(p) __builtin_prefetch((p), 1, 0)
+#define cmpbge	__builtin_alpha_cmpbge
+/* Avoid warnings.  */
+#define extql(a, b)	__builtin_alpha_extql(a, (uint64_t) (b))
+#define extwl(a, b)	__builtin_alpha_extwl(a, (uint64_t) (b))
+#define extqh(a, b)	__builtin_alpha_extqh(a, (uint64_t) (b))
+#define zap	__builtin_alpha_zap
+#define zapnot	__builtin_alpha_zapnot
+#define amask	__builtin_alpha_amask
+#define implver	__builtin_alpha_implver
+#define rpcc	__builtin_alpha_rpcc
+#else
+#define prefetch(p)     asm volatile("ldl $31,%0"  : : "m"(*(const char *) (p)) : "memory")
+#define prefetch_en(p)  asm volatile("ldq $31,%0"  : : "m"(*(const char *) (p)) : "memory")
+#define prefetch_m(p)   asm volatile("lds $f31,%0" : : "m"(*(const char *) (p)) : "memory")
+#define prefetch_men(p) asm volatile("ldt $f31,%0" : : "m"(*(const char *) (p)) : "memory")
+#define cmpbge(a, b) ({ uint64_t __r; asm ("cmpbge  %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define extql(a, b)  ({ uint64_t __r; asm ("extql   %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define extwl(a, b)  ({ uint64_t __r; asm ("extwl   %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define extqh(a, b)  ({ uint64_t __r; asm ("extqh   %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define zap(a, b)    ({ uint64_t __r; asm ("zap     %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define zapnot(a, b) ({ uint64_t __r; asm ("zapnot  %r1,%2,%0"  : "=r" (__r) : "rJ"  (a), "rI" (b)); __r; })
+#define amask(a)     ({ uint64_t __r; asm ("amask   %1,%0"      : "=r" (__r) : "rI"  (a));	     __r; })
+#define implver()    ({ uint64_t __r; asm ("implver %0"         : "=r" (__r));			     __r; })
+#define rpcc()	     ({ uint64_t __r; asm volatile ("rpcc %0"   : "=r" (__r));			     __r; })
+#endif
+#define wh64(p) asm volatile("wh64 (%0)" : : "r"(p) : "memory")
+
+#if GNUC_PREREQ(3,3) && defined(__alpha_max__)
+#define minub8	__builtin_alpha_minub8
+#define minsb8	__builtin_alpha_minsb8
+#define minuw4	__builtin_alpha_minuw4
+#define minsw4	__builtin_alpha_minsw4
+#define maxub8	__builtin_alpha_maxub8
+#define maxsb8	__builtin_alpha_maxsb8
+#define maxuw4	__builtin_alpha_maxuw4	
+#define maxsw4	__builtin_alpha_maxsw4
+#define perr	__builtin_alpha_perr
+#define pklb	__builtin_alpha_pklb
+#define pkwb	__builtin_alpha_pkwb
+#define unpkbl	__builtin_alpha_unpkbl
+#define unpkbw	__builtin_alpha_unpkbw
+#else
+#define minub8(a, b) ({ uint64_t __r; asm (".arch ev6; minub8  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define minsb8(a, b) ({ uint64_t __r; asm (".arch ev6; minsb8  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define minuw4(a, b) ({ uint64_t __r; asm (".arch ev6; minuw4  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define minsw4(a, b) ({ uint64_t __r; asm (".arch ev6; minsw4  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define maxub8(a, b) ({ uint64_t __r; asm (".arch ev6; maxub8  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define maxsb8(a, b) ({ uint64_t __r; asm (".arch ev6; maxsb8  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define maxuw4(a, b) ({ uint64_t __r; asm (".arch ev6; maxuw4  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define maxsw4(a, b) ({ uint64_t __r; asm (".arch ev6; maxsw4  %r1,%2,%0"  : "=r" (__r) : "%rJ" (a), "rI" (b)); __r; })
+#define perr(a, b)   ({ uint64_t __r; asm (".arch ev6; perr    %r1,%r2,%0" : "=r" (__r) : "%rJ" (a), "rJ" (b)); __r; })
+#define pklb(a)      ({ uint64_t __r; asm (".arch ev6; pklb    %r1,%0"     : "=r" (__r) : "rJ"  (a));	     __r; })
+#define pkwb(a)      ({ uint64_t __r; asm (".arch ev6; pkwb    %r1,%0"     : "=r" (__r) : "rJ"  (a));	     __r; })
+#define unpkbl(a)    ({ uint64_t __r; asm (".arch ev6; unpkbl  %r1,%0"     : "=r" (__r) : "rJ"  (a));	     __r; })
+#define unpkbw(a)    ({ uint64_t __r; asm (".arch ev6; unpkbw  %r1,%0"     : "=r" (__r) : "rJ"  (a));	     __r; })
+#endif
+
+#elif defined(__DECC)		/* Digital/Compaq/hp "ccc" compiler */
+
+#include <c_asm.h>
+#define ldq_u(a)     asm ("ldq_u   %v0,0(%a0)", a)
+#define uldq(a)	     (*(const __unaligned uint64_t *) (a))
+#define cmpbge(a, b) asm ("cmpbge  %a0,%a1,%v0", a, b)
+#define extql(a, b)  asm ("extql   %a0,%a1,%v0", a, b)
+#define extwl(a, b)  asm ("extwl   %a0,%a1,%v0", a, b)
+#define extqh(a, b)  asm ("extqh   %a0,%a1,%v0", a, b)
+#define zap(a, b)    asm ("zap     %a0,%a1,%v0", a, b)
+#define zapnot(a, b) asm ("zapnot  %a0,%a1,%v0", a, b)
+#define amask(a)     asm ("amask   %a0,%v0", a)
+#define implver()    asm ("implver %v0")
+#define rpcc()	     asm ("rpcc	   %v0")
+#define minub8(a, b) asm ("minub8  %a0,%a1,%v0", a, b)
+#define minsb8(a, b) asm ("minsb8  %a0,%a1,%v0", a, b)
+#define minuw4(a, b) asm ("minuw4  %a0,%a1,%v0", a, b)
+#define minsw4(a, b) asm ("minsw4  %a0,%a1,%v0", a, b)
+#define maxub8(a, b) asm ("maxub8  %a0,%a1,%v0", a, b)
+#define maxsb8(a, b) asm ("maxsb8  %a0,%a1,%v0", a, b)
+#define maxuw4(a, b) asm ("maxuw4  %a0,%a1,%v0", a, b)
+#define maxsw4(a, b) asm ("maxsw4  %a0,%a1,%v0", a, b)
+#define perr(a, b)   asm ("perr    %a0,%a1,%v0", a, b)
+#define pklb(a)      asm ("pklb    %a0,%v0", a)
+#define pkwb(a)      asm ("pkwb    %a0,%v0", a)
+#define unpkbl(a)    asm ("unpkbl  %a0,%v0", a)
+#define unpkbw(a)    asm ("unpkbw  %a0,%v0", a)
+#define wh64(a)      asm ("wh64    %a0", a)
+
+#else
+#error "Unknown compiler!"
+#endif
+
+#endif /* ALPHA_ASM_H */
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/attributes.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmpeg2/attributes.h	Wed Jul 04 03:09:58 2007 +0300
@@ -0,0 +1,44 @@
+/*
+ * attributes.h
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
+ *
+ * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
+ * See http://libmpeg2.sourceforge.net/ for updates.
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "../xine-utils/attributes.h" /* hmm... */
+
+#if 0
+
+/* use gcc attribs to align critical data structures */
+#ifdef ATTRIBUTE_ALIGNED_MAX
+#define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
+#else
+#define ATTR_ALIGN(align)
+#endif
+
+#endif
+
+#ifdef HAVE_BUILTIN_EXPECT
+#define likely(x) __builtin_expect ((x) != 0, 1)
+#define unlikely(x) __builtin_expect ((x) != 0, 0)
+#else
+#define likely(x) (x)
+#define unlikely(x) (x)
+#endif
+
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/cpu_state.c
--- a/src/libmpeg2/cpu_state.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/cpu_state.c	Wed Jul 04 03:09:58 2007 +0300
@@ -40,74 +40,19 @@ static void state_restore_mmx (cpu_state
 #endif
 
 #if defined (ARCH_PPC) && defined (ENABLE_ALTIVEC)
-
-#ifndef HOST_OS_DARWIN
-
-static void state_save_altivec (cpu_state_t * state)
-{
-    asm ("						\n"
-	"	li		%r9,  16		\n"
-	"	stvx		%v20, 0,    %r3		\n"
-	"	li		%r11, 32		\n"
-	"	stvx		%v21, %r9,  %r3		\n"
-	"	li		%r9,  48		\n"
-	"	stvx		%v22, %r11, %r3		\n"
-	"	li		%r11, 64		\n"
-	"	stvx		%v23, %r9,  %r3		\n"
-	"	li		%r9,  80		\n"
-	"	stvx		%v24, %r11, %r3		\n"
-	"	li		%r11, 96		\n"
-	"	stvx		%v25, %r9,  %r3		\n"
-	"	li		%r9,  112		\n"
-	"	stvx		%v26, %r11, %r3		\n"
-	"	li		%r11, 128		\n"
-	"	stvx		%v27, %r9,  %r3		\n"
-	"	li		%r9,  144		\n"
-	"	stvx		%v28, %r11, %r3		\n"
-	"	li		%r11, 160		\n"
-	"	stvx		%v29, %r9,  %r3		\n"
-	"	li		%r9,  176		\n"
-	"	stvx		%v30, %r11, %r3		\n"
-	"	stvx		%v31, %r9,  %r3		\n"
-	 );
-}
-
-static void state_restore_altivec (cpu_state_t * state)
-{
-    asm ("						\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v20, 0,    %r3		\n"
-	"	li		%r11, 32		\n"
-	"	lvx		%v21, %r9,  %r3		\n"
-	"	li		%r9,  48		\n"
-	"	lvx		%v22, %r11, %r3		\n"
-	"	li		%r11, 64		\n"
-	"	lvx		%v23, %r9,  %r3		\n"
-	"	li		%r9,  80		\n"
-	"	lvx		%v24, %r11, %r3		\n"
-	"	li		%r11, 96		\n"
-	"	lvx		%v25, %r9,  %r3		\n"
-	"	li		%r9,  112		\n"
-	"	lvx		%v26, %r11, %r3		\n"
-	"	li		%r11, 128		\n"
-	"	lvx		%v27, %r9,  %r3		\n"
-	"	li		%r9,  144		\n"
-	"	lvx		%v28, %r11, %r3		\n"
-	"	li		%r11, 160		\n"
-	"	lvx		%v29, %r9,  %r3		\n"
-	"	li		%r9,  176		\n"
-	"	lvx		%v30, %r11, %r3		\n"
-	"	lvx		%v31, %r9,  %r3		\n"
-	 );
-}
-
-#else /* HOST_OS_DARWIN */
-
+#ifdef HAVE_ALTIVEC_H	/* gnu */
+#define LI(a,b) "li " #a "," #b "\n\t"
+#define STVX0(a,b,c) "stvx " #a ",0," #c "\n\t"
+#define STVX(a,b,c) "stvx " #a "," #b "," #c "\n\t"
+#define LVX0(a,b,c) "lvx " #a ",0," #c "\n\t"
+#define LVX(a,b,c) "lvx " #a "," #b "," #c "\n\t"
+#else			/* apple */
 #define LI(a,b) "li r" #a "," #b "\n\t"
 #define STVX0(a,b,c) "stvx v" #a ",0,r" #c "\n\t"
 #define STVX(a,b,c) "stvx v" #a ",r" #b ",r" #c "\n\t"
 #define LVX0(a,b,c) "lvx v" #a ",0,r" #c "\n\t"
 #define LVX(a,b,c) "lvx v" #a ",r" #b ",r" #c "\n\t"
+#endif
 
 static void state_save_altivec (cpu_state_t * state)
 {
@@ -162,9 +107,7 @@ static void state_restore_altivec (cpu_s
 	 LVX (30, 11, 3)
 	 LVX (31, 9, 3));
 }
-#endif /* HOST_OS_DARWIN */
-
-#endif /* defined (ARCH_PPC) && defined (ENABLE_ALTIVEC) */
+#endif
 
 void mpeg2_cpu_state_init (uint32_t mm_accel)
 {
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct.c
--- a/src/libmpeg2/idct.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/idct.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,11 +1,7 @@
 /*
  * idct.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
- *
- * Portions of this code are from the MPEG software simulation group
- * idct implementation. This code will be replaced with a new
- * implementation soon.
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
  * See http://libmpeg2.sourceforge.net/ for updates.
@@ -25,196 +21,158 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-/**********************************************************/
-/* inverse two dimensional DCT, Chen-Wang algorithm */
-/* (cf. IEEE ASSP-32, pp. 803-816, Aug. 1984) */
-/* 32-bit integer arithmetic (8 bit coefficients) */
-/* 11 mults, 29 adds per DCT */
-/* sE, 18.8.91 */
-/**********************************************************/
-/* coefficients extended to 12 bit for IEEE1180-1990 */
-/* compliance sE, 2.1.94 */
-/**********************************************************/
-
-/* this code assumes >> to be a two's-complement arithmetic */
-/* right shift: (-2)>>1 == -1 , (-3)>>1 == -2 */
-
 #include "config.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <inttypes.h>
 
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
 #include "xineutils.h"
 
-#define W1 2841 /* 2048*sqrt (2)*cos (1*pi/16) */
-#define W2 2676 /* 2048*sqrt (2)*cos (2*pi/16) */
-#define W3 2408 /* 2048*sqrt (2)*cos (3*pi/16) */
-#define W5 1609 /* 2048*sqrt (2)*cos (5*pi/16) */
-#define W6 1108 /* 2048*sqrt (2)*cos (6*pi/16) */
-#define W7 565  /* 2048*sqrt (2)*cos (7*pi/16) */
-
-/* idct main entry points  */
+#define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */
+#define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */
+#define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */
+#define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */
+#define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */
+#define W7 565  /* 2048 * sqrt (2) * cos (7 * pi / 16) */
+
+/* idct main entry point  */
 void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
-void (* mpeg2_idct_add) (int16_t * block, uint8_t * dest, int stride);
+void (* mpeg2_idct_add) (int last, int16_t * block,
+			 uint8_t * dest, int stride);
 void (* mpeg2_idct) (int16_t * block);
-void (*	mpeg2_zero_block) (int16_t * block);
-
-static uint8_t clip_lut[1024];
-#define CLIP(i) ((clip_lut+384)[ (i)])
-
-/* row (horizontal) IDCT
- *
- * 7 pi 1
- * dst[k] = sum c[l] * src[l] * cos ( -- * ( k + - ) * l )
- * l=0 8 2
- *
- * where: c[0] = 128
- * c[1..7] = 128*sqrt (2)
+void (* mpeg2_zero_block) (int16_t * block);
+
+/*
+ * In legal streams, the IDCT output should be between -384 and +384.
+ * In corrupted streams, it is possible to force the IDCT output to go
+ * to +-3826 - this is the worst case for a column IDCT where the
+ * column inputs are 16-bit values.
  */
-
-static void inline idct_row (int16_t * block)
-{
-    int x0, x1, x2, x3, x4, x5, x6, x7, x8;
-
-    x1 = block[4] << 11;
-    x2 = block[6];
-    x3 = block[2];
-    x4 = block[1];
-    x5 = block[7];
-    x6 = block[5];
-    x7 = block[3];
+uint8_t mpeg2_clip[3840 * 2 + 256];
+#define CLIP(i) ((mpeg2_clip + 3840)[i])
+
+#if 0
+#define BUTTERFLY(t0,t1,W0,W1,d0,d1)	\
+do {					\
+    t0 = W0 * d0 + W1 * d1;		\
+    t1 = W0 * d1 - W1 * d0;		\
+} while (0)
+#else
+#define BUTTERFLY(t0,t1,W0,W1,d0,d1)	\
+do {					\
+    int tmp = W0 * (d0 + d1);		\
+    t0 = tmp + (W1 - W0) * d1;		\
+    t1 = tmp - (W1 + W0) * d0;		\
+} while (0)
+#endif
+
+static inline void idct_row (int16_t * const block)
+{
+    int d0, d1, d2, d3;
+    int a0, a1, a2, a3, b0, b1, b2, b3;
+    int t0, t1, t2, t3;
 
     /* shortcut */
-    if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
-	block[0] = block[1] = block[2] = block[3] = block[4] =
-	    block[5] = block[6] = block[7] = block[0]<<3;
+    if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] |
+		  ((int32_t *)block)[3]))) {
+	uint32_t tmp = (uint16_t) (block[0] << 3);
+	tmp |= tmp << 16;
+	((int32_t *)block)[0] = tmp;
+	((int32_t *)block)[1] = tmp;
+	((int32_t *)block)[2] = tmp;
+	((int32_t *)block)[3] = tmp;
 	return;
     }
 
-    x0 = (block[0] << 11) + 128; /* for proper rounding in the fourth stage */
-
-    /* first stage */
-    x8 = W7 * (x4 + x5);
-    x4 = x8 + (W1 - W7) * x4;
-    x5 = x8 - (W1 + W7) * x5;
-    x8 = W3 * (x6 + x7);
-    x6 = x8 - (W3 - W5) * x6;
-    x7 = x8 - (W3 + W5) * x7;
- 
-    /* second stage */
-    x8 = x0 + x1;
-    x0 -= x1;
-    x1 = W6 * (x3 + x2);
-    x2 = x1 - (W2 + W6) * x2;
-    x3 = x1 + (W2 - W6) * x3;
-    x1 = x4 + x6;
-    x4 -= x6;
-    x6 = x5 + x7;
-    x5 -= x7;
- 
-    /* third stage */
-    x7 = x8 + x3;
-    x8 -= x3;
-    x3 = x0 + x2;
-    x0 -= x2;
-    x2 = (181 * (x4 + x5) + 128) >> 8;
-    x4 = (181 * (x4 - x5) + 128) >> 8;
- 
-    /* fourth stage */
-    block[0] = (x7 + x1) >> 8;
-    block[1] = (x3 + x2) >> 8;
-    block[2] = (x0 + x4) >> 8;
-    block[3] = (x8 + x6) >> 8;
-    block[4] = (x8 - x6) >> 8;
-    block[5] = (x0 - x4) >> 8;
-    block[6] = (x3 - x2) >> 8;
-    block[7] = (x7 - x1) >> 8;
-}
-
-/* column (vertical) IDCT
- *
- * 7 pi 1
- * dst[8*k] = sum c[l] * src[8*l] * cos ( -- * ( k + - ) * l )
- * l=0 8 2
- *
- * where: c[0] = 1/1024
- * c[1..7] = (1/1024)*sqrt (2)
- */
-
-static void inline idct_col (int16_t *block)
-{
-    int x0, x1, x2, x3, x4, x5, x6, x7, x8;
-
-    /* shortcut */
-    x1 = block [8*4] << 8;
-    x2 = block [8*6];
-    x3 = block [8*2];
-    x4 = block [8*1];
-    x5 = block [8*7];
-    x6 = block [8*5];
-    x7 = block [8*3];
-
-#if 0
-    if (! (x1 | x2 | x3 | x4 | x5 | x6 | x7 )) {
-	block[8*0] = block[8*1] = block[8*2] = block[8*3] = block[8*4] =
-	    block[8*5] = block[8*6] = block[8*7] = (block[8*0] + 32) >> 6;
-	return;
-    }
-#endif
-
-    x0 = (block[8*0] << 8) + 8192;
-
-    /* first stage */
-    x8 = W7 * (x4 + x5) + 4;
-    x4 = (x8 + (W1 - W7) * x4) >> 3;
-    x5 = (x8 - (W1 + W7) * x5) >> 3;
-    x8 = W3 * (x6 + x7) + 4;
-    x6 = (x8 - (W3 - W5) * x6) >> 3;
-    x7 = (x8 - (W3 + W5) * x7) >> 3;
- 
-    /* second stage */
-    x8 = x0 + x1;
-    x0 -= x1;
-    x1 = W6 * (x3 + x2) + 4;
-    x2 = (x1 - (W2 + W6) * x2) >> 3;
-    x3 = (x1 + (W2 - W6) * x3) >> 3;
-    x1 = x4 + x6;
-    x4 -= x6;
-    x6 = x5 + x7;
-    x5 -= x7;
- 
-    /* third stage */
-    x7 = x8 + x3;
-    x8 -= x3;
-    x3 = x0 + x2;
-    x0 -= x2;
-    x2 = (181 * (x4 + x5) + 128) >> 8;
-    x4 = (181 * (x4 - x5) + 128) >> 8;
- 
-    /* fourth stage */
-    block[8*0] = (x7 + x1) >> 14;
-    block[8*1] = (x3 + x2) >> 14;
-    block[8*2] = (x0 + x4) >> 14;
-    block[8*3] = (x8 + x6) >> 14;
-    block[8*4] = (x8 - x6) >> 14;
-    block[8*5] = (x0 - x4) >> 14;
-    block[8*6] = (x3 - x2) >> 14;
-    block[8*7] = (x7 - x1) >> 14;
-}
-
-static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest, int stride)
+    d0 = (block[0] << 11) + 128;
+    d1 = block[1];
+    d2 = block[2] << 11;
+    d3 = block[3];
+    t0 = d0 + d2;
+    t1 = d0 - d2;
+    BUTTERFLY (t2, t3, W6, W2, d3, d1);
+    a0 = t0 + t2;
+    a1 = t1 + t3;
+    a2 = t1 - t3;
+    a3 = t0 - t2;
+
+    d0 = block[4];
+    d1 = block[5];
+    d2 = block[6];
+    d3 = block[7];
+    BUTTERFLY (t0, t1, W7, W1, d3, d0);
+    BUTTERFLY (t2, t3, W3, W5, d1, d2);
+    b0 = t0 + t2;
+    b3 = t1 + t3;
+    t0 -= t2;
+    t1 -= t3;
+    b1 = ((t0 + t1) >> 8) * 181;
+    b2 = ((t0 - t1) >> 8) * 181;
+
+    block[0] = (a0 + b0) >> 8;
+    block[1] = (a1 + b1) >> 8;
+    block[2] = (a2 + b2) >> 8;
+    block[3] = (a3 + b3) >> 8;
+    block[4] = (a3 - b3) >> 8;
+    block[5] = (a2 - b2) >> 8;
+    block[6] = (a1 - b1) >> 8;
+    block[7] = (a0 - b0) >> 8;
+}
+
+static inline void idct_col (int16_t * const block)
+{
+    int d0, d1, d2, d3;
+    int a0, a1, a2, a3, b0, b1, b2, b3;
+    int t0, t1, t2, t3;
+
+    d0 = (block[8*0] << 11) + 65536;
+    d1 = block[8*1];
+    d2 = block[8*2] << 11;
+    d3 = block[8*3];
+    t0 = d0 + d2;
+    t1 = d0 - d2;
+    BUTTERFLY (t2, t3, W6, W2, d3, d1);
+    a0 = t0 + t2;
+    a1 = t1 + t3;
+    a2 = t1 - t3;
+    a3 = t0 - t2;
+
+    d0 = block[8*4];
+    d1 = block[8*5];
+    d2 = block[8*6];
+    d3 = block[8*7];
+    BUTTERFLY (t0, t1, W7, W1, d3, d0);
+    BUTTERFLY (t2, t3, W3, W5, d1, d2);
+    b0 = t0 + t2;
+    b3 = t1 + t3;
+    t0 -= t2;
+    t1 -= t3;
+    b1 = ((t0 + t1) >> 8) * 181;
+    b2 = ((t0 - t1) >> 8) * 181;
+
+    block[8*0] = (a0 + b0) >> 17;
+    block[8*1] = (a1 + b1) >> 17;
+    block[8*2] = (a2 + b2) >> 17;
+    block[8*3] = (a3 + b3) >> 17;
+    block[8*4] = (a3 - b3) >> 17;
+    block[8*5] = (a2 - b2) >> 17;
+    block[8*6] = (a1 - b1) >> 17;
+    block[8*7] = (a0 - b0) >> 17;
+}
+
+static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest,
+			       const int stride)
 {
     int i;
 
     for (i = 0; i < 8; i++)
 	idct_row (block + 8 * i);
-
     for (i = 0; i < 8; i++)
 	idct_col (block + i);
-
-    i = 8;
     do {
 	dest[0] = CLIP (block[0]);
 	dest[1] = CLIP (block[1]);
@@ -225,41 +183,58 @@ static void mpeg2_idct_copy_c (int16_t *
 	dest[6] = CLIP (block[6]);
 	dest[7] = CLIP (block[7]);
 
-	block[0] = 0;   block[1] = 0;   block[2] = 0;   block[3] = 0;
-	block[4] = 0;   block[5] = 0;   block[6] = 0;   block[7] = 0;
+	((int32_t *)block)[0] = 0;	((int32_t *)block)[1] = 0;
+	((int32_t *)block)[2] = 0;	((int32_t *)block)[3] = 0;
 
 	dest += stride;
 	block += 8;
     } while (--i);
 }
 
-static void mpeg2_idct_add_c (int16_t * block, uint8_t * dest, int stride)
+static void mpeg2_idct_add_c (const int last, int16_t * block,
+			      uint8_t * dest, const int stride)
 {
     int i;
 
-    for (i = 0; i < 8; i++)
-	idct_row (block + 8 * i);
-
-    for (i = 0; i < 8; i++)
-	idct_col (block + i);
-
-    i = 8;
-    do {
-	dest[0] = CLIP (block[0] + dest[0]);
-	dest[1] = CLIP (block[1] + dest[1]);
-	dest[2] = CLIP (block[2] + dest[2]);
-	dest[3] = CLIP (block[3] + dest[3]);
-	dest[4] = CLIP (block[4] + dest[4]);
-	dest[5] = CLIP (block[5] + dest[5]);
-	dest[6] = CLIP (block[6] + dest[6]);
-	dest[7] = CLIP (block[7] + dest[7]);
-
-	block[0] = 0;   block[1] = 0;   block[2] = 0;   block[3] = 0;
-	block[4] = 0;   block[5] = 0;   block[6] = 0;   block[7] = 0;
-
-	dest += stride;
-	block += 8;
-    } while (--i);
+    if (last != 129 || (block[0] & 7) == 4) {
+	for (i = 0; i < 8; i++)
+	    idct_row (block + 8 * i);
+	for (i = 0; i < 8; i++)
+	    idct_col (block + i);
+	do {
+	    dest[0] = CLIP (block[0] + dest[0]);
+	    dest[1] = CLIP (block[1] + dest[1]);
+	    dest[2] = CLIP (block[2] + dest[2]);
+	    dest[3] = CLIP (block[3] + dest[3]);
+	    dest[4] = CLIP (block[4] + dest[4]);
+	    dest[5] = CLIP (block[5] + dest[5]);
+	    dest[6] = CLIP (block[6] + dest[6]);
+	    dest[7] = CLIP (block[7] + dest[7]);
+
+	    ((int32_t *)block)[0] = 0;	((int32_t *)block)[1] = 0;
+	    ((int32_t *)block)[2] = 0;	((int32_t *)block)[3] = 0;
+
+	    dest += stride;
+	    block += 8;
+	} while (--i);
+    } else {
+	int DC;
+
+	DC = (block[0] + 4) >> 3;
+	block[0] = block[63] = 0;
+	i = 8;
+	do {
+	    dest[0] = CLIP (DC + dest[0]);
+	    dest[1] = CLIP (DC + dest[1]);
+	    dest[2] = CLIP (DC + dest[2]);
+	    dest[3] = CLIP (DC + dest[3]);
+	    dest[4] = CLIP (DC + dest[4]);
+	    dest[5] = CLIP (DC + dest[5]);
+	    dest[6] = CLIP (DC + dest[6]);
+	    dest[7] = CLIP (DC + dest[7]);
+	    dest += stride;
+	} while (--i);
+    }
 }
 
 static void mpeg2_idct_c (int16_t * block)
@@ -314,35 +289,22 @@ void mpeg2_idct_init (uint32_t mm_accel)
 	mpeg2_idct       = mpeg2_idct_c;
     } else
 #endif
-#ifdef LIBMPEG2_MLIB
-    if (mm_accel & MM_ACCEL_MLIB) {
-	char * env_var;
-
-	env_var = getenv ("MLIB_NON_IEEE");
-
-	mpeg2_idct = mpeg2_idct_mlib;
-	if (env_var == NULL) {
-#ifdef LOG
-	    fprintf (stderr, "Using mlib for IDCT transform\n");
-#endif
-	    mpeg2_idct_add = mpeg2_idct_add_mlib;
-	} else {
-	    fprintf (stderr, "Using non-IEEE mlib for IDCT transform\n");
-	    mpeg2_idct_add = mpeg2_idct_add_mlib_non_ieee;
+    {
+	int i, j;
+
+#ifdef LOG
+	fprintf (stderr, "No accelerated IDCT transform found\n");
+#endif
+	mpeg2_idct_copy = mpeg2_idct_copy_c;
+	mpeg2_idct_add = mpeg2_idct_add_c;
+	mpeg2_idct      = mpeg2_idct_c;
+	for (i = -3840; i < 3840 + 256; i++)
+	    CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
+	for (i = 0; i < 64; i++) {
+	    j = mpeg2_scan_norm[i];
+	    mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
+	    j = mpeg2_scan_alt[i];
+	    mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
 	}
-	mpeg2_idct_copy = mpeg2_idct_copy_mlib_non_ieee;
-    } else
-#endif
-    {
-	int i;
-
-#ifdef LOG
-	fprintf (stderr, "No accelerated IDCT transform found\n");
-#endif
-	mpeg2_idct_copy = mpeg2_idct_copy_c;
-	mpeg2_idct_add  = mpeg2_idct_add_c;
-	mpeg2_idct      = mpeg2_idct_c;
-	for (i = -384; i < 640; i++)
-	    clip_lut[i+384] = (i < 0) ? 0 : ((i > 255) ? 255 : i);
     }
 }
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct_alpha.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmpeg2/idct_alpha.c	Wed Jul 04 03:09:58 2007 +0300
@@ -0,0 +1,379 @@
+/*
+ * idct_alpha.c
+ * Copyright (C) 2002-2003 Falk Hueffner <falk@debian.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
+ *
+ * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
+ * See http://libmpeg2.sourceforge.net/ for updates.
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "config.h"
+
+#ifdef ARCH_ALPHA
+
+#include <stdlib.h>
+#include <inttypes.h>
+
+#include "mpeg2.h"
+#include "attributes.h"
+#include "mpeg2_internal.h"
+#include "alpha_asm.h"
+
+#define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */
+#define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */
+#define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */
+#define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */
+#define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */
+#define W7 565  /* 2048 * sqrt (2) * cos (7 * pi / 16) */
+
+extern uint8_t mpeg2_clip[3840 * 2 + 256];
+#define CLIP(i) ((mpeg2_clip + 3840)[i])
+
+#if 0
+#define BUTTERFLY(t0,t1,W0,W1,d0,d1)	\
+do {					\
+    t0 = W0 * d0 + W1 * d1;			\
+    t1 = W0 * d1 - W1 * d0;			\
+} while (0)
+#else
+#define BUTTERFLY(t0,t1,W0,W1,d0,d1)	\
+do {					\
+    int_fast32_t tmp = W0 * (d0 + d1);	\
+    t0 = tmp + (W1 - W0) * d1;		\
+    t1 = tmp - (W1 + W0) * d0;		\
+} while (0)
+#endif
+
+static inline void idct_row (int16_t * const block)
+{
+    uint64_t l, r;
+    int_fast32_t d0, d1, d2, d3;
+    int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
+    int_fast32_t t0, t1, t2, t3;
+
+    l = ldq (block);
+    r = ldq (block + 4);
+
+    /* shortcut */
+    if (likely (!((l & ~0xffffUL) | r))) {
+	uint64_t tmp = (uint16_t) (l << 3);
+	tmp |= tmp << 16;
+	tmp |= tmp << 32;
+	((int32_t *)block)[0] = tmp;
+	((int32_t *)block)[1] = tmp;
+	((int32_t *)block)[2] = tmp;
+	((int32_t *)block)[3] = tmp;
+	return;
+    }
+
+    d0 = (sextw (l) << 11) + 128;
+    d1 = sextw (extwl (l, 2));
+    d2 = sextw (extwl (l, 4)) << 11;
+    d3 = sextw (extwl (l, 6));
+    t0 = d0 + d2;
+    t1 = d0 - d2;
+    BUTTERFLY (t2, t3, W6, W2, d3, d1);
+    a0 = t0 + t2;
+    a1 = t1 + t3;
+    a2 = t1 - t3;
+    a3 = t0 - t2;
+
+    d0 = sextw (r);
+    d1 = sextw (extwl (r, 2));
+    d2 = sextw (extwl (r, 4));
+    d3 = sextw (extwl (r, 6));
+    BUTTERFLY (t0, t1, W7, W1, d3, d0);
+    BUTTERFLY (t2, t3, W3, W5, d1, d2);
+    b0 = t0 + t2;
+    b3 = t1 + t3;
+    t0 -= t2;
+    t1 -= t3;
+    b1 = ((t0 + t1) >> 8) * 181;
+    b2 = ((t0 - t1) >> 8) * 181;
+
+    block[0] = (a0 + b0) >> 8;
+    block[1] = (a1 + b1) >> 8;
+    block[2] = (a2 + b2) >> 8;
+    block[3] = (a3 + b3) >> 8;
+    block[4] = (a3 - b3) >> 8;
+    block[5] = (a2 - b2) >> 8;
+    block[6] = (a1 - b1) >> 8;
+    block[7] = (a0 - b0) >> 8;
+}
+
+static inline void idct_col (int16_t * const block)
+{
+    int_fast32_t d0, d1, d2, d3;
+    int_fast32_t a0, a1, a2, a3, b0, b1, b2, b3;
+    int_fast32_t t0, t1, t2, t3;
+
+    d0 = (block[8*0] << 11) + 65536;
+    d1 = block[8*1];
+    d2 = block[8*2] << 11;
+    d3 = block[8*3];
+    t0 = d0 + d2;
+    t1 = d0 - d2;
+    BUTTERFLY (t2, t3, W6, W2, d3, d1);
+    a0 = t0 + t2;
+    a1 = t1 + t3;
+    a2 = t1 - t3;
+    a3 = t0 - t2;
+
+    d0 = block[8*4];
+    d1 = block[8*5];
+    d2 = block[8*6];
+    d3 = block[8*7];
+    BUTTERFLY (t0, t1, W7, W1, d3, d0);
+    BUTTERFLY (t2, t3, W3, W5, d1, d2);
+    b0 = t0 + t2;
+    b3 = t1 + t3;
+    t0 -= t2;
+    t1 -= t3;
+    b1 = ((t0 + t1) >> 8) * 181;
+    b2 = ((t0 - t1) >> 8) * 181;
+
+    block[8*0] = (a0 + b0) >> 17;
+    block[8*1] = (a1 + b1) >> 17;
+    block[8*2] = (a2 + b2) >> 17;
+    block[8*3] = (a3 + b3) >> 17;
+    block[8*4] = (a3 - b3) >> 17;
+    block[8*5] = (a2 - b2) >> 17;
+    block[8*6] = (a1 - b1) >> 17;
+    block[8*7] = (a0 - b0) >> 17;
+}
+
+void mpeg2_idct_copy_mvi (int16_t * block, uint8_t * dest, const int stride)
+{
+    uint64_t clampmask;
+    int i;
+
+    for (i = 0; i < 8; i++)
+	idct_row (block + 8 * i);
+
+    for (i = 0; i < 8; i++)
+	idct_col (block + i);
+
+    clampmask = zap (-1, 0xaa);	/* 0x00ff00ff00ff00ff */
+    do {
+	uint64_t shorts0, shorts1;
+
+	shorts0 = ldq (block);
+	shorts0 = maxsw4 (shorts0, 0);
+	shorts0 = minsw4 (shorts0, clampmask);
+	stl (pkwb (shorts0), dest);
+
+	shorts1 = ldq (block + 4);
+	shorts1 = maxsw4 (shorts1, 0);
+	shorts1 = minsw4 (shorts1, clampmask);
+	stl (pkwb (shorts1), dest + 4);
+
+	stq (0, block);
+	stq (0, block + 4);
+
+	dest += stride;
+	block += 8;
+    } while (--i);
+}
+
+void mpeg2_idct_add_mvi (const int last, int16_t * block,
+			 uint8_t * dest, const int stride)
+{
+    uint64_t clampmask;
+    uint64_t signmask;
+    int i;
+
+    if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) {
+	for (i = 0; i < 8; i++)
+	    idct_row (block + 8 * i);
+	for (i = 0; i < 8; i++)
+	    idct_col (block + i);
+	clampmask = zap (-1, 0xaa);	/* 0x00ff00ff00ff00ff */
+	signmask = zap (-1, 0x33);
+	signmask ^= signmask >> 1;	/* 0x8000800080008000 */
+
+	do {
+	    uint64_t shorts0, pix0, signs0;
+	    uint64_t shorts1, pix1, signs1;
+
+	    shorts0 = ldq (block);
+	    shorts1 = ldq (block + 4);
+
+	    pix0 = unpkbw (ldl (dest));
+	    /* signed subword add (MMX paddw).  */
+	    signs0 = shorts0 & signmask;
+	    shorts0 &= ~signmask;
+	    shorts0 += pix0;
+	    shorts0 ^= signs0;
+	    /* clamp. */
+	    shorts0 = maxsw4 (shorts0, 0);
+	    shorts0 = minsw4 (shorts0, clampmask);	
+
+	    /* next 4.  */
+	    pix1 = unpkbw (ldl (dest + 4));
+	    signs1 = shorts1 & signmask;
+	    shorts1 &= ~signmask;
+	    shorts1 += pix1;
+	    shorts1 ^= signs1;
+	    shorts1 = maxsw4 (shorts1, 0);
+	    shorts1 = minsw4 (shorts1, clampmask);
+
+	    stl (pkwb (shorts0), dest);
+	    stl (pkwb (shorts1), dest + 4);
+	    stq (0, block);
+	    stq (0, block + 4);
+
+	    dest += stride;
+	    block += 8;
+	} while (--i);
+    } else {
+	int DC;
+	uint64_t p0, p1, p2, p3, p4, p5, p6, p7;
+	uint64_t DCs;
+
+	DC = (block[0] + 64) >> 7;
+	block[0] = block[63] = 0;
+
+	p0 = ldq (dest + 0 * stride);
+	p1 = ldq (dest + 1 * stride);
+	p2 = ldq (dest + 2 * stride);
+	p3 = ldq (dest + 3 * stride);
+	p4 = ldq (dest + 4 * stride);
+	p5 = ldq (dest + 5 * stride);
+	p6 = ldq (dest + 6 * stride);
+	p7 = ldq (dest + 7 * stride);
+
+	if (DC > 0) {
+	    DCs = BYTE_VEC (likely (DC <= 255) ? DC : 255);
+	    p0 += minub8 (DCs, ~p0);
+	    p1 += minub8 (DCs, ~p1);
+	    p2 += minub8 (DCs, ~p2);
+	    p3 += minub8 (DCs, ~p3);
+	    p4 += minub8 (DCs, ~p4);
+	    p5 += minub8 (DCs, ~p5);
+	    p6 += minub8 (DCs, ~p6);
+	    p7 += minub8 (DCs, ~p7);
+	} else {
+	    DCs = BYTE_VEC (likely (-DC <= 255) ? -DC : 255);
+	    p0 -= minub8 (DCs, p0);
+	    p1 -= minub8 (DCs, p1);
+	    p2 -= minub8 (DCs, p2);
+	    p3 -= minub8 (DCs, p3);
+	    p4 -= minub8 (DCs, p4);
+	    p5 -= minub8 (DCs, p5);
+	    p6 -= minub8 (DCs, p6);
+	    p7 -= minub8 (DCs, p7);
+	}
+
+	stq (p0, dest + 0 * stride);
+	stq (p1, dest + 1 * stride);
+	stq (p2, dest + 2 * stride);
+	stq (p3, dest + 3 * stride);
+	stq (p4, dest + 4 * stride);
+	stq (p5, dest + 5 * stride);
+	stq (p6, dest + 6 * stride);
+	stq (p7, dest + 7 * stride);
+    }
+}
+
+void mpeg2_idct_copy_alpha (int16_t * block, uint8_t * dest, const int stride)
+{
+    int i;
+
+    for (i = 0; i < 8; i++)
+	idct_row (block + 8 * i);
+    for (i = 0; i < 8; i++)
+	idct_col (block + i);
+    do {
+	dest[0] = CLIP (block[0]);
+	dest[1] = CLIP (block[1]);
+	dest[2] = CLIP (block[2]);
+	dest[3] = CLIP (block[3]);
+	dest[4] = CLIP (block[4]);
+	dest[5] = CLIP (block[5]);
+	dest[6] = CLIP (block[6]);
+	dest[7] = CLIP (block[7]);
+
+	stq(0, block);
+	stq(0, block + 4);
+
+	dest += stride;
+	block += 8;
+    } while (--i);
+}
+
+void mpeg2_idct_add_alpha (const int last, int16_t * block,
+			   uint8_t * dest, const int stride)
+{
+    int i;
+
+    if (last != 129 || (block[0] & 7) == 4) {
+	for (i = 0; i < 8; i++)
+	    idct_row (block + 8 * i);
+	for (i = 0; i < 8; i++)
+	    idct_col (block + i);
+	do {
+	    dest[0] = CLIP (block[0] + dest[0]);
+	    dest[1] = CLIP (block[1] + dest[1]);
+	    dest[2] = CLIP (block[2] + dest[2]);
+	    dest[3] = CLIP (block[3] + dest[3]);
+	    dest[4] = CLIP (block[4] + dest[4]);
+	    dest[5] = CLIP (block[5] + dest[5]);
+	    dest[6] = CLIP (block[6] + dest[6]);
+	    dest[7] = CLIP (block[7] + dest[7]);
+
+	    stq(0, block);
+	    stq(0, block + 4);
+
+	    dest += stride;
+	    block += 8;
+	} while (--i);
+    } else {
+	int DC;
+
+	DC = (block[0] + 4) >> 3;
+	block[0] = block[63] = 0;
+	i = 8;
+	do {
+	    dest[0] = CLIP (DC + dest[0]);
+	    dest[1] = CLIP (DC + dest[1]);
+	    dest[2] = CLIP (DC + dest[2]);
+	    dest[3] = CLIP (DC + dest[3]);
+	    dest[4] = CLIP (DC + dest[4]);
+	    dest[5] = CLIP (DC + dest[5]);
+	    dest[6] = CLIP (DC + dest[6]);
+	    dest[7] = CLIP (DC + dest[7]);
+	    dest += stride;
+	} while (--i);
+    }
+}
+
+void mpeg2_idct_alpha_init (void)
+{
+    extern uint8_t mpeg2_scan_norm[64];
+    extern uint8_t mpeg2_scan_alt[64];
+    int i, j;
+
+    for (i = 0; i < 64; i++) {
+	j = mpeg2_scan_norm[i];
+	mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
+	j = mpeg2_scan_alt[i];
+	mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
+    }
+}
+
+#endif /* ARCH_ALPHA */
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct_altivec.c
--- a/src/libmpeg2/idct_altivec.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/idct_altivec.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * idct_altivec.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -26,53 +26,50 @@
 #if defined (ARCH_PPC) && defined (ENABLE_ALTIVEC)
 
 #include <altivec.h>
-
 #include <inttypes.h>
 
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
-#include "xineutils.h"
-
-#define vector_s16_t vector signed short
-#define vector_u16_t vector unsigned short
-#define vector_s8_t vector signed char
-#define vector_u8_t vector unsigned char
-#define vector_s32_t vector signed int
-#define vector_u32_t vector unsigned int
-
-#define IDCT_HALF					\
-    /* 1st stage */					\
-    t1 = vec_mradds (a1, vx7, vx1 );			\
-    t8 = vec_mradds (a1, vx1, vec_subs (zero, vx7));	\
-    t7 = vec_mradds (a2, vx5, vx3);			\
-    t3 = vec_mradds (ma2, vx3, vx5);			\
-							\
-    /* 2nd stage */					\
-    t5 = vec_adds (vx0, vx4);				\
-    t0 = vec_subs (vx0, vx4);				\
-    t2 = vec_mradds (a0, vx6, vx2);			\
-    t4 = vec_mradds (a0, vx2, vec_subs (zero,vx6));	\
-    t6 = vec_adds (t8, t3);				\
-    t3 = vec_subs (t8, t3);				\
-    t8 = vec_subs (t1, t7);				\
-    t1 = vec_adds (t1, t7);				\
-							\
-    /* 3rd stage */					\
-    t7 = vec_adds (t5, t2);				\
-    t2 = vec_subs (t5, t2);				\
-    t5 = vec_adds (t0, t4);				\
-    t0 = vec_subs (t0, t4);				\
-    t4 = vec_subs (t8, t3);				\
-    t3 = vec_adds (t8, t3);				\
-							\
-    /* 4th stage */					\
-    vy0 = vec_adds (t7, t1);				\
-    vy7 = vec_subs (t7, t1);				\
-    vy1 = vec_mradds (c4, t3, t5);			\
-    vy6 = vec_mradds (mc4, t3, t5);			\
-    vy2 = vec_mradds (c4, t4, t0);			\
-    vy5 = vec_mradds (mc4, t4, t0);			\
-    vy3 = vec_adds (t2, t6);				\
-    vy4 = vec_subs (t2, t6);
+
+typedef vector signed char vector_s8_t;
+typedef vector unsigned char vector_u8_t;
+typedef vector signed short vector_s16_t;
+typedef vector unsigned short vector_u16_t;
+typedef vector signed int vector_s32_t;
+typedef vector unsigned int vector_u32_t;
+
+#if defined(HAVE_ALTIVEC_H) && (__GNUC__ * 100 + __GNUC_MINOR__ < 303)
+/* work around gcc <3.3 vec_mergel bug */
+static inline vector_s16_t my_vec_mergel (vector_s16_t const A,
+					  vector_s16_t const B)
+{
+    static const vector_u8_t mergel = {
+	0x08, 0x09, 0x18, 0x19, 0x0a, 0x0b, 0x1a, 0x1b,
+	0x0c, 0x0d, 0x1c, 0x1d, 0x0e, 0x0f, 0x1e, 0x1f
+    };
+    return vec_perm (A, B, mergel);
+}
+#undef vec_mergel
+#define vec_mergel my_vec_mergel
+#endif
+
+#ifdef HAVE_ALTIVEC_H	/* gnu */
+#define VEC_S16(a,b,c,d,e,f,g,h) {a, b, c, d, e, f, g, h}
+#else			/* apple */
+#define VEC_S16(a,b,c,d,e,f,g,h) (vector_s16_t) (a, b, c, d, e, f, g, h)
+#endif
+
+static const vector_s16_t constants ATTR_ALIGN(16) =
+    VEC_S16 (23170, 13573, 6518, 21895, -23170, -21895, 32, 31);
+static const vector_s16_t constants_1 ATTR_ALIGN(16) =
+    VEC_S16 (16384, 22725, 21407, 19266, 16384, 19266, 21407, 22725);
+static const vector_s16_t constants_2 ATTR_ALIGN(16) =
+    VEC_S16 (16069, 22289, 20995, 18895, 16069, 18895, 20995, 22289);
+static const vector_s16_t constants_3 ATTR_ALIGN(16) =
+    VEC_S16 (21407, 29692, 27969, 25172, 21407, 25172, 27969, 29692);
+static const vector_s16_t constants_4 ATTR_ALIGN(16) =
+    VEC_S16 (13623, 18895, 17799, 16019, 13623, 16019, 17799, 18895);
 
 #define IDCT								\
     vector_s16_t vx0, vx1, vx2, vx3, vx4, vx5, vx6, vx7;		\
@@ -81,27 +78,68 @@
     vector_s16_t t0, t1, t2, t3, t4, t5, t6, t7, t8;			\
     vector_u16_t shift;							\
 									\
-    c4 = vec_splat (constants[0], 0);					\
-    a0 = vec_splat (constants[0], 1);					\
-    a1 = vec_splat (constants[0], 2);					\
-    a2 = vec_splat (constants[0], 3);					\
-    mc4 = vec_splat (constants[0], 4);					\
-    ma2 = vec_splat (constants[0], 5);					\
-    bias = (vector_s16_t)vec_splat ((vector_s32_t)constants[0], 3);	\
+    c4 = vec_splat (constants, 0);					\
+    a0 = vec_splat (constants, 1);					\
+    a1 = vec_splat (constants, 2);					\
+    a2 = vec_splat (constants, 3);					\
+    mc4 = vec_splat (constants, 4);					\
+    ma2 = vec_splat (constants, 5);					\
+    bias = (vector_s16_t)vec_splat ((vector_s32_t)constants, 3);	\
 									\
     zero = vec_splat_s16 (0);						\
+									\
     shift = vec_splat_u16 (4);						\
-									\
-    vx0 = vec_mradds (vec_sl (block[0], shift), constants[1], zero);	\
-    vx1 = vec_mradds (vec_sl (block[1], shift), constants[2], zero);	\
-    vx2 = vec_mradds (vec_sl (block[2], shift), constants[3], zero);	\
-    vx3 = vec_mradds (vec_sl (block[3], shift), constants[4], zero);	\
-    vx4 = vec_mradds (vec_sl (block[4], shift), constants[1], zero);	\
-    vx5 = vec_mradds (vec_sl (block[5], shift), constants[4], zero);	\
-    vx6 = vec_mradds (vec_sl (block[6], shift), constants[3], zero);	\
-    vx7 = vec_mradds (vec_sl (block[7], shift), constants[2], zero);	\
-									\
-    IDCT_HALF								\
+    block[0] = vec_sl (block[0], shift);				\
+    block[1] = vec_sl (block[1], shift);				\
+    block[2] = vec_sl (block[2], shift);				\
+    block[3] = vec_sl (block[3], shift);				\
+    block[4] = vec_sl (block[4], shift);				\
+    block[5] = vec_sl (block[5], shift);				\
+    block[6] = vec_sl (block[6], shift);				\
+    block[7] = vec_sl (block[7], shift);				\
+									\
+    vx0 = vec_adds (block[0], block[4]);				\
+    vx4 = vec_subs (block[0], block[4]);				\
+    t5 = vec_mradds (vx0, constants_1, zero);				\
+    t0 = vec_mradds (vx4, constants_1, zero);				\
+									\
+    vx1 = vec_mradds (a1, block[7], block[1]);				\
+    vx7 = vec_mradds (a1, block[1], vec_subs (zero, block[7]));		\
+    t1 = vec_mradds (vx1, constants_2, zero);				\
+    t8 = vec_mradds (vx7, constants_2, zero);				\
+									\
+    vx2 = vec_mradds (a0, block[6], block[2]);				\
+    vx6 = vec_mradds (a0, block[2], vec_subs (zero, block[6]));		\
+    t2 = vec_mradds (vx2, constants_3, zero);				\
+    t4 = vec_mradds (vx6, constants_3, zero);				\
+									\
+    vx3 = vec_mradds (block[3], constants_4, zero);			\
+    vx5 = vec_mradds (block[5], constants_4, zero);			\
+    t7 = vec_mradds (a2, vx5, vx3);					\
+    t3 = vec_mradds (ma2, vx3, vx5);					\
+									\
+    t6 = vec_adds (t8, t3);						\
+    t3 = vec_subs (t8, t3);						\
+    t8 = vec_subs (t1, t7);						\
+    t1 = vec_adds (t1, t7);						\
+    t6 = vec_mradds (a0, t6, t6);	/* a0+1 == 2*c4 */		\
+    t1 = vec_mradds (a0, t1, t1);	/* a0+1 == 2*c4 */		\
+									\
+    t7 = vec_adds (t5, t2);						\
+    t2 = vec_subs (t5, t2);						\
+    t5 = vec_adds (t0, t4);						\
+    t0 = vec_subs (t0, t4);						\
+    t4 = vec_subs (t8, t3);						\
+    t3 = vec_adds (t8, t3);						\
+									\
+    vy0 = vec_adds (t7, t1);						\
+    vy7 = vec_subs (t7, t1);						\
+    vy1 = vec_adds (t5, t3);						\
+    vy6 = vec_subs (t5, t3);						\
+    vy2 = vec_adds (t0, t4);						\
+    vy5 = vec_subs (t0, t4);						\
+    vy3 = vec_adds (t2, t6);						\
+    vy4 = vec_subs (t2, t6);						\
 									\
     vx0 = vec_mergeh (vy0, vy4);					\
     vx1 = vec_mergel (vy0, vy4);					\
@@ -121,7 +159,7 @@
     vy6 = vec_mergeh (vx3, vx7);					\
     vy7 = vec_mergel (vx3, vx7);					\
 									\
-    vx0 = vec_adds (vec_mergeh (vy0, vy4), bias);			\
+    vx0 = vec_mergeh (vy0, vy4);					\
     vx1 = vec_mergel (vy0, vy4);					\
     vx2 = vec_mergeh (vy1, vy5);					\
     vx3 = vec_mergel (vy1, vy5);					\
@@ -130,7 +168,39 @@
     vx6 = vec_mergeh (vy3, vy7);					\
     vx7 = vec_mergel (vy3, vy7);					\
 									\
-    IDCT_HALF								\
+    vx0 = vec_adds (vx0, bias);						\
+    t5 = vec_adds (vx0, vx4);						\
+    t0 = vec_subs (vx0, vx4);						\
+									\
+    t1 = vec_mradds (a1, vx7, vx1);					\
+    t8 = vec_mradds (a1, vx1, vec_subs (zero, vx7));			\
+									\
+    t2 = vec_mradds (a0, vx6, vx2);					\
+    t4 = vec_mradds (a0, vx2, vec_subs (zero, vx6));			\
+									\
+    t7 = vec_mradds (a2, vx5, vx3);					\
+    t3 = vec_mradds (ma2, vx3, vx5);					\
+									\
+    t6 = vec_adds (t8, t3);						\
+    t3 = vec_subs (t8, t3);						\
+    t8 = vec_subs (t1, t7);						\
+    t1 = vec_adds (t1, t7);						\
+									\
+    t7 = vec_adds (t5, t2);						\
+    t2 = vec_subs (t5, t2);						\
+    t5 = vec_adds (t0, t4);						\
+    t0 = vec_subs (t0, t4);						\
+    t4 = vec_subs (t8, t3);						\
+    t3 = vec_adds (t8, t3);						\
+									\
+    vy0 = vec_adds (t7, t1);						\
+    vy7 = vec_subs (t7, t1);						\
+    vy1 = vec_mradds (c4, t3, t5);					\
+    vy6 = vec_mradds (mc4, t3, t5);					\
+    vy2 = vec_mradds (c4, t4, t0);					\
+    vy5 = vec_mradds (mc4, t4, t0);					\
+    vy3 = vec_adds (t2, t6);						\
+    vy4 = vec_subs (t2, t6);						\
 									\
     shift = vec_splat_u16 (6);						\
     vx0 = vec_sra (vy0, shift);						\
@@ -142,23 +212,10 @@
     vx6 = vec_sra (vy6, shift);						\
     vx7 = vec_sra (vy7, shift);
 
-#if defined( __APPLE_CC__ ) && defined( __APPLE_ALTIVEC__ ) /* apple */
-#define VEC_S16(a,b,c,d,e,f,g,h) (vector_s16_t) (a, b, c, d, e, f, g, h)
-#else			/* gnu */
-#define VEC_S16(a,b,c,d,e,f,g,h) (vector_s16_t) {a, b, c, d, e, f, g, h}
-#endif
-
-static vector_s16_t constants[5] = {
-    VEC_S16(23170, 13573, 6518, 21895, -23170, -21895, 32, 31),
-    VEC_S16(16384, 22725, 21407, 19266, 16384, 19266, 21407, 22725),
-    VEC_S16(22725, 31521, 29692, 26722, 22725, 26722, 29692, 31521),
-    VEC_S16(21407, 29692, 27969, 25172, 21407, 25172, 27969, 29692),
-    VEC_S16(19266, 26722, 25172, 22654, 19266, 22654, 25172, 26722)
-};
-
-void mpeg2_idct_copy_altivec (vector_s16_t * block, unsigned char * dest,
-			      int stride)
-{
+void mpeg2_idct_copy_altivec (int16_t * const _block, uint8_t * dest,
+			      const int stride)
+{
+    vector_s16_t * const block = (vector_s16_t *)_block;
     vector_u8_t tmp;
 
     IDCT
@@ -176,12 +233,15 @@ void mpeg2_idct_copy_altivec (vector_s16
     COPY (dest, vx5)	dest += stride;
     COPY (dest, vx6)	dest += stride;
     COPY (dest, vx7)
-    memset (block, 0, 64 * sizeof (signed short));
-}
-
-void mpeg2_idct_add_altivec (vector_s16_t * block, unsigned char * dest,
-			     int stride)
-{
+
+    block[0] = block[1] = block[2] = block[3] = zero;
+    block[4] = block[5] = block[6] = block[7] = zero;
+}
+
+void mpeg2_idct_add_altivec (const int last, int16_t * const _block,
+			     uint8_t * dest, const int stride)
+{
+    vector_s16_t * const block = (vector_s16_t *)_block;
     vector_u8_t tmp;
     vector_s16_t tmp2, tmp3;
     vector_u8_t perm0;
@@ -213,7 +273,9 @@ void mpeg2_idct_add_altivec (vector_s16_
     ADD (dest, vx5, perm1)	dest += stride;
     ADD (dest, vx6, perm0)	dest += stride;
     ADD (dest, vx7, perm1)
-    memset (block, 0, 64 * sizeof (signed short));
+
+    block[0] = block[1] = block[2] = block[3] = zero;
+    block[4] = block[5] = block[6] = block[7] = zero;
 }
 
 void mpeg2_idct_altivec_init (void)
@@ -229,5 +291,4 @@ void mpeg2_idct_altivec_init (void)
     }
 }
 
-#endif	/* ARCH_PPC && ENABLED_ALTIVEC */
-
+#endif
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct_mlib.c
--- a/src/libmpeg2/idct_mlib.c	Mon Jun 25 13:05:45 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-/*
- * idct_mlib.c
- * Copyright (C) 1999-2002 Håkan Hjort <d95hjort@dtek.chalmers.se>
- *
- * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
- * See http://libmpeg2.sourceforge.net/ for updates.
- *
- * mpeg2dec is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpeg2dec is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include "config.h"
-
-#ifdef LIBMPEG2_MLIB
-
-#include <mlib_types.h>
-#include <mlib_status.h>
-#include <mlib_sys.h>
-#include <mlib_video.h>
-#include <string.h>
-#include <inttypes.h>
-
-#include "mpeg2_internal.h"
-
-void mpeg2_idct_add_mlib (int16_t * block, uint8_t * dest, int stride)
-{
-    mlib_VideoIDCT_IEEE_S16_S16 (block, block);
-    mlib_VideoAddBlock_U8_S16 (dest, block, stride);
-    memset (block, 0, 64 * sizeof (uint16_t));
-}
-
-void mpeg2_idct_copy_mlib_non_ieee (int16_t * block, uint8_t * dest,
-				    int stride)
-{
-    mlib_VideoIDCT8x8_U8_S16 (dest, block, stride);
-    memset (block, 0, 64 * sizeof (uint16_t));
-}
-
-void mpeg2_idct_add_mlib_non_ieee (int16_t * block, uint8_t * dest, int stride)
-{
-    mlib_VideoIDCT8x8_S16_S16 (block, block);
-    mlib_VideoAddBlock_U8_S16 (dest, block, stride);
-    memset (block, 0, 64 * sizeof (uint16_t));
-}
-
-void mpeg2_idct_mlib (int16_t * block)
-{
-    mlib_VideoIDCT_IEEE_S16_S16 (block, block);
-}
-
-#endif
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct_mlib.h
--- a/src/libmpeg2/idct_mlib.h	Mon Jun 25 13:05:45 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,25 +0,0 @@
-/*
- * idct_mlib.h
- *
- * Copyright (C) 1999, Håkan Hjort <d95hjort@dtek.chalmers.se>
- *
- * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
- *	
- * mpeg2dec is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- * 
- * mpeg2dec is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * 
- * You should have received a copy of the GNU General Public License
- * along with GNU Make; see the file COPYING. If not, write to
- * the Free Software Foundation, 
- *
- */
-
-void idct_block_copy_mlib (int16_t * block, uint8_t * dest, int stride);
-void idct_block_add_mlib (int16_t * block, uint8_t * dest, int stride);
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/idct_mmx.c
--- a/src/libmpeg2/idct_mmx.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/idct_mmx.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * idct_mmx.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -27,8 +27,10 @@
 
 #include <inttypes.h>
 
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
-#include "xineutils.h"
+#include "mmx.h"
 
 #define ROW_SHIFT 11
 #define COL_SHIFT 6
@@ -88,104 +90,107 @@ static inline void idct_row (int16_t * r
 						   c5, -c1,  c3, -c1,	\
 						   c7,  c3,  c7, -c5 }
 
-static inline void mmxext_row_head (int16_t * row, int offset, int16_t * table)
-{
-    movq_m2r (*(row+offset), mm2);	// mm2 = x6 x4 x2 x0
-
-    movq_m2r (*(row+offset+4), mm5);	// mm5 = x7 x5 x3 x1
-    movq_r2r (mm2, mm0);		// mm0 = x6 x4 x2 x0
-
-    movq_m2r (*table, mm3);		// mm3 = -C2 -C4 C2 C4
-    movq_r2r (mm5, mm6);		// mm6 = x7 x5 x3 x1
-
-    movq_m2r (*(table+4), mm4);		// mm4 = C6 C4 C6 C4
-    pmaddwd_r2r (mm0, mm3);		// mm3 = -C4*x4-C2*x6 C4*x0+C2*x2
-
-    pshufw_r2r (mm2, mm2, 0x4e);	// mm2 = x2 x0 x6 x4
-}
-
-static inline void mmxext_row (int16_t * table, int32_t * rounder)
-{
-    movq_m2r (*(table+8), mm1);		// mm1 = -C5 -C1 C3 C1
-    pmaddwd_r2r (mm2, mm4);		// mm4 = C4*x0+C6*x2 C4*x4+C6*x6
-
-    pmaddwd_m2r (*(table+16), mm0);	// mm0 = C4*x4-C6*x6 C4*x0-C6*x2
-    pshufw_r2r (mm6, mm6, 0x4e);	// mm6 = x3 x1 x7 x5
-
-    movq_m2r (*(table+12), mm7);	// mm7 = -C7 C3 C7 C5
-    pmaddwd_r2r (mm5, mm1);		// mm1 = -C1*x5-C5*x7 C1*x1+C3*x3
-
-    paddd_m2r (*rounder, mm3);		// mm3 += rounder
-    pmaddwd_r2r (mm6, mm7);		// mm7 = C3*x1-C7*x3 C5*x5+C7*x7
-
-    pmaddwd_m2r (*(table+20), mm2);	// mm2 = C4*x0-C2*x2 -C4*x4+C2*x6
-    paddd_r2r (mm4, mm3);		// mm3 = a1 a0 + rounder
-
-    pmaddwd_m2r (*(table+24), mm5);	// mm5 = C3*x5-C1*x7 C5*x1-C1*x3
-    movq_r2r (mm3, mm4);		// mm4 = a1 a0 + rounder
-
-    pmaddwd_m2r (*(table+28), mm6);	// mm6 = C7*x1-C5*x3 C7*x5+C3*x7
-    paddd_r2r (mm7, mm1);		// mm1 = b1 b0
-
-    paddd_m2r (*rounder, mm0);		// mm0 += rounder
-    psubd_r2r (mm1, mm3);		// mm3 = a1-b1 a0-b0 + rounder
-
-    psrad_i2r (ROW_SHIFT, mm3);		// mm3 = y6 y7
-    paddd_r2r (mm4, mm1);		// mm1 = a1+b1 a0+b0 + rounder
-
-    paddd_r2r (mm2, mm0);		// mm0 = a3 a2 + rounder
-    psrad_i2r (ROW_SHIFT, mm1);		// mm1 = y1 y0
-
-    paddd_r2r (mm6, mm5);		// mm5 = b3 b2
-    movq_r2r (mm0, mm4);		// mm4 = a3 a2 + rounder
-
-    paddd_r2r (mm5, mm0);		// mm0 = a3+b3 a2+b2 + rounder
-    psubd_r2r (mm5, mm4);		// mm4 = a3-b3 a2-b2 + rounder
-}
-
-static inline void mmxext_row_tail (int16_t * row, int store)
-{
-    psrad_i2r (ROW_SHIFT, mm0);		// mm0 = y3 y2
-
-    psrad_i2r (ROW_SHIFT, mm4);		// mm4 = y4 y5
-
-    packssdw_r2r (mm0, mm1);		// mm1 = y3 y2 y1 y0
-
-    packssdw_r2r (mm3, mm4);		// mm4 = y6 y7 y4 y5
-
-    movq_r2m (mm1, *(row+store));	// save y3 y2 y1 y0
-    pshufw_r2r (mm4, mm4, 0xb1);	// mm4 = y7 y6 y5 y4
+static inline void mmxext_row_head (int16_t * const row, const int offset,
+				    const int16_t * const table)
+{
+    movq_m2r (*(row+offset), mm2);	/* mm2 = x6 x4 x2 x0 */
+
+    movq_m2r (*(row+offset+4), mm5);	/* mm5 = x7 x5 x3 x1 */
+    movq_r2r (mm2, mm0);		/* mm0 = x6 x4 x2 x0 */
+
+    movq_m2r (*table, mm3);		/* mm3 = -C2 -C4 C2 C4 */
+    movq_r2r (mm5, mm6);		/* mm6 = x7 x5 x3 x1 */
+
+    movq_m2r (*(table+4), mm4);		/* mm4 = C6 C4 C6 C4 */
+    pmaddwd_r2r (mm0, mm3);		/* mm3 = -C4*x4-C2*x6 C4*x0+C2*x2 */
+
+    pshufw_r2r (mm2, mm2, 0x4e);	/* mm2 = x2 x0 x6 x4 */
+}
+
+static inline void mmxext_row (const int16_t * const table,
+			       const int32_t * const rounder)
+{
+    movq_m2r (*(table+8), mm1);		/* mm1 = -C5 -C1 C3 C1 */
+    pmaddwd_r2r (mm2, mm4);		/* mm4 = C4*x0+C6*x2 C4*x4+C6*x6 */
+
+    pmaddwd_m2r (*(table+16), mm0);	/* mm0 = C4*x4-C6*x6 C4*x0-C6*x2 */
+    pshufw_r2r (mm6, mm6, 0x4e);	/* mm6 = x3 x1 x7 x5 */
+
+    movq_m2r (*(table+12), mm7);	/* mm7 = -C7 C3 C7 C5 */
+    pmaddwd_r2r (mm5, mm1);		/* mm1 = -C1*x5-C5*x7 C1*x1+C3*x3 */
+
+    paddd_m2r (*rounder, mm3);		/* mm3 += rounder */
+    pmaddwd_r2r (mm6, mm7);		/* mm7 = C3*x1-C7*x3 C5*x5+C7*x7 */
+
+    pmaddwd_m2r (*(table+20), mm2);	/* mm2 = C4*x0-C2*x2 -C4*x4+C2*x6 */
+    paddd_r2r (mm4, mm3);		/* mm3 = a1 a0 + rounder */
+
+    pmaddwd_m2r (*(table+24), mm5);	/* mm5 = C3*x5-C1*x7 C5*x1-C1*x3 */
+    movq_r2r (mm3, mm4);		/* mm4 = a1 a0 + rounder */
+
+    pmaddwd_m2r (*(table+28), mm6);	/* mm6 = C7*x1-C5*x3 C7*x5+C3*x7 */
+    paddd_r2r (mm7, mm1);		/* mm1 = b1 b0 */
+
+    paddd_m2r (*rounder, mm0);		/* mm0 += rounder */
+    psubd_r2r (mm1, mm3);		/* mm3 = a1-b1 a0-b0 + rounder */
+
+    psrad_i2r (ROW_SHIFT, mm3);		/* mm3 = y6 y7 */
+    paddd_r2r (mm4, mm1);		/* mm1 = a1+b1 a0+b0 + rounder */
+
+    paddd_r2r (mm2, mm0);		/* mm0 = a3 a2 + rounder */
+    psrad_i2r (ROW_SHIFT, mm1);		/* mm1 = y1 y0 */
+
+    paddd_r2r (mm6, mm5);		/* mm5 = b3 b2 */
+    movq_r2r (mm0, mm4);		/* mm4 = a3 a2 + rounder */
+
+    paddd_r2r (mm5, mm0);		/* mm0 = a3+b3 a2+b2 + rounder */
+    psubd_r2r (mm5, mm4);		/* mm4 = a3-b3 a2-b2 + rounder */
+}
+
+static inline void mmxext_row_tail (int16_t * const row, const int store)
+{
+    psrad_i2r (ROW_SHIFT, mm0);		/* mm0 = y3 y2 */
+
+    psrad_i2r (ROW_SHIFT, mm4);		/* mm4 = y4 y5 */
+
+    packssdw_r2r (mm0, mm1);		/* mm1 = y3 y2 y1 y0 */
+
+    packssdw_r2r (mm3, mm4);		/* mm4 = y6 y7 y4 y5 */
+
+    movq_r2m (mm1, *(row+store));	/* save y3 y2 y1 y0 */
+    pshufw_r2r (mm4, mm4, 0xb1);	/* mm4 = y7 y6 y5 y4 */
 
     /* slot */
 
-    movq_r2m (mm4, *(row+store+4));	// save y7 y6 y5 y4
-}
-
-static inline void mmxext_row_mid (int16_t * row, int store,
-				   int offset, int16_t * table)
-{
-    movq_m2r (*(row+offset), mm2);	// mm2 = x6 x4 x2 x0
-    psrad_i2r (ROW_SHIFT, mm0);		// mm0 = y3 y2
-
-    movq_m2r (*(row+offset+4), mm5);	// mm5 = x7 x5 x3 x1
-    psrad_i2r (ROW_SHIFT, mm4);		// mm4 = y4 y5
-
-    packssdw_r2r (mm0, mm1);		// mm1 = y3 y2 y1 y0
-    movq_r2r (mm5, mm6);		// mm6 = x7 x5 x3 x1
-
-    packssdw_r2r (mm3, mm4);		// mm4 = y6 y7 y4 y5
-    movq_r2r (mm2, mm0);		// mm0 = x6 x4 x2 x0
-
-    movq_r2m (mm1, *(row+store));	// save y3 y2 y1 y0
-    pshufw_r2r (mm4, mm4, 0xb1);	// mm4 = y7 y6 y5 y4
-
-    movq_m2r (*table, mm3);		// mm3 = -C2 -C4 C2 C4
-    movq_r2m (mm4, *(row+store+4));	// save y7 y6 y5 y4
-
-    pmaddwd_r2r (mm0, mm3);		// mm3 = -C4*x4-C2*x6 C4*x0+C2*x2
-
-    movq_m2r (*(table+4), mm4);		// mm4 = C6 C4 C6 C4
-    pshufw_r2r (mm2, mm2, 0x4e);	// mm2 = x2 x0 x6 x4
+    movq_r2m (mm4, *(row+store+4));	/* save y7 y6 y5 y4 */
+}
+
+static inline void mmxext_row_mid (int16_t * const row, const int store,
+				   const int offset,
+				   const int16_t * const table)
+{
+    movq_m2r (*(row+offset), mm2);	/* mm2 = x6 x4 x2 x0 */
+    psrad_i2r (ROW_SHIFT, mm0);		/* mm0 = y3 y2 */
+
+    movq_m2r (*(row+offset+4), mm5);	/* mm5 = x7 x5 x3 x1 */
+    psrad_i2r (ROW_SHIFT, mm4);		/* mm4 = y4 y5 */
+
+    packssdw_r2r (mm0, mm1);		/* mm1 = y3 y2 y1 y0 */
+    movq_r2r (mm5, mm6);		/* mm6 = x7 x5 x3 x1 */
+
+    packssdw_r2r (mm3, mm4);		/* mm4 = y6 y7 y4 y5 */
+    movq_r2r (mm2, mm0);		/* mm0 = x6 x4 x2 x0 */
+
+    movq_r2m (mm1, *(row+store));	/* save y3 y2 y1 y0 */
+    pshufw_r2r (mm4, mm4, 0xb1);	/* mm4 = y7 y6 y5 y4 */
+
+    movq_m2r (*table, mm3);		/* mm3 = -C2 -C4 C2 C4 */
+    movq_r2m (mm4, *(row+store+4));	/* save y7 y6 y5 y4 */
+
+    pmaddwd_r2r (mm0, mm3);		/* mm3 = -C4*x4-C2*x6 C4*x0+C2*x2 */
+
+    movq_m2r (*(table+4), mm4);		/* mm4 = C6 C4 C6 C4 */
+    pshufw_r2r (mm2, mm2, 0x4e);	/* mm2 = x2 x0 x6 x4 */
 }
 
 
@@ -200,125 +205,127 @@ static inline void mmxext_row_mid (int16
 					   c5, -c1,  c7, -c5,	\
 					   c7,  c3,  c3, -c1 }
 
-static inline void mmx_row_head (int16_t * row, int offset, int16_t * table)
-{
-    movq_m2r (*(row+offset), mm2);	// mm2 = x6 x4 x2 x0
-
-    movq_m2r (*(row+offset+4), mm5);	// mm5 = x7 x5 x3 x1
-    movq_r2r (mm2, mm0);		// mm0 = x6 x4 x2 x0
-
-    movq_m2r (*table, mm3);		// mm3 = C6 C4 C2 C4
-    movq_r2r (mm5, mm6);		// mm6 = x7 x5 x3 x1
-
-    punpckldq_r2r (mm0, mm0);		// mm0 = x2 x0 x2 x0
-
-    movq_m2r (*(table+4), mm4);		// mm4 = -C2 -C4 C6 C4
-    pmaddwd_r2r (mm0, mm3);		// mm3 = C4*x0+C6*x2 C4*x0+C2*x2
-
-    movq_m2r (*(table+8), mm1);		// mm1 = -C7 C3 C3 C1
-    punpckhdq_r2r (mm2, mm2);		// mm2 = x6 x4 x6 x4
-}
-
-static inline void mmx_row (int16_t * table, int32_t * rounder)
-{
-    pmaddwd_r2r (mm2, mm4);		// mm4 = -C4*x4-C2*x6 C4*x4+C6*x6
-    punpckldq_r2r (mm5, mm5);		// mm5 = x3 x1 x3 x1
-
-    pmaddwd_m2r (*(table+16), mm0);	// mm0 = C4*x0-C2*x2 C4*x0-C6*x2
-    punpckhdq_r2r (mm6, mm6);		// mm6 = x7 x5 x7 x5
-
-    movq_m2r (*(table+12), mm7);	// mm7 = -C5 -C1 C7 C5
-    pmaddwd_r2r (mm5, mm1);		// mm1 = C3*x1-C7*x3 C1*x1+C3*x3
-
-    paddd_m2r (*rounder, mm3);		// mm3 += rounder
-    pmaddwd_r2r (mm6, mm7);		// mm7 = -C1*x5-C5*x7 C5*x5+C7*x7
-
-    pmaddwd_m2r (*(table+20), mm2);	// mm2 = C4*x4-C6*x6 -C4*x4+C2*x6
-    paddd_r2r (mm4, mm3);		// mm3 = a1 a0 + rounder
-
-    pmaddwd_m2r (*(table+24), mm5);	// mm5 = C7*x1-C5*x3 C5*x1-C1*x3
-    movq_r2r (mm3, mm4);		// mm4 = a1 a0 + rounder
-
-    pmaddwd_m2r (*(table+28), mm6);	// mm6 = C3*x5-C1*x7 C7*x5+C3*x7
-    paddd_r2r (mm7, mm1);		// mm1 = b1 b0
-
-    paddd_m2r (*rounder, mm0);		// mm0 += rounder
-    psubd_r2r (mm1, mm3);		// mm3 = a1-b1 a0-b0 + rounder
-
-    psrad_i2r (ROW_SHIFT, mm3);		// mm3 = y6 y7
-    paddd_r2r (mm4, mm1);		// mm1 = a1+b1 a0+b0 + rounder
-
-    paddd_r2r (mm2, mm0);		// mm0 = a3 a2 + rounder
-    psrad_i2r (ROW_SHIFT, mm1);		// mm1 = y1 y0
-
-    paddd_r2r (mm6, mm5);		// mm5 = b3 b2
-    movq_r2r (mm0, mm7);		// mm7 = a3 a2 + rounder
-
-    paddd_r2r (mm5, mm0);		// mm0 = a3+b3 a2+b2 + rounder
-    psubd_r2r (mm5, mm7);		// mm7 = a3-b3 a2-b2 + rounder
-}
-
-static inline void mmx_row_tail (int16_t * row, int store)
-{
-    psrad_i2r (ROW_SHIFT, mm0);		// mm0 = y3 y2
-
-    psrad_i2r (ROW_SHIFT, mm7);		// mm7 = y4 y5
-
-    packssdw_r2r (mm0, mm1);		// mm1 = y3 y2 y1 y0
-
-    packssdw_r2r (mm3, mm7);		// mm7 = y6 y7 y4 y5
-
-    movq_r2m (mm1, *(row+store));	// save y3 y2 y1 y0
-    movq_r2r (mm7, mm4);		// mm4 = y6 y7 y4 y5
-
-    pslld_i2r (16, mm7);		// mm7 = y7 0 y5 0
-
-    psrld_i2r (16, mm4);		// mm4 = 0 y6 0 y4
-
-    por_r2r (mm4, mm7);			// mm7 = y7 y6 y5 y4
+static inline void mmx_row_head (int16_t * const row, const int offset,
+				 const int16_t * const table)
+{
+    movq_m2r (*(row+offset), mm2);	/* mm2 = x6 x4 x2 x0 */
+
+    movq_m2r (*(row+offset+4), mm5);	/* mm5 = x7 x5 x3 x1 */
+    movq_r2r (mm2, mm0);		/* mm0 = x6 x4 x2 x0 */
+
+    movq_m2r (*table, mm3);		/* mm3 = C6 C4 C2 C4 */
+    movq_r2r (mm5, mm6);		/* mm6 = x7 x5 x3 x1 */
+
+    punpckldq_r2r (mm0, mm0);		/* mm0 = x2 x0 x2 x0 */
+
+    movq_m2r (*(table+4), mm4);		/* mm4 = -C2 -C4 C6 C4 */
+    pmaddwd_r2r (mm0, mm3);		/* mm3 = C4*x0+C6*x2 C4*x0+C2*x2 */
+
+    movq_m2r (*(table+8), mm1);		/* mm1 = -C7 C3 C3 C1 */
+    punpckhdq_r2r (mm2, mm2);		/* mm2 = x6 x4 x6 x4 */
+}
+
+static inline void mmx_row (const int16_t * const table,
+			    const int32_t * const rounder)
+{
+    pmaddwd_r2r (mm2, mm4);		/* mm4 = -C4*x4-C2*x6 C4*x4+C6*x6 */
+    punpckldq_r2r (mm5, mm5);		/* mm5 = x3 x1 x3 x1 */
+
+    pmaddwd_m2r (*(table+16), mm0);	/* mm0 = C4*x0-C2*x2 C4*x0-C6*x2 */
+    punpckhdq_r2r (mm6, mm6);		/* mm6 = x7 x5 x7 x5 */
+
+    movq_m2r (*(table+12), mm7);	/* mm7 = -C5 -C1 C7 C5 */
+    pmaddwd_r2r (mm5, mm1);		/* mm1 = C3*x1-C7*x3 C1*x1+C3*x3 */
+
+    paddd_m2r (*rounder, mm3);		/* mm3 += rounder */
+    pmaddwd_r2r (mm6, mm7);		/* mm7 = -C1*x5-C5*x7 C5*x5+C7*x7 */
+
+    pmaddwd_m2r (*(table+20), mm2);	/* mm2 = C4*x4-C6*x6 -C4*x4+C2*x6 */
+    paddd_r2r (mm4, mm3);		/* mm3 = a1 a0 + rounder */
+
+    pmaddwd_m2r (*(table+24), mm5);	/* mm5 = C7*x1-C5*x3 C5*x1-C1*x3 */
+    movq_r2r (mm3, mm4);		/* mm4 = a1 a0 + rounder */
+
+    pmaddwd_m2r (*(table+28), mm6);	/* mm6 = C3*x5-C1*x7 C7*x5+C3*x7 */
+    paddd_r2r (mm7, mm1);		/* mm1 = b1 b0 */
+
+    paddd_m2r (*rounder, mm0);		/* mm0 += rounder */
+    psubd_r2r (mm1, mm3);		/* mm3 = a1-b1 a0-b0 + rounder */
+
+    psrad_i2r (ROW_SHIFT, mm3);		/* mm3 = y6 y7 */
+    paddd_r2r (mm4, mm1);		/* mm1 = a1+b1 a0+b0 + rounder */
+
+    paddd_r2r (mm2, mm0);		/* mm0 = a3 a2 + rounder */
+    psrad_i2r (ROW_SHIFT, mm1);		/* mm1 = y1 y0 */
+
+    paddd_r2r (mm6, mm5);		/* mm5 = b3 b2 */
+    movq_r2r (mm0, mm7);		/* mm7 = a3 a2 + rounder */
+
+    paddd_r2r (mm5, mm0);		/* mm0 = a3+b3 a2+b2 + rounder */
+    psubd_r2r (mm5, mm7);		/* mm7 = a3-b3 a2-b2 + rounder */
+}
+
+static inline void mmx_row_tail (int16_t * const row, const int store)
+{
+    psrad_i2r (ROW_SHIFT, mm0);		/* mm0 = y3 y2 */
+
+    psrad_i2r (ROW_SHIFT, mm7);		/* mm7 = y4 y5 */
+
+    packssdw_r2r (mm0, mm1);		/* mm1 = y3 y2 y1 y0 */
+
+    packssdw_r2r (mm3, mm7);		/* mm7 = y6 y7 y4 y5 */
+
+    movq_r2m (mm1, *(row+store));	/* save y3 y2 y1 y0 */
+    movq_r2r (mm7, mm4);		/* mm4 = y6 y7 y4 y5 */
+
+    pslld_i2r (16, mm7);		/* mm7 = y7 0 y5 0 */
+
+    psrld_i2r (16, mm4);		/* mm4 = 0 y6 0 y4 */
+
+    por_r2r (mm4, mm7);			/* mm7 = y7 y6 y5 y4 */
 
     /* slot */
 
-    movq_r2m (mm7, *(row+store+4));	// save y7 y6 y5 y4
-}
-
-static inline void mmx_row_mid (int16_t * row, int store,
-				int offset, int16_t * table)
-{
-    movq_m2r (*(row+offset), mm2);	// mm2 = x6 x4 x2 x0
-    psrad_i2r (ROW_SHIFT, mm0);		// mm0 = y3 y2
-
-    movq_m2r (*(row+offset+4), mm5);	// mm5 = x7 x5 x3 x1
-    psrad_i2r (ROW_SHIFT, mm7);		// mm7 = y4 y5
-
-    packssdw_r2r (mm0, mm1);		// mm1 = y3 y2 y1 y0
-    movq_r2r (mm5, mm6);		// mm6 = x7 x5 x3 x1
-
-    packssdw_r2r (mm3, mm7);		// mm7 = y6 y7 y4 y5
-    movq_r2r (mm2, mm0);		// mm0 = x6 x4 x2 x0
-
-    movq_r2m (mm1, *(row+store));	// save y3 y2 y1 y0
-    movq_r2r (mm7, mm1);		// mm1 = y6 y7 y4 y5
-
-    punpckldq_r2r (mm0, mm0);		// mm0 = x2 x0 x2 x0
-    psrld_i2r (16, mm7);		// mm7 = 0 y6 0 y4
-
-    movq_m2r (*table, mm3);		// mm3 = C6 C4 C2 C4
-    pslld_i2r (16, mm1);		// mm1 = y7 0 y5 0
-
-    movq_m2r (*(table+4), mm4);		// mm4 = -C2 -C4 C6 C4
-    por_r2r (mm1, mm7);			// mm7 = y7 y6 y5 y4
-
-    movq_m2r (*(table+8), mm1);		// mm1 = -C7 C3 C3 C1
-    punpckhdq_r2r (mm2, mm2);		// mm2 = x6 x4 x6 x4
-
-    movq_r2m (mm7, *(row+store+4));	// save y7 y6 y5 y4
-    pmaddwd_r2r (mm0, mm3);		// mm3 = C4*x0+C6*x2 C4*x0+C2*x2
+    movq_r2m (mm7, *(row+store+4));	/* save y7 y6 y5 y4 */
+}
+
+static inline void mmx_row_mid (int16_t * const row, const int store,
+				const int offset, const int16_t * const table)
+{
+    movq_m2r (*(row+offset), mm2);	/* mm2 = x6 x4 x2 x0 */
+    psrad_i2r (ROW_SHIFT, mm0);		/* mm0 = y3 y2 */
+
+    movq_m2r (*(row+offset+4), mm5);	/* mm5 = x7 x5 x3 x1 */
+    psrad_i2r (ROW_SHIFT, mm7);		/* mm7 = y4 y5 */
+
+    packssdw_r2r (mm0, mm1);		/* mm1 = y3 y2 y1 y0 */
+    movq_r2r (mm5, mm6);		/* mm6 = x7 x5 x3 x1 */
+
+    packssdw_r2r (mm3, mm7);		/* mm7 = y6 y7 y4 y5 */
+    movq_r2r (mm2, mm0);		/* mm0 = x6 x4 x2 x0 */
+
+    movq_r2m (mm1, *(row+store));	/* save y3 y2 y1 y0 */
+    movq_r2r (mm7, mm1);		/* mm1 = y6 y7 y4 y5 */
+
+    punpckldq_r2r (mm0, mm0);		/* mm0 = x2 x0 x2 x0 */
+    psrld_i2r (16, mm7);		/* mm7 = 0 y6 0 y4 */
+
+    movq_m2r (*table, mm3);		/* mm3 = C6 C4 C2 C4 */
+    pslld_i2r (16, mm1);		/* mm1 = y7 0 y5 0 */
+
+    movq_m2r (*(table+4), mm4);		/* mm4 = -C2 -C4 C6 C4 */
+    por_r2r (mm1, mm7);			/* mm7 = y7 y6 y5 y4 */
+
+    movq_m2r (*(table+8), mm1);		/* mm1 = -C7 C3 C3 C1 */
+    punpckhdq_r2r (mm2, mm2);		/* mm2 = x6 x4 x6 x4 */
+
+    movq_r2m (mm7, *(row+store+4));	/* save y7 y6 y5 y4 */
+    pmaddwd_r2r (mm0, mm3);		/* mm3 = C4*x0+C6*x2 C4*x0+C2*x2 */
 }
 
 
 #if 0
-// C column IDCT - its just here to document the MMXEXT and MMX versions
+/* C column IDCT - its just here to document the MMXEXT and MMX versions */
 static inline void idct_col (int16_t * col, int offset)
 {
 /* multiplication - as implemented on mmx */
@@ -389,178 +396,178 @@ static inline void idct_col (int16_t * c
 #endif
 
 
-// MMX column IDCT
-static inline void idct_col (int16_t * col, int offset)
+/* MMX column IDCT */
+static inline void idct_col (int16_t * const col, const int offset)
 {
 #define T1 13036
 #define T2 27146
 #define T3 43790
 #define C4 23170
 
-    static short _T1[] ATTR_ALIGN(8) = {T1,T1,T1,T1};
-    static short _T2[] ATTR_ALIGN(8) = {T2,T2,T2,T2};
-    static short _T3[] ATTR_ALIGN(8) = {T3,T3,T3,T3};
-    static short _C4[] ATTR_ALIGN(8) = {C4,C4,C4,C4};
+    static const short _T1[] ATTR_ALIGN(8) = {T1,T1,T1,T1};
+    static const short _T2[] ATTR_ALIGN(8) = {T2,T2,T2,T2};
+    static const short _T3[] ATTR_ALIGN(8) = {T3,T3,T3,T3};
+    static const short _C4[] ATTR_ALIGN(8) = {C4,C4,C4,C4};
 
     /* column code adapted from peter gubanov */
     /* http://www.elecard.com/peter/idct.shtml */
 
-    movq_m2r (*_T1, mm0);		// mm0 = T1
-
-    movq_m2r (*(col+offset+1*8), mm1);	// mm1 = x1
-    movq_r2r (mm0, mm2);		// mm2 = T1
-
-    movq_m2r (*(col+offset+7*8), mm4);	// mm4 = x7
-    pmulhw_r2r (mm1, mm0);		// mm0 = T1*x1
-
-    movq_m2r (*_T3, mm5);		// mm5 = T3
-    pmulhw_r2r (mm4, mm2);		// mm2 = T1*x7
-
-    movq_m2r (*(col+offset+5*8), mm6);	// mm6 = x5
-    movq_r2r (mm5, mm7);		// mm7 = T3-1
-
-    movq_m2r (*(col+offset+3*8), mm3);	// mm3 = x3
-    psubsw_r2r (mm4, mm0);		// mm0 = v17
-
-    movq_m2r (*_T2, mm4);		// mm4 = T2
-    pmulhw_r2r (mm3, mm5);		// mm5 = (T3-1)*x3
-
-    paddsw_r2r (mm2, mm1);		// mm1 = u17
-    pmulhw_r2r (mm6, mm7);		// mm7 = (T3-1)*x5
+    movq_m2r (*_T1, mm0);		/* mm0 = T1 */
+
+    movq_m2r (*(col+offset+1*8), mm1);	/* mm1 = x1 */
+    movq_r2r (mm0, mm2);		/* mm2 = T1 */
+
+    movq_m2r (*(col+offset+7*8), mm4);	/* mm4 = x7 */
+    pmulhw_r2r (mm1, mm0);		/* mm0 = T1*x1 */
+
+    movq_m2r (*_T3, mm5);		/* mm5 = T3 */
+    pmulhw_r2r (mm4, mm2);		/* mm2 = T1*x7 */
+
+    movq_m2r (*(col+offset+5*8), mm6);	/* mm6 = x5 */
+    movq_r2r (mm5, mm7);		/* mm7 = T3-1 */
+
+    movq_m2r (*(col+offset+3*8), mm3);	/* mm3 = x3 */
+    psubsw_r2r (mm4, mm0);		/* mm0 = v17 */
+
+    movq_m2r (*_T2, mm4);		/* mm4 = T2 */
+    pmulhw_r2r (mm3, mm5);		/* mm5 = (T3-1)*x3 */
+
+    paddsw_r2r (mm2, mm1);		/* mm1 = u17 */
+    pmulhw_r2r (mm6, mm7);		/* mm7 = (T3-1)*x5 */
 
     /* slot */
 
-    movq_r2r (mm4, mm2);		// mm2 = T2
-    paddsw_r2r (mm3, mm5);		// mm5 = T3*x3
-
-    pmulhw_m2r (*(col+offset+2*8), mm4);// mm4 = T2*x2
-    paddsw_r2r (mm6, mm7);		// mm7 = T3*x5
-
-    psubsw_r2r (mm6, mm5);		// mm5 = v35
-    paddsw_r2r (mm3, mm7);		// mm7 = u35
-
-    movq_m2r (*(col+offset+6*8), mm3);	// mm3 = x6
-    movq_r2r (mm0, mm6);		// mm6 = v17
-
-    pmulhw_r2r (mm3, mm2);		// mm2 = T2*x6
-    psubsw_r2r (mm5, mm0);		// mm0 = b3
-
-    psubsw_r2r (mm3, mm4);		// mm4 = v26
-    paddsw_r2r (mm6, mm5);		// mm5 = v12
-
-    movq_r2m (mm0, *(col+offset+3*8));	// save b3 in scratch0
-    movq_r2r (mm1, mm6);		// mm6 = u17
-
-    paddsw_m2r (*(col+offset+2*8), mm2);// mm2 = u26
-    paddsw_r2r (mm7, mm6);		// mm6 = b0
-
-    psubsw_r2r (mm7, mm1);		// mm1 = u12
-    movq_r2r (mm1, mm7);		// mm7 = u12
-
-    movq_m2r (*(col+offset+0*8), mm3);	// mm3 = x0
-    paddsw_r2r (mm5, mm1);		// mm1 = u12+v12
-
-    movq_m2r (*_C4, mm0);		// mm0 = C4/2
-    psubsw_r2r (mm5, mm7);		// mm7 = u12-v12
-
-    movq_r2m (mm6, *(col+offset+5*8));	// save b0 in scratch1
-    pmulhw_r2r (mm0, mm1);		// mm1 = b1/2
-
-    movq_r2r (mm4, mm6);		// mm6 = v26
-    pmulhw_r2r (mm0, mm7);		// mm7 = b2/2
-
-    movq_m2r (*(col+offset+4*8), mm5);	// mm5 = x4
-    movq_r2r (mm3, mm0);		// mm0 = x0
-
-    psubsw_r2r (mm5, mm3);		// mm3 = v04
-    paddsw_r2r (mm5, mm0);		// mm0 = u04
-
-    paddsw_r2r (mm3, mm4);		// mm4 = a1
-    movq_r2r (mm0, mm5);		// mm5 = u04
-
-    psubsw_r2r (mm6, mm3);		// mm3 = a2
-    paddsw_r2r (mm2, mm5);		// mm5 = a0
-
-    paddsw_r2r (mm1, mm1);		// mm1 = b1
-    psubsw_r2r (mm2, mm0);		// mm0 = a3
-
-    paddsw_r2r (mm7, mm7);		// mm7 = b2
-    movq_r2r (mm3, mm2);		// mm2 = a2
-
-    movq_r2r (mm4, mm6);		// mm6 = a1
-    paddsw_r2r (mm7, mm3);		// mm3 = a2+b2
-
-    psraw_i2r (COL_SHIFT, mm3);		// mm3 = y2
-    paddsw_r2r (mm1, mm4);		// mm4 = a1+b1
-
-    psraw_i2r (COL_SHIFT, mm4);		// mm4 = y1
-    psubsw_r2r (mm1, mm6);		// mm6 = a1-b1
-
-    movq_m2r (*(col+offset+5*8), mm1);	// mm1 = b0
-    psubsw_r2r (mm7, mm2);		// mm2 = a2-b2
-
-    psraw_i2r (COL_SHIFT, mm6);		// mm6 = y6
-    movq_r2r (mm5, mm7);		// mm7 = a0
-
-    movq_r2m (mm4, *(col+offset+1*8));	// save y1
-    psraw_i2r (COL_SHIFT, mm2);		// mm2 = y5
-
-    movq_r2m (mm3, *(col+offset+2*8));	// save y2
-    paddsw_r2r (mm1, mm5);		// mm5 = a0+b0
-
-    movq_m2r (*(col+offset+3*8), mm4);	// mm4 = b3
-    psubsw_r2r (mm1, mm7);		// mm7 = a0-b0
-
-    psraw_i2r (COL_SHIFT, mm5);		// mm5 = y0
-    movq_r2r (mm0, mm3);		// mm3 = a3
-
-    movq_r2m (mm2, *(col+offset+5*8));	// save y5
-    psubsw_r2r (mm4, mm3);		// mm3 = a3-b3
-
-    psraw_i2r (COL_SHIFT, mm7);		// mm7 = y7
-    paddsw_r2r (mm0, mm4);		// mm4 = a3+b3
-
-    movq_r2m (mm5, *(col+offset+0*8));	// save y0
-    psraw_i2r (COL_SHIFT, mm3);		// mm3 = y4
-
-    movq_r2m (mm6, *(col+offset+6*8));	// save y6
-    psraw_i2r (COL_SHIFT, mm4);		// mm4 = y3
-
-    movq_r2m (mm7, *(col+offset+7*8));	// save y7
-
-    movq_r2m (mm3, *(col+offset+4*8));	// save y4
-
-    movq_r2m (mm4, *(col+offset+3*8));	// save y3
-}
-
-
-static int32_t rounder0[] ATTR_ALIGN(8) =
+    movq_r2r (mm4, mm2);		/* mm2 = T2 */
+    paddsw_r2r (mm3, mm5);		/* mm5 = T3*x3 */
+
+    pmulhw_m2r (*(col+offset+2*8), mm4);/* mm4 = T2*x2 */
+    paddsw_r2r (mm6, mm7);		/* mm7 = T3*x5 */
+
+    psubsw_r2r (mm6, mm5);		/* mm5 = v35 */
+    paddsw_r2r (mm3, mm7);		/* mm7 = u35 */
+
+    movq_m2r (*(col+offset+6*8), mm3);	/* mm3 = x6 */
+    movq_r2r (mm0, mm6);		/* mm6 = v17 */
+
+    pmulhw_r2r (mm3, mm2);		/* mm2 = T2*x6 */
+    psubsw_r2r (mm5, mm0);		/* mm0 = b3 */
+
+    psubsw_r2r (mm3, mm4);		/* mm4 = v26 */
+    paddsw_r2r (mm6, mm5);		/* mm5 = v12 */
+
+    movq_r2m (mm0, *(col+offset+3*8));	/* save b3 in scratch0 */
+    movq_r2r (mm1, mm6);		/* mm6 = u17 */
+
+    paddsw_m2r (*(col+offset+2*8), mm2);/* mm2 = u26 */
+    paddsw_r2r (mm7, mm6);		/* mm6 = b0 */
+
+    psubsw_r2r (mm7, mm1);		/* mm1 = u12 */
+    movq_r2r (mm1, mm7);		/* mm7 = u12 */
+
+    movq_m2r (*(col+offset+0*8), mm3);	/* mm3 = x0 */
+    paddsw_r2r (mm5, mm1);		/* mm1 = u12+v12 */
+
+    movq_m2r (*_C4, mm0);		/* mm0 = C4/2 */
+    psubsw_r2r (mm5, mm7);		/* mm7 = u12-v12 */
+
+    movq_r2m (mm6, *(col+offset+5*8));	/* save b0 in scratch1 */
+    pmulhw_r2r (mm0, mm1);		/* mm1 = b1/2 */
+
+    movq_r2r (mm4, mm6);		/* mm6 = v26 */
+    pmulhw_r2r (mm0, mm7);		/* mm7 = b2/2 */
+
+    movq_m2r (*(col+offset+4*8), mm5);	/* mm5 = x4 */
+    movq_r2r (mm3, mm0);		/* mm0 = x0 */
+
+    psubsw_r2r (mm5, mm3);		/* mm3 = v04 */
+    paddsw_r2r (mm5, mm0);		/* mm0 = u04 */
+
+    paddsw_r2r (mm3, mm4);		/* mm4 = a1 */
+    movq_r2r (mm0, mm5);		/* mm5 = u04 */
+
+    psubsw_r2r (mm6, mm3);		/* mm3 = a2 */
+    paddsw_r2r (mm2, mm5);		/* mm5 = a0 */
+
+    paddsw_r2r (mm1, mm1);		/* mm1 = b1 */
+    psubsw_r2r (mm2, mm0);		/* mm0 = a3 */
+
+    paddsw_r2r (mm7, mm7);		/* mm7 = b2 */
+    movq_r2r (mm3, mm2);		/* mm2 = a2 */
+
+    movq_r2r (mm4, mm6);		/* mm6 = a1 */
+    paddsw_r2r (mm7, mm3);		/* mm3 = a2+b2 */
+
+    psraw_i2r (COL_SHIFT, mm3);		/* mm3 = y2 */
+    paddsw_r2r (mm1, mm4);		/* mm4 = a1+b1 */
+
+    psraw_i2r (COL_SHIFT, mm4);		/* mm4 = y1 */
+    psubsw_r2r (mm1, mm6);		/* mm6 = a1-b1 */
+
+    movq_m2r (*(col+offset+5*8), mm1);	/* mm1 = b0 */
+    psubsw_r2r (mm7, mm2);		/* mm2 = a2-b2 */
+
+    psraw_i2r (COL_SHIFT, mm6);		/* mm6 = y6 */
+    movq_r2r (mm5, mm7);		/* mm7 = a0 */
+
+    movq_r2m (mm4, *(col+offset+1*8));	/* save y1 */
+    psraw_i2r (COL_SHIFT, mm2);		/* mm2 = y5 */
+
+    movq_r2m (mm3, *(col+offset+2*8));	/* save y2 */
+    paddsw_r2r (mm1, mm5);		/* mm5 = a0+b0 */
+
+    movq_m2r (*(col+offset+3*8), mm4);	/* mm4 = b3 */
+    psubsw_r2r (mm1, mm7);		/* mm7 = a0-b0 */
+
+    psraw_i2r (COL_SHIFT, mm5);		/* mm5 = y0 */
+    movq_r2r (mm0, mm3);		/* mm3 = a3 */
+
+    movq_r2m (mm2, *(col+offset+5*8));	/* save y5 */
+    psubsw_r2r (mm4, mm3);		/* mm3 = a3-b3 */
+
+    psraw_i2r (COL_SHIFT, mm7);		/* mm7 = y7 */
+    paddsw_r2r (mm0, mm4);		/* mm4 = a3+b3 */
+
+    movq_r2m (mm5, *(col+offset+0*8));	/* save y0 */
+    psraw_i2r (COL_SHIFT, mm3);		/* mm3 = y4 */
+
+    movq_r2m (mm6, *(col+offset+6*8));	/* save y6 */
+    psraw_i2r (COL_SHIFT, mm4);		/* mm4 = y3 */
+
+    movq_r2m (mm7, *(col+offset+7*8));	/* save y7 */
+
+    movq_r2m (mm3, *(col+offset+4*8));	/* save y4 */
+
+    movq_r2m (mm4, *(col+offset+3*8));	/* save y3 */
+}
+
+
+static const int32_t rounder0[] ATTR_ALIGN(8) =
     rounder ((1 << (COL_SHIFT - 1)) - 0.5);
-static int32_t rounder4[] ATTR_ALIGN(8) = rounder (0);
-static int32_t rounder1[] ATTR_ALIGN(8) =
+static const int32_t rounder4[] ATTR_ALIGN(8) = rounder (0);
+static const int32_t rounder1[] ATTR_ALIGN(8) =
     rounder (1.25683487303);	/* C1*(C1/C4+C1+C7)/2 */
-static int32_t rounder7[] ATTR_ALIGN(8) =
+static const int32_t rounder7[] ATTR_ALIGN(8) =
     rounder (-0.25);		/* C1*(C7/C4+C7-C1)/2 */
-static int32_t rounder2[] ATTR_ALIGN(8) =
+static const int32_t rounder2[] ATTR_ALIGN(8) =
     rounder (0.60355339059);	/* C2 * (C6+C2)/2 */
-static int32_t rounder6[] ATTR_ALIGN(8) =
+static const int32_t rounder6[] ATTR_ALIGN(8) =
     rounder (-0.25);		/* C2 * (C6-C2)/2 */
-static int32_t rounder3[] ATTR_ALIGN(8) =
+static const int32_t rounder3[] ATTR_ALIGN(8) =
     rounder (0.087788325588);	/* C3*(-C3/C4+C3+C5)/2 */
-static int32_t rounder5[] ATTR_ALIGN(8) =
+static const int32_t rounder5[] ATTR_ALIGN(8) =
     rounder (-0.441341716183);	/* C3*(-C5/C4+C5-C3)/2 */
 
 
 #define declare_idct(idct,table,idct_row_head,idct_row,idct_row_tail,idct_row_mid)	\
-static inline void idct (int16_t * block)				\
+static inline void idct (int16_t * const block)				\
 {									\
-    static int16_t table04[] ATTR_ALIGN(16) =				\
+    static const int16_t table04[] ATTR_ALIGN(16) =			\
 	table (22725, 21407, 19266, 16384, 12873,  8867, 4520);		\
-    static int16_t table17[] ATTR_ALIGN(16) =				\
+    static const int16_t table17[] ATTR_ALIGN(16) =			\
 	table (31521, 29692, 26722, 22725, 17855, 12299, 6270);		\
-    static int16_t table26[] ATTR_ALIGN(16) =				\
+    static const int16_t table26[] ATTR_ALIGN(16) =			\
 	table (29692, 27969, 25172, 21407, 16819, 11585, 5906);		\
-    static int16_t table35[] ATTR_ALIGN(16) =				\
+    static const int16_t table35[] ATTR_ALIGN(16) =			\
 	table (26722, 25172, 22654, 19266, 15137, 10426, 5315);		\
 									\
     idct_row_head (block, 0*8, table04);				\
@@ -595,7 +602,8 @@ do {					\
     packuswb_r2r (r1, r0);		\
 } while (0)
 
-static void block_copy (int16_t * block, uint8_t * dest, int stride)
+static inline void block_copy (int16_t * const block, uint8_t * dest,
+			       const int stride)
 {
     movq_m2r (*(block+0*8), mm0);
     movq_m2r (*(block+0*8+4), mm1);
@@ -627,7 +635,8 @@ do {					\
     paddsw_m2r (*(block+offset+4), r2);	\
 } while (0)
 
-static void block_add (int16_t * block, uint8_t * dest, int stride)
+static inline void block_add (int16_t * const block, uint8_t * dest,
+			      const int stride)
 {
     movq_m2r (*dest, mm1);
     pxor_r2r (mm0, mm0);
@@ -654,41 +663,112 @@ static void block_add (int16_t * block, 
     movq_r2m (mm3, *(dest+stride));
 }
 
-static inline void block_zero (int16_t * block)   {       
-  pxor_r2r (mm0, mm0);
-  movq_r2m (mm0, *(block+0*4));
-  movq_r2m (mm0, *(block+1*4));       
-  movq_r2m (mm0, *(block+2*4));       
-  movq_r2m (mm0, *(block+3*4));       
-  movq_r2m (mm0, *(block+4*4));       
-  movq_r2m (mm0, *(block+5*4));       
-  movq_r2m (mm0, *(block+6*4));       
-  movq_r2m (mm0, *(block+7*4));       
-  movq_r2m (mm0, *(block+8*4));       
-  movq_r2m (mm0, *(block+9*4));       
-  movq_r2m (mm0, *(block+10*4));       
-  movq_r2m (mm0, *(block+11*4));       
-  movq_r2m (mm0, *(block+12*4));       
-  movq_r2m (mm0, *(block+13*4));       
-  movq_r2m (mm0, *(block+14*4));       
-  movq_r2m (mm0, *(block+15*4));   
-}
+
+static inline void block_zero (int16_t * const block)
+{
+    pxor_r2r (mm0, mm0);
+    movq_r2m (mm0, *(block+0*4));
+    movq_r2m (mm0, *(block+1*4));
+    movq_r2m (mm0, *(block+2*4));
+    movq_r2m (mm0, *(block+3*4));
+    movq_r2m (mm0, *(block+4*4));
+    movq_r2m (mm0, *(block+5*4));
+    movq_r2m (mm0, *(block+6*4));
+    movq_r2m (mm0, *(block+7*4));
+    movq_r2m (mm0, *(block+8*4));
+    movq_r2m (mm0, *(block+9*4));
+    movq_r2m (mm0, *(block+10*4));
+    movq_r2m (mm0, *(block+11*4));
+    movq_r2m (mm0, *(block+12*4));
+    movq_r2m (mm0, *(block+13*4));
+    movq_r2m (mm0, *(block+14*4));
+    movq_r2m (mm0, *(block+15*4));
+}
+
+
+#define CPU_MMXEXT 0
+#define CPU_MMX 1
+
+#define dup4(reg)			\
+do {					\
+    if (cpu != CPU_MMXEXT) {		\
+	punpcklwd_r2r (reg, reg);	\
+	punpckldq_r2r (reg, reg);	\
+    } else				\
+	pshufw_r2r (reg, reg, 0x00);	\
+} while (0)
+
+static inline void block_add_DC (int16_t * const block, uint8_t * dest,
+				 const int stride, const int cpu)
+{
+    movd_v2r ((block[0] + 4) >> 3, mm0);
+    pxor_r2r (mm1, mm1);
+    movq_m2r (*dest, mm2);
+    dup4 (mm0);
+    psubsw_r2r (mm0, mm1);
+    packuswb_r2r (mm0, mm0);
+    paddusb_r2r (mm0, mm2);
+    packuswb_r2r (mm1, mm1);
+    movq_m2r (*(dest + stride), mm3);
+    psubusb_r2r (mm1, mm2);
+    block[0] = 0;
+    paddusb_r2r (mm0, mm3);
+    movq_r2m (mm2, *dest);
+    psubusb_r2r (mm1, mm3);
+    movq_m2r (*(dest + 2*stride), mm2);
+    dest += stride;
+    movq_r2m (mm3, *dest);
+    paddusb_r2r (mm0, mm2);
+    movq_m2r (*(dest + 2*stride), mm3);
+    psubusb_r2r (mm1, mm2);
+    dest += stride;
+    paddusb_r2r (mm0, mm3);
+    movq_r2m (mm2, *dest);
+    psubusb_r2r (mm1, mm3);
+    movq_m2r (*(dest + 2*stride), mm2);
+    dest += stride;
+    movq_r2m (mm3, *dest);
+    paddusb_r2r (mm0, mm2);
+    movq_m2r (*(dest + 2*stride), mm3);
+    psubusb_r2r (mm1, mm2);
+    dest += stride;
+    paddusb_r2r (mm0, mm3);
+    movq_r2m (mm2, *dest);
+    psubusb_r2r (mm1, mm3);
+    movq_m2r (*(dest + 2*stride), mm2);
+    dest += stride;
+    movq_r2m (mm3, *dest);
+    paddusb_r2r (mm0, mm2);
+    movq_m2r (*(dest + 2*stride), mm3);
+    psubusb_r2r (mm1, mm2);
+    block[63] = 0;
+    paddusb_r2r (mm0, mm3);
+    movq_r2m (mm2, *(dest + stride));
+    psubusb_r2r (mm1, mm3);
+    movq_r2m (mm3, *(dest + 2*stride));
+}
+
 
 declare_idct (mmxext_idct, mmxext_table,
 	      mmxext_row_head, mmxext_row, mmxext_row_tail, mmxext_row_mid)
 
-void mpeg2_idct_copy_mmxext (int16_t * block, uint8_t * dest, int stride)
+void mpeg2_idct_copy_mmxext (int16_t * const block, uint8_t * const dest,
+			     const int stride)
 {
     mmxext_idct (block);
     block_copy (block, dest, stride);
     block_zero (block);
 }
 
-void mpeg2_idct_add_mmxext (int16_t * block, uint8_t * dest, int stride)
-{
-    mmxext_idct (block);
-    block_add (block, dest, stride);
-    block_zero (block);
+void mpeg2_idct_add_mmxext (const int last, int16_t * const block,
+			    uint8_t * const dest, const int stride)
+{
+    if (last != 129 || (block[0] & 7) == 4) {
+	mmxext_idct (block);
+	block_add (block, dest, stride);
+	block_zero (block);
+    } else
+	block_add_DC (block, dest, stride, CPU_MMXEXT);
 }
 
 void mpeg2_idct_mmxext (int16_t * block)
@@ -699,18 +779,23 @@ declare_idct (mmx_idct, mmx_table,
 declare_idct (mmx_idct, mmx_table,
 	      mmx_row_head, mmx_row, mmx_row_tail, mmx_row_mid)
 
-void mpeg2_idct_copy_mmx (int16_t * block, uint8_t * dest, int stride)
+void mpeg2_idct_copy_mmx (int16_t * const block, uint8_t * const dest,
+			  const int stride)
 {
     mmx_idct (block);
     block_copy (block, dest, stride);
     block_zero (block);
 }
 
-void mpeg2_idct_add_mmx (int16_t * block, uint8_t * dest, int stride)
-{
-    mmx_idct (block);
-    block_add (block, dest, stride);
-    block_zero (block);
+void mpeg2_idct_add_mmx (const int last, int16_t * const block,
+			 uint8_t * const dest, const int stride)
+{
+    if (last != 129 || (block[0] & 7) == 4) {
+	mmx_idct (block);
+	block_add (block, dest, stride);
+	block_zero (block);
+    } else
+	block_add_DC (block, dest, stride, CPU_MMX);
 }
 
 void mpeg2_idct_mmx (int16_t * block)
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/mmx.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmpeg2/mmx.h	Wed Jul 04 03:09:58 2007 +0300
@@ -0,0 +1,263 @@
+/*
+ * mmx.h
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
+ *
+ * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
+ * See http://libmpeg2.sourceforge.net/ for updates.
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ * The type of an value that fits in an MMX register (note that long
+ * long constant values MUST be suffixed by LL and unsigned long long
+ * values by ULL, lest they be truncated by the compiler)
+ */
+
+typedef	union {
+	long long		q;	/* Quadword (64-bit) value */
+	unsigned long long	uq;	/* Unsigned Quadword */
+	int			d[2];	/* 2 Doubleword (32-bit) values */
+	unsigned int		ud[2];	/* 2 Unsigned Doubleword */
+	short			w[4];	/* 4 Word (16-bit) values */
+	unsigned short		uw[4];	/* 4 Unsigned Word */
+	char			b[8];	/* 8 Byte (8-bit) values */
+	unsigned char		ub[8];	/* 8 Unsigned Byte */
+	float			s[2];	/* Single-precision (32-bit) value */
+} ATTR_ALIGN(8) mmx_t;	/* On an 8-byte (64-bit) boundary */
+
+
+#define	mmx_i2r(op,imm,reg) \
+	__asm__ __volatile__ (#op " %0, %%" #reg \
+			      : /* nothing */ \
+			      : "i" (imm) )
+
+#define	mmx_m2r(op,mem,reg) \
+	__asm__ __volatile__ (#op " %0, %%" #reg \
+			      : /* nothing */ \
+			      : "m" (mem))
+
+#define	mmx_r2m(op,reg,mem) \
+	__asm__ __volatile__ (#op " %%" #reg ", %0" \
+			      : "=m" (mem) \
+			      : /* nothing */ )
+
+#define	mmx_r2r(op,regs,regd) \
+	__asm__ __volatile__ (#op " %" #regs ", %" #regd)
+
+
+#define	emms() __asm__ __volatile__ ("emms")
+
+#define	movd_m2r(var,reg)	mmx_m2r (movd, var, reg)
+#define	movd_r2m(reg,var)	mmx_r2m (movd, reg, var)
+#define	movd_v2r(var,reg)	__asm__ __volatile__ ("movd %0, %%" #reg \
+						      : /* nothing */ \
+						      : "rm" (var))
+#define	movd_r2v(reg,var)	__asm__ __volatile__ ("movd %%" #reg ", %0" \
+						      : "=rm" (var) \
+						      : /* nothing */ )
+
+#define	movq_m2r(var,reg)	mmx_m2r (movq, var, reg)
+#define	movq_r2m(reg,var)	mmx_r2m (movq, reg, var)
+#define	movq_r2r(regs,regd)	mmx_r2r (movq, regs, regd)
+
+#define	packssdw_m2r(var,reg)	mmx_m2r (packssdw, var, reg)
+#define	packssdw_r2r(regs,regd) mmx_r2r (packssdw, regs, regd)
+#define	packsswb_m2r(var,reg)	mmx_m2r (packsswb, var, reg)
+#define	packsswb_r2r(regs,regd) mmx_r2r (packsswb, regs, regd)
+
+#define	packuswb_m2r(var,reg)	mmx_m2r (packuswb, var, reg)
+#define	packuswb_r2r(regs,regd) mmx_r2r (packuswb, regs, regd)
+
+#define	paddb_m2r(var,reg)	mmx_m2r (paddb, var, reg)
+#define	paddb_r2r(regs,regd)	mmx_r2r (paddb, regs, regd)
+#define	paddd_m2r(var,reg)	mmx_m2r (paddd, var, reg)
+#define	paddd_r2r(regs,regd)	mmx_r2r (paddd, regs, regd)
+#define	paddw_m2r(var,reg)	mmx_m2r (paddw, var, reg)
+#define	paddw_r2r(regs,regd)	mmx_r2r (paddw, regs, regd)
+
+#define	paddsb_m2r(var,reg)	mmx_m2r (paddsb, var, reg)
+#define	paddsb_r2r(regs,regd)	mmx_r2r (paddsb, regs, regd)
+#define	paddsw_m2r(var,reg)	mmx_m2r (paddsw, var, reg)
+#define	paddsw_r2r(regs,regd)	mmx_r2r (paddsw, regs, regd)
+
+#define	paddusb_m2r(var,reg)	mmx_m2r (paddusb, var, reg)
+#define	paddusb_r2r(regs,regd)	mmx_r2r (paddusb, regs, regd)
+#define	paddusw_m2r(var,reg)	mmx_m2r (paddusw, var, reg)
+#define	paddusw_r2r(regs,regd)	mmx_r2r (paddusw, regs, regd)
+
+#define	pand_m2r(var,reg)	mmx_m2r (pand, var, reg)
+#define	pand_r2r(regs,regd)	mmx_r2r (pand, regs, regd)
+
+#define	pandn_m2r(var,reg)	mmx_m2r (pandn, var, reg)
+#define	pandn_r2r(regs,regd)	mmx_r2r (pandn, regs, regd)
+
+#define	pcmpeqb_m2r(var,reg)	mmx_m2r (pcmpeqb, var, reg)
+#define	pcmpeqb_r2r(regs,regd)	mmx_r2r (pcmpeqb, regs, regd)
+#define	pcmpeqd_m2r(var,reg)	mmx_m2r (pcmpeqd, var, reg)
+#define	pcmpeqd_r2r(regs,regd)	mmx_r2r (pcmpeqd, regs, regd)
+#define	pcmpeqw_m2r(var,reg)	mmx_m2r (pcmpeqw, var, reg)
+#define	pcmpeqw_r2r(regs,regd)	mmx_r2r (pcmpeqw, regs, regd)
+
+#define	pcmpgtb_m2r(var,reg)	mmx_m2r (pcmpgtb, var, reg)
+#define	pcmpgtb_r2r(regs,regd)	mmx_r2r (pcmpgtb, regs, regd)
+#define	pcmpgtd_m2r(var,reg)	mmx_m2r (pcmpgtd, var, reg)
+#define	pcmpgtd_r2r(regs,regd)	mmx_r2r (pcmpgtd, regs, regd)
+#define	pcmpgtw_m2r(var,reg)	mmx_m2r (pcmpgtw, var, reg)
+#define	pcmpgtw_r2r(regs,regd)	mmx_r2r (pcmpgtw, regs, regd)
+
+#define	pmaddwd_m2r(var,reg)	mmx_m2r (pmaddwd, var, reg)
+#define	pmaddwd_r2r(regs,regd)	mmx_r2r (pmaddwd, regs, regd)
+
+#define	pmulhw_m2r(var,reg)	mmx_m2r (pmulhw, var, reg)
+#define	pmulhw_r2r(regs,regd)	mmx_r2r (pmulhw, regs, regd)
+
+#define	pmullw_m2r(var,reg)	mmx_m2r (pmullw, var, reg)
+#define	pmullw_r2r(regs,regd)	mmx_r2r (pmullw, regs, regd)
+
+#define	por_m2r(var,reg)	mmx_m2r (por, var, reg)
+#define	por_r2r(regs,regd)	mmx_r2r (por, regs, regd)
+
+#define	pslld_i2r(imm,reg)	mmx_i2r (pslld, imm, reg)
+#define	pslld_m2r(var,reg)	mmx_m2r (pslld, var, reg)
+#define	pslld_r2r(regs,regd)	mmx_r2r (pslld, regs, regd)
+#define	psllq_i2r(imm,reg)	mmx_i2r (psllq, imm, reg)
+#define	psllq_m2r(var,reg)	mmx_m2r (psllq, var, reg)
+#define	psllq_r2r(regs,regd)	mmx_r2r (psllq, regs, regd)
+#define	psllw_i2r(imm,reg)	mmx_i2r (psllw, imm, reg)
+#define	psllw_m2r(var,reg)	mmx_m2r (psllw, var, reg)
+#define	psllw_r2r(regs,regd)	mmx_r2r (psllw, regs, regd)
+
+#define	psrad_i2r(imm,reg)	mmx_i2r (psrad, imm, reg)
+#define	psrad_m2r(var,reg)	mmx_m2r (psrad, var, reg)
+#define	psrad_r2r(regs,regd)	mmx_r2r (psrad, regs, regd)
+#define	psraw_i2r(imm,reg)	mmx_i2r (psraw, imm, reg)
+#define	psraw_m2r(var,reg)	mmx_m2r (psraw, var, reg)
+#define	psraw_r2r(regs,regd)	mmx_r2r (psraw, regs, regd)
+
+#define	psrld_i2r(imm,reg)	mmx_i2r (psrld, imm, reg)
+#define	psrld_m2r(var,reg)	mmx_m2r (psrld, var, reg)
+#define	psrld_r2r(regs,regd)	mmx_r2r (psrld, regs, regd)
+#define	psrlq_i2r(imm,reg)	mmx_i2r (psrlq, imm, reg)
+#define	psrlq_m2r(var,reg)	mmx_m2r (psrlq, var, reg)
+#define	psrlq_r2r(regs,regd)	mmx_r2r (psrlq, regs, regd)
+#define	psrlw_i2r(imm,reg)	mmx_i2r (psrlw, imm, reg)
+#define	psrlw_m2r(var,reg)	mmx_m2r (psrlw, var, reg)
+#define	psrlw_r2r(regs,regd)	mmx_r2r (psrlw, regs, regd)
+
+#define	psubb_m2r(var,reg)	mmx_m2r (psubb, var, reg)
+#define	psubb_r2r(regs,regd)	mmx_r2r (psubb, regs, regd)
+#define	psubd_m2r(var,reg)	mmx_m2r (psubd, var, reg)
+#define	psubd_r2r(regs,regd)	mmx_r2r (psubd, regs, regd)
+#define	psubw_m2r(var,reg)	mmx_m2r (psubw, var, reg)
+#define	psubw_r2r(regs,regd)	mmx_r2r (psubw, regs, regd)
+
+#define	psubsb_m2r(var,reg)	mmx_m2r (psubsb, var, reg)
+#define	psubsb_r2r(regs,regd)	mmx_r2r (psubsb, regs, regd)
+#define	psubsw_m2r(var,reg)	mmx_m2r (psubsw, var, reg)
+#define	psubsw_r2r(regs,regd)	mmx_r2r (psubsw, regs, regd)
+
+#define	psubusb_m2r(var,reg)	mmx_m2r (psubusb, var, reg)
+#define	psubusb_r2r(regs,regd)	mmx_r2r (psubusb, regs, regd)
+#define	psubusw_m2r(var,reg)	mmx_m2r (psubusw, var, reg)
+#define	psubusw_r2r(regs,regd)	mmx_r2r (psubusw, regs, regd)
+
+#define	punpckhbw_m2r(var,reg)		mmx_m2r (punpckhbw, var, reg)
+#define	punpckhbw_r2r(regs,regd)	mmx_r2r (punpckhbw, regs, regd)
+#define	punpckhdq_m2r(var,reg)		mmx_m2r (punpckhdq, var, reg)
+#define	punpckhdq_r2r(regs,regd)	mmx_r2r (punpckhdq, regs, regd)
+#define	punpckhwd_m2r(var,reg)		mmx_m2r (punpckhwd, var, reg)
+#define	punpckhwd_r2r(regs,regd)	mmx_r2r (punpckhwd, regs, regd)
+
+#define	punpcklbw_m2r(var,reg) 		mmx_m2r (punpcklbw, var, reg)
+#define	punpcklbw_r2r(regs,regd)	mmx_r2r (punpcklbw, regs, regd)
+#define	punpckldq_m2r(var,reg)		mmx_m2r (punpckldq, var, reg)
+#define	punpckldq_r2r(regs,regd)	mmx_r2r (punpckldq, regs, regd)
+#define	punpcklwd_m2r(var,reg)		mmx_m2r (punpcklwd, var, reg)
+#define	punpcklwd_r2r(regs,regd)	mmx_r2r (punpcklwd, regs, regd)
+
+#define	pxor_m2r(var,reg)	mmx_m2r (pxor, var, reg)
+#define	pxor_r2r(regs,regd)	mmx_r2r (pxor, regs, regd)
+
+
+/* 3DNOW extensions */
+
+#define pavgusb_m2r(var,reg)	mmx_m2r (pavgusb, var, reg)
+#define pavgusb_r2r(regs,regd)	mmx_r2r (pavgusb, regs, regd)
+
+
+/* AMD MMX extensions - also available in intel SSE */
+
+
+#define mmx_m2ri(op,mem,reg,imm) \
+	__asm__ __volatile__ (#op " %1, %0, %%" #reg \
+			      : /* nothing */ \
+			      : "m" (mem), "i" (imm))
+
+#define mmx_r2ri(op,regs,regd,imm) \
+	__asm__ __volatile__ (#op " %0, %%" #regs ", %%" #regd \
+			      : /* nothing */ \
+			      : "i" (imm) )
+
+#define	mmx_fetch(mem,hint) \
+	__asm__ __volatile__ ("prefetch" #hint " %0" \
+			      : /* nothing */ \
+			      : "m" (mem))
+
+
+#define	maskmovq(regs,maskreg)		mmx_r2ri (maskmovq, regs, maskreg)
+
+#define	movntq_r2m(mmreg,var)		mmx_r2m (movntq, mmreg, var)
+
+#define	pavgb_m2r(var,reg)		mmx_m2r (pavgb, var, reg)
+#define	pavgb_r2r(regs,regd)		mmx_r2r (pavgb, regs, regd)
+#define	pavgw_m2r(var,reg)		mmx_m2r (pavgw, var, reg)
+#define	pavgw_r2r(regs,regd)		mmx_r2r (pavgw, regs, regd)
+
+#define	pextrw_r2r(mmreg,reg,imm)	mmx_r2ri (pextrw, mmreg, reg, imm)
+
+#define	pinsrw_r2r(reg,mmreg,imm)	mmx_r2ri (pinsrw, reg, mmreg, imm)
+
+#define	pmaxsw_m2r(var,reg)		mmx_m2r (pmaxsw, var, reg)
+#define	pmaxsw_r2r(regs,regd)		mmx_r2r (pmaxsw, regs, regd)
+
+#define	pmaxub_m2r(var,reg)		mmx_m2r (pmaxub, var, reg)
+#define	pmaxub_r2r(regs,regd)		mmx_r2r (pmaxub, regs, regd)
+
+#define	pminsw_m2r(var,reg)		mmx_m2r (pminsw, var, reg)
+#define	pminsw_r2r(regs,regd)		mmx_r2r (pminsw, regs, regd)
+
+#define	pminub_m2r(var,reg)		mmx_m2r (pminub, var, reg)
+#define	pminub_r2r(regs,regd)		mmx_r2r (pminub, regs, regd)
+
+#define	pmovmskb(mmreg,reg) \
+	__asm__ __volatile__ ("movmskps %" #mmreg ", %" #reg)
+
+#define	pmulhuw_m2r(var,reg)		mmx_m2r (pmulhuw, var, reg)
+#define	pmulhuw_r2r(regs,regd)		mmx_r2r (pmulhuw, regs, regd)
+
+#define	prefetcht0(mem)			mmx_fetch (mem, t0)
+#define	prefetcht1(mem)			mmx_fetch (mem, t1)
+#define	prefetcht2(mem)			mmx_fetch (mem, t2)
+#define	prefetchnta(mem)		mmx_fetch (mem, nta)
+
+#define	psadbw_m2r(var,reg)		mmx_m2r (psadbw, var, reg)
+#define	psadbw_r2r(regs,regd)		mmx_r2r (psadbw, regs, regd)
+
+#define	pshufw_m2r(var,reg,imm)		mmx_m2ri(pshufw, var, reg, imm)
+#define	pshufw_r2r(regs,regd,imm)	mmx_r2ri(pshufw, regs, regd, imm)
+
+#define	sfence() __asm__ __volatile__ ("sfence\n\t")
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp.c
--- a/src/libmpeg2/motion_comp.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/motion_comp.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * motion_comp.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -33,15 +33,6 @@ mpeg2_mc_t mpeg2_mc;
 
 void mpeg2_mc_init (uint32_t mm_accel)
 {
-#ifdef LIBMPEG2_MLIB
-    if (mm_accel & MM_ACCEL_MLIB) {
-#ifdef LOG
-	fprintf (stderr, "Using mediaLib for motion compensation\n");
-#endif
-	mpeg2_mc = mpeg2_mc_mlib;
-    }
-#endif
-
 #if defined(ARCH_X86) || defined(ARCH_X86_64)
     if (mm_accel & MM_ACCEL_X86_MMXEXT) {
 #ifdef LOG
@@ -99,8 +90,8 @@ void mpeg2_mc_init (uint32_t mm_accel)
 /* mc function template */
 
 #define MC_FUNC(op,xy)							\
-static void MC_##op##_##xy##_16_c (uint8_t * dest, uint8_t * ref,	\
-				 int stride, int height)		\
+static void MC_##op##_##xy##_16_c (uint8_t * dest, const uint8_t * ref,	\
+				   const int stride, int height)	\
 {									\
     do {								\
 	op (predict_##xy, 0);						\
@@ -123,8 +114,8 @@ static void MC_##op##_##xy##_16_c (uint8
 	dest += stride;							\
     } while (--height);							\
 }									\
-static void MC_##op##_##xy##_8_c (uint8_t * dest, uint8_t * ref,	\
-				int stride, int height)			\
+static void MC_##op##_##xy##_8_c (uint8_t * dest, const uint8_t * ref,	\
+				  const int stride, int height)		\
 {									\
     do {								\
 	op (predict_##xy, 0);						\
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp_alpha.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/libmpeg2/motion_comp_alpha.c	Wed Jul 04 03:09:58 2007 +0300
@@ -0,0 +1,253 @@
+/*
+ * motion_comp_alpha.c
+ * Copyright (C) 2002-2003 Falk Hueffner <falk@debian.org>
+ *
+ * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
+ * See http://libmpeg2.sourceforge.net/ for updates.
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "config.h"
+
+#ifdef ARCH_ALPHA
+
+#include <inttypes.h>
+
+#include "mpeg2.h"
+#include "attributes.h"
+#include "mpeg2_internal.h"
+#include "alpha_asm.h"
+
+static inline uint64_t avg2 (uint64_t a, uint64_t b)
+{
+    return (a | b) - (((a ^ b) & BYTE_VEC (0xfe)) >> 1);
+}
+
+// Load two unaligned quadwords from addr. This macro only works if
+// addr is actually unaligned.
+#define ULOAD16(ret_l,ret_r,addr)			\
+    do {						\
+	uint64_t _l = ldq_u (addr +  0);		\
+	uint64_t _m = ldq_u (addr +  8);		\
+	uint64_t _r = ldq_u (addr + 16);		\
+	ret_l = extql (_l, addr) | extqh (_m, addr);	\
+	ret_r = extql (_m, addr) | extqh (_r, addr);	\
+    } while (0)
+
+// Load two aligned quadwords from addr.
+#define ALOAD16(ret_l,ret_r,addr)			\
+    do {						\
+	ret_l = ldq (addr);				\
+	ret_r = ldq (addr + 8);				\
+    } while (0)
+
+#define OP8(LOAD,LOAD16,STORE)			\
+    do {					\
+	STORE (LOAD (pixels), block);		\
+	pixels += line_size;			\
+	block += line_size;			\
+    } while (--h)
+
+#define OP16(LOAD,LOAD16,STORE)			\
+    do {					\
+	uint64_t l, r;				\
+	LOAD16 (l, r, pixels);			\
+	STORE (l, block);			\
+	STORE (r, block + 8);			\
+	pixels += line_size;			\
+	block += line_size;			\
+    } while (--h)
+
+#define OP8_X2(LOAD,LOAD16,STORE)			\
+    do {						\
+	uint64_t p0, p1;				\
+							\
+	p0 = LOAD (pixels);				\
+	p1 = p0 >> 8 | ((uint64_t) pixels[8] << 56);	\
+	STORE (avg2 (p0, p1), block);			\
+	pixels += line_size;				\
+	block += line_size;				\
+    } while (--h)
+
+#define OP16_X2(LOAD,LOAD16,STORE)				\
+    do {							\
+	uint64_t p0, p1;					\
+								\
+	LOAD16 (p0, p1, pixels);				\
+	STORE (avg2(p0, p0 >> 8 | p1 << 56), block);		\
+	STORE (avg2(p1, p1 >> 8 | (uint64_t) pixels[16] << 56),	\
+	       block + 8);					\
+	pixels += line_size;					\
+	block += line_size;					\
+    } while (--h)
+
+#define OP8_Y2(LOAD,LOAD16,STORE)		\
+    do {					\
+	uint64_t p0, p1;			\
+	p0 = LOAD (pixels);			\
+	pixels += line_size;			\
+	p1 = LOAD (pixels);			\
+	do {					\
+	    uint64_t av = avg2 (p0, p1);	\
+	    if (--h == 0) line_size = 0;	\
+	    pixels += line_size;		\
+	    p0 = p1;				\
+	    p1 = LOAD (pixels);			\
+	    STORE (av, block);			\
+	    block += line_size;			\
+	} while (h);				\
+    } while (0)
+
+#define OP16_Y2(LOAD,LOAD16,STORE)		\
+    do {					\
+	uint64_t p0l, p0r, p1l, p1r;		\
+	LOAD16 (p0l, p0r, pixels);		\
+	pixels += line_size;			\
+	LOAD16 (p1l, p1r, pixels);		\
+	do {					\
+	    uint64_t avl, avr;			\
+	    if (--h == 0) line_size = 0;	\
+	    avl = avg2 (p0l, p1l);		\
+	    avr = avg2 (p0r, p1r);		\
+	    p0l = p1l;				\
+	    p0r = p1r;				\
+	    pixels += line_size;		\
+	    LOAD16 (p1l, p1r, pixels);		\
+	    STORE (avl, block);			\
+	    STORE (avr, block + 8);		\
+	    block += line_size;			\
+	} while (h);				\
+    } while (0)
+
+#define OP8_XY2(LOAD,LOAD16,STORE)				\
+    do {							\
+	uint64_t pl, ph;					\
+	uint64_t p1 = LOAD (pixels);				\
+	uint64_t p2 = p1 >> 8 | ((uint64_t) pixels[8] << 56);	\
+								\
+	ph = (((p1 & ~BYTE_VEC (0x03)) >> 2) +			\
+	      ((p2 & ~BYTE_VEC (0x03)) >> 2));			\
+	pl = ((p1 & BYTE_VEC (0x03)) +				\
+	      (p2 & BYTE_VEC (0x03)));				\
+								\
+	do {							\
+	    uint64_t npl, nph;					\
+								\
+	    pixels += line_size;				\
+	    p1 = LOAD (pixels);					\
+	    p2 = (p1 >> 8) | ((uint64_t) pixels[8] << 56);	\
+	    nph = (((p1 & ~BYTE_VEC (0x03)) >> 2) +		\
+	           ((p2 & ~BYTE_VEC (0x03)) >> 2));		\
+	    npl = ((p1 & BYTE_VEC (0x03)) +			\
+	           (p2 & BYTE_VEC (0x03)));			\
+								\
+	    STORE (ph + nph +					\
+		   (((pl + npl + BYTE_VEC (0x02)) >> 2) &	\
+		    BYTE_VEC (0x03)), block);			\
+								\
+	    block += line_size;					\
+            pl = npl;						\
+	    ph = nph;						\
+	} while (--h);						\
+    } while (0)
+
+#define OP16_XY2(LOAD,LOAD16,STORE)				\
+    do {							\
+	uint64_t p0, p1, p2, p3, pl_l, ph_l, pl_r, ph_r;	\
+	LOAD16 (p0, p2, pixels);				\
+	p1 = p0 >> 8 | (p2 << 56);				\
+	p3 = p2 >> 8 | ((uint64_t)pixels[16] << 56);		\
+								\
+	ph_l = (((p0 & ~BYTE_VEC (0x03)) >> 2) +		\
+	        ((p1 & ~BYTE_VEC (0x03)) >> 2));		\
+	pl_l = ((p0 & BYTE_VEC (0x03)) +			\
+	        (p1 & BYTE_VEC(0x03)));				\
+	ph_r = (((p2 & ~BYTE_VEC (0x03)) >> 2) +		\
+	        ((p3 & ~BYTE_VEC (0x03)) >> 2));		\
+	pl_r = ((p2 & BYTE_VEC (0x03)) +			\
+	        (p3 & BYTE_VEC (0x03)));			\
+								\
+	do {							\
+	    uint64_t npl_l, nph_l, npl_r, nph_r;		\
+								\
+	    pixels += line_size;				\
+	    LOAD16 (p0, p2, pixels);				\
+	    p1 = p0 >> 8 | (p2 << 56);				\
+	    p3 = p2 >> 8 | ((uint64_t)pixels[16] << 56);	\
+	    nph_l = (((p0 & ~BYTE_VEC (0x03)) >> 2) +		\
+		     ((p1 & ~BYTE_VEC (0x03)) >> 2));		\
+	    npl_l = ((p0 & BYTE_VEC (0x03)) +			\
+		     (p1 & BYTE_VEC (0x03)));			\
+	    nph_r = (((p2 & ~BYTE_VEC (0x03)) >> 2) +		\
+		     ((p3 & ~BYTE_VEC (0x03)) >> 2));		\
+	    npl_r = ((p2 & BYTE_VEC (0x03)) +			\
+		     (p3 & BYTE_VEC (0x03)));			\
+								\
+	    STORE (ph_l + nph_l +				\
+		   (((pl_l + npl_l + BYTE_VEC (0x02)) >> 2) &	\
+		    BYTE_VEC(0x03)), block);			\
+	    STORE (ph_r + nph_r +				\
+		   (((pl_r + npl_r + BYTE_VEC (0x02)) >> 2) &	\
+		    BYTE_VEC(0x03)), block + 8);		\
+								\
+	    block += line_size;					\
+	    pl_l = npl_l;					\
+	    ph_l = nph_l;					\
+	    pl_r = npl_r;					\
+	    ph_r = nph_r;					\
+	} while (--h);						\
+    } while (0)
+
+#define MAKE_OP(OPNAME,SIZE,SUFF,OPKIND,STORE)				\
+static void MC_ ## OPNAME ## _ ## SUFF ## _ ## SIZE ## _alpha		\
+	(uint8_t *restrict block, const uint8_t *restrict pixels,	\
+	 int line_size, int h)						\
+{									\
+    if ((uint64_t) pixels & 0x7) {					\
+	OPKIND (uldq, ULOAD16, STORE);					\
+    } else {								\
+	OPKIND (ldq, ALOAD16, STORE);					\
+    }									\
+}
+
+#define PIXOP(OPNAME,STORE)			\
+    MAKE_OP (OPNAME, 8,  o,  OP8,      STORE);	\
+    MAKE_OP (OPNAME, 8,  x,  OP8_X2,   STORE);	\
+    MAKE_OP (OPNAME, 8,  y,  OP8_Y2,   STORE);	\
+    MAKE_OP (OPNAME, 8,  xy, OP8_XY2,  STORE);	\
+    MAKE_OP (OPNAME, 16, o,  OP16,     STORE);	\
+    MAKE_OP (OPNAME, 16, x,  OP16_X2,  STORE);	\
+    MAKE_OP (OPNAME, 16, y,  OP16_Y2,  STORE);	\
+    MAKE_OP (OPNAME, 16, xy, OP16_XY2, STORE);
+
+#define STORE(l,b) stq (l, b)
+PIXOP (put, STORE);
+#undef STORE
+#define STORE(l,b) stq (avg2 (l, ldq (b)), b);
+PIXOP (avg, STORE);
+
+mpeg2_mc_t mpeg2_mc_alpha = {
+    { MC_put_o_16_alpha, MC_put_x_16_alpha,
+      MC_put_y_16_alpha, MC_put_xy_16_alpha,
+      MC_put_o_8_alpha, MC_put_x_8_alpha,
+      MC_put_y_8_alpha, MC_put_xy_8_alpha },
+    { MC_avg_o_16_alpha, MC_avg_x_16_alpha,
+      MC_avg_y_16_alpha, MC_avg_xy_16_alpha,
+      MC_avg_o_8_alpha, MC_avg_x_8_alpha,
+      MC_avg_y_8_alpha, MC_avg_xy_8_alpha }
+};
+
+#endif
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp_altivec.c
--- a/src/libmpeg2/motion_comp_altivec.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/motion_comp_altivec.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * motion_comp_altivec.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -23,1065 +23,51 @@
 
 #include "config.h"
 
-#ifndef HOST_OS_DARWIN
-
 #if defined (ARCH_PPC) && defined (ENABLE_ALTIVEC)
 
+#ifdef HAVE_ALTIVEC_H
+#include <altivec.h>
+#endif
+#include <inttypes.h>
+
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
 
-#include <inttypes.h>
-
-/*
- * The asm code is generated with:
- *
- * gcc-2.95 -fvec -DHOST_OS_DARWIN -O9 -fomit-frame-pointer -mregnames -S
- *      motion_comp_altivec.c
- *
- * sed 's/.L/._L/g' motion_comp_altivec.s |
- * awk '{args=""; len=split ($2, arg, ",");
- *      for (i=1; i<=len; i++) { a=arg[i]; if (i<len) a=a",";
- *                               args = args sprintf ("%-6s", a) }
- *      printf ("\t\"\t%-16s%-24s\\n\"\n", $1, args) }' |
- * unexpand -a
- */
-
-static void MC_put_o_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	li		%r9,  15		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	lvsl		%v12, 0,    %r4		\n"
-	"	mtctr		%r6			\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	add		%r0,  %r5,  %r5		\n"
-	"	vperm		%v13, %v1,  %v0,  %v12	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"._L6:						\n"
-	"	li		%r9,  15		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	stvx		%v13, 0,    %r3		\n"
-	"	vperm		%v13, %v1,  %v0,  %v12	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	stvx		%v13, %r5,  %r3		\n"
-	"	vperm		%v13, %v1,  %v0,  %v12	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r0		\n"
-	"	bdnz		._L6			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	stvx		%v13, 0,    %r3		\n"
-	"	vperm		%v13, %v1,  %v0,  %v12	\n"
-	"	stvx		%v13, %r5,  %r3		\n"
-	 );
-}
-
-static void MC_put_o_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v12, 0,    %r4		\n"
-	"	lvsl		%v1,  %r5,  %r4		\n"
-	"	vmrghb		%v12, %v12, %v12	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	li		%r9,  7			\n"
-	"	vmrghb		%v1,  %v1,  %v1		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vpkuhum		%v10, %v12, %v12	\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	mtctr		%r6			\n"
-	"	vpkuhum		%v11, %v1,  %v1		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v12, %v13, %v0,  %v10	\n"
-	"._L11:						\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	stvewx		%v12, 0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v1,  %v13, %v0,  %v11	\n"
-	"	stvewx		%v12, %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	vperm		%v12, %v13, %v0,  %v10	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	bdnz		._L11			\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	stvewx		%v12, 0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v1,  %v13, %v0,  %v11	\n"
-	"	stvewx		%v12, %r9,  %r3		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	 );
-}
-
-static void MC_put_x_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v11, 0,    %r4		\n"
-	"	vspltisb	%v0,  1			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	vaddubm		%v10, %v11, %v0		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v1,  %v12, %v13, %v10	\n"
-	"	vperm		%v0,  %v12, %v13, %v11	\n"
-	"	mtctr		%r6			\n"
-	"	add		%r0,  %r5,  %r5		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"._L16:						\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	vperm		%v1,  %v12, %v13, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v0,  %v12, %v13, %v11	\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	"	vperm		%v1,  %v12, %v13, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v0,  %v12, %v13, %v11	\n"
-	"	add		%r3,  %r3,  %r0		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	bdnz		._L16			\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	vperm		%v1,  %v12, %v13, %v10	\n"
-	"	vperm		%v0,  %v12, %v13, %v11	\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	 );
-}
-
-static void MC_put_x_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v0,  0,    %r4		\n"
-	"	vspltisb	%v13, 1			\n"
-	"	lvsl		%v10, %r5,  %r4		\n"
-	"	vmrghb		%v0,  %v0,  %v0		\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	vmrghb		%v10, %v10, %v10	\n"
-	"	vpkuhum		%v8,  %v0,  %v0		\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vpkuhum		%v9,  %v10, %v10	\n"
-	"	vaddubm		%v7,  %v8,  %v13	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v1,  %v11, %v12, %v8	\n"
-	"	mtctr		%r6			\n"
-	"	vaddubm		%v13, %v9,  %v13	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v0,  %v11, %v12, %v7	\n"
-	"	vavgub		%v0,  %v1,  %v0		\n"
-	"._L21:						\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v1,  %v11, %v12, %v13	\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v9	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	vavgub		%v10, %v0,  %v1		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	vperm		%v1,  %v11, %v12, %v7	\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	bdnz		._L21			\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v1,  %v11, %v12, %v13	\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v9	\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v10, %v0,  %v1		\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	 );
-}
-
-static void MC_put_y_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	li		%r9,  15		\n"
-	"	lvsl		%v10, 0,    %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v12, %v13, %v1,  %v10	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v11, %v13, %v1,  %v10	\n"
-	"	mtctr		%r6			\n"
-	"	add		%r0,  %r5,  %r5		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v12, %v11	\n"
-	"._L26:						\n"
-	"	li		%r9,  15		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	vperm		%v12, %v13, %v1,  %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	vavgub		%v0,  %v12, %v11	\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	"	vperm		%v11, %v13, %v1,  %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r0		\n"
-	"	vavgub		%v0,  %v12, %v11	\n"
-	"	bdnz		._L26			\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	vperm		%v12, %v13, %v1,  %v10	\n"
-	"	vavgub		%v0,  %v12, %v11	\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	 );
-}
-
-static void MC_put_y_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v13, 0,    %r4		\n"
-	"	lvsl		%v11, %r5,  %r4		\n"
-	"	vmrghb		%v13, %v13, %v13	\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	vmrghb		%v11, %v11, %v11	\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	vpkuhum		%v9,  %v13, %v13	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vpkuhum		%v10, %v11, %v11	\n"
-	"	vperm		%v13, %v12, %v1,  %v9	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v11, %v12, %v1,  %v10	\n"
-	"	mtctr		%r6			\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v13, %v11	\n"
-	"._L31:						\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v13, %v12, %v1,  %v9	\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v13, %v11	\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	vperm		%v11, %v12, %v1,  %v10	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	"	vavgub		%v0,  %v13, %v11	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	bdnz		._L31			\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v12, 0,    %r4		\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v13, %v12, %v1,  %v9	\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v0,  %v13, %v11	\n"
-	"	stvewx		%v0,  0,    %r3		\n"
-	"	stvewx		%v0,  %r9,  %r3		\n"
-	 );
-}
-
-static void MC_put_xy_16_altivec (uint8_t * dest, uint8_t * ref,
-				  int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v5,  0,    %r4		\n"
-	"	vspltisb	%v3,  1			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	vaddubm		%v4,  %v5,  %v3		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v1,  %v0,  %v4	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	mtctr		%r6			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	add		%r0,  %r5,  %r5		\n"
-	"	vperm		%v10, %v1,  %v0,  %v4	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	vxor		%v6,  %v11, %v10	\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v0,  %v8,  %v6		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v0,  %v3,  %v0		\n"
-	"	vavgub		%v1,  %v9,  %v7		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v13, %v1,  %v0		\n"
-	"._L36:						\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	stvx		%v13, 0,    %r3		\n"
-	"	vperm		%v10, %v1,  %v0,  %v4	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v1,  %v0,  %v4	\n"
-	"	vavgub		%v12, %v9,  %v7		\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	vor		%v13, %v8,  %v6		\n"
-	"	vxor		%v0,  %v9,  %v7		\n"
-	"	vxor		%v6,  %v11, %v10	\n"
-	"	vand		%v13, %v3,  %v13	\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v1,  %v8,  %v6		\n"
-	"	vand		%v13, %v13, %v0		\n"
-	"	vxor		%v0,  %v9,  %v7		\n"
-	"	vand		%v1,  %v3,  %v1		\n"
-	"	vsububm		%v13, %v12, %v13	\n"
-	"	vand		%v1,  %v1,  %v0		\n"
-	"	stvx		%v13, %r5,  %r3		\n"
-	"	vavgub		%v0,  %v9,  %v7		\n"
-	"	add		%r3,  %r3,  %r0		\n"
-	"	vsububm		%v13, %v0,  %v1		\n"
-	"	bdnz		._L36			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	stvx		%v13, 0,    %r3		\n"
-	"	vperm		%v10, %v1,  %v0,  %v4	\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vor		%v0,  %v8,  %v6		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v0,  %v3,  %v0		\n"
-	"	vavgub		%v1,  %v9,  %v7		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v13, %v1,  %v0		\n"
-	"	stvx		%v13, %r5,  %r3		\n"
-	 );
-}
-
-static void MC_put_xy_8_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v4,  0,    %r4		\n"
-	"	vspltisb	%v3,  1			\n"
-	"	lvsl		%v5,  %r5,  %r4		\n"
-	"	vmrghb		%v4,  %v4,  %v4		\n"
-	"	li		%r9,  16		\n"
-	"	vmrghb		%v5,  %v5,  %v5		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	vpkuhum		%v4,  %v4,  %v4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	vpkuhum		%v5,  %v5,  %v5		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vaddubm		%v2,  %v4,  %v3		\n"
-	"	vperm		%v11, %v1,  %v0,  %v4	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vaddubm		%v19, %v5,  %v3		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v10, %v1,  %v0,  %v2	\n"
-	"	mtctr		%r6			\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v1,  %v0,  %v19	\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	vxor		%v6,  %v11, %v10	\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v0,  %v8,  %v6		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v0,  %v3,  %v0		\n"
-	"	vavgub		%v1,  %v9,  %v7		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v13, %v1,  %v0		\n"
-	"._L41:						\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v10, %v1,  %v0,  %v2	\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	vperm		%v11, %v1,  %v0,  %v4	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  16		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	vavgub		%v12, %v9,  %v7		\n"
-	"	vor		%v13, %v8,  %v6		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vperm		%v10, %v1,  %v0,  %v19	\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v11, %v1,  %v0,  %v5	\n"
-	"	vand		%v13, %v3,  %v13	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vxor		%v0,  %v9,  %v7		\n"
-	"	vxor		%v6,  %v11, %v10	\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v1,  %v8,  %v6		\n"
-	"	vand		%v13, %v13, %v0		\n"
-	"	vxor		%v0,  %v9,  %v7		\n"
-	"	vand		%v1,  %v3,  %v1		\n"
-	"	vsububm		%v13, %v12, %v13	\n"
-	"	vand		%v1,  %v1,  %v0		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	vavgub		%v0,  %v9,  %v7		\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vsububm		%v13, %v0,  %v1		\n"
-	"	bdnz		._L41			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	vperm		%v10, %v1,  %v0,  %v2	\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v11, %v1,  %v0,  %v4	\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vor		%v0,  %v8,  %v6		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v0,  %v3,  %v0		\n"
-	"	vavgub		%v1,  %v9,  %v7		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v13, %v1,  %v0		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	 );
-}
-
-static void MC_avg_o_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	li		%r9,  15		\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvsl		%v11, 0,    %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v0,  %v1,  %v0,  %v11	\n"
-	"	lvx		%v13, 0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	add		%r9,  %r5,  %r5		\n"
-	"	vavgub		%v12, %v13, %v0		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"._L46:						\n"
-	"	li		%r11, 15		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r11, %r4		\n"
-	"	lvx		%v13, %r5,  %r3		\n"
-	"	vperm		%v0,  %v1,  %v0,  %v11	\n"
-	"	stvx		%v12, 0,    %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v12, %v13, %v0		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v0,  %r11, %r4		\n"
-	"	lvx		%v13, %r9,  %r3		\n"
-	"	vperm		%v0,  %v1,  %v0,  %v11	\n"
-	"	stvx		%v12, %r5,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v12, %v13, %v0		\n"
-	"	add		%r3,  %r3,  %r9		\n"
-	"	bdnz		._L46			\n"
-	"	lvx		%v0,  %r11, %r4		\n"
-	"	lvx		%v1,  0,    %r4		\n"
-	"	lvx		%v13, %r5,  %r3		\n"
-	"	vperm		%v0,  %v1,  %v0,  %v11	\n"
-	"	stvx		%v12, 0,    %r3		\n"
-	"	vavgub		%v12, %v13, %v0		\n"
-	"	stvx		%v12, %r5,  %r3		\n"
-	 );
-}
-
-static void MC_avg_o_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v12, 0,    %r4		\n"
-	"	li		%r9,  7			\n"
-	"	vmrghb		%v12, %v12, %v12	\n"
-	"	lvsl		%v1,  %r5,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	vpkuhum		%v9,  %v12, %v12	\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vmrghb		%v1,  %v1,  %v1		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v0,  %v13, %v0,  %v9	\n"
-	"	lvx		%v11, 0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	vpkuhum		%v10, %v1,  %v1		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v12, %v11, %v0		\n"
-	"._L51:						\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v11, %r5,  %r3		\n"
-	"	stvewx		%v12, 0,    %r3		\n"
-	"	vperm		%v0,  %v13, %v0,  %v10	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v12, %r9,  %r3		\n"
-	"	vavgub		%v1,  %v11, %v0		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v11, %r5,  %r3		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	vperm		%v0,  %v13, %v0,  %v9	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	"	vavgub		%v12, %v11, %v0		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	bdnz		._L51			\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v0,  %r9,  %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v11, %r5,  %r3		\n"
-	"	stvewx		%v12, 0,    %r3		\n"
-	"	vperm		%v0,  %v13, %v0,  %v10	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v12, %r9,  %r3		\n"
-	"	vavgub		%v1,  %v11, %v0		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	 );
-}
-
-static void MC_avg_x_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v8,  0,    %r4		\n"
-	"	vspltisb	%v0,  1			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	vaddubm		%v7,  %v8,  %v0		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vperm		%v1,  %v11, %v12, %v7	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	lvx		%v9,  0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	add		%r9,  %r5,  %r5		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"._L56:						\n"
-	"	li		%r11, 16		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v12, %r11, %r4		\n"
-	"	lvx		%v9,  %r5,  %r3		\n"
-	"	stvx		%v10, 0,    %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v7	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v1,  %v11, %v12, %v8	\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v12, %r11, %r4		\n"
-	"	vavgub		%v1,  %v1,  %v0		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v13, %v11, %v12, %v7	\n"
-	"	vavgub		%v10, %v9,  %v1		\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	lvx		%v9,  %r9,  %r3		\n"
-	"	stvx		%v10, %r5,  %r3		\n"
-	"	vavgub		%v0,  %v0,  %v13	\n"
-	"	add		%r3,  %r3,  %r9		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"	bdnz		._L56			\n"
-	"	lvx		%v12, %r11, %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v9,  %r5,  %r3		\n"
-	"	vperm		%v1,  %v11, %v12, %v7	\n"
-	"	stvx		%v10, 0,    %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"	stvx		%v10, %r5,  %r3		\n"
-	 );
-}
-
-static void MC_avg_x_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v10, 0,    %r4		\n"
-	"	vspltisb	%v13, 1			\n"
-	"	li		%r9,  8			\n"
-	"	vmrghb		%v10, %v10, %v10	\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	vpkuhum		%v7,  %v10, %v10	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	lvsl		%v10, %r5,  %r4		\n"
-	"	vaddubm		%v6,  %v7,  %v13	\n"
-	"	vperm		%v0,  %v11, %v12, %v7	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vmrghb		%v10, %v10, %v10	\n"
-	"	lvx		%v9,  0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	vperm		%v1,  %v11, %v12, %v6	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vpkuhum		%v8,  %v10, %v10	\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	vaddubm		%v13, %v8,  %v13	\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"._L61:						\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v9,  %r5,  %r3		\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	vperm		%v1,  %v11, %v12, %v13	\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vperm		%v1,  %v11, %v12, %v6	\n"
-	"	lvx		%v9,  %r5,  %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v7	\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"	bdnz		._L61			\n"
-	"	li		%r9,  8			\n"
-	"	lvx		%v12, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v9,  %r5,  %r3		\n"
-	"	vperm		%v1,  %v11, %v12, %v13	\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	vperm		%v0,  %v11, %v12, %v8	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	"	vavgub		%v0,  %v0,  %v1		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v10, %v9,  %v0		\n"
-	"	stvewx		%v10, 0,    %r3		\n"
-	"	stvewx		%v10, %r9,  %r3		\n"
-	 );
-}
-
-static void MC_avg_y_16_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	li		%r9,  15		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvsl		%v9,  0,    %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v11, %v13, %v1,  %v9	\n"
-	"	li		%r11, 15		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vperm		%v10, %v13, %v1,  %v9	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	lvx		%v12, 0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	vavgub		%v0,  %v11, %v10	\n"
-	"	add		%r9,  %r5,  %r5		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v12, %v0		\n"
-	"._L66:						\n"
-	"	li		%r11, 15		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	lvx		%v12, %r5,  %r3		\n"
-	"	vperm		%v11, %v13, %v1,  %v9	\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v11, %v10	\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	vavgub		%v0,  %v12, %v0		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	lvx		%v12, %r9,  %r3		\n"
-	"	vperm		%v10, %v13, %v1,  %v9	\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	"	vavgub		%v0,  %v11, %v10	\n"
-	"	add		%r3,  %r3,  %r9		\n"
-	"	vavgub		%v0,  %v12, %v0		\n"
-	"	bdnz		._L66			\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v12, %r5,  %r3		\n"
-	"	vperm		%v11, %v13, %v1,  %v9	\n"
-	"	stvx		%v0,  0,    %r3		\n"
-	"	vavgub		%v0,  %v11, %v10	\n"
-	"	vavgub		%v0,  %v12, %v0		\n"
-	"	stvx		%v0,  %r5,  %r3		\n"
-	 );
-}
-
-static void MC_avg_y_8_altivec (uint8_t * dest, uint8_t * ref,
-				int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v12, 0,    %r4		\n"
-	"	lvsl		%v9,  %r5,  %r4		\n"
-	"	vmrghb		%v12, %v12, %v12	\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	vmrghb		%v9,  %v9,  %v9		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	vpkuhum		%v7,  %v12, %v12	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vpkuhum		%v8,  %v9,  %v9		\n"
-	"	vperm		%v12, %v11, %v13, %v7	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v9,  %v11, %v13, %v8	\n"
-	"	lvx		%v10, 0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v0,  %v12, %v9		\n"
-	"	vavgub		%v1,  %v10, %v0		\n"
-	"._L71:						\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v10, %r5,  %r3		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	vperm		%v12, %v11, %v13, %v7	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	"	vavgub		%v0,  %v12, %v9		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  7			\n"
-	"	vavgub		%v1,  %v10, %v0		\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vperm		%v9,  %v11, %v13, %v8	\n"
-	"	lvx		%v10, %r5,  %r3		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	vavgub		%v0,  %v12, %v9		\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vavgub		%v1,  %v10, %v0		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	bdnz		._L71			\n"
-	"	li		%r9,  7			\n"
-	"	lvx		%v13, %r9,  %r4		\n"
-	"	lvx		%v11, 0,    %r4		\n"
-	"	lvx		%v10, %r5,  %r3		\n"
-	"	vperm		%v12, %v11, %v13, %v7	\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	li		%r9,  4			\n"
-	"	vavgub		%v0,  %v12, %v9		\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v1,  %v10, %v0		\n"
-	"	stvewx		%v1,  0,    %r3		\n"
-	"	stvewx		%v1,  %r9,  %r3		\n"
-	 );
-}
-
-static void MC_avg_xy_16_altivec (uint8_t * dest, uint8_t * ref,
-				  int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v4,  0,    %r4		\n"
-	"	vspltisb	%v2,  1			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	vaddubm		%v3,  %v4,  %v2		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v13, %v1,  %v3	\n"
-	"	li		%r11, 16		\n"
-	"	vperm		%v11, %v13, %v1,  %v4	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v10, %v13, %v1,  %v3	\n"
-	"	lvx		%v6,  0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	vperm		%v11, %v13, %v1,  %v4	\n"
-	"	add		%r9,  %r5,  %r5		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vxor		%v5,  %v11, %v10	\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v1,  %v8,  %v5		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v1,  %v2,  %v1		\n"
-	"	vavgub		%v0,  %v9,  %v7		\n"
-	"	vand		%v1,  %v1,  %v13	\n"
-	"	vsububm		%v0,  %v0,  %v1		\n"
-	"	vavgub		%v12, %v6,  %v0		\n"
-	"._L76:						\n"
-	"	li		%r11, 16		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	lvx		%v6,  %r5,  %r3		\n"
-	"	stvx		%v12, 0,    %r3		\n"
-	"	vperm		%v10, %v13, %v1,  %v3	\n"
-	"	vperm		%v11, %v13, %v1,  %v4	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v13, %v1,  %v3	\n"
-	"	vavgub		%v12, %v9,  %v7		\n"
-	"	vperm		%v11, %v13, %v1,  %v4	\n"
-	"	vor		%v0,  %v8,  %v5		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vxor		%v5,  %v11, %v10	\n"
-	"	vand		%v0,  %v2,  %v0		\n"
-	"	vavgub		%v7,  %v11, %v10	\n"
-	"	vor		%v1,  %v8,  %v5		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vand		%v1,  %v2,  %v1		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vsububm		%v12, %v12, %v0		\n"
-	"	vand		%v1,  %v1,  %v13	\n"
-	"	vavgub		%v0,  %v9,  %v7		\n"
-	"	vavgub		%v12, %v6,  %v12	\n"
-	"	lvx		%v6,  %r9,  %r3		\n"
-	"	vsububm		%v0,  %v0,  %v1		\n"
-	"	stvx		%v12, %r5,  %r3		\n"
-	"	vavgub		%v12, %v6,  %v0		\n"
-	"	add		%r3,  %r3,  %r9		\n"
-	"	bdnz		._L76			\n"
-	"	lvx		%v1,  %r11, %r4		\n"
-	"	lvx		%v13, 0,    %r4		\n"
-	"	lvx		%v6,  %r5,  %r3		\n"
-	"	vperm		%v10, %v13, %v1,  %v3	\n"
-	"	stvx		%v12, 0,    %r3		\n"
-	"	vperm		%v11, %v13, %v1,  %v4	\n"
-	"	vxor		%v8,  %v11, %v10	\n"
-	"	vavgub		%v9,  %v11, %v10	\n"
-	"	vor		%v0,  %v8,  %v5		\n"
-	"	vxor		%v13, %v9,  %v7		\n"
-	"	vand		%v0,  %v2,  %v0		\n"
-	"	vavgub		%v1,  %v9,  %v7		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v1,  %v1,  %v0		\n"
-	"	vavgub		%v12, %v6,  %v1		\n"
-	"	stvx		%v12, %r5,  %r3		\n"
-	 );
-}
-
-static void MC_avg_xy_8_altivec (uint8_t * dest, uint8_t * ref,
-				 int stride, int height)
-{
-    asm ("						\n"
-	"	lvsl		%v2,  0,    %r4		\n"
-	"	vspltisb	%v19, 1			\n"
-	"	lvsl		%v3,  %r5,  %r4		\n"
-	"	vmrghb		%v2,  %v2,  %v2		\n"
-	"	li		%r9,  16		\n"
-	"	vmrghb		%v3,  %v3,  %v3		\n"
-	"	lvx		%v9,  0,    %r4		\n"
-	"	vpkuhum		%v2,  %v2,  %v2		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	vpkuhum		%v3,  %v3,  %v3		\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vaddubm		%v18, %v2,  %v19	\n"
-	"	vperm		%v11, %v9,  %v1,  %v2	\n"
-	"	srawi		%r6,  %r6,  1		\n"
-	"	vaddubm		%v17, %v3,  %v19	\n"
-	"	addi		%r6,  %r6,  -1		\n"
-	"	vperm		%v10, %v9,  %v1,  %v18	\n"
-	"	lvx		%v4,  0,    %r3		\n"
-	"	mtctr		%r6			\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v9,  0,    %r4		\n"
-	"	vavgub		%v8,  %v11, %v10	\n"
-	"	vxor		%v7,  %v11, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vperm		%v10, %v9,  %v1,  %v17	\n"
-	"	vperm		%v11, %v9,  %v1,  %v3	\n"
-	"	vxor		%v5,  %v11, %v10	\n"
-	"	vavgub		%v6,  %v11, %v10	\n"
-	"	vor		%v1,  %v7,  %v5		\n"
-	"	vxor		%v13, %v8,  %v6		\n"
-	"	vand		%v1,  %v19, %v1		\n"
-	"	vavgub		%v0,  %v8,  %v6		\n"
-	"	vand		%v1,  %v1,  %v13	\n"
-	"	vsububm		%v0,  %v0,  %v1		\n"
-	"	vavgub		%v13, %v4,  %v0		\n"
-	"._L81:						\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v9,  0,    %r4		\n"
-	"	lvx		%v4,  %r5,  %r3		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	vperm		%v10, %v9,  %v1,  %v18	\n"
-	"	vperm		%v11, %v9,  %v1,  %v2	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	vxor		%v7,  %v11, %v10	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	li		%r9,  16		\n"
-	"	vavgub		%v8,  %v11, %v10	\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	vor		%v0,  %v7,  %v5		\n"
-	"	lvx		%v9,  0,    %r4		\n"
-	"	vxor		%v12, %v8,  %v6		\n"
-	"	vand		%v0,  %v19, %v0		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vperm		%v10, %v9,  %v1,  %v17	\n"
-	"	vavgub		%v13, %v8,  %v6		\n"
-	"	li		%r9,  4			\n"
-	"	vperm		%v11, %v9,  %v1,  %v3	\n"
-	"	vand		%v0,  %v0,  %v12	\n"
-	"	add		%r4,  %r4,  %r5		\n"
-	"	vxor		%v5,  %v11, %v10	\n"
-	"	vavgub		%v6,  %v11, %v10	\n"
-	"	vor		%v1,  %v7,  %v5		\n"
-	"	vsububm		%v13, %v13, %v0		\n"
-	"	vxor		%v0,  %v8,  %v6		\n"
-	"	vand		%v1,  %v19, %v1		\n"
-	"	vavgub		%v13, %v4,  %v13	\n"
-	"	vand		%v1,  %v1,  %v0		\n"
-	"	lvx		%v4,  %r5,  %r3		\n"
-	"	vavgub		%v0,  %v8,  %v6		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	vsububm		%v0,  %v0,  %v1		\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v13, %v4,  %v0		\n"
-	"	bdnz		._L81			\n"
-	"	li		%r9,  16		\n"
-	"	lvx		%v1,  %r9,  %r4		\n"
-	"	lvx		%v9,  0,    %r4		\n"
-	"	lvx		%v4,  %r5,  %r3		\n"
-	"	vperm		%v10, %v9,  %v1,  %v18	\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	vperm		%v11, %v9,  %v1,  %v2	\n"
-	"	li		%r9,  4			\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	"	vxor		%v7,  %v11, %v10	\n"
-	"	add		%r3,  %r3,  %r5		\n"
-	"	vavgub		%v8,  %v11, %v10	\n"
-	"	vor		%v0,  %v7,  %v5		\n"
-	"	vxor		%v13, %v8,  %v6		\n"
-	"	vand		%v0,  %v19, %v0		\n"
-	"	vavgub		%v1,  %v8,  %v6		\n"
-	"	vand		%v0,  %v0,  %v13	\n"
-	"	vsububm		%v1,  %v1,  %v0		\n"
-	"	vavgub		%v13, %v4,  %v1		\n"
-	"	stvewx		%v13, 0,    %r3		\n"
-	"	stvewx		%v13, %r9,  %r3		\n"
-	 );
-}
-
-MPEG2_MC_EXTERN (altivec)
-
-#endif	/* ARCH_PPC */
-
-#else	/* HOST_OS_DARWIN */
-
-#ifdef ENABLE_ALTIVEC
-
-#include "mpeg2_internal.h"
-
-#define vector_s16_t vector signed short
-#define vector_u16_t vector unsigned short
-#define vector_s8_t vector signed char
-#define vector_u8_t vector unsigned char
-#define vector_s32_t vector signed int
-#define vector_u32_t vector unsigned int
-
-void MC_put_o_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+typedef vector signed char vector_s8_t;
+typedef vector unsigned char vector_u8_t;
+typedef vector signed short vector_s16_t;
+typedef vector unsigned short vector_u16_t;
+typedef vector signed int vector_s32_t;
+typedef vector unsigned int vector_u32_t;
+
+#ifndef COFFEE_BREAK	/* Workarounds for gcc suckage */
+
+static inline vector_u8_t my_vec_ld (int const A, const uint8_t * const B)
+{
+    return vec_ld (A, (uint8_t *)B);
+}
+#undef vec_ld
+#define vec_ld my_vec_ld
+
+static inline vector_u8_t my_vec_and (vector_u8_t const A, vector_u8_t const B)
+{
+    return vec_and (A, B);
+}
+#undef vec_and
+#define vec_and my_vec_and
+
+static inline vector_u8_t my_vec_avg (vector_u8_t const A, vector_u8_t const B)
+{
+    return vec_avg (A, B);
+}
+#undef vec_avg
+#define vec_avg my_vec_avg
+
+#endif
+
+static void MC_put_o_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm, ref0, ref1, tmp;
 
@@ -1116,8 +102,8 @@ void MC_put_o_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_put_o_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_put_o_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0, perm1, tmp0, tmp1, ref0, ref1;
 
@@ -1163,8 +149,8 @@ void MC_put_o_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp1, 4, (unsigned int *)dest);
 }
 
-void MC_put_x_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_put_x_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t permA, permB, ref0, ref1, tmp;
 
@@ -1204,8 +190,8 @@ void MC_put_x_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_put_x_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_put_x_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0A, perm0B, perm1A, perm1B, ones, tmp0, tmp1, ref0, ref1;
 
@@ -1258,8 +244,8 @@ void MC_put_x_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp1, 4, (unsigned int *)dest);
 }
 
-void MC_put_y_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_put_y_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm, ref0, ref1, tmp0, tmp1, tmp;
 
@@ -1302,8 +288,8 @@ void MC_put_y_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_put_y_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_put_y_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0, perm1, tmp0, tmp1, tmp, ref0, ref1;
 
@@ -1357,8 +343,8 @@ void MC_put_y_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
 }
 
-void MC_put_xy_16_altivec (unsigned char * dest, unsigned char * ref,
-			   int stride, int height)
+static void MC_put_xy_16_altivec (uint8_t * dest, const uint8_t * ref,
+				  const int stride, int height)
 {
     vector_u8_t permA, permB, ref0, ref1, A, B, avg0, avg1, xor0, xor1, tmp;
     vector_u8_t ones;
@@ -1387,7 +373,6 @@ void MC_put_xy_16_altivec (unsigned char
     tmp = vec_sub (vec_avg (avg0, avg1),
 		   vec_and (vec_and (ones, vec_or (xor0, xor1)),
 			    vec_xor (avg0, avg1)));
-
 
     do {
 	ref0 = vec_ld (0, ref);
@@ -1429,8 +414,8 @@ void MC_put_xy_16_altivec (unsigned char
     vec_st (tmp, stride, dest);
 }
 
-void MC_put_xy_8_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_put_xy_8_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm0A, perm0B, perm1A, perm1B, ref0, ref1, A, B;
     vector_u8_t avg0, avg1, xor0, xor1, tmp, ones;
@@ -1448,7 +433,7 @@ void MC_put_xy_8_altivec (unsigned char 
     height = (height >> 1) - 1;
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     ref += stride;
     A = vec_perm (ref0, ref1, perm0A);
     B = vec_perm (ref0, ref1, perm0B);
@@ -1456,7 +441,7 @@ void MC_put_xy_8_altivec (unsigned char 
     xor0 = vec_xor (A, B);
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     ref += stride;
     A = vec_perm (ref0, ref1, perm1A);
     B = vec_perm (ref0, ref1, perm1B);
@@ -1466,10 +451,9 @@ void MC_put_xy_8_altivec (unsigned char 
 		   vec_and (vec_and (ones, vec_or (xor0, xor1)),
 			    vec_xor (avg0, avg1)));
 
-
-    do {
-	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+    do {
+	ref0 = vec_ld (0, ref);
+	ref1 = vec_ld (8, ref);
 	ref += stride;
 	vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
 	vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
@@ -1483,7 +467,7 @@ void MC_put_xy_8_altivec (unsigned char 
 				vec_xor (avg0, avg1)));
 
 	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+	ref1 = vec_ld (8, ref);
 	ref += stride;
 	vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
 	vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
@@ -1498,7 +482,7 @@ void MC_put_xy_8_altivec (unsigned char 
     } while (--height);
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
     dest += stride;
@@ -1514,8 +498,8 @@ void MC_put_xy_8_altivec (unsigned char 
 }
 
 #if 0
-void MC_put_xy_8_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_put_xy_8_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t permA, permB, ref0, ref1, A, B, C, D, tmp, zero, ones;
     vector_u16_t splat2, temp;
@@ -1529,12 +513,12 @@ void MC_put_xy_8_altivec (unsigned char 
 
     do {
 	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+	ref1 = vec_ld (8, ref);
 	ref += stride;
 	A = vec_perm (ref0, ref1, permA);
 	B = vec_perm (ref0, ref1, permB);
 	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+	ref1 = vec_ld (8, ref);
 	C = vec_perm (ref0, ref1, permA);
 	D = vec_perm (ref0, ref1, permB);
 
@@ -1553,8 +537,8 @@ void MC_put_xy_8_altivec (unsigned char 
 }
 #endif
 
-void MC_avg_o_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_avg_o_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm, ref0, ref1, tmp, prev;
 
@@ -1593,8 +577,8 @@ void MC_avg_o_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_avg_o_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_avg_o_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0, perm1, tmp0, tmp1, ref0, ref1, prev;
 
@@ -1644,8 +628,8 @@ void MC_avg_o_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp1, 4, (unsigned int *)dest);
 }
 
-void MC_avg_x_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_avg_x_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t permA, permB, ref0, ref1, tmp, prev;
 
@@ -1689,8 +673,8 @@ void MC_avg_x_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_avg_x_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_avg_x_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0A, perm0B, perm1A, perm1B, ones, tmp0, tmp1, ref0, ref1;
     vector_u8_t prev;
@@ -1748,8 +732,8 @@ void MC_avg_x_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp1, 4, (unsigned int *)dest);
 }
 
-void MC_avg_y_16_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_avg_y_16_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm, ref0, ref1, tmp0, tmp1, tmp, prev;
 
@@ -1796,8 +780,8 @@ void MC_avg_y_16_altivec (unsigned char 
     vec_st (tmp, stride, dest);
 }
 
-void MC_avg_y_8_altivec (unsigned char * dest, unsigned char * ref,
-			 int stride, int height)
+static void MC_avg_y_8_altivec (uint8_t * dest, const uint8_t * ref,
+				const int stride, int height)
 {
     vector_u8_t perm0, perm1, tmp0, tmp1, tmp, ref0, ref1, prev;
 
@@ -1855,8 +839,8 @@ void MC_avg_y_8_altivec (unsigned char *
     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
 }
 
-void MC_avg_xy_16_altivec (unsigned char * dest, unsigned char * ref,
-			   int stride, int height)
+static void MC_avg_xy_16_altivec (uint8_t * dest, const uint8_t * ref,
+				  const int stride, int height)
 {
     vector_u8_t permA, permB, ref0, ref1, A, B, avg0, avg1, xor0, xor1, tmp;
     vector_u8_t ones, prev;
@@ -1886,7 +870,6 @@ void MC_avg_xy_16_altivec (unsigned char
     tmp = vec_avg (prev, vec_sub (vec_avg (avg0, avg1),
 				  vec_and (vec_and (ones, vec_or (xor0, xor1)),
 					   vec_xor (avg0, avg1))));
-
 
     do {
 	ref0 = vec_ld (0, ref);
@@ -1933,8 +916,8 @@ void MC_avg_xy_16_altivec (unsigned char
     vec_st (tmp, stride, dest);
 }
 
-void MC_avg_xy_8_altivec (unsigned char * dest, unsigned char * ref,
-			  int stride, int height)
+static void MC_avg_xy_8_altivec (uint8_t * dest, const uint8_t * ref,
+				 const int stride, int height)
 {
     vector_u8_t perm0A, perm0B, perm1A, perm1B, ref0, ref1, A, B;
     vector_u8_t avg0, avg1, xor0, xor1, tmp, ones, prev;
@@ -1952,7 +935,7 @@ void MC_avg_xy_8_altivec (unsigned char 
     height = (height >> 1) - 1;
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     ref += stride;
     A = vec_perm (ref0, ref1, perm0A);
     B = vec_perm (ref0, ref1, perm0B);
@@ -1960,7 +943,7 @@ void MC_avg_xy_8_altivec (unsigned char 
     xor0 = vec_xor (A, B);
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     ref += stride;
     prev = vec_ld (0, dest);
     A = vec_perm (ref0, ref1, perm1A);
@@ -1971,10 +954,9 @@ void MC_avg_xy_8_altivec (unsigned char 
 				  vec_and (vec_and (ones, vec_or (xor0, xor1)),
 					   vec_xor (avg0, avg1))));
 
-
-    do {
-	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+    do {
+	ref0 = vec_ld (0, ref);
+	ref1 = vec_ld (8, ref);
 	ref += stride;
 	prev = vec_ld (stride, dest);
 	vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
@@ -1990,7 +972,7 @@ void MC_avg_xy_8_altivec (unsigned char 
 					 vec_xor (avg0, avg1))));
 
 	ref0 = vec_ld (0, ref);
-	ref1 = vec_ld (16, ref);
+	ref1 = vec_ld (8, ref);
 	ref += stride;
 	prev = vec_ld (stride, dest);
 	vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
@@ -2007,7 +989,7 @@ void MC_avg_xy_8_altivec (unsigned char 
     } while (--height);
 
     ref0 = vec_ld (0, ref);
-    ref1 = vec_ld (16, ref);
+    ref1 = vec_ld (8, ref);
     prev = vec_ld (stride, dest);
     vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);
     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
@@ -2025,7 +1007,4 @@ void MC_avg_xy_8_altivec (unsigned char 
 
 MPEG2_MC_EXTERN (altivec)
 
-#endif /* ENABLE_ALTIVEC */
-
-#endif	/* HOST_OS_DARWIN */
-
+#endif
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp_mlib.c
--- a/src/libmpeg2/motion_comp_mlib.c	Mon Jun 25 13:05:45 2007 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,181 +0,0 @@
-/*
- * motion_comp_mlib.c
- * Copyright (C) 2000-2002 Håkan Hjort <d95hjort@dtek.chalmers.se>
- *
- * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
- * See http://libmpeg2.sourceforge.net/ for updates.
- *
- * mpeg2dec is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpeg2dec is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include "config.h"
-
-#ifdef LIBMPEG2_MLIB
-
-#include <mlib_types.h>
-#include <mlib_status.h>
-#include <mlib_sys.h>
-#include <mlib_video.h>
-#include <inttypes.h>
-
-#include "mpeg2_internal.h"
-
-static void MC_put_o_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16) 
-	mlib_VideoCopyRef_U8_U8_16x16 (dest, ref, stride);
-    else
-	mlib_VideoCopyRef_U8_U8_16x8 (dest, ref, stride);
-}
-
-static void MC_put_x_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpX_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpX_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_put_y_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpY_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpY_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_put_xy_16_mlib (uint8_t * dest, uint8_t * ref,
-			       int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpXY_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpXY_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_put_o_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoCopyRef_U8_U8_8x8 (dest, ref, stride);
-    else
-	mlib_VideoCopyRef_U8_U8_8x4 (dest, ref, stride);
-}
-
-static void MC_put_x_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoInterpX_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpX_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-static void MC_put_y_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoInterpY_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpY_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-static void MC_put_xy_8_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 8) 
-	mlib_VideoInterpXY_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpXY_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-static void MC_avg_o_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoCopyRefAve_U8_U8_16x16 (dest, ref, stride);
-    else
-	mlib_VideoCopyRefAve_U8_U8_16x8 (dest, ref, stride);
-}
-
-static void MC_avg_x_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpAveX_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveX_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_avg_y_16_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpAveY_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveY_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_avg_xy_16_mlib (uint8_t * dest, uint8_t * ref,
-			       int stride, int height)
-{
-    if (height == 16)
-	mlib_VideoInterpAveXY_U8_U8_16x16 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveXY_U8_U8_16x8 (dest, ref, stride, stride);
-}
-
-static void MC_avg_o_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoCopyRefAve_U8_U8_8x8 (dest, ref, stride);
-    else
-	mlib_VideoCopyRefAve_U8_U8_8x4 (dest, ref, stride);
-}
-
-static void MC_avg_x_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoInterpAveX_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveX_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-static void MC_avg_y_8_mlib (uint8_t * dest, uint8_t * ref,
-			     int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoInterpAveY_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveY_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-static void MC_avg_xy_8_mlib (uint8_t * dest, uint8_t * ref,
-			      int stride, int height)
-{
-    if (height == 8)
-	mlib_VideoInterpAveXY_U8_U8_8x8 (dest, ref, stride, stride);
-    else
-	mlib_VideoInterpAveXY_U8_U8_8x4 (dest, ref, stride, stride);
-}
-
-MPEG2_MC_EXTERN (mlib)
-
-#endif
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp_mmx.c
--- a/src/libmpeg2/motion_comp_mmx.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/motion_comp_mmx.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * motion_comp_mmx.c
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -27,8 +27,10 @@
 
 #include <inttypes.h>
 
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
-#include "xineutils.h"
+#include "mmx.h"
 
 #define CPU_MMXEXT 0
 #define CPU_3DNOW 1
@@ -36,8 +38,21 @@
 
 /* MMX code - needs a rewrite */
 
+/*
+ * Motion Compensation frequently needs to average values using the
+ * formula (x+y+1)>>1. Both MMXEXT and 3Dnow include one instruction
+ * to compute this, but it's been left out of classic MMX.
+ *
+ * We need to be careful of overflows when doing this computation.
+ * Rather than unpacking data to 16-bits, which reduces parallelism,
+ * we use the following formulas:
+ *
+ * (x+y)>>1 == (x&y)+((x^y)>>1)
+ * (x+y+1)>>1 == (x|y)-((x^y)>>1)
+ */
+
 /* some rounding constants */
-static mmx_t round1 = {0x0001000100010001LL};
+static mmx_t mask1 = {0xfefefefefefefefeLL};
 static mmx_t round4 = {0x0002000200020002LL};
 
 /*
@@ -54,202 +69,176 @@ static inline void mmx_zero_reg ()
     pxor_r2r (mm0, mm0);
 }
 
-static inline void mmx_average_2_U8 (uint8_t * dest,
-				     uint8_t * src1, uint8_t * src2)
+static inline void mmx_average_2_U8 (uint8_t * dest, const uint8_t * src1,
+				     const uint8_t * src2)
 {
     /* *dest = (*src1 + *src2 + 1)/ 2; */
 
-    movq_m2r (*src1, mm1);	// load 8 src1 bytes
-    movq_r2r (mm1, mm2);	// copy 8 src1 bytes
-
-    movq_m2r (*src2, mm3);	// load 8 src2 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src2 bytes
-
-    punpcklbw_r2r (mm0, mm1);	// unpack low src1 bytes
-    punpckhbw_r2r (mm0, mm2);	// unpack high src1 bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src2 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src2 bytes
-
-    paddw_r2r (mm3, mm1);	// add lows to mm1
-    paddw_m2r (round1, mm1);
-    psraw_i2r (1, mm1);		// /2
-
-    paddw_r2r (mm4, mm2);	// add highs to mm2
-    paddw_m2r (round1, mm2);
-    psraw_i2r (1, mm2);		// /2
-
-    packuswb_r2r (mm2, mm1);	// pack (w/ saturation)
-    movq_r2m (mm1, *dest);	// store result in dest
+    movq_m2r (*src1, mm1);	/* load 8 src1 bytes */
+    movq_r2r (mm1, mm2);	/* copy 8 src1 bytes */
+
+    movq_m2r (*src2, mm3);	/* load 8 src2 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src2 bytes */
+
+    pxor_r2r (mm1, mm3);	/* xor src1 and src2 */
+    pand_m2r (mask1, mm3);	/* mask lower bits */
+    psrlq_i2r (1, mm3);		/* /2 */
+    por_r2r (mm2, mm4);		/* or src1 and src2 */
+    psubb_r2r (mm3, mm4);	/* subtract subresults */
+    movq_r2m (mm4, *dest);	/* store result in dest */
 }
 
 static inline void mmx_interp_average_2_U8 (uint8_t * dest,
-					    uint8_t * src1, uint8_t * src2)
+					    const uint8_t * src1,
+					    const uint8_t * src2)
 {
     /* *dest = (*dest + (*src1 + *src2 + 1)/ 2 + 1)/ 2; */
 
-    movq_m2r (*dest, mm1);	// load 8 dest bytes
-    movq_r2r (mm1, mm2);	// copy 8 dest bytes
-
-    movq_m2r (*src1, mm3);	// load 8 src1 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src1 bytes
-
-    movq_m2r (*src2, mm5);	// load 8 src2 bytes
-    movq_r2r (mm5, mm6);	// copy 8 src2 bytes
-
-    punpcklbw_r2r (mm0, mm1);	// unpack low dest bytes
-    punpckhbw_r2r (mm0, mm2);	// unpack high dest bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src1 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src1 bytes
-
-    punpcklbw_r2r (mm0, mm5);	// unpack low src2 bytes
-    punpckhbw_r2r (mm0, mm6);	// unpack high src2 bytes
-
-    paddw_r2r (mm5, mm3);	// add lows
-    paddw_m2r (round1, mm3);
-    psraw_i2r (1, mm3);		// /2
-
-    paddw_r2r (mm6, mm4);	// add highs
-    paddw_m2r (round1, mm4);
-    psraw_i2r (1, mm4);		// /2
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_m2r (round1, mm1);
-    psraw_i2r (1, mm1);		// /2
-
-    paddw_r2r (mm4, mm2);	// add highs
-    paddw_m2r (round1, mm2);
-    psraw_i2r (1, mm2);		// /2
-
-    packuswb_r2r (mm2, mm1);	// pack (w/ saturation)
-    movq_r2m (mm1, *dest);	// store result in dest
-}
-
-static inline void mmx_average_4_U8 (uint8_t * dest,
-				     uint8_t * src1, uint8_t * src2,
-				     uint8_t * src3, uint8_t * src4)
+    movq_m2r (*dest, mm1);	/* load 8 dest bytes */
+    movq_r2r (mm1, mm2);	/* copy 8 dest bytes */
+
+    movq_m2r (*src1, mm3);	/* load 8 src1 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src1 bytes */
+
+    movq_m2r (*src2, mm5);	/* load 8 src2 bytes */
+    movq_r2r (mm5, mm6);	/* copy 8 src2 bytes */
+
+    pxor_r2r (mm3, mm5);	/* xor src1 and src2 */
+    pand_m2r (mask1, mm5);	/* mask lower bits */
+    psrlq_i2r (1, mm5);		/* /2 */
+    por_r2r (mm4, mm6);		/* or src1 and src2 */
+    psubb_r2r (mm5, mm6);	/* subtract subresults */
+    movq_r2r (mm6, mm5);	/* copy subresult */
+
+    pxor_r2r (mm1, mm5);	/* xor srcavg and dest */
+    pand_m2r (mask1, mm5);	/* mask lower bits */
+    psrlq_i2r (1, mm5);		/* /2 */
+    por_r2r (mm2, mm6);		/* or srcavg and dest */
+    psubb_r2r (mm5, mm6);	/* subtract subresults */
+    movq_r2m (mm6, *dest);	/* store result in dest */
+}
+
+static inline void mmx_average_4_U8 (uint8_t * dest, const uint8_t * src1,
+				     const uint8_t * src2,
+				     const uint8_t * src3,
+				     const uint8_t * src4)
 {
     /* *dest = (*src1 + *src2 + *src3 + *src4 + 2)/ 4; */
 
-    movq_m2r (*src1, mm1);	// load 8 src1 bytes
-    movq_r2r (mm1, mm2);	// copy 8 src1 bytes
-
-    punpcklbw_r2r (mm0, mm1);	// unpack low src1 bytes
-    punpckhbw_r2r (mm0, mm2);	// unpack high src1 bytes
-
-    movq_m2r (*src2, mm3);	// load 8 src2 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src2 bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src2 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src2 bytes
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_r2r (mm4, mm2);	// add highs
+    movq_m2r (*src1, mm1);	/* load 8 src1 bytes */
+    movq_r2r (mm1, mm2);	/* copy 8 src1 bytes */
+
+    punpcklbw_r2r (mm0, mm1);	/* unpack low src1 bytes */
+    punpckhbw_r2r (mm0, mm2);	/* unpack high src1 bytes */
+
+    movq_m2r (*src2, mm3);	/* load 8 src2 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src2 bytes */
+
+    punpcklbw_r2r (mm0, mm3);	/* unpack low src2 bytes */
+    punpckhbw_r2r (mm0, mm4);	/* unpack high src2 bytes */
+
+    paddw_r2r (mm3, mm1);	/* add lows */
+    paddw_r2r (mm4, mm2);	/* add highs */
 
     /* now have partials in mm1 and mm2 */
 
-    movq_m2r (*src3, mm3);	// load 8 src3 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src3 bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src3 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src3 bytes
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_r2r (mm4, mm2);	// add highs
-
-    movq_m2r (*src4, mm5);	// load 8 src4 bytes
-    movq_r2r (mm5, mm6);	// copy 8 src4 bytes
-
-    punpcklbw_r2r (mm0, mm5);	// unpack low src4 bytes
-    punpckhbw_r2r (mm0, mm6);	// unpack high src4 bytes
-
-    paddw_r2r (mm5, mm1);	// add lows
-    paddw_r2r (mm6, mm2);	// add highs
+    movq_m2r (*src3, mm3);	/* load 8 src3 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src3 bytes */
+
+    punpcklbw_r2r (mm0, mm3);	/* unpack low src3 bytes */
+    punpckhbw_r2r (mm0, mm4);	/* unpack high src3 bytes */
+
+    paddw_r2r (mm3, mm1);	/* add lows */
+    paddw_r2r (mm4, mm2);	/* add highs */
+
+    movq_m2r (*src4, mm5);	/* load 8 src4 bytes */
+    movq_r2r (mm5, mm6);	/* copy 8 src4 bytes */
+
+    punpcklbw_r2r (mm0, mm5);	/* unpack low src4 bytes */
+    punpckhbw_r2r (mm0, mm6);	/* unpack high src4 bytes */
+
+    paddw_r2r (mm5, mm1);	/* add lows */
+    paddw_r2r (mm6, mm2);	/* add highs */
 
     /* now have subtotal in mm1 and mm2 */
 
     paddw_m2r (round4, mm1);
-    psraw_i2r (2, mm1);		// /4
+    psraw_i2r (2, mm1);		/* /4 */
     paddw_m2r (round4, mm2);
-    psraw_i2r (2, mm2);		// /4
-
-    packuswb_r2r (mm2, mm1);	// pack (w/ saturation)
-    movq_r2m (mm1, *dest);	// store result in dest
+    psraw_i2r (2, mm2);		/* /4 */
+
+    packuswb_r2r (mm2, mm1);	/* pack (w/ saturation) */
+    movq_r2m (mm1, *dest);	/* store result in dest */
 }
 
 static inline void mmx_interp_average_4_U8 (uint8_t * dest,
-					    uint8_t * src1, uint8_t * src2,
-					    uint8_t * src3, uint8_t * src4)
+					    const uint8_t * src1,
+					    const uint8_t * src2,
+					    const uint8_t * src3,
+					    const uint8_t * src4)
 {
     /* *dest = (*dest + (*src1 + *src2 + *src3 + *src4 + 2)/ 4 + 1)/ 2; */
 
-    movq_m2r (*src1, mm1);	// load 8 src1 bytes
-    movq_r2r (mm1, mm2);	// copy 8 src1 bytes
-
-    punpcklbw_r2r (mm0, mm1);	// unpack low src1 bytes
-    punpckhbw_r2r (mm0, mm2);	// unpack high src1 bytes
-
-    movq_m2r (*src2, mm3);	// load 8 src2 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src2 bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src2 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src2 bytes
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_r2r (mm4, mm2);	// add highs
+    movq_m2r (*src1, mm1);	/* load 8 src1 bytes */
+    movq_r2r (mm1, mm2);	/* copy 8 src1 bytes */
+
+    punpcklbw_r2r (mm0, mm1);	/* unpack low src1 bytes */
+    punpckhbw_r2r (mm0, mm2);	/* unpack high src1 bytes */
+
+    movq_m2r (*src2, mm3);	/* load 8 src2 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src2 bytes */
+
+    punpcklbw_r2r (mm0, mm3);	/* unpack low src2 bytes */
+    punpckhbw_r2r (mm0, mm4);	/* unpack high src2 bytes */
+
+    paddw_r2r (mm3, mm1);	/* add lows */
+    paddw_r2r (mm4, mm2);	/* add highs */
 
     /* now have partials in mm1 and mm2 */
 
-    movq_m2r (*src3, mm3);	// load 8 src3 bytes
-    movq_r2r (mm3, mm4);	// copy 8 src3 bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low src3 bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high src3 bytes
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_r2r (mm4, mm2);	// add highs
-
-    movq_m2r (*src4, mm5);	// load 8 src4 bytes
-    movq_r2r (mm5, mm6);	// copy 8 src4 bytes
-
-    punpcklbw_r2r (mm0, mm5);	// unpack low src4 bytes
-    punpckhbw_r2r (mm0, mm6);	// unpack high src4 bytes
-
-    paddw_r2r (mm5, mm1);	// add lows
-    paddw_r2r (mm6, mm2);	// add highs
+    movq_m2r (*src3, mm3);	/* load 8 src3 bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 src3 bytes */
+
+    punpcklbw_r2r (mm0, mm3);	/* unpack low src3 bytes */
+    punpckhbw_r2r (mm0, mm4);	/* unpack high src3 bytes */
+
+    paddw_r2r (mm3, mm1);	/* add lows */
+    paddw_r2r (mm4, mm2);	/* add highs */
+
+    movq_m2r (*src4, mm5);	/* load 8 src4 bytes */
+    movq_r2r (mm5, mm6);	/* copy 8 src4 bytes */
+
+    punpcklbw_r2r (mm0, mm5);	/* unpack low src4 bytes */
+    punpckhbw_r2r (mm0, mm6);	/* unpack high src4 bytes */
+
+    paddw_r2r (mm5, mm1);	/* add lows */
+    paddw_r2r (mm6, mm2);	/* add highs */
 
     paddw_m2r (round4, mm1);
-    psraw_i2r (2, mm1);		// /4
+    psraw_i2r (2, mm1);		/* /4 */
     paddw_m2r (round4, mm2);
-    psraw_i2r (2, mm2);		// /4
+    psraw_i2r (2, mm2);		/* /4 */
 
     /* now have subtotal/4 in mm1 and mm2 */
 
-    movq_m2r (*dest, mm3);	// load 8 dest bytes
-    movq_r2r (mm3, mm4);	// copy 8 dest bytes
-
-    punpcklbw_r2r (mm0, mm3);	// unpack low dest bytes
-    punpckhbw_r2r (mm0, mm4);	// unpack high dest bytes
-
-    paddw_r2r (mm3, mm1);	// add lows
-    paddw_r2r (mm4, mm2);	// add highs
-
-    paddw_m2r (round1, mm1);
-    psraw_i2r (1, mm1);		// /2
-    paddw_m2r (round1, mm2);
-    psraw_i2r (1, mm2);		// /2
-
-    /* now have end value in mm1 and mm2 */
-
-    packuswb_r2r (mm2, mm1);	// pack (w/ saturation)
-    movq_r2m (mm1,*dest);	// store result in dest
+    movq_m2r (*dest, mm3);	/* load 8 dest bytes */
+    movq_r2r (mm3, mm4);	/* copy 8 dest bytes */
+
+    packuswb_r2r (mm2, mm1);	/* pack (w/ saturation) */
+    movq_r2r (mm1,mm2);		/* copy subresult */
+
+    pxor_r2r (mm1, mm3);	/* xor srcavg and dest */
+    pand_m2r (mask1, mm3);	/* mask lower bits */
+    psrlq_i2r (1, mm3);		/* /2 */
+    por_r2r (mm2, mm4);		/* or srcavg and dest */
+    psubb_r2r (mm3, mm4);	/* subtract subresults */
+    movq_r2m (mm4, *dest);	/* store result in dest */
 }
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_avg_mmx (int width, int height,
-			       uint8_t * dest, uint8_t * ref, int stride)
+static inline void MC_avg_mmx (const int width, int height, uint8_t * dest,
+			       const uint8_t * ref, const int stride)
 {
     mmx_zero_reg ();
 
@@ -264,13 +253,13 @@ static inline void MC_avg_mmx (int width
     } while (--height);
 }
 
-static void MC_avg_o_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_avg_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_avg_o_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_avg_mmx (8, height, dest, ref, stride);
@@ -278,19 +267,19 @@ static void MC_avg_o_8_mmx (uint8_t * de
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_put_mmx (int width, int height,
-			       uint8_t * dest, uint8_t * ref, int stride)
+static inline void MC_put_mmx (const int width, int height, uint8_t * dest,
+			       const uint8_t * ref, const int stride)
 {
     mmx_zero_reg ();
 
     do {
-	movq_m2r (* ref, mm1);	// load 8 ref bytes
-	movq_r2m (mm1,* dest);	// store 8 bytes at curr
+	movq_m2r (* ref, mm1);	/* load 8 ref bytes */
+	movq_r2m (mm1,* dest);	/* store 8 bytes at curr */
 
 	if (width == 16)
 	    {
-		movq_m2r (* (ref+8), mm1);	// load 8 ref bytes
-		movq_r2m (mm1,* (dest+8));	// store 8 bytes at curr
+		movq_m2r (* (ref+8), mm1);	/* load 8 ref bytes */
+		movq_r2m (mm1,* (dest+8));	/* store 8 bytes at curr */
 	    }
 
 	dest += stride;
@@ -298,13 +287,13 @@ static inline void MC_put_mmx (int width
     } while (--height);
 }
 
-static void MC_put_o_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_put_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_put_o_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_put_mmx (8, height, dest, ref, stride);
@@ -313,8 +302,8 @@ static void MC_put_o_8_mmx (uint8_t * de
 /*-----------------------------------------------------------------------*/
 
 /* Half pixel interpolation in the x direction */
-static inline void MC_avg_x_mmx (int width, int height,
-				 uint8_t * dest, uint8_t * ref, int stride)
+static inline void MC_avg_x_mmx (const int width, int height, uint8_t * dest,
+				 const uint8_t * ref, const int stride)
 {
     mmx_zero_reg ();
 
@@ -329,13 +318,13 @@ static inline void MC_avg_x_mmx (int wid
     } while (--height);
 }
 
-static void MC_avg_x_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_avg_x_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_avg_x_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_avg_x_mmx (8, height, dest, ref, stride);
@@ -343,8 +332,8 @@ static void MC_avg_x_8_mmx (uint8_t * de
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_put_x_mmx (int width, int height,
-				 uint8_t * dest, uint8_t * ref, int stride)
+static inline void MC_put_x_mmx (const int width, int height, uint8_t * dest,
+				 const uint8_t * ref, const int stride)
 {
     mmx_zero_reg ();
 
@@ -359,13 +348,13 @@ static inline void MC_put_x_mmx (int wid
     } while (--height);
 }
 
-static void MC_put_x_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_put_x_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_put_x_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_put_x_mmx (8, height, dest, ref, stride);
@@ -373,10 +362,10 @@ static void MC_put_x_8_mmx (uint8_t * de
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_avg_xy_mmx (int width, int height,
-				  uint8_t * dest, uint8_t * ref, int stride)
-{
-    uint8_t * ref_next = ref+stride;
+static inline void MC_avg_xy_mmx (const int width, int height, uint8_t * dest,
+				  const uint8_t * ref, const int stride)
+{
+    const uint8_t * ref_next = ref + stride;
 
     mmx_zero_reg ();
 
@@ -393,13 +382,13 @@ static inline void MC_avg_xy_mmx (int wi
     } while (--height);
 }
 
-static void MC_avg_xy_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_16_mmx (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_avg_xy_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_avg_xy_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_8_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_avg_xy_mmx (8, height, dest, ref, stride);
@@ -407,10 +396,10 @@ static void MC_avg_xy_8_mmx (uint8_t * d
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_put_xy_mmx (int width, int height,
-				  uint8_t * dest, uint8_t * ref, int stride)
-{
-    uint8_t * ref_next = ref+stride;
+static inline void MC_put_xy_mmx (const int width, int height, uint8_t * dest,
+				  const uint8_t * ref, const int stride)
+{
+    const uint8_t * ref_next = ref + stride;
 
     mmx_zero_reg ();
 
@@ -426,13 +415,13 @@ static inline void MC_put_xy_mmx (int wi
     } while (--height);
 }
 
-static void MC_put_xy_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_16_mmx (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_put_xy_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_put_xy_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_8_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_put_xy_mmx (8, height, dest, ref, stride);
@@ -440,10 +429,10 @@ static void MC_put_xy_8_mmx (uint8_t * d
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_avg_y_mmx (int width, int height,
-				 uint8_t * dest, uint8_t * ref, int stride)
-{
-    uint8_t * ref_next = ref+stride;
+static inline void MC_avg_y_mmx (const int width, int height, uint8_t * dest,
+				 const uint8_t * ref, const int stride)
+{
+    const uint8_t * ref_next = ref + stride;
 
     mmx_zero_reg ();
 
@@ -459,13 +448,13 @@ static inline void MC_avg_y_mmx (int wid
     } while (--height);
 }
 
-static void MC_avg_y_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_avg_y_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_avg_y_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_avg_y_mmx (8, height, dest, ref, stride);
@@ -473,10 +462,10 @@ static void MC_avg_y_8_mmx (uint8_t * de
 
 /*-----------------------------------------------------------------------*/
 
-static inline void MC_put_y_mmx (int width, int height,
-				 uint8_t * dest, uint8_t * ref, int stride)
-{
-    uint8_t * ref_next = ref+stride;
+static inline void MC_put_y_mmx (const int width, int height, uint8_t * dest,
+				 const uint8_t * ref, const int stride)
+{
+    const uint8_t * ref_next = ref + stride;
 
     mmx_zero_reg ();
 
@@ -492,13 +481,13 @@ static inline void MC_put_y_mmx (int wid
     } while (--height);
 }
 
-static void MC_put_y_16_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_16_mmx (uint8_t * dest, const uint8_t * ref,
 			     int stride, int height)
 {
     MC_put_y_mmx (16, height, dest, ref, stride);
 }
 
-static void MC_put_y_8_mmx (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_8_mmx (uint8_t * dest, const uint8_t * ref,
 			    int stride, int height)
 {
     MC_put_y_mmx (8, height, dest, ref, stride);
@@ -535,19 +524,19 @@ do {					\
 /* CPU_MMXEXT code */
 
 
-static inline void MC_put1_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride)
-{
-    do {
-	movq_m2r (*ref, mm0);
-	movq_r2m (mm0, *dest);
-	ref += stride;
-	dest += stride;
-    } while (--height);
-}
-
-static inline void MC_put1_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride)
+static inline void MC_put1_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride)
+{
+    do {
+	movq_m2r (*ref, mm0);
+	movq_r2m (mm0, *dest);
+	ref += stride;
+	dest += stride;
+    } while (--height);
+}
+
+static inline void MC_put1_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -559,8 +548,8 @@ static inline void MC_put1_16 (int heigh
     } while (--height);
 }
 
-static inline void MC_avg1_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride, int cpu)
+static inline void MC_avg1_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride, const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -571,8 +560,8 @@ static inline void MC_avg1_8 (int height
     } while (--height);
 }
 
-static inline void MC_avg1_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride, int cpu)
+static inline void MC_avg1_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride, const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -586,8 +575,9 @@ static inline void MC_avg1_16 (int heigh
     } while (--height);
 }
 
-static inline void MC_put2_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride, int offset, int cpu)
+static inline void MC_put2_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride, const int offset,
+			      const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -598,8 +588,9 @@ static inline void MC_put2_8 (int height
     } while (--height);
 }
 
-static inline void MC_put2_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride, int offset, int cpu)
+static inline void MC_put2_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride, const int offset,
+			       const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -613,8 +604,9 @@ static inline void MC_put2_16 (int heigh
     } while (--height);
 }
 
-static inline void MC_avg2_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride, int offset, int cpu)
+static inline void MC_avg2_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride, const int offset,
+			      const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -626,8 +618,9 @@ static inline void MC_avg2_8 (int height
     } while (--height);
 }
 
-static inline void MC_avg2_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride, int offset, int cpu)
+static inline void MC_avg2_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride, const int offset,
+			       const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -645,8 +638,8 @@ static inline void MC_avg2_16 (int heigh
 
 static mmx_t mask_one = {0x0101010101010101LL};
 
-static inline void MC_put4_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride, int cpu)
+static inline void MC_put4_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride, const int cpu)
 {
     movq_m2r (*ref, mm0);
     movq_m2r (*(ref+1), mm1);
@@ -679,13 +672,13 @@ static inline void MC_put4_8 (int height
 	movq_r2m (mm0, *dest);
 	dest += stride;
 
-	movq_r2r (mm6, mm7);	// unroll !
-	movq_r2r (mm2, mm0);	// unroll !
-    } while (--height);
-}
-
-static inline void MC_put4_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride, int cpu)
+	movq_r2r (mm6, mm7);	/* unroll ! */
+	movq_r2r (mm2, mm0);	/* unroll ! */
+    } while (--height);
+}
+
+static inline void MC_put4_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride, const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -730,8 +723,8 @@ static inline void MC_put4_16 (int heigh
     } while (--height);
 }
 
-static inline void MC_avg4_8 (int height, uint8_t * dest, uint8_t * ref,
-			      int stride, int cpu)
+static inline void MC_avg4_8 (int height, uint8_t * dest, const uint8_t * ref,
+			      const int stride, const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -759,8 +752,8 @@ static inline void MC_avg4_8 (int height
     } while (--height);
 }
 
-static inline void MC_avg4_16 (int height, uint8_t * dest, uint8_t * ref,
-			       int stride, int cpu)
+static inline void MC_avg4_16 (int height, uint8_t * dest, const uint8_t * ref,
+			       const int stride, const int cpu)
 {
     do {
 	movq_m2r (*ref, mm0);
@@ -809,97 +802,97 @@ static inline void MC_avg4_16 (int heigh
     } while (--height);
 }
 
-static void MC_avg_o_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_avg1_16 (height, dest, ref, stride, CPU_MMXEXT);
 }
 
-static void MC_avg_o_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg1_8 (height, dest, ref, stride, CPU_MMXEXT);
 }
 
-static void MC_put_o_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_put1_16 (height, dest, ref, stride);
 }
 
-static void MC_put_o_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put1_8 (height, dest, ref, stride);
 }
 
-static void MC_avg_x_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_avg2_16 (height, dest, ref, stride, 1, CPU_MMXEXT);
 }
 
-static void MC_avg_x_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg2_8 (height, dest, ref, stride, 1, CPU_MMXEXT);
 }
 
-static void MC_put_x_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_put2_16 (height, dest, ref, stride, 1, CPU_MMXEXT);
 }
 
-static void MC_put_x_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put2_8 (height, dest, ref, stride, 1, CPU_MMXEXT);
 }
 
-static void MC_avg_y_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_avg2_16 (height, dest, ref, stride, stride, CPU_MMXEXT);
 }
 
-static void MC_avg_y_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg2_8 (height, dest, ref, stride, stride, CPU_MMXEXT);
 }
 
-static void MC_put_y_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_put2_16 (height, dest, ref, stride, stride, CPU_MMXEXT);
 }
 
-static void MC_put_y_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_8_mmxext (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put2_8 (height, dest, ref, stride, stride, CPU_MMXEXT);
 }
 
-static void MC_avg_xy_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				 int stride, int height)
 {
     MC_avg4_16 (height, dest, ref, stride, CPU_MMXEXT);
 }
 
-static void MC_avg_xy_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_8_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_avg4_8 (height, dest, ref, stride, CPU_MMXEXT);
 }
 
-static void MC_put_xy_16_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_16_mmxext (uint8_t * dest, const uint8_t * ref,
 				 int stride, int height)
 {
     MC_put4_16 (height, dest, ref, stride, CPU_MMXEXT);
 }
 
-static void MC_put_xy_8_mmxext (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_8_mmxext (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_put4_8 (height, dest, ref, stride, CPU_MMXEXT);
@@ -910,97 +903,97 @@ MPEG2_MC_EXTERN (mmxext)
 
 
 
-static void MC_avg_o_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg1_16 (height, dest, ref, stride, CPU_3DNOW);
 }
 
-static void MC_avg_o_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_o_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_avg1_8 (height, dest, ref, stride, CPU_3DNOW);
 }
 
-static void MC_put_o_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put1_16 (height, dest, ref, stride);
 }
 
-static void MC_put_o_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_o_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_put1_8 (height, dest, ref, stride);
 }
 
-static void MC_avg_x_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg2_16 (height, dest, ref, stride, 1, CPU_3DNOW);
 }
 
-static void MC_avg_x_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_x_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_avg2_8 (height, dest, ref, stride, 1, CPU_3DNOW);
 }
 
-static void MC_put_x_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put2_16 (height, dest, ref, stride, 1, CPU_3DNOW);
 }
 
-static void MC_put_x_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_x_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_put2_8 (height, dest, ref, stride, 1, CPU_3DNOW);
 }
 
-static void MC_avg_y_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg2_16 (height, dest, ref, stride, stride, CPU_3DNOW);
 }
 
-static void MC_avg_y_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_y_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_avg2_8 (height, dest, ref, stride, stride, CPU_3DNOW);
 }
 
-static void MC_put_y_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_16_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put2_16 (height, dest, ref, stride, stride, CPU_3DNOW);
 }
 
-static void MC_put_y_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_y_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			      int stride, int height)
 {
     MC_put2_8 (height, dest, ref, stride, stride, CPU_3DNOW);
 }
 
-static void MC_avg_xy_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_16_3dnow (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_avg4_16 (height, dest, ref, stride, CPU_3DNOW);
 }
 
-static void MC_avg_xy_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_avg_xy_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_avg4_8 (height, dest, ref, stride, CPU_3DNOW);
 }
 
-static void MC_put_xy_16_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_16_3dnow (uint8_t * dest, const uint8_t * ref,
 				int stride, int height)
 {
     MC_put4_16 (height, dest, ref, stride, CPU_3DNOW);
 }
 
-static void MC_put_xy_8_3dnow (uint8_t * dest, uint8_t * ref,
+static void MC_put_xy_8_3dnow (uint8_t * dest, const uint8_t * ref,
 			       int stride, int height)
 {
     MC_put4_8 (height, dest, ref, stride, CPU_3DNOW);
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/motion_comp_vis.c
--- a/src/libmpeg2/motion_comp_vis.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/motion_comp_vis.c	Wed Jul 04 03:09:58 2007 +0300
@@ -26,6 +26,8 @@
 
 #include <inttypes.h>
 
+#include "mpeg2.h"
+#include "attributes.h"
 #include "mpeg2_internal.h"
 #include "vis.h"
 
@@ -116,8 +118,8 @@ static const int16_t constants256_1024[]
 #define TMP30		56
 #define TMP32		58
 
-static void MC_put_o_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_put_o_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int offset;
@@ -141,8 +143,8 @@ static void MC_put_o_16_vis (uint8_t * d
 	} while (--height);
 }
 
-static void MC_put_o_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_put_o_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int offset;
@@ -164,8 +166,8 @@ static void MC_put_o_8_vis (uint8_t * de
 }
 
 
-static void MC_avg_o_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_avg_o_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int stride_8 = stride + 8;
@@ -327,8 +329,8 @@ static void MC_avg_o_16_vis (uint8_t * d
 	vis_st64_2(TMP22, dest, 8);
 }
 
-static void MC_avg_o_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_avg_o_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int offset;
@@ -423,8 +425,8 @@ static void MC_avg_o_8_vis (uint8_t * de
 	vis_st64(TMP4, dest[0]);
 }
 
-static void MC_put_x_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_put_x_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -616,8 +618,8 @@ static void MC_put_x_16_vis (uint8_t * d
 	vis_st64_2(TMP8, dest, 8);
 }
 
-static void MC_put_x_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_put_x_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -740,8 +742,8 @@ static void MC_put_x_8_vis (uint8_t * de
 	dest += stride;
 }
 
-static void MC_avg_x_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_avg_x_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -831,8 +833,8 @@ static void MC_avg_x_16_vis (uint8_t * d
 	} while (--height);
 }
 
-static void MC_avg_x_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_avg_x_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -997,8 +999,8 @@ static void MC_avg_x_8_vis (uint8_t * de
 	} while (--height);
 }
 
-static void MC_put_y_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_put_y_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int offset;
@@ -1156,8 +1158,8 @@ static void MC_put_y_16_vis (uint8_t * d
 	vis_st64_2(TMP2, dest, 8);
 }
 
-static void MC_put_y_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_put_y_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int offset;
@@ -1251,8 +1253,8 @@ static void MC_put_y_8_vis (uint8_t * de
 	vis_st64(DST_0, dest[0]);
 }
 
-static void MC_avg_y_16_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_avg_y_16_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int stride_8 = stride + 8;
@@ -1383,8 +1385,8 @@ static void MC_avg_y_16_vis (uint8_t * d
 	} while (--height);
 }
 
-static void MC_avg_y_8_vis (uint8_t * dest, uint8_t * _ref,
-			    int stride, int height)
+static void MC_avg_y_8_vis (uint8_t * dest, const uint8_t * _ref,
+			    const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	int stride_8;
@@ -1466,8 +1468,8 @@ static void MC_avg_y_8_vis (uint8_t * de
 	} while (--height);
 }
 
-static void MC_put_xy_16_vis (uint8_t * dest, uint8_t * _ref,
-			      int stride, int height)
+static void MC_put_xy_16_vis (uint8_t * dest, const uint8_t * _ref,
+			      const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -1631,8 +1633,8 @@ static void MC_put_xy_16_vis (uint8_t * 
 	} while (--height);
 }
 
-static void MC_put_xy_8_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_put_xy_8_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -1736,8 +1738,8 @@ static void MC_put_xy_8_vis (uint8_t * d
 	} while (--height);
 }
 
-static void MC_avg_xy_16_vis (uint8_t * dest, uint8_t * _ref,
-			      int stride, int height)
+static void MC_avg_xy_16_vis (uint8_t * dest, const uint8_t * _ref,
+			      const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
@@ -1933,8 +1935,8 @@ static void MC_avg_xy_16_vis (uint8_t * 
 	} while (--height);
 }
 
-static void MC_avg_xy_8_vis (uint8_t * dest, uint8_t * _ref,
-			     int stride, int height)
+static void MC_avg_xy_8_vis (uint8_t * dest, const uint8_t * _ref,
+			     const int stride, int height)
 {
 	uint8_t *ref = (uint8_t *) _ref;
 	unsigned long off = (unsigned long) ref & 0x7;
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/mpeg2.h
--- a/src/libmpeg2/mpeg2.h	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/mpeg2.h	Wed Jul 04 03:09:58 2007 +0300
@@ -25,6 +25,10 @@
 #define MPEG2_H
 
 #include "libmpeg2_accel.h"
+
+#ifndef HAVE_SPU_API_H
+typedef struct spu_decoder_s * spu_decoder_t;
+#endif
 
 typedef struct mpeg2dec_s {
     xine_video_port_t * output;
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/mpeg2_internal.h
--- a/src/libmpeg2/mpeg2_internal.h	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/mpeg2_internal.h	Wed Jul 04 03:09:58 2007 +0300
@@ -1,6 +1,6 @@
 /*
  * mpeg2_internal.h
- * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
+ * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  *
  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
@@ -52,13 +52,15 @@
 #define BOTTOM_FIELD  VO_BOTTOM_FIELD
 #define FRAME_PICTURE VO_BOTH_FIELDS
 
-/* picture coding type (mpeg2 header) */
+/* picture coding type */
 #define I_TYPE 1
 #define P_TYPE 2
 #define B_TYPE 3
 #define D_TYPE 4
-               
-typedef struct motion_s {
+
+typedef void mpeg2_mc_fct (uint8_t *, const uint8_t *, int, int);
+
+typedef struct {
     uint8_t * ref[2][3];
     uint8_t ** ref2[2];
     int pmv[2][2];
@@ -70,8 +72,7 @@ typedef struct picture_s {
     /* next inside a slice, and is never used outside of mpeg2_slice() */
 
     /* DCT coefficients - should be kept aligned ! */
-    int16_t DCTblock[64];
-
+    int16_t DCTblock[64] ATTR_ALIGN(64);
     /* XvMC DCT block and macroblock data for XvMC acceleration */
     xine_macroblocks_t *mc;
     int XvMC_mb_type;
@@ -207,6 +208,9 @@ typedef struct cpu_state_s {
     int dummy;
 } cpu_state_t;
 
+/* cpu_accel.c */
+/*uint32_t mpeg2_detect_accel (void);*/
+
 /* cpu_state.c */
 extern void (* mpeg2_cpu_state_save) (cpu_state_t * state);
 extern void (* mpeg2_cpu_state_restore) (cpu_state_t * state);
@@ -223,25 +227,19 @@ int mpeg2_header_group_of_pictures (pict
 
 /* idct.c */
 extern void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
-extern void (* mpeg2_idct_add) (int16_t * block, uint8_t * dest, int stride);
+extern void (* mpeg2_idct_add) (const int last, int16_t * block, uint8_t * dest, int stride);
 extern void (* mpeg2_idct) (int16_t * block);
 extern void (* mpeg2_zero_block) (int16_t * block);
 void mpeg2_idct_init (uint32_t mm_accel);
 
-/* idct_mlib.c */
-void mpeg2_idct_add_mlib (int16_t * block, uint8_t * dest, int stride);
-void mpeg2_idct_copy_mlib_non_ieee (int16_t * block, uint8_t * dest,
-				    int stride);
-void mpeg2_idct_add_mlib_non_ieee (int16_t * block, uint8_t * dest,
-				   int stride);
-void mpeg2_idct_mlib (int16_t * block);
-
 /* idct_mmx.c */
 void mpeg2_idct_copy_mmxext (int16_t * block, uint8_t * dest, int stride);
-void mpeg2_idct_add_mmxext (int16_t * block, uint8_t * dest, int stride);
+void mpeg2_idct_add_mmxext (int last, int16_t * block,
+			    uint8_t * dest, int stride);
 void mpeg2_idct_mmxext (int16_t * block);
 void mpeg2_idct_copy_mmx (int16_t * block, uint8_t * dest, int stride);
-void mpeg2_idct_add_mmx (int16_t * block, uint8_t * dest, int stride);
+void mpeg2_idct_add_mmx (int last, int16_t * block,
+			 uint8_t * dest, int stride);
 void mpeg2_idct_mmx (int16_t * block);
 void mpeg2_zero_block_mmx (int16_t * block);
 void mpeg2_idct_mmx_init (void);
@@ -250,22 +248,30 @@ void mpeg2_idct_mmx_init (void);
 # ifdef ENABLE_ALTIVEC
 void mpeg2_idct_copy_altivec (vector signed short * block, unsigned char * dest,
 			      int stride);
-void mpeg2_idct_add_altivec (vector signed short * block, unsigned char * dest,
+void mpeg2_idct_add_altivec (const int last, vector signed short * block, unsigned char * dest,
 			     int stride);
 # else /* ! ENABLE_ALTIVEC */
-void mpeg2_idct_copy_altivec (signed short * block, unsigned char * dest,
-			      int stride);
-void mpeg2_idct_add_altivec (signed short * block, unsigned char * dest,
-			     int stride);
+void mpeg2_idct_copy_altivec (int16_t * block, uint8_t * dest, int stride);
+void mpeg2_idct_add_altivec (int last, int16_t * block,
+			     uint8_t * dest, int stride);
 # endif /* ENABLE_ALTIVEC */
 void mpeg2_idct_altivec_init (void);
 
+/* idct_alpha.c */
+void mpeg2_idct_copy_mvi (int16_t * block, uint8_t * dest, int stride);
+void mpeg2_idct_add_mvi (int last, int16_t * block,
+			 uint8_t * dest, int stride);
+void mpeg2_idct_copy_alpha (int16_t * block, uint8_t * dest, int stride);
+void mpeg2_idct_add_alpha (int last, int16_t * block,
+			   uint8_t * dest, int stride);
+void mpeg2_idct_alpha_init (void);
+
 /* motion_comp.c */
-void mpeg2_mc_init (uint32_t mm_accel);
-
-typedef struct mpeg2_mc_s {
-    void (* put [8]) (uint8_t * dst, uint8_t *, int32_t, int32_t);
-    void (* avg [8]) (uint8_t * dst, uint8_t *, int32_t, int32_t);
+void mpeg2_mc_init (uint32_t accel);
+
+typedef struct {
+    mpeg2_mc_fct * put [8];
+    mpeg2_mc_fct * avg [8];
 } mpeg2_mc_t;
 
 #define MPEG2_MC_EXTERN(x) mpeg2_mc_t mpeg2_mc_##x = {			  \
@@ -281,7 +287,7 @@ extern mpeg2_mc_t mpeg2_mc_mmxext;
 extern mpeg2_mc_t mpeg2_mc_mmxext;
 extern mpeg2_mc_t mpeg2_mc_3dnow;
 extern mpeg2_mc_t mpeg2_mc_altivec;
-extern mpeg2_mc_t mpeg2_mc_mlib;
+extern mpeg2_mc_t mpeg2_mc_alpha;
 extern mpeg2_mc_t mpeg2_mc_vis;
 
 /* slice.c */
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/slice.c
--- a/src/libmpeg2/slice.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/slice.c	Wed Jul 04 03:09:58 2007 +0300
@@ -575,7 +575,7 @@ static void get_intra_block_B15 (picture
     picture->bitstream_ptr = bit_ptr;
 }
 
-static void get_non_intra_block (picture_t * picture)
+static int get_non_intra_block (picture_t * picture)
 {
     int i;
     int j;
@@ -696,6 +696,7 @@ static void get_non_intra_block (picture
     picture->bitstream_buf = bit_buf;
     picture->bitstream_bits = bits;
     picture->bitstream_ptr = bit_ptr;
+    return i;
 }
 
 static void get_mpeg1_intra_block (picture_t * picture)
@@ -817,7 +818,7 @@ static void get_mpeg1_intra_block (pictu
     picture->bitstream_ptr = bit_ptr;
 }
 
-static void get_mpeg1_non_intra_block (picture_t * picture)
+static int get_mpeg1_non_intra_block (picture_t * picture)
 {
     int i;
     int j;
@@ -944,6 +945,7 @@ static void get_mpeg1_non_intra_block (p
     picture->bitstream_buf = bit_buf;
     picture->bitstream_bits = bits;
     picture->bitstream_ptr = bit_ptr;
+    return i;
 }
 
 static inline void slice_intra_DCT (picture_t * picture, int cc,
@@ -977,11 +979,12 @@ static inline void slice_non_intra_DCT (
 static inline void slice_non_intra_DCT (picture_t * picture, uint8_t * dest,
 					int stride)
 {
+    int last;
     if (picture->mpeg1)
-	get_mpeg1_non_intra_block (picture);
+	last = get_mpeg1_non_intra_block (picture);
     else
-	get_non_intra_block (picture);
-    mpeg2_idct_add (picture->DCTblock, dest, stride);
+	last = get_non_intra_block (picture);
+    mpeg2_idct_add (last, picture->DCTblock, dest, stride);
 }
 
 #define MOTION(table,ref,motion_x,motion_y,size,y)			      \
@@ -1045,8 +1048,9 @@ static inline void slice_non_intra_DCT (
 		         (motion_y op) + src_field) * picture->pitches[2])),  \
 		      2 * picture->pitches[2], 4)
 
-static void motion_mp1 (picture_t * picture, motion_t * motion,
-			void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_mp1 (picture_t * picture, 
+			motion_t * const motion,
+			mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1076,8 +1080,9 @@ static void motion_mp1 (picture_t * pict
 #undef bit_ptr
 }
 
-static void motion_fr_frame (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fr_frame (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1103,8 +1108,9 @@ static void motion_fr_frame (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fr_field (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fr_field (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1150,8 +1156,9 @@ static void motion_fr_field (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fr_dmv (picture_t * picture, motion_t * motion,
-			   void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fr_dmv (picture_t * picture, 
+			   motion_t * const motion,
+			   mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1229,8 +1236,9 @@ static void motion_fr_dmv (picture_t * p
 #undef bit_ptr
 }
 
-static void motion_reuse (picture_t * picture, motion_t * motion,
-			  void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_reuse (picture_t * picture, 
+			  motion_t * const motion,
+			  mpeg2_mc_fct * const * const table)
 {
     int motion_x, motion_y;
     unsigned int pos_x, pos_y, xy_half;
@@ -1241,8 +1249,9 @@ static void motion_reuse (picture_t * pi
     MOTION (table, motion->ref[0], motion_x, motion_y, 16, 0);
 }
 
-static void motion_zero (picture_t * picture, motion_t * motion,
-			 void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_zero (picture_t * picture, 
+			 motion_t * const motion,
+			 mpeg2_mc_fct * const * const table)
 {
     table[0] (picture->dest[0] + picture->offset,
 	      (motion->ref[0][0] + picture->offset +
@@ -1285,8 +1294,9 @@ static void motion_fr_conceal (picture_t
 #undef bit_ptr
 }
 
-static void motion_fi_field (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_field (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1316,8 +1326,9 @@ static void motion_fi_field (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fi_16x8 (picture_t * picture, motion_t * motion,
-			    void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_16x8 (picture_t * picture, 
+			    motion_t * const motion,
+			    mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1364,8 +1375,9 @@ static void motion_fi_16x8 (picture_t * 
 #undef bit_ptr
 }
 
-static void motion_fi_dmv (picture_t * picture, motion_t * motion,
-			   void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_dmv (picture_t * picture, 
+			   motion_t * const motion,
+			   mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
diff -r 268ad8e747c8 -r 79a96eefa515 src/libmpeg2/slice_xvmc.c
--- a/src/libmpeg2/slice_xvmc.c	Mon Jun 25 13:05:45 2007 +0200
+++ b/src/libmpeg2/slice_xvmc.c	Wed Jul 04 03:09:58 2007 +0300
@@ -1122,8 +1122,9 @@ static inline void slice_xvmc_non_intra_
     picture->mc->blockptr += 64;
 }
 
-static void motion_mp1 (picture_t * picture, motion_t * motion,
-			void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_mp1 (picture_t * picture, 
+			motion_t * const motion,
+			mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1151,8 +1152,9 @@ static void motion_mp1 (picture_t * pict
 #undef bit_ptr
 }
 
-static void motion_fr_frame (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fr_frame (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1176,8 +1178,9 @@ static void motion_fr_frame (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fr_field (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int),
+static void motion_fr_field (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table,
 			     int dir)
 {
 #define bit_buf (picture->bitstream_buf)
@@ -1224,8 +1227,9 @@ static void motion_fr_field (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fr_dmv (picture_t * picture, motion_t * motion,
-			   void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fr_dmv (picture_t * picture, 
+			   motion_t * const motion,
+			   mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1252,8 +1256,9 @@ static void motion_fr_dmv (picture_t * p
 #undef bit_ptr
 }
 
-static void motion_reuse (picture_t * picture, motion_t * motion,
-			  void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_reuse (picture_t * picture, 
+			  motion_t * const motion,
+			  mpeg2_mc_fct * const * const table)
 {
     int motion_x, motion_y;
 
@@ -1288,8 +1293,9 @@ static void motion_fr_conceal (picture_t
 #undef bit_ptr
 }
 
-static void motion_fi_field (picture_t * picture, motion_t * motion,
-			     void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_field (picture_t * picture, 
+			     motion_t * const motion,
+			     mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1321,8 +1327,9 @@ static void motion_fi_field (picture_t *
 #undef bit_ptr
 }
 
-static void motion_fi_16x8 (picture_t * picture, motion_t * motion,
-			    void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_16x8 (picture_t * picture, 
+			    motion_t * const motion,
+			    mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
@@ -1374,8 +1381,9 @@ static void motion_fi_16x8 (picture_t * 
 #undef bit_ptr
 }
 
-static void motion_fi_dmv (picture_t * picture, motion_t * motion,
-			   void (** table) (uint8_t *, uint8_t *, int, int))
+static void motion_fi_dmv (picture_t * picture, 
+			   motion_t * const motion,
+			   mpeg2_mc_fct * const * const table)
 {
 #define bit_buf (picture->bitstream_buf)
 #define bits (picture->bitstream_bits)
