2 * AltiVec acceleration for colorspace conversion
4 * copyright (C) 2004 Marc Hoffman <marc.hoffman@analog.com>
6 * This file is part of FFmpeg.
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.
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.
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
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.
30 * Lots of optimizations to be done here.
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.
35 * 2. The inefficient use of chroma loading needs a bit of brushing up.
37 * 3. Analysis of pipeline stalls needs to be done. Use shark to identify
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.
47 * ADDED altivec_yuv2packedX vertical scl + RGB converter
50 * PERFORMANCE ANALYSIS
52 * The C version uses 25% of the processor or ~250Mips for D1 video rawvideo
54 * The AltiVec version uses 10% of the processor or ~100Mips for D1 video
57 * 720 * 480 * 30 ~10MPS
59 * so we have roughly 10 clocks per pixel. This is too high, something has
62 * OPTIMIZED clip codes to utilize vec_max and vec_packs removing the
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.
72 * Now memcpy is the largest mips consumer in the system, probably due
73 * to the inefficient X11 stuff.
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.)
80 * GL2 libraries work now with patch for RGB32.
82 * NOTE: quartz vo driver ARGB32_to_RGB24 consumes 30% of the processor.
84 * Integrated luma prescaling adjustment for saturation/contrast/brightness
95 #include "libswscale/rgb2rgb.h"
96 #include "libswscale/swscale.h"
97 #include "libswscale/swscale_internal.h"
98 #include "libavutil/cpu.h"
99 #include "libavutil/pixdesc.h"
100 #include "yuv2rgb_altivec.h"
102 #undef PROFILE_THE_BEAST
105 typedef unsigned char ubyte;
106 typedef signed char sbyte;
108 /* RGB interleaver, 16 planar pels 8-bit samples per channel in
109 * homogeneous vector registers x0,x1,x2 are interleaved with the
110 * following technique:
112 * o0 = vec_mergeh(x0, x1);
113 * o1 = vec_perm(o0, x2, perm_rgb_0);
114 * o2 = vec_perm(o0, x2, perm_rgb_1);
115 * o3 = vec_mergel(x0, x1);
116 * o4 = vec_perm(o3, o2, perm_rgb_2);
117 * o5 = vec_perm(o3, o2, perm_rgb_3);
119 * perm_rgb_0: o0(RG).h v1(B) --> o1*
121 * rgbr|gbrg|brgb|rgbr
122 * 0010 0100 1001 0010
123 * 0102 3145 2673 894A
125 * perm_rgb_1: o0(RG).h v1(B) --> o2
127 * gbrg|brgb|bbbb|bbbb
128 * 0100 1001 1111 1111
129 * B5CD 6EF7 89AB CDEF
131 * perm_rgb_2: o3(RG).l o2(rgbB.l) --> o4*
133 * gbrg|brgb|rgbr|gbrg
134 * 1111 1111 0010 0100
135 * 89AB CDEF 0182 3945
137 * perm_rgb_2: o3(RG).l o2(rgbB.l) ---> o5*
139 * brgb|rgbr|gbrg|brgb
140 * 1001 0010 0100 1001
141 * a67b 89cA BdCD eEFf
144 static const vector unsigned char
145 perm_rgb_0 = { 0x00, 0x01, 0x10, 0x02, 0x03, 0x11, 0x04, 0x05,
146 0x12, 0x06, 0x07, 0x13, 0x08, 0x09, 0x14, 0x0a },
147 perm_rgb_1 = { 0x0b, 0x15, 0x0c, 0x0d, 0x16, 0x0e, 0x0f, 0x17,
148 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f },
149 perm_rgb_2 = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
150 0x00, 0x01, 0x18, 0x02, 0x03, 0x19, 0x04, 0x05 },
151 perm_rgb_3 = { 0x1a, 0x06, 0x07, 0x1b, 0x08, 0x09, 0x1c, 0x0a,
152 0x0b, 0x1d, 0x0c, 0x0d, 0x1e, 0x0e, 0x0f, 0x1f };
154 #define vec_merge3(x2, x1, x0, y0, y1, y2) \
156 __typeof__(x0) o0, o2, o3; \
157 o0 = vec_mergeh(x0, x1); \
158 y0 = vec_perm(o0, x2, perm_rgb_0); \
159 o2 = vec_perm(o0, x2, perm_rgb_1); \
160 o3 = vec_mergel(x0, x1); \
161 y1 = vec_perm(o3, o2, perm_rgb_2); \
162 y2 = vec_perm(o3, o2, perm_rgb_3); \
165 #define vec_mstbgr24(x0, x1, x2, ptr) \
167 __typeof__(x0) _0, _1, _2; \
168 vec_merge3(x0, x1, x2, _0, _1, _2); \
169 vec_st(_0, 0, ptr++); \
170 vec_st(_1, 0, ptr++); \
171 vec_st(_2, 0, ptr++); \
174 #define vec_mstrgb24(x0, x1, x2, ptr) \
176 __typeof__(x0) _0, _1, _2; \
177 vec_merge3(x2, x1, x0, _0, _1, _2); \
178 vec_st(_0, 0, ptr++); \
179 vec_st(_1, 0, ptr++); \
180 vec_st(_2, 0, ptr++); \
183 /* pack the pixels in rgb0 format
187 #define vec_mstrgb32(T, x0, x1, x2, x3, ptr) \
190 _0 = vec_mergeh(x0, x1); \
191 _1 = vec_mergeh(x2, x3); \
192 _2 = (T) vec_mergeh((vector unsigned short) _0, \
193 (vector unsigned short) _1); \
194 _3 = (T) vec_mergel((vector unsigned short) _0, \
195 (vector unsigned short) _1); \
196 vec_st(_2, 0 * 16, (T *) ptr); \
197 vec_st(_3, 1 * 16, (T *) ptr); \
198 _0 = vec_mergel(x0, x1); \
199 _1 = vec_mergel(x2, x3); \
200 _2 = (T) vec_mergeh((vector unsigned short) _0, \
201 (vector unsigned short) _1); \
202 _3 = (T) vec_mergel((vector unsigned short) _0, \
203 (vector unsigned short) _1); \
204 vec_st(_2, 2 * 16, (T *) ptr); \
205 vec_st(_3, 3 * 16, (T *) ptr); \
211 * 1 -0.3441 -0.7142 |x| Cb|
218 * typical YUV conversion works on Y: 0-255 this version has been
219 * optimized for JPEG decoding.
223 (vector signed short) \
224 vec_perm(x, (__typeof__(x)) { 0 }, \
225 ((vector unsigned char) { \
226 0x10, 0x00, 0x10, 0x01, 0x10, 0x02, 0x10, 0x03, \
227 0x10, 0x04, 0x10, 0x05, 0x10, 0x06, 0x10, 0x07 }))
230 (vector signed short) \
231 vec_perm(x, (__typeof__(x)) { 0 }, \
232 ((vector unsigned char) { \
233 0x10, 0x08, 0x10, 0x09, 0x10, 0x0A, 0x10, 0x0B, \
234 0x10, 0x0C, 0x10, 0x0D, 0x10, 0x0E, 0x10, 0x0F }))
236 #define vec_clip_s16(x) \
237 vec_max(vec_min(x, ((vector signed short) { \
238 235, 235, 235, 235, 235, 235, 235, 235 })), \
239 ((vector signed short) { 16, 16, 16, 16, 16, 16, 16, 16 }))
241 #define vec_packclp(x, y) \
242 (vector unsigned char) \
243 vec_packs((vector unsigned short) \
244 vec_max(x, ((vector signed short) { 0 })), \
245 (vector unsigned short) \
246 vec_max(y, ((vector signed short) { 0 })))
248 //#define out_pixels(a, b, c, ptr) vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, a, a, ptr)
250 static inline void cvtyuvtoRGB(SwsContext *c, vector signed short Y,
251 vector signed short U, vector signed short V,
252 vector signed short *R, vector signed short *G,
253 vector signed short *B)
255 vector signed short vx, ux, uvx;
257 Y = vec_mradds(Y, c->CY, c->OY);
258 U = vec_sub(U, (vector signed short)
259 vec_splat((vector signed short) { 128 }, 0));
260 V = vec_sub(V, (vector signed short)
261 vec_splat((vector signed short) { 128 }, 0));
263 // ux = (CBU * (u << c->CSHIFT) + 0x4000) >> 15;
264 ux = vec_sl(U, c->CSHIFT);
265 *B = vec_mradds(ux, c->CBU, Y);
267 // vx = (CRV * (v << c->CSHIFT) + 0x4000) >> 15;
268 vx = vec_sl(V, c->CSHIFT);
269 *R = vec_mradds(vx, c->CRV, Y);
271 // uvx = ((CGU * u) + (CGV * v)) >> 15;
272 uvx = vec_mradds(U, c->CGU, Y);
273 *G = vec_mradds(V, c->CGV, uvx);
277 * ------------------------------------------------------------------------------
279 * ------------------------------------------------------------------------------
282 #define DEFCSP420_CVT(name, out_pixels) \
283 static int altivec_ ## name(SwsContext *c, const unsigned char **in, \
284 int *instrides, int srcSliceY, int srcSliceH, \
285 unsigned char **oplanes, int *outstrides) \
290 int instrides_scl[3]; \
291 vector unsigned char y0, y1; \
293 vector signed char u, v; \
295 vector signed short Y0, Y1, Y2, Y3; \
296 vector signed short U, V; \
297 vector signed short vx, ux, uvx; \
298 vector signed short vx0, ux0, uvx0; \
299 vector signed short vx1, ux1, uvx1; \
300 vector signed short R0, G0, B0; \
301 vector signed short R1, G1, B1; \
302 vector unsigned char R, G, B; \
304 const vector unsigned char *y1ivP, *y2ivP, *uivP, *vivP; \
305 vector unsigned char align_perm; \
307 vector signed short lCY = c->CY; \
308 vector signed short lOY = c->OY; \
309 vector signed short lCRV = c->CRV; \
310 vector signed short lCBU = c->CBU; \
311 vector signed short lCGU = c->CGU; \
312 vector signed short lCGV = c->CGV; \
313 vector unsigned short lCSHIFT = c->CSHIFT; \
315 const ubyte *y1i = in[0]; \
316 const ubyte *y2i = in[0] + instrides[0]; \
317 const ubyte *ui = in[1]; \
318 const ubyte *vi = in[2]; \
320 vector unsigned char *oute = \
321 (vector unsigned char *) \
322 (oplanes[0] + srcSliceY * outstrides[0]); \
323 vector unsigned char *outo = \
324 (vector unsigned char *) \
325 (oplanes[0] + srcSliceY * outstrides[0] + outstrides[0]); \
327 /* loop moves y{1, 2}i by w */ \
328 instrides_scl[0] = instrides[0] * 2 - w; \
329 /* loop moves ui by w / 2 */ \
330 instrides_scl[1] = instrides[1] - w / 2; \
331 /* loop moves vi by w / 2 */ \
332 instrides_scl[2] = instrides[2] - w / 2; \
334 for (i = 0; i < h / 2; i++) { \
335 vec_dstst(outo, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 0); \
336 vec_dstst(oute, (0x02000002 | (((w * 3 + 32) / 32) << 16)), 1); \
338 for (j = 0; j < w / 16; j++) { \
339 y1ivP = (const vector unsigned char *) y1i; \
340 y2ivP = (const vector unsigned char *) y2i; \
341 uivP = (const vector unsigned char *) ui; \
342 vivP = (const vector unsigned char *) vi; \
344 align_perm = vec_lvsl(0, y1i); \
345 y0 = (vector unsigned char) \
346 vec_perm(y1ivP[0], y1ivP[1], align_perm); \
348 align_perm = vec_lvsl(0, y2i); \
349 y1 = (vector unsigned char) \
350 vec_perm(y2ivP[0], y2ivP[1], align_perm); \
352 align_perm = vec_lvsl(0, ui); \
353 u = (vector signed char) \
354 vec_perm(uivP[0], uivP[1], align_perm); \
356 align_perm = vec_lvsl(0, vi); \
357 v = (vector signed char) \
358 vec_perm(vivP[0], vivP[1], align_perm); \
360 u = (vector signed char) \
362 (vector signed char) \
363 vec_splat((vector signed char) { 128 }, 0)); \
364 v = (vector signed char) \
366 (vector signed char) \
367 vec_splat((vector signed char) { 128 }, 0)); \
369 U = vec_unpackh(u); \
370 V = vec_unpackh(v); \
377 Y0 = vec_mradds(Y0, lCY, lOY); \
378 Y1 = vec_mradds(Y1, lCY, lOY); \
379 Y2 = vec_mradds(Y2, lCY, lOY); \
380 Y3 = vec_mradds(Y3, lCY, lOY); \
382 /* ux = (CBU * (u << CSHIFT) + 0x4000) >> 15 */ \
383 ux = vec_sl(U, lCSHIFT); \
384 ux = vec_mradds(ux, lCBU, (vector signed short) { 0 }); \
385 ux0 = vec_mergeh(ux, ux); \
386 ux1 = vec_mergel(ux, ux); \
388 /* vx = (CRV * (v << CSHIFT) + 0x4000) >> 15; */ \
389 vx = vec_sl(V, lCSHIFT); \
390 vx = vec_mradds(vx, lCRV, (vector signed short) { 0 }); \
391 vx0 = vec_mergeh(vx, vx); \
392 vx1 = vec_mergel(vx, vx); \
394 /* uvx = ((CGU * u) + (CGV * v)) >> 15 */ \
395 uvx = vec_mradds(U, lCGU, (vector signed short) { 0 }); \
396 uvx = vec_mradds(V, lCGV, uvx); \
397 uvx0 = vec_mergeh(uvx, uvx); \
398 uvx1 = vec_mergel(uvx, uvx); \
400 R0 = vec_add(Y0, vx0); \
401 G0 = vec_add(Y0, uvx0); \
402 B0 = vec_add(Y0, ux0); \
403 R1 = vec_add(Y1, vx1); \
404 G1 = vec_add(Y1, uvx1); \
405 B1 = vec_add(Y1, ux1); \
407 R = vec_packclp(R0, R1); \
408 G = vec_packclp(G0, G1); \
409 B = vec_packclp(B0, B1); \
411 out_pixels(R, G, B, oute); \
413 R0 = vec_add(Y2, vx0); \
414 G0 = vec_add(Y2, uvx0); \
415 B0 = vec_add(Y2, ux0); \
416 R1 = vec_add(Y3, vx1); \
417 G1 = vec_add(Y3, uvx1); \
418 B1 = vec_add(Y3, ux1); \
419 R = vec_packclp(R0, R1); \
420 G = vec_packclp(G0, G1); \
421 B = vec_packclp(B0, B1); \
424 out_pixels(R, G, B, outo); \
432 outo += (outstrides[0]) >> 4; \
433 oute += (outstrides[0]) >> 4; \
435 ui += instrides_scl[1]; \
436 vi += instrides_scl[2]; \
437 y1i += instrides_scl[0]; \
438 y2i += instrides_scl[0]; \
443 #define out_abgr(a, b, c, ptr) \
444 vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), c, b, a, ptr)
445 #define out_bgra(a, b, c, ptr) \
446 vec_mstrgb32(__typeof__(a), c, b, a, ((__typeof__(a)) { 255 }), ptr)
447 #define out_rgba(a, b, c, ptr) \
448 vec_mstrgb32(__typeof__(a), a, b, c, ((__typeof__(a)) { 255 }), ptr)
449 #define out_argb(a, b, c, ptr) \
450 vec_mstrgb32(__typeof__(a), ((__typeof__(a)) { 255 }), a, b, c, ptr)
451 #define out_rgb24(a, b, c, ptr) vec_mstrgb24(a, b, c, ptr)
452 #define out_bgr24(a, b, c, ptr) vec_mstbgr24(a, b, c, ptr)
454 DEFCSP420_CVT(yuv2_abgr, out_abgr)
455 DEFCSP420_CVT(yuv2_bgra, out_bgra)
456 DEFCSP420_CVT(yuv2_rgba, out_rgba)
457 DEFCSP420_CVT(yuv2_argb, out_argb)
458 DEFCSP420_CVT(yuv2_rgb24, out_rgb24)
459 DEFCSP420_CVT(yuv2_bgr24, out_bgr24)
461 // uyvy|uyvy|uyvy|uyvy
462 // 0123 4567 89ab cdef
463 static const vector unsigned char
464 demux_u = { 0x10, 0x00, 0x10, 0x00,
465 0x10, 0x04, 0x10, 0x04,
466 0x10, 0x08, 0x10, 0x08,
467 0x10, 0x0c, 0x10, 0x0c },
468 demux_v = { 0x10, 0x02, 0x10, 0x02,
469 0x10, 0x06, 0x10, 0x06,
470 0x10, 0x0A, 0x10, 0x0A,
471 0x10, 0x0E, 0x10, 0x0E },
472 demux_y = { 0x10, 0x01, 0x10, 0x03,
473 0x10, 0x05, 0x10, 0x07,
474 0x10, 0x09, 0x10, 0x0B,
475 0x10, 0x0D, 0x10, 0x0F };
478 * this is so I can play live CCIR raw video
480 static int altivec_uyvy_rgb32(SwsContext *c, const unsigned char **in,
481 int *instrides, int srcSliceY, int srcSliceH,
482 unsigned char **oplanes, int *outstrides)
487 vector unsigned char uyvy;
488 vector signed short Y, U, V;
489 vector signed short R0, G0, B0, R1, G1, B1;
490 vector unsigned char R, G, B;
491 vector unsigned char *out;
495 out = (vector unsigned char *) (oplanes[0] + srcSliceY * outstrides[0]);
497 for (i = 0; i < h; i++)
498 for (j = 0; j < w / 16; j++) {
499 uyvy = vec_ld(0, img);
501 U = (vector signed short)
502 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
503 V = (vector signed short)
504 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
505 Y = (vector signed short)
506 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
508 cvtyuvtoRGB(c, Y, U, V, &R0, &G0, &B0);
510 uyvy = vec_ld(16, img);
512 U = (vector signed short)
513 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_u);
514 V = (vector signed short)
515 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_v);
516 Y = (vector signed short)
517 vec_perm(uyvy, (vector unsigned char) { 0 }, demux_y);
519 cvtyuvtoRGB(c, Y, U, V, &R1, &G1, &B1);
521 R = vec_packclp(R0, R1);
522 G = vec_packclp(G0, G1);
523 B = vec_packclp(B0, B1);
525 // vec_mstbgr24 (R,G,B, out);
526 out_rgba(R, G, B, out);
533 /* Ok currently the acceleration routine only supports
534 * inputs of widths a multiple of 16
535 * and heights a multiple 2
537 * So we just fall back to the C codes for this.
539 SwsFunc ff_yuv2rgb_init_altivec(SwsContext *c)
541 if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
545 * and this seems not to matter too much I tried a bunch of
546 * videos with abnormal widths and MPlayer crashes elsewhere.
547 * mplayer -vo x11 -rawvideo on:w=350:h=240 raw-350x240.eyuv
548 * boom with X11 bad match.
551 if ((c->srcW & 0xf) != 0)
554 switch (c->srcFormat) {
555 case PIX_FMT_YUV410P:
556 case PIX_FMT_YUV420P:
557 /*case IMGFMT_CLPL: ??? */
561 if ((c->srcH & 0x1) != 0)
564 switch (c->dstFormat) {
566 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGB24\n");
567 return altivec_yuv2_rgb24;
569 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGR24\n");
570 return altivec_yuv2_bgr24;
572 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ARGB\n");
573 return altivec_yuv2_argb;
575 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space ABGR\n");
576 return altivec_yuv2_abgr;
578 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space RGBA\n");
579 return altivec_yuv2_rgba;
581 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space BGRA\n");
582 return altivec_yuv2_bgra;
583 default: return NULL;
587 case PIX_FMT_UYVY422:
588 switch (c->dstFormat) {
590 av_log(c, AV_LOG_WARNING, "ALTIVEC: Color Space UYVY -> RGB32\n");
591 return altivec_uyvy_rgb32;
592 default: return NULL;
599 void ff_yuv2rgb_init_tables_altivec(SwsContext *c, const int inv_table[4],
600 int brightness, int contrast,
604 DECLARE_ALIGNED(16, signed short, tmp)[8];
605 vector signed short vec;
608 buf.tmp[0] = ((0xffffLL) * contrast >> 8) >> 9; // cy
609 buf.tmp[1] = -256 * brightness; // oy
610 buf.tmp[2] = (inv_table[0] >> 3) * (contrast >> 16) * (saturation >> 16); // crv
611 buf.tmp[3] = (inv_table[1] >> 3) * (contrast >> 16) * (saturation >> 16); // cbu
612 buf.tmp[4] = -((inv_table[2] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgu
613 buf.tmp[5] = -((inv_table[3] >> 1) * (contrast >> 16) * (saturation >> 16)); // cgv
615 c->CSHIFT = (vector unsigned short) vec_splat_u16(2);
616 c->CY = vec_splat((vector signed short) buf.vec, 0);
617 c->OY = vec_splat((vector signed short) buf.vec, 1);
618 c->CRV = vec_splat((vector signed short) buf.vec, 2);
619 c->CBU = vec_splat((vector signed short) buf.vec, 3);
620 c->CGU = vec_splat((vector signed short) buf.vec, 4);
621 c->CGV = vec_splat((vector signed short) buf.vec, 5);
625 static av_always_inline void ff_yuv2packedX_altivec(SwsContext *c,
626 const int16_t *lumFilter,
627 const int16_t **lumSrc,
629 const int16_t *chrFilter,
630 const int16_t **chrUSrc,
631 const int16_t **chrVSrc,
633 const int16_t **alpSrc,
636 enum PixelFormat target)
639 vector signed short X, X0, X1, Y0, U0, V0, Y1, U1, V1, U, V;
640 vector signed short R0, G0, B0, R1, G1, B1;
642 vector unsigned char R, G, B;
643 vector unsigned char *out, *nout;
645 vector signed short RND = vec_splat_s16(1 << 3);
646 vector unsigned short SCL = vec_splat_u16(4);
647 DECLARE_ALIGNED(16, unsigned int, scratch)[16];
649 vector signed short *YCoeffs, *CCoeffs;
651 YCoeffs = c->vYCoeffsBank + dstY * lumFilterSize;
652 CCoeffs = c->vCCoeffsBank + dstY * chrFilterSize;
654 out = (vector unsigned char *) dest;
656 for (i = 0; i < dstW; i += 16) {
659 /* extract 16 coeffs from lumSrc */
660 for (j = 0; j < lumFilterSize; j++) {
661 X0 = vec_ld(0, &lumSrc[j][i]);
662 X1 = vec_ld(16, &lumSrc[j][i]);
663 Y0 = vec_mradds(X0, YCoeffs[j], Y0);
664 Y1 = vec_mradds(X1, YCoeffs[j], Y1);
669 /* extract 8 coeffs from U,V */
670 for (j = 0; j < chrFilterSize; j++) {
671 X = vec_ld(0, &chrUSrc[j][i / 2]);
672 U = vec_mradds(X, CCoeffs[j], U);
673 X = vec_ld(0, &chrVSrc[j][i / 2]);
674 V = vec_mradds(X, CCoeffs[j], V);
677 /* scale and clip signals */
678 Y0 = vec_sra(Y0, SCL);
679 Y1 = vec_sra(Y1, SCL);
683 Y0 = vec_clip_s16(Y0);
684 Y1 = vec_clip_s16(Y1);
689 * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
690 * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
692 * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
693 * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
694 * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
697 U0 = vec_mergeh(U, U);
698 V0 = vec_mergeh(V, V);
700 U1 = vec_mergel(U, U);
701 V1 = vec_mergel(V, V);
703 cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
704 cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
706 R = vec_packclp(R0, R1);
707 G = vec_packclp(G0, G1);
708 B = vec_packclp(B0, B1);
712 out_abgr(R, G, B, out);
715 out_bgra(R, G, B, out);
718 out_rgba(R, G, B, out);
721 out_argb(R, G, B, out);
724 out_rgb24(R, G, B, out);
727 out_bgr24(R, G, B, out);
731 /* If this is reached, the caller should have called yuv2packedXinC
733 static int printed_error_message;
734 if (!printed_error_message) {
735 av_log(c, AV_LOG_ERROR,
736 "altivec_yuv2packedX doesn't support %s output\n",
737 av_get_pix_fmt_name(c->dstFormat));
738 printed_error_message = 1;
750 /* extract 16 coeffs from lumSrc */
751 for (j = 0; j < lumFilterSize; j++) {
752 X0 = vec_ld(0, &lumSrc[j][i]);
753 X1 = vec_ld(16, &lumSrc[j][i]);
754 Y0 = vec_mradds(X0, YCoeffs[j], Y0);
755 Y1 = vec_mradds(X1, YCoeffs[j], Y1);
760 /* extract 8 coeffs from U,V */
761 for (j = 0; j < chrFilterSize; j++) {
762 X = vec_ld(0, &chrUSrc[j][i / 2]);
763 U = vec_mradds(X, CCoeffs[j], U);
764 X = vec_ld(0, &chrVSrc[j][i / 2]);
765 V = vec_mradds(X, CCoeffs[j], V);
768 /* scale and clip signals */
769 Y0 = vec_sra(Y0, SCL);
770 Y1 = vec_sra(Y1, SCL);
774 Y0 = vec_clip_s16(Y0);
775 Y1 = vec_clip_s16(Y1);
780 * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
781 * U = u0 u1 u2 u3 u4 u5 u6 u7 V = v0 v1 v2 v3 v4 v5 v6 v7
783 * Y0 = y0 y1 y2 y3 y4 y5 y6 y7 Y1 = y8 y9 y10 y11 y12 y13 y14 y15
784 * U0 = u0 u0 u1 u1 u2 u2 u3 u3 U1 = u4 u4 u5 u5 u6 u6 u7 u7
785 * V0 = v0 v0 v1 v1 v2 v2 v3 v3 V1 = v4 v4 v5 v5 v6 v6 v7 v7
788 U0 = vec_mergeh(U, U);
789 V0 = vec_mergeh(V, V);
791 U1 = vec_mergel(U, U);
792 V1 = vec_mergel(V, V);
794 cvtyuvtoRGB(c, Y0, U0, V0, &R0, &G0, &B0);
795 cvtyuvtoRGB(c, Y1, U1, V1, &R1, &G1, &B1);
797 R = vec_packclp(R0, R1);
798 G = vec_packclp(G0, G1);
799 B = vec_packclp(B0, B1);
801 nout = (vector unsigned char *) scratch;
804 out_abgr(R, G, B, nout);
807 out_bgra(R, G, B, nout);
810 out_rgba(R, G, B, nout);
813 out_argb(R, G, B, nout);
816 out_rgb24(R, G, B, nout);
819 out_bgr24(R, G, B, nout);
822 /* Unreachable, I think. */
823 av_log(c, AV_LOG_ERROR,
824 "altivec_yuv2packedX doesn't support %s output\n",
825 av_get_pix_fmt_name(c->dstFormat));
829 memcpy(&((uint32_t *) dest)[i], scratch, (dstW - i) / 4);
833 #define YUV2PACKEDX_WRAPPER(suffix, pixfmt) \
834 void ff_yuv2 ## suffix ## _X_altivec(SwsContext *c, \
835 const int16_t *lumFilter, \
836 const int16_t **lumSrc, \
838 const int16_t *chrFilter, \
839 const int16_t **chrUSrc, \
840 const int16_t **chrVSrc, \
842 const int16_t **alpSrc, \
843 uint8_t *dest, int dstW, int dstY) \
845 ff_yuv2packedX_altivec(c, lumFilter, lumSrc, lumFilterSize, \
846 chrFilter, chrUSrc, chrVSrc, \
847 chrFilterSize, alpSrc, \
848 dest, dstW, dstY, pixfmt); \
851 YUV2PACKEDX_WRAPPER(abgr, PIX_FMT_ABGR);
852 YUV2PACKEDX_WRAPPER(bgra, PIX_FMT_BGRA);
853 YUV2PACKEDX_WRAPPER(argb, PIX_FMT_ARGB);
854 YUV2PACKEDX_WRAPPER(rgba, PIX_FMT_RGBA);
855 YUV2PACKEDX_WRAPPER(rgb24, PIX_FMT_RGB24);
856 YUV2PACKEDX_WRAPPER(bgr24, PIX_FMT_BGR24);