1 // Copyright 2019 The libgav1 Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/motion_vector.h"
16
17 #include <algorithm>
18 #include <cassert>
19 #include <cstdint>
20 #include <cstdlib>
21 #include <memory>
22
23 #include "src/dsp/dsp.h"
24 #include "src/utils/bit_mask_set.h"
25 #include "src/utils/common.h"
26 #include "src/utils/constants.h"
27 #include "src/utils/logging.h"
28
29 namespace libgav1 {
30 namespace {
31
32 // Entry at index i is computed as:
33 // Clip3(std::max(kBlockWidthPixels[i], kBlockHeightPixels[i], 16, 112)).
34 constexpr int kWarpValidThreshold[kMaxBlockSizes] = {
35 16, 16, 16, 16, 16, 16, 32, 16, 16, 16, 32,
36 64, 32, 32, 32, 64, 64, 64, 64, 112, 112, 112};
37
38 // 7.10.2.10.
LowerMvPrecision(const ObuFrameHeader & frame_header,MotionVector * const mvs)39 void LowerMvPrecision(const ObuFrameHeader& frame_header,
40 MotionVector* const mvs) {
41 if (frame_header.allow_high_precision_mv) return;
42 if (frame_header.force_integer_mv != 0) {
43 for (auto& mv : mvs->mv) {
44 // The next line is equivalent to:
45 // const int value = (std::abs(static_cast<int>(mv)) + 3) & ~7;
46 // const int sign = mv >> 15;
47 // mv = ApplySign(value, sign);
48 mv = (mv + 3 - (mv >> 15)) & ~7;
49 }
50 } else {
51 for (auto& mv : mvs->mv) {
52 // The next line is equivalent to:
53 // if ((mv & 1) != 0) mv += (mv > 0) ? -1 : 1;
54 mv = (mv - (mv >> 15)) & ~1;
55 }
56 }
57 }
58
59 // 7.10.2.1.
SetupGlobalMv(const Tile::Block & block,int index,MotionVector * const mv)60 void SetupGlobalMv(const Tile::Block& block, int index,
61 MotionVector* const mv) {
62 const BlockParameters& bp = *block.bp;
63 const ObuFrameHeader& frame_header = block.tile.frame_header();
64 ReferenceFrameType reference_type = bp.reference_frame[index];
65 const auto& gm = frame_header.global_motion[reference_type];
66 if (reference_type == kReferenceFrameIntra ||
67 gm.type == kGlobalMotionTransformationTypeIdentity) {
68 mv->mv32 = 0;
69 return;
70 }
71 if (gm.type == kGlobalMotionTransformationTypeTranslation) {
72 for (int i = 0; i < 2; ++i) {
73 mv->mv[i] = gm.params[i] >> (kWarpedModelPrecisionBits - 3);
74 }
75 LowerMvPrecision(frame_header, mv);
76 return;
77 }
78 const int x = MultiplyBy4(block.column4x4) + DivideBy2(block.width) - 1;
79 const int y = MultiplyBy4(block.row4x4) + DivideBy2(block.height) - 1;
80 const int xc = (gm.params[2] - (1 << kWarpedModelPrecisionBits)) * x +
81 gm.params[3] * y + gm.params[0];
82 const int yc = gm.params[4] * x +
83 (gm.params[5] - (1 << kWarpedModelPrecisionBits)) * y +
84 gm.params[1];
85 if (frame_header.allow_high_precision_mv) {
86 mv->mv[MotionVector::kRow] =
87 RightShiftWithRoundingSigned(yc, kWarpedModelPrecisionBits - 3);
88 mv->mv[MotionVector::kColumn] =
89 RightShiftWithRoundingSigned(xc, kWarpedModelPrecisionBits - 3);
90 } else {
91 mv->mv[MotionVector::kRow] = MultiplyBy2(
92 RightShiftWithRoundingSigned(yc, kWarpedModelPrecisionBits - 2));
93 mv->mv[MotionVector::kColumn] = MultiplyBy2(
94 RightShiftWithRoundingSigned(xc, kWarpedModelPrecisionBits - 2));
95 LowerMvPrecision(frame_header, mv);
96 }
97 }
98
99 constexpr BitMaskSet kPredictionModeNewMvMask(kPredictionModeNewMv,
100 kPredictionModeNewNewMv,
101 kPredictionModeNearNewMv,
102 kPredictionModeNewNearMv,
103 kPredictionModeNearestNewMv,
104 kPredictionModeNewNearestMv);
105
106 // 7.10.2.8.
SearchStack(const Tile::Block & block,const BlockParameters & mv_bp,int index,int weight,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)107 void SearchStack(const Tile::Block& block, const BlockParameters& mv_bp,
108 int index, int weight, bool* const found_new_mv,
109 bool* const found_match, int* const num_mv_found) {
110 const BlockParameters& bp = *block.bp;
111 const std::array<GlobalMotion, kNumReferenceFrameTypes>& global_motion =
112 block.tile.frame_header().global_motion;
113 PredictionParameters& prediction_parameters = *bp.prediction_parameters;
114 MotionVector candidate_mv;
115 // LowerMvPrecision() is not necessary, since the values in
116 // |prediction_parameters.global_mv| and |mv_bp.mv| were generated by it.
117 const auto global_motion_type = global_motion[bp.reference_frame[0]].type;
118 if (IsGlobalMvBlock(mv_bp.is_global_mv_block, global_motion_type)) {
119 candidate_mv = prediction_parameters.global_mv[0];
120 } else {
121 candidate_mv = mv_bp.mv.mv[index];
122 }
123 *found_new_mv |= kPredictionModeNewMvMask.Contains(mv_bp.y_mode);
124 *found_match = true;
125 MotionVector* const ref_mv_stack = prediction_parameters.ref_mv_stack;
126 const int num_found = *num_mv_found;
127 const auto result = std::find_if(ref_mv_stack, ref_mv_stack + num_found,
128 [&candidate_mv](const MotionVector& ref_mv) {
129 return ref_mv == candidate_mv;
130 });
131 if (result != ref_mv_stack + num_found) {
132 prediction_parameters.IncreaseWeight(std::distance(ref_mv_stack, result),
133 weight);
134 return;
135 }
136 if (num_found >= kMaxRefMvStackSize) return;
137 ref_mv_stack[num_found] = candidate_mv;
138 prediction_parameters.SetWeightIndexStackEntry(num_found, weight);
139 ++*num_mv_found;
140 }
141
142 // 7.10.2.9.
CompoundSearchStack(const Tile::Block & block,const BlockParameters & mv_bp,int weight,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)143 void CompoundSearchStack(const Tile::Block& block, const BlockParameters& mv_bp,
144 int weight, bool* const found_new_mv,
145 bool* const found_match, int* const num_mv_found) {
146 const BlockParameters& bp = *block.bp;
147 const std::array<GlobalMotion, kNumReferenceFrameTypes>& global_motion =
148 block.tile.frame_header().global_motion;
149 PredictionParameters& prediction_parameters = *bp.prediction_parameters;
150 // LowerMvPrecision() is not necessary, since the values in
151 // |prediction_parameters.global_mv| and |mv_bp.mv| were generated by it.
152 CompoundMotionVector candidate_mv = mv_bp.mv;
153 for (int i = 0; i < 2; ++i) {
154 const auto global_motion_type = global_motion[bp.reference_frame[i]].type;
155 if (IsGlobalMvBlock(mv_bp.is_global_mv_block, global_motion_type)) {
156 candidate_mv.mv[i] = prediction_parameters.global_mv[i];
157 }
158 }
159 *found_new_mv |= kPredictionModeNewMvMask.Contains(mv_bp.y_mode);
160 *found_match = true;
161 CompoundMotionVector* const compound_ref_mv_stack =
162 prediction_parameters.compound_ref_mv_stack;
163 const int num_found = *num_mv_found;
164 const auto result =
165 std::find_if(compound_ref_mv_stack, compound_ref_mv_stack + num_found,
166 [&candidate_mv](const CompoundMotionVector& ref_mv) {
167 return ref_mv == candidate_mv;
168 });
169 if (result != compound_ref_mv_stack + num_found) {
170 prediction_parameters.IncreaseWeight(
171 std::distance(compound_ref_mv_stack, result), weight);
172 return;
173 }
174 if (num_found >= kMaxRefMvStackSize) return;
175 compound_ref_mv_stack[num_found] = candidate_mv;
176 prediction_parameters.SetWeightIndexStackEntry(num_found, weight);
177 ++*num_mv_found;
178 }
179
180 // 7.10.2.7.
AddReferenceMvCandidate(const Tile::Block & block,const BlockParameters & mv_bp,bool is_compound,int weight,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)181 void AddReferenceMvCandidate(const Tile::Block& block,
182 const BlockParameters& mv_bp, bool is_compound,
183 int weight, bool* const found_new_mv,
184 bool* const found_match, int* const num_mv_found) {
185 if (!mv_bp.is_inter) return;
186 const BlockParameters& bp = *block.bp;
187 if (is_compound) {
188 if (mv_bp.reference_frame[0] == bp.reference_frame[0] &&
189 mv_bp.reference_frame[1] == bp.reference_frame[1]) {
190 CompoundSearchStack(block, mv_bp, weight, found_new_mv, found_match,
191 num_mv_found);
192 }
193 return;
194 }
195 for (int i = 0; i < 2; ++i) {
196 if (mv_bp.reference_frame[i] == bp.reference_frame[0]) {
197 SearchStack(block, mv_bp, i, weight, found_new_mv, found_match,
198 num_mv_found);
199 }
200 }
201 }
202
GetMinimumStep(int block_width_or_height4x4,int delta_row_or_column)203 int GetMinimumStep(int block_width_or_height4x4, int delta_row_or_column) {
204 assert(delta_row_or_column < 0);
205 if (block_width_or_height4x4 >= 16) return 4;
206 if (delta_row_or_column < -1) return 2;
207 return 0;
208 }
209
210 // 7.10.2.2.
ScanRow(const Tile::Block & block,int mv_column,int delta_row,bool is_compound,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)211 void ScanRow(const Tile::Block& block, int mv_column, int delta_row,
212 bool is_compound, bool* const found_new_mv,
213 bool* const found_match, int* const num_mv_found) {
214 const int mv_row = block.row4x4 + delta_row;
215 const Tile& tile = block.tile;
216 if (!tile.IsTopInside(mv_row + 1)) return;
217 const int width4x4 = block.width4x4;
218 const int min_step = GetMinimumStep(width4x4, delta_row);
219 BlockParameters** bps = tile.BlockParametersAddress(mv_row, mv_column);
220 BlockParameters** const end_bps =
221 bps + std::min({static_cast<int>(width4x4),
222 tile.frame_header().columns4x4 - block.column4x4, 16});
223 do {
224 const BlockParameters& mv_bp = **bps;
225 const int step = std::max(
226 std::min(width4x4, static_cast<int>(kNum4x4BlocksWide[mv_bp.size])),
227 min_step);
228 AddReferenceMvCandidate(block, mv_bp, is_compound, MultiplyBy2(step),
229 found_new_mv, found_match, num_mv_found);
230 bps += step;
231 } while (bps < end_bps);
232 }
233
234 // 7.10.2.3.
ScanColumn(const Tile::Block & block,int mv_row,int delta_column,bool is_compound,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)235 void ScanColumn(const Tile::Block& block, int mv_row, int delta_column,
236 bool is_compound, bool* const found_new_mv,
237 bool* const found_match, int* const num_mv_found) {
238 const int mv_column = block.column4x4 + delta_column;
239 const Tile& tile = block.tile;
240 if (!tile.IsLeftInside(mv_column + 1)) return;
241 const int height4x4 = block.height4x4;
242 const int min_step = GetMinimumStep(height4x4, delta_column);
243 const ptrdiff_t stride = tile.BlockParametersStride();
244 BlockParameters** bps = tile.BlockParametersAddress(mv_row, mv_column);
245 BlockParameters** const end_bps =
246 bps + stride * std::min({static_cast<int>(height4x4),
247 tile.frame_header().rows4x4 - block.row4x4, 16});
248 do {
249 const BlockParameters& mv_bp = **bps;
250 const int step = std::max(
251 std::min(height4x4, static_cast<int>(kNum4x4BlocksHigh[mv_bp.size])),
252 min_step);
253 AddReferenceMvCandidate(block, mv_bp, is_compound, MultiplyBy2(step),
254 found_new_mv, found_match, num_mv_found);
255 bps += step * stride;
256 } while (bps < end_bps);
257 }
258
259 // 7.10.2.4.
ScanPoint(const Tile::Block & block,int delta_row,int delta_column,bool is_compound,bool * const found_new_mv,bool * const found_match,int * const num_mv_found)260 void ScanPoint(const Tile::Block& block, int delta_row, int delta_column,
261 bool is_compound, bool* const found_new_mv,
262 bool* const found_match, int* const num_mv_found) {
263 const int mv_row = block.row4x4 + delta_row;
264 const int mv_column = block.column4x4 + delta_column;
265 const Tile& tile = block.tile;
266 if (!tile.IsInside(mv_row, mv_column) ||
267 !tile.HasParameters(mv_row, mv_column)) {
268 return;
269 }
270 const BlockParameters& mv_bp = tile.Parameters(mv_row, mv_column);
271 if (mv_bp.reference_frame[0] == kReferenceFrameNone) return;
272 AddReferenceMvCandidate(block, mv_bp, is_compound, 4, found_new_mv,
273 found_match, num_mv_found);
274 }
275
276 // 7.10.2.6.
AddTemporalReferenceMvCandidate(const ObuFrameHeader & frame_header,const int reference_offsets[2],const MotionVector * const temporal_mvs,const int8_t * const temporal_reference_offsets,int count,bool is_compound,int * const zero_mv_context,int * const num_mv_found,PredictionParameters * const prediction_parameters)277 void AddTemporalReferenceMvCandidate(
278 const ObuFrameHeader& frame_header, const int reference_offsets[2],
279 const MotionVector* const temporal_mvs,
280 const int8_t* const temporal_reference_offsets, int count, bool is_compound,
281 int* const zero_mv_context, int* const num_mv_found,
282 PredictionParameters* const prediction_parameters) {
283 const int mv_projection_function_index =
284 frame_header.allow_high_precision_mv ? 2 : frame_header.force_integer_mv;
285 const MotionVector* const global_mv = prediction_parameters->global_mv;
286 if (is_compound) {
287 CompoundMotionVector candidate_mvs[kMaxTemporalMvCandidatesWithPadding];
288 const dsp::Dsp& dsp = *dsp::GetDspTable(8);
289 dsp.mv_projection_compound[mv_projection_function_index](
290 temporal_mvs, temporal_reference_offsets, reference_offsets, count,
291 candidate_mvs);
292 if (*zero_mv_context == -1) {
293 int max_difference =
294 std::max(std::abs(candidate_mvs[0].mv[0].mv[0] - global_mv[0].mv[0]),
295 std::abs(candidate_mvs[0].mv[0].mv[1] - global_mv[0].mv[1]));
296 max_difference =
297 std::max(max_difference,
298 std::abs(candidate_mvs[0].mv[1].mv[0] - global_mv[1].mv[0]));
299 max_difference =
300 std::max(max_difference,
301 std::abs(candidate_mvs[0].mv[1].mv[1] - global_mv[1].mv[1]));
302 *zero_mv_context = static_cast<int>(max_difference >= 16);
303 }
304 CompoundMotionVector* const compound_ref_mv_stack =
305 prediction_parameters->compound_ref_mv_stack;
306 int num_found = *num_mv_found;
307 int index = 0;
308 do {
309 const CompoundMotionVector& candidate_mv = candidate_mvs[index];
310 const auto result =
311 std::find_if(compound_ref_mv_stack, compound_ref_mv_stack + num_found,
312 [&candidate_mv](const CompoundMotionVector& ref_mv) {
313 return ref_mv == candidate_mv;
314 });
315 if (result != compound_ref_mv_stack + num_found) {
316 prediction_parameters->IncreaseWeight(
317 std::distance(compound_ref_mv_stack, result), 2);
318 continue;
319 }
320 if (num_found >= kMaxRefMvStackSize) continue;
321 compound_ref_mv_stack[num_found] = candidate_mv;
322 prediction_parameters->SetWeightIndexStackEntry(num_found, 2);
323 ++num_found;
324 } while (++index < count);
325 *num_mv_found = num_found;
326 return;
327 }
328 MotionVector* const ref_mv_stack = prediction_parameters->ref_mv_stack;
329 if (reference_offsets[0] == 0) {
330 if (*zero_mv_context == -1) {
331 const int max_difference =
332 std::max(std::abs(global_mv[0].mv[0]), std::abs(global_mv[0].mv[1]));
333 *zero_mv_context = static_cast<int>(max_difference >= 16);
334 }
335 const MotionVector candidate_mv = {};
336 const int num_found = *num_mv_found;
337 const auto result =
338 std::find_if(ref_mv_stack, ref_mv_stack + num_found,
339 [&candidate_mv](const MotionVector& ref_mv) {
340 return ref_mv == candidate_mv;
341 });
342 if (result != ref_mv_stack + num_found) {
343 prediction_parameters->IncreaseWeight(std::distance(ref_mv_stack, result),
344 2 * count);
345 return;
346 }
347 if (num_found >= kMaxRefMvStackSize) return;
348 ref_mv_stack[num_found] = candidate_mv;
349 prediction_parameters->SetWeightIndexStackEntry(num_found, 2 * count);
350 ++*num_mv_found;
351 return;
352 }
353 alignas(kMaxAlignment)
354 MotionVector candidate_mvs[kMaxTemporalMvCandidatesWithPadding];
355 const dsp::Dsp& dsp = *dsp::GetDspTable(8);
356 dsp.mv_projection_single[mv_projection_function_index](
357 temporal_mvs, temporal_reference_offsets, reference_offsets[0], count,
358 candidate_mvs);
359 if (*zero_mv_context == -1) {
360 const int max_difference =
361 std::max(std::abs(candidate_mvs[0].mv[0] - global_mv[0].mv[0]),
362 std::abs(candidate_mvs[0].mv[1] - global_mv[0].mv[1]));
363 *zero_mv_context = static_cast<int>(max_difference >= 16);
364 }
365 int num_found = *num_mv_found;
366 int index = 0;
367 do {
368 const MotionVector& candidate_mv = candidate_mvs[index];
369 const auto result =
370 std::find_if(ref_mv_stack, ref_mv_stack + num_found,
371 [&candidate_mv](const MotionVector& ref_mv) {
372 return ref_mv == candidate_mv;
373 });
374 if (result != ref_mv_stack + num_found) {
375 prediction_parameters->IncreaseWeight(std::distance(ref_mv_stack, result),
376 2);
377 continue;
378 }
379 if (num_found >= kMaxRefMvStackSize) continue;
380 ref_mv_stack[num_found] = candidate_mv;
381 prediction_parameters->SetWeightIndexStackEntry(num_found, 2);
382 ++num_found;
383 } while (++index < count);
384 *num_mv_found = num_found;
385 }
386
387 // Part of 7.10.2.5.
IsWithinTheSame64x64Block(const Tile::Block & block,int delta_row,int delta_column)388 bool IsWithinTheSame64x64Block(const Tile::Block& block, int delta_row,
389 int delta_column) {
390 const int row = (block.row4x4 & 15) + delta_row;
391 const int column = (block.column4x4 & 15) + delta_column;
392 // |block.height4x4| is at least 2 for all elements in |kTemporalScanMask|.
393 // So |row| are all non-negative.
394 assert(row >= 0);
395 return row < 16 && column >= 0 && column < 16;
396 }
397
398 constexpr BitMaskSet kTemporalScanMask(kBlock8x8, kBlock8x16, kBlock8x32,
399 kBlock16x8, kBlock16x16, kBlock16x32,
400 kBlock32x8, kBlock32x16, kBlock32x32);
401
402 // 7.10.2.5.
TemporalScan(const Tile::Block & block,bool is_compound,int * const zero_mv_context,int * const num_mv_found)403 void TemporalScan(const Tile::Block& block, bool is_compound,
404 int* const zero_mv_context, int* const num_mv_found) {
405 const int step_w = (block.width4x4 >= 16) ? 4 : 2;
406 const int step_h = (block.height4x4 >= 16) ? 4 : 2;
407 const int row_start = block.row4x4 | 1;
408 const int column_start = block.column4x4 | 1;
409 const int row_end =
410 row_start + std::min(static_cast<int>(block.height4x4), 16);
411 const int column_end =
412 column_start + std::min(static_cast<int>(block.width4x4), 16);
413 const Tile& tile = block.tile;
414 const TemporalMotionField& motion_field = tile.motion_field();
415 const int stride = motion_field.mv.columns();
416 const MotionVector* motion_field_mv = motion_field.mv[0];
417 const int8_t* motion_field_reference_offset =
418 motion_field.reference_offset[0];
419 alignas(kMaxAlignment)
420 MotionVector temporal_mvs[kMaxTemporalMvCandidatesWithPadding];
421 int8_t temporal_reference_offsets[kMaxTemporalMvCandidatesWithPadding];
422 int count = 0;
423 int offset = stride * (row_start >> 1);
424 int mv_row = row_start;
425 do {
426 int mv_column = column_start;
427 do {
428 // Both horizontal and vertical offsets are positive. Only bottom and
429 // right boundaries need to be checked.
430 if (tile.IsBottomRightInside(mv_row, mv_column)) {
431 const int x8 = mv_column >> 1;
432 const MotionVector temporal_mv = motion_field_mv[offset + x8];
433 if (temporal_mv.mv[0] == kInvalidMvValue) {
434 if (mv_row == row_start && mv_column == column_start) {
435 *zero_mv_context = 1;
436 }
437 } else {
438 temporal_mvs[count] = temporal_mv;
439 temporal_reference_offsets[count++] =
440 motion_field_reference_offset[offset + x8];
441 }
442 }
443 mv_column += step_w;
444 } while (mv_column < column_end);
445 offset += stride * step_h >> 1;
446 mv_row += step_h;
447 } while (mv_row < row_end);
448 if (kTemporalScanMask.Contains(block.size)) {
449 const int temporal_sample_positions[3][2] = {
450 {block.height4x4, -2},
451 {block.height4x4, block.width4x4},
452 {block.height4x4 - 2, block.width4x4}};
453 // Getting the address of an element in Array2D is slow. Precalculate the
454 // offsets.
455 int temporal_sample_offsets[3];
456 temporal_sample_offsets[0] = stride * ((row_start + block.height4x4) >> 1) +
457 ((column_start - 2) >> 1);
458 temporal_sample_offsets[1] =
459 temporal_sample_offsets[0] + ((block.width4x4 + 2) >> 1);
460 temporal_sample_offsets[2] = temporal_sample_offsets[1] - stride;
461 for (int i = 0; i < 3; i++) {
462 const int row = temporal_sample_positions[i][0];
463 const int column = temporal_sample_positions[i][1];
464 if (!IsWithinTheSame64x64Block(block, row, column)) continue;
465 const int mv_row = row_start + row;
466 const int mv_column = column_start + column;
467 // IsWithinTheSame64x64Block() guarantees the reference block is inside
468 // the top and left boundary.
469 if (!tile.IsBottomRightInside(mv_row, mv_column)) continue;
470 const MotionVector temporal_mv =
471 motion_field_mv[temporal_sample_offsets[i]];
472 if (temporal_mv.mv[0] != kInvalidMvValue) {
473 temporal_mvs[count] = temporal_mv;
474 temporal_reference_offsets[count++] =
475 motion_field_reference_offset[temporal_sample_offsets[i]];
476 }
477 }
478 }
479 if (count != 0) {
480 BlockParameters* const bp = block.bp;
481 int reference_offsets[2];
482 const int offset_0 = tile.current_frame()
483 .reference_info()
484 ->relative_distance_to[bp->reference_frame[0]];
485 reference_offsets[0] =
486 Clip3(offset_0, -kMaxFrameDistance, kMaxFrameDistance);
487 if (is_compound) {
488 const int offset_1 = tile.current_frame()
489 .reference_info()
490 ->relative_distance_to[bp->reference_frame[1]];
491 reference_offsets[1] =
492 Clip3(offset_1, -kMaxFrameDistance, kMaxFrameDistance);
493 // Pad so that SIMD implementations won't read uninitialized memory.
494 if ((count & 1) != 0) {
495 temporal_mvs[count].mv32 = 0;
496 temporal_reference_offsets[count] = 0;
497 }
498 } else {
499 // Pad so that SIMD implementations won't read uninitialized memory.
500 for (int i = count; i < ((count + 3) & ~3); ++i) {
501 temporal_mvs[i].mv32 = 0;
502 temporal_reference_offsets[i] = 0;
503 }
504 }
505 AddTemporalReferenceMvCandidate(
506 tile.frame_header(), reference_offsets, temporal_mvs,
507 temporal_reference_offsets, count, is_compound, zero_mv_context,
508 num_mv_found, &(*bp->prediction_parameters));
509 }
510 }
511
512 // Part of 7.10.2.13.
AddExtraCompoundMvCandidate(const Tile::Block & block,int mv_row,int mv_column,int * const ref_id_count,MotionVector ref_id[2][2],int * const ref_diff_count,MotionVector ref_diff[2][2])513 void AddExtraCompoundMvCandidate(const Tile::Block& block, int mv_row,
514 int mv_column, int* const ref_id_count,
515 MotionVector ref_id[2][2],
516 int* const ref_diff_count,
517 MotionVector ref_diff[2][2]) {
518 const auto& bp = block.tile.Parameters(mv_row, mv_column);
519 const std::array<bool, kNumReferenceFrameTypes>& reference_frame_sign_bias =
520 block.tile.reference_frame_sign_bias();
521 for (int i = 0; i < 2; ++i) {
522 const ReferenceFrameType candidate_reference_frame = bp.reference_frame[i];
523 if (candidate_reference_frame <= kReferenceFrameIntra) continue;
524 for (int j = 0; j < 2; ++j) {
525 MotionVector candidate_mv = bp.mv.mv[i];
526 const ReferenceFrameType block_reference_frame =
527 block.bp->reference_frame[j];
528 if (candidate_reference_frame == block_reference_frame &&
529 ref_id_count[j] < 2) {
530 ref_id[j][ref_id_count[j]] = candidate_mv;
531 ++ref_id_count[j];
532 } else if (ref_diff_count[j] < 2) {
533 if (reference_frame_sign_bias[candidate_reference_frame] !=
534 reference_frame_sign_bias[block_reference_frame]) {
535 candidate_mv.mv[0] *= -1;
536 candidate_mv.mv[1] *= -1;
537 }
538 ref_diff[j][ref_diff_count[j]] = candidate_mv;
539 ++ref_diff_count[j];
540 }
541 }
542 }
543 }
544
545 // Part of 7.10.2.13.
AddExtraSingleMvCandidate(const Tile::Block & block,int mv_row,int mv_column,int * const num_mv_found)546 void AddExtraSingleMvCandidate(const Tile::Block& block, int mv_row,
547 int mv_column, int* const num_mv_found) {
548 const auto& bp = block.tile.Parameters(mv_row, mv_column);
549 const std::array<bool, kNumReferenceFrameTypes>& reference_frame_sign_bias =
550 block.tile.reference_frame_sign_bias();
551 const ReferenceFrameType block_reference_frame = block.bp->reference_frame[0];
552 PredictionParameters& prediction_parameters =
553 *block.bp->prediction_parameters;
554 MotionVector* const ref_mv_stack = prediction_parameters.ref_mv_stack;
555 int num_found = *num_mv_found;
556 for (int i = 0; i < 2; ++i) {
557 const ReferenceFrameType candidate_reference_frame = bp.reference_frame[i];
558 if (candidate_reference_frame <= kReferenceFrameIntra) continue;
559 MotionVector candidate_mv = bp.mv.mv[i];
560 if (reference_frame_sign_bias[candidate_reference_frame] !=
561 reference_frame_sign_bias[block_reference_frame]) {
562 candidate_mv.mv[0] *= -1;
563 candidate_mv.mv[1] *= -1;
564 }
565 assert(num_found <= 2);
566 if ((num_found != 0 && ref_mv_stack[0] == candidate_mv) ||
567 (num_found == 2 && ref_mv_stack[1] == candidate_mv)) {
568 continue;
569 }
570 ref_mv_stack[num_found] = candidate_mv;
571 prediction_parameters.SetWeightIndexStackEntry(num_found, 0);
572 ++num_found;
573 }
574 *num_mv_found = num_found;
575 }
576
577 // 7.10.2.12.
ExtraSearch(const Tile::Block & block,bool is_compound,int * const num_mv_found)578 void ExtraSearch(const Tile::Block& block, bool is_compound,
579 int* const num_mv_found) {
580 const Tile& tile = block.tile;
581 const int num4x4 = std::min({static_cast<int>(block.width4x4),
582 tile.frame_header().columns4x4 - block.column4x4,
583 static_cast<int>(block.height4x4),
584 tile.frame_header().rows4x4 - block.row4x4, 16});
585 int ref_id_count[2] = {};
586 MotionVector ref_id[2][2] = {};
587 int ref_diff_count[2] = {};
588 MotionVector ref_diff[2][2] = {};
589 PredictionParameters& prediction_parameters =
590 *block.bp->prediction_parameters;
591 for (int pass = 0; pass < 2 && *num_mv_found < 2; ++pass) {
592 for (int i = 0; i < num4x4;) {
593 const int mv_row = block.row4x4 + ((pass == 0) ? -1 : i);
594 const int mv_column = block.column4x4 + ((pass == 0) ? i : -1);
595 if (!tile.IsTopLeftInside(mv_row + 1, mv_column + 1)) break;
596 if (is_compound) {
597 AddExtraCompoundMvCandidate(block, mv_row, mv_column, ref_id_count,
598 ref_id, ref_diff_count, ref_diff);
599 } else {
600 AddExtraSingleMvCandidate(block, mv_row, mv_column, num_mv_found);
601 if (*num_mv_found >= 2) break;
602 }
603 const auto& bp = tile.Parameters(mv_row, mv_column);
604 i +=
605 (pass == 0) ? kNum4x4BlocksWide[bp.size] : kNum4x4BlocksHigh[bp.size];
606 }
607 }
608 if (is_compound) {
609 // Merge compound mode extra search into mv stack.
610 CompoundMotionVector* const compound_ref_mv_stack =
611 prediction_parameters.compound_ref_mv_stack;
612 CompoundMotionVector combined_mvs[2] = {};
613 for (int i = 0; i < 2; ++i) {
614 int count = 0;
615 assert(ref_id_count[i] <= 2);
616 for (int j = 0; j < ref_id_count[i]; ++j, ++count) {
617 combined_mvs[count].mv[i] = ref_id[i][j];
618 }
619 for (int j = 0; j < ref_diff_count[i] && count < 2; ++j, ++count) {
620 combined_mvs[count].mv[i] = ref_diff[i][j];
621 }
622 for (; count < 2; ++count) {
623 combined_mvs[count].mv[i] = prediction_parameters.global_mv[i];
624 }
625 }
626 if (*num_mv_found == 1) {
627 if (combined_mvs[0] == compound_ref_mv_stack[0]) {
628 compound_ref_mv_stack[1] = combined_mvs[1];
629 } else {
630 compound_ref_mv_stack[1] = combined_mvs[0];
631 }
632 prediction_parameters.SetWeightIndexStackEntry(1, 0);
633 } else {
634 assert(*num_mv_found == 0);
635 for (int i = 0; i < 2; ++i) {
636 compound_ref_mv_stack[i] = combined_mvs[i];
637 prediction_parameters.SetWeightIndexStackEntry(i, 0);
638 }
639 }
640 *num_mv_found = 2;
641 } else {
642 // single prediction mode
643 MotionVector* const ref_mv_stack = prediction_parameters.ref_mv_stack;
644 for (int i = *num_mv_found; i < 2; ++i) {
645 ref_mv_stack[i] = prediction_parameters.global_mv[0];
646 prediction_parameters.SetWeightIndexStackEntry(i, 0);
647 }
648 }
649 }
650
DescendingOrderTwo(int * const a,int * const b)651 void DescendingOrderTwo(int* const a, int* const b) {
652 if (*a < *b) {
653 std::swap(*a, *b);
654 }
655 }
656
657 // Comparator used for sorting candidate motion vectors in descending order of
658 // their weights (as specified in 7.10.2.11).
CompareCandidateMotionVectors(const int16_t & lhs,const int16_t & rhs)659 bool CompareCandidateMotionVectors(const int16_t& lhs, const int16_t& rhs) {
660 return lhs > rhs;
661 }
662
SortWeightIndexStack(const int size,const int sort_to_n,int16_t * const weight_index_stack)663 void SortWeightIndexStack(const int size, const int sort_to_n,
664 int16_t* const weight_index_stack) {
665 if (size <= 1) return;
666 if (size <= 3) {
667 // Specialize small sort sizes to speed up.
668 int weight_index_0 = weight_index_stack[0];
669 int weight_index_1 = weight_index_stack[1];
670 DescendingOrderTwo(&weight_index_0, &weight_index_1);
671 if (size == 3) {
672 int weight_index_2 = weight_index_stack[2];
673 DescendingOrderTwo(&weight_index_1, &weight_index_2);
674 DescendingOrderTwo(&weight_index_0, &weight_index_1);
675 weight_index_stack[2] = weight_index_2;
676 }
677 weight_index_stack[0] = weight_index_0;
678 weight_index_stack[1] = weight_index_1;
679 return;
680 }
681 if (sort_to_n == 1) {
682 // std::max_element() is not efficient. Find the max element in a loop.
683 int16_t max_element = weight_index_stack[0];
684 int i = 1;
685 do {
686 max_element = std::max(max_element, weight_index_stack[i]);
687 } while (++i < size);
688 weight_index_stack[0] = max_element;
689 return;
690 }
691 std::partial_sort(&weight_index_stack[0], &weight_index_stack[sort_to_n],
692 &weight_index_stack[size], CompareCandidateMotionVectors);
693 }
694
695 // 7.10.2.14 (part 2).
ComputeContexts(bool found_new_mv,int nearest_matches,int total_matches,int * new_mv_context,int * reference_mv_context)696 void ComputeContexts(bool found_new_mv, int nearest_matches, int total_matches,
697 int* new_mv_context, int* reference_mv_context) {
698 switch (nearest_matches) {
699 case 0:
700 *new_mv_context = std::min(total_matches, 1);
701 *reference_mv_context = total_matches;
702 break;
703 case 1:
704 *new_mv_context = 3 - static_cast<int>(found_new_mv);
705 *reference_mv_context = 2 + total_matches;
706 break;
707 default:
708 *new_mv_context = 5 - static_cast<int>(found_new_mv);
709 *reference_mv_context = 5;
710 break;
711 }
712 }
713
714 // 7.10.4.2.
AddSample(const Tile::Block & block,int delta_row,int delta_column,int * const num_warp_samples,int * const num_samples_scanned,int candidates[kMaxLeastSquaresSamples][4])715 void AddSample(const Tile::Block& block, int delta_row, int delta_column,
716 int* const num_warp_samples, int* const num_samples_scanned,
717 int candidates[kMaxLeastSquaresSamples][4]) {
718 if (*num_samples_scanned >= kMaxLeastSquaresSamples) return;
719 const int mv_row = block.row4x4 + delta_row;
720 const int mv_column = block.column4x4 + delta_column;
721 const Tile& tile = block.tile;
722 if (!tile.IsInside(mv_row, mv_column) ||
723 !tile.HasParameters(mv_row, mv_column)) {
724 return;
725 }
726 const BlockParameters& bp = *block.bp;
727 const BlockParameters& mv_bp = tile.Parameters(mv_row, mv_column);
728 if (mv_bp.reference_frame[0] != bp.reference_frame[0] ||
729 mv_bp.reference_frame[1] != kReferenceFrameNone) {
730 return;
731 }
732 ++*num_samples_scanned;
733 const int candidate_height4x4 = kNum4x4BlocksHigh[mv_bp.size];
734 const int candidate_row = mv_row & ~(candidate_height4x4 - 1);
735 const int candidate_width4x4 = kNum4x4BlocksWide[mv_bp.size];
736 const int candidate_column = mv_column & ~(candidate_width4x4 - 1);
737 const BlockParameters& candidate_bp =
738 tile.Parameters(candidate_row, candidate_column);
739 const int mv_diff_row =
740 std::abs(candidate_bp.mv.mv[0].mv[0] - bp.mv.mv[0].mv[0]);
741 const int mv_diff_column =
742 std::abs(candidate_bp.mv.mv[0].mv[1] - bp.mv.mv[0].mv[1]);
743 const bool is_valid =
744 mv_diff_row + mv_diff_column <= kWarpValidThreshold[block.size];
745 if (!is_valid && *num_samples_scanned > 1) {
746 return;
747 }
748 const int mid_y =
749 MultiplyBy4(candidate_row) + MultiplyBy2(candidate_height4x4) - 1;
750 const int mid_x =
751 MultiplyBy4(candidate_column) + MultiplyBy2(candidate_width4x4) - 1;
752 candidates[*num_warp_samples][0] = MultiplyBy8(mid_y);
753 candidates[*num_warp_samples][1] = MultiplyBy8(mid_x);
754 candidates[*num_warp_samples][2] =
755 MultiplyBy8(mid_y) + candidate_bp.mv.mv[0].mv[0];
756 candidates[*num_warp_samples][3] =
757 MultiplyBy8(mid_x) + candidate_bp.mv.mv[0].mv[1];
758 if (is_valid) ++*num_warp_samples;
759 }
760
761 // 7.9.2.
762 // In the spec, |dst_sign| is either 1 or -1. Here we set |dst_sign| to either 0
763 // or -1 so that it can be XORed and subtracted directly in ApplySign() and
764 // corresponding SIMD implementations.
MotionFieldProjection(const ObuFrameHeader & frame_header,const std::array<RefCountedBufferPtr,kNumReferenceFrameTypes> & reference_frames,ReferenceFrameType source,int reference_to_current_with_sign,int dst_sign,int y8_start,int y8_end,int x8_start,int x8_end,TemporalMotionField * const motion_field)765 bool MotionFieldProjection(
766 const ObuFrameHeader& frame_header,
767 const std::array<RefCountedBufferPtr, kNumReferenceFrameTypes>&
768 reference_frames,
769 ReferenceFrameType source, int reference_to_current_with_sign, int dst_sign,
770 int y8_start, int y8_end, int x8_start, int x8_end,
771 TemporalMotionField* const motion_field) {
772 const int source_index =
773 frame_header.reference_frame_index[source - kReferenceFrameLast];
774 auto* const source_frame = reference_frames[source_index].get();
775 assert(source_frame != nullptr);
776 assert(dst_sign == 0 || dst_sign == -1);
777 if (source_frame->rows4x4() != frame_header.rows4x4 ||
778 source_frame->columns4x4() != frame_header.columns4x4 ||
779 IsIntraFrame(source_frame->frame_type())) {
780 return false;
781 }
782 assert(reference_to_current_with_sign >= -kMaxFrameDistance);
783 if (reference_to_current_with_sign > kMaxFrameDistance) return true;
784 const ReferenceInfo& reference_info = *source_frame->reference_info();
785 const dsp::Dsp& dsp = *dsp::GetDspTable(8);
786 dsp.motion_field_projection_kernel(
787 reference_info, reference_to_current_with_sign, dst_sign, y8_start,
788 y8_end, x8_start, x8_end, motion_field);
789 return true;
790 }
791
792 } // namespace
793
FindMvStack(const Tile::Block & block,bool is_compound,MvContexts * const contexts)794 void FindMvStack(const Tile::Block& block, bool is_compound,
795 MvContexts* const contexts) {
796 PredictionParameters& prediction_parameters =
797 *block.bp->prediction_parameters;
798 SetupGlobalMv(block, 0, &prediction_parameters.global_mv[0]);
799 if (is_compound) SetupGlobalMv(block, 1, &prediction_parameters.global_mv[1]);
800 bool found_new_mv = false;
801 bool found_row_match = false;
802 int num_mv_found = 0;
803 ScanRow(block, block.column4x4, -1, is_compound, &found_new_mv,
804 &found_row_match, &num_mv_found);
805 bool found_column_match = false;
806 ScanColumn(block, block.row4x4, -1, is_compound, &found_new_mv,
807 &found_column_match, &num_mv_found);
808 if (std::max(block.width4x4, block.height4x4) <= 16) {
809 ScanPoint(block, -1, block.width4x4, is_compound, &found_new_mv,
810 &found_row_match, &num_mv_found);
811 }
812 const int nearest_matches =
813 static_cast<int>(found_row_match) + static_cast<int>(found_column_match);
814 prediction_parameters.nearest_mv_count = num_mv_found;
815 if (block.tile.frame_header().use_ref_frame_mvs) {
816 // Initialize to invalid value, and it will be set when temporal mv is zero.
817 contexts->zero_mv = -1;
818 TemporalScan(block, is_compound, &contexts->zero_mv, &num_mv_found);
819 } else {
820 contexts->zero_mv = 0;
821 }
822 bool dummy_bool = false;
823 ScanPoint(block, -1, -1, is_compound, &dummy_bool, &found_row_match,
824 &num_mv_found);
825 static constexpr int deltas[2] = {-3, -5};
826 for (int i = 0; i < 2; ++i) {
827 if (i == 0 || block.height4x4 > 1) {
828 ScanRow(block, block.column4x4 | 1, deltas[i] + (block.row4x4 & 1),
829 is_compound, &dummy_bool, &found_row_match, &num_mv_found);
830 }
831 if (i == 0 || block.width4x4 > 1) {
832 ScanColumn(block, block.row4x4 | 1, deltas[i] + (block.column4x4 & 1),
833 is_compound, &dummy_bool, &found_column_match, &num_mv_found);
834 }
835 }
836 if (num_mv_found < 2) {
837 ExtraSearch(block, is_compound, &num_mv_found);
838 } else {
839 // The sort of |weight_index_stack| could be moved to Tile::AssignIntraMv()
840 // and Tile::AssignInterMv(), and only do a partial sort to the max index we
841 // need. However, the speed gain is trivial.
842 // For intra case, only the first 1 or 2 mvs in the stack will be used.
843 // For inter case, |prediction_parameters.ref_mv_index| is at most 3.
844 // We only need to do the partial sort up to the first 4 mvs.
845 SortWeightIndexStack(prediction_parameters.nearest_mv_count, 4,
846 prediction_parameters.weight_index_stack);
847 // When there are 4 or more nearest mvs, the other mvs will not be used.
848 if (prediction_parameters.nearest_mv_count < 4) {
849 SortWeightIndexStack(
850 num_mv_found - prediction_parameters.nearest_mv_count,
851 4 - prediction_parameters.nearest_mv_count,
852 prediction_parameters.weight_index_stack +
853 prediction_parameters.nearest_mv_count);
854 }
855 }
856 prediction_parameters.ref_mv_count = num_mv_found;
857 const int total_matches =
858 static_cast<int>(found_row_match) + static_cast<int>(found_column_match);
859 ComputeContexts(found_new_mv, nearest_matches, total_matches,
860 &contexts->new_mv, &contexts->reference_mv);
861 // The mv stack clamping process is in Tile::AssignIntraMv() and
862 // Tile::AssignInterMv(), and only up to two mvs are clamped.
863 }
864
FindWarpSamples(const Tile::Block & block,int * const num_warp_samples,int * const num_samples_scanned,int candidates[kMaxLeastSquaresSamples][4])865 void FindWarpSamples(const Tile::Block& block, int* const num_warp_samples,
866 int* const num_samples_scanned,
867 int candidates[kMaxLeastSquaresSamples][4]) {
868 const Tile& tile = block.tile;
869 bool top_left = true;
870 bool top_right = true;
871 int step = 1;
872 if (block.top_available[kPlaneY]) {
873 BlockSize source_size =
874 tile.Parameters(block.row4x4 - 1, block.column4x4).size;
875 const int source_width4x4 = kNum4x4BlocksWide[source_size];
876 if (block.width4x4 <= source_width4x4) {
877 // The & here is equivalent to % since source_width4x4 is a power of two.
878 const int column_offset = -(block.column4x4 & (source_width4x4 - 1));
879 if (column_offset < 0) top_left = false;
880 if (column_offset + source_width4x4 > block.width4x4) top_right = false;
881 AddSample(block, -1, 0, num_warp_samples, num_samples_scanned,
882 candidates);
883 } else {
884 for (int i = 0;
885 i < std::min(static_cast<int>(block.width4x4),
886 tile.frame_header().columns4x4 - block.column4x4);
887 i += step) {
888 source_size =
889 tile.Parameters(block.row4x4 - 1, block.column4x4 + i).size;
890 step = std::min(static_cast<int>(block.width4x4),
891 static_cast<int>(kNum4x4BlocksWide[source_size]));
892 AddSample(block, -1, i, num_warp_samples, num_samples_scanned,
893 candidates);
894 }
895 }
896 }
897 if (block.left_available[kPlaneY]) {
898 BlockSize source_size =
899 tile.Parameters(block.row4x4, block.column4x4 - 1).size;
900 const int source_height4x4 = kNum4x4BlocksHigh[source_size];
901 if (block.height4x4 <= source_height4x4) {
902 const int row_offset = -(block.row4x4 & (source_height4x4 - 1));
903 if (row_offset < 0) top_left = false;
904 AddSample(block, 0, -1, num_warp_samples, num_samples_scanned,
905 candidates);
906 } else {
907 for (int i = 0; i < std::min(static_cast<int>(block.height4x4),
908 tile.frame_header().rows4x4 - block.row4x4);
909 i += step) {
910 source_size =
911 tile.Parameters(block.row4x4 + i, block.column4x4 - 1).size;
912 step = std::min(static_cast<int>(block.height4x4),
913 static_cast<int>(kNum4x4BlocksHigh[source_size]));
914 AddSample(block, i, -1, num_warp_samples, num_samples_scanned,
915 candidates);
916 }
917 }
918 }
919 if (top_left) {
920 AddSample(block, -1, -1, num_warp_samples, num_samples_scanned, candidates);
921 }
922 if (top_right && block.size <= kBlock64x64) {
923 AddSample(block, -1, block.width4x4, num_warp_samples, num_samples_scanned,
924 candidates);
925 }
926 if (*num_warp_samples == 0 && *num_samples_scanned > 0) *num_warp_samples = 1;
927 }
928
SetupMotionField(const ObuFrameHeader & frame_header,const RefCountedBuffer & current_frame,const std::array<RefCountedBufferPtr,kNumReferenceFrameTypes> & reference_frames,int row4x4_start,int row4x4_end,int column4x4_start,int column4x4_end,TemporalMotionField * const motion_field)929 void SetupMotionField(
930 const ObuFrameHeader& frame_header, const RefCountedBuffer& current_frame,
931 const std::array<RefCountedBufferPtr, kNumReferenceFrameTypes>&
932 reference_frames,
933 int row4x4_start, int row4x4_end, int column4x4_start, int column4x4_end,
934 TemporalMotionField* const motion_field) {
935 assert(frame_header.use_ref_frame_mvs);
936 const int y8_start = DivideBy2(row4x4_start);
937 const int y8_end = DivideBy2(std::min(row4x4_end, frame_header.rows4x4));
938 const int x8_start = DivideBy2(column4x4_start);
939 const int x8_end =
940 DivideBy2(std::min(column4x4_end, frame_header.columns4x4));
941 const int last_index = frame_header.reference_frame_index[0];
942 const ReferenceInfo& reference_info = *current_frame.reference_info();
943 if (!IsIntraFrame(reference_frames[last_index]->frame_type())) {
944 const int last_alternate_order_hint =
945 reference_frames[last_index]
946 ->reference_info()
947 ->order_hint[kReferenceFrameAlternate];
948 const int current_gold_order_hint =
949 reference_info.order_hint[kReferenceFrameGolden];
950 if (last_alternate_order_hint != current_gold_order_hint) {
951 const int reference_offset_last =
952 -reference_info.relative_distance_from[kReferenceFrameLast];
953 if (std::abs(reference_offset_last) <= kMaxFrameDistance) {
954 MotionFieldProjection(frame_header, reference_frames,
955 kReferenceFrameLast, reference_offset_last, -1,
956 y8_start, y8_end, x8_start, x8_end, motion_field);
957 }
958 }
959 }
960 int ref_stamp = 1;
961 const int reference_offset_backward =
962 reference_info.relative_distance_from[kReferenceFrameBackward];
963 if (reference_offset_backward > 0 &&
964 MotionFieldProjection(frame_header, reference_frames,
965 kReferenceFrameBackward, reference_offset_backward,
966 0, y8_start, y8_end, x8_start, x8_end,
967 motion_field)) {
968 --ref_stamp;
969 }
970 const int reference_offset_alternate2 =
971 reference_info.relative_distance_from[kReferenceFrameAlternate2];
972 if (reference_offset_alternate2 > 0 &&
973 MotionFieldProjection(frame_header, reference_frames,
974 kReferenceFrameAlternate2,
975 reference_offset_alternate2, 0, y8_start, y8_end,
976 x8_start, x8_end, motion_field)) {
977 --ref_stamp;
978 }
979 if (ref_stamp >= 0) {
980 const int reference_offset_alternate =
981 reference_info.relative_distance_from[kReferenceFrameAlternate];
982 if (reference_offset_alternate > 0 &&
983 MotionFieldProjection(frame_header, reference_frames,
984 kReferenceFrameAlternate,
985 reference_offset_alternate, 0, y8_start, y8_end,
986 x8_start, x8_end, motion_field)) {
987 --ref_stamp;
988 }
989 }
990 if (ref_stamp >= 0) {
991 const int reference_offset_last2 =
992 -reference_info.relative_distance_from[kReferenceFrameLast2];
993 if (std::abs(reference_offset_last2) <= kMaxFrameDistance) {
994 MotionFieldProjection(frame_header, reference_frames,
995 kReferenceFrameLast2, reference_offset_last2, -1,
996 y8_start, y8_end, x8_start, x8_end, motion_field);
997 }
998 }
999 }
1000
1001 } // namespace libgav1
1002