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
update_mv_component_stats(int comp,nmv_component * mvcomp,MvSubpelPrecision precision)23 static void update_mv_component_stats(int comp, nmv_component *mvcomp,
24 MvSubpelPrecision precision) {
25 assert(comp != 0);
26 int offset;
27 const int sign = comp < 0;
28 const int mag = sign ? -comp : comp;
29 const int mv_class = av1_get_mv_class(mag - 1, &offset);
30 const int d = offset >> 3; // int mv data
31 const int fr = (offset >> 1) & 3; // fractional mv data
32 const int hp = offset & 1; // high precision mv data
33
34 // Sign
35 update_cdf(mvcomp->sign_cdf, sign, 2);
36
37 // Class
38 update_cdf(mvcomp->classes_cdf, mv_class, MV_CLASSES);
39
40 // Integer bits
41 if (mv_class == MV_CLASS_0) {
42 update_cdf(mvcomp->class0_cdf, d, CLASS0_SIZE);
43 } else {
44 const int n = mv_class + CLASS0_BITS - 1; // number of bits
45 for (int i = 0; i < n; ++i)
46 update_cdf(mvcomp->bits_cdf[i], (d >> i) & 1, 2);
47 }
48 // Fractional bits
49 if (precision > MV_SUBPEL_NONE) {
50 aom_cdf_prob *fp_cdf =
51 mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf;
52 update_cdf(fp_cdf, fr, MV_FP_SIZE);
53 }
54
55 // High precision bit
56 if (precision > MV_SUBPEL_LOW_PRECISION) {
57 aom_cdf_prob *hp_cdf =
58 mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf;
59 update_cdf(hp_cdf, hp, 2);
60 }
61 }
62
av1_update_mv_stats(const MV * mv,const MV * ref,nmv_context * mvctx,MvSubpelPrecision precision)63 void av1_update_mv_stats(const MV *mv, const MV *ref, nmv_context *mvctx,
64 MvSubpelPrecision precision) {
65 const MV diff = { mv->row - ref->row, mv->col - ref->col };
66 const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
67
68 update_cdf(mvctx->joints_cdf, j, MV_JOINTS);
69
70 if (mv_joint_vertical(j))
71 update_mv_component_stats(diff.row, &mvctx->comps[0], precision);
72
73 if (mv_joint_horizontal(j))
74 update_mv_component_stats(diff.col, &mvctx->comps[1], precision);
75 }
76
encode_mv_component(aom_writer * w,int comp,nmv_component * mvcomp,MvSubpelPrecision precision)77 static void encode_mv_component(aom_writer *w, int comp, nmv_component *mvcomp,
78 MvSubpelPrecision precision) {
79 assert(comp != 0);
80 int offset;
81 const int sign = comp < 0;
82 const int mag = sign ? -comp : comp;
83 const int mv_class = av1_get_mv_class(mag - 1, &offset);
84 const int d = offset >> 3; // int mv data
85 const int fr = (offset >> 1) & 3; // fractional mv data
86 const int hp = offset & 1; // high precision mv data
87
88 // Sign
89 aom_write_symbol(w, sign, mvcomp->sign_cdf, 2);
90
91 // Class
92 aom_write_symbol(w, mv_class, mvcomp->classes_cdf, MV_CLASSES);
93
94 // Integer bits
95 if (mv_class == MV_CLASS_0) {
96 aom_write_symbol(w, d, mvcomp->class0_cdf, CLASS0_SIZE);
97 } else {
98 int i;
99 const int n = mv_class + CLASS0_BITS - 1; // number of bits
100 for (i = 0; i < n; ++i)
101 aom_write_symbol(w, (d >> i) & 1, mvcomp->bits_cdf[i], 2);
102 }
103 // Fractional bits
104 if (precision > MV_SUBPEL_NONE) {
105 aom_write_symbol(
106 w, fr,
107 mv_class == MV_CLASS_0 ? mvcomp->class0_fp_cdf[d] : mvcomp->fp_cdf,
108 MV_FP_SIZE);
109 }
110
111 // High precision bit
112 if (precision > MV_SUBPEL_LOW_PRECISION)
113 aom_write_symbol(
114 w, hp, mv_class == MV_CLASS_0 ? mvcomp->class0_hp_cdf : mvcomp->hp_cdf,
115 2);
116 }
117
build_nmv_component_cost_table(int * mvcost,const nmv_component * const mvcomp,MvSubpelPrecision precision)118 static void build_nmv_component_cost_table(int *mvcost,
119 const nmv_component *const mvcomp,
120 MvSubpelPrecision precision) {
121 int i, v;
122 int sign_cost[2], class_cost[MV_CLASSES], class0_cost[CLASS0_SIZE];
123 int bits_cost[MV_OFFSET_BITS][2];
124 int class0_fp_cost[CLASS0_SIZE][MV_FP_SIZE], fp_cost[MV_FP_SIZE];
125 int class0_hp_cost[2], hp_cost[2];
126
127 av1_cost_tokens_from_cdf(sign_cost, mvcomp->sign_cdf, NULL);
128 av1_cost_tokens_from_cdf(class_cost, mvcomp->classes_cdf, NULL);
129 av1_cost_tokens_from_cdf(class0_cost, mvcomp->class0_cdf, NULL);
130 for (i = 0; i < MV_OFFSET_BITS; ++i) {
131 av1_cost_tokens_from_cdf(bits_cost[i], mvcomp->bits_cdf[i], NULL);
132 }
133
134 for (i = 0; i < CLASS0_SIZE; ++i)
135 av1_cost_tokens_from_cdf(class0_fp_cost[i], mvcomp->class0_fp_cdf[i], NULL);
136 av1_cost_tokens_from_cdf(fp_cost, mvcomp->fp_cdf, NULL);
137
138 if (precision > MV_SUBPEL_LOW_PRECISION) {
139 av1_cost_tokens_from_cdf(class0_hp_cost, mvcomp->class0_hp_cdf, NULL);
140 av1_cost_tokens_from_cdf(hp_cost, mvcomp->hp_cdf, NULL);
141 }
142 mvcost[0] = 0;
143 for (v = 1; v <= MV_MAX; ++v) {
144 int z, c, o, d, e, f, cost = 0;
145 z = v - 1;
146 c = av1_get_mv_class(z, &o);
147 cost += class_cost[c];
148 d = (o >> 3); /* int mv data */
149 f = (o >> 1) & 3; /* fractional pel mv data */
150 e = (o & 1); /* high precision mv data */
151 if (c == MV_CLASS_0) {
152 cost += class0_cost[d];
153 } else {
154 const int b = c + CLASS0_BITS - 1; /* number of bits */
155 for (i = 0; i < b; ++i) cost += bits_cost[i][((d >> i) & 1)];
156 }
157 if (precision > MV_SUBPEL_NONE) {
158 if (c == MV_CLASS_0) {
159 cost += class0_fp_cost[d][f];
160 } else {
161 cost += fp_cost[f];
162 }
163 if (precision > MV_SUBPEL_LOW_PRECISION) {
164 if (c == MV_CLASS_0) {
165 cost += class0_hp_cost[e];
166 } else {
167 cost += hp_cost[e];
168 }
169 }
170 }
171 mvcost[v] = cost + sign_cost[0];
172 mvcost[-v] = cost + sign_cost[1];
173 }
174 }
175
av1_encode_mv(AV1_COMP * cpi,aom_writer * w,const MV * mv,const MV * ref,nmv_context * mvctx,int usehp)176 void av1_encode_mv(AV1_COMP *cpi, aom_writer *w, const MV *mv, const MV *ref,
177 nmv_context *mvctx, int usehp) {
178 const MV diff = { mv->row - ref->row, mv->col - ref->col };
179 const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
180 // If the mv_diff is zero, then we should have used near or nearest instead.
181 assert(j != MV_JOINT_ZERO);
182 if (cpi->common.features.cur_frame_force_integer_mv) {
183 usehp = MV_SUBPEL_NONE;
184 }
185 aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
186 if (mv_joint_vertical(j))
187 encode_mv_component(w, diff.row, &mvctx->comps[0], usehp);
188
189 if (mv_joint_horizontal(j))
190 encode_mv_component(w, diff.col, &mvctx->comps[1], usehp);
191
192 // If auto_mv_step_size is enabled then keep track of the largest
193 // motion vector component used.
194 if (cpi->sf.mv_sf.auto_mv_step_size) {
195 int maxv = AOMMAX(abs(mv->row), abs(mv->col)) >> 3;
196 cpi->mv_search_params.max_mv_magnitude =
197 AOMMAX(maxv, cpi->mv_search_params.max_mv_magnitude);
198 }
199 }
200
av1_encode_dv(aom_writer * w,const MV * mv,const MV * ref,nmv_context * mvctx)201 void av1_encode_dv(aom_writer *w, const MV *mv, const MV *ref,
202 nmv_context *mvctx) {
203 // DV and ref DV should not have sub-pel.
204 assert((mv->col & 7) == 0);
205 assert((mv->row & 7) == 0);
206 assert((ref->col & 7) == 0);
207 assert((ref->row & 7) == 0);
208 const MV diff = { mv->row - ref->row, mv->col - ref->col };
209 const MV_JOINT_TYPE j = av1_get_mv_joint(&diff);
210
211 aom_write_symbol(w, j, mvctx->joints_cdf, MV_JOINTS);
212 if (mv_joint_vertical(j))
213 encode_mv_component(w, diff.row, &mvctx->comps[0], MV_SUBPEL_NONE);
214
215 if (mv_joint_horizontal(j))
216 encode_mv_component(w, diff.col, &mvctx->comps[1], MV_SUBPEL_NONE);
217 }
218
av1_build_nmv_cost_table(int * mvjoint,int * mvcost[2],const nmv_context * ctx,MvSubpelPrecision precision)219 void av1_build_nmv_cost_table(int *mvjoint, int *mvcost[2],
220 const nmv_context *ctx,
221 MvSubpelPrecision precision) {
222 av1_cost_tokens_from_cdf(mvjoint, ctx->joints_cdf, NULL);
223 build_nmv_component_cost_table(mvcost[0], &ctx->comps[0], precision);
224 build_nmv_component_cost_table(mvcost[1], &ctx->comps[1], precision);
225 }
226
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)227 int_mv av1_get_ref_mv_from_stack(int ref_idx,
228 const MV_REFERENCE_FRAME *ref_frame,
229 int ref_mv_idx,
230 const MB_MODE_INFO_EXT *mbmi_ext) {
231 const int8_t ref_frame_type = av1_ref_frame_type(ref_frame);
232 const CANDIDATE_MV *curr_ref_mv_stack =
233 mbmi_ext->ref_mv_stack[ref_frame_type];
234
235 if (ref_frame[1] > INTRA_FRAME) {
236 assert(ref_idx == 0 || ref_idx == 1);
237 return ref_idx ? curr_ref_mv_stack[ref_mv_idx].comp_mv
238 : curr_ref_mv_stack[ref_mv_idx].this_mv;
239 }
240
241 assert(ref_idx == 0);
242 return ref_mv_idx < mbmi_ext->ref_mv_count[ref_frame_type]
243 ? curr_ref_mv_stack[ref_mv_idx].this_mv
244 : mbmi_ext->global_mvs[ref_frame_type];
245 }
246
av1_get_ref_mv(const MACROBLOCK * x,int ref_idx)247 int_mv av1_get_ref_mv(const MACROBLOCK *x, int ref_idx) {
248 const MACROBLOCKD *xd = &x->e_mbd;
249 const MB_MODE_INFO *mbmi = xd->mi[0];
250 int ref_mv_idx = mbmi->ref_mv_idx;
251 if (mbmi->mode == NEAR_NEWMV || mbmi->mode == NEW_NEARMV) {
252 assert(has_second_ref(mbmi));
253 ref_mv_idx += 1;
254 }
255 return av1_get_ref_mv_from_stack(ref_idx, mbmi->ref_frame, ref_mv_idx,
256 x->mbmi_ext);
257 }
258
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)259 void av1_find_best_ref_mvs_from_stack(int allow_hp,
260 const MB_MODE_INFO_EXT *mbmi_ext,
261 MV_REFERENCE_FRAME ref_frame,
262 int_mv *nearest_mv, int_mv *near_mv,
263 int is_integer) {
264 const int ref_idx = 0;
265 MV_REFERENCE_FRAME ref_frames[2] = { ref_frame, NONE_FRAME };
266 *nearest_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 0, mbmi_ext);
267 lower_mv_precision(&nearest_mv->as_mv, allow_hp, is_integer);
268 *near_mv = av1_get_ref_mv_from_stack(ref_idx, ref_frames, 1, mbmi_ext);
269 lower_mv_precision(&near_mv->as_mv, allow_hp, is_integer);
270 }
271