h264.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "common/bit_util.h"
  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. std::vector<u8>& H264::ComposeFrameHeader(NvdecCommon::NvdecRegisters& state, bool is_first_frame) {
  41. H264DecoderContext context{};
  42. gpu.MemoryManager().ReadBlock(state.picture_info_offset, &context, sizeof(H264DecoderContext));
  43. const s32 frame_number = static_cast<s32>((context.h264_parameter_set.flags >> 46) & 0x1ffff);
  44. if (!is_first_frame && frame_number != 0) {
  45. frame.resize(context.frame_data_size);
  46. gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset, frame.data(), frame.size());
  47. } else {
  48. /// Encode header
  49. H264BitWriter writer{};
  50. writer.WriteU(1, 24);
  51. writer.WriteU(0, 1);
  52. writer.WriteU(3, 2);
  53. writer.WriteU(7, 5);
  54. writer.WriteU(100, 8);
  55. writer.WriteU(0, 8);
  56. writer.WriteU(31, 8);
  57. writer.WriteUe(0);
  58. const auto chroma_format_idc =
  59. static_cast<u32>((context.h264_parameter_set.flags >> 12) & 3);
  60. writer.WriteUe(chroma_format_idc);
  61. if (chroma_format_idc == 3) {
  62. writer.WriteBit(false);
  63. }
  64. writer.WriteUe(0);
  65. writer.WriteUe(0);
  66. writer.WriteBit(false); // QpprimeYZeroTransformBypassFlag
  67. writer.WriteBit(false); // Scaling matrix present flag
  68. const auto order_cnt_type = static_cast<u32>((context.h264_parameter_set.flags >> 14) & 3);
  69. writer.WriteUe(static_cast<u32>((context.h264_parameter_set.flags >> 8) & 0xf));
  70. writer.WriteUe(order_cnt_type);
  71. if (order_cnt_type == 0) {
  72. writer.WriteUe(context.h264_parameter_set.log2_max_pic_order_cnt);
  73. } else if (order_cnt_type == 1) {
  74. writer.WriteBit(context.h264_parameter_set.delta_pic_order_always_zero_flag != 0);
  75. writer.WriteSe(0);
  76. writer.WriteSe(0);
  77. writer.WriteUe(0);
  78. }
  79. const s32 pic_height = context.h264_parameter_set.pic_height_in_map_units /
  80. (context.h264_parameter_set.frame_mbs_only_flag ? 1 : 2);
  81. writer.WriteUe(16);
  82. writer.WriteBit(false);
  83. writer.WriteUe(context.h264_parameter_set.pic_width_in_mbs - 1);
  84. writer.WriteUe(pic_height - 1);
  85. writer.WriteBit(context.h264_parameter_set.frame_mbs_only_flag != 0);
  86. if (!context.h264_parameter_set.frame_mbs_only_flag) {
  87. writer.WriteBit(((context.h264_parameter_set.flags >> 0) & 1) != 0);
  88. }
  89. writer.WriteBit(((context.h264_parameter_set.flags >> 1) & 1) != 0);
  90. writer.WriteBit(false); // Frame cropping flag
  91. writer.WriteBit(false); // VUI parameter present flag
  92. writer.End();
  93. // H264 PPS
  94. writer.WriteU(1, 24);
  95. writer.WriteU(0, 1);
  96. writer.WriteU(3, 2);
  97. writer.WriteU(8, 5);
  98. writer.WriteUe(0);
  99. writer.WriteUe(0);
  100. writer.WriteBit(context.h264_parameter_set.entropy_coding_mode_flag != 0);
  101. writer.WriteBit(false);
  102. writer.WriteUe(0);
  103. writer.WriteUe(context.h264_parameter_set.num_refidx_l0_default_active);
  104. writer.WriteUe(context.h264_parameter_set.num_refidx_l1_default_active);
  105. writer.WriteBit(((context.h264_parameter_set.flags >> 2) & 1) != 0);
  106. writer.WriteU(static_cast<s32>((context.h264_parameter_set.flags >> 32) & 0x3), 2);
  107. s32 pic_init_qp = static_cast<s32>((context.h264_parameter_set.flags >> 16) & 0x3f);
  108. pic_init_qp = (pic_init_qp << 26) >> 26;
  109. writer.WriteSe(pic_init_qp);
  110. writer.WriteSe(0);
  111. s32 chroma_qp_index_offset =
  112. static_cast<s32>((context.h264_parameter_set.flags >> 22) & 0x1f);
  113. chroma_qp_index_offset = (chroma_qp_index_offset << 27) >> 27;
  114. writer.WriteSe(chroma_qp_index_offset);
  115. writer.WriteBit(context.h264_parameter_set.deblocking_filter_control_flag != 0);
  116. writer.WriteBit(((context.h264_parameter_set.flags >> 3) & 1) != 0);
  117. writer.WriteBit(context.h264_parameter_set.redundant_pic_count_flag != 0);
  118. writer.WriteBit(context.h264_parameter_set.transform_8x8_mode_flag != 0);
  119. writer.WriteBit(true);
  120. for (s32 index = 0; index < 6; index++) {
  121. writer.WriteBit(true);
  122. const auto matrix_x4 =
  123. std::vector<u8>(context.scaling_matrix_4.begin(), context.scaling_matrix_4.end());
  124. writer.WriteScalingList(matrix_x4, index * 16, 16);
  125. }
  126. if (context.h264_parameter_set.transform_8x8_mode_flag) {
  127. for (s32 index = 0; index < 2; index++) {
  128. writer.WriteBit(true);
  129. const auto matrix_x8 = std::vector<u8>(context.scaling_matrix_8.begin(),
  130. context.scaling_matrix_8.end());
  131. writer.WriteScalingList(matrix_x8, index * 64, 64);
  132. }
  133. }
  134. s32 chroma_qp_index_offset2 =
  135. static_cast<s32>((context.h264_parameter_set.flags >> 27) & 0x1f);
  136. chroma_qp_index_offset2 = (chroma_qp_index_offset2 << 27) >> 27;
  137. writer.WriteSe(chroma_qp_index_offset2);
  138. writer.End();
  139. const auto& encoded_header = writer.GetByteArray();
  140. frame.resize(encoded_header.size() + context.frame_data_size);
  141. std::memcpy(frame.data(), encoded_header.data(), encoded_header.size());
  142. gpu.MemoryManager().ReadBlock(state.frame_bitstream_offset,
  143. frame.data() + encoded_header.size(),
  144. context.frame_data_size);
  145. }
  146. return frame;
  147. }
  148. H264BitWriter::H264BitWriter() = default;
  149. H264BitWriter::~H264BitWriter() = default;
  150. void H264BitWriter::WriteU(s32 value, s32 value_sz) {
  151. WriteBits(value, value_sz);
  152. }
  153. void H264BitWriter::WriteSe(s32 value) {
  154. WriteExpGolombCodedInt(value);
  155. }
  156. void H264BitWriter::WriteUe(u32 value) {
  157. WriteExpGolombCodedUInt(value);
  158. }
  159. void H264BitWriter::End() {
  160. WriteBit(true);
  161. Flush();
  162. }
  163. void H264BitWriter::WriteBit(bool state) {
  164. WriteBits(state ? 1 : 0, 1);
  165. }
  166. void H264BitWriter::WriteScalingList(const std::vector<u8>& list, s32 start, s32 count) {
  167. std::vector<u8> scan(count);
  168. if (count == 16) {
  169. std::memcpy(scan.data(), zig_zag_scan.data(), scan.size());
  170. } else {
  171. std::memcpy(scan.data(), zig_zag_direct.data(), scan.size());
  172. }
  173. u8 last_scale = 8;
  174. for (s32 index = 0; index < count; index++) {
  175. const u8 value = list[start + scan[index]];
  176. const s32 delta_scale = static_cast<s32>(value - last_scale);
  177. WriteSe(delta_scale);
  178. last_scale = value;
  179. }
  180. }
  181. std::vector<u8>& H264BitWriter::GetByteArray() {
  182. return byte_array;
  183. }
  184. const std::vector<u8>& H264BitWriter::GetByteArray() const {
  185. return byte_array;
  186. }
  187. void H264BitWriter::WriteBits(s32 value, s32 bit_count) {
  188. s32 value_pos = 0;
  189. s32 remaining = bit_count;
  190. while (remaining > 0) {
  191. s32 copy_size = remaining;
  192. const s32 free_bits = GetFreeBufferBits();
  193. if (copy_size > free_bits) {
  194. copy_size = free_bits;
  195. }
  196. const s32 mask = (1 << copy_size) - 1;
  197. const s32 src_shift = (bit_count - value_pos) - copy_size;
  198. const s32 dst_shift = (buffer_size - buffer_pos) - copy_size;
  199. buffer |= ((value >> src_shift) & mask) << dst_shift;
  200. value_pos += copy_size;
  201. buffer_pos += copy_size;
  202. remaining -= copy_size;
  203. }
  204. }
  205. void H264BitWriter::WriteExpGolombCodedInt(s32 value) {
  206. const s32 sign = value <= 0 ? 0 : 1;
  207. if (value < 0) {
  208. value = -value;
  209. }
  210. value = (value << 1) - sign;
  211. WriteExpGolombCodedUInt(value);
  212. }
  213. void H264BitWriter::WriteExpGolombCodedUInt(u32 value) {
  214. const s32 size = 32 - Common::CountLeadingZeroes32(static_cast<s32>(value + 1));
  215. WriteBits(1, size);
  216. value -= (1U << (size - 1)) - 1;
  217. WriteBits(static_cast<s32>(value), size - 1);
  218. }
  219. s32 H264BitWriter::GetFreeBufferBits() {
  220. if (buffer_pos == buffer_size) {
  221. Flush();
  222. }
  223. return buffer_size - buffer_pos;
  224. }
  225. void H264BitWriter::Flush() {
  226. if (buffer_pos == 0) {
  227. return;
  228. }
  229. byte_array.push_back(static_cast<u8>(buffer));
  230. buffer = 0;
  231. buffer_pos = 0;
  232. }
  233. } // namespace Tegra::Decoder