MythTV  0.26-pre
idct_altivec.c
Go to the documentation of this file.
00001 /*
00002  * idct_altivec.c
00003  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
00004  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
00005  *
00006  * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
00007  * See http://libmpeg2.sourceforge.net/ for updates.
00008  *
00009  * mpeg2dec is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * mpeg2dec is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include "mpeg2config.h"
00025 
00026 #if ARCH_PPC
00027 
00028 #if HAVE_ALTIVEC_H
00029 #include <altivec.h>
00030 #endif
00031 #include <inttypes.h>
00032 
00033 #include "mpeg2.h"
00034 #include "attributes.h"
00035 #include "mpeg2_internal.h"
00036 
00037 typedef vector signed char vector_s8_t;
00038 typedef vector unsigned char vector_u8_t;
00039 typedef vector signed short vector_s16_t;
00040 typedef vector unsigned short vector_u16_t;
00041 typedef vector signed int vector_s32_t;
00042 typedef vector unsigned int vector_u32_t;
00043 
00044 #if HAVE_ALTIVEC_H && (__GNUC__ * 100 + __GNUC_MINOR__ < 303)
00045 /* work around gcc <3.3 vec_mergel bug */
00046 static inline vector_s16_t my_vec_mergel (vector_s16_t const A,
00047                                           vector_s16_t const B)
00048 {
00049     static const vector_u8_t mergel = {
00050         0x08, 0x09, 0x18, 0x19, 0x0a, 0x0b, 0x1a, 0x1b,
00051         0x0c, 0x0d, 0x1c, 0x1d, 0x0e, 0x0f, 0x1e, 0x1f
00052     };
00053     return vec_perm (A, B, mergel);
00054 }
00055 #undef vec_mergel
00056 #define vec_mergel my_vec_mergel
00057 #endif
00058 
00059 #if HAVE_ALTIVEC_H      /* gnu */
00060 #define VEC_S16(a,b,c,d,e,f,g,h) {a, b, c, d, e, f, g, h}
00061 #else                   /* apple */
00062 #define VEC_S16(a,b,c,d,e,f,g,h) (vector_s16_t) (a, b, c, d, e, f, g, h)
00063 #endif
00064 
00065 static const vector_s16_t constants ATTR_ALIGN(16) =
00066     VEC_S16 (23170, 13573, 6518, 21895, -23170, -21895, 32, 31);
00067 static const vector_s16_t constants_1 ATTR_ALIGN(16) =
00068     VEC_S16 (16384, 22725, 21407, 19266, 16384, 19266, 21407, 22725);
00069 static const vector_s16_t constants_2 ATTR_ALIGN(16) =
00070     VEC_S16 (16069, 22289, 20995, 18895, 16069, 18895, 20995, 22289);
00071 static const vector_s16_t constants_3 ATTR_ALIGN(16) =
00072     VEC_S16 (21407, 29692, 27969, 25172, 21407, 25172, 27969, 29692);
00073 static const vector_s16_t constants_4 ATTR_ALIGN(16) =
00074     VEC_S16 (13623, 18895, 17799, 16019, 13623, 16019, 17799, 18895);
00075 
00076 #define IDCT                                                            \
00077     vector_s16_t vx0, vx1, vx2, vx3, vx4, vx5, vx6, vx7;                \
00078     vector_s16_t vy0, vy1, vy2, vy3, vy4, vy5, vy6, vy7;                \
00079     vector_s16_t a0, a1, a2, ma2, c4, mc4, zero, bias;                  \
00080     vector_s16_t t0, t1, t2, t3, t4, t5, t6, t7, t8;                    \
00081     vector_u16_t shift;                                                 \
00082                                                                         \
00083     c4 = vec_splat (constants, 0);                                      \
00084     a0 = vec_splat (constants, 1);                                      \
00085     a1 = vec_splat (constants, 2);                                      \
00086     a2 = vec_splat (constants, 3);                                      \
00087     mc4 = vec_splat (constants, 4);                                     \
00088     ma2 = vec_splat (constants, 5);                                     \
00089     bias = (vector_s16_t)vec_splat ((vector_s32_t)constants, 3);        \
00090                                                                         \
00091     zero = vec_splat_s16 (0);                                           \
00092                                                                         \
00093     vx0 = vec_adds (block[0], block[4]);                                \
00094     vx4 = vec_subs (block[0], block[4]);                                \
00095     t5 = vec_mradds (vx0, constants_1, zero);                           \
00096     t0 = vec_mradds (vx4, constants_1, zero);                           \
00097                                                                         \
00098     vx1 = vec_mradds (a1, block[7], block[1]);                          \
00099     vx7 = vec_mradds (a1, block[1], vec_subs (zero, block[7]));         \
00100     t1 = vec_mradds (vx1, constants_2, zero);                           \
00101     t8 = vec_mradds (vx7, constants_2, zero);                           \
00102                                                                         \
00103     vx2 = vec_mradds (a0, block[6], block[2]);                          \
00104     vx6 = vec_mradds (a0, block[2], vec_subs (zero, block[6]));         \
00105     t2 = vec_mradds (vx2, constants_3, zero);                           \
00106     t4 = vec_mradds (vx6, constants_3, zero);                           \
00107                                                                         \
00108     vx3 = vec_mradds (block[3], constants_4, zero);                     \
00109     vx5 = vec_mradds (block[5], constants_4, zero);                     \
00110     t7 = vec_mradds (a2, vx5, vx3);                                     \
00111     t3 = vec_mradds (ma2, vx3, vx5);                                    \
00112                                                                         \
00113     t6 = vec_adds (t8, t3);                                             \
00114     t3 = vec_subs (t8, t3);                                             \
00115     t8 = vec_subs (t1, t7);                                             \
00116     t1 = vec_adds (t1, t7);                                             \
00117     t6 = vec_mradds (a0, t6, t6);       /* a0+1 == 2*c4 */              \
00118     t1 = vec_mradds (a0, t1, t1);       /* a0+1 == 2*c4 */              \
00119                                                                         \
00120     t7 = vec_adds (t5, t2);                                             \
00121     t2 = vec_subs (t5, t2);                                             \
00122     t5 = vec_adds (t0, t4);                                             \
00123     t0 = vec_subs (t0, t4);                                             \
00124     t4 = vec_subs (t8, t3);                                             \
00125     t3 = vec_adds (t8, t3);                                             \
00126                                                                         \
00127     vy0 = vec_adds (t7, t1);                                            \
00128     vy7 = vec_subs (t7, t1);                                            \
00129     vy1 = vec_adds (t5, t3);                                            \
00130     vy6 = vec_subs (t5, t3);                                            \
00131     vy2 = vec_adds (t0, t4);                                            \
00132     vy5 = vec_subs (t0, t4);                                            \
00133     vy3 = vec_adds (t2, t6);                                            \
00134     vy4 = vec_subs (t2, t6);                                            \
00135                                                                         \
00136     vx0 = vec_mergeh (vy0, vy4);                                        \
00137     vx1 = vec_mergel (vy0, vy4);                                        \
00138     vx2 = vec_mergeh (vy1, vy5);                                        \
00139     vx3 = vec_mergel (vy1, vy5);                                        \
00140     vx4 = vec_mergeh (vy2, vy6);                                        \
00141     vx5 = vec_mergel (vy2, vy6);                                        \
00142     vx6 = vec_mergeh (vy3, vy7);                                        \
00143     vx7 = vec_mergel (vy3, vy7);                                        \
00144                                                                         \
00145     vy0 = vec_mergeh (vx0, vx4);                                        \
00146     vy1 = vec_mergel (vx0, vx4);                                        \
00147     vy2 = vec_mergeh (vx1, vx5);                                        \
00148     vy3 = vec_mergel (vx1, vx5);                                        \
00149     vy4 = vec_mergeh (vx2, vx6);                                        \
00150     vy5 = vec_mergel (vx2, vx6);                                        \
00151     vy6 = vec_mergeh (vx3, vx7);                                        \
00152     vy7 = vec_mergel (vx3, vx7);                                        \
00153                                                                         \
00154     vx0 = vec_mergeh (vy0, vy4);                                        \
00155     vx1 = vec_mergel (vy0, vy4);                                        \
00156     vx2 = vec_mergeh (vy1, vy5);                                        \
00157     vx3 = vec_mergel (vy1, vy5);                                        \
00158     vx4 = vec_mergeh (vy2, vy6);                                        \
00159     vx5 = vec_mergel (vy2, vy6);                                        \
00160     vx6 = vec_mergeh (vy3, vy7);                                        \
00161     vx7 = vec_mergel (vy3, vy7);                                        \
00162                                                                         \
00163     vx0 = vec_adds (vx0, bias);                                         \
00164     t5 = vec_adds (vx0, vx4);                                           \
00165     t0 = vec_subs (vx0, vx4);                                           \
00166                                                                         \
00167     t1 = vec_mradds (a1, vx7, vx1);                                     \
00168     t8 = vec_mradds (a1, vx1, vec_subs (zero, vx7));                    \
00169                                                                         \
00170     t2 = vec_mradds (a0, vx6, vx2);                                     \
00171     t4 = vec_mradds (a0, vx2, vec_subs (zero, vx6));                    \
00172                                                                         \
00173     t7 = vec_mradds (a2, vx5, vx3);                                     \
00174     t3 = vec_mradds (ma2, vx3, vx5);                                    \
00175                                                                         \
00176     t6 = vec_adds (t8, t3);                                             \
00177     t3 = vec_subs (t8, t3);                                             \
00178     t8 = vec_subs (t1, t7);                                             \
00179     t1 = vec_adds (t1, t7);                                             \
00180                                                                         \
00181     t7 = vec_adds (t5, t2);                                             \
00182     t2 = vec_subs (t5, t2);                                             \
00183     t5 = vec_adds (t0, t4);                                             \
00184     t0 = vec_subs (t0, t4);                                             \
00185     t4 = vec_subs (t8, t3);                                             \
00186     t3 = vec_adds (t8, t3);                                             \
00187                                                                         \
00188     vy0 = vec_adds (t7, t1);                                            \
00189     vy7 = vec_subs (t7, t1);                                            \
00190     vy1 = vec_mradds (c4, t3, t5);                                      \
00191     vy6 = vec_mradds (mc4, t3, t5);                                     \
00192     vy2 = vec_mradds (c4, t4, t0);                                      \
00193     vy5 = vec_mradds (mc4, t4, t0);                                     \
00194     vy3 = vec_adds (t2, t6);                                            \
00195     vy4 = vec_subs (t2, t6);                                            \
00196                                                                         \
00197     shift = vec_splat_u16 (6);                                          \
00198     vx0 = vec_sra (vy0, shift);                                         \
00199     vx1 = vec_sra (vy1, shift);                                         \
00200     vx2 = vec_sra (vy2, shift);                                         \
00201     vx3 = vec_sra (vy3, shift);                                         \
00202     vx4 = vec_sra (vy4, shift);                                         \
00203     vx5 = vec_sra (vy5, shift);                                         \
00204     vx6 = vec_sra (vy6, shift);                                         \
00205     vx7 = vec_sra (vy7, shift);
00206 
00207 void mpeg2_idct_copy_altivec (int16_t * const _block, uint8_t * dest,
00208                               const int stride)
00209 {
00210     vector_s16_t * const block = (vector_s16_t *)_block;
00211     vector_u8_t tmp;
00212 
00213     IDCT
00214 
00215 #define COPY(dest,src)                                          \
00216     tmp = vec_packsu (src, src);                                \
00217     vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);       \
00218     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
00219 
00220     COPY (dest, vx0)    dest += stride;
00221     COPY (dest, vx1)    dest += stride;
00222     COPY (dest, vx2)    dest += stride;
00223     COPY (dest, vx3)    dest += stride;
00224     COPY (dest, vx4)    dest += stride;
00225     COPY (dest, vx5)    dest += stride;
00226     COPY (dest, vx6)    dest += stride;
00227     COPY (dest, vx7)
00228 
00229     block[0] = block[1] = block[2] = block[3] = zero;
00230     block[4] = block[5] = block[6] = block[7] = zero;
00231 }
00232 
00233 void mpeg2_idct_add_altivec (const int last, int16_t * const _block,
00234                              uint8_t * dest, const int stride)
00235 {
00236     vector_s16_t * const block = (vector_s16_t *)_block;
00237     vector_u8_t tmp;
00238     vector_s16_t tmp2, tmp3;
00239     vector_u8_t perm0;
00240     vector_u8_t perm1;
00241     vector_u8_t p0, p1, p;
00242 
00243     IDCT
00244 
00245     p0 = vec_lvsl (0, dest);
00246     p1 = vec_lvsl (stride, dest);
00247     p = vec_splat_u8 (-1);
00248     perm0 = vec_mergeh (p, p0);
00249     perm1 = vec_mergeh (p, p1);
00250 
00251 #define ADD(dest,src,perm)                                              \
00252     /* *(uint64_t *)&tmp = *(uint64_t *)dest; */                        \
00253     tmp = vec_ld (0, dest);                                             \
00254     tmp2 = (vector_s16_t)vec_perm (tmp, (vector_u8_t)zero, perm);       \
00255     tmp3 = vec_adds (tmp2, src);                                        \
00256     tmp = vec_packsu (tmp3, tmp3);                                      \
00257     vec_ste ((vector_u32_t)tmp, 0, (unsigned int *)dest);               \
00258     vec_ste ((vector_u32_t)tmp, 4, (unsigned int *)dest);
00259 
00260     ADD (dest, vx0, perm0)      dest += stride;
00261     ADD (dest, vx1, perm1)      dest += stride;
00262     ADD (dest, vx2, perm0)      dest += stride;
00263     ADD (dest, vx3, perm1)      dest += stride;
00264     ADD (dest, vx4, perm0)      dest += stride;
00265     ADD (dest, vx5, perm1)      dest += stride;
00266     ADD (dest, vx6, perm0)      dest += stride;
00267     ADD (dest, vx7, perm1)
00268 
00269     block[0] = block[1] = block[2] = block[3] = zero;
00270     block[4] = block[5] = block[6] = block[7] = zero;
00271 }
00272 
00273 void mpeg2_idct_altivec_init (void)
00274 {
00275     extern uint8_t mpeg2_scan_norm[64];
00276     extern uint8_t mpeg2_scan_alt[64];
00277     int i, j;
00278 
00279     /* the altivec idct uses a transposed input, so we patch scan tables */
00280     for (i = 0; i < 64; i++) {
00281         j = mpeg2_scan_norm[i];
00282         mpeg2_scan_norm[i] = (j >> 3) | ((j & 7) << 3);
00283         j = mpeg2_scan_alt[i];
00284         mpeg2_scan_alt[i] = (j >> 3) | ((j & 7) << 3);
00285     }
00286 }
00287 
00288 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends