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 static void
00096 yuv2yuvX_altivec_real(SwsContext *c,
00097 const int16_t *lumFilter, const int16_t **lumSrc,
00098 int lumFilterSize, const int16_t *chrFilter,
00099 const int16_t **chrUSrc, const int16_t **chrVSrc,
00100 int chrFilterSize, const int16_t **alpSrc,
00101 uint8_t *dest, uint8_t *uDest,
00102 uint8_t *vDest, uint8_t *aDest,
00103 int dstW, int chrDstW)
00104 {
00105 const vector signed int vini = {(1 << 18), (1 << 18), (1 << 18), (1 << 18)};
00106 register int i, j;
00107 {
00108 DECLARE_ALIGNED(16, int, val)[dstW];
00109
00110 for (i = 0; i < (dstW -7); i+=4) {
00111 vec_st(vini, i << 2, val);
00112 }
00113 for (; i < dstW; i++) {
00114 val[i] = (1 << 18);
00115 }
00116
00117 for (j = 0; j < lumFilterSize; j++) {
00118 vector signed short l1, vLumFilter = vec_ld(j << 1, lumFilter);
00119 vector unsigned char perm, perm0 = vec_lvsl(j << 1, lumFilter);
00120 vLumFilter = vec_perm(vLumFilter, vLumFilter, perm0);
00121 vLumFilter = vec_splat(vLumFilter, 0);
00122
00123 perm = vec_lvsl(0, lumSrc[j]);
00124 l1 = vec_ld(0, lumSrc[j]);
00125
00126 for (i = 0; i < (dstW - 7); i+=8) {
00127 int offset = i << 2;
00128 vector signed short l2 = vec_ld((i << 1) + 16, lumSrc[j]);
00129
00130 vector signed int v1 = vec_ld(offset, val);
00131 vector signed int v2 = vec_ld(offset + 16, val);
00132
00133 vector signed short ls = vec_perm(l1, l2, perm);
00134
00135 vector signed int i1 = vec_mule(vLumFilter, ls);
00136 vector signed int i2 = vec_mulo(vLumFilter, ls);
00137
00138 vector signed int vf1 = vec_mergeh(i1, i2);
00139 vector signed int vf2 = vec_mergel(i1, i2);
00140
00141 vector signed int vo1 = vec_add(v1, vf1);
00142 vector signed int vo2 = vec_add(v2, vf2);
00143
00144 vec_st(vo1, offset, val);
00145 vec_st(vo2, offset + 16, val);
00146
00147 l1 = l2;
00148 }
00149 for ( ; i < dstW; i++) {
00150 val[i] += lumSrc[j][i] * lumFilter[j];
00151 }
00152 }
00153 altivec_packIntArrayToCharArray(val, dest, dstW);
00154 }
00155 if (uDest != 0) {
00156 DECLARE_ALIGNED(16, int, u)[chrDstW];
00157 DECLARE_ALIGNED(16, int, v)[chrDstW];
00158
00159 for (i = 0; i < (chrDstW -7); i+=4) {
00160 vec_st(vini, i << 2, u);
00161 vec_st(vini, i << 2, v);
00162 }
00163 for (; i < chrDstW; i++) {
00164 u[i] = (1 << 18);
00165 v[i] = (1 << 18);
00166 }
00167
00168 for (j = 0; j < chrFilterSize; j++) {
00169 vector signed short l1, l1_V, vChrFilter = vec_ld(j << 1, chrFilter);
00170 vector unsigned char perm, perm0 = vec_lvsl(j << 1, chrFilter);
00171 vChrFilter = vec_perm(vChrFilter, vChrFilter, perm0);
00172 vChrFilter = vec_splat(vChrFilter, 0);
00173
00174 perm = vec_lvsl(0, chrUSrc[j]);
00175 l1 = vec_ld(0, chrUSrc[j]);
00176 l1_V = vec_ld(0, chrVSrc[j]);
00177
00178 for (i = 0; i < (chrDstW - 7); i+=8) {
00179 int offset = i << 2;
00180 vector signed short l2 = vec_ld((i << 1) + 16, chrUSrc[j]);
00181 vector signed short l2_V = vec_ld((i << 1) + 16, chrVSrc[j]);
00182
00183 vector signed int v1 = vec_ld(offset, u);
00184 vector signed int v2 = vec_ld(offset + 16, u);
00185 vector signed int v1_V = vec_ld(offset, v);
00186 vector signed int v2_V = vec_ld(offset + 16, v);
00187
00188 vector signed short ls = vec_perm(l1, l2, perm);
00189 vector signed short ls_V = vec_perm(l1_V, l2_V, perm);
00190
00191 vector signed int i1 = vec_mule(vChrFilter, ls);
00192 vector signed int i2 = vec_mulo(vChrFilter, ls);
00193 vector signed int i1_V = vec_mule(vChrFilter, ls_V);
00194 vector signed int i2_V = vec_mulo(vChrFilter, ls_V);
00195
00196 vector signed int vf1 = vec_mergeh(i1, i2);
00197 vector signed int vf2 = vec_mergel(i1, i2);
00198 vector signed int vf1_V = vec_mergeh(i1_V, i2_V);
00199 vector signed int vf2_V = vec_mergel(i1_V, i2_V);
00200
00201 vector signed int vo1 = vec_add(v1, vf1);
00202 vector signed int vo2 = vec_add(v2, vf2);
00203 vector signed int vo1_V = vec_add(v1_V, vf1_V);
00204 vector signed int vo2_V = vec_add(v2_V, vf2_V);
00205
00206 vec_st(vo1, offset, u);
00207 vec_st(vo2, offset + 16, u);
00208 vec_st(vo1_V, offset, v);
00209 vec_st(vo2_V, offset + 16, v);
00210
00211 l1 = l2;
00212 l1_V = l2_V;
00213 }
00214 for ( ; i < chrDstW; i++) {
00215 u[i] += chrUSrc[j][i] * chrFilter[j];
00216 v[i] += chrVSrc[j][i] * chrFilter[j];
00217 }
00218 }
00219 altivec_packIntArrayToCharArray(u, uDest, chrDstW);
00220 altivec_packIntArrayToCharArray(v, vDest, chrDstW);
00221 }
00222 }
00223
00224 static void hScale_altivec_real(int16_t *dst, int dstW,
00225 const uint8_t *src, int srcW,
00226 int xInc, const int16_t *filter,
00227 const int16_t *filterPos, int filterSize)
00228 {
00229 register int i;
00230 DECLARE_ALIGNED(16, int, tempo)[4];
00231
00232 if (filterSize % 4) {
00233 for (i=0; i<dstW; i++) {
00234 register int j;
00235 register int srcPos = filterPos[i];
00236 register int val = 0;
00237 for (j=0; j<filterSize; j++) {
00238 val += ((int)src[srcPos + j])*filter[filterSize*i + j];
00239 }
00240 dst[i] = FFMIN(val>>7, (1<<15)-1);
00241 }
00242 }
00243 else
00244 switch (filterSize) {
00245 case 4:
00246 {
00247 for (i=0; i<dstW; i++) {
00248 register int srcPos = filterPos[i];
00249
00250 vector unsigned char src_v0 = vec_ld(srcPos, src);
00251 vector unsigned char src_v1, src_vF;
00252 vector signed short src_v, filter_v;
00253 vector signed int val_vEven, val_s;
00254 if ((((uintptr_t)src + srcPos) % 16) > 12) {
00255 src_v1 = vec_ld(srcPos + 16, src);
00256 }
00257 src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00258
00259 src_v =
00260 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00261
00262 src_v = vec_mergeh(src_v, (vector signed short)vzero);
00263
00264 filter_v = vec_ld(i << 3, filter);
00265
00266
00267
00268
00269
00270
00271 if ((i << 3) % 16)
00272 filter_v = vec_mergel(filter_v, (vector signed short)vzero);
00273 else
00274 filter_v = vec_mergeh(filter_v, (vector signed short)vzero);
00275
00276 val_vEven = vec_mule(src_v, filter_v);
00277 val_s = vec_sums(val_vEven, vzero);
00278 vec_st(val_s, 0, tempo);
00279 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00280 }
00281 }
00282 break;
00283
00284 case 8:
00285 {
00286 for (i=0; i<dstW; i++) {
00287 register int srcPos = filterPos[i];
00288
00289 vector unsigned char src_v0 = vec_ld(srcPos, src);
00290 vector unsigned char src_v1, src_vF;
00291 vector signed short src_v, filter_v;
00292 vector signed int val_v, val_s;
00293 if ((((uintptr_t)src + srcPos) % 16) > 8) {
00294 src_v1 = vec_ld(srcPos + 16, src);
00295 }
00296 src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00297
00298 src_v =
00299 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00300 filter_v = vec_ld(i << 4, filter);
00301
00302
00303 val_v = vec_msums(src_v, filter_v, (vector signed int)vzero);
00304 val_s = vec_sums(val_v, vzero);
00305 vec_st(val_s, 0, tempo);
00306 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00307 }
00308 }
00309 break;
00310
00311 case 16:
00312 {
00313 for (i=0; i<dstW; i++) {
00314 register int srcPos = filterPos[i];
00315
00316 vector unsigned char src_v0 = vec_ld(srcPos, src);
00317 vector unsigned char src_v1 = vec_ld(srcPos + 16, src);
00318 vector unsigned char src_vF = vec_perm(src_v0, src_v1, vec_lvsl(srcPos, src));
00319
00320 vector signed short src_vA =
00321 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00322 vector signed short src_vB =
00323 (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
00324
00325 vector signed short filter_v0 = vec_ld(i << 5, filter);
00326 vector signed short filter_v1 = vec_ld((i << 5) + 16, filter);
00327
00328
00329 vector signed int val_acc = vec_msums(src_vA, filter_v0, (vector signed int)vzero);
00330 vector signed int val_v = vec_msums(src_vB, filter_v1, val_acc);
00331
00332 vector signed int val_s = vec_sums(val_v, vzero);
00333
00334 vec_st(val_s, 0, tempo);
00335 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00336 }
00337 }
00338 break;
00339
00340 default:
00341 {
00342 for (i=0; i<dstW; i++) {
00343 register int j;
00344 register int srcPos = filterPos[i];
00345
00346 vector signed int val_s, val_v = (vector signed int)vzero;
00347 vector signed short filter_v0R = vec_ld(i * 2 * filterSize, filter);
00348 vector unsigned char permF = vec_lvsl((i * 2 * filterSize), filter);
00349
00350 vector unsigned char src_v0 = vec_ld(srcPos, src);
00351 vector unsigned char permS = vec_lvsl(srcPos, src);
00352
00353 for (j = 0 ; j < filterSize - 15; j += 16) {
00354 vector unsigned char src_v1 = vec_ld(srcPos + j + 16, src);
00355 vector unsigned char src_vF = vec_perm(src_v0, src_v1, permS);
00356
00357 vector signed short src_vA =
00358 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00359 vector signed short src_vB =
00360 (vector signed short)(vec_mergel((vector unsigned char)vzero, src_vF));
00361
00362 vector signed short filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
00363 vector signed short filter_v2R = vec_ld((i * 2 * filterSize) + (j * 2) + 32, filter);
00364 vector signed short filter_v0 = vec_perm(filter_v0R, filter_v1R, permF);
00365 vector signed short filter_v1 = vec_perm(filter_v1R, filter_v2R, permF);
00366
00367 vector signed int val_acc = vec_msums(src_vA, filter_v0, val_v);
00368 val_v = vec_msums(src_vB, filter_v1, val_acc);
00369
00370 filter_v0R = filter_v2R;
00371 src_v0 = src_v1;
00372 }
00373
00374 if (j < filterSize-7) {
00375
00376
00377 vector unsigned char src_v1, src_vF;
00378 vector signed short src_v, filter_v1R, filter_v;
00379 if ((((uintptr_t)src + srcPos) % 16) > 8) {
00380 src_v1 = vec_ld(srcPos + j + 16, src);
00381 }
00382 src_vF = vec_perm(src_v0, src_v1, permS);
00383
00384 src_v =
00385 (vector signed short)(vec_mergeh((vector unsigned char)vzero, src_vF));
00386
00387
00388 filter_v1R = vec_ld((i * 2 * filterSize) + (j * 2) + 16, filter);
00389 filter_v = vec_perm(filter_v0R, filter_v1R, permF);
00390
00391 val_v = vec_msums(src_v, filter_v, val_v);
00392 }
00393
00394 val_s = vec_sums(val_v, vzero);
00395
00396 vec_st(val_s, 0, tempo);
00397 dst[i] = FFMIN(tempo[3]>>7, (1<<15)-1);
00398 }
00399
00400 }
00401 }
00402 }
00403
00404 void ff_sws_init_swScale_altivec(SwsContext *c)
00405 {
00406 enum PixelFormat dstFormat = c->dstFormat;
00407
00408 if (!(av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC))
00409 return;
00410
00411 c->hScale = hScale_altivec_real;
00412 if (!is16BPS(dstFormat) && !is9_OR_10BPS(dstFormat)) {
00413 c->yuv2yuvX = yuv2yuvX_altivec_real;
00414 }
00415
00416
00417
00418 if (!(c->flags & (SWS_BITEXACT | SWS_FULL_CHR_H_INT)) && !c->alpPixBuf &&
00419 (c->dstFormat==PIX_FMT_ABGR || c->dstFormat==PIX_FMT_BGRA ||
00420 c->dstFormat==PIX_FMT_BGR24 || c->dstFormat==PIX_FMT_RGB24 ||
00421 c->dstFormat==PIX_FMT_RGBA || c->dstFormat==PIX_FMT_ARGB)) {
00422 c->yuv2packedX = ff_yuv2packedX_altivec;
00423 }
00424 }