1 // Copyright 2020 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/dsp/motion_field_projection.h"
16
17 #include <algorithm>
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21
22 #include "src/dsp/dsp.h"
23 #include "src/utils/common.h"
24 #include "src/utils/constants.h"
25 #include "src/utils/reference_info.h"
26 #include "src/utils/types.h"
27
28 namespace libgav1 {
29 namespace dsp {
30 namespace {
31
32 // Silence unused function warnings when MotionFieldProjectionKernel_C is
33 // not used.
34 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS || \
35 !defined(LIBGAV1_Dsp8bpp_MotionFieldProjectionKernel)
36
37 // 7.9.2.
MotionFieldProjectionKernel_C(const ReferenceInfo & reference_info,int reference_to_current_with_sign,int dst_sign,int y8_start,int y8_end,int x8_start,int x8_end,TemporalMotionField * motion_field)38 void MotionFieldProjectionKernel_C(const ReferenceInfo& reference_info,
39 int reference_to_current_with_sign,
40 int dst_sign, int y8_start, int y8_end,
41 int x8_start, int x8_end,
42 TemporalMotionField* motion_field) {
43 const ptrdiff_t stride = motion_field->mv.columns();
44 // The column range has to be offset by kProjectionMvMaxHorizontalOffset since
45 // coordinates in that range could end up being position_x8 because of
46 // projection.
47 const int adjusted_x8_start =
48 std::max(x8_start - kProjectionMvMaxHorizontalOffset, 0);
49 const int adjusted_x8_end = std::min(
50 x8_end + kProjectionMvMaxHorizontalOffset, static_cast<int>(stride));
51 const int8_t* const reference_offsets =
52 reference_info.relative_distance_to.data();
53 const bool* const skip_references = reference_info.skip_references.data();
54 const int16_t* const projection_divisions =
55 reference_info.projection_divisions.data();
56 const ReferenceFrameType* source_reference_types =
57 &reference_info.motion_field_reference_frame[y8_start][0];
58 const MotionVector* mv = &reference_info.motion_field_mv[y8_start][0];
59 int8_t* dst_reference_offset = motion_field->reference_offset[y8_start];
60 MotionVector* dst_mv = motion_field->mv[y8_start];
61 assert(stride == motion_field->reference_offset.columns());
62 assert((y8_start & 7) == 0);
63
64 int y8 = y8_start;
65 do {
66 const int y8_floor = (y8 & ~7) - y8;
67 const int y8_ceiling = std::min(y8_end - y8, y8_floor + 8);
68 int x8 = adjusted_x8_start;
69 do {
70 const int source_reference_type = source_reference_types[x8];
71 if (skip_references[source_reference_type]) continue;
72 MotionVector projection_mv;
73 // reference_to_current_with_sign could be 0.
74 GetMvProjection(mv[x8], reference_to_current_with_sign,
75 projection_divisions[source_reference_type],
76 &projection_mv);
77 // Do not update the motion vector if the block position is not valid or
78 // if position_x8 is outside the current range of x8_start and x8_end.
79 // Note that position_y8 will always be within the range of y8_start and
80 // y8_end.
81 const int position_y8 = Project(0, projection_mv.mv[0], dst_sign);
82 if (position_y8 < y8_floor || position_y8 >= y8_ceiling) continue;
83 const int x8_base = x8 & ~7;
84 const int x8_floor =
85 std::max(x8_start, x8_base - kProjectionMvMaxHorizontalOffset);
86 const int x8_ceiling =
87 std::min(x8_end, x8_base + 8 + kProjectionMvMaxHorizontalOffset);
88 const int position_x8 = Project(x8, projection_mv.mv[1], dst_sign);
89 if (position_x8 < x8_floor || position_x8 >= x8_ceiling) continue;
90 dst_mv[position_y8 * stride + position_x8] = mv[x8];
91 dst_reference_offset[position_y8 * stride + position_x8] =
92 reference_offsets[source_reference_type];
93 } while (++x8 < adjusted_x8_end);
94 source_reference_types += stride;
95 mv += stride;
96 dst_reference_offset += stride;
97 dst_mv += stride;
98 } while (++y8 < y8_end);
99 }
100
101 #endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS ||
102 // !defined(LIBGAV1_Dsp8bpp_MotionFieldProjectionKernel)
103
104 } // namespace
105
MotionFieldProjectionInit_C()106 void MotionFieldProjectionInit_C() {
107 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS || \
108 !defined(LIBGAV1_Dsp8bpp_MotionFieldProjectionKernel)
109 Dsp* const dsp = dsp_internal::GetWritableDspTable(kBitdepth8);
110 assert(dsp != nullptr);
111 dsp->motion_field_projection_kernel = MotionFieldProjectionKernel_C;
112 #endif
113 }
114
115 } // namespace dsp
116 } // namespace libgav1
117