]> git.sesse.net Git - ffmpeg/blob - libavcodec/ppc/h264_qpel.c
Merge commit '685e6f2e3939f124b41c7801cc541dad8252af3d'
[ffmpeg] / libavcodec / ppc / h264_qpel.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 "config.h"
22 #include "libavutil/attributes.h"
23 #include "libavcodec/h264qpel.h"
24
25 #if HAVE_ALTIVEC
26 #include "libavutil/cpu.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/ppc/types_altivec.h"
29 #include "libavutil/ppc/util_altivec.h"
30 #include "dsputil_altivec.h"
31
32 #define PUT_OP_U8_ALTIVEC(d, s, dst) d = s
33 #define AVG_OP_U8_ALTIVEC(d, s, dst) d = vec_avg(dst, s)
34
35 #define OP_U8_ALTIVEC                          PUT_OP_U8_ALTIVEC
36 #define PREFIX_h264_chroma_mc8_altivec         put_h264_chroma_mc8_altivec
37 #define PREFIX_h264_chroma_mc8_num             altivec_put_h264_chroma_mc8_num
38 #define PREFIX_h264_qpel16_h_lowpass_altivec   put_h264_qpel16_h_lowpass_altivec
39 #define PREFIX_h264_qpel16_h_lowpass_num       altivec_put_h264_qpel16_h_lowpass_num
40 #define PREFIX_h264_qpel16_v_lowpass_altivec   put_h264_qpel16_v_lowpass_altivec
41 #define PREFIX_h264_qpel16_v_lowpass_num       altivec_put_h264_qpel16_v_lowpass_num
42 #define PREFIX_h264_qpel16_hv_lowpass_altivec  put_h264_qpel16_hv_lowpass_altivec
43 #define PREFIX_h264_qpel16_hv_lowpass_num      altivec_put_h264_qpel16_hv_lowpass_num
44 #include "h264_qpel_template.c"
45 #undef OP_U8_ALTIVEC
46 #undef PREFIX_h264_chroma_mc8_altivec
47 #undef PREFIX_h264_chroma_mc8_num
48 #undef PREFIX_h264_qpel16_h_lowpass_altivec
49 #undef PREFIX_h264_qpel16_h_lowpass_num
50 #undef PREFIX_h264_qpel16_v_lowpass_altivec
51 #undef PREFIX_h264_qpel16_v_lowpass_num
52 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
53 #undef PREFIX_h264_qpel16_hv_lowpass_num
54
55 #define OP_U8_ALTIVEC                          AVG_OP_U8_ALTIVEC
56 #define PREFIX_h264_chroma_mc8_altivec         avg_h264_chroma_mc8_altivec
57 #define PREFIX_h264_chroma_mc8_num             altivec_avg_h264_chroma_mc8_num
58 #define PREFIX_h264_qpel16_h_lowpass_altivec   avg_h264_qpel16_h_lowpass_altivec
59 #define PREFIX_h264_qpel16_h_lowpass_num       altivec_avg_h264_qpel16_h_lowpass_num
60 #define PREFIX_h264_qpel16_v_lowpass_altivec   avg_h264_qpel16_v_lowpass_altivec
61 #define PREFIX_h264_qpel16_v_lowpass_num       altivec_avg_h264_qpel16_v_lowpass_num
62 #define PREFIX_h264_qpel16_hv_lowpass_altivec  avg_h264_qpel16_hv_lowpass_altivec
63 #define PREFIX_h264_qpel16_hv_lowpass_num      altivec_avg_h264_qpel16_hv_lowpass_num
64 #include "h264_qpel_template.c"
65 #undef OP_U8_ALTIVEC
66 #undef PREFIX_h264_chroma_mc8_altivec
67 #undef PREFIX_h264_chroma_mc8_num
68 #undef PREFIX_h264_qpel16_h_lowpass_altivec
69 #undef PREFIX_h264_qpel16_h_lowpass_num
70 #undef PREFIX_h264_qpel16_v_lowpass_altivec
71 #undef PREFIX_h264_qpel16_v_lowpass_num
72 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
73 #undef PREFIX_h264_qpel16_hv_lowpass_num
74
75 #define H264_MC(OPNAME, SIZE, CODETYPE) \
76 static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, int stride){\
77     ff_ ## OPNAME ## pixels ## SIZE ## _ ## CODETYPE(dst, src, stride, SIZE);\
78 }\
79 \
80 static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){ \
81     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
82     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
83     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
84 }\
85 \
86 static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
87     OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(dst, src, stride, stride);\
88 }\
89 \
90 static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
91     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
92     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
93     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+1, half, stride, stride, SIZE);\
94 }\
95 \
96 static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
97     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
98     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
99     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
100 }\
101 \
102 static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
103     OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(dst, src, stride, stride);\
104 }\
105 \
106 static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
107     DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
108     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
109     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+stride, half, stride, stride, SIZE);\
110 }\
111 \
112 static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
113     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
114     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
115     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
116     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
117     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
118 }\
119 \
120 static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
121     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
122     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
123     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
124     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
125     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
126 }\
127 \
128 static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
129     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
130     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
131     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
132     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
133     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
134 }\
135 \
136 static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
137     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
138     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
139     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
140     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
141     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
142 }\
143 \
144 static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
145     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
146     OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(dst, tmp, src, stride, SIZE, stride);\
147 }\
148 \
149 static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
150     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
151     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
152     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
153     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
154     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
155     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
156 }\
157 \
158 static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
159     DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
160     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
161     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
162     put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
163     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
164     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
165 }\
166 \
167 static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
168     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
169     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
170     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
171     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
172     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
173     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
174 }\
175 \
176 static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
177     DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
178     DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
179     DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
180     put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
181     put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
182     OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
183 }\
184
185 static inline void put_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
186                                     const uint8_t * src2, int dst_stride,
187                                     int src_stride1, int h)
188 {
189     int i;
190     vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
191
192     mask_ = vec_lvsl(0, src2);
193
194     for (i = 0; i < h; i++) {
195
196         tmp1 = vec_ld(i * src_stride1, src1);
197         mask = vec_lvsl(i * src_stride1, src1);
198         tmp2 = vec_ld(i * src_stride1 + 15, src1);
199
200         a = vec_perm(tmp1, tmp2, mask);
201
202         tmp1 = vec_ld(i * 16, src2);
203         tmp2 = vec_ld(i * 16 + 15, src2);
204
205         b = vec_perm(tmp1, tmp2, mask_);
206
207         tmp1 = vec_ld(0, dst);
208         mask = vec_lvsl(0, dst);
209         tmp2 = vec_ld(15, dst);
210
211         d = vec_avg(a, b);
212
213         edges = vec_perm(tmp2, tmp1, mask);
214
215         align = vec_lvsr(0, dst);
216
217         tmp2 = vec_perm(d, edges, align);
218         tmp1 = vec_perm(edges, d, align);
219
220         vec_st(tmp2, 15, dst);
221         vec_st(tmp1, 0 , dst);
222
223         dst += dst_stride;
224     }
225 }
226
227 static inline void avg_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
228                                     const uint8_t * src2, int dst_stride,
229                                     int src_stride1, int h)
230 {
231     int i;
232     vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
233
234     mask_ = vec_lvsl(0, src2);
235
236     for (i = 0; i < h; i++) {
237
238         tmp1 = vec_ld(i * src_stride1, src1);
239         mask = vec_lvsl(i * src_stride1, src1);
240         tmp2 = vec_ld(i * src_stride1 + 15, src1);
241
242         a = vec_perm(tmp1, tmp2, mask);
243
244         tmp1 = vec_ld(i * 16, src2);
245         tmp2 = vec_ld(i * 16 + 15, src2);
246
247         b = vec_perm(tmp1, tmp2, mask_);
248
249         tmp1 = vec_ld(0, dst);
250         mask = vec_lvsl(0, dst);
251         tmp2 = vec_ld(15, dst);
252
253         d = vec_avg(vec_perm(tmp1, tmp2, mask), vec_avg(a, b));
254
255         edges = vec_perm(tmp2, tmp1, mask);
256
257         align = vec_lvsr(0, dst);
258
259         tmp2 = vec_perm(d, edges, align);
260         tmp1 = vec_perm(edges, d, align);
261
262         vec_st(tmp2, 15, dst);
263         vec_st(tmp1, 0 , dst);
264
265         dst += dst_stride;
266     }
267 }
268
269 /* Implemented but could be faster
270 #define put_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) put_pixels16_l2(d,s1,s2,ds,s1s,16,h)
271 #define avg_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) avg_pixels16_l2(d,s1,s2,ds,s1s,16,h)
272  */
273
274 H264_MC(put_, 16, altivec)
275 H264_MC(avg_, 16, altivec)
276
277 void ff_dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx)
278 {
279     const int high_bit_depth = avctx->bits_per_raw_sample > 8;
280
281     if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
282     if (!high_bit_depth) {
283         c->put_h264_chroma_pixels_tab[0] = put_h264_chroma_mc8_altivec;
284         c->avg_h264_chroma_pixels_tab[0] = avg_h264_chroma_mc8_altivec;
285     }
286     }
287 }
288 #endif /* HAVE_ALTIVEC */
289
290 av_cold void ff_h264qpel_init_ppc(H264QpelContext *c, int bit_depth)
291 {
292 #if HAVE_ALTIVEC
293     const int high_bit_depth = bit_depth > 8;
294
295     if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
296     if (!high_bit_depth) {
297 #define dspfunc(PFX, IDX, NUM) \
298         c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_altivec; \
299         c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_altivec; \
300         c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_altivec; \
301         c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_altivec; \
302         c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_altivec; \
303         c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_altivec; \
304         c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_altivec; \
305         c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_altivec; \
306         c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_altivec; \
307         c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_altivec; \
308         c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_altivec; \
309         c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_altivec; \
310         c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_altivec; \
311         c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_altivec; \
312         c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_altivec; \
313         c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_altivec
314
315         dspfunc(put_h264_qpel, 0, 16);
316         dspfunc(avg_h264_qpel, 0, 16);
317 #undef dspfunc
318     }
319     }
320 #endif /* HAVE_ALTIVEC */
321 }