xref: /aosp_15_r20/external/libaom/av1/encoder/encodemv.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 <math.h>
13*77c1e3ccSAndroid Build Coastguard Worker 
14*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/common.h"
15*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/entropymode.h"
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/cost.h"
18*77c1e3ccSAndroid Build Coastguard Worker #include "av1/encoder/encodemv.h"
19*77c1e3ccSAndroid Build Coastguard Worker 
20*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/aom_dsp_common.h"
21*77c1e3ccSAndroid Build Coastguard Worker #include "aom_ports/bitops.h"
22*77c1e3ccSAndroid Build Coastguard Worker 
update_mv_component_stats(int comp,nmv_component * mvcomp,MvSubpelPrecision precision)23*77c1e3ccSAndroid Build Coastguard Worker static void update_mv_component_stats(int comp, nmv_component *mvcomp,
24*77c1e3ccSAndroid Build Coastguard Worker                                       MvSubpelPrecision precision) {
25*77c1e3ccSAndroid Build Coastguard Worker   assert(comp != 0);
26*77c1e3ccSAndroid Build Coastguard Worker   int offset;
27*77c1e3ccSAndroid Build Coastguard Worker   const int sign = comp < 0;
28*77c1e3ccSAndroid Build Coastguard Worker   const int mag = sign ? -comp : comp;
29*77c1e3ccSAndroid Build Coastguard Worker   const int mv_class = av1_get_mv_class(mag - 1, &offset);
30*77c1e3ccSAndroid Build Coastguard Worker   const int d = offset >> 3;         // int mv data
31*77c1e3ccSAndroid Build Coastguard Worker   const int fr = (offset >> 1) & 3;  // fractional mv data
32*77c1e3ccSAndroid Build Coastguard Worker   const int hp = offset & 1;         // high precision mv data
33*77c1e3ccSAndroid Build Coastguard Worker 
34*77c1e3ccSAndroid Build Coastguard Worker   // Sign
35*77c1e3ccSAndroid Build Coastguard Worker   update_cdf(mvcomp->sign_cdf, sign, 2);
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker   // Class
38*77c1e3ccSAndroid Build Coastguard Worker   update_cdf(mvcomp->classes_cdf, mv_class, MV_CLASSES);
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker   // Integer bits
41*77c1e3ccSAndroid Build Coastguard Worker   if (mv_class == MV_CLASS_0) {
42*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(mvcomp->class0_cdf, d, CLASS0_SIZE);
43*77c1e3ccSAndroid Build Coastguard Worker   } else {
44*77c1e3ccSAndroid Build Coastguard Worker     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
45*77c1e3ccSAndroid Build Coastguard Worker     for (int i = 0; i < n; ++i)
46*77c1e3ccSAndroid Build Coastguard Worker       update_cdf(mvcomp->bits_cdf[i], (d >> i) & 1, 2);
47*77c1e3ccSAndroid Build Coastguard Worker   }
48*77c1e3ccSAndroid Build Coastguard Worker   // Fractional bits
49*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_NONE) {
50*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob *fp_cdf =
51*77c1e3ccSAndroid Build Coastguard Worker         mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf;
52*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(fp_cdf, fr, MV_FP_SIZE);
53*77c1e3ccSAndroid Build Coastguard Worker   }
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker   // High precision bit
56*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_LOW_PRECISION) {
57*77c1e3ccSAndroid Build Coastguard Worker     aom_cdf_prob *hp_cdf =
58*77c1e3ccSAndroid Build Coastguard Worker         mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf;
59*77c1e3ccSAndroid Build Coastguard Worker     update_cdf(hp_cdf, hp, 2);
60*77c1e3ccSAndroid Build Coastguard Worker   }
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker 
av1_update_mv_stats(const MV * mv,const MV * ref,nmv_context * mvctx,MvSubpelPrecision precision)63*77c1e3ccSAndroid Build Coastguard Worker void av1_update_mv_stats(const MV *mv, const MV *ref, nmv_context *mvctx,
64*77c1e3ccSAndroid Build Coastguard Worker                          MvSubpelPrecision precision) {
65*77c1e3ccSAndroid Build Coastguard Worker   const MV diff = { mv->row - ref->row, mv->col - ref->col };
66*77c1e3ccSAndroid Build Coastguard Worker   const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
67*77c1e3ccSAndroid Build Coastguard Worker 
68*77c1e3ccSAndroid Build Coastguard Worker   update_cdf(mvctx->joints_cdf, j, MV_JOINTS);
69*77c1e3ccSAndroid Build Coastguard Worker 
70*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_vertical(j))
71*77c1e3ccSAndroid Build Coastguard Worker     update_mv_component_stats(diff.row, &mvctx->comps[0], precision);
72*77c1e3ccSAndroid Build Coastguard Worker 
73*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_horizontal(j))
74*77c1e3ccSAndroid Build Coastguard Worker     update_mv_component_stats(diff.col, &mvctx->comps[1], precision);
75*77c1e3ccSAndroid Build Coastguard Worker }
76*77c1e3ccSAndroid Build Coastguard Worker 
encode_mv_component(aom_writer * w,int comp,nmv_component * mvcomp,MvSubpelPrecision precision)77*77c1e3ccSAndroid Build Coastguard Worker static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
78*77c1e3ccSAndroid Build Coastguard Worker                                 MvSubpelPrecision precision) {
79*77c1e3ccSAndroid Build Coastguard Worker   assert(comp != 0);
80*77c1e3ccSAndroid Build Coastguard Worker   int offset;
81*77c1e3ccSAndroid Build Coastguard Worker   const int sign = comp < 0;
82*77c1e3ccSAndroid Build Coastguard Worker   const int mag = sign ? -comp : comp;
83*77c1e3ccSAndroid Build Coastguard Worker   const int mv_class = av1_get_mv_class(mag - 1, &offset);
84*77c1e3ccSAndroid Build Coastguard Worker   const int d = offset >> 3;         // int mv data
85*77c1e3ccSAndroid Build Coastguard Worker   const int fr = (offset >> 1) & 3;  // fractional mv data
86*77c1e3ccSAndroid Build Coastguard Worker   const int hp = offset & 1;         // high precision mv data
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker   // Sign
89*77c1e3ccSAndroid Build Coastguard Worker   aom_write_symbol(w, sign, mvcomp->sign_cdf, 2);
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker   // Class
92*77c1e3ccSAndroid Build Coastguard Worker   aom_write_symbol(w, mv_class, mvcomp->classes_cdf, MV_CLASSES);
93*77c1e3ccSAndroid Build Coastguard Worker 
94*77c1e3ccSAndroid Build Coastguard Worker   // Integer bits
95*77c1e3ccSAndroid Build Coastguard Worker   if (mv_class == MV_CLASS_0) {
96*77c1e3ccSAndroid Build Coastguard Worker     aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
97*77c1e3ccSAndroid Build Coastguard Worker   } else {
98*77c1e3ccSAndroid Build Coastguard Worker     int i;
99*77c1e3ccSAndroid Build Coastguard Worker     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
100*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < n; ++i)
101*77c1e3ccSAndroid Build Coastguard Worker       aom_write_symbol(w, (d >> i) & 1, mvcomp->bits_cdf[i], 2);
102*77c1e3ccSAndroid Build Coastguard Worker   }
103*77c1e3ccSAndroid Build Coastguard Worker   // Fractional bits
104*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_NONE) {
105*77c1e3ccSAndroid Build Coastguard Worker     aom_write_symbol(
106*77c1e3ccSAndroid Build Coastguard Worker         w, fr,
107*77c1e3ccSAndroid Build Coastguard Worker         mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
108*77c1e3ccSAndroid Build Coastguard Worker         MV_FP_SIZE);
109*77c1e3ccSAndroid Build Coastguard Worker   }
110*77c1e3ccSAndroid Build Coastguard Worker 
111*77c1e3ccSAndroid Build Coastguard Worker   // High precision bit
112*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_LOW_PRECISION)
113*77c1e3ccSAndroid Build Coastguard Worker     aom_write_symbol(
114*77c1e3ccSAndroid Build Coastguard Worker         w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
115*77c1e3ccSAndroid Build Coastguard Worker         2);
116*77c1e3ccSAndroid Build Coastguard Worker }
117*77c1e3ccSAndroid Build Coastguard Worker 
118*77c1e3ccSAndroid Build Coastguard Worker /* TODO([email protected]): This function writes MV_VALS ints or 128 KiB. This
119*77c1e3ccSAndroid Build Coastguard Worker  *   is more than most L1D caches and is a significant chunk of L2. Write
120*77c1e3ccSAndroid Build Coastguard Worker  *   SIMD that uses streaming writes to avoid loading all of that into L1, or
121*77c1e3ccSAndroid Build Coastguard Worker  *   just don't update the larger component costs every time this called
122*77c1e3ccSAndroid Build Coastguard Worker  *   (or both).
123*77c1e3ccSAndroid Build Coastguard Worker  */
av1_build_nmv_component_cost_table(int * mvcost,const nmv_component * const mvcomp,MvSubpelPrecision precision)124*77c1e3ccSAndroid Build Coastguard Worker void av1_build_nmv_component_cost_table(int *mvcost,
125*77c1e3ccSAndroid Build Coastguard Worker                                         const nmv_component *const mvcomp,
126*77c1e3ccSAndroid Build Coastguard Worker                                         MvSubpelPrecision precision) {
127*77c1e3ccSAndroid Build Coastguard Worker   int i, j, v, o, mantissa;
128*77c1e3ccSAndroid Build Coastguard Worker   int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
129*77c1e3ccSAndroid Build Coastguard Worker   int bits_cost[MV_OFFSET_BITS][2];
130*77c1e3ccSAndroid Build Coastguard Worker   int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE] = { 0 },
131*77c1e3ccSAndroid Build Coastguard Worker       fp_cost[MV_FP_SIZE] = { 0 };
132*77c1e3ccSAndroid Build Coastguard Worker   int class0_hp_cost[2] = { 0 }, hp_cost[2] = { 0 };
133*77c1e3ccSAndroid Build Coastguard Worker 
134*77c1e3ccSAndroid Build Coastguard Worker   av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, NULL);
135*77c1e3ccSAndroid Build Coastguard Worker   av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, NULL);
136*77c1e3ccSAndroid Build Coastguard Worker   av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, NULL);
137*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < MV_OFFSET_BITS; ++i) {
138*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], NULL);
139*77c1e3ccSAndroid Build Coastguard Worker   }
140*77c1e3ccSAndroid Build Coastguard Worker 
141*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_NONE) {
142*77c1e3ccSAndroid Build Coastguard Worker     for (i = 0; i < CLASS0_SIZE; ++i)
143*77c1e3ccSAndroid Build Coastguard Worker       av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i],
144*77c1e3ccSAndroid Build Coastguard Worker                                NULL);
145*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, NULL);
146*77c1e3ccSAndroid Build Coastguard Worker   }
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker   if (precision > MV_SUBPEL_LOW_PRECISION) {
149*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, NULL);
150*77c1e3ccSAndroid Build Coastguard Worker     av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, NULL);
151*77c1e3ccSAndroid Build Coastguard Worker   }
152*77c1e3ccSAndroid Build Coastguard Worker 
153*77c1e3ccSAndroid Build Coastguard Worker   // Instead of accumulating the cost of each vector component's bits
154*77c1e3ccSAndroid Build Coastguard Worker   //   individually, compute the costs based on smaller vectors. Costs for
155*77c1e3ccSAndroid Build Coastguard Worker   //   [2^exp, 2 * 2^exp - 1] are calculated based on [0, 2^exp - 1]
156*77c1e3ccSAndroid Build Coastguard Worker   //   respectively. Offsets are maintained to swap both 1) class costs when
157*77c1e3ccSAndroid Build Coastguard Worker   //   treated as a complete vector component with the highest set bit when
158*77c1e3ccSAndroid Build Coastguard Worker   //   treated as a mantissa (significand) and 2) leading zeros to account for
159*77c1e3ccSAndroid Build Coastguard Worker   //   the current exponent.
160*77c1e3ccSAndroid Build Coastguard Worker 
161*77c1e3ccSAndroid Build Coastguard Worker   // Cost offsets
162*77c1e3ccSAndroid Build Coastguard Worker   int cost_swap[MV_OFFSET_BITS] = { 0 };
163*77c1e3ccSAndroid Build Coastguard Worker   // Delta to convert positive vector to negative vector costs
164*77c1e3ccSAndroid Build Coastguard Worker   int negate_sign = sign_cost[1] - sign_cost[0];
165*77c1e3ccSAndroid Build Coastguard Worker 
166*77c1e3ccSAndroid Build Coastguard Worker   // Initialize with offsets to swap the class costs with the costs of the
167*77c1e3ccSAndroid Build Coastguard Worker   //   highest set bit.
168*77c1e3ccSAndroid Build Coastguard Worker   for (i = 1; i < MV_OFFSET_BITS; ++i) {
169*77c1e3ccSAndroid Build Coastguard Worker     cost_swap[i] = bits_cost[i - 1][1];
170*77c1e3ccSAndroid Build Coastguard Worker     if (i > CLASS0_BITS) cost_swap[i] -= class_cost[i - CLASS0_BITS];
171*77c1e3ccSAndroid Build Coastguard Worker   }
172*77c1e3ccSAndroid Build Coastguard Worker 
173*77c1e3ccSAndroid Build Coastguard Worker   // Seed the fractional costs onto the output (overwritten latter).
174*77c1e3ccSAndroid Build Coastguard Worker   for (o = 0; o < MV_FP_SIZE; ++o) {
175*77c1e3ccSAndroid Build Coastguard Worker     int hp;
176*77c1e3ccSAndroid Build Coastguard Worker     for (hp = 0; hp < 2; ++hp) {
177*77c1e3ccSAndroid Build Coastguard Worker       v = 2 * o + hp + 1;
178*77c1e3ccSAndroid Build Coastguard Worker       mvcost[v] = fp_cost[o] + hp_cost[hp] + sign_cost[0];
179*77c1e3ccSAndroid Build Coastguard Worker     }
180*77c1e3ccSAndroid Build Coastguard Worker   }
181*77c1e3ccSAndroid Build Coastguard Worker 
182*77c1e3ccSAndroid Build Coastguard Worker   mvcost[0] = 0;
183*77c1e3ccSAndroid Build Coastguard Worker   // Fill the costs for each exponent's vectors, using the costs set in the
184*77c1e3ccSAndroid Build Coastguard Worker   //   previous exponents.
185*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < MV_OFFSET_BITS; ++i) {
186*77c1e3ccSAndroid Build Coastguard Worker     const int exponent = (2 * MV_FP_SIZE) << i;
187*77c1e3ccSAndroid Build Coastguard Worker 
188*77c1e3ccSAndroid Build Coastguard Worker     int class = 0;
189*77c1e3ccSAndroid Build Coastguard Worker     if (i >= CLASS0_BITS) {
190*77c1e3ccSAndroid Build Coastguard Worker       class = class_cost[i - CLASS0_BITS + 1];
191*77c1e3ccSAndroid Build Coastguard Worker     }
192*77c1e3ccSAndroid Build Coastguard Worker 
193*77c1e3ccSAndroid Build Coastguard Worker     // Iterate through mantissas, keeping track of the location
194*77c1e3ccSAndroid Build Coastguard Worker     //   of the highest set bit for the mantissa.
195*77c1e3ccSAndroid Build Coastguard Worker     // To be clear: in the outer loop, the position of the highest set bit
196*77c1e3ccSAndroid Build Coastguard Worker     //   (exponent) is tracked and, in this loop, the highest set bit of the
197*77c1e3ccSAndroid Build Coastguard Worker     //   mantissa is tracked.
198*77c1e3ccSAndroid Build Coastguard Worker     mantissa = 0;
199*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j <= i; ++j) {
200*77c1e3ccSAndroid Build Coastguard Worker       for (; mantissa < (2 * MV_FP_SIZE) << j; ++mantissa) {
201*77c1e3ccSAndroid Build Coastguard Worker         int cost = mvcost[mantissa + 1] + class + cost_swap[j];
202*77c1e3ccSAndroid Build Coastguard Worker         v = exponent + mantissa + 1;
203*77c1e3ccSAndroid Build Coastguard Worker         mvcost[v] = cost;
204*77c1e3ccSAndroid Build Coastguard Worker         mvcost[-v] = cost + negate_sign;
205*77c1e3ccSAndroid Build Coastguard Worker       }
206*77c1e3ccSAndroid Build Coastguard Worker       cost_swap[j] += bits_cost[i][0];
207*77c1e3ccSAndroid Build Coastguard Worker     }
208*77c1e3ccSAndroid Build Coastguard Worker   }
209*77c1e3ccSAndroid Build Coastguard Worker 
210*77c1e3ccSAndroid Build Coastguard Worker   // Special case to avoid buffer overrun
211*77c1e3ccSAndroid Build Coastguard Worker   {
212*77c1e3ccSAndroid Build Coastguard Worker     int exponent = (2 * MV_FP_SIZE) << MV_OFFSET_BITS;
213*77c1e3ccSAndroid Build Coastguard Worker     int class = class_cost[MV_CLASSES - 1];
214*77c1e3ccSAndroid Build Coastguard Worker     mantissa = 0;
215*77c1e3ccSAndroid Build Coastguard Worker     for (j = 0; j < MV_OFFSET_BITS; ++j) {
216*77c1e3ccSAndroid Build Coastguard Worker       for (; mantissa < (2 * MV_FP_SIZE) << j; ++mantissa) {
217*77c1e3ccSAndroid Build Coastguard Worker         int cost = mvcost[mantissa + 1] + class + cost_swap[j];
218*77c1e3ccSAndroid Build Coastguard Worker         v = exponent + mantissa + 1;
219*77c1e3ccSAndroid Build Coastguard Worker         mvcost[v] = cost;
220*77c1e3ccSAndroid Build Coastguard Worker         mvcost[-v] = cost + negate_sign;
221*77c1e3ccSAndroid Build Coastguard Worker       }
222*77c1e3ccSAndroid Build Coastguard Worker     }
223*77c1e3ccSAndroid Build Coastguard Worker     // At this point: mantissa = exponent >> 1
224*77c1e3ccSAndroid Build Coastguard Worker 
225*77c1e3ccSAndroid Build Coastguard Worker     // Manually calculate the final cost offset
226*77c1e3ccSAndroid Build Coastguard Worker     int cost_swap_hi =
227*77c1e3ccSAndroid Build Coastguard Worker         bits_cost[MV_OFFSET_BITS - 1][1] - class_cost[MV_CLASSES - 2];
228*77c1e3ccSAndroid Build Coastguard Worker     for (; mantissa < exponent - 1; ++mantissa) {
229*77c1e3ccSAndroid Build Coastguard Worker       int cost = mvcost[mantissa + 1] + class + cost_swap_hi;
230*77c1e3ccSAndroid Build Coastguard Worker       v = exponent + mantissa + 1;
231*77c1e3ccSAndroid Build Coastguard Worker       mvcost[v] = cost;
232*77c1e3ccSAndroid Build Coastguard Worker       mvcost[-v] = cost + negate_sign;
233*77c1e3ccSAndroid Build Coastguard Worker     }
234*77c1e3ccSAndroid Build Coastguard Worker   }
235*77c1e3ccSAndroid Build Coastguard Worker 
236*77c1e3ccSAndroid Build Coastguard Worker   // Fill costs for class0 vectors, overwriting previous placeholder values
237*77c1e3ccSAndroid Build Coastguard Worker   //   used for calculating the costs of the larger vectors.
238*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < CLASS0_SIZE; ++i) {
239*77c1e3ccSAndroid Build Coastguard Worker     const int top = i * 2 * MV_FP_SIZE;
240*77c1e3ccSAndroid Build Coastguard Worker     for (o = 0; o < MV_FP_SIZE; ++o) {
241*77c1e3ccSAndroid Build Coastguard Worker       int hp;
242*77c1e3ccSAndroid Build Coastguard Worker       int cost = class0_fp_cost[i][o] + class_cost[0] + class0_cost[i];
243*77c1e3ccSAndroid Build Coastguard Worker       for (hp = 0; hp < 2; ++hp) {
244*77c1e3ccSAndroid Build Coastguard Worker         v = top + 2 * o + hp + 1;
245*77c1e3ccSAndroid Build Coastguard Worker         mvcost[v] = cost + class0_hp_cost[hp] + sign_cost[0];
246*77c1e3ccSAndroid Build Coastguard Worker         mvcost[-v] = cost + class0_hp_cost[hp] + sign_cost[1];
247*77c1e3ccSAndroid Build Coastguard Worker       }
248*77c1e3ccSAndroid Build Coastguard Worker     }
249*77c1e3ccSAndroid Build Coastguard Worker   }
250*77c1e3ccSAndroid Build Coastguard Worker }
251*77c1e3ccSAndroid Build Coastguard Worker 
av1_encode_mv(AV1_COMP * cpi,aom_writer * w,ThreadData * td,const MV * mv,const MV * ref,nmv_context * mvctx,int usehp)252*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, ThreadData *td, const MV *mv,
253*77c1e3ccSAndroid Build Coastguard Worker                    const MV *ref, nmv_context *mvctx, int usehp) {
254*77c1e3ccSAndroid Build Coastguard Worker   const MV diff = { mv->row - ref->row, mv->col - ref->col };
255*77c1e3ccSAndroid Build Coastguard Worker   const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
256*77c1e3ccSAndroid Build Coastguard Worker   // If the mv_diff is zero, then we should have used near or nearest instead.
257*77c1e3ccSAndroid Build Coastguard Worker   assert(j != MV_JOINT_ZERO);
258*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->common.features.cur_frame_force_integer_mv) {
259*77c1e3ccSAndroid Build Coastguard Worker     usehp = MV_SUBPEL_NONE;
260*77c1e3ccSAndroid Build Coastguard Worker   }
261*77c1e3ccSAndroid Build Coastguard Worker   aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
262*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_vertical(j))
263*77c1e3ccSAndroid Build Coastguard Worker     encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
264*77c1e3ccSAndroid Build Coastguard Worker 
265*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_horizontal(j))
266*77c1e3ccSAndroid Build Coastguard Worker     encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
267*77c1e3ccSAndroid Build Coastguard Worker 
268*77c1e3ccSAndroid Build Coastguard Worker   // If auto_mv_step_size is enabled then keep track of the largest
269*77c1e3ccSAndroid Build Coastguard Worker   // motion vector component used.
270*77c1e3ccSAndroid Build Coastguard Worker   if (cpi->sf.mv_sf.auto_mv_step_size) {
271*77c1e3ccSAndroid Build Coastguard Worker     int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
272*77c1e3ccSAndroid Build Coastguard Worker     td->max_mv_magnitude = AOMMAX(maxv, td->max_mv_magnitude);
273*77c1e3ccSAndroid Build Coastguard Worker   }
274*77c1e3ccSAndroid Build Coastguard Worker }
275*77c1e3ccSAndroid Build Coastguard Worker 
av1_encode_dv(aom_writer * w,const MV * mv,const MV * ref,nmv_context * mvctx)276*77c1e3ccSAndroid Build Coastguard Worker void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
277*77c1e3ccSAndroid Build Coastguard Worker                    nmv_context *mvctx) {
278*77c1e3ccSAndroid Build Coastguard Worker   // DV and ref DV should not have sub-pel.
279*77c1e3ccSAndroid Build Coastguard Worker   assert((mv->col & 7) == 0);
280*77c1e3ccSAndroid Build Coastguard Worker   assert((mv->row & 7) == 0);
281*77c1e3ccSAndroid Build Coastguard Worker   assert((ref->col & 7) == 0);
282*77c1e3ccSAndroid Build Coastguard Worker   assert((ref->row & 7) == 0);
283*77c1e3ccSAndroid Build Coastguard Worker   const MV diff = { mv->row - ref->row, mv->col - ref->col };
284*77c1e3ccSAndroid Build Coastguard Worker   const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
285*77c1e3ccSAndroid Build Coastguard Worker 
286*77c1e3ccSAndroid Build Coastguard Worker   aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
287*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_vertical(j))
288*77c1e3ccSAndroid Build Coastguard Worker     encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
289*77c1e3ccSAndroid Build Coastguard Worker 
290*77c1e3ccSAndroid Build Coastguard Worker   if (mv_joint_horizontal(j))
291*77c1e3ccSAndroid Build Coastguard Worker     encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
292*77c1e3ccSAndroid Build Coastguard Worker }
293*77c1e3ccSAndroid Build Coastguard Worker 
av1_build_nmv_cost_table(int * mvjoint,int * mvcost[2],const nmv_context * ctx,MvSubpelPrecision precision)294*77c1e3ccSAndroid Build Coastguard Worker void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
295*77c1e3ccSAndroid Build Coastguard Worker                               const nmv_context *ctx,
296*77c1e3ccSAndroid Build Coastguard Worker                               MvSubpelPrecision precision) {
297*77c1e3ccSAndroid Build Coastguard Worker   av1_cost_tokens_from_cdf(mvjoint, ctx->joints_cdf, NULL);
298*77c1e3ccSAndroid Build Coastguard Worker   av1_build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
299*77c1e3ccSAndroid Build Coastguard Worker   av1_build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
300*77c1e3ccSAndroid Build Coastguard Worker }
301*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_ref_mv_from_stack(int ref_idx,const MV_REFERENCE_FRAME * ref_frame,int ref_mv_idx,const MB_MODE_INFO_EXT * mbmi_ext)302*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_get_ref_mv_from_stack(int ref_idx,
303*77c1e3ccSAndroid Build Coastguard Worker                                  const MV_REFERENCE_FRAME *ref_frame,
304*77c1e3ccSAndroid Build Coastguard Worker                                  int ref_mv_idx,
305*77c1e3ccSAndroid Build Coastguard Worker                                  const MB_MODE_INFO_EXT *mbmi_ext) {
306*77c1e3ccSAndroid Build Coastguard Worker   const int8_t ref_frame_type = av1_ref_frame_type(ref_frame);
307*77c1e3ccSAndroid Build Coastguard Worker   const CANDIDATE_MV *curr_ref_mv_stack =
308*77c1e3ccSAndroid Build Coastguard Worker       mbmi_ext->ref_mv_stack[ref_frame_type];
309*77c1e3ccSAndroid Build Coastguard Worker 
310*77c1e3ccSAndroid Build Coastguard Worker   if (ref_frame[1] > INTRA_FRAME) {
311*77c1e3ccSAndroid Build Coastguard Worker     assert(ref_idx == 0 || ref_idx == 1);
312*77c1e3ccSAndroid Build Coastguard Worker     return ref_idx ? curr_ref_mv_stack[ref_mv_idx].comp_mv
313*77c1e3ccSAndroid Build Coastguard Worker                    : curr_ref_mv_stack[ref_mv_idx].this_mv;
314*77c1e3ccSAndroid Build Coastguard Worker   }
315*77c1e3ccSAndroid Build Coastguard Worker 
316*77c1e3ccSAndroid Build Coastguard Worker   assert(ref_idx == 0);
317*77c1e3ccSAndroid Build Coastguard Worker   return ref_mv_idx < mbmi_ext->ref_mv_count[ref_frame_type]
318*77c1e3ccSAndroid Build Coastguard Worker              ? curr_ref_mv_stack[ref_mv_idx].this_mv
319*77c1e3ccSAndroid Build Coastguard Worker              : mbmi_ext->global_mvs[ref_frame_type];
320*77c1e3ccSAndroid Build Coastguard Worker }
321*77c1e3ccSAndroid Build Coastguard Worker 
av1_get_ref_mv(const MACROBLOCK * x,int ref_idx)322*77c1e3ccSAndroid Build Coastguard Worker int_mv av1_get_ref_mv(const MACROBLOCK *x, int ref_idx) {
323*77c1e3ccSAndroid Build Coastguard Worker   const MACROBLOCKD *xd = &x->e_mbd;
324*77c1e3ccSAndroid Build Coastguard Worker   const MB_MODE_INFO *mbmi = xd->mi[0];
325*77c1e3ccSAndroid Build Coastguard Worker   int ref_mv_idx = mbmi->ref_mv_idx;
326*77c1e3ccSAndroid Build Coastguard Worker   if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) {
327*77c1e3ccSAndroid Build Coastguard Worker     assert(has_second_ref(mbmi));
328*77c1e3ccSAndroid Build Coastguard Worker     ref_mv_idx += 1;
329*77c1e3ccSAndroid Build Coastguard Worker   }
330*77c1e3ccSAndroid Build Coastguard Worker   return av1_get_ref_mv_from_stack(ref_idx, mbmi->ref_frame, ref_mv_idx,
331*77c1e3ccSAndroid Build Coastguard Worker                                    &x->mbmi_ext);
332*77c1e3ccSAndroid Build Coastguard Worker }
333*77c1e3ccSAndroid Build Coastguard Worker 
av1_find_best_ref_mvs_from_stack(int allow_hp,const MB_MODE_INFO_EXT * mbmi_ext,MV_REFERENCE_FRAME ref_frame,int_mv * nearest_mv,int_mv * near_mv,int is_integer)334*77c1e3ccSAndroid Build Coastguard Worker void av1_find_best_ref_mvs_from_stack(int allow_hp,
335*77c1e3ccSAndroid Build Coastguard Worker                                       const MB_MODE_INFO_EXT *mbmi_ext,
336*77c1e3ccSAndroid Build Coastguard Worker                                       MV_REFERENCE_FRAME ref_frame,
337*77c1e3ccSAndroid Build Coastguard Worker                                       int_mv *nearest_mv, int_mv *near_mv,
338*77c1e3ccSAndroid Build Coastguard Worker                                       int is_integer) {
339*77c1e3ccSAndroid Build Coastguard Worker   const int ref_idx = 0;
340*77c1e3ccSAndroid Build Coastguard Worker   MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, NONE_FRAME };
341*77c1e3ccSAndroid Build Coastguard Worker   *nearest_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 0, mbmi_ext);
342*77c1e3ccSAndroid Build Coastguard Worker   lower_mv_precision(&nearest_mv->as_mv, allow_hp, is_integer);
343*77c1e3ccSAndroid Build Coastguard Worker   *near_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 1, mbmi_ext);
344*77c1e3ccSAndroid Build Coastguard Worker   lower_mv_precision(&near_mv->as_mv, allow_hp, is_integer);
345*77c1e3ccSAndroid Build Coastguard Worker }
346