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 "libavutil/attributes.h"
23 #include "libavcodec/h264qpel.h"
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"
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)
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"
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
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"
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
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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);\
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)
190 vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
192 mask_ = vec_lvsl(0, src2);
194 for (i = 0; i < h; i++) {
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);
200 a = vec_perm(tmp1, tmp2, mask);
202 tmp1 = vec_ld(i * 16, src2);
203 tmp2 = vec_ld(i * 16 + 15, src2);
205 b = vec_perm(tmp1, tmp2, mask_);
207 tmp1 = vec_ld(0, dst);
208 mask = vec_lvsl(0, dst);
209 tmp2 = vec_ld(15, dst);
213 edges = vec_perm(tmp2, tmp1, mask);
215 align = vec_lvsr(0, dst);
217 tmp2 = vec_perm(d, edges, align);
218 tmp1 = vec_perm(edges, d, align);
220 vec_st(tmp2, 15, dst);
221 vec_st(tmp1, 0 , dst);
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)
232 vec_u8 a, b, d, tmp1, tmp2, mask, mask_, edges, align;
234 mask_ = vec_lvsl(0, src2);
236 for (i = 0; i < h; i++) {
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);
242 a = vec_perm(tmp1, tmp2, mask);
244 tmp1 = vec_ld(i * 16, src2);
245 tmp2 = vec_ld(i * 16 + 15, src2);
247 b = vec_perm(tmp1, tmp2, mask_);
249 tmp1 = vec_ld(0, dst);
250 mask = vec_lvsl(0, dst);
251 tmp2 = vec_ld(15, dst);
253 d = vec_avg(vec_perm(tmp1, tmp2, mask), vec_avg(a, b));
255 edges = vec_perm(tmp2, tmp1, mask);
257 align = vec_lvsr(0, dst);
259 tmp2 = vec_perm(d, edges, align);
260 tmp1 = vec_perm(edges, d, align);
262 vec_st(tmp2, 15, dst);
263 vec_st(tmp1, 0 , dst);
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)
274 H264_MC(put_, 16, altivec)
275 H264_MC(avg_, 16, altivec)
277 void ff_dsputil_h264_init_ppc(DSPContext* c, AVCodecContext *avctx)
279 const int high_bit_depth = avctx->bits_per_raw_sample > 8;
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;
288 #endif /* HAVE_ALTIVEC */
290 av_cold void ff_h264qpel_init_ppc(H264QpelContext *c, int bit_depth)
293 const int high_bit_depth = bit_depth > 8;
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
315 dspfunc(put_h264_qpel, 0, 16);
316 dspfunc(avg_h264_qpel, 0, 16);
320 #endif /* HAVE_ALTIVEC */