h264.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. // MIT License
  2. //
  3. // Copyright (c) Ryujinx Team and Contributors
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. // associated documentation files (the "Software"), to deal in the Software without restriction,
  7. // including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in all copies or
  12. // substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  17. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. #include <array>
  21. #include <bit>
  22. #include "video_core/command_classes/codecs/h264.h"
  23. #include "video_core/gpu.h"
  24. #include "video_core/memory_manager.h"
  25. namespace Tegra::Decoder {
  26. namespace {
  27. // ZigZag LUTs from libavcodec.
  28. constexpr std::array<u8, 64> zig_zag_direct{
  29. 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48,
  30. 41, 34, 27, 20, 13, 6, 7, 14, 21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23,
  31. 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
  32. };
  33. constexpr std::array<u8, 16> zig_zag_scan{
  34. 0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4, 1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4,
  35. 1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4, 3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
  36. };
  37. } // Anonymous namespace
  38. H264::H264(GPU& gpu_) : gpu(gpu_) {}
  39. H264::~H264() = default;
  40. const std::vector<u8>& H264::ComposeFrameHeader(const NvdecCommon::NvdecRegisters& state,
  41. bool is_first_frame) {
  42. H264DecoderContext context;
  43. gpu.MemoryManager().ReadBlock(state.picture_info_offset, &context, sizeof(H264DecoderContext));
  44. const s64 frame_number = context.h264_parameter_set.frame_number.Value();
  45. if (!is_first_frame && frame_number != 0) {
  46. frame.resize(context.stream_len);
  47. gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.data(), frame.size());
  48. return frame;
  49. }
  50. // Encode header
  51. H264BitWriter writer{};
  52. writer.WriteU(1, 24);
  53. writer.WriteU(0, 1);
  54. writer.WriteU(3, 2);
  55. writer.WriteU(7, 5);
  56. writer.WriteU(100, 8);
  57. writer.WriteU(0, 8);
  58. writer.WriteU(31, 8);
  59. writer.WriteUe(0);
  60. const u32 chroma_format_idc =
  61. static_cast<u32>(context.h264_parameter_set.chroma_format_idc.Value());
  62. writer.WriteUe(chroma_format_idc);
  63. if (chroma_format_idc == 3) {
  64. writer.WriteBit(false);
  65. }
  66. writer.WriteUe(0);
  67. writer.WriteUe(0);
  68. writer.WriteBit(false); // QpprimeYZeroTransformBypassFlag
  69. writer.WriteBit(false); // Scaling matrix present flag
  70. writer.WriteUe(static_cast<u32>(context.h264_parameter_set.log2_max_frame_num_minus4.Value()));
  71. const auto order_cnt_type =
  72. static_cast<u32>(context.h264_parameter_set.pic_order_cnt_type.Value());
  73. writer.WriteUe(order_cnt_type);
  74. if (order_cnt_type == 0) {
  75. writer.WriteUe(context.h264_parameter_set.log2_max_pic_order_cnt_lsb_minus4);
  76. } else if (order_cnt_type == 1) {
  77. writer.WriteBit(context.h264_parameter_set.delta_pic_order_always_zero_flag != 0);
  78. writer.WriteSe(0);
  79. writer.WriteSe(0);
  80. writer.WriteUe(0);
  81. }
  82. const s32 pic_height = context.h264_parameter_set.frame_height_in_map_units /
  83. (context.h264_parameter_set.frame_mbs_only_flag ? 1 : 2);
  84. // TODO (ameerj): Where do we get this number, it seems to be particular for each stream
  85. writer.WriteUe(6); // Max number of reference frames
  86. writer.WriteBit(false);
  87. writer.WriteUe(context.h264_parameter_set.pic_width_in_mbs - 1);
  88. writer.WriteUe(pic_height - 1);
  89. writer.WriteBit(context.h264_parameter_set.frame_mbs_only_flag != 0);
  90. if (!context.h264_parameter_set.frame_mbs_only_flag) {
  91. writer.WriteBit(context.h264_parameter_set.flags.mbaff_frame.Value() != 0);
  92. }
  93. writer.WriteBit(context.h264_parameter_set.flags.direct_8x8_inference.Value() != 0);
  94. writer.WriteBit(false); // Frame cropping flag
  95. writer.WriteBit(false); // VUI parameter present flag
  96. writer.End();
  97. // H264 PPS
  98. writer.WriteU(1, 24);
  99. writer.WriteU(0, 1);
  100. writer.WriteU(3, 2);
  101. writer.WriteU(8, 5);
  102. writer.WriteUe(0);
  103. writer.WriteUe(0);
  104. writer.WriteBit(context.h264_parameter_set.entropy_coding_mode_flag != 0);
  105. writer.WriteBit(false);
  106. writer.WriteUe(0);
  107. writer.WriteUe(context.h264_parameter_set.num_refidx_l0_default_active);
  108. writer.WriteUe(context.h264_parameter_set.num_refidx_l1_default_active);
  109. writer.WriteBit(context.h264_parameter_set.flags.weighted_pred.Value() != 0);
  110. writer.WriteU(static_cast<s32>(context.h264_parameter_set.weighted_bipred_idc.Value()), 2);
  111. s32 pic_init_qp = static_cast<s32>(context.h264_parameter_set.pic_init_qp_minus26.Value());
  112. writer.WriteSe(pic_init_qp);
  113. writer.WriteSe(0);
  114. s32 chroma_qp_index_offset =
  115. static_cast<s32>(context.h264_parameter_set.chroma_qp_index_offset.Value());
  116. writer.WriteSe(chroma_qp_index_offset);
  117. writer.WriteBit(context.h264_parameter_set.deblocking_filter_control_present_flag != 0);
  118. writer.WriteBit(context.h264_parameter_set.flags.constrained_intra_pred.Value() != 0);
  119. writer.WriteBit(context.h264_parameter_set.redundant_pic_cnt_present_flag != 0);
  120. writer.WriteBit(context.h264_parameter_set.transform_8x8_mode_flag != 0);
  121. writer.WriteBit(true);
  122. for (s32 index = 0; index < 6; index++) {
  123. writer.WriteBit(true);
  124. std::span<const u8> matrix{context.weight_scale};
  125. writer.WriteScalingList(matrix, index * 16, 16);
  126. }
  127. if (context.h264_parameter_set.transform_8x8_mode_flag) {
  128. for (s32 index = 0; index < 2; index++) {
  129. writer.WriteBit(true);
  130. std::span<const u8> matrix{context.weight_scale_8x8};
  131. writer.WriteScalingList(matrix, index * 64, 64);
  132. }
  133. }
  134. s32 chroma_qp_index_offset2 =
  135. static_cast<s32>(context.h264_parameter_set.second_chroma_qp_index_offset.Value());
  136. writer.WriteSe(chroma_qp_index_offset2);
  137. writer.End();
  138. const auto& encoded_header = writer.GetByteArray();
  139. frame.resize(encoded_header.size() + context.stream_len);
  140. std::memcpy(frame.data(), encoded_header.data(), encoded_header.size());
  141. gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset,
  142. frame.data() + encoded_header.size(), context.stream_len);
  143. return frame;
  144. }
  145. H264BitWriter::H264BitWriter() = default;
  146. H264BitWriter::~H264BitWriter() = default;
  147. void H264BitWriter::WriteU(s32 value, s32 value_sz) {
  148. WriteBits(value, value_sz);
  149. }
  150. void H264BitWriter::WriteSe(s32 value) {
  151. WriteExpGolombCodedInt(value);
  152. }
  153. void H264BitWriter::WriteUe(u32 value) {
  154. WriteExpGolombCodedUInt(value);
  155. }
  156. void H264BitWriter::End() {
  157. WriteBit(true);
  158. Flush();
  159. }
  160. void H264BitWriter::WriteBit(bool state) {
  161. WriteBits(state ? 1 : 0, 1);
  162. }
  163. void H264BitWriter::WriteScalingList(std::span<const u8> list, s32 start, s32 count) {
  164. std::vector<u8> scan(count);
  165. if (count == 16) {
  166. std::memcpy(scan.data(), zig_zag_scan.data(), scan.size());
  167. } else {
  168. std::memcpy(scan.data(), zig_zag_direct.data(), scan.size());
  169. }
  170. u8 last_scale = 8;
  171. for (s32 index = 0; index < count; index++) {
  172. const u8 value = list[start + scan[index]];
  173. const s32 delta_scale = static_cast<s32>(value - last_scale);
  174. WriteSe(delta_scale);
  175. last_scale = value;
  176. }
  177. }
  178. std::vector<u8>& H264BitWriter::GetByteArray() {
  179. return byte_array;
  180. }
  181. const std::vector<u8>& H264BitWriter::GetByteArray() const {
  182. return byte_array;
  183. }
  184. void H264BitWriter::WriteBits(s32 value, s32 bit_count) {
  185. s32 value_pos = 0;
  186. s32 remaining = bit_count;
  187. while (remaining > 0) {
  188. s32 copy_size = remaining;
  189. const s32 free_bits = GetFreeBufferBits();
  190. if (copy_size > free_bits) {
  191. copy_size = free_bits;
  192. }
  193. const s32 mask = (1 << copy_size) - 1;
  194. const s32 src_shift = (bit_count - value_pos) - copy_size;
  195. const s32 dst_shift = (buffer_size - buffer_pos) - copy_size;
  196. buffer |= ((value >> src_shift) & mask) << dst_shift;
  197. value_pos += copy_size;
  198. buffer_pos += copy_size;
  199. remaining -= copy_size;
  200. }
  201. }
  202. void H264BitWriter::WriteExpGolombCodedInt(s32 value) {
  203. const s32 sign = value <= 0 ? 0 : 1;
  204. if (value < 0) {
  205. value = -value;
  206. }
  207. value = (value << 1) - sign;
  208. WriteExpGolombCodedUInt(value);
  209. }
  210. void H264BitWriter::WriteExpGolombCodedUInt(u32 value) {
  211. const s32 size = 32 - std::countl_zero(value + 1);
  212. WriteBits(1, size);
  213. value -= (1U << (size - 1)) - 1;
  214. WriteBits(static_cast<s32>(value), size - 1);
  215. }
  216. s32 H264BitWriter::GetFreeBufferBits() {
  217. if (buffer_pos == buffer_size) {
  218. Flush();
  219. }
  220. return buffer_size - buffer_pos;
  221. }
  222. void H264BitWriter::Flush() {
  223. if (buffer_pos == 0) {
  224. return;
  225. }
  226. byte_array.push_back(static_cast<u8>(buffer));
  227. buffer = 0;
  228. buffer_pos = 0;
  229. }
  230. } // namespace Tegra::Decoder