• 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_TILE_H_
18 #define LIBGAV1_SRC_TILE_H_
19 
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <condition_variable>  // NOLINT (unapproved c++11 header)
24 #include <cstddef>
25 #include <cstdint>
26 #include <memory>
27 #include <mutex>  // NOLINT (unapproved c++11 header)
28 #include <vector>
29 
30 #include "src/buffer_pool.h"
31 #include "src/decoder_state.h"
32 #include "src/dsp/common.h"
33 #include "src/dsp/constants.h"
34 #include "src/dsp/dsp.h"
35 #include "src/frame_scratch_buffer.h"
36 #include "src/loop_restoration_info.h"
37 #include "src/obu_parser.h"
38 #include "src/post_filter.h"
39 #include "src/quantizer.h"
40 #include "src/residual_buffer_pool.h"
41 #include "src/symbol_decoder_context.h"
42 #include "src/tile_scratch_buffer.h"
43 #include "src/utils/array_2d.h"
44 #include "src/utils/block_parameters_holder.h"
45 #include "src/utils/blocking_counter.h"
46 #include "src/utils/common.h"
47 #include "src/utils/compiler_attributes.h"
48 #include "src/utils/constants.h"
49 #include "src/utils/entropy_decoder.h"
50 #include "src/utils/memory.h"
51 #include "src/utils/segmentation_map.h"
52 #include "src/utils/threadpool.h"
53 #include "src/utils/types.h"
54 #include "src/yuv_buffer.h"
55 
56 namespace libgav1 {
57 
58 // Indicates what the ProcessSuperBlock() and TransformBlock() functions should
59 // do. "Parse" refers to consuming the bitstream, reading the transform
60 // coefficients and performing the dequantization. "Decode" refers to computing
61 // the prediction, applying the inverse transforms and adding the residual.
62 enum ProcessingMode {
63   kProcessingModeParseOnly,
64   kProcessingModeDecodeOnly,
65   kProcessingModeParseAndDecode,
66 };
67 
68 // The alignment requirement is due to the SymbolDecoderContext member
69 // symbol_decoder_context_.
70 class Tile : public MaxAlignedAllocable {
71  public:
Create(int tile_number,const uint8_t * const data,size_t size,const ObuSequenceHeader & sequence_header,const ObuFrameHeader & frame_header,RefCountedBuffer * const current_frame,const DecoderState & state,FrameScratchBuffer * const frame_scratch_buffer,const WedgeMaskArray & wedge_masks,const QuantizerMatrix & quantizer_matrix,SymbolDecoderContext * const saved_symbol_decoder_context,const SegmentationMap * prev_segment_ids,PostFilter * const post_filter,const dsp::Dsp * const dsp,ThreadPool * const thread_pool,BlockingCounterWithStatus * const pending_tiles,bool frame_parallel,bool use_intra_prediction_buffer)72   static std::unique_ptr<Tile> Create(
73       int tile_number, const uint8_t* const data, size_t size,
74       const ObuSequenceHeader& sequence_header,
75       const ObuFrameHeader& frame_header, RefCountedBuffer* const current_frame,
76       const DecoderState& state, FrameScratchBuffer* const frame_scratch_buffer,
77       const WedgeMaskArray& wedge_masks,
78       const QuantizerMatrix& quantizer_matrix,
79       SymbolDecoderContext* const saved_symbol_decoder_context,
80       const SegmentationMap* prev_segment_ids, PostFilter* const post_filter,
81       const dsp::Dsp* const dsp, ThreadPool* const thread_pool,
82       BlockingCounterWithStatus* const pending_tiles, bool frame_parallel,
83       bool use_intra_prediction_buffer) {
84     std::unique_ptr<Tile> tile(new (std::nothrow) Tile(
85         tile_number, data, size, sequence_header, frame_header, current_frame,
86         state, frame_scratch_buffer, wedge_masks, quantizer_matrix,
87         saved_symbol_decoder_context, prev_segment_ids, post_filter, dsp,
88         thread_pool, pending_tiles, frame_parallel,
89         use_intra_prediction_buffer));
90     return (tile != nullptr && tile->Init()) ? std::move(tile) : nullptr;
91   }
92 
93   // Move only.
94   Tile(Tile&& tile) noexcept;
95   Tile& operator=(Tile&& tile) noexcept;
96   Tile(const Tile&) = delete;
97   Tile& operator=(const Tile&) = delete;
98 
99   struct Block;  // Defined after this class.
100 
101   // Parses the entire tile.
102   bool Parse();
103   // Decodes the entire tile. |superblock_row_progress| and
104   // |superblock_row_progress_condvar| are arrays of size equal to the number of
105   // superblock rows in the frame. Increments |superblock_row_progress[i]| after
106   // each superblock row at index |i| is decoded. If the count reaches the
107   // number of tile columns, then it notifies
108   // |superblock_row_progress_condvar[i]|.
109   bool Decode(std::mutex* mutex, int* superblock_row_progress,
110               std::condition_variable* superblock_row_progress_condvar);
111   // Parses and decodes the entire tile. Depending on the configuration of this
112   // Tile, this function may do multithreaded decoding.
113   bool ParseAndDecode();  // 5.11.2.
114   // Processes all the columns of the superblock row at |row4x4| that are within
115   // this Tile. If |save_symbol_decoder_context| is true, then
116   // SaveSymbolDecoderContext() is invoked for the last superblock row.
117   template <ProcessingMode processing_mode, bool save_symbol_decoder_context>
118   bool ProcessSuperBlockRow(int row4x4, TileScratchBuffer* scratch_buffer);
119 
sequence_header()120   const ObuSequenceHeader& sequence_header() const { return sequence_header_; }
frame_header()121   const ObuFrameHeader& frame_header() const { return frame_header_; }
current_frame()122   const RefCountedBuffer& current_frame() const { return current_frame_; }
motion_field()123   const TemporalMotionField& motion_field() const { return motion_field_; }
reference_frame_sign_bias()124   const std::array<bool, kNumReferenceFrameTypes>& reference_frame_sign_bias()
125       const {
126     return reference_frame_sign_bias_;
127   }
128 
IsRow4x4Inside(int row4x4)129   bool IsRow4x4Inside(int row4x4) const {
130     return row4x4 >= row4x4_start_ && row4x4 < row4x4_end_;
131   }
132 
133   // 5.11.51.
IsInside(int row4x4,int column4x4)134   bool IsInside(int row4x4, int column4x4) const {
135     return IsRow4x4Inside(row4x4) && column4x4 >= column4x4_start_ &&
136            column4x4 < column4x4_end_;
137   }
138 
IsLeftInside(int column4x4)139   bool IsLeftInside(int column4x4) const {
140     // We use "larger than" as the condition. Don't pass in the left column
141     // offset column4x4 - 1.
142     assert(column4x4 <= column4x4_end_);
143     return column4x4 > column4x4_start_;
144   }
145 
IsTopInside(int row4x4)146   bool IsTopInside(int row4x4) const {
147     // We use "larger than" as the condition. Don't pass in the top row offset
148     // row4x4 - 1.
149     assert(row4x4 <= row4x4_end_);
150     return row4x4 > row4x4_start_;
151   }
152 
IsTopLeftInside(int row4x4,int column4x4)153   bool IsTopLeftInside(int row4x4, int column4x4) const {
154     // We use "larger than" as the condition. Don't pass in the top row offset
155     // row4x4 - 1 or the left column offset column4x4 - 1.
156     assert(row4x4 <= row4x4_end_);
157     assert(column4x4 <= column4x4_end_);
158     return row4x4 > row4x4_start_ && column4x4 > column4x4_start_;
159   }
160 
IsBottomRightInside(int row4x4,int column4x4)161   bool IsBottomRightInside(int row4x4, int column4x4) const {
162     assert(row4x4 >= row4x4_start_);
163     assert(column4x4 >= column4x4_start_);
164     return row4x4 < row4x4_end_ && column4x4 < column4x4_end_;
165   }
166 
BlockParametersAddress(int row4x4,int column4x4)167   BlockParameters** BlockParametersAddress(int row4x4, int column4x4) const {
168     return block_parameters_holder_.Address(row4x4, column4x4);
169   }
170 
BlockParametersStride()171   int BlockParametersStride() const {
172     return block_parameters_holder_.columns4x4();
173   }
174 
175   // Returns true if Parameters() can be called with |row| and |column| as
176   // inputs, false otherwise.
HasParameters(int row,int column)177   bool HasParameters(int row, int column) const {
178     return block_parameters_holder_.Find(row, column) != nullptr;
179   }
Parameters(int row,int column)180   const BlockParameters& Parameters(int row, int column) const {
181     return *block_parameters_holder_.Find(row, column);
182   }
183 
number()184   int number() const { return number_; }
superblock_rows()185   int superblock_rows() const { return superblock_rows_; }
superblock_columns()186   int superblock_columns() const { return superblock_columns_; }
row4x4_start()187   int row4x4_start() const { return row4x4_start_; }
column4x4_start()188   int column4x4_start() const { return column4x4_start_; }
column4x4_end()189   int column4x4_end() const { return column4x4_end_; }
190 
191  private:
192   // Stores the transform tree state when reading variable size transform trees
193   // and when applying the transform tree. When applying the transform tree,
194   // |depth| is not used.
195   struct TransformTreeNode {
196     // The default constructor is invoked by the Stack<TransformTreeNode, n>
197     // constructor. Stack<> does not use the default-constructed elements, so it
198     // is safe for the default constructor to not initialize the members.
199     TransformTreeNode() = default;
200     TransformTreeNode(int x, int y, TransformSize tx_size, int depth = -1)
xTransformTreeNode201         : x(x), y(y), tx_size(tx_size), depth(depth) {}
202 
203     int x;
204     int y;
205     TransformSize tx_size;
206     int depth;
207   };
208 
209   // Enum to track the processing state of a superblock.
210   enum SuperBlockState : uint8_t {
211     kSuperBlockStateNone,       // Not yet parsed or decoded.
212     kSuperBlockStateParsed,     // Parsed but not yet decoded.
213     kSuperBlockStateScheduled,  // Scheduled for decoding.
214     kSuperBlockStateDecoded     // Parsed and decoded.
215   };
216 
217   // Parameters used to facilitate multi-threading within the Tile.
218   struct ThreadingParameters {
219     std::mutex mutex;
220     // 2d array of size |superblock_rows_| by |superblock_columns_| containing
221     // the processing state of each superblock.
222     Array2D<SuperBlockState> sb_state LIBGAV1_GUARDED_BY(mutex);
223     // Variable used to indicate either parse or decode failure.
224     bool abort LIBGAV1_GUARDED_BY(mutex) = false;
225     int pending_jobs LIBGAV1_GUARDED_BY(mutex) = 0;
226     std::condition_variable pending_jobs_zero_condvar;
227   };
228 
229   // The residual pointer is used to traverse the |residual_buffer_|. It is
230   // used in two different ways.
231   // If |split_parse_and_decode_| is true:
232   //    The pointer points to the beginning of the |residual_buffer_| when the
233   //    "parse" and "decode" steps begin. It is then moved forward tx_size in
234   //    each iteration of the "parse" and the "decode" steps. In this case, the
235   //    ResidualPtr variable passed into various functions starting from
236   //    ProcessSuperBlock is used as an in/out parameter to keep track of the
237   //    residual pointer.
238   // If |split_parse_and_decode_| is false:
239   //    The pointer is reset to the beginning of the |residual_buffer_| for
240   //    every transform block.
241   using ResidualPtr = uint8_t*;
242 
243   Tile(int tile_number, const uint8_t* data, size_t size,
244        const ObuSequenceHeader& sequence_header,
245        const ObuFrameHeader& frame_header, RefCountedBuffer* current_frame,
246        const DecoderState& state, FrameScratchBuffer* frame_scratch_buffer,
247        const WedgeMaskArray& wedge_masks,
248        const QuantizerMatrix& quantizer_matrix,
249        SymbolDecoderContext* saved_symbol_decoder_context,
250        const SegmentationMap* prev_segment_ids, PostFilter* post_filter,
251        const dsp::Dsp* dsp, ThreadPool* thread_pool,
252        BlockingCounterWithStatus* pending_tiles, bool frame_parallel,
253        bool use_intra_prediction_buffer);
254 
255   // Performs member initializations that may fail. Helper function used by
256   // Create().
257   LIBGAV1_MUST_USE_RESULT bool Init();
258 
259   // Saves the symbol decoder context of this tile into
260   // |saved_symbol_decoder_context_| if necessary.
261   void SaveSymbolDecoderContext();
262 
263   // Entry point for multi-threaded decoding. This function performs the same
264   // functionality as ParseAndDecode(). The current thread does the "parse" step
265   // while the worker threads do the "decode" step.
266   bool ThreadedParseAndDecode();
267 
268   // Returns whether or not the prerequisites for decoding the superblock at
269   // |row_index| and |column_index| are satisfied. |threading_.mutex| must be
270   // held when calling this function.
271   bool CanDecode(int row_index, int column_index) const;
272 
273   // This function is run by the worker threads when multi-threaded decoding is
274   // enabled. Once a superblock is decoded, this function will set the
275   // corresponding |threading_.sb_state| entry to kSuperBlockStateDecoded. On
276   // failure, |threading_.abort| will be set to true. If at any point
277   // |threading_.abort| becomes true, this function will return as early as it
278   // can. If the decoding succeeds, this function will also schedule the
279   // decoding jobs for the superblock to the bottom-left and the superblock to
280   // the right of this superblock (if it is allowed).
281   void DecodeSuperBlock(int row_index, int column_index, int block_width4x4);
282 
283   // If |use_intra_prediction_buffer_| is true, then this function copies the
284   // last row of the superblockrow starting at |row4x4| into the
285   // |intra_prediction_buffer_| (which may be used by the intra prediction
286   // process for the next superblock row).
287   void PopulateIntraPredictionBuffer(int row4x4);
288 
289   uint16_t* GetPartitionCdf(int row4x4, int column4x4, BlockSize block_size);
290   bool ReadPartition(int row4x4, int column4x4, BlockSize block_size,
291                      bool has_rows, bool has_columns, Partition* partition);
292   // Processes the Partition starting at |row4x4_start|, |column4x4_start|
293   // iteratively. It performs a DFS traversal over the partition tree to process
294   // the blocks in the right order.
295   bool ProcessPartition(
296       int row4x4_start, int column4x4_start, TileScratchBuffer* scratch_buffer,
297       ResidualPtr* residual);  // Iterative implementation of 5.11.4.
298   bool ProcessBlock(int row4x4, int column4x4, BlockSize block_size,
299                     TileScratchBuffer* scratch_buffer,
300                     ResidualPtr* residual);   // 5.11.5.
301   void ResetCdef(int row4x4, int column4x4);  // 5.11.55.
302 
303   // This function is used to decode a superblock when the parsing has already
304   // been done for that superblock.
305   bool DecodeSuperBlock(int sb_row_index, int sb_column_index,
306                         TileScratchBuffer* scratch_buffer);
307   // Helper function used by DecodeSuperBlock(). Note that the decode_block()
308   // function in the spec is equivalent to ProcessBlock() in the code.
309   bool DecodeBlock(int row4x4, int column4x4, BlockSize block_size,
310                    TileScratchBuffer* scratch_buffer, ResidualPtr* residual);
311 
312   void ClearBlockDecoded(TileScratchBuffer* scratch_buffer, int row4x4,
313                          int column4x4);  // 5.11.3.
314   bool ProcessSuperBlock(int row4x4, int column4x4,
315                          TileScratchBuffer* scratch_buffer,
316                          ProcessingMode mode);
317   void ResetLoopRestorationParams();
318   void ReadLoopRestorationCoefficients(int row4x4, int column4x4,
319                                        BlockSize block_size);  // 5.11.57.
320 
321   // Helper functions for DecodeBlock.
322   bool ReadSegmentId(const Block& block);       // 5.11.9.
323   bool ReadIntraSegmentId(const Block& block);  // 5.11.8.
324   void ReadSkip(const Block& block);            // 5.11.11.
325   bool ReadSkipMode(const Block& block);        // 5.11.10.
326   void ReadCdef(const Block& block);            // 5.11.56.
327   // Returns the new value. |cdf| is an array of size kDeltaSymbolCount + 1.
328   int ReadAndClipDelta(uint16_t* cdf, int delta_small, int scale, int min_value,
329                        int max_value, int value);
330   void ReadQuantizerIndexDelta(const Block& block);  // 5.11.12.
331   void ReadLoopFilterDelta(const Block& block);      // 5.11.13.
332   // Populates |BlockParameters::deblock_filter_level| for the given |block|
333   // using |deblock_filter_levels_|.
334   void PopulateDeblockFilterLevel(const Block& block);
335   void PopulateCdefSkip(const Block& block);
336   void ReadPredictionModeY(const Block& block, bool intra_y_mode);
337   void ReadIntraAngleInfo(const Block& block,
338                           PlaneType plane_type);  // 5.11.42 and 5.11.43.
339   void ReadPredictionModeUV(const Block& block);
340   void ReadCflAlpha(const Block& block);  // 5.11.45.
341   int GetPaletteCache(const Block& block, PlaneType plane_type,
342                       uint16_t* cache);
343   void ReadPaletteColors(const Block& block, Plane plane);
344   void ReadPaletteModeInfo(const Block& block);      // 5.11.46.
345   void ReadFilterIntraModeInfo(const Block& block);  // 5.11.24.
346   int ReadMotionVectorComponent(const Block& block,
347                                 int component);                // 5.11.32.
348   void ReadMotionVector(const Block& block, int index);        // 5.11.31.
349   bool DecodeIntraModeInfo(const Block& block);                // 5.11.7.
350   int8_t ComputePredictedSegmentId(const Block& block) const;  // 5.11.21.
351   bool ReadInterSegmentId(const Block& block, bool pre_skip);  // 5.11.19.
352   void ReadIsInter(const Block& block, bool skip_mode);        // 5.11.20.
353   bool ReadIntraBlockModeInfo(const Block& block,
354                               bool intra_y_mode);  // 5.11.22.
355   CompoundReferenceType ReadCompoundReferenceType(const Block& block);
356   template <bool is_single, bool is_backward, int index>
357   uint16_t* GetReferenceCdf(const Block& block, CompoundReferenceType type =
358                                                     kNumCompoundReferenceTypes);
359   void ReadReferenceFrames(const Block& block, bool skip_mode);  // 5.11.25.
360   void ReadInterPredictionModeY(const Block& block,
361                                 const MvContexts& mode_contexts,
362                                 bool skip_mode);
363   void ReadRefMvIndex(const Block& block);
364   void ReadInterIntraMode(const Block& block, bool is_compound,
365                           bool skip_mode);        // 5.11.28.
IsScaled(ReferenceFrameType type)366   bool IsScaled(ReferenceFrameType type) const {  // Part of 5.11.27.
367     const int index =
368         frame_header_.reference_frame_index[type - kReferenceFrameLast];
369     return reference_frames_[index]->upscaled_width() != frame_header_.width ||
370            reference_frames_[index]->frame_height() != frame_header_.height;
371   }
372   void ReadMotionMode(const Block& block, bool is_compound,
373                       bool skip_mode);  // 5.11.27.
374   uint16_t* GetIsExplicitCompoundTypeCdf(const Block& block);
375   uint16_t* GetIsCompoundTypeAverageCdf(const Block& block);
376   void ReadCompoundType(const Block& block, bool is_compound, bool skip_mode,
377                         bool* is_explicit_compound_type,
378                         bool* is_compound_type_average);  // 5.11.29.
379   uint16_t* GetInterpolationFilterCdf(const Block& block, int direction);
380   void ReadInterpolationFilter(const Block& block, bool skip_mode);
381   bool ReadInterBlockModeInfo(const Block& block, bool skip_mode);  // 5.11.23.
382   bool DecodeInterModeInfo(const Block& block);                     // 5.11.18.
383   bool DecodeModeInfo(const Block& block);                          // 5.11.6.
384   bool IsMvValid(const Block& block, bool is_compound) const;       // 6.10.25.
385   bool AssignInterMv(const Block& block, bool is_compound);         // 5.11.26.
386   bool AssignIntraMv(const Block& block);                           // 5.11.26.
387   int GetTopTransformWidth(const Block& block, int row4x4, int column4x4,
388                            bool ignore_skip);
389   int GetLeftTransformHeight(const Block& block, int row4x4, int column4x4,
390                              bool ignore_skip);
391   TransformSize ReadFixedTransformSize(const Block& block);  // 5.11.15.
392   // Iterative implementation of 5.11.17.
393   void ReadVariableTransformTree(const Block& block, int row4x4, int column4x4,
394                                  TransformSize tx_size);
395   void DecodeTransformSize(const Block& block);  // 5.11.16.
396   bool ComputePrediction(const Block& block);    // 5.11.33.
397   // |x4| and |y4| are the column and row positions of the 4x4 block. |w4| and
398   // |h4| are the width and height in 4x4 units of |tx_size|.
399   int GetTransformAllZeroContext(const Block& block, Plane plane,
400                                  TransformSize tx_size, int x4, int y4, int w4,
401                                  int h4);
402   TransformSet GetTransformSet(TransformSize tx_size,
403                                bool is_inter) const;  // 5.11.48.
404   TransformType ComputeTransformType(const Block& block, Plane plane,
405                                      TransformSize tx_size, int block_x,
406                                      int block_y);  // 5.11.40.
407   void ReadTransformType(const Block& block, int x4, int y4,
408                          TransformSize tx_size);  // 5.11.47.
409   template <typename ResidualType>
410   void ReadCoeffBase2D(
411       const uint16_t* scan, TransformSize tx_size, int adjusted_tx_width_log2,
412       int eob,
413       uint16_t coeff_base_cdf[kCoeffBaseContexts][kCoeffBaseSymbolCount + 1],
414       uint16_t coeff_base_range_cdf[kCoeffBaseRangeContexts]
415                                    [kCoeffBaseRangeSymbolCount + 1],
416       ResidualType* quantized_buffer, uint8_t* level_buffer);
417   template <typename ResidualType>
418   void ReadCoeffBaseHorizontal(
419       const uint16_t* scan, TransformSize tx_size, int adjusted_tx_width_log2,
420       int eob,
421       uint16_t coeff_base_cdf[kCoeffBaseContexts][kCoeffBaseSymbolCount + 1],
422       uint16_t coeff_base_range_cdf[kCoeffBaseRangeContexts]
423                                    [kCoeffBaseRangeSymbolCount + 1],
424       ResidualType* quantized_buffer, uint8_t* level_buffer);
425   template <typename ResidualType>
426   void ReadCoeffBaseVertical(
427       const uint16_t* scan, TransformSize tx_size, int adjusted_tx_width_log2,
428       int eob,
429       uint16_t coeff_base_cdf[kCoeffBaseContexts][kCoeffBaseSymbolCount + 1],
430       uint16_t coeff_base_range_cdf[kCoeffBaseRangeContexts]
431                                    [kCoeffBaseRangeSymbolCount + 1],
432       ResidualType* quantized_buffer, uint8_t* level_buffer);
433   int GetDcSignContext(int x4, int y4, int w4, int h4, Plane plane);
434   void SetEntropyContexts(int x4, int y4, int w4, int h4, Plane plane,
435                           uint8_t coefficient_level, int8_t dc_category);
436   void InterIntraPrediction(
437       uint16_t* prediction_0, const uint8_t* prediction_mask,
438       ptrdiff_t prediction_mask_stride,
439       const PredictionParameters& prediction_parameters, int prediction_width,
440       int prediction_height, int subsampling_x, int subsampling_y,
441       uint8_t* dest,
442       ptrdiff_t dest_stride);  // Part of section 7.11.3.1 in the spec.
443   void CompoundInterPrediction(
444       const Block& block, const uint8_t* prediction_mask,
445       ptrdiff_t prediction_mask_stride, int prediction_width,
446       int prediction_height, int subsampling_x, int subsampling_y,
447       int candidate_row, int candidate_column, uint8_t* dest,
448       ptrdiff_t dest_stride);  // Part of section 7.11.3.1 in the spec.
449   GlobalMotion* GetWarpParams(const Block& block, Plane plane,
450                               int prediction_width, int prediction_height,
451                               const PredictionParameters& prediction_parameters,
452                               ReferenceFrameType reference_type,
453                               bool* is_local_valid,
454                               GlobalMotion* global_motion_params,
455                               GlobalMotion* local_warp_params)
456       const;  // Part of section 7.11.3.1 in the spec.
457   bool InterPrediction(const Block& block, Plane plane, int x, int y,
458                        int prediction_width, int prediction_height,
459                        int candidate_row, int candidate_column,
460                        bool* is_local_valid,
461                        GlobalMotion* local_warp_params);  // 7.11.3.1.
462   void ScaleMotionVector(const MotionVector& mv, Plane plane,
463                          int reference_frame_index, int x, int y, int* start_x,
464                          int* start_y, int* step_x, int* step_y);  // 7.11.3.3.
465   // If the method returns false, the caller only uses the output parameters
466   // *ref_block_start_x and *ref_block_start_y. If the method returns true, the
467   // caller uses all three output parameters.
468   static bool GetReferenceBlockPosition(
469       int reference_frame_index, bool is_scaled, int width, int height,
470       int ref_start_x, int ref_last_x, int ref_start_y, int ref_last_y,
471       int start_x, int start_y, int step_x, int step_y, int left_border,
472       int right_border, int top_border, int bottom_border,
473       int* ref_block_start_x, int* ref_block_start_y, int* ref_block_end_x);
474 
475   template <typename Pixel>
476   void BuildConvolveBlock(Plane plane, int reference_frame_index,
477                           bool is_scaled, int height, int ref_start_x,
478                           int ref_last_x, int ref_start_y, int ref_last_y,
479                           int step_y, int ref_block_start_x,
480                           int ref_block_end_x, int ref_block_start_y,
481                           uint8_t* block_buffer,
482                           ptrdiff_t convolve_buffer_stride,
483                           ptrdiff_t block_extended_width);
484   bool BlockInterPrediction(const Block& block, Plane plane,
485                             int reference_frame_index, const MotionVector& mv,
486                             int x, int y, int width, int height,
487                             int candidate_row, int candidate_column,
488                             uint16_t* prediction, bool is_compound,
489                             bool is_inter_intra, uint8_t* dest,
490                             ptrdiff_t dest_stride);  // 7.11.3.4.
491   bool BlockWarpProcess(const Block& block, Plane plane, int index,
492                         int block_start_x, int block_start_y, int width,
493                         int height, GlobalMotion* warp_params, bool is_compound,
494                         bool is_inter_intra, uint8_t* dest,
495                         ptrdiff_t dest_stride);  // 7.11.3.5.
496   bool ObmcBlockPrediction(const Block& block, const MotionVector& mv,
497                            Plane plane, int reference_frame_index, int width,
498                            int height, int x, int y, int candidate_row,
499                            int candidate_column,
500                            ObmcDirection blending_direction);
501   bool ObmcPrediction(const Block& block, Plane plane, int width,
502                       int height);  // 7.11.3.9.
503   void DistanceWeightedPrediction(void* prediction_0, void* prediction_1,
504                                   int width, int height, int candidate_row,
505                                   int candidate_column, uint8_t* dest,
506                                   ptrdiff_t dest_stride);  // 7.11.3.15.
507   // This function specializes the parsing of DC coefficient by removing some of
508   // the branches when i == 0 (since scan[0] is always 0 and scan[i] is always
509   // non-zero for all other possible values of i). |dc_category| is an output
510   // parameter that is populated when |is_dc_coefficient| is true.
511   // |coefficient_level| is an output parameter which accumulates the
512   // coefficient level.
513   template <typename ResidualType, bool is_dc_coefficient>
514   LIBGAV1_ALWAYS_INLINE bool ReadSignAndApplyDequantization(
515       const uint16_t* scan, int i, int q_value, const uint8_t* quantizer_matrix,
516       int shift, int max_value, uint16_t* dc_sign_cdf, int8_t* dc_category,
517       int* coefficient_level,
518       ResidualType* residual_buffer);     // Part of 5.11.39.
519   int ReadCoeffBaseRange(uint16_t* cdf);  // Part of 5.11.39.
520   // Returns the number of non-zero coefficients that were read. |tx_type| is an
521   // output parameter that stores the computed transform type for the plane
522   // whose coefficients were read. Returns -1 on failure.
523   template <typename ResidualType>
524   int ReadTransformCoefficients(const Block& block, Plane plane, int start_x,
525                                 int start_y, TransformSize tx_size,
526                                 TransformType* tx_type);  // 5.11.39.
527   bool TransformBlock(const Block& block, Plane plane, int base_x, int base_y,
528                       TransformSize tx_size, int x, int y,
529                       ProcessingMode mode);  // 5.11.35.
530   // Iterative implementation of 5.11.36.
531   bool TransformTree(const Block& block, int start_x, int start_y,
532                      BlockSize plane_size, ProcessingMode mode);
533   void ReconstructBlock(const Block& block, Plane plane, int start_x,
534                         int start_y, TransformSize tx_size,
535                         TransformType tx_type,
536                         int non_zero_coeff_count);         // Part of 7.12.3.
537   bool Residual(const Block& block, ProcessingMode mode);  // 5.11.34.
538   // part of 5.11.5 (reset_block_context() in the spec).
539   void ResetEntropyContext(const Block& block);
540   // Populates the |color_context| and |color_order| for the |i|th iteration
541   // with entries counting down from |start| to |end| (|start| > |end|).
542   void PopulatePaletteColorContexts(
543       const Block& block, PlaneType plane_type, int i, int start, int end,
544       uint8_t color_order[kMaxPaletteSquare][kMaxPaletteSize],
545       uint8_t color_context[kMaxPaletteSquare]);  // 5.11.50.
546   bool ReadPaletteTokens(const Block& block);     // 5.11.49.
547   template <typename Pixel>
548   void IntraPrediction(const Block& block, Plane plane, int x, int y,
549                        bool has_left, bool has_top, bool has_top_right,
550                        bool has_bottom_left, PredictionMode mode,
551                        TransformSize tx_size);
552   int GetIntraEdgeFilterType(const Block& block,
553                              Plane plane) const;  // 7.11.2.8.
554   template <typename Pixel>
555   void DirectionalPrediction(const Block& block, Plane plane, int x, int y,
556                              bool has_left, bool has_top, bool needs_left,
557                              bool needs_top, int prediction_angle, int width,
558                              int height, int max_x, int max_y,
559                              TransformSize tx_size, Pixel* top_row,
560                              Pixel* left_column);  // 7.11.2.4.
561   template <typename Pixel>
562   void PalettePrediction(const Block& block, Plane plane, int start_x,
563                          int start_y, int x, int y,
564                          TransformSize tx_size);  // 7.11.4.
565   template <typename Pixel>
566   void ChromaFromLumaPrediction(const Block& block, Plane plane, int start_x,
567                                 int start_y,
568                                 TransformSize tx_size);  // 7.11.5.
569   // Section 7.19. Applies some filtering and reordering to the motion vectors
570   // for the given |block| and stores them into |current_frame_|.
571   void StoreMotionFieldMvsIntoCurrentFrame(const Block& block);
572 
573   // SetCdfContext*() functions will populate the |left_context_| and
574   // |top_context_| for the |block|.
575   void SetCdfContextUsePredictedSegmentId(const Block& block,
576                                           bool use_predicted_segment_id);
577   void SetCdfContextCompoundType(const Block& block,
578                                  bool is_explicit_compound_type,
579                                  bool is_compound_type_average);
580   void SetCdfContextSkipMode(const Block& block, bool skip_mode);
581   void SetCdfContextPaletteSize(const Block& block);
582   void SetCdfContextUVMode(const Block& block);
583 
584   // Returns the zero-based index of the super block that contains |row4x4|
585   // relative to the start of this tile.
SuperBlockRowIndex(int row4x4)586   int SuperBlockRowIndex(int row4x4) const {
587     return (row4x4 - row4x4_start_) >>
588            (sequence_header_.use_128x128_superblock ? 5 : 4);
589   }
590 
591   // Returns the zero-based index of the super block that contains |column4x4|
592   // relative to the start of this tile.
SuperBlockColumnIndex(int column4x4)593   int SuperBlockColumnIndex(int column4x4) const {
594     return (column4x4 - column4x4_start_) >>
595            (sequence_header_.use_128x128_superblock ? 5 : 4);
596   }
597 
598   // Returns the zero-based index of the block that starts at row4x4 or
599   // column4x4 relative to the start of the superblock that contains the block.
600   // This is used to index into the members of |left_context_| and
601   // |top_context_|.
CdfContextIndex(int row_or_column4x4)602   int CdfContextIndex(int row_or_column4x4) const {
603     return row_or_column4x4 -
604            (row_or_column4x4 &
605             (sequence_header_.use_128x128_superblock ? ~31 : ~15));
606   }
607 
SuperBlockSize()608   BlockSize SuperBlockSize() const {
609     return sequence_header_.use_128x128_superblock ? kBlock128x128
610                                                    : kBlock64x64;
611   }
PlaneCount()612   int PlaneCount() const {
613     return sequence_header_.color_config.is_monochrome ? kMaxPlanesMonochrome
614                                                        : kMaxPlanes;
615   }
616 
617   const int number_;
618   const int row_;
619   const int column_;
620   const uint8_t* const data_;
621   size_t size_;
622   int row4x4_start_;
623   int row4x4_end_;
624   int column4x4_start_;
625   int column4x4_end_;
626   int superblock_rows_;
627   int superblock_columns_;
628   bool read_deltas_;
629   const int8_t subsampling_x_[kMaxPlanes];
630   const int8_t subsampling_y_[kMaxPlanes];
631 
632   // The dimensions (in order) are: segment_id, level_index (based on plane and
633   // direction), reference_frame and mode_id.
634   uint8_t deblock_filter_levels_[kMaxSegments][kFrameLfCount]
635                                 [kNumReferenceFrameTypes][2];
636 
637   // current_quantizer_index_ is in the range [0, 255].
638   uint8_t current_quantizer_index_;
639   // These two arrays (|coefficient_levels_| and |dc_categories_|) are used to
640   // store the entropy context. Their dimensions are as follows: First -
641   // left/top; Second - plane; Third - row4x4 (if first dimension is
642   // left)/column4x4 (if first dimension is top).
643   //
644   // This is equivalent to the LeftLevelContext and AboveLevelContext arrays in
645   // the spec. In the spec, it stores values from 0 through 63 (inclusive). The
646   // stored values are used to compute the left and top contexts in
647   // GetTransformAllZeroContext. In that function, we only care about the
648   // following values: 0, 1, 2, 3 and >= 4. So instead of clamping to 63, we
649   // clamp to 4 (i.e.) all the values greater than 4 are stored as 4.
650   std::array<Array2D<uint8_t>, 2> coefficient_levels_;
651   // This is equivalent to the LeftDcContext and AboveDcContext arrays in the
652   // spec. In the spec, it can store 3 possible values: 0, 1 and 2 (where 1
653   // means the value is < 0, 2 means the value is > 0 and 0 means the value is
654   // equal to 0).
655   //
656   // The stored values are used in two places:
657   //  * GetTransformAllZeroContext: Here, we only care about whether the
658   //  value is 0 or not (whether it is 1 or 2 is irrelevant).
659   //  * GetDcSignContext: Here, we do the following computation: if the
660   //  stored value is 1, we decrement a counter. If the stored value is 2
661   //  we increment a counter.
662   //
663   // Based on this usage, we can simply replace 1 with -1 and 2 with 1 and
664   // use that value to compute the counter.
665   //
666   // The usage on GetTransformAllZeroContext is unaffected since there we
667   // only care about whether it is 0 or not.
668   std::array<Array2D<int8_t>, 2> dc_categories_;
669   const ObuSequenceHeader& sequence_header_;
670   const ObuFrameHeader& frame_header_;
671   const std::array<bool, kNumReferenceFrameTypes>& reference_frame_sign_bias_;
672   const std::array<RefCountedBufferPtr, kNumReferenceFrameTypes>&
673       reference_frames_;
674   TemporalMotionField& motion_field_;
675   const std::array<uint8_t, kNumReferenceFrameTypes>& reference_order_hint_;
676   const WedgeMaskArray& wedge_masks_;
677   const QuantizerMatrix& quantizer_matrix_;
678   EntropyDecoder reader_;
679   SymbolDecoderContext symbol_decoder_context_;
680   SymbolDecoderContext* const saved_symbol_decoder_context_;
681   const SegmentationMap* prev_segment_ids_;
682   const dsp::Dsp& dsp_;
683   PostFilter& post_filter_;
684   BlockParametersHolder& block_parameters_holder_;
685   Quantizer quantizer_;
686   // When there is no multi-threading within the Tile, |residual_buffer_| is
687   // used. When there is multi-threading within the Tile,
688   // |residual_buffer_threaded_| is used. In the following comment,
689   // |residual_buffer| refers to either |residual_buffer_| or
690   // |residual_buffer_threaded_| depending on whether multi-threading is enabled
691   // within the Tile or not.
692   // The |residual_buffer| is used to help with the dequantization and the
693   // inverse transform processes. It is declared as a uint8_t, but is always
694   // accessed either as an int16_t or int32_t depending on |bitdepth|. Here is
695   // what it stores at various stages of the decoding process (in the order
696   // which they happen):
697   //   1) In ReadTransformCoefficients(), this buffer is used to store the
698   //   dequantized values.
699   //   2) In Reconstruct(), this buffer is used as the input to the row
700   //   transform process.
701   // The size of this buffer would be:
702   //    For |residual_buffer_|: (4096 + 32 * |kResidualPaddingVertical|) *
703   //        |residual_size_|. Where 4096 = 64x64 which is the maximum transform
704   //        size, and 32 * |kResidualPaddingVertical| is the padding to avoid
705   //        bottom boundary checks when parsing quantized coefficients. This
706   //        memory is allocated and owned by the Tile class.
707   //    For |residual_buffer_threaded_|: See the comment below. This memory is
708   //        not allocated or owned by the Tile class.
709   AlignedUniquePtr<uint8_t> residual_buffer_;
710   // This is a 2d array of pointers of size |superblock_rows_| by
711   // |superblock_columns_| where each pointer points to a ResidualBuffer for a
712   // single super block. The array is populated when the parsing process begins
713   // by calling |residual_buffer_pool_->Get()| and the memory is released back
714   // to the pool by calling |residual_buffer_pool_->Release()| when the decoding
715   // process is complete.
716   Array2D<std::unique_ptr<ResidualBuffer>> residual_buffer_threaded_;
717   // sizeof(int16_t or int32_t) depending on |bitdepth|.
718   const size_t residual_size_;
719   // Number of superblocks on the top-right that will have to be decoded before
720   // the current superblock can be decoded. This will be 1 if allow_intrabc is
721   // false. If allow_intrabc is true, then this value will be
722   // use_128x128_superblock ? 3 : 5. This is the allowed range of reference for
723   // the top rows for intrabc.
724   const int intra_block_copy_lag_;
725 
726   // In the Tile class, we use the "current_frame" in two ways:
727   //   1) To write the decoded output into (using the |buffer_| view).
728   //   2) To read the pixels for intra block copy (using the |current_frame_|
729   //      reference).
730   //
731   // When intra block copy is off, |buffer_| and |current_frame_| may or may not
732   // point to the same plane pointers. But it is okay since |current_frame_| is
733   // never used in this case.
734   //
735   // When intra block copy is on, |buffer_| and |current_frame_| always point to
736   // the same plane pointers (since post filtering is disabled). So the usage in
737   // both case 1 and case 2 remain valid.
738   Array2DView<uint8_t> buffer_[kMaxPlanes];
739   RefCountedBuffer& current_frame_;
740 
741   Array2D<int8_t>& cdef_index_;
742   Array2D<uint8_t>& cdef_skip_;
743   Array2D<TransformSize>& inter_transform_sizes_;
744   std::array<RestorationUnitInfo, kMaxPlanes> reference_unit_info_;
745   // If |thread_pool_| is nullptr, the calling thread will do the parsing and
746   // the decoding in one pass. If |thread_pool_| is not nullptr, then the main
747   // thread will do the parsing while the thread pool workers will do the
748   // decoding.
749   ThreadPool* const thread_pool_;
750   ThreadingParameters threading_;
751   ResidualBufferPool* const residual_buffer_pool_;
752   TileScratchBufferPool* const tile_scratch_buffer_pool_;
753   BlockingCounterWithStatus* const pending_tiles_;
754   bool split_parse_and_decode_;
755   // This is used only when |split_parse_and_decode_| is false.
756   std::unique_ptr<PredictionParameters> prediction_parameters_ = nullptr;
757   // Stores the |transform_type| for the super block being decoded at a 4x4
758   // granularity. The spec uses absolute indices for this array but it is
759   // sufficient to use indices relative to the super block being decoded.
760   TransformType transform_types_[32][32];
761   // delta_lf_[i] is in the range [-63, 63].
762   int8_t delta_lf_[kFrameLfCount];
763   // True if all the values in |delta_lf_| are zero. False otherwise.
764   bool delta_lf_all_zero_;
765   const bool frame_parallel_;
766   const bool use_intra_prediction_buffer_;
767   // Buffer used to store the unfiltered pixels that are necessary for decoding
768   // the next superblock row (for the intra prediction process). Used only if
769   // |use_intra_prediction_buffer_| is true. The |frame_scratch_buffer| contains
770   // one row buffer for each tile row. This tile will have to use the buffer
771   // corresponding to this tile's row.
772   IntraPredictionBuffer* const intra_prediction_buffer_;
773   // Stores the progress of the reference frames. This will be used to avoid
774   // unnecessary calls into RefCountedBuffer::WaitUntil().
775   std::array<int, kNumReferenceFrameTypes> reference_frame_progress_cache_;
776   // Stores the CDF contexts necessary for the "left" block.
777   BlockCdfContext left_context_;
778   // Stores the CDF contexts necessary for the "top" block. The size of this
779   // buffer is the number of superblock columns in this tile. For each block,
780   // the access index will be the corresponding SuperBlockColumnIndex()'th
781   // entry.
782   DynamicBuffer<BlockCdfContext> top_context_;
783 };
784 
785 struct Tile::Block {
BlockBlock786   Block(Tile* tile_ptr, BlockSize size, int row4x4, int column4x4,
787         TileScratchBuffer* const scratch_buffer, ResidualPtr* residual)
788       : tile(*tile_ptr),
789         size(size),
790         row4x4(row4x4),
791         column4x4(column4x4),
792         width(kBlockWidthPixels[size]),
793         height(kBlockHeightPixels[size]),
794         width4x4(width >> 2),
795         height4x4(height >> 2),
796         scratch_buffer(scratch_buffer),
797         residual(residual),
798         top_context(tile.top_context_.get() +
799                     tile.SuperBlockColumnIndex(column4x4)),
800         top_context_index(tile.CdfContextIndex(column4x4)),
801         left_context_index(tile.CdfContextIndex(row4x4)) {
802     assert(size != kBlockInvalid);
803     residual_size[kPlaneY] = kPlaneResidualSize[size][0][0];
804     residual_size[kPlaneU] = residual_size[kPlaneV] =
805         kPlaneResidualSize[size][tile.subsampling_x_[kPlaneU]]
806                           [tile.subsampling_y_[kPlaneU]];
807     assert(residual_size[kPlaneY] != kBlockInvalid);
808     if (tile.PlaneCount() > 1) {
809       assert(residual_size[kPlaneU] != kBlockInvalid);
810     }
811     if ((row4x4 & 1) == 0 &&
812         (tile.sequence_header_.color_config.subsampling_y & height4x4) == 1) {
813       has_chroma = false;
814     } else if ((column4x4 & 1) == 0 &&
815                (tile.sequence_header_.color_config.subsampling_x & width4x4) ==
816                    1) {
817       has_chroma = false;
818     } else {
819       has_chroma = !tile.sequence_header_.color_config.is_monochrome;
820     }
821     top_available[kPlaneY] = tile.IsTopInside(row4x4);
822     left_available[kPlaneY] = tile.IsLeftInside(column4x4);
823     if (has_chroma) {
824       // top_available[kPlaneU] and top_available[kPlaneV] are valid only if
825       // has_chroma is true.
826       // The next 3 lines are equivalent to:
827       // top_available[kPlaneU] = top_available[kPlaneV] =
828       //     top_available[kPlaneY] &&
829       //     ((tile.sequence_header_.color_config.subsampling_y & height4x4) ==
830       //     0 || tile.IsTopInside(row4x4 - 1));
831       top_available[kPlaneU] = top_available[kPlaneV] = tile.IsTopInside(
832           row4x4 -
833           (tile.sequence_header_.color_config.subsampling_y & height4x4));
834       // left_available[kPlaneU] and left_available[kPlaneV] are valid only if
835       // has_chroma is true.
836       // The next 3 lines are equivalent to:
837       // left_available[kPlaneU] = left_available[kPlaneV] =
838       //     left_available[kPlaneY] &&
839       //     ((tile.sequence_header_.color_config.subsampling_x & width4x4) == 0
840       //      || tile.IsLeftInside(column4x4 - 1));
841       left_available[kPlaneU] = left_available[kPlaneV] = tile.IsLeftInside(
842           column4x4 -
843           (tile.sequence_header_.color_config.subsampling_x & width4x4));
844     }
845     const ptrdiff_t stride = tile.BlockParametersStride();
846     BlockParameters** const bps =
847         tile.BlockParametersAddress(row4x4, column4x4);
848     bp = *bps;
849     // bp_top is valid only if top_available[kPlaneY] is true.
850     if (top_available[kPlaneY]) {
851       bp_top = *(bps - stride);
852     }
853     // bp_left is valid only if left_available[kPlaneY] is true.
854     if (left_available[kPlaneY]) {
855       bp_left = *(bps - 1);
856     }
857   }
858 
HasChromaBlock859   bool HasChroma() const { return has_chroma; }
860 
861   // These return values of these group of functions are valid only if the
862   // corresponding top_available or left_available is true.
TopReferenceBlock863   ReferenceFrameType TopReference(int index) const {
864     return bp_top->reference_frame[index];
865   }
866 
LeftReferenceBlock867   ReferenceFrameType LeftReference(int index) const {
868     return bp_left->reference_frame[index];
869   }
870 
IsTopIntraBlock871   bool IsTopIntra() const { return TopReference(0) <= kReferenceFrameIntra; }
IsLeftIntraBlock872   bool IsLeftIntra() const { return LeftReference(0) <= kReferenceFrameIntra; }
873 
IsTopSingleBlock874   bool IsTopSingle() const { return TopReference(1) <= kReferenceFrameIntra; }
IsLeftSingleBlock875   bool IsLeftSingle() const { return LeftReference(1) <= kReferenceFrameIntra; }
876 
CountReferencesBlock877   int CountReferences(ReferenceFrameType type) const {
878     return static_cast<int>(top_available[kPlaneY] &&
879                             bp_top->reference_frame[0] == type) +
880            static_cast<int>(top_available[kPlaneY] &&
881                             bp_top->reference_frame[1] == type) +
882            static_cast<int>(left_available[kPlaneY] &&
883                             bp_left->reference_frame[0] == type) +
884            static_cast<int>(left_available[kPlaneY] &&
885                             bp_left->reference_frame[1] == type);
886   }
887 
888   // 7.10.3.
889   // Checks if there are any inter blocks to the left or above. If so, it
890   // returns true indicating that the block has neighbors that are suitable for
891   // use by overlapped motion compensation.
HasOverlappableCandidatesBlock892   bool HasOverlappableCandidates() const {
893     const ptrdiff_t stride = tile.BlockParametersStride();
894     BlockParameters** const bps = tile.BlockParametersAddress(0, 0);
895     if (top_available[kPlaneY]) {
896       BlockParameters** bps_top = bps + (row4x4 - 1) * stride + (column4x4 | 1);
897       const int columns = std::min(tile.frame_header_.columns4x4 - column4x4,
898                                    static_cast<int>(width4x4));
899       BlockParameters** const bps_top_end = bps_top + columns;
900       do {
901         if ((*bps_top)->reference_frame[0] > kReferenceFrameIntra) {
902           return true;
903         }
904         bps_top += 2;
905       } while (bps_top < bps_top_end);
906     }
907     if (left_available[kPlaneY]) {
908       BlockParameters** bps_left = bps + (row4x4 | 1) * stride + column4x4 - 1;
909       const int rows = std::min(tile.frame_header_.rows4x4 - row4x4,
910                                 static_cast<int>(height4x4));
911       BlockParameters** const bps_left_end = bps_left + rows * stride;
912       do {
913         if ((*bps_left)->reference_frame[0] > kReferenceFrameIntra) {
914           return true;
915         }
916         bps_left += 2 * stride;
917       } while (bps_left < bps_left_end);
918     }
919     return false;
920   }
921 
922   Tile& tile;
923   bool has_chroma;
924   const BlockSize size;
925   bool top_available[kMaxPlanes];
926   bool left_available[kMaxPlanes];
927   BlockSize residual_size[kMaxPlanes];
928   const int row4x4;
929   const int column4x4;
930   const int width;
931   const int height;
932   const int width4x4;
933   const int height4x4;
934   const BlockParameters* bp_top;
935   const BlockParameters* bp_left;
936   BlockParameters* bp;
937   TileScratchBuffer* const scratch_buffer;
938   ResidualPtr* const residual;
939   BlockCdfContext* const top_context;
940   const int top_context_index;
941   const int left_context_index;
942 };
943 
944 extern template bool
945 Tile::ProcessSuperBlockRow<kProcessingModeDecodeOnly, false>(
946     int row4x4, TileScratchBuffer* scratch_buffer);
947 extern template bool
948 Tile::ProcessSuperBlockRow<kProcessingModeParseAndDecode, true>(
949     int row4x4, TileScratchBuffer* scratch_buffer);
950 
951 }  // namespace libgav1
952 
953 #endif  // LIBGAV1_SRC_TILE_H_
954