• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright (c) 2020, Alliance for Open Media. All rights reserved
4  *
5  * This source code is subject to the terms of the BSD 2 Clause License and
6  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
7  * was not distributed with this source code in the LICENSE file, you can
8  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
9  * Media Patent License 1.0 was not distributed with this source code in the
10  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
11  */
12 #include <arm_neon.h>
13 #include <assert.h>
14 
15 #include "aom_dsp/arm/mem_neon.h"
16 #include "aom_dsp/arm/transpose_neon.h"
17 #include "av1/common/resize.h"
18 #include "config/av1_rtcd.h"
19 #include "config/aom_scale_rtcd.h"
20 
convolve8_4(const int16x4_t s0,const int16x4_t s1,const int16x4_t s2,const int16x4_t s3,const int16x4_t s4,const int16x4_t s5,const int16x4_t s6,const int16x4_t s7,const int16x8_t filter)21 static INLINE int16x4_t convolve8_4(const int16x4_t s0, const int16x4_t s1,
22                                     const int16x4_t s2, const int16x4_t s3,
23                                     const int16x4_t s4, const int16x4_t s5,
24                                     const int16x4_t s6, const int16x4_t s7,
25                                     const int16x8_t filter) {
26   const int16x4_t filter_lo = vget_low_s16(filter);
27   const int16x4_t filter_hi = vget_high_s16(filter);
28 
29   int16x4_t sum = vmul_lane_s16(s0, filter_lo, 0);
30   sum = vmla_lane_s16(sum, s1, filter_lo, 1);
31   sum = vmla_lane_s16(sum, s2, filter_lo, 2);
32   sum = vmla_lane_s16(sum, s5, filter_hi, 1);
33   sum = vmla_lane_s16(sum, s6, filter_hi, 2);
34   sum = vmla_lane_s16(sum, s7, filter_hi, 3);
35   sum = vqadd_s16(sum, vmul_lane_s16(s3, filter_lo, 3));
36   sum = vqadd_s16(sum, vmul_lane_s16(s4, filter_hi, 0));
37   return sum;
38 }
39 
convolve8_8(const int16x8_t s0,const int16x8_t s1,const int16x8_t s2,const int16x8_t s3,const int16x8_t s4,const int16x8_t s5,const int16x8_t s6,const int16x8_t s7,const int16x8_t filter)40 static INLINE uint8x8_t convolve8_8(const int16x8_t s0, const int16x8_t s1,
41                                     const int16x8_t s2, const int16x8_t s3,
42                                     const int16x8_t s4, const int16x8_t s5,
43                                     const int16x8_t s6, const int16x8_t s7,
44                                     const int16x8_t filter) {
45   const int16x4_t filter_lo = vget_low_s16(filter);
46   const int16x4_t filter_hi = vget_high_s16(filter);
47 
48   int16x8_t sum = vmulq_lane_s16(s0, filter_lo, 0);
49   sum = vmlaq_lane_s16(sum, s1, filter_lo, 1);
50   sum = vmlaq_lane_s16(sum, s2, filter_lo, 2);
51   sum = vmlaq_lane_s16(sum, s5, filter_hi, 1);
52   sum = vmlaq_lane_s16(sum, s6, filter_hi, 2);
53   sum = vmlaq_lane_s16(sum, s7, filter_hi, 3);
54   sum = vqaddq_s16(sum, vmulq_lane_s16(s3, filter_lo, 3));
55   sum = vqaddq_s16(sum, vmulq_lane_s16(s4, filter_hi, 0));
56   return vqrshrun_n_s16(sum, 7);
57 }
58 
scale_filter_8(const uint8x8_t * const s,const int16x8_t filter)59 static INLINE uint8x8_t scale_filter_8(const uint8x8_t *const s,
60                                        const int16x8_t filter) {
61   int16x8_t ss0 = vreinterpretq_s16_u16(vmovl_u8(s[0]));
62   int16x8_t ss1 = vreinterpretq_s16_u16(vmovl_u8(s[1]));
63   int16x8_t ss2 = vreinterpretq_s16_u16(vmovl_u8(s[2]));
64   int16x8_t ss3 = vreinterpretq_s16_u16(vmovl_u8(s[3]));
65   int16x8_t ss4 = vreinterpretq_s16_u16(vmovl_u8(s[4]));
66   int16x8_t ss5 = vreinterpretq_s16_u16(vmovl_u8(s[5]));
67   int16x8_t ss6 = vreinterpretq_s16_u16(vmovl_u8(s[6]));
68   int16x8_t ss7 = vreinterpretq_s16_u16(vmovl_u8(s[7]));
69 
70   return convolve8_8(ss0, ss1, ss2, ss3, ss4, ss5, ss6, ss7, filter);
71 }
72 
scale_plane_2_to_1_phase_0(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h)73 static INLINE void scale_plane_2_to_1_phase_0(const uint8_t *src,
74                                               const int src_stride,
75                                               uint8_t *dst,
76                                               const int dst_stride, const int w,
77                                               const int h) {
78   const int max_width = (w + 15) & ~15;
79   int y = h;
80 
81   assert(w && h);
82 
83   do {
84     int x = max_width;
85     do {
86       const uint8x16x2_t s = vld2q_u8(src);
87       vst1q_u8(dst, s.val[0]);
88       src += 32;
89       dst += 16;
90       x -= 16;
91     } while (x);
92     src += 2 * (src_stride - max_width);
93     dst += dst_stride - max_width;
94   } while (--y);
95 }
96 
scale_plane_4_to_1_phase_0(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h)97 static INLINE void scale_plane_4_to_1_phase_0(const uint8_t *src,
98                                               const int src_stride,
99                                               uint8_t *dst,
100                                               const int dst_stride, const int w,
101                                               const int h) {
102   const int max_width = (w + 15) & ~15;
103   int y = h;
104 
105   assert(w && h);
106 
107   do {
108     int x = max_width;
109     do {
110       const uint8x16x4_t s = vld4q_u8(src);
111       vst1q_u8(dst, s.val[0]);
112       src += 64;
113       dst += 16;
114       x -= 16;
115     } while (x);
116     src += 4 * (src_stride - max_width);
117     dst += dst_stride - max_width;
118   } while (--y);
119 }
120 
scale_plane_bilinear_kernel(const uint8x16_t in0,const uint8x16_t in1,const uint8x16_t in2,const uint8x16_t in3,const uint8x8_t coef0,const uint8x8_t coef1,uint8_t * const dst)121 static INLINE void scale_plane_bilinear_kernel(
122     const uint8x16_t in0, const uint8x16_t in1, const uint8x16_t in2,
123     const uint8x16_t in3, const uint8x8_t coef0, const uint8x8_t coef1,
124     uint8_t *const dst) {
125   const uint16x8_t h0 = vmull_u8(vget_low_u8(in0), coef0);
126   const uint16x8_t h1 = vmull_u8(vget_high_u8(in0), coef0);
127   const uint16x8_t h2 = vmull_u8(vget_low_u8(in2), coef0);
128   const uint16x8_t h3 = vmull_u8(vget_high_u8(in2), coef0);
129   const uint16x8_t h4 = vmlal_u8(h0, vget_low_u8(in1), coef1);
130   const uint16x8_t h5 = vmlal_u8(h1, vget_high_u8(in1), coef1);
131   const uint16x8_t h6 = vmlal_u8(h2, vget_low_u8(in3), coef1);
132   const uint16x8_t h7 = vmlal_u8(h3, vget_high_u8(in3), coef1);
133 
134   const uint8x8_t hor0 = vrshrn_n_u16(h4, 7);  // temp: 00 01 02 03 04 05 06 07
135   const uint8x8_t hor1 = vrshrn_n_u16(h5, 7);  // temp: 08 09 0A 0B 0C 0D 0E 0F
136   const uint8x8_t hor2 = vrshrn_n_u16(h6, 7);  // temp: 10 11 12 13 14 15 16 17
137   const uint8x8_t hor3 = vrshrn_n_u16(h7, 7);  // temp: 18 19 1A 1B 1C 1D 1E 1F
138   const uint16x8_t v0 = vmull_u8(hor0, coef0);
139   const uint16x8_t v1 = vmull_u8(hor1, coef0);
140   const uint16x8_t v2 = vmlal_u8(v0, hor2, coef1);
141   const uint16x8_t v3 = vmlal_u8(v1, hor3, coef1);
142   // dst: 0 1 2 3 4 5 6 7  8 9 A B C D E F
143   const uint8x16_t d = vcombine_u8(vrshrn_n_u16(v2, 7), vrshrn_n_u16(v3, 7));
144   vst1q_u8(dst, d);
145 }
146 
scale_plane_2_to_1_bilinear(const uint8_t * const src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const int16_t c0,const int16_t c1)147 static INLINE void scale_plane_2_to_1_bilinear(
148     const uint8_t *const src, const int src_stride, uint8_t *dst,
149     const int dst_stride, const int w, const int h, const int16_t c0,
150     const int16_t c1) {
151   const int max_width = (w + 15) & ~15;
152   const uint8_t *src0 = src;
153   const uint8_t *src1 = src + src_stride;
154   const uint8x8_t coef0 = vdup_n_u8(c0);
155   const uint8x8_t coef1 = vdup_n_u8(c1);
156   int y = h;
157 
158   assert(w && h);
159 
160   do {
161     int x = max_width;
162     do {
163       // 000 002 004 006 008 00A 00C 00E  010 012 014 016 018 01A 01C 01E
164       // 001 003 005 007 009 00B 00D 00F  011 013 015 017 019 01B 01D 01F
165       const uint8x16x2_t s0 = vld2q_u8(src0);
166       // 100 102 104 106 108 10A 10C 10E  110 112 114 116 118 11A 11C 11E
167       // 101 103 105 107 109 10B 10D 10F  111 113 115 117 119 11B 11D 11F
168       const uint8x16x2_t s1 = vld2q_u8(src1);
169       scale_plane_bilinear_kernel(s0.val[0], s0.val[1], s1.val[0], s1.val[1],
170                                   coef0, coef1, dst);
171       src0 += 32;
172       src1 += 32;
173       dst += 16;
174       x -= 16;
175     } while (x);
176     src0 += 2 * (src_stride - max_width);
177     src1 += 2 * (src_stride - max_width);
178     dst += dst_stride - max_width;
179   } while (--y);
180 }
181 
scale_plane_4_to_1_bilinear(const uint8_t * const src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const int16_t c0,const int16_t c1)182 static INLINE void scale_plane_4_to_1_bilinear(
183     const uint8_t *const src, const int src_stride, uint8_t *dst,
184     const int dst_stride, const int w, const int h, const int16_t c0,
185     const int16_t c1) {
186   const int max_width = (w + 15) & ~15;
187   const uint8_t *src0 = src;
188   const uint8_t *src1 = src + src_stride;
189   const uint8x8_t coef0 = vdup_n_u8(c0);
190   const uint8x8_t coef1 = vdup_n_u8(c1);
191   int y = h;
192 
193   assert(w && h);
194 
195   do {
196     int x = max_width;
197     do {
198       // (*) -- useless
199       // 000 004 008 00C 010 014 018 01C  020 024 028 02C 030 034 038 03C
200       // 001 005 009 00D 011 015 019 01D  021 025 029 02D 031 035 039 03D
201       // 002 006 00A 00E 012 016 01A 01E  022 026 02A 02E 032 036 03A 03E (*)
202       // 003 007 00B 00F 013 017 01B 01F  023 027 02B 02F 033 037 03B 03F (*)
203       const uint8x16x4_t s0 = vld4q_u8(src0);
204       // 100 104 108 10C 110 114 118 11C  120 124 128 12C 130 134 138 13C
205       // 101 105 109 10D 111 115 119 11D  121 125 129 12D 131 135 139 13D
206       // 102 106 10A 10E 112 116 11A 11E  122 126 12A 12E 132 136 13A 13E (*)
207       // 103 107 10B 10F 113 117 11B 11F  123 127 12B 12F 133 137 13B 13F (*)
208       const uint8x16x4_t s1 = vld4q_u8(src1);
209       scale_plane_bilinear_kernel(s0.val[0], s0.val[1], s1.val[0], s1.val[1],
210                                   coef0, coef1, dst);
211       src0 += 64;
212       src1 += 64;
213       dst += 16;
214       x -= 16;
215     } while (x);
216     src0 += 4 * (src_stride - max_width);
217     src1 += 4 * (src_stride - max_width);
218     dst += dst_stride - max_width;
219   } while (--y);
220 }
221 
scale_plane_2_to_1_general(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const int16_t * const coef,uint8_t * const temp_buffer)222 static void scale_plane_2_to_1_general(const uint8_t *src, const int src_stride,
223                                        uint8_t *dst, const int dst_stride,
224                                        const int w, const int h,
225                                        const int16_t *const coef,
226                                        uint8_t *const temp_buffer) {
227   const int width_hor = (w + 3) & ~3;
228   const int width_ver = (w + 7) & ~7;
229   const int height_hor = (2 * h + SUBPEL_TAPS - 2 + 7) & ~7;
230   const int height_ver = (h + 3) & ~3;
231   const int16x8_t filters = vld1q_s16(coef);
232   int x, y = height_hor;
233   uint8_t *t = temp_buffer;
234   uint8x8_t s[14], d[4];
235 
236   assert(w && h);
237 
238   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 1;
239 
240   // horizontal 4x8
241   // Note: processing 4x8 is about 20% faster than processing row by row using
242   // vld4_u8().
243   do {
244     load_u8_8x8(src + 2, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
245                 &s[6], &s[7]);
246     transpose_elems_inplace_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
247                                    &s[6], &s[7]);
248     x = width_hor;
249 
250     do {
251       src += 8;
252       load_u8_8x8(src, src_stride, &s[6], &s[7], &s[8], &s[9], &s[10], &s[11],
253                   &s[12], &s[13]);
254       transpose_elems_inplace_u8_8x8(&s[6], &s[7], &s[8], &s[9], &s[10], &s[11],
255                                      &s[12], &s[13]);
256 
257       d[0] = scale_filter_8(&s[0], filters);  // 00 10 20 30 40 50 60 70
258       d[1] = scale_filter_8(&s[2], filters);  // 01 11 21 31 41 51 61 71
259       d[2] = scale_filter_8(&s[4], filters);  // 02 12 22 32 42 52 62 72
260       d[3] = scale_filter_8(&s[6], filters);  // 03 13 23 33 43 53 63 73
261       // 00 01 02 03 40 41 42 43
262       // 10 11 12 13 50 51 52 53
263       // 20 21 22 23 60 61 62 63
264       // 30 31 32 33 70 71 72 73
265       transpose_elems_inplace_u8_8x4(&d[0], &d[1], &d[2], &d[3]);
266       vst1_lane_u32((uint32_t *)(t + 0 * width_hor), vreinterpret_u32_u8(d[0]),
267                     0);
268       vst1_lane_u32((uint32_t *)(t + 1 * width_hor), vreinterpret_u32_u8(d[1]),
269                     0);
270       vst1_lane_u32((uint32_t *)(t + 2 * width_hor), vreinterpret_u32_u8(d[2]),
271                     0);
272       vst1_lane_u32((uint32_t *)(t + 3 * width_hor), vreinterpret_u32_u8(d[3]),
273                     0);
274       vst1_lane_u32((uint32_t *)(t + 4 * width_hor), vreinterpret_u32_u8(d[0]),
275                     1);
276       vst1_lane_u32((uint32_t *)(t + 5 * width_hor), vreinterpret_u32_u8(d[1]),
277                     1);
278       vst1_lane_u32((uint32_t *)(t + 6 * width_hor), vreinterpret_u32_u8(d[2]),
279                     1);
280       vst1_lane_u32((uint32_t *)(t + 7 * width_hor), vreinterpret_u32_u8(d[3]),
281                     1);
282 
283       s[0] = s[8];
284       s[1] = s[9];
285       s[2] = s[10];
286       s[3] = s[11];
287       s[4] = s[12];
288       s[5] = s[13];
289 
290       t += 4;
291       x -= 4;
292     } while (x);
293     src += 8 * src_stride - 2 * width_hor;
294     t += 7 * width_hor;
295     y -= 8;
296   } while (y);
297 
298   // vertical 8x4
299   x = width_ver;
300   t = temp_buffer;
301   do {
302     load_u8_8x8(t, width_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
303                 &s[7]);
304     t += 6 * width_hor;
305     y = height_ver;
306 
307     do {
308       load_u8_8x8(t, width_hor, &s[6], &s[7], &s[8], &s[9], &s[10], &s[11],
309                   &s[12], &s[13]);
310       t += 8 * width_hor;
311 
312       d[0] = scale_filter_8(&s[0], filters);  // 00 01 02 03 04 05 06 07
313       d[1] = scale_filter_8(&s[2], filters);  // 10 11 12 13 14 15 16 17
314       d[2] = scale_filter_8(&s[4], filters);  // 20 21 22 23 24 25 26 27
315       d[3] = scale_filter_8(&s[6], filters);  // 30 31 32 33 34 35 36 37
316       vst1_u8(dst + 0 * dst_stride, d[0]);
317       vst1_u8(dst + 1 * dst_stride, d[1]);
318       vst1_u8(dst + 2 * dst_stride, d[2]);
319       vst1_u8(dst + 3 * dst_stride, d[3]);
320 
321       s[0] = s[8];
322       s[1] = s[9];
323       s[2] = s[10];
324       s[3] = s[11];
325       s[4] = s[12];
326       s[5] = s[13];
327 
328       dst += 4 * dst_stride;
329       y -= 4;
330     } while (y);
331     t -= width_hor * (2 * height_ver + 6);
332     t += 8;
333     dst -= height_ver * dst_stride;
334     dst += 8;
335     x -= 8;
336   } while (x);
337 }
338 
scale_plane_4_to_1_general(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const int16_t * const coef,uint8_t * const temp_buffer)339 static void scale_plane_4_to_1_general(const uint8_t *src, const int src_stride,
340                                        uint8_t *dst, const int dst_stride,
341                                        const int w, const int h,
342                                        const int16_t *const coef,
343                                        uint8_t *const temp_buffer) {
344   const int width_hor = (w + 1) & ~1;
345   const int width_ver = (w + 7) & ~7;
346   const int height_hor = (4 * h + SUBPEL_TAPS - 2 + 7) & ~7;
347   const int height_ver = (h + 1) & ~1;
348   const int16x8_t filters = vld1q_s16(coef);
349   int x, y = height_hor;
350   uint8_t *t = temp_buffer;
351   uint8x8_t s[12], d[2];
352 
353   assert(w && h);
354 
355   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 3;
356 
357   // horizontal 2x8
358   // Note: processing 2x8 is about 20% faster than processing row by row using
359   // vld4_u8().
360   do {
361     load_u8_8x8(src + 4, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
362                 &s[6], &s[7]);
363     transpose_elems_u8_4x8(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
364                            &s[0], &s[1], &s[2], &s[3]);
365     x = width_hor;
366 
367     do {
368       uint8x8x2_t dd;
369       src += 8;
370       load_u8_8x8(src, src_stride, &s[4], &s[5], &s[6], &s[7], &s[8], &s[9],
371                   &s[10], &s[11]);
372       transpose_elems_inplace_u8_8x8(&s[4], &s[5], &s[6], &s[7], &s[8], &s[9],
373                                      &s[10], &s[11]);
374 
375       d[0] = scale_filter_8(&s[0], filters);  // 00 10 20 30 40 50 60 70
376       d[1] = scale_filter_8(&s[4], filters);  // 01 11 21 31 41 51 61 71
377       // dd.val[0]: 00 01 20 21 40 41 60 61
378       // dd.val[1]: 10 11 30 31 50 51 70 71
379       dd = vtrn_u8(d[0], d[1]);
380       vst1_lane_u16((uint16_t *)(t + 0 * width_hor),
381                     vreinterpret_u16_u8(dd.val[0]), 0);
382       vst1_lane_u16((uint16_t *)(t + 1 * width_hor),
383                     vreinterpret_u16_u8(dd.val[1]), 0);
384       vst1_lane_u16((uint16_t *)(t + 2 * width_hor),
385                     vreinterpret_u16_u8(dd.val[0]), 1);
386       vst1_lane_u16((uint16_t *)(t + 3 * width_hor),
387                     vreinterpret_u16_u8(dd.val[1]), 1);
388       vst1_lane_u16((uint16_t *)(t + 4 * width_hor),
389                     vreinterpret_u16_u8(dd.val[0]), 2);
390       vst1_lane_u16((uint16_t *)(t + 5 * width_hor),
391                     vreinterpret_u16_u8(dd.val[1]), 2);
392       vst1_lane_u16((uint16_t *)(t + 6 * width_hor),
393                     vreinterpret_u16_u8(dd.val[0]), 3);
394       vst1_lane_u16((uint16_t *)(t + 7 * width_hor),
395                     vreinterpret_u16_u8(dd.val[1]), 3);
396 
397       s[0] = s[8];
398       s[1] = s[9];
399       s[2] = s[10];
400       s[3] = s[11];
401 
402       t += 2;
403       x -= 2;
404     } while (x);
405     src += 8 * src_stride - 4 * width_hor;
406     t += 7 * width_hor;
407     y -= 8;
408   } while (y);
409 
410   // vertical 8x2
411   x = width_ver;
412   t = temp_buffer;
413   do {
414     load_u8_8x4(t, width_hor, &s[0], &s[1], &s[2], &s[3]);
415     t += 4 * width_hor;
416     y = height_ver;
417 
418     do {
419       load_u8_8x8(t, width_hor, &s[4], &s[5], &s[6], &s[7], &s[8], &s[9],
420                   &s[10], &s[11]);
421       t += 8 * width_hor;
422 
423       d[0] = scale_filter_8(&s[0], filters);  // 00 01 02 03 04 05 06 07
424       d[1] = scale_filter_8(&s[4], filters);  // 10 11 12 13 14 15 16 17
425       vst1_u8(dst + 0 * dst_stride, d[0]);
426       vst1_u8(dst + 1 * dst_stride, d[1]);
427 
428       s[0] = s[8];
429       s[1] = s[9];
430       s[2] = s[10];
431       s[3] = s[11];
432 
433       dst += 2 * dst_stride;
434       y -= 2;
435     } while (y);
436     t -= width_hor * (4 * height_ver + 4);
437     t += 8;
438     dst -= height_ver * dst_stride;
439     dst += 8;
440     x -= 8;
441   } while (x);
442 }
443 
scale_filter_bilinear(const uint8x8_t * const s,const uint8x8_t * const coef)444 static INLINE uint8x8_t scale_filter_bilinear(const uint8x8_t *const s,
445                                               const uint8x8_t *const coef) {
446   const uint16x8_t h0 = vmull_u8(s[0], coef[0]);
447   const uint16x8_t h1 = vmlal_u8(h0, s[1], coef[1]);
448 
449   return vrshrn_n_u16(h1, 7);
450 }
451 
452 // Notes for 4 to 3 scaling:
453 //
454 // 1. 6 rows are calculated in each horizontal inner loop, so width_hor must be
455 // multiple of 6, and no less than w.
456 //
457 // 2. 8 rows are calculated in each vertical inner loop, so width_ver must be
458 // multiple of 8, and no less than w.
459 //
460 // 3. 8 columns are calculated in each horizontal inner loop for further
461 // vertical scaling, so height_hor must be multiple of 8, and no less than
462 // 4 * h / 3.
463 //
464 // 4. 6 columns are calculated in each vertical inner loop, so height_ver must
465 // be multiple of 6, and no less than h.
466 //
467 // 5. The physical location of the last row of the 4 to 3 scaled frame is
468 // decided by phase_scaler, and are always less than 1 pixel below the last row
469 // of the original image.
scale_plane_4_to_3_bilinear(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const int phase_scaler,uint8_t * const temp_buffer)470 static void scale_plane_4_to_3_bilinear(const uint8_t *src,
471                                         const int src_stride, uint8_t *dst,
472                                         const int dst_stride, const int w,
473                                         const int h, const int phase_scaler,
474                                         uint8_t *const temp_buffer) {
475   static const int step_q4 = 16 * 4 / 3;
476   const int width_hor = (w + 5) - ((w + 5) % 6);
477   const int stride_hor = width_hor + 2;  // store 2 extra pixels
478   const int width_ver = (w + 7) & ~7;
479   // We only need 1 extra row below because there are only 2 bilinear
480   // coefficients.
481   const int height_hor = (4 * h / 3 + 1 + 7) & ~7;
482   const int height_ver = (h + 5) - ((h + 5) % 6);
483   int x, y = height_hor;
484   uint8_t *t = temp_buffer;
485   uint8x8_t s[9], d[8], c[6];
486   const InterpKernel *interp_kernel =
487       (const InterpKernel *)av1_interp_filter_params_list[BILINEAR].filter_ptr;
488   assert(w && h);
489 
490   c[0] = vdup_n_u8((uint8_t)interp_kernel[phase_scaler][3]);
491   c[1] = vdup_n_u8((uint8_t)interp_kernel[phase_scaler][4]);
492   c[2] = vdup_n_u8(
493       (uint8_t)interp_kernel[(phase_scaler + 1 * step_q4) & SUBPEL_MASK][3]);
494   c[3] = vdup_n_u8(
495       (uint8_t)interp_kernel[(phase_scaler + 1 * step_q4) & SUBPEL_MASK][4]);
496   c[4] = vdup_n_u8(
497       (uint8_t)interp_kernel[(phase_scaler + 2 * step_q4) & SUBPEL_MASK][3]);
498   c[5] = vdup_n_u8(
499       (uint8_t)interp_kernel[(phase_scaler + 2 * step_q4) & SUBPEL_MASK][4]);
500 
501   d[6] = vdup_n_u8(0);
502   d[7] = vdup_n_u8(0);
503 
504   // horizontal 6x8
505   do {
506     load_u8_8x8(src, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
507                 &s[6], &s[7]);
508     src += 1;
509     transpose_elems_inplace_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
510                                    &s[6], &s[7]);
511     x = width_hor;
512 
513     do {
514       load_u8_8x8(src, src_stride, &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
515                   &s[7], &s[8]);
516       src += 8;
517       transpose_elems_inplace_u8_8x8(&s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
518                                      &s[7], &s[8]);
519 
520       // 00 10 20 30 40 50 60 70
521       // 01 11 21 31 41 51 61 71
522       // 02 12 22 32 42 52 62 72
523       // 03 13 23 33 43 53 63 73
524       // 04 14 24 34 44 54 64 74
525       // 05 15 25 35 45 55 65 75
526       d[0] = scale_filter_bilinear(&s[0], &c[0]);
527       d[1] =
528           scale_filter_bilinear(&s[(phase_scaler + 1 * step_q4) >> 4], &c[2]);
529       d[2] =
530           scale_filter_bilinear(&s[(phase_scaler + 2 * step_q4) >> 4], &c[4]);
531       d[3] = scale_filter_bilinear(&s[4], &c[0]);
532       d[4] = scale_filter_bilinear(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)],
533                                    &c[2]);
534       d[5] = scale_filter_bilinear(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)],
535                                    &c[4]);
536 
537       // 00 01 02 03 04 05 xx xx
538       // 10 11 12 13 14 15 xx xx
539       // 20 21 22 23 24 25 xx xx
540       // 30 31 32 33 34 35 xx xx
541       // 40 41 42 43 44 45 xx xx
542       // 50 51 52 53 54 55 xx xx
543       // 60 61 62 63 64 65 xx xx
544       // 70 71 72 73 74 75 xx xx
545       transpose_elems_inplace_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5],
546                                      &d[6], &d[7]);
547       // store 2 extra pixels
548       vst1_u8(t + 0 * stride_hor, d[0]);
549       vst1_u8(t + 1 * stride_hor, d[1]);
550       vst1_u8(t + 2 * stride_hor, d[2]);
551       vst1_u8(t + 3 * stride_hor, d[3]);
552       vst1_u8(t + 4 * stride_hor, d[4]);
553       vst1_u8(t + 5 * stride_hor, d[5]);
554       vst1_u8(t + 6 * stride_hor, d[6]);
555       vst1_u8(t + 7 * stride_hor, d[7]);
556 
557       s[0] = s[8];
558 
559       t += 6;
560       x -= 6;
561     } while (x);
562     src += 8 * src_stride - 4 * width_hor / 3 - 1;
563     t += 7 * stride_hor + 2;
564     y -= 8;
565   } while (y);
566 
567   // vertical 8x6
568   x = width_ver;
569   t = temp_buffer;
570   do {
571     load_u8_8x8(t, stride_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
572                 &s[7]);
573     t += stride_hor;
574     y = height_ver;
575 
576     do {
577       load_u8_8x8(t, stride_hor, &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
578                   &s[7], &s[8]);
579       t += 8 * stride_hor;
580 
581       d[0] = scale_filter_bilinear(&s[0], &c[0]);
582       d[1] =
583           scale_filter_bilinear(&s[(phase_scaler + 1 * step_q4) >> 4], &c[2]);
584       d[2] =
585           scale_filter_bilinear(&s[(phase_scaler + 2 * step_q4) >> 4], &c[4]);
586       d[3] = scale_filter_bilinear(&s[4], &c[0]);
587       d[4] = scale_filter_bilinear(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)],
588                                    &c[2]);
589       d[5] = scale_filter_bilinear(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)],
590                                    &c[4]);
591       vst1_u8(dst + 0 * dst_stride, d[0]);
592       vst1_u8(dst + 1 * dst_stride, d[1]);
593       vst1_u8(dst + 2 * dst_stride, d[2]);
594       vst1_u8(dst + 3 * dst_stride, d[3]);
595       vst1_u8(dst + 4 * dst_stride, d[4]);
596       vst1_u8(dst + 5 * dst_stride, d[5]);
597 
598       s[0] = s[8];
599 
600       dst += 6 * dst_stride;
601       y -= 6;
602     } while (y);
603     t -= stride_hor * (4 * height_ver / 3 + 1);
604     t += 8;
605     dst -= height_ver * dst_stride;
606     dst += 8;
607     x -= 8;
608   } while (x);
609 }
610 
scale_plane_4_to_3_general(const uint8_t * src,const int src_stride,uint8_t * dst,const int dst_stride,const int w,const int h,const InterpKernel * const coef,const int phase_scaler,uint8_t * const temp_buffer)611 static void scale_plane_4_to_3_general(const uint8_t *src, const int src_stride,
612                                        uint8_t *dst, const int dst_stride,
613                                        const int w, const int h,
614                                        const InterpKernel *const coef,
615                                        const int phase_scaler,
616                                        uint8_t *const temp_buffer) {
617   static const int step_q4 = 16 * 4 / 3;
618   const int width_hor = (w + 5) - ((w + 5) % 6);
619   const int stride_hor = width_hor + 2;  // store 2 extra pixels
620   const int width_ver = (w + 7) & ~7;
621   // We need (SUBPEL_TAPS - 1) extra rows: (SUBPEL_TAPS / 2 - 1) extra rows
622   // above and (SUBPEL_TAPS / 2) extra rows below.
623   const int height_hor = (4 * h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
624   const int height_ver = (h + 5) - ((h + 5) % 6);
625   const int16x8_t filters0 = vld1q_s16(
626       (const int16_t *)&coef[(phase_scaler + 0 * step_q4) & SUBPEL_MASK]);
627   const int16x8_t filters1 = vld1q_s16(
628       (const int16_t *)&coef[(phase_scaler + 1 * step_q4) & SUBPEL_MASK]);
629   const int16x8_t filters2 = vld1q_s16(
630       (const int16_t *)&coef[(phase_scaler + 2 * step_q4) & SUBPEL_MASK]);
631   int x, y = height_hor;
632   uint8_t *t = temp_buffer;
633   uint8x8_t s[15], d[8];
634 
635   assert(w && h);
636 
637   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2;
638   d[6] = vdup_n_u8(0);
639   d[7] = vdup_n_u8(0);
640 
641   // horizontal 6x8
642   do {
643     load_u8_8x8(src + 1, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
644                 &s[6], &s[7]);
645     transpose_elems_inplace_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
646                                    &s[6], &s[7]);
647     x = width_hor;
648 
649     do {
650       src += 8;
651       load_u8_8x8(src, src_stride, &s[7], &s[8], &s[9], &s[10], &s[11], &s[12],
652                   &s[13], &s[14]);
653       transpose_elems_inplace_u8_8x8(&s[7], &s[8], &s[9], &s[10], &s[11],
654                                      &s[12], &s[13], &s[14]);
655 
656       // 00 10 20 30 40 50 60 70
657       // 01 11 21 31 41 51 61 71
658       // 02 12 22 32 42 52 62 72
659       // 03 13 23 33 43 53 63 73
660       // 04 14 24 34 44 54 64 74
661       // 05 15 25 35 45 55 65 75
662       d[0] = scale_filter_8(&s[0], filters0);
663       d[1] = scale_filter_8(&s[(phase_scaler + 1 * step_q4) >> 4], filters1);
664       d[2] = scale_filter_8(&s[(phase_scaler + 2 * step_q4) >> 4], filters2);
665       d[3] = scale_filter_8(&s[4], filters0);
666       d[4] =
667           scale_filter_8(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)], filters1);
668       d[5] =
669           scale_filter_8(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)], filters2);
670 
671       // 00 01 02 03 04 05 xx xx
672       // 10 11 12 13 14 15 xx xx
673       // 20 21 22 23 24 25 xx xx
674       // 30 31 32 33 34 35 xx xx
675       // 40 41 42 43 44 45 xx xx
676       // 50 51 52 53 54 55 xx xx
677       // 60 61 62 63 64 65 xx xx
678       // 70 71 72 73 74 75 xx xx
679       transpose_elems_inplace_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5],
680                                      &d[6], &d[7]);
681       // store 2 extra pixels
682       vst1_u8(t + 0 * stride_hor, d[0]);
683       vst1_u8(t + 1 * stride_hor, d[1]);
684       vst1_u8(t + 2 * stride_hor, d[2]);
685       vst1_u8(t + 3 * stride_hor, d[3]);
686       vst1_u8(t + 4 * stride_hor, d[4]);
687       vst1_u8(t + 5 * stride_hor, d[5]);
688       vst1_u8(t + 6 * stride_hor, d[6]);
689       vst1_u8(t + 7 * stride_hor, d[7]);
690 
691       s[0] = s[8];
692       s[1] = s[9];
693       s[2] = s[10];
694       s[3] = s[11];
695       s[4] = s[12];
696       s[5] = s[13];
697       s[6] = s[14];
698 
699       t += 6;
700       x -= 6;
701     } while (x);
702     src += 8 * src_stride - 4 * width_hor / 3;
703     t += 7 * stride_hor + 2;
704     y -= 8;
705   } while (y);
706 
707   // vertical 8x6
708   x = width_ver;
709   t = temp_buffer;
710   do {
711     load_u8_8x8(t, stride_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
712                 &s[7]);
713     t += 7 * stride_hor;
714     y = height_ver;
715 
716     do {
717       load_u8_8x8(t, stride_hor, &s[7], &s[8], &s[9], &s[10], &s[11], &s[12],
718                   &s[13], &s[14]);
719       t += 8 * stride_hor;
720 
721       d[0] = scale_filter_8(&s[0], filters0);
722       d[1] = scale_filter_8(&s[(phase_scaler + 1 * step_q4) >> 4], filters1);
723       d[2] = scale_filter_8(&s[(phase_scaler + 2 * step_q4) >> 4], filters2);
724       d[3] = scale_filter_8(&s[4], filters0);
725       d[4] =
726           scale_filter_8(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)], filters1);
727       d[5] =
728           scale_filter_8(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)], filters2);
729       vst1_u8(dst + 0 * dst_stride, d[0]);
730       vst1_u8(dst + 1 * dst_stride, d[1]);
731       vst1_u8(dst + 2 * dst_stride, d[2]);
732       vst1_u8(dst + 3 * dst_stride, d[3]);
733       vst1_u8(dst + 4 * dst_stride, d[4]);
734       vst1_u8(dst + 5 * dst_stride, d[5]);
735 
736       s[0] = s[8];
737       s[1] = s[9];
738       s[2] = s[10];
739       s[3] = s[11];
740       s[4] = s[12];
741       s[5] = s[13];
742       s[6] = s[14];
743 
744       dst += 6 * dst_stride;
745       y -= 6;
746     } while (y);
747     t -= stride_hor * (4 * height_ver / 3 + 7);
748     t += 8;
749     dst -= height_ver * dst_stride;
750     dst += 8;
751     x -= 8;
752   } while (x);
753 }
754 
755 // There's SIMD optimizations for 1/4, 1/2 and 3/4 downscaling in NEON.
has_normative_scaler_neon(const int src_width,const int src_height,const int dst_width,const int dst_height)756 static INLINE bool has_normative_scaler_neon(const int src_width,
757                                              const int src_height,
758                                              const int dst_width,
759                                              const int dst_height) {
760   const bool has_normative_scaler =
761       (2 * dst_width == src_width && 2 * dst_height == src_height) ||
762       (4 * dst_width == src_width && 4 * dst_height == src_height) ||
763       (4 * dst_width == 3 * src_width && 4 * dst_height == 3 * src_height);
764 
765   return has_normative_scaler;
766 }
767 
av1_resize_and_extend_frame_neon(const YV12_BUFFER_CONFIG * src,YV12_BUFFER_CONFIG * dst,const InterpFilter filter,const int phase,const int num_planes)768 void av1_resize_and_extend_frame_neon(const YV12_BUFFER_CONFIG *src,
769                                       YV12_BUFFER_CONFIG *dst,
770                                       const InterpFilter filter,
771                                       const int phase, const int num_planes) {
772   bool has_normative_scaler =
773       has_normative_scaler_neon(src->y_crop_width, src->y_crop_height,
774                                 dst->y_crop_width, dst->y_crop_height);
775 
776   if (num_planes > 1) {
777     has_normative_scaler =
778         has_normative_scaler &&
779         has_normative_scaler_neon(src->uv_crop_width, src->uv_crop_height,
780                                   dst->uv_crop_width, dst->uv_crop_height);
781   }
782 
783   if (!has_normative_scaler) {
784     av1_resize_and_extend_frame_c(src, dst, filter, phase, num_planes);
785     return;
786   }
787 
788   // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
789   // the static analysis warnings.
790   int malloc_failed = 0;
791   for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) {
792     const int is_uv = i > 0;
793     const int src_w = src->crop_widths[is_uv];
794     const int src_h = src->crop_heights[is_uv];
795     const int dst_w = dst->crop_widths[is_uv];
796     const int dst_h = dst->crop_heights[is_uv];
797     const int dst_y_w = (dst->crop_widths[0] + 1) & ~1;
798     const int dst_y_h = (dst->crop_heights[0] + 1) & ~1;
799 
800     if (2 * dst_w == src_w && 2 * dst_h == src_h) {
801       if (phase == 0) {
802         scale_plane_2_to_1_phase_0(src->buffers[i], src->strides[is_uv],
803                                    dst->buffers[i], dst->strides[is_uv], dst_w,
804                                    dst_h);
805       } else if (filter == BILINEAR) {
806         const int16_t c0 = av1_bilinear_filters[phase][3];
807         const int16_t c1 = av1_bilinear_filters[phase][4];
808         scale_plane_2_to_1_bilinear(src->buffers[i], src->strides[is_uv],
809                                     dst->buffers[i], dst->strides[is_uv], dst_w,
810                                     dst_h, c0, c1);
811       } else {
812         const int buffer_stride = (dst_y_w + 3) & ~3;
813         const int buffer_height = (2 * dst_y_h + SUBPEL_TAPS - 2 + 7) & ~7;
814         uint8_t *const temp_buffer =
815             (uint8_t *)malloc(buffer_stride * buffer_height);
816         if (!temp_buffer) {
817           malloc_failed = 1;
818           break;
819         }
820         const InterpKernel *interp_kernel =
821             (const InterpKernel *)av1_interp_filter_params_list[filter]
822                 .filter_ptr;
823         scale_plane_2_to_1_general(src->buffers[i], src->strides[is_uv],
824                                    dst->buffers[i], dst->strides[is_uv], dst_w,
825                                    dst_h, interp_kernel[phase], temp_buffer);
826         free(temp_buffer);
827       }
828     } else if (4 * dst_w == src_w && 4 * dst_h == src_h) {
829       if (phase == 0) {
830         scale_plane_4_to_1_phase_0(src->buffers[i], src->strides[is_uv],
831                                    dst->buffers[i], dst->strides[is_uv], dst_w,
832                                    dst_h);
833       } else if (filter == BILINEAR) {
834         const int16_t c0 = av1_bilinear_filters[phase][3];
835         const int16_t c1 = av1_bilinear_filters[phase][4];
836         scale_plane_4_to_1_bilinear(src->buffers[i], src->strides[is_uv],
837                                     dst->buffers[i], dst->strides[is_uv], dst_w,
838                                     dst_h, c0, c1);
839       } else {
840         const int buffer_stride = (dst_y_w + 1) & ~1;
841         const int buffer_height = (4 * dst_y_h + SUBPEL_TAPS - 2 + 7) & ~7;
842         uint8_t *const temp_buffer =
843             (uint8_t *)malloc(buffer_stride * buffer_height);
844         if (!temp_buffer) {
845           malloc_failed = 1;
846           break;
847         }
848         const InterpKernel *interp_kernel =
849             (const InterpKernel *)av1_interp_filter_params_list[filter]
850                 .filter_ptr;
851         scale_plane_4_to_1_general(src->buffers[i], src->strides[is_uv],
852                                    dst->buffers[i], dst->strides[is_uv], dst_w,
853                                    dst_h, interp_kernel[phase], temp_buffer);
854         free(temp_buffer);
855       }
856     } else {
857       assert(4 * dst_w == 3 * src_w && 4 * dst_h == 3 * src_h);
858       // 4 to 3
859       const int buffer_stride = (dst_y_w + 5) - ((dst_y_w + 5) % 6) + 2;
860       const int buffer_height = (4 * dst_y_h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
861       uint8_t *const temp_buffer =
862           (uint8_t *)malloc(buffer_stride * buffer_height);
863       if (!temp_buffer) {
864         malloc_failed = 1;
865         break;
866       }
867       if (filter == BILINEAR) {
868         scale_plane_4_to_3_bilinear(src->buffers[i], src->strides[is_uv],
869                                     dst->buffers[i], dst->strides[is_uv], dst_w,
870                                     dst_h, phase, temp_buffer);
871       } else {
872         const InterpKernel *interp_kernel =
873             (const InterpKernel *)av1_interp_filter_params_list[filter]
874                 .filter_ptr;
875         scale_plane_4_to_3_general(src->buffers[i], src->strides[is_uv],
876                                    dst->buffers[i], dst->strides[is_uv], dst_w,
877                                    dst_h, interp_kernel, phase, temp_buffer);
878       }
879       free(temp_buffer);
880     }
881   }
882 
883   if (malloc_failed) {
884     av1_resize_and_extend_frame_c(src, dst, filter, phase, num_planes);
885   } else {
886     aom_extend_frame_borders(dst, num_planes);
887   }
888 }
889 
scaledconvolve_horiz_w4(const uint8_t * src,const ptrdiff_t src_stride,uint8_t * dst,const ptrdiff_t dst_stride,const InterpKernel * const x_filters,const int x0_q4,const int x_step_q4,const int w,const int h)890 static INLINE void scaledconvolve_horiz_w4(
891     const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
892     const ptrdiff_t dst_stride, const InterpKernel *const x_filters,
893     const int x0_q4, const int x_step_q4, const int w, const int h) {
894   DECLARE_ALIGNED(16, uint8_t, temp[4 * 4]);
895   int x, y, z;
896 
897   src -= SUBPEL_TAPS / 2 - 1;
898 
899   y = h;
900   do {
901     int x_q4 = x0_q4;
902     x = 0;
903     do {
904       // process 4 src_x steps
905       for (z = 0; z < 4; ++z) {
906         const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
907         if (x_q4 & SUBPEL_MASK) {
908           const int16x8_t filters = vld1q_s16(x_filters[x_q4 & SUBPEL_MASK]);
909           uint8x8_t s[8], d;
910           int16x8_t ss[4];
911           int16x4_t t[8], tt;
912 
913           load_u8_8x4(src_x, src_stride, &s[0], &s[1], &s[2], &s[3]);
914           transpose_elems_inplace_u8_8x4(&s[0], &s[1], &s[2], &s[3]);
915 
916           ss[0] = vreinterpretq_s16_u16(vmovl_u8(s[0]));
917           ss[1] = vreinterpretq_s16_u16(vmovl_u8(s[1]));
918           ss[2] = vreinterpretq_s16_u16(vmovl_u8(s[2]));
919           ss[3] = vreinterpretq_s16_u16(vmovl_u8(s[3]));
920           t[0] = vget_low_s16(ss[0]);
921           t[1] = vget_low_s16(ss[1]);
922           t[2] = vget_low_s16(ss[2]);
923           t[3] = vget_low_s16(ss[3]);
924           t[4] = vget_high_s16(ss[0]);
925           t[5] = vget_high_s16(ss[1]);
926           t[6] = vget_high_s16(ss[2]);
927           t[7] = vget_high_s16(ss[3]);
928 
929           tt = convolve8_4(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7],
930                            filters);
931           d = vqrshrun_n_s16(vcombine_s16(tt, tt), 7);
932           store_u8_4x1(&temp[4 * z], d);
933         } else {
934           int i;
935           for (i = 0; i < 4; ++i) {
936             temp[z * 4 + i] = src_x[i * src_stride + 3];
937           }
938         }
939         x_q4 += x_step_q4;
940       }
941 
942       // transpose the 4x4 filters values back to dst
943       {
944         const uint8x8x4_t d4 = vld4_u8(temp);
945         store_u8_4x1(&dst[x + 0 * dst_stride], d4.val[0]);
946         store_u8_4x1(&dst[x + 1 * dst_stride], d4.val[1]);
947         store_u8_4x1(&dst[x + 2 * dst_stride], d4.val[2]);
948         store_u8_4x1(&dst[x + 3 * dst_stride], d4.val[3]);
949       }
950       x += 4;
951     } while (x < w);
952 
953     src += src_stride * 4;
954     dst += dst_stride * 4;
955     y -= 4;
956   } while (y > 0);
957 }
958 
scaledconvolve_horiz_w8(const uint8_t * src,const ptrdiff_t src_stride,uint8_t * dst,const ptrdiff_t dst_stride,const InterpKernel * const x_filters,const int x0_q4,const int x_step_q4,const int w,const int h)959 static INLINE void scaledconvolve_horiz_w8(
960     const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
961     const ptrdiff_t dst_stride, const InterpKernel *const x_filters,
962     const int x0_q4, const int x_step_q4, const int w, const int h) {
963   DECLARE_ALIGNED(16, uint8_t, temp[8 * 8]);
964   int x, y, z;
965   src -= SUBPEL_TAPS / 2 - 1;
966 
967   // This function processes 8x8 areas. The intermediate height is not always
968   // a multiple of 8, so force it to be a multiple of 8 here.
969   y = (h + 7) & ~7;
970 
971   do {
972     int x_q4 = x0_q4;
973     x = 0;
974     do {
975       uint8x8_t d[8];
976       // process 8 src_x steps
977       for (z = 0; z < 8; ++z) {
978         const uint8_t *const src_x = &src[x_q4 >> SUBPEL_BITS];
979 
980         if (x_q4 & SUBPEL_MASK) {
981           const int16x8_t filters = vld1q_s16(x_filters[x_q4 & SUBPEL_MASK]);
982           uint8x8_t s[8];
983           load_u8_8x8(src_x, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4],
984                       &s[5], &s[6], &s[7]);
985           transpose_elems_inplace_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4],
986                                          &s[5], &s[6], &s[7]);
987           d[0] = scale_filter_8(s, filters);
988           vst1_u8(&temp[8 * z], d[0]);
989         } else {
990           int i;
991           for (i = 0; i < 8; ++i) {
992             temp[z * 8 + i] = src_x[i * src_stride + 3];
993           }
994         }
995         x_q4 += x_step_q4;
996       }
997 
998       // transpose the 8x8 filters values back to dst
999       load_u8_8x8(temp, 8, &d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6],
1000                   &d[7]);
1001       transpose_elems_inplace_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5],
1002                                      &d[6], &d[7]);
1003       store_u8_8x8(dst + x, dst_stride, d[0], d[1], d[2], d[3], d[4], d[5],
1004                    d[6], d[7]);
1005       x += 8;
1006     } while (x < w);
1007 
1008     src += src_stride * 8;
1009     dst += dst_stride * 8;
1010   } while (y -= 8);
1011 }
1012 
scaledconvolve_vert_w4(const uint8_t * src,const ptrdiff_t src_stride,uint8_t * dst,const ptrdiff_t dst_stride,const InterpKernel * const y_filters,const int y0_q4,const int y_step_q4,const int w,const int h)1013 static INLINE void scaledconvolve_vert_w4(
1014     const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
1015     const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
1016     const int y0_q4, const int y_step_q4, const int w, const int h) {
1017   int y;
1018   int y_q4 = y0_q4;
1019 
1020   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1021   y = h;
1022   do {
1023     const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1024 
1025     if (y_q4 & SUBPEL_MASK) {
1026       const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
1027       uint8x8_t s[8], d;
1028       int16x4_t t[8], tt;
1029 
1030       load_u8_8x8(src_y, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
1031                   &s[6], &s[7]);
1032       t[0] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[0])));
1033       t[1] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[1])));
1034       t[2] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[2])));
1035       t[3] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[3])));
1036       t[4] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[4])));
1037       t[5] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[5])));
1038       t[6] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[6])));
1039       t[7] = vget_low_s16(vreinterpretq_s16_u16(vmovl_u8(s[7])));
1040 
1041       tt = convolve8_4(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], filters);
1042       d = vqrshrun_n_s16(vcombine_s16(tt, tt), 7);
1043       store_u8_4x1(dst, d);
1044     } else {
1045       memcpy(dst, &src_y[3 * src_stride], w);
1046     }
1047 
1048     dst += dst_stride;
1049     y_q4 += y_step_q4;
1050   } while (--y);
1051 }
1052 
scaledconvolve_vert_w8(const uint8_t * src,const ptrdiff_t src_stride,uint8_t * dst,const ptrdiff_t dst_stride,const InterpKernel * const y_filters,const int y0_q4,const int y_step_q4,const int w,const int h)1053 static INLINE void scaledconvolve_vert_w8(
1054     const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
1055     const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
1056     const int y0_q4, const int y_step_q4, const int w, const int h) {
1057   int y;
1058   int y_q4 = y0_q4;
1059 
1060   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1061   y = h;
1062   do {
1063     const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1064     if (y_q4 & SUBPEL_MASK) {
1065       const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
1066       uint8x8_t s[8], d;
1067       load_u8_8x8(src_y, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
1068                   &s[6], &s[7]);
1069       d = scale_filter_8(s, filters);
1070       vst1_u8(dst, d);
1071     } else {
1072       memcpy(dst, &src_y[3 * src_stride], w);
1073     }
1074     dst += dst_stride;
1075     y_q4 += y_step_q4;
1076   } while (--y);
1077 }
1078 
scaledconvolve_vert_w16(const uint8_t * src,const ptrdiff_t src_stride,uint8_t * dst,const ptrdiff_t dst_stride,const InterpKernel * const y_filters,const int y0_q4,const int y_step_q4,const int w,const int h)1079 static INLINE void scaledconvolve_vert_w16(
1080     const uint8_t *src, const ptrdiff_t src_stride, uint8_t *dst,
1081     const ptrdiff_t dst_stride, const InterpKernel *const y_filters,
1082     const int y0_q4, const int y_step_q4, const int w, const int h) {
1083   int x, y;
1084   int y_q4 = y0_q4;
1085 
1086   src -= src_stride * (SUBPEL_TAPS / 2 - 1);
1087   y = h;
1088   do {
1089     const unsigned char *src_y = &src[(y_q4 >> SUBPEL_BITS) * src_stride];
1090     if (y_q4 & SUBPEL_MASK) {
1091       x = 0;
1092       do {
1093         const int16x8_t filters = vld1q_s16(y_filters[y_q4 & SUBPEL_MASK]);
1094         uint8x16_t ss[8];
1095         uint8x8_t s[8], d[2];
1096         load_u8_16x8(src_y, src_stride, &ss[0], &ss[1], &ss[2], &ss[3], &ss[4],
1097                      &ss[5], &ss[6], &ss[7]);
1098         s[0] = vget_low_u8(ss[0]);
1099         s[1] = vget_low_u8(ss[1]);
1100         s[2] = vget_low_u8(ss[2]);
1101         s[3] = vget_low_u8(ss[3]);
1102         s[4] = vget_low_u8(ss[4]);
1103         s[5] = vget_low_u8(ss[5]);
1104         s[6] = vget_low_u8(ss[6]);
1105         s[7] = vget_low_u8(ss[7]);
1106         d[0] = scale_filter_8(s, filters);
1107 
1108         s[0] = vget_high_u8(ss[0]);
1109         s[1] = vget_high_u8(ss[1]);
1110         s[2] = vget_high_u8(ss[2]);
1111         s[3] = vget_high_u8(ss[3]);
1112         s[4] = vget_high_u8(ss[4]);
1113         s[5] = vget_high_u8(ss[5]);
1114         s[6] = vget_high_u8(ss[6]);
1115         s[7] = vget_high_u8(ss[7]);
1116         d[1] = scale_filter_8(s, filters);
1117         vst1q_u8(&dst[x], vcombine_u8(d[0], d[1]));
1118         src_y += 16;
1119         x += 16;
1120       } while (x < w);
1121     } else {
1122       memcpy(dst, &src_y[3 * src_stride], w);
1123     }
1124     dst += dst_stride;
1125     y_q4 += y_step_q4;
1126   } while (--y);
1127 }
1128 
aom_scaled_2d_neon(const uint8_t * src,ptrdiff_t src_stride,uint8_t * dst,ptrdiff_t dst_stride,const InterpKernel * filter,int x0_q4,int x_step_q4,int y0_q4,int y_step_q4,int w,int h)1129 void aom_scaled_2d_neon(const uint8_t *src, ptrdiff_t src_stride, uint8_t *dst,
1130                         ptrdiff_t dst_stride, const InterpKernel *filter,
1131                         int x0_q4, int x_step_q4, int y0_q4, int y_step_q4,
1132                         int w, int h) {
1133   // Note: Fixed size intermediate buffer, temp, places limits on parameters.
1134   // 2d filtering proceeds in 2 steps:
1135   //   (1) Interpolate horizontally into an intermediate buffer, temp.
1136   //   (2) Interpolate temp vertically to derive the sub-pixel result.
1137   // Deriving the maximum number of rows in the temp buffer (135):
1138   // --Smallest scaling factor is x1/2 ==> y_step_q4 = 32 (Normative).
1139   // --Largest block size is 64x64 pixels.
1140   // --64 rows in the downscaled frame span a distance of (64 - 1) * 32 in the
1141   //   original frame (in 1/16th pixel units).
1142   // --Must round-up because block may be located at sub-pixel position.
1143   // --Require an additional SUBPEL_TAPS rows for the 8-tap filter tails.
1144   // --((64 - 1) * 32 + 15) >> 4 + 8 = 135.
1145   // --Require an additional 8 rows for the horiz_w8 transpose tail.
1146   // When calling in frame scaling function, the smallest scaling factor is x1/4
1147   // ==> y_step_q4 = 64. Since w and h are at most 16, the temp buffer is still
1148   // big enough.
1149   DECLARE_ALIGNED(16, uint8_t, temp[(135 + 8) * 64]);
1150   const int intermediate_height =
1151       (((h - 1) * y_step_q4 + y0_q4) >> SUBPEL_BITS) + SUBPEL_TAPS;
1152 
1153   assert(w <= 64);
1154   assert(h <= 64);
1155   assert(y_step_q4 <= 32 || (y_step_q4 <= 64 && h <= 32));
1156   assert(x_step_q4 <= 64);
1157 
1158   if (w >= 8) {
1159     scaledconvolve_horiz_w8(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1160                             src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
1161                             intermediate_height);
1162   } else {
1163     scaledconvolve_horiz_w4(src - src_stride * (SUBPEL_TAPS / 2 - 1),
1164                             src_stride, temp, 64, filter, x0_q4, x_step_q4, w,
1165                             intermediate_height);
1166   }
1167 
1168   if (w >= 16) {
1169     scaledconvolve_vert_w16(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
1170                             dst_stride, filter, y0_q4, y_step_q4, w, h);
1171   } else if (w == 8) {
1172     scaledconvolve_vert_w8(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
1173                            dst_stride, filter, y0_q4, y_step_q4, w, h);
1174   } else {
1175     scaledconvolve_vert_w4(temp + 64 * (SUBPEL_TAPS / 2 - 1), 64, dst,
1176                            dst_stride, filter, y0_q4, y_step_q4, w, h);
1177   }
1178 }
1179