h264.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-FileCopyrightText: Ryujinx Team and Contributors
  2. // SPDX-License-Identifier: MIT
  3. #include <array>
  4. #include <bit>
  5. #include "common/settings.h"
  6. #include "video_core/host1x/codecs/h264.h"
  7. #include "video_core/host1x/host1x.h"
  8. #include "video_core/memory_manager.h"
  9. namespace Tegra::Decoder {
  10. namespace {
  11. // ZigZag LUTs from libavcodec.
  12. constexpr std::array<u8, 64> zig_zag_direct{
  13. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48,
  14. 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23,
  15. 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
  16. };
  17. constexpr std::array<u8, 16> zig_zag_scan{
  18. 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4,
  19. 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4, 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
  20. };
  21. } // Anonymous namespace
  22. H264::H264(Host1x::Host1x& host1x_) : host1x{host1x_} {}
  23. H264::~H264() = default;
  24. const std::vector<u8>& H264::ComposeFrame(const Host1x::NvdecCommon::NvdecRegisters& state,
  25. bool is_first_frame) {
  26. H264DecoderContext context;
  27. host1x.MemoryManager().ReadBlock(state.picture_info_offset, &context,
  28. sizeof(H264DecoderContext));
  29. const s64 frame_number = context.h264_parameter_set.frame_number.Value();
  30. if (!is_first_frame && frame_number != 0) {
  31. frame.resize(context.stream_len);
  32. host1x.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.data(), frame.size());
  33. return frame;
  34. }
  35. // Encode header
  36. H264BitWriter writer{};
  37. writer.WriteU(1, 24);
  38. writer.WriteU(0, 1);
  39. writer.WriteU(3, 2);
  40. writer.WriteU(7, 5);
  41. writer.WriteU(100, 8);
  42. writer.WriteU(0, 8);
  43. writer.WriteU(31, 8);
  44. writer.WriteUe(0);
  45. const u32 chroma_format_idc =
  46. static_cast<u32>(context.h264_parameter_set.chroma_format_idc.Value());
  47. writer.WriteUe(chroma_format_idc);
  48. if (chroma_format_idc == 3) {
  49. writer.WriteBit(false);
  50. }
  51. writer.WriteUe(0);
  52. writer.WriteUe(0);
  53. writer.WriteBit(false); // QpprimeYZeroTransformBypassFlag
  54. writer.WriteBit(false); // Scaling matrix present flag
  55. writer.WriteUe(static_cast<u32>(context.h264_parameter_set.log2_max_frame_num_minus4.Value()));
  56. const auto order_cnt_type =
  57. static_cast<u32>(context.h264_parameter_set.pic_order_cnt_type.Value());
  58. writer.WriteUe(order_cnt_type);
  59. if (order_cnt_type == 0) {
  60. writer.WriteUe(context.h264_parameter_set.log2_max_pic_order_cnt_lsb_minus4);
  61. } else if (order_cnt_type == 1) {
  62. writer.WriteBit(context.h264_parameter_set.delta_pic_order_always_zero_flag != 0);
  63. writer.WriteSe(0);
  64. writer.WriteSe(0);
  65. writer.WriteUe(0);
  66. }
  67. const s32 pic_height = context.h264_parameter_set.frame_height_in_map_units /
  68. (context.h264_parameter_set.frame_mbs_only_flag ? 1 : 2);
  69. // TODO (ameerj): Where do we get this number, it seems to be particular for each stream
  70. const auto nvdec_decoding = Settings::values.nvdec_emulation.GetValue();
  71. const bool uses_gpu_decoding = nvdec_decoding == Settings::NvdecEmulation::GPU;
  72. const u32 max_num_ref_frames = uses_gpu_decoding ? 6u : 16u;
  73. writer.WriteUe(max_num_ref_frames);
  74. writer.WriteBit(false);
  75. writer.WriteUe(context.h264_parameter_set.pic_width_in_mbs - 1);
  76. writer.WriteUe(pic_height - 1);
  77. writer.WriteBit(context.h264_parameter_set.frame_mbs_only_flag != 0);
  78. if (!context.h264_parameter_set.frame_mbs_only_flag) {
  79. writer.WriteBit(context.h264_parameter_set.flags.mbaff_frame.Value() != 0);
  80. }
  81. writer.WriteBit(context.h264_parameter_set.flags.direct_8x8_inference.Value() != 0);
  82. writer.WriteBit(false); // Frame cropping flag
  83. writer.WriteBit(false); // VUI parameter present flag
  84. writer.End();
  85. // H264 PPS
  86. writer.WriteU(1, 24);
  87. writer.WriteU(0, 1);
  88. writer.WriteU(3, 2);
  89. writer.WriteU(8, 5);
  90. writer.WriteUe(0);
  91. writer.WriteUe(0);
  92. writer.WriteBit(context.h264_parameter_set.entropy_coding_mode_flag != 0);
  93. writer.WriteBit(context.h264_parameter_set.pic_order_present_flag != 0);
  94. writer.WriteUe(0);
  95. writer.WriteUe(context.h264_parameter_set.num_refidx_l0_default_active);
  96. writer.WriteUe(context.h264_parameter_set.num_refidx_l1_default_active);
  97. writer.WriteBit(context.h264_parameter_set.flags.weighted_pred.Value() != 0);
  98. writer.WriteU(static_cast<s32>(context.h264_parameter_set.weighted_bipred_idc.Value()), 2);
  99. s32 pic_init_qp = static_cast<s32>(context.h264_parameter_set.pic_init_qp_minus26.Value());
  100. writer.WriteSe(pic_init_qp);
  101. writer.WriteSe(0);
  102. s32 chroma_qp_index_offset =
  103. static_cast<s32>(context.h264_parameter_set.chroma_qp_index_offset.Value());
  104. writer.WriteSe(chroma_qp_index_offset);
  105. writer.WriteBit(context.h264_parameter_set.deblocking_filter_control_present_flag != 0);
  106. writer.WriteBit(context.h264_parameter_set.flags.constrained_intra_pred.Value() != 0);
  107. writer.WriteBit(context.h264_parameter_set.redundant_pic_cnt_present_flag != 0);
  108. writer.WriteBit(context.h264_parameter_set.transform_8x8_mode_flag != 0);
  109. writer.WriteBit(true); // pic_scaling_matrix_present_flag
  110. for (s32 index = 0; index < 6; index++) {
  111. writer.WriteBit(true);
  112. std::span<const u8> matrix{context.weight_scale};
  113. writer.WriteScalingList(matrix, index * 16, 16);
  114. }
  115. if (context.h264_parameter_set.transform_8x8_mode_flag) {
  116. for (s32 index = 0; index < 2; index++) {
  117. writer.WriteBit(true);
  118. std::span<const u8> matrix{context.weight_scale_8x8};
  119. writer.WriteScalingList(matrix, index * 64, 64);
  120. }
  121. }
  122. s32 chroma_qp_index_offset2 =
  123. static_cast<s32>(context.h264_parameter_set.second_chroma_qp_index_offset.Value());
  124. writer.WriteSe(chroma_qp_index_offset2);
  125. writer.End();
  126. const auto& encoded_header = writer.GetByteArray();
  127. frame.resize(encoded_header.size() + context.stream_len);
  128. std::memcpy(frame.data(), encoded_header.data(), encoded_header.size());
  129. host1x.MemoryManager().ReadBlock(state.frame_bitstream_offset,
  130. frame.data() + encoded_header.size(), context.stream_len);
  131. return frame;
  132. }
  133. H264BitWriter::H264BitWriter() = default;
  134. H264BitWriter::~H264BitWriter() = default;
  135. void H264BitWriter::WriteU(s32 value, s32 value_sz) {
  136. WriteBits(value, value_sz);
  137. }
  138. void H264BitWriter::WriteSe(s32 value) {
  139. WriteExpGolombCodedInt(value);
  140. }
  141. void H264BitWriter::WriteUe(u32 value) {
  142. WriteExpGolombCodedUInt(value);
  143. }
  144. void H264BitWriter::End() {
  145. WriteBit(true);
  146. Flush();
  147. }
  148. void H264BitWriter::WriteBit(bool state) {
  149. WriteBits(state ? 1 : 0, 1);
  150. }
  151. void H264BitWriter::WriteScalingList(std::span<const u8> list, s32 start, s32 count) {
  152. std::vector<u8> scan(count);
  153. if (count == 16) {
  154. std::memcpy(scan.data(), zig_zag_scan.data(), scan.size());
  155. } else {
  156. std::memcpy(scan.data(), zig_zag_direct.data(), scan.size());
  157. }
  158. u8 last_scale = 8;
  159. for (s32 index = 0; index < count; index++) {
  160. const u8 value = list[start + scan[index]];
  161. const s32 delta_scale = static_cast<s32>(value - last_scale);
  162. WriteSe(delta_scale);
  163. last_scale = value;
  164. }
  165. }
  166. std::vector<u8>& H264BitWriter::GetByteArray() {
  167. return byte_array;
  168. }
  169. const std::vector<u8>& H264BitWriter::GetByteArray() const {
  170. return byte_array;
  171. }
  172. void H264BitWriter::WriteBits(s32 value, s32 bit_count) {
  173. s32 value_pos = 0;
  174. s32 remaining = bit_count;
  175. while (remaining > 0) {
  176. s32 copy_size = remaining;
  177. const s32 free_bits = GetFreeBufferBits();
  178. if (copy_size > free_bits) {
  179. copy_size = free_bits;
  180. }
  181. const s32 mask = (1 << copy_size) - 1;
  182. const s32 src_shift = (bit_count - value_pos) - copy_size;
  183. const s32 dst_shift = (buffer_size - buffer_pos) - copy_size;
  184. buffer |= ((value >> src_shift) & mask) << dst_shift;
  185. value_pos += copy_size;
  186. buffer_pos += copy_size;
  187. remaining -= copy_size;
  188. }
  189. }
  190. void H264BitWriter::WriteExpGolombCodedInt(s32 value) {
  191. const s32 sign = value <= 0 ? 0 : 1;
  192. if (value < 0) {
  193. value = -value;
  194. }
  195. value = (value << 1) - sign;
  196. WriteExpGolombCodedUInt(value);
  197. }
  198. void H264BitWriter::WriteExpGolombCodedUInt(u32 value) {
  199. const s32 size = 32 - std::countl_zero(value + 1);
  200. WriteBits(1, size);
  201. value -= (1U << (size - 1)) - 1;
  202. WriteBits(static_cast<s32>(value), size - 1);
  203. }
  204. s32 H264BitWriter::GetFreeBufferBits() {
  205. if (buffer_pos == buffer_size) {
  206. Flush();
  207. }
  208. return buffer_size - buffer_pos;
  209. }
  210. void H264BitWriter::Flush() {
  211. if (buffer_pos == 0) {
  212. return;
  213. }
  214. byte_array.push_back(static_cast<u8>(buffer));
  215. buffer = 0;
  216. buffer_pos = 0;
  217. }
  218. } // namespace Tegra::Decoder