xref: /aosp_15_r20/external/libgav1/src/utils/reference_info.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1 /*
2  * Copyright 2020 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_SRC_UTILS_REFERENCE_INFO_H_
18 #define LIBGAV1_SRC_UTILS_REFERENCE_INFO_H_
19 
20 #include <array>
21 #include <cstdint>
22 
23 #include "src/utils/array_2d.h"
24 #include "src/utils/compiler_attributes.h"
25 #include "src/utils/constants.h"
26 #include "src/utils/types.h"
27 
28 namespace libgav1 {
29 
30 // This struct collects some members related to reference frames in one place to
31 // make it easier to pass them as parameters to some dsp functions.
32 struct ReferenceInfo {
33   // Initialize |motion_field_reference_frame| so that
34   // Tile::StoreMotionFieldMvsIntoCurrentFrame() can skip some updates when
35   // the updates are the same as the initialized value.
36   // Set to kReferenceFrameIntra instead of kReferenceFrameNone to simplify
37   // branch conditions in motion field projection.
38   // The following memory initialization of contiguous memory is very fast. It
39   // is not recommended to make the initialization multi-threaded, unless the
40   // memory which needs to be initialized in each thread is still contiguous.
ResetReferenceInfo41   LIBGAV1_MUST_USE_RESULT bool Reset(int rows, int columns) {
42     return motion_field_reference_frame.Reset(rows, columns,
43                                               /*zero_initialize=*/true) &&
44            motion_field_mv.Reset(
45                rows, columns,
46 #if LIBGAV1_MSAN
47                // It is set in Tile::StoreMotionFieldMvsIntoCurrentFrame() only
48                // for qualified blocks. In MotionFieldProjectionKernel() dsp
49                // optimizations, it is read no matter it was set or not.
50                /*zero_initialize=*/true
51 #else
52                /*zero_initialize=*/false
53 #endif
54            );
55   }
56 
57   // All members are used by inter frames only.
58   // For intra frames, they are not initialized.
59 
60   std::array<uint8_t, kNumReferenceFrameTypes> order_hint;
61 
62   // An example when |relative_distance_from| does not equal
63   // -|relative_distance_to|:
64   // |relative_distance_from| = GetRelativeDistance(7, 71, 25) = -64
65   // -|relative_distance_to| = -GetRelativeDistance(71, 7, 25) = 64
66   // This is why we need both |relative_distance_from| and
67   // |relative_distance_to|.
68   // |relative_distance_from|: Relative distances from reference frames to this
69   // frame.
70   std::array<int8_t, kNumReferenceFrameTypes> relative_distance_from;
71   // |relative_distance_to|: Relative distances to reference frames.
72   std::array<int8_t, kNumReferenceFrameTypes> relative_distance_to;
73 
74   // Skip motion field projection of specific types of frames if their
75   // |relative_distance_to| is negative or too large.
76   std::array<bool, kNumReferenceFrameTypes> skip_references;
77   // Lookup table to get motion field projection division multiplier of specific
78   // types of frames. Derived from kProjectionMvDivisionLookup.
79   std::array<int16_t, kNumReferenceFrameTypes> projection_divisions;
80 
81   // The current frame's |motion_field_reference_frame| and |motion_field_mv_|
82   // are guaranteed to be allocated only when refresh_frame_flags is not 0.
83   // Array of size (rows4x4 / 2) x (columns4x4 / 2). Entry at i, j corresponds
84   // to MfRefFrames[i * 2 + 1][j * 2 + 1] in the spec.
85   Array2D<ReferenceFrameType> motion_field_reference_frame;
86   // Array of size (rows4x4 / 2) x (columns4x4 / 2). Entry at i, j corresponds
87   // to MfMvs[i * 2 + 1][j * 2 + 1] in the spec.
88   Array2D<MotionVector> motion_field_mv;
89 };
90 
91 }  // namespace libgav1
92 
93 #endif  // LIBGAV1_SRC_UTILS_REFERENCE_INFO_H_
94