• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 namespace libgav1 {
18 
19 template <typename Pixel>
ApplyLoopRestorationForOneRow(const Pixel * src_buffer,const ptrdiff_t stride,const Plane plane,const int plane_height,const int plane_width,const int unit_y,const int unit_row,const int current_process_unit_height,const int plane_unit_size,Pixel * dst_buffer)20 void PostFilter::ApplyLoopRestorationForOneRow(
21     const Pixel* src_buffer, const ptrdiff_t stride, const Plane plane,
22     const int plane_height, const int plane_width, const int unit_y,
23     const int unit_row, const int current_process_unit_height,
24     const int plane_unit_size, Pixel* dst_buffer) {
25   const int num_horizontal_units =
26       restoration_info_->num_horizontal_units(static_cast<Plane>(plane));
27   const RestorationUnitInfo* const restoration_info =
28       restoration_info_->loop_restoration_info(static_cast<Plane>(plane),
29                                                unit_row * num_horizontal_units);
30   const bool in_place = DoCdef() || thread_pool_ != nullptr;
31   const Pixel* border = nullptr;
32   ptrdiff_t border_stride = 0;
33   src_buffer += unit_y * stride;
34   if (in_place) {
35     const int border_unit_y = std::max(
36         RightShiftWithCeiling(unit_y, 4 - subsampling_y_[plane]) - 4, 0);
37     border_stride = loop_restoration_border_.stride(plane) / sizeof(Pixel);
38     border =
39         reinterpret_cast<const Pixel*>(loop_restoration_border_.data(plane)) +
40         border_unit_y * border_stride;
41   }
42   int unit_column = 0;
43   int column = 0;
44   do {
45     const int current_process_unit_width =
46         std::min(plane_unit_size, plane_width - column);
47     const Pixel* src = src_buffer + column;
48     unit_column = std::min(unit_column, num_horizontal_units - 1);
49     if (restoration_info[unit_column].type == kLoopRestorationTypeNone) {
50       Pixel* dst = dst_buffer + column;
51       if (in_place) {
52         int k = current_process_unit_height;
53         do {
54           memmove(dst, src, current_process_unit_width * sizeof(Pixel));
55           src += stride;
56           dst += stride;
57         } while (--k != 0);
58       } else {
59         CopyPlane(src, stride, current_process_unit_width,
60                   current_process_unit_height, dst, stride);
61       }
62     } else {
63       const Pixel* top_border = src - kRestorationVerticalBorder * stride;
64       ptrdiff_t top_border_stride = stride;
65       const Pixel* bottom_border = src + current_process_unit_height * stride;
66       ptrdiff_t bottom_border_stride = stride;
67       const bool frame_bottom_border =
68           (unit_y + current_process_unit_height >= plane_height);
69       if (in_place && (unit_y != 0 || !frame_bottom_border)) {
70         const Pixel* loop_restoration_border = border + column;
71         if (unit_y != 0) {
72           top_border = loop_restoration_border;
73           top_border_stride = border_stride;
74           loop_restoration_border += 4 * border_stride;
75         }
76         if (!frame_bottom_border) {
77           bottom_border = loop_restoration_border +
78                           kRestorationVerticalBorder * border_stride;
79           bottom_border_stride = border_stride;
80         }
81       }
82       RestorationBuffer restoration_buffer;
83       const LoopRestorationType type = restoration_info[unit_column].type;
84       assert(type == kLoopRestorationTypeSgrProj ||
85              type == kLoopRestorationTypeWiener);
86       const dsp::LoopRestorationFunc restoration_func =
87           dsp_.loop_restorations[type - 2];
88       restoration_func(restoration_info[unit_column], src, stride, top_border,
89                        top_border_stride, bottom_border, bottom_border_stride,
90                        current_process_unit_width, current_process_unit_height,
91                        &restoration_buffer, dst_buffer + column);
92     }
93     ++unit_column;
94     column += plane_unit_size;
95   } while (column < plane_width);
96 }
97 
98 template <typename Pixel>
ApplyLoopRestorationForOneSuperBlockRow(const int row4x4_start,const int sb4x4)99 void PostFilter::ApplyLoopRestorationForOneSuperBlockRow(const int row4x4_start,
100                                                          const int sb4x4) {
101   assert(row4x4_start >= 0);
102   assert(DoRestoration());
103   int plane = kPlaneY;
104   const int upscaled_width = frame_header_.upscaled_width;
105   const int height = frame_header_.height;
106   do {
107     if (loop_restoration_.type[plane] == kLoopRestorationTypeNone) {
108       continue;
109     }
110     const ptrdiff_t stride = frame_buffer_.stride(plane) / sizeof(Pixel);
111     const int unit_height_offset =
112         kRestorationUnitOffset >> subsampling_y_[plane];
113     const int plane_height = SubsampledValue(height, subsampling_y_[plane]);
114     const int plane_width =
115         SubsampledValue(upscaled_width, subsampling_x_[plane]);
116     const int plane_unit_size = 1 << loop_restoration_.unit_size_log2[plane];
117     const int plane_process_unit_height =
118         kRestorationUnitHeight >> subsampling_y_[plane];
119     int y = (row4x4_start == 0)
120                 ? 0
121                 : (MultiplyBy4(row4x4_start) >> subsampling_y_[plane]) -
122                       unit_height_offset;
123     int expected_height = plane_process_unit_height -
124                           ((row4x4_start == 0) ? unit_height_offset : 0);
125     int current_process_unit_height;
126     for (int sb_y = 0; sb_y < sb4x4;
127          sb_y += 16, y += current_process_unit_height) {
128       if (y >= plane_height) break;
129       const int unit_row = std::min(
130           (y + unit_height_offset) >> loop_restoration_.unit_size_log2[plane],
131           restoration_info_->num_vertical_units(static_cast<Plane>(plane)) - 1);
132       current_process_unit_height = std::min(expected_height, plane_height - y);
133       expected_height = plane_process_unit_height;
134       ApplyLoopRestorationForOneRow<Pixel>(
135           reinterpret_cast<Pixel*>(superres_buffer_[plane]), stride,
136           static_cast<Plane>(plane), plane_height, plane_width, y, unit_row,
137           current_process_unit_height, plane_unit_size,
138           reinterpret_cast<Pixel*>(loop_restoration_buffer_[plane]) +
139               y * stride);
140     }
141   } while (++plane < planes_);
142 }
143 
ApplyLoopRestoration(const int row4x4_start,const int sb4x4)144 void PostFilter::ApplyLoopRestoration(const int row4x4_start, const int sb4x4) {
145 #if LIBGAV1_MAX_BITDEPTH >= 10
146   if (bitdepth_ >= 10) {
147     ApplyLoopRestorationForOneSuperBlockRow<uint16_t>(row4x4_start, sb4x4);
148     return;
149   }
150 #endif
151   ApplyLoopRestorationForOneSuperBlockRow<uint8_t>(row4x4_start, sb4x4);
152 }
153 
ApplyLoopRestorationWorker(std::atomic<int> * row4x4_atomic)154 void PostFilter::ApplyLoopRestorationWorker(std::atomic<int>* row4x4_atomic) {
155   int row4x4;
156   // Loop Restoration operates with a lag of 8 rows (4 for chroma with
157   // subsampling) and hence we need to make sure to cover the last 8 rows of the
158   // last superblock row. So we run this loop for an extra iteration to
159   // accomplish that.
160   const int row4x4_end = frame_header_.rows4x4 + kNum4x4InLoopRestorationUnit;
161   while ((row4x4 = row4x4_atomic->fetch_add(kNum4x4InLoopRestorationUnit,
162                                             std::memory_order_relaxed)) <
163          row4x4_end) {
164     CopyBordersForOneSuperBlockRow(row4x4, kNum4x4InLoopRestorationUnit,
165                                    /*for_loop_restoration=*/true);
166 #if LIBGAV1_MAX_BITDEPTH >= 10
167     if (bitdepth_ >= 10) {
168       ApplyLoopRestorationForOneSuperBlockRow<uint16_t>(
169           row4x4, kNum4x4InLoopRestorationUnit);
170       continue;
171     }
172 #endif
173     ApplyLoopRestorationForOneSuperBlockRow<uint8_t>(
174         row4x4, kNum4x4InLoopRestorationUnit);
175   }
176 }
177 
178 }  // namespace libgav1
179