• 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   do {
105     if (loop_restoration_.type[plane] == kLoopRestorationTypeNone) {
106       continue;
107     }
108     const ptrdiff_t stride = frame_buffer_.stride(plane) / sizeof(Pixel);
109     const int unit_height_offset =
110         kRestorationUnitOffset >> subsampling_y_[plane];
111     const int plane_height = SubsampledValue(height_, subsampling_y_[plane]);
112     const int plane_width =
113         SubsampledValue(upscaled_width_, subsampling_x_[plane]);
114     const int plane_unit_size = 1 << loop_restoration_.unit_size_log2[plane];
115     const int plane_process_unit_height =
116         kRestorationUnitHeight >> subsampling_y_[plane];
117     int y = (row4x4_start == 0)
118                 ? 0
119                 : (MultiplyBy4(row4x4_start) >> subsampling_y_[plane]) -
120                       unit_height_offset;
121     int expected_height = plane_process_unit_height -
122                           ((row4x4_start == 0) ? unit_height_offset : 0);
123     int current_process_unit_height;
124     for (int sb_y = 0; sb_y < sb4x4;
125          sb_y += 16, y += current_process_unit_height) {
126       if (y >= plane_height) break;
127       const int unit_row = std::min(
128           (y + unit_height_offset) >> loop_restoration_.unit_size_log2[plane],
129           restoration_info_->num_vertical_units(static_cast<Plane>(plane)) - 1);
130       current_process_unit_height = std::min(expected_height, plane_height - y);
131       expected_height = plane_process_unit_height;
132       ApplyLoopRestorationForOneRow<Pixel>(
133           reinterpret_cast<Pixel*>(superres_buffer_[plane]), stride,
134           static_cast<Plane>(plane), plane_height, plane_width, y, unit_row,
135           current_process_unit_height, plane_unit_size,
136           reinterpret_cast<Pixel*>(loop_restoration_buffer_[plane]) +
137               y * stride);
138     }
139   } while (++plane < planes_);
140 }
141 
ApplyLoopRestoration(const int row4x4_start,const int sb4x4)142 void PostFilter::ApplyLoopRestoration(const int row4x4_start, const int sb4x4) {
143 #if LIBGAV1_MAX_BITDEPTH >= 10
144   if (bitdepth_ >= 10) {
145     ApplyLoopRestorationForOneSuperBlockRow<uint16_t>(row4x4_start, sb4x4);
146     return;
147   }
148 #endif
149   ApplyLoopRestorationForOneSuperBlockRow<uint8_t>(row4x4_start, sb4x4);
150 }
151 
ApplyLoopRestorationWorker(std::atomic<int> * row4x4_atomic)152 void PostFilter::ApplyLoopRestorationWorker(std::atomic<int>* row4x4_atomic) {
153   int row4x4;
154   // Loop Restoration operates with a lag of 8 rows (4 for chroma with
155   // subsampling) and hence we need to make sure to cover the last 8 rows of the
156   // last superblock row. So we run this loop for an extra iteration to
157   // accomplish that.
158   const int row4x4_end = frame_header_.rows4x4 + kNum4x4InLoopRestorationUnit;
159   while ((row4x4 = row4x4_atomic->fetch_add(kNum4x4InLoopRestorationUnit,
160                                             std::memory_order_relaxed)) <
161          row4x4_end) {
162     CopyBordersForOneSuperBlockRow(row4x4, kNum4x4InLoopRestorationUnit,
163                                    /*for_loop_restoration=*/true);
164 #if LIBGAV1_MAX_BITDEPTH >= 10
165     if (bitdepth_ >= 10) {
166       ApplyLoopRestorationForOneSuperBlockRow<uint16_t>(
167           row4x4, kNum4x4InLoopRestorationUnit);
168       continue;
169     }
170 #endif
171     ApplyLoopRestorationForOneSuperBlockRow<uint8_t>(
172         row4x4, kNum4x4InLoopRestorationUnit);
173   }
174 }
175 
176 }  // namespace libgav1
177