09a739bdb8e8fffda5c22c40c3752ba4c15c9dbf
[ffmpeg.git] / libavcodec / ppc / h264_altivec.c
1 /*
2  * Copyright (c) 2004 Romain Dolbeau <romain@dolbeau.org>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavcodec/dsputil.h"
22
23 #include "gcc_fixes.h"
24
25 #include "dsputil_ppc.h"
26 #include "dsputil_altivec.h"
27 #include "util_altivec.h"
28 #include "types_altivec.h"
29
30 #define PUT_OP_U8_ALTIVEC(d, s, dst) d = s
31 #define AVG_OP_U8_ALTIVEC(d, s, dst) d = vec_avg(dst, s)
32
33 #define OP_U8_ALTIVEC                          PUT_OP_U8_ALTIVEC
34 #define PREFIX_h264_chroma_mc8_altivec         put_h264_chroma_mc8_altivec
35 #define PREFIX_h264_chroma_mc8_num             altivec_put_h264_chroma_mc8_num
36 #define PREFIX_h264_qpel16_h_lowpass_altivec   put_h264_qpel16_h_lowpass_altivec
37 #define PREFIX_h264_qpel16_h_lowpass_num       altivec_put_h264_qpel16_h_lowpass_num
38 #define PREFIX_h264_qpel16_v_lowpass_altivec   put_h264_qpel16_v_lowpass_altivec
39 #define PREFIX_h264_qpel16_v_lowpass_num       altivec_put_h264_qpel16_v_lowpass_num
40 #define PREFIX_h264_qpel16_hv_lowpass_altivec  put_h264_qpel16_hv_lowpass_altivec
41 #define PREFIX_h264_qpel16_hv_lowpass_num      altivec_put_h264_qpel16_hv_lowpass_num
42 #include "h264_template_altivec.c"
43 #undef OP_U8_ALTIVEC
44 #undef PREFIX_h264_chroma_mc8_altivec
45 #undef PREFIX_h264_chroma_mc8_num
46 #undef PREFIX_h264_qpel16_h_lowpass_altivec
47 #undef PREFIX_h264_qpel16_h_lowpass_num
48 #undef PREFIX_h264_qpel16_v_lowpass_altivec
49 #undef PREFIX_h264_qpel16_v_lowpass_num
50 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
51 #undef PREFIX_h264_qpel16_hv_lowpass_num
52
53 #define OP_U8_ALTIVEC                          AVG_OP_U8_ALTIVEC
54 #define PREFIX_h264_chroma_mc8_altivec         avg_h264_chroma_mc8_altivec
55 #define PREFIX_h264_chroma_mc8_num             altivec_avg_h264_chroma_mc8_num
56 #define PREFIX_h264_qpel16_h_lowpass_altivec   avg_h264_qpel16_h_lowpass_altivec
57 #define PREFIX_h264_qpel16_h_lowpass_num       altivec_avg_h264_qpel16_h_lowpass_num
58 #define PREFIX_h264_qpel16_v_lowpass_altivec   avg_h264_qpel16_v_lowpass_altivec
59 #define PREFIX_h264_qpel16_v_lowpass_num       altivec_avg_h264_qpel16_v_lowpass_num
60 #define PREFIX_h264_qpel16_hv_lowpass_altivec  avg_h264_qpel16_hv_lowpass_altivec
61 #define PREFIX_h264_qpel16_hv_lowpass_num      altivec_avg_h264_qpel16_hv_lowpass_num
62 #include "h264_template_altivec.c"
63 #undef OP_U8_ALTIVEC
64 #undef PREFIX_h264_chroma_mc8_altivec
65 #undef PREFIX_h264_chroma_mc8_num
66 #undef PREFIX_h264_qpel16_h_lowpass_altivec
67 #undef PREFIX_h264_qpel16_h_lowpass_num
68 #undef PREFIX_h264_qpel16_v_lowpass_altivec
69 #undef PREFIX_h264_qpel16_v_lowpass_num
70 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
71 #undef PREFIX_h264_qpel16_hv_lowpass_num
72
73 #define H264_MC(OPNAME, SIZE, CODETYPE) \
74 static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, int stride){\
75     OPNAME ## pixels ## SIZE ## _ ## CODETYPE(dst, src, stride, SIZE);\
76 }\
77 \
78 static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){ \
79     DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
80     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
81     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
82 }\
83 \
84 static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
85     OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(dst, src, stride, stride);\
86 }\
87 \
88 static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
89     DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
90     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
91     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+1, half, stride, stride, SIZE);\
92 }\
93 \
94 static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
95     DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
96     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
97     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
98 }\
99 \
100 static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
101     OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(dst, src, stride, stride);\
102 }\
103 \
104 static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
105     DECLARE_ALIGNED_16(uint8_t, half[SIZE*SIZE]);\
106     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
107     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+stride, half, stride, stride, SIZE);\
108 }\
109 \
110 static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
111     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
112     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
113     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
114     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
115     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
116 }\
117 \
118 static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
119     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
120     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
121     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
122     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
123     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
124 }\
125 \
126 static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
127     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
128     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
129     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
130     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
131     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
132 }\
133 \
134 static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
135     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
136     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
137     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
138     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
139     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
140 }\
141 \
142 static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
143     DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
144     OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(dst, tmp, src, stride, SIZE, stride);\
145 }\
146 \
147 static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
148     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
149     DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
150     DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
151     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
152     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
153     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
154 }\
155 \
156 static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
157     DECLARE_ALIGNED_16(uint8_t, halfH[SIZE*SIZE]);\
158     DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
159     DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
160     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
161     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
162     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
163 }\
164 \
165 static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
166     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
167     DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
168     DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
169     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
170     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
171     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
172 }\
173 \
174 static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
175     DECLARE_ALIGNED_16(uint8_t, halfV[SIZE*SIZE]);\
176     DECLARE_ALIGNED_16(uint8_t, halfHV[SIZE*SIZE]);\
177     DECLARE_ALIGNED_16(int16_t, tmp[SIZE*(SIZE+8)]);\
178     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
179     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
180     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
181 }\
182
183 /* this code assume that stride % 16 == 0 */
184 void put_no_rnd_h264_chroma_mc8_altivec(uint8_t * dst, uint8_t * src, int stride, int h, int x, int y) {
185    DECLARE_ALIGNED_16(signed int, ABCD[4]) =
186                         {((8 - x) * (8 - y)),
187                              ((x) * (8 - y)),
188                          ((8 - x) * (y)),
189                              ((x) * (y))};
190     register int i;
191     vec_u8_t fperm;
192     const vec_s32_t vABCD = vec_ld(0, ABCD);
193     const vec_s16_t vA = vec_splat((vec_s16_t)vABCD, 1);
194     const vec_s16_t vB = vec_splat((vec_s16_t)vABCD, 3);
195     const vec_s16_t vC = vec_splat((vec_s16_t)vABCD, 5);
196     const vec_s16_t vD = vec_splat((vec_s16_t)vABCD, 7);
197     LOAD_ZERO;
198     const vec_s16_t v28ss = vec_sub(vec_sl(vec_splat_s16(1),vec_splat_u16(5)),vec_splat_s16(4));
199     const vec_u16_t v6us = vec_splat_u16(6);
200     register int loadSecond     = (((unsigned long)src) % 16) <= 7 ? 0 : 1;
201     register int reallyBadAlign = (((unsigned long)src) % 16) == 15 ? 1 : 0;
202
203     vec_u8_t vsrcAuc, vsrcBuc, vsrcperm0, vsrcperm1;
204     vec_u8_t vsrc0uc, vsrc1uc;
205     vec_s16_t vsrc0ssH, vsrc1ssH;
206     vec_u8_t vsrcCuc, vsrc2uc, vsrc3uc;
207     vec_s16_t vsrc2ssH, vsrc3ssH, psum;
208     vec_u8_t vdst, ppsum, fsum;
209
210     if (((unsigned long)dst) % 16 == 0) {
211         fperm = (vec_u8_t)AVV(0x10, 0x11, 0x12, 0x13,
212                               0x14, 0x15, 0x16, 0x17,
213                               0x08, 0x09, 0x0A, 0x0B,
214                               0x0C, 0x0D, 0x0E, 0x0F);
215     } else {
216         fperm = (vec_u8_t)AVV(0x00, 0x01, 0x02, 0x03,
217                               0x04, 0x05, 0x06, 0x07,
218                               0x18, 0x19, 0x1A, 0x1B,
219                               0x1C, 0x1D, 0x1E, 0x1F);
220     }
221
222     vsrcAuc = vec_ld(0, src);
223
224     if (loadSecond)
225         vsrcBuc = vec_ld(16, src);
226     vsrcperm0 = vec_lvsl(0, src);
227     vsrcperm1 = vec_lvsl(1, src);
228
229     vsrc0uc = vec_perm(vsrcAuc, vsrcBuc, vsrcperm0);
230     if (reallyBadAlign)
231         vsrc1uc = vsrcBuc;
232     else
233         vsrc1uc = vec_perm(vsrcAuc, vsrcBuc, vsrcperm1);
234
235     vsrc0ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc0uc);
236     vsrc1ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc1uc);
237
238     if (!loadSecond) {// -> !reallyBadAlign
239         for (i = 0 ; i < h ; i++) {
240
241
242             vsrcCuc = vec_ld(stride + 0, src);
243
244             vsrc2uc = vec_perm(vsrcCuc, vsrcCuc, vsrcperm0);
245             vsrc3uc = vec_perm(vsrcCuc, vsrcCuc, vsrcperm1);
246
247             vsrc2ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc2uc);
248             vsrc3ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc3uc);
249
250             psum = vec_mladd(vA, vsrc0ssH, vec_splat_s16(0));
251             psum = vec_mladd(vB, vsrc1ssH, psum);
252             psum = vec_mladd(vC, vsrc2ssH, psum);
253             psum = vec_mladd(vD, vsrc3ssH, psum);
254             psum = vec_add(v28ss, psum);
255             psum = vec_sra(psum, v6us);
256
257             vdst = vec_ld(0, dst);
258             ppsum = (vec_u8_t)vec_packsu(psum, psum);
259             fsum = vec_perm(vdst, ppsum, fperm);
260
261             vec_st(fsum, 0, dst);
262
263             vsrc0ssH = vsrc2ssH;
264             vsrc1ssH = vsrc3ssH;
265
266             dst += stride;
267             src += stride;
268         }
269     } else {
270         vec_u8_t vsrcDuc;
271         for (i = 0 ; i < h ; i++) {
272             vsrcCuc = vec_ld(stride + 0, src);
273             vsrcDuc = vec_ld(stride + 16, src);
274
275             vsrc2uc = vec_perm(vsrcCuc, vsrcDuc, vsrcperm0);
276             if (reallyBadAlign)
277                 vsrc3uc = vsrcDuc;
278             else
279                 vsrc3uc = vec_perm(vsrcCuc, vsrcDuc, vsrcperm1);
280
281             vsrc2ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc2uc);
282             vsrc3ssH = (vec_s16_t)vec_mergeh(zero_u8v, (vec_u8_t)vsrc3uc);
283
284             psum = vec_mladd(vA, vsrc0ssH, vec_splat_s16(0));
285             psum = vec_mladd(vB, vsrc1ssH, psum);
286             psum = vec_mladd(vC, vsrc2ssH, psum);
287             psum = vec_mladd(vD, vsrc3ssH, psum);
288             psum = vec_add(v28ss, psum);
289             psum = vec_sr(psum, v6us);
290
291             vdst = vec_ld(0, dst);
292             ppsum = (vec_u8_t)vec_pack(psum, psum);
293             fsum = vec_perm(vdst, ppsum, fperm);
294
295             vec_st(fsum, 0, dst);
296
297             vsrc0ssH = vsrc2ssH;
298             vsrc1ssH = vsrc3ssH;
299
300             dst += stride;
301             src += stride;
302         }
303     }
304 }
305
306 static inline void put_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
307                                     const uint8_t * src2, int dst_stride,
308                                     int src_stride1, int h)
309 {
310     int i;
311     vec_u8_t a, b, d, tmp1, tmp2, mask, mask_, edges, align;
312
313     mask_ = vec_lvsl(0, src2);
314
315     for (i = 0; i < h; i++) {
316
317         tmp1 = vec_ld(i * src_stride1, src1);
318         mask = vec_lvsl(i * src_stride1, src1);
319         tmp2 = vec_ld(i * src_stride1 + 15, src1);
320
321         a = vec_perm(tmp1, tmp2, mask);
322
323         tmp1 = vec_ld(i * 16, src2);
324         tmp2 = vec_ld(i * 16 + 15, src2);
325
326         b = vec_perm(tmp1, tmp2, mask_);
327
328         tmp1 = vec_ld(0, dst);
329         mask = vec_lvsl(0, dst);
330         tmp2 = vec_ld(15, dst);
331
332         d = vec_avg(a, b);
333
334         edges = vec_perm(tmp2, tmp1, mask);
335
336         align = vec_lvsr(0, dst);
337
338         tmp2 = vec_perm(d, edges, align);
339         tmp1 = vec_perm(edges, d, align);
340
341         vec_st(tmp2, 15, dst);
342         vec_st(tmp1, 0 , dst);
343
344         dst += dst_stride;
345     }
346 }
347
348 static inline void avg_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
349                                     const uint8_t * src2, int dst_stride,
350                                     int src_stride1, int h)
351 {
352     int i;
353     vec_u8_t a, b, d, tmp1, tmp2, mask, mask_, edges, align;
354
355     mask_ = vec_lvsl(0, src2);
356
357     for (i = 0; i < h; i++) {
358
359         tmp1 = vec_ld(i * src_stride1, src1);
360         mask = vec_lvsl(i * src_stride1, src1);
361         tmp2 = vec_ld(i * src_stride1 + 15, src1);
362
363         a = vec_perm(tmp1, tmp2, mask);
364
365         tmp1 = vec_ld(i * 16, src2);
366         tmp2 = vec_ld(i * 16 + 15, src2);
367
368         b = vec_perm(tmp1, tmp2, mask_);
369
370         tmp1 = vec_ld(0, dst);
371         mask = vec_lvsl(0, dst);
372         tmp2 = vec_ld(15, dst);
373
374         d = vec_avg(vec_perm(tmp1, tmp2, mask), vec_avg(a, b));
375
376         edges = vec_perm(tmp2, tmp1, mask);
377
378         align = vec_lvsr(0, dst);
379
380         tmp2 = vec_perm(d, edges, align);
381         tmp1 = vec_perm(edges, d, align);
382
383         vec_st(tmp2, 15, dst);
384         vec_st(tmp1, 0 , dst);
385
386         dst += dst_stride;
387     }
388 }
389
390 /* Implemented but could be faster
391 #define put_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) put_pixels16_l2(d,s1,s2,ds,s1s,16,h)
392 #define avg_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) avg_pixels16_l2(d,s1,s2,ds,s1s,16,h)
393  */
394
395   H264_MC(put_, 16, altivec)
396   H264_MC(avg_, 16, altivec)
397
398
399 /****************************************************************************
400  * IDCT transform:
401  ****************************************************************************/
402
403 #define VEC_1D_DCT(vb0,vb1,vb2,vb3,va0,va1,va2,va3)               \
404     /* 1st stage */                                               \
405     vz0 = vec_add(vb0,vb2);       /* temp[0] = Y[0] + Y[2] */     \
406     vz1 = vec_sub(vb0,vb2);       /* temp[1] = Y[0] - Y[2] */     \
407     vz2 = vec_sra(vb1,vec_splat_u16(1));                          \
408     vz2 = vec_sub(vz2,vb3);       /* temp[2] = Y[1].1/2 - Y[3] */ \
409     vz3 = vec_sra(vb3,vec_splat_u16(1));                          \
410     vz3 = vec_add(vb1,vz3);       /* temp[3] = Y[1] + Y[3].1/2 */ \
411     /* 2nd stage: output */                                       \
412     va0 = vec_add(vz0,vz3);       /* x[0] = temp[0] + temp[3] */  \
413     va1 = vec_add(vz1,vz2);       /* x[1] = temp[1] + temp[2] */  \
414     va2 = vec_sub(vz1,vz2);       /* x[2] = temp[1] - temp[2] */  \
415     va3 = vec_sub(vz0,vz3)        /* x[3] = temp[0] - temp[3] */
416
417 #define VEC_TRANSPOSE_4(a0,a1,a2,a3,b0,b1,b2,b3) \
418     b0 = vec_mergeh( a0, a0 ); \
419     b1 = vec_mergeh( a1, a0 ); \
420     b2 = vec_mergeh( a2, a0 ); \
421     b3 = vec_mergeh( a3, a0 ); \
422     a0 = vec_mergeh( b0, b2 ); \
423     a1 = vec_mergel( b0, b2 ); \
424     a2 = vec_mergeh( b1, b3 ); \
425     a3 = vec_mergel( b1, b3 ); \
426     b0 = vec_mergeh( a0, a2 ); \
427     b1 = vec_mergel( a0, a2 ); \
428     b2 = vec_mergeh( a1, a3 ); \
429     b3 = vec_mergel( a1, a3 )
430
431 #define VEC_LOAD_U8_ADD_S16_STORE_U8(va)                      \
432     vdst_orig = vec_ld(0, dst);                               \
433     vdst = vec_perm(vdst_orig, zero_u8v, vdst_mask);          \
434     vdst_ss = (vec_s16_t) vec_mergeh(zero_u8v, vdst);         \
435     va = vec_add(va, vdst_ss);                                \
436     va_u8 = vec_packsu(va, zero_s16v);                        \
437     va_u32 = vec_splat((vec_u32_t)va_u8, 0);                  \
438     vec_ste(va_u32, element, (uint32_t*)dst);
439
440 static void ff_h264_idct_add_altivec(uint8_t *dst, DCTELEM *block, int stride)
441 {
442     vec_s16_t va0, va1, va2, va3;
443     vec_s16_t vz0, vz1, vz2, vz3;
444     vec_s16_t vtmp0, vtmp1, vtmp2, vtmp3;
445     vec_u8_t va_u8;
446     vec_u32_t va_u32;
447     vec_s16_t vdst_ss;
448     const vec_u16_t v6us = vec_splat_u16(6);
449     vec_u8_t vdst, vdst_orig;
450     vec_u8_t vdst_mask = vec_lvsl(0, dst);
451     int element = ((unsigned long)dst & 0xf) >> 2;
452     LOAD_ZERO;
453
454     block[0] += 32;  /* add 32 as a DC-level for rounding */
455
456     vtmp0 = vec_ld(0,block);
457     vtmp1 = vec_sld(vtmp0, vtmp0, 8);
458     vtmp2 = vec_ld(16,block);
459     vtmp3 = vec_sld(vtmp2, vtmp2, 8);
460
461     VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
462     VEC_TRANSPOSE_4(va0,va1,va2,va3,vtmp0,vtmp1,vtmp2,vtmp3);
463     VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
464
465     va0 = vec_sra(va0,v6us);
466     va1 = vec_sra(va1,v6us);
467     va2 = vec_sra(va2,v6us);
468     va3 = vec_sra(va3,v6us);
469
470     VEC_LOAD_U8_ADD_S16_STORE_U8(va0);
471     dst += stride;
472     VEC_LOAD_U8_ADD_S16_STORE_U8(va1);
473     dst += stride;
474     VEC_LOAD_U8_ADD_S16_STORE_U8(va2);
475     dst += stride;
476     VEC_LOAD_U8_ADD_S16_STORE_U8(va3);
477 }
478
479 #define IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7,  d0, d1, d2, d3, d4, d5, d6, d7) {\
480     /*        a0  = SRC(0) + SRC(4); */ \
481     vec_s16_t a0v = vec_add(s0, s4);    \
482     /*        a2  = SRC(0) - SRC(4); */ \
483     vec_s16_t a2v = vec_sub(s0, s4);    \
484     /*        a4  =           (SRC(2)>>1) - SRC(6); */ \
485     vec_s16_t a4v = vec_sub(vec_sra(s2, onev), s6);    \
486     /*        a6  =           (SRC(6)>>1) + SRC(2); */ \
487     vec_s16_t a6v = vec_add(vec_sra(s6, onev), s2);    \
488     /*        b0  =         a0 + a6; */ \
489     vec_s16_t b0v = vec_add(a0v, a6v);  \
490     /*        b2  =         a2 + a4; */ \
491     vec_s16_t b2v = vec_add(a2v, a4v);  \
492     /*        b4  =         a2 - a4; */ \
493     vec_s16_t b4v = vec_sub(a2v, a4v);  \
494     /*        b6  =         a0 - a6; */ \
495     vec_s16_t b6v = vec_sub(a0v, a6v);  \
496     /* a1 =  SRC(5) - SRC(3) - SRC(7) - (SRC(7)>>1); */ \
497     /*        a1 =             (SRC(5)-SRC(3)) -  (SRC(7)  +  (SRC(7)>>1)); */ \
498     vec_s16_t a1v = vec_sub( vec_sub(s5, s3), vec_add(s7, vec_sra(s7, onev)) ); \
499     /* a3 =  SRC(7) + SRC(1) - SRC(3) - (SRC(3)>>1); */ \
500     /*        a3 =             (SRC(7)+SRC(1)) -  (SRC(3)  +  (SRC(3)>>1)); */ \
501     vec_s16_t a3v = vec_sub( vec_add(s7, s1), vec_add(s3, vec_sra(s3, onev)) );\
502     /* a5 =  SRC(7) - SRC(1) + SRC(5) + (SRC(5)>>1); */ \
503     /*        a5 =             (SRC(7)-SRC(1)) +   SRC(5) +   (SRC(5)>>1); */ \
504     vec_s16_t a5v = vec_add( vec_sub(s7, s1), vec_add(s5, vec_sra(s5, onev)) );\
505     /*        a7 =                SRC(5)+SRC(3) +  SRC(1) +   (SRC(1)>>1); */ \
506     vec_s16_t a7v = vec_add( vec_add(s5, s3), vec_add(s1, vec_sra(s1, onev)) );\
507     /*        b1 =                  (a7>>2)  +  a1; */ \
508     vec_s16_t b1v = vec_add( vec_sra(a7v, twov), a1v); \
509     /*        b3 =          a3 +        (a5>>2); */ \
510     vec_s16_t b3v = vec_add(a3v, vec_sra(a5v, twov)); \
511     /*        b5 =                  (a3>>2)  -   a5; */ \
512     vec_s16_t b5v = vec_sub( vec_sra(a3v, twov), a5v); \
513     /*        b7 =           a7 -        (a1>>2); */ \
514     vec_s16_t b7v = vec_sub( a7v, vec_sra(a1v, twov)); \
515     /* DST(0,    b0 + b7); */ \
516     d0 = vec_add(b0v, b7v); \
517     /* DST(1,    b2 + b5); */ \
518     d1 = vec_add(b2v, b5v); \
519     /* DST(2,    b4 + b3); */ \
520     d2 = vec_add(b4v, b3v); \
521     /* DST(3,    b6 + b1); */ \
522     d3 = vec_add(b6v, b1v); \
523     /* DST(4,    b6 - b1); */ \
524     d4 = vec_sub(b6v, b1v); \
525     /* DST(5,    b4 - b3); */ \
526     d5 = vec_sub(b4v, b3v); \
527     /* DST(6,    b2 - b5); */ \
528     d6 = vec_sub(b2v, b5v); \
529     /* DST(7,    b0 - b7); */ \
530     d7 = vec_sub(b0v, b7v); \
531 }
532
533 #define ALTIVEC_STORE_SUM_CLIP(dest, idctv, perm_ldv, perm_stv, sel) { \
534     /* unaligned load */                                       \
535     vec_u8_t hv = vec_ld( 0, dest );                           \
536     vec_u8_t lv = vec_ld( 7, dest );                           \
537     vec_u8_t dstv   = vec_perm( hv, lv, (vec_u8_t)perm_ldv );  \
538     vec_s16_t idct_sh6 = vec_sra(idctv, sixv);                 \
539     vec_u16_t dst16 = (vec_u16_t)vec_mergeh(zero_u8v, dstv);   \
540     vec_s16_t idstsum = vec_adds(idct_sh6, (vec_s16_t)dst16);  \
541     vec_u8_t idstsum8 = vec_packsu(zero_s16v, idstsum);        \
542     vec_u8_t edgehv;                                           \
543     /* unaligned store */                                      \
544     vec_u8_t bodyv  = vec_perm( idstsum8, idstsum8, perm_stv );\
545     vec_u8_t edgelv = vec_perm( sel, zero_u8v, perm_stv );     \
546     lv    = vec_sel( lv, bodyv, edgelv );                      \
547     vec_st( lv, 7, dest );                                     \
548     hv    = vec_ld( 0, dest );                                 \
549     edgehv = vec_perm( zero_u8v, sel, perm_stv );              \
550     hv    = vec_sel( hv, bodyv, edgehv );                      \
551     vec_st( hv, 0, dest );                                     \
552  }
553
554 void ff_h264_idct8_add_altivec( uint8_t *dst, DCTELEM *dct, int stride ) {
555     vec_s16_t s0, s1, s2, s3, s4, s5, s6, s7;
556     vec_s16_t d0, d1, d2, d3, d4, d5, d6, d7;
557     vec_s16_t idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7;
558
559     vec_u8_t perm_ldv = vec_lvsl(0, dst);
560     vec_u8_t perm_stv = vec_lvsr(8, dst);
561
562     const vec_u16_t onev = vec_splat_u16(1);
563     const vec_u16_t twov = vec_splat_u16(2);
564     const vec_u16_t sixv = vec_splat_u16(6);
565
566     const vec_u8_t sel = (vec_u8_t) AVV(0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1);
567     LOAD_ZERO;
568
569     dct[0] += 32; // rounding for the >>6 at the end
570
571     s0 = vec_ld(0x00, (int16_t*)dct);
572     s1 = vec_ld(0x10, (int16_t*)dct);
573     s2 = vec_ld(0x20, (int16_t*)dct);
574     s3 = vec_ld(0x30, (int16_t*)dct);
575     s4 = vec_ld(0x40, (int16_t*)dct);
576     s5 = vec_ld(0x50, (int16_t*)dct);
577     s6 = vec_ld(0x60, (int16_t*)dct);
578     s7 = vec_ld(0x70, (int16_t*)dct);
579
580     IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7,
581                      d0, d1, d2, d3, d4, d5, d6, d7);
582
583     TRANSPOSE8( d0,  d1,  d2,  d3,  d4,  d5,  d6, d7 );
584
585     IDCT8_1D_ALTIVEC(d0,  d1,  d2,  d3,  d4,  d5,  d6, d7,
586                      idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7);
587
588     ALTIVEC_STORE_SUM_CLIP(&dst[0*stride], idct0, perm_ldv, perm_stv, sel);
589     ALTIVEC_STORE_SUM_CLIP(&dst[1*stride], idct1, perm_ldv, perm_stv, sel);
590     ALTIVEC_STORE_SUM_CLIP(&dst[2*stride], idct2, perm_ldv, perm_stv, sel);
591     ALTIVEC_STORE_SUM_CLIP(&dst[3*stride], idct3, perm_ldv, perm_stv, sel);
592     ALTIVEC_STORE_SUM_CLIP(&dst[4*stride], idct4, perm_ldv, perm_stv, sel);
593     ALTIVEC_STORE_SUM_CLIP(&dst[5*stride], idct5, perm_ldv, perm_stv, sel);
594     ALTIVEC_STORE_SUM_CLIP(&dst[6*stride], idct6, perm_ldv, perm_stv, sel);
595     ALTIVEC_STORE_SUM_CLIP(&dst[7*stride], idct7, perm_ldv, perm_stv, sel);
596 }
597
598 #define transpose4x16(r0, r1, r2, r3) {      \
599     register vec_u8_t r4;                    \
600     register vec_u8_t r5;                    \
601     register vec_u8_t r6;                    \
602     register vec_u8_t r7;                    \
603                                              \
604     r4 = vec_mergeh(r0, r2);  /*0, 2 set 0*/ \
605     r5 = vec_mergel(r0, r2);  /*0, 2 set 1*/ \
606     r6 = vec_mergeh(r1, r3);  /*1, 3 set 0*/ \
607     r7 = vec_mergel(r1, r3);  /*1, 3 set 1*/ \
608                                              \
609     r0 = vec_mergeh(r4, r6);  /*all set 0*/  \
610     r1 = vec_mergel(r4, r6);  /*all set 1*/  \
611     r2 = vec_mergeh(r5, r7);  /*all set 2*/  \
612     r3 = vec_mergel(r5, r7);  /*all set 3*/  \
613 }
614
615 static inline void write16x4(uint8_t *dst, int dst_stride,
616                              register vec_u8_t r0, register vec_u8_t r1,
617                              register vec_u8_t r2, register vec_u8_t r3) {
618     DECLARE_ALIGNED_16(unsigned char, result[64]);
619     uint32_t *src_int = (uint32_t *)result, *dst_int = (uint32_t *)dst;
620     int int_dst_stride = dst_stride/4;
621
622     vec_st(r0, 0, result);
623     vec_st(r1, 16, result);
624     vec_st(r2, 32, result);
625     vec_st(r3, 48, result);
626     /* FIXME: there has to be a better way!!!! */
627     *dst_int = *src_int;
628     *(dst_int+   int_dst_stride) = *(src_int + 1);
629     *(dst_int+ 2*int_dst_stride) = *(src_int + 2);
630     *(dst_int+ 3*int_dst_stride) = *(src_int + 3);
631     *(dst_int+ 4*int_dst_stride) = *(src_int + 4);
632     *(dst_int+ 5*int_dst_stride) = *(src_int + 5);
633     *(dst_int+ 6*int_dst_stride) = *(src_int + 6);
634     *(dst_int+ 7*int_dst_stride) = *(src_int + 7);
635     *(dst_int+ 8*int_dst_stride) = *(src_int + 8);
636     *(dst_int+ 9*int_dst_stride) = *(src_int + 9);
637     *(dst_int+10*int_dst_stride) = *(src_int + 10);
638     *(dst_int+11*int_dst_stride) = *(src_int + 11);
639     *(dst_int+12*int_dst_stride) = *(src_int + 12);
640     *(dst_int+13*int_dst_stride) = *(src_int + 13);
641     *(dst_int+14*int_dst_stride) = *(src_int + 14);
642     *(dst_int+15*int_dst_stride) = *(src_int + 15);
643 }
644
645 /** \brief performs a 6x16 transpose of data in src, and stores it to dst
646     \todo FIXME: see if we can't spare some vec_lvsl() by them factorizing
647     out of unaligned_load() */
648 #define readAndTranspose16x6(src, src_stride, r8, r9, r10, r11, r12, r13) {\
649     register vec_u8_t r0  = unaligned_load(0,             src);            \
650     register vec_u8_t r1  = unaligned_load(   src_stride, src);            \
651     register vec_u8_t r2  = unaligned_load(2* src_stride, src);            \
652     register vec_u8_t r3  = unaligned_load(3* src_stride, src);            \
653     register vec_u8_t r4  = unaligned_load(4* src_stride, src);            \
654     register vec_u8_t r5  = unaligned_load(5* src_stride, src);            \
655     register vec_u8_t r6  = unaligned_load(6* src_stride, src);            \
656     register vec_u8_t r7  = unaligned_load(7* src_stride, src);            \
657     register vec_u8_t r14 = unaligned_load(14*src_stride, src);            \
658     register vec_u8_t r15 = unaligned_load(15*src_stride, src);            \
659                                                                            \
660     r8  = unaligned_load( 8*src_stride, src);                              \
661     r9  = unaligned_load( 9*src_stride, src);                              \
662     r10 = unaligned_load(10*src_stride, src);                              \
663     r11 = unaligned_load(11*src_stride, src);                              \
664     r12 = unaligned_load(12*src_stride, src);                              \
665     r13 = unaligned_load(13*src_stride, src);                              \
666                                                                            \
667     /*Merge first pairs*/                                                  \
668     r0 = vec_mergeh(r0, r8);    /*0, 8*/                                   \
669     r1 = vec_mergeh(r1, r9);    /*1, 9*/                                   \
670     r2 = vec_mergeh(r2, r10);   /*2,10*/                                   \
671     r3 = vec_mergeh(r3, r11);   /*3,11*/                                   \
672     r4 = vec_mergeh(r4, r12);   /*4,12*/                                   \
673     r5 = vec_mergeh(r5, r13);   /*5,13*/                                   \
674     r6 = vec_mergeh(r6, r14);   /*6,14*/                                   \
675     r7 = vec_mergeh(r7, r15);   /*7,15*/                                   \
676                                                                            \
677     /*Merge second pairs*/                                                 \
678     r8  = vec_mergeh(r0, r4);   /*0,4, 8,12 set 0*/                        \
679     r9  = vec_mergel(r0, r4);   /*0,4, 8,12 set 1*/                        \
680     r10 = vec_mergeh(r1, r5);   /*1,5, 9,13 set 0*/                        \
681     r11 = vec_mergel(r1, r5);   /*1,5, 9,13 set 1*/                        \
682     r12 = vec_mergeh(r2, r6);   /*2,6,10,14 set 0*/                        \
683     r13 = vec_mergel(r2, r6);   /*2,6,10,14 set 1*/                        \
684     r14 = vec_mergeh(r3, r7);   /*3,7,11,15 set 0*/                        \
685     r15 = vec_mergel(r3, r7);   /*3,7,11,15 set 1*/                        \
686                                                                            \
687     /*Third merge*/                                                        \
688     r0 = vec_mergeh(r8, r12);   /*0,2,4,6,8,10,12,14 set 0*/               \
689     r1 = vec_mergel(r8, r12);   /*0,2,4,6,8,10,12,14 set 1*/               \
690     r2 = vec_mergeh(r9, r13);   /*0,2,4,6,8,10,12,14 set 2*/               \
691     r4 = vec_mergeh(r10, r14);  /*1,3,5,7,9,11,13,15 set 0*/               \
692     r5 = vec_mergel(r10, r14);  /*1,3,5,7,9,11,13,15 set 1*/               \
693     r6 = vec_mergeh(r11, r15);  /*1,3,5,7,9,11,13,15 set 2*/               \
694     /* Don't need to compute 3 and 7*/                                     \
695                                                                            \
696     /*Final merge*/                                                        \
697     r8  = vec_mergeh(r0, r4);   /*all set 0*/                              \
698     r9  = vec_mergel(r0, r4);   /*all set 1*/                              \
699     r10 = vec_mergeh(r1, r5);   /*all set 2*/                              \
700     r11 = vec_mergel(r1, r5);   /*all set 3*/                              \
701     r12 = vec_mergeh(r2, r6);   /*all set 4*/                              \
702     r13 = vec_mergel(r2, r6);   /*all set 5*/                              \
703     /* Don't need to compute 14 and 15*/                                   \
704                                                                            \
705 }
706
707 // out: o = |x-y| < a
708 static inline vec_u8_t diff_lt_altivec ( register vec_u8_t x,
709                                          register vec_u8_t y,
710                                          register vec_u8_t a) {
711
712     register vec_u8_t diff = vec_subs(x, y);
713     register vec_u8_t diffneg = vec_subs(y, x);
714     register vec_u8_t o = vec_or(diff, diffneg); /* |x-y| */
715     o = (vec_u8_t)vec_cmplt(o, a);
716     return o;
717 }
718
719 static inline vec_u8_t h264_deblock_mask ( register vec_u8_t p0,
720                                            register vec_u8_t p1,
721                                            register vec_u8_t q0,
722                                            register vec_u8_t q1,
723                                            register vec_u8_t alpha,
724                                            register vec_u8_t beta) {
725
726     register vec_u8_t mask;
727     register vec_u8_t tempmask;
728
729     mask = diff_lt_altivec(p0, q0, alpha);
730     tempmask = diff_lt_altivec(p1, p0, beta);
731     mask = vec_and(mask, tempmask);
732     tempmask = diff_lt_altivec(q1, q0, beta);
733     mask = vec_and(mask, tempmask);
734
735     return mask;
736 }
737
738 // out: newp1 = clip((p2 + ((p0 + q0 + 1) >> 1)) >> 1, p1-tc0, p1+tc0)
739 static inline vec_u8_t h264_deblock_q1(register vec_u8_t p0,
740                                        register vec_u8_t p1,
741                                        register vec_u8_t p2,
742                                        register vec_u8_t q0,
743                                        register vec_u8_t tc0) {
744
745     register vec_u8_t average = vec_avg(p0, q0);
746     register vec_u8_t temp;
747     register vec_u8_t uncliped;
748     register vec_u8_t ones;
749     register vec_u8_t max;
750     register vec_u8_t min;
751     register vec_u8_t newp1;
752
753     temp = vec_xor(average, p2);
754     average = vec_avg(average, p2);     /*avg(p2, avg(p0, q0)) */
755     ones = vec_splat_u8(1);
756     temp = vec_and(temp, ones);         /*(p2^avg(p0, q0)) & 1 */
757     uncliped = vec_subs(average, temp); /*(p2+((p0+q0+1)>>1))>>1 */
758     max = vec_adds(p1, tc0);
759     min = vec_subs(p1, tc0);
760     newp1 = vec_max(min, uncliped);
761     newp1 = vec_min(max, newp1);
762     return newp1;
763 }
764
765 #define h264_deblock_p0_q0(p0, p1, q0, q1, tc0masked) {                                           \
766                                                                                                   \
767     const vec_u8_t A0v = vec_sl(vec_splat_u8(10), vec_splat_u8(4));                               \
768                                                                                                   \
769     register vec_u8_t pq0bit = vec_xor(p0,q0);                                                    \
770     register vec_u8_t q1minus;                                                                    \
771     register vec_u8_t p0minus;                                                                    \
772     register vec_u8_t stage1;                                                                     \
773     register vec_u8_t stage2;                                                                     \
774     register vec_u8_t vec160;                                                                     \
775     register vec_u8_t delta;                                                                      \
776     register vec_u8_t deltaneg;                                                                   \
777                                                                                                   \
778     q1minus = vec_nor(q1, q1);                 /* 255 - q1 */                                     \
779     stage1 = vec_avg(p1, q1minus);             /* (p1 - q1 + 256)>>1 */                           \
780     stage2 = vec_sr(stage1, vec_splat_u8(1));  /* (p1 - q1 + 256)>>2 = 64 + (p1 - q1) >> 2 */     \
781     p0minus = vec_nor(p0, p0);                 /* 255 - p0 */                                     \
782     stage1 = vec_avg(q0, p0minus);             /* (q0 - p0 + 256)>>1 */                           \
783     pq0bit = vec_and(pq0bit, vec_splat_u8(1));                                                    \
784     stage2 = vec_avg(stage2, pq0bit);          /* 32 + ((q0 - p0)&1 + (p1 - q1) >> 2 + 1) >> 1 */ \
785     stage2 = vec_adds(stage2, stage1);         /* 160 + ((p0 - q0) + (p1 - q1) >> 2 + 1) >> 1 */  \
786     vec160 = vec_ld(0, &A0v);                                                                     \
787     deltaneg = vec_subs(vec160, stage2);       /* -d */                                           \
788     delta = vec_subs(stage2, vec160);          /* d */                                            \
789     deltaneg = vec_min(tc0masked, deltaneg);                                                      \
790     delta = vec_min(tc0masked, delta);                                                            \
791     p0 = vec_subs(p0, deltaneg);                                                                  \
792     q0 = vec_subs(q0, delta);                                                                     \
793     p0 = vec_adds(p0, delta);                                                                     \
794     q0 = vec_adds(q0, deltaneg);                                                                  \
795 }
796
797 #define h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0) {            \
798     DECLARE_ALIGNED_16(unsigned char, temp[16]);                                             \
799     register vec_u8_t alphavec;                                                              \
800     register vec_u8_t betavec;                                                               \
801     register vec_u8_t mask;                                                                  \
802     register vec_u8_t p1mask;                                                                \
803     register vec_u8_t q1mask;                                                                \
804     register vector signed   char tc0vec;                                                    \
805     register vec_u8_t finaltc0;                                                              \
806     register vec_u8_t tc0masked;                                                             \
807     register vec_u8_t newp1;                                                                 \
808     register vec_u8_t newq1;                                                                 \
809                                                                                              \
810     temp[0] = alpha;                                                                         \
811     temp[1] = beta;                                                                          \
812     alphavec = vec_ld(0, temp);                                                              \
813     betavec = vec_splat(alphavec, 0x1);                                                      \
814     alphavec = vec_splat(alphavec, 0x0);                                                     \
815     mask = h264_deblock_mask(p0, p1, q0, q1, alphavec, betavec); /*if in block */            \
816                                                                                              \
817     *((int *)temp) = *((int *)tc0);                                                          \
818     tc0vec = vec_ld(0, (signed char*)temp);                                                  \
819     tc0vec = vec_mergeh(tc0vec, tc0vec);                                                     \
820     tc0vec = vec_mergeh(tc0vec, tc0vec);                                                     \
821     mask = vec_and(mask, vec_cmpgt(tc0vec, vec_splat_s8(-1)));  /* if tc0[i] >= 0 */         \
822     finaltc0 = vec_and((vec_u8_t)tc0vec, mask);     /* tc = tc0 */                           \
823                                                                                              \
824     p1mask = diff_lt_altivec(p2, p0, betavec);                                               \
825     p1mask = vec_and(p1mask, mask);                             /* if ( |p2 - p0| < beta) */ \
826     tc0masked = vec_and(p1mask, (vec_u8_t)tc0vec);                                           \
827     finaltc0 = vec_sub(finaltc0, p1mask);                       /* tc++ */                   \
828     newp1 = h264_deblock_q1(p0, p1, p2, q0, tc0masked);                                      \
829     /*end if*/                                                                               \
830                                                                                              \
831     q1mask = diff_lt_altivec(q2, q0, betavec);                                               \
832     q1mask = vec_and(q1mask, mask);                             /* if ( |q2 - q0| < beta ) */\
833     tc0masked = vec_and(q1mask, (vec_u8_t)tc0vec);                                           \
834     finaltc0 = vec_sub(finaltc0, q1mask);                       /* tc++ */                   \
835     newq1 = h264_deblock_q1(p0, q1, q2, q0, tc0masked);                                      \
836     /*end if*/                                                                               \
837                                                                                              \
838     h264_deblock_p0_q0(p0, p1, q0, q1, finaltc0);                                            \
839     p1 = newp1;                                                                              \
840     q1 = newq1;                                                                              \
841 }
842
843 static void h264_v_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
844
845     if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) >= 0) {
846         register vec_u8_t p2 = vec_ld(-3*stride, pix);
847         register vec_u8_t p1 = vec_ld(-2*stride, pix);
848         register vec_u8_t p0 = vec_ld(-1*stride, pix);
849         register vec_u8_t q0 = vec_ld(0, pix);
850         register vec_u8_t q1 = vec_ld(stride, pix);
851         register vec_u8_t q2 = vec_ld(2*stride, pix);
852         h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0);
853         vec_st(p1, -2*stride, pix);
854         vec_st(p0, -1*stride, pix);
855         vec_st(q0, 0, pix);
856         vec_st(q1, stride, pix);
857     }
858 }
859
860 static void h264_h_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
861
862     register vec_u8_t line0, line1, line2, line3, line4, line5;
863     if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) < 0)
864         return;
865     readAndTranspose16x6(pix-3, stride, line0, line1, line2, line3, line4, line5);
866     h264_loop_filter_luma_altivec(line0, line1, line2, line3, line4, line5, alpha, beta, tc0);
867     transpose4x16(line1, line2, line3, line4);
868     write16x4(pix-2, stride, line1, line2, line3, line4);
869 }
870
871 void dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx) {
872
873     if (has_altivec()) {
874         c->put_h264_chroma_pixels_tab[0] = put_h264_chroma_mc8_altivec;
875         c->put_no_rnd_h264_chroma_pixels_tab[0] = put_no_rnd_h264_chroma_mc8_altivec;
876         c->avg_h264_chroma_pixels_tab[0] = avg_h264_chroma_mc8_altivec;
877         c->h264_idct_add = ff_h264_idct_add_altivec;
878         c->h264_idct8_add = ff_h264_idct8_add_altivec;
879         c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_altivec;
880         c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_altivec;
881
882 #define dspfunc(PFX, IDX, NUM) \
883         c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_altivec; \
884         c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_altivec; \
885         c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_altivec; \
886         c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_altivec; \
887         c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_altivec; \
888         c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_altivec; \
889         c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_altivec; \
890         c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_altivec; \
891         c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_altivec; \
892         c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_altivec; \
893         c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_altivec; \
894         c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_altivec; \
895         c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_altivec; \
896         c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_altivec; \
897         c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_altivec; \
898         c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_altivec
899
900         dspfunc(put_h264_qpel, 0, 16);
901         dspfunc(avg_h264_qpel, 0, 16);
902 #undef dspfunc
903     }
904 }