h264.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-FileCopyrightText: Ryujinx Team and Contributors
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <span>
  5. #include <vector>
  6. #include "common/bit_field.h"
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/scratch_buffer.h"
  10. #include "video_core/host1x/nvdec_common.h"
  11. namespace Tegra {
  12. namespace Host1x {
  13. class Host1x;
  14. } // namespace Host1x
  15. namespace Decoder {
  16. class H264BitWriter {
  17. public:
  18. H264BitWriter();
  19. ~H264BitWriter();
  20. /// The following Write methods are based on clause 9.1 in the H.264 specification.
  21. /// WriteSe and WriteUe write in the Exp-Golomb-coded syntax
  22. void WriteU(s32 value, s32 value_sz);
  23. void WriteSe(s32 value);
  24. void WriteUe(u32 value);
  25. /// Finalize the bitstream
  26. void End();
  27. /// append a bit to the stream, equivalent value to the state parameter
  28. void WriteBit(bool state);
  29. /// Based on section 7.3.2.1.1.1 and Table 7-4 in the H.264 specification
  30. /// Writes the scaling matrices of the sream
  31. void WriteScalingList(Common::ScratchBuffer<u8>& scan, std::span<const u8> list, s32 start,
  32. s32 count);
  33. /// Return the bitstream as a vector.
  34. [[nodiscard]] std::vector<u8>& GetByteArray();
  35. [[nodiscard]] const std::vector<u8>& GetByteArray() const;
  36. private:
  37. void WriteBits(s32 value, s32 bit_count);
  38. void WriteExpGolombCodedInt(s32 value);
  39. void WriteExpGolombCodedUInt(u32 value);
  40. [[nodiscard]] s32 GetFreeBufferBits();
  41. void Flush();
  42. s32 buffer_size{8};
  43. s32 buffer{};
  44. s32 buffer_pos{};
  45. std::vector<u8> byte_array;
  46. };
  47. class H264 {
  48. public:
  49. explicit H264(Host1x::Host1x& host1x);
  50. ~H264();
  51. /// Compose the H264 frame for FFmpeg decoding
  52. [[nodiscard]] std::span<const u8> ComposeFrame(const Host1x::NvdecCommon::NvdecRegisters& state,
  53. size_t* out_configuration_size,
  54. bool is_first_frame = false);
  55. private:
  56. Common::ScratchBuffer<u8> frame;
  57. Common::ScratchBuffer<u8> scan;
  58. Host1x::Host1x& host1x;
  59. struct H264ParameterSet {
  60. s32 log2_max_pic_order_cnt_lsb_minus4; ///< 0x00
  61. s32 delta_pic_order_always_zero_flag; ///< 0x04
  62. s32 frame_mbs_only_flag; ///< 0x08
  63. u32 pic_width_in_mbs; ///< 0x0C
  64. u32 frame_height_in_map_units; ///< 0x10
  65. union { ///< 0x14
  66. BitField<0, 2, u32> tile_format;
  67. BitField<2, 3, u32> gob_height;
  68. };
  69. u32 entropy_coding_mode_flag; ///< 0x18
  70. s32 pic_order_present_flag; ///< 0x1C
  71. s32 num_refidx_l0_default_active; ///< 0x20
  72. s32 num_refidx_l1_default_active; ///< 0x24
  73. s32 deblocking_filter_control_present_flag; ///< 0x28
  74. s32 redundant_pic_cnt_present_flag; ///< 0x2C
  75. u32 transform_8x8_mode_flag; ///< 0x30
  76. u32 pitch_luma; ///< 0x34
  77. u32 pitch_chroma; ///< 0x38
  78. u32 luma_top_offset; ///< 0x3C
  79. u32 luma_bot_offset; ///< 0x40
  80. u32 luma_frame_offset; ///< 0x44
  81. u32 chroma_top_offset; ///< 0x48
  82. u32 chroma_bot_offset; ///< 0x4C
  83. u32 chroma_frame_offset; ///< 0x50
  84. u32 hist_buffer_size; ///< 0x54
  85. union { ///< 0x58
  86. union {
  87. BitField<0, 1, u64> mbaff_frame;
  88. BitField<1, 1, u64> direct_8x8_inference;
  89. BitField<2, 1, u64> weighted_pred;
  90. BitField<3, 1, u64> constrained_intra_pred;
  91. BitField<4, 1, u64> ref_pic;
  92. BitField<5, 1, u64> field_pic;
  93. BitField<6, 1, u64> bottom_field;
  94. BitField<7, 1, u64> second_field;
  95. } flags;
  96. BitField<8, 4, u64> log2_max_frame_num_minus4;
  97. BitField<12, 2, u64> chroma_format_idc;
  98. BitField<14, 2, u64> pic_order_cnt_type;
  99. BitField<16, 6, s64> pic_init_qp_minus26;
  100. BitField<22, 5, s64> chroma_qp_index_offset;
  101. BitField<27, 5, s64> second_chroma_qp_index_offset;
  102. BitField<32, 2, u64> weighted_bipred_idc;
  103. BitField<34, 7, u64> curr_pic_idx;
  104. BitField<41, 5, u64> curr_col_idx;
  105. BitField<46, 16, u64> frame_number;
  106. BitField<62, 1, u64> frame_surfaces;
  107. BitField<63, 1, u64> output_memory_layout;
  108. };
  109. };
  110. static_assert(sizeof(H264ParameterSet) == 0x60, "H264ParameterSet is an invalid size");
  111. struct H264DecoderContext {
  112. INSERT_PADDING_WORDS_NOINIT(18); ///< 0x0000
  113. u32 stream_len; ///< 0x0048
  114. INSERT_PADDING_WORDS_NOINIT(3); ///< 0x004C
  115. H264ParameterSet h264_parameter_set; ///< 0x0058
  116. INSERT_PADDING_WORDS_NOINIT(66); ///< 0x00B8
  117. std::array<u8, 0x60> weight_scale; ///< 0x01C0
  118. std::array<u8, 0x80> weight_scale_8x8; ///< 0x0220
  119. };
  120. static_assert(sizeof(H264DecoderContext) == 0x2A0, "H264DecoderContext is an invalid size");
  121. #define ASSERT_POSITION(field_name, position) \
  122. static_assert(offsetof(H264ParameterSet, field_name) == position, \
  123. "Field " #field_name " has invalid position")
  124. ASSERT_POSITION(log2_max_pic_order_cnt_lsb_minus4, 0x00);
  125. ASSERT_POSITION(delta_pic_order_always_zero_flag, 0x04);
  126. ASSERT_POSITION(frame_mbs_only_flag, 0x08);
  127. ASSERT_POSITION(pic_width_in_mbs, 0x0C);
  128. ASSERT_POSITION(frame_height_in_map_units, 0x10);
  129. ASSERT_POSITION(tile_format, 0x14);
  130. ASSERT_POSITION(entropy_coding_mode_flag, 0x18);
  131. ASSERT_POSITION(pic_order_present_flag, 0x1C);
  132. ASSERT_POSITION(num_refidx_l0_default_active, 0x20);
  133. ASSERT_POSITION(num_refidx_l1_default_active, 0x24);
  134. ASSERT_POSITION(deblocking_filter_control_present_flag, 0x28);
  135. ASSERT_POSITION(redundant_pic_cnt_present_flag, 0x2C);
  136. ASSERT_POSITION(transform_8x8_mode_flag, 0x30);
  137. ASSERT_POSITION(pitch_luma, 0x34);
  138. ASSERT_POSITION(pitch_chroma, 0x38);
  139. ASSERT_POSITION(luma_top_offset, 0x3C);
  140. ASSERT_POSITION(luma_bot_offset, 0x40);
  141. ASSERT_POSITION(luma_frame_offset, 0x44);
  142. ASSERT_POSITION(chroma_top_offset, 0x48);
  143. ASSERT_POSITION(chroma_bot_offset, 0x4C);
  144. ASSERT_POSITION(chroma_frame_offset, 0x50);
  145. ASSERT_POSITION(hist_buffer_size, 0x54);
  146. ASSERT_POSITION(flags, 0x58);
  147. #undef ASSERT_POSITION
  148. #define ASSERT_POSITION(field_name, position) \
  149. static_assert(offsetof(H264DecoderContext, field_name) == position, \
  150. "Field " #field_name " has invalid position")
  151. ASSERT_POSITION(stream_len, 0x48);
  152. ASSERT_POSITION(h264_parameter_set, 0x58);
  153. ASSERT_POSITION(weight_scale, 0x1C0);
  154. #undef ASSERT_POSITION
  155. };
  156. } // namespace Decoder
  157. } // namespace Tegra