lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / ppc / h264_altivec.c
blob59642fdc363453b529586279f95f9c9a96c8b138
1 /*
2 * Copyright (c) 2004 Romain Dolbeau <romain@dolbeau.org>
4 * This file is part of Libav.
6 * Libav 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.
11 * Libav 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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/attributes.h"
22 #include "libavutil/cpu.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/ppc/types_altivec.h"
25 #include "libavutil/ppc/util_altivec.h"
26 #include "libavcodec/h264data.h"
27 #include "libavcodec/h264dsp.h"
29 /****************************************************************************
30 * IDCT transform:
31 ****************************************************************************/
33 #define VEC_1D_DCT(vb0,vb1,vb2,vb3,va0,va1,va2,va3) \
34 /* 1st stage */ \
35 vz0 = vec_add(vb0,vb2); /* temp[0] = Y[0] + Y[2] */ \
36 vz1 = vec_sub(vb0,vb2); /* temp[1] = Y[0] - Y[2] */ \
37 vz2 = vec_sra(vb1,vec_splat_u16(1)); \
38 vz2 = vec_sub(vz2,vb3); /* temp[2] = Y[1].1/2 - Y[3] */ \
39 vz3 = vec_sra(vb3,vec_splat_u16(1)); \
40 vz3 = vec_add(vb1,vz3); /* temp[3] = Y[1] + Y[3].1/2 */ \
41 /* 2nd stage: output */ \
42 va0 = vec_add(vz0,vz3); /* x[0] = temp[0] + temp[3] */ \
43 va1 = vec_add(vz1,vz2); /* x[1] = temp[1] + temp[2] */ \
44 va2 = vec_sub(vz1,vz2); /* x[2] = temp[1] - temp[2] */ \
45 va3 = vec_sub(vz0,vz3) /* x[3] = temp[0] - temp[3] */
47 #define VEC_TRANSPOSE_4(a0,a1,a2,a3,b0,b1,b2,b3) \
48 b0 = vec_mergeh( a0, a0 ); \
49 b1 = vec_mergeh( a1, a0 ); \
50 b2 = vec_mergeh( a2, a0 ); \
51 b3 = vec_mergeh( a3, a0 ); \
52 a0 = vec_mergeh( b0, b2 ); \
53 a1 = vec_mergel( b0, b2 ); \
54 a2 = vec_mergeh( b1, b3 ); \
55 a3 = vec_mergel( b1, b3 ); \
56 b0 = vec_mergeh( a0, a2 ); \
57 b1 = vec_mergel( a0, a2 ); \
58 b2 = vec_mergeh( a1, a3 ); \
59 b3 = vec_mergel( a1, a3 )
61 #define VEC_LOAD_U8_ADD_S16_STORE_U8(va) \
62 vdst_orig = vec_ld(0, dst); \
63 vdst = vec_perm(vdst_orig, zero_u8v, vdst_mask); \
64 vdst_ss = (vec_s16) vec_mergeh(zero_u8v, vdst); \
65 va = vec_add(va, vdst_ss); \
66 va_u8 = vec_packsu(va, zero_s16v); \
67 va_u32 = vec_splat((vec_u32)va_u8, 0); \
68 vec_ste(va_u32, element, (uint32_t*)dst);
70 static void ff_h264_idct_add_altivec(uint8_t *dst, int16_t *block, int stride)
72 vec_s16 va0, va1, va2, va3;
73 vec_s16 vz0, vz1, vz2, vz3;
74 vec_s16 vtmp0, vtmp1, vtmp2, vtmp3;
75 vec_u8 va_u8;
76 vec_u32 va_u32;
77 vec_s16 vdst_ss;
78 const vec_u16 v6us = vec_splat_u16(6);
79 vec_u8 vdst, vdst_orig;
80 vec_u8 vdst_mask = vec_lvsl(0, dst);
81 int element = ((unsigned long)dst & 0xf) >> 2;
82 LOAD_ZERO;
84 block[0] += 32; /* add 32 as a DC-level for rounding */
86 vtmp0 = vec_ld(0,block);
87 vtmp1 = vec_sld(vtmp0, vtmp0, 8);
88 vtmp2 = vec_ld(16,block);
89 vtmp3 = vec_sld(vtmp2, vtmp2, 8);
91 VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
92 VEC_TRANSPOSE_4(va0,va1,va2,va3,vtmp0,vtmp1,vtmp2,vtmp3);
93 VEC_1D_DCT(vtmp0,vtmp1,vtmp2,vtmp3,va0,va1,va2,va3);
95 va0 = vec_sra(va0,v6us);
96 va1 = vec_sra(va1,v6us);
97 va2 = vec_sra(va2,v6us);
98 va3 = vec_sra(va3,v6us);
100 VEC_LOAD_U8_ADD_S16_STORE_U8(va0);
101 dst += stride;
102 VEC_LOAD_U8_ADD_S16_STORE_U8(va1);
103 dst += stride;
104 VEC_LOAD_U8_ADD_S16_STORE_U8(va2);
105 dst += stride;
106 VEC_LOAD_U8_ADD_S16_STORE_U8(va3);
109 #define IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7, d0, d1, d2, d3, d4, d5, d6, d7) {\
110 /* a0 = SRC(0) + SRC(4); */ \
111 vec_s16 a0v = vec_add(s0, s4); \
112 /* a2 = SRC(0) - SRC(4); */ \
113 vec_s16 a2v = vec_sub(s0, s4); \
114 /* a4 = (SRC(2)>>1) - SRC(6); */ \
115 vec_s16 a4v = vec_sub(vec_sra(s2, onev), s6); \
116 /* a6 = (SRC(6)>>1) + SRC(2); */ \
117 vec_s16 a6v = vec_add(vec_sra(s6, onev), s2); \
118 /* b0 = a0 + a6; */ \
119 vec_s16 b0v = vec_add(a0v, a6v); \
120 /* b2 = a2 + a4; */ \
121 vec_s16 b2v = vec_add(a2v, a4v); \
122 /* b4 = a2 - a4; */ \
123 vec_s16 b4v = vec_sub(a2v, a4v); \
124 /* b6 = a0 - a6; */ \
125 vec_s16 b6v = vec_sub(a0v, a6v); \
126 /* a1 = SRC(5) - SRC(3) - SRC(7) - (SRC(7)>>1); */ \
127 /* a1 = (SRC(5)-SRC(3)) - (SRC(7) + (SRC(7)>>1)); */ \
128 vec_s16 a1v = vec_sub( vec_sub(s5, s3), vec_add(s7, vec_sra(s7, onev)) ); \
129 /* a3 = SRC(7) + SRC(1) - SRC(3) - (SRC(3)>>1); */ \
130 /* a3 = (SRC(7)+SRC(1)) - (SRC(3) + (SRC(3)>>1)); */ \
131 vec_s16 a3v = vec_sub( vec_add(s7, s1), vec_add(s3, vec_sra(s3, onev)) );\
132 /* a5 = SRC(7) - SRC(1) + SRC(5) + (SRC(5)>>1); */ \
133 /* a5 = (SRC(7)-SRC(1)) + SRC(5) + (SRC(5)>>1); */ \
134 vec_s16 a5v = vec_add( vec_sub(s7, s1), vec_add(s5, vec_sra(s5, onev)) );\
135 /* a7 = SRC(5)+SRC(3) + SRC(1) + (SRC(1)>>1); */ \
136 vec_s16 a7v = vec_add( vec_add(s5, s3), vec_add(s1, vec_sra(s1, onev)) );\
137 /* b1 = (a7>>2) + a1; */ \
138 vec_s16 b1v = vec_add( vec_sra(a7v, twov), a1v); \
139 /* b3 = a3 + (a5>>2); */ \
140 vec_s16 b3v = vec_add(a3v, vec_sra(a5v, twov)); \
141 /* b5 = (a3>>2) - a5; */ \
142 vec_s16 b5v = vec_sub( vec_sra(a3v, twov), a5v); \
143 /* b7 = a7 - (a1>>2); */ \
144 vec_s16 b7v = vec_sub( a7v, vec_sra(a1v, twov)); \
145 /* DST(0, b0 + b7); */ \
146 d0 = vec_add(b0v, b7v); \
147 /* DST(1, b2 + b5); */ \
148 d1 = vec_add(b2v, b5v); \
149 /* DST(2, b4 + b3); */ \
150 d2 = vec_add(b4v, b3v); \
151 /* DST(3, b6 + b1); */ \
152 d3 = vec_add(b6v, b1v); \
153 /* DST(4, b6 - b1); */ \
154 d4 = vec_sub(b6v, b1v); \
155 /* DST(5, b4 - b3); */ \
156 d5 = vec_sub(b4v, b3v); \
157 /* DST(6, b2 - b5); */ \
158 d6 = vec_sub(b2v, b5v); \
159 /* DST(7, b0 - b7); */ \
160 d7 = vec_sub(b0v, b7v); \
163 #define ALTIVEC_STORE_SUM_CLIP(dest, idctv, perm_ldv, perm_stv, sel) { \
164 /* unaligned load */ \
165 vec_u8 hv = vec_ld( 0, dest ); \
166 vec_u8 lv = vec_ld( 7, dest ); \
167 vec_u8 dstv = vec_perm( hv, lv, (vec_u8)perm_ldv ); \
168 vec_s16 idct_sh6 = vec_sra(idctv, sixv); \
169 vec_u16 dst16 = (vec_u16)vec_mergeh(zero_u8v, dstv); \
170 vec_s16 idstsum = vec_adds(idct_sh6, (vec_s16)dst16); \
171 vec_u8 idstsum8 = vec_packsu(zero_s16v, idstsum); \
172 vec_u8 edgehv; \
173 /* unaligned store */ \
174 vec_u8 bodyv = vec_perm( idstsum8, idstsum8, perm_stv );\
175 vec_u8 edgelv = vec_perm( sel, zero_u8v, perm_stv ); \
176 lv = vec_sel( lv, bodyv, edgelv ); \
177 vec_st( lv, 7, dest ); \
178 hv = vec_ld( 0, dest ); \
179 edgehv = vec_perm( zero_u8v, sel, perm_stv ); \
180 hv = vec_sel( hv, bodyv, edgehv ); \
181 vec_st( hv, 0, dest ); \
184 static void ff_h264_idct8_add_altivec( uint8_t *dst, int16_t *dct, int stride ) {
185 vec_s16 s0, s1, s2, s3, s4, s5, s6, s7;
186 vec_s16 d0, d1, d2, d3, d4, d5, d6, d7;
187 vec_s16 idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7;
189 vec_u8 perm_ldv = vec_lvsl(0, dst);
190 vec_u8 perm_stv = vec_lvsr(8, dst);
192 const vec_u16 onev = vec_splat_u16(1);
193 const vec_u16 twov = vec_splat_u16(2);
194 const vec_u16 sixv = vec_splat_u16(6);
196 const vec_u8 sel = (vec_u8) {0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1};
197 LOAD_ZERO;
199 dct[0] += 32; // rounding for the >>6 at the end
201 s0 = vec_ld(0x00, (int16_t*)dct);
202 s1 = vec_ld(0x10, (int16_t*)dct);
203 s2 = vec_ld(0x20, (int16_t*)dct);
204 s3 = vec_ld(0x30, (int16_t*)dct);
205 s4 = vec_ld(0x40, (int16_t*)dct);
206 s5 = vec_ld(0x50, (int16_t*)dct);
207 s6 = vec_ld(0x60, (int16_t*)dct);
208 s7 = vec_ld(0x70, (int16_t*)dct);
210 IDCT8_1D_ALTIVEC(s0, s1, s2, s3, s4, s5, s6, s7,
211 d0, d1, d2, d3, d4, d5, d6, d7);
213 TRANSPOSE8( d0, d1, d2, d3, d4, d5, d6, d7 );
215 IDCT8_1D_ALTIVEC(d0, d1, d2, d3, d4, d5, d6, d7,
216 idct0, idct1, idct2, idct3, idct4, idct5, idct6, idct7);
218 ALTIVEC_STORE_SUM_CLIP(&dst[0*stride], idct0, perm_ldv, perm_stv, sel);
219 ALTIVEC_STORE_SUM_CLIP(&dst[1*stride], idct1, perm_ldv, perm_stv, sel);
220 ALTIVEC_STORE_SUM_CLIP(&dst[2*stride], idct2, perm_ldv, perm_stv, sel);
221 ALTIVEC_STORE_SUM_CLIP(&dst[3*stride], idct3, perm_ldv, perm_stv, sel);
222 ALTIVEC_STORE_SUM_CLIP(&dst[4*stride], idct4, perm_ldv, perm_stv, sel);
223 ALTIVEC_STORE_SUM_CLIP(&dst[5*stride], idct5, perm_ldv, perm_stv, sel);
224 ALTIVEC_STORE_SUM_CLIP(&dst[6*stride], idct6, perm_ldv, perm_stv, sel);
225 ALTIVEC_STORE_SUM_CLIP(&dst[7*stride], idct7, perm_ldv, perm_stv, sel);
228 static av_always_inline void h264_idct_dc_add_internal(uint8_t *dst, int16_t *block, int stride, int size)
230 vec_s16 dc16;
231 vec_u8 dcplus, dcminus, v0, v1, v2, v3, aligner;
232 LOAD_ZERO;
233 DECLARE_ALIGNED(16, int, dc);
234 int i;
236 dc = (block[0] + 32) >> 6;
237 dc16 = vec_splat((vec_s16) vec_lde(0, &dc), 1);
239 if (size == 4)
240 dc16 = vec_sld(dc16, zero_s16v, 8);
241 dcplus = vec_packsu(dc16, zero_s16v);
242 dcminus = vec_packsu(vec_sub(zero_s16v, dc16), zero_s16v);
244 aligner = vec_lvsr(0, dst);
245 dcplus = vec_perm(dcplus, dcplus, aligner);
246 dcminus = vec_perm(dcminus, dcminus, aligner);
248 for (i = 0; i < size; i += 4) {
249 v0 = vec_ld(0, dst+0*stride);
250 v1 = vec_ld(0, dst+1*stride);
251 v2 = vec_ld(0, dst+2*stride);
252 v3 = vec_ld(0, dst+3*stride);
254 v0 = vec_adds(v0, dcplus);
255 v1 = vec_adds(v1, dcplus);
256 v2 = vec_adds(v2, dcplus);
257 v3 = vec_adds(v3, dcplus);
259 v0 = vec_subs(v0, dcminus);
260 v1 = vec_subs(v1, dcminus);
261 v2 = vec_subs(v2, dcminus);
262 v3 = vec_subs(v3, dcminus);
264 vec_st(v0, 0, dst+0*stride);
265 vec_st(v1, 0, dst+1*stride);
266 vec_st(v2, 0, dst+2*stride);
267 vec_st(v3, 0, dst+3*stride);
269 dst += 4*stride;
273 static void h264_idct_dc_add_altivec(uint8_t *dst, int16_t *block, int stride)
275 h264_idct_dc_add_internal(dst, block, stride, 4);
278 static void ff_h264_idct8_dc_add_altivec(uint8_t *dst, int16_t *block, int stride)
280 h264_idct_dc_add_internal(dst, block, stride, 8);
283 static void ff_h264_idct_add16_altivec(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
284 int i;
285 for(i=0; i<16; i++){
286 int nnz = nnzc[ scan8[i] ];
287 if(nnz){
288 if(nnz==1 && block[i*16]) h264_idct_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
289 else ff_h264_idct_add_altivec(dst + block_offset[i], block + i*16, stride);
294 static void ff_h264_idct_add16intra_altivec(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
295 int i;
296 for(i=0; i<16; i++){
297 if(nnzc[ scan8[i] ]) ff_h264_idct_add_altivec(dst + block_offset[i], block + i*16, stride);
298 else if(block[i*16]) h264_idct_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
302 static void ff_h264_idct8_add4_altivec(uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
303 int i;
304 for(i=0; i<16; i+=4){
305 int nnz = nnzc[ scan8[i] ];
306 if(nnz){
307 if(nnz==1 && block[i*16]) ff_h264_idct8_dc_add_altivec(dst + block_offset[i], block + i*16, stride);
308 else ff_h264_idct8_add_altivec (dst + block_offset[i], block + i*16, stride);
313 static void ff_h264_idct_add8_altivec(uint8_t **dest, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]){
314 int i, j;
315 for (j = 1; j < 3; j++) {
316 for(i = j * 16; i < j * 16 + 4; i++){
317 if(nnzc[ scan8[i] ])
318 ff_h264_idct_add_altivec(dest[j-1] + block_offset[i], block + i*16, stride);
319 else if(block[i*16])
320 h264_idct_dc_add_altivec(dest[j-1] + block_offset[i], block + i*16, stride);
325 #define transpose4x16(r0, r1, r2, r3) { \
326 register vec_u8 r4; \
327 register vec_u8 r5; \
328 register vec_u8 r6; \
329 register vec_u8 r7; \
331 r4 = vec_mergeh(r0, r2); /*0, 2 set 0*/ \
332 r5 = vec_mergel(r0, r2); /*0, 2 set 1*/ \
333 r6 = vec_mergeh(r1, r3); /*1, 3 set 0*/ \
334 r7 = vec_mergel(r1, r3); /*1, 3 set 1*/ \
336 r0 = vec_mergeh(r4, r6); /*all set 0*/ \
337 r1 = vec_mergel(r4, r6); /*all set 1*/ \
338 r2 = vec_mergeh(r5, r7); /*all set 2*/ \
339 r3 = vec_mergel(r5, r7); /*all set 3*/ \
342 static inline void write16x4(uint8_t *dst, int dst_stride,
343 register vec_u8 r0, register vec_u8 r1,
344 register vec_u8 r2, register vec_u8 r3) {
345 DECLARE_ALIGNED(16, unsigned char, result)[64];
346 uint32_t *src_int = (uint32_t *)result, *dst_int = (uint32_t *)dst;
347 int int_dst_stride = dst_stride/4;
349 vec_st(r0, 0, result);
350 vec_st(r1, 16, result);
351 vec_st(r2, 32, result);
352 vec_st(r3, 48, result);
353 /* FIXME: there has to be a better way!!!! */
354 *dst_int = *src_int;
355 *(dst_int+ int_dst_stride) = *(src_int + 1);
356 *(dst_int+ 2*int_dst_stride) = *(src_int + 2);
357 *(dst_int+ 3*int_dst_stride) = *(src_int + 3);
358 *(dst_int+ 4*int_dst_stride) = *(src_int + 4);
359 *(dst_int+ 5*int_dst_stride) = *(src_int + 5);
360 *(dst_int+ 6*int_dst_stride) = *(src_int + 6);
361 *(dst_int+ 7*int_dst_stride) = *(src_int + 7);
362 *(dst_int+ 8*int_dst_stride) = *(src_int + 8);
363 *(dst_int+ 9*int_dst_stride) = *(src_int + 9);
364 *(dst_int+10*int_dst_stride) = *(src_int + 10);
365 *(dst_int+11*int_dst_stride) = *(src_int + 11);
366 *(dst_int+12*int_dst_stride) = *(src_int + 12);
367 *(dst_int+13*int_dst_stride) = *(src_int + 13);
368 *(dst_int+14*int_dst_stride) = *(src_int + 14);
369 *(dst_int+15*int_dst_stride) = *(src_int + 15);
372 /** @brief performs a 6x16 transpose of data in src, and stores it to dst
373 @todo FIXME: see if we can't spare some vec_lvsl() by them factorizing
374 out of unaligned_load() */
375 #define readAndTranspose16x6(src, src_stride, r8, r9, r10, r11, r12, r13) {\
376 register vec_u8 r0 = unaligned_load(0, src); \
377 register vec_u8 r1 = unaligned_load( src_stride, src); \
378 register vec_u8 r2 = unaligned_load(2* src_stride, src); \
379 register vec_u8 r3 = unaligned_load(3* src_stride, src); \
380 register vec_u8 r4 = unaligned_load(4* src_stride, src); \
381 register vec_u8 r5 = unaligned_load(5* src_stride, src); \
382 register vec_u8 r6 = unaligned_load(6* src_stride, src); \
383 register vec_u8 r7 = unaligned_load(7* src_stride, src); \
384 register vec_u8 r14 = unaligned_load(14*src_stride, src); \
385 register vec_u8 r15 = unaligned_load(15*src_stride, src); \
387 r8 = unaligned_load( 8*src_stride, src); \
388 r9 = unaligned_load( 9*src_stride, src); \
389 r10 = unaligned_load(10*src_stride, src); \
390 r11 = unaligned_load(11*src_stride, src); \
391 r12 = unaligned_load(12*src_stride, src); \
392 r13 = unaligned_load(13*src_stride, src); \
394 /*Merge first pairs*/ \
395 r0 = vec_mergeh(r0, r8); /*0, 8*/ \
396 r1 = vec_mergeh(r1, r9); /*1, 9*/ \
397 r2 = vec_mergeh(r2, r10); /*2,10*/ \
398 r3 = vec_mergeh(r3, r11); /*3,11*/ \
399 r4 = vec_mergeh(r4, r12); /*4,12*/ \
400 r5 = vec_mergeh(r5, r13); /*5,13*/ \
401 r6 = vec_mergeh(r6, r14); /*6,14*/ \
402 r7 = vec_mergeh(r7, r15); /*7,15*/ \
404 /*Merge second pairs*/ \
405 r8 = vec_mergeh(r0, r4); /*0,4, 8,12 set 0*/ \
406 r9 = vec_mergel(r0, r4); /*0,4, 8,12 set 1*/ \
407 r10 = vec_mergeh(r1, r5); /*1,5, 9,13 set 0*/ \
408 r11 = vec_mergel(r1, r5); /*1,5, 9,13 set 1*/ \
409 r12 = vec_mergeh(r2, r6); /*2,6,10,14 set 0*/ \
410 r13 = vec_mergel(r2, r6); /*2,6,10,14 set 1*/ \
411 r14 = vec_mergeh(r3, r7); /*3,7,11,15 set 0*/ \
412 r15 = vec_mergel(r3, r7); /*3,7,11,15 set 1*/ \
414 /*Third merge*/ \
415 r0 = vec_mergeh(r8, r12); /*0,2,4,6,8,10,12,14 set 0*/ \
416 r1 = vec_mergel(r8, r12); /*0,2,4,6,8,10,12,14 set 1*/ \
417 r2 = vec_mergeh(r9, r13); /*0,2,4,6,8,10,12,14 set 2*/ \
418 r4 = vec_mergeh(r10, r14); /*1,3,5,7,9,11,13,15 set 0*/ \
419 r5 = vec_mergel(r10, r14); /*1,3,5,7,9,11,13,15 set 1*/ \
420 r6 = vec_mergeh(r11, r15); /*1,3,5,7,9,11,13,15 set 2*/ \
421 /* Don't need to compute 3 and 7*/ \
423 /*Final merge*/ \
424 r8 = vec_mergeh(r0, r4); /*all set 0*/ \
425 r9 = vec_mergel(r0, r4); /*all set 1*/ \
426 r10 = vec_mergeh(r1, r5); /*all set 2*/ \
427 r11 = vec_mergel(r1, r5); /*all set 3*/ \
428 r12 = vec_mergeh(r2, r6); /*all set 4*/ \
429 r13 = vec_mergel(r2, r6); /*all set 5*/ \
430 /* Don't need to compute 14 and 15*/ \
434 // out: o = |x-y| < a
435 static inline vec_u8 diff_lt_altivec ( register vec_u8 x,
436 register vec_u8 y,
437 register vec_u8 a) {
439 register vec_u8 diff = vec_subs(x, y);
440 register vec_u8 diffneg = vec_subs(y, x);
441 register vec_u8 o = vec_or(diff, diffneg); /* |x-y| */
442 o = (vec_u8)vec_cmplt(o, a);
443 return o;
446 static inline vec_u8 h264_deblock_mask ( register vec_u8 p0,
447 register vec_u8 p1,
448 register vec_u8 q0,
449 register vec_u8 q1,
450 register vec_u8 alpha,
451 register vec_u8 beta) {
453 register vec_u8 mask;
454 register vec_u8 tempmask;
456 mask = diff_lt_altivec(p0, q0, alpha);
457 tempmask = diff_lt_altivec(p1, p0, beta);
458 mask = vec_and(mask, tempmask);
459 tempmask = diff_lt_altivec(q1, q0, beta);
460 mask = vec_and(mask, tempmask);
462 return mask;
465 // out: newp1 = clip((p2 + ((p0 + q0 + 1) >> 1)) >> 1, p1-tc0, p1+tc0)
466 static inline vec_u8 h264_deblock_q1(register vec_u8 p0,
467 register vec_u8 p1,
468 register vec_u8 p2,
469 register vec_u8 q0,
470 register vec_u8 tc0) {
472 register vec_u8 average = vec_avg(p0, q0);
473 register vec_u8 temp;
474 register vec_u8 uncliped;
475 register vec_u8 ones;
476 register vec_u8 max;
477 register vec_u8 min;
478 register vec_u8 newp1;
480 temp = vec_xor(average, p2);
481 average = vec_avg(average, p2); /*avg(p2, avg(p0, q0)) */
482 ones = vec_splat_u8(1);
483 temp = vec_and(temp, ones); /*(p2^avg(p0, q0)) & 1 */
484 uncliped = vec_subs(average, temp); /*(p2+((p0+q0+1)>>1))>>1 */
485 max = vec_adds(p1, tc0);
486 min = vec_subs(p1, tc0);
487 newp1 = vec_max(min, uncliped);
488 newp1 = vec_min(max, newp1);
489 return newp1;
492 #define h264_deblock_p0_q0(p0, p1, q0, q1, tc0masked) { \
494 const vec_u8 A0v = vec_sl(vec_splat_u8(10), vec_splat_u8(4)); \
496 register vec_u8 pq0bit = vec_xor(p0,q0); \
497 register vec_u8 q1minus; \
498 register vec_u8 p0minus; \
499 register vec_u8 stage1; \
500 register vec_u8 stage2; \
501 register vec_u8 vec160; \
502 register vec_u8 delta; \
503 register vec_u8 deltaneg; \
505 q1minus = vec_nor(q1, q1); /* 255 - q1 */ \
506 stage1 = vec_avg(p1, q1minus); /* (p1 - q1 + 256)>>1 */ \
507 stage2 = vec_sr(stage1, vec_splat_u8(1)); /* (p1 - q1 + 256)>>2 = 64 + (p1 - q1) >> 2 */ \
508 p0minus = vec_nor(p0, p0); /* 255 - p0 */ \
509 stage1 = vec_avg(q0, p0minus); /* (q0 - p0 + 256)>>1 */ \
510 pq0bit = vec_and(pq0bit, vec_splat_u8(1)); \
511 stage2 = vec_avg(stage2, pq0bit); /* 32 + ((q0 - p0)&1 + (p1 - q1) >> 2 + 1) >> 1 */ \
512 stage2 = vec_adds(stage2, stage1); /* 160 + ((p0 - q0) + (p1 - q1) >> 2 + 1) >> 1 */ \
513 vec160 = vec_ld(0, &A0v); \
514 deltaneg = vec_subs(vec160, stage2); /* -d */ \
515 delta = vec_subs(stage2, vec160); /* d */ \
516 deltaneg = vec_min(tc0masked, deltaneg); \
517 delta = vec_min(tc0masked, delta); \
518 p0 = vec_subs(p0, deltaneg); \
519 q0 = vec_subs(q0, delta); \
520 p0 = vec_adds(p0, delta); \
521 q0 = vec_adds(q0, deltaneg); \
524 #define h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0) { \
525 DECLARE_ALIGNED(16, unsigned char, temp)[16]; \
526 register vec_u8 alphavec; \
527 register vec_u8 betavec; \
528 register vec_u8 mask; \
529 register vec_u8 p1mask; \
530 register vec_u8 q1mask; \
531 register vector signed char tc0vec; \
532 register vec_u8 finaltc0; \
533 register vec_u8 tc0masked; \
534 register vec_u8 newp1; \
535 register vec_u8 newq1; \
537 temp[0] = alpha; \
538 temp[1] = beta; \
539 alphavec = vec_ld(0, temp); \
540 betavec = vec_splat(alphavec, 0x1); \
541 alphavec = vec_splat(alphavec, 0x0); \
542 mask = h264_deblock_mask(p0, p1, q0, q1, alphavec, betavec); /*if in block */ \
544 AV_COPY32(temp, tc0); \
545 tc0vec = vec_ld(0, (signed char*)temp); \
546 tc0vec = vec_mergeh(tc0vec, tc0vec); \
547 tc0vec = vec_mergeh(tc0vec, tc0vec); \
548 mask = vec_and(mask, vec_cmpgt(tc0vec, vec_splat_s8(-1))); /* if tc0[i] >= 0 */ \
549 finaltc0 = vec_and((vec_u8)tc0vec, mask); /* tc = tc0 */ \
551 p1mask = diff_lt_altivec(p2, p0, betavec); \
552 p1mask = vec_and(p1mask, mask); /* if ( |p2 - p0| < beta) */ \
553 tc0masked = vec_and(p1mask, (vec_u8)tc0vec); \
554 finaltc0 = vec_sub(finaltc0, p1mask); /* tc++ */ \
555 newp1 = h264_deblock_q1(p0, p1, p2, q0, tc0masked); \
556 /*end if*/ \
558 q1mask = diff_lt_altivec(q2, q0, betavec); \
559 q1mask = vec_and(q1mask, mask); /* if ( |q2 - q0| < beta ) */\
560 tc0masked = vec_and(q1mask, (vec_u8)tc0vec); \
561 finaltc0 = vec_sub(finaltc0, q1mask); /* tc++ */ \
562 newq1 = h264_deblock_q1(p0, q1, q2, q0, tc0masked); \
563 /*end if*/ \
565 h264_deblock_p0_q0(p0, p1, q0, q1, finaltc0); \
566 p1 = newp1; \
567 q1 = newq1; \
570 static void h264_v_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
572 if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) >= 0) {
573 register vec_u8 p2 = vec_ld(-3*stride, pix);
574 register vec_u8 p1 = vec_ld(-2*stride, pix);
575 register vec_u8 p0 = vec_ld(-1*stride, pix);
576 register vec_u8 q0 = vec_ld(0, pix);
577 register vec_u8 q1 = vec_ld(stride, pix);
578 register vec_u8 q2 = vec_ld(2*stride, pix);
579 h264_loop_filter_luma_altivec(p2, p1, p0, q0, q1, q2, alpha, beta, tc0);
580 vec_st(p1, -2*stride, pix);
581 vec_st(p0, -1*stride, pix);
582 vec_st(q0, 0, pix);
583 vec_st(q1, stride, pix);
587 static void h264_h_loop_filter_luma_altivec(uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0) {
589 register vec_u8 line0, line1, line2, line3, line4, line5;
590 if ((tc0[0] & tc0[1] & tc0[2] & tc0[3]) < 0)
591 return;
592 readAndTranspose16x6(pix-3, stride, line0, line1, line2, line3, line4, line5);
593 h264_loop_filter_luma_altivec(line0, line1, line2, line3, line4, line5, alpha, beta, tc0);
594 transpose4x16(line1, line2, line3, line4);
595 write16x4(pix-2, stride, line1, line2, line3, line4);
598 static av_always_inline
599 void weight_h264_W_altivec(uint8_t *block, int stride, int height,
600 int log2_denom, int weight, int offset, int w)
602 int y, aligned;
603 vec_u8 vblock;
604 vec_s16 vtemp, vweight, voffset, v0, v1;
605 vec_u16 vlog2_denom;
606 DECLARE_ALIGNED(16, int32_t, temp)[4];
607 LOAD_ZERO;
609 offset <<= log2_denom;
610 if(log2_denom) offset += 1<<(log2_denom-1);
611 temp[0] = log2_denom;
612 temp[1] = weight;
613 temp[2] = offset;
615 vtemp = (vec_s16)vec_ld(0, temp);
616 vlog2_denom = (vec_u16)vec_splat(vtemp, 1);
617 vweight = vec_splat(vtemp, 3);
618 voffset = vec_splat(vtemp, 5);
619 aligned = !((unsigned long)block & 0xf);
621 for (y = 0; y < height; y++) {
622 vblock = vec_ld(0, block);
624 v0 = (vec_s16)vec_mergeh(zero_u8v, vblock);
625 v1 = (vec_s16)vec_mergel(zero_u8v, vblock);
627 if (w == 16 || aligned) {
628 v0 = vec_mladd(v0, vweight, zero_s16v);
629 v0 = vec_adds(v0, voffset);
630 v0 = vec_sra(v0, vlog2_denom);
632 if (w == 16 || !aligned) {
633 v1 = vec_mladd(v1, vweight, zero_s16v);
634 v1 = vec_adds(v1, voffset);
635 v1 = vec_sra(v1, vlog2_denom);
637 vblock = vec_packsu(v0, v1);
638 vec_st(vblock, 0, block);
640 block += stride;
644 static av_always_inline
645 void biweight_h264_W_altivec(uint8_t *dst, uint8_t *src, int stride, int height,
646 int log2_denom, int weightd, int weights, int offset, int w)
648 int y, dst_aligned, src_aligned;
649 vec_u8 vsrc, vdst;
650 vec_s16 vtemp, vweights, vweightd, voffset, v0, v1, v2, v3;
651 vec_u16 vlog2_denom;
652 DECLARE_ALIGNED(16, int32_t, temp)[4];
653 LOAD_ZERO;
655 offset = ((offset + 1) | 1) << log2_denom;
656 temp[0] = log2_denom+1;
657 temp[1] = weights;
658 temp[2] = weightd;
659 temp[3] = offset;
661 vtemp = (vec_s16)vec_ld(0, temp);
662 vlog2_denom = (vec_u16)vec_splat(vtemp, 1);
663 vweights = vec_splat(vtemp, 3);
664 vweightd = vec_splat(vtemp, 5);
665 voffset = vec_splat(vtemp, 7);
666 dst_aligned = !((unsigned long)dst & 0xf);
667 src_aligned = !((unsigned long)src & 0xf);
669 for (y = 0; y < height; y++) {
670 vdst = vec_ld(0, dst);
671 vsrc = vec_ld(0, src);
673 v0 = (vec_s16)vec_mergeh(zero_u8v, vdst);
674 v1 = (vec_s16)vec_mergel(zero_u8v, vdst);
675 v2 = (vec_s16)vec_mergeh(zero_u8v, vsrc);
676 v3 = (vec_s16)vec_mergel(zero_u8v, vsrc);
678 if (w == 8) {
679 if (src_aligned)
680 v3 = v2;
681 else
682 v2 = v3;
685 if (w == 16 || dst_aligned) {
686 v0 = vec_mladd(v0, vweightd, zero_s16v);
687 v2 = vec_mladd(v2, vweights, zero_s16v);
689 v0 = vec_adds(v0, voffset);
690 v0 = vec_adds(v0, v2);
691 v0 = vec_sra(v0, vlog2_denom);
693 if (w == 16 || !dst_aligned) {
694 v1 = vec_mladd(v1, vweightd, zero_s16v);
695 v3 = vec_mladd(v3, vweights, zero_s16v);
697 v1 = vec_adds(v1, voffset);
698 v1 = vec_adds(v1, v3);
699 v1 = vec_sra(v1, vlog2_denom);
701 vdst = vec_packsu(v0, v1);
702 vec_st(vdst, 0, dst);
704 dst += stride;
705 src += stride;
709 #define H264_WEIGHT(W) \
710 static void ff_weight_h264_pixels ## W ## _altivec(uint8_t *block, int stride, int height, \
711 int log2_denom, int weight, int offset){ \
712 weight_h264_W_altivec(block, stride, height, log2_denom, weight, offset, W); \
714 static void ff_biweight_h264_pixels ## W ## _altivec(uint8_t *dst, uint8_t *src, int stride, int height, \
715 int log2_denom, int weightd, int weights, int offset){ \
716 biweight_h264_W_altivec(dst, src, stride, height, log2_denom, weightd, weights, offset, W); \
719 H264_WEIGHT(16)
720 H264_WEIGHT( 8)
722 av_cold void ff_h264dsp_init_ppc(H264DSPContext *c, const int bit_depth,
723 const int chroma_format_idc)
725 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
726 if (bit_depth == 8) {
727 c->h264_idct_add = ff_h264_idct_add_altivec;
728 if (chroma_format_idc == 1)
729 c->h264_idct_add8 = ff_h264_idct_add8_altivec;
730 c->h264_idct_add16 = ff_h264_idct_add16_altivec;
731 c->h264_idct_add16intra = ff_h264_idct_add16intra_altivec;
732 c->h264_idct_dc_add= h264_idct_dc_add_altivec;
733 c->h264_idct8_dc_add = ff_h264_idct8_dc_add_altivec;
734 c->h264_idct8_add = ff_h264_idct8_add_altivec;
735 c->h264_idct8_add4 = ff_h264_idct8_add4_altivec;
736 c->h264_v_loop_filter_luma= h264_v_loop_filter_luma_altivec;
737 c->h264_h_loop_filter_luma= h264_h_loop_filter_luma_altivec;
739 c->weight_h264_pixels_tab[0] = ff_weight_h264_pixels16_altivec;
740 c->weight_h264_pixels_tab[1] = ff_weight_h264_pixels8_altivec;
741 c->biweight_h264_pixels_tab[0] = ff_biweight_h264_pixels16_altivec;
742 c->biweight_h264_pixels_tab[1] = ff_biweight_h264_pixels8_altivec;