FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
yuv2rgb_altivec.c
Go to the documentation of this file.
1 /*
2  * AltiVec acceleration for colorspace conversion
3  *
4  * copyright (C) 2004 Marc Hoffman <marc.hoffman@analog.com>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /*
24  * Convert I420 YV12 to RGB in various formats,
25  * it rejects images that are not in 420 formats,
26  * it rejects images that don't have widths of multiples of 16,
27  * it rejects images that don't have heights of multiples of 2.
28  * Reject defers to C simulation code.
29  *
30  * Lots of optimizations to be done here.
31  *
32  * 1. Need to fix saturation code. I just couldn't get it to fly with packs
33  * and adds, so we currently use max/min to clip.
34  *
35  * 2. The inefficient use of chroma loading needs a bit of brushing up.
36  *
37  * 3. Analysis of pipeline stalls needs to be done. Use shark to identify
38  * pipeline stalls.
39  *
40  *
41  * MODIFIED to calculate coeffs from currently selected color space.
42  * MODIFIED core to be a macro where you specify the output format.
43  * ADDED UYVY conversion which is never called due to some thing in swscale.
44  * CORRECTED algorithim selection to be strict on input formats.
45  * ADDED runtime detection of AltiVec.
46  *
47  * ADDED altivec_yuv2packedX vertical scl + RGB converter
48  *
49  * March 27,2004
50  * PERFORMANCE ANALYSIS
51  *
52  * The C version uses 25% of the processor or ~250Mips for D1 video rawvideo
53  * used as test.
54  * The AltiVec version uses 10% of the processor or ~100Mips for D1 video
55  * same sequence.
56  *
57  * 720 * 480 * 30 ~10MPS
58  *
59  * so we have roughly 10 clocks per pixel. This is too high, something has
60  * to be wrong.
61  *
62  * OPTIMIZED clip codes to utilize vec_max and vec_packs removing the
63  * need for vec_min.
64  *
65  * OPTIMIZED DST OUTPUT cache/DMA controls. We are pretty much guaranteed to
66  * have the input video frame, it was just decompressed so it probably resides
67  * in L1 caches. However, we are creating the output video stream. This needs
68  * to use the DSTST instruction to optimize for the cache. We couple this with
69  * the fact that we are not going to be visiting the input buffer again so we
70  * mark it Least Recently Used. This shaves 25% of the processor cycles off.
71  *
72  * Now memcpy is the largest mips consumer in the system, probably due
73  * to the inefficient X11 stuff.
74  *
75  * GL libraries seem to be very slow on this machine 1.33Ghz PB running
76  * Jaguar, this is not the case for my 1Ghz PB. I thought it might be
77  * a versioning issue, however I have libGL.1.2.dylib for both
78  * machines. (We need to figure this out now.)
79  *
80  * GL2 libraries work now with patch for RGB32.
81  *
82  * NOTE: quartz vo driver ARGB32_to_RGB24 consumes 30% of the processor.
83  *
84  * Integrated luma prescaling adjustment for saturation/contrast/brightness
85  * adjustment.
86  */
87 
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <inttypes.h>
92 #include <assert.h>
93 
94 #include "config.h"
95 #include "libswscale/rgb2rgb.h"
96 #include "libswscale/swscale.h"
98 #include "libavutil/attributes.h"
99 #include "libavutil/cpu.h"
100 #include "libavutil/pixdesc.h"
101 #include "yuv2rgb_altivec.h"
102 
103 #undef PROFILE_THE_BEAST
104 #undef INC_SCALING
105 
106 typedef unsigned char ubyte;
107 typedef signed char sbyte;
108 
109 /* RGB interleaver, 16 planar pels 8-bit samples per channel in
110  * homogeneous vector registers x0,x1,x2 are interleaved with the
111  * following technique:
112  *
113  * o0 = vec_mergeh(x0, x1);
114  * o1 = vec_perm(o0, x2, perm_rgb_0);
115  * o2 = vec_perm(o0, x2, perm_rgb_1);
116  * o3 = vec_mergel(x0, x1);
117  * o4 = vec_perm(o3, o2, perm_rgb_2);
118  * o5 = vec_perm(o3, o2, perm_rgb_3);
119  *
120  * perm_rgb_0: o0(RG).h v1(B) --> o1*
121  * 0 1 2 3 4
122  * rgbr|gbrg|brgb|rgbr
123  * 0010 0100 1001 0010
124  * 0102 3145 2673 894A
125  *
126  * perm_rgb_1: o0(RG).h v1(B) --> o2
127  * 0 1 2 3 4
128  * gbrg|brgb|bbbb|bbbb
129  * 0100 1001 1111 1111
130  * B5CD 6EF7 89AB CDEF
131  *
132  * perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
133  * 0 1 2 3 4
134  * gbrg|brgb|rgbr|gbrg
135  * 1111 1111 0010 0100
136  * 89AB CDEF 0182 3945
137  *
138  * perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
139  * 0 1 2 3 4
140  * brgb|rgbr|gbrg|brgb
141  * 1001 0010 0100 1001
142  * a67b 89cA BdCD eEFf
143  *
144  */
145 static const vector unsigned char
146  perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
147  0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
148  perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
149  0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
150  perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
151  0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
152  perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
153  0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
154 
155 #define vec_merge3(x2, x1, x0, y0, y1, y2) \
156  do { \
157  __typeof__(x0) o0, o2, o3; \
158  o0 = vec_mergeh(x0, x1); \
159  y0 = vec_perm(o0, x2, perm_rgb_0); \
160  o2 = vec_perm(o0, x2, perm_rgb_1); \
161  o3 = vec_mergel(x0, x1); \
162  y1 = vec_perm(o3, o2, perm_rgb_2); \
163  y2 = vec_perm(o3, o2, perm_rgb_3); \
164  } while (0)
165 
166 #define vec_mstbgr24(x0, x1, x2, ptr) \
167  do { \
168  __typeof__(x0) _0, _1, _2; \
169  vec_merge3(x0, x1, x2, _0, _1, _2); \
170  vec_st(_0, 0, ptr++); \
171  vec_st(_1, 0, ptr++); \
172  vec_st(_2, 0, ptr++); \
173  } while (0)
174 
175 #define vec_mstrgb24(x0, x1, x2, ptr) \
176  do { \
177  __typeof__(x0) _0, _1, _2; \
178  vec_merge3(x2, x1, x0, _0, _1, _2); \
179  vec_st(_0, 0, ptr++); \
180  vec_st(_1, 0, ptr++); \
181  vec_st(_2, 0, ptr++); \
182  } while (0)
183 
184 /* pack the pixels in rgb0 format
185  * msb R
186  * lsb 0
187  */
188 #define vec_mstrgb32(T, x0, x1, x2, x3, ptr) \
189  do { \
190  T _0, _1, _2, _3; \
191  _0 = vec_mergeh(x0, x1); \
192  _1 = vec_mergeh(x2, x3); \
193  _2 = (T) vec_mergeh((vector unsigned short) _0, \
194  (vector unsigned short) _1); \
195  _3 = (T) vec_mergel((vector unsigned short) _0, \
196  (vector unsigned short) _1); \
197  vec_st(_2, 0 * 16, (T *) ptr); \
198  vec_st(_3, 1 * 16, (T *) ptr); \
199  _0 = vec_mergel(x0, x1); \
200  _1 = vec_mergel(x2, x3); \
201  _2 = (T) vec_mergeh((vector unsigned short) _0, \
202  (vector unsigned short) _1); \
203  _3 = (T) vec_mergel((vector unsigned short) _0, \
204  (vector unsigned short) _1); \
205  vec_st(_2, 2 * 16, (T *) ptr); \
206  vec_st(_3, 3 * 16, (T *) ptr); \
207  ptr += 4; \
208  } while (0)
209 
210 /*
211  * 1 0 1.4021 | | Y |
212  * 1 -0.3441 -0.7142 |x| Cb|
213  * 1 1.7718 0 | | Cr|
214  *
215  *
216  * Y: [-128 127]
217  * Cb/Cr : [-128 127]
218  *
219  * typical YUV conversion works on Y: 0-255 this version has been
220  * optimized for JPEG decoding.
221  */
222 
223 #define vec_unh(x) \
224  (vector signed short) \
225  vec_perm(x, (__typeof__(x)) { 0 }, \
226  ((vector unsigned char) { \
227  0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03, \
228  0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
229 
230 #define vec_unl(x) \
231  (vector signed short) \
232  vec_perm(x, (__typeof__(x)) { 0 }, \
233  ((vector unsigned char) { \
234  0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B, \
235  0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
236 
237 #define vec_clip_s16(x) \
238  vec_max(vec_min(x, ((vector signed short) { \
239  235, 235, 235, 235, 235, 235, 235, 235 })), \
240  ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
241 
242 #define vec_packclp(x, y) \
243  (vector unsigned char) \
244  vec_packs((vector unsigned short) \
245  vec_max(x, ((vector signed short) { 0 })), \
246  (vector unsigned short) \
247  vec_max(y, ((vector signed short) { 0 })))
248 
249 static inline void cvtyuvtoRGB(SwsContext *c, vector signed short Y,
250  vector signed short U, vector signed short V,
251  vector signed short *R, vector signed short *G,
252  vector signed short *B)
253 {
254  vector signed short vx, ux, uvx;
255 
256  Y = vec_mradds(Y, c->CY, c->OY);
257  U = vec_sub(U, (vector signed short)
258  vec_splat((vector signed short) { 128 }, 0));
259  V = vec_sub(V, (vector signed short)
260  vec_splat((vector signed short) { 128 }, 0));
261 
262  // ux = (CBU * (u << c->CSHIFT) + 0x4000) >> 15;
263  ux = vec_sl(U, c->CSHIFT);
264  *B = vec_mradds(ux, c->CBU, Y);
265 
266  // vx = (CRV * (v << c->CSHIFT) + 0x4000) >> 15;
267  vx = vec_sl(V, c->CSHIFT);
268  *R = vec_mradds(vx, c->CRV, Y);
269 
270  // uvx = ((CGU * u) + (CGV * v)) >> 15;
271  uvx = vec_mradds(U, c->CGU, Y);
272  *G = vec_mradds(V, c->CGV, uvx);
273 }
274 
275 /*
276  * ------------------------------------------------------------------------------
277  * CS converters
278  * ------------------------------------------------------------------------------
279  */
280 
281 #define DEFCSP420_CVT(name, out_pixels) \
282 static int altivec_ ## name(SwsContext *c, const unsigned char **in, \
283  int *instrides, int srcSliceY, int srcSliceH, \
284  unsigned char **oplanes, int *outstrides) \
285 { \
286  int w = c->srcW; \
287  int h = srcSliceH; \
288  int i, j; \
289  int instrides_scl[3]; \
290  vector unsigned char y0, y1; \
291  \
292  vector signed char u, v; \
293  \
294  vector signed short Y0, Y1, Y2, Y3; \
295  vector signed short U, V; \
296  vector signed short vx, ux, uvx; \
297  vector signed short vx0, ux0, uvx0; \
298  vector signed short vx1, ux1, uvx1; \
299  vector signed short R0, G0, B0; \
300  vector signed short R1, G1, B1; \
301  vector unsigned char R, G, B; \
302  \
303  const vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP; \
304  vector unsigned char align_perm; \
305  \
306  vector signed short lCY = c->CY; \
307  vector signed short lOY = c->OY; \
308  vector signed short lCRV = c->CRV; \
309  vector signed short lCBU = c->CBU; \
310  vector signed short lCGU = c->CGU; \
311  vector signed short lCGV = c->CGV; \
312  vector unsigned short lCSHIFT = c->CSHIFT; \
313  \
314  const ubyte *y1i = in[0]; \
315  const ubyte *y2i = in[0] + instrides[0]; \
316  const ubyte *ui = in[1]; \
317  const ubyte *vi = in[2]; \
318  \
319  vector unsigned char *oute, *outo; \
320  \
321  /* loop moves y{1, 2}i by w */ \
322  instrides_scl[0] = instrides[0] * 2 - w; \
323  /* loop moves ui by w / 2 */ \
324  instrides_scl[1] = instrides[1] - w / 2; \
325  /* loop moves vi by w / 2 */ \
326  instrides_scl[2] = instrides[2] - w / 2; \
327  \
328  for (i = 0; i < h / 2; i++) { \
329  oute = (vector unsigned char *)(oplanes[0] + outstrides[0] * \
330  (srcSliceY + i * 2)); \
331  outo = oute + (outstrides[0] >> 4); \
332  vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0); \
333  vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1); \
334  \
335  for (j = 0; j < w / 16; j++) { \
336  y1ivP = (const vector unsigned char *) y1i; \
337  y2ivP = (const vector unsigned char *) y2i; \
338  uivP = (const vector unsigned char *) ui; \
339  vivP = (const vector unsigned char *) vi; \
340  \
341  align_perm = vec_lvsl(0, y1i); \
342  y0 = (vector unsigned char) \
343  vec_perm(y1ivP[0], y1ivP[1], align_perm); \
344  \
345  align_perm = vec_lvsl(0, y2i); \
346  y1 = (vector unsigned char) \
347  vec_perm(y2ivP[0], y2ivP[1], align_perm); \
348  \
349  align_perm = vec_lvsl(0, ui); \
350  u = (vector signed char) \
351  vec_perm(uivP[0], uivP[1], align_perm); \
352  \
353  align_perm = vec_lvsl(0, vi); \
354  v = (vector signed char) \
355  vec_perm(vivP[0], vivP[1], align_perm); \
356  \
357  u = (vector signed char) \
358  vec_sub(u, \
359  (vector signed char) \
360  vec_splat((vector signed char) { 128 }, 0)); \
361  v = (vector signed char) \
362  vec_sub(v, \
363  (vector signed char) \
364  vec_splat((vector signed char) { 128 }, 0)); \
365  \
366  U = vec_unpackh(u); \
367  V = vec_unpackh(v); \
368  \
369  Y0 = vec_unh(y0); \
370  Y1 = vec_unl(y0); \
371  Y2 = vec_unh(y1); \
372  Y3 = vec_unl(y1); \
373  \
374  Y0 = vec_mradds(Y0, lCY, lOY); \
375  Y1 = vec_mradds(Y1, lCY, lOY); \
376  Y2 = vec_mradds(Y2, lCY, lOY); \
377  Y3 = vec_mradds(Y3, lCY, lOY); \
378  \
379  /* ux = (CBU * (u << CSHIFT) + 0x4000) >> 15 */ \
380  ux = vec_sl(U, lCSHIFT); \
381  ux = vec_mradds(ux, lCBU, (vector signed short) { 0 }); \
382  ux0 = vec_mergeh(ux, ux); \
383  ux1 = vec_mergel(ux, ux); \
384  \
385  /* vx = (CRV * (v << CSHIFT) + 0x4000) >> 15; */ \
386  vx = vec_sl(V, lCSHIFT); \
387  vx = vec_mradds(vx, lCRV, (vector signed short) { 0 }); \
388  vx0 = vec_mergeh(vx, vx); \
389  vx1 = vec_mergel(vx, vx); \
390  \
391  /* uvx = ((CGU * u) + (CGV * v)) >> 15 */ \
392  uvx = vec_mradds(U, lCGU, (vector signed short) { 0 }); \
393  uvx = vec_mradds(V, lCGV, uvx); \
394  uvx0 = vec_mergeh(uvx, uvx); \
395  uvx1 = vec_mergel(uvx, uvx); \
396  \
397  R0 = vec_add(Y0, vx0); \
398  G0 = vec_add(Y0, uvx0); \
399  B0 = vec_add(Y0, ux0); \
400  R1 = vec_add(Y1, vx1); \
401  G1 = vec_add(Y1, uvx1); \
402  B1 = vec_add(Y1, ux1); \
403  \
404  R = vec_packclp(R0, R1); \
405  G = vec_packclp(G0, G1); \
406  B = vec_packclp(B0, B1); \
407  \
408  out_pixels(R, G, B, oute); \
409  \
410  R0 = vec_add(Y2, vx0); \
411  G0 = vec_add(Y2, uvx0); \
412  B0 = vec_add(Y2, ux0); \
413  R1 = vec_add(Y3, vx1); \
414  G1 = vec_add(Y3, uvx1); \
415  B1 = vec_add(Y3, ux1); \
416  R = vec_packclp(R0, R1); \
417  G = vec_packclp(G0, G1); \
418  B = vec_packclp(B0, B1); \
419  \
420  \
421  out_pixels(R, G, B, outo); \
422  \
423  y1i += 16; \
424  y2i += 16; \
425  ui += 8; \
426  vi += 8; \
427  } \
428  \
429  ui += instrides_scl[1]; \
430  vi += instrides_scl[2]; \
431  y1i += instrides_scl[0]; \
432  y2i += instrides_scl[0]; \
433  } \
434  return srcSliceH; \
435 }
436 
437 #define out_abgr(a, b, c, ptr) \
438  vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
439 #define out_bgra(a, b, c, ptr) \
440  vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
441 #define out_rgba(a, b, c, ptr) \
442  vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
443 #define out_argb(a, b, c, ptr) \
444  vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
445 #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
446 #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
447 
448 DEFCSP420_CVT(yuv2_abgr, out_abgr)
449 DEFCSP420_CVT(yuv2_bgra, out_bgra)
450 DEFCSP420_CVT(yuv2_rgba, out_rgba)
451 DEFCSP420_CVT(yuv2_argb, out_argb)
452 DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
453 DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
454 
455 // uyvy|uyvy|uyvy|uyvy
456 // 0123 4567 89ab cdef
457 static const vector unsigned char
458  demux_u = { 0x10, 0x00, 0x10, 0x00,
459  0x10, 0x04, 0x10, 0x04,
460  0x10, 0x08, 0x10, 0x08,
461  0x10, 0x0c, 0x10, 0x0c },
462  demux_v = { 0x10, 0x02, 0x10, 0x02,
463  0x10, 0x06, 0x10, 0x06,
464  0x10, 0x0A, 0x10, 0x0A,
465  0x10, 0x0E, 0x10, 0x0E },
466  demux_y = { 0x10, 0x01, 0x10, 0x03,
467  0x10, 0x05, 0x10, 0x07,
468  0x10, 0x09, 0x10, 0x0B,
469  0x10, 0x0D, 0x10, 0x0F };
470 
471 /*
472  * this is so I can play live CCIR raw video
473  */
474 static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
475  int *instrides, int srcSliceY, int srcSliceH,
476  unsigned char **oplanes, int *outstrides)
477 {
478  int w = c->srcW;
479  int h = srcSliceH;
480  int i, j;
481  vector unsigned char uyvy;
482  vector signed short Y, U, V;
483  vector signed short R0, G0, B0, R1, G1, B1;
484  vector unsigned char R, G, B;
485  vector unsigned char *out;
486  const ubyte *img;
487 
488  img = in[0];
489  out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
490 
491  for (i = 0; i < h; i++)
492  for (j = 0; j < w / 16; j++) {
493  uyvy = vec_ld(0, img);
494 
495  U = (vector signed short)
496  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
497  V = (vector signed short)
498  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
499  Y = (vector signed short)
500  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
501 
502  cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
503 
504  uyvy = vec_ld(16, img);
505 
506  U = (vector signed short)
507  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
508  V = (vector signed short)
509  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
510  Y = (vector signed short)
511  vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
512 
513  cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
514 
515  R = vec_packclp(R0, R1);
516  G = vec_packclp(G0, G1);
517  B = vec_packclp(B0, B1);
518 
519  // vec_mstbgr24 (R,G,B, out);
520  out_rgba(R, G, B, out);
521 
522  img += 32;
523  }
524  return srcSliceH;
525 }
526 
527 /* Ok currently the acceleration routine only supports
528  * inputs of widths a multiple of 16
529  * and heights a multiple 2
530  *
531  * So we just fall back to the C codes for this.
532  */
534 {
536  return NULL;
537 
538  /*
539  * and this seems not to matter too much I tried a bunch of
540  * videos with abnormal widths and MPlayer crashes elsewhere.
541  * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
542  * boom with X11 bad match.
543  *
544  */
545  if ((c->srcW & 0xf) != 0)
546  return NULL;
547 
548  switch (c->srcFormat) {
549  case AV_PIX_FMT_YUV410P:
550  case AV_PIX_FMT_YUV420P:
551  /*case IMGFMT_CLPL: ??? */
552  case AV_PIX_FMT_GRAY8:
553  case AV_PIX_FMT_NV12:
554  case AV_PIX_FMT_NV21:
555  if ((c->srcH & 0x1) != 0)
556  return NULL;
557 
558  switch (c->dstFormat) {
559  case AV_PIX_FMT_RGB24:
560  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
561  return altivec_yuv2_rgb24;
562  case AV_PIX_FMT_BGR24:
563  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
564  return altivec_yuv2_bgr24;
565  case AV_PIX_FMT_ARGB:
566  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
567  return altivec_yuv2_argb;
568  case AV_PIX_FMT_ABGR:
569  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
570  return altivec_yuv2_abgr;
571  case AV_PIX_FMT_RGBA:
572  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
573  return altivec_yuv2_rgba;
574  case AV_PIX_FMT_BGRA:
575  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
576  return altivec_yuv2_bgra;
577  default: return NULL;
578  }
579  break;
580 
581  case AV_PIX_FMT_UYVY422:
582  switch (c->dstFormat) {
583  case AV_PIX_FMT_BGR32:
584  av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
585  return altivec_uyvy_rgb32;
586  default: return NULL;
587  }
588  break;
589  }
590  return NULL;
591 }
592 
594  const int inv_table[4],
595  int brightness,
596  int contrast,
597  int saturation)
598 {
599  union {
600  DECLARE_ALIGNED(16, signed short, tmp)[8];
601  vector signed short vec;
602  } buf;
603 
604  buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
605  buf.tmp[1] = -256 * brightness; // oy
606  buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
607  buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
608  buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
609  buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
610 
611  c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
612  c->CY = vec_splat((vector signed short) buf.vec, 0);
613  c->OY = vec_splat((vector signed short) buf.vec, 1);
614  c->CRV = vec_splat((vector signed short) buf.vec, 2);
615  c->CBU = vec_splat((vector signed short) buf.vec, 3);
616  c->CGU = vec_splat((vector signed short) buf.vec, 4);
617  c->CGV = vec_splat((vector signed short) buf.vec, 5);
618  return;
619 }
620 
622  const int16_t *lumFilter,
623  const int16_t **lumSrc,
624  int lumFilterSize,
625  const int16_t *chrFilter,
626  const int16_t **chrUSrc,
627  const int16_t **chrVSrc,
628  int chrFilterSize,
629  const int16_t **alpSrc,
630  uint8_t *dest,
631  int dstW, int dstY,
632  enum AVPixelFormat target)
633 {
634  int i, j;
635  vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
636  vector signed short R0, G0, B0, R1, G1, B1;
637 
638  vector unsigned char R, G, B;
639  vector unsigned char *out, *nout;
640 
641  vector signed short RND = vec_splat_s16(1 << 3);
642  vector unsigned short SCL = vec_splat_u16(4);
643  DECLARE_ALIGNED(16, unsigned int, scratch)[16];
644 
645  vector signed short *YCoeffs, *CCoeffs;
646 
647  YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
648  CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
649 
650  out = (vector unsigned char *) dest;
651 
652  for (i = 0; i < dstW; i += 16) {
653  Y0 = RND;
654  Y1 = RND;
655  /* extract 16 coeffs from lumSrc */
656  for (j = 0; j < lumFilterSize; j++) {
657  X0 = vec_ld(0, &lumSrc[j][i]);
658  X1 = vec_ld(16, &lumSrc[j][i]);
659  Y0 = vec_mradds(X0, YCoeffs[j], Y0);
660  Y1 = vec_mradds(X1, YCoeffs[j], Y1);
661  }
662 
663  U = RND;
664  V = RND;
665  /* extract 8 coeffs from U,V */
666  for (j = 0; j < chrFilterSize; j++) {
667  X = vec_ld(0, &chrUSrc[j][i / 2]);
668  U = vec_mradds(X, CCoeffs[j], U);
669  X = vec_ld(0, &chrVSrc[j][i / 2]);
670  V = vec_mradds(X, CCoeffs[j], V);
671  }
672 
673  /* scale and clip signals */
674  Y0 = vec_sra(Y0, SCL);
675  Y1 = vec_sra(Y1, SCL);
676  U = vec_sra(U, SCL);
677  V = vec_sra(V, SCL);
678 
679  Y0 = vec_clip_s16(Y0);
680  Y1 = vec_clip_s16(Y1);
681  U = vec_clip_s16(U);
682  V = vec_clip_s16(V);
683 
684  /* now we have
685  * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
686  * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
687  *
688  * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
689  * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
690  * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
691  */
692 
693  U0 = vec_mergeh(U, U);
694  V0 = vec_mergeh(V, V);
695 
696  U1 = vec_mergel(U, U);
697  V1 = vec_mergel(V, V);
698 
699  cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
700  cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
701 
702  R = vec_packclp(R0, R1);
703  G = vec_packclp(G0, G1);
704  B = vec_packclp(B0, B1);
705 
706  switch (target) {
707  case AV_PIX_FMT_ABGR:
708  out_abgr(R, G, B, out);
709  break;
710  case AV_PIX_FMT_BGRA:
711  out_bgra(R, G, B, out);
712  break;
713  case AV_PIX_FMT_RGBA:
714  out_rgba(R, G, B, out);
715  break;
716  case AV_PIX_FMT_ARGB:
717  out_argb(R, G, B, out);
718  break;
719  case AV_PIX_FMT_RGB24:
720  out_rgb24(R, G, B, out);
721  break;
722  case AV_PIX_FMT_BGR24:
723  out_bgr24(R, G, B, out);
724  break;
725  default:
726  {
727  /* If this is reached, the caller should have called yuv2packedXinC
728  * instead. */
729  static int printed_error_message;
730  if (!printed_error_message) {
731  av_log(c, AV_LOG_ERROR,
732  "altivec_yuv2packedX doesn't support %s output\n",
734  printed_error_message = 1;
735  }
736  return;
737  }
738  }
739  }
740 
741  if (i < dstW) {
742  i -= 16;
743 
744  Y0 = RND;
745  Y1 = RND;
746  /* extract 16 coeffs from lumSrc */
747  for (j = 0; j < lumFilterSize; j++) {
748  X0 = vec_ld(0, &lumSrc[j][i]);
749  X1 = vec_ld(16, &lumSrc[j][i]);
750  Y0 = vec_mradds(X0, YCoeffs[j], Y0);
751  Y1 = vec_mradds(X1, YCoeffs[j], Y1);
752  }
753 
754  U = RND;
755  V = RND;
756  /* extract 8 coeffs from U,V */
757  for (j = 0; j < chrFilterSize; j++) {
758  X = vec_ld(0, &chrUSrc[j][i / 2]);
759  U = vec_mradds(X, CCoeffs[j], U);
760  X = vec_ld(0, &chrVSrc[j][i / 2]);
761  V = vec_mradds(X, CCoeffs[j], V);
762  }
763 
764  /* scale and clip signals */
765  Y0 = vec_sra(Y0, SCL);
766  Y1 = vec_sra(Y1, SCL);
767  U = vec_sra(U, SCL);
768  V = vec_sra(V, SCL);
769 
770  Y0 = vec_clip_s16(Y0);
771  Y1 = vec_clip_s16(Y1);
772  U = vec_clip_s16(U);
773  V = vec_clip_s16(V);
774 
775  /* now we have
776  * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
777  * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
778  *
779  * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
780  * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
781  * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
782  */
783 
784  U0 = vec_mergeh(U, U);
785  V0 = vec_mergeh(V, V);
786 
787  U1 = vec_mergel(U, U);
788  V1 = vec_mergel(V, V);
789 
790  cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
791  cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
792 
793  R = vec_packclp(R0, R1);
794  G = vec_packclp(G0, G1);
795  B = vec_packclp(B0, B1);
796 
797  nout = (vector unsigned char *) scratch;
798  switch (target) {
799  case AV_PIX_FMT_ABGR:
800  out_abgr(R, G, B, nout);
801  break;
802  case AV_PIX_FMT_BGRA:
803  out_bgra(R, G, B, nout);
804  break;
805  case AV_PIX_FMT_RGBA:
806  out_rgba(R, G, B, nout);
807  break;
808  case AV_PIX_FMT_ARGB:
809  out_argb(R, G, B, nout);
810  break;
811  case AV_PIX_FMT_RGB24:
812  out_rgb24(R, G, B, nout);
813  break;
814  case AV_PIX_FMT_BGR24:
815  out_bgr24(R, G, B, nout);
816  break;
817  default:
818  /* Unreachable, I think. */
819  av_log(c, AV_LOG_ERROR,
820  "altivec_yuv2packedX doesn't support %s output\n",
822  return;
823  }
824 
825  memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
826  }
827 }
828 
829 #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
830 void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
831  const int16_t *lumFilter, \
832  const int16_t **lumSrc, \
833  int lumFilterSize, \
834  const int16_t *chrFilter, \
835  const int16_t **chrUSrc, \
836  const int16_t **chrVSrc, \
837  int chrFilterSize, \
838  const int16_t **alpSrc, \
839  uint8_t *dest, int dstW, int dstY) \
840 { \
841  yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
842  chrFilter, chrUSrc, chrVSrc, \
843  chrFilterSize, alpSrc, \
844  dest, dstW, dstY, pixfmt); \
845 }
846