1*09537850SAkhilesh Sanikop /* 2*09537850SAkhilesh Sanikop * Copyright 2019 The libgav1 Authors 3*09537850SAkhilesh Sanikop * 4*09537850SAkhilesh Sanikop * Licensed under the Apache License, Version 2.0 (the "License"); 5*09537850SAkhilesh Sanikop * you may not use this file except in compliance with the License. 6*09537850SAkhilesh Sanikop * You may obtain a copy of the License at 7*09537850SAkhilesh Sanikop * 8*09537850SAkhilesh Sanikop * http://www.apache.org/licenses/LICENSE-2.0 9*09537850SAkhilesh Sanikop * 10*09537850SAkhilesh Sanikop * Unless required by applicable law or agreed to in writing, software 11*09537850SAkhilesh Sanikop * distributed under the License is distributed on an "AS IS" BASIS, 12*09537850SAkhilesh Sanikop * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*09537850SAkhilesh Sanikop * See the License for the specific language governing permissions and 14*09537850SAkhilesh Sanikop * limitations under the License. 15*09537850SAkhilesh Sanikop */ 16*09537850SAkhilesh Sanikop 17*09537850SAkhilesh Sanikop #ifndef LIBGAV1_SRC_UTILS_BLOCK_PARAMETERS_HOLDER_H_ 18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_UTILS_BLOCK_PARAMETERS_HOLDER_H_ 19*09537850SAkhilesh Sanikop 20*09537850SAkhilesh Sanikop #include <atomic> 21*09537850SAkhilesh Sanikop #include <memory> 22*09537850SAkhilesh Sanikop 23*09537850SAkhilesh Sanikop #include "src/utils/array_2d.h" 24*09537850SAkhilesh Sanikop #include "src/utils/compiler_attributes.h" 25*09537850SAkhilesh Sanikop #include "src/utils/constants.h" 26*09537850SAkhilesh Sanikop #include "src/utils/dynamic_buffer.h" 27*09537850SAkhilesh Sanikop #include "src/utils/types.h" 28*09537850SAkhilesh Sanikop 29*09537850SAkhilesh Sanikop namespace libgav1 { 30*09537850SAkhilesh Sanikop 31*09537850SAkhilesh Sanikop // Holds the BlockParameters pointers to each 4x4 block in the frame. 32*09537850SAkhilesh Sanikop class BlockParametersHolder { 33*09537850SAkhilesh Sanikop public: 34*09537850SAkhilesh Sanikop BlockParametersHolder() = default; 35*09537850SAkhilesh Sanikop 36*09537850SAkhilesh Sanikop // Not copyable or movable. 37*09537850SAkhilesh Sanikop BlockParametersHolder(const BlockParametersHolder&) = delete; 38*09537850SAkhilesh Sanikop BlockParametersHolder& operator=(const BlockParametersHolder&) = delete; 39*09537850SAkhilesh Sanikop 40*09537850SAkhilesh Sanikop LIBGAV1_MUST_USE_RESULT bool Reset(int rows4x4, int columns4x4); 41*09537850SAkhilesh Sanikop 42*09537850SAkhilesh Sanikop // Returns a pointer to a BlockParameters object that can be used safely until 43*09537850SAkhilesh Sanikop // the next call to Reset(). Returns nullptr on memory allocation failure. It 44*09537850SAkhilesh Sanikop // also fills the cache matrix for the block starting at |row4x4|, |column4x4| 45*09537850SAkhilesh Sanikop // of size |block_size| with the returned pointer. 46*09537850SAkhilesh Sanikop BlockParameters* Get(int row4x4, int column4x4, BlockSize block_size); 47*09537850SAkhilesh Sanikop 48*09537850SAkhilesh Sanikop // Finds the BlockParameters corresponding to |row4x4| and |column4x4|. This 49*09537850SAkhilesh Sanikop // is done as a simple look up of the |block_parameters_cache_| matrix. 50*09537850SAkhilesh Sanikop // Returns nullptr if the BlockParameters cannot be found. Find(int row4x4,int column4x4)51*09537850SAkhilesh Sanikop BlockParameters* Find(int row4x4, int column4x4) const { 52*09537850SAkhilesh Sanikop return block_parameters_cache_[row4x4][column4x4]; 53*09537850SAkhilesh Sanikop } 54*09537850SAkhilesh Sanikop Address(int row4x4,int column4x4)55*09537850SAkhilesh Sanikop BlockParameters** Address(int row4x4, int column4x4) { 56*09537850SAkhilesh Sanikop return block_parameters_cache_.data() + row4x4 * columns4x4_ + column4x4; 57*09537850SAkhilesh Sanikop } 58*09537850SAkhilesh Sanikop Address(int row4x4,int column4x4)59*09537850SAkhilesh Sanikop BlockParameters* const* Address(int row4x4, int column4x4) const { 60*09537850SAkhilesh Sanikop return block_parameters_cache_.data() + row4x4 * columns4x4_ + column4x4; 61*09537850SAkhilesh Sanikop } 62*09537850SAkhilesh Sanikop columns4x4()63*09537850SAkhilesh Sanikop int columns4x4() const { return columns4x4_; } 64*09537850SAkhilesh Sanikop 65*09537850SAkhilesh Sanikop private: 66*09537850SAkhilesh Sanikop // Needs access to FillCache for testing Cdef. 67*09537850SAkhilesh Sanikop template <int bitdepth, typename Pixel> 68*09537850SAkhilesh Sanikop friend class PostFilterApplyCdefTest; 69*09537850SAkhilesh Sanikop 70*09537850SAkhilesh Sanikop void FillCache(int row4x4, int column4x4, BlockSize block_size, 71*09537850SAkhilesh Sanikop BlockParameters* bp); 72*09537850SAkhilesh Sanikop 73*09537850SAkhilesh Sanikop int rows4x4_ = 0; 74*09537850SAkhilesh Sanikop int columns4x4_ = 0; 75*09537850SAkhilesh Sanikop 76*09537850SAkhilesh Sanikop // Owns the memory of BlockParameters pointers for the entire frame. It can 77*09537850SAkhilesh Sanikop // hold upto |rows4x4_| * |columns4x4_| objects. Each object will be allocated 78*09537850SAkhilesh Sanikop // on demand and re-used across frames. 79*09537850SAkhilesh Sanikop DynamicBuffer<std::unique_ptr<BlockParameters>> block_parameters_; 80*09537850SAkhilesh Sanikop 81*09537850SAkhilesh Sanikop // Points to the next available index of |block_parameters_|. 82*09537850SAkhilesh Sanikop std::atomic<int> index_; 83*09537850SAkhilesh Sanikop 84*09537850SAkhilesh Sanikop // This is a 2d array of size |rows4x4_| * |columns4x4_|. This is filled in by 85*09537850SAkhilesh Sanikop // FillCache() and used by Find() to perform look ups using exactly one look 86*09537850SAkhilesh Sanikop // up (instead of traversing the entire tree). 87*09537850SAkhilesh Sanikop Array2D<BlockParameters*> block_parameters_cache_; 88*09537850SAkhilesh Sanikop }; 89*09537850SAkhilesh Sanikop 90*09537850SAkhilesh Sanikop } // namespace libgav1 91*09537850SAkhilesh Sanikop 92*09537850SAkhilesh Sanikop #endif // LIBGAV1_SRC_UTILS_BLOCK_PARAMETERS_HOLDER_H_ 93