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 <algorithm>
16 #include <cstdint>
17 #include <cstring>
18 
19 #include "src/dsp/constants.h"
20 #include "src/obu_parser.h"
21 #include "src/symbol_decoder_context.h"
22 #include "src/tile.h"
23 #include "src/utils/array_2d.h"
24 #include "src/utils/block_parameters_holder.h"
25 #include "src/utils/common.h"
26 #include "src/utils/constants.h"
27 #include "src/utils/entropy_decoder.h"
28 #include "src/utils/segmentation.h"
29 #include "src/utils/stack.h"
30 #include "src/utils/types.h"
31 
32 namespace libgav1 {
33 namespace {
34 
35 constexpr uint8_t kMaxVariableTransformTreeDepth = 2;
36 // Max_Tx_Depth array from section 5.11.5 in the spec with the following
37 // modification: If the element is not zero, it is subtracted by one. That is
38 // the only way in which this array is being used.
39 constexpr int kTxDepthCdfIndex[kMaxBlockSizes] = {
40     0, 0, 1, 0, 0, 1, 2, 1, 1, 1, 2, 3, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3};
41 
42 constexpr TransformSize kMaxTransformSizeRectangle[kMaxBlockSizes] = {
43     kTransformSize4x4,   kTransformSize4x8,   kTransformSize4x16,
44     kTransformSize8x4,   kTransformSize8x8,   kTransformSize8x16,
45     kTransformSize8x32,  kTransformSize16x4,  kTransformSize16x8,
46     kTransformSize16x16, kTransformSize16x32, kTransformSize16x64,
47     kTransformSize32x8,  kTransformSize32x16, kTransformSize32x32,
48     kTransformSize32x64, kTransformSize64x16, kTransformSize64x32,
49     kTransformSize64x64, kTransformSize64x64, kTransformSize64x64,
50     kTransformSize64x64};
51 
GetSquareTransformSize(uint8_t pixels)52 TransformSize GetSquareTransformSize(uint8_t pixels) {
53   switch (pixels) {
54     case 128:
55     case 64:
56       return kTransformSize64x64;
57     case 32:
58       return kTransformSize32x32;
59     case 16:
60       return kTransformSize16x16;
61     case 8:
62       return kTransformSize8x8;
63     default:
64       return kTransformSize4x4;
65   }
66 }
67 
68 }  // namespace
69 
GetTopTransformWidth(const Block & block,int row4x4,int column4x4,bool ignore_skip)70 int Tile::GetTopTransformWidth(const Block& block, int row4x4, int column4x4,
71                                bool ignore_skip) {
72   if (row4x4 == block.row4x4) {
73     if (!block.top_available[kPlaneY]) return 64;
74     const BlockParameters& bp_top =
75         *block_parameters_holder_.Find(row4x4 - 1, column4x4);
76     if ((ignore_skip || bp_top.skip) && bp_top.is_inter) {
77       return kBlockWidthPixels[bp_top.size];
78     }
79   }
80   return kTransformWidth[inter_transform_sizes_[row4x4 - 1][column4x4]];
81 }
82 
GetLeftTransformHeight(const Block & block,int row4x4,int column4x4,bool ignore_skip)83 int Tile::GetLeftTransformHeight(const Block& block, int row4x4, int column4x4,
84                                  bool ignore_skip) {
85   if (column4x4 == block.column4x4) {
86     if (!block.left_available[kPlaneY]) return 64;
87     const BlockParameters& bp_left =
88         *block_parameters_holder_.Find(row4x4, column4x4 - 1);
89     if ((ignore_skip || bp_left.skip) && bp_left.is_inter) {
90       return kBlockHeightPixels[bp_left.size];
91     }
92   }
93   return kTransformHeight[inter_transform_sizes_[row4x4][column4x4 - 1]];
94 }
95 
ReadFixedTransformSize(const Block & block)96 TransformSize Tile::ReadFixedTransformSize(const Block& block) {
97   BlockParameters& bp = *block.bp;
98   if (frame_header_.segmentation.lossless[bp.segment_id]) {
99     return kTransformSize4x4;
100   }
101   const TransformSize max_rect_tx_size = kMaxTransformSizeRectangle[block.size];
102   const bool allow_select = !bp.skip || !bp.is_inter;
103   if (block.size == kBlock4x4 || !allow_select ||
104       frame_header_.tx_mode != kTxModeSelect) {
105     return max_rect_tx_size;
106   }
107   const int max_tx_width = kTransformWidth[max_rect_tx_size];
108   const int max_tx_height = kTransformHeight[max_rect_tx_size];
109   const int top_width =
110       block.top_available[kPlaneY]
111           ? GetTopTransformWidth(block, block.row4x4, block.column4x4, true)
112           : 0;
113   const int left_height =
114       block.left_available[kPlaneY]
115           ? GetLeftTransformHeight(block, block.row4x4, block.column4x4, true)
116           : 0;
117   const auto context = static_cast<int>(top_width >= max_tx_width) +
118                        static_cast<int>(left_height >= max_tx_height);
119   const int cdf_index = kTxDepthCdfIndex[block.size];
120   const int symbol_count = 3 - static_cast<int>(cdf_index == 0);
121   const int tx_depth = reader_.ReadSymbol(
122       symbol_decoder_context_.tx_depth_cdf[cdf_index][context], symbol_count);
123   assert(tx_depth < 3);
124   TransformSize tx_size = max_rect_tx_size;
125   if (tx_depth == 0) return tx_size;
126   tx_size = kSplitTransformSize[tx_size];
127   if (tx_depth == 1) return tx_size;
128   return kSplitTransformSize[tx_size];
129 }
130 
ReadVariableTransformTree(const Block & block,int row4x4,int column4x4,TransformSize tx_size)131 void Tile::ReadVariableTransformTree(const Block& block, int row4x4,
132                                      int column4x4, TransformSize tx_size) {
133   const uint8_t pixels = std::max(block.width, block.height);
134   const TransformSize max_tx_size = GetSquareTransformSize(pixels);
135   const int context_delta = (kNumSquareTransformSizes - 1 -
136                              TransformSizeToSquareTransformIndex(max_tx_size)) *
137                             6;
138 
139   // Branching factor is 4 and maximum depth is 2. So the maximum stack size
140   // necessary is (4 - 1) + 4 = 7.
141   Stack<TransformTreeNode, 7> stack;
142   stack.Push(TransformTreeNode(column4x4, row4x4, tx_size, 0));
143 
144   while (!stack.Empty()) {
145     TransformTreeNode node = stack.Pop();
146     const int tx_width4x4 = kTransformWidth4x4[node.tx_size];
147     const int tx_height4x4 = kTransformHeight4x4[node.tx_size];
148     if (node.tx_size != kTransformSize4x4 &&
149         node.depth != kMaxVariableTransformTreeDepth) {
150       const auto top =
151           static_cast<int>(GetTopTransformWidth(block, node.y, node.x, false) <
152                            kTransformWidth[node.tx_size]);
153       const auto left = static_cast<int>(
154           GetLeftTransformHeight(block, node.y, node.x, false) <
155           kTransformHeight[node.tx_size]);
156       const int context =
157           static_cast<int>(max_tx_size > kTransformSize8x8 &&
158                            kTransformSizeSquareMax[node.tx_size] !=
159                                max_tx_size) *
160               3 +
161           context_delta + top + left;
162       // tx_split.
163       if (reader_.ReadSymbol(symbol_decoder_context_.tx_split_cdf[context])) {
164         const TransformSize sub_tx_size = kSplitTransformSize[node.tx_size];
165         const int step_width4x4 = kTransformWidth4x4[sub_tx_size];
166         const int step_height4x4 = kTransformHeight4x4[sub_tx_size];
167         // The loops have to run in reverse order because we use a stack for
168         // DFS.
169         for (int i = tx_height4x4 - step_height4x4; i >= 0;
170              i -= step_height4x4) {
171           for (int j = tx_width4x4 - step_width4x4; j >= 0;
172                j -= step_width4x4) {
173             if (node.y + i >= frame_header_.rows4x4 ||
174                 node.x + j >= frame_header_.columns4x4) {
175               continue;
176             }
177             stack.Push(TransformTreeNode(node.x + j, node.y + i, sub_tx_size,
178                                          node.depth + 1));
179           }
180         }
181         continue;
182       }
183     }
184     // tx_split is false.
185     for (int i = 0; i < tx_height4x4; ++i) {
186       static_assert(sizeof(TransformSize) == 1, "");
187       memset(&inter_transform_sizes_[node.y + i][node.x], node.tx_size,
188              tx_width4x4);
189     }
190     block_parameters_holder_.Find(node.y, node.x)->transform_size =
191         node.tx_size;
192   }
193 }
194 
DecodeTransformSize(const Block & block)195 void Tile::DecodeTransformSize(const Block& block) {
196   BlockParameters& bp = *block.bp;
197   if (frame_header_.tx_mode == kTxModeSelect && block.size > kBlock4x4 &&
198       bp.is_inter && !bp.skip &&
199       !frame_header_.segmentation.lossless[bp.segment_id]) {
200     const TransformSize max_tx_size = kMaxTransformSizeRectangle[block.size];
201     const int tx_width4x4 = kTransformWidth4x4[max_tx_size];
202     const int tx_height4x4 = kTransformHeight4x4[max_tx_size];
203     for (int row = block.row4x4; row < block.row4x4 + block.height4x4;
204          row += tx_height4x4) {
205       for (int column = block.column4x4;
206            column < block.column4x4 + block.width4x4; column += tx_width4x4) {
207         ReadVariableTransformTree(block, row, column, max_tx_size);
208       }
209     }
210   } else {
211     bp.transform_size = ReadFixedTransformSize(block);
212     for (int row = block.row4x4; row < block.row4x4 + block.height4x4; ++row) {
213       static_assert(sizeof(TransformSize) == 1, "");
214       memset(&inter_transform_sizes_[row][block.column4x4], bp.transform_size,
215              block.width4x4);
216     }
217   }
218 }
219 
220 }  // namespace libgav1
221