snow_altivec.c 22.7 KB
Newer Older
Luca Barbato's avatar
Luca Barbato committed
1
/*
2
 * AltiVec-optimized snow DSP utils
Luca Barbato's avatar
Luca Barbato committed
3 4
 * Copyright (c) 2006 Luca Barbato <lu_zero@gentoo.org>
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
Luca Barbato's avatar
Luca Barbato committed
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
Luca Barbato's avatar
Luca Barbato committed
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
Luca Barbato's avatar
Luca Barbato committed
13 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
Luca Barbato's avatar
Luca Barbato committed
19 20 21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

22 23
#include "libavcodec/dsputil.h"
#include "libavcodec/snow.h"
Luca Barbato's avatar
Luca Barbato committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#include "gcc_fixes.h"
#include "dsputil_altivec.h"

#undef NDEBUG
#include <assert.h>



//FIXME remove this replication
#define slice_buffer_get_line(slice_buf, line_num) ((slice_buf)->line[line_num] ? (slice_buf)->line[line_num] : slice_buffer_load_line((slice_buf), (line_num)))

static DWTELEM * slice_buffer_load_line(slice_buffer * buf, int line)
{
    int offset;
    DWTELEM * buffer;

//  av_log(NULL, AV_LOG_DEBUG, "Cache hit: %d\n", line);

    assert(buf->data_stack_top >= 0);
//  assert(!buf->line[line]);
    if (buf->line[line])
        return buf->line[line];

    offset = buf->line_width * line;
    buffer = buf->data_stack[buf->data_stack_top];
    buf->data_stack_top--;
    buf->line[line] = buffer;

//  av_log(NULL, AV_LOG_DEBUG, "slice_buffer_load_line: line: %d remaining: %d\n", line, buf->data_stack_top + 1);

    return buffer;
}


//altivec code

Luca Barbato's avatar
Luca Barbato committed
61
void ff_snow_horizontal_compose97i_altivec(IDWTELEM *b, int width)
Luca Barbato's avatar
Luca Barbato committed
62
{
Luca Barbato's avatar
Luca Barbato committed
63
#if 0
Luca Barbato's avatar
Luca Barbato committed
64
    const int w2= (width+1)>>1;
Luca Barbato's avatar
Luca Barbato committed
65
    DECLARE_ALIGNED_16(IDWTELEM, temp[(width>>1)]);
Luca Barbato's avatar
Luca Barbato committed
66 67 68
    const int w_l= (width>>1);
    const int w_r= w2 - 1;
    int i;
Luca Barbato's avatar
Luca Barbato committed
69 70
    vector signed short t1, t2, x, y, tmp1, tmp2;
    vector signed short *vbuf, *vtmp;
Luca Barbato's avatar
Luca Barbato committed
71 72 73
    vector unsigned char align;

    { // Lift 0
Luca Barbato's avatar
Luca Barbato committed
74 75 76 77
        IDWTELEM * const ref = b + w2 - 1;
        IDWTELEM b_0 = b[0];
        vector signed short v7 = vec_splat_s16(7);
        vbuf = (vector signed short *)b;
Luca Barbato's avatar
Luca Barbato committed
78 79 80 81

        tmp1 = vec_ld (0, ref);
        align = vec_lvsl (0, ref);
        tmp2 = vec_ld (15, ref);
Luca Barbato's avatar
Luca Barbato committed
82
        t1 = vec_perm(tmp1, tmp2, align);
Luca Barbato's avatar
Luca Barbato committed
83 84 85

        for (i=0; i<w_l-15; i+=16) {
#if 0
Luca Barbato's avatar
Luca Barbato committed
86
/*        b[i+0] = b[i+0] - ((3 * (ref[i+0] + ref[i+1]) + 4) >> 3);
Luca Barbato's avatar
Luca Barbato committed
87 88
        b[i+1] = b[i+1] - ((3 * (ref[i+1] + ref[i+2]) + 4) >> 3);
        b[i+2] = b[i+2] - ((3 * (ref[i+2] + ref[i+3]) + 4) >> 3);
Luca Barbato's avatar
Luca Barbato committed
89 90
        b[i+3] = b[i+3] - ((3 * (ref[i+3] + ref[i+4]) + 4) >> 3);*/
        b[i+0] = b[i+0] + ((7 * (ref[i+0] + ref[i+1])-1) >> 8);
Luca Barbato's avatar
Luca Barbato committed
91 92
#else

Luca Barbato's avatar
Luca Barbato committed
93 94
        tmp1 = vec_ld (0, ref+8+i);
        tmp2 = vec_ld (15, ref+8+i);
Luca Barbato's avatar
Luca Barbato committed
95 96 97

        t2 = vec_perm(tmp1, tmp2, align);

Luca Barbato's avatar
Luca Barbato committed
98 99
        y = vec_add(t1, vec_sld(t1,t2,2));
//        y = vec_add(vec_add(y,y),y);
Luca Barbato's avatar
Luca Barbato committed
100

Luca Barbato's avatar
Luca Barbato committed
101
        tmp1 = vec_ld (0, ref+12+i);
Luca Barbato's avatar
Luca Barbato committed
102 103 104 105

        y = vec_add(y, vec_splat_s32(4));
        y = vec_sra(y, vec_splat_u32(3));

Luca Barbato's avatar
Luca Barbato committed
106
        tmp2 = vec_ld (15, ref+12+i);
Luca Barbato's avatar
Luca Barbato committed
107 108 109

        *vbuf = vec_sub(*vbuf, y);

Luca Barbato's avatar
Luca Barbato committed
110
        t1 = t2;
Luca Barbato's avatar
Luca Barbato committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

        vbuf++;

        t2 = vec_perm(tmp1, tmp2, align);

        y = vec_add(t1,vec_sld(t1,t2,4));
        y = vec_add(vec_add(y,y),y);

        tmp1 = vec_ld (0, ref+12+i);

        y = vec_add(y, vec_splat_s32(4));
        y = vec_sra(y, vec_splat_u32(3));

        tmp2 = vec_ld (15, ref+12+i);

        *vbuf = vec_sub(*vbuf, y);

        t1=t2;

        vbuf++;

        t2 = vec_perm(tmp1, tmp2, align);

        y = vec_add(t1,vec_sld(t1,t2,4));
        y = vec_add(vec_add(y,y),y);

        tmp1 = vec_ld (0, ref+16+i);

        y = vec_add(y, vec_splat_s32(4));
        y = vec_sra(y, vec_splat_u32(3));

        tmp2 = vec_ld (15, ref+16+i);

        *vbuf = vec_sub(*vbuf, y);

        t1=t2;

        t2 = vec_perm(tmp1, tmp2, align);

        y = vec_add(t1,vec_sld(t1,t2,4));
        y = vec_add(vec_add(y,y),y);

        vbuf++;

        y = vec_add(y, vec_splat_s32(4));
        y = vec_sra(y, vec_splat_u32(3));
        *vbuf = vec_sub(*vbuf, y);

        t1=t2;

        vbuf++;

#endif
Luca Barbato's avatar
Luca Barbato committed
164

Luca Barbato's avatar
Luca Barbato committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
        }

        snow_horizontal_compose_lift_lead_out(i, b, b, ref, width, w_l, 0, W_DM, W_DO, W_DS);
        b[0] = b_0 - ((W_DM * 2 * ref[1]+W_DO)>>W_DS);
    }

    { // Lift 1
        DWTELEM * const dst = b+w2;

        i = 0;
        for(; (((long)&dst[i]) & 0xF) && i<w_r; i++){
            dst[i] = dst[i] - (b[i] + b[i + 1]);
        }

        align = vec_lvsl(0, b+i);
        tmp1 = vec_ld(0, b+i);
        vbuf = (vector signed int*) (dst + i);
        tmp2 = vec_ld(15, b+i);

        t1 = vec_perm(tmp1, tmp2, align);

        for (; i<w_r-3; i+=4) {

#if 0
            dst[i]   = dst[i]   - (b[i]   + b[i + 1]);
            dst[i+1] = dst[i+1] - (b[i+1] + b[i + 2]);
            dst[i+2] = dst[i+2] - (b[i+2] + b[i + 3]);
            dst[i+3] = dst[i+3] - (b[i+3] + b[i + 4]);
#else

        tmp1 = vec_ld(0, b+4+i);
        tmp2 = vec_ld(15, b+4+i);

        t2 = vec_perm(tmp1, tmp2, align);

        y = vec_add(t1, vec_sld(t1,t2,4));
        *vbuf = vec_sub (*vbuf, y);

        vbuf++;

        t1 = t2;

#endif

        }

        snow_horizontal_compose_lift_lead_out(i, dst, dst, b, width, w_r, 1, W_CM, W_CO, W_CS);
    }

    { // Lift 2
        DWTELEM * const ref = b+w2 - 1;
        DWTELEM b_0 = b[0];
        vbuf= (vector signed int *) b;

        tmp1 = vec_ld (0, ref);
        align = vec_lvsl (0, ref);
        tmp2 = vec_ld (15, ref);
        t1= vec_perm(tmp1, tmp2, align);

        i = 0;
        for (; i<w_l-15; i+=16) {
#if 0
            b[i]   = b[i]   - (((8 -(ref[i]   + ref[i+1])) - (b[i]  <<2)) >> 4);
            b[i+1] = b[i+1] - (((8 -(ref[i+1] + ref[i+2])) - (b[i+1]<<2)) >> 4);
            b[i+2] = b[i+2] - (((8 -(ref[i+2] + ref[i+3])) - (b[i+2]<<2)) >> 4);
            b[i+3] = b[i+3] - (((8 -(ref[i+3] + ref[i+4])) - (b[i+3]<<2)) >> 4);
#else
            tmp1 = vec_ld (0, ref+4+i);
            tmp2 = vec_ld (15, ref+4+i);

            t2 = vec_perm(tmp1, tmp2, align);

            y = vec_add(t1,vec_sld(t1,t2,4));
            y = vec_sub(vec_splat_s32(8),y);

            tmp1 = vec_ld (0, ref+8+i);

            x = vec_sl(*vbuf,vec_splat_u32(2));
            y = vec_sra(vec_sub(y,x),vec_splat_u32(4));

            tmp2 = vec_ld (15, ref+8+i);

            *vbuf = vec_sub( *vbuf, y);

            t1 = t2;

            vbuf++;

            t2 = vec_perm(tmp1, tmp2, align);

            y = vec_add(t1,vec_sld(t1,t2,4));
            y = vec_sub(vec_splat_s32(8),y);

            tmp1 = vec_ld (0, ref+12+i);

            x = vec_sl(*vbuf,vec_splat_u32(2));
            y = vec_sra(vec_sub(y,x),vec_splat_u32(4));

            tmp2 = vec_ld (15, ref+12+i);

            *vbuf = vec_sub( *vbuf, y);

            t1 = t2;

            vbuf++;

            t2 = vec_perm(tmp1, tmp2, align);

            y = vec_add(t1,vec_sld(t1,t2,4));
            y = vec_sub(vec_splat_s32(8),y);

            tmp1 = vec_ld (0, ref+16+i);

            x = vec_sl(*vbuf,vec_splat_u32(2));
            y = vec_sra(vec_sub(y,x),vec_splat_u32(4));

            tmp2 = vec_ld (15, ref+16+i);

            *vbuf = vec_sub( *vbuf, y);

            t1 = t2;

            vbuf++;

            t2 = vec_perm(tmp1, tmp2, align);

            y = vec_add(t1,vec_sld(t1,t2,4));
            y = vec_sub(vec_splat_s32(8),y);

            t1 = t2;

            x = vec_sl(*vbuf,vec_splat_u32(2));
            y = vec_sra(vec_sub(y,x),vec_splat_u32(4));
            *vbuf = vec_sub( *vbuf, y);

            vbuf++;

#endif
        }

        snow_horizontal_compose_liftS_lead_out(i, b, b, ref, width, w_l);
        b[0] = b_0 - (((-2 * ref[1] + W_BO) - 4 * b_0) >> W_BS);
    }

    { // Lift 3
        DWTELEM * const src = b+w2;

        vbuf = (vector signed int *)b;
        vtmp = (vector signed int *)temp;

        i = 0;
        align = vec_lvsl(0, src);

        for (; i<w_r-3; i+=4) {
#if 0
            temp[i] = src[i] - ((-3*(b[i] + b[i+1]))>>1);
            temp[i+1] = src[i+1] - ((-3*(b[i+1] + b[i+2]))>>1);
            temp[i+2] = src[i+2] - ((-3*(b[i+2] + b[i+3]))>>1);
            temp[i+3] = src[i+3] - ((-3*(b[i+3] + b[i+4]))>>1);
#else
            tmp1 = vec_ld(0,src+i);
            t1 = vec_add(vbuf[0],vec_sld(vbuf[0],vbuf[1],4));
            tmp2 = vec_ld(15,src+i);
            t1 = vec_sub(vec_splat_s32(0),t1); //bad!
            t1 = vec_add(t1,vec_add(t1,t1));
            t2 = vec_perm(tmp1 ,tmp2 ,align);
            t1 = vec_sra(t1,vec_splat_u32(1));
            vbuf++;
            *vtmp = vec_sub(t2,t1);
            vtmp++;

#endif

        }

        snow_horizontal_compose_lift_lead_out(i, temp, src, b, width, w_r, 1, -3, 0, 1);
    }

    {
    //Interleave
        int a;
        vector signed int *t = (vector signed int *)temp,
                          *v = (vector signed int *)b;

        snow_interleave_line_header(&i, width, b, temp);

        for (; (i & 0xE) != 0xE; i-=2){
            b[i+1] = temp[i>>1];
            b[i] = b[i>>1];
        }
        for (i-=14; i>=0; i-=16){
           a=i/4;

           v[a+3]=vec_mergel(v[(a>>1)+1],t[(a>>1)+1]);
           v[a+2]=vec_mergeh(v[(a>>1)+1],t[(a>>1)+1]);
           v[a+1]=vec_mergel(v[a>>1],t[a>>1]);
           v[a]=vec_mergeh(v[a>>1],t[a>>1]);

        }

    }
Luca Barbato's avatar
Luca Barbato committed
366
#endif
Luca Barbato's avatar
Luca Barbato committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
}

void ff_snow_vertical_compose97i_altivec(DWTELEM *b0, DWTELEM *b1, DWTELEM *b2, DWTELEM *b3, DWTELEM *b4, DWTELEM *b5, int width)
{
    int i, w4 = width/4;
    vector signed int *v0, *v1,*v2,*v3,*v4,*v5;
    vector signed int t1, t2;

    v0=(vector signed int *)b0;
    v1=(vector signed int *)b1;
    v2=(vector signed int *)b2;
    v3=(vector signed int *)b3;
    v4=(vector signed int *)b4;
    v5=(vector signed int *)b5;

382
    for (i=0; i< w4;i++) {
Luca Barbato's avatar
Luca Barbato committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

    #if 0
        b4[i] -= (3*(b3[i] + b5[i])+4)>>3;
        b3[i] -= ((b2[i] + b4[i]));
        b2[i] += ((b1[i] + b3[i])+4*b2[i]+8)>>4;
        b1[i] += (3*(b0[i] + b2[i]))>>1;
    #else
        t1 = vec_add(v3[i], v5[i]);
        t2 = vec_add(t1, vec_add(t1,t1));
        t1 = vec_add(t2, vec_splat_s32(4));
        v4[i] = vec_sub(v4[i], vec_sra(t1,vec_splat_u32(3)));

        v3[i] = vec_sub(v3[i], vec_add(v2[i], v4[i]));

        t1 = vec_add(vec_splat_s32(8), vec_add(v1[i], v3[i]));
        t2 = vec_sl(v2[i], vec_splat_u32(2));
        v2[i] = vec_add(v2[i], vec_sra(vec_add(t1,t2),vec_splat_u32(4)));
        t1 = vec_add(v0[i], v2[i]);
        t2 = vec_add(t1, vec_add(t1,t1));
        v1[i] = vec_add(v1[i], vec_sra(t2,vec_splat_u32(1)));

    #endif
    }

    for(i*=4; i < width; i++)
    {
        b4[i] -= (W_DM*(b3[i] + b5[i])+W_DO)>>W_DS;
        b3[i] -= (W_CM*(b2[i] + b4[i])+W_CO)>>W_CS;
        b2[i] += (W_BM*(b1[i] + b3[i])+4*b2[i]+W_BO)>>W_BS;
        b1[i] += (W_AM*(b0[i] + b2[i])+W_AO)>>W_AS;
    }
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#define LOAD_BLOCKS \
            tmp1 = vec_ld(0, &block[3][y*src_stride]);\
            align = vec_lvsl(0, &block[3][y*src_stride]);\
            tmp2 = vec_ld(15, &block[3][y*src_stride]);\
\
            b3 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, &block[2][y*src_stride]);\
            align = vec_lvsl(0, &block[2][y*src_stride]);\
            tmp2 = vec_ld(15, &block[2][y*src_stride]);\
\
            b2 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, &block[1][y*src_stride]);\
            align = vec_lvsl(0, &block[1][y*src_stride]);\
            tmp2 = vec_ld(15, &block[1][y*src_stride]);\
\
            b1 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, &block[0][y*src_stride]);\
            align = vec_lvsl(0, &block[0][y*src_stride]);\
            tmp2 = vec_ld(15, &block[0][y*src_stride]);\
\
            b0 = vec_perm(tmp1,tmp2,align);

#define LOAD_OBMCS \
            tmp1 = vec_ld(0, obmc1);\
            align = vec_lvsl(0, obmc1);\
            tmp2 = vec_ld(15, obmc1);\
\
            ob1 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, obmc2);\
            align = vec_lvsl(0, obmc2);\
            tmp2 = vec_ld(15, obmc2);\
\
            ob2 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, obmc3);\
            align = vec_lvsl(0, obmc3);\
            tmp2 = vec_ld(15, obmc3);\
\
            ob3 = vec_perm(tmp1,tmp2,align);\
\
            tmp1 = vec_ld(0, obmc4);\
            align = vec_lvsl(0, obmc4);\
            tmp2 = vec_ld(15, obmc4);\
\
            ob4 = vec_perm(tmp1,tmp2,align);

/* interleave logic
 * h1 <- [ a,b,a,b, a,b,a,b, a,b,a,b, a,b,a,b ]
 * h2 <- [ c,d,c,d, c,d,c,d, c,d,c,d, c,d,c,d ]
 * h  <- [ a,b,c,d, a,b,c,d, a,b,c,d, a,b,c,d ]
 */

#define STEPS_0_1\
            h1 = (vector unsigned short)\
                 vec_mergeh(ob1, ob2);\
\
            h2 = (vector unsigned short)\
                 vec_mergeh(ob3, ob4);\
\
            ih = (vector unsigned char)\
                 vec_mergeh(h1,h2);\
\
            l1 = (vector unsigned short) vec_mergeh(b3, b2);\
\
            ih1 = (vector unsigned char) vec_mergel(h1, h2);\
\
            l2 = (vector unsigned short) vec_mergeh(b1, b0);\
\
            il = (vector unsigned char) vec_mergeh(l1, l2);\
\
            v[0] = (vector signed int) vec_msum(ih, il, vec_splat_u32(0));\
\
            il1 = (vector unsigned char) vec_mergel(l1, l2);\
\
            v[1] = (vector signed int) vec_msum(ih1, il1, vec_splat_u32(0));

#define FINAL_STEP_SCALAR\
        for(x=0; x<b_w; x++)\
            if(add){\
                vbuf[x] += dst[x + src_x];\
                vbuf[x] = (vbuf[x] + (1<<(FRAC_BITS-1))) >> FRAC_BITS;\
                if(vbuf[x]&(~255)) vbuf[x]= ~(vbuf[x]>>31);\
                dst8[x + y*src_stride] = vbuf[x];\
            }else{\
                dst[x + src_x] -= vbuf[x];\
            }
Luca Barbato's avatar
Luca Barbato committed
506 507 508 509 510 511 512 513 514 515 516

static void inner_add_yblock_bw_8_obmc_16_altivec(uint8_t *obmc,
                                             const int obmc_stride,
                                             uint8_t * * block, int b_w,
                                             int b_h, int src_x, int src_y,
                                             int src_stride, slice_buffer * sb,
                                             int add, uint8_t * dst8)
{
    int y, x;
    DWTELEM * dst;
    vector unsigned short h1, h2, l1, l2;
517
    vector unsigned char ih, il, ih1, il1, tmp1, tmp2, align;
Luca Barbato's avatar
Luca Barbato committed
518
    vector unsigned char b0,b1,b2,b3;
519 520 521 522
    vector unsigned char ob1,ob2,ob3,ob4;

    DECLARE_ALIGNED_16(int, vbuf[16]);
    vector signed int *v = (vector signed int *)vbuf, *d;
Luca Barbato's avatar
Luca Barbato committed
523 524

    for(y=0; y<b_h; y++){
525
        //FIXME ugly misuse of obmc_stride
Luca Barbato's avatar
Luca Barbato committed
526 527 528 529 530 531 532 533 534

        uint8_t *obmc1= obmc + y*obmc_stride;
        uint8_t *obmc2= obmc1+ (obmc_stride>>1);
        uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
        uint8_t *obmc4= obmc3+ (obmc_stride>>1);

        dst = slice_buffer_get_line(sb, src_y + y);
        d = (vector signed int *)(dst + src_x);

535
//FIXME i could avoid some loads!
Luca Barbato's avatar
Luca Barbato committed
536

537 538
        // load blocks
        LOAD_BLOCKS
Luca Barbato's avatar
Luca Barbato committed
539

540 541
        // load obmcs
        LOAD_OBMCS
Luca Barbato's avatar
Luca Barbato committed
542

543 544
        // steps 0 1
        STEPS_0_1
Luca Barbato's avatar
Luca Barbato committed
545

546
        FINAL_STEP_SCALAR
Luca Barbato's avatar
Luca Barbato committed
547

548
       }
Luca Barbato's avatar
Luca Barbato committed
549

550
}
Luca Barbato's avatar
Luca Barbato committed
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
#define STEPS_2_3\
            h1 = (vector unsigned short) vec_mergel(ob1, ob2);\
\
            h2 = (vector unsigned short) vec_mergel(ob3, ob4);\
\
            ih = (vector unsigned char) vec_mergeh(h1,h2);\
\
            l1 = (vector unsigned short) vec_mergel(b3, b2);\
\
            l2 = (vector unsigned short) vec_mergel(b1, b0);\
\
            ih1 = (vector unsigned char) vec_mergel(h1,h2);\
\
            il = (vector unsigned char) vec_mergeh(l1,l2);\
\
            v[2] = (vector signed int) vec_msum(ih, il, vec_splat_u32(0));\
\
            il1 = (vector unsigned char) vec_mergel(l1,l2);\
\
            v[3] = (vector signed int) vec_msum(ih1, il1, vec_splat_u32(0));
Luca Barbato's avatar
Luca Barbato committed
572 573


574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
static void inner_add_yblock_bw_16_obmc_32_altivec(uint8_t *obmc,
                                             const int obmc_stride,
                                             uint8_t * * block, int b_w,
                                             int b_h, int src_x, int src_y,
                                             int src_stride, slice_buffer * sb,
                                             int add, uint8_t * dst8)
{
    int y, x;
    DWTELEM * dst;
    vector unsigned short h1, h2, l1, l2;
    vector unsigned char ih, il, ih1, il1, tmp1, tmp2, align;
    vector unsigned char b0,b1,b2,b3;
    vector unsigned char ob1,ob2,ob3,ob4;
    DECLARE_ALIGNED_16(int, vbuf[b_w]);
    vector signed int *v = (vector signed int *)vbuf, *d;
Luca Barbato's avatar
Luca Barbato committed
589

590
    for(y=0; y<b_h; y++){
591
        //FIXME ugly misuse of obmc_stride
Luca Barbato's avatar
Luca Barbato committed
592

593 594 595 596
        uint8_t *obmc1= obmc + y*obmc_stride;
        uint8_t *obmc2= obmc1+ (obmc_stride>>1);
        uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
        uint8_t *obmc4= obmc3+ (obmc_stride>>1);
Luca Barbato's avatar
Luca Barbato committed
597

598 599
        dst = slice_buffer_get_line(sb, src_y + y);
        d = (vector signed int *)(dst + src_x);
Luca Barbato's avatar
Luca Barbato committed
600

601 602
        // load blocks
        LOAD_BLOCKS
Luca Barbato's avatar
Luca Barbato committed
603

604 605
        // load obmcs
        LOAD_OBMCS
Luca Barbato's avatar
Luca Barbato committed
606

607 608
        // steps 0 1 2 3
        STEPS_0_1
Luca Barbato's avatar
Luca Barbato committed
609

610
        STEPS_2_3
Luca Barbato's avatar
Luca Barbato committed
611

612
        FINAL_STEP_SCALAR
Luca Barbato's avatar
Luca Barbato committed
613

614 615
    }
}
Luca Barbato's avatar
Luca Barbato committed
616

617 618 619 620 621 622 623 624 625 626 627 628
#define FINAL_STEP_VEC \
\
    if(add)\
        {\
            for(x=0; x<b_w/4; x++)\
            {\
                v[x] = vec_add(v[x], d[x]);\
                v[x] = vec_sra(vec_add(v[x],\
                                       vec_sl( vec_splat_s32(1),\
                                               vec_splat_u32(7))),\
                               vec_splat_u32(8));\
\
Luca Barbato's avatar
Luca Barbato committed
629
                mask = (vector bool int) vec_sl((vector signed int)\
630
                        vec_cmpeq(v[x],v[x]),vec_splat_u32(8));\
Luca Barbato's avatar
Luca Barbato committed
631
                mask = (vector bool int) vec_and(v[x],vec_nor(mask,mask));\
632
\
Luca Barbato's avatar
Luca Barbato committed
633
                mask = (vector bool int)\
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                        vec_cmpeq((vector signed int)mask,\
                                  (vector signed int)vec_splat_u32(0));\
\
                vs = vec_sra(v[x],vec_splat_u32(8));\
                vs = vec_sra(v[x],vec_splat_u32(8));\
                vs = vec_sra(v[x],vec_splat_u32(15));\
\
                vs = vec_nor(vs,vs);\
\
                v[x]= vec_sel(v[x],vs,mask);\
            }\
\
            for(x=0; x<b_w; x++)\
                dst8[x + y*src_stride] = vbuf[x];\
\
        }\
         else\
            for(x=0; x<b_w/4; x++)\
                d[x] = vec_sub(d[x], v[x]);
Luca Barbato's avatar
Luca Barbato committed
653

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static void inner_add_yblock_a_bw_8_obmc_16_altivec(uint8_t *obmc,
                                             const int obmc_stride,
                                             uint8_t * * block, int b_w,
                                             int b_h, int src_x, int src_y,
                                             int src_stride, slice_buffer * sb,
                                             int add, uint8_t * dst8)
{
    int y, x;
    DWTELEM * dst;
    vector bool int mask;
    vector signed int vs;
    vector unsigned short h1, h2, l1, l2;
    vector unsigned char ih, il, ih1, il1, tmp1, tmp2, align;
    vector unsigned char b0,b1,b2,b3;
    vector unsigned char ob1,ob2,ob3,ob4;
Luca Barbato's avatar
Luca Barbato committed
669

670 671
    DECLARE_ALIGNED_16(int, vbuf[16]);
    vector signed int *v = (vector signed int *)vbuf, *d;
Luca Barbato's avatar
Luca Barbato committed
672

673
    for(y=0; y<b_h; y++){
674
        //FIXME ugly misuse of obmc_stride
Luca Barbato's avatar
Luca Barbato committed
675

676 677 678 679
        uint8_t *obmc1= obmc + y*obmc_stride;
        uint8_t *obmc2= obmc1+ (obmc_stride>>1);
        uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
        uint8_t *obmc4= obmc3+ (obmc_stride>>1);
Luca Barbato's avatar
Luca Barbato committed
680

681 682
        dst = slice_buffer_get_line(sb, src_y + y);
        d = (vector signed int *)(dst + src_x);
Luca Barbato's avatar
Luca Barbato committed
683

684
//FIXME i could avoid some loads!
Luca Barbato's avatar
Luca Barbato committed
685

686 687
        // load blocks
        LOAD_BLOCKS
Luca Barbato's avatar
Luca Barbato committed
688

689 690
        // load obmcs
        LOAD_OBMCS
Luca Barbato's avatar
Luca Barbato committed
691

692 693
        // steps 0 1
        STEPS_0_1
Luca Barbato's avatar
Luca Barbato committed
694

695
        FINAL_STEP_VEC
Luca Barbato's avatar
Luca Barbato committed
696

Luca Barbato's avatar
Luca Barbato committed
697 698 699 700
       }

}

701
static void inner_add_yblock_a_bw_16_obmc_32_altivec(uint8_t *obmc,
Luca Barbato's avatar
Luca Barbato committed
702 703 704 705 706 707 708 709
                                             const int obmc_stride,
                                             uint8_t * * block, int b_w,
                                             int b_h, int src_x, int src_y,
                                             int src_stride, slice_buffer * sb,
                                             int add, uint8_t * dst8)
{
    int y, x;
    DWTELEM * dst;
710 711
    vector bool int mask;
    vector signed int vs;
Luca Barbato's avatar
Luca Barbato committed
712
    vector unsigned short h1, h2, l1, l2;
713
    vector unsigned char ih, il, ih1, il1, tmp1, tmp2, align;
Luca Barbato's avatar
Luca Barbato committed
714
    vector unsigned char b0,b1,b2,b3;
715 716 717
    vector unsigned char ob1,ob2,ob3,ob4;
    DECLARE_ALIGNED_16(int, vbuf[b_w]);
    vector signed int *v = (vector signed int *)vbuf, *d;
Luca Barbato's avatar
Luca Barbato committed
718 719

    for(y=0; y<b_h; y++){
720
        //FIXME ugly misuse of obmc_stride
Luca Barbato's avatar
Luca Barbato committed
721 722 723 724 725 726 727 728 729 730

        uint8_t *obmc1= obmc + y*obmc_stride;
        uint8_t *obmc2= obmc1+ (obmc_stride>>1);
        uint8_t *obmc3= obmc1+ obmc_stride*(obmc_stride>>1);
        uint8_t *obmc4= obmc3+ (obmc_stride>>1);

        dst = slice_buffer_get_line(sb, src_y + y);
        d = (vector signed int *)(dst + src_x);

        // load blocks
731
        LOAD_BLOCKS
Luca Barbato's avatar
Luca Barbato committed
732

733 734
        // load obmcs
        LOAD_OBMCS
Luca Barbato's avatar
Luca Barbato committed
735

736 737
        // steps 0 1 2 3
        STEPS_0_1
Luca Barbato's avatar
Luca Barbato committed
738

739
        STEPS_2_3
Luca Barbato's avatar
Luca Barbato committed
740

741
        FINAL_STEP_VEC
Luca Barbato's avatar
Luca Barbato committed
742

743
    }
Luca Barbato's avatar
Luca Barbato committed
744 745 746 747 748 749 750 751 752
}


void ff_snow_inner_add_yblock_altivec(uint8_t *obmc, const int obmc_stride,
                                      uint8_t * * block, int b_w, int b_h,
                                      int src_x, int src_y, int src_stride,
                                      slice_buffer * sb, int add,
                                      uint8_t * dst8)
{
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
    if (src_x&15) {
        if (b_w == 16)
            inner_add_yblock_bw_16_obmc_32_altivec(obmc, obmc_stride, block,
                                                   b_w, b_h, src_x, src_y,
                                                   src_stride, sb, add, dst8);
        else if (b_w == 8)
            inner_add_yblock_bw_8_obmc_16_altivec(obmc, obmc_stride, block,
                                                  b_w, b_h, src_x, src_y,
                                                  src_stride, sb, add, dst8);
        else
            ff_snow_inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,
                                     src_y, src_stride, sb, add, dst8);
    } else {
        if (b_w == 16)
            inner_add_yblock_a_bw_16_obmc_32_altivec(obmc, obmc_stride, block,
                                                     b_w, b_h, src_x, src_y,
                                                     src_stride, sb, add, dst8);
        else if (b_w == 8)
            inner_add_yblock_a_bw_8_obmc_16_altivec(obmc, obmc_stride, block,
                                                    b_w, b_h, src_x, src_y,
                                                    src_stride, sb, add, dst8);
        else
            ff_snow_inner_add_yblock(obmc, obmc_stride, block, b_w, b_h, src_x,
                                     src_y, src_stride, sb, add, dst8);
    }
Luca Barbato's avatar
Luca Barbato committed
778
}
779 780 781 782


void snow_init_altivec(DSPContext* c, AVCodecContext *avctx)
{
Luca Barbato's avatar
Luca Barbato committed
783
#if 0
784 785 786
    c->horizontal_compose97i = ff_snow_horizontal_compose97i_altivec;
    c->vertical_compose97i = ff_snow_vertical_compose97i_altivec;
    c->inner_add_yblock = ff_snow_inner_add_yblock_altivec;
Luca Barbato's avatar
Luca Barbato committed
787
#endif
788
}