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