Parser.h 7.7 KB

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