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 "av1/common/arm/convolve_neon.h"
19 #include "config/av1_rtcd.h"
20 #include "config/aom_scale_rtcd.h"
21 
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)22 static INLINE void scale_plane_2_to_1_phase_0(const uint8_t *src,
23                                               const int src_stride,
24                                               uint8_t *dst,
25                                               const int dst_stride, const int w,
26                                               const int h) {
27   const int max_width = (w + 15) & ~15;
28   int y = h;
29 
30   assert(w && h);
31 
32   do {
33     int x = max_width;
34     do {
35       const uint8x16x2_t s = vld2q_u8(src);
36       vst1q_u8(dst, s.val[0]);
37       src += 32;
38       dst += 16;
39       x -= 16;
40     } while (x);
41     src += 2 * (src_stride - max_width);
42     dst += dst_stride - max_width;
43   } while (--y);
44 }
45 
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)46 static INLINE void scale_plane_4_to_1_phase_0(const uint8_t *src,
47                                               const int src_stride,
48                                               uint8_t *dst,
49                                               const int dst_stride, const int w,
50                                               const int h) {
51   const int max_width = (w + 15) & ~15;
52   int y = h;
53 
54   assert(w && h);
55 
56   do {
57     int x = max_width;
58     do {
59       const uint8x16x4_t s = vld4q_u8(src);
60       vst1q_u8(dst, s.val[0]);
61       src += 64;
62       dst += 16;
63       x -= 16;
64     } while (x);
65     src += 4 * (src_stride - max_width);
66     dst += dst_stride - max_width;
67   } while (--y);
68 }
69 
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)70 static INLINE void scale_plane_bilinear_kernel(
71     const uint8x16_t in0, const uint8x16_t in1, const uint8x16_t in2,
72     const uint8x16_t in3, const uint8x8_t coef0, const uint8x8_t coef1,
73     uint8_t *const dst) {
74   const uint16x8_t h0 = vmull_u8(vget_low_u8(in0), coef0);
75   const uint16x8_t h1 = vmull_u8(vget_high_u8(in0), coef0);
76   const uint16x8_t h2 = vmull_u8(vget_low_u8(in2), coef0);
77   const uint16x8_t h3 = vmull_u8(vget_high_u8(in2), coef0);
78   const uint16x8_t h4 = vmlal_u8(h0, vget_low_u8(in1), coef1);
79   const uint16x8_t h5 = vmlal_u8(h1, vget_high_u8(in1), coef1);
80   const uint16x8_t h6 = vmlal_u8(h2, vget_low_u8(in3), coef1);
81   const uint16x8_t h7 = vmlal_u8(h3, vget_high_u8(in3), coef1);
82 
83   const uint8x8_t hor0 = vrshrn_n_u16(h4, 7);  // temp: 00 01 02 03 04 05 06 07
84   const uint8x8_t hor1 = vrshrn_n_u16(h5, 7);  // temp: 08 09 0A 0B 0C 0D 0E 0F
85   const uint8x8_t hor2 = vrshrn_n_u16(h6, 7);  // temp: 10 11 12 13 14 15 16 17
86   const uint8x8_t hor3 = vrshrn_n_u16(h7, 7);  // temp: 18 19 1A 1B 1C 1D 1E 1F
87   const uint16x8_t v0 = vmull_u8(hor0, coef0);
88   const uint16x8_t v1 = vmull_u8(hor1, coef0);
89   const uint16x8_t v2 = vmlal_u8(v0, hor2, coef1);
90   const uint16x8_t v3 = vmlal_u8(v1, hor3, coef1);
91   // dst: 0 1 2 3 4 5 6 7  8 9 A B C D E F
92   const uint8x16_t d = vcombine_u8(vrshrn_n_u16(v2, 7), vrshrn_n_u16(v3, 7));
93   vst1q_u8(dst, d);
94 }
95 
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)96 static INLINE void scale_plane_2_to_1_bilinear(
97     const uint8_t *const src, const int src_stride, uint8_t *dst,
98     const int dst_stride, const int w, const int h, const int16_t c0,
99     const int16_t c1) {
100   const int max_width = (w + 15) & ~15;
101   const uint8_t *src0 = src;
102   const uint8_t *src1 = src + src_stride;
103   const uint8x8_t coef0 = vdup_n_u8(c0);
104   const uint8x8_t coef1 = vdup_n_u8(c1);
105   int y = h;
106 
107   assert(w && h);
108 
109   do {
110     int x = max_width;
111     do {
112       // 000 002 004 006 008 00A 00C 00E  010 012 014 016 018 01A 01C 01E
113       // 001 003 005 007 009 00B 00D 00F  011 013 015 017 019 01B 01D 01F
114       const uint8x16x2_t s0 = vld2q_u8(src0);
115       // 100 102 104 106 108 10A 10C 10E  110 112 114 116 118 11A 11C 11E
116       // 101 103 105 107 109 10B 10D 10F  111 113 115 117 119 11B 11D 11F
117       const uint8x16x2_t s1 = vld2q_u8(src1);
118       scale_plane_bilinear_kernel(s0.val[0], s0.val[1], s1.val[0], s1.val[1],
119                                   coef0, coef1, dst);
120       src0 += 32;
121       src1 += 32;
122       dst += 16;
123       x -= 16;
124     } while (x);
125     src0 += 2 * (src_stride - max_width);
126     src1 += 2 * (src_stride - max_width);
127     dst += dst_stride - max_width;
128   } while (--y);
129 }
130 
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)131 static INLINE void scale_plane_4_to_1_bilinear(
132     const uint8_t *const src, const int src_stride, uint8_t *dst,
133     const int dst_stride, const int w, const int h, const int16_t c0,
134     const int16_t c1) {
135   const int max_width = (w + 15) & ~15;
136   const uint8_t *src0 = src;
137   const uint8_t *src1 = src + src_stride;
138   const uint8x8_t coef0 = vdup_n_u8(c0);
139   const uint8x8_t coef1 = vdup_n_u8(c1);
140   int y = h;
141 
142   assert(w && h);
143 
144   do {
145     int x = max_width;
146     do {
147       // (*) -- useless
148       // 000 004 008 00C 010 014 018 01C  020 024 028 02C 030 034 038 03C
149       // 001 005 009 00D 011 015 019 01D  021 025 029 02D 031 035 039 03D
150       // 002 006 00A 00E 012 016 01A 01E  022 026 02A 02E 032 036 03A 03E (*)
151       // 003 007 00B 00F 013 017 01B 01F  023 027 02B 02F 033 037 03B 03F (*)
152       const uint8x16x4_t s0 = vld4q_u8(src0);
153       // 100 104 108 10C 110 114 118 11C  120 124 128 12C 130 134 138 13C
154       // 101 105 109 10D 111 115 119 11D  121 125 129 12D 131 135 139 13D
155       // 102 106 10A 10E 112 116 11A 11E  122 126 12A 12E 132 136 13A 13E (*)
156       // 103 107 10B 10F 113 117 11B 11F  123 127 12B 12F 133 137 13B 13F (*)
157       const uint8x16x4_t s1 = vld4q_u8(src1);
158       scale_plane_bilinear_kernel(s0.val[0], s0.val[1], s1.val[0], s1.val[1],
159                                   coef0, coef1, dst);
160       src0 += 64;
161       src1 += 64;
162       dst += 16;
163       x -= 16;
164     } while (x);
165     src0 += 4 * (src_stride - max_width);
166     src1 += 4 * (src_stride - max_width);
167     dst += dst_stride - max_width;
168   } while (--y);
169 }
170 
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)171 static void scale_plane_2_to_1_general(const uint8_t *src, const int src_stride,
172                                        uint8_t *dst, const int dst_stride,
173                                        const int w, const int h,
174                                        const int16_t *const coef,
175                                        uint8_t *const temp_buffer) {
176   const int width_hor = (w + 3) & ~3;
177   const int width_ver = (w + 7) & ~7;
178   const int height_hor = (2 * h + SUBPEL_TAPS - 2 + 7) & ~7;
179   const int height_ver = (h + 3) & ~3;
180   const int16x8_t filters = vld1q_s16(coef);
181   int x, y = height_hor;
182   uint8_t *t = temp_buffer;
183   uint8x8_t s[14], d[4];
184 
185   assert(w && h);
186 
187   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 1;
188 
189   // horizontal 4x8
190   // Note: processing 4x8 is about 20% faster than processing row by row using
191   // vld4_u8().
192   do {
193     load_u8_8x8(src + 2, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
194                 &s[6], &s[7]);
195     transpose_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
196     x = width_hor;
197 
198     do {
199       src += 8;
200       load_u8_8x8(src, src_stride, &s[6], &s[7], &s[8], &s[9], &s[10], &s[11],
201                   &s[12], &s[13]);
202       transpose_u8_8x8(&s[6], &s[7], &s[8], &s[9], &s[10], &s[11], &s[12],
203                        &s[13]);
204 
205       d[0] = scale_filter_8(&s[0], filters);  // 00 10 20 30 40 50 60 70
206       d[1] = scale_filter_8(&s[2], filters);  // 01 11 21 31 41 51 61 71
207       d[2] = scale_filter_8(&s[4], filters);  // 02 12 22 32 42 52 62 72
208       d[3] = scale_filter_8(&s[6], filters);  // 03 13 23 33 43 53 63 73
209       // 00 01 02 03 40 41 42 43
210       // 10 11 12 13 50 51 52 53
211       // 20 21 22 23 60 61 62 63
212       // 30 31 32 33 70 71 72 73
213       transpose_u8_8x4(&d[0], &d[1], &d[2], &d[3]);
214       vst1_lane_u32((uint32_t *)(t + 0 * width_hor), vreinterpret_u32_u8(d[0]),
215                     0);
216       vst1_lane_u32((uint32_t *)(t + 1 * width_hor), vreinterpret_u32_u8(d[1]),
217                     0);
218       vst1_lane_u32((uint32_t *)(t + 2 * width_hor), vreinterpret_u32_u8(d[2]),
219                     0);
220       vst1_lane_u32((uint32_t *)(t + 3 * width_hor), vreinterpret_u32_u8(d[3]),
221                     0);
222       vst1_lane_u32((uint32_t *)(t + 4 * width_hor), vreinterpret_u32_u8(d[0]),
223                     1);
224       vst1_lane_u32((uint32_t *)(t + 5 * width_hor), vreinterpret_u32_u8(d[1]),
225                     1);
226       vst1_lane_u32((uint32_t *)(t + 6 * width_hor), vreinterpret_u32_u8(d[2]),
227                     1);
228       vst1_lane_u32((uint32_t *)(t + 7 * width_hor), vreinterpret_u32_u8(d[3]),
229                     1);
230 
231       s[0] = s[8];
232       s[1] = s[9];
233       s[2] = s[10];
234       s[3] = s[11];
235       s[4] = s[12];
236       s[5] = s[13];
237 
238       t += 4;
239       x -= 4;
240     } while (x);
241     src += 8 * src_stride - 2 * width_hor;
242     t += 7 * width_hor;
243     y -= 8;
244   } while (y);
245 
246   // vertical 8x4
247   x = width_ver;
248   t = temp_buffer;
249   do {
250     load_u8_8x8(t, width_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
251                 &s[7]);
252     t += 6 * width_hor;
253     y = height_ver;
254 
255     do {
256       load_u8_8x8(t, width_hor, &s[6], &s[7], &s[8], &s[9], &s[10], &s[11],
257                   &s[12], &s[13]);
258       t += 8 * width_hor;
259 
260       d[0] = scale_filter_8(&s[0], filters);  // 00 01 02 03 04 05 06 07
261       d[1] = scale_filter_8(&s[2], filters);  // 10 11 12 13 14 15 16 17
262       d[2] = scale_filter_8(&s[4], filters);  // 20 21 22 23 24 25 26 27
263       d[3] = scale_filter_8(&s[6], filters);  // 30 31 32 33 34 35 36 37
264       vst1_u8(dst + 0 * dst_stride, d[0]);
265       vst1_u8(dst + 1 * dst_stride, d[1]);
266       vst1_u8(dst + 2 * dst_stride, d[2]);
267       vst1_u8(dst + 3 * dst_stride, d[3]);
268 
269       s[0] = s[8];
270       s[1] = s[9];
271       s[2] = s[10];
272       s[3] = s[11];
273       s[4] = s[12];
274       s[5] = s[13];
275 
276       dst += 4 * dst_stride;
277       y -= 4;
278     } while (y);
279     t -= width_hor * (2 * height_ver + 6);
280     t += 8;
281     dst -= height_ver * dst_stride;
282     dst += 8;
283     x -= 8;
284   } while (x);
285 }
286 
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)287 static void scale_plane_4_to_1_general(const uint8_t *src, const int src_stride,
288                                        uint8_t *dst, const int dst_stride,
289                                        const int w, const int h,
290                                        const int16_t *const coef,
291                                        uint8_t *const temp_buffer) {
292   const int width_hor = (w + 1) & ~1;
293   const int width_ver = (w + 7) & ~7;
294   const int height_hor = (4 * h + SUBPEL_TAPS - 2 + 7) & ~7;
295   const int height_ver = (h + 1) & ~1;
296   const int16x8_t filters = vld1q_s16(coef);
297   int x, y = height_hor;
298   uint8_t *t = temp_buffer;
299   uint8x8_t s[12], d[2];
300 
301   assert(w && h);
302 
303   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2 + 3;
304 
305   // horizontal 2x8
306   // Note: processing 2x8 is about 20% faster than processing row by row using
307   // vld4_u8().
308   do {
309     load_u8_8x8(src + 4, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
310                 &s[6], &s[7]);
311     transpose_u8_4x8(&s[0], &s[1], &s[2], &s[3], s[4], s[5], s[6], s[7]);
312     x = width_hor;
313 
314     do {
315       uint8x8x2_t dd;
316       src += 8;
317       load_u8_8x8(src, src_stride, &s[4], &s[5], &s[6], &s[7], &s[8], &s[9],
318                   &s[10], &s[11]);
319       transpose_u8_8x8(&s[4], &s[5], &s[6], &s[7], &s[8], &s[9], &s[10],
320                        &s[11]);
321 
322       d[0] = scale_filter_8(&s[0], filters);  // 00 10 20 30 40 50 60 70
323       d[1] = scale_filter_8(&s[4], filters);  // 01 11 21 31 41 51 61 71
324       // dd.val[0]: 00 01 20 21 40 41 60 61
325       // dd.val[1]: 10 11 30 31 50 51 70 71
326       dd = vtrn_u8(d[0], d[1]);
327       vst1_lane_u16((uint16_t *)(t + 0 * width_hor),
328                     vreinterpret_u16_u8(dd.val[0]), 0);
329       vst1_lane_u16((uint16_t *)(t + 1 * width_hor),
330                     vreinterpret_u16_u8(dd.val[1]), 0);
331       vst1_lane_u16((uint16_t *)(t + 2 * width_hor),
332                     vreinterpret_u16_u8(dd.val[0]), 1);
333       vst1_lane_u16((uint16_t *)(t + 3 * width_hor),
334                     vreinterpret_u16_u8(dd.val[1]), 1);
335       vst1_lane_u16((uint16_t *)(t + 4 * width_hor),
336                     vreinterpret_u16_u8(dd.val[0]), 2);
337       vst1_lane_u16((uint16_t *)(t + 5 * width_hor),
338                     vreinterpret_u16_u8(dd.val[1]), 2);
339       vst1_lane_u16((uint16_t *)(t + 6 * width_hor),
340                     vreinterpret_u16_u8(dd.val[0]), 3);
341       vst1_lane_u16((uint16_t *)(t + 7 * width_hor),
342                     vreinterpret_u16_u8(dd.val[1]), 3);
343 
344       s[0] = s[8];
345       s[1] = s[9];
346       s[2] = s[10];
347       s[3] = s[11];
348 
349       t += 2;
350       x -= 2;
351     } while (x);
352     src += 8 * src_stride - 4 * width_hor;
353     t += 7 * width_hor;
354     y -= 8;
355   } while (y);
356 
357   // vertical 8x2
358   x = width_ver;
359   t = temp_buffer;
360   do {
361     load_u8_8x4(t, width_hor, &s[0], &s[1], &s[2], &s[3]);
362     t += 4 * width_hor;
363     y = height_ver;
364 
365     do {
366       load_u8_8x8(t, width_hor, &s[4], &s[5], &s[6], &s[7], &s[8], &s[9],
367                   &s[10], &s[11]);
368       t += 8 * width_hor;
369 
370       d[0] = scale_filter_8(&s[0], filters);  // 00 01 02 03 04 05 06 07
371       d[1] = scale_filter_8(&s[4], filters);  // 10 11 12 13 14 15 16 17
372       vst1_u8(dst + 0 * dst_stride, d[0]);
373       vst1_u8(dst + 1 * dst_stride, d[1]);
374 
375       s[0] = s[8];
376       s[1] = s[9];
377       s[2] = s[10];
378       s[3] = s[11];
379 
380       dst += 2 * dst_stride;
381       y -= 2;
382     } while (y);
383     t -= width_hor * (4 * height_ver + 4);
384     t += 8;
385     dst -= height_ver * dst_stride;
386     dst += 8;
387     x -= 8;
388   } while (x);
389 }
390 
scale_filter_bilinear(const uint8x8_t * const s,const uint8x8_t * const coef)391 static INLINE uint8x8_t scale_filter_bilinear(const uint8x8_t *const s,
392                                               const uint8x8_t *const coef) {
393   const uint16x8_t h0 = vmull_u8(s[0], coef[0]);
394   const uint16x8_t h1 = vmlal_u8(h0, s[1], coef[1]);
395 
396   return vrshrn_n_u16(h1, 7);
397 }
398 
399 // Notes for 4 to 3 scaling:
400 //
401 // 1. 6 rows are calculated in each horizontal inner loop, so width_hor must be
402 // multiple of 6, and no less than w.
403 //
404 // 2. 8 rows are calculated in each vertical inner loop, so width_ver must be
405 // multiple of 8, and no less than w.
406 //
407 // 3. 8 columns are calculated in each horizontal inner loop for further
408 // vertical scaling, so height_hor must be multiple of 8, and no less than
409 // 4 * h / 3.
410 //
411 // 4. 6 columns are calculated in each vertical inner loop, so height_ver must
412 // be multiple of 6, and no less than h.
413 //
414 // 5. The physical location of the last row of the 4 to 3 scaled frame is
415 // decided by phase_scaler, and are always less than 1 pixel below the last row
416 // 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)417 static void scale_plane_4_to_3_bilinear(const uint8_t *src,
418                                         const int src_stride, uint8_t *dst,
419                                         const int dst_stride, const int w,
420                                         const int h, const int phase_scaler,
421                                         uint8_t *const temp_buffer) {
422   static const int step_q4 = 16 * 4 / 3;
423   const int width_hor = (w + 5) - ((w + 5) % 6);
424   const int stride_hor = width_hor + 2;  // store 2 extra pixels
425   const int width_ver = (w + 7) & ~7;
426   // We only need 1 extra row below because there are only 2 bilinear
427   // coefficients.
428   const int height_hor = (4 * h / 3 + 1 + 7) & ~7;
429   const int height_ver = (h + 5) - ((h + 5) % 6);
430   int x, y = height_hor;
431   uint8_t *t = temp_buffer;
432   uint8x8_t s[9], d[8], c[6];
433   const InterpKernel *interp_kernel =
434       (const InterpKernel *)av1_interp_filter_params_list[BILINEAR].filter_ptr;
435   assert(w && h);
436 
437   c[0] = vdup_n_u8((uint8_t)interp_kernel[phase_scaler][3]);
438   c[1] = vdup_n_u8((uint8_t)interp_kernel[phase_scaler][4]);
439   c[2] = vdup_n_u8(
440       (uint8_t)interp_kernel[(phase_scaler + 1 * step_q4) & SUBPEL_MASK][3]);
441   c[3] = vdup_n_u8(
442       (uint8_t)interp_kernel[(phase_scaler + 1 * step_q4) & SUBPEL_MASK][4]);
443   c[4] = vdup_n_u8(
444       (uint8_t)interp_kernel[(phase_scaler + 2 * step_q4) & SUBPEL_MASK][3]);
445   c[5] = vdup_n_u8(
446       (uint8_t)interp_kernel[(phase_scaler + 2 * step_q4) & SUBPEL_MASK][4]);
447 
448   d[6] = vdup_n_u8(0);
449   d[7] = vdup_n_u8(0);
450 
451   // horizontal 6x8
452   do {
453     load_u8_8x8(src, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
454                 &s[6], &s[7]);
455     src += 1;
456     transpose_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
457     x = width_hor;
458 
459     do {
460       load_u8_8x8(src, src_stride, &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
461                   &s[7], &s[8]);
462       src += 8;
463       transpose_u8_8x8(&s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7], &s[8]);
464 
465       // 00 10 20 30 40 50 60 70
466       // 01 11 21 31 41 51 61 71
467       // 02 12 22 32 42 52 62 72
468       // 03 13 23 33 43 53 63 73
469       // 04 14 24 34 44 54 64 74
470       // 05 15 25 35 45 55 65 75
471       d[0] = scale_filter_bilinear(&s[0], &c[0]);
472       d[1] =
473           scale_filter_bilinear(&s[(phase_scaler + 1 * step_q4) >> 4], &c[2]);
474       d[2] =
475           scale_filter_bilinear(&s[(phase_scaler + 2 * step_q4) >> 4], &c[4]);
476       d[3] = scale_filter_bilinear(&s[4], &c[0]);
477       d[4] = scale_filter_bilinear(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)],
478                                    &c[2]);
479       d[5] = scale_filter_bilinear(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)],
480                                    &c[4]);
481 
482       // 00 01 02 03 04 05 xx xx
483       // 10 11 12 13 14 15 xx xx
484       // 20 21 22 23 24 25 xx xx
485       // 30 31 32 33 34 35 xx xx
486       // 40 41 42 43 44 45 xx xx
487       // 50 51 52 53 54 55 xx xx
488       // 60 61 62 63 64 65 xx xx
489       // 70 71 72 73 74 75 xx xx
490       transpose_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6], &d[7]);
491       // store 2 extra pixels
492       vst1_u8(t + 0 * stride_hor, d[0]);
493       vst1_u8(t + 1 * stride_hor, d[1]);
494       vst1_u8(t + 2 * stride_hor, d[2]);
495       vst1_u8(t + 3 * stride_hor, d[3]);
496       vst1_u8(t + 4 * stride_hor, d[4]);
497       vst1_u8(t + 5 * stride_hor, d[5]);
498       vst1_u8(t + 6 * stride_hor, d[6]);
499       vst1_u8(t + 7 * stride_hor, d[7]);
500 
501       s[0] = s[8];
502 
503       t += 6;
504       x -= 6;
505     } while (x);
506     src += 8 * src_stride - 4 * width_hor / 3 - 1;
507     t += 7 * stride_hor + 2;
508     y -= 8;
509   } while (y);
510 
511   // vertical 8x6
512   x = width_ver;
513   t = temp_buffer;
514   do {
515     load_u8_8x8(t, stride_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
516                 &s[7]);
517     t += stride_hor;
518     y = height_ver;
519 
520     do {
521       load_u8_8x8(t, stride_hor, &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
522                   &s[7], &s[8]);
523       t += 8 * stride_hor;
524 
525       d[0] = scale_filter_bilinear(&s[0], &c[0]);
526       d[1] =
527           scale_filter_bilinear(&s[(phase_scaler + 1 * step_q4) >> 4], &c[2]);
528       d[2] =
529           scale_filter_bilinear(&s[(phase_scaler + 2 * step_q4) >> 4], &c[4]);
530       d[3] = scale_filter_bilinear(&s[4], &c[0]);
531       d[4] = scale_filter_bilinear(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)],
532                                    &c[2]);
533       d[5] = scale_filter_bilinear(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)],
534                                    &c[4]);
535       vst1_u8(dst + 0 * dst_stride, d[0]);
536       vst1_u8(dst + 1 * dst_stride, d[1]);
537       vst1_u8(dst + 2 * dst_stride, d[2]);
538       vst1_u8(dst + 3 * dst_stride, d[3]);
539       vst1_u8(dst + 4 * dst_stride, d[4]);
540       vst1_u8(dst + 5 * dst_stride, d[5]);
541 
542       s[0] = s[8];
543 
544       dst += 6 * dst_stride;
545       y -= 6;
546     } while (y);
547     t -= stride_hor * (4 * height_ver / 3 + 1);
548     t += 8;
549     dst -= height_ver * dst_stride;
550     dst += 8;
551     x -= 8;
552   } while (x);
553 }
554 
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)555 static void scale_plane_4_to_3_general(const uint8_t *src, const int src_stride,
556                                        uint8_t *dst, const int dst_stride,
557                                        const int w, const int h,
558                                        const InterpKernel *const coef,
559                                        const int phase_scaler,
560                                        uint8_t *const temp_buffer) {
561   static const int step_q4 = 16 * 4 / 3;
562   const int width_hor = (w + 5) - ((w + 5) % 6);
563   const int stride_hor = width_hor + 2;  // store 2 extra pixels
564   const int width_ver = (w + 7) & ~7;
565   // We need (SUBPEL_TAPS - 1) extra rows: (SUBPEL_TAPS / 2 - 1) extra rows
566   // above and (SUBPEL_TAPS / 2) extra rows below.
567   const int height_hor = (4 * h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
568   const int height_ver = (h + 5) - ((h + 5) % 6);
569   const int16x8_t filters0 = vld1q_s16(
570       (const int16_t *)&coef[(phase_scaler + 0 * step_q4) & SUBPEL_MASK]);
571   const int16x8_t filters1 = vld1q_s16(
572       (const int16_t *)&coef[(phase_scaler + 1 * step_q4) & SUBPEL_MASK]);
573   const int16x8_t filters2 = vld1q_s16(
574       (const int16_t *)&coef[(phase_scaler + 2 * step_q4) & SUBPEL_MASK]);
575   int x, y = height_hor;
576   uint8_t *t = temp_buffer;
577   uint8x8_t s[15], d[8];
578 
579   assert(w && h);
580 
581   src -= (SUBPEL_TAPS / 2 - 1) * src_stride + SUBPEL_TAPS / 2;
582   d[6] = vdup_n_u8(0);
583   d[7] = vdup_n_u8(0);
584 
585   // horizontal 6x8
586   do {
587     load_u8_8x8(src + 1, src_stride, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5],
588                 &s[6], &s[7]);
589     transpose_u8_8x8(&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
590     x = width_hor;
591 
592     do {
593       src += 8;
594       load_u8_8x8(src, src_stride, &s[7], &s[8], &s[9], &s[10], &s[11], &s[12],
595                   &s[13], &s[14]);
596       transpose_u8_8x8(&s[7], &s[8], &s[9], &s[10], &s[11], &s[12], &s[13],
597                        &s[14]);
598 
599       // 00 10 20 30 40 50 60 70
600       // 01 11 21 31 41 51 61 71
601       // 02 12 22 32 42 52 62 72
602       // 03 13 23 33 43 53 63 73
603       // 04 14 24 34 44 54 64 74
604       // 05 15 25 35 45 55 65 75
605       d[0] = scale_filter_8(&s[0], filters0);
606       d[1] = scale_filter_8(&s[(phase_scaler + 1 * step_q4) >> 4], filters1);
607       d[2] = scale_filter_8(&s[(phase_scaler + 2 * step_q4) >> 4], filters2);
608       d[3] = scale_filter_8(&s[4], filters0);
609       d[4] =
610           scale_filter_8(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)], filters1);
611       d[5] =
612           scale_filter_8(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)], filters2);
613 
614       // 00 01 02 03 04 05 xx xx
615       // 10 11 12 13 14 15 xx xx
616       // 20 21 22 23 24 25 xx xx
617       // 30 31 32 33 34 35 xx xx
618       // 40 41 42 43 44 45 xx xx
619       // 50 51 52 53 54 55 xx xx
620       // 60 61 62 63 64 65 xx xx
621       // 70 71 72 73 74 75 xx xx
622       transpose_u8_8x8(&d[0], &d[1], &d[2], &d[3], &d[4], &d[5], &d[6], &d[7]);
623       // store 2 extra pixels
624       vst1_u8(t + 0 * stride_hor, d[0]);
625       vst1_u8(t + 1 * stride_hor, d[1]);
626       vst1_u8(t + 2 * stride_hor, d[2]);
627       vst1_u8(t + 3 * stride_hor, d[3]);
628       vst1_u8(t + 4 * stride_hor, d[4]);
629       vst1_u8(t + 5 * stride_hor, d[5]);
630       vst1_u8(t + 6 * stride_hor, d[6]);
631       vst1_u8(t + 7 * stride_hor, d[7]);
632 
633       s[0] = s[8];
634       s[1] = s[9];
635       s[2] = s[10];
636       s[3] = s[11];
637       s[4] = s[12];
638       s[5] = s[13];
639       s[6] = s[14];
640 
641       t += 6;
642       x -= 6;
643     } while (x);
644     src += 8 * src_stride - 4 * width_hor / 3;
645     t += 7 * stride_hor + 2;
646     y -= 8;
647   } while (y);
648 
649   // vertical 8x6
650   x = width_ver;
651   t = temp_buffer;
652   do {
653     load_u8_8x8(t, stride_hor, &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6],
654                 &s[7]);
655     t += 7 * stride_hor;
656     y = height_ver;
657 
658     do {
659       load_u8_8x8(t, stride_hor, &s[7], &s[8], &s[9], &s[10], &s[11], &s[12],
660                   &s[13], &s[14]);
661       t += 8 * stride_hor;
662 
663       d[0] = scale_filter_8(&s[0], filters0);
664       d[1] = scale_filter_8(&s[(phase_scaler + 1 * step_q4) >> 4], filters1);
665       d[2] = scale_filter_8(&s[(phase_scaler + 2 * step_q4) >> 4], filters2);
666       d[3] = scale_filter_8(&s[4], filters0);
667       d[4] =
668           scale_filter_8(&s[4 + ((phase_scaler + 1 * step_q4) >> 4)], filters1);
669       d[5] =
670           scale_filter_8(&s[4 + ((phase_scaler + 2 * step_q4) >> 4)], filters2);
671       vst1_u8(dst + 0 * dst_stride, d[0]);
672       vst1_u8(dst + 1 * dst_stride, d[1]);
673       vst1_u8(dst + 2 * dst_stride, d[2]);
674       vst1_u8(dst + 3 * dst_stride, d[3]);
675       vst1_u8(dst + 4 * dst_stride, d[4]);
676       vst1_u8(dst + 5 * dst_stride, d[5]);
677 
678       s[0] = s[8];
679       s[1] = s[9];
680       s[2] = s[10];
681       s[3] = s[11];
682       s[4] = s[12];
683       s[5] = s[13];
684       s[6] = s[14];
685 
686       dst += 6 * dst_stride;
687       y -= 6;
688     } while (y);
689     t -= stride_hor * (4 * height_ver / 3 + 7);
690     t += 8;
691     dst -= height_ver * dst_stride;
692     dst += 8;
693     x -= 8;
694   } while (x);
695 }
696 
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)697 void av1_resize_and_extend_frame_neon(const YV12_BUFFER_CONFIG *src,
698                                       YV12_BUFFER_CONFIG *dst,
699                                       const InterpFilter filter,
700                                       const int phase, const int num_planes) {
701   // We use AOMMIN(num_planes, MAX_MB_PLANE) instead of num_planes to quiet
702   // the static analysis warnings.
703   int scaled = 0;
704   for (int i = 0; i < AOMMIN(num_planes, MAX_MB_PLANE); ++i) {
705     const int is_uv = i > 0;
706     const int src_w = src->crop_widths[is_uv];
707     const int src_h = src->crop_heights[is_uv];
708     const int dst_w = dst->crop_widths[is_uv];
709     const int dst_h = dst->crop_heights[is_uv];
710     const int dst_y_w = (dst->crop_widths[0] + 1) & ~1;
711     const int dst_y_h = (dst->crop_heights[0] + 1) & ~1;
712 
713     if (2 * dst_w == src_w && 2 * dst_h == src_h) {
714       scaled = 1;
715       if (phase == 0) {
716         scale_plane_2_to_1_phase_0(src->buffers[i], src->strides[is_uv],
717                                    dst->buffers[i], dst->strides[is_uv], dst_w,
718                                    dst_h);
719       } else if (filter == BILINEAR) {
720         const int16_t c0 = av1_bilinear_filters[phase][3];
721         const int16_t c1 = av1_bilinear_filters[phase][4];
722         scale_plane_2_to_1_bilinear(src->buffers[i], src->strides[is_uv],
723                                     dst->buffers[i], dst->strides[is_uv], dst_w,
724                                     dst_h, c0, c1);
725       } else {
726         const int buffer_stride = (dst_y_w + 3) & ~3;
727         const int buffer_height = (2 * dst_y_h + SUBPEL_TAPS - 2 + 7) & ~7;
728         uint8_t *const temp_buffer =
729             (uint8_t *)malloc(buffer_stride * buffer_height);
730         if (temp_buffer) {
731           const InterpKernel *interp_kernel =
732               (const InterpKernel *)av1_interp_filter_params_list[filter]
733                   .filter_ptr;
734           scale_plane_2_to_1_general(src->buffers[i], src->strides[is_uv],
735                                      dst->buffers[i], dst->strides[is_uv],
736                                      dst_w, dst_h, interp_kernel[phase],
737                                      temp_buffer);
738           free(temp_buffer);
739         } else {
740           scaled = 0;
741         }
742       }
743     } else if (4 * dst_w == src_w && 4 * dst_h == src_h) {
744       scaled = 1;
745       if (phase == 0) {
746         scale_plane_4_to_1_phase_0(src->buffers[i], src->strides[is_uv],
747                                    dst->buffers[i], dst->strides[is_uv], dst_w,
748                                    dst_h);
749       } else if (filter == BILINEAR) {
750         const int16_t c0 = av1_bilinear_filters[phase][3];
751         const int16_t c1 = av1_bilinear_filters[phase][4];
752         scale_plane_4_to_1_bilinear(src->buffers[i], src->strides[is_uv],
753                                     dst->buffers[i], dst->strides[is_uv], dst_w,
754                                     dst_h, c0, c1);
755       } else {
756         const int buffer_stride = (dst_y_w + 1) & ~1;
757         const int buffer_height = (4 * dst_y_h + SUBPEL_TAPS - 2 + 7) & ~7;
758         uint8_t *const temp_buffer =
759             (uint8_t *)malloc(buffer_stride * buffer_height);
760         if (temp_buffer) {
761           const InterpKernel *interp_kernel =
762               (const InterpKernel *)av1_interp_filter_params_list[filter]
763                   .filter_ptr;
764           scale_plane_4_to_1_general(src->buffers[i], src->strides[is_uv],
765                                      dst->buffers[i], dst->strides[is_uv],
766                                      dst_w, dst_h, interp_kernel[phase],
767                                      temp_buffer);
768           free(temp_buffer);
769         } else {
770           scaled = 0;
771         }
772       }
773     } else if (4 * dst_w == 3 * src_w && 4 * dst_h == 3 * src_h) {
774       // 4 to 3
775       const int buffer_stride = (dst_y_w + 5) - ((dst_y_w + 5) % 6) + 2;
776       const int buffer_height = (4 * dst_y_h / 3 + SUBPEL_TAPS - 1 + 7) & ~7;
777       uint8_t *const temp_buffer =
778           (uint8_t *)malloc(buffer_stride * buffer_height);
779       if (temp_buffer) {
780         scaled = 1;
781         if (filter == BILINEAR) {
782           scale_plane_4_to_3_bilinear(src->buffers[i], src->strides[is_uv],
783                                       dst->buffers[i], dst->strides[is_uv],
784                                       dst_w, dst_h, phase, temp_buffer);
785         } else {
786           const InterpKernel *interp_kernel =
787               (const InterpKernel *)av1_interp_filter_params_list[filter]
788                   .filter_ptr;
789           scale_plane_4_to_3_general(src->buffers[i], src->strides[is_uv],
790                                      dst->buffers[i], dst->strides[is_uv],
791                                      dst_w, dst_h, interp_kernel, phase,
792                                      temp_buffer);
793         }
794         free(temp_buffer);
795       } else {
796         scaled = 0;
797       }
798     }
799   }
800   if (!scaled) {
801     av1_resize_and_extend_frame_c(src, dst, filter, phase, num_planes);
802   } else {
803     aom_extend_frame_borders(dst, num_planes);
804   }
805 }
806