• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_SRC_UTILS_COMMON_H_
18 #define LIBGAV1_SRC_UTILS_COMMON_H_
19 
20 #if defined(_MSC_VER)
21 #include <intrin.h>
22 #pragma intrinsic(_BitScanForward)
23 #pragma intrinsic(_BitScanReverse)
24 #if defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
25 #pragma intrinsic(_BitScanReverse64)
26 #define HAVE_BITSCANREVERSE64
27 #endif  // defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64)
28 #endif  // defined(_MSC_VER)
29 
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdint>
33 #include <cstring>
34 #include <type_traits>
35 
36 #include "src/utils/bit_mask_set.h"
37 #include "src/utils/constants.h"
38 #include "src/utils/memory.h"
39 #include "src/utils/types.h"
40 
41 namespace libgav1 {
42 
43 // Aligns |value| to the desired |alignment|. |alignment| must be a power of 2.
44 template <typename T>
Align(T value,T alignment)45 inline T Align(T value, T alignment) {
46   assert(alignment != 0);
47   const T alignment_mask = alignment - 1;
48   return (value + alignment_mask) & ~alignment_mask;
49 }
50 
51 // Aligns |addr| to the desired |alignment|. |alignment| must be a power of 2.
AlignAddr(uint8_t * const addr,const uintptr_t alignment)52 inline uint8_t* AlignAddr(uint8_t* const addr, const uintptr_t alignment) {
53   const auto value = reinterpret_cast<uintptr_t>(addr);
54   return reinterpret_cast<uint8_t*>(Align(value, alignment));
55 }
56 
Clip3(int32_t value,int32_t low,int32_t high)57 inline int32_t Clip3(int32_t value, int32_t low, int32_t high) {
58   return value < low ? low : (value > high ? high : value);
59 }
60 
61 template <typename Pixel>
ExtendLine(void * const line_start,const int width,const int left,const int right)62 void ExtendLine(void* const line_start, const int width, const int left,
63                 const int right) {
64   auto* const start = static_cast<Pixel*>(line_start);
65   const Pixel* src = start;
66   Pixel* dst = start - left;
67   // Copy to left and right borders.
68   Memset(dst, src[0], left);
69   Memset(dst + left + width, src[width - 1], right);
70 }
71 
72 // The following 2 templates set a block of data with uncontiguous memory to
73 // |value|. The compilers usually generate several branches to handle different
74 // cases of |columns| when inlining memset() and std::fill(), and these branches
75 // are unfortunately within the loop of |rows|. So calling these templates
76 // directly could be inefficient. It is recommended to specialize common cases
77 // of |columns|, such as 1, 2, 4, 8, 16 and 32, etc. in advance before
78 // processing the generic case of |columns|. The code size may be larger, but
79 // there would be big speed gains.
80 // Call template MemSetBlock<> when sizeof(|T|) is 1.
81 // Call template SetBlock<> when sizeof(|T|) is larger than 1.
82 template <typename T>
MemSetBlock(int rows,int columns,T value,T * dst,ptrdiff_t stride)83 void MemSetBlock(int rows, int columns, T value, T* dst, ptrdiff_t stride) {
84   static_assert(sizeof(T) == 1, "");
85   do {
86     memset(dst, value, columns);
87     dst += stride;
88   } while (--rows != 0);
89 }
90 
91 template <typename T>
SetBlock(int rows,int columns,T value,T * dst,ptrdiff_t stride)92 void SetBlock(int rows, int columns, T value, T* dst, ptrdiff_t stride) {
93   do {
94     std::fill(dst, dst + columns, value);
95     dst += stride;
96   } while (--rows != 0);
97 }
98 
99 #if defined(__GNUC__)
100 
CountLeadingZeros(uint32_t n)101 inline int CountLeadingZeros(uint32_t n) {
102   assert(n != 0);
103   return __builtin_clz(n);
104 }
105 
CountLeadingZeros(uint64_t n)106 inline int CountLeadingZeros(uint64_t n) {
107   assert(n != 0);
108   return __builtin_clzll(n);
109 }
110 
CountTrailingZeros(uint32_t n)111 inline int CountTrailingZeros(uint32_t n) {
112   assert(n != 0);
113   return __builtin_ctz(n);
114 }
115 
116 #elif defined(_MSC_VER)
117 
CountLeadingZeros(uint32_t n)118 inline int CountLeadingZeros(uint32_t n) {
119   assert(n != 0);
120   unsigned long first_set_bit;  // NOLINT(runtime/int)
121   const unsigned char bit_set = _BitScanReverse(&first_set_bit, n);
122   assert(bit_set != 0);
123   static_cast<void>(bit_set);
124   return 31 ^ static_cast<int>(first_set_bit);
125 }
126 
CountLeadingZeros(uint64_t n)127 inline int CountLeadingZeros(uint64_t n) {
128   assert(n != 0);
129   unsigned long first_set_bit;  // NOLINT(runtime/int)
130 #if defined(HAVE_BITSCANREVERSE64)
131   const unsigned char bit_set =
132       _BitScanReverse64(&first_set_bit, static_cast<unsigned __int64>(n));
133 #else   // !defined(HAVE_BITSCANREVERSE64)
134   const auto n_hi = static_cast<unsigned long>(n >> 32);  // NOLINT(runtime/int)
135   if (n_hi != 0) {
136     const unsigned char bit_set = _BitScanReverse(&first_set_bit, n_hi);
137     assert(bit_set != 0);
138     static_cast<void>(bit_set);
139     return 31 ^ static_cast<int>(first_set_bit);
140   }
141   const unsigned char bit_set = _BitScanReverse(
142       &first_set_bit, static_cast<unsigned long>(n));  // NOLINT(runtime/int)
143 #endif  // defined(HAVE_BITSCANREVERSE64)
144   assert(bit_set != 0);
145   static_cast<void>(bit_set);
146   return 63 ^ static_cast<int>(first_set_bit);
147 }
148 
149 #undef HAVE_BITSCANREVERSE64
150 
CountTrailingZeros(uint32_t n)151 inline int CountTrailingZeros(uint32_t n) {
152   assert(n != 0);
153   unsigned long first_set_bit;  // NOLINT(runtime/int)
154   const unsigned char bit_set = _BitScanForward(&first_set_bit, n);
155   assert(bit_set != 0);
156   static_cast<void>(bit_set);
157   return static_cast<int>(first_set_bit);
158 }
159 
160 #else  // !defined(__GNUC__) && !defined(_MSC_VER)
161 
162 template <const int kMSB, typename T>
CountLeadingZeros(T n)163 inline int CountLeadingZeros(T n) {
164   assert(n != 0);
165   const T msb = T{1} << kMSB;
166   int count = 0;
167   while ((n & msb) == 0) {
168     ++count;
169     n <<= 1;
170   }
171   return count;
172 }
173 
CountLeadingZeros(uint32_t n)174 inline int CountLeadingZeros(uint32_t n) { return CountLeadingZeros<31>(n); }
175 
CountLeadingZeros(uint64_t n)176 inline int CountLeadingZeros(uint64_t n) { return CountLeadingZeros<63>(n); }
177 
178 // This is the algorithm on the left in Figure 5-23, Hacker's Delight, Second
179 // Edition, page 109. The book says:
180 //   If the number of trailing 0's is expected to be small or large, then the
181 //   simple loops shown in Figure 5-23 are quite fast.
CountTrailingZeros(uint32_t n)182 inline int CountTrailingZeros(uint32_t n) {
183   assert(n != 0);
184   // Create a word with 1's at the positions of the trailing 0's in |n|, and
185   // 0's elsewhere (e.g., 01011000 => 00000111).
186   n = ~n & (n - 1);
187   int count = 0;
188   while (n != 0) {
189     ++count;
190     n >>= 1;
191   }
192   return count;
193 }
194 
195 #endif  // defined(__GNUC__)
196 
FloorLog2(int32_t n)197 inline int FloorLog2(int32_t n) {
198   assert(n > 0);
199   return 31 ^ CountLeadingZeros(static_cast<uint32_t>(n));
200 }
201 
FloorLog2(uint32_t n)202 inline int FloorLog2(uint32_t n) {
203   assert(n > 0);
204   return 31 ^ CountLeadingZeros(n);
205 }
206 
FloorLog2(int64_t n)207 inline int FloorLog2(int64_t n) {
208   assert(n > 0);
209   return 63 ^ CountLeadingZeros(static_cast<uint64_t>(n));
210 }
211 
FloorLog2(uint64_t n)212 inline int FloorLog2(uint64_t n) {
213   assert(n > 0);
214   return 63 ^ CountLeadingZeros(n);
215 }
216 
CeilLog2(unsigned int n)217 inline int CeilLog2(unsigned int n) {
218   // The expression FloorLog2(n - 1) + 1 is undefined not only for n == 0 but
219   // also for n == 1, so this expression must be guarded by the n < 2 test. An
220   // alternative implementation is:
221   // return (n == 0) ? 0 : FloorLog2(n) + static_cast<int>((n & (n - 1)) != 0);
222   return (n < 2) ? 0 : FloorLog2(n - 1) + 1;
223 }
224 
RightShiftWithCeiling(int value,int bits)225 inline int RightShiftWithCeiling(int value, int bits) {
226   assert(bits > 0);
227   return (value + (1 << bits) - 1) >> bits;
228 }
229 
RightShiftWithRounding(int32_t value,int bits)230 inline int32_t RightShiftWithRounding(int32_t value, int bits) {
231   assert(bits >= 0);
232   return (value + ((1 << bits) >> 1)) >> bits;
233 }
234 
RightShiftWithRounding(uint32_t value,int bits)235 inline uint32_t RightShiftWithRounding(uint32_t value, int bits) {
236   assert(bits >= 0);
237   return (value + ((1 << bits) >> 1)) >> bits;
238 }
239 
240 // This variant is used when |value| can exceed 32 bits. Although the final
241 // result must always fit into int32_t.
RightShiftWithRounding(int64_t value,int bits)242 inline int32_t RightShiftWithRounding(int64_t value, int bits) {
243   assert(bits >= 0);
244   return static_cast<int32_t>((value + ((int64_t{1} << bits) >> 1)) >> bits);
245 }
246 
RightShiftWithRoundingSigned(int32_t value,int bits)247 inline int32_t RightShiftWithRoundingSigned(int32_t value, int bits) {
248   assert(bits > 0);
249   // The next line is equivalent to:
250   // return (value >= 0) ? RightShiftWithRounding(value, bits)
251   //                     : -RightShiftWithRounding(-value, bits);
252   return RightShiftWithRounding(value + (value >> 31), bits);
253 }
254 
255 // This variant is used when |value| can exceed 32 bits. Although the final
256 // result must always fit into int32_t.
RightShiftWithRoundingSigned(int64_t value,int bits)257 inline int32_t RightShiftWithRoundingSigned(int64_t value, int bits) {
258   assert(bits > 0);
259   // The next line is equivalent to:
260   // return (value >= 0) ? RightShiftWithRounding(value, bits)
261   //                     : -RightShiftWithRounding(-value, bits);
262   return RightShiftWithRounding(value + (value >> 63), bits);
263 }
264 
DivideBy2(int n)265 constexpr int DivideBy2(int n) { return n >> 1; }
DivideBy4(int n)266 constexpr int DivideBy4(int n) { return n >> 2; }
DivideBy8(int n)267 constexpr int DivideBy8(int n) { return n >> 3; }
DivideBy16(int n)268 constexpr int DivideBy16(int n) { return n >> 4; }
DivideBy32(int n)269 constexpr int DivideBy32(int n) { return n >> 5; }
DivideBy64(int n)270 constexpr int DivideBy64(int n) { return n >> 6; }
DivideBy128(int n)271 constexpr int DivideBy128(int n) { return n >> 7; }
272 
273 // Convert |value| to unsigned before shifting to avoid undefined behavior with
274 // negative values.
LeftShift(int value,int bits)275 inline int LeftShift(int value, int bits) {
276   assert(bits >= 0);
277   assert(value >= -(int64_t{1} << (31 - bits)));
278   assert(value <= (int64_t{1} << (31 - bits)) - ((bits == 0) ? 1 : 0));
279   return static_cast<int>(static_cast<uint32_t>(value) << bits);
280 }
MultiplyBy2(int n)281 inline int MultiplyBy2(int n) { return LeftShift(n, 1); }
MultiplyBy4(int n)282 inline int MultiplyBy4(int n) { return LeftShift(n, 2); }
MultiplyBy8(int n)283 inline int MultiplyBy8(int n) { return LeftShift(n, 3); }
MultiplyBy16(int n)284 inline int MultiplyBy16(int n) { return LeftShift(n, 4); }
MultiplyBy32(int n)285 inline int MultiplyBy32(int n) { return LeftShift(n, 5); }
MultiplyBy64(int n)286 inline int MultiplyBy64(int n) { return LeftShift(n, 6); }
287 
Mod32(int n)288 constexpr int Mod32(int n) { return n & 0x1f; }
Mod64(int n)289 constexpr int Mod64(int n) { return n & 0x3f; }
290 
291 //------------------------------------------------------------------------------
292 // Bitstream functions
293 
IsIntraFrame(FrameType type)294 constexpr bool IsIntraFrame(FrameType type) {
295   return type == kFrameKey || type == kFrameIntraOnly;
296 }
297 
GetTransformClass(TransformType tx_type)298 inline TransformClass GetTransformClass(TransformType tx_type) {
299   constexpr BitMaskSet kTransformClassVerticalMask(
300       kTransformTypeIdentityDct, kTransformTypeIdentityAdst,
301       kTransformTypeIdentityFlipadst);
302   if (kTransformClassVerticalMask.Contains(tx_type)) {
303     return kTransformClassVertical;
304   }
305   constexpr BitMaskSet kTransformClassHorizontalMask(
306       kTransformTypeDctIdentity, kTransformTypeAdstIdentity,
307       kTransformTypeFlipadstIdentity);
308   if (kTransformClassHorizontalMask.Contains(tx_type)) {
309     return kTransformClassHorizontal;
310   }
311   return kTransformClass2D;
312 }
313 
RowOrColumn4x4ToPixel(int row_or_column4x4,Plane plane,int8_t subsampling)314 inline int RowOrColumn4x4ToPixel(int row_or_column4x4, Plane plane,
315                                  int8_t subsampling) {
316   return MultiplyBy4(row_or_column4x4) >> (plane == kPlaneY ? 0 : subsampling);
317 }
318 
GetPlaneType(Plane plane)319 constexpr PlaneType GetPlaneType(Plane plane) {
320   return static_cast<PlaneType>(plane != kPlaneY);
321 }
322 
323 // 5.11.44.
IsDirectionalMode(PredictionMode mode)324 constexpr bool IsDirectionalMode(PredictionMode mode) {
325   return mode >= kPredictionModeVertical && mode <= kPredictionModeD67;
326 }
327 
328 // 5.9.3.
329 //
330 // |a| and |b| are order hints, treated as unsigned order_hint_bits-bit
331 // integers. |order_hint_shift_bits| equals (32 - order_hint_bits) % 32.
332 // order_hint_bits is at most 8, so |order_hint_shift_bits| is zero or a
333 // value between 24 and 31 (inclusive).
334 //
335 // If |order_hint_shift_bits| is zero, |a| and |b| are both zeros, and the
336 // result is zero. If |order_hint_shift_bits| is not zero, returns the
337 // signed difference |a| - |b| using "modular arithmetic". More precisely, the
338 // signed difference |a| - |b| is treated as a signed order_hint_bits-bit
339 // integer and cast to an int. The returned difference is between
340 // -(1 << (order_hint_bits - 1)) and (1 << (order_hint_bits - 1)) - 1
341 // (inclusive).
342 //
343 // NOTE: |a| and |b| are the order_hint_bits least significant bits of the
344 // actual values. This function returns the signed difference between the
345 // actual values. The returned difference is correct as long as the actual
346 // values are not more than 1 << (order_hint_bits - 1) - 1 apart.
347 //
348 // Example: Suppose order_hint_bits is 4 and |order_hint_shift_bits|
349 // is 28. Then |a| and |b| are in the range [0, 15], and the actual values for
350 // |a| and |b| must not be more than 7 apart. (If the actual values for |a| and
351 // |b| are exactly 8 apart, this function cannot tell whether the actual value
352 // for |a| is before or after the actual value for |b|.)
353 //
354 // First, consider the order hints 2 and 6. For this simple case, we have
355 //   GetRelativeDistance(2, 6, 28) = 2 - 6 = -4, and
356 //   GetRelativeDistance(6, 2, 28) = 6 - 2 = 4.
357 //
358 // On the other hand, consider the order hints 2 and 14. The order hints are
359 // 12 (> 7) apart, so we need to use the actual values instead. The actual
360 // values may be 34 (= 2 mod 16) and 30 (= 14 mod 16), respectively. Therefore
361 // we have
362 //   GetRelativeDistance(2, 14, 28) = 34 - 30 = 4, and
363 //   GetRelativeDistance(14, 2, 28) = 30 - 34 = -4.
364 //
365 // The following comments apply only to specific CPUs' SIMD implementations,
366 // such as intrinsics code.
367 // For the 2 shift operations in this function, if the SIMD packed data is
368 // 16-bit wide, try to use |order_hint_shift_bits| - 16 as the number of bits to
369 // shift; If the SIMD packed data is 8-bit wide, try to use
370 // |order_hint_shift_bits| - 24 as as the number of bits to shift.
371 // |order_hint_shift_bits| - 16 and |order_hint_shift_bits| - 24 could be -16 or
372 // -24. In these cases diff is 0, and the behavior of left or right shifting -16
373 // or -24 bits is defined for x86 SIMD instructions and ARM NEON instructions,
374 // and the result of shifting 0 is still 0. There is no guarantee that this
375 // behavior and result apply to other CPUs' SIMD instructions.
GetRelativeDistance(const unsigned int a,const unsigned int b,const unsigned int order_hint_shift_bits)376 inline int GetRelativeDistance(const unsigned int a, const unsigned int b,
377                                const unsigned int order_hint_shift_bits) {
378   const int diff = static_cast<int>(a) - static_cast<int>(b);
379   assert(order_hint_shift_bits <= 31);
380   if (order_hint_shift_bits == 0) {
381     assert(a == 0);
382     assert(b == 0);
383   } else {
384     assert(order_hint_shift_bits >= 24);  // i.e., order_hint_bits <= 8
385     assert(a < (1u << (32 - order_hint_shift_bits)));
386     assert(b < (1u << (32 - order_hint_shift_bits)));
387     assert(diff < (1 << (32 - order_hint_shift_bits)));
388     assert(diff >= -(1 << (32 - order_hint_shift_bits)));
389   }
390   // Sign extend the result of subtracting the values.
391   // Cast to unsigned int and then left shift to avoid undefined behavior with
392   // negative values. Cast to int to do the sign extension through right shift.
393   // This requires the right shift of a signed integer be an arithmetic shift,
394   // which is true for clang, gcc, and Visual C++.
395   // These two casts do not generate extra instructions.
396   // Don't use LeftShift(diff) since a valid diff may fail its assertions.
397   // For example, GetRelativeDistance(2, 14, 28), diff equals -12 and is less
398   // than the minimum allowed value of LeftShift() which is -8.
399   // The next 3 lines are equivalent to:
400   // const int order_hint_bits = Mod32(32 - order_hint_shift_bits);
401   // const int m = (1 << order_hint_bits) >> 1;
402   // return (diff & (m - 1)) - (diff & m);
403   return static_cast<int>(static_cast<unsigned int>(diff)
404                           << order_hint_shift_bits) >>
405          order_hint_shift_bits;
406 }
407 
408 // Applies |sign| (must be 0 or -1) to |value|, i.e.,
409 //   return (sign == 0) ? value : -value;
410 // and does so without a branch.
ApplySign(int value,int sign)411 constexpr int ApplySign(int value, int sign) { return (value ^ sign) - sign; }
412 
413 // 7.9.3. (without the clamp for numerator and denominator).
GetMvProjection(const MotionVector & mv,int numerator,int division_multiplier,MotionVector * projection_mv)414 inline void GetMvProjection(const MotionVector& mv, int numerator,
415                             int division_multiplier,
416                             MotionVector* projection_mv) {
417   // Allow numerator and to be 0 so that this function can be called
418   // unconditionally. When numerator is 0, |projection_mv| will be 0, and this
419   // is what we want.
420   assert(std::abs(numerator) <= kMaxFrameDistance);
421   for (int i = 0; i < 2; ++i) {
422     projection_mv->mv[i] =
423         Clip3(RightShiftWithRoundingSigned(
424                   mv.mv[i] * numerator * division_multiplier, 14),
425               -kProjectionMvClamp, kProjectionMvClamp);
426   }
427 }
428 
429 // 7.9.4.
Project(int value,int delta,int dst_sign)430 constexpr int Project(int value, int delta, int dst_sign) {
431   return value + ApplySign(delta / 64, dst_sign);
432 }
433 
IsBlockSmallerThan8x8(BlockSize size)434 inline bool IsBlockSmallerThan8x8(BlockSize size) {
435   return size < kBlock8x8 && size != kBlock4x16;
436 }
437 
438 // Returns true if the either the width or the height of the block is equal to
439 // four.
IsBlockDimension4(BlockSize size)440 inline bool IsBlockDimension4(BlockSize size) {
441   return size < kBlock8x8 || size == kBlock16x4;
442 }
443 
444 // Converts bitdepth 8, 10, and 12 to array index 0, 1, and 2, respectively.
BitdepthToArrayIndex(int bitdepth)445 constexpr int BitdepthToArrayIndex(int bitdepth) { return (bitdepth - 8) >> 1; }
446 
447 // Maps a square transform to an index between [0, 4]. kTransformSize4x4 maps
448 // to 0, kTransformSize8x8 maps to 1 and so on.
TransformSizeToSquareTransformIndex(TransformSize tx_size)449 inline int TransformSizeToSquareTransformIndex(TransformSize tx_size) {
450   assert(kTransformWidth[tx_size] == kTransformHeight[tx_size]);
451 
452   // The values of the square transform sizes happen to be in the right
453   // ranges, so we can just divide them by 4 to get the indexes.
454   static_assert(
455       std::is_unsigned<std::underlying_type<TransformSize>::type>::value, "");
456   static_assert(kTransformSize4x4 < 4, "");
457   static_assert(4 <= kTransformSize8x8 && kTransformSize8x8 < 8, "");
458   static_assert(8 <= kTransformSize16x16 && kTransformSize16x16 < 12, "");
459   static_assert(12 <= kTransformSize32x32 && kTransformSize32x32 < 16, "");
460   static_assert(16 <= kTransformSize64x64 && kTransformSize64x64 < 20, "");
461   return DivideBy4(tx_size);
462 }
463 
464 // Gets the corresponding Y/U/V position, to set and get filter masks
465 // in deblock filtering.
466 // Returns luma_position if it's Y plane, whose subsampling must be 0.
467 // Returns the odd position for U/V plane, if there is subsampling.
GetDeblockPosition(const int luma_position,const int subsampling)468 constexpr int GetDeblockPosition(const int luma_position,
469                                  const int subsampling) {
470   return luma_position | subsampling;
471 }
472 
473 // Returns the size of the residual buffer required to hold the residual values
474 // for a block or frame of size |rows| by |columns| (taking into account
475 // |subsampling_x|, |subsampling_y| and |residual_size|). |residual_size| is the
476 // number of bytes required to represent one residual value.
GetResidualBufferSize(const int rows,const int columns,const int subsampling_x,const int subsampling_y,const size_t residual_size)477 inline size_t GetResidualBufferSize(const int rows, const int columns,
478                                     const int subsampling_x,
479                                     const int subsampling_y,
480                                     const size_t residual_size) {
481   // The subsampling multipliers are:
482   //   Both x and y are subsampled: 3 / 2.
483   //   Only x or y is subsampled: 2 / 1 (which is equivalent to 4 / 2).
484   //   Both x and y are not subsampled: 3 / 1 (which is equivalent to 6 / 2).
485   // So we compute the final subsampling multiplier as follows:
486   //   multiplier = (2 + (4 >> subsampling_x >> subsampling_y)) / 2.
487   // Add 32 * |kResidualPaddingVertical| padding to avoid bottom boundary checks
488   // when parsing quantized coefficients.
489   const int subsampling_multiplier_num =
490       2 + (4 >> subsampling_x >> subsampling_y);
491   const int number_elements =
492       (rows * columns * subsampling_multiplier_num) >> 1;
493   const int tx_padding = 32 * kResidualPaddingVertical;
494   return residual_size * (number_elements + tx_padding);
495 }
496 
497 // This function is equivalent to:
498 // std::min({kTransformWidthLog2[tx_size] - 2,
499 //           kTransformWidthLog2[left_tx_size] - 2,
500 //           2});
GetTransformSizeIdWidth(TransformSize tx_size,TransformSize left_tx_size)501 constexpr LoopFilterTransformSizeId GetTransformSizeIdWidth(
502     TransformSize tx_size, TransformSize left_tx_size) {
503   return static_cast<LoopFilterTransformSizeId>(
504       static_cast<int>(tx_size > kTransformSize4x16 &&
505                        left_tx_size > kTransformSize4x16) +
506       static_cast<int>(tx_size > kTransformSize8x32 &&
507                        left_tx_size > kTransformSize8x32));
508 }
509 
510 // This is used for 7.11.3.4 Block Inter Prediction Process, to select convolve
511 // filters.
GetFilterIndex(const int filter_index,const int length)512 inline int GetFilterIndex(const int filter_index, const int length) {
513   if (length <= 4) {
514     if (filter_index == kInterpolationFilterEightTap ||
515         filter_index == kInterpolationFilterEightTapSharp) {
516       return 4;
517     }
518     if (filter_index == kInterpolationFilterEightTapSmooth) {
519       return 5;
520     }
521   }
522   return filter_index;
523 }
524 
525 // This has identical results as RightShiftWithRounding since |subsampling| can
526 // only be 0 or 1.
SubsampledValue(int value,int subsampling)527 constexpr int SubsampledValue(int value, int subsampling) {
528   return (value + subsampling) >> subsampling;
529 }
530 
531 }  // namespace libgav1
532 
533 #endif  // LIBGAV1_SRC_UTILS_COMMON_H_
534