• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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 
15 #include "src/dsp/intrapred_filter.h"
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <cstring>
23 
24 #include "src/dsp/constants.h"
25 #include "src/dsp/dsp.h"
26 #include "src/utils/common.h"
27 #include "src/utils/constants.h"
28 #include "src/utils/memory.h"
29 
30 namespace libgav1 {
31 namespace dsp {
32 namespace {
33 
34 //------------------------------------------------------------------------------
35 // FilterIntraPredictor_C
36 
37 // The recursive filter applies a different filter to the top 4 and 2 left
38 // pixels to produce each pixel in a 4x2 sub-block. Each successive 4x2 uses the
39 // prediction output of the blocks above and to the left, unless they are
40 // adjacent to the |top_row| or |left_column|. The set of 8 filters is selected
41 // according to |pred|.
42 template <int bitdepth, typename Pixel>
FilterIntraPredictor_C(void * const dest,ptrdiff_t stride,const void * const top_row,const void * const left_column,const FilterIntraPredictor pred,const int width,const int height)43 void FilterIntraPredictor_C(void* const dest, ptrdiff_t stride,
44                             const void* const top_row,
45                             const void* const left_column,
46                             const FilterIntraPredictor pred, const int width,
47                             const int height) {
48   const int kMaxPixel = (1 << bitdepth) - 1;
49   const auto* const top = static_cast<const Pixel*>(top_row);
50   const auto* const left = static_cast<const Pixel*>(left_column);
51 
52   assert(width <= 32 && height <= 32);
53 
54   Pixel buffer[3][33];  // cache 2 rows + top & left boundaries
55   memcpy(buffer[0], &top[-1], (width + 1) * sizeof(top[0]));
56 
57   auto* dst = static_cast<Pixel*>(dest);
58   stride /= sizeof(Pixel);
59   int row0 = 0, row2 = 2;
60   int ystep = 1;
61   int y = 0;
62   do {
63     buffer[1][0] = left[y];
64     buffer[row2][0] = left[y + 1];
65     int x = 1;
66     do {
67       const Pixel p0 = buffer[row0][x - 1];  // top-left
68       const Pixel p1 = buffer[row0][x + 0];  // top 0
69       const Pixel p2 = buffer[row0][x + 1];  // top 1
70       const Pixel p3 = buffer[row0][x + 2];  // top 2
71       const Pixel p4 = buffer[row0][x + 3];  // top 3
72       const Pixel p5 = buffer[1][x - 1];     // left 0
73       const Pixel p6 = buffer[row2][x - 1];  // left 1
74       for (int i = 0; i < 8; ++i) {
75         const int xoffset = i & 0x03;
76         const int yoffset = (i >> 2) * ystep;
77         const int value = kFilterIntraTaps[pred][i][0] * p0 +
78                           kFilterIntraTaps[pred][i][1] * p1 +
79                           kFilterIntraTaps[pred][i][2] * p2 +
80                           kFilterIntraTaps[pred][i][3] * p3 +
81                           kFilterIntraTaps[pred][i][4] * p4 +
82                           kFilterIntraTaps[pred][i][5] * p5 +
83                           kFilterIntraTaps[pred][i][6] * p6;
84         // Section 7.11.2.3 specifies the right-hand side of the assignment as
85         //   Clip1( Round2Signed( pr, INTRA_FILTER_SCALE_BITS ) ).
86         // Since Clip1() clips a negative value to 0, it is safe to replace
87         // Round2Signed() with Round2().
88         buffer[1 + yoffset][x + xoffset] = static_cast<Pixel>(
89             Clip3(RightShiftWithRounding(value, 4), 0, kMaxPixel));
90       }
91       x += 4;
92     } while (x < width);
93     memcpy(dst, &buffer[1][1], width * sizeof(dst[0]));
94     dst += stride;
95     memcpy(dst, &buffer[row2][1], width * sizeof(dst[0]));
96     dst += stride;
97 
98     // The final row becomes the top for the next pass.
99     row0 ^= 2;
100     row2 ^= 2;
101     ystep = -ystep;
102     y += 2;
103   } while (y < height);
104 }
105 
Init8bpp()106 void Init8bpp() {
107   Dsp* const dsp = dsp_internal::GetWritableDspTable(8);
108   assert(dsp != nullptr);
109 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
110   dsp->filter_intra_predictor = FilterIntraPredictor_C<8, uint8_t>;
111 #else  // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
112   static_cast<void>(dsp);
113 #ifndef LIBGAV1_Dsp8bpp_FilterIntraPredictor
114   dsp->filter_intra_predictor = FilterIntraPredictor_C<8, uint8_t>;
115 #endif
116 #endif  // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
117 }
118 
119 #if LIBGAV1_MAX_BITDEPTH >= 10
Init10bpp()120 void Init10bpp() {
121   Dsp* const dsp = dsp_internal::GetWritableDspTable(10);
122   assert(dsp != nullptr);
123 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
124   dsp->filter_intra_predictor = FilterIntraPredictor_C<10, uint16_t>;
125 #else  // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
126   static_cast<void>(dsp);
127 #ifndef LIBGAV1_Dsp10bpp_FilterIntraPredictor
128   dsp->filter_intra_predictor = FilterIntraPredictor_C<10, uint16_t>;
129 #endif
130 #endif  // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
131 }
132 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
133 
134 }  // namespace
135 
IntraPredFilterInit_C()136 void IntraPredFilterInit_C() {
137   Init8bpp();
138 #if LIBGAV1_MAX_BITDEPTH >= 10
139   Init10bpp();
140 #endif
141 }
142 
143 }  // namespace dsp
144 }  // namespace libgav1
145