xref: /aosp_15_r20/external/libaom/av1/common/warped_motion.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
14*77c1e3ccSAndroid Build Coastguard Worker #include <memory.h>
15*77c1e3ccSAndroid Build Coastguard Worker #include <math.h>
16*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
17*77c1e3ccSAndroid Build Coastguard Worker 
18*77c1e3ccSAndroid Build Coastguard Worker #include "config/av1_rtcd.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/av1_common_int.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/warped_motion.h"
22*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/scale.h"
23*77c1e3ccSAndroid Build Coastguard Worker 
24*77c1e3ccSAndroid Build Coastguard Worker // For warping, we really use a 6-tap filter, but we do blocks of 8 pixels
25*77c1e3ccSAndroid Build Coastguard Worker // at a time. The zoom/rotation/shear in the model are applied to the
26*77c1e3ccSAndroid Build Coastguard Worker // "fractional" position of each pixel, which therefore varies within
27*77c1e3ccSAndroid Build Coastguard Worker // [-1, 2) * WARPEDPIXEL_PREC_SHIFTS.
28*77c1e3ccSAndroid Build Coastguard Worker // We need an extra 2 taps to fit this in, for a total of 8 taps.
29*77c1e3ccSAndroid Build Coastguard Worker /* clang-format off */
30*77c1e3ccSAndroid Build Coastguard Worker const int16_t av1_warped_filter[WARPEDPIXEL_PREC_SHIFTS * 3 + 1][8] = {
31*77c1e3ccSAndroid Build Coastguard Worker   // [-1, 0)
32*77c1e3ccSAndroid Build Coastguard Worker   { 0,   0, 127,   1,   0, 0, 0, 0 }, { 0, - 1, 127,   2,   0, 0, 0, 0 },
33*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 3, 127,   4, - 1, 0, 0, 0 }, { 1, - 4, 126,   6, - 2, 1, 0, 0 },
34*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 5, 126,   8, - 3, 1, 0, 0 }, { 1, - 6, 125,  11, - 4, 1, 0, 0 },
35*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 7, 124,  13, - 4, 1, 0, 0 }, { 2, - 8, 123,  15, - 5, 1, 0, 0 },
36*77c1e3ccSAndroid Build Coastguard Worker   { 2, - 9, 122,  18, - 6, 1, 0, 0 }, { 2, -10, 121,  20, - 6, 1, 0, 0 },
37*77c1e3ccSAndroid Build Coastguard Worker   { 2, -11, 120,  22, - 7, 2, 0, 0 }, { 2, -12, 119,  25, - 8, 2, 0, 0 },
38*77c1e3ccSAndroid Build Coastguard Worker   { 3, -13, 117,  27, - 8, 2, 0, 0 }, { 3, -13, 116,  29, - 9, 2, 0, 0 },
39*77c1e3ccSAndroid Build Coastguard Worker   { 3, -14, 114,  32, -10, 3, 0, 0 }, { 3, -15, 113,  35, -10, 2, 0, 0 },
40*77c1e3ccSAndroid Build Coastguard Worker   { 3, -15, 111,  37, -11, 3, 0, 0 }, { 3, -16, 109,  40, -11, 3, 0, 0 },
41*77c1e3ccSAndroid Build Coastguard Worker   { 3, -16, 108,  42, -12, 3, 0, 0 }, { 4, -17, 106,  45, -13, 3, 0, 0 },
42*77c1e3ccSAndroid Build Coastguard Worker   { 4, -17, 104,  47, -13, 3, 0, 0 }, { 4, -17, 102,  50, -14, 3, 0, 0 },
43*77c1e3ccSAndroid Build Coastguard Worker   { 4, -17, 100,  52, -14, 3, 0, 0 }, { 4, -18,  98,  55, -15, 4, 0, 0 },
44*77c1e3ccSAndroid Build Coastguard Worker   { 4, -18,  96,  58, -15, 3, 0, 0 }, { 4, -18,  94,  60, -16, 4, 0, 0 },
45*77c1e3ccSAndroid Build Coastguard Worker   { 4, -18,  91,  63, -16, 4, 0, 0 }, { 4, -18,  89,  65, -16, 4, 0, 0 },
46*77c1e3ccSAndroid Build Coastguard Worker   { 4, -18,  87,  68, -17, 4, 0, 0 }, { 4, -18,  85,  70, -17, 4, 0, 0 },
47*77c1e3ccSAndroid Build Coastguard Worker   { 4, -18,  82,  73, -17, 4, 0, 0 }, { 4, -18,  80,  75, -17, 4, 0, 0 },
48*77c1e3ccSAndroid Build Coastguard Worker   { 4, -18,  78,  78, -18, 4, 0, 0 }, { 4, -17,  75,  80, -18, 4, 0, 0 },
49*77c1e3ccSAndroid Build Coastguard Worker   { 4, -17,  73,  82, -18, 4, 0, 0 }, { 4, -17,  70,  85, -18, 4, 0, 0 },
50*77c1e3ccSAndroid Build Coastguard Worker   { 4, -17,  68,  87, -18, 4, 0, 0 }, { 4, -16,  65,  89, -18, 4, 0, 0 },
51*77c1e3ccSAndroid Build Coastguard Worker   { 4, -16,  63,  91, -18, 4, 0, 0 }, { 4, -16,  60,  94, -18, 4, 0, 0 },
52*77c1e3ccSAndroid Build Coastguard Worker   { 3, -15,  58,  96, -18, 4, 0, 0 }, { 4, -15,  55,  98, -18, 4, 0, 0 },
53*77c1e3ccSAndroid Build Coastguard Worker   { 3, -14,  52, 100, -17, 4, 0, 0 }, { 3, -14,  50, 102, -17, 4, 0, 0 },
54*77c1e3ccSAndroid Build Coastguard Worker   { 3, -13,  47, 104, -17, 4, 0, 0 }, { 3, -13,  45, 106, -17, 4, 0, 0 },
55*77c1e3ccSAndroid Build Coastguard Worker   { 3, -12,  42, 108, -16, 3, 0, 0 }, { 3, -11,  40, 109, -16, 3, 0, 0 },
56*77c1e3ccSAndroid Build Coastguard Worker   { 3, -11,  37, 111, -15, 3, 0, 0 }, { 2, -10,  35, 113, -15, 3, 0, 0 },
57*77c1e3ccSAndroid Build Coastguard Worker   { 3, -10,  32, 114, -14, 3, 0, 0 }, { 2, - 9,  29, 116, -13, 3, 0, 0 },
58*77c1e3ccSAndroid Build Coastguard Worker   { 2, - 8,  27, 117, -13, 3, 0, 0 }, { 2, - 8,  25, 119, -12, 2, 0, 0 },
59*77c1e3ccSAndroid Build Coastguard Worker   { 2, - 7,  22, 120, -11, 2, 0, 0 }, { 1, - 6,  20, 121, -10, 2, 0, 0 },
60*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 6,  18, 122, - 9, 2, 0, 0 }, { 1, - 5,  15, 123, - 8, 2, 0, 0 },
61*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 4,  13, 124, - 7, 1, 0, 0 }, { 1, - 4,  11, 125, - 6, 1, 0, 0 },
62*77c1e3ccSAndroid Build Coastguard Worker   { 1, - 3,   8, 126, - 5, 1, 0, 0 }, { 1, - 2,   6, 126, - 4, 1, 0, 0 },
63*77c1e3ccSAndroid Build Coastguard Worker   { 0, - 1,   4, 127, - 3, 1, 0, 0 }, { 0,   0,   2, 127, - 1, 0, 0, 0 },
64*77c1e3ccSAndroid Build Coastguard Worker 
65*77c1e3ccSAndroid Build Coastguard Worker   // [0, 1)
66*77c1e3ccSAndroid Build Coastguard Worker   { 0,  0,   0, 127,   1,   0,  0,  0}, { 0,  0,  -1, 127,   2,   0,  0,  0},
67*77c1e3ccSAndroid Build Coastguard Worker   { 0,  1,  -3, 127,   4,  -2,  1,  0}, { 0,  1,  -5, 127,   6,  -2,  1,  0},
68*77c1e3ccSAndroid Build Coastguard Worker   { 0,  2,  -6, 126,   8,  -3,  1,  0}, {-1,  2,  -7, 126,  11,  -4,  2, -1},
69*77c1e3ccSAndroid Build Coastguard Worker   {-1,  3,  -8, 125,  13,  -5,  2, -1}, {-1,  3, -10, 124,  16,  -6,  3, -1},
70*77c1e3ccSAndroid Build Coastguard Worker   {-1,  4, -11, 123,  18,  -7,  3, -1}, {-1,  4, -12, 122,  20,  -7,  3, -1},
71*77c1e3ccSAndroid Build Coastguard Worker   {-1,  4, -13, 121,  23,  -8,  3, -1}, {-2,  5, -14, 120,  25,  -9,  4, -1},
72*77c1e3ccSAndroid Build Coastguard Worker   {-1,  5, -15, 119,  27, -10,  4, -1}, {-1,  5, -16, 118,  30, -11,  4, -1},
73*77c1e3ccSAndroid Build Coastguard Worker   {-2,  6, -17, 116,  33, -12,  5, -1}, {-2,  6, -17, 114,  35, -12,  5, -1},
74*77c1e3ccSAndroid Build Coastguard Worker   {-2,  6, -18, 113,  38, -13,  5, -1}, {-2,  7, -19, 111,  41, -14,  6, -2},
75*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -19, 110,  43, -15,  6, -2}, {-2,  7, -20, 108,  46, -15,  6, -2},
76*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -20, 106,  49, -16,  6, -2}, {-2,  7, -21, 104,  51, -16,  7, -2},
77*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -21, 102,  54, -17,  7, -2}, {-2,  8, -21, 100,  56, -18,  7, -2},
78*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -22,  98,  59, -18,  7, -2}, {-2,  8, -22,  96,  62, -19,  7, -2},
79*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -22,  94,  64, -19,  7, -2}, {-2,  8, -22,  91,  67, -20,  8, -2},
80*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -22,  89,  69, -20,  8, -2}, {-2,  8, -22,  87,  72, -21,  8, -2},
81*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -21,  84,  74, -21,  8, -2}, {-2,  8, -22,  82,  77, -21,  8, -2},
82*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -21,  79,  79, -21,  8, -2}, {-2,  8, -21,  77,  82, -22,  8, -2},
83*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -21,  74,  84, -21,  8, -2}, {-2,  8, -21,  72,  87, -22,  8, -2},
84*77c1e3ccSAndroid Build Coastguard Worker   {-2,  8, -20,  69,  89, -22,  8, -2}, {-2,  8, -20,  67,  91, -22,  8, -2},
85*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -19,  64,  94, -22,  8, -2}, {-2,  7, -19,  62,  96, -22,  8, -2},
86*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -18,  59,  98, -22,  8, -2}, {-2,  7, -18,  56, 100, -21,  8, -2},
87*77c1e3ccSAndroid Build Coastguard Worker   {-2,  7, -17,  54, 102, -21,  7, -2}, {-2,  7, -16,  51, 104, -21,  7, -2},
88*77c1e3ccSAndroid Build Coastguard Worker   {-2,  6, -16,  49, 106, -20,  7, -2}, {-2,  6, -15,  46, 108, -20,  7, -2},
89*77c1e3ccSAndroid Build Coastguard Worker   {-2,  6, -15,  43, 110, -19,  7, -2}, {-2,  6, -14,  41, 111, -19,  7, -2},
90*77c1e3ccSAndroid Build Coastguard Worker   {-1,  5, -13,  38, 113, -18,  6, -2}, {-1,  5, -12,  35, 114, -17,  6, -2},
91*77c1e3ccSAndroid Build Coastguard Worker   {-1,  5, -12,  33, 116, -17,  6, -2}, {-1,  4, -11,  30, 118, -16,  5, -1},
92*77c1e3ccSAndroid Build Coastguard Worker   {-1,  4, -10,  27, 119, -15,  5, -1}, {-1,  4,  -9,  25, 120, -14,  5, -2},
93*77c1e3ccSAndroid Build Coastguard Worker   {-1,  3,  -8,  23, 121, -13,  4, -1}, {-1,  3,  -7,  20, 122, -12,  4, -1},
94*77c1e3ccSAndroid Build Coastguard Worker   {-1,  3,  -7,  18, 123, -11,  4, -1}, {-1,  3,  -6,  16, 124, -10,  3, -1},
95*77c1e3ccSAndroid Build Coastguard Worker   {-1,  2,  -5,  13, 125,  -8,  3, -1}, {-1,  2,  -4,  11, 126,  -7,  2, -1},
96*77c1e3ccSAndroid Build Coastguard Worker   { 0,  1,  -3,   8, 126,  -6,  2,  0}, { 0,  1,  -2,   6, 127,  -5,  1,  0},
97*77c1e3ccSAndroid Build Coastguard Worker   { 0,  1,  -2,   4, 127,  -3,  1,  0}, { 0,  0,   0,   2, 127,  -1,  0,  0},
98*77c1e3ccSAndroid Build Coastguard Worker 
99*77c1e3ccSAndroid Build Coastguard Worker   // [1, 2)
100*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 0,   1, 127,   0,   0, 0 }, { 0, 0, 0, - 1, 127,   2,   0, 0 },
101*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 3, 127,   4, - 1, 0 }, { 0, 0, 1, - 4, 126,   6, - 2, 1 },
102*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 5, 126,   8, - 3, 1 }, { 0, 0, 1, - 6, 125,  11, - 4, 1 },
103*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 7, 124,  13, - 4, 1 }, { 0, 0, 2, - 8, 123,  15, - 5, 1 },
104*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 2, - 9, 122,  18, - 6, 1 }, { 0, 0, 2, -10, 121,  20, - 6, 1 },
105*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 2, -11, 120,  22, - 7, 2 }, { 0, 0, 2, -12, 119,  25, - 8, 2 },
106*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -13, 117,  27, - 8, 2 }, { 0, 0, 3, -13, 116,  29, - 9, 2 },
107*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -14, 114,  32, -10, 3 }, { 0, 0, 3, -15, 113,  35, -10, 2 },
108*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -15, 111,  37, -11, 3 }, { 0, 0, 3, -16, 109,  40, -11, 3 },
109*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -16, 108,  42, -12, 3 }, { 0, 0, 4, -17, 106,  45, -13, 3 },
110*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -17, 104,  47, -13, 3 }, { 0, 0, 4, -17, 102,  50, -14, 3 },
111*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -17, 100,  52, -14, 3 }, { 0, 0, 4, -18,  98,  55, -15, 4 },
112*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -18,  96,  58, -15, 3 }, { 0, 0, 4, -18,  94,  60, -16, 4 },
113*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -18,  91,  63, -16, 4 }, { 0, 0, 4, -18,  89,  65, -16, 4 },
114*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -18,  87,  68, -17, 4 }, { 0, 0, 4, -18,  85,  70, -17, 4 },
115*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -18,  82,  73, -17, 4 }, { 0, 0, 4, -18,  80,  75, -17, 4 },
116*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -18,  78,  78, -18, 4 }, { 0, 0, 4, -17,  75,  80, -18, 4 },
117*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -17,  73,  82, -18, 4 }, { 0, 0, 4, -17,  70,  85, -18, 4 },
118*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -17,  68,  87, -18, 4 }, { 0, 0, 4, -16,  65,  89, -18, 4 },
119*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 4, -16,  63,  91, -18, 4 }, { 0, 0, 4, -16,  60,  94, -18, 4 },
120*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -15,  58,  96, -18, 4 }, { 0, 0, 4, -15,  55,  98, -18, 4 },
121*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -14,  52, 100, -17, 4 }, { 0, 0, 3, -14,  50, 102, -17, 4 },
122*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -13,  47, 104, -17, 4 }, { 0, 0, 3, -13,  45, 106, -17, 4 },
123*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -12,  42, 108, -16, 3 }, { 0, 0, 3, -11,  40, 109, -16, 3 },
124*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -11,  37, 111, -15, 3 }, { 0, 0, 2, -10,  35, 113, -15, 3 },
125*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 3, -10,  32, 114, -14, 3 }, { 0, 0, 2, - 9,  29, 116, -13, 3 },
126*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 2, - 8,  27, 117, -13, 3 }, { 0, 0, 2, - 8,  25, 119, -12, 2 },
127*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 2, - 7,  22, 120, -11, 2 }, { 0, 0, 1, - 6,  20, 121, -10, 2 },
128*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 6,  18, 122, - 9, 2 }, { 0, 0, 1, - 5,  15, 123, - 8, 2 },
129*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 4,  13, 124, - 7, 1 }, { 0, 0, 1, - 4,  11, 125, - 6, 1 },
130*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 1, - 3,   8, 126, - 5, 1 }, { 0, 0, 1, - 2,   6, 126, - 4, 1 },
131*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 0, - 1,   4, 127, - 3, 1 }, { 0, 0, 0,   0,   2, 127, - 1, 0 },
132*77c1e3ccSAndroid Build Coastguard Worker   // dummy (replicate row index 191)
133*77c1e3ccSAndroid Build Coastguard Worker   { 0, 0, 0,   0,   2, 127, - 1, 0 },
134*77c1e3ccSAndroid Build Coastguard Worker };
135*77c1e3ccSAndroid Build Coastguard Worker 
136*77c1e3ccSAndroid Build Coastguard Worker /* clang-format on */
137*77c1e3ccSAndroid Build Coastguard Worker 
138*77c1e3ccSAndroid Build Coastguard Worker #define DIV_LUT_PREC_BITS 14
139*77c1e3ccSAndroid Build Coastguard Worker #define DIV_LUT_BITS 8
140*77c1e3ccSAndroid Build Coastguard Worker #define DIV_LUT_NUM (1 << DIV_LUT_BITS)
141*77c1e3ccSAndroid Build Coastguard Worker 
142*77c1e3ccSAndroid Build Coastguard Worker static const uint16_t div_lut[DIV_LUT_NUM + 1] = {
143*77c1e3ccSAndroid Build Coastguard Worker   16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768,
144*77c1e3ccSAndroid Build Coastguard Worker   15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142,
145*77c1e3ccSAndroid Build Coastguard Worker   15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564,
146*77c1e3ccSAndroid Build Coastguard Worker   14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028,
147*77c1e3ccSAndroid Build Coastguard Worker   13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530,
148*77c1e3ccSAndroid Build Coastguard Worker   13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066,
149*77c1e3ccSAndroid Build Coastguard Worker   13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633,
150*77c1e3ccSAndroid Build Coastguard Worker   12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228,
151*77c1e3ccSAndroid Build Coastguard Worker   12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848,
152*77c1e3ccSAndroid Build Coastguard Worker   11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491,
153*77c1e3ccSAndroid Build Coastguard Worker   11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155,
154*77c1e3ccSAndroid Build Coastguard Worker   11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838,
155*77c1e3ccSAndroid Build Coastguard Worker   10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538,
156*77c1e3ccSAndroid Build Coastguard Worker   10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255,
157*77c1e3ccSAndroid Build Coastguard Worker   10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986,
158*77c1e3ccSAndroid Build Coastguard Worker   9963,  9939,  9916,  9892,  9869,  9846,  9823,  9800,  9777,  9754,  9732,
159*77c1e3ccSAndroid Build Coastguard Worker   9709,  9687,  9664,  9642,  9620,  9598,  9576,  9554,  9533,  9511,  9489,
160*77c1e3ccSAndroid Build Coastguard Worker   9468,  9447,  9425,  9404,  9383,  9362,  9341,  9321,  9300,  9279,  9259,
161*77c1e3ccSAndroid Build Coastguard Worker   9239,  9218,  9198,  9178,  9158,  9138,  9118,  9098,  9079,  9059,  9039,
162*77c1e3ccSAndroid Build Coastguard Worker   9020,  9001,  8981,  8962,  8943,  8924,  8905,  8886,  8867,  8849,  8830,
163*77c1e3ccSAndroid Build Coastguard Worker   8812,  8793,  8775,  8756,  8738,  8720,  8702,  8684,  8666,  8648,  8630,
164*77c1e3ccSAndroid Build Coastguard Worker   8613,  8595,  8577,  8560,  8542,  8525,  8508,  8490,  8473,  8456,  8439,
165*77c1e3ccSAndroid Build Coastguard Worker   8422,  8405,  8389,  8372,  8355,  8339,  8322,  8306,  8289,  8273,  8257,
166*77c1e3ccSAndroid Build Coastguard Worker   8240,  8224,  8208,  8192,
167*77c1e3ccSAndroid Build Coastguard Worker };
168*77c1e3ccSAndroid Build Coastguard Worker 
169*77c1e3ccSAndroid Build Coastguard Worker // Decomposes a divisor D such that 1/D = y/2^shift, where y is returned
170*77c1e3ccSAndroid Build Coastguard Worker // at precision of DIV_LUT_PREC_BITS along with the shift.
resolve_divisor_64(uint64_t D,int16_t * shift)171*77c1e3ccSAndroid Build Coastguard Worker static int16_t resolve_divisor_64(uint64_t D, int16_t *shift) {
172*77c1e3ccSAndroid Build Coastguard Worker   int64_t f;
173*77c1e3ccSAndroid Build Coastguard Worker   *shift = (int16_t)((D >> 32) ? get_msb((unsigned int)(D >> 32)) + 32
174*77c1e3ccSAndroid Build Coastguard Worker                                : get_msb((unsigned int)D));
175*77c1e3ccSAndroid Build Coastguard Worker   // e is obtained from D after resetting the most significant 1 bit.
176*77c1e3ccSAndroid Build Coastguard Worker   const int64_t e = D - ((uint64_t)1 << *shift);
177*77c1e3ccSAndroid Build Coastguard Worker   // Get the most significant DIV_LUT_BITS (8) bits of e into f
178*77c1e3ccSAndroid Build Coastguard Worker   if (*shift > DIV_LUT_BITS)
179*77c1e3ccSAndroid Build Coastguard Worker     f = ROUND_POWER_OF_TWO_64(e, *shift - DIV_LUT_BITS);
180*77c1e3ccSAndroid Build Coastguard Worker   else
181*77c1e3ccSAndroid Build Coastguard Worker     f = e << (DIV_LUT_BITS - *shift);
182*77c1e3ccSAndroid Build Coastguard Worker   assert(f <= DIV_LUT_NUM);
183*77c1e3ccSAndroid Build Coastguard Worker   *shift += DIV_LUT_PREC_BITS;
184*77c1e3ccSAndroid Build Coastguard Worker   // Use f as lookup into the precomputed table of multipliers
185*77c1e3ccSAndroid Build Coastguard Worker   return div_lut[f];
186*77c1e3ccSAndroid Build Coastguard Worker }
187*77c1e3ccSAndroid Build Coastguard Worker 
resolve_divisor_32(uint32_t D,int16_t * shift)188*77c1e3ccSAndroid Build Coastguard Worker static int16_t resolve_divisor_32(uint32_t D, int16_t *shift) {
189*77c1e3ccSAndroid Build Coastguard Worker   int32_t f;
190*77c1e3ccSAndroid Build Coastguard Worker   *shift = get_msb(D);
191*77c1e3ccSAndroid Build Coastguard Worker   // e is obtained from D after resetting the most significant 1 bit.
192*77c1e3ccSAndroid Build Coastguard Worker   const int32_t e = D - ((uint32_t)1 << *shift);
193*77c1e3ccSAndroid Build Coastguard Worker   // Get the most significant DIV_LUT_BITS (8) bits of e into f
194*77c1e3ccSAndroid Build Coastguard Worker   if (*shift > DIV_LUT_BITS)
195*77c1e3ccSAndroid Build Coastguard Worker     f = ROUND_POWER_OF_TWO(e, *shift - DIV_LUT_BITS);
196*77c1e3ccSAndroid Build Coastguard Worker   else
197*77c1e3ccSAndroid Build Coastguard Worker     f = e << (DIV_LUT_BITS - *shift);
198*77c1e3ccSAndroid Build Coastguard Worker   assert(f <= DIV_LUT_NUM);
199*77c1e3ccSAndroid Build Coastguard Worker   *shift += DIV_LUT_PREC_BITS;
200*77c1e3ccSAndroid Build Coastguard Worker   // Use f as lookup into the precomputed table of multipliers
201*77c1e3ccSAndroid Build Coastguard Worker   return div_lut[f];
202*77c1e3ccSAndroid Build Coastguard Worker }
203*77c1e3ccSAndroid Build Coastguard Worker 
is_affine_valid(const WarpedMotionParams * const wm)204*77c1e3ccSAndroid Build Coastguard Worker static int is_affine_valid(const WarpedMotionParams *const wm) {
205*77c1e3ccSAndroid Build Coastguard Worker   const int32_t *mat = wm->wmmat;
206*77c1e3ccSAndroid Build Coastguard Worker   return (mat[2] > 0);
207*77c1e3ccSAndroid Build Coastguard Worker }
208*77c1e3ccSAndroid Build Coastguard Worker 
is_affine_shear_allowed(int16_t alpha,int16_t beta,int16_t gamma,int16_t delta)209*77c1e3ccSAndroid Build Coastguard Worker static int is_affine_shear_allowed(int16_t alpha, int16_t beta, int16_t gamma,
210*77c1e3ccSAndroid Build Coastguard Worker                                    int16_t delta) {
211*77c1e3ccSAndroid Build Coastguard Worker   if ((4 * abs(alpha) + 7 * abs(beta) >= (1 << WARPEDMODEL_PREC_BITS)) ||
212*77c1e3ccSAndroid Build Coastguard Worker       (4 * abs(gamma) + 4 * abs(delta) >= (1 << WARPEDMODEL_PREC_BITS)))
213*77c1e3ccSAndroid Build Coastguard Worker     return 0;
214*77c1e3ccSAndroid Build Coastguard Worker   else
215*77c1e3ccSAndroid Build Coastguard Worker     return 1;
216*77c1e3ccSAndroid Build Coastguard Worker }
217*77c1e3ccSAndroid Build Coastguard Worker 
218*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
219*77c1e3ccSAndroid Build Coastguard Worker // Check that the given warp model satisfies the relevant constraints for
220*77c1e3ccSAndroid Build Coastguard Worker // its stated model type
check_model_consistency(WarpedMotionParams * wm)221*77c1e3ccSAndroid Build Coastguard Worker static void check_model_consistency(WarpedMotionParams *wm) {
222*77c1e3ccSAndroid Build Coastguard Worker   switch (wm->wmtype) {
223*77c1e3ccSAndroid Build Coastguard Worker     case IDENTITY:
224*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[0] == 0);
225*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[1] == 0);
226*77c1e3ccSAndroid Build Coastguard Worker       AOM_FALLTHROUGH_INTENDED;
227*77c1e3ccSAndroid Build Coastguard Worker     case TRANSLATION:
228*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[2] == 1 << WARPEDMODEL_PREC_BITS);
229*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[3] == 0);
230*77c1e3ccSAndroid Build Coastguard Worker       AOM_FALLTHROUGH_INTENDED;
231*77c1e3ccSAndroid Build Coastguard Worker     case ROTZOOM:
232*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[4] == -wm->wmmat[3]);
233*77c1e3ccSAndroid Build Coastguard Worker       assert(wm->wmmat[5] == wm->wmmat[2]);
234*77c1e3ccSAndroid Build Coastguard Worker       AOM_FALLTHROUGH_INTENDED;
235*77c1e3ccSAndroid Build Coastguard Worker     case AFFINE: break;
236*77c1e3ccSAndroid Build Coastguard Worker     default: assert(0 && "Bad wmtype");
237*77c1e3ccSAndroid Build Coastguard Worker   }
238*77c1e3ccSAndroid Build Coastguard Worker }
239*77c1e3ccSAndroid Build Coastguard Worker #endif  // NDEBUG
240*77c1e3ccSAndroid Build Coastguard Worker 
241*77c1e3ccSAndroid Build Coastguard Worker // Returns 1 on success or 0 on an invalid affine set
av1_get_shear_params(WarpedMotionParams * wm)242*77c1e3ccSAndroid Build Coastguard Worker int av1_get_shear_params(WarpedMotionParams *wm) {
243*77c1e3ccSAndroid Build Coastguard Worker #ifndef NDEBUG
244*77c1e3ccSAndroid Build Coastguard Worker   // Check that models have been constructed sensibly
245*77c1e3ccSAndroid Build Coastguard Worker   // This is a good place to check, because this function does not need to
246*77c1e3ccSAndroid Build Coastguard Worker   // be called until after model construction is complete, but must be called
247*77c1e3ccSAndroid Build Coastguard Worker   // before the model can be used for prediction.
248*77c1e3ccSAndroid Build Coastguard Worker   check_model_consistency(wm);
249*77c1e3ccSAndroid Build Coastguard Worker #endif  // NDEBUG
250*77c1e3ccSAndroid Build Coastguard Worker 
251*77c1e3ccSAndroid Build Coastguard Worker   const int32_t *mat = wm->wmmat;
252*77c1e3ccSAndroid Build Coastguard Worker   if (!is_affine_valid(wm)) return 0;
253*77c1e3ccSAndroid Build Coastguard Worker 
254*77c1e3ccSAndroid Build Coastguard Worker   wm->alpha =
255*77c1e3ccSAndroid Build Coastguard Worker       clamp(mat[2] - (1 << WARPEDMODEL_PREC_BITS), INT16_MIN, INT16_MAX);
256*77c1e3ccSAndroid Build Coastguard Worker   wm->beta = clamp(mat[3], INT16_MIN, INT16_MAX);
257*77c1e3ccSAndroid Build Coastguard Worker   int16_t shift;
258*77c1e3ccSAndroid Build Coastguard Worker   int16_t y = resolve_divisor_32(abs(mat[2]), &shift) * (mat[2] < 0 ? -1 : 1);
259*77c1e3ccSAndroid Build Coastguard Worker   int64_t v = ((int64_t)mat[4] * (1 << WARPEDMODEL_PREC_BITS)) * y;
260*77c1e3ccSAndroid Build Coastguard Worker   wm->gamma =
261*77c1e3ccSAndroid Build Coastguard Worker       clamp((int)ROUND_POWER_OF_TWO_SIGNED_64(v, shift), INT16_MIN, INT16_MAX);
262*77c1e3ccSAndroid Build Coastguard Worker   v = ((int64_t)mat[3] * mat[4]) * y;
263*77c1e3ccSAndroid Build Coastguard Worker   wm->delta = clamp(mat[5] - (int)ROUND_POWER_OF_TWO_SIGNED_64(v, shift) -
264*77c1e3ccSAndroid Build Coastguard Worker                         (1 << WARPEDMODEL_PREC_BITS),
265*77c1e3ccSAndroid Build Coastguard Worker                     INT16_MIN, INT16_MAX);
266*77c1e3ccSAndroid Build Coastguard Worker 
267*77c1e3ccSAndroid Build Coastguard Worker   wm->alpha = ROUND_POWER_OF_TWO_SIGNED(wm->alpha, WARP_PARAM_REDUCE_BITS) *
268*77c1e3ccSAndroid Build Coastguard Worker               (1 << WARP_PARAM_REDUCE_BITS);
269*77c1e3ccSAndroid Build Coastguard Worker   wm->beta = ROUND_POWER_OF_TWO_SIGNED(wm->beta, WARP_PARAM_REDUCE_BITS) *
270*77c1e3ccSAndroid Build Coastguard Worker              (1 << WARP_PARAM_REDUCE_BITS);
271*77c1e3ccSAndroid Build Coastguard Worker   wm->gamma = ROUND_POWER_OF_TWO_SIGNED(wm->gamma, WARP_PARAM_REDUCE_BITS) *
272*77c1e3ccSAndroid Build Coastguard Worker               (1 << WARP_PARAM_REDUCE_BITS);
273*77c1e3ccSAndroid Build Coastguard Worker   wm->delta = ROUND_POWER_OF_TWO_SIGNED(wm->delta, WARP_PARAM_REDUCE_BITS) *
274*77c1e3ccSAndroid Build Coastguard Worker               (1 << WARP_PARAM_REDUCE_BITS);
275*77c1e3ccSAndroid Build Coastguard Worker 
276*77c1e3ccSAndroid Build Coastguard Worker   if (!is_affine_shear_allowed(wm->alpha, wm->beta, wm->gamma, wm->delta))
277*77c1e3ccSAndroid Build Coastguard Worker     return 0;
278*77c1e3ccSAndroid Build Coastguard Worker 
279*77c1e3ccSAndroid Build Coastguard Worker   return 1;
280*77c1e3ccSAndroid Build Coastguard Worker }
281*77c1e3ccSAndroid Build Coastguard Worker 
282*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
283*77c1e3ccSAndroid Build Coastguard Worker /* Note: For an explanation of the warp algorithm, and some notes on bit widths
284*77c1e3ccSAndroid Build Coastguard Worker     for hardware implementations, see the comments above av1_warp_affine_c
285*77c1e3ccSAndroid Build Coastguard Worker */
av1_highbd_warp_affine_c(const int32_t * mat,const uint16_t * ref,int width,int height,int stride,uint16_t * pred,int p_col,int p_row,int p_width,int p_height,int p_stride,int subsampling_x,int subsampling_y,int bd,ConvolveParams * conv_params,int16_t alpha,int16_t beta,int16_t gamma,int16_t delta)286*77c1e3ccSAndroid Build Coastguard Worker void av1_highbd_warp_affine_c(const int32_t *mat, const uint16_t *ref,
287*77c1e3ccSAndroid Build Coastguard Worker                               int width, int height, int stride, uint16_t *pred,
288*77c1e3ccSAndroid Build Coastguard Worker                               int p_col, int p_row, int p_width, int p_height,
289*77c1e3ccSAndroid Build Coastguard Worker                               int p_stride, int subsampling_x,
290*77c1e3ccSAndroid Build Coastguard Worker                               int subsampling_y, int bd,
291*77c1e3ccSAndroid Build Coastguard Worker                               ConvolveParams *conv_params, int16_t alpha,
292*77c1e3ccSAndroid Build Coastguard Worker                               int16_t beta, int16_t gamma, int16_t delta) {
293*77c1e3ccSAndroid Build Coastguard Worker   int32_t tmp[15 * 8];
294*77c1e3ccSAndroid Build Coastguard Worker   const int reduce_bits_horiz = conv_params->round_0;
295*77c1e3ccSAndroid Build Coastguard Worker   const int reduce_bits_vert = conv_params->is_compound
296*77c1e3ccSAndroid Build Coastguard Worker                                    ? conv_params->round_1
297*77c1e3ccSAndroid Build Coastguard Worker                                    : 2 * FILTER_BITS - reduce_bits_horiz;
298*77c1e3ccSAndroid Build Coastguard Worker   const int max_bits_horiz = bd + FILTER_BITS + 1 - reduce_bits_horiz;
299*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits_horiz = bd + FILTER_BITS - 1;
300*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits_vert = bd + 2 * FILTER_BITS - reduce_bits_horiz;
301*77c1e3ccSAndroid Build Coastguard Worker   const int round_bits =
302*77c1e3ccSAndroid Build Coastguard Worker       2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
303*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
304*77c1e3ccSAndroid Build Coastguard Worker   (void)max_bits_horiz;
305*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(conv_params->is_compound, conv_params->dst != NULL));
306*77c1e3ccSAndroid Build Coastguard Worker 
307*77c1e3ccSAndroid Build Coastguard Worker   // Check that, even with 12-bit input, the intermediate values will fit
308*77c1e3ccSAndroid Build Coastguard Worker   // into an unsigned 16-bit intermediate array.
309*77c1e3ccSAndroid Build Coastguard Worker   assert(bd + FILTER_BITS + 2 - conv_params->round_0 <= 16);
310*77c1e3ccSAndroid Build Coastguard Worker 
311*77c1e3ccSAndroid Build Coastguard Worker   for (int i = p_row; i < p_row + p_height; i += 8) {
312*77c1e3ccSAndroid Build Coastguard Worker     for (int j = p_col; j < p_col + p_width; j += 8) {
313*77c1e3ccSAndroid Build Coastguard Worker       // Calculate the center of this 8x8 block,
314*77c1e3ccSAndroid Build Coastguard Worker       // project to luma coordinates (if in a subsampled chroma plane),
315*77c1e3ccSAndroid Build Coastguard Worker       // apply the affine transformation,
316*77c1e3ccSAndroid Build Coastguard Worker       // then convert back to the original coordinates (if necessary)
317*77c1e3ccSAndroid Build Coastguard Worker       const int32_t src_x = (j + 4) << subsampling_x;
318*77c1e3ccSAndroid Build Coastguard Worker       const int32_t src_y = (i + 4) << subsampling_y;
319*77c1e3ccSAndroid Build Coastguard Worker       const int64_t dst_x =
320*77c1e3ccSAndroid Build Coastguard Worker           (int64_t)mat[2] * src_x + (int64_t)mat[3] * src_y + (int64_t)mat[0];
321*77c1e3ccSAndroid Build Coastguard Worker       const int64_t dst_y =
322*77c1e3ccSAndroid Build Coastguard Worker           (int64_t)mat[4] * src_x + (int64_t)mat[5] * src_y + (int64_t)mat[1];
323*77c1e3ccSAndroid Build Coastguard Worker       const int64_t x4 = dst_x >> subsampling_x;
324*77c1e3ccSAndroid Build Coastguard Worker       const int64_t y4 = dst_y >> subsampling_y;
325*77c1e3ccSAndroid Build Coastguard Worker 
326*77c1e3ccSAndroid Build Coastguard Worker       const int32_t ix4 = (int32_t)(x4 >> WARPEDMODEL_PREC_BITS);
327*77c1e3ccSAndroid Build Coastguard Worker       int32_t sx4 = x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
328*77c1e3ccSAndroid Build Coastguard Worker       const int32_t iy4 = (int32_t)(y4 >> WARPEDMODEL_PREC_BITS);
329*77c1e3ccSAndroid Build Coastguard Worker       int32_t sy4 = y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
330*77c1e3ccSAndroid Build Coastguard Worker 
331*77c1e3ccSAndroid Build Coastguard Worker       sx4 += alpha * (-4) + beta * (-4);
332*77c1e3ccSAndroid Build Coastguard Worker       sy4 += gamma * (-4) + delta * (-4);
333*77c1e3ccSAndroid Build Coastguard Worker 
334*77c1e3ccSAndroid Build Coastguard Worker       sx4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
335*77c1e3ccSAndroid Build Coastguard Worker       sy4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
336*77c1e3ccSAndroid Build Coastguard Worker 
337*77c1e3ccSAndroid Build Coastguard Worker       // Horizontal filter
338*77c1e3ccSAndroid Build Coastguard Worker       for (int k = -7; k < 8; ++k) {
339*77c1e3ccSAndroid Build Coastguard Worker         const int iy = clamp(iy4 + k, 0, height - 1);
340*77c1e3ccSAndroid Build Coastguard Worker 
341*77c1e3ccSAndroid Build Coastguard Worker         int sx = sx4 + beta * (k + 4);
342*77c1e3ccSAndroid Build Coastguard Worker         for (int l = -4; l < 4; ++l) {
343*77c1e3ccSAndroid Build Coastguard Worker           int ix = ix4 + l - 3;
344*77c1e3ccSAndroid Build Coastguard Worker           const int offs = ROUND_POWER_OF_TWO(sx, WARPEDDIFF_PREC_BITS) +
345*77c1e3ccSAndroid Build Coastguard Worker                            WARPEDPIXEL_PREC_SHIFTS;
346*77c1e3ccSAndroid Build Coastguard Worker           assert(offs >= 0 && offs <= WARPEDPIXEL_PREC_SHIFTS * 3);
347*77c1e3ccSAndroid Build Coastguard Worker           const int16_t *coeffs = av1_warped_filter[offs];
348*77c1e3ccSAndroid Build Coastguard Worker 
349*77c1e3ccSAndroid Build Coastguard Worker           int32_t sum = 1 << offset_bits_horiz;
350*77c1e3ccSAndroid Build Coastguard Worker           for (int m = 0; m < 8; ++m) {
351*77c1e3ccSAndroid Build Coastguard Worker             const int sample_x = clamp(ix + m, 0, width - 1);
352*77c1e3ccSAndroid Build Coastguard Worker             sum += ref[iy * stride + sample_x] * coeffs[m];
353*77c1e3ccSAndroid Build Coastguard Worker           }
354*77c1e3ccSAndroid Build Coastguard Worker           sum = ROUND_POWER_OF_TWO(sum, reduce_bits_horiz);
355*77c1e3ccSAndroid Build Coastguard Worker           assert(0 <= sum && sum < (1 << max_bits_horiz));
356*77c1e3ccSAndroid Build Coastguard Worker           tmp[(k + 7) * 8 + (l + 4)] = sum;
357*77c1e3ccSAndroid Build Coastguard Worker           sx += alpha;
358*77c1e3ccSAndroid Build Coastguard Worker         }
359*77c1e3ccSAndroid Build Coastguard Worker       }
360*77c1e3ccSAndroid Build Coastguard Worker 
361*77c1e3ccSAndroid Build Coastguard Worker       // Vertical filter
362*77c1e3ccSAndroid Build Coastguard Worker       for (int k = -4; k < AOMMIN(4, p_row + p_height - i - 4); ++k) {
363*77c1e3ccSAndroid Build Coastguard Worker         int sy = sy4 + delta * (k + 4);
364*77c1e3ccSAndroid Build Coastguard Worker         for (int l = -4; l < AOMMIN(4, p_col + p_width - j - 4); ++l) {
365*77c1e3ccSAndroid Build Coastguard Worker           const int offs = ROUND_POWER_OF_TWO(sy, WARPEDDIFF_PREC_BITS) +
366*77c1e3ccSAndroid Build Coastguard Worker                            WARPEDPIXEL_PREC_SHIFTS;
367*77c1e3ccSAndroid Build Coastguard Worker           assert(offs >= 0 && offs <= WARPEDPIXEL_PREC_SHIFTS * 3);
368*77c1e3ccSAndroid Build Coastguard Worker           const int16_t *coeffs = av1_warped_filter[offs];
369*77c1e3ccSAndroid Build Coastguard Worker 
370*77c1e3ccSAndroid Build Coastguard Worker           int32_t sum = 1 << offset_bits_vert;
371*77c1e3ccSAndroid Build Coastguard Worker           for (int m = 0; m < 8; ++m) {
372*77c1e3ccSAndroid Build Coastguard Worker             sum += tmp[(k + m + 4) * 8 + (l + 4)] * coeffs[m];
373*77c1e3ccSAndroid Build Coastguard Worker           }
374*77c1e3ccSAndroid Build Coastguard Worker 
375*77c1e3ccSAndroid Build Coastguard Worker           if (conv_params->is_compound) {
376*77c1e3ccSAndroid Build Coastguard Worker             CONV_BUF_TYPE *p =
377*77c1e3ccSAndroid Build Coastguard Worker                 &conv_params
378*77c1e3ccSAndroid Build Coastguard Worker                      ->dst[(i - p_row + k + 4) * conv_params->dst_stride +
379*77c1e3ccSAndroid Build Coastguard Worker                            (j - p_col + l + 4)];
380*77c1e3ccSAndroid Build Coastguard Worker             sum = ROUND_POWER_OF_TWO(sum, reduce_bits_vert);
381*77c1e3ccSAndroid Build Coastguard Worker             if (conv_params->do_average) {
382*77c1e3ccSAndroid Build Coastguard Worker               uint16_t *dst16 =
383*77c1e3ccSAndroid Build Coastguard Worker                   &pred[(i - p_row + k + 4) * p_stride + (j - p_col + l + 4)];
384*77c1e3ccSAndroid Build Coastguard Worker               int32_t tmp32 = *p;
385*77c1e3ccSAndroid Build Coastguard Worker               if (conv_params->use_dist_wtd_comp_avg) {
386*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 * conv_params->fwd_offset +
387*77c1e3ccSAndroid Build Coastguard Worker                         sum * conv_params->bck_offset;
388*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 >> DIST_PRECISION_BITS;
389*77c1e3ccSAndroid Build Coastguard Worker               } else {
390*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 += sum;
391*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 >> 1;
392*77c1e3ccSAndroid Build Coastguard Worker               }
393*77c1e3ccSAndroid Build Coastguard Worker               tmp32 = tmp32 - (1 << (offset_bits - conv_params->round_1)) -
394*77c1e3ccSAndroid Build Coastguard Worker                       (1 << (offset_bits - conv_params->round_1 - 1));
395*77c1e3ccSAndroid Build Coastguard Worker               *dst16 =
396*77c1e3ccSAndroid Build Coastguard Worker                   clip_pixel_highbd(ROUND_POWER_OF_TWO(tmp32, round_bits), bd);
397*77c1e3ccSAndroid Build Coastguard Worker             } else {
398*77c1e3ccSAndroid Build Coastguard Worker               *p = sum;
399*77c1e3ccSAndroid Build Coastguard Worker             }
400*77c1e3ccSAndroid Build Coastguard Worker           } else {
401*77c1e3ccSAndroid Build Coastguard Worker             uint16_t *p =
402*77c1e3ccSAndroid Build Coastguard Worker                 &pred[(i - p_row + k + 4) * p_stride + (j - p_col + l + 4)];
403*77c1e3ccSAndroid Build Coastguard Worker             sum = ROUND_POWER_OF_TWO(sum, reduce_bits_vert);
404*77c1e3ccSAndroid Build Coastguard Worker             assert(0 <= sum && sum < (1 << (bd + 2)));
405*77c1e3ccSAndroid Build Coastguard Worker             *p = clip_pixel_highbd(sum - (1 << (bd - 1)) - (1 << bd), bd);
406*77c1e3ccSAndroid Build Coastguard Worker           }
407*77c1e3ccSAndroid Build Coastguard Worker           sy += gamma;
408*77c1e3ccSAndroid Build Coastguard Worker         }
409*77c1e3ccSAndroid Build Coastguard Worker       }
410*77c1e3ccSAndroid Build Coastguard Worker     }
411*77c1e3ccSAndroid Build Coastguard Worker   }
412*77c1e3ccSAndroid Build Coastguard Worker }
413*77c1e3ccSAndroid Build Coastguard Worker 
highbd_warp_plane(WarpedMotionParams * wm,const uint16_t * const ref,int width,int height,int stride,uint16_t * const pred,int p_col,int p_row,int p_width,int p_height,int p_stride,int subsampling_x,int subsampling_y,int bd,ConvolveParams * conv_params)414*77c1e3ccSAndroid Build Coastguard Worker void highbd_warp_plane(WarpedMotionParams *wm, const uint16_t *const ref,
415*77c1e3ccSAndroid Build Coastguard Worker                        int width, int height, int stride, uint16_t *const pred,
416*77c1e3ccSAndroid Build Coastguard Worker                        int p_col, int p_row, int p_width, int p_height,
417*77c1e3ccSAndroid Build Coastguard Worker                        int p_stride, int subsampling_x, int subsampling_y,
418*77c1e3ccSAndroid Build Coastguard Worker                        int bd, ConvolveParams *conv_params) {
419*77c1e3ccSAndroid Build Coastguard Worker   const int32_t *const mat = wm->wmmat;
420*77c1e3ccSAndroid Build Coastguard Worker   const int16_t alpha = wm->alpha;
421*77c1e3ccSAndroid Build Coastguard Worker   const int16_t beta = wm->beta;
422*77c1e3ccSAndroid Build Coastguard Worker   const int16_t gamma = wm->gamma;
423*77c1e3ccSAndroid Build Coastguard Worker   const int16_t delta = wm->delta;
424*77c1e3ccSAndroid Build Coastguard Worker 
425*77c1e3ccSAndroid Build Coastguard Worker   av1_highbd_warp_affine(mat, ref, width, height, stride, pred, p_col, p_row,
426*77c1e3ccSAndroid Build Coastguard Worker                          p_width, p_height, p_stride, subsampling_x,
427*77c1e3ccSAndroid Build Coastguard Worker                          subsampling_y, bd, conv_params, alpha, beta, gamma,
428*77c1e3ccSAndroid Build Coastguard Worker                          delta);
429*77c1e3ccSAndroid Build Coastguard Worker }
430*77c1e3ccSAndroid Build Coastguard Worker #endif  // CONFIG_AV1_HIGHBITDEPTH
431*77c1e3ccSAndroid Build Coastguard Worker 
432*77c1e3ccSAndroid Build Coastguard Worker /* The warp filter for ROTZOOM and AFFINE models works as follows:
433*77c1e3ccSAndroid Build Coastguard Worker    * Split the input into 8x8 blocks
434*77c1e3ccSAndroid Build Coastguard Worker    * For each block, project the point (4, 4) within the block, to get the
435*77c1e3ccSAndroid Build Coastguard Worker      overall block position. Split into integer and fractional coordinates,
436*77c1e3ccSAndroid Build Coastguard Worker      maintaining full WARPEDMODEL precision
437*77c1e3ccSAndroid Build Coastguard Worker    * Filter horizontally: Generate 15 rows of 8 pixels each. Each pixel gets a
438*77c1e3ccSAndroid Build Coastguard Worker      variable horizontal offset. This means that, while the rows of the
439*77c1e3ccSAndroid Build Coastguard Worker      intermediate buffer align with the rows of the *reference* image, the
440*77c1e3ccSAndroid Build Coastguard Worker      columns align with the columns of the *destination* image.
441*77c1e3ccSAndroid Build Coastguard Worker    * Filter vertically: Generate the output block (up to 8x8 pixels, but if the
442*77c1e3ccSAndroid Build Coastguard Worker      destination is too small we crop the output at this stage). Each pixel has
443*77c1e3ccSAndroid Build Coastguard Worker      a variable vertical offset, so that the resulting rows are aligned with
444*77c1e3ccSAndroid Build Coastguard Worker      the rows of the destination image.
445*77c1e3ccSAndroid Build Coastguard Worker 
446*77c1e3ccSAndroid Build Coastguard Worker    To accomplish these alignments, we factor the warp matrix as a
447*77c1e3ccSAndroid Build Coastguard Worker    product of two shear / asymmetric zoom matrices:
448*77c1e3ccSAndroid Build Coastguard Worker    / a b \  = /   1       0    \ * / 1+alpha  beta \
449*77c1e3ccSAndroid Build Coastguard Worker    \ c d /    \ gamma  1+delta /   \    0      1   /
450*77c1e3ccSAndroid Build Coastguard Worker    where a, b, c, d are wmmat[2], wmmat[3], wmmat[4], wmmat[5] respectively.
451*77c1e3ccSAndroid Build Coastguard Worker    The horizontal shear (with alpha and beta) is applied first,
452*77c1e3ccSAndroid Build Coastguard Worker    then the vertical shear (with gamma and delta) is applied second.
453*77c1e3ccSAndroid Build Coastguard Worker 
454*77c1e3ccSAndroid Build Coastguard Worker    The only limitation is that, to fit this in a fixed 8-tap filter size,
455*77c1e3ccSAndroid Build Coastguard Worker    the fractional pixel offsets must be at most +-1. Since the horizontal filter
456*77c1e3ccSAndroid Build Coastguard Worker    generates 15 rows of 8 columns, and the initial point we project is at (4, 4)
457*77c1e3ccSAndroid Build Coastguard Worker    within the block, the parameters must satisfy
458*77c1e3ccSAndroid Build Coastguard Worker    4 * |alpha| + 7 * |beta| <= 1   and   4 * |gamma| + 4 * |delta| <= 1
459*77c1e3ccSAndroid Build Coastguard Worker    for this filter to be applicable.
460*77c1e3ccSAndroid Build Coastguard Worker 
461*77c1e3ccSAndroid Build Coastguard Worker    Note: This function assumes that the caller has done all of the relevant
462*77c1e3ccSAndroid Build Coastguard Worker    checks, ie. that we have a ROTZOOM or AFFINE model, that wm[4] and wm[5]
463*77c1e3ccSAndroid Build Coastguard Worker    are set appropriately (if using a ROTZOOM model), and that alpha, beta,
464*77c1e3ccSAndroid Build Coastguard Worker    gamma, delta are all in range.
465*77c1e3ccSAndroid Build Coastguard Worker 
466*77c1e3ccSAndroid Build Coastguard Worker    TODO(rachelbarker): Maybe support scaled references?
467*77c1e3ccSAndroid Build Coastguard Worker */
468*77c1e3ccSAndroid Build Coastguard Worker /* A note on hardware implementation:
469*77c1e3ccSAndroid Build Coastguard Worker     The warp filter is intended to be implementable using the same hardware as
470*77c1e3ccSAndroid Build Coastguard Worker     the high-precision convolve filters from the loop-restoration and
471*77c1e3ccSAndroid Build Coastguard Worker     convolve-round experiments.
472*77c1e3ccSAndroid Build Coastguard Worker 
473*77c1e3ccSAndroid Build Coastguard Worker     For a single filter stage, considering all of the coefficient sets for the
474*77c1e3ccSAndroid Build Coastguard Worker     warp filter and the regular convolution filter, an input in the range
475*77c1e3ccSAndroid Build Coastguard Worker     [0, 2^k - 1] is mapped into the range [-56 * (2^k - 1), 184 * (2^k - 1)]
476*77c1e3ccSAndroid Build Coastguard Worker     before rounding.
477*77c1e3ccSAndroid Build Coastguard Worker 
478*77c1e3ccSAndroid Build Coastguard Worker     Allowing for some changes to the filter coefficient sets, call the range
479*77c1e3ccSAndroid Build Coastguard Worker     [-64 * 2^k, 192 * 2^k]. Then, if we initialize the accumulator to 64 * 2^k,
480*77c1e3ccSAndroid Build Coastguard Worker     we can replace this by the range [0, 256 * 2^k], which can be stored in an
481*77c1e3ccSAndroid Build Coastguard Worker     unsigned value with 8 + k bits.
482*77c1e3ccSAndroid Build Coastguard Worker 
483*77c1e3ccSAndroid Build Coastguard Worker     This allows the derivation of the appropriate bit widths and offsets for
484*77c1e3ccSAndroid Build Coastguard Worker     the various intermediate values: If
485*77c1e3ccSAndroid Build Coastguard Worker 
486*77c1e3ccSAndroid Build Coastguard Worker     F := FILTER_BITS = 7 (or else the above ranges need adjusting)
487*77c1e3ccSAndroid Build Coastguard Worker          So a *single* filter stage maps a k-bit input to a (k + F + 1)-bit
488*77c1e3ccSAndroid Build Coastguard Worker          intermediate value.
489*77c1e3ccSAndroid Build Coastguard Worker     H := ROUND0_BITS
490*77c1e3ccSAndroid Build Coastguard Worker     V := VERSHEAR_REDUCE_PREC_BITS
491*77c1e3ccSAndroid Build Coastguard Worker     (and note that we must have H + V = 2*F for the output to have the same
492*77c1e3ccSAndroid Build Coastguard Worker      scale as the input)
493*77c1e3ccSAndroid Build Coastguard Worker 
494*77c1e3ccSAndroid Build Coastguard Worker     then we end up with the following offsets and ranges:
495*77c1e3ccSAndroid Build Coastguard Worker     Horizontal filter: Apply an offset of 1 << (bd + F - 1), sum fits into a
496*77c1e3ccSAndroid Build Coastguard Worker                        uint{bd + F + 1}
497*77c1e3ccSAndroid Build Coastguard Worker     After rounding: The values stored in 'tmp' fit into a uint{bd + F + 1 - H}.
498*77c1e3ccSAndroid Build Coastguard Worker     Vertical filter: Apply an offset of 1 << (bd + 2*F - H), sum fits into a
499*77c1e3ccSAndroid Build Coastguard Worker                      uint{bd + 2*F + 2 - H}
500*77c1e3ccSAndroid Build Coastguard Worker     After rounding: The final value, before undoing the offset, fits into a
501*77c1e3ccSAndroid Build Coastguard Worker                     uint{bd + 2}.
502*77c1e3ccSAndroid Build Coastguard Worker 
503*77c1e3ccSAndroid Build Coastguard Worker     Then we need to undo the offsets before clamping to a pixel. Note that,
504*77c1e3ccSAndroid Build Coastguard Worker     if we do this at the end, the amount to subtract is actually independent
505*77c1e3ccSAndroid Build Coastguard Worker     of H and V:
506*77c1e3ccSAndroid Build Coastguard Worker 
507*77c1e3ccSAndroid Build Coastguard Worker     offset to subtract = (1 << ((bd + F - 1) - H + F - V)) +
508*77c1e3ccSAndroid Build Coastguard Worker                          (1 << ((bd + 2*F - H) - V))
509*77c1e3ccSAndroid Build Coastguard Worker                       == (1 << (bd - 1)) + (1 << bd)
510*77c1e3ccSAndroid Build Coastguard Worker 
511*77c1e3ccSAndroid Build Coastguard Worker     This allows us to entirely avoid clamping in both the warp filter and
512*77c1e3ccSAndroid Build Coastguard Worker     the convolve-round experiment. As of the time of writing, the Wiener filter
513*77c1e3ccSAndroid Build Coastguard Worker     from loop-restoration can encode a central coefficient up to 216, which
514*77c1e3ccSAndroid Build Coastguard Worker     leads to a maximum value of about 282 * 2^k after applying the offset.
515*77c1e3ccSAndroid Build Coastguard Worker     So in that case we still need to clamp.
516*77c1e3ccSAndroid Build Coastguard Worker */
av1_warp_affine_c(const int32_t * mat,const uint8_t * ref,int width,int height,int stride,uint8_t * pred,int p_col,int p_row,int p_width,int p_height,int p_stride,int subsampling_x,int subsampling_y,ConvolveParams * conv_params,int16_t alpha,int16_t beta,int16_t gamma,int16_t delta)517*77c1e3ccSAndroid Build Coastguard Worker void av1_warp_affine_c(const int32_t *mat, const uint8_t *ref, int width,
518*77c1e3ccSAndroid Build Coastguard Worker                        int height, int stride, uint8_t *pred, int p_col,
519*77c1e3ccSAndroid Build Coastguard Worker                        int p_row, int p_width, int p_height, int p_stride,
520*77c1e3ccSAndroid Build Coastguard Worker                        int subsampling_x, int subsampling_y,
521*77c1e3ccSAndroid Build Coastguard Worker                        ConvolveParams *conv_params, int16_t alpha, int16_t beta,
522*77c1e3ccSAndroid Build Coastguard Worker                        int16_t gamma, int16_t delta) {
523*77c1e3ccSAndroid Build Coastguard Worker   int32_t tmp[15 * 8];
524*77c1e3ccSAndroid Build Coastguard Worker   const int bd = 8;
525*77c1e3ccSAndroid Build Coastguard Worker   const int reduce_bits_horiz = conv_params->round_0;
526*77c1e3ccSAndroid Build Coastguard Worker   const int reduce_bits_vert = conv_params->is_compound
527*77c1e3ccSAndroid Build Coastguard Worker                                    ? conv_params->round_1
528*77c1e3ccSAndroid Build Coastguard Worker                                    : 2 * FILTER_BITS - reduce_bits_horiz;
529*77c1e3ccSAndroid Build Coastguard Worker   const int max_bits_horiz = bd + FILTER_BITS + 1 - reduce_bits_horiz;
530*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits_horiz = bd + FILTER_BITS - 1;
531*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits_vert = bd + 2 * FILTER_BITS - reduce_bits_horiz;
532*77c1e3ccSAndroid Build Coastguard Worker   const int round_bits =
533*77c1e3ccSAndroid Build Coastguard Worker       2 * FILTER_BITS - conv_params->round_0 - conv_params->round_1;
534*77c1e3ccSAndroid Build Coastguard Worker   const int offset_bits = bd + 2 * FILTER_BITS - conv_params->round_0;
535*77c1e3ccSAndroid Build Coastguard Worker   (void)max_bits_horiz;
536*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(conv_params->is_compound, conv_params->dst != NULL));
537*77c1e3ccSAndroid Build Coastguard Worker   assert(IMPLIES(conv_params->do_average, conv_params->is_compound));
538*77c1e3ccSAndroid Build Coastguard Worker 
539*77c1e3ccSAndroid Build Coastguard Worker   for (int i = p_row; i < p_row + p_height; i += 8) {
540*77c1e3ccSAndroid Build Coastguard Worker     for (int j = p_col; j < p_col + p_width; j += 8) {
541*77c1e3ccSAndroid Build Coastguard Worker       // Calculate the center of this 8x8 block,
542*77c1e3ccSAndroid Build Coastguard Worker       // project to luma coordinates (if in a subsampled chroma plane),
543*77c1e3ccSAndroid Build Coastguard Worker       // apply the affine transformation,
544*77c1e3ccSAndroid Build Coastguard Worker       // then convert back to the original coordinates (if necessary)
545*77c1e3ccSAndroid Build Coastguard Worker       const int32_t src_x = (j + 4) << subsampling_x;
546*77c1e3ccSAndroid Build Coastguard Worker       const int32_t src_y = (i + 4) << subsampling_y;
547*77c1e3ccSAndroid Build Coastguard Worker       const int64_t dst_x =
548*77c1e3ccSAndroid Build Coastguard Worker           (int64_t)mat[2] * src_x + (int64_t)mat[3] * src_y + (int64_t)mat[0];
549*77c1e3ccSAndroid Build Coastguard Worker       const int64_t dst_y =
550*77c1e3ccSAndroid Build Coastguard Worker           (int64_t)mat[4] * src_x + (int64_t)mat[5] * src_y + (int64_t)mat[1];
551*77c1e3ccSAndroid Build Coastguard Worker       const int64_t x4 = dst_x >> subsampling_x;
552*77c1e3ccSAndroid Build Coastguard Worker       const int64_t y4 = dst_y >> subsampling_y;
553*77c1e3ccSAndroid Build Coastguard Worker 
554*77c1e3ccSAndroid Build Coastguard Worker       int32_t ix4 = (int32_t)(x4 >> WARPEDMODEL_PREC_BITS);
555*77c1e3ccSAndroid Build Coastguard Worker       int32_t sx4 = x4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
556*77c1e3ccSAndroid Build Coastguard Worker       int32_t iy4 = (int32_t)(y4 >> WARPEDMODEL_PREC_BITS);
557*77c1e3ccSAndroid Build Coastguard Worker       int32_t sy4 = y4 & ((1 << WARPEDMODEL_PREC_BITS) - 1);
558*77c1e3ccSAndroid Build Coastguard Worker 
559*77c1e3ccSAndroid Build Coastguard Worker       sx4 += alpha * (-4) + beta * (-4);
560*77c1e3ccSAndroid Build Coastguard Worker       sy4 += gamma * (-4) + delta * (-4);
561*77c1e3ccSAndroid Build Coastguard Worker 
562*77c1e3ccSAndroid Build Coastguard Worker       sx4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
563*77c1e3ccSAndroid Build Coastguard Worker       sy4 &= ~((1 << WARP_PARAM_REDUCE_BITS) - 1);
564*77c1e3ccSAndroid Build Coastguard Worker 
565*77c1e3ccSAndroid Build Coastguard Worker       // Horizontal filter
566*77c1e3ccSAndroid Build Coastguard Worker       for (int k = -7; k < 8; ++k) {
567*77c1e3ccSAndroid Build Coastguard Worker         // Clamp to top/bottom edge of the frame
568*77c1e3ccSAndroid Build Coastguard Worker         const int iy = clamp(iy4 + k, 0, height - 1);
569*77c1e3ccSAndroid Build Coastguard Worker 
570*77c1e3ccSAndroid Build Coastguard Worker         int sx = sx4 + beta * (k + 4);
571*77c1e3ccSAndroid Build Coastguard Worker 
572*77c1e3ccSAndroid Build Coastguard Worker         for (int l = -4; l < 4; ++l) {
573*77c1e3ccSAndroid Build Coastguard Worker           int ix = ix4 + l - 3;
574*77c1e3ccSAndroid Build Coastguard Worker           // At this point, sx = sx4 + alpha * l + beta * k
575*77c1e3ccSAndroid Build Coastguard Worker           const int offs = ROUND_POWER_OF_TWO(sx, WARPEDDIFF_PREC_BITS) +
576*77c1e3ccSAndroid Build Coastguard Worker                            WARPEDPIXEL_PREC_SHIFTS;
577*77c1e3ccSAndroid Build Coastguard Worker           assert(offs >= 0 && offs <= WARPEDPIXEL_PREC_SHIFTS * 3);
578*77c1e3ccSAndroid Build Coastguard Worker           const int16_t *coeffs = av1_warped_filter[offs];
579*77c1e3ccSAndroid Build Coastguard Worker 
580*77c1e3ccSAndroid Build Coastguard Worker           int32_t sum = 1 << offset_bits_horiz;
581*77c1e3ccSAndroid Build Coastguard Worker           for (int m = 0; m < 8; ++m) {
582*77c1e3ccSAndroid Build Coastguard Worker             // Clamp to left/right edge of the frame
583*77c1e3ccSAndroid Build Coastguard Worker             const int sample_x = clamp(ix + m, 0, width - 1);
584*77c1e3ccSAndroid Build Coastguard Worker 
585*77c1e3ccSAndroid Build Coastguard Worker             sum += ref[iy * stride + sample_x] * coeffs[m];
586*77c1e3ccSAndroid Build Coastguard Worker           }
587*77c1e3ccSAndroid Build Coastguard Worker           sum = ROUND_POWER_OF_TWO(sum, reduce_bits_horiz);
588*77c1e3ccSAndroid Build Coastguard Worker           assert(0 <= sum && sum < (1 << max_bits_horiz));
589*77c1e3ccSAndroid Build Coastguard Worker           tmp[(k + 7) * 8 + (l + 4)] = sum;
590*77c1e3ccSAndroid Build Coastguard Worker           sx += alpha;
591*77c1e3ccSAndroid Build Coastguard Worker         }
592*77c1e3ccSAndroid Build Coastguard Worker       }
593*77c1e3ccSAndroid Build Coastguard Worker 
594*77c1e3ccSAndroid Build Coastguard Worker       // Vertical filter
595*77c1e3ccSAndroid Build Coastguard Worker       for (int k = -4; k < AOMMIN(4, p_row + p_height - i - 4); ++k) {
596*77c1e3ccSAndroid Build Coastguard Worker         int sy = sy4 + delta * (k + 4);
597*77c1e3ccSAndroid Build Coastguard Worker         for (int l = -4; l < AOMMIN(4, p_col + p_width - j - 4); ++l) {
598*77c1e3ccSAndroid Build Coastguard Worker           // At this point, sy = sy4 + gamma * l + delta * k
599*77c1e3ccSAndroid Build Coastguard Worker           const int offs = ROUND_POWER_OF_TWO(sy, WARPEDDIFF_PREC_BITS) +
600*77c1e3ccSAndroid Build Coastguard Worker                            WARPEDPIXEL_PREC_SHIFTS;
601*77c1e3ccSAndroid Build Coastguard Worker           assert(offs >= 0 && offs <= WARPEDPIXEL_PREC_SHIFTS * 3);
602*77c1e3ccSAndroid Build Coastguard Worker           const int16_t *coeffs = av1_warped_filter[offs];
603*77c1e3ccSAndroid Build Coastguard Worker 
604*77c1e3ccSAndroid Build Coastguard Worker           int32_t sum = 1 << offset_bits_vert;
605*77c1e3ccSAndroid Build Coastguard Worker           for (int m = 0; m < 8; ++m) {
606*77c1e3ccSAndroid Build Coastguard Worker             sum += tmp[(k + m + 4) * 8 + (l + 4)] * coeffs[m];
607*77c1e3ccSAndroid Build Coastguard Worker           }
608*77c1e3ccSAndroid Build Coastguard Worker 
609*77c1e3ccSAndroid Build Coastguard Worker           if (conv_params->is_compound) {
610*77c1e3ccSAndroid Build Coastguard Worker             CONV_BUF_TYPE *p =
611*77c1e3ccSAndroid Build Coastguard Worker                 &conv_params
612*77c1e3ccSAndroid Build Coastguard Worker                      ->dst[(i - p_row + k + 4) * conv_params->dst_stride +
613*77c1e3ccSAndroid Build Coastguard Worker                            (j - p_col + l + 4)];
614*77c1e3ccSAndroid Build Coastguard Worker             sum = ROUND_POWER_OF_TWO(sum, reduce_bits_vert);
615*77c1e3ccSAndroid Build Coastguard Worker             if (conv_params->do_average) {
616*77c1e3ccSAndroid Build Coastguard Worker               uint8_t *dst8 =
617*77c1e3ccSAndroid Build Coastguard Worker                   &pred[(i - p_row + k + 4) * p_stride + (j - p_col + l + 4)];
618*77c1e3ccSAndroid Build Coastguard Worker               int32_t tmp32 = *p;
619*77c1e3ccSAndroid Build Coastguard Worker               if (conv_params->use_dist_wtd_comp_avg) {
620*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 * conv_params->fwd_offset +
621*77c1e3ccSAndroid Build Coastguard Worker                         sum * conv_params->bck_offset;
622*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 >> DIST_PRECISION_BITS;
623*77c1e3ccSAndroid Build Coastguard Worker               } else {
624*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 += sum;
625*77c1e3ccSAndroid Build Coastguard Worker                 tmp32 = tmp32 >> 1;
626*77c1e3ccSAndroid Build Coastguard Worker               }
627*77c1e3ccSAndroid Build Coastguard Worker               tmp32 = tmp32 - (1 << (offset_bits - conv_params->round_1)) -
628*77c1e3ccSAndroid Build Coastguard Worker                       (1 << (offset_bits - conv_params->round_1 - 1));
629*77c1e3ccSAndroid Build Coastguard Worker               *dst8 = clip_pixel(ROUND_POWER_OF_TWO(tmp32, round_bits));
630*77c1e3ccSAndroid Build Coastguard Worker             } else {
631*77c1e3ccSAndroid Build Coastguard Worker               *p = sum;
632*77c1e3ccSAndroid Build Coastguard Worker             }
633*77c1e3ccSAndroid Build Coastguard Worker           } else {
634*77c1e3ccSAndroid Build Coastguard Worker             uint8_t *p =
635*77c1e3ccSAndroid Build Coastguard Worker                 &pred[(i - p_row + k + 4) * p_stride + (j - p_col + l + 4)];
636*77c1e3ccSAndroid Build Coastguard Worker             sum = ROUND_POWER_OF_TWO(sum, reduce_bits_vert);
637*77c1e3ccSAndroid Build Coastguard Worker             assert(0 <= sum && sum < (1 << (bd + 2)));
638*77c1e3ccSAndroid Build Coastguard Worker             *p = clip_pixel(sum - (1 << (bd - 1)) - (1 << bd));
639*77c1e3ccSAndroid Build Coastguard Worker           }
640*77c1e3ccSAndroid Build Coastguard Worker           sy += gamma;
641*77c1e3ccSAndroid Build Coastguard Worker         }
642*77c1e3ccSAndroid Build Coastguard Worker       }
643*77c1e3ccSAndroid Build Coastguard Worker     }
644*77c1e3ccSAndroid Build Coastguard Worker   }
645*77c1e3ccSAndroid Build Coastguard Worker }
646*77c1e3ccSAndroid Build Coastguard Worker 
warp_plane(WarpedMotionParams * wm,const uint8_t * const ref,int width,int height,int stride,uint8_t * pred,int p_col,int p_row,int p_width,int p_height,int p_stride,int subsampling_x,int subsampling_y,ConvolveParams * conv_params)647*77c1e3ccSAndroid Build Coastguard Worker void warp_plane(WarpedMotionParams *wm, const uint8_t *const ref, int width,
648*77c1e3ccSAndroid Build Coastguard Worker                 int height, int stride, uint8_t *pred, int p_col, int p_row,
649*77c1e3ccSAndroid Build Coastguard Worker                 int p_width, int p_height, int p_stride, int subsampling_x,
650*77c1e3ccSAndroid Build Coastguard Worker                 int subsampling_y, ConvolveParams *conv_params) {
651*77c1e3ccSAndroid Build Coastguard Worker   const int32_t *const mat = wm->wmmat;
652*77c1e3ccSAndroid Build Coastguard Worker   const int16_t alpha = wm->alpha;
653*77c1e3ccSAndroid Build Coastguard Worker   const int16_t beta = wm->beta;
654*77c1e3ccSAndroid Build Coastguard Worker   const int16_t gamma = wm->gamma;
655*77c1e3ccSAndroid Build Coastguard Worker   const int16_t delta = wm->delta;
656*77c1e3ccSAndroid Build Coastguard Worker   av1_warp_affine(mat, ref, width, height, stride, pred, p_col, p_row, p_width,
657*77c1e3ccSAndroid Build Coastguard Worker                   p_height, p_stride, subsampling_x, subsampling_y, conv_params,
658*77c1e3ccSAndroid Build Coastguard Worker                   alpha, beta, gamma, delta);
659*77c1e3ccSAndroid Build Coastguard Worker }
660*77c1e3ccSAndroid Build Coastguard Worker 
av1_warp_plane(WarpedMotionParams * wm,int use_hbd,int bd,const uint8_t * ref,int width,int height,int stride,uint8_t * pred,int p_col,int p_row,int p_width,int p_height,int p_stride,int subsampling_x,int subsampling_y,ConvolveParams * conv_params)661*77c1e3ccSAndroid Build Coastguard Worker void av1_warp_plane(WarpedMotionParams *wm, int use_hbd, int bd,
662*77c1e3ccSAndroid Build Coastguard Worker                     const uint8_t *ref, int width, int height, int stride,
663*77c1e3ccSAndroid Build Coastguard Worker                     uint8_t *pred, int p_col, int p_row, int p_width,
664*77c1e3ccSAndroid Build Coastguard Worker                     int p_height, int p_stride, int subsampling_x,
665*77c1e3ccSAndroid Build Coastguard Worker                     int subsampling_y, ConvolveParams *conv_params) {
666*77c1e3ccSAndroid Build Coastguard Worker #if CONFIG_AV1_HIGHBITDEPTH
667*77c1e3ccSAndroid Build Coastguard Worker   if (use_hbd)
668*77c1e3ccSAndroid Build Coastguard Worker     highbd_warp_plane(wm, CONVERT_TO_SHORTPTR(ref), width, height, stride,
669*77c1e3ccSAndroid Build Coastguard Worker                       CONVERT_TO_SHORTPTR(pred), p_col, p_row, p_width,
670*77c1e3ccSAndroid Build Coastguard Worker                       p_height, p_stride, subsampling_x, subsampling_y, bd,
671*77c1e3ccSAndroid Build Coastguard Worker                       conv_params);
672*77c1e3ccSAndroid Build Coastguard Worker   else
673*77c1e3ccSAndroid Build Coastguard Worker     warp_plane(wm, ref, width, height, stride, pred, p_col, p_row, p_width,
674*77c1e3ccSAndroid Build Coastguard Worker                p_height, p_stride, subsampling_x, subsampling_y, conv_params);
675*77c1e3ccSAndroid Build Coastguard Worker #else
676*77c1e3ccSAndroid Build Coastguard Worker   (void)use_hbd;
677*77c1e3ccSAndroid Build Coastguard Worker   (void)bd;
678*77c1e3ccSAndroid Build Coastguard Worker   warp_plane(wm, ref, width, height, stride, pred, p_col, p_row, p_width,
679*77c1e3ccSAndroid Build Coastguard Worker              p_height, p_stride, subsampling_x, subsampling_y, conv_params);
680*77c1e3ccSAndroid Build Coastguard Worker #endif
681*77c1e3ccSAndroid Build Coastguard Worker }
682*77c1e3ccSAndroid Build Coastguard Worker 
683*77c1e3ccSAndroid Build Coastguard Worker #define LS_MV_MAX 256  // max mv in 1/8-pel
684*77c1e3ccSAndroid Build Coastguard Worker // Use LS_STEP = 8 so that 2 less bits needed for A, Bx, By.
685*77c1e3ccSAndroid Build Coastguard Worker #define LS_STEP 8
686*77c1e3ccSAndroid Build Coastguard Worker 
687*77c1e3ccSAndroid Build Coastguard Worker // Assuming LS_MV_MAX is < MAX_SB_SIZE * 8,
688*77c1e3ccSAndroid Build Coastguard Worker // the precision needed is:
689*77c1e3ccSAndroid Build Coastguard Worker //   (MAX_SB_SIZE_LOG2 + 3) [for sx * sx magnitude] +
690*77c1e3ccSAndroid Build Coastguard Worker //   (MAX_SB_SIZE_LOG2 + 4) [for sx * dx magnitude] +
691*77c1e3ccSAndroid Build Coastguard Worker //   1 [for sign] +
692*77c1e3ccSAndroid Build Coastguard Worker //   LEAST_SQUARES_SAMPLES_MAX_BITS
693*77c1e3ccSAndroid Build Coastguard Worker //        [for adding up to LEAST_SQUARES_SAMPLES_MAX samples]
694*77c1e3ccSAndroid Build Coastguard Worker // The value is 23
695*77c1e3ccSAndroid Build Coastguard Worker #define LS_MAT_RANGE_BITS \
696*77c1e3ccSAndroid Build Coastguard Worker   ((MAX_SB_SIZE_LOG2 + 4) * 2 + LEAST_SQUARES_SAMPLES_MAX_BITS)
697*77c1e3ccSAndroid Build Coastguard Worker 
698*77c1e3ccSAndroid Build Coastguard Worker // Bit-depth reduction from the full-range
699*77c1e3ccSAndroid Build Coastguard Worker #define LS_MAT_DOWN_BITS 2
700*77c1e3ccSAndroid Build Coastguard Worker 
701*77c1e3ccSAndroid Build Coastguard Worker // bits range of A, Bx and By after downshifting
702*77c1e3ccSAndroid Build Coastguard Worker #define LS_MAT_BITS (LS_MAT_RANGE_BITS - LS_MAT_DOWN_BITS)
703*77c1e3ccSAndroid Build Coastguard Worker #define LS_MAT_MIN (-(1 << (LS_MAT_BITS - 1)))
704*77c1e3ccSAndroid Build Coastguard Worker #define LS_MAT_MAX ((1 << (LS_MAT_BITS - 1)) - 1)
705*77c1e3ccSAndroid Build Coastguard Worker 
706*77c1e3ccSAndroid Build Coastguard Worker // By setting LS_STEP = 8, the least 2 bits of every elements in A, Bx, By are
707*77c1e3ccSAndroid Build Coastguard Worker // 0. So, we can reduce LS_MAT_RANGE_BITS(2) bits here.
708*77c1e3ccSAndroid Build Coastguard Worker #define LS_SQUARE(a)                                          \
709*77c1e3ccSAndroid Build Coastguard Worker   (((a) * (a)*4 + (a)*4 * LS_STEP + LS_STEP * LS_STEP * 2) >> \
710*77c1e3ccSAndroid Build Coastguard Worker    (2 + LS_MAT_DOWN_BITS))
711*77c1e3ccSAndroid Build Coastguard Worker #define LS_PRODUCT1(a, b)                                           \
712*77c1e3ccSAndroid Build Coastguard Worker   (((a) * (b)*4 + ((a) + (b)) * 2 * LS_STEP + LS_STEP * LS_STEP) >> \
713*77c1e3ccSAndroid Build Coastguard Worker    (2 + LS_MAT_DOWN_BITS))
714*77c1e3ccSAndroid Build Coastguard Worker #define LS_PRODUCT2(a, b)                                               \
715*77c1e3ccSAndroid Build Coastguard Worker   (((a) * (b)*4 + ((a) + (b)) * 2 * LS_STEP + LS_STEP * LS_STEP * 2) >> \
716*77c1e3ccSAndroid Build Coastguard Worker    (2 + LS_MAT_DOWN_BITS))
717*77c1e3ccSAndroid Build Coastguard Worker 
718*77c1e3ccSAndroid Build Coastguard Worker #define USE_LIMITED_PREC_MULT 0
719*77c1e3ccSAndroid Build Coastguard Worker 
720*77c1e3ccSAndroid Build Coastguard Worker #if USE_LIMITED_PREC_MULT
721*77c1e3ccSAndroid Build Coastguard Worker 
722*77c1e3ccSAndroid Build Coastguard Worker #define MUL_PREC_BITS 16
resolve_multiplier_64(uint64_t D,int16_t * shift)723*77c1e3ccSAndroid Build Coastguard Worker static uint16_t resolve_multiplier_64(uint64_t D, int16_t *shift) {
724*77c1e3ccSAndroid Build Coastguard Worker   int msb = 0;
725*77c1e3ccSAndroid Build Coastguard Worker   uint16_t mult = 0;
726*77c1e3ccSAndroid Build Coastguard Worker   *shift = 0;
727*77c1e3ccSAndroid Build Coastguard Worker   if (D != 0) {
728*77c1e3ccSAndroid Build Coastguard Worker     msb = (int16_t)((D >> 32) ? get_msb((unsigned int)(D >> 32)) + 32
729*77c1e3ccSAndroid Build Coastguard Worker                               : get_msb((unsigned int)D));
730*77c1e3ccSAndroid Build Coastguard Worker     if (msb >= MUL_PREC_BITS) {
731*77c1e3ccSAndroid Build Coastguard Worker       mult = (uint16_t)ROUND_POWER_OF_TWO_64(D, msb + 1 - MUL_PREC_BITS);
732*77c1e3ccSAndroid Build Coastguard Worker       *shift = msb + 1 - MUL_PREC_BITS;
733*77c1e3ccSAndroid Build Coastguard Worker     } else {
734*77c1e3ccSAndroid Build Coastguard Worker       mult = (uint16_t)D;
735*77c1e3ccSAndroid Build Coastguard Worker       *shift = 0;
736*77c1e3ccSAndroid Build Coastguard Worker     }
737*77c1e3ccSAndroid Build Coastguard Worker   }
738*77c1e3ccSAndroid Build Coastguard Worker   return mult;
739*77c1e3ccSAndroid Build Coastguard Worker }
740*77c1e3ccSAndroid Build Coastguard Worker 
get_mult_shift_ndiag(int64_t Px,int16_t iDet,int shift)741*77c1e3ccSAndroid Build Coastguard Worker static int32_t get_mult_shift_ndiag(int64_t Px, int16_t iDet, int shift) {
742*77c1e3ccSAndroid Build Coastguard Worker   int32_t ret;
743*77c1e3ccSAndroid Build Coastguard Worker   int16_t mshift;
744*77c1e3ccSAndroid Build Coastguard Worker   uint16_t Mul = resolve_multiplier_64(llabs(Px), &mshift);
745*77c1e3ccSAndroid Build Coastguard Worker   int32_t v = (int32_t)Mul * (int32_t)iDet * (Px < 0 ? -1 : 1);
746*77c1e3ccSAndroid Build Coastguard Worker   shift -= mshift;
747*77c1e3ccSAndroid Build Coastguard Worker   if (shift > 0) {
748*77c1e3ccSAndroid Build Coastguard Worker     return (int32_t)clamp(ROUND_POWER_OF_TWO_SIGNED(v, shift),
749*77c1e3ccSAndroid Build Coastguard Worker                           -WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
750*77c1e3ccSAndroid Build Coastguard Worker                           WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
751*77c1e3ccSAndroid Build Coastguard Worker   } else {
752*77c1e3ccSAndroid Build Coastguard Worker     return (int32_t)clamp(v * (1 << (-shift)),
753*77c1e3ccSAndroid Build Coastguard Worker                           -WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
754*77c1e3ccSAndroid Build Coastguard Worker                           WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
755*77c1e3ccSAndroid Build Coastguard Worker   }
756*77c1e3ccSAndroid Build Coastguard Worker   return ret;
757*77c1e3ccSAndroid Build Coastguard Worker }
758*77c1e3ccSAndroid Build Coastguard Worker 
get_mult_shift_diag(int64_t Px,int16_t iDet,int shift)759*77c1e3ccSAndroid Build Coastguard Worker static int32_t get_mult_shift_diag(int64_t Px, int16_t iDet, int shift) {
760*77c1e3ccSAndroid Build Coastguard Worker   int16_t mshift;
761*77c1e3ccSAndroid Build Coastguard Worker   uint16_t Mul = resolve_multiplier_64(llabs(Px), &mshift);
762*77c1e3ccSAndroid Build Coastguard Worker   int32_t v = (int32_t)Mul * (int32_t)iDet * (Px < 0 ? -1 : 1);
763*77c1e3ccSAndroid Build Coastguard Worker   shift -= mshift;
764*77c1e3ccSAndroid Build Coastguard Worker   if (shift > 0) {
765*77c1e3ccSAndroid Build Coastguard Worker     return (int32_t)clamp(
766*77c1e3ccSAndroid Build Coastguard Worker         ROUND_POWER_OF_TWO_SIGNED(v, shift),
767*77c1e3ccSAndroid Build Coastguard Worker         (1 << WARPEDMODEL_PREC_BITS) - WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
768*77c1e3ccSAndroid Build Coastguard Worker         (1 << WARPEDMODEL_PREC_BITS) + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
769*77c1e3ccSAndroid Build Coastguard Worker   } else {
770*77c1e3ccSAndroid Build Coastguard Worker     return (int32_t)clamp(
771*77c1e3ccSAndroid Build Coastguard Worker         v * (1 << (-shift)),
772*77c1e3ccSAndroid Build Coastguard Worker         (1 << WARPEDMODEL_PREC_BITS) - WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
773*77c1e3ccSAndroid Build Coastguard Worker         (1 << WARPEDMODEL_PREC_BITS) + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
774*77c1e3ccSAndroid Build Coastguard Worker   }
775*77c1e3ccSAndroid Build Coastguard Worker }
776*77c1e3ccSAndroid Build Coastguard Worker 
777*77c1e3ccSAndroid Build Coastguard Worker #else
778*77c1e3ccSAndroid Build Coastguard Worker 
get_mult_shift_ndiag(int64_t Px,int16_t iDet,int shift)779*77c1e3ccSAndroid Build Coastguard Worker static int32_t get_mult_shift_ndiag(int64_t Px, int16_t iDet, int shift) {
780*77c1e3ccSAndroid Build Coastguard Worker   int64_t v = Px * (int64_t)iDet;
781*77c1e3ccSAndroid Build Coastguard Worker   return (int32_t)clamp64(ROUND_POWER_OF_TWO_SIGNED_64(v, shift),
782*77c1e3ccSAndroid Build Coastguard Worker                           -WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
783*77c1e3ccSAndroid Build Coastguard Worker                           WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
784*77c1e3ccSAndroid Build Coastguard Worker }
785*77c1e3ccSAndroid Build Coastguard Worker 
get_mult_shift_diag(int64_t Px,int16_t iDet,int shift)786*77c1e3ccSAndroid Build Coastguard Worker static int32_t get_mult_shift_diag(int64_t Px, int16_t iDet, int shift) {
787*77c1e3ccSAndroid Build Coastguard Worker   int64_t v = Px * (int64_t)iDet;
788*77c1e3ccSAndroid Build Coastguard Worker   return (int32_t)clamp64(
789*77c1e3ccSAndroid Build Coastguard Worker       ROUND_POWER_OF_TWO_SIGNED_64(v, shift),
790*77c1e3ccSAndroid Build Coastguard Worker       (1 << WARPEDMODEL_PREC_BITS) - WARPEDMODEL_NONDIAGAFFINE_CLAMP + 1,
791*77c1e3ccSAndroid Build Coastguard Worker       (1 << WARPEDMODEL_PREC_BITS) + WARPEDMODEL_NONDIAGAFFINE_CLAMP - 1);
792*77c1e3ccSAndroid Build Coastguard Worker }
793*77c1e3ccSAndroid Build Coastguard Worker #endif  // USE_LIMITED_PREC_MULT
794*77c1e3ccSAndroid Build Coastguard Worker 
find_affine_int(int np,const int * pts1,const int * pts2,BLOCK_SIZE bsize,int mvy,int mvx,WarpedMotionParams * wm,int mi_row,int mi_col)795*77c1e3ccSAndroid Build Coastguard Worker static int find_affine_int(int np, const int *pts1, const int *pts2,
796*77c1e3ccSAndroid Build Coastguard Worker                            BLOCK_SIZE bsize, int mvy, int mvx,
797*77c1e3ccSAndroid Build Coastguard Worker                            WarpedMotionParams *wm, int mi_row, int mi_col) {
798*77c1e3ccSAndroid Build Coastguard Worker   int32_t A[2][2] = { { 0, 0 }, { 0, 0 } };
799*77c1e3ccSAndroid Build Coastguard Worker   int32_t Bx[2] = { 0, 0 };
800*77c1e3ccSAndroid Build Coastguard Worker   int32_t By[2] = { 0, 0 };
801*77c1e3ccSAndroid Build Coastguard Worker 
802*77c1e3ccSAndroid Build Coastguard Worker   const int bw = block_size_wide[bsize];
803*77c1e3ccSAndroid Build Coastguard Worker   const int bh = block_size_high[bsize];
804*77c1e3ccSAndroid Build Coastguard Worker   const int rsuy = bh / 2 - 1;
805*77c1e3ccSAndroid Build Coastguard Worker   const int rsux = bw / 2 - 1;
806*77c1e3ccSAndroid Build Coastguard Worker   const int suy = rsuy * 8;
807*77c1e3ccSAndroid Build Coastguard Worker   const int sux = rsux * 8;
808*77c1e3ccSAndroid Build Coastguard Worker   const int duy = suy + mvy;
809*77c1e3ccSAndroid Build Coastguard Worker   const int dux = sux + mvx;
810*77c1e3ccSAndroid Build Coastguard Worker 
811*77c1e3ccSAndroid Build Coastguard Worker   // Assume the center pixel of the block has exactly the same motion vector
812*77c1e3ccSAndroid Build Coastguard Worker   // as transmitted for the block. First shift the origin of the source
813*77c1e3ccSAndroid Build Coastguard Worker   // points to the block center, and the origin of the destination points to
814*77c1e3ccSAndroid Build Coastguard Worker   // the block center added to the motion vector transmitted.
815*77c1e3ccSAndroid Build Coastguard Worker   // Let (xi, yi) denote the source points and (xi', yi') denote destination
816*77c1e3ccSAndroid Build Coastguard Worker   // points after origin shfifting, for i = 0, 1, 2, .... n-1.
817*77c1e3ccSAndroid Build Coastguard Worker   // Then if  P = [x0, y0,
818*77c1e3ccSAndroid Build Coastguard Worker   //               x1, y1
819*77c1e3ccSAndroid Build Coastguard Worker   //               x2, y1,
820*77c1e3ccSAndroid Build Coastguard Worker   //                ....
821*77c1e3ccSAndroid Build Coastguard Worker   //              ]
822*77c1e3ccSAndroid Build Coastguard Worker   //          q = [x0', x1', x2', ... ]'
823*77c1e3ccSAndroid Build Coastguard Worker   //          r = [y0', y1', y2', ... ]'
824*77c1e3ccSAndroid Build Coastguard Worker   // the least squares problems that need to be solved are:
825*77c1e3ccSAndroid Build Coastguard Worker   //          [h1, h2]' = inv(P'P)P'q and
826*77c1e3ccSAndroid Build Coastguard Worker   //          [h3, h4]' = inv(P'P)P'r
827*77c1e3ccSAndroid Build Coastguard Worker   // where the affine transformation is given by:
828*77c1e3ccSAndroid Build Coastguard Worker   //          x' = h1.x + h2.y
829*77c1e3ccSAndroid Build Coastguard Worker   //          y' = h3.x + h4.y
830*77c1e3ccSAndroid Build Coastguard Worker   //
831*77c1e3ccSAndroid Build Coastguard Worker   // The loop below computes: A = P'P, Bx = P'q, By = P'r
832*77c1e3ccSAndroid Build Coastguard Worker   // We need to just compute inv(A).Bx and inv(A).By for the solutions.
833*77c1e3ccSAndroid Build Coastguard Worker   // Contribution from neighbor block
834*77c1e3ccSAndroid Build Coastguard Worker   for (int i = 0; i < np; i++) {
835*77c1e3ccSAndroid Build Coastguard Worker     const int dx = pts2[i * 2] - dux;
836*77c1e3ccSAndroid Build Coastguard Worker     const int dy = pts2[i * 2 + 1] - duy;
837*77c1e3ccSAndroid Build Coastguard Worker     const int sx = pts1[i * 2] - sux;
838*77c1e3ccSAndroid Build Coastguard Worker     const int sy = pts1[i * 2 + 1] - suy;
839*77c1e3ccSAndroid Build Coastguard Worker     // (TODO)yunqing: This comparison wouldn't be necessary if the sample
840*77c1e3ccSAndroid Build Coastguard Worker     // selection is done in find_samples(). Also, global offset can be removed
841*77c1e3ccSAndroid Build Coastguard Worker     // while collecting samples.
842*77c1e3ccSAndroid Build Coastguard Worker     if (abs(sx - dx) < LS_MV_MAX && abs(sy - dy) < LS_MV_MAX) {
843*77c1e3ccSAndroid Build Coastguard Worker       A[0][0] += LS_SQUARE(sx);
844*77c1e3ccSAndroid Build Coastguard Worker       A[0][1] += LS_PRODUCT1(sx, sy);
845*77c1e3ccSAndroid Build Coastguard Worker       A[1][1] += LS_SQUARE(sy);
846*77c1e3ccSAndroid Build Coastguard Worker       Bx[0] += LS_PRODUCT2(sx, dx);
847*77c1e3ccSAndroid Build Coastguard Worker       Bx[1] += LS_PRODUCT1(sy, dx);
848*77c1e3ccSAndroid Build Coastguard Worker       By[0] += LS_PRODUCT1(sx, dy);
849*77c1e3ccSAndroid Build Coastguard Worker       By[1] += LS_PRODUCT2(sy, dy);
850*77c1e3ccSAndroid Build Coastguard Worker     }
851*77c1e3ccSAndroid Build Coastguard Worker   }
852*77c1e3ccSAndroid Build Coastguard Worker 
853*77c1e3ccSAndroid Build Coastguard Worker   // Just for debugging, and can be removed later.
854*77c1e3ccSAndroid Build Coastguard Worker   assert(A[0][0] >= LS_MAT_MIN && A[0][0] <= LS_MAT_MAX);
855*77c1e3ccSAndroid Build Coastguard Worker   assert(A[0][1] >= LS_MAT_MIN && A[0][1] <= LS_MAT_MAX);
856*77c1e3ccSAndroid Build Coastguard Worker   assert(A[1][1] >= LS_MAT_MIN && A[1][1] <= LS_MAT_MAX);
857*77c1e3ccSAndroid Build Coastguard Worker   assert(Bx[0] >= LS_MAT_MIN && Bx[0] <= LS_MAT_MAX);
858*77c1e3ccSAndroid Build Coastguard Worker   assert(Bx[1] >= LS_MAT_MIN && Bx[1] <= LS_MAT_MAX);
859*77c1e3ccSAndroid Build Coastguard Worker   assert(By[0] >= LS_MAT_MIN && By[0] <= LS_MAT_MAX);
860*77c1e3ccSAndroid Build Coastguard Worker   assert(By[1] >= LS_MAT_MIN && By[1] <= LS_MAT_MAX);
861*77c1e3ccSAndroid Build Coastguard Worker 
862*77c1e3ccSAndroid Build Coastguard Worker   // Compute Determinant of A
863*77c1e3ccSAndroid Build Coastguard Worker   const int64_t Det = (int64_t)A[0][0] * A[1][1] - (int64_t)A[0][1] * A[0][1];
864*77c1e3ccSAndroid Build Coastguard Worker   if (Det == 0) return 1;
865*77c1e3ccSAndroid Build Coastguard Worker 
866*77c1e3ccSAndroid Build Coastguard Worker   int16_t shift;
867*77c1e3ccSAndroid Build Coastguard Worker   int16_t iDet = resolve_divisor_64(llabs(Det), &shift) * (Det < 0 ? -1 : 1);
868*77c1e3ccSAndroid Build Coastguard Worker   shift -= WARPEDMODEL_PREC_BITS;
869*77c1e3ccSAndroid Build Coastguard Worker   if (shift < 0) {
870*77c1e3ccSAndroid Build Coastguard Worker     iDet <<= (-shift);
871*77c1e3ccSAndroid Build Coastguard Worker     shift = 0;
872*77c1e3ccSAndroid Build Coastguard Worker   }
873*77c1e3ccSAndroid Build Coastguard Worker 
874*77c1e3ccSAndroid Build Coastguard Worker   int64_t Px[2], Py[2];
875*77c1e3ccSAndroid Build Coastguard Worker   // These divided by the Det, are the least squares solutions
876*77c1e3ccSAndroid Build Coastguard Worker   Px[0] = (int64_t)A[1][1] * Bx[0] - (int64_t)A[0][1] * Bx[1];
877*77c1e3ccSAndroid Build Coastguard Worker   Px[1] = -(int64_t)A[0][1] * Bx[0] + (int64_t)A[0][0] * Bx[1];
878*77c1e3ccSAndroid Build Coastguard Worker   Py[0] = (int64_t)A[1][1] * By[0] - (int64_t)A[0][1] * By[1];
879*77c1e3ccSAndroid Build Coastguard Worker   Py[1] = -(int64_t)A[0][1] * By[0] + (int64_t)A[0][0] * By[1];
880*77c1e3ccSAndroid Build Coastguard Worker 
881*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[2] = get_mult_shift_diag(Px[0], iDet, shift);
882*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[3] = get_mult_shift_ndiag(Px[1], iDet, shift);
883*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[4] = get_mult_shift_ndiag(Py[0], iDet, shift);
884*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[5] = get_mult_shift_diag(Py[1], iDet, shift);
885*77c1e3ccSAndroid Build Coastguard Worker 
886*77c1e3ccSAndroid Build Coastguard Worker   const int isuy = (mi_row * MI_SIZE + rsuy);
887*77c1e3ccSAndroid Build Coastguard Worker   const int isux = (mi_col * MI_SIZE + rsux);
888*77c1e3ccSAndroid Build Coastguard Worker   // Note: In the vx, vy expressions below, the max value of each of the
889*77c1e3ccSAndroid Build Coastguard Worker   // 2nd and 3rd terms are (2^16 - 1) * (2^13 - 1). That leaves enough room
890*77c1e3ccSAndroid Build Coastguard Worker   // for the first term so that the overall sum in the worst case fits
891*77c1e3ccSAndroid Build Coastguard Worker   // within 32 bits overall.
892*77c1e3ccSAndroid Build Coastguard Worker   const int32_t vx = mvx * (1 << (WARPEDMODEL_PREC_BITS - 3)) -
893*77c1e3ccSAndroid Build Coastguard Worker                      (isux * (wm->wmmat[2] - (1 << WARPEDMODEL_PREC_BITS)) +
894*77c1e3ccSAndroid Build Coastguard Worker                       isuy * wm->wmmat[3]);
895*77c1e3ccSAndroid Build Coastguard Worker   const int32_t vy = mvy * (1 << (WARPEDMODEL_PREC_BITS - 3)) -
896*77c1e3ccSAndroid Build Coastguard Worker                      (isux * wm->wmmat[4] +
897*77c1e3ccSAndroid Build Coastguard Worker                       isuy * (wm->wmmat[5] - (1 << WARPEDMODEL_PREC_BITS)));
898*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[0] =
899*77c1e3ccSAndroid Build Coastguard Worker       clamp(vx, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1);
900*77c1e3ccSAndroid Build Coastguard Worker   wm->wmmat[1] =
901*77c1e3ccSAndroid Build Coastguard Worker       clamp(vy, -WARPEDMODEL_TRANS_CLAMP, WARPEDMODEL_TRANS_CLAMP - 1);
902*77c1e3ccSAndroid Build Coastguard Worker   return 0;
903*77c1e3ccSAndroid Build Coastguard Worker }
904*77c1e3ccSAndroid Build Coastguard Worker 
av1_find_projection(int np,const int * pts1,const int * pts2,BLOCK_SIZE bsize,int mvy,int mvx,WarpedMotionParams * wm_params,int mi_row,int mi_col)905*77c1e3ccSAndroid Build Coastguard Worker int av1_find_projection(int np, const int *pts1, const int *pts2,
906*77c1e3ccSAndroid Build Coastguard Worker                         BLOCK_SIZE bsize, int mvy, int mvx,
907*77c1e3ccSAndroid Build Coastguard Worker                         WarpedMotionParams *wm_params, int mi_row, int mi_col) {
908*77c1e3ccSAndroid Build Coastguard Worker   assert(wm_params->wmtype == AFFINE);
909*77c1e3ccSAndroid Build Coastguard Worker 
910*77c1e3ccSAndroid Build Coastguard Worker   if (find_affine_int(np, pts1, pts2, bsize, mvy, mvx, wm_params, mi_row,
911*77c1e3ccSAndroid Build Coastguard Worker                       mi_col))
912*77c1e3ccSAndroid Build Coastguard Worker     return 1;
913*77c1e3ccSAndroid Build Coastguard Worker 
914*77c1e3ccSAndroid Build Coastguard Worker   // check compatibility with the fast warp filter
915*77c1e3ccSAndroid Build Coastguard Worker   if (!av1_get_shear_params(wm_params)) return 1;
916*77c1e3ccSAndroid Build Coastguard Worker 
917*77c1e3ccSAndroid Build Coastguard Worker   return 0;
918*77c1e3ccSAndroid Build Coastguard Worker }
919