Utilities.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2021, Hunter Salyer <thefalsehonesty@gmail.com>
  3. * Copyright (c) 2022, Gregory Bertilson <zaggy1024@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Types.h>
  9. #include <LibGfx/Size.h>
  10. #include "LookupTables.h"
  11. namespace Video::VP9 {
  12. // FIXME: Once everything is working, replace this with plain clamp
  13. // since parameter order is different
  14. template<typename T>
  15. T clip_3(T x, T y, T z)
  16. {
  17. return clamp(z, x, y);
  18. }
  19. template<typename T>
  20. u16 clip_1(u8 bit_depth, T x)
  21. {
  22. if (x < 0) {
  23. return 0u;
  24. }
  25. const T max = (1u << bit_depth) - 1u;
  26. if (x > max)
  27. return max;
  28. return x;
  29. }
  30. template<u8 bits>
  31. inline u8 brev(u8 value)
  32. {
  33. static_assert(bits <= 8, "brev() expects an 8-bit value.");
  34. static constexpr auto lookup_table = [] {
  35. constexpr size_t value_count = 1 << bits;
  36. Array<u8, value_count> the_table;
  37. for (u8 lookup_value = 0; lookup_value < value_count; lookup_value++) {
  38. u8 reversed = 0;
  39. for (u8 bit_index = 0; bit_index < bits; bit_index++) {
  40. auto bit = (lookup_value >> bit_index) & 1;
  41. reversed |= bit << (bits - 1 - bit_index);
  42. }
  43. the_table[lookup_value] = reversed;
  44. }
  45. return the_table;
  46. }();
  47. return lookup_table[value];
  48. }
  49. inline BlockSubsize get_subsampled_block_size(BlockSubsize size, bool subsampling_x, bool subsampling_y)
  50. {
  51. return ss_size_lookup[size < Block_8x8 ? Block_8x8 : size][subsampling_x][subsampling_y];
  52. }
  53. inline Gfx::Size<u8> block_size_to_blocks(BlockSubsize size)
  54. {
  55. return Gfx::Size<u8>(num_8x8_blocks_wide_lookup[size], num_8x8_blocks_high_lookup[size]);
  56. }
  57. inline Gfx::Size<u8> block_size_to_sub_blocks(BlockSubsize size)
  58. {
  59. return Gfx::Size<u8>(num_4x4_blocks_wide_lookup[size], num_4x4_blocks_high_lookup[size]);
  60. }
  61. template<Integral T>
  62. inline T blocks_to_superblocks(T blocks)
  63. {
  64. return blocks >> 3;
  65. }
  66. template<Integral T>
  67. inline T superblocks_to_blocks(T superblocks)
  68. {
  69. return superblocks << 3;
  70. }
  71. template<Integral T>
  72. inline T blocks_ceiled_to_superblocks(T blocks)
  73. {
  74. return blocks_to_superblocks(blocks + 7);
  75. }
  76. template<Integral T>
  77. inline T blocks_to_sub_blocks(T blocks)
  78. {
  79. return blocks << 1;
  80. }
  81. template<Integral T>
  82. inline T sub_blocks_to_blocks(T sub_blocks)
  83. {
  84. return sub_blocks >> 1;
  85. }
  86. template<Integral T>
  87. inline T sub_blocks_to_pixels(T sub_blocks)
  88. {
  89. return sub_blocks << 2;
  90. }
  91. template<Integral T>
  92. inline T pixels_to_sub_blocks(T pixels)
  93. {
  94. return pixels >> 2;
  95. }
  96. template<Integral T>
  97. inline T blocks_to_pixels(T blocks)
  98. {
  99. return sub_blocks_to_pixels(blocks_to_sub_blocks(blocks));
  100. }
  101. template<Integral T>
  102. inline T pixels_to_blocks(T pixels)
  103. {
  104. return sub_blocks_to_blocks(pixels_to_sub_blocks(pixels));
  105. }
  106. inline u8 transform_size_to_sub_blocks(TransformSize transform_size)
  107. {
  108. return 1 << transform_size;
  109. }
  110. inline u32 y_size_to_uv_size(bool subsampled, u32 size)
  111. {
  112. return (size + subsampled) >> subsampled;
  113. }
  114. }