Decoder.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/ByteBuffer.h>
  9. #include <AK/Error.h>
  10. #include <AK/NonnullOwnPtr.h>
  11. #include <AK/Queue.h>
  12. #include <AK/Span.h>
  13. #include <LibVideo/Color/CodingIndependentCodePoints.h>
  14. #include <LibVideo/DecoderError.h>
  15. #include <LibVideo/VideoDecoder.h>
  16. #include <LibVideo/VideoFrame.h>
  17. #include "Parser.h"
  18. namespace Video::VP9 {
  19. class Decoder : public VideoDecoder {
  20. friend class Parser;
  21. public:
  22. Decoder();
  23. ~Decoder() override { }
  24. /* (8.1) General */
  25. DecoderErrorOr<void> receive_sample(ReadonlyBytes) override;
  26. DecoderErrorOr<NonnullOwnPtr<VideoFrame>> get_decoded_frame() override;
  27. private:
  28. typedef i32 Intermediate;
  29. // Based on the maximum size resulting from num_4x4_blocks_wide_lookup.
  30. static constexpr size_t maximum_block_dimensions = 64ULL;
  31. static constexpr size_t maximum_block_size = maximum_block_dimensions * maximum_block_dimensions;
  32. // Based on the maximum for TXSize.
  33. static constexpr size_t maximum_transform_size = 32ULL * 32ULL;
  34. DecoderErrorOr<void> decode_frame(ReadonlyBytes);
  35. DecoderErrorOr<void> create_video_frame(FrameContext const&);
  36. DecoderErrorOr<void> allocate_buffers(FrameContext const&);
  37. Vector<u16>& get_output_buffer(u8 plane);
  38. /* (8.4) Probability Adaptation Process */
  39. u8 merge_prob(u8 pre_prob, u32 count_0, u32 count_1, u8 count_sat, u8 max_update_factor);
  40. u32 merge_probs(int const* tree, int index, u8* probs, u32* counts, u8 count_sat, u8 max_update_factor);
  41. DecoderErrorOr<void> adapt_coef_probs(FrameContext const&);
  42. DecoderErrorOr<void> adapt_non_coef_probs(FrameContext const&);
  43. void adapt_probs(int const* tree, u8* probs, u32* counts);
  44. u8 adapt_prob(u8 prob, u32 counts[2]);
  45. /* (8.5) Prediction Processes */
  46. // (8.5.1) Intra prediction process
  47. DecoderErrorOr<void> predict_intra(u8 plane, BlockContext const& block_context, u32 x, u32 y, bool have_left, bool have_above, bool not_on_right, TransformSize transform_size, u32 block_index);
  48. DecoderErrorOr<void> prepare_referenced_frame(Gfx::Size<u32> frame_size, u8 reference_frame_index);
  49. // (8.5.1) Inter prediction process
  50. DecoderErrorOr<void> predict_inter(u8 plane, BlockContext const& block_context, u32 x, u32 y, u32 width, u32 height, u32 block_index);
  51. // (8.5.2.1) Motion vector selection process
  52. MotionVector select_motion_vector(u8 plane, BlockContext const&, ReferenceIndex, u32 block_index);
  53. // (8.5.2.2) Motion vector clamping process
  54. MotionVector clamp_motion_vector(u8 plane, BlockContext const&, u32 block_row, u32 block_column, MotionVector vector);
  55. // From (8.5.1) Inter prediction process, steps 2-5
  56. DecoderErrorOr<void> predict_inter_block(u8 plane, BlockContext const&, ReferenceIndex, u32 block_row, u32 block_column, u32 x, u32 y, u32 width, u32 height, u32 block_index, Span<u16> block_buffer);
  57. /* (8.6) Reconstruction and Dequantization */
  58. // Returns the quantizer index for the current block
  59. static u8 get_base_quantizer_index(SegmentFeatureStatus alternative_quantizer_feature, bool should_use_absolute_segment_base_quantizer, u8 base_quantizer_index);
  60. // Returns the quantizer value for the dc coefficient for a particular plane
  61. static u16 get_dc_quantizer(u8 bit_depth, u8 base, i8 delta);
  62. // Returns the quantizer value for the ac coefficient for a particular plane
  63. static u16 get_ac_quantizer(u8 bit_depth, u8 base, i8 delta);
  64. // (8.6.2) Reconstruct process
  65. DecoderErrorOr<void> reconstruct(u8 plane, BlockContext const&, u32 transform_block_x, u32 transform_block_y, TransformSize transform_block_size, TransformSet);
  66. template<u8 log2_of_block_size>
  67. DecoderErrorOr<void> reconstruct_templated(u8 plane, BlockContext const&, u32 transform_block_x, u32 transform_block_y, TransformSet);
  68. // (8.7) Inverse transform process
  69. template<u8 log2_of_block_size>
  70. DecoderErrorOr<void> inverse_transform_2d(BlockContext const&, Span<Intermediate> dequantized, TransformSet);
  71. // (8.7.1) 1D Transforms
  72. // (8.7.1.1) Butterfly functions
  73. inline i32 cos64(u8 angle);
  74. inline i32 sin64(u8 angle);
  75. // The function B( a, b, angle, 0 ) performs a butterfly rotation.
  76. inline void butterfly_rotation_in_place(Span<Intermediate> data, size_t index_a, size_t index_b, u8 angle, bool flip);
  77. // The function H( a, b, 0 ) performs a Hadamard rotation.
  78. inline void hadamard_rotation_in_place(Span<Intermediate> data, size_t index_a, size_t index_b, bool flip);
  79. // The function SB( a, b, angle, 0 ) performs a butterfly rotation.
  80. // Spec defines the source as array T, and the destination array as S.
  81. template<typename S, typename D>
  82. inline void butterfly_rotation(Span<S> source, Span<D> destination, size_t index_a, size_t index_b, u8 angle, bool flip);
  83. // The function SH( a, b ) performs a Hadamard rotation and rounding.
  84. // Spec defines the source array as S, and the destination array as T.
  85. template<typename S, typename D>
  86. inline void hadamard_rotation(Span<S> source, Span<D> destination, size_t index_a, size_t index_b);
  87. // (8.7.1.10) This process does an in-place Walsh-Hadamard transform of the array T (of length 4).
  88. inline DecoderErrorOr<void> inverse_walsh_hadamard_transform(Span<Intermediate> data, u8 log2_of_block_size, u8 shift);
  89. // (8.7.1.2) Inverse DCT array permutation process
  90. template<u8 log2_of_block_size>
  91. inline DecoderErrorOr<void> inverse_discrete_cosine_transform_array_permutation(Span<Intermediate> data);
  92. // (8.7.1.3) Inverse DCT process
  93. template<u8 log2_of_block_size>
  94. inline DecoderErrorOr<void> inverse_discrete_cosine_transform(Span<Intermediate> data);
  95. // (8.7.1.4) This process performs the in-place permutation of the array T of length 2 n which is required as the first step of
  96. // the inverse ADST.
  97. template<u8 log2_of_block_size>
  98. inline void inverse_asymmetric_discrete_sine_transform_input_array_permutation(Span<Intermediate> data);
  99. // (8.7.1.5) This process performs the in-place permutation of the array T of length 2 n which is required before the final
  100. // step of the inverse ADST.
  101. template<u8 log2_of_block_size>
  102. inline void inverse_asymmetric_discrete_sine_transform_output_array_permutation(Span<Intermediate> data);
  103. // (8.7.1.6) This process does an in-place transform of the array T to perform an inverse ADST.
  104. inline void inverse_asymmetric_discrete_sine_transform_4(Span<Intermediate> data);
  105. // (8.7.1.7) This process does an in-place transform of the array T using a higher precision array S for intermediate
  106. // results.
  107. inline DecoderErrorOr<void> inverse_asymmetric_discrete_sine_transform_8(Span<Intermediate> data);
  108. // (8.7.1.8) This process does an in-place transform of the array T using a higher precision array S for intermediate
  109. // results.
  110. inline DecoderErrorOr<void> inverse_asymmetric_discrete_sine_transform_16(Span<Intermediate> data);
  111. // (8.7.1.9) This process performs an in-place inverse ADST process on the array T of size 2 n for 2 ≤ n ≤ 4.
  112. template<u8 log2_of_block_size>
  113. inline DecoderErrorOr<void> inverse_asymmetric_discrete_sine_transform(Span<Intermediate> data);
  114. /* (8.10) Reference Frame Update Process */
  115. DecoderErrorOr<void> update_reference_frames(FrameContext const&);
  116. NonnullOwnPtr<Parser> m_parser;
  117. Vector<u16> m_output_buffers[3];
  118. Queue<NonnullOwnPtr<VideoFrame>, 1> m_video_frame_queue;
  119. };
  120. }