vp9.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <unordered_map>
  6. #include <vector>
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/stream.h"
  10. #include "video_core/command_classes/codecs/vp9_types.h"
  11. #include "video_core/command_classes/nvdec_common.h"
  12. namespace Tegra {
  13. class GPU;
  14. enum class FrameType { KeyFrame = 0, InterFrame = 1 };
  15. namespace Decoder {
  16. /// The VpxRangeEncoder, and VpxBitStreamWriter classes are used to compose the
  17. /// VP9 header bitstreams.
  18. class VpxRangeEncoder {
  19. public:
  20. VpxRangeEncoder();
  21. ~VpxRangeEncoder();
  22. /// Writes the rightmost value_size bits from value into the stream
  23. void Write(s32 value, s32 value_size);
  24. /// Writes a single bit with half probability
  25. void Write(bool bit);
  26. /// Writes a bit to the base_stream encoded with probability
  27. void Write(bool bit, s32 probability);
  28. /// Signal the end of the bitstream
  29. void End();
  30. std::vector<u8>& GetBuffer() {
  31. return base_stream.GetBuffer();
  32. }
  33. const std::vector<u8>& GetBuffer() const {
  34. return base_stream.GetBuffer();
  35. }
  36. private:
  37. u8 PeekByte();
  38. Common::Stream base_stream{};
  39. u32 low_value{};
  40. u32 range{0xff};
  41. s32 count{-24};
  42. s32 half_probability{128};
  43. static constexpr std::array<s32, 256> norm_lut{
  44. 0, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  45. 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  46. 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  53. };
  54. };
  55. class VpxBitStreamWriter {
  56. public:
  57. VpxBitStreamWriter();
  58. ~VpxBitStreamWriter();
  59. /// Write an unsigned integer value
  60. void WriteU(u32 value, u32 value_size);
  61. /// Write a signed integer value
  62. void WriteS(s32 value, u32 value_size);
  63. /// Based on 6.2.10 of VP9 Spec, writes a delta coded value
  64. void WriteDeltaQ(u32 value);
  65. /// Write a single bit.
  66. void WriteBit(bool state);
  67. /// Pushes current buffer into buffer_array, resets buffer
  68. void Flush();
  69. /// Returns byte_array
  70. std::vector<u8>& GetByteArray();
  71. /// Returns const byte_array
  72. const std::vector<u8>& GetByteArray() const;
  73. private:
  74. /// Write bit_count bits from value into buffer
  75. void WriteBits(u32 value, u32 bit_count);
  76. /// Gets next available position in buffer, invokes Flush() if buffer is full
  77. s32 GetFreeBufferBits();
  78. s32 buffer_size{8};
  79. s32 buffer{};
  80. s32 buffer_pos{};
  81. std::vector<u8> byte_array;
  82. };
  83. class VP9 {
  84. public:
  85. explicit VP9(GPU& gpu);
  86. ~VP9();
  87. /// Composes the VP9 frame from the GPU state information. Based on the official VP9 spec
  88. /// documentation
  89. std::vector<u8>& ComposeFrameHeader(NvdecCommon::NvdecRegisters& state);
  90. /// Returns true if the most recent frame was a hidden frame.
  91. bool WasFrameHidden() const {
  92. return hidden;
  93. }
  94. private:
  95. /// Generates compressed header probability updates in the bitstream writer
  96. template <typename T, std::size_t N>
  97. void WriteProbabilityUpdate(VpxRangeEncoder& writer, const std::array<T, N>& new_prob,
  98. const std::array<T, N>& old_prob);
  99. /// Generates compressed header probability updates in the bitstream writer
  100. /// If probs are not equal, WriteProbabilityDelta is invoked
  101. void WriteProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob);
  102. /// Generates compressed header probability deltas in the bitstream writer
  103. void WriteProbabilityDelta(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob);
  104. /// Adjusts old_prob depending on new_prob. Based on section 6.3.5 of VP9 Specification
  105. s32 RemapProbability(s32 new_prob, s32 old_prob);
  106. /// Recenters probability. Based on section 6.3.6 of VP9 Specification
  107. s32 RecenterNonNeg(s32 new_prob, s32 old_prob);
  108. /// Inverse of 6.3.4 Decode term subexp
  109. void EncodeTermSubExp(VpxRangeEncoder& writer, s32 value);
  110. /// Writes if the value is less than the test value
  111. bool WriteLessThan(VpxRangeEncoder& writer, s32 value, s32 test);
  112. /// Writes probability updates for the Coef probabilities
  113. void WriteCoefProbabilityUpdate(VpxRangeEncoder& writer, s32 tx_mode,
  114. const std::array<u8, 2304>& new_prob,
  115. const std::array<u8, 2304>& old_prob);
  116. /// Write probabilities for 4-byte aligned structures
  117. template <typename T, std::size_t N>
  118. void WriteProbabilityUpdateAligned4(VpxRangeEncoder& writer, const std::array<T, N>& new_prob,
  119. const std::array<T, N>& old_prob);
  120. /// Write motion vector probability updates. 6.3.17 in the spec
  121. void WriteMvProbabilityUpdate(VpxRangeEncoder& writer, u8 new_prob, u8 old_prob);
  122. /// 6.2.14 Tile size calculation
  123. s32 CalcMinLog2TileCols(s32 frame_width);
  124. s32 CalcMaxLog2TileCols(s32 frame_width);
  125. /// Returns VP9 information from NVDEC provided offset and size
  126. Vp9PictureInfo GetVp9PictureInfo(const NvdecCommon::NvdecRegisters& state);
  127. /// Read and convert NVDEC provided entropy probs to Vp9EntropyProbs struct
  128. void InsertEntropy(u64 offset, Vp9EntropyProbs& dst);
  129. /// Returns frame to be decoded after buffering
  130. Vp9FrameContainer GetCurrentFrame(const NvdecCommon::NvdecRegisters& state);
  131. /// Use NVDEC providied information to compose the headers for the current frame
  132. std::vector<u8> ComposeCompressedHeader();
  133. VpxBitStreamWriter ComposeUncompressedHeader();
  134. GPU& gpu;
  135. std::vector<u8> frame;
  136. std::array<s8, 4> loop_filter_ref_deltas{};
  137. std::array<s8, 2> loop_filter_mode_deltas{};
  138. bool hidden;
  139. s64 current_frame_number = -2; // since we buffer 2 frames
  140. s32 grace_period = 6; // frame offsets need to stabilize
  141. std::array<FrameContexts, 4> frame_ctxs{};
  142. Vp9FrameContainer next_frame{};
  143. Vp9FrameContainer next_next_frame{};
  144. bool swap_next_golden{};
  145. Vp9PictureInfo current_frame_info{};
  146. Vp9EntropyProbs prev_frame_probs{};
  147. s32 diff_update_probability = 252;
  148. s32 frame_sync_code = 0x498342;
  149. static constexpr std::array<s32, 254> map_lut = {
  150. 20, 21, 22, 23, 24, 25, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  151. 36, 37, 1, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 2, 50,
  152. 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 3, 62, 63, 64, 65, 66,
  153. 67, 68, 69, 70, 71, 72, 73, 4, 74, 75, 76, 77, 78, 79, 80, 81, 82,
  154. 83, 84, 85, 5, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 6,
  155. 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 7, 110, 111, 112, 113,
  156. 114, 115, 116, 117, 118, 119, 120, 121, 8, 122, 123, 124, 125, 126, 127, 128, 129,
  157. 130, 131, 132, 133, 9, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145,
  158. 10, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 11, 158, 159, 160,
  159. 161, 162, 163, 164, 165, 166, 167, 168, 169, 12, 170, 171, 172, 173, 174, 175, 176,
  160. 177, 178, 179, 180, 181, 13, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192,
  161. 193, 14, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 15, 206, 207,
  162. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 16, 218, 219, 220, 221, 222, 223,
  163. 224, 225, 226, 227, 228, 229, 17, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  164. 240, 241, 18, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 19,
  165. };
  166. };
  167. } // namespace Decoder
  168. } // namespace Tegra