Parser.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/Array.h>
  9. #include <AK/OwnPtr.h>
  10. #include <AK/Span.h>
  11. #include <LibGfx/Size.h>
  12. #include <LibVideo/Color/CodingIndependentCodePoints.h>
  13. #include <LibVideo/DecoderError.h>
  14. #include "BitStream.h"
  15. #include "ContextStorage.h"
  16. #include "LookupTables.h"
  17. #include "MotionVector.h"
  18. #include "ProbabilityTables.h"
  19. #include "SyntaxElementCounter.h"
  20. #include "TreeParser.h"
  21. namespace Video::VP9 {
  22. class Decoder;
  23. struct FrameContext;
  24. struct TileContext;
  25. struct BlockContext;
  26. struct MotionVectorCandidate;
  27. class Parser {
  28. friend class TreeParser;
  29. friend class Decoder;
  30. public:
  31. explicit Parser(Decoder&);
  32. ~Parser();
  33. DecoderErrorOr<FrameContext> parse_frame(ReadonlyBytes);
  34. private:
  35. /* Annex B: Superframes are a method of storing multiple coded frames into a single chunk
  36. * See also section 5.26. */
  37. Vector<size_t> parse_superframe_sizes(ReadonlyBytes);
  38. DecoderErrorOr<ColorRange> read_color_range();
  39. /* (6.1) Frame Syntax */
  40. bool trailing_bits();
  41. DecoderErrorOr<void> refresh_probs(FrameContext const&);
  42. /* (6.2) Uncompressed Header Syntax */
  43. DecoderErrorOr<FrameContext> uncompressed_header();
  44. DecoderErrorOr<void> frame_sync_code();
  45. DecoderErrorOr<ColorConfig> parse_color_config(FrameContext const&);
  46. DecoderErrorOr<Gfx::Size<u32>> parse_frame_size();
  47. DecoderErrorOr<Gfx::Size<u32>> parse_frame_size_with_refs(Array<u8, 3> const& reference_indices);
  48. DecoderErrorOr<Gfx::Size<u32>> parse_render_size(Gfx::Size<u32> frame_size);
  49. DecoderErrorOr<void> compute_image_size(FrameContext&);
  50. DecoderErrorOr<InterpolationFilter> read_interpolation_filter();
  51. DecoderErrorOr<void> loop_filter_params(FrameContext&);
  52. DecoderErrorOr<void> quantization_params(FrameContext&);
  53. DecoderErrorOr<i8> read_delta_q();
  54. DecoderErrorOr<void> segmentation_params(FrameContext&);
  55. DecoderErrorOr<u8> read_prob();
  56. DecoderErrorOr<void> parse_tile_counts(FrameContext&);
  57. void setup_past_independence();
  58. /* (6.3) Compressed Header Syntax */
  59. DecoderErrorOr<void> compressed_header(FrameContext&);
  60. DecoderErrorOr<TransformMode> read_tx_mode(FrameContext const&);
  61. DecoderErrorOr<void> tx_mode_probs();
  62. DecoderErrorOr<u8> diff_update_prob(u8 prob);
  63. DecoderErrorOr<u8> decode_term_subexp();
  64. u8 inv_remap_prob(u8 delta_prob, u8 prob);
  65. u8 inv_recenter_nonneg(u8 v, u8 m);
  66. DecoderErrorOr<void> read_coef_probs(TransformMode);
  67. DecoderErrorOr<void> read_skip_prob();
  68. DecoderErrorOr<void> read_inter_mode_probs();
  69. DecoderErrorOr<void> read_interp_filter_probs();
  70. DecoderErrorOr<void> read_is_inter_probs();
  71. DecoderErrorOr<void> frame_reference_mode(FrameContext&);
  72. DecoderErrorOr<void> frame_reference_mode_probs(FrameContext const&);
  73. DecoderErrorOr<void> read_y_mode_probs();
  74. DecoderErrorOr<void> read_partition_probs();
  75. DecoderErrorOr<void> mv_probs(FrameContext const&);
  76. DecoderErrorOr<u8> update_mv_prob(u8 prob);
  77. /* (6.4) Decode Tiles Syntax */
  78. DecoderErrorOr<void> decode_tiles(FrameContext&);
  79. DecoderErrorOr<void> decode_tile(TileContext&);
  80. void clear_left_context(TileContext&);
  81. DecoderErrorOr<void> decode_partition(TileContext&, u32 row, u32 column, BlockSubsize subsize);
  82. DecoderErrorOr<void> decode_block(TileContext&, u32 row, u32 column, BlockSubsize subsize);
  83. DecoderErrorOr<void> mode_info(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  84. DecoderErrorOr<void> intra_frame_mode_info(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  85. DecoderErrorOr<void> set_intra_segment_id(BlockContext&);
  86. DecoderErrorOr<bool> read_should_skip_residuals(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  87. static bool seg_feature_active(BlockContext const&, u8 feature);
  88. DecoderErrorOr<TransformSize> read_tx_size(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context, bool allow_select);
  89. DecoderErrorOr<void> inter_frame_mode_info(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  90. DecoderErrorOr<void> set_inter_segment_id(BlockContext&);
  91. u8 get_segment_id(BlockContext const&);
  92. DecoderErrorOr<bool> read_is_inter(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  93. DecoderErrorOr<void> intra_block_mode_info(BlockContext&);
  94. DecoderErrorOr<void> inter_block_mode_info(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  95. DecoderErrorOr<void> read_ref_frames(BlockContext&, FrameBlockContext above_context, FrameBlockContext left_context);
  96. DecoderErrorOr<MotionVectorPair> get_motion_vector(BlockContext const&, BlockMotionVectorCandidates const&);
  97. DecoderErrorOr<MotionVector> read_motion_vector(BlockContext const&, BlockMotionVectorCandidates const&, ReferenceIndex);
  98. DecoderErrorOr<i32> read_single_motion_vector_component(u8 component, bool use_high_precision);
  99. DecoderErrorOr<bool> residual(BlockContext&, bool has_block_above, bool has_block_left);
  100. DecoderErrorOr<bool> tokens(BlockContext&, size_t plane, u32 x, u32 y, TransformSize, TransformSet, Array<u8, 1024> token_cache);
  101. DecoderErrorOr<i32> read_coef(u8 bit_depth, Token token);
  102. /* (6.5) Motion Vector Prediction */
  103. MotionVectorPair find_reference_motion_vectors(BlockContext&, ReferenceFrameType, i32 block);
  104. void select_best_sub_block_reference_motion_vectors(BlockContext&, BlockMotionVectorCandidates&, i32 block, ReferenceIndex);
  105. size_t get_image_index(FrameContext const&, u32 row, u32 column) const;
  106. MotionVectorCandidate get_motion_vector_from_current_or_previous_frame(BlockContext const&, MotionVector candidate_vector, ReferenceIndex, bool use_prev);
  107. void add_motion_vector_if_reference_frame_type_is_same(BlockContext const&, MotionVector candidate_vector, ReferenceFrameType ref_frame, Vector<MotionVector, 2>& list, bool use_prev);
  108. void add_motion_vector_if_reference_frame_type_is_different(BlockContext const&, MotionVector candidate_vector, ReferenceFrameType ref_frame, Vector<MotionVector, 2>& list, bool use_prev);
  109. Gfx::Point<size_t> get_decoded_point_for_plane(FrameContext const&, u32 row, u32 column, u8 plane);
  110. Gfx::Size<size_t> get_decoded_size_for_plane(FrameContext const&, u8 plane);
  111. bool m_is_first_compute_image_size_invoke { true };
  112. Gfx::Size<u32> m_previous_frame_size { 0, 0 };
  113. bool m_previous_show_frame { false };
  114. ColorConfig m_previous_color_config;
  115. FrameType m_previous_frame_type { FrameType::KeyFrame };
  116. Array<i8, MAX_REF_FRAMES> m_previous_loop_filter_ref_deltas;
  117. Array<i8, 2> m_previous_loop_filter_mode_deltas;
  118. bool m_previous_should_use_absolute_segment_base_quantizer;
  119. Array<Array<SegmentFeature, SEG_LVL_MAX>, MAX_SEGMENTS> m_previous_segmentation_features;
  120. ReferenceFrame m_reference_frames[NUM_REF_FRAMES];
  121. Vector2D<FrameBlockContext> m_reusable_frame_block_contexts;
  122. Vector2D<PersistentBlockContext> m_previous_block_contexts;
  123. OwnPtr<BitStream> m_bit_stream;
  124. OwnPtr<ProbabilityTables> m_probability_tables;
  125. OwnPtr<SyntaxElementCounter> m_syntax_element_counter;
  126. Decoder& m_decoder;
  127. };
  128. }