FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
swscale_altivec.c
Go to the documentation of this file.
1 /*
2  * AltiVec-enhanced yuv2yuvX
3  *
4  * Copyright (C) 2004 Romain Dolbeau <romain@dolbeau.org>
5  * based on the equivalent C code in swscale.c
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <inttypes.h>
25 
26 #include "config.h"
27 #include "libswscale/swscale.h"
29 #include "libavutil/attributes.h"
30 #include "libavutil/cpu.h"
31 #include "yuv2rgb_altivec.h"
33 
34 #if HAVE_ALTIVEC
35 #define vzero vec_splat_s32(0)
36 
37 #if HAVE_BIGENDIAN
38 #define GET_LS(a,b,c,s) {\
39  vector signed short l2 = vec_ld(((b) << 1) + 16, s);\
40  ls = vec_perm(a, l2, c);\
41  a = l2;\
42  }
43 #define GET_VF(a, b, c,d) {\
44  a = vec_mergeh(c, d);\
45  b = vec_mergel(c, d);\
46  }
47 #else
48 #define GET_LS(a,b,c,s) {\
49  ls = a;\
50  a = vec_vsx_ld(((b) << 1) + 16, s);\
51  }
52 #define GET_VF(a, b, c, d) {\
53  a = vec_mergel(d, c);\
54  b = vec_mergeh(d, c);\
55  }
56 #endif
57 
58 #define yuv2planeX_8(d1, d2, l1, src, x, perm, filter) do {\
59  vector signed short ls;\
60  GET_LS(l1, x, perm, src);\
61  vector signed int i1 = vec_mule(filter, ls);\
62  vector signed int i2 = vec_mulo(filter, ls);\
63  vector signed int vf1, vf2;\
64  GET_VF(vf1, vf2, i1, i2);\
65  d1 = vec_add(d1, vf1);\
66  d2 = vec_add(d2, vf2);\
67  } while (0)
68 
69 #if HAVE_BIGENDIAN
70 #define LOAD_FILTER(vf,f) {\
71  vector unsigned char perm0 = vec_lvsl(joffset, f);\
72  vf = vec_ld(joffset, f);\
73  vf = vec_perm(vf, vf, perm0);\
74 }
75 #define LOAD_L1(ll1,s,p){\
76  p = vec_lvsl(xoffset, s);\
77  ll1 = vec_ld(xoffset, s);\
78 }
79 #else
80 #define LOAD_FILTER(vf,f) {\
81  vf = vec_vsx_ld(joffset, f);\
82 }
83 #define LOAD_L1(ll1,s,p){\
84  ll1 = vec_vsx_ld(xoffset, s);\
85 }
86 #endif
87 
88 static void yuv2planeX_16_altivec(const int16_t *filter, int filterSize,
89  const int16_t **src, uint8_t *dest,
90  const uint8_t *dither, int offset, int x)
91 {
92  register int i, j;
93  DECLARE_ALIGNED(16, int, val)[16];
94  vector signed int vo1, vo2, vo3, vo4;
95  vector unsigned short vs1, vs2;
96  vector unsigned char vf;
97  vector unsigned int altivec_vectorShiftInt19 =
98  vec_add(vec_splat_u32(10), vec_splat_u32(9));
99 
100  for (i = 0; i < 16; i++)
101  val[i] = dither[(x + i + offset) & 7] << 12;
102 
103  vo1 = vec_ld(0, val);
104  vo2 = vec_ld(16, val);
105  vo3 = vec_ld(32, val);
106  vo4 = vec_ld(48, val);
107 
108  for (j = 0; j < filterSize; j++) {
109  unsigned int joffset=j<<1;
110  unsigned int xoffset=x<<1;
111  vector unsigned char perm;
112  vector signed short l1,vLumFilter;
113  LOAD_FILTER(vLumFilter,filter);
114  vLumFilter = vec_splat(vLumFilter, 0);
115  LOAD_L1(l1,src[j],perm);
116  yuv2planeX_8(vo1, vo2, l1, src[j], x, perm, vLumFilter);
117  yuv2planeX_8(vo3, vo4, l1, src[j], x + 8, perm, vLumFilter);
118  }
119 
120  vo1 = vec_sra(vo1, altivec_vectorShiftInt19);
121  vo2 = vec_sra(vo2, altivec_vectorShiftInt19);
122  vo3 = vec_sra(vo3, altivec_vectorShiftInt19);
123  vo4 = vec_sra(vo4, altivec_vectorShiftInt19);
124  vs1 = vec_packsu(vo1, vo2);
125  vs2 = vec_packsu(vo3, vo4);
126  vf = vec_packsu(vs1, vs2);
127  VEC_ST(vf, 0, dest);
128 }
129 
130 
131 static inline void yuv2planeX_u(const int16_t *filter, int filterSize,
132  const int16_t **src, uint8_t *dest, int dstW,
133  const uint8_t *dither, int offset, int x)
134 {
135  int i, j;
136 
137  for (i = x; i < dstW; i++) {
138  int t = dither[(i + offset) & 7] << 12;
139  for (j = 0; j < filterSize; j++)
140  t += src[j][i] * filter[j];
141  dest[i] = av_clip_uint8(t >> 19);
142  }
143 }
144 
145 static void yuv2planeX_altivec(const int16_t *filter, int filterSize,
146  const int16_t **src, uint8_t *dest, int dstW,
147  const uint8_t *dither, int offset)
148 {
149  int dst_u = -(uintptr_t)dest & 15;
150  int i;
151 
152  yuv2planeX_u(filter, filterSize, src, dest, dst_u, dither, offset, 0);
153 
154  for (i = dst_u; i < dstW - 15; i += 16)
155  yuv2planeX_16_altivec(filter, filterSize, src, dest + i, dither,
156  offset, i);
157 
158  yuv2planeX_u(filter, filterSize, src, dest, dstW, dither, offset, i);
159 }
160 
161 #if HAVE_BIGENDIAN
162 // The 3 above is 2 (filterSize == 4) + 1 (sizeof(short) == 2).
163 
164 // The neat trick: We only care for half the elements,
165 // high or low depending on (i<<3)%16 (it's 0 or 8 here),
166 // and we're going to use vec_mule, so we choose
167 // carefully how to "unpack" the elements into the even slots.
168 #define GET_VF4(a, vf, f) {\
169  vf = vec_ld(a<< 3, f);\
170  if ((a << 3) % 16)\
171  vf = vec_mergel(vf, (vector signed short)vzero);\
172  else\
173  vf = vec_mergeh(vf, (vector signed short)vzero);\
174 }
175 #define FIRST_LOAD(sv, pos, s, per) {\
176  sv = vec_ld(pos, s);\
177  per = vec_lvsl(pos, s);\
178 }
179 #define UPDATE_PTR(s0, d0, s1, d1) {\
180  d0 = s0;\
181  d1 = s1;\
182 }
183 #define LOAD_SRCV(pos, a, s, per, v0, v1, vf) {\
184  v1 = vec_ld(pos + a + 16, s);\
185  vf = vec_perm(v0, v1, per);\
186 }
187 #define LOAD_SRCV8(pos, a, s, per, v0, v1, vf) {\
188  if ((((uintptr_t)s + pos) % 16) > 8) {\
189  v1 = vec_ld(pos + a + 16, s);\
190  }\
191  vf = vec_perm(v0, src_v1, per);\
192 }
193 #define GET_VFD(a, b, f, vf0, vf1, per, vf, off) {\
194  vf1 = vec_ld((a * 2 * filterSize) + (b * 2) + 16 + off, f);\
195  vf = vec_perm(vf0, vf1, per);\
196 }
197 #else /* else of #if HAVE_BIGENDIAN */
198 #define GET_VF4(a, vf, f) {\
199  vf = (vector signed short)vec_vsx_ld(a << 3, f);\
200  vf = vec_mergeh(vf, (vector signed short)vzero);\
201 }
202 #define FIRST_LOAD(sv, pos, s, per) {}
203 #define UPDATE_PTR(s0, d0, s1, d1) {}
204 #define LOAD_SRCV(pos, a, s, per, v0, v1, vf) {\
205  vf = vec_vsx_ld(pos + a, s);\
206 }
207 #define LOAD_SRCV8(pos, a, s, per, v0, v1, vf) LOAD_SRCV(pos, a, s, per, v0, v1, vf)
208 #define GET_VFD(a, b, f, vf0, vf1, per, vf, off) {\
209  vf = vec_vsx_ld((a * 2 * filterSize) + (b * 2) + off, f);\
210 }
211 #endif /* end of #if HAVE_BIGENDIAN */
212 
213 static void hScale_altivec_real(SwsContext *c, int16_t *dst, int dstW,
214  const uint8_t *src, const int16_t *filter,
215  const int32_t *filterPos, int filterSize)
216 {
217  register int i;
218  DECLARE_ALIGNED(16, int, tempo)[4];
219 
220  if (filterSize % 4) {
221  for (i = 0; i < dstW; i++) {
222  register int j;
223  register int srcPos = filterPos[i];
224  register int val = 0;
225  for (j = 0; j < filterSize; j++)
226  val += ((int)src[srcPos + j]) * filter[filterSize * i + j];
227  dst[i] = FFMIN(val >> 7, (1 << 15) - 1);
228  }
229  } else
230  switch (filterSize) {
231  case 4:
232  for (i = 0; i < dstW; i++) {
233  register int srcPos = filterPos[i];
234 
235  vector unsigned char src_vF = unaligned_load(srcPos, src);
236  vector signed short src_v, filter_v;
237  vector signed int val_vEven, val_s;
238  src_v = // vec_unpackh sign-extends...
239  (vector signed short)(VEC_MERGEH((vector unsigned char)vzero, src_vF));
240  // now put our elements in the even slots
241  src_v = vec_mergeh(src_v, (vector signed short)vzero);
242  GET_VF4(i, filter_v, filter);
243  val_vEven = vec_mule(src_v, filter_v);
244  val_s = vec_sums(val_vEven, vzero);
245  vec_st(val_s, 0, tempo);
246  dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
247  }
248  break;
249  case 8:
250  for (i = 0; i < dstW; i++) {
251  register int srcPos = filterPos[i];
252  vector unsigned char src_vF, src_v0, src_v1;
253  vector unsigned char permS;
254  vector signed short src_v, filter_v;
255  vector signed int val_v, val_s;
256  FIRST_LOAD(src_v0, srcPos, src, permS);
257  LOAD_SRCV8(srcPos, 0, src, permS, src_v0, src_v1, src_vF);
258  src_v = // vec_unpackh sign-extends...
259  (vector signed short)(VEC_MERGEH((vector unsigned char)vzero, src_vF));
260  filter_v = vec_ld(i << 4, filter);
261  val_v = vec_msums(src_v, filter_v, (vector signed int)vzero);
262  val_s = vec_sums(val_v, vzero);
263  vec_st(val_s, 0, tempo);
264  dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
265  }
266  break;
267 
268  case 16:
269  for (i = 0; i < dstW; i++) {
270  register int srcPos = filterPos[i];
271 
272  vector unsigned char src_vF = unaligned_load(srcPos, src);
273  vector signed short src_vA = // vec_unpackh sign-extends...
274  (vector signed short)(VEC_MERGEH((vector unsigned char)vzero, src_vF));
275  vector signed short src_vB = // vec_unpackh sign-extends...
276  (vector signed short)(VEC_MERGEL((vector unsigned char)vzero, src_vF));
277  vector signed short filter_v0 = vec_ld(i << 5, filter);
278  vector signed short filter_v1 = vec_ld((i << 5) + 16, filter);
279 
280  vector signed int val_acc = vec_msums(src_vA, filter_v0, (vector signed int)vzero);
281  vector signed int val_v = vec_msums(src_vB, filter_v1, val_acc);
282 
283  vector signed int val_s = vec_sums(val_v, vzero);
284 
285  VEC_ST(val_s, 0, tempo);
286  dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
287  }
288  break;
289 
290  default:
291  for (i = 0; i < dstW; i++) {
292  register int j, offset = i * 2 * filterSize;
293  register int srcPos = filterPos[i];
294 
295  vector signed int val_s, val_v = (vector signed int)vzero;
296  vector signed short filter_v0R;
297  vector unsigned char permF, src_v0, permS;
298  FIRST_LOAD(filter_v0R, offset, filter, permF);
299  FIRST_LOAD(src_v0, srcPos, src, permS);
300 
301  for (j = 0; j < filterSize - 15; j += 16) {
302  vector unsigned char src_v1, src_vF;
303  vector signed short filter_v1R, filter_v2R, filter_v0, filter_v1;
304  LOAD_SRCV(srcPos, j, src, permS, src_v0, src_v1, src_vF);
305  vector signed short src_vA = // vec_unpackh sign-extends...
306  (vector signed short)(VEC_MERGEH((vector unsigned char)vzero, src_vF));
307  vector signed short src_vB = // vec_unpackh sign-extends...
308  (vector signed short)(VEC_MERGEL((vector unsigned char)vzero, src_vF));
309  GET_VFD(i, j, filter, filter_v0R, filter_v1R, permF, filter_v0, 0);
310  GET_VFD(i, j, filter, filter_v1R, filter_v2R, permF, filter_v1, 16);
311 
312  vector signed int val_acc = vec_msums(src_vA, filter_v0, val_v);
313  val_v = vec_msums(src_vB, filter_v1, val_acc);
314  UPDATE_PTR(filter_v2R, filter_v0R, src_v1, src_v0);
315  }
316 
317  if (j < filterSize - 7) {
318  // loading src_v0 is useless, it's already done above
319  vector unsigned char src_v1, src_vF;
320  vector signed short src_v, filter_v1R, filter_v;
321  LOAD_SRCV8(srcPos, j, src, permS, src_v0, src_v1, src_vF);
322  src_v = // vec_unpackh sign-extends...
323  (vector signed short)(VEC_MERGEH((vector unsigned char)vzero, src_vF));
324  GET_VFD(i, j, filter, filter_v0R, filter_v1R, permF, filter_v, 0);
325  val_v = vec_msums(src_v, filter_v, val_v);
326  }
327  val_s = vec_sums(val_v, vzero);
328 
329  VEC_ST(val_s, 0, tempo);
330  dst[i] = FFMIN(tempo[3] >> 7, (1 << 15) - 1);
331  }
332  }
333 }
334 #endif /* HAVE_ALTIVEC */
335 
337 {
338 #if HAVE_ALTIVEC
339  enum AVPixelFormat dstFormat = c->dstFormat;
340 
342  return;
343 
344  if (c->srcBpc == 8 && c->dstBpc <= 14) {
345  c->hyScale = c->hcScale = hScale_altivec_real;
346  }
347  if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat) &&
348  dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
349  !c->alpPixBuf) {
350  c->yuv2planeX = yuv2planeX_altivec;
351  }
352 
353  /* The following list of supported dstFormat values should
354  * match what's found in the body of ff_yuv2packedX_altivec() */
355  if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->alpPixBuf) {
356  switch (c->dstFormat) {
357  case AV_PIX_FMT_ABGR:
358  c->yuv2packedX = ff_yuv2abgr_X_altivec;
359  break;
360  case AV_PIX_FMT_BGRA:
361  c->yuv2packedX = ff_yuv2bgra_X_altivec;
362  break;
363  case AV_PIX_FMT_ARGB:
364  c->yuv2packedX = ff_yuv2argb_X_altivec;
365  break;
366  case AV_PIX_FMT_RGBA:
367  c->yuv2packedX = ff_yuv2rgba_X_altivec;
368  break;
369  case AV_PIX_FMT_BGR24:
370  c->yuv2packedX = ff_yuv2bgr24_X_altivec;
371  break;
372  case AV_PIX_FMT_RGB24:
373  c->yuv2packedX = ff_yuv2rgb24_X_altivec;
374  break;
375  }
376  }
377 #endif /* HAVE_ALTIVEC */
378 }