2 * Copyright (c) 2004 Romain Dolbeau <romain@dolbeau.org>
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/h264qpel.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/ppc/types_altivec.h"
28 #include "libavutil/ppc/util_altivec.h"
29 #include "dsputil_altivec.h"
31 #define PUT_OP_U8_ALTIVEC(d, s, dst) d = s
32 #define AVG_OP_U8_ALTIVEC(d, s, dst) d = vec_avg(dst, s)
34 #define OP_U8_ALTIVEC PUT_OP_U8_ALTIVEC
35 #define PREFIX_h264_chroma_mc8_altivec put_h264_chroma_mc8_altivec
36 #define PREFIX_h264_chroma_mc8_num altivec_put_h264_chroma_mc8_num
37 #define PREFIX_h264_qpel16_h_lowpass_altivec put_h264_qpel16_h_lowpass_altivec
38 #define PREFIX_h264_qpel16_h_lowpass_num altivec_put_h264_qpel16_h_lowpass_num
39 #define PREFIX_h264_qpel16_v_lowpass_altivec put_h264_qpel16_v_lowpass_altivec
40 #define PREFIX_h264_qpel16_v_lowpass_num altivec_put_h264_qpel16_v_lowpass_num
41 #define PREFIX_h264_qpel16_hv_lowpass_altivec put_h264_qpel16_hv_lowpass_altivec
42 #define PREFIX_h264_qpel16_hv_lowpass_num altivec_put_h264_qpel16_hv_lowpass_num
43 #include "h264_qpel_template.c"
45 #undef PREFIX_h264_chroma_mc8_altivec
46 #undef PREFIX_h264_chroma_mc8_num
47 #undef PREFIX_h264_qpel16_h_lowpass_altivec
48 #undef PREFIX_h264_qpel16_h_lowpass_num
49 #undef PREFIX_h264_qpel16_v_lowpass_altivec
50 #undef PREFIX_h264_qpel16_v_lowpass_num
51 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
52 #undef PREFIX_h264_qpel16_hv_lowpass_num
54 #define OP_U8_ALTIVEC AVG_OP_U8_ALTIVEC
55 #define PREFIX_h264_chroma_mc8_altivec avg_h264_chroma_mc8_altivec
56 #define PREFIX_h264_chroma_mc8_num altivec_avg_h264_chroma_mc8_num
57 #define PREFIX_h264_qpel16_h_lowpass_altivec avg_h264_qpel16_h_lowpass_altivec
58 #define PREFIX_h264_qpel16_h_lowpass_num altivec_avg_h264_qpel16_h_lowpass_num
59 #define PREFIX_h264_qpel16_v_lowpass_altivec avg_h264_qpel16_v_lowpass_altivec
60 #define PREFIX_h264_qpel16_v_lowpass_num altivec_avg_h264_qpel16_v_lowpass_num
61 #define PREFIX_h264_qpel16_hv_lowpass_altivec avg_h264_qpel16_hv_lowpass_altivec
62 #define PREFIX_h264_qpel16_hv_lowpass_num altivec_avg_h264_qpel16_hv_lowpass_num
63 #include "h264_qpel_template.c"
65 #undef PREFIX_h264_chroma_mc8_altivec
66 #undef PREFIX_h264_chroma_mc8_num
67 #undef PREFIX_h264_qpel16_h_lowpass_altivec
68 #undef PREFIX_h264_qpel16_h_lowpass_num
69 #undef PREFIX_h264_qpel16_v_lowpass_altivec
70 #undef PREFIX_h264_qpel16_v_lowpass_num
71 #undef PREFIX_h264_qpel16_hv_lowpass_altivec
72 #undef PREFIX_h264_qpel16_hv_lowpass_num
74 #define H264_MC(OPNAME, SIZE, CODETYPE) \
75 static void OPNAME ## h264_qpel ## SIZE ## _mc00_ ## CODETYPE (uint8_t *dst, uint8_t *src, int stride){\
76 ff_ ## OPNAME ## pixels ## SIZE ## _ ## CODETYPE(dst, src, stride, SIZE);\
79 static void OPNAME ## h264_qpel ## SIZE ## _mc10_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){ \
80 DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
81 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
82 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
85 static void OPNAME ## h264_qpel ## SIZE ## _mc20_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
86 OPNAME ## h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(dst, src, stride, stride);\
89 static void OPNAME ## h264_qpel ## SIZE ## _mc30_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
90 DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
91 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
92 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+1, half, stride, stride, SIZE);\
95 static void OPNAME ## h264_qpel ## SIZE ## _mc01_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
96 DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
97 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
98 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src, half, stride, stride, SIZE);\
101 static void OPNAME ## h264_qpel ## SIZE ## _mc02_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
102 OPNAME ## h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(dst, src, stride, stride);\
105 static void OPNAME ## h264_qpel ## SIZE ## _mc03_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
106 DECLARE_ALIGNED(16, uint8_t, half)[SIZE*SIZE];\
107 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(half, src, SIZE, stride);\
108 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, src+stride, half, stride, stride, SIZE);\
111 static void OPNAME ## h264_qpel ## SIZE ## _mc11_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
112 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
113 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
114 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
115 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
116 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
119 static void OPNAME ## h264_qpel ## SIZE ## _mc31_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
120 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
121 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
122 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
123 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
124 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
127 static void OPNAME ## h264_qpel ## SIZE ## _mc13_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
128 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
129 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
130 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
131 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
132 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
135 static void OPNAME ## h264_qpel ## SIZE ## _mc33_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
136 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
137 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
138 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
139 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
140 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfV, stride, SIZE, SIZE);\
143 static void OPNAME ## h264_qpel ## SIZE ## _mc22_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
144 DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
145 OPNAME ## h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(dst, tmp, src, stride, SIZE, stride);\
148 static void OPNAME ## h264_qpel ## SIZE ## _mc21_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
149 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
150 DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
151 DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
152 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src, SIZE, stride);\
153 put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
154 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
157 static void OPNAME ## h264_qpel ## SIZE ## _mc23_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
158 DECLARE_ALIGNED(16, uint8_t, halfH)[SIZE*SIZE];\
159 DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
160 DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
161 put_h264_qpel ## SIZE ## _h_lowpass_ ## CODETYPE(halfH, src + stride, SIZE, stride);\
162 put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
163 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfH, halfHV, stride, SIZE, SIZE);\
166 static void OPNAME ## h264_qpel ## SIZE ## _mc12_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
167 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
168 DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
169 DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
170 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src, SIZE, stride);\
171 put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
172 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
175 static void OPNAME ## h264_qpel ## SIZE ## _mc32_ ## CODETYPE(uint8_t *dst, uint8_t *src, int stride){\
176 DECLARE_ALIGNED(16, uint8_t, halfV)[SIZE*SIZE];\
177 DECLARE_ALIGNED(16, uint8_t, halfHV)[SIZE*SIZE];\
178 DECLARE_ALIGNED(16, int16_t, tmp)[SIZE*(SIZE+8)];\
179 put_h264_qpel ## SIZE ## _v_lowpass_ ## CODETYPE(halfV, src+1, SIZE, stride);\
180 put_h264_qpel ## SIZE ## _hv_lowpass_ ## CODETYPE(halfHV, tmp, src, SIZE, SIZE, stride);\
181 OPNAME ## pixels ## SIZE ## _l2_ ## CODETYPE(dst, halfV, halfHV, stride, SIZE, SIZE);\
184 static inline void put_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
185 const uint8_t * src2, int dst_stride,
186 int src_stride1, int h)
189 vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
191 mask_ = vec_lvsl(0, src2);
193 for (i = 0; i < h; i++) {
195 tmp1 = vec_ld(i * src_stride1, src1);
196 mask = vec_lvsl(i * src_stride1, src1);
197 tmp2 = vec_ld(i * src_stride1 + 15, src1);
199 a = vec_perm(tmp1, tmp2, mask);
201 tmp1 = vec_ld(i * 16, src2);
202 tmp2 = vec_ld(i * 16 + 15, src2);
204 b = vec_perm(tmp1, tmp2, mask_);
206 tmp1 = vec_ld(0, dst);
207 mask = vec_lvsl(0, dst);
208 tmp2 = vec_ld(15, dst);
212 edges = vec_perm(tmp2, tmp1, mask);
214 align = vec_lvsr(0, dst);
216 tmp2 = vec_perm(d, edges, align);
217 tmp1 = vec_perm(edges, d, align);
219 vec_st(tmp2, 15, dst);
220 vec_st(tmp1, 0 , dst);
226 static inline void avg_pixels16_l2_altivec( uint8_t * dst, const uint8_t * src1,
227 const uint8_t * src2, int dst_stride,
228 int src_stride1, int h)
231 vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
233 mask_ = vec_lvsl(0, src2);
235 for (i = 0; i < h; i++) {
237 tmp1 = vec_ld(i * src_stride1, src1);
238 mask = vec_lvsl(i * src_stride1, src1);
239 tmp2 = vec_ld(i * src_stride1 + 15, src1);
241 a = vec_perm(tmp1, tmp2, mask);
243 tmp1 = vec_ld(i * 16, src2);
244 tmp2 = vec_ld(i * 16 + 15, src2);
246 b = vec_perm(tmp1, tmp2, mask_);
248 tmp1 = vec_ld(0, dst);
249 mask = vec_lvsl(0, dst);
250 tmp2 = vec_ld(15, dst);
252 d = vec_avg(vec_perm(tmp1, tmp2, mask), vec_avg(a, b));
254 edges = vec_perm(tmp2, tmp1, mask);
256 align = vec_lvsr(0, dst);
258 tmp2 = vec_perm(d, edges, align);
259 tmp1 = vec_perm(edges, d, align);
261 vec_st(tmp2, 15, dst);
262 vec_st(tmp1, 0 , dst);
268 /* Implemented but could be faster
269 #define put_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) put_pixels16_l2(d,s1,s2,ds,s1s,16,h)
270 #define avg_pixels16_l2_altivec(d,s1,s2,ds,s1s,h) avg_pixels16_l2(d,s1,s2,ds,s1s,16,h)
273 H264_MC(put_, 16, altivec)
274 H264_MC(avg_, 16, altivec)
276 void ff_dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx)
278 const int high_bit_depth = avctx->bits_per_raw_sample > 8;
280 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
281 if (!high_bit_depth) {
282 c->put_h264_chroma_pixels_tab[0] = put_h264_chroma_mc8_altivec;
283 c->avg_h264_chroma_pixels_tab[0] = avg_h264_chroma_mc8_altivec;
287 #endif /* HAVE_ALTIVEC */
289 void ff_h264qpel_init_ppc(H264QpelContext* c, int bit_depth) {
291 const int high_bit_depth = bit_depth > 8;
293 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
294 if (!high_bit_depth) {
295 #define dspfunc(PFX, IDX, NUM) \
296 c->PFX ## _pixels_tab[IDX][ 0] = PFX ## NUM ## _mc00_altivec; \
297 c->PFX ## _pixels_tab[IDX][ 1] = PFX ## NUM ## _mc10_altivec; \
298 c->PFX ## _pixels_tab[IDX][ 2] = PFX ## NUM ## _mc20_altivec; \
299 c->PFX ## _pixels_tab[IDX][ 3] = PFX ## NUM ## _mc30_altivec; \
300 c->PFX ## _pixels_tab[IDX][ 4] = PFX ## NUM ## _mc01_altivec; \
301 c->PFX ## _pixels_tab[IDX][ 5] = PFX ## NUM ## _mc11_altivec; \
302 c->PFX ## _pixels_tab[IDX][ 6] = PFX ## NUM ## _mc21_altivec; \
303 c->PFX ## _pixels_tab[IDX][ 7] = PFX ## NUM ## _mc31_altivec; \
304 c->PFX ## _pixels_tab[IDX][ 8] = PFX ## NUM ## _mc02_altivec; \
305 c->PFX ## _pixels_tab[IDX][ 9] = PFX ## NUM ## _mc12_altivec; \
306 c->PFX ## _pixels_tab[IDX][10] = PFX ## NUM ## _mc22_altivec; \
307 c->PFX ## _pixels_tab[IDX][11] = PFX ## NUM ## _mc32_altivec; \
308 c->PFX ## _pixels_tab[IDX][12] = PFX ## NUM ## _mc03_altivec; \
309 c->PFX ## _pixels_tab[IDX][13] = PFX ## NUM ## _mc13_altivec; \
310 c->PFX ## _pixels_tab[IDX][14] = PFX ## NUM ## _mc23_altivec; \
311 c->PFX ## _pixels_tab[IDX][15] = PFX ## NUM ## _mc33_altivec
313 dspfunc(put_h264_qpel, 0, 16);
314 dspfunc(avg_h264_qpel, 0, 16);
318 #endif /* HAVE_ALTIVEC */