• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <math.h>
13 
14 #include "av1/common/common.h"
15 #include "av1/common/entropymode.h"
16 
17 #include "av1/encoder/cost.h"
18 #include "av1/encoder/encodemv.h"
19 
20 #include "aom_dsp/aom_dsp_common.h"
21 #include "aom_ports/bitops.h"
22 
mv_class_base(MV_CLASS_TYPE c)23 static INLINE int mv_class_base(MV_CLASS_TYPE c) {
24   return c ? CLASS0_SIZE << (c + 2) : 0;
25 }
26 
27 // If n != 0, returns the floor of log base 2 of n. If n == 0, returns 0.
log_in_base_2(unsigned int n)28 static INLINE uint8_t log_in_base_2(unsigned int n) {
29   // get_msb() is only valid when n != 0.
30   return n == 0 ? 0 : get_msb(n);
31 }
32 
get_mv_class(int z,int * offset)33 static INLINE MV_CLASS_TYPE get_mv_class(int z, int *offset) {
34   const MV_CLASS_TYPE c = (z >= CLASS0_SIZE * 4096)
35                               ? MV_CLASS_10
36                               : (MV_CLASS_TYPE)log_in_base_2(z >> 3);
37   if (offset) *offset = z - mv_class_base(c);
38   return c;
39 }
40 
encode_mv_component(aom_writer * w,int comp,nmv_component * mvcomp,MvSubpelPrecision precision)41 static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
42                                 MvSubpelPrecision precision) {
43   assert(comp != 0);
44   int offset;
45   const int sign = comp < 0;
46   const int mag = sign ? -comp : comp;
47   const int mv_class = get_mv_class(mag - 1, &offset);
48   const int d = offset >> 3;         // int mv data
49   const int fr = (offset >> 1) & 3;  // fractional mv data
50   const int hp = offset & 1;         // high precision mv data
51 
52   // Sign
53   aom_write_symbol(w, sign, mvcomp->sign_cdf, 2);
54 
55   // Class
56   aom_write_symbol(w, mv_class, mvcomp->classes_cdf, MV_CLASSES);
57 
58   // Integer bits
59   if (mv_class == MV_CLASS_0) {
60     aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
61   } else {
62     int i;
63     const int n = mv_class + CLASS0_BITS - 1;  // number of bits
64     for (i = 0; i < n; ++i)
65       aom_write_symbol(w, (d >> i) & 1, mvcomp->bits_cdf[i], 2);
66   }
67   // Fractional bits
68   if (precision > MV_SUBPEL_NONE) {
69     aom_write_symbol(
70         w, fr,
71         mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
72         MV_FP_SIZE);
73   }
74 
75   // High precision bit
76   if (precision > MV_SUBPEL_LOW_PRECISION)
77     aom_write_symbol(
78         w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
79         2);
80 }
81 
build_nmv_component_cost_table(int * mvcost,const nmv_component * const mvcomp,MvSubpelPrecision precision)82 static void build_nmv_component_cost_table(int *mvcost,
83                                            const nmv_component *const mvcomp,
84                                            MvSubpelPrecision precision) {
85   int i, v;
86   int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
87   int bits_cost[MV_OFFSET_BITS][2];
88   int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
89   int class0_hp_cost[2], hp_cost[2];
90 
91   av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, NULL);
92   av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, NULL);
93   av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, NULL);
94   for (i = 0; i < MV_OFFSET_BITS; ++i) {
95     av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], NULL);
96   }
97 
98   for (i = 0; i < CLASS0_SIZE; ++i)
99     av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i], NULL);
100   av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, NULL);
101 
102   if (precision > MV_SUBPEL_LOW_PRECISION) {
103     av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, NULL);
104     av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, NULL);
105   }
106   mvcost[0] = 0;
107   for (v = 1; v <= MV_MAX; ++v) {
108     int z, c, o, d, e, f, cost = 0;
109     z = v - 1;
110     c = get_mv_class(z, &o);
111     cost += class_cost[c];
112     d = (o >> 3);     /* int mv data */
113     f = (o >> 1) & 3; /* fractional pel mv data */
114     e = (o & 1);      /* high precision mv data */
115     if (c == MV_CLASS_0) {
116       cost += class0_cost[d];
117     } else {
118       const int b = c + CLASS0_BITS - 1; /* number of bits */
119       for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
120     }
121     if (precision > MV_SUBPEL_NONE) {
122       if (c == MV_CLASS_0) {
123         cost += class0_fp_cost[d][f];
124       } else {
125         cost += fp_cost[f];
126       }
127       if (precision > MV_SUBPEL_LOW_PRECISION) {
128         if (c == MV_CLASS_0) {
129           cost += class0_hp_cost[e];
130         } else {
131           cost += hp_cost[e];
132         }
133       }
134     }
135     mvcost[v] = cost + sign_cost[0];
136     mvcost[-v] = cost + sign_cost[1];
137   }
138 }
139 
av1_encode_mv(AV1_COMP * cpi,aom_writer * w,const MV * mv,const MV * ref,nmv_context * mvctx,int usehp)140 void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
141                    nmv_context *mvctx, int usehp) {
142   const MV diff = { mv->row - ref->row, mv->col - ref->col };
143   const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
144   if (cpi->common.cur_frame_force_integer_mv) {
145     usehp = MV_SUBPEL_NONE;
146   }
147   aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
148   if (mv_joint_vertical(j))
149     encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
150 
151   if (mv_joint_horizontal(j))
152     encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
153 
154   // If auto_mv_step_size is enabled then keep track of the largest
155   // motion vector component used.
156   if (cpi->sf.mv.auto_mv_step_size) {
157     unsigned int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
158     cpi->max_mv_magnitude = AOMMAX(maxv, cpi->max_mv_magnitude);
159   }
160 }
161 
av1_encode_dv(aom_writer * w,const MV * mv,const MV * ref,nmv_context * mvctx)162 void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
163                    nmv_context *mvctx) {
164   // DV and ref DV should not have sub-pel.
165   assert((mv->col & 7) == 0);
166   assert((mv->row & 7) == 0);
167   assert((ref->col & 7) == 0);
168   assert((ref->row & 7) == 0);
169   const MV diff = { mv->row - ref->row, mv->col - ref->col };
170   const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
171 
172   aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
173   if (mv_joint_vertical(j))
174     encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
175 
176   if (mv_joint_horizontal(j))
177     encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
178 }
179 
av1_build_nmv_cost_table(int * mvjoint,int * mvcost[2],const nmv_context * ctx,MvSubpelPrecision precision)180 void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
181                               const nmv_context *ctx,
182                               MvSubpelPrecision precision) {
183   av1_cost_tokens_from_cdf(mvjoint, ctx->joints_cdf, NULL);
184   build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
185   build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
186 }
187 
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)188 int_mv av1_get_ref_mv_from_stack(int ref_idx,
189                                  const MV_REFERENCE_FRAME *ref_frame,
190                                  int ref_mv_idx,
191                                  const MB_MODE_INFO_EXT *mbmi_ext) {
192   const int8_t ref_frame_type = av1_ref_frame_type(ref_frame);
193   const CANDIDATE_MV *curr_ref_mv_stack =
194       mbmi_ext->ref_mv_stack[ref_frame_type];
195   int_mv ref_mv;
196   ref_mv.as_int = INVALID_MV;
197 
198   if (ref_frame[1] > INTRA_FRAME) {
199     if (ref_idx == 0) {
200       ref_mv = curr_ref_mv_stack[ref_mv_idx].this_mv;
201     } else {
202       assert(ref_idx == 1);
203       ref_mv = curr_ref_mv_stack[ref_mv_idx].comp_mv;
204     }
205   } else {
206     assert(ref_idx == 0);
207     if (ref_mv_idx < mbmi_ext->ref_mv_count[ref_frame_type]) {
208       ref_mv = curr_ref_mv_stack[ref_mv_idx].this_mv;
209     } else {
210       ref_mv = mbmi_ext->global_mvs[ref_frame_type];
211     }
212   }
213   return ref_mv;
214 }
215 
av1_get_ref_mv(const MACROBLOCK * x,int ref_idx)216 int_mv av1_get_ref_mv(const MACROBLOCK *x, int ref_idx) {
217   const MACROBLOCKD *xd = &x->e_mbd;
218   const MB_MODE_INFO *mbmi = xd->mi[0];
219   int ref_mv_idx = mbmi->ref_mv_idx;
220   if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) {
221     assert(has_second_ref(mbmi));
222     ref_mv_idx += 1;
223   }
224   return av1_get_ref_mv_from_stack(ref_idx, mbmi->ref_frame, ref_mv_idx,
225                                    x->mbmi_ext);
226 }
227 
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)228 void av1_find_best_ref_mvs_from_stack(int allow_hp,
229                                       const MB_MODE_INFO_EXT *mbmi_ext,
230                                       MV_REFERENCE_FRAME ref_frame,
231                                       int_mv *nearest_mv, int_mv *near_mv,
232                                       int is_integer) {
233   const int ref_idx = 0;
234   MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, NONE_FRAME };
235   *nearest_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 0, mbmi_ext);
236   lower_mv_precision(&nearest_mv->as_mv, allow_hp, is_integer);
237   *near_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 1, mbmi_ext);
238   lower_mv_precision(&near_mv->as_mv, allow_hp, is_integer);
239 }
240