TreeParser.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 "BitStream.h"
  9. #include "Enums.h"
  10. #include "ProbabilityTables.h"
  11. #include "SyntaxElementCounter.h"
  12. namespace Video::VP9 {
  13. class Parser;
  14. class TreeParser {
  15. public:
  16. explicit TreeParser(Parser& decoder)
  17. : m_decoder(decoder)
  18. {
  19. }
  20. // FIXME: Move or remove this class once it is unused in the header.
  21. class TreeSelection {
  22. public:
  23. union TreeSelectionValue {
  24. int const* m_tree;
  25. int m_value;
  26. };
  27. constexpr TreeSelection(int const* values)
  28. : m_is_single_value(false)
  29. , m_value { .m_tree = values }
  30. {
  31. }
  32. constexpr TreeSelection(int value)
  33. : m_is_single_value(true)
  34. , m_value { .m_value = value }
  35. {
  36. }
  37. bool is_single_value() const { return m_is_single_value; }
  38. int single_value() const { return m_value.m_value; }
  39. int const* tree() const { return m_value.m_tree; }
  40. private:
  41. bool m_is_single_value;
  42. TreeSelectionValue m_value;
  43. };
  44. /* (9.3.3) */
  45. template<typename T = int>
  46. ErrorOr<T> parse_tree(SyntaxElementType type);
  47. /* (9.3.1) */
  48. TreeSelection select_tree(SyntaxElementType type);
  49. /* (9.3.2) */
  50. u8 select_tree_probability(SyntaxElementType type, u8 node);
  51. /* (9.3.4) */
  52. void count_syntax_element(SyntaxElementType type, int value);
  53. static ErrorOr<Partition> parse_partition(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, bool has_rows, bool has_columns, BlockSubsize block_subsize, u8 num_8x8, Vector<u8> const& above_partition_context, Vector<u8> const& left_partition_context, u32 row, u32 column, bool frame_is_intra);
  54. static ErrorOr<PredictionMode> parse_default_intra_mode(BitStream&, ProbabilityTables const&, BlockSubsize mi_size, Optional<Array<PredictionMode, 4> const&> above_context, Optional<Array<PredictionMode, 4> const&> left_context, PredictionMode block_sub_modes[4], u8 index_x, u8 index_y);
  55. static ErrorOr<PredictionMode> parse_default_uv_mode(BitStream&, ProbabilityTables const&, PredictionMode y_mode);
  56. static ErrorOr<PredictionMode> parse_intra_mode(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, BlockSubsize mi_size);
  57. static ErrorOr<PredictionMode> parse_sub_intra_mode(BitStream&, ProbabilityTables const&, SyntaxElementCounter&);
  58. static ErrorOr<PredictionMode> parse_uv_mode(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, PredictionMode y_mode);
  59. static ErrorOr<PredictionMode> parse_inter_mode(BitStream&, ProbabilityTables const&, SyntaxElementCounter&, u8 mode_context_for_ref_frame_0);
  60. void set_default_intra_mode_variables(u8 idx, u8 idy)
  61. {
  62. m_idx = idx;
  63. m_idy = idy;
  64. }
  65. void set_tokens_variables(u8 band, u32 c, u32 plane, TXSize tx_size, u32 pos);
  66. void set_start_x_and_y(u32 start_x, u32 start_y)
  67. {
  68. m_start_x = start_x;
  69. m_start_y = start_y;
  70. }
  71. void set_mv_component(u8 component)
  72. {
  73. m_mv_component = component;
  74. }
  75. ErrorOr<bool> parse_mv_bit(u8 bit)
  76. {
  77. m_mv_bit = bit;
  78. return parse_tree<bool>(SyntaxElementType::MVBit);
  79. }
  80. ErrorOr<u8> parse_mv_class0_fr(bool mv_class0_bit)
  81. {
  82. m_mv_class0_bit = mv_class0_bit;
  83. return parse_tree<u8>(SyntaxElementType::MVClass0FR);
  84. }
  85. private:
  86. u8 calculate_segment_id_probability(u8 node);
  87. u8 calculate_skip_probability();
  88. u8 calculate_seg_id_predicted_probability();
  89. u8 calculate_is_inter_probability();
  90. u8 calculate_comp_mode_probability();
  91. u8 calculate_comp_ref_probability();
  92. u8 calculate_single_ref_p1_probability();
  93. u8 calculate_single_ref_p2_probability();
  94. u8 calculate_tx_size_probability(u8 node);
  95. u8 calculate_interp_filter_probability(u8 node);
  96. u8 calculate_token_probability(u8 node);
  97. u8 calculate_more_coefs_probability();
  98. Parser& m_decoder;
  99. // m_ctx is a member variable because it is required for syntax element counting (section 9.3.4)
  100. u8 m_ctx { 0 };
  101. // These are variables necessary for parsing tree data, but aren't useful otherwise, so they're
  102. // not stored in the Decoder itself.
  103. u8 m_idx { 0 };
  104. u8 m_idy { 0 };
  105. u8 m_band { 0 };
  106. u32 m_start_x { 0 };
  107. u32 m_start_y { 0 };
  108. u32 m_c { 0 };
  109. u32 m_plane { 0 };
  110. TXSize m_tx_size;
  111. u32 m_pos { 0 };
  112. u8 m_mv_component { 0 };
  113. // 0xFF indicates the value has not been set.
  114. // parse_mv_bit should be called to set this.
  115. u8 m_mv_bit { 0xFF };
  116. // 0xFF indicates the value has not been set.
  117. // parse_mv_class0_fr should be called to set this.
  118. u8 m_mv_class0_bit { 0xFF };
  119. };
  120. struct PartitionTreeContext {
  121. bool has_rows;
  122. bool has_columns;
  123. BlockSubsize block_subsize;
  124. u8 num_8x8;
  125. Vector<u8> const& above_partition_context;
  126. Vector<u8> const& left_partition_context;
  127. u32 row;
  128. u32 column;
  129. bool frame_is_intra;
  130. };
  131. }