xref: /aosp_15_r20/external/libgav1/src/dsp/obmc.cc (revision 095378508e87ed692bf8dfeb34008b65b3735891)
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/obmc.h"
16 
17 #include <cassert>
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "src/dsp/dsp.h"
22 #include "src/utils/common.h"
23 #include "src/utils/constants.h"
24 
25 namespace libgav1 {
26 namespace dsp {
27 namespace {
28 
29 #include "src/dsp/obmc.inc"
30 
31 // 7.11.3.10 (from top samples).
32 template <typename Pixel>
OverlapBlendVertical_C(void * LIBGAV1_RESTRICT const prediction,const ptrdiff_t prediction_stride,const int width,const int height,const void * LIBGAV1_RESTRICT const obmc_prediction,const ptrdiff_t obmc_prediction_stride)33 void OverlapBlendVertical_C(void* LIBGAV1_RESTRICT const prediction,
34                             const ptrdiff_t prediction_stride, const int width,
35                             const int height,
36                             const void* LIBGAV1_RESTRICT const obmc_prediction,
37                             const ptrdiff_t obmc_prediction_stride) {
38   auto* pred = static_cast<Pixel*>(prediction);
39   const ptrdiff_t pred_stride = prediction_stride / sizeof(Pixel);
40   const auto* obmc_pred = static_cast<const Pixel*>(obmc_prediction);
41   const ptrdiff_t obmc_pred_stride = obmc_prediction_stride / sizeof(Pixel);
42   const uint8_t* const mask = kObmcMask + height - 2;
43   assert(width >= 4);
44   assert(height >= 2);
45 
46   for (int y = 0; y < height; ++y) {
47     const uint8_t mask_value = mask[y];
48     for (int x = 0; x < width; ++x) {
49       pred[x] = static_cast<Pixel>(RightShiftWithRounding(
50           mask_value * pred[x] + (64 - mask_value) * obmc_pred[x], 6));
51     }
52     pred += pred_stride;
53     obmc_pred += obmc_pred_stride;
54   }
55 }
56 
57 // 7.11.3.10 (from left samples).
58 template <typename Pixel>
OverlapBlendHorizontal_C(void * LIBGAV1_RESTRICT const prediction,const ptrdiff_t prediction_stride,const int width,const int height,const void * LIBGAV1_RESTRICT const obmc_prediction,const ptrdiff_t obmc_prediction_stride)59 void OverlapBlendHorizontal_C(
60     void* LIBGAV1_RESTRICT const prediction, const ptrdiff_t prediction_stride,
61     const int width, const int height,
62     const void* LIBGAV1_RESTRICT const obmc_prediction,
63     const ptrdiff_t obmc_prediction_stride) {
64   auto* pred = static_cast<Pixel*>(prediction);
65   const ptrdiff_t pred_stride = prediction_stride / sizeof(Pixel);
66   const auto* obmc_pred = static_cast<const Pixel*>(obmc_prediction);
67   const ptrdiff_t obmc_pred_stride = obmc_prediction_stride / sizeof(Pixel);
68   const uint8_t* const mask = kObmcMask + width - 2;
69   assert(width >= 2);
70   assert(height >= 4);
71 
72   for (int y = 0; y < height; ++y) {
73     for (int x = 0; x < width; ++x) {
74       const uint8_t mask_value = mask[x];
75       pred[x] = static_cast<Pixel>(RightShiftWithRounding(
76           mask_value * pred[x] + (64 - mask_value) * obmc_pred[x], 6));
77     }
78     pred += pred_stride;
79     obmc_pred += obmc_pred_stride;
80   }
81 }
82 
Init8bpp()83 void Init8bpp() {
84   Dsp* const dsp = dsp_internal::GetWritableDspTable(8);
85   assert(dsp != nullptr);
86 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
87   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint8_t>;
88   dsp->obmc_blend[kObmcDirectionHorizontal] = OverlapBlendHorizontal_C<uint8_t>;
89 #else  // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
90   static_cast<void>(dsp);
91 #ifndef LIBGAV1_Dsp8bpp_ObmcVertical
92   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint8_t>;
93 #endif
94 #ifndef LIBGAV1_Dsp8bpp_ObmcHorizontal
95   dsp->obmc_blend[kObmcDirectionHorizontal] = OverlapBlendHorizontal_C<uint8_t>;
96 #endif
97 #endif  // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
98 }
99 
100 #if LIBGAV1_MAX_BITDEPTH >= 10
Init10bpp()101 void Init10bpp() {
102   Dsp* const dsp = dsp_internal::GetWritableDspTable(10);
103   assert(dsp != nullptr);
104 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
105   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint16_t>;
106   dsp->obmc_blend[kObmcDirectionHorizontal] =
107       OverlapBlendHorizontal_C<uint16_t>;
108 #else  // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
109   static_cast<void>(dsp);
110 #ifndef LIBGAV1_Dsp10bpp_ObmcVertical
111   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint16_t>;
112 #endif
113 #ifndef LIBGAV1_Dsp10bpp_ObmcHorizontal
114   dsp->obmc_blend[kObmcDirectionHorizontal] =
115       OverlapBlendHorizontal_C<uint16_t>;
116 #endif
117 #endif  // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
118 }
119 #endif  // LIBGAV1_MAX_BITDEPTH >= 10
120 
121 #if LIBGAV1_MAX_BITDEPTH == 12
Init12bpp()122 void Init12bpp() {
123   Dsp* const dsp = dsp_internal::GetWritableDspTable(12);
124   assert(dsp != nullptr);
125 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
126   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint16_t>;
127   dsp->obmc_blend[kObmcDirectionHorizontal] =
128       OverlapBlendHorizontal_C<uint16_t>;
129 #else  // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
130   static_cast<void>(dsp);
131 #ifndef LIBGAV1_Dsp12bpp_ObmcVertical
132   dsp->obmc_blend[kObmcDirectionVertical] = OverlapBlendVertical_C<uint16_t>;
133 #endif
134 #ifndef LIBGAV1_Dsp12bpp_ObmcHorizontal
135   dsp->obmc_blend[kObmcDirectionHorizontal] =
136       OverlapBlendHorizontal_C<uint16_t>;
137 #endif
138 #endif  // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
139 }
140 #endif  // LIBGAV1_MAX_BITDEPTH == 12
141 
142 }  // namespace
143 
ObmcInit_C()144 void ObmcInit_C() {
145   Init8bpp();
146 #if LIBGAV1_MAX_BITDEPTH >= 10
147   Init10bpp();
148 #endif
149 #if LIBGAV1_MAX_BITDEPTH == 12
150   Init12bpp();
151 #endif
152 }
153 
154 }  // namespace dsp
155 }  // namespace libgav1
156