1 /*****************************************************************************
2  * mc.c: motion compensation
3  *****************************************************************************
4  * Copyright (C) 2003-2014 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *
23  * This program is also available under a commercial proprietary license.
24  * For more information, contact us at licensing@x264.com.
25  *****************************************************************************/
26 
27 #include "common.h"
28 
29 #if HAVE_MMX
30 #include "x86/mc.h"
31 #endif
32 #if ARCH_PPC
33 #include "ppc/mc.h"
34 #endif
35 #if ARCH_ARM
36 #include "arm/mc.h"
37 #endif
38 #if ARCH_AARCH64
39 #include "aarch64/mc.h"
40 #endif
41 
42 
pixel_avg(pixel * dst,intptr_t i_dst_stride,pixel * src1,intptr_t i_src1_stride,pixel * src2,intptr_t i_src2_stride,int i_width,int i_height)43 static inline void pixel_avg( pixel *dst,  intptr_t i_dst_stride,
44                               pixel *src1, intptr_t i_src1_stride,
45                               pixel *src2, intptr_t i_src2_stride, int i_width, int i_height )
46 {
47 	int y;
48 	int x;
49 
50 	for( y = 0; y < i_height; y++ )
51     {
52         for( x = 0; x < i_width; x++ )
53             dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;
54         dst  += i_dst_stride;
55         src1 += i_src1_stride;
56         src2 += i_src2_stride;
57     }
58 }
59 
pixel_avg_wxh(pixel * dst,intptr_t i_dst,pixel * src1,intptr_t i_src1,pixel * src2,intptr_t i_src2,int width,int height)60 static inline void pixel_avg_wxh( pixel *dst,  intptr_t i_dst,
61                                   pixel *src1, intptr_t i_src1,
62                                   pixel *src2, intptr_t i_src2, int width, int height )
63 {
64 	int y;
65 	int x;
66 
67 	for( y = 0; y < height; y++ )
68     {
69         for( x = 0; x < width; x++ )
70             dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;
71         src1 += i_src1;
72         src2 += i_src2;
73         dst += i_dst;
74     }
75 }
76 
77 /* Implicit weighted bipred only:
78  * assumes log2_denom = 5, offset = 0, weight1 + weight2 = 64 */
pixel_avg_weight_wxh(pixel * dst,intptr_t i_dst,pixel * src1,intptr_t i_src1,pixel * src2,intptr_t i_src2,int width,int height,int i_weight1)79 static inline void pixel_avg_weight_wxh( pixel *dst,  intptr_t i_dst,
80                                          pixel *src1, intptr_t i_src1,
81                                          pixel *src2, intptr_t i_src2, int width, int height, int i_weight1 )
82 {
83     int i_weight2 = 64 - i_weight1;
84 	int y;
85 	int x;
86 
87 	for( y = 0; y<height; y++, dst += i_dst, src1 += i_src1, src2 += i_src2 )
88         for( x = 0; x<width; x++ )
89             dst[x] = x264_clip_pixel( (src1[x]*i_weight1 + src2[x]*i_weight2 + (1<<5)) >> 6 );
90 }
91 #undef op_scale2
92 
93 #define PIXEL_AVG_C( name, width, height ) \
94 static void name( pixel *pix1, intptr_t i_stride_pix1, \
95                   pixel *pix2, intptr_t i_stride_pix2, \
96                   pixel *pix3, intptr_t i_stride_pix3, int weight ) \
97 { \
98     if( weight == 32 ) \
99         pixel_avg_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, pix3, i_stride_pix3, width, height ); \
100     else \
101         pixel_avg_weight_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, pix3, i_stride_pix3, width, height, weight ); \
102 }
103 PIXEL_AVG_C( pixel_avg_16x16, 16, 16 )
104 PIXEL_AVG_C( pixel_avg_16x8,  16, 8 )
105 PIXEL_AVG_C( pixel_avg_8x16,  8, 16 )
106 PIXEL_AVG_C( pixel_avg_8x8,   8, 8 )
107 PIXEL_AVG_C( pixel_avg_8x4,   8, 4 )
108 PIXEL_AVG_C( pixel_avg_4x16,  4, 16 )
109 PIXEL_AVG_C( pixel_avg_4x8,   4, 8 )
110 PIXEL_AVG_C( pixel_avg_4x4,   4, 4 )
111 PIXEL_AVG_C( pixel_avg_4x2,   4, 2 )
112 PIXEL_AVG_C( pixel_avg_2x8,   2, 8 )
113 PIXEL_AVG_C( pixel_avg_2x4,   2, 4 )
114 PIXEL_AVG_C( pixel_avg_2x2,   2, 2 )
115 
x264_weight_cache(x264_t * h,x264_weight_t * w)116 static void x264_weight_cache( x264_t *h, x264_weight_t *w )
117 {
118     w->weightfn = h->mc.weight;
119 }
120 #define opscale(x) dst[x] = x264_clip_pixel( ((src[x] * scale + (1<<(denom - 1))) >> denom) + offset )
121 #define opscale_noden(x) dst[x] = x264_clip_pixel( src[x] * scale + offset )
mc_weight(pixel * dst,intptr_t i_dst_stride,pixel * src,intptr_t i_src_stride,const x264_weight_t * weight,int i_width,int i_height)122 static void mc_weight( pixel *dst, intptr_t i_dst_stride, pixel *src, intptr_t i_src_stride,
123                        const x264_weight_t *weight, int i_width, int i_height )
124 {
125     int offset = weight->i_offset << (BIT_DEPTH-8);
126     int scale = weight->i_scale;
127     int denom = weight->i_denom;
128     if( denom >= 1 )
129     {
130 		int y;
131 		int x;
132 
133 		for( y = 0; y < i_height; y++, dst += i_dst_stride, src += i_src_stride )
134             for( x = 0; x < i_width; x++ )
135                 opscale( x );
136     }
137     else
138     {
139 		int y;
140 		int x;
141 
142 		for( y = 0; y < i_height; y++, dst += i_dst_stride, src += i_src_stride )
143             for( x = 0; x < i_width; x++ )
144                 opscale_noden( x );
145     }
146 }
147 
148 #define MC_WEIGHT_C( name, width ) \
149     static void name( pixel *dst, intptr_t i_dst_stride, pixel *src, intptr_t i_src_stride, const x264_weight_t *weight, int height ) \
150 { \
151     mc_weight( dst, i_dst_stride, src, i_src_stride, weight, width, height );\
152 }
153 
154 MC_WEIGHT_C( mc_weight_w20, 20 )
155 MC_WEIGHT_C( mc_weight_w16, 16 )
156 MC_WEIGHT_C( mc_weight_w12, 12 )
157 MC_WEIGHT_C( mc_weight_w8,   8 )
158 MC_WEIGHT_C( mc_weight_w4,   4 )
159 MC_WEIGHT_C( mc_weight_w2,   2 )
160 
161 static weight_fn_t x264_mc_weight_wtab[6] =
162 {
163     mc_weight_w2,
164     mc_weight_w4,
165     mc_weight_w8,
166     mc_weight_w12,
167     mc_weight_w16,
168     mc_weight_w20,
169 };
170 const x264_weight_t x264_weight_none[3] = { {{0}} };
mc_copy(pixel * src,intptr_t i_src_stride,pixel * dst,intptr_t i_dst_stride,int i_width,int i_height)171 static void mc_copy( pixel *src, intptr_t i_src_stride, pixel *dst, intptr_t i_dst_stride, int i_width, int i_height )
172 {
173 	int y;
174 
175 	for( y = 0; y < i_height; y++ )
176     {
177         memcpy( dst, src, i_width * sizeof(pixel) );
178 
179         src += i_src_stride;
180         dst += i_dst_stride;
181     }
182 }
183 
184 #define TAPFILTER(pix, d) ((pix)[x-2*d] + (pix)[x+3*d] - 5*((pix)[x-d] + (pix)[x+2*d]) + 20*((pix)[x] + (pix)[x+d]))
hpel_filter(pixel * dsth,pixel * dstv,pixel * dstc,pixel * src,intptr_t stride,int width,int height,int16_t * buf)185 static void hpel_filter( pixel *dsth, pixel *dstv, pixel *dstc, pixel *src,
186                          intptr_t stride, int width, int height, int16_t *buf )
187 {
188     const int pad = (BIT_DEPTH > 9) ? (-10 * PIXEL_MAX) : 0;
189 	int y;
190 	int x;
191 
192 	for( y = 0; y < height; y++ )
193     {
194         for( x = -2; x < width+3; x++ )
195         {
196             int v = TAPFILTER(src,stride);
197             dstv[x] = x264_clip_pixel( (v + 16) >> 5 );
198             /* transform v for storage in a 16-bit integer */
199             buf[x+2] = v + pad;
200         }
201         for( x = 0; x < width; x++ )
202             dstc[x] = x264_clip_pixel( (TAPFILTER(buf+2,1) - 32*pad + 512) >> 10 );
203         for( x = 0; x < width; x++ )
204             dsth[x] = x264_clip_pixel( (TAPFILTER(src,1) + 16) >> 5 );
205         dsth += stride;
206         dstv += stride;
207         dstc += stride;
208         src += stride;
209     }
210 }
211 
212 static const uint8_t hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};
213 static const uint8_t hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};
214 
mc_luma(pixel * dst,intptr_t i_dst_stride,pixel * src[4],intptr_t i_src_stride,int mvx,int mvy,int i_width,int i_height,const x264_weight_t * weight)215 static void mc_luma( pixel *dst,    intptr_t i_dst_stride,
216                      pixel *src[4], intptr_t i_src_stride,
217                      int mvx, int mvy,
218                      int i_width, int i_height, const x264_weight_t *weight )
219 {
220     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
221     int offset = (mvy>>2)*i_src_stride + (mvx>>2);
222     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;
223 
224     if( qpel_idx & 5 ) /* qpel interpolation needed */
225     {
226         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
227         pixel_avg( dst, i_dst_stride, src1, i_src_stride,
228                    src2, i_src_stride, i_width, i_height );
229         if( weight->weightfn )
230             mc_weight( dst, i_dst_stride, dst, i_dst_stride, weight, i_width, i_height );
231     }
232     else if( weight->weightfn )
233         mc_weight( dst, i_dst_stride, src1, i_src_stride, weight, i_width, i_height );
234     else
235         mc_copy( src1, i_src_stride, dst, i_dst_stride, i_width, i_height );
236 }
237 
get_ref(pixel * dst,intptr_t * i_dst_stride,pixel * src[4],intptr_t i_src_stride,int mvx,int mvy,int i_width,int i_height,const x264_weight_t * weight)238 static pixel *get_ref( pixel *dst,   intptr_t *i_dst_stride,
239                        pixel *src[4], intptr_t i_src_stride,
240                        int mvx, int mvy,
241                        int i_width, int i_height, const x264_weight_t *weight )
242 {
243     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
244     int offset = (mvy>>2)*i_src_stride + (mvx>>2);
245     pixel *src1 = src[hpel_ref0[qpel_idx]] + offset + ((mvy&3) == 3) * i_src_stride;
246 
247     if( qpel_idx & 5 ) /* qpel interpolation needed */
248     {
249         pixel *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
250         pixel_avg( dst, *i_dst_stride, src1, i_src_stride,
251                    src2, i_src_stride, i_width, i_height );
252         if( weight->weightfn )
253             mc_weight( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_width, i_height );
254         return dst;
255     }
256     else if( weight->weightfn )
257     {
258         mc_weight( dst, *i_dst_stride, src1, i_src_stride, weight, i_width, i_height );
259         return dst;
260     }
261     else
262     {
263         *i_dst_stride = i_src_stride;
264         return src1;
265     }
266 }
267 
268 /* full chroma mc (ie until 1/8 pixel)*/
mc_chroma(pixel * dstu,pixel * dstv,intptr_t i_dst_stride,pixel * src,intptr_t i_src_stride,int mvx,int mvy,int i_width,int i_height)269 static void mc_chroma( pixel *dstu, pixel *dstv, intptr_t i_dst_stride,
270                        pixel *src, intptr_t i_src_stride,
271                        int mvx, int mvy,
272                        int i_width, int i_height )
273 {
274     pixel *srcp;
275 
276     int d8x = mvx&0x07;
277     int d8y = mvy&0x07;
278     int cA = (8-d8x)*(8-d8y);
279     int cB = d8x    *(8-d8y);
280     int cC = (8-d8x)*d8y;
281     int cD = d8x    *d8y;
282 	int y;
283 	int x;
284 
285     src += (mvy >> 3) * i_src_stride + (mvx >> 3)*2;
286     srcp = &src[i_src_stride];
287 
288     for( y = 0; y < i_height; y++ )
289     {
290         for( x = 0; x < i_width; x++ )
291         {
292             dstu[x] = ( cA*src[2*x]  + cB*src[2*x+2] +
293                         cC*srcp[2*x] + cD*srcp[2*x+2] + 32 ) >> 6;
294             dstv[x] = ( cA*src[2*x+1]  + cB*src[2*x+3] +
295                         cC*srcp[2*x+1] + cD*srcp[2*x+3] + 32 ) >> 6;
296         }
297         dstu += i_dst_stride;
298         dstv += i_dst_stride;
299         src   = srcp;
300         srcp += i_src_stride;
301     }
302 }
303 
304 #define MC_COPY(W) \
305 static void mc_copy_w##W( pixel *dst, intptr_t i_dst, pixel *src, intptr_t i_src, int i_height ) \
306 { \
307     mc_copy( src, i_src, dst, i_dst, W, i_height ); \
308 }
309 MC_COPY( 16 )
310 MC_COPY( 8 )
311 MC_COPY( 4 )
312 
x264_plane_copy_c(pixel * dst,intptr_t i_dst,pixel * src,intptr_t i_src,int w,int h)313 void x264_plane_copy_c( pixel *dst, intptr_t i_dst,
314                         pixel *src, intptr_t i_src, int w, int h )
315 {
316     while( h-- )
317     {
318         memcpy( dst, src, w * sizeof(pixel) );
319         dst += i_dst;
320         src += i_src;
321     }
322 }
323 
x264_plane_copy_interleave_c(pixel * dst,intptr_t i_dst,pixel * srcu,intptr_t i_srcu,pixel * srcv,intptr_t i_srcv,int w,int h)324 void x264_plane_copy_interleave_c( pixel *dst,  intptr_t i_dst,
325                                    pixel *srcu, intptr_t i_srcu,
326                                    pixel *srcv, intptr_t i_srcv, int w, int h )
327 {
328 	int y;
329 	int x;
330 
331 	for( y=0; y<h; y++, dst+=i_dst, srcu+=i_srcu, srcv+=i_srcv )
332         for( x=0; x<w; x++ )
333         {
334             dst[2*x]   = srcu[x];
335             dst[2*x+1] = srcv[x];
336         }
337 }
338 
x264_plane_copy_deinterleave_c(pixel * dstu,intptr_t i_dstu,pixel * dstv,intptr_t i_dstv,pixel * src,intptr_t i_src,int w,int h)339 static void x264_plane_copy_deinterleave_c( pixel *dstu, intptr_t i_dstu,
340                                             pixel *dstv, intptr_t i_dstv,
341                                             pixel *src,  intptr_t i_src, int w, int h )
342 {
343 	int y;
344 	int x;
345 
346 	for( y=0; y<h; y++, dstu+=i_dstu, dstv+=i_dstv, src+=i_src )
347         for( x=0; x<w; x++ )
348         {
349             dstu[x] = src[2*x];
350             dstv[x] = src[2*x+1];
351         }
352 }
353 
x264_plane_copy_deinterleave_rgb_c(pixel * dsta,intptr_t i_dsta,pixel * dstb,intptr_t i_dstb,pixel * dstc,intptr_t i_dstc,pixel * src,intptr_t i_src,int pw,int w,int h)354 static void x264_plane_copy_deinterleave_rgb_c( pixel *dsta, intptr_t i_dsta,
355                                                 pixel *dstb, intptr_t i_dstb,
356                                                 pixel *dstc, intptr_t i_dstc,
357                                                 pixel *src,  intptr_t i_src, int pw, int w, int h )
358 {
359 	int y;
360 	int x;
361 
362 	for( y=0; y<h; y++, dsta+=i_dsta, dstb+=i_dstb, dstc+=i_dstc, src+=i_src )
363     {
364         for( x=0; x<w; x++ )
365         {
366             dsta[x] = src[x*pw];
367             dstb[x] = src[x*pw+1];
368             dstc[x] = src[x*pw+2];
369         }
370     }
371 }
372 
x264_plane_copy_deinterleave_v210_c(pixel * dsty,intptr_t i_dsty,pixel * dstc,intptr_t i_dstc,uint32_t * src,intptr_t i_src,int w,int h)373 void x264_plane_copy_deinterleave_v210_c( pixel *dsty, intptr_t i_dsty,
374                                           pixel *dstc, intptr_t i_dstc,
375                                           uint32_t *src, intptr_t i_src, int w, int h )
376 {
377 	int l;
378 
379 	for( l = 0; l < h; l++ )
380     {
381         pixel *dsty0 = dsty;
382         pixel *dstc0 = dstc;
383         uint32_t *src0 = src;
384 		int n;
385 
386         for( n = 0; n < w; n += 3 )
387         {
388             *(dstc0++) = *src0 & 0x03FF;
389             *(dsty0++) = ( *src0 >> 10 ) & 0x03FF;
390             *(dstc0++) = ( *src0 >> 20 ) & 0x03FF;
391             src0++;
392             *(dsty0++) = *src0 & 0x03FF;
393             *(dstc0++) = ( *src0 >> 10 ) & 0x03FF;
394             *(dsty0++) = ( *src0 >> 20 ) & 0x03FF;
395             src0++;
396         }
397 
398         dsty += i_dsty;
399         dstc += i_dstc;
400         src  += i_src;
401     }
402 }
403 
store_interleave_chroma(pixel * dst,intptr_t i_dst,pixel * srcu,pixel * srcv,int height)404 static void store_interleave_chroma( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height )
405 {
406 	int y;
407 	int x;
408 
409 	for( y=0; y<height; y++, dst+=i_dst, srcu+=FDEC_STRIDE, srcv+=FDEC_STRIDE )
410         for( x=0; x<8; x++ )
411         {
412             dst[2*x]   = srcu[x];
413             dst[2*x+1] = srcv[x];
414         }
415 }
416 
load_deinterleave_chroma_fenc(pixel * dst,pixel * src,intptr_t i_src,int height)417 static void load_deinterleave_chroma_fenc( pixel *dst, pixel *src, intptr_t i_src, int height )
418 {
419     x264_plane_copy_deinterleave_c( dst, FENC_STRIDE, dst+FENC_STRIDE/2, FENC_STRIDE, src, i_src, 8, height );
420 }
421 
load_deinterleave_chroma_fdec(pixel * dst,pixel * src,intptr_t i_src,int height)422 static void load_deinterleave_chroma_fdec( pixel *dst, pixel *src, intptr_t i_src, int height )
423 {
424     x264_plane_copy_deinterleave_c( dst, FDEC_STRIDE, dst+FDEC_STRIDE/2, FDEC_STRIDE, src, i_src, 8, height );
425 }
426 
prefetch_fenc_null(pixel * pix_y,intptr_t stride_y,pixel * pix_uv,intptr_t stride_uv,int mb_x)427 static void prefetch_fenc_null( pixel *pix_y,  intptr_t stride_y,
428                                 pixel *pix_uv, intptr_t stride_uv, int mb_x )
429 {}
430 
prefetch_ref_null(pixel * pix,intptr_t stride,int parity)431 static void prefetch_ref_null( pixel *pix, intptr_t stride, int parity )
432 {}
433 
memzero_aligned(void * dst,size_t n)434 static void memzero_aligned( void * dst, size_t n )
435 {
436     memset( dst, 0, n );
437 }
438 
integral_init4h(uint16_t * sum,pixel * pix,intptr_t stride)439 static void integral_init4h( uint16_t *sum, pixel *pix, intptr_t stride )
440 {
441     int v = pix[0]+pix[1]+pix[2]+pix[3];
442 	int x;
443 
444 	for( x = 0; x < stride-4; x++ )
445     {
446         sum[x] = v + sum[x-stride];
447         v += pix[x+4] - pix[x];
448     }
449 }
450 
integral_init8h(uint16_t * sum,pixel * pix,intptr_t stride)451 static void integral_init8h( uint16_t *sum, pixel *pix, intptr_t stride )
452 {
453     int v = pix[0]+pix[1]+pix[2]+pix[3]+pix[4]+pix[5]+pix[6]+pix[7];
454 	int x;
455 
456 	for( x = 0; x < stride-8; x++ )
457     {
458         sum[x] = v + sum[x-stride];
459         v += pix[x+8] - pix[x];
460     }
461 }
462 
integral_init4v(uint16_t * sum8,uint16_t * sum4,intptr_t stride)463 static void integral_init4v( uint16_t *sum8, uint16_t *sum4, intptr_t stride )
464 {
465 	int x;
466 
467 	for( x = 0; x < stride-8; x++ )
468         sum4[x] = sum8[x+4*stride] - sum8[x];
469     for( x = 0; x < stride-8; x++ )
470         sum8[x] = sum8[x+8*stride] + sum8[x+8*stride+4] - sum8[x] - sum8[x+4];
471 }
472 
integral_init8v(uint16_t * sum8,intptr_t stride)473 static void integral_init8v( uint16_t *sum8, intptr_t stride )
474 {
475 	int x;
476 
477 	for( x = 0; x < stride-8; x++ )
478         sum8[x] = sum8[x+8*stride] - sum8[x];
479 }
480 
x264_frame_init_lowres(x264_t * h,x264_frame_t * frame)481 void x264_frame_init_lowres( x264_t *h, x264_frame_t *frame )
482 {
483     pixel *src = frame->plane[0];
484     int i_stride = frame->i_stride[0];
485     int i_height = frame->i_lines[0];
486     int i_width  = frame->i_width[0];
487 	int y;
488     int x;
489 
490     // duplicate last row and column so that their interpolation doesn't have to be special-cased
491     for( y = 0; y < i_height; y++ )
492         src[i_width+y*i_stride] = src[i_width-1+y*i_stride];
493     memcpy( src+i_stride*i_height, src+i_stride*(i_height-1), (i_width+1) * sizeof(pixel) );
494     h->mc.frame_init_lowres_core( src, frame->lowres[0], frame->lowres[1], frame->lowres[2], frame->lowres[3],
495                                   i_stride, frame->i_stride_lowres, frame->i_width_lowres, frame->i_lines_lowres );
496     x264_frame_expand_border_lowres( frame );
497 
498     memset( frame->i_cost_est, -1, sizeof(frame->i_cost_est) );
499 
500     for( y = 0; y < h->param.i_bframe + 2; y++ ) {
501 		for( x = 0; x < h->param.i_bframe + 2; x++ )
502             frame->i_row_satds[y][x][0] = -1;
503 	}
504     for( y = 0; y <= !!h->param.i_bframe; y++ )
505         for( x = 0; x <= h->param.i_bframe; x++ )
506             frame->lowres_mvs[y][x][0][0] = 0x7FFF;
507 }
508 
frame_init_lowres_core(pixel * src0,pixel * dst0,pixel * dsth,pixel * dstv,pixel * dstc,intptr_t src_stride,intptr_t dst_stride,int width,int height)509 static void frame_init_lowres_core( pixel *src0, pixel *dst0, pixel *dsth, pixel *dstv, pixel *dstc,
510                                     intptr_t src_stride, intptr_t dst_stride, int width, int height )
511 {
512 	int y;
513 
514 	for( y = 0; y < height; y++ )
515     {
516 		int x;
517 
518 		pixel *src1 = src0+src_stride;
519         pixel *src2 = src1+src_stride;
520 		for( x = 0; x<width; x++ )
521         {
522             // slower than naive bilinear, but matches asm
523 #define FILTER(a,b,c,d) ((((a+b+1)>>1)+((c+d+1)>>1)+1)>>1)
524             dst0[x] = FILTER(src0[2*x  ], src1[2*x  ], src0[2*x+1], src1[2*x+1]);
525             dsth[x] = FILTER(src0[2*x+1], src1[2*x+1], src0[2*x+2], src1[2*x+2]);
526             dstv[x] = FILTER(src1[2*x  ], src2[2*x  ], src1[2*x+1], src2[2*x+1]);
527             dstc[x] = FILTER(src1[2*x+1], src2[2*x+1], src1[2*x+2], src2[2*x+2]);
528 #undef FILTER
529         }
530         src0 += src_stride*2;
531         dst0 += dst_stride;
532         dsth += dst_stride;
533         dstv += dst_stride;
534         dstc += dst_stride;
535     }
536 }
537 
538 /* Estimate the total amount of influence on future quality that could be had if we
539  * were to improve the reference samples used to inter predict any given macroblock. */
mbtree_propagate_cost(int16_t * dst,uint16_t * propagate_in,uint16_t * intra_costs,uint16_t * inter_costs,uint16_t * inv_qscales,float * fps_factor,int len)540 static void mbtree_propagate_cost( int16_t *dst, uint16_t *propagate_in, uint16_t *intra_costs,
541                                    uint16_t *inter_costs, uint16_t *inv_qscales, float *fps_factor, int len )
542 {
543     float fps = *fps_factor;
544 	int i;
545 
546 	for( i = 0; i < len; i++ )
547     {
548         int intra_cost = intra_costs[i];
549         int inter_cost = X264_MIN(intra_costs[i], inter_costs[i] & LOWRES_COST_MASK);
550         float propagate_intra  = intra_cost * inv_qscales[i];
551         float propagate_amount = propagate_in[i] + propagate_intra*fps;
552         float propagate_num    = intra_cost - inter_cost;
553         float propagate_denom  = intra_cost;
554         dst[i] = X264_MIN((int)(propagate_amount * propagate_num / propagate_denom + 0.5f), 32767);
555     }
556 }
557 
mbtree_propagate_list(x264_t * h,uint16_t * ref_costs,int16_t (* mvs)[2],int16_t * propagate_amount,uint16_t * lowres_costs,int bipred_weight,int mb_y,int len,int list)558 static void mbtree_propagate_list( x264_t *h, uint16_t *ref_costs, int16_t (*mvs)[2],
559                                    int16_t *propagate_amount, uint16_t *lowres_costs,
560                                    int bipred_weight, int mb_y, int len, int list )
561 {
562     unsigned stride = h->mb.i_mb_stride;
563     unsigned width = h->mb.i_mb_width;
564     unsigned height = h->mb.i_mb_height;
565 	unsigned i;
566 
567 	for( i = 0; i < len; i++ )
568     {
569 #define CLIP_ADD(s,x) (s) = X264_MIN((s)+(x),(1<<15)-1)
570         int lists_used = lowres_costs[i]>>LOWRES_COST_SHIFT;
571         int listamount;
572         int x;
573         int y;
574         unsigned mbx;
575         unsigned mby;
576         unsigned idx0;
577         unsigned idx2;
578         int idx0weight;
579         int idx1weight;
580         int idx2weight;
581         int idx3weight;
582 
583         if( !(lists_used & (1 << list)) )
584             continue;
585 
586         listamount = propagate_amount[i];
587 
588 		/* Apply bipred weighting. */
589         if( lists_used == 3 )
590             listamount = (listamount * bipred_weight + 32) >> 6;
591 
592         /* Early termination for simple case of mv0. */
593         if( !M32( mvs[i] ) )
594         {
595             CLIP_ADD( ref_costs[mb_y*stride + i], listamount );
596             continue;
597         }
598 
599         x = mvs[i][0];
600         y = mvs[i][1];
601         mbx = (x>>5)+i;
602         mby = (y>>5)+mb_y;
603         idx0 = mbx + mby * stride;
604         idx2 = idx0 + stride;
605         x &= 31;
606         y &= 31;
607         idx0weight = (32-y)*(32-x);
608         idx1weight = (32-y)*x;
609         idx2weight = y*(32-x);
610         idx3weight = y*x;
611         idx0weight = (idx0weight * listamount + 512) >> 10;
612         idx1weight = (idx1weight * listamount + 512) >> 10;
613         idx2weight = (idx2weight * listamount + 512) >> 10;
614         idx3weight = (idx3weight * listamount + 512) >> 10;
615 
616         if( mbx < width-1 && mby < height-1 )
617         {
618             CLIP_ADD( ref_costs[idx0+0], idx0weight );
619             CLIP_ADD( ref_costs[idx0+1], idx1weight );
620             CLIP_ADD( ref_costs[idx2+0], idx2weight );
621             CLIP_ADD( ref_costs[idx2+1], idx3weight );
622         }
623         else
624         {
625             /* Note: this takes advantage of unsigned representation to
626              * catch negative mbx/mby. */
627             if( mby < height )
628             {
629                 if( mbx < width )
630                     CLIP_ADD( ref_costs[idx0+0], idx0weight );
631                 if( mbx+1 < width )
632                     CLIP_ADD( ref_costs[idx0+1], idx1weight );
633             }
634             if( mby+1 < height )
635             {
636                 if( mbx < width )
637                     CLIP_ADD( ref_costs[idx2+0], idx2weight );
638                 if( mbx+1 < width )
639                     CLIP_ADD( ref_costs[idx2+1], idx3weight );
640             }
641         }
642     }
643 #undef CLIP_ADD
644 }
645 
x264_mc_init(int cpu,x264_mc_functions_t * pf,int cpu_independent)646 void x264_mc_init( int cpu, x264_mc_functions_t *pf, int cpu_independent )
647 {
648     pf->mc_luma   = mc_luma;
649     pf->get_ref   = get_ref;
650 
651     pf->mc_chroma = mc_chroma;
652 
653     pf->avg[PIXEL_16x16]= pixel_avg_16x16;
654     pf->avg[PIXEL_16x8] = pixel_avg_16x8;
655     pf->avg[PIXEL_8x16] = pixel_avg_8x16;
656     pf->avg[PIXEL_8x8]  = pixel_avg_8x8;
657     pf->avg[PIXEL_8x4]  = pixel_avg_8x4;
658     pf->avg[PIXEL_4x16] = pixel_avg_4x16;
659     pf->avg[PIXEL_4x8]  = pixel_avg_4x8;
660     pf->avg[PIXEL_4x4]  = pixel_avg_4x4;
661     pf->avg[PIXEL_4x2]  = pixel_avg_4x2;
662     pf->avg[PIXEL_2x8]  = pixel_avg_2x8;
663     pf->avg[PIXEL_2x4]  = pixel_avg_2x4;
664     pf->avg[PIXEL_2x2]  = pixel_avg_2x2;
665 
666     pf->weight    = x264_mc_weight_wtab;
667     pf->offsetadd = x264_mc_weight_wtab;
668     pf->offsetsub = x264_mc_weight_wtab;
669     pf->weight_cache = x264_weight_cache;
670 
671     pf->copy_16x16_unaligned = mc_copy_w16;
672     pf->copy[PIXEL_16x16] = mc_copy_w16;
673     pf->copy[PIXEL_8x8]   = mc_copy_w8;
674     pf->copy[PIXEL_4x4]   = mc_copy_w4;
675 
676     pf->store_interleave_chroma       = store_interleave_chroma;
677     pf->load_deinterleave_chroma_fenc = load_deinterleave_chroma_fenc;
678     pf->load_deinterleave_chroma_fdec = load_deinterleave_chroma_fdec;
679 
680     pf->plane_copy = x264_plane_copy_c;
681     pf->plane_copy_interleave = x264_plane_copy_interleave_c;
682     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_c;
683     pf->plane_copy_deinterleave_rgb = x264_plane_copy_deinterleave_rgb_c;
684     pf->plane_copy_deinterleave_v210 = x264_plane_copy_deinterleave_v210_c;
685 
686     pf->hpel_filter = hpel_filter;
687 
688     pf->prefetch_fenc_420 = prefetch_fenc_null;
689     pf->prefetch_fenc_422 = prefetch_fenc_null;
690     pf->prefetch_ref  = prefetch_ref_null;
691     pf->memcpy_aligned = memcpy;
692     pf->memzero_aligned = memzero_aligned;
693     pf->frame_init_lowres_core = frame_init_lowres_core;
694 
695     pf->integral_init4h = integral_init4h;
696     pf->integral_init8h = integral_init8h;
697     pf->integral_init4v = integral_init4v;
698     pf->integral_init8v = integral_init8v;
699 
700     pf->mbtree_propagate_cost = mbtree_propagate_cost;
701     pf->mbtree_propagate_list = mbtree_propagate_list;
702 
703 #if HAVE_MMX
704     x264_mc_init_mmx( cpu, pf );
705 #endif
706 #if HAVE_ALTIVEC
707     if( cpu&X264_CPU_ALTIVEC )
708         x264_mc_altivec_init( pf );
709 #endif
710 #if HAVE_ARMV6
711     x264_mc_init_arm( cpu, pf );
712 #endif
713 #if ARCH_AARCH64
714     x264_mc_init_aarch64( cpu, pf );
715 #endif
716 
717     if( cpu_independent )
718     {
719         pf->mbtree_propagate_cost = mbtree_propagate_cost;
720         pf->mbtree_propagate_list = mbtree_propagate_list;
721     }
722 }
723 
x264_frame_filter(x264_t * h,x264_frame_t * frame,int mb_y,int b_end)724 void x264_frame_filter( x264_t *h, x264_frame_t *frame, int mb_y, int b_end )
725 {
726     const int b_interlaced = PARAM_INTERLACED;
727     int start = mb_y*16 - 8; // buffer = 4 for deblock + 3 for 6tap, rounded to 8
728     int height = (b_end ? frame->i_lines[0] + 16*PARAM_INTERLACED : (mb_y+b_interlaced)*16) + 8;
729 	int p;
730 
731     if( mb_y & b_interlaced )
732         return;
733 
734     for( p = 0; p < (CHROMA444 ? 3 : 1); p++ )
735     {
736         int stride = frame->i_stride[p];
737         const int width = frame->i_width[p];
738         int offs = start*stride - 8; // buffer = 3 for 6tap, aligned to 8 for simd
739 
740         if( !b_interlaced || h->mb.b_adaptive_mbaff )
741             h->mc.hpel_filter(
742                 frame->filtered[p][1] + offs,
743                 frame->filtered[p][2] + offs,
744                 frame->filtered[p][3] + offs,
745                 frame->plane[p] + offs,
746                 stride, width + 16, height - start,
747                 h->scratch_buffer );
748 
749         if( b_interlaced )
750         {
751             int height_fld;
752 			int i;
753 
754 			/* MC must happen between pixels in the same field. */
755             stride = frame->i_stride[p] << 1;
756             start = (mb_y*16 >> 1) - 8;
757             height_fld = ((b_end ? frame->i_lines[p] : mb_y*16) >> 1) + 8;
758             offs = start*stride - 8;
759             for( i = 0; i < 2; i++, offs += frame->i_stride[p] )
760             {
761                 h->mc.hpel_filter(
762                     frame->filtered_fld[p][1] + offs,
763                     frame->filtered_fld[p][2] + offs,
764                     frame->filtered_fld[p][3] + offs,
765                     frame->plane_fld[p] + offs,
766                     stride, width + 16, height_fld - start,
767                     h->scratch_buffer );
768             }
769         }
770     }
771 
772     /* generate integral image:
773      * frame->integral contains 2 planes. in the upper plane, each element is
774      * the sum of an 8x8 pixel region with top-left corner on that point.
775      * in the lower plane, 4x4 sums (needed only with --partitions p4x4). */
776 
777     if( frame->integral )
778     {
779         int stride = frame->i_stride[0];
780 		int y;
781 
782 		if( start < 0 )
783         {
784             memset( frame->integral - PADV * stride - PADH, 0, stride * sizeof(uint16_t) );
785             start = -PADV;
786         }
787         if( b_end )
788             height += PADV-9;
789         for( y = start; y < height; y++ )
790         {
791             pixel    *pix  = frame->plane[0] + y * stride - PADH;
792             uint16_t *sum8 = frame->integral + (y+1) * stride - PADH;
793             uint16_t *sum4;
794             if( h->frames.b_have_sub8x8_esa )
795             {
796                 h->mc.integral_init4h( sum8, pix, stride );
797                 sum8 -= 8*stride;
798                 sum4 = sum8 + stride * (frame->i_lines[0] + PADV*2);
799                 if( y >= 8-PADV )
800                     h->mc.integral_init4v( sum8, sum4, stride );
801             }
802             else
803             {
804                 h->mc.integral_init8h( sum8, pix, stride );
805                 if( y >= 8-PADV )
806                     h->mc.integral_init8v( sum8-8*stride, stride );
807             }
808         }
809     }
810 }
811