00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <inttypes.h>
00025 #include "config.h"
00026 #include "libswscale/swscale.h"
00027 #include "libswscale/swscale_internal.h"
00028 #include "libavutil/cpu.h"
00029 #include "yuv2rgb_altivec.h"
00030
00031 #define vzero vec_splat_s32(0)
00032
00033 static inline void
00034 altivec_packIntArrayToCharArray(int *val, uint8_t* dest, int dstW)
00035 {
00036 register int i;
00037 vector unsigned int altivec_vectorShiftInt19 =
00038 vec_add(vec_splat_u32(10), vec_splat_u32(9));
00039 if ((uintptr_t)dest % 16) {
00040
00041
00042 vector unsigned char perm1;
00043 vector signed int v1;
00044 for (i = 0 ; (i < dstW) &&
00045 (((uintptr_t)dest + i) % 16) ; i++) {
00046 int t = val[i] >> 19;
00047 dest[i] = (t < 0) ? 0 : ((t > 255) ? 255 : t);
00048 }
00049 perm1 = vec_lvsl(i << 2, val);
00050 v1 = vec_ld(i << 2, val);
00051 for ( ; i < (dstW - 15); i+=16) {
00052 int offset = i << 2;
00053 vector signed int v2 = vec_ld(offset + 16, val);
00054 vector signed int v3 = vec_ld(offset + 32, val);
00055 vector signed int v4 = vec_ld(offset + 48, val);
00056 vector signed int v5 = vec_ld(offset + 64, val);
00057 vector signed int v12 = vec_perm(v1, v2, perm1);
00058 vector signed int v23 = vec_perm(v2, v3, perm1);
00059 vector signed int v34 = vec_perm(v3, v4, perm1);
00060 vector signed int v45 = vec_perm(v4, v5, perm1);
00061
00062 vector signed int vA = vec_sra(v12, altivec_vectorShiftInt19);
00063 vector signed int vB = vec_sra(v23, altivec_vectorShiftInt19);
00064 vector signed int vC = vec_sra(v34, altivec_vectorShiftInt19);
00065 vector signed int vD = vec_sra(v45, altivec_vectorShiftInt19);
00066 vector unsigned short vs1 = vec_packsu(vA, vB);
00067 vector unsigned short vs2 = vec_packsu(vC, vD);
00068 vector unsigned char vf = vec_packsu(vs1, vs2);
00069 vec_st(vf, i, dest);
00070 v1 = v5;
00071 }
00072 } else {
00073 for (i = 0; i < (dstW - 15); i+=16) {
00074 int offset = i << 2;
00075 vector signed int v1 = vec_ld(offset, val);
00076 vector signed int v2 = vec_ld(offset + 16, val);
00077 vector signed int v3 = vec_ld(offset + 32, val);
00078 vector signed int v4 = vec_ld(offset + 48, val);
00079 vector signed int v5 = vec_sra(v1, altivec_vectorShiftInt19);
00080 vector signed int v6 = vec_sra(v2, altivec_vectorShiftInt19);
00081 vector signed int v7 = vec_sra(v3, altivec_vectorShiftInt19);
00082 vector signed int v8 = vec_sra(v4, altivec_vectorShiftInt19);
00083 vector unsigned short vs1 = vec_packsu(v5, v6);
00084 vector unsigned short vs2 = vec_packsu(v7, v8);
00085 vector unsigned char vf = vec_packsu(vs1, vs2);
00086 vec_st(vf, i, dest);
00087 }
00088 }
00089 for ( ; i < dstW ; i++) {
00090 int t = val[i] >> 19;
00091 dest[i] = (t < 0) ? 0 : ((t > 255) ? 255 : t);
00092 }
00093 }
00094
00095
00096 static void
00097 yuv2planeX_altivec(const int16_t *filter, int filterSize,
00098 const int16_t **src, uint8_t *dest, int dstW,
00099 const uint8_t *dither, int offset)
00100 {
00101 register int i, j;
00102 {
00103 DECLARE_ALIGNED(16, int, val)[dstW];
00104
00105 for (i=0; i<dstW; i++)
00106 val[i] = dither[(i + offset) & 7] << 12;
00107
00108 for (j = 0; j < filterSize; j++) {
00109 vector signed short l1, vLumFilter = vec_ld(j << 1, filter);
00110 vector unsigned char perm, perm0 = vec_lvsl(j << 1, filter);
00111 vLumFilter = vec_perm(vLumFilter, vLumFilter, perm0);
00112 vLumFilter = vec_splat(vLumFilter, 0);
00113
00114 perm = vec_lvsl(0, src[j]);
00115 l1 = vec_ld(0, src[j]);
00116
00117 for (i = 0; i < (dstW - 7); i+=8) {
00118 int offset = i << 2;
00119 vector signed short l2 = vec_ld((i << 1) + 16, src[j]);
00120
00121 vector signed int v1 = vec_ld(offset, val);
00122 vector signed int v2 = vec_ld(offset + 16, val);
00123
00124 vector signed short ls = vec_perm(l1, l2, perm);
00125
00126 vector signed int i1 = vec_mule(vLumFilter, ls);
00127 vector signed int i2 = vec_mulo(vLumFilter, ls);
00128
00129 vector signed int vf1 = vec_mergeh(i1, i2);
00130 vector signed int vf2 = vec_mergel(i1, i2);
00131
00132 vector signed int vo1 = vec_add(v1, vf1);
00133 vector signed int vo2 = vec_add(v2, vf2);
00134
00135 vec_st(vo1, offset, val);
00136 vec_st(vo2, offset + 16, val);
00137
00138 l1 = l2;
00139 }
00140 for ( ; i < dstW; i++) {
00141 val[i] += src[j][i] * filter[j];
00142 }
00143 }
00144 altivec_packIntArrayToCharArray(val, dest, dstW);
00145 }
00146 }
00147
00148 static void hScale_altivec_real(SwsContext *c, int16_t *dst, int dstW,
00149 const uint8_t *src, const int16_t *filter,
00150 const int16_t *filterPos, int filterSize)
00151 {
00152 register int i;
00153 DECLARE_ALIGNED(16, int, tempo)[4];
00154
00155 if (filterSize % 4) {
00156 for (i=0; i<dstW; i++) {
00157 register int j;
00158 register int srcPos = filterPos[i];
00159 register int val = 0;
00160 for (j=0; j<filterSize; j++) {
00161 val += ((int)src[srcPos + j])*filter[filterSize*i + j];
00162 }
00163 dst[i] = FFMIN(val>>7, (1<<15)-1);
00164 }
00165 }
00166 else
00167 switch (filterSize) {
00168 case 4:
00169 {
00170 for (i=0; i<dstW; i++) {
00171 register int srcPos = filterPos[i];
00172
00173 vector unsigned char src_v0 = vec_ld(srcPos, src);
00174 vector unsigned char src_v1, src_vF;
00175 vector signed short src_v, filter_v;
00176 vector signed int val_vEven, val_s;
00177 if ((((uintptr_t)src + srcPos) % 16) > 12) {
00178 src_v1 = vec_ld(srcPos + 16, src);
00179 }
00180 src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00181
00182 src_v =
00183 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00184
00185 src_v = vec_mergeh(src_v, (vector signed short)vzero);
00186
00187 filter_v = vec_ld(i << 3, filter);
00188
00189
00190
00191
00192
00193
00194 if ((i << 3) % 16)
00195 filter_v = vec_mergel(filter_v, (vector signed short)vzero);
00196 else
00197 filter_v = vec_mergeh(filter_v, (vector signed short)vzero);
00198
00199 val_vEven = vec_mule(src_v, filter_v);
00200 val_s = vec_sums(val_vEven, vzero);
00201 vec_st(val_s, 0, tempo);
00202 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00203 }
00204 }
00205 break;
00206
00207 case 8:
00208 {
00209 for (i=0; i<dstW; i++) {
00210 register int srcPos = filterPos[i];
00211
00212 vector unsigned char src_v0 = vec_ld(srcPos, src);
00213 vector unsigned char src_v1, src_vF;
00214 vector signed short src_v, filter_v;
00215 vector signed int val_v, val_s;
00216 if ((((uintptr_t)src + srcPos) % 16) > 8) {
00217 src_v1 = vec_ld(srcPos + 16, src);
00218 }
00219 src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00220
00221 src_v =
00222 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00223 filter_v = vec_ld(i << 4, filter);
00224
00225
00226 val_v = vec_msums(src_v, filter_v, (vector signed int)vzero);
00227 val_s = vec_sums(val_v, vzero);
00228 vec_st(val_s, 0, tempo);
00229 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00230 }
00231 }
00232 break;
00233
00234 case 16:
00235 {
00236 for (i=0; i<dstW; i++) {
00237 register int srcPos = filterPos[i];
00238
00239 vector unsigned char src_v0 = vec_ld(srcPos, src);
00240 vector unsigned char src_v1 = vec_ld(srcPos + 16, src);
00241 vector unsigned char src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00242
00243 vector signed short src_vA =
00244 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00245 vector signed short src_vB =
00246 (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
00247
00248 vector signed short filter_v0 = vec_ld(i << 5, filter);
00249 vector signed short filter_v1 = vec_ld((i << 5) + 16, filter);
00250
00251
00252 vector signed int val_acc = vec_msums(src_vA, filter_v0, (vector signed int)vzero);
00253 vector signed int val_v = vec_msums(src_vB, filter_v1, val_acc);
00254
00255 vector signed int val_s = vec_sums(val_v, vzero);
00256
00257 vec_st(val_s, 0, tempo);
00258 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00259 }
00260 }
00261 break;
00262
00263 default:
00264 {
00265 for (i=0; i<dstW; i++) {
00266 register int j;
00267 register int srcPos = filterPos[i];
00268
00269 vector signed int val_s, val_v = (vector signed int)vzero;
00270 vector signed short filter_v0R = vec_ld(i * 2 * filterSize, filter);
00271 vector unsigned char permF = vec_lvsl((i * 2 * filterSize), filter);
00272
00273 vector unsigned char src_v0 = vec_ld(srcPos, src);
00274 vector unsigned char permS = vec_lvsl(srcPos, src);
00275
00276 for (j = 0 ; j < filterSize - 15; j += 16) {
00277 vector unsigned char src_v1 = vec_ld(srcPos + j + 16, src);
00278 vector unsigned char src_vF = vec_perm(src_v0, src_v1, permS);
00279
00280 vector signed short src_vA =
00281 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00282 vector signed short src_vB =
00283 (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
00284
00285 vector signed short filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
00286 vector signed short filter_v2R = vec_ld((i * 2 * filterSize) + (j * 2) + 32, filter);
00287 vector signed short filter_v0 = vec_perm(filter_v0R, filter_v1R, permF);
00288 vector signed short filter_v1 = vec_perm(filter_v1R, filter_v2R, permF);
00289
00290 vector signed int val_acc = vec_msums(src_vA, filter_v0, val_v);
00291 val_v = vec_msums(src_vB, filter_v1, val_acc);
00292
00293 filter_v0R = filter_v2R;
00294 src_v0 = src_v1;
00295 }
00296
00297 if (j < filterSize-7) {
00298
00299
00300 vector unsigned char src_v1, src_vF;
00301 vector signed short src_v, filter_v1R, filter_v;
00302 if ((((uintptr_t)src + srcPos) % 16) > 8) {
00303 src_v1 = vec_ld(srcPos + j + 16, src);
00304 }
00305 src_vF = vec_perm(src_v0, src_v1, permS);
00306
00307 src_v =
00308 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00309
00310
00311 filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
00312 filter_v = vec_perm(filter_v0R, filter_v1R, permF);
00313
00314 val_v = vec_msums(src_v, filter_v, val_v);
00315 }
00316
00317 val_s = vec_sums(val_v, vzero);
00318
00319 vec_st(val_s, 0, tempo);
00320 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00321 }
00322
00323 }
00324 }
00325 }
00326
00327 void ff_sws_init_swScale_altivec(SwsContext *c)
00328 {
00329 enum PixelFormat dstFormat = c->dstFormat;
00330
00331 if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
00332 return;
00333
00334 if (c->srcBpc == 8 && c->dstBpc <= 10) {
00335 c->hyScale = c->hcScale = hScale_altivec_real;
00336 }
00337 if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat) &&
00338 dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21 &&
00339 !c->alpPixBuf) {
00340 c->yuv2planeX = yuv2planeX_altivec;
00341 }
00342
00343
00344
00345 if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->alpPixBuf) {
00346 switch (c->dstFormat) {
00347 case PIX_FMT_ABGR: c->yuv2packedX = ff_yuv2abgr_X_altivec; break;
00348 case PIX_FMT_BGRA: c->yuv2packedX = ff_yuv2bgra_X_altivec; break;
00349 case PIX_FMT_ARGB: c->yuv2packedX = ff_yuv2argb_X_altivec; break;
00350 case PIX_FMT_RGBA: c->yuv2packedX = ff_yuv2rgba_X_altivec; break;
00351 case PIX_FMT_BGR24: c->yuv2packedX = ff_yuv2bgr24_X_altivec; break;
00352 case PIX_FMT_RGB24: c->yuv2packedX = ff_yuv2rgb24_X_altivec; break;
00353 }
00354 }
00355 }