1 // Copyright 2019 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/intra_edge.h"
16
17 #include <cassert>
18 #include <cstdint>
19 #include <cstring>
20
21 #include "src/dsp/dsp.h"
22 #include "src/utils/common.h"
23
24 namespace libgav1 {
25 namespace dsp {
26 namespace {
27
28 constexpr int kKernelTaps = 5;
29 constexpr int kKernels[3][kKernelTaps] = {
30 {0, 4, 8, 4, 0}, {0, 5, 6, 5, 0}, {2, 4, 4, 4, 2}};
31 constexpr int kMaxUpsampleSize = 16;
32
33 template <typename Pixel>
IntraEdgeFilter_C(void * buffer,int size,int strength)34 void IntraEdgeFilter_C(void* buffer, int size, int strength) {
35 assert(strength > 0);
36 Pixel edge[129];
37 memcpy(edge, buffer, sizeof(edge[0]) * size);
38 auto* const dst_buffer = static_cast<Pixel*>(buffer);
39 const int kernel_index = strength - 1;
40 for (int i = 1; i < size; ++i) {
41 int sum = 0;
42 for (int j = 0; j < kKernelTaps; ++j) {
43 const int k = Clip3(i + j - 2, 0, size - 1);
44 sum += kKernels[kernel_index][j] * edge[k];
45 }
46 dst_buffer[i] = RightShiftWithRounding(sum, 4);
47 }
48 }
49
50 template <int bitdepth, typename Pixel>
IntraEdgeUpsampler_C(void * buffer,int size)51 void IntraEdgeUpsampler_C(void* buffer, int size) {
52 assert(size % 4 == 0 && size <= kMaxUpsampleSize);
53 auto* const pixel_buffer = static_cast<Pixel*>(buffer);
54 Pixel temp[kMaxUpsampleSize + 3];
55 temp[0] = temp[1] = pixel_buffer[-1];
56 memcpy(temp + 2, pixel_buffer, sizeof(temp[0]) * size);
57 temp[size + 2] = pixel_buffer[size - 1];
58
59 pixel_buffer[-2] = temp[0];
60 for (int i = 0; i < size; ++i) {
61 const int sum =
62 -temp[i] + (9 * temp[i + 1]) + (9 * temp[i + 2]) - temp[i + 3];
63 pixel_buffer[2 * i - 1] =
64 Clip3(RightShiftWithRounding(sum, 4), 0, (1 << bitdepth) - 1);
65 pixel_buffer[2 * i] = temp[i + 2];
66 }
67 }
68
Init8bpp()69 void Init8bpp() {
70 Dsp* const dsp = dsp_internal::GetWritableDspTable(8);
71 assert(dsp != nullptr);
72 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
73 dsp->intra_edge_filter = IntraEdgeFilter_C<uint8_t>;
74 dsp->intra_edge_upsampler = IntraEdgeUpsampler_C<8, uint8_t>;
75 #else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
76 static_cast<void>(dsp);
77 #ifndef LIBGAV1_Dsp8bpp_IntraEdgeFilter
78 dsp->intra_edge_filter = IntraEdgeFilter_C<uint8_t>;
79 #endif
80 #ifndef LIBGAV1_Dsp8bpp_IntraEdgeUpsampler
81 dsp->intra_edge_upsampler = IntraEdgeUpsampler_C<8, uint8_t>;
82 #endif
83 #endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
84 }
85
86 #if LIBGAV1_MAX_BITDEPTH >= 10
Init10bpp()87 void Init10bpp() {
88 Dsp* const dsp = dsp_internal::GetWritableDspTable(10);
89 assert(dsp != nullptr);
90 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
91 dsp->intra_edge_filter = IntraEdgeFilter_C<uint16_t>;
92 dsp->intra_edge_upsampler = IntraEdgeUpsampler_C<10, uint16_t>;
93 #else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
94 static_cast<void>(dsp);
95 #ifndef LIBGAV1_Dsp10bpp_IntraEdgeFilter
96 dsp->intra_edge_filter = IntraEdgeFilter_C<uint16_t>;
97 #endif
98 #ifndef LIBGAV1_Dsp10bpp_IntraEdgeUpsampler
99 dsp->intra_edge_upsampler = IntraEdgeUpsampler_C<10, uint16_t>;
100 #endif
101 #endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
102 }
103 #endif
104
105 } // namespace
106
IntraEdgeInit_C()107 void IntraEdgeInit_C() {
108 Init8bpp();
109 #if LIBGAV1_MAX_BITDEPTH >= 10
110 Init10bpp();
111 #endif
112 }
113
114 } // namespace dsp
115 } // namespace libgav1
116