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/distance_weighted_blend.h"
16
17 #include <cassert>
18 #include <cstddef>
19 #include <cstdint>
20 #include <type_traits>
21
22 #include "src/dsp/dsp.h"
23 #include "src/utils/common.h"
24
25 namespace libgav1 {
26 namespace dsp {
27 namespace {
28
29 template <int bitdepth, typename Pixel>
DistanceWeightedBlend_C(const void * prediction_0,const void * prediction_1,const uint8_t weight_0,const uint8_t weight_1,const int width,const int height,void * const dest,const ptrdiff_t dest_stride)30 void DistanceWeightedBlend_C(const void* prediction_0, const void* prediction_1,
31 const uint8_t weight_0, const uint8_t weight_1,
32 const int width, const int height,
33 void* const dest, const ptrdiff_t dest_stride) {
34 // 7.11.3.2 Rounding variables derivation process
35 // 2 * FILTER_BITS(7) - (InterRound0(3|5) + InterRound1(7))
36 constexpr int inter_post_round_bits = (bitdepth == 12) ? 2 : 4;
37 using PredType =
38 typename std::conditional<bitdepth == 8, int16_t, uint16_t>::type;
39 const auto* pred_0 = static_cast<const PredType*>(prediction_0);
40 const auto* pred_1 = static_cast<const PredType*>(prediction_1);
41 auto* dst = static_cast<Pixel*>(dest);
42 const ptrdiff_t dst_stride = dest_stride / sizeof(Pixel);
43
44 int y = 0;
45 do {
46 int x = 0;
47 do {
48 // See warp.cc and convolve.cc for detailed prediction ranges.
49 // weight_0 + weight_1 = 16.
50 int res = pred_0[x] * weight_0 + pred_1[x] * weight_1;
51 res -= (bitdepth == 8) ? 0 : kCompoundOffset * 16;
52 dst[x] = static_cast<Pixel>(
53 Clip3(RightShiftWithRounding(res, inter_post_round_bits + 4), 0,
54 (1 << bitdepth) - 1));
55 } while (++x < width);
56
57 dst += dst_stride;
58 pred_0 += width;
59 pred_1 += width;
60 } while (++y < height);
61 }
62
Init8bpp()63 void Init8bpp() {
64 Dsp* const dsp = dsp_internal::GetWritableDspTable(8);
65 assert(dsp != nullptr);
66 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
67 dsp->distance_weighted_blend = DistanceWeightedBlend_C<8, uint8_t>;
68 #else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
69 static_cast<void>(dsp);
70 #ifndef LIBGAV1_Dsp8bpp_DistanceWeightedBlend
71 dsp->distance_weighted_blend = DistanceWeightedBlend_C<8, uint8_t>;
72 #endif
73 #endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
74 }
75
76 #if LIBGAV1_MAX_BITDEPTH >= 10
Init10bpp()77 void Init10bpp() {
78 Dsp* const dsp = dsp_internal::GetWritableDspTable(10);
79 assert(dsp != nullptr);
80 #if LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
81 dsp->distance_weighted_blend = DistanceWeightedBlend_C<10, uint16_t>;
82 #else // !LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
83 static_cast<void>(dsp);
84 #ifndef LIBGAV1_Dsp10bpp_DistanceWeightedBlend
85 dsp->distance_weighted_blend = DistanceWeightedBlend_C<10, uint16_t>;
86 #endif
87 #endif // LIBGAV1_ENABLE_ALL_DSP_FUNCTIONS
88 }
89 #endif
90
91 } // namespace
92
DistanceWeightedBlendInit_C()93 void DistanceWeightedBlendInit_C() {
94 Init8bpp();
95 #if LIBGAV1_MAX_BITDEPTH >= 10
96 Init10bpp();
97 #endif
98 }
99
100 } // namespace dsp
101 } // namespace libgav1
102