h264pred_template.c 43 KB
Newer Older
1 2 3 4
/*
 * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
 * Copyright (c) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 9 10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
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
19 20 21 22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
24
 * H.264 / AVC / MPEG-4 part10 prediction functions.
25 26 27
 * @author Michael Niedermayer <michaelni@gmx.at>
 */

28 29
#include "libavutil/intreadwrite.h"

30
#include "mathops.h"
31 32

#include "bit_depth_template.c"
33

34 35 36
static void FUNCC(pred4x4_vertical)(uint8_t *_src, const uint8_t *topright,
                                    ptrdiff_t _stride)
{
37
    pixel *src = (pixel*)_src;
38
    int stride = _stride>>(sizeof(pixel)-1);
39 40 41 42 43 44
    const pixel4 a= AV_RN4PA(src-stride);

    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
45 46
}

47 48 49
static void FUNCC(pred4x4_horizontal)(uint8_t *_src, const uint8_t *topright,
                                      ptrdiff_t _stride)
{
50
    pixel *src = (pixel*)_src;
51
    int stride = _stride>>(sizeof(pixel)-1);
52 53 54 55
    AV_WN4PA(src+0*stride, PIXEL_SPLAT_X4(src[-1+0*stride]));
    AV_WN4PA(src+1*stride, PIXEL_SPLAT_X4(src[-1+1*stride]));
    AV_WN4PA(src+2*stride, PIXEL_SPLAT_X4(src[-1+2*stride]));
    AV_WN4PA(src+3*stride, PIXEL_SPLAT_X4(src[-1+3*stride]));
56 57
}

58 59 60
static void FUNCC(pred4x4_dc)(uint8_t *_src, const uint8_t *topright,
                              ptrdiff_t _stride)
{
61
    pixel *src = (pixel*)_src;
62
    int stride = _stride>>(sizeof(pixel)-1);
63 64
    const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
                   + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
65
    const pixel4 a = PIXEL_SPLAT_X4(dc);
66

67 68 69 70
    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
71 72
}

73 74 75
static void FUNCC(pred4x4_left_dc)(uint8_t *_src, const uint8_t *topright,
                                   ptrdiff_t _stride)
{
76
    pixel *src = (pixel*)_src;
77
    int stride = _stride>>(sizeof(pixel)-1);
78
    const int dc= (  src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
79
    const pixel4 a = PIXEL_SPLAT_X4(dc);
80

81 82 83 84
    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
85 86
}

87 88 89
static void FUNCC(pred4x4_top_dc)(uint8_t *_src, const uint8_t *topright,
                                  ptrdiff_t _stride)
{
90
    pixel *src = (pixel*)_src;
91
    int stride = _stride>>(sizeof(pixel)-1);
92
    const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
93
    const pixel4 a = PIXEL_SPLAT_X4(dc);
94

95 96 97 98
    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
99 100
}

101 102 103
static void FUNCC(pred4x4_128_dc)(uint8_t *_src, const uint8_t *topright,
                                  ptrdiff_t _stride)
{
104
    pixel *src = (pixel*)_src;
105
    int stride = _stride>>(sizeof(pixel)-1);
106 107 108 109 110 111
    const pixel4 a = PIXEL_SPLAT_X4(1<<(BIT_DEPTH-1));

    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
112 113
}

114 115 116
static void FUNCC(pred4x4_127_dc)(uint8_t *_src, const uint8_t *topright,
                                  ptrdiff_t _stride)
{
117
    pixel *src = (pixel*)_src;
118
    int stride = _stride>>(sizeof(pixel)-1);
119 120 121 122 123 124
    const pixel4 a = PIXEL_SPLAT_X4((1<<(BIT_DEPTH-1))-1);

    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
125 126
}

127 128 129
static void FUNCC(pred4x4_129_dc)(uint8_t *_src, const uint8_t *topright,
                                  ptrdiff_t _stride)
{
130
    pixel *src = (pixel*)_src;
131
    int stride = _stride>>(sizeof(pixel)-1);
132 133 134 135 136 137
    const pixel4 a = PIXEL_SPLAT_X4((1<<(BIT_DEPTH-1))+1);

    AV_WN4PA(src+0*stride, a);
    AV_WN4PA(src+1*stride, a);
    AV_WN4PA(src+2*stride, a);
    AV_WN4PA(src+3*stride, a);
138 139 140 141
}


#define LOAD_TOP_RIGHT_EDGE\
142 143 144 145
    const unsigned av_unused t4 = topright[0];\
    const unsigned av_unused t5 = topright[1];\
    const unsigned av_unused t6 = topright[2];\
    const unsigned av_unused t7 = topright[3];\
146 147

#define LOAD_DOWN_LEFT_EDGE\
148 149 150 151
    const unsigned av_unused l4 = src[-1+4*stride];\
    const unsigned av_unused l5 = src[-1+5*stride];\
    const unsigned av_unused l6 = src[-1+6*stride];\
    const unsigned av_unused l7 = src[-1+7*stride];\
152 153

#define LOAD_LEFT_EDGE\
154 155 156 157
    const unsigned av_unused l0 = src[-1+0*stride];\
    const unsigned av_unused l1 = src[-1+1*stride];\
    const unsigned av_unused l2 = src[-1+2*stride];\
    const unsigned av_unused l3 = src[-1+3*stride];\
158 159

#define LOAD_TOP_EDGE\
160 161 162 163
    const unsigned av_unused t0 = src[ 0-1*stride];\
    const unsigned av_unused t1 = src[ 1-1*stride];\
    const unsigned av_unused t2 = src[ 2-1*stride];\
    const unsigned av_unused t3 = src[ 3-1*stride];\
164

165 166 167
static void FUNCC(pred4x4_down_right)(uint8_t *_src, const uint8_t *topright,
                                      ptrdiff_t _stride)
{
168
    pixel *src = (pixel*)_src;
169
    int stride = _stride>>(sizeof(pixel)-1);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    const int lt= src[-1-1*stride];
    LOAD_TOP_EDGE
    LOAD_LEFT_EDGE

    src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
    src[0+2*stride]=
    src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
    src[0+1*stride]=
    src[1+2*stride]=
    src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
    src[0+0*stride]=
    src[1+1*stride]=
    src[2+2*stride]=
    src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
    src[1+0*stride]=
    src[2+1*stride]=
    src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
    src[2+0*stride]=
    src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
    src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
}

192 193 194
static void FUNCC(pred4x4_down_left)(uint8_t *_src, const uint8_t *_topright,
                                     ptrdiff_t _stride)
{
195 196
    pixel *src = (pixel*)_src;
    const pixel *topright = (const pixel*)_topright;
197
    int stride = _stride>>(sizeof(pixel)-1);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    LOAD_TOP_EDGE
    LOAD_TOP_RIGHT_EDGE
//    LOAD_LEFT_EDGE

    src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
    src[1+0*stride]=
    src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
    src[2+0*stride]=
    src[1+1*stride]=
    src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
    src[3+0*stride]=
    src[2+1*stride]=
    src[1+2*stride]=
    src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
    src[3+1*stride]=
    src[2+2*stride]=
    src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
    src[3+2*stride]=
    src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
    src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
}

220 221 222 223
static void FUNCC(pred4x4_vertical_right)(uint8_t *_src,
                                          const uint8_t *topright,
                                          ptrdiff_t _stride)
{
224
    pixel *src = (pixel*)_src;
225
    int stride = _stride>>(sizeof(pixel)-1);
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    const int lt= src[-1-1*stride];
    LOAD_TOP_EDGE
    LOAD_LEFT_EDGE

    src[0+0*stride]=
    src[1+2*stride]=(lt + t0 + 1)>>1;
    src[1+0*stride]=
    src[2+2*stride]=(t0 + t1 + 1)>>1;
    src[2+0*stride]=
    src[3+2*stride]=(t1 + t2 + 1)>>1;
    src[3+0*stride]=(t2 + t3 + 1)>>1;
    src[0+1*stride]=
    src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
    src[1+1*stride]=
    src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
    src[2+1*stride]=
    src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
    src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
    src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
    src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
}

248 249 250 251
static void FUNCC(pred4x4_vertical_left)(uint8_t *_src,
                                         const uint8_t *_topright,
                                         ptrdiff_t _stride)
{
252 253
    pixel *src = (pixel*)_src;
    const pixel *topright = (const pixel*)_topright;
254
    int stride = _stride>>(sizeof(pixel)-1);
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    LOAD_TOP_EDGE
    LOAD_TOP_RIGHT_EDGE

    src[0+0*stride]=(t0 + t1 + 1)>>1;
    src[1+0*stride]=
    src[0+2*stride]=(t1 + t2 + 1)>>1;
    src[2+0*stride]=
    src[1+2*stride]=(t2 + t3 + 1)>>1;
    src[3+0*stride]=
    src[2+2*stride]=(t3 + t4+ 1)>>1;
    src[3+2*stride]=(t4 + t5+ 1)>>1;
    src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
    src[1+1*stride]=
    src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
    src[2+1*stride]=
    src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
    src[3+1*stride]=
    src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
    src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
}

276 277 278
static void FUNCC(pred4x4_horizontal_up)(uint8_t *_src, const uint8_t *topright,
                                         ptrdiff_t _stride)
{
279
    pixel *src = (pixel*)_src;
280
    int stride = _stride>>(sizeof(pixel)-1);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    LOAD_LEFT_EDGE

    src[0+0*stride]=(l0 + l1 + 1)>>1;
    src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
    src[2+0*stride]=
    src[0+1*stride]=(l1 + l2 + 1)>>1;
    src[3+0*stride]=
    src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
    src[2+1*stride]=
    src[0+2*stride]=(l2 + l3 + 1)>>1;
    src[3+1*stride]=
    src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
    src[3+2*stride]=
    src[1+3*stride]=
    src[0+3*stride]=
    src[2+2*stride]=
    src[2+3*stride]=
    src[3+3*stride]=l3;
}

301 302 303 304
static void FUNCC(pred4x4_horizontal_down)(uint8_t *_src,
                                           const uint8_t *topright,
                                           ptrdiff_t _stride)
{
305
    pixel *src = (pixel*)_src;
306
    int stride = _stride>>(sizeof(pixel)-1);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    const int lt= src[-1-1*stride];
    LOAD_TOP_EDGE
    LOAD_LEFT_EDGE

    src[0+0*stride]=
    src[2+1*stride]=(lt + l0 + 1)>>1;
    src[1+0*stride]=
    src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
    src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
    src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
    src[0+1*stride]=
    src[2+2*stride]=(l0 + l1 + 1)>>1;
    src[1+1*stride]=
    src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
    src[0+2*stride]=
    src[2+3*stride]=(l1 + l2+ 1)>>1;
    src[1+2*stride]=
    src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
    src[0+3*stride]=(l2 + l3 + 1)>>1;
    src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
}

329 330
static void FUNCC(pred16x16_vertical)(uint8_t *_src, ptrdiff_t _stride)
{
331
    int i;
332
    pixel *src = (pixel*)_src;
333
    int stride = _stride>>(sizeof(pixel)-1);
334 335 336 337
    const pixel4 a = AV_RN4PA(((pixel4*)(src-stride))+0);
    const pixel4 b = AV_RN4PA(((pixel4*)(src-stride))+1);
    const pixel4 c = AV_RN4PA(((pixel4*)(src-stride))+2);
    const pixel4 d = AV_RN4PA(((pixel4*)(src-stride))+3);
338 339

    for(i=0; i<16; i++){
340 341 342 343
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
        AV_WN4PA(((pixel4*)(src+i*stride))+2, c);
        AV_WN4PA(((pixel4*)(src+i*stride))+3, d);
344 345 346
    }
}

347 348
static void FUNCC(pred16x16_horizontal)(uint8_t *_src, ptrdiff_t stride)
{
349
    int i;
350
    pixel *src = (pixel*)_src;
351
    stride >>= sizeof(pixel)-1;
352 353

    for(i=0; i<16; i++){
354 355 356 357 358 359
        const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);

        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+2, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+3, a);
360 361 362
    }
}

363 364
#define PREDICT_16x16_DC(v)\
    for(i=0; i<16; i++){\
365 366 367 368
        AV_WN4PA(src+ 0, v);\
        AV_WN4PA(src+ 4, v);\
        AV_WN4PA(src+ 8, v);\
        AV_WN4PA(src+12, v);\
369 370 371
        src += stride;\
    }

372 373
static void FUNCC(pred16x16_dc)(uint8_t *_src, ptrdiff_t stride)
{
374
    int i, dc=0;
375 376
    pixel *src = (pixel*)_src;
    pixel4 dcsplat;
377
    stride >>= sizeof(pixel)-1;
378 379 380 381 382 383 384 385 386

    for(i=0;i<16; i++){
        dc+= src[-1+i*stride];
    }

    for(i=0;i<16; i++){
        dc+= src[i-stride];
    }

387 388
    dcsplat = PIXEL_SPLAT_X4((dc+16)>>5);
    PREDICT_16x16_DC(dcsplat);
389 390
}

391 392
static void FUNCC(pred16x16_left_dc)(uint8_t *_src, ptrdiff_t stride)
{
393
    int i, dc=0;
394 395
    pixel *src = (pixel*)_src;
    pixel4 dcsplat;
396
    stride >>= sizeof(pixel)-1;
397 398 399 400 401

    for(i=0;i<16; i++){
        dc+= src[-1+i*stride];
    }

402 403
    dcsplat = PIXEL_SPLAT_X4((dc+8)>>4);
    PREDICT_16x16_DC(dcsplat);
404 405
}

406 407
static void FUNCC(pred16x16_top_dc)(uint8_t *_src, ptrdiff_t stride)
{
408
    int i, dc=0;
409 410
    pixel *src = (pixel*)_src;
    pixel4 dcsplat;
411
    stride >>= sizeof(pixel)-1;
412 413 414 415 416

    for(i=0;i<16; i++){
        dc+= src[i-stride];
    }

417 418
    dcsplat = PIXEL_SPLAT_X4((dc+8)>>4);
    PREDICT_16x16_DC(dcsplat);
419 420
}

421
#define PRED16x16_X(n, v) \
422 423
static void FUNCC(pred16x16_##n##_dc)(uint8_t *_src, ptrdiff_t stride)\
{\
424 425
    int i;\
    pixel *src = (pixel*)_src;\
426
    stride >>= sizeof(pixel)-1;\
427
    PREDICT_16x16_DC(PIXEL_SPLAT_X4(v));\
428 429
}

430 431 432
PRED16x16_X(127, (1<<(BIT_DEPTH-1))-1)
PRED16x16_X(128, (1<<(BIT_DEPTH-1))+0)
PRED16x16_X(129, (1<<(BIT_DEPTH-1))+1)
433

434 435 436 437 438
static inline void FUNCC(pred16x16_plane_compat)(uint8_t *_src,
                                                 ptrdiff_t _stride,
                                                 const int svq3,
                                                 const int rv40)
{
439 440
  int i, j, k;
  int a;
441 442
  INIT_CLIP
  pixel *src = (pixel*)_src;
443
  int stride = _stride>>(sizeof(pixel)-1);
444 445 446
  const pixel * const src0 = src +7-stride;
  const pixel *       src1 = src +8*stride-1;
  const pixel *       src2 = src1-2*stride;    // == src+6*stride-1;
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
  int H = src0[1] - src0[-1];
  int V = src1[0] - src2[ 0];
  for(k=2; k<=8; ++k) {
    src1 += stride; src2 -= stride;
    H += k*(src0[k] - src0[-k]);
    V += k*(src1[0] - src2[ 0]);
  }
  if(svq3){
    H = ( 5*(H/4) ) / 16;
    V = ( 5*(V/4) ) / 16;

    /* required for 100% accuracy */
    i = H; H = V; V = i;
  }else if(rv40){
    H = ( H + (H>>2) ) >> 4;
    V = ( V + (V>>2) ) >> 4;
  }else{
    H = ( 5*H+32 ) >> 6;
    V = ( 5*V+32 ) >> 6;
  }

  a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
  for(j=16; j>0; --j) {
    int b = a;
    a += V;
    for(i=-16; i<0; i+=4) {
473 474 475 476
      src[16+i] = CLIP((b    ) >> 5);
      src[17+i] = CLIP((b+  H) >> 5);
      src[18+i] = CLIP((b+2*H) >> 5);
      src[19+i] = CLIP((b+3*H) >> 5);
477 478 479 480 481 482
      b += 4*H;
    }
    src += stride;
  }
}

483 484
static void FUNCC(pred16x16_plane)(uint8_t *src, ptrdiff_t stride)
{
485
    FUNCC(pred16x16_plane_compat)(src, stride, 0, 0);
486 487
}

488 489
static void FUNCC(pred8x8_vertical)(uint8_t *_src, ptrdiff_t _stride)
{
490
    int i;
491
    pixel *src = (pixel*)_src;
492
    int stride = _stride>>(sizeof(pixel)-1);
493 494
    const pixel4 a= AV_RN4PA(((pixel4*)(src-stride))+0);
    const pixel4 b= AV_RN4PA(((pixel4*)(src-stride))+1);
495 496

    for(i=0; i<8; i++){
497 498
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
499 500 501
    }
}

502 503
static void FUNCC(pred8x16_vertical)(uint8_t *_src, ptrdiff_t _stride)
{
504 505 506 507 508 509 510 511 512 513 514 515
    int i;
    pixel *src = (pixel*)_src;
    int stride = _stride>>(sizeof(pixel)-1);
    const pixel4 a= AV_RN4PA(((pixel4*)(src-stride))+0);
    const pixel4 b= AV_RN4PA(((pixel4*)(src-stride))+1);

    for(i=0; i<16; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
    }
}

516 517
static void FUNCC(pred8x8_horizontal)(uint8_t *_src, ptrdiff_t stride)
{
518
    int i;
519
    pixel *src = (pixel*)_src;
520
    stride >>= sizeof(pixel)-1;
521 522

    for(i=0; i<8; i++){
523 524 525
        const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
526 527 528
    }
}

529 530
static void FUNCC(pred8x16_horizontal)(uint8_t *_src, ptrdiff_t stride)
{
531 532 533 534 535 536 537 538 539 540
    int i;
    pixel *src = (pixel*)_src;
    stride >>= sizeof(pixel)-1;
    for(i=0; i<16; i++){
        const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
    }
}

541
#define PRED8x8_X(n, v)\
542 543
static void FUNCC(pred8x8_##n##_dc)(uint8_t *_src, ptrdiff_t stride)\
{\
544
    int i;\
545
    const pixel4 a = PIXEL_SPLAT_X4(v);\
546
    pixel *src = (pixel*)_src;\
547
    stride >>= sizeof(pixel)-1;\
548
    for(i=0; i<8; i++){\
549 550
        AV_WN4PA(((pixel4*)(src+i*stride))+0, a);\
        AV_WN4PA(((pixel4*)(src+i*stride))+1, a);\
551
    }\
552 553
}

554 555 556
PRED8x8_X(127, (1<<(BIT_DEPTH-1))-1)
PRED8x8_X(128, (1<<(BIT_DEPTH-1))+0)
PRED8x8_X(129, (1<<(BIT_DEPTH-1))+1)
557

558 559
static void FUNCC(pred8x16_128_dc)(uint8_t *_src, ptrdiff_t stride)
{
560 561 562 563
    FUNCC(pred8x8_128_dc)(_src, stride);
    FUNCC(pred8x8_128_dc)(_src+8*stride, stride);
}

564 565
static void FUNCC(pred8x8_left_dc)(uint8_t *_src, ptrdiff_t stride)
{
566 567
    int i;
    int dc0, dc2;
568 569
    pixel4 dc0splat, dc2splat;
    pixel *src = (pixel*)_src;
570
    stride >>= sizeof(pixel)-1;
571 572 573 574 575 576

    dc0=dc2=0;
    for(i=0;i<4; i++){
        dc0+= src[-1+i*stride];
        dc2+= src[-1+(i+4)*stride];
    }
577 578
    dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
    dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
579 580

    for(i=0; i<4; i++){
581 582
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc0splat);
583 584
    }
    for(i=4; i<8; i++){
585 586
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc2splat);
587 588 589
    }
}

590 591
static void FUNCC(pred8x16_left_dc)(uint8_t *_src, ptrdiff_t stride)
{
592 593 594 595
    FUNCC(pred8x8_left_dc)(_src, stride);
    FUNCC(pred8x8_left_dc)(_src+8*stride, stride);
}

596 597
static void FUNCC(pred8x8_top_dc)(uint8_t *_src, ptrdiff_t stride)
{
598 599
    int i;
    int dc0, dc1;
600 601
    pixel4 dc0splat, dc1splat;
    pixel *src = (pixel*)_src;
602
    stride >>= sizeof(pixel)-1;
603 604 605 606 607 608

    dc0=dc1=0;
    for(i=0;i<4; i++){
        dc0+= src[i-stride];
        dc1+= src[4+i-stride];
    }
609 610
    dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
    dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
611 612

    for(i=0; i<4; i++){
613 614
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
615 616
    }
    for(i=4; i<8; i++){
617 618
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
619 620 621
    }
}

622 623
static void FUNCC(pred8x16_top_dc)(uint8_t *_src, ptrdiff_t stride)
{
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    int i;
    int dc0, dc1;
    pixel4 dc0splat, dc1splat;
    pixel *src = (pixel*)_src;
    stride >>= sizeof(pixel)-1;

    dc0=dc1=0;
    for(i=0;i<4; i++){
        dc0+= src[i-stride];
        dc1+= src[4+i-stride];
    }
    dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
    dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);

    for(i=0; i<16; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
    }
}

644 645
static void FUNCC(pred8x8_dc)(uint8_t *_src, ptrdiff_t stride)
{
646
    int i;
647 648 649
    int dc0, dc1, dc2;
    pixel4 dc0splat, dc1splat, dc2splat, dc3splat;
    pixel *src = (pixel*)_src;
650
    stride >>= sizeof(pixel)-1;
651 652 653 654 655 656 657

    dc0=dc1=dc2=0;
    for(i=0;i<4; i++){
        dc0+= src[-1+i*stride] + src[i-stride];
        dc1+= src[4+i-stride];
        dc2+= src[-1+(i+4)*stride];
    }
658 659 660 661
    dc0splat = PIXEL_SPLAT_X4((dc0 + 4)>>3);
    dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
    dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
    dc3splat = PIXEL_SPLAT_X4((dc1 + dc2 + 4)>>3);
662 663

    for(i=0; i<4; i++){
664 665
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
666 667
    }
    for(i=4; i<8; i++){
668 669
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc3splat);
670 671 672
    }
}

673 674
static void FUNCC(pred8x16_dc)(uint8_t *_src, ptrdiff_t stride)
{
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
    int i;
    int dc0, dc1, dc2, dc3, dc4;
    pixel4 dc0splat, dc1splat, dc2splat, dc3splat, dc4splat, dc5splat, dc6splat, dc7splat;
    pixel *src = (pixel*)_src;
    stride >>= sizeof(pixel)-1;

    dc0=dc1=dc2=dc3=dc4=0;
    for(i=0;i<4; i++){
        dc0+= src[-1+i*stride] + src[i-stride];
        dc1+= src[4+i-stride];
        dc2+= src[-1+(i+4)*stride];
        dc3+= src[-1+(i+8)*stride];
        dc4+= src[-1+(i+12)*stride];
    }
    dc0splat = PIXEL_SPLAT_X4((dc0 + 4)>>3);
    dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
    dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
    dc3splat = PIXEL_SPLAT_X4((dc1 + dc2 + 4)>>3);
    dc4splat = PIXEL_SPLAT_X4((dc3 + 2)>>2);
    dc5splat = PIXEL_SPLAT_X4((dc1 + dc3 + 4)>>3);
    dc6splat = PIXEL_SPLAT_X4((dc4 + 2)>>2);
    dc7splat = PIXEL_SPLAT_X4((dc1 + dc4 + 4)>>3);

    for(i=0; i<4; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
    }
    for(i=4; i<8; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc3splat);
    }
    for(i=8; i<12; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc4splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc5splat);
    }
    for(i=12; i<16; i++){
        AV_WN4PA(((pixel4*)(src+i*stride))+0, dc6splat);
        AV_WN4PA(((pixel4*)(src+i*stride))+1, dc7splat);
    }
}

716
//the following 4 function should not be optimized!
717 718
static void FUNC(pred8x8_mad_cow_dc_l0t)(uint8_t *src, ptrdiff_t stride)
{
719 720
    FUNCC(pred8x8_top_dc)(src, stride);
    FUNCC(pred4x4_dc)(src, NULL, stride);
721 722
}

723 724
static void FUNC(pred8x16_mad_cow_dc_l0t)(uint8_t *src, ptrdiff_t stride)
{
725 726 727 728
    FUNCC(pred8x16_top_dc)(src, stride);
    FUNCC(pred4x4_dc)(src, NULL, stride);
}

729 730
static void FUNC(pred8x8_mad_cow_dc_0lt)(uint8_t *src, ptrdiff_t stride)
{
731 732
    FUNCC(pred8x8_dc)(src, stride);
    FUNCC(pred4x4_top_dc)(src, NULL, stride);
733 734
}

735 736
static void FUNC(pred8x16_mad_cow_dc_0lt)(uint8_t *src, ptrdiff_t stride)
{
737 738 739 740
    FUNCC(pred8x16_dc)(src, stride);
    FUNCC(pred4x4_top_dc)(src, NULL, stride);
}

741 742
static void FUNC(pred8x8_mad_cow_dc_l00)(uint8_t *src, ptrdiff_t stride)
{
743 744 745
    FUNCC(pred8x8_left_dc)(src, stride);
    FUNCC(pred4x4_128_dc)(src + 4*stride                  , NULL, stride);
    FUNCC(pred4x4_128_dc)(src + 4*stride + 4*sizeof(pixel), NULL, stride);
746 747
}

748 749
static void FUNC(pred8x16_mad_cow_dc_l00)(uint8_t *src, ptrdiff_t stride)
{
750 751 752 753 754
    FUNCC(pred8x16_left_dc)(src, stride);
    FUNCC(pred4x4_128_dc)(src + 4*stride                  , NULL, stride);
    FUNCC(pred4x4_128_dc)(src + 4*stride + 4*sizeof(pixel), NULL, stride);
}

755 756
static void FUNC(pred8x8_mad_cow_dc_0l0)(uint8_t *src, ptrdiff_t stride)
{
757 758 759
    FUNCC(pred8x8_left_dc)(src, stride);
    FUNCC(pred4x4_128_dc)(src                  , NULL, stride);
    FUNCC(pred4x4_128_dc)(src + 4*sizeof(pixel), NULL, stride);
760 761
}

762 763
static void FUNC(pred8x16_mad_cow_dc_0l0)(uint8_t *src, ptrdiff_t stride)
{
764 765 766 767 768
    FUNCC(pred8x16_left_dc)(src, stride);
    FUNCC(pred4x4_128_dc)(src                  , NULL, stride);
    FUNCC(pred4x4_128_dc)(src + 4*sizeof(pixel), NULL, stride);
}

769 770
static void FUNCC(pred8x8_plane)(uint8_t *_src, ptrdiff_t _stride)
{
771 772
  int j, k;
  int a;
773 774
  INIT_CLIP
  pixel *src = (pixel*)_src;
775
  int stride = _stride>>(sizeof(pixel)-1);
776 777 778
  const pixel * const src0 = src +3-stride;
  const pixel *       src1 = src +4*stride-1;
  const pixel *       src2 = src1-2*stride;    // == src+2*stride-1;
779 780 781 782 783 784 785 786 787 788 789 790 791 792
  int H = src0[1] - src0[-1];
  int V = src1[0] - src2[ 0];
  for(k=2; k<=4; ++k) {
    src1 += stride; src2 -= stride;
    H += k*(src0[k] - src0[-k]);
    V += k*(src1[0] - src2[ 0]);
  }
  H = ( 17*H+16 ) >> 5;
  V = ( 17*V+16 ) >> 5;

  a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
  for(j=8; j>0; --j) {
    int b = a;
    a += V;
793 794 795 796 797 798 799 800
    src[0] = CLIP((b    ) >> 5);
    src[1] = CLIP((b+  H) >> 5);
    src[2] = CLIP((b+2*H) >> 5);
    src[3] = CLIP((b+3*H) >> 5);
    src[4] = CLIP((b+4*H) >> 5);
    src[5] = CLIP((b+5*H) >> 5);
    src[6] = CLIP((b+6*H) >> 5);
    src[7] = CLIP((b+7*H) >> 5);
801 802 803 804
    src += stride;
  }
}

805 806
static void FUNCC(pred8x16_plane)(uint8_t *_src, ptrdiff_t _stride)
{
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  int j, k;
  int a;
  INIT_CLIP
  pixel *src = (pixel*)_src;
  int stride = _stride>>(sizeof(pixel)-1);
  const pixel * const src0 = src +3-stride;
  const pixel *       src1 = src +8*stride-1;
  const pixel *       src2 = src1-2*stride;    // == src+6*stride-1;
  int H = src0[1] - src0[-1];
  int V = src1[0] - src2[ 0];

  for (k = 2; k <= 4; ++k) {
      src1 += stride; src2 -= stride;
      H += k*(src0[k] - src0[-k]);
      V += k*(src1[0] - src2[ 0]);
  }
  for (; k <= 8; ++k) {
      src1 += stride; src2 -= stride;
      V += k*(src1[0] - src2[0]);
  }

  H = (17*H+16) >> 5;
  V = (5*V+32) >> 6;

  a = 16*(src1[0] + src2[8] + 1) - 7*V - 3*H;
  for(j=16; j>0; --j) {
    int b = a;
    a += V;
    src[0] = CLIP((b    ) >> 5);
    src[1] = CLIP((b+  H) >> 5);
    src[2] = CLIP((b+2*H) >> 5);
    src[3] = CLIP((b+3*H) >> 5);
    src[4] = CLIP((b+4*H) >> 5);
    src[5] = CLIP((b+5*H) >> 5);
    src[6] = CLIP((b+6*H) >> 5);
    src[7] = CLIP((b+7*H) >> 5);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
    src += stride;
  }
}

#define SRC(x,y) src[(x)+(y)*stride]
#define PL(y) \
    const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
#define PREDICT_8x8_LOAD_LEFT \
    const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
                     + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
    PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
    const int l7 av_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2

#define PT(x) \
    const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
#define PREDICT_8x8_LOAD_TOP \
    const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
                     + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
    PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
    const int t7 av_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
                     + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2

#define PTR(x) \
    t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
#define PREDICT_8x8_LOAD_TOPRIGHT \
    int t8, t9, t10, t11, t12, t13, t14, t15; \
    if(has_topright) { \
        PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
        t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
    } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);

#define PREDICT_8x8_LOAD_TOPLEFT \
    const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2

#define PREDICT_8x8_DC(v) \
    int y; \
    for( y = 0; y < 8; y++ ) { \
880 881
        AV_WN4PA(((pixel4*)src)+0, v); \
        AV_WN4PA(((pixel4*)src)+1, v); \
882 883 884
        src += stride; \
    }

885 886
static void FUNCC(pred8x8l_128_dc)(uint8_t *_src, int has_topleft,
                                   int has_topright, ptrdiff_t _stride)
887
{
888
    pixel *src = (pixel*)_src;
889
    int stride = _stride>>(sizeof(pixel)-1);
890 891

    PREDICT_8x8_DC(PIXEL_SPLAT_X4(1<<(BIT_DEPTH-1)));
892
}
893 894
static void FUNCC(pred8x8l_left_dc)(uint8_t *_src, int has_topleft,
                                    int has_topright, ptrdiff_t _stride)
895
{
896
    pixel *src = (pixel*)_src;
897
    int stride = _stride>>(sizeof(pixel)-1);
898

899
    PREDICT_8x8_LOAD_LEFT;
900
    const pixel4 dc = PIXEL_SPLAT_X4((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3);
901 902
    PREDICT_8x8_DC(dc);
}
903 904
static void FUNCC(pred8x8l_top_dc)(uint8_t *_src, int has_topleft,
                                   int has_topright, ptrdiff_t _stride)
905
{
906
    pixel *src = (pixel*)_src;
907
    int stride = _stride>>(sizeof(pixel)-1);
908

909
    PREDICT_8x8_LOAD_TOP;
910
    const pixel4 dc = PIXEL_SPLAT_X4((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3);
911 912
    PREDICT_8x8_DC(dc);
}
913 914
static void FUNCC(pred8x8l_dc)(uint8_t *_src, int has_topleft,
                               int has_topright, ptrdiff_t _stride)
915
{
916
    pixel *src = (pixel*)_src;
917
    int stride = _stride>>(sizeof(pixel)-1);
918

919 920
    PREDICT_8x8_LOAD_LEFT;
    PREDICT_8x8_LOAD_TOP;
921 922
    const pixel4 dc = PIXEL_SPLAT_X4((l0+l1+l2+l3+l4+l5+l6+l7
                                     +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4);
923 924
    PREDICT_8x8_DC(dc);
}
925 926
static void FUNCC(pred8x8l_horizontal)(uint8_t *_src, int has_topleft,
                                       int has_topright, ptrdiff_t _stride)
927
{
928
    pixel *src = (pixel*)_src;
929
    int stride = _stride>>(sizeof(pixel)-1);
930
    pixel4 a;
931

932
    PREDICT_8x8_LOAD_LEFT;
933 934 935
#define ROW(y) a = PIXEL_SPLAT_X4(l##y); \
               AV_WN4PA(src+y*stride, a); \
               AV_WN4PA(src+y*stride+4, a);
936 937 938
    ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
#undef ROW
}
939 940
static void FUNCC(pred8x8l_vertical)(uint8_t *_src, int has_topleft,
                                     int has_topright, ptrdiff_t _stride)
941 942
{
    int y;
943
    pixel *src = (pixel*)_src;
944
    int stride = _stride>>(sizeof(pixel)-1);
945
    pixel4 a, b;
946

947 948 949 950 951 952 953 954 955
    PREDICT_8x8_LOAD_TOP;
    src[0] = t0;
    src[1] = t1;
    src[2] = t2;
    src[3] = t3;
    src[4] = t4;
    src[5] = t5;
    src[6] = t6;
    src[7] = t7;
956 957
    a = AV_RN4PA(((pixel4*)src)+0);
    b = AV_RN4PA(((pixel4*)src)+1);
958
    for( y = 1; y < 8; y++ ) {
959 960
        AV_WN4PA(((pixel4*)(src+y*stride))+0, a);
        AV_WN4PA(((pixel4*)(src+y*stride))+1, b);
961
    }
962
}
963 964
static void FUNCC(pred8x8l_down_left)(uint8_t *_src, int has_topleft,
                                      int has_topright, ptrdiff_t _stride)
965
{
966
    pixel *src = (pixel*)_src;
967
    int stride = _stride>>(sizeof(pixel)-1);
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
    PREDICT_8x8_LOAD_TOP;
    PREDICT_8x8_LOAD_TOPRIGHT;
    SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
    SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
    SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
    SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
    SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
    SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
    SRC(0,6)=SRC(1,5)=SRC(2,4)=SRC(3,3)=SRC(4,2)=SRC(5,1)=SRC(6,0)= (t6 + 2*t7 + t8 + 2) >> 2;
    SRC(0,7)=SRC(1,6)=SRC(2,5)=SRC(3,4)=SRC(4,3)=SRC(5,2)=SRC(6,1)=SRC(7,0)= (t7 + 2*t8 + t9 + 2) >> 2;
    SRC(1,7)=SRC(2,6)=SRC(3,5)=SRC(4,4)=SRC(5,3)=SRC(6,2)=SRC(7,1)= (t8 + 2*t9 + t10 + 2) >> 2;
    SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
    SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
    SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
    SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
    SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
    SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
}
986 987
static void FUNCC(pred8x8l_down_right)(uint8_t *_src, int has_topleft,
                                       int has_topright, ptrdiff_t _stride)
988
{
989
    pixel *src = (pixel*)_src;
990
    int stride = _stride>>(sizeof(pixel)-1);
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
    PREDICT_8x8_LOAD_TOP;
    PREDICT_8x8_LOAD_LEFT;
    PREDICT_8x8_LOAD_TOPLEFT;
    SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
    SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
    SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
    SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
    SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
    SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
    SRC(0,1)=SRC(1,2)=SRC(2,3)=SRC(3,4)=SRC(4,5)=SRC(5,6)=SRC(6,7)= (l1 + 2*l0 + lt + 2) >> 2;
    SRC(0,0)=SRC(1,1)=SRC(2,2)=SRC(3,3)=SRC(4,4)=SRC(5,5)=SRC(6,6)=SRC(7,7)= (l0 + 2*lt + t0 + 2) >> 2;
    SRC(1,0)=SRC(2,1)=SRC(3,2)=SRC(4,3)=SRC(5,4)=SRC(6,5)=SRC(7,6)= (lt + 2*t0 + t1 + 2) >> 2;
    SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
    SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
    SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
    SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
    SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
    SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
}
1010 1011
static void FUNCC(pred8x8l_vertical_right)(uint8_t *_src, int has_topleft,
                                           int has_topright, ptrdiff_t _stride)
1012
{
1013
    pixel *src = (pixel*)_src;
1014
    int stride = _stride>>(sizeof(pixel)-1);
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    PREDICT_8x8_LOAD_TOP;
    PREDICT_8x8_LOAD_LEFT;
    PREDICT_8x8_LOAD_TOPLEFT;
    SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
    SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
    SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
    SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
    SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
    SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
    SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
    SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
    SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
    SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
    SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
    SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
    SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
    SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
    SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
    SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
    SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
    SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
    SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
    SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
    SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
    SRC(7,0)= (t6 + t7 + 1) >> 1;
}
1041 1042
static void FUNCC(pred8x8l_horizontal_down)(uint8_t *_src, int has_topleft,
                                            int has_topright, ptrdiff_t _stride)
1043
{
1044
    pixel *src = (pixel*)_src;
1045
    int stride = _stride>>(sizeof(pixel)-1);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
    PREDICT_8x8_LOAD_TOP;
    PREDICT_8x8_LOAD_LEFT;
    PREDICT_8x8_LOAD_TOPLEFT;
    SRC(0,7)= (l6 + l7 + 1) >> 1;
    SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
    SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
    SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
    SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
    SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
    SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
    SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
    SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
    SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
    SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
    SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
    SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
    SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
    SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
    SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
    SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
    SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
    SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
    SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
    SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
    SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
}
1072 1073
static void FUNCC(pred8x8l_vertical_left)(uint8_t *_src, int has_topleft,
                                          int has_topright, ptrdiff_t _stride)
1074
{
1075
    pixel *src = (pixel*)_src;
1076
    int stride = _stride>>(sizeof(pixel)-1);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    PREDICT_8x8_LOAD_TOP;
    PREDICT_8x8_LOAD_TOPRIGHT;
    SRC(0,0)= (t0 + t1 + 1) >> 1;
    SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
    SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
    SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
    SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
    SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
    SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
    SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
    SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
    SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
    SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
    SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
    SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
    SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
    SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
    SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
    SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
    SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
    SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
    SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
    SRC(7,6)= (t10 + t11 + 1) >> 1;
    SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
}
1102 1103
static void FUNCC(pred8x8l_horizontal_up)(uint8_t *_src, int has_topleft,
                                          int has_topright, ptrdiff_t _stride)
1104
{
1105
    pixel *src = (pixel*)_src;
1106
    int stride = _stride>>(sizeof(pixel)-1);
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    PREDICT_8x8_LOAD_LEFT;
    SRC(0,0)= (l0 + l1 + 1) >> 1;
    SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
    SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
    SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
    SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
    SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
    SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
    SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
    SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
    SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
    SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
    SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
    SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
    SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
    SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
    SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
    SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
    SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
}
1127 1128

static void FUNCC(pred8x8l_vertical_filter_add)(uint8_t *_src, int16_t *_block, int has_topleft,
1129
                                     int has_topright, ptrdiff_t _stride)
1130 1131 1132 1133 1134
{
    int i;
    pixel *src = (pixel*)_src;
    const dctcoef *block = (const dctcoef*)_block;
    pixel pix[8];
1135
    int stride = _stride>>(sizeof(pixel)-1);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
    PREDICT_8x8_LOAD_TOP;

    pix[0] = t0;
    pix[1] = t1;
    pix[2] = t2;
    pix[3] = t3;
    pix[4] = t4;
    pix[5] = t5;
    pix[6] = t6;
    pix[7] = t7;

    for(i=0; i<8; i++){
        pixel v = pix[i];
        src[0*stride]= v += block[0];
        src[1*stride]= v += block[8];
        src[2*stride]= v += block[16];
        src[3*stride]= v += block[24];
        src[4*stride]= v += block[32];
        src[5*stride]= v += block[40];
        src[6*stride]= v += block[48];
        src[7*stride]= v +  block[56];
        src++;
        block++;
    }

    memset(_block, 0, sizeof(dctcoef) * 64);
}

static void FUNCC(pred8x8l_horizontal_filter_add)(uint8_t *_src, int16_t *_block, int has_topleft,
1165
                               int has_topright, ptrdiff_t _stride)
1166 1167 1168 1169 1170
{
    int i;
    pixel *src = (pixel*)_src;
    const dctcoef *block = (const dctcoef*)_block;
    pixel pix[8];
1171
    int stride = _stride>>(sizeof(pixel)-1);
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
    PREDICT_8x8_LOAD_LEFT;

    pix[0] = l0;
    pix[1] = l1;
    pix[2] = l2;
    pix[3] = l3;
    pix[4] = l4;
    pix[5] = l5;
    pix[6] = l6;
    pix[7] = l7;

    for(i=0; i<8; i++){
        pixel v = pix[i];
        src[0]= v += block[0];
        src[1]= v += block[1];
        src[2]= v += block[2];
        src[3]= v += block[3];
        src[4]= v += block[4];
        src[5]= v += block[5];
        src[6]= v += block[6];
        src[7]= v +  block[7];
        src+= stride;
        block+= 8;
    }

    memset(_block, 0, sizeof(dctcoef) * 64);
}

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
#undef PREDICT_8x8_LOAD_LEFT
#undef PREDICT_8x8_LOAD_TOP
#undef PREDICT_8x8_LOAD_TOPLEFT
#undef PREDICT_8x8_LOAD_TOPRIGHT
#undef PREDICT_8x8_DC
#undef PTR
#undef PT
#undef PL
#undef SRC

1210
static void FUNCC(pred4x4_vertical_add)(uint8_t *_pix, int16_t *_block,
1211 1212
                                        ptrdiff_t stride)
{
1213
    int i;
1214 1215
    pixel *pix = (pixel*)_pix;
    const dctcoef *block = (const dctcoef*)_block;
1216
    stride >>= sizeof(pixel)-1;
1217 1218
    pix -= stride;
    for(i=0; i<4; i++){
1219
        pixel v = pix[0];
1220 1221 1222 1223 1224 1225 1226
        pix[1*stride]= v += block[0];
        pix[2*stride]= v += block[4];
        pix[3*stride]= v += block[8];
        pix[4*stride]= v +  block[12];
        pix++;
        block++;
    }
1227 1228

    memset(_block, 0, sizeof(dctcoef) * 16);
1229 1230
}

1231
static void FUNCC(pred4x4_horizontal_add)(uint8_t *_pix, int16_t *_block,
1232 1233
                                          ptrdiff_t stride)
{
1234
    int i;
1235 1236
    pixel *pix = (pixel*)_pix;
    const dctcoef *block = (const dctcoef*)_block;
1237
    stride >>= sizeof(pixel)-1;
1238
    for(i=0; i<4; i++){
1239
        pixel v = pix[-1];
1240 1241 1242 1243 1244 1245 1246
        pix[0]= v += block[0];
        pix[1]= v += block[1];
        pix[2]= v += block[2];
        pix[3]= v +  block[3];
        pix+= stride;
        block+= 4;
    }
1247 1248

    memset(_block, 0, sizeof(dctcoef) * 16);
1249 1250
}

1251
static void FUNCC(pred8x8l_vertical_add)(uint8_t *_pix, int16_t *_block,
1252 1253
                                         ptrdiff_t stride)
{
1254
    int i;
1255 1256
    pixel *pix = (pixel*)_pix;
    const dctcoef *block = (const dctcoef*)_block;
1257
    stride >>= sizeof(pixel)-1;
1258 1259
    pix -= stride;
    for(i=0; i<8; i++){
1260
        pixel v = pix[0];
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
        pix[1*stride]= v += block[0];
        pix[2*stride]= v += block[8];
        pix[3*stride]= v += block[16];
        pix[4*stride]= v += block[24];
        pix[5*stride]= v += block[32];
        pix[6*stride]= v += block[40];
        pix[7*stride]= v += block[48];
        pix[8*stride]= v +  block[56];
        pix++;
        block++;
    }
1272 1273

    memset(_block, 0, sizeof(dctcoef) * 64);
1274 1275
}

1276
static void FUNCC(pred8x8l_horizontal_add)(uint8_t *_pix, int16_t *_block,
1277 1278
                                           ptrdiff_t stride)
{
1279
    int i;
1280 1281
    pixel *pix = (pixel*)_pix;
    const dctcoef *block = (const dctcoef*)_block;
1282
    stride >>= sizeof(pixel)-1;
1283
    for(i=0; i<8; i++){
1284
        pixel v = pix[-1];
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
        pix[0]= v += block[0];
        pix[1]= v += block[1];
        pix[2]= v += block[2];
        pix[3]= v += block[3];
        pix[4]= v += block[4];
        pix[5]= v += block[5];
        pix[6]= v += block[6];
        pix[7]= v +  block[7];
        pix+= stride;
        block+= 8;
    }
1296 1297

    memset(_block, 0, sizeof(dctcoef) * 64);
1298 1299
}

1300
static void FUNCC(pred16x16_vertical_add)(uint8_t *pix, const int *block_offset,
1301
                                          int16_t *block,
1302 1303
                                          ptrdiff_t stride)
{
1304 1305
    int i;
    for(i=0; i<16; i++)
1306
        FUNCC(pred4x4_vertical_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1307 1308
}

1309 1310
static void FUNCC(pred16x16_horizontal_add)(uint8_t *pix,
                                            const int *block_offset,
1311
                                            int16_t *block,
1312 1313
                                            ptrdiff_t stride)
{
1314 1315
    int i;
    for(i=0; i<16; i++)
1316
        FUNCC(pred4x4_horizontal_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1317 1318
}

1319
static void FUNCC(pred8x8_vertical_add)(uint8_t *pix, const int *block_offset,
1320
                                        int16_t *block, ptrdiff_t stride)
1321
{
1322 1323
    int i;
    for(i=0; i<4; i++)
1324
        FUNCC(pred4x4_vertical_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1325 1326
}

1327
static void FUNCC(pred8x16_vertical_add)(uint8_t *pix, const int *block_offset,
1328
                                         int16_t *block, ptrdiff_t stride)
1329
{
1330 1331 1332 1333 1334 1335 1336
    int i;
    for(i=0; i<4; i++)
        FUNCC(pred4x4_vertical_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
    for(i=4; i<8; i++)
        FUNCC(pred4x4_vertical_add)(pix + block_offset[i+4], block + i*16*sizeof(pixel), stride);
}

1337
static void FUNCC(pred8x8_horizontal_add)(uint8_t *pix, const int *block_offset,
1338
                                          int16_t *block,
1339 1340
                                          ptrdiff_t stride)
{
1341 1342
    int i;
    for(i=0; i<4; i++)
1343
        FUNCC(pred4x4_horizontal_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1344
}
1345

1346 1347
static void FUNCC(pred8x16_horizontal_add)(uint8_t *pix,
                                           const int *block_offset,
1348
                                           int16_t *block, ptrdiff_t stride)
1349
{
1350 1351 1352 1353 1354 1355
    int i;
    for(i=0; i<4; i++)
        FUNCC(pred4x4_horizontal_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
    for(i=4; i<8; i++)
        FUNCC(pred4x4_horizontal_add)(pix + block_offset[i+4], block + i*16*sizeof(pixel), stride);
}