1 // Copyright 2020 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 #include "src/post_filter.h"
15 #include "src/utils/blocking_counter.h"
16 #include "src/utils/compiler_attributes.h"
17 #include "src/utils/constants.h"
18 
19 namespace libgav1 {
20 namespace {
21 
22 constexpr int kStep64x64 = 16;  // =64/4.
23 constexpr int kCdefSkip = 8;
24 
25 constexpr uint8_t kCdefUvDirection[2][2][8] = {
26     {{0, 1, 2, 3, 4, 5, 6, 7}, {1, 2, 2, 2, 3, 4, 6, 0}},
27     {{7, 0, 2, 4, 5, 6, 6, 6}, {0, 1, 2, 3, 4, 5, 6, 7}}};
28 
29 constexpr int kCdefBorderRows[2][4] = {{0, 1, 62, 63}, {0, 1, 30, 31}};
30 
31 template <typename Pixel>
CopyRowForCdef(const Pixel * src,int block_width,int unit_width,bool is_frame_left,bool is_frame_right,uint16_t * const dst,const Pixel * left_border=nullptr)32 void CopyRowForCdef(const Pixel* src, int block_width, int unit_width,
33                     bool is_frame_left, bool is_frame_right,
34                     uint16_t* const dst, const Pixel* left_border = nullptr) {
35   if (sizeof(src[0]) == sizeof(dst[0])) {
36     if (is_frame_left) {
37       Memset(dst - kCdefBorder, kCdefLargeValue, kCdefBorder);
38     } else if (left_border == nullptr) {
39       memcpy(dst - kCdefBorder, src - kCdefBorder,
40              kCdefBorder * sizeof(dst[0]));
41     } else {
42       memcpy(dst - kCdefBorder, left_border, kCdefBorder * sizeof(dst[0]));
43     }
44     memcpy(dst, src, block_width * sizeof(dst[0]));
45     if (is_frame_right) {
46       Memset(dst + block_width, kCdefLargeValue,
47              unit_width + kCdefBorder - block_width);
48     } else {
49       memcpy(dst + block_width, src + block_width,
50              (unit_width + kCdefBorder - block_width) * sizeof(dst[0]));
51     }
52     return;
53   }
54   if (is_frame_left) {
55     for (int x = -kCdefBorder; x < 0; ++x) {
56       dst[x] = static_cast<uint16_t>(kCdefLargeValue);
57     }
58   } else if (left_border == nullptr) {
59     for (int x = -kCdefBorder; x < 0; ++x) {
60       dst[x] = src[x];
61     }
62   } else {
63     for (int x = -kCdefBorder; x < 0; ++x) {
64       dst[x] = left_border[x + kCdefBorder];
65     }
66   }
67   for (int x = 0; x < block_width; ++x) {
68     dst[x] = src[x];
69   }
70   for (int x = block_width; x < unit_width + kCdefBorder; ++x) {
71     dst[x] = is_frame_right ? static_cast<uint16_t>(kCdefLargeValue) : src[x];
72   }
73 }
74 
75 // For |height| rows, copy |width| pixels of size |pixel_size| from |src| to
76 // |dst|.
CopyPixels(const uint8_t * src,int src_stride,uint8_t * dst,int dst_stride,int width,int height,size_t pixel_size)77 void CopyPixels(const uint8_t* src, int src_stride, uint8_t* dst,
78                 int dst_stride, int width, int height, size_t pixel_size) {
79   int y = height;
80   do {
81     memcpy(dst, src, width * pixel_size);
82     src += src_stride;
83     dst += dst_stride;
84   } while (--y != 0);
85 }
86 
87 }  // namespace
88 
SetupCdefBorder(int row4x4)89 void PostFilter::SetupCdefBorder(int row4x4) {
90   assert(row4x4 >= 0);
91   assert(DoCdef());
92   int plane = kPlaneY;
93   do {
94     const ptrdiff_t src_stride = frame_buffer_.stride(plane);
95     const ptrdiff_t dst_stride = cdef_border_.stride(plane);
96     const int row_offset = DivideBy4(row4x4);
97     const int num_pixels = SubsampledValue(
98         MultiplyBy4(frame_header_.columns4x4), subsampling_x_[plane]);
99     const int plane_height = SubsampledValue(MultiplyBy4(frame_header_.rows4x4),
100                                              subsampling_y_[plane]);
101     for (int i = 0; i < 4; ++i) {
102       const int row = kCdefBorderRows[subsampling_y_[plane]][i];
103       const int absolute_row =
104           (MultiplyBy4(row4x4) >> subsampling_y_[plane]) + row;
105       if (absolute_row >= plane_height) break;
106       const uint8_t* src =
107           GetSourceBuffer(static_cast<Plane>(plane), row4x4, 0) +
108           row * src_stride;
109       uint8_t* dst = cdef_border_.data(plane) + dst_stride * (row_offset + i);
110       memcpy(dst, src, num_pixels * pixel_size_);
111     }
112   } while (++plane < planes_);
113 }
114 
115 template <typename Pixel>
PrepareCdefBlock(int block_width4x4,int block_height4x4,int row4x4,int column4x4,uint16_t * cdef_source,ptrdiff_t cdef_stride,const bool y_plane,const uint8_t border_columns[kMaxPlanes][256],bool use_border_columns)116 void PostFilter::PrepareCdefBlock(int block_width4x4, int block_height4x4,
117                                   int row4x4, int column4x4,
118                                   uint16_t* cdef_source, ptrdiff_t cdef_stride,
119                                   const bool y_plane,
120                                   const uint8_t border_columns[kMaxPlanes][256],
121                                   bool use_border_columns) {
122   assert(y_plane || planes_ == kMaxPlanes);
123   const int max_planes = y_plane ? 1 : kMaxPlanes;
124   const int8_t subsampling_x = y_plane ? 0 : subsampling_x_[kPlaneU];
125   const int8_t subsampling_y = y_plane ? 0 : subsampling_y_[kPlaneU];
126   const int start_x = MultiplyBy4(column4x4) >> subsampling_x;
127   const int start_y = MultiplyBy4(row4x4) >> subsampling_y;
128   const int plane_width = SubsampledValue(width_, subsampling_x);
129   const int plane_height = SubsampledValue(height_, subsampling_y);
130   const int block_width = MultiplyBy4(block_width4x4) >> subsampling_x;
131   const int block_height = MultiplyBy4(block_height4x4) >> subsampling_y;
132   // unit_width, unit_height are the same as block_width, block_height unless
133   // it reaches the frame boundary, where block_width < 64 or
134   // block_height < 64. unit_width, unit_height guarantee we build blocks on
135   // a multiple of 8.
136   const int unit_width = Align(block_width, 8 >> subsampling_x);
137   const int unit_height = Align(block_height, 8 >> subsampling_y);
138   const bool is_frame_left = column4x4 == 0;
139   const bool is_frame_right = start_x + block_width >= plane_width;
140   const bool is_frame_top = row4x4 == 0;
141   const bool is_frame_bottom = start_y + block_height >= plane_height;
142   const int y_offset = is_frame_top ? 0 : kCdefBorder;
143   const int cdef_border_row_offset = DivideBy4(row4x4) - (is_frame_top ? 0 : 2);
144 
145   for (int plane = y_plane ? kPlaneY : kPlaneU; plane < max_planes; ++plane) {
146     uint16_t* cdef_src = cdef_source + static_cast<int>(plane == kPlaneV) *
147                                            kCdefUnitSizeWithBorders *
148                                            kCdefUnitSizeWithBorders;
149     const int src_stride = frame_buffer_.stride(plane) / sizeof(Pixel);
150     const Pixel* src_buffer =
151         reinterpret_cast<const Pixel*>(source_buffer_[plane]) +
152         (start_y - y_offset) * src_stride + start_x;
153     const int cdef_border_stride = cdef_border_.stride(plane) / sizeof(Pixel);
154     const Pixel* cdef_border =
155         (thread_pool_ == nullptr)
156             ? nullptr
157             : reinterpret_cast<const Pixel*>(cdef_border_.data(plane)) +
158                   cdef_border_row_offset * cdef_border_stride + start_x;
159 
160     // All the copying code will use negative indices for populating the left
161     // border. So the starting point is set to kCdefBorder.
162     cdef_src += kCdefBorder;
163 
164     // Copy the top 2 rows as follows;
165     // If is_frame_top is true, both the rows are set to kCdefLargeValue.
166     // Otherwise:
167     //   If multi-threaded filtering is off, the rows are copied from
168     //   |src_buffer|.
169     //   Otherwise, the rows are copied from |cdef_border|.
170     if (is_frame_top) {
171       for (int y = 0; y < kCdefBorder; ++y) {
172         Memset(cdef_src - kCdefBorder, kCdefLargeValue,
173                unit_width + 2 * kCdefBorder);
174         cdef_src += cdef_stride;
175       }
176     } else {
177       const Pixel* top_border =
178           (thread_pool_ == nullptr) ? src_buffer : cdef_border;
179       const int top_border_stride =
180           (thread_pool_ == nullptr) ? src_stride : cdef_border_stride;
181       for (int y = 0; y < kCdefBorder; ++y) {
182         CopyRowForCdef(top_border, block_width, unit_width, is_frame_left,
183                        is_frame_right, cdef_src);
184         top_border += top_border_stride;
185         cdef_src += cdef_stride;
186         // We need to increment |src_buffer| and |cdef_border| in this loop to
187         // set them up for the subsequent loops below.
188         src_buffer += src_stride;
189         cdef_border += cdef_border_stride;
190       }
191     }
192 
193     // Copy the body as follows;
194     // If multi-threaded filtering is off or if is_frame_bottom is true, all the
195     // rows are copied from |src_buffer|.
196     // Otherwise, the first |block_height|-kCdefBorder rows are copied from
197     // |src_buffer| and the last kCdefBorder rows are coped from |cdef_border|.
198     int y = block_height;
199     const int y_threshold =
200         (thread_pool_ == nullptr || is_frame_bottom) ? 0 : kCdefBorder;
201     const Pixel* left_border =
202         (thread_pool_ == nullptr || !use_border_columns)
203             ? nullptr
204             : reinterpret_cast<const Pixel*>(border_columns[plane]);
205     do {
206       CopyRowForCdef(src_buffer, block_width, unit_width, is_frame_left,
207                      is_frame_right, cdef_src, left_border);
208       cdef_src += cdef_stride;
209       src_buffer += src_stride;
210       if (left_border != nullptr) left_border += kCdefBorder;
211     } while (--y != y_threshold);
212 
213     if (y > 0) {
214       assert(y == kCdefBorder);
215       // |cdef_border| now points to the top 2 rows of the current block. For
216       // the next loop, we need it to point to the bottom 2 rows of the
217       // current block. So increment it by 2 rows.
218       cdef_border += MultiplyBy2(cdef_border_stride);
219       for (int i = 0; i < kCdefBorder; ++i) {
220         CopyRowForCdef(cdef_border, block_width, unit_width, is_frame_left,
221                        is_frame_right, cdef_src);
222         cdef_src += cdef_stride;
223         cdef_border += cdef_border_stride;
224       }
225     }
226 
227     // Copy the bottom 2 rows as follows;
228     // If is_frame_bottom is true, both the rows are set to kCdefLargeValue.
229     // Otherwise:
230     //   If multi-threaded filtering is off, the rows are copied from
231     //   |src_buffer|.
232     //   Otherwise, the rows are copied from |cdef_border|.
233     y = 0;
234     if (is_frame_bottom) {
235       do {
236         Memset(cdef_src - kCdefBorder, kCdefLargeValue,
237                unit_width + 2 * kCdefBorder);
238         cdef_src += cdef_stride;
239       } while (++y < kCdefBorder + unit_height - block_height);
240     } else {
241       const Pixel* bottom_border =
242           (thread_pool_ == nullptr) ? src_buffer : cdef_border;
243       const int bottom_border_stride =
244           (thread_pool_ == nullptr) ? src_stride : cdef_border_stride;
245       do {
246         CopyRowForCdef(bottom_border, block_width, unit_width, is_frame_left,
247                        is_frame_right, cdef_src);
248         bottom_border += bottom_border_stride;
249         cdef_src += cdef_stride;
250       } while (++y < kCdefBorder + unit_height - block_height);
251     }
252   }
253 }
254 
255 template <typename Pixel>
ApplyCdefForOneUnit(uint16_t * cdef_block,const int index,const int block_width4x4,const int block_height4x4,const int row4x4_start,const int column4x4_start,uint8_t border_columns[2][kMaxPlanes][256],bool use_border_columns[2][2])256 void PostFilter::ApplyCdefForOneUnit(uint16_t* cdef_block, const int index,
257                                      const int block_width4x4,
258                                      const int block_height4x4,
259                                      const int row4x4_start,
260                                      const int column4x4_start,
261                                      uint8_t border_columns[2][kMaxPlanes][256],
262                                      bool use_border_columns[2][2]) {
263   // Cdef operates in 8x8 blocks (4x4 for chroma with subsampling).
264   static constexpr int kStep = 8;
265   static constexpr int kStep4x4 = 2;
266 
267   int cdef_buffer_row_base_stride[kMaxPlanes];
268   uint8_t* cdef_buffer_row_base[kMaxPlanes];
269   int src_buffer_row_base_stride[kMaxPlanes];
270   const uint8_t* src_buffer_row_base[kMaxPlanes];
271   const uint16_t* cdef_src_row_base[kMaxPlanes];
272   int cdef_src_row_base_stride[kMaxPlanes];
273   int column_step[kMaxPlanes];
274   assert(planes_ >= 1);
275   int plane = kPlaneY;
276   do {
277     cdef_buffer_row_base[plane] =
278         GetCdefBuffer(static_cast<Plane>(plane), row4x4_start, column4x4_start);
279     cdef_buffer_row_base_stride[plane] =
280         frame_buffer_.stride(plane) * (kStep >> subsampling_y_[plane]);
281     src_buffer_row_base[plane] = GetSourceBuffer(static_cast<Plane>(plane),
282                                                  row4x4_start, column4x4_start);
283     src_buffer_row_base_stride[plane] =
284         frame_buffer_.stride(plane) * (kStep >> subsampling_y_[plane]);
285     cdef_src_row_base[plane] =
286         cdef_block +
287         static_cast<int>(plane == kPlaneV) * kCdefUnitSizeWithBorders *
288             kCdefUnitSizeWithBorders +
289         kCdefBorder * kCdefUnitSizeWithBorders + kCdefBorder;
290     cdef_src_row_base_stride[plane] =
291         kCdefUnitSizeWithBorders * (kStep >> subsampling_y_[plane]);
292     column_step[plane] = (kStep >> subsampling_x_[plane]) * sizeof(Pixel);
293   } while (++plane < planes_);
294 
295   // |border_columns| contains two buffers. In each call to this function, we
296   // will use one of them as the "destination" for the current call. And the
297   // other one as the "source" for the current call (which would have been the
298   // "destination" of the previous call). We will use the src_index to populate
299   // the borders which were backed up in the previous call. We will use the
300   // dst_index to populate the borders to be used in the next call.
301   const int border_columns_src_index = DivideBy16(column4x4_start) & 1;
302   const int border_columns_dst_index = border_columns_src_index ^ 1;
303 
304   if (index == -1) {
305     if (thread_pool_ == nullptr) {
306       int plane = kPlaneY;
307       do {
308         CopyPixels(src_buffer_row_base[plane], frame_buffer_.stride(plane),
309                    cdef_buffer_row_base[plane], frame_buffer_.stride(plane),
310                    MultiplyBy4(block_width4x4) >> subsampling_x_[plane],
311                    MultiplyBy4(block_height4x4) >> subsampling_y_[plane],
312                    sizeof(Pixel));
313       } while (++plane < planes_);
314     }
315     use_border_columns[border_columns_dst_index][0] = false;
316     use_border_columns[border_columns_dst_index][1] = false;
317     return;
318   }
319 
320   const bool is_frame_right =
321       MultiplyBy4(column4x4_start) + MultiplyBy4(block_width4x4) >= width_;
322   if (!is_frame_right && thread_pool_ != nullptr) {
323     // Backup the last 2 columns for use in the next iteration.
324     use_border_columns[border_columns_dst_index][0] = true;
325     const uint8_t* src_line =
326         GetSourceBuffer(kPlaneY, row4x4_start,
327                         column4x4_start + block_width4x4) -
328         kCdefBorder * sizeof(Pixel);
329     CopyPixels(src_line, frame_buffer_.stride(kPlaneY),
330                border_columns[border_columns_dst_index][kPlaneY],
331                kCdefBorder * sizeof(Pixel), kCdefBorder,
332                MultiplyBy4(block_height4x4), sizeof(Pixel));
333   }
334 
335   PrepareCdefBlock<Pixel>(
336       block_width4x4, block_height4x4, row4x4_start, column4x4_start,
337       cdef_block, kCdefUnitSizeWithBorders, true,
338       (border_columns != nullptr) ? border_columns[border_columns_src_index]
339                                   : nullptr,
340       use_border_columns[border_columns_src_index][0]);
341 
342   // Stored direction used during the u/v pass.  If bit 3 is set, then block is
343   // a skip.
344   int direction_y[8 * 8];
345   int y_index = 0;
346 
347   const uint8_t y_primary_strength =
348       frame_header_.cdef.y_primary_strength[index];
349   const uint8_t y_secondary_strength =
350       frame_header_.cdef.y_secondary_strength[index];
351   // y_strength_index is 0 for both primary and secondary strengths being
352   // non-zero, 1 for primary only, 2 for secondary only. This will be updated
353   // with y_primary_strength after variance is applied.
354   int y_strength_index = static_cast<int>(y_secondary_strength == 0);
355 
356   const bool compute_direction_and_variance =
357       (y_primary_strength | frame_header_.cdef.uv_primary_strength[index]) != 0;
358   BlockParameters* const* bp_row0_base =
359       block_parameters_.Address(row4x4_start, column4x4_start);
360   BlockParameters* const* bp_row1_base =
361       bp_row0_base + block_parameters_.columns4x4();
362   const int bp_stride = MultiplyBy2(block_parameters_.columns4x4());
363   int row4x4 = row4x4_start;
364   do {
365     uint8_t* cdef_buffer_base = cdef_buffer_row_base[kPlaneY];
366     const uint8_t* src_buffer_base = src_buffer_row_base[kPlaneY];
367     const uint16_t* cdef_src_base = cdef_src_row_base[kPlaneY];
368     BlockParameters* const* bp0 = bp_row0_base;
369     BlockParameters* const* bp1 = bp_row1_base;
370     int column4x4 = column4x4_start;
371     do {
372       const int block_width = kStep;
373       const int block_height = kStep;
374       const int cdef_stride = frame_buffer_.stride(kPlaneY);
375       uint8_t* const cdef_buffer = cdef_buffer_base;
376       const uint16_t* const cdef_src = cdef_src_base;
377       const int src_stride = frame_buffer_.stride(kPlaneY);
378       const uint8_t* const src_buffer = src_buffer_base;
379 
380       const bool skip = (*bp0)->skip && (*(bp0 + 1))->skip && (*bp1)->skip &&
381                         (*(bp1 + 1))->skip;
382 
383       if (skip) {  // No cdef filtering.
384         direction_y[y_index] = kCdefSkip;
385         if (thread_pool_ == nullptr) {
386           CopyPixels(src_buffer, src_stride, cdef_buffer, cdef_stride,
387                      block_width, block_height, sizeof(Pixel));
388         }
389       } else {
390         // Zero out residual skip flag.
391         direction_y[y_index] = 0;
392 
393         int variance = 0;
394         if (compute_direction_and_variance) {
395           if (thread_pool_ == nullptr ||
396               row4x4 + kStep4x4 < row4x4_start + block_height4x4) {
397             dsp_.cdef_direction(src_buffer, src_stride, &direction_y[y_index],
398                                 &variance);
399           } else if (sizeof(Pixel) == 2) {
400             dsp_.cdef_direction(cdef_src, kCdefUnitSizeWithBorders * 2,
401                                 &direction_y[y_index], &variance);
402           } else {
403             // If we are in the last row4x4 for this unit, then the last two
404             // input rows have to come from |cdef_border_|. Since we already
405             // have |cdef_src| populated correctly, use that as the input
406             // for the direction process.
407             uint8_t direction_src[8][8];
408             const uint16_t* cdef_src_line = cdef_src;
409             for (auto& direction_src_line : direction_src) {
410               for (int i = 0; i < 8; ++i) {
411                 direction_src_line[i] = cdef_src_line[i];
412               }
413               cdef_src_line += kCdefUnitSizeWithBorders;
414             }
415             dsp_.cdef_direction(direction_src, 8, &direction_y[y_index],
416                                 &variance);
417           }
418         }
419         const int direction =
420             (y_primary_strength == 0) ? 0 : direction_y[y_index];
421         const int variance_strength =
422             ((variance >> 6) != 0) ? std::min(FloorLog2(variance >> 6), 12) : 0;
423         const uint8_t primary_strength =
424             (variance != 0)
425                 ? (y_primary_strength * (4 + variance_strength) + 8) >> 4
426                 : 0;
427         if ((primary_strength | y_secondary_strength) == 0) {
428           if (thread_pool_ == nullptr) {
429             CopyPixels(src_buffer, src_stride, cdef_buffer, cdef_stride,
430                        block_width, block_height, sizeof(Pixel));
431           }
432         } else {
433           const int strength_index =
434               y_strength_index | (static_cast<int>(primary_strength == 0) << 1);
435           dsp_.cdef_filters[1][strength_index](
436               cdef_src, kCdefUnitSizeWithBorders, block_height,
437               primary_strength, y_secondary_strength,
438               frame_header_.cdef.damping, direction, cdef_buffer, cdef_stride);
439         }
440       }
441       cdef_buffer_base += column_step[kPlaneY];
442       src_buffer_base += column_step[kPlaneY];
443       cdef_src_base += column_step[kPlaneY] / sizeof(Pixel);
444 
445       bp0 += kStep4x4;
446       bp1 += kStep4x4;
447       column4x4 += kStep4x4;
448       y_index++;
449     } while (column4x4 < column4x4_start + block_width4x4);
450 
451     cdef_buffer_row_base[kPlaneY] += cdef_buffer_row_base_stride[kPlaneY];
452     src_buffer_row_base[kPlaneY] += src_buffer_row_base_stride[kPlaneY];
453     cdef_src_row_base[kPlaneY] += cdef_src_row_base_stride[kPlaneY];
454     bp_row0_base += bp_stride;
455     bp_row1_base += bp_stride;
456     row4x4 += kStep4x4;
457   } while (row4x4 < row4x4_start + block_height4x4);
458 
459   if (planes_ == kMaxPlanesMonochrome) {
460     return;
461   }
462 
463   const uint8_t uv_primary_strength =
464       frame_header_.cdef.uv_primary_strength[index];
465   const uint8_t uv_secondary_strength =
466       frame_header_.cdef.uv_secondary_strength[index];
467 
468   if ((uv_primary_strength | uv_secondary_strength) == 0) {
469     if (thread_pool_ == nullptr) {
470       for (int plane = kPlaneU; plane <= kPlaneV; ++plane) {
471         CopyPixels(src_buffer_row_base[plane], frame_buffer_.stride(plane),
472                    cdef_buffer_row_base[plane], frame_buffer_.stride(plane),
473                    MultiplyBy4(block_width4x4) >> subsampling_x_[plane],
474                    MultiplyBy4(block_height4x4) >> subsampling_y_[plane],
475                    sizeof(Pixel));
476       }
477     }
478     use_border_columns[border_columns_dst_index][1] = false;
479     return;
480   }
481 
482   if (!is_frame_right && thread_pool_ != nullptr) {
483     use_border_columns[border_columns_dst_index][1] = true;
484     for (int plane = kPlaneU; plane <= kPlaneV; ++plane) {
485       // Backup the last 2 columns for use in the next iteration.
486       const uint8_t* src_line =
487           GetSourceBuffer(static_cast<Plane>(plane), row4x4_start,
488                           column4x4_start + block_width4x4) -
489           kCdefBorder * sizeof(Pixel);
490       CopyPixels(src_line, frame_buffer_.stride(plane),
491                  border_columns[border_columns_dst_index][plane],
492                  kCdefBorder * sizeof(Pixel), kCdefBorder,
493                  MultiplyBy4(block_height4x4) >> subsampling_y_[plane],
494                  sizeof(Pixel));
495     }
496   }
497 
498   PrepareCdefBlock<Pixel>(
499       block_width4x4, block_height4x4, row4x4_start, column4x4_start,
500       cdef_block, kCdefUnitSizeWithBorders, false,
501       (border_columns != nullptr) ? border_columns[border_columns_src_index]
502                                   : nullptr,
503       use_border_columns[border_columns_src_index][1]);
504 
505   // uv_strength_index is 0 for both primary and secondary strengths being
506   // non-zero, 1 for primary only, 2 for secondary only.
507   const int uv_strength_index =
508       (static_cast<int>(uv_primary_strength == 0) << 1) |
509       static_cast<int>(uv_secondary_strength == 0);
510   for (int plane = kPlaneU; plane <= kPlaneV; ++plane) {
511     const int8_t subsampling_x = subsampling_x_[plane];
512     const int8_t subsampling_y = subsampling_y_[plane];
513     const int block_width = kStep >> subsampling_x;
514     const int block_height = kStep >> subsampling_y;
515     int row4x4 = row4x4_start;
516 
517     y_index = 0;
518     do {
519       uint8_t* cdef_buffer_base = cdef_buffer_row_base[plane];
520       const uint8_t* src_buffer_base = src_buffer_row_base[plane];
521       const uint16_t* cdef_src_base = cdef_src_row_base[plane];
522       int column4x4 = column4x4_start;
523       do {
524         const int cdef_stride = frame_buffer_.stride(plane);
525         uint8_t* const cdef_buffer = cdef_buffer_base;
526         const int src_stride = frame_buffer_.stride(plane);
527         const uint8_t* const src_buffer = src_buffer_base;
528         const uint16_t* const cdef_src = cdef_src_base;
529         const bool skip = (direction_y[y_index] & kCdefSkip) != 0;
530         int dual_cdef = 0;
531 
532         if (skip) {  // No cdef filtering.
533           if (thread_pool_ == nullptr) {
534             CopyPixels(src_buffer, src_stride, cdef_buffer, cdef_stride,
535                        block_width, block_height, sizeof(Pixel));
536           }
537         } else {
538           // Make sure block pair is not out of bounds.
539           if (column4x4 + (kStep4x4 * 2) <= column4x4_start + block_width4x4) {
540             // Enable dual processing if subsampling_x is 1.
541             dual_cdef = subsampling_x;
542           }
543 
544           int direction = (uv_primary_strength == 0)
545                               ? 0
546                               : kCdefUvDirection[subsampling_x][subsampling_y]
547                                                 [direction_y[y_index]];
548 
549           if (dual_cdef != 0) {
550             if (uv_primary_strength &&
551                 direction_y[y_index] != direction_y[y_index + 1]) {
552               // Disable dual processing if the second block of the pair does
553               // not have the same direction.
554               dual_cdef = 0;
555             }
556 
557             // Disable dual processing if the second block of the pair is a
558             // skip.
559             if (direction_y[y_index + 1] == kCdefSkip) {
560               dual_cdef = 0;
561             }
562           }
563 
564           // Block width is 8 if either dual_cdef is true or subsampling_x == 0.
565           const int width_index = dual_cdef | (subsampling_x ^ 1);
566           dsp_.cdef_filters[width_index][uv_strength_index](
567               cdef_src, kCdefUnitSizeWithBorders, block_height,
568               uv_primary_strength, uv_secondary_strength,
569               frame_header_.cdef.damping - 1, direction, cdef_buffer,
570               cdef_stride);
571         }
572         // When dual_cdef is set, the above cdef_filter() will process 2 blocks,
573         // so adjust the pointers and indexes for 2 blocks.
574         cdef_buffer_base += column_step[plane] << dual_cdef;
575         src_buffer_base += column_step[plane] << dual_cdef;
576         cdef_src_base += (column_step[plane] / sizeof(Pixel)) << dual_cdef;
577         column4x4 += kStep4x4 << dual_cdef;
578         y_index += 1 << dual_cdef;
579       } while (column4x4 < column4x4_start + block_width4x4);
580 
581       cdef_buffer_row_base[plane] += cdef_buffer_row_base_stride[plane];
582       src_buffer_row_base[plane] += src_buffer_row_base_stride[plane];
583       cdef_src_row_base[plane] += cdef_src_row_base_stride[plane];
584       row4x4 += kStep4x4;
585     } while (row4x4 < row4x4_start + block_height4x4);
586   }
587 }
588 
ApplyCdefForOneSuperBlockRowHelper(uint16_t * cdef_block,uint8_t border_columns[2][kMaxPlanes][256],int row4x4,int block_height4x4)589 void PostFilter::ApplyCdefForOneSuperBlockRowHelper(
590     uint16_t* cdef_block, uint8_t border_columns[2][kMaxPlanes][256],
591     int row4x4, int block_height4x4) {
592   bool use_border_columns[2][2] = {};
593   for (int column4x4 = 0; column4x4 < frame_header_.columns4x4;
594        column4x4 += kStep64x64) {
595     const int index = cdef_index_[DivideBy16(row4x4)][DivideBy16(column4x4)];
596     const int block_width4x4 =
597         std::min(kStep64x64, frame_header_.columns4x4 - column4x4);
598 
599 #if LIBGAV1_MAX_BITDEPTH >= 10
600     if (bitdepth_ >= 10) {
601       ApplyCdefForOneUnit<uint16_t>(cdef_block, index, block_width4x4,
602                                     block_height4x4, row4x4, column4x4,
603                                     border_columns, use_border_columns);
604       continue;
605     }
606 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
607     ApplyCdefForOneUnit<uint8_t>(cdef_block, index, block_width4x4,
608                                  block_height4x4, row4x4, column4x4,
609                                  border_columns, use_border_columns);
610   }
611 }
612 
ApplyCdefForOneSuperBlockRow(int row4x4_start,int sb4x4,bool is_last_row)613 void PostFilter::ApplyCdefForOneSuperBlockRow(int row4x4_start, int sb4x4,
614                                               bool is_last_row) {
615   assert(row4x4_start >= 0);
616   assert(DoCdef());
617   for (int y = 0; y < sb4x4; y += kStep64x64) {
618     const int row4x4 = row4x4_start + y;
619     if (row4x4 >= frame_header_.rows4x4) return;
620 
621     // Apply cdef for the last 8 rows of the previous superblock row.
622     // One exception: If the superblock size is 128x128 and is_last_row is true,
623     // then we simply apply cdef for the entire superblock row without any lag.
624     // In that case, apply cdef for the previous superblock row only during the
625     // first iteration (y == 0).
626     if (row4x4 > 0 && (!is_last_row || y == 0)) {
627       assert(row4x4 >= 16);
628       ApplyCdefForOneSuperBlockRowHelper(cdef_block_, nullptr, row4x4 - 2, 2);
629     }
630 
631     // Apply cdef for the current superblock row. If this is the last superblock
632     // row we apply cdef for all the rows, otherwise we leave out the last 8
633     // rows.
634     const int block_height4x4 =
635         std::min(kStep64x64, frame_header_.rows4x4 - row4x4);
636     const int height4x4 = block_height4x4 - (is_last_row ? 0 : 2);
637     if (height4x4 > 0) {
638       ApplyCdefForOneSuperBlockRowHelper(cdef_block_, nullptr, row4x4,
639                                          height4x4);
640     }
641   }
642 }
643 
ApplyCdefWorker(std::atomic<int> * row4x4_atomic)644 void PostFilter::ApplyCdefWorker(std::atomic<int>* row4x4_atomic) {
645   int row4x4;
646   uint16_t cdef_block[kCdefUnitSizeWithBorders * kCdefUnitSizeWithBorders * 2];
647   // Each border_column buffer has to store 64 rows and 2 columns for each
648   // plane. For 10bit, that is 64*2*2 = 256 bytes.
649   alignas(kMaxAlignment) uint8_t border_columns[2][kMaxPlanes][256];
650   while ((row4x4 = row4x4_atomic->fetch_add(
651               kStep64x64, std::memory_order_relaxed)) < frame_header_.rows4x4) {
652     const int block_height4x4 =
653         std::min(kStep64x64, frame_header_.rows4x4 - row4x4);
654     ApplyCdefForOneSuperBlockRowHelper(cdef_block, border_columns, row4x4,
655                                        block_height4x4);
656   }
657 }
658 
659 }  // namespace libgav1
660