• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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_BLOCK_PARAMETERS_HOLDER_H_
18 #define LIBGAV1_SRC_UTILS_BLOCK_PARAMETERS_HOLDER_H_
19 
20 #include <atomic>
21 #include <memory>
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/dynamic_buffer.h"
27 #include "src/utils/types.h"
28 
29 namespace libgav1 {
30 
31 // Holds the BlockParameters pointers to each 4x4 block in the frame.
32 class BlockParametersHolder {
33  public:
34   BlockParametersHolder() = default;
35 
36   // Not copyable or movable.
37   BlockParametersHolder(const BlockParametersHolder&) = delete;
38   BlockParametersHolder& operator=(const BlockParametersHolder&) = delete;
39 
40   LIBGAV1_MUST_USE_RESULT bool Reset(int rows4x4, int columns4x4);
41 
42   // Returns a pointer to a BlockParameters object that can be used safely until
43   // the next call to Reset(). Returns nullptr on memory allocation failure. It
44   // also fills the cache matrix for the block starting at |row4x4|, |column4x4|
45   // of size |block_size| with the returned pointer.
46   BlockParameters* Get(int row4x4, int column4x4, BlockSize block_size);
47 
48   // Finds the BlockParameters corresponding to |row4x4| and |column4x4|. This
49   // is done as a simple look up of the |block_parameters_cache_| matrix.
50   // Returns nullptr if the BlockParameters cannot be found.
Find(int row4x4,int column4x4)51   BlockParameters* Find(int row4x4, int column4x4) const {
52     return block_parameters_cache_[row4x4][column4x4];
53   }
54 
Address(int row4x4,int column4x4)55   BlockParameters** Address(int row4x4, int column4x4) {
56     return block_parameters_cache_.data() + row4x4 * columns4x4_ + column4x4;
57   }
58 
Address(int row4x4,int column4x4)59   BlockParameters* const* Address(int row4x4, int column4x4) const {
60     return block_parameters_cache_.data() + row4x4 * columns4x4_ + column4x4;
61   }
62 
columns4x4()63   int columns4x4() const { return columns4x4_; }
64 
65  private:
66   // Needs access to FillCache for testing Cdef.
67   template <int bitdepth, typename Pixel>
68   friend class PostFilterApplyCdefTest;
69 
70   void FillCache(int row4x4, int column4x4, BlockSize block_size,
71                  BlockParameters* bp);
72 
73   int rows4x4_ = 0;
74   int columns4x4_ = 0;
75 
76   // Owns the memory of BlockParameters pointers for the entire frame. It can
77   // hold upto |rows4x4_| * |columns4x4_| objects. Each object will be allocated
78   // on demand and re-used across frames.
79   DynamicBuffer<std::unique_ptr<BlockParameters>> block_parameters_;
80 
81   // Points to the next available index of |block_parameters_|.
82   std::atomic<int> index_;
83 
84   // This is a 2d array of size |rows4x4_| * |columns4x4_|. This is filled in by
85   // FillCache() and used by Find() to perform look ups using exactly one look
86   // up (instead of traversing the entire tree).
87   Array2D<BlockParameters*> block_parameters_cache_;
88 };
89 
90 }  // namespace libgav1
91 
92 #endif  // LIBGAV1_SRC_UTILS_BLOCK_PARAMETERS_HOLDER_H_
93