]> git.sesse.net Git - ffmpeg/blob - libswscale/ppc/yuv2rgb_altivec.c
h264: stricter reference limit enforcement.
[ffmpeg] / libswscale / ppc / yuv2rgb_altivec.c
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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"
97 #include "libswscale/swscale_internal.h"
98 #include "libavutil/cpu.h"
99 #include "yuv2rgb_altivec.h"
100
101 #undef PROFILE_THE_BEAST
102 #undef INC_SCALING
103
104 typedef unsigned char ubyte;
105 typedef signed char   sbyte;
106
107 /* RGB interleaver, 16 planar pels 8-bit samples per channel in
108  * homogeneous vector registers x0,x1,x2 are interleaved with the
109  * following technique:
110  *
111  *    o0 = vec_mergeh(x0, x1);
112  *    o1 = vec_perm(o0, x2, perm_rgb_0);
113  *    o2 = vec_perm(o0, x2, perm_rgb_1);
114  *    o3 = vec_mergel(x0, x1);
115  *    o4 = vec_perm(o3, o2, perm_rgb_2);
116  *    o5 = vec_perm(o3, o2, perm_rgb_3);
117  *
118  * perm_rgb_0:   o0(RG).h v1(B) --> o1*
119  *            0   1  2   3   4
120  *           rgbr|gbrg|brgb|rgbr
121  *           0010 0100 1001 0010
122  *           0102 3145 2673 894A
123  *
124  * perm_rgb_1:   o0(RG).h v1(B) --> o2
125  *            0   1  2   3   4
126  *           gbrg|brgb|bbbb|bbbb
127  *           0100 1001 1111 1111
128  *           B5CD 6EF7 89AB CDEF
129  *
130  * perm_rgb_2:   o3(RG).l o2(rgbB.l) --> o4*
131  *            0   1  2   3   4
132  *           gbrg|brgb|rgbr|gbrg
133  *           1111 1111 0010 0100
134  *           89AB CDEF 0182 3945
135  *
136  * perm_rgb_2:   o3(RG).l o2(rgbB.l) ---> o5*
137  *            0   1  2   3   4
138  *           brgb|rgbr|gbrg|brgb
139  *           1001 0010 0100 1001
140  *           a67b 89cA BdCD eEFf
141  *
142  */
143 static const vector unsigned char
144     perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
145                    0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
146     perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
147                    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
148     perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
149                    0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
150     perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
151                    0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
152
153 #define vec_merge3(x2, x1, x0, y0, y1, y2)     \
154     do {                                       \
155         __typeof__(x0) o0, o2, o3;             \
156         o0 = vec_mergeh(x0, x1);               \
157         y0 = vec_perm(o0, x2, perm_rgb_0);     \
158         o2 = vec_perm(o0, x2, perm_rgb_1);     \
159         o3 = vec_mergel(x0, x1);               \
160         y1 = vec_perm(o3, o2, perm_rgb_2);     \
161         y2 = vec_perm(o3, o2, perm_rgb_3);     \
162     } while (0)
163
164 #define vec_mstbgr24(x0, x1, x2, ptr)          \
165     do {                                       \
166         __typeof__(x0) _0, _1, _2;             \
167         vec_merge3(x0, x1, x2, _0, _1, _2);    \
168         vec_st(_0, 0, ptr++);                  \
169         vec_st(_1, 0, ptr++);                  \
170         vec_st(_2, 0, ptr++);                  \
171     } while (0)
172
173 #define vec_mstrgb24(x0, x1, x2, ptr)          \
174     do {                                       \
175         __typeof__(x0) _0, _1, _2;             \
176         vec_merge3(x2, x1, x0, _0, _1, _2);    \
177         vec_st(_0, 0, ptr++);                  \
178         vec_st(_1, 0, ptr++);                  \
179         vec_st(_2, 0, ptr++);                  \
180     } while (0)
181
182 /* pack the pixels in rgb0 format
183  * msb R
184  * lsb 0
185  */
186 #define vec_mstrgb32(T, x0, x1, x2, x3, ptr)                            \
187     do {                                                                \
188         T _0, _1, _2, _3;                                               \
189         _0 = vec_mergeh(x0, x1);                                        \
190         _1 = vec_mergeh(x2, x3);                                        \
191         _2 = (T) vec_mergeh((vector unsigned short) _0,                 \
192                             (vector unsigned short) _1);                \
193         _3 = (T) vec_mergel((vector unsigned short) _0,                 \
194                             (vector unsigned short) _1);                \
195         vec_st(_2, 0 * 16, (T *) ptr);                                  \
196         vec_st(_3, 1 * 16, (T *) ptr);                                  \
197         _0 = vec_mergel(x0, x1);                                        \
198         _1 = vec_mergel(x2, x3);                                        \
199         _2 = (T) vec_mergeh((vector unsigned short) _0,                 \
200                             (vector unsigned short) _1);                \
201         _3 = (T) vec_mergel((vector unsigned short) _0,                 \
202                             (vector unsigned short) _1);                \
203         vec_st(_2, 2 * 16, (T *) ptr);                                  \
204         vec_st(_3, 3 * 16, (T *) ptr);                                  \
205         ptr += 4;                                                       \
206     } while (0)
207
208 /*
209  * 1     0       1.4021   | | Y |
210  * 1    -0.3441 -0.7142   |x| Cb|
211  * 1     1.7718  0        | | Cr|
212  *
213  *
214  * Y:      [-128 127]
215  * Cb/Cr : [-128 127]
216  *
217  * typical YUV conversion works on Y: 0-255 this version has been
218  * optimized for JPEG decoding.
219  */
220
221 #define vec_unh(x)                                                      \
222     (vector signed short)                                               \
223         vec_perm(x, (__typeof__(x)) { 0 },                              \
224                  ((vector unsigned char) {                              \
225                      0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03,    \
226                      0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
227
228 #define vec_unl(x)                                                      \
229     (vector signed short)                                               \
230         vec_perm(x, (__typeof__(x)) { 0 },                              \
231                  ((vector unsigned char) {                              \
232                      0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B,    \
233                      0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
234
235 #define vec_clip_s16(x)                                                 \
236     vec_max(vec_min(x, ((vector signed short) {                         \
237                     235, 235, 235, 235, 235, 235, 235, 235 })),         \
238             ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
239
240 #define vec_packclp(x, y)                                               \
241     (vector unsigned char)                                              \
242         vec_packs((vector unsigned short)                               \
243                       vec_max(x, ((vector signed short) { 0 })),        \
244                   (vector unsigned short)                               \
245                       vec_max(y, ((vector signed short) { 0 })))
246
247 //#define out_pixels(a, b, c, ptr) vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, a, a, ptr)
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     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 =                                              \
320         (vector unsigned char *)                                              \
321             (oplanes[0] + srcSliceY * outstrides[0]);                         \
322     vector unsigned char *outo =                                              \
323         (vector unsigned char *)                                              \
324             (oplanes[0] + srcSliceY * outstrides[0] + outstrides[0]);         \
325                                                                               \
326     /* loop moves y{1, 2}i by w */                                            \
327     instrides_scl[0] = instrides[0] * 2 - w;                                  \
328     /* loop moves ui by w / 2 */                                              \
329     instrides_scl[1] = instrides[1] - w / 2;                                  \
330     /* loop moves vi by w / 2 */                                              \
331     instrides_scl[2] = instrides[2] - w / 2;                                  \
332                                                                               \
333     for (i = 0; i < h / 2; i++) {                                             \
334         vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0);       \
335         vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1);       \
336                                                                               \
337         for (j = 0; j < w / 16; j++) {                                        \
338             y1ivP = (vector unsigned char *) y1i;                             \
339             y2ivP = (vector unsigned char *) y2i;                             \
340             uivP  = (vector unsigned char *) ui;                              \
341             vivP  = (vector unsigned char *) vi;                              \
342                                                                               \
343             align_perm = vec_lvsl(0, y1i);                                    \
344             y0 = (vector unsigned char)                                       \
345                      vec_perm(y1ivP[0], y1ivP[1], align_perm);                \
346                                                                               \
347             align_perm = vec_lvsl(0, y2i);                                    \
348             y1 = (vector unsigned char)                                       \
349                      vec_perm(y2ivP[0], y2ivP[1], align_perm);                \
350                                                                               \
351             align_perm = vec_lvsl(0, ui);                                     \
352             u = (vector signed char)                                          \
353                     vec_perm(uivP[0], uivP[1], align_perm);                   \
354                                                                               \
355             align_perm = vec_lvsl(0, vi);                                     \
356             v = (vector signed char)                                          \
357                     vec_perm(vivP[0], vivP[1], align_perm);                   \
358                                                                               \
359             u = (vector signed char)                                          \
360                     vec_sub(u,                                                \
361                             (vector signed char)                              \
362                                 vec_splat((vector signed char) { 128 }, 0));  \
363             v = (vector signed char)                                          \
364                     vec_sub(v,                                                \
365                             (vector signed char)                              \
366                                 vec_splat((vector signed char) { 128 }, 0));  \
367                                                                               \
368             U = vec_unpackh(u);                                               \
369             V = vec_unpackh(v);                                               \
370                                                                               \
371             Y0 = vec_unh(y0);                                                 \
372             Y1 = vec_unl(y0);                                                 \
373             Y2 = vec_unh(y1);                                                 \
374             Y3 = vec_unl(y1);                                                 \
375                                                                               \
376             Y0 = vec_mradds(Y0, lCY, lOY);                                    \
377             Y1 = vec_mradds(Y1, lCY, lOY);                                    \
378             Y2 = vec_mradds(Y2, lCY, lOY);                                    \
379             Y3 = vec_mradds(Y3, lCY, lOY);                                    \
380                                                                               \
381             /* ux  = (CBU * (u << CSHIFT) + 0x4000) >> 15 */                  \
382             ux  = vec_sl(U, lCSHIFT);                                         \
383             ux  = vec_mradds(ux, lCBU, (vector signed short) { 0 });          \
384             ux0 = vec_mergeh(ux, ux);                                         \
385             ux1 = vec_mergel(ux, ux);                                         \
386                                                                               \
387             /* vx  = (CRV * (v << CSHIFT) + 0x4000) >> 15; */                 \
388             vx  = vec_sl(V, lCSHIFT);                                         \
389             vx  = vec_mradds(vx, lCRV, (vector signed short) { 0 });          \
390             vx0 = vec_mergeh(vx, vx);                                         \
391             vx1 = vec_mergel(vx, vx);                                         \
392                                                                               \
393             /* uvx = ((CGU * u) + (CGV * v)) >> 15 */                         \
394             uvx  = vec_mradds(U, lCGU, (vector signed short) { 0 });          \
395             uvx  = vec_mradds(V, lCGV, uvx);                                  \
396             uvx0 = vec_mergeh(uvx, uvx);                                      \
397             uvx1 = vec_mergel(uvx, uvx);                                      \
398                                                                               \
399             R0 = vec_add(Y0, vx0);                                            \
400             G0 = vec_add(Y0, uvx0);                                           \
401             B0 = vec_add(Y0, ux0);                                            \
402             R1 = vec_add(Y1, vx1);                                            \
403             G1 = vec_add(Y1, uvx1);                                           \
404             B1 = vec_add(Y1, ux1);                                            \
405                                                                               \
406             R = vec_packclp(R0, R1);                                          \
407             G = vec_packclp(G0, G1);                                          \
408             B = vec_packclp(B0, B1);                                          \
409                                                                               \
410             out_pixels(R, G, B, oute);                                        \
411                                                                               \
412             R0 = vec_add(Y2, vx0);                                            \
413             G0 = vec_add(Y2, uvx0);                                           \
414             B0 = vec_add(Y2, ux0);                                            \
415             R1 = vec_add(Y3, vx1);                                            \
416             G1 = vec_add(Y3, uvx1);                                           \
417             B1 = vec_add(Y3, ux1);                                            \
418             R  = vec_packclp(R0, R1);                                         \
419             G  = vec_packclp(G0, G1);                                         \
420             B  = vec_packclp(B0, B1);                                         \
421                                                                               \
422                                                                               \
423             out_pixels(R, G, B, outo);                                        \
424                                                                               \
425             y1i += 16;                                                        \
426             y2i += 16;                                                        \
427             ui  += 8;                                                         \
428             vi  += 8;                                                         \
429         }                                                                     \
430                                                                               \
431         outo += (outstrides[0]) >> 4;                                         \
432         oute += (outstrides[0]) >> 4;                                         \
433                                                                               \
434         ui  += instrides_scl[1];                                              \
435         vi  += instrides_scl[2];                                              \
436         y1i += instrides_scl[0];                                              \
437         y2i += instrides_scl[0];                                              \
438     }                                                                         \
439     return srcSliceH;                                                         \
440 }
441
442 #define out_abgr(a, b, c, ptr)                                          \
443     vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
444 #define out_bgra(a, b, c, ptr)                                          \
445     vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
446 #define out_rgba(a, b, c, ptr)                                          \
447     vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
448 #define out_argb(a, b, c, ptr)                                          \
449     vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
450 #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
451 #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
452
453 DEFCSP420_CVT(yuv2_abgr,  out_abgr)
454 DEFCSP420_CVT(yuv2_bgra,  out_bgra)
455 DEFCSP420_CVT(yuv2_rgba,  out_rgba)
456 DEFCSP420_CVT(yuv2_argb,  out_argb)
457 DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
458 DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
459
460 // uyvy|uyvy|uyvy|uyvy
461 // 0123 4567 89ab cdef
462 static const vector unsigned char
463     demux_u = { 0x10, 0x00, 0x10, 0x00,
464                 0x10, 0x04, 0x10, 0x04,
465                 0x10, 0x08, 0x10, 0x08,
466                 0x10, 0x0c, 0x10, 0x0c },
467     demux_v = { 0x10, 0x02, 0x10, 0x02,
468                 0x10, 0x06, 0x10, 0x06,
469                 0x10, 0x0A, 0x10, 0x0A,
470                 0x10, 0x0E, 0x10, 0x0E },
471     demux_y = { 0x10, 0x01, 0x10, 0x03,
472                 0x10, 0x05, 0x10, 0x07,
473                 0x10, 0x09, 0x10, 0x0B,
474                 0x10, 0x0D, 0x10, 0x0F };
475
476 /*
477  * this is so I can play live CCIR raw video
478  */
479 static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
480                               int *instrides, int srcSliceY, int srcSliceH,
481                               unsigned char **oplanes, int *outstrides)
482 {
483     int w = c->srcW;
484     int h = srcSliceH;
485     int i, j;
486     vector unsigned char uyvy;
487     vector signed short Y, U, V;
488     vector signed short R0, G0, B0, R1, G1, B1;
489     vector unsigned char R, G, B;
490     vector unsigned char *out;
491     const ubyte *img;
492
493     img = in[0];
494     out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
495
496     for (i = 0; i < h; i++)
497         for (j = 0; j < w / 16; j++) {
498             uyvy = vec_ld(0, img);
499
500             U = (vector signed short)
501                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
502             V = (vector signed short)
503                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
504             Y = (vector signed short)
505                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
506
507             cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
508
509             uyvy = vec_ld(16, img);
510
511             U = (vector signed short)
512                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
513             V = (vector signed short)
514                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
515             Y = (vector signed short)
516                     vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
517
518             cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
519
520             R = vec_packclp(R0, R1);
521             G = vec_packclp(G0, G1);
522             B = vec_packclp(B0, B1);
523
524             // vec_mstbgr24 (R,G,B, out);
525             out_rgba(R, G, B, out);
526
527             img += 32;
528         }
529     return srcSliceH;
530 }
531
532 /* Ok currently the acceleration routine only supports
533  * inputs of widths a multiple of 16
534  * and heights a multiple 2
535  *
536  * So we just fall back to the C codes for this.
537  */
538 SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
539 {
540     if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
541         return NULL;
542
543     /*
544      * and this seems not to matter too much I tried a bunch of
545      * videos with abnormal widths and MPlayer crashes elsewhere.
546      * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
547      * boom with X11 bad match.
548      *
549      */
550     if ((c->srcW & 0xf) != 0)
551         return NULL;
552
553     switch (c->srcFormat) {
554     case PIX_FMT_YUV410P:
555     case PIX_FMT_YUV420P:
556     /*case IMGFMT_CLPL:        ??? */
557     case PIX_FMT_GRAY8:
558     case PIX_FMT_NV12:
559     case PIX_FMT_NV21:
560         if ((c->srcH & 0x1) != 0)
561             return NULL;
562
563         switch (c->dstFormat) {
564         case PIX_FMT_RGB24:
565             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
566             return altivec_yuv2_rgb24;
567         case PIX_FMT_BGR24:
568             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
569             return altivec_yuv2_bgr24;
570         case PIX_FMT_ARGB:
571             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
572             return altivec_yuv2_argb;
573         case PIX_FMT_ABGR:
574             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
575             return altivec_yuv2_abgr;
576         case PIX_FMT_RGBA:
577             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
578             return altivec_yuv2_rgba;
579         case PIX_FMT_BGRA:
580             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
581             return altivec_yuv2_bgra;
582         default: return NULL;
583         }
584         break;
585
586     case PIX_FMT_UYVY422:
587         switch (c->dstFormat) {
588         case PIX_FMT_BGR32:
589             av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
590             return altivec_uyvy_rgb32;
591         default: return NULL;
592         }
593         break;
594     }
595     return NULL;
596 }
597
598 void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
599                                     int brightness, int contrast,
600                                     int saturation)
601 {
602     union {
603         DECLARE_ALIGNED(16, signed short, tmp)[8];
604         vector signed short vec;
605     } buf;
606
607     buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9;                               // cy
608     buf.tmp[1] = -256 * brightness;                                               // oy
609     buf.tmp[2] =   (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16);   // crv
610     buf.tmp[3] =   (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16);   // cbu
611     buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16));  // cgu
612     buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16));  // cgv
613
614     c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
615     c->CY     = vec_splat((vector signed short) buf.vec, 0);
616     c->OY     = vec_splat((vector signed short) buf.vec, 1);
617     c->CRV    = vec_splat((vector signed short) buf.vec, 2);
618     c->CBU    = vec_splat((vector signed short) buf.vec, 3);
619     c->CGU    = vec_splat((vector signed short) buf.vec, 4);
620     c->CGV    = vec_splat((vector signed short) buf.vec, 5);
621     return;
622 }
623
624 static av_always_inline void ff_yuv2packedX_altivec(SwsContext *c,
625                                                     const int16_t *lumFilter,
626                                                     const int16_t **lumSrc,
627                                                     int lumFilterSize,
628                                                     const int16_t *chrFilter,
629                                                     const int16_t **chrUSrc,
630                                                     const int16_t **chrVSrc,
631                                                     int chrFilterSize,
632                                                     const int16_t **alpSrc,
633                                                     uint8_t *dest,
634                                                     int dstW, int dstY,
635                                                     enum PixelFormat target)
636 {
637     int i, j;
638     vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
639     vector signed short R0, G0, B0, R1, G1, B1;
640
641     vector unsigned char R, G, B;
642     vector unsigned char *out, *nout;
643
644     vector signed short RND   = vec_splat_s16(1 << 3);
645     vector unsigned short SCL = vec_splat_u16(4);
646     DECLARE_ALIGNED(16, unsigned int, scratch)[16];
647
648     vector signed short *YCoeffs, *CCoeffs;
649
650     YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
651     CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
652
653     out = (vector unsigned char *) dest;
654
655     for (i = 0; i < dstW; i += 16) {
656         Y0 = RND;
657         Y1 = RND;
658         /* extract 16 coeffs from lumSrc */
659         for (j = 0; j < lumFilterSize; j++) {
660             X0 = vec_ld(0, &lumSrc[j][i]);
661             X1 = vec_ld(16, &lumSrc[j][i]);
662             Y0 = vec_mradds(X0, YCoeffs[j], Y0);
663             Y1 = vec_mradds(X1, YCoeffs[j], Y1);
664         }
665
666         U = RND;
667         V = RND;
668         /* extract 8 coeffs from U,V */
669         for (j = 0; j < chrFilterSize; j++) {
670             X = vec_ld(0, &chrUSrc[j][i / 2]);
671             U = vec_mradds(X, CCoeffs[j], U);
672             X = vec_ld(0, &chrVSrc[j][i / 2]);
673             V = vec_mradds(X, CCoeffs[j], V);
674         }
675
676         /* scale and clip signals */
677         Y0 = vec_sra(Y0, SCL);
678         Y1 = vec_sra(Y1, SCL);
679         U  = vec_sra(U, SCL);
680         V  = vec_sra(V, SCL);
681
682         Y0 = vec_clip_s16(Y0);
683         Y1 = vec_clip_s16(Y1);
684         U  = vec_clip_s16(U);
685         V  = vec_clip_s16(V);
686
687         /* now we have
688          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
689          * U  = u0 u1 u2 u3 u4 u5 u6 u7    V  = v0 v1 v2 v3 v4 v5 v6 v7
690          *
691          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
692          * U0 = u0 u0 u1 u1 u2 u2 u3 u3    U1 = u4 u4 u5 u5 u6 u6 u7 u7
693          * V0 = v0 v0 v1 v1 v2 v2 v3 v3    V1 = v4 v4 v5 v5 v6 v6 v7 v7
694          */
695
696         U0 = vec_mergeh(U, U);
697         V0 = vec_mergeh(V, V);
698
699         U1 = vec_mergel(U, U);
700         V1 = vec_mergel(V, V);
701
702         cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
703         cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
704
705         R = vec_packclp(R0, R1);
706         G = vec_packclp(G0, G1);
707         B = vec_packclp(B0, B1);
708
709         switch (target) {
710         case PIX_FMT_ABGR:
711             out_abgr(R, G, B, out);
712             break;
713         case PIX_FMT_BGRA:
714             out_bgra(R, G, B, out);
715             break;
716         case PIX_FMT_RGBA:
717             out_rgba(R, G, B, out);
718             break;
719         case PIX_FMT_ARGB:
720             out_argb(R, G, B, out);
721             break;
722         case PIX_FMT_RGB24:
723             out_rgb24(R, G, B, out);
724             break;
725         case PIX_FMT_BGR24:
726             out_bgr24(R, G, B, out);
727             break;
728         default:
729         {
730             /* If this is reached, the caller should have called yuv2packedXinC
731              * instead. */
732             static int printed_error_message;
733             if (!printed_error_message) {
734                 av_log(c, AV_LOG_ERROR,
735                        "altivec_yuv2packedX doesn't support %s output\n",
736                        sws_format_name(c->dstFormat));
737                 printed_error_message = 1;
738             }
739             return;
740         }
741         }
742     }
743
744     if (i < dstW) {
745         i -= 16;
746
747         Y0 = RND;
748         Y1 = RND;
749         /* extract 16 coeffs from lumSrc */
750         for (j = 0; j < lumFilterSize; j++) {
751             X0 = vec_ld(0, &lumSrc[j][i]);
752             X1 = vec_ld(16, &lumSrc[j][i]);
753             Y0 = vec_mradds(X0, YCoeffs[j], Y0);
754             Y1 = vec_mradds(X1, YCoeffs[j], Y1);
755         }
756
757         U = RND;
758         V = RND;
759         /* extract 8 coeffs from U,V */
760         for (j = 0; j < chrFilterSize; j++) {
761             X = vec_ld(0, &chrUSrc[j][i / 2]);
762             U = vec_mradds(X, CCoeffs[j], U);
763             X = vec_ld(0, &chrVSrc[j][i / 2]);
764             V = vec_mradds(X, CCoeffs[j], V);
765         }
766
767         /* scale and clip signals */
768         Y0 = vec_sra(Y0, SCL);
769         Y1 = vec_sra(Y1, SCL);
770         U  = vec_sra(U, SCL);
771         V  = vec_sra(V, SCL);
772
773         Y0 = vec_clip_s16(Y0);
774         Y1 = vec_clip_s16(Y1);
775         U  = vec_clip_s16(U);
776         V  = vec_clip_s16(V);
777
778         /* now we have
779          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
780          * U  = u0 u1 u2 u3 u4 u5 u6 u7    V  = v0 v1 v2 v3 v4 v5 v6 v7
781          *
782          * Y0 = y0 y1 y2 y3 y4 y5 y6 y7    Y1 = y8 y9 y10 y11 y12 y13 y14 y15
783          * U0 = u0 u0 u1 u1 u2 u2 u3 u3    U1 = u4 u4 u5 u5 u6 u6 u7 u7
784          * V0 = v0 v0 v1 v1 v2 v2 v3 v3    V1 = v4 v4 v5 v5 v6 v6 v7 v7
785          */
786
787         U0 = vec_mergeh(U, U);
788         V0 = vec_mergeh(V, V);
789
790         U1 = vec_mergel(U, U);
791         V1 = vec_mergel(V, V);
792
793         cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
794         cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
795
796         R = vec_packclp(R0, R1);
797         G = vec_packclp(G0, G1);
798         B = vec_packclp(B0, B1);
799
800         nout = (vector unsigned char *) scratch;
801         switch (target) {
802         case PIX_FMT_ABGR:
803             out_abgr(R, G, B, nout);
804             break;
805         case PIX_FMT_BGRA:
806             out_bgra(R, G, B, nout);
807             break;
808         case PIX_FMT_RGBA:
809             out_rgba(R, G, B, nout);
810             break;
811         case PIX_FMT_ARGB:
812             out_argb(R, G, B, nout);
813             break;
814         case PIX_FMT_RGB24:
815             out_rgb24(R, G, B, nout);
816             break;
817         case PIX_FMT_BGR24:
818             out_bgr24(R, G, B, nout);
819             break;
820         default:
821             /* Unreachable, I think. */
822             av_log(c, AV_LOG_ERROR,
823                    "altivec_yuv2packedX doesn't support %s output\n",
824                    sws_format_name(c->dstFormat));
825             return;
826         }
827
828         memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
829     }
830 }
831
832 #define YUV2PACKEDX_WRAPPER(suffix, pixfmt)                             \
833 void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c,                     \
834                                      const int16_t *lumFilter,          \
835                                      const int16_t **lumSrc,            \
836                                      int lumFilterSize,                 \
837                                      const int16_t *chrFilter,          \
838                                      const int16_t **chrUSrc,           \
839                                      const int16_t **chrVSrc,           \
840                                      int chrFilterSize,                 \
841                                      const int16_t **alpSrc,            \
842                                      uint8_t *dest, int dstW, int dstY) \
843 {                                                                       \
844     ff_yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize,         \
845                            chrFilter, chrUSrc, chrVSrc,                 \
846                            chrFilterSize, alpSrc,                       \
847                            dest, dstW, dstY, pixfmt);                   \
848 }
849
850 YUV2PACKEDX_WRAPPER(abgr,  PIX_FMT_ABGR);
851 YUV2PACKEDX_WRAPPER(bgra,  PIX_FMT_BGRA);
852 YUV2PACKEDX_WRAPPER(argb,  PIX_FMT_ARGB);
853 YUV2PACKEDX_WRAPPER(rgba,  PIX_FMT_RGBA);
854 YUV2PACKEDX_WRAPPER(rgb24, PIX_FMT_RGB24);
855 YUV2PACKEDX_WRAPPER(bgr24, PIX_FMT_BGR24);