]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264pred_template.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / h264pred_template.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... encoder/decoder
3  * Copyright (c) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * 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  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 prediction functions.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include "mathops.h"
29
30 #include "bit_depth_template.c"
31
32 static void FUNCC(pred4x4_vertical)(uint8_t *_src, const uint8_t *topright, int _stride){
33     pixel *src = (pixel*)_src;
34     int stride = _stride>>(sizeof(pixel)-1);
35     const pixel4 a= AV_RN4PA(src-stride);
36
37     AV_WN4PA(src+0*stride, a);
38     AV_WN4PA(src+1*stride, a);
39     AV_WN4PA(src+2*stride, a);
40     AV_WN4PA(src+3*stride, a);
41 }
42
43 static void FUNCC(pred4x4_horizontal)(uint8_t *_src, const uint8_t *topright, int _stride){
44     pixel *src = (pixel*)_src;
45     int stride = _stride>>(sizeof(pixel)-1);
46     AV_WN4PA(src+0*stride, PIXEL_SPLAT_X4(src[-1+0*stride]));
47     AV_WN4PA(src+1*stride, PIXEL_SPLAT_X4(src[-1+1*stride]));
48     AV_WN4PA(src+2*stride, PIXEL_SPLAT_X4(src[-1+2*stride]));
49     AV_WN4PA(src+3*stride, PIXEL_SPLAT_X4(src[-1+3*stride]));
50 }
51
52 static void FUNCC(pred4x4_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
53     pixel *src = (pixel*)_src;
54     int stride = _stride>>(sizeof(pixel)-1);
55     const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride]
56                    + src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 4) >>3;
57     const pixel4 a = PIXEL_SPLAT_X4(dc);
58
59     AV_WN4PA(src+0*stride, a);
60     AV_WN4PA(src+1*stride, a);
61     AV_WN4PA(src+2*stride, a);
62     AV_WN4PA(src+3*stride, a);
63 }
64
65 static void FUNCC(pred4x4_left_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
66     pixel *src = (pixel*)_src;
67     int stride = _stride>>(sizeof(pixel)-1);
68     const int dc= (  src[-1+0*stride] + src[-1+1*stride] + src[-1+2*stride] + src[-1+3*stride] + 2) >>2;
69     const pixel4 a = PIXEL_SPLAT_X4(dc);
70
71     AV_WN4PA(src+0*stride, a);
72     AV_WN4PA(src+1*stride, a);
73     AV_WN4PA(src+2*stride, a);
74     AV_WN4PA(src+3*stride, a);
75 }
76
77 static void FUNCC(pred4x4_top_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
78     pixel *src = (pixel*)_src;
79     int stride = _stride>>(sizeof(pixel)-1);
80     const int dc= (  src[-stride] + src[1-stride] + src[2-stride] + src[3-stride] + 2) >>2;
81     const pixel4 a = PIXEL_SPLAT_X4(dc);
82
83     AV_WN4PA(src+0*stride, a);
84     AV_WN4PA(src+1*stride, a);
85     AV_WN4PA(src+2*stride, a);
86     AV_WN4PA(src+3*stride, a);
87 }
88
89 static void FUNCC(pred4x4_128_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
90     pixel *src = (pixel*)_src;
91     int stride = _stride>>(sizeof(pixel)-1);
92     const pixel4 a = PIXEL_SPLAT_X4(1<<(BIT_DEPTH-1));
93
94     AV_WN4PA(src+0*stride, a);
95     AV_WN4PA(src+1*stride, a);
96     AV_WN4PA(src+2*stride, a);
97     AV_WN4PA(src+3*stride, a);
98 }
99
100 static void FUNCC(pred4x4_127_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
101     pixel *src = (pixel*)_src;
102     int stride = _stride>>(sizeof(pixel)-1);
103     const pixel4 a = PIXEL_SPLAT_X4((1<<(BIT_DEPTH-1))-1);
104
105     AV_WN4PA(src+0*stride, a);
106     AV_WN4PA(src+1*stride, a);
107     AV_WN4PA(src+2*stride, a);
108     AV_WN4PA(src+3*stride, a);
109 }
110
111 static void FUNCC(pred4x4_129_dc)(uint8_t *_src, const uint8_t *topright, int _stride){
112     pixel *src = (pixel*)_src;
113     int stride = _stride>>(sizeof(pixel)-1);
114     const pixel4 a = PIXEL_SPLAT_X4((1<<(BIT_DEPTH-1))+1);
115
116     AV_WN4PA(src+0*stride, a);
117     AV_WN4PA(src+1*stride, a);
118     AV_WN4PA(src+2*stride, a);
119     AV_WN4PA(src+3*stride, a);
120 }
121
122
123 #define LOAD_TOP_RIGHT_EDGE\
124     const unsigned av_unused t4 = topright[0];\
125     const unsigned av_unused t5 = topright[1];\
126     const unsigned av_unused t6 = topright[2];\
127     const unsigned av_unused t7 = topright[3];\
128
129 #define LOAD_DOWN_LEFT_EDGE\
130     const unsigned av_unused l4 = src[-1+4*stride];\
131     const unsigned av_unused l5 = src[-1+5*stride];\
132     const unsigned av_unused l6 = src[-1+6*stride];\
133     const unsigned av_unused l7 = src[-1+7*stride];\
134
135 #define LOAD_LEFT_EDGE\
136     const unsigned av_unused l0 = src[-1+0*stride];\
137     const unsigned av_unused l1 = src[-1+1*stride];\
138     const unsigned av_unused l2 = src[-1+2*stride];\
139     const unsigned av_unused l3 = src[-1+3*stride];\
140
141 #define LOAD_TOP_EDGE\
142     const unsigned av_unused t0 = src[ 0-1*stride];\
143     const unsigned av_unused t1 = src[ 1-1*stride];\
144     const unsigned av_unused t2 = src[ 2-1*stride];\
145     const unsigned av_unused t3 = src[ 3-1*stride];\
146
147 static void FUNCC(pred4x4_down_right)(uint8_t *_src, const uint8_t *topright, int _stride){
148     pixel *src = (pixel*)_src;
149     int stride = _stride>>(sizeof(pixel)-1);
150     const int lt= src[-1-1*stride];
151     LOAD_TOP_EDGE
152     LOAD_LEFT_EDGE
153
154     src[0+3*stride]=(l3 + 2*l2 + l1 + 2)>>2;
155     src[0+2*stride]=
156     src[1+3*stride]=(l2 + 2*l1 + l0 + 2)>>2;
157     src[0+1*stride]=
158     src[1+2*stride]=
159     src[2+3*stride]=(l1 + 2*l0 + lt + 2)>>2;
160     src[0+0*stride]=
161     src[1+1*stride]=
162     src[2+2*stride]=
163     src[3+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
164     src[1+0*stride]=
165     src[2+1*stride]=
166     src[3+2*stride]=(lt + 2*t0 + t1 + 2)>>2;
167     src[2+0*stride]=
168     src[3+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
169     src[3+0*stride]=(t1 + 2*t2 + t3 + 2)>>2;
170 }
171
172 static void FUNCC(pred4x4_down_left)(uint8_t *_src, const uint8_t *_topright, int _stride){
173     pixel *src = (pixel*)_src;
174     const pixel *topright = (const pixel*)_topright;
175     int stride = _stride>>(sizeof(pixel)-1);
176     LOAD_TOP_EDGE
177     LOAD_TOP_RIGHT_EDGE
178 //    LOAD_LEFT_EDGE
179
180     src[0+0*stride]=(t0 + t2 + 2*t1 + 2)>>2;
181     src[1+0*stride]=
182     src[0+1*stride]=(t1 + t3 + 2*t2 + 2)>>2;
183     src[2+0*stride]=
184     src[1+1*stride]=
185     src[0+2*stride]=(t2 + t4 + 2*t3 + 2)>>2;
186     src[3+0*stride]=
187     src[2+1*stride]=
188     src[1+2*stride]=
189     src[0+3*stride]=(t3 + t5 + 2*t4 + 2)>>2;
190     src[3+1*stride]=
191     src[2+2*stride]=
192     src[1+3*stride]=(t4 + t6 + 2*t5 + 2)>>2;
193     src[3+2*stride]=
194     src[2+3*stride]=(t5 + t7 + 2*t6 + 2)>>2;
195     src[3+3*stride]=(t6 + 3*t7 + 2)>>2;
196 }
197
198 static void FUNCC(pred4x4_vertical_right)(uint8_t *_src, const uint8_t *topright, int _stride){
199     pixel *src = (pixel*)_src;
200     int stride = _stride>>(sizeof(pixel)-1);
201     const int lt= src[-1-1*stride];
202     LOAD_TOP_EDGE
203     LOAD_LEFT_EDGE
204
205     src[0+0*stride]=
206     src[1+2*stride]=(lt + t0 + 1)>>1;
207     src[1+0*stride]=
208     src[2+2*stride]=(t0 + t1 + 1)>>1;
209     src[2+0*stride]=
210     src[3+2*stride]=(t1 + t2 + 1)>>1;
211     src[3+0*stride]=(t2 + t3 + 1)>>1;
212     src[0+1*stride]=
213     src[1+3*stride]=(l0 + 2*lt + t0 + 2)>>2;
214     src[1+1*stride]=
215     src[2+3*stride]=(lt + 2*t0 + t1 + 2)>>2;
216     src[2+1*stride]=
217     src[3+3*stride]=(t0 + 2*t1 + t2 + 2)>>2;
218     src[3+1*stride]=(t1 + 2*t2 + t3 + 2)>>2;
219     src[0+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
220     src[0+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
221 }
222
223 static void FUNCC(pred4x4_vertical_left)(uint8_t *_src, const uint8_t *_topright, int _stride){
224     pixel *src = (pixel*)_src;
225     const pixel *topright = (const pixel*)_topright;
226     int stride = _stride>>(sizeof(pixel)-1);
227     LOAD_TOP_EDGE
228     LOAD_TOP_RIGHT_EDGE
229
230     src[0+0*stride]=(t0 + t1 + 1)>>1;
231     src[1+0*stride]=
232     src[0+2*stride]=(t1 + t2 + 1)>>1;
233     src[2+0*stride]=
234     src[1+2*stride]=(t2 + t3 + 1)>>1;
235     src[3+0*stride]=
236     src[2+2*stride]=(t3 + t4+ 1)>>1;
237     src[3+2*stride]=(t4 + t5+ 1)>>1;
238     src[0+1*stride]=(t0 + 2*t1 + t2 + 2)>>2;
239     src[1+1*stride]=
240     src[0+3*stride]=(t1 + 2*t2 + t3 + 2)>>2;
241     src[2+1*stride]=
242     src[1+3*stride]=(t2 + 2*t3 + t4 + 2)>>2;
243     src[3+1*stride]=
244     src[2+3*stride]=(t3 + 2*t4 + t5 + 2)>>2;
245     src[3+3*stride]=(t4 + 2*t5 + t6 + 2)>>2;
246 }
247
248 static void FUNCC(pred4x4_horizontal_up)(uint8_t *_src, const uint8_t *topright, int _stride){
249     pixel *src = (pixel*)_src;
250     int stride = _stride>>(sizeof(pixel)-1);
251     LOAD_LEFT_EDGE
252
253     src[0+0*stride]=(l0 + l1 + 1)>>1;
254     src[1+0*stride]=(l0 + 2*l1 + l2 + 2)>>2;
255     src[2+0*stride]=
256     src[0+1*stride]=(l1 + l2 + 1)>>1;
257     src[3+0*stride]=
258     src[1+1*stride]=(l1 + 2*l2 + l3 + 2)>>2;
259     src[2+1*stride]=
260     src[0+2*stride]=(l2 + l3 + 1)>>1;
261     src[3+1*stride]=
262     src[1+2*stride]=(l2 + 2*l3 + l3 + 2)>>2;
263     src[3+2*stride]=
264     src[1+3*stride]=
265     src[0+3*stride]=
266     src[2+2*stride]=
267     src[2+3*stride]=
268     src[3+3*stride]=l3;
269 }
270
271 static void FUNCC(pred4x4_horizontal_down)(uint8_t *_src, const uint8_t *topright, int _stride){
272     pixel *src = (pixel*)_src;
273     int stride = _stride>>(sizeof(pixel)-1);
274     const int lt= src[-1-1*stride];
275     LOAD_TOP_EDGE
276     LOAD_LEFT_EDGE
277
278     src[0+0*stride]=
279     src[2+1*stride]=(lt + l0 + 1)>>1;
280     src[1+0*stride]=
281     src[3+1*stride]=(l0 + 2*lt + t0 + 2)>>2;
282     src[2+0*stride]=(lt + 2*t0 + t1 + 2)>>2;
283     src[3+0*stride]=(t0 + 2*t1 + t2 + 2)>>2;
284     src[0+1*stride]=
285     src[2+2*stride]=(l0 + l1 + 1)>>1;
286     src[1+1*stride]=
287     src[3+2*stride]=(lt + 2*l0 + l1 + 2)>>2;
288     src[0+2*stride]=
289     src[2+3*stride]=(l1 + l2+ 1)>>1;
290     src[1+2*stride]=
291     src[3+3*stride]=(l0 + 2*l1 + l2 + 2)>>2;
292     src[0+3*stride]=(l2 + l3 + 1)>>1;
293     src[1+3*stride]=(l1 + 2*l2 + l3 + 2)>>2;
294 }
295
296 static void FUNCC(pred16x16_vertical)(uint8_t *_src, int _stride){
297     int i;
298     pixel *src = (pixel*)_src;
299     int stride = _stride>>(sizeof(pixel)-1);
300     const pixel4 a = AV_RN4PA(((pixel4*)(src-stride))+0);
301     const pixel4 b = AV_RN4PA(((pixel4*)(src-stride))+1);
302     const pixel4 c = AV_RN4PA(((pixel4*)(src-stride))+2);
303     const pixel4 d = AV_RN4PA(((pixel4*)(src-stride))+3);
304
305     for(i=0; i<16; i++){
306         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
307         AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
308         AV_WN4PA(((pixel4*)(src+i*stride))+2, c);
309         AV_WN4PA(((pixel4*)(src+i*stride))+3, d);
310     }
311 }
312
313 static void FUNCC(pred16x16_horizontal)(uint8_t *_src, int stride){
314     int i;
315     pixel *src = (pixel*)_src;
316     stride >>= sizeof(pixel)-1;
317
318     for(i=0; i<16; i++){
319         const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);
320
321         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
322         AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
323         AV_WN4PA(((pixel4*)(src+i*stride))+2, a);
324         AV_WN4PA(((pixel4*)(src+i*stride))+3, a);
325     }
326 }
327
328 #define PREDICT_16x16_DC(v)\
329     for(i=0; i<16; i++){\
330         AV_WN4PA(src+ 0, v);\
331         AV_WN4PA(src+ 4, v);\
332         AV_WN4PA(src+ 8, v);\
333         AV_WN4PA(src+12, v);\
334         src += stride;\
335     }
336
337 static void FUNCC(pred16x16_dc)(uint8_t *_src, int stride){
338     int i, dc=0;
339     pixel *src = (pixel*)_src;
340     pixel4 dcsplat;
341     stride >>= sizeof(pixel)-1;
342
343     for(i=0;i<16; i++){
344         dc+= src[-1+i*stride];
345     }
346
347     for(i=0;i<16; i++){
348         dc+= src[i-stride];
349     }
350
351     dcsplat = PIXEL_SPLAT_X4((dc+16)>>5);
352     PREDICT_16x16_DC(dcsplat);
353 }
354
355 static void FUNCC(pred16x16_left_dc)(uint8_t *_src, int stride){
356     int i, dc=0;
357     pixel *src = (pixel*)_src;
358     pixel4 dcsplat;
359     stride >>= sizeof(pixel)-1;
360
361     for(i=0;i<16; i++){
362         dc+= src[-1+i*stride];
363     }
364
365     dcsplat = PIXEL_SPLAT_X4((dc+8)>>4);
366     PREDICT_16x16_DC(dcsplat);
367 }
368
369 static void FUNCC(pred16x16_top_dc)(uint8_t *_src, int stride){
370     int i, dc=0;
371     pixel *src = (pixel*)_src;
372     pixel4 dcsplat;
373     stride >>= sizeof(pixel)-1;
374
375     for(i=0;i<16; i++){
376         dc+= src[i-stride];
377     }
378
379     dcsplat = PIXEL_SPLAT_X4((dc+8)>>4);
380     PREDICT_16x16_DC(dcsplat);
381 }
382
383 #define PRED16x16_X(n, v) \
384 static void FUNCC(pred16x16_##n##_dc)(uint8_t *_src, int stride){\
385     int i;\
386     pixel *src = (pixel*)_src;\
387     stride >>= sizeof(pixel)-1;\
388     PREDICT_16x16_DC(PIXEL_SPLAT_X4(v));\
389 }
390
391 PRED16x16_X(127, (1<<(BIT_DEPTH-1))-1);
392 PRED16x16_X(128, (1<<(BIT_DEPTH-1))+0);
393 PRED16x16_X(129, (1<<(BIT_DEPTH-1))+1);
394
395 static inline void FUNCC(pred16x16_plane_compat)(uint8_t *p_src, int p_stride, const int svq3, const int rv40){
396   int i, j, k;
397   int a;
398   INIT_CLIP
399   pixel *src = (pixel*)p_src;
400   int stride = p_stride>>(sizeof(pixel)-1);
401   const pixel * const src0 = src +7-stride;
402   const pixel *       src1 = src +8*stride-1;
403   const pixel *       src2 = src1-2*stride;    // == src+6*stride-1;
404   int H = src0[1] - src0[-1];
405   int V = src1[0] - src2[ 0];
406   for(k=2; k<=8; ++k) {
407     src1 += stride; src2 -= stride;
408     H += k*(src0[k] - src0[-k]);
409     V += k*(src1[0] - src2[ 0]);
410   }
411   if(svq3){
412     H = ( 5*(H/4) ) / 16;
413     V = ( 5*(V/4) ) / 16;
414
415     /* required for 100% accuracy */
416     i = H; H = V; V = i;
417   }else if(rv40){
418     H = ( H + (H>>2) ) >> 4;
419     V = ( V + (V>>2) ) >> 4;
420   }else{
421     H = ( 5*H+32 ) >> 6;
422     V = ( 5*V+32 ) >> 6;
423   }
424
425   a = 16*(src1[0] + src2[16] + 1) - 7*(V+H);
426   for(j=16; j>0; --j) {
427     int b = a;
428     a += V;
429     for(i=-16; i<0; i+=4) {
430       src[16+i] = CLIP((b    ) >> 5);
431       src[17+i] = CLIP((b+  H) >> 5);
432       src[18+i] = CLIP((b+2*H) >> 5);
433       src[19+i] = CLIP((b+3*H) >> 5);
434       b += 4*H;
435     }
436     src += stride;
437   }
438 }
439
440 static void FUNCC(pred16x16_plane)(uint8_t *src, int stride){
441     FUNCC(pred16x16_plane_compat)(src, stride, 0, 0);
442 }
443
444 static void FUNCC(pred8x8_vertical)(uint8_t *_src, int _stride){
445     int i;
446     pixel *src = (pixel*)_src;
447     int stride = _stride>>(sizeof(pixel)-1);
448     const pixel4 a= AV_RN4PA(((pixel4*)(src-stride))+0);
449     const pixel4 b= AV_RN4PA(((pixel4*)(src-stride))+1);
450
451     for(i=0; i<8; i++){
452         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
453         AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
454     }
455 }
456
457 static void FUNCC(pred8x16_vertical)(uint8_t *_src, int _stride){
458     int i;
459     pixel *src = (pixel*)_src;
460     int stride = _stride>>(sizeof(pixel)-1);
461     const pixel4 a= AV_RN4PA(((pixel4*)(src-stride))+0);
462     const pixel4 b= AV_RN4PA(((pixel4*)(src-stride))+1);
463
464     for(i=0; i<16; i++){
465         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
466         AV_WN4PA(((pixel4*)(src+i*stride))+1, b);
467     }
468 }
469
470 static void FUNCC(pred8x8_horizontal)(uint8_t *_src, int stride){
471     int i;
472     pixel *src = (pixel*)_src;
473     stride >>= sizeof(pixel)-1;
474
475     for(i=0; i<8; i++){
476         const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);
477         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
478         AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
479     }
480 }
481
482 static void FUNCC(pred8x16_horizontal)(uint8_t *_src, int stride){
483     int i;
484     pixel *src = (pixel*)_src;
485     stride >>= sizeof(pixel)-1;
486     for(i=0; i<16; i++){
487         const pixel4 a = PIXEL_SPLAT_X4(src[-1+i*stride]);
488         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);
489         AV_WN4PA(((pixel4*)(src+i*stride))+1, a);
490     }
491 }
492
493 #define PRED8x8_X(n, v)\
494 static void FUNCC(pred8x8_##n##_dc)(uint8_t *_src, int stride){\
495     int i;\
496     const pixel4 a = PIXEL_SPLAT_X4(v);\
497     pixel *src = (pixel*)_src;\
498     stride >>= sizeof(pixel)-1;\
499     for(i=0; i<8; i++){\
500         AV_WN4PA(((pixel4*)(src+i*stride))+0, a);\
501         AV_WN4PA(((pixel4*)(src+i*stride))+1, a);\
502     }\
503 }
504
505 PRED8x8_X(127, (1<<(BIT_DEPTH-1))-1);
506 PRED8x8_X(128, (1<<(BIT_DEPTH-1))+0);
507 PRED8x8_X(129, (1<<(BIT_DEPTH-1))+1);
508
509 static void FUNCC(pred8x16_128_dc)(uint8_t *_src, int stride){
510     FUNCC(pred8x8_128_dc)(_src, stride);
511     FUNCC(pred8x8_128_dc)(_src+8*stride, stride);
512 }
513
514 static void FUNCC(pred8x8_left_dc)(uint8_t *_src, int stride){
515     int i;
516     int dc0, dc2;
517     pixel4 dc0splat, dc2splat;
518     pixel *src = (pixel*)_src;
519     stride >>= sizeof(pixel)-1;
520
521     dc0=dc2=0;
522     for(i=0;i<4; i++){
523         dc0+= src[-1+i*stride];
524         dc2+= src[-1+(i+4)*stride];
525     }
526     dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
527     dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
528
529     for(i=0; i<4; i++){
530         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
531         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc0splat);
532     }
533     for(i=4; i<8; i++){
534         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
535         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc2splat);
536     }
537 }
538
539 static void FUNCC(pred8x16_left_dc)(uint8_t *_src, int stride){
540     FUNCC(pred8x8_left_dc)(_src, stride);
541     FUNCC(pred8x8_left_dc)(_src+8*stride, stride);
542 }
543
544 static void FUNCC(pred8x8_top_dc)(uint8_t *_src, int stride){
545     int i;
546     int dc0, dc1;
547     pixel4 dc0splat, dc1splat;
548     pixel *src = (pixel*)_src;
549     stride >>= sizeof(pixel)-1;
550
551     dc0=dc1=0;
552     for(i=0;i<4; i++){
553         dc0+= src[i-stride];
554         dc1+= src[4+i-stride];
555     }
556     dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
557     dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
558
559     for(i=0; i<4; i++){
560         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
561         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
562     }
563     for(i=4; i<8; i++){
564         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
565         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
566     }
567 }
568
569 static void FUNCC(pred8x16_top_dc)(uint8_t *_src, int stride){
570     int i;
571     int dc0, dc1;
572     pixel4 dc0splat, dc1splat;
573     pixel *src = (pixel*)_src;
574     stride >>= sizeof(pixel)-1;
575
576     dc0=dc1=0;
577     for(i=0;i<4; i++){
578         dc0+= src[i-stride];
579         dc1+= src[4+i-stride];
580     }
581     dc0splat = PIXEL_SPLAT_X4((dc0 + 2)>>2);
582     dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
583
584     for(i=0; i<16; i++){
585         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
586         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
587     }
588 }
589
590 static void FUNCC(pred8x8_dc)(uint8_t *_src, int stride){
591     int i;
592     int dc0, dc1, dc2;
593     pixel4 dc0splat, dc1splat, dc2splat, dc3splat;
594     pixel *src = (pixel*)_src;
595     stride >>= sizeof(pixel)-1;
596
597     dc0=dc1=dc2=0;
598     for(i=0;i<4; i++){
599         dc0+= src[-1+i*stride] + src[i-stride];
600         dc1+= src[4+i-stride];
601         dc2+= src[-1+(i+4)*stride];
602     }
603     dc0splat = PIXEL_SPLAT_X4((dc0 + 4)>>3);
604     dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
605     dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
606     dc3splat = PIXEL_SPLAT_X4((dc1 + dc2 + 4)>>3);
607
608     for(i=0; i<4; i++){
609         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
610         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
611     }
612     for(i=4; i<8; i++){
613         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
614         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc3splat);
615     }
616 }
617
618 static void FUNCC(pred8x16_dc)(uint8_t *_src, int stride){
619     int i;
620     int dc0, dc1, dc2, dc3, dc4;
621     pixel4 dc0splat, dc1splat, dc2splat, dc3splat, dc4splat, dc5splat, dc6splat, dc7splat;
622     pixel *src = (pixel*)_src;
623     stride >>= sizeof(pixel)-1;
624
625     dc0=dc1=dc2=dc3=dc4=0;
626     for(i=0;i<4; i++){
627         dc0+= src[-1+i*stride] + src[i-stride];
628         dc1+= src[4+i-stride];
629         dc2+= src[-1+(i+4)*stride];
630         dc3+= src[-1+(i+8)*stride];
631         dc4+= src[-1+(i+12)*stride];
632     }
633     dc0splat = PIXEL_SPLAT_X4((dc0 + 4)>>3);
634     dc1splat = PIXEL_SPLAT_X4((dc1 + 2)>>2);
635     dc2splat = PIXEL_SPLAT_X4((dc2 + 2)>>2);
636     dc3splat = PIXEL_SPLAT_X4((dc1 + dc2 + 4)>>3);
637     dc4splat = PIXEL_SPLAT_X4((dc3 + 2)>>2);
638     dc5splat = PIXEL_SPLAT_X4((dc1 + dc3 + 4)>>3);
639     dc6splat = PIXEL_SPLAT_X4((dc4 + 2)>>2);
640     dc7splat = PIXEL_SPLAT_X4((dc1 + dc4 + 4)>>3);
641
642     for(i=0; i<4; i++){
643         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc0splat);
644         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc1splat);
645     }
646     for(i=4; i<8; i++){
647         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc2splat);
648         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc3splat);
649     }
650     for(i=8; i<12; i++){
651         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc4splat);
652         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc5splat);
653     }
654     for(i=12; i<16; i++){
655         AV_WN4PA(((pixel4*)(src+i*stride))+0, dc6splat);
656         AV_WN4PA(((pixel4*)(src+i*stride))+1, dc7splat);
657     }
658 }
659
660 //the following 4 function should not be optimized!
661 static void FUNC(pred8x8_mad_cow_dc_l0t)(uint8_t *src, int stride){
662     FUNCC(pred8x8_top_dc)(src, stride);
663     FUNCC(pred4x4_dc)(src, NULL, stride);
664 }
665
666 static void FUNC(pred8x8_mad_cow_dc_0lt)(uint8_t *src, int stride){
667     FUNCC(pred8x8_dc)(src, stride);
668     FUNCC(pred4x4_top_dc)(src, NULL, stride);
669 }
670
671 static void FUNC(pred8x8_mad_cow_dc_l00)(uint8_t *src, int stride){
672     FUNCC(pred8x8_left_dc)(src, stride);
673     FUNCC(pred4x4_128_dc)(src + 4*stride                  , NULL, stride);
674     FUNCC(pred4x4_128_dc)(src + 4*stride + 4*sizeof(pixel), NULL, stride);
675 }
676
677 static void FUNC(pred8x8_mad_cow_dc_0l0)(uint8_t *src, int stride){
678     FUNCC(pred8x8_left_dc)(src, stride);
679     FUNCC(pred4x4_128_dc)(src                  , NULL, stride);
680     FUNCC(pred4x4_128_dc)(src + 4*sizeof(pixel), NULL, stride);
681 }
682
683 static void FUNCC(pred8x8_plane)(uint8_t *_src, int _stride){
684   int j, k;
685   int a;
686   INIT_CLIP
687   pixel *src = (pixel*)_src;
688   int stride = _stride>>(sizeof(pixel)-1);
689   const pixel * const src0 = src +3-stride;
690   const pixel *       src1 = src +4*stride-1;
691   const pixel *       src2 = src1-2*stride;    // == src+2*stride-1;
692   int H = src0[1] - src0[-1];
693   int V = src1[0] - src2[ 0];
694   for(k=2; k<=4; ++k) {
695     src1 += stride; src2 -= stride;
696     H += k*(src0[k] - src0[-k]);
697     V += k*(src1[0] - src2[ 0]);
698   }
699   H = ( 17*H+16 ) >> 5;
700   V = ( 17*V+16 ) >> 5;
701
702   a = 16*(src1[0] + src2[8]+1) - 3*(V+H);
703   for(j=8; j>0; --j) {
704     int b = a;
705     a += V;
706     src[0] = CLIP((b    ) >> 5);
707     src[1] = CLIP((b+  H) >> 5);
708     src[2] = CLIP((b+2*H) >> 5);
709     src[3] = CLIP((b+3*H) >> 5);
710     src[4] = CLIP((b+4*H) >> 5);
711     src[5] = CLIP((b+5*H) >> 5);
712     src[6] = CLIP((b+6*H) >> 5);
713     src[7] = CLIP((b+7*H) >> 5);
714     src += stride;
715   }
716 }
717
718 static void FUNCC(pred8x16_plane)(uint8_t *_src, int _stride){
719   int j, k;
720   int a;
721   INIT_CLIP
722   pixel *src = (pixel*)_src;
723   int stride = _stride>>(sizeof(pixel)-1);
724   const pixel * const src0 = src +3-stride;
725   const pixel *       src1 = src +8*stride-1;
726   const pixel *       src2 = src1-2*stride;    // == src+6*stride-1;
727   int H = src0[1] - src0[-1];
728   int V = src1[0] - src2[ 0];
729
730   for (k = 2; k <= 4; ++k) {
731       src1 += stride; src2 -= stride;
732       H += k*(src0[k] - src0[-k]);
733       V += k*(src1[0] - src2[ 0]);
734   }
735   for (; k <= 8; ++k) {
736       src1 += stride; src2 -= stride;
737       V += k*(src1[0] - src2[0]);
738   }
739
740   H = (17*H+16) >> 5;
741   V = (5*V+32) >> 6;
742
743   a = 16*(src1[0] + src2[8] + 1) - 7*V - 3*H;
744   for(j=16; j>0; --j) {
745     int b = a;
746     a += V;
747     src[0] = CLIP((b    ) >> 5);
748     src[1] = CLIP((b+  H) >> 5);
749     src[2] = CLIP((b+2*H) >> 5);
750     src[3] = CLIP((b+3*H) >> 5);
751     src[4] = CLIP((b+4*H) >> 5);
752     src[5] = CLIP((b+5*H) >> 5);
753     src[6] = CLIP((b+6*H) >> 5);
754     src[7] = CLIP((b+7*H) >> 5);
755     src += stride;
756   }
757 }
758
759 #define SRC(x,y) src[(x)+(y)*stride]
760 #define PL(y) \
761     const int l##y = (SRC(-1,y-1) + 2*SRC(-1,y) + SRC(-1,y+1) + 2) >> 2;
762 #define PREDICT_8x8_LOAD_LEFT \
763     const int l0 = ((has_topleft ? SRC(-1,-1) : SRC(-1,0)) \
764                      + 2*SRC(-1,0) + SRC(-1,1) + 2) >> 2; \
765     PL(1) PL(2) PL(3) PL(4) PL(5) PL(6) \
766     const int l7 av_unused = (SRC(-1,6) + 3*SRC(-1,7) + 2) >> 2
767
768 #define PT(x) \
769     const int t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
770 #define PREDICT_8x8_LOAD_TOP \
771     const int t0 = ((has_topleft ? SRC(-1,-1) : SRC(0,-1)) \
772                      + 2*SRC(0,-1) + SRC(1,-1) + 2) >> 2; \
773     PT(1) PT(2) PT(3) PT(4) PT(5) PT(6) \
774     const int t7 av_unused = ((has_topright ? SRC(8,-1) : SRC(7,-1)) \
775                      + 2*SRC(7,-1) + SRC(6,-1) + 2) >> 2
776
777 #define PTR(x) \
778     t##x = (SRC(x-1,-1) + 2*SRC(x,-1) + SRC(x+1,-1) + 2) >> 2;
779 #define PREDICT_8x8_LOAD_TOPRIGHT \
780     int t8, t9, t10, t11, t12, t13, t14, t15; \
781     if(has_topright) { \
782         PTR(8) PTR(9) PTR(10) PTR(11) PTR(12) PTR(13) PTR(14) \
783         t15 = (SRC(14,-1) + 3*SRC(15,-1) + 2) >> 2; \
784     } else t8=t9=t10=t11=t12=t13=t14=t15= SRC(7,-1);
785
786 #define PREDICT_8x8_LOAD_TOPLEFT \
787     const int lt = (SRC(-1,0) + 2*SRC(-1,-1) + SRC(0,-1) + 2) >> 2
788
789 #define PREDICT_8x8_DC(v) \
790     int y; \
791     for( y = 0; y < 8; y++ ) { \
792         AV_WN4PA(((pixel4*)src)+0, v); \
793         AV_WN4PA(((pixel4*)src)+1, v); \
794         src += stride; \
795     }
796
797 static void FUNCC(pred8x8l_128_dc)(uint8_t *_src, int has_topleft, int has_topright, int _stride)
798 {
799     pixel *src = (pixel*)_src;
800     int stride = _stride>>(sizeof(pixel)-1);
801
802     PREDICT_8x8_DC(PIXEL_SPLAT_X4(1<<(BIT_DEPTH-1)));
803 }
804 static void FUNCC(pred8x8l_left_dc)(uint8_t *_src, int has_topleft, int has_topright, int _stride)
805 {
806     pixel *src = (pixel*)_src;
807     int stride = _stride>>(sizeof(pixel)-1);
808
809     PREDICT_8x8_LOAD_LEFT;
810     const pixel4 dc = PIXEL_SPLAT_X4((l0+l1+l2+l3+l4+l5+l6+l7+4) >> 3);
811     PREDICT_8x8_DC(dc);
812 }
813 static void FUNCC(pred8x8l_top_dc)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
814 {
815     pixel *src = (pixel*)p_src;
816     int stride = p_stride>>(sizeof(pixel)-1);
817
818     PREDICT_8x8_LOAD_TOP;
819     const pixel4 dc = PIXEL_SPLAT_X4((t0+t1+t2+t3+t4+t5+t6+t7+4) >> 3);
820     PREDICT_8x8_DC(dc);
821 }
822 static void FUNCC(pred8x8l_dc)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
823 {
824     pixel *src = (pixel*)p_src;
825     int stride = p_stride>>(sizeof(pixel)-1);
826
827     PREDICT_8x8_LOAD_LEFT;
828     PREDICT_8x8_LOAD_TOP;
829     const pixel4 dc = PIXEL_SPLAT_X4((l0+l1+l2+l3+l4+l5+l6+l7
830                                      +t0+t1+t2+t3+t4+t5+t6+t7+8) >> 4);
831     PREDICT_8x8_DC(dc);
832 }
833 static void FUNCC(pred8x8l_horizontal)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
834 {
835     pixel *src = (pixel*)p_src;
836     int stride = p_stride>>(sizeof(pixel)-1);
837     pixel4 a;
838
839     PREDICT_8x8_LOAD_LEFT;
840 #define ROW(y) a = PIXEL_SPLAT_X4(l##y); \
841                AV_WN4PA(src+y*stride, a); \
842                AV_WN4PA(src+y*stride+4, a);
843     ROW(0); ROW(1); ROW(2); ROW(3); ROW(4); ROW(5); ROW(6); ROW(7);
844 #undef ROW
845 }
846 static void FUNCC(pred8x8l_vertical)(uint8_t *_src, int has_topleft, int has_topright, int _stride)
847 {
848     int y;
849     pixel *src = (pixel*)_src;
850     int stride = _stride>>(sizeof(pixel)-1);
851     pixel4 a, b;
852
853     PREDICT_8x8_LOAD_TOP;
854     src[0] = t0;
855     src[1] = t1;
856     src[2] = t2;
857     src[3] = t3;
858     src[4] = t4;
859     src[5] = t5;
860     src[6] = t6;
861     src[7] = t7;
862     a = AV_RN4PA(((pixel4*)src)+0);
863     b = AV_RN4PA(((pixel4*)src)+1);
864     for( y = 1; y < 8; y++ ) {
865         AV_WN4PA(((pixel4*)(src+y*stride))+0, a);
866         AV_WN4PA(((pixel4*)(src+y*stride))+1, b);
867     }
868 }
869 static void FUNCC(pred8x8l_down_left)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
870 {
871     pixel *src = (pixel*)p_src;
872     int stride = p_stride>>(sizeof(pixel)-1);
873     PREDICT_8x8_LOAD_TOP;
874     PREDICT_8x8_LOAD_TOPRIGHT;
875     SRC(0,0)= (t0 + 2*t1 + t2 + 2) >> 2;
876     SRC(0,1)=SRC(1,0)= (t1 + 2*t2 + t3 + 2) >> 2;
877     SRC(0,2)=SRC(1,1)=SRC(2,0)= (t2 + 2*t3 + t4 + 2) >> 2;
878     SRC(0,3)=SRC(1,2)=SRC(2,1)=SRC(3,0)= (t3 + 2*t4 + t5 + 2) >> 2;
879     SRC(0,4)=SRC(1,3)=SRC(2,2)=SRC(3,1)=SRC(4,0)= (t4 + 2*t5 + t6 + 2) >> 2;
880     SRC(0,5)=SRC(1,4)=SRC(2,3)=SRC(3,2)=SRC(4,1)=SRC(5,0)= (t5 + 2*t6 + t7 + 2) >> 2;
881     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;
882     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;
883     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;
884     SRC(2,7)=SRC(3,6)=SRC(4,5)=SRC(5,4)=SRC(6,3)=SRC(7,2)= (t9 + 2*t10 + t11 + 2) >> 2;
885     SRC(3,7)=SRC(4,6)=SRC(5,5)=SRC(6,4)=SRC(7,3)= (t10 + 2*t11 + t12 + 2) >> 2;
886     SRC(4,7)=SRC(5,6)=SRC(6,5)=SRC(7,4)= (t11 + 2*t12 + t13 + 2) >> 2;
887     SRC(5,7)=SRC(6,6)=SRC(7,5)= (t12 + 2*t13 + t14 + 2) >> 2;
888     SRC(6,7)=SRC(7,6)= (t13 + 2*t14 + t15 + 2) >> 2;
889     SRC(7,7)= (t14 + 3*t15 + 2) >> 2;
890 }
891 static void FUNCC(pred8x8l_down_right)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
892 {
893     pixel *src = (pixel*)p_src;
894     int stride = p_stride>>(sizeof(pixel)-1);
895     PREDICT_8x8_LOAD_TOP;
896     PREDICT_8x8_LOAD_LEFT;
897     PREDICT_8x8_LOAD_TOPLEFT;
898     SRC(0,7)= (l7 + 2*l6 + l5 + 2) >> 2;
899     SRC(0,6)=SRC(1,7)= (l6 + 2*l5 + l4 + 2) >> 2;
900     SRC(0,5)=SRC(1,6)=SRC(2,7)= (l5 + 2*l4 + l3 + 2) >> 2;
901     SRC(0,4)=SRC(1,5)=SRC(2,6)=SRC(3,7)= (l4 + 2*l3 + l2 + 2) >> 2;
902     SRC(0,3)=SRC(1,4)=SRC(2,5)=SRC(3,6)=SRC(4,7)= (l3 + 2*l2 + l1 + 2) >> 2;
903     SRC(0,2)=SRC(1,3)=SRC(2,4)=SRC(3,5)=SRC(4,6)=SRC(5,7)= (l2 + 2*l1 + l0 + 2) >> 2;
904     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;
905     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;
906     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;
907     SRC(2,0)=SRC(3,1)=SRC(4,2)=SRC(5,3)=SRC(6,4)=SRC(7,5)= (t0 + 2*t1 + t2 + 2) >> 2;
908     SRC(3,0)=SRC(4,1)=SRC(5,2)=SRC(6,3)=SRC(7,4)= (t1 + 2*t2 + t3 + 2) >> 2;
909     SRC(4,0)=SRC(5,1)=SRC(6,2)=SRC(7,3)= (t2 + 2*t3 + t4 + 2) >> 2;
910     SRC(5,0)=SRC(6,1)=SRC(7,2)= (t3 + 2*t4 + t5 + 2) >> 2;
911     SRC(6,0)=SRC(7,1)= (t4 + 2*t5 + t6 + 2) >> 2;
912     SRC(7,0)= (t5 + 2*t6 + t7 + 2) >> 2;
913 }
914 static void FUNCC(pred8x8l_vertical_right)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
915 {
916     pixel *src = (pixel*)p_src;
917     int stride = p_stride>>(sizeof(pixel)-1);
918     PREDICT_8x8_LOAD_TOP;
919     PREDICT_8x8_LOAD_LEFT;
920     PREDICT_8x8_LOAD_TOPLEFT;
921     SRC(0,6)= (l5 + 2*l4 + l3 + 2) >> 2;
922     SRC(0,7)= (l6 + 2*l5 + l4 + 2) >> 2;
923     SRC(0,4)=SRC(1,6)= (l3 + 2*l2 + l1 + 2) >> 2;
924     SRC(0,5)=SRC(1,7)= (l4 + 2*l3 + l2 + 2) >> 2;
925     SRC(0,2)=SRC(1,4)=SRC(2,6)= (l1 + 2*l0 + lt + 2) >> 2;
926     SRC(0,3)=SRC(1,5)=SRC(2,7)= (l2 + 2*l1 + l0 + 2) >> 2;
927     SRC(0,1)=SRC(1,3)=SRC(2,5)=SRC(3,7)= (l0 + 2*lt + t0 + 2) >> 2;
928     SRC(0,0)=SRC(1,2)=SRC(2,4)=SRC(3,6)= (lt + t0 + 1) >> 1;
929     SRC(1,1)=SRC(2,3)=SRC(3,5)=SRC(4,7)= (lt + 2*t0 + t1 + 2) >> 2;
930     SRC(1,0)=SRC(2,2)=SRC(3,4)=SRC(4,6)= (t0 + t1 + 1) >> 1;
931     SRC(2,1)=SRC(3,3)=SRC(4,5)=SRC(5,7)= (t0 + 2*t1 + t2 + 2) >> 2;
932     SRC(2,0)=SRC(3,2)=SRC(4,4)=SRC(5,6)= (t1 + t2 + 1) >> 1;
933     SRC(3,1)=SRC(4,3)=SRC(5,5)=SRC(6,7)= (t1 + 2*t2 + t3 + 2) >> 2;
934     SRC(3,0)=SRC(4,2)=SRC(5,4)=SRC(6,6)= (t2 + t3 + 1) >> 1;
935     SRC(4,1)=SRC(5,3)=SRC(6,5)=SRC(7,7)= (t2 + 2*t3 + t4 + 2) >> 2;
936     SRC(4,0)=SRC(5,2)=SRC(6,4)=SRC(7,6)= (t3 + t4 + 1) >> 1;
937     SRC(5,1)=SRC(6,3)=SRC(7,5)= (t3 + 2*t4 + t5 + 2) >> 2;
938     SRC(5,0)=SRC(6,2)=SRC(7,4)= (t4 + t5 + 1) >> 1;
939     SRC(6,1)=SRC(7,3)= (t4 + 2*t5 + t6 + 2) >> 2;
940     SRC(6,0)=SRC(7,2)= (t5 + t6 + 1) >> 1;
941     SRC(7,1)= (t5 + 2*t6 + t7 + 2) >> 2;
942     SRC(7,0)= (t6 + t7 + 1) >> 1;
943 }
944 static void FUNCC(pred8x8l_horizontal_down)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
945 {
946     pixel *src = (pixel*)p_src;
947     int stride = p_stride>>(sizeof(pixel)-1);
948     PREDICT_8x8_LOAD_TOP;
949     PREDICT_8x8_LOAD_LEFT;
950     PREDICT_8x8_LOAD_TOPLEFT;
951     SRC(0,7)= (l6 + l7 + 1) >> 1;
952     SRC(1,7)= (l5 + 2*l6 + l7 + 2) >> 2;
953     SRC(0,6)=SRC(2,7)= (l5 + l6 + 1) >> 1;
954     SRC(1,6)=SRC(3,7)= (l4 + 2*l5 + l6 + 2) >> 2;
955     SRC(0,5)=SRC(2,6)=SRC(4,7)= (l4 + l5 + 1) >> 1;
956     SRC(1,5)=SRC(3,6)=SRC(5,7)= (l3 + 2*l4 + l5 + 2) >> 2;
957     SRC(0,4)=SRC(2,5)=SRC(4,6)=SRC(6,7)= (l3 + l4 + 1) >> 1;
958     SRC(1,4)=SRC(3,5)=SRC(5,6)=SRC(7,7)= (l2 + 2*l3 + l4 + 2) >> 2;
959     SRC(0,3)=SRC(2,4)=SRC(4,5)=SRC(6,6)= (l2 + l3 + 1) >> 1;
960     SRC(1,3)=SRC(3,4)=SRC(5,5)=SRC(7,6)= (l1 + 2*l2 + l3 + 2) >> 2;
961     SRC(0,2)=SRC(2,3)=SRC(4,4)=SRC(6,5)= (l1 + l2 + 1) >> 1;
962     SRC(1,2)=SRC(3,3)=SRC(5,4)=SRC(7,5)= (l0 + 2*l1 + l2 + 2) >> 2;
963     SRC(0,1)=SRC(2,2)=SRC(4,3)=SRC(6,4)= (l0 + l1 + 1) >> 1;
964     SRC(1,1)=SRC(3,2)=SRC(5,3)=SRC(7,4)= (lt + 2*l0 + l1 + 2) >> 2;
965     SRC(0,0)=SRC(2,1)=SRC(4,2)=SRC(6,3)= (lt + l0 + 1) >> 1;
966     SRC(1,0)=SRC(3,1)=SRC(5,2)=SRC(7,3)= (l0 + 2*lt + t0 + 2) >> 2;
967     SRC(2,0)=SRC(4,1)=SRC(6,2)= (t1 + 2*t0 + lt + 2) >> 2;
968     SRC(3,0)=SRC(5,1)=SRC(7,2)= (t2 + 2*t1 + t0 + 2) >> 2;
969     SRC(4,0)=SRC(6,1)= (t3 + 2*t2 + t1 + 2) >> 2;
970     SRC(5,0)=SRC(7,1)= (t4 + 2*t3 + t2 + 2) >> 2;
971     SRC(6,0)= (t5 + 2*t4 + t3 + 2) >> 2;
972     SRC(7,0)= (t6 + 2*t5 + t4 + 2) >> 2;
973 }
974 static void FUNCC(pred8x8l_vertical_left)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
975 {
976     pixel *src = (pixel*)p_src;
977     int stride = p_stride>>(sizeof(pixel)-1);
978     PREDICT_8x8_LOAD_TOP;
979     PREDICT_8x8_LOAD_TOPRIGHT;
980     SRC(0,0)= (t0 + t1 + 1) >> 1;
981     SRC(0,1)= (t0 + 2*t1 + t2 + 2) >> 2;
982     SRC(0,2)=SRC(1,0)= (t1 + t2 + 1) >> 1;
983     SRC(0,3)=SRC(1,1)= (t1 + 2*t2 + t3 + 2) >> 2;
984     SRC(0,4)=SRC(1,2)=SRC(2,0)= (t2 + t3 + 1) >> 1;
985     SRC(0,5)=SRC(1,3)=SRC(2,1)= (t2 + 2*t3 + t4 + 2) >> 2;
986     SRC(0,6)=SRC(1,4)=SRC(2,2)=SRC(3,0)= (t3 + t4 + 1) >> 1;
987     SRC(0,7)=SRC(1,5)=SRC(2,3)=SRC(3,1)= (t3 + 2*t4 + t5 + 2) >> 2;
988     SRC(1,6)=SRC(2,4)=SRC(3,2)=SRC(4,0)= (t4 + t5 + 1) >> 1;
989     SRC(1,7)=SRC(2,5)=SRC(3,3)=SRC(4,1)= (t4 + 2*t5 + t6 + 2) >> 2;
990     SRC(2,6)=SRC(3,4)=SRC(4,2)=SRC(5,0)= (t5 + t6 + 1) >> 1;
991     SRC(2,7)=SRC(3,5)=SRC(4,3)=SRC(5,1)= (t5 + 2*t6 + t7 + 2) >> 2;
992     SRC(3,6)=SRC(4,4)=SRC(5,2)=SRC(6,0)= (t6 + t7 + 1) >> 1;
993     SRC(3,7)=SRC(4,5)=SRC(5,3)=SRC(6,1)= (t6 + 2*t7 + t8 + 2) >> 2;
994     SRC(4,6)=SRC(5,4)=SRC(6,2)=SRC(7,0)= (t7 + t8 + 1) >> 1;
995     SRC(4,7)=SRC(5,5)=SRC(6,3)=SRC(7,1)= (t7 + 2*t8 + t9 + 2) >> 2;
996     SRC(5,6)=SRC(6,4)=SRC(7,2)= (t8 + t9 + 1) >> 1;
997     SRC(5,7)=SRC(6,5)=SRC(7,3)= (t8 + 2*t9 + t10 + 2) >> 2;
998     SRC(6,6)=SRC(7,4)= (t9 + t10 + 1) >> 1;
999     SRC(6,7)=SRC(7,5)= (t9 + 2*t10 + t11 + 2) >> 2;
1000     SRC(7,6)= (t10 + t11 + 1) >> 1;
1001     SRC(7,7)= (t10 + 2*t11 + t12 + 2) >> 2;
1002 }
1003 static void FUNCC(pred8x8l_horizontal_up)(uint8_t *p_src, int has_topleft, int has_topright, int p_stride)
1004 {
1005     pixel *src = (pixel*)p_src;
1006     int stride = p_stride>>(sizeof(pixel)-1);
1007     PREDICT_8x8_LOAD_LEFT;
1008     SRC(0,0)= (l0 + l1 + 1) >> 1;
1009     SRC(1,0)= (l0 + 2*l1 + l2 + 2) >> 2;
1010     SRC(0,1)=SRC(2,0)= (l1 + l2 + 1) >> 1;
1011     SRC(1,1)=SRC(3,0)= (l1 + 2*l2 + l3 + 2) >> 2;
1012     SRC(0,2)=SRC(2,1)=SRC(4,0)= (l2 + l3 + 1) >> 1;
1013     SRC(1,2)=SRC(3,1)=SRC(5,0)= (l2 + 2*l3 + l4 + 2) >> 2;
1014     SRC(0,3)=SRC(2,2)=SRC(4,1)=SRC(6,0)= (l3 + l4 + 1) >> 1;
1015     SRC(1,3)=SRC(3,2)=SRC(5,1)=SRC(7,0)= (l3 + 2*l4 + l5 + 2) >> 2;
1016     SRC(0,4)=SRC(2,3)=SRC(4,2)=SRC(6,1)= (l4 + l5 + 1) >> 1;
1017     SRC(1,4)=SRC(3,3)=SRC(5,2)=SRC(7,1)= (l4 + 2*l5 + l6 + 2) >> 2;
1018     SRC(0,5)=SRC(2,4)=SRC(4,3)=SRC(6,2)= (l5 + l6 + 1) >> 1;
1019     SRC(1,5)=SRC(3,4)=SRC(5,3)=SRC(7,2)= (l5 + 2*l6 + l7 + 2) >> 2;
1020     SRC(0,6)=SRC(2,5)=SRC(4,4)=SRC(6,3)= (l6 + l7 + 1) >> 1;
1021     SRC(1,6)=SRC(3,5)=SRC(5,4)=SRC(7,3)= (l6 + 3*l7 + 2) >> 2;
1022     SRC(0,7)=SRC(1,7)=SRC(2,6)=SRC(2,7)=SRC(3,6)=
1023     SRC(3,7)=SRC(4,5)=SRC(4,6)=SRC(4,7)=SRC(5,5)=
1024     SRC(5,6)=SRC(5,7)=SRC(6,4)=SRC(6,5)=SRC(6,6)=
1025     SRC(6,7)=SRC(7,4)=SRC(7,5)=SRC(7,6)=SRC(7,7)= l7;
1026 }
1027 #undef PREDICT_8x8_LOAD_LEFT
1028 #undef PREDICT_8x8_LOAD_TOP
1029 #undef PREDICT_8x8_LOAD_TOPLEFT
1030 #undef PREDICT_8x8_LOAD_TOPRIGHT
1031 #undef PREDICT_8x8_DC
1032 #undef PTR
1033 #undef PT
1034 #undef PL
1035 #undef SRC
1036
1037 static void FUNCC(pred4x4_vertical_add)(uint8_t *p_pix, const DCTELEM *p_block, int stride){
1038     int i;
1039     pixel *pix = (pixel*)p_pix;
1040     const dctcoef *block = (const dctcoef*)p_block;
1041     stride >>= sizeof(pixel)-1;
1042     pix -= stride;
1043     for(i=0; i<4; i++){
1044         pixel v = pix[0];
1045         pix[1*stride]= v += block[0];
1046         pix[2*stride]= v += block[4];
1047         pix[3*stride]= v += block[8];
1048         pix[4*stride]= v +  block[12];
1049         pix++;
1050         block++;
1051     }
1052 }
1053
1054 static void FUNCC(pred4x4_horizontal_add)(uint8_t *p_pix, const DCTELEM *p_block, int stride){
1055     int i;
1056     pixel *pix = (pixel*)p_pix;
1057     const dctcoef *block = (const dctcoef*)p_block;
1058     stride >>= sizeof(pixel)-1;
1059     for(i=0; i<4; i++){
1060         pixel v = pix[-1];
1061         pix[0]= v += block[0];
1062         pix[1]= v += block[1];
1063         pix[2]= v += block[2];
1064         pix[3]= v +  block[3];
1065         pix+= stride;
1066         block+= 4;
1067     }
1068 }
1069
1070 static void FUNCC(pred8x8l_vertical_add)(uint8_t *p_pix, const DCTELEM *p_block, int stride){
1071     int i;
1072     pixel *pix = (pixel*)p_pix;
1073     const dctcoef *block = (const dctcoef*)p_block;
1074     stride >>= sizeof(pixel)-1;
1075     pix -= stride;
1076     for(i=0; i<8; i++){
1077         pixel v = pix[0];
1078         pix[1*stride]= v += block[0];
1079         pix[2*stride]= v += block[8];
1080         pix[3*stride]= v += block[16];
1081         pix[4*stride]= v += block[24];
1082         pix[5*stride]= v += block[32];
1083         pix[6*stride]= v += block[40];
1084         pix[7*stride]= v += block[48];
1085         pix[8*stride]= v +  block[56];
1086         pix++;
1087         block++;
1088     }
1089 }
1090
1091 static void FUNCC(pred8x8l_horizontal_add)(uint8_t *p_pix, const DCTELEM *p_block, int stride){
1092     int i;
1093     pixel *pix = (pixel*)p_pix;
1094     const dctcoef *block = (const dctcoef*)p_block;
1095     stride >>= sizeof(pixel)-1;
1096     for(i=0; i<8; i++){
1097         pixel v = pix[-1];
1098         pix[0]= v += block[0];
1099         pix[1]= v += block[1];
1100         pix[2]= v += block[2];
1101         pix[3]= v += block[3];
1102         pix[4]= v += block[4];
1103         pix[5]= v += block[5];
1104         pix[6]= v += block[6];
1105         pix[7]= v +  block[7];
1106         pix+= stride;
1107         block+= 8;
1108     }
1109 }
1110
1111 static void FUNCC(pred16x16_vertical_add)(uint8_t *pix, const int *block_offset, const DCTELEM *block, int stride){
1112     int i;
1113     for(i=0; i<16; i++)
1114         FUNCC(pred4x4_vertical_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1115 }
1116
1117 static void FUNCC(pred16x16_horizontal_add)(uint8_t *pix, const int *block_offset, const DCTELEM *block, int stride){
1118     int i;
1119     for(i=0; i<16; i++)
1120         FUNCC(pred4x4_horizontal_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1121 }
1122
1123 static void FUNCC(pred8x8_vertical_add)(uint8_t *pix, const int *block_offset, const DCTELEM *block, int stride){
1124     int i;
1125     for(i=0; i<4; i++)
1126         FUNCC(pred4x4_vertical_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1127 }
1128
1129 static void FUNCC(pred8x8_horizontal_add)(uint8_t *pix, const int *block_offset, const DCTELEM *block, int stride){
1130     int i;
1131     for(i=0; i<4; i++)
1132         FUNCC(pred4x4_horizontal_add)(pix + block_offset[i], block + i*16*sizeof(pixel), stride);
1133 }