• 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_POST_FILTER_H_
18 #define LIBGAV1_SRC_POST_FILTER_H_
19 
20 #include <algorithm>
21 #include <array>
22 #include <atomic>
23 #include <cstddef>
24 #include <cstdint>
25 #include <cstring>
26 #include <type_traits>
27 
28 #include "src/dsp/common.h"
29 #include "src/dsp/dsp.h"
30 #include "src/frame_scratch_buffer.h"
31 #include "src/loop_restoration_info.h"
32 #include "src/obu_parser.h"
33 #include "src/utils/array_2d.h"
34 #include "src/utils/block_parameters_holder.h"
35 #include "src/utils/common.h"
36 #include "src/utils/constants.h"
37 #include "src/utils/memory.h"
38 #include "src/utils/threadpool.h"
39 #include "src/yuv_buffer.h"
40 
41 namespace libgav1 {
42 
43 // This class applies in-loop filtering for each frame after it is
44 // reconstructed. The in-loop filtering contains all post processing filtering
45 // for the reconstructed frame, including deblock filter, CDEF, superres,
46 // and loop restoration.
47 // Historically, for example in libaom, loop filter refers to deblock filter.
48 // To avoid name conflicts, we call this class PostFilter (post processing).
49 // In-loop post filtering order is:
50 // deblock --> CDEF --> super resolution--> loop restoration.
51 // When CDEF and super resolution is not used, we can combine deblock
52 // and restoration together to only filter frame buffer once.
53 class PostFilter {
54  public:
55   // This class does not take ownership of the masks/restoration_info, but it
56   // may change their values.
57   //
58   // The overall flow of data in this class (for both single and multi-threaded
59   // cases) is as follows:
60   //   -> Input: |frame_buffer_|.
61   //   -> Initialize |source_buffer_|, |cdef_buffer_|, |superres_buffer_| and
62   //      |loop_restoration_buffer_|.
63   //   -> Deblocking:
64   //      * Input: |source_buffer_|
65   //      * Output: |source_buffer_|
66   //   -> CDEF:
67   //      * Input: |source_buffer_|
68   //      * Output: |cdef_buffer_|
69   //   -> SuperRes:
70   //      * Input: |cdef_buffer_|
71   //      * Output: |superres_buffer_|
72   //   -> Loop Restoration:
73   //      * Input: |superres_buffer_|
74   //      * Output: |loop_restoration_buffer_|.
75   //   -> Now |frame_buffer_| contains the filtered frame.
76   PostFilter(const ObuFrameHeader& frame_header,
77              const ObuSequenceHeader& sequence_header,
78              FrameScratchBuffer* frame_scratch_buffer, YuvBuffer* frame_buffer,
79              const dsp::Dsp* dsp, int do_post_filter_mask);
80 
81   // non copyable/movable.
82   PostFilter(const PostFilter&) = delete;
83   PostFilter& operator=(const PostFilter&) = delete;
84   PostFilter(PostFilter&&) = delete;
85   PostFilter& operator=(PostFilter&&) = delete;
86 
87   // The overall function that applies all post processing filtering with
88   // multiple threads.
89   // * The filtering order is:
90   //   deblock --> CDEF --> super resolution--> loop restoration.
91   // * The output of each filter is the input for the following filter. A
92   //   special case is that loop restoration needs a few rows of the deblocked
93   //   frame and the entire cdef filtered frame:
94   //   deblock --> CDEF --> super resolution --> loop restoration.
95   //              |                                 ^
96   //              |                                 |
97   //              -----------> super resolution -----
98   // * Any of these filters could be present or absent.
99   // * |frame_buffer_| points to the decoded frame buffer. When
100   //   ApplyFilteringThreaded() is called, |frame_buffer_| is modified by each
101   //   of the filters as described below.
102   // Filter behavior (multi-threaded):
103   // * Deblock: In-place filtering. The output is written to |source_buffer_|.
104   //            If cdef and loop restoration are both on, then 4 rows (as
105   //            specified by |kLoopRestorationBorderRows|) in every 64x64 block
106   //            is copied into |loop_restoration_border_|.
107   // * Cdef: In-place filtering. Uses the |source_buffer_| and |cdef_border_| as
108   //         the input and the output is written into |cdef_buffer_| (which is
109   //         the same as |source_buffer_|).
110   // * SuperRes: Near in-place filtering. Uses the |cdef_buffer_| and
111   //             |superres_line_buffer_| as the input and the output is written
112   //             into |superres_buffer_| (which is just |cdef_buffer_| with a
113   //             shift to the top).
114   // * Restoration: Near in-place filtering.
115   //                Uses the |superres_buffer_| and |loop_restoration_border_|
116   //                as the input and the output is written into
117   //                |loop_restoration_buffer_| (which is just |superres_buffer_|
118   //                with a shift to the left).
119   void ApplyFilteringThreaded();
120 
121   // Does the overall post processing filter for one superblock row starting at
122   // |row4x4| with height 4*|sb4x4|. If |do_deblock| is false, deblocking filter
123   // will not be applied.
124   //
125   // Filter behavior (single-threaded):
126   // * Deblock: In-place filtering. The output is written to |source_buffer_|.
127   //            If cdef and loop restoration are both on, then 4 rows (as
128   //            specified by |kLoopRestorationBorderRows|) in every 64x64 block
129   //            is copied into |loop_restoration_border_|.
130   // * Cdef: In-place filtering. The output is written into |cdef_buffer_|
131   //         (which is just |source_buffer_| with a shift to the top-left).
132   // * SuperRes: Near in-place filtering. Uses the |cdef_buffer_| as the input
133   //             and the output is written into |superres_buffer_| (which is
134   //             just |cdef_buffer_| with a shift to the top).
135   // * Restoration: Near in-place filtering.
136   //                Uses the |superres_buffer_| and |loop_restoration_border_|
137   //                as the input and the output is written into
138   //                |loop_restoration_buffer_| (which is just |superres_buffer_|
139   //                with a shift to the left or top-left).
140   // Returns the index of the last row whose post processing is complete and can
141   // be used for referencing.
142   int ApplyFilteringForOneSuperBlockRow(int row4x4, int sb4x4, bool is_last_row,
143                                         bool do_deblock);
144 
145   // Apply deblocking filter in one direction (specified by |loop_filter_type|)
146   // for the superblock row starting at |row4x4_start| for columns starting from
147   // |column4x4_start| in increments of 16 (or 8 for chroma with subsampling)
148   // until the smallest multiple of 16 that is >= |column4x4_end| or until
149   // |frame_header_.columns4x4|, whichever is lower. This function must be
150   // called only if |DoDeblock()| returns true.
151   void ApplyDeblockFilter(LoopFilterType loop_filter_type, int row4x4_start,
152                           int column4x4_start, int column4x4_end, int sb4x4);
153 
DoCdef(const ObuFrameHeader & frame_header,int do_post_filter_mask)154   static bool DoCdef(const ObuFrameHeader& frame_header,
155                      int do_post_filter_mask) {
156     return (frame_header.cdef.bits > 0 ||
157             frame_header.cdef.y_primary_strength[0] > 0 ||
158             frame_header.cdef.y_secondary_strength[0] > 0 ||
159             frame_header.cdef.uv_primary_strength[0] > 0 ||
160             frame_header.cdef.uv_secondary_strength[0] > 0) &&
161            (do_post_filter_mask & 0x02) != 0;
162   }
DoCdef()163   bool DoCdef() const { return do_cdef_; }
164   // If filter levels for Y plane (0 for vertical, 1 for horizontal),
165   // are all zero, deblock filter will not be applied.
DoDeblock(const ObuFrameHeader & frame_header,uint8_t do_post_filter_mask)166   static bool DoDeblock(const ObuFrameHeader& frame_header,
167                         uint8_t do_post_filter_mask) {
168     return (frame_header.loop_filter.level[0] > 0 ||
169             frame_header.loop_filter.level[1] > 0) &&
170            (do_post_filter_mask & 0x01) != 0;
171   }
DoDeblock()172   bool DoDeblock() const { return do_deblock_; }
173 
GetZeroDeltaDeblockFilterLevel(int segment_id,int level_index,ReferenceFrameType type,int mode_id)174   uint8_t GetZeroDeltaDeblockFilterLevel(int segment_id, int level_index,
175                                          ReferenceFrameType type,
176                                          int mode_id) const {
177     return deblock_filter_levels_[segment_id][level_index][type][mode_id];
178   }
179   // Computes the deblock filter levels using |delta_lf| and stores them in
180   // |deblock_filter_levels|.
181   void ComputeDeblockFilterLevels(
182       const int8_t delta_lf[kFrameLfCount],
183       uint8_t deblock_filter_levels[kMaxSegments][kFrameLfCount]
184                                    [kNumReferenceFrameTypes][2]) const;
185   // Returns true if loop restoration will be performed for the given parameters
186   // and mask.
DoRestoration(const LoopRestoration & loop_restoration,uint8_t do_post_filter_mask,int num_planes)187   static bool DoRestoration(const LoopRestoration& loop_restoration,
188                             uint8_t do_post_filter_mask, int num_planes) {
189     if (num_planes == kMaxPlanesMonochrome) {
190       return loop_restoration.type[kPlaneY] != kLoopRestorationTypeNone &&
191              (do_post_filter_mask & 0x08) != 0;
192     }
193     return (loop_restoration.type[kPlaneY] != kLoopRestorationTypeNone ||
194             loop_restoration.type[kPlaneU] != kLoopRestorationTypeNone ||
195             loop_restoration.type[kPlaneV] != kLoopRestorationTypeNone) &&
196            (do_post_filter_mask & 0x08) != 0;
197   }
DoRestoration()198   bool DoRestoration() const { return do_restoration_; }
199 
200   // Returns a pointer to the unfiltered buffer. This is used by the Tile class
201   // to determine where to write the output of the tile decoding process taking
202   // in-place filtering offsets into consideration.
GetUnfilteredBuffer(int plane)203   uint8_t* GetUnfilteredBuffer(int plane) { return source_buffer_[plane]; }
frame_buffer()204   const YuvBuffer& frame_buffer() const { return frame_buffer_; }
205 
206   // Returns true if SuperRes will be performed for the given frame header and
207   // mask.
DoSuperRes(const ObuFrameHeader & frame_header,uint8_t do_post_filter_mask)208   static bool DoSuperRes(const ObuFrameHeader& frame_header,
209                          uint8_t do_post_filter_mask) {
210     return frame_header.width != frame_header.upscaled_width &&
211            (do_post_filter_mask & 0x04) != 0;
212   }
DoSuperRes()213   bool DoSuperRes() const { return do_superres_; }
restoration_info()214   LoopRestorationInfo* restoration_info() const { return restoration_info_; }
GetBufferOffset(uint8_t * base_buffer,int stride,Plane plane,int row,int column)215   uint8_t* GetBufferOffset(uint8_t* base_buffer, int stride, Plane plane,
216                            int row, int column) const {
217     return base_buffer + (row >> subsampling_y_[plane]) * stride +
218            ((column >> subsampling_x_[plane]) << pixel_size_log2_);
219   }
GetSourceBuffer(Plane plane,int row4x4,int column4x4)220   uint8_t* GetSourceBuffer(Plane plane, int row4x4, int column4x4) const {
221     return GetBufferOffset(source_buffer_[plane], frame_buffer_.stride(plane),
222                            plane, MultiplyBy4(row4x4), MultiplyBy4(column4x4));
223   }
GetCdefBuffer(Plane plane,int row4x4,int column4x4)224   uint8_t* GetCdefBuffer(Plane plane, int row4x4, int column4x4) const {
225     return GetBufferOffset(cdef_buffer_[plane], frame_buffer_.stride(plane),
226                            plane, MultiplyBy4(row4x4), MultiplyBy4(column4x4));
227   }
GetSuperResBuffer(Plane plane,int row4x4,int column4x4)228   uint8_t* GetSuperResBuffer(Plane plane, int row4x4, int column4x4) const {
229     return GetBufferOffset(superres_buffer_[plane], frame_buffer_.stride(plane),
230                            plane, MultiplyBy4(row4x4), MultiplyBy4(column4x4));
231   }
232 
233   template <typename Pixel>
234   static void ExtendFrame(Pixel* frame_start, int width, int height,
235                           ptrdiff_t stride, int left, int right, int top,
236                           int bottom);
237 
238  private:
239   // The type of the HorizontalDeblockFilter and VerticalDeblockFilter member
240   // functions.
241   using DeblockFilter = void (PostFilter::*)(int row4x4_start, int row4x4_end,
242                                              int column4x4_start,
243                                              int column4x4_end);
244   // Functions common to all post filters.
245 
246   // Extends the frame by setting the border pixel values to the one from its
247   // closest frame boundary.
248   void ExtendFrameBoundary(uint8_t* frame_start, int width, int height,
249                            ptrdiff_t stride, int left, int right, int top,
250                            int bottom) const;
251   // Extend frame boundary for referencing if the frame will be saved as a
252   // reference frame.
253   void ExtendBordersForReferenceFrame();
254   // Copies the deblocked pixels needed for loop restoration.
255   void CopyDeblockedPixels(Plane plane, int row4x4);
256   // Copies the border for one superblock row. If |for_loop_restoration| is
257   // true, then it assumes that the border extension is being performed for the
258   // input of the loop restoration process. If |for_loop_restoration| is false,
259   // then it assumes that the border extension is being performed for using the
260   // current frame as a reference frame. In this case, |progress_row_| is also
261   // updated.
262   void CopyBordersForOneSuperBlockRow(int row4x4, int sb4x4,
263                                       bool for_loop_restoration);
264   // Sets up the |loop_restoration_border_| for loop restoration.
265   // This is called when there is no CDEF filter. We copy rows from
266   // |superres_buffer_| and do the line extension.
267   void SetupLoopRestorationBorder(int row4x4_start);
268   // This is called when there is CDEF filter. We copy rows from
269   // |source_buffer_|, apply superres and do the line extension.
270   void SetupLoopRestorationBorder(int row4x4_start, int sb4x4);
271   // Returns true if we can perform border extension in loop (i.e.) without
272   // waiting until the entire frame is decoded. If intra_block_copy is true, we
273   // do in-loop border extension only if the upscaled_width is the same as 4 *
274   // columns4x4. Otherwise, we cannot do in loop border extension since those
275   // pixels may be used by intra block copy.
DoBorderExtensionInLoop()276   bool DoBorderExtensionInLoop() const {
277     return !frame_header_.allow_intrabc ||
278            frame_header_.upscaled_width ==
279                MultiplyBy4(frame_header_.columns4x4);
280   }
281   template <typename Pixel>
CopyPlane(const Pixel * src,ptrdiff_t src_stride,int width,int height,Pixel * dst,ptrdiff_t dst_stride)282   void CopyPlane(const Pixel* src, ptrdiff_t src_stride, int width, int height,
283                  Pixel* dst, ptrdiff_t dst_stride) {
284     assert(height > 0);
285     do {
286       memcpy(dst, src, width * sizeof(Pixel));
287       src += src_stride;
288       dst += dst_stride;
289     } while (--height != 0);
290   }
291 
292   // Worker function used for multi-threaded implementation of Deblocking, CDEF
293   // and Loop Restoration.
294   using WorkerFunction = void (PostFilter::*)(std::atomic<int>* row4x4_atomic);
295   // Schedules |worker| jobs to the |thread_pool_|, runs them in the calling
296   // thread and returns once all the jobs are completed.
297   void RunJobs(WorkerFunction worker);
298 
299   // Functions for the Deblocking filter.
300 
301   bool GetHorizontalDeblockFilterEdgeInfo(int row4x4, int column4x4,
302                                           uint8_t* level, int* step,
303                                           int* filter_length) const;
304   void GetHorizontalDeblockFilterEdgeInfoUV(int row4x4, int column4x4,
305                                             uint8_t* level_u, uint8_t* level_v,
306                                             int* step,
307                                             int* filter_length) const;
308   bool GetVerticalDeblockFilterEdgeInfo(int row4x4, int column4x4,
309                                         BlockParameters* const* bp_ptr,
310                                         uint8_t* level, int* step,
311                                         int* filter_length) const;
312   void GetVerticalDeblockFilterEdgeInfoUV(int column4x4,
313                                           BlockParameters* const* bp_ptr,
314                                           uint8_t* level_u, uint8_t* level_v,
315                                           int* step, int* filter_length) const;
316   void HorizontalDeblockFilter(int row4x4_start, int row4x4_end,
317                                int column4x4_start, int column4x4_end);
318   void VerticalDeblockFilter(int row4x4_start, int row4x4_end,
319                              int column4x4_start, int column4x4_end);
320   // HorizontalDeblockFilter and VerticalDeblockFilter must have the correct
321   // signature.
322   static_assert(std::is_same<decltype(&PostFilter::HorizontalDeblockFilter),
323                              DeblockFilter>::value,
324                 "");
325   static_assert(std::is_same<decltype(&PostFilter::VerticalDeblockFilter),
326                              DeblockFilter>::value,
327                 "");
328   // Worker function used for multi-threaded deblocking.
329   template <LoopFilterType loop_filter_type>
330   void DeblockFilterWorker(std::atomic<int>* row4x4_atomic);
331   static_assert(
332       std::is_same<
333           decltype(&PostFilter::DeblockFilterWorker<kLoopFilterTypeVertical>),
334           WorkerFunction>::value,
335       "");
336   static_assert(
337       std::is_same<
338           decltype(&PostFilter::DeblockFilterWorker<kLoopFilterTypeHorizontal>),
339           WorkerFunction>::value,
340       "");
341 
342   // Functions for the cdef filter.
343 
344   // Copies the deblocked pixels necessary for use by the multi-threaded cdef
345   // implementation into |cdef_border_|.
346   void SetupCdefBorder(int row4x4);
347   // This function prepares the input source block for cdef filtering. The input
348   // source block contains a 12x12 block, with the inner 8x8 as the desired
349   // filter region. It pads the block if the 12x12 block includes out of frame
350   // pixels with a large value. This achieves the required behavior defined in
351   // section 5.11.52 of the spec.
352   template <typename Pixel>
353   void PrepareCdefBlock(int block_width4x4, int block_height4x4, int row4x4,
354                         int column4x4, uint16_t* cdef_source,
355                         ptrdiff_t cdef_stride, bool y_plane,
356                         const uint8_t border_columns[kMaxPlanes][256],
357                         bool use_border_columns);
358   // Applies cdef for one 64x64 block.
359   template <typename Pixel>
360   void ApplyCdefForOneUnit(uint16_t* cdef_block, int index, int block_width4x4,
361                            int block_height4x4, int row4x4_start,
362                            int column4x4_start,
363                            uint8_t border_columns[2][kMaxPlanes][256],
364                            bool use_border_columns[2][2]);
365   // Helper function used by ApplyCdefForOneSuperBlockRow to avoid some code
366   // duplication.
367   void ApplyCdefForOneSuperBlockRowHelper(
368       uint16_t* cdef_block, uint8_t border_columns[2][kMaxPlanes][256],
369       int row4x4, int block_height4x4);
370   // Applies CDEF filtering for the superblock row starting at |row4x4| with a
371   // height of 4*|sb4x4|.
372   void ApplyCdefForOneSuperBlockRow(int row4x4, int sb4x4, bool is_last_row);
373   // Worker function used for multi-threaded CDEF.
374   void ApplyCdefWorker(std::atomic<int>* row4x4_atomic);
375   static_assert(std::is_same<decltype(&PostFilter::ApplyCdefWorker),
376                              WorkerFunction>::value,
377                 "");
378 
379   // Functions for the SuperRes filter.
380 
381   // Applies super resolution for the |src| for |rows[plane]| rows of each
382   // plane. If |line_buffer_row| is larger than or equal to 0, one more row will
383   // be processed, the line buffer indicated by |line_buffer_row| will be used
384   // as the source. If |dst_is_loop_restoration_border| is true, then it means
385   // that the |dst| pointers come from |loop_restoration_border_| and the
386   // strides will be populated from that buffer.
387   void ApplySuperRes(
388       const std::array<uint8_t*, kMaxPlanes>& src,
389       const std::array<int, kMaxPlanes>& rows, int line_buffer_row,
390       const std::array<uint8_t*, kMaxPlanes>& dst,
391       bool dst_is_loop_restoration_border = false);  // Section 7.16.
392   // Applies SuperRes for the superblock row starting at |row4x4| with a height
393   // of 4*|sb4x4|.
394   void ApplySuperResForOneSuperBlockRow(int row4x4, int sb4x4,
395                                         bool is_last_row);
396   void ApplySuperResThreaded();
397 
398   // Functions for the Loop Restoration filter.
399 
400   // Notes about Loop Restoration:
401   // (1). Loop restoration processing unit size is default to 64x64.
402   // Only when the remaining filtering area is smaller than 64x64, the
403   // processing unit size is the actual area size.
404   // For U/V plane, it is (64 >> subsampling_x) x (64 >> subsampling_y).
405   // (2). Loop restoration unit size can be 64x64, 128x128, 256x256 for Y
406   // plane. The unit size for chroma can be the same or half, depending on
407   // subsampling. If either subsampling_x or subsampling_y is one, unit size
408   // is halved on both x and y sides.
409   // All loop restoration units have the same size for one plane.
410   // One loop restoration unit could contain multiple processing units.
411   // But they share the same sets of loop restoration parameters.
412   // (3). Loop restoration has a row offset, kRestorationUnitOffset = 8. The
413   // size of first row of loop restoration units and processing units is
414   // shrunk by the offset.
415   // (4). Loop restoration units wrap the bottom and the right of the frame,
416   // if the remaining area is small. The criteria is whether the number of
417   // remaining rows/columns is smaller than half of loop restoration unit
418   // size.
419   // For example, if the frame size is 140x140, loop restoration unit size is
420   // 128x128. The size of the first loop restoration unit is 128x(128-8) =
421   // 128 columns x 120 rows.
422   // Since 140 - 120 < 128/2. The remaining 20 rows will be folded to the loop
423   // restoration unit. Similarly, the remaining 12 columns will also be folded
424   // to current loop restoration unit. So, even frame size is 140x140,
425   // there's only one loop restoration unit. Suppose processing unit is 64x64,
426   // then sizes of the first row of processing units are 64x56, 64x56, 12x56,
427   // respectively. The second row is 64x64, 64x64, 12x64.
428   // The third row is 64x20, 64x20, 12x20.
429 
430   // |stride| is shared by |src_buffer| and |dst_buffer|.
431   template <typename Pixel>
432   void ApplyLoopRestorationForOneRow(const Pixel* src_buffer, ptrdiff_t stride,
433                                      Plane plane, int plane_height,
434                                      int plane_width, int y, int unit_row,
435                                      int current_process_unit_height,
436                                      int plane_unit_size, Pixel* dst_buffer);
437   // Applies loop restoration for the superblock row starting at |row4x4_start|
438   // with a height of 4*|sb4x4|.
439   template <typename Pixel>
440   void ApplyLoopRestorationForOneSuperBlockRow(int row4x4_start, int sb4x4);
441   // Helper function that calls the right variant of
442   // ApplyLoopRestorationForOneSuperBlockRow based on the bitdepth.
443   void ApplyLoopRestoration(int row4x4_start, int sb4x4);
444   // Worker function used for multithreaded Loop Restoration.
445   void ApplyLoopRestorationWorker(std::atomic<int>* row4x4_atomic);
446   static_assert(std::is_same<decltype(&PostFilter::ApplyLoopRestorationWorker),
447                              WorkerFunction>::value,
448                 "");
449 
450   // The lookup table for picking the deblock filter, according to deblock
451   // filter type.
452   const DeblockFilter deblock_filter_func_[2] = {
453       &PostFilter::VerticalDeblockFilter, &PostFilter::HorizontalDeblockFilter};
454   const ObuFrameHeader& frame_header_;
455   const LoopRestoration& loop_restoration_;
456   const dsp::Dsp& dsp_;
457   const int8_t bitdepth_;
458   const int8_t subsampling_x_[kMaxPlanes];
459   const int8_t subsampling_y_[kMaxPlanes];
460   const int8_t planes_;
461   const int pixel_size_log2_;
462   const uint8_t* const inner_thresh_;
463   const uint8_t* const outer_thresh_;
464   const bool needs_chroma_deblock_;
465   const bool do_cdef_;
466   const bool do_deblock_;
467   const bool do_restoration_;
468   const bool do_superres_;
469   // This stores the deblocking filter levels assuming that the delta is zero.
470   // This will be used by all superblocks whose delta is zero (without having to
471   // recompute them). The dimensions (in order) are: segment_id, level_index
472   // (based on plane and direction), reference_frame and mode_id.
473   uint8_t deblock_filter_levels_[kMaxSegments][kFrameLfCount]
474                                 [kNumReferenceFrameTypes][2];
475   // Stores the SuperRes info for the frame.
476   struct {
477     int upscaled_width;
478     int initial_subpixel_x;
479     int step;
480   } super_res_info_[kMaxPlanes];
481   const Array2D<int8_t>& cdef_index_;
482   const Array2D<uint8_t>& cdef_skip_;
483   const Array2D<TransformSize>& inter_transform_sizes_;
484   LoopRestorationInfo* const restoration_info_;
485   uint8_t* const superres_coefficients_[kNumPlaneTypes];
486   // Line buffer used by multi-threaded ApplySuperRes().
487   // In the multi-threaded case, this buffer will store the last downscaled row
488   // input of each thread to avoid overwrites by the first upscaled row output
489   // of the thread below it.
490   YuvBuffer& superres_line_buffer_;
491   const BlockParametersHolder& block_parameters_;
492   // Frame buffer to hold cdef filtered frame.
493   YuvBuffer cdef_filtered_buffer_;
494   // Input frame buffer.
495   YuvBuffer& frame_buffer_;
496   // A view into |frame_buffer_| that points to the input and output of the
497   // deblocking process.
498   uint8_t* source_buffer_[kMaxPlanes];
499   // A view into |frame_buffer_| that points to the output of the CDEF filtered
500   // planes (to facilitate in-place CDEF filtering).
501   uint8_t* cdef_buffer_[kMaxPlanes];
502   // A view into |frame_buffer_| that points to the planes after the SuperRes
503   // filter is applied (to facilitate in-place SuperRes).
504   uint8_t* superres_buffer_[kMaxPlanes];
505   // A view into |frame_buffer_| that points to the output of the Loop Restored
506   // planes (to facilitate in-place Loop Restoration).
507   uint8_t* loop_restoration_buffer_[kMaxPlanes];
508   YuvBuffer& cdef_border_;
509   // Buffer used to store the border pixels that are necessary for loop
510   // restoration. This buffer will store 4 rows for every 64x64 block (4 rows
511   // for every 32x32 for chroma with subsampling). The indices of the rows that
512   // are stored are specified in |kLoopRestorationBorderRows|. First 4 rows of
513   // this buffer are never populated and never used.
514   // This buffer is used only when both of the following conditions are true:
515   //   (1). Loop Restoration is on.
516   //   (2). Cdef is on, or multi-threading is enabled for post filter.
517   YuvBuffer& loop_restoration_border_;
518   ThreadPool* const thread_pool_;
519 
520   // Tracks the progress of the post filters.
521   int progress_row_ = -1;
522 
523   // A block buffer to hold the input that is converted to uint16_t before
524   // cdef filtering. Only used in single threaded case. Y plane is processed
525   // separately. U and V planes are processed together. So it is sufficient to
526   // have this buffer to accommodate 2 planes at a time.
527   uint16_t cdef_block_[kCdefUnitSizeWithBorders * kCdefUnitSizeWithBorders * 2];
528 
529   template <int bitdepth, typename Pixel>
530   friend class PostFilterSuperResTest;
531 
532   template <int bitdepth, typename Pixel>
533   friend class PostFilterHelperFuncTest;
534 };
535 
536 extern template void PostFilter::ExtendFrame<uint8_t>(uint8_t* frame_start,
537                                                       int width, int height,
538                                                       ptrdiff_t stride,
539                                                       int left, int right,
540                                                       int top, int bottom);
541 
542 #if LIBGAV1_MAX_BITDEPTH >= 10
543 extern template void PostFilter::ExtendFrame<uint16_t>(uint16_t* frame_start,
544                                                        int width, int height,
545                                                        ptrdiff_t stride,
546                                                        int left, int right,
547                                                        int top, int bottom);
548 #endif
549 
550 }  // namespace libgav1
551 
552 #endif  // LIBGAV1_SRC_POST_FILTER_H_
553