1 /*****************************************************************************
2  * mc-c.c: aarch64 motion compensation
3  *****************************************************************************
4  * Copyright (C) 2009-2021 x264 project
5  *
6  * Authors: David Conrad <lessen42@gmail.com>
7  *          Janne Grunau <janne-x264@jannau.net>
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/common.h"
28 #include "mc.h"
29 
30 #define x264_prefetch_ref_aarch64 x264_template(prefetch_ref_aarch64)
31 void x264_prefetch_ref_aarch64( uint8_t *, intptr_t, int );
32 #define x264_prefetch_fenc_420_aarch64 x264_template(prefetch_fenc_420_aarch64)
33 void x264_prefetch_fenc_420_aarch64( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
34 #define x264_prefetch_fenc_422_aarch64 x264_template(prefetch_fenc_422_aarch64)
35 void x264_prefetch_fenc_422_aarch64( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
36 
37 #define x264_memcpy_aligned_neon x264_template(memcpy_aligned_neon)
38 void *x264_memcpy_aligned_neon( void *dst, const void *src, size_t n );
39 #define x264_memzero_aligned_neon x264_template(memzero_aligned_neon)
40 void x264_memzero_aligned_neon( void *dst, size_t n );
41 
42 #define x264_pixel_avg_16x16_neon x264_template(pixel_avg_16x16_neon)
43 void x264_pixel_avg_16x16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
44 #define x264_pixel_avg_16x8_neon x264_template(pixel_avg_16x8_neon)
45 void x264_pixel_avg_16x8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
46 #define x264_pixel_avg_8x16_neon x264_template(pixel_avg_8x16_neon)
47 void x264_pixel_avg_8x16_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
48 #define x264_pixel_avg_8x8_neon x264_template(pixel_avg_8x8_neon)
49 void x264_pixel_avg_8x8_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
50 #define x264_pixel_avg_8x4_neon x264_template(pixel_avg_8x4_neon)
51 void x264_pixel_avg_8x4_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
52 #define x264_pixel_avg_4x16_neon x264_template(pixel_avg_4x16_neon)
53 void x264_pixel_avg_4x16_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
54 #define x264_pixel_avg_4x8_neon x264_template(pixel_avg_4x8_neon)
55 void x264_pixel_avg_4x8_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
56 #define x264_pixel_avg_4x4_neon x264_template(pixel_avg_4x4_neon)
57 void x264_pixel_avg_4x4_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
58 #define x264_pixel_avg_4x2_neon x264_template(pixel_avg_4x2_neon)
59 void x264_pixel_avg_4x2_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
60 
61 #define x264_pixel_avg2_w4_neon x264_template(pixel_avg2_w4_neon)
62 void x264_pixel_avg2_w4_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
63 #define x264_pixel_avg2_w8_neon x264_template(pixel_avg2_w8_neon)
64 void x264_pixel_avg2_w8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
65 #define x264_pixel_avg2_w16_neon x264_template(pixel_avg2_w16_neon)
66 void x264_pixel_avg2_w16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
67 #define x264_pixel_avg2_w20_neon x264_template(pixel_avg2_w20_neon)
68 void x264_pixel_avg2_w20_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
69 
70 #define x264_plane_copy_core_neon x264_template(plane_copy_core_neon)
71 void x264_plane_copy_core_neon( pixel *dst, intptr_t i_dst,
72                                 pixel *src, intptr_t i_src, int w, int h );
73 #define x264_plane_copy_swap_core_neon x264_template(plane_copy_swap_core_neon)
74 void x264_plane_copy_swap_core_neon( pixel *dst, intptr_t i_dst,
75                                      pixel *src, intptr_t i_src, int w, int h );
76 #define x264_plane_copy_deinterleave_neon x264_template(plane_copy_deinterleave_neon)
77 void x264_plane_copy_deinterleave_neon(  pixel *dstu, intptr_t i_dstu,
78                                          pixel *dstv, intptr_t i_dstv,
79                                          pixel *src,  intptr_t i_src, int w, int h );
80 #define x264_plane_copy_deinterleave_rgb_neon x264_template(plane_copy_deinterleave_rgb_neon)
81 void x264_plane_copy_deinterleave_rgb_neon( pixel *dsta, intptr_t i_dsta,
82                                             pixel *dstb, intptr_t i_dstb,
83                                             pixel *dstc, intptr_t i_dstc,
84                                             pixel *src,  intptr_t i_src, int pw, int w, int h );
85 #define x264_plane_copy_interleave_core_neon x264_template(plane_copy_interleave_core_neon)
86 void x264_plane_copy_interleave_core_neon( pixel *dst,  intptr_t i_dst,
87                                            pixel *srcu, intptr_t i_srcu,
88                                            pixel *srcv, intptr_t i_srcv, int w, int h );
89 
90 #define x264_store_interleave_chroma_neon x264_template(store_interleave_chroma_neon)
91 void x264_store_interleave_chroma_neon( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );
92 #define x264_load_deinterleave_chroma_fdec_neon x264_template(load_deinterleave_chroma_fdec_neon)
93 void x264_load_deinterleave_chroma_fdec_neon( pixel *dst, pixel *src, intptr_t i_src, int height );
94 #define x264_load_deinterleave_chroma_fenc_neon x264_template(load_deinterleave_chroma_fenc_neon)
95 void x264_load_deinterleave_chroma_fenc_neon( pixel *dst, pixel *src, intptr_t i_src, int height );
96 
97 #define x264_mc_weight_w16_neon x264_template(mc_weight_w16_neon)
98 #define x264_mc_weight_w16_nodenom_neon x264_template(mc_weight_w16_nodenom_neon)
99 #define x264_mc_weight_w16_offsetadd_neon x264_template(mc_weight_w16_offsetadd_neon)
100 #define x264_mc_weight_w16_offsetsub_neon x264_template(mc_weight_w16_offsetsub_neon)
101 #define x264_mc_weight_w20_neon x264_template(mc_weight_w20_neon)
102 #define x264_mc_weight_w20_nodenom_neon x264_template(mc_weight_w20_nodenom_neon)
103 #define x264_mc_weight_w20_offsetadd_neon x264_template(mc_weight_w20_offsetadd_neon)
104 #define x264_mc_weight_w20_offsetsub_neon x264_template(mc_weight_w20_offsetsub_neon)
105 #define x264_mc_weight_w4_neon x264_template(mc_weight_w4_neon)
106 #define x264_mc_weight_w4_nodenom_neon x264_template(mc_weight_w4_nodenom_neon)
107 #define x264_mc_weight_w4_offsetadd_neon x264_template(mc_weight_w4_offsetadd_neon)
108 #define x264_mc_weight_w4_offsetsub_neon x264_template(mc_weight_w4_offsetsub_neon)
109 #define x264_mc_weight_w8_neon x264_template(mc_weight_w8_neon)
110 #define x264_mc_weight_w8_nodenom_neon x264_template(mc_weight_w8_nodenom_neon)
111 #define x264_mc_weight_w8_offsetadd_neon x264_template(mc_weight_w8_offsetadd_neon)
112 #define x264_mc_weight_w8_offsetsub_neon x264_template(mc_weight_w8_offsetsub_neon)
113 #define MC_WEIGHT(func)\
114 void x264_mc_weight_w20##func##_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
115 void x264_mc_weight_w16##func##_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
116 void x264_mc_weight_w8##func##_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
117 void x264_mc_weight_w4##func##_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
118 \
119 static void (* mc##func##_wtab_neon[6])( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int ) =\
120 {\
121     x264_mc_weight_w4##func##_neon,\
122     x264_mc_weight_w4##func##_neon,\
123     x264_mc_weight_w8##func##_neon,\
124     x264_mc_weight_w16##func##_neon,\
125     x264_mc_weight_w16##func##_neon,\
126     x264_mc_weight_w20##func##_neon,\
127 };
128 
129 #if !HIGH_BIT_DEPTH
130 MC_WEIGHT()
131 MC_WEIGHT(_nodenom)
132 MC_WEIGHT(_offsetadd)
133 MC_WEIGHT(_offsetsub)
134 #endif
135 
136 #define x264_mc_copy_w4_neon x264_template(mc_copy_w4_neon)
137 void x264_mc_copy_w4_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
138 #define x264_mc_copy_w8_neon x264_template(mc_copy_w8_neon)
139 void x264_mc_copy_w8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
140 #define x264_mc_copy_w16_neon x264_template(mc_copy_w16_neon)
141 void x264_mc_copy_w16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
142 
143 #define x264_mc_chroma_neon x264_template(mc_chroma_neon)
144 void x264_mc_chroma_neon( uint8_t *, uint8_t *, intptr_t, uint8_t *, intptr_t, int, int, int, int );
145 #define x264_integral_init4h_neon x264_template(integral_init4h_neon)
146 void x264_integral_init4h_neon( uint16_t *, uint8_t *, intptr_t );
147 #define x264_integral_init4v_neon x264_template(integral_init4v_neon)
148 void x264_integral_init4v_neon( uint16_t *, uint16_t *, intptr_t );
149 #define x264_integral_init8h_neon x264_template(integral_init8h_neon)
150 void x264_integral_init8h_neon( uint16_t *, uint8_t *, intptr_t );
151 #define x264_integral_init8v_neon x264_template(integral_init8v_neon)
152 void x264_integral_init8v_neon( uint16_t *, intptr_t );
153 #define x264_frame_init_lowres_core_neon x264_template(frame_init_lowres_core_neon)
154 void x264_frame_init_lowres_core_neon( uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *, intptr_t, intptr_t, int, int );
155 
156 #define x264_mbtree_propagate_cost_neon x264_template(mbtree_propagate_cost_neon)
157 void x264_mbtree_propagate_cost_neon( int16_t *, uint16_t *, uint16_t *, uint16_t *, uint16_t *, float *, int );
158 
159 #define x264_mbtree_fix8_pack_neon x264_template(mbtree_fix8_pack_neon)
160 void x264_mbtree_fix8_pack_neon( uint16_t *dst, float *src, int count );
161 #define x264_mbtree_fix8_unpack_neon x264_template(mbtree_fix8_unpack_neon)
162 void x264_mbtree_fix8_unpack_neon( float *dst, uint16_t *src, int count );
163 
164 #if !HIGH_BIT_DEPTH
weight_cache_neon(x264_t * h,x264_weight_t * w)165 static void weight_cache_neon( x264_t *h, x264_weight_t *w )
166 {
167     if( w->i_scale == 1<<w->i_denom )
168     {
169         if( w->i_offset < 0 )
170         {
171             w->weightfn = mc_offsetsub_wtab_neon;
172             w->cachea[0] = -w->i_offset;
173         }
174         else
175         {
176             w->weightfn = mc_offsetadd_wtab_neon;
177             w->cachea[0] = w->i_offset;
178         }
179     }
180     else if( !w->i_denom )
181         w->weightfn = mc_nodenom_wtab_neon;
182     else
183         w->weightfn = mc_wtab_neon;
184 }
185 
186 static void (* const pixel_avg_wtab_neon[6])( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int ) =
187 {
188     NULL,
189     x264_pixel_avg2_w4_neon,
190     x264_pixel_avg2_w8_neon,
191     x264_pixel_avg2_w16_neon,   // no slower than w12, so no point in a separate function
192     x264_pixel_avg2_w16_neon,
193     x264_pixel_avg2_w20_neon,
194 };
195 
196 static void (* const mc_copy_wtab_neon[5])( uint8_t *, intptr_t, uint8_t *, intptr_t, int ) =
197 {
198     NULL,
199     x264_mc_copy_w4_neon,
200     x264_mc_copy_w8_neon,
201     NULL,
202     x264_mc_copy_w16_neon,
203 };
204 
mc_luma_neon(uint8_t * dst,intptr_t i_dst_stride,uint8_t * src[4],intptr_t i_src_stride,int mvx,int mvy,int i_width,int i_height,const x264_weight_t * weight)205 static void mc_luma_neon( uint8_t *dst,    intptr_t i_dst_stride,
206                           uint8_t *src[4], intptr_t i_src_stride,
207                           int mvx, int mvy,
208                           int i_width, int i_height, const x264_weight_t *weight )
209 {
210     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
211     intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
212     uint8_t *src1 = src[x264_hpel_ref0[qpel_idx]] + offset;
213     if( (mvy&3) == 3 )             // explict if() to force conditional add
214         src1 += i_src_stride;
215 
216     if( qpel_idx & 5 ) /* qpel interpolation needed */
217     {
218         uint8_t *src2 = src[x264_hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
219         pixel_avg_wtab_neon[i_width>>2](
220                 dst, i_dst_stride, src1, i_src_stride,
221                 src2, i_height );
222         if( weight->weightfn )
223             weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );
224     }
225     else if( weight->weightfn )
226         weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );
227     else
228         mc_copy_wtab_neon[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, i_height );
229 }
230 
get_ref_neon(uint8_t * dst,intptr_t * i_dst_stride,uint8_t * src[4],intptr_t i_src_stride,int mvx,int mvy,int i_width,int i_height,const x264_weight_t * weight)231 static uint8_t *get_ref_neon( uint8_t *dst,   intptr_t *i_dst_stride,
232                               uint8_t *src[4], intptr_t i_src_stride,
233                               int mvx, int mvy,
234                               int i_width, int i_height, const x264_weight_t *weight )
235 {
236     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
237     intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
238     uint8_t *src1 = src[x264_hpel_ref0[qpel_idx]] + offset;
239     if( (mvy&3) == 3 )             // explict if() to force conditional add
240         src1 += i_src_stride;
241 
242     if( qpel_idx & 5 ) /* qpel interpolation needed */
243     {
244         uint8_t *src2 = src[x264_hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
245         pixel_avg_wtab_neon[i_width>>2](
246                 dst, *i_dst_stride, src1, i_src_stride,
247                 src2, i_height );
248         if( weight->weightfn )
249             weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );
250         return dst;
251     }
252     else if( weight->weightfn )
253     {
254         weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );
255         return dst;
256     }
257     else
258     {
259         *i_dst_stride = i_src_stride;
260         return src1;
261     }
262 }
263 
264 #define x264_hpel_filter_neon x264_template(hpel_filter_neon)
265 void x264_hpel_filter_neon( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc,
266                             uint8_t *src, intptr_t stride, int width,
267                             int height, int16_t *buf );
268 
269 PLANE_COPY(16, neon)
270 PLANE_COPY_SWAP(16, neon)
PLANE_INTERLEAVE(neon)271 PLANE_INTERLEAVE(neon)
272 PROPAGATE_LIST(neon)
273 #endif // !HIGH_BIT_DEPTH
274 
275 void x264_mc_init_aarch64( uint32_t cpu, x264_mc_functions_t *pf )
276 {
277 #if !HIGH_BIT_DEPTH
278     if( cpu&X264_CPU_ARMV8 )
279     {
280         pf->prefetch_fenc_420 = x264_prefetch_fenc_420_aarch64;
281         pf->prefetch_fenc_422 = x264_prefetch_fenc_422_aarch64;
282         pf->prefetch_ref      = x264_prefetch_ref_aarch64;
283     }
284 
285     if( !(cpu&X264_CPU_NEON) )
286         return;
287 
288     pf->copy_16x16_unaligned = x264_mc_copy_w16_neon;
289     pf->copy[PIXEL_16x16]    = x264_mc_copy_w16_neon;
290     pf->copy[PIXEL_8x8]      = x264_mc_copy_w8_neon;
291     pf->copy[PIXEL_4x4]      = x264_mc_copy_w4_neon;
292 
293     pf->plane_copy                  = plane_copy_neon;
294     pf->plane_copy_swap             = plane_copy_swap_neon;
295     pf->plane_copy_deinterleave     = x264_plane_copy_deinterleave_neon;
296     pf->plane_copy_deinterleave_rgb = x264_plane_copy_deinterleave_rgb_neon;
297     pf->plane_copy_interleave       = plane_copy_interleave_neon;
298 
299     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_neon;
300     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_neon;
301     pf->store_interleave_chroma       = x264_store_interleave_chroma_neon;
302 
303     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_neon;
304     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_neon;
305     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_neon;
306     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_neon;
307     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_neon;
308     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_neon;
309     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_neon;
310     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_neon;
311     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_neon;
312 
313     pf->weight       = mc_wtab_neon;
314     pf->offsetadd    = mc_offsetadd_wtab_neon;
315     pf->offsetsub    = mc_offsetsub_wtab_neon;
316     pf->weight_cache = weight_cache_neon;
317 
318     pf->mc_chroma = x264_mc_chroma_neon;
319     pf->mc_luma = mc_luma_neon;
320     pf->get_ref = get_ref_neon;
321     pf->hpel_filter = x264_hpel_filter_neon;
322     pf->frame_init_lowres_core = x264_frame_init_lowres_core_neon;
323 
324     pf->integral_init4h = x264_integral_init4h_neon;
325     pf->integral_init8h = x264_integral_init8h_neon;
326     pf->integral_init4v = x264_integral_init4v_neon;
327     pf->integral_init8v = x264_integral_init8v_neon;
328 
329     pf->mbtree_propagate_cost = x264_mbtree_propagate_cost_neon;
330     pf->mbtree_propagate_list = mbtree_propagate_list_neon;
331     pf->mbtree_fix8_pack      = x264_mbtree_fix8_pack_neon;
332     pf->mbtree_fix8_unpack    = x264_mbtree_fix8_unpack_neon;
333 
334     pf->memcpy_aligned  = x264_memcpy_aligned_neon;
335     pf->memzero_aligned = x264_memzero_aligned_neon;
336 #endif // !HIGH_BIT_DEPTH
337 }
338