]> git.sesse.net Git - ffmpeg/blob - libswscale/ppc/yuv2yuv_altivec.c
Replace PIX_FMT_* -> AV_PIX_FMT_*, PixelFormat -> AVPixelFormat
[ffmpeg] / libswscale / ppc / yuv2yuv_altivec.c
1 /*
2  * AltiVec-enhanced yuv-to-yuv convertion routines.
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 Libav.
8  *
9  * Libav 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  * Libav 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 Libav; 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"
28 #include "libswscale/swscale_internal.h"
29 #include "libavutil/cpu.h"
30
31 static int yv12toyuy2_unscaled_altivec(SwsContext *c, const uint8_t *src[],
32                                        int srcStride[], int srcSliceY,
33                                        int srcSliceH, uint8_t *dstParam[],
34                                        int dstStride_a[])
35 {
36     uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
37     // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
38     //            srcStride[0], srcStride[1], dstStride[0]);
39     const uint8_t *ysrc   = src[0];
40     const uint8_t *usrc   = src[1];
41     const uint8_t *vsrc   = src[2];
42     const int width       = c->srcW;
43     const int height      = srcSliceH;
44     const int lumStride   = srcStride[0];
45     const int chromStride = srcStride[1];
46     const int dstStride   = dstStride_a[0];
47     const vector unsigned char yperm = vec_lvsl(0, ysrc);
48     const int vertLumPerChroma       = 2;
49     register unsigned int y;
50
51     /* This code assumes:
52      *
53      * 1) dst is 16 bytes-aligned
54      * 2) dstStride is a multiple of 16
55      * 3) width is a multiple of 16
56      * 4) lum & chrom stride are multiples of 8
57      */
58
59     for (y = 0; y < height; y++) {
60         int i;
61         for (i = 0; i < width - 31; i += 32) {
62             const unsigned int j          = i >> 1;
63             vector unsigned char v_yA     = vec_ld(i, ysrc);
64             vector unsigned char v_yB     = vec_ld(i + 16, ysrc);
65             vector unsigned char v_yC     = vec_ld(i + 32, ysrc);
66             vector unsigned char v_y1     = vec_perm(v_yA, v_yB, yperm);
67             vector unsigned char v_y2     = vec_perm(v_yB, v_yC, yperm);
68             vector unsigned char v_uA     = vec_ld(j, usrc);
69             vector unsigned char v_uB     = vec_ld(j + 16, usrc);
70             vector unsigned char v_u      = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
71             vector unsigned char v_vA     = vec_ld(j, vsrc);
72             vector unsigned char v_vB     = vec_ld(j + 16, vsrc);
73             vector unsigned char v_v      = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
74             vector unsigned char v_uv_a   = vec_mergeh(v_u, v_v);
75             vector unsigned char v_uv_b   = vec_mergel(v_u, v_v);
76             vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
77             vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
78             vector unsigned char v_yuy2_2 = vec_mergeh(v_y2, v_uv_b);
79             vector unsigned char v_yuy2_3 = vec_mergel(v_y2, v_uv_b);
80             vec_st(v_yuy2_0, (i << 1), dst);
81             vec_st(v_yuy2_1, (i << 1) + 16, dst);
82             vec_st(v_yuy2_2, (i << 1) + 32, dst);
83             vec_st(v_yuy2_3, (i << 1) + 48, dst);
84         }
85         if (i < width) {
86             const unsigned int j          = i >> 1;
87             vector unsigned char v_y1     = vec_ld(i, ysrc);
88             vector unsigned char v_u      = vec_ld(j, usrc);
89             vector unsigned char v_v      = vec_ld(j, vsrc);
90             vector unsigned char v_uv_a   = vec_mergeh(v_u, v_v);
91             vector unsigned char v_yuy2_0 = vec_mergeh(v_y1, v_uv_a);
92             vector unsigned char v_yuy2_1 = vec_mergel(v_y1, v_uv_a);
93             vec_st(v_yuy2_0, (i << 1), dst);
94             vec_st(v_yuy2_1, (i << 1) + 16, dst);
95         }
96         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
97             usrc += chromStride;
98             vsrc += chromStride;
99         }
100         ysrc += lumStride;
101         dst  += dstStride;
102     }
103
104     return srcSliceH;
105 }
106
107 static int yv12touyvy_unscaled_altivec(SwsContext *c, const uint8_t *src[],
108                                        int srcStride[], int srcSliceY,
109                                        int srcSliceH, uint8_t *dstParam[],
110                                        int dstStride_a[])
111 {
112     uint8_t *dst = dstParam[0] + dstStride_a[0] * srcSliceY;
113     // yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH,
114     //            srcStride[0], srcStride[1], dstStride[0]);
115     const uint8_t *ysrc              = src[0];
116     const uint8_t *usrc              = src[1];
117     const uint8_t *vsrc              = src[2];
118     const int width                  = c->srcW;
119     const int height                 = srcSliceH;
120     const int lumStride              = srcStride[0];
121     const int chromStride            = srcStride[1];
122     const int dstStride              = dstStride_a[0];
123     const int vertLumPerChroma       = 2;
124     const vector unsigned char yperm = vec_lvsl(0, ysrc);
125     register unsigned int y;
126
127     /* This code assumes:
128      *
129      * 1) dst is 16 bytes-aligned
130      * 2) dstStride is a multiple of 16
131      * 3) width is a multiple of 16
132      * 4) lum & chrom stride are multiples of 8
133      */
134
135     for (y = 0; y < height; y++) {
136         int i;
137         for (i = 0; i < width - 31; i += 32) {
138             const unsigned int j          = i >> 1;
139             vector unsigned char v_yA     = vec_ld(i, ysrc);
140             vector unsigned char v_yB     = vec_ld(i + 16, ysrc);
141             vector unsigned char v_yC     = vec_ld(i + 32, ysrc);
142             vector unsigned char v_y1     = vec_perm(v_yA, v_yB, yperm);
143             vector unsigned char v_y2     = vec_perm(v_yB, v_yC, yperm);
144             vector unsigned char v_uA     = vec_ld(j, usrc);
145             vector unsigned char v_uB     = vec_ld(j + 16, usrc);
146             vector unsigned char v_u      = vec_perm(v_uA, v_uB, vec_lvsl(j, usrc));
147             vector unsigned char v_vA     = vec_ld(j, vsrc);
148             vector unsigned char v_vB     = vec_ld(j + 16, vsrc);
149             vector unsigned char v_v      = vec_perm(v_vA, v_vB, vec_lvsl(j, vsrc));
150             vector unsigned char v_uv_a   = vec_mergeh(v_u, v_v);
151             vector unsigned char v_uv_b   = vec_mergel(v_u, v_v);
152             vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
153             vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
154             vector unsigned char v_uyvy_2 = vec_mergeh(v_uv_b, v_y2);
155             vector unsigned char v_uyvy_3 = vec_mergel(v_uv_b, v_y2);
156             vec_st(v_uyvy_0, (i << 1), dst);
157             vec_st(v_uyvy_1, (i << 1) + 16, dst);
158             vec_st(v_uyvy_2, (i << 1) + 32, dst);
159             vec_st(v_uyvy_3, (i << 1) + 48, dst);
160         }
161         if (i < width) {
162             const unsigned int j          = i >> 1;
163             vector unsigned char v_y1     = vec_ld(i, ysrc);
164             vector unsigned char v_u      = vec_ld(j, usrc);
165             vector unsigned char v_v      = vec_ld(j, vsrc);
166             vector unsigned char v_uv_a   = vec_mergeh(v_u, v_v);
167             vector unsigned char v_uyvy_0 = vec_mergeh(v_uv_a, v_y1);
168             vector unsigned char v_uyvy_1 = vec_mergel(v_uv_a, v_y1);
169             vec_st(v_uyvy_0, (i << 1), dst);
170             vec_st(v_uyvy_1, (i << 1) + 16, dst);
171         }
172         if ((y & (vertLumPerChroma - 1)) == vertLumPerChroma - 1) {
173             usrc += chromStride;
174             vsrc += chromStride;
175         }
176         ysrc += lumStride;
177         dst  += dstStride;
178     }
179     return srcSliceH;
180 }
181
182 void ff_swscale_get_unscaled_altivec(SwsContext *c)
183 {
184     if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) && !(c->srcW & 15) &&
185         !(c->flags & SWS_BITEXACT) && c->srcFormat == AV_PIX_FMT_YUV420P) {
186         enum AVPixelFormat dstFormat = c->dstFormat;
187
188         // unscaled YV12 -> packed YUV, we want speed
189         if (dstFormat == AV_PIX_FMT_YUYV422)
190             c->swScale = yv12toyuy2_unscaled_altivec;
191         else if (dstFormat == AV_PIX_FMT_UYVY422)
192             c->swScale = yv12touyvy_unscaled_altivec;
193     }
194 }