]> git.sesse.net Git - x264/blob - common/arm/mc-c.c
arm: use the weight_fn_t typedef for mc weight function arrays
[x264] / common / arm / mc-c.c
1 /*****************************************************************************
2  * mc-c.c: arm motion compensation
3  *****************************************************************************
4  * Copyright (C) 2009-2014 x264 project
5  *
6  * Authors: David Conrad <lessen42@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21  *
22  * This program is also available under a commercial proprietary license.
23  * For more information, contact us at licensing@x264.com.
24  *****************************************************************************/
25
26 #include "common/common.h"
27 #include "mc.h"
28
29 void x264_prefetch_ref_arm( uint8_t *, intptr_t, int );
30 void x264_prefetch_fenc_arm( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
31
32 void *x264_memcpy_aligned_neon( void *dst, const void *src, size_t n );
33 void x264_memzero_aligned_neon( void *dst, size_t n );
34
35 void x264_pixel_avg_16x16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
36 void x264_pixel_avg_16x8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
37 void x264_pixel_avg_8x16_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
38 void x264_pixel_avg_8x8_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
39 void x264_pixel_avg_8x4_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
40 void x264_pixel_avg_4x16_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
41 void x264_pixel_avg_4x8_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
42 void x264_pixel_avg_4x4_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
43 void x264_pixel_avg_4x2_neon  ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, intptr_t, int );
44
45 void x264_pixel_avg2_w4_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
46 void x264_pixel_avg2_w8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
47 void x264_pixel_avg2_w16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
48 void x264_pixel_avg2_w20_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int );
49
50 void x264_plane_copy_deinterleave_neon(  pixel *dstu, intptr_t i_dstu,
51                                          pixel *dstv, intptr_t i_dstv,
52                                          pixel *src,  intptr_t i_src, int w, int h );
53 void x264_plane_copy_deinterleave_rgb_neon( pixel *dsta, intptr_t i_dsta,
54                                             pixel *dstb, intptr_t i_dstb,
55                                             pixel *dstc, intptr_t i_dstc,
56                                             pixel *src,  intptr_t i_src, int pw, int w, int h );
57 void x264_plane_copy_interleave_neon( pixel *dst,  intptr_t i_dst,
58                                       pixel *srcu, intptr_t i_srcu,
59                                       pixel *srcv, intptr_t i_srcv, int w, int h );
60
61 void x264_store_interleave_chroma_neon( pixel *dst, intptr_t i_dst, pixel *srcu, pixel *srcv, int height );
62 void x264_load_deinterleave_chroma_fdec_neon( pixel *dst, pixel *src, intptr_t i_src, int height );
63 void x264_load_deinterleave_chroma_fenc_neon( pixel *dst, pixel *src, intptr_t i_src, int height );
64
65 #define MC_WEIGHT(func)\
66 void x264_mc_weight_w20##func##_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
67 void x264_mc_weight_w16##func##_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
68 void x264_mc_weight_w8##func##_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
69 void x264_mc_weight_w4##func##_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, const x264_weight_t *, int );\
70 \
71 static weight_fn_t x264_mc##func##_wtab_neon[6] =\
72 {\
73     x264_mc_weight_w4##func##_neon,\
74     x264_mc_weight_w4##func##_neon,\
75     x264_mc_weight_w8##func##_neon,\
76     x264_mc_weight_w16##func##_neon,\
77     x264_mc_weight_w16##func##_neon,\
78     x264_mc_weight_w20##func##_neon,\
79 };
80
81 MC_WEIGHT()
82 MC_WEIGHT(_nodenom)
83 MC_WEIGHT(_offsetadd)
84 MC_WEIGHT(_offsetsub)
85
86 void x264_mc_copy_w4_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
87 void x264_mc_copy_w8_neon ( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
88 void x264_mc_copy_w16_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
89 void x264_mc_copy_w16_aligned_neon( uint8_t *, intptr_t, uint8_t *, intptr_t, int );
90
91 void x264_mc_chroma_neon( uint8_t *, uint8_t *, intptr_t, uint8_t *, intptr_t, int, int, int, int );
92 void x264_frame_init_lowres_core_neon( uint8_t *, uint8_t *, uint8_t *, uint8_t *, uint8_t *, intptr_t, intptr_t, int, int );
93
94 void x264_hpel_filter_v_neon( uint8_t *, uint8_t *, int16_t *, intptr_t, int );
95 void x264_hpel_filter_c_neon( uint8_t *, int16_t *, int );
96 void x264_hpel_filter_h_neon( uint8_t *, uint8_t *, int );
97
98 #if !HIGH_BIT_DEPTH
99 static void x264_weight_cache_neon( x264_t *h, x264_weight_t *w )
100 {
101     if( w->i_scale == 1<<w->i_denom )
102     {
103         if( w->i_offset < 0 )
104         {
105             w->weightfn = x264_mc_offsetsub_wtab_neon;
106             w->cachea[0] = -w->i_offset;
107         }
108         else
109         {
110             w->weightfn = x264_mc_offsetadd_wtab_neon;
111             w->cachea[0] = w->i_offset;
112         }
113     }
114     else if( !w->i_denom )
115         w->weightfn = x264_mc_nodenom_wtab_neon;
116     else
117         w->weightfn = x264_mc_wtab_neon;
118 }
119
120 static void (* const x264_pixel_avg_wtab_neon[6])( uint8_t *, intptr_t, uint8_t *, intptr_t, uint8_t *, int ) =
121 {
122     NULL,
123     x264_pixel_avg2_w4_neon,
124     x264_pixel_avg2_w8_neon,
125     x264_pixel_avg2_w16_neon,   // no slower than w12, so no point in a separate function
126     x264_pixel_avg2_w16_neon,
127     x264_pixel_avg2_w20_neon,
128 };
129
130 static void (* const x264_mc_copy_wtab_neon[5])( uint8_t *, intptr_t, uint8_t *, intptr_t, int ) =
131 {
132     NULL,
133     x264_mc_copy_w4_neon,
134     x264_mc_copy_w8_neon,
135     NULL,
136     x264_mc_copy_w16_neon,
137 };
138
139 static const uint8_t hpel_ref0[16] = {0,1,1,1,0,1,1,1,2,3,3,3,0,1,1,1};
140 static const uint8_t hpel_ref1[16] = {0,0,0,0,2,2,3,2,2,2,3,2,2,2,3,2};
141
142 static void mc_luma_neon( uint8_t *dst,    intptr_t i_dst_stride,
143                           uint8_t *src[4], intptr_t i_src_stride,
144                           int mvx, int mvy,
145                           int i_width, int i_height, const x264_weight_t *weight )
146 {
147     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
148     intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
149     uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset;
150     if ( (mvy&3) == 3 )             // explict if() to force conditional add
151         src1 += i_src_stride;
152
153     if( qpel_idx & 5 ) /* qpel interpolation needed */
154     {
155         uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
156         x264_pixel_avg_wtab_neon[i_width>>2](
157                 dst, i_dst_stride, src1, i_src_stride,
158                 src2, i_height );
159         if( weight->weightfn )
160             weight->weightfn[i_width>>2]( dst, i_dst_stride, dst, i_dst_stride, weight, i_height );
161     }
162     else if( weight->weightfn )
163         weight->weightfn[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, weight, i_height );
164     else
165         x264_mc_copy_wtab_neon[i_width>>2]( dst, i_dst_stride, src1, i_src_stride, i_height );
166 }
167
168 static uint8_t *get_ref_neon( uint8_t *dst,   intptr_t *i_dst_stride,
169                               uint8_t *src[4], intptr_t i_src_stride,
170                               int mvx, int mvy,
171                               int i_width, int i_height, const x264_weight_t *weight )
172 {
173     int qpel_idx = ((mvy&3)<<2) + (mvx&3);
174     intptr_t offset = (mvy>>2)*i_src_stride + (mvx>>2);
175     uint8_t *src1 = src[hpel_ref0[qpel_idx]] + offset;
176     if ( (mvy&3) == 3 )             // explict if() to force conditional add
177         src1 += i_src_stride;
178
179     if( qpel_idx & 5 ) /* qpel interpolation needed */
180     {
181         uint8_t *src2 = src[hpel_ref1[qpel_idx]] + offset + ((mvx&3) == 3);
182         x264_pixel_avg_wtab_neon[i_width>>2](
183                 dst, *i_dst_stride, src1, i_src_stride,
184                 src2, i_height );
185         if( weight->weightfn )
186             weight->weightfn[i_width>>2]( dst, *i_dst_stride, dst, *i_dst_stride, weight, i_height );
187         return dst;
188     }
189     else if( weight->weightfn )
190     {
191         weight->weightfn[i_width>>2]( dst, *i_dst_stride, src1, i_src_stride, weight, i_height );
192         return dst;
193     }
194     else
195     {
196         *i_dst_stride = i_src_stride;
197         return src1;
198     }
199 }
200
201 static void hpel_filter_neon( uint8_t *dsth, uint8_t *dstv, uint8_t *dstc, uint8_t *src,
202                               intptr_t stride, int width, int height, int16_t *buf )
203 {
204     intptr_t realign = (intptr_t)src & 15;
205     src -= realign;
206     dstv -= realign;
207     dstc -= realign;
208     dsth -= realign;
209     width += realign;
210     while( height-- )
211     {
212         x264_hpel_filter_v_neon( dstv, src, buf+8, stride, width );
213         x264_hpel_filter_c_neon( dstc, buf+8, width );
214         x264_hpel_filter_h_neon( dsth, src, width );
215         dsth += stride;
216         dstv += stride;
217         dstc += stride;
218         src  += stride;
219     }
220 }
221 #endif // !HIGH_BIT_DEPTH
222
223 void x264_mc_init_arm( int cpu, x264_mc_functions_t *pf )
224 {
225     if( !(cpu&X264_CPU_ARMV6) )
226         return;
227
228 #if !HIGH_BIT_DEPTH
229     pf->prefetch_fenc_420 = x264_prefetch_fenc_arm;
230     pf->prefetch_fenc_422 = x264_prefetch_fenc_arm; /* FIXME */
231     pf->prefetch_ref  = x264_prefetch_ref_arm;
232 #endif // !HIGH_BIT_DEPTH
233
234     if( !(cpu&X264_CPU_NEON) )
235         return;
236
237 #if !HIGH_BIT_DEPTH
238     pf->copy_16x16_unaligned = x264_mc_copy_w16_neon;
239     pf->copy[PIXEL_16x16] = x264_mc_copy_w16_aligned_neon;
240     pf->copy[PIXEL_8x8]   = x264_mc_copy_w8_neon;
241     pf->copy[PIXEL_4x4]   = x264_mc_copy_w4_neon;
242
243     pf->plane_copy_deinterleave = x264_plane_copy_deinterleave_neon;
244     pf->plane_copy_deinterleave_rgb = x264_plane_copy_deinterleave_rgb_neon;
245     pf->plane_copy_interleave = x264_plane_copy_interleave_neon;
246
247     pf->store_interleave_chroma = x264_store_interleave_chroma_neon;
248     pf->load_deinterleave_chroma_fdec = x264_load_deinterleave_chroma_fdec_neon;
249     pf->load_deinterleave_chroma_fenc = x264_load_deinterleave_chroma_fenc_neon;
250
251     pf->avg[PIXEL_16x16] = x264_pixel_avg_16x16_neon;
252     pf->avg[PIXEL_16x8]  = x264_pixel_avg_16x8_neon;
253     pf->avg[PIXEL_8x16]  = x264_pixel_avg_8x16_neon;
254     pf->avg[PIXEL_8x8]   = x264_pixel_avg_8x8_neon;
255     pf->avg[PIXEL_8x4]   = x264_pixel_avg_8x4_neon;
256     pf->avg[PIXEL_4x16]  = x264_pixel_avg_4x16_neon;
257     pf->avg[PIXEL_4x8]   = x264_pixel_avg_4x8_neon;
258     pf->avg[PIXEL_4x4]   = x264_pixel_avg_4x4_neon;
259     pf->avg[PIXEL_4x2]   = x264_pixel_avg_4x2_neon;
260
261     pf->weight    = x264_mc_wtab_neon;
262     pf->offsetadd = x264_mc_offsetadd_wtab_neon;
263     pf->offsetsub = x264_mc_offsetsub_wtab_neon;
264     pf->weight_cache = x264_weight_cache_neon;
265
266     pf->mc_chroma = x264_mc_chroma_neon;
267     pf->mc_luma = mc_luma_neon;
268     pf->get_ref = get_ref_neon;
269     pf->hpel_filter = hpel_filter_neon;
270     pf->frame_init_lowres_core = x264_frame_init_lowres_core_neon;
271 #endif // !HIGH_BIT_DEPTH
272
273 // Apple's gcc stupidly cannot align stack variables, and ALIGNED_ARRAY can't work on structs
274 #ifndef SYS_MACOSX
275     pf->memcpy_aligned  = x264_memcpy_aligned_neon;
276 #endif
277     pf->memzero_aligned = x264_memzero_aligned_neon;
278 }