• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/utils/block_parameters_holder.h"
16 
17 #include <algorithm>
18 
19 #include "src/utils/common.h"
20 #include "src/utils/constants.h"
21 #include "src/utils/logging.h"
22 #include "src/utils/parameter_tree.h"
23 #include "src/utils/types.h"
24 
25 namespace libgav1 {
26 
27 namespace {
28 
29 // Returns the number of super block rows/columns for |value4x4| where value4x4
30 // is either rows4x4 or columns4x4.
RowsOrColumns4x4ToSuperBlocks(int value4x4,bool use_128x128_superblock)31 int RowsOrColumns4x4ToSuperBlocks(int value4x4, bool use_128x128_superblock) {
32   return use_128x128_superblock ? DivideBy128(MultiplyBy4(value4x4) + 127)
33                                 : DivideBy64(MultiplyBy4(value4x4) + 63);
34 }
35 
36 }  // namespace
37 
Reset(int rows4x4,int columns4x4,bool use_128x128_superblock)38 bool BlockParametersHolder::Reset(int rows4x4, int columns4x4,
39                                   bool use_128x128_superblock) {
40   rows4x4_ = rows4x4;
41   columns4x4_ = columns4x4;
42   use_128x128_superblock_ = use_128x128_superblock;
43   if (!block_parameters_cache_.Reset(rows4x4_, columns4x4_)) {
44     LIBGAV1_DLOG(ERROR, "block_parameters_cache_.Reset() failed.");
45     return false;
46   }
47   const int rows =
48       RowsOrColumns4x4ToSuperBlocks(rows4x4_, use_128x128_superblock_);
49   const int columns =
50       RowsOrColumns4x4ToSuperBlocks(columns4x4_, use_128x128_superblock_);
51   const BlockSize sb_size =
52       use_128x128_superblock_ ? kBlock128x128 : kBlock64x64;
53   const int multiplier = kNum4x4BlocksWide[sb_size];
54   if (!trees_.Reset(rows, columns)) {
55     LIBGAV1_DLOG(ERROR, "trees_.Reset() failed.");
56     return false;
57   }
58   for (int i = 0; i < rows; ++i) {
59     for (int j = 0; j < columns; ++j) {
60       trees_[i][j] =
61           ParameterTree::Create(i * multiplier, j * multiplier, sb_size);
62       if (trees_[i][j] == nullptr) {
63         LIBGAV1_DLOG(ERROR, "Allocation of trees_[%d][%d] failed.", i, j);
64         return false;
65       }
66     }
67   }
68   return true;
69 }
70 
FillCache(int row4x4,int column4x4,BlockSize block_size,BlockParameters * const bp)71 void BlockParametersHolder::FillCache(int row4x4, int column4x4,
72                                       BlockSize block_size,
73                                       BlockParameters* const bp) {
74   int rows = std::min(static_cast<int>(kNum4x4BlocksHigh[block_size]),
75                       rows4x4_ - row4x4);
76   const int columns = std::min(static_cast<int>(kNum4x4BlocksWide[block_size]),
77                                columns4x4_ - column4x4);
78   auto* bp_dst = &block_parameters_cache_[row4x4][column4x4];
79   // Specialize columns cases (values in kNum4x4BlocksWide[]) for better
80   // performance.
81   if (columns == 1) {
82     SetBlock<BlockParameters*>(rows, 1, bp, bp_dst, columns4x4_);
83   } else if (columns == 2) {
84     SetBlock<BlockParameters*>(rows, 2, bp, bp_dst, columns4x4_);
85   } else if (columns == 4) {
86     SetBlock<BlockParameters*>(rows, 4, bp, bp_dst, columns4x4_);
87   } else if (columns == 8) {
88     SetBlock<BlockParameters*>(rows, 8, bp, bp_dst, columns4x4_);
89   } else if (columns == 16) {
90     SetBlock<BlockParameters*>(rows, 16, bp, bp_dst, columns4x4_);
91   } else if (columns == 32) {
92     SetBlock<BlockParameters*>(rows, 32, bp, bp_dst, columns4x4_);
93   } else {
94     do {
95       // The following loop has better performance than using std::fill().
96       // std::fill() has some overhead in checking zero loop count.
97       int x = columns;
98       auto* d = bp_dst;
99       do {
100         *d++ = bp;
101       } while (--x != 0);
102       bp_dst += columns4x4_;
103     } while (--rows != 0);
104   }
105 }
106 
107 }  // namespace libgav1
108